LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - inherit.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 97.4 % 233 227
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 8 8
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 84.5 % 148 125

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * inherit.c
       4                 :             :  *    Routines to process child relations in inheritance trees
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *    src/backend/optimizer/util/inherit.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include <limits.h>
      18                 :             : 
      19                 :             : #include "access/sysattr.h"
      20                 :             : #include "access/table.h"
      21                 :             : #include "catalog/partition.h"
      22                 :             : #include "catalog/pg_inherits.h"
      23                 :             : #include "catalog/pg_type.h"
      24                 :             : #include "miscadmin.h"
      25                 :             : #include "nodes/makefuncs.h"
      26                 :             : #include "optimizer/appendinfo.h"
      27                 :             : #include "optimizer/inherit.h"
      28                 :             : #include "optimizer/optimizer.h"
      29                 :             : #include "optimizer/pathnode.h"
      30                 :             : #include "optimizer/plancat.h"
      31                 :             : #include "optimizer/planmain.h"
      32                 :             : #include "optimizer/planner.h"
      33                 :             : #include "optimizer/prep.h"
      34                 :             : #include "optimizer/restrictinfo.h"
      35                 :             : #include "parser/parsetree.h"
      36                 :             : #include "parser/parse_relation.h"
      37                 :             : #include "partitioning/partdesc.h"
      38                 :             : #include "partitioning/partprune.h"
      39                 :             : #include "utils/rel.h"
      40                 :             : 
      41                 :             : 
      42                 :             : static void expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
      43                 :             :                                        RangeTblEntry *parentrte,
      44                 :             :                                        Index parentRTindex, Relation parentrel,
      45                 :             :                                        Bitmapset *parent_updatedCols,
      46                 :             :                                        PlanRowMark *top_parentrc, LOCKMODE lockmode);
      47                 :             : static void expand_single_inheritance_child(PlannerInfo *root,
      48                 :             :                                             RangeTblEntry *parentrte,
      49                 :             :                                             Index parentRTindex, Relation parentrel,
      50                 :             :                                             PlanRowMark *top_parentrc, Relation childrel,
      51                 :             :                                             RangeTblEntry **childrte_p,
      52                 :             :                                             Index *childRTindex_p);
      53                 :             : static Bitmapset *translate_col_privs(const Bitmapset *parent_privs,
      54                 :             :                                       List *translated_vars);
      55                 :             : static Bitmapset *translate_col_privs_multilevel(PlannerInfo *root,
      56                 :             :                                                  RelOptInfo *rel,
      57                 :             :                                                  RelOptInfo *parent_rel,
      58                 :             :                                                  Bitmapset *parent_cols);
      59                 :             : static void expand_appendrel_subquery(PlannerInfo *root, RelOptInfo *rel,
      60                 :             :                                       RangeTblEntry *rte, Index rti);
      61                 :             : 
      62                 :             : 
      63                 :             : /*
      64                 :             :  * expand_inherited_rtentry
      65                 :             :  *      Expand a rangetable entry that has the "inh" bit set.
      66                 :             :  *
      67                 :             :  * "inh" is only allowed in two cases: RELATION and SUBQUERY RTEs.
      68                 :             :  *
      69                 :             :  * "inh" on a plain RELATION RTE means that it is a partitioned table or the
      70                 :             :  * parent of a traditional-inheritance set.  In this case we must add entries
      71                 :             :  * for all the interesting child tables to the query's rangetable, and build
      72                 :             :  * additional planner data structures for them, including RelOptInfos,
      73                 :             :  * AppendRelInfos, and possibly PlanRowMarks.
      74                 :             :  *
      75                 :             :  * Note that the original RTE is considered to represent the whole inheritance
      76                 :             :  * set.  In the case of traditional inheritance, the first of the generated
      77                 :             :  * RTEs is an RTE for the same table, but with inh = false, to represent the
      78                 :             :  * parent table in its role as a simple member of the inheritance set.  For
      79                 :             :  * partitioning, we don't need a second RTE because the partitioned table
      80                 :             :  * itself has no data and need not be scanned.
      81                 :             :  *
      82                 :             :  * "inh" on a SUBQUERY RTE means that it's the parent of a UNION ALL group,
      83                 :             :  * which is treated as an appendrel similarly to inheritance cases; however,
      84                 :             :  * we already made RTEs and AppendRelInfos for the subqueries.  We only need
      85                 :             :  * to build RelOptInfos for them, which is done by expand_appendrel_subquery.
      86                 :             :  */
      87                 :             : void
      88                 :       17953 : expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel,
      89                 :             :                          RangeTblEntry *rte, Index rti)
      90                 :             : {
      91                 :             :     Oid         parentOID;
      92                 :             :     Relation    oldrelation;
      93                 :             :     LOCKMODE    lockmode;
      94                 :             :     PlanRowMark *oldrc;
      95                 :       17953 :     bool        old_isParent = false;
      96                 :       17953 :     int         old_allMarkTypes = 0;
      97                 :             : 
      98                 :             :     Assert(rte->inh);            /* else caller error */
      99                 :             : 
     100         [ +  + ]:       17953 :     if (rte->rtekind == RTE_SUBQUERY)
     101                 :             :     {
     102                 :        4431 :         expand_appendrel_subquery(root, rel, rte, rti);
     103                 :        4431 :         return;
     104                 :             :     }
     105                 :             : 
     106                 :             :     Assert(rte->rtekind == RTE_RELATION);
     107                 :             : 
     108                 :       13522 :     parentOID = rte->relid;
     109                 :             : 
     110                 :             :     /*
     111                 :             :      * We used to check has_subclass() here, but there's no longer any need
     112                 :             :      * to, because subquery_planner already did.
     113                 :             :      */
     114                 :             : 
     115                 :             :     /*
     116                 :             :      * The rewriter should already have obtained an appropriate lock on each
     117                 :             :      * relation named in the query, so we can open the parent relation without
     118                 :             :      * locking it.  However, for each child relation we add to the query, we
     119                 :             :      * must obtain an appropriate lock, because this will be the first use of
     120                 :             :      * those relations in the parse/rewrite/plan pipeline.  Child rels should
     121                 :             :      * use the same lockmode as their parent.
     122                 :             :      */
     123                 :       13522 :     oldrelation = table_open(parentOID, NoLock);
     124                 :       13522 :     lockmode = rte->rellockmode;
     125                 :             : 
     126                 :             :     /*
     127                 :             :      * If parent relation is selected FOR UPDATE/SHARE, we need to mark its
     128                 :             :      * PlanRowMark as isParent = true, and generate a new PlanRowMark for each
     129                 :             :      * child.
     130                 :             :      */
     131                 :       13522 :     oldrc = get_plan_rowmark(root->rowMarks, rti);
     132         [ +  + ]:       13522 :     if (oldrc)
     133                 :             :     {
     134                 :        1294 :         old_isParent = oldrc->isParent;
     135                 :        1294 :         oldrc->isParent = true;
     136                 :             :         /* Save initial value of allMarkTypes before children add to it */
     137                 :        1294 :         old_allMarkTypes = oldrc->allMarkTypes;
     138                 :             :     }
     139                 :             : 
     140                 :             :     /* Scan the inheritance set and expand it */
     141         [ +  + ]:       13522 :     if (oldrelation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     142                 :             :     {
     143                 :             :         RTEPermissionInfo *perminfo;
     144                 :             : 
     145                 :       11132 :         perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
     146                 :             : 
     147                 :             :         /*
     148                 :             :          * Partitioned table, so set up for partitioning.
     149                 :             :          */
     150                 :             :         Assert(rte->relkind == RELKIND_PARTITIONED_TABLE);
     151                 :             : 
     152                 :             :         /*
     153                 :             :          * Recursively expand and lock the partitions.  While at it, also
     154                 :             :          * extract the partition key columns of all the partitioned tables.
     155                 :             :          */
     156                 :       11132 :         expand_partitioned_rtentry(root, rel, rte, rti,
     157                 :             :                                    oldrelation,
     158                 :             :                                    perminfo->updatedCols,
     159                 :             :                                    oldrc, lockmode);
     160                 :             :     }
     161                 :             :     else
     162                 :             :     {
     163                 :             :         /*
     164                 :             :          * Ordinary table, so process traditional-inheritance children.  (Note
     165                 :             :          * that partitioned tables are not allowed to have inheritance
     166                 :             :          * children, so it's not possible for both cases to apply.)
     167                 :             :          */
     168                 :             :         List       *inhOIDs;
     169                 :             :         ListCell   *l;
     170                 :             : 
     171                 :             :         /* Scan for all members of inheritance set, acquire needed locks */
     172                 :        2390 :         inhOIDs = find_all_inheritors(parentOID, lockmode, NULL);
     173                 :             : 
     174                 :             :         /*
     175                 :             :          * We used to special-case the situation where the table no longer has
     176                 :             :          * any children, by clearing rte->inh and exiting.  That no longer
     177                 :             :          * works, because this function doesn't get run until after decisions
     178                 :             :          * have been made that depend on rte->inh.  We have to treat such
     179                 :             :          * situations as normal inheritance.  The table itself should always
     180                 :             :          * have been found, though.
     181                 :             :          */
     182                 :             :         Assert(inhOIDs != NIL);
     183                 :             :         Assert(linitial_oid(inhOIDs) == parentOID);
     184                 :             : 
     185                 :             :         /* Expand simple_rel_array and friends to hold child objects. */
     186                 :        2390 :         expand_planner_arrays(root, list_length(inhOIDs));
     187                 :             : 
     188                 :             :         /*
     189                 :             :          * Expand inheritance children in the order the OIDs were returned by
     190                 :             :          * find_all_inheritors.
     191                 :             :          */
     192   [ +  -  +  +  :        8627 :         foreach(l, inhOIDs)
                   +  + ]
     193                 :             :         {
     194                 :        6238 :             Oid         childOID = lfirst_oid(l);
     195                 :             :             Relation    newrelation;
     196                 :             :             RangeTblEntry *childrte;
     197                 :             :             Index       childRTindex;
     198                 :             : 
     199                 :             :             /* Open rel if needed; we already have required locks */
     200         [ +  + ]:        6238 :             if (childOID != parentOID)
     201                 :        3848 :                 newrelation = table_open(childOID, NoLock);
     202                 :             :             else
     203                 :        2390 :                 newrelation = oldrelation;
     204                 :             : 
     205                 :             :             /*
     206                 :             :              * It is possible that the parent table has children that are temp
     207                 :             :              * tables of other backends.  We cannot safely access such tables
     208                 :             :              * (because of buffering issues), and the best thing to do seems
     209                 :             :              * to be to silently ignore them.
     210                 :             :              */
     211   [ +  +  +  +  :        6238 :             if (childOID != parentOID && RELATION_IS_OTHER_TEMP(newrelation))
                   +  + ]
     212                 :             :             {
     213                 :          21 :                 table_close(newrelation, lockmode);
     214                 :          21 :                 continue;
     215                 :             :             }
     216                 :             : 
     217                 :             :             /* Create RTE and AppendRelInfo, plus PlanRowMark if needed. */
     218                 :        6217 :             expand_single_inheritance_child(root, rte, rti, oldrelation,
     219                 :             :                                             oldrc, newrelation,
     220                 :             :                                             &childrte, &childRTindex);
     221                 :             : 
     222                 :             :             /* Create the otherrel RelOptInfo too. */
     223                 :        6216 :             (void) build_simple_rel(root, childRTindex, rel);
     224                 :             : 
     225                 :             :             /* Close child relations, but keep locks */
     226         [ +  + ]:        6216 :             if (childOID != parentOID)
     227                 :        3826 :                 table_close(newrelation, NoLock);
     228                 :             :         }
     229                 :             :     }
     230                 :             : 
     231                 :             :     /*
     232                 :             :      * Some children might require different mark types, which would've been
     233                 :             :      * reported into oldrc.  If so, add relevant entries to the top-level
     234                 :             :      * targetlist and update parent rel's reltarget.  This should match what
     235                 :             :      * preprocess_targetlist() would have added if the mark types had been
     236                 :             :      * requested originally.
     237                 :             :      *
     238                 :             :      * (Someday it might be useful to fold these resjunk columns into the
     239                 :             :      * row-identity-column management used for UPDATE/DELETE.  Today is not
     240                 :             :      * that day, however.)
     241                 :             :      */
     242         [ +  + ]:       13521 :     if (oldrc)
     243                 :             :     {
     244                 :        1294 :         int         new_allMarkTypes = oldrc->allMarkTypes;
     245                 :             :         Var        *var;
     246                 :             :         TargetEntry *tle;
     247                 :             :         char        resname[32];
     248                 :        1294 :         List       *newvars = NIL;
     249                 :             : 
     250                 :             :         /* Add TID junk Var if needed, unless we had it already */
     251         [ +  + ]:        1294 :         if (new_allMarkTypes & ~(1 << ROW_MARK_COPY) &&
     252         [ +  + ]:        1292 :             !(old_allMarkTypes & ~(1 << ROW_MARK_COPY)))
     253                 :             :         {
     254                 :             :             /* Need to fetch TID */
     255                 :           2 :             var = makeVar(oldrc->rti,
     256                 :             :                           SelfItemPointerAttributeNumber,
     257                 :             :                           TIDOID,
     258                 :             :                           -1,
     259                 :             :                           InvalidOid,
     260                 :             :                           0);
     261                 :           2 :             snprintf(resname, sizeof(resname), "ctid%u", oldrc->rowmarkId);
     262                 :           2 :             tle = makeTargetEntry((Expr *) var,
     263                 :           2 :                                   list_length(root->processed_tlist) + 1,
     264                 :             :                                   pstrdup(resname),
     265                 :             :                                   true);
     266                 :           2 :             root->processed_tlist = lappend(root->processed_tlist, tle);
     267                 :           2 :             newvars = lappend(newvars, var);
     268                 :             :         }
     269                 :             : 
     270                 :             :         /* Add whole-row junk Var if needed, unless we had it already */
     271         [ +  + ]:        1294 :         if ((new_allMarkTypes & (1 << ROW_MARK_COPY)) &&
     272         [ +  + ]:          23 :             !(old_allMarkTypes & (1 << ROW_MARK_COPY)))
     273                 :             :         {
     274                 :          19 :             var = makeWholeRowVar(planner_rt_fetch(oldrc->rti, root),
     275         [ +  - ]:          19 :                                   oldrc->rti,
     276                 :             :                                   0,
     277                 :             :                                   false);
     278                 :          19 :             snprintf(resname, sizeof(resname), "wholerow%u", oldrc->rowmarkId);
     279                 :          19 :             tle = makeTargetEntry((Expr *) var,
     280                 :          19 :                                   list_length(root->processed_tlist) + 1,
     281                 :             :                                   pstrdup(resname),
     282                 :             :                                   true);
     283                 :          19 :             root->processed_tlist = lappend(root->processed_tlist, tle);
     284                 :          19 :             newvars = lappend(newvars, var);
     285                 :             :         }
     286                 :             : 
     287                 :             :         /* Add tableoid junk Var, unless we had it already */
     288         [ +  - ]:        1294 :         if (!old_isParent)
     289                 :             :         {
     290                 :        1294 :             var = makeVar(oldrc->rti,
     291                 :             :                           TableOidAttributeNumber,
     292                 :             :                           OIDOID,
     293                 :             :                           -1,
     294                 :             :                           InvalidOid,
     295                 :             :                           0);
     296                 :        1294 :             snprintf(resname, sizeof(resname), "tableoid%u", oldrc->rowmarkId);
     297                 :        1294 :             tle = makeTargetEntry((Expr *) var,
     298                 :        1294 :                                   list_length(root->processed_tlist) + 1,
     299                 :             :                                   pstrdup(resname),
     300                 :             :                                   true);
     301                 :        1294 :             root->processed_tlist = lappend(root->processed_tlist, tle);
     302                 :        1294 :             newvars = lappend(newvars, var);
     303                 :             :         }
     304                 :             : 
     305                 :             :         /*
     306                 :             :          * Add the newly added Vars to parent's reltarget.  We needn't worry
     307                 :             :          * about the children's reltargets, they'll be made later.
     308                 :             :          */
     309                 :        1294 :         add_vars_to_targetlist(root, newvars, bms_make_singleton(0));
     310                 :             :     }
     311                 :             : 
     312                 :       13521 :     table_close(oldrelation, NoLock);
     313                 :             : }
     314                 :             : 
     315                 :             : /*
     316                 :             :  * expand_partitioned_rtentry
     317                 :             :  *      Recursively expand an RTE for a partitioned table.
     318                 :             :  */
     319                 :             : static void
     320                 :       14160 : expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
     321                 :             :                            RangeTblEntry *parentrte,
     322                 :             :                            Index parentRTindex, Relation parentrel,
     323                 :             :                            Bitmapset *parent_updatedCols,
     324                 :             :                            PlanRowMark *top_parentrc, LOCKMODE lockmode)
     325                 :             : {
     326                 :             :     PartitionDesc partdesc;
     327                 :             :     int         num_live_parts;
     328                 :             :     int         i;
     329                 :             : 
     330                 :       14160 :     check_stack_depth();
     331                 :             : 
     332                 :             :     Assert(parentrte->inh);
     333                 :             : 
     334                 :       14160 :     partdesc = PartitionDirectoryLookup(root->glob->partition_directory,
     335                 :             :                                         parentrel);
     336                 :             : 
     337                 :             :     /* A partitioned table should always have a partition descriptor. */
     338                 :             :     Assert(partdesc);
     339                 :             : 
     340                 :             :     /* Nothing further to do here if there are no partitions. */
     341         [ +  + ]:       14160 :     if (partdesc->nparts == 0)
     342                 :          55 :         return;
     343                 :             : 
     344                 :             :     /*
     345                 :             :      * Perform partition pruning using restriction clauses assigned to parent
     346                 :             :      * relation.  live_parts will contain PartitionDesc indexes of partitions
     347                 :             :      * that survive pruning.  Below, we will initialize child objects for the
     348                 :             :      * surviving partitions.
     349                 :             :      */
     350                 :       14105 :     relinfo->live_parts = prune_append_rel_partitions(relinfo);
     351                 :             : 
     352                 :             :     /* Expand simple_rel_array and friends to hold child objects. */
     353                 :       14105 :     num_live_parts = bms_num_members(relinfo->live_parts);
     354         [ +  + ]:       14105 :     if (num_live_parts > 0)
     355                 :       13861 :         expand_planner_arrays(root, num_live_parts);
     356                 :             : 
     357                 :             :     /*
     358                 :             :      * We also store partition RelOptInfo pointers in the parent relation.
     359                 :             :      * Since we're palloc0'ing, slots corresponding to pruned partitions will
     360                 :             :      * contain NULL.
     361                 :             :      */
     362                 :             :     Assert(relinfo->part_rels == NULL);
     363                 :       14105 :     relinfo->part_rels = (RelOptInfo **)
     364                 :       14105 :         palloc0(relinfo->nparts * sizeof(RelOptInfo *));
     365                 :             : 
     366                 :             :     /*
     367                 :             :      * Create a child RTE for each live partition.  Note that unlike
     368                 :             :      * traditional inheritance, we don't need a child RTE for the partitioned
     369                 :             :      * table itself, because it's not going to be scanned.
     370                 :             :      */
     371                 :       14105 :     i = -1;
     372         [ +  + ]:       43912 :     while ((i = bms_next_member(relinfo->live_parts, i)) >= 0)
     373                 :             :     {
     374                 :       29807 :         Oid         childOID = partdesc->oids[i];
     375                 :             :         Relation    childrel;
     376                 :             :         RangeTblEntry *childrte;
     377                 :             :         Index       childRTindex;
     378                 :             :         RelOptInfo *childrelinfo;
     379                 :             : 
     380                 :             :         /*
     381                 :             :          * Open rel, acquiring required locks.  If a partition was recently
     382                 :             :          * detached and subsequently dropped, then opening it will fail.  In
     383                 :             :          * this case, behave as though the partition had been pruned.
     384                 :             :          */
     385                 :       29807 :         childrel = try_table_open(childOID, lockmode);
     386         [ -  + ]:       29807 :         if (childrel == NULL)
     387                 :             :         {
     388                 :           0 :             relinfo->live_parts = bms_del_member(relinfo->live_parts, i);
     389                 :           0 :             continue;
     390                 :             :         }
     391                 :             : 
     392                 :             :         /*
     393                 :             :          * Temporary partitions belonging to other sessions should have been
     394                 :             :          * disallowed at definition, but for paranoia's sake, let's double
     395                 :             :          * check.
     396                 :             :          */
     397   [ +  +  -  + ]:       29807 :         if (RELATION_IS_OTHER_TEMP(childrel))
     398         [ #  # ]:           0 :             elog(ERROR, "temporary relation from another session found as partition");
     399                 :             : 
     400                 :             :         /* Create RTE and AppendRelInfo, plus PlanRowMark if needed. */
     401                 :       29807 :         expand_single_inheritance_child(root, parentrte, parentRTindex,
     402                 :             :                                         parentrel, top_parentrc, childrel,
     403                 :             :                                         &childrte, &childRTindex);
     404                 :             : 
     405                 :             :         /* Create the otherrel RelOptInfo too. */
     406                 :       29807 :         childrelinfo = build_simple_rel(root, childRTindex, relinfo);
     407                 :       29807 :         relinfo->part_rels[i] = childrelinfo;
     408                 :       59614 :         relinfo->all_partrels = bms_add_members(relinfo->all_partrels,
     409                 :       29807 :                                                 childrelinfo->relids);
     410                 :             : 
     411                 :             :         /* If this child is itself partitioned, recurse */
     412         [ +  + ]:       29807 :         if (childrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     413                 :             :         {
     414                 :        3028 :             AppendRelInfo *appinfo = root->append_rel_array[childRTindex];
     415                 :             :             Bitmapset  *child_updatedCols;
     416                 :             : 
     417                 :        3028 :             child_updatedCols = translate_col_privs(parent_updatedCols,
     418                 :             :                                                     appinfo->translated_vars);
     419                 :             : 
     420                 :        3028 :             expand_partitioned_rtentry(root, childrelinfo,
     421                 :             :                                        childrte, childRTindex,
     422                 :             :                                        childrel,
     423                 :             :                                        child_updatedCols,
     424                 :             :                                        top_parentrc, lockmode);
     425                 :             :         }
     426                 :             : 
     427                 :             :         /* Close child relation, but keep locks */
     428                 :       29807 :         table_close(childrel, NoLock);
     429                 :             :     }
     430                 :             : }
     431                 :             : 
     432                 :             : /*
     433                 :             :  * expand_single_inheritance_child
     434                 :             :  *      Build a RangeTblEntry and an AppendRelInfo, plus maybe a PlanRowMark.
     435                 :             :  *
     436                 :             :  * We now expand the partition hierarchy level by level, creating a
     437                 :             :  * corresponding hierarchy of AppendRelInfos and RelOptInfos, where each
     438                 :             :  * partitioned descendant acts as a parent of its immediate partitions.
     439                 :             :  * (This is a difference from what older versions of PostgreSQL did and what
     440                 :             :  * is still done in the case of table inheritance for unpartitioned tables,
     441                 :             :  * where the hierarchy is flattened during RTE expansion.)
     442                 :             :  *
     443                 :             :  * PlanRowMarks still carry the top-parent's RTI, and the top-parent's
     444                 :             :  * allMarkTypes field still accumulates values from all descendents.
     445                 :             :  *
     446                 :             :  * "parentrte" and "parentRTindex" are immediate parent's RTE and
     447                 :             :  * RTI. "top_parentrc" is top parent's PlanRowMark.
     448                 :             :  *
     449                 :             :  * The child RangeTblEntry and its RTI are returned in "childrte_p" and
     450                 :             :  * "childRTindex_p" resp.
     451                 :             :  */
     452                 :             : static void
     453                 :       36024 : expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte,
     454                 :             :                                 Index parentRTindex, Relation parentrel,
     455                 :             :                                 PlanRowMark *top_parentrc, Relation childrel,
     456                 :             :                                 RangeTblEntry **childrte_p,
     457                 :             :                                 Index *childRTindex_p)
     458                 :             : {
     459                 :       36024 :     Query      *parse = root->parse;
     460                 :       36024 :     Oid         parentOID = RelationGetRelid(parentrel);
     461                 :       36024 :     Oid         childOID = RelationGetRelid(childrel);
     462                 :             :     RangeTblEntry *childrte;
     463                 :             :     Index       childRTindex;
     464                 :             :     AppendRelInfo *appinfo;
     465                 :             :     TupleDesc   child_tupdesc;
     466                 :             :     List       *parent_colnames;
     467                 :             :     List       *child_colnames;
     468                 :             : 
     469                 :             :     /*
     470                 :             :      * Build an RTE for the child, and attach to query's rangetable list. We
     471                 :             :      * copy most scalar fields of the parent's RTE, but replace relation OID,
     472                 :             :      * relkind, and inh for the child.  Set the child's securityQuals to
     473                 :             :      * empty, because we only want to apply the parent's RLS conditions
     474                 :             :      * regardless of what RLS properties individual children may have. (This
     475                 :             :      * is an intentional choice to make inherited RLS work like regular
     476                 :             :      * permissions checks.) The parent securityQuals will be propagated to
     477                 :             :      * children along with other base restriction clauses, so we don't need to
     478                 :             :      * do it here.  Other infrastructure of the parent RTE has to be
     479                 :             :      * translated to match the child table's column ordering, which we do
     480                 :             :      * below, so a "flat" copy is sufficient to start with.
     481                 :             :      */
     482                 :       36024 :     childrte = makeNode(RangeTblEntry);
     483                 :       36024 :     memcpy(childrte, parentrte, sizeof(RangeTblEntry));
     484                 :             :     Assert(parentrte->rtekind == RTE_RELATION); /* else this is dubious */
     485                 :       36024 :     childrte->relid = childOID;
     486                 :       36024 :     childrte->relkind = childrel->rd_rel->relkind;
     487                 :             :     /* A partitioned child will need to be expanded further. */
     488         [ +  + ]:       36024 :     if (childrte->relkind == RELKIND_PARTITIONED_TABLE)
     489                 :             :     {
     490                 :             :         Assert(childOID != parentOID);
     491                 :        3028 :         childrte->inh = true;
     492                 :             :     }
     493                 :             :     else
     494                 :       32996 :         childrte->inh = false;
     495                 :       36024 :     childrte->securityQuals = NIL;
     496                 :             : 
     497                 :             :     /* No permission checking for child RTEs. */
     498                 :       36024 :     childrte->perminfoindex = 0;
     499                 :             : 
     500                 :             :     /* Link not-yet-fully-filled child RTE into data structures */
     501                 :       36024 :     parse->rtable = lappend(parse->rtable, childrte);
     502                 :       36024 :     childRTindex = list_length(parse->rtable);
     503                 :       36024 :     *childrte_p = childrte;
     504                 :       36024 :     *childRTindex_p = childRTindex;
     505                 :             : 
     506                 :             :     /*
     507                 :             :      * Retrieve column not-null constraint information for the child relation
     508                 :             :      * if its relation OID is different from the parent's.
     509                 :             :      */
     510         [ +  + ]:       36024 :     if (childOID != parentOID)
     511                 :       33634 :         get_relation_notnullatts(root, childrel);
     512                 :             : 
     513                 :             :     /*
     514                 :             :      * Build an AppendRelInfo struct for each parent/child pair.
     515                 :             :      */
     516                 :       36024 :     appinfo = make_append_rel_info(parentrel, childrel,
     517                 :             :                                    parentRTindex, childRTindex);
     518                 :       36023 :     root->append_rel_list = lappend(root->append_rel_list, appinfo);
     519                 :             : 
     520                 :             :     /* tablesample is probably null, but copy it */
     521                 :       36023 :     childrte->tablesample = copyObject(parentrte->tablesample);
     522                 :             : 
     523                 :             :     /*
     524                 :             :      * Construct an alias clause for the child, which we can also use as eref.
     525                 :             :      * This is important so that EXPLAIN will print the right column aliases
     526                 :             :      * for child-table columns.  (Since ruleutils.c doesn't have any easy way
     527                 :             :      * to reassociate parent and child columns, we must get the child column
     528                 :             :      * aliases right to start with.  Note that setting childrte->alias forces
     529                 :             :      * ruleutils.c to use these column names, which it otherwise would not.)
     530                 :             :      */
     531                 :       36023 :     child_tupdesc = RelationGetDescr(childrel);
     532                 :       36023 :     parent_colnames = parentrte->eref->colnames;
     533                 :       36023 :     child_colnames = NIL;
     534         [ +  + ]:      134686 :     for (int cattno = 0; cattno < child_tupdesc->natts; cattno++)
     535                 :             :     {
     536                 :       98663 :         Form_pg_attribute att = TupleDescAttr(child_tupdesc, cattno);
     537                 :             :         const char *attname;
     538                 :             : 
     539         [ +  + ]:       98663 :         if (att->attisdropped)
     540                 :             :         {
     541                 :             :             /* Always insert an empty string for a dropped column */
     542                 :        2091 :             attname = "";
     543                 :             :         }
     544   [ +  +  +  - ]:      189791 :         else if (appinfo->parent_colnos[cattno] > 0 &&
     545                 :       93219 :                  appinfo->parent_colnos[cattno] <= list_length(parent_colnames))
     546                 :             :         {
     547                 :             :             /* Duplicate the query-assigned name for the parent column */
     548                 :       93219 :             attname = strVal(list_nth(parent_colnames,
     549                 :             :                                       appinfo->parent_colnos[cattno] - 1));
     550                 :             :         }
     551                 :             :         else
     552                 :             :         {
     553                 :             :             /* New column, just use its real name */
     554                 :        3353 :             attname = NameStr(att->attname);
     555                 :             :         }
     556                 :       98663 :         child_colnames = lappend(child_colnames, makeString(pstrdup(attname)));
     557                 :             :     }
     558                 :             : 
     559                 :             :     /*
     560                 :             :      * We just duplicate the parent's table alias name for each child.  If the
     561                 :             :      * plan gets printed, ruleutils.c has to sort out unique table aliases to
     562                 :             :      * use, which it can handle.
     563                 :             :      */
     564                 :       36023 :     childrte->alias = childrte->eref = makeAlias(parentrte->eref->aliasname,
     565                 :             :                                                  child_colnames);
     566                 :             : 
     567                 :             :     /*
     568                 :             :      * Store the RTE and appinfo in the respective PlannerInfo arrays, which
     569                 :             :      * the caller must already have allocated space for.
     570                 :             :      */
     571                 :             :     Assert(childRTindex < root->simple_rel_array_size);
     572                 :             :     Assert(root->simple_rte_array[childRTindex] == NULL);
     573                 :       36023 :     root->simple_rte_array[childRTindex] = childrte;
     574                 :             :     Assert(root->append_rel_array[childRTindex] == NULL);
     575                 :       36023 :     root->append_rel_array[childRTindex] = appinfo;
     576                 :             : 
     577                 :             :     /*
     578                 :             :      * Build a PlanRowMark if parent is marked FOR UPDATE/SHARE.
     579                 :             :      */
     580         [ +  + ]:       36023 :     if (top_parentrc)
     581                 :             :     {
     582                 :        1894 :         PlanRowMark *childrc = makeNode(PlanRowMark);
     583                 :             : 
     584                 :        1894 :         childrc->rti = childRTindex;
     585                 :        1894 :         childrc->prti = top_parentrc->rti;
     586                 :        1894 :         childrc->rowmarkId = top_parentrc->rowmarkId;
     587                 :             :         /* Reselect rowmark type, because relkind might not match parent */
     588                 :        1894 :         childrc->markType = select_rowmark_type(childrte,
     589                 :             :                                                 top_parentrc->strength);
     590                 :        1894 :         childrc->allMarkTypes = (1 << childrc->markType);
     591                 :        1894 :         childrc->strength = top_parentrc->strength;
     592                 :        1894 :         childrc->waitPolicy = top_parentrc->waitPolicy;
     593                 :             : 
     594                 :             :         /*
     595                 :             :          * We mark RowMarks for partitioned child tables as parent RowMarks so
     596                 :             :          * that the executor ignores them (except their existence means that
     597                 :             :          * the child tables will be locked using the appropriate mode).
     598                 :             :          */
     599                 :        1894 :         childrc->isParent = (childrte->relkind == RELKIND_PARTITIONED_TABLE);
     600                 :             : 
     601                 :             :         /* Include child's rowmark type in top parent's allMarkTypes */
     602                 :        1894 :         top_parentrc->allMarkTypes |= childrc->allMarkTypes;
     603                 :             : 
     604                 :        1894 :         root->rowMarks = lappend(root->rowMarks, childrc);
     605                 :             :     }
     606                 :             : 
     607                 :             :     /*
     608                 :             :      * If we are creating a child of the query target relation (only possible
     609                 :             :      * in UPDATE/DELETE/MERGE), add it to all_result_relids, as well as
     610                 :             :      * leaf_result_relids if appropriate, and make sure that we generate
     611                 :             :      * required row-identity data.
     612                 :             :      */
     613         [ +  + ]:       36023 :     if (bms_is_member(parentRTindex, root->all_result_relids))
     614                 :             :     {
     615                 :             :         /* OK, record the child as a result rel too. */
     616                 :        4976 :         root->all_result_relids = bms_add_member(root->all_result_relids,
     617                 :             :                                                  childRTindex);
     618                 :             : 
     619                 :             :         /* Non-leaf partitions don't need any row identity info. */
     620         [ +  + ]:        4976 :         if (childrte->relkind != RELKIND_PARTITIONED_TABLE)
     621                 :             :         {
     622                 :             :             Var        *rrvar;
     623                 :             : 
     624                 :        4488 :             root->leaf_result_relids = bms_add_member(root->leaf_result_relids,
     625                 :             :                                                       childRTindex);
     626                 :             : 
     627                 :             :             /*
     628                 :             :              * If we have any child target relations, assume they all need to
     629                 :             :              * generate a junk "tableoid" column.  (If only one child survives
     630                 :             :              * pruning, we wouldn't really need this, but it's not worth
     631                 :             :              * thrashing about to avoid it.)
     632                 :             :              */
     633                 :        4488 :             rrvar = makeVar(childRTindex,
     634                 :             :                             TableOidAttributeNumber,
     635                 :             :                             OIDOID,
     636                 :             :                             -1,
     637                 :             :                             InvalidOid,
     638                 :             :                             0);
     639                 :        4488 :             add_row_identity_var(root, rrvar, childRTindex, "tableoid");
     640                 :             : 
     641                 :             :             /* Register any row-identity columns needed by this child. */
     642                 :        4488 :             add_row_identity_columns(root, childRTindex,
     643                 :             :                                      childrte, childrel);
     644                 :             :         }
     645                 :             :     }
     646                 :       36023 : }
     647                 :             : 
     648                 :             : /*
     649                 :             :  * get_rel_all_updated_cols
     650                 :             :  *      Returns the set of columns of a given "simple" relation that are
     651                 :             :  *      updated by this query.
     652                 :             :  */
     653                 :             : Bitmapset *
     654                 :          46 : get_rel_all_updated_cols(PlannerInfo *root, RelOptInfo *rel)
     655                 :             : {
     656                 :             :     Index       relid;
     657                 :             :     RangeTblEntry *rte;
     658                 :             :     RTEPermissionInfo *perminfo;
     659                 :             :     Bitmapset  *updatedCols,
     660                 :             :                *extraUpdatedCols;
     661                 :             : 
     662                 :             :     Assert(root->parse->commandType == CMD_UPDATE);
     663                 :             :     Assert(IS_SIMPLE_REL(rel));
     664                 :             : 
     665                 :             :     /*
     666                 :             :      * We obtain updatedCols for the query's result relation.  Then, if
     667                 :             :      * necessary, we map it to the column numbers of the relation for which
     668                 :             :      * they were requested.
     669                 :             :      */
     670                 :          46 :     relid = root->parse->resultRelation;
     671         [ +  - ]:          46 :     rte = planner_rt_fetch(relid, root);
     672                 :          46 :     perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
     673                 :             : 
     674                 :          46 :     updatedCols = perminfo->updatedCols;
     675                 :             : 
     676         [ +  + ]:          46 :     if (rel->relid != relid)
     677                 :             :     {
     678                 :          22 :         RelOptInfo *top_parent_rel = find_base_rel(root, relid);
     679                 :             : 
     680                 :             :         Assert(IS_OTHER_REL(rel));
     681                 :             : 
     682                 :          22 :         updatedCols = translate_col_privs_multilevel(root, rel, top_parent_rel,
     683                 :             :                                                      updatedCols);
     684                 :             :     }
     685                 :             : 
     686                 :             :     /*
     687                 :             :      * Now we must check to see if there are any generated columns that depend
     688                 :             :      * on the updatedCols, and add them to the result.
     689                 :             :      */
     690                 :          46 :     extraUpdatedCols = get_dependent_generated_columns(root, rel->relid,
     691                 :             :                                                        updatedCols);
     692                 :             : 
     693                 :          46 :     return bms_union(updatedCols, extraUpdatedCols);
     694                 :             : }
     695                 :             : 
     696                 :             : /*
     697                 :             :  * translate_col_privs
     698                 :             :  *    Translate a bitmapset representing per-column privileges from the
     699                 :             :  *    parent rel's attribute numbering to the child's.
     700                 :             :  *
     701                 :             :  * The only surprise here is that we don't translate a parent whole-row
     702                 :             :  * reference into a child whole-row reference.  That would mean requiring
     703                 :             :  * permissions on all child columns, which is overly strict, since the
     704                 :             :  * query is really only going to reference the inherited columns.  Instead
     705                 :             :  * we set the per-column bits for all inherited columns.
     706                 :             :  */
     707                 :             : static Bitmapset *
     708                 :        3052 : translate_col_privs(const Bitmapset *parent_privs,
     709                 :             :                     List *translated_vars)
     710                 :             : {
     711                 :        3052 :     Bitmapset  *child_privs = NULL;
     712                 :             :     bool        whole_row;
     713                 :             :     int         attno;
     714                 :             :     ListCell   *lc;
     715                 :             : 
     716                 :             :     /* System attributes have the same numbers in all tables */
     717         [ +  + ]:       21364 :     for (attno = FirstLowInvalidHeapAttributeNumber + 1; attno < 0; attno++)
     718                 :             :     {
     719         [ -  + ]:       18312 :         if (bms_is_member(attno - FirstLowInvalidHeapAttributeNumber,
     720                 :             :                           parent_privs))
     721                 :           0 :             child_privs = bms_add_member(child_privs,
     722                 :             :                                          attno - FirstLowInvalidHeapAttributeNumber);
     723                 :             :     }
     724                 :             : 
     725                 :             :     /* Check if parent has whole-row reference */
     726                 :        3052 :     whole_row = bms_is_member(InvalidAttrNumber - FirstLowInvalidHeapAttributeNumber,
     727                 :             :                               parent_privs);
     728                 :             : 
     729                 :             :     /* And now translate the regular user attributes, using the vars list */
     730                 :        3052 :     attno = InvalidAttrNumber;
     731   [ +  -  +  +  :       10852 :     foreach(lc, translated_vars)
                   +  + ]
     732                 :             :     {
     733                 :        7800 :         Var        *var = lfirst_node(Var, lc);
     734                 :             : 
     735                 :        7800 :         attno++;
     736         [ +  + ]:        7800 :         if (var == NULL)        /* ignore dropped columns */
     737                 :         110 :             continue;
     738   [ +  -  +  + ]:       15380 :         if (whole_row ||
     739                 :        7690 :             bms_is_member(attno - FirstLowInvalidHeapAttributeNumber,
     740                 :             :                           parent_privs))
     741                 :         442 :             child_privs = bms_add_member(child_privs,
     742                 :         442 :                                          var->varattno - FirstLowInvalidHeapAttributeNumber);
     743                 :             :     }
     744                 :             : 
     745                 :        3052 :     return child_privs;
     746                 :             : }
     747                 :             : 
     748                 :             : /*
     749                 :             :  * translate_col_privs_multilevel
     750                 :             :  *      Recursively translates the column numbers contained in 'parent_cols'
     751                 :             :  *      to the column numbers of a descendant relation given by 'rel'
     752                 :             :  *
     753                 :             :  * Note that because this is based on translate_col_privs, it will expand
     754                 :             :  * a whole-row reference into all inherited columns.  This is not an issue
     755                 :             :  * for current usages, but beware.
     756                 :             :  */
     757                 :             : static Bitmapset *
     758                 :          24 : translate_col_privs_multilevel(PlannerInfo *root, RelOptInfo *rel,
     759                 :             :                                RelOptInfo *parent_rel,
     760                 :             :                                Bitmapset *parent_cols)
     761                 :             : {
     762                 :             :     AppendRelInfo *appinfo;
     763                 :             : 
     764                 :             :     /* Fast path for easy case. */
     765         [ -  + ]:          24 :     if (parent_cols == NULL)
     766                 :           0 :         return NULL;
     767                 :             : 
     768                 :             :     /* Recurse if immediate parent is not the top parent. */
     769         [ +  + ]:          24 :     if (rel->parent != parent_rel)
     770                 :             :     {
     771         [ +  - ]:           2 :         if (rel->parent)
     772                 :           2 :             parent_cols = translate_col_privs_multilevel(root, rel->parent,
     773                 :             :                                                          parent_rel,
     774                 :             :                                                          parent_cols);
     775                 :             :         else
     776         [ #  # ]:           0 :             elog(ERROR, "rel with relid %u is not a child rel", rel->relid);
     777                 :             :     }
     778                 :             : 
     779                 :             :     /* Now translate for this child. */
     780                 :             :     Assert(root->append_rel_array != NULL);
     781                 :          24 :     appinfo = root->append_rel_array[rel->relid];
     782                 :             :     Assert(appinfo != NULL);
     783                 :             : 
     784                 :          24 :     return translate_col_privs(parent_cols, appinfo->translated_vars);
     785                 :             : }
     786                 :             : 
     787                 :             : /*
     788                 :             :  * expand_appendrel_subquery
     789                 :             :  *      Add "other rel" RelOptInfos for the children of an appendrel baserel
     790                 :             :  *
     791                 :             :  * "rel" is a subquery relation that has the rte->inh flag set, meaning it
     792                 :             :  * is a UNION ALL subquery that's been flattened into an appendrel, with
     793                 :             :  * child subqueries listed in root->append_rel_list.  We need to build
     794                 :             :  * a RelOptInfo for each child relation so that we can plan scans on them.
     795                 :             :  */
     796                 :             : static void
     797                 :        4431 : expand_appendrel_subquery(PlannerInfo *root, RelOptInfo *rel,
     798                 :             :                           RangeTblEntry *rte, Index rti)
     799                 :             : {
     800                 :             :     ListCell   *l;
     801                 :             : 
     802   [ +  -  +  +  :       17325 :     foreach(l, root->append_rel_list)
                   +  + ]
     803                 :             :     {
     804                 :       12894 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
     805                 :       12894 :         Index       childRTindex = appinfo->child_relid;
     806                 :             :         RangeTblEntry *childrte;
     807                 :             :         RelOptInfo *childrel;
     808                 :             : 
     809                 :             :         /* append_rel_list contains all append rels; ignore others */
     810         [ +  + ]:       12894 :         if (appinfo->parent_relid != rti)
     811                 :         987 :             continue;
     812                 :             : 
     813                 :             :         /* find the child RTE, which should already exist */
     814                 :             :         Assert(childRTindex < root->simple_rel_array_size);
     815                 :       11907 :         childrte = root->simple_rte_array[childRTindex];
     816                 :             :         Assert(childrte != NULL);
     817                 :             : 
     818                 :             :         /* Build the child RelOptInfo. */
     819                 :       11907 :         childrel = build_simple_rel(root, childRTindex, rel);
     820                 :             : 
     821                 :             :         /* Child may itself be an inherited rel, either table or subquery. */
     822         [ +  + ]:       11907 :         if (childrte->inh)
     823                 :         194 :             expand_inherited_rtentry(root, childrel, childrte, childRTindex);
     824                 :             :     }
     825                 :        4431 : }
     826                 :             : 
     827                 :             : 
     828                 :             : /*
     829                 :             :  * apply_child_basequals
     830                 :             :  *      Populate childrel's base restriction quals from parent rel's quals,
     831                 :             :  *      translating them using appinfo.
     832                 :             :  *
     833                 :             :  * If any of the resulting clauses evaluate to constant false or NULL, we
     834                 :             :  * return false and don't apply any quals.  Caller should mark the relation as
     835                 :             :  * a dummy rel in this case, since it doesn't need to be scanned.  Constant
     836                 :             :  * true quals are ignored.
     837                 :             :  */
     838                 :             : bool
     839                 :       47930 : apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
     840                 :             :                       RelOptInfo *childrel, RangeTblEntry *childRTE,
     841                 :             :                       AppendRelInfo *appinfo)
     842                 :             : {
     843                 :             :     List       *childquals;
     844                 :             :     Index       cq_min_security;
     845                 :             :     ListCell   *lc;
     846                 :             : 
     847                 :             :     /*
     848                 :             :      * The child rel's targetlist might contain non-Var expressions, which
     849                 :             :      * means that substitution into the quals could produce opportunities for
     850                 :             :      * const-simplification, and perhaps even pseudoconstant quals. Therefore,
     851                 :             :      * transform each RestrictInfo separately to see if it reduces to a
     852                 :             :      * constant or pseudoconstant.  (We must process them separately to keep
     853                 :             :      * track of the security level of each qual.)
     854                 :             :      */
     855                 :       47930 :     childquals = NIL;
     856                 :       47930 :     cq_min_security = UINT_MAX;
     857   [ +  +  +  +  :       72355 :     foreach(lc, parentrel->baserestrictinfo)
                   +  + ]
     858                 :             :     {
     859                 :       24508 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
     860                 :             :         Node       *childqual;
     861                 :             :         ListCell   *lc2;
     862                 :             : 
     863                 :             :         Assert(IsA(rinfo, RestrictInfo));
     864                 :       24508 :         childqual = adjust_appendrel_attrs(root,
     865                 :       24508 :                                            (Node *) rinfo->clause,
     866                 :             :                                            1, &appinfo);
     867                 :       24508 :         childqual = eval_const_expressions(root, childqual);
     868                 :             :         /* check for flat-out constant */
     869   [ +  -  +  + ]:       24508 :         if (childqual && IsA(childqual, Const))
     870                 :             :         {
     871         [ +  - ]:         178 :             if (((Const *) childqual)->constisnull ||
     872         [ +  + ]:         178 :                 !DatumGetBool(((Const *) childqual)->constvalue))
     873                 :             :             {
     874                 :             :                 /* Restriction reduces to constant FALSE or NULL */
     875                 :          83 :                 return false;
     876                 :             :             }
     877                 :             :             /* Restriction reduces to constant TRUE, so drop it */
     878                 :          95 :             continue;
     879                 :             :         }
     880                 :             :         /* might have gotten an AND clause, if so flatten it */
     881   [ +  -  +  +  :       48670 :         foreach(lc2, make_ands_implicit((Expr *) childqual))
                   +  + ]
     882                 :             :         {
     883                 :       24340 :             Node       *onecq = (Node *) lfirst(lc2);
     884                 :             :             bool        pseudoconstant;
     885                 :             :             RestrictInfo *childrinfo;
     886                 :             : 
     887                 :             :             /* check for pseudoconstant (no Vars or volatile functions) */
     888                 :       24340 :             pseudoconstant =
     889         [ +  + ]:       24375 :                 !contain_vars_of_level(onecq, 0) &&
     890         [ +  - ]:          35 :                 !contain_volatile_functions(onecq);
     891         [ +  + ]:       24340 :             if (pseudoconstant)
     892                 :             :             {
     893                 :             :                 /* tell createplan.c to check for gating quals */
     894                 :          35 :                 root->hasPseudoConstantQuals = true;
     895                 :             :             }
     896                 :             :             /* reconstitute RestrictInfo with appropriate properties */
     897                 :       24340 :             childrinfo = make_restrictinfo(root,
     898                 :             :                                            (Expr *) onecq,
     899                 :       24340 :                                            rinfo->is_pushed_down,
     900                 :       24340 :                                            rinfo->has_clone,
     901                 :       24340 :                                            rinfo->is_clone,
     902                 :             :                                            pseudoconstant,
     903                 :             :                                            rinfo->security_level,
     904                 :             :                                            NULL, NULL, NULL);
     905                 :             : 
     906                 :       24340 :             childquals = lappend(childquals, childrinfo);
     907                 :             :             /* track minimum security level among child quals */
     908                 :       24340 :             cq_min_security = Min(cq_min_security, childrinfo->security_level);
     909                 :             :         }
     910                 :             :     }
     911                 :             : 
     912                 :             :     /*
     913                 :             :      * In addition to the quals inherited from the parent, we might have
     914                 :             :      * securityQuals associated with this particular child node.  (Currently
     915                 :             :      * this can only happen in appendrels originating from UNION ALL;
     916                 :             :      * inheritance child tables don't have their own securityQuals, see
     917                 :             :      * expand_single_inheritance_child().)  Pull any such securityQuals up
     918                 :             :      * into the baserestrictinfo for the child.  This is similar to
     919                 :             :      * process_security_barrier_quals() for the parent rel, except that we
     920                 :             :      * can't make any general deductions from such quals, since they don't
     921                 :             :      * hold for the whole appendrel.
     922                 :             :      */
     923         [ +  + ]:       47847 :     if (childRTE->securityQuals)
     924                 :             :     {
     925                 :          40 :         Index       security_level = 0;
     926                 :             : 
     927   [ +  -  +  +  :          80 :         foreach(lc, childRTE->securityQuals)
                   +  + ]
     928                 :             :         {
     929                 :          40 :             List       *qualset = (List *) lfirst(lc);
     930                 :             :             ListCell   *lc2;
     931                 :             : 
     932   [ +  -  +  +  :          80 :             foreach(lc2, qualset)
                   +  + ]
     933                 :             :             {
     934                 :          40 :                 Expr       *qual = (Expr *) lfirst(lc2);
     935                 :             : 
     936                 :             :                 /* not likely that we'd see constants here, so no check */
     937                 :          40 :                 childquals = lappend(childquals,
     938                 :          40 :                                      make_restrictinfo(root, qual,
     939                 :             :                                                        true,
     940                 :             :                                                        false, false,
     941                 :             :                                                        false,
     942                 :             :                                                        security_level,
     943                 :             :                                                        NULL, NULL, NULL));
     944                 :          40 :                 cq_min_security = Min(cq_min_security, security_level);
     945                 :             :             }
     946                 :          40 :             security_level++;
     947                 :             :         }
     948                 :             :         Assert(security_level <= root->qual_security_level);
     949                 :             :     }
     950                 :             : 
     951                 :             :     /*
     952                 :             :      * OK, we've got all the baserestrictinfo quals for this child.
     953                 :             :      */
     954                 :       47847 :     childrel->baserestrictinfo = childquals;
     955                 :       47847 :     childrel->baserestrict_min_security = cq_min_security;
     956                 :             : 
     957                 :       47847 :     return true;
     958                 :             : }
        

Generated by: LCOV version 2.0-1