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

Generated by: LCOV version 1.11