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

Generated by: LCOV version 1.14