Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/backend/cdb/cdbllize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1423,16 +1423,28 @@ makeFlow(FlowType flotype, int numsegments)

/*
* Is the node a "subclass" of Plan?
*
* Node tags are generated by gen_node_support.pl in the order the structs
* appear in plannodes.h, so the T_Plan_Start..T_Plan_End range is not exact:
* it contains a few non-Plan nodes defined among the Plan nodes, and misses
* PartitionSelector, which is defined after PlanRowMark.
*/
bool
is_plan_node(Node *node)
{
if (node == NULL)
return false;

if (nodeTag(node) >= T_Plan_Start && nodeTag(node) < T_Plan_End)
return true;
return false;
switch (nodeTag(node))
{
case T_ExternalScanInfo:
case T_NestLoopParam:
return false;
case T_PartitionSelector:
return true;
default:
return nodeTag(node) >= T_Plan_Start && nodeTag(node) < T_Plan_End;
}
}

#define SANITY_MOTION 0x1
Expand Down
6 changes: 3 additions & 3 deletions src/backend/nodes/gen_node_support.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1433,11 +1433,11 @@ sub elem
}
! unless $no_read;

# for out and read fast
# for out and read fast: the binary format has no field labels,
# and the reader below expects a length-prefixed string.
print $ofaf q{
/* CustomName is a key to lookup CustomScanMethods */
appendStringInfoString(str, " :methods ");
outToken(str, node->methods->CustomName);
WRITE_STRING_VAR(node->methods->CustomName);
};
print $rfaf q!
{
Expand Down
13 changes: 11 additions & 2 deletions src/backend/nodes/outfast.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,15 +833,24 @@ static void
_outExtensibleNode(StringInfo str, const ExtensibleNode *node)
{
const ExtensibleNodeMethods *methods;
StringInfoData buf;

methods = GetExtensibleNodeMethods(node->extnodename, false);

WRITE_NODE_TYPE("EXTENSIBLENODE");

WRITE_STRING_FIELD(extnodename);

/* serialize the private fields */
methods->nodeOut(str, node);
/*
* Serialize the private fields. nodeOut() emits the text format, so
* collect it in a separate buffer and write it as a length-prefixed
* string: _readExtensibleNode() reads it back with READ_STRING_VAR()
* and hands it to nodeRead() through pg_strtok().
*/
initStringInfo(&buf);
methods->nodeOut(&buf, node);
WRITE_STRING_VAR(buf.data);
pfree(buf.data);
}

static void
Expand Down
2 changes: 2 additions & 0 deletions src/include/utils/tuplesort.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ extern void tuplesort_initialize_shared(Sharedsort *shared, int nWorkers,
dsm_segment *seg);
extern void tuplesort_attach_shared(Sharedsort *shared, dsm_segment *seg);

struct Instrumentation;

extern void tuplesort_set_instrument(Tuplesortstate *state,
struct Instrumentation *instrument,
struct StringInfoData *explainbuf);
Expand Down
66 changes: 66 additions & 0 deletions src/test/regress/expected/shared_scan.out
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,69 @@ WITH

RESET statement_timeout;
DROP TABLE ss_t1, ss_t2;
-- A Shared Scan below a Partition Selector must be processed like any other
-- Shared Scan. PartitionSelector used to be missed by is_plan_node(), so the
-- shared input walker didn't descend into its subtree, and executing the
-- query failed with "cannot execute alien Share Input Scan".
CREATE TABLE ss_pt (a int, b int) DISTRIBUTED BY (a)
PARTITION BY RANGE (b) (START (0) END (10) EVERY (1));
CREATE TABLE ss_t (a int, b int) DISTRIBUTED BY (a);
INSERT INTO ss_pt SELECT i, i % 10 FROM generate_series(1, 1000) i;
INSERT INTO ss_t SELECT i, i % 3 FROM generate_series(1, 30) i;
ANALYZE ss_pt;
ANALYZE ss_t;
SET optimizer = off;
SET gp_cte_sharing = on;
EXPLAIN (COSTS OFF)
WITH c AS (SELECT a, b FROM ss_t WHERE a < 20)
SELECT count(*) FROM ss_pt JOIN c ON ss_pt.b = c.b
UNION ALL
SELECT count(*) FROM c;
QUERY PLAN
----------------------------------------------------------------------------------------
Append
-> Finalize Aggregate
-> Gather Motion 3:1 (slice1; segments: 3)
-> Partial Aggregate
-> Hash Join
Hash Cond: (ss_pt.b = c.b)
-> Append
Partition Selectors: $0
-> Seq Scan on ss_pt_1_prt_1 ss_pt_1
-> Seq Scan on ss_pt_1_prt_2 ss_pt_2
-> Seq Scan on ss_pt_1_prt_3 ss_pt_3
-> Seq Scan on ss_pt_1_prt_4 ss_pt_4
-> Seq Scan on ss_pt_1_prt_5 ss_pt_5
-> Seq Scan on ss_pt_1_prt_6 ss_pt_6
-> Seq Scan on ss_pt_1_prt_7 ss_pt_7
-> Seq Scan on ss_pt_1_prt_8 ss_pt_8
-> Seq Scan on ss_pt_1_prt_9 ss_pt_9
-> Seq Scan on ss_pt_1_prt_10 ss_pt_10
-> Hash
-> Partition Selector (selector id: $0)
-> Broadcast Motion 3:3 (slice2; segments: 3)
-> Subquery Scan on c
-> Shared Scan (share slice:id 2:0)
-> Seq Scan on ss_t
Filter: (a < 20)
-> Finalize Aggregate
-> Gather Motion 3:1 (slice3; segments: 3)
-> Partial Aggregate
-> Shared Scan (share slice:id 3:0)
Optimizer: Postgres query optimizer
(30 rows)

WITH c AS (SELECT a, b FROM ss_t WHERE a < 20)
SELECT count(*) FROM ss_pt JOIN c ON ss_pt.b = c.b
UNION ALL
SELECT count(*) FROM c
ORDER BY 1;
count
-------
19
1900
(2 rows)

RESET gp_cte_sharing;
RESET optimizer;
DROP TABLE ss_pt, ss_t;
66 changes: 66 additions & 0 deletions src/test/regress/expected/shared_scan_optimizer.out
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,69 @@ WITH

RESET statement_timeout;
DROP TABLE ss_t1, ss_t2;
-- A Shared Scan below a Partition Selector must be processed like any other
-- Shared Scan. PartitionSelector used to be missed by is_plan_node(), so the
-- shared input walker didn't descend into its subtree, and executing the
-- query failed with "cannot execute alien Share Input Scan".
CREATE TABLE ss_pt (a int, b int) DISTRIBUTED BY (a)
PARTITION BY RANGE (b) (START (0) END (10) EVERY (1));
CREATE TABLE ss_t (a int, b int) DISTRIBUTED BY (a);
INSERT INTO ss_pt SELECT i, i % 10 FROM generate_series(1, 1000) i;
INSERT INTO ss_t SELECT i, i % 3 FROM generate_series(1, 30) i;
ANALYZE ss_pt;
ANALYZE ss_t;
SET optimizer = off;
SET gp_cte_sharing = on;
EXPLAIN (COSTS OFF)
WITH c AS (SELECT a, b FROM ss_t WHERE a < 20)
SELECT count(*) FROM ss_pt JOIN c ON ss_pt.b = c.b
UNION ALL
SELECT count(*) FROM c;
QUERY PLAN
----------------------------------------------------------------------------------------
Append
-> Finalize Aggregate
-> Gather Motion 3:1 (slice1; segments: 3)
-> Partial Aggregate
-> Hash Join
Hash Cond: (ss_pt.b = c.b)
-> Append
Partition Selectors: $0
-> Seq Scan on ss_pt_1_prt_1 ss_pt_1
-> Seq Scan on ss_pt_1_prt_2 ss_pt_2
-> Seq Scan on ss_pt_1_prt_3 ss_pt_3
-> Seq Scan on ss_pt_1_prt_4 ss_pt_4
-> Seq Scan on ss_pt_1_prt_5 ss_pt_5
-> Seq Scan on ss_pt_1_prt_6 ss_pt_6
-> Seq Scan on ss_pt_1_prt_7 ss_pt_7
-> Seq Scan on ss_pt_1_prt_8 ss_pt_8
-> Seq Scan on ss_pt_1_prt_9 ss_pt_9
-> Seq Scan on ss_pt_1_prt_10 ss_pt_10
-> Hash
-> Partition Selector (selector id: $0)
-> Broadcast Motion 3:3 (slice2; segments: 3)
-> Subquery Scan on c
-> Shared Scan (share slice:id 2:0)
-> Seq Scan on ss_t
Filter: (a < 20)
-> Finalize Aggregate
-> Gather Motion 3:1 (slice3; segments: 3)
-> Partial Aggregate
-> Shared Scan (share slice:id 3:0)
Optimizer: Postgres query optimizer
(30 rows)

WITH c AS (SELECT a, b FROM ss_t WHERE a < 20)
SELECT count(*) FROM ss_pt JOIN c ON ss_pt.b = c.b
UNION ALL
SELECT count(*) FROM c
ORDER BY 1;
count
-------
19
1900
(2 rows)

RESET gp_cte_sharing;
RESET optimizer;
DROP TABLE ss_pt, ss_t;
28 changes: 28 additions & 0 deletions src/test/regress/sql/shared_scan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,31 @@ WITH
LIMIT 1;
RESET statement_timeout;
DROP TABLE ss_t1, ss_t2;

-- A Shared Scan below a Partition Selector must be processed like any other
-- Shared Scan. PartitionSelector used to be missed by is_plan_node(), so the
-- shared input walker didn't descend into its subtree, and executing the
-- query failed with "cannot execute alien Share Input Scan".
CREATE TABLE ss_pt (a int, b int) DISTRIBUTED BY (a)
PARTITION BY RANGE (b) (START (0) END (10) EVERY (1));
CREATE TABLE ss_t (a int, b int) DISTRIBUTED BY (a);
INSERT INTO ss_pt SELECT i, i % 10 FROM generate_series(1, 1000) i;
INSERT INTO ss_t SELECT i, i % 3 FROM generate_series(1, 30) i;
ANALYZE ss_pt;
ANALYZE ss_t;

SET optimizer = off;
SET gp_cte_sharing = on;
EXPLAIN (COSTS OFF)
WITH c AS (SELECT a, b FROM ss_t WHERE a < 20)
SELECT count(*) FROM ss_pt JOIN c ON ss_pt.b = c.b
UNION ALL
SELECT count(*) FROM c;
WITH c AS (SELECT a, b FROM ss_t WHERE a < 20)
SELECT count(*) FROM ss_pt JOIN c ON ss_pt.b = c.b
UNION ALL
SELECT count(*) FROM c
ORDER BY 1;
RESET gp_cte_sharing;
RESET optimizer;
DROP TABLE ss_pt, ss_t;
Loading