Skip to content

Commit

Permalink
[#20831] YSQL: Show distinct prefix keys explicitly in the explain ou…
Browse files Browse the repository at this point in the history
…tput

Summary:
### Objective

Before this change, the explain output looks as follows

```
yugabyte=# explain select distinct  col_int_key from mm;
                                            QUERY PLAN
---------------------------------------------------------------------------------------------------
 Unique  (cost=0.00..12.00 rows=93 width=4)
   ->  Distinct Index Only Scan using idx_mm_col_int_key on mm  (cost=0.00..12.00 rows=93 width=4)
         Distinct Prefix: 1
(3 rows)
```

We displayed the length of the distinct prefix because that is the sole parameter necessary for the HybridScan on the DocDB side.

However, users may find it more useful if we instead displayed the keys corresponding to the index columns instead. The new output looks

```
yugabyte=# explain select distinct k1 from t;
                                    QUERY PLAN
----------------------------------------------------------------------------------
 Unique  (cost=0.00..22.80 rows=200 width=4)
   ->  Distinct Index Scan using t_pkey on t  (cost=0.00..22.80 rows=200 width=4)
         Distinct Prefix Keys: k1
(3 rows)
```

### Solution

Pick the prefix length number of leading columns from the list of index targets and print them in explain.
Also, change the regression test output to reflect the change.
Jira: DB-9820

Test Plan:
Jenkins
./yb_build.sh --java-test TestPgRegressDistinctPushdown

Reviewers: tnayak

Reviewed By: tnayak

Subscribers: yql

Differential Revision: https://phorge.dev.yugabyte.com/D33042
  • Loading branch information
pao214 committed Mar 15, 2024
1 parent de2e3da commit e75e20d
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 114 deletions.
58 changes: 51 additions & 7 deletions src/postgres/src/backend/commands/explain.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ static void
YbAggregateExplainableRPCRequestStat(ExplainState *es,
const YbInstrumentation *instr);
static void YbExplainDistinctPrefixLen(
int yb_distinct_prefixlen, ExplainState *es);
PlanState *planstate, List *indextlist, int yb_distinct_prefixlen,
ExplainState *es, List *ancestors);
static void show_ybtidbitmap_info(YbBitmapTableScanState *planstate,
ExplainState *es);

