Debian Codegraph @

src/backend/tcop/utility.c

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