LCOV - code coverage report
Current view: top level - src/backend/rewrite - rewriteHandler.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 90.6 % 1360 1232
Test Date: 2026-07-25 22:15:46 Functions: 100.0 % 33 33
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 77.3 % 1441 1114

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * rewriteHandler.c
       4                 :             :  *      Primary module of query rewriter.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *    src/backend/rewrite/rewriteHandler.c
      11                 :             :  *
      12                 :             :  * NOTES
      13                 :             :  *    Some of the terms used in this file are of historic nature: "retrieve"
      14                 :             :  *    was the PostQUEL keyword for what today is SELECT. "RIR" stands for
      15                 :             :  *    "Retrieve-Instead-Retrieve", that is an ON SELECT DO INSTEAD SELECT rule
      16                 :             :  *    (which has to be unconditional and where only one rule can exist on each
      17                 :             :  *    relation).
      18                 :             :  *
      19                 :             :  *-------------------------------------------------------------------------
      20                 :             :  */
      21                 :             : #include "postgres.h"
      22                 :             : 
      23                 :             : #include "access/relation.h"
      24                 :             : #include "access/sysattr.h"
      25                 :             : #include "access/table.h"
      26                 :             : #include "catalog/dependency.h"
      27                 :             : #include "commands/trigger.h"
      28                 :             : #include "executor/executor.h"
      29                 :             : #include "foreign/fdwapi.h"
      30                 :             : #include "miscadmin.h"
      31                 :             : #include "nodes/makefuncs.h"
      32                 :             : #include "nodes/nodeFuncs.h"
      33                 :             : #include "optimizer/optimizer.h"
      34                 :             : #include "parser/analyze.h"
      35                 :             : #include "parser/parse_coerce.h"
      36                 :             : #include "parser/parse_relation.h"
      37                 :             : #include "parser/parsetree.h"
      38                 :             : #include "rewrite/rewriteDefine.h"
      39                 :             : #include "rewrite/rewriteGraphTable.h"
      40                 :             : #include "rewrite/rewriteHandler.h"
      41                 :             : #include "rewrite/rewriteManip.h"
      42                 :             : #include "rewrite/rewriteSearchCycle.h"
      43                 :             : #include "rewrite/rowsecurity.h"
      44                 :             : #include "tcop/tcopprot.h"
      45                 :             : #include "utils/builtins.h"
      46                 :             : #include "utils/lsyscache.h"
      47                 :             : #include "utils/rel.h"
      48                 :             : 
      49                 :             : 
      50                 :             : /* We use a list of these to detect recursion in RewriteQuery */
      51                 :             : typedef struct rewrite_event
      52                 :             : {
      53                 :             :     Oid         relation;       /* OID of relation having rules */
      54                 :             :     CmdType     event;          /* type of rule being fired */
      55                 :             : } rewrite_event;
      56                 :             : 
      57                 :             : typedef struct acquireLocksOnSubLinks_context
      58                 :             : {
      59                 :             :     bool        for_execute;    /* AcquireRewriteLocks' forExecute param */
      60                 :             : } acquireLocksOnSubLinks_context;
      61                 :             : 
      62                 :             : typedef struct fireRIRonSubLink_context
      63                 :             : {
      64                 :             :     List       *activeRIRs;
      65                 :             :     bool        hasRowSecurity;
      66                 :             : } fireRIRonSubLink_context;
      67                 :             : 
      68                 :             : static bool acquireLocksOnSubLinks(Node *node,
      69                 :             :                                    acquireLocksOnSubLinks_context *context);
      70                 :             : static Query *rewriteRuleAction(Query *parsetree,
      71                 :             :                                 Query *rule_action,
      72                 :             :                                 Node *rule_qual,
      73                 :             :                                 int rt_index,
      74                 :             :                                 CmdType event,
      75                 :             :                                 bool *returning_flag);
      76                 :             : static List *adjustJoinTreeList(Query *parsetree, bool removert, int rt_index);
      77                 :             : static List *rewriteTargetListIU(List *targetList,
      78                 :             :                                  CmdType commandType,
      79                 :             :                                  OverridingKind override,
      80                 :             :                                  Relation target_relation,
      81                 :             :                                  RangeTblEntry *values_rte,
      82                 :             :                                  int values_rte_index,
      83                 :             :                                  Bitmapset **unused_values_attrnos);
      84                 :             : static TargetEntry *process_matched_tle(TargetEntry *src_tle,
      85                 :             :                                         TargetEntry *prior_tle,
      86                 :             :                                         const char *attrName);
      87                 :             : static Node *get_assignment_input(Node *node);
      88                 :             : static Bitmapset *findDefaultOnlyColumns(RangeTblEntry *rte);
      89                 :             : static bool rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
      90                 :             :                              Relation target_relation,
      91                 :             :                              Bitmapset *unused_cols);
      92                 :             : static void rewriteValuesRTEToNulls(Query *parsetree, RangeTblEntry *rte);
      93                 :             : static void markQueryForLocking(Query *qry, Node *jtnode,
      94                 :             :                                 LockClauseStrength strength, LockWaitPolicy waitPolicy,
      95                 :             :                                 bool pushedDown);
      96                 :             : static List *matchLocks(CmdType event, Relation relation,
      97                 :             :                         int varno, Query *parsetree, bool *hasUpdate);
      98                 :             : static Query *fireRIRrules(Query *parsetree, List *activeRIRs);
      99                 :             : static Bitmapset *adjust_view_column_set(Bitmapset *cols, List *targetlist);
     100                 :             : static List *get_generated_columns(Relation rel, int rt_index, bool include_stored);
     101                 :             : 
     102                 :             : 
     103                 :             : /*
     104                 :             :  * AcquireRewriteLocks -
     105                 :             :  *    Acquire suitable locks on all the relations mentioned in the Query.
     106                 :             :  *    These locks will ensure that the relation schemas don't change under us
     107                 :             :  *    while we are rewriting, planning, and executing the query.
     108                 :             :  *
     109                 :             :  * Caution: this may modify the querytree, therefore caller should usually
     110                 :             :  * have done a copyObject() to make a writable copy of the querytree in the
     111                 :             :  * current memory context.
     112                 :             :  *
     113                 :             :  * forExecute indicates that the query is about to be executed.  If so,
     114                 :             :  * we'll acquire the lock modes specified in the RTE rellockmode fields.
     115                 :             :  * If forExecute is false, AccessShareLock is acquired on all relations.
     116                 :             :  * This case is suitable for ruleutils.c, for example, where we only need
     117                 :             :  * schema stability and we don't intend to actually modify any relations.
     118                 :             :  *
     119                 :             :  * forUpdatePushedDown indicates that a pushed-down FOR [KEY] UPDATE/SHARE
     120                 :             :  * applies to the current subquery, requiring all rels to be opened with at
     121                 :             :  * least RowShareLock.  This should always be false at the top of the
     122                 :             :  * recursion.  When it is true, we adjust RTE rellockmode fields to reflect
     123                 :             :  * the higher lock level.  This flag is ignored if forExecute is false.
     124                 :             :  *
     125                 :             :  * A secondary purpose of this routine is to fix up JOIN RTE references to
     126                 :             :  * dropped columns (see details below).  Such RTEs are modified in-place.
     127                 :             :  *
     128                 :             :  * This processing can, and for efficiency's sake should, be skipped when the
     129                 :             :  * querytree has just been built by the parser: parse analysis already got
     130                 :             :  * all the same locks we'd get here, and the parser will have omitted dropped
     131                 :             :  * columns from JOINs to begin with.  But we must do this whenever we are
     132                 :             :  * dealing with a querytree produced earlier than the current command.
     133                 :             :  *
     134                 :             :  * About JOINs and dropped columns: although the parser never includes an
     135                 :             :  * already-dropped column in a JOIN RTE's alias var list, it is possible for
     136                 :             :  * such a list in a stored rule to include references to dropped columns.
     137                 :             :  * (If the column is not explicitly referenced anywhere else in the query,
     138                 :             :  * the dependency mechanism won't consider it used by the rule and so won't
     139                 :             :  * prevent the column drop.)  To support get_rte_attribute_is_dropped(), we
     140                 :             :  * replace join alias vars that reference dropped columns with null pointers.
     141                 :             :  *
     142                 :             :  * (In PostgreSQL 8.0, we did not do this processing but instead had
     143                 :             :  * get_rte_attribute_is_dropped() recurse to detect dropped columns in joins.
     144                 :             :  * That approach had horrible performance unfortunately; in particular
     145                 :             :  * construction of a nested join was O(N^2) in the nesting depth.)
     146                 :             :  */
     147                 :             : void
     148                 :       28505 : AcquireRewriteLocks(Query *parsetree,
     149                 :             :                     bool forExecute,
     150                 :             :                     bool forUpdatePushedDown)
     151                 :             : {
     152                 :             :     ListCell   *l;
     153                 :             :     int         rt_index;
     154                 :             :     acquireLocksOnSubLinks_context context;
     155                 :             : 
     156                 :       28505 :     context.for_execute = forExecute;
     157                 :             : 
     158                 :             :     /*
     159                 :             :      * First, process RTEs of the current query level.
     160                 :             :      */
     161                 :       28505 :     rt_index = 0;
     162   [ +  +  +  +  :       92926 :     foreach(l, parsetree->rtable)
                   +  + ]
     163                 :             :     {
     164                 :       64421 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
     165                 :             :         Relation    rel;
     166                 :             :         LOCKMODE    lockmode;
     167                 :             :         List       *newaliasvars;
     168                 :             :         Index       curinputvarno;
     169                 :             :         RangeTblEntry *curinputrte;
     170                 :             :         ListCell   *ll;
     171                 :             : 
     172                 :       64421 :         ++rt_index;
     173   [ +  +  +  + ]:       64421 :         switch (rte->rtekind)
     174                 :             :         {
     175                 :       37848 :             case RTE_RELATION:
     176                 :             :             case RTE_GRAPH_TABLE:
     177                 :             : 
     178                 :             :                 /*
     179                 :             :                  * Grab the appropriate lock type for the relation, and do not
     180                 :             :                  * release it until end of transaction.  This protects the
     181                 :             :                  * rewriter, planner, and executor against schema changes
     182                 :             :                  * mid-query.
     183                 :             :                  *
     184                 :             :                  * If forExecute is false, ignore rellockmode and just use
     185                 :             :                  * AccessShareLock.
     186                 :             :                  */
     187         [ +  + ]:       37848 :                 if (!forExecute)
     188                 :        4779 :                     lockmode = AccessShareLock;
     189         [ +  + ]:       33069 :                 else if (forUpdatePushedDown)
     190                 :             :                 {
     191                 :             :                     /* Upgrade RTE's lock mode to reflect pushed-down lock */
     192         [ +  - ]:          64 :                     if (rte->rellockmode == AccessShareLock)
     193                 :          64 :                         rte->rellockmode = RowShareLock;
     194                 :          64 :                     lockmode = rte->rellockmode;
     195                 :             :                 }
     196                 :             :                 else
     197                 :       33005 :                     lockmode = rte->rellockmode;
     198                 :             : 
     199                 :       37848 :                 rel = relation_open(rte->relid, lockmode);
     200                 :             : 
     201                 :             :                 /*
     202                 :             :                  * While we have the relation open, update the RTE's relkind,
     203                 :             :                  * just in case it changed since this rule was made.
     204                 :             :                  */
     205                 :       37848 :                 rte->relkind = rel->rd_rel->relkind;
     206                 :             : 
     207                 :       37848 :                 relation_close(rel, NoLock);
     208                 :       37848 :                 break;
     209                 :             : 
     210                 :       14832 :             case RTE_JOIN:
     211                 :             : 
     212                 :             :                 /*
     213                 :             :                  * Scan the join's alias var list to see if any columns have
     214                 :             :                  * been dropped, and if so replace those Vars with null
     215                 :             :                  * pointers.
     216                 :             :                  *
     217                 :             :                  * Since a join has only two inputs, we can expect to see
     218                 :             :                  * multiple references to the same input RTE; optimize away
     219                 :             :                  * multiple fetches.
     220                 :             :                  */
     221                 :       14832 :                 newaliasvars = NIL;
     222                 :       14832 :                 curinputvarno = 0;
     223                 :       14832 :                 curinputrte = NULL;
     224   [ +  -  +  +  :      595971 :                 foreach(ll, rte->joinaliasvars)
                   +  + ]
     225                 :             :                 {
     226                 :      581139 :                     Var        *aliasitem = (Var *) lfirst(ll);
     227                 :      581139 :                     Var        *aliasvar = aliasitem;
     228                 :             : 
     229                 :             :                     /* Look through any implicit coercion */
     230                 :      581139 :                     aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
     231                 :             : 
     232                 :             :                     /*
     233                 :             :                      * If the list item isn't a simple Var, then it must
     234                 :             :                      * represent a merged column, ie a USING column, and so it
     235                 :             :                      * couldn't possibly be dropped, since it's referenced in
     236                 :             :                      * the join clause.  (Conceivably it could also be a null
     237                 :             :                      * pointer already?  But that's OK too.)
     238                 :             :                      */
     239   [ +  -  +  + ]:      581139 :                     if (aliasvar && IsA(aliasvar, Var))
     240                 :             :                     {
     241                 :             :                         /*
     242                 :             :                          * The elements of an alias list have to refer to
     243                 :             :                          * earlier RTEs of the same rtable, because that's the
     244                 :             :                          * order the planner builds things in.  So we already
     245                 :             :                          * processed the referenced RTE, and so it's safe to
     246                 :             :                          * use get_rte_attribute_is_dropped on it. (This might
     247                 :             :                          * not hold after rewriting or planning, but it's OK
     248                 :             :                          * to assume here.)
     249                 :             :                          */
     250                 :             :                         Assert(aliasvar->varlevelsup == 0);
     251         [ +  + ]:      581023 :                         if (aliasvar->varno != curinputvarno)
     252                 :             :                         {
     253                 :       39588 :                             curinputvarno = aliasvar->varno;
     254         [ -  + ]:       39588 :                             if (curinputvarno >= rt_index)
     255         [ #  # ]:           0 :                                 elog(ERROR, "unexpected varno %d in JOIN RTE %d",
     256                 :             :                                      curinputvarno, rt_index);
     257                 :       39588 :                             curinputrte = rt_fetch(curinputvarno,
     258                 :             :                                                    parsetree->rtable);
     259                 :             :                         }
     260         [ +  + ]:      581023 :                         if (get_rte_attribute_is_dropped(curinputrte,
     261                 :      581023 :                                                          aliasvar->varattno))
     262                 :             :                         {
     263                 :             :                             /* Replace the join alias item with a NULL */
     264                 :           4 :                             aliasitem = NULL;
     265                 :             :                         }
     266                 :             :                     }
     267                 :      581139 :                     newaliasvars = lappend(newaliasvars, aliasitem);
     268                 :             :                 }
     269                 :       14832 :                 rte->joinaliasvars = newaliasvars;
     270                 :       14832 :                 break;
     271                 :             : 
     272                 :        5255 :             case RTE_SUBQUERY:
     273                 :             : 
     274                 :             :                 /*
     275                 :             :                  * The subquery RTE itself is all right, but we have to
     276                 :             :                  * recurse to process the represented subquery.
     277                 :             :                  */
     278                 :        5255 :                 AcquireRewriteLocks(rte->subquery,
     279                 :             :                                     forExecute,
     280   [ +  -  -  + ]:       10510 :                                     (forUpdatePushedDown ||
     281                 :       10510 :                                      get_parse_rowmark(parsetree, rt_index) != NULL));
     282                 :        5255 :                 break;
     283                 :             : 
     284                 :        6486 :             default:
     285                 :             :                 /* ignore other types of RTEs */
     286                 :        6486 :                 break;
     287                 :             :         }
     288                 :             :     }
     289                 :             : 
     290                 :             :     /* Recurse into subqueries in WITH */
     291   [ +  +  +  +  :       28718 :     foreach(l, parsetree->cteList)
                   +  + ]
     292                 :             :     {
     293                 :         213 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
     294                 :             : 
     295                 :         213 :         AcquireRewriteLocks((Query *) cte->ctequery, forExecute, false);
     296                 :             :     }
     297                 :             : 
     298                 :             :     /*
     299                 :             :      * Recurse into sublink subqueries, too.  But we already did the ones in
     300                 :             :      * the rtable and cteList.
     301                 :             :      */
     302         [ +  + ]:       28505 :     if (parsetree->hasSubLinks)
     303                 :        1358 :         query_tree_walker(parsetree, acquireLocksOnSubLinks, &context,
     304                 :             :                           QTW_IGNORE_RC_SUBQUERIES);
     305                 :       28505 : }
     306                 :             : 
     307                 :             : /*
     308                 :             :  * Walker to find sublink subqueries for AcquireRewriteLocks
     309                 :             :  */
     310                 :             : static bool
     311                 :      131842 : acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
     312                 :             : {
     313         [ +  + ]:      131842 :     if (node == NULL)
     314                 :       26719 :         return false;
     315         [ +  + ]:      105123 :     if (IsA(node, SubLink))
     316                 :             :     {
     317                 :        2947 :         SubLink    *sub = (SubLink *) node;
     318                 :             : 
     319                 :             :         /* Do what we came for */
     320                 :        2947 :         AcquireRewriteLocks((Query *) sub->subselect,
     321                 :        2947 :                             context->for_execute,
     322                 :             :                             false);
     323                 :             :         /* Fall through to process lefthand args of SubLink */
     324                 :             :     }
     325                 :             : 
     326                 :             :     /*
     327                 :             :      * Do NOT recurse into Query nodes, because AcquireRewriteLocks already
     328                 :             :      * processed subselects of subselects for us.
     329                 :             :      */
     330                 :      105123 :     return expression_tree_walker(node, acquireLocksOnSubLinks, context);
     331                 :             : }
     332                 :             : 
     333                 :             : 
     334                 :             : /*
     335                 :             :  * rewriteRuleAction -
     336                 :             :  *    Rewrite the rule action with appropriate qualifiers (taken from
     337                 :             :  *    the triggering query).
     338                 :             :  *
     339                 :             :  * Input arguments:
     340                 :             :  *  parsetree - original query
     341                 :             :  *  rule_action - one action (query) of a rule
     342                 :             :  *  rule_qual - WHERE condition of rule, or NULL if unconditional
     343                 :             :  *  rt_index - RT index of result relation in original query
     344                 :             :  *  event - type of rule event
     345                 :             :  * Output arguments:
     346                 :             :  *  *returning_flag - set true if we rewrite RETURNING clause in rule_action
     347                 :             :  *                  (must be initialized to false)
     348                 :             :  * Return value:
     349                 :             :  *  rewritten form of rule_action
     350                 :             :  */
     351                 :             : static Query *
     352                 :         992 : rewriteRuleAction(Query *parsetree,
     353                 :             :                   Query *rule_action,
     354                 :             :                   Node *rule_qual,
     355                 :             :                   int rt_index,
     356                 :             :                   CmdType event,
     357                 :             :                   bool *returning_flag)
     358                 :             : {
     359                 :             :     int         current_varno,
     360                 :             :                 new_varno;
     361                 :             :     int         rt_length;
     362                 :             :     Query      *sub_action;
     363                 :             :     Query     **sub_action_ptr;
     364                 :             :     acquireLocksOnSubLinks_context context;
     365                 :             :     ListCell   *lc;
     366                 :             : 
     367                 :         992 :     context.for_execute = true;
     368                 :             : 
     369                 :             :     /*
     370                 :             :      * Make modifiable copies of rule action and qual (what we're passed are
     371                 :             :      * the stored versions in the relcache; don't touch 'em!).
     372                 :             :      */
     373                 :         992 :     rule_action = copyObject(rule_action);
     374                 :         992 :     rule_qual = copyObject(rule_qual);
     375                 :             : 
     376                 :             :     /*
     377                 :             :      * Acquire necessary locks and fix any deleted JOIN RTE entries.
     378                 :             :      */
     379                 :         992 :     AcquireRewriteLocks(rule_action, true, false);
     380                 :         992 :     (void) acquireLocksOnSubLinks(rule_qual, &context);
     381                 :             : 
     382                 :         992 :     current_varno = rt_index;
     383                 :         992 :     rt_length = list_length(parsetree->rtable);
     384                 :         992 :     new_varno = PRS2_NEW_VARNO + rt_length;
     385                 :             : 
     386                 :             :     /*
     387                 :             :      * Adjust rule action and qual to offset its varnos, so that we can merge
     388                 :             :      * its rtable with the main parsetree's rtable.
     389                 :             :      *
     390                 :             :      * If the rule action is an INSERT...SELECT, the OLD/NEW rtable entries
     391                 :             :      * will be in the SELECT part, and we have to modify that rather than the
     392                 :             :      * top-level INSERT (kluge!).
     393                 :             :      */
     394                 :         992 :     sub_action = getInsertSelectQuery(rule_action, &sub_action_ptr);
     395                 :             : 
     396                 :         992 :     OffsetVarNodes((Node *) sub_action, rt_length, 0);
     397                 :         992 :     OffsetVarNodes(rule_qual, rt_length, 0);
     398                 :             :     /* but references to OLD should point at original rt_index */
     399                 :         992 :     ChangeVarNodes((Node *) sub_action,
     400                 :             :                    PRS2_OLD_VARNO + rt_length, rt_index, 0);
     401                 :         992 :     ChangeVarNodes(rule_qual,
     402                 :             :                    PRS2_OLD_VARNO + rt_length, rt_index, 0);
     403                 :             : 
     404                 :             :     /*
     405                 :             :      * Mark any subquery RTEs in the rule action as LATERAL if they contain
     406                 :             :      * Vars referring to the current query level (references to NEW/OLD).
     407                 :             :      * Those really are lateral references, but we've historically not
     408                 :             :      * required users to mark such subqueries with LATERAL explicitly.  But
     409                 :             :      * the planner will complain if such Vars exist in a non-LATERAL subquery,
     410                 :             :      * so we have to fix things up here.
     411                 :             :      */
     412   [ +  +  +  +  :        3948 :     foreach(lc, sub_action->rtable)
                   +  + ]
     413                 :             :     {
     414                 :        2956 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
     415                 :             : 
     416   [ +  +  +  -  :        2964 :         if (rte->rtekind == RTE_SUBQUERY && !rte->lateral &&
                   +  - ]
     417                 :           8 :             contain_vars_of_level((Node *) rte->subquery, 1))
     418                 :           8 :             rte->lateral = true;
     419                 :             :     }
     420                 :             : 
     421                 :             :     /*
     422                 :             :      * Generate expanded rtable consisting of main parsetree's rtable plus
     423                 :             :      * rule action's rtable; this becomes the complete rtable for the rule
     424                 :             :      * action.  Some of the entries may be unused after we finish rewriting,
     425                 :             :      * but we leave them all in place to avoid having to adjust the query's
     426                 :             :      * varnos.  RT entries that are not referenced in the completed jointree
     427                 :             :      * will be ignored by the planner, so they do not affect query semantics.
     428                 :             :      *
     429                 :             :      * Also merge RTEPermissionInfo lists to ensure that all permissions are
     430                 :             :      * checked correctly.
     431                 :             :      *
     432                 :             :      * If the rule is INSTEAD, then the original query won't be executed at
     433                 :             :      * all, and so its rteperminfos must be preserved so that the executor
     434                 :             :      * will do the correct permissions checks on the relations referenced in
     435                 :             :      * it. This allows us to check that the caller has, say, insert-permission
     436                 :             :      * on a view, when the view is not semantically referenced at all in the
     437                 :             :      * resulting query.
     438                 :             :      *
     439                 :             :      * When a rule is not INSTEAD, the permissions checks done using the
     440                 :             :      * copied entries will be redundant with those done during execution of
     441                 :             :      * the original query, but we don't bother to treat that case differently.
     442                 :             :      *
     443                 :             :      * NOTE: because planner will destructively alter rtable and rteperminfos,
     444                 :             :      * we must ensure that rule action's lists are separate and shares no
     445                 :             :      * substructure with the main query's lists.  Hence do a deep copy here
     446                 :             :      * for both.
     447                 :             :      */
     448                 :             :     {
     449                 :         992 :         List       *rtable_tail = sub_action->rtable;
     450                 :         992 :         List       *perminfos_tail = sub_action->rteperminfos;
     451                 :             : 
     452                 :             :         /*
     453                 :             :          * RewriteQuery relies on the fact that RT entries from the original
     454                 :             :          * query appear at the start of the expanded rtable, so we put the
     455                 :             :          * action's original table at the end of the list.
     456                 :             :          */
     457                 :         992 :         sub_action->rtable = copyObject(parsetree->rtable);
     458                 :         992 :         sub_action->rteperminfos = copyObject(parsetree->rteperminfos);
     459                 :         992 :         CombineRangeTables(&sub_action->rtable, &sub_action->rteperminfos,
     460                 :             :                            rtable_tail, perminfos_tail);
     461                 :             :     }
     462                 :             : 
     463                 :             :     /*
     464                 :             :      * There could have been some SubLinks in parsetree's rtable, in which
     465                 :             :      * case we'd better mark the sub_action correctly.
     466                 :             :      */
     467   [ +  +  +  - ]:         992 :     if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
     468                 :             :     {
     469   [ +  -  +  +  :          60 :         foreach(lc, parsetree->rtable)
                   +  + ]
     470                 :             :         {
     471                 :          40 :             RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
     472                 :             : 
     473   [ +  -  -  -  :          40 :             switch (rte->rtekind)
                      + ]
     474                 :             :             {
     475                 :          36 :                 case RTE_RELATION:
     476                 :          36 :                     sub_action->hasSubLinks =
     477                 :          36 :                         checkExprHasSubLink((Node *) rte->tablesample);
     478                 :          36 :                     break;
     479                 :           0 :                 case RTE_FUNCTION:
     480                 :           0 :                     sub_action->hasSubLinks =
     481                 :           0 :                         checkExprHasSubLink((Node *) rte->functions);
     482                 :           0 :                     break;
     483                 :           0 :                 case RTE_TABLEFUNC:
     484                 :           0 :                     sub_action->hasSubLinks =
     485                 :           0 :                         checkExprHasSubLink((Node *) rte->tablefunc);
     486                 :           0 :                     break;
     487                 :           0 :                 case RTE_VALUES:
     488                 :           0 :                     sub_action->hasSubLinks =
     489                 :           0 :                         checkExprHasSubLink((Node *) rte->values_lists);
     490                 :           0 :                     break;
     491                 :           4 :                 default:
     492                 :             :                     /* other RTE types don't contain bare expressions */
     493                 :           4 :                     break;
     494                 :             :             }
     495                 :          40 :             sub_action->hasSubLinks |=
     496                 :          40 :                 checkExprHasSubLink((Node *) rte->securityQuals);
     497         [ +  + ]:          40 :             if (sub_action->hasSubLinks)
     498                 :           4 :                 break;          /* no need to keep scanning rtable */
     499                 :             :         }
     500                 :             :     }
     501                 :             : 
     502                 :             :     /*
     503                 :             :      * Also, we might have absorbed some RTEs with RLS conditions into the
     504                 :             :      * sub_action.  If so, mark it as hasRowSecurity, whether or not those
     505                 :             :      * RTEs will be referenced after we finish rewriting.  (Note: currently
     506                 :             :      * this is a no-op because RLS conditions aren't added till later, but it
     507                 :             :      * seems like good future-proofing to do this anyway.)
     508                 :             :      */
     509                 :         992 :     sub_action->hasRowSecurity |= parsetree->hasRowSecurity;
     510                 :             : 
     511                 :             :     /*
     512                 :             :      * Each rule action's jointree should be the main parsetree's jointree
     513                 :             :      * plus that rule's jointree, but usually *without* the original rtindex
     514                 :             :      * that we're replacing (if present, which it won't be for INSERT). Note
     515                 :             :      * that if the rule action refers to OLD, its jointree will add a
     516                 :             :      * reference to rt_index.  If the rule action doesn't refer to OLD, but
     517                 :             :      * either the rule_qual or the user query quals do, then we need to keep
     518                 :             :      * the original rtindex in the jointree to provide data for the quals.  We
     519                 :             :      * don't want the original rtindex to be joined twice, however, so avoid
     520                 :             :      * keeping it if the rule action mentions it.
     521                 :             :      *
     522                 :             :      * As above, the action's jointree must not share substructure with the
     523                 :             :      * main parsetree's.
     524                 :             :      */
     525         [ +  + ]:         992 :     if (sub_action->commandType != CMD_UTILITY)
     526                 :             :     {
     527                 :             :         bool        keeporig;
     528                 :             :         List       *newjointree;
     529                 :             : 
     530                 :             :         Assert(sub_action->jointree != NULL);
     531                 :         972 :         keeporig = (!rangeTableEntry_used((Node *) sub_action->jointree,
     532   [ +  +  +  - ]:        2292 :                                           rt_index, 0)) &&
     533         [ +  + ]:        1320 :             (rangeTableEntry_used(rule_qual, rt_index, 0) ||
     534                 :         660 :              rangeTableEntry_used(parsetree->jointree->quals, rt_index, 0));
     535                 :         972 :         newjointree = adjustJoinTreeList(parsetree, !keeporig, rt_index);
     536         [ +  + ]:         972 :         if (newjointree != NIL)
     537                 :             :         {
     538                 :             :             /*
     539                 :             :              * If sub_action is a setop, manipulating its jointree will do no
     540                 :             :              * good at all, because the jointree is dummy.  (Perhaps someday
     541                 :             :              * we could push the joining and quals down to the member
     542                 :             :              * statements of the setop?)
     543                 :             :              */
     544         [ -  + ]:         196 :             if (sub_action->setOperations != NULL)
     545         [ #  # ]:           0 :                 ereport(ERROR,
     546                 :             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     547                 :             :                          errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
     548                 :             : 
     549                 :         392 :             sub_action->jointree->fromlist =
     550                 :         196 :                 list_concat(newjointree, sub_action->jointree->fromlist);
     551                 :             : 
     552                 :             :             /*
     553                 :             :              * There could have been some SubLinks in newjointree, in which
     554                 :             :              * case we'd better mark the sub_action correctly.
     555                 :             :              */
     556   [ +  +  +  - ]:         196 :             if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
     557                 :           4 :                 sub_action->hasSubLinks =
     558                 :           4 :                     checkExprHasSubLink((Node *) newjointree);
     559                 :             :         }
     560                 :             :     }
     561                 :             : 
     562                 :             :     /*
     563                 :             :      * If the original query has any CTEs, copy them into the rule action. But
     564                 :             :      * we don't need them for a utility action.
     565                 :             :      */
     566   [ +  +  +  - ]:         992 :     if (parsetree->cteList != NIL && sub_action->commandType != CMD_UTILITY)
     567                 :             :     {
     568                 :             :         /*
     569                 :             :          * Annoying implementation restriction: because CTEs are identified by
     570                 :             :          * name within a cteList, we can't merge a CTE from the original query
     571                 :             :          * if it has the same name as any CTE in the rule action.
     572                 :             :          *
     573                 :             :          * This could possibly be fixed by using some sort of internally
     574                 :             :          * generated ID, instead of names, to link CTE RTEs to their CTEs.
     575                 :             :          * However, decompiling the results would be quite confusing; note the
     576                 :             :          * merge of hasRecursive flags below, which could change the apparent
     577                 :             :          * semantics of such redundantly-named CTEs.
     578                 :             :          */
     579   [ +  -  +  +  :          48 :         foreach(lc, parsetree->cteList)
                   +  + ]
     580                 :             :         {
     581                 :          24 :             CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
     582                 :             :             ListCell   *lc2;
     583                 :             : 
     584   [ +  +  +  +  :          28 :             foreach(lc2, sub_action->cteList)
                   +  + ]
     585                 :             :             {
     586                 :           4 :                 CommonTableExpr *cte2 = (CommonTableExpr *) lfirst(lc2);
     587                 :             : 
     588         [ -  + ]:           4 :                 if (strcmp(cte->ctename, cte2->ctename) == 0)
     589         [ #  # ]:           0 :                     ereport(ERROR,
     590                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     591                 :             :                              errmsg("WITH query name \"%s\" appears in both a rule action and the query being rewritten",
     592                 :             :                                     cte->ctename)));
     593                 :             :             }
     594                 :             :         }
     595                 :             : 
     596                 :             :         /*
     597                 :             :          * OK, it's safe to combine the CTE lists.  Beware that RewriteQuery
     598                 :             :          * knows we concatenate the lists in this order.
     599                 :             :          */
     600                 :          24 :         sub_action->cteList = list_concat(sub_action->cteList,
     601                 :          24 :                                           copyObject(parsetree->cteList));
     602                 :             :         /* ... and don't forget about the associated flags */
     603                 :          24 :         sub_action->hasRecursive |= parsetree->hasRecursive;
     604                 :          24 :         sub_action->hasModifyingCTE |= parsetree->hasModifyingCTE;
     605                 :             : 
     606                 :             :         /*
     607                 :             :          * If rule_action is different from sub_action (i.e., the rule action
     608                 :             :          * is an INSERT...SELECT), then we might have just added some
     609                 :             :          * data-modifying CTEs that are not at the top query level.  This is
     610                 :             :          * disallowed by the parser and we mustn't generate such trees here
     611                 :             :          * either, so throw an error.
     612                 :             :          *
     613                 :             :          * Conceivably such cases could be supported by attaching the original
     614                 :             :          * query's CTEs to rule_action not sub_action.  But to do that, we'd
     615                 :             :          * have to increment ctelevelsup in RTEs and SubLinks copied from the
     616                 :             :          * original query.  For now, it doesn't seem worth the trouble.
     617                 :             :          */
     618   [ +  +  +  + ]:          24 :         if (sub_action->hasModifyingCTE && rule_action != sub_action)
     619         [ +  - ]:           4 :             ereport(ERROR,
     620                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     621                 :             :                      errmsg("INSERT ... SELECT rule actions are not supported for queries having data-modifying statements in WITH")));
     622                 :             :     }
     623                 :             : 
     624                 :             :     /*
     625                 :             :      * Event Qualification forces copying of parsetree and splitting into two
     626                 :             :      * queries one w/rule_qual, one w/NOT rule_qual. Also add user query qual
     627                 :             :      * onto rule action
     628                 :             :      */
     629                 :         988 :     AddQual(sub_action, rule_qual);
     630                 :             : 
     631                 :         988 :     AddQual(sub_action, parsetree->jointree->quals);
     632                 :             : 
     633                 :             :     /*
     634                 :             :      * Rewrite new.attribute with right hand side of target-list entry for
     635                 :             :      * appropriate field name in insert/update.
     636                 :             :      *
     637                 :             :      * KLUGE ALERT: since ReplaceVarsFromTargetList returns a mutated copy, we
     638                 :             :      * can't just apply it to sub_action; we have to remember to update the
     639                 :             :      * sublink inside rule_action, too.
     640                 :             :      */
     641   [ +  +  +  + ]:         988 :     if ((event == CMD_INSERT || event == CMD_UPDATE) &&
     642         [ +  + ]:         868 :         sub_action->commandType != CMD_UTILITY)
     643                 :             :     {
     644                 :         848 :         RangeTblEntry *new_rte = rt_fetch(new_varno, sub_action->rtable);
     645                 :             :         Relation    new_rel;
     646                 :             :         List       *gen_cols;
     647                 :             : 
     648                 :             :         /*
     649                 :             :          * The target list does not contain entries for generated columns
     650                 :             :          * (they are removed by rewriteTargetListIU), so we must build entries
     651                 :             :          * for them here, so that new.gen_col can be rewritten correctly.
     652                 :             :          */
     653                 :         848 :         new_rel = relation_open(new_rte->relid, NoLock);
     654                 :         848 :         gen_cols = get_generated_columns(new_rel, new_varno, true);
     655                 :         848 :         relation_close(new_rel, NoLock);
     656                 :             : 
     657                 :             :         /*
     658                 :             :          * The generated column expressions refer to new.attribute, so they
     659                 :             :          * must be rewritten before they can be used as replacements.
     660                 :             :          */
     661                 :             :         gen_cols = (List *)
     662         [ +  + ]:         848 :             ReplaceVarsFromTargetList((Node *) gen_cols,
     663                 :             :                                       new_varno,
     664                 :             :                                       0,
     665                 :             :                                       new_rte,
     666                 :             :                                       parsetree->targetList,
     667                 :             :                                       sub_action->resultRelation,
     668                 :             :                                       (event == CMD_UPDATE) ?
     669                 :             :                                       REPLACEVARS_CHANGE_VARNO :
     670                 :             :                                       REPLACEVARS_SUBSTITUTE_NULL,
     671                 :             :                                       current_varno,
     672                 :             :                                       &sub_action->hasSubLinks);
     673                 :             : 
     674                 :             :         /*
     675                 :             :          * Now rewrite new.attribute in sub_action, using both the target list
     676                 :             :          * and the rewritten generated column expressions.
     677                 :             :          */
     678                 :             :         sub_action = (Query *)
     679         [ +  + ]:        1696 :             ReplaceVarsFromTargetList((Node *) sub_action,
     680                 :             :                                       new_varno,
     681                 :             :                                       0,
     682                 :             :                                       new_rte,
     683                 :         848 :                                       list_concat(gen_cols, parsetree->targetList),
     684                 :             :                                       sub_action->resultRelation,
     685                 :             :                                       (event == CMD_UPDATE) ?
     686                 :             :                                       REPLACEVARS_CHANGE_VARNO :
     687                 :             :                                       REPLACEVARS_SUBSTITUTE_NULL,
     688                 :             :                                       current_varno,
     689                 :             :                                       NULL);
     690         [ +  + ]:         848 :         if (sub_action_ptr)
     691                 :          36 :             *sub_action_ptr = sub_action;
     692                 :             :         else
     693                 :         812 :             rule_action = sub_action;
     694                 :             :     }
     695                 :             : 
     696                 :             :     /*
     697                 :             :      * If rule_action is INSERT .. ON CONFLICT DO SELECT, the parser should
     698                 :             :      * have verified that it has a RETURNING clause, but we must also check
     699                 :             :      * that the triggering query has a RETURNING clause.
     700                 :             :      */
     701         [ +  + ]:         988 :     if (rule_action->onConflict &&
     702         [ +  + ]:          52 :         rule_action->onConflict->action == ONCONFLICT_SELECT &&
     703   [ +  -  +  + ]:          20 :         (!rule_action->returningList || !parsetree->returningList))
     704         [ +  - ]:           4 :         ereport(ERROR,
     705                 :             :                 errcode(ERRCODE_SYNTAX_ERROR),
     706                 :             :                 errmsg("ON CONFLICT DO SELECT requires a RETURNING clause"),
     707                 :             :                 errdetail("A rule action is INSERT ... ON CONFLICT DO SELECT, which requires a RETURNING clause."));
     708                 :             : 
     709                 :             :     /*
     710                 :             :      * If rule_action has a RETURNING clause, then either throw it away if the
     711                 :             :      * triggering query has no RETURNING clause, or rewrite it to emit what
     712                 :             :      * the triggering query's RETURNING clause asks for.  Throw an error if
     713                 :             :      * more than one rule has a RETURNING clause.
     714                 :             :      */
     715         [ +  + ]:         984 :     if (!parsetree->returningList)
     716                 :         864 :         rule_action->returningList = NIL;
     717         [ +  + ]:         120 :     else if (rule_action->returningList)
     718                 :             :     {
     719         [ -  + ]:         112 :         if (*returning_flag)
     720         [ #  # ]:           0 :             ereport(ERROR,
     721                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     722                 :             :                      errmsg("cannot have RETURNING lists in multiple rules")));
     723                 :         112 :         *returning_flag = true;
     724                 :         112 :         rule_action->returningList = (List *)
     725                 :         112 :             ReplaceVarsFromTargetList((Node *) parsetree->returningList,
     726                 :             :                                       parsetree->resultRelation,
     727                 :             :                                       0,
     728                 :         112 :                                       rt_fetch(parsetree->resultRelation,
     729                 :             :                                                parsetree->rtable),
     730                 :             :                                       rule_action->returningList,
     731                 :             :                                       rule_action->resultRelation,
     732                 :             :                                       REPLACEVARS_REPORT_ERROR,
     733                 :             :                                       0,
     734                 :             :                                       &rule_action->hasSubLinks);
     735                 :             : 
     736                 :             :         /* use triggering query's aliases for OLD and NEW in RETURNING list */
     737                 :         112 :         rule_action->returningOldAlias = parsetree->returningOldAlias;
     738                 :         112 :         rule_action->returningNewAlias = parsetree->returningNewAlias;
     739                 :             : 
     740                 :             :         /*
     741                 :             :          * There could have been some SubLinks in parsetree's returningList,
     742                 :             :          * in which case we'd better mark the rule_action correctly.
     743                 :             :          */
     744   [ -  +  -  - ]:         112 :         if (parsetree->hasSubLinks && !rule_action->hasSubLinks)
     745                 :           0 :             rule_action->hasSubLinks =
     746                 :           0 :                 checkExprHasSubLink((Node *) rule_action->returningList);
     747                 :             :     }
     748                 :             : 
     749                 :         984 :     return rule_action;
     750                 :             : }
     751                 :             : 
     752                 :             : /*
     753                 :             :  * Copy the query's jointree list, and optionally attempt to remove any
     754                 :             :  * occurrence of the given rt_index as a top-level join item (we do not look
     755                 :             :  * for it within join items; this is OK because we are only expecting to find
     756                 :             :  * it as an UPDATE or DELETE target relation, which will be at the top level
     757                 :             :  * of the join).  Returns modified jointree list --- this is a separate copy
     758                 :             :  * sharing no nodes with the original.
     759                 :             :  */
     760                 :             : static List *
     761                 :         972 : adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
     762                 :             : {
     763                 :         972 :     List       *newjointree = copyObject(parsetree->jointree->fromlist);
     764                 :             :     ListCell   *l;
     765                 :             : 
     766         [ +  + ]:         972 :     if (removert)
     767                 :             :     {
     768   [ +  +  +  +  :        1120 :         foreach(l, newjointree)
                   +  + ]
     769                 :             :         {
     770                 :         508 :             RangeTblRef *rtr = lfirst(l);
     771                 :             : 
     772         [ +  - ]:         508 :             if (IsA(rtr, RangeTblRef) &&
     773         [ +  + ]:         508 :                 rtr->rtindex == rt_index)
     774                 :             :             {
     775                 :         348 :                 newjointree = foreach_delete_current(newjointree, l);
     776                 :         348 :                 break;
     777                 :             :             }
     778                 :             :         }
     779                 :             :     }
     780                 :         972 :     return newjointree;
     781                 :             : }
     782                 :             : 
     783                 :             : 
     784                 :             : /*
     785                 :             :  * rewriteTargetListIU - rewrite INSERT/UPDATE targetlist into standard form
     786                 :             :  *
     787                 :             :  * This has the following responsibilities:
     788                 :             :  *
     789                 :             :  * 1. For an INSERT, add tlist entries to compute default values for any
     790                 :             :  * attributes that have defaults and are not assigned to in the given tlist.
     791                 :             :  * (We do not insert anything for default-less attributes, however.  The
     792                 :             :  * planner will later insert NULLs for them, but there's no reason to slow
     793                 :             :  * down rewriter processing with extra tlist nodes.)  Also, for both INSERT
     794                 :             :  * and UPDATE, replace explicit DEFAULT specifications with column default
     795                 :             :  * expressions.
     796                 :             :  *
     797                 :             :  * 2. Merge multiple entries for the same target attribute, or declare error
     798                 :             :  * if we can't.  Multiple entries are only allowed for INSERT/UPDATE of
     799                 :             :  * portions of an array or record field, for example
     800                 :             :  *          UPDATE table SET foo[2] = 42, foo[4] = 43;
     801                 :             :  * We can merge such operations into a single assignment op.  Essentially,
     802                 :             :  * the expression we want to produce in this case is like
     803                 :             :  *      foo = array_set_element(array_set_element(foo, 2, 42), 4, 43)
     804                 :             :  *
     805                 :             :  * 3. Sort the tlist into standard order: non-junk fields in order by resno,
     806                 :             :  * then junk fields (these in no particular order).
     807                 :             :  *
     808                 :             :  * We must do items 1 and 2 before firing rewrite rules, else rewritten
     809                 :             :  * references to NEW.foo will produce wrong or incomplete results.  Item 3
     810                 :             :  * is not needed for rewriting, but it is helpful for the planner, and we
     811                 :             :  * can do it essentially for free while handling the other items.
     812                 :             :  *
     813                 :             :  * If values_rte is non-NULL (i.e., we are doing a multi-row INSERT using
     814                 :             :  * values from a VALUES RTE), we populate *unused_values_attrnos with the
     815                 :             :  * attribute numbers of any unused columns from the VALUES RTE.  This can
     816                 :             :  * happen for identity and generated columns whose targetlist entries are
     817                 :             :  * replaced with generated expressions (if INSERT ... OVERRIDING USER VALUE is
     818                 :             :  * used, or all the values to be inserted are DEFAULT).  This information is
     819                 :             :  * required by rewriteValuesRTE() to handle any DEFAULT items in the unused
     820                 :             :  * columns.  The caller must have initialized *unused_values_attrnos to NULL.
     821                 :             :  */
     822                 :             : static List *
     823                 :       58754 : rewriteTargetListIU(List *targetList,
     824                 :             :                     CmdType commandType,
     825                 :             :                     OverridingKind override,
     826                 :             :                     Relation target_relation,
     827                 :             :                     RangeTblEntry *values_rte,
     828                 :             :                     int values_rte_index,
     829                 :             :                     Bitmapset **unused_values_attrnos)
     830                 :             : {
     831                 :             :     TargetEntry **new_tles;
     832                 :       58754 :     List       *new_tlist = NIL;
     833                 :       58754 :     List       *junk_tlist = NIL;
     834                 :             :     Form_pg_attribute att_tup;
     835                 :             :     int         attrno,
     836                 :             :                 next_junk_attrno,
     837                 :             :                 numattrs;
     838                 :             :     ListCell   *temp;
     839                 :       58754 :     Bitmapset  *default_only_cols = NULL;
     840                 :             : 
     841                 :             :     /*
     842                 :             :      * We process the normal (non-junk) attributes by scanning the input tlist
     843                 :             :      * once and transferring TLEs into an array, then scanning the array to
     844                 :             :      * build an output tlist.  This avoids O(N^2) behavior for large numbers
     845                 :             :      * of attributes.
     846                 :             :      *
     847                 :             :      * Junk attributes are tossed into a separate list during the same tlist
     848                 :             :      * scan, then appended to the reconstructed tlist.
     849                 :             :      */
     850                 :       58754 :     numattrs = RelationGetNumberOfAttributes(target_relation);
     851                 :       58754 :     new_tles = (TargetEntry **) palloc0(numattrs * sizeof(TargetEntry *));
     852                 :       58754 :     next_junk_attrno = numattrs + 1;
     853                 :             : 
     854   [ +  +  +  +  :      165250 :     foreach(temp, targetList)
                   +  + ]
     855                 :             :     {
     856                 :      106508 :         TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
     857                 :             : 
     858         [ +  + ]:      106508 :         if (!old_tle->resjunk)
     859                 :             :         {
     860                 :             :             /* Normal attr: stash it into new_tles[] */
     861                 :      106421 :             attrno = old_tle->resno;
     862   [ +  -  -  + ]:      106421 :             if (attrno < 1 || attrno > numattrs)
     863         [ #  # ]:           0 :                 elog(ERROR, "bogus resno %d in targetlist", attrno);
     864                 :      106421 :             att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
     865                 :             : 
     866                 :             :             /* We can (and must) ignore deleted attributes */
     867         [ -  + ]:      106421 :             if (att_tup->attisdropped)
     868                 :           0 :                 continue;
     869                 :             : 
     870                 :             :             /* Merge with any prior assignment to same attribute */
     871                 :      106409 :             new_tles[attrno - 1] =
     872                 :      106421 :                 process_matched_tle(old_tle,
     873                 :      106421 :                                     new_tles[attrno - 1],
     874                 :      106421 :                                     NameStr(att_tup->attname));
     875                 :             :         }
     876                 :             :         else
     877                 :             :         {
     878                 :             :             /*
     879                 :             :              * Copy all resjunk tlist entries to junk_tlist, and assign them
     880                 :             :              * resnos above the last real resno.
     881                 :             :              *
     882                 :             :              * Typical junk entries include ORDER BY or GROUP BY expressions
     883                 :             :              * (are these actually possible in an INSERT or UPDATE?), system
     884                 :             :              * attribute references, etc.
     885                 :             :              */
     886                 :             : 
     887                 :             :             /* Get the resno right, but don't copy unnecessarily */
     888         [ -  + ]:          87 :             if (old_tle->resno != next_junk_attrno)
     889                 :             :             {
     890                 :           0 :                 old_tle = flatCopyTargetEntry(old_tle);
     891                 :           0 :                 old_tle->resno = next_junk_attrno;
     892                 :             :             }
     893                 :          87 :             junk_tlist = lappend(junk_tlist, old_tle);
     894                 :          87 :             next_junk_attrno++;
     895                 :             :         }
     896                 :             :     }
     897                 :             : 
     898         [ +  + ]:      252643 :     for (attrno = 1; attrno <= numattrs; attrno++)
     899                 :             :     {
     900                 :      194045 :         TargetEntry *new_tle = new_tles[attrno - 1];
     901                 :             :         bool        apply_default;
     902                 :             : 
     903                 :      194045 :         att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
     904                 :             : 
     905                 :             :         /* We can (and must) ignore deleted attributes */
     906         [ +  + ]:      194045 :         if (att_tup->attisdropped)
     907                 :         664 :             continue;
     908                 :             : 
     909                 :             :         /*
     910                 :             :          * Handle the two cases where we need to insert a default expression:
     911                 :             :          * it's an INSERT and there's no tlist entry for the column, or the
     912                 :             :          * tlist entry is a DEFAULT placeholder node.
     913                 :             :          */
     914   [ +  +  +  +  :      299529 :         apply_default = ((new_tle == NULL && commandType == CMD_INSERT) ||
                   +  + ]
     915   [ +  -  +  + ]:      106148 :                          (new_tle && new_tle->expr && IsA(new_tle->expr, SetToDefault)));
     916                 :             : 
     917         [ +  + ]:      193381 :         if (commandType == CMD_INSERT)
     918                 :             :         {
     919                 :      106846 :             int         values_attrno = 0;
     920                 :             : 
     921                 :             :             /* Source attribute number for values that come from a VALUES RTE */
     922   [ +  +  +  +  :      106846 :             if (values_rte && new_tle && IsA(new_tle->expr, Var))
                   +  + ]
     923                 :             :             {
     924                 :        6512 :                 Var        *var = (Var *) new_tle->expr;
     925                 :             : 
     926         [ +  - ]:        6512 :                 if (var->varno == values_rte_index)
     927                 :        6512 :                     values_attrno = var->varattno;
     928                 :             :             }
     929                 :             : 
     930                 :             :             /*
     931                 :             :              * Can only insert DEFAULT into GENERATED ALWAYS identity columns,
     932                 :             :              * unless either OVERRIDING USER VALUE or OVERRIDING SYSTEM VALUE
     933                 :             :              * is specified.
     934                 :             :              */
     935   [ +  +  +  + ]:      106846 :             if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS && !apply_default)
     936                 :             :             {
     937         [ +  + ]:          94 :                 if (override == OVERRIDING_USER_VALUE)
     938                 :          28 :                     apply_default = true;
     939         [ +  + ]:          66 :                 else if (override != OVERRIDING_SYSTEM_VALUE)
     940                 :             :                 {
     941                 :             :                     /*
     942                 :             :                      * If this column's values come from a VALUES RTE, test
     943                 :             :                      * whether it contains only SetToDefault items.  Since the
     944                 :             :                      * VALUES list might be quite large, we arrange to only
     945                 :             :                      * scan it once.
     946                 :             :                      */
     947         [ +  + ]:          34 :                     if (values_attrno != 0)
     948                 :             :                     {
     949         [ +  - ]:          18 :                         if (default_only_cols == NULL)
     950                 :          18 :                             default_only_cols = findDefaultOnlyColumns(values_rte);
     951                 :             : 
     952         [ +  + ]:          18 :                         if (bms_is_member(values_attrno, default_only_cols))
     953                 :           6 :                             apply_default = true;
     954                 :             :                     }
     955                 :             : 
     956         [ +  + ]:          34 :                     if (!apply_default)
     957         [ +  - ]:          28 :                         ereport(ERROR,
     958                 :             :                                 (errcode(ERRCODE_GENERATED_ALWAYS),
     959                 :             :                                  errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
     960                 :             :                                         NameStr(att_tup->attname)),
     961                 :             :                                  errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
     962                 :             :                                            NameStr(att_tup->attname)),
     963                 :             :                                  errhint("Use OVERRIDING SYSTEM VALUE to override.")));
     964                 :             :                 }
     965                 :             :             }
     966                 :             : 
     967                 :             :             /*
     968                 :             :              * Although inserting into a GENERATED BY DEFAULT identity column
     969                 :             :              * is allowed, apply the default if OVERRIDING USER VALUE is
     970                 :             :              * specified.
     971                 :             :              */
     972   [ +  +  +  + ]:      106818 :             if (att_tup->attidentity == ATTRIBUTE_IDENTITY_BY_DEFAULT &&
     973                 :             :                 override == OVERRIDING_USER_VALUE)
     974                 :          12 :                 apply_default = true;
     975                 :             : 
     976                 :             :             /*
     977                 :             :              * Can only insert DEFAULT into generated columns.  (The
     978                 :             :              * OVERRIDING clause does not apply to generated columns, so we
     979                 :             :              * don't consider it here.)
     980                 :             :              */
     981   [ +  +  +  + ]:      106818 :             if (att_tup->attgenerated && !apply_default)
     982                 :             :             {
     983                 :             :                 /*
     984                 :             :                  * If this column's values come from a VALUES RTE, test
     985                 :             :                  * whether it contains only SetToDefault items, as above.
     986                 :             :                  */
     987         [ +  + ]:         117 :                 if (values_attrno != 0)
     988                 :             :                 {
     989         [ +  - ]:          81 :                     if (default_only_cols == NULL)
     990                 :          81 :                         default_only_cols = findDefaultOnlyColumns(values_rte);
     991                 :             : 
     992         [ +  + ]:          81 :                     if (bms_is_member(values_attrno, default_only_cols))
     993                 :          25 :                         apply_default = true;
     994                 :             :                 }
     995                 :             : 
     996         [ +  + ]:         117 :                 if (!apply_default)
     997         [ +  - ]:          92 :                     ereport(ERROR,
     998                 :             :                             (errcode(ERRCODE_GENERATED_ALWAYS),
     999                 :             :                              errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
    1000                 :             :                                     NameStr(att_tup->attname)),
    1001                 :             :                              errdetail("Column \"%s\" is a generated column.",
    1002                 :             :                                        NameStr(att_tup->attname))));
    1003                 :             :             }
    1004                 :             : 
    1005                 :             :             /*
    1006                 :             :              * For an INSERT from a VALUES RTE, return the attribute numbers
    1007                 :             :              * of any VALUES columns that will no longer be used (due to the
    1008                 :             :              * targetlist entry being replaced by a default expression).
    1009                 :             :              */
    1010   [ +  +  +  +  :      106726 :             if (values_attrno != 0 && apply_default && unused_values_attrnos)
                   +  - ]
    1011                 :          47 :                 *unused_values_attrnos = bms_add_member(*unused_values_attrnos,
    1012                 :             :                                                         values_attrno);
    1013                 :             :         }
    1014                 :             : 
    1015                 :             :         /*
    1016                 :             :          * Updates to identity and generated columns follow the same rules as
    1017                 :             :          * above, except that UPDATE doesn't admit OVERRIDING clauses.  Also,
    1018                 :             :          * the source can't be a VALUES RTE, so we needn't consider that.
    1019                 :             :          */
    1020         [ +  + ]:      193261 :         if (commandType == CMD_UPDATE)
    1021                 :             :         {
    1022   [ +  +  +  - ]:       86535 :             if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS &&
    1023         [ +  + ]:           8 :                 new_tle && !apply_default)
    1024         [ +  - ]:           4 :                 ereport(ERROR,
    1025                 :             :                         (errcode(ERRCODE_GENERATED_ALWAYS),
    1026                 :             :                          errmsg("column \"%s\" can only be updated to DEFAULT",
    1027                 :             :                                 NameStr(att_tup->attname)),
    1028                 :             :                          errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
    1029                 :             :                                    NameStr(att_tup->attname))));
    1030                 :             : 
    1031   [ +  +  +  +  :       86531 :             if (att_tup->attgenerated && new_tle && !apply_default)
                   +  + ]
    1032         [ +  - ]:          20 :                 ereport(ERROR,
    1033                 :             :                         (errcode(ERRCODE_GENERATED_ALWAYS),
    1034                 :             :                          errmsg("column \"%s\" can only be updated to DEFAULT",
    1035                 :             :                                 NameStr(att_tup->attname)),
    1036                 :             :                          errdetail("Column \"%s\" is a generated column.",
    1037                 :             :                                    NameStr(att_tup->attname))));
    1038                 :             :         }
    1039                 :             : 
    1040         [ +  + ]:      193237 :         if (att_tup->attgenerated)
    1041                 :             :         {
    1042                 :             :             /*
    1043                 :             :              * virtual generated column stores a null value; stored generated
    1044                 :             :              * column will be fixed in executor
    1045                 :             :              */
    1046                 :        1304 :             new_tle = NULL;
    1047                 :             :         }
    1048         [ +  + ]:      191933 :         else if (apply_default)
    1049                 :             :         {
    1050                 :             :             Node       *new_expr;
    1051                 :             : 
    1052                 :       15863 :             new_expr = build_column_default(target_relation, attrno);
    1053                 :             : 
    1054                 :             :             /*
    1055                 :             :              * If there is no default (ie, default is effectively NULL), we
    1056                 :             :              * can omit the tlist entry in the INSERT case, since the planner
    1057                 :             :              * can insert a NULL for itself, and there's no point in spending
    1058                 :             :              * any more rewriter cycles on the entry.  But in the UPDATE case
    1059                 :             :              * we've got to explicitly set the column to NULL.
    1060                 :             :              */
    1061         [ +  + ]:       15863 :             if (!new_expr)
    1062                 :             :             {
    1063         [ +  + ]:       11707 :                 if (commandType == CMD_INSERT)
    1064                 :       11694 :                     new_tle = NULL;
    1065                 :             :                 else
    1066                 :          13 :                     new_expr = coerce_null_to_domain(att_tup->atttypid,
    1067                 :             :                                                      att_tup->atttypmod,
    1068                 :             :                                                      att_tup->attcollation,
    1069                 :          13 :                                                      att_tup->attlen,
    1070                 :          13 :                                                      att_tup->attbyval);
    1071                 :             :             }
    1072                 :             : 
    1073         [ +  + ]:       15863 :             if (new_expr)
    1074                 :        4169 :                 new_tle = makeTargetEntry((Expr *) new_expr,
    1075                 :             :                                           attrno,
    1076                 :        4169 :                                           pstrdup(NameStr(att_tup->attname)),
    1077                 :             :                                           false);
    1078                 :             :         }
    1079                 :             : 
    1080         [ +  + ]:      193237 :         if (new_tle)
    1081                 :      109608 :             new_tlist = lappend(new_tlist, new_tle);
    1082                 :             :     }
    1083                 :             : 
    1084                 :       58598 :     pfree(new_tles);
    1085                 :             : 
    1086                 :       58598 :     return list_concat(new_tlist, junk_tlist);
    1087                 :             : }
    1088                 :             : 
    1089                 :             : 
    1090                 :             : /*
    1091                 :             :  * Convert a matched TLE from the original tlist into a correct new TLE.
    1092                 :             :  *
    1093                 :             :  * This routine detects and handles multiple assignments to the same target
    1094                 :             :  * attribute.  (The attribute name is needed only for error messages.)
    1095                 :             :  */
    1096                 :             : static TargetEntry *
    1097                 :      106421 : process_matched_tle(TargetEntry *src_tle,
    1098                 :             :                     TargetEntry *prior_tle,
    1099                 :             :                     const char *attrName)
    1100                 :             : {
    1101                 :             :     TargetEntry *result;
    1102                 :      106421 :     CoerceToDomain *coerce_expr = NULL;
    1103                 :             :     Node       *src_expr;
    1104                 :             :     Node       *prior_expr;
    1105                 :             :     Node       *src_input;
    1106                 :             :     Node       *prior_input;
    1107                 :             :     Node       *priorbottom;
    1108                 :             :     Node       *newexpr;
    1109                 :             : 
    1110         [ +  + ]:      106421 :     if (prior_tle == NULL)
    1111                 :             :     {
    1112                 :             :         /*
    1113                 :             :          * Normal case where this is the first assignment to the attribute.
    1114                 :             :          */
    1115                 :      106196 :         return src_tle;
    1116                 :             :     }
    1117                 :             : 
    1118                 :             :     /*----------
    1119                 :             :      * Multiple assignments to same attribute.  Allow only if all are
    1120                 :             :      * FieldStore or SubscriptingRef assignment operations.  This is a bit
    1121                 :             :      * tricky because what we may actually be looking at is a nest of
    1122                 :             :      * such nodes; consider
    1123                 :             :      *      UPDATE tab SET col.fld1.subfld1 = x, col.fld2.subfld2 = y
    1124                 :             :      * The two expressions produced by the parser will look like
    1125                 :             :      *      FieldStore(col, fld1, FieldStore(placeholder, subfld1, x))
    1126                 :             :      *      FieldStore(col, fld2, FieldStore(placeholder, subfld2, y))
    1127                 :             :      * However, we can ignore the substructure and just consider the top
    1128                 :             :      * FieldStore or SubscriptingRef from each assignment, because it works to
    1129                 :             :      * combine these as
    1130                 :             :      *      FieldStore(FieldStore(col, fld1,
    1131                 :             :      *                            FieldStore(placeholder, subfld1, x)),
    1132                 :             :      *                 fld2, FieldStore(placeholder, subfld2, y))
    1133                 :             :      * Note the leftmost expression goes on the inside so that the
    1134                 :             :      * assignments appear to occur left-to-right.
    1135                 :             :      *
    1136                 :             :      * For FieldStore, instead of nesting we can generate a single
    1137                 :             :      * FieldStore with multiple target fields.  We must nest when
    1138                 :             :      * SubscriptingRefs are involved though.
    1139                 :             :      *
    1140                 :             :      * As a further complication, the destination column might be a domain,
    1141                 :             :      * resulting in each assignment containing a CoerceToDomain node over a
    1142                 :             :      * FieldStore or SubscriptingRef.  These should have matching target
    1143                 :             :      * domains, so we strip them and reconstitute a single CoerceToDomain over
    1144                 :             :      * the combined FieldStore/SubscriptingRef nodes.  (Notice that this has
    1145                 :             :      * the result that the domain's checks are applied only after we do all
    1146                 :             :      * the field or element updates, not after each one.  This is desirable.)
    1147                 :             :      *----------
    1148                 :             :      */
    1149                 :         225 :     src_expr = (Node *) src_tle->expr;
    1150                 :         225 :     prior_expr = (Node *) prior_tle->expr;
    1151                 :             : 
    1152   [ +  -  +  +  :         225 :     if (src_expr && IsA(src_expr, CoerceToDomain) &&
                   +  - ]
    1153         [ +  - ]:         108 :         prior_expr && IsA(prior_expr, CoerceToDomain) &&
    1154                 :         108 :         ((CoerceToDomain *) src_expr)->resulttype ==
    1155         [ +  - ]:         108 :         ((CoerceToDomain *) prior_expr)->resulttype)
    1156                 :             :     {
    1157                 :             :         /* we assume without checking that resulttypmod/resultcollid match */
    1158                 :         108 :         coerce_expr = (CoerceToDomain *) src_expr;
    1159                 :         108 :         src_expr = (Node *) ((CoerceToDomain *) src_expr)->arg;
    1160                 :         108 :         prior_expr = (Node *) ((CoerceToDomain *) prior_expr)->arg;
    1161                 :             :     }
    1162                 :             : 
    1163                 :         225 :     src_input = get_assignment_input(src_expr);
    1164                 :         225 :     prior_input = get_assignment_input(prior_expr);
    1165   [ +  +  +  - ]:         225 :     if (src_input == NULL ||
    1166         [ -  + ]:         213 :         prior_input == NULL ||
    1167                 :         213 :         exprType(src_expr) != exprType(prior_expr))
    1168         [ +  - ]:          12 :         ereport(ERROR,
    1169                 :             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1170                 :             :                  errmsg("multiple assignments to same column \"%s\"",
    1171                 :             :                         attrName)));
    1172                 :             : 
    1173                 :             :     /*
    1174                 :             :      * Prior TLE could be a nest of assignments if we do this more than once.
    1175                 :             :      */
    1176                 :         213 :     priorbottom = prior_input;
    1177                 :             :     for (;;)
    1178                 :          28 :     {
    1179                 :         241 :         Node       *newbottom = get_assignment_input(priorbottom);
    1180                 :             : 
    1181         [ +  + ]:         241 :         if (newbottom == NULL)
    1182                 :         213 :             break;              /* found the original Var reference */
    1183                 :          28 :         priorbottom = newbottom;
    1184                 :             :     }
    1185         [ -  + ]:         213 :     if (!equal(priorbottom, src_input))
    1186         [ #  # ]:           0 :         ereport(ERROR,
    1187                 :             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1188                 :             :                  errmsg("multiple assignments to same column \"%s\"",
    1189                 :             :                         attrName)));
    1190                 :             : 
    1191                 :             :     /*
    1192                 :             :      * Looks OK to nest 'em.
    1193                 :             :      */
    1194         [ +  + ]:         213 :     if (IsA(src_expr, FieldStore))
    1195                 :             :     {
    1196                 :          84 :         FieldStore *fstore = makeNode(FieldStore);
    1197                 :             : 
    1198         [ +  - ]:          84 :         if (IsA(prior_expr, FieldStore))
    1199                 :             :         {
    1200                 :             :             /* combine the two */
    1201                 :          84 :             memcpy(fstore, prior_expr, sizeof(FieldStore));
    1202                 :          84 :             fstore->newvals =
    1203                 :          84 :                 list_concat_copy(((FieldStore *) prior_expr)->newvals,
    1204                 :          84 :                                  ((FieldStore *) src_expr)->newvals);
    1205                 :          84 :             fstore->fieldnums =
    1206                 :          84 :                 list_concat_copy(((FieldStore *) prior_expr)->fieldnums,
    1207                 :          84 :                                  ((FieldStore *) src_expr)->fieldnums);
    1208                 :             :         }
    1209                 :             :         else
    1210                 :             :         {
    1211                 :             :             /* general case, just nest 'em */
    1212                 :           0 :             memcpy(fstore, src_expr, sizeof(FieldStore));
    1213                 :           0 :             fstore->arg = (Expr *) prior_expr;
    1214                 :             :         }
    1215                 :          84 :         newexpr = (Node *) fstore;
    1216                 :             :     }
    1217         [ +  - ]:         129 :     else if (IsA(src_expr, SubscriptingRef))
    1218                 :             :     {
    1219                 :         129 :         SubscriptingRef *sbsref = makeNode(SubscriptingRef);
    1220                 :             : 
    1221                 :         129 :         memcpy(sbsref, src_expr, sizeof(SubscriptingRef));
    1222                 :         129 :         sbsref->refexpr = (Expr *) prior_expr;
    1223                 :         129 :         newexpr = (Node *) sbsref;
    1224                 :             :     }
    1225                 :             :     else
    1226                 :             :     {
    1227         [ #  # ]:           0 :         elog(ERROR, "cannot happen");
    1228                 :             :         newexpr = NULL;
    1229                 :             :     }
    1230                 :             : 
    1231         [ +  + ]:         213 :     if (coerce_expr)
    1232                 :             :     {
    1233                 :             :         /* put back the CoerceToDomain */
    1234                 :         108 :         CoerceToDomain *newcoerce = makeNode(CoerceToDomain);
    1235                 :             : 
    1236                 :         108 :         memcpy(newcoerce, coerce_expr, sizeof(CoerceToDomain));
    1237                 :         108 :         newcoerce->arg = (Expr *) newexpr;
    1238                 :         108 :         newexpr = (Node *) newcoerce;
    1239                 :             :     }
    1240                 :             : 
    1241                 :         213 :     result = flatCopyTargetEntry(src_tle);
    1242                 :         213 :     result->expr = (Expr *) newexpr;
    1243                 :         213 :     return result;
    1244                 :             : }
    1245                 :             : 
    1246                 :             : /*
    1247                 :             :  * If node is an assignment node, return its input; else return NULL
    1248                 :             :  */
    1249                 :             : static Node *
    1250                 :         691 : get_assignment_input(Node *node)
    1251                 :             : {
    1252         [ -  + ]:         691 :     if (node == NULL)
    1253                 :           0 :         return NULL;
    1254         [ +  + ]:         691 :     if (IsA(node, FieldStore))
    1255                 :             :     {
    1256                 :         168 :         FieldStore *fstore = (FieldStore *) node;
    1257                 :             : 
    1258                 :         168 :         return (Node *) fstore->arg;
    1259                 :             :     }
    1260         [ +  + ]:         523 :     else if (IsA(node, SubscriptingRef))
    1261                 :             :     {
    1262                 :         286 :         SubscriptingRef *sbsref = (SubscriptingRef *) node;
    1263                 :             : 
    1264         [ -  + ]:         286 :         if (sbsref->refassgnexpr == NULL)
    1265                 :           0 :             return NULL;
    1266                 :             : 
    1267                 :         286 :         return (Node *) sbsref->refexpr;
    1268                 :             :     }
    1269                 :             : 
    1270                 :         237 :     return NULL;
    1271                 :             : }
    1272                 :             : 
    1273                 :             : /*
    1274                 :             :  * Make an expression tree for the default value for a column.
    1275                 :             :  *
    1276                 :             :  * If there is no default, return a NULL instead.
    1277                 :             :  */
    1278                 :             : Node *
    1279                 :       99933 : build_column_default(Relation rel, int attrno)
    1280                 :             : {
    1281                 :       99933 :     TupleDesc   rd_att = rel->rd_att;
    1282                 :       99933 :     Form_pg_attribute att_tup = TupleDescAttr(rd_att, attrno - 1);
    1283                 :       99933 :     Oid         atttype = att_tup->atttypid;
    1284                 :       99933 :     int32       atttypmod = att_tup->atttypmod;
    1285                 :       99933 :     Node       *expr = NULL;
    1286                 :             :     Oid         exprtype;
    1287                 :             : 
    1288         [ +  + ]:       99933 :     if (att_tup->attidentity)
    1289                 :             :     {
    1290                 :         336 :         NextValueExpr *nve = makeNode(NextValueExpr);
    1291                 :             : 
    1292                 :         336 :         nve->seqid = getIdentitySequence(rel, attrno, false);
    1293                 :         336 :         nve->typeId = att_tup->atttypid;
    1294                 :             : 
    1295                 :         336 :         return (Node *) nve;
    1296                 :             :     }
    1297                 :             : 
    1298                 :             :     /*
    1299                 :             :      * If relation has a default for this column, fetch that expression.
    1300                 :             :      */
    1301         [ +  + ]:       99597 :     if (att_tup->atthasdef)
    1302                 :             :     {
    1303                 :       78158 :         expr = TupleDescGetDefault(rd_att, attrno);
    1304         [ -  + ]:       78158 :         if (expr == NULL)
    1305         [ #  # ]:           0 :             elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
    1306                 :             :                  attrno, RelationGetRelationName(rel));
    1307                 :             :     }
    1308                 :             : 
    1309                 :             :     /*
    1310                 :             :      * No per-column default, so look for a default for the type itself.  But
    1311                 :             :      * not for generated columns.
    1312                 :             :      */
    1313   [ +  +  +  - ]:       99597 :     if (expr == NULL && !att_tup->attgenerated)
    1314                 :       21439 :         expr = get_typdefault(atttype);
    1315                 :             : 
    1316         [ +  + ]:       99597 :     if (expr == NULL)
    1317                 :       21286 :         return NULL;            /* No default anywhere */
    1318                 :             : 
    1319                 :             :     /*
    1320                 :             :      * Make sure the value is coerced to the target column type; this will
    1321                 :             :      * generally be true already, but there seem to be some corner cases
    1322                 :             :      * involving domain defaults where it might not be true. This should match
    1323                 :             :      * the parser's processing of non-defaulted expressions --- see
    1324                 :             :      * transformAssignedExpr().
    1325                 :             :      */
    1326                 :       78311 :     exprtype = exprType(expr);
    1327                 :             : 
    1328                 :       78311 :     expr = coerce_to_target_type(NULL,  /* no UNKNOWN params here */
    1329                 :             :                                  expr, exprtype,
    1330                 :             :                                  atttype, atttypmod,
    1331                 :             :                                  COERCION_ASSIGNMENT,
    1332                 :             :                                  COERCE_IMPLICIT_CAST,
    1333                 :             :                                  -1);
    1334         [ -  + ]:       78311 :     if (expr == NULL)
    1335         [ #  # ]:           0 :         ereport(ERROR,
    1336                 :             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    1337                 :             :                  errmsg("column \"%s\" is of type %s"
    1338                 :             :                         " but default expression is of type %s",
    1339                 :             :                         NameStr(att_tup->attname),
    1340                 :             :                         format_type_be(atttype),
    1341                 :             :                         format_type_be(exprtype)),
    1342                 :             :                  errhint("You will need to rewrite or cast the expression.")));
    1343                 :             : 
    1344                 :       78311 :     return expr;
    1345                 :             : }
    1346                 :             : 
    1347                 :             : 
    1348                 :             : /* Does VALUES RTE contain any SetToDefault items? */
    1349                 :             : static bool
    1350                 :        3399 : searchForDefault(RangeTblEntry *rte)
    1351                 :             : {
    1352                 :             :     ListCell   *lc;
    1353                 :             : 
    1354   [ +  -  +  +  :       14526 :     foreach(lc, rte->values_lists)
                   +  + ]
    1355                 :             :     {
    1356                 :       11334 :         List       *sublist = (List *) lfirst(lc);
    1357                 :             :         ListCell   *lc2;
    1358                 :             : 
    1359   [ +  -  +  +  :       35576 :         foreach(lc2, sublist)
                   +  + ]
    1360                 :             :         {
    1361                 :       24449 :             Node       *col = (Node *) lfirst(lc2);
    1362                 :             : 
    1363         [ +  + ]:       24449 :             if (IsA(col, SetToDefault))
    1364                 :         207 :                 return true;
    1365                 :             :         }
    1366                 :             :     }
    1367                 :        3192 :     return false;
    1368                 :             : }
    1369                 :             : 
    1370                 :             : 
    1371                 :             : /*
    1372                 :             :  * Search a VALUES RTE for columns that contain only SetToDefault items,
    1373                 :             :  * returning a Bitmapset containing the attribute numbers of any such columns.
    1374                 :             :  */
    1375                 :             : static Bitmapset *
    1376                 :          99 : findDefaultOnlyColumns(RangeTblEntry *rte)
    1377                 :             : {
    1378                 :          99 :     Bitmapset  *default_only_cols = NULL;
    1379                 :             :     ListCell   *lc;
    1380                 :             : 
    1381   [ +  -  +  +  :         189 :     foreach(lc, rte->values_lists)
                   +  + ]
    1382                 :             :     {
    1383                 :         158 :         List       *sublist = (List *) lfirst(lc);
    1384                 :             :         ListCell   *lc2;
    1385                 :             :         int         i;
    1386                 :             : 
    1387         [ +  + ]:         158 :         if (default_only_cols == NULL)
    1388                 :             :         {
    1389                 :             :             /* Populate the initial result bitmap from the first row */
    1390                 :          99 :             i = 0;
    1391   [ +  -  +  +  :         305 :             foreach(lc2, sublist)
                   +  + ]
    1392                 :             :             {
    1393                 :         206 :                 Node       *col = (Node *) lfirst(lc2);
    1394                 :             : 
    1395                 :         206 :                 i++;
    1396         [ +  + ]:         206 :                 if (IsA(col, SetToDefault))
    1397                 :          52 :                     default_only_cols = bms_add_member(default_only_cols, i);
    1398                 :             :             }
    1399                 :             :         }
    1400                 :             :         else
    1401                 :             :         {
    1402                 :             :             /* Update the result bitmap from this next row */
    1403                 :          59 :             i = 0;
    1404   [ +  -  +  +  :         193 :             foreach(lc2, sublist)
                   +  + ]
    1405                 :             :             {
    1406                 :         134 :                 Node       *col = (Node *) lfirst(lc2);
    1407                 :             : 
    1408                 :         134 :                 i++;
    1409         [ +  + ]:         134 :                 if (!IsA(col, SetToDefault))
    1410                 :          94 :                     default_only_cols = bms_del_member(default_only_cols, i);
    1411                 :             :             }
    1412                 :             :         }
    1413                 :             : 
    1414                 :             :         /*
    1415                 :             :          * If no column in the rows read so far contains only DEFAULT items,
    1416                 :             :          * we are done.
    1417                 :             :          */
    1418         [ +  + ]:         158 :         if (bms_is_empty(default_only_cols))
    1419                 :          68 :             break;
    1420                 :             :     }
    1421                 :             : 
    1422                 :          99 :     return default_only_cols;
    1423                 :             : }
    1424                 :             : 
    1425                 :             : 
    1426                 :             : /*
    1427                 :             :  * When processing INSERT ... VALUES with a VALUES RTE (ie, multiple VALUES
    1428                 :             :  * lists), we have to replace any DEFAULT items in the VALUES lists with
    1429                 :             :  * the appropriate default expressions.  The other aspects of targetlist
    1430                 :             :  * rewriting need be applied only to the query's targetlist proper.
    1431                 :             :  *
    1432                 :             :  * For an auto-updatable view, each DEFAULT item in the VALUES list is
    1433                 :             :  * replaced with the default from the view, if it has one.  Otherwise it is
    1434                 :             :  * left untouched so that the underlying base relation's default can be
    1435                 :             :  * applied instead (when we later recurse to here after rewriting the query
    1436                 :             :  * to refer to the base relation instead of the view).
    1437                 :             :  *
    1438                 :             :  * For other types of relation, including rule- and trigger-updatable views,
    1439                 :             :  * all DEFAULT items are replaced, and if the target relation doesn't have a
    1440                 :             :  * default, the value is explicitly set to NULL.
    1441                 :             :  *
    1442                 :             :  * Also, if a DEFAULT item is found in a column mentioned in unused_cols,
    1443                 :             :  * it is explicitly set to NULL.  This happens for columns in the VALUES RTE
    1444                 :             :  * whose corresponding targetlist entries have already been replaced with the
    1445                 :             :  * relation's default expressions, so that any values in those columns of the
    1446                 :             :  * VALUES RTE are no longer used.  This can happen for identity and generated
    1447                 :             :  * columns (if INSERT ... OVERRIDING USER VALUE is used, or all the values to
    1448                 :             :  * be inserted are DEFAULT).  In principle we could replace all entries in
    1449                 :             :  * such a column with NULL, whether DEFAULT or not; but it doesn't seem worth
    1450                 :             :  * the trouble.
    1451                 :             :  *
    1452                 :             :  * Note that we may have subscripted or field assignment targetlist entries,
    1453                 :             :  * as well as more complex expressions from already-replaced DEFAULT items if
    1454                 :             :  * we have recursed to here for an auto-updatable view. However, it ought to
    1455                 :             :  * be impossible for such entries to have DEFAULTs assigned to them, except
    1456                 :             :  * for unused columns, as described above --- we should only have to replace
    1457                 :             :  * DEFAULT items for targetlist entries that contain simple Vars referencing
    1458                 :             :  * the VALUES RTE, or which are no longer referred to by the targetlist.
    1459                 :             :  *
    1460                 :             :  * Returns true if all DEFAULT items were replaced, and false if some were
    1461                 :             :  * left untouched.
    1462                 :             :  */
    1463                 :             : static bool
    1464                 :        3399 : rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
    1465                 :             :                  Relation target_relation,
    1466                 :             :                  Bitmapset *unused_cols)
    1467                 :             : {
    1468                 :             :     List       *newValues;
    1469                 :             :     ListCell   *lc;
    1470                 :             :     bool        isAutoUpdatableView;
    1471                 :             :     bool        allReplaced;
    1472                 :             :     int         numattrs;
    1473                 :             :     int        *attrnos;
    1474                 :             : 
    1475                 :             :     /* Steps below are not sensible for non-INSERT queries */
    1476                 :             :     Assert(parsetree->commandType == CMD_INSERT);
    1477                 :             :     Assert(rte->rtekind == RTE_VALUES);
    1478                 :             : 
    1479                 :             :     /*
    1480                 :             :      * Rebuilding all the lists is a pretty expensive proposition in a big
    1481                 :             :      * VALUES list, and it's a waste of time if there aren't any DEFAULT
    1482                 :             :      * placeholders.  So first scan to see if there are any.
    1483                 :             :      */
    1484         [ +  + ]:        3399 :     if (!searchForDefault(rte))
    1485                 :        3192 :         return true;            /* nothing to do */
    1486                 :             : 
    1487                 :             :     /*
    1488                 :             :      * Scan the targetlist for entries referring to the VALUES RTE, and note
    1489                 :             :      * the target attributes. As noted above, we should only need to do this
    1490                 :             :      * for targetlist entries containing simple Vars --- nothing else in the
    1491                 :             :      * VALUES RTE should contain DEFAULT items (except possibly for unused
    1492                 :             :      * columns), and we complain if such a thing does occur.
    1493                 :             :      */
    1494                 :         207 :     numattrs = list_length(linitial(rte->values_lists));
    1495                 :         207 :     attrnos = (int *) palloc0(numattrs * sizeof(int));
    1496                 :             : 
    1497   [ +  -  +  +  :         832 :     foreach(lc, parsetree->targetList)
                   +  + ]
    1498                 :             :     {
    1499                 :         625 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
    1500                 :             : 
    1501         [ +  + ]:         625 :         if (IsA(tle->expr, Var))
    1502                 :             :         {
    1503                 :         535 :             Var        *var = (Var *) tle->expr;
    1504                 :             : 
    1505         [ +  - ]:         535 :             if (var->varno == rti)
    1506                 :             :             {
    1507                 :         535 :                 int         attrno = var->varattno;
    1508                 :             : 
    1509                 :             :                 Assert(attrno >= 1 && attrno <= numattrs);
    1510                 :         535 :                 attrnos[attrno - 1] = tle->resno;
    1511                 :             :             }
    1512                 :             :         }
    1513                 :             :     }
    1514                 :             : 
    1515                 :             :     /*
    1516                 :             :      * Check if the target relation is an auto-updatable view, in which case
    1517                 :             :      * unresolved defaults will be left untouched rather than being set to
    1518                 :             :      * NULL.
    1519                 :             :      */
    1520                 :         207 :     isAutoUpdatableView = false;
    1521         [ +  + ]:         207 :     if (target_relation->rd_rel->relkind == RELKIND_VIEW &&
    1522         [ +  + ]:          76 :         !view_has_instead_trigger(target_relation, CMD_INSERT, NIL))
    1523                 :             :     {
    1524                 :             :         List       *locks;
    1525                 :             :         bool        hasUpdate;
    1526                 :             :         bool        found;
    1527                 :             :         ListCell   *l;
    1528                 :             : 
    1529                 :             :         /* Look for an unconditional DO INSTEAD rule */
    1530                 :          68 :         locks = matchLocks(CMD_INSERT, target_relation,
    1531                 :             :                            parsetree->resultRelation, parsetree, &hasUpdate);
    1532                 :             : 
    1533                 :          68 :         found = false;
    1534   [ +  +  +  +  :          84 :         foreach(l, locks)
                   +  + ]
    1535                 :             :         {
    1536                 :          24 :             RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
    1537                 :             : 
    1538         [ +  + ]:          24 :             if (rule_lock->isInstead &&
    1539         [ +  - ]:           8 :                 rule_lock->qual == NULL)
    1540                 :             :             {
    1541                 :           8 :                 found = true;
    1542                 :           8 :                 break;
    1543                 :             :             }
    1544                 :             :         }
    1545                 :             : 
    1546                 :             :         /*
    1547                 :             :          * If we didn't find an unconditional DO INSTEAD rule, assume that the
    1548                 :             :          * view is auto-updatable.  If it isn't, rewriteTargetView() will
    1549                 :             :          * throw an error.
    1550                 :             :          */
    1551         [ +  + ]:          68 :         if (!found)
    1552                 :          60 :             isAutoUpdatableView = true;
    1553                 :             :     }
    1554                 :             : 
    1555                 :         207 :     newValues = NIL;
    1556                 :         207 :     allReplaced = true;
    1557   [ +  -  +  +  :         637 :     foreach(lc, rte->values_lists)
                   +  + ]
    1558                 :             :     {
    1559                 :         430 :         List       *sublist = (List *) lfirst(lc);
    1560                 :         430 :         List       *newList = NIL;
    1561                 :             :         ListCell   *lc2;
    1562                 :             :         int         i;
    1563                 :             : 
    1564                 :             :         Assert(list_length(sublist) == numattrs);
    1565                 :             : 
    1566                 :         430 :         i = 0;
    1567   [ +  -  +  +  :        1710 :         foreach(lc2, sublist)
                   +  + ]
    1568                 :             :         {
    1569                 :        1280 :             Node       *col = (Node *) lfirst(lc2);
    1570                 :        1280 :             int         attrno = attrnos[i++];
    1571                 :             : 
    1572         [ +  + ]:        1280 :             if (IsA(col, SetToDefault))
    1573                 :             :             {
    1574                 :             :                 Form_pg_attribute att_tup;
    1575                 :             :                 Node       *new_expr;
    1576                 :             : 
    1577                 :             :                 /*
    1578                 :             :                  * If this column isn't used, just replace the DEFAULT with
    1579                 :             :                  * NULL (attrno will be 0 in this case because the targetlist
    1580                 :             :                  * entry will have been replaced by the default expression).
    1581                 :             :                  */
    1582         [ +  + ]:         592 :                 if (bms_is_member(i, unused_cols))
    1583                 :          86 :                 {
    1584                 :          86 :                     SetToDefault *def = (SetToDefault *) col;
    1585                 :             : 
    1586                 :          86 :                     newList = lappend(newList,
    1587                 :          86 :                                       makeNullConst(def->typeId,
    1588                 :             :                                                     def->typeMod,
    1589                 :             :                                                     def->collation));
    1590                 :          86 :                     continue;
    1591                 :             :                 }
    1592                 :             : 
    1593         [ -  + ]:         506 :                 if (attrno == 0)
    1594         [ #  # ]:           0 :                     elog(ERROR, "cannot set value in column %d to DEFAULT", i);
    1595                 :             :                 Assert(attrno > 0 && attrno <= target_relation->rd_att->natts);
    1596                 :         506 :                 att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
    1597                 :             : 
    1598         [ +  - ]:         506 :                 if (!att_tup->attisdropped)
    1599                 :         506 :                     new_expr = build_column_default(target_relation, attrno);
    1600                 :             :                 else
    1601                 :           0 :                     new_expr = NULL;    /* force a NULL if dropped */
    1602                 :             : 
    1603                 :             :                 /*
    1604                 :             :                  * If there is no default (ie, default is effectively NULL),
    1605                 :             :                  * we've got to explicitly set the column to NULL, unless the
    1606                 :             :                  * target relation is an auto-updatable view.
    1607                 :             :                  */
    1608         [ +  + ]:         506 :                 if (!new_expr)
    1609                 :             :                 {
    1610         [ +  + ]:         238 :                     if (isAutoUpdatableView)
    1611                 :             :                     {
    1612                 :             :                         /* Leave the value untouched */
    1613                 :         100 :                         newList = lappend(newList, col);
    1614                 :         100 :                         allReplaced = false;
    1615                 :         100 :                         continue;
    1616                 :             :                     }
    1617                 :             : 
    1618                 :         138 :                     new_expr = coerce_null_to_domain(att_tup->atttypid,
    1619                 :             :                                                      att_tup->atttypmod,
    1620                 :             :                                                      att_tup->attcollation,
    1621                 :         138 :                                                      att_tup->attlen,
    1622                 :         138 :                                                      att_tup->attbyval);
    1623                 :             :                 }
    1624                 :         406 :                 newList = lappend(newList, new_expr);
    1625                 :             :             }
    1626                 :             :             else
    1627                 :         688 :                 newList = lappend(newList, col);
    1628                 :             :         }
    1629                 :         430 :         newValues = lappend(newValues, newList);
    1630                 :             :     }
    1631                 :         207 :     rte->values_lists = newValues;
    1632                 :             : 
    1633                 :         207 :     pfree(attrnos);
    1634                 :             : 
    1635                 :         207 :     return allReplaced;
    1636                 :             : }
    1637                 :             : 
    1638                 :             : /*
    1639                 :             :  * Mop up any remaining DEFAULT items in the given VALUES RTE by
    1640                 :             :  * replacing them with NULL constants.
    1641                 :             :  *
    1642                 :             :  * This is used for the product queries generated by DO ALSO rules attached to
    1643                 :             :  * an auto-updatable view.  The action can't depend on the "target relation"
    1644                 :             :  * since the product query might not have one (it needn't be an INSERT).
    1645                 :             :  * Essentially, such queries are treated as being attached to a rule-updatable
    1646                 :             :  * view.
    1647                 :             :  */
    1648                 :             : static void
    1649                 :          16 : rewriteValuesRTEToNulls(Query *parsetree, RangeTblEntry *rte)
    1650                 :             : {
    1651                 :             :     List       *newValues;
    1652                 :             :     ListCell   *lc;
    1653                 :             : 
    1654                 :          16 :     newValues = NIL;
    1655   [ +  -  +  +  :          48 :     foreach(lc, rte->values_lists)
                   +  + ]
    1656                 :             :     {
    1657                 :          32 :         List       *sublist = (List *) lfirst(lc);
    1658                 :          32 :         List       *newList = NIL;
    1659                 :             :         ListCell   *lc2;
    1660                 :             : 
    1661   [ +  -  +  +  :         136 :         foreach(lc2, sublist)
                   +  + ]
    1662                 :             :         {
    1663                 :         104 :             Node       *col = (Node *) lfirst(lc2);
    1664                 :             : 
    1665         [ +  + ]:         104 :             if (IsA(col, SetToDefault))
    1666                 :             :             {
    1667                 :          44 :                 SetToDefault *def = (SetToDefault *) col;
    1668                 :             : 
    1669                 :          44 :                 newList = lappend(newList, makeNullConst(def->typeId,
    1670                 :             :                                                          def->typeMod,
    1671                 :             :                                                          def->collation));
    1672                 :             :             }
    1673                 :             :             else
    1674                 :          60 :                 newList = lappend(newList, col);
    1675                 :             :         }
    1676                 :          32 :         newValues = lappend(newValues, newList);
    1677                 :             :     }
    1678                 :          16 :     rte->values_lists = newValues;
    1679                 :          16 : }
    1680                 :             : 
    1681                 :             : 
    1682                 :             : /*
    1683                 :             :  * matchLocks -
    1684                 :             :  *    match a relation's list of locks and returns the matching rules
    1685                 :             :  */
    1686                 :             : static List *
    1687                 :       60836 : matchLocks(CmdType event,
    1688                 :             :            Relation relation,
    1689                 :             :            int varno,
    1690                 :             :            Query *parsetree,
    1691                 :             :            bool *hasUpdate)
    1692                 :             : {
    1693                 :       60836 :     RuleLock   *rulelocks = relation->rd_rules;
    1694                 :       60836 :     List       *matching_locks = NIL;
    1695                 :             :     int         nlocks;
    1696                 :             :     int         i;
    1697                 :             : 
    1698         [ +  + ]:       60836 :     if (rulelocks == NULL)
    1699                 :       56875 :         return NIL;
    1700                 :             : 
    1701         [ +  - ]:        3961 :     if (parsetree->commandType != CMD_SELECT)
    1702                 :             :     {
    1703         [ -  + ]:        3961 :         if (parsetree->resultRelation != varno)
    1704                 :           0 :             return NIL;
    1705                 :             :     }
    1706                 :             : 
    1707                 :        3961 :     nlocks = rulelocks->numLocks;
    1708                 :             : 
    1709         [ +  + ]:        9082 :     for (i = 0; i < nlocks; i++)
    1710                 :             :     {
    1711                 :        5133 :         RewriteRule *oneLock = rulelocks->rules[i];
    1712                 :             : 
    1713         [ +  + ]:        5133 :         if (oneLock->event == CMD_UPDATE)
    1714                 :         488 :             *hasUpdate = true;
    1715                 :             : 
    1716                 :             :         /*
    1717                 :             :          * Suppress ON INSERT/UPDATE/DELETE rules that are disabled or
    1718                 :             :          * configured to not fire during the current session's replication
    1719                 :             :          * role. ON SELECT rules will always be applied in order to keep views
    1720                 :             :          * working even in LOCAL or REPLICA role.
    1721                 :             :          */
    1722         [ +  + ]:        5133 :         if (oneLock->event != CMD_SELECT)
    1723                 :             :         {
    1724         [ +  + ]:        1996 :             if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA)
    1725                 :             :             {
    1726         [ +  + ]:           8 :                 if (oneLock->enabled == RULE_FIRES_ON_ORIGIN ||
    1727         [ -  + ]:           4 :                     oneLock->enabled == RULE_DISABLED)
    1728                 :           4 :                     continue;
    1729                 :             :             }
    1730                 :             :             else                /* ORIGIN or LOCAL ROLE */
    1731                 :             :             {
    1732         [ +  + ]:        1988 :                 if (oneLock->enabled == RULE_FIRES_ON_REPLICA ||
    1733         [ +  + ]:        1984 :                     oneLock->enabled == RULE_DISABLED)
    1734                 :          20 :                     continue;
    1735                 :             :             }
    1736                 :             : 
    1737                 :             :             /* Non-SELECT rules are not supported for MERGE */
    1738         [ +  + ]:        1972 :             if (parsetree->commandType == CMD_MERGE)
    1739         [ +  - ]:          12 :                 ereport(ERROR,
    1740                 :             :                         errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1741                 :             :                         errmsg("cannot execute MERGE on relation \"%s\"",
    1742                 :             :                                RelationGetRelationName(relation)),
    1743                 :             :                         errdetail("MERGE is not supported for relations with rules."));
    1744                 :             :         }
    1745                 :             : 
    1746         [ +  + ]:        5097 :         if (oneLock->event == event)
    1747                 :             :         {
    1748   [ -  +  -  - ]:        1128 :             if (parsetree->commandType != CMD_SELECT ||
    1749                 :           0 :                 rangeTableEntry_used((Node *) parsetree, varno, 0))
    1750                 :        1128 :                 matching_locks = lappend(matching_locks, oneLock);
    1751                 :             :         }
    1752                 :             :     }
    1753                 :             : 
    1754                 :        3949 :     return matching_locks;
    1755                 :             : }
    1756                 :             : 
    1757                 :             : 
    1758                 :             : /*
    1759                 :             :  * ApplyRetrieveRule - expand an ON SELECT rule
    1760                 :             :  */
    1761                 :             : static Query *
    1762                 :       10481 : ApplyRetrieveRule(Query *parsetree,
    1763                 :             :                   RewriteRule *rule,
    1764                 :             :                   int rt_index,
    1765                 :             :                   Relation relation,
    1766                 :             :                   List *activeRIRs)
    1767                 :             : {
    1768                 :             :     Query      *rule_action;
    1769                 :             :     RangeTblEntry *rte;
    1770                 :             :     RowMarkClause *rc;
    1771                 :             :     int         numCols;
    1772                 :             : 
    1773         [ -  + ]:       10481 :     if (list_length(rule->actions) != 1)
    1774         [ #  # ]:           0 :         elog(ERROR, "expected just one rule action");
    1775         [ -  + ]:       10481 :     if (rule->qual != NULL)
    1776         [ #  # ]:           0 :         elog(ERROR, "cannot handle qualified ON SELECT rule");
    1777                 :             : 
    1778                 :             :     /* Check if the expansion of non-system views are restricted */
    1779   [ +  +  +  +  :       10481 :     if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_VIEW) != 0 &&
                   +  + ]
    1780                 :             :                  RelationGetRelid(relation) >= FirstNormalObjectId))
    1781         [ +  - ]:           4 :         ereport(ERROR,
    1782                 :             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1783                 :             :                  errmsg("access to non-system view \"%s\" is restricted",
    1784                 :             :                         RelationGetRelationName(relation))));
    1785                 :             : 
    1786         [ +  + ]:       10477 :     if (rt_index == parsetree->resultRelation)
    1787                 :             :     {
    1788                 :             :         /*
    1789                 :             :          * We have a view as the result relation of the query, and it wasn't
    1790                 :             :          * rewritten by any rule.  This case is supported if there is an
    1791                 :             :          * INSTEAD OF trigger that will trap attempts to insert/update/delete
    1792                 :             :          * view rows.  The executor will check that; for the moment just plow
    1793                 :             :          * ahead.  We have two cases:
    1794                 :             :          *
    1795                 :             :          * For INSERT, we needn't do anything.  The unmodified RTE will serve
    1796                 :             :          * fine as the result relation.
    1797                 :             :          *
    1798                 :             :          * For UPDATE/DELETE/MERGE, we need to expand the view so as to have
    1799                 :             :          * source data for the operation.  But we also need an unmodified RTE
    1800                 :             :          * to serve as the target.  So, copy the RTE and add the copy to the
    1801                 :             :          * rangetable.  Note that the copy does not get added to the jointree.
    1802                 :             :          * Also note that there's a hack in fireRIRrules to avoid calling this
    1803                 :             :          * function again when it arrives at the copied RTE.
    1804                 :             :          */
    1805         [ +  + ]:         269 :         if (parsetree->commandType == CMD_INSERT)
    1806                 :          79 :             return parsetree;
    1807         [ +  + ]:         190 :         else if (parsetree->commandType == CMD_UPDATE ||
    1808         [ +  + ]:          87 :                  parsetree->commandType == CMD_DELETE ||
    1809         [ +  - ]:          52 :                  parsetree->commandType == CMD_MERGE)
    1810                 :         190 :         {
    1811                 :             :             RangeTblEntry *newrte;
    1812                 :             :             Var        *var;
    1813                 :             :             TargetEntry *tle;
    1814                 :             : 
    1815                 :         190 :             rte = rt_fetch(rt_index, parsetree->rtable);
    1816                 :         190 :             newrte = copyObject(rte);
    1817                 :         190 :             parsetree->rtable = lappend(parsetree->rtable, newrte);
    1818                 :         190 :             parsetree->resultRelation = list_length(parsetree->rtable);
    1819                 :             :             /* parsetree->mergeTargetRelation unchanged (use expanded view) */
    1820                 :             : 
    1821                 :             :             /*
    1822                 :             :              * For the most part, Vars referencing the view should remain as
    1823                 :             :              * they are, meaning that they implicitly represent OLD values.
    1824                 :             :              * But in the RETURNING list if any, we want such Vars to
    1825                 :             :              * represent NEW values, so change them to reference the new RTE.
    1826                 :             :              *
    1827                 :             :              * Since ChangeVarNodes scribbles on the tree in-place, copy the
    1828                 :             :              * RETURNING list first for safety.
    1829                 :             :              */
    1830                 :         190 :             parsetree->returningList = copyObject(parsetree->returningList);
    1831                 :         190 :             ChangeVarNodes((Node *) parsetree->returningList, rt_index,
    1832                 :             :                            parsetree->resultRelation, 0);
    1833                 :             : 
    1834                 :             :             /*
    1835                 :             :              * To allow the executor to compute the original view row to pass
    1836                 :             :              * to the INSTEAD OF trigger, we add a resjunk whole-row Var
    1837                 :             :              * referencing the original RTE.  This will later get expanded
    1838                 :             :              * into a RowExpr computing all the OLD values of the view row.
    1839                 :             :              */
    1840                 :         190 :             var = makeWholeRowVar(rte, rt_index, 0, false);
    1841                 :         190 :             tle = makeTargetEntry((Expr *) var,
    1842                 :         190 :                                   list_length(parsetree->targetList) + 1,
    1843                 :             :                                   pstrdup("wholerow"),
    1844                 :             :                                   true);
    1845                 :             : 
    1846                 :         190 :             parsetree->targetList = lappend(parsetree->targetList, tle);
    1847                 :             : 
    1848                 :             :             /* Now, continue with expanding the original view RTE */
    1849                 :             :         }
    1850                 :             :         else
    1851         [ #  # ]:           0 :             elog(ERROR, "unrecognized commandType: %d",
    1852                 :             :                  (int) parsetree->commandType);
    1853                 :             :     }
    1854                 :             : 
    1855                 :             :     /*
    1856                 :             :      * Check if there's a FOR [KEY] UPDATE/SHARE clause applying to this view.
    1857                 :             :      *
    1858                 :             :      * Note: we needn't explicitly consider any such clauses appearing in
    1859                 :             :      * ancestor query levels; their effects have already been pushed down to
    1860                 :             :      * here by markQueryForLocking, and will be reflected in "rc".
    1861                 :             :      */
    1862                 :       10398 :     rc = get_parse_rowmark(parsetree, rt_index);
    1863                 :             : 
    1864                 :             :     /*
    1865                 :             :      * Make a modifiable copy of the view query, and acquire needed locks on
    1866                 :             :      * the relations it mentions.  Force at least RowShareLock for all such
    1867                 :             :      * rels if there's a FOR [KEY] UPDATE/SHARE clause affecting this view.
    1868                 :             :      */
    1869                 :       10398 :     rule_action = copyObject(linitial(rule->actions));
    1870                 :             : 
    1871                 :       10398 :     AcquireRewriteLocks(rule_action, true, (rc != NULL));
    1872                 :             : 
    1873                 :             :     /*
    1874                 :             :      * If FOR [KEY] UPDATE/SHARE of view, mark all the contained tables as
    1875                 :             :      * implicit FOR [KEY] UPDATE/SHARE, the same as the parser would have done
    1876                 :             :      * if the view's subquery had been written out explicitly.
    1877                 :             :      */
    1878         [ +  + ]:       10398 :     if (rc != NULL)
    1879                 :          64 :         markQueryForLocking(rule_action, (Node *) rule_action->jointree,
    1880                 :             :                             rc->strength, rc->waitPolicy, true);
    1881                 :             : 
    1882                 :             :     /*
    1883                 :             :      * Recursively expand any view references inside the view.
    1884                 :             :      */
    1885                 :       10398 :     rule_action = fireRIRrules(rule_action, activeRIRs);
    1886                 :             : 
    1887                 :             :     /*
    1888                 :             :      * Make sure the query is marked as having row security if the view query
    1889                 :             :      * does.
    1890                 :             :      */
    1891                 :       10378 :     parsetree->hasRowSecurity |= rule_action->hasRowSecurity;
    1892                 :             : 
    1893                 :             :     /*
    1894                 :             :      * Now, plug the view query in as a subselect, converting the relation's
    1895                 :             :      * original RTE to a subquery RTE.
    1896                 :             :      */
    1897                 :       10378 :     rte = rt_fetch(rt_index, parsetree->rtable);
    1898                 :             : 
    1899                 :       10378 :     rte->rtekind = RTE_SUBQUERY;
    1900                 :       10378 :     rte->subquery = rule_action;
    1901         [ +  + ]:       10378 :     rte->security_barrier = RelationIsSecurityView(relation);
    1902                 :             : 
    1903                 :             :     /*
    1904                 :             :      * Clear fields that should not be set in a subquery RTE.  Note that we
    1905                 :             :      * leave the relid, relkind, rellockmode, and perminfoindex fields set, so
    1906                 :             :      * that the view relation can be appropriately locked before execution and
    1907                 :             :      * its permissions checked.
    1908                 :             :      */
    1909                 :       10378 :     rte->tablesample = NULL;
    1910                 :       10378 :     rte->inh = false;            /* must not be set for a subquery */
    1911                 :             : 
    1912                 :             :     /*
    1913                 :             :      * Since we allow CREATE OR REPLACE VIEW to add columns to a view, the
    1914                 :             :      * rule_action might emit more columns than we expected when the current
    1915                 :             :      * query was parsed.  Various places expect rte->eref->colnames to be
    1916                 :             :      * consistent with the non-junk output columns of the subquery, so patch
    1917                 :             :      * things up if necessary by adding some dummy column names.
    1918                 :             :      */
    1919                 :       10378 :     numCols = ExecCleanTargetListLength(rule_action->targetList);
    1920         [ +  + ]:       10390 :     while (list_length(rte->eref->colnames) < numCols)
    1921                 :             :     {
    1922                 :          12 :         rte->eref->colnames = lappend(rte->eref->colnames,
    1923                 :          12 :                                       makeString(pstrdup("?column?")));
    1924                 :             :     }
    1925                 :             : 
    1926                 :       10378 :     return parsetree;
    1927                 :             : }
    1928                 :             : 
    1929                 :             : /*
    1930                 :             :  * Recursively mark all relations used by a view as FOR [KEY] UPDATE/SHARE.
    1931                 :             :  *
    1932                 :             :  * This may generate an invalid query, eg if some sub-query uses an
    1933                 :             :  * aggregate.  We leave it to the planner to detect that.
    1934                 :             :  *
    1935                 :             :  * NB: this must agree with the parser's transformLockingClause() routine.
    1936                 :             :  * However, we used to have to avoid marking a view's OLD and NEW rels for
    1937                 :             :  * updating, which motivated scanning the jointree to determine which rels
    1938                 :             :  * are used.  Possibly that could now be simplified into just scanning the
    1939                 :             :  * rangetable as the parser does.
    1940                 :             :  */
    1941                 :             : static void
    1942                 :         128 : markQueryForLocking(Query *qry, Node *jtnode,
    1943                 :             :                     LockClauseStrength strength, LockWaitPolicy waitPolicy,
    1944                 :             :                     bool pushedDown)
    1945                 :             : {
    1946         [ -  + ]:         128 :     if (jtnode == NULL)
    1947                 :           0 :         return;
    1948         [ +  + ]:         128 :     if (IsA(jtnode, RangeTblRef))
    1949                 :             :     {
    1950                 :          64 :         int         rti = ((RangeTblRef *) jtnode)->rtindex;
    1951                 :          64 :         RangeTblEntry *rte = rt_fetch(rti, qry->rtable);
    1952                 :             : 
    1953         [ +  - ]:          64 :         if (rte->rtekind == RTE_RELATION)
    1954                 :             :         {
    1955                 :             :             RTEPermissionInfo *perminfo;
    1956                 :             : 
    1957                 :          64 :             applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
    1958                 :             : 
    1959                 :          64 :             perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
    1960                 :          64 :             perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
    1961                 :             :         }
    1962         [ #  # ]:           0 :         else if (rte->rtekind == RTE_SUBQUERY)
    1963                 :             :         {
    1964                 :           0 :             applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
    1965                 :             :             /* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
    1966                 :           0 :             markQueryForLocking(rte->subquery, (Node *) rte->subquery->jointree,
    1967                 :             :                                 strength, waitPolicy, true);
    1968                 :             :         }
    1969                 :             :         /* other RTE types are unaffected by FOR UPDATE */
    1970                 :             :     }
    1971         [ +  - ]:          64 :     else if (IsA(jtnode, FromExpr))
    1972                 :             :     {
    1973                 :          64 :         FromExpr   *f = (FromExpr *) jtnode;
    1974                 :             :         ListCell   *l;
    1975                 :             : 
    1976   [ +  -  +  +  :         128 :         foreach(l, f->fromlist)
                   +  + ]
    1977                 :          64 :             markQueryForLocking(qry, lfirst(l), strength, waitPolicy, pushedDown);
    1978                 :             :     }
    1979         [ #  # ]:           0 :     else if (IsA(jtnode, JoinExpr))
    1980                 :             :     {
    1981                 :           0 :         JoinExpr   *j = (JoinExpr *) jtnode;
    1982                 :             : 
    1983                 :           0 :         markQueryForLocking(qry, j->larg, strength, waitPolicy, pushedDown);
    1984                 :           0 :         markQueryForLocking(qry, j->rarg, strength, waitPolicy, pushedDown);
    1985                 :             :     }
    1986                 :             :     else
    1987         [ #  # ]:           0 :         elog(ERROR, "unrecognized node type: %d",
    1988                 :             :              (int) nodeTag(jtnode));
    1989                 :             : }
    1990                 :             : 
    1991                 :             : 
    1992                 :             : /*
    1993                 :             :  * fireRIRonSubLink -
    1994                 :             :  *  Apply fireRIRrules() to each SubLink (subselect in expression) found
    1995                 :             :  *  in the given tree.
    1996                 :             :  *
    1997                 :             :  * NOTE: although this has the form of a walker, we cheat and modify the
    1998                 :             :  * SubLink nodes in-place.  It is caller's responsibility to ensure that
    1999                 :             :  * no unwanted side-effects occur!
    2000                 :             :  *
    2001                 :             :  * This is unlike most of the other routines that recurse into subselects,
    2002                 :             :  * because we must take control at the SubLink node in order to replace
    2003                 :             :  * the SubLink's subselect link with the possibly-rewritten subquery.
    2004                 :             :  */
    2005                 :             : static bool
    2006                 :     1841981 : fireRIRonSubLink(Node *node, fireRIRonSubLink_context *context)
    2007                 :             : {
    2008         [ +  + ]:     1841981 :     if (node == NULL)
    2009                 :      399783 :         return false;
    2010         [ +  + ]:     1442198 :     if (IsA(node, SubLink))
    2011                 :             :     {
    2012                 :       34575 :         SubLink    *sub = (SubLink *) node;
    2013                 :             : 
    2014                 :             :         /* Do what we came for */
    2015                 :       34575 :         sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
    2016                 :             :                                                context->activeRIRs);
    2017                 :             : 
    2018                 :             :         /*
    2019                 :             :          * Remember if any of the sublinks have row security.
    2020                 :             :          */
    2021                 :       34523 :         context->hasRowSecurity |= ((Query *) sub->subselect)->hasRowSecurity;
    2022                 :             : 
    2023                 :             :         /* Fall through to process lefthand args of SubLink */
    2024                 :             :     }
    2025                 :             : 
    2026                 :             :     /*
    2027                 :             :      * Do NOT recurse into Query nodes, because fireRIRrules already processed
    2028                 :             :      * subselects of subselects for us.
    2029                 :             :      */
    2030                 :     1442146 :     return expression_tree_walker(node, fireRIRonSubLink, context);
    2031                 :             : }
    2032                 :             : 
    2033                 :             : 
    2034                 :             : /*
    2035                 :             :  * fireRIRrules -
    2036                 :             :  *  Apply all RIR rules on each rangetable entry in the given query
    2037                 :             :  *
    2038                 :             :  * activeRIRs is a list of the OIDs of views we're already processing RIR
    2039                 :             :  * rules for, used to detect/reject recursion.
    2040                 :             :  */
    2041                 :             : static Query *
    2042                 :      374609 : fireRIRrules(Query *parsetree, List *activeRIRs)
    2043                 :             : {
    2044                 :      374609 :     int         origResultRelation = parsetree->resultRelation;
    2045                 :             :     int         rt_index;
    2046                 :             :     ListCell   *lc;
    2047                 :             : 
    2048                 :             :     /*
    2049                 :             :      * Expand SEARCH and CYCLE clauses in CTEs.
    2050                 :             :      *
    2051                 :             :      * This is just a convenient place to do this, since we are already
    2052                 :             :      * looking at each Query.
    2053                 :             :      */
    2054   [ +  +  +  +  :      377228 :     foreach(lc, parsetree->cteList)
                   +  + ]
    2055                 :             :     {
    2056                 :        2623 :         CommonTableExpr *cte = lfirst_node(CommonTableExpr, lc);
    2057                 :             : 
    2058   [ +  +  +  + ]:        2623 :         if (cte->search_clause || cte->cycle_clause)
    2059                 :             :         {
    2060                 :          96 :             cte = rewriteSearchAndCycle(cte);
    2061                 :          92 :             lfirst(lc) = cte;
    2062                 :             :         }
    2063                 :             :     }
    2064                 :             : 
    2065                 :             :     /*
    2066                 :             :      * don't try to convert this into a foreach loop, because rtable list can
    2067                 :             :      * get changed each time through...
    2068                 :             :      */
    2069                 :      374605 :     rt_index = 0;
    2070         [ +  + ]:      840216 :     while (rt_index < list_length(parsetree->rtable))
    2071                 :             :     {
    2072                 :             :         RangeTblEntry *rte;
    2073                 :             :         Relation    rel;
    2074                 :             :         List       *locks;
    2075                 :             :         RuleLock   *rules;
    2076                 :             :         RewriteRule *rule;
    2077                 :             :         int         i;
    2078                 :             : 
    2079                 :      465699 :         ++rt_index;
    2080                 :             : 
    2081                 :      465699 :         rte = rt_fetch(rt_index, parsetree->rtable);
    2082                 :             : 
    2083                 :             :         /*
    2084                 :             :          * Convert GRAPH_TABLE clause into a subquery using relational
    2085                 :             :          * operators.  (This will change the rtekind to subquery, so it must
    2086                 :             :          * be done before the subquery handling below.)
    2087                 :             :          */
    2088         [ +  + ]:      465699 :         if (rte->rtekind == RTE_GRAPH_TABLE)
    2089                 :             :         {
    2090                 :         514 :             parsetree = rewriteGraphTable(parsetree, rt_index);
    2091                 :             :         }
    2092                 :             : 
    2093                 :             :         /*
    2094                 :             :          * A subquery RTE can't have associated rules, so there's nothing to
    2095                 :             :          * do to this level of the query, but we must recurse into the
    2096                 :             :          * subquery to expand any rule references in it.
    2097                 :             :          */
    2098         [ +  + ]:      465659 :         if (rte->rtekind == RTE_SUBQUERY)
    2099                 :             :         {
    2100                 :       38640 :             rte->subquery = fireRIRrules(rte->subquery, activeRIRs);
    2101                 :             : 
    2102                 :             :             /*
    2103                 :             :              * While we are here, make sure the query is marked as having row
    2104                 :             :              * security if any of its subqueries do.
    2105                 :             :              */
    2106                 :       38616 :             parsetree->hasRowSecurity |= rte->subquery->hasRowSecurity;
    2107                 :             : 
    2108                 :       38616 :             continue;
    2109                 :             :         }
    2110                 :             : 
    2111                 :             :         /*
    2112                 :             :          * Joins and other non-relation RTEs can be ignored completely.
    2113                 :             :          */
    2114         [ +  + ]:      427019 :         if (rte->rtekind != RTE_RELATION)
    2115                 :      107122 :             continue;
    2116                 :             : 
    2117                 :             :         /*
    2118                 :             :          * Always ignore RIR rules for materialized views referenced in
    2119                 :             :          * queries.  (This does not prevent refreshing MVs, since they aren't
    2120                 :             :          * referenced in their own query definitions.)
    2121                 :             :          *
    2122                 :             :          * Note: in the future we might want to allow MVs to be conditionally
    2123                 :             :          * expanded as if they were regular views, if they are not scannable.
    2124                 :             :          * In that case this test would need to be postponed till after we've
    2125                 :             :          * opened the rel, so that we could check its state.
    2126                 :             :          */
    2127         [ +  + ]:      319897 :         if (rte->relkind == RELKIND_MATVIEW)
    2128                 :         291 :             continue;
    2129                 :             : 
    2130                 :             :         /*
    2131                 :             :          * In INSERT ... ON CONFLICT, ignore the EXCLUDED pseudo-relation;
    2132                 :             :          * even if it points to a view, we needn't expand it, and should not
    2133                 :             :          * because we want the RTE to remain of RTE_RELATION type.  Otherwise,
    2134                 :             :          * it would get changed to RTE_SUBQUERY type, which is an
    2135                 :             :          * untested/unsupported situation.
    2136                 :             :          */
    2137         [ +  + ]:      319606 :         if (parsetree->onConflict &&
    2138         [ +  + ]:        3144 :             rt_index == parsetree->onConflict->exclRelIndex)
    2139                 :        1159 :             continue;
    2140                 :             : 
    2141                 :             :         /*
    2142                 :             :          * If the table is not referenced in the query, then we ignore it.
    2143                 :             :          * This prevents infinite expansion loop due to new rtable entries
    2144                 :             :          * inserted by expansion of a rule. A table is referenced if it is
    2145                 :             :          * part of the join set (a source table), or is referenced by any Var
    2146                 :             :          * nodes, or is the result table.
    2147                 :             :          */
    2148         [ +  + ]:      318447 :         if (rt_index != parsetree->resultRelation &&
    2149         [ +  + ]:      260529 :             !rangeTableEntry_used((Node *) parsetree, rt_index, 0))
    2150                 :        5098 :             continue;
    2151                 :             : 
    2152                 :             :         /*
    2153                 :             :          * Also, if this is a new result relation introduced by
    2154                 :             :          * ApplyRetrieveRule, we don't want to do anything more with it.
    2155                 :             :          */
    2156   [ +  +  +  + ]:      313349 :         if (rt_index == parsetree->resultRelation &&
    2157                 :             :             rt_index != origResultRelation)
    2158                 :         190 :             continue;
    2159                 :             : 
    2160                 :             :         /*
    2161                 :             :          * We can use NoLock here since either the parser or
    2162                 :             :          * AcquireRewriteLocks should have locked the rel already.
    2163                 :             :          */
    2164                 :      313159 :         rel = relation_open(rte->relid, NoLock);
    2165                 :             : 
    2166                 :             :         /*
    2167                 :             :          * Collect the RIR rules that we must apply
    2168                 :             :          */
    2169                 :      313159 :         rules = rel->rd_rules;
    2170         [ +  + ]:      313159 :         if (rules != NULL)
    2171                 :             :         {
    2172                 :       11484 :             locks = NIL;
    2173         [ +  + ]:       25102 :             for (i = 0; i < rules->numLocks; i++)
    2174                 :             :             {
    2175                 :       13618 :                 rule = rules->rules[i];
    2176         [ +  + ]:       13618 :                 if (rule->event != CMD_SELECT)
    2177                 :        3137 :                     continue;
    2178                 :             : 
    2179                 :       10481 :                 locks = lappend(locks, rule);
    2180                 :             :             }
    2181                 :             : 
    2182                 :             :             /*
    2183                 :             :              * If we found any, apply them --- but first check for recursion!
    2184                 :             :              */
    2185         [ +  + ]:       11484 :             if (locks != NIL)
    2186                 :             :             {
    2187                 :             :                 ListCell   *l;
    2188                 :             : 
    2189         [ -  + ]:       10481 :                 if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
    2190         [ #  # ]:           0 :                     ereport(ERROR,
    2191                 :             :                             (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    2192                 :             :                              errmsg("infinite recursion detected in rules for relation \"%s\"",
    2193                 :             :                                     RelationGetRelationName(rel))));
    2194                 :       10481 :                 activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
    2195                 :             : 
    2196   [ +  -  +  +  :       20938 :                 foreach(l, locks)
                   +  + ]
    2197                 :             :                 {
    2198                 :       10481 :                     rule = lfirst(l);
    2199                 :             : 
    2200                 :       10481 :                     parsetree = ApplyRetrieveRule(parsetree,
    2201                 :             :                                                   rule,
    2202                 :             :                                                   rt_index,
    2203                 :             :                                                   rel,
    2204                 :             :                                                   activeRIRs);
    2205                 :             :                 }
    2206                 :             : 
    2207                 :       10457 :                 activeRIRs = list_delete_last(activeRIRs);
    2208                 :             :             }
    2209                 :             :         }
    2210                 :             : 
    2211                 :      313135 :         table_close(rel, NoLock);
    2212                 :             :     }
    2213                 :             : 
    2214                 :             :     /* Recurse into subqueries in WITH */
    2215   [ +  +  +  +  :      377136 :     foreach(lc, parsetree->cteList)
                   +  + ]
    2216                 :             :     {
    2217                 :        2619 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
    2218                 :             : 
    2219                 :        2619 :         cte->ctequery = (Node *)
    2220                 :        2619 :             fireRIRrules((Query *) cte->ctequery, activeRIRs);
    2221                 :             : 
    2222                 :             :         /*
    2223                 :             :          * While we are here, make sure the query is marked as having row
    2224                 :             :          * security if any of its CTEs do.
    2225                 :             :          */
    2226                 :        2619 :         parsetree->hasRowSecurity |= ((Query *) cte->ctequery)->hasRowSecurity;
    2227                 :             :     }
    2228                 :             : 
    2229                 :             :     /*
    2230                 :             :      * Recurse into sublink subqueries, too.  But we already did the ones in
    2231                 :             :      * the rtable and cteList.
    2232                 :             :      */
    2233         [ +  + ]:      374517 :     if (parsetree->hasSubLinks)
    2234                 :             :     {
    2235                 :             :         fireRIRonSubLink_context context;
    2236                 :             : 
    2237                 :       26064 :         context.activeRIRs = activeRIRs;
    2238                 :       26064 :         context.hasRowSecurity = false;
    2239                 :             : 
    2240                 :       26064 :         query_tree_walker(parsetree, fireRIRonSubLink, &context,
    2241                 :             :                           QTW_IGNORE_RC_SUBQUERIES);
    2242                 :             : 
    2243                 :             :         /*
    2244                 :             :          * Make sure the query is marked as having row security if any of its
    2245                 :             :          * sublinks do.
    2246                 :             :          */
    2247                 :       26064 :         parsetree->hasRowSecurity |= context.hasRowSecurity;
    2248                 :             :     }
    2249                 :             : 
    2250                 :             :     /*
    2251                 :             :      * Apply any row-level security policies.  We do this last because it
    2252                 :             :      * requires special recursion detection if the new quals have sublink
    2253                 :             :      * subqueries, and if we did it in the loop above query_tree_walker would
    2254                 :             :      * then recurse into those quals a second time.
    2255                 :             :      */
    2256                 :      374517 :     rt_index = 0;
    2257   [ +  +  +  +  :      839988 :     foreach(lc, parsetree->rtable)
                   +  + ]
    2258                 :             :     {
    2259                 :      465611 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
    2260                 :             :         Relation    rel;
    2261                 :             :         List       *securityQuals;
    2262                 :             :         List       *withCheckOptions;
    2263                 :             :         bool        hasRowSecurity;
    2264                 :             :         bool        hasSubLinks;
    2265                 :             : 
    2266                 :      465611 :         ++rt_index;
    2267                 :             : 
    2268                 :             :         /* Only normal relations can have RLS policies */
    2269         [ +  + ]:      465611 :         if (rte->rtekind != RTE_RELATION ||
    2270         [ +  + ]:      309495 :             (rte->relkind != RELKIND_RELATION &&
    2271         [ +  + ]:       18011 :              rte->relkind != RELKIND_PARTITIONED_TABLE))
    2272                 :      162055 :             continue;
    2273                 :             : 
    2274                 :      303556 :         rel = relation_open(rte->relid, NoLock);
    2275                 :             : 
    2276                 :             :         /*
    2277                 :             :          * Fetch any new security quals that must be applied to this RTE.
    2278                 :             :          */
    2279                 :      303556 :         get_row_security_policies(parsetree, rte, rt_index,
    2280                 :             :                                   &securityQuals, &withCheckOptions,
    2281                 :             :                                   &hasRowSecurity, &hasSubLinks);
    2282                 :             : 
    2283   [ +  +  +  + ]:      303500 :         if (securityQuals != NIL || withCheckOptions != NIL)
    2284                 :             :         {
    2285         [ +  + ]:        2243 :             if (hasSubLinks)
    2286                 :             :             {
    2287                 :             :                 acquireLocksOnSubLinks_context context;
    2288                 :             :                 fireRIRonSubLink_context fire_context;
    2289                 :             : 
    2290                 :             :                 /*
    2291                 :             :                  * Recursively process the new quals, checking for infinite
    2292                 :             :                  * recursion.
    2293                 :             :                  */
    2294         [ +  + ]:         537 :                 if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
    2295         [ +  - ]:          32 :                     ereport(ERROR,
    2296                 :             :                             (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    2297                 :             :                              errmsg("infinite recursion detected in policy for relation \"%s\"",
    2298                 :             :                                     RelationGetRelationName(rel))));
    2299                 :             : 
    2300                 :         505 :                 activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
    2301                 :             : 
    2302                 :             :                 /*
    2303                 :             :                  * get_row_security_policies just passed back securityQuals
    2304                 :             :                  * and/or withCheckOptions, and there were SubLinks, make sure
    2305                 :             :                  * we lock any relations which are referenced.
    2306                 :             :                  *
    2307                 :             :                  * These locks would normally be acquired by the parser, but
    2308                 :             :                  * securityQuals and withCheckOptions are added post-parsing.
    2309                 :             :                  */
    2310                 :         505 :                 context.for_execute = true;
    2311                 :         505 :                 (void) acquireLocksOnSubLinks((Node *) securityQuals, &context);
    2312                 :         505 :                 (void) acquireLocksOnSubLinks((Node *) withCheckOptions,
    2313                 :             :                                               &context);
    2314                 :             : 
    2315                 :             :                 /*
    2316                 :             :                  * Now that we have the locks on anything added by
    2317                 :             :                  * get_row_security_policies, fire any RIR rules for them.
    2318                 :             :                  */
    2319                 :         505 :                 fire_context.activeRIRs = activeRIRs;
    2320                 :         505 :                 fire_context.hasRowSecurity = false;
    2321                 :             : 
    2322                 :         505 :                 expression_tree_walker((Node *) securityQuals,
    2323                 :             :                                        fireRIRonSubLink, &fire_context);
    2324                 :             : 
    2325                 :         457 :                 expression_tree_walker((Node *) withCheckOptions,
    2326                 :             :                                        fireRIRonSubLink, &fire_context);
    2327                 :             : 
    2328                 :             :                 /*
    2329                 :             :                  * We can ignore the value of fire_context.hasRowSecurity
    2330                 :             :                  * since we only reach this code in cases where hasRowSecurity
    2331                 :             :                  * is already true.
    2332                 :             :                  */
    2333                 :             :                 Assert(hasRowSecurity);
    2334                 :             : 
    2335                 :         453 :                 activeRIRs = list_delete_last(activeRIRs);
    2336                 :             :             }
    2337                 :             : 
    2338                 :             :             /*
    2339                 :             :              * Add the new security barrier quals to the start of the RTE's
    2340                 :             :              * list so that they get applied before any existing barrier quals
    2341                 :             :              * (which would have come from a security-barrier view, and should
    2342                 :             :              * get lower priority than RLS conditions on the table itself).
    2343                 :             :              */
    2344                 :        4318 :             rte->securityQuals = list_concat(securityQuals,
    2345                 :        2159 :                                              rte->securityQuals);
    2346                 :             : 
    2347                 :        2159 :             parsetree->withCheckOptions = list_concat(withCheckOptions,
    2348                 :        2159 :                                                       parsetree->withCheckOptions);
    2349                 :             :         }
    2350                 :             : 
    2351                 :             :         /*
    2352                 :             :          * Make sure the query is marked correctly if row-level security
    2353                 :             :          * applies, or if the new quals had sublinks.
    2354                 :             :          */
    2355         [ +  + ]:      303416 :         if (hasRowSecurity)
    2356                 :        2649 :             parsetree->hasRowSecurity = true;
    2357         [ +  + ]:      303416 :         if (hasSubLinks)
    2358                 :         453 :             parsetree->hasSubLinks = true;
    2359                 :             : 
    2360                 :      303416 :         table_close(rel, NoLock);
    2361                 :             :     }
    2362                 :             : 
    2363                 :      374377 :     return parsetree;
    2364                 :             : }
    2365                 :             : 
    2366                 :             : 
    2367                 :             : /*
    2368                 :             :  * Modify the given query by adding 'AND rule_qual IS NOT TRUE' to its
    2369                 :             :  * qualification.  This is used to generate suitable "else clauses" for
    2370                 :             :  * conditional INSTEAD rules.  (Unfortunately we must use "x IS NOT TRUE",
    2371                 :             :  * not just "NOT x" which the planner is much smarter about, else we will
    2372                 :             :  * do the wrong thing when the qual evaluates to NULL.)
    2373                 :             :  *
    2374                 :             :  * The rule_qual may contain references to OLD or NEW.  OLD references are
    2375                 :             :  * replaced by references to the specified rt_index (the relation that the
    2376                 :             :  * rule applies to).  NEW references are only possible for INSERT and UPDATE
    2377                 :             :  * queries on the relation itself, and so they should be replaced by copies
    2378                 :             :  * of the related entries in the query's own targetlist.
    2379                 :             :  */
    2380                 :             : static Query *
    2381                 :         304 : CopyAndAddInvertedQual(Query *parsetree,
    2382                 :             :                        Node *rule_qual,
    2383                 :             :                        int rt_index,
    2384                 :             :                        CmdType event)
    2385                 :             : {
    2386                 :             :     /* Don't scribble on the passed qual (it's in the relcache!) */
    2387                 :         304 :     Node       *new_qual = copyObject(rule_qual);
    2388                 :             :     acquireLocksOnSubLinks_context context;
    2389                 :             : 
    2390                 :         304 :     context.for_execute = true;
    2391                 :             : 
    2392                 :             :     /*
    2393                 :             :      * In case there are subqueries in the qual, acquire necessary locks and
    2394                 :             :      * fix any deleted JOIN RTE entries.  (This is somewhat redundant with
    2395                 :             :      * rewriteRuleAction, but not entirely ... consider restructuring so that
    2396                 :             :      * we only need to process the qual this way once.)
    2397                 :             :      */
    2398                 :         304 :     (void) acquireLocksOnSubLinks(new_qual, &context);
    2399                 :             : 
    2400                 :             :     /* Fix references to OLD */
    2401                 :         304 :     ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
    2402                 :             :     /* Fix references to NEW */
    2403   [ +  +  +  + ]:         304 :     if (event == CMD_INSERT || event == CMD_UPDATE)
    2404                 :             :     {
    2405                 :         296 :         RangeTblEntry *rte = rt_fetch(rt_index, parsetree->rtable);
    2406                 :             :         Relation    rel;
    2407                 :             :         List       *gen_cols;
    2408                 :             : 
    2409                 :             :         /*
    2410                 :             :          * As in rewriteRuleAction, build entries for generated columns so
    2411                 :             :          * that new.gen_col in the rule qualification can be rewritten
    2412                 :             :          * correctly.
    2413                 :             :          */
    2414                 :         296 :         rel = relation_open(rte->relid, NoLock);
    2415                 :         296 :         gen_cols = get_generated_columns(rel, PRS2_NEW_VARNO, true);
    2416                 :         296 :         relation_close(rel, NoLock);
    2417                 :             : 
    2418                 :             :         /*
    2419                 :             :          * The generated column expressions refer to new.attribute, so they
    2420                 :             :          * must be rewritten before they can be used as replacements.
    2421                 :             :          */
    2422                 :             :         gen_cols = (List *)
    2423         [ +  + ]:         296 :             ReplaceVarsFromTargetList((Node *) gen_cols,
    2424                 :             :                                       PRS2_NEW_VARNO,
    2425                 :             :                                       0,
    2426                 :             :                                       rte,
    2427                 :             :                                       parsetree->targetList,
    2428                 :             :                                       parsetree->resultRelation,
    2429                 :             :                                       (event == CMD_UPDATE) ?
    2430                 :             :                                       REPLACEVARS_CHANGE_VARNO :
    2431                 :             :                                       REPLACEVARS_SUBSTITUTE_NULL,
    2432                 :             :                                       rt_index,
    2433                 :             :                                       &parsetree->hasSubLinks);
    2434                 :             : 
    2435         [ +  + ]:         592 :         new_qual = ReplaceVarsFromTargetList(new_qual,
    2436                 :             :                                              PRS2_NEW_VARNO,
    2437                 :             :                                              0,
    2438                 :             :                                              rte,
    2439                 :             :                                              list_concat(gen_cols,
    2440                 :         296 :                                                          parsetree->targetList),
    2441                 :             :                                              parsetree->resultRelation,
    2442                 :             :                                              (event == CMD_UPDATE) ?
    2443                 :             :                                              REPLACEVARS_CHANGE_VARNO :
    2444                 :             :                                              REPLACEVARS_SUBSTITUTE_NULL,
    2445                 :             :                                              rt_index,
    2446                 :             :                                              &parsetree->hasSubLinks);
    2447                 :             :     }
    2448                 :             :     /* And attach the fixed qual */
    2449                 :         304 :     AddInvertedQual(parsetree, new_qual);
    2450                 :             : 
    2451                 :         304 :     return parsetree;
    2452                 :             : }
    2453                 :             : 
    2454                 :             : 
    2455                 :             : /*
    2456                 :             :  *  fireRules -
    2457                 :             :  *     Iterate through rule locks applying rules.
    2458                 :             :  *
    2459                 :             :  * Input arguments:
    2460                 :             :  *  parsetree - original query
    2461                 :             :  *  rt_index - RT index of result relation in original query
    2462                 :             :  *  event - type of rule event
    2463                 :             :  *  locks - list of rules to fire
    2464                 :             :  * Output arguments:
    2465                 :             :  *  *instead_flag - set true if any unqualified INSTEAD rule is found
    2466                 :             :  *                  (must be initialized to false)
    2467                 :             :  *  *returning_flag - set true if we rewrite RETURNING clause in any rule
    2468                 :             :  *                  (must be initialized to false)
    2469                 :             :  *  *qual_product - filled with modified original query if any qualified
    2470                 :             :  *                  INSTEAD rule is found (must be initialized to NULL)
    2471                 :             :  * Return value:
    2472                 :             :  *  list of rule actions adjusted for use with this query
    2473                 :             :  *
    2474                 :             :  * Qualified INSTEAD rules generate their action with the qualification
    2475                 :             :  * condition added.  They also generate a modified version of the original
    2476                 :             :  * query with the negated qualification added, so that it will run only for
    2477                 :             :  * rows that the qualified action doesn't act on.  (If there are multiple
    2478                 :             :  * qualified INSTEAD rules, we AND all the negated quals onto a single
    2479                 :             :  * modified original query.)  We won't execute the original, unmodified
    2480                 :             :  * query if we find either qualified or unqualified INSTEAD rules.  If
    2481                 :             :  * we find both, the modified original query is discarded too.
    2482                 :             :  */
    2483                 :             : static List *
    2484                 :       60756 : fireRules(Query *parsetree,
    2485                 :             :           int rt_index,
    2486                 :             :           CmdType event,
    2487                 :             :           List *locks,
    2488                 :             :           bool *instead_flag,
    2489                 :             :           bool *returning_flag,
    2490                 :             :           Query **qual_product)
    2491                 :             : {
    2492                 :       60756 :     List       *results = NIL;
    2493                 :             :     ListCell   *l;
    2494                 :             : 
    2495   [ +  +  +  +  :       61852 :     foreach(l, locks)
                   +  + ]
    2496                 :             :     {
    2497                 :        1104 :         RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
    2498                 :        1104 :         Node       *event_qual = rule_lock->qual;
    2499                 :        1104 :         List       *actions = rule_lock->actions;
    2500                 :             :         QuerySource qsrc;
    2501                 :             :         ListCell   *r;
    2502                 :             : 
    2503                 :             :         /* Determine correct QuerySource value for actions */
    2504         [ +  + ]:        1104 :         if (rule_lock->isInstead)
    2505                 :             :         {
    2506         [ +  + ]:         816 :             if (event_qual != NULL)
    2507                 :         308 :                 qsrc = QSRC_QUAL_INSTEAD_RULE;
    2508                 :             :             else
    2509                 :             :             {
    2510                 :         508 :                 qsrc = QSRC_INSTEAD_RULE;
    2511                 :         508 :                 *instead_flag = true;   /* report unqualified INSTEAD */
    2512                 :             :             }
    2513                 :             :         }
    2514                 :             :         else
    2515                 :         288 :             qsrc = QSRC_NON_INSTEAD_RULE;
    2516                 :             : 
    2517         [ +  + ]:        1104 :         if (qsrc == QSRC_QUAL_INSTEAD_RULE)
    2518                 :             :         {
    2519                 :             :             /*
    2520                 :             :              * If there are INSTEAD rules with qualifications, the original
    2521                 :             :              * query is still performed. But all the negated rule
    2522                 :             :              * qualifications of the INSTEAD rules are added so it does its
    2523                 :             :              * actions only in cases where the rule quals of all INSTEAD rules
    2524                 :             :              * are false. Think of it as the default action in a case. We save
    2525                 :             :              * this in *qual_product so RewriteQuery() can add it to the query
    2526                 :             :              * list after we mangled it up enough.
    2527                 :             :              *
    2528                 :             :              * If we have already found an unqualified INSTEAD rule, then
    2529                 :             :              * *qual_product won't be used, so don't bother building it.
    2530                 :             :              */
    2531         [ +  + ]:         308 :             if (!*instead_flag)
    2532                 :             :             {
    2533         [ +  + ]:         304 :                 if (*qual_product == NULL)
    2534                 :         248 :                     *qual_product = copyObject(parsetree);
    2535                 :         304 :                 *qual_product = CopyAndAddInvertedQual(*qual_product,
    2536                 :             :                                                        event_qual,
    2537                 :             :                                                        rt_index,
    2538                 :             :                                                        event);
    2539                 :             :             }
    2540                 :             :         }
    2541                 :             : 
    2542                 :             :         /* Now process the rule's actions and add them to the result list */
    2543   [ +  -  +  +  :        2236 :         foreach(r, actions)
                   +  + ]
    2544                 :             :         {
    2545                 :        1140 :             Query      *rule_action = lfirst(r);
    2546                 :             : 
    2547         [ +  + ]:        1140 :             if (rule_action->commandType == CMD_NOTHING)
    2548                 :         148 :                 continue;
    2549                 :             : 
    2550                 :         992 :             rule_action = rewriteRuleAction(parsetree, rule_action,
    2551                 :             :                                             event_qual, rt_index, event,
    2552                 :             :                                             returning_flag);
    2553                 :             : 
    2554                 :         984 :             rule_action->querySource = qsrc;
    2555                 :         984 :             rule_action->canSetTag = false; /* might change later */
    2556                 :             : 
    2557                 :         984 :             results = lappend(results, rule_action);
    2558                 :             :         }
    2559                 :             :     }
    2560                 :             : 
    2561                 :       60748 :     return results;
    2562                 :             : }
    2563                 :             : 
    2564                 :             : 
    2565                 :             : /*
    2566                 :             :  * get_view_query - get the Query from a view's _RETURN rule.
    2567                 :             :  *
    2568                 :             :  * Caller should have verified that the relation is a view, and therefore
    2569                 :             :  * we should find an ON SELECT action.
    2570                 :             :  *
    2571                 :             :  * Note that the pointer returned is into the relcache and therefore must
    2572                 :             :  * be treated as read-only to the caller and not modified or scribbled on.
    2573                 :             :  */
    2574                 :             : Query *
    2575                 :        4019 : get_view_query(Relation view)
    2576                 :             : {
    2577                 :             :     int         i;
    2578                 :             : 
    2579                 :             :     Assert(view->rd_rel->relkind == RELKIND_VIEW);
    2580                 :             : 
    2581         [ +  - ]:        4019 :     for (i = 0; i < view->rd_rules->numLocks; i++)
    2582                 :             :     {
    2583                 :        4019 :         RewriteRule *rule = view->rd_rules->rules[i];
    2584                 :             : 
    2585         [ +  - ]:        4019 :         if (rule->event == CMD_SELECT)
    2586                 :             :         {
    2587                 :             :             /* A _RETURN rule should have only one action */
    2588         [ -  + ]:        4019 :             if (list_length(rule->actions) != 1)
    2589         [ #  # ]:           0 :                 elog(ERROR, "invalid _RETURN rule action specification");
    2590                 :             : 
    2591                 :        4019 :             return (Query *) linitial(rule->actions);
    2592                 :             :         }
    2593                 :             :     }
    2594                 :             : 
    2595         [ #  # ]:           0 :     elog(ERROR, "failed to find _RETURN rule for view");
    2596                 :             :     return NULL;                /* keep compiler quiet */
    2597                 :             : }
    2598                 :             : 
    2599                 :             : 
    2600                 :             : /*
    2601                 :             :  * view_has_instead_trigger - does view have an INSTEAD OF trigger for event?
    2602                 :             :  *
    2603                 :             :  * If it does, we don't want to treat it as auto-updatable.  This test can't
    2604                 :             :  * be folded into view_query_is_auto_updatable because it's not an error
    2605                 :             :  * condition.
    2606                 :             :  *
    2607                 :             :  * For MERGE, this will return true if there is an INSTEAD OF trigger for
    2608                 :             :  * every action in mergeActionList, and false if there are any actions that
    2609                 :             :  * lack an INSTEAD OF trigger.  If there are no data-modifying MERGE actions
    2610                 :             :  * (only DO NOTHING actions), true is returned so that the view is treated
    2611                 :             :  * as trigger-updatable, rather than erroring out if it's not auto-updatable.
    2612                 :             :  */
    2613                 :             : bool
    2614                 :        3792 : view_has_instead_trigger(Relation view, CmdType event, List *mergeActionList)
    2615                 :             : {
    2616                 :        3792 :     TriggerDesc *trigDesc = view->trigdesc;
    2617                 :             : 
    2618   [ +  +  +  +  :        3792 :     switch (event)
                      - ]
    2619                 :             :     {
    2620                 :        1238 :         case CMD_INSERT:
    2621   [ +  +  +  - ]:        1238 :             if (trigDesc && trigDesc->trig_insert_instead_row)
    2622                 :         174 :                 return true;
    2623                 :        1064 :             break;
    2624                 :        1416 :         case CMD_UPDATE:
    2625   [ +  +  +  - ]:        1416 :             if (trigDesc && trigDesc->trig_update_instead_row)
    2626                 :         226 :                 return true;
    2627                 :        1190 :             break;
    2628                 :         438 :         case CMD_DELETE:
    2629   [ +  +  +  - ]:         438 :             if (trigDesc && trigDesc->trig_delete_instead_row)
    2630                 :          78 :                 return true;
    2631                 :         360 :             break;
    2632                 :         700 :         case CMD_MERGE:
    2633   [ +  -  +  +  :         988 :             foreach_node(MergeAction, action, mergeActionList)
                   +  + ]
    2634                 :             :             {
    2635   [ +  +  +  +  :         780 :                 switch (action->commandType)
                      - ]
    2636                 :             :                 {
    2637                 :         136 :                     case CMD_INSERT:
    2638   [ +  +  +  + ]:         136 :                         if (!trigDesc || !trigDesc->trig_insert_instead_row)
    2639                 :         596 :                             return false;
    2640                 :          56 :                         break;
    2641                 :         496 :                     case CMD_UPDATE:
    2642   [ +  +  -  + ]:         496 :                         if (!trigDesc || !trigDesc->trig_update_instead_row)
    2643                 :         424 :                             return false;
    2644                 :          72 :                         break;
    2645                 :         108 :                     case CMD_DELETE:
    2646   [ +  +  +  + ]:         108 :                         if (!trigDesc || !trigDesc->trig_delete_instead_row)
    2647                 :          92 :                             return false;
    2648                 :          16 :                         break;
    2649                 :          40 :                     case CMD_NOTHING:
    2650                 :             :                         /* No trigger required */
    2651                 :          40 :                         break;
    2652                 :           0 :                     default:
    2653         [ #  # ]:           0 :                         elog(ERROR, "unrecognized commandType: %d", action->commandType);
    2654                 :             :                         break;
    2655                 :             :                 }
    2656                 :             :             }
    2657                 :         104 :             return true;        /* no actions without an INSTEAD OF trigger */
    2658                 :           0 :         default:
    2659         [ #  # ]:           0 :             elog(ERROR, "unrecognized CmdType: %d", (int) event);
    2660                 :             :             break;
    2661                 :             :     }
    2662                 :        2614 :     return false;
    2663                 :             : }
    2664                 :             : 
    2665                 :             : 
    2666                 :             : /*
    2667                 :             :  * view_col_is_auto_updatable - test whether the specified column of a view
    2668                 :             :  * is auto-updatable. Returns NULL (if the column can be updated) or a message
    2669                 :             :  * string giving the reason that it cannot be.
    2670                 :             :  *
    2671                 :             :  * The returned string has not been translated; if it is shown as an error
    2672                 :             :  * message, the caller should apply _() to translate it.
    2673                 :             :  *
    2674                 :             :  * Note that the checks performed here are local to this view. We do not check
    2675                 :             :  * whether the referenced column of the underlying base relation is updatable.
    2676                 :             :  */
    2677                 :             : static const char *
    2678                 :        9948 : view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)
    2679                 :             : {
    2680                 :        9948 :     Var        *var = (Var *) tle->expr;
    2681                 :             : 
    2682                 :             :     /*
    2683                 :             :      * For now, the only updatable columns we support are those that are Vars
    2684                 :             :      * referring to user columns of the underlying base relation.
    2685                 :             :      *
    2686                 :             :      * The view targetlist may contain resjunk columns (e.g., a view defined
    2687                 :             :      * like "SELECT * FROM t ORDER BY a+b" is auto-updatable) but such columns
    2688                 :             :      * are not auto-updatable, and in fact should never appear in the outer
    2689                 :             :      * query's targetlist.
    2690                 :             :      */
    2691         [ +  + ]:        9948 :     if (tle->resjunk)
    2692                 :         120 :         return gettext_noop("Junk view columns are not updatable.");
    2693                 :             : 
    2694         [ +  + ]:        9828 :     if (!IsA(var, Var) ||
    2695         [ +  - ]:        8808 :         var->varno != rtr->rtindex ||
    2696         [ -  + ]:        8808 :         var->varlevelsup != 0)
    2697                 :        1020 :         return gettext_noop("View columns that are not columns of their base relation are not updatable.");
    2698                 :             : 
    2699         [ +  + ]:        8808 :     if (var->varattno < 0)
    2700                 :         268 :         return gettext_noop("View columns that refer to system columns are not updatable.");
    2701                 :             : 
    2702         [ -  + ]:        8540 :     if (var->varattno == 0)
    2703                 :           0 :         return gettext_noop("View columns that return whole-row references are not updatable.");
    2704                 :             : 
    2705                 :        8540 :     return NULL;                /* the view column is updatable */
    2706                 :             : }
    2707                 :             : 
    2708                 :             : 
    2709                 :             : /*
    2710                 :             :  * view_query_is_auto_updatable - test whether the specified view definition
    2711                 :             :  * represents an auto-updatable view. Returns NULL (if the view can be updated)
    2712                 :             :  * or a message string giving the reason that it cannot be.
    2713                 :             :  *
    2714                 :             :  * The returned string has not been translated; if it is shown as an error
    2715                 :             :  * message, the caller should apply _() to translate it.
    2716                 :             :  *
    2717                 :             :  * If check_cols is true, the view is required to have at least one updatable
    2718                 :             :  * column (necessary for INSERT/UPDATE). Otherwise the view's columns are not
    2719                 :             :  * checked for updatability. See also view_cols_are_auto_updatable.
    2720                 :             :  *
    2721                 :             :  * Note that the checks performed here are only based on the view definition.
    2722                 :             :  * We do not check whether any base relations referred to by the view are
    2723                 :             :  * updatable.
    2724                 :             :  */
    2725                 :             : const char *
    2726                 :        3862 : view_query_is_auto_updatable(Query *viewquery, bool check_cols)
    2727                 :             : {
    2728                 :             :     RangeTblRef *rtr;
    2729                 :             :     RangeTblEntry *base_rte;
    2730                 :             : 
    2731                 :             :     /*----------
    2732                 :             :      * Check if the view is simply updatable.  According to SQL-92 this means:
    2733                 :             :      *  - No DISTINCT clause.
    2734                 :             :      *  - Each TLE is a column reference, and each column appears at most once.
    2735                 :             :      *  - FROM contains exactly one base relation.
    2736                 :             :      *  - No GROUP BY or HAVING clauses.
    2737                 :             :      *  - No set operations (UNION, INTERSECT or EXCEPT).
    2738                 :             :      *  - No sub-queries in the WHERE clause that reference the target table.
    2739                 :             :      *
    2740                 :             :      * We ignore that last restriction since it would be complex to enforce
    2741                 :             :      * and there isn't any actual benefit to disallowing sub-queries.  (The
    2742                 :             :      * semantic issues that the standard is presumably concerned about don't
    2743                 :             :      * arise in Postgres, since any such sub-query will not see any updates
    2744                 :             :      * executed by the outer query anyway, thanks to MVCC snapshotting.)
    2745                 :             :      *
    2746                 :             :      * We also relax the second restriction by supporting part of SQL:1999
    2747                 :             :      * feature T111, which allows for a mix of updatable and non-updatable
    2748                 :             :      * columns, provided that an INSERT or UPDATE doesn't attempt to assign to
    2749                 :             :      * a non-updatable column.
    2750                 :             :      *
    2751                 :             :      * In addition we impose these constraints, involving features that are
    2752                 :             :      * not part of SQL-92:
    2753                 :             :      *  - No CTEs (WITH clauses).
    2754                 :             :      *  - No OFFSET or LIMIT clauses (this matches a SQL:2008 restriction).
    2755                 :             :      *  - No system columns (including whole-row references) in the tlist.
    2756                 :             :      *  - No window functions in the tlist.
    2757                 :             :      *  - No set-returning functions in the tlist.
    2758                 :             :      *
    2759                 :             :      * Note that we do these checks without recursively expanding the view.
    2760                 :             :      * If the base relation is a view, we'll recursively deal with it later.
    2761                 :             :      *----------
    2762                 :             :      */
    2763         [ +  + ]:        3862 :     if (viewquery->distinctClause != NIL)
    2764                 :          48 :         return gettext_noop("Views containing DISTINCT are not automatically updatable.");
    2765                 :             : 
    2766   [ +  +  -  + ]:        3814 :     if (viewquery->groupClause != NIL || viewquery->groupingSets)
    2767                 :          24 :         return gettext_noop("Views containing GROUP BY are not automatically updatable.");
    2768                 :             : 
    2769         [ +  + ]:        3790 :     if (viewquery->havingQual != NULL)
    2770                 :          20 :         return gettext_noop("Views containing HAVING are not automatically updatable.");
    2771                 :             : 
    2772         [ +  + ]:        3770 :     if (viewquery->setOperations != NULL)
    2773                 :          24 :         return gettext_noop("Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
    2774                 :             : 
    2775         [ +  + ]:        3746 :     if (viewquery->cteList != NIL)
    2776                 :          24 :         return gettext_noop("Views containing WITH are not automatically updatable.");
    2777                 :             : 
    2778   [ +  +  +  + ]:        3722 :     if (viewquery->limitOffset != NULL || viewquery->limitCount != NULL)
    2779                 :         384 :         return gettext_noop("Views containing LIMIT or OFFSET are not automatically updatable.");
    2780                 :             : 
    2781                 :             :     /*
    2782                 :             :      * We must not allow window functions or set returning functions in the
    2783                 :             :      * targetlist. Otherwise we might end up inserting them into the quals of
    2784                 :             :      * the main query. We must also check for aggregates in the targetlist in
    2785                 :             :      * case they appear without a GROUP BY.
    2786                 :             :      *
    2787                 :             :      * These restrictions ensure that each row of the view corresponds to a
    2788                 :             :      * unique row in the underlying base relation.
    2789                 :             :      */
    2790         [ +  + ]:        3338 :     if (viewquery->hasAggs)
    2791                 :          20 :         return gettext_noop("Views that return aggregate functions are not automatically updatable.");
    2792                 :             : 
    2793         [ +  + ]:        3318 :     if (viewquery->hasWindowFuncs)
    2794                 :          24 :         return gettext_noop("Views that return window functions are not automatically updatable.");
    2795                 :             : 
    2796         [ +  + ]:        3294 :     if (viewquery->hasTargetSRFs)
    2797                 :          28 :         return gettext_noop("Views that return set-returning functions are not automatically updatable.");
    2798                 :             : 
    2799                 :             :     /*
    2800                 :             :      * The view query should select from a single base relation, which must be
    2801                 :             :      * a table or another view.
    2802                 :             :      */
    2803         [ +  + ]:        3266 :     if (list_length(viewquery->jointree->fromlist) != 1)
    2804                 :          44 :         return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
    2805                 :             : 
    2806                 :        3222 :     rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
    2807         [ -  + ]:        3222 :     if (!IsA(rtr, RangeTblRef))
    2808                 :           0 :         return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
    2809                 :             : 
    2810                 :        3222 :     base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
    2811         [ +  + ]:        3222 :     if (base_rte->rtekind != RTE_RELATION ||
    2812         [ +  + ]:        3146 :         (base_rte->relkind != RELKIND_RELATION &&
    2813         [ +  + ]:        1197 :          base_rte->relkind != RELKIND_FOREIGN_TABLE &&
    2814         [ +  + ]:        1186 :          base_rte->relkind != RELKIND_VIEW &&
    2815         [ +  + ]:         166 :          base_rte->relkind != RELKIND_PARTITIONED_TABLE))
    2816                 :         104 :         return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
    2817                 :             : 
    2818         [ +  + ]:        3118 :     if (base_rte->tablesample)
    2819                 :           4 :         return gettext_noop("Views containing TABLESAMPLE are not automatically updatable.");
    2820                 :             : 
    2821                 :             :     /*
    2822                 :             :      * Check that the view has at least one updatable column. This is required
    2823                 :             :      * for INSERT/UPDATE but not for DELETE.
    2824                 :             :      */
    2825         [ +  + ]:        3114 :     if (check_cols)
    2826                 :             :     {
    2827                 :             :         ListCell   *cell;
    2828                 :             :         bool        found;
    2829                 :             : 
    2830                 :        2146 :         found = false;
    2831   [ +  -  +  -  :        2270 :         foreach(cell, viewquery->targetList)
                   +  - ]
    2832                 :             :         {
    2833                 :        2270 :             TargetEntry *tle = (TargetEntry *) lfirst(cell);
    2834                 :             : 
    2835         [ +  + ]:        2270 :             if (view_col_is_auto_updatable(rtr, tle) == NULL)
    2836                 :             :             {
    2837                 :        2146 :                 found = true;
    2838                 :        2146 :                 break;
    2839                 :             :             }
    2840                 :             :         }
    2841                 :             : 
    2842         [ -  + ]:        2146 :         if (!found)
    2843                 :           0 :             return gettext_noop("Views that have no updatable columns are not automatically updatable.");
    2844                 :             :     }
    2845                 :             : 
    2846                 :        3114 :     return NULL;                /* the view is updatable */
    2847                 :             : }
    2848                 :             : 
    2849                 :             : 
    2850                 :             : /*
    2851                 :             :  * view_cols_are_auto_updatable - test whether all of the required columns of
    2852                 :             :  * an auto-updatable view are actually updatable. Returns NULL (if all the
    2853                 :             :  * required columns can be updated) or a message string giving the reason that
    2854                 :             :  * they cannot be.
    2855                 :             :  *
    2856                 :             :  * The returned string has not been translated; if it is shown as an error
    2857                 :             :  * message, the caller should apply _() to translate it.
    2858                 :             :  *
    2859                 :             :  * This should be used for INSERT/UPDATE to ensure that we don't attempt to
    2860                 :             :  * assign to any non-updatable columns.
    2861                 :             :  *
    2862                 :             :  * Additionally it may be used to retrieve the set of updatable columns in the
    2863                 :             :  * view, or if one or more of the required columns is not updatable, the name
    2864                 :             :  * of the first offending non-updatable column.
    2865                 :             :  *
    2866                 :             :  * The caller must have already verified that this is an auto-updatable view
    2867                 :             :  * using view_query_is_auto_updatable.
    2868                 :             :  *
    2869                 :             :  * Note that the checks performed here are only based on the view definition.
    2870                 :             :  * We do not check whether the referenced columns of the base relation are
    2871                 :             :  * updatable.
    2872                 :             :  */
    2873                 :             : static const char *
    2874                 :        2770 : view_cols_are_auto_updatable(Query *viewquery,
    2875                 :             :                              Bitmapset *required_cols,
    2876                 :             :                              Bitmapset **updatable_cols,
    2877                 :             :                              char **non_updatable_col)
    2878                 :             : {
    2879                 :             :     RangeTblRef *rtr;
    2880                 :             :     AttrNumber  col;
    2881                 :             :     ListCell   *cell;
    2882                 :             : 
    2883                 :             :     /*
    2884                 :             :      * The caller should have verified that this view is auto-updatable and so
    2885                 :             :      * there should be a single base relation.
    2886                 :             :      */
    2887                 :             :     Assert(list_length(viewquery->jointree->fromlist) == 1);
    2888                 :        2770 :     rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
    2889                 :             : 
    2890                 :             :     /* Initialize the optional return values */
    2891         [ +  + ]:        2770 :     if (updatable_cols != NULL)
    2892                 :         700 :         *updatable_cols = NULL;
    2893         [ +  + ]:        2770 :     if (non_updatable_col != NULL)
    2894                 :        2070 :         *non_updatable_col = NULL;
    2895                 :             : 
    2896                 :             :     /* Test each view column for updatability */
    2897                 :        2770 :     col = -FirstLowInvalidHeapAttributeNumber;
    2898   [ +  -  +  +  :       10360 :     foreach(cell, viewquery->targetList)
                   +  + ]
    2899                 :             :     {
    2900                 :        7678 :         TargetEntry *tle = (TargetEntry *) lfirst(cell);
    2901                 :             :         const char *col_update_detail;
    2902                 :             : 
    2903                 :        7678 :         col++;
    2904                 :        7678 :         col_update_detail = view_col_is_auto_updatable(rtr, tle);
    2905                 :             : 
    2906         [ +  + ]:        7678 :         if (col_update_detail == NULL)
    2907                 :             :         {
    2908                 :             :             /* The column is updatable */
    2909         [ +  + ]:        6394 :             if (updatable_cols != NULL)
    2910                 :        1424 :                 *updatable_cols = bms_add_member(*updatable_cols, col);
    2911                 :             :         }
    2912         [ +  + ]:        1284 :         else if (bms_is_member(col, required_cols))
    2913                 :             :         {
    2914                 :             :             /* The required column is not updatable */
    2915         [ +  - ]:          88 :             if (non_updatable_col != NULL)
    2916                 :          88 :                 *non_updatable_col = tle->resname;
    2917                 :          88 :             return col_update_detail;
    2918                 :             :         }
    2919                 :             :     }
    2920                 :             : 
    2921                 :        2682 :     return NULL;                /* all the required view columns are updatable */
    2922                 :             : }
    2923                 :             : 
    2924                 :             : 
    2925                 :             : /*
    2926                 :             :  * relation_is_updatable - determine which update events the specified
    2927                 :             :  * relation supports.
    2928                 :             :  *
    2929                 :             :  * Note that views may contain a mix of updatable and non-updatable columns.
    2930                 :             :  * For a view to support INSERT/UPDATE it must have at least one updatable
    2931                 :             :  * column, but there is no such restriction for DELETE. If include_cols is
    2932                 :             :  * non-NULL, then only the specified columns are considered when testing for
    2933                 :             :  * updatability.
    2934                 :             :  *
    2935                 :             :  * Unlike the preceding functions, this does recurse to look at a view's
    2936                 :             :  * base relations, so it needs to detect recursion.  To do that, we pass
    2937                 :             :  * a list of currently-considered outer relations.  External callers need
    2938                 :             :  * only pass NIL.
    2939                 :             :  *
    2940                 :             :  * This is used for the information_schema views, which have separate concepts
    2941                 :             :  * of "updatable" and "trigger updatable".  A relation is "updatable" if it
    2942                 :             :  * can be updated without the need for triggers (either because it has a
    2943                 :             :  * suitable RULE, or because it is simple enough to be automatically updated).
    2944                 :             :  * A relation is "trigger updatable" if it has a suitable INSTEAD OF trigger.
    2945                 :             :  * The SQL standard regards this as not necessarily updatable, presumably
    2946                 :             :  * because there is no way of knowing what the trigger will actually do.
    2947                 :             :  * The information_schema views therefore call this function with
    2948                 :             :  * include_triggers = false.  However, other callers might only care whether
    2949                 :             :  * data-modifying SQL will work, so they can pass include_triggers = true
    2950                 :             :  * to have trigger updatability included in the result.
    2951                 :             :  *
    2952                 :             :  * The return value is a bitmask of rule event numbers indicating which of
    2953                 :             :  * the INSERT, UPDATE and DELETE operations are supported.  (We do it this way
    2954                 :             :  * so that we can test for UPDATE plus DELETE support in a single call.)
    2955                 :             :  */
    2956                 :             : int
    2957                 :        1408 : relation_is_updatable(Oid reloid,
    2958                 :             :                       List *outer_reloids,
    2959                 :             :                       bool include_triggers,
    2960                 :             :                       Bitmapset *include_cols)
    2961                 :             : {
    2962                 :        1408 :     int         events = 0;
    2963                 :             :     Relation    rel;
    2964                 :             :     RuleLock   *rulelocks;
    2965                 :             : 
    2966                 :             : #define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
    2967                 :             : 
    2968                 :             :     /* Since this function recurses, it could be driven to stack overflow */
    2969                 :        1408 :     check_stack_depth();
    2970                 :             : 
    2971                 :        1408 :     rel = try_relation_open(reloid, AccessShareLock);
    2972                 :             : 
    2973                 :             :     /*
    2974                 :             :      * If the relation doesn't exist, return zero rather than throwing an
    2975                 :             :      * error.  This is helpful since scanning an information_schema view under
    2976                 :             :      * MVCC rules can result in referencing rels that have actually been
    2977                 :             :      * deleted already.
    2978                 :             :      */
    2979         [ -  + ]:        1408 :     if (rel == NULL)
    2980                 :           0 :         return 0;
    2981                 :             : 
    2982                 :             :     /* If we detect a recursive view, report that it is not updatable */
    2983         [ -  + ]:        1408 :     if (list_member_oid(outer_reloids, RelationGetRelid(rel)))
    2984                 :             :     {
    2985                 :           0 :         relation_close(rel, AccessShareLock);
    2986                 :           0 :         return 0;
    2987                 :             :     }
    2988                 :             : 
    2989                 :             :     /* If the relation is a table, it is always updatable */
    2990         [ +  - ]:        1408 :     if (rel->rd_rel->relkind == RELKIND_RELATION ||
    2991         [ +  + ]:        1408 :         rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
    2992                 :             :     {
    2993                 :          12 :         relation_close(rel, AccessShareLock);
    2994                 :          12 :         return ALL_EVENTS;
    2995                 :             :     }
    2996                 :             : 
    2997                 :             :     /* Look for unconditional DO INSTEAD rules, and note supported events */
    2998                 :        1396 :     rulelocks = rel->rd_rules;
    2999         [ +  - ]:        1396 :     if (rulelocks != NULL)
    3000                 :             :     {
    3001                 :             :         int         i;
    3002                 :             : 
    3003         [ +  + ]:        3040 :         for (i = 0; i < rulelocks->numLocks; i++)
    3004                 :             :         {
    3005         [ +  + ]:        1644 :             if (rulelocks->rules[i]->isInstead &&
    3006         [ +  - ]:        1636 :                 rulelocks->rules[i]->qual == NULL)
    3007                 :             :             {
    3008                 :        1636 :                 events |= ((1 << rulelocks->rules[i]->event) & ALL_EVENTS);
    3009                 :             :             }
    3010                 :             :         }
    3011                 :             : 
    3012                 :             :         /* If we have rules for all events, we're done */
    3013         [ +  + ]:        1396 :         if (events == ALL_EVENTS)
    3014                 :             :         {
    3015                 :          40 :             relation_close(rel, AccessShareLock);
    3016                 :          40 :             return events;
    3017                 :             :         }
    3018                 :             :     }
    3019                 :             : 
    3020                 :             :     /* Similarly look for INSTEAD OF triggers, if they are to be included */
    3021         [ -  + ]:        1356 :     if (include_triggers)
    3022                 :             :     {
    3023                 :           0 :         TriggerDesc *trigDesc = rel->trigdesc;
    3024                 :             : 
    3025         [ #  # ]:           0 :         if (trigDesc)
    3026                 :             :         {
    3027         [ #  # ]:           0 :             if (trigDesc->trig_insert_instead_row)
    3028                 :           0 :                 events |= (1 << CMD_INSERT);
    3029         [ #  # ]:           0 :             if (trigDesc->trig_update_instead_row)
    3030                 :           0 :                 events |= (1 << CMD_UPDATE);
    3031         [ #  # ]:           0 :             if (trigDesc->trig_delete_instead_row)
    3032                 :           0 :                 events |= (1 << CMD_DELETE);
    3033                 :             : 
    3034                 :             :             /* If we have triggers for all events, we're done */
    3035         [ #  # ]:           0 :             if (events == ALL_EVENTS)
    3036                 :             :             {
    3037                 :           0 :                 relation_close(rel, AccessShareLock);
    3038                 :           0 :                 return events;
    3039                 :             :             }
    3040                 :             :         }
    3041                 :             :     }
    3042                 :             : 
    3043                 :             :     /* If this is a foreign table, check which update events it supports */
    3044         [ -  + ]:        1356 :     if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
    3045                 :             :     {
    3046                 :           0 :         FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);
    3047                 :             : 
    3048         [ #  # ]:           0 :         if (fdwroutine->IsForeignRelUpdatable != NULL)
    3049                 :           0 :             events |= fdwroutine->IsForeignRelUpdatable(rel);
    3050                 :             :         else
    3051                 :             :         {
    3052                 :             :             /* Assume presence of executor functions is sufficient */
    3053         [ #  # ]:           0 :             if (fdwroutine->ExecForeignInsert != NULL)
    3054                 :           0 :                 events |= (1 << CMD_INSERT);
    3055         [ #  # ]:           0 :             if (fdwroutine->ExecForeignUpdate != NULL)
    3056                 :           0 :                 events |= (1 << CMD_UPDATE);
    3057         [ #  # ]:           0 :             if (fdwroutine->ExecForeignDelete != NULL)
    3058                 :           0 :                 events |= (1 << CMD_DELETE);
    3059                 :             :         }
    3060                 :             : 
    3061                 :           0 :         relation_close(rel, AccessShareLock);
    3062                 :           0 :         return events;
    3063                 :             :     }
    3064                 :             : 
    3065                 :             :     /* Check if this is an automatically updatable view */
    3066         [ +  - ]:        1356 :     if (rel->rd_rel->relkind == RELKIND_VIEW)
    3067                 :             :     {
    3068                 :        1356 :         Query      *viewquery = get_view_query(rel);
    3069                 :             : 
    3070         [ +  + ]:        1356 :         if (view_query_is_auto_updatable(viewquery, false) == NULL)
    3071                 :             :         {
    3072                 :             :             Bitmapset  *updatable_cols;
    3073                 :             :             int         auto_events;
    3074                 :             :             RangeTblRef *rtr;
    3075                 :             :             RangeTblEntry *base_rte;
    3076                 :             :             Oid         baseoid;
    3077                 :             : 
    3078                 :             :             /*
    3079                 :             :              * Determine which of the view's columns are updatable. If there
    3080                 :             :              * are none within the set of columns we are looking at, then the
    3081                 :             :              * view doesn't support INSERT/UPDATE, but it may still support
    3082                 :             :              * DELETE.
    3083                 :             :              */
    3084                 :         700 :             view_cols_are_auto_updatable(viewquery, NULL,
    3085                 :             :                                          &updatable_cols, NULL);
    3086                 :             : 
    3087         [ +  + ]:         700 :             if (include_cols != NULL)
    3088                 :         384 :                 updatable_cols = bms_int_members(updatable_cols, include_cols);
    3089                 :             : 
    3090         [ +  + ]:         700 :             if (bms_is_empty(updatable_cols))
    3091                 :         100 :                 auto_events = (1 << CMD_DELETE);  /* May support DELETE */
    3092                 :             :             else
    3093                 :         600 :                 auto_events = ALL_EVENTS;   /* May support all events */
    3094                 :             : 
    3095                 :             :             /*
    3096                 :             :              * The base relation must also support these update commands.
    3097                 :             :              * Tables are always updatable, but for any other kind of base
    3098                 :             :              * relation we must do a recursive check limited to the columns
    3099                 :             :              * referenced by the locally updatable columns in this view.
    3100                 :             :              */
    3101                 :         700 :             rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
    3102                 :         700 :             base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
    3103                 :             :             Assert(base_rte->rtekind == RTE_RELATION);
    3104                 :             : 
    3105         [ +  + ]:         700 :             if (base_rte->relkind != RELKIND_RELATION &&
    3106         [ +  + ]:         388 :                 base_rte->relkind != RELKIND_PARTITIONED_TABLE)
    3107                 :             :             {
    3108                 :         368 :                 baseoid = base_rte->relid;
    3109                 :         368 :                 outer_reloids = lappend_oid(outer_reloids,
    3110                 :             :                                             RelationGetRelid(rel));
    3111                 :         368 :                 include_cols = adjust_view_column_set(updatable_cols,
    3112                 :             :                                                       viewquery->targetList);
    3113                 :         368 :                 auto_events &= relation_is_updatable(baseoid,
    3114                 :             :                                                      outer_reloids,
    3115                 :             :                                                      include_triggers,
    3116                 :             :                                                      include_cols);
    3117                 :         368 :                 outer_reloids = list_delete_last(outer_reloids);
    3118                 :             :             }
    3119                 :         700 :             events |= auto_events;
    3120                 :             :         }
    3121                 :             :     }
    3122                 :             : 
    3123                 :             :     /* If we reach here, the relation may support some update commands */
    3124                 :        1356 :     relation_close(rel, AccessShareLock);
    3125                 :        1356 :     return events;
    3126                 :             : }
    3127                 :             : 
    3128                 :             : 
    3129                 :             : /*
    3130                 :             :  * adjust_view_column_set - map a set of column numbers according to targetlist
    3131                 :             :  *
    3132                 :             :  * This is used with simply-updatable views to map column-permissions sets for
    3133                 :             :  * the view columns onto the matching columns in the underlying base relation.
    3134                 :             :  * Relevant entries in the targetlist must be plain Vars of the underlying
    3135                 :             :  * relation (as per the checks above in view_query_is_auto_updatable).
    3136                 :             :  */
    3137                 :             : static Bitmapset *
    3138                 :        4812 : adjust_view_column_set(Bitmapset *cols, List *targetlist)
    3139                 :             : {
    3140                 :        4812 :     Bitmapset  *result = NULL;
    3141                 :             :     int         col;
    3142                 :             : 
    3143                 :        4812 :     col = -1;
    3144         [ +  + ]:        8348 :     while ((col = bms_next_member(cols, col)) >= 0)
    3145                 :             :     {
    3146                 :             :         /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
    3147                 :        3536 :         AttrNumber  attno = col + FirstLowInvalidHeapAttributeNumber;
    3148                 :             : 
    3149         [ -  + ]:        3536 :         if (attno == InvalidAttrNumber)
    3150                 :             :         {
    3151                 :             :             /*
    3152                 :             :              * There's a whole-row reference to the view.  For permissions
    3153                 :             :              * purposes, treat it as a reference to each column available from
    3154                 :             :              * the view.  (We should *not* convert this to a whole-row
    3155                 :             :              * reference to the base relation, since the view may not touch
    3156                 :             :              * all columns of the base relation.)
    3157                 :             :              */
    3158                 :             :             ListCell   *lc;
    3159                 :             : 
    3160   [ #  #  #  #  :           0 :             foreach(lc, targetlist)
                   #  # ]
    3161                 :             :             {
    3162                 :           0 :                 TargetEntry *tle = lfirst_node(TargetEntry, lc);
    3163                 :             :                 Var        *var;
    3164                 :             : 
    3165         [ #  # ]:           0 :                 if (tle->resjunk)
    3166                 :           0 :                     continue;
    3167                 :           0 :                 var = castNode(Var, tle->expr);
    3168                 :           0 :                 result = bms_add_member(result,
    3169                 :           0 :                                         var->varattno - FirstLowInvalidHeapAttributeNumber);
    3170                 :             :             }
    3171                 :             :         }
    3172                 :             :         else
    3173                 :             :         {
    3174                 :             :             /*
    3175                 :             :              * Views do not have system columns, so we do not expect to see
    3176                 :             :              * any other system attnos here.  If we do find one, the error
    3177                 :             :              * case will apply.
    3178                 :             :              */
    3179                 :        3536 :             TargetEntry *tle = get_tle_by_resno(targetlist, attno);
    3180                 :             : 
    3181   [ +  -  +  -  :        3536 :             if (tle != NULL && !tle->resjunk && IsA(tle->expr, Var))
                   +  - ]
    3182                 :        3536 :             {
    3183                 :        3536 :                 Var        *var = (Var *) tle->expr;
    3184                 :             : 
    3185                 :        3536 :                 result = bms_add_member(result,
    3186                 :        3536 :                                         var->varattno - FirstLowInvalidHeapAttributeNumber);
    3187                 :             :             }
    3188                 :             :             else
    3189         [ #  # ]:           0 :                 elog(ERROR, "attribute number %d not found in view targetlist",
    3190                 :             :                      attno);
    3191                 :             :         }
    3192                 :             :     }
    3193                 :             : 
    3194                 :        4812 :     return result;
    3195                 :             : }
    3196                 :             : 
    3197                 :             : 
    3198                 :             : /*
    3199                 :             :  * error_view_not_updatable -
    3200                 :             :  *    Report an error due to an attempt to update a non-updatable view.
    3201                 :             :  *
    3202                 :             :  * Generally this is expected to be called from the rewriter, with suitable
    3203                 :             :  * error detail explaining why the view is not updatable.  Note, however, that
    3204                 :             :  * the executor also performs a just-in-case check that the target view is
    3205                 :             :  * updatable.  That check is expected to never fail, but if it does, it will
    3206                 :             :  * call this function with NULL error detail --- see CheckValidResultRel().
    3207                 :             :  *
    3208                 :             :  * Note: for MERGE, at least one of the actions in mergeActionList is expected
    3209                 :             :  * to lack a suitable INSTEAD OF trigger --- see view_has_instead_trigger().
    3210                 :             :  */
    3211                 :             : void
    3212                 :         104 : error_view_not_updatable(Relation view,
    3213                 :             :                          CmdType command,
    3214                 :             :                          List *mergeActionList,
    3215                 :             :                          const char *detail)
    3216                 :             : {
    3217                 :         104 :     TriggerDesc *trigDesc = view->trigdesc;
    3218                 :             : 
    3219   [ +  +  +  +  :         104 :     switch (command)
                      - ]
    3220                 :             :     {
    3221                 :          16 :         case CMD_INSERT:
    3222   [ +  -  +  - ]:          16 :             ereport(ERROR,
    3223                 :             :                     errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3224                 :             :                     errmsg("cannot insert into view \"%s\"",
    3225                 :             :                            RelationGetRelationName(view)),
    3226                 :             :                     detail ? errdetail_internal("%s", _(detail)) : 0,
    3227                 :             :                     errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule."));
    3228                 :             :             break;
    3229                 :          36 :         case CMD_UPDATE:
    3230   [ +  -  +  - ]:          36 :             ereport(ERROR,
    3231                 :             :                     errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3232                 :             :                     errmsg("cannot update view \"%s\"",
    3233                 :             :                            RelationGetRelationName(view)),
    3234                 :             :                     detail ? errdetail_internal("%s", _(detail)) : 0,
    3235                 :             :                     errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule."));
    3236                 :             :             break;
    3237                 :          32 :         case CMD_DELETE:
    3238   [ +  -  +  - ]:          32 :             ereport(ERROR,
    3239                 :             :                     errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3240                 :             :                     errmsg("cannot delete from view \"%s\"",
    3241                 :             :                            RelationGetRelationName(view)),
    3242                 :             :                     detail ? errdetail_internal("%s", _(detail)) : 0,
    3243                 :             :                     errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule."));
    3244                 :             :             break;
    3245                 :          20 :         case CMD_MERGE:
    3246                 :             : 
    3247                 :             :             /*
    3248                 :             :              * Note that the error hints here differ from above, since MERGE
    3249                 :             :              * doesn't support rules.
    3250                 :             :              */
    3251   [ +  -  +  -  :          24 :             foreach_node(MergeAction, action, mergeActionList)
                   +  - ]
    3252                 :             :             {
    3253   [ +  +  +  -  :          24 :                 switch (action->commandType)
                      - ]
    3254                 :             :                 {
    3255                 :           8 :                     case CMD_INSERT:
    3256   [ +  +  +  - ]:           8 :                         if (!trigDesc || !trigDesc->trig_insert_instead_row)
    3257   [ +  -  +  - ]:           8 :                             ereport(ERROR,
    3258                 :             :                                     errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3259                 :             :                                     errmsg("cannot insert into view \"%s\"",
    3260                 :             :                                            RelationGetRelationName(view)),
    3261                 :             :                                     detail ? errdetail_internal("%s", _(detail)) : 0,
    3262                 :             :                                     errhint("To enable inserting into the view using MERGE, provide an INSTEAD OF INSERT trigger."));
    3263                 :           0 :                         break;
    3264                 :           8 :                     case CMD_UPDATE:
    3265   [ +  +  -  + ]:           8 :                         if (!trigDesc || !trigDesc->trig_update_instead_row)
    3266   [ +  -  +  - ]:           4 :                             ereport(ERROR,
    3267                 :             :                                     errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3268                 :             :                                     errmsg("cannot update view \"%s\"",
    3269                 :             :                                            RelationGetRelationName(view)),
    3270                 :             :                                     detail ? errdetail_internal("%s", _(detail)) : 0,
    3271                 :             :                                     errhint("To enable updating the view using MERGE, provide an INSTEAD OF UPDATE trigger."));
    3272                 :           4 :                         break;
    3273                 :           8 :                     case CMD_DELETE:
    3274   [ +  +  +  - ]:           8 :                         if (!trigDesc || !trigDesc->trig_delete_instead_row)
    3275   [ +  -  +  - ]:           8 :                             ereport(ERROR,
    3276                 :             :                                     errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3277                 :             :                                     errmsg("cannot delete from view \"%s\"",
    3278                 :             :                                            RelationGetRelationName(view)),
    3279                 :             :                                     detail ? errdetail_internal("%s", _(detail)) : 0,
    3280                 :             :                                     errhint("To enable deleting from the view using MERGE, provide an INSTEAD OF DELETE trigger."));
    3281                 :           0 :                         break;
    3282                 :           0 :                     case CMD_NOTHING:
    3283                 :           0 :                         break;
    3284                 :           0 :                     default:
    3285         [ #  # ]:           0 :                         elog(ERROR, "unrecognized commandType: %d", action->commandType);
    3286                 :             :                         break;
    3287                 :             :                 }
    3288                 :             :             }
    3289                 :           0 :             break;
    3290                 :           0 :         default:
    3291         [ #  # ]:           0 :             elog(ERROR, "unrecognized CmdType: %d", (int) command);
    3292                 :             :             break;
    3293                 :             :     }
    3294                 :           0 : }
    3295                 :             : 
    3296                 :             : 
    3297                 :             : /*
    3298                 :             :  * rewriteTargetView -
    3299                 :             :  *    Attempt to rewrite a query where the target relation is a view, so that
    3300                 :             :  *    the view's base relation becomes the target relation.
    3301                 :             :  *
    3302                 :             :  * Note that the base relation here may itself be a view, which may or may not
    3303                 :             :  * have INSTEAD OF triggers or rules to handle the update.  That is handled by
    3304                 :             :  * the recursion in RewriteQuery.
    3305                 :             :  */
    3306                 :             : static Query *
    3307                 :        2410 : rewriteTargetView(Query *parsetree, Relation view)
    3308                 :             : {
    3309                 :             :     Query      *viewquery;
    3310                 :             :     bool        insert_or_update;
    3311                 :             :     const char *auto_update_detail;
    3312                 :             :     RangeTblRef *rtr;
    3313                 :             :     int         base_rt_index;
    3314                 :             :     int         new_rt_index;
    3315                 :             :     RangeTblEntry *base_rte;
    3316                 :             :     RangeTblEntry *view_rte;
    3317                 :             :     RangeTblEntry *new_rte;
    3318                 :             :     RTEPermissionInfo *base_perminfo;
    3319                 :             :     RTEPermissionInfo *view_perminfo;
    3320                 :             :     RTEPermissionInfo *new_perminfo;
    3321                 :             :     Relation    base_rel;
    3322                 :             :     List       *view_targetlist;
    3323                 :             :     ListCell   *lc;
    3324                 :             : 
    3325                 :             :     /*
    3326                 :             :      * Get the Query from the view's ON SELECT rule.  We're going to munge the
    3327                 :             :      * Query to change the view's base relation into the target relation,
    3328                 :             :      * along with various other changes along the way, so we need to make a
    3329                 :             :      * copy of it (get_view_query() returns a pointer into the relcache, so we
    3330                 :             :      * have to treat it as read-only).
    3331                 :             :      */
    3332                 :        2410 :     viewquery = copyObject(get_view_query(view));
    3333                 :             : 
    3334                 :             :     /* Locate RTE and perminfo describing the view in the outer query */
    3335                 :        2410 :     view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
    3336                 :        2410 :     view_perminfo = getRTEPermissionInfo(parsetree->rteperminfos, view_rte);
    3337                 :             : 
    3338                 :             :     /*
    3339                 :             :      * Are we doing INSERT/UPDATE, or MERGE containing INSERT/UPDATE?  If so,
    3340                 :             :      * various additional checks on the view columns need to be applied, and
    3341                 :             :      * any view CHECK OPTIONs need to be enforced.
    3342                 :             :      */
    3343                 :        2410 :     insert_or_update =
    3344         [ +  + ]:        3960 :         (parsetree->commandType == CMD_INSERT ||
    3345         [ +  + ]:        1550 :          parsetree->commandType == CMD_UPDATE);
    3346                 :             : 
    3347         [ +  + ]:        2410 :     if (parsetree->commandType == CMD_MERGE)
    3348                 :             :     {
    3349   [ +  -  +  +  :        1284 :         foreach_node(MergeAction, action, parsetree->mergeActionList)
                   +  + ]
    3350                 :             :         {
    3351         [ +  + ]:         628 :             if (action->commandType == CMD_INSERT ||
    3352         [ +  + ]:         556 :                 action->commandType == CMD_UPDATE)
    3353                 :             :             {
    3354                 :         536 :                 insert_or_update = true;
    3355                 :         536 :                 break;
    3356                 :             :             }
    3357                 :             :         }
    3358                 :             :     }
    3359                 :             : 
    3360                 :             :     /* Check if the expansion of non-system views are restricted */
    3361   [ +  +  +  -  :        2410 :     if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_VIEW) != 0 &&
                   +  + ]
    3362                 :             :                  RelationGetRelid(view) >= FirstNormalObjectId))
    3363         [ +  - ]:           4 :         ereport(ERROR,
    3364                 :             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3365                 :             :                  errmsg("access to non-system view \"%s\" is restricted",
    3366                 :             :                         RelationGetRelationName(view))));
    3367                 :             : 
    3368                 :             :     /*
    3369                 :             :      * The view must be updatable, else fail.
    3370                 :             :      *
    3371                 :             :      * If we are doing INSERT/UPDATE (or MERGE containing INSERT/UPDATE), we
    3372                 :             :      * also check that there is at least one updatable column.
    3373                 :             :      */
    3374                 :             :     auto_update_detail =
    3375                 :        2406 :         view_query_is_auto_updatable(viewquery, insert_or_update);
    3376                 :             : 
    3377         [ +  + ]:        2406 :     if (auto_update_detail)
    3378                 :          92 :         error_view_not_updatable(view,
    3379                 :             :                                  parsetree->commandType,
    3380                 :             :                                  parsetree->mergeActionList,
    3381                 :             :                                  auto_update_detail);
    3382                 :             : 
    3383                 :             :     /*
    3384                 :             :      * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE) the modified
    3385                 :             :      * columns must all be updatable.
    3386                 :             :      */
    3387         [ +  + ]:        2314 :     if (insert_or_update)
    3388                 :             :     {
    3389                 :             :         Bitmapset  *modified_cols;
    3390                 :             :         char       *non_updatable_col;
    3391                 :             : 
    3392                 :             :         /*
    3393                 :             :          * Compute the set of modified columns as those listed in the result
    3394                 :             :          * RTE's insertedCols and/or updatedCols sets plus those that are
    3395                 :             :          * targets of the query's targetlist(s).  We must consider the query's
    3396                 :             :          * targetlist because rewriteTargetListIU may have added additional
    3397                 :             :          * targetlist entries for view defaults, and these must also be
    3398                 :             :          * updatable.  But rewriteTargetListIU can also remove entries if they
    3399                 :             :          * are DEFAULT markers and the column's default is NULL, so
    3400                 :             :          * considering only the targetlist would also be wrong.
    3401                 :             :          */
    3402                 :        2046 :         modified_cols = bms_union(view_perminfo->insertedCols,
    3403                 :        2046 :                                   view_perminfo->updatedCols);
    3404                 :             : 
    3405   [ +  +  +  +  :        4350 :         foreach(lc, parsetree->targetList)
                   +  + ]
    3406                 :             :         {
    3407                 :        2304 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
    3408                 :             : 
    3409         [ +  - ]:        2304 :             if (!tle->resjunk)
    3410                 :        2304 :                 modified_cols = bms_add_member(modified_cols,
    3411                 :        2304 :                                                tle->resno - FirstLowInvalidHeapAttributeNumber);
    3412                 :             :         }
    3413                 :             : 
    3414         [ +  + ]:        2046 :         if (parsetree->onConflict)
    3415                 :             :         {
    3416   [ +  +  +  +  :         280 :             foreach(lc, parsetree->onConflict->onConflictSet)
                   +  + ]
    3417                 :             :             {
    3418                 :         108 :                 TargetEntry *tle = (TargetEntry *) lfirst(lc);
    3419                 :             : 
    3420         [ +  - ]:         108 :                 if (!tle->resjunk)
    3421                 :         108 :                     modified_cols = bms_add_member(modified_cols,
    3422                 :         108 :                                                    tle->resno - FirstLowInvalidHeapAttributeNumber);
    3423                 :             :             }
    3424                 :             :         }
    3425                 :             : 
    3426   [ +  +  +  +  :        4744 :         foreach_node(MergeAction, action, parsetree->mergeActionList)
                   +  + ]
    3427                 :             :         {
    3428         [ +  + ]:         652 :             if (action->commandType == CMD_INSERT ||
    3429         [ +  + ]:         512 :                 action->commandType == CMD_UPDATE)
    3430                 :             :             {
    3431   [ +  -  +  +  :        1964 :                 foreach_node(TargetEntry, tle, action->targetList)
                   +  + ]
    3432                 :             :                 {
    3433         [ +  - ]:         756 :                     if (!tle->resjunk)
    3434                 :         756 :                         modified_cols = bms_add_member(modified_cols,
    3435                 :         756 :                                                        tle->resno - FirstLowInvalidHeapAttributeNumber);
    3436                 :             :                 }
    3437                 :             :             }
    3438                 :             :         }
    3439                 :             : 
    3440                 :        2046 :         auto_update_detail = view_cols_are_auto_updatable(viewquery,
    3441                 :             :                                                           modified_cols,
    3442                 :             :                                                           NULL,
    3443                 :             :                                                           &non_updatable_col);
    3444         [ +  + ]:        2046 :         if (auto_update_detail)
    3445                 :             :         {
    3446                 :             :             /*
    3447                 :             :              * This is a different error, caused by an attempt to update a
    3448                 :             :              * non-updatable column in an otherwise updatable view.
    3449                 :             :              */
    3450   [ +  +  +  - ]:          84 :             switch (parsetree->commandType)
    3451                 :             :             {
    3452                 :          48 :                 case CMD_INSERT:
    3453         [ +  - ]:          48 :                     ereport(ERROR,
    3454                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3455                 :             :                              errmsg("cannot insert into column \"%s\" of view \"%s\"",
    3456                 :             :                                     non_updatable_col,
    3457                 :             :                                     RelationGetRelationName(view)),
    3458                 :             :                              errdetail_internal("%s", _(auto_update_detail))));
    3459                 :             :                     break;
    3460                 :          32 :                 case CMD_UPDATE:
    3461         [ +  - ]:          32 :                     ereport(ERROR,
    3462                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3463                 :             :                              errmsg("cannot update column \"%s\" of view \"%s\"",
    3464                 :             :                                     non_updatable_col,
    3465                 :             :                                     RelationGetRelationName(view)),
    3466                 :             :                              errdetail_internal("%s", _(auto_update_detail))));
    3467                 :             :                     break;
    3468                 :           4 :                 case CMD_MERGE:
    3469         [ +  - ]:           4 :                     ereport(ERROR,
    3470                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3471                 :             :                              errmsg("cannot merge into column \"%s\" of view \"%s\"",
    3472                 :             :                                     non_updatable_col,
    3473                 :             :                                     RelationGetRelationName(view)),
    3474                 :             :                              errdetail_internal("%s", _(auto_update_detail))));
    3475                 :             :                     break;
    3476                 :           0 :                 default:
    3477         [ #  # ]:           0 :                     elog(ERROR, "unrecognized CmdType: %d",
    3478                 :             :                          (int) parsetree->commandType);
    3479                 :             :                     break;
    3480                 :             :             }
    3481                 :             :         }
    3482                 :             :     }
    3483                 :             : 
    3484                 :             :     /*
    3485                 :             :      * Similarly, make sure the FOR PORTION OF column is updateable. This is
    3486                 :             :      * not included in the columns tested above, and we have to test it even
    3487                 :             :      * for DELETEs.
    3488                 :             :      */
    3489         [ +  + ]:        2230 :     if (parsetree->forPortionOf)
    3490                 :             :     {
    3491                 :             :         AttrNumber  rangeAttno;
    3492                 :             :         Bitmapset  *fpo_cols;
    3493                 :             :         char       *non_updatable_col;
    3494                 :             :         const char *fpo_update_detail;
    3495                 :             : 
    3496                 :          24 :         rangeAttno = parsetree->forPortionOf->rangeVar->varattno;
    3497                 :          24 :         fpo_cols = bms_make_singleton(rangeAttno - FirstLowInvalidHeapAttributeNumber);
    3498                 :             : 
    3499                 :          24 :         fpo_update_detail = view_cols_are_auto_updatable(viewquery,
    3500                 :             :                                                          fpo_cols,
    3501                 :             :                                                          NULL,
    3502                 :             :                                                          &non_updatable_col);
    3503         [ +  + ]:          24 :         if (fpo_update_detail)
    3504                 :             :         {
    3505      [ -  +  - ]:           4 :             switch (parsetree->commandType)
    3506                 :             :             {
    3507                 :           0 :                 case CMD_UPDATE:
    3508         [ #  # ]:           0 :                     ereport(ERROR,
    3509                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3510                 :             :                              errmsg("cannot update column \"%s\" of view \"%s\"",
    3511                 :             :                                     non_updatable_col,
    3512                 :             :                                     RelationGetRelationName(view)),
    3513                 :             :                              errdetail_internal("%s", _(fpo_update_detail))));
    3514                 :             :                     break;
    3515                 :           4 :                 case CMD_DELETE:
    3516         [ +  - ]:           4 :                     ereport(ERROR,
    3517                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3518                 :             :                              errmsg("cannot delete from view \"%s\" using FOR PORTION OF \"%s\"",
    3519                 :             :                                     RelationGetRelationName(view),
    3520                 :             :                                     non_updatable_col),
    3521                 :             :                              errdetail_internal("%s", _(fpo_update_detail))));
    3522                 :             :                     break;
    3523                 :           0 :                 default:
    3524         [ #  # ]:           0 :                     elog(ERROR, "unrecognized CmdType: %d",
    3525                 :             :                          (int) parsetree->commandType);
    3526                 :             :                     break;
    3527                 :             :             }
    3528                 :             :         }
    3529                 :             :     }
    3530                 :             : 
    3531                 :             :     /*
    3532                 :             :      * For MERGE, there must not be any INSTEAD OF triggers on an otherwise
    3533                 :             :      * updatable view.  The caller already checked that there isn't a full set
    3534                 :             :      * of INSTEAD OF triggers, so this is to guard against having a partial
    3535                 :             :      * set (mixing auto-update and trigger-update actions in a single command
    3536                 :             :      * isn't supported).
    3537                 :             :      */
    3538         [ +  + ]:        2226 :     if (parsetree->commandType == CMD_MERGE)
    3539                 :             :     {
    3540   [ +  -  +  +  :        1832 :         foreach_node(MergeAction, action, parsetree->mergeActionList)
                   +  + ]
    3541                 :             :         {
    3542   [ +  -  +  + ]:        1392 :             if (action->commandType != CMD_NOTHING &&
    3543                 :         696 :                 view_has_instead_trigger(view, action->commandType, NIL))
    3544         [ +  - ]:           4 :                 ereport(ERROR,
    3545                 :             :                         errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3546                 :             :                         errmsg("cannot merge into view \"%s\"",
    3547                 :             :                                RelationGetRelationName(view)),
    3548                 :             :                         errdetail("MERGE is not supported for views with INSTEAD OF triggers for some actions but not all."),
    3549                 :             :                         errhint("To enable merging into the view, either provide a full set of INSTEAD OF triggers or drop the existing INSTEAD OF triggers."));
    3550                 :             :         }
    3551                 :             :     }
    3552                 :             : 
    3553                 :             :     /*
    3554                 :             :      * If we get here, view_query_is_auto_updatable() has verified that the
    3555                 :             :      * view contains a single base relation.
    3556                 :             :      */
    3557                 :             :     Assert(list_length(viewquery->jointree->fromlist) == 1);
    3558                 :        2222 :     rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
    3559                 :             : 
    3560                 :        2222 :     base_rt_index = rtr->rtindex;
    3561                 :        2222 :     base_rte = rt_fetch(base_rt_index, viewquery->rtable);
    3562                 :             :     Assert(base_rte->rtekind == RTE_RELATION);
    3563                 :        2222 :     base_perminfo = getRTEPermissionInfo(viewquery->rteperminfos, base_rte);
    3564                 :             : 
    3565                 :             :     /*
    3566                 :             :      * Up to now, the base relation hasn't been touched at all in our query.
    3567                 :             :      * We need to acquire lock on it before we try to do anything with it.
    3568                 :             :      * (The subsequent recursive call of RewriteQuery will suppose that we
    3569                 :             :      * already have the right lock!)  Since it will become the query target
    3570                 :             :      * relation, RowExclusiveLock is always the right thing.
    3571                 :             :      */
    3572                 :        2222 :     base_rel = relation_open(base_rte->relid, RowExclusiveLock);
    3573                 :             : 
    3574                 :             :     /*
    3575                 :             :      * While we have the relation open, update the RTE's relkind, just in case
    3576                 :             :      * it changed since this view was made (cf. AcquireRewriteLocks).
    3577                 :             :      */
    3578                 :        2222 :     base_rte->relkind = base_rel->rd_rel->relkind;
    3579                 :             : 
    3580                 :             :     /*
    3581                 :             :      * If the view query contains any sublink subqueries then we need to also
    3582                 :             :      * acquire locks on any relations they refer to.  We know that there won't
    3583                 :             :      * be any subqueries in the range table or CTEs, so we can skip those, as
    3584                 :             :      * in AcquireRewriteLocks.
    3585                 :             :      */
    3586         [ +  + ]:        2222 :     if (viewquery->hasSubLinks)
    3587                 :             :     {
    3588                 :             :         acquireLocksOnSubLinks_context context;
    3589                 :             : 
    3590                 :         172 :         context.for_execute = true;
    3591                 :         172 :         query_tree_walker(viewquery, acquireLocksOnSubLinks, &context,
    3592                 :             :                           QTW_IGNORE_RC_SUBQUERIES);
    3593                 :             :     }
    3594                 :             : 
    3595                 :             :     /*
    3596                 :             :      * Create a new target RTE describing the base relation, and add it to the
    3597                 :             :      * outer query's rangetable.  (What's happening in the next few steps is
    3598                 :             :      * very much like what the planner would do to "pull up" the view into the
    3599                 :             :      * outer query.  Perhaps someday we should refactor things enough so that
    3600                 :             :      * we can share code with the planner.)
    3601                 :             :      *
    3602                 :             :      * Be sure to set rellockmode to the correct thing for the target table.
    3603                 :             :      * Since we copied the whole viewquery above, we can just scribble on
    3604                 :             :      * base_rte instead of copying it.
    3605                 :             :      */
    3606                 :        2222 :     new_rte = base_rte;
    3607                 :        2222 :     new_rte->rellockmode = RowExclusiveLock;
    3608                 :             : 
    3609                 :        2222 :     parsetree->rtable = lappend(parsetree->rtable, new_rte);
    3610                 :        2222 :     new_rt_index = list_length(parsetree->rtable);
    3611                 :             : 
    3612                 :             :     /*
    3613                 :             :      * INSERTs never inherit.  For UPDATE/DELETE/MERGE, we use the view
    3614                 :             :      * query's inheritance flag for the base relation.
    3615                 :             :      */
    3616         [ +  + ]:        2222 :     if (parsetree->commandType == CMD_INSERT)
    3617                 :         796 :         new_rte->inh = false;
    3618                 :             : 
    3619                 :             :     /*
    3620                 :             :      * Adjust the view's targetlist Vars to reference the new target RTE, ie
    3621                 :             :      * make their varnos be new_rt_index instead of base_rt_index.  There can
    3622                 :             :      * be no Vars for other rels in the tlist, so this is sufficient to pull
    3623                 :             :      * up the tlist expressions for use in the outer query.  The tlist will
    3624                 :             :      * provide the replacement expressions used by ReplaceVarsFromTargetList
    3625                 :             :      * below.
    3626                 :             :      */
    3627                 :        2222 :     view_targetlist = viewquery->targetList;
    3628                 :             : 
    3629                 :        2222 :     ChangeVarNodes((Node *) view_targetlist,
    3630                 :             :                    base_rt_index,
    3631                 :             :                    new_rt_index,
    3632                 :             :                    0);
    3633                 :             : 
    3634                 :             :     /*
    3635                 :             :      * If the view has "security_invoker" set, mark the new target relation
    3636                 :             :      * for the permissions checks that we want to enforce against the query
    3637                 :             :      * caller. Otherwise we want to enforce them against the view owner.
    3638                 :             :      *
    3639                 :             :      * At the relation level, require the same INSERT/UPDATE/DELETE
    3640                 :             :      * permissions that the query caller needs against the view.  We drop the
    3641                 :             :      * ACL_SELECT bit that is presumably in new_perminfo->requiredPerms
    3642                 :             :      * initially.
    3643                 :             :      *
    3644                 :             :      * Note: the original view's RTEPermissionInfo remains in the query's
    3645                 :             :      * rteperminfos so that the executor still performs appropriate
    3646                 :             :      * permissions checks for the query caller's use of the view.
    3647                 :             :      *
    3648                 :             :      * Disregard the perminfo in viewquery->rteperminfos that the base_rte
    3649                 :             :      * would currently be pointing at, because we'd like it to point now to a
    3650                 :             :      * new one that will be filled below.  Must set perminfoindex to 0 to not
    3651                 :             :      * trip over the Assert in addRTEPermissionInfo().
    3652                 :             :      */
    3653                 :        2222 :     new_rte->perminfoindex = 0;
    3654                 :        2222 :     new_perminfo = addRTEPermissionInfo(&parsetree->rteperminfos, new_rte);
    3655   [ +  +  +  + ]:        2222 :     if (RelationHasSecurityInvoker(view))
    3656                 :         324 :         new_perminfo->checkAsUser = InvalidOid;
    3657                 :             :     else
    3658                 :        1898 :         new_perminfo->checkAsUser = view->rd_rel->relowner;
    3659                 :        2222 :     new_perminfo->requiredPerms = view_perminfo->requiredPerms;
    3660                 :             : 
    3661                 :             :     /*
    3662                 :             :      * Now for the per-column permissions bits.
    3663                 :             :      *
    3664                 :             :      * Initially, new_perminfo (base_perminfo) contains selectedCols
    3665                 :             :      * permission check bits for all base-rel columns referenced by the view,
    3666                 :             :      * but since the view is a SELECT query its insertedCols/updatedCols is
    3667                 :             :      * empty.  We set insertedCols and updatedCols to include all the columns
    3668                 :             :      * the outer query is trying to modify, adjusting the column numbers as
    3669                 :             :      * needed.  But we leave selectedCols as-is, so the view owner must have
    3670                 :             :      * read permission for all columns used in the view definition, even if
    3671                 :             :      * some of them are not read by the outer query.  We could try to limit
    3672                 :             :      * selectedCols to only columns used in the transformed query, but that
    3673                 :             :      * does not correspond to what happens in ordinary SELECT usage of a view:
    3674                 :             :      * all referenced columns must have read permission, even if optimization
    3675                 :             :      * finds that some of them can be discarded during query transformation.
    3676                 :             :      * The flattening we're doing here is an optional optimization, too.  (If
    3677                 :             :      * you are unpersuaded and want to change this, note that applying
    3678                 :             :      * adjust_view_column_set to view_perminfo->selectedCols is clearly *not*
    3679                 :             :      * the right answer, since that neglects base-rel columns used in the
    3680                 :             :      * view's WHERE quals.)
    3681                 :             :      *
    3682                 :             :      * This step needs the modified view targetlist, so we have to do things
    3683                 :             :      * in this order.
    3684                 :             :      */
    3685                 :             :     Assert(bms_is_empty(new_perminfo->insertedCols) &&
    3686                 :             :            bms_is_empty(new_perminfo->updatedCols));
    3687                 :             : 
    3688                 :        2222 :     new_perminfo->selectedCols = base_perminfo->selectedCols;
    3689                 :             : 
    3690                 :        2222 :     new_perminfo->insertedCols =
    3691                 :        2222 :         adjust_view_column_set(view_perminfo->insertedCols, view_targetlist);
    3692                 :             : 
    3693                 :        2222 :     new_perminfo->updatedCols =
    3694                 :        2222 :         adjust_view_column_set(view_perminfo->updatedCols, view_targetlist);
    3695                 :             : 
    3696                 :             :     /*
    3697                 :             :      * Move any security barrier quals from the view RTE onto the new target
    3698                 :             :      * RTE.  Any such quals should now apply to the new target RTE and will
    3699                 :             :      * not reference the original view RTE in the rewritten query.
    3700                 :             :      */
    3701                 :        2222 :     new_rte->securityQuals = view_rte->securityQuals;
    3702                 :        2222 :     view_rte->securityQuals = NIL;
    3703                 :             : 
    3704                 :             :     /*
    3705                 :             :      * Now update all Vars in the outer query that reference the view to
    3706                 :             :      * reference the appropriate column of the base relation instead.
    3707                 :             :      */
    3708                 :             :     parsetree = (Query *)
    3709                 :        2222 :         ReplaceVarsFromTargetList((Node *) parsetree,
    3710                 :             :                                   parsetree->resultRelation,
    3711                 :             :                                   0,
    3712                 :             :                                   view_rte,
    3713                 :             :                                   view_targetlist,
    3714                 :             :                                   new_rt_index,
    3715                 :             :                                   REPLACEVARS_REPORT_ERROR,
    3716                 :             :                                   0,
    3717                 :             :                                   NULL);
    3718                 :             : 
    3719                 :             :     /*
    3720                 :             :      * Update all other RTI references in the query that point to the view
    3721                 :             :      * (for example, parsetree->resultRelation itself) to point to the new
    3722                 :             :      * base relation instead.  Vars will not be affected since none of them
    3723                 :             :      * reference parsetree->resultRelation any longer.
    3724                 :             :      */
    3725                 :        2222 :     ChangeVarNodes((Node *) parsetree,
    3726                 :             :                    parsetree->resultRelation,
    3727                 :             :                    new_rt_index,
    3728                 :             :                    0);
    3729                 :             :     Assert(parsetree->resultRelation == new_rt_index);
    3730                 :             : 
    3731                 :             :     /*
    3732                 :             :      * For INSERT/UPDATE we must also update resnos in the targetlist to refer
    3733                 :             :      * to columns of the base relation, since those indicate the target
    3734                 :             :      * columns to be affected.  Similarly, for MERGE we must update the resnos
    3735                 :             :      * in the merge action targetlists of any INSERT/UPDATE actions.
    3736                 :             :      *
    3737                 :             :      * Note that this destroys the resno ordering of the targetlists, but that
    3738                 :             :      * will be fixed when we recurse through RewriteQuery, which will invoke
    3739                 :             :      * rewriteTargetListIU again on the updated targetlists.
    3740                 :             :      */
    3741         [ +  + ]:        2222 :     if (parsetree->commandType != CMD_DELETE)
    3742                 :             :     {
    3743   [ +  +  +  +  :        4162 :         foreach(lc, parsetree->targetList)
                   +  + ]
    3744                 :             :         {
    3745                 :        2148 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
    3746                 :             :             TargetEntry *view_tle;
    3747                 :             : 
    3748         [ -  + ]:        2148 :             if (tle->resjunk)
    3749                 :           0 :                 continue;
    3750                 :             : 
    3751                 :        2148 :             view_tle = get_tle_by_resno(view_targetlist, tle->resno);
    3752   [ +  -  +  -  :        2148 :             if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
                   +  - ]
    3753                 :        2148 :                 tle->resno = ((Var *) view_tle->expr)->varattno;
    3754                 :             :             else
    3755         [ #  # ]:           0 :                 elog(ERROR, "attribute number %d not found in view targetlist",
    3756                 :             :                      tle->resno);
    3757                 :             :         }
    3758                 :             : 
    3759   [ +  +  +  +  :        4720 :         foreach_node(MergeAction, action, parsetree->mergeActionList)
                   +  + ]
    3760                 :             :         {
    3761         [ +  + ]:         692 :             if (action->commandType == CMD_INSERT ||
    3762         [ +  + ]:         560 :                 action->commandType == CMD_UPDATE)
    3763                 :             :             {
    3764   [ +  -  +  +  :        1904 :                 foreach_node(TargetEntry, tle, action->targetList)
                   +  + ]
    3765                 :             :                 {
    3766                 :             :                     TargetEntry *view_tle;
    3767                 :             : 
    3768         [ -  + ]:         728 :                     if (tle->resjunk)
    3769                 :           0 :                         continue;
    3770                 :             : 
    3771                 :         728 :                     view_tle = get_tle_by_resno(view_targetlist, tle->resno);
    3772   [ +  -  +  -  :         728 :                     if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
                   +  - ]
    3773                 :         728 :                         tle->resno = ((Var *) view_tle->expr)->varattno;
    3774                 :             :                     else
    3775         [ #  # ]:           0 :                         elog(ERROR, "attribute number %d not found in view targetlist",
    3776                 :             :                              tle->resno);
    3777                 :             :                 }
    3778                 :             :             }
    3779                 :             :         }
    3780                 :             :     }
    3781                 :             : 
    3782                 :             :     /*
    3783                 :             :      * For INSERT .. ON CONFLICT .. DO SELECT/UPDATE, we must also update
    3784                 :             :      * assorted stuff in the onConflict data structure.
    3785                 :             :      */
    3786         [ +  + ]:        2222 :     if (parsetree->onConflict &&
    3787         [ +  + ]:         164 :         (parsetree->onConflict->action == ONCONFLICT_UPDATE ||
    3788         [ +  + ]:          64 :          parsetree->onConflict->action == ONCONFLICT_SELECT))
    3789                 :             :     {
    3790                 :             :         Index       old_exclRelIndex,
    3791                 :             :                     new_exclRelIndex;
    3792                 :             :         ParseNamespaceItem *new_exclNSItem;
    3793                 :             :         RangeTblEntry *new_exclRte;
    3794                 :             :         List       *tmp_tlist;
    3795                 :             : 
    3796                 :             :         /*
    3797                 :             :          * For ON CONFLICT DO UPDATE, update the resnos in the auxiliary
    3798                 :             :          * UPDATE targetlist to refer to columns of the base relation.
    3799                 :             :          */
    3800   [ +  +  +  +  :         248 :         foreach(lc, parsetree->onConflict->onConflictSet)
                   +  + ]
    3801                 :             :         {
    3802                 :         100 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
    3803                 :             :             TargetEntry *view_tle;
    3804                 :             : 
    3805         [ -  + ]:         100 :             if (tle->resjunk)
    3806                 :           0 :                 continue;
    3807                 :             : 
    3808                 :         100 :             view_tle = get_tle_by_resno(view_targetlist, tle->resno);
    3809   [ +  -  +  -  :         100 :             if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
                   +  - ]
    3810                 :         100 :                 tle->resno = ((Var *) view_tle->expr)->varattno;
    3811                 :             :             else
    3812         [ #  # ]:           0 :                 elog(ERROR, "attribute number %d not found in view targetlist",
    3813                 :             :                      tle->resno);
    3814                 :             :         }
    3815                 :             : 
    3816                 :             :         /*
    3817                 :             :          * Create a new RTE for the EXCLUDED pseudo-relation, using the
    3818                 :             :          * query's new base rel (which may well have a different column list
    3819                 :             :          * from the view, hence we need a new column alias list).  This should
    3820                 :             :          * match transformOnConflictClause.  In particular, note that the
    3821                 :             :          * relkind is set to composite to signal that we're not dealing with
    3822                 :             :          * an actual relation.
    3823                 :             :          */
    3824                 :         148 :         old_exclRelIndex = parsetree->onConflict->exclRelIndex;
    3825                 :             : 
    3826                 :         148 :         new_exclNSItem = addRangeTableEntryForRelation(make_parsestate(NULL),
    3827                 :             :                                                        base_rel,
    3828                 :             :                                                        RowExclusiveLock,
    3829                 :             :                                                        makeAlias("excluded", NIL),
    3830                 :             :                                                        false, false);
    3831                 :         148 :         new_exclRte = new_exclNSItem->p_rte;
    3832                 :         148 :         new_exclRte->relkind = RELKIND_COMPOSITE_TYPE;
    3833                 :             :         /* Ignore the RTEPermissionInfo that would've been added. */
    3834                 :         148 :         new_exclRte->perminfoindex = 0;
    3835                 :             : 
    3836                 :         148 :         parsetree->rtable = lappend(parsetree->rtable, new_exclRte);
    3837                 :         296 :         new_exclRelIndex = parsetree->onConflict->exclRelIndex =
    3838                 :         148 :             list_length(parsetree->rtable);
    3839                 :             : 
    3840                 :             :         /*
    3841                 :             :          * Replace the targetlist for the EXCLUDED pseudo-relation with a new
    3842                 :             :          * one, representing the columns from the new base relation.
    3843                 :             :          */
    3844                 :         296 :         parsetree->onConflict->exclRelTlist =
    3845                 :         148 :             BuildOnConflictExcludedTargetlist(base_rel, new_exclRelIndex);
    3846                 :             : 
    3847                 :             :         /*
    3848                 :             :          * Update all Vars in the ON CONFLICT clause that refer to the old
    3849                 :             :          * EXCLUDED pseudo-relation.  We want to use the column mappings
    3850                 :             :          * defined in the view targetlist, but we need the outputs to refer to
    3851                 :             :          * the new EXCLUDED pseudo-relation rather than the new target RTE.
    3852                 :             :          * Also notice that "EXCLUDED.*" will be expanded using the view's
    3853                 :             :          * rowtype, which seems correct.
    3854                 :             :          */
    3855                 :         148 :         tmp_tlist = copyObject(view_targetlist);
    3856                 :             : 
    3857                 :         148 :         ChangeVarNodes((Node *) tmp_tlist, new_rt_index,
    3858                 :             :                        new_exclRelIndex, 0);
    3859                 :             : 
    3860                 :         148 :         parsetree->onConflict = (OnConflictExpr *)
    3861                 :         148 :             ReplaceVarsFromTargetList((Node *) parsetree->onConflict,
    3862                 :             :                                       old_exclRelIndex,
    3863                 :             :                                       0,
    3864                 :             :                                       view_rte,
    3865                 :             :                                       tmp_tlist,
    3866                 :             :                                       new_rt_index,
    3867                 :             :                                       REPLACEVARS_REPORT_ERROR,
    3868                 :             :                                       0,
    3869                 :             :                                       &parsetree->hasSubLinks);
    3870                 :             :     }
    3871                 :             : 
    3872   [ +  +  +  + ]:        2222 :     if (parsetree->forPortionOf && parsetree->commandType == CMD_UPDATE)
    3873                 :             :     {
    3874                 :             :         /*
    3875                 :             :          * Like the INSERT/UPDATE code above, update the resnos in the
    3876                 :             :          * auxiliary UPDATE targetlist to refer to columns of the base
    3877                 :             :          * relation.
    3878                 :             :          */
    3879   [ +  -  +  +  :          24 :         foreach(lc, parsetree->forPortionOf->rangeTargetList)
                   +  + ]
    3880                 :             :         {
    3881                 :          12 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
    3882                 :             :             TargetEntry *view_tle;
    3883                 :             : 
    3884         [ -  + ]:          12 :             if (tle->resjunk)
    3885                 :           0 :                 continue;
    3886                 :             : 
    3887                 :          12 :             view_tle = get_tle_by_resno(view_targetlist, tle->resno);
    3888   [ +  -  +  -  :          12 :             if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
                   +  - ]
    3889                 :          12 :                 tle->resno = ((Var *) view_tle->expr)->varattno;
    3890                 :             :             else
    3891         [ #  # ]:           0 :                 elog(ERROR, "attribute number %d not found in view targetlist",
    3892                 :             :                      tle->resno);
    3893                 :             :         }
    3894                 :             :     }
    3895                 :             : 
    3896                 :             :     /*
    3897                 :             :      * For UPDATE/DELETE/MERGE, pull up any WHERE quals from the view.  We
    3898                 :             :      * know that any Vars in the quals must reference the one base relation,
    3899                 :             :      * so we need only adjust their varnos to reference the new target (just
    3900                 :             :      * the same as we did with the view targetlist).
    3901                 :             :      *
    3902                 :             :      * If it's a security-barrier view, its WHERE quals must be applied before
    3903                 :             :      * quals from the outer query, so we attach them to the RTE as security
    3904                 :             :      * barrier quals rather than adding them to the main WHERE clause.
    3905                 :             :      *
    3906                 :             :      * For INSERT, the view's quals can be ignored in the main query.
    3907                 :             :      */
    3908         [ +  + ]:        2222 :     if (parsetree->commandType != CMD_INSERT &&
    3909         [ +  + ]:        1426 :         viewquery->jointree->quals != NULL)
    3910                 :             :     {
    3911                 :         502 :         Node       *viewqual = (Node *) viewquery->jointree->quals;
    3912                 :             : 
    3913                 :             :         /*
    3914                 :             :          * Even though we copied viewquery already at the top of this
    3915                 :             :          * function, we must duplicate the viewqual again here, because we may
    3916                 :             :          * need to use the quals again below for a WithCheckOption clause.
    3917                 :             :          */
    3918                 :         502 :         viewqual = copyObject(viewqual);
    3919                 :             : 
    3920                 :         502 :         ChangeVarNodes(viewqual, base_rt_index, new_rt_index, 0);
    3921                 :             : 
    3922   [ +  +  +  + ]:         502 :         if (RelationIsSecurityView(view))
    3923                 :             :         {
    3924                 :             :             /*
    3925                 :             :              * The view's quals go in front of existing barrier quals: those
    3926                 :             :              * would have come from an outer level of security-barrier view,
    3927                 :             :              * and so must get evaluated later.
    3928                 :             :              *
    3929                 :             :              * Note: the parsetree has been mutated, so the new_rte pointer is
    3930                 :             :              * stale and needs to be re-computed.
    3931                 :             :              */
    3932                 :         156 :             new_rte = rt_fetch(new_rt_index, parsetree->rtable);
    3933                 :         156 :             new_rte->securityQuals = lcons(viewqual, new_rte->securityQuals);
    3934                 :             : 
    3935                 :             :             /*
    3936                 :             :              * Do not set parsetree->hasRowSecurity, because these aren't RLS
    3937                 :             :              * conditions (they aren't affected by enabling/disabling RLS).
    3938                 :             :              */
    3939                 :             : 
    3940                 :             :             /*
    3941                 :             :              * Make sure that the query is marked correctly if the added qual
    3942                 :             :              * has sublinks.
    3943                 :             :              */
    3944         [ +  + ]:         156 :             if (!parsetree->hasSubLinks)
    3945                 :         140 :                 parsetree->hasSubLinks = checkExprHasSubLink(viewqual);
    3946                 :             :         }
    3947                 :             :         else
    3948                 :         346 :             AddQual(parsetree, viewqual);
    3949                 :             :     }
    3950                 :             : 
    3951                 :             :     /*
    3952                 :             :      * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE), if the view has
    3953                 :             :      * the WITH CHECK OPTION, or any parent view specified WITH CASCADED CHECK
    3954                 :             :      * OPTION, add the quals from the view to the query's withCheckOptions
    3955                 :             :      * list.
    3956                 :             :      */
    3957         [ +  + ]:        2222 :     if (insert_or_update)
    3958                 :             :     {
    3959   [ +  +  +  + ]:        1958 :         bool        has_wco = RelationHasCheckOption(view);
    3960   [ +  +  +  + ]:        1958 :         bool        cascaded = RelationHasCascadedCheckOption(view);
    3961                 :             : 
    3962                 :             :         /*
    3963                 :             :          * If the parent view has a cascaded check option, treat this view as
    3964                 :             :          * if it also had a cascaded check option.
    3965                 :             :          *
    3966                 :             :          * New WithCheckOptions are added to the start of the list, so if
    3967                 :             :          * there is a cascaded check option, it will be the first item in the
    3968                 :             :          * list.
    3969                 :             :          */
    3970         [ +  + ]:        1958 :         if (parsetree->withCheckOptions != NIL)
    3971                 :             :         {
    3972                 :          76 :             WithCheckOption *parent_wco =
    3973                 :          76 :                 (WithCheckOption *) linitial(parsetree->withCheckOptions);
    3974                 :             : 
    3975         [ +  + ]:          76 :             if (parent_wco->cascaded)
    3976                 :             :             {
    3977                 :          60 :                 has_wco = true;
    3978                 :          60 :                 cascaded = true;
    3979                 :             :             }
    3980                 :             :         }
    3981                 :             : 
    3982                 :             :         /*
    3983                 :             :          * Add the new WithCheckOption to the start of the list, so that
    3984                 :             :          * checks on inner views are run before checks on outer views, as
    3985                 :             :          * required by the SQL standard.
    3986                 :             :          *
    3987                 :             :          * If the new check is CASCADED, we need to add it even if this view
    3988                 :             :          * has no quals, since there may be quals on child views.  A LOCAL
    3989                 :             :          * check can be omitted if this view has no quals.
    3990                 :             :          */
    3991   [ +  +  +  +  :        1958 :         if (has_wco && (cascaded || viewquery->jointree->quals != NULL))
                   +  - ]
    3992                 :             :         {
    3993                 :             :             WithCheckOption *wco;
    3994                 :             : 
    3995                 :         430 :             wco = makeNode(WithCheckOption);
    3996                 :         430 :             wco->kind = WCO_VIEW_CHECK;
    3997                 :         430 :             wco->relname = pstrdup(RelationGetRelationName(view));
    3998                 :         430 :             wco->polname = NULL;
    3999                 :         430 :             wco->qual = NULL;
    4000                 :         430 :             wco->cascaded = cascaded;
    4001                 :             : 
    4002                 :         430 :             parsetree->withCheckOptions = lcons(wco,
    4003                 :             :                                                 parsetree->withCheckOptions);
    4004                 :             : 
    4005         [ +  + ]:         430 :             if (viewquery->jointree->quals != NULL)
    4006                 :             :             {
    4007                 :         390 :                 wco->qual = (Node *) viewquery->jointree->quals;
    4008                 :         390 :                 ChangeVarNodes(wco->qual, base_rt_index, new_rt_index, 0);
    4009                 :             : 
    4010                 :             :                 /*
    4011                 :             :                  * For INSERT, make sure that the query is marked correctly if
    4012                 :             :                  * the added qual has sublinks.  This can be skipped for
    4013                 :             :                  * UPDATE/MERGE, since the same qual will have already been
    4014                 :             :                  * added above, and the check will already have been done.
    4015                 :             :                  */
    4016         [ +  + ]:         390 :                 if (!parsetree->hasSubLinks &&
    4017         [ +  + ]:         326 :                     parsetree->commandType == CMD_INSERT)
    4018                 :         204 :                     parsetree->hasSubLinks = checkExprHasSubLink(wco->qual);
    4019                 :             :             }
    4020                 :             :         }
    4021                 :             :     }
    4022                 :             : 
    4023                 :        2222 :     table_close(base_rel, NoLock);
    4024                 :             : 
    4025                 :        2222 :     return parsetree;
    4026                 :             : }
    4027                 :             : 
    4028                 :             : 
    4029                 :             : /*
    4030                 :             :  * RewriteQuery -
    4031                 :             :  *    rewrites the query and apply the rules again on the queries rewritten
    4032                 :             :  *
    4033                 :             :  * rewrite_events is a list of open query-rewrite actions, so we can detect
    4034                 :             :  * infinite recursion.
    4035                 :             :  *
    4036                 :             :  * orig_rt_length is the length of the originating query's rtable, for product
    4037                 :             :  * queries created by fireRules(), and 0 otherwise.  This is used to skip any
    4038                 :             :  * already-processed VALUES RTEs from the original query.
    4039                 :             :  *
    4040                 :             :  * num_ctes_processed is the number of CTEs at the end of the query's cteList
    4041                 :             :  * that have already been rewritten, and must not be rewritten again.
    4042                 :             :  */
    4043                 :             : static List *
    4044                 :      291785 : RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length,
    4045                 :             :              int num_ctes_processed)
    4046                 :             : {
    4047                 :      291785 :     CmdType     event = parsetree->commandType;
    4048                 :      291785 :     bool        instead = false;
    4049                 :      291785 :     bool        returning = false;
    4050                 :      291785 :     bool        updatableview = false;
    4051                 :      291785 :     Query      *qual_product = NULL;
    4052                 :      291785 :     List       *rewritten = NIL;
    4053                 :             :     ListCell   *lc1;
    4054                 :             : 
    4055                 :             :     /*
    4056                 :             :      * First, recursively process any insert/update/delete/merge statements in
    4057                 :             :      * WITH clauses.  (We have to do this first because the WITH clauses may
    4058                 :             :      * get copied into rule actions below.)
    4059                 :             :      *
    4060                 :             :      * Any new WITH clauses from rule actions are processed when we recurse
    4061                 :             :      * into product queries below.  However, when recursing, we must take care
    4062                 :             :      * to avoid rewriting a CTE query more than once (because expanding
    4063                 :             :      * generated columns in the targetlist more than once would fail).  Since
    4064                 :             :      * new CTEs from product queries are added to the start of the list (see
    4065                 :             :      * rewriteRuleAction), we just skip the last num_ctes_processed items.
    4066                 :             :      */
    4067   [ +  +  +  +  :      294055 :     foreach(lc1, parsetree->cteList)
                   +  + ]
    4068                 :             :     {
    4069                 :        2318 :         CommonTableExpr *cte = lfirst_node(CommonTableExpr, lc1);
    4070                 :        2318 :         Query      *ctequery = castNode(Query, cte->ctequery);
    4071                 :        2318 :         int         i = foreach_current_index(lc1);
    4072                 :             :         List       *newstuff;
    4073                 :             : 
    4074                 :             :         /* Skip already-processed CTEs at the end of the list */
    4075         [ +  + ]:        2318 :         if (i >= list_length(parsetree->cteList) - num_ctes_processed)
    4076                 :          28 :             break;
    4077                 :             : 
    4078         [ +  + ]:        2290 :         if (ctequery->commandType == CMD_SELECT)
    4079                 :        2040 :             continue;
    4080                 :             : 
    4081                 :         250 :         newstuff = RewriteQuery(ctequery, rewrite_events, 0, 0);
    4082                 :             : 
    4083                 :             :         /*
    4084                 :             :          * Currently we can only handle unconditional, single-statement DO
    4085                 :             :          * INSTEAD rules correctly; we have to get exactly one non-utility
    4086                 :             :          * Query out of the rewrite operation to stuff back into the CTE node.
    4087                 :             :          */
    4088         [ +  + ]:         250 :         if (list_length(newstuff) == 1)
    4089                 :             :         {
    4090                 :             :             /* Must check it's not a utility command */
    4091                 :         234 :             ctequery = linitial_node(Query, newstuff);
    4092         [ +  - ]:         234 :             if (!(ctequery->commandType == CMD_SELECT ||
    4093         [ +  + ]:         234 :                   ctequery->commandType == CMD_UPDATE ||
    4094         [ +  + ]:         169 :                   ctequery->commandType == CMD_INSERT ||
    4095         [ +  + ]:          54 :                   ctequery->commandType == CMD_DELETE ||
    4096         [ +  + ]:          23 :                   ctequery->commandType == CMD_MERGE))
    4097                 :             :             {
    4098                 :             :                 /*
    4099                 :             :                  * Currently it could only be NOTIFY; this error message will
    4100                 :             :                  * need work if we ever allow other utility commands in rules.
    4101                 :             :                  */
    4102         [ +  - ]:           4 :                 ereport(ERROR,
    4103                 :             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4104                 :             :                          errmsg("DO INSTEAD NOTIFY rules are not supported for data-modifying statements in WITH")));
    4105                 :             :             }
    4106                 :             :             /* WITH queries should never be canSetTag */
    4107                 :             :             Assert(!ctequery->canSetTag);
    4108                 :             :             /* Push the single Query back into the CTE node */
    4109                 :         230 :             cte->ctequery = (Node *) ctequery;
    4110                 :             :         }
    4111         [ +  + ]:          16 :         else if (newstuff == NIL)
    4112                 :             :         {
    4113         [ +  - ]:           4 :             ereport(ERROR,
    4114                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4115                 :             :                      errmsg("DO INSTEAD NOTHING rules are not supported for data-modifying statements in WITH")));
    4116                 :             :         }
    4117                 :             :         else
    4118                 :             :         {
    4119                 :             :             ListCell   *lc2;
    4120                 :             : 
    4121                 :             :             /* examine queries to determine which error message to issue */
    4122   [ +  -  +  +  :          28 :             foreach(lc2, newstuff)
                   +  + ]
    4123                 :             :             {
    4124                 :          24 :                 Query      *q = (Query *) lfirst(lc2);
    4125                 :             : 
    4126         [ +  + ]:          24 :                 if (q->querySource == QSRC_QUAL_INSTEAD_RULE)
    4127         [ +  - ]:           4 :                     ereport(ERROR,
    4128                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4129                 :             :                              errmsg("conditional DO INSTEAD rules are not supported for data-modifying statements in WITH")));
    4130         [ +  + ]:          20 :                 if (q->querySource == QSRC_NON_INSTEAD_RULE)
    4131         [ +  - ]:           4 :                     ereport(ERROR,
    4132                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4133                 :             :                              errmsg("DO ALSO rules are not supported for data-modifying statements in WITH")));
    4134                 :             :             }
    4135                 :             : 
    4136         [ +  - ]:           4 :             ereport(ERROR,
    4137                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4138                 :             :                      errmsg("multi-statement DO INSTEAD rules are not supported for data-modifying statements in WITH")));
    4139                 :             :         }
    4140                 :             :     }
    4141                 :      291765 :     num_ctes_processed = list_length(parsetree->cteList);
    4142                 :             : 
    4143                 :             :     /*
    4144                 :             :      * If the statement is an insert, update, delete, or merge, adjust its
    4145                 :             :      * targetlist as needed, and then fire INSERT/UPDATE/DELETE rules on it.
    4146                 :             :      *
    4147                 :             :      * SELECT rules are handled later when we have all the queries that should
    4148                 :             :      * get executed.  Also, utilities aren't rewritten at all (do we still
    4149                 :             :      * need that check?)
    4150                 :             :      */
    4151   [ +  +  +  + ]:      291765 :     if (event != CMD_SELECT && event != CMD_UTILITY)
    4152                 :             :     {
    4153                 :             :         int         result_relation;
    4154                 :             :         RangeTblEntry *rt_entry;
    4155                 :             :         Relation    rt_entry_relation;
    4156                 :             :         List       *locks;
    4157                 :             :         int         product_orig_rt_length;
    4158                 :             :         List       *product_queries;
    4159                 :       60948 :         bool        hasUpdate = false;
    4160                 :       60948 :         int         values_rte_index = 0;
    4161                 :       60948 :         bool        defaults_remaining = false;
    4162                 :             : 
    4163                 :       60948 :         result_relation = parsetree->resultRelation;
    4164                 :             :         Assert(result_relation != 0);
    4165                 :       60948 :         rt_entry = rt_fetch(result_relation, parsetree->rtable);
    4166                 :             :         Assert(rt_entry->rtekind == RTE_RELATION);
    4167                 :             : 
    4168                 :             :         /*
    4169                 :             :          * We can use NoLock here since either the parser or
    4170                 :             :          * AcquireRewriteLocks should have locked the rel already.
    4171                 :             :          */
    4172                 :       60948 :         rt_entry_relation = relation_open(rt_entry->relid, NoLock);
    4173                 :             : 
    4174                 :             :         /* We don't support FOR PORTION OF on views with INSTEAD OF triggers. */
    4175         [ +  + ]:       60948 :         if (parsetree->forPortionOf &&
    4176   [ +  +  +  + ]:        1097 :             rt_entry_relation->rd_rel->relkind == RELKIND_VIEW &&
    4177                 :          52 :             view_has_instead_trigger(rt_entry_relation, event, NIL))
    4178         [ +  - ]:          24 :             ereport(ERROR,
    4179                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4180                 :             :                      errmsg("views with INSTEAD OF triggers do not support FOR PORTION OF")));
    4181                 :             : 
    4182                 :             :         /*
    4183                 :             :          * Rewrite the targetlist as needed for the command type.
    4184                 :             :          */
    4185         [ +  + ]:       60924 :         if (event == CMD_INSERT)
    4186                 :             :         {
    4187                 :             :             ListCell   *lc2;
    4188                 :       45587 :             RangeTblEntry *values_rte = NULL;
    4189                 :             : 
    4190                 :             :             /*
    4191                 :             :              * Test if it's a multi-row INSERT ... VALUES (...), (...), ... by
    4192                 :             :              * looking for a VALUES RTE in the fromlist.  For product queries,
    4193                 :             :              * we must ignore any already-processed VALUES RTEs from the
    4194                 :             :              * original query.  These appear at the start of the rangetable.
    4195                 :             :              */
    4196   [ +  +  +  +  :       53789 :             foreach(lc2, parsetree->jointree->fromlist)
                   +  + ]
    4197                 :             :             {
    4198                 :        8202 :                 RangeTblRef *rtr = (RangeTblRef *) lfirst(lc2);
    4199                 :             : 
    4200   [ +  -  +  + ]:        8202 :                 if (IsA(rtr, RangeTblRef) && rtr->rtindex > orig_rt_length)
    4201                 :             :                 {
    4202                 :        7970 :                     RangeTblEntry *rte = rt_fetch(rtr->rtindex,
    4203                 :             :                                                   parsetree->rtable);
    4204                 :             : 
    4205         [ +  + ]:        7970 :                     if (rte->rtekind == RTE_VALUES)
    4206                 :             :                     {
    4207                 :             :                         /* should not find more than one VALUES RTE */
    4208         [ -  + ]:        3467 :                         if (values_rte != NULL)
    4209         [ #  # ]:           0 :                             elog(ERROR, "more than one VALUES RTE found");
    4210                 :             : 
    4211                 :        3467 :                         values_rte = rte;
    4212                 :        3467 :                         values_rte_index = rtr->rtindex;
    4213                 :             :                     }
    4214                 :             :                 }
    4215                 :             :             }
    4216                 :             : 
    4217         [ +  + ]:       45587 :             if (values_rte)
    4218                 :             :             {
    4219                 :        3467 :                 Bitmapset  *unused_values_attrnos = NULL;
    4220                 :             : 
    4221                 :             :                 /* Process the main targetlist ... */
    4222                 :        3467 :                 parsetree->targetList = rewriteTargetListIU(parsetree->targetList,
    4223                 :             :                                                             parsetree->commandType,
    4224                 :             :                                                             parsetree->override,
    4225                 :             :                                                             rt_entry_relation,
    4226                 :             :                                                             values_rte,
    4227                 :             :                                                             values_rte_index,
    4228                 :             :                                                             &unused_values_attrnos);
    4229                 :             :                 /* ... and the VALUES expression lists */
    4230         [ +  + ]:        3399 :                 if (!rewriteValuesRTE(parsetree, values_rte, values_rte_index,
    4231                 :             :                                       rt_entry_relation,
    4232                 :             :                                       unused_values_attrnos))
    4233                 :          52 :                     defaults_remaining = true;
    4234                 :             :             }
    4235                 :             :             else
    4236                 :             :             {
    4237                 :             :                 /* Process just the main targetlist */
    4238                 :       42068 :                 parsetree->targetList =
    4239                 :       42120 :                     rewriteTargetListIU(parsetree->targetList,
    4240                 :             :                                         parsetree->commandType,
    4241                 :             :                                         parsetree->override,
    4242                 :             :                                         rt_entry_relation,
    4243                 :             :                                         NULL, 0, NULL);
    4244                 :             :             }
    4245                 :             : 
    4246         [ +  + ]:       45467 :             if (parsetree->onConflict &&
    4247         [ +  + ]:        1709 :                 parsetree->onConflict->action == ONCONFLICT_UPDATE)
    4248                 :             :             {
    4249                 :        1023 :                 parsetree->onConflict->onConflictSet =
    4250                 :        1023 :                     rewriteTargetListIU(parsetree->onConflict->onConflictSet,
    4251                 :             :                                         CMD_UPDATE,
    4252                 :             :                                         parsetree->override,
    4253                 :             :                                         rt_entry_relation,
    4254                 :             :                                         NULL, 0, NULL);
    4255                 :             :             }
    4256                 :             :         }
    4257         [ +  + ]:       15337 :         else if (event == CMD_UPDATE)
    4258                 :             :         {
    4259                 :             :             Assert(parsetree->override == OVERRIDING_NOT_SET);
    4260                 :             : 
    4261         [ +  + ]:        9901 :             if (parsetree->forPortionOf)
    4262                 :             :             {
    4263                 :             :                 /*
    4264                 :             :                  * Don't add FOR PORTION OF details until we're done rewriting
    4265                 :             :                  * a view update, so that we don't add the same qual and TLE
    4266                 :             :                  * on the recursion.
    4267                 :             :                  *
    4268                 :             :                  * Views don't need to do anything special here to remap Vars;
    4269                 :             :                  * that is handled by the tree walker.
    4270                 :             :                  */
    4271         [ +  + ]:         578 :                 if (rt_entry_relation->rd_rel->relkind != RELKIND_VIEW)
    4272                 :             :                 {
    4273                 :             :                     ListCell   *tl;
    4274                 :             : 
    4275                 :             :                     /*
    4276                 :             :                      * Add qual: UPDATE FOR PORTION OF should be limited to
    4277                 :             :                      * rows that overlap the target range.
    4278                 :             :                      */
    4279                 :         562 :                     AddQual(parsetree, parsetree->forPortionOf->overlapsExpr);
    4280                 :             : 
    4281                 :             :                     /* Update FOR PORTION OF column(s) automatically. */
    4282   [ +  -  +  +  :        1124 :                     foreach(tl, parsetree->forPortionOf->rangeTargetList)
                   +  + ]
    4283                 :             :                     {
    4284                 :         562 :                         TargetEntry *tle = (TargetEntry *) lfirst(tl);
    4285                 :             : 
    4286                 :         562 :                         parsetree->targetList = lappend(parsetree->targetList, tle);
    4287                 :             :                     }
    4288                 :             :                 }
    4289                 :             :             }
    4290                 :             : 
    4291                 :        9869 :             parsetree->targetList =
    4292                 :        9901 :                 rewriteTargetListIU(parsetree->targetList,
    4293                 :             :                                     parsetree->commandType,
    4294                 :             :                                     parsetree->override,
    4295                 :             :                                     rt_entry_relation,
    4296                 :             :                                     NULL, 0, NULL);
    4297                 :             :         }
    4298         [ +  + ]:        5436 :         else if (event == CMD_MERGE)
    4299                 :             :         {
    4300                 :             :             Assert(parsetree->override == OVERRIDING_NOT_SET);
    4301                 :             : 
    4302                 :             :             /*
    4303                 :             :              * Rewrite each action targetlist separately
    4304                 :             :              */
    4305   [ +  -  +  +  :        4659 :             foreach(lc1, parsetree->mergeActionList)
                   +  + ]
    4306                 :             :             {
    4307                 :        2757 :                 MergeAction *action = (MergeAction *) lfirst(lc1);
    4308                 :             : 
    4309      [ +  +  - ]:        2757 :                 switch (action->commandType)
    4310                 :             :                 {
    4311                 :         514 :                     case CMD_NOTHING:
    4312                 :             :                     case CMD_DELETE:    /* Nothing to do here */
    4313                 :         514 :                         break;
    4314                 :        2243 :                     case CMD_UPDATE:
    4315                 :             :                     case CMD_INSERT:
    4316                 :             : 
    4317                 :             :                         /*
    4318                 :             :                          * MERGE actions do not permit multi-row INSERTs, so
    4319                 :             :                          * there is no VALUES RTE to deal with here.
    4320                 :             :                          */
    4321                 :        2239 :                         action->targetList =
    4322                 :        2243 :                             rewriteTargetListIU(action->targetList,
    4323                 :             :                                                 action->commandType,
    4324                 :             :                                                 action->override,
    4325                 :             :                                                 rt_entry_relation,
    4326                 :             :                                                 NULL, 0, NULL);
    4327                 :        2239 :                         break;
    4328                 :           0 :                     default:
    4329         [ #  # ]:           0 :                         elog(ERROR, "unrecognized commandType: %d", action->commandType);
    4330                 :             :                         break;
    4331                 :             :                 }
    4332                 :             :             }
    4333                 :             :         }
    4334         [ +  - ]:        3530 :         else if (event == CMD_DELETE)
    4335                 :             :         {
    4336         [ +  + ]:        3530 :             if (parsetree->forPortionOf)
    4337                 :             :             {
    4338                 :             :                 /*
    4339                 :             :                  * Don't add FOR PORTION OF details until we're done rewriting
    4340                 :             :                  * a view delete, so that we don't add the same qual on the
    4341                 :             :                  * recursion.
    4342                 :             :                  *
    4343                 :             :                  * Views don't need to do anything special here to remap Vars;
    4344                 :             :                  * that is handled by the tree walker.
    4345                 :             :                  */
    4346         [ +  + ]:         443 :                 if (rt_entry_relation->rd_rel->relkind != RELKIND_VIEW)
    4347                 :             :                 {
    4348                 :             :                     /*
    4349                 :             :                      * Add qual: DELETE FOR PORTION OF should be limited to
    4350                 :             :                      * rows that overlap the target range.
    4351                 :             :                      */
    4352                 :         431 :                     AddQual(parsetree, parsetree->forPortionOf->overlapsExpr);
    4353                 :             :                 }
    4354                 :             :             }
    4355                 :             :         }
    4356                 :             :         else
    4357         [ #  # ]:           0 :             elog(ERROR, "unrecognized commandType: %d", (int) event);
    4358                 :             : 
    4359                 :             :         /*
    4360                 :             :          * Collect and apply the appropriate rules.
    4361                 :             :          */
    4362                 :       60768 :         locks = matchLocks(event, rt_entry_relation,
    4363                 :             :                            result_relation, parsetree, &hasUpdate);
    4364                 :             : 
    4365                 :       60756 :         product_orig_rt_length = list_length(parsetree->rtable);
    4366                 :       60756 :         product_queries = fireRules(parsetree,
    4367                 :             :                                     result_relation,
    4368                 :             :                                     event,
    4369                 :             :                                     locks,
    4370                 :             :                                     &instead,
    4371                 :             :                                     &returning,
    4372                 :             :                                     &qual_product);
    4373                 :             : 
    4374                 :             :         /*
    4375                 :             :          * If we have a VALUES RTE with any remaining untouched DEFAULT items,
    4376                 :             :          * and we got any product queries, finalize the VALUES RTE for each
    4377                 :             :          * product query (replacing the remaining DEFAULT items with NULLs).
    4378                 :             :          * We don't do this for the original query, because we know that it
    4379                 :             :          * must be an auto-insert on a view, and so should use the base
    4380                 :             :          * relation's defaults for any remaining DEFAULT items.
    4381                 :             :          */
    4382   [ +  +  +  + ]:       60748 :         if (defaults_remaining && product_queries != NIL)
    4383                 :             :         {
    4384                 :             :             ListCell   *n;
    4385                 :             : 
    4386                 :             :             /*
    4387                 :             :              * Each product query has its own copy of the VALUES RTE at the
    4388                 :             :              * same index in the rangetable, so we must finalize each one.
    4389                 :             :              *
    4390                 :             :              * Note that if the product query is an INSERT ... SELECT, then
    4391                 :             :              * the VALUES RTE will be at the same index in the SELECT part of
    4392                 :             :              * the product query rather than the top-level product query
    4393                 :             :              * itself.
    4394                 :             :              */
    4395   [ +  -  +  +  :          32 :             foreach(n, product_queries)
                   +  + ]
    4396                 :             :             {
    4397                 :          16 :                 Query      *pt = (Query *) lfirst(n);
    4398                 :             :                 RangeTblEntry *values_rte;
    4399                 :             : 
    4400         [ +  - ]:          16 :                 if (pt->commandType == CMD_INSERT &&
    4401   [ +  -  +  -  :          32 :                     pt->jointree && IsA(pt->jointree, FromExpr) &&
                   +  - ]
    4402                 :          16 :                     list_length(pt->jointree->fromlist) == 1)
    4403                 :             :                 {
    4404                 :          16 :                     Node       *jtnode = (Node *) linitial(pt->jointree->fromlist);
    4405                 :             : 
    4406         [ +  - ]:          16 :                     if (IsA(jtnode, RangeTblRef))
    4407                 :             :                     {
    4408                 :          16 :                         int         rtindex = ((RangeTblRef *) jtnode)->rtindex;
    4409                 :          16 :                         RangeTblEntry *src_rte = rt_fetch(rtindex, pt->rtable);
    4410                 :             : 
    4411         [ +  + ]:          16 :                         if (src_rte->rtekind == RTE_SUBQUERY &&
    4412         [ +  - ]:           4 :                             src_rte->subquery &&
    4413         [ +  - ]:           4 :                             IsA(src_rte->subquery, Query) &&
    4414         [ +  - ]:           4 :                             src_rte->subquery->commandType == CMD_SELECT)
    4415                 :           4 :                             pt = src_rte->subquery;
    4416                 :             :                     }
    4417                 :             :                 }
    4418                 :             : 
    4419                 :          16 :                 values_rte = rt_fetch(values_rte_index, pt->rtable);
    4420         [ -  + ]:          16 :                 if (values_rte->rtekind != RTE_VALUES)
    4421         [ #  # ]:           0 :                     elog(ERROR, "failed to find VALUES RTE in product query");
    4422                 :             : 
    4423                 :          16 :                 rewriteValuesRTEToNulls(pt, values_rte);
    4424                 :             :             }
    4425                 :             :         }
    4426                 :             : 
    4427                 :             :         /*
    4428                 :             :          * If there was no unqualified INSTEAD rule, and the target relation
    4429                 :             :          * is a view without any INSTEAD OF triggers, see if the view can be
    4430                 :             :          * automatically updated.  If so, we perform the necessary query
    4431                 :             :          * transformation here and add the resulting query to the
    4432                 :             :          * product_queries list, so that it gets recursively rewritten if
    4433                 :             :          * necessary.  For MERGE, the view must be automatically updatable if
    4434                 :             :          * any of the merge actions lack a corresponding INSTEAD OF trigger.
    4435                 :             :          *
    4436                 :             :          * If the view cannot be automatically updated, we throw an error here
    4437                 :             :          * which is OK since the query would fail at runtime anyway.  Throwing
    4438                 :             :          * the error here is preferable to the executor check since we have
    4439                 :             :          * more detailed information available about why the view isn't
    4440                 :             :          * updatable.
    4441                 :             :          */
    4442         [ +  + ]:       60748 :         if (!instead &&
    4443         [ +  + ]:       60256 :             rt_entry_relation->rd_rel->relkind == RELKIND_VIEW &&
    4444         [ +  + ]:        2691 :             !view_has_instead_trigger(rt_entry_relation, event,
    4445                 :             :                                       parsetree->mergeActionList))
    4446                 :             :         {
    4447                 :             :             /*
    4448                 :             :              * If there were any qualified INSTEAD rules, don't allow the view
    4449                 :             :              * to be automatically updated (an unqualified INSTEAD rule or
    4450                 :             :              * INSTEAD OF trigger is required).
    4451                 :             :              */
    4452         [ +  + ]:        2422 :             if (qual_product != NULL)
    4453                 :          12 :                 error_view_not_updatable(rt_entry_relation,
    4454                 :             :                                          parsetree->commandType,
    4455                 :             :                                          parsetree->mergeActionList,
    4456                 :             :                                          gettext_noop("Views with conditional DO INSTEAD rules are not automatically updatable."));
    4457                 :             : 
    4458                 :             :             /*
    4459                 :             :              * Attempt to rewrite the query to automatically update the view.
    4460                 :             :              * This throws an error if the view can't be automatically
    4461                 :             :              * updated.
    4462                 :             :              */
    4463                 :        2410 :             parsetree = rewriteTargetView(parsetree, rt_entry_relation);
    4464                 :             : 
    4465                 :             :             /*
    4466                 :             :              * At this point product_queries contains any DO ALSO rule
    4467                 :             :              * actions. Add the rewritten query before or after those.  This
    4468                 :             :              * must match the handling the original query would have gotten
    4469                 :             :              * below, if we allowed it to be included again.
    4470                 :             :              */
    4471         [ +  + ]:        2222 :             if (parsetree->commandType == CMD_INSERT)
    4472                 :         796 :                 product_queries = lcons(parsetree, product_queries);
    4473                 :             :             else
    4474                 :        1426 :                 product_queries = lappend(product_queries, parsetree);
    4475                 :             : 
    4476                 :             :             /*
    4477                 :             :              * Set the "instead" flag, as if there had been an unqualified
    4478                 :             :              * INSTEAD, to prevent the original query from being included a
    4479                 :             :              * second time below.  The transformation will have rewritten any
    4480                 :             :              * RETURNING list, so we can also set "returning" to forestall
    4481                 :             :              * throwing an error below.
    4482                 :             :              */
    4483                 :        2222 :             instead = true;
    4484                 :        2222 :             returning = true;
    4485                 :        2222 :             updatableview = true;
    4486                 :             :         }
    4487                 :             : 
    4488                 :             :         /*
    4489                 :             :          * If we got any product queries, recursively rewrite them --- but
    4490                 :             :          * first check for recursion!
    4491                 :             :          */
    4492         [ +  + ]:       60548 :         if (product_queries != NIL)
    4493                 :             :         {
    4494                 :             :             ListCell   *n;
    4495                 :             :             rewrite_event *rev;
    4496                 :             : 
    4497   [ +  +  +  +  :        3678 :             foreach(n, rewrite_events)
                   +  + ]
    4498                 :             :             {
    4499                 :         644 :                 rev = (rewrite_event *) lfirst(n);
    4500         [ -  + ]:         644 :                 if (rev->relation == RelationGetRelid(rt_entry_relation) &&
    4501         [ #  # ]:           0 :                     rev->event == event)
    4502         [ #  # ]:           0 :                     ereport(ERROR,
    4503                 :             :                             (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    4504                 :             :                              errmsg("infinite recursion detected in rules for relation \"%s\"",
    4505                 :             :                                     RelationGetRelationName(rt_entry_relation))));
    4506                 :             :             }
    4507                 :             : 
    4508                 :        3034 :             rev = palloc_object(rewrite_event);
    4509                 :        3034 :             rev->relation = RelationGetRelid(rt_entry_relation);
    4510                 :        3034 :             rev->event = event;
    4511                 :        3034 :             rewrite_events = lappend(rewrite_events, rev);
    4512                 :             : 
    4513   [ +  -  +  +  :        6124 :             foreach(n, product_queries)
                   +  + ]
    4514                 :             :             {
    4515                 :        3194 :                 Query      *pt = (Query *) lfirst(n);
    4516                 :             :                 List       *newstuff;
    4517                 :             : 
    4518                 :             :                 /*
    4519                 :             :                  * For an updatable view, pt might be the rewritten version of
    4520                 :             :                  * the original query, in which case we pass on orig_rt_length
    4521                 :             :                  * to finish processing any VALUES RTE it contained.
    4522                 :             :                  *
    4523                 :             :                  * Otherwise, we have a product query created by fireRules().
    4524                 :             :                  * Any VALUES RTEs from the original query have been fully
    4525                 :             :                  * processed, and must be skipped when we recurse.
    4526                 :             :                  */
    4527         [ +  + ]:        3194 :                 newstuff = RewriteQuery(pt, rewrite_events,
    4528                 :             :                                         pt == parsetree ?
    4529                 :             :                                         orig_rt_length :
    4530                 :             :                                         product_orig_rt_length,
    4531                 :             :                                         num_ctes_processed);
    4532                 :        3090 :                 rewritten = list_concat(rewritten, newstuff);
    4533                 :             :             }
    4534                 :             : 
    4535                 :        2930 :             rewrite_events = list_delete_last(rewrite_events);
    4536                 :             :         }
    4537                 :             : 
    4538                 :             :         /*
    4539                 :             :          * If there is an INSTEAD, and the original query has a RETURNING, we
    4540                 :             :          * have to have found a RETURNING in the rule(s), else fail. (Because
    4541                 :             :          * DefineQueryRewrite only allows RETURNING in unconditional INSTEAD
    4542                 :             :          * rules, there's no need to worry whether the substituted RETURNING
    4543                 :             :          * will actually be executed --- it must be.)
    4544                 :             :          */
    4545   [ +  +  +  + ]:       60444 :         if ((instead || qual_product != NULL) &&
    4546         [ +  + ]:        2830 :             parsetree->returningList &&
    4547         [ +  + ]:         324 :             !returning)
    4548                 :             :         {
    4549   [ +  -  -  - ]:           4 :             switch (event)
    4550                 :             :             {
    4551                 :           4 :                 case CMD_INSERT:
    4552         [ +  - ]:           4 :                     ereport(ERROR,
    4553                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4554                 :             :                              errmsg("cannot perform INSERT RETURNING on relation \"%s\"",
    4555                 :             :                                     RelationGetRelationName(rt_entry_relation)),
    4556                 :             :                              errhint("You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.")));
    4557                 :             :                     break;
    4558                 :           0 :                 case CMD_UPDATE:
    4559         [ #  # ]:           0 :                     ereport(ERROR,
    4560                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4561                 :             :                              errmsg("cannot perform UPDATE RETURNING on relation \"%s\"",
    4562                 :             :                                     RelationGetRelationName(rt_entry_relation)),
    4563                 :             :                              errhint("You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.")));
    4564                 :             :                     break;
    4565                 :           0 :                 case CMD_DELETE:
    4566         [ #  # ]:           0 :                     ereport(ERROR,
    4567                 :             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4568                 :             :                              errmsg("cannot perform DELETE RETURNING on relation \"%s\"",
    4569                 :             :                                     RelationGetRelationName(rt_entry_relation)),
    4570                 :             :                              errhint("You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.")));
    4571                 :             :                     break;
    4572                 :           0 :                 default:
    4573         [ #  # ]:           0 :                     elog(ERROR, "unrecognized commandType: %d",
    4574                 :             :                          (int) event);
    4575                 :             :                     break;
    4576                 :             :             }
    4577                 :             :         }
    4578                 :             : 
    4579                 :             :         /*
    4580                 :             :          * Updatable views are supported by ON CONFLICT, so don't prevent that
    4581                 :             :          * case from proceeding
    4582                 :             :          */
    4583   [ +  +  +  + ]:       60440 :         if (parsetree->onConflict &&
    4584         [ -  + ]:        1529 :             (product_queries != NIL || hasUpdate) &&
    4585         [ +  + ]:         172 :             !updatableview)
    4586         [ +  - ]:           8 :             ereport(ERROR,
    4587                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4588                 :             :                      errmsg("INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules")));
    4589                 :             : 
    4590                 :       60432 :         table_close(rt_entry_relation, NoLock);
    4591                 :             :     }
    4592                 :             : 
    4593                 :             :     /*
    4594                 :             :      * For INSERTs, the original query is done first; for UPDATE/DELETE, it is
    4595                 :             :      * done last.  This is needed because update and delete rule actions might
    4596                 :             :      * not do anything if they are invoked after the update or delete is
    4597                 :             :      * performed. The command counter increment between the query executions
    4598                 :             :      * makes the deleted (and maybe the updated) tuples disappear so the scans
    4599                 :             :      * for them in the rule actions cannot find them.
    4600                 :             :      *
    4601                 :             :      * If we found any unqualified INSTEAD, the original query is not done at
    4602                 :             :      * all, in any form.  Otherwise, we add the modified form if qualified
    4603                 :             :      * INSTEADs were found, else the unmodified form.
    4604                 :             :      */
    4605         [ +  + ]:      291249 :     if (!instead)
    4606                 :             :     {
    4607         [ +  + ]:      288647 :         if (parsetree->commandType == CMD_INSERT)
    4608                 :             :         {
    4609         [ +  + ]:       44311 :             if (qual_product != NULL)
    4610                 :         196 :                 rewritten = lcons(qual_product, rewritten);
    4611                 :             :             else
    4612                 :       44115 :                 rewritten = lcons(parsetree, rewritten);
    4613                 :             :         }
    4614                 :             :         else
    4615                 :             :         {
    4616         [ +  + ]:      244336 :             if (qual_product != NULL)
    4617                 :          20 :                 rewritten = lappend(rewritten, qual_product);
    4618                 :             :             else
    4619                 :      244316 :                 rewritten = lappend(rewritten, parsetree);
    4620                 :             :         }
    4621                 :             :     }
    4622                 :             : 
    4623                 :             :     /*
    4624                 :             :      * If the original query has a CTE list, and we generated more than one
    4625                 :             :      * non-utility result query, we have to fail because we'll have copied the
    4626                 :             :      * CTE list into each result query.  That would break the expectation of
    4627                 :             :      * single evaluation of CTEs.  This could possibly be fixed by
    4628                 :             :      * restructuring so that a CTE list can be shared across multiple Query
    4629                 :             :      * and PlannableStatement nodes.
    4630                 :             :      */
    4631         [ +  + ]:      291249 :     if (parsetree->cteList != NIL)
    4632                 :             :     {
    4633                 :        1611 :         int         qcount = 0;
    4634                 :             : 
    4635   [ +  -  +  +  :        3222 :         foreach(lc1, rewritten)
                   +  + ]
    4636                 :             :         {
    4637                 :        1611 :             Query      *q = (Query *) lfirst(lc1);
    4638                 :             : 
    4639         [ +  - ]:        1611 :             if (q->commandType != CMD_UTILITY)
    4640                 :        1611 :                 qcount++;
    4641                 :             :         }
    4642         [ -  + ]:        1611 :         if (qcount > 1)
    4643         [ #  # ]:           0 :             ereport(ERROR,
    4644                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4645                 :             :                      errmsg("WITH cannot be used in a query that is rewritten by rules into multiple queries")));
    4646                 :             :     }
    4647                 :             : 
    4648                 :      291249 :     return rewritten;
    4649                 :             : }
    4650                 :             : 
    4651                 :             : 
    4652                 :             : /*
    4653                 :             :  * Get a table's generated columns
    4654                 :             :  *
    4655                 :             :  * If include_stored is true, both stored and virtual generated columns are
    4656                 :             :  * returned.  Otherwise, only virtual generated columns are returned.
    4657                 :             :  *
    4658                 :             :  * Returns a list of TargetEntry, one for each generated column, containing
    4659                 :             :  * the attribute numbers and generation expressions.
    4660                 :             :  */
    4661                 :             : static List *
    4662                 :        1509 : get_generated_columns(Relation rel, int rt_index, bool include_stored)
    4663                 :             : {
    4664                 :        1509 :     List       *gen_cols = NIL;
    4665                 :             :     TupleDesc   tupdesc;
    4666                 :             : 
    4667                 :        1509 :     tupdesc = RelationGetDescr(rel);
    4668         [ +  + ]:        1509 :     if (tupdesc->constr &&
    4669   [ +  +  +  - ]:         637 :         (tupdesc->constr->has_generated_virtual ||
    4670         [ +  + ]:         256 :          (include_stored && tupdesc->constr->has_generated_stored)))
    4671                 :             :     {
    4672         [ +  + ]:        1504 :         for (int i = 0; i < tupdesc->natts; i++)
    4673                 :             :         {
    4674                 :        1107 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
    4675                 :             : 
    4676   [ +  +  +  + ]:        1107 :             if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL ||
    4677         [ +  + ]:          48 :                 (include_stored && attr->attgenerated == ATTRIBUTE_GENERATED_STORED))
    4678                 :             :             {
    4679                 :             :                 Node       *defexpr;
    4680                 :             :                 TargetEntry *te;
    4681                 :             : 
    4682                 :         517 :                 defexpr = build_generation_expression(rel, i + 1);
    4683                 :         517 :                 ChangeVarNodes(defexpr, 1, rt_index, 0);
    4684                 :             : 
    4685                 :         517 :                 te = makeTargetEntry((Expr *) defexpr, i + 1, 0, false);
    4686                 :         517 :                 gen_cols = lappend(gen_cols, te);
    4687                 :             :             }
    4688                 :             :         }
    4689                 :             :     }
    4690                 :             : 
    4691                 :        1509 :     return gen_cols;
    4692                 :             : }
    4693                 :             : 
    4694                 :             : /*
    4695                 :             :  * Expand virtual generated columns in an expression
    4696                 :             :  *
    4697                 :             :  * This is for expressions that are not part of a query, such as default
    4698                 :             :  * expressions or index predicates.  The rt_index is usually 1.
    4699                 :             :  */
    4700                 :             : Node *
    4701                 :       10859 : expand_generated_columns_in_expr(Node *node, Relation rel, int rt_index)
    4702                 :             : {
    4703                 :       10859 :     TupleDesc   tupdesc = RelationGetDescr(rel);
    4704                 :             : 
    4705   [ +  +  +  + ]:       10859 :     if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
    4706                 :             :     {
    4707                 :             :         RangeTblEntry *rte;
    4708                 :             :         List       *vcols;
    4709                 :             : 
    4710                 :         365 :         rte = makeNode(RangeTblEntry);
    4711                 :             :         /* eref needs to be set, but the actual name doesn't matter */
    4712                 :         365 :         rte->eref = makeAlias(RelationGetRelationName(rel), NIL);
    4713                 :         365 :         rte->rtekind = RTE_RELATION;
    4714                 :         365 :         rte->relid = RelationGetRelid(rel);
    4715                 :             : 
    4716                 :         365 :         vcols = get_generated_columns(rel, rt_index, false);
    4717                 :             : 
    4718         [ +  - ]:         365 :         if (vcols)
    4719                 :             :         {
    4720                 :             :             /*
    4721                 :             :              * Passing NULL for outer_hasSubLinks is safe because generation
    4722                 :             :              * expressions cannot contain SubLinks, so the replacement cannot
    4723                 :             :              * introduce any.
    4724                 :             :              */
    4725                 :         365 :             node = ReplaceVarsFromTargetList(node, rt_index, 0, rte, vcols, 0,
    4726                 :             :                                              REPLACEVARS_CHANGE_VARNO, rt_index,
    4727                 :             :                                              NULL);
    4728                 :             :         }
    4729                 :             :     }
    4730                 :             : 
    4731                 :       10859 :     return node;
    4732                 :             : }
    4733                 :             : 
    4734                 :             : /*
    4735                 :             :  * Build the generation expression for a generated column.
    4736                 :             :  *
    4737                 :             :  * Error out if there is no generation expression found for the given column.
    4738                 :             :  */
    4739                 :             : Node *
    4740                 :        2201 : build_generation_expression(Relation rel, int attrno)
    4741                 :             : {
    4742                 :        2201 :     TupleDesc   rd_att = RelationGetDescr(rel);
    4743                 :        2201 :     Form_pg_attribute att_tup = TupleDescAttr(rd_att, attrno - 1);
    4744                 :             :     Node       *defexpr;
    4745                 :             :     Oid         attcollid;
    4746                 :             : 
    4747                 :             :     Assert(rd_att->constr &&
    4748                 :             :            (rd_att->constr->has_generated_virtual ||
    4749                 :             :             rd_att->constr->has_generated_stored));
    4750                 :             :     Assert(att_tup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL ||
    4751                 :             :            att_tup->attgenerated == ATTRIBUTE_GENERATED_STORED);
    4752                 :             : 
    4753                 :        2201 :     defexpr = build_column_default(rel, attrno);
    4754         [ -  + ]:        2201 :     if (defexpr == NULL)
    4755         [ #  # ]:           0 :         elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
    4756                 :             :              attrno, RelationGetRelationName(rel));
    4757                 :             : 
    4758                 :             :     /*
    4759                 :             :      * If the column definition has a collation and it is different from the
    4760                 :             :      * collation of the generation expression, put a COLLATE clause around the
    4761                 :             :      * expression.
    4762                 :             :      */
    4763                 :        2201 :     attcollid = att_tup->attcollation;
    4764   [ +  +  +  + ]:        2201 :     if (attcollid && attcollid != exprCollation(defexpr))
    4765                 :             :     {
    4766                 :          10 :         CollateExpr *ce = makeNode(CollateExpr);
    4767                 :             : 
    4768                 :          10 :         ce->arg = (Expr *) defexpr;
    4769                 :          10 :         ce->collOid = attcollid;
    4770                 :          10 :         ce->location = -1;
    4771                 :             : 
    4772                 :          10 :         defexpr = (Node *) ce;
    4773                 :             :     }
    4774                 :             : 
    4775                 :        2201 :     return defexpr;
    4776                 :             : }
    4777                 :             : 
    4778                 :             : 
    4779                 :             : /*
    4780                 :             :  * QueryRewrite -
    4781                 :             :  *    Primary entry point to the query rewriter.
    4782                 :             :  *    Rewrite one query via query rewrite system, possibly returning 0
    4783                 :             :  *    or many queries.
    4784                 :             :  *
    4785                 :             :  * NOTE: the parsetree must either have come straight from the parser,
    4786                 :             :  * or have been scanned by AcquireRewriteLocks to acquire suitable locks.
    4787                 :             :  */
    4788                 :             : List *
    4789                 :      288341 : QueryRewrite(Query *parsetree)
    4790                 :             : {
    4791                 :      288341 :     int64       input_query_id = parsetree->queryId;
    4792                 :             :     List       *querylist;
    4793                 :             :     List       *results;
    4794                 :             :     ListCell   *l;
    4795                 :             :     CmdType     origCmdType;
    4796                 :             :     bool        foundOriginalQuery;
    4797                 :             :     Query      *lastInstead;
    4798                 :             : 
    4799                 :             :     /*
    4800                 :             :      * This function is only applied to top-level original queries
    4801                 :             :      */
    4802                 :             :     Assert(parsetree->querySource == QSRC_ORIGINAL);
    4803                 :             :     Assert(parsetree->canSetTag);
    4804                 :             : 
    4805                 :             :     /*
    4806                 :             :      * Step 1
    4807                 :             :      *
    4808                 :             :      * Apply all non-SELECT rules possibly getting 0 or many queries
    4809                 :             :      */
    4810                 :      288341 :     querylist = RewriteQuery(parsetree, NIL, 0, 0);
    4811                 :             : 
    4812                 :             :     /*
    4813                 :             :      * Step 2
    4814                 :             :      *
    4815                 :             :      * Apply all the RIR rules on each query
    4816                 :             :      *
    4817                 :             :      * This is also a handy place to mark each query with the original queryId
    4818                 :             :      */
    4819                 :      287909 :     results = NIL;
    4820   [ +  +  +  +  :      576150 :     foreach(l, querylist)
                   +  + ]
    4821                 :             :     {
    4822                 :      288377 :         Query      *query = (Query *) lfirst(l);
    4823                 :             : 
    4824                 :      288377 :         query = fireRIRrules(query, NIL);
    4825                 :             : 
    4826                 :      288241 :         query->queryId = input_query_id;
    4827                 :             : 
    4828                 :      288241 :         results = lappend(results, query);
    4829                 :             :     }
    4830                 :             : 
    4831                 :             :     /*
    4832                 :             :      * Step 3
    4833                 :             :      *
    4834                 :             :      * Determine which, if any, of the resulting queries is supposed to set
    4835                 :             :      * the command-result tag; and update the canSetTag fields accordingly.
    4836                 :             :      *
    4837                 :             :      * If the original query is still in the list, it sets the command tag.
    4838                 :             :      * Otherwise, the last INSTEAD query of the same kind as the original is
    4839                 :             :      * allowed to set the tag.  (Note these rules can leave us with no query
    4840                 :             :      * setting the tag.  The tcop code has to cope with this by setting up a
    4841                 :             :      * default tag based on the original un-rewritten query.)
    4842                 :             :      *
    4843                 :             :      * The Asserts verify that at most one query in the result list is marked
    4844                 :             :      * canSetTag.  If we aren't checking asserts, we can fall out of the loop
    4845                 :             :      * as soon as we find the original query.
    4846                 :             :      */
    4847                 :      287773 :     origCmdType = parsetree->commandType;
    4848                 :      287773 :     foundOriginalQuery = false;
    4849                 :      287773 :     lastInstead = NULL;
    4850                 :             : 
    4851   [ +  +  +  +  :      288389 :     foreach(l, results)
                   +  + ]
    4852                 :             :     {
    4853                 :      287929 :         Query      *query = (Query *) lfirst(l);
    4854                 :             : 
    4855         [ +  + ]:      287929 :         if (query->querySource == QSRC_ORIGINAL)
    4856                 :             :         {
    4857                 :             :             Assert(query->canSetTag);
    4858                 :             :             Assert(!foundOriginalQuery);
    4859                 :      287313 :             foundOriginalQuery = true;
    4860                 :             : #ifndef USE_ASSERT_CHECKING
    4861                 :      287313 :             break;
    4862                 :             : #endif
    4863                 :             :         }
    4864                 :             :         else
    4865                 :             :         {
    4866                 :             :             Assert(!query->canSetTag);
    4867         [ +  + ]:         616 :             if (query->commandType == origCmdType &&
    4868         [ +  + ]:         432 :                 (query->querySource == QSRC_INSTEAD_RULE ||
    4869         [ +  + ]:          72 :                  query->querySource == QSRC_QUAL_INSTEAD_RULE))
    4870                 :         392 :                 lastInstead = query;
    4871                 :             :         }
    4872                 :             :     }
    4873                 :             : 
    4874   [ +  +  +  + ]:      287773 :     if (!foundOriginalQuery && lastInstead != NULL)
    4875                 :         376 :         lastInstead->canSetTag = true;
    4876                 :             : 
    4877                 :      287773 :     return results;
    4878                 :             : }
        

Generated by: LCOV version 2.0-1