4646
4747PG_MODULE_MAGIC ;
4848
49- bool inheritance_disabled ;
49+ List * inheritance_disabled_relids = NIL ;
50+ List * inheritance_enabled_relids = NIL ;
5051bool pg_pathman_enable ;
5152PathmanState * 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
360398static void
0 commit comments