LCOV - code coverage report
Current view: top level - src/backend/rewrite - rewriteHandler.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 1051 1161 90.5 %
Date: 2021-12-05 01:09:12 Functions: 29 29 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14