Skip to content

Commit

Permalink
Investigating #7782 - catch when Postgres planning removes all Citus
Browse files Browse the repository at this point in the history
tables
  • Loading branch information
colm-mchugh committed Feb 20, 2025
1 parent 459c283 commit 1c472be
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/backend/distributed/planner/distributed_planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,28 @@ distributed_planner(Query *parse,
planContext.plan = standard_planner(planContext.query, NULL,
planContext.cursorOptions,
planContext.boundParams);

if (needsDistributedPlanning)
{
Node *origQuals = planContext.originalQuery->jointree->quals;
Node *plannedQuals = planContext.query->jointree->quals;

/*
* The WHERE quals have been eliminated by the Postgres planner, possibly by
* an OR clause that was simplified to TRUE. In such cases, we need to check
* if planned query still requires distributed planning.
*/
if (origQuals != NULL && plannedQuals == NULL)
{
List *rtesPostPlan = ExtractRangeTableEntryList(planContext.query);
if (list_length(rtesPostPlan) < list_length(rangeTableList))
{
needsDistributedPlanning = ListContainsDistributedTableRTE(

Check warning on line 291 in src/backend/distributed/planner/distributed_planner.c

View check run for this annotation

Codecov / codecov/patch

src/backend/distributed/planner/distributed_planner.c#L291

Added line #L291 was not covered by tests
rtesPostPlan, NULL);
}
}
}

if (needsDistributedPlanning)
{
result = PlanDistributedStmt(&planContext, rteIdCounter);
Expand Down
46 changes: 46 additions & 0 deletions src/test/regress/expected/subquery_in_where.out
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,53 @@ WHERE (SELECT COUNT(DISTINCT e1.value_2)

(1 row)

-- Test redundant WHERE clause (fix #7782, #7783)
create table t0 (vkey int4, pkey int4, c0 timestamp);
create table t1 (vkey int4, pkey int4, c4 timestamp, c5 text, c6 text);
create table t3 (vkey int4, pkey int4, c9 timestamp);
create table t7 (vkey int4, pkey int4);
insert into t0 (vkey, pkey, c0) values
(3, 13000, make_timestamp(2032, 9, 4, 13, 38, 0));
SELECT create_reference_table('t1');
create_reference_table
---------------------------------------------------------------------

(1 row)

SELECT create_distributed_table('t3', 'c9');
create_distributed_table
---------------------------------------------------------------------

(1 row)

update t0 set vkey = 117
where (((t0.pkey) in (select t7.vkey from t7 where false
union all
select t3.pkey from t3 where false
)))
or TRUE;
-- Local table t0 is updated
SELECT vkey, pkey, c0 FROM t0;
vkey | pkey | c0
---------------------------------------------------------------------
117 | 13000 | Sat Sep 04 13:38:00 2032
(1 row)

delete from t0
where TRUE or (((t0.vkey) >= (select
pg_catalog.regexp_count(ref_0.c5, ref_0.c6)
from t1 as ref_0 where true)));
-- Local table t0 is now empty (0 rows)
SELECT vkey, pkey, c0 FROM t0;
vkey | pkey | c0
---------------------------------------------------------------------
(0 rows)

SET client_min_messages TO DEFAULT;
DROP TABLE local_table;
DROP TABLE t0;
DROP TABLE t1;
DROP TABLE t3;
DROP TABLE t7;
DROP SCHEMA subquery_in_where CASCADE;
SET search_path TO public;
34 changes: 34 additions & 0 deletions src/test/regress/sql/subquery_in_where.sql
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,42 @@ WHERE (SELECT COUNT(DISTINCT e1.value_2)
WHERE e1.user_id = u1.user_id
) > 115 AND false;

-- Test redundant WHERE clause (fix #7782, #7783)
create table t0 (vkey int4, pkey int4, c0 timestamp);
create table t1 (vkey int4, pkey int4, c4 timestamp, c5 text, c6 text);
create table t3 (vkey int4, pkey int4, c9 timestamp);
create table t7 (vkey int4, pkey int4);

insert into t0 (vkey, pkey, c0) values
(3, 13000, make_timestamp(2032, 9, 4, 13, 38, 0));

SELECT create_reference_table('t1');
SELECT create_distributed_table('t3', 'c9');

update t0 set vkey = 117
where (((t0.pkey) in (select t7.vkey from t7 where false
union all
select t3.pkey from t3 where false
)))
or TRUE;

-- Local table t0 is updated
SELECT vkey, pkey, c0 FROM t0;

delete from t0
where TRUE or (((t0.vkey) >= (select
pg_catalog.regexp_count(ref_0.c5, ref_0.c6)
from t1 as ref_0 where true)));

-- Local table t0 is now empty (0 rows)
SELECT vkey, pkey, c0 FROM t0;

SET client_min_messages TO DEFAULT;

DROP TABLE local_table;
DROP TABLE t0;
DROP TABLE t1;
DROP TABLE t3;
DROP TABLE t7;
DROP SCHEMA subquery_in_where CASCADE;
SET search_path TO public;

0 comments on commit 1c472be

Please sign in to comment.