Expand Down Expand Up @@ -2413,7 +2414,8 @@ ExplainNode(PlanState *planstate, List *ancestors,
* that's currently the order of operations in DocDB.
*/
YbExplainDistinctPrefixLen(
((IndexScan *) plan)->yb_distinct_prefixlen, es);
planstate, ((IndexScan *) plan)->indextlist,
((IndexScan *) plan)->yb_distinct_prefixlen, es, ancestors);
show_scan_qual(((IndexScan *) plan)->yb_idx_pushdown.quals,
"Storage Index Filter", planstate, ancestors, es);
show_scan_qual(((IndexScan *) plan)->yb_rel_pushdown.quals,
Expand Down Expand Up @@ -2451,7 +2453,8 @@ ExplainNode(PlanState *planstate, List *ancestors,
* that's currently the order of operations in DocDB.
*/
YbExplainDistinctPrefixLen(
((IndexOnlyScan *) plan)->yb_distinct_prefixlen, es);
planstate, ((IndexOnlyScan *) plan)->indextlist,
((IndexOnlyScan *) plan)->yb_distinct_prefixlen, es, ancestors);
/*
* Storage filter is applied first, so it is output first.
*/
Expand Down Expand Up @@ -5059,15 +5062,56 @@ YbAggregateExplainableRPCRequestStat(ExplainState *es,
* --------------
* Distinct Index Scan
* ...
* Distinct Prefix: <prefix length>
* Distinct Keys: <Index Prefix Keys>
* ...
*
* Adds Distinct Prefix to explain info
*/
static void
YbExplainDistinctPrefixLen(int yb_distinct_prefixlen, ExplainState *es)
YbExplainDistinctPrefixLen(PlanState *planstate, List *indextlist,
int yb_distinct_prefixlen, ExplainState *es,
List *ancestors)
{
if (yb_distinct_prefixlen > 0)
ExplainPropertyInteger(
"Distinct Prefix", NULL, yb_distinct_prefixlen, es);
{
/* Print distinct prefix keys. */
List *context;
List *result = NIL;
StringInfoData distinct_prefix_key_buf;
bool useprefix;
int keyno;
ListCell *tlelc;

initStringInfo(&distinct_prefix_key_buf);

/* Set up deparsing context */
context = set_deparse_context_planstate(es->deparse_cxt,
(Node *) planstate,
ancestors);
useprefix = (list_length(es->rtable) > 1 || es->verbose);

keyno = 0;
foreach(tlelc, indextlist)
{
TargetEntry *indextle;
char *exprstr;

if (keyno >= yb_distinct_prefixlen)
break;

indextle = (TargetEntry *) lfirst(tlelc);

/* Deparse the expression, showing any top-level cast */
exprstr = deparse_expression((Node *) indextle->expr, context,
useprefix, true);
resetStringInfo(&distinct_prefix_key_buf);
appendStringInfoString(&distinct_prefix_key_buf, exprstr);
/* Emit one property-list item per key */
result = lappend(result, pstrdup(distinct_prefix_key_buf.data));

keyno++;
}

ExplainPropertyList("Distinct Keys", result, es);
}
}
2 changes: 1 addition & 1 deletion src/postgres/src/test/regress/expected/yb_aggregates.out
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ EXPLAIN (COSTS OFF) SELECT DISTINCT int_8 FROM ybaggtest;
HashAggregate
Group Key: int_8
-> Distinct Index Only Scan using ybaggtestindex on ybaggtest
Distinct Prefix: 2
Distinct Keys: int_8, int_2
(4 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(distinct int_4), SUM(int_4) FROM ybaggtest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SELECT DISTINCT r1 FROM test_distinct WHERE r1 < 2 ORDER BY r1;
Unique (actual rows=1 loops=1)
-> Distinct Index Scan using test_distinct_pkey on test_distinct (actual rows=2 loops=1)
Index Cond: (r1 < 2)
Distinct Prefix: 1
Distinct Keys: r1
Storage Table Read Requests: 2
Storage Table Rows Scanned: 2
(6 rows)
Expand All @@ -32,7 +32,7 @@ SELECT DISTINCT r1, r2 FROM test_distinct WHERE r1 < 2 OR r2 < 3 ORDER BY r1, r2
---------------------------------------------------------------------------------------------
Unique (actual rows=6 loops=1)
-> Distinct Index Scan using test_distinct_pkey on test_distinct (actual rows=7 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
Storage Filter: ((r1 < 2) OR (r2 < 3))
(4 rows)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1 FROM t;
---------------------------------------------------------------------
Unique (actual rows=2 loops=1)
-> Distinct Index Scan using t_pkey on t (actual rows=3 loops=1)
Distinct Prefix: 1
Distinct Keys: r1
(3 rows)

SELECT DISTINCT r1 FROM t;
Expand All @@ -47,7 +47,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1, r2 FRO
---------------------------------------------------------------------
Unique (actual rows=6 loops=1)
-> Distinct Index Scan using t_pkey on t (actual rows=7 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
(3 rows)

SELECT DISTINCT r1, r2 FROM t;
Expand All @@ -71,7 +71,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r2 FROM t;
HashAggregate (actual rows=3 loops=1)
Group Key: r2
-> Distinct Index Scan using t_pkey on t (actual rows=7 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
(4 rows)

SELECT DISTINCT r2 FROM t;
Expand All @@ -90,7 +90,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1 FROM t
Limit (actual rows=2 loops=1)
-> Unique (actual rows=2 loops=1)
-> Distinct Index Scan using t_pkey on t (actual rows=3 loops=1)
Distinct Prefix: 1
Distinct Keys: r1
(4 rows)

SELECT DISTINCT r1 FROM t LIMIT 2;
Expand All @@ -109,7 +109,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1 FROM tr
-----------------------------------------------------------------------
Unique (actual rows=1 loops=1)
-> Distinct Index Scan using tr_pkey on tr (actual rows=1 loops=1)
Distinct Prefix: 1
Distinct Keys: r1
(3 rows)

SELECT DISTINCT r1 FROM tr;
Expand All @@ -127,7 +127,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1 FROM ts
-----------------------------------------------------------------------
Unique (actual rows=1 loops=1)
-> Distinct Index Scan using ts_pkey on ts (actual rows=1 loops=1)
Distinct Prefix: 1
Distinct Keys: r1
(3 rows)

SELECT DISTINCT r1 FROM ts;
Expand Down Expand Up @@ -170,7 +170,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1 FROM t
---------------------------------------------------------------------
Unique (actual rows=2 loops=1)
-> Distinct Index Scan using t_pkey on t (actual rows=3 loops=1)
Distinct Prefix: 1
Distinct Keys: r1
(3 rows)

SELECT DISTINCT r1 FROM t ORDER BY r1;
Expand All @@ -185,7 +185,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1 FROM t
------------------------------------------------------------------------------
Unique (actual rows=2 loops=1)
-> Distinct Index Scan Backward using t_pkey on t (actual rows=3 loops=1)
Distinct Prefix: 1
Distinct Keys: r1
(3 rows)

SELECT DISTINCT r1 FROM t ORDER BY r1 DESC;
Expand All @@ -201,7 +201,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1, r2 FRO
---------------------------------------------------------------------
Unique (actual rows=6 loops=1)
-> Distinct Index Scan using t_pkey on t (actual rows=7 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
(3 rows)

SELECT DISTINCT r1, r2 FROM t ORDER BY r1, r2;
Expand All @@ -220,7 +220,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1, r2 FRO
------------------------------------------------------------------------------
Unique (actual rows=6 loops=1)
-> Distinct Index Scan Backward using t_pkey on t (actual rows=7 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
(3 rows)

SELECT DISTINCT r1, r2 FROM t ORDER BY r1 DESC, r2 DESC;
Expand All @@ -245,7 +245,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1, r2 FRO
Sort Method: quicksort
-> Unique (actual rows=6 loops=1)
-> Distinct Index Scan using t_pkey on t (actual rows=7 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
(6 rows)

SELECT DISTINCT r1, r2 FROM t ORDER BY r2;
Expand All @@ -271,7 +271,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1, r2 FRO
-----------------------------------------------------------------------
Unique (actual rows=3 loops=1)
-> Distinct Index Scan using tm_pkey on tm (actual rows=3 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
(3 rows)

SELECT DISTINCT r1, r2 FROM tm;
Expand All @@ -288,7 +288,7 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT DISTINCT r1, r2 FRO
--------------------------------------------------------------------------------
Unique (actual rows=3 loops=1)
-> Distinct Index Scan Backward using tm_pkey on tm (actual rows=3 loops=1)
Distinct Prefix: 2
Distinct Keys: r1, r2
(3 rows)

SELECT DISTINCT r1, r2 FROM tm ORDER BY r1, r2 DESC;
Expand Down
Loading

0 comments on commit e75e20d

Please sign in to comment.