LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - inherit.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 82.8 % 373 309
Test Date: 2026-01-26 10:56:24 Functions: 75.0 % 8 6
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 63.5 % 230 146

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

Generated by: LCOV version 2.3.2-1