Coverage Report

Created: 2026-12-01 07:00

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