LCOV - code coverage report
Current view: top level - src/backend/tcop - utility.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 1482 1966 75.4 %
Date: 2025-11-08 21:17:50 Functions: 16 17 94.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * utility.c
       4             :  *    Contains functions which control the execution of the POSTGRES utility
       5             :  *    commands.  At one time acted as an interface between the Lisp and C
       6             :  *    systems.
       7             :  *
       8             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       9             :  * Portions Copyright (c) 1994, Regents of the University of California
      10             :  *
      11             :  *
      12             :  * IDENTIFICATION
      13             :  *    src/backend/tcop/utility.c
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : #include "postgres.h"
      18             : 
      19             : #include "access/reloptions.h"
      20             : #include "access/twophase.h"
      21             : #include "access/xact.h"
      22             : #include "access/xlog.h"
      23             : #include "catalog/namespace.h"
      24             : #include "catalog/pg_authid.h"
      25             : #include "catalog/pg_inherits.h"
      26             : #include "catalog/toasting.h"
      27             : #include "commands/alter.h"
      28             : #include "commands/async.h"
      29             : #include "commands/cluster.h"
      30             : #include "commands/collationcmds.h"
      31             : #include "commands/comment.h"
      32             : #include "commands/conversioncmds.h"
      33             : #include "commands/copy.h"
      34             : #include "commands/createas.h"
      35             : #include "commands/dbcommands.h"
      36             : #include "commands/defrem.h"
      37             : #include "commands/discard.h"
      38             : #include "commands/event_trigger.h"
      39             : #include "commands/explain.h"
      40             : #include "commands/extension.h"
      41             : #include "commands/lockcmds.h"
      42             : #include "commands/matview.h"
      43             : #include "commands/policy.h"
      44             : #include "commands/portalcmds.h"
      45             : #include "commands/prepare.h"
      46             : #include "commands/proclang.h"
      47             : #include "commands/publicationcmds.h"
      48             : #include "commands/schemacmds.h"
      49             : #include "commands/seclabel.h"
      50             : #include "commands/sequence.h"
      51             : #include "commands/subscriptioncmds.h"
      52             : #include "commands/tablecmds.h"
      53             : #include "commands/tablespace.h"
      54             : #include "commands/trigger.h"
      55             : #include "commands/typecmds.h"
      56             : #include "commands/user.h"
      57             : #include "commands/vacuum.h"
      58             : #include "commands/view.h"
      59             : #include "commands/wait.h"
      60             : #include "miscadmin.h"
      61             : #include "parser/parse_utilcmd.h"
      62             : #include "postmaster/bgwriter.h"
      63             : #include "rewrite/rewriteDefine.h"
      64             : #include "storage/fd.h"
      65             : #include "tcop/utility.h"
      66             : #include "utils/acl.h"
      67             : #include "utils/guc.h"
      68             : #include "utils/lsyscache.h"
      69             : 
      70             : /* Hook for plugins to get control in ProcessUtility() */
      71             : ProcessUtility_hook_type ProcessUtility_hook = NULL;
      72             : 
      73             : /* local function declarations */
      74             : static int  ClassifyUtilityCommandAsReadOnly(Node *parsetree);
      75             : static void ProcessUtilitySlow(ParseState *pstate,
      76             :                                PlannedStmt *pstmt,
      77             :                                const char *queryString,
      78             :                                ProcessUtilityContext context,
      79             :                                ParamListInfo params,
      80             :                                QueryEnvironment *queryEnv,
      81             :                                DestReceiver *dest,
      82             :                                QueryCompletion *qc);
      83             : static void ExecDropStmt(DropStmt *stmt, bool isTopLevel);
      84             : 
      85             : /*
      86             :  * CommandIsReadOnly: is an executable query read-only?
      87             :  *
      88             :  * This is a much stricter test than we apply for XactReadOnly mode;
      89             :  * the query must be *in truth* read-only, because the caller wishes
      90             :  * not to do CommandCounterIncrement for it.
      91             :  *
      92             :  * Note: currently no need to support raw or analyzed queries here
      93             :  */
      94             : bool
      95       18588 : CommandIsReadOnly(PlannedStmt *pstmt)
      96             : {
      97             :     Assert(IsA(pstmt, PlannedStmt));
      98       18588 :     switch (pstmt->commandType)
      99             :     {
     100       18588 :         case CMD_SELECT:
     101       18588 :             if (pstmt->rowMarks != NIL)
     102           0 :                 return false;   /* SELECT FOR [KEY] UPDATE/SHARE */
     103       18588 :             else if (pstmt->hasModifyingCTE)
     104           0 :                 return false;   /* data-modifying CTE */
     105             :             else
     106       18588 :                 return true;
     107           0 :         case CMD_UPDATE:
     108             :         case CMD_INSERT:
     109             :         case CMD_DELETE:
     110             :         case CMD_MERGE:
     111           0 :             return false;
     112           0 :         case CMD_UTILITY:
     113             :             /* For now, treat all utility commands as read/write */
     114           0 :             return false;
     115           0 :         default:
     116           0 :             elog(WARNING, "unrecognized commandType: %d",
     117             :                  (int) pstmt->commandType);
     118           0 :             break;
     119             :     }
     120           0 :     return false;
     121             : }
     122             : 
     123             : /*
     124             :  * Determine the degree to which a utility command is read only.
     125             :  *
     126             :  * Note the definitions of the relevant flags in src/include/tcop/utility.h.
     127             :  */
     128             : static int
     129      420086 : ClassifyUtilityCommandAsReadOnly(Node *parsetree)
     130             : {
     131      420086 :     switch (nodeTag(parsetree))
     132             :     {
     133      252546 :         case T_AlterCollationStmt:
     134             :         case T_AlterDatabaseRefreshCollStmt:
     135             :         case T_AlterDatabaseSetStmt:
     136             :         case T_AlterDatabaseStmt:
     137             :         case T_AlterDefaultPrivilegesStmt:
     138             :         case T_AlterDomainStmt:
     139             :         case T_AlterEnumStmt:
     140             :         case T_AlterEventTrigStmt:
     141             :         case T_AlterExtensionContentsStmt:
     142             :         case T_AlterExtensionStmt:
     143             :         case T_AlterFdwStmt:
     144             :         case T_AlterForeignServerStmt:
     145             :         case T_AlterFunctionStmt:
     146             :         case T_AlterObjectDependsStmt:
     147             :         case T_AlterObjectSchemaStmt:
     148             :         case T_AlterOpFamilyStmt:
     149             :         case T_AlterOperatorStmt:
     150             :         case T_AlterOwnerStmt:
     151             :         case T_AlterPolicyStmt:
     152             :         case T_AlterPublicationStmt:
     153             :         case T_AlterRoleSetStmt:
     154             :         case T_AlterRoleStmt:
     155             :         case T_AlterSeqStmt:
     156             :         case T_AlterStatsStmt:
     157             :         case T_AlterSubscriptionStmt:
     158             :         case T_AlterTSConfigurationStmt:
     159             :         case T_AlterTSDictionaryStmt:
     160             :         case T_AlterTableMoveAllStmt:
     161             :         case T_AlterTableSpaceOptionsStmt:
     162             :         case T_AlterTableStmt:
     163             :         case T_AlterTypeStmt:
     164             :         case T_AlterUserMappingStmt:
     165             :         case T_CommentStmt:
     166             :         case T_CompositeTypeStmt:
     167             :         case T_CreateAmStmt:
     168             :         case T_CreateCastStmt:
     169             :         case T_CreateConversionStmt:
     170             :         case T_CreateDomainStmt:
     171             :         case T_CreateEnumStmt:
     172             :         case T_CreateEventTrigStmt:
     173             :         case T_CreateExtensionStmt:
     174             :         case T_CreateFdwStmt:
     175             :         case T_CreateForeignServerStmt:
     176             :         case T_CreateForeignTableStmt:
     177             :         case T_CreateFunctionStmt:
     178             :         case T_CreateOpClassStmt:
     179             :         case T_CreateOpFamilyStmt:
     180             :         case T_CreatePLangStmt:
     181             :         case T_CreatePolicyStmt:
     182             :         case T_CreatePublicationStmt:
     183             :         case T_CreateRangeStmt:
     184             :         case T_CreateRoleStmt:
     185             :         case T_CreateSchemaStmt:
     186             :         case T_CreateSeqStmt:
     187             :         case T_CreateStatsStmt:
     188             :         case T_CreateStmt:
     189             :         case T_CreateSubscriptionStmt:
     190             :         case T_CreateTableAsStmt:
     191             :         case T_CreateTableSpaceStmt:
     192             :         case T_CreateTransformStmt:
     193             :         case T_CreateTrigStmt:
     194             :         case T_CreateUserMappingStmt:
     195             :         case T_CreatedbStmt:
     196             :         case T_DefineStmt:
     197             :         case T_DropOwnedStmt:
     198             :         case T_DropRoleStmt:
     199             :         case T_DropStmt:
     200             :         case T_DropSubscriptionStmt:
     201             :         case T_DropTableSpaceStmt:
     202             :         case T_DropUserMappingStmt:
     203             :         case T_DropdbStmt:
     204             :         case T_GrantRoleStmt:
     205             :         case T_GrantStmt:
     206             :         case T_ImportForeignSchemaStmt:
     207             :         case T_IndexStmt:
     208             :         case T_ReassignOwnedStmt:
     209             :         case T_RefreshMatViewStmt:
     210             :         case T_RenameStmt:
     211             :         case T_RuleStmt:
     212             :         case T_SecLabelStmt:
     213             :         case T_TruncateStmt:
     214             :         case T_ViewStmt:
     215             :             {
     216             :                 /* DDL is not read-only, and neither is TRUNCATE. */
     217      252546 :                 return COMMAND_IS_NOT_READ_ONLY;
     218             :             }
     219             : 
     220         198 :         case T_AlterSystemStmt:
     221             :             {
     222             :                 /*
     223             :                  * Surprisingly, ALTER SYSTEM meets all our definitions of
     224             :                  * read-only: it changes nothing that affects the output of
     225             :                  * pg_dump, it doesn't write WAL or imperil the application of
     226             :                  * future WAL, and it doesn't depend on any state that needs
     227             :                  * to be synchronized with parallel workers.
     228             :                  *
     229             :                  * So, despite the fact that it writes to a file, it's read
     230             :                  * only!
     231             :                  */
     232         198 :                 return COMMAND_IS_STRICTLY_READ_ONLY;
     233             :             }
     234             : 
     235        2088 :         case T_CallStmt:
     236             :         case T_DoStmt:
     237             :             {
     238             :                 /*
     239             :                  * Commands inside the DO block or the called procedure might
     240             :                  * not be read only, but they'll be checked separately when we
     241             :                  * try to execute them.  Here we only need to worry about the
     242             :                  * DO or CALL command itself.
     243             :                  */
     244        2088 :                 return COMMAND_IS_STRICTLY_READ_ONLY;
     245             :             }
     246             : 
     247         886 :         case T_CheckPointStmt:
     248             :             {
     249             :                 /*
     250             :                  * You might think that this should not be permitted in
     251             :                  * recovery, but we interpret a CHECKPOINT command during
     252             :                  * recovery as a request for a restartpoint instead. We allow
     253             :                  * this since it can be a useful way of reducing switchover
     254             :                  * time when using various forms of replication.
     255             :                  */
     256         886 :                 return COMMAND_IS_STRICTLY_READ_ONLY;
     257             :             }
     258             : 
     259       74912 :         case T_ClosePortalStmt:
     260             :         case T_ConstraintsSetStmt:
     261             :         case T_DeallocateStmt:
     262             :         case T_DeclareCursorStmt:
     263             :         case T_DiscardStmt:
     264             :         case T_ExecuteStmt:
     265             :         case T_FetchStmt:
     266             :         case T_LoadStmt:
     267             :         case T_PrepareStmt:
     268             :         case T_UnlistenStmt:
     269             :         case T_VariableSetStmt:
     270             :         case T_WaitStmt:
     271             :             {
     272             :                 /*
     273             :                  * These modify only backend-local state, so they're OK to run
     274             :                  * in a read-only transaction or on a standby. However, they
     275             :                  * are disallowed in parallel mode, because they either rely
     276             :                  * upon or modify backend-local state that might not be
     277             :                  * synchronized among cooperating backends.
     278             :                  */
     279       74912 :                 return COMMAND_OK_IN_RECOVERY | COMMAND_OK_IN_READ_ONLY_TXN;
     280             :             }
     281             : 
     282       15294 :         case T_ClusterStmt:
     283             :         case T_ReindexStmt:
     284             :         case T_VacuumStmt:
     285             :             {
     286             :                 /*
     287             :                  * These commands write WAL, so they're not strictly
     288             :                  * read-only, and running them in parallel workers isn't
     289             :                  * supported.
     290             :                  *
     291             :                  * However, they don't change the database state in a way that
     292             :                  * would affect pg_dump output, so it's fine to run them in a
     293             :                  * read-only transaction. (CLUSTER might change the order of
     294             :                  * rows on disk, which could affect the ordering of pg_dump
     295             :                  * output, but that's not semantically significant.)
     296             :                  */
     297       15294 :                 return COMMAND_OK_IN_READ_ONLY_TXN;
     298             :             }
     299             : 
     300       11326 :         case T_CopyStmt:
     301             :             {
     302       11326 :                 CopyStmt   *stmt = (CopyStmt *) parsetree;
     303             : 
     304             :                 /*
     305             :                  * You might think that COPY FROM is not at all read only, but
     306             :                  * it's OK to copy into a temporary table, because that
     307             :                  * wouldn't change the output of pg_dump.  If the target table
     308             :                  * turns out to be non-temporary, DoCopy itself will call
     309             :                  * PreventCommandIfReadOnly.
     310             :                  */
     311       11326 :                 if (stmt->is_from)
     312        1874 :                     return COMMAND_OK_IN_READ_ONLY_TXN;
     313             :                 else
     314        9452 :                     return COMMAND_IS_STRICTLY_READ_ONLY;
     315             :             }
     316             : 
     317       25312 :         case T_ExplainStmt:
     318             :         case T_VariableShowStmt:
     319             :             {
     320             :                 /*
     321             :                  * These commands don't modify any data and are safe to run in
     322             :                  * a parallel worker.
     323             :                  */
     324       25312 :                 return COMMAND_IS_STRICTLY_READ_ONLY;
     325             :             }
     326             : 
     327         162 :         case T_ListenStmt:
     328             :         case T_NotifyStmt:
     329             :             {
     330             :                 /*
     331             :                  * NOTIFY requires an XID assignment, so it can't be permitted
     332             :                  * on a standby. Perhaps LISTEN could, since without NOTIFY it
     333             :                  * would be OK to just do nothing, at least until promotion,
     334             :                  * but we currently prohibit it lest the user get the wrong
     335             :                  * idea.
     336             :                  *
     337             :                  * (We do allow T_UnlistenStmt on a standby, though, because
     338             :                  * it's a no-op.)
     339             :                  */
     340         162 :                 return COMMAND_OK_IN_READ_ONLY_TXN;
     341             :             }
     342             : 
     343        1048 :         case T_LockStmt:
     344             :             {
     345        1048 :                 LockStmt   *stmt = (LockStmt *) parsetree;
     346             : 
     347             :                 /*
     348             :                  * Only weaker locker modes are allowed during recovery. The
     349             :                  * restrictions here must match those in
     350             :                  * LockAcquireExtended().
     351             :                  */
     352        1048 :                 if (stmt->mode > RowExclusiveLock)
     353         496 :                     return COMMAND_OK_IN_READ_ONLY_TXN;
     354             :                 else
     355         552 :                     return COMMAND_IS_STRICTLY_READ_ONLY;
     356             :             }
     357             : 
     358       36314 :         case T_TransactionStmt:
     359             :             {
     360       36314 :                 TransactionStmt *stmt = (TransactionStmt *) parsetree;
     361             : 
     362             :                 /*
     363             :                  * PREPARE, COMMIT PREPARED, and ROLLBACK PREPARED all write
     364             :                  * WAL, so they're not read-only in the strict sense; but the
     365             :                  * first and third do not change pg_dump output, so they're OK
     366             :                  * in a read-only transactions.
     367             :                  *
     368             :                  * We also consider COMMIT PREPARED to be OK in a read-only
     369             :                  * transaction environment, by way of exception.
     370             :                  */
     371       36314 :                 switch (stmt->kind)
     372             :                 {
     373       35042 :                     case TRANS_STMT_BEGIN:
     374             :                     case TRANS_STMT_START:
     375             :                     case TRANS_STMT_COMMIT:
     376             :                     case TRANS_STMT_ROLLBACK:
     377             :                     case TRANS_STMT_SAVEPOINT:
     378             :                     case TRANS_STMT_RELEASE:
     379             :                     case TRANS_STMT_ROLLBACK_TO:
     380       35042 :                         return COMMAND_IS_STRICTLY_READ_ONLY;
     381             : 
     382        1272 :                     case TRANS_STMT_PREPARE:
     383             :                     case TRANS_STMT_COMMIT_PREPARED:
     384             :                     case TRANS_STMT_ROLLBACK_PREPARED:
     385        1272 :                         return COMMAND_OK_IN_READ_ONLY_TXN;
     386             :                 }
     387           0 :                 elog(ERROR, "unrecognized TransactionStmtKind: %d",
     388             :                      (int) stmt->kind);
     389             :                 return 0;       /* silence stupider compilers */
     390             :             }
     391             : 
     392           0 :         default:
     393           0 :             elog(ERROR, "unrecognized node type: %d",
     394             :                  (int) nodeTag(parsetree));
     395             :             return 0;           /* silence stupider compilers */
     396             :     }
     397             : }
     398             : 
     399             : /*
     400             :  * PreventCommandIfReadOnly: throw error if XactReadOnly
     401             :  *
     402             :  * This is useful partly to ensure consistency of the error message wording;
     403             :  * some callers have checked XactReadOnly for themselves.
     404             :  */
     405             : void
     406      262210 : PreventCommandIfReadOnly(const char *cmdname)
     407             : {
     408      262210 :     if (XactReadOnly)
     409         100 :         ereport(ERROR,
     410             :                 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
     411             :         /* translator: %s is name of a SQL command, eg CREATE */
     412             :                  errmsg("cannot execute %s in a read-only transaction",
     413             :                         cmdname)));
     414      262110 : }
     415             : 
     416             : /*
     417             :  * PreventCommandIfParallelMode: throw error if current (sub)transaction is
     418             :  * in parallel mode.
     419             :  *
     420             :  * This is useful partly to ensure consistency of the error message wording;
     421             :  * some callers have checked IsInParallelMode() for themselves.
     422             :  */
     423             : void
     424      399436 : PreventCommandIfParallelMode(const char *cmdname)
     425             : {
     426      399436 :     if (IsInParallelMode())
     427           0 :         ereport(ERROR,
     428             :                 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
     429             :         /* translator: %s is name of a SQL command, eg CREATE */
     430             :                  errmsg("cannot execute %s during a parallel operation",
     431             :                         cmdname)));
     432      399436 : }
     433             : 
     434             : /*
     435             :  * PreventCommandDuringRecovery: throw error if RecoveryInProgress
     436             :  *
     437             :  * The majority of operations that are unsafe in a Hot Standby
     438             :  * will be rejected by XactReadOnly tests.  However there are a few
     439             :  * commands that are allowed in "read-only" xacts but cannot be allowed
     440             :  * in Hot Standby mode.  Those commands should call this function.
     441             :  */
     442             : void
     443        7850 : PreventCommandDuringRecovery(const char *cmdname)
     444             : {
     445        7850 :     if (RecoveryInProgress())
     446           0 :         ereport(ERROR,
     447             :                 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
     448             :         /* translator: %s is name of a SQL command, eg CREATE */
     449             :                  errmsg("cannot execute %s during recovery",
     450             :                         cmdname)));
     451        7850 : }
     452             : 
     453             : /*
     454             :  * CheckRestrictedOperation: throw error for hazardous command if we're
     455             :  * inside a security restriction context.
     456             :  *
     457             :  * This is needed to protect session-local state for which there is not any
     458             :  * better-defined protection mechanism, such as ownership.
     459             :  */
     460             : static void
     461        8420 : CheckRestrictedOperation(const char *cmdname)
     462             : {
     463        8420 :     if (InSecurityRestrictedOperation())
     464           0 :         ereport(ERROR,
     465             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     466             :         /* translator: %s is name of a SQL command, eg PREPARE */
     467             :                  errmsg("cannot execute %s within security-restricted operation",
     468             :                         cmdname)));
     469        8420 : }
     470             : 
     471             : /*
     472             :  * ProcessUtility
     473             :  *      general utility function invoker
     474             :  *
     475             :  *  pstmt: PlannedStmt wrapper for the utility statement
     476             :  *  queryString: original source text of command
     477             :  *  readOnlyTree: if true, pstmt's node tree must not be modified
     478             :  *  context: identifies source of statement (toplevel client command,
     479             :  *      non-toplevel client command, subcommand of a larger utility command)
     480             :  *  params: parameters to use during execution
     481             :  *  queryEnv: environment for parse through execution (e.g., ephemeral named
     482             :  *      tables like trigger transition tables).  May be NULL.
     483             :  *  dest: where to send results
     484             :  *  qc: where to store command completion status data.  May be NULL,
     485             :  *      but if not, then caller must have initialized it.
     486             :  *
     487             :  * Caller MUST supply a queryString; it is not allowed (anymore) to pass NULL.
     488             :  * If you really don't have source text, you can pass a constant string,
     489             :  * perhaps "(query not available)".
     490             :  *
     491             :  * Note for users of ProcessUtility_hook: the same queryString may be passed
     492             :  * to multiple invocations of ProcessUtility when processing a query string
     493             :  * containing multiple semicolon-separated statements.  One should use
     494             :  * pstmt->stmt_location and pstmt->stmt_len to identify the substring
     495             :  * containing the current statement.  Keep in mind also that some utility
     496             :  * statements (e.g., CREATE SCHEMA) will recurse to ProcessUtility to process
     497             :  * sub-statements, often passing down the same queryString, stmt_location,
     498             :  * and stmt_len that were given for the whole statement.
     499             :  */
     500             : void
     501      420086 : ProcessUtility(PlannedStmt *pstmt,
     502             :                const char *queryString,
     503             :                bool readOnlyTree,
     504             :                ProcessUtilityContext context,
     505             :                ParamListInfo params,
     506             :                QueryEnvironment *queryEnv,
     507             :                DestReceiver *dest,
     508             :                QueryCompletion *qc)
     509             : {
     510             :     Assert(IsA(pstmt, PlannedStmt));
     511             :     Assert(pstmt->commandType == CMD_UTILITY);
     512             :     Assert(queryString != NULL);    /* required as of 8.4 */
     513             :     Assert(qc == NULL || qc->commandTag == CMDTAG_UNKNOWN);
     514             : 
     515             :     /*
     516             :      * We provide a function hook variable that lets loadable plugins get
     517             :      * control when ProcessUtility is called.  Such a plugin would normally
     518             :      * call standard_ProcessUtility().
     519             :      */
     520      420086 :     if (ProcessUtility_hook)
     521       69728 :         (*ProcessUtility_hook) (pstmt, queryString, readOnlyTree,
     522             :                                 context, params, queryEnv,
     523             :                                 dest, qc);
     524             :     else
     525      350358 :         standard_ProcessUtility(pstmt, queryString, readOnlyTree,
     526             :                                 context, params, queryEnv,
     527             :                                 dest, qc);
     528      403562 : }
     529             : 
     530             : /*
     531             :  * standard_ProcessUtility itself deals only with utility commands for
     532             :  * which we do not provide event trigger support.  Commands that do have
     533             :  * such support are passed down to ProcessUtilitySlow, which contains the
     534             :  * necessary infrastructure for such triggers.
     535             :  *
     536             :  * This division is not just for performance: it's critical that the
     537             :  * event trigger code not be invoked when doing START TRANSACTION for
     538             :  * example, because we might need to refresh the event trigger cache,
     539             :  * which requires being in a valid transaction.
     540             :  *
     541             :  * When adding or moving utility commands, check that the documentation in
     542             :  * event-trigger.sgml is kept up to date.
     543             :  */
     544             : void
     545      420086 : standard_ProcessUtility(PlannedStmt *pstmt,
     546             :                         const char *queryString,
     547             :                         bool readOnlyTree,
     548             :                         ProcessUtilityContext context,
     549             :                         ParamListInfo params,
     550             :                         QueryEnvironment *queryEnv,
     551             :                         DestReceiver *dest,
     552             :                         QueryCompletion *qc)
     553             : {
     554             :     Node       *parsetree;
     555      420086 :     bool        isTopLevel = (context == PROCESS_UTILITY_TOPLEVEL);
     556      420086 :     bool        isAtomicContext = (!(context == PROCESS_UTILITY_TOPLEVEL || context == PROCESS_UTILITY_QUERY_NONATOMIC) || IsTransactionBlock());
     557             :     ParseState *pstate;
     558             :     int         readonly_flags;
     559             : 
     560             :     /* This can recurse, so check for excessive recursion */
     561      420086 :     check_stack_depth();
     562             : 
     563             :     /*
     564             :      * If the given node tree is read-only, make a copy to ensure that parse
     565             :      * transformations don't damage the original tree.  This could be
     566             :      * refactored to avoid making unnecessary copies in more cases, but it's
     567             :      * not clear that it's worth a great deal of trouble over.  Statements
     568             :      * that are complex enough to be expensive to copy are exactly the ones
     569             :      * we'd need to copy, so that only marginal savings seem possible.
     570             :      */
     571      420086 :     if (readOnlyTree)
     572       33080 :         pstmt = copyObject(pstmt);
     573      420086 :     parsetree = pstmt->utilityStmt;
     574             : 
     575             :     /* Prohibit read/write commands in read-only states. */
     576      420086 :     readonly_flags = ClassifyUtilityCommandAsReadOnly(parsetree);
     577      420086 :     if (readonly_flags != COMMAND_IS_STRICTLY_READ_ONLY &&
     578      346556 :         (XactReadOnly || IsInParallelMode()))
     579             :     {
     580       16804 :         CommandTag  commandtag = CreateCommandTag(parsetree);
     581             : 
     582       16804 :         if ((readonly_flags & COMMAND_OK_IN_READ_ONLY_TXN) == 0)
     583          12 :             PreventCommandIfReadOnly(GetCommandTagName(commandtag));
     584       16792 :         if ((readonly_flags & COMMAND_OK_IN_PARALLEL_MODE) == 0)
     585       16792 :             PreventCommandIfParallelMode(GetCommandTagName(commandtag));
     586       16792 :         if ((readonly_flags & COMMAND_OK_IN_RECOVERY) == 0)
     587           0 :             PreventCommandDuringRecovery(GetCommandTagName(commandtag));
     588             :     }
     589             : 
     590      420074 :     pstate = make_parsestate(NULL);
     591      420074 :     pstate->p_sourcetext = queryString;
     592      420074 :     pstate->p_queryEnv = queryEnv;
     593             : 
     594      420074 :     switch (nodeTag(parsetree))
     595             :     {
     596             :             /*
     597             :              * ******************** transactions ********************
     598             :              */
     599       36314 :         case T_TransactionStmt:
     600             :             {
     601       36314 :                 TransactionStmt *stmt = (TransactionStmt *) parsetree;
     602             : 
     603       36314 :                 switch (stmt->kind)
     604             :                 {
     605             :                         /*
     606             :                          * START TRANSACTION, as defined by SQL99: Identical
     607             :                          * to BEGIN.  Same code for both.
     608             :                          */
     609       16570 :                     case TRANS_STMT_BEGIN:
     610             :                     case TRANS_STMT_START:
     611             :                         {
     612             :                             ListCell   *lc;
     613             : 
     614       16570 :                             BeginTransactionBlock();
     615       24062 :                             foreach(lc, stmt->options)
     616             :                             {
     617        7492 :                                 DefElem    *item = (DefElem *) lfirst(lc);
     618             : 
     619        7492 :                                 if (strcmp(item->defname, "transaction_isolation") == 0)
     620        6496 :                                     SetPGVariable("transaction_isolation",
     621        6496 :                                                   list_make1(item->arg),
     622             :                                                   true);
     623         996 :                                 else if (strcmp(item->defname, "transaction_read_only") == 0)
     624         948 :                                     SetPGVariable("transaction_read_only",
     625         948 :                                                   list_make1(item->arg),
     626             :                                                   true);
     627          48 :                                 else if (strcmp(item->defname, "transaction_deferrable") == 0)
     628          48 :                                     SetPGVariable("transaction_deferrable",
     629          48 :                                                   list_make1(item->arg),
     630             :                                                   true);
     631             :                             }
     632             :                         }
     633       16570 :                         break;
     634             : 
     635       12580 :                     case TRANS_STMT_COMMIT:
     636       12580 :                         if (!EndTransactionBlock(stmt->chain))
     637             :                         {
     638             :                             /* report unsuccessful commit in qc */
     639         812 :                             if (qc)
     640         812 :                                 SetQueryCompletion(qc, CMDTAG_ROLLBACK, 0);
     641             :                         }
     642       12550 :                         break;
     643             : 
     644         676 :                     case TRANS_STMT_PREPARE:
     645         676 :                         if (!PrepareTransactionBlock(stmt->gid))
     646             :                         {
     647             :                             /* report unsuccessful commit in qc */
     648           4 :                             if (qc)
     649           4 :                                 SetQueryCompletion(qc, CMDTAG_ROLLBACK, 0);
     650             :                         }
     651         676 :                         break;
     652             : 
     653         520 :                     case TRANS_STMT_COMMIT_PREPARED:
     654         520 :                         PreventInTransactionBlock(isTopLevel, "COMMIT PREPARED");
     655         520 :                         FinishPreparedTransaction(stmt->gid, true);
     656         514 :                         break;
     657             : 
     658          76 :                     case TRANS_STMT_ROLLBACK_PREPARED:
     659          76 :                         PreventInTransactionBlock(isTopLevel, "ROLLBACK PREPARED");
     660          76 :                         FinishPreparedTransaction(stmt->gid, false);
     661          70 :                         break;
     662             : 
     663        2924 :                     case TRANS_STMT_ROLLBACK:
     664        2924 :                         UserAbortTransactionBlock(stmt->chain);
     665        2894 :                         break;
     666             : 
     667        1958 :                     case TRANS_STMT_SAVEPOINT:
     668        1958 :                         RequireTransactionBlock(isTopLevel, "SAVEPOINT");
     669        1944 :                         DefineSavepoint(stmt->savepoint_name);
     670        1932 :                         break;
     671             : 
     672         288 :                     case TRANS_STMT_RELEASE:
     673         288 :                         RequireTransactionBlock(isTopLevel, "RELEASE SAVEPOINT");
     674         282 :                         ReleaseSavepoint(stmt->savepoint_name);
     675         276 :                         break;
     676             : 
     677         722 :                     case TRANS_STMT_ROLLBACK_TO:
     678         722 :                         RequireTransactionBlock(isTopLevel, "ROLLBACK TO SAVEPOINT");
     679         716 :                         RollbackToSavepoint(stmt->savepoint_name);
     680             : 
     681             :                         /*
     682             :                          * CommitTransactionCommand is in charge of
     683             :                          * re-defining the savepoint again
     684             :                          */
     685         704 :                         break;
     686             :                 }
     687             :             }
     688       36186 :             break;
     689             : 
     690             :             /*
     691             :              * Portal (cursor) manipulation
     692             :              */
     693        4500 :         case T_DeclareCursorStmt:
     694        4500 :             PerformCursorOpen(pstate, (DeclareCursorStmt *) parsetree, params,
     695             :                               isTopLevel);
     696        4486 :             break;
     697             : 
     698        2246 :         case T_ClosePortalStmt:
     699             :             {
     700        2246 :                 ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
     701             : 
     702        2246 :                 CheckRestrictedOperation("CLOSE");
     703        2246 :                 PerformPortalClose(stmt->portalname);
     704             :             }
     705        2244 :             break;
     706             : 
     707        7712 :         case T_FetchStmt:
     708        7712 :             PerformPortalFetch((FetchStmt *) parsetree, dest, qc);
     709        7610 :             break;
     710             : 
     711        1596 :         case T_DoStmt:
     712        1596 :             ExecuteDoStmt(pstate, (DoStmt *) parsetree, isAtomicContext);
     713        1188 :             break;
     714             : 
     715         130 :         case T_CreateTableSpaceStmt:
     716             :             /* no event triggers for global objects */
     717         130 :             PreventInTransactionBlock(isTopLevel, "CREATE TABLESPACE");
     718         130 :             CreateTableSpace((CreateTableSpaceStmt *) parsetree);
     719         100 :             break;
     720             : 
     721          64 :         case T_DropTableSpaceStmt:
     722             :             /* no event triggers for global objects */
     723          64 :             PreventInTransactionBlock(isTopLevel, "DROP TABLESPACE");
     724          64 :             DropTableSpace((DropTableSpaceStmt *) parsetree);
     725          50 :             break;
     726             : 
     727          24 :         case T_AlterTableSpaceOptionsStmt:
     728             :             /* no event triggers for global objects */
     729          24 :             AlterTableSpaceOptions((AlterTableSpaceOptionsStmt *) parsetree);
     730          12 :             break;
     731             : 
     732        1774 :         case T_TruncateStmt:
     733        1774 :             ExecuteTruncate((TruncateStmt *) parsetree);
     734        1636 :             break;
     735             : 
     736       11326 :         case T_CopyStmt:
     737             :             {
     738             :                 uint64      processed;
     739             : 
     740       11326 :                 DoCopy(pstate, (CopyStmt *) parsetree,
     741             :                        pstmt->stmt_location, pstmt->stmt_len,
     742             :                        &processed);
     743       10372 :                 if (qc)
     744       10372 :                     SetQueryCompletion(qc, CMDTAG_COPY, processed);
     745             :             }
     746       10372 :             break;
     747             : 
     748        1936 :         case T_PrepareStmt:
     749        1936 :             CheckRestrictedOperation("PREPARE");
     750        1936 :             PrepareQuery(pstate, (PrepareStmt *) parsetree,
     751             :                          pstmt->stmt_location, pstmt->stmt_len);
     752        1924 :             break;
     753             : 
     754       15734 :         case T_ExecuteStmt:
     755       15734 :             ExecuteQuery(pstate,
     756             :                          (ExecuteStmt *) parsetree, NULL,
     757             :                          params,
     758             :                          dest, qc);
     759       15630 :             break;
     760             : 
     761        4096 :         case T_DeallocateStmt:
     762        4096 :             CheckRestrictedOperation("DEALLOCATE");
     763        4096 :             DeallocateQuery((DeallocateStmt *) parsetree);
     764        4096 :             break;
     765             : 
     766         922 :         case T_GrantRoleStmt:
     767             :             /* no event triggers for global objects */
     768         922 :             GrantRole(pstate, (GrantRoleStmt *) parsetree);
     769         796 :             break;
     770             : 
     771         806 :         case T_CreatedbStmt:
     772             :             /* no event triggers for global objects */
     773         806 :             PreventInTransactionBlock(isTopLevel, "CREATE DATABASE");
     774         806 :             createdb(pstate, (CreatedbStmt *) parsetree);
     775         768 :             break;
     776             : 
     777          82 :         case T_AlterDatabaseStmt:
     778             :             /* no event triggers for global objects */
     779          82 :             AlterDatabase(pstate, (AlterDatabaseStmt *) parsetree, isTopLevel);
     780          80 :             break;
     781             : 
     782           6 :         case T_AlterDatabaseRefreshCollStmt:
     783             :             /* no event triggers for global objects */
     784           6 :             AlterDatabaseRefreshColl((AlterDatabaseRefreshCollStmt *) parsetree);
     785           6 :             break;
     786             : 
     787        1242 :         case T_AlterDatabaseSetStmt:
     788             :             /* no event triggers for global objects */
     789        1242 :             AlterDatabaseSet((AlterDatabaseSetStmt *) parsetree);
     790        1238 :             break;
     791             : 
     792         124 :         case T_DropdbStmt:
     793             :             /* no event triggers for global objects */
     794         124 :             PreventInTransactionBlock(isTopLevel, "DROP DATABASE");
     795         124 :             DropDatabase(pstate, (DropdbStmt *) parsetree);
     796         106 :             break;
     797             : 
     798             :             /* Query-level asynchronous notification */
     799          88 :         case T_NotifyStmt:
     800             :             {
     801          88 :                 NotifyStmt *stmt = (NotifyStmt *) parsetree;
     802             : 
     803          88 :                 Async_Notify(stmt->conditionname, stmt->payload);
     804             :             }
     805          88 :             break;
     806             : 
     807          74 :         case T_ListenStmt:
     808             :             {
     809          74 :                 ListenStmt *stmt = (ListenStmt *) parsetree;
     810             : 
     811          74 :                 CheckRestrictedOperation("LISTEN");
     812             : 
     813             :                 /*
     814             :                  * We don't allow LISTEN in background processes, as there is
     815             :                  * no mechanism for them to collect NOTIFY messages, so they'd
     816             :                  * just block cleanout of the async SLRU indefinitely.
     817             :                  * (Authors of custom background workers could bypass this
     818             :                  * restriction by calling Async_Listen directly, but then it's
     819             :                  * on them to provide some mechanism to process the message
     820             :                  * queue.)  Note there seems no reason to forbid UNLISTEN.
     821             :                  */
     822          74 :                 if (MyBackendType != B_BACKEND)
     823           0 :                     ereport(ERROR,
     824             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     825             :                     /* translator: %s is name of a SQL command, eg LISTEN */
     826             :                              errmsg("cannot execute %s within a background process",
     827             :                                     "LISTEN")));
     828             : 
     829          74 :                 Async_Listen(stmt->conditionname);
     830             :             }
     831          74 :             break;
     832             : 
     833          38 :         case T_UnlistenStmt:
     834             :             {
     835          38 :                 UnlistenStmt *stmt = (UnlistenStmt *) parsetree;
     836             : 
     837          38 :                 CheckRestrictedOperation("UNLISTEN");
     838          38 :                 if (stmt->conditionname)
     839           6 :                     Async_Unlisten(stmt->conditionname);
     840             :                 else
     841          32 :                     Async_UnlistenAll();
     842             :             }
     843          38 :             break;
     844             : 
     845          64 :         case T_LoadStmt:
     846             :             {
     847          64 :                 LoadStmt   *stmt = (LoadStmt *) parsetree;
     848             : 
     849          64 :                 closeAllVfds(); /* probably not necessary... */
     850             :                 /* Allowed names are restricted if you're not superuser */
     851          64 :                 load_file(stmt->filename, !superuser());
     852             :             }
     853          64 :             break;
     854             : 
     855         492 :         case T_CallStmt:
     856         492 :             ExecuteCallStmt(castNode(CallStmt, parsetree), params, isAtomicContext, dest);
     857         448 :             break;
     858             : 
     859         242 :         case T_ClusterStmt:
     860         242 :             cluster(pstate, (ClusterStmt *) parsetree, isTopLevel);
     861         212 :             break;
     862             : 
     863       13934 :         case T_VacuumStmt:
     864       13934 :             ExecVacuum(pstate, (VacuumStmt *) parsetree, isTopLevel);
     865       13736 :             break;
     866             : 
     867       24450 :         case T_ExplainStmt:
     868       24450 :             ExplainQuery(pstate, (ExplainStmt *) parsetree, params, dest);
     869       24318 :             break;
     870             : 
     871         198 :         case T_AlterSystemStmt:
     872         198 :             PreventInTransactionBlock(isTopLevel, "ALTER SYSTEM");
     873         198 :             AlterSystemSetConfigFile((AlterSystemStmt *) parsetree);
     874         150 :             break;
     875             : 
     876       38402 :         case T_VariableSetStmt:
     877       38402 :             ExecSetVariableStmt((VariableSetStmt *) parsetree, isTopLevel);
     878       38208 :             break;
     879             : 
     880         862 :         case T_VariableShowStmt:
     881             :             {
     882         862 :                 VariableShowStmt *n = (VariableShowStmt *) parsetree;
     883             : 
     884         862 :                 GetPGVariable(n->name, dest);
     885             :             }
     886         862 :             break;
     887             : 
     888          30 :         case T_DiscardStmt:
     889             :             /* should we allow DISCARD PLANS? */
     890          30 :             CheckRestrictedOperation("DISCARD");
     891          30 :             DiscardCommand((DiscardStmt *) parsetree, isTopLevel);
     892          30 :             break;
     893             : 
     894         200 :         case T_CreateEventTrigStmt:
     895             :             /* no event triggers on event triggers */
     896         200 :             CreateEventTrigger((CreateEventTrigStmt *) parsetree);
     897         134 :             break;
     898             : 
     899          48 :         case T_AlterEventTrigStmt:
     900             :             /* no event triggers on event triggers */
     901          48 :             AlterEventTrigger((AlterEventTrigStmt *) parsetree);
     902          48 :             break;
     903             : 
     904             :             /*
     905             :              * ******************************** ROLE statements ****
     906             :              */
     907        1874 :         case T_CreateRoleStmt:
     908             :             /* no event triggers for global objects */
     909        1874 :             CreateRole(pstate, (CreateRoleStmt *) parsetree);
     910        1738 :             break;
     911             : 
     912         476 :         case T_AlterRoleStmt:
     913             :             /* no event triggers for global objects */
     914         476 :             AlterRole(pstate, (AlterRoleStmt *) parsetree);
     915         386 :             break;
     916             : 
     917          94 :         case T_AlterRoleSetStmt:
     918             :             /* no event triggers for global objects */
     919          94 :             AlterRoleSet((AlterRoleSetStmt *) parsetree);
     920          82 :             break;
     921             : 
     922        1724 :         case T_DropRoleStmt:
     923             :             /* no event triggers for global objects */
     924        1724 :             DropRole((DropRoleStmt *) parsetree);
     925        1492 :             break;
     926             : 
     927          46 :         case T_ReassignOwnedStmt:
     928             :             /* no event triggers for global objects */
     929          46 :             ReassignOwnedObjects((ReassignOwnedStmt *) parsetree);
     930          28 :             break;
     931             : 
     932        1048 :         case T_LockStmt:
     933             : 
     934             :             /*
     935             :              * Since the lock would just get dropped immediately, LOCK TABLE
     936             :              * outside a transaction block is presumed to be user error.
     937             :              */
     938        1048 :             RequireTransactionBlock(isTopLevel, "LOCK TABLE");
     939        1040 :             LockTableCommand((LockStmt *) parsetree);
     940         964 :             break;
     941             : 
     942         102 :         case T_ConstraintsSetStmt:
     943         102 :             WarnNoTransactionBlock(isTopLevel, "SET CONSTRAINTS");
     944         102 :             AfterTriggerSetState((ConstraintsSetStmt *) parsetree);
     945          86 :             break;
     946             : 
     947         886 :         case T_CheckPointStmt:
     948         886 :             ExecCheckpoint(pstate, (CheckPointStmt *) parsetree);
     949         874 :             break;
     950             : 
     951             :             /*
     952             :              * The following statements are supported by Event Triggers only
     953             :              * in some cases, so we "fast path" them in the other cases.
     954             :              */
     955             : 
     956       29592 :         case T_GrantStmt:
     957             :             {
     958       29592 :                 GrantStmt  *stmt = (GrantStmt *) parsetree;
     959             : 
     960       29592 :                 if (EventTriggerSupportsObjectType(stmt->objtype))
     961       29164 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
     962             :                                        context, params, queryEnv,
     963             :                                        dest, qc);
     964             :                 else
     965         428 :                     ExecuteGrantStmt(stmt);
     966             :             }
     967       29452 :             break;
     968             : 
     969       26380 :         case T_DropStmt:
     970             :             {
     971       26380 :                 DropStmt   *stmt = (DropStmt *) parsetree;
     972             : 
     973       26380 :                 if (EventTriggerSupportsObjectType(stmt->removeType))
     974       26256 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
     975             :                                        context, params, queryEnv,
     976             :                                        dest, qc);
     977             :                 else
     978         124 :                     ExecDropStmt(stmt, isTopLevel);
     979             :             }
     980       25380 :             break;
     981             : 
     982        1546 :         case T_RenameStmt:
     983             :             {
     984        1546 :                 RenameStmt *stmt = (RenameStmt *) parsetree;
     985             : 
     986        1546 :                 if (EventTriggerSupportsObjectType(stmt->renameType))
     987        1484 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
     988             :                                        context, params, queryEnv,
     989             :                                        dest, qc);
     990             :                 else
     991          62 :                     ExecRenameStmt(stmt);
     992             :             }
     993        1148 :             break;
     994             : 
     995          46 :         case T_AlterObjectDependsStmt:
     996             :             {
     997          46 :                 AlterObjectDependsStmt *stmt = (AlterObjectDependsStmt *) parsetree;
     998             : 
     999          46 :                 if (EventTriggerSupportsObjectType(stmt->objectType))
    1000          46 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
    1001             :                                        context, params, queryEnv,
    1002             :                                        dest, qc);
    1003             :                 else
    1004           0 :                     ExecAlterObjectDependsStmt(stmt, NULL);
    1005             :             }
    1006          46 :             break;
    1007             : 
    1008         398 :         case T_AlterObjectSchemaStmt:
    1009             :             {
    1010         398 :                 AlterObjectSchemaStmt *stmt = (AlterObjectSchemaStmt *) parsetree;
    1011             : 
    1012         398 :                 if (EventTriggerSupportsObjectType(stmt->objectType))
    1013         398 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
    1014             :                                        context, params, queryEnv,
    1015             :                                        dest, qc);
    1016             :                 else
    1017           0 :                     ExecAlterObjectSchemaStmt(stmt, NULL);
    1018             :             }
    1019         266 :             break;
    1020             : 
    1021        1672 :         case T_AlterOwnerStmt:
    1022             :             {
    1023        1672 :                 AlterOwnerStmt *stmt = (AlterOwnerStmt *) parsetree;
    1024             : 
    1025        1672 :                 if (EventTriggerSupportsObjectType(stmt->objectType))
    1026        1566 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
    1027             :                                        context, params, queryEnv,
    1028             :                                        dest, qc);
    1029             :                 else
    1030         106 :                     ExecAlterOwnerStmt(stmt);
    1031             :             }
    1032        1418 :             break;
    1033             : 
    1034        7214 :         case T_CommentStmt:
    1035             :             {
    1036        7214 :                 CommentStmt *stmt = (CommentStmt *) parsetree;
    1037             : 
    1038        7214 :                 if (EventTriggerSupportsObjectType(stmt->objtype))
    1039        6958 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
    1040             :                                        context, params, queryEnv,
    1041             :                                        dest, qc);
    1042             :                 else
    1043         256 :                     CommentObject(stmt);
    1044        7056 :                 break;
    1045             :             }
    1046             : 
    1047         118 :         case T_SecLabelStmt:
    1048             :             {
    1049         118 :                 SecLabelStmt *stmt = (SecLabelStmt *) parsetree;
    1050             : 
    1051         118 :                 if (EventTriggerSupportsObjectType(stmt->objtype))
    1052          80 :                     ProcessUtilitySlow(pstate, pstmt, queryString,
    1053             :                                        context, params, queryEnv,
    1054             :                                        dest, qc);
    1055             :                 else
    1056          38 :                     ExecSecLabelStmt(stmt);
    1057          46 :                 break;
    1058             :             }
    1059             : 
    1060          52 :         case T_WaitStmt:
    1061             :             {
    1062          52 :                 ExecWaitStmt(pstate, (WaitStmt *) parsetree, dest);
    1063             :             }
    1064          28 :             break;
    1065             : 
    1066      177050 :         default:
    1067             :             /* All other statement types have event trigger support */
    1068      177050 :             ProcessUtilitySlow(pstate, pstmt, queryString,
    1069             :                                context, params, queryEnv,
    1070             :                                dest, qc);
    1071      166134 :             break;
    1072             :     }
    1073             : 
    1074      403562 :     free_parsestate(pstate);
    1075             : 
    1076             :     /*
    1077             :      * Make effects of commands visible, for instance so that
    1078             :      * PreCommit_on_commit_actions() can see them (see for example bug
    1079             :      * #15631).
    1080             :      */
    1081      403562 :     CommandCounterIncrement();
    1082      403562 : }
    1083             : 
    1084             : /*
    1085             :  * The "Slow" variant of ProcessUtility should only receive statements
    1086             :  * supported by the event triggers facility.  Therefore, we always
    1087             :  * perform the trigger support calls if the context allows it.
    1088             :  */
    1089             : static void
    1090      243002 : ProcessUtilitySlow(ParseState *pstate,
    1091             :                    PlannedStmt *pstmt,
    1092             :                    const char *queryString,
    1093             :                    ProcessUtilityContext context,
    1094             :                    ParamListInfo params,
    1095             :                    QueryEnvironment *queryEnv,
    1096             :                    DestReceiver *dest,
    1097             :                    QueryCompletion *qc)
    1098             : {
    1099      243002 :     Node       *parsetree = pstmt->utilityStmt;
    1100      243002 :     bool        isTopLevel = (context == PROCESS_UTILITY_TOPLEVEL);
    1101      243002 :     bool        isCompleteQuery = (context != PROCESS_UTILITY_SUBCOMMAND);
    1102             :     bool        needCleanup;
    1103      243002 :     bool        commandCollected = false;
    1104             :     ObjectAddress address;
    1105      243002 :     ObjectAddress secondaryObject = InvalidObjectAddress;
    1106             : 
    1107             :     /* All event trigger calls are done only when isCompleteQuery is true */
    1108      243002 :     needCleanup = isCompleteQuery && EventTriggerBeginCompleteQuery();
    1109             : 
    1110             :     /* PG_TRY block is to ensure we call EventTriggerEndCompleteQuery */
    1111      243002 :     PG_TRY();
    1112             :     {
    1113      243002 :         if (isCompleteQuery)
    1114      230496 :             EventTriggerDDLCommandStart(parsetree);
    1115             : 
    1116      243002 :         switch (nodeTag(parsetree))
    1117             :         {
    1118             :                 /*
    1119             :                  * relation and attribute manipulation
    1120             :                  */
    1121        1078 :             case T_CreateSchemaStmt:
    1122        1078 :                 CreateSchemaCommand((CreateSchemaStmt *) parsetree,
    1123             :                                     queryString,
    1124             :                                     pstmt->stmt_location,
    1125             :                                     pstmt->stmt_len);
    1126             : 
    1127             :                 /*
    1128             :                  * EventTriggerCollectSimpleCommand called by
    1129             :                  * CreateSchemaCommand
    1130             :                  */
    1131         948 :                 commandCollected = true;
    1132         948 :                 break;
    1133             : 
    1134       38794 :             case T_CreateStmt:
    1135             :             case T_CreateForeignTableStmt:
    1136             :                 {
    1137             :                     List       *stmts;
    1138       38794 :                     RangeVar   *table_rv = NULL;
    1139             : 
    1140             :                     /* Run parse analysis ... */
    1141       38794 :                     stmts = transformCreateStmt((CreateStmt *) parsetree,
    1142             :                                                 queryString);
    1143             : 
    1144             :                     /*
    1145             :                      * ... and do it.  We can't use foreach() because we may
    1146             :                      * modify the list midway through, so pick off the
    1147             :                      * elements one at a time, the hard way.
    1148             :                      */
    1149       86658 :                     while (stmts != NIL)
    1150             :                     {
    1151       50042 :                         Node       *stmt = (Node *) linitial(stmts);
    1152             : 
    1153       50042 :                         stmts = list_delete_first(stmts);
    1154             : 
    1155       50042 :                         if (IsA(stmt, CreateStmt))
    1156             :                         {
    1157       37968 :                             CreateStmt *cstmt = (CreateStmt *) stmt;
    1158             :                             Datum       toast_options;
    1159       37968 :                             const char *const validnsps[] = HEAP_RELOPT_NAMESPACES;
    1160             : 
    1161             :                             /* Remember transformed RangeVar for LIKE */
    1162       37968 :                             table_rv = cstmt->relation;
    1163             : 
    1164             :                             /* Create the table itself */
    1165       37968 :                             address = DefineRelation(cstmt,
    1166             :                                                      RELKIND_RELATION,
    1167             :                                                      InvalidOid, NULL,
    1168             :                                                      queryString);
    1169       36650 :                             EventTriggerCollectSimpleCommand(address,
    1170             :                                                              secondaryObject,
    1171             :                                                              stmt);
    1172             : 
    1173             :                             /*
    1174             :                              * Let NewRelationCreateToastTable decide if this
    1175             :                              * one needs a secondary relation too.
    1176             :                              */
    1177       36650 :                             CommandCounterIncrement();
    1178             : 
    1179             :                             /*
    1180             :                              * parse and validate reloptions for the toast
    1181             :                              * table
    1182             :                              */
    1183       36650 :                             toast_options = transformRelOptions((Datum) 0,
    1184             :                                                                 cstmt->options,
    1185             :                                                                 "toast",
    1186             :                                                                 validnsps,
    1187             :                                                                 true,
    1188             :                                                                 false);
    1189       36650 :                             (void) heap_reloptions(RELKIND_TOASTVALUE,
    1190             :                                                    toast_options,
    1191             :                                                    true);
    1192             : 
    1193       36644 :                             NewRelationCreateToastTable(address.objectId,
    1194             :                                                         toast_options);
    1195             :                         }
    1196       12074 :                         else if (IsA(stmt, CreateForeignTableStmt))
    1197             :                         {
    1198         490 :                             CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) stmt;
    1199             : 
    1200             :                             /* Remember transformed RangeVar for LIKE */
    1201         490 :                             table_rv = cstmt->base.relation;
    1202             : 
    1203             :                             /* Create the table itself */
    1204         490 :                             address = DefineRelation(&cstmt->base,
    1205             :                                                      RELKIND_FOREIGN_TABLE,
    1206             :                                                      InvalidOid, NULL,
    1207             :                                                      queryString);
    1208         464 :                             CreateForeignTable(cstmt,
    1209             :                                                address.objectId);
    1210         394 :                             EventTriggerCollectSimpleCommand(address,
    1211             :                                                              secondaryObject,
    1212             :                                                              stmt);
    1213             :                         }
    1214       11584 :                         else if (IsA(stmt, TableLikeClause))
    1215             :                         {
    1216             :                             /*
    1217             :                              * Do delayed processing of LIKE options.  This
    1218             :                              * will result in additional sub-statements for us
    1219             :                              * to process.  Those should get done before any
    1220             :                              * remaining actions, so prepend them to "stmts".
    1221             :                              */
    1222         194 :                             TableLikeClause *like = (TableLikeClause *) stmt;
    1223             :                             List       *morestmts;
    1224             : 
    1225             :                             Assert(table_rv != NULL);
    1226             : 
    1227         194 :                             morestmts = expandTableLikeClause(table_rv, like);
    1228         194 :                             stmts = list_concat(morestmts, stmts);
    1229             :                         }
    1230             :                         else
    1231             :                         {
    1232             :                             /*
    1233             :                              * Recurse for anything else.  Note the recursive
    1234             :                              * call will stash the objects so created into our
    1235             :                              * event trigger context.
    1236             :                              */
    1237             :                             PlannedStmt *wrapper;
    1238             : 
    1239       11390 :                             wrapper = makeNode(PlannedStmt);
    1240       11390 :                             wrapper->commandType = CMD_UTILITY;
    1241       11390 :                             wrapper->canSetTag = false;
    1242       11390 :                             wrapper->utilityStmt = stmt;
    1243       11390 :                             wrapper->stmt_location = pstmt->stmt_location;
    1244       11390 :                             wrapper->stmt_len = pstmt->stmt_len;
    1245       11390 :                             wrapper->planOrigin = PLAN_STMT_INTERNAL;
    1246             : 
    1247       11390 :                             ProcessUtility(wrapper,
    1248             :                                            queryString,
    1249             :                                            false,
    1250             :                                            PROCESS_UTILITY_SUBCOMMAND,
    1251             :                                            params,
    1252             :                                            NULL,
    1253             :                                            None_Receiver,
    1254             :                                            NULL);
    1255             :                         }
    1256             : 
    1257             :                         /* Need CCI between commands */
    1258       48186 :                         if (stmts != NIL)
    1259       11578 :                             CommandCounterIncrement();
    1260             :                     }
    1261             : 
    1262             :                     /*
    1263             :                      * The multiple commands generated here are stashed
    1264             :                      * individually, so disable collection below.
    1265             :                      */
    1266       36616 :                     commandCollected = true;
    1267             :                 }
    1268       36616 :                 break;
    1269             : 
    1270       32352 :             case T_AlterTableStmt:
    1271             :                 {
    1272       32352 :                     AlterTableStmt *atstmt = (AlterTableStmt *) parsetree;
    1273             :                     Oid         relid;
    1274             :                     LOCKMODE    lockmode;
    1275             :                     ListCell   *cell;
    1276             : 
    1277             :                     /*
    1278             :                      * Disallow ALTER TABLE .. DETACH CONCURRENTLY in a
    1279             :                      * transaction block or function.  (Perhaps it could be
    1280             :                      * allowed in a procedure, but don't hold your breath.)
    1281             :                      */
    1282       65894 :                     foreach(cell, atstmt->cmds)
    1283             :                     {
    1284       33548 :                         AlterTableCmd *cmd = (AlterTableCmd *) lfirst(cell);
    1285             : 
    1286             :                         /* Disallow DETACH CONCURRENTLY in a transaction block */
    1287       33548 :                         if (cmd->subtype == AT_DetachPartition)
    1288             :                         {
    1289         602 :                             if (((PartitionCmd *) cmd->def)->concurrent)
    1290         170 :                                 PreventInTransactionBlock(isTopLevel,
    1291             :                                                           "ALTER TABLE ... DETACH CONCURRENTLY");
    1292             :                         }
    1293             :                     }
    1294             : 
    1295             :                     /*
    1296             :                      * Figure out lock mode, and acquire lock.  This also does
    1297             :                      * basic permissions checks, so that we won't wait for a
    1298             :                      * lock on (for example) a relation on which we have no
    1299             :                      * permissions.
    1300             :                      */
    1301       32346 :                     lockmode = AlterTableGetLockLevel(atstmt->cmds);
    1302       32346 :                     relid = AlterTableLookupRelation(atstmt, lockmode);
    1303             : 
    1304       32258 :                     if (OidIsValid(relid))
    1305             :                     {
    1306             :                         AlterTableUtilityContext atcontext;
    1307             : 
    1308             :                         /* Set up info needed for recursive callbacks ... */
    1309       32120 :                         atcontext.pstmt = pstmt;
    1310       32120 :                         atcontext.queryString = queryString;
    1311       32120 :                         atcontext.relid = relid;
    1312       32120 :                         atcontext.params = params;
    1313       32120 :                         atcontext.queryEnv = queryEnv;
    1314             : 
    1315             :                         /* ... ensure we have an event trigger context ... */
    1316       32120 :                         EventTriggerAlterTableStart(parsetree);
    1317       32120 :                         EventTriggerAlterTableRelid(relid);
    1318             : 
    1319             :                         /* ... and do it */
    1320       32120 :                         AlterTable(atstmt, lockmode, &atcontext);
    1321             : 
    1322             :                         /* done */
    1323       28394 :                         EventTriggerAlterTableEnd();
    1324             :                     }
    1325             :                     else
    1326         138 :                         ereport(NOTICE,
    1327             :                                 (errmsg("relation \"%s\" does not exist, skipping",
    1328             :                                         atstmt->relation->relname)));
    1329             :                 }
    1330             : 
    1331             :                 /* ALTER TABLE stashes commands internally */
    1332       28532 :                 commandCollected = true;
    1333       28532 :                 break;
    1334             : 
    1335         290 :             case T_AlterDomainStmt:
    1336             :                 {
    1337         290 :                     AlterDomainStmt *stmt = (AlterDomainStmt *) parsetree;
    1338             : 
    1339             :                     /*
    1340             :                      * Some or all of these functions are recursive to cover
    1341             :                      * inherited things, so permission checks are done there.
    1342             :                      */
    1343         290 :                     switch (stmt->subtype)
    1344             :                     {
    1345          14 :                         case AD_AlterDefault:
    1346             : 
    1347             :                             /*
    1348             :                              * Recursively alter column default for table and,
    1349             :                              * if requested, for descendants
    1350             :                              */
    1351             :                             address =
    1352          14 :                                 AlterDomainDefault(stmt->typeName,
    1353             :                                                    stmt->def);
    1354          14 :                             break;
    1355          12 :                         case AD_DropNotNull:
    1356             :                             address =
    1357          12 :                                 AlterDomainNotNull(stmt->typeName,
    1358             :                                                    false);
    1359          12 :                             break;
    1360          24 :                         case AD_SetNotNull:
    1361             :                             address =
    1362          24 :                                 AlterDomainNotNull(stmt->typeName,
    1363             :                                                    true);
    1364          12 :                             break;
    1365         168 :                         case AD_AddConstraint:
    1366             :                             address =
    1367         168 :                                 AlterDomainAddConstraint(stmt->typeName,
    1368             :                                                          stmt->def,
    1369             :                                                          &secondaryObject);
    1370          96 :                             break;
    1371          60 :                         case AD_DropConstraint:
    1372             :                             address =
    1373          60 :                                 AlterDomainDropConstraint(stmt->typeName,
    1374          60 :                                                           stmt->name,
    1375             :                                                           stmt->behavior,
    1376          60 :                                                           stmt->missing_ok);
    1377          54 :                             break;
    1378          12 :                         case AD_ValidateConstraint:
    1379             :                             address =
    1380          12 :                                 AlterDomainValidateConstraint(stmt->typeName,
    1381          12 :                                                               stmt->name);
    1382           6 :                             break;
    1383           0 :                         default:    /* oops */
    1384           0 :                             elog(ERROR, "unrecognized alter domain type: %d",
    1385             :                                  (int) stmt->subtype);
    1386             :                             break;
    1387             :                     }
    1388             :                 }
    1389         194 :                 break;
    1390             : 
    1391             :                 /*
    1392             :                  * ************* object creation / destruction **************
    1393             :                  */
    1394        9282 :             case T_DefineStmt:
    1395             :                 {
    1396        9282 :                     DefineStmt *stmt = (DefineStmt *) parsetree;
    1397             : 
    1398        9282 :                     switch (stmt->kind)
    1399             :                     {
    1400         906 :                         case OBJECT_AGGREGATE:
    1401             :                             address =
    1402         906 :                                 DefineAggregate(pstate, stmt->defnames, stmt->args,
    1403         906 :                                                 stmt->oldstyle,
    1404             :                                                 stmt->definition,
    1405         906 :                                                 stmt->replace);
    1406         576 :                             break;
    1407        1634 :                         case OBJECT_OPERATOR:
    1408             :                             Assert(stmt->args == NIL);
    1409        1634 :                             address = DefineOperator(stmt->defnames,
    1410             :                                                      stmt->definition);
    1411        1536 :                             break;
    1412         396 :                         case OBJECT_TYPE:
    1413             :                             Assert(stmt->args == NIL);
    1414         396 :                             address = DefineType(pstate,
    1415             :                                                  stmt->defnames,
    1416             :                                                  stmt->definition);
    1417         360 :                             break;
    1418          40 :                         case OBJECT_TSPARSER:
    1419             :                             Assert(stmt->args == NIL);
    1420          40 :                             address = DefineTSParser(stmt->defnames,
    1421             :                                                      stmt->definition);
    1422          34 :                             break;
    1423        2930 :                         case OBJECT_TSDICTIONARY:
    1424             :                             Assert(stmt->args == NIL);
    1425        2930 :                             address = DefineTSDictionary(stmt->defnames,
    1426             :                                                          stmt->definition);
    1427        2906 :                             break;
    1428         140 :                         case OBJECT_TSTEMPLATE:
    1429             :                             Assert(stmt->args == NIL);
    1430         140 :                             address = DefineTSTemplate(stmt->defnames,
    1431             :                                                        stmt->definition);
    1432         134 :                             break;
    1433        2872 :                         case OBJECT_TSCONFIGURATION:
    1434             :                             Assert(stmt->args == NIL);
    1435        2872 :                             address = DefineTSConfiguration(stmt->defnames,
    1436             :                                                             stmt->definition,
    1437             :                                                             &secondaryObject);
    1438        2872 :                             break;
    1439         364 :                         case OBJECT_COLLATION:
    1440             :                             Assert(stmt->args == NIL);
    1441         364 :                             address = DefineCollation(pstate,
    1442             :                                                       stmt->defnames,
    1443             :                                                       stmt->definition,
    1444         364 :                                                       stmt->if_not_exists);
    1445         218 :                             break;
    1446           0 :                         default:
    1447           0 :                             elog(ERROR, "unrecognized define stmt type: %d",
    1448             :                                  (int) stmt->kind);
    1449             :                             break;
    1450             :                     }
    1451             :                 }
    1452        8636 :                 break;
    1453             : 
    1454       14044 :             case T_IndexStmt:   /* CREATE INDEX */
    1455             :                 {
    1456       14044 :                     IndexStmt  *stmt = (IndexStmt *) parsetree;
    1457             :                     Oid         relid;
    1458             :                     LOCKMODE    lockmode;
    1459       14044 :                     int         nparts = -1;
    1460             :                     bool        is_alter_table;
    1461             : 
    1462       14044 :                     if (stmt->concurrent)
    1463         228 :                         PreventInTransactionBlock(isTopLevel,
    1464             :                                                   "CREATE INDEX CONCURRENTLY");
    1465             : 
    1466             :                     /*
    1467             :                      * Look up the relation OID just once, right here at the
    1468             :                      * beginning, so that we don't end up repeating the name
    1469             :                      * lookup later and latching onto a different relation
    1470             :                      * partway through.  To avoid lock upgrade hazards, it's
    1471             :                      * important that we take the strongest lock that will
    1472             :                      * eventually be needed here, so the lockmode calculation
    1473             :                      * needs to match what DefineIndex() does.
    1474             :                      */
    1475       28064 :                     lockmode = stmt->concurrent ? ShareUpdateExclusiveLock
    1476       14032 :                         : ShareLock;
    1477             :                     relid =
    1478       14032 :                         RangeVarGetRelidExtended(stmt->relation, lockmode,
    1479             :                                                  0,
    1480             :                                                  RangeVarCallbackOwnsRelation,
    1481             :                                                  NULL);
    1482             : 
    1483             :                     /*
    1484             :                      * CREATE INDEX on partitioned tables (but not regular
    1485             :                      * inherited tables) recurses to partitions, so we must
    1486             :                      * acquire locks early to avoid deadlocks.
    1487             :                      *
    1488             :                      * We also take the opportunity to verify that all
    1489             :                      * partitions are something we can put an index on, to
    1490             :                      * avoid building some indexes only to fail later.  While
    1491             :                      * at it, also count the partitions, so that DefineIndex
    1492             :                      * needn't do a duplicative find_all_inheritors search.
    1493             :                      */
    1494       14030 :                     if (stmt->relation->inh &&
    1495       13834 :                         get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE)
    1496             :                     {
    1497             :                         ListCell   *lc;
    1498        1332 :                         List       *inheritors = NIL;
    1499             : 
    1500        1332 :                         inheritors = find_all_inheritors(relid, lockmode, NULL);
    1501        3430 :                         foreach(lc, inheritors)
    1502             :                         {
    1503        2110 :                             Oid         partrelid = lfirst_oid(lc);
    1504        2110 :                             char        relkind = get_rel_relkind(partrelid);
    1505             : 
    1506        2110 :                             if (relkind != RELKIND_RELATION &&
    1507        1462 :                                 relkind != RELKIND_MATVIEW &&
    1508          18 :                                 relkind != RELKIND_PARTITIONED_TABLE &&
    1509             :                                 relkind != RELKIND_FOREIGN_TABLE)
    1510           0 :                                 elog(ERROR, "unexpected relkind \"%c\" on partition \"%s\"",
    1511             :                                      relkind, stmt->relation->relname);
    1512             : 
    1513        2110 :                             if (relkind == RELKIND_FOREIGN_TABLE &&
    1514          18 :                                 (stmt->unique || stmt->primary))
    1515          12 :                                 ereport(ERROR,
    1516             :                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1517             :                                          errmsg("cannot create unique index on partitioned table \"%s\"",
    1518             :                                                 stmt->relation->relname),
    1519             :                                          errdetail("Table \"%s\" contains partitions that are foreign tables.",
    1520             :                                                    stmt->relation->relname)));
    1521             :                         }
    1522             :                         /* count direct and indirect children, but not rel */
    1523        1320 :                         nparts = list_length(inheritors) - 1;
    1524        1320 :                         list_free(inheritors);
    1525             :                     }
    1526             : 
    1527             :                     /*
    1528             :                      * If the IndexStmt is already transformed, it must have
    1529             :                      * come from generateClonedIndexStmt, which in current
    1530             :                      * usage means it came from expandTableLikeClause rather
    1531             :                      * than from original parse analysis.  And that means we
    1532             :                      * must treat it like ALTER TABLE ADD INDEX, not CREATE.
    1533             :                      * (This is a bit grotty, but currently it doesn't seem
    1534             :                      * worth adding a separate bool field for the purpose.)
    1535             :                      */
    1536       14018 :                     is_alter_table = stmt->transformed;
    1537             : 
    1538             :                     /* Run parse analysis ... */
    1539       14018 :                     stmt = transformIndexStmt(relid, stmt, queryString);
    1540             : 
    1541             :                     /* ... and do it */
    1542       14006 :                     EventTriggerAlterTableStart(parsetree);
    1543             :                     address =
    1544       14006 :                         DefineIndex(relid,  /* OID of heap relation */
    1545             :                                     stmt,
    1546             :                                     InvalidOid, /* no predefined OID */
    1547             :                                     InvalidOid, /* no parent index */
    1548             :                                     InvalidOid, /* no parent constraint */
    1549             :                                     nparts, /* # of partitions, or -1 */
    1550             :                                     is_alter_table,
    1551             :                                     true,   /* check_rights */
    1552             :                                     true,   /* check_not_in_use */
    1553             :                                     false,  /* skip_build */
    1554             :                                     false); /* quiet */
    1555             : 
    1556             :                     /*
    1557             :                      * Add the CREATE INDEX node itself to stash right away;
    1558             :                      * if there were any commands stashed in the ALTER TABLE
    1559             :                      * code, we need them to appear after this one.
    1560             :                      */
    1561       13426 :                     EventTriggerCollectSimpleCommand(address, secondaryObject,
    1562             :                                                      parsetree);
    1563       13426 :                     commandCollected = true;
    1564       13426 :                     EventTriggerAlterTableEnd();
    1565             :                 }
    1566       13426 :                 break;
    1567             : 
    1568        1118 :             case T_ReindexStmt:
    1569        1118 :                 ExecReindex(pstate, (ReindexStmt *) parsetree, isTopLevel);
    1570             : 
    1571             :                 /* EventTriggerCollectSimpleCommand is called directly */
    1572         796 :                 commandCollected = true;
    1573         796 :                 break;
    1574             : 
    1575         558 :             case T_CreateExtensionStmt:
    1576         558 :                 address = CreateExtension(pstate, (CreateExtensionStmt *) parsetree);
    1577         514 :                 break;
    1578             : 
    1579          38 :             case T_AlterExtensionStmt:
    1580          38 :                 address = ExecAlterExtensionStmt(pstate, (AlterExtensionStmt *) parsetree);
    1581          34 :                 break;
    1582             : 
    1583         276 :             case T_AlterExtensionContentsStmt:
    1584         276 :                 address = ExecAlterExtensionContentsStmt((AlterExtensionContentsStmt *) parsetree,
    1585             :                                                          &secondaryObject);
    1586         274 :                 break;
    1587             : 
    1588         204 :             case T_CreateFdwStmt:
    1589         204 :                 address = CreateForeignDataWrapper(pstate, (CreateFdwStmt *) parsetree);
    1590         154 :                 break;
    1591             : 
    1592         122 :             case T_AlterFdwStmt:
    1593         122 :                 address = AlterForeignDataWrapper(pstate, (AlterFdwStmt *) parsetree);
    1594          48 :                 break;
    1595             : 
    1596         300 :             case T_CreateForeignServerStmt:
    1597         300 :                 address = CreateForeignServer((CreateForeignServerStmt *) parsetree);
    1598         250 :                 break;
    1599             : 
    1600         232 :             case T_AlterForeignServerStmt:
    1601         232 :                 address = AlterForeignServer((AlterForeignServerStmt *) parsetree);
    1602         178 :                 break;
    1603             : 
    1604         260 :             case T_CreateUserMappingStmt:
    1605         260 :                 address = CreateUserMapping((CreateUserMappingStmt *) parsetree);
    1606         192 :                 break;
    1607             : 
    1608         118 :             case T_AlterUserMappingStmt:
    1609         118 :                 address = AlterUserMapping((AlterUserMappingStmt *) parsetree);
    1610          58 :                 break;
    1611             : 
    1612         126 :             case T_DropUserMappingStmt:
    1613         126 :                 RemoveUserMapping((DropUserMappingStmt *) parsetree);
    1614             :                 /* no commands stashed for DROP */
    1615          88 :                 commandCollected = true;
    1616          88 :                 break;
    1617             : 
    1618          48 :             case T_ImportForeignSchemaStmt:
    1619          48 :                 ImportForeignSchema((ImportForeignSchemaStmt *) parsetree);
    1620             :                 /* commands are stashed inside ImportForeignSchema */
    1621          14 :                 commandCollected = true;
    1622          14 :                 break;
    1623             : 
    1624        4502 :             case T_CompositeTypeStmt:   /* CREATE TYPE (composite) */
    1625             :                 {
    1626        4502 :                     CompositeTypeStmt *stmt = (CompositeTypeStmt *) parsetree;
    1627             : 
    1628        4502 :                     address = DefineCompositeType(stmt->typevar,
    1629             :                                                   stmt->coldeflist);
    1630             :                 }
    1631        4490 :                 break;
    1632             : 
    1633         444 :             case T_CreateEnumStmt:  /* CREATE TYPE AS ENUM */
    1634         444 :                 address = DefineEnum((CreateEnumStmt *) parsetree);
    1635         436 :                 break;
    1636             : 
    1637         184 :             case T_CreateRangeStmt: /* CREATE TYPE AS RANGE */
    1638         184 :                 address = DefineRange(pstate, (CreateRangeStmt *) parsetree);
    1639         160 :                 break;
    1640             : 
    1641         396 :             case T_AlterEnumStmt:   /* ALTER TYPE (enum) */
    1642         396 :                 address = AlterEnum((AlterEnumStmt *) parsetree);
    1643         366 :                 break;
    1644             : 
    1645       17046 :             case T_ViewStmt:    /* CREATE VIEW */
    1646       17046 :                 EventTriggerAlterTableStart(parsetree);
    1647       17046 :                 address = DefineView((ViewStmt *) parsetree, queryString,
    1648             :                                      pstmt->stmt_location, pstmt->stmt_len);
    1649       16960 :                 EventTriggerCollectSimpleCommand(address, secondaryObject,
    1650             :                                                  parsetree);
    1651             :                 /* stashed internally */
    1652       16960 :                 commandCollected = true;
    1653       16960 :                 EventTriggerAlterTableEnd();
    1654       16960 :                 break;
    1655             : 
    1656       25736 :             case T_CreateFunctionStmt:  /* CREATE FUNCTION */
    1657       25736 :                 address = CreateFunction(pstate, (CreateFunctionStmt *) parsetree);
    1658       25242 :                 break;
    1659             : 
    1660        1390 :             case T_AlterFunctionStmt:   /* ALTER FUNCTION */
    1661        1390 :                 address = AlterFunction(pstate, (AlterFunctionStmt *) parsetree);
    1662        1360 :                 break;
    1663             : 
    1664        1092 :             case T_RuleStmt:    /* CREATE RULE */
    1665        1092 :                 address = DefineRule((RuleStmt *) parsetree, queryString);
    1666        1048 :                 break;
    1667             : 
    1668        1946 :             case T_CreateSeqStmt:
    1669        1946 :                 address = DefineSequence(pstate, (CreateSeqStmt *) parsetree);
    1670        1848 :                 break;
    1671             : 
    1672        1490 :             case T_AlterSeqStmt:
    1673        1490 :                 address = AlterSequence(pstate, (AlterSeqStmt *) parsetree);
    1674        1442 :                 break;
    1675             : 
    1676        1808 :             case T_CreateTableAsStmt:
    1677        1808 :                 address = ExecCreateTableAs(pstate, (CreateTableAsStmt *) parsetree,
    1678             :                                             params, queryEnv, qc);
    1679        1696 :                 break;
    1680             : 
    1681         268 :             case T_RefreshMatViewStmt:
    1682             : 
    1683             :                 /*
    1684             :                  * REFRESH CONCURRENTLY executes some DDL commands internally.
    1685             :                  * Inhibit DDL command collection here to avoid those commands
    1686             :                  * from showing up in the deparsed command queue.  The refresh
    1687             :                  * command itself is queued, which is enough.
    1688             :                  */
    1689         268 :                 EventTriggerInhibitCommandCollection();
    1690         268 :                 PG_TRY(2);
    1691             :                 {
    1692         268 :                     address = ExecRefreshMatView((RefreshMatViewStmt *) parsetree,
    1693             :                                                  queryString, qc);
    1694             :                 }
    1695          72 :                 PG_FINALLY(2);
    1696             :                 {
    1697         268 :                     EventTriggerUndoInhibitCommandCollection();
    1698             :                 }
    1699         268 :                 PG_END_TRY(2);
    1700         196 :                 break;
    1701             : 
    1702        3222 :             case T_CreateTrigStmt:
    1703        3222 :                 address = CreateTrigger((CreateTrigStmt *) parsetree,
    1704             :                                         queryString, InvalidOid, InvalidOid,
    1705             :                                         InvalidOid, InvalidOid, InvalidOid,
    1706             :                                         InvalidOid, NULL, false, false);
    1707        2998 :                 break;
    1708             : 
    1709         142 :             case T_CreatePLangStmt:
    1710         142 :                 address = CreateProceduralLanguage((CreatePLangStmt *) parsetree);
    1711         142 :                 break;
    1712             : 
    1713        1464 :             case T_CreateDomainStmt:
    1714        1464 :                 address = DefineDomain(pstate, (CreateDomainStmt *) parsetree);
    1715        1350 :                 break;
    1716             : 
    1717          64 :             case T_CreateConversionStmt:
    1718          64 :                 address = CreateConversionCommand((CreateConversionStmt *) parsetree);
    1719          52 :                 break;
    1720             : 
    1721         278 :             case T_CreateCastStmt:
    1722         278 :                 address = CreateCast((CreateCastStmt *) parsetree);
    1723         272 :                 break;
    1724             : 
    1725         556 :             case T_CreateOpClassStmt:
    1726         556 :                 DefineOpClass((CreateOpClassStmt *) parsetree);
    1727             :                 /* command is stashed in DefineOpClass */
    1728         556 :                 commandCollected = true;
    1729         556 :                 break;
    1730             : 
    1731         148 :             case T_CreateOpFamilyStmt:
    1732         148 :                 address = DefineOpFamily((CreateOpFamilyStmt *) parsetree);
    1733             : 
    1734             :                 /*
    1735             :                  * DefineOpFamily calls EventTriggerCollectSimpleCommand
    1736             :                  * directly.
    1737             :                  */
    1738         148 :                 commandCollected = true;
    1739         148 :                 break;
    1740             : 
    1741          50 :             case T_CreateTransformStmt:
    1742          50 :                 address = CreateTransform((CreateTransformStmt *) parsetree);
    1743          40 :                 break;
    1744             : 
    1745         920 :             case T_AlterOpFamilyStmt:
    1746         920 :                 AlterOpFamily((AlterOpFamilyStmt *) parsetree);
    1747             :                 /* commands are stashed in AlterOpFamily */
    1748         758 :                 commandCollected = true;
    1749         758 :                 break;
    1750             : 
    1751          40 :             case T_AlterTSDictionaryStmt:
    1752          40 :                 address = AlterTSDictionary((AlterTSDictionaryStmt *) parsetree);
    1753          32 :                 break;
    1754             : 
    1755        8592 :             case T_AlterTSConfigurationStmt:
    1756        8592 :                 AlterTSConfiguration((AlterTSConfigurationStmt *) parsetree);
    1757             : 
    1758             :                 /*
    1759             :                  * Commands are stashed in MakeConfigurationMapping and
    1760             :                  * DropConfigurationMapping, which are called from
    1761             :                  * AlterTSConfiguration
    1762             :                  */
    1763        8568 :                 commandCollected = true;
    1764        8568 :                 break;
    1765             : 
    1766          30 :             case T_AlterTableMoveAllStmt:
    1767          30 :                 AlterTableMoveAll((AlterTableMoveAllStmt *) parsetree);
    1768             :                 /* commands are stashed in AlterTableMoveAll */
    1769          30 :                 commandCollected = true;
    1770          30 :                 break;
    1771             : 
    1772       26256 :             case T_DropStmt:
    1773       26256 :                 ExecDropStmt((DropStmt *) parsetree, isTopLevel);
    1774             :                 /* no commands stashed for DROP */
    1775       25280 :                 commandCollected = true;
    1776       25280 :                 break;
    1777             : 
    1778        1484 :             case T_RenameStmt:
    1779        1484 :                 address = ExecRenameStmt((RenameStmt *) parsetree);
    1780        1098 :                 break;
    1781             : 
    1782          46 :             case T_AlterObjectDependsStmt:
    1783             :                 address =
    1784          46 :                     ExecAlterObjectDependsStmt((AlterObjectDependsStmt *) parsetree,
    1785             :                                                &secondaryObject);
    1786          46 :                 break;
    1787             : 
    1788         398 :             case T_AlterObjectSchemaStmt:
    1789             :                 address =
    1790         398 :                     ExecAlterObjectSchemaStmt((AlterObjectSchemaStmt *) parsetree,
    1791             :                                               &secondaryObject);
    1792         266 :                 break;
    1793             : 
    1794        1566 :             case T_AlterOwnerStmt:
    1795        1566 :                 address = ExecAlterOwnerStmt((AlterOwnerStmt *) parsetree);
    1796        1318 :                 break;
    1797             : 
    1798         608 :             case T_AlterOperatorStmt:
    1799         608 :                 address = AlterOperator((AlterOperatorStmt *) parsetree);
    1800         542 :                 break;
    1801             : 
    1802          60 :             case T_AlterTypeStmt:
    1803          60 :                 address = AlterType((AlterTypeStmt *) parsetree);
    1804          48 :                 break;
    1805             : 
    1806        6958 :             case T_CommentStmt:
    1807        6958 :                 address = CommentObject((CommentStmt *) parsetree);
    1808        6806 :                 break;
    1809             : 
    1810       29164 :             case T_GrantStmt:
    1811       29164 :                 ExecuteGrantStmt((GrantStmt *) parsetree);
    1812             :                 /* commands are stashed in ExecGrantStmt_oids */
    1813       29026 :                 commandCollected = true;
    1814       29026 :                 break;
    1815             : 
    1816         148 :             case T_DropOwnedStmt:
    1817         148 :                 DropOwnedObjects((DropOwnedStmt *) parsetree);
    1818             :                 /* no commands stashed for DROP */
    1819         130 :                 commandCollected = true;
    1820         130 :                 break;
    1821             : 
    1822         206 :             case T_AlterDefaultPrivilegesStmt:
    1823         206 :                 ExecAlterDefaultPrivilegesStmt(pstate, (AlterDefaultPrivilegesStmt *) parsetree);
    1824         194 :                 EventTriggerCollectAlterDefPrivs((AlterDefaultPrivilegesStmt *) parsetree);
    1825         194 :                 commandCollected = true;
    1826         194 :                 break;
    1827             : 
    1828         772 :             case T_CreatePolicyStmt:    /* CREATE POLICY */
    1829         772 :                 address = CreatePolicy((CreatePolicyStmt *) parsetree);
    1830         758 :                 break;
    1831             : 
    1832          84 :             case T_AlterPolicyStmt: /* ALTER POLICY */
    1833          84 :                 address = AlterPolicy((AlterPolicyStmt *) parsetree);
    1834          72 :                 break;
    1835             : 
    1836          80 :             case T_SecLabelStmt:
    1837          80 :                 address = ExecSecLabelStmt((SecLabelStmt *) parsetree);
    1838          42 :                 break;
    1839             : 
    1840          62 :             case T_CreateAmStmt:
    1841          62 :                 address = CreateAccessMethod((CreateAmStmt *) parsetree);
    1842          38 :                 break;
    1843             : 
    1844         908 :             case T_CreatePublicationStmt:
    1845         908 :                 address = CreatePublication(pstate, (CreatePublicationStmt *) parsetree);
    1846         744 :                 break;
    1847             : 
    1848        1094 :             case T_AlterPublicationStmt:
    1849        1094 :                 AlterPublication(pstate, (AlterPublicationStmt *) parsetree);
    1850             : 
    1851             :                 /*
    1852             :                  * AlterPublication calls EventTriggerCollectSimpleCommand
    1853             :                  * directly
    1854             :                  */
    1855         872 :                 commandCollected = true;
    1856         872 :                 break;
    1857             : 
    1858         482 :             case T_CreateSubscriptionStmt:
    1859         482 :                 address = CreateSubscription(pstate,
    1860             :                                              (CreateSubscriptionStmt *) parsetree,
    1861             :                                              isTopLevel);
    1862         330 :                 break;
    1863             : 
    1864         528 :             case T_AlterSubscriptionStmt:
    1865         528 :                 address = AlterSubscription(pstate,
    1866             :                                             (AlterSubscriptionStmt *) parsetree,
    1867             :                                             isTopLevel);
    1868         414 :                 break;
    1869             : 
    1870         248 :             case T_DropSubscriptionStmt:
    1871         248 :                 DropSubscription((DropSubscriptionStmt *) parsetree, isTopLevel);
    1872             :                 /* no commands stashed for DROP */
    1873         234 :                 commandCollected = true;
    1874         234 :                 break;
    1875             : 
    1876         770 :             case T_CreateStatsStmt:
    1877             :                 {
    1878             :                     Oid         relid;
    1879         770 :                     CreateStatsStmt *stmt = (CreateStatsStmt *) parsetree;
    1880         770 :                     RangeVar   *rel = (RangeVar *) linitial(stmt->relations);
    1881             : 
    1882         770 :                     if (!IsA(rel, RangeVar))
    1883          42 :                         ereport(ERROR,
    1884             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1885             :                                  errmsg("CREATE STATISTICS only supports relation names in the FROM clause")));
    1886             : 
    1887             :                     /*
    1888             :                      * CREATE STATISTICS will influence future execution plans
    1889             :                      * but does not interfere with currently executing plans.
    1890             :                      * So it should be enough to take ShareUpdateExclusiveLock
    1891             :                      * on relation, conflicting with ANALYZE and other DDL
    1892             :                      * that sets statistical information, but not with normal
    1893             :                      * queries.
    1894             :                      *
    1895             :                      * XXX RangeVarCallbackOwnsRelation not needed here, to
    1896             :                      * keep the same behavior as before.
    1897             :                      */
    1898         728 :                     relid = RangeVarGetRelid(rel, ShareUpdateExclusiveLock, false);
    1899             : 
    1900             :                     /* Run parse analysis ... */
    1901         722 :                     stmt = transformStatsStmt(relid, stmt, queryString);
    1902             : 
    1903         722 :                     address = CreateStatistics(stmt);
    1904             :                 }
    1905         590 :                 break;
    1906             : 
    1907          26 :             case T_AlterStatsStmt:
    1908          26 :                 address = AlterStatistics((AlterStatsStmt *) parsetree);
    1909          20 :                 break;
    1910             : 
    1911           6 :             case T_AlterCollationStmt:
    1912           6 :                 address = AlterCollation((AlterCollationStmt *) parsetree);
    1913           6 :                 break;
    1914             : 
    1915           0 :             default:
    1916           0 :                 elog(ERROR, "unrecognized node type: %d",
    1917             :                      (int) nodeTag(parsetree));
    1918             :                 break;
    1919             :         }
    1920             : 
    1921             :         /*
    1922             :          * Remember the object so that ddl_command_end event triggers have
    1923             :          * access to it.
    1924             :          */
    1925      230016 :         if (!commandCollected)
    1926       66840 :             EventTriggerCollectSimpleCommand(address, secondaryObject,
    1927             :                                              parsetree);
    1928             : 
    1929      230016 :         if (isCompleteQuery)
    1930             :         {
    1931      217974 :             EventTriggerSQLDrop(parsetree);
    1932      217956 :             EventTriggerDDLCommandEnd(parsetree);
    1933             :         }
    1934             :     }
    1935       13004 :     PG_FINALLY();
    1936             :     {
    1937      243002 :         if (needCleanup)
    1938        2898 :             EventTriggerEndCompleteQuery();
    1939             :     }
    1940      243002 :     PG_END_TRY();
    1941      229998 : }
    1942             : 
    1943             : /*
    1944             :  * ProcessUtilityForAlterTable
    1945             :  *      Recursive entry from ALTER TABLE
    1946             :  *
    1947             :  * ALTER TABLE sometimes generates subcommands such as CREATE INDEX.
    1948             :  * It calls this, not the main entry point ProcessUtility, to execute
    1949             :  * such subcommands.
    1950             :  *
    1951             :  * stmt: the utility command to execute
    1952             :  * context: opaque passthrough struct with the info we need
    1953             :  *
    1954             :  * It's caller's responsibility to do CommandCounterIncrement after
    1955             :  * calling this, if needed.
    1956             :  */
    1957             : void
    1958         584 : ProcessUtilityForAlterTable(Node *stmt, AlterTableUtilityContext *context)
    1959             : {
    1960             :     PlannedStmt *wrapper;
    1961             : 
    1962             :     /*
    1963             :      * For event triggers, we must "close" the current complex-command set,
    1964             :      * and start a new one afterwards; this is needed to ensure the ordering
    1965             :      * of command events is consistent with the way they were executed.
    1966             :      */
    1967         584 :     EventTriggerAlterTableEnd();
    1968             : 
    1969             :     /* Create a suitable wrapper */
    1970         584 :     wrapper = makeNode(PlannedStmt);
    1971         584 :     wrapper->commandType = CMD_UTILITY;
    1972         584 :     wrapper->canSetTag = false;
    1973         584 :     wrapper->utilityStmt = stmt;
    1974         584 :     wrapper->stmt_location = context->pstmt->stmt_location;
    1975         584 :     wrapper->stmt_len = context->pstmt->stmt_len;
    1976         584 :     wrapper->planOrigin = PLAN_STMT_INTERNAL;
    1977             : 
    1978         584 :     ProcessUtility(wrapper,
    1979             :                    context->queryString,
    1980             :                    false,
    1981             :                    PROCESS_UTILITY_SUBCOMMAND,
    1982             :                    context->params,
    1983             :                    context->queryEnv,
    1984             :                    None_Receiver,
    1985             :                    NULL);
    1986             : 
    1987         572 :     EventTriggerAlterTableStart(context->pstmt->utilityStmt);
    1988         572 :     EventTriggerAlterTableRelid(context->relid);
    1989         572 : }
    1990             : 
    1991             : /*
    1992             :  * Dispatch function for DropStmt
    1993             :  */
    1994             : static void
    1995       26380 : ExecDropStmt(DropStmt *stmt, bool isTopLevel)
    1996             : {
    1997       26380 :     switch (stmt->removeType)
    1998             :     {
    1999         870 :         case OBJECT_INDEX:
    2000         870 :             if (stmt->concurrent)
    2001         182 :                 PreventInTransactionBlock(isTopLevel,
    2002             :                                           "DROP INDEX CONCURRENTLY");
    2003             :             /* fall through */
    2004             : 
    2005             :         case OBJECT_TABLE:
    2006             :         case OBJECT_SEQUENCE:
    2007             :         case OBJECT_VIEW:
    2008             :         case OBJECT_MATVIEW:
    2009             :         case OBJECT_FOREIGN_TABLE:
    2010       17560 :             RemoveRelations(stmt);
    2011       17248 :             break;
    2012        8814 :         default:
    2013        8814 :             RemoveObjects(stmt);
    2014        8150 :             break;
    2015             :     }
    2016       25398 : }
    2017             : 
    2018             : 
    2019             : /*
    2020             :  * UtilityReturnsTuples
    2021             :  *      Return "true" if this utility statement will send output to the
    2022             :  *      destination.
    2023             :  *
    2024             :  * Generally, there should be a case here for each case in ProcessUtility
    2025             :  * where "dest" is passed on.
    2026             :  */
    2027             : bool
    2028      384516 : UtilityReturnsTuples(Node *parsetree)
    2029             : {
    2030      384516 :     switch (nodeTag(parsetree))
    2031             :     {
    2032         480 :         case T_CallStmt:
    2033             :             {
    2034         480 :                 CallStmt   *stmt = (CallStmt *) parsetree;
    2035             : 
    2036         480 :                 return (stmt->funcexpr->funcresulttype == RECORDOID);
    2037             :             }
    2038        7712 :         case T_FetchStmt:
    2039             :             {
    2040        7712 :                 FetchStmt  *stmt = (FetchStmt *) parsetree;
    2041             :                 Portal      portal;
    2042             : 
    2043        7712 :                 if (stmt->ismove)
    2044          68 :                     return false;
    2045        7644 :                 portal = GetPortalByName(stmt->portalname);
    2046        7644 :                 if (!PortalIsValid(portal))
    2047          34 :                     return false;   /* not our business to raise error */
    2048        7610 :                 return portal->tupDesc ? true : false;
    2049             :             }
    2050             : 
    2051       15740 :         case T_ExecuteStmt:
    2052             :             {
    2053       15740 :                 ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
    2054             :                 PreparedStatement *entry;
    2055             : 
    2056       15740 :                 entry = FetchPreparedStatement(stmt->name, false);
    2057       15740 :                 if (!entry)
    2058           0 :                     return false;   /* not our business to raise error */
    2059       15740 :                 if (entry->plansource->resultDesc)
    2060       15618 :                     return true;
    2061         122 :                 return false;
    2062             :             }
    2063             : 
    2064       32550 :         case T_ExplainStmt:
    2065       32550 :             return true;
    2066             : 
    2067         902 :         case T_VariableShowStmt:
    2068         902 :             return true;
    2069             : 
    2070          52 :         case T_WaitStmt:
    2071          52 :             return true;
    2072             : 
    2073      327080 :         default:
    2074      327080 :             return false;
    2075             :     }
    2076             : }
    2077             : 
    2078             : /*
    2079             :  * UtilityTupleDescriptor
    2080             :  *      Fetch the actual output tuple descriptor for a utility statement
    2081             :  *      for which UtilityReturnsTuples() previously returned "true".
    2082             :  *
    2083             :  * The returned descriptor is created in (or copied into) the current memory
    2084             :  * context.
    2085             :  */
    2086             : TupleDesc
    2087       56928 : UtilityTupleDescriptor(Node *parsetree)
    2088             : {
    2089       56928 :     switch (nodeTag(parsetree))
    2090             :     {
    2091         196 :         case T_CallStmt:
    2092         196 :             return CallStmtResultDesc((CallStmt *) parsetree);
    2093             : 
    2094        7610 :         case T_FetchStmt:
    2095             :             {
    2096        7610 :                 FetchStmt  *stmt = (FetchStmt *) parsetree;
    2097             :                 Portal      portal;
    2098             : 
    2099        7610 :                 if (stmt->ismove)
    2100           0 :                     return NULL;
    2101        7610 :                 portal = GetPortalByName(stmt->portalname);
    2102        7610 :                 if (!PortalIsValid(portal))
    2103           0 :                     return NULL;    /* not our business to raise error */
    2104        7610 :                 return CreateTupleDescCopy(portal->tupDesc);
    2105             :             }
    2106             : 
    2107       15618 :         case T_ExecuteStmt:
    2108             :             {
    2109       15618 :                 ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
    2110             :                 PreparedStatement *entry;
    2111             : 
    2112       15618 :                 entry = FetchPreparedStatement(stmt->name, false);
    2113       15618 :                 if (!entry)
    2114           0 :                     return NULL;    /* not our business to raise error */
    2115       15618 :                 return FetchPreparedStatementResultDesc(entry);
    2116             :             }
    2117             : 
    2118       32550 :         case T_ExplainStmt:
    2119       32550 :             return ExplainResultDesc((ExplainStmt *) parsetree);
    2120             : 
    2121         902 :         case T_VariableShowStmt:
    2122             :             {
    2123         902 :                 VariableShowStmt *n = (VariableShowStmt *) parsetree;
    2124             : 
    2125         902 :                 return GetPGVariableResultDesc(n->name);
    2126             :             }
    2127             : 
    2128          52 :         case T_WaitStmt:
    2129          52 :             return WaitStmtResultDesc((WaitStmt *) parsetree);
    2130             : 
    2131           0 :         default:
    2132           0 :             return NULL;
    2133             :     }
    2134             : }
    2135             : 
    2136             : 
    2137             : /*
    2138             :  * QueryReturnsTuples
    2139             :  *      Return "true" if this Query will send output to the destination.
    2140             :  */
    2141             : #ifdef NOT_USED
    2142             : bool
    2143             : QueryReturnsTuples(Query *parsetree)
    2144             : {
    2145             :     switch (parsetree->commandType)
    2146             :     {
    2147             :         case CMD_SELECT:
    2148             :             /* returns tuples */
    2149             :             return true;
    2150             :         case CMD_INSERT:
    2151             :         case CMD_UPDATE:
    2152             :         case CMD_DELETE:
    2153             :         case CMD_MERGE:
    2154             :             /* the forms with RETURNING return tuples */
    2155             :             if (parsetree->returningList)
    2156             :                 return true;
    2157             :             break;
    2158             :         case CMD_UTILITY:
    2159             :             return UtilityReturnsTuples(parsetree->utilityStmt);
    2160             :         case CMD_UNKNOWN:
    2161             :         case CMD_NOTHING:
    2162             :             /* probably shouldn't get here */
    2163             :             break;
    2164             :     }
    2165             :     return false;               /* default */
    2166             : }
    2167             : #endif
    2168             : 
    2169             : 
    2170             : /*
    2171             :  * UtilityContainsQuery
    2172             :  *      Return the contained Query, or NULL if there is none
    2173             :  *
    2174             :  * Certain utility statements, such as EXPLAIN, contain a plannable Query.
    2175             :  * This function encapsulates knowledge of exactly which ones do.
    2176             :  * We assume it is invoked only on already-parse-analyzed statements
    2177             :  * (else the contained parsetree isn't a Query yet).
    2178             :  *
    2179             :  * In some cases (currently, only EXPLAIN of CREATE TABLE AS/SELECT INTO and
    2180             :  * CREATE MATERIALIZED VIEW), potentially Query-containing utility statements
    2181             :  * can be nested.  This function will drill down to a non-utility Query, or
    2182             :  * return NULL if none.
    2183             :  */
    2184             : Query *
    2185       41004 : UtilityContainsQuery(Node *parsetree)
    2186             : {
    2187             :     Query      *qry;
    2188             : 
    2189       41004 :     switch (nodeTag(parsetree))
    2190             :     {
    2191        3444 :         case T_DeclareCursorStmt:
    2192        3444 :             qry = castNode(Query, ((DeclareCursorStmt *) parsetree)->query);
    2193        3444 :             if (qry->commandType == CMD_UTILITY)
    2194           0 :                 return UtilityContainsQuery(qry->utilityStmt);
    2195        3444 :             return qry;
    2196             : 
    2197       16200 :         case T_ExplainStmt:
    2198       16200 :             qry = castNode(Query, ((ExplainStmt *) parsetree)->query);
    2199       16200 :             if (qry->commandType == CMD_UTILITY)
    2200         108 :                 return UtilityContainsQuery(qry->utilityStmt);
    2201       16092 :             return qry;
    2202             : 
    2203          98 :         case T_CreateTableAsStmt:
    2204          98 :             qry = castNode(Query, ((CreateTableAsStmt *) parsetree)->query);
    2205          98 :             if (qry->commandType == CMD_UTILITY)
    2206           0 :                 return UtilityContainsQuery(qry->utilityStmt);
    2207          98 :             return qry;
    2208             : 
    2209       21262 :         default:
    2210       21262 :             return NULL;
    2211             :     }
    2212             : }
    2213             : 
    2214             : 
    2215             : /*
    2216             :  * AlterObjectTypeCommandTag
    2217             :  *      helper function for CreateCommandTag
    2218             :  *
    2219             :  * This covers most cases where ALTER is used with an ObjectType enum.
    2220             :  */
    2221             : static CommandTag
    2222       34728 : AlterObjectTypeCommandTag(ObjectType objtype)
    2223             : {
    2224             :     CommandTag  tag;
    2225             : 
    2226       34728 :     switch (objtype)
    2227             :     {
    2228         208 :         case OBJECT_AGGREGATE:
    2229         208 :             tag = CMDTAG_ALTER_AGGREGATE;
    2230         208 :             break;
    2231          24 :         case OBJECT_ATTRIBUTE:
    2232          24 :             tag = CMDTAG_ALTER_TYPE;
    2233          24 :             break;
    2234           0 :         case OBJECT_CAST:
    2235           0 :             tag = CMDTAG_ALTER_CAST;
    2236           0 :             break;
    2237          42 :         case OBJECT_COLLATION:
    2238          42 :             tag = CMDTAG_ALTER_COLLATION;
    2239          42 :             break;
    2240           0 :         case OBJECT_COLUMN:
    2241           0 :             tag = CMDTAG_ALTER_TABLE;
    2242           0 :             break;
    2243          72 :         case OBJECT_CONVERSION:
    2244          72 :             tag = CMDTAG_ALTER_CONVERSION;
    2245          72 :             break;
    2246          98 :         case OBJECT_DATABASE:
    2247          98 :             tag = CMDTAG_ALTER_DATABASE;
    2248          98 :             break;
    2249          66 :         case OBJECT_DOMAIN:
    2250             :         case OBJECT_DOMCONSTRAINT:
    2251          66 :             tag = CMDTAG_ALTER_DOMAIN;
    2252          66 :             break;
    2253          12 :         case OBJECT_EXTENSION:
    2254          12 :             tag = CMDTAG_ALTER_EXTENSION;
    2255          12 :             break;
    2256          44 :         case OBJECT_FDW:
    2257          44 :             tag = CMDTAG_ALTER_FOREIGN_DATA_WRAPPER;
    2258          44 :             break;
    2259          92 :         case OBJECT_FOREIGN_SERVER:
    2260          92 :             tag = CMDTAG_ALTER_SERVER;
    2261          92 :             break;
    2262         522 :         case OBJECT_FOREIGN_TABLE:
    2263         522 :             tag = CMDTAG_ALTER_FOREIGN_TABLE;
    2264         522 :             break;
    2265         678 :         case OBJECT_FUNCTION:
    2266         678 :             tag = CMDTAG_ALTER_FUNCTION;
    2267         678 :             break;
    2268         854 :         case OBJECT_INDEX:
    2269         854 :             tag = CMDTAG_ALTER_INDEX;
    2270         854 :             break;
    2271          48 :         case OBJECT_LANGUAGE:
    2272          48 :             tag = CMDTAG_ALTER_LANGUAGE;
    2273          48 :             break;
    2274          16 :         case OBJECT_LARGEOBJECT:
    2275          16 :             tag = CMDTAG_ALTER_LARGE_OBJECT;
    2276          16 :             break;
    2277         102 :         case OBJECT_OPCLASS:
    2278         102 :             tag = CMDTAG_ALTER_OPERATOR_CLASS;
    2279         102 :             break;
    2280          64 :         case OBJECT_OPERATOR:
    2281          64 :             tag = CMDTAG_ALTER_OPERATOR;
    2282          64 :             break;
    2283         110 :         case OBJECT_OPFAMILY:
    2284         110 :             tag = CMDTAG_ALTER_OPERATOR_FAMILY;
    2285         110 :             break;
    2286          30 :         case OBJECT_POLICY:
    2287          30 :             tag = CMDTAG_ALTER_POLICY;
    2288          30 :             break;
    2289          24 :         case OBJECT_PROCEDURE:
    2290          24 :             tag = CMDTAG_ALTER_PROCEDURE;
    2291          24 :             break;
    2292          32 :         case OBJECT_ROLE:
    2293          32 :             tag = CMDTAG_ALTER_ROLE;
    2294          32 :             break;
    2295          24 :         case OBJECT_ROUTINE:
    2296          24 :             tag = CMDTAG_ALTER_ROUTINE;
    2297          24 :             break;
    2298          34 :         case OBJECT_RULE:
    2299          34 :             tag = CMDTAG_ALTER_RULE;
    2300          34 :             break;
    2301          84 :         case OBJECT_SCHEMA:
    2302          84 :             tag = CMDTAG_ALTER_SCHEMA;
    2303          84 :             break;
    2304         116 :         case OBJECT_SEQUENCE:
    2305         116 :             tag = CMDTAG_ALTER_SEQUENCE;
    2306         116 :             break;
    2307       30088 :         case OBJECT_TABLE:
    2308             :         case OBJECT_TABCONSTRAINT:
    2309       30088 :             tag = CMDTAG_ALTER_TABLE;
    2310       30088 :             break;
    2311          12 :         case OBJECT_TABLESPACE:
    2312          12 :             tag = CMDTAG_ALTER_TABLESPACE;
    2313          12 :             break;
    2314          50 :         case OBJECT_TRIGGER:
    2315          50 :             tag = CMDTAG_ALTER_TRIGGER;
    2316          50 :             break;
    2317          26 :         case OBJECT_EVENT_TRIGGER:
    2318          26 :             tag = CMDTAG_ALTER_EVENT_TRIGGER;
    2319          26 :             break;
    2320          80 :         case OBJECT_TSCONFIGURATION:
    2321          80 :             tag = CMDTAG_ALTER_TEXT_SEARCH_CONFIGURATION;
    2322          80 :             break;
    2323          90 :         case OBJECT_TSDICTIONARY:
    2324          90 :             tag = CMDTAG_ALTER_TEXT_SEARCH_DICTIONARY;
    2325          90 :             break;
    2326          30 :         case OBJECT_TSPARSER:
    2327          30 :             tag = CMDTAG_ALTER_TEXT_SEARCH_PARSER;
    2328          30 :             break;
    2329          30 :         case OBJECT_TSTEMPLATE:
    2330          30 :             tag = CMDTAG_ALTER_TEXT_SEARCH_TEMPLATE;
    2331          30 :             break;
    2332         350 :         case OBJECT_TYPE:
    2333         350 :             tag = CMDTAG_ALTER_TYPE;
    2334         350 :             break;
    2335         280 :         case OBJECT_VIEW:
    2336         280 :             tag = CMDTAG_ALTER_VIEW;
    2337         280 :             break;
    2338          82 :         case OBJECT_MATVIEW:
    2339          82 :             tag = CMDTAG_ALTER_MATERIALIZED_VIEW;
    2340          82 :             break;
    2341          78 :         case OBJECT_PUBLICATION:
    2342          78 :             tag = CMDTAG_ALTER_PUBLICATION;
    2343          78 :             break;
    2344          56 :         case OBJECT_SUBSCRIPTION:
    2345          56 :             tag = CMDTAG_ALTER_SUBSCRIPTION;
    2346          56 :             break;
    2347          80 :         case OBJECT_STATISTIC_EXT:
    2348          80 :             tag = CMDTAG_ALTER_STATISTICS;
    2349          80 :             break;
    2350           0 :         default:
    2351           0 :             tag = CMDTAG_UNKNOWN;
    2352           0 :             break;
    2353             :     }
    2354             : 
    2355       34728 :     return tag;
    2356             : }
    2357             : 
    2358             : /*
    2359             :  * CreateCommandTag
    2360             :  *      utility to get a CommandTag for the command operation,
    2361             :  *      given either a raw (un-analyzed) parsetree, an analyzed Query,
    2362             :  *      or a PlannedStmt.
    2363             :  *
    2364             :  * This must handle all command types, but since the vast majority
    2365             :  * of 'em are utility commands, it seems sensible to keep it here.
    2366             :  */
    2367             : CommandTag
    2368      803310 : CreateCommandTag(Node *parsetree)
    2369             : {
    2370             :     CommandTag  tag;
    2371             : 
    2372      803310 :     switch (nodeTag(parsetree))
    2373             :     {
    2374             :             /* recurse if we're given a RawStmt */
    2375           0 :         case T_RawStmt:
    2376           0 :             tag = CreateCommandTag(((RawStmt *) parsetree)->stmt);
    2377           0 :             break;
    2378             : 
    2379             :             /* raw plannable queries */
    2380       73384 :         case T_InsertStmt:
    2381       73384 :             tag = CMDTAG_INSERT;
    2382       73384 :             break;
    2383             : 
    2384        4114 :         case T_DeleteStmt:
    2385        4114 :             tag = CMDTAG_DELETE;
    2386        4114 :             break;
    2387             : 
    2388       13224 :         case T_UpdateStmt:
    2389       13224 :             tag = CMDTAG_UPDATE;
    2390       13224 :             break;
    2391             : 
    2392        1828 :         case T_MergeStmt:
    2393        1828 :             tag = CMDTAG_MERGE;
    2394        1828 :             break;
    2395             : 
    2396      313480 :         case T_SelectStmt:
    2397      313480 :             tag = CMDTAG_SELECT;
    2398      313480 :             break;
    2399             : 
    2400        3874 :         case T_PLAssignStmt:
    2401        3874 :             tag = CMDTAG_SELECT;
    2402        3874 :             break;
    2403             : 
    2404             :             /* utility statements --- same whether raw or cooked */
    2405       35956 :         case T_TransactionStmt:
    2406             :             {
    2407       35956 :                 TransactionStmt *stmt = (TransactionStmt *) parsetree;
    2408             : 
    2409       35956 :                 switch (stmt->kind)
    2410             :                 {
    2411       14734 :                     case TRANS_STMT_BEGIN:
    2412       14734 :                         tag = CMDTAG_BEGIN;
    2413       14734 :                         break;
    2414             : 
    2415        1646 :                     case TRANS_STMT_START:
    2416        1646 :                         tag = CMDTAG_START_TRANSACTION;
    2417        1646 :                         break;
    2418             : 
    2419       12390 :                     case TRANS_STMT_COMMIT:
    2420       12390 :                         tag = CMDTAG_COMMIT;
    2421       12390 :                         break;
    2422             : 
    2423        3650 :                     case TRANS_STMT_ROLLBACK:
    2424             :                     case TRANS_STMT_ROLLBACK_TO:
    2425        3650 :                         tag = CMDTAG_ROLLBACK;
    2426        3650 :                         break;
    2427             : 
    2428        1970 :                     case TRANS_STMT_SAVEPOINT:
    2429        1970 :                         tag = CMDTAG_SAVEPOINT;
    2430        1970 :                         break;
    2431             : 
    2432         294 :                     case TRANS_STMT_RELEASE:
    2433         294 :                         tag = CMDTAG_RELEASE;
    2434         294 :                         break;
    2435             : 
    2436         676 :                     case TRANS_STMT_PREPARE:
    2437         676 :                         tag = CMDTAG_PREPARE_TRANSACTION;
    2438         676 :                         break;
    2439             : 
    2440         520 :                     case TRANS_STMT_COMMIT_PREPARED:
    2441         520 :                         tag = CMDTAG_COMMIT_PREPARED;
    2442         520 :                         break;
    2443             : 
    2444          76 :                     case TRANS_STMT_ROLLBACK_PREPARED:
    2445          76 :                         tag = CMDTAG_ROLLBACK_PREPARED;
    2446          76 :                         break;
    2447             : 
    2448           0 :                     default:
    2449           0 :                         tag = CMDTAG_UNKNOWN;
    2450           0 :                         break;
    2451             :                 }
    2452             :             }
    2453       35956 :             break;
    2454             : 
    2455        4692 :         case T_DeclareCursorStmt:
    2456        4692 :             tag = CMDTAG_DECLARE_CURSOR;
    2457        4692 :             break;
    2458             : 
    2459        2408 :         case T_ClosePortalStmt:
    2460             :             {
    2461        2408 :                 ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
    2462             : 
    2463        2408 :                 if (stmt->portalname == NULL)
    2464          12 :                     tag = CMDTAG_CLOSE_CURSOR_ALL;
    2465             :                 else
    2466        2396 :                     tag = CMDTAG_CLOSE_CURSOR;
    2467             :             }
    2468        2408 :             break;
    2469             : 
    2470        7982 :         case T_FetchStmt:
    2471             :             {
    2472        7982 :                 FetchStmt  *stmt = (FetchStmt *) parsetree;
    2473             : 
    2474        7982 :                 tag = (stmt->ismove) ? CMDTAG_MOVE : CMDTAG_FETCH;
    2475             :             }
    2476        7982 :             break;
    2477             : 
    2478        1474 :         case T_CreateDomainStmt:
    2479        1474 :             tag = CMDTAG_CREATE_DOMAIN;
    2480        1474 :             break;
    2481             : 
    2482        1106 :         case T_CreateSchemaStmt:
    2483        1106 :             tag = CMDTAG_CREATE_SCHEMA;
    2484        1106 :             break;
    2485             : 
    2486       38210 :         case T_CreateStmt:
    2487       38210 :             tag = CMDTAG_CREATE_TABLE;
    2488       38210 :             break;
    2489             : 
    2490         130 :         case T_CreateTableSpaceStmt:
    2491         130 :             tag = CMDTAG_CREATE_TABLESPACE;
    2492         130 :             break;
    2493             : 
    2494          64 :         case T_DropTableSpaceStmt:
    2495          64 :             tag = CMDTAG_DROP_TABLESPACE;
    2496          64 :             break;
    2497             : 
    2498          24 :         case T_AlterTableSpaceOptionsStmt:
    2499          24 :             tag = CMDTAG_ALTER_TABLESPACE;
    2500          24 :             break;
    2501             : 
    2502         564 :         case T_CreateExtensionStmt:
    2503         564 :             tag = CMDTAG_CREATE_EXTENSION;
    2504         564 :             break;
    2505             : 
    2506          38 :         case T_AlterExtensionStmt:
    2507          38 :             tag = CMDTAG_ALTER_EXTENSION;
    2508          38 :             break;
    2509             : 
    2510         180 :         case T_AlterExtensionContentsStmt:
    2511         180 :             tag = CMDTAG_ALTER_EXTENSION;
    2512         180 :             break;
    2513             : 
    2514         194 :         case T_CreateFdwStmt:
    2515         194 :             tag = CMDTAG_CREATE_FOREIGN_DATA_WRAPPER;
    2516         194 :             break;
    2517             : 
    2518         122 :         case T_AlterFdwStmt:
    2519         122 :             tag = CMDTAG_ALTER_FOREIGN_DATA_WRAPPER;
    2520         122 :             break;
    2521             : 
    2522         300 :         case T_CreateForeignServerStmt:
    2523         300 :             tag = CMDTAG_CREATE_SERVER;
    2524         300 :             break;
    2525             : 
    2526         232 :         case T_AlterForeignServerStmt:
    2527         232 :             tag = CMDTAG_ALTER_SERVER;
    2528         232 :             break;
    2529             : 
    2530         274 :         case T_CreateUserMappingStmt:
    2531         274 :             tag = CMDTAG_CREATE_USER_MAPPING;
    2532         274 :             break;
    2533             : 
    2534         118 :         case T_AlterUserMappingStmt:
    2535         118 :             tag = CMDTAG_ALTER_USER_MAPPING;
    2536         118 :             break;
    2537             : 
    2538         126 :         case T_DropUserMappingStmt:
    2539         126 :             tag = CMDTAG_DROP_USER_MAPPING;
    2540         126 :             break;
    2541             : 
    2542         448 :         case T_CreateForeignTableStmt:
    2543         448 :             tag = CMDTAG_CREATE_FOREIGN_TABLE;
    2544         448 :             break;
    2545             : 
    2546          48 :         case T_ImportForeignSchemaStmt:
    2547          48 :             tag = CMDTAG_IMPORT_FOREIGN_SCHEMA;
    2548          48 :             break;
    2549             : 
    2550       26270 :         case T_DropStmt:
    2551       26270 :             switch (((DropStmt *) parsetree)->removeType)
    2552             :             {
    2553       15538 :                 case OBJECT_TABLE:
    2554       15538 :                     tag = CMDTAG_DROP_TABLE;
    2555       15538 :                     break;
    2556         178 :                 case OBJECT_SEQUENCE:
    2557         178 :                     tag = CMDTAG_DROP_SEQUENCE;
    2558         178 :                     break;
    2559         900 :                 case OBJECT_VIEW:
    2560         900 :                     tag = CMDTAG_DROP_VIEW;
    2561         900 :                     break;
    2562         126 :                 case OBJECT_MATVIEW:
    2563         126 :                     tag = CMDTAG_DROP_MATERIALIZED_VIEW;
    2564         126 :                     break;
    2565         898 :                 case OBJECT_INDEX:
    2566         898 :                     tag = CMDTAG_DROP_INDEX;
    2567         898 :                     break;
    2568         586 :                 case OBJECT_TYPE:
    2569         586 :                     tag = CMDTAG_DROP_TYPE;
    2570         586 :                     break;
    2571         512 :                 case OBJECT_DOMAIN:
    2572         512 :                     tag = CMDTAG_DROP_DOMAIN;
    2573         512 :                     break;
    2574          88 :                 case OBJECT_COLLATION:
    2575          88 :                     tag = CMDTAG_DROP_COLLATION;
    2576          88 :                     break;
    2577          36 :                 case OBJECT_CONVERSION:
    2578          36 :                     tag = CMDTAG_DROP_CONVERSION;
    2579          36 :                     break;
    2580         578 :                 case OBJECT_SCHEMA:
    2581         578 :                     tag = CMDTAG_DROP_SCHEMA;
    2582         578 :                     break;
    2583          18 :                 case OBJECT_TSPARSER:
    2584          18 :                     tag = CMDTAG_DROP_TEXT_SEARCH_PARSER;
    2585          18 :                     break;
    2586          24 :                 case OBJECT_TSDICTIONARY:
    2587          24 :                     tag = CMDTAG_DROP_TEXT_SEARCH_DICTIONARY;
    2588          24 :                     break;
    2589          18 :                 case OBJECT_TSTEMPLATE:
    2590          18 :                     tag = CMDTAG_DROP_TEXT_SEARCH_TEMPLATE;
    2591          18 :                     break;
    2592          30 :                 case OBJECT_TSCONFIGURATION:
    2593          30 :                     tag = CMDTAG_DROP_TEXT_SEARCH_CONFIGURATION;
    2594          30 :                     break;
    2595         170 :                 case OBJECT_FOREIGN_TABLE:
    2596         170 :                     tag = CMDTAG_DROP_FOREIGN_TABLE;
    2597         170 :                     break;
    2598         166 :                 case OBJECT_EXTENSION:
    2599         166 :                     tag = CMDTAG_DROP_EXTENSION;
    2600         166 :                     break;
    2601        3402 :                 case OBJECT_FUNCTION:
    2602        3402 :                     tag = CMDTAG_DROP_FUNCTION;
    2603        3402 :                     break;
    2604         146 :                 case OBJECT_PROCEDURE:
    2605         146 :                     tag = CMDTAG_DROP_PROCEDURE;
    2606         146 :                     break;
    2607          30 :                 case OBJECT_ROUTINE:
    2608          30 :                     tag = CMDTAG_DROP_ROUTINE;
    2609          30 :                     break;
    2610         104 :                 case OBJECT_AGGREGATE:
    2611         104 :                     tag = CMDTAG_DROP_AGGREGATE;
    2612         104 :                     break;
    2613         182 :                 case OBJECT_OPERATOR:
    2614         182 :                     tag = CMDTAG_DROP_OPERATOR;
    2615         182 :                     break;
    2616          26 :                 case OBJECT_LANGUAGE:
    2617          26 :                     tag = CMDTAG_DROP_LANGUAGE;
    2618          26 :                     break;
    2619          60 :                 case OBJECT_CAST:
    2620          60 :                     tag = CMDTAG_DROP_CAST;
    2621          60 :                     break;
    2622         778 :                 case OBJECT_TRIGGER:
    2623         778 :                     tag = CMDTAG_DROP_TRIGGER;
    2624         778 :                     break;
    2625         124 :                 case OBJECT_EVENT_TRIGGER:
    2626         124 :                     tag = CMDTAG_DROP_EVENT_TRIGGER;
    2627         124 :                     break;
    2628         236 :                 case OBJECT_RULE:
    2629         236 :                     tag = CMDTAG_DROP_RULE;
    2630         236 :                     break;
    2631         150 :                 case OBJECT_FDW:
    2632         150 :                     tag = CMDTAG_DROP_FOREIGN_DATA_WRAPPER;
    2633         150 :                     break;
    2634         134 :                 case OBJECT_FOREIGN_SERVER:
    2635         134 :                     tag = CMDTAG_DROP_SERVER;
    2636         134 :                     break;
    2637          56 :                 case OBJECT_OPCLASS:
    2638          56 :                     tag = CMDTAG_DROP_OPERATOR_CLASS;
    2639          56 :                     break;
    2640         128 :                 case OBJECT_OPFAMILY:
    2641         128 :                     tag = CMDTAG_DROP_OPERATOR_FAMILY;
    2642         128 :                     break;
    2643         188 :                 case OBJECT_POLICY:
    2644         188 :                     tag = CMDTAG_DROP_POLICY;
    2645         188 :                     break;
    2646          16 :                 case OBJECT_TRANSFORM:
    2647          16 :                     tag = CMDTAG_DROP_TRANSFORM;
    2648          16 :                     break;
    2649          36 :                 case OBJECT_ACCESS_METHOD:
    2650          36 :                     tag = CMDTAG_DROP_ACCESS_METHOD;
    2651          36 :                     break;
    2652         410 :                 case OBJECT_PUBLICATION:
    2653         410 :                     tag = CMDTAG_DROP_PUBLICATION;
    2654         410 :                     break;
    2655         198 :                 case OBJECT_STATISTIC_EXT:
    2656         198 :                     tag = CMDTAG_DROP_STATISTICS;
    2657         198 :                     break;
    2658           0 :                 default:
    2659           0 :                     tag = CMDTAG_UNKNOWN;
    2660             :             }
    2661       26270 :             break;
    2662             : 
    2663        1774 :         case T_TruncateStmt:
    2664        1774 :             tag = CMDTAG_TRUNCATE_TABLE;
    2665        1774 :             break;
    2666             : 
    2667        6882 :         case T_CommentStmt:
    2668        6882 :             tag = CMDTAG_COMMENT;
    2669        6882 :             break;
    2670             : 
    2671         122 :         case T_SecLabelStmt:
    2672         122 :             tag = CMDTAG_SECURITY_LABEL;
    2673         122 :             break;
    2674             : 
    2675       11334 :         case T_CopyStmt:
    2676       11334 :             tag = CMDTAG_COPY;
    2677       11334 :             break;
    2678             : 
    2679        1570 :         case T_RenameStmt:
    2680             : 
    2681             :             /*
    2682             :              * When the column is renamed, the command tag is created from its
    2683             :              * relation type
    2684             :              */
    2685        1570 :             tag = AlterObjectTypeCommandTag(((RenameStmt *) parsetree)->renameType == OBJECT_COLUMN ?
    2686             :                                             ((RenameStmt *) parsetree)->relationType :
    2687             :                                             ((RenameStmt *) parsetree)->renameType);
    2688        1570 :             break;
    2689             : 
    2690          46 :         case T_AlterObjectDependsStmt:
    2691          46 :             tag = AlterObjectTypeCommandTag(((AlterObjectDependsStmt *) parsetree)->objectType);
    2692          46 :             break;
    2693             : 
    2694         410 :         case T_AlterObjectSchemaStmt:
    2695         410 :             tag = AlterObjectTypeCommandTag(((AlterObjectSchemaStmt *) parsetree)->objectType);
    2696         410 :             break;
    2697             : 
    2698        1560 :         case T_AlterOwnerStmt:
    2699        1560 :             tag = AlterObjectTypeCommandTag(((AlterOwnerStmt *) parsetree)->objectType);
    2700        1560 :             break;
    2701             : 
    2702          30 :         case T_AlterTableMoveAllStmt:
    2703          30 :             tag = AlterObjectTypeCommandTag(((AlterTableMoveAllStmt *) parsetree)->objtype);
    2704          30 :             break;
    2705             : 
    2706       31112 :         case T_AlterTableStmt:
    2707       31112 :             tag = AlterObjectTypeCommandTag(((AlterTableStmt *) parsetree)->objtype);
    2708       31112 :             break;
    2709             : 
    2710         296 :         case T_AlterDomainStmt:
    2711         296 :             tag = CMDTAG_ALTER_DOMAIN;
    2712         296 :             break;
    2713             : 
    2714         186 :         case T_AlterFunctionStmt:
    2715         186 :             switch (((AlterFunctionStmt *) parsetree)->objtype)
    2716             :             {
    2717         168 :                 case OBJECT_FUNCTION:
    2718         168 :                     tag = CMDTAG_ALTER_FUNCTION;
    2719         168 :                     break;
    2720          18 :                 case OBJECT_PROCEDURE:
    2721          18 :                     tag = CMDTAG_ALTER_PROCEDURE;
    2722          18 :                     break;
    2723           0 :                 case OBJECT_ROUTINE:
    2724           0 :                     tag = CMDTAG_ALTER_ROUTINE;
    2725           0 :                     break;
    2726           0 :                 default:
    2727           0 :                     tag = CMDTAG_UNKNOWN;
    2728             :             }
    2729         186 :             break;
    2730             : 
    2731       20644 :         case T_GrantStmt:
    2732             :             {
    2733       20644 :                 GrantStmt  *stmt = (GrantStmt *) parsetree;
    2734             : 
    2735       20644 :                 tag = (stmt->is_grant) ? CMDTAG_GRANT : CMDTAG_REVOKE;
    2736             :             }
    2737       20644 :             break;
    2738             : 
    2739         874 :         case T_GrantRoleStmt:
    2740             :             {
    2741         874 :                 GrantRoleStmt *stmt = (GrantRoleStmt *) parsetree;
    2742             : 
    2743         874 :                 tag = (stmt->is_grant) ? CMDTAG_GRANT_ROLE : CMDTAG_REVOKE_ROLE;
    2744             :             }
    2745         874 :             break;
    2746             : 
    2747         228 :         case T_AlterDefaultPrivilegesStmt:
    2748         228 :             tag = CMDTAG_ALTER_DEFAULT_PRIVILEGES;
    2749         228 :             break;
    2750             : 
    2751        7920 :         case T_DefineStmt:
    2752        7920 :             switch (((DefineStmt *) parsetree)->kind)
    2753             :             {
    2754         892 :                 case OBJECT_AGGREGATE:
    2755         892 :                     tag = CMDTAG_CREATE_AGGREGATE;
    2756         892 :                     break;
    2757         418 :                 case OBJECT_OPERATOR:
    2758         418 :                     tag = CMDTAG_CREATE_OPERATOR;
    2759         418 :                     break;
    2760         278 :                 case OBJECT_TYPE:
    2761         278 :                     tag = CMDTAG_CREATE_TYPE;
    2762         278 :                     break;
    2763          40 :                 case OBJECT_TSPARSER:
    2764          40 :                     tag = CMDTAG_CREATE_TEXT_SEARCH_PARSER;
    2765          40 :                     break;
    2766        2926 :                 case OBJECT_TSDICTIONARY:
    2767        2926 :                     tag = CMDTAG_CREATE_TEXT_SEARCH_DICTIONARY;
    2768        2926 :                     break;
    2769         136 :                 case OBJECT_TSTEMPLATE:
    2770         136 :                     tag = CMDTAG_CREATE_TEXT_SEARCH_TEMPLATE;
    2771         136 :                     break;
    2772        2880 :                 case OBJECT_TSCONFIGURATION:
    2773        2880 :                     tag = CMDTAG_CREATE_TEXT_SEARCH_CONFIGURATION;
    2774        2880 :                     break;
    2775         350 :                 case OBJECT_COLLATION:
    2776         350 :                     tag = CMDTAG_CREATE_COLLATION;
    2777         350 :                     break;
    2778           0 :                 case OBJECT_ACCESS_METHOD:
    2779           0 :                     tag = CMDTAG_CREATE_ACCESS_METHOD;
    2780           0 :                     break;
    2781           0 :                 default:
    2782           0 :                     tag = CMDTAG_UNKNOWN;
    2783             :             }
    2784        7920 :             break;
    2785             : 
    2786        4502 :         case T_CompositeTypeStmt:
    2787        4502 :             tag = CMDTAG_CREATE_TYPE;
    2788        4502 :             break;
    2789             : 
    2790         208 :         case T_CreateEnumStmt:
    2791         208 :             tag = CMDTAG_CREATE_TYPE;
    2792         208 :             break;
    2793             : 
    2794         190 :         case T_CreateRangeStmt:
    2795         190 :             tag = CMDTAG_CREATE_TYPE;
    2796         190 :             break;
    2797             : 
    2798         408 :         case T_AlterEnumStmt:
    2799         408 :             tag = CMDTAG_ALTER_TYPE;
    2800         408 :             break;
    2801             : 
    2802       16988 :         case T_ViewStmt:
    2803       16988 :             tag = CMDTAG_CREATE_VIEW;
    2804       16988 :             break;
    2805             : 
    2806       18516 :         case T_CreateFunctionStmt:
    2807       18516 :             if (((CreateFunctionStmt *) parsetree)->is_procedure)
    2808         382 :                 tag = CMDTAG_CREATE_PROCEDURE;
    2809             :             else
    2810       18134 :                 tag = CMDTAG_CREATE_FUNCTION;
    2811       18516 :             break;
    2812             : 
    2813        6582 :         case T_IndexStmt:
    2814        6582 :             tag = CMDTAG_CREATE_INDEX;
    2815        6582 :             break;
    2816             : 
    2817        1118 :         case T_RuleStmt:
    2818        1118 :             tag = CMDTAG_CREATE_RULE;
    2819        1118 :             break;
    2820             : 
    2821         698 :         case T_CreateSeqStmt:
    2822         698 :             tag = CMDTAG_CREATE_SEQUENCE;
    2823         698 :             break;
    2824             : 
    2825         254 :         case T_AlterSeqStmt:
    2826         254 :             tag = CMDTAG_ALTER_SEQUENCE;
    2827         254 :             break;
    2828             : 
    2829        1138 :         case T_DoStmt:
    2830        1138 :             tag = CMDTAG_DO;
    2831        1138 :             break;
    2832             : 
    2833         806 :         case T_CreatedbStmt:
    2834         806 :             tag = CMDTAG_CREATE_DATABASE;
    2835         806 :             break;
    2836             : 
    2837        1330 :         case T_AlterDatabaseStmt:
    2838             :         case T_AlterDatabaseRefreshCollStmt:
    2839             :         case T_AlterDatabaseSetStmt:
    2840        1330 :             tag = CMDTAG_ALTER_DATABASE;
    2841        1330 :             break;
    2842             : 
    2843         124 :         case T_DropdbStmt:
    2844         124 :             tag = CMDTAG_DROP_DATABASE;
    2845         124 :             break;
    2846             : 
    2847          88 :         case T_NotifyStmt:
    2848          88 :             tag = CMDTAG_NOTIFY;
    2849          88 :             break;
    2850             : 
    2851          74 :         case T_ListenStmt:
    2852          74 :             tag = CMDTAG_LISTEN;
    2853          74 :             break;
    2854             : 
    2855          38 :         case T_UnlistenStmt:
    2856          38 :             tag = CMDTAG_UNLISTEN;
    2857          38 :             break;
    2858             : 
    2859          64 :         case T_LoadStmt:
    2860          64 :             tag = CMDTAG_LOAD;
    2861          64 :             break;
    2862             : 
    2863         508 :         case T_CallStmt:
    2864         508 :             tag = CMDTAG_CALL;
    2865         508 :             break;
    2866             : 
    2867         242 :         case T_ClusterStmt:
    2868         242 :             tag = CMDTAG_CLUSTER;
    2869         242 :             break;
    2870             : 
    2871       13884 :         case T_VacuumStmt:
    2872       13884 :             if (((VacuumStmt *) parsetree)->is_vacuumcmd)
    2873        8992 :                 tag = CMDTAG_VACUUM;
    2874             :             else
    2875        4892 :                 tag = CMDTAG_ANALYZE;
    2876       13884 :             break;
    2877             : 
    2878       24470 :         case T_ExplainStmt:
    2879       24470 :             tag = CMDTAG_EXPLAIN;
    2880       24470 :             break;
    2881             : 
    2882        1746 :         case T_CreateTableAsStmt:
    2883        1746 :             switch (((CreateTableAsStmt *) parsetree)->objtype)
    2884             :             {
    2885        1246 :                 case OBJECT_TABLE:
    2886        1246 :                     if (((CreateTableAsStmt *) parsetree)->is_select_into)
    2887           0 :                         tag = CMDTAG_SELECT_INTO;
    2888             :                     else
    2889        1246 :                         tag = CMDTAG_CREATE_TABLE_AS;
    2890        1246 :                     break;
    2891         500 :                 case OBJECT_MATVIEW:
    2892         500 :                     tag = CMDTAG_CREATE_MATERIALIZED_VIEW;
    2893         500 :                     break;
    2894           0 :                 default:
    2895           0 :                     tag = CMDTAG_UNKNOWN;
    2896             :             }
    2897        1746 :             break;
    2898             : 
    2899         274 :         case T_RefreshMatViewStmt:
    2900         274 :             tag = CMDTAG_REFRESH_MATERIALIZED_VIEW;
    2901         274 :             break;
    2902             : 
    2903         218 :         case T_AlterSystemStmt:
    2904         218 :             tag = CMDTAG_ALTER_SYSTEM;
    2905         218 :             break;
    2906             : 
    2907       28306 :         case T_VariableSetStmt:
    2908       28306 :             switch (((VariableSetStmt *) parsetree)->kind)
    2909             :             {
    2910       23512 :                 case VAR_SET_VALUE:
    2911             :                 case VAR_SET_CURRENT:
    2912             :                 case VAR_SET_DEFAULT:
    2913             :                 case VAR_SET_MULTI:
    2914       23512 :                     tag = CMDTAG_SET;
    2915       23512 :                     break;
    2916        4794 :                 case VAR_RESET:
    2917             :                 case VAR_RESET_ALL:
    2918        4794 :                     tag = CMDTAG_RESET;
    2919        4794 :                     break;
    2920           0 :                 default:
    2921           0 :                     tag = CMDTAG_UNKNOWN;
    2922             :             }
    2923       28306 :             break;
    2924             : 
    2925         884 :         case T_VariableShowStmt:
    2926         884 :             tag = CMDTAG_SHOW;
    2927         884 :             break;
    2928             : 
    2929          30 :         case T_DiscardStmt:
    2930          30 :             switch (((DiscardStmt *) parsetree)->target)
    2931             :             {
    2932           6 :                 case DISCARD_ALL:
    2933           6 :                     tag = CMDTAG_DISCARD_ALL;
    2934           6 :                     break;
    2935           4 :                 case DISCARD_PLANS:
    2936           4 :                     tag = CMDTAG_DISCARD_PLANS;
    2937           4 :                     break;
    2938           8 :                 case DISCARD_TEMP:
    2939           8 :                     tag = CMDTAG_DISCARD_TEMP;
    2940           8 :                     break;
    2941          12 :                 case DISCARD_SEQUENCES:
    2942          12 :                     tag = CMDTAG_DISCARD_SEQUENCES;
    2943          12 :                     break;
    2944           0 :                 default:
    2945           0 :                     tag = CMDTAG_UNKNOWN;
    2946             :             }
    2947          30 :             break;
    2948             : 
    2949          38 :         case T_CreateTransformStmt:
    2950          38 :             tag = CMDTAG_CREATE_TRANSFORM;
    2951          38 :             break;
    2952             : 
    2953        3256 :         case T_CreateTrigStmt:
    2954        3256 :             tag = CMDTAG_CREATE_TRIGGER;
    2955        3256 :             break;
    2956             : 
    2957         198 :         case T_CreateEventTrigStmt:
    2958         198 :             tag = CMDTAG_CREATE_EVENT_TRIGGER;
    2959         198 :             break;
    2960             : 
    2961          46 :         case T_AlterEventTrigStmt:
    2962          46 :             tag = CMDTAG_ALTER_EVENT_TRIGGER;
    2963          46 :             break;
    2964             : 
    2965          14 :         case T_CreatePLangStmt:
    2966          14 :             tag = CMDTAG_CREATE_LANGUAGE;
    2967          14 :             break;
    2968             : 
    2969        1876 :         case T_CreateRoleStmt:
    2970        1876 :             tag = CMDTAG_CREATE_ROLE;
    2971        1876 :             break;
    2972             : 
    2973         476 :         case T_AlterRoleStmt:
    2974         476 :             tag = CMDTAG_ALTER_ROLE;
    2975         476 :             break;
    2976             : 
    2977          94 :         case T_AlterRoleSetStmt:
    2978          94 :             tag = CMDTAG_ALTER_ROLE;
    2979          94 :             break;
    2980             : 
    2981        1732 :         case T_DropRoleStmt:
    2982        1732 :             tag = CMDTAG_DROP_ROLE;
    2983        1732 :             break;
    2984             : 
    2985         156 :         case T_DropOwnedStmt:
    2986         156 :             tag = CMDTAG_DROP_OWNED;
    2987         156 :             break;
    2988             : 
    2989          46 :         case T_ReassignOwnedStmt:
    2990          46 :             tag = CMDTAG_REASSIGN_OWNED;
    2991          46 :             break;
    2992             : 
    2993        1048 :         case T_LockStmt:
    2994        1048 :             tag = CMDTAG_LOCK_TABLE;
    2995        1048 :             break;
    2996             : 
    2997         102 :         case T_ConstraintsSetStmt:
    2998         102 :             tag = CMDTAG_SET_CONSTRAINTS;
    2999         102 :             break;
    3000             : 
    3001         248 :         case T_CheckPointStmt:
    3002         248 :             tag = CMDTAG_CHECKPOINT;
    3003         248 :             break;
    3004             : 
    3005        1346 :         case T_ReindexStmt:
    3006        1346 :             tag = CMDTAG_REINDEX;
    3007        1346 :             break;
    3008             : 
    3009          72 :         case T_CreateConversionStmt:
    3010          72 :             tag = CMDTAG_CREATE_CONVERSION;
    3011          72 :             break;
    3012             : 
    3013         190 :         case T_CreateCastStmt:
    3014         190 :             tag = CMDTAG_CREATE_CAST;
    3015         190 :             break;
    3016             : 
    3017         148 :         case T_CreateOpClassStmt:
    3018         148 :             tag = CMDTAG_CREATE_OPERATOR_CLASS;
    3019         148 :             break;
    3020             : 
    3021         156 :         case T_CreateOpFamilyStmt:
    3022         156 :             tag = CMDTAG_CREATE_OPERATOR_FAMILY;
    3023         156 :             break;
    3024             : 
    3025         300 :         case T_AlterOpFamilyStmt:
    3026         300 :             tag = CMDTAG_ALTER_OPERATOR_FAMILY;
    3027         300 :             break;
    3028             : 
    3029         168 :         case T_AlterOperatorStmt:
    3030         168 :             tag = CMDTAG_ALTER_OPERATOR;
    3031         168 :             break;
    3032             : 
    3033          28 :         case T_AlterTypeStmt:
    3034          28 :             tag = CMDTAG_ALTER_TYPE;
    3035          28 :             break;
    3036             : 
    3037          40 :         case T_AlterTSDictionaryStmt:
    3038          40 :             tag = CMDTAG_ALTER_TEXT_SEARCH_DICTIONARY;
    3039          40 :             break;
    3040             : 
    3041        8598 :         case T_AlterTSConfigurationStmt:
    3042        8598 :             tag = CMDTAG_ALTER_TEXT_SEARCH_CONFIGURATION;
    3043        8598 :             break;
    3044             : 
    3045         808 :         case T_CreatePolicyStmt:
    3046         808 :             tag = CMDTAG_CREATE_POLICY;
    3047         808 :             break;
    3048             : 
    3049          96 :         case T_AlterPolicyStmt:
    3050          96 :             tag = CMDTAG_ALTER_POLICY;
    3051          96 :             break;
    3052             : 
    3053          52 :         case T_CreateAmStmt:
    3054          52 :             tag = CMDTAG_CREATE_ACCESS_METHOD;
    3055          52 :             break;
    3056             : 
    3057         922 :         case T_CreatePublicationStmt:
    3058         922 :             tag = CMDTAG_CREATE_PUBLICATION;
    3059         922 :             break;
    3060             : 
    3061        1112 :         case T_AlterPublicationStmt:
    3062        1112 :             tag = CMDTAG_ALTER_PUBLICATION;
    3063        1112 :             break;
    3064             : 
    3065         488 :         case T_CreateSubscriptionStmt:
    3066         488 :             tag = CMDTAG_CREATE_SUBSCRIPTION;
    3067         488 :             break;
    3068             : 
    3069         528 :         case T_AlterSubscriptionStmt:
    3070         528 :             tag = CMDTAG_ALTER_SUBSCRIPTION;
    3071         528 :             break;
    3072             : 
    3073         248 :         case T_DropSubscriptionStmt:
    3074         248 :             tag = CMDTAG_DROP_SUBSCRIPTION;
    3075         248 :             break;
    3076             : 
    3077           6 :         case T_AlterCollationStmt:
    3078           6 :             tag = CMDTAG_ALTER_COLLATION;
    3079           6 :             break;
    3080             : 
    3081        3302 :         case T_PrepareStmt:
    3082        3302 :             tag = CMDTAG_PREPARE;
    3083        3302 :             break;
    3084             : 
    3085       30054 :         case T_ExecuteStmt:
    3086       30054 :             tag = CMDTAG_EXECUTE;
    3087       30054 :             break;
    3088             : 
    3089         734 :         case T_CreateStatsStmt:
    3090         734 :             tag = CMDTAG_CREATE_STATISTICS;
    3091         734 :             break;
    3092             : 
    3093          28 :         case T_AlterStatsStmt:
    3094          28 :             tag = CMDTAG_ALTER_STATISTICS;
    3095          28 :             break;
    3096             : 
    3097        4096 :         case T_DeallocateStmt:
    3098             :             {
    3099        4096 :                 DeallocateStmt *stmt = (DeallocateStmt *) parsetree;
    3100             : 
    3101        4096 :                 if (stmt->name == NULL)
    3102          72 :                     tag = CMDTAG_DEALLOCATE_ALL;
    3103             :                 else
    3104        4024 :                     tag = CMDTAG_DEALLOCATE;
    3105             :             }
    3106        4096 :             break;
    3107             : 
    3108          98 :         case T_WaitStmt:
    3109          98 :             tag = CMDTAG_WAIT;
    3110          98 :             break;
    3111             : 
    3112             :             /* already-planned queries */
    3113          28 :         case T_PlannedStmt:
    3114             :             {
    3115          28 :                 PlannedStmt *stmt = (PlannedStmt *) parsetree;
    3116             : 
    3117          28 :                 switch (stmt->commandType)
    3118             :                 {
    3119           0 :                     case CMD_SELECT:
    3120             : 
    3121             :                         /*
    3122             :                          * We take a little extra care here so that the result
    3123             :                          * will be useful for complaints about read-only
    3124             :                          * statements
    3125             :                          */
    3126           0 :                         if (stmt->rowMarks != NIL)
    3127             :                         {
    3128             :                             /* not 100% but probably close enough */
    3129           0 :                             switch (((PlanRowMark *) linitial(stmt->rowMarks))->strength)
    3130             :                             {
    3131           0 :                                 case LCS_FORKEYSHARE:
    3132           0 :                                     tag = CMDTAG_SELECT_FOR_KEY_SHARE;
    3133           0 :                                     break;
    3134           0 :                                 case LCS_FORSHARE:
    3135           0 :                                     tag = CMDTAG_SELECT_FOR_SHARE;
    3136           0 :                                     break;
    3137           0 :                                 case LCS_FORNOKEYUPDATE:
    3138           0 :                                     tag = CMDTAG_SELECT_FOR_NO_KEY_UPDATE;
    3139           0 :                                     break;
    3140           0 :                                 case LCS_FORUPDATE:
    3141           0 :                                     tag = CMDTAG_SELECT_FOR_UPDATE;
    3142           0 :                                     break;
    3143           0 :                                 default:
    3144           0 :                                     tag = CMDTAG_SELECT;
    3145           0 :                                     break;
    3146             :                             }
    3147             :                         }
    3148             :                         else
    3149           0 :                             tag = CMDTAG_SELECT;
    3150           0 :                         break;
    3151          12 :                     case CMD_UPDATE:
    3152          12 :                         tag = CMDTAG_UPDATE;
    3153          12 :                         break;
    3154          10 :                     case CMD_INSERT:
    3155          10 :                         tag = CMDTAG_INSERT;
    3156          10 :                         break;
    3157           6 :                     case CMD_DELETE:
    3158           6 :                         tag = CMDTAG_DELETE;
    3159           6 :                         break;
    3160           0 :                     case CMD_MERGE:
    3161           0 :                         tag = CMDTAG_MERGE;
    3162           0 :                         break;
    3163           0 :                     case CMD_UTILITY:
    3164           0 :                         tag = CreateCommandTag(stmt->utilityStmt);
    3165           0 :                         break;
    3166           0 :                     default:
    3167           0 :                         elog(WARNING, "unrecognized commandType: %d",
    3168             :                              (int) stmt->commandType);
    3169           0 :                         tag = CMDTAG_UNKNOWN;
    3170           0 :                         break;
    3171             :                 }
    3172             :             }
    3173          28 :             break;
    3174             : 
    3175             :             /* parsed-and-rewritten-but-not-planned queries */
    3176         794 :         case T_Query:
    3177             :             {
    3178         794 :                 Query      *stmt = (Query *) parsetree;
    3179             : 
    3180         794 :                 switch (stmt->commandType)
    3181             :                 {
    3182         788 :                     case CMD_SELECT:
    3183             : 
    3184             :                         /*
    3185             :                          * We take a little extra care here so that the result
    3186             :                          * will be useful for complaints about read-only
    3187             :                          * statements
    3188             :                          */
    3189         788 :                         if (stmt->rowMarks != NIL)
    3190             :                         {
    3191             :                             /* not 100% but probably close enough */
    3192           0 :                             switch (((RowMarkClause *) linitial(stmt->rowMarks))->strength)
    3193             :                             {
    3194           0 :                                 case LCS_FORKEYSHARE:
    3195           0 :                                     tag = CMDTAG_SELECT_FOR_KEY_SHARE;
    3196           0 :                                     break;
    3197           0 :                                 case LCS_FORSHARE:
    3198           0 :                                     tag = CMDTAG_SELECT_FOR_SHARE;
    3199           0 :                                     break;
    3200           0 :                                 case LCS_FORNOKEYUPDATE:
    3201           0 :                                     tag = CMDTAG_SELECT_FOR_NO_KEY_UPDATE;
    3202           0 :                                     break;
    3203           0 :                                 case LCS_FORUPDATE:
    3204           0 :                                     tag = CMDTAG_SELECT_FOR_UPDATE;
    3205           0 :                                     break;
    3206           0 :                                 default:
    3207           0 :                                     tag = CMDTAG_UNKNOWN;
    3208           0 :                                     break;
    3209             :                             }
    3210             :                         }
    3211             :                         else
    3212         788 :                             tag = CMDTAG_SELECT;
    3213         788 :                         break;
    3214           0 :                     case CMD_UPDATE:
    3215           0 :                         tag = CMDTAG_UPDATE;
    3216           0 :                         break;
    3217           6 :                     case CMD_INSERT:
    3218           6 :                         tag = CMDTAG_INSERT;
    3219           6 :                         break;
    3220           0 :                     case CMD_DELETE:
    3221           0 :                         tag = CMDTAG_DELETE;
    3222           0 :                         break;
    3223           0 :                     case CMD_MERGE:
    3224           0 :                         tag = CMDTAG_MERGE;
    3225           0 :                         break;
    3226           0 :                     case CMD_UTILITY:
    3227           0 :                         tag = CreateCommandTag(stmt->utilityStmt);
    3228           0 :                         break;
    3229           0 :                     default:
    3230           0 :                         elog(WARNING, "unrecognized commandType: %d",
    3231             :                              (int) stmt->commandType);
    3232           0 :                         tag = CMDTAG_UNKNOWN;
    3233           0 :                         break;
    3234             :                 }
    3235             :             }
    3236         794 :             break;
    3237             : 
    3238           0 :         default:
    3239           0 :             elog(WARNING, "unrecognized node type: %d",
    3240             :                  (int) nodeTag(parsetree));
    3241           0 :             tag = CMDTAG_UNKNOWN;
    3242           0 :             break;
    3243             :     }
    3244             : 
    3245      803310 :     return tag;
    3246             : }
    3247             : 
    3248             : 
    3249             : /*
    3250             :  * GetCommandLogLevel
    3251             :  *      utility to get the minimum log_statement level for a command,
    3252             :  *      given either a raw (un-analyzed) parsetree, an analyzed Query,
    3253             :  *      or a PlannedStmt.
    3254             :  *
    3255             :  * This must handle all command types, but since the vast majority
    3256             :  * of 'em are utility commands, it seems sensible to keep it here.
    3257             :  */
    3258             : LogStmtLevel
    3259           0 : GetCommandLogLevel(Node *parsetree)
    3260             : {
    3261             :     LogStmtLevel lev;
    3262             : 
    3263           0 :     switch (nodeTag(parsetree))
    3264             :     {
    3265             :             /* recurse if we're given a RawStmt */
    3266           0 :         case T_RawStmt:
    3267           0 :             lev = GetCommandLogLevel(((RawStmt *) parsetree)->stmt);
    3268           0 :             break;
    3269             : 
    3270             :             /* raw plannable queries */
    3271           0 :         case T_InsertStmt:
    3272             :         case T_DeleteStmt:
    3273             :         case T_UpdateStmt:
    3274             :         case T_MergeStmt:
    3275           0 :             lev = LOGSTMT_MOD;
    3276           0 :             break;
    3277             : 
    3278           0 :         case T_SelectStmt:
    3279           0 :             if (((SelectStmt *) parsetree)->intoClause)
    3280           0 :                 lev = LOGSTMT_DDL;  /* SELECT INTO */
    3281             :             else
    3282           0 :                 lev = LOGSTMT_ALL;
    3283           0 :             break;
    3284             : 
    3285           0 :         case T_PLAssignStmt:
    3286           0 :             lev = LOGSTMT_ALL;
    3287           0 :             break;
    3288             : 
    3289             :             /* utility statements --- same whether raw or cooked */
    3290           0 :         case T_TransactionStmt:
    3291           0 :             lev = LOGSTMT_ALL;
    3292           0 :             break;
    3293             : 
    3294           0 :         case T_DeclareCursorStmt:
    3295           0 :             lev = LOGSTMT_ALL;
    3296           0 :             break;
    3297             : 
    3298           0 :         case T_ClosePortalStmt:
    3299           0 :             lev = LOGSTMT_ALL;
    3300           0 :             break;
    3301             : 
    3302           0 :         case T_FetchStmt:
    3303           0 :             lev = LOGSTMT_ALL;
    3304           0 :             break;
    3305             : 
    3306           0 :         case T_CreateSchemaStmt:
    3307           0 :             lev = LOGSTMT_DDL;
    3308           0 :             break;
    3309             : 
    3310           0 :         case T_CreateStmt:
    3311             :         case T_CreateForeignTableStmt:
    3312           0 :             lev = LOGSTMT_DDL;
    3313           0 :             break;
    3314             : 
    3315           0 :         case T_CreateTableSpaceStmt:
    3316             :         case T_DropTableSpaceStmt:
    3317             :         case T_AlterTableSpaceOptionsStmt:
    3318           0 :             lev = LOGSTMT_DDL;
    3319           0 :             break;
    3320             : 
    3321           0 :         case T_CreateExtensionStmt:
    3322             :         case T_AlterExtensionStmt:
    3323             :         case T_AlterExtensionContentsStmt:
    3324           0 :             lev = LOGSTMT_DDL;
    3325           0 :             break;
    3326             : 
    3327           0 :         case T_CreateFdwStmt:
    3328             :         case T_AlterFdwStmt:
    3329             :         case T_CreateForeignServerStmt:
    3330             :         case T_AlterForeignServerStmt:
    3331             :         case T_CreateUserMappingStmt:
    3332             :         case T_AlterUserMappingStmt:
    3333             :         case T_DropUserMappingStmt:
    3334             :         case T_ImportForeignSchemaStmt:
    3335           0 :             lev = LOGSTMT_DDL;
    3336           0 :             break;
    3337             : 
    3338           0 :         case T_DropStmt:
    3339           0 :             lev = LOGSTMT_DDL;
    3340           0 :             break;
    3341             : 
    3342           0 :         case T_TruncateStmt:
    3343           0 :             lev = LOGSTMT_MOD;
    3344           0 :             break;
    3345             : 
    3346           0 :         case T_CommentStmt:
    3347           0 :             lev = LOGSTMT_DDL;
    3348           0 :             break;
    3349             : 
    3350           0 :         case T_SecLabelStmt:
    3351           0 :             lev = LOGSTMT_DDL;
    3352           0 :             break;
    3353             : 
    3354           0 :         case T_CopyStmt:
    3355           0 :             if (((CopyStmt *) parsetree)->is_from)
    3356           0 :                 lev = LOGSTMT_MOD;
    3357             :             else
    3358           0 :                 lev = LOGSTMT_ALL;
    3359           0 :             break;
    3360             : 
    3361           0 :         case T_PrepareStmt:
    3362             :             {
    3363           0 :                 PrepareStmt *stmt = (PrepareStmt *) parsetree;
    3364             : 
    3365             :                 /* Look through a PREPARE to the contained stmt */
    3366           0 :                 lev = GetCommandLogLevel(stmt->query);
    3367             :             }
    3368           0 :             break;
    3369             : 
    3370           0 :         case T_ExecuteStmt:
    3371             :             {
    3372           0 :                 ExecuteStmt *stmt = (ExecuteStmt *) parsetree;
    3373             :                 PreparedStatement *ps;
    3374             : 
    3375             :                 /* Look through an EXECUTE to the referenced stmt */
    3376           0 :                 ps = FetchPreparedStatement(stmt->name, false);
    3377           0 :                 if (ps && ps->plansource->raw_parse_tree)
    3378           0 :                     lev = GetCommandLogLevel(ps->plansource->raw_parse_tree->stmt);
    3379             :                 else
    3380           0 :                     lev = LOGSTMT_ALL;
    3381             :             }
    3382           0 :             break;
    3383             : 
    3384           0 :         case T_DeallocateStmt:
    3385           0 :             lev = LOGSTMT_ALL;
    3386           0 :             break;
    3387             : 
    3388           0 :         case T_RenameStmt:
    3389           0 :             lev = LOGSTMT_DDL;
    3390           0 :             break;
    3391             : 
    3392           0 :         case T_AlterObjectDependsStmt:
    3393           0 :             lev = LOGSTMT_DDL;
    3394           0 :             break;
    3395             : 
    3396           0 :         case T_AlterObjectSchemaStmt:
    3397           0 :             lev = LOGSTMT_DDL;
    3398           0 :             break;
    3399             : 
    3400           0 :         case T_AlterOwnerStmt:
    3401           0 :             lev = LOGSTMT_DDL;
    3402           0 :             break;
    3403             : 
    3404           0 :         case T_AlterOperatorStmt:
    3405           0 :             lev = LOGSTMT_DDL;
    3406           0 :             break;
    3407             : 
    3408           0 :         case T_AlterTypeStmt:
    3409           0 :             lev = LOGSTMT_DDL;
    3410           0 :             break;
    3411             : 
    3412           0 :         case T_AlterTableMoveAllStmt:
    3413             :         case T_AlterTableStmt:
    3414           0 :             lev = LOGSTMT_DDL;
    3415           0 :             break;
    3416             : 
    3417           0 :         case T_AlterDomainStmt:
    3418           0 :             lev = LOGSTMT_DDL;
    3419           0 :             break;
    3420             : 
    3421           0 :         case T_GrantStmt:
    3422           0 :             lev = LOGSTMT_DDL;
    3423           0 :             break;
    3424             : 
    3425           0 :         case T_GrantRoleStmt:
    3426           0 :             lev = LOGSTMT_DDL;
    3427           0 :             break;
    3428             : 
    3429           0 :         case T_AlterDefaultPrivilegesStmt:
    3430           0 :             lev = LOGSTMT_DDL;
    3431           0 :             break;
    3432             : 
    3433           0 :         case T_DefineStmt:
    3434           0 :             lev = LOGSTMT_DDL;
    3435           0 :             break;
    3436             : 
    3437           0 :         case T_CompositeTypeStmt:
    3438           0 :             lev = LOGSTMT_DDL;
    3439           0 :             break;
    3440             : 
    3441           0 :         case T_CreateEnumStmt:
    3442           0 :             lev = LOGSTMT_DDL;
    3443           0 :             break;
    3444             : 
    3445           0 :         case T_CreateRangeStmt:
    3446           0 :             lev = LOGSTMT_DDL;
    3447           0 :             break;
    3448             : 
    3449           0 :         case T_AlterEnumStmt:
    3450           0 :             lev = LOGSTMT_DDL;
    3451           0 :             break;
    3452             : 
    3453           0 :         case T_ViewStmt:
    3454           0 :             lev = LOGSTMT_DDL;
    3455           0 :             break;
    3456             : 
    3457           0 :         case T_CreateFunctionStmt:
    3458           0 :             lev = LOGSTMT_DDL;
    3459           0 :             break;
    3460             : 
    3461           0 :         case T_AlterFunctionStmt:
    3462           0 :             lev = LOGSTMT_DDL;
    3463           0 :             break;
    3464             : 
    3465           0 :         case T_IndexStmt:
    3466           0 :             lev = LOGSTMT_DDL;
    3467           0 :             break;
    3468             : 
    3469           0 :         case T_RuleStmt:
    3470           0 :             lev = LOGSTMT_DDL;
    3471           0 :             break;
    3472             : 
    3473           0 :         case T_CreateSeqStmt:
    3474           0 :             lev = LOGSTMT_DDL;
    3475           0 :             break;
    3476             : 
    3477           0 :         case T_AlterSeqStmt:
    3478           0 :             lev = LOGSTMT_DDL;
    3479           0 :             break;
    3480             : 
    3481           0 :         case T_DoStmt:
    3482           0 :             lev = LOGSTMT_ALL;
    3483           0 :             break;
    3484             : 
    3485           0 :         case T_CreatedbStmt:
    3486           0 :             lev = LOGSTMT_DDL;
    3487           0 :             break;
    3488             : 
    3489           0 :         case T_AlterDatabaseStmt:
    3490             :         case T_AlterDatabaseRefreshCollStmt:
    3491             :         case T_AlterDatabaseSetStmt:
    3492           0 :             lev = LOGSTMT_DDL;
    3493           0 :             break;
    3494             : 
    3495           0 :         case T_DropdbStmt:
    3496           0 :             lev = LOGSTMT_DDL;
    3497           0 :             break;
    3498             : 
    3499           0 :         case T_NotifyStmt:
    3500           0 :             lev = LOGSTMT_ALL;
    3501           0 :             break;
    3502             : 
    3503           0 :         case T_ListenStmt:
    3504           0 :             lev = LOGSTMT_ALL;
    3505           0 :             break;
    3506             : 
    3507           0 :         case T_UnlistenStmt:
    3508           0 :             lev = LOGSTMT_ALL;
    3509           0 :             break;
    3510             : 
    3511           0 :         case T_LoadStmt:
    3512           0 :             lev = LOGSTMT_ALL;
    3513           0 :             break;
    3514             : 
    3515           0 :         case T_CallStmt:
    3516           0 :             lev = LOGSTMT_ALL;
    3517           0 :             break;
    3518             : 
    3519           0 :         case T_ClusterStmt:
    3520           0 :             lev = LOGSTMT_DDL;
    3521           0 :             break;
    3522             : 
    3523           0 :         case T_VacuumStmt:
    3524           0 :             lev = LOGSTMT_ALL;
    3525           0 :             break;
    3526             : 
    3527           0 :         case T_ExplainStmt:
    3528             :             {
    3529           0 :                 ExplainStmt *stmt = (ExplainStmt *) parsetree;
    3530           0 :                 bool        analyze = false;
    3531             :                 ListCell   *lc;
    3532             : 
    3533             :                 /* Look through an EXPLAIN ANALYZE to the contained stmt */
    3534           0 :                 foreach(lc, stmt->options)
    3535             :                 {
    3536           0 :                     DefElem    *opt = (DefElem *) lfirst(lc);
    3537             : 
    3538           0 :                     if (strcmp(opt->defname, "analyze") == 0)
    3539           0 :                         analyze = defGetBoolean(opt);
    3540             :                     /* don't "break", as explain.c will use the last value */
    3541             :                 }
    3542           0 :                 if (analyze)
    3543           0 :                     return GetCommandLogLevel(stmt->query);
    3544             : 
    3545             :                 /* Plain EXPLAIN isn't so interesting */
    3546           0 :                 lev = LOGSTMT_ALL;
    3547             :             }
    3548           0 :             break;
    3549             : 
    3550           0 :         case T_CreateTableAsStmt:
    3551           0 :             lev = LOGSTMT_DDL;
    3552           0 :             break;
    3553             : 
    3554           0 :         case T_RefreshMatViewStmt:
    3555           0 :             lev = LOGSTMT_DDL;
    3556           0 :             break;
    3557             : 
    3558           0 :         case T_AlterSystemStmt:
    3559           0 :             lev = LOGSTMT_DDL;
    3560           0 :             break;
    3561             : 
    3562           0 :         case T_VariableSetStmt:
    3563           0 :             lev = LOGSTMT_ALL;
    3564           0 :             break;
    3565             : 
    3566           0 :         case T_VariableShowStmt:
    3567           0 :             lev = LOGSTMT_ALL;
    3568           0 :             break;
    3569             : 
    3570           0 :         case T_DiscardStmt:
    3571           0 :             lev = LOGSTMT_ALL;
    3572           0 :             break;
    3573             : 
    3574           0 :         case T_CreateTrigStmt:
    3575           0 :             lev = LOGSTMT_DDL;
    3576           0 :             break;
    3577             : 
    3578           0 :         case T_CreateEventTrigStmt:
    3579           0 :             lev = LOGSTMT_DDL;
    3580           0 :             break;
    3581             : 
    3582           0 :         case T_AlterEventTrigStmt:
    3583           0 :             lev = LOGSTMT_DDL;
    3584           0 :             break;
    3585             : 
    3586           0 :         case T_CreatePLangStmt:
    3587           0 :             lev = LOGSTMT_DDL;
    3588           0 :             break;
    3589             : 
    3590           0 :         case T_CreateDomainStmt:
    3591           0 :             lev = LOGSTMT_DDL;
    3592           0 :             break;
    3593             : 
    3594           0 :         case T_CreateRoleStmt:
    3595           0 :             lev = LOGSTMT_DDL;
    3596           0 :             break;
    3597             : 
    3598           0 :         case T_AlterRoleStmt:
    3599           0 :             lev = LOGSTMT_DDL;
    3600           0 :             break;
    3601             : 
    3602           0 :         case T_AlterRoleSetStmt:
    3603           0 :             lev = LOGSTMT_DDL;
    3604           0 :             break;
    3605             : 
    3606           0 :         case T_DropRoleStmt:
    3607           0 :             lev = LOGSTMT_DDL;
    3608           0 :             break;
    3609             : 
    3610           0 :         case T_DropOwnedStmt:
    3611           0 :             lev = LOGSTMT_DDL;
    3612           0 :             break;
    3613             : 
    3614           0 :         case T_ReassignOwnedStmt:
    3615           0 :             lev = LOGSTMT_DDL;
    3616           0 :             break;
    3617             : 
    3618           0 :         case T_LockStmt:
    3619           0 :             lev = LOGSTMT_ALL;
    3620           0 :             break;
    3621             : 
    3622           0 :         case T_ConstraintsSetStmt:
    3623           0 :             lev = LOGSTMT_ALL;
    3624           0 :             break;
    3625             : 
    3626           0 :         case T_CheckPointStmt:
    3627           0 :             lev = LOGSTMT_ALL;
    3628           0 :             break;
    3629             : 
    3630           0 :         case T_ReindexStmt:
    3631           0 :             lev = LOGSTMT_ALL;  /* should this be DDL? */
    3632           0 :             break;
    3633             : 
    3634           0 :         case T_CreateConversionStmt:
    3635           0 :             lev = LOGSTMT_DDL;
    3636           0 :             break;
    3637             : 
    3638           0 :         case T_CreateCastStmt:
    3639           0 :             lev = LOGSTMT_DDL;
    3640           0 :             break;
    3641             : 
    3642           0 :         case T_CreateOpClassStmt:
    3643           0 :             lev = LOGSTMT_DDL;
    3644           0 :             break;
    3645             : 
    3646           0 :         case T_CreateOpFamilyStmt:
    3647           0 :             lev = LOGSTMT_DDL;
    3648           0 :             break;
    3649             : 
    3650           0 :         case T_CreateTransformStmt:
    3651           0 :             lev = LOGSTMT_DDL;
    3652           0 :             break;
    3653             : 
    3654           0 :         case T_AlterOpFamilyStmt:
    3655           0 :             lev = LOGSTMT_DDL;
    3656           0 :             break;
    3657             : 
    3658           0 :         case T_CreatePolicyStmt:
    3659           0 :             lev = LOGSTMT_DDL;
    3660           0 :             break;
    3661             : 
    3662           0 :         case T_AlterPolicyStmt:
    3663           0 :             lev = LOGSTMT_DDL;
    3664           0 :             break;
    3665             : 
    3666           0 :         case T_AlterTSDictionaryStmt:
    3667           0 :             lev = LOGSTMT_DDL;
    3668           0 :             break;
    3669             : 
    3670           0 :         case T_AlterTSConfigurationStmt:
    3671           0 :             lev = LOGSTMT_DDL;
    3672           0 :             break;
    3673             : 
    3674           0 :         case T_CreateAmStmt:
    3675           0 :             lev = LOGSTMT_DDL;
    3676           0 :             break;
    3677             : 
    3678           0 :         case T_CreatePublicationStmt:
    3679           0 :             lev = LOGSTMT_DDL;
    3680           0 :             break;
    3681             : 
    3682           0 :         case T_AlterPublicationStmt:
    3683           0 :             lev = LOGSTMT_DDL;
    3684           0 :             break;
    3685             : 
    3686           0 :         case T_CreateSubscriptionStmt:
    3687           0 :             lev = LOGSTMT_DDL;
    3688           0 :             break;
    3689             : 
    3690           0 :         case T_AlterSubscriptionStmt:
    3691           0 :             lev = LOGSTMT_DDL;
    3692           0 :             break;
    3693             : 
    3694           0 :         case T_DropSubscriptionStmt:
    3695           0 :             lev = LOGSTMT_DDL;
    3696           0 :             break;
    3697             : 
    3698           0 :         case T_CreateStatsStmt:
    3699           0 :             lev = LOGSTMT_DDL;
    3700           0 :             break;
    3701             : 
    3702           0 :         case T_AlterStatsStmt:
    3703           0 :             lev = LOGSTMT_DDL;
    3704           0 :             break;
    3705             : 
    3706           0 :         case T_AlterCollationStmt:
    3707           0 :             lev = LOGSTMT_DDL;
    3708           0 :             break;
    3709             : 
    3710           0 :         case T_WaitStmt:
    3711           0 :             lev = LOGSTMT_ALL;
    3712           0 :             break;
    3713             : 
    3714             :             /* already-planned queries */
    3715           0 :         case T_PlannedStmt:
    3716             :             {
    3717           0 :                 PlannedStmt *stmt = (PlannedStmt *) parsetree;
    3718             : 
    3719           0 :                 switch (stmt->commandType)
    3720             :                 {
    3721           0 :                     case CMD_SELECT:
    3722           0 :                         lev = LOGSTMT_ALL;
    3723           0 :                         break;
    3724             : 
    3725           0 :                     case CMD_UPDATE:
    3726             :                     case CMD_INSERT:
    3727             :                     case CMD_DELETE:
    3728             :                     case CMD_MERGE:
    3729           0 :                         lev = LOGSTMT_MOD;
    3730           0 :                         break;
    3731             : 
    3732           0 :                     case CMD_UTILITY:
    3733           0 :                         lev = GetCommandLogLevel(stmt->utilityStmt);
    3734           0 :                         break;
    3735             : 
    3736           0 :                     default:
    3737           0 :                         elog(WARNING, "unrecognized commandType: %d",
    3738             :                              (int) stmt->commandType);
    3739           0 :                         lev = LOGSTMT_ALL;
    3740           0 :                         break;
    3741             :                 }
    3742             :             }
    3743           0 :             break;
    3744             : 
    3745             :             /* parsed-and-rewritten-but-not-planned queries */
    3746           0 :         case T_Query:
    3747             :             {
    3748           0 :                 Query      *stmt = (Query *) parsetree;
    3749             : 
    3750           0 :                 switch (stmt->commandType)
    3751             :                 {
    3752           0 :                     case CMD_SELECT:
    3753           0 :                         lev = LOGSTMT_ALL;
    3754           0 :                         break;
    3755             : 
    3756           0 :                     case CMD_UPDATE:
    3757             :                     case CMD_INSERT:
    3758             :                     case CMD_DELETE:
    3759             :                     case CMD_MERGE:
    3760           0 :                         lev = LOGSTMT_MOD;
    3761           0 :                         break;
    3762             : 
    3763           0 :                     case CMD_UTILITY:
    3764           0 :                         lev = GetCommandLogLevel(stmt->utilityStmt);
    3765           0 :                         break;
    3766             : 
    3767           0 :                     default:
    3768           0 :                         elog(WARNING, "unrecognized commandType: %d",
    3769             :                              (int) stmt->commandType);
    3770           0 :                         lev = LOGSTMT_ALL;
    3771           0 :                         break;
    3772             :                 }
    3773             :             }
    3774           0 :             break;
    3775             : 
    3776           0 :         default:
    3777           0 :             elog(WARNING, "unrecognized node type: %d",
    3778             :                  (int) nodeTag(parsetree));
    3779           0 :             lev = LOGSTMT_ALL;
    3780           0 :             break;
    3781             :     }
    3782             : 
    3783           0 :     return lev;
    3784             : }

Generated by: LCOV version 1.16