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

Generated by: LCOV version 2.0-1