Skip to content

Commit 4f367b7

Browse files
committed
using the same table with and without ONLY modifier in select query is prohibited
1 parent f6c5e52 commit 4f367b7

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

expected/pg_pathman.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ SELECT COUNT(*) FROM ONLY test.num_range_rel;
116116
0
117117
(1 row)
118118

119+
SELECT * FROM ONLY test.range_rel UNION SELECT * FROM test.range_rel;
120+
ERROR: It is prohibited to query partitioned tables both with and without ONLY modifier
119121
SET pg_pathman.enable_runtimeappend = OFF;
120122
SET pg_pathman.enable_runtimemergeappend = OFF;
121123
VACUUM;

sql/pg_pathman.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ INSERT INTO test.num_range_rel
4646
SELECT COUNT(*) FROM test.num_range_rel;
4747
SELECT COUNT(*) FROM ONLY test.num_range_rel;
4848

49+
SELECT * FROM ONLY test.range_rel UNION SELECT * FROM test.range_rel;
50+
4951
SET pg_pathman.enable_runtimeappend = OFF;
5052
SET pg_pathman.enable_runtimemergeappend = OFF;
5153

src/hooks.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ pathman_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTb
161161
return;
162162

163163
/* This works only for SELECT queries (at least for now) */
164-
if (root->parse->commandType != CMD_SELECT || !inheritance_disabled)
164+
if (root->parse->commandType != CMD_SELECT ||
165+
!list_member_oid(inheritance_enabled_relids, rte->relid))
165166
return;
166167

167168
/* Lookup partitioning information for parent relation */

src/pathman.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,16 @@ typedef struct PathmanState
126126
DsmArray databases;
127127
} PathmanState;
128128

129-
extern bool inheritance_disabled;
129+
/*
130+
* The list of partitioned relation relids that must be handled by pg_pathman
131+
*/
132+
extern List *inheritance_enabled_relids;
133+
/*
134+
* This list is used to ensure that partitioned relation isn't used both
135+
* with and without ONLY modifiers
136+
*/
137+
extern List *inheritance_disabled_relids;
138+
130139
extern bool pg_pathman_enable;
131140
extern PathmanState *pmstate;
132141

src/pg_pathman.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646

4747
PG_MODULE_MAGIC;
4848

49-
bool inheritance_disabled;
49+
List *inheritance_disabled_relids = NIL;
50+
List *inheritance_enabled_relids = NIL;
5051
bool pg_pathman_enable;
5152
PathmanState *pmstate;
5253

@@ -266,6 +267,11 @@ pathman_post_parse_analysis_hook(ParseState *pstate, Query *query)
266267

267268
if (post_parse_analyze_hook_original)
268269
post_parse_analyze_hook_original(pstate, query);
270+
271+
// list_free(inheritance_disabled_relids);
272+
// list_free(inheritance_enabled_relids);
273+
inheritance_disabled_relids = NIL;
274+
inheritance_enabled_relids = NIL;
269275
}
270276

271277
/*
@@ -280,7 +286,7 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
280286

281287
if (pg_pathman_enable)
282288
{
283-
inheritance_disabled = false;
289+
// inheritance_disabled = false;
284290
switch(parse->commandType)
285291
{
286292
case CMD_SELECT:
@@ -303,6 +309,9 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
303309
else
304310
result = standard_planner(parse, cursorOptions, boundParams);
305311

312+
list_free(inheritance_disabled_relids);
313+
inheritance_disabled_relids = NIL;
314+
306315
return result;
307316
}
308317

@@ -327,6 +336,7 @@ disable_inheritance(Query *parse)
327336
foreach(lc, parse->rtable)
328337
{
329338
rte = (RangeTblEntry*) lfirst(lc);
339+
330340
switch(rte->rtekind)
331341
{
332342
case RTE_RELATION:
@@ -343,9 +353,28 @@ disable_inheritance(Query *parse)
343353
* when user uses ONLY statement from case when we
344354
* make rte->inh false intentionally.
345355
*/
346-
inheritance_disabled = true;
356+
inheritance_enabled_relids = \
357+
lappend_oid(inheritance_enabled_relids, rte->relid);
358+
359+
/*
360+
* Check if relation was already found with ONLY modifier. In
361+
* this case throw an error because we cannot handle
362+
* situations when partitioned table used both with and
363+
* without ONLY modifier in SELECT queries
364+
*/
365+
if (list_member_oid(inheritance_disabled_relids, rte->relid))
366+
goto disable_error;
367+
368+
goto disable_next;
347369
}
348370
}
371+
372+
inheritance_disabled_relids = \
373+
lappend_oid(inheritance_disabled_relids, rte->relid);
374+
375+
/* Check if relation was already found withoud ONLY modifier */
376+
if (list_member_oid(inheritance_enabled_relids, rte->relid))
377+
goto disable_error;
349378
break;
350379
case RTE_SUBQUERY:
351380
/* Recursively disable inheritance for subqueries */
@@ -354,7 +383,16 @@ disable_inheritance(Query *parse)
354383
default:
355384
break;
356385
}
386+
387+
disable_next:
388+
;
357389
}
390+
391+
return;
392+
393+
disable_error:
394+
elog(ERROR, "It is prohibited to query partitioned tables both "
395+
"with and without ONLY modifier");
358396
}
359397

360398
static void

0 commit comments

Comments
 (0)