LCOV - code coverage report
Current view: top level - src/backend/rewrite - rewriteHandler.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 1129 1252 90.2 %
Date: 2024-05-09 12:10:51 Functions: 30 30 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14