1 /*-------------------------------------------------------------------------
2  *
3  * utility.h
4  *	  prototypes for utility.c.
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/tcop/utility.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef UTILITY_H
15 #define UTILITY_H
16 
17 #include "tcop/cmdtag.h"
18 #include "tcop/tcopprot.h"
19 
20 typedef enum
21 {
22 	PROCESS_UTILITY_TOPLEVEL,	/* toplevel interactive command */
23 	PROCESS_UTILITY_QUERY,		/* a complete query, but not toplevel */
24 	PROCESS_UTILITY_QUERY_NONATOMIC,	/* a complete query, nonatomic
25 										 * execution context */
26 	PROCESS_UTILITY_SUBCOMMAND	/* a portion of a query */
27 } ProcessUtilityContext;
28 
29 /* Info needed when recursing from ALTER TABLE */
30 typedef struct AlterTableUtilityContext
31 {
32 	PlannedStmt *pstmt;			/* PlannedStmt for outer ALTER TABLE command */
33 	const char *queryString;	/* its query string */
34 	Oid			relid;			/* OID of ALTER's target table */
35 	ParamListInfo params;		/* any parameters available to ALTER TABLE */
36 	QueryEnvironment *queryEnv; /* execution environment for ALTER TABLE */
37 } AlterTableUtilityContext;
38 
39 /*
40  * These constants are used to describe the extent to which a particular
41  * command is read-only.
42  *
43  * COMMAND_OK_IN_READ_ONLY_TXN means that the command is permissible even when
44  * XactReadOnly is set. This bit should be set for commands that don't change
45  * the state of the database (data or schema) in a way that would affect the
46  * output of pg_dump.
47  *
48  * COMMAND_OK_IN_PARALLEL_MODE means that the command is permissible even
49  * when in parallel mode. Writing tuples is forbidden, as is anything that
50  * might confuse cooperating processes.
51  *
52  * COMMAND_OK_IN_RECOVERY means that the command is permissible even when in
53  * recovery. It can't write WAL, nor can it do things that would imperil
54  * replay of future WAL received from the master.
55  */
56 #define COMMAND_OK_IN_READ_ONLY_TXN	0x0001
57 #define COMMAND_OK_IN_PARALLEL_MODE	0x0002
58 #define	COMMAND_OK_IN_RECOVERY		0x0004
59 
60 /*
61  * We say that a command is strictly read-only if it is sufficiently read-only
62  * for all purposes. For clarity, we also have a constant for commands that are
63  * in no way read-only.
64  */
65 #define COMMAND_IS_STRICTLY_READ_ONLY \
66 	(COMMAND_OK_IN_READ_ONLY_TXN | COMMAND_OK_IN_RECOVERY | \
67 	 COMMAND_OK_IN_PARALLEL_MODE)
68 #define COMMAND_IS_NOT_READ_ONLY	0
69 
70 /* Hook for plugins to get control in ProcessUtility() */
71 typedef void (*ProcessUtility_hook_type) (PlannedStmt *pstmt,
72 										  const char *queryString, ProcessUtilityContext context,
73 										  ParamListInfo params,
74 										  QueryEnvironment *queryEnv,
75 										  DestReceiver *dest, QueryCompletion *qc);
76 extern PGDLLIMPORT ProcessUtility_hook_type ProcessUtility_hook;
77 
78 extern void ProcessUtility(PlannedStmt *pstmt, const char *queryString,
79 						   ProcessUtilityContext context, ParamListInfo params,
80 						   QueryEnvironment *queryEnv,
81 						   DestReceiver *dest, QueryCompletion *qc);
82 extern void standard_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
83 									ProcessUtilityContext context, ParamListInfo params,
84 									QueryEnvironment *queryEnv,
85 									DestReceiver *dest, QueryCompletion *qc);
86 
87 extern void ProcessUtilityForAlterTable(Node *stmt,
88 										AlterTableUtilityContext *context);
89 
90 extern bool UtilityReturnsTuples(Node *parsetree);
91 
92 extern TupleDesc UtilityTupleDescriptor(Node *parsetree);
93 
94 extern Query *UtilityContainsQuery(Node *parsetree);
95 
96 extern CommandTag CreateCommandTag(Node *parsetree);
97 
98 static inline const char *
CreateCommandName(Node * parsetree)99 CreateCommandName(Node *parsetree)
100 {
101 	return GetCommandTagName(CreateCommandTag(parsetree));
102 }
103 
104 extern LogStmtLevel GetCommandLogLevel(Node *parsetree);
105 
106 extern bool CommandIsReadOnly(PlannedStmt *pstmt);
107 
108 #endif							/* UTILITY_H */
109