LCOV - code coverage report
Current view: top level - src/backend/rewrite - rewriteHandler.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 92.0 % 1782 1640
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 33 33
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 72.1 % 1586 1143

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

Generated by: LCOV version 2.3.2-1