PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test_ddl_deparse.c
Go to the documentation of this file.
1/*----------------------------------------------------------------------
2 * test_ddl_deparse.c
3 * Support functions for the test_ddl_deparse module
4 *
5 * Copyright (c) 2014-2025, PostgreSQL Global Development Group
6 *
7 * IDENTIFICATION
8 * src/test/modules/test_ddl_deparse/test_ddl_deparse.c
9 *----------------------------------------------------------------------
10 */
11#include "postgres.h"
12
13#include "funcapi.h"
14#include "nodes/execnodes.h"
16#include "tcop/utility.h"
17#include "utils/builtins.h"
18
20
24
25/*
26 * Return the textual representation of the struct type used to represent a
27 * command in struct CollectedCommand format.
28 */
31{
33 const char *type;
34
35 switch (cmd->type)
36 {
37 case SCT_Simple:
38 type = "simple";
39 break;
40 case SCT_AlterTable:
41 type = "alter table";
42 break;
43 case SCT_Grant:
44 type = "grant";
45 break;
47 type = "alter operator family";
48 break;
50 type = "alter default privileges";
51 break;
53 type = "create operator class";
54 break;
56 type = "alter text search configuration";
57 break;
58 default:
59 type = "unknown command type";
60 break;
61 }
62
64}
65
66/*
67 * Return the command tag corresponding to a parse node contained in a
68 * CollectedCommand struct.
69 */
72{
74
75 if (!cmd->parsetree)
77
79}
80
81/*
82 * Return a text array representation of the subcommands of an ALTER TABLE
83 * command.
84 */
87{
89 ListCell *cell;
90 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
91
92 if (cmd->type != SCT_AlterTable)
93 elog(ERROR, "command is not ALTER TABLE");
94
95 InitMaterializedSRF(fcinfo, 0);
96
97 if (cmd->d.alterTable.subcmds == NIL)
98 elog(ERROR, "empty alter table subcommand list");
99
100 foreach(cell, cmd->d.alterTable.subcmds)
101 {
102 CollectedATSubcmd *sub = lfirst(cell);
104 const char *strtype = "unrecognized";
105 Datum values[2];
106 bool nulls[2];
107
108 memset(values, 0, sizeof(values));
109 memset(nulls, 0, sizeof(nulls));
110
111 switch (subcmd->subtype)
112 {
113 case AT_AddColumn:
114 strtype = "ADD COLUMN";
115 break;
117 strtype = "ADD COLUMN TO VIEW";
118 break;
119 case AT_ColumnDefault:
120 strtype = "ALTER COLUMN SET DEFAULT";
121 break;
123 strtype = "ALTER COLUMN SET DEFAULT (precooked)";
124 break;
125 case AT_DropNotNull:
126 strtype = "DROP NOT NULL";
127 break;
128 case AT_SetNotNull:
129 strtype = "SET NOT NULL";
130 break;
131 case AT_SetExpression:
132 strtype = "SET EXPRESSION";
133 break;
135 strtype = "DROP EXPRESSION";
136 break;
137 case AT_SetStatistics:
138 strtype = "SET STATS";
139 break;
140 case AT_SetOptions:
141 strtype = "SET OPTIONS";
142 break;
143 case AT_ResetOptions:
144 strtype = "RESET OPTIONS";
145 break;
146 case AT_SetStorage:
147 strtype = "SET STORAGE";
148 break;
150 strtype = "SET COMPRESSION";
151 break;
152 case AT_DropColumn:
153 strtype = "DROP COLUMN";
154 break;
155 case AT_AddIndex:
156 strtype = "ADD INDEX";
157 break;
158 case AT_ReAddIndex:
159 strtype = "(re) ADD INDEX";
160 break;
161 case AT_AddConstraint:
162 strtype = "ADD CONSTRAINT";
163 break;
165 strtype = "(re) ADD CONSTRAINT";
166 break;
168 strtype = "(re) ADD DOMAIN CONSTRAINT";
169 break;
171 strtype = "ALTER CONSTRAINT";
172 break;
174 strtype = "VALIDATE CONSTRAINT";
175 break;
177 strtype = "ADD CONSTRAINT (using index)";
178 break;
180 strtype = "DROP CONSTRAINT";
181 break;
182 case AT_ReAddComment:
183 strtype = "(re) ADD COMMENT";
184 break;
186 strtype = "ALTER COLUMN SET TYPE";
187 break;
189 strtype = "ALTER COLUMN SET OPTIONS";
190 break;
191 case AT_ChangeOwner:
192 strtype = "CHANGE OWNER";
193 break;
194 case AT_ClusterOn:
195 strtype = "CLUSTER";
196 break;
197 case AT_DropCluster:
198 strtype = "DROP CLUSTER";
199 break;
200 case AT_SetLogged:
201 strtype = "SET LOGGED";
202 break;
203 case AT_SetUnLogged:
204 strtype = "SET UNLOGGED";
205 break;
206 case AT_DropOids:
207 strtype = "DROP OIDS";
208 break;
210 strtype = "SET ACCESS METHOD";
211 break;
212 case AT_SetTableSpace:
213 strtype = "SET TABLESPACE";
214 break;
215 case AT_SetRelOptions:
216 strtype = "SET RELOPTIONS";
217 break;
219 strtype = "RESET RELOPTIONS";
220 break;
222 strtype = "REPLACE RELOPTIONS";
223 break;
224 case AT_EnableTrig:
225 strtype = "ENABLE TRIGGER";
226 break;
228 strtype = "ENABLE TRIGGER (always)";
229 break;
231 strtype = "ENABLE TRIGGER (replica)";
232 break;
233 case AT_DisableTrig:
234 strtype = "DISABLE TRIGGER";
235 break;
236 case AT_EnableTrigAll:
237 strtype = "ENABLE TRIGGER (all)";
238 break;
240 strtype = "DISABLE TRIGGER (all)";
241 break;
243 strtype = "ENABLE TRIGGER (user)";
244 break;
246 strtype = "DISABLE TRIGGER (user)";
247 break;
248 case AT_EnableRule:
249 strtype = "ENABLE RULE";
250 break;
252 strtype = "ENABLE RULE (always)";
253 break;
255 strtype = "ENABLE RULE (replica)";
256 break;
257 case AT_DisableRule:
258 strtype = "DISABLE RULE";
259 break;
260 case AT_AddInherit:
261 strtype = "ADD INHERIT";
262 break;
263 case AT_DropInherit:
264 strtype = "DROP INHERIT";
265 break;
266 case AT_AddOf:
267 strtype = "OF";
268 break;
269 case AT_DropOf:
270 strtype = "NOT OF";
271 break;
273 strtype = "REPLICA IDENTITY";
274 break;
276 strtype = "ENABLE ROW SECURITY";
277 break;
279 strtype = "DISABLE ROW SECURITY";
280 break;
282 strtype = "FORCE ROW SECURITY";
283 break;
285 strtype = "NO FORCE ROW SECURITY";
286 break;
288 strtype = "SET OPTIONS";
289 break;
291 strtype = "DETACH PARTITION";
292 break;
294 strtype = "ATTACH PARTITION";
295 break;
297 strtype = "DETACH PARTITION ... FINALIZE";
298 break;
299 case AT_AddIdentity:
300 strtype = "ADD IDENTITY";
301 break;
302 case AT_SetIdentity:
303 strtype = "SET IDENTITY";
304 break;
305 case AT_DropIdentity:
306 strtype = "DROP IDENTITY";
307 break;
309 strtype = "(re) ADD STATS";
310 break;
311 }
312
313 if (subcmd->recurse)
314 values[0] = CStringGetTextDatum(psprintf("%s (and recurse)", strtype));
315 else
316 values[0] = CStringGetTextDatum(strtype);
317 if (OidIsValid(sub->address.objectId))
318 {
319 char *objdesc;
320
321 objdesc = getObjectDescription((const ObjectAddress *) &sub->address, false);
322 values[1] = CStringGetTextDatum(objdesc);
323 }
324 else
325 nulls[1] = true;
326
327 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
328 }
329
330 return (Datum) 0;
331}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define OidIsValid(objectId)
Definition: c.h:746
@ SCT_Simple
@ SCT_AlterTSConfig
@ SCT_AlterDefaultPrivileges
@ SCT_Grant
@ SCT_CreateOpClass
@ SCT_AlterOpFamily
@ SCT_AlterTable
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
@ AT_AddIndexConstraint
Definition: parsenodes.h:2430
@ AT_DropOf
Definition: parsenodes.h:2461
@ AT_SetOptions
Definition: parsenodes.h:2418
@ AT_DropIdentity
Definition: parsenodes.h:2473
@ AT_DisableTrigUser
Definition: parsenodes.h:2453
@ AT_DropNotNull
Definition: parsenodes.h:2413
@ AT_AddOf
Definition: parsenodes.h:2460
@ AT_ResetOptions
Definition: parsenodes.h:2419
@ AT_ReplicaIdentity
Definition: parsenodes.h:2462
@ AT_ReplaceRelOptions
Definition: parsenodes.h:2445
@ AT_EnableRowSecurity
Definition: parsenodes.h:2463
@ AT_AddColumnToView
Definition: parsenodes.h:2410
@ AT_ResetRelOptions
Definition: parsenodes.h:2444
@ AT_EnableReplicaTrig
Definition: parsenodes.h:2448
@ AT_DropOids
Definition: parsenodes.h:2440
@ AT_SetIdentity
Definition: parsenodes.h:2472
@ AT_ReAddStatistics
Definition: parsenodes.h:2474
@ AT_SetUnLogged
Definition: parsenodes.h:2439
@ AT_DisableTrig
Definition: parsenodes.h:2449
@ AT_SetCompression
Definition: parsenodes.h:2421
@ AT_DropExpression
Definition: parsenodes.h:2416
@ AT_AddIndex
Definition: parsenodes.h:2423
@ AT_EnableReplicaRule
Definition: parsenodes.h:2456
@ AT_ReAddIndex
Definition: parsenodes.h:2424
@ AT_DropConstraint
Definition: parsenodes.h:2431
@ AT_SetNotNull
Definition: parsenodes.h:2414
@ AT_ClusterOn
Definition: parsenodes.h:2436
@ AT_AddIdentity
Definition: parsenodes.h:2471
@ AT_ForceRowSecurity
Definition: parsenodes.h:2465
@ AT_EnableAlwaysRule
Definition: parsenodes.h:2455
@ AT_SetAccessMethod
Definition: parsenodes.h:2441
@ AT_AlterColumnType
Definition: parsenodes.h:2433
@ AT_DetachPartitionFinalize
Definition: parsenodes.h:2470
@ AT_AddInherit
Definition: parsenodes.h:2458
@ AT_ReAddDomainConstraint
Definition: parsenodes.h:2427
@ AT_EnableTrig
Definition: parsenodes.h:2446
@ AT_DropColumn
Definition: parsenodes.h:2422
@ AT_ReAddComment
Definition: parsenodes.h:2432
@ AT_AlterColumnGenericOptions
Definition: parsenodes.h:2434
@ AT_DisableTrigAll
Definition: parsenodes.h:2451
@ AT_EnableRule
Definition: parsenodes.h:2454
@ AT_NoForceRowSecurity
Definition: parsenodes.h:2466
@ AT_DetachPartition
Definition: parsenodes.h:2469
@ AT_SetStatistics
Definition: parsenodes.h:2417
@ AT_AttachPartition
Definition: parsenodes.h:2468
@ AT_AddConstraint
Definition: parsenodes.h:2425
@ AT_DropInherit
Definition: parsenodes.h:2459
@ AT_EnableAlwaysTrig
Definition: parsenodes.h:2447
@ AT_SetLogged
Definition: parsenodes.h:2438
@ AT_SetStorage
Definition: parsenodes.h:2420
@ AT_DisableRule
Definition: parsenodes.h:2457
@ AT_DisableRowSecurity
Definition: parsenodes.h:2464
@ AT_SetRelOptions
Definition: parsenodes.h:2443
@ AT_ChangeOwner
Definition: parsenodes.h:2435
@ AT_EnableTrigUser
Definition: parsenodes.h:2452
@ AT_SetExpression
Definition: parsenodes.h:2415
@ AT_ReAddConstraint
Definition: parsenodes.h:2426
@ AT_SetTableSpace
Definition: parsenodes.h:2442
@ AT_GenericOptions
Definition: parsenodes.h:2467
@ AT_ColumnDefault
Definition: parsenodes.h:2411
@ AT_CookedColumnDefault
Definition: parsenodes.h:2412
@ AT_AlterConstraint
Definition: parsenodes.h:2428
@ AT_EnableTrigAll
Definition: parsenodes.h:2450
@ AT_DropCluster
Definition: parsenodes.h:2437
@ AT_ValidateConstraint
Definition: parsenodes.h:2429
@ AT_AddColumn
Definition: parsenodes.h:2409
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
uintptr_t Datum
Definition: postgres.h:69
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
AlterTableType subtype
Definition: parsenodes.h:2480
ObjectAddress address
struct CollectedCommand::@128::@130 alterTable
CollectedCommandType type
union CollectedCommand::@128 d
TupleDesc setDesc
Definition: execnodes.h:359
Tuplestorestate * setResult
Definition: execnodes.h:358
PG_MODULE_MAGIC
Datum get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
Datum get_command_type(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(get_command_type)
Datum get_command_tag(PG_FUNCTION_ARGS)
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784
static const char * CreateCommandName(Node *parsetree)
Definition: utility.h:103
text * cstring_to_text(const char *s)
Definition: varlena.c:192
const char * type