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

Generated by: LCOV version 2.0-1