LCOV - differential code coverage report
Current view: top level - src/backend/rewrite - rewriteHandler.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 90.5 % 1208 1093 32 62 21 28 711 25 329 63 713 3 19
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 29 29 26 1 2 25 1
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a