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

Generated by: LCOV version 1.16