1515#include "postgres.h"
1616
1717#include "access/htup_details.h"
18- #include "access/sysattr.h"
1918#include "nodes/makefuncs.h"
2019#include "nodes/nodeFuncs.h"
2120#include "optimizer/appendinfo.h"
2221#include "parser/parsetree.h"
23- #include "utils/rel.h"
2422#include "utils/lsyscache.h"
23+ #include "utils/rel.h"
2524#include "utils/syscache.h"
2625
2726
@@ -38,8 +37,6 @@ static void make_inh_translation_list(Relation oldrelation,
3837 List * * translated_vars );
3938static Node * adjust_appendrel_attrs_mutator (Node * node ,
4039 adjust_appendrel_attrs_context * context );
41- static Relids adjust_child_relids (Relids relids , int nappinfos ,
42- AppendRelInfo * * appinfos );
4340static List * adjust_inherited_tlist (List * tlist ,
4441 AppendRelInfo * context );
4542
@@ -166,58 +163,6 @@ make_inh_translation_list(Relation oldrelation, Relation newrelation,
166163 * translated_vars = vars ;
167164}
168165
169- /*
170- * translate_col_privs
171- * Translate a bitmapset representing per-column privileges from the
172- * parent rel's attribute numbering to the child's.
173- *
174- * The only surprise here is that we don't translate a parent whole-row
175- * reference into a child whole-row reference. That would mean requiring
176- * permissions on all child columns, which is overly strict, since the
177- * query is really only going to reference the inherited columns. Instead
178- * we set the per-column bits for all inherited columns.
179- */
180- Bitmapset *
181- translate_col_privs (const Bitmapset * parent_privs ,
182- List * translated_vars )
183- {
184- Bitmapset * child_privs = NULL ;
185- bool whole_row ;
186- int attno ;
187- ListCell * lc ;
188-
189- /* System attributes have the same numbers in all tables */
190- for (attno = FirstLowInvalidHeapAttributeNumber + 1 ; attno < 0 ; attno ++ )
191- {
192- if (bms_is_member (attno - FirstLowInvalidHeapAttributeNumber ,
193- parent_privs ))
194- child_privs = bms_add_member (child_privs ,
195- attno - FirstLowInvalidHeapAttributeNumber );
196- }
197-
198- /* Check if parent has whole-row reference */
199- whole_row = bms_is_member (InvalidAttrNumber - FirstLowInvalidHeapAttributeNumber ,
200- parent_privs );
201-
202- /* And now translate the regular user attributes, using the vars list */
203- attno = InvalidAttrNumber ;
204- foreach (lc , translated_vars )
205- {
206- Var * var = lfirst_node (Var , lc );
207-
208- attno ++ ;
209- if (var == NULL ) /* ignore dropped columns */
210- continue ;
211- if (whole_row ||
212- bms_is_member (attno - FirstLowInvalidHeapAttributeNumber ,
213- parent_privs ))
214- child_privs = bms_add_member (child_privs ,
215- var -> varattno - FirstLowInvalidHeapAttributeNumber );
216- }
217-
218- return child_privs ;
219- }
220-
221166/*
222167 * adjust_appendrel_attrs
223168 * Copy the specified query or expression and translate Vars referring to a
@@ -527,11 +472,53 @@ adjust_appendrel_attrs_mutator(Node *node,
527472 (void * ) context );
528473}
529474
475+ /*
476+ * adjust_appendrel_attrs_multilevel
477+ * Apply Var translations from a toplevel appendrel parent down to a child.
478+ *
479+ * In some cases we need to translate expressions referencing a parent relation
480+ * to reference an appendrel child that's multiple levels removed from it.
481+ */
482+ Node *
483+ adjust_appendrel_attrs_multilevel (PlannerInfo * root , Node * node ,
484+ Relids child_relids ,
485+ Relids top_parent_relids )
486+ {
487+ AppendRelInfo * * appinfos ;
488+ Bitmapset * parent_relids = NULL ;
489+ int nappinfos ;
490+ int cnt ;
491+
492+ Assert (bms_num_members (child_relids ) == bms_num_members (top_parent_relids ));
493+
494+ appinfos = find_appinfos_by_relids (root , child_relids , & nappinfos );
495+
496+ /* Construct relids set for the immediate parent of given child. */
497+ for (cnt = 0 ; cnt < nappinfos ; cnt ++ )
498+ {
499+ AppendRelInfo * appinfo = appinfos [cnt ];
500+
501+ parent_relids = bms_add_member (parent_relids , appinfo -> parent_relid );
502+ }
503+
504+ /* Recurse if immediate parent is not the top parent. */
505+ if (!bms_equal (parent_relids , top_parent_relids ))
506+ node = adjust_appendrel_attrs_multilevel (root , node , parent_relids ,
507+ top_parent_relids );
508+
509+ /* Now translate for this child */
510+ node = adjust_appendrel_attrs (root , node , nappinfos , appinfos );
511+
512+ pfree (appinfos );
513+
514+ return node ;
515+ }
516+
530517/*
531518 * Substitute child relids for parent relids in a Relid set. The array of
532519 * appinfos specifies the substitutions to be performed.
533520 */
534- static Relids
521+ Relids
535522adjust_child_relids (Relids relids , int nappinfos , AppendRelInfo * * appinfos )
536523{
537524 Bitmapset * result = NULL ;
@@ -711,90 +698,6 @@ adjust_inherited_tlist(List *tlist, AppendRelInfo *context)
711698 return new_tlist ;
712699}
713700
714- /*
715- * adjust_appendrel_attrs_multilevel
716- * Apply Var translations from a toplevel appendrel parent down to a child.
717- *
718- * In some cases we need to translate expressions referencing a parent relation
719- * to reference an appendrel child that's multiple levels removed from it.
720- */
721- Node *
722- adjust_appendrel_attrs_multilevel (PlannerInfo * root , Node * node ,
723- Relids child_relids ,
724- Relids top_parent_relids )
725- {
726- AppendRelInfo * * appinfos ;
727- Bitmapset * parent_relids = NULL ;
728- int nappinfos ;
729- int cnt ;
730-
731- Assert (bms_num_members (child_relids ) == bms_num_members (top_parent_relids ));
732-
733- appinfos = find_appinfos_by_relids (root , child_relids , & nappinfos );
734-
735- /* Construct relids set for the immediate parent of given child. */
736- for (cnt = 0 ; cnt < nappinfos ; cnt ++ )
737- {
738- AppendRelInfo * appinfo = appinfos [cnt ];
739-
740- parent_relids = bms_add_member (parent_relids , appinfo -> parent_relid );
741- }
742-
743- /* Recurse if immediate parent is not the top parent. */
744- if (!bms_equal (parent_relids , top_parent_relids ))
745- node = adjust_appendrel_attrs_multilevel (root , node , parent_relids ,
746- top_parent_relids );
747-
748- /* Now translate for this child */
749- node = adjust_appendrel_attrs (root , node , nappinfos , appinfos );
750-
751- pfree (appinfos );
752-
753- return node ;
754- }
755-
756- /*
757- * Construct the SpecialJoinInfo for a child-join by translating
758- * SpecialJoinInfo for the join between parents. left_relids and right_relids
759- * are the relids of left and right side of the join respectively.
760- */
761- SpecialJoinInfo *
762- build_child_join_sjinfo (PlannerInfo * root , SpecialJoinInfo * parent_sjinfo ,
763- Relids left_relids , Relids right_relids )
764- {
765- SpecialJoinInfo * sjinfo = makeNode (SpecialJoinInfo );
766- AppendRelInfo * * left_appinfos ;
767- int left_nappinfos ;
768- AppendRelInfo * * right_appinfos ;
769- int right_nappinfos ;
770-
771- memcpy (sjinfo , parent_sjinfo , sizeof (SpecialJoinInfo ));
772- left_appinfos = find_appinfos_by_relids (root , left_relids ,
773- & left_nappinfos );
774- right_appinfos = find_appinfos_by_relids (root , right_relids ,
775- & right_nappinfos );
776-
777- sjinfo -> min_lefthand = adjust_child_relids (sjinfo -> min_lefthand ,
778- left_nappinfos , left_appinfos );
779- sjinfo -> min_righthand = adjust_child_relids (sjinfo -> min_righthand ,
780- right_nappinfos ,
781- right_appinfos );
782- sjinfo -> syn_lefthand = adjust_child_relids (sjinfo -> syn_lefthand ,
783- left_nappinfos , left_appinfos );
784- sjinfo -> syn_righthand = adjust_child_relids (sjinfo -> syn_righthand ,
785- right_nappinfos ,
786- right_appinfos );
787- sjinfo -> semi_rhs_exprs = (List * ) adjust_appendrel_attrs (root ,
788- (Node * ) sjinfo -> semi_rhs_exprs ,
789- right_nappinfos ,
790- right_appinfos );
791-
792- pfree (left_appinfos );
793- pfree (right_appinfos );
794-
795- return sjinfo ;
796- }
797-
798701/*
799702 * find_appinfos_by_relids
800703 * Find AppendRelInfo structures for all relations specified by relids.
0 commit comments