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

Generated by: LCOV version 1.14