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

Generated by: LCOV version 1.13