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

Generated by: LCOV version 2.0-1