Commit c96d92a
Add pg_get_table_ddl() to reconstruct CREATE TABLE statements
The function reconstructs the CREATE TABLE statement for an ordinary or
partitioned table, followed by the ALTER TABLE / CREATE INDEX /
CREATE RULE / CREATE STATISTICS statements needed to restore its full
definition. Each statement is returned as a separate row.
Supported per-column features: data type with type modifiers, COLLATE,
STORAGE, COMPRESSION (pglz / lz4), GENERATED ALWAYS AS (expr)
STORED/VIRTUAL, GENERATED ALWAYS|BY DEFAULT AS IDENTITY (with sequence
options), DEFAULT, NOT NULL (including named NOT NULL constraints), and
per-column attoptions emitted as ALTER COLUMN SET (...).
Supported table-level features: UNLOGGED, INHERITS, PARTITION BY (RANGE
/ LIST / HASH parents), PARTITION OF parent FOR VALUES (FROM/TO, WITH
modulus/remainder, DEFAULT), USING table access method, WITH
(reloptions), TABLESPACE, and inline CHECK constraints in the CREATE
TABLE body.
Supported sub-objects (re-using existing deparse helpers from
ruleutils.c): indexes (including partial and functional) via
pg_get_indexdef_ddl; constraints (PRIMARY KEY with WITHOUT OVERLAPS for
temporal keys, UNIQUE with NULLS NOT DISTINCT and INCLUDE columns,
FOREIGN KEY with ON DELETE/UPDATE referential actions and MATCH clause,
NOT ENFORCED foreign keys, EXCLUDE, named NOT NULL) via
pg_get_constraintdef_body; rules via pg_get_ruledef_ddl; extended
statistics via pg_get_statisticsobjdef_ddl; REPLICA IDENTITY
NOTHING/FULL/USING INDEX; ALTER TABLE ENABLE/FORCE ROW LEVEL SECURITY;
and child-local DEFAULT overrides on inheritance/partition children.
DDL for partition children of a partitioned-table parent is appended
after the parent by default.
The function signature follows the named-parameter convention
established by pg_get_role_ddl(), pg_get_tablespace_ddl(), and
pg_get_database_ddl():
pg_get_table_ddl(relation regclass,
pretty boolean DEFAULT false,
owner boolean DEFAULT true,
tablespace boolean DEFAULT true,
schema_qualified boolean DEFAULT true,
only_kinds text[] DEFAULT NULL,
except_kinds text[] DEFAULT NULL)
pretty controls pretty-printed output. owner controls emission of the
ALTER TABLE ... OWNER TO statement. tablespace controls the TABLESPACE
clause on CREATE TABLE. schema_qualified controls whether object names
are emitted with their schema prefix: when true (the default) the
active search_path is temporarily narrowed to pg_catalog so every
deparse helper produces fully-qualified names; when false it is narrowed
to the target table's own schema so same-schema references come out
unqualified while cross-schema references remain qualified for
correctness. Temporary tables are never schema-qualified regardless of
this setting: the TEMPORARY keyword already places them in pg_temp, and
emitting pg_temp_NN.relname would produce non-replayable DDL.
Object-class filtering uses two mutually-exclusive text-array
parameters, only_kinds and except_kinds. When only_kinds is set, only
the listed kinds are emitted; when except_kinds is set, every kind
except the listed ones is emitted; when neither is set every kind is
emitted. Each array element is matched case-insensitively with
leading/trailing whitespace trimmed; an unrecognized name raises an
error. The kind vocabulary is:
table, index, primary_key, unique, check, foreign_key, exclusion,
rule, statistics, rls, replica_identity, partition
trigger and policy are accepted in the vocabulary but currently produce
no output; they are reserved for when standalone pg_get_trigger_ddl()
and pg_get_policy_ddl() helpers become available.
NOT NULL is not part of the vocabulary: it is always emitted to prevent
producing schemas that silently accept NULLs the source would reject.
When replica_identity is in the active filter and the table's REPLICA
IDENTITY USING INDEX references an index that the filter would suppress,
the function raises an error before emitting any output, so the
generated DDL never references an index it did not produce.
Default omission convention: every optional clause is omitted when its
value matches what the server would reapply on round-trip, including
type-default COLLATE, per-type STORAGE, the auto-generated identity
sequence name and parameter defaults, heap access method, default
REPLICA IDENTITY, disabled RLS toggles, empty reloptions, and the
default tablespace.
Author: Akshay Joshi <akshay.joshi@enterprisedb.com>
Reviewed-by: Marcos Pegoraro <marcos@f10.com.br>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Rui Zhao <zhaorui126@gmail.com>1 parent d34f950 commit c96d92a
13 files changed
Lines changed: 7087 additions & 35 deletions
File tree
- doc/src/sgml/func
- src
- backend
- catalog
- commands
- utils/adt
- include
- catalog
- commands
- utils
- test/regress
- expected
- sql
- tools/pgindent
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3965 | 3965 | | |
3966 | 3966 | | |
3967 | 3967 | | |
| 3968 | + | |
| 3969 | + | |
| 3970 | + | |
| 3971 | + | |
| 3972 | + | |
| 3973 | + | |
| 3974 | + | |
| 3975 | + | |
| 3976 | + | |
| 3977 | + | |
| 3978 | + | |
| 3979 | + | |
| 3980 | + | |
| 3981 | + | |
| 3982 | + | |
| 3983 | + | |
| 3984 | + | |
| 3985 | + | |
| 3986 | + | |
| 3987 | + | |
| 3988 | + | |
| 3989 | + | |
| 3990 | + | |
| 3991 | + | |
| 3992 | + | |
| 3993 | + | |
| 3994 | + | |
| 3995 | + | |
| 3996 | + | |
| 3997 | + | |
| 3998 | + | |
| 3999 | + | |
| 4000 | + | |
| 4001 | + | |
| 4002 | + | |
| 4003 | + | |
| 4004 | + | |
| 4005 | + | |
| 4006 | + | |
| 4007 | + | |
| 4008 | + | |
| 4009 | + | |
| 4010 | + | |
| 4011 | + | |
| 4012 | + | |
| 4013 | + | |
| 4014 | + | |
| 4015 | + | |
| 4016 | + | |
| 4017 | + | |
| 4018 | + | |
| 4019 | + | |
| 4020 | + | |
| 4021 | + | |
| 4022 | + | |
| 4023 | + | |
| 4024 | + | |
| 4025 | + | |
| 4026 | + | |
| 4027 | + | |
| 4028 | + | |
| 4029 | + | |
| 4030 | + | |
| 4031 | + | |
| 4032 | + | |
| 4033 | + | |
| 4034 | + | |
| 4035 | + | |
| 4036 | + | |
| 4037 | + | |
| 4038 | + | |
| 4039 | + | |
| 4040 | + | |
| 4041 | + | |
| 4042 | + | |
| 4043 | + | |
| 4044 | + | |
| 4045 | + | |
| 4046 | + | |
| 4047 | + | |
| 4048 | + | |
| 4049 | + | |
| 4050 | + | |
| 4051 | + | |
| 4052 | + | |
| 4053 | + | |
| 4054 | + | |
| 4055 | + | |
| 4056 | + | |
| 4057 | + | |
| 4058 | + | |
| 4059 | + | |
| 4060 | + | |
| 4061 | + | |
| 4062 | + | |
| 4063 | + | |
| 4064 | + | |
| 4065 | + | |
| 4066 | + | |
| 4067 | + | |
| 4068 | + | |
| 4069 | + | |
| 4070 | + | |
| 4071 | + | |
| 4072 | + | |
| 4073 | + | |
| 4074 | + | |
| 4075 | + | |
| 4076 | + | |
| 4077 | + | |
| 4078 | + | |
| 4079 | + | |
| 4080 | + | |
| 4081 | + | |
| 4082 | + | |
| 4083 | + | |
| 4084 | + | |
| 4085 | + | |
| 4086 | + | |
| 4087 | + | |
| 4088 | + | |
| 4089 | + | |
| 4090 | + | |
| 4091 | + | |
| 4092 | + | |
| 4093 | + | |
| 4094 | + | |
| 4095 | + | |
| 4096 | + | |
| 4097 | + | |
| 4098 | + | |
| 4099 | + | |
| 4100 | + | |
| 4101 | + | |
| 4102 | + | |
| 4103 | + | |
| 4104 | + | |
| 4105 | + | |
| 4106 | + | |
| 4107 | + | |
| 4108 | + | |
| 4109 | + | |
| 4110 | + | |
| 4111 | + | |
| 4112 | + | |
| 4113 | + | |
| 4114 | + | |
| 4115 | + | |
| 4116 | + | |
| 4117 | + | |
| 4118 | + | |
| 4119 | + | |
| 4120 | + | |
| 4121 | + | |
| 4122 | + | |
| 4123 | + | |
| 4124 | + | |
| 4125 | + | |
| 4126 | + | |
| 4127 | + | |
| 4128 | + | |
| 4129 | + | |
| 4130 | + | |
| 4131 | + | |
| 4132 | + | |
| 4133 | + | |
| 4134 | + | |
| 4135 | + | |
| 4136 | + | |
| 4137 | + | |
| 4138 | + | |
| 4139 | + | |
| 4140 | + | |
| 4141 | + | |
| 4142 | + | |
| 4143 | + | |
| 4144 | + | |
| 4145 | + | |
| 4146 | + | |
| 4147 | + | |
| 4148 | + | |
| 4149 | + | |
| 4150 | + | |
| 4151 | + | |
| 4152 | + | |
| 4153 | + | |
| 4154 | + | |
| 4155 | + | |
| 4156 | + | |
| 4157 | + | |
| 4158 | + | |
| 4159 | + | |
| 4160 | + | |
| 4161 | + | |
| 4162 | + | |
| 4163 | + | |
| 4164 | + | |
| 4165 | + | |
| 4166 | + | |
| 4167 | + | |
| 4168 | + | |
| 4169 | + | |
| 4170 | + | |
| 4171 | + | |
| 4172 | + | |
| 4173 | + | |
| 4174 | + | |
| 4175 | + | |
| 4176 | + | |
| 4177 | + | |
| 4178 | + | |
3968 | 4179 | | |
3969 | 4180 | | |
3970 | 4181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
239 | 332 | | |
240 | 333 | | |
241 | 334 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
747 | 747 | | |
748 | 748 | | |
749 | 749 | | |
750 | | - | |
751 | 750 | | |
752 | 751 | | |
753 | 752 | | |
| |||
2525 | 2524 | | |
2526 | 2525 | | |
2527 | 2526 | | |
2528 | | - | |
| 2527 | + | |
2529 | 2528 | | |
2530 | 2529 | | |
2531 | 2530 | | |
| |||
20003 | 20002 | | |
20004 | 20003 | | |
20005 | 20004 | | |
| 20005 | + | |
| 20006 | + | |
| 20007 | + | |
| 20008 | + | |
| 20009 | + | |
| 20010 | + | |
| 20011 | + | |
| 20012 | + | |
| 20013 | + | |
| 20014 | + | |
| 20015 | + | |
| 20016 | + | |
| 20017 | + | |
| 20018 | + | |
| 20019 | + | |
| 20020 | + | |
| 20021 | + | |
| 20022 | + | |
| 20023 | + | |
| 20024 | + | |
| 20025 | + | |
| 20026 | + | |
| 20027 | + | |
| 20028 | + | |
| 20029 | + | |
| 20030 | + | |
| 20031 | + | |
20006 | 20032 | | |
20007 | 20033 | | |
20008 | 20034 | | |
| |||
0 commit comments