Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_dump.c
4 : : * pg_dump is a utility for dumping out a postgres database
5 : : * into a script file.
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * pg_dump will read the system catalogs in a database and dump out a
11 : : * script that reproduces the schema in terms of SQL that is understood
12 : : * by PostgreSQL
13 : : *
14 : : * Note that pg_dump runs in a transaction-snapshot mode transaction,
15 : : * so it sees a consistent snapshot of the database including system
16 : : * catalogs. However, it relies in part on various specialized backend
17 : : * functions like pg_get_indexdef(), and those things tend to look at
18 : : * the currently committed state. So it is possible to get 'cache
19 : : * lookup failed' error if someone performs DDL changes while a dump is
20 : : * happening. The window for this sort of thing is from the acquisition
21 : : * of the transaction snapshot to getSchemaData() (when pg_dump acquires
22 : : * AccessShareLock on every table it intends to dump). It isn't very large,
23 : : * but it can happen.
24 : : *
25 : : * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
26 : : *
27 : : * IDENTIFICATION
28 : : * src/bin/pg_dump/pg_dump.c
29 : : *
30 : : *-------------------------------------------------------------------------
31 : : */
32 : : #include "postgres_fe.h"
33 : :
34 : : #include <unistd.h>
35 : : #include <ctype.h>
36 : : #include <limits.h>
37 : : #ifdef HAVE_TERMIOS_H
38 : : #include <termios.h>
39 : : #endif
40 : :
41 : : #include "access/attnum.h"
42 : : #include "access/sysattr.h"
43 : : #include "access/transam.h"
44 : : #include "access/xlogdefs.h"
45 : : #include "catalog/pg_aggregate_d.h"
46 : : #include "catalog/pg_am_d.h"
47 : : #include "catalog/pg_attribute_d.h"
48 : : #include "catalog/pg_authid_d.h"
49 : : #include "catalog/pg_cast_d.h"
50 : : #include "catalog/pg_class_d.h"
51 : : #include "catalog/pg_constraint_d.h"
52 : : #include "catalog/pg_default_acl_d.h"
53 : : #include "catalog/pg_largeobject_d.h"
54 : : #include "catalog/pg_largeobject_metadata_d.h"
55 : : #include "catalog/pg_proc_d.h"
56 : : #include "catalog/pg_publication_d.h"
57 : : #include "catalog/pg_shdepend_d.h"
58 : : #include "catalog/pg_subscription_d.h"
59 : : #include "catalog/pg_type_d.h"
60 : : #include "common/connect.h"
61 : : #include "common/int.h"
62 : : #include "common/relpath.h"
63 : : #include "common/shortest_dec.h"
64 : : #include "compress_io.h"
65 : : #include "dumputils.h"
66 : : #include "fe_utils/option_utils.h"
67 : : #include "fe_utils/string_utils.h"
68 : : #include "filter.h"
69 : : #include "getopt_long.h"
70 : : #include "libpq/libpq-fs.h"
71 : : #include "parallel.h"
72 : : #include "pg_backup_db.h"
73 : : #include "pg_backup_utils.h"
74 : : #include "pg_dump.h"
75 : : #include "statistics/statistics_format.h"
76 : : #include "storage/block.h"
77 : :
78 : : typedef struct
79 : : {
80 : : Oid roleoid; /* role's OID */
81 : : const char *rolename; /* role's name */
82 : : } RoleNameItem;
83 : :
84 : : typedef struct
85 : : {
86 : : const char *descr; /* comment for an object */
87 : : Oid classoid; /* object class (catalog OID) */
88 : : Oid objoid; /* object OID */
89 : : int objsubid; /* subobject (table column #) */
90 : : } CommentItem;
91 : :
92 : : typedef struct
93 : : {
94 : : const char *provider; /* label provider of this security label */
95 : : const char *label; /* security label for an object */
96 : : Oid classoid; /* object class (catalog OID) */
97 : : Oid objoid; /* object OID */
98 : : int objsubid; /* subobject (table column #) */
99 : : } SecLabelItem;
100 : :
101 : : typedef struct
102 : : {
103 : : Oid oid; /* object OID */
104 : : char relkind; /* object kind */
105 : : RelFileNumber relfilenumber; /* object filenode */
106 : : Oid toast_oid; /* toast table OID */
107 : : RelFileNumber toast_relfilenumber; /* toast table filenode */
108 : : Oid toast_chunk_id_typoid; /* type of chunk_id attribute */
109 : : Oid toast_index_oid; /* toast table index OID */
110 : : RelFileNumber toast_index_relfilenumber; /* toast table index filenode */
111 : : } BinaryUpgradeClassOidItem;
112 : :
113 : : /* sequence types */
114 : : typedef enum SeqType
115 : : {
116 : : SEQTYPE_SMALLINT,
117 : : SEQTYPE_INTEGER,
118 : : SEQTYPE_BIGINT,
119 : : } SeqType;
120 : :
121 : : static const char *const SeqTypeNames[] =
122 : : {
123 : : [SEQTYPE_SMALLINT] = "smallint",
124 : : [SEQTYPE_INTEGER] = "integer",
125 : : [SEQTYPE_BIGINT] = "bigint",
126 : : };
127 : :
128 : : StaticAssertDecl(lengthof(SeqTypeNames) == (SEQTYPE_BIGINT + 1),
129 : : "array length mismatch");
130 : :
131 : : typedef struct
132 : : {
133 : : Oid oid; /* sequence OID */
134 : : SeqType seqtype; /* data type of sequence */
135 : : bool cycled; /* whether sequence cycles */
136 : : int64 minv; /* minimum value */
137 : : int64 maxv; /* maximum value */
138 : : int64 startv; /* start value */
139 : : int64 incby; /* increment value */
140 : : int64 cache; /* cache size */
141 : : int64 last_value; /* last value of sequence */
142 : : bool is_called; /* whether nextval advances before returning */
143 : : bool null_seqtuple; /* did pg_get_sequence_data return nulls? */
144 : : } SequenceItem;
145 : :
146 : : typedef enum OidOptions
147 : : {
148 : : zeroIsError = 1,
149 : : zeroAsStar = 2,
150 : : zeroAsNone = 4,
151 : : } OidOptions;
152 : :
153 : : /* global decls */
154 : : static bool dosync = true; /* Issue fsync() to make dump durable on disk. */
155 : :
156 : : static Oid g_last_builtin_oid; /* value of the last builtin oid */
157 : :
158 : : /* The specified names/patterns should to match at least one entity */
159 : : static int strict_names = 0;
160 : :
161 : : static pg_compress_algorithm compression_algorithm = PG_COMPRESSION_NONE;
162 : :
163 : : /*
164 : : * Object inclusion/exclusion lists
165 : : *
166 : : * The string lists record the patterns given by command-line switches,
167 : : * which we then convert to lists of OIDs of matching objects.
168 : : */
169 : : static SimpleStringList schema_include_patterns = {NULL, NULL};
170 : : static SimpleOidList schema_include_oids = {NULL, NULL};
171 : : static SimpleStringList schema_exclude_patterns = {NULL, NULL};
172 : : static SimpleOidList schema_exclude_oids = {NULL, NULL};
173 : :
174 : : static SimpleStringList table_include_patterns = {NULL, NULL};
175 : : static SimpleStringList table_include_patterns_and_children = {NULL, NULL};
176 : : static SimpleOidList table_include_oids = {NULL, NULL};
177 : : static SimpleStringList table_exclude_patterns = {NULL, NULL};
178 : : static SimpleStringList table_exclude_patterns_and_children = {NULL, NULL};
179 : : static SimpleOidList table_exclude_oids = {NULL, NULL};
180 : : static SimpleStringList tabledata_exclude_patterns = {NULL, NULL};
181 : : static SimpleStringList tabledata_exclude_patterns_and_children = {NULL, NULL};
182 : : static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
183 : :
184 : : static SimpleStringList foreign_servers_include_patterns = {NULL, NULL};
185 : : static SimpleOidList foreign_servers_include_oids = {NULL, NULL};
186 : :
187 : : static SimpleStringList extension_include_patterns = {NULL, NULL};
188 : : static SimpleOidList extension_include_oids = {NULL, NULL};
189 : :
190 : : static SimpleStringList extension_exclude_patterns = {NULL, NULL};
191 : : static SimpleOidList extension_exclude_oids = {NULL, NULL};
192 : :
193 : : static const CatalogId nilCatalogId = {0, 0};
194 : :
195 : : /* override for standard extra_float_digits setting */
196 : : static bool have_extra_float_digits = false;
197 : : static int extra_float_digits;
198 : :
199 : : /* sorted table of role names */
200 : : static RoleNameItem *rolenames = NULL;
201 : : static int nrolenames = 0;
202 : :
203 : : /* sorted table of comments */
204 : : static CommentItem *comments = NULL;
205 : : static int ncomments = 0;
206 : :
207 : : /* sorted table of security labels */
208 : : static SecLabelItem *seclabels = NULL;
209 : : static int nseclabels = 0;
210 : :
211 : : /* sorted table of pg_class information for binary upgrade */
212 : : static BinaryUpgradeClassOidItem *binaryUpgradeClassOids = NULL;
213 : : static int nbinaryUpgradeClassOids = 0;
214 : :
215 : : /* sorted table of sequences */
216 : : static SequenceItem *sequences = NULL;
217 : : static int nsequences = 0;
218 : :
219 : : /* Maximum number of relations to fetch in a fetchAttributeStats() call. */
220 : : #define MAX_ATTR_STATS_RELS 64
221 : :
222 : : /*
223 : : * The default number of rows per INSERT when
224 : : * --inserts is specified without --rows-per-insert
225 : : */
226 : : #define DUMP_DEFAULT_ROWS_PER_INSERT 1
227 : :
228 : : /*
229 : : * Maximum number of large objects to group into a single ArchiveEntry.
230 : : * At some point we might want to make this user-controllable, but for now
231 : : * a hard-wired setting will suffice.
232 : : */
233 : : #define MAX_BLOBS_PER_ARCHIVE_ENTRY 1000
234 : :
235 : : /*
236 : : * Macro for producing quoted, schema-qualified name of a dumpable object.
237 : : */
238 : : #define fmtQualifiedDumpable(obj) \
239 : : fmtQualifiedId((obj)->dobj.namespace->dobj.name, \
240 : : (obj)->dobj.name)
241 : :
242 : : static void help(const char *progname);
243 : : static void setup_connection(Archive *AH,
244 : : const char *dumpencoding, const char *dumpsnapshot,
245 : : char *use_role);
246 : : static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
247 : : static void expand_schema_name_patterns(Archive *fout,
248 : : SimpleStringList *patterns,
249 : : SimpleOidList *oids,
250 : : bool strict_names);
251 : : static void expand_extension_name_patterns(Archive *fout,
252 : : SimpleStringList *patterns,
253 : : SimpleOidList *oids,
254 : : bool strict_names);
255 : : static void expand_foreign_server_name_patterns(Archive *fout,
256 : : SimpleStringList *patterns,
257 : : SimpleOidList *oids);
258 : : static void expand_table_name_patterns(Archive *fout,
259 : : SimpleStringList *patterns,
260 : : SimpleOidList *oids,
261 : : bool strict_names,
262 : : bool with_child_tables);
263 : : static void prohibit_crossdb_refs(PGconn *conn, const char *dbname,
264 : : const char *pattern);
265 : :
266 : : static NamespaceInfo *findNamespace(Oid nsoid);
267 : : static void dumpTableData(Archive *fout, const TableDataInfo *tdinfo);
268 : : static void refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo);
269 : : static const char *getRoleName(const char *roleoid_str);
270 : : static void collectRoleNames(Archive *fout);
271 : : static void getAdditionalACLs(Archive *fout);
272 : : static void dumpCommentExtended(Archive *fout, const char *type,
273 : : const char *name, const char *namespace,
274 : : const char *owner, CatalogId catalogId,
275 : : int subid, DumpId dumpId,
276 : : const char *initdb_comment);
277 : : static inline void dumpComment(Archive *fout, const char *type,
278 : : const char *name, const char *namespace,
279 : : const char *owner, CatalogId catalogId,
280 : : int subid, DumpId dumpId);
281 : : static int findComments(Oid classoid, Oid objoid, CommentItem **items);
282 : : static void collectComments(Archive *fout);
283 : : static void dumpSecLabel(Archive *fout, const char *type, const char *name,
284 : : const char *namespace, const char *owner,
285 : : CatalogId catalogId, int subid, DumpId dumpId);
286 : : static int findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items);
287 : : static void collectSecLabels(Archive *fout);
288 : : static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
289 : : static void dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo);
290 : : static void dumpExtension(Archive *fout, const ExtensionInfo *extinfo);
291 : : static void dumpType(Archive *fout, const TypeInfo *tyinfo);
292 : : static void dumpBaseType(Archive *fout, const TypeInfo *tyinfo);
293 : : static void dumpEnumType(Archive *fout, const TypeInfo *tyinfo);
294 : : static void dumpRangeType(Archive *fout, const TypeInfo *tyinfo);
295 : : static void dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo);
296 : : static void dumpDomain(Archive *fout, const TypeInfo *tyinfo);
297 : : static void dumpCompositeType(Archive *fout, const TypeInfo *tyinfo);
298 : : static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
299 : : PGresult *res);
300 : : static void dumpShellType(Archive *fout, const ShellTypeInfo *stinfo);
301 : : static void dumpProcLang(Archive *fout, const ProcLangInfo *plang);
302 : : static void dumpFunc(Archive *fout, const FuncInfo *finfo);
303 : : static void dumpCast(Archive *fout, const CastInfo *cast);
304 : : static void dumpTransform(Archive *fout, const TransformInfo *transform);
305 : : static void dumpOpr(Archive *fout, const OprInfo *oprinfo);
306 : : static void dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo);
307 : : static void dumpOpclass(Archive *fout, const OpclassInfo *opcinfo);
308 : : static void dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo);
309 : : static void dumpCollation(Archive *fout, const CollInfo *collinfo);
310 : : static void dumpConversion(Archive *fout, const ConvInfo *convinfo);
311 : : static void dumpRule(Archive *fout, const RuleInfo *rinfo);
312 : : static void dumpAgg(Archive *fout, const AggInfo *agginfo);
313 : : static void dumpTrigger(Archive *fout, const TriggerInfo *tginfo);
314 : : static void dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo);
315 : : static void dumpTable(Archive *fout, const TableInfo *tbinfo);
316 : : static void dumpTableSchema(Archive *fout, const TableInfo *tbinfo);
317 : : static void dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo);
318 : : static void dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo);
319 : : static void collectSequences(Archive *fout);
320 : : static void dumpSequence(Archive *fout, const TableInfo *tbinfo);
321 : : static void dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo);
322 : : static void dumpIndex(Archive *fout, const IndxInfo *indxinfo);
323 : : static void dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo);
324 : : static void dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo);
325 : : static void dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo);
326 : : static void dumpConstraint(Archive *fout, const ConstraintInfo *coninfo);
327 : : static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo);
328 : : static void dumpTSParser(Archive *fout, const TSParserInfo *prsinfo);
329 : : static void dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo);
330 : : static void dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo);
331 : : static void dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo);
332 : : static void dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo);
333 : : static void dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo);
334 : : static void dumpUserMappings(Archive *fout,
335 : : const char *servername, const char *namespace,
336 : : const char *owner, CatalogId catalogId, DumpId dumpId);
337 : : static void dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo);
338 : :
339 : : static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
340 : : const char *type, const char *name, const char *subname,
341 : : const char *nspname, const char *tag, const char *owner,
342 : : const DumpableAcl *dacl);
343 : :
344 : : static void getDependencies(Archive *fout);
345 : : static void BuildArchiveDependencies(Archive *fout);
346 : : static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
347 : : DumpId **dependencies, int *nDeps, int *allocDeps);
348 : :
349 : : static DumpableObject *createBoundaryObjects(void);
350 : : static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
351 : : DumpableObject *boundaryObjs);
352 : :
353 : : static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx);
354 : : static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
355 : : static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
356 : : static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
357 : : static void buildMatViewRefreshDependencies(Archive *fout);
358 : : static void getTableDataFKConstraints(void);
359 : : static void determineNotNullFlags(Archive *fout, PGresult *res, int r,
360 : : TableInfo *tbinfo, int j,
361 : : int i_notnull_name,
362 : : int i_notnull_comment,
363 : : int i_notnull_invalidoid,
364 : : int i_notnull_noinherit,
365 : : int i_notnull_islocal,
366 : : PQExpBuffer *invalidnotnulloids);
367 : : static char *format_function_arguments(const FuncInfo *finfo, const char *funcargs,
368 : : bool is_agg);
369 : : static char *format_function_signature(Archive *fout,
370 : : const FuncInfo *finfo, bool honor_quotes);
371 : : static char *convertRegProcReference(const char *proc);
372 : : static char *getFormattedOperatorName(const char *oproid);
373 : : static char *convertTSFunction(Archive *fout, Oid funcOid);
374 : : static const char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
375 : : static void getLOs(Archive *fout);
376 : : static void dumpLO(Archive *fout, const LoInfo *loinfo);
377 : : static int dumpLOs(Archive *fout, const void *arg);
378 : : static void dumpPolicy(Archive *fout, const PolicyInfo *polinfo);
379 : : static void dumpPublication(Archive *fout, const PublicationInfo *pubinfo);
380 : : static void dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo);
381 : : static void dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo);
382 : : static void dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo);
383 : : static void dumpDatabase(Archive *fout);
384 : : static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
385 : : const char *dbname, Oid dboid);
386 : : static void dumpEncoding(Archive *AH);
387 : : static void dumpStdStrings(Archive *AH);
388 : : static void dumpSearchPath(Archive *AH);
389 : : static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
390 : : PQExpBuffer upgrade_buffer,
391 : : Oid pg_type_oid,
392 : : bool force_array_type,
393 : : bool include_multirange_type);
394 : : static void binary_upgrade_set_type_oids_by_rel(Archive *fout,
395 : : PQExpBuffer upgrade_buffer,
396 : : const TableInfo *tbinfo);
397 : : static void collectBinaryUpgradeClassOids(Archive *fout);
398 : : static void binary_upgrade_set_pg_class_oids(Archive *fout,
399 : : PQExpBuffer upgrade_buffer,
400 : : Oid pg_class_oid);
401 : : static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
402 : : const DumpableObject *dobj,
403 : : const char *objtype,
404 : : const char *objname,
405 : : const char *objnamespace);
406 : : static void binary_upgrade_set_next_attribute_versions(PQExpBuffer upgrade_buffer,
407 : : int numatts,
408 : : const uint64 *attversions);
409 : : static const char *getAttrName(int attrnum, const TableInfo *tblInfo);
410 : : static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
411 : : static bool nonemptyReloptions(const char *reloptions);
412 : : static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
413 : : const char *prefix, Archive *fout);
414 : : static char *get_synchronized_snapshot(Archive *fout);
415 : : static void set_restrict_relation_kind(Archive *AH, const char *value);
416 : : static void setupDumpWorker(Archive *AH);
417 : : static TableInfo *getRootTableInfo(const TableInfo *tbinfo);
418 : : static bool forcePartitionRootLoad(const TableInfo *tbinfo);
419 : : static void read_dump_filters(const char *filename, DumpOptions *dopt);
420 : :
421 : :
422 : : int
423 : 333 : main(int argc, char **argv)
424 : : {
425 : : int c;
426 : 333 : const char *filename = NULL;
427 : 333 : const char *format = "p";
428 : : TableInfo *tblinfo;
429 : : int numTables;
430 : : DumpableObject **dobjs;
431 : : int numObjs;
432 : : DumpableObject *boundaryObjs;
433 : : int i;
434 : : int optindex;
435 : : RestoreOptions *ropt;
436 : : Archive *fout; /* the script file */
437 : 333 : bool g_verbose = false;
438 : 333 : const char *dumpencoding = NULL;
439 : 333 : const char *dumpsnapshot = NULL;
440 : 333 : char *use_role = NULL;
441 : 333 : int numWorkers = 1;
442 : 333 : int plainText = 0;
443 : 333 : ArchiveFormat archiveFormat = archUnknown;
444 : : ArchiveMode archiveMode;
445 : 333 : pg_compress_specification compression_spec = {0};
446 : 333 : char *compression_detail = NULL;
447 : 333 : char *compression_algorithm_str = "none";
448 : 333 : char *error_detail = NULL;
449 : 333 : bool user_compression_defined = false;
450 : 333 : DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
451 : 333 : bool data_only = false;
452 : 333 : bool schema_only = false;
453 : 333 : bool statistics_only = false;
454 : 333 : bool with_statistics = false;
455 : 333 : bool no_data = false;
456 : 333 : bool no_schema = false;
457 : 333 : bool no_statistics = false;
458 : :
459 : : static DumpOptions dopt;
460 : :
461 : : static struct option long_options[] = {
462 : : {"data-only", no_argument, NULL, 'a'},
463 : : {"blobs", no_argument, NULL, 'b'},
464 : : {"large-objects", no_argument, NULL, 'b'},
465 : : {"no-blobs", no_argument, NULL, 'B'},
466 : : {"no-large-objects", no_argument, NULL, 'B'},
467 : : {"clean", no_argument, NULL, 'c'},
468 : : {"create", no_argument, NULL, 'C'},
469 : : {"dbname", required_argument, NULL, 'd'},
470 : : {"extension", required_argument, NULL, 'e'},
471 : : {"file", required_argument, NULL, 'f'},
472 : : {"format", required_argument, NULL, 'F'},
473 : : {"host", required_argument, NULL, 'h'},
474 : : {"jobs", 1, NULL, 'j'},
475 : : {"no-reconnect", no_argument, NULL, 'R'},
476 : : {"no-owner", no_argument, NULL, 'O'},
477 : : {"port", required_argument, NULL, 'p'},
478 : : {"schema", required_argument, NULL, 'n'},
479 : : {"exclude-schema", required_argument, NULL, 'N'},
480 : : {"schema-only", no_argument, NULL, 's'},
481 : : {"superuser", required_argument, NULL, 'S'},
482 : : {"table", required_argument, NULL, 't'},
483 : : {"exclude-table", required_argument, NULL, 'T'},
484 : : {"no-password", no_argument, NULL, 'w'},
485 : : {"password", no_argument, NULL, 'W'},
486 : : {"username", required_argument, NULL, 'U'},
487 : : {"verbose", no_argument, NULL, 'v'},
488 : : {"no-privileges", no_argument, NULL, 'x'},
489 : : {"no-acl", no_argument, NULL, 'x'},
490 : : {"compress", required_argument, NULL, 'Z'},
491 : : {"encoding", required_argument, NULL, 'E'},
492 : : {"help", no_argument, NULL, '?'},
493 : : {"version", no_argument, NULL, 'V'},
494 : :
495 : : /*
496 : : * the following options don't have an equivalent short option letter
497 : : */
498 : : {"attribute-inserts", no_argument, &dopt.column_inserts, 1},
499 : : {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1},
500 : : {"column-inserts", no_argument, &dopt.column_inserts, 1},
501 : : {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1},
502 : : {"disable-triggers", no_argument, &dopt.disable_triggers, 1},
503 : : {"enable-row-security", no_argument, &dopt.enable_row_security, 1},
504 : : {"exclude-table-data", required_argument, NULL, 4},
505 : : {"extra-float-digits", required_argument, NULL, 8},
506 : : {"if-exists", no_argument, &dopt.if_exists, 1},
507 : : {"inserts", no_argument, NULL, 9},
508 : : {"lock-wait-timeout", required_argument, NULL, 2},
509 : : {"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1},
510 : : {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
511 : : {"quote-all-identifiers", no_argument, "e_all_identifiers, 1},
512 : : {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
513 : : {"role", required_argument, NULL, 3},
514 : : {"section", required_argument, NULL, 5},
515 : : {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
516 : : {"snapshot", required_argument, NULL, 6},
517 : : {"statistics", no_argument, NULL, 22},
518 : : {"statistics-only", no_argument, NULL, 18},
519 : : {"strict-names", no_argument, &strict_names, 1},
520 : : {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1},
521 : : {"no-comments", no_argument, &dopt.no_comments, 1},
522 : : {"no-data", no_argument, NULL, 19},
523 : : {"no-policies", no_argument, &dopt.no_policies, 1},
524 : : {"no-publications", no_argument, &dopt.no_publications, 1},
525 : : {"no-schema", no_argument, NULL, 20},
526 : : {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
527 : : {"no-statistics", no_argument, NULL, 21},
528 : : {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
529 : : {"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
530 : : {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
531 : : {"no-sync", no_argument, NULL, 7},
532 : : {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
533 : : {"rows-per-insert", required_argument, NULL, 10},
534 : : {"include-foreign-data", required_argument, NULL, 11},
535 : : {"table-and-children", required_argument, NULL, 12},
536 : : {"exclude-table-and-children", required_argument, NULL, 13},
537 : : {"exclude-table-data-and-children", required_argument, NULL, 14},
538 : : {"sync-method", required_argument, NULL, 15},
539 : : {"filter", required_argument, NULL, 16},
540 : : {"exclude-extension", required_argument, NULL, 17},
541 : : {"sequence-data", no_argument, &dopt.sequence_data, 1},
542 : : {"restrict-key", required_argument, NULL, 25},
543 : :
544 : : {NULL, 0, NULL, 0}
545 : : };
546 : :
547 : 333 : pg_logging_init(argv[0]);
548 : 333 : pg_logging_set_level(PG_LOG_WARNING);
549 : 333 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
550 : :
551 : : /*
552 : : * Initialize what we need for parallel execution, especially for thread
553 : : * support on Windows.
554 : : */
555 : 333 : init_parallel_dump_utils();
556 : :
557 : 333 : progname = get_progname(argv[0]);
558 : :
559 [ + - ]: 333 : if (argc > 1)
560 : : {
561 [ + + - + ]: 333 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
562 : : {
563 : 1 : help(progname);
564 : 1 : exit_nicely(0);
565 : : }
566 [ + + + + ]: 332 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
567 : : {
568 : 72 : puts("pg_dump (PostgreSQL) " PG_VERSION);
569 : 72 : exit_nicely(0);
570 : : }
571 : : }
572 : :
573 : 260 : InitDumpOptions(&dopt);
574 : :
575 : 1498 : while ((c = getopt_long(argc, argv, "abBcCd:e:E:f:F:h:j:n:N:Op:RsS:t:T:U:vwWxXZ:",
576 [ + + ]: 1498 : long_options, &optindex)) != -1)
577 : : {
578 [ + + + + : 1246 : switch (c)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + - +
+ + + + +
+ + - + +
+ + + + +
+ + ]
579 : : {
580 : 9 : case 'a': /* Dump data only */
581 : 9 : data_only = true;
582 : 9 : break;
583 : :
584 : 1 : case 'b': /* Dump LOs */
585 : 1 : dopt.outputLOs = true;
586 : 1 : break;
587 : :
588 : 2 : case 'B': /* Don't dump LOs */
589 : 2 : dopt.dontOutputLOs = true;
590 : 2 : break;
591 : :
592 : 18 : case 'c': /* clean (i.e., drop) schema prior to create */
593 : 18 : dopt.outputClean = 1;
594 : 18 : break;
595 : :
596 : 35 : case 'C': /* Create DB */
597 : 35 : dopt.outputCreateDB = 1;
598 : 35 : break;
599 : :
600 : 5 : case 'd': /* database name */
601 : 5 : dopt.cparams.dbname = pg_strdup(optarg);
602 : 5 : break;
603 : :
604 : 4 : case 'e': /* include extension(s) */
605 : 4 : simple_string_list_append(&extension_include_patterns, optarg);
606 : 4 : dopt.include_everything = false;
607 : 4 : break;
608 : :
609 : 3 : case 'E': /* Dump encoding */
610 : 3 : dumpencoding = pg_strdup(optarg);
611 : 3 : break;
612 : :
613 : 213 : case 'f':
614 : 213 : filename = pg_strdup(optarg);
615 : 213 : break;
616 : :
617 : 132 : case 'F':
618 : 132 : format = pg_strdup(optarg);
619 : 132 : break;
620 : :
621 : 42 : case 'h': /* server host */
622 : 42 : dopt.cparams.pghost = pg_strdup(optarg);
623 : 42 : break;
624 : :
625 : 11 : case 'j': /* number of dump jobs */
626 [ + + ]: 11 : if (!option_parse_int(optarg, "-j/--jobs", 1,
627 : : PG_MAX_JOBS,
628 : : &numWorkers))
629 : 1 : exit_nicely(1);
630 : 10 : break;
631 : :
632 : 18 : case 'n': /* include schema(s) */
633 : 18 : simple_string_list_append(&schema_include_patterns, optarg);
634 : 18 : dopt.include_everything = false;
635 : 18 : break;
636 : :
637 : 1 : case 'N': /* exclude schema(s) */
638 : 1 : simple_string_list_append(&schema_exclude_patterns, optarg);
639 : 1 : break;
640 : :
641 : 2 : case 'O': /* Don't reconnect to match owner */
642 : 2 : dopt.outputNoOwner = 1;
643 : 2 : break;
644 : :
645 : 81 : case 'p': /* server port */
646 : 81 : dopt.cparams.pgport = pg_strdup(optarg);
647 : 81 : break;
648 : :
649 : 2 : case 'R':
650 : : /* no-op, still accepted for backwards compatibility */
651 : 2 : break;
652 : :
653 : 7 : case 's': /* dump schema only */
654 : 7 : schema_only = true;
655 : 7 : break;
656 : :
657 : 1 : case 'S': /* Username for superuser in plain text output */
658 : 1 : dopt.outputSuperuser = pg_strdup(optarg);
659 : 1 : break;
660 : :
661 : 8 : case 't': /* include table(s) */
662 : 8 : simple_string_list_append(&table_include_patterns, optarg);
663 : 8 : dopt.include_everything = false;
664 : 8 : break;
665 : :
666 : 4 : case 'T': /* exclude table(s) */
667 : 4 : simple_string_list_append(&table_exclude_patterns, optarg);
668 : 4 : break;
669 : :
670 : 44 : case 'U':
671 : 44 : dopt.cparams.username = pg_strdup(optarg);
672 : 44 : break;
673 : :
674 : 12 : case 'v': /* verbose */
675 : 12 : g_verbose = true;
676 : 12 : pg_logging_increase_verbosity();
677 : 12 : break;
678 : :
679 : 1 : case 'w':
680 : 1 : dopt.cparams.promptPassword = TRI_NO;
681 : 1 : break;
682 : :
683 : 0 : case 'W':
684 : 0 : dopt.cparams.promptPassword = TRI_YES;
685 : 0 : break;
686 : :
687 : 2 : case 'x': /* skip ACL dump */
688 : 2 : dopt.aclsSkip = true;
689 : 2 : break;
690 : :
691 : 13 : case 'Z': /* Compression */
692 : 13 : parse_compress_options(optarg, &compression_algorithm_str,
693 : : &compression_detail);
694 : 13 : user_compression_defined = true;
695 : 13 : break;
696 : :
697 : 155 : case 0:
698 : : /* This covers the long options. */
699 : 155 : break;
700 : :
701 : 2 : case 2: /* lock-wait-timeout */
702 : 2 : dopt.lockWaitTimeout = pg_strdup(optarg);
703 : 2 : break;
704 : :
705 : 6 : case 3: /* SET ROLE */
706 : 6 : use_role = pg_strdup(optarg);
707 : 6 : break;
708 : :
709 : 1 : case 4: /* exclude table(s) data */
710 : 1 : simple_string_list_append(&tabledata_exclude_patterns, optarg);
711 : 1 : break;
712 : :
713 : 6 : case 5: /* section */
714 : 6 : set_dump_section(optarg, &dopt.dumpSections);
715 : 6 : break;
716 : :
717 : 0 : case 6: /* snapshot */
718 : 0 : dumpsnapshot = pg_strdup(optarg);
719 : 0 : break;
720 : :
721 : 170 : case 7: /* no-sync */
722 : 170 : dosync = false;
723 : 170 : break;
724 : :
725 : 1 : case 8:
726 : 1 : have_extra_float_digits = true;
727 [ + - ]: 1 : if (!option_parse_int(optarg, "--extra-float-digits", -15, 3,
728 : : &extra_float_digits))
729 : 1 : exit_nicely(1);
730 : 0 : break;
731 : :
732 : 4 : case 9: /* inserts */
733 : :
734 : : /*
735 : : * dump_inserts also stores --rows-per-insert, careful not to
736 : : * overwrite that.
737 : : */
738 [ + - ]: 4 : if (dopt.dump_inserts == 0)
739 : 4 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
740 : 4 : break;
741 : :
742 : 4 : case 10: /* rows per insert */
743 [ + + ]: 4 : if (!option_parse_int(optarg, "--rows-per-insert", 1, INT_MAX,
744 : : &dopt.dump_inserts))
745 : 1 : exit_nicely(1);
746 : 3 : break;
747 : :
748 : 4 : case 11: /* include foreign data */
749 : 4 : simple_string_list_append(&foreign_servers_include_patterns,
750 : : optarg);
751 : 4 : break;
752 : :
753 : 1 : case 12: /* include table(s) and their children */
754 : 1 : simple_string_list_append(&table_include_patterns_and_children,
755 : : optarg);
756 : 1 : dopt.include_everything = false;
757 : 1 : break;
758 : :
759 : 1 : case 13: /* exclude table(s) and their children */
760 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
761 : : optarg);
762 : 1 : break;
763 : :
764 : 1 : case 14: /* exclude data of table(s) and children */
765 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
766 : : optarg);
767 : 1 : break;
768 : :
769 : 0 : case 15:
770 [ # # ]: 0 : if (!parse_sync_method(optarg, &sync_method))
771 : 0 : exit_nicely(1);
772 : 0 : break;
773 : :
774 : 26 : case 16: /* read object filters from file */
775 : 26 : read_dump_filters(optarg, &dopt);
776 : 22 : break;
777 : :
778 : 1 : case 17: /* exclude extension(s) */
779 : 1 : simple_string_list_append(&extension_exclude_patterns,
780 : : optarg);
781 : 1 : break;
782 : :
783 : 5 : case 18:
784 : 5 : statistics_only = true;
785 : 5 : break;
786 : :
787 : 44 : case 19:
788 : 44 : no_data = true;
789 : 44 : break;
790 : :
791 : 2 : case 20:
792 : 2 : no_schema = true;
793 : 2 : break;
794 : :
795 : 8 : case 21:
796 : 8 : no_statistics = true;
797 : 8 : break;
798 : :
799 : 98 : case 22:
800 : 98 : with_statistics = true;
801 : 98 : break;
802 : :
803 : 34 : case 25:
804 : 34 : dopt.restrict_key = pg_strdup(optarg);
805 : 34 : break;
806 : :
807 : 1 : default:
808 : : /* getopt_long already emitted a complaint */
809 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
810 : 1 : exit_nicely(1);
811 : : }
812 : : }
813 : :
814 : : /*
815 : : * Non-option argument specifies database name as long as it wasn't
816 : : * already specified with -d / --dbname
817 : : */
818 [ + + + - ]: 252 : if (optind < argc && dopt.cparams.dbname == NULL)
819 : 216 : dopt.cparams.dbname = argv[optind++];
820 : :
821 : : /* Complain if any arguments remain */
822 [ + + ]: 252 : if (optind < argc)
823 : : {
824 : 1 : pg_log_error("too many command-line arguments (first is \"%s\")",
825 : : argv[optind]);
826 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
827 : 1 : exit_nicely(1);
828 : : }
829 : :
830 : : /* --column-inserts implies --inserts */
831 [ + + + + ]: 251 : if (dopt.column_inserts && dopt.dump_inserts == 0)
832 : 1 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
833 : :
834 : : /* *-only options are incompatible with each other */
835 : 251 : check_mut_excl_opts(data_only, "-a/--data-only",
836 : : schema_only, "-s/--schema-only",
837 : : statistics_only, "--statistics-only");
838 : :
839 : : /* --no-* and *-only for same thing are incompatible */
840 : 248 : check_mut_excl_opts(data_only, "-a/--data-only",
841 : : no_data, "--no-data");
842 : 248 : check_mut_excl_opts(schema_only, "-s/--schema-only",
843 : : no_schema, "--no-schema");
844 : 248 : check_mut_excl_opts(statistics_only, "--statistics-only",
845 : : no_statistics, "--no-statistics");
846 : :
847 : : /* --statistics and --no-statistics are incompatible */
848 : 247 : check_mut_excl_opts(with_statistics, "--statistics",
849 : : no_statistics, "--no-statistics");
850 : :
851 : : /* --statistics is incompatible with *-only (except --statistics-only) */
852 : 247 : check_mut_excl_opts(with_statistics, "--statistics",
853 : : data_only, "-a/--data-only",
854 : : schema_only, "-s/--schema-only");
855 : :
856 : : /* --include-foreign-data is incompatible with --schema-only */
857 : 246 : check_mut_excl_opts(foreign_servers_include_patterns.head, "--include-foreign-data",
858 : : schema_only, "-s/--schema-only");
859 : :
860 [ + + + + ]: 245 : if (numWorkers > 1 && foreign_servers_include_patterns.head != NULL)
861 : 1 : pg_fatal("option %s is not supported with parallel backup",
862 : : "--include-foreign-data");
863 : :
864 : : /* --clean is incompatible with --data-only */
865 : 244 : check_mut_excl_opts(dopt.outputClean, "-c/--clean",
866 : : data_only, "-a/--data-only");
867 : :
868 [ + + + + ]: 243 : if (dopt.if_exists && !dopt.outputClean)
869 : 1 : pg_fatal("option %s requires option %s",
870 : : "--if-exists", "-c/--clean");
871 : :
872 : : /*
873 : : * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
874 : : * "--schema-only --no-schema", will have already caused an error in one
875 : : * of the checks above.
876 : : */
877 [ + + + + : 242 : dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) ||
- + ]
878 [ + - + + ]: 484 : data_only) && !no_data;
879 [ + + + + : 242 : dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) ||
- + ]
880 [ + - + + ]: 484 : schema_only) && !no_schema;
881 [ - - - - : 242 : dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) ||
+ + ]
882 [ - + + + : 484 : (statistics_only || with_statistics)) && !no_statistics;
+ - ]
883 : :
884 : :
885 : : /*
886 : : * --inserts are already implied above if --column-inserts or
887 : : * --rows-per-insert were specified.
888 : : */
889 [ + + + + ]: 242 : if (dopt.do_nothing && dopt.dump_inserts == 0)
890 : 1 : pg_fatal("option %s requires option %s, %s, or %s",
891 : : "--on-conflict-do-nothing",
892 : : "--inserts", "--rows-per-insert", "--column-inserts");
893 : :
894 : : /* Identify archive format to emit */
895 : 241 : archiveFormat = parseArchiveFormat(format, &archiveMode);
896 : :
897 : : /* archiveFormat specific setup */
898 [ + + ]: 240 : if (archiveFormat == archNull)
899 : 166 : plainText = 1;
900 [ + + + + ]: 240 : if (archiveFormat == archNull ||
901 : : archiveFormat == archTar)
902 : : {
903 : : /*
904 : : * If you don't provide a restrict key, one will be appointed for you.
905 : : * tar format uses this for its undocumented restore.sql file.
906 : : */
907 [ + + ]: 171 : if (!dopt.restrict_key)
908 : 137 : dopt.restrict_key = generate_restrict_key();
909 [ - + ]: 171 : if (!dopt.restrict_key)
910 : 0 : pg_fatal("could not generate restrict key");
911 [ - + ]: 171 : if (!valid_restrict_key(dopt.restrict_key))
912 : 0 : pg_fatal("invalid restrict key");
913 : : }
914 [ - + ]: 69 : else if (dopt.restrict_key)
915 : 0 : pg_fatal("option %s can only be used with %s or %s",
916 : : "--restrict-key", "--format=plain", "--format=tar");
917 : :
918 : : /*
919 : : * Custom and directory formats are compressed by default with gzip when
920 : : * available, not the others. If gzip is not available, no compression is
921 : : * done by default.
922 : : */
923 [ + + + + ]: 240 : if ((archiveFormat == archCustom || archiveFormat == archDirectory) &&
924 [ + + ]: 69 : !user_compression_defined)
925 : : {
926 : : #ifdef HAVE_LIBZ
927 : 63 : compression_algorithm_str = "gzip";
928 : : #else
929 : : compression_algorithm_str = "none";
930 : : #endif
931 : : }
932 : :
933 : : /*
934 : : * Compression options
935 : : */
936 [ + + ]: 240 : if (!parse_compress_algorithm(compression_algorithm_str,
937 : : &compression_algorithm))
938 : 1 : pg_fatal("unrecognized compression algorithm: \"%s\"",
939 : : compression_algorithm_str);
940 : :
941 : 239 : parse_compress_specification(compression_algorithm, compression_detail,
942 : : &compression_spec);
943 : 239 : error_detail = validate_compress_specification(&compression_spec);
944 [ + + ]: 239 : if (error_detail != NULL)
945 : 3 : pg_fatal("invalid compression specification: %s",
946 : : error_detail);
947 : :
948 : 236 : error_detail = supports_compression(compression_spec);
949 [ - + ]: 236 : if (error_detail != NULL)
950 : 0 : pg_fatal("%s", error_detail);
951 : :
952 : : /*
953 : : * Disable support for zstd workers for now - these are based on
954 : : * threading, and it's unclear how it interacts with parallel dumps on
955 : : * platforms where that relies on threads too (e.g. Windows).
956 : : */
957 [ - + ]: 236 : if (compression_spec.options & PG_COMPRESSION_OPTION_WORKERS)
958 : 0 : pg_log_warning("compression option \"%s\" is not currently supported by pg_dump",
959 : : "workers");
960 : :
961 : : /*
962 : : * If emitting an archive format, we always want to emit a DATABASE item,
963 : : * in case --create is specified at pg_restore time.
964 : : */
965 [ + + ]: 236 : if (!plainText)
966 : 74 : dopt.outputCreateDB = 1;
967 : :
968 : : /* Parallel backup only in the directory archive format so far */
969 [ + + + + ]: 236 : if (archiveFormat != archDirectory && numWorkers > 1)
970 : 1 : pg_fatal("parallel backup only supported by the directory format");
971 : :
972 : : /* Open the output file */
973 : 235 : fout = CreateArchive(filename, archiveFormat, compression_spec,
974 : : dosync, archiveMode, setupDumpWorker, sync_method);
975 : :
976 : : /* Make dump options accessible right away */
977 : 234 : SetArchiveOptions(fout, &dopt, NULL);
978 : :
979 : : /* Register the cleanup hook */
980 : 234 : on_exit_close_archive(fout);
981 : :
982 : : /* Let the archiver know how noisy to be */
983 : 234 : fout->verbose = g_verbose;
984 : :
985 : :
986 : : /*
987 : : * We allow the server to be back to 10, and up to any minor release of
988 : : * our own major version. (See also version check in pg_dumpall.c.)
989 : : */
990 : 234 : fout->minRemoteVersion = 100000;
991 : 234 : fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
992 : :
993 : 234 : fout->numWorkers = numWorkers;
994 : :
995 : : /*
996 : : * Open the database using the Archiver, so it knows about it. Errors mean
997 : : * death.
998 : : */
999 : 234 : ConnectDatabaseAhx(fout, &dopt.cparams, false);
1000 : 232 : setup_connection(fout, dumpencoding, dumpsnapshot, use_role);
1001 : :
1002 : : /*
1003 : : * On hot standbys, never try to dump unlogged table data, since it will
1004 : : * just throw an error.
1005 : : */
1006 [ + + ]: 232 : if (fout->isStandby)
1007 : 4 : dopt.no_unlogged_table_data = true;
1008 : :
1009 : : /*
1010 : : * Find the last built-in OID, if needed (prior to 8.1)
1011 : : *
1012 : : * With 8.1 and above, we can just use FirstNormalObjectId - 1.
1013 : : */
1014 : 232 : g_last_builtin_oid = FirstNormalObjectId - 1;
1015 : :
1016 : 232 : pg_log_info("last built-in OID is %u", g_last_builtin_oid);
1017 : :
1018 : : /* Expand schema selection patterns into OID lists */
1019 [ + + ]: 232 : if (schema_include_patterns.head != NULL)
1020 : : {
1021 : 19 : expand_schema_name_patterns(fout, &schema_include_patterns,
1022 : : &schema_include_oids,
1023 : : strict_names);
1024 [ + + ]: 13 : if (schema_include_oids.head == NULL)
1025 : 1 : pg_fatal("no matching schemas were found");
1026 : : }
1027 : 225 : expand_schema_name_patterns(fout, &schema_exclude_patterns,
1028 : : &schema_exclude_oids,
1029 : : false);
1030 : : /* non-matching exclusion patterns aren't an error */
1031 : :
1032 : : /* Expand table selection patterns into OID lists */
1033 : 225 : expand_table_name_patterns(fout, &table_include_patterns,
1034 : : &table_include_oids,
1035 : : strict_names, false);
1036 : 220 : expand_table_name_patterns(fout, &table_include_patterns_and_children,
1037 : : &table_include_oids,
1038 : : strict_names, true);
1039 [ + + ]: 220 : if ((table_include_patterns.head != NULL ||
1040 [ + + ]: 209 : table_include_patterns_and_children.head != NULL) &&
1041 [ + + ]: 13 : table_include_oids.head == NULL)
1042 : 2 : pg_fatal("no matching tables were found");
1043 : :
1044 : 218 : expand_table_name_patterns(fout, &table_exclude_patterns,
1045 : : &table_exclude_oids,
1046 : : false, false);
1047 : 218 : expand_table_name_patterns(fout, &table_exclude_patterns_and_children,
1048 : : &table_exclude_oids,
1049 : : false, true);
1050 : :
1051 : 218 : expand_table_name_patterns(fout, &tabledata_exclude_patterns,
1052 : : &tabledata_exclude_oids,
1053 : : false, false);
1054 : 218 : expand_table_name_patterns(fout, &tabledata_exclude_patterns_and_children,
1055 : : &tabledata_exclude_oids,
1056 : : false, true);
1057 : :
1058 : 218 : expand_foreign_server_name_patterns(fout, &foreign_servers_include_patterns,
1059 : : &foreign_servers_include_oids);
1060 : :
1061 : : /* non-matching exclusion patterns aren't an error */
1062 : :
1063 : : /* Expand extension selection patterns into OID lists */
1064 [ + + ]: 217 : if (extension_include_patterns.head != NULL)
1065 : : {
1066 : 5 : expand_extension_name_patterns(fout, &extension_include_patterns,
1067 : : &extension_include_oids,
1068 : : strict_names);
1069 [ + + ]: 5 : if (extension_include_oids.head == NULL)
1070 : 1 : pg_fatal("no matching extensions were found");
1071 : : }
1072 : 216 : expand_extension_name_patterns(fout, &extension_exclude_patterns,
1073 : : &extension_exclude_oids,
1074 : : false);
1075 : : /* non-matching exclusion patterns aren't an error */
1076 : :
1077 : : /*
1078 : : * Dumping LOs is the default for dumps where an inclusion switch is not
1079 : : * used (an "include everything" dump). -B can be used to exclude LOs
1080 : : * from those dumps. -b can be used to include LOs even when an inclusion
1081 : : * switch is used.
1082 : : *
1083 : : * -s means "schema only" and LOs are data, not schema, so we never
1084 : : * include LOs when -s is used.
1085 : : */
1086 [ + + + + : 216 : if (dopt.include_everything && dopt.dumpData && !dopt.dontOutputLOs)
+ + ]
1087 : 142 : dopt.outputLOs = true;
1088 : :
1089 : : /*
1090 : : * Collect role names so we can map object owner OIDs to names.
1091 : : */
1092 : 216 : collectRoleNames(fout);
1093 : :
1094 : : /*
1095 : : * Now scan the database and create DumpableObject structs for all the
1096 : : * objects we intend to dump.
1097 : : */
1098 : 216 : tblinfo = getSchemaData(fout, &numTables);
1099 : :
1100 [ + + ]: 215 : if (dopt.dumpData)
1101 : : {
1102 : 166 : getTableData(&dopt, tblinfo, numTables, 0);
1103 : 166 : buildMatViewRefreshDependencies(fout);
1104 [ + + ]: 166 : if (!dopt.dumpSchema)
1105 : 7 : getTableDataFKConstraints();
1106 : : }
1107 : :
1108 [ + + + + ]: 215 : if (!dopt.dumpData && dopt.sequence_data)
1109 : 40 : getTableData(&dopt, tblinfo, numTables, RELKIND_SEQUENCE);
1110 : :
1111 : : /*
1112 : : * For binary upgrade mode, dump the pg_shdepend rows for large objects
1113 : : * and maybe even pg_largeobject_metadata (see comment below for details).
1114 : : * This is faster to restore than the equivalent set of large object
1115 : : * commands.
1116 : : */
1117 [ + + ]: 215 : if (dopt.binary_upgrade)
1118 : : {
1119 : : TableInfo *shdepend;
1120 : :
1121 : 44 : shdepend = findTableByOid(SharedDependRelationId);
1122 : 44 : makeTableDataInfo(&dopt, shdepend);
1123 : :
1124 : : /*
1125 : : * Only dump large object shdepend rows for this database.
1126 : : */
1127 : 44 : shdepend->dataObj->filtercond = "WHERE classid = 'pg_largeobject'::regclass "
1128 : : "AND dbid = (SELECT oid FROM pg_database "
1129 : : " WHERE datname = current_database())";
1130 : :
1131 : : /*
1132 : : * For binary upgrades from v16 and newer versions, we can copy
1133 : : * pg_largeobject_metadata's files from the old cluster, so we don't
1134 : : * need to dump its contents. pg_upgrade can't copy/link the files
1135 : : * from older versions because aclitem (needed by
1136 : : * pg_largeobject_metadata.lomacl) changed its storage format in v16.
1137 : : */
1138 [ - + ]: 44 : if (fout->remoteVersion < 160000)
1139 : : {
1140 : : TableInfo *lo_metadata;
1141 : :
1142 : 0 : lo_metadata = findTableByOid(LargeObjectMetadataRelationId);
1143 : 0 : makeTableDataInfo(&dopt, lo_metadata);
1144 : : }
1145 : : }
1146 : :
1147 : : /*
1148 : : * In binary-upgrade mode, we do not have to worry about the actual LO
1149 : : * data or the associated metadata that resides in the pg_largeobject and
1150 : : * pg_largeobject_metadata tables, respectively.
1151 : : *
1152 : : * However, we do need to collect LO information as there may be comments
1153 : : * or other information on LOs that we do need to dump out.
1154 : : */
1155 [ + + + + ]: 215 : if (dopt.outputLOs || dopt.binary_upgrade)
1156 : 186 : getLOs(fout);
1157 : :
1158 : : /*
1159 : : * Collect dependency data to assist in ordering the objects.
1160 : : */
1161 : 215 : getDependencies(fout);
1162 : :
1163 : : /*
1164 : : * Collect ACLs, comments, and security labels, if wanted.
1165 : : */
1166 [ + + ]: 215 : if (!dopt.aclsSkip)
1167 : 213 : getAdditionalACLs(fout);
1168 [ + - ]: 215 : if (!dopt.no_comments)
1169 : 215 : collectComments(fout);
1170 [ + - ]: 215 : if (!dopt.no_security_labels)
1171 : 215 : collectSecLabels(fout);
1172 : :
1173 : : /* For binary upgrade mode, collect required pg_class information. */
1174 [ + + ]: 215 : if (dopt.binary_upgrade)
1175 : 44 : collectBinaryUpgradeClassOids(fout);
1176 : :
1177 : : /* Collect sequence information. */
1178 : 215 : collectSequences(fout);
1179 : :
1180 : : /* Lastly, create dummy objects to represent the section boundaries */
1181 : 215 : boundaryObjs = createBoundaryObjects();
1182 : :
1183 : : /* Get pointers to all the known DumpableObjects */
1184 : 215 : getDumpableObjects(&dobjs, &numObjs);
1185 : :
1186 : : /*
1187 : : * Add dummy dependencies to enforce the dump section ordering.
1188 : : */
1189 : 215 : addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
1190 : :
1191 : : /*
1192 : : * Sort the objects into a safe dump order (no forward references).
1193 : : *
1194 : : * We rely on dependency information to help us determine a safe order, so
1195 : : * the initial sort is mostly for cosmetic purposes: we sort by name to
1196 : : * ensure that logically identical schemas will dump identically.
1197 : : */
1198 : 215 : sortDumpableObjectsByTypeName(dobjs, numObjs);
1199 : :
1200 : 215 : sortDumpableObjects(dobjs, numObjs,
1201 : 215 : boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
1202 : :
1203 : : /*
1204 : : * Create archive TOC entries for all the objects to be dumped, in a safe
1205 : : * order.
1206 : : */
1207 : :
1208 : : /*
1209 : : * First the special entries for ENCODING, STDSTRINGS, and SEARCHPATH.
1210 : : */
1211 : 215 : dumpEncoding(fout);
1212 : 215 : dumpStdStrings(fout);
1213 : 215 : dumpSearchPath(fout);
1214 : :
1215 : : /* The database items are always next, unless we don't want them at all */
1216 [ + + ]: 215 : if (dopt.outputCreateDB)
1217 : 108 : dumpDatabase(fout);
1218 : :
1219 : : /* Now the rearrangeable objects. */
1220 [ + + ]: 811453 : for (i = 0; i < numObjs; i++)
1221 : 811238 : dumpDumpableObject(fout, dobjs[i]);
1222 : :
1223 : : /*
1224 : : * Set up options info to ensure we dump what we want.
1225 : : */
1226 : 215 : ropt = NewRestoreOptions();
1227 : 215 : ropt->filename = filename;
1228 : :
1229 : : /* if you change this list, see dumpOptionsFromRestoreOptions */
1230 [ + + ]: 215 : ropt->cparams.dbname = dopt.cparams.dbname ? pg_strdup(dopt.cparams.dbname) : NULL;
1231 [ + + ]: 215 : ropt->cparams.pgport = dopt.cparams.pgport ? pg_strdup(dopt.cparams.pgport) : NULL;
1232 [ + + ]: 215 : ropt->cparams.pghost = dopt.cparams.pghost ? pg_strdup(dopt.cparams.pghost) : NULL;
1233 [ + + ]: 215 : ropt->cparams.username = dopt.cparams.username ? pg_strdup(dopt.cparams.username) : NULL;
1234 : 215 : ropt->cparams.promptPassword = dopt.cparams.promptPassword;
1235 : 215 : ropt->dropSchema = dopt.outputClean;
1236 : 215 : ropt->dumpData = dopt.dumpData;
1237 : 215 : ropt->dumpSchema = dopt.dumpSchema;
1238 : 215 : ropt->dumpStatistics = dopt.dumpStatistics;
1239 : 215 : ropt->if_exists = dopt.if_exists;
1240 : 215 : ropt->column_inserts = dopt.column_inserts;
1241 : 215 : ropt->dumpSections = dopt.dumpSections;
1242 : 215 : ropt->aclsSkip = dopt.aclsSkip;
1243 : 215 : ropt->superuser = dopt.outputSuperuser;
1244 : 215 : ropt->createDB = dopt.outputCreateDB;
1245 : 215 : ropt->noOwner = dopt.outputNoOwner;
1246 : 215 : ropt->noTableAm = dopt.outputNoTableAm;
1247 : 215 : ropt->noTablespace = dopt.outputNoTablespaces;
1248 : 215 : ropt->disable_triggers = dopt.disable_triggers;
1249 : 215 : ropt->use_setsessauth = dopt.use_setsessauth;
1250 : 215 : ropt->disable_dollar_quoting = dopt.disable_dollar_quoting;
1251 : 215 : ropt->dump_inserts = dopt.dump_inserts;
1252 : 215 : ropt->no_comments = dopt.no_comments;
1253 : 215 : ropt->no_policies = dopt.no_policies;
1254 : 215 : ropt->no_publications = dopt.no_publications;
1255 : 215 : ropt->no_security_labels = dopt.no_security_labels;
1256 : 215 : ropt->no_subscriptions = dopt.no_subscriptions;
1257 : 215 : ropt->lockWaitTimeout = dopt.lockWaitTimeout;
1258 : 215 : ropt->include_everything = dopt.include_everything;
1259 : 215 : ropt->enable_row_security = dopt.enable_row_security;
1260 : 215 : ropt->sequence_data = dopt.sequence_data;
1261 : 215 : ropt->binary_upgrade = dopt.binary_upgrade;
1262 [ + + ]: 215 : ropt->restrict_key = dopt.restrict_key ? pg_strdup(dopt.restrict_key) : NULL;
1263 : :
1264 : 215 : ropt->compression_spec = compression_spec;
1265 : :
1266 : 215 : ropt->suppressDumpWarnings = true; /* We've already shown them */
1267 : :
1268 : 215 : SetArchiveOptions(fout, &dopt, ropt);
1269 : :
1270 : : /* Mark which entries should be output */
1271 : 215 : ProcessArchiveRestoreOptions(fout);
1272 : :
1273 : : /*
1274 : : * The archive's TOC entries are now marked as to which ones will actually
1275 : : * be output, so we can set up their dependency lists properly. This isn't
1276 : : * necessary for plain-text output, though.
1277 : : */
1278 [ + + ]: 215 : if (!plainText)
1279 : 73 : BuildArchiveDependencies(fout);
1280 : :
1281 : : /*
1282 : : * And finally we can do the actual output.
1283 : : *
1284 : : * Note: for non-plain-text output formats, the output file is written
1285 : : * inside CloseArchive(). This is, um, bizarre; but not worth changing
1286 : : * right now.
1287 : : */
1288 [ + + ]: 215 : if (plainText)
1289 : 142 : RestoreArchive(fout);
1290 : :
1291 : 214 : CloseArchive(fout);
1292 : :
1293 : 214 : exit_nicely(0);
1294 : : }
1295 : :
1296 : :
1297 : : static void
1298 : 1 : help(const char *progname)
1299 : : {
1300 : 1 : printf(_("%s exports a PostgreSQL database as an SQL script or to other formats.\n\n"), progname);
1301 : 1 : printf(_("Usage:\n"));
1302 : 1 : printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
1303 : :
1304 : 1 : printf(_("\nGeneral options:\n"));
1305 : 1 : printf(_(" -f, --file=FILENAME output file or directory name\n"));
1306 : 1 : printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
1307 : : " plain text (default))\n"));
1308 : 1 : printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n"));
1309 : 1 : printf(_(" -v, --verbose verbose mode\n"));
1310 : 1 : printf(_(" -V, --version output version information, then exit\n"));
1311 : 1 : printf(_(" -Z, --compress=METHOD[:DETAIL]\n"
1312 : : " compress as specified\n"));
1313 : 1 : printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
1314 : 1 : printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
1315 : 1 : printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
1316 : 1 : printf(_(" -?, --help show this help, then exit\n"));
1317 : :
1318 : 1 : printf(_("\nOptions controlling the output content:\n"));
1319 : 1 : printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
1320 : 1 : printf(_(" -b, --large-objects include large objects in dump\n"));
1321 : 1 : printf(_(" --blobs (same as --large-objects, deprecated)\n"));
1322 : 1 : printf(_(" -B, --no-large-objects exclude large objects in dump\n"));
1323 : 1 : printf(_(" --no-blobs (same as --no-large-objects, deprecated)\n"));
1324 : 1 : printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
1325 : 1 : printf(_(" -C, --create include commands to create database in dump\n"));
1326 : 1 : printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n"));
1327 : 1 : printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
1328 : 1 : printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n"));
1329 : 1 : printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n"));
1330 : 1 : printf(_(" -O, --no-owner skip restoration of object ownership in\n"
1331 : : " plain-text format\n"));
1332 : 1 : printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
1333 : 1 : printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n"));
1334 : 1 : printf(_(" -t, --table=PATTERN dump only the specified table(s)\n"));
1335 : 1 : printf(_(" -T, --exclude-table=PATTERN do NOT dump the specified table(s)\n"));
1336 : 1 : printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
1337 : 1 : printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
1338 : 1 : printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
1339 : 1 : printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
1340 : 1 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
1341 : 1 : printf(_(" --enable-row-security enable row security (dump only content user has\n"
1342 : : " access to)\n"));
1343 : 1 : printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n"));
1344 : 1 : printf(_(" --exclude-table-and-children=PATTERN\n"
1345 : : " do NOT dump the specified table(s), including\n"
1346 : : " child and partition tables\n"));
1347 : 1 : printf(_(" --exclude-table-data=PATTERN do NOT dump data for the specified table(s)\n"));
1348 : 1 : printf(_(" --exclude-table-data-and-children=PATTERN\n"
1349 : : " do NOT dump data for the specified table(s),\n"
1350 : : " including child and partition tables\n"));
1351 : 1 : printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
1352 : 1 : printf(_(" --filter=FILENAME include or exclude objects and data from dump\n"
1353 : : " based on expressions in FILENAME\n"));
1354 : 1 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
1355 : 1 : printf(_(" --include-foreign-data=PATTERN\n"
1356 : : " include data of foreign tables on foreign\n"
1357 : : " servers matching PATTERN\n"));
1358 : 1 : printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
1359 : 1 : printf(_(" --load-via-partition-root load partitions via the root table\n"));
1360 : 1 : printf(_(" --no-comments do not dump comment commands\n"));
1361 : 1 : printf(_(" --no-data do not dump data\n"));
1362 : 1 : printf(_(" --no-policies do not dump row security policies\n"));
1363 : 1 : printf(_(" --no-publications do not dump publications\n"));
1364 : 1 : printf(_(" --no-schema do not dump schema\n"));
1365 : 1 : printf(_(" --no-security-labels do not dump security label assignments\n"));
1366 : 1 : printf(_(" --no-statistics do not dump statistics\n"));
1367 : 1 : printf(_(" --no-subscriptions do not dump subscriptions\n"));
1368 : 1 : printf(_(" --no-table-access-method do not dump table access methods\n"));
1369 : 1 : printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
1370 : 1 : printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
1371 : 1 : printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
1372 : 1 : printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
1373 : 1 : printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
1374 : 1 : printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
1375 : 1 : printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
1376 : 1 : printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
1377 : 1 : printf(_(" --sequence-data include sequence data in dump\n"));
1378 : 1 : printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
1379 : 1 : printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n"));
1380 : 1 : printf(_(" --statistics dump the statistics\n"));
1381 : 1 : printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
1382 : 1 : printf(_(" --strict-names require table and/or schema include patterns to\n"
1383 : : " match at least one entity each\n"));
1384 : 1 : printf(_(" --table-and-children=PATTERN dump only the specified table(s), including\n"
1385 : : " child and partition tables\n"));
1386 : 1 : printf(_(" --use-set-session-authorization\n"
1387 : : " use SET SESSION AUTHORIZATION commands instead of\n"
1388 : : " ALTER OWNER commands to set ownership\n"));
1389 : :
1390 : 1 : printf(_("\nConnection options:\n"));
1391 : 1 : printf(_(" -d, --dbname=DBNAME database to dump\n"));
1392 : 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
1393 : 1 : printf(_(" -p, --port=PORT database server port number\n"));
1394 : 1 : printf(_(" -U, --username=NAME connect as specified database user\n"));
1395 : 1 : printf(_(" -w, --no-password never prompt for password\n"));
1396 : 1 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
1397 : 1 : printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
1398 : :
1399 : 1 : printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
1400 : : "variable value is used.\n\n"));
1401 : 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1402 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1403 : 1 : }
1404 : :
1405 : : static void
1406 : 248 : setup_connection(Archive *AH, const char *dumpencoding,
1407 : : const char *dumpsnapshot, char *use_role)
1408 : : {
1409 : 248 : DumpOptions *dopt = AH->dopt;
1410 : 248 : PGconn *conn = GetConnection(AH);
1411 : :
1412 : 248 : PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL));
1413 : :
1414 : : /*
1415 : : * Set the client encoding if requested.
1416 : : */
1417 [ + + ]: 248 : if (dumpencoding)
1418 : : {
1419 [ - + ]: 19 : if (PQsetClientEncoding(conn, dumpencoding) < 0)
1420 : 0 : pg_fatal("invalid client encoding \"%s\" specified",
1421 : : dumpencoding);
1422 : : }
1423 : :
1424 : : /*
1425 : : * Force standard_conforming_strings on, just in case we are dumping from
1426 : : * an old server that has it disabled. Without this, literals in views,
1427 : : * expressions, etc, would be incorrect for modern servers.
1428 : : */
1429 : 248 : ExecuteSqlStatement(AH, "SET standard_conforming_strings = on");
1430 : :
1431 : : /*
1432 : : * And reflect that to AH->std_strings. You might think that we should
1433 : : * just delete that variable and the code that checks it, but that would
1434 : : * be problematic for pg_restore, which at least for now should still cope
1435 : : * with archives containing the other setting (cf. processStdStringsEntry
1436 : : * in pg_backup_archiver.c).
1437 : : */
1438 : 248 : AH->std_strings = true;
1439 : :
1440 : : /*
1441 : : * Get the active encoding, so we know how to escape strings.
1442 : : */
1443 : 248 : AH->encoding = PQclientEncoding(conn);
1444 : 248 : setFmtEncoding(AH->encoding);
1445 : :
1446 : : /*
1447 : : * Set the role if requested. In a parallel dump worker, we'll be passed
1448 : : * use_role == NULL, but AH->use_role is already set (if user specified it
1449 : : * originally) and we should use that.
1450 : : */
1451 [ + + + + ]: 248 : if (!use_role && AH->use_role)
1452 : 2 : use_role = AH->use_role;
1453 : :
1454 : : /* Set the role if requested */
1455 [ + + ]: 248 : if (use_role)
1456 : : {
1457 : 8 : PQExpBuffer query = createPQExpBuffer();
1458 : :
1459 : 8 : appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
1460 : 8 : ExecuteSqlStatement(AH, query->data);
1461 : 8 : destroyPQExpBuffer(query);
1462 : :
1463 : : /* save it for possible later use by parallel workers */
1464 [ + + ]: 8 : if (!AH->use_role)
1465 : 6 : AH->use_role = pg_strdup(use_role);
1466 : : }
1467 : :
1468 : : /* Set the datestyle to ISO to ensure the dump's portability */
1469 : 248 : ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
1470 : :
1471 : : /* Likewise, avoid using sql_standard intervalstyle */
1472 : 248 : ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
1473 : :
1474 : : /*
1475 : : * Use an explicitly specified extra_float_digits if it has been provided.
1476 : : * Otherwise, set extra_float_digits so that we can dump float data
1477 : : * exactly (given correctly implemented float I/O code, anyway).
1478 : : */
1479 [ - + ]: 248 : if (have_extra_float_digits)
1480 : : {
1481 : 0 : PQExpBuffer q = createPQExpBuffer();
1482 : :
1483 : 0 : appendPQExpBuffer(q, "SET extra_float_digits TO %d",
1484 : : extra_float_digits);
1485 : 0 : ExecuteSqlStatement(AH, q->data);
1486 : 0 : destroyPQExpBuffer(q);
1487 : : }
1488 : : else
1489 : 248 : ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
1490 : :
1491 : : /*
1492 : : * Disable synchronized scanning, to prevent unpredictable changes in row
1493 : : * ordering across a dump and reload.
1494 : : */
1495 : 248 : ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
1496 : :
1497 : : /*
1498 : : * Disable timeouts if supported.
1499 : : */
1500 : 248 : ExecuteSqlStatement(AH, "SET statement_timeout = 0");
1501 : 248 : ExecuteSqlStatement(AH, "SET lock_timeout = 0");
1502 : 248 : ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
1503 [ + - ]: 248 : if (AH->remoteVersion >= 170000)
1504 : 248 : ExecuteSqlStatement(AH, "SET transaction_timeout = 0");
1505 : :
1506 : : /*
1507 : : * Quote all identifiers, if requested.
1508 : : */
1509 [ + + ]: 248 : if (quote_all_identifiers)
1510 : 42 : ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1511 : :
1512 : : /*
1513 : : * Adjust row-security mode, if supported.
1514 : : */
1515 [ - + ]: 248 : if (dopt->enable_row_security)
1516 : 0 : ExecuteSqlStatement(AH, "SET row_security = on");
1517 : : else
1518 : 248 : ExecuteSqlStatement(AH, "SET row_security = off");
1519 : :
1520 : : /*
1521 : : * For security reasons, we restrict the expansion of non-system views and
1522 : : * access to foreign tables during the pg_dump process. This restriction
1523 : : * is adjusted when dumping foreign table data.
1524 : : */
1525 : 248 : set_restrict_relation_kind(AH, "view, foreign-table");
1526 : :
1527 : : /*
1528 : : * Initialize prepared-query state to "nothing prepared". We do this here
1529 : : * so that a parallel dump worker will have its own state.
1530 : : */
1531 : 248 : AH->is_prepared = pg_malloc0_array(bool, NUM_PREP_QUERIES);
1532 : :
1533 : : /*
1534 : : * Start transaction-snapshot mode transaction to dump consistent data.
1535 : : */
1536 : 248 : ExecuteSqlStatement(AH, "BEGIN");
1537 : :
1538 : : /*
1539 : : * To support the combination of serializable_deferrable with the jobs
1540 : : * option we use REPEATABLE READ for the worker connections that are
1541 : : * passed a snapshot. As long as the snapshot is acquired in a
1542 : : * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a
1543 : : * REPEATABLE READ transaction provides the appropriate integrity
1544 : : * guarantees. This is a kluge, but safe for back-patching.
1545 : : */
1546 [ - + - - ]: 248 : if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
1547 : 0 : ExecuteSqlStatement(AH,
1548 : : "SET TRANSACTION ISOLATION LEVEL "
1549 : : "SERIALIZABLE, READ ONLY, DEFERRABLE");
1550 : : else
1551 : 248 : ExecuteSqlStatement(AH,
1552 : : "SET TRANSACTION ISOLATION LEVEL "
1553 : : "REPEATABLE READ, READ ONLY");
1554 : :
1555 : : /*
1556 : : * If user specified a snapshot to use, select that. In a parallel dump
1557 : : * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id
1558 : : * is already set (if the server can handle it) and we should use that.
1559 : : */
1560 [ - + ]: 248 : if (dumpsnapshot)
1561 : 0 : AH->sync_snapshot_id = pg_strdup(dumpsnapshot);
1562 : :
1563 [ + + ]: 248 : if (AH->sync_snapshot_id)
1564 : : {
1565 : 16 : PQExpBuffer query = createPQExpBuffer();
1566 : :
1567 : 16 : appendPQExpBufferStr(query, "SET TRANSACTION SNAPSHOT ");
1568 : 16 : appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1569 : 16 : ExecuteSqlStatement(AH, query->data);
1570 : 16 : destroyPQExpBuffer(query);
1571 : : }
1572 [ + + ]: 232 : else if (AH->numWorkers > 1)
1573 : 8 : AH->sync_snapshot_id = get_synchronized_snapshot(AH);
1574 : 248 : }
1575 : :
1576 : : /* Set up connection for a parallel worker process */
1577 : : static void
1578 : 16 : setupDumpWorker(Archive *AH)
1579 : : {
1580 : : /*
1581 : : * We want to re-select all the same values the leader connection is
1582 : : * using. We'll have inherited directly-usable values in
1583 : : * AH->sync_snapshot_id and AH->use_role, but we need to translate the
1584 : : * inherited encoding value back to a string to pass to setup_connection.
1585 : : */
1586 : 16 : setup_connection(AH,
1587 : : pg_encoding_to_char(AH->encoding),
1588 : : NULL,
1589 : : NULL);
1590 : 16 : }
1591 : :
1592 : : static char *
1593 : 8 : get_synchronized_snapshot(Archive *fout)
1594 : : {
1595 : 8 : char *query = "SELECT pg_catalog.pg_export_snapshot()";
1596 : : char *result;
1597 : : PGresult *res;
1598 : :
1599 : 8 : res = ExecuteSqlQueryForSingleRow(fout, query);
1600 : 8 : result = pg_strdup(PQgetvalue(res, 0, 0));
1601 : 8 : PQclear(res);
1602 : :
1603 : 8 : return result;
1604 : : }
1605 : :
1606 : : static ArchiveFormat
1607 : 241 : parseArchiveFormat(const char *format, ArchiveMode *mode)
1608 : : {
1609 : : ArchiveFormat archiveFormat;
1610 : :
1611 : 241 : *mode = archModeWrite;
1612 : :
1613 [ + + - + ]: 241 : if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1614 : : {
1615 : : /* This is used by pg_dumpall, and is not documented */
1616 : 54 : archiveFormat = archNull;
1617 : 54 : *mode = archModeAppend;
1618 : : }
1619 [ - + ]: 187 : else if (pg_strcasecmp(format, "c") == 0)
1620 : 0 : archiveFormat = archCustom;
1621 [ + + ]: 187 : else if (pg_strcasecmp(format, "custom") == 0)
1622 : 57 : archiveFormat = archCustom;
1623 [ - + ]: 130 : else if (pg_strcasecmp(format, "d") == 0)
1624 : 0 : archiveFormat = archDirectory;
1625 [ + + ]: 130 : else if (pg_strcasecmp(format, "directory") == 0)
1626 : 12 : archiveFormat = archDirectory;
1627 [ + + ]: 118 : else if (pg_strcasecmp(format, "p") == 0)
1628 : 109 : archiveFormat = archNull;
1629 [ + + ]: 9 : else if (pg_strcasecmp(format, "plain") == 0)
1630 : 3 : archiveFormat = archNull;
1631 [ - + ]: 6 : else if (pg_strcasecmp(format, "t") == 0)
1632 : 0 : archiveFormat = archTar;
1633 [ + + ]: 6 : else if (pg_strcasecmp(format, "tar") == 0)
1634 : 5 : archiveFormat = archTar;
1635 : : else
1636 : 1 : pg_fatal("invalid output format \"%s\" specified", format);
1637 : 240 : return archiveFormat;
1638 : : }
1639 : :
1640 : : /*
1641 : : * Find the OIDs of all schemas matching the given list of patterns,
1642 : : * and append them to the given OID list.
1643 : : */
1644 : : static void
1645 : 244 : expand_schema_name_patterns(Archive *fout,
1646 : : SimpleStringList *patterns,
1647 : : SimpleOidList *oids,
1648 : : bool strict_names)
1649 : : {
1650 : : PQExpBuffer query;
1651 : : PGresult *res;
1652 : : SimpleStringListCell *cell;
1653 : : int i;
1654 : :
1655 [ + + ]: 244 : if (patterns->head == NULL)
1656 : 222 : return; /* nothing to do */
1657 : :
1658 : 22 : query = createPQExpBuffer();
1659 : :
1660 : : /*
1661 : : * The loop below runs multiple SELECTs might sometimes result in
1662 : : * duplicate entries in the OID list, but we don't care.
1663 : : */
1664 : :
1665 [ + + ]: 38 : for (cell = patterns->head; cell; cell = cell->next)
1666 : : {
1667 : : PQExpBufferData dbbuf;
1668 : : int dotcnt;
1669 : :
1670 : 22 : appendPQExpBufferStr(query,
1671 : : "SELECT oid FROM pg_catalog.pg_namespace n\n");
1672 : 22 : initPQExpBuffer(&dbbuf);
1673 : 22 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1674 : : false, NULL, "n.nspname", NULL, NULL, &dbbuf,
1675 : : &dotcnt);
1676 [ + + ]: 22 : if (dotcnt > 1)
1677 : 2 : pg_fatal("improper qualified name (too many dotted names): %s",
1678 : : cell->val);
1679 [ + + ]: 20 : else if (dotcnt == 1)
1680 : 3 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1681 : 17 : termPQExpBuffer(&dbbuf);
1682 : :
1683 : 17 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1684 [ + + + - ]: 17 : if (strict_names && PQntuples(res) == 0)
1685 : 1 : pg_fatal("no matching schemas were found for pattern \"%s\"", cell->val);
1686 : :
1687 [ + + ]: 31 : for (i = 0; i < PQntuples(res); i++)
1688 : : {
1689 : 15 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1690 : : }
1691 : :
1692 : 16 : PQclear(res);
1693 : 16 : resetPQExpBuffer(query);
1694 : : }
1695 : :
1696 : 16 : destroyPQExpBuffer(query);
1697 : : }
1698 : :
1699 : : /*
1700 : : * Find the OIDs of all extensions matching the given list of patterns,
1701 : : * and append them to the given OID list.
1702 : : */
1703 : : static void
1704 : 221 : expand_extension_name_patterns(Archive *fout,
1705 : : SimpleStringList *patterns,
1706 : : SimpleOidList *oids,
1707 : : bool strict_names)
1708 : : {
1709 : : PQExpBuffer query;
1710 : : PGresult *res;
1711 : : SimpleStringListCell *cell;
1712 : : int i;
1713 : :
1714 [ + + ]: 221 : if (patterns->head == NULL)
1715 : 214 : return; /* nothing to do */
1716 : :
1717 : 7 : query = createPQExpBuffer();
1718 : :
1719 : : /*
1720 : : * The loop below runs multiple SELECTs might sometimes result in
1721 : : * duplicate entries in the OID list, but we don't care.
1722 : : */
1723 [ + + ]: 14 : for (cell = patterns->head; cell; cell = cell->next)
1724 : : {
1725 : : int dotcnt;
1726 : :
1727 : 7 : appendPQExpBufferStr(query,
1728 : : "SELECT oid FROM pg_catalog.pg_extension e\n");
1729 : 7 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1730 : : false, NULL, "e.extname", NULL, NULL, NULL,
1731 : : &dotcnt);
1732 [ - + ]: 7 : if (dotcnt > 0)
1733 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1734 : : cell->val);
1735 : :
1736 : 7 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1737 [ - + - - ]: 7 : if (strict_names && PQntuples(res) == 0)
1738 : 0 : pg_fatal("no matching extensions were found for pattern \"%s\"", cell->val);
1739 : :
1740 [ + + ]: 13 : for (i = 0; i < PQntuples(res); i++)
1741 : : {
1742 : 6 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1743 : : }
1744 : :
1745 : 7 : PQclear(res);
1746 : 7 : resetPQExpBuffer(query);
1747 : : }
1748 : :
1749 : 7 : destroyPQExpBuffer(query);
1750 : : }
1751 : :
1752 : : /*
1753 : : * Find the OIDs of all foreign servers matching the given list of patterns,
1754 : : * and append them to the given OID list.
1755 : : */
1756 : : static void
1757 : 218 : expand_foreign_server_name_patterns(Archive *fout,
1758 : : SimpleStringList *patterns,
1759 : : SimpleOidList *oids)
1760 : : {
1761 : : PQExpBuffer query;
1762 : : PGresult *res;
1763 : : SimpleStringListCell *cell;
1764 : : int i;
1765 : :
1766 [ + + ]: 218 : if (patterns->head == NULL)
1767 : 215 : return; /* nothing to do */
1768 : :
1769 : 3 : query = createPQExpBuffer();
1770 : :
1771 : : /*
1772 : : * The loop below runs multiple SELECTs might sometimes result in
1773 : : * duplicate entries in the OID list, but we don't care.
1774 : : */
1775 : :
1776 [ + + ]: 5 : for (cell = patterns->head; cell; cell = cell->next)
1777 : : {
1778 : : int dotcnt;
1779 : :
1780 : 3 : appendPQExpBufferStr(query,
1781 : : "SELECT oid FROM pg_catalog.pg_foreign_server s\n");
1782 : 3 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1783 : : false, NULL, "s.srvname", NULL, NULL, NULL,
1784 : : &dotcnt);
1785 [ - + ]: 3 : if (dotcnt > 0)
1786 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1787 : : cell->val);
1788 : :
1789 : 3 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1790 [ + + ]: 3 : if (PQntuples(res) == 0)
1791 : 1 : pg_fatal("no matching foreign servers were found for pattern \"%s\"", cell->val);
1792 : :
1793 [ + + ]: 4 : for (i = 0; i < PQntuples(res); i++)
1794 : 2 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1795 : :
1796 : 2 : PQclear(res);
1797 : 2 : resetPQExpBuffer(query);
1798 : : }
1799 : :
1800 : 2 : destroyPQExpBuffer(query);
1801 : : }
1802 : :
1803 : : /*
1804 : : * Find the OIDs of all tables matching the given list of patterns,
1805 : : * and append them to the given OID list. See also expand_dbname_patterns()
1806 : : * in pg_dumpall.c
1807 : : */
1808 : : static void
1809 : 1317 : expand_table_name_patterns(Archive *fout,
1810 : : SimpleStringList *patterns, SimpleOidList *oids,
1811 : : bool strict_names, bool with_child_tables)
1812 : : {
1813 : : PQExpBuffer query;
1814 : : PGresult *res;
1815 : : SimpleStringListCell *cell;
1816 : : int i;
1817 : :
1818 [ + + ]: 1317 : if (patterns->head == NULL)
1819 : 1288 : return; /* nothing to do */
1820 : :
1821 : 29 : query = createPQExpBuffer();
1822 : :
1823 : : /*
1824 : : * this might sometimes result in duplicate entries in the OID list, but
1825 : : * we don't care.
1826 : : */
1827 : :
1828 [ + + ]: 59 : for (cell = patterns->head; cell; cell = cell->next)
1829 : : {
1830 : : PQExpBufferData dbbuf;
1831 : : int dotcnt;
1832 : :
1833 : : /*
1834 : : * Query must remain ABSOLUTELY devoid of unqualified names. This
1835 : : * would be unnecessary given a pg_table_is_visible() variant taking a
1836 : : * search_path argument.
1837 : : *
1838 : : * For with_child_tables, we start with the basic query's results and
1839 : : * recursively search the inheritance tree to add child tables.
1840 : : */
1841 [ + + ]: 35 : if (with_child_tables)
1842 : : {
1843 : 6 : appendPQExpBufferStr(query, "WITH RECURSIVE partition_tree (relid) AS (\n");
1844 : : }
1845 : :
1846 : 35 : appendPQExpBuffer(query,
1847 : : "SELECT c.oid"
1848 : : "\nFROM pg_catalog.pg_class c"
1849 : : "\n LEFT JOIN pg_catalog.pg_namespace n"
1850 : : "\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace"
1851 : : "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY"
1852 : : "\n (array['%c', '%c', '%c', '%c', '%c', '%c'])\n",
1853 : : RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1854 : : RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
1855 : : RELKIND_PARTITIONED_TABLE);
1856 : 35 : initPQExpBuffer(&dbbuf);
1857 : 35 : processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1858 : : false, "n.nspname", "c.relname", NULL,
1859 : : "pg_catalog.pg_table_is_visible(c.oid)", &dbbuf,
1860 : : &dotcnt);
1861 [ + + ]: 35 : if (dotcnt > 2)
1862 : 1 : pg_fatal("improper relation name (too many dotted names): %s",
1863 : : cell->val);
1864 [ + + ]: 34 : else if (dotcnt == 2)
1865 : 2 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1866 : 32 : termPQExpBuffer(&dbbuf);
1867 : :
1868 [ + + ]: 32 : if (with_child_tables)
1869 : : {
1870 : 6 : appendPQExpBufferStr(query, "UNION"
1871 : : "\nSELECT i.inhrelid"
1872 : : "\nFROM partition_tree p"
1873 : : "\n JOIN pg_catalog.pg_inherits i"
1874 : : "\n ON p.relid OPERATOR(pg_catalog.=) i.inhparent"
1875 : : "\n)"
1876 : : "\nSELECT relid FROM partition_tree");
1877 : : }
1878 : :
1879 : 32 : ExecuteSqlStatement(fout, "RESET search_path");
1880 : 32 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1881 : 32 : PQclear(ExecuteSqlQueryForSingleRow(fout,
1882 : : ALWAYS_SECURE_SEARCH_PATH_SQL));
1883 [ + + + + ]: 32 : if (strict_names && PQntuples(res) == 0)
1884 : 2 : pg_fatal("no matching tables were found for pattern \"%s\"", cell->val);
1885 : :
1886 [ + + ]: 74 : for (i = 0; i < PQntuples(res); i++)
1887 : : {
1888 : 44 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1889 : : }
1890 : :
1891 : 30 : PQclear(res);
1892 : 30 : resetPQExpBuffer(query);
1893 : : }
1894 : :
1895 : 24 : destroyPQExpBuffer(query);
1896 : : }
1897 : :
1898 : : /*
1899 : : * Verifies that the connected database name matches the given database name,
1900 : : * and if not, dies with an error about the given pattern.
1901 : : *
1902 : : * The 'dbname' argument should be a literal name parsed from 'pattern'.
1903 : : */
1904 : : static void
1905 : 5 : prohibit_crossdb_refs(PGconn *conn, const char *dbname, const char *pattern)
1906 : : {
1907 : : const char *db;
1908 : :
1909 : 5 : db = PQdb(conn);
1910 [ - + ]: 5 : if (db == NULL)
1911 : 0 : pg_fatal("You are currently not connected to a database.");
1912 : :
1913 [ + - ]: 5 : if (strcmp(db, dbname) != 0)
1914 : 5 : pg_fatal("cross-database references are not implemented: %s",
1915 : : pattern);
1916 : 0 : }
1917 : :
1918 : : /*
1919 : : * checkExtensionMembership
1920 : : * Determine whether object is an extension member, and if so,
1921 : : * record an appropriate dependency and set the object's dump flag.
1922 : : *
1923 : : * It's important to call this for each object that could be an extension
1924 : : * member. Generally, we integrate this with determining the object's
1925 : : * to-be-dumped-ness, since extension membership overrides other rules for that.
1926 : : *
1927 : : * Returns true if object is an extension member, else false.
1928 : : */
1929 : : static bool
1930 : 691600 : checkExtensionMembership(DumpableObject *dobj, Archive *fout)
1931 : : {
1932 : 691600 : ExtensionInfo *ext = findOwningExtension(dobj->catId);
1933 : :
1934 [ + + ]: 691600 : if (ext == NULL)
1935 : 690753 : return false;
1936 : :
1937 : 847 : dobj->ext_member = true;
1938 : :
1939 : : /* Record dependency so that getDependencies needn't deal with that */
1940 : 847 : addObjectDependency(dobj, ext->dobj.dumpId);
1941 : :
1942 : : /*
1943 : : * Mark the member object to have any non-initial ACLs dumped. (Any
1944 : : * initial ACLs will be removed later, using data from pg_init_privs, so
1945 : : * that we'll dump only the delta from the extension's initial setup.)
1946 : : *
1947 : : * In binary upgrades, we still dump all components of the members
1948 : : * individually, since the idea is to exactly reproduce the database
1949 : : * contents rather than replace the extension contents with something
1950 : : * different.
1951 : : *
1952 : : * Note: it might be interesting someday to implement storage and delta
1953 : : * dumping of extension members' RLS policies and/or security labels.
1954 : : * However there is a pitfall for RLS policies: trying to dump them
1955 : : * requires getting a lock on their tables, and the calling user might not
1956 : : * have privileges for that. We need no lock to examine a table's ACLs,
1957 : : * so the current feature doesn't have a problem of that sort.
1958 : : */
1959 [ + + ]: 847 : if (fout->dopt->binary_upgrade)
1960 : 202 : dobj->dump = ext->dobj.dump;
1961 : : else
1962 : 645 : dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL);
1963 : :
1964 : 847 : return true;
1965 : : }
1966 : :
1967 : : /*
1968 : : * selectDumpableNamespace: policy-setting subroutine
1969 : : * Mark a namespace as to be dumped or not
1970 : : */
1971 : : static void
1972 : 1780 : selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout)
1973 : : {
1974 : : /*
1975 : : * DUMP_COMPONENT_DEFINITION typically implies a CREATE SCHEMA statement
1976 : : * and (for --clean) a DROP SCHEMA statement. (In the absence of
1977 : : * DUMP_COMPONENT_DEFINITION, this value is irrelevant.)
1978 : : */
1979 : 1780 : nsinfo->create = true;
1980 : :
1981 : : /*
1982 : : * If specific tables are being dumped, do not dump any complete
1983 : : * namespaces. If specific namespaces are being dumped, dump just those
1984 : : * namespaces. Otherwise, dump all non-system namespaces.
1985 : : */
1986 [ + + ]: 1780 : if (table_include_oids.head != NULL)
1987 : 61 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1988 [ + + ]: 1719 : else if (schema_include_oids.head != NULL)
1989 : 205 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump =
1990 : 205 : simple_oid_list_member(&schema_include_oids,
1991 : : nsinfo->dobj.catId.oid) ?
1992 [ + + ]: 205 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1993 [ + + ]: 1514 : else if (strcmp(nsinfo->dobj.name, "pg_catalog") == 0)
1994 : : {
1995 : : /*
1996 : : * We dump out any ACLs defined in pg_catalog, if they are interesting
1997 : : * (and not the original ACLs which were set at initdb time, see
1998 : : * pg_init_privs).
1999 : : */
2000 : 193 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
2001 : : }
2002 [ + + ]: 1321 : else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
2003 [ + + ]: 563 : strcmp(nsinfo->dobj.name, "information_schema") == 0)
2004 : : {
2005 : : /* Other system schemas don't get dumped */
2006 : 951 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2007 : : }
2008 [ + + ]: 370 : else if (strcmp(nsinfo->dobj.name, "public") == 0)
2009 : : {
2010 : : /*
2011 : : * The public schema is a strange beast that sits in a sort of
2012 : : * no-mans-land between being a system object and a user object.
2013 : : * CREATE SCHEMA would fail, so its DUMP_COMPONENT_DEFINITION is just
2014 : : * a comment and an indication of ownership. If the owner is the
2015 : : * default, omit that superfluous DUMP_COMPONENT_DEFINITION. Before
2016 : : * v15, the default owner was BOOTSTRAP_SUPERUSERID.
2017 : : */
2018 : 189 : nsinfo->create = false;
2019 : 189 : nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2020 [ + + ]: 189 : if (nsinfo->nspowner == ROLE_PG_DATABASE_OWNER)
2021 : 129 : nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION;
2022 : 189 : nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL;
2023 : :
2024 : : /*
2025 : : * Also, make like it has a comment even if it doesn't; this is so
2026 : : * that we'll emit a command to drop the comment, if appropriate.
2027 : : * (Without this, we'd not call dumpCommentExtended for it.)
2028 : : */
2029 : 189 : nsinfo->dobj.components |= DUMP_COMPONENT_COMMENT;
2030 : : }
2031 : : else
2032 : 181 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2033 : :
2034 : : /*
2035 : : * In any case, a namespace can be excluded by an exclusion switch
2036 : : */
2037 [ + + + + ]: 2355 : if (nsinfo->dobj.dump_contains &&
2038 : 575 : simple_oid_list_member(&schema_exclude_oids,
2039 : : nsinfo->dobj.catId.oid))
2040 : 3 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2041 : :
2042 : : /*
2043 : : * If the schema belongs to an extension, allow extension membership to
2044 : : * override the dump decision for the schema itself. However, this does
2045 : : * not change dump_contains, so this won't change what we do with objects
2046 : : * within the schema. (If they belong to the extension, they'll get
2047 : : * suppressed by it, otherwise not.)
2048 : : */
2049 : 1780 : (void) checkExtensionMembership(&nsinfo->dobj, fout);
2050 : 1780 : }
2051 : :
2052 : : /*
2053 : : * selectDumpableTable: policy-setting subroutine
2054 : : * Mark a table as to be dumped or not
2055 : : */
2056 : : static void
2057 : 57413 : selectDumpableTable(TableInfo *tbinfo, Archive *fout)
2058 : : {
2059 [ + + ]: 57413 : if (checkExtensionMembership(&tbinfo->dobj, fout))
2060 : 225 : return; /* extension membership overrides all else */
2061 : :
2062 : : /*
2063 : : * If specific tables are being dumped, dump just those tables; else, dump
2064 : : * according to the parent namespace's dump flag.
2065 : : */
2066 [ + + ]: 57188 : if (table_include_oids.head != NULL)
2067 : 5350 : tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
2068 : : tbinfo->dobj.catId.oid) ?
2069 [ + + ]: 2675 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2070 : : else
2071 : 54513 : tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains;
2072 : :
2073 : : /*
2074 : : * In any case, a table can be excluded by an exclusion switch
2075 : : */
2076 [ + + + + ]: 94203 : if (tbinfo->dobj.dump &&
2077 : 37015 : simple_oid_list_member(&table_exclude_oids,
2078 : : tbinfo->dobj.catId.oid))
2079 : 12 : tbinfo->dobj.dump = DUMP_COMPONENT_NONE;
2080 : : }
2081 : :
2082 : : /*
2083 : : * selectDumpableType: policy-setting subroutine
2084 : : * Mark a type as to be dumped or not
2085 : : *
2086 : : * If it's a table's rowtype or an autogenerated array type, we also apply a
2087 : : * special type code to facilitate sorting into the desired order. (We don't
2088 : : * want to consider those to be ordinary types because that would bring tables
2089 : : * up into the datatype part of the dump order.) We still set the object's
2090 : : * dump flag; that's not going to cause the dummy type to be dumped, but we
2091 : : * need it so that casts involving such types will be dumped correctly -- see
2092 : : * dumpCast. This means the flag should be set the same as for the underlying
2093 : : * object (the table or base type).
2094 : : */
2095 : : static void
2096 : 157788 : selectDumpableType(TypeInfo *tyinfo, Archive *fout)
2097 : : {
2098 : : /* skip complex types, except for standalone composite types */
2099 [ + + ]: 157788 : if (OidIsValid(tyinfo->typrelid) &&
2100 [ + + ]: 56715 : tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
2101 : : {
2102 : 56469 : TableInfo *tytable = findTableByOid(tyinfo->typrelid);
2103 : :
2104 : 56469 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2105 [ + - ]: 56469 : if (tytable != NULL)
2106 : 56469 : tyinfo->dobj.dump = tytable->dobj.dump;
2107 : : else
2108 : 0 : tyinfo->dobj.dump = DUMP_COMPONENT_NONE;
2109 : 56469 : return;
2110 : : }
2111 : :
2112 : : /* skip auto-generated array and multirange types */
2113 [ + + + + ]: 101319 : if (tyinfo->isArray || tyinfo->isMultirange)
2114 : : {
2115 : 77184 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2116 : :
2117 : : /*
2118 : : * Fall through to set the dump flag; we assume that the subsequent
2119 : : * rules will do the same thing as they would for the array's base
2120 : : * type or multirange's range type. (We cannot reliably look up the
2121 : : * base type here, since getTypes may not have processed it yet.)
2122 : : */
2123 : : }
2124 : :
2125 [ + + ]: 101319 : if (checkExtensionMembership(&tyinfo->dobj, fout))
2126 : 150 : return; /* extension membership overrides all else */
2127 : :
2128 : : /* Dump based on if the contents of the namespace are being dumped */
2129 : 101169 : tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains;
2130 : : }
2131 : :
2132 : : /*
2133 : : * selectDumpableDefaultACL: policy-setting subroutine
2134 : : * Mark a default ACL as to be dumped or not
2135 : : *
2136 : : * For per-schema default ACLs, dump if the schema is to be dumped.
2137 : : * Otherwise dump if we are dumping "everything". Note that dumpSchema
2138 : : * and aclsSkip are checked separately.
2139 : : */
2140 : : static void
2141 : 206 : selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt)
2142 : : {
2143 : : /* Default ACLs can't be extension members */
2144 : :
2145 [ + + ]: 206 : if (dinfo->dobj.namespace)
2146 : : /* default ACLs are considered part of the namespace */
2147 : 96 : dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains;
2148 : : else
2149 : 110 : dinfo->dobj.dump = dopt->include_everything ?
2150 [ + + ]: 110 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2151 : 206 : }
2152 : :
2153 : : /*
2154 : : * selectDumpableCast: policy-setting subroutine
2155 : : * Mark a cast as to be dumped or not
2156 : : *
2157 : : * Casts do not belong to any particular namespace (since they haven't got
2158 : : * names), nor do they have identifiable owners. To distinguish user-defined
2159 : : * casts from built-in ones, we must resort to checking whether the cast's
2160 : : * OID is in the range reserved for initdb.
2161 : : */
2162 : : static void
2163 : 52335 : selectDumpableCast(CastInfo *cast, Archive *fout)
2164 : : {
2165 [ - + ]: 52335 : if (checkExtensionMembership(&cast->dobj, fout))
2166 : 0 : return; /* extension membership overrides all else */
2167 : :
2168 : : /*
2169 : : * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
2170 : : * support ACLs currently.
2171 : : */
2172 [ + + ]: 52335 : if (cast->dobj.catId.oid <= g_last_builtin_oid)
2173 : 52245 : cast->dobj.dump = DUMP_COMPONENT_NONE;
2174 : : else
2175 : 90 : cast->dobj.dump = fout->dopt->include_everything ?
2176 [ + + ]: 90 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2177 : : }
2178 : :
2179 : : /*
2180 : : * selectDumpableProcLang: policy-setting subroutine
2181 : : * Mark a procedural language as to be dumped or not
2182 : : *
2183 : : * Procedural languages do not belong to any particular namespace. To
2184 : : * identify built-in languages, we must resort to checking whether the
2185 : : * language's OID is in the range reserved for initdb.
2186 : : */
2187 : : static void
2188 : 263 : selectDumpableProcLang(ProcLangInfo *plang, Archive *fout)
2189 : : {
2190 [ + + ]: 263 : if (checkExtensionMembership(&plang->dobj, fout))
2191 : 209 : return; /* extension membership overrides all else */
2192 : :
2193 : : /*
2194 : : * Only include procedural languages when we are dumping everything.
2195 : : *
2196 : : * For from-initdb procedural languages, only include ACLs, as we do for
2197 : : * the pg_catalog namespace. We need this because procedural languages do
2198 : : * not live in any namespace.
2199 : : */
2200 [ + + ]: 54 : if (!fout->dopt->include_everything)
2201 : 9 : plang->dobj.dump = DUMP_COMPONENT_NONE;
2202 : : else
2203 : : {
2204 [ + + ]: 45 : if (plang->dobj.catId.oid <= g_last_builtin_oid)
2205 : 6 : plang->dobj.dump = DUMP_COMPONENT_ACL;
2206 : : else
2207 : 39 : plang->dobj.dump = DUMP_COMPONENT_ALL;
2208 : : }
2209 : : }
2210 : :
2211 : : /*
2212 : : * selectDumpableAccessMethod: policy-setting subroutine
2213 : : * Mark an access method as to be dumped or not
2214 : : *
2215 : : * Access methods do not belong to any particular namespace. To identify
2216 : : * built-in access methods, we must resort to checking whether the
2217 : : * method's OID is in the range reserved for initdb.
2218 : : */
2219 : : static void
2220 : 1633 : selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout)
2221 : : {
2222 [ + + ]: 1633 : if (checkExtensionMembership(&method->dobj, fout))
2223 : 25 : return; /* extension membership overrides all else */
2224 : :
2225 : : /*
2226 : : * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
2227 : : * they do not support ACLs currently.
2228 : : */
2229 [ + + ]: 1608 : if (method->dobj.catId.oid <= g_last_builtin_oid)
2230 : 1505 : method->dobj.dump = DUMP_COMPONENT_NONE;
2231 : : else
2232 : 103 : method->dobj.dump = fout->dopt->include_everything ?
2233 [ + + ]: 103 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2234 : : }
2235 : :
2236 : : /*
2237 : : * selectDumpableExtension: policy-setting subroutine
2238 : : * Mark an extension as to be dumped or not
2239 : : *
2240 : : * Built-in extensions should be skipped except for checking ACLs, since we
2241 : : * assume those will already be installed in the target database. We identify
2242 : : * such extensions by their having OIDs in the range reserved for initdb.
2243 : : * We dump all user-added extensions by default. No extensions are dumped
2244 : : * if include_everything is false (i.e., a --schema or --table switch was
2245 : : * given), except if --extension specifies a list of extensions to dump.
2246 : : */
2247 : : static void
2248 : 247 : selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
2249 : : {
2250 : : /*
2251 : : * Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to
2252 : : * change permissions on their member objects, if they wish to, and have
2253 : : * those changes preserved.
2254 : : */
2255 [ + + ]: 247 : if (extinfo->dobj.catId.oid <= g_last_builtin_oid)
2256 : 216 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
2257 : : else
2258 : : {
2259 : : /* check if there is a list of extensions to dump */
2260 [ + + ]: 31 : if (extension_include_oids.head != NULL)
2261 : 4 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2262 : 4 : simple_oid_list_member(&extension_include_oids,
2263 : : extinfo->dobj.catId.oid) ?
2264 [ + + ]: 4 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2265 : : else
2266 : 27 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2267 : 27 : dopt->include_everything ?
2268 [ + + ]: 27 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2269 : :
2270 : : /* check that the extension is not explicitly excluded */
2271 [ + + + + ]: 58 : if (extinfo->dobj.dump &&
2272 : 27 : simple_oid_list_member(&extension_exclude_oids,
2273 : : extinfo->dobj.catId.oid))
2274 : 2 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE;
2275 : : }
2276 : 247 : }
2277 : :
2278 : : /*
2279 : : * selectDumpablePublicationObject: policy-setting subroutine
2280 : : * Mark a publication object as to be dumped or not
2281 : : *
2282 : : * A publication can have schemas and tables which have schemas, but those are
2283 : : * ignored in decision making, because publications are only dumped when we are
2284 : : * dumping everything.
2285 : : */
2286 : : static void
2287 : 502 : selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout)
2288 : : {
2289 [ - + ]: 502 : if (checkExtensionMembership(dobj, fout))
2290 : 0 : return; /* extension membership overrides all else */
2291 : :
2292 : 502 : dobj->dump = fout->dopt->include_everything ?
2293 [ + + ]: 502 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2294 : : }
2295 : :
2296 : : /*
2297 : : * selectDumpableStatisticsObject: policy-setting subroutine
2298 : : * Mark an extended statistics object as to be dumped or not
2299 : : *
2300 : : * We dump an extended statistics object if the schema it's in and the table
2301 : : * it's for are being dumped. (This'll need more thought if statistics
2302 : : * objects ever support cross-table stats.)
2303 : : */
2304 : : static void
2305 : 233 : selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout)
2306 : : {
2307 [ - + ]: 233 : if (checkExtensionMembership(&sobj->dobj, fout))
2308 : 0 : return; /* extension membership overrides all else */
2309 : :
2310 : 233 : sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains;
2311 [ + - ]: 233 : if (sobj->stattable == NULL ||
2312 [ + + ]: 233 : !(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION))
2313 : 35 : sobj->dobj.dump = DUMP_COMPONENT_NONE;
2314 : : }
2315 : :
2316 : : /*
2317 : : * selectDumpableObject: policy-setting subroutine
2318 : : * Mark a generic dumpable object as to be dumped or not
2319 : : *
2320 : : * Use this only for object types without a special-case routine above.
2321 : : */
2322 : : static void
2323 : 476122 : selectDumpableObject(DumpableObject *dobj, Archive *fout)
2324 : : {
2325 [ + + ]: 476122 : if (checkExtensionMembership(dobj, fout))
2326 : 213 : return; /* extension membership overrides all else */
2327 : :
2328 : : /*
2329 : : * Default policy is to dump if parent namespace is dumpable, or for
2330 : : * non-namespace-associated items, dump if we're dumping "everything".
2331 : : */
2332 [ + + ]: 475909 : if (dobj->namespace)
2333 : 474989 : dobj->dump = dobj->namespace->dobj.dump_contains;
2334 : : else
2335 : 920 : dobj->dump = fout->dopt->include_everything ?
2336 [ + + ]: 920 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2337 : : }
2338 : :
2339 : : /*
2340 : : * Dump a table's contents for loading using the COPY command
2341 : : * - this routine is called by the Archiver when it wants the table
2342 : : * to be dumped.
2343 : : */
2344 : : static int
2345 : 4441 : dumpTableData_copy(Archive *fout, const void *dcontext)
2346 : : {
2347 : 4441 : const TableDataInfo *tdinfo = dcontext;
2348 : 4441 : const TableInfo *tbinfo = tdinfo->tdtable;
2349 : 4441 : const char *classname = tbinfo->dobj.name;
2350 : 4441 : PQExpBuffer q = createPQExpBuffer();
2351 : :
2352 : : /*
2353 : : * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
2354 : : * which uses it already.
2355 : : */
2356 : 4441 : PQExpBuffer clistBuf = createPQExpBuffer();
2357 : 4441 : PGconn *conn = GetConnection(fout);
2358 : : PGresult *res;
2359 : : int ret;
2360 : : char *copybuf;
2361 : : const char *column_list;
2362 : :
2363 : 4441 : pg_log_info("dumping contents of table \"%s.%s\"",
2364 : : tbinfo->dobj.namespace->dobj.name, classname);
2365 : :
2366 : : /*
2367 : : * Specify the column list explicitly so that we have no possibility of
2368 : : * retrieving data in the wrong column order. (The default column
2369 : : * ordering of COPY will not be what we want in certain corner cases
2370 : : * involving ADD COLUMN and inheritance.)
2371 : : */
2372 : 4441 : column_list = fmtCopyColumnList(tbinfo, clistBuf);
2373 : :
2374 : : /*
2375 : : * Use COPY (SELECT ...) TO when dumping a foreign table's data, when a
2376 : : * filter condition was specified, and when in binary upgrade mode and
2377 : : * dumping an old pg_largeobject_metadata defined WITH OIDS. For other
2378 : : * cases a simple COPY suffices.
2379 : : */
2380 [ + + + + ]: 4441 : if (tdinfo->filtercond || tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
2381 [ - + - - ]: 4396 : (fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
2382 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
2383 : : {
2384 : : /* Temporary allows to access to foreign tables to dump data */
2385 [ + + ]: 45 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2386 : 1 : set_restrict_relation_kind(fout, "view");
2387 : :
2388 : 45 : appendPQExpBufferStr(q, "COPY (SELECT ");
2389 : : /* klugery to get rid of parens in column list */
2390 [ + - ]: 45 : if (strlen(column_list) > 2)
2391 : : {
2392 : 45 : appendPQExpBufferStr(q, column_list + 1);
2393 : 45 : q->data[q->len - 1] = ' ';
2394 : : }
2395 : : else
2396 : 0 : appendPQExpBufferStr(q, "* ");
2397 : :
2398 : 90 : appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
2399 : 45 : fmtQualifiedDumpable(tbinfo),
2400 [ + + ]: 45 : tdinfo->filtercond ? tdinfo->filtercond : "");
2401 : : }
2402 : : else
2403 : : {
2404 : 4396 : appendPQExpBuffer(q, "COPY %s %s TO stdout;",
2405 : 4396 : fmtQualifiedDumpable(tbinfo),
2406 : : column_list);
2407 : : }
2408 : 4441 : res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
2409 : 4440 : PQclear(res);
2410 : 4440 : destroyPQExpBuffer(clistBuf);
2411 : :
2412 : : for (;;)
2413 : : {
2414 : 1822835 : ret = PQgetCopyData(conn, ©buf, 0);
2415 : :
2416 [ + + ]: 1822835 : if (ret < 0)
2417 : 4440 : break; /* done or error */
2418 : :
2419 [ + - ]: 1818395 : if (copybuf)
2420 : : {
2421 : 1818395 : WriteData(fout, copybuf, ret);
2422 : 1818395 : PQfreemem(copybuf);
2423 : : }
2424 : :
2425 : : /* ----------
2426 : : * THROTTLE:
2427 : : *
2428 : : * There was considerable discussion in late July, 2000 regarding
2429 : : * slowing down pg_dump when backing up large tables. Users with both
2430 : : * slow & fast (multi-processor) machines experienced performance
2431 : : * degradation when doing a backup.
2432 : : *
2433 : : * Initial attempts based on sleeping for a number of ms for each ms
2434 : : * of work were deemed too complex, then a simple 'sleep in each loop'
2435 : : * implementation was suggested. The latter failed because the loop
2436 : : * was too tight. Finally, the following was implemented:
2437 : : *
2438 : : * If throttle is non-zero, then
2439 : : * See how long since the last sleep.
2440 : : * Work out how long to sleep (based on ratio).
2441 : : * If sleep is more than 100ms, then
2442 : : * sleep
2443 : : * reset timer
2444 : : * EndIf
2445 : : * EndIf
2446 : : *
2447 : : * where the throttle value was the number of ms to sleep per ms of
2448 : : * work. The calculation was done in each loop.
2449 : : *
2450 : : * Most of the hard work is done in the backend, and this solution
2451 : : * still did not work particularly well: on slow machines, the ratio
2452 : : * was 50:1, and on medium paced machines, 1:1, and on fast
2453 : : * multi-processor machines, it had little or no effect, for reasons
2454 : : * that were unclear.
2455 : : *
2456 : : * Further discussion ensued, and the proposal was dropped.
2457 : : *
2458 : : * For those people who want this feature, it can be implemented using
2459 : : * gettimeofday in each loop, calculating the time since last sleep,
2460 : : * multiplying that by the sleep ratio, then if the result is more
2461 : : * than a preset 'minimum sleep time' (say 100ms), call the 'select'
2462 : : * function to sleep for a subsecond period ie.
2463 : : *
2464 : : * select(0, NULL, NULL, NULL, &tvi);
2465 : : *
2466 : : * This will return after the interval specified in the structure tvi.
2467 : : * Finally, call gettimeofday again to save the 'last sleep time'.
2468 : : * ----------
2469 : : */
2470 : : }
2471 : 4440 : archprintf(fout, "\\.\n\n\n");
2472 : :
2473 [ - + ]: 4440 : if (ret == -2)
2474 : : {
2475 : : /* copy data transfer failed */
2476 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.", classname);
2477 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2478 : 0 : pg_log_error_detail("Command was: %s", q->data);
2479 : 0 : exit_nicely(1);
2480 : : }
2481 : :
2482 : : /* Check command status and return to normal libpq state */
2483 : 4440 : res = PQgetResult(conn);
2484 [ - + ]: 4440 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
2485 : : {
2486 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetResult() failed.", classname);
2487 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2488 : 0 : pg_log_error_detail("Command was: %s", q->data);
2489 : 0 : exit_nicely(1);
2490 : : }
2491 : 4440 : PQclear(res);
2492 : :
2493 : : /* Do this to ensure we've pumped libpq back to idle state */
2494 [ - + ]: 4440 : if (PQgetResult(conn) != NULL)
2495 : 0 : pg_log_warning("unexpected extra results during COPY of table \"%s\"",
2496 : : classname);
2497 : :
2498 : 4440 : destroyPQExpBuffer(q);
2499 : :
2500 : : /* Revert back the setting */
2501 [ - + ]: 4440 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2502 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2503 : :
2504 : 4440 : return 1;
2505 : : }
2506 : :
2507 : : /*
2508 : : * Dump table data using INSERT commands.
2509 : : *
2510 : : * Caution: when we restore from an archive file direct to database, the
2511 : : * INSERT commands emitted by this function have to be parsed by
2512 : : * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments,
2513 : : * E'' strings, or dollar-quoted strings. So don't emit anything like that.
2514 : : */
2515 : : static int
2516 : 105 : dumpTableData_insert(Archive *fout, const void *dcontext)
2517 : : {
2518 : 105 : const TableDataInfo *tdinfo = dcontext;
2519 : 105 : const TableInfo *tbinfo = tdinfo->tdtable;
2520 : 105 : DumpOptions *dopt = fout->dopt;
2521 : 105 : PQExpBuffer q = createPQExpBuffer();
2522 : 105 : PQExpBuffer insertStmt = NULL;
2523 : : char *attgenerated;
2524 : : PGresult *res;
2525 : : int nfields,
2526 : : i;
2527 : 105 : int rows_per_statement = dopt->dump_inserts;
2528 : 105 : int rows_this_statement = 0;
2529 : :
2530 : : /* Temporary allows to access to foreign tables to dump data */
2531 [ - + ]: 105 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2532 : 0 : set_restrict_relation_kind(fout, "view");
2533 : :
2534 : : /*
2535 : : * If we're going to emit INSERTs with column names, the most efficient
2536 : : * way to deal with generated columns is to exclude them entirely. For
2537 : : * INSERTs without column names, we have to emit DEFAULT rather than the
2538 : : * actual column value --- but we can save a few cycles by fetching nulls
2539 : : * rather than the uninteresting-to-us value.
2540 : : */
2541 : 105 : attgenerated = pg_malloc_array(char, tbinfo->numatts);
2542 : 105 : appendPQExpBufferStr(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT ");
2543 : 105 : nfields = 0;
2544 [ + + ]: 331 : for (i = 0; i < tbinfo->numatts; i++)
2545 : : {
2546 [ + + ]: 226 : if (tbinfo->attisdropped[i])
2547 : 6 : continue;
2548 [ + + + + ]: 220 : if (tbinfo->attgenerated[i] && dopt->column_inserts)
2549 : 10 : continue;
2550 [ + + ]: 210 : if (nfields > 0)
2551 : 113 : appendPQExpBufferStr(q, ", ");
2552 [ + + ]: 210 : if (tbinfo->attgenerated[i])
2553 : 12 : appendPQExpBufferStr(q, "NULL");
2554 : : else
2555 : 198 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[i]));
2556 : 210 : attgenerated[nfields] = tbinfo->attgenerated[i];
2557 : 210 : nfields++;
2558 : : }
2559 : : /* Servers before 9.4 will complain about zero-column SELECT */
2560 [ + + ]: 105 : if (nfields == 0)
2561 : 8 : appendPQExpBufferStr(q, "NULL");
2562 : 105 : appendPQExpBuffer(q, " FROM ONLY %s",
2563 : 105 : fmtQualifiedDumpable(tbinfo));
2564 [ - + ]: 105 : if (tdinfo->filtercond)
2565 : 0 : appendPQExpBuffer(q, " %s", tdinfo->filtercond);
2566 : :
2567 : 105 : ExecuteSqlStatement(fout, q->data);
2568 : :
2569 : : while (1)
2570 : : {
2571 : 168 : res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
2572 : : PGRES_TUPLES_OK);
2573 : :
2574 : : /* cross-check field count, allowing for dummy NULL if any */
2575 [ + + + - ]: 168 : if (nfields != PQnfields(res) &&
2576 [ - + ]: 12 : !(nfields == 0 && PQnfields(res) == 1))
2577 : 0 : pg_fatal("wrong number of fields retrieved from table \"%s\"",
2578 : : tbinfo->dobj.name);
2579 : :
2580 : : /*
2581 : : * First time through, we build as much of the INSERT statement as
2582 : : * possible in "insertStmt", which we can then just print for each
2583 : : * statement. If the table happens to have zero dumpable columns then
2584 : : * this will be a complete statement, otherwise it will end in
2585 : : * "VALUES" and be ready to have the row's column values printed.
2586 : : */
2587 [ + + ]: 168 : if (insertStmt == NULL)
2588 : : {
2589 : : const TableInfo *targettab;
2590 : :
2591 : 105 : insertStmt = createPQExpBuffer();
2592 : :
2593 : : /*
2594 : : * When load-via-partition-root is set or forced, get the root
2595 : : * table name for the partition table, so that we can reload data
2596 : : * through the root table.
2597 : : */
2598 [ + + ]: 105 : if (tbinfo->ispartition &&
2599 [ + - + + ]: 54 : (dopt->load_via_partition_root ||
2600 : 27 : forcePartitionRootLoad(tbinfo)))
2601 : 10 : targettab = getRootTableInfo(tbinfo);
2602 : : else
2603 : 95 : targettab = tbinfo;
2604 : :
2605 : 105 : appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
2606 : 105 : fmtQualifiedDumpable(targettab));
2607 : :
2608 : : /* corner case for zero-column table */
2609 [ + + ]: 105 : if (nfields == 0)
2610 : : {
2611 : 8 : appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n");
2612 : : }
2613 : : else
2614 : : {
2615 : : /* append the list of column names if required */
2616 [ + + ]: 97 : if (dopt->column_inserts)
2617 : : {
2618 : 42 : appendPQExpBufferChar(insertStmt, '(');
2619 [ + + ]: 128 : for (int field = 0; field < nfields; field++)
2620 : : {
2621 [ + + ]: 86 : if (field > 0)
2622 : 44 : appendPQExpBufferStr(insertStmt, ", ");
2623 : 86 : appendPQExpBufferStr(insertStmt,
2624 : 86 : fmtId(PQfname(res, field)));
2625 : : }
2626 : 42 : appendPQExpBufferStr(insertStmt, ") ");
2627 : : }
2628 : :
2629 [ + + ]: 97 : if (tbinfo->needs_override)
2630 : 5 : appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE ");
2631 : :
2632 : 97 : appendPQExpBufferStr(insertStmt, "VALUES");
2633 : : }
2634 : : }
2635 : :
2636 [ + + ]: 3660 : for (int tuple = 0; tuple < PQntuples(res); tuple++)
2637 : : {
2638 : : /* Write the INSERT if not in the middle of a multi-row INSERT. */
2639 [ + + ]: 3492 : if (rows_this_statement == 0)
2640 : 3482 : archputs(insertStmt->data, fout);
2641 : :
2642 : : /*
2643 : : * If it is zero-column table then we've already written the
2644 : : * complete statement, which will mean we've disobeyed
2645 : : * --rows-per-insert when it's set greater than 1. We do support
2646 : : * a way to make this multi-row with: SELECT UNION ALL SELECT
2647 : : * UNION ALL ... but that's non-standard so we should avoid it
2648 : : * given that using INSERTs is mostly only ever needed for
2649 : : * cross-database exports.
2650 : : */
2651 [ + + ]: 3492 : if (nfields == 0)
2652 : 7 : continue;
2653 : :
2654 : : /* Emit a row heading */
2655 [ + + ]: 3485 : if (rows_per_statement == 1)
2656 : 3463 : archputs(" (", fout);
2657 [ + + ]: 22 : else if (rows_this_statement > 0)
2658 : 10 : archputs(",\n\t(", fout);
2659 : : else
2660 : 12 : archputs("\n\t(", fout);
2661 : :
2662 [ + + ]: 10531 : for (int field = 0; field < nfields; field++)
2663 : : {
2664 [ + + ]: 7046 : if (field > 0)
2665 : 3561 : archputs(", ", fout);
2666 [ + + ]: 7046 : if (attgenerated[field])
2667 : : {
2668 : 10 : archputs("DEFAULT", fout);
2669 : 10 : continue;
2670 : : }
2671 [ + + ]: 7036 : if (PQgetisnull(res, tuple, field))
2672 : : {
2673 : 86 : archputs("NULL", fout);
2674 : 86 : continue;
2675 : : }
2676 : :
2677 : : /* XXX This code is partially duplicated in ruleutils.c */
2678 [ + + + + ]: 6950 : switch (PQftype(res, field))
2679 : : {
2680 : 4887 : case INT2OID:
2681 : : case INT4OID:
2682 : : case INT8OID:
2683 : : case OIDOID:
2684 : : case FLOAT4OID:
2685 : : case FLOAT8OID:
2686 : : case NUMERICOID:
2687 : : {
2688 : : /*
2689 : : * These types are printed without quotes unless
2690 : : * they contain values that aren't accepted by the
2691 : : * scanner unquoted (e.g., 'NaN'). Note that
2692 : : * strtod() and friends might accept NaN, so we
2693 : : * can't use that to test.
2694 : : *
2695 : : * In reality we only need to defend against
2696 : : * infinity and NaN, so we need not get too crazy
2697 : : * about pattern matching here.
2698 : : */
2699 : 4887 : const char *s = PQgetvalue(res, tuple, field);
2700 : :
2701 [ + + ]: 4887 : if (strspn(s, "0123456789 +-eE.") == strlen(s))
2702 : 4882 : archputs(s, fout);
2703 : : else
2704 : 5 : archprintf(fout, "'%s'", s);
2705 : : }
2706 : 4887 : break;
2707 : :
2708 : 8 : case BITOID:
2709 : : case VARBITOID:
2710 : 8 : archprintf(fout, "B'%s'",
2711 : : PQgetvalue(res, tuple, field));
2712 : 8 : break;
2713 : :
2714 : 13 : case BOOLOID:
2715 [ + + ]: 13 : if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
2716 : 8 : archputs("true", fout);
2717 : : else
2718 : 5 : archputs("false", fout);
2719 : 13 : break;
2720 : :
2721 : 2042 : default:
2722 : : /* All other types are printed as string literals. */
2723 : 2042 : resetPQExpBuffer(q);
2724 : 2042 : appendStringLiteralAH(q,
2725 : : PQgetvalue(res, tuple, field),
2726 : : fout);
2727 : 2042 : archputs(q->data, fout);
2728 : 2042 : break;
2729 : : }
2730 : : }
2731 : :
2732 : : /* Terminate the row ... */
2733 : 3485 : archputs(")", fout);
2734 : :
2735 : : /* ... and the statement, if the target no. of rows is reached */
2736 [ + + ]: 3485 : if (++rows_this_statement >= rows_per_statement)
2737 : : {
2738 [ + + ]: 3469 : if (dopt->do_nothing)
2739 : 2 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2740 : : else
2741 : 3467 : archputs(";\n", fout);
2742 : : /* Reset the row counter */
2743 : 3469 : rows_this_statement = 0;
2744 : : }
2745 : : }
2746 : :
2747 [ + + ]: 168 : if (PQntuples(res) <= 0)
2748 : : {
2749 : 105 : PQclear(res);
2750 : 105 : break;
2751 : : }
2752 : 63 : PQclear(res);
2753 : : }
2754 : :
2755 : : /* Terminate any statements that didn't make the row count. */
2756 [ + + ]: 105 : if (rows_this_statement > 0)
2757 : : {
2758 [ + + ]: 6 : if (dopt->do_nothing)
2759 : 3 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2760 : : else
2761 : 3 : archputs(";\n", fout);
2762 : : }
2763 : :
2764 : 105 : archputs("\n\n", fout);
2765 : :
2766 : 105 : ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
2767 : :
2768 : 105 : destroyPQExpBuffer(q);
2769 [ + - ]: 105 : if (insertStmt != NULL)
2770 : 105 : destroyPQExpBuffer(insertStmt);
2771 : 105 : pg_free(attgenerated);
2772 : :
2773 : : /* Revert back the setting */
2774 [ - + ]: 105 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2775 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2776 : :
2777 : 105 : return 1;
2778 : : }
2779 : :
2780 : : /*
2781 : : * getRootTableInfo:
2782 : : * get the root TableInfo for the given partition table.
2783 : : */
2784 : : static TableInfo *
2785 : 102 : getRootTableInfo(const TableInfo *tbinfo)
2786 : : {
2787 : : TableInfo *parentTbinfo;
2788 : :
2789 : : Assert(tbinfo->ispartition);
2790 : : Assert(tbinfo->numParents == 1);
2791 : :
2792 : 102 : parentTbinfo = tbinfo->parents[0];
2793 [ - + ]: 102 : while (parentTbinfo->ispartition)
2794 : : {
2795 : : Assert(parentTbinfo->numParents == 1);
2796 : 0 : parentTbinfo = parentTbinfo->parents[0];
2797 : : }
2798 : :
2799 : 102 : return parentTbinfo;
2800 : : }
2801 : :
2802 : : /*
2803 : : * forcePartitionRootLoad
2804 : : * Check if we must force load_via_partition_root for this partition.
2805 : : *
2806 : : * This is required if any level of ancestral partitioned table has an
2807 : : * unsafe partitioning scheme.
2808 : : */
2809 : : static bool
2810 : 1125 : forcePartitionRootLoad(const TableInfo *tbinfo)
2811 : : {
2812 : : TableInfo *parentTbinfo;
2813 : :
2814 : : Assert(tbinfo->ispartition);
2815 : : Assert(tbinfo->numParents == 1);
2816 : :
2817 : 1125 : parentTbinfo = tbinfo->parents[0];
2818 [ + + ]: 1125 : if (parentTbinfo->unsafe_partitions)
2819 : 102 : return true;
2820 [ + + ]: 1243 : while (parentTbinfo->ispartition)
2821 : : {
2822 : : Assert(parentTbinfo->numParents == 1);
2823 : 220 : parentTbinfo = parentTbinfo->parents[0];
2824 [ - + ]: 220 : if (parentTbinfo->unsafe_partitions)
2825 : 0 : return true;
2826 : : }
2827 : :
2828 : 1023 : return false;
2829 : : }
2830 : :
2831 : : /*
2832 : : * dumpTableData -
2833 : : * dump the contents of a single table
2834 : : *
2835 : : * Actually, this just makes an ArchiveEntry for the table contents.
2836 : : */
2837 : : static void
2838 : 4638 : dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
2839 : : {
2840 : 4638 : DumpOptions *dopt = fout->dopt;
2841 : 4638 : const TableInfo *tbinfo = tdinfo->tdtable;
2842 : 4638 : PQExpBuffer copyBuf = createPQExpBuffer();
2843 : 4638 : PQExpBuffer clistBuf = createPQExpBuffer();
2844 : : DataDumperPtr dumpFn;
2845 : 4638 : char *tdDefn = NULL;
2846 : : char *copyStmt;
2847 : : const char *copyFrom;
2848 : :
2849 : : /* We had better have loaded per-column details about this table */
2850 : : Assert(tbinfo->interesting);
2851 : :
2852 : : /*
2853 : : * When load-via-partition-root is set or forced, get the root table name
2854 : : * for the partition table, so that we can reload data through the root
2855 : : * table. Then construct a comment to be inserted into the TOC entry's
2856 : : * defn field, so that such cases can be identified reliably.
2857 : : */
2858 [ + + ]: 4638 : if (tbinfo->ispartition &&
2859 [ + - + + ]: 2196 : (dopt->load_via_partition_root ||
2860 : 1098 : forcePartitionRootLoad(tbinfo)))
2861 : 92 : {
2862 : : const TableInfo *parentTbinfo;
2863 : : char *sanitized;
2864 : :
2865 : 92 : parentTbinfo = getRootTableInfo(tbinfo);
2866 : 92 : copyFrom = fmtQualifiedDumpable(parentTbinfo);
2867 : 92 : sanitized = sanitize_line(copyFrom, true);
2868 : 92 : printfPQExpBuffer(copyBuf, "-- load via partition root %s",
2869 : : sanitized);
2870 : 92 : free(sanitized);
2871 : 92 : tdDefn = pg_strdup(copyBuf->data);
2872 : : }
2873 : : else
2874 : 4546 : copyFrom = fmtQualifiedDumpable(tbinfo);
2875 : :
2876 [ + + ]: 4638 : if (dopt->dump_inserts == 0)
2877 : : {
2878 : : /* Dump/restore using COPY */
2879 : 4533 : dumpFn = dumpTableData_copy;
2880 : : /* must use 2 steps here 'cause fmtId is nonreentrant */
2881 : 4533 : printfPQExpBuffer(copyBuf, "COPY %s ",
2882 : : copyFrom);
2883 : 4533 : appendPQExpBuffer(copyBuf, "%s FROM stdin;\n",
2884 : : fmtCopyColumnList(tbinfo, clistBuf));
2885 : 4533 : copyStmt = copyBuf->data;
2886 : : }
2887 : : else
2888 : : {
2889 : : /* Restore using INSERT */
2890 : 105 : dumpFn = dumpTableData_insert;
2891 : 105 : copyStmt = NULL;
2892 : : }
2893 : :
2894 : : /*
2895 : : * Note: although the TableDataInfo is a full DumpableObject, we treat its
2896 : : * dependency on its table as "special" and pass it to ArchiveEntry now.
2897 : : * See comments for BuildArchiveDependencies.
2898 : : */
2899 [ + - ]: 4638 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2900 : : {
2901 : : TocEntry *te;
2902 : :
2903 : 4638 : te = ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
2904 : 4638 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
2905 : : .namespace = tbinfo->dobj.namespace->dobj.name,
2906 : : .owner = tbinfo->rolname,
2907 : : .description = "TABLE DATA",
2908 : : .section = SECTION_DATA,
2909 : : .createStmt = tdDefn,
2910 : : .copyStmt = copyStmt,
2911 : : .deps = &(tbinfo->dobj.dumpId),
2912 : : .nDeps = 1,
2913 : : .dumpFn = dumpFn,
2914 : : .dumpArg = tdinfo));
2915 : :
2916 : : /*
2917 : : * Set the TocEntry's dataLength in case we are doing a parallel dump
2918 : : * and want to order dump jobs by table size. We choose to measure
2919 : : * dataLength in table pages (including TOAST pages) during dump, so
2920 : : * no scaling is needed.
2921 : : *
2922 : : * However, relpages is declared as "integer" in pg_class, and hence
2923 : : * also in TableInfo, but it's really BlockNumber a/k/a unsigned int.
2924 : : * Cast so that we get the right interpretation of table sizes
2925 : : * exceeding INT_MAX pages.
2926 : : */
2927 : 4638 : te->dataLength = (BlockNumber) tbinfo->relpages;
2928 : 4638 : te->dataLength += (BlockNumber) tbinfo->toastpages;
2929 : :
2930 : : /*
2931 : : * If pgoff_t is only 32 bits wide, the above refinement is useless,
2932 : : * and instead we'd better worry about integer overflow. Clamp to
2933 : : * INT_MAX if the correct result exceeds that.
2934 : : */
2935 : : if (sizeof(te->dataLength) == 4 &&
2936 : : (tbinfo->relpages < 0 || tbinfo->toastpages < 0 ||
2937 : : te->dataLength < 0))
2938 : : te->dataLength = INT_MAX;
2939 : : }
2940 : :
2941 : 4638 : destroyPQExpBuffer(copyBuf);
2942 : 4638 : destroyPQExpBuffer(clistBuf);
2943 : 4638 : }
2944 : :
2945 : : /*
2946 : : * refreshMatViewData -
2947 : : * load or refresh the contents of a single materialized view
2948 : : *
2949 : : * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
2950 : : * statement.
2951 : : */
2952 : : static void
2953 : 363 : refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo)
2954 : : {
2955 : 363 : TableInfo *tbinfo = tdinfo->tdtable;
2956 : : PQExpBuffer q;
2957 : :
2958 : : /* If the materialized view is not flagged as populated, skip this. */
2959 [ + + ]: 363 : if (!tbinfo->relispopulated)
2960 : 72 : return;
2961 : :
2962 : 291 : q = createPQExpBuffer();
2963 : :
2964 : 291 : appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
2965 : 291 : fmtQualifiedDumpable(tbinfo));
2966 : :
2967 [ + - ]: 291 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2968 : 291 : ArchiveEntry(fout,
2969 : : tdinfo->dobj.catId, /* catalog ID */
2970 : 291 : tdinfo->dobj.dumpId, /* dump ID */
2971 : 291 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
2972 : : .namespace = tbinfo->dobj.namespace->dobj.name,
2973 : : .owner = tbinfo->rolname,
2974 : : .description = "MATERIALIZED VIEW DATA",
2975 : : .section = SECTION_POST_DATA,
2976 : : .createStmt = q->data,
2977 : : .deps = tdinfo->dobj.dependencies,
2978 : : .nDeps = tdinfo->dobj.nDeps));
2979 : :
2980 : 291 : destroyPQExpBuffer(q);
2981 : : }
2982 : :
2983 : : /*
2984 : : * getTableData -
2985 : : * set up dumpable objects representing the contents of tables
2986 : : */
2987 : : static void
2988 : 206 : getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
2989 : : {
2990 : : int i;
2991 : :
2992 [ + + ]: 55330 : for (i = 0; i < numTables; i++)
2993 : : {
2994 [ + + + + ]: 55124 : if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
2995 [ + + ]: 986 : (!relkind || tblinfo[i].relkind == relkind))
2996 : 6458 : makeTableDataInfo(dopt, &(tblinfo[i]));
2997 : : }
2998 : 206 : }
2999 : :
3000 : : /*
3001 : : * Make a dumpable object for the data of this specific table
3002 : : *
3003 : : * Note: we make a TableDataInfo if and only if we are going to dump the
3004 : : * table data; the "dump" field in such objects isn't very interesting.
3005 : : */
3006 : : static void
3007 : 6541 : makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo)
3008 : : {
3009 : : TableDataInfo *tdinfo;
3010 : :
3011 : : /*
3012 : : * Nothing to do if we already decided to dump the table. This will
3013 : : * happen for "config" tables.
3014 : : */
3015 [ + + ]: 6541 : if (tbinfo->dataObj != NULL)
3016 : 1 : return;
3017 : :
3018 : : /* Skip VIEWs (no data to dump) */
3019 [ + + ]: 6540 : if (tbinfo->relkind == RELKIND_VIEW)
3020 : 524 : return;
3021 : : /* Skip FOREIGN TABLEs (no data to dump) unless requested explicitly */
3022 [ + + ]: 6016 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
3023 [ + + ]: 40 : (foreign_servers_include_oids.head == NULL ||
3024 [ + + ]: 4 : !simple_oid_list_member(&foreign_servers_include_oids,
3025 : : tbinfo->foreign_server)))
3026 : 39 : return;
3027 : : /* Skip partitioned tables (data in partitions) */
3028 [ + + ]: 5977 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
3029 : 535 : return;
3030 : :
3031 : : /* Don't dump data in unlogged tables, if so requested */
3032 [ + + ]: 5442 : if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
3033 [ + + ]: 41 : dopt->no_unlogged_table_data)
3034 : 18 : return;
3035 : :
3036 : : /* Check that the data is not explicitly excluded */
3037 [ + + ]: 5424 : if (simple_oid_list_member(&tabledata_exclude_oids,
3038 : : tbinfo->dobj.catId.oid))
3039 : 8 : return;
3040 : :
3041 : : /* OK, let's dump it */
3042 : 5416 : tdinfo = pg_malloc_object(TableDataInfo);
3043 : :
3044 [ + + ]: 5416 : if (tbinfo->relkind == RELKIND_MATVIEW)
3045 : 363 : tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
3046 [ + + ]: 5053 : else if (tbinfo->relkind == RELKIND_SEQUENCE)
3047 : 415 : tdinfo->dobj.objType = DO_SEQUENCE_SET;
3048 : : else
3049 : 4638 : tdinfo->dobj.objType = DO_TABLE_DATA;
3050 : :
3051 : : /*
3052 : : * Note: use tableoid 0 so that this object won't be mistaken for
3053 : : * something that pg_depend entries apply to.
3054 : : */
3055 : 5416 : tdinfo->dobj.catId.tableoid = 0;
3056 : 5416 : tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
3057 : 5416 : AssignDumpId(&tdinfo->dobj);
3058 : 5416 : tdinfo->dobj.name = tbinfo->dobj.name;
3059 : 5416 : tdinfo->dobj.namespace = tbinfo->dobj.namespace;
3060 : 5416 : tdinfo->tdtable = tbinfo;
3061 : 5416 : tdinfo->filtercond = NULL; /* might get set later */
3062 : 5416 : addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
3063 : :
3064 : : /* A TableDataInfo contains data, of course */
3065 : 5416 : tdinfo->dobj.components |= DUMP_COMPONENT_DATA;
3066 : :
3067 : 5416 : tbinfo->dataObj = tdinfo;
3068 : :
3069 : : /*
3070 : : * Materialized view statistics must be restored after the data, because
3071 : : * REFRESH MATERIALIZED VIEW replaces the storage and resets the stats.
3072 : : *
3073 : : * The dependency is added here because the statistics objects are created
3074 : : * first.
3075 : : */
3076 [ + + + + ]: 5416 : if (tbinfo->relkind == RELKIND_MATVIEW && tbinfo->stats != NULL)
3077 : : {
3078 : 286 : tbinfo->stats->section = SECTION_POST_DATA;
3079 : 286 : addObjectDependency(&tbinfo->stats->dobj, tdinfo->dobj.dumpId);
3080 : : }
3081 : :
3082 : : /* Make sure that we'll collect per-column info for this table. */
3083 : 5416 : tbinfo->interesting = true;
3084 : : }
3085 : :
3086 : : /*
3087 : : * The refresh for a materialized view must be dependent on the refresh for
3088 : : * any materialized view that this one is dependent on.
3089 : : *
3090 : : * This must be called after all the objects are created, but before they are
3091 : : * sorted.
3092 : : */
3093 : : static void
3094 : 166 : buildMatViewRefreshDependencies(Archive *fout)
3095 : : {
3096 : : PQExpBuffer query;
3097 : : PGresult *res;
3098 : : int ntups,
3099 : : i;
3100 : : int i_classid,
3101 : : i_objid,
3102 : : i_refobjid;
3103 : :
3104 : 166 : query = createPQExpBuffer();
3105 : :
3106 : 166 : appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
3107 : : "( "
3108 : : "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
3109 : : "FROM pg_depend d1 "
3110 : : "JOIN pg_class c1 ON c1.oid = d1.objid "
3111 : : "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
3112 : : " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
3113 : : "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
3114 : : "AND d2.objid = r1.oid "
3115 : : "AND d2.refobjid <> d1.objid "
3116 : : "JOIN pg_class c2 ON c2.oid = d2.refobjid "
3117 : : "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3118 : : CppAsString2(RELKIND_VIEW) ") "
3119 : : "WHERE d1.classid = 'pg_class'::regclass "
3120 : : "UNION "
3121 : : "SELECT w.objid, d3.refobjid, c3.relkind "
3122 : : "FROM w "
3123 : : "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
3124 : : "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
3125 : : "AND d3.objid = r3.oid "
3126 : : "AND d3.refobjid <> w.refobjid "
3127 : : "JOIN pg_class c3 ON c3.oid = d3.refobjid "
3128 : : "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3129 : : CppAsString2(RELKIND_VIEW) ") "
3130 : : ") "
3131 : : "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
3132 : : "FROM w "
3133 : : "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
3134 : :
3135 : 166 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3136 : :
3137 : 166 : ntups = PQntuples(res);
3138 : :
3139 : 166 : i_classid = PQfnumber(res, "classid");
3140 : 166 : i_objid = PQfnumber(res, "objid");
3141 : 166 : i_refobjid = PQfnumber(res, "refobjid");
3142 : :
3143 [ + + ]: 442 : for (i = 0; i < ntups; i++)
3144 : : {
3145 : : CatalogId objId;
3146 : : CatalogId refobjId;
3147 : : DumpableObject *dobj;
3148 : : DumpableObject *refdobj;
3149 : : TableInfo *tbinfo;
3150 : : TableInfo *reftbinfo;
3151 : :
3152 : 276 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
3153 : 276 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
3154 : 276 : refobjId.tableoid = objId.tableoid;
3155 : 276 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
3156 : :
3157 : 276 : dobj = findObjectByCatalogId(objId);
3158 [ - + ]: 276 : if (dobj == NULL)
3159 : 48 : continue;
3160 : :
3161 : : Assert(dobj->objType == DO_TABLE);
3162 : 276 : tbinfo = (TableInfo *) dobj;
3163 : : Assert(tbinfo->relkind == RELKIND_MATVIEW);
3164 : 276 : dobj = (DumpableObject *) tbinfo->dataObj;
3165 [ + + ]: 276 : if (dobj == NULL)
3166 : 48 : continue;
3167 : : Assert(dobj->objType == DO_REFRESH_MATVIEW);
3168 : :
3169 : 228 : refdobj = findObjectByCatalogId(refobjId);
3170 [ - + ]: 228 : if (refdobj == NULL)
3171 : 0 : continue;
3172 : :
3173 : : Assert(refdobj->objType == DO_TABLE);
3174 : 228 : reftbinfo = (TableInfo *) refdobj;
3175 : : Assert(reftbinfo->relkind == RELKIND_MATVIEW);
3176 : 228 : refdobj = (DumpableObject *) reftbinfo->dataObj;
3177 [ - + ]: 228 : if (refdobj == NULL)
3178 : 0 : continue;
3179 : : Assert(refdobj->objType == DO_REFRESH_MATVIEW);
3180 : :
3181 : 228 : addObjectDependency(dobj, refdobj->dumpId);
3182 : :
3183 [ + + ]: 228 : if (!reftbinfo->relispopulated)
3184 : 36 : tbinfo->relispopulated = false;
3185 : : }
3186 : :
3187 : 166 : PQclear(res);
3188 : :
3189 : 166 : destroyPQExpBuffer(query);
3190 : 166 : }
3191 : :
3192 : : /*
3193 : : * getTableDataFKConstraints -
3194 : : * add dump-order dependencies reflecting foreign key constraints
3195 : : *
3196 : : * This code is executed only in a data-only dump --- in schema+data dumps
3197 : : * we handle foreign key issues by not creating the FK constraints until
3198 : : * after the data is loaded. In a data-only dump, however, we want to
3199 : : * order the table data objects in such a way that a table's referenced
3200 : : * tables are restored first. (In the presence of circular references or
3201 : : * self-references this may be impossible; we'll detect and complain about
3202 : : * that during the dependency sorting step.)
3203 : : */
3204 : : static void
3205 : 7 : getTableDataFKConstraints(void)
3206 : : {
3207 : : DumpableObject **dobjs;
3208 : : int numObjs;
3209 : : int i;
3210 : :
3211 : : /* Search through all the dumpable objects for FK constraints */
3212 : 7 : getDumpableObjects(&dobjs, &numObjs);
3213 [ + + ]: 26534 : for (i = 0; i < numObjs; i++)
3214 : : {
3215 [ + + ]: 26527 : if (dobjs[i]->objType == DO_FK_CONSTRAINT)
3216 : : {
3217 : 8 : ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
3218 : : TableInfo *ftable;
3219 : :
3220 : : /* Not interesting unless both tables are to be dumped */
3221 [ + - ]: 8 : if (cinfo->contable == NULL ||
3222 [ + + ]: 8 : cinfo->contable->dataObj == NULL)
3223 : 4 : continue;
3224 : 4 : ftable = findTableByOid(cinfo->confrelid);
3225 [ + - ]: 4 : if (ftable == NULL ||
3226 [ - + ]: 4 : ftable->dataObj == NULL)
3227 : 0 : continue;
3228 : :
3229 : : /*
3230 : : * Okay, make referencing table's TABLE_DATA object depend on the
3231 : : * referenced table's TABLE_DATA object.
3232 : : */
3233 : 4 : addObjectDependency(&cinfo->contable->dataObj->dobj,
3234 : 4 : ftable->dataObj->dobj.dumpId);
3235 : : }
3236 : : }
3237 : 7 : free(dobjs);
3238 : 7 : }
3239 : :
3240 : :
3241 : : /*
3242 : : * dumpDatabase:
3243 : : * dump the database definition
3244 : : */
3245 : : static void
3246 : 108 : dumpDatabase(Archive *fout)
3247 : : {
3248 : 108 : DumpOptions *dopt = fout->dopt;
3249 : 108 : PQExpBuffer dbQry = createPQExpBuffer();
3250 : 108 : PQExpBuffer delQry = createPQExpBuffer();
3251 : 108 : PQExpBuffer creaQry = createPQExpBuffer();
3252 : 108 : PQExpBuffer labelq = createPQExpBuffer();
3253 : 108 : PGconn *conn = GetConnection(fout);
3254 : : PGresult *res;
3255 : : int i_tableoid,
3256 : : i_oid,
3257 : : i_datname,
3258 : : i_datdba,
3259 : : i_encoding,
3260 : : i_datlocprovider,
3261 : : i_collate,
3262 : : i_ctype,
3263 : : i_datlocale,
3264 : : i_daticurules,
3265 : : i_frozenxid,
3266 : : i_minmxid,
3267 : : i_datacl,
3268 : : i_acldefault,
3269 : : i_datistemplate,
3270 : : i_datconnlimit,
3271 : : i_datcollversion,
3272 : : i_tablespace;
3273 : : CatalogId dbCatId;
3274 : : DumpId dbDumpId;
3275 : : DumpableAcl dbdacl;
3276 : : const char *datname,
3277 : : *dba,
3278 : : *encoding,
3279 : : *datlocprovider,
3280 : : *collate,
3281 : : *ctype,
3282 : : *locale,
3283 : : *icurules,
3284 : : *datistemplate,
3285 : : *datconnlimit,
3286 : : *tablespace;
3287 : : uint32 frozenxid,
3288 : : minmxid;
3289 : : char *qdatname;
3290 : :
3291 : 108 : pg_log_info("saving database definition");
3292 : :
3293 : : /*
3294 : : * Fetch the database-level properties for this database.
3295 : : */
3296 : 108 : appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, "
3297 : : "datdba, "
3298 : : "pg_encoding_to_char(encoding) AS encoding, "
3299 : : "datcollate, datctype, datfrozenxid, "
3300 : : "datacl, acldefault('d', datdba) AS acldefault, "
3301 : : "datistemplate, datconnlimit, ");
3302 : 108 : appendPQExpBufferStr(dbQry, "datminmxid, ");
3303 [ + - ]: 108 : if (fout->remoteVersion >= 170000)
3304 : 108 : appendPQExpBufferStr(dbQry, "datlocprovider, datlocale, datcollversion, ");
3305 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
3306 : 0 : appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale AS datlocale, datcollversion, ");
3307 : : else
3308 : 0 : appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS datlocale, NULL AS datcollversion, ");
3309 [ + - ]: 108 : if (fout->remoteVersion >= 160000)
3310 : 108 : appendPQExpBufferStr(dbQry, "daticurules, ");
3311 : : else
3312 : 0 : appendPQExpBufferStr(dbQry, "NULL AS daticurules, ");
3313 : 108 : appendPQExpBufferStr(dbQry,
3314 : : "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
3315 : : "shobj_description(oid, 'pg_database') AS description "
3316 : : "FROM pg_database "
3317 : : "WHERE datname = current_database()");
3318 : :
3319 : 108 : res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
3320 : :
3321 : 108 : i_tableoid = PQfnumber(res, "tableoid");
3322 : 108 : i_oid = PQfnumber(res, "oid");
3323 : 108 : i_datname = PQfnumber(res, "datname");
3324 : 108 : i_datdba = PQfnumber(res, "datdba");
3325 : 108 : i_encoding = PQfnumber(res, "encoding");
3326 : 108 : i_datlocprovider = PQfnumber(res, "datlocprovider");
3327 : 108 : i_collate = PQfnumber(res, "datcollate");
3328 : 108 : i_ctype = PQfnumber(res, "datctype");
3329 : 108 : i_datlocale = PQfnumber(res, "datlocale");
3330 : 108 : i_daticurules = PQfnumber(res, "daticurules");
3331 : 108 : i_frozenxid = PQfnumber(res, "datfrozenxid");
3332 : 108 : i_minmxid = PQfnumber(res, "datminmxid");
3333 : 108 : i_datacl = PQfnumber(res, "datacl");
3334 : 108 : i_acldefault = PQfnumber(res, "acldefault");
3335 : 108 : i_datistemplate = PQfnumber(res, "datistemplate");
3336 : 108 : i_datconnlimit = PQfnumber(res, "datconnlimit");
3337 : 108 : i_datcollversion = PQfnumber(res, "datcollversion");
3338 : 108 : i_tablespace = PQfnumber(res, "tablespace");
3339 : :
3340 : 108 : dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
3341 : 108 : dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
3342 : 108 : datname = PQgetvalue(res, 0, i_datname);
3343 : 108 : dba = getRoleName(PQgetvalue(res, 0, i_datdba));
3344 : 108 : encoding = PQgetvalue(res, 0, i_encoding);
3345 : 108 : datlocprovider = PQgetvalue(res, 0, i_datlocprovider);
3346 : 108 : collate = PQgetvalue(res, 0, i_collate);
3347 : 108 : ctype = PQgetvalue(res, 0, i_ctype);
3348 [ + + ]: 108 : if (!PQgetisnull(res, 0, i_datlocale))
3349 : 14 : locale = PQgetvalue(res, 0, i_datlocale);
3350 : : else
3351 : 94 : locale = NULL;
3352 [ - + ]: 108 : if (!PQgetisnull(res, 0, i_daticurules))
3353 : 0 : icurules = PQgetvalue(res, 0, i_daticurules);
3354 : : else
3355 : 108 : icurules = NULL;
3356 : 108 : frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
3357 : 108 : minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
3358 : 108 : dbdacl.acl = PQgetvalue(res, 0, i_datacl);
3359 : 108 : dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault);
3360 : 108 : datistemplate = PQgetvalue(res, 0, i_datistemplate);
3361 : 108 : datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
3362 : 108 : tablespace = PQgetvalue(res, 0, i_tablespace);
3363 : :
3364 : 108 : qdatname = pg_strdup(fmtId(datname));
3365 : :
3366 : : /*
3367 : : * Prepare the CREATE DATABASE command. We must specify OID (if we want
3368 : : * to preserve that), as well as the encoding, locale, and tablespace
3369 : : * since those can't be altered later. Other DB properties are left to
3370 : : * the DATABASE PROPERTIES entry, so that they can be applied after
3371 : : * reconnecting to the target DB.
3372 : : *
3373 : : * For binary upgrade, we use the FILE_COPY strategy because testing has
3374 : : * shown it to be faster. When the server is in binary upgrade mode, it
3375 : : * will also skip the checkpoints this strategy ordinarily performs.
3376 : : */
3377 [ + + ]: 108 : if (dopt->binary_upgrade)
3378 : : {
3379 : 43 : appendPQExpBuffer(creaQry,
3380 : : "CREATE DATABASE %s WITH TEMPLATE = template0 "
3381 : : "OID = %u STRATEGY = FILE_COPY",
3382 : : qdatname, dbCatId.oid);
3383 : : }
3384 : : else
3385 : : {
3386 : 65 : appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
3387 : : qdatname);
3388 : : }
3389 [ + - ]: 108 : if (strlen(encoding) > 0)
3390 : : {
3391 : 108 : appendPQExpBufferStr(creaQry, " ENCODING = ");
3392 : 108 : appendStringLiteralAH(creaQry, encoding, fout);
3393 : : }
3394 : :
3395 : 108 : appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = ");
3396 [ + + ]: 108 : if (datlocprovider[0] == 'b')
3397 : 14 : appendPQExpBufferStr(creaQry, "builtin");
3398 [ + - ]: 94 : else if (datlocprovider[0] == 'c')
3399 : 94 : appendPQExpBufferStr(creaQry, "libc");
3400 [ # # ]: 0 : else if (datlocprovider[0] == 'i')
3401 : 0 : appendPQExpBufferStr(creaQry, "icu");
3402 : : else
3403 : 0 : pg_fatal("unrecognized locale provider: %s",
3404 : : datlocprovider);
3405 : :
3406 [ + - + - ]: 108 : if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
3407 : : {
3408 : 108 : appendPQExpBufferStr(creaQry, " LOCALE = ");
3409 : 108 : appendStringLiteralAH(creaQry, collate, fout);
3410 : : }
3411 : : else
3412 : : {
3413 [ # # ]: 0 : if (strlen(collate) > 0)
3414 : : {
3415 : 0 : appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
3416 : 0 : appendStringLiteralAH(creaQry, collate, fout);
3417 : : }
3418 [ # # ]: 0 : if (strlen(ctype) > 0)
3419 : : {
3420 : 0 : appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
3421 : 0 : appendStringLiteralAH(creaQry, ctype, fout);
3422 : : }
3423 : : }
3424 [ + + ]: 108 : if (locale)
3425 : : {
3426 [ + - ]: 14 : if (datlocprovider[0] == 'b')
3427 : 14 : appendPQExpBufferStr(creaQry, " BUILTIN_LOCALE = ");
3428 : : else
3429 : 0 : appendPQExpBufferStr(creaQry, " ICU_LOCALE = ");
3430 : :
3431 : 14 : appendStringLiteralAH(creaQry, locale, fout);
3432 : : }
3433 : :
3434 [ - + ]: 108 : if (icurules)
3435 : : {
3436 : 0 : appendPQExpBufferStr(creaQry, " ICU_RULES = ");
3437 : 0 : appendStringLiteralAH(creaQry, icurules, fout);
3438 : : }
3439 : :
3440 : : /*
3441 : : * For binary upgrade, carry over the collation version. For normal
3442 : : * dump/restore, omit the version, so that it is computed upon restore.
3443 : : */
3444 [ + + ]: 108 : if (dopt->binary_upgrade)
3445 : : {
3446 [ + - ]: 43 : if (!PQgetisnull(res, 0, i_datcollversion))
3447 : : {
3448 : 43 : appendPQExpBufferStr(creaQry, " COLLATION_VERSION = ");
3449 : 43 : appendStringLiteralAH(creaQry,
3450 : : PQgetvalue(res, 0, i_datcollversion),
3451 : : fout);
3452 : : }
3453 : : }
3454 : :
3455 : : /*
3456 : : * Note: looking at dopt->outputNoTablespaces here is completely the wrong
3457 : : * thing; the decision whether to specify a tablespace should be left till
3458 : : * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
3459 : : * label the DATABASE entry with the tablespace and let the normal
3460 : : * tablespace selection logic work ... but CREATE DATABASE doesn't pay
3461 : : * attention to default_tablespace, so that won't work.
3462 : : */
3463 [ + - + + ]: 108 : if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
3464 [ + - ]: 5 : !dopt->outputNoTablespaces)
3465 : 5 : appendPQExpBuffer(creaQry, " TABLESPACE = %s",
3466 : : fmtId(tablespace));
3467 : 108 : appendPQExpBufferStr(creaQry, ";\n");
3468 : :
3469 : 108 : appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
3470 : : qdatname);
3471 : :
3472 : 108 : dbDumpId = createDumpId();
3473 : :
3474 : 108 : ArchiveEntry(fout,
3475 : : dbCatId, /* catalog ID */
3476 : : dbDumpId, /* dump ID */
3477 : 108 : ARCHIVE_OPTS(.tag = datname,
3478 : : .owner = dba,
3479 : : .description = "DATABASE",
3480 : : .section = SECTION_PRE_DATA,
3481 : : .createStmt = creaQry->data,
3482 : : .dropStmt = delQry->data));
3483 : :
3484 : : /* Compute correct tag for archive entry */
3485 : 108 : appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
3486 : :
3487 : : /* Dump DB comment if any */
3488 : : {
3489 : : /*
3490 : : * 8.2 and up keep comments on shared objects in a shared table, so we
3491 : : * cannot use the dumpComment() code used for other database objects.
3492 : : * Be careful that the ArchiveEntry parameters match that function.
3493 : : */
3494 : 108 : char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
3495 : :
3496 [ + - + + : 108 : if (comment && *comment && !dopt->no_comments)
+ - ]
3497 : : {
3498 : 55 : resetPQExpBuffer(dbQry);
3499 : :
3500 : : /*
3501 : : * Generates warning when loaded into a differently-named
3502 : : * database.
3503 : : */
3504 : 55 : appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
3505 : 55 : appendStringLiteralAH(dbQry, comment, fout);
3506 : 55 : appendPQExpBufferStr(dbQry, ";\n");
3507 : :
3508 : 55 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3509 : 55 : ARCHIVE_OPTS(.tag = labelq->data,
3510 : : .owner = dba,
3511 : : .description = "COMMENT",
3512 : : .section = SECTION_NONE,
3513 : : .createStmt = dbQry->data,
3514 : : .deps = &dbDumpId,
3515 : : .nDeps = 1));
3516 : : }
3517 : : }
3518 : :
3519 : : /* Dump DB security label, if enabled */
3520 [ + - ]: 108 : if (!dopt->no_security_labels)
3521 : : {
3522 : : PGresult *shres;
3523 : : PQExpBuffer seclabelQry;
3524 : :
3525 : 108 : seclabelQry = createPQExpBuffer();
3526 : :
3527 : 108 : buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
3528 : 108 : shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
3529 : 108 : resetPQExpBuffer(seclabelQry);
3530 : 108 : emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
3531 [ - + ]: 108 : if (seclabelQry->len > 0)
3532 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3533 : 0 : ARCHIVE_OPTS(.tag = labelq->data,
3534 : : .owner = dba,
3535 : : .description = "SECURITY LABEL",
3536 : : .section = SECTION_NONE,
3537 : : .createStmt = seclabelQry->data,
3538 : : .deps = &dbDumpId,
3539 : : .nDeps = 1));
3540 : 108 : destroyPQExpBuffer(seclabelQry);
3541 : 108 : PQclear(shres);
3542 : : }
3543 : :
3544 : : /*
3545 : : * Dump ACL if any. Note that we do not support initial privileges
3546 : : * (pg_init_privs) on databases.
3547 : : */
3548 : 108 : dbdacl.privtype = 0;
3549 : 108 : dbdacl.initprivs = NULL;
3550 : :
3551 : 108 : dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE",
3552 : : qdatname, NULL, NULL,
3553 : : NULL, dba, &dbdacl);
3554 : :
3555 : : /*
3556 : : * Now construct a DATABASE PROPERTIES archive entry to restore any
3557 : : * non-default database-level properties. (The reason this must be
3558 : : * separate is that we cannot put any additional commands into the TOC
3559 : : * entry that has CREATE DATABASE. pg_restore would execute such a group
3560 : : * in an implicit transaction block, and the backend won't allow CREATE
3561 : : * DATABASE in that context.)
3562 : : */
3563 : 108 : resetPQExpBuffer(creaQry);
3564 : 108 : resetPQExpBuffer(delQry);
3565 : :
3566 [ + - - + ]: 108 : if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
3567 : 0 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
3568 : : qdatname, datconnlimit);
3569 : :
3570 [ + + ]: 108 : if (strcmp(datistemplate, "t") == 0)
3571 : : {
3572 : 16 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
3573 : : qdatname);
3574 : :
3575 : : /*
3576 : : * The backend won't accept DROP DATABASE on a template database. We
3577 : : * can deal with that by removing the template marking before the DROP
3578 : : * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
3579 : : * since no such command is currently supported, fake it with a direct
3580 : : * UPDATE on pg_database.
3581 : : */
3582 : 16 : appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
3583 : : "SET datistemplate = false WHERE datname = ");
3584 : 16 : appendStringLiteralAH(delQry, datname, fout);
3585 : 16 : appendPQExpBufferStr(delQry, ";\n");
3586 : : }
3587 : :
3588 : : /*
3589 : : * We do not restore pg_database.dathasloginevt because it is set
3590 : : * automatically on login event trigger creation.
3591 : : */
3592 : :
3593 : : /* Add database-specific SET options */
3594 : 108 : dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
3595 : :
3596 : : /*
3597 : : * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
3598 : : * entry, too, for lack of a better place.
3599 : : */
3600 [ + + ]: 108 : if (dopt->binary_upgrade)
3601 : : {
3602 : 43 : appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
3603 : 43 : appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
3604 : : "SET datfrozenxid = '%u', datminmxid = '%u'\n"
3605 : : "WHERE datname = ",
3606 : : frozenxid, minmxid);
3607 : 43 : appendStringLiteralAH(creaQry, datname, fout);
3608 : 43 : appendPQExpBufferStr(creaQry, ";\n");
3609 : : }
3610 : :
3611 [ + + ]: 108 : if (creaQry->len > 0)
3612 : 49 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3613 : 49 : ARCHIVE_OPTS(.tag = datname,
3614 : : .owner = dba,
3615 : : .description = "DATABASE PROPERTIES",
3616 : : .section = SECTION_PRE_DATA,
3617 : : .createStmt = creaQry->data,
3618 : : .dropStmt = delQry->data,
3619 : : .deps = &dbDumpId));
3620 : :
3621 : : /*
3622 : : * pg_largeobject comes from the old system intact, so set its
3623 : : * relfrozenxids, relminmxids and relfilenode.
3624 : : *
3625 : : * pg_largeobject_metadata also comes from the old system intact for
3626 : : * upgrades from v16 and newer, so set its relfrozenxids, relminmxids, and
3627 : : * relfilenode, too. pg_upgrade can't copy/link the files from older
3628 : : * versions because aclitem (needed by pg_largeobject_metadata.lomacl)
3629 : : * changed its storage format in v16.
3630 : : */
3631 [ + + ]: 108 : if (dopt->binary_upgrade)
3632 : : {
3633 : : PGresult *lo_res;
3634 : 43 : PQExpBuffer loFrozenQry = createPQExpBuffer();
3635 : 43 : PQExpBuffer loOutQry = createPQExpBuffer();
3636 : 43 : PQExpBuffer lomOutQry = createPQExpBuffer();
3637 : 43 : PQExpBuffer loHorizonQry = createPQExpBuffer();
3638 : 43 : PQExpBuffer lomHorizonQry = createPQExpBuffer();
3639 : : int ii_relfrozenxid,
3640 : : ii_relfilenode,
3641 : : ii_oid,
3642 : : ii_relminmxid;
3643 : :
3644 : 43 : appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n"
3645 : : "FROM pg_catalog.pg_class\n"
3646 : : "WHERE oid IN (%u, %u, %u, %u);\n",
3647 : : LargeObjectRelationId, LargeObjectLOidPNIndexId,
3648 : : LargeObjectMetadataRelationId, LargeObjectMetadataOidIndexId);
3649 : :
3650 : 43 : lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
3651 : :
3652 : 43 : ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
3653 : 43 : ii_relminmxid = PQfnumber(lo_res, "relminmxid");
3654 : 43 : ii_relfilenode = PQfnumber(lo_res, "relfilenode");
3655 : 43 : ii_oid = PQfnumber(lo_res, "oid");
3656 : :
3657 : 43 : appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
3658 : 43 : appendPQExpBufferStr(lomHorizonQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n");
3659 : 43 : appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n");
3660 : 43 : appendPQExpBufferStr(lomOutQry, "\n-- For binary upgrade, preserve pg_largeobject_metadata and index relfilenodes\n");
3661 [ + + ]: 215 : for (int i = 0; i < PQntuples(lo_res); ++i)
3662 : : {
3663 : : Oid oid;
3664 : : RelFileNumber relfilenumber;
3665 : : PQExpBuffer horizonQry;
3666 : : PQExpBuffer outQry;
3667 : :
3668 : 172 : oid = atooid(PQgetvalue(lo_res, i, ii_oid));
3669 : 172 : relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode));
3670 : :
3671 [ + + + + ]: 172 : if (oid == LargeObjectRelationId ||
3672 : : oid == LargeObjectLOidPNIndexId)
3673 : : {
3674 : 86 : horizonQry = loHorizonQry;
3675 : 86 : outQry = loOutQry;
3676 : : }
3677 : : else
3678 : : {
3679 : 86 : horizonQry = lomHorizonQry;
3680 : 86 : outQry = lomOutQry;
3681 : : }
3682 : :
3683 : 172 : appendPQExpBuffer(horizonQry, "UPDATE pg_catalog.pg_class\n"
3684 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
3685 : : "WHERE oid = %u;\n",
3686 : 172 : atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)),
3687 : 172 : atooid(PQgetvalue(lo_res, i, ii_relminmxid)),
3688 : 172 : atooid(PQgetvalue(lo_res, i, ii_oid)));
3689 : :
3690 [ + + + + ]: 172 : if (oid == LargeObjectRelationId ||
3691 : : oid == LargeObjectMetadataRelationId)
3692 : 86 : appendPQExpBuffer(outQry,
3693 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
3694 : : relfilenumber);
3695 [ + + + - ]: 86 : else if (oid == LargeObjectLOidPNIndexId ||
3696 : : oid == LargeObjectMetadataOidIndexId)
3697 : 86 : appendPQExpBuffer(outQry,
3698 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
3699 : : relfilenumber);
3700 : : }
3701 : :
3702 : 43 : appendPQExpBufferStr(loOutQry,
3703 : : "TRUNCATE pg_catalog.pg_largeobject;\n");
3704 : 43 : appendPQExpBufferStr(lomOutQry,
3705 : : "TRUNCATE pg_catalog.pg_largeobject_metadata;\n");
3706 : :
3707 : 43 : appendPQExpBufferStr(loOutQry, loHorizonQry->data);
3708 : 43 : appendPQExpBufferStr(lomOutQry, lomHorizonQry->data);
3709 : :
3710 : 43 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3711 : 43 : ARCHIVE_OPTS(.tag = "pg_largeobject",
3712 : : .description = "pg_largeobject",
3713 : : .section = SECTION_PRE_DATA,
3714 : : .createStmt = loOutQry->data));
3715 : :
3716 [ + - ]: 43 : if (fout->remoteVersion >= 160000)
3717 : 43 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3718 : 43 : ARCHIVE_OPTS(.tag = "pg_largeobject_metadata",
3719 : : .description = "pg_largeobject_metadata",
3720 : : .section = SECTION_PRE_DATA,
3721 : : .createStmt = lomOutQry->data));
3722 : :
3723 : 43 : PQclear(lo_res);
3724 : :
3725 : 43 : destroyPQExpBuffer(loFrozenQry);
3726 : 43 : destroyPQExpBuffer(loHorizonQry);
3727 : 43 : destroyPQExpBuffer(lomHorizonQry);
3728 : 43 : destroyPQExpBuffer(loOutQry);
3729 : 43 : destroyPQExpBuffer(lomOutQry);
3730 : : }
3731 : :
3732 : 108 : PQclear(res);
3733 : :
3734 : 108 : pg_free(qdatname);
3735 : 108 : destroyPQExpBuffer(dbQry);
3736 : 108 : destroyPQExpBuffer(delQry);
3737 : 108 : destroyPQExpBuffer(creaQry);
3738 : 108 : destroyPQExpBuffer(labelq);
3739 : 108 : }
3740 : :
3741 : : /*
3742 : : * Collect any database-specific or role-and-database-specific SET options
3743 : : * for this database, and append them to outbuf.
3744 : : */
3745 : : static void
3746 : 108 : dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
3747 : : const char *dbname, Oid dboid)
3748 : : {
3749 : 108 : PGconn *conn = GetConnection(AH);
3750 : 108 : PQExpBuffer buf = createPQExpBuffer();
3751 : : PGresult *res;
3752 : :
3753 : : /* First collect database-specific options */
3754 : 108 : printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3755 : : "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3756 : : dboid);
3757 : :
3758 : 108 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3759 : :
3760 [ + + ]: 138 : for (int i = 0; i < PQntuples(res); i++)
3761 : 30 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 0),
3762 : : "DATABASE", dbname, NULL, NULL,
3763 : : outbuf);
3764 : :
3765 : 108 : PQclear(res);
3766 : :
3767 : : /* Now look for role-and-database-specific options */
3768 : 108 : printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3769 : : "FROM pg_db_role_setting s, pg_roles r "
3770 : : "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3771 : : dboid);
3772 : :
3773 : 108 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3774 : :
3775 [ - + ]: 108 : for (int i = 0; i < PQntuples(res); i++)
3776 : 0 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 1),
3777 : 0 : "ROLE", PQgetvalue(res, i, 0),
3778 : : "DATABASE", dbname,
3779 : : outbuf);
3780 : :
3781 : 108 : PQclear(res);
3782 : :
3783 : 108 : destroyPQExpBuffer(buf);
3784 : 108 : }
3785 : :
3786 : : /*
3787 : : * dumpEncoding: put the correct encoding into the archive
3788 : : */
3789 : : static void
3790 : 215 : dumpEncoding(Archive *AH)
3791 : : {
3792 : 215 : const char *encname = pg_encoding_to_char(AH->encoding);
3793 : 215 : PQExpBuffer qry = createPQExpBuffer();
3794 : :
3795 : 215 : pg_log_info("saving encoding = %s", encname);
3796 : :
3797 : 215 : appendPQExpBufferStr(qry, "SET client_encoding = ");
3798 : 215 : appendStringLiteralAH(qry, encname, AH);
3799 : 215 : appendPQExpBufferStr(qry, ";\n");
3800 : :
3801 : 215 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3802 : 215 : ARCHIVE_OPTS(.tag = "ENCODING",
3803 : : .description = "ENCODING",
3804 : : .section = SECTION_PRE_DATA,
3805 : : .createStmt = qry->data));
3806 : :
3807 : 215 : destroyPQExpBuffer(qry);
3808 : 215 : }
3809 : :
3810 : :
3811 : : /*
3812 : : * dumpStdStrings: put the correct escape string behavior into the archive
3813 : : */
3814 : : static void
3815 : 215 : dumpStdStrings(Archive *AH)
3816 : : {
3817 [ + - ]: 215 : const char *stdstrings = AH->std_strings ? "on" : "off";
3818 : 215 : PQExpBuffer qry = createPQExpBuffer();
3819 : :
3820 : 215 : pg_log_info("saving \"standard_conforming_strings = %s\"",
3821 : : stdstrings);
3822 : :
3823 : 215 : appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
3824 : : stdstrings);
3825 : :
3826 : 215 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3827 : 215 : ARCHIVE_OPTS(.tag = "STDSTRINGS",
3828 : : .description = "STDSTRINGS",
3829 : : .section = SECTION_PRE_DATA,
3830 : : .createStmt = qry->data));
3831 : :
3832 : 215 : destroyPQExpBuffer(qry);
3833 : 215 : }
3834 : :
3835 : : /*
3836 : : * dumpSearchPath: record the active search_path in the archive
3837 : : */
3838 : : static void
3839 : 215 : dumpSearchPath(Archive *AH)
3840 : : {
3841 : 215 : PQExpBuffer qry = createPQExpBuffer();
3842 : 215 : PQExpBuffer path = createPQExpBuffer();
3843 : : PGresult *res;
3844 : 215 : char **schemanames = NULL;
3845 : 215 : int nschemanames = 0;
3846 : : int i;
3847 : :
3848 : : /*
3849 : : * We use the result of current_schemas(), not the search_path GUC,
3850 : : * because that might contain wildcards such as "$user", which won't
3851 : : * necessarily have the same value during restore. Also, this way avoids
3852 : : * listing schemas that may appear in search_path but not actually exist,
3853 : : * which seems like a prudent exclusion.
3854 : : */
3855 : 215 : res = ExecuteSqlQueryForSingleRow(AH,
3856 : : "SELECT pg_catalog.current_schemas(false)");
3857 : :
3858 [ - + ]: 215 : if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames))
3859 : 0 : pg_fatal("could not parse result of current_schemas()");
3860 : :
3861 : : /*
3862 : : * We use set_config(), not a simple "SET search_path" command, because
3863 : : * the latter has less-clean behavior if the search path is empty. While
3864 : : * that's likely to get fixed at some point, it seems like a good idea to
3865 : : * be as backwards-compatible as possible in what we put into archives.
3866 : : */
3867 [ - + ]: 215 : for (i = 0; i < nschemanames; i++)
3868 : : {
3869 [ # # ]: 0 : if (i > 0)
3870 : 0 : appendPQExpBufferStr(path, ", ");
3871 : 0 : appendPQExpBufferStr(path, fmtId(schemanames[i]));
3872 : : }
3873 : :
3874 : 215 : appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', ");
3875 : 215 : appendStringLiteralAH(qry, path->data, AH);
3876 : 215 : appendPQExpBufferStr(qry, ", false);\n");
3877 : :
3878 : 215 : pg_log_info("saving \"search_path = %s\"", path->data);
3879 : :
3880 : 215 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3881 : 215 : ARCHIVE_OPTS(.tag = "SEARCHPATH",
3882 : : .description = "SEARCHPATH",
3883 : : .section = SECTION_PRE_DATA,
3884 : : .createStmt = qry->data));
3885 : :
3886 : : /* Also save it in AH->searchpath, in case we're doing plain text dump */
3887 : 215 : AH->searchpath = pg_strdup(qry->data);
3888 : :
3889 : 215 : free(schemanames);
3890 : 215 : PQclear(res);
3891 : 215 : destroyPQExpBuffer(qry);
3892 : 215 : destroyPQExpBuffer(path);
3893 : 215 : }
3894 : :
3895 : :
3896 : : /*
3897 : : * getLOs:
3898 : : * Collect schema-level data about large objects
3899 : : */
3900 : : static void
3901 : 186 : getLOs(Archive *fout)
3902 : : {
3903 : 186 : DumpOptions *dopt = fout->dopt;
3904 : 186 : PQExpBuffer loQry = createPQExpBuffer();
3905 : : PGresult *res;
3906 : : int ntups;
3907 : : int i;
3908 : : int n;
3909 : : int i_oid;
3910 : : int i_lomowner;
3911 : : int i_lomacl;
3912 : : int i_acldefault;
3913 : :
3914 : 186 : pg_log_info("reading large objects");
3915 : :
3916 : : /*
3917 : : * Fetch LO OIDs and owner/ACL data. Order the data so that all the blobs
3918 : : * with the same owner/ACL appear together.
3919 : : */
3920 : 186 : appendPQExpBufferStr(loQry,
3921 : : "SELECT oid, lomowner, lomacl, "
3922 : : "acldefault('L', lomowner) AS acldefault "
3923 : : "FROM pg_largeobject_metadata ");
3924 : :
3925 : : /*
3926 : : * For binary upgrades, we transfer pg_largeobject_metadata via COPY or by
3927 : : * copying/linking its files from the old cluster. On such upgrades, we
3928 : : * only need to consider large objects that have comments or security
3929 : : * labels, since we still restore those objects via COMMENT/SECURITY LABEL
3930 : : * commands.
3931 : : */
3932 [ + + ]: 186 : if (dopt->binary_upgrade)
3933 : 44 : appendPQExpBufferStr(loQry,
3934 : : "WHERE oid IN "
3935 : : "(SELECT objoid FROM pg_description "
3936 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) " "
3937 : : "UNION SELECT objoid FROM pg_seclabel "
3938 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) ") ");
3939 : :
3940 : 186 : appendPQExpBufferStr(loQry,
3941 : : "ORDER BY lomowner, lomacl::pg_catalog.text, oid");
3942 : :
3943 : 186 : res = ExecuteSqlQuery(fout, loQry->data, PGRES_TUPLES_OK);
3944 : :
3945 : 186 : i_oid = PQfnumber(res, "oid");
3946 : 186 : i_lomowner = PQfnumber(res, "lomowner");
3947 : 186 : i_lomacl = PQfnumber(res, "lomacl");
3948 : 186 : i_acldefault = PQfnumber(res, "acldefault");
3949 : :
3950 : 186 : ntups = PQntuples(res);
3951 : :
3952 : : /*
3953 : : * Group the blobs into suitably-sized groups that have the same owner and
3954 : : * ACL setting, and build a metadata and a data DumpableObject for each
3955 : : * group. (If we supported initprivs for blobs, we'd have to insist that
3956 : : * groups also share initprivs settings, since the DumpableObject only has
3957 : : * room for one.) i is the index of the first tuple in the current group,
3958 : : * and n is the number of tuples we include in the group.
3959 : : */
3960 [ + + ]: 274 : for (i = 0; i < ntups; i += n)
3961 : : {
3962 : 88 : Oid thisoid = atooid(PQgetvalue(res, i, i_oid));
3963 : 88 : char *thisowner = PQgetvalue(res, i, i_lomowner);
3964 : 88 : char *thisacl = PQgetvalue(res, i, i_lomacl);
3965 : : LoInfo *loinfo;
3966 : : DumpableObject *lodata;
3967 : : char namebuf[64];
3968 : :
3969 : : /* Scan to find first tuple not to be included in group */
3970 : 88 : n = 1;
3971 [ + - + + ]: 102 : while (n < MAX_BLOBS_PER_ARCHIVE_ENTRY && i + n < ntups)
3972 : : {
3973 [ + - ]: 49 : if (strcmp(thisowner, PQgetvalue(res, i + n, i_lomowner)) != 0 ||
3974 [ + + ]: 49 : strcmp(thisacl, PQgetvalue(res, i + n, i_lomacl)) != 0)
3975 : : break;
3976 : 14 : n++;
3977 : : }
3978 : :
3979 : : /* Build the metadata DumpableObject */
3980 : 88 : loinfo = (LoInfo *) pg_malloc(offsetof(LoInfo, looids) + n * sizeof(Oid));
3981 : :
3982 : 88 : loinfo->dobj.objType = DO_LARGE_OBJECT;
3983 : 88 : loinfo->dobj.catId.tableoid = LargeObjectRelationId;
3984 : 88 : loinfo->dobj.catId.oid = thisoid;
3985 : 88 : AssignDumpId(&loinfo->dobj);
3986 : :
3987 [ + + ]: 88 : if (n > 1)
3988 : 10 : snprintf(namebuf, sizeof(namebuf), "%u..%u", thisoid,
3989 : 10 : atooid(PQgetvalue(res, i + n - 1, i_oid)));
3990 : : else
3991 : 78 : snprintf(namebuf, sizeof(namebuf), "%u", thisoid);
3992 : 88 : loinfo->dobj.name = pg_strdup(namebuf);
3993 : 88 : loinfo->dacl.acl = pg_strdup(thisacl);
3994 : 88 : loinfo->dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
3995 : 88 : loinfo->dacl.privtype = 0;
3996 : 88 : loinfo->dacl.initprivs = NULL;
3997 : 88 : loinfo->rolname = getRoleName(thisowner);
3998 : 88 : loinfo->numlos = n;
3999 : 88 : loinfo->looids[0] = thisoid;
4000 : : /* Collect OIDs of the remaining blobs in this group */
4001 [ + + ]: 102 : for (int k = 1; k < n; k++)
4002 : : {
4003 : : CatalogId extraID;
4004 : :
4005 : 14 : loinfo->looids[k] = atooid(PQgetvalue(res, i + k, i_oid));
4006 : :
4007 : : /* Make sure we can look up loinfo by any of the blobs' OIDs */
4008 : 14 : extraID.tableoid = LargeObjectRelationId;
4009 : 14 : extraID.oid = loinfo->looids[k];
4010 : 14 : recordAdditionalCatalogID(extraID, &loinfo->dobj);
4011 : : }
4012 : :
4013 : : /* LOs have data */
4014 : 88 : loinfo->dobj.components |= DUMP_COMPONENT_DATA;
4015 : :
4016 : : /* Mark whether LO group has a non-empty ACL */
4017 [ + + ]: 88 : if (!PQgetisnull(res, i, i_lomacl))
4018 : 36 : loinfo->dobj.components |= DUMP_COMPONENT_ACL;
4019 : :
4020 : : /*
4021 : : * In binary upgrade mode, pg_largeobject and pg_largeobject_metadata
4022 : : * are transferred via COPY or by copying/linking the files from the
4023 : : * old cluster. Thus, we do not need to dump LO data, definitions, or
4024 : : * ACLs.
4025 : : */
4026 [ + + ]: 88 : if (dopt->binary_upgrade)
4027 : 7 : loinfo->dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL | DUMP_COMPONENT_DEFINITION);
4028 : :
4029 : : /*
4030 : : * Create a "BLOBS" data item for the group, too. This is just a
4031 : : * placeholder for sorting; it carries no data now.
4032 : : */
4033 : 88 : lodata = pg_malloc_object(DumpableObject);
4034 : 88 : lodata->objType = DO_LARGE_OBJECT_DATA;
4035 : 88 : lodata->catId = nilCatalogId;
4036 : 88 : AssignDumpId(lodata);
4037 : 88 : lodata->name = pg_strdup(namebuf);
4038 : 88 : lodata->components |= DUMP_COMPONENT_DATA;
4039 : : /* Set up explicit dependency from data to metadata */
4040 : 88 : lodata->dependencies = pg_malloc_object(DumpId);
4041 : 88 : lodata->dependencies[0] = loinfo->dobj.dumpId;
4042 : 88 : lodata->nDeps = lodata->allocDeps = 1;
4043 : : }
4044 : :
4045 : 186 : PQclear(res);
4046 : 186 : destroyPQExpBuffer(loQry);
4047 : 186 : }
4048 : :
4049 : : /*
4050 : : * dumpLO
4051 : : *
4052 : : * dump the definition (metadata) of the given large object group
4053 : : */
4054 : : static void
4055 : 88 : dumpLO(Archive *fout, const LoInfo *loinfo)
4056 : : {
4057 : 88 : PQExpBuffer cquery = createPQExpBuffer();
4058 : :
4059 : : /*
4060 : : * The "definition" is just a newline-separated list of OIDs. We need to
4061 : : * put something into the dropStmt too, but it can just be a comment.
4062 : : */
4063 [ + + ]: 190 : for (int i = 0; i < loinfo->numlos; i++)
4064 : 102 : appendPQExpBuffer(cquery, "%u\n", loinfo->looids[i]);
4065 : :
4066 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4067 : 81 : ArchiveEntry(fout, loinfo->dobj.catId, loinfo->dobj.dumpId,
4068 : 81 : ARCHIVE_OPTS(.tag = loinfo->dobj.name,
4069 : : .owner = loinfo->rolname,
4070 : : .description = "BLOB METADATA",
4071 : : .section = SECTION_DATA,
4072 : : .createStmt = cquery->data,
4073 : : .dropStmt = "-- dummy"));
4074 : :
4075 : : /*
4076 : : * Dump per-blob comments and seclabels if any. We assume these are rare
4077 : : * enough that it's okay to generate retail TOC entries for them.
4078 : : */
4079 [ + + ]: 88 : if (loinfo->dobj.dump & (DUMP_COMPONENT_COMMENT |
4080 : : DUMP_COMPONENT_SECLABEL))
4081 : : {
4082 [ + + ]: 106 : for (int i = 0; i < loinfo->numlos; i++)
4083 : : {
4084 : : CatalogId catId;
4085 : : char namebuf[32];
4086 : :
4087 : : /* Build identifying info for this blob */
4088 : 60 : catId.tableoid = loinfo->dobj.catId.tableoid;
4089 : 60 : catId.oid = loinfo->looids[i];
4090 : 60 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[i]);
4091 : :
4092 [ + - ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4093 : 60 : dumpComment(fout, "LARGE OBJECT", namebuf,
4094 : 60 : NULL, loinfo->rolname,
4095 : 60 : catId, 0, loinfo->dobj.dumpId);
4096 : :
4097 [ + + ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4098 : 10 : dumpSecLabel(fout, "LARGE OBJECT", namebuf,
4099 : 10 : NULL, loinfo->rolname,
4100 : 10 : catId, 0, loinfo->dobj.dumpId);
4101 : : }
4102 : : }
4103 : :
4104 : : /*
4105 : : * Dump the ACLs if any (remember that all blobs in the group will have
4106 : : * the same ACL). If there's just one blob, dump a simple ACL entry; if
4107 : : * there's more, make a "LARGE OBJECTS" entry that really contains only
4108 : : * the ACL for the first blob. _printTocEntry() will be cued by the tag
4109 : : * string to emit a mutated version for each blob.
4110 : : */
4111 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_ACL)
4112 : : {
4113 : : char namebuf[32];
4114 : :
4115 : : /* Build identifying info for the first blob */
4116 : 35 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[0]);
4117 : :
4118 [ - + ]: 35 : if (loinfo->numlos > 1)
4119 : : {
4120 : : char tagbuf[64];
4121 : :
4122 : 0 : snprintf(tagbuf, sizeof(tagbuf), "LARGE OBJECTS %u..%u",
4123 : 0 : loinfo->looids[0], loinfo->looids[loinfo->numlos - 1]);
4124 : :
4125 : 0 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4126 : : "LARGE OBJECT", namebuf, NULL, NULL,
4127 : 0 : tagbuf, loinfo->rolname, &loinfo->dacl);
4128 : : }
4129 : : else
4130 : : {
4131 : 35 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4132 : : "LARGE OBJECT", namebuf, NULL, NULL,
4133 : 35 : NULL, loinfo->rolname, &loinfo->dacl);
4134 : : }
4135 : : }
4136 : :
4137 : 88 : destroyPQExpBuffer(cquery);
4138 : 88 : }
4139 : :
4140 : : /*
4141 : : * dumpLOs:
4142 : : * dump the data contents of the large objects in the given group
4143 : : */
4144 : : static int
4145 : 77 : dumpLOs(Archive *fout, const void *arg)
4146 : : {
4147 : 77 : const LoInfo *loinfo = (const LoInfo *) arg;
4148 : 77 : PGconn *conn = GetConnection(fout);
4149 : : char buf[LOBBUFSIZE];
4150 : :
4151 : 77 : pg_log_info("saving large objects \"%s\"", loinfo->dobj.name);
4152 : :
4153 [ + + ]: 162 : for (int i = 0; i < loinfo->numlos; i++)
4154 : : {
4155 : 85 : Oid loOid = loinfo->looids[i];
4156 : : int loFd;
4157 : : int cnt;
4158 : :
4159 : : /* Open the LO */
4160 : 85 : loFd = lo_open(conn, loOid, INV_READ);
4161 [ - + ]: 85 : if (loFd == -1)
4162 : 0 : pg_fatal("could not open large object %u: %s",
4163 : : loOid, PQerrorMessage(conn));
4164 : :
4165 : 85 : StartLO(fout, loOid);
4166 : :
4167 : : /* Now read it in chunks, sending data to archive */
4168 : : do
4169 : : {
4170 : 133 : cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
4171 [ - + ]: 133 : if (cnt < 0)
4172 : 0 : pg_fatal("error reading large object %u: %s",
4173 : : loOid, PQerrorMessage(conn));
4174 : :
4175 : 133 : WriteData(fout, buf, cnt);
4176 [ + + ]: 133 : } while (cnt > 0);
4177 : :
4178 : 85 : lo_close(conn, loFd);
4179 : :
4180 : 85 : EndLO(fout, loOid);
4181 : : }
4182 : :
4183 : 77 : return 1;
4184 : : }
4185 : :
4186 : : /*
4187 : : * getPolicies
4188 : : * get information about all RLS policies on dumpable tables.
4189 : : */
4190 : : void
4191 : 215 : getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
4192 : : {
4193 : 215 : DumpOptions *dopt = fout->dopt;
4194 : : PQExpBuffer query;
4195 : : PQExpBuffer tbloids;
4196 : : PGresult *res;
4197 : : PolicyInfo *polinfo;
4198 : : int i_oid;
4199 : : int i_tableoid;
4200 : : int i_polrelid;
4201 : : int i_polname;
4202 : : int i_polcmd;
4203 : : int i_polpermissive;
4204 : : int i_polroles;
4205 : : int i_polqual;
4206 : : int i_polwithcheck;
4207 : : int i,
4208 : : j,
4209 : : ntups;
4210 : :
4211 : : /* Skip if --no-policies was specified */
4212 [ + + ]: 215 : if (dopt->no_policies)
4213 : 1 : return;
4214 : :
4215 : 214 : query = createPQExpBuffer();
4216 : 214 : tbloids = createPQExpBuffer();
4217 : :
4218 : : /*
4219 : : * Identify tables of interest, and check which ones have RLS enabled.
4220 : : */
4221 : 214 : appendPQExpBufferChar(tbloids, '{');
4222 [ + + ]: 57309 : for (i = 0; i < numTables; i++)
4223 : : {
4224 : 57095 : TableInfo *tbinfo = &tblinfo[i];
4225 : :
4226 : : /* Ignore row security on tables not to be dumped */
4227 [ + + ]: 57095 : if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
4228 : 49536 : continue;
4229 : :
4230 : : /* It can't have RLS or policies if it's not a table */
4231 [ + + ]: 7559 : if (tbinfo->relkind != RELKIND_RELATION &&
4232 [ + + ]: 2095 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
4233 : 1451 : continue;
4234 : :
4235 : : /* Add it to the list of table OIDs to be probed below */
4236 [ + + ]: 6108 : if (tbloids->len > 1) /* do we have more than the '{'? */
4237 : 5966 : appendPQExpBufferChar(tbloids, ',');
4238 : 6108 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
4239 : :
4240 : : /* Is RLS enabled? (That's separate from whether it has policies) */
4241 [ + + ]: 6108 : if (tbinfo->rowsec)
4242 : : {
4243 : 56 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4244 : :
4245 : : /*
4246 : : * We represent RLS being enabled on a table by creating a
4247 : : * PolicyInfo object with null polname.
4248 : : *
4249 : : * Note: use tableoid 0 so that this object won't be mistaken for
4250 : : * something that pg_depend entries apply to.
4251 : : */
4252 : 56 : polinfo = pg_malloc_object(PolicyInfo);
4253 : 56 : polinfo->dobj.objType = DO_POLICY;
4254 : 56 : polinfo->dobj.catId.tableoid = 0;
4255 : 56 : polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
4256 : 56 : AssignDumpId(&polinfo->dobj);
4257 : 56 : polinfo->dobj.namespace = tbinfo->dobj.namespace;
4258 : 56 : polinfo->dobj.name = pg_strdup(tbinfo->dobj.name);
4259 : 56 : polinfo->poltable = tbinfo;
4260 : 56 : polinfo->polname = NULL;
4261 : 56 : polinfo->polcmd = '\0';
4262 : 56 : polinfo->polpermissive = 0;
4263 : 56 : polinfo->polroles = NULL;
4264 : 56 : polinfo->polqual = NULL;
4265 : 56 : polinfo->polwithcheck = NULL;
4266 : : }
4267 : : }
4268 : 214 : appendPQExpBufferChar(tbloids, '}');
4269 : :
4270 : : /*
4271 : : * Now, read all RLS policies belonging to the tables of interest, and
4272 : : * create PolicyInfo objects for them. (Note that we must filter the
4273 : : * results server-side not locally, because we dare not apply pg_get_expr
4274 : : * to tables we don't have lock on.)
4275 : : */
4276 : 214 : pg_log_info("reading row-level security policies");
4277 : :
4278 : 214 : printfPQExpBuffer(query,
4279 : : "SELECT pol.oid, pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
4280 : 214 : appendPQExpBufferStr(query, "pol.polpermissive, ");
4281 : 214 : appendPQExpBuffer(query,
4282 : : "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
4283 : : " pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
4284 : : "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
4285 : : "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
4286 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
4287 : : "JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)",
4288 : : tbloids->data);
4289 : :
4290 : 214 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4291 : :
4292 : 214 : ntups = PQntuples(res);
4293 [ + + ]: 214 : if (ntups > 0)
4294 : : {
4295 : 46 : i_oid = PQfnumber(res, "oid");
4296 : 46 : i_tableoid = PQfnumber(res, "tableoid");
4297 : 46 : i_polrelid = PQfnumber(res, "polrelid");
4298 : 46 : i_polname = PQfnumber(res, "polname");
4299 : 46 : i_polcmd = PQfnumber(res, "polcmd");
4300 : 46 : i_polpermissive = PQfnumber(res, "polpermissive");
4301 : 46 : i_polroles = PQfnumber(res, "polroles");
4302 : 46 : i_polqual = PQfnumber(res, "polqual");
4303 : 46 : i_polwithcheck = PQfnumber(res, "polwithcheck");
4304 : :
4305 : 46 : polinfo = pg_malloc_array(PolicyInfo, ntups);
4306 : :
4307 [ + + ]: 337 : for (j = 0; j < ntups; j++)
4308 : : {
4309 : 291 : Oid polrelid = atooid(PQgetvalue(res, j, i_polrelid));
4310 : 291 : TableInfo *tbinfo = findTableByOid(polrelid);
4311 : :
4312 : 291 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4313 : :
4314 : 291 : polinfo[j].dobj.objType = DO_POLICY;
4315 : 291 : polinfo[j].dobj.catId.tableoid =
4316 : 291 : atooid(PQgetvalue(res, j, i_tableoid));
4317 : 291 : polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
4318 : 291 : AssignDumpId(&polinfo[j].dobj);
4319 : 291 : polinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4320 : 291 : polinfo[j].poltable = tbinfo;
4321 : 291 : polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname));
4322 : 291 : polinfo[j].dobj.name = pg_strdup(polinfo[j].polname);
4323 : :
4324 : 291 : polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd));
4325 : 291 : polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't';
4326 : :
4327 [ + + ]: 291 : if (PQgetisnull(res, j, i_polroles))
4328 : 127 : polinfo[j].polroles = NULL;
4329 : : else
4330 : 164 : polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles));
4331 : :
4332 [ + + ]: 291 : if (PQgetisnull(res, j, i_polqual))
4333 : 41 : polinfo[j].polqual = NULL;
4334 : : else
4335 : 250 : polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual));
4336 : :
4337 [ + + ]: 291 : if (PQgetisnull(res, j, i_polwithcheck))
4338 : 153 : polinfo[j].polwithcheck = NULL;
4339 : : else
4340 : 138 : polinfo[j].polwithcheck
4341 : 138 : = pg_strdup(PQgetvalue(res, j, i_polwithcheck));
4342 : : }
4343 : : }
4344 : :
4345 : 214 : PQclear(res);
4346 : :
4347 : 214 : destroyPQExpBuffer(query);
4348 : 214 : destroyPQExpBuffer(tbloids);
4349 : : }
4350 : :
4351 : : /*
4352 : : * dumpPolicy
4353 : : * dump the definition of the given policy
4354 : : */
4355 : : static void
4356 : 347 : dumpPolicy(Archive *fout, const PolicyInfo *polinfo)
4357 : : {
4358 : 347 : DumpOptions *dopt = fout->dopt;
4359 : 347 : TableInfo *tbinfo = polinfo->poltable;
4360 : : PQExpBuffer query;
4361 : : PQExpBuffer delqry;
4362 : : PQExpBuffer polprefix;
4363 : : char *qtabname;
4364 : : const char *cmd;
4365 : : char *tag;
4366 : :
4367 : : /* Do nothing if not dumping schema */
4368 [ + + ]: 347 : if (!dopt->dumpSchema)
4369 : 56 : return;
4370 : :
4371 : : /*
4372 : : * If polname is NULL, then this record is just indicating that ROW LEVEL
4373 : : * SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE
4374 : : * ROW LEVEL SECURITY.
4375 : : */
4376 [ + + ]: 291 : if (polinfo->polname == NULL)
4377 : : {
4378 : 48 : query = createPQExpBuffer();
4379 : :
4380 : 48 : appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;",
4381 : 48 : fmtQualifiedDumpable(tbinfo));
4382 : :
4383 : : /*
4384 : : * We must emit the ROW SECURITY object's dependency on its table
4385 : : * explicitly, because it will not match anything in pg_depend (unlike
4386 : : * the case for other PolicyInfo objects).
4387 : : */
4388 [ + - ]: 48 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4389 : 48 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4390 : 48 : ARCHIVE_OPTS(.tag = polinfo->dobj.name,
4391 : : .namespace = polinfo->dobj.namespace->dobj.name,
4392 : : .owner = tbinfo->rolname,
4393 : : .description = "ROW SECURITY",
4394 : : .section = SECTION_POST_DATA,
4395 : : .createStmt = query->data,
4396 : : .deps = &(tbinfo->dobj.dumpId),
4397 : : .nDeps = 1));
4398 : :
4399 : 48 : destroyPQExpBuffer(query);
4400 : 48 : return;
4401 : : }
4402 : :
4403 [ + + ]: 243 : if (polinfo->polcmd == '*')
4404 : 81 : cmd = "";
4405 [ + + ]: 162 : else if (polinfo->polcmd == 'r')
4406 : 43 : cmd = " FOR SELECT";
4407 [ + + ]: 119 : else if (polinfo->polcmd == 'a')
4408 : 33 : cmd = " FOR INSERT";
4409 [ + + ]: 86 : else if (polinfo->polcmd == 'w')
4410 : 43 : cmd = " FOR UPDATE";
4411 [ + - ]: 43 : else if (polinfo->polcmd == 'd')
4412 : 43 : cmd = " FOR DELETE";
4413 : : else
4414 : 0 : pg_fatal("unexpected policy command type: %c",
4415 : : polinfo->polcmd);
4416 : :
4417 : 243 : query = createPQExpBuffer();
4418 : 243 : delqry = createPQExpBuffer();
4419 : 243 : polprefix = createPQExpBuffer();
4420 : :
4421 : 243 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
4422 : :
4423 : 243 : appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname));
4424 : :
4425 : 243 : appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo),
4426 [ + + ]: 243 : !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd);
4427 : :
4428 [ + + ]: 243 : if (polinfo->polroles != NULL)
4429 : 132 : appendPQExpBuffer(query, " TO %s", polinfo->polroles);
4430 : :
4431 [ + + ]: 243 : if (polinfo->polqual != NULL)
4432 : 210 : appendPQExpBuffer(query, " USING (%s)", polinfo->polqual);
4433 : :
4434 [ + + ]: 243 : if (polinfo->polwithcheck != NULL)
4435 : 114 : appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck);
4436 : :
4437 : 243 : appendPQExpBufferStr(query, ";\n");
4438 : :
4439 : 243 : appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname));
4440 : 243 : appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo));
4441 : :
4442 : 243 : appendPQExpBuffer(polprefix, "POLICY %s ON",
4443 : 243 : fmtId(polinfo->polname));
4444 : :
4445 : 243 : tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
4446 : :
4447 [ + - ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4448 : 243 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4449 : 243 : ARCHIVE_OPTS(.tag = tag,
4450 : : .namespace = polinfo->dobj.namespace->dobj.name,
4451 : : .owner = tbinfo->rolname,
4452 : : .description = "POLICY",
4453 : : .section = SECTION_POST_DATA,
4454 : : .createStmt = query->data,
4455 : : .dropStmt = delqry->data));
4456 : :
4457 [ + + ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4458 : 33 : dumpComment(fout, polprefix->data, qtabname,
4459 : 33 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
4460 : 33 : polinfo->dobj.catId, 0, polinfo->dobj.dumpId);
4461 : :
4462 : 243 : pfree(tag);
4463 : 243 : destroyPQExpBuffer(query);
4464 : 243 : destroyPQExpBuffer(delqry);
4465 : 243 : destroyPQExpBuffer(polprefix);
4466 : 243 : pg_free(qtabname);
4467 : : }
4468 : :
4469 : : /*
4470 : : * getPublications
4471 : : * get information about publications
4472 : : */
4473 : : void
4474 : 215 : getPublications(Archive *fout)
4475 : : {
4476 : 215 : DumpOptions *dopt = fout->dopt;
4477 : : PQExpBuffer query;
4478 : : PGresult *res;
4479 : : PublicationInfo *pubinfo;
4480 : : int i_tableoid;
4481 : : int i_oid;
4482 : : int i_pubname;
4483 : : int i_pubowner;
4484 : : int i_puballtables;
4485 : : int i_puballsequences;
4486 : : int i_pubinsert;
4487 : : int i_pubupdate;
4488 : : int i_pubdelete;
4489 : : int i_pubtruncate;
4490 : : int i_pubviaroot;
4491 : : int i_pubgencols;
4492 : : int i,
4493 : : ntups;
4494 : :
4495 [ - + ]: 215 : if (dopt->no_publications)
4496 : 0 : return;
4497 : :
4498 : 215 : query = createPQExpBuffer();
4499 : :
4500 : : /* Get the publications. */
4501 : 215 : appendPQExpBufferStr(query, "SELECT p.tableoid, p.oid, p.pubname, "
4502 : : "p.pubowner, p.puballtables, p.pubinsert, "
4503 : : "p.pubupdate, p.pubdelete, ");
4504 : :
4505 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
4506 : 215 : appendPQExpBufferStr(query, "p.pubtruncate, ");
4507 : : else
4508 : 0 : appendPQExpBufferStr(query, "false AS pubtruncate, ");
4509 : :
4510 [ + - ]: 215 : if (fout->remoteVersion >= 130000)
4511 : 215 : appendPQExpBufferStr(query, "p.pubviaroot, ");
4512 : : else
4513 : 0 : appendPQExpBufferStr(query, "false AS pubviaroot, ");
4514 : :
4515 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
4516 : 215 : appendPQExpBufferStr(query, "p.pubgencols, ");
4517 : : else
4518 : 0 : appendPQExpBuffer(query, "'%c' AS pubgencols, ", PUBLISH_GENCOLS_NONE);
4519 : :
4520 [ + - ]: 215 : if (fout->remoteVersion >= 190000)
4521 : 215 : appendPQExpBufferStr(query, "p.puballsequences ");
4522 : : else
4523 : 0 : appendPQExpBufferStr(query, "false AS puballsequences ");
4524 : :
4525 : 215 : appendPQExpBufferStr(query, "FROM pg_publication p");
4526 : :
4527 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4528 : :
4529 : 215 : ntups = PQntuples(res);
4530 : :
4531 [ + + ]: 215 : if (ntups == 0)
4532 : 159 : goto cleanup;
4533 : :
4534 : 56 : i_tableoid = PQfnumber(res, "tableoid");
4535 : 56 : i_oid = PQfnumber(res, "oid");
4536 : 56 : i_pubname = PQfnumber(res, "pubname");
4537 : 56 : i_pubowner = PQfnumber(res, "pubowner");
4538 : 56 : i_puballtables = PQfnumber(res, "puballtables");
4539 : 56 : i_puballsequences = PQfnumber(res, "puballsequences");
4540 : 56 : i_pubinsert = PQfnumber(res, "pubinsert");
4541 : 56 : i_pubupdate = PQfnumber(res, "pubupdate");
4542 : 56 : i_pubdelete = PQfnumber(res, "pubdelete");
4543 : 56 : i_pubtruncate = PQfnumber(res, "pubtruncate");
4544 : 56 : i_pubviaroot = PQfnumber(res, "pubviaroot");
4545 : 56 : i_pubgencols = PQfnumber(res, "pubgencols");
4546 : :
4547 : 56 : pubinfo = pg_malloc_array(PublicationInfo, ntups);
4548 : :
4549 [ + + ]: 572 : for (i = 0; i < ntups; i++)
4550 : : {
4551 : 516 : pubinfo[i].dobj.objType = DO_PUBLICATION;
4552 : 516 : pubinfo[i].dobj.catId.tableoid =
4553 : 516 : atooid(PQgetvalue(res, i, i_tableoid));
4554 : 516 : pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4555 : 516 : AssignDumpId(&pubinfo[i].dobj);
4556 : 516 : pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname));
4557 : 516 : pubinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_pubowner));
4558 : 516 : pubinfo[i].puballtables =
4559 : 516 : (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0);
4560 : 516 : pubinfo[i].puballsequences =
4561 : 516 : (strcmp(PQgetvalue(res, i, i_puballsequences), "t") == 0);
4562 : 516 : pubinfo[i].pubinsert =
4563 : 516 : (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0);
4564 : 516 : pubinfo[i].pubupdate =
4565 : 516 : (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0);
4566 : 516 : pubinfo[i].pubdelete =
4567 : 516 : (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
4568 : 516 : pubinfo[i].pubtruncate =
4569 : 516 : (strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0);
4570 : 516 : pubinfo[i].pubviaroot =
4571 : 516 : (strcmp(PQgetvalue(res, i, i_pubviaroot), "t") == 0);
4572 : 516 : pubinfo[i].pubgencols_type =
4573 : 516 : *(PQgetvalue(res, i, i_pubgencols));
4574 : 516 : pubinfo[i].except_tables = (SimplePtrList)
4575 : : {
4576 : : NULL, NULL
4577 : : };
4578 : :
4579 : : /* Decide whether we want to dump it */
4580 : 516 : selectDumpableObject(&(pubinfo[i].dobj), fout);
4581 : :
4582 : : /*
4583 : : * Get the list of tables for publications specified in the EXCEPT
4584 : : * TABLE clause.
4585 : : *
4586 : : * Although individual table entries in EXCEPT list could be stored in
4587 : : * PublicationRelInfo, dumpPublicationTable cannot be used to emit
4588 : : * them, because there is no ALTER PUBLICATION ... ADD command to add
4589 : : * individual table entries to the EXCEPT list.
4590 : : *
4591 : : * Therefore, the approach is to dump the complete EXCEPT list in a
4592 : : * single CREATE PUBLICATION statement. PublicationInfo is used to
4593 : : * collect this information, which is then emitted by
4594 : : * dumpPublication().
4595 : : */
4596 [ + - ]: 516 : if (fout->remoteVersion >= 190000)
4597 : : {
4598 : : int ntbls;
4599 : : PGresult *res_tbls;
4600 : :
4601 : 516 : resetPQExpBuffer(query);
4602 : 516 : appendPQExpBuffer(query,
4603 : : "SELECT prrelid\n"
4604 : : "FROM pg_catalog.pg_publication_rel\n"
4605 : : "WHERE prpubid = %u AND prexcept",
4606 : 516 : pubinfo[i].dobj.catId.oid);
4607 : :
4608 : 516 : res_tbls = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4609 : :
4610 : 516 : ntbls = PQntuples(res_tbls);
4611 : :
4612 [ + + ]: 756 : for (int j = 0; j < ntbls; j++)
4613 : : {
4614 : : Oid prrelid;
4615 : : TableInfo *tbinfo;
4616 : :
4617 : 240 : prrelid = atooid(PQgetvalue(res_tbls, j, 0));
4618 : :
4619 : 240 : tbinfo = findTableByOid(prrelid);
4620 : :
4621 [ + - ]: 240 : if (tbinfo != NULL)
4622 : 240 : simple_ptr_list_append(&pubinfo[i].except_tables, tbinfo);
4623 : : }
4624 : :
4625 : 516 : PQclear(res_tbls);
4626 : : }
4627 : : }
4628 : :
4629 : 56 : cleanup:
4630 : 215 : PQclear(res);
4631 : :
4632 : 215 : destroyPQExpBuffer(query);
4633 : : }
4634 : :
4635 : : /*
4636 : : * dumpPublication
4637 : : * dump the definition of the given publication
4638 : : */
4639 : : static void
4640 : 416 : dumpPublication(Archive *fout, const PublicationInfo *pubinfo)
4641 : : {
4642 : 416 : DumpOptions *dopt = fout->dopt;
4643 : : PQExpBuffer delq;
4644 : : PQExpBuffer query;
4645 : : char *qpubname;
4646 : 416 : bool first = true;
4647 : :
4648 : : /* Do nothing if not dumping schema */
4649 [ + + ]: 416 : if (!dopt->dumpSchema)
4650 : 60 : return;
4651 : :
4652 : 356 : delq = createPQExpBuffer();
4653 : 356 : query = createPQExpBuffer();
4654 : :
4655 : 356 : qpubname = pg_strdup(fmtId(pubinfo->dobj.name));
4656 : :
4657 : 356 : appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n",
4658 : : qpubname);
4659 : :
4660 : 356 : appendPQExpBuffer(query, "CREATE PUBLICATION %s",
4661 : : qpubname);
4662 : :
4663 [ + + ]: 356 : if (pubinfo->puballtables)
4664 : : {
4665 : 166 : int n_except = 0;
4666 : :
4667 : 166 : appendPQExpBufferStr(query, " FOR ALL TABLES");
4668 : :
4669 : : /* Include EXCEPT (TABLE) clause if there are except_tables. */
4670 [ + + ]: 331 : for (SimplePtrListCell *cell = pubinfo->except_tables.head; cell; cell = cell->next)
4671 : : {
4672 : 165 : TableInfo *tbinfo = (TableInfo *) cell->ptr;
4673 : :
4674 [ + + ]: 165 : if (++n_except == 1)
4675 : 99 : appendPQExpBufferStr(query, " EXCEPT (");
4676 : : else
4677 : 66 : appendPQExpBufferStr(query, ", ");
4678 : 165 : appendPQExpBuffer(query, "TABLE ONLY %s", fmtQualifiedDumpable(tbinfo));
4679 : : }
4680 [ + + ]: 166 : if (n_except > 0)
4681 : 99 : appendPQExpBufferChar(query, ')');
4682 : :
4683 [ + + ]: 166 : if (pubinfo->puballsequences)
4684 : 33 : appendPQExpBufferStr(query, ", ALL SEQUENCES");
4685 : : }
4686 [ + + ]: 190 : else if (pubinfo->puballsequences)
4687 : 33 : appendPQExpBufferStr(query, " FOR ALL SEQUENCES");
4688 : :
4689 : 356 : appendPQExpBufferStr(query, " WITH (publish = '");
4690 [ + + ]: 356 : if (pubinfo->pubinsert)
4691 : : {
4692 : 290 : appendPQExpBufferStr(query, "insert");
4693 : 290 : first = false;
4694 : : }
4695 : :
4696 [ + + ]: 356 : if (pubinfo->pubupdate)
4697 : : {
4698 [ + - ]: 290 : if (!first)
4699 : 290 : appendPQExpBufferStr(query, ", ");
4700 : :
4701 : 290 : appendPQExpBufferStr(query, "update");
4702 : 290 : first = false;
4703 : : }
4704 : :
4705 [ + + ]: 356 : if (pubinfo->pubdelete)
4706 : : {
4707 [ + - ]: 290 : if (!first)
4708 : 290 : appendPQExpBufferStr(query, ", ");
4709 : :
4710 : 290 : appendPQExpBufferStr(query, "delete");
4711 : 290 : first = false;
4712 : : }
4713 : :
4714 [ + + ]: 356 : if (pubinfo->pubtruncate)
4715 : : {
4716 [ + - ]: 290 : if (!first)
4717 : 290 : appendPQExpBufferStr(query, ", ");
4718 : :
4719 : 290 : appendPQExpBufferStr(query, "truncate");
4720 : 290 : first = false;
4721 : : }
4722 : :
4723 : 356 : appendPQExpBufferChar(query, '\'');
4724 : :
4725 [ + + ]: 356 : if (pubinfo->pubviaroot)
4726 : 5 : appendPQExpBufferStr(query, ", publish_via_partition_root = true");
4727 : :
4728 [ + + ]: 356 : if (pubinfo->pubgencols_type == PUBLISH_GENCOLS_STORED)
4729 : 33 : appendPQExpBufferStr(query, ", publish_generated_columns = stored");
4730 : :
4731 : 356 : appendPQExpBufferStr(query, ");\n");
4732 : :
4733 [ + - ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4734 : 356 : ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
4735 : 356 : ARCHIVE_OPTS(.tag = pubinfo->dobj.name,
4736 : : .owner = pubinfo->rolname,
4737 : : .description = "PUBLICATION",
4738 : : .section = SECTION_POST_DATA,
4739 : : .createStmt = query->data,
4740 : : .dropStmt = delq->data));
4741 : :
4742 [ + + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4743 : 33 : dumpComment(fout, "PUBLICATION", qpubname,
4744 : 33 : NULL, pubinfo->rolname,
4745 : 33 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4746 : :
4747 [ - + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4748 : 0 : dumpSecLabel(fout, "PUBLICATION", qpubname,
4749 : 0 : NULL, pubinfo->rolname,
4750 : 0 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4751 : :
4752 : 356 : destroyPQExpBuffer(delq);
4753 : 356 : destroyPQExpBuffer(query);
4754 : 356 : pg_free(qpubname);
4755 : : }
4756 : :
4757 : : /*
4758 : : * getPublicationNamespaces
4759 : : * get information about publication membership for dumpable schemas.
4760 : : */
4761 : : void
4762 : 215 : getPublicationNamespaces(Archive *fout)
4763 : : {
4764 : : PQExpBuffer query;
4765 : : PGresult *res;
4766 : : PublicationSchemaInfo *pubsinfo;
4767 : 215 : DumpOptions *dopt = fout->dopt;
4768 : : int i_tableoid;
4769 : : int i_oid;
4770 : : int i_pnpubid;
4771 : : int i_pnnspid;
4772 : : int i,
4773 : : j,
4774 : : ntups;
4775 : :
4776 [ + - - + ]: 215 : if (dopt->no_publications || fout->remoteVersion < 150000)
4777 : 0 : return;
4778 : :
4779 : 215 : query = createPQExpBuffer();
4780 : :
4781 : : /* Collect all publication membership info. */
4782 : 215 : appendPQExpBufferStr(query,
4783 : : "SELECT tableoid, oid, pnpubid, pnnspid "
4784 : : "FROM pg_catalog.pg_publication_namespace");
4785 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4786 : :
4787 : 215 : ntups = PQntuples(res);
4788 : :
4789 : 215 : i_tableoid = PQfnumber(res, "tableoid");
4790 : 215 : i_oid = PQfnumber(res, "oid");
4791 : 215 : i_pnpubid = PQfnumber(res, "pnpubid");
4792 : 215 : i_pnnspid = PQfnumber(res, "pnnspid");
4793 : :
4794 : : /* this allocation may be more than we need */
4795 : 215 : pubsinfo = pg_malloc_array(PublicationSchemaInfo, ntups);
4796 : 215 : j = 0;
4797 : :
4798 [ + + ]: 346 : for (i = 0; i < ntups; i++)
4799 : : {
4800 : 131 : Oid pnpubid = atooid(PQgetvalue(res, i, i_pnpubid));
4801 : 131 : Oid pnnspid = atooid(PQgetvalue(res, i, i_pnnspid));
4802 : : PublicationInfo *pubinfo;
4803 : : NamespaceInfo *nspinfo;
4804 : :
4805 : : /*
4806 : : * Ignore any entries for which we aren't interested in either the
4807 : : * publication or the rel.
4808 : : */
4809 : 131 : pubinfo = findPublicationByOid(pnpubid);
4810 [ - + ]: 131 : if (pubinfo == NULL)
4811 : 0 : continue;
4812 : 131 : nspinfo = findNamespaceByOid(pnnspid);
4813 [ - + ]: 131 : if (nspinfo == NULL)
4814 : 0 : continue;
4815 : :
4816 : : /* OK, make a DumpableObject for this relationship */
4817 : 131 : pubsinfo[j].dobj.objType = DO_PUBLICATION_TABLE_IN_SCHEMA;
4818 : 131 : pubsinfo[j].dobj.catId.tableoid =
4819 : 131 : atooid(PQgetvalue(res, i, i_tableoid));
4820 : 131 : pubsinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4821 : 131 : AssignDumpId(&pubsinfo[j].dobj);
4822 : 131 : pubsinfo[j].dobj.namespace = nspinfo->dobj.namespace;
4823 : 131 : pubsinfo[j].dobj.name = nspinfo->dobj.name;
4824 : 131 : pubsinfo[j].publication = pubinfo;
4825 : 131 : pubsinfo[j].pubschema = nspinfo;
4826 : :
4827 : : /* Decide whether we want to dump it */
4828 : 131 : selectDumpablePublicationObject(&(pubsinfo[j].dobj), fout);
4829 : :
4830 : 131 : j++;
4831 : : }
4832 : :
4833 : 215 : PQclear(res);
4834 : 215 : destroyPQExpBuffer(query);
4835 : : }
4836 : :
4837 : : /*
4838 : : * getPublicationTables
4839 : : * get information about publication membership for dumpable tables.
4840 : : */
4841 : : void
4842 : 215 : getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
4843 : : {
4844 : : PQExpBuffer query;
4845 : : PGresult *res;
4846 : : PublicationRelInfo *pubrinfo;
4847 : 215 : DumpOptions *dopt = fout->dopt;
4848 : : int i_tableoid;
4849 : : int i_oid;
4850 : : int i_prpubid;
4851 : : int i_prrelid;
4852 : : int i_prrelqual;
4853 : : int i_prattrs;
4854 : : int i,
4855 : : j,
4856 : : ntups;
4857 : :
4858 [ - + ]: 215 : if (dopt->no_publications)
4859 : 0 : return;
4860 : :
4861 : 215 : query = createPQExpBuffer();
4862 : :
4863 : : /* Collect all publication membership info. */
4864 [ + - ]: 215 : if (fout->remoteVersion >= 150000)
4865 : : {
4866 : 215 : appendPQExpBufferStr(query,
4867 : : "SELECT tableoid, oid, prpubid, prrelid, "
4868 : : "pg_catalog.pg_get_expr(prqual, prrelid) AS prrelqual, "
4869 : : "(CASE\n"
4870 : : " WHEN pr.prattrs IS NOT NULL THEN\n"
4871 : : " (SELECT array_agg(attname)\n"
4872 : : " FROM\n"
4873 : : " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
4874 : : " pg_catalog.pg_attribute\n"
4875 : : " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
4876 : : " ELSE NULL END) prattrs "
4877 : : "FROM pg_catalog.pg_publication_rel pr");
4878 [ + - ]: 215 : if (fout->remoteVersion >= 190000)
4879 : 215 : appendPQExpBufferStr(query, " WHERE NOT pr.prexcept");
4880 : : }
4881 : : else
4882 : 0 : appendPQExpBufferStr(query,
4883 : : "SELECT tableoid, oid, prpubid, prrelid, "
4884 : : "NULL AS prrelqual, NULL AS prattrs "
4885 : : "FROM pg_catalog.pg_publication_rel");
4886 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4887 : :
4888 : 215 : ntups = PQntuples(res);
4889 : :
4890 : 215 : i_tableoid = PQfnumber(res, "tableoid");
4891 : 215 : i_oid = PQfnumber(res, "oid");
4892 : 215 : i_prpubid = PQfnumber(res, "prpubid");
4893 : 215 : i_prrelid = PQfnumber(res, "prrelid");
4894 : 215 : i_prrelqual = PQfnumber(res, "prrelqual");
4895 : 215 : i_prattrs = PQfnumber(res, "prattrs");
4896 : :
4897 : : /* this allocation may be more than we need */
4898 : 215 : pubrinfo = pg_malloc_array(PublicationRelInfo, ntups);
4899 : 215 : j = 0;
4900 : :
4901 [ + + ]: 586 : for (i = 0; i < ntups; i++)
4902 : : {
4903 : 371 : Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid));
4904 : 371 : Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid));
4905 : : PublicationInfo *pubinfo;
4906 : : TableInfo *tbinfo;
4907 : :
4908 : : /*
4909 : : * Ignore any entries for which we aren't interested in either the
4910 : : * publication or the rel.
4911 : : */
4912 : 371 : pubinfo = findPublicationByOid(prpubid);
4913 [ - + ]: 371 : if (pubinfo == NULL)
4914 : 0 : continue;
4915 : 371 : tbinfo = findTableByOid(prrelid);
4916 [ - + ]: 371 : if (tbinfo == NULL)
4917 : 0 : continue;
4918 : :
4919 : : /* OK, make a DumpableObject for this relationship */
4920 : 371 : pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
4921 : 371 : pubrinfo[j].dobj.catId.tableoid =
4922 : 371 : atooid(PQgetvalue(res, i, i_tableoid));
4923 : 371 : pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4924 : 371 : AssignDumpId(&pubrinfo[j].dobj);
4925 : 371 : pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4926 : 371 : pubrinfo[j].dobj.name = tbinfo->dobj.name;
4927 : 371 : pubrinfo[j].publication = pubinfo;
4928 : 371 : pubrinfo[j].pubtable = tbinfo;
4929 [ + + ]: 371 : if (PQgetisnull(res, i, i_prrelqual))
4930 : 206 : pubrinfo[j].pubrelqual = NULL;
4931 : : else
4932 : 165 : pubrinfo[j].pubrelqual = pg_strdup(PQgetvalue(res, i, i_prrelqual));
4933 : :
4934 [ + + ]: 371 : if (!PQgetisnull(res, i, i_prattrs))
4935 : : {
4936 : : char **attnames;
4937 : : int nattnames;
4938 : : PQExpBuffer attribs;
4939 : :
4940 [ - + ]: 117 : if (!parsePGArray(PQgetvalue(res, i, i_prattrs),
4941 : : &attnames, &nattnames))
4942 : 0 : pg_fatal("could not parse %s array", "prattrs");
4943 : 117 : attribs = createPQExpBuffer();
4944 [ + + ]: 337 : for (int k = 0; k < nattnames; k++)
4945 : : {
4946 [ + + ]: 220 : if (k > 0)
4947 : 103 : appendPQExpBufferStr(attribs, ", ");
4948 : :
4949 : 220 : appendPQExpBufferStr(attribs, fmtId(attnames[k]));
4950 : : }
4951 : 117 : pubrinfo[j].pubrattrs = attribs->data;
4952 : 117 : free(attribs); /* but not attribs->data */
4953 : 117 : free(attnames);
4954 : : }
4955 : : else
4956 : 254 : pubrinfo[j].pubrattrs = NULL;
4957 : :
4958 : : /* Decide whether we want to dump it */
4959 : 371 : selectDumpablePublicationObject(&(pubrinfo[j].dobj), fout);
4960 : :
4961 : 371 : j++;
4962 : : }
4963 : :
4964 : 215 : PQclear(res);
4965 : 215 : destroyPQExpBuffer(query);
4966 : : }
4967 : :
4968 : : /*
4969 : : * dumpPublicationNamespace
4970 : : * dump the definition of the given publication schema mapping.
4971 : : */
4972 : : static void
4973 : 103 : dumpPublicationNamespace(Archive *fout, const PublicationSchemaInfo *pubsinfo)
4974 : : {
4975 : 103 : DumpOptions *dopt = fout->dopt;
4976 : 103 : NamespaceInfo *schemainfo = pubsinfo->pubschema;
4977 : 103 : PublicationInfo *pubinfo = pubsinfo->publication;
4978 : : PQExpBuffer query;
4979 : : char *tag;
4980 : :
4981 : : /* Do nothing if not dumping schema */
4982 [ + + ]: 103 : if (!dopt->dumpSchema)
4983 : 12 : return;
4984 : :
4985 : 91 : tag = psprintf("%s %s", pubinfo->dobj.name, schemainfo->dobj.name);
4986 : :
4987 : 91 : query = createPQExpBuffer();
4988 : :
4989 : 91 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ", fmtId(pubinfo->dobj.name));
4990 : 91 : appendPQExpBuffer(query, "ADD TABLES IN SCHEMA %s;\n", fmtId(schemainfo->dobj.name));
4991 : :
4992 : : /*
4993 : : * There is no point in creating drop query as the drop is done by schema
4994 : : * drop.
4995 : : */
4996 [ + - ]: 91 : if (pubsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4997 : 91 : ArchiveEntry(fout, pubsinfo->dobj.catId, pubsinfo->dobj.dumpId,
4998 : 91 : ARCHIVE_OPTS(.tag = tag,
4999 : : .namespace = schemainfo->dobj.name,
5000 : : .owner = pubinfo->rolname,
5001 : : .description = "PUBLICATION TABLES IN SCHEMA",
5002 : : .section = SECTION_POST_DATA,
5003 : : .createStmt = query->data));
5004 : :
5005 : : /* These objects can't currently have comments or seclabels */
5006 : :
5007 : 91 : pfree(tag);
5008 : 91 : destroyPQExpBuffer(query);
5009 : : }
5010 : :
5011 : : /*
5012 : : * dumpPublicationTable
5013 : : * dump the definition of the given publication table mapping
5014 : : */
5015 : : static void
5016 : 298 : dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo)
5017 : : {
5018 : 298 : DumpOptions *dopt = fout->dopt;
5019 : 298 : PublicationInfo *pubinfo = pubrinfo->publication;
5020 : 298 : TableInfo *tbinfo = pubrinfo->pubtable;
5021 : : PQExpBuffer query;
5022 : : char *tag;
5023 : :
5024 : : /* Do nothing if not dumping schema */
5025 [ + + ]: 298 : if (!dopt->dumpSchema)
5026 : 42 : return;
5027 : :
5028 : 256 : tag = psprintf("%s %s", pubinfo->dobj.name, tbinfo->dobj.name);
5029 : :
5030 : 256 : query = createPQExpBuffer();
5031 : :
5032 : 256 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
5033 : 256 : fmtId(pubinfo->dobj.name));
5034 : 256 : appendPQExpBuffer(query, " %s",
5035 : 256 : fmtQualifiedDumpable(tbinfo));
5036 : :
5037 [ + + ]: 256 : if (pubrinfo->pubrattrs)
5038 : 81 : appendPQExpBuffer(query, " (%s)", pubrinfo->pubrattrs);
5039 : :
5040 [ + + ]: 256 : if (pubrinfo->pubrelqual)
5041 : : {
5042 : : /*
5043 : : * It's necessary to add parentheses around the expression because
5044 : : * pg_get_expr won't supply the parentheses for things like WHERE
5045 : : * TRUE.
5046 : : */
5047 : 114 : appendPQExpBuffer(query, " WHERE (%s)", pubrinfo->pubrelqual);
5048 : : }
5049 : 256 : appendPQExpBufferStr(query, ";\n");
5050 : :
5051 : : /*
5052 : : * There is no point in creating a drop query as the drop is done by table
5053 : : * drop. (If you think to change this, see also _printTocEntry().)
5054 : : * Although this object doesn't really have ownership as such, set the
5055 : : * owner field anyway to ensure that the command is run by the correct
5056 : : * role at restore time.
5057 : : */
5058 [ + - ]: 256 : if (pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5059 : 256 : ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
5060 : 256 : ARCHIVE_OPTS(.tag = tag,
5061 : : .namespace = tbinfo->dobj.namespace->dobj.name,
5062 : : .owner = pubinfo->rolname,
5063 : : .description = "PUBLICATION TABLE",
5064 : : .section = SECTION_POST_DATA,
5065 : : .createStmt = query->data));
5066 : :
5067 : : /* These objects can't currently have comments or seclabels */
5068 : :
5069 : 256 : pfree(tag);
5070 : 256 : destroyPQExpBuffer(query);
5071 : : }
5072 : :
5073 : : /*
5074 : : * Is the currently connected user a superuser?
5075 : : */
5076 : : static bool
5077 : 214 : is_superuser(Archive *fout)
5078 : : {
5079 : 214 : ArchiveHandle *AH = (ArchiveHandle *) fout;
5080 : : const char *val;
5081 : :
5082 : 214 : val = PQparameterStatus(AH->connection, "is_superuser");
5083 : :
5084 [ + - + + ]: 214 : if (val && strcmp(val, "on") == 0)
5085 : 211 : return true;
5086 : :
5087 : 3 : return false;
5088 : : }
5089 : :
5090 : : /*
5091 : : * Set the given value to restrict_nonsystem_relation_kind value. Since
5092 : : * restrict_nonsystem_relation_kind is introduced in minor version releases,
5093 : : * the setting query is effective only where available.
5094 : : */
5095 : : static void
5096 : 249 : set_restrict_relation_kind(Archive *AH, const char *value)
5097 : : {
5098 : 249 : PQExpBuffer query = createPQExpBuffer();
5099 : : PGresult *res;
5100 : :
5101 : 249 : appendPQExpBuffer(query,
5102 : : "SELECT set_config(name, '%s', false) "
5103 : : "FROM pg_settings "
5104 : : "WHERE name = 'restrict_nonsystem_relation_kind'",
5105 : : value);
5106 : 249 : res = ExecuteSqlQuery(AH, query->data, PGRES_TUPLES_OK);
5107 : :
5108 : 249 : PQclear(res);
5109 : 249 : destroyPQExpBuffer(query);
5110 : 249 : }
5111 : :
5112 : : /*
5113 : : * getSubscriptions
5114 : : * get information about subscriptions
5115 : : */
5116 : : void
5117 : 215 : getSubscriptions(Archive *fout)
5118 : : {
5119 : 215 : DumpOptions *dopt = fout->dopt;
5120 : : PQExpBuffer query;
5121 : : PGresult *res;
5122 : : SubscriptionInfo *subinfo;
5123 : : int i_tableoid;
5124 : : int i_oid;
5125 : : int i_subname;
5126 : : int i_subowner;
5127 : : int i_subbinary;
5128 : : int i_substream;
5129 : : int i_subtwophasestate;
5130 : : int i_subdisableonerr;
5131 : : int i_subpasswordrequired;
5132 : : int i_subrunasowner;
5133 : : int i_subservername;
5134 : : int i_subconninfo;
5135 : : int i_subslotname;
5136 : : int i_subsynccommit;
5137 : : int i_subwalrcvtimeout;
5138 : : int i_subpublications;
5139 : : int i_suborigin;
5140 : : int i_suboriginremotelsn;
5141 : : int i_subenabled;
5142 : : int i_subfailover;
5143 : : int i_subretaindeadtuples;
5144 : : int i_submaxretention;
5145 : : int i,
5146 : : ntups;
5147 : :
5148 [ + + ]: 215 : if (dopt->no_subscriptions)
5149 : 1 : return;
5150 : :
5151 [ + + ]: 214 : if (!is_superuser(fout))
5152 : : {
5153 : : int n;
5154 : :
5155 : 3 : res = ExecuteSqlQuery(fout,
5156 : : "SELECT count(*) FROM pg_subscription "
5157 : : "WHERE subdbid = (SELECT oid FROM pg_database"
5158 : : " WHERE datname = current_database())",
5159 : : PGRES_TUPLES_OK);
5160 : 3 : n = atoi(PQgetvalue(res, 0, 0));
5161 [ + + ]: 3 : if (n > 0)
5162 : 2 : pg_log_warning("subscriptions not dumped because current user is not a superuser");
5163 : 3 : PQclear(res);
5164 : 3 : return;
5165 : : }
5166 : :
5167 : 211 : query = createPQExpBuffer();
5168 : :
5169 : : /* Get the subscriptions in current database. */
5170 : 211 : appendPQExpBufferStr(query,
5171 : : "SELECT s.tableoid, s.oid, s.subname,\n"
5172 : : " s.subowner,\n"
5173 : : " s.subconninfo, s.subslotname, s.subsynccommit,\n"
5174 : : " s.subpublications,\n");
5175 : :
5176 [ + - ]: 211 : if (fout->remoteVersion >= 140000)
5177 : 211 : appendPQExpBufferStr(query, " s.subbinary,\n");
5178 : : else
5179 : 0 : appendPQExpBufferStr(query, " false AS subbinary,\n");
5180 : :
5181 [ + - ]: 211 : if (fout->remoteVersion >= 140000)
5182 : 211 : appendPQExpBufferStr(query, " s.substream,\n");
5183 : : else
5184 : 0 : appendPQExpBufferStr(query, " 'f' AS substream,\n");
5185 : :
5186 [ + - ]: 211 : if (fout->remoteVersion >= 150000)
5187 : 211 : appendPQExpBufferStr(query,
5188 : : " s.subtwophasestate,\n"
5189 : : " s.subdisableonerr,\n");
5190 : : else
5191 : 0 : appendPQExpBuffer(query,
5192 : : " '%c' AS subtwophasestate,\n"
5193 : : " false AS subdisableonerr,\n",
5194 : : LOGICALREP_TWOPHASE_STATE_DISABLED);
5195 : :
5196 [ + - ]: 211 : if (fout->remoteVersion >= 160000)
5197 : 211 : appendPQExpBufferStr(query,
5198 : : " s.subpasswordrequired,\n"
5199 : : " s.subrunasowner,\n"
5200 : : " s.suborigin,\n");
5201 : : else
5202 : 0 : appendPQExpBuffer(query,
5203 : : " 't' AS subpasswordrequired,\n"
5204 : : " 't' AS subrunasowner,\n"
5205 : : " '%s' AS suborigin,\n",
5206 : : LOGICALREP_ORIGIN_ANY);
5207 : :
5208 [ + + + - ]: 211 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5209 : 44 : appendPQExpBufferStr(query, " o.remote_lsn AS suboriginremotelsn,\n"
5210 : : " s.subenabled,\n");
5211 : : else
5212 : 167 : appendPQExpBufferStr(query, " NULL AS suboriginremotelsn,\n"
5213 : : " false AS subenabled,\n");
5214 : :
5215 [ + - ]: 211 : if (fout->remoteVersion >= 170000)
5216 : 211 : appendPQExpBufferStr(query,
5217 : : " s.subfailover,\n");
5218 : : else
5219 : 0 : appendPQExpBufferStr(query,
5220 : : " false AS subfailover,\n");
5221 : :
5222 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5223 : 211 : appendPQExpBufferStr(query,
5224 : : " s.subretaindeadtuples,\n");
5225 : : else
5226 : 0 : appendPQExpBufferStr(query,
5227 : : " false AS subretaindeadtuples,\n");
5228 : :
5229 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5230 : 211 : appendPQExpBufferStr(query,
5231 : : " s.submaxretention,\n");
5232 : : else
5233 : 0 : appendPQExpBufferStr(query, " 0 AS submaxretention,\n");
5234 : :
5235 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5236 : 211 : appendPQExpBufferStr(query,
5237 : : " s.subwalrcvtimeout,\n");
5238 : : else
5239 : 0 : appendPQExpBufferStr(query,
5240 : : " '-1' AS subwalrcvtimeout,\n");
5241 : :
5242 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5243 : 211 : appendPQExpBufferStr(query, " fs.srvname AS subservername\n");
5244 : : else
5245 : 0 : appendPQExpBufferStr(query, " NULL AS subservername\n");
5246 : :
5247 : 211 : appendPQExpBufferStr(query,
5248 : : "FROM pg_subscription s\n");
5249 : :
5250 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5251 : 211 : appendPQExpBufferStr(query,
5252 : : "LEFT JOIN pg_catalog.pg_foreign_server fs \n"
5253 : : " ON fs.oid = s.subserver \n");
5254 : :
5255 [ + + + - ]: 211 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5256 : 44 : appendPQExpBufferStr(query,
5257 : : "LEFT JOIN pg_catalog.pg_replication_origin_status o \n"
5258 : : " ON o.external_id = 'pg_' || s.oid::text \n");
5259 : :
5260 : 211 : appendPQExpBufferStr(query,
5261 : : "WHERE s.subdbid = (SELECT oid FROM pg_database\n"
5262 : : " WHERE datname = current_database())");
5263 : :
5264 : 211 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5265 : :
5266 : 211 : ntups = PQntuples(res);
5267 : :
5268 : : /*
5269 : : * Get subscription fields. We don't include subskiplsn in the dump as
5270 : : * after restoring the dump this value may no longer be relevant.
5271 : : */
5272 : 211 : i_tableoid = PQfnumber(res, "tableoid");
5273 : 211 : i_oid = PQfnumber(res, "oid");
5274 : 211 : i_subname = PQfnumber(res, "subname");
5275 : 211 : i_subowner = PQfnumber(res, "subowner");
5276 : 211 : i_subenabled = PQfnumber(res, "subenabled");
5277 : 211 : i_subbinary = PQfnumber(res, "subbinary");
5278 : 211 : i_substream = PQfnumber(res, "substream");
5279 : 211 : i_subtwophasestate = PQfnumber(res, "subtwophasestate");
5280 : 211 : i_subdisableonerr = PQfnumber(res, "subdisableonerr");
5281 : 211 : i_subpasswordrequired = PQfnumber(res, "subpasswordrequired");
5282 : 211 : i_subrunasowner = PQfnumber(res, "subrunasowner");
5283 : 211 : i_subfailover = PQfnumber(res, "subfailover");
5284 : 211 : i_subretaindeadtuples = PQfnumber(res, "subretaindeadtuples");
5285 : 211 : i_submaxretention = PQfnumber(res, "submaxretention");
5286 : 211 : i_subservername = PQfnumber(res, "subservername");
5287 : 211 : i_subconninfo = PQfnumber(res, "subconninfo");
5288 : 211 : i_subslotname = PQfnumber(res, "subslotname");
5289 : 211 : i_subsynccommit = PQfnumber(res, "subsynccommit");
5290 : 211 : i_subwalrcvtimeout = PQfnumber(res, "subwalrcvtimeout");
5291 : 211 : i_subpublications = PQfnumber(res, "subpublications");
5292 : 211 : i_suborigin = PQfnumber(res, "suborigin");
5293 : 211 : i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn");
5294 : :
5295 : 211 : subinfo = pg_malloc_array(SubscriptionInfo, ntups);
5296 : :
5297 [ + + ]: 351 : for (i = 0; i < ntups; i++)
5298 : : {
5299 : 140 : subinfo[i].dobj.objType = DO_SUBSCRIPTION;
5300 : 140 : subinfo[i].dobj.catId.tableoid =
5301 : 140 : atooid(PQgetvalue(res, i, i_tableoid));
5302 : 140 : subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5303 : 140 : AssignDumpId(&subinfo[i].dobj);
5304 : 140 : subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
5305 : 140 : subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner));
5306 : :
5307 : 140 : subinfo[i].subenabled =
5308 : 140 : (strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0);
5309 [ + - ]: 140 : if (PQgetisnull(res, i, i_subservername))
5310 : 140 : subinfo[i].subservername = NULL;
5311 : : else
5312 : 0 : subinfo[i].subservername = pg_strdup(PQgetvalue(res, i, i_subservername));
5313 : 140 : subinfo[i].subbinary =
5314 : 140 : (strcmp(PQgetvalue(res, i, i_subbinary), "t") == 0);
5315 : 140 : subinfo[i].substream = *(PQgetvalue(res, i, i_substream));
5316 : 140 : subinfo[i].subtwophasestate = *(PQgetvalue(res, i, i_subtwophasestate));
5317 : 140 : subinfo[i].subdisableonerr =
5318 : 140 : (strcmp(PQgetvalue(res, i, i_subdisableonerr), "t") == 0);
5319 : 140 : subinfo[i].subpasswordrequired =
5320 : 140 : (strcmp(PQgetvalue(res, i, i_subpasswordrequired), "t") == 0);
5321 : 140 : subinfo[i].subrunasowner =
5322 : 140 : (strcmp(PQgetvalue(res, i, i_subrunasowner), "t") == 0);
5323 : 140 : subinfo[i].subfailover =
5324 : 140 : (strcmp(PQgetvalue(res, i, i_subfailover), "t") == 0);
5325 : 140 : subinfo[i].subretaindeadtuples =
5326 : 140 : (strcmp(PQgetvalue(res, i, i_subretaindeadtuples), "t") == 0);
5327 : 140 : subinfo[i].submaxretention =
5328 : 140 : atoi(PQgetvalue(res, i, i_submaxretention));
5329 [ - + ]: 140 : if (PQgetisnull(res, i, i_subconninfo))
5330 : 0 : subinfo[i].subconninfo = NULL;
5331 : : else
5332 : 140 : subinfo[i].subconninfo =
5333 : 140 : pg_strdup(PQgetvalue(res, i, i_subconninfo));
5334 [ - + ]: 140 : if (PQgetisnull(res, i, i_subslotname))
5335 : 0 : subinfo[i].subslotname = NULL;
5336 : : else
5337 : 140 : subinfo[i].subslotname =
5338 : 140 : pg_strdup(PQgetvalue(res, i, i_subslotname));
5339 : 280 : subinfo[i].subsynccommit =
5340 : 140 : pg_strdup(PQgetvalue(res, i, i_subsynccommit));
5341 : 280 : subinfo[i].subwalrcvtimeout =
5342 : 140 : pg_strdup(PQgetvalue(res, i, i_subwalrcvtimeout));
5343 : 280 : subinfo[i].subpublications =
5344 : 140 : pg_strdup(PQgetvalue(res, i, i_subpublications));
5345 : 140 : subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin));
5346 [ + + ]: 140 : if (PQgetisnull(res, i, i_suboriginremotelsn))
5347 : 138 : subinfo[i].suboriginremotelsn = NULL;
5348 : : else
5349 : 2 : subinfo[i].suboriginremotelsn =
5350 : 2 : pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn));
5351 : :
5352 : : /* Decide whether we want to dump it */
5353 : 140 : selectDumpableObject(&(subinfo[i].dobj), fout);
5354 : : }
5355 : 211 : PQclear(res);
5356 : :
5357 : 211 : destroyPQExpBuffer(query);
5358 : : }
5359 : :
5360 : : /*
5361 : : * getSubscriptionRelations
5362 : : * Get information about subscription membership for dumpable relations. This
5363 : : * will be used only in binary-upgrade mode for PG17 or later versions.
5364 : : */
5365 : : void
5366 : 215 : getSubscriptionRelations(Archive *fout)
5367 : : {
5368 : 215 : DumpOptions *dopt = fout->dopt;
5369 : 215 : SubscriptionInfo *subinfo = NULL;
5370 : : SubRelInfo *subrinfo;
5371 : : PGresult *res;
5372 : : int i_srsubid;
5373 : : int i_srrelid;
5374 : : int i_srsubstate;
5375 : : int i_srsublsn;
5376 : : int ntups;
5377 : 215 : Oid last_srsubid = InvalidOid;
5378 : :
5379 [ + + + + ]: 215 : if (dopt->no_subscriptions || !dopt->binary_upgrade ||
5380 [ - + ]: 44 : fout->remoteVersion < 170000)
5381 : 171 : return;
5382 : :
5383 : 44 : res = ExecuteSqlQuery(fout,
5384 : : "SELECT srsubid, srrelid, srsubstate, srsublsn "
5385 : : "FROM pg_catalog.pg_subscription_rel "
5386 : : "ORDER BY srsubid",
5387 : : PGRES_TUPLES_OK);
5388 : 44 : ntups = PQntuples(res);
5389 [ + + ]: 44 : if (ntups == 0)
5390 : 43 : goto cleanup;
5391 : :
5392 : : /* Get pg_subscription_rel attributes */
5393 : 1 : i_srsubid = PQfnumber(res, "srsubid");
5394 : 1 : i_srrelid = PQfnumber(res, "srrelid");
5395 : 1 : i_srsubstate = PQfnumber(res, "srsubstate");
5396 : 1 : i_srsublsn = PQfnumber(res, "srsublsn");
5397 : :
5398 : 1 : subrinfo = pg_malloc_array(SubRelInfo, ntups);
5399 [ + + ]: 4 : for (int i = 0; i < ntups; i++)
5400 : : {
5401 : 3 : Oid cur_srsubid = atooid(PQgetvalue(res, i, i_srsubid));
5402 : 3 : Oid relid = atooid(PQgetvalue(res, i, i_srrelid));
5403 : : TableInfo *tblinfo;
5404 : :
5405 : : /*
5406 : : * If we switched to a new subscription, check if the subscription
5407 : : * exists.
5408 : : */
5409 [ + + ]: 3 : if (cur_srsubid != last_srsubid)
5410 : : {
5411 : 2 : subinfo = findSubscriptionByOid(cur_srsubid);
5412 [ - + ]: 2 : if (subinfo == NULL)
5413 : 0 : pg_fatal("subscription with OID %u does not exist", cur_srsubid);
5414 : :
5415 : 2 : last_srsubid = cur_srsubid;
5416 : : }
5417 : :
5418 : 3 : tblinfo = findTableByOid(relid);
5419 [ - + ]: 3 : if (tblinfo == NULL)
5420 : 0 : pg_fatal("failed sanity check, relation with OID %u not found",
5421 : : relid);
5422 : :
5423 : : /* OK, make a DumpableObject for this relationship */
5424 : 3 : subrinfo[i].dobj.objType = DO_SUBSCRIPTION_REL;
5425 : 3 : subrinfo[i].dobj.catId.tableoid = relid;
5426 : 3 : subrinfo[i].dobj.catId.oid = cur_srsubid;
5427 : 3 : AssignDumpId(&subrinfo[i].dobj);
5428 : 3 : subrinfo[i].dobj.namespace = tblinfo->dobj.namespace;
5429 : 3 : subrinfo[i].dobj.name = tblinfo->dobj.name;
5430 : 3 : subrinfo[i].subinfo = subinfo;
5431 : 3 : subrinfo[i].tblinfo = tblinfo;
5432 : 3 : subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0];
5433 [ + + ]: 3 : if (PQgetisnull(res, i, i_srsublsn))
5434 : 1 : subrinfo[i].srsublsn = NULL;
5435 : : else
5436 : 2 : subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn));
5437 : :
5438 : : /* Decide whether we want to dump it */
5439 : 3 : selectDumpableObject(&(subrinfo[i].dobj), fout);
5440 : : }
5441 : :
5442 : 1 : cleanup:
5443 : 44 : PQclear(res);
5444 : : }
5445 : :
5446 : : /*
5447 : : * dumpSubscriptionTable
5448 : : * Dump the definition of the given subscription table mapping. This will be
5449 : : * used only in binary-upgrade mode for PG17 or later versions.
5450 : : */
5451 : : static void
5452 : 3 : dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
5453 : : {
5454 : 3 : DumpOptions *dopt = fout->dopt;
5455 : 3 : SubscriptionInfo *subinfo = subrinfo->subinfo;
5456 : : PQExpBuffer query;
5457 : : char *tag;
5458 : :
5459 : : /* Do nothing if not dumping schema */
5460 [ - + ]: 3 : if (!dopt->dumpSchema)
5461 : 0 : return;
5462 : :
5463 : : Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000);
5464 : :
5465 : 3 : tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->tblinfo->dobj.name);
5466 : :
5467 : 3 : query = createPQExpBuffer();
5468 : :
5469 [ + - ]: 3 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5470 : : {
5471 : : /*
5472 : : * binary_upgrade_add_sub_rel_state will add the subscription relation
5473 : : * to pg_subscription_rel table. This will be used only in
5474 : : * binary-upgrade mode.
5475 : : */
5476 : 3 : appendPQExpBufferStr(query,
5477 : : "\n-- For binary upgrade, must preserve the subscriber table.\n");
5478 : 3 : appendPQExpBufferStr(query,
5479 : : "SELECT pg_catalog.binary_upgrade_add_sub_rel_state(");
5480 : 3 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5481 : 3 : appendPQExpBuffer(query,
5482 : : ", %u, '%c'",
5483 : 3 : subrinfo->tblinfo->dobj.catId.oid,
5484 : 3 : subrinfo->srsubstate);
5485 : :
5486 [ + + + - ]: 3 : if (subrinfo->srsublsn && subrinfo->srsublsn[0] != '\0')
5487 : 2 : appendPQExpBuffer(query, ", '%s'", subrinfo->srsublsn);
5488 : : else
5489 : 1 : appendPQExpBufferStr(query, ", NULL");
5490 : :
5491 : 3 : appendPQExpBufferStr(query, ");\n");
5492 : : }
5493 : :
5494 : : /*
5495 : : * There is no point in creating a drop query as the drop is done by table
5496 : : * drop. (If you think to change this, see also _printTocEntry().)
5497 : : * Although this object doesn't really have ownership as such, set the
5498 : : * owner field anyway to ensure that the command is run by the correct
5499 : : * role at restore time.
5500 : : */
5501 [ + - ]: 3 : if (subrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5502 : 3 : ArchiveEntry(fout, subrinfo->dobj.catId, subrinfo->dobj.dumpId,
5503 : 3 : ARCHIVE_OPTS(.tag = tag,
5504 : : .namespace = subrinfo->tblinfo->dobj.namespace->dobj.name,
5505 : : .owner = subinfo->rolname,
5506 : : .description = "SUBSCRIPTION TABLE",
5507 : : .section = SECTION_POST_DATA,
5508 : : .createStmt = query->data));
5509 : :
5510 : : /* These objects can't currently have comments or seclabels */
5511 : :
5512 : 3 : pfree(tag);
5513 : 3 : destroyPQExpBuffer(query);
5514 : : }
5515 : :
5516 : : /*
5517 : : * dumpSubscription
5518 : : * dump the definition of the given subscription
5519 : : */
5520 : : static void
5521 : 119 : dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
5522 : : {
5523 : 119 : DumpOptions *dopt = fout->dopt;
5524 : : PQExpBuffer delq;
5525 : : PQExpBuffer query;
5526 : : PQExpBuffer publications;
5527 : : char *qsubname;
5528 : 119 : char **pubnames = NULL;
5529 : 119 : int npubnames = 0;
5530 : : int i;
5531 : :
5532 : : /* Do nothing if not dumping schema */
5533 [ + + ]: 119 : if (!dopt->dumpSchema)
5534 : 18 : return;
5535 : :
5536 : 101 : delq = createPQExpBuffer();
5537 : 101 : query = createPQExpBuffer();
5538 : :
5539 : 101 : qsubname = pg_strdup(fmtId(subinfo->dobj.name));
5540 : :
5541 : 101 : appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n",
5542 : : qsubname);
5543 : :
5544 : 101 : appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s ",
5545 : : qsubname);
5546 [ - + ]: 101 : if (subinfo->subservername)
5547 : : {
5548 : 0 : appendPQExpBuffer(query, "SERVER %s", fmtId(subinfo->subservername));
5549 : : }
5550 : : else
5551 : : {
5552 : 101 : appendPQExpBufferStr(query, "CONNECTION ");
5553 : 101 : appendStringLiteralAH(query, subinfo->subconninfo, fout);
5554 : : }
5555 : :
5556 : : /* Build list of quoted publications and append them to query. */
5557 [ - + ]: 101 : if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames))
5558 : 0 : pg_fatal("could not parse %s array", "subpublications");
5559 : :
5560 : 101 : publications = createPQExpBuffer();
5561 [ + + ]: 202 : for (i = 0; i < npubnames; i++)
5562 : : {
5563 [ - + ]: 101 : if (i > 0)
5564 : 0 : appendPQExpBufferStr(publications, ", ");
5565 : :
5566 : 101 : appendPQExpBufferStr(publications, fmtId(pubnames[i]));
5567 : : }
5568 : :
5569 : 101 : appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data);
5570 [ + - ]: 101 : if (subinfo->subslotname)
5571 : 101 : appendStringLiteralAH(query, subinfo->subslotname, fout);
5572 : : else
5573 : 0 : appendPQExpBufferStr(query, "NONE");
5574 : :
5575 [ - + ]: 101 : if (subinfo->subbinary)
5576 : 0 : appendPQExpBufferStr(query, ", binary = true");
5577 : :
5578 [ + + ]: 101 : if (subinfo->substream == LOGICALREP_STREAM_ON)
5579 : 32 : appendPQExpBufferStr(query, ", streaming = on");
5580 [ + + ]: 69 : else if (subinfo->substream == LOGICALREP_STREAM_PARALLEL)
5581 : 37 : appendPQExpBufferStr(query, ", streaming = parallel");
5582 : : else
5583 : 32 : appendPQExpBufferStr(query, ", streaming = off");
5584 : :
5585 [ - + ]: 101 : if (subinfo->subtwophasestate != LOGICALREP_TWOPHASE_STATE_DISABLED)
5586 : 0 : appendPQExpBufferStr(query, ", two_phase = on");
5587 : :
5588 [ - + ]: 101 : if (subinfo->subdisableonerr)
5589 : 0 : appendPQExpBufferStr(query, ", disable_on_error = true");
5590 : :
5591 [ - + ]: 101 : if (!subinfo->subpasswordrequired)
5592 : 0 : appendPQExpBufferStr(query, ", password_required = false");
5593 : :
5594 [ - + ]: 101 : if (subinfo->subrunasowner)
5595 : 0 : appendPQExpBufferStr(query, ", run_as_owner = true");
5596 : :
5597 [ + + ]: 101 : if (subinfo->subfailover)
5598 : 1 : appendPQExpBufferStr(query, ", failover = true");
5599 : :
5600 [ + + ]: 101 : if (subinfo->subretaindeadtuples)
5601 : 1 : appendPQExpBufferStr(query, ", retain_dead_tuples = true");
5602 : :
5603 [ - + ]: 101 : if (subinfo->submaxretention)
5604 : 0 : appendPQExpBuffer(query, ", max_retention_duration = %d", subinfo->submaxretention);
5605 : :
5606 [ - + ]: 101 : if (strcmp(subinfo->subsynccommit, "off") != 0)
5607 : 0 : appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));
5608 : :
5609 [ - + ]: 101 : if (strcmp(subinfo->subwalrcvtimeout, "-1") != 0)
5610 : 0 : appendPQExpBuffer(query, ", wal_receiver_timeout = %s", fmtId(subinfo->subwalrcvtimeout));
5611 : :
5612 [ + + ]: 101 : if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0)
5613 : 32 : appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin);
5614 : :
5615 : 101 : appendPQExpBufferStr(query, ");\n");
5616 : :
5617 : : /*
5618 : : * In binary-upgrade mode, we allow the replication to continue after the
5619 : : * upgrade.
5620 : : */
5621 [ + + + - ]: 101 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5622 : : {
5623 [ + + ]: 6 : if (subinfo->suboriginremotelsn)
5624 : : {
5625 : : /*
5626 : : * Preserve the remote_lsn for the subscriber's replication
5627 : : * origin. This value is required to start the replication from
5628 : : * the position before the upgrade. This value will be stale if
5629 : : * the publisher gets upgraded before the subscriber node.
5630 : : * However, this shouldn't be a problem as the upgrade of the
5631 : : * publisher ensures that all the transactions were replicated
5632 : : * before upgrading it.
5633 : : */
5634 : 2 : appendPQExpBufferStr(query,
5635 : : "\n-- For binary upgrade, must preserve the remote_lsn for the subscriber's replication origin.\n");
5636 : 2 : appendPQExpBufferStr(query,
5637 : : "SELECT pg_catalog.binary_upgrade_replorigin_advance(");
5638 : 2 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5639 : 2 : appendPQExpBuffer(query, ", '%s');\n", subinfo->suboriginremotelsn);
5640 : : }
5641 : :
5642 [ + + ]: 6 : if (subinfo->subenabled)
5643 : : {
5644 : : /*
5645 : : * Enable the subscription to allow the replication to continue
5646 : : * after the upgrade.
5647 : : */
5648 : 2 : appendPQExpBufferStr(query,
5649 : : "\n-- For binary upgrade, must preserve the subscriber's running state.\n");
5650 : 2 : appendPQExpBuffer(query, "ALTER SUBSCRIPTION %s ENABLE;\n", qsubname);
5651 : : }
5652 : : }
5653 : :
5654 [ + - ]: 101 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5655 : 101 : ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
5656 : 101 : ARCHIVE_OPTS(.tag = subinfo->dobj.name,
5657 : : .owner = subinfo->rolname,
5658 : : .description = "SUBSCRIPTION",
5659 : : .section = SECTION_POST_DATA,
5660 : : .createStmt = query->data,
5661 : : .dropStmt = delq->data));
5662 : :
5663 [ + + ]: 101 : if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
5664 : 32 : dumpComment(fout, "SUBSCRIPTION", qsubname,
5665 : 32 : NULL, subinfo->rolname,
5666 : 32 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5667 : :
5668 [ - + ]: 101 : if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
5669 : 0 : dumpSecLabel(fout, "SUBSCRIPTION", qsubname,
5670 : 0 : NULL, subinfo->rolname,
5671 : 0 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5672 : :
5673 : 101 : destroyPQExpBuffer(publications);
5674 : 101 : free(pubnames);
5675 : :
5676 : 101 : destroyPQExpBuffer(delq);
5677 : 101 : destroyPQExpBuffer(query);
5678 : 101 : pg_free(qsubname);
5679 : : }
5680 : :
5681 : : /*
5682 : : * Given a "create query", append as many ALTER ... DEPENDS ON EXTENSION as
5683 : : * the object needs.
5684 : : */
5685 : : static void
5686 : 5334 : append_depends_on_extension(Archive *fout,
5687 : : PQExpBuffer create,
5688 : : const DumpableObject *dobj,
5689 : : const char *catalog,
5690 : : const char *keyword,
5691 : : const char *objname)
5692 : : {
5693 [ + + ]: 5334 : if (dobj->depends_on_ext)
5694 : : {
5695 : : char *nm;
5696 : : PGresult *res;
5697 : : PQExpBuffer query;
5698 : : int ntups;
5699 : : int i_extname;
5700 : : int i;
5701 : :
5702 : : /* dodge fmtId() non-reentrancy */
5703 : 42 : nm = pg_strdup(objname);
5704 : :
5705 : 42 : query = createPQExpBuffer();
5706 : 42 : appendPQExpBuffer(query,
5707 : : "SELECT e.extname "
5708 : : "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
5709 : : "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
5710 : : "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
5711 : : "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
5712 : : catalog,
5713 : 42 : dobj->catId.oid);
5714 : 42 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5715 : 42 : ntups = PQntuples(res);
5716 : 42 : i_extname = PQfnumber(res, "extname");
5717 [ + + ]: 84 : for (i = 0; i < ntups; i++)
5718 : : {
5719 : 42 : appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
5720 : : keyword, nm,
5721 : 42 : fmtId(PQgetvalue(res, i, i_extname)));
5722 : : }
5723 : :
5724 : 42 : PQclear(res);
5725 : 42 : destroyPQExpBuffer(query);
5726 : 42 : pg_free(nm);
5727 : : }
5728 : 5334 : }
5729 : :
5730 : : static Oid
5731 : 0 : get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
5732 : : {
5733 : : /*
5734 : : * If the old version didn't assign an array type, but the new version
5735 : : * does, we must select an unused type OID to assign. This currently only
5736 : : * happens for domains, when upgrading pre-v11 to v11 and up.
5737 : : *
5738 : : * Note: local state here is kind of ugly, but we must have some, since we
5739 : : * mustn't choose the same unused OID more than once.
5740 : : */
5741 : : static Oid next_possible_free_oid = FirstNormalObjectId;
5742 : : PGresult *res;
5743 : : bool is_dup;
5744 : :
5745 : : do
5746 : : {
5747 : 0 : ++next_possible_free_oid;
5748 : 0 : printfPQExpBuffer(upgrade_query,
5749 : : "SELECT EXISTS(SELECT 1 "
5750 : : "FROM pg_catalog.pg_type "
5751 : : "WHERE oid = '%u'::pg_catalog.oid);",
5752 : : next_possible_free_oid);
5753 : 0 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5754 : 0 : is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
5755 : 0 : PQclear(res);
5756 [ # # ]: 0 : } while (is_dup);
5757 : :
5758 : 0 : return next_possible_free_oid;
5759 : : }
5760 : :
5761 : : static void
5762 : 1011 : binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
5763 : : PQExpBuffer upgrade_buffer,
5764 : : Oid pg_type_oid,
5765 : : bool force_array_type,
5766 : : bool include_multirange_type)
5767 : : {
5768 : 1011 : PQExpBuffer upgrade_query = createPQExpBuffer();
5769 : : PGresult *res;
5770 : : Oid pg_type_array_oid;
5771 : : Oid pg_type_multirange_oid;
5772 : : Oid pg_type_multirange_array_oid;
5773 : : TypeInfo *tinfo;
5774 : :
5775 : 1011 : appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
5776 : 1011 : appendPQExpBuffer(upgrade_buffer,
5777 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5778 : : pg_type_oid);
5779 : :
5780 : 1011 : tinfo = findTypeByOid(pg_type_oid);
5781 [ + - ]: 1011 : if (tinfo)
5782 : 1011 : pg_type_array_oid = tinfo->typarray;
5783 : : else
5784 : 0 : pg_type_array_oid = InvalidOid;
5785 : :
5786 [ + + - + ]: 1011 : if (!OidIsValid(pg_type_array_oid) && force_array_type)
5787 : 0 : pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5788 : :
5789 [ + + ]: 1011 : if (OidIsValid(pg_type_array_oid))
5790 : : {
5791 : 1009 : appendPQExpBufferStr(upgrade_buffer,
5792 : : "\n-- For binary upgrade, must preserve pg_type array oid\n");
5793 : 1009 : appendPQExpBuffer(upgrade_buffer,
5794 : : "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5795 : : pg_type_array_oid);
5796 : : }
5797 : :
5798 : : /*
5799 : : * Pre-set the multirange type oid and its own array type oid.
5800 : : */
5801 [ + + ]: 1011 : if (include_multirange_type)
5802 : : {
5803 [ + - ]: 9 : if (fout->remoteVersion >= 140000)
5804 : : {
5805 : 9 : printfPQExpBuffer(upgrade_query,
5806 : : "SELECT t.oid, t.typarray "
5807 : : "FROM pg_catalog.pg_type t "
5808 : : "JOIN pg_catalog.pg_range r "
5809 : : "ON t.oid = r.rngmultitypid "
5810 : : "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
5811 : : pg_type_oid);
5812 : :
5813 : 9 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5814 : :
5815 : 9 : pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
5816 : 9 : pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5817 : :
5818 : 9 : PQclear(res);
5819 : : }
5820 : : else
5821 : : {
5822 : 0 : pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5823 : 0 : pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5824 : : }
5825 : :
5826 : 9 : appendPQExpBufferStr(upgrade_buffer,
5827 : : "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
5828 : 9 : appendPQExpBuffer(upgrade_buffer,
5829 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5830 : : pg_type_multirange_oid);
5831 : 9 : appendPQExpBufferStr(upgrade_buffer,
5832 : : "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
5833 : 9 : appendPQExpBuffer(upgrade_buffer,
5834 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5835 : : pg_type_multirange_array_oid);
5836 : : }
5837 : :
5838 : 1011 : destroyPQExpBuffer(upgrade_query);
5839 : 1011 : }
5840 : :
5841 : : static void
5842 : 925 : binary_upgrade_set_type_oids_by_rel(Archive *fout,
5843 : : PQExpBuffer upgrade_buffer,
5844 : : const TableInfo *tbinfo)
5845 : : {
5846 : 925 : Oid pg_type_oid = tbinfo->reltype;
5847 : :
5848 [ + - ]: 925 : if (OidIsValid(pg_type_oid))
5849 : 925 : binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5850 : : pg_type_oid, false, false);
5851 : 925 : }
5852 : :
5853 : : /*
5854 : : * bsearch() comparator for BinaryUpgradeClassOidItem
5855 : : */
5856 : : static int
5857 : 13297 : BinaryUpgradeClassOidItemCmp(const void *p1, const void *p2)
5858 : : {
5859 : 13297 : BinaryUpgradeClassOidItem v1 = *((const BinaryUpgradeClassOidItem *) p1);
5860 : 13297 : BinaryUpgradeClassOidItem v2 = *((const BinaryUpgradeClassOidItem *) p2);
5861 : :
5862 : 13297 : return pg_cmp_u32(v1.oid, v2.oid);
5863 : : }
5864 : :
5865 : : /*
5866 : : * collectBinaryUpgradeClassOids
5867 : : *
5868 : : * Construct a table of pg_class information required for
5869 : : * binary_upgrade_set_pg_class_oids(). The table is sorted by OID for speed in
5870 : : * lookup.
5871 : : */
5872 : : static void
5873 : 44 : collectBinaryUpgradeClassOids(Archive *fout)
5874 : : {
5875 : : PGresult *res;
5876 : : const char *query;
5877 : :
5878 : 44 : query = "SELECT c.oid, c.relkind, c.relfilenode, c.reltoastrelid, "
5879 : : "ct.relfilenode, i.indexrelid, cti.relfilenode, "
5880 : : "(SELECT a.atttypid FROM pg_attribute AS a "
5881 : : " WHERE a.attrelid = c.reltoastrelid AND attname = 'chunk_id'::text) "
5882 : : " AS toastchunktypid "
5883 : : "FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index i "
5884 : : "ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5885 : : "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5886 : : "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5887 : : "ORDER BY c.oid;";
5888 : :
5889 : 44 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
5890 : :
5891 : 44 : nbinaryUpgradeClassOids = PQntuples(res);
5892 : 44 : binaryUpgradeClassOids =
5893 : 44 : pg_malloc_array(BinaryUpgradeClassOidItem, nbinaryUpgradeClassOids);
5894 : :
5895 [ + + ]: 20639 : for (int i = 0; i < nbinaryUpgradeClassOids; i++)
5896 : : {
5897 : 20595 : binaryUpgradeClassOids[i].oid = atooid(PQgetvalue(res, i, 0));
5898 : 20595 : binaryUpgradeClassOids[i].relkind = *PQgetvalue(res, i, 1);
5899 : 20595 : binaryUpgradeClassOids[i].relfilenumber = atooid(PQgetvalue(res, i, 2));
5900 : 20595 : binaryUpgradeClassOids[i].toast_oid = atooid(PQgetvalue(res, i, 3));
5901 : 20595 : binaryUpgradeClassOids[i].toast_relfilenumber = atooid(PQgetvalue(res, i, 4));
5902 : 20595 : binaryUpgradeClassOids[i].toast_index_oid = atooid(PQgetvalue(res, i, 5));
5903 : 20595 : binaryUpgradeClassOids[i].toast_index_relfilenumber = atooid(PQgetvalue(res, i, 6));
5904 : 20595 : binaryUpgradeClassOids[i].toast_chunk_id_typoid = atooid(PQgetvalue(res, i, 7));
5905 : : }
5906 : :
5907 : 44 : PQclear(res);
5908 : 44 : }
5909 : :
5910 : : static void
5911 : 1343 : binary_upgrade_set_pg_class_oids(Archive *fout,
5912 : : PQExpBuffer upgrade_buffer, Oid pg_class_oid)
5913 : : {
5914 : 1343 : BinaryUpgradeClassOidItem key = {0};
5915 : : BinaryUpgradeClassOidItem *entry;
5916 : :
5917 : : Assert(binaryUpgradeClassOids);
5918 : :
5919 : : /*
5920 : : * Preserve the OID and relfilenumber of the table, table's index, table's
5921 : : * toast table, toast table's chunk type and toast table's index if any.
5922 : : *
5923 : : * One complexity is that the current table definition might not require
5924 : : * the creation of a TOAST table, but the old database might have a TOAST
5925 : : * table that was created earlier, before some wide columns were dropped.
5926 : : * By setting the TOAST oid we force creation of the TOAST heap and index
5927 : : * by the new backend, so we can copy the files during binary upgrade
5928 : : * without worrying about this case.
5929 : : */
5930 : 1343 : key.oid = pg_class_oid;
5931 : 1343 : entry = bsearch(&key, binaryUpgradeClassOids, nbinaryUpgradeClassOids,
5932 : : sizeof(BinaryUpgradeClassOidItem),
5933 : : BinaryUpgradeClassOidItemCmp);
5934 : :
5935 : 1343 : appendPQExpBufferStr(upgrade_buffer,
5936 : : "\n-- For binary upgrade, must preserve pg_class oids, toast chunk type oids and relfilenodes\n");
5937 : :
5938 [ + + ]: 1343 : if (entry->relkind != RELKIND_INDEX &&
5939 [ + + ]: 1048 : entry->relkind != RELKIND_PARTITIONED_INDEX)
5940 : : {
5941 : 1017 : appendPQExpBuffer(upgrade_buffer,
5942 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5943 : : pg_class_oid);
5944 : :
5945 : : /*
5946 : : * Not every relation has storage. Also, in a pre-v12 database,
5947 : : * partitioned tables have a relfilenumber, which should not be
5948 : : * preserved when upgrading.
5949 : : */
5950 [ + + ]: 1017 : if (RelFileNumberIsValid(entry->relfilenumber) &&
5951 [ + - ]: 836 : entry->relkind != RELKIND_PARTITIONED_TABLE)
5952 : 836 : appendPQExpBuffer(upgrade_buffer,
5953 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
5954 : : entry->relfilenumber);
5955 : :
5956 : : /*
5957 : : * In a pre-v12 database, partitioned tables might be marked as having
5958 : : * toast tables, but we should ignore them if so.
5959 : : */
5960 [ + + ]: 1017 : if (OidIsValid(entry->toast_oid) &&
5961 [ + - ]: 298 : entry->relkind != RELKIND_PARTITIONED_TABLE)
5962 : : {
5963 : 298 : appendPQExpBuffer(upgrade_buffer,
5964 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
5965 : : entry->toast_oid);
5966 : 298 : appendPQExpBuffer(upgrade_buffer,
5967 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
5968 : : entry->toast_relfilenumber);
5969 : 298 : appendPQExpBuffer(upgrade_buffer,
5970 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_chunk_id_typoid('%u'::pg_catalog.oid);\n",
5971 : : entry->toast_chunk_id_typoid);
5972 : :
5973 : : /* every toast table has an index */
5974 : 298 : appendPQExpBuffer(upgrade_buffer,
5975 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5976 : : entry->toast_index_oid);
5977 : 298 : appendPQExpBuffer(upgrade_buffer,
5978 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5979 : : entry->toast_index_relfilenumber);
5980 : : }
5981 : : }
5982 : : else
5983 : : {
5984 : : /* Preserve the OID and relfilenumber of the index */
5985 : 326 : appendPQExpBuffer(upgrade_buffer,
5986 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5987 : : pg_class_oid);
5988 : 326 : appendPQExpBuffer(upgrade_buffer,
5989 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5990 : : entry->relfilenumber);
5991 : : }
5992 : :
5993 : 1343 : appendPQExpBufferChar(upgrade_buffer, '\n');
5994 : 1343 : }
5995 : :
5996 : : /*
5997 : : * Emit SQL to pre-set the attribute versions for the next relation to be
5998 : : * created.
5999 : : *
6000 : : * attversions[j] is the version of attribute j+1, or InvalidXLogRecPtr if it
6001 : : * was unversioned in the source cluster. This needs to include dropped
6002 : : * attributes.
6003 : : *
6004 : : * The version has to be set as part of the CREATE itself, rather than be
6005 : : * fixed up afterwards, as some CREATE statements can contain expressions that
6006 : : * can embed a row type constant of the relation being created. If the
6007 : : * attribute versions were to be updated subsequently, the embedded datums
6008 : : * would be considered "out-of-date" and throw errors on use.
6009 : : */
6010 : : static void
6011 : 951 : binary_upgrade_set_next_attribute_versions(PQExpBuffer upgrade_buffer,
6012 : : int numatts,
6013 : : const uint64 *attversions)
6014 : : {
6015 : 951 : appendPQExpBufferStr(upgrade_buffer,
6016 : : "\n-- For binary upgrade, must preserve the attribute versions\n"
6017 : : "SELECT pg_catalog.binary_upgrade_set_next_attribute_versions('{");
6018 : :
6019 [ + + ]: 4457 : for (int j = 0; j < numatts; j++)
6020 : 3506 : appendPQExpBuffer(upgrade_buffer, "%s%X/%X",
6021 : : j > 0 ? "," : "",
6022 [ + + ]: 3506 : LSN_FORMAT_ARGS(attversions[j]));
6023 : :
6024 : 951 : appendPQExpBufferStr(upgrade_buffer, "}'::pg_catalog.pg_lsn[]);\n\n");
6025 : 951 : }
6026 : :
6027 : : /*
6028 : : * If the DumpableObject is a member of an extension, add a suitable
6029 : : * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
6030 : : *
6031 : : * For somewhat historical reasons, objname should already be quoted,
6032 : : * but not objnamespace (if any).
6033 : : */
6034 : : static void
6035 : 1600 : binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
6036 : : const DumpableObject *dobj,
6037 : : const char *objtype,
6038 : : const char *objname,
6039 : : const char *objnamespace)
6040 : : {
6041 : 1600 : DumpableObject *extobj = NULL;
6042 : : int i;
6043 : :
6044 [ + + ]: 1600 : if (!dobj->ext_member)
6045 : 1578 : return;
6046 : :
6047 : : /*
6048 : : * Find the parent extension. We could avoid this search if we wanted to
6049 : : * add a link field to DumpableObject, but the space costs of that would
6050 : : * be considerable. We assume that member objects could only have a
6051 : : * direct dependency on their own extension, not any others.
6052 : : */
6053 [ + - ]: 22 : for (i = 0; i < dobj->nDeps; i++)
6054 : : {
6055 : 22 : extobj = findObjectByDumpId(dobj->dependencies[i]);
6056 [ + - + - ]: 22 : if (extobj && extobj->objType == DO_EXTENSION)
6057 : 22 : break;
6058 : 0 : extobj = NULL;
6059 : : }
6060 [ - + ]: 22 : if (extobj == NULL)
6061 : 0 : pg_fatal("could not find parent extension for %s %s",
6062 : : objtype, objname);
6063 : :
6064 : 22 : appendPQExpBufferStr(upgrade_buffer,
6065 : : "\n-- For binary upgrade, handle extension membership the hard way\n");
6066 : 22 : appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
6067 : 22 : fmtId(extobj->name),
6068 : : objtype);
6069 [ + + + - ]: 22 : if (objnamespace && *objnamespace)
6070 : 19 : appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
6071 : 22 : appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
6072 : : }
6073 : :
6074 : : /*
6075 : : * getNamespaces:
6076 : : * get information about all namespaces in the system catalogs
6077 : : */
6078 : : void
6079 : 216 : getNamespaces(Archive *fout)
6080 : : {
6081 : : PGresult *res;
6082 : : int ntups;
6083 : : int i;
6084 : : PQExpBuffer query;
6085 : : NamespaceInfo *nsinfo;
6086 : : int i_tableoid;
6087 : : int i_oid;
6088 : : int i_nspname;
6089 : : int i_nspowner;
6090 : : int i_nspacl;
6091 : : int i_acldefault;
6092 : :
6093 : 216 : query = createPQExpBuffer();
6094 : :
6095 : : /*
6096 : : * we fetch all namespaces including system ones, so that every object we
6097 : : * read in can be linked to a containing namespace.
6098 : : */
6099 : 216 : appendPQExpBufferStr(query, "SELECT n.tableoid, n.oid, n.nspname, "
6100 : : "n.nspowner, "
6101 : : "n.nspacl, "
6102 : : "acldefault('n', n.nspowner) AS acldefault "
6103 : : "FROM pg_namespace n");
6104 : :
6105 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6106 : :
6107 : 216 : ntups = PQntuples(res);
6108 : :
6109 : 216 : nsinfo = pg_malloc_array(NamespaceInfo, ntups);
6110 : :
6111 : 216 : i_tableoid = PQfnumber(res, "tableoid");
6112 : 216 : i_oid = PQfnumber(res, "oid");
6113 : 216 : i_nspname = PQfnumber(res, "nspname");
6114 : 216 : i_nspowner = PQfnumber(res, "nspowner");
6115 : 216 : i_nspacl = PQfnumber(res, "nspacl");
6116 : 216 : i_acldefault = PQfnumber(res, "acldefault");
6117 : :
6118 [ + + ]: 1996 : for (i = 0; i < ntups; i++)
6119 : : {
6120 : : const char *nspowner;
6121 : :
6122 : 1780 : nsinfo[i].dobj.objType = DO_NAMESPACE;
6123 : 1780 : nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6124 : 1780 : nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6125 : 1780 : AssignDumpId(&nsinfo[i].dobj);
6126 : 1780 : nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
6127 : 1780 : nsinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_nspacl));
6128 : 1780 : nsinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6129 : 1780 : nsinfo[i].dacl.privtype = 0;
6130 : 1780 : nsinfo[i].dacl.initprivs = NULL;
6131 : 1780 : nspowner = PQgetvalue(res, i, i_nspowner);
6132 : 1780 : nsinfo[i].nspowner = atooid(nspowner);
6133 : 1780 : nsinfo[i].rolname = getRoleName(nspowner);
6134 : :
6135 : : /* Decide whether to dump this namespace */
6136 : 1780 : selectDumpableNamespace(&nsinfo[i], fout);
6137 : :
6138 : : /* Mark whether namespace has an ACL */
6139 [ + + ]: 1780 : if (!PQgetisnull(res, i, i_nspacl))
6140 : 929 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6141 : :
6142 : : /*
6143 : : * We ignore any pg_init_privs.initprivs entry for the public schema
6144 : : * and assume a predetermined default, for several reasons. First,
6145 : : * dropping and recreating the schema removes its pg_init_privs entry,
6146 : : * but an empty destination database starts with this ACL nonetheless.
6147 : : * Second, we support dump/reload of public schema ownership changes.
6148 : : * ALTER SCHEMA OWNER filters nspacl through aclnewowner(), but
6149 : : * initprivs continues to reflect the initial owner. Hence,
6150 : : * synthesize the value that nspacl will have after the restore's
6151 : : * ALTER SCHEMA OWNER. Third, this makes the destination database
6152 : : * match the source's ACL, even if the latter was an initdb-default
6153 : : * ACL, which changed in v15. An upgrade pulls in changes to most
6154 : : * system object ACLs that the DBA had not customized. We've made the
6155 : : * public schema depart from that, because changing its ACL so easily
6156 : : * breaks applications.
6157 : : */
6158 [ + + ]: 1780 : if (strcmp(nsinfo[i].dobj.name, "public") == 0)
6159 : : {
6160 : 212 : PQExpBuffer aclarray = createPQExpBuffer();
6161 : 212 : PQExpBuffer aclitem = createPQExpBuffer();
6162 : :
6163 : : /* Standard ACL as of v15 is {owner=UC/owner,=U/owner} */
6164 : 212 : appendPQExpBufferChar(aclarray, '{');
6165 : 212 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6166 : 212 : appendPQExpBufferStr(aclitem, "=UC/");
6167 : 212 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6168 : 212 : appendPGArray(aclarray, aclitem->data);
6169 : 212 : resetPQExpBuffer(aclitem);
6170 : 212 : appendPQExpBufferStr(aclitem, "=U/");
6171 : 212 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6172 : 212 : appendPGArray(aclarray, aclitem->data);
6173 : 212 : appendPQExpBufferChar(aclarray, '}');
6174 : :
6175 : 212 : nsinfo[i].dacl.privtype = 'i';
6176 : 212 : nsinfo[i].dacl.initprivs = pstrdup(aclarray->data);
6177 : 212 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6178 : :
6179 : 212 : destroyPQExpBuffer(aclarray);
6180 : 212 : destroyPQExpBuffer(aclitem);
6181 : : }
6182 : : }
6183 : :
6184 : 216 : PQclear(res);
6185 : 216 : destroyPQExpBuffer(query);
6186 : 216 : }
6187 : :
6188 : : /*
6189 : : * findNamespace:
6190 : : * given a namespace OID, look up the info read by getNamespaces
6191 : : */
6192 : : static NamespaceInfo *
6193 : 690977 : findNamespace(Oid nsoid)
6194 : : {
6195 : : NamespaceInfo *nsinfo;
6196 : :
6197 : 690977 : nsinfo = findNamespaceByOid(nsoid);
6198 [ - + ]: 690977 : if (nsinfo == NULL)
6199 : 0 : pg_fatal("schema with OID %u does not exist", nsoid);
6200 : 690977 : return nsinfo;
6201 : : }
6202 : :
6203 : : /*
6204 : : * getExtensions:
6205 : : * read all extensions in the system catalogs and return them in the
6206 : : * ExtensionInfo* structure
6207 : : *
6208 : : * numExtensions is set to the number of extensions read in
6209 : : */
6210 : : ExtensionInfo *
6211 : 216 : getExtensions(Archive *fout, int *numExtensions)
6212 : : {
6213 : 216 : DumpOptions *dopt = fout->dopt;
6214 : : PGresult *res;
6215 : : int ntups;
6216 : : int i;
6217 : : PQExpBuffer query;
6218 : 216 : ExtensionInfo *extinfo = NULL;
6219 : : int i_tableoid;
6220 : : int i_oid;
6221 : : int i_extname;
6222 : : int i_nspname;
6223 : : int i_extrelocatable;
6224 : : int i_extversion;
6225 : : int i_extconfig;
6226 : : int i_extcondition;
6227 : :
6228 : 216 : query = createPQExpBuffer();
6229 : :
6230 : 216 : appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, "
6231 : : "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
6232 : : "FROM pg_extension x "
6233 : : "JOIN pg_namespace n ON n.oid = x.extnamespace");
6234 : :
6235 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6236 : :
6237 : 216 : ntups = PQntuples(res);
6238 [ - + ]: 216 : if (ntups == 0)
6239 : 0 : goto cleanup;
6240 : :
6241 : 216 : extinfo = pg_malloc_array(ExtensionInfo, ntups);
6242 : :
6243 : 216 : i_tableoid = PQfnumber(res, "tableoid");
6244 : 216 : i_oid = PQfnumber(res, "oid");
6245 : 216 : i_extname = PQfnumber(res, "extname");
6246 : 216 : i_nspname = PQfnumber(res, "nspname");
6247 : 216 : i_extrelocatable = PQfnumber(res, "extrelocatable");
6248 : 216 : i_extversion = PQfnumber(res, "extversion");
6249 : 216 : i_extconfig = PQfnumber(res, "extconfig");
6250 : 216 : i_extcondition = PQfnumber(res, "extcondition");
6251 : :
6252 [ + + ]: 463 : for (i = 0; i < ntups; i++)
6253 : : {
6254 : 247 : extinfo[i].dobj.objType = DO_EXTENSION;
6255 : 247 : extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6256 : 247 : extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6257 : 247 : AssignDumpId(&extinfo[i].dobj);
6258 : 247 : extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
6259 : 247 : extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
6260 : 247 : extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
6261 : 247 : extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
6262 : 247 : extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
6263 : 247 : extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
6264 : :
6265 : : /* Decide whether we want to dump it */
6266 : 247 : selectDumpableExtension(&(extinfo[i]), dopt);
6267 : : }
6268 : :
6269 : 216 : cleanup:
6270 : 216 : PQclear(res);
6271 : 216 : destroyPQExpBuffer(query);
6272 : :
6273 : 216 : *numExtensions = ntups;
6274 : :
6275 : 216 : return extinfo;
6276 : : }
6277 : :
6278 : : /*
6279 : : * getTypes:
6280 : : * get information about all types in the system catalogs
6281 : : *
6282 : : * NB: this must run after getFuncs() because we assume we can do
6283 : : * findFuncByOid().
6284 : : */
6285 : : void
6286 : 215 : getTypes(Archive *fout)
6287 : : {
6288 : : PGresult *res;
6289 : : int ntups;
6290 : : int i;
6291 : 215 : PQExpBuffer query = createPQExpBuffer();
6292 : : TypeInfo *tyinfo;
6293 : : ShellTypeInfo *stinfo;
6294 : : int i_tableoid;
6295 : : int i_oid;
6296 : : int i_typname;
6297 : : int i_typnamespace;
6298 : : int i_typacl;
6299 : : int i_acldefault;
6300 : : int i_typowner;
6301 : : int i_typelem;
6302 : : int i_typrelid;
6303 : : int i_typrelkind;
6304 : : int i_typtype;
6305 : : int i_typisdefined;
6306 : : int i_isarray;
6307 : : int i_typarray;
6308 : :
6309 : : /*
6310 : : * we include even the built-in types because those may be used as array
6311 : : * elements by user-defined types
6312 : : *
6313 : : * we filter out the built-in types when we dump out the types
6314 : : *
6315 : : * same approach for undefined (shell) types and array types
6316 : : *
6317 : : * Note: as of 8.3 we can reliably detect whether a type is an
6318 : : * auto-generated array type by checking the element type's typarray.
6319 : : * (Before that the test is capable of generating false positives.) We
6320 : : * still check for name beginning with '_', though, so as to avoid the
6321 : : * cost of the subselect probe for all standard types. This would have to
6322 : : * be revisited if the backend ever allows renaming of array types.
6323 : : */
6324 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, typname, "
6325 : : "typnamespace, typacl, "
6326 : : "acldefault('T', typowner) AS acldefault, "
6327 : : "typowner, "
6328 : : "typelem, typrelid, typarray, "
6329 : : "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
6330 : : "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
6331 : : "typtype, typisdefined, "
6332 : : "typname[0] = '_' AND typelem != 0 AND "
6333 : : "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
6334 : : "FROM pg_type");
6335 : :
6336 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6337 : :
6338 : 215 : ntups = PQntuples(res);
6339 : :
6340 : 215 : tyinfo = pg_malloc_array(TypeInfo, ntups);
6341 : :
6342 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6343 : 215 : i_oid = PQfnumber(res, "oid");
6344 : 215 : i_typname = PQfnumber(res, "typname");
6345 : 215 : i_typnamespace = PQfnumber(res, "typnamespace");
6346 : 215 : i_typacl = PQfnumber(res, "typacl");
6347 : 215 : i_acldefault = PQfnumber(res, "acldefault");
6348 : 215 : i_typowner = PQfnumber(res, "typowner");
6349 : 215 : i_typelem = PQfnumber(res, "typelem");
6350 : 215 : i_typrelid = PQfnumber(res, "typrelid");
6351 : 215 : i_typrelkind = PQfnumber(res, "typrelkind");
6352 : 215 : i_typtype = PQfnumber(res, "typtype");
6353 : 215 : i_typisdefined = PQfnumber(res, "typisdefined");
6354 : 215 : i_isarray = PQfnumber(res, "isarray");
6355 : 215 : i_typarray = PQfnumber(res, "typarray");
6356 : :
6357 [ + + ]: 158003 : for (i = 0; i < ntups; i++)
6358 : : {
6359 : 157788 : tyinfo[i].dobj.objType = DO_TYPE;
6360 : 157788 : tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6361 : 157788 : tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6362 : 157788 : AssignDumpId(&tyinfo[i].dobj);
6363 : 157788 : tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
6364 : 315576 : tyinfo[i].dobj.namespace =
6365 : 157788 : findNamespace(atooid(PQgetvalue(res, i, i_typnamespace)));
6366 : 157788 : tyinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_typacl));
6367 : 157788 : tyinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6368 : 157788 : tyinfo[i].dacl.privtype = 0;
6369 : 157788 : tyinfo[i].dacl.initprivs = NULL;
6370 : 157788 : tyinfo[i].ftypname = NULL; /* may get filled later */
6371 : 157788 : tyinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_typowner));
6372 : 157788 : tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
6373 : 157788 : tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
6374 : 157788 : tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
6375 : 157788 : tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
6376 : 157788 : tyinfo[i].shellType = NULL;
6377 : :
6378 [ + + ]: 157788 : if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
6379 : 157733 : tyinfo[i].isDefined = true;
6380 : : else
6381 : 55 : tyinfo[i].isDefined = false;
6382 : :
6383 [ + + ]: 157788 : if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
6384 : 75749 : tyinfo[i].isArray = true;
6385 : : else
6386 : 82039 : tyinfo[i].isArray = false;
6387 : :
6388 : 157788 : tyinfo[i].typarray = atooid(PQgetvalue(res, i, i_typarray));
6389 : :
6390 [ + + ]: 157788 : if (tyinfo[i].typtype == TYPTYPE_MULTIRANGE)
6391 : 1435 : tyinfo[i].isMultirange = true;
6392 : : else
6393 : 156353 : tyinfo[i].isMultirange = false;
6394 : :
6395 : : /* Decide whether we want to dump it */
6396 : 157788 : selectDumpableType(&tyinfo[i], fout);
6397 : :
6398 : : /* Mark whether type has an ACL */
6399 [ + + ]: 157788 : if (!PQgetisnull(res, i, i_typacl))
6400 : 217 : tyinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6401 : :
6402 : : /*
6403 : : * If it's a domain, fetch info about its constraints, if any
6404 : : */
6405 : 157788 : tyinfo[i].nDomChecks = 0;
6406 : 157788 : tyinfo[i].domChecks = NULL;
6407 : 157788 : tyinfo[i].notnull = NULL;
6408 [ + + ]: 157788 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6409 [ + + ]: 16550 : tyinfo[i].typtype == TYPTYPE_DOMAIN)
6410 : 171 : getDomainConstraints(fout, &(tyinfo[i]));
6411 : :
6412 : : /*
6413 : : * If it's a base type, make a DumpableObject representing a shell
6414 : : * definition of the type. We will need to dump that ahead of the I/O
6415 : : * functions for the type. Similarly, range types need a shell
6416 : : * definition in case they have a canonicalize function.
6417 : : *
6418 : : * Note: the shell type doesn't have a catId. You might think it
6419 : : * should copy the base type's catId, but then it might capture the
6420 : : * pg_depend entries for the type, which we don't want.
6421 : : */
6422 [ + + ]: 157788 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6423 [ + + ]: 16550 : (tyinfo[i].typtype == TYPTYPE_BASE ||
6424 [ + + ]: 8057 : tyinfo[i].typtype == TYPTYPE_RANGE))
6425 : : {
6426 : 8628 : stinfo = pg_malloc_object(ShellTypeInfo);
6427 : 8628 : stinfo->dobj.objType = DO_SHELL_TYPE;
6428 : 8628 : stinfo->dobj.catId = nilCatalogId;
6429 : 8628 : AssignDumpId(&stinfo->dobj);
6430 : 8628 : stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
6431 : 8628 : stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
6432 : 8628 : stinfo->baseType = &(tyinfo[i]);
6433 : 8628 : tyinfo[i].shellType = stinfo;
6434 : :
6435 : : /*
6436 : : * Initially mark the shell type as not to be dumped. We'll only
6437 : : * dump it if the I/O or canonicalize functions need to be dumped;
6438 : : * this is taken care of while sorting dependencies.
6439 : : */
6440 : 8628 : stinfo->dobj.dump = DUMP_COMPONENT_NONE;
6441 : : }
6442 : : }
6443 : :
6444 : 215 : PQclear(res);
6445 : :
6446 : 215 : destroyPQExpBuffer(query);
6447 : 215 : }
6448 : :
6449 : : /*
6450 : : * getOperators:
6451 : : * get information about all operators in the system catalogs
6452 : : */
6453 : : void
6454 : 215 : getOperators(Archive *fout)
6455 : : {
6456 : : PGresult *res;
6457 : : int ntups;
6458 : : int i;
6459 : 215 : PQExpBuffer query = createPQExpBuffer();
6460 : : OprInfo *oprinfo;
6461 : : int i_tableoid;
6462 : : int i_oid;
6463 : : int i_oprname;
6464 : : int i_oprnamespace;
6465 : : int i_oprowner;
6466 : : int i_oprkind;
6467 : : int i_oprleft;
6468 : : int i_oprright;
6469 : : int i_oprcode;
6470 : :
6471 : : /*
6472 : : * find all operators, including builtin operators; we filter out
6473 : : * system-defined operators at dump-out time.
6474 : : */
6475 : :
6476 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, oprname, "
6477 : : "oprnamespace, "
6478 : : "oprowner, "
6479 : : "oprkind, "
6480 : : "oprleft, "
6481 : : "oprright, "
6482 : : "oprcode::oid AS oprcode "
6483 : : "FROM pg_operator");
6484 : :
6485 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6486 : :
6487 : 215 : ntups = PQntuples(res);
6488 : :
6489 : 215 : oprinfo = pg_malloc_array(OprInfo, ntups);
6490 : :
6491 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6492 : 215 : i_oid = PQfnumber(res, "oid");
6493 : 215 : i_oprname = PQfnumber(res, "oprname");
6494 : 215 : i_oprnamespace = PQfnumber(res, "oprnamespace");
6495 : 215 : i_oprowner = PQfnumber(res, "oprowner");
6496 : 215 : i_oprkind = PQfnumber(res, "oprkind");
6497 : 215 : i_oprleft = PQfnumber(res, "oprleft");
6498 : 215 : i_oprright = PQfnumber(res, "oprright");
6499 : 215 : i_oprcode = PQfnumber(res, "oprcode");
6500 : :
6501 [ + + ]: 173435 : for (i = 0; i < ntups; i++)
6502 : : {
6503 : 173220 : oprinfo[i].dobj.objType = DO_OPERATOR;
6504 : 173220 : oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6505 : 173220 : oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6506 : 173220 : AssignDumpId(&oprinfo[i].dobj);
6507 : 173220 : oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
6508 : 346440 : oprinfo[i].dobj.namespace =
6509 : 173220 : findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)));
6510 : 173220 : oprinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_oprowner));
6511 : 173220 : oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
6512 : 173220 : oprinfo[i].oprleft = atooid(PQgetvalue(res, i, i_oprleft));
6513 : 173220 : oprinfo[i].oprright = atooid(PQgetvalue(res, i, i_oprright));
6514 : 173220 : oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
6515 : :
6516 : : /* Decide whether we want to dump it */
6517 : 173220 : selectDumpableObject(&(oprinfo[i].dobj), fout);
6518 : : }
6519 : :
6520 : 215 : PQclear(res);
6521 : :
6522 : 215 : destroyPQExpBuffer(query);
6523 : 215 : }
6524 : :
6525 : : /*
6526 : : * getCollations:
6527 : : * get information about all collations in the system catalogs
6528 : : */
6529 : : void
6530 : 215 : getCollations(Archive *fout)
6531 : : {
6532 : : PGresult *res;
6533 : : int ntups;
6534 : : int i;
6535 : : PQExpBuffer query;
6536 : : CollInfo *collinfo;
6537 : : int i_tableoid;
6538 : : int i_oid;
6539 : : int i_collname;
6540 : : int i_collnamespace;
6541 : : int i_collowner;
6542 : : int i_collencoding;
6543 : :
6544 : 215 : query = createPQExpBuffer();
6545 : :
6546 : : /*
6547 : : * find all collations, including builtin collations; we filter out
6548 : : * system-defined collations at dump-out time.
6549 : : */
6550 : :
6551 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, collname, "
6552 : : "collnamespace, "
6553 : : "collowner, "
6554 : : "collencoding "
6555 : : "FROM pg_collation");
6556 : :
6557 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6558 : :
6559 : 215 : ntups = PQntuples(res);
6560 : :
6561 : 215 : collinfo = pg_malloc_array(CollInfo, ntups);
6562 : :
6563 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6564 : 215 : i_oid = PQfnumber(res, "oid");
6565 : 215 : i_collname = PQfnumber(res, "collname");
6566 : 215 : i_collnamespace = PQfnumber(res, "collnamespace");
6567 : 215 : i_collowner = PQfnumber(res, "collowner");
6568 : 215 : i_collencoding = PQfnumber(res, "collencoding");
6569 : :
6570 [ + + ]: 189532 : for (i = 0; i < ntups; i++)
6571 : : {
6572 : 189317 : collinfo[i].dobj.objType = DO_COLLATION;
6573 : 189317 : collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6574 : 189317 : collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6575 : 189317 : AssignDumpId(&collinfo[i].dobj);
6576 : 189317 : collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
6577 : 378634 : collinfo[i].dobj.namespace =
6578 : 189317 : findNamespace(atooid(PQgetvalue(res, i, i_collnamespace)));
6579 : 189317 : collinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_collowner));
6580 : 189317 : collinfo[i].collencoding = atoi(PQgetvalue(res, i, i_collencoding));
6581 : :
6582 : : /* Decide whether we want to dump it */
6583 : 189317 : selectDumpableObject(&(collinfo[i].dobj), fout);
6584 : : }
6585 : :
6586 : 215 : PQclear(res);
6587 : :
6588 : 215 : destroyPQExpBuffer(query);
6589 : 215 : }
6590 : :
6591 : : /*
6592 : : * getConversions:
6593 : : * get information about all conversions in the system catalogs
6594 : : */
6595 : : void
6596 : 215 : getConversions(Archive *fout)
6597 : : {
6598 : : PGresult *res;
6599 : : int ntups;
6600 : : int i;
6601 : : PQExpBuffer query;
6602 : : ConvInfo *convinfo;
6603 : : int i_tableoid;
6604 : : int i_oid;
6605 : : int i_conname;
6606 : : int i_connamespace;
6607 : : int i_conowner;
6608 : :
6609 : 215 : query = createPQExpBuffer();
6610 : :
6611 : : /*
6612 : : * find all conversions, including builtin conversions; we filter out
6613 : : * system-defined conversions at dump-out time.
6614 : : */
6615 : :
6616 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, conname, "
6617 : : "connamespace, "
6618 : : "conowner "
6619 : : "FROM pg_conversion");
6620 : :
6621 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6622 : :
6623 : 215 : ntups = PQntuples(res);
6624 : :
6625 : 215 : convinfo = pg_malloc_array(ConvInfo, ntups);
6626 : :
6627 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6628 : 215 : i_oid = PQfnumber(res, "oid");
6629 : 215 : i_conname = PQfnumber(res, "conname");
6630 : 215 : i_connamespace = PQfnumber(res, "connamespace");
6631 : 215 : i_conowner = PQfnumber(res, "conowner");
6632 : :
6633 [ + + ]: 21333 : for (i = 0; i < ntups; i++)
6634 : : {
6635 : 21118 : convinfo[i].dobj.objType = DO_CONVERSION;
6636 : 21118 : convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6637 : 21118 : convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6638 : 21118 : AssignDumpId(&convinfo[i].dobj);
6639 : 21118 : convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
6640 : 42236 : convinfo[i].dobj.namespace =
6641 : 21118 : findNamespace(atooid(PQgetvalue(res, i, i_connamespace)));
6642 : 21118 : convinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_conowner));
6643 : :
6644 : : /* Decide whether we want to dump it */
6645 : 21118 : selectDumpableObject(&(convinfo[i].dobj), fout);
6646 : : }
6647 : :
6648 : 215 : PQclear(res);
6649 : :
6650 : 215 : destroyPQExpBuffer(query);
6651 : 215 : }
6652 : :
6653 : : /*
6654 : : * getAccessMethods:
6655 : : * get information about all user-defined access methods
6656 : : */
6657 : : void
6658 : 215 : getAccessMethods(Archive *fout)
6659 : : {
6660 : : PGresult *res;
6661 : : int ntups;
6662 : : int i;
6663 : : PQExpBuffer query;
6664 : : AccessMethodInfo *aminfo;
6665 : : int i_tableoid;
6666 : : int i_oid;
6667 : : int i_amname;
6668 : : int i_amhandler;
6669 : : int i_amtype;
6670 : :
6671 : 215 : query = createPQExpBuffer();
6672 : :
6673 : : /*
6674 : : * Select all access methods from pg_am table.
6675 : : */
6676 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, ");
6677 : 215 : appendPQExpBufferStr(query,
6678 : : "amtype, "
6679 : : "amhandler::pg_catalog.regproc AS amhandler ");
6680 : 215 : appendPQExpBufferStr(query, "FROM pg_am");
6681 : :
6682 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6683 : :
6684 : 215 : ntups = PQntuples(res);
6685 : :
6686 : 215 : aminfo = pg_malloc_array(AccessMethodInfo, ntups);
6687 : :
6688 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6689 : 215 : i_oid = PQfnumber(res, "oid");
6690 : 215 : i_amname = PQfnumber(res, "amname");
6691 : 215 : i_amhandler = PQfnumber(res, "amhandler");
6692 : 215 : i_amtype = PQfnumber(res, "amtype");
6693 : :
6694 [ + + ]: 1848 : for (i = 0; i < ntups; i++)
6695 : : {
6696 : 1633 : aminfo[i].dobj.objType = DO_ACCESS_METHOD;
6697 : 1633 : aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6698 : 1633 : aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6699 : 1633 : AssignDumpId(&aminfo[i].dobj);
6700 : 1633 : aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname));
6701 : 1633 : aminfo[i].dobj.namespace = NULL;
6702 : 1633 : aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler));
6703 : 1633 : aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype));
6704 : :
6705 : : /* Decide whether we want to dump it */
6706 : 1633 : selectDumpableAccessMethod(&(aminfo[i]), fout);
6707 : : }
6708 : :
6709 : 215 : PQclear(res);
6710 : :
6711 : 215 : destroyPQExpBuffer(query);
6712 : 215 : }
6713 : :
6714 : :
6715 : : /*
6716 : : * getOpclasses:
6717 : : * get information about all opclasses in the system catalogs
6718 : : */
6719 : : void
6720 : 215 : getOpclasses(Archive *fout)
6721 : : {
6722 : : PGresult *res;
6723 : : int ntups;
6724 : : int i;
6725 : 215 : PQExpBuffer query = createPQExpBuffer();
6726 : : OpclassInfo *opcinfo;
6727 : : int i_tableoid;
6728 : : int i_oid;
6729 : : int i_opcmethod;
6730 : : int i_opcname;
6731 : : int i_opcnamespace;
6732 : : int i_opcowner;
6733 : :
6734 : : /*
6735 : : * find all opclasses, including builtin opclasses; we filter out
6736 : : * system-defined opclasses at dump-out time.
6737 : : */
6738 : :
6739 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opcmethod, opcname, "
6740 : : "opcnamespace, "
6741 : : "opcowner "
6742 : : "FROM pg_opclass");
6743 : :
6744 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6745 : :
6746 : 215 : ntups = PQntuples(res);
6747 : :
6748 : 215 : opcinfo = pg_malloc_array(OpclassInfo, ntups);
6749 : :
6750 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6751 : 215 : i_oid = PQfnumber(res, "oid");
6752 : 215 : i_opcmethod = PQfnumber(res, "opcmethod");
6753 : 215 : i_opcname = PQfnumber(res, "opcname");
6754 : 215 : i_opcnamespace = PQfnumber(res, "opcnamespace");
6755 : 215 : i_opcowner = PQfnumber(res, "opcowner");
6756 : :
6757 [ + + ]: 38865 : for (i = 0; i < ntups; i++)
6758 : : {
6759 : 38650 : opcinfo[i].dobj.objType = DO_OPCLASS;
6760 : 38650 : opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6761 : 38650 : opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6762 : 38650 : AssignDumpId(&opcinfo[i].dobj);
6763 : 38650 : opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
6764 : 77300 : opcinfo[i].dobj.namespace =
6765 : 38650 : findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace)));
6766 : 38650 : opcinfo[i].opcmethod = atooid(PQgetvalue(res, i, i_opcmethod));
6767 : 38650 : opcinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opcowner));
6768 : :
6769 : : /* Decide whether we want to dump it */
6770 : 38650 : selectDumpableObject(&(opcinfo[i].dobj), fout);
6771 : : }
6772 : :
6773 : 215 : PQclear(res);
6774 : :
6775 : 215 : destroyPQExpBuffer(query);
6776 : 215 : }
6777 : :
6778 : : /*
6779 : : * getOpfamilies:
6780 : : * get information about all opfamilies in the system catalogs
6781 : : */
6782 : : void
6783 : 215 : getOpfamilies(Archive *fout)
6784 : : {
6785 : : PGresult *res;
6786 : : int ntups;
6787 : : int i;
6788 : : PQExpBuffer query;
6789 : : OpfamilyInfo *opfinfo;
6790 : : int i_tableoid;
6791 : : int i_oid;
6792 : : int i_opfmethod;
6793 : : int i_opfname;
6794 : : int i_opfnamespace;
6795 : : int i_opfowner;
6796 : :
6797 : 215 : query = createPQExpBuffer();
6798 : :
6799 : : /*
6800 : : * find all opfamilies, including builtin opfamilies; we filter out
6801 : : * system-defined opfamilies at dump-out time.
6802 : : */
6803 : :
6804 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opfmethod, opfname, "
6805 : : "opfnamespace, "
6806 : : "opfowner "
6807 : : "FROM pg_opfamily");
6808 : :
6809 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6810 : :
6811 : 215 : ntups = PQntuples(res);
6812 : :
6813 : 215 : opfinfo = pg_malloc_array(OpfamilyInfo, ntups);
6814 : :
6815 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6816 : 215 : i_oid = PQfnumber(res, "oid");
6817 : 215 : i_opfname = PQfnumber(res, "opfname");
6818 : 215 : i_opfmethod = PQfnumber(res, "opfmethod");
6819 : 215 : i_opfnamespace = PQfnumber(res, "opfnamespace");
6820 : 215 : i_opfowner = PQfnumber(res, "opfowner");
6821 : :
6822 [ + + ]: 32180 : for (i = 0; i < ntups; i++)
6823 : : {
6824 : 31965 : opfinfo[i].dobj.objType = DO_OPFAMILY;
6825 : 31965 : opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6826 : 31965 : opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6827 : 31965 : AssignDumpId(&opfinfo[i].dobj);
6828 : 31965 : opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
6829 : 63930 : opfinfo[i].dobj.namespace =
6830 : 31965 : findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace)));
6831 : 31965 : opfinfo[i].opfmethod = atooid(PQgetvalue(res, i, i_opfmethod));
6832 : 31965 : opfinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opfowner));
6833 : :
6834 : : /* Decide whether we want to dump it */
6835 : 31965 : selectDumpableObject(&(opfinfo[i].dobj), fout);
6836 : : }
6837 : :
6838 : 215 : PQclear(res);
6839 : :
6840 : 215 : destroyPQExpBuffer(query);
6841 : 215 : }
6842 : :
6843 : : /*
6844 : : * getAggregates:
6845 : : * get information about all user-defined aggregates in the system catalogs
6846 : : */
6847 : : void
6848 : 215 : getAggregates(Archive *fout)
6849 : : {
6850 : 215 : DumpOptions *dopt = fout->dopt;
6851 : : PGresult *res;
6852 : : int ntups;
6853 : : int i;
6854 : 215 : PQExpBuffer query = createPQExpBuffer();
6855 : : AggInfo *agginfo;
6856 : : int i_tableoid;
6857 : : int i_oid;
6858 : : int i_aggname;
6859 : : int i_aggnamespace;
6860 : : int i_pronargs;
6861 : : int i_proargtypes;
6862 : : int i_proowner;
6863 : : int i_aggacl;
6864 : : int i_acldefault;
6865 : : const char *agg_check;
6866 : :
6867 : : /*
6868 : : * Find all interesting aggregates. See comment in getFuncs() for the
6869 : : * rationale behind the filtering logic.
6870 : : */
6871 : 430 : agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'"
6872 [ + - ]: 215 : : "p.proisagg");
6873 : :
6874 : 215 : appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, "
6875 : : "p.proname AS aggname, "
6876 : : "p.pronamespace AS aggnamespace, "
6877 : : "p.pronargs, p.proargtypes, "
6878 : : "p.proowner, "
6879 : : "p.proacl AS aggacl, "
6880 : : "acldefault('f', p.proowner) AS acldefault "
6881 : : "FROM pg_proc p "
6882 : : "LEFT JOIN pg_init_privs pip ON "
6883 : : "(p.oid = pip.objoid "
6884 : : "AND pip.classoid = 'pg_proc'::regclass "
6885 : : "AND pip.objsubid = 0) "
6886 : : "WHERE %s AND ("
6887 : : "p.pronamespace != "
6888 : : "(SELECT oid FROM pg_namespace "
6889 : : "WHERE nspname = 'pg_catalog') OR "
6890 : : "p.proacl IS DISTINCT FROM pip.initprivs",
6891 : : agg_check);
6892 [ + + ]: 215 : if (dopt->binary_upgrade)
6893 : 44 : appendPQExpBufferStr(query,
6894 : : " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
6895 : : "classid = 'pg_proc'::regclass AND "
6896 : : "objid = p.oid AND "
6897 : : "refclassid = 'pg_extension'::regclass AND "
6898 : : "deptype = 'e')");
6899 : 215 : appendPQExpBufferChar(query, ')');
6900 : :
6901 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6902 : :
6903 : 215 : ntups = PQntuples(res);
6904 : :
6905 : 215 : agginfo = pg_malloc_array(AggInfo, ntups);
6906 : :
6907 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6908 : 215 : i_oid = PQfnumber(res, "oid");
6909 : 215 : i_aggname = PQfnumber(res, "aggname");
6910 : 215 : i_aggnamespace = PQfnumber(res, "aggnamespace");
6911 : 215 : i_pronargs = PQfnumber(res, "pronargs");
6912 : 215 : i_proargtypes = PQfnumber(res, "proargtypes");
6913 : 215 : i_proowner = PQfnumber(res, "proowner");
6914 : 215 : i_aggacl = PQfnumber(res, "aggacl");
6915 : 215 : i_acldefault = PQfnumber(res, "acldefault");
6916 : :
6917 [ + + ]: 617 : for (i = 0; i < ntups; i++)
6918 : : {
6919 : 402 : agginfo[i].aggfn.dobj.objType = DO_AGG;
6920 : 402 : agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6921 : 402 : agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6922 : 402 : AssignDumpId(&agginfo[i].aggfn.dobj);
6923 : 402 : agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
6924 : 804 : agginfo[i].aggfn.dobj.namespace =
6925 : 402 : findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace)));
6926 : 402 : agginfo[i].aggfn.dacl.acl = pg_strdup(PQgetvalue(res, i, i_aggacl));
6927 : 402 : agginfo[i].aggfn.dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6928 : 402 : agginfo[i].aggfn.dacl.privtype = 0;
6929 : 402 : agginfo[i].aggfn.dacl.initprivs = NULL;
6930 : 402 : agginfo[i].aggfn.rolname = getRoleName(PQgetvalue(res, i, i_proowner));
6931 : 402 : agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
6932 : 402 : agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */
6933 : 402 : agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs));
6934 [ + + ]: 402 : if (agginfo[i].aggfn.nargs == 0)
6935 : 56 : agginfo[i].aggfn.argtypes = NULL;
6936 : : else
6937 : 346 : agginfo[i].aggfn.argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
6938 : 346 : agginfo[i].aggfn.nargs);
6939 : 402 : agginfo[i].aggfn.postponed_def = false; /* might get set during sort */
6940 : :
6941 : : /* Decide whether we want to dump it */
6942 : 402 : selectDumpableObject(&(agginfo[i].aggfn.dobj), fout);
6943 : :
6944 : : /* Mark whether aggregate has an ACL */
6945 [ + + ]: 402 : if (!PQgetisnull(res, i, i_aggacl))
6946 : 25 : agginfo[i].aggfn.dobj.components |= DUMP_COMPONENT_ACL;
6947 : : }
6948 : :
6949 : 215 : PQclear(res);
6950 : :
6951 : 215 : destroyPQExpBuffer(query);
6952 : 215 : }
6953 : :
6954 : : /*
6955 : : * getFuncs:
6956 : : * get information about all user-defined functions in the system catalogs
6957 : : */
6958 : : void
6959 : 215 : getFuncs(Archive *fout)
6960 : : {
6961 : 215 : DumpOptions *dopt = fout->dopt;
6962 : : PGresult *res;
6963 : : int ntups;
6964 : : int i;
6965 : 215 : PQExpBuffer query = createPQExpBuffer();
6966 : : FuncInfo *finfo;
6967 : : int i_tableoid;
6968 : : int i_oid;
6969 : : int i_proname;
6970 : : int i_pronamespace;
6971 : : int i_proowner;
6972 : : int i_prolang;
6973 : : int i_pronargs;
6974 : : int i_proargtypes;
6975 : : int i_prorettype;
6976 : : int i_proacl;
6977 : : int i_acldefault;
6978 : : const char *not_agg_check;
6979 : :
6980 : : /*
6981 : : * Find all interesting functions. This is a bit complicated:
6982 : : *
6983 : : * 1. Always exclude aggregates; those are handled elsewhere.
6984 : : *
6985 : : * 2. Always exclude functions that are internally dependent on something
6986 : : * else, since presumably those will be created as a result of creating
6987 : : * the something else. This currently acts only to suppress constructor
6988 : : * functions for range types. Note this is OK only because the
6989 : : * constructors don't have any dependencies the range type doesn't have;
6990 : : * otherwise we might not get creation ordering correct.
6991 : : *
6992 : : * 3. Otherwise, we normally exclude functions in pg_catalog. However, if
6993 : : * they're members of extensions and we are in binary-upgrade mode then
6994 : : * include them, since we want to dump extension members individually in
6995 : : * that mode. Also, if they are used by casts or transforms then we need
6996 : : * to gather the information about them, though they won't be dumped if
6997 : : * they are built-in. Also, include functions in pg_catalog if they have
6998 : : * an ACL different from what's shown in pg_init_privs (so we have to join
6999 : : * to pg_init_privs; annoying).
7000 : : */
7001 : 430 : not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'"
7002 [ + - ]: 215 : : "NOT p.proisagg");
7003 : :
7004 : 215 : appendPQExpBuffer(query,
7005 : : "SELECT p.tableoid, p.oid, p.proname, p.prolang, "
7006 : : "p.pronargs, p.proargtypes, p.prorettype, "
7007 : : "p.proacl, "
7008 : : "acldefault('f', p.proowner) AS acldefault, "
7009 : : "p.pronamespace, "
7010 : : "p.proowner "
7011 : : "FROM pg_proc p "
7012 : : "LEFT JOIN pg_init_privs pip ON "
7013 : : "(p.oid = pip.objoid "
7014 : : "AND pip.classoid = 'pg_proc'::regclass "
7015 : : "AND pip.objsubid = 0) "
7016 : : "WHERE %s"
7017 : : "\n AND NOT EXISTS (SELECT 1 FROM pg_depend "
7018 : : "WHERE classid = 'pg_proc'::regclass AND "
7019 : : "objid = p.oid AND deptype = 'i')"
7020 : : "\n AND ("
7021 : : "\n pronamespace != "
7022 : : "(SELECT oid FROM pg_namespace "
7023 : : "WHERE nspname = 'pg_catalog')"
7024 : : "\n OR EXISTS (SELECT 1 FROM pg_cast"
7025 : : "\n WHERE pg_cast.oid > %u "
7026 : : "\n AND p.oid = pg_cast.castfunc)"
7027 : : "\n OR EXISTS (SELECT 1 FROM pg_transform"
7028 : : "\n WHERE pg_transform.oid > %u AND "
7029 : : "\n (p.oid = pg_transform.trffromsql"
7030 : : "\n OR p.oid = pg_transform.trftosql))",
7031 : : not_agg_check,
7032 : : g_last_builtin_oid,
7033 : : g_last_builtin_oid);
7034 [ + + ]: 215 : if (dopt->binary_upgrade)
7035 : 44 : appendPQExpBufferStr(query,
7036 : : "\n OR EXISTS(SELECT 1 FROM pg_depend WHERE "
7037 : : "classid = 'pg_proc'::regclass AND "
7038 : : "objid = p.oid AND "
7039 : : "refclassid = 'pg_extension'::regclass AND "
7040 : : "deptype = 'e')");
7041 : 215 : appendPQExpBufferStr(query,
7042 : : "\n OR p.proacl IS DISTINCT FROM pip.initprivs");
7043 : 215 : appendPQExpBufferChar(query, ')');
7044 : :
7045 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7046 : :
7047 : 215 : ntups = PQntuples(res);
7048 : :
7049 : 215 : finfo = pg_malloc0_array(FuncInfo, ntups);
7050 : :
7051 : 215 : i_tableoid = PQfnumber(res, "tableoid");
7052 : 215 : i_oid = PQfnumber(res, "oid");
7053 : 215 : i_proname = PQfnumber(res, "proname");
7054 : 215 : i_pronamespace = PQfnumber(res, "pronamespace");
7055 : 215 : i_proowner = PQfnumber(res, "proowner");
7056 : 215 : i_prolang = PQfnumber(res, "prolang");
7057 : 215 : i_pronargs = PQfnumber(res, "pronargs");
7058 : 215 : i_proargtypes = PQfnumber(res, "proargtypes");
7059 : 215 : i_prorettype = PQfnumber(res, "prorettype");
7060 : 215 : i_proacl = PQfnumber(res, "proacl");
7061 : 215 : i_acldefault = PQfnumber(res, "acldefault");
7062 : :
7063 [ + + ]: 5409 : for (i = 0; i < ntups; i++)
7064 : : {
7065 : 5194 : finfo[i].dobj.objType = DO_FUNC;
7066 : 5194 : finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7067 : 5194 : finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7068 : 5194 : AssignDumpId(&finfo[i].dobj);
7069 : 5194 : finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
7070 : 10388 : finfo[i].dobj.namespace =
7071 : 5194 : findNamespace(atooid(PQgetvalue(res, i, i_pronamespace)));
7072 : 5194 : finfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_proacl));
7073 : 5194 : finfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7074 : 5194 : finfo[i].dacl.privtype = 0;
7075 : 5194 : finfo[i].dacl.initprivs = NULL;
7076 : 5194 : finfo[i].rolname = getRoleName(PQgetvalue(res, i, i_proowner));
7077 : 5194 : finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
7078 : 5194 : finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
7079 : 5194 : finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
7080 [ + + ]: 5194 : if (finfo[i].nargs == 0)
7081 : 1104 : finfo[i].argtypes = NULL;
7082 : : else
7083 : 4090 : finfo[i].argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
7084 : 4090 : finfo[i].nargs);
7085 : 5194 : finfo[i].postponed_def = false; /* might get set during sort */
7086 : :
7087 : : /* Decide whether we want to dump it */
7088 : 5194 : selectDumpableObject(&(finfo[i].dobj), fout);
7089 : :
7090 : : /* Mark whether function has an ACL */
7091 [ + + ]: 5194 : if (!PQgetisnull(res, i, i_proacl))
7092 : 146 : finfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7093 : : }
7094 : :
7095 : 215 : PQclear(res);
7096 : :
7097 : 215 : destroyPQExpBuffer(query);
7098 : 215 : }
7099 : :
7100 : : /*
7101 : : * getRelationStatistics
7102 : : * register the statistics object as a dependent of the relation.
7103 : : *
7104 : : * reltuples is passed as a string to avoid complexities in converting from/to
7105 : : * floating point.
7106 : : */
7107 : : static RelStatsInfo *
7108 : 10476 : getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
7109 : : char *reltuples, int32 relallvisible,
7110 : : int32 relallfrozen, char relkind,
7111 : : char **indAttNames, int nindAttNames)
7112 : : {
7113 [ + + ]: 10476 : if (!fout->dopt->dumpStatistics)
7114 : 6428 : return NULL;
7115 : :
7116 [ + + + + ]: 4048 : if ((relkind == RELKIND_RELATION) ||
7117 [ + + ]: 1632 : (relkind == RELKIND_PARTITIONED_TABLE) ||
7118 [ + + ]: 956 : (relkind == RELKIND_INDEX) ||
7119 [ + + ]: 619 : (relkind == RELKIND_PARTITIONED_INDEX) ||
7120 [ + + ]: 288 : (relkind == RELKIND_MATVIEW ||
7121 : : relkind == RELKIND_FOREIGN_TABLE))
7122 : : {
7123 : 3796 : RelStatsInfo *info = pg_malloc0_object(RelStatsInfo);
7124 : 3796 : DumpableObject *dobj = &info->dobj;
7125 : :
7126 : 3796 : dobj->objType = DO_REL_STATS;
7127 : 3796 : dobj->catId.tableoid = 0;
7128 : 3796 : dobj->catId.oid = 0;
7129 : 3796 : AssignDumpId(dobj);
7130 : 3796 : dobj->dependencies = pg_malloc_object(DumpId);
7131 : 3796 : dobj->dependencies[0] = rel->dumpId;
7132 : 3796 : dobj->nDeps = 1;
7133 : 3796 : dobj->allocDeps = 1;
7134 : 3796 : dobj->components |= DUMP_COMPONENT_STATISTICS;
7135 : 3796 : dobj->name = pg_strdup(rel->name);
7136 : 3796 : dobj->namespace = rel->namespace;
7137 : 3796 : info->relid = rel->catId.oid;
7138 : 3796 : info->relpages = relpages;
7139 : 3796 : info->reltuples = pstrdup(reltuples);
7140 : 3796 : info->relallvisible = relallvisible;
7141 : 3796 : info->relallfrozen = relallfrozen;
7142 : 3796 : info->relkind = relkind;
7143 : 3796 : info->indAttNames = indAttNames;
7144 : 3796 : info->nindAttNames = nindAttNames;
7145 : :
7146 : : /*
7147 : : * Ordinarily, stats go in SECTION_DATA for tables and
7148 : : * SECTION_POST_DATA for indexes.
7149 : : *
7150 : : * However, the section may be updated later for materialized view
7151 : : * stats. REFRESH MATERIALIZED VIEW replaces the storage and resets
7152 : : * the stats, so the stats must be restored after the data. Also, the
7153 : : * materialized view definition may be postponed to SECTION_POST_DATA
7154 : : * (see repairMatViewBoundaryMultiLoop()).
7155 : : */
7156 [ + + - ]: 3796 : switch (info->relkind)
7157 : : {
7158 : 2783 : case RELKIND_RELATION:
7159 : : case RELKIND_PARTITIONED_TABLE:
7160 : : case RELKIND_MATVIEW:
7161 : : case RELKIND_FOREIGN_TABLE:
7162 : 2783 : info->section = SECTION_DATA;
7163 : 2783 : break;
7164 : 1013 : case RELKIND_INDEX:
7165 : : case RELKIND_PARTITIONED_INDEX:
7166 : 1013 : info->section = SECTION_POST_DATA;
7167 : 1013 : break;
7168 : 0 : default:
7169 : 0 : pg_fatal("cannot dump statistics for relation kind \"%c\"",
7170 : : info->relkind);
7171 : : }
7172 : :
7173 : 3796 : return info;
7174 : : }
7175 : 252 : return NULL;
7176 : : }
7177 : :
7178 : : /*
7179 : : * getTables
7180 : : * read all the tables (no indexes) in the system catalogs,
7181 : : * and return them as an array of TableInfo structures
7182 : : *
7183 : : * *numTables is set to the number of tables read in
7184 : : */
7185 : : TableInfo *
7186 : 216 : getTables(Archive *fout, int *numTables)
7187 : : {
7188 : 216 : DumpOptions *dopt = fout->dopt;
7189 : : PGresult *res;
7190 : : int ntups;
7191 : : int i;
7192 : 216 : PQExpBuffer query = createPQExpBuffer();
7193 : : TableInfo *tblinfo;
7194 : : int i_reltableoid;
7195 : : int i_reloid;
7196 : : int i_relname;
7197 : : int i_relnamespace;
7198 : : int i_relkind;
7199 : : int i_reltype;
7200 : : int i_relowner;
7201 : : int i_relchecks;
7202 : : int i_relhasindex;
7203 : : int i_relhasrules;
7204 : : int i_relpages;
7205 : : int i_reltuples;
7206 : : int i_relallvisible;
7207 : : int i_relallfrozen;
7208 : : int i_toastpages;
7209 : : int i_owning_tab;
7210 : : int i_owning_col;
7211 : : int i_reltablespace;
7212 : : int i_relhasoids;
7213 : : int i_relhastriggers;
7214 : : int i_relpersistence;
7215 : : int i_relispopulated;
7216 : : int i_relreplident;
7217 : : int i_relrowsec;
7218 : : int i_relforcerowsec;
7219 : : int i_relfrozenxid;
7220 : : int i_toastfrozenxid;
7221 : : int i_toastoid;
7222 : : int i_relminmxid;
7223 : : int i_toastminmxid;
7224 : : int i_reloptions;
7225 : : int i_checkoption;
7226 : : int i_toastreloptions;
7227 : : int i_reloftype;
7228 : : int i_foreignserver;
7229 : : int i_amname;
7230 : : int i_is_identity_sequence;
7231 : : int i_relacl;
7232 : : int i_acldefault;
7233 : : int i_ispartition;
7234 : :
7235 : : /*
7236 : : * Find all the tables and table-like objects.
7237 : : *
7238 : : * We must fetch all tables in this phase because otherwise we cannot
7239 : : * correctly identify inherited columns, owned sequences, etc.
7240 : : *
7241 : : * We include system catalogs, so that we can work if a user table is
7242 : : * defined to inherit from a system catalog (pretty weird, but...)
7243 : : *
7244 : : * Note: in this phase we should collect only a minimal amount of
7245 : : * information about each table, basically just enough to decide if it is
7246 : : * interesting. In particular, since we do not yet have lock on any user
7247 : : * table, we MUST NOT invoke any server-side data collection functions
7248 : : * (for instance, pg_get_partkeydef()). Those are likely to fail or give
7249 : : * wrong answers if any concurrent DDL is happening.
7250 : : */
7251 : :
7252 : 216 : appendPQExpBufferStr(query,
7253 : : "SELECT c.tableoid, c.oid, c.relname, "
7254 : : "c.relnamespace, c.relkind, c.reltype, "
7255 : : "c.relowner, "
7256 : : "c.relchecks, "
7257 : : "c.relhasindex, c.relhasrules, c.relpages, "
7258 : : "c.reltuples, c.relallvisible, ");
7259 : :
7260 [ + - ]: 216 : if (fout->remoteVersion >= 180000)
7261 : 216 : appendPQExpBufferStr(query, "c.relallfrozen, ");
7262 : : else
7263 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7264 : :
7265 : 216 : appendPQExpBufferStr(query,
7266 : : "c.relhastriggers, c.relpersistence, "
7267 : : "c.reloftype, "
7268 : : "c.relacl, "
7269 : : "acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
7270 : : " THEN 's'::\"char\" ELSE 'r'::\"char\" END, c.relowner) AS acldefault, "
7271 : : "CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN "
7272 : : "(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) "
7273 : : "ELSE 0 END AS foreignserver, "
7274 : : "c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, "
7275 : : "tc.oid AS toid, "
7276 : : "tc.relpages AS toastpages, "
7277 : : "tc.reloptions AS toast_reloptions, "
7278 : : "d.refobjid AS owning_tab, "
7279 : : "d.refobjsubid AS owning_col, "
7280 : : "tsp.spcname AS reltablespace, ");
7281 : :
7282 [ + - ]: 216 : if (fout->remoteVersion >= 120000)
7283 : 216 : appendPQExpBufferStr(query,
7284 : : "false AS relhasoids, ");
7285 : : else
7286 : 0 : appendPQExpBufferStr(query,
7287 : : "c.relhasoids, ");
7288 : :
7289 : 216 : appendPQExpBufferStr(query,
7290 : : "c.relispopulated, ");
7291 : :
7292 : 216 : appendPQExpBufferStr(query,
7293 : : "c.relreplident, ");
7294 : :
7295 : 216 : appendPQExpBufferStr(query,
7296 : : "c.relrowsecurity, c.relforcerowsecurity, ");
7297 : :
7298 : 216 : appendPQExpBufferStr(query,
7299 : : "c.relminmxid, tc.relminmxid AS tminmxid, ");
7300 : :
7301 : 216 : appendPQExpBufferStr(query,
7302 : : "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
7303 : : "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
7304 : : "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, ");
7305 : :
7306 : 216 : appendPQExpBufferStr(query,
7307 : : "am.amname, ");
7308 : :
7309 : 216 : appendPQExpBufferStr(query,
7310 : : "(d.deptype = 'i') IS TRUE AS is_identity_sequence, ");
7311 : :
7312 : 216 : appendPQExpBufferStr(query,
7313 : : "c.relispartition AS ispartition ");
7314 : :
7315 : : /*
7316 : : * Left join to pg_depend to pick up dependency info linking sequences to
7317 : : * their owning column, if any (note this dependency is AUTO except for
7318 : : * identity sequences, where it's INTERNAL). Also join to pg_tablespace to
7319 : : * collect the spcname.
7320 : : */
7321 : 216 : appendPQExpBufferStr(query,
7322 : : "\nFROM pg_class c\n"
7323 : : "LEFT JOIN pg_depend d ON "
7324 : : "(c.relkind = " CppAsString2(RELKIND_SEQUENCE) " AND "
7325 : : "d.classid = 'pg_class'::regclass AND d.objid = c.oid AND "
7326 : : "d.objsubid = 0 AND "
7327 : : "d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i'))\n"
7328 : : "LEFT JOIN pg_tablespace tsp ON (tsp.oid = c.reltablespace)\n");
7329 : :
7330 : : /*
7331 : : * Left join to pg_am to pick up the amname.
7332 : : */
7333 : 216 : appendPQExpBufferStr(query,
7334 : : "LEFT JOIN pg_am am ON (c.relam = am.oid)\n");
7335 : :
7336 : : /*
7337 : : * We purposefully ignore toast OIDs for partitioned tables; the reason is
7338 : : * that versions 10 and 11 have them, but later versions do not, so
7339 : : * emitting them causes the upgrade to fail.
7340 : : */
7341 : 216 : appendPQExpBufferStr(query,
7342 : : "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid"
7343 : : " AND tc.relkind = " CppAsString2(RELKIND_TOASTVALUE)
7344 : : " AND c.relkind <> " CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
7345 : :
7346 : : /*
7347 : : * Restrict to interesting relkinds (in particular, not indexes). Not all
7348 : : * relkinds are possible in older servers, but it's not worth the trouble
7349 : : * to emit a version-dependent list.
7350 : : *
7351 : : * Composite-type table entries won't be dumped as such, but we have to
7352 : : * make a DumpableObject for them so that we can track dependencies of the
7353 : : * composite type (pg_depend entries for columns of the composite type
7354 : : * link to the pg_class entry not the pg_type entry).
7355 : : */
7356 : 216 : appendPQExpBufferStr(query,
7357 : : "WHERE c.relkind IN ("
7358 : : CppAsString2(RELKIND_RELATION) ", "
7359 : : CppAsString2(RELKIND_SEQUENCE) ", "
7360 : : CppAsString2(RELKIND_VIEW) ", "
7361 : : CppAsString2(RELKIND_COMPOSITE_TYPE) ", "
7362 : : CppAsString2(RELKIND_MATVIEW) ", "
7363 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
7364 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n"
7365 : : "ORDER BY c.oid");
7366 : :
7367 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7368 : :
7369 : 216 : ntups = PQntuples(res);
7370 : :
7371 : 216 : *numTables = ntups;
7372 : :
7373 : : /*
7374 : : * Extract data from result and lock dumpable tables. We do the locking
7375 : : * before anything else, to minimize the window wherein a table could
7376 : : * disappear under us.
7377 : : *
7378 : : * Note that we have to save info about all tables here, even when dumping
7379 : : * only one, because we don't yet know which tables might be inheritance
7380 : : * ancestors of the target table.
7381 : : */
7382 : 216 : tblinfo = pg_malloc0_array(TableInfo, ntups);
7383 : :
7384 : 216 : i_reltableoid = PQfnumber(res, "tableoid");
7385 : 216 : i_reloid = PQfnumber(res, "oid");
7386 : 216 : i_relname = PQfnumber(res, "relname");
7387 : 216 : i_relnamespace = PQfnumber(res, "relnamespace");
7388 : 216 : i_relkind = PQfnumber(res, "relkind");
7389 : 216 : i_reltype = PQfnumber(res, "reltype");
7390 : 216 : i_relowner = PQfnumber(res, "relowner");
7391 : 216 : i_relchecks = PQfnumber(res, "relchecks");
7392 : 216 : i_relhasindex = PQfnumber(res, "relhasindex");
7393 : 216 : i_relhasrules = PQfnumber(res, "relhasrules");
7394 : 216 : i_relpages = PQfnumber(res, "relpages");
7395 : 216 : i_reltuples = PQfnumber(res, "reltuples");
7396 : 216 : i_relallvisible = PQfnumber(res, "relallvisible");
7397 : 216 : i_relallfrozen = PQfnumber(res, "relallfrozen");
7398 : 216 : i_toastpages = PQfnumber(res, "toastpages");
7399 : 216 : i_owning_tab = PQfnumber(res, "owning_tab");
7400 : 216 : i_owning_col = PQfnumber(res, "owning_col");
7401 : 216 : i_reltablespace = PQfnumber(res, "reltablespace");
7402 : 216 : i_relhasoids = PQfnumber(res, "relhasoids");
7403 : 216 : i_relhastriggers = PQfnumber(res, "relhastriggers");
7404 : 216 : i_relpersistence = PQfnumber(res, "relpersistence");
7405 : 216 : i_relispopulated = PQfnumber(res, "relispopulated");
7406 : 216 : i_relreplident = PQfnumber(res, "relreplident");
7407 : 216 : i_relrowsec = PQfnumber(res, "relrowsecurity");
7408 : 216 : i_relforcerowsec = PQfnumber(res, "relforcerowsecurity");
7409 : 216 : i_relfrozenxid = PQfnumber(res, "relfrozenxid");
7410 : 216 : i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
7411 : 216 : i_toastoid = PQfnumber(res, "toid");
7412 : 216 : i_relminmxid = PQfnumber(res, "relminmxid");
7413 : 216 : i_toastminmxid = PQfnumber(res, "tminmxid");
7414 : 216 : i_reloptions = PQfnumber(res, "reloptions");
7415 : 216 : i_checkoption = PQfnumber(res, "checkoption");
7416 : 216 : i_toastreloptions = PQfnumber(res, "toast_reloptions");
7417 : 216 : i_reloftype = PQfnumber(res, "reloftype");
7418 : 216 : i_foreignserver = PQfnumber(res, "foreignserver");
7419 : 216 : i_amname = PQfnumber(res, "amname");
7420 : 216 : i_is_identity_sequence = PQfnumber(res, "is_identity_sequence");
7421 : 216 : i_relacl = PQfnumber(res, "relacl");
7422 : 216 : i_acldefault = PQfnumber(res, "acldefault");
7423 : 216 : i_ispartition = PQfnumber(res, "ispartition");
7424 : :
7425 [ + + ]: 216 : if (dopt->lockWaitTimeout)
7426 : : {
7427 : : /*
7428 : : * Arrange to fail instead of waiting forever for a table lock.
7429 : : *
7430 : : * NB: this coding assumes that the only queries issued within the
7431 : : * following loop are LOCK TABLEs; else the timeout may be undesirably
7432 : : * applied to other things too.
7433 : : */
7434 : 2 : resetPQExpBuffer(query);
7435 : 2 : appendPQExpBufferStr(query, "SET statement_timeout = ");
7436 : 2 : appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout));
7437 : 2 : ExecuteSqlStatement(fout, query->data);
7438 : : }
7439 : :
7440 : 216 : resetPQExpBuffer(query);
7441 : :
7442 [ + + ]: 57877 : for (i = 0; i < ntups; i++)
7443 : : {
7444 : 57661 : int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible));
7445 : 57661 : int32 relallfrozen = atoi(PQgetvalue(res, i, i_relallfrozen));
7446 : :
7447 : 57661 : tblinfo[i].dobj.objType = DO_TABLE;
7448 : 57661 : tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
7449 : 57661 : tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
7450 : 57661 : AssignDumpId(&tblinfo[i].dobj);
7451 : 57661 : tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
7452 : 115322 : tblinfo[i].dobj.namespace =
7453 : 57661 : findNamespace(atooid(PQgetvalue(res, i, i_relnamespace)));
7454 : 57661 : tblinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_relacl));
7455 : 57661 : tblinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7456 : 57661 : tblinfo[i].dacl.privtype = 0;
7457 : 57661 : tblinfo[i].dacl.initprivs = NULL;
7458 : 57661 : tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
7459 : 57661 : tblinfo[i].reltype = atooid(PQgetvalue(res, i, i_reltype));
7460 : 57661 : tblinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_relowner));
7461 : 57661 : tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
7462 : 57661 : tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
7463 : 57661 : tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
7464 : 57661 : tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages));
7465 [ + + ]: 57661 : if (PQgetisnull(res, i, i_toastpages))
7466 : 46683 : tblinfo[i].toastpages = 0;
7467 : : else
7468 : 10978 : tblinfo[i].toastpages = atoi(PQgetvalue(res, i, i_toastpages));
7469 [ + + ]: 57661 : if (PQgetisnull(res, i, i_owning_tab))
7470 : : {
7471 : 57221 : tblinfo[i].owning_tab = InvalidOid;
7472 : 57221 : tblinfo[i].owning_col = 0;
7473 : : }
7474 : : else
7475 : : {
7476 : 440 : tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
7477 : 440 : tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
7478 : : }
7479 : 57661 : tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace));
7480 : 57661 : tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
7481 : 57661 : tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
7482 : 57661 : tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence));
7483 : 57661 : tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0);
7484 : 57661 : tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident));
7485 : 57661 : tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0);
7486 : 57661 : tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0);
7487 : 57661 : tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
7488 : 57661 : tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
7489 : 57661 : tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
7490 : 57661 : tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid));
7491 : 57661 : tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid));
7492 : 57661 : tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions));
7493 [ + + ]: 57661 : if (PQgetisnull(res, i, i_checkoption))
7494 : 57612 : tblinfo[i].checkoption = NULL;
7495 : : else
7496 : 49 : tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption));
7497 : 57661 : tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions));
7498 : 57661 : tblinfo[i].reloftype = atooid(PQgetvalue(res, i, i_reloftype));
7499 : 57661 : tblinfo[i].foreign_server = atooid(PQgetvalue(res, i, i_foreignserver));
7500 [ + + ]: 57661 : if (PQgetisnull(res, i, i_amname))
7501 : 35266 : tblinfo[i].amname = NULL;
7502 : : else
7503 : 22395 : tblinfo[i].amname = pg_strdup(PQgetvalue(res, i, i_amname));
7504 : 57661 : tblinfo[i].is_identity_sequence = (strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0);
7505 : 57661 : tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0);
7506 : :
7507 : : /* other fields were zeroed above */
7508 : :
7509 : : /*
7510 : : * Decide whether we want to dump this table.
7511 : : */
7512 [ + + ]: 57661 : if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
7513 : 248 : tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE;
7514 : : else
7515 : 57413 : selectDumpableTable(&tblinfo[i], fout);
7516 : :
7517 : : /*
7518 : : * Now, consider the table "interesting" if we need to dump its
7519 : : * definition, data or its statistics. Later on, we'll skip a lot of
7520 : : * data collection for uninteresting tables.
7521 : : *
7522 : : * Note: the "interesting" flag will also be set by flagInhTables for
7523 : : * parents of interesting tables, so that we collect necessary
7524 : : * inheritance info even when the parents are not themselves being
7525 : : * dumped. This is the main reason why we need an "interesting" flag
7526 : : * that's separate from the components-to-dump bitmask.
7527 : : */
7528 : 57661 : tblinfo[i].interesting = (tblinfo[i].dobj.dump &
7529 : : (DUMP_COMPONENT_DEFINITION |
7530 : : DUMP_COMPONENT_DATA |
7531 : 57661 : DUMP_COMPONENT_STATISTICS)) != 0;
7532 : :
7533 : 57661 : tblinfo[i].dummy_view = false; /* might get set during sort */
7534 : 57661 : tblinfo[i].postponed_def = false; /* might get set during sort */
7535 : :
7536 : : /* Tables have data */
7537 : 57661 : tblinfo[i].dobj.components |= DUMP_COMPONENT_DATA;
7538 : :
7539 : : /* Mark whether table has an ACL */
7540 [ + + ]: 57661 : if (!PQgetisnull(res, i, i_relacl))
7541 : 46810 : tblinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7542 : 57661 : tblinfo[i].hascolumnACLs = false; /* may get set later */
7543 : :
7544 : : /* Add statistics */
7545 [ + + ]: 57661 : if (tblinfo[i].interesting)
7546 : : {
7547 : : RelStatsInfo *stats;
7548 : :
7549 : 15352 : stats = getRelationStatistics(fout, &tblinfo[i].dobj,
7550 : 7676 : tblinfo[i].relpages,
7551 : : PQgetvalue(res, i, i_reltuples),
7552 : : relallvisible, relallfrozen,
7553 : 7676 : tblinfo[i].relkind, NULL, 0);
7554 [ + + ]: 7676 : if (tblinfo[i].relkind == RELKIND_MATVIEW)
7555 : 426 : tblinfo[i].stats = stats;
7556 : : }
7557 : :
7558 : : /*
7559 : : * Read-lock target tables to make sure they aren't DROPPED or altered
7560 : : * in schema before we get around to dumping them.
7561 : : *
7562 : : * Note that we don't explicitly lock parents of the target tables; we
7563 : : * assume our lock on the child is enough to prevent schema
7564 : : * alterations to parent tables.
7565 : : *
7566 : : * NOTE: it'd be kinda nice to lock other relations too, not only
7567 : : * plain or partitioned tables, but the backend doesn't presently
7568 : : * allow that.
7569 : : *
7570 : : * We only need to lock the table for certain components; see
7571 : : * pg_dump.h
7572 : : */
7573 [ + + ]: 57661 : if ((tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK) &&
7574 [ + + ]: 7676 : (tblinfo[i].relkind == RELKIND_RELATION ||
7575 [ + + ]: 2128 : tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE))
7576 : : {
7577 : : /*
7578 : : * Tables are locked in batches. When dumping from a remote
7579 : : * server this can save a significant amount of time by reducing
7580 : : * the number of round trips.
7581 : : */
7582 [ + + ]: 6200 : if (query->len == 0)
7583 : 144 : appendPQExpBuffer(query, "LOCK TABLE %s",
7584 : 144 : fmtQualifiedDumpable(&tblinfo[i]));
7585 : : else
7586 : : {
7587 : 6056 : appendPQExpBuffer(query, ", %s",
7588 : 6056 : fmtQualifiedDumpable(&tblinfo[i]));
7589 : :
7590 : : /* Arbitrarily end a batch when query length reaches 100K. */
7591 [ - + ]: 6056 : if (query->len >= 100000)
7592 : : {
7593 : : /* Lock another batch of tables. */
7594 : 0 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7595 : 0 : ExecuteSqlStatement(fout, query->data);
7596 : 0 : resetPQExpBuffer(query);
7597 : : }
7598 : : }
7599 : : }
7600 : : }
7601 : :
7602 [ + + ]: 216 : if (query->len != 0)
7603 : : {
7604 : : /* Lock the tables in the last batch. */
7605 : 144 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7606 : 144 : ExecuteSqlStatement(fout, query->data);
7607 : : }
7608 : :
7609 [ + + ]: 215 : if (dopt->lockWaitTimeout)
7610 : : {
7611 : 2 : ExecuteSqlStatement(fout, "SET statement_timeout = 0");
7612 : : }
7613 : :
7614 : 215 : PQclear(res);
7615 : :
7616 : 215 : destroyPQExpBuffer(query);
7617 : :
7618 : 215 : return tblinfo;
7619 : : }
7620 : :
7621 : : /*
7622 : : * getOwnedSeqs
7623 : : * identify owned sequences and mark them as dumpable if owning table is
7624 : : *
7625 : : * We used to do this in getTables(), but it's better to do it after the
7626 : : * index used by findTableByOid() has been set up.
7627 : : */
7628 : : void
7629 : 215 : getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables)
7630 : : {
7631 : : int i;
7632 : :
7633 : : /*
7634 : : * Force sequences that are "owned" by table columns to be dumped whenever
7635 : : * their owning table is being dumped.
7636 : : */
7637 [ + + ]: 57593 : for (i = 0; i < numTables; i++)
7638 : : {
7639 : 57378 : TableInfo *seqinfo = &tblinfo[i];
7640 : : TableInfo *owning_tab;
7641 : :
7642 [ + + ]: 57378 : if (!OidIsValid(seqinfo->owning_tab))
7643 : 56941 : continue; /* not an owned sequence */
7644 : :
7645 : 437 : owning_tab = findTableByOid(seqinfo->owning_tab);
7646 [ - + ]: 437 : if (owning_tab == NULL)
7647 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
7648 : : seqinfo->owning_tab, seqinfo->dobj.catId.oid);
7649 : :
7650 : : /*
7651 : : * For an identity sequence, dump exactly the same components for the
7652 : : * sequence as for the owning table. This is important because we
7653 : : * treat the identity sequence as an integral part of the table. For
7654 : : * example, there is not any DDL command that allows creation of such
7655 : : * a sequence independently of the table.
7656 : : *
7657 : : * For other owned sequences such as serial sequences, we need to dump
7658 : : * the components that are being dumped for the table and any
7659 : : * components that the sequence is explicitly marked with.
7660 : : *
7661 : : * We can't simply use the set of components which are being dumped
7662 : : * for the table as the table might be in an extension (and only the
7663 : : * non-extension components, eg: ACLs if changed, security labels, and
7664 : : * policies, are being dumped) while the sequence is not (and
7665 : : * therefore the definition and other components should also be
7666 : : * dumped).
7667 : : *
7668 : : * If the sequence is part of the extension then it should be properly
7669 : : * marked by checkExtensionMembership() and this will be a no-op as
7670 : : * the table will be equivalently marked.
7671 : : */
7672 [ + + ]: 437 : if (seqinfo->is_identity_sequence)
7673 : 218 : seqinfo->dobj.dump = owning_tab->dobj.dump;
7674 : : else
7675 : 219 : seqinfo->dobj.dump |= owning_tab->dobj.dump;
7676 : :
7677 : : /* Make sure that necessary data is available if we're dumping it */
7678 [ + + ]: 437 : if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE)
7679 : : {
7680 : 341 : seqinfo->interesting = true;
7681 : 341 : owning_tab->interesting = true;
7682 : : }
7683 : : }
7684 : 215 : }
7685 : :
7686 : : /*
7687 : : * getInherits
7688 : : * read all the inheritance information
7689 : : * from the system catalogs return them in the InhInfo* structure
7690 : : *
7691 : : * numInherits is set to the number of pairs read in
7692 : : */
7693 : : InhInfo *
7694 : 215 : getInherits(Archive *fout, int *numInherits)
7695 : : {
7696 : : PGresult *res;
7697 : : int ntups;
7698 : : int i;
7699 : 215 : PQExpBuffer query = createPQExpBuffer();
7700 : : InhInfo *inhinfo;
7701 : :
7702 : : int i_inhrelid;
7703 : : int i_inhparent;
7704 : :
7705 : : /* find all the inheritance information */
7706 : 215 : appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits");
7707 : :
7708 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7709 : :
7710 : 215 : ntups = PQntuples(res);
7711 : :
7712 : 215 : *numInherits = ntups;
7713 : :
7714 : 215 : inhinfo = pg_malloc_array(InhInfo, ntups);
7715 : :
7716 : 215 : i_inhrelid = PQfnumber(res, "inhrelid");
7717 : 215 : i_inhparent = PQfnumber(res, "inhparent");
7718 : :
7719 [ + + ]: 3862 : for (i = 0; i < ntups; i++)
7720 : : {
7721 : 3647 : inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
7722 : 3647 : inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
7723 : : }
7724 : :
7725 : 215 : PQclear(res);
7726 : :
7727 : 215 : destroyPQExpBuffer(query);
7728 : :
7729 : 215 : return inhinfo;
7730 : : }
7731 : :
7732 : : /*
7733 : : * getPartitioningInfo
7734 : : * get information about partitioning
7735 : : *
7736 : : * For the most part, we only collect partitioning info about tables we
7737 : : * intend to dump. However, this function has to consider all partitioned
7738 : : * tables in the database, because we need to know about parents of partitions
7739 : : * we are going to dump even if the parents themselves won't be dumped.
7740 : : *
7741 : : * Specifically, what we need to know is whether each partitioned table
7742 : : * has an "unsafe" partitioning scheme that requires us to force
7743 : : * load-via-partition-root mode for its children. Currently the only case
7744 : : * for which we force that is hash partitioning on enum columns, since the
7745 : : * hash codes depend on enum value OIDs which won't be replicated across
7746 : : * dump-and-reload. There are other cases in which load-via-partition-root
7747 : : * might be necessary, but we expect users to cope with them.
7748 : : */
7749 : : void
7750 : 215 : getPartitioningInfo(Archive *fout)
7751 : : {
7752 : : PQExpBuffer query;
7753 : : PGresult *res;
7754 : : int ntups;
7755 : :
7756 : : /* hash partitioning didn't exist before v11 */
7757 [ - + ]: 215 : if (fout->remoteVersion < 110000)
7758 : 0 : return;
7759 : : /* needn't bother if not dumping data */
7760 [ + + ]: 215 : if (!fout->dopt->dumpData)
7761 : 49 : return;
7762 : :
7763 : 166 : query = createPQExpBuffer();
7764 : :
7765 : : /*
7766 : : * Unsafe partitioning schemes are exactly those for which hash enum_ops
7767 : : * appears among the partition opclasses. We needn't check partstrat.
7768 : : *
7769 : : * Note that this query may well retrieve info about tables we aren't
7770 : : * going to dump and hence have no lock on. That's okay since we need not
7771 : : * invoke any unsafe server-side functions.
7772 : : */
7773 : 166 : appendPQExpBufferStr(query,
7774 : : "SELECT partrelid FROM pg_partitioned_table WHERE\n"
7775 : : "(SELECT c.oid FROM pg_opclass c JOIN pg_am a "
7776 : : "ON c.opcmethod = a.oid\n"
7777 : : "WHERE opcname = 'enum_ops' "
7778 : : "AND opcnamespace = 'pg_catalog'::regnamespace "
7779 : : "AND amname = 'hash') = ANY(partclass)");
7780 : :
7781 : 166 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7782 : :
7783 : 166 : ntups = PQntuples(res);
7784 : :
7785 [ + + ]: 227 : for (int i = 0; i < ntups; i++)
7786 : : {
7787 : 61 : Oid tabrelid = atooid(PQgetvalue(res, i, 0));
7788 : : TableInfo *tbinfo;
7789 : :
7790 : 61 : tbinfo = findTableByOid(tabrelid);
7791 [ - + ]: 61 : if (tbinfo == NULL)
7792 : 0 : pg_fatal("failed sanity check, table OID %u appearing in pg_partitioned_table not found",
7793 : : tabrelid);
7794 : 61 : tbinfo->unsafe_partitions = true;
7795 : : }
7796 : :
7797 : 166 : PQclear(res);
7798 : :
7799 : 166 : destroyPQExpBuffer(query);
7800 : : }
7801 : :
7802 : : /*
7803 : : * getIndexes
7804 : : * get information about every index on a dumpable table
7805 : : *
7806 : : * Note: index data is not returned directly to the caller, but it
7807 : : * does get entered into the DumpableObject tables.
7808 : : */
7809 : : void
7810 : 215 : getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
7811 : : {
7812 : 215 : PQExpBuffer query = createPQExpBuffer();
7813 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
7814 : : PGresult *res;
7815 : : int ntups;
7816 : : int curtblindx;
7817 : : IndxInfo *indxinfo;
7818 : : int i_tableoid,
7819 : : i_oid,
7820 : : i_indrelid,
7821 : : i_indexname,
7822 : : i_relpages,
7823 : : i_reltuples,
7824 : : i_relallvisible,
7825 : : i_relallfrozen,
7826 : : i_parentidx,
7827 : : i_indexdef,
7828 : : i_indnkeyatts,
7829 : : i_indnatts,
7830 : : i_indkey,
7831 : : i_indisclustered,
7832 : : i_indisreplident,
7833 : : i_indnullsnotdistinct,
7834 : : i_contype,
7835 : : i_conname,
7836 : : i_condeferrable,
7837 : : i_condeferred,
7838 : : i_conperiod,
7839 : : i_contableoid,
7840 : : i_conoid,
7841 : : i_condef,
7842 : : i_indattnames,
7843 : : i_tablespace,
7844 : : i_indreloptions,
7845 : : i_indstatcols,
7846 : : i_indstatvals;
7847 : :
7848 : : /*
7849 : : * We want to perform just one query against pg_index. However, we
7850 : : * mustn't try to select every row of the catalog and then sort it out on
7851 : : * the client side, because some of the server-side functions we need
7852 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
7853 : : * build an array of the OIDs of tables we care about (and now have lock
7854 : : * on!), and use a WHERE clause to constrain which rows are selected.
7855 : : */
7856 : 215 : appendPQExpBufferChar(tbloids, '{');
7857 [ + + ]: 57593 : for (int i = 0; i < numTables; i++)
7858 : : {
7859 : 57378 : TableInfo *tbinfo = &tblinfo[i];
7860 : :
7861 [ + + ]: 57378 : if (!tbinfo->hasindex)
7862 : 41017 : continue;
7863 : :
7864 : : /*
7865 : : * We can ignore indexes of uninteresting tables.
7866 : : */
7867 [ + + ]: 16361 : if (!tbinfo->interesting)
7868 : 14192 : continue;
7869 : :
7870 : : /* OK, we need info for this table */
7871 [ + + ]: 2169 : if (tbloids->len > 1) /* do we have more than the '{'? */
7872 : 2074 : appendPQExpBufferChar(tbloids, ',');
7873 : 2169 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
7874 : : }
7875 : 215 : appendPQExpBufferChar(tbloids, '}');
7876 : :
7877 : 215 : appendPQExpBufferStr(query,
7878 : : "SELECT t.tableoid, t.oid, i.indrelid, "
7879 : : "t.relname AS indexname, "
7880 : : "t.relpages, t.reltuples, t.relallvisible, ");
7881 : :
7882 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
7883 : 215 : appendPQExpBufferStr(query, "t.relallfrozen, ");
7884 : : else
7885 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7886 : :
7887 : 215 : appendPQExpBufferStr(query,
7888 : : "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
7889 : : "i.indkey, i.indisclustered, "
7890 : : "c.contype, c.conname, "
7891 : : "c.condeferrable, c.condeferred, "
7892 : : "c.tableoid AS contableoid, "
7893 : : "c.oid AS conoid, "
7894 : : "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
7895 : : "CASE WHEN i.indexprs IS NOT NULL THEN "
7896 : : "(SELECT pg_catalog.array_agg(attname ORDER BY attnum)"
7897 : : " FROM pg_catalog.pg_attribute "
7898 : : " WHERE attrelid = i.indexrelid) "
7899 : : "ELSE NULL END AS indattnames, "
7900 : : "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
7901 : : "t.reloptions AS indreloptions, ");
7902 : :
7903 : :
7904 : 215 : appendPQExpBufferStr(query,
7905 : : "i.indisreplident, ");
7906 : :
7907 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
7908 : 215 : appendPQExpBufferStr(query,
7909 : : "inh.inhparent AS parentidx, "
7910 : : "i.indnkeyatts AS indnkeyatts, "
7911 : : "i.indnatts AS indnatts, "
7912 : : "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) "
7913 : : " FROM pg_catalog.pg_attribute "
7914 : : " WHERE attrelid = i.indexrelid AND "
7915 : : " attstattarget >= 0) AS indstatcols, "
7916 : : "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) "
7917 : : " FROM pg_catalog.pg_attribute "
7918 : : " WHERE attrelid = i.indexrelid AND "
7919 : : " attstattarget >= 0) AS indstatvals, ");
7920 : : else
7921 : 0 : appendPQExpBufferStr(query,
7922 : : "0 AS parentidx, "
7923 : : "i.indnatts AS indnkeyatts, "
7924 : : "i.indnatts AS indnatts, "
7925 : : "'' AS indstatcols, "
7926 : : "'' AS indstatvals, ");
7927 : :
7928 [ + - ]: 215 : if (fout->remoteVersion >= 150000)
7929 : 215 : appendPQExpBufferStr(query,
7930 : : "i.indnullsnotdistinct, ");
7931 : : else
7932 : 0 : appendPQExpBufferStr(query,
7933 : : "false AS indnullsnotdistinct, ");
7934 : :
7935 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
7936 : 215 : appendPQExpBufferStr(query,
7937 : : "c.conperiod ");
7938 : : else
7939 : 0 : appendPQExpBufferStr(query,
7940 : : "NULL AS conperiod ");
7941 : :
7942 : : /*
7943 : : * The point of the messy-looking outer join is to find a constraint that
7944 : : * is related by an internal dependency link to the index. If we find one,
7945 : : * create a CONSTRAINT entry linked to the INDEX entry. We assume an
7946 : : * index won't have more than one internal dependency.
7947 : : *
7948 : : * Note: the check on conrelid is redundant, but useful because that
7949 : : * column is indexed while conindid is not.
7950 : : */
7951 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
7952 : : {
7953 : 215 : appendPQExpBuffer(query,
7954 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
7955 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
7956 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
7957 : : "JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) "
7958 : : "LEFT JOIN pg_catalog.pg_constraint c "
7959 : : "ON (i.indrelid = c.conrelid AND "
7960 : : "i.indexrelid = c.conindid AND "
7961 : : "c.contype IN ('p','u','x')) "
7962 : : "LEFT JOIN pg_catalog.pg_inherits inh "
7963 : : "ON (inh.inhrelid = indexrelid) "
7964 : : "WHERE (i.indisvalid OR t2.relkind = 'p') "
7965 : : "AND i.indisready "
7966 : : "ORDER BY i.indrelid, indexname",
7967 : : tbloids->data);
7968 : : }
7969 : : else
7970 : : {
7971 : : /*
7972 : : * the test on indisready is necessary in 9.2, and harmless in
7973 : : * earlier/later versions
7974 : : */
7975 : 0 : appendPQExpBuffer(query,
7976 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
7977 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
7978 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
7979 : : "LEFT JOIN pg_catalog.pg_constraint c "
7980 : : "ON (i.indrelid = c.conrelid AND "
7981 : : "i.indexrelid = c.conindid AND "
7982 : : "c.contype IN ('p','u','x')) "
7983 : : "WHERE i.indisvalid AND i.indisready "
7984 : : "ORDER BY i.indrelid, indexname",
7985 : : tbloids->data);
7986 : : }
7987 : :
7988 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7989 : :
7990 : 215 : ntups = PQntuples(res);
7991 : :
7992 : 215 : i_tableoid = PQfnumber(res, "tableoid");
7993 : 215 : i_oid = PQfnumber(res, "oid");
7994 : 215 : i_indrelid = PQfnumber(res, "indrelid");
7995 : 215 : i_indexname = PQfnumber(res, "indexname");
7996 : 215 : i_relpages = PQfnumber(res, "relpages");
7997 : 215 : i_reltuples = PQfnumber(res, "reltuples");
7998 : 215 : i_relallvisible = PQfnumber(res, "relallvisible");
7999 : 215 : i_relallfrozen = PQfnumber(res, "relallfrozen");
8000 : 215 : i_parentidx = PQfnumber(res, "parentidx");
8001 : 215 : i_indexdef = PQfnumber(res, "indexdef");
8002 : 215 : i_indnkeyatts = PQfnumber(res, "indnkeyatts");
8003 : 215 : i_indnatts = PQfnumber(res, "indnatts");
8004 : 215 : i_indkey = PQfnumber(res, "indkey");
8005 : 215 : i_indisclustered = PQfnumber(res, "indisclustered");
8006 : 215 : i_indisreplident = PQfnumber(res, "indisreplident");
8007 : 215 : i_indnullsnotdistinct = PQfnumber(res, "indnullsnotdistinct");
8008 : 215 : i_contype = PQfnumber(res, "contype");
8009 : 215 : i_conname = PQfnumber(res, "conname");
8010 : 215 : i_condeferrable = PQfnumber(res, "condeferrable");
8011 : 215 : i_condeferred = PQfnumber(res, "condeferred");
8012 : 215 : i_conperiod = PQfnumber(res, "conperiod");
8013 : 215 : i_contableoid = PQfnumber(res, "contableoid");
8014 : 215 : i_conoid = PQfnumber(res, "conoid");
8015 : 215 : i_condef = PQfnumber(res, "condef");
8016 : 215 : i_indattnames = PQfnumber(res, "indattnames");
8017 : 215 : i_tablespace = PQfnumber(res, "tablespace");
8018 : 215 : i_indreloptions = PQfnumber(res, "indreloptions");
8019 : 215 : i_indstatcols = PQfnumber(res, "indstatcols");
8020 : 215 : i_indstatvals = PQfnumber(res, "indstatvals");
8021 : :
8022 : 215 : indxinfo = pg_malloc_array(IndxInfo, ntups);
8023 : :
8024 : : /*
8025 : : * Outer loop iterates once per table, not once per row. Incrementing of
8026 : : * j is handled by the inner loop.
8027 : : */
8028 : 215 : curtblindx = -1;
8029 [ + + ]: 2364 : for (int j = 0; j < ntups;)
8030 : : {
8031 : 2149 : Oid indrelid = atooid(PQgetvalue(res, j, i_indrelid));
8032 : 2149 : TableInfo *tbinfo = NULL;
8033 : : int numinds;
8034 : :
8035 : : /* Count rows for this table */
8036 [ + + ]: 2800 : for (numinds = 1; numinds < ntups - j; numinds++)
8037 [ + + ]: 2705 : if (atooid(PQgetvalue(res, j + numinds, i_indrelid)) != indrelid)
8038 : 2054 : break;
8039 : :
8040 : : /*
8041 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8042 : : * order.
8043 : : */
8044 [ + - ]: 28018 : while (++curtblindx < numTables)
8045 : : {
8046 : 28018 : tbinfo = &tblinfo[curtblindx];
8047 [ + + ]: 28018 : if (tbinfo->dobj.catId.oid == indrelid)
8048 : 2149 : break;
8049 : : }
8050 [ - + ]: 2149 : if (curtblindx >= numTables)
8051 : 0 : pg_fatal("unrecognized table OID %u", indrelid);
8052 : : /* cross-check that we only got requested tables */
8053 [ + - ]: 2149 : if (!tbinfo->hasindex ||
8054 [ - + ]: 2149 : !tbinfo->interesting)
8055 : 0 : pg_fatal("unexpected index data for table \"%s\"",
8056 : : tbinfo->dobj.name);
8057 : :
8058 : : /* Save data for this table */
8059 : 2149 : tbinfo->indexes = indxinfo + j;
8060 : 2149 : tbinfo->numIndexes = numinds;
8061 : :
8062 [ + + ]: 4949 : for (int c = 0; c < numinds; c++, j++)
8063 : : {
8064 : : char contype;
8065 : : char indexkind;
8066 : 2800 : char **indAttNames = NULL;
8067 : 2800 : int nindAttNames = 0;
8068 : : RelStatsInfo *relstats;
8069 : 2800 : int32 relpages = atoi(PQgetvalue(res, j, i_relpages));
8070 : 2800 : int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible));
8071 : 2800 : int32 relallfrozen = atoi(PQgetvalue(res, j, i_relallfrozen));
8072 : :
8073 : 2800 : indxinfo[j].dobj.objType = DO_INDEX;
8074 : 2800 : indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8075 : 2800 : indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8076 : 2800 : AssignDumpId(&indxinfo[j].dobj);
8077 : 2800 : indxinfo[j].dobj.dump = tbinfo->dobj.dump;
8078 : 2800 : indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname));
8079 : 2800 : indxinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8080 : 2800 : indxinfo[j].indextable = tbinfo;
8081 : 2800 : indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef));
8082 : 2800 : indxinfo[j].indnkeyattrs = atoi(PQgetvalue(res, j, i_indnkeyatts));
8083 : 2800 : indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts));
8084 : 2800 : indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
8085 : 2800 : indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions));
8086 : 2800 : indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols));
8087 : 2800 : indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals));
8088 : 2800 : indxinfo[j].indkeys = parseIntArray(PQgetvalue(res, j, i_indkey),
8089 : 2800 : indxinfo[j].indnattrs);
8090 : 2800 : indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
8091 : 2800 : indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
8092 : 2800 : indxinfo[j].indnullsnotdistinct = (PQgetvalue(res, j, i_indnullsnotdistinct)[0] == 't');
8093 : 2800 : indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx));
8094 : 2800 : indxinfo[j].partattaches = (SimplePtrList)
8095 : : {
8096 : : NULL, NULL
8097 : : };
8098 : :
8099 [ + + ]: 2800 : if (indxinfo[j].parentidx == 0)
8100 : 2175 : indexkind = RELKIND_INDEX;
8101 : : else
8102 : 625 : indexkind = RELKIND_PARTITIONED_INDEX;
8103 : :
8104 [ + + ]: 2800 : if (!PQgetisnull(res, j, i_indattnames))
8105 : : {
8106 [ - + ]: 208 : if (!parsePGArray(PQgetvalue(res, j, i_indattnames),
8107 : : &indAttNames, &nindAttNames))
8108 : 0 : pg_fatal("could not parse %s array", "indattnames");
8109 : : }
8110 : :
8111 : 2800 : relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages,
8112 : : PQgetvalue(res, j, i_reltuples),
8113 : : relallvisible, relallfrozen, indexkind,
8114 : : indAttNames, nindAttNames);
8115 : :
8116 : 2800 : contype = *(PQgetvalue(res, j, i_contype));
8117 [ + + + + : 2800 : if (contype == 'p' || contype == 'u' || contype == 'x')
+ + ]
8118 : 1618 : {
8119 : : /*
8120 : : * If we found a constraint matching the index, create an
8121 : : * entry for it.
8122 : : */
8123 : : ConstraintInfo *constrinfo;
8124 : :
8125 : 1618 : constrinfo = pg_malloc_object(ConstraintInfo);
8126 : 1618 : constrinfo->dobj.objType = DO_CONSTRAINT;
8127 : 1618 : constrinfo->dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8128 : 1618 : constrinfo->dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8129 : 1618 : AssignDumpId(&constrinfo->dobj);
8130 : 1618 : constrinfo->dobj.dump = tbinfo->dobj.dump;
8131 : 1618 : constrinfo->dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8132 : 1618 : constrinfo->dobj.namespace = tbinfo->dobj.namespace;
8133 : 1618 : constrinfo->contable = tbinfo;
8134 : 1618 : constrinfo->condomain = NULL;
8135 : 1618 : constrinfo->contype = contype;
8136 [ + + ]: 1618 : if (contype == 'x')
8137 : 61 : constrinfo->condef = pg_strdup(PQgetvalue(res, j, i_condef));
8138 : : else
8139 : 1557 : constrinfo->condef = NULL;
8140 : 1618 : constrinfo->confrelid = InvalidOid;
8141 : 1618 : constrinfo->conindex = indxinfo[j].dobj.dumpId;
8142 : 1618 : constrinfo->condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't';
8143 : 1618 : constrinfo->condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't';
8144 : 1618 : constrinfo->conperiod = *(PQgetvalue(res, j, i_conperiod)) == 't';
8145 : 1618 : constrinfo->conislocal = true;
8146 : 1618 : constrinfo->separate = true;
8147 : :
8148 : 1618 : indxinfo[j].indexconstraint = constrinfo->dobj.dumpId;
8149 [ + + ]: 1618 : if (relstats != NULL)
8150 : 600 : addObjectDependency(&relstats->dobj, constrinfo->dobj.dumpId);
8151 : : }
8152 : : else
8153 : : {
8154 : : /* Plain secondary index */
8155 : 1182 : indxinfo[j].indexconstraint = 0;
8156 : : }
8157 : : }
8158 : : }
8159 : :
8160 : 215 : PQclear(res);
8161 : :
8162 : 215 : destroyPQExpBuffer(query);
8163 : 215 : destroyPQExpBuffer(tbloids);
8164 : 215 : }
8165 : :
8166 : : /*
8167 : : * getExtendedStatistics
8168 : : * get information about extended-statistics objects.
8169 : : *
8170 : : * Note: extended statistics data is not returned directly to the caller, but
8171 : : * it does get entered into the DumpableObject tables.
8172 : : */
8173 : : void
8174 : 215 : getExtendedStatistics(Archive *fout)
8175 : : {
8176 : : PQExpBuffer query;
8177 : : PGresult *res;
8178 : : StatsExtInfo *statsextinfo;
8179 : : int ntups;
8180 : : int i_tableoid;
8181 : : int i_oid;
8182 : : int i_stxname;
8183 : : int i_stxnamespace;
8184 : : int i_stxowner;
8185 : : int i_stxrelid;
8186 : : int i_stattarget;
8187 : : int i;
8188 : :
8189 : 215 : query = createPQExpBuffer();
8190 : :
8191 [ - + ]: 215 : if (fout->remoteVersion < 130000)
8192 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8193 : : "stxnamespace, stxowner, stxrelid, NULL AS stxstattarget "
8194 : : "FROM pg_catalog.pg_statistic_ext");
8195 : : else
8196 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8197 : : "stxnamespace, stxowner, stxrelid, stxstattarget "
8198 : : "FROM pg_catalog.pg_statistic_ext");
8199 : :
8200 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8201 : :
8202 : 215 : ntups = PQntuples(res);
8203 : :
8204 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8205 : 215 : i_oid = PQfnumber(res, "oid");
8206 : 215 : i_stxname = PQfnumber(res, "stxname");
8207 : 215 : i_stxnamespace = PQfnumber(res, "stxnamespace");
8208 : 215 : i_stxowner = PQfnumber(res, "stxowner");
8209 : 215 : i_stxrelid = PQfnumber(res, "stxrelid");
8210 : 215 : i_stattarget = PQfnumber(res, "stxstattarget");
8211 : :
8212 : 215 : statsextinfo = pg_malloc_array(StatsExtInfo, ntups);
8213 : :
8214 [ + + ]: 448 : for (i = 0; i < ntups; i++)
8215 : : {
8216 : 233 : statsextinfo[i].dobj.objType = DO_STATSEXT;
8217 : 233 : statsextinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8218 : 233 : statsextinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8219 : 233 : AssignDumpId(&statsextinfo[i].dobj);
8220 : 233 : statsextinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_stxname));
8221 : 466 : statsextinfo[i].dobj.namespace =
8222 : 233 : findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace)));
8223 : 233 : statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner));
8224 : 466 : statsextinfo[i].stattable =
8225 : 233 : findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid)));
8226 [ + + ]: 233 : if (PQgetisnull(res, i, i_stattarget))
8227 : 185 : statsextinfo[i].stattarget = -1;
8228 : : else
8229 : 48 : statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget));
8230 : :
8231 : : /* Decide whether we want to dump it */
8232 : 233 : selectDumpableStatisticsObject(&(statsextinfo[i]), fout);
8233 : :
8234 [ + + ]: 233 : if (fout->dopt->dumpStatistics)
8235 : 164 : statsextinfo[i].dobj.components |= DUMP_COMPONENT_STATISTICS;
8236 : : }
8237 : :
8238 : 215 : PQclear(res);
8239 : 215 : destroyPQExpBuffer(query);
8240 : 215 : }
8241 : :
8242 : : /*
8243 : : * getConstraints
8244 : : *
8245 : : * Get info about constraints on dumpable tables.
8246 : : *
8247 : : * Currently handles foreign keys only.
8248 : : * Unique and primary key constraints are handled with indexes,
8249 : : * while check constraints are processed in getTableAttrs().
8250 : : */
8251 : : void
8252 : 215 : getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
8253 : : {
8254 : 215 : PQExpBuffer query = createPQExpBuffer();
8255 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
8256 : : PGresult *res;
8257 : : int ntups;
8258 : : int curtblindx;
8259 : 215 : TableInfo *tbinfo = NULL;
8260 : : ConstraintInfo *constrinfo;
8261 : : int i_contableoid,
8262 : : i_conoid,
8263 : : i_conrelid,
8264 : : i_conname,
8265 : : i_confrelid,
8266 : : i_conindid,
8267 : : i_condef;
8268 : :
8269 : : /*
8270 : : * We want to perform just one query against pg_constraint. However, we
8271 : : * mustn't try to select every row of the catalog and then sort it out on
8272 : : * the client side, because some of the server-side functions we need
8273 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8274 : : * build an array of the OIDs of tables we care about (and now have lock
8275 : : * on!), and use a WHERE clause to constrain which rows are selected.
8276 : : */
8277 : 215 : appendPQExpBufferChar(tbloids, '{');
8278 [ + + ]: 57593 : for (int i = 0; i < numTables; i++)
8279 : : {
8280 : 57378 : TableInfo *tinfo = &tblinfo[i];
8281 : :
8282 [ + + ]: 57378 : if (!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8283 : 49759 : continue;
8284 : :
8285 : : /* OK, we need info for this table */
8286 [ + + ]: 7619 : if (tbloids->len > 1) /* do we have more than the '{'? */
8287 : 7474 : appendPQExpBufferChar(tbloids, ',');
8288 : 7619 : appendPQExpBuffer(tbloids, "%u", tinfo->dobj.catId.oid);
8289 : : }
8290 : 215 : appendPQExpBufferChar(tbloids, '}');
8291 : :
8292 : 215 : appendPQExpBufferStr(query,
8293 : : "SELECT c.tableoid, c.oid, "
8294 : : "conrelid, conname, confrelid, ");
8295 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
8296 : 215 : appendPQExpBufferStr(query, "conindid, ");
8297 : : else
8298 : 0 : appendPQExpBufferStr(query, "0 AS conindid, ");
8299 : 215 : appendPQExpBuffer(query,
8300 : : "pg_catalog.pg_get_constraintdef(c.oid) AS condef\n"
8301 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8302 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
8303 : : "WHERE contype = 'f' ",
8304 : : tbloids->data);
8305 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
8306 : 215 : appendPQExpBufferStr(query,
8307 : : "AND conparentid = 0 ");
8308 : 215 : appendPQExpBufferStr(query,
8309 : : "ORDER BY conrelid, conname");
8310 : :
8311 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8312 : :
8313 : 215 : ntups = PQntuples(res);
8314 : :
8315 : 215 : i_contableoid = PQfnumber(res, "tableoid");
8316 : 215 : i_conoid = PQfnumber(res, "oid");
8317 : 215 : i_conrelid = PQfnumber(res, "conrelid");
8318 : 215 : i_conname = PQfnumber(res, "conname");
8319 : 215 : i_confrelid = PQfnumber(res, "confrelid");
8320 : 215 : i_conindid = PQfnumber(res, "conindid");
8321 : 215 : i_condef = PQfnumber(res, "condef");
8322 : :
8323 : 215 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8324 : :
8325 : 215 : curtblindx = -1;
8326 [ + + ]: 392 : for (int j = 0; j < ntups; j++)
8327 : : {
8328 : 177 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
8329 : : TableInfo *reftable;
8330 : :
8331 : : /*
8332 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8333 : : * order.
8334 : : */
8335 [ + + + + ]: 177 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != conrelid)
8336 : : {
8337 [ + - ]: 15591 : while (++curtblindx < numTables)
8338 : : {
8339 : 15591 : tbinfo = &tblinfo[curtblindx];
8340 [ + + ]: 15591 : if (tbinfo->dobj.catId.oid == conrelid)
8341 : 167 : break;
8342 : : }
8343 [ - + ]: 167 : if (curtblindx >= numTables)
8344 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
8345 : : }
8346 : :
8347 : 177 : constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
8348 : 177 : constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8349 : 177 : constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8350 : 177 : AssignDumpId(&constrinfo[j].dobj);
8351 : 177 : constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8352 : 177 : constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8353 : 177 : constrinfo[j].contable = tbinfo;
8354 : 177 : constrinfo[j].condomain = NULL;
8355 : 177 : constrinfo[j].contype = 'f';
8356 : 177 : constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
8357 : 177 : constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid));
8358 : 177 : constrinfo[j].conindex = 0;
8359 : 177 : constrinfo[j].condeferrable = false;
8360 : 177 : constrinfo[j].condeferred = false;
8361 : 177 : constrinfo[j].conislocal = true;
8362 : 177 : constrinfo[j].separate = true;
8363 : :
8364 : : /*
8365 : : * Restoring an FK that points to a partitioned table requires that
8366 : : * all partition indexes have been attached beforehand. Ensure that
8367 : : * happens by making the constraint depend on each index partition
8368 : : * attach object.
8369 : : */
8370 : 177 : reftable = findTableByOid(constrinfo[j].confrelid);
8371 [ + - + + ]: 177 : if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
8372 : : {
8373 : 20 : Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
8374 : :
8375 [ + - ]: 20 : if (indexOid != InvalidOid)
8376 : : {
8377 [ + - ]: 20 : for (int k = 0; k < reftable->numIndexes; k++)
8378 : : {
8379 : : IndxInfo *refidx;
8380 : :
8381 : : /* not our index? */
8382 [ - + ]: 20 : if (reftable->indexes[k].dobj.catId.oid != indexOid)
8383 : 0 : continue;
8384 : :
8385 : 20 : refidx = &reftable->indexes[k];
8386 : 20 : addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
8387 : 20 : break;
8388 : : }
8389 : : }
8390 : : }
8391 : : }
8392 : :
8393 : 215 : PQclear(res);
8394 : :
8395 : 215 : destroyPQExpBuffer(query);
8396 : 215 : destroyPQExpBuffer(tbloids);
8397 : 215 : }
8398 : :
8399 : : /*
8400 : : * addConstrChildIdxDeps
8401 : : *
8402 : : * Recursive subroutine for getConstraints
8403 : : *
8404 : : * Given an object representing a foreign key constraint and an index on the
8405 : : * partitioned table it references, mark the constraint object as dependent
8406 : : * on the DO_INDEX_ATTACH object of each index partition, recursively
8407 : : * drilling down to their partitions if any. This ensures that the FK is not
8408 : : * restored until the index is fully marked valid.
8409 : : */
8410 : : static void
8411 : 45 : addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
8412 : : {
8413 : : SimplePtrListCell *cell;
8414 : :
8415 : : Assert(dobj->objType == DO_FK_CONSTRAINT);
8416 : :
8417 [ + + ]: 155 : for (cell = refidx->partattaches.head; cell; cell = cell->next)
8418 : : {
8419 : 110 : IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
8420 : :
8421 : 110 : addObjectDependency(dobj, attach->dobj.dumpId);
8422 : :
8423 [ + + ]: 110 : if (attach->partitionIdx->partattaches.head != NULL)
8424 : 25 : addConstrChildIdxDeps(dobj, attach->partitionIdx);
8425 : : }
8426 : 45 : }
8427 : :
8428 : : /*
8429 : : * getDomainConstraints
8430 : : *
8431 : : * Get info about constraints on a domain.
8432 : : */
8433 : : static void
8434 : 171 : getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
8435 : : {
8436 : : ConstraintInfo *constrinfo;
8437 : 171 : PQExpBuffer query = createPQExpBuffer();
8438 : : PGresult *res;
8439 : : int i_tableoid,
8440 : : i_oid,
8441 : : i_conname,
8442 : : i_consrc,
8443 : : i_convalidated,
8444 : : i_contype;
8445 : : int ntups;
8446 : :
8447 [ + + ]: 171 : if (!fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS])
8448 : : {
8449 : : /*
8450 : : * Set up query for constraint-specific details. For servers 17 and
8451 : : * up, domains have constraints of type 'n' as well as 'c', otherwise
8452 : : * just the latter.
8453 : : */
8454 : 46 : appendPQExpBuffer(query,
8455 : : "PREPARE getDomainConstraints(pg_catalog.oid) AS\n"
8456 : : "SELECT tableoid, oid, conname, "
8457 : : "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8458 : : "convalidated, contype "
8459 : : "FROM pg_catalog.pg_constraint "
8460 : : "WHERE contypid = $1 AND contype IN (%s) "
8461 : : "ORDER BY conname",
8462 [ - + ]: 46 : fout->remoteVersion < 170000 ? "'c'" : "'c', 'n'");
8463 : :
8464 : 46 : ExecuteSqlStatement(fout, query->data);
8465 : :
8466 : 46 : fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS] = true;
8467 : : }
8468 : :
8469 : 171 : printfPQExpBuffer(query,
8470 : : "EXECUTE getDomainConstraints('%u')",
8471 : : tyinfo->dobj.catId.oid);
8472 : :
8473 : 171 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8474 : :
8475 : 171 : ntups = PQntuples(res);
8476 : :
8477 : 171 : i_tableoid = PQfnumber(res, "tableoid");
8478 : 171 : i_oid = PQfnumber(res, "oid");
8479 : 171 : i_conname = PQfnumber(res, "conname");
8480 : 171 : i_consrc = PQfnumber(res, "consrc");
8481 : 171 : i_convalidated = PQfnumber(res, "convalidated");
8482 : 171 : i_contype = PQfnumber(res, "contype");
8483 : :
8484 : 171 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8485 : 171 : tyinfo->domChecks = constrinfo;
8486 : :
8487 : : /* 'i' tracks result rows; 'j' counts CHECK constraints */
8488 [ + + ]: 353 : for (int i = 0, j = 0; i < ntups; i++)
8489 : : {
8490 : 182 : bool validated = PQgetvalue(res, i, i_convalidated)[0] == 't';
8491 : 182 : char contype = (PQgetvalue(res, i, i_contype))[0];
8492 : : ConstraintInfo *constraint;
8493 : :
8494 [ + + ]: 182 : if (contype == CONSTRAINT_CHECK)
8495 : : {
8496 : 126 : constraint = &constrinfo[j++];
8497 : 126 : tyinfo->nDomChecks++;
8498 : : }
8499 : : else
8500 : : {
8501 : : Assert(contype == CONSTRAINT_NOTNULL);
8502 : : Assert(tyinfo->notnull == NULL);
8503 : : /* use last item in array for the not-null constraint */
8504 : 56 : tyinfo->notnull = &(constrinfo[ntups - 1]);
8505 : 56 : constraint = tyinfo->notnull;
8506 : : }
8507 : :
8508 : 182 : constraint->dobj.objType = DO_CONSTRAINT;
8509 : 182 : constraint->dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8510 : 182 : constraint->dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8511 : 182 : AssignDumpId(&(constraint->dobj));
8512 : 182 : constraint->dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
8513 : 182 : constraint->dobj.namespace = tyinfo->dobj.namespace;
8514 : 182 : constraint->contable = NULL;
8515 : 182 : constraint->condomain = tyinfo;
8516 : 182 : constraint->contype = contype;
8517 : 182 : constraint->condef = pg_strdup(PQgetvalue(res, i, i_consrc));
8518 : 182 : constraint->confrelid = InvalidOid;
8519 : 182 : constraint->conindex = 0;
8520 : 182 : constraint->condeferrable = false;
8521 : 182 : constraint->condeferred = false;
8522 : 182 : constraint->conislocal = true;
8523 : :
8524 : 182 : constraint->separate = !validated;
8525 : :
8526 : : /*
8527 : : * Make the domain depend on the constraint, ensuring it won't be
8528 : : * output till any constraint dependencies are OK. If the constraint
8529 : : * has not been validated, it's going to be dumped after the domain
8530 : : * anyway, so this doesn't matter.
8531 : : */
8532 [ + + ]: 182 : if (validated)
8533 : 177 : addObjectDependency(&tyinfo->dobj, constraint->dobj.dumpId);
8534 : : }
8535 : :
8536 : 171 : PQclear(res);
8537 : :
8538 : 171 : destroyPQExpBuffer(query);
8539 : 171 : }
8540 : :
8541 : : /*
8542 : : * getRules
8543 : : * get basic information about every rule in the system
8544 : : */
8545 : : void
8546 : 215 : getRules(Archive *fout)
8547 : : {
8548 : : PGresult *res;
8549 : : int ntups;
8550 : : int i;
8551 : 215 : PQExpBuffer query = createPQExpBuffer();
8552 : : RuleInfo *ruleinfo;
8553 : : int i_tableoid;
8554 : : int i_oid;
8555 : : int i_rulename;
8556 : : int i_ruletable;
8557 : : int i_ev_type;
8558 : : int i_is_instead;
8559 : : int i_ev_enabled;
8560 : :
8561 : 215 : appendPQExpBufferStr(query, "SELECT "
8562 : : "tableoid, oid, rulename, "
8563 : : "ev_class AS ruletable, ev_type, is_instead, "
8564 : : "ev_enabled "
8565 : : "FROM pg_rewrite "
8566 : : "ORDER BY oid");
8567 : :
8568 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8569 : :
8570 : 215 : ntups = PQntuples(res);
8571 : :
8572 : 215 : ruleinfo = pg_malloc_array(RuleInfo, ntups);
8573 : :
8574 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8575 : 215 : i_oid = PQfnumber(res, "oid");
8576 : 215 : i_rulename = PQfnumber(res, "rulename");
8577 : 215 : i_ruletable = PQfnumber(res, "ruletable");
8578 : 215 : i_ev_type = PQfnumber(res, "ev_type");
8579 : 215 : i_is_instead = PQfnumber(res, "is_instead");
8580 : 215 : i_ev_enabled = PQfnumber(res, "ev_enabled");
8581 : :
8582 [ + + ]: 34739 : for (i = 0; i < ntups; i++)
8583 : : {
8584 : : Oid ruletableoid;
8585 : :
8586 : 34524 : ruleinfo[i].dobj.objType = DO_RULE;
8587 : 34524 : ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8588 : 34524 : ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8589 : 34524 : AssignDumpId(&ruleinfo[i].dobj);
8590 : 34524 : ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename));
8591 : 34524 : ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
8592 : 34524 : ruleinfo[i].ruletable = findTableByOid(ruletableoid);
8593 [ - + ]: 34524 : if (ruleinfo[i].ruletable == NULL)
8594 : 0 : pg_fatal("failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found",
8595 : : ruletableoid, ruleinfo[i].dobj.catId.oid);
8596 : 34524 : ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace;
8597 : 34524 : ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump;
8598 : 34524 : ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
8599 : 34524 : ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
8600 : 34524 : ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled));
8601 [ + - ]: 34524 : if (ruleinfo[i].ruletable)
8602 : : {
8603 : : /*
8604 : : * If the table is a view or materialized view, force its ON
8605 : : * SELECT rule to be sorted before the view itself --- this
8606 : : * ensures that any dependencies for the rule affect the table's
8607 : : * positioning. Other rules are forced to appear after their
8608 : : * table.
8609 : : */
8610 [ + + ]: 34524 : if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW ||
8611 [ + + ]: 727 : ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) &&
8612 [ + + + - ]: 34293 : ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
8613 : : {
8614 : 33821 : addObjectDependency(&ruleinfo[i].ruletable->dobj,
8615 : 33821 : ruleinfo[i].dobj.dumpId);
8616 : : /* We'll merge the rule into CREATE VIEW, if possible */
8617 : 33821 : ruleinfo[i].separate = false;
8618 : : }
8619 : : else
8620 : : {
8621 : 703 : addObjectDependency(&ruleinfo[i].dobj,
8622 : 703 : ruleinfo[i].ruletable->dobj.dumpId);
8623 : 703 : ruleinfo[i].separate = true;
8624 : : }
8625 : : }
8626 : : else
8627 : 0 : ruleinfo[i].separate = true;
8628 : : }
8629 : :
8630 : 215 : PQclear(res);
8631 : :
8632 : 215 : destroyPQExpBuffer(query);
8633 : 215 : }
8634 : :
8635 : : /*
8636 : : * getTriggers
8637 : : * get information about every trigger on a dumpable table
8638 : : *
8639 : : * Note: trigger data is not returned directly to the caller, but it
8640 : : * does get entered into the DumpableObject tables.
8641 : : */
8642 : : void
8643 : 215 : getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
8644 : : {
8645 : 215 : PQExpBuffer query = createPQExpBuffer();
8646 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
8647 : : PGresult *res;
8648 : : int ntups;
8649 : : int curtblindx;
8650 : : TriggerInfo *tginfo;
8651 : : int i_tableoid,
8652 : : i_oid,
8653 : : i_tgrelid,
8654 : : i_tgname,
8655 : : i_tgenabled,
8656 : : i_tgispartition,
8657 : : i_tgdef;
8658 : :
8659 : : /*
8660 : : * We want to perform just one query against pg_trigger. However, we
8661 : : * mustn't try to select every row of the catalog and then sort it out on
8662 : : * the client side, because some of the server-side functions we need
8663 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8664 : : * build an array of the OIDs of tables we care about (and now have lock
8665 : : * on!), and use a WHERE clause to constrain which rows are selected.
8666 : : */
8667 : 215 : appendPQExpBufferChar(tbloids, '{');
8668 [ + + ]: 57593 : for (int i = 0; i < numTables; i++)
8669 : : {
8670 : 57378 : TableInfo *tbinfo = &tblinfo[i];
8671 : :
8672 [ + + ]: 57378 : if (!tbinfo->hastriggers ||
8673 [ + + ]: 1141 : !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8674 : 56503 : continue;
8675 : :
8676 : : /* OK, we need info for this table */
8677 [ + + ]: 875 : if (tbloids->len > 1) /* do we have more than the '{'? */
8678 : 821 : appendPQExpBufferChar(tbloids, ',');
8679 : 875 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
8680 : : }
8681 : 215 : appendPQExpBufferChar(tbloids, '}');
8682 : :
8683 [ + - ]: 215 : if (fout->remoteVersion >= 150000)
8684 : : {
8685 : : /*
8686 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8687 : : * result in non-forward-compatible dumps of WHEN clauses due to
8688 : : * under-parenthesization.
8689 : : *
8690 : : * NB: We need to see partition triggers in case the tgenabled flag
8691 : : * has been changed from the parent.
8692 : : */
8693 : 215 : appendPQExpBuffer(query,
8694 : : "SELECT t.tgrelid, t.tgname, "
8695 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8696 : : "t.tgenabled, t.tableoid, t.oid, "
8697 : : "t.tgparentid <> 0 AS tgispartition\n"
8698 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8699 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8700 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8701 : : "WHERE ((NOT t.tgisinternal AND t.tgparentid = 0) "
8702 : : "OR t.tgenabled != u.tgenabled) "
8703 : : "ORDER BY t.tgrelid, t.tgname",
8704 : : tbloids->data);
8705 : : }
8706 [ # # ]: 0 : else if (fout->remoteVersion >= 130000)
8707 : : {
8708 : : /*
8709 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8710 : : * result in non-forward-compatible dumps of WHEN clauses due to
8711 : : * under-parenthesization.
8712 : : *
8713 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8714 : : * tgenabled flag has been changed from the parent.
8715 : : */
8716 : 0 : appendPQExpBuffer(query,
8717 : : "SELECT t.tgrelid, t.tgname, "
8718 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8719 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition\n"
8720 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8721 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8722 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8723 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != u.tgenabled) "
8724 : : "ORDER BY t.tgrelid, t.tgname",
8725 : : tbloids->data);
8726 : : }
8727 [ # # ]: 0 : else if (fout->remoteVersion >= 110000)
8728 : : {
8729 : : /*
8730 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8731 : : * tgenabled flag has been changed from the parent. No tgparentid in
8732 : : * version 11-12, so we have to match them via pg_depend.
8733 : : *
8734 : : * See above about pretty=true in pg_get_triggerdef.
8735 : : */
8736 : 0 : appendPQExpBuffer(query,
8737 : : "SELECT t.tgrelid, t.tgname, "
8738 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8739 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition "
8740 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8741 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8742 : : "LEFT JOIN pg_catalog.pg_depend AS d ON "
8743 : : " d.classid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8744 : : " d.refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8745 : : " d.objid = t.oid "
8746 : : "LEFT JOIN pg_catalog.pg_trigger AS pt ON pt.oid = refobjid "
8747 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != pt.tgenabled) "
8748 : : "ORDER BY t.tgrelid, t.tgname",
8749 : : tbloids->data);
8750 : : }
8751 : : else
8752 : : {
8753 : : /* See above about pretty=true in pg_get_triggerdef */
8754 : 0 : appendPQExpBuffer(query,
8755 : : "SELECT t.tgrelid, t.tgname, "
8756 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8757 : : "t.tgenabled, false as tgispartition, "
8758 : : "t.tableoid, t.oid "
8759 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8760 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8761 : : "WHERE NOT tgisinternal "
8762 : : "ORDER BY t.tgrelid, t.tgname",
8763 : : tbloids->data);
8764 : : }
8765 : :
8766 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8767 : :
8768 : 215 : ntups = PQntuples(res);
8769 : :
8770 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8771 : 215 : i_oid = PQfnumber(res, "oid");
8772 : 215 : i_tgrelid = PQfnumber(res, "tgrelid");
8773 : 215 : i_tgname = PQfnumber(res, "tgname");
8774 : 215 : i_tgenabled = PQfnumber(res, "tgenabled");
8775 : 215 : i_tgispartition = PQfnumber(res, "tgispartition");
8776 : 215 : i_tgdef = PQfnumber(res, "tgdef");
8777 : :
8778 : 215 : tginfo = pg_malloc_array(TriggerInfo, ntups);
8779 : :
8780 : : /*
8781 : : * Outer loop iterates once per table, not once per row. Incrementing of
8782 : : * j is handled by the inner loop.
8783 : : */
8784 : 215 : curtblindx = -1;
8785 [ + + ]: 533 : for (int j = 0; j < ntups;)
8786 : : {
8787 : 318 : Oid tgrelid = atooid(PQgetvalue(res, j, i_tgrelid));
8788 : 318 : TableInfo *tbinfo = NULL;
8789 : : int numtrigs;
8790 : :
8791 : : /* Count rows for this table */
8792 [ + + ]: 535 : for (numtrigs = 1; numtrigs < ntups - j; numtrigs++)
8793 [ + + ]: 481 : if (atooid(PQgetvalue(res, j + numtrigs, i_tgrelid)) != tgrelid)
8794 : 264 : break;
8795 : :
8796 : : /*
8797 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8798 : : * order.
8799 : : */
8800 [ + - ]: 17331 : while (++curtblindx < numTables)
8801 : : {
8802 : 17331 : tbinfo = &tblinfo[curtblindx];
8803 [ + + ]: 17331 : if (tbinfo->dobj.catId.oid == tgrelid)
8804 : 318 : break;
8805 : : }
8806 [ - + ]: 318 : if (curtblindx >= numTables)
8807 : 0 : pg_fatal("unrecognized table OID %u", tgrelid);
8808 : :
8809 : : /* Save data for this table */
8810 [ + + ]: 853 : for (int c = 0; c < numtrigs; c++, j++)
8811 : : {
8812 : 535 : tginfo[j].dobj.objType = DO_TRIGGER;
8813 : 535 : tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8814 : 535 : tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8815 : 535 : AssignDumpId(&tginfo[j].dobj);
8816 : 535 : tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname));
8817 : 535 : tginfo[j].dobj.namespace = tbinfo->dobj.namespace;
8818 : 535 : tginfo[j].tgtable = tbinfo;
8819 : 535 : tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled));
8820 : 535 : tginfo[j].tgispartition = *(PQgetvalue(res, j, i_tgispartition)) == 't';
8821 : 535 : tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef));
8822 : : }
8823 : : }
8824 : :
8825 : 215 : PQclear(res);
8826 : :
8827 : 215 : destroyPQExpBuffer(query);
8828 : 215 : destroyPQExpBuffer(tbloids);
8829 : 215 : }
8830 : :
8831 : : /*
8832 : : * getEventTriggers
8833 : : * get information about event triggers
8834 : : */
8835 : : void
8836 : 215 : getEventTriggers(Archive *fout)
8837 : : {
8838 : : int i;
8839 : : PQExpBuffer query;
8840 : : PGresult *res;
8841 : : EventTriggerInfo *evtinfo;
8842 : : int i_tableoid,
8843 : : i_oid,
8844 : : i_evtname,
8845 : : i_evtevent,
8846 : : i_evtowner,
8847 : : i_evttags,
8848 : : i_evtfname,
8849 : : i_evtenabled;
8850 : : int ntups;
8851 : :
8852 : 215 : query = createPQExpBuffer();
8853 : :
8854 : 215 : appendPQExpBufferStr(query,
8855 : : "SELECT e.tableoid, e.oid, evtname, evtenabled, "
8856 : : "evtevent, evtowner, "
8857 : : "array_to_string(array("
8858 : : "select quote_literal(x) "
8859 : : " from unnest(evttags) as t(x)), ', ') as evttags, "
8860 : : "e.evtfoid::regproc as evtfname "
8861 : : "FROM pg_event_trigger e "
8862 : : "ORDER BY e.oid");
8863 : :
8864 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8865 : :
8866 : 215 : ntups = PQntuples(res);
8867 : :
8868 : 215 : evtinfo = pg_malloc_array(EventTriggerInfo, ntups);
8869 : :
8870 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8871 : 215 : i_oid = PQfnumber(res, "oid");
8872 : 215 : i_evtname = PQfnumber(res, "evtname");
8873 : 215 : i_evtevent = PQfnumber(res, "evtevent");
8874 : 215 : i_evtowner = PQfnumber(res, "evtowner");
8875 : 215 : i_evttags = PQfnumber(res, "evttags");
8876 : 215 : i_evtfname = PQfnumber(res, "evtfname");
8877 : 215 : i_evtenabled = PQfnumber(res, "evtenabled");
8878 : :
8879 [ + + ]: 270 : for (i = 0; i < ntups; i++)
8880 : : {
8881 : 55 : evtinfo[i].dobj.objType = DO_EVENT_TRIGGER;
8882 : 55 : evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8883 : 55 : evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8884 : 55 : AssignDumpId(&evtinfo[i].dobj);
8885 : 55 : evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname));
8886 : 55 : evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname));
8887 : 55 : evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent));
8888 : 55 : evtinfo[i].evtowner = getRoleName(PQgetvalue(res, i, i_evtowner));
8889 : 55 : evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags));
8890 : 55 : evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname));
8891 : 55 : evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled));
8892 : :
8893 : : /* Decide whether we want to dump it */
8894 : 55 : selectDumpableObject(&(evtinfo[i].dobj), fout);
8895 : : }
8896 : :
8897 : 215 : PQclear(res);
8898 : :
8899 : 215 : destroyPQExpBuffer(query);
8900 : 215 : }
8901 : :
8902 : : /*
8903 : : * getProcLangs
8904 : : * get basic information about every procedural language in the system
8905 : : *
8906 : : * NB: this must run after getFuncs() because we assume we can do
8907 : : * findFuncByOid().
8908 : : */
8909 : : void
8910 : 215 : getProcLangs(Archive *fout)
8911 : : {
8912 : : PGresult *res;
8913 : : int ntups;
8914 : : int i;
8915 : 215 : PQExpBuffer query = createPQExpBuffer();
8916 : : ProcLangInfo *planginfo;
8917 : : int i_tableoid;
8918 : : int i_oid;
8919 : : int i_lanname;
8920 : : int i_lanpltrusted;
8921 : : int i_lanplcallfoid;
8922 : : int i_laninline;
8923 : : int i_lanvalidator;
8924 : : int i_lanacl;
8925 : : int i_acldefault;
8926 : : int i_lanowner;
8927 : :
8928 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
8929 : : "lanname, lanpltrusted, lanplcallfoid, "
8930 : : "laninline, lanvalidator, "
8931 : : "lanacl, "
8932 : : "acldefault('l', lanowner) AS acldefault, "
8933 : : "lanowner "
8934 : : "FROM pg_language "
8935 : : "WHERE lanispl "
8936 : : "ORDER BY oid");
8937 : :
8938 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8939 : :
8940 : 215 : ntups = PQntuples(res);
8941 : :
8942 : 215 : planginfo = pg_malloc_array(ProcLangInfo, ntups);
8943 : :
8944 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8945 : 215 : i_oid = PQfnumber(res, "oid");
8946 : 215 : i_lanname = PQfnumber(res, "lanname");
8947 : 215 : i_lanpltrusted = PQfnumber(res, "lanpltrusted");
8948 : 215 : i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
8949 : 215 : i_laninline = PQfnumber(res, "laninline");
8950 : 215 : i_lanvalidator = PQfnumber(res, "lanvalidator");
8951 : 215 : i_lanacl = PQfnumber(res, "lanacl");
8952 : 215 : i_acldefault = PQfnumber(res, "acldefault");
8953 : 215 : i_lanowner = PQfnumber(res, "lanowner");
8954 : :
8955 [ + + ]: 478 : for (i = 0; i < ntups; i++)
8956 : : {
8957 : 263 : planginfo[i].dobj.objType = DO_PROCLANG;
8958 : 263 : planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8959 : 263 : planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8960 : 263 : AssignDumpId(&planginfo[i].dobj);
8961 : :
8962 : 263 : planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
8963 : 263 : planginfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_lanacl));
8964 : 263 : planginfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
8965 : 263 : planginfo[i].dacl.privtype = 0;
8966 : 263 : planginfo[i].dacl.initprivs = NULL;
8967 : 263 : planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
8968 : 263 : planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
8969 : 263 : planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
8970 : 263 : planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
8971 : 263 : planginfo[i].lanowner = getRoleName(PQgetvalue(res, i, i_lanowner));
8972 : :
8973 : : /* Decide whether we want to dump it */
8974 : 263 : selectDumpableProcLang(&(planginfo[i]), fout);
8975 : :
8976 : : /* Mark whether language has an ACL */
8977 [ + + ]: 263 : if (!PQgetisnull(res, i, i_lanacl))
8978 : 61 : planginfo[i].dobj.components |= DUMP_COMPONENT_ACL;
8979 : : }
8980 : :
8981 : 215 : PQclear(res);
8982 : :
8983 : 215 : destroyPQExpBuffer(query);
8984 : 215 : }
8985 : :
8986 : : /*
8987 : : * getCasts
8988 : : * get basic information about most casts in the system
8989 : : *
8990 : : * Skip casts from a range to its multirange, since we'll create those
8991 : : * automatically.
8992 : : */
8993 : : void
8994 : 215 : getCasts(Archive *fout)
8995 : : {
8996 : : PGresult *res;
8997 : : int ntups;
8998 : : int i;
8999 : 215 : PQExpBuffer query = createPQExpBuffer();
9000 : : CastInfo *castinfo;
9001 : : int i_tableoid;
9002 : : int i_oid;
9003 : : int i_castsource;
9004 : : int i_casttarget;
9005 : : int i_castfunc;
9006 : : int i_castcontext;
9007 : : int i_castmethod;
9008 : :
9009 [ + - ]: 215 : if (fout->remoteVersion >= 140000)
9010 : : {
9011 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9012 : : "castsource, casttarget, castfunc, castcontext, "
9013 : : "castmethod "
9014 : : "FROM pg_cast c "
9015 : : "WHERE NOT EXISTS ( "
9016 : : "SELECT 1 FROM pg_range r "
9017 : : "WHERE c.castsource = r.rngtypid "
9018 : : "AND c.casttarget = r.rngmultitypid "
9019 : : ") "
9020 : : "ORDER BY 3,4");
9021 : : }
9022 : : else
9023 : : {
9024 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9025 : : "castsource, casttarget, castfunc, castcontext, "
9026 : : "castmethod "
9027 : : "FROM pg_cast ORDER BY 3,4");
9028 : : }
9029 : :
9030 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9031 : :
9032 : 215 : ntups = PQntuples(res);
9033 : :
9034 : 215 : castinfo = pg_malloc_array(CastInfo, ntups);
9035 : :
9036 : 215 : i_tableoid = PQfnumber(res, "tableoid");
9037 : 215 : i_oid = PQfnumber(res, "oid");
9038 : 215 : i_castsource = PQfnumber(res, "castsource");
9039 : 215 : i_casttarget = PQfnumber(res, "casttarget");
9040 : 215 : i_castfunc = PQfnumber(res, "castfunc");
9041 : 215 : i_castcontext = PQfnumber(res, "castcontext");
9042 : 215 : i_castmethod = PQfnumber(res, "castmethod");
9043 : :
9044 [ + + ]: 52550 : for (i = 0; i < ntups; i++)
9045 : : {
9046 : : PQExpBufferData namebuf;
9047 : : TypeInfo *sTypeInfo;
9048 : : TypeInfo *tTypeInfo;
9049 : :
9050 : 52335 : castinfo[i].dobj.objType = DO_CAST;
9051 : 52335 : castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9052 : 52335 : castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9053 : 52335 : AssignDumpId(&castinfo[i].dobj);
9054 : 52335 : castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
9055 : 52335 : castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
9056 : 52335 : castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
9057 : 52335 : castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
9058 : 52335 : castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod));
9059 : :
9060 : : /*
9061 : : * Try to name cast as concatenation of typnames. This is only used
9062 : : * for purposes of sorting. If we fail to find either type, the name
9063 : : * will be an empty string.
9064 : : */
9065 : 52335 : initPQExpBuffer(&namebuf);
9066 : 52335 : sTypeInfo = findTypeByOid(castinfo[i].castsource);
9067 : 52335 : tTypeInfo = findTypeByOid(castinfo[i].casttarget);
9068 [ + - + - ]: 52335 : if (sTypeInfo && tTypeInfo)
9069 : 52335 : appendPQExpBuffer(&namebuf, "%s %s",
9070 : : sTypeInfo->dobj.name, tTypeInfo->dobj.name);
9071 : 52335 : castinfo[i].dobj.name = namebuf.data;
9072 : :
9073 : : /* Decide whether we want to dump it */
9074 : 52335 : selectDumpableCast(&(castinfo[i]), fout);
9075 : : }
9076 : :
9077 : 215 : PQclear(res);
9078 : :
9079 : 215 : destroyPQExpBuffer(query);
9080 : 215 : }
9081 : :
9082 : : static char *
9083 : 93 : get_language_name(Archive *fout, Oid langid)
9084 : : {
9085 : : PQExpBuffer query;
9086 : : PGresult *res;
9087 : : char *lanname;
9088 : :
9089 : 93 : query = createPQExpBuffer();
9090 : 93 : appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid);
9091 : 93 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
9092 : 93 : lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
9093 : 93 : destroyPQExpBuffer(query);
9094 : 93 : PQclear(res);
9095 : :
9096 : 93 : return lanname;
9097 : : }
9098 : :
9099 : : /*
9100 : : * getTransforms
9101 : : * get basic information about every transform in the system
9102 : : */
9103 : : void
9104 : 215 : getTransforms(Archive *fout)
9105 : : {
9106 : : PGresult *res;
9107 : : int ntups;
9108 : : int i;
9109 : : PQExpBuffer query;
9110 : : TransformInfo *transforminfo;
9111 : : int i_tableoid;
9112 : : int i_oid;
9113 : : int i_trftype;
9114 : : int i_trflang;
9115 : : int i_trffromsql;
9116 : : int i_trftosql;
9117 : :
9118 : 215 : query = createPQExpBuffer();
9119 : :
9120 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9121 : : "trftype, trflang, trffromsql::oid, trftosql::oid "
9122 : : "FROM pg_transform "
9123 : : "ORDER BY 3,4");
9124 : :
9125 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9126 : :
9127 : 215 : ntups = PQntuples(res);
9128 : :
9129 : 215 : transforminfo = pg_malloc_array(TransformInfo, ntups);
9130 : :
9131 : 215 : i_tableoid = PQfnumber(res, "tableoid");
9132 : 215 : i_oid = PQfnumber(res, "oid");
9133 : 215 : i_trftype = PQfnumber(res, "trftype");
9134 : 215 : i_trflang = PQfnumber(res, "trflang");
9135 : 215 : i_trffromsql = PQfnumber(res, "trffromsql");
9136 : 215 : i_trftosql = PQfnumber(res, "trftosql");
9137 : :
9138 [ + + ]: 270 : for (i = 0; i < ntups; i++)
9139 : : {
9140 : : PQExpBufferData namebuf;
9141 : : TypeInfo *typeInfo;
9142 : : char *lanname;
9143 : :
9144 : 55 : transforminfo[i].dobj.objType = DO_TRANSFORM;
9145 : 55 : transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9146 : 55 : transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9147 : 55 : AssignDumpId(&transforminfo[i].dobj);
9148 : 55 : transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype));
9149 : 55 : transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang));
9150 : 55 : transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql));
9151 : 55 : transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql));
9152 : :
9153 : : /*
9154 : : * Try to name transform as concatenation of type and language name.
9155 : : * This is only used for purposes of sorting. If we fail to find
9156 : : * either, the name will be an empty string.
9157 : : */
9158 : 55 : initPQExpBuffer(&namebuf);
9159 : 55 : typeInfo = findTypeByOid(transforminfo[i].trftype);
9160 : 55 : lanname = get_language_name(fout, transforminfo[i].trflang);
9161 [ + - + - ]: 55 : if (typeInfo && lanname)
9162 : 55 : appendPQExpBuffer(&namebuf, "%s %s",
9163 : : typeInfo->dobj.name, lanname);
9164 : 55 : transforminfo[i].dobj.name = namebuf.data;
9165 : 55 : free(lanname);
9166 : :
9167 : : /* Decide whether we want to dump it */
9168 : 55 : selectDumpableObject(&(transforminfo[i].dobj), fout);
9169 : : }
9170 : :
9171 : 215 : PQclear(res);
9172 : :
9173 : 215 : destroyPQExpBuffer(query);
9174 : 215 : }
9175 : :
9176 : : /*
9177 : : * getTableAttrs -
9178 : : * for each interesting table, read info about its attributes
9179 : : * (names, types, default values, CHECK constraints, etc)
9180 : : *
9181 : : * modifies tblinfo
9182 : : */
9183 : : void
9184 : 215 : getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
9185 : : {
9186 : 215 : DumpOptions *dopt = fout->dopt;
9187 : 215 : PQExpBuffer q = createPQExpBuffer();
9188 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
9189 : 215 : PQExpBuffer checkoids = createPQExpBuffer();
9190 : 215 : PQExpBuffer invalidnotnulloids = NULL;
9191 : : PGresult *res;
9192 : : int ntups;
9193 : : int curtblindx;
9194 : : int i_attrelid;
9195 : : int i_attnum;
9196 : : int i_attname;
9197 : : int i_atttypname;
9198 : : int i_attstattarget;
9199 : : int i_attstorage;
9200 : : int i_typstorage;
9201 : : int i_attidentity;
9202 : : int i_attgenerated;
9203 : : int i_attisdropped;
9204 : : int i_attlen;
9205 : : int i_attalign;
9206 : : int i_attislocal;
9207 : : int i_notnull_name;
9208 : : int i_notnull_comment;
9209 : : int i_notnull_noinherit;
9210 : : int i_notnull_islocal;
9211 : : int i_notnull_invalidoid;
9212 : : int i_attoptions;
9213 : : int i_attversion;
9214 : : int i_attcollation;
9215 : : int i_attcompression;
9216 : : int i_attfdwoptions;
9217 : : int i_attmissingval;
9218 : : int i_atthasdef;
9219 : :
9220 : : /*
9221 : : * We want to perform just one query against pg_attribute, and then just
9222 : : * one against pg_attrdef (for DEFAULTs) and two against pg_constraint
9223 : : * (for CHECK constraints and for NOT NULL constraints). However, we
9224 : : * mustn't try to select every row of those catalogs and then sort it out
9225 : : * on the client side, because some of the server-side functions we need
9226 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
9227 : : * build an array of the OIDs of tables we care about (and now have lock
9228 : : * on!), and use a WHERE clause to constrain which rows are selected.
9229 : : */
9230 : 215 : appendPQExpBufferChar(tbloids, '{');
9231 : 215 : appendPQExpBufferChar(checkoids, '{');
9232 [ + + ]: 57593 : for (int i = 0; i < numTables; i++)
9233 : : {
9234 : 57378 : TableInfo *tbinfo = &tblinfo[i];
9235 : :
9236 : : /* Don't bother to collect info for sequences */
9237 [ + + ]: 57378 : if (tbinfo->relkind == RELKIND_SEQUENCE)
9238 : 663 : continue;
9239 : :
9240 : : /*
9241 : : * Don't bother with uninteresting tables, either. For binary
9242 : : * upgrades, this is bypassed for pg_largeobject_metadata and
9243 : : * pg_shdepend so that the columns names are collected for the
9244 : : * corresponding COPY commands. Restoring the data for those catalogs
9245 : : * is faster than restoring the equivalent set of large object
9246 : : * commands.
9247 : : */
9248 [ + + ]: 56715 : if (!tbinfo->interesting &&
9249 [ + + ]: 49474 : !(fout->dopt->binary_upgrade &&
9250 [ + + ]: 9750 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9251 [ + + ]: 9706 : tbinfo->dobj.catId.oid == SharedDependRelationId)))
9252 : 49386 : continue;
9253 : :
9254 : : /* OK, we need info for this table */
9255 [ + + ]: 7329 : if (tbloids->len > 1) /* do we have more than the '{'? */
9256 : 7160 : appendPQExpBufferChar(tbloids, ',');
9257 : 7329 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9258 : :
9259 [ + + ]: 7329 : if (tbinfo->ncheck > 0)
9260 : : {
9261 : : /* Also make a list of the ones with check constraints */
9262 [ + + ]: 548 : if (checkoids->len > 1) /* do we have more than the '{'? */
9263 : 476 : appendPQExpBufferChar(checkoids, ',');
9264 : 548 : appendPQExpBuffer(checkoids, "%u", tbinfo->dobj.catId.oid);
9265 : : }
9266 : : }
9267 : 215 : appendPQExpBufferChar(tbloids, '}');
9268 : 215 : appendPQExpBufferChar(checkoids, '}');
9269 : :
9270 : : /*
9271 : : * Find all the user attributes and their types.
9272 : : *
9273 : : * Since we only want to dump COLLATE clauses for attributes whose
9274 : : * collation is different from their type's default, we use a CASE here to
9275 : : * suppress uninteresting attcollations cheaply.
9276 : : */
9277 : 215 : appendPQExpBufferStr(q,
9278 : : "SELECT\n"
9279 : : "a.attrelid,\n"
9280 : : "a.attnum,\n"
9281 : : "a.attname,\n"
9282 : : "a.attstattarget,\n"
9283 : : "a.attstorage,\n"
9284 : : "t.typstorage,\n"
9285 : : "a.atthasdef,\n"
9286 : : "a.attisdropped,\n"
9287 : : "a.attlen,\n"
9288 : : "a.attalign,\n"
9289 : : "a.attislocal,\n"
9290 : : "pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n");
9291 : :
9292 : : /*
9293 : : * Read attoptions and attversion.
9294 : : *
9295 : : * Attversion is stored as a special attoptions value in the
9296 : : * back-branches. In that case we need to remove the special attoptions
9297 : : * value.
9298 : : *
9299 : : * Attribute versioning may also have been backpatched to versions older
9300 : : * than the ones it was backpatched to upstream, so do this for all server
9301 : : * versions, without using functions missing in old versions (like
9302 : : * starts_with()).
9303 : : */
9304 [ + - ]: 215 : if (fout->remoteVersion >= 200000)
9305 : 215 : appendPQExpBufferStr(q,
9306 : : "array_to_string(a.attoptions, ', ') AS attoptions,\n"
9307 : : "a.attversion - '0/0' AS attversion,\n");
9308 : : else
9309 : 0 : appendPQExpBuffer(q,
9310 : : "array_to_string(ARRAY(\n"
9311 : : " SELECT o FROM unnest(a.attoptions) o\n"
9312 : : " WHERE substr(o, 1, %2$zu) <> '%1$s='\n"
9313 : : "), ', ') AS attoptions,\n"
9314 : : "(\n"
9315 : : " SELECT substr(o, %2$zu + 1)\n"
9316 : : " FROM unnest(a.attoptions) o\n"
9317 : : " WHERE substr(o, 1, %2$zu) = '%1$s='\n"
9318 : : ") AS attversion,\n",
9319 : : ATTRIBUTE_VERSION_ATTOPTION,
9320 : : strlen(ATTRIBUTE_VERSION_ATTOPTION) + 1);
9321 : :
9322 : 215 : appendPQExpBufferStr(q,
9323 : : "CASE WHEN a.attcollation <> t.typcollation "
9324 : : "THEN a.attcollation ELSE 0 END AS attcollation,\n"
9325 : : "pg_catalog.array_to_string(ARRAY("
9326 : : "SELECT pg_catalog.quote_ident(option_name) || "
9327 : : "' ' || pg_catalog.quote_literal(option_value) "
9328 : : "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
9329 : : "ORDER BY option_name"
9330 : : "), E',\n ') AS attfdwoptions,\n");
9331 : :
9332 : : /*
9333 : : * Find out any NOT NULL markings for each column. In 18 and up we read
9334 : : * pg_constraint to obtain the constraint name, and for valid constraints
9335 : : * also pg_description to obtain its comment. notnull_noinherit is set
9336 : : * according to the NO INHERIT property. For versions prior to 18, we
9337 : : * store an empty string as the name when a constraint is marked as
9338 : : * attnotnull (this cues dumpTableSchema to print the NOT NULL clause
9339 : : * without a name); also, such cases are never NO INHERIT.
9340 : : *
9341 : : * For invalid constraints, we need to store their OIDs for processing
9342 : : * elsewhere, so we bring the pg_constraint.oid value when the constraint
9343 : : * is invalid, and NULL otherwise. Their comments are handled not here
9344 : : * but by collectComments, because they're their own dumpable object.
9345 : : *
9346 : : * We track in notnull_islocal whether the constraint was defined directly
9347 : : * in this table or via an ancestor, for binary upgrade. flagInhAttrs
9348 : : * might modify this later.
9349 : : */
9350 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
9351 : 215 : appendPQExpBufferStr(q,
9352 : : "co.conname AS notnull_name,\n"
9353 : : "CASE WHEN co.convalidated THEN pt.description"
9354 : : " ELSE NULL END AS notnull_comment,\n"
9355 : : "CASE WHEN NOT co.convalidated THEN co.oid "
9356 : : "ELSE NULL END AS notnull_invalidoid,\n"
9357 : : "co.connoinherit AS notnull_noinherit,\n"
9358 : : "co.conislocal AS notnull_islocal,\n");
9359 : : else
9360 : 0 : appendPQExpBufferStr(q,
9361 : : "CASE WHEN a.attnotnull THEN '' ELSE NULL END AS notnull_name,\n"
9362 : : "NULL AS notnull_comment,\n"
9363 : : "NULL AS notnull_invalidoid,\n"
9364 : : "false AS notnull_noinherit,\n"
9365 : : "CASE WHEN a.attislocal THEN true\n"
9366 : : " WHEN a.attnotnull AND NOT a.attislocal THEN true\n"
9367 : : " ELSE false\n"
9368 : : "END AS notnull_islocal,\n");
9369 : :
9370 [ + - ]: 215 : if (fout->remoteVersion >= 140000)
9371 : 215 : appendPQExpBufferStr(q,
9372 : : "a.attcompression AS attcompression,\n");
9373 : : else
9374 : 0 : appendPQExpBufferStr(q,
9375 : : "'' AS attcompression,\n");
9376 : :
9377 : 215 : appendPQExpBufferStr(q,
9378 : : "a.attidentity,\n");
9379 : :
9380 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
9381 : 215 : appendPQExpBufferStr(q,
9382 : : "CASE WHEN a.atthasmissing AND NOT a.attisdropped "
9383 : : "THEN a.attmissingval ELSE null END AS attmissingval,\n");
9384 : : else
9385 : 0 : appendPQExpBufferStr(q,
9386 : : "NULL AS attmissingval,\n");
9387 : :
9388 [ + - ]: 215 : if (fout->remoteVersion >= 120000)
9389 : 215 : appendPQExpBufferStr(q,
9390 : : "a.attgenerated\n");
9391 : : else
9392 : 0 : appendPQExpBufferStr(q,
9393 : : "'' AS attgenerated\n");
9394 : :
9395 : : /* need left join to pg_type to not fail on dropped columns ... */
9396 : 215 : appendPQExpBuffer(q,
9397 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9398 : : "JOIN pg_catalog.pg_attribute a ON (src.tbloid = a.attrelid) "
9399 : : "LEFT JOIN pg_catalog.pg_type t "
9400 : : "ON (a.atttypid = t.oid)\n",
9401 : : tbloids->data);
9402 : :
9403 : : /*
9404 : : * In versions 18 and up, we need pg_constraint for explicit NOT NULL
9405 : : * entries and pg_description to get their comments.
9406 : : */
9407 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
9408 : 215 : appendPQExpBufferStr(q,
9409 : : " LEFT JOIN pg_catalog.pg_constraint co ON "
9410 : : "(a.attrelid = co.conrelid\n"
9411 : : " AND co.contype = 'n' AND "
9412 : : "co.conkey = array[a.attnum])\n"
9413 : : " LEFT JOIN pg_catalog.pg_description pt ON "
9414 : : "(pt.classoid = co.tableoid AND pt.objoid = co.oid)\n");
9415 : :
9416 : 215 : appendPQExpBufferStr(q,
9417 : : "WHERE a.attnum > 0::pg_catalog.int2\n");
9418 : :
9419 : : /*
9420 : : * For binary upgrades from <v12, be sure to pick up
9421 : : * pg_largeobject_metadata's oid column.
9422 : : */
9423 [ + + - + ]: 215 : if (fout->dopt->binary_upgrade && fout->remoteVersion < 120000)
9424 : 0 : appendPQExpBufferStr(q,
9425 : : "OR (a.attnum = -2::pg_catalog.int2 AND src.tbloid = "
9426 : : CppAsString2(LargeObjectMetadataRelationId) ")\n");
9427 : :
9428 : 215 : appendPQExpBufferStr(q,
9429 : : "ORDER BY a.attrelid, a.attnum");
9430 : :
9431 : 215 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9432 : :
9433 : 215 : ntups = PQntuples(res);
9434 : :
9435 : 215 : i_attrelid = PQfnumber(res, "attrelid");
9436 : 215 : i_attnum = PQfnumber(res, "attnum");
9437 : 215 : i_attname = PQfnumber(res, "attname");
9438 : 215 : i_atttypname = PQfnumber(res, "atttypname");
9439 : 215 : i_attstattarget = PQfnumber(res, "attstattarget");
9440 : 215 : i_attstorage = PQfnumber(res, "attstorage");
9441 : 215 : i_typstorage = PQfnumber(res, "typstorage");
9442 : 215 : i_attidentity = PQfnumber(res, "attidentity");
9443 : 215 : i_attgenerated = PQfnumber(res, "attgenerated");
9444 : 215 : i_attisdropped = PQfnumber(res, "attisdropped");
9445 : 215 : i_attlen = PQfnumber(res, "attlen");
9446 : 215 : i_attalign = PQfnumber(res, "attalign");
9447 : 215 : i_attislocal = PQfnumber(res, "attislocal");
9448 : 215 : i_notnull_name = PQfnumber(res, "notnull_name");
9449 : 215 : i_notnull_comment = PQfnumber(res, "notnull_comment");
9450 : 215 : i_notnull_invalidoid = PQfnumber(res, "notnull_invalidoid");
9451 : 215 : i_notnull_noinherit = PQfnumber(res, "notnull_noinherit");
9452 : 215 : i_notnull_islocal = PQfnumber(res, "notnull_islocal");
9453 : 215 : i_attoptions = PQfnumber(res, "attoptions");
9454 : 215 : i_attversion = PQfnumber(res, "attversion");
9455 : 215 : i_attcollation = PQfnumber(res, "attcollation");
9456 : 215 : i_attcompression = PQfnumber(res, "attcompression");
9457 : 215 : i_attfdwoptions = PQfnumber(res, "attfdwoptions");
9458 : 215 : i_attmissingval = PQfnumber(res, "attmissingval");
9459 : 215 : i_atthasdef = PQfnumber(res, "atthasdef");
9460 : :
9461 : : /* Within the next loop, we'll accumulate OIDs of tables with defaults */
9462 : 215 : resetPQExpBuffer(tbloids);
9463 : 215 : appendPQExpBufferChar(tbloids, '{');
9464 : :
9465 : : /*
9466 : : * Outer loop iterates once per table, not once per row. Incrementing of
9467 : : * r is handled by the inner loop.
9468 : : */
9469 : 215 : curtblindx = -1;
9470 [ + + ]: 7397 : for (int r = 0; r < ntups;)
9471 : : {
9472 : 7182 : Oid attrelid = atooid(PQgetvalue(res, r, i_attrelid));
9473 : 7182 : TableInfo *tbinfo = NULL;
9474 : : int numatts;
9475 : : bool hasdefaults;
9476 : :
9477 : : /* Count rows for this table */
9478 [ + + ]: 26446 : for (numatts = 1; numatts < ntups - r; numatts++)
9479 [ + + ]: 26280 : if (atooid(PQgetvalue(res, r + numatts, i_attrelid)) != attrelid)
9480 : 7016 : break;
9481 : :
9482 : : /*
9483 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
9484 : : * order.
9485 : : */
9486 [ + - ]: 40230 : while (++curtblindx < numTables)
9487 : : {
9488 : 40230 : tbinfo = &tblinfo[curtblindx];
9489 [ + + ]: 40230 : if (tbinfo->dobj.catId.oid == attrelid)
9490 : 7182 : break;
9491 : : }
9492 [ - + ]: 7182 : if (curtblindx >= numTables)
9493 : 0 : pg_fatal("unrecognized table OID %u", attrelid);
9494 : : /* cross-check that we only got requested tables */
9495 [ + - ]: 7182 : if (tbinfo->relkind == RELKIND_SEQUENCE ||
9496 [ + + ]: 7182 : (!tbinfo->interesting &&
9497 [ + - ]: 88 : !(fout->dopt->binary_upgrade &&
9498 [ + + ]: 88 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9499 [ - + ]: 44 : tbinfo->dobj.catId.oid == SharedDependRelationId))))
9500 : 0 : pg_fatal("unexpected column data for table \"%s\"",
9501 : : tbinfo->dobj.name);
9502 : :
9503 : : /* Save data for this table */
9504 : 7182 : tbinfo->numatts = numatts;
9505 : 7182 : tbinfo->attnames = pg_malloc_array(char *, numatts);
9506 : 7182 : tbinfo->atttypnames = pg_malloc_array(char *, numatts);
9507 : 7182 : tbinfo->attstattarget = pg_malloc_array(int, numatts);
9508 : 7182 : tbinfo->attstorage = pg_malloc_array(char, numatts);
9509 : 7182 : tbinfo->typstorage = pg_malloc_array(char, numatts);
9510 : 7182 : tbinfo->attidentity = pg_malloc_array(char, numatts);
9511 : 7182 : tbinfo->attgenerated = pg_malloc_array(char, numatts);
9512 : 7182 : tbinfo->attisdropped = pg_malloc_array(bool, numatts);
9513 : 7182 : tbinfo->attlen = pg_malloc_array(int, numatts);
9514 : 7182 : tbinfo->attalign = pg_malloc_array(char, numatts);
9515 : 7182 : tbinfo->attislocal = pg_malloc_array(bool, numatts);
9516 : 7182 : tbinfo->attoptions = pg_malloc_array(char *, numatts);
9517 : 7182 : tbinfo->attversion = pg_malloc_array(uint64, numatts);
9518 : 7182 : tbinfo->attcollation = pg_malloc_array(Oid, numatts);
9519 : 7182 : tbinfo->attcompression = pg_malloc_array(char, numatts);
9520 : 7182 : tbinfo->attfdwoptions = pg_malloc_array(char *, numatts);
9521 : 7182 : tbinfo->attmissingval = pg_malloc_array(char *, numatts);
9522 : 7182 : tbinfo->notnull_constrs = pg_malloc_array(char *, numatts);
9523 : 7182 : tbinfo->notnull_comment = pg_malloc_array(char *, numatts);
9524 : 7182 : tbinfo->notnull_invalid = pg_malloc_array(bool, numatts);
9525 : 7182 : tbinfo->notnull_noinh = pg_malloc_array(bool, numatts);
9526 : 7182 : tbinfo->notnull_islocal = pg_malloc_array(bool, numatts);
9527 : 7182 : tbinfo->attrdefs = pg_malloc_array(AttrDefInfo *, numatts);
9528 : 7182 : hasdefaults = false;
9529 : :
9530 [ + + ]: 33628 : for (int j = 0; j < numatts; j++, r++)
9531 : : {
9532 [ - + ]: 26446 : if (j + 1 != atoi(PQgetvalue(res, r, i_attnum)) &&
9533 [ # # # # ]: 0 : !(fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
9534 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
9535 : 0 : pg_fatal("invalid column numbering in table \"%s\"",
9536 : : tbinfo->dobj.name);
9537 : 26446 : tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, r, i_attname));
9538 : 26446 : tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, r, i_atttypname));
9539 [ + + ]: 26446 : if (PQgetisnull(res, r, i_attstattarget))
9540 : 26403 : tbinfo->attstattarget[j] = -1;
9541 : : else
9542 : 43 : tbinfo->attstattarget[j] = atoi(PQgetvalue(res, r, i_attstattarget));
9543 : 26446 : tbinfo->attstorage[j] = *(PQgetvalue(res, r, i_attstorage));
9544 : 26446 : tbinfo->typstorage[j] = *(PQgetvalue(res, r, i_typstorage));
9545 : 26446 : tbinfo->attidentity[j] = *(PQgetvalue(res, r, i_attidentity));
9546 : 26446 : tbinfo->attgenerated[j] = *(PQgetvalue(res, r, i_attgenerated));
9547 : 26446 : tbinfo->attisdropped[j] = (PQgetvalue(res, r, i_attisdropped)[0] == 't');
9548 : :
9549 : : /*
9550 : : * ALTER TABLE DROP COLUMN does not clear attidentity, so we must
9551 : : * ignore dropped columns here or we'd emit a spurious OVERRIDING
9552 : : * SYSTEM VALUE clause for the table's data.
9553 : : */
9554 [ + + ]: 26446 : if (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS &&
9555 [ + + ]: 178 : !tbinfo->attisdropped[j])
9556 : 137 : tbinfo->needs_override = true;
9557 : :
9558 : 26446 : tbinfo->attlen[j] = atoi(PQgetvalue(res, r, i_attlen));
9559 : 26446 : tbinfo->attalign[j] = *(PQgetvalue(res, r, i_attalign));
9560 : 26446 : tbinfo->attislocal[j] = (PQgetvalue(res, r, i_attislocal)[0] == 't');
9561 : :
9562 : : /* Handle not-null constraint name and flags */
9563 : 26446 : determineNotNullFlags(fout, res, r,
9564 : : tbinfo, j,
9565 : : i_notnull_name,
9566 : : i_notnull_comment,
9567 : : i_notnull_invalidoid,
9568 : : i_notnull_noinherit,
9569 : : i_notnull_islocal,
9570 : : &invalidnotnulloids);
9571 : :
9572 : 26446 : tbinfo->notnull_comment[j] = PQgetisnull(res, r, i_notnull_comment) ?
9573 [ + + ]: 26446 : NULL : pg_strdup(PQgetvalue(res, r, i_notnull_comment));
9574 : 26446 : tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, r, i_attoptions));
9575 [ - + ]: 26446 : if (PQgetisnull(res, r, i_attversion))
9576 : 0 : tbinfo->attversion[j] = 0;
9577 : : else
9578 : 26446 : tbinfo->attversion[j] = strtou64(PQgetvalue(res, r, i_attversion), NULL, 10);
9579 : 26446 : tbinfo->attcollation[j] = atooid(PQgetvalue(res, r, i_attcollation));
9580 : 26446 : tbinfo->attcompression[j] = *(PQgetvalue(res, r, i_attcompression));
9581 : 26446 : tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, r, i_attfdwoptions));
9582 : 26446 : tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, r, i_attmissingval));
9583 : 26446 : tbinfo->attrdefs[j] = NULL; /* fix below */
9584 [ + + ]: 26446 : if (PQgetvalue(res, r, i_atthasdef)[0] == 't')
9585 : 1398 : hasdefaults = true;
9586 : : }
9587 : :
9588 [ + + ]: 7182 : if (hasdefaults)
9589 : : {
9590 : : /* Collect OIDs of interesting tables that have defaults */
9591 [ + + ]: 1048 : if (tbloids->len > 1) /* do we have more than the '{'? */
9592 : 961 : appendPQExpBufferChar(tbloids, ',');
9593 : 1048 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9594 : : }
9595 : : }
9596 : :
9597 : : /* If invalidnotnulloids has any data, finalize it */
9598 [ + + ]: 215 : if (invalidnotnulloids != NULL)
9599 : 46 : appendPQExpBufferChar(invalidnotnulloids, '}');
9600 : :
9601 : 215 : PQclear(res);
9602 : :
9603 : : /*
9604 : : * Now get info about column defaults. This is skipped for a data-only
9605 : : * dump, as it is only needed for table schemas.
9606 : : */
9607 [ + + + + ]: 215 : if (dopt->dumpSchema && tbloids->len > 1)
9608 : : {
9609 : : AttrDefInfo *attrdefs;
9610 : : int numDefaults;
9611 : 78 : TableInfo *tbinfo = NULL;
9612 : :
9613 : 78 : pg_log_info("finding table default expressions");
9614 : :
9615 : 78 : appendPQExpBufferChar(tbloids, '}');
9616 : :
9617 : 78 : printfPQExpBuffer(q, "SELECT a.tableoid, a.oid, adrelid, adnum, "
9618 : : "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc\n"
9619 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9620 : : "JOIN pg_catalog.pg_attrdef a ON (src.tbloid = a.adrelid)\n"
9621 : : "ORDER BY a.adrelid, a.adnum",
9622 : : tbloids->data);
9623 : :
9624 : 78 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9625 : :
9626 : 78 : numDefaults = PQntuples(res);
9627 : 78 : attrdefs = pg_malloc_array(AttrDefInfo, numDefaults);
9628 : :
9629 : 78 : curtblindx = -1;
9630 [ + + ]: 1367 : for (int j = 0; j < numDefaults; j++)
9631 : : {
9632 : 1289 : Oid adtableoid = atooid(PQgetvalue(res, j, 0));
9633 : 1289 : Oid adoid = atooid(PQgetvalue(res, j, 1));
9634 : 1289 : Oid adrelid = atooid(PQgetvalue(res, j, 2));
9635 : 1289 : int adnum = atoi(PQgetvalue(res, j, 3));
9636 : 1289 : char *adsrc = PQgetvalue(res, j, 4);
9637 : :
9638 : : /*
9639 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9640 : : * OID order.
9641 : : */
9642 [ + + + + ]: 1289 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != adrelid)
9643 : : {
9644 [ + - ]: 23485 : while (++curtblindx < numTables)
9645 : : {
9646 : 23485 : tbinfo = &tblinfo[curtblindx];
9647 [ + + ]: 23485 : if (tbinfo->dobj.catId.oid == adrelid)
9648 : 973 : break;
9649 : : }
9650 [ - + ]: 973 : if (curtblindx >= numTables)
9651 : 0 : pg_fatal("unrecognized table OID %u", adrelid);
9652 : : }
9653 : :
9654 [ + - - + ]: 1289 : if (adnum <= 0 || adnum > tbinfo->numatts)
9655 : 0 : pg_fatal("invalid adnum value %d for table \"%s\"",
9656 : : adnum, tbinfo->dobj.name);
9657 : :
9658 : : /*
9659 : : * dropped columns shouldn't have defaults, but just in case,
9660 : : * ignore 'em
9661 : : */
9662 [ - + ]: 1289 : if (tbinfo->attisdropped[adnum - 1])
9663 : 0 : continue;
9664 : :
9665 : 1289 : attrdefs[j].dobj.objType = DO_ATTRDEF;
9666 : 1289 : attrdefs[j].dobj.catId.tableoid = adtableoid;
9667 : 1289 : attrdefs[j].dobj.catId.oid = adoid;
9668 : 1289 : AssignDumpId(&attrdefs[j].dobj);
9669 : 1289 : attrdefs[j].adtable = tbinfo;
9670 : 1289 : attrdefs[j].adnum = adnum;
9671 : 1289 : attrdefs[j].adef_expr = pg_strdup(adsrc);
9672 : :
9673 : 1289 : attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name);
9674 : 1289 : attrdefs[j].dobj.namespace = tbinfo->dobj.namespace;
9675 : :
9676 : 1289 : attrdefs[j].dobj.dump = tbinfo->dobj.dump;
9677 : :
9678 : : /*
9679 : : * Figure out whether the default/generation expression should be
9680 : : * dumped as part of the main CREATE TABLE (or similar) command or
9681 : : * as a separate ALTER TABLE (or similar) command. The preference
9682 : : * is to put it into the CREATE command, but in some cases that's
9683 : : * not possible.
9684 : : */
9685 [ + + ]: 1289 : if (tbinfo->attgenerated[adnum - 1])
9686 : : {
9687 : : /*
9688 : : * Column generation expressions cannot be dumped separately,
9689 : : * because there is no syntax for it. By setting separate to
9690 : : * false here we prevent the "default" from being processed as
9691 : : * its own dumpable object. Later, flagInhAttrs() will mark
9692 : : * it as not to be dumped at all, if possible (that is, if it
9693 : : * can be inherited from a parent).
9694 : : */
9695 : 754 : attrdefs[j].separate = false;
9696 : : }
9697 [ + + ]: 535 : else if (tbinfo->relkind == RELKIND_VIEW)
9698 : : {
9699 : : /*
9700 : : * Defaults on a VIEW must always be dumped as separate ALTER
9701 : : * TABLE commands.
9702 : : */
9703 : 34 : attrdefs[j].separate = true;
9704 : : }
9705 [ + + ]: 501 : else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1))
9706 : : {
9707 : : /* column will be suppressed, print default separately */
9708 : 4 : attrdefs[j].separate = true;
9709 : : }
9710 : : else
9711 : : {
9712 : 497 : attrdefs[j].separate = false;
9713 : : }
9714 : :
9715 [ + + ]: 1289 : if (!attrdefs[j].separate)
9716 : : {
9717 : : /*
9718 : : * Mark the default as needing to appear before the table, so
9719 : : * that any dependencies it has must be emitted before the
9720 : : * CREATE TABLE. If this is not possible, we'll change to
9721 : : * "separate" mode while sorting dependencies.
9722 : : */
9723 : 1251 : addObjectDependency(&tbinfo->dobj,
9724 : 1251 : attrdefs[j].dobj.dumpId);
9725 : : }
9726 : :
9727 : 1289 : tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
9728 : : }
9729 : :
9730 : 78 : PQclear(res);
9731 : : }
9732 : :
9733 : : /*
9734 : : * Get info about NOT NULL NOT VALID constraints. This is skipped for a
9735 : : * data-only dump, as it is only needed for table schemas.
9736 : : */
9737 [ + + + + ]: 215 : if (dopt->dumpSchema && invalidnotnulloids)
9738 : : {
9739 : : ConstraintInfo *constrs;
9740 : : int numConstrs;
9741 : : int i_tableoid;
9742 : : int i_oid;
9743 : : int i_conrelid;
9744 : : int i_conname;
9745 : : int i_consrc;
9746 : : int i_conislocal;
9747 : :
9748 : 39 : pg_log_info("finding invalid not-null constraints");
9749 : :
9750 : 39 : resetPQExpBuffer(q);
9751 : 39 : appendPQExpBuffer(q,
9752 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9753 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9754 : : "conislocal, convalidated "
9755 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(conoid)\n"
9756 : : "JOIN pg_catalog.pg_constraint c ON (src.conoid = c.oid)\n"
9757 : : "ORDER BY c.conrelid, c.conname",
9758 : 39 : invalidnotnulloids->data);
9759 : :
9760 : 39 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9761 : :
9762 : 39 : numConstrs = PQntuples(res);
9763 : 39 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9764 : :
9765 : 39 : i_tableoid = PQfnumber(res, "tableoid");
9766 : 39 : i_oid = PQfnumber(res, "oid");
9767 : 39 : i_conrelid = PQfnumber(res, "conrelid");
9768 : 39 : i_conname = PQfnumber(res, "conname");
9769 : 39 : i_consrc = PQfnumber(res, "consrc");
9770 : 39 : i_conislocal = PQfnumber(res, "conislocal");
9771 : :
9772 : : /* As above, this loop iterates once per table, not once per row */
9773 : 39 : curtblindx = -1;
9774 [ + + ]: 108 : for (int j = 0; j < numConstrs;)
9775 : : {
9776 : 69 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9777 : 69 : TableInfo *tbinfo = NULL;
9778 : : int numcons;
9779 : :
9780 : : /* Count rows for this table */
9781 [ + + ]: 69 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9782 [ + - ]: 30 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9783 : 30 : break;
9784 : :
9785 : : /*
9786 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9787 : : * OID order.
9788 : : */
9789 [ + - ]: 13702 : while (++curtblindx < numTables)
9790 : : {
9791 : 13702 : tbinfo = &tblinfo[curtblindx];
9792 [ + + ]: 13702 : if (tbinfo->dobj.catId.oid == conrelid)
9793 : 69 : break;
9794 : : }
9795 [ - + ]: 69 : if (curtblindx >= numTables)
9796 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9797 : :
9798 [ + + ]: 138 : for (int c = 0; c < numcons; c++, j++)
9799 : : {
9800 : 69 : constrs[j].dobj.objType = DO_CONSTRAINT;
9801 : 69 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9802 : 69 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9803 : 69 : AssignDumpId(&constrs[j].dobj);
9804 : 69 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9805 : 69 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9806 : 69 : constrs[j].contable = tbinfo;
9807 : 69 : constrs[j].condomain = NULL;
9808 : 69 : constrs[j].contype = 'n';
9809 : 69 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9810 : 69 : constrs[j].confrelid = InvalidOid;
9811 : 69 : constrs[j].conindex = 0;
9812 : 69 : constrs[j].condeferrable = false;
9813 : 69 : constrs[j].condeferred = false;
9814 : 69 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9815 : :
9816 : : /*
9817 : : * All invalid not-null constraints must be dumped separately,
9818 : : * because CREATE TABLE would not create them as invalid, and
9819 : : * also because they must be created after potentially
9820 : : * violating data has been loaded.
9821 : : */
9822 : 69 : constrs[j].separate = true;
9823 : :
9824 : 69 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9825 : : }
9826 : : }
9827 : 39 : PQclear(res);
9828 : : }
9829 : :
9830 : : /*
9831 : : * Get info about table CHECK constraints. This is skipped for a
9832 : : * data-only dump, as it is only needed for table schemas.
9833 : : */
9834 [ + + + + ]: 215 : if (dopt->dumpSchema && checkoids->len > 2)
9835 : : {
9836 : : ConstraintInfo *constrs;
9837 : : int numConstrs;
9838 : : int i_tableoid;
9839 : : int i_oid;
9840 : : int i_conrelid;
9841 : : int i_conname;
9842 : : int i_consrc;
9843 : : int i_conislocal;
9844 : : int i_convalidated;
9845 : :
9846 : 63 : pg_log_info("finding table check constraints");
9847 : :
9848 : 63 : resetPQExpBuffer(q);
9849 : 63 : appendPQExpBuffer(q,
9850 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9851 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9852 : : "conislocal, convalidated "
9853 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9854 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
9855 : : "WHERE contype = 'c' "
9856 : : "ORDER BY c.conrelid, c.conname",
9857 : : checkoids->data);
9858 : :
9859 : 63 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9860 : :
9861 : 63 : numConstrs = PQntuples(res);
9862 : 63 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9863 : :
9864 : 63 : i_tableoid = PQfnumber(res, "tableoid");
9865 : 63 : i_oid = PQfnumber(res, "oid");
9866 : 63 : i_conrelid = PQfnumber(res, "conrelid");
9867 : 63 : i_conname = PQfnumber(res, "conname");
9868 : 63 : i_consrc = PQfnumber(res, "consrc");
9869 : 63 : i_conislocal = PQfnumber(res, "conislocal");
9870 : 63 : i_convalidated = PQfnumber(res, "convalidated");
9871 : :
9872 : : /* As above, this loop iterates once per table, not once per row */
9873 : 63 : curtblindx = -1;
9874 [ + + ]: 556 : for (int j = 0; j < numConstrs;)
9875 : : {
9876 : 493 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9877 : 493 : TableInfo *tbinfo = NULL;
9878 : : int numcons;
9879 : :
9880 : : /* Count rows for this table */
9881 [ + + ]: 632 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9882 [ + + ]: 569 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9883 : 430 : break;
9884 : :
9885 : : /*
9886 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9887 : : * OID order.
9888 : : */
9889 [ + - ]: 19111 : while (++curtblindx < numTables)
9890 : : {
9891 : 19111 : tbinfo = &tblinfo[curtblindx];
9892 [ + + ]: 19111 : if (tbinfo->dobj.catId.oid == conrelid)
9893 : 493 : break;
9894 : : }
9895 [ - + ]: 493 : if (curtblindx >= numTables)
9896 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9897 : :
9898 [ - + ]: 493 : if (numcons != tbinfo->ncheck)
9899 : : {
9900 : 0 : pg_log_error(ngettext("expected %d check constraint on table \"%s\" but found %d",
9901 : : "expected %d check constraints on table \"%s\" but found %d",
9902 : : tbinfo->ncheck),
9903 : : tbinfo->ncheck, tbinfo->dobj.name, numcons);
9904 : 0 : pg_log_error_hint("The system catalogs might be corrupted.");
9905 : 0 : exit_nicely(1);
9906 : : }
9907 : :
9908 : 493 : tbinfo->checkexprs = constrs + j;
9909 : :
9910 [ + + ]: 1125 : for (int c = 0; c < numcons; c++, j++)
9911 : : {
9912 : 632 : bool validated = PQgetvalue(res, j, i_convalidated)[0] == 't';
9913 : :
9914 : 632 : constrs[j].dobj.objType = DO_CONSTRAINT;
9915 : 632 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9916 : 632 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9917 : 632 : AssignDumpId(&constrs[j].dobj);
9918 : 632 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9919 : 632 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9920 : 632 : constrs[j].contable = tbinfo;
9921 : 632 : constrs[j].condomain = NULL;
9922 : 632 : constrs[j].contype = 'c';
9923 : 632 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9924 : 632 : constrs[j].confrelid = InvalidOid;
9925 : 632 : constrs[j].conindex = 0;
9926 : 632 : constrs[j].condeferrable = false;
9927 : 632 : constrs[j].condeferred = false;
9928 : 632 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9929 : :
9930 : : /*
9931 : : * An unvalidated constraint needs to be dumped separately, so
9932 : : * that potentially-violating existing data is loaded before
9933 : : * the constraint.
9934 : : */
9935 : 632 : constrs[j].separate = !validated;
9936 : :
9937 : 632 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9938 : :
9939 : : /*
9940 : : * Mark the constraint as needing to appear before the table
9941 : : * --- this is so that any other dependencies of the
9942 : : * constraint will be emitted before we try to create the
9943 : : * table. If the constraint is to be dumped separately, it
9944 : : * will be dumped after data is loaded anyway, so don't do it.
9945 : : * (There's an automatic dependency in the opposite direction
9946 : : * anyway, so don't need to add one manually here.)
9947 : : */
9948 [ + + ]: 632 : if (!constrs[j].separate)
9949 : 567 : addObjectDependency(&tbinfo->dobj,
9950 : 567 : constrs[j].dobj.dumpId);
9951 : :
9952 : : /*
9953 : : * We will detect later whether the constraint must be split
9954 : : * out from the table definition.
9955 : : */
9956 : : }
9957 : : }
9958 : :
9959 : 63 : PQclear(res);
9960 : : }
9961 : :
9962 : 215 : destroyPQExpBuffer(q);
9963 : 215 : destroyPQExpBuffer(tbloids);
9964 : 215 : destroyPQExpBuffer(checkoids);
9965 : 215 : }
9966 : :
9967 : : /*
9968 : : * Based on the getTableAttrs query's row corresponding to one column, set
9969 : : * the name and flags to handle a not-null constraint for that column in
9970 : : * the tbinfo struct.
9971 : : *
9972 : : * Result row 'r' is for tbinfo's attribute 'j'.
9973 : : *
9974 : : * There are four possibilities:
9975 : : * 1) the column has no not-null constraints. In that case, ->notnull_constrs
9976 : : * (the constraint name) remains NULL.
9977 : : * 2) The column has a constraint with no name (this is the case when
9978 : : * constraints come from pre-18 servers). In this case, ->notnull_constrs
9979 : : * is set to the empty string; dumpTableSchema will print just "NOT NULL".
9980 : : * 3) The column has an invalid not-null constraint. This must be treated
9981 : : * as a separate object (because it must be created after the table data
9982 : : * is loaded). So we add its OID to invalidnotnulloids for processing
9983 : : * elsewhere and do nothing further with it here. We distinguish this
9984 : : * case because the "notnull_invalidoid" column has been set to a non-NULL
9985 : : * value, which is the constraint OID. Valid constraints have a null OID.
9986 : : * 4) The column has a constraint with a known name; in that case
9987 : : * notnull_constrs carries that name and dumpTableSchema will print
9988 : : * "CONSTRAINT the_name NOT NULL". However, if the name is the default
9989 : : * (table_column_not_null) and there's no comment on the constraint,
9990 : : * there's no need to print that name in the dump, so notnull_constrs
9991 : : * is set to the empty string and it behaves as case 2.
9992 : : *
9993 : : * In a child table that inherits from a parent already containing NOT NULL
9994 : : * constraints and the columns in the child don't have their own NOT NULL
9995 : : * declarations, we suppress printing constraints in the child: the
9996 : : * constraints are acquired at the point where the child is attached to the
9997 : : * parent. This is tracked in ->notnull_islocal; for servers pre-18 this is
9998 : : * set not here but in flagInhAttrs. That flag is also used when the
9999 : : * constraint was validated in a child but all its parent have it as NOT
10000 : : * VALID.
10001 : : *
10002 : : * Any of these constraints might have the NO INHERIT bit. If so we set
10003 : : * ->notnull_noinh and NO INHERIT will be printed by dumpTableSchema.
10004 : : *
10005 : : * In case 4 above, the name comparison is a bit of a hack; it actually fails
10006 : : * to do the right thing in all but the trivial case. However, the downside
10007 : : * of getting it wrong is simply that the name is printed rather than
10008 : : * suppressed, so it's not a big deal.
10009 : : *
10010 : : * invalidnotnulloids is expected to be given as NULL; if any invalid not-null
10011 : : * constraints are found, it is initialized and filled with the array of
10012 : : * OIDs of such constraints, for later processing.
10013 : : */
10014 : : static void
10015 : 26446 : determineNotNullFlags(Archive *fout, PGresult *res, int r,
10016 : : TableInfo *tbinfo, int j,
10017 : : int i_notnull_name,
10018 : : int i_notnull_comment,
10019 : : int i_notnull_invalidoid,
10020 : : int i_notnull_noinherit,
10021 : : int i_notnull_islocal,
10022 : : PQExpBuffer *invalidnotnulloids)
10023 : : {
10024 : 26446 : DumpOptions *dopt = fout->dopt;
10025 : :
10026 : : /*
10027 : : * If this not-null constraint is not valid, list its OID in
10028 : : * invalidnotnulloids and do nothing further. It'll be processed
10029 : : * elsewhere later.
10030 : : *
10031 : : * Because invalid not-null constraints are rare, we don't want to malloc
10032 : : * invalidnotnulloids until we're sure we're going it need it, which
10033 : : * happens here.
10034 : : */
10035 [ + + ]: 26446 : if (!PQgetisnull(res, r, i_notnull_invalidoid))
10036 : : {
10037 : 76 : char *constroid = PQgetvalue(res, r, i_notnull_invalidoid);
10038 : :
10039 [ + + ]: 76 : if (*invalidnotnulloids == NULL)
10040 : : {
10041 : 46 : *invalidnotnulloids = createPQExpBuffer();
10042 : 46 : appendPQExpBufferChar(*invalidnotnulloids, '{');
10043 : 46 : appendPQExpBufferStr(*invalidnotnulloids, constroid);
10044 : : }
10045 : : else
10046 : 30 : appendPQExpBuffer(*invalidnotnulloids, ",%s", constroid);
10047 : :
10048 : : /*
10049 : : * Track when a parent constraint is invalid for the cases where a
10050 : : * child constraint has been validated independenly.
10051 : : */
10052 : 76 : tbinfo->notnull_invalid[j] = true;
10053 : :
10054 : : /* nothing else to do */
10055 : 76 : tbinfo->notnull_constrs[j] = NULL;
10056 : 76 : return;
10057 : : }
10058 : :
10059 : : /*
10060 : : * notnull_noinh is straight from the query result. notnull_islocal also,
10061 : : * though flagInhAttrs may change that one later.
10062 : : */
10063 : 26370 : tbinfo->notnull_noinh[j] = PQgetvalue(res, r, i_notnull_noinherit)[0] == 't';
10064 : 26370 : tbinfo->notnull_islocal[j] = PQgetvalue(res, r, i_notnull_islocal)[0] == 't';
10065 : 26370 : tbinfo->notnull_invalid[j] = false;
10066 : :
10067 : : /*
10068 : : * Determine a constraint name to use. If the column is not marked not-
10069 : : * null, we set NULL which cues ... to do nothing. An empty string says
10070 : : * to print an unnamed NOT NULL, and anything else is a constraint name to
10071 : : * use.
10072 : : */
10073 [ - + ]: 26370 : if (fout->remoteVersion < 180000)
10074 : : {
10075 : : /*
10076 : : * < 18 doesn't have not-null names, so an unnamed constraint is
10077 : : * sufficient.
10078 : : */
10079 [ # # ]: 0 : if (PQgetisnull(res, r, i_notnull_name))
10080 : 0 : tbinfo->notnull_constrs[j] = NULL;
10081 : : else
10082 : 0 : tbinfo->notnull_constrs[j] = "";
10083 : : }
10084 : : else
10085 : : {
10086 [ + + ]: 26370 : if (PQgetisnull(res, r, i_notnull_name))
10087 : 23523 : tbinfo->notnull_constrs[j] = NULL;
10088 : : else
10089 : : {
10090 : : /*
10091 : : * In binary upgrade of inheritance child tables, must have a
10092 : : * constraint name that we can UPDATE later; same if there's a
10093 : : * comment on the constraint.
10094 : : */
10095 [ + + ]: 2847 : if ((dopt->binary_upgrade &&
10096 [ + + ]: 348 : !tbinfo->ispartition &&
10097 [ + + + + ]: 3090 : !tbinfo->notnull_islocal[j]) ||
10098 : 2826 : !PQgetisnull(res, r, i_notnull_comment))
10099 : : {
10100 : 70 : tbinfo->notnull_constrs[j] =
10101 : 70 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10102 : : }
10103 : : else
10104 : : {
10105 : : char *default_name;
10106 : :
10107 : : /* XXX should match ChooseConstraintName better */
10108 : 2777 : default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name,
10109 : 2777 : tbinfo->attnames[j]);
10110 [ + + ]: 2777 : if (strcmp(default_name,
10111 : 2777 : PQgetvalue(res, r, i_notnull_name)) == 0)
10112 : 1833 : tbinfo->notnull_constrs[j] = "";
10113 : : else
10114 : : {
10115 : 944 : tbinfo->notnull_constrs[j] =
10116 : 944 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10117 : : }
10118 : 2777 : pfree(default_name);
10119 : : }
10120 : : }
10121 : : }
10122 : : }
10123 : :
10124 : : /*
10125 : : * Test whether a column should be printed as part of table's CREATE TABLE.
10126 : : * Column number is zero-based.
10127 : : *
10128 : : * Normally this is always true, but it's false for dropped columns, as well
10129 : : * as those that were inherited without any local definition. (If we print
10130 : : * such a column it will mistakenly get pg_attribute.attislocal set to true.)
10131 : : * For partitions, it's always true, because we want the partitions to be
10132 : : * created independently and ATTACH PARTITION used afterwards.
10133 : : *
10134 : : * In binary_upgrade mode, we must print all columns and fix the attislocal/
10135 : : * attisdropped state later, so as to keep control of the physical column
10136 : : * order.
10137 : : *
10138 : : * This function exists because there are scattered nonobvious places that
10139 : : * must be kept in sync with this decision.
10140 : : */
10141 : : bool
10142 : 42519 : shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
10143 : : {
10144 [ + + ]: 42519 : if (dopt->binary_upgrade)
10145 : 6469 : return true;
10146 [ + + ]: 36050 : if (tbinfo->attisdropped[colno])
10147 : 912 : return false;
10148 [ + + + + ]: 35138 : return (tbinfo->attislocal[colno] || tbinfo->ispartition);
10149 : : }
10150 : :
10151 : :
10152 : : /*
10153 : : * getTSParsers:
10154 : : * get information about all text search parsers in the system catalogs
10155 : : */
10156 : : void
10157 : 215 : getTSParsers(Archive *fout)
10158 : : {
10159 : : PGresult *res;
10160 : : int ntups;
10161 : : int i;
10162 : : PQExpBuffer query;
10163 : : TSParserInfo *prsinfo;
10164 : : int i_tableoid;
10165 : : int i_oid;
10166 : : int i_prsname;
10167 : : int i_prsnamespace;
10168 : : int i_prsstart;
10169 : : int i_prstoken;
10170 : : int i_prsend;
10171 : : int i_prsheadline;
10172 : : int i_prslextype;
10173 : :
10174 : 215 : query = createPQExpBuffer();
10175 : :
10176 : : /*
10177 : : * find all text search objects, including builtin ones; we filter out
10178 : : * system-defined objects at dump-out time.
10179 : : */
10180 : :
10181 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, "
10182 : : "prsstart::oid, prstoken::oid, "
10183 : : "prsend::oid, prsheadline::oid, prslextype::oid "
10184 : : "FROM pg_ts_parser");
10185 : :
10186 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10187 : :
10188 : 215 : ntups = PQntuples(res);
10189 : :
10190 : 215 : prsinfo = pg_malloc_array(TSParserInfo, ntups);
10191 : :
10192 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10193 : 215 : i_oid = PQfnumber(res, "oid");
10194 : 215 : i_prsname = PQfnumber(res, "prsname");
10195 : 215 : i_prsnamespace = PQfnumber(res, "prsnamespace");
10196 : 215 : i_prsstart = PQfnumber(res, "prsstart");
10197 : 215 : i_prstoken = PQfnumber(res, "prstoken");
10198 : 215 : i_prsend = PQfnumber(res, "prsend");
10199 : 215 : i_prsheadline = PQfnumber(res, "prsheadline");
10200 : 215 : i_prslextype = PQfnumber(res, "prslextype");
10201 : :
10202 [ + + ]: 478 : for (i = 0; i < ntups; i++)
10203 : : {
10204 : 263 : prsinfo[i].dobj.objType = DO_TSPARSER;
10205 : 263 : prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10206 : 263 : prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10207 : 263 : AssignDumpId(&prsinfo[i].dobj);
10208 : 263 : prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
10209 : 526 : prsinfo[i].dobj.namespace =
10210 : 263 : findNamespace(atooid(PQgetvalue(res, i, i_prsnamespace)));
10211 : 263 : prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
10212 : 263 : prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
10213 : 263 : prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
10214 : 263 : prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline));
10215 : 263 : prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype));
10216 : :
10217 : : /* Decide whether we want to dump it */
10218 : 263 : selectDumpableObject(&(prsinfo[i].dobj), fout);
10219 : : }
10220 : :
10221 : 215 : PQclear(res);
10222 : :
10223 : 215 : destroyPQExpBuffer(query);
10224 : 215 : }
10225 : :
10226 : : /*
10227 : : * getTSDictionaries:
10228 : : * get information about all text search dictionaries in the system catalogs
10229 : : */
10230 : : void
10231 : 215 : getTSDictionaries(Archive *fout)
10232 : : {
10233 : : PGresult *res;
10234 : : int ntups;
10235 : : int i;
10236 : : PQExpBuffer query;
10237 : : TSDictInfo *dictinfo;
10238 : : int i_tableoid;
10239 : : int i_oid;
10240 : : int i_dictname;
10241 : : int i_dictnamespace;
10242 : : int i_dictowner;
10243 : : int i_dicttemplate;
10244 : : int i_dictinitoption;
10245 : :
10246 : 215 : query = createPQExpBuffer();
10247 : :
10248 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, dictname, "
10249 : : "dictnamespace, dictowner, "
10250 : : "dicttemplate, dictinitoption "
10251 : : "FROM pg_ts_dict");
10252 : :
10253 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10254 : :
10255 : 215 : ntups = PQntuples(res);
10256 : :
10257 : 215 : dictinfo = pg_malloc_array(TSDictInfo, ntups);
10258 : :
10259 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10260 : 215 : i_oid = PQfnumber(res, "oid");
10261 : 215 : i_dictname = PQfnumber(res, "dictname");
10262 : 215 : i_dictnamespace = PQfnumber(res, "dictnamespace");
10263 : 215 : i_dictowner = PQfnumber(res, "dictowner");
10264 : 215 : i_dictinitoption = PQfnumber(res, "dictinitoption");
10265 : 215 : i_dicttemplate = PQfnumber(res, "dicttemplate");
10266 : :
10267 [ + + ]: 7206 : for (i = 0; i < ntups; i++)
10268 : : {
10269 : 6991 : dictinfo[i].dobj.objType = DO_TSDICT;
10270 : 6991 : dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10271 : 6991 : dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10272 : 6991 : AssignDumpId(&dictinfo[i].dobj);
10273 : 6991 : dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
10274 : 13982 : dictinfo[i].dobj.namespace =
10275 : 6991 : findNamespace(atooid(PQgetvalue(res, i, i_dictnamespace)));
10276 : 6991 : dictinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_dictowner));
10277 : 6991 : dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
10278 [ + + ]: 6991 : if (PQgetisnull(res, i, i_dictinitoption))
10279 : 263 : dictinfo[i].dictinitoption = NULL;
10280 : : else
10281 : 6728 : dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption));
10282 : :
10283 : : /* Decide whether we want to dump it */
10284 : 6991 : selectDumpableObject(&(dictinfo[i].dobj), fout);
10285 : : }
10286 : :
10287 : 215 : PQclear(res);
10288 : :
10289 : 215 : destroyPQExpBuffer(query);
10290 : 215 : }
10291 : :
10292 : : /*
10293 : : * getTSTemplates:
10294 : : * get information about all text search templates in the system catalogs
10295 : : */
10296 : : void
10297 : 215 : getTSTemplates(Archive *fout)
10298 : : {
10299 : : PGresult *res;
10300 : : int ntups;
10301 : : int i;
10302 : : PQExpBuffer query;
10303 : : TSTemplateInfo *tmplinfo;
10304 : : int i_tableoid;
10305 : : int i_oid;
10306 : : int i_tmplname;
10307 : : int i_tmplnamespace;
10308 : : int i_tmplinit;
10309 : : int i_tmpllexize;
10310 : :
10311 : 215 : query = createPQExpBuffer();
10312 : :
10313 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, "
10314 : : "tmplnamespace, tmplinit::oid, tmpllexize::oid "
10315 : : "FROM pg_ts_template");
10316 : :
10317 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10318 : :
10319 : 215 : ntups = PQntuples(res);
10320 : :
10321 : 215 : tmplinfo = pg_malloc_array(TSTemplateInfo, ntups);
10322 : :
10323 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10324 : 215 : i_oid = PQfnumber(res, "oid");
10325 : 215 : i_tmplname = PQfnumber(res, "tmplname");
10326 : 215 : i_tmplnamespace = PQfnumber(res, "tmplnamespace");
10327 : 215 : i_tmplinit = PQfnumber(res, "tmplinit");
10328 : 215 : i_tmpllexize = PQfnumber(res, "tmpllexize");
10329 : :
10330 [ + + ]: 1338 : for (i = 0; i < ntups; i++)
10331 : : {
10332 : 1123 : tmplinfo[i].dobj.objType = DO_TSTEMPLATE;
10333 : 1123 : tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10334 : 1123 : tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10335 : 1123 : AssignDumpId(&tmplinfo[i].dobj);
10336 : 1123 : tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
10337 : 2246 : tmplinfo[i].dobj.namespace =
10338 : 1123 : findNamespace(atooid(PQgetvalue(res, i, i_tmplnamespace)));
10339 : 1123 : tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
10340 : 1123 : tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
10341 : :
10342 : : /* Decide whether we want to dump it */
10343 : 1123 : selectDumpableObject(&(tmplinfo[i].dobj), fout);
10344 : : }
10345 : :
10346 : 215 : PQclear(res);
10347 : :
10348 : 215 : destroyPQExpBuffer(query);
10349 : 215 : }
10350 : :
10351 : : /*
10352 : : * getTSConfigurations:
10353 : : * get information about all text search configurations
10354 : : */
10355 : : void
10356 : 215 : getTSConfigurations(Archive *fout)
10357 : : {
10358 : : PGresult *res;
10359 : : int ntups;
10360 : : int i;
10361 : : PQExpBuffer query;
10362 : : TSConfigInfo *cfginfo;
10363 : : int i_tableoid;
10364 : : int i_oid;
10365 : : int i_cfgname;
10366 : : int i_cfgnamespace;
10367 : : int i_cfgowner;
10368 : : int i_cfgparser;
10369 : :
10370 : 215 : query = createPQExpBuffer();
10371 : :
10372 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, cfgname, "
10373 : : "cfgnamespace, cfgowner, cfgparser "
10374 : : "FROM pg_ts_config");
10375 : :
10376 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10377 : :
10378 : 215 : ntups = PQntuples(res);
10379 : :
10380 : 215 : cfginfo = pg_malloc_array(TSConfigInfo, ntups);
10381 : :
10382 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10383 : 215 : i_oid = PQfnumber(res, "oid");
10384 : 215 : i_cfgname = PQfnumber(res, "cfgname");
10385 : 215 : i_cfgnamespace = PQfnumber(res, "cfgnamespace");
10386 : 215 : i_cfgowner = PQfnumber(res, "cfgowner");
10387 : 215 : i_cfgparser = PQfnumber(res, "cfgparser");
10388 : :
10389 [ + + ]: 7171 : for (i = 0; i < ntups; i++)
10390 : : {
10391 : 6956 : cfginfo[i].dobj.objType = DO_TSCONFIG;
10392 : 6956 : cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10393 : 6956 : cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10394 : 6956 : AssignDumpId(&cfginfo[i].dobj);
10395 : 6956 : cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
10396 : 13912 : cfginfo[i].dobj.namespace =
10397 : 6956 : findNamespace(atooid(PQgetvalue(res, i, i_cfgnamespace)));
10398 : 6956 : cfginfo[i].rolname = getRoleName(PQgetvalue(res, i, i_cfgowner));
10399 : 6956 : cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
10400 : :
10401 : : /* Decide whether we want to dump it */
10402 : 6956 : selectDumpableObject(&(cfginfo[i].dobj), fout);
10403 : : }
10404 : :
10405 : 215 : PQclear(res);
10406 : :
10407 : 215 : destroyPQExpBuffer(query);
10408 : 215 : }
10409 : :
10410 : : /*
10411 : : * getForeignDataWrappers:
10412 : : * get information about all foreign-data wrappers in the system catalogs
10413 : : */
10414 : : void
10415 : 215 : getForeignDataWrappers(Archive *fout)
10416 : : {
10417 : : PGresult *res;
10418 : : int ntups;
10419 : : int i;
10420 : : PQExpBuffer query;
10421 : : FdwInfo *fdwinfo;
10422 : : int i_tableoid;
10423 : : int i_oid;
10424 : : int i_fdwname;
10425 : : int i_fdwowner;
10426 : : int i_fdwhandler;
10427 : : int i_fdwvalidator;
10428 : : int i_fdwconnection;
10429 : : int i_fdwacl;
10430 : : int i_acldefault;
10431 : : int i_fdwoptions;
10432 : :
10433 : 215 : query = createPQExpBuffer();
10434 : :
10435 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, fdwname, "
10436 : : "fdwowner, "
10437 : : "fdwhandler::pg_catalog.regproc, "
10438 : : "fdwvalidator::pg_catalog.regproc, ");
10439 : :
10440 [ + - ]: 215 : if (fout->remoteVersion >= 190000)
10441 : 215 : appendPQExpBufferStr(query, "fdwconnection::pg_catalog.regproc, ");
10442 : : else
10443 : 0 : appendPQExpBufferStr(query, "'-' AS fdwconnection, ");
10444 : :
10445 : 215 : appendPQExpBufferStr(query,
10446 : : "fdwacl, "
10447 : : "acldefault('F', fdwowner) AS acldefault, "
10448 : : "array_to_string(ARRAY("
10449 : : "SELECT quote_ident(option_name) || ' ' || "
10450 : : "quote_literal(option_value) "
10451 : : "FROM pg_options_to_table(fdwoptions) "
10452 : : "ORDER BY option_name"
10453 : : "), E',\n ') AS fdwoptions "
10454 : : "FROM pg_foreign_data_wrapper");
10455 : :
10456 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10457 : :
10458 : 215 : ntups = PQntuples(res);
10459 : :
10460 : 215 : fdwinfo = pg_malloc_array(FdwInfo, ntups);
10461 : :
10462 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10463 : 215 : i_oid = PQfnumber(res, "oid");
10464 : 215 : i_fdwname = PQfnumber(res, "fdwname");
10465 : 215 : i_fdwowner = PQfnumber(res, "fdwowner");
10466 : 215 : i_fdwhandler = PQfnumber(res, "fdwhandler");
10467 : 215 : i_fdwvalidator = PQfnumber(res, "fdwvalidator");
10468 : 215 : i_fdwconnection = PQfnumber(res, "fdwconnection");
10469 : 215 : i_fdwacl = PQfnumber(res, "fdwacl");
10470 : 215 : i_acldefault = PQfnumber(res, "acldefault");
10471 : 215 : i_fdwoptions = PQfnumber(res, "fdwoptions");
10472 : :
10473 [ + + ]: 290 : for (i = 0; i < ntups; i++)
10474 : : {
10475 : 75 : fdwinfo[i].dobj.objType = DO_FDW;
10476 : 75 : fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10477 : 75 : fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10478 : 75 : AssignDumpId(&fdwinfo[i].dobj);
10479 : 75 : fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname));
10480 : 75 : fdwinfo[i].dobj.namespace = NULL;
10481 : 75 : fdwinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_fdwacl));
10482 : 75 : fdwinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10483 : 75 : fdwinfo[i].dacl.privtype = 0;
10484 : 75 : fdwinfo[i].dacl.initprivs = NULL;
10485 : 75 : fdwinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_fdwowner));
10486 : 75 : fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler));
10487 : 75 : fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator));
10488 : 75 : fdwinfo[i].fdwconnection = pg_strdup(PQgetvalue(res, i, i_fdwconnection));
10489 : 75 : fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions));
10490 : :
10491 : : /* Decide whether we want to dump it */
10492 : 75 : selectDumpableObject(&(fdwinfo[i].dobj), fout);
10493 : :
10494 : : /* Mark whether FDW has an ACL */
10495 [ + + ]: 75 : if (!PQgetisnull(res, i, i_fdwacl))
10496 : 48 : fdwinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10497 : : }
10498 : :
10499 : 215 : PQclear(res);
10500 : :
10501 : 215 : destroyPQExpBuffer(query);
10502 : 215 : }
10503 : :
10504 : : /*
10505 : : * getForeignServers:
10506 : : * get information about all foreign servers in the system catalogs
10507 : : */
10508 : : void
10509 : 215 : getForeignServers(Archive *fout)
10510 : : {
10511 : : PGresult *res;
10512 : : int ntups;
10513 : : int i;
10514 : : PQExpBuffer query;
10515 : : ForeignServerInfo *srvinfo;
10516 : : int i_tableoid;
10517 : : int i_oid;
10518 : : int i_srvname;
10519 : : int i_srvowner;
10520 : : int i_srvfdw;
10521 : : int i_srvtype;
10522 : : int i_srvversion;
10523 : : int i_srvacl;
10524 : : int i_acldefault;
10525 : : int i_srvoptions;
10526 : :
10527 : 215 : query = createPQExpBuffer();
10528 : :
10529 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, srvname, "
10530 : : "srvowner, "
10531 : : "srvfdw, srvtype, srvversion, srvacl, "
10532 : : "acldefault('S', srvowner) AS acldefault, "
10533 : : "array_to_string(ARRAY("
10534 : : "SELECT quote_ident(option_name) || ' ' || "
10535 : : "quote_literal(option_value) "
10536 : : "FROM pg_options_to_table(srvoptions) "
10537 : : "ORDER BY option_name"
10538 : : "), E',\n ') AS srvoptions "
10539 : : "FROM pg_foreign_server");
10540 : :
10541 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10542 : :
10543 : 215 : ntups = PQntuples(res);
10544 : :
10545 : 215 : srvinfo = pg_malloc_array(ForeignServerInfo, ntups);
10546 : :
10547 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10548 : 215 : i_oid = PQfnumber(res, "oid");
10549 : 215 : i_srvname = PQfnumber(res, "srvname");
10550 : 215 : i_srvowner = PQfnumber(res, "srvowner");
10551 : 215 : i_srvfdw = PQfnumber(res, "srvfdw");
10552 : 215 : i_srvtype = PQfnumber(res, "srvtype");
10553 : 215 : i_srvversion = PQfnumber(res, "srvversion");
10554 : 215 : i_srvacl = PQfnumber(res, "srvacl");
10555 : 215 : i_acldefault = PQfnumber(res, "acldefault");
10556 : 215 : i_srvoptions = PQfnumber(res, "srvoptions");
10557 : :
10558 [ + + ]: 294 : for (i = 0; i < ntups; i++)
10559 : : {
10560 : 79 : srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
10561 : 79 : srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10562 : 79 : srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10563 : 79 : AssignDumpId(&srvinfo[i].dobj);
10564 : 79 : srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname));
10565 : 79 : srvinfo[i].dobj.namespace = NULL;
10566 : 79 : srvinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_srvacl));
10567 : 79 : srvinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10568 : 79 : srvinfo[i].dacl.privtype = 0;
10569 : 79 : srvinfo[i].dacl.initprivs = NULL;
10570 : 79 : srvinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_srvowner));
10571 : 79 : srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw));
10572 : 79 : srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype));
10573 : 79 : srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion));
10574 : 79 : srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions));
10575 : :
10576 : : /* Decide whether we want to dump it */
10577 : 79 : selectDumpableObject(&(srvinfo[i].dobj), fout);
10578 : :
10579 : : /* Servers have user mappings */
10580 : 79 : srvinfo[i].dobj.components |= DUMP_COMPONENT_USERMAP;
10581 : :
10582 : : /* Mark whether server has an ACL */
10583 [ + + ]: 79 : if (!PQgetisnull(res, i, i_srvacl))
10584 : 48 : srvinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10585 : : }
10586 : :
10587 : 215 : PQclear(res);
10588 : :
10589 : 215 : destroyPQExpBuffer(query);
10590 : 215 : }
10591 : :
10592 : : /*
10593 : : * getDefaultACLs:
10594 : : * get information about all default ACL information in the system catalogs
10595 : : */
10596 : : void
10597 : 215 : getDefaultACLs(Archive *fout)
10598 : : {
10599 : 215 : DumpOptions *dopt = fout->dopt;
10600 : : DefaultACLInfo *daclinfo;
10601 : : PQExpBuffer query;
10602 : : PGresult *res;
10603 : : int i_oid;
10604 : : int i_tableoid;
10605 : : int i_defaclrole;
10606 : : int i_defaclnamespace;
10607 : : int i_defaclobjtype;
10608 : : int i_defaclacl;
10609 : : int i_acldefault;
10610 : : int i,
10611 : : ntups;
10612 : :
10613 : 215 : query = createPQExpBuffer();
10614 : :
10615 : : /*
10616 : : * Global entries (with defaclnamespace=0) replace the hard-wired default
10617 : : * ACL for their object type. We should dump them as deltas from the
10618 : : * default ACL, since that will be used as a starting point for
10619 : : * interpreting the ALTER DEFAULT PRIVILEGES commands. On the other hand,
10620 : : * non-global entries can only add privileges not revoke them. We must
10621 : : * dump those as-is (i.e., as deltas from an empty ACL).
10622 : : *
10623 : : * We can use defaclobjtype as the object type for acldefault(), except
10624 : : * for the case of 'S' (DEFACLOBJ_SEQUENCE) which must be converted to
10625 : : * 's'.
10626 : : */
10627 : 215 : appendPQExpBufferStr(query,
10628 : : "SELECT oid, tableoid, "
10629 : : "defaclrole, "
10630 : : "defaclnamespace, "
10631 : : "defaclobjtype, "
10632 : : "defaclacl, "
10633 : : "CASE WHEN defaclnamespace = 0 THEN "
10634 : : "acldefault(CASE WHEN defaclobjtype = 'S' "
10635 : : "THEN 's'::\"char\" ELSE defaclobjtype END, "
10636 : : "defaclrole) ELSE '{}' END AS acldefault "
10637 : : "FROM pg_default_acl");
10638 : :
10639 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10640 : :
10641 : 215 : ntups = PQntuples(res);
10642 : :
10643 : 215 : daclinfo = pg_malloc_array(DefaultACLInfo, ntups);
10644 : :
10645 : 215 : i_oid = PQfnumber(res, "oid");
10646 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10647 : 215 : i_defaclrole = PQfnumber(res, "defaclrole");
10648 : 215 : i_defaclnamespace = PQfnumber(res, "defaclnamespace");
10649 : 215 : i_defaclobjtype = PQfnumber(res, "defaclobjtype");
10650 : 215 : i_defaclacl = PQfnumber(res, "defaclacl");
10651 : 215 : i_acldefault = PQfnumber(res, "acldefault");
10652 : :
10653 [ + + ]: 421 : for (i = 0; i < ntups; i++)
10654 : : {
10655 : 206 : Oid nspid = atooid(PQgetvalue(res, i, i_defaclnamespace));
10656 : :
10657 : 206 : daclinfo[i].dobj.objType = DO_DEFAULT_ACL;
10658 : 206 : daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10659 : 206 : daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10660 : 206 : AssignDumpId(&daclinfo[i].dobj);
10661 : : /* cheesy ... is it worth coming up with a better object name? */
10662 : 206 : daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
10663 : :
10664 [ + + ]: 206 : if (nspid != InvalidOid)
10665 : 96 : daclinfo[i].dobj.namespace = findNamespace(nspid);
10666 : : else
10667 : 110 : daclinfo[i].dobj.namespace = NULL;
10668 : :
10669 : 206 : daclinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_defaclacl));
10670 : 206 : daclinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10671 : 206 : daclinfo[i].dacl.privtype = 0;
10672 : 206 : daclinfo[i].dacl.initprivs = NULL;
10673 : 206 : daclinfo[i].defaclrole = getRoleName(PQgetvalue(res, i, i_defaclrole));
10674 : 206 : daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype));
10675 : :
10676 : : /* Default ACLs are ACLs, of course */
10677 : 206 : daclinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10678 : :
10679 : : /* Decide whether we want to dump it */
10680 : 206 : selectDumpableDefaultACL(&(daclinfo[i]), dopt);
10681 : : }
10682 : :
10683 : 215 : PQclear(res);
10684 : :
10685 : 215 : destroyPQExpBuffer(query);
10686 : 215 : }
10687 : :
10688 : : /*
10689 : : * getRoleName -- look up the name of a role, given its OID
10690 : : *
10691 : : * In current usage, we don't expect failures, so error out for a bad OID.
10692 : : */
10693 : : static const char *
10694 : 692805 : getRoleName(const char *roleoid_str)
10695 : : {
10696 : 692805 : Oid roleoid = atooid(roleoid_str);
10697 : :
10698 : : /*
10699 : : * Do binary search to find the appropriate item.
10700 : : */
10701 [ + - ]: 692805 : if (nrolenames > 0)
10702 : : {
10703 : 692805 : RoleNameItem *low = &rolenames[0];
10704 : 692805 : RoleNameItem *high = &rolenames[nrolenames - 1];
10705 : :
10706 [ + - ]: 2771412 : while (low <= high)
10707 : : {
10708 : 2771412 : RoleNameItem *middle = low + (high - low) / 2;
10709 : :
10710 [ + + ]: 2771412 : if (roleoid < middle->roleoid)
10711 : 2077146 : high = middle - 1;
10712 [ + + ]: 694266 : else if (roleoid > middle->roleoid)
10713 : 1461 : low = middle + 1;
10714 : : else
10715 : 692805 : return middle->rolename; /* found a match */
10716 : : }
10717 : : }
10718 : :
10719 : 0 : pg_fatal("role with OID %u does not exist", roleoid);
10720 : : return NULL; /* keep compiler quiet */
10721 : : }
10722 : :
10723 : : /*
10724 : : * collectRoleNames --
10725 : : *
10726 : : * Construct a table of all known roles.
10727 : : * The table is sorted by OID for speed in lookup.
10728 : : */
10729 : : static void
10730 : 216 : collectRoleNames(Archive *fout)
10731 : : {
10732 : : PGresult *res;
10733 : : const char *query;
10734 : : int i;
10735 : :
10736 : 216 : query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
10737 : :
10738 : 216 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
10739 : :
10740 : 216 : nrolenames = PQntuples(res);
10741 : :
10742 : 216 : rolenames = pg_malloc_array(RoleNameItem, nrolenames);
10743 : :
10744 [ + + ]: 4160 : for (i = 0; i < nrolenames; i++)
10745 : : {
10746 : 3944 : rolenames[i].roleoid = atooid(PQgetvalue(res, i, 0));
10747 : 3944 : rolenames[i].rolename = pg_strdup(PQgetvalue(res, i, 1));
10748 : : }
10749 : :
10750 : 216 : PQclear(res);
10751 : 216 : }
10752 : :
10753 : : /*
10754 : : * getAdditionalACLs
10755 : : *
10756 : : * We have now created all the DumpableObjects, and collected the ACL data
10757 : : * that appears in the directly-associated catalog entries. However, there's
10758 : : * more ACL-related info to collect. If any of a table's columns have ACLs,
10759 : : * we must set the TableInfo's DUMP_COMPONENT_ACL components flag, as well as
10760 : : * its hascolumnACLs flag (we won't store the ACLs themselves here, though).
10761 : : * Also, in versions having the pg_init_privs catalog, read that and load the
10762 : : * information into the relevant DumpableObjects.
10763 : : */
10764 : : static void
10765 : 213 : getAdditionalACLs(Archive *fout)
10766 : : {
10767 : 213 : PQExpBuffer query = createPQExpBuffer();
10768 : : PGresult *res;
10769 : : int ntups,
10770 : : i;
10771 : :
10772 : : /* Check for per-column ACLs */
10773 : 213 : appendPQExpBufferStr(query,
10774 : : "SELECT DISTINCT attrelid FROM pg_attribute "
10775 : : "WHERE attacl IS NOT NULL");
10776 : :
10777 : 213 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10778 : :
10779 : 213 : ntups = PQntuples(res);
10780 [ + + ]: 605 : for (i = 0; i < ntups; i++)
10781 : : {
10782 : 392 : Oid relid = atooid(PQgetvalue(res, i, 0));
10783 : : TableInfo *tblinfo;
10784 : :
10785 : 392 : tblinfo = findTableByOid(relid);
10786 : : /* OK to ignore tables we haven't got a DumpableObject for */
10787 [ + - ]: 392 : if (tblinfo)
10788 : : {
10789 : 392 : tblinfo->dobj.components |= DUMP_COMPONENT_ACL;
10790 : 392 : tblinfo->hascolumnACLs = true;
10791 : : }
10792 : : }
10793 : 213 : PQclear(res);
10794 : :
10795 : : /* Fetch initial-privileges data */
10796 : 213 : printfPQExpBuffer(query,
10797 : : "SELECT objoid, classoid, objsubid, privtype, initprivs "
10798 : : "FROM pg_init_privs");
10799 : :
10800 : 213 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10801 : :
10802 : 213 : ntups = PQntuples(res);
10803 [ + + ]: 53558 : for (i = 0; i < ntups; i++)
10804 : : {
10805 : 53345 : Oid objoid = atooid(PQgetvalue(res, i, 0));
10806 : 53345 : Oid classoid = atooid(PQgetvalue(res, i, 1));
10807 : 53345 : int objsubid = atoi(PQgetvalue(res, i, 2));
10808 : 53345 : char privtype = *(PQgetvalue(res, i, 3));
10809 : 53345 : char *initprivs = PQgetvalue(res, i, 4);
10810 : : CatalogId objId;
10811 : : DumpableObject *dobj;
10812 : :
10813 : 53345 : objId.tableoid = classoid;
10814 : 53345 : objId.oid = objoid;
10815 : 53345 : dobj = findObjectByCatalogId(objId);
10816 : : /* OK to ignore entries we haven't got a DumpableObject for */
10817 [ + + ]: 53345 : if (dobj)
10818 : : {
10819 : : /* Cope with sub-object initprivs */
10820 [ + + ]: 38482 : if (objsubid != 0)
10821 : : {
10822 [ + - ]: 5136 : if (dobj->objType == DO_TABLE)
10823 : : {
10824 : : /* For a column initprivs, set the table's ACL flags */
10825 : 5136 : dobj->components |= DUMP_COMPONENT_ACL;
10826 : 5136 : ((TableInfo *) dobj)->hascolumnACLs = true;
10827 : : }
10828 : : else
10829 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10830 : : classoid, objoid, objsubid);
10831 : 5345 : continue;
10832 : : }
10833 : :
10834 : : /*
10835 : : * We ignore any pg_init_privs.initprivs entry for the public
10836 : : * schema, as explained in getNamespaces().
10837 : : */
10838 [ + + ]: 33346 : if (dobj->objType == DO_NAMESPACE &&
10839 [ + + ]: 635 : strcmp(dobj->name, "public") == 0)
10840 : 209 : continue;
10841 : :
10842 : : /* Else it had better be of a type we think has ACLs */
10843 [ + + ]: 33137 : if (dobj->objType == DO_NAMESPACE ||
10844 [ + + ]: 32711 : dobj->objType == DO_TYPE ||
10845 [ + + ]: 32687 : dobj->objType == DO_FUNC ||
10846 [ + + ]: 32592 : dobj->objType == DO_AGG ||
10847 [ - + ]: 32568 : dobj->objType == DO_TABLE ||
10848 [ # # ]: 0 : dobj->objType == DO_PROCLANG ||
10849 [ # # ]: 0 : dobj->objType == DO_FDW ||
10850 [ # # ]: 0 : dobj->objType == DO_FOREIGN_SERVER)
10851 : 33137 : {
10852 : 33137 : DumpableObjectWithAcl *daobj = (DumpableObjectWithAcl *) dobj;
10853 : :
10854 : 33137 : daobj->dacl.privtype = privtype;
10855 : 33137 : daobj->dacl.initprivs = pstrdup(initprivs);
10856 : : }
10857 : : else
10858 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10859 : : classoid, objoid, objsubid);
10860 : : }
10861 : : }
10862 : 213 : PQclear(res);
10863 : :
10864 : 213 : destroyPQExpBuffer(query);
10865 : 213 : }
10866 : :
10867 : : /*
10868 : : * dumpCommentExtended --
10869 : : *
10870 : : * This routine is used to dump any comments associated with the
10871 : : * object handed to this routine. The routine takes the object type
10872 : : * and object name (ready to print, except for schema decoration), plus
10873 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
10874 : : * plus catalog ID and subid which are the lookup key for pg_description,
10875 : : * plus the dump ID for the object (for setting a dependency).
10876 : : * If a matching pg_description entry is found, it is dumped.
10877 : : *
10878 : : * Note: in some cases, such as comments for triggers and rules, the "type"
10879 : : * string really looks like, e.g., "TRIGGER name ON". This is a bit of a hack
10880 : : * but it doesn't seem worth complicating the API for all callers to make
10881 : : * it cleaner.
10882 : : *
10883 : : * Note: although this routine takes a dumpId for dependency purposes,
10884 : : * that purpose is just to mark the dependency in the emitted dump file
10885 : : * for possible future use by pg_restore. We do NOT use it for determining
10886 : : * ordering of the comment in the dump file, because this routine is called
10887 : : * after dependency sorting occurs. This routine should be called just after
10888 : : * calling ArchiveEntry() for the specified object.
10889 : : */
10890 : : static void
10891 : 6667 : dumpCommentExtended(Archive *fout, const char *type,
10892 : : const char *name, const char *namespace,
10893 : : const char *owner, CatalogId catalogId,
10894 : : int subid, DumpId dumpId,
10895 : : const char *initdb_comment)
10896 : : {
10897 : 6667 : DumpOptions *dopt = fout->dopt;
10898 : : CommentItem *comments;
10899 : : int ncomments;
10900 : :
10901 : : /* do nothing, if --no-comments is supplied */
10902 [ - + ]: 6667 : if (dopt->no_comments)
10903 : 0 : return;
10904 : :
10905 : : /* Comments are schema not data ... except LO comments are data */
10906 [ + + ]: 6667 : if (strcmp(type, "LARGE OBJECT") != 0)
10907 : : {
10908 [ - + ]: 6607 : if (!dopt->dumpSchema)
10909 : 0 : return;
10910 : : }
10911 : : else
10912 : : {
10913 : : /* We do dump LO comments in binary-upgrade mode */
10914 [ + + - + ]: 60 : if (!dopt->dumpData && !dopt->binary_upgrade)
10915 : 0 : return;
10916 : : }
10917 : :
10918 : : /* Search for comments associated with catalogId, using table */
10919 : 6667 : ncomments = findComments(catalogId.tableoid, catalogId.oid,
10920 : : &comments);
10921 : :
10922 : : /* Is there one matching the subid? */
10923 [ + + ]: 6667 : while (ncomments > 0)
10924 : : {
10925 [ + - ]: 6620 : if (comments->objsubid == subid)
10926 : 6620 : break;
10927 : 0 : comments++;
10928 : 0 : ncomments--;
10929 : : }
10930 : :
10931 [ + + ]: 6667 : if (initdb_comment != NULL)
10932 : : {
10933 : : static CommentItem empty_comment = {.descr = ""};
10934 : :
10935 : : /*
10936 : : * initdb creates this object with a comment. Skip dumping the
10937 : : * initdb-provided comment, which would complicate matters for
10938 : : * non-superuser use of pg_dump. When the DBA has removed initdb's
10939 : : * comment, replicate that.
10940 : : */
10941 [ + + ]: 137 : if (ncomments == 0)
10942 : : {
10943 : 4 : comments = &empty_comment;
10944 : 4 : ncomments = 1;
10945 : : }
10946 [ + - ]: 133 : else if (strcmp(comments->descr, initdb_comment) == 0)
10947 : 133 : ncomments = 0;
10948 : : }
10949 : :
10950 : : /* If a comment exists, build COMMENT ON statement */
10951 [ + + ]: 6667 : if (ncomments > 0)
10952 : : {
10953 : 6491 : PQExpBuffer query = createPQExpBuffer();
10954 : 6491 : PQExpBuffer tag = createPQExpBuffer();
10955 : :
10956 : 6491 : appendPQExpBuffer(query, "COMMENT ON %s ", type);
10957 [ + + + - ]: 6491 : if (namespace && *namespace)
10958 : 6300 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
10959 : 6491 : appendPQExpBuffer(query, "%s IS ", name);
10960 : 6491 : appendStringLiteralAH(query, comments->descr, fout);
10961 : 6491 : appendPQExpBufferStr(query, ";\n");
10962 : :
10963 : 6491 : appendPQExpBuffer(tag, "%s %s", type, name);
10964 : :
10965 : : /*
10966 : : * We mark comments as SECTION_NONE because they really belong in the
10967 : : * same section as their parent, whether that is pre-data or
10968 : : * post-data.
10969 : : */
10970 : 6491 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
10971 : 6491 : ARCHIVE_OPTS(.tag = tag->data,
10972 : : .namespace = namespace,
10973 : : .owner = owner,
10974 : : .description = "COMMENT",
10975 : : .section = SECTION_NONE,
10976 : : .createStmt = query->data,
10977 : : .deps = &dumpId,
10978 : : .nDeps = 1));
10979 : :
10980 : 6491 : destroyPQExpBuffer(query);
10981 : 6491 : destroyPQExpBuffer(tag);
10982 : : }
10983 : : }
10984 : :
10985 : : /*
10986 : : * dumpComment --
10987 : : *
10988 : : * Typical simplification of the above function.
10989 : : */
10990 : : static inline void
10991 : 6483 : dumpComment(Archive *fout, const char *type,
10992 : : const char *name, const char *namespace,
10993 : : const char *owner, CatalogId catalogId,
10994 : : int subid, DumpId dumpId)
10995 : : {
10996 : 6483 : dumpCommentExtended(fout, type, name, namespace, owner,
10997 : : catalogId, subid, dumpId, NULL);
10998 : 6483 : }
10999 : :
11000 : : /*
11001 : : * appendNamedArgument --
11002 : : *
11003 : : * Convenience routine for constructing parameters of the form:
11004 : : * 'paraname', 'value'::type
11005 : : */
11006 : : static void
11007 : 6565 : appendNamedArgument(PQExpBuffer out, Archive *fout, const char *argname,
11008 : : const char *argtype, const char *argval)
11009 : : {
11010 : 6565 : appendPQExpBufferStr(out, ",\n\t");
11011 : :
11012 : 6565 : appendStringLiteralAH(out, argname, fout);
11013 : 6565 : appendPQExpBufferStr(out, ", ");
11014 : :
11015 : 6565 : appendStringLiteralAH(out, argval, fout);
11016 : 6565 : appendPQExpBuffer(out, "::%s", argtype);
11017 : 6565 : }
11018 : :
11019 : : /*
11020 : : * fetchAttributeStats --
11021 : : *
11022 : : * Fetch next batch of attribute statistics for dumpRelationStats_dumper().
11023 : : */
11024 : : static PGresult *
11025 : 1136 : fetchAttributeStats(Archive *fout)
11026 : : {
11027 : 1136 : ArchiveHandle *AH = (ArchiveHandle *) fout;
11028 : 1136 : PQExpBuffer relids = createPQExpBuffer();
11029 : 1136 : PQExpBuffer nspnames = createPQExpBuffer();
11030 : 1136 : PQExpBuffer relnames = createPQExpBuffer();
11031 : 1136 : int count = 0;
11032 : 1136 : PGresult *res = NULL;
11033 : : static TocEntry *te;
11034 : : static bool restarted;
11035 : 1136 : int max_rels = MAX_ATTR_STATS_RELS;
11036 : :
11037 : : /* If we're just starting, set our TOC pointer. */
11038 [ + + ]: 1136 : if (!te)
11039 : 66 : te = AH->toc->next;
11040 : :
11041 : : /*
11042 : : * We can't easily avoid a second TOC scan for the tar format because it
11043 : : * writes restore.sql separately, which means we must execute the queries
11044 : : * twice. This feels risky, but there is no known reason it should
11045 : : * generate different output than the first pass. Even if it does, the
11046 : : * worst-case scenario is that restore.sql might have different statistics
11047 : : * data than the archive.
11048 : : */
11049 [ + + + + : 1136 : if (!restarted && te == AH->toc && AH->format == archTar)
+ + ]
11050 : : {
11051 : 1 : te = AH->toc->next;
11052 : 1 : restarted = true;
11053 : : }
11054 : :
11055 : 1136 : appendPQExpBufferChar(relids, '{');
11056 : 1136 : appendPQExpBufferChar(nspnames, '{');
11057 : 1136 : appendPQExpBufferChar(relnames, '{');
11058 : :
11059 : : /*
11060 : : * Scan the TOC for the next set of relevant stats entries. We assume
11061 : : * that statistics are dumped in the order they are listed in the TOC.
11062 : : * This is perhaps not the sturdiest assumption, so we verify it matches
11063 : : * reality in dumpRelationStats_dumper().
11064 : : */
11065 [ + + + + ]: 17565 : for (; te != AH->toc && count < max_rels; te = te->next)
11066 : : {
11067 [ + + ]: 16429 : if ((te->reqs & REQ_STATS) == 0 ||
11068 [ + + ]: 3758 : strcmp(te->desc, "STATISTICS DATA") != 0)
11069 : 12709 : continue;
11070 : :
11071 [ + - ]: 3720 : if (fout->remoteVersion >= 190000)
11072 : : {
11073 : 3720 : const RelStatsInfo *rsinfo = (const RelStatsInfo *) te->defnDumperArg;
11074 : : char relid[32];
11075 : :
11076 : 3720 : sprintf(relid, "%u", rsinfo->relid);
11077 : 3720 : appendPGArray(relids, relid);
11078 : : }
11079 : : else
11080 : : {
11081 : 0 : appendPGArray(nspnames, te->namespace);
11082 : 0 : appendPGArray(relnames, te->tag);
11083 : : }
11084 : :
11085 : 3720 : count++;
11086 : : }
11087 : :
11088 : 1136 : appendPQExpBufferChar(relids, '}');
11089 : 1136 : appendPQExpBufferChar(nspnames, '}');
11090 : 1136 : appendPQExpBufferChar(relnames, '}');
11091 : :
11092 : : /* Execute the query for the next batch of relations. */
11093 [ + + ]: 1136 : if (count > 0)
11094 : : {
11095 : 112 : PQExpBuffer query = createPQExpBuffer();
11096 : :
11097 : 112 : appendPQExpBufferStr(query, "EXECUTE getAttributeStats(");
11098 : :
11099 [ + - ]: 112 : if (fout->remoteVersion >= 190000)
11100 : : {
11101 : 112 : appendStringLiteralAH(query, relids->data, fout);
11102 : 112 : appendPQExpBufferStr(query, "::pg_catalog.oid[])");
11103 : : }
11104 : : else
11105 : : {
11106 : 0 : appendStringLiteralAH(query, nspnames->data, fout);
11107 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[],");
11108 : 0 : appendStringLiteralAH(query, relnames->data, fout);
11109 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[])");
11110 : : }
11111 : :
11112 : 112 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11113 : 112 : destroyPQExpBuffer(query);
11114 : : }
11115 : :
11116 : 1136 : destroyPQExpBuffer(relids);
11117 : 1136 : destroyPQExpBuffer(nspnames);
11118 : 1136 : destroyPQExpBuffer(relnames);
11119 : 1136 : return res;
11120 : : }
11121 : :
11122 : : /*
11123 : : * dumpRelationStats_dumper --
11124 : : *
11125 : : * Generate command to import stats into the relation on the new database.
11126 : : * This routine is called by the Archiver when it wants the statistics to be
11127 : : * dumped.
11128 : : */
11129 : : static char *
11130 : 3720 : dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te)
11131 : : {
11132 : 3720 : const RelStatsInfo *rsinfo = userArg;
11133 : : static PGresult *res;
11134 : : static int rownum;
11135 : : PQExpBuffer query;
11136 : : PQExpBufferData out_data;
11137 : 3720 : PQExpBuffer out = &out_data;
11138 : : int i_schemaname;
11139 : : int i_tablename;
11140 : : int i_attname;
11141 : : int i_inherited;
11142 : : int i_null_frac;
11143 : : int i_avg_width;
11144 : : int i_n_distinct;
11145 : : int i_most_common_vals;
11146 : : int i_most_common_freqs;
11147 : : int i_histogram_bounds;
11148 : : int i_correlation;
11149 : : int i_most_common_elems;
11150 : : int i_most_common_elem_freqs;
11151 : : int i_elem_count_histogram;
11152 : : int i_range_length_histogram;
11153 : : int i_range_empty_frac;
11154 : : int i_range_bounds_histogram;
11155 : : static TocEntry *expected_te;
11156 : :
11157 : : /*
11158 : : * fetchAttributeStats() assumes that the statistics are dumped in the
11159 : : * order they are listed in the TOC. We verify that here for safety.
11160 : : */
11161 [ + + ]: 3720 : if (!expected_te)
11162 : 66 : expected_te = ((ArchiveHandle *) fout)->toc;
11163 : :
11164 : 3720 : expected_te = expected_te->next;
11165 [ + + ]: 14384 : while ((expected_te->reqs & REQ_STATS) == 0 ||
11166 [ + + ]: 3721 : strcmp(expected_te->desc, "STATISTICS DATA") != 0)
11167 : 10664 : expected_te = expected_te->next;
11168 : :
11169 [ - + ]: 3720 : if (te != expected_te)
11170 : 0 : pg_fatal("statistics dumped out of order (current: %d %s %s, expected: %d %s %s)",
11171 : : te->dumpId, te->desc, te->tag,
11172 : : expected_te->dumpId, expected_te->desc, expected_te->tag);
11173 : :
11174 : 3720 : query = createPQExpBuffer();
11175 [ + + ]: 3720 : if (!fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS])
11176 : : {
11177 [ + - ]: 66 : if (fout->remoteVersion >= 190000)
11178 : 66 : appendPQExpBufferStr(query,
11179 : : "PREPARE getAttributeStats(pg_catalog.oid[]) AS\n");
11180 : : else
11181 : 0 : appendPQExpBufferStr(query,
11182 : : "PREPARE getAttributeStats(pg_catalog.name[], pg_catalog.name[]) AS\n");
11183 : :
11184 : 66 : appendPQExpBufferStr(query,
11185 : : "SELECT s.schemaname, s.tablename, s.attname, s.inherited, "
11186 : : "s.null_frac, s.avg_width, s.n_distinct, "
11187 : : "s.most_common_vals, s.most_common_freqs, "
11188 : : "s.histogram_bounds, s.correlation, "
11189 : : "s.most_common_elems, s.most_common_elem_freqs, "
11190 : : "s.elem_count_histogram, ");
11191 : :
11192 [ + - ]: 66 : if (fout->remoteVersion >= 170000)
11193 : 66 : appendPQExpBufferStr(query,
11194 : : "s.range_length_histogram, "
11195 : : "s.range_empty_frac, "
11196 : : "s.range_bounds_histogram ");
11197 : : else
11198 : 0 : appendPQExpBufferStr(query,
11199 : : "NULL AS range_length_histogram,"
11200 : : "NULL AS range_empty_frac,"
11201 : : "NULL AS range_bounds_histogram ");
11202 : :
11203 : : /*
11204 : : * The results must be in the order of the relations supplied in the
11205 : : * parameters to ensure we remain in sync as we walk through the TOC.
11206 : : * The redundant filter clause seems sufficient to convince the
11207 : : * planner to use pg_class_relname_nsp_index, which avoids a full scan
11208 : : * of pg_stats. This may not work for all versions.
11209 : : */
11210 [ + - ]: 66 : if (fout->remoteVersion >= 190000)
11211 : 66 : appendPQExpBufferStr(query,
11212 : : "FROM pg_catalog.pg_stats s "
11213 : : "JOIN unnest($1) WITH ORDINALITY AS u (tableid, ord) "
11214 : : "ON s.tableid = u.tableid "
11215 : : "WHERE s.tableid = ANY($1) "
11216 : : "ORDER BY u.ord, s.attname, s.inherited");
11217 : : else
11218 : 0 : appendPQExpBufferStr(query,
11219 : : "FROM pg_catalog.pg_stats s "
11220 : : "JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) "
11221 : : "ON s.schemaname = u.schemaname "
11222 : : "AND s.tablename = u.tablename "
11223 : : "WHERE s.tablename = ANY($2) "
11224 : : "ORDER BY u.ord, s.attname, s.inherited");
11225 : :
11226 : 66 : ExecuteSqlStatement(fout, query->data);
11227 : :
11228 : 66 : fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS] = true;
11229 : 66 : resetPQExpBuffer(query);
11230 : : }
11231 : :
11232 : 3720 : initPQExpBuffer(out);
11233 : :
11234 : : /* restore relation stats */
11235 : 3720 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(\n");
11236 : 3720 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11237 : : fout->remoteVersion);
11238 : 3720 : appendPQExpBufferStr(out, "\t'schemaname', ");
11239 : 3720 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11240 : 3720 : appendPQExpBufferStr(out, ",\n");
11241 : 3720 : appendPQExpBufferStr(out, "\t'relname', ");
11242 : 3720 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11243 : 3720 : appendPQExpBufferStr(out, ",\n");
11244 : 3720 : appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages);
11245 : :
11246 : : /*
11247 : : * Before v14, a reltuples value of 0 was ambiguous: it could either mean
11248 : : * the relation is empty, or it could mean that it hadn't yet been
11249 : : * vacuumed or analyzed. (Newer versions use -1 for the latter case.)
11250 : : * This ambiguity allegedly can cause the planner to choose inefficient
11251 : : * plans after restoring to v18 or newer. To deal with this, let's just
11252 : : * set reltuples to -1 in that case.
11253 : : */
11254 [ - + - - ]: 3720 : if (fout->remoteVersion < 140000 && strcmp("0", rsinfo->reltuples) == 0)
11255 : 0 : appendPQExpBufferStr(out, "\t'reltuples', '-1'::real,\n");
11256 : : else
11257 : 3720 : appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples);
11258 : :
11259 : 3720 : appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer",
11260 : 3720 : rsinfo->relallvisible);
11261 : :
11262 [ + - ]: 3720 : if (fout->remoteVersion >= 180000)
11263 : 3720 : appendPQExpBuffer(out, ",\n\t'relallfrozen', '%d'::integer", rsinfo->relallfrozen);
11264 : :
11265 : 3720 : appendPQExpBufferStr(out, "\n);\n");
11266 : :
11267 : : /* Fetch the next batch of attribute statistics if needed. */
11268 [ + + ]: 3720 : if (rownum >= PQntuples(res))
11269 : : {
11270 : 1136 : PQclear(res);
11271 : 1136 : res = fetchAttributeStats(fout);
11272 : 1136 : rownum = 0;
11273 : : }
11274 : :
11275 : 3720 : i_schemaname = PQfnumber(res, "schemaname");
11276 : 3720 : i_tablename = PQfnumber(res, "tablename");
11277 : 3720 : i_attname = PQfnumber(res, "attname");
11278 : 3720 : i_inherited = PQfnumber(res, "inherited");
11279 : 3720 : i_null_frac = PQfnumber(res, "null_frac");
11280 : 3720 : i_avg_width = PQfnumber(res, "avg_width");
11281 : 3720 : i_n_distinct = PQfnumber(res, "n_distinct");
11282 : 3720 : i_most_common_vals = PQfnumber(res, "most_common_vals");
11283 : 3720 : i_most_common_freqs = PQfnumber(res, "most_common_freqs");
11284 : 3720 : i_histogram_bounds = PQfnumber(res, "histogram_bounds");
11285 : 3720 : i_correlation = PQfnumber(res, "correlation");
11286 : 3720 : i_most_common_elems = PQfnumber(res, "most_common_elems");
11287 : 3720 : i_most_common_elem_freqs = PQfnumber(res, "most_common_elem_freqs");
11288 : 3720 : i_elem_count_histogram = PQfnumber(res, "elem_count_histogram");
11289 : 3720 : i_range_length_histogram = PQfnumber(res, "range_length_histogram");
11290 : 3720 : i_range_empty_frac = PQfnumber(res, "range_empty_frac");
11291 : 3720 : i_range_bounds_histogram = PQfnumber(res, "range_bounds_histogram");
11292 : :
11293 : : /* restore attribute stats */
11294 [ + + ]: 4665 : for (; rownum < PQntuples(res); rownum++)
11295 : : {
11296 : : const char *attname;
11297 : :
11298 : : /* Stop if the next stat row in our cache isn't for this relation. */
11299 [ + + ]: 3529 : if (strcmp(te->tag, PQgetvalue(res, rownum, i_tablename)) != 0 ||
11300 [ + - ]: 945 : strcmp(te->namespace, PQgetvalue(res, rownum, i_schemaname)) != 0)
11301 : : break;
11302 : :
11303 : 945 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_attribute_stats(\n");
11304 : 945 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11305 : : fout->remoteVersion);
11306 : 945 : appendPQExpBufferStr(out, "\t'schemaname', ");
11307 : 945 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11308 : 945 : appendPQExpBufferStr(out, ",\n\t'relname', ");
11309 : 945 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11310 : :
11311 [ - + ]: 945 : if (PQgetisnull(res, rownum, i_attname))
11312 : 0 : pg_fatal("unexpected null attname");
11313 : 945 : attname = PQgetvalue(res, rownum, i_attname);
11314 : :
11315 : : /*
11316 : : * Indexes look up attname in indAttNames to derive attnum, all others
11317 : : * use attname directly. We must specify attnum for indexes, since
11318 : : * their attnames are not necessarily stable across dump/reload.
11319 : : */
11320 [ + + ]: 945 : if (rsinfo->nindAttNames == 0)
11321 : : {
11322 : 905 : appendPQExpBufferStr(out, ",\n\t'attname', ");
11323 : 905 : appendStringLiteralAH(out, attname, fout);
11324 : : }
11325 : : else
11326 : : {
11327 : 40 : bool found = false;
11328 : :
11329 [ + - ]: 74 : for (int i = 0; i < rsinfo->nindAttNames; i++)
11330 : : {
11331 [ + + ]: 74 : if (strcmp(attname, rsinfo->indAttNames[i]) == 0)
11332 : : {
11333 : 40 : appendPQExpBuffer(out, ",\n\t'attnum', '%d'::smallint",
11334 : : i + 1);
11335 : 40 : found = true;
11336 : 40 : break;
11337 : : }
11338 : : }
11339 : :
11340 [ - + ]: 40 : if (!found)
11341 : 0 : pg_fatal("could not find index attname \"%s\"", attname);
11342 : : }
11343 : :
11344 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_inherited))
11345 : 945 : appendNamedArgument(out, fout, "inherited", "boolean",
11346 : 945 : PQgetvalue(res, rownum, i_inherited));
11347 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_null_frac))
11348 : 945 : appendNamedArgument(out, fout, "null_frac", "real",
11349 : 945 : PQgetvalue(res, rownum, i_null_frac));
11350 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_avg_width))
11351 : 945 : appendNamedArgument(out, fout, "avg_width", "integer",
11352 : 945 : PQgetvalue(res, rownum, i_avg_width));
11353 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_n_distinct))
11354 : 945 : appendNamedArgument(out, fout, "n_distinct", "real",
11355 : 945 : PQgetvalue(res, rownum, i_n_distinct));
11356 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_vals))
11357 : 492 : appendNamedArgument(out, fout, "most_common_vals", "text",
11358 : 492 : PQgetvalue(res, rownum, i_most_common_vals));
11359 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_freqs))
11360 : 492 : appendNamedArgument(out, fout, "most_common_freqs", "real[]",
11361 : 492 : PQgetvalue(res, rownum, i_most_common_freqs));
11362 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_histogram_bounds))
11363 : 604 : appendNamedArgument(out, fout, "histogram_bounds", "text",
11364 : 604 : PQgetvalue(res, rownum, i_histogram_bounds));
11365 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_correlation))
11366 : 902 : appendNamedArgument(out, fout, "correlation", "real",
11367 : 902 : PQgetvalue(res, rownum, i_correlation));
11368 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_elems))
11369 : 8 : appendNamedArgument(out, fout, "most_common_elems", "text",
11370 : 8 : PQgetvalue(res, rownum, i_most_common_elems));
11371 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_elem_freqs))
11372 : 8 : appendNamedArgument(out, fout, "most_common_elem_freqs", "real[]",
11373 : 8 : PQgetvalue(res, rownum, i_most_common_elem_freqs));
11374 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_elem_count_histogram))
11375 : 7 : appendNamedArgument(out, fout, "elem_count_histogram", "real[]",
11376 : 7 : PQgetvalue(res, rownum, i_elem_count_histogram));
11377 [ + - ]: 945 : if (fout->remoteVersion >= 170000)
11378 : : {
11379 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_length_histogram))
11380 : 4 : appendNamedArgument(out, fout, "range_length_histogram", "text",
11381 : 4 : PQgetvalue(res, rownum, i_range_length_histogram));
11382 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_empty_frac))
11383 : 4 : appendNamedArgument(out, fout, "range_empty_frac", "real",
11384 : 4 : PQgetvalue(res, rownum, i_range_empty_frac));
11385 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_bounds_histogram))
11386 : 4 : appendNamedArgument(out, fout, "range_bounds_histogram", "text",
11387 : 4 : PQgetvalue(res, rownum, i_range_bounds_histogram));
11388 : : }
11389 : 945 : appendPQExpBufferStr(out, "\n);\n");
11390 : : }
11391 : :
11392 : 3720 : destroyPQExpBuffer(query);
11393 : 3720 : return out->data;
11394 : : }
11395 : :
11396 : : /*
11397 : : * dumpRelationStats --
11398 : : *
11399 : : * Make an ArchiveEntry for the relation statistics. The Archiver will take
11400 : : * care of gathering the statistics and generating the restore commands when
11401 : : * they are needed.
11402 : : */
11403 : : static void
11404 : 3796 : dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
11405 : : {
11406 : 3796 : const DumpableObject *dobj = &rsinfo->dobj;
11407 : :
11408 : : /* nothing to do if we are not dumping statistics */
11409 [ - + ]: 3796 : if (!fout->dopt->dumpStatistics)
11410 : 0 : return;
11411 : :
11412 : 3796 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11413 : 3796 : ARCHIVE_OPTS(.tag = dobj->name,
11414 : : .namespace = dobj->namespace->dobj.name,
11415 : : .description = "STATISTICS DATA",
11416 : : .section = rsinfo->section,
11417 : : .defnFn = dumpRelationStats_dumper,
11418 : : .defnArg = rsinfo,
11419 : : .deps = dobj->dependencies,
11420 : : .nDeps = dobj->nDeps));
11421 : : }
11422 : :
11423 : : /*
11424 : : * dumpTableComment --
11425 : : *
11426 : : * As above, but dump comments for both the specified table (or view)
11427 : : * and its columns.
11428 : : */
11429 : : static void
11430 : 78 : dumpTableComment(Archive *fout, const TableInfo *tbinfo,
11431 : : const char *reltypename)
11432 : : {
11433 : 78 : DumpOptions *dopt = fout->dopt;
11434 : : CommentItem *comments;
11435 : : int ncomments;
11436 : : PQExpBuffer query;
11437 : : PQExpBuffer tag;
11438 : :
11439 : : /* do nothing, if --no-comments is supplied */
11440 [ - + ]: 78 : if (dopt->no_comments)
11441 : 0 : return;
11442 : :
11443 : : /* Comments are SCHEMA not data */
11444 [ - + ]: 78 : if (!dopt->dumpSchema)
11445 : 0 : return;
11446 : :
11447 : : /* Search for comments associated with relation, using table */
11448 : 78 : ncomments = findComments(tbinfo->dobj.catId.tableoid,
11449 : 78 : tbinfo->dobj.catId.oid,
11450 : : &comments);
11451 : :
11452 : : /* If comments exist, build COMMENT ON statements */
11453 [ - + ]: 78 : if (ncomments <= 0)
11454 : 0 : return;
11455 : :
11456 : 78 : query = createPQExpBuffer();
11457 : 78 : tag = createPQExpBuffer();
11458 : :
11459 [ + + ]: 224 : while (ncomments > 0)
11460 : : {
11461 : 146 : const char *descr = comments->descr;
11462 : 146 : int objsubid = comments->objsubid;
11463 : :
11464 [ + + ]: 146 : if (objsubid == 0)
11465 : : {
11466 : 34 : resetPQExpBuffer(tag);
11467 : 34 : appendPQExpBuffer(tag, "%s %s", reltypename,
11468 : 34 : fmtId(tbinfo->dobj.name));
11469 : :
11470 : 34 : resetPQExpBuffer(query);
11471 : 34 : appendPQExpBuffer(query, "COMMENT ON %s %s IS ", reltypename,
11472 : 34 : fmtQualifiedDumpable(tbinfo));
11473 : 34 : appendStringLiteralAH(query, descr, fout);
11474 : 34 : appendPQExpBufferStr(query, ";\n");
11475 : :
11476 : 34 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11477 : 34 : ARCHIVE_OPTS(.tag = tag->data,
11478 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11479 : : .owner = tbinfo->rolname,
11480 : : .description = "COMMENT",
11481 : : .section = SECTION_NONE,
11482 : : .createStmt = query->data,
11483 : : .deps = &(tbinfo->dobj.dumpId),
11484 : : .nDeps = 1));
11485 : : }
11486 [ + - + - ]: 112 : else if (objsubid > 0 && objsubid <= tbinfo->numatts)
11487 : : {
11488 : 112 : resetPQExpBuffer(tag);
11489 : 112 : appendPQExpBuffer(tag, "COLUMN %s.",
11490 : 112 : fmtId(tbinfo->dobj.name));
11491 : 112 : appendPQExpBufferStr(tag, fmtId(tbinfo->attnames[objsubid - 1]));
11492 : :
11493 : 112 : resetPQExpBuffer(query);
11494 : 112 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11495 : 112 : fmtQualifiedDumpable(tbinfo));
11496 : 112 : appendPQExpBuffer(query, "%s IS ",
11497 : 112 : fmtId(tbinfo->attnames[objsubid - 1]));
11498 : 112 : appendStringLiteralAH(query, descr, fout);
11499 : 112 : appendPQExpBufferStr(query, ";\n");
11500 : :
11501 : 112 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11502 : 112 : ARCHIVE_OPTS(.tag = tag->data,
11503 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11504 : : .owner = tbinfo->rolname,
11505 : : .description = "COMMENT",
11506 : : .section = SECTION_NONE,
11507 : : .createStmt = query->data,
11508 : : .deps = &(tbinfo->dobj.dumpId),
11509 : : .nDeps = 1));
11510 : : }
11511 : :
11512 : 146 : comments++;
11513 : 146 : ncomments--;
11514 : : }
11515 : :
11516 : 78 : destroyPQExpBuffer(query);
11517 : 78 : destroyPQExpBuffer(tag);
11518 : : }
11519 : :
11520 : : /*
11521 : : * findComments --
11522 : : *
11523 : : * Find the comment(s), if any, associated with the given object. All the
11524 : : * objsubid values associated with the given classoid/objoid are found with
11525 : : * one search.
11526 : : */
11527 : : static int
11528 : 6779 : findComments(Oid classoid, Oid objoid, CommentItem **items)
11529 : : {
11530 : 6779 : CommentItem *middle = NULL;
11531 : : CommentItem *low;
11532 : : CommentItem *high;
11533 : : int nmatch;
11534 : :
11535 : : /*
11536 : : * Do binary search to find some item matching the object.
11537 : : */
11538 : 6779 : low = &comments[0];
11539 : 6779 : high = &comments[ncomments - 1];
11540 [ + + ]: 67895 : while (low <= high)
11541 : : {
11542 : 67848 : middle = low + (high - low) / 2;
11543 : :
11544 [ + + ]: 67848 : if (classoid < middle->classoid)
11545 : 7331 : high = middle - 1;
11546 [ + + ]: 60517 : else if (classoid > middle->classoid)
11547 : 7381 : low = middle + 1;
11548 [ + + ]: 53136 : else if (objoid < middle->objoid)
11549 : 22504 : high = middle - 1;
11550 [ + + ]: 30632 : else if (objoid > middle->objoid)
11551 : 23900 : low = middle + 1;
11552 : : else
11553 : 6732 : break; /* found a match */
11554 : : }
11555 : :
11556 [ + + ]: 6779 : if (low > high) /* no matches */
11557 : : {
11558 : 47 : *items = NULL;
11559 : 47 : return 0;
11560 : : }
11561 : :
11562 : : /*
11563 : : * Now determine how many items match the object. The search loop
11564 : : * invariant still holds: only items between low and high inclusive could
11565 : : * match.
11566 : : */
11567 : 6732 : nmatch = 1;
11568 [ + + ]: 6788 : while (middle > low)
11569 : : {
11570 [ + + ]: 3284 : if (classoid != middle[-1].classoid ||
11571 [ + + ]: 3122 : objoid != middle[-1].objoid)
11572 : : break;
11573 : 56 : middle--;
11574 : 56 : nmatch++;
11575 : : }
11576 : :
11577 : 6732 : *items = middle;
11578 : :
11579 : 6732 : middle += nmatch;
11580 [ + + ]: 6744 : while (middle <= high)
11581 : : {
11582 [ + + ]: 3491 : if (classoid != middle->classoid ||
11583 [ + + ]: 3370 : objoid != middle->objoid)
11584 : : break;
11585 : 12 : middle++;
11586 : 12 : nmatch++;
11587 : : }
11588 : :
11589 : 6732 : return nmatch;
11590 : : }
11591 : :
11592 : : /*
11593 : : * collectComments --
11594 : : *
11595 : : * Construct a table of all comments available for database objects;
11596 : : * also set the has-comment component flag for each relevant object.
11597 : : *
11598 : : * We used to do per-object queries for the comments, but it's much faster
11599 : : * to pull them all over at once, and on most databases the memory cost
11600 : : * isn't high.
11601 : : *
11602 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
11603 : : */
11604 : : static void
11605 : 215 : collectComments(Archive *fout)
11606 : : {
11607 : : PGresult *res;
11608 : : PQExpBuffer query;
11609 : : int i_description;
11610 : : int i_classoid;
11611 : : int i_objoid;
11612 : : int i_objsubid;
11613 : : int ntups;
11614 : : int i;
11615 : : DumpableObject *dobj;
11616 : :
11617 : 215 : query = createPQExpBuffer();
11618 : :
11619 : 215 : appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
11620 : : "FROM pg_catalog.pg_description "
11621 : : "ORDER BY classoid, objoid, objsubid");
11622 : :
11623 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11624 : :
11625 : : /* Construct lookup table containing OIDs in numeric form */
11626 : :
11627 : 215 : i_description = PQfnumber(res, "description");
11628 : 215 : i_classoid = PQfnumber(res, "classoid");
11629 : 215 : i_objoid = PQfnumber(res, "objoid");
11630 : 215 : i_objsubid = PQfnumber(res, "objsubid");
11631 : :
11632 : 215 : ntups = PQntuples(res);
11633 : :
11634 : 215 : comments = pg_malloc_array(CommentItem, ntups);
11635 : 215 : ncomments = 0;
11636 : 215 : dobj = NULL;
11637 : :
11638 [ + + ]: 1162298 : for (i = 0; i < ntups; i++)
11639 : : {
11640 : : CatalogId objId;
11641 : : int subid;
11642 : :
11643 : 1162083 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
11644 : 1162083 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
11645 : 1162083 : subid = atoi(PQgetvalue(res, i, i_objsubid));
11646 : :
11647 : : /* We needn't remember comments that don't match any dumpable object */
11648 [ + + ]: 1162083 : if (dobj == NULL ||
11649 [ + + ]: 421517 : dobj->catId.tableoid != objId.tableoid ||
11650 [ + + ]: 418901 : dobj->catId.oid != objId.oid)
11651 : 1161987 : dobj = findObjectByCatalogId(objId);
11652 [ + + ]: 1162083 : if (dobj == NULL)
11653 : 740357 : continue;
11654 : :
11655 : : /*
11656 : : * Comments on columns of composite types are linked to the type's
11657 : : * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
11658 : : * in the type's own DumpableObject.
11659 : : */
11660 [ + + + - ]: 421726 : if (subid != 0 && dobj->objType == DO_TABLE &&
11661 [ + + ]: 206 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
11662 : 48 : {
11663 : : TypeInfo *cTypeInfo;
11664 : :
11665 : 48 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
11666 [ + - ]: 48 : if (cTypeInfo)
11667 : 48 : cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
11668 : : }
11669 : : else
11670 : 421678 : dobj->components |= DUMP_COMPONENT_COMMENT;
11671 : :
11672 : 421726 : comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
11673 : 421726 : comments[ncomments].classoid = objId.tableoid;
11674 : 421726 : comments[ncomments].objoid = objId.oid;
11675 : 421726 : comments[ncomments].objsubid = subid;
11676 : 421726 : ncomments++;
11677 : : }
11678 : :
11679 : 215 : PQclear(res);
11680 : 215 : destroyPQExpBuffer(query);
11681 : 215 : }
11682 : :
11683 : : /*
11684 : : * dumpDumpableObject
11685 : : *
11686 : : * This routine and its subsidiaries are responsible for creating
11687 : : * ArchiveEntries (TOC objects) for each object to be dumped.
11688 : : */
11689 : : static void
11690 : 811238 : dumpDumpableObject(Archive *fout, DumpableObject *dobj)
11691 : : {
11692 : : /*
11693 : : * Clear any dump-request bits for components that don't exist for this
11694 : : * object. (This makes it safe to initially use DUMP_COMPONENT_ALL as the
11695 : : * request for every kind of object.)
11696 : : */
11697 : 811238 : dobj->dump &= dobj->components;
11698 : :
11699 : : /* Now, short-circuit if there's nothing to be done here. */
11700 [ + + ]: 811238 : if (dobj->dump == 0)
11701 : 725274 : return;
11702 : :
11703 [ + + + + : 85964 : switch (dobj->objType)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + - ]
11704 : : {
11705 : 546 : case DO_NAMESPACE:
11706 : 546 : dumpNamespace(fout, (const NamespaceInfo *) dobj);
11707 : 546 : break;
11708 : 25 : case DO_EXTENSION:
11709 : 25 : dumpExtension(fout, (const ExtensionInfo *) dobj);
11710 : 25 : break;
11711 : 1029 : case DO_TYPE:
11712 : 1029 : dumpType(fout, (const TypeInfo *) dobj);
11713 : 1029 : break;
11714 : 76 : case DO_SHELL_TYPE:
11715 : 76 : dumpShellType(fout, (const ShellTypeInfo *) dobj);
11716 : 76 : break;
11717 : 1892 : case DO_FUNC:
11718 : 1892 : dumpFunc(fout, (const FuncInfo *) dobj);
11719 : 1892 : break;
11720 : 295 : case DO_AGG:
11721 : 295 : dumpAgg(fout, (const AggInfo *) dobj);
11722 : 295 : break;
11723 : 2525 : case DO_OPERATOR:
11724 : 2525 : dumpOpr(fout, (const OprInfo *) dobj);
11725 : 2525 : break;
11726 : 84 : case DO_ACCESS_METHOD:
11727 : 84 : dumpAccessMethod(fout, (const AccessMethodInfo *) dobj);
11728 : 84 : break;
11729 : 675 : case DO_OPCLASS:
11730 : 675 : dumpOpclass(fout, (const OpclassInfo *) dobj);
11731 : 675 : break;
11732 : 561 : case DO_OPFAMILY:
11733 : 561 : dumpOpfamily(fout, (const OpfamilyInfo *) dobj);
11734 : 561 : break;
11735 : 2733 : case DO_COLLATION:
11736 : 2733 : dumpCollation(fout, (const CollInfo *) dobj);
11737 : 2733 : break;
11738 : 335 : case DO_CONVERSION:
11739 : 335 : dumpConversion(fout, (const ConvInfo *) dobj);
11740 : 335 : break;
11741 : 36965 : case DO_TABLE:
11742 : 36965 : dumpTable(fout, (const TableInfo *) dobj);
11743 : 36965 : break;
11744 : 1475 : case DO_TABLE_ATTACH:
11745 : 1475 : dumpTableAttach(fout, (const TableAttachInfo *) dobj);
11746 : 1475 : break;
11747 : 1143 : case DO_ATTRDEF:
11748 : 1143 : dumpAttrDef(fout, (const AttrDefInfo *) dobj);
11749 : 1143 : break;
11750 : 2791 : case DO_INDEX:
11751 : 2791 : dumpIndex(fout, (const IndxInfo *) dobj);
11752 : 2791 : break;
11753 : 625 : case DO_INDEX_ATTACH:
11754 : 625 : dumpIndexAttach(fout, (const IndexAttachInfo *) dobj);
11755 : 625 : break;
11756 : 196 : case DO_STATSEXT:
11757 : 196 : dumpStatisticsExt(fout, (const StatsExtInfo *) dobj);
11758 : 196 : dumpStatisticsExtStats(fout, (const StatsExtInfo *) dobj);
11759 : 196 : break;
11760 : 363 : case DO_REFRESH_MATVIEW:
11761 : 363 : refreshMatViewData(fout, (const TableDataInfo *) dobj);
11762 : 363 : break;
11763 : 1201 : case DO_RULE:
11764 : 1201 : dumpRule(fout, (const RuleInfo *) dobj);
11765 : 1201 : break;
11766 : 535 : case DO_TRIGGER:
11767 : 535 : dumpTrigger(fout, (const TriggerInfo *) dobj);
11768 : 535 : break;
11769 : 44 : case DO_EVENT_TRIGGER:
11770 : 44 : dumpEventTrigger(fout, (const EventTriggerInfo *) dobj);
11771 : 44 : break;
11772 : 2457 : case DO_CONSTRAINT:
11773 : 2457 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11774 : 2457 : break;
11775 : 177 : case DO_FK_CONSTRAINT:
11776 : 177 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11777 : 177 : break;
11778 : 100 : case DO_PROCLANG:
11779 : 100 : dumpProcLang(fout, (const ProcLangInfo *) dobj);
11780 : 100 : break;
11781 : 69 : case DO_CAST:
11782 : 69 : dumpCast(fout, (const CastInfo *) dobj);
11783 : 69 : break;
11784 : 44 : case DO_TRANSFORM:
11785 : 44 : dumpTransform(fout, (const TransformInfo *) dobj);
11786 : 44 : break;
11787 : 415 : case DO_SEQUENCE_SET:
11788 : 415 : dumpSequenceData(fout, (const TableDataInfo *) dobj);
11789 : 415 : break;
11790 : 4638 : case DO_TABLE_DATA:
11791 : 4638 : dumpTableData(fout, (const TableDataInfo *) dobj);
11792 : 4638 : break;
11793 : 15539 : case DO_DUMMY_TYPE:
11794 : : /* table rowtypes and array types are never dumped separately */
11795 : 15539 : break;
11796 : 44 : case DO_TSPARSER:
11797 : 44 : dumpTSParser(fout, (const TSParserInfo *) dobj);
11798 : 44 : break;
11799 : 182 : case DO_TSDICT:
11800 : 182 : dumpTSDictionary(fout, (const TSDictInfo *) dobj);
11801 : 182 : break;
11802 : 56 : case DO_TSTEMPLATE:
11803 : 56 : dumpTSTemplate(fout, (const TSTemplateInfo *) dobj);
11804 : 56 : break;
11805 : 157 : case DO_TSCONFIG:
11806 : 157 : dumpTSConfig(fout, (const TSConfigInfo *) dobj);
11807 : 157 : break;
11808 : 55 : case DO_FDW:
11809 : 55 : dumpForeignDataWrapper(fout, (const FdwInfo *) dobj);
11810 : 55 : break;
11811 : 59 : case DO_FOREIGN_SERVER:
11812 : 59 : dumpForeignServer(fout, (const ForeignServerInfo *) dobj);
11813 : 59 : break;
11814 : 170 : case DO_DEFAULT_ACL:
11815 : 170 : dumpDefaultACL(fout, (const DefaultACLInfo *) dobj);
11816 : 170 : break;
11817 : 88 : case DO_LARGE_OBJECT:
11818 : 88 : dumpLO(fout, (const LoInfo *) dobj);
11819 : 88 : break;
11820 : 88 : case DO_LARGE_OBJECT_DATA:
11821 [ + - ]: 88 : if (dobj->dump & DUMP_COMPONENT_DATA)
11822 : : {
11823 : : LoInfo *loinfo;
11824 : : TocEntry *te;
11825 : :
11826 : 88 : loinfo = (LoInfo *) findObjectByDumpId(dobj->dependencies[0]);
11827 [ - + ]: 88 : if (loinfo == NULL)
11828 : 0 : pg_fatal("missing metadata for large objects \"%s\"",
11829 : : dobj->name);
11830 : :
11831 : 88 : te = ArchiveEntry(fout, dobj->catId, dobj->dumpId,
11832 : 88 : ARCHIVE_OPTS(.tag = dobj->name,
11833 : : .owner = loinfo->rolname,
11834 : : .description = "BLOBS",
11835 : : .section = SECTION_DATA,
11836 : : .deps = dobj->dependencies,
11837 : : .nDeps = dobj->nDeps,
11838 : : .dumpFn = dumpLOs,
11839 : : .dumpArg = loinfo));
11840 : :
11841 : : /*
11842 : : * Set the TocEntry's dataLength in case we are doing a
11843 : : * parallel dump and want to order dump jobs by table size.
11844 : : * (We need some size estimate for every TocEntry with a
11845 : : * DataDumper function.) We don't currently have any cheap
11846 : : * way to estimate the size of LOs, but fortunately it doesn't
11847 : : * matter too much as long as we get large batches of LOs
11848 : : * processed reasonably early. Assume 8K per blob.
11849 : : */
11850 : 88 : te->dataLength = loinfo->numlos * (pgoff_t) 8192;
11851 : : }
11852 : 88 : break;
11853 : 347 : case DO_POLICY:
11854 : 347 : dumpPolicy(fout, (const PolicyInfo *) dobj);
11855 : 347 : break;
11856 : 416 : case DO_PUBLICATION:
11857 : 416 : dumpPublication(fout, (const PublicationInfo *) dobj);
11858 : 416 : break;
11859 : 298 : case DO_PUBLICATION_REL:
11860 : 298 : dumpPublicationTable(fout, (const PublicationRelInfo *) dobj);
11861 : 298 : break;
11862 : 103 : case DO_PUBLICATION_TABLE_IN_SCHEMA:
11863 : 103 : dumpPublicationNamespace(fout,
11864 : : (const PublicationSchemaInfo *) dobj);
11865 : 103 : break;
11866 : 119 : case DO_SUBSCRIPTION:
11867 : 119 : dumpSubscription(fout, (const SubscriptionInfo *) dobj);
11868 : 119 : break;
11869 : 3 : case DO_SUBSCRIPTION_REL:
11870 : 3 : dumpSubscriptionTable(fout, (const SubRelInfo *) dobj);
11871 : 3 : break;
11872 : 3796 : case DO_REL_STATS:
11873 : 3796 : dumpRelationStats(fout, (const RelStatsInfo *) dobj);
11874 : 3796 : break;
11875 : 430 : case DO_PRE_DATA_BOUNDARY:
11876 : : case DO_POST_DATA_BOUNDARY:
11877 : : /* never dumped, nothing to do */
11878 : 430 : break;
11879 : : }
11880 : : }
11881 : :
11882 : : /*
11883 : : * dumpNamespace
11884 : : * writes out to fout the queries to recreate a user-defined namespace
11885 : : */
11886 : : static void
11887 : 546 : dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo)
11888 : : {
11889 : 546 : DumpOptions *dopt = fout->dopt;
11890 : : PQExpBuffer q;
11891 : : PQExpBuffer delq;
11892 : : char *qnspname;
11893 : :
11894 : : /* Do nothing if not dumping schema */
11895 [ + + ]: 546 : if (!dopt->dumpSchema)
11896 : 29 : return;
11897 : :
11898 : 517 : q = createPQExpBuffer();
11899 : 517 : delq = createPQExpBuffer();
11900 : :
11901 : 517 : qnspname = pg_strdup(fmtId(nspinfo->dobj.name));
11902 : :
11903 [ + + ]: 517 : if (nspinfo->create)
11904 : : {
11905 : 338 : appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
11906 : 338 : appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname);
11907 : : }
11908 : : else
11909 : : {
11910 : : /* see selectDumpableNamespace() */
11911 : 179 : appendPQExpBufferStr(delq,
11912 : : "-- *not* dropping schema, since initdb creates it\n");
11913 : 179 : appendPQExpBufferStr(q,
11914 : : "-- *not* creating schema, since initdb creates it\n");
11915 : : }
11916 : :
11917 [ + + ]: 517 : if (dopt->binary_upgrade)
11918 : 106 : binary_upgrade_extension_member(q, &nspinfo->dobj,
11919 : : "SCHEMA", qnspname, NULL);
11920 : :
11921 [ + + ]: 517 : if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11922 : 207 : ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
11923 : 207 : ARCHIVE_OPTS(.tag = nspinfo->dobj.name,
11924 : : .owner = nspinfo->rolname,
11925 : : .description = "SCHEMA",
11926 : : .section = SECTION_PRE_DATA,
11927 : : .createStmt = q->data,
11928 : : .dropStmt = delq->data));
11929 : :
11930 : : /* Dump Schema Comments and Security Labels */
11931 [ + + ]: 517 : if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11932 : : {
11933 : 184 : const char *initdb_comment = NULL;
11934 : :
11935 [ + + + + ]: 184 : if (!nspinfo->create && strcmp(qnspname, "public") == 0)
11936 : 137 : initdb_comment = "standard public schema";
11937 : 184 : dumpCommentExtended(fout, "SCHEMA", qnspname,
11938 : 184 : NULL, nspinfo->rolname,
11939 : 184 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId,
11940 : : initdb_comment);
11941 : : }
11942 : :
11943 [ - + ]: 517 : if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11944 : 0 : dumpSecLabel(fout, "SCHEMA", qnspname,
11945 : 0 : NULL, nspinfo->rolname,
11946 : 0 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
11947 : :
11948 [ + + ]: 517 : if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL)
11949 : 413 : dumpACL(fout, nspinfo->dobj.dumpId, InvalidDumpId, "SCHEMA",
11950 : : qnspname, NULL, NULL,
11951 : 413 : NULL, nspinfo->rolname, &nspinfo->dacl);
11952 : :
11953 : 517 : pg_free(qnspname);
11954 : :
11955 : 517 : destroyPQExpBuffer(q);
11956 : 517 : destroyPQExpBuffer(delq);
11957 : : }
11958 : :
11959 : : /*
11960 : : * dumpExtension
11961 : : * writes out to fout the queries to recreate an extension
11962 : : */
11963 : : static void
11964 : 25 : dumpExtension(Archive *fout, const ExtensionInfo *extinfo)
11965 : : {
11966 : 25 : DumpOptions *dopt = fout->dopt;
11967 : : PQExpBuffer q;
11968 : : PQExpBuffer delq;
11969 : : char *qextname;
11970 : :
11971 : : /* Do nothing if not dumping schema */
11972 [ + + ]: 25 : if (!dopt->dumpSchema)
11973 : 1 : return;
11974 : :
11975 : 24 : q = createPQExpBuffer();
11976 : 24 : delq = createPQExpBuffer();
11977 : :
11978 : 24 : qextname = pg_strdup(fmtId(extinfo->dobj.name));
11979 : :
11980 : 24 : appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname);
11981 : :
11982 [ + + ]: 24 : if (!dopt->binary_upgrade)
11983 : : {
11984 : : /*
11985 : : * In a regular dump, we simply create the extension, intentionally
11986 : : * not specifying a version, so that the destination installation's
11987 : : * default version is used.
11988 : : *
11989 : : * Use of IF NOT EXISTS here is unlike our behavior for other object
11990 : : * types; but there are various scenarios in which it's convenient to
11991 : : * manually create the desired extension before restoring, so we
11992 : : * prefer to allow it to exist already.
11993 : : */
11994 : 17 : appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n",
11995 : 17 : qextname, fmtId(extinfo->namespace));
11996 : : }
11997 : : else
11998 : : {
11999 : : /*
12000 : : * In binary-upgrade mode, it's critical to reproduce the state of the
12001 : : * database exactly, so our procedure is to create an empty extension,
12002 : : * restore all the contained objects normally, and add them to the
12003 : : * extension one by one. This function performs just the first of
12004 : : * those steps. binary_upgrade_extension_member() takes care of
12005 : : * adding member objects as they're created.
12006 : : */
12007 : : int i;
12008 : : int n;
12009 : :
12010 : 7 : appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
12011 : :
12012 : : /*
12013 : : * We unconditionally create the extension, so we must drop it if it
12014 : : * exists. This could happen if the user deleted 'plpgsql' and then
12015 : : * readded it, causing its oid to be greater than g_last_builtin_oid.
12016 : : */
12017 : 7 : appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
12018 : :
12019 : 7 : appendPQExpBufferStr(q,
12020 : : "SELECT pg_catalog.binary_upgrade_create_empty_extension(");
12021 : 7 : appendStringLiteralAH(q, extinfo->dobj.name, fout);
12022 : 7 : appendPQExpBufferStr(q, ", ");
12023 : 7 : appendStringLiteralAH(q, extinfo->namespace, fout);
12024 : 7 : appendPQExpBufferStr(q, ", ");
12025 [ + - ]: 7 : appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false");
12026 : 7 : appendStringLiteralAH(q, extinfo->extversion, fout);
12027 : 7 : appendPQExpBufferStr(q, ", ");
12028 : :
12029 : : /*
12030 : : * Note that we're pushing extconfig (an OID array) back into
12031 : : * pg_extension exactly as-is. This is OK because pg_class OIDs are
12032 : : * preserved in binary upgrade.
12033 : : */
12034 [ + + ]: 7 : if (strlen(extinfo->extconfig) > 2)
12035 : 1 : appendStringLiteralAH(q, extinfo->extconfig, fout);
12036 : : else
12037 : 6 : appendPQExpBufferStr(q, "NULL");
12038 : 7 : appendPQExpBufferStr(q, ", ");
12039 [ + + ]: 7 : if (strlen(extinfo->extcondition) > 2)
12040 : 1 : appendStringLiteralAH(q, extinfo->extcondition, fout);
12041 : : else
12042 : 6 : appendPQExpBufferStr(q, "NULL");
12043 : 7 : appendPQExpBufferStr(q, ", ");
12044 : 7 : appendPQExpBufferStr(q, "ARRAY[");
12045 : 7 : n = 0;
12046 [ + + ]: 14 : for (i = 0; i < extinfo->dobj.nDeps; i++)
12047 : : {
12048 : : DumpableObject *extobj;
12049 : :
12050 : 7 : extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]);
12051 [ + - - + ]: 7 : if (extobj && extobj->objType == DO_EXTENSION)
12052 : : {
12053 [ # # ]: 0 : if (n++ > 0)
12054 : 0 : appendPQExpBufferChar(q, ',');
12055 : 0 : appendStringLiteralAH(q, extobj->name, fout);
12056 : : }
12057 : : }
12058 : 7 : appendPQExpBufferStr(q, "]::pg_catalog.text[]");
12059 : 7 : appendPQExpBufferStr(q, ");\n");
12060 : : }
12061 : :
12062 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12063 : 24 : ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId,
12064 : 24 : ARCHIVE_OPTS(.tag = extinfo->dobj.name,
12065 : : .description = "EXTENSION",
12066 : : .section = SECTION_PRE_DATA,
12067 : : .createStmt = q->data,
12068 : : .dropStmt = delq->data));
12069 : :
12070 : : /* Dump Extension Comments */
12071 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12072 : 24 : dumpComment(fout, "EXTENSION", qextname,
12073 : : NULL, "",
12074 : 24 : extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
12075 : :
12076 : 24 : pg_free(qextname);
12077 : :
12078 : 24 : destroyPQExpBuffer(q);
12079 : 24 : destroyPQExpBuffer(delq);
12080 : : }
12081 : :
12082 : : /*
12083 : : * dumpType
12084 : : * writes out to fout the queries to recreate a user-defined type
12085 : : */
12086 : : static void
12087 : 1029 : dumpType(Archive *fout, const TypeInfo *tyinfo)
12088 : : {
12089 : 1029 : DumpOptions *dopt = fout->dopt;
12090 : :
12091 : : /* Do nothing if not dumping schema */
12092 [ + + ]: 1029 : if (!dopt->dumpSchema)
12093 : 63 : return;
12094 : :
12095 : : /* Dump out in proper style */
12096 [ + + ]: 966 : if (tyinfo->typtype == TYPTYPE_BASE)
12097 : 285 : dumpBaseType(fout, tyinfo);
12098 [ + + ]: 681 : else if (tyinfo->typtype == TYPTYPE_DOMAIN)
12099 : 164 : dumpDomain(fout, tyinfo);
12100 [ + + ]: 517 : else if (tyinfo->typtype == TYPTYPE_COMPOSITE)
12101 : 177 : dumpCompositeType(fout, tyinfo);
12102 [ + + ]: 340 : else if (tyinfo->typtype == TYPTYPE_ENUM)
12103 : 105 : dumpEnumType(fout, tyinfo);
12104 [ + + ]: 235 : else if (tyinfo->typtype == TYPTYPE_RANGE)
12105 : 121 : dumpRangeType(fout, tyinfo);
12106 [ + - + + ]: 114 : else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
12107 : 39 : dumpUndefinedType(fout, tyinfo);
12108 : : else
12109 : 75 : pg_log_warning("typtype of data type \"%s\" appears to be invalid",
12110 : : tyinfo->dobj.name);
12111 : : }
12112 : :
12113 : : /*
12114 : : * dumpEnumType
12115 : : * writes out to fout the queries to recreate a user-defined enum type
12116 : : */
12117 : : static void
12118 : 105 : dumpEnumType(Archive *fout, const TypeInfo *tyinfo)
12119 : : {
12120 : 105 : DumpOptions *dopt = fout->dopt;
12121 : 105 : PQExpBuffer q = createPQExpBuffer();
12122 : 105 : PQExpBuffer delq = createPQExpBuffer();
12123 : 105 : PQExpBuffer query = createPQExpBuffer();
12124 : : PGresult *res;
12125 : : int num,
12126 : : i;
12127 : : Oid enum_oid;
12128 : : char *qtypname;
12129 : : char *qualtypname;
12130 : : char *label;
12131 : : int i_enumlabel;
12132 : : int i_oid;
12133 : :
12134 [ + + ]: 105 : if (!fout->is_prepared[PREPQUERY_DUMPENUMTYPE])
12135 : : {
12136 : : /* Set up query for enum-specific details */
12137 : 58 : appendPQExpBufferStr(query,
12138 : : "PREPARE dumpEnumType(pg_catalog.oid) AS\n"
12139 : : "SELECT oid, enumlabel "
12140 : : "FROM pg_catalog.pg_enum "
12141 : : "WHERE enumtypid = $1 "
12142 : : "ORDER BY enumsortorder");
12143 : :
12144 : 58 : ExecuteSqlStatement(fout, query->data);
12145 : :
12146 : 58 : fout->is_prepared[PREPQUERY_DUMPENUMTYPE] = true;
12147 : : }
12148 : :
12149 : 105 : printfPQExpBuffer(query,
12150 : : "EXECUTE dumpEnumType('%u')",
12151 : 105 : tyinfo->dobj.catId.oid);
12152 : :
12153 : 105 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12154 : :
12155 : 105 : num = PQntuples(res);
12156 : :
12157 : 105 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12158 : 105 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12159 : :
12160 : : /*
12161 : : * CASCADE shouldn't be required here as for normal types since the I/O
12162 : : * functions are generic and do not get dropped.
12163 : : */
12164 : 105 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12165 : :
12166 [ + + ]: 105 : if (dopt->binary_upgrade)
12167 : 6 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12168 : 6 : tyinfo->dobj.catId.oid,
12169 : : false, false);
12170 : :
12171 : 105 : appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
12172 : : qualtypname);
12173 : :
12174 [ + + ]: 105 : if (!dopt->binary_upgrade)
12175 : : {
12176 : 99 : i_enumlabel = PQfnumber(res, "enumlabel");
12177 : :
12178 : : /* Labels with server-assigned oids */
12179 [ + + ]: 562 : for (i = 0; i < num; i++)
12180 : : {
12181 : 463 : label = PQgetvalue(res, i, i_enumlabel);
12182 [ + + ]: 463 : if (i > 0)
12183 : 364 : appendPQExpBufferChar(q, ',');
12184 : 463 : appendPQExpBufferStr(q, "\n ");
12185 : 463 : appendStringLiteralAH(q, label, fout);
12186 : : }
12187 : : }
12188 : :
12189 : 105 : appendPQExpBufferStr(q, "\n);\n");
12190 : :
12191 [ + + ]: 105 : if (dopt->binary_upgrade)
12192 : : {
12193 : 6 : i_oid = PQfnumber(res, "oid");
12194 : 6 : i_enumlabel = PQfnumber(res, "enumlabel");
12195 : :
12196 : : /* Labels with dump-assigned (preserved) oids */
12197 [ + + ]: 62 : for (i = 0; i < num; i++)
12198 : : {
12199 : 56 : enum_oid = atooid(PQgetvalue(res, i, i_oid));
12200 : 56 : label = PQgetvalue(res, i, i_enumlabel);
12201 : :
12202 [ + + ]: 56 : if (i == 0)
12203 : 6 : appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
12204 : 56 : appendPQExpBuffer(q,
12205 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n",
12206 : : enum_oid);
12207 : 56 : appendPQExpBuffer(q, "ALTER TYPE %s ADD VALUE ", qualtypname);
12208 : 56 : appendStringLiteralAH(q, label, fout);
12209 : 56 : appendPQExpBufferStr(q, ";\n\n");
12210 : : }
12211 : : }
12212 : :
12213 [ + + ]: 105 : if (dopt->binary_upgrade)
12214 : 6 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12215 : : "TYPE", qtypname,
12216 : 6 : tyinfo->dobj.namespace->dobj.name);
12217 : :
12218 [ + - ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12219 : 105 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12220 : 105 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12221 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12222 : : .owner = tyinfo->rolname,
12223 : : .description = "TYPE",
12224 : : .section = SECTION_PRE_DATA,
12225 : : .createStmt = q->data,
12226 : : .dropStmt = delq->data));
12227 : :
12228 : : /* Dump Type Comments and Security Labels */
12229 [ + + ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12230 : 34 : dumpComment(fout, "TYPE", qtypname,
12231 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12232 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12233 : :
12234 [ - + ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12235 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12236 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12237 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12238 : :
12239 [ + + ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12240 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12241 : : qtypname, NULL,
12242 : 34 : tyinfo->dobj.namespace->dobj.name,
12243 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12244 : :
12245 : 105 : PQclear(res);
12246 : 105 : destroyPQExpBuffer(q);
12247 : 105 : destroyPQExpBuffer(delq);
12248 : 105 : destroyPQExpBuffer(query);
12249 : 105 : pg_free(qtypname);
12250 : 105 : pg_free(qualtypname);
12251 : 105 : }
12252 : :
12253 : : /*
12254 : : * dumpRangeType
12255 : : * writes out to fout the queries to recreate a user-defined range type
12256 : : */
12257 : : static void
12258 : 121 : dumpRangeType(Archive *fout, const TypeInfo *tyinfo)
12259 : : {
12260 : 121 : DumpOptions *dopt = fout->dopt;
12261 : 121 : PQExpBuffer q = createPQExpBuffer();
12262 : 121 : PQExpBuffer delq = createPQExpBuffer();
12263 : 121 : PQExpBuffer query = createPQExpBuffer();
12264 : : PGresult *res;
12265 : : Oid collationOid;
12266 : : char *qtypname;
12267 : : char *qualtypname;
12268 : : char *procname;
12269 : :
12270 [ + + ]: 121 : if (!fout->is_prepared[PREPQUERY_DUMPRANGETYPE])
12271 : : {
12272 : : /* Set up query for range-specific details */
12273 : 42 : appendPQExpBufferStr(query,
12274 : : "PREPARE dumpRangeType(pg_catalog.oid) AS\n");
12275 : :
12276 : 42 : appendPQExpBufferStr(query,
12277 : : "SELECT ");
12278 : :
12279 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12280 : 42 : appendPQExpBufferStr(query,
12281 : : "pg_catalog.format_type(rngmultitypid, NULL) AS rngmultitype, ");
12282 : : else
12283 : 0 : appendPQExpBufferStr(query,
12284 : : "NULL AS rngmultitype, ");
12285 : :
12286 : 42 : appendPQExpBufferStr(query,
12287 : : "pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
12288 : : "opc.opcname AS opcname, "
12289 : : "(SELECT nspname FROM pg_catalog.pg_namespace nsp "
12290 : : " WHERE nsp.oid = opc.opcnamespace) AS opcnsp, "
12291 : : "opc.opcdefault, "
12292 : : "CASE WHEN rngcollation = st.typcollation THEN 0 "
12293 : : " ELSE rngcollation END AS collation, "
12294 : : "rngcanonical, rngsubdiff "
12295 : : "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, "
12296 : : " pg_catalog.pg_opclass opc "
12297 : : "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND "
12298 : : "rngtypid = $1");
12299 : :
12300 : 42 : ExecuteSqlStatement(fout, query->data);
12301 : :
12302 : 42 : fout->is_prepared[PREPQUERY_DUMPRANGETYPE] = true;
12303 : : }
12304 : :
12305 : 121 : printfPQExpBuffer(query,
12306 : : "EXECUTE dumpRangeType('%u')",
12307 : 121 : tyinfo->dobj.catId.oid);
12308 : :
12309 : 121 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12310 : :
12311 : 121 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12312 : 121 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12313 : :
12314 : : /*
12315 : : * CASCADE shouldn't be required here as for normal types since the I/O
12316 : : * functions are generic and do not get dropped.
12317 : : */
12318 : 121 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12319 : :
12320 [ + + ]: 121 : if (dopt->binary_upgrade)
12321 : 9 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12322 : 9 : tyinfo->dobj.catId.oid,
12323 : : false, true);
12324 : :
12325 : 121 : appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
12326 : : qualtypname);
12327 : :
12328 : 121 : appendPQExpBuffer(q, "\n subtype = %s",
12329 : : PQgetvalue(res, 0, PQfnumber(res, "rngsubtype")));
12330 : :
12331 [ + - ]: 121 : if (!PQgetisnull(res, 0, PQfnumber(res, "rngmultitype")))
12332 : 121 : appendPQExpBuffer(q, ",\n multirange_type_name = %s",
12333 : : PQgetvalue(res, 0, PQfnumber(res, "rngmultitype")));
12334 : :
12335 : : /* print subtype_opclass only if not default for subtype */
12336 [ + + ]: 121 : if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't')
12337 : : {
12338 : 34 : char *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname"));
12339 : 34 : char *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp"));
12340 : :
12341 : 34 : appendPQExpBuffer(q, ",\n subtype_opclass = %s.",
12342 : : fmtId(nspname));
12343 : 34 : appendPQExpBufferStr(q, fmtId(opcname));
12344 : : }
12345 : :
12346 : 121 : collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation")));
12347 [ + + ]: 121 : if (OidIsValid(collationOid))
12348 : : {
12349 : 39 : CollInfo *coll = findCollationByOid(collationOid);
12350 : :
12351 [ + - ]: 39 : if (coll)
12352 : 39 : appendPQExpBuffer(q, ",\n collation = %s",
12353 : 39 : fmtQualifiedDumpable(coll));
12354 : : }
12355 : :
12356 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical"));
12357 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12358 : 9 : appendPQExpBuffer(q, ",\n canonical = %s", procname);
12359 : :
12360 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff"));
12361 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12362 : 23 : appendPQExpBuffer(q, ",\n subtype_diff = %s", procname);
12363 : :
12364 : 121 : appendPQExpBufferStr(q, "\n);\n");
12365 : :
12366 [ + + ]: 121 : if (dopt->binary_upgrade)
12367 : 9 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12368 : : "TYPE", qtypname,
12369 : 9 : tyinfo->dobj.namespace->dobj.name);
12370 : :
12371 [ + - ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12372 : 121 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12373 : 121 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12374 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12375 : : .owner = tyinfo->rolname,
12376 : : .description = "TYPE",
12377 : : .section = SECTION_PRE_DATA,
12378 : : .createStmt = q->data,
12379 : : .dropStmt = delq->data));
12380 : :
12381 : : /* Dump Type Comments and Security Labels */
12382 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12383 : 52 : dumpComment(fout, "TYPE", qtypname,
12384 : 52 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12385 : 52 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12386 : :
12387 [ - + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12388 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12389 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12390 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12391 : :
12392 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12393 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12394 : : qtypname, NULL,
12395 : 34 : tyinfo->dobj.namespace->dobj.name,
12396 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12397 : :
12398 : 121 : PQclear(res);
12399 : 121 : destroyPQExpBuffer(q);
12400 : 121 : destroyPQExpBuffer(delq);
12401 : 121 : destroyPQExpBuffer(query);
12402 : 121 : pg_free(qtypname);
12403 : 121 : pg_free(qualtypname);
12404 : 121 : }
12405 : :
12406 : : /*
12407 : : * dumpUndefinedType
12408 : : * writes out to fout the queries to recreate a !typisdefined type
12409 : : *
12410 : : * This is a shell type, but we use different terminology to distinguish
12411 : : * this case from where we have to emit a shell type definition to break
12412 : : * circular dependencies. An undefined type shouldn't ever have anything
12413 : : * depending on it.
12414 : : */
12415 : : static void
12416 : 39 : dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo)
12417 : : {
12418 : 39 : DumpOptions *dopt = fout->dopt;
12419 : 39 : PQExpBuffer q = createPQExpBuffer();
12420 : 39 : PQExpBuffer delq = createPQExpBuffer();
12421 : : char *qtypname;
12422 : : char *qualtypname;
12423 : :
12424 : 39 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12425 : 39 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12426 : :
12427 : 39 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12428 : :
12429 [ + + ]: 39 : if (dopt->binary_upgrade)
12430 : 2 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12431 : 2 : tyinfo->dobj.catId.oid,
12432 : : false, false);
12433 : :
12434 : 39 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
12435 : : qualtypname);
12436 : :
12437 [ + + ]: 39 : if (dopt->binary_upgrade)
12438 : 2 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12439 : : "TYPE", qtypname,
12440 : 2 : tyinfo->dobj.namespace->dobj.name);
12441 : :
12442 [ + - ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12443 : 39 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12444 : 39 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12445 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12446 : : .owner = tyinfo->rolname,
12447 : : .description = "TYPE",
12448 : : .section = SECTION_PRE_DATA,
12449 : : .createStmt = q->data,
12450 : : .dropStmt = delq->data));
12451 : :
12452 : : /* Dump Type Comments and Security Labels */
12453 [ + + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12454 : 34 : dumpComment(fout, "TYPE", qtypname,
12455 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12456 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12457 : :
12458 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12459 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12460 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12461 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12462 : :
12463 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12464 : 0 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12465 : : qtypname, NULL,
12466 : 0 : tyinfo->dobj.namespace->dobj.name,
12467 : 0 : NULL, tyinfo->rolname, &tyinfo->dacl);
12468 : :
12469 : 39 : destroyPQExpBuffer(q);
12470 : 39 : destroyPQExpBuffer(delq);
12471 : 39 : pg_free(qtypname);
12472 : 39 : pg_free(qualtypname);
12473 : 39 : }
12474 : :
12475 : : /*
12476 : : * dumpBaseType
12477 : : * writes out to fout the queries to recreate a user-defined base type
12478 : : */
12479 : : static void
12480 : 285 : dumpBaseType(Archive *fout, const TypeInfo *tyinfo)
12481 : : {
12482 : 285 : DumpOptions *dopt = fout->dopt;
12483 : 285 : PQExpBuffer q = createPQExpBuffer();
12484 : 285 : PQExpBuffer delq = createPQExpBuffer();
12485 : 285 : PQExpBuffer query = createPQExpBuffer();
12486 : : PGresult *res;
12487 : : char *qtypname;
12488 : : char *qualtypname;
12489 : : char *typlen;
12490 : : char *typinput;
12491 : : char *typoutput;
12492 : : char *typreceive;
12493 : : char *typsend;
12494 : : char *typmodin;
12495 : : char *typmodout;
12496 : : char *typanalyze;
12497 : : char *typsubscript;
12498 : : Oid typreceiveoid;
12499 : : Oid typsendoid;
12500 : : Oid typmodinoid;
12501 : : Oid typmodoutoid;
12502 : : Oid typanalyzeoid;
12503 : : Oid typsubscriptoid;
12504 : : char *typcategory;
12505 : : char *typispreferred;
12506 : : char *typdelim;
12507 : : char *typbyval;
12508 : : char *typalign;
12509 : : char *typstorage;
12510 : : char *typcollatable;
12511 : : char *typdefault;
12512 : 285 : bool typdefault_is_literal = false;
12513 : :
12514 [ + + ]: 285 : if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
12515 : : {
12516 : : /* Set up query for type-specific details */
12517 : 42 : appendPQExpBufferStr(query,
12518 : : "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
12519 : : "SELECT typlen, "
12520 : : "typinput, typoutput, typreceive, typsend, "
12521 : : "typreceive::pg_catalog.oid AS typreceiveoid, "
12522 : : "typsend::pg_catalog.oid AS typsendoid, "
12523 : : "typanalyze, "
12524 : : "typanalyze::pg_catalog.oid AS typanalyzeoid, "
12525 : : "typdelim, typbyval, typalign, typstorage, "
12526 : : "typmodin, typmodout, "
12527 : : "typmodin::pg_catalog.oid AS typmodinoid, "
12528 : : "typmodout::pg_catalog.oid AS typmodoutoid, "
12529 : : "typcategory, typispreferred, "
12530 : : "(typcollation <> 0) AS typcollatable, "
12531 : : "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
12532 : :
12533 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12534 : 42 : appendPQExpBufferStr(query,
12535 : : "typsubscript, "
12536 : : "typsubscript::pg_catalog.oid AS typsubscriptoid ");
12537 : : else
12538 : 0 : appendPQExpBufferStr(query,
12539 : : "'-' AS typsubscript, 0 AS typsubscriptoid ");
12540 : :
12541 : 42 : appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
12542 : : "WHERE oid = $1");
12543 : :
12544 : 42 : ExecuteSqlStatement(fout, query->data);
12545 : :
12546 : 42 : fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
12547 : : }
12548 : :
12549 : 285 : printfPQExpBuffer(query,
12550 : : "EXECUTE dumpBaseType('%u')",
12551 : 285 : tyinfo->dobj.catId.oid);
12552 : :
12553 : 285 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12554 : :
12555 : 285 : typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
12556 : 285 : typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
12557 : 285 : typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
12558 : 285 : typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
12559 : 285 : typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
12560 : 285 : typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
12561 : 285 : typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
12562 : 285 : typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
12563 : 285 : typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
12564 : 285 : typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
12565 : 285 : typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
12566 : 285 : typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
12567 : 285 : typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
12568 : 285 : typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
12569 : 285 : typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
12570 : 285 : typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
12571 : 285 : typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
12572 : 285 : typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
12573 : 285 : typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
12574 : 285 : typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
12575 : 285 : typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
12576 : 285 : typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
12577 [ - + ]: 285 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12578 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12579 [ + + ]: 285 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12580 : : {
12581 : 44 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12582 : 44 : typdefault_is_literal = true; /* it needs quotes */
12583 : : }
12584 : : else
12585 : 241 : typdefault = NULL;
12586 : :
12587 : 285 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12588 : 285 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12589 : :
12590 : : /*
12591 : : * The reason we include CASCADE is that the circular dependency between
12592 : : * the type and its I/O functions makes it impossible to drop the type any
12593 : : * other way.
12594 : : */
12595 : 285 : appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
12596 : :
12597 : : /*
12598 : : * We might already have a shell type, but setting pg_type_oid is
12599 : : * harmless, and in any case we'd better set the array type OID.
12600 : : */
12601 [ + + ]: 285 : if (dopt->binary_upgrade)
12602 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12603 : 8 : tyinfo->dobj.catId.oid,
12604 : : false, false);
12605 : :
12606 : 285 : appendPQExpBuffer(q,
12607 : : "CREATE TYPE %s (\n"
12608 : : " INTERNALLENGTH = %s",
12609 : : qualtypname,
12610 [ + + ]: 285 : (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
12611 : :
12612 : : /* regproc result is sufficiently quoted already */
12613 : 285 : appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
12614 : 285 : appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
12615 [ + + ]: 285 : if (OidIsValid(typreceiveoid))
12616 : 210 : appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
12617 [ + + ]: 285 : if (OidIsValid(typsendoid))
12618 : 210 : appendPQExpBuffer(q, ",\n SEND = %s", typsend);
12619 [ + + ]: 285 : if (OidIsValid(typmodinoid))
12620 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
12621 [ + + ]: 285 : if (OidIsValid(typmodoutoid))
12622 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
12623 [ + + ]: 285 : if (OidIsValid(typanalyzeoid))
12624 : 3 : appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
12625 : :
12626 [ + + ]: 285 : if (strcmp(typcollatable, "t") == 0)
12627 : 30 : appendPQExpBufferStr(q, ",\n COLLATABLE = true");
12628 : :
12629 [ + + ]: 285 : if (typdefault != NULL)
12630 : : {
12631 : 44 : appendPQExpBufferStr(q, ",\n DEFAULT = ");
12632 [ + - ]: 44 : if (typdefault_is_literal)
12633 : 44 : appendStringLiteralAH(q, typdefault, fout);
12634 : : else
12635 : 0 : appendPQExpBufferStr(q, typdefault);
12636 : : }
12637 : :
12638 [ + + ]: 285 : if (OidIsValid(typsubscriptoid))
12639 : 29 : appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
12640 : :
12641 [ + + ]: 285 : if (OidIsValid(tyinfo->typelem))
12642 : 26 : appendPQExpBuffer(q, ",\n ELEMENT = %s",
12643 : 26 : getFormattedTypeName(fout, tyinfo->typelem,
12644 : : zeroIsError));
12645 : :
12646 [ + + ]: 285 : if (strcmp(typcategory, "U") != 0)
12647 : : {
12648 : 161 : appendPQExpBufferStr(q, ",\n CATEGORY = ");
12649 : 161 : appendStringLiteralAH(q, typcategory, fout);
12650 : : }
12651 : :
12652 [ + + ]: 285 : if (strcmp(typispreferred, "t") == 0)
12653 : 29 : appendPQExpBufferStr(q, ",\n PREFERRED = true");
12654 : :
12655 [ + - + + ]: 285 : if (typdelim && strcmp(typdelim, ",") != 0)
12656 : : {
12657 : 3 : appendPQExpBufferStr(q, ",\n DELIMITER = ");
12658 : 3 : appendStringLiteralAH(q, typdelim, fout);
12659 : : }
12660 : :
12661 [ + + ]: 285 : if (*typalign == TYPALIGN_CHAR)
12662 : 12 : appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
12663 [ + + ]: 273 : else if (*typalign == TYPALIGN_SHORT)
12664 : 6 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
12665 [ + + ]: 267 : else if (*typalign == TYPALIGN_INT)
12666 : 189 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
12667 [ + - ]: 78 : else if (*typalign == TYPALIGN_DOUBLE)
12668 : 78 : appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
12669 : :
12670 [ + + ]: 285 : if (*typstorage == TYPSTORAGE_PLAIN)
12671 : 210 : appendPQExpBufferStr(q, ",\n STORAGE = plain");
12672 [ - + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTERNAL)
12673 : 0 : appendPQExpBufferStr(q, ",\n STORAGE = external");
12674 [ + + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTENDED)
12675 : 66 : appendPQExpBufferStr(q, ",\n STORAGE = extended");
12676 [ + - ]: 9 : else if (*typstorage == TYPSTORAGE_MAIN)
12677 : 9 : appendPQExpBufferStr(q, ",\n STORAGE = main");
12678 : :
12679 [ + + ]: 285 : if (strcmp(typbyval, "t") == 0)
12680 : 139 : appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
12681 : :
12682 : 285 : appendPQExpBufferStr(q, "\n);\n");
12683 : :
12684 [ + + ]: 285 : if (dopt->binary_upgrade)
12685 : 8 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12686 : : "TYPE", qtypname,
12687 : 8 : tyinfo->dobj.namespace->dobj.name);
12688 : :
12689 [ + - ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12690 : 285 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12691 : 285 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12692 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12693 : : .owner = tyinfo->rolname,
12694 : : .description = "TYPE",
12695 : : .section = SECTION_PRE_DATA,
12696 : : .createStmt = q->data,
12697 : : .dropStmt = delq->data));
12698 : :
12699 : : /* Dump Type Comments and Security Labels */
12700 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12701 : 250 : dumpComment(fout, "TYPE", qtypname,
12702 : 250 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12703 : 250 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12704 : :
12705 [ - + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12706 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12707 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12708 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12709 : :
12710 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12711 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12712 : : qtypname, NULL,
12713 : 34 : tyinfo->dobj.namespace->dobj.name,
12714 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12715 : :
12716 : 285 : PQclear(res);
12717 : 285 : destroyPQExpBuffer(q);
12718 : 285 : destroyPQExpBuffer(delq);
12719 : 285 : destroyPQExpBuffer(query);
12720 : 285 : pg_free(qtypname);
12721 : 285 : pg_free(qualtypname);
12722 : 285 : }
12723 : :
12724 : : /*
12725 : : * dumpDomain
12726 : : * writes out to fout the queries to recreate a user-defined domain
12727 : : */
12728 : : static void
12729 : 164 : dumpDomain(Archive *fout, const TypeInfo *tyinfo)
12730 : : {
12731 : 164 : DumpOptions *dopt = fout->dopt;
12732 : 164 : PQExpBuffer q = createPQExpBuffer();
12733 : 164 : PQExpBuffer delq = createPQExpBuffer();
12734 : 164 : PQExpBuffer query = createPQExpBuffer();
12735 : : PGresult *res;
12736 : : int i;
12737 : : char *qtypname;
12738 : : char *qualtypname;
12739 : : char *typnotnull;
12740 : : char *typdefn;
12741 : : char *typdefault;
12742 : : Oid typcollation;
12743 : 164 : bool typdefault_is_literal = false;
12744 : :
12745 [ + + ]: 164 : if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN])
12746 : : {
12747 : : /* Set up query for domain-specific details */
12748 : 39 : appendPQExpBufferStr(query,
12749 : : "PREPARE dumpDomain(pg_catalog.oid) AS\n");
12750 : :
12751 : 39 : appendPQExpBufferStr(query, "SELECT t.typnotnull, "
12752 : : "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
12753 : : "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
12754 : : "t.typdefault, "
12755 : : "CASE WHEN t.typcollation <> u.typcollation "
12756 : : "THEN t.typcollation ELSE 0 END AS typcollation "
12757 : : "FROM pg_catalog.pg_type t "
12758 : : "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
12759 : : "WHERE t.oid = $1");
12760 : :
12761 : 39 : ExecuteSqlStatement(fout, query->data);
12762 : :
12763 : 39 : fout->is_prepared[PREPQUERY_DUMPDOMAIN] = true;
12764 : : }
12765 : :
12766 : 164 : printfPQExpBuffer(query,
12767 : : "EXECUTE dumpDomain('%u')",
12768 : 164 : tyinfo->dobj.catId.oid);
12769 : :
12770 : 164 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12771 : :
12772 : 164 : typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
12773 : 164 : typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
12774 [ + + ]: 164 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12775 : 39 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12776 [ - + ]: 125 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12777 : : {
12778 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12779 : 0 : typdefault_is_literal = true; /* it needs quotes */
12780 : : }
12781 : : else
12782 : 125 : typdefault = NULL;
12783 : 164 : typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
12784 : :
12785 [ + + ]: 164 : if (dopt->binary_upgrade)
12786 : 27 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12787 : 27 : tyinfo->dobj.catId.oid,
12788 : : true, /* force array type */
12789 : : false); /* force multirange type */
12790 : :
12791 : 164 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12792 : 164 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12793 : :
12794 : 164 : appendPQExpBuffer(q,
12795 : : "CREATE DOMAIN %s AS %s",
12796 : : qualtypname,
12797 : : typdefn);
12798 : :
12799 : : /* Print collation only if different from base type's collation */
12800 [ + + ]: 164 : if (OidIsValid(typcollation))
12801 : : {
12802 : : CollInfo *coll;
12803 : :
12804 : 34 : coll = findCollationByOid(typcollation);
12805 [ + - ]: 34 : if (coll)
12806 : 34 : appendPQExpBuffer(q, " COLLATE %s", fmtQualifiedDumpable(coll));
12807 : : }
12808 : :
12809 : : /*
12810 : : * Print a not-null constraint if there's one. In servers older than 17
12811 : : * these don't have names, so just print it unadorned; in newer ones they
12812 : : * do, but most of the time it's going to be the standard generated one,
12813 : : * so omit the name in that case also.
12814 : : */
12815 [ + + ]: 164 : if (typnotnull[0] == 't')
12816 : : {
12817 [ + - - + ]: 49 : if (fout->remoteVersion < 170000 || tyinfo->notnull == NULL)
12818 : 0 : appendPQExpBufferStr(q, " NOT NULL");
12819 : : else
12820 : : {
12821 : 49 : ConstraintInfo *notnull = tyinfo->notnull;
12822 : :
12823 [ + - ]: 49 : if (!notnull->separate)
12824 : : {
12825 : : char *default_name;
12826 : :
12827 : : /* XXX should match ChooseConstraintName better */
12828 : 49 : default_name = psprintf("%s_not_null", tyinfo->dobj.name);
12829 : :
12830 [ + + ]: 49 : if (strcmp(default_name, notnull->dobj.name) == 0)
12831 : 15 : appendPQExpBufferStr(q, " NOT NULL");
12832 : : else
12833 : 34 : appendPQExpBuffer(q, " CONSTRAINT %s %s",
12834 : 34 : fmtId(notnull->dobj.name), notnull->condef);
12835 : 49 : pfree(default_name);
12836 : : }
12837 : : }
12838 : : }
12839 : :
12840 [ + + ]: 164 : if (typdefault != NULL)
12841 : : {
12842 : 39 : appendPQExpBufferStr(q, " DEFAULT ");
12843 [ - + ]: 39 : if (typdefault_is_literal)
12844 : 0 : appendStringLiteralAH(q, typdefault, fout);
12845 : : else
12846 : 39 : appendPQExpBufferStr(q, typdefault);
12847 : : }
12848 : :
12849 : 164 : PQclear(res);
12850 : :
12851 : : /*
12852 : : * Add any CHECK constraints for the domain
12853 : : */
12854 [ + + ]: 283 : for (i = 0; i < tyinfo->nDomChecks; i++)
12855 : : {
12856 : 119 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12857 : :
12858 [ + + + - ]: 119 : if (!domcheck->separate && domcheck->contype == 'c')
12859 : 114 : appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
12860 : 114 : fmtId(domcheck->dobj.name), domcheck->condef);
12861 : : }
12862 : :
12863 : 164 : appendPQExpBufferStr(q, ";\n");
12864 : :
12865 : 164 : appendPQExpBuffer(delq, "DROP DOMAIN %s;\n", qualtypname);
12866 : :
12867 [ + + ]: 164 : if (dopt->binary_upgrade)
12868 : 27 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12869 : : "DOMAIN", qtypname,
12870 : 27 : tyinfo->dobj.namespace->dobj.name);
12871 : :
12872 [ + - ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12873 : 164 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12874 : 164 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12875 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12876 : : .owner = tyinfo->rolname,
12877 : : .description = "DOMAIN",
12878 : : .section = SECTION_PRE_DATA,
12879 : : .createStmt = q->data,
12880 : : .dropStmt = delq->data));
12881 : :
12882 : : /* Dump Domain Comments and Security Labels */
12883 [ - + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12884 : 0 : dumpComment(fout, "DOMAIN", qtypname,
12885 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12886 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12887 : :
12888 [ - + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12889 : 0 : dumpSecLabel(fout, "DOMAIN", qtypname,
12890 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12891 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12892 : :
12893 [ + + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12894 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12895 : : qtypname, NULL,
12896 : 34 : tyinfo->dobj.namespace->dobj.name,
12897 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12898 : :
12899 : : /* Dump any per-constraint comments */
12900 [ + + ]: 283 : for (i = 0; i < tyinfo->nDomChecks; i++)
12901 : : {
12902 : 119 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12903 : : PQExpBuffer conprefix;
12904 : :
12905 : : /* but only if the constraint itself was dumped here */
12906 [ + + ]: 119 : if (domcheck->separate)
12907 : 5 : continue;
12908 : :
12909 : 114 : conprefix = createPQExpBuffer();
12910 : 114 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12911 : 114 : fmtId(domcheck->dobj.name));
12912 : :
12913 [ + + ]: 114 : if (domcheck->dobj.dump & DUMP_COMPONENT_COMMENT)
12914 : 34 : dumpComment(fout, conprefix->data, qtypname,
12915 : 34 : tyinfo->dobj.namespace->dobj.name,
12916 : 34 : tyinfo->rolname,
12917 : 34 : domcheck->dobj.catId, 0, tyinfo->dobj.dumpId);
12918 : :
12919 : 114 : destroyPQExpBuffer(conprefix);
12920 : : }
12921 : :
12922 : : /*
12923 : : * And a comment on the not-null constraint, if there's one -- but only if
12924 : : * the constraint itself was dumped here
12925 : : */
12926 [ + + + - ]: 164 : if (tyinfo->notnull != NULL && !tyinfo->notnull->separate)
12927 : : {
12928 : 49 : PQExpBuffer conprefix = createPQExpBuffer();
12929 : :
12930 : 49 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12931 : 49 : fmtId(tyinfo->notnull->dobj.name));
12932 : :
12933 [ + + ]: 49 : if (tyinfo->notnull->dobj.dump & DUMP_COMPONENT_COMMENT)
12934 : 34 : dumpComment(fout, conprefix->data, qtypname,
12935 : 34 : tyinfo->dobj.namespace->dobj.name,
12936 : 34 : tyinfo->rolname,
12937 : 34 : tyinfo->notnull->dobj.catId, 0, tyinfo->dobj.dumpId);
12938 : 49 : destroyPQExpBuffer(conprefix);
12939 : : }
12940 : :
12941 : 164 : destroyPQExpBuffer(q);
12942 : 164 : destroyPQExpBuffer(delq);
12943 : 164 : destroyPQExpBuffer(query);
12944 : 164 : pg_free(qtypname);
12945 : 164 : pg_free(qualtypname);
12946 : 164 : }
12947 : :
12948 : : /*
12949 : : * dumpCompositeType
12950 : : * writes out to fout the queries to recreate a user-defined stand-alone
12951 : : * composite type
12952 : : */
12953 : : static void
12954 : 177 : dumpCompositeType(Archive *fout, const TypeInfo *tyinfo)
12955 : : {
12956 : 177 : DumpOptions *dopt = fout->dopt;
12957 : 177 : PQExpBuffer q = createPQExpBuffer();
12958 : 177 : PQExpBuffer dropped = createPQExpBuffer();
12959 : 177 : PQExpBuffer delq = createPQExpBuffer();
12960 : 177 : PQExpBuffer query = createPQExpBuffer();
12961 : : PGresult *res;
12962 : : char *qtypname;
12963 : : char *qualtypname;
12964 : : int ntups;
12965 : : int i_attname;
12966 : : int i_atttypdefn;
12967 : : int i_attlen;
12968 : : int i_attalign;
12969 : : int i_attisdropped;
12970 : : int i_attcollation;
12971 : : int i_attnum;
12972 : : int i_attversion;
12973 : : int i;
12974 : : int actual_atts;
12975 : 177 : uint64 *attversions = NULL;
12976 : :
12977 [ + + ]: 177 : if (!fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE])
12978 : : {
12979 : : /*
12980 : : * Set up query for type-specific details.
12981 : : *
12982 : : * Since we only want to dump COLLATE clauses for attributes whose
12983 : : * collation is different from their type's default, we use a CASE
12984 : : * here to suppress uninteresting attcollations cheaply. atttypid
12985 : : * will be 0 for dropped columns; collation does not matter for those.
12986 : : */
12987 : 58 : appendPQExpBufferStr(query,
12988 : : "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
12989 : : "SELECT a.attname, a.attnum, "
12990 : : "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
12991 : : "a.attlen, a.attalign, a.attisdropped, "
12992 : : "CASE WHEN a.attcollation <> at.typcollation "
12993 : : "THEN a.attcollation ELSE 0 END AS attcollation, ");
12994 : :
12995 : : /*
12996 : : * See similar code in getTableAttrs().
12997 : : *
12998 : : * XXX: This query is non-trivial enough to make deduplicating with
12999 : : * getTableAttrs() worthwhile. But ISTM the proper way for that is to
13000 : : * deduplicate all of getTableAttrs() and dumpCompositeType().
13001 : : */
13002 [ + - ]: 58 : if (fout->remoteVersion >= 200000)
13003 : 58 : appendPQExpBufferStr(query,
13004 : : "a.attversion - '0/0' AS attversion ");
13005 : : else
13006 : 0 : appendPQExpBuffer(query,
13007 : : "(\n"
13008 : : " SELECT substr(o, %2$zu + 1)\n"
13009 : : " FROM unnest(a.attoptions) o\n"
13010 : : " WHERE substr(o, 1, %2$zu) = '%1$s=') AS attversion\n",
13011 : : ATTRIBUTE_VERSION_ATTOPTION,
13012 : : strlen(ATTRIBUTE_VERSION_ATTOPTION) + 1);
13013 : :
13014 : 58 : appendPQExpBufferStr(query,
13015 : : "FROM pg_catalog.pg_type ct "
13016 : : "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
13017 : : "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
13018 : : "WHERE ct.oid = $1 "
13019 : : "ORDER BY a.attnum");
13020 : :
13021 : 58 : ExecuteSqlStatement(fout, query->data);
13022 : :
13023 : 58 : fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE] = true;
13024 : : }
13025 : :
13026 : 177 : printfPQExpBuffer(query,
13027 : : "EXECUTE dumpCompositeType('%u')",
13028 : 177 : tyinfo->dobj.catId.oid);
13029 : :
13030 : 177 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
13031 : :
13032 : 177 : ntups = PQntuples(res);
13033 : :
13034 : 177 : i_attname = PQfnumber(res, "attname");
13035 : 177 : i_atttypdefn = PQfnumber(res, "atttypdefn");
13036 : 177 : i_attlen = PQfnumber(res, "attlen");
13037 : 177 : i_attalign = PQfnumber(res, "attalign");
13038 : 177 : i_attisdropped = PQfnumber(res, "attisdropped");
13039 : 177 : i_attcollation = PQfnumber(res, "attcollation");
13040 : 177 : i_attnum = PQfnumber(res, "attnum");
13041 : 177 : i_attversion = PQfnumber(res, "attversion");
13042 : :
13043 [ + + ]: 177 : if (dopt->binary_upgrade)
13044 : : {
13045 : : /*
13046 : : * Collect the attribute versions in a pass of their own, so that they
13047 : : * can be emitted before the CREATE TYPE, like in dumpTableSchema().
13048 : : */
13049 : 26 : attversions = pg_malloc0_array(uint64, ntups);
13050 [ + + ]: 86 : for (i = 0; i < ntups; i++)
13051 : : {
13052 : 60 : int attnum = atoi(PQgetvalue(res, i, i_attnum));
13053 : :
13054 [ + - + - : 120 : if (attnum >= 1 && attnum <= ntups &&
+ - ]
13055 : 60 : !PQgetisnull(res, i, i_attversion))
13056 : 60 : attversions[attnum - 1] = strtou64(PQgetvalue(res, i, i_attversion), NULL, 10);
13057 : : }
13058 : :
13059 : 26 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
13060 : 26 : tyinfo->dobj.catId.oid,
13061 : : false, false);
13062 : 26 : binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid);
13063 : 26 : binary_upgrade_set_next_attribute_versions(q, ntups, attversions);
13064 : : }
13065 : :
13066 : 177 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
13067 : 177 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
13068 : :
13069 : 177 : appendPQExpBuffer(q, "CREATE TYPE %s AS (",
13070 : : qualtypname);
13071 : :
13072 : 177 : actual_atts = 0;
13073 [ + + ]: 587 : for (i = 0; i < ntups; i++)
13074 : : {
13075 : : char *attname;
13076 : : char *atttypdefn;
13077 : : char *attlen;
13078 : : char *attalign;
13079 : : bool attisdropped;
13080 : : Oid attcollation;
13081 : :
13082 : 410 : attname = PQgetvalue(res, i, i_attname);
13083 : 410 : atttypdefn = PQgetvalue(res, i, i_atttypdefn);
13084 : 410 : attlen = PQgetvalue(res, i, i_attlen);
13085 : 410 : attalign = PQgetvalue(res, i, i_attalign);
13086 : 410 : attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
13087 : 410 : attcollation = atooid(PQgetvalue(res, i, i_attcollation));
13088 : :
13089 [ + + + + ]: 410 : if (attisdropped && !dopt->binary_upgrade)
13090 : 41 : continue;
13091 : :
13092 : : /* Format properly if not first attr */
13093 [ + + ]: 369 : if (actual_atts++ > 0)
13094 : 193 : appendPQExpBufferChar(q, ',');
13095 : 369 : appendPQExpBufferStr(q, "\n\t");
13096 : :
13097 [ + + ]: 369 : if (!attisdropped)
13098 : : {
13099 : 365 : appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
13100 : :
13101 : : /* Add collation if not default for the column type */
13102 [ - + ]: 365 : if (OidIsValid(attcollation))
13103 : : {
13104 : : CollInfo *coll;
13105 : :
13106 : 0 : coll = findCollationByOid(attcollation);
13107 [ # # ]: 0 : if (coll)
13108 : 0 : appendPQExpBuffer(q, " COLLATE %s",
13109 : 0 : fmtQualifiedDumpable(coll));
13110 : : }
13111 : : }
13112 : : else
13113 : : {
13114 : : /*
13115 : : * This is a dropped attribute and we're in binary_upgrade mode.
13116 : : * Insert a placeholder for it in the CREATE TYPE command, and set
13117 : : * length and alignment with direct UPDATE to the catalogs
13118 : : * afterwards. See similar code in dumpTableSchema().
13119 : : */
13120 : 4 : appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
13121 : :
13122 : : /* stash separately for insertion after the CREATE TYPE */
13123 : 4 : appendPQExpBufferStr(dropped,
13124 : : "\n-- For binary upgrade, recreate dropped column.\n");
13125 : 4 : appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
13126 : : "SET attlen = %s, "
13127 : : "attalign = '%s', attbyval = false\n"
13128 : : "WHERE attname = ", attlen, attalign);
13129 : 4 : appendStringLiteralAH(dropped, attname, fout);
13130 : 4 : appendPQExpBufferStr(dropped, "\n AND attrelid = ");
13131 : 4 : appendStringLiteralAH(dropped, qualtypname, fout);
13132 : 4 : appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
13133 : :
13134 : 4 : appendPQExpBuffer(dropped, "ALTER TYPE %s ",
13135 : : qualtypname);
13136 : 4 : appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
13137 : : fmtId(attname));
13138 : : }
13139 : : }
13140 : 177 : appendPQExpBufferStr(q, "\n);\n");
13141 : 177 : appendPQExpBufferStr(q, dropped->data);
13142 : :
13143 : 177 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
13144 : :
13145 [ + + ]: 177 : if (dopt->binary_upgrade)
13146 : 26 : binary_upgrade_extension_member(q, &tyinfo->dobj,
13147 : : "TYPE", qtypname,
13148 : 26 : tyinfo->dobj.namespace->dobj.name);
13149 : :
13150 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13151 : 160 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
13152 : 160 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
13153 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13154 : : .owner = tyinfo->rolname,
13155 : : .description = "TYPE",
13156 : : .section = SECTION_PRE_DATA,
13157 : : .createStmt = q->data,
13158 : : .dropStmt = delq->data));
13159 : :
13160 : :
13161 : : /* Dump Type Comments and Security Labels */
13162 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13163 : 34 : dumpComment(fout, "TYPE", qtypname,
13164 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13165 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13166 : :
13167 [ - + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13168 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
13169 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13170 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13171 : :
13172 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
13173 : 18 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
13174 : : qtypname, NULL,
13175 : 18 : tyinfo->dobj.namespace->dobj.name,
13176 : 18 : NULL, tyinfo->rolname, &tyinfo->dacl);
13177 : :
13178 : : /* Dump any per-column comments */
13179 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13180 : 34 : dumpCompositeTypeColComments(fout, tyinfo, res);
13181 : :
13182 : 177 : PQclear(res);
13183 : 177 : destroyPQExpBuffer(q);
13184 : 177 : destroyPQExpBuffer(dropped);
13185 [ + + ]: 177 : if (attversions != NULL)
13186 : 26 : pg_free(attversions);
13187 : 177 : destroyPQExpBuffer(delq);
13188 : 177 : destroyPQExpBuffer(query);
13189 : 177 : pg_free(qtypname);
13190 : 177 : pg_free(qualtypname);
13191 : 177 : }
13192 : :
13193 : : /*
13194 : : * dumpCompositeTypeColComments
13195 : : * writes out to fout the queries to recreate comments on the columns of
13196 : : * a user-defined stand-alone composite type.
13197 : : *
13198 : : * The caller has already made a query to collect the names and attnums
13199 : : * of the type's columns, so we just pass that result into here rather
13200 : : * than reading them again.
13201 : : */
13202 : : static void
13203 : 34 : dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
13204 : : PGresult *res)
13205 : : {
13206 : : CommentItem *comments;
13207 : : int ncomments;
13208 : : PQExpBuffer query;
13209 : : PQExpBuffer target;
13210 : : int i;
13211 : : int ntups;
13212 : : int i_attname;
13213 : : int i_attnum;
13214 : : int i_attisdropped;
13215 : :
13216 : : /* do nothing, if --no-comments is supplied */
13217 [ - + ]: 34 : if (fout->dopt->no_comments)
13218 : 0 : return;
13219 : :
13220 : : /* Search for comments associated with type's pg_class OID */
13221 : 34 : ncomments = findComments(RelationRelationId, tyinfo->typrelid,
13222 : : &comments);
13223 : :
13224 : : /* If no comments exist, we're done */
13225 [ - + ]: 34 : if (ncomments <= 0)
13226 : 0 : return;
13227 : :
13228 : : /* Build COMMENT ON statements */
13229 : 34 : query = createPQExpBuffer();
13230 : 34 : target = createPQExpBuffer();
13231 : :
13232 : 34 : ntups = PQntuples(res);
13233 : 34 : i_attnum = PQfnumber(res, "attnum");
13234 : 34 : i_attname = PQfnumber(res, "attname");
13235 : 34 : i_attisdropped = PQfnumber(res, "attisdropped");
13236 [ + + ]: 68 : while (ncomments > 0)
13237 : : {
13238 : : const char *attname;
13239 : :
13240 : 34 : attname = NULL;
13241 [ + - ]: 34 : for (i = 0; i < ntups; i++)
13242 : : {
13243 [ + - ]: 34 : if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
13244 [ + - ]: 34 : PQgetvalue(res, i, i_attisdropped)[0] != 't')
13245 : : {
13246 : 34 : attname = PQgetvalue(res, i, i_attname);
13247 : 34 : break;
13248 : : }
13249 : : }
13250 [ + - ]: 34 : if (attname) /* just in case we don't find it */
13251 : : {
13252 : 34 : const char *descr = comments->descr;
13253 : :
13254 : 34 : resetPQExpBuffer(target);
13255 : 34 : appendPQExpBuffer(target, "COLUMN %s.",
13256 : 34 : fmtId(tyinfo->dobj.name));
13257 : 34 : appendPQExpBufferStr(target, fmtId(attname));
13258 : :
13259 : 34 : resetPQExpBuffer(query);
13260 : 34 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
13261 : 34 : fmtQualifiedDumpable(tyinfo));
13262 : 34 : appendPQExpBuffer(query, "%s IS ", fmtId(attname));
13263 : 34 : appendStringLiteralAH(query, descr, fout);
13264 : 34 : appendPQExpBufferStr(query, ";\n");
13265 : :
13266 : 34 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
13267 : 34 : ARCHIVE_OPTS(.tag = target->data,
13268 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13269 : : .owner = tyinfo->rolname,
13270 : : .description = "COMMENT",
13271 : : .section = SECTION_NONE,
13272 : : .createStmt = query->data,
13273 : : .deps = &(tyinfo->dobj.dumpId),
13274 : : .nDeps = 1));
13275 : : }
13276 : :
13277 : 34 : comments++;
13278 : 34 : ncomments--;
13279 : : }
13280 : :
13281 : 34 : destroyPQExpBuffer(query);
13282 : 34 : destroyPQExpBuffer(target);
13283 : : }
13284 : :
13285 : : /*
13286 : : * dumpShellType
13287 : : * writes out to fout the queries to create a shell type
13288 : : *
13289 : : * We dump a shell definition in advance of the I/O functions for the type.
13290 : : */
13291 : : static void
13292 : 76 : dumpShellType(Archive *fout, const ShellTypeInfo *stinfo)
13293 : : {
13294 : 76 : DumpOptions *dopt = fout->dopt;
13295 : : PQExpBuffer q;
13296 : :
13297 : : /* Do nothing if not dumping schema */
13298 [ + + ]: 76 : if (!dopt->dumpSchema)
13299 : 7 : return;
13300 : :
13301 : 69 : q = createPQExpBuffer();
13302 : :
13303 : : /*
13304 : : * Note the lack of a DROP command for the shell type; any required DROP
13305 : : * is driven off the base type entry, instead. This interacts with
13306 : : * _printTocEntry()'s use of the presence of a DROP command to decide
13307 : : * whether an entry needs an ALTER OWNER command. We don't want to alter
13308 : : * the shell type's owner immediately on creation; that should happen only
13309 : : * after it's filled in, otherwise the backend complains.
13310 : : */
13311 : :
13312 [ + + ]: 69 : if (dopt->binary_upgrade)
13313 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
13314 : 8 : stinfo->baseType->dobj.catId.oid,
13315 : : false, false);
13316 : :
13317 : 69 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
13318 : 69 : fmtQualifiedDumpable(stinfo));
13319 : :
13320 [ + - ]: 69 : if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13321 : 69 : ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId,
13322 : 69 : ARCHIVE_OPTS(.tag = stinfo->dobj.name,
13323 : : .namespace = stinfo->dobj.namespace->dobj.name,
13324 : : .owner = stinfo->baseType->rolname,
13325 : : .description = "SHELL TYPE",
13326 : : .section = SECTION_PRE_DATA,
13327 : : .createStmt = q->data));
13328 : :
13329 : 69 : destroyPQExpBuffer(q);
13330 : : }
13331 : :
13332 : : /*
13333 : : * dumpProcLang
13334 : : * writes out to fout the queries to recreate a user-defined
13335 : : * procedural language
13336 : : */
13337 : : static void
13338 : 100 : dumpProcLang(Archive *fout, const ProcLangInfo *plang)
13339 : : {
13340 : 100 : DumpOptions *dopt = fout->dopt;
13341 : : PQExpBuffer defqry;
13342 : : PQExpBuffer delqry;
13343 : : bool useParams;
13344 : : char *qlanname;
13345 : : FuncInfo *funcInfo;
13346 : 100 : FuncInfo *inlineInfo = NULL;
13347 : 100 : FuncInfo *validatorInfo = NULL;
13348 : :
13349 : : /* Do nothing if not dumping schema */
13350 [ + + ]: 100 : if (!dopt->dumpSchema)
13351 : 14 : return;
13352 : :
13353 : : /*
13354 : : * Try to find the support function(s). It is not an error if we don't
13355 : : * find them --- if the functions are in the pg_catalog schema, as is
13356 : : * standard in 8.1 and up, then we won't have loaded them. (In this case
13357 : : * we will emit a parameterless CREATE LANGUAGE command, which will
13358 : : * require PL template knowledge in the backend to reload.)
13359 : : */
13360 : :
13361 : 86 : funcInfo = findFuncByOid(plang->lanplcallfoid);
13362 [ + + + + ]: 86 : if (funcInfo != NULL && !funcInfo->dobj.dump)
13363 : 2 : funcInfo = NULL; /* treat not-dumped same as not-found */
13364 : :
13365 [ + + ]: 86 : if (OidIsValid(plang->laninline))
13366 : : {
13367 : 53 : inlineInfo = findFuncByOid(plang->laninline);
13368 [ + + + - ]: 53 : if (inlineInfo != NULL && !inlineInfo->dobj.dump)
13369 : 1 : inlineInfo = NULL;
13370 : : }
13371 : :
13372 [ + + ]: 86 : if (OidIsValid(plang->lanvalidator))
13373 : : {
13374 : 53 : validatorInfo = findFuncByOid(plang->lanvalidator);
13375 [ + + + - ]: 53 : if (validatorInfo != NULL && !validatorInfo->dobj.dump)
13376 : 1 : validatorInfo = NULL;
13377 : : }
13378 : :
13379 : : /*
13380 : : * If the functions are dumpable then emit a complete CREATE LANGUAGE with
13381 : : * parameters. Otherwise, we'll write a parameterless command, which will
13382 : : * be interpreted as CREATE EXTENSION.
13383 : : */
13384 [ + - ]: 32 : useParams = (funcInfo != NULL &&
13385 [ + + + - : 150 : (inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
+ - ]
13386 [ + - ]: 32 : (validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
13387 : :
13388 : 86 : defqry = createPQExpBuffer();
13389 : 86 : delqry = createPQExpBuffer();
13390 : :
13391 : 86 : qlanname = pg_strdup(fmtId(plang->dobj.name));
13392 : :
13393 : 86 : appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
13394 : : qlanname);
13395 : :
13396 [ + + ]: 86 : if (useParams)
13397 : : {
13398 : 32 : appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
13399 [ - + ]: 32 : plang->lanpltrusted ? "TRUSTED " : "",
13400 : : qlanname);
13401 : 32 : appendPQExpBuffer(defqry, " HANDLER %s",
13402 : 32 : fmtQualifiedDumpable(funcInfo));
13403 [ - + ]: 32 : if (OidIsValid(plang->laninline))
13404 : 0 : appendPQExpBuffer(defqry, " INLINE %s",
13405 : 0 : fmtQualifiedDumpable(inlineInfo));
13406 [ - + ]: 32 : if (OidIsValid(plang->lanvalidator))
13407 : 0 : appendPQExpBuffer(defqry, " VALIDATOR %s",
13408 : 0 : fmtQualifiedDumpable(validatorInfo));
13409 : : }
13410 : : else
13411 : : {
13412 : : /*
13413 : : * If not dumping parameters, then use CREATE OR REPLACE so that the
13414 : : * command will not fail if the language is preinstalled in the target
13415 : : * database.
13416 : : *
13417 : : * Modern servers will interpret this as CREATE EXTENSION IF NOT
13418 : : * EXISTS; perhaps we should emit that instead? But it might just add
13419 : : * confusion.
13420 : : */
13421 : 54 : appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s",
13422 : : qlanname);
13423 : : }
13424 : 86 : appendPQExpBufferStr(defqry, ";\n");
13425 : :
13426 [ + + ]: 86 : if (dopt->binary_upgrade)
13427 : 2 : binary_upgrade_extension_member(defqry, &plang->dobj,
13428 : : "LANGUAGE", qlanname, NULL);
13429 : :
13430 [ + + ]: 86 : if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION)
13431 : 33 : ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
13432 : 33 : ARCHIVE_OPTS(.tag = plang->dobj.name,
13433 : : .owner = plang->lanowner,
13434 : : .description = "PROCEDURAL LANGUAGE",
13435 : : .section = SECTION_PRE_DATA,
13436 : : .createStmt = defqry->data,
13437 : : .dropStmt = delqry->data,
13438 : : ));
13439 : :
13440 : : /* Dump Proc Lang Comments and Security Labels */
13441 [ - + ]: 86 : if (plang->dobj.dump & DUMP_COMPONENT_COMMENT)
13442 : 0 : dumpComment(fout, "LANGUAGE", qlanname,
13443 : 0 : NULL, plang->lanowner,
13444 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13445 : :
13446 [ - + ]: 86 : if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL)
13447 : 0 : dumpSecLabel(fout, "LANGUAGE", qlanname,
13448 : 0 : NULL, plang->lanowner,
13449 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13450 : :
13451 [ + + + - ]: 86 : if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL)
13452 : 53 : dumpACL(fout, plang->dobj.dumpId, InvalidDumpId, "LANGUAGE",
13453 : : qlanname, NULL, NULL,
13454 : 53 : NULL, plang->lanowner, &plang->dacl);
13455 : :
13456 : 86 : pg_free(qlanname);
13457 : :
13458 : 86 : destroyPQExpBuffer(defqry);
13459 : 86 : destroyPQExpBuffer(delqry);
13460 : : }
13461 : :
13462 : : /*
13463 : : * format_function_arguments: generate function name and argument list
13464 : : *
13465 : : * This is used when we can rely on pg_get_function_arguments to format
13466 : : * the argument list. Note, however, that pg_get_function_arguments
13467 : : * does not special-case zero-argument aggregates.
13468 : : */
13469 : : static char *
13470 : 4218 : format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
13471 : : {
13472 : : PQExpBufferData fn;
13473 : :
13474 : 4218 : initPQExpBuffer(&fn);
13475 : 4218 : appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name));
13476 [ + + + + ]: 4218 : if (is_agg && finfo->nargs == 0)
13477 : 80 : appendPQExpBufferStr(&fn, "(*)");
13478 : : else
13479 : 4138 : appendPQExpBuffer(&fn, "(%s)", funcargs);
13480 : 4218 : return fn.data;
13481 : : }
13482 : :
13483 : : /*
13484 : : * format_function_signature: generate function name and argument list
13485 : : *
13486 : : * Only a minimal list of input argument types is generated; this is
13487 : : * sufficient to reference the function, but not to define it.
13488 : : *
13489 : : * If honor_quotes is false then the function name is never quoted.
13490 : : * This is appropriate for use in TOC tags, but not in SQL commands.
13491 : : */
13492 : : static char *
13493 : 2223 : format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
13494 : : {
13495 : : PQExpBufferData fn;
13496 : : int j;
13497 : :
13498 : 2223 : initPQExpBuffer(&fn);
13499 [ + + ]: 2223 : if (honor_quotes)
13500 : 401 : appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
13501 : : else
13502 : 1822 : appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
13503 [ + + ]: 4085 : for (j = 0; j < finfo->nargs; j++)
13504 : : {
13505 [ + + ]: 1862 : if (j > 0)
13506 : 437 : appendPQExpBufferStr(&fn, ", ");
13507 : :
13508 : 1862 : appendPQExpBufferStr(&fn,
13509 : 1862 : getFormattedTypeName(fout, finfo->argtypes[j],
13510 : : zeroIsError));
13511 : : }
13512 : 2223 : appendPQExpBufferChar(&fn, ')');
13513 : 2223 : return fn.data;
13514 : : }
13515 : :
13516 : :
13517 : : /*
13518 : : * dumpFunc:
13519 : : * dump out one function
13520 : : */
13521 : : static void
13522 : 1892 : dumpFunc(Archive *fout, const FuncInfo *finfo)
13523 : : {
13524 : 1892 : DumpOptions *dopt = fout->dopt;
13525 : : PQExpBuffer query;
13526 : : PQExpBuffer q;
13527 : : PQExpBuffer delqry;
13528 : : PQExpBuffer asPart;
13529 : : PGresult *res;
13530 : : char *funcsig; /* identity signature */
13531 : 1892 : char *funcfullsig = NULL; /* full signature */
13532 : : char *funcsig_tag;
13533 : : char *qual_funcsig;
13534 : : char *proretset;
13535 : : char *prosrc;
13536 : : char *probin;
13537 : : char *prosqlbody;
13538 : : char *funcargs;
13539 : : char *funciargs;
13540 : : char *funcresult;
13541 : : char *protrftypes;
13542 : : char *prokind;
13543 : : char *provolatile;
13544 : : char *proisstrict;
13545 : : char *prosecdef;
13546 : : char *proleakproof;
13547 : : char *proconfig;
13548 : : char *procost;
13549 : : char *prorows;
13550 : : char *prosupport;
13551 : : char *proparallel;
13552 : : char *lanname;
13553 : 1892 : char **configitems = NULL;
13554 : 1892 : int nconfigitems = 0;
13555 : : const char *keyword;
13556 : :
13557 : : /* Do nothing if not dumping schema */
13558 [ + + ]: 1892 : if (!dopt->dumpSchema)
13559 : 70 : return;
13560 : :
13561 : 1822 : query = createPQExpBuffer();
13562 : 1822 : q = createPQExpBuffer();
13563 : 1822 : delqry = createPQExpBuffer();
13564 : 1822 : asPart = createPQExpBuffer();
13565 : :
13566 [ + + ]: 1822 : if (!fout->is_prepared[PREPQUERY_DUMPFUNC])
13567 : : {
13568 : : /* Set up query for function-specific details */
13569 : 69 : appendPQExpBufferStr(query,
13570 : : "PREPARE dumpFunc(pg_catalog.oid) AS\n");
13571 : :
13572 : 69 : appendPQExpBufferStr(query,
13573 : : "SELECT\n"
13574 : : "proretset,\n"
13575 : : "prosrc,\n"
13576 : : "probin,\n"
13577 : : "provolatile,\n"
13578 : : "proisstrict,\n"
13579 : : "prosecdef,\n"
13580 : : "lanname,\n"
13581 : : "proconfig,\n"
13582 : : "procost,\n"
13583 : : "prorows,\n"
13584 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13585 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n"
13586 : : "pg_catalog.pg_get_function_result(p.oid) AS funcresult,\n"
13587 : : "proleakproof,\n");
13588 : :
13589 : 69 : appendPQExpBufferStr(query,
13590 : : "array_to_string(protrftypes, ' ') AS protrftypes,\n");
13591 : :
13592 : 69 : appendPQExpBufferStr(query,
13593 : : "proparallel,\n");
13594 : :
13595 [ + - ]: 69 : if (fout->remoteVersion >= 110000)
13596 : 69 : appendPQExpBufferStr(query,
13597 : : "prokind,\n");
13598 : : else
13599 : 0 : appendPQExpBufferStr(query,
13600 : : "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind,\n");
13601 : :
13602 [ + - ]: 69 : if (fout->remoteVersion >= 120000)
13603 : 69 : appendPQExpBufferStr(query,
13604 : : "prosupport,\n");
13605 : : else
13606 : 0 : appendPQExpBufferStr(query,
13607 : : "'-' AS prosupport,\n");
13608 : :
13609 [ + - ]: 69 : if (fout->remoteVersion >= 140000)
13610 : 69 : appendPQExpBufferStr(query,
13611 : : "pg_get_function_sqlbody(p.oid) AS prosqlbody\n");
13612 : : else
13613 : 0 : appendPQExpBufferStr(query,
13614 : : "NULL AS prosqlbody\n");
13615 : :
13616 : 69 : appendPQExpBufferStr(query,
13617 : : "FROM pg_catalog.pg_proc p, pg_catalog.pg_language l\n"
13618 : : "WHERE p.oid = $1 "
13619 : : "AND l.oid = p.prolang");
13620 : :
13621 : 69 : ExecuteSqlStatement(fout, query->data);
13622 : :
13623 : 69 : fout->is_prepared[PREPQUERY_DUMPFUNC] = true;
13624 : : }
13625 : :
13626 : 1822 : printfPQExpBuffer(query,
13627 : : "EXECUTE dumpFunc('%u')",
13628 : 1822 : finfo->dobj.catId.oid);
13629 : :
13630 : 1822 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
13631 : :
13632 : 1822 : proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
13633 [ + + ]: 1822 : if (PQgetisnull(res, 0, PQfnumber(res, "prosqlbody")))
13634 : : {
13635 : 1772 : prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
13636 : 1772 : probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
13637 : 1772 : prosqlbody = NULL;
13638 : : }
13639 : : else
13640 : : {
13641 : 50 : prosrc = NULL;
13642 : 50 : probin = NULL;
13643 : 50 : prosqlbody = PQgetvalue(res, 0, PQfnumber(res, "prosqlbody"));
13644 : : }
13645 : 1822 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13646 : 1822 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13647 : 1822 : funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult"));
13648 : 1822 : protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes"));
13649 : 1822 : prokind = PQgetvalue(res, 0, PQfnumber(res, "prokind"));
13650 : 1822 : provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
13651 : 1822 : proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
13652 : 1822 : prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
13653 : 1822 : proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof"));
13654 : 1822 : proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig"));
13655 : 1822 : procost = PQgetvalue(res, 0, PQfnumber(res, "procost"));
13656 : 1822 : prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows"));
13657 : 1822 : prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport"));
13658 : 1822 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13659 : 1822 : lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
13660 : :
13661 : : /*
13662 : : * See backend/commands/functioncmds.c for details of how the 'AS' clause
13663 : : * is used.
13664 : : */
13665 [ + + ]: 1822 : if (prosqlbody)
13666 : : {
13667 : 50 : appendPQExpBufferStr(asPart, prosqlbody);
13668 : : }
13669 [ + + ]: 1772 : else if (probin[0] != '\0')
13670 : : {
13671 : 180 : appendPQExpBufferStr(asPart, "AS ");
13672 : 180 : appendStringLiteralAH(asPart, probin, fout);
13673 [ + - ]: 180 : if (prosrc[0] != '\0')
13674 : : {
13675 : 180 : appendPQExpBufferStr(asPart, ", ");
13676 : :
13677 : : /*
13678 : : * where we have bin, use dollar quoting if allowed and src
13679 : : * contains quote or backslash; else use regular quoting.
13680 : : */
13681 [ + - ]: 180 : if (dopt->disable_dollar_quoting ||
13682 [ + - + - ]: 180 : (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL))
13683 : 180 : appendStringLiteralAH(asPart, prosrc, fout);
13684 : : else
13685 : 0 : appendStringLiteralDQ(asPart, prosrc, NULL);
13686 : : }
13687 : : }
13688 : : else
13689 : : {
13690 : 1592 : appendPQExpBufferStr(asPart, "AS ");
13691 : : /* with no bin, dollar quote src unconditionally if allowed */
13692 [ - + ]: 1592 : if (dopt->disable_dollar_quoting)
13693 : 0 : appendStringLiteralAH(asPart, prosrc, fout);
13694 : : else
13695 : 1592 : appendStringLiteralDQ(asPart, prosrc, NULL);
13696 : : }
13697 : :
13698 [ + + ]: 1822 : if (*proconfig)
13699 : : {
13700 [ - + ]: 15 : if (!parsePGArray(proconfig, &configitems, &nconfigitems))
13701 : 0 : pg_fatal("could not parse %s array", "proconfig");
13702 : : }
13703 : : else
13704 : : {
13705 : 1807 : configitems = NULL;
13706 : 1807 : nconfigitems = 0;
13707 : : }
13708 : :
13709 : 1822 : funcfullsig = format_function_arguments(finfo, funcargs, false);
13710 : 1822 : funcsig = format_function_arguments(finfo, funciargs, false);
13711 : :
13712 : 1822 : funcsig_tag = format_function_signature(fout, finfo, false);
13713 : :
13714 : 1822 : qual_funcsig = psprintf("%s.%s",
13715 : 1822 : fmtId(finfo->dobj.namespace->dobj.name),
13716 : : funcsig);
13717 : :
13718 [ + + ]: 1822 : if (prokind[0] == PROKIND_PROCEDURE)
13719 : 94 : keyword = "PROCEDURE";
13720 : : else
13721 : 1728 : keyword = "FUNCTION"; /* works for window functions too */
13722 : :
13723 : 1822 : appendPQExpBuffer(delqry, "DROP %s %s;\n",
13724 : : keyword, qual_funcsig);
13725 : :
13726 [ + - ]: 3644 : appendPQExpBuffer(q, "CREATE %s %s.%s",
13727 : : keyword,
13728 : 1822 : fmtId(finfo->dobj.namespace->dobj.name),
13729 : : funcfullsig ? funcfullsig :
13730 : : funcsig);
13731 : :
13732 [ + + ]: 1822 : if (prokind[0] == PROKIND_PROCEDURE)
13733 : : /* no result type to output */ ;
13734 [ + - ]: 1728 : else if (funcresult)
13735 : 1728 : appendPQExpBuffer(q, " RETURNS %s", funcresult);
13736 : : else
13737 : 0 : appendPQExpBuffer(q, " RETURNS %s%s",
13738 [ # # ]: 0 : (proretset[0] == 't') ? "SETOF " : "",
13739 : 0 : getFormattedTypeName(fout, finfo->prorettype,
13740 : : zeroIsError));
13741 : :
13742 : 1822 : appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname));
13743 : :
13744 [ + + ]: 1822 : if (*protrftypes)
13745 : : {
13746 : 5 : Oid *typeids = parseOidArray(protrftypes, -1);
13747 : :
13748 : 5 : appendPQExpBufferStr(q, " TRANSFORM ");
13749 [ + + ]: 10 : for (int i = 0; typeids[i]; i++)
13750 : : {
13751 [ - + ]: 5 : if (i != 0)
13752 : 0 : appendPQExpBufferStr(q, ", ");
13753 : 5 : appendPQExpBuffer(q, "FOR TYPE %s",
13754 : 5 : getFormattedTypeName(fout, typeids[i], zeroAsNone));
13755 : : }
13756 : :
13757 : 5 : pg_free(typeids);
13758 : : }
13759 : :
13760 [ + + ]: 1822 : if (prokind[0] == PROKIND_WINDOW)
13761 : 5 : appendPQExpBufferStr(q, " WINDOW");
13762 : :
13763 [ + + ]: 1822 : if (provolatile[0] != PROVOLATILE_VOLATILE)
13764 : : {
13765 [ + + ]: 355 : if (provolatile[0] == PROVOLATILE_IMMUTABLE)
13766 : 334 : appendPQExpBufferStr(q, " IMMUTABLE");
13767 [ + - ]: 21 : else if (provolatile[0] == PROVOLATILE_STABLE)
13768 : 21 : appendPQExpBufferStr(q, " STABLE");
13769 [ # # ]: 0 : else if (provolatile[0] != PROVOLATILE_VOLATILE)
13770 : 0 : pg_fatal("unrecognized provolatile value for function \"%s\"",
13771 : : finfo->dobj.name);
13772 : : }
13773 : :
13774 [ + + ]: 1822 : if (proisstrict[0] == 't')
13775 : 384 : appendPQExpBufferStr(q, " STRICT");
13776 : :
13777 [ - + ]: 1822 : if (prosecdef[0] == 't')
13778 : 0 : appendPQExpBufferStr(q, " SECURITY DEFINER");
13779 : :
13780 [ + + ]: 1822 : if (proleakproof[0] == 't')
13781 : 10 : appendPQExpBufferStr(q, " LEAKPROOF");
13782 : :
13783 : : /*
13784 : : * COST and ROWS are emitted only if present and not default, so as not to
13785 : : * break backwards-compatibility of the dump without need. Keep this code
13786 : : * in sync with the defaults in functioncmds.c.
13787 : : */
13788 [ + - ]: 1822 : if (strcmp(procost, "0") != 0)
13789 : : {
13790 [ + + + + ]: 1822 : if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0)
13791 : : {
13792 : : /* default cost is 1 */
13793 [ - + ]: 417 : if (strcmp(procost, "1") != 0)
13794 : 0 : appendPQExpBuffer(q, " COST %s", procost);
13795 : : }
13796 : : else
13797 : : {
13798 : : /* default cost is 100 */
13799 [ + + ]: 1405 : if (strcmp(procost, "100") != 0)
13800 : 6 : appendPQExpBuffer(q, " COST %s", procost);
13801 : : }
13802 : : }
13803 [ + + ]: 1822 : if (proretset[0] == 't' &&
13804 [ + - - + ]: 189 : strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0)
13805 : 0 : appendPQExpBuffer(q, " ROWS %s", prorows);
13806 : :
13807 [ + + ]: 1822 : if (strcmp(prosupport, "-") != 0)
13808 : : {
13809 : : /* We rely on regprocout to provide quoting and qualification */
13810 : 44 : appendPQExpBuffer(q, " SUPPORT %s", prosupport);
13811 : : }
13812 : :
13813 [ + + ]: 1822 : if (proparallel[0] != PROPARALLEL_UNSAFE)
13814 : : {
13815 [ + + ]: 120 : if (proparallel[0] == PROPARALLEL_SAFE)
13816 : 115 : appendPQExpBufferStr(q, " PARALLEL SAFE");
13817 [ + - ]: 5 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13818 : 5 : appendPQExpBufferStr(q, " PARALLEL RESTRICTED");
13819 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
13820 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
13821 : : finfo->dobj.name);
13822 : : }
13823 : :
13824 [ + + ]: 1862 : for (int i = 0; i < nconfigitems; i++)
13825 : : {
13826 : : /* we feel free to scribble on configitems[] here */
13827 : 40 : char *configitem = configitems[i];
13828 : : char *pos;
13829 : :
13830 : 40 : pos = strchr(configitem, '=');
13831 [ - + ]: 40 : if (pos == NULL)
13832 : 0 : continue;
13833 : 40 : *pos++ = '\0';
13834 : 40 : appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem));
13835 : :
13836 : : /*
13837 : : * Variables that are marked GUC_LIST_QUOTE were already fully quoted
13838 : : * by flatten_set_variable_args() before they were put into the
13839 : : * proconfig array. However, because the quoting rules used there
13840 : : * aren't exactly like SQL's, we have to break the list value apart
13841 : : * and then quote the elements as string literals. (The elements may
13842 : : * be double-quoted as-is, but we can't just feed them to the SQL
13843 : : * parser; it would do the wrong thing with elements that are
13844 : : * zero-length or longer than NAMEDATALEN.) Also, we need a special
13845 : : * case for empty lists.
13846 : : *
13847 : : * Variables that are not so marked should just be emitted as simple
13848 : : * string literals. If the variable is not known to
13849 : : * variable_is_guc_list_quote(), we'll do that; this makes it unsafe
13850 : : * to use GUC_LIST_QUOTE for extension variables.
13851 : : */
13852 [ + + ]: 40 : if (variable_is_guc_list_quote(configitem))
13853 : : {
13854 : : char **namelist;
13855 : : char **nameptr;
13856 : :
13857 : : /* Parse string into list of identifiers */
13858 : : /* this shouldn't fail really */
13859 [ + - ]: 15 : if (SplitGUCList(pos, ',', &namelist))
13860 : : {
13861 : : /* Special case: represent an empty list as NULL */
13862 [ + + ]: 15 : if (*namelist == NULL)
13863 : 5 : appendPQExpBufferStr(q, "NULL");
13864 [ + + ]: 40 : for (nameptr = namelist; *nameptr; nameptr++)
13865 : : {
13866 [ + + ]: 25 : if (nameptr != namelist)
13867 : 15 : appendPQExpBufferStr(q, ", ");
13868 : 25 : appendStringLiteralAH(q, *nameptr, fout);
13869 : : }
13870 : : }
13871 : 15 : pg_free(namelist);
13872 : : }
13873 : : else
13874 : 25 : appendStringLiteralAH(q, pos, fout);
13875 : : }
13876 : :
13877 : 1822 : appendPQExpBuffer(q, "\n %s;\n", asPart->data);
13878 : :
13879 : 1822 : append_depends_on_extension(fout, q, &finfo->dobj,
13880 : : "pg_catalog.pg_proc", keyword,
13881 : : qual_funcsig);
13882 : :
13883 [ + + ]: 1822 : if (dopt->binary_upgrade)
13884 : 303 : binary_upgrade_extension_member(q, &finfo->dobj,
13885 : : keyword, funcsig,
13886 : 303 : finfo->dobj.namespace->dobj.name);
13887 : :
13888 [ + + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13889 : 1722 : ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
13890 [ + + ]: 1722 : ARCHIVE_OPTS(.tag = funcsig_tag,
13891 : : .namespace = finfo->dobj.namespace->dobj.name,
13892 : : .owner = finfo->rolname,
13893 : : .description = keyword,
13894 : : .section = finfo->postponed_def ?
13895 : : SECTION_POST_DATA : SECTION_PRE_DATA,
13896 : : .createStmt = q->data,
13897 : : .dropStmt = delqry->data));
13898 : :
13899 : : /* Dump Function Comments and Security Labels */
13900 [ + + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13901 : 9 : dumpComment(fout, keyword, funcsig,
13902 : 9 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13903 : 9 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13904 : :
13905 [ - + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13906 : 0 : dumpSecLabel(fout, keyword, funcsig,
13907 : 0 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13908 : 0 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13909 : :
13910 [ + + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_ACL)
13911 : 104 : dumpACL(fout, finfo->dobj.dumpId, InvalidDumpId, keyword,
13912 : : funcsig, NULL,
13913 : 104 : finfo->dobj.namespace->dobj.name,
13914 : 104 : NULL, finfo->rolname, &finfo->dacl);
13915 : :
13916 : 1822 : PQclear(res);
13917 : :
13918 : 1822 : destroyPQExpBuffer(query);
13919 : 1822 : destroyPQExpBuffer(q);
13920 : 1822 : destroyPQExpBuffer(delqry);
13921 : 1822 : destroyPQExpBuffer(asPart);
13922 : 1822 : free(funcsig);
13923 : 1822 : free(funcfullsig);
13924 : 1822 : free(funcsig_tag);
13925 : 1822 : pfree(qual_funcsig);
13926 : 1822 : free(configitems);
13927 : : }
13928 : :
13929 : :
13930 : : /*
13931 : : * Dump a user-defined cast
13932 : : */
13933 : : static void
13934 : 69 : dumpCast(Archive *fout, const CastInfo *cast)
13935 : : {
13936 : 69 : DumpOptions *dopt = fout->dopt;
13937 : : PQExpBuffer defqry;
13938 : : PQExpBuffer delqry;
13939 : : PQExpBuffer labelq;
13940 : : PQExpBuffer castargs;
13941 : 69 : FuncInfo *funcInfo = NULL;
13942 : : const char *sourceType;
13943 : : const char *targetType;
13944 : :
13945 : : /* Do nothing if not dumping schema */
13946 [ + + ]: 69 : if (!dopt->dumpSchema)
13947 : 6 : return;
13948 : :
13949 : : /* Cannot dump if we don't have the cast function's info */
13950 [ + + ]: 63 : if (OidIsValid(cast->castfunc))
13951 : : {
13952 : 38 : funcInfo = findFuncByOid(cast->castfunc);
13953 [ - + ]: 38 : if (funcInfo == NULL)
13954 : 0 : pg_fatal("could not find function definition for function with OID %u",
13955 : : cast->castfunc);
13956 : : }
13957 : :
13958 : 63 : defqry = createPQExpBuffer();
13959 : 63 : delqry = createPQExpBuffer();
13960 : 63 : labelq = createPQExpBuffer();
13961 : 63 : castargs = createPQExpBuffer();
13962 : :
13963 : 63 : sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
13964 : 63 : targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
13965 : 63 : appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
13966 : : sourceType, targetType);
13967 : :
13968 : 63 : appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
13969 : : sourceType, targetType);
13970 : :
13971 [ + - + - ]: 63 : switch (cast->castmethod)
13972 : : {
13973 : 25 : case COERCION_METHOD_BINARY:
13974 : 25 : appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
13975 : 25 : break;
13976 : 0 : case COERCION_METHOD_INOUT:
13977 : 0 : appendPQExpBufferStr(defqry, "WITH INOUT");
13978 : 0 : break;
13979 : 38 : case COERCION_METHOD_FUNCTION:
13980 [ + - ]: 38 : if (funcInfo)
13981 : : {
13982 : 38 : char *fsig = format_function_signature(fout, funcInfo, true);
13983 : :
13984 : : /*
13985 : : * Always qualify the function name (format_function_signature
13986 : : * won't qualify it).
13987 : : */
13988 : 38 : appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
13989 : 38 : fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
13990 : 38 : free(fsig);
13991 : : }
13992 : : else
13993 : 0 : pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
13994 : 38 : break;
13995 : 0 : default:
13996 : 0 : pg_log_warning("bogus value in pg_cast.castmethod field");
13997 : : }
13998 : :
13999 [ + + ]: 63 : if (cast->castcontext == 'a')
14000 : 33 : appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
14001 [ + + ]: 30 : else if (cast->castcontext == 'i')
14002 : 10 : appendPQExpBufferStr(defqry, " AS IMPLICIT");
14003 : 63 : appendPQExpBufferStr(defqry, ";\n");
14004 : :
14005 : 63 : appendPQExpBuffer(labelq, "CAST (%s AS %s)",
14006 : : sourceType, targetType);
14007 : :
14008 : 63 : appendPQExpBuffer(castargs, "(%s AS %s)",
14009 : : sourceType, targetType);
14010 : :
14011 [ + + ]: 63 : if (dopt->binary_upgrade)
14012 : 7 : binary_upgrade_extension_member(defqry, &cast->dobj,
14013 : 7 : "CAST", castargs->data, NULL);
14014 : :
14015 [ + - ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
14016 : 63 : ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
14017 : 63 : ARCHIVE_OPTS(.tag = labelq->data,
14018 : : .description = "CAST",
14019 : : .section = SECTION_PRE_DATA,
14020 : : .createStmt = defqry->data,
14021 : : .dropStmt = delqry->data));
14022 : :
14023 : : /* Dump Cast Comments */
14024 [ - + ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
14025 : 0 : dumpComment(fout, "CAST", castargs->data,
14026 : : NULL, "",
14027 : 0 : cast->dobj.catId, 0, cast->dobj.dumpId);
14028 : :
14029 : 63 : destroyPQExpBuffer(defqry);
14030 : 63 : destroyPQExpBuffer(delqry);
14031 : 63 : destroyPQExpBuffer(labelq);
14032 : 63 : destroyPQExpBuffer(castargs);
14033 : : }
14034 : :
14035 : : /*
14036 : : * Dump a transform
14037 : : */
14038 : : static void
14039 : 44 : dumpTransform(Archive *fout, const TransformInfo *transform)
14040 : : {
14041 : 44 : DumpOptions *dopt = fout->dopt;
14042 : : PQExpBuffer defqry;
14043 : : PQExpBuffer delqry;
14044 : : PQExpBuffer labelq;
14045 : : PQExpBuffer transformargs;
14046 : 44 : FuncInfo *fromsqlFuncInfo = NULL;
14047 : 44 : FuncInfo *tosqlFuncInfo = NULL;
14048 : : char *lanname;
14049 : : const char *transformType;
14050 : :
14051 : : /* Do nothing if not dumping schema */
14052 [ + + ]: 44 : if (!dopt->dumpSchema)
14053 : 6 : return;
14054 : :
14055 : : /* Cannot dump if we don't have the transform functions' info */
14056 [ + - ]: 38 : if (OidIsValid(transform->trffromsql))
14057 : : {
14058 : 38 : fromsqlFuncInfo = findFuncByOid(transform->trffromsql);
14059 [ - + ]: 38 : if (fromsqlFuncInfo == NULL)
14060 : 0 : pg_fatal("could not find function definition for function with OID %u",
14061 : : transform->trffromsql);
14062 : : }
14063 [ + - ]: 38 : if (OidIsValid(transform->trftosql))
14064 : : {
14065 : 38 : tosqlFuncInfo = findFuncByOid(transform->trftosql);
14066 [ - + ]: 38 : if (tosqlFuncInfo == NULL)
14067 : 0 : pg_fatal("could not find function definition for function with OID %u",
14068 : : transform->trftosql);
14069 : : }
14070 : :
14071 : 38 : defqry = createPQExpBuffer();
14072 : 38 : delqry = createPQExpBuffer();
14073 : 38 : labelq = createPQExpBuffer();
14074 : 38 : transformargs = createPQExpBuffer();
14075 : :
14076 : 38 : lanname = get_language_name(fout, transform->trflang);
14077 : 38 : transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone);
14078 : :
14079 : 38 : appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n",
14080 : : transformType, lanname);
14081 : :
14082 : 38 : appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (",
14083 : : transformType, lanname);
14084 : :
14085 [ - + - - ]: 38 : if (!transform->trffromsql && !transform->trftosql)
14086 : 0 : pg_log_warning("bogus transform definition, at least one of trffromsql and trftosql should be nonzero");
14087 : :
14088 [ + - ]: 38 : if (transform->trffromsql)
14089 : : {
14090 [ + - ]: 38 : if (fromsqlFuncInfo)
14091 : : {
14092 : 38 : char *fsig = format_function_signature(fout, fromsqlFuncInfo, true);
14093 : :
14094 : : /*
14095 : : * Always qualify the function name (format_function_signature
14096 : : * won't qualify it).
14097 : : */
14098 : 38 : appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s",
14099 : 38 : fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig);
14100 : 38 : free(fsig);
14101 : : }
14102 : : else
14103 : 0 : pg_log_warning("bogus value in pg_transform.trffromsql field");
14104 : : }
14105 : :
14106 [ + - ]: 38 : if (transform->trftosql)
14107 : : {
14108 [ + - ]: 38 : if (transform->trffromsql)
14109 : 38 : appendPQExpBufferStr(defqry, ", ");
14110 : :
14111 [ + - ]: 38 : if (tosqlFuncInfo)
14112 : : {
14113 : 38 : char *fsig = format_function_signature(fout, tosqlFuncInfo, true);
14114 : :
14115 : : /*
14116 : : * Always qualify the function name (format_function_signature
14117 : : * won't qualify it).
14118 : : */
14119 : 38 : appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s",
14120 : 38 : fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig);
14121 : 38 : free(fsig);
14122 : : }
14123 : : else
14124 : 0 : pg_log_warning("bogus value in pg_transform.trftosql field");
14125 : : }
14126 : :
14127 : 38 : appendPQExpBufferStr(defqry, ");\n");
14128 : :
14129 : 38 : appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s",
14130 : : transformType, lanname);
14131 : :
14132 : 38 : appendPQExpBuffer(transformargs, "FOR %s LANGUAGE %s",
14133 : : transformType, lanname);
14134 : :
14135 [ + + ]: 38 : if (dopt->binary_upgrade)
14136 : 2 : binary_upgrade_extension_member(defqry, &transform->dobj,
14137 : 2 : "TRANSFORM", transformargs->data, NULL);
14138 : :
14139 [ + - ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION)
14140 : 38 : ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId,
14141 : 38 : ARCHIVE_OPTS(.tag = labelq->data,
14142 : : .description = "TRANSFORM",
14143 : : .section = SECTION_PRE_DATA,
14144 : : .createStmt = defqry->data,
14145 : : .dropStmt = delqry->data,
14146 : : .deps = transform->dobj.dependencies,
14147 : : .nDeps = transform->dobj.nDeps));
14148 : :
14149 : : /* Dump Transform Comments */
14150 [ - + ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_COMMENT)
14151 : 0 : dumpComment(fout, "TRANSFORM", transformargs->data,
14152 : : NULL, "",
14153 : 0 : transform->dobj.catId, 0, transform->dobj.dumpId);
14154 : :
14155 : 38 : free(lanname);
14156 : 38 : destroyPQExpBuffer(defqry);
14157 : 38 : destroyPQExpBuffer(delqry);
14158 : 38 : destroyPQExpBuffer(labelq);
14159 : 38 : destroyPQExpBuffer(transformargs);
14160 : : }
14161 : :
14162 : :
14163 : : /*
14164 : : * dumpOpr
14165 : : * write out a single operator definition
14166 : : */
14167 : : static void
14168 : 2525 : dumpOpr(Archive *fout, const OprInfo *oprinfo)
14169 : : {
14170 : 2525 : DumpOptions *dopt = fout->dopt;
14171 : : PQExpBuffer query;
14172 : : PQExpBuffer q;
14173 : : PQExpBuffer delq;
14174 : : PQExpBuffer oprid;
14175 : : PQExpBuffer details;
14176 : : PGresult *res;
14177 : : int i_oprkind;
14178 : : int i_oprcode;
14179 : : int i_oprleft;
14180 : : int i_oprright;
14181 : : int i_oprcom;
14182 : : int i_oprnegate;
14183 : : int i_oprrest;
14184 : : int i_oprjoin;
14185 : : int i_oprcanmerge;
14186 : : int i_oprcanhash;
14187 : : char *oprkind;
14188 : : char *oprcode;
14189 : : char *oprleft;
14190 : : char *oprright;
14191 : : char *oprcom;
14192 : : char *oprnegate;
14193 : : char *oprrest;
14194 : : char *oprjoin;
14195 : : char *oprcanmerge;
14196 : : char *oprcanhash;
14197 : : char *oprregproc;
14198 : : char *oprref;
14199 : :
14200 : : /* Do nothing if not dumping schema */
14201 [ + + ]: 2525 : if (!dopt->dumpSchema)
14202 : 7 : return;
14203 : :
14204 : : /*
14205 : : * some operators are invalid because they were the result of user
14206 : : * defining operators before commutators exist
14207 : : */
14208 [ + + ]: 2518 : if (!OidIsValid(oprinfo->oprcode))
14209 : 14 : return;
14210 : :
14211 : 2504 : query = createPQExpBuffer();
14212 : 2504 : q = createPQExpBuffer();
14213 : 2504 : delq = createPQExpBuffer();
14214 : 2504 : oprid = createPQExpBuffer();
14215 : 2504 : details = createPQExpBuffer();
14216 : :
14217 [ + + ]: 2504 : if (!fout->is_prepared[PREPQUERY_DUMPOPR])
14218 : : {
14219 : : /* Set up query for operator-specific details */
14220 : 42 : appendPQExpBufferStr(query,
14221 : : "PREPARE dumpOpr(pg_catalog.oid) AS\n"
14222 : : "SELECT oprkind, "
14223 : : "oprcode::pg_catalog.regprocedure, "
14224 : : "oprleft::pg_catalog.regtype, "
14225 : : "oprright::pg_catalog.regtype, "
14226 : : "oprcom, "
14227 : : "oprnegate, "
14228 : : "oprrest::pg_catalog.regprocedure, "
14229 : : "oprjoin::pg_catalog.regprocedure, "
14230 : : "oprcanmerge, oprcanhash "
14231 : : "FROM pg_catalog.pg_operator "
14232 : : "WHERE oid = $1");
14233 : :
14234 : 42 : ExecuteSqlStatement(fout, query->data);
14235 : :
14236 : 42 : fout->is_prepared[PREPQUERY_DUMPOPR] = true;
14237 : : }
14238 : :
14239 : 2504 : printfPQExpBuffer(query,
14240 : : "EXECUTE dumpOpr('%u')",
14241 : 2504 : oprinfo->dobj.catId.oid);
14242 : :
14243 : 2504 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14244 : :
14245 : 2504 : i_oprkind = PQfnumber(res, "oprkind");
14246 : 2504 : i_oprcode = PQfnumber(res, "oprcode");
14247 : 2504 : i_oprleft = PQfnumber(res, "oprleft");
14248 : 2504 : i_oprright = PQfnumber(res, "oprright");
14249 : 2504 : i_oprcom = PQfnumber(res, "oprcom");
14250 : 2504 : i_oprnegate = PQfnumber(res, "oprnegate");
14251 : 2504 : i_oprrest = PQfnumber(res, "oprrest");
14252 : 2504 : i_oprjoin = PQfnumber(res, "oprjoin");
14253 : 2504 : i_oprcanmerge = PQfnumber(res, "oprcanmerge");
14254 : 2504 : i_oprcanhash = PQfnumber(res, "oprcanhash");
14255 : :
14256 : 2504 : oprkind = PQgetvalue(res, 0, i_oprkind);
14257 : 2504 : oprcode = PQgetvalue(res, 0, i_oprcode);
14258 : 2504 : oprleft = PQgetvalue(res, 0, i_oprleft);
14259 : 2504 : oprright = PQgetvalue(res, 0, i_oprright);
14260 : 2504 : oprcom = PQgetvalue(res, 0, i_oprcom);
14261 : 2504 : oprnegate = PQgetvalue(res, 0, i_oprnegate);
14262 : 2504 : oprrest = PQgetvalue(res, 0, i_oprrest);
14263 : 2504 : oprjoin = PQgetvalue(res, 0, i_oprjoin);
14264 : 2504 : oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
14265 : 2504 : oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
14266 : :
14267 : : /* In PG14 upwards postfix operator support does not exist anymore. */
14268 [ - + ]: 2504 : if (strcmp(oprkind, "r") == 0)
14269 : 0 : pg_log_warning("postfix operators are not supported anymore (operator \"%s\")",
14270 : : oprcode);
14271 : :
14272 : 2504 : oprregproc = convertRegProcReference(oprcode);
14273 [ + - ]: 2504 : if (oprregproc)
14274 : : {
14275 : 2504 : appendPQExpBuffer(details, " FUNCTION = %s", oprregproc);
14276 : 2504 : free(oprregproc);
14277 : : }
14278 : :
14279 : 2504 : appendPQExpBuffer(oprid, "%s (",
14280 : 2504 : oprinfo->dobj.name);
14281 : :
14282 : : /*
14283 : : * right unary means there's a left arg and left unary means there's a
14284 : : * right arg. (Although the "r" case is dead code for PG14 and later,
14285 : : * continue to support it in case we're dumping from an old server.)
14286 : : */
14287 [ + - ]: 2504 : if (strcmp(oprkind, "r") == 0 ||
14288 [ + + ]: 2504 : strcmp(oprkind, "b") == 0)
14289 : : {
14290 : 2361 : appendPQExpBuffer(details, ",\n LEFTARG = %s", oprleft);
14291 : 2361 : appendPQExpBufferStr(oprid, oprleft);
14292 : : }
14293 : : else
14294 : 143 : appendPQExpBufferStr(oprid, "NONE");
14295 : :
14296 [ + + ]: 2504 : if (strcmp(oprkind, "l") == 0 ||
14297 [ + - ]: 2361 : strcmp(oprkind, "b") == 0)
14298 : : {
14299 : 2504 : appendPQExpBuffer(details, ",\n RIGHTARG = %s", oprright);
14300 : 2504 : appendPQExpBuffer(oprid, ", %s)", oprright);
14301 : : }
14302 : : else
14303 : 0 : appendPQExpBufferStr(oprid, ", NONE)");
14304 : :
14305 : 2504 : oprref = getFormattedOperatorName(oprcom);
14306 [ + + ]: 2504 : if (oprref)
14307 : : {
14308 : 1679 : appendPQExpBuffer(details, ",\n COMMUTATOR = %s", oprref);
14309 : 1679 : free(oprref);
14310 : : }
14311 : :
14312 : 2504 : oprref = getFormattedOperatorName(oprnegate);
14313 [ + + ]: 2504 : if (oprref)
14314 : : {
14315 : 1181 : appendPQExpBuffer(details, ",\n NEGATOR = %s", oprref);
14316 : 1181 : free(oprref);
14317 : : }
14318 : :
14319 [ + + ]: 2504 : if (strcmp(oprcanmerge, "t") == 0)
14320 : 188 : appendPQExpBufferStr(details, ",\n MERGES");
14321 : :
14322 [ + + ]: 2504 : if (strcmp(oprcanhash, "t") == 0)
14323 : 141 : appendPQExpBufferStr(details, ",\n HASHES");
14324 : :
14325 : 2504 : oprregproc = convertRegProcReference(oprrest);
14326 [ + + ]: 2504 : if (oprregproc)
14327 : : {
14328 : 1532 : appendPQExpBuffer(details, ",\n RESTRICT = %s", oprregproc);
14329 : 1532 : free(oprregproc);
14330 : : }
14331 : :
14332 : 2504 : oprregproc = convertRegProcReference(oprjoin);
14333 [ + + ]: 2504 : if (oprregproc)
14334 : : {
14335 : 1532 : appendPQExpBuffer(details, ",\n JOIN = %s", oprregproc);
14336 : 1532 : free(oprregproc);
14337 : : }
14338 : :
14339 : 2504 : appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
14340 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14341 : : oprid->data);
14342 : :
14343 : 2504 : appendPQExpBuffer(q, "CREATE OPERATOR %s.%s (\n%s\n);\n",
14344 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14345 : 2504 : oprinfo->dobj.name, details->data);
14346 : :
14347 [ + + ]: 2504 : if (dopt->binary_upgrade)
14348 : 12 : binary_upgrade_extension_member(q, &oprinfo->dobj,
14349 : 12 : "OPERATOR", oprid->data,
14350 : 12 : oprinfo->dobj.namespace->dobj.name);
14351 : :
14352 [ + - ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14353 : 2504 : ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
14354 : 2504 : ARCHIVE_OPTS(.tag = oprinfo->dobj.name,
14355 : : .namespace = oprinfo->dobj.namespace->dobj.name,
14356 : : .owner = oprinfo->rolname,
14357 : : .description = "OPERATOR",
14358 : : .section = SECTION_PRE_DATA,
14359 : : .createStmt = q->data,
14360 : : .dropStmt = delq->data));
14361 : :
14362 : : /* Dump Operator Comments */
14363 [ + + ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14364 : 2415 : dumpComment(fout, "OPERATOR", oprid->data,
14365 : 2415 : oprinfo->dobj.namespace->dobj.name, oprinfo->rolname,
14366 : 2415 : oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
14367 : :
14368 : 2504 : PQclear(res);
14369 : :
14370 : 2504 : destroyPQExpBuffer(query);
14371 : 2504 : destroyPQExpBuffer(q);
14372 : 2504 : destroyPQExpBuffer(delq);
14373 : 2504 : destroyPQExpBuffer(oprid);
14374 : 2504 : destroyPQExpBuffer(details);
14375 : : }
14376 : :
14377 : : /*
14378 : : * Convert a function reference obtained from pg_operator
14379 : : *
14380 : : * Returns allocated string of what to print, or NULL if function references
14381 : : * is InvalidOid. Returned string is expected to be free'd by the caller.
14382 : : *
14383 : : * The input is a REGPROCEDURE display; we have to strip the argument-types
14384 : : * part.
14385 : : */
14386 : : static char *
14387 : 7512 : convertRegProcReference(const char *proc)
14388 : : {
14389 : : char *name;
14390 : : char *paren;
14391 : : bool inquote;
14392 : :
14393 : : /* In all cases "-" means a null reference */
14394 [ + + ]: 7512 : if (strcmp(proc, "-") == 0)
14395 : 1944 : return NULL;
14396 : :
14397 : 5568 : name = pg_strdup(proc);
14398 : : /* find non-double-quoted left paren */
14399 : 5568 : inquote = false;
14400 [ + - ]: 67010 : for (paren = name; *paren; paren++)
14401 : : {
14402 [ + + + - ]: 67010 : if (*paren == '(' && !inquote)
14403 : : {
14404 : 5568 : *paren = '\0';
14405 : 5568 : break;
14406 : : }
14407 [ + + ]: 61442 : if (*paren == '"')
14408 : 50 : inquote = !inquote;
14409 : : }
14410 : 5568 : return name;
14411 : : }
14412 : :
14413 : : /*
14414 : : * getFormattedOperatorName - retrieve the operator name for the
14415 : : * given operator OID (presented in string form).
14416 : : *
14417 : : * Returns an allocated string, or NULL if the given OID is invalid.
14418 : : * Caller is responsible for free'ing result string.
14419 : : *
14420 : : * What we produce has the format "OPERATOR(schema.oprname)". This is only
14421 : : * useful in commands where the operator's argument types can be inferred from
14422 : : * context. We always schema-qualify the name, though. The predecessor to
14423 : : * this code tried to skip the schema qualification if possible, but that led
14424 : : * to wrong results in corner cases, such as if an operator and its negator
14425 : : * are in different schemas.
14426 : : */
14427 : : static char *
14428 : 5295 : getFormattedOperatorName(const char *oproid)
14429 : : {
14430 : : OprInfo *oprInfo;
14431 : :
14432 : : /* In all cases "0" means a null reference */
14433 [ + + ]: 5295 : if (strcmp(oproid, "0") == 0)
14434 : 2435 : return NULL;
14435 : :
14436 : 2860 : oprInfo = findOprByOid(atooid(oproid));
14437 [ - + ]: 2860 : if (oprInfo == NULL)
14438 : : {
14439 : 0 : pg_log_warning("could not find operator with OID %s",
14440 : : oproid);
14441 : 0 : return NULL;
14442 : : }
14443 : :
14444 : 2860 : return psprintf("OPERATOR(%s.%s)",
14445 : 2860 : fmtId(oprInfo->dobj.namespace->dobj.name),
14446 : : oprInfo->dobj.name);
14447 : : }
14448 : :
14449 : : /*
14450 : : * Convert a function OID obtained from pg_ts_parser or pg_ts_template
14451 : : *
14452 : : * It is sufficient to use REGPROC rather than REGPROCEDURE, since the
14453 : : * argument lists of these functions are predetermined. Note that the
14454 : : * caller should ensure we are in the proper schema, because the results
14455 : : * are search path dependent!
14456 : : */
14457 : : static char *
14458 : 215 : convertTSFunction(Archive *fout, Oid funcOid)
14459 : : {
14460 : : char *result;
14461 : : char query[128];
14462 : : PGresult *res;
14463 : :
14464 : 215 : snprintf(query, sizeof(query),
14465 : : "SELECT '%u'::pg_catalog.regproc", funcOid);
14466 : 215 : res = ExecuteSqlQueryForSingleRow(fout, query);
14467 : :
14468 : 215 : result = pg_strdup(PQgetvalue(res, 0, 0));
14469 : :
14470 : 215 : PQclear(res);
14471 : :
14472 : 215 : return result;
14473 : : }
14474 : :
14475 : : /*
14476 : : * dumpAccessMethod
14477 : : * write out a single access method definition
14478 : : */
14479 : : static void
14480 : 84 : dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo)
14481 : : {
14482 : 84 : DumpOptions *dopt = fout->dopt;
14483 : : PQExpBuffer q;
14484 : : PQExpBuffer delq;
14485 : : char *qamname;
14486 : :
14487 : : /* Do nothing if not dumping schema */
14488 [ + + ]: 84 : if (!dopt->dumpSchema)
14489 : 12 : return;
14490 : :
14491 : 72 : q = createPQExpBuffer();
14492 : 72 : delq = createPQExpBuffer();
14493 : :
14494 : 72 : qamname = pg_strdup(fmtId(aminfo->dobj.name));
14495 : :
14496 : 72 : appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
14497 : :
14498 [ + + - ]: 72 : switch (aminfo->amtype)
14499 : : {
14500 : 34 : case AMTYPE_INDEX:
14501 : 34 : appendPQExpBufferStr(q, "TYPE INDEX ");
14502 : 34 : break;
14503 : 38 : case AMTYPE_TABLE:
14504 : 38 : appendPQExpBufferStr(q, "TYPE TABLE ");
14505 : 38 : break;
14506 : 0 : default:
14507 : 0 : pg_log_warning("invalid type \"%c\" of access method \"%s\"",
14508 : : aminfo->amtype, qamname);
14509 : 0 : destroyPQExpBuffer(q);
14510 : 0 : destroyPQExpBuffer(delq);
14511 : 0 : pg_free(qamname);
14512 : 0 : return;
14513 : : }
14514 : :
14515 : 72 : appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
14516 : :
14517 : 72 : appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
14518 : : qamname);
14519 : :
14520 [ + + ]: 72 : if (dopt->binary_upgrade)
14521 : 4 : binary_upgrade_extension_member(q, &aminfo->dobj,
14522 : : "ACCESS METHOD", qamname, NULL);
14523 : :
14524 [ + - ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14525 : 72 : ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
14526 : 72 : ARCHIVE_OPTS(.tag = aminfo->dobj.name,
14527 : : .description = "ACCESS METHOD",
14528 : : .section = SECTION_PRE_DATA,
14529 : : .createStmt = q->data,
14530 : : .dropStmt = delq->data));
14531 : :
14532 : : /* Dump Access Method Comments */
14533 [ - + ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14534 : 0 : dumpComment(fout, "ACCESS METHOD", qamname,
14535 : : NULL, "",
14536 : 0 : aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
14537 : :
14538 : 72 : destroyPQExpBuffer(q);
14539 : 72 : destroyPQExpBuffer(delq);
14540 : 72 : pg_free(qamname);
14541 : : }
14542 : :
14543 : : /*
14544 : : * dumpOpclass
14545 : : * write out a single operator class definition
14546 : : */
14547 : : static void
14548 : 675 : dumpOpclass(Archive *fout, const OpclassInfo *opcinfo)
14549 : : {
14550 : 675 : DumpOptions *dopt = fout->dopt;
14551 : : PQExpBuffer query;
14552 : : PQExpBuffer q;
14553 : : PQExpBuffer delq;
14554 : : PQExpBuffer nameusing;
14555 : : PGresult *res;
14556 : : int ntups;
14557 : : int i_opcintype;
14558 : : int i_opckeytype;
14559 : : int i_opcdefault;
14560 : : int i_opcfamily;
14561 : : int i_opcfamilyname;
14562 : : int i_opcfamilynsp;
14563 : : int i_amname;
14564 : : int i_amopstrategy;
14565 : : int i_amopopr;
14566 : : int i_sortfamily;
14567 : : int i_sortfamilynsp;
14568 : : int i_amprocnum;
14569 : : int i_amproc;
14570 : : int i_amproclefttype;
14571 : : int i_amprocrighttype;
14572 : : char *opcintype;
14573 : : char *opckeytype;
14574 : : char *opcdefault;
14575 : : char *opcfamily;
14576 : : char *opcfamilyname;
14577 : : char *opcfamilynsp;
14578 : : char *amname;
14579 : : char *amopstrategy;
14580 : : char *amopopr;
14581 : : char *sortfamily;
14582 : : char *sortfamilynsp;
14583 : : char *amprocnum;
14584 : : char *amproc;
14585 : : char *amproclefttype;
14586 : : char *amprocrighttype;
14587 : : bool needComma;
14588 : : int i;
14589 : :
14590 : : /* Do nothing if not dumping schema */
14591 [ + + ]: 675 : if (!dopt->dumpSchema)
14592 : 21 : return;
14593 : :
14594 : 654 : query = createPQExpBuffer();
14595 : 654 : q = createPQExpBuffer();
14596 : 654 : delq = createPQExpBuffer();
14597 : 654 : nameusing = createPQExpBuffer();
14598 : :
14599 : : /* Get additional fields from the pg_opclass row */
14600 : 654 : appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
14601 : : "opckeytype::pg_catalog.regtype, "
14602 : : "opcdefault, opcfamily, "
14603 : : "opfname AS opcfamilyname, "
14604 : : "nspname AS opcfamilynsp, "
14605 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
14606 : : "FROM pg_catalog.pg_opclass c "
14607 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
14608 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14609 : : "WHERE c.oid = '%u'::pg_catalog.oid",
14610 : 654 : opcinfo->dobj.catId.oid);
14611 : :
14612 : 654 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14613 : :
14614 : 654 : i_opcintype = PQfnumber(res, "opcintype");
14615 : 654 : i_opckeytype = PQfnumber(res, "opckeytype");
14616 : 654 : i_opcdefault = PQfnumber(res, "opcdefault");
14617 : 654 : i_opcfamily = PQfnumber(res, "opcfamily");
14618 : 654 : i_opcfamilyname = PQfnumber(res, "opcfamilyname");
14619 : 654 : i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
14620 : 654 : i_amname = PQfnumber(res, "amname");
14621 : :
14622 : : /* opcintype may still be needed after we PQclear res */
14623 : 654 : opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
14624 : 654 : opckeytype = PQgetvalue(res, 0, i_opckeytype);
14625 : 654 : opcdefault = PQgetvalue(res, 0, i_opcdefault);
14626 : : /* opcfamily will still be needed after we PQclear res */
14627 : 654 : opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily));
14628 : 654 : opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname);
14629 : 654 : opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
14630 : : /* amname will still be needed after we PQclear res */
14631 : 654 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14632 : :
14633 : 654 : appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
14634 : 654 : fmtQualifiedDumpable(opcinfo));
14635 : 654 : appendPQExpBuffer(delq, " USING %s;\n",
14636 : : fmtId(amname));
14637 : :
14638 : : /* Build the fixed portion of the CREATE command */
14639 : 654 : appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ",
14640 : 654 : fmtQualifiedDumpable(opcinfo));
14641 [ + + ]: 654 : if (strcmp(opcdefault, "t") == 0)
14642 : 366 : appendPQExpBufferStr(q, "DEFAULT ");
14643 : 654 : appendPQExpBuffer(q, "FOR TYPE %s USING %s",
14644 : : opcintype,
14645 : : fmtId(amname));
14646 [ + - ]: 654 : if (strlen(opcfamilyname) > 0)
14647 : : {
14648 : 654 : appendPQExpBufferStr(q, " FAMILY ");
14649 : 654 : appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
14650 : 654 : appendPQExpBufferStr(q, fmtId(opcfamilyname));
14651 : : }
14652 : 654 : appendPQExpBufferStr(q, " AS\n ");
14653 : :
14654 : 654 : needComma = false;
14655 : :
14656 [ + + ]: 654 : if (strcmp(opckeytype, "-") != 0)
14657 : : {
14658 : 252 : appendPQExpBuffer(q, "STORAGE %s",
14659 : : opckeytype);
14660 : 252 : needComma = true;
14661 : : }
14662 : :
14663 : 654 : PQclear(res);
14664 : :
14665 : : /*
14666 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
14667 : : *
14668 : : * Print only those opfamily members that are tied to the opclass by
14669 : : * pg_depend entries.
14670 : : */
14671 : 654 : resetPQExpBuffer(query);
14672 : 654 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14673 : : "amopopr::pg_catalog.regoperator, "
14674 : : "opfname AS sortfamily, "
14675 : : "nspname AS sortfamilynsp "
14676 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14677 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14678 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14679 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14680 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14681 : : "AND refobjid = '%u'::pg_catalog.oid "
14682 : : "AND amopfamily = '%s'::pg_catalog.oid "
14683 : : "ORDER BY amopstrategy",
14684 : 654 : opcinfo->dobj.catId.oid,
14685 : : opcfamily);
14686 : :
14687 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14688 : :
14689 : 654 : ntups = PQntuples(res);
14690 : :
14691 : 654 : i_amopstrategy = PQfnumber(res, "amopstrategy");
14692 : 654 : i_amopopr = PQfnumber(res, "amopopr");
14693 : 654 : i_sortfamily = PQfnumber(res, "sortfamily");
14694 : 654 : i_sortfamilynsp = PQfnumber(res, "sortfamilynsp");
14695 : :
14696 [ + + ]: 868 : for (i = 0; i < ntups; i++)
14697 : : {
14698 : 214 : amopstrategy = PQgetvalue(res, i, i_amopstrategy);
14699 : 214 : amopopr = PQgetvalue(res, i, i_amopopr);
14700 : 214 : sortfamily = PQgetvalue(res, i, i_sortfamily);
14701 : 214 : sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp);
14702 : :
14703 [ + + ]: 214 : if (needComma)
14704 : 136 : appendPQExpBufferStr(q, " ,\n ");
14705 : :
14706 : 214 : appendPQExpBuffer(q, "OPERATOR %s %s",
14707 : : amopstrategy, amopopr);
14708 : :
14709 [ - + ]: 214 : if (strlen(sortfamily) > 0)
14710 : : {
14711 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
14712 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
14713 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
14714 : : }
14715 : :
14716 : 214 : needComma = true;
14717 : : }
14718 : :
14719 : 654 : PQclear(res);
14720 : :
14721 : : /*
14722 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
14723 : : *
14724 : : * Print only those opfamily members that are tied to the opclass by
14725 : : * pg_depend entries.
14726 : : *
14727 : : * We print the amproclefttype/amprocrighttype even though in most cases
14728 : : * the backend could deduce the right values, because of the corner case
14729 : : * of a btree sort support function for a cross-type comparison.
14730 : : */
14731 : 654 : resetPQExpBuffer(query);
14732 : :
14733 : 654 : appendPQExpBuffer(query, "SELECT amprocnum, "
14734 : : "amproc::pg_catalog.regprocedure, "
14735 : : "amproclefttype::pg_catalog.regtype, "
14736 : : "amprocrighttype::pg_catalog.regtype "
14737 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14738 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14739 : : "AND refobjid = '%u'::pg_catalog.oid "
14740 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14741 : : "AND objid = ap.oid "
14742 : : "ORDER BY amprocnum",
14743 : 654 : opcinfo->dobj.catId.oid);
14744 : :
14745 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14746 : :
14747 : 654 : ntups = PQntuples(res);
14748 : :
14749 : 654 : i_amprocnum = PQfnumber(res, "amprocnum");
14750 : 654 : i_amproc = PQfnumber(res, "amproc");
14751 : 654 : i_amproclefttype = PQfnumber(res, "amproclefttype");
14752 : 654 : i_amprocrighttype = PQfnumber(res, "amprocrighttype");
14753 : :
14754 [ + + ]: 688 : for (i = 0; i < ntups; i++)
14755 : : {
14756 : 34 : amprocnum = PQgetvalue(res, i, i_amprocnum);
14757 : 34 : amproc = PQgetvalue(res, i, i_amproc);
14758 : 34 : amproclefttype = PQgetvalue(res, i, i_amproclefttype);
14759 : 34 : amprocrighttype = PQgetvalue(res, i, i_amprocrighttype);
14760 : :
14761 [ + - ]: 34 : if (needComma)
14762 : 34 : appendPQExpBufferStr(q, " ,\n ");
14763 : :
14764 : 34 : appendPQExpBuffer(q, "FUNCTION %s", amprocnum);
14765 : :
14766 [ + - + - ]: 34 : if (*amproclefttype && *amprocrighttype)
14767 : 34 : appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype);
14768 : :
14769 : 34 : appendPQExpBuffer(q, " %s", amproc);
14770 : :
14771 : 34 : needComma = true;
14772 : : }
14773 : :
14774 : 654 : PQclear(res);
14775 : :
14776 : : /*
14777 : : * If needComma is still false it means we haven't added anything after
14778 : : * the AS keyword. To avoid printing broken SQL, append a dummy STORAGE
14779 : : * clause with the same datatype. This isn't sanctioned by the
14780 : : * documentation, but actually DefineOpClass will treat it as a no-op.
14781 : : */
14782 [ + + ]: 654 : if (!needComma)
14783 : 324 : appendPQExpBuffer(q, "STORAGE %s", opcintype);
14784 : :
14785 : 654 : appendPQExpBufferStr(q, ";\n");
14786 : :
14787 : 654 : appendPQExpBufferStr(nameusing, fmtId(opcinfo->dobj.name));
14788 : 654 : appendPQExpBuffer(nameusing, " USING %s",
14789 : : fmtId(amname));
14790 : :
14791 [ + + ]: 654 : if (dopt->binary_upgrade)
14792 : 6 : binary_upgrade_extension_member(q, &opcinfo->dobj,
14793 : 6 : "OPERATOR CLASS", nameusing->data,
14794 : 6 : opcinfo->dobj.namespace->dobj.name);
14795 : :
14796 [ + - ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14797 : 654 : ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
14798 : 654 : ARCHIVE_OPTS(.tag = opcinfo->dobj.name,
14799 : : .namespace = opcinfo->dobj.namespace->dobj.name,
14800 : : .owner = opcinfo->rolname,
14801 : : .description = "OPERATOR CLASS",
14802 : : .section = SECTION_PRE_DATA,
14803 : : .createStmt = q->data,
14804 : : .dropStmt = delq->data));
14805 : :
14806 : : /* Dump Operator Class Comments */
14807 [ - + ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14808 : 0 : dumpComment(fout, "OPERATOR CLASS", nameusing->data,
14809 : 0 : opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
14810 : 0 : opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
14811 : :
14812 : 654 : pg_free(opcintype);
14813 : 654 : pg_free(opcfamily);
14814 : 654 : pg_free(amname);
14815 : 654 : destroyPQExpBuffer(query);
14816 : 654 : destroyPQExpBuffer(q);
14817 : 654 : destroyPQExpBuffer(delq);
14818 : 654 : destroyPQExpBuffer(nameusing);
14819 : : }
14820 : :
14821 : : /*
14822 : : * dumpOpfamily
14823 : : * write out a single operator family definition
14824 : : *
14825 : : * Note: this also dumps any "loose" operator members that aren't bound to a
14826 : : * specific opclass within the opfamily.
14827 : : */
14828 : : static void
14829 : 561 : dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo)
14830 : : {
14831 : 561 : DumpOptions *dopt = fout->dopt;
14832 : : PQExpBuffer query;
14833 : : PQExpBuffer q;
14834 : : PQExpBuffer delq;
14835 : : PQExpBuffer nameusing;
14836 : : PGresult *res;
14837 : : PGresult *res_ops;
14838 : : PGresult *res_procs;
14839 : : int ntups;
14840 : : int i_amname;
14841 : : int i_amopstrategy;
14842 : : int i_amopopr;
14843 : : int i_sortfamily;
14844 : : int i_sortfamilynsp;
14845 : : int i_amprocnum;
14846 : : int i_amproc;
14847 : : int i_amproclefttype;
14848 : : int i_amprocrighttype;
14849 : : char *amname;
14850 : : char *amopstrategy;
14851 : : char *amopopr;
14852 : : char *sortfamily;
14853 : : char *sortfamilynsp;
14854 : : char *amprocnum;
14855 : : char *amproc;
14856 : : char *amproclefttype;
14857 : : char *amprocrighttype;
14858 : : bool needComma;
14859 : : int i;
14860 : :
14861 : : /* Do nothing if not dumping schema */
14862 [ + + ]: 561 : if (!dopt->dumpSchema)
14863 : 14 : return;
14864 : :
14865 : 547 : query = createPQExpBuffer();
14866 : 547 : q = createPQExpBuffer();
14867 : 547 : delq = createPQExpBuffer();
14868 : 547 : nameusing = createPQExpBuffer();
14869 : :
14870 : : /*
14871 : : * Fetch only those opfamily members that are tied directly to the
14872 : : * opfamily by pg_depend entries.
14873 : : */
14874 : 547 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14875 : : "amopopr::pg_catalog.regoperator, "
14876 : : "opfname AS sortfamily, "
14877 : : "nspname AS sortfamilynsp "
14878 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14879 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14880 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14881 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14882 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14883 : : "AND refobjid = '%u'::pg_catalog.oid "
14884 : : "AND amopfamily = '%u'::pg_catalog.oid "
14885 : : "ORDER BY amopstrategy",
14886 : 547 : opfinfo->dobj.catId.oid,
14887 : 547 : opfinfo->dobj.catId.oid);
14888 : :
14889 : 547 : res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14890 : :
14891 : 547 : resetPQExpBuffer(query);
14892 : :
14893 : 547 : appendPQExpBuffer(query, "SELECT amprocnum, "
14894 : : "amproc::pg_catalog.regprocedure, "
14895 : : "amproclefttype::pg_catalog.regtype, "
14896 : : "amprocrighttype::pg_catalog.regtype "
14897 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14898 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14899 : : "AND refobjid = '%u'::pg_catalog.oid "
14900 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14901 : : "AND objid = ap.oid "
14902 : : "ORDER BY amprocnum",
14903 : 547 : opfinfo->dobj.catId.oid);
14904 : :
14905 : 547 : res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14906 : :
14907 : : /* Get additional fields from the pg_opfamily row */
14908 : 547 : resetPQExpBuffer(query);
14909 : :
14910 : 547 : appendPQExpBuffer(query, "SELECT "
14911 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
14912 : : "FROM pg_catalog.pg_opfamily "
14913 : : "WHERE oid = '%u'::pg_catalog.oid",
14914 : 547 : opfinfo->dobj.catId.oid);
14915 : :
14916 : 547 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14917 : :
14918 : 547 : i_amname = PQfnumber(res, "amname");
14919 : :
14920 : : /* amname will still be needed after we PQclear res */
14921 : 547 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14922 : :
14923 : 547 : appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
14924 : 547 : fmtQualifiedDumpable(opfinfo));
14925 : 547 : appendPQExpBuffer(delq, " USING %s;\n",
14926 : : fmtId(amname));
14927 : :
14928 : : /* Build the fixed portion of the CREATE command */
14929 : 547 : appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
14930 : 547 : fmtQualifiedDumpable(opfinfo));
14931 : 547 : appendPQExpBuffer(q, " USING %s;\n",
14932 : : fmtId(amname));
14933 : :
14934 : 547 : PQclear(res);
14935 : :
14936 : : /* Do we need an ALTER to add loose members? */
14937 [ + + + + ]: 547 : if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
14938 : : {
14939 : 49 : appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
14940 : 49 : fmtQualifiedDumpable(opfinfo));
14941 : 49 : appendPQExpBuffer(q, " USING %s ADD\n ",
14942 : : fmtId(amname));
14943 : :
14944 : 49 : needComma = false;
14945 : :
14946 : : /*
14947 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
14948 : : */
14949 : 49 : ntups = PQntuples(res_ops);
14950 : :
14951 : 49 : i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
14952 : 49 : i_amopopr = PQfnumber(res_ops, "amopopr");
14953 : 49 : i_sortfamily = PQfnumber(res_ops, "sortfamily");
14954 : 49 : i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp");
14955 : :
14956 [ + + ]: 219 : for (i = 0; i < ntups; i++)
14957 : : {
14958 : 170 : amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
14959 : 170 : amopopr = PQgetvalue(res_ops, i, i_amopopr);
14960 : 170 : sortfamily = PQgetvalue(res_ops, i, i_sortfamily);
14961 : 170 : sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp);
14962 : :
14963 [ + + ]: 170 : if (needComma)
14964 : 136 : appendPQExpBufferStr(q, " ,\n ");
14965 : :
14966 : 170 : appendPQExpBuffer(q, "OPERATOR %s %s",
14967 : : amopstrategy, amopopr);
14968 : :
14969 [ - + ]: 170 : if (strlen(sortfamily) > 0)
14970 : : {
14971 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
14972 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
14973 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
14974 : : }
14975 : :
14976 : 170 : needComma = true;
14977 : : }
14978 : :
14979 : : /*
14980 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
14981 : : */
14982 : 49 : ntups = PQntuples(res_procs);
14983 : :
14984 : 49 : i_amprocnum = PQfnumber(res_procs, "amprocnum");
14985 : 49 : i_amproc = PQfnumber(res_procs, "amproc");
14986 : 49 : i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
14987 : 49 : i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
14988 : :
14989 [ + + ]: 234 : for (i = 0; i < ntups; i++)
14990 : : {
14991 : 185 : amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
14992 : 185 : amproc = PQgetvalue(res_procs, i, i_amproc);
14993 : 185 : amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
14994 : 185 : amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
14995 : :
14996 [ + + ]: 185 : if (needComma)
14997 : 170 : appendPQExpBufferStr(q, " ,\n ");
14998 : :
14999 : 185 : appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
15000 : : amprocnum, amproclefttype, amprocrighttype,
15001 : : amproc);
15002 : :
15003 : 185 : needComma = true;
15004 : : }
15005 : :
15006 : 49 : appendPQExpBufferStr(q, ";\n");
15007 : : }
15008 : :
15009 : 547 : appendPQExpBufferStr(nameusing, fmtId(opfinfo->dobj.name));
15010 : 547 : appendPQExpBuffer(nameusing, " USING %s",
15011 : : fmtId(amname));
15012 : :
15013 [ + + ]: 547 : if (dopt->binary_upgrade)
15014 : 9 : binary_upgrade_extension_member(q, &opfinfo->dobj,
15015 : 9 : "OPERATOR FAMILY", nameusing->data,
15016 : 9 : opfinfo->dobj.namespace->dobj.name);
15017 : :
15018 [ + - ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15019 : 547 : ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
15020 : 547 : ARCHIVE_OPTS(.tag = opfinfo->dobj.name,
15021 : : .namespace = opfinfo->dobj.namespace->dobj.name,
15022 : : .owner = opfinfo->rolname,
15023 : : .description = "OPERATOR FAMILY",
15024 : : .section = SECTION_PRE_DATA,
15025 : : .createStmt = q->data,
15026 : : .dropStmt = delq->data));
15027 : :
15028 : : /* Dump Operator Family Comments */
15029 [ - + ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15030 : 0 : dumpComment(fout, "OPERATOR FAMILY", nameusing->data,
15031 : 0 : opfinfo->dobj.namespace->dobj.name, opfinfo->rolname,
15032 : 0 : opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
15033 : :
15034 : 547 : pg_free(amname);
15035 : 547 : PQclear(res_ops);
15036 : 547 : PQclear(res_procs);
15037 : 547 : destroyPQExpBuffer(query);
15038 : 547 : destroyPQExpBuffer(q);
15039 : 547 : destroyPQExpBuffer(delq);
15040 : 547 : destroyPQExpBuffer(nameusing);
15041 : : }
15042 : :
15043 : : /*
15044 : : * dumpCollation
15045 : : * write out a single collation definition
15046 : : */
15047 : : static void
15048 : 2733 : dumpCollation(Archive *fout, const CollInfo *collinfo)
15049 : : {
15050 : 2733 : DumpOptions *dopt = fout->dopt;
15051 : : PQExpBuffer query;
15052 : : PQExpBuffer q;
15053 : : PQExpBuffer delq;
15054 : : char *qcollname;
15055 : : PGresult *res;
15056 : : int i_collprovider;
15057 : : int i_collisdeterministic;
15058 : : int i_collcollate;
15059 : : int i_collctype;
15060 : : int i_colllocale;
15061 : : int i_collicurules;
15062 : : const char *collprovider;
15063 : : const char *collcollate;
15064 : : const char *collctype;
15065 : : const char *colllocale;
15066 : : const char *collicurules;
15067 : :
15068 : : /* Do nothing if not dumping schema */
15069 [ + + ]: 2733 : if (!dopt->dumpSchema)
15070 : 12 : return;
15071 : :
15072 : 2721 : query = createPQExpBuffer();
15073 : 2721 : q = createPQExpBuffer();
15074 : 2721 : delq = createPQExpBuffer();
15075 : :
15076 : 2721 : qcollname = pg_strdup(fmtId(collinfo->dobj.name));
15077 : :
15078 : : /* Get collation-specific details */
15079 : 2721 : appendPQExpBufferStr(query, "SELECT ");
15080 : :
15081 : 2721 : appendPQExpBufferStr(query,
15082 : : "collprovider, "
15083 : : "collversion, ");
15084 : :
15085 [ + - ]: 2721 : if (fout->remoteVersion >= 120000)
15086 : 2721 : appendPQExpBufferStr(query,
15087 : : "collisdeterministic, ");
15088 : : else
15089 : 0 : appendPQExpBufferStr(query,
15090 : : "true AS collisdeterministic, ");
15091 : :
15092 [ + - ]: 2721 : if (fout->remoteVersion >= 170000)
15093 : 2721 : appendPQExpBufferStr(query,
15094 : : "colllocale, ");
15095 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
15096 : 0 : appendPQExpBufferStr(query,
15097 : : "colliculocale AS colllocale, ");
15098 : : else
15099 : 0 : appendPQExpBufferStr(query,
15100 : : "NULL AS colllocale, ");
15101 : :
15102 [ + - ]: 2721 : if (fout->remoteVersion >= 160000)
15103 : 2721 : appendPQExpBufferStr(query,
15104 : : "collicurules, ");
15105 : : else
15106 : 0 : appendPQExpBufferStr(query,
15107 : : "NULL AS collicurules, ");
15108 : :
15109 : 2721 : appendPQExpBuffer(query,
15110 : : "collcollate, "
15111 : : "collctype "
15112 : : "FROM pg_catalog.pg_collation c "
15113 : : "WHERE c.oid = '%u'::pg_catalog.oid",
15114 : 2721 : collinfo->dobj.catId.oid);
15115 : :
15116 : 2721 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15117 : :
15118 : 2721 : i_collprovider = PQfnumber(res, "collprovider");
15119 : 2721 : i_collisdeterministic = PQfnumber(res, "collisdeterministic");
15120 : 2721 : i_collcollate = PQfnumber(res, "collcollate");
15121 : 2721 : i_collctype = PQfnumber(res, "collctype");
15122 : 2721 : i_colllocale = PQfnumber(res, "colllocale");
15123 : 2721 : i_collicurules = PQfnumber(res, "collicurules");
15124 : :
15125 : 2721 : collprovider = PQgetvalue(res, 0, i_collprovider);
15126 : :
15127 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collcollate))
15128 : 48 : collcollate = PQgetvalue(res, 0, i_collcollate);
15129 : : else
15130 : 2673 : collcollate = NULL;
15131 : :
15132 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collctype))
15133 : 48 : collctype = PQgetvalue(res, 0, i_collctype);
15134 : : else
15135 : 2673 : collctype = NULL;
15136 : :
15137 : : /*
15138 : : * Before version 15, collcollate and collctype were of type NAME and
15139 : : * non-nullable. Treat empty strings as NULL for consistency.
15140 : : */
15141 [ - + ]: 2721 : if (fout->remoteVersion < 150000)
15142 : : {
15143 [ # # ]: 0 : if (collcollate[0] == '\0')
15144 : 0 : collcollate = NULL;
15145 [ # # ]: 0 : if (collctype[0] == '\0')
15146 : 0 : collctype = NULL;
15147 : : }
15148 : :
15149 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_colllocale))
15150 : 2670 : colllocale = PQgetvalue(res, 0, i_colllocale);
15151 : : else
15152 : 51 : colllocale = NULL;
15153 : :
15154 [ - + ]: 2721 : if (!PQgetisnull(res, 0, i_collicurules))
15155 : 0 : collicurules = PQgetvalue(res, 0, i_collicurules);
15156 : : else
15157 : 2721 : collicurules = NULL;
15158 : :
15159 : 2721 : appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
15160 : 2721 : fmtQualifiedDumpable(collinfo));
15161 : :
15162 : 2721 : appendPQExpBuffer(q, "CREATE COLLATION %s (",
15163 : 2721 : fmtQualifiedDumpable(collinfo));
15164 : :
15165 : 2721 : appendPQExpBufferStr(q, "provider = ");
15166 [ + + ]: 2721 : if (collprovider[0] == 'b')
15167 : 19 : appendPQExpBufferStr(q, "builtin");
15168 [ + + ]: 2702 : else if (collprovider[0] == 'c')
15169 : 48 : appendPQExpBufferStr(q, "libc");
15170 [ + + ]: 2654 : else if (collprovider[0] == 'i')
15171 : 2651 : appendPQExpBufferStr(q, "icu");
15172 [ + - ]: 3 : else if (collprovider[0] == 'd')
15173 : : /* to allow dumping pg_catalog; not accepted on input */
15174 : 3 : appendPQExpBufferStr(q, "default");
15175 : : else
15176 : 0 : pg_fatal("unrecognized collation provider: %s",
15177 : : collprovider);
15178 : :
15179 [ - + ]: 2721 : if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
15180 : 0 : appendPQExpBufferStr(q, ", deterministic = false");
15181 : :
15182 [ + + ]: 2721 : if (collprovider[0] == 'd')
15183 : : {
15184 [ + - + - : 3 : if (collcollate || collctype || colllocale || collicurules)
+ - - + ]
15185 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15186 : :
15187 : : /* no locale -- the default collation cannot be reloaded anyway */
15188 : : }
15189 [ + + ]: 2718 : else if (collprovider[0] == 'b')
15190 : : {
15191 [ + - + - : 19 : if (collcollate || collctype || !colllocale || collicurules)
+ - - + ]
15192 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15193 : :
15194 : 19 : appendPQExpBufferStr(q, ", locale = ");
15195 [ + - ]: 19 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15196 : : fout);
15197 : : }
15198 [ + + ]: 2699 : else if (collprovider[0] == 'i')
15199 : : {
15200 [ + - ]: 2651 : if (fout->remoteVersion >= 150000)
15201 : : {
15202 [ + - + - : 2651 : if (collcollate || collctype || !colllocale)
- + ]
15203 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15204 : :
15205 : 2651 : appendPQExpBufferStr(q, ", locale = ");
15206 [ + - ]: 2651 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15207 : : fout);
15208 : : }
15209 : : else
15210 : : {
15211 [ # # # # : 0 : if (!collcollate || !collctype || colllocale ||
# # ]
15212 [ # # ]: 0 : strcmp(collcollate, collctype) != 0)
15213 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15214 : :
15215 : 0 : appendPQExpBufferStr(q, ", locale = ");
15216 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15217 : : }
15218 : :
15219 [ - + ]: 2651 : if (collicurules)
15220 : : {
15221 : 0 : appendPQExpBufferStr(q, ", rules = ");
15222 [ # # ]: 0 : appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
15223 : : }
15224 : : }
15225 [ + - ]: 48 : else if (collprovider[0] == 'c')
15226 : : {
15227 [ + - + - : 48 : if (colllocale || collicurules || !collcollate || !collctype)
+ - - + ]
15228 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15229 : :
15230 [ + - + - : 48 : if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
+ - ]
15231 : : {
15232 : 48 : appendPQExpBufferStr(q, ", locale = ");
15233 [ + - ]: 48 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15234 : : }
15235 : : else
15236 : : {
15237 : 0 : appendPQExpBufferStr(q, ", lc_collate = ");
15238 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15239 : 0 : appendPQExpBufferStr(q, ", lc_ctype = ");
15240 [ # # ]: 0 : appendStringLiteralAH(q, collctype ? collctype : "", fout);
15241 : : }
15242 : : }
15243 : : else
15244 : 0 : pg_fatal("unrecognized collation provider: %s", collprovider);
15245 : :
15246 : : /*
15247 : : * For binary upgrade, carry over the collation version. For normal
15248 : : * dump/restore, omit the version, so that it is computed upon restore.
15249 : : */
15250 [ + + ]: 2721 : if (dopt->binary_upgrade)
15251 : : {
15252 : : int i_collversion;
15253 : :
15254 : 5 : i_collversion = PQfnumber(res, "collversion");
15255 [ + + ]: 5 : if (!PQgetisnull(res, 0, i_collversion))
15256 : : {
15257 : 4 : appendPQExpBufferStr(q, ", version = ");
15258 : 4 : appendStringLiteralAH(q,
15259 : : PQgetvalue(res, 0, i_collversion),
15260 : : fout);
15261 : : }
15262 : : }
15263 : :
15264 : 2721 : appendPQExpBufferStr(q, ");\n");
15265 : :
15266 [ + + ]: 2721 : if (dopt->binary_upgrade)
15267 : 5 : binary_upgrade_extension_member(q, &collinfo->dobj,
15268 : : "COLLATION", qcollname,
15269 : 5 : collinfo->dobj.namespace->dobj.name);
15270 : :
15271 [ + - ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15272 : 2721 : ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
15273 : 2721 : ARCHIVE_OPTS(.tag = collinfo->dobj.name,
15274 : : .namespace = collinfo->dobj.namespace->dobj.name,
15275 : : .owner = collinfo->rolname,
15276 : : .description = "COLLATION",
15277 : : .section = SECTION_PRE_DATA,
15278 : : .createStmt = q->data,
15279 : : .dropStmt = delq->data));
15280 : :
15281 : : /* Dump Collation Comments */
15282 [ + + ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15283 : 2613 : dumpComment(fout, "COLLATION", qcollname,
15284 : 2613 : collinfo->dobj.namespace->dobj.name, collinfo->rolname,
15285 : 2613 : collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
15286 : :
15287 : 2721 : PQclear(res);
15288 : :
15289 : 2721 : destroyPQExpBuffer(query);
15290 : 2721 : destroyPQExpBuffer(q);
15291 : 2721 : destroyPQExpBuffer(delq);
15292 : 2721 : pg_free(qcollname);
15293 : : }
15294 : :
15295 : : /*
15296 : : * dumpConversion
15297 : : * write out a single conversion definition
15298 : : */
15299 : : static void
15300 : 335 : dumpConversion(Archive *fout, const ConvInfo *convinfo)
15301 : : {
15302 : 335 : DumpOptions *dopt = fout->dopt;
15303 : : PQExpBuffer query;
15304 : : PQExpBuffer q;
15305 : : PQExpBuffer delq;
15306 : : char *qconvname;
15307 : : PGresult *res;
15308 : : int i_conforencoding;
15309 : : int i_contoencoding;
15310 : : int i_conproc;
15311 : : int i_condefault;
15312 : : const char *conforencoding;
15313 : : const char *contoencoding;
15314 : : const char *conproc;
15315 : : bool condefault;
15316 : :
15317 : : /* Do nothing if not dumping schema */
15318 [ + + ]: 335 : if (!dopt->dumpSchema)
15319 : 7 : return;
15320 : :
15321 : 328 : query = createPQExpBuffer();
15322 : 328 : q = createPQExpBuffer();
15323 : 328 : delq = createPQExpBuffer();
15324 : :
15325 : 328 : qconvname = pg_strdup(fmtId(convinfo->dobj.name));
15326 : :
15327 : : /* Get conversion-specific details */
15328 : 328 : appendPQExpBuffer(query, "SELECT "
15329 : : "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
15330 : : "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
15331 : : "conproc, condefault "
15332 : : "FROM pg_catalog.pg_conversion c "
15333 : : "WHERE c.oid = '%u'::pg_catalog.oid",
15334 : 328 : convinfo->dobj.catId.oid);
15335 : :
15336 : 328 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15337 : :
15338 : 328 : i_conforencoding = PQfnumber(res, "conforencoding");
15339 : 328 : i_contoencoding = PQfnumber(res, "contoencoding");
15340 : 328 : i_conproc = PQfnumber(res, "conproc");
15341 : 328 : i_condefault = PQfnumber(res, "condefault");
15342 : :
15343 : 328 : conforencoding = PQgetvalue(res, 0, i_conforencoding);
15344 : 328 : contoencoding = PQgetvalue(res, 0, i_contoencoding);
15345 : 328 : conproc = PQgetvalue(res, 0, i_conproc);
15346 : 328 : condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
15347 : :
15348 : 328 : appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
15349 : 328 : fmtQualifiedDumpable(convinfo));
15350 : :
15351 [ + - ]: 328 : appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
15352 : : (condefault) ? "DEFAULT " : "",
15353 : 328 : fmtQualifiedDumpable(convinfo));
15354 : 328 : appendStringLiteralAH(q, conforencoding, fout);
15355 : 328 : appendPQExpBufferStr(q, " TO ");
15356 : 328 : appendStringLiteralAH(q, contoencoding, fout);
15357 : : /* regproc output is already sufficiently quoted */
15358 : 328 : appendPQExpBuffer(q, " FROM %s;\n", conproc);
15359 : :
15360 [ + + ]: 328 : if (dopt->binary_upgrade)
15361 : 1 : binary_upgrade_extension_member(q, &convinfo->dobj,
15362 : : "CONVERSION", qconvname,
15363 : 1 : convinfo->dobj.namespace->dobj.name);
15364 : :
15365 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15366 : 328 : ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
15367 : 328 : ARCHIVE_OPTS(.tag = convinfo->dobj.name,
15368 : : .namespace = convinfo->dobj.namespace->dobj.name,
15369 : : .owner = convinfo->rolname,
15370 : : .description = "CONVERSION",
15371 : : .section = SECTION_PRE_DATA,
15372 : : .createStmt = q->data,
15373 : : .dropStmt = delq->data));
15374 : :
15375 : : /* Dump Conversion Comments */
15376 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15377 : 328 : dumpComment(fout, "CONVERSION", qconvname,
15378 : 328 : convinfo->dobj.namespace->dobj.name, convinfo->rolname,
15379 : 328 : convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
15380 : :
15381 : 328 : PQclear(res);
15382 : :
15383 : 328 : destroyPQExpBuffer(query);
15384 : 328 : destroyPQExpBuffer(q);
15385 : 328 : destroyPQExpBuffer(delq);
15386 : 328 : pg_free(qconvname);
15387 : : }
15388 : :
15389 : : /*
15390 : : * format_aggregate_signature: generate aggregate name and argument list
15391 : : *
15392 : : * The argument type names are qualified if needed. The aggregate name
15393 : : * is never qualified.
15394 : : */
15395 : : static char *
15396 : 287 : format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
15397 : : {
15398 : : PQExpBufferData buf;
15399 : : int j;
15400 : :
15401 : 287 : initPQExpBuffer(&buf);
15402 [ - + ]: 287 : if (honor_quotes)
15403 : 0 : appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name));
15404 : : else
15405 : 287 : appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name);
15406 : :
15407 [ + + ]: 287 : if (agginfo->aggfn.nargs == 0)
15408 : 40 : appendPQExpBufferStr(&buf, "(*)");
15409 : : else
15410 : : {
15411 : 247 : appendPQExpBufferChar(&buf, '(');
15412 [ + + ]: 539 : for (j = 0; j < agginfo->aggfn.nargs; j++)
15413 [ + + ]: 292 : appendPQExpBuffer(&buf, "%s%s",
15414 : : (j > 0) ? ", " : "",
15415 : : getFormattedTypeName(fout,
15416 : 292 : agginfo->aggfn.argtypes[j],
15417 : : zeroIsError));
15418 : 247 : appendPQExpBufferChar(&buf, ')');
15419 : : }
15420 : 287 : return buf.data;
15421 : : }
15422 : :
15423 : : /*
15424 : : * dumpAgg
15425 : : * write out a single aggregate definition
15426 : : */
15427 : : static void
15428 : 295 : dumpAgg(Archive *fout, const AggInfo *agginfo)
15429 : : {
15430 : 295 : DumpOptions *dopt = fout->dopt;
15431 : : PQExpBuffer query;
15432 : : PQExpBuffer q;
15433 : : PQExpBuffer delq;
15434 : : PQExpBuffer details;
15435 : : char *aggsig; /* identity signature */
15436 : 295 : char *aggfullsig = NULL; /* full signature */
15437 : : char *aggsig_tag;
15438 : : PGresult *res;
15439 : : int i_agginitval;
15440 : : int i_aggminitval;
15441 : : const char *aggtransfn;
15442 : : const char *aggfinalfn;
15443 : : const char *aggcombinefn;
15444 : : const char *aggserialfn;
15445 : : const char *aggdeserialfn;
15446 : : const char *aggmtransfn;
15447 : : const char *aggminvtransfn;
15448 : : const char *aggmfinalfn;
15449 : : bool aggfinalextra;
15450 : : bool aggmfinalextra;
15451 : : char aggfinalmodify;
15452 : : char aggmfinalmodify;
15453 : : const char *aggsortop;
15454 : : char *aggsortconvop;
15455 : : char aggkind;
15456 : : const char *aggtranstype;
15457 : : const char *aggtransspace;
15458 : : const char *aggmtranstype;
15459 : : const char *aggmtransspace;
15460 : : const char *agginitval;
15461 : : const char *aggminitval;
15462 : : const char *proparallel;
15463 : : const char *prosupport;
15464 : : char defaultfinalmodify;
15465 : :
15466 : : /* Do nothing if not dumping schema */
15467 [ + + ]: 295 : if (!dopt->dumpSchema)
15468 : 8 : return;
15469 : :
15470 : 287 : query = createPQExpBuffer();
15471 : 287 : q = createPQExpBuffer();
15472 : 287 : delq = createPQExpBuffer();
15473 : 287 : details = createPQExpBuffer();
15474 : :
15475 [ + + ]: 287 : if (!fout->is_prepared[PREPQUERY_DUMPAGG])
15476 : : {
15477 : : /* Set up query for aggregate-specific details */
15478 : 57 : appendPQExpBufferStr(query,
15479 : : "PREPARE dumpAgg(pg_catalog.oid) AS\n");
15480 : :
15481 : 57 : appendPQExpBufferStr(query,
15482 : : "SELECT "
15483 : : "aggtransfn,\n"
15484 : : "aggfinalfn,\n"
15485 : : "aggtranstype::pg_catalog.regtype,\n"
15486 : : "agginitval,\n"
15487 : : "aggsortop,\n"
15488 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
15489 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
15490 : :
15491 : 57 : appendPQExpBufferStr(query,
15492 : : "aggkind,\n"
15493 : : "aggmtransfn,\n"
15494 : : "aggminvtransfn,\n"
15495 : : "aggmfinalfn,\n"
15496 : : "aggmtranstype::pg_catalog.regtype,\n"
15497 : : "aggfinalextra,\n"
15498 : : "aggmfinalextra,\n"
15499 : : "aggtransspace,\n"
15500 : : "aggmtransspace,\n"
15501 : : "aggminitval,\n");
15502 : :
15503 : 57 : appendPQExpBufferStr(query,
15504 : : "aggcombinefn,\n"
15505 : : "aggserialfn,\n"
15506 : : "aggdeserialfn,\n"
15507 : : "proparallel,\n");
15508 : :
15509 [ + - ]: 57 : if (fout->remoteVersion >= 110000)
15510 : 57 : appendPQExpBufferStr(query,
15511 : : "aggfinalmodify,\n"
15512 : : "aggmfinalmodify,\n");
15513 : : else
15514 : 0 : appendPQExpBufferStr(query,
15515 : : "'0' AS aggfinalmodify,\n"
15516 : : "'0' AS aggmfinalmodify,\n");
15517 : :
15518 [ + - ]: 57 : if (fout->remoteVersion >= 120000)
15519 : 57 : appendPQExpBufferStr(query,
15520 : : "prosupport\n");
15521 : : else
15522 : 0 : appendPQExpBufferStr(query,
15523 : : "'-' AS prosupport\n");
15524 : :
15525 : 57 : appendPQExpBufferStr(query,
15526 : : "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
15527 : : "WHERE a.aggfnoid = p.oid "
15528 : : "AND p.oid = $1");
15529 : :
15530 : 57 : ExecuteSqlStatement(fout, query->data);
15531 : :
15532 : 57 : fout->is_prepared[PREPQUERY_DUMPAGG] = true;
15533 : : }
15534 : :
15535 : 287 : printfPQExpBuffer(query,
15536 : : "EXECUTE dumpAgg('%u')",
15537 : 287 : agginfo->aggfn.dobj.catId.oid);
15538 : :
15539 : 287 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15540 : :
15541 : 287 : i_agginitval = PQfnumber(res, "agginitval");
15542 : 287 : i_aggminitval = PQfnumber(res, "aggminitval");
15543 : :
15544 : 287 : aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
15545 : 287 : aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
15546 : 287 : aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
15547 : 287 : aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
15548 : 287 : aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
15549 : 287 : aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
15550 : 287 : aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
15551 : 287 : aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
15552 : 287 : aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
15553 : 287 : aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
15554 : 287 : aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
15555 : 287 : aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
15556 : 287 : aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
15557 : 287 : aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
15558 : 287 : aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
15559 : 287 : aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
15560 : 287 : aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
15561 : 287 : aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
15562 : 287 : agginitval = PQgetvalue(res, 0, i_agginitval);
15563 : 287 : aggminitval = PQgetvalue(res, 0, i_aggminitval);
15564 : 287 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
15565 : 287 : prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport"));
15566 : :
15567 : : {
15568 : : char *funcargs;
15569 : : char *funciargs;
15570 : :
15571 : 287 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
15572 : 287 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
15573 : 287 : aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
15574 : 287 : aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
15575 : : }
15576 : :
15577 : 287 : aggsig_tag = format_aggregate_signature(agginfo, fout, false);
15578 : :
15579 : : /* identify default modify flag for aggkind (must match DefineAggregate) */
15580 [ + + ]: 287 : defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
15581 : : /* replace omitted flags for old versions */
15582 [ - + ]: 287 : if (aggfinalmodify == '0')
15583 : 0 : aggfinalmodify = defaultfinalmodify;
15584 [ - + ]: 287 : if (aggmfinalmodify == '0')
15585 : 0 : aggmfinalmodify = defaultfinalmodify;
15586 : :
15587 : : /* regproc and regtype output is already sufficiently quoted */
15588 : 287 : appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
15589 : : aggtransfn, aggtranstype);
15590 : :
15591 [ + + ]: 287 : if (strcmp(aggtransspace, "0") != 0)
15592 : : {
15593 : 5 : appendPQExpBuffer(details, ",\n SSPACE = %s",
15594 : : aggtransspace);
15595 : : }
15596 : :
15597 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_agginitval))
15598 : : {
15599 : 209 : appendPQExpBufferStr(details, ",\n INITCOND = ");
15600 : 209 : appendStringLiteralAH(details, agginitval, fout);
15601 : : }
15602 : :
15603 [ + + ]: 287 : if (strcmp(aggfinalfn, "-") != 0)
15604 : : {
15605 : 134 : appendPQExpBuffer(details, ",\n FINALFUNC = %s",
15606 : : aggfinalfn);
15607 [ + + ]: 134 : if (aggfinalextra)
15608 : 10 : appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
15609 [ + + ]: 134 : if (aggfinalmodify != defaultfinalmodify)
15610 : : {
15611 [ - + - - ]: 34 : switch (aggfinalmodify)
15612 : : {
15613 : 0 : case AGGMODIFY_READ_ONLY:
15614 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
15615 : 0 : break;
15616 : 34 : case AGGMODIFY_SHAREABLE:
15617 : 34 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
15618 : 34 : break;
15619 : 0 : case AGGMODIFY_READ_WRITE:
15620 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
15621 : 0 : break;
15622 : 0 : default:
15623 : 0 : pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
15624 : : agginfo->aggfn.dobj.name);
15625 : : break;
15626 : : }
15627 : : }
15628 : : }
15629 : :
15630 [ - + ]: 287 : if (strcmp(aggcombinefn, "-") != 0)
15631 : 0 : appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
15632 : :
15633 [ - + ]: 287 : if (strcmp(aggserialfn, "-") != 0)
15634 : 0 : appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
15635 : :
15636 [ - + ]: 287 : if (strcmp(aggdeserialfn, "-") != 0)
15637 : 0 : appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
15638 : :
15639 [ + + ]: 287 : if (strcmp(aggmtransfn, "-") != 0)
15640 : : {
15641 : 30 : appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
15642 : : aggmtransfn,
15643 : : aggminvtransfn,
15644 : : aggmtranstype);
15645 : : }
15646 : :
15647 [ - + ]: 287 : if (strcmp(aggmtransspace, "0") != 0)
15648 : : {
15649 : 0 : appendPQExpBuffer(details, ",\n MSSPACE = %s",
15650 : : aggmtransspace);
15651 : : }
15652 : :
15653 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_aggminitval))
15654 : : {
15655 : 10 : appendPQExpBufferStr(details, ",\n MINITCOND = ");
15656 : 10 : appendStringLiteralAH(details, aggminitval, fout);
15657 : : }
15658 : :
15659 [ - + ]: 287 : if (strcmp(aggmfinalfn, "-") != 0)
15660 : : {
15661 : 0 : appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
15662 : : aggmfinalfn);
15663 [ # # ]: 0 : if (aggmfinalextra)
15664 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
15665 [ # # ]: 0 : if (aggmfinalmodify != defaultfinalmodify)
15666 : : {
15667 [ # # # # ]: 0 : switch (aggmfinalmodify)
15668 : : {
15669 : 0 : case AGGMODIFY_READ_ONLY:
15670 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
15671 : 0 : break;
15672 : 0 : case AGGMODIFY_SHAREABLE:
15673 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
15674 : 0 : break;
15675 : 0 : case AGGMODIFY_READ_WRITE:
15676 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
15677 : 0 : break;
15678 : 0 : default:
15679 : 0 : pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
15680 : : agginfo->aggfn.dobj.name);
15681 : : break;
15682 : : }
15683 : : }
15684 : : }
15685 : :
15686 : 287 : aggsortconvop = getFormattedOperatorName(aggsortop);
15687 [ - + ]: 287 : if (aggsortconvop)
15688 : : {
15689 : 0 : appendPQExpBuffer(details, ",\n SORTOP = %s",
15690 : : aggsortconvop);
15691 : 0 : free(aggsortconvop);
15692 : : }
15693 : :
15694 [ + + ]: 287 : if (strcmp(prosupport, "-") != 0)
15695 : : {
15696 : 5 : appendPQExpBuffer(details, ",\n SUPPORT = %s", prosupport);
15697 : : }
15698 : :
15699 [ + + ]: 287 : if (aggkind == AGGKIND_HYPOTHETICAL)
15700 : 5 : appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
15701 : :
15702 [ + + ]: 287 : if (proparallel[0] != PROPARALLEL_UNSAFE)
15703 : : {
15704 [ + - ]: 5 : if (proparallel[0] == PROPARALLEL_SAFE)
15705 : 5 : appendPQExpBufferStr(details, ",\n PARALLEL = safe");
15706 [ # # ]: 0 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
15707 : 0 : appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
15708 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
15709 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
15710 : : agginfo->aggfn.dobj.name);
15711 : : }
15712 : :
15713 : 287 : appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
15714 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15715 : : aggsig);
15716 : :
15717 [ + - ]: 574 : appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
15718 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15719 : : aggfullsig ? aggfullsig : aggsig, details->data);
15720 : :
15721 [ + + ]: 287 : if (dopt->binary_upgrade)
15722 : 49 : binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
15723 : : "AGGREGATE", aggsig,
15724 : 49 : agginfo->aggfn.dobj.namespace->dobj.name);
15725 : :
15726 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
15727 : 270 : ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
15728 : 270 : agginfo->aggfn.dobj.dumpId,
15729 : 270 : ARCHIVE_OPTS(.tag = aggsig_tag,
15730 : : .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
15731 : : .owner = agginfo->aggfn.rolname,
15732 : : .description = "AGGREGATE",
15733 : : .section = SECTION_PRE_DATA,
15734 : : .createStmt = q->data,
15735 : : .dropStmt = delq->data));
15736 : :
15737 : : /* Dump Aggregate Comments */
15738 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
15739 : 10 : dumpComment(fout, "AGGREGATE", aggsig,
15740 : 10 : agginfo->aggfn.dobj.namespace->dobj.name,
15741 : 10 : agginfo->aggfn.rolname,
15742 : 10 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15743 : :
15744 [ - + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
15745 : 0 : dumpSecLabel(fout, "AGGREGATE", aggsig,
15746 : 0 : agginfo->aggfn.dobj.namespace->dobj.name,
15747 : 0 : agginfo->aggfn.rolname,
15748 : 0 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15749 : :
15750 : : /*
15751 : : * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
15752 : : * command look like a function's GRANT; in particular this affects the
15753 : : * syntax for zero-argument aggregates and ordered-set aggregates.
15754 : : */
15755 : 287 : free(aggsig);
15756 : :
15757 : 287 : aggsig = format_function_signature(fout, &agginfo->aggfn, true);
15758 : :
15759 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
15760 : 18 : dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
15761 : : "FUNCTION", aggsig, NULL,
15762 : 18 : agginfo->aggfn.dobj.namespace->dobj.name,
15763 : 18 : NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
15764 : :
15765 : 287 : free(aggsig);
15766 : 287 : free(aggfullsig);
15767 : 287 : free(aggsig_tag);
15768 : :
15769 : 287 : PQclear(res);
15770 : :
15771 : 287 : destroyPQExpBuffer(query);
15772 : 287 : destroyPQExpBuffer(q);
15773 : 287 : destroyPQExpBuffer(delq);
15774 : 287 : destroyPQExpBuffer(details);
15775 : : }
15776 : :
15777 : : /*
15778 : : * dumpTSParser
15779 : : * write out a single text search parser
15780 : : */
15781 : : static void
15782 : 44 : dumpTSParser(Archive *fout, const TSParserInfo *prsinfo)
15783 : : {
15784 : 44 : DumpOptions *dopt = fout->dopt;
15785 : : PQExpBuffer q;
15786 : : PQExpBuffer delq;
15787 : : char *qprsname;
15788 : :
15789 : : /* Do nothing if not dumping schema */
15790 [ + + ]: 44 : if (!dopt->dumpSchema)
15791 : 7 : return;
15792 : :
15793 : 37 : q = createPQExpBuffer();
15794 : 37 : delq = createPQExpBuffer();
15795 : :
15796 : 37 : qprsname = pg_strdup(fmtId(prsinfo->dobj.name));
15797 : :
15798 : 37 : appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n",
15799 : 37 : fmtQualifiedDumpable(prsinfo));
15800 : :
15801 : 37 : appendPQExpBuffer(q, " START = %s,\n",
15802 : 37 : convertTSFunction(fout, prsinfo->prsstart));
15803 : 37 : appendPQExpBuffer(q, " GETTOKEN = %s,\n",
15804 : 37 : convertTSFunction(fout, prsinfo->prstoken));
15805 : 37 : appendPQExpBuffer(q, " END = %s,\n",
15806 : 37 : convertTSFunction(fout, prsinfo->prsend));
15807 [ + + ]: 37 : if (prsinfo->prsheadline != InvalidOid)
15808 : 3 : appendPQExpBuffer(q, " HEADLINE = %s,\n",
15809 : 3 : convertTSFunction(fout, prsinfo->prsheadline));
15810 : 37 : appendPQExpBuffer(q, " LEXTYPES = %s );\n",
15811 : 37 : convertTSFunction(fout, prsinfo->prslextype));
15812 : :
15813 : 37 : appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s;\n",
15814 : 37 : fmtQualifiedDumpable(prsinfo));
15815 : :
15816 [ + + ]: 37 : if (dopt->binary_upgrade)
15817 : 1 : binary_upgrade_extension_member(q, &prsinfo->dobj,
15818 : : "TEXT SEARCH PARSER", qprsname,
15819 : 1 : prsinfo->dobj.namespace->dobj.name);
15820 : :
15821 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15822 : 37 : ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId,
15823 : 37 : ARCHIVE_OPTS(.tag = prsinfo->dobj.name,
15824 : : .namespace = prsinfo->dobj.namespace->dobj.name,
15825 : : .description = "TEXT SEARCH PARSER",
15826 : : .section = SECTION_PRE_DATA,
15827 : : .createStmt = q->data,
15828 : : .dropStmt = delq->data));
15829 : :
15830 : : /* Dump Parser Comments */
15831 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15832 : 37 : dumpComment(fout, "TEXT SEARCH PARSER", qprsname,
15833 : 37 : prsinfo->dobj.namespace->dobj.name, "",
15834 : 37 : prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId);
15835 : :
15836 : 37 : destroyPQExpBuffer(q);
15837 : 37 : destroyPQExpBuffer(delq);
15838 : 37 : pg_free(qprsname);
15839 : : }
15840 : :
15841 : : /*
15842 : : * dumpTSDictionary
15843 : : * write out a single text search dictionary
15844 : : */
15845 : : static void
15846 : 182 : dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo)
15847 : : {
15848 : 182 : DumpOptions *dopt = fout->dopt;
15849 : : PQExpBuffer q;
15850 : : PQExpBuffer delq;
15851 : : PQExpBuffer query;
15852 : : char *qdictname;
15853 : : PGresult *res;
15854 : : char *nspname;
15855 : : char *tmplname;
15856 : :
15857 : : /* Do nothing if not dumping schema */
15858 [ + + ]: 182 : if (!dopt->dumpSchema)
15859 : 7 : return;
15860 : :
15861 : 175 : q = createPQExpBuffer();
15862 : 175 : delq = createPQExpBuffer();
15863 : 175 : query = createPQExpBuffer();
15864 : :
15865 : 175 : qdictname = pg_strdup(fmtId(dictinfo->dobj.name));
15866 : :
15867 : : /* Fetch name and namespace of the dictionary's template */
15868 : 175 : appendPQExpBuffer(query, "SELECT nspname, tmplname "
15869 : : "FROM pg_ts_template p, pg_namespace n "
15870 : : "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
15871 : 175 : dictinfo->dicttemplate);
15872 : 175 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15873 : 175 : nspname = PQgetvalue(res, 0, 0);
15874 : 175 : tmplname = PQgetvalue(res, 0, 1);
15875 : :
15876 : 175 : appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n",
15877 : 175 : fmtQualifiedDumpable(dictinfo));
15878 : :
15879 : 175 : appendPQExpBufferStr(q, " TEMPLATE = ");
15880 : 175 : appendPQExpBuffer(q, "%s.", fmtId(nspname));
15881 : 175 : appendPQExpBufferStr(q, fmtId(tmplname));
15882 : :
15883 : 175 : PQclear(res);
15884 : :
15885 : : /* the dictinitoption can be dumped straight into the command */
15886 [ + + ]: 175 : if (dictinfo->dictinitoption)
15887 : 138 : appendPQExpBuffer(q, ",\n %s", dictinfo->dictinitoption);
15888 : :
15889 : 175 : appendPQExpBufferStr(q, " );\n");
15890 : :
15891 : 175 : appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s;\n",
15892 : 175 : fmtQualifiedDumpable(dictinfo));
15893 : :
15894 [ + + ]: 175 : if (dopt->binary_upgrade)
15895 : 10 : binary_upgrade_extension_member(q, &dictinfo->dobj,
15896 : : "TEXT SEARCH DICTIONARY", qdictname,
15897 : 10 : dictinfo->dobj.namespace->dobj.name);
15898 : :
15899 [ + - ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15900 : 175 : ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId,
15901 : 175 : ARCHIVE_OPTS(.tag = dictinfo->dobj.name,
15902 : : .namespace = dictinfo->dobj.namespace->dobj.name,
15903 : : .owner = dictinfo->rolname,
15904 : : .description = "TEXT SEARCH DICTIONARY",
15905 : : .section = SECTION_PRE_DATA,
15906 : : .createStmt = q->data,
15907 : : .dropStmt = delq->data));
15908 : :
15909 : : /* Dump Dictionary Comments */
15910 [ + + ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15911 : 130 : dumpComment(fout, "TEXT SEARCH DICTIONARY", qdictname,
15912 : 130 : dictinfo->dobj.namespace->dobj.name, dictinfo->rolname,
15913 : 130 : dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId);
15914 : :
15915 : 175 : destroyPQExpBuffer(q);
15916 : 175 : destroyPQExpBuffer(delq);
15917 : 175 : destroyPQExpBuffer(query);
15918 : 175 : pg_free(qdictname);
15919 : : }
15920 : :
15921 : : /*
15922 : : * dumpTSTemplate
15923 : : * write out a single text search template
15924 : : */
15925 : : static void
15926 : 56 : dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo)
15927 : : {
15928 : 56 : DumpOptions *dopt = fout->dopt;
15929 : : PQExpBuffer q;
15930 : : PQExpBuffer delq;
15931 : : char *qtmplname;
15932 : :
15933 : : /* Do nothing if not dumping schema */
15934 [ + + ]: 56 : if (!dopt->dumpSchema)
15935 : 7 : return;
15936 : :
15937 : 49 : q = createPQExpBuffer();
15938 : 49 : delq = createPQExpBuffer();
15939 : :
15940 : 49 : qtmplname = pg_strdup(fmtId(tmplinfo->dobj.name));
15941 : :
15942 : 49 : appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n",
15943 : 49 : fmtQualifiedDumpable(tmplinfo));
15944 : :
15945 [ + + ]: 49 : if (tmplinfo->tmplinit != InvalidOid)
15946 : 15 : appendPQExpBuffer(q, " INIT = %s,\n",
15947 : 15 : convertTSFunction(fout, tmplinfo->tmplinit));
15948 : 49 : appendPQExpBuffer(q, " LEXIZE = %s );\n",
15949 : 49 : convertTSFunction(fout, tmplinfo->tmpllexize));
15950 : :
15951 : 49 : appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s;\n",
15952 : 49 : fmtQualifiedDumpable(tmplinfo));
15953 : :
15954 [ + + ]: 49 : if (dopt->binary_upgrade)
15955 : 1 : binary_upgrade_extension_member(q, &tmplinfo->dobj,
15956 : : "TEXT SEARCH TEMPLATE", qtmplname,
15957 : 1 : tmplinfo->dobj.namespace->dobj.name);
15958 : :
15959 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15960 : 49 : ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId,
15961 : 49 : ARCHIVE_OPTS(.tag = tmplinfo->dobj.name,
15962 : : .namespace = tmplinfo->dobj.namespace->dobj.name,
15963 : : .description = "TEXT SEARCH TEMPLATE",
15964 : : .section = SECTION_PRE_DATA,
15965 : : .createStmt = q->data,
15966 : : .dropStmt = delq->data));
15967 : :
15968 : : /* Dump Template Comments */
15969 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15970 : 49 : dumpComment(fout, "TEXT SEARCH TEMPLATE", qtmplname,
15971 : 49 : tmplinfo->dobj.namespace->dobj.name, "",
15972 : 49 : tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId);
15973 : :
15974 : 49 : destroyPQExpBuffer(q);
15975 : 49 : destroyPQExpBuffer(delq);
15976 : 49 : pg_free(qtmplname);
15977 : : }
15978 : :
15979 : : /*
15980 : : * dumpTSConfig
15981 : : * write out a single text search configuration
15982 : : */
15983 : : static void
15984 : 157 : dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo)
15985 : : {
15986 : 157 : DumpOptions *dopt = fout->dopt;
15987 : : PQExpBuffer q;
15988 : : PQExpBuffer delq;
15989 : : PQExpBuffer query;
15990 : : char *qcfgname;
15991 : : PGresult *res;
15992 : : char *nspname;
15993 : : char *prsname;
15994 : : int ntups,
15995 : : i;
15996 : : int i_tokenname;
15997 : : int i_dictname;
15998 : :
15999 : : /* Do nothing if not dumping schema */
16000 [ + + ]: 157 : if (!dopt->dumpSchema)
16001 : 7 : return;
16002 : :
16003 : 150 : q = createPQExpBuffer();
16004 : 150 : delq = createPQExpBuffer();
16005 : 150 : query = createPQExpBuffer();
16006 : :
16007 : 150 : qcfgname = pg_strdup(fmtId(cfginfo->dobj.name));
16008 : :
16009 : : /* Fetch name and namespace of the config's parser */
16010 : 150 : appendPQExpBuffer(query, "SELECT nspname, prsname "
16011 : : "FROM pg_ts_parser p, pg_namespace n "
16012 : : "WHERE p.oid = '%u' AND n.oid = prsnamespace",
16013 : 150 : cfginfo->cfgparser);
16014 : 150 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
16015 : 150 : nspname = PQgetvalue(res, 0, 0);
16016 : 150 : prsname = PQgetvalue(res, 0, 1);
16017 : :
16018 : 150 : appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n",
16019 : 150 : fmtQualifiedDumpable(cfginfo));
16020 : :
16021 : 150 : appendPQExpBuffer(q, " PARSER = %s.", fmtId(nspname));
16022 : 150 : appendPQExpBuffer(q, "%s );\n", fmtId(prsname));
16023 : :
16024 : 150 : PQclear(res);
16025 : :
16026 : 150 : resetPQExpBuffer(query);
16027 : 150 : appendPQExpBuffer(query,
16028 : : "SELECT\n"
16029 : : " ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n"
16030 : : " WHERE t.tokid = m.maptokentype ) AS tokenname,\n"
16031 : : " m.mapdict::pg_catalog.regdictionary AS dictname\n"
16032 : : "FROM pg_catalog.pg_ts_config_map AS m\n"
16033 : : "WHERE m.mapcfg = '%u'\n"
16034 : : "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
16035 : 150 : cfginfo->cfgparser, cfginfo->dobj.catId.oid);
16036 : :
16037 : 150 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16038 : 150 : ntups = PQntuples(res);
16039 : :
16040 : 150 : i_tokenname = PQfnumber(res, "tokenname");
16041 : 150 : i_dictname = PQfnumber(res, "dictname");
16042 : :
16043 [ + + ]: 3135 : for (i = 0; i < ntups; i++)
16044 : : {
16045 : 2985 : char *tokenname = PQgetvalue(res, i, i_tokenname);
16046 : 2985 : char *dictname = PQgetvalue(res, i, i_dictname);
16047 : :
16048 [ + + ]: 2985 : if (i == 0 ||
16049 [ + + ]: 2835 : strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0)
16050 : : {
16051 : : /* starting a new token type, so start a new command */
16052 [ + + ]: 2850 : if (i > 0)
16053 : 2700 : appendPQExpBufferStr(q, ";\n");
16054 : 2850 : appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n",
16055 : 2850 : fmtQualifiedDumpable(cfginfo));
16056 : : /* tokenname needs quoting, dictname does NOT */
16057 : 2850 : appendPQExpBuffer(q, " ADD MAPPING FOR %s WITH %s",
16058 : : fmtId(tokenname), dictname);
16059 : : }
16060 : : else
16061 : 135 : appendPQExpBuffer(q, ", %s", dictname);
16062 : : }
16063 : :
16064 [ + - ]: 150 : if (ntups > 0)
16065 : 150 : appendPQExpBufferStr(q, ";\n");
16066 : :
16067 : 150 : PQclear(res);
16068 : :
16069 : 150 : appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s;\n",
16070 : 150 : fmtQualifiedDumpable(cfginfo));
16071 : :
16072 [ + + ]: 150 : if (dopt->binary_upgrade)
16073 : 5 : binary_upgrade_extension_member(q, &cfginfo->dobj,
16074 : : "TEXT SEARCH CONFIGURATION", qcfgname,
16075 : 5 : cfginfo->dobj.namespace->dobj.name);
16076 : :
16077 [ + - ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16078 : 150 : ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId,
16079 : 150 : ARCHIVE_OPTS(.tag = cfginfo->dobj.name,
16080 : : .namespace = cfginfo->dobj.namespace->dobj.name,
16081 : : .owner = cfginfo->rolname,
16082 : : .description = "TEXT SEARCH CONFIGURATION",
16083 : : .section = SECTION_PRE_DATA,
16084 : : .createStmt = q->data,
16085 : : .dropStmt = delq->data));
16086 : :
16087 : : /* Dump Configuration Comments */
16088 [ + + ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16089 : 130 : dumpComment(fout, "TEXT SEARCH CONFIGURATION", qcfgname,
16090 : 130 : cfginfo->dobj.namespace->dobj.name, cfginfo->rolname,
16091 : 130 : cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId);
16092 : :
16093 : 150 : destroyPQExpBuffer(q);
16094 : 150 : destroyPQExpBuffer(delq);
16095 : 150 : destroyPQExpBuffer(query);
16096 : 150 : pg_free(qcfgname);
16097 : : }
16098 : :
16099 : : /*
16100 : : * dumpForeignDataWrapper
16101 : : * write out a single foreign-data wrapper definition
16102 : : */
16103 : : static void
16104 : 55 : dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo)
16105 : : {
16106 : 55 : DumpOptions *dopt = fout->dopt;
16107 : : PQExpBuffer q;
16108 : : PQExpBuffer delq;
16109 : : char *qfdwname;
16110 : :
16111 : : /* Do nothing if not dumping schema */
16112 [ + + ]: 55 : if (!dopt->dumpSchema)
16113 : 7 : return;
16114 : :
16115 : 48 : q = createPQExpBuffer();
16116 : 48 : delq = createPQExpBuffer();
16117 : :
16118 : 48 : qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name));
16119 : :
16120 : 48 : appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s",
16121 : : qfdwname);
16122 : :
16123 [ - + ]: 48 : if (strcmp(fdwinfo->fdwhandler, "-") != 0)
16124 : 0 : appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler);
16125 : :
16126 [ - + ]: 48 : if (strcmp(fdwinfo->fdwvalidator, "-") != 0)
16127 : 0 : appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator);
16128 : :
16129 [ - + ]: 48 : if (strcmp(fdwinfo->fdwconnection, "-") != 0)
16130 : 0 : appendPQExpBuffer(q, " CONNECTION %s", fdwinfo->fdwconnection);
16131 : :
16132 [ - + ]: 48 : if (strlen(fdwinfo->fdwoptions) > 0)
16133 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", fdwinfo->fdwoptions);
16134 : :
16135 : 48 : appendPQExpBufferStr(q, ";\n");
16136 : :
16137 : 48 : appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n",
16138 : : qfdwname);
16139 : :
16140 [ + + ]: 48 : if (dopt->binary_upgrade)
16141 : 3 : binary_upgrade_extension_member(q, &fdwinfo->dobj,
16142 : : "FOREIGN DATA WRAPPER", qfdwname,
16143 : : NULL);
16144 : :
16145 [ + - ]: 48 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16146 : 48 : ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
16147 : 48 : ARCHIVE_OPTS(.tag = fdwinfo->dobj.name,
16148 : : .owner = fdwinfo->rolname,
16149 : : .description = "FOREIGN DATA WRAPPER",
16150 : : .section = SECTION_PRE_DATA,
16151 : : .createStmt = q->data,
16152 : : .dropStmt = delq->data));
16153 : :
16154 : : /* Dump Foreign Data Wrapper Comments */
16155 [ - + ]: 48 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16156 : 0 : dumpComment(fout, "FOREIGN DATA WRAPPER", qfdwname,
16157 : 0 : NULL, fdwinfo->rolname,
16158 : 0 : fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
16159 : :
16160 : : /* Handle the ACL */
16161 [ + + ]: 48 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL)
16162 : 33 : dumpACL(fout, fdwinfo->dobj.dumpId, InvalidDumpId,
16163 : : "FOREIGN DATA WRAPPER", qfdwname, NULL, NULL,
16164 : 33 : NULL, fdwinfo->rolname, &fdwinfo->dacl);
16165 : :
16166 : 48 : pg_free(qfdwname);
16167 : :
16168 : 48 : destroyPQExpBuffer(q);
16169 : 48 : destroyPQExpBuffer(delq);
16170 : : }
16171 : :
16172 : : /*
16173 : : * dumpForeignServer
16174 : : * write out a foreign server definition
16175 : : */
16176 : : static void
16177 : 59 : dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo)
16178 : : {
16179 : 59 : DumpOptions *dopt = fout->dopt;
16180 : : PQExpBuffer q;
16181 : : PQExpBuffer delq;
16182 : : PQExpBuffer query;
16183 : : PGresult *res;
16184 : : char *qsrvname;
16185 : : char *fdwname;
16186 : :
16187 : : /* Do nothing if not dumping schema */
16188 [ + + ]: 59 : if (!dopt->dumpSchema)
16189 : 9 : return;
16190 : :
16191 : 50 : q = createPQExpBuffer();
16192 : 50 : delq = createPQExpBuffer();
16193 : 50 : query = createPQExpBuffer();
16194 : :
16195 : 50 : qsrvname = pg_strdup(fmtId(srvinfo->dobj.name));
16196 : :
16197 : : /* look up the foreign-data wrapper */
16198 : 50 : appendPQExpBuffer(query, "SELECT fdwname "
16199 : : "FROM pg_foreign_data_wrapper w "
16200 : : "WHERE w.oid = '%u'",
16201 : 50 : srvinfo->srvfdw);
16202 : 50 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
16203 : 50 : fdwname = PQgetvalue(res, 0, 0);
16204 : :
16205 : 50 : appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
16206 [ + - - + ]: 50 : if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0)
16207 : : {
16208 : 0 : appendPQExpBufferStr(q, " TYPE ");
16209 : 0 : appendStringLiteralAH(q, srvinfo->srvtype, fout);
16210 : : }
16211 [ + - - + ]: 50 : if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0)
16212 : : {
16213 : 0 : appendPQExpBufferStr(q, " VERSION ");
16214 : 0 : appendStringLiteralAH(q, srvinfo->srvversion, fout);
16215 : : }
16216 : :
16217 : 50 : appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER ");
16218 : 50 : appendPQExpBufferStr(q, fmtId(fdwname));
16219 : :
16220 [ + - - + ]: 50 : if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0)
16221 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", srvinfo->srvoptions);
16222 : :
16223 : 50 : appendPQExpBufferStr(q, ";\n");
16224 : :
16225 : 50 : appendPQExpBuffer(delq, "DROP SERVER %s;\n",
16226 : : qsrvname);
16227 : :
16228 [ + + ]: 50 : if (dopt->binary_upgrade)
16229 : 3 : binary_upgrade_extension_member(q, &srvinfo->dobj,
16230 : : "SERVER", qsrvname, NULL);
16231 : :
16232 [ + - ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16233 : 50 : ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
16234 : 50 : ARCHIVE_OPTS(.tag = srvinfo->dobj.name,
16235 : : .owner = srvinfo->rolname,
16236 : : .description = "SERVER",
16237 : : .section = SECTION_PRE_DATA,
16238 : : .createStmt = q->data,
16239 : : .dropStmt = delq->data));
16240 : :
16241 : : /* Dump Foreign Server Comments */
16242 [ - + ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16243 : 0 : dumpComment(fout, "SERVER", qsrvname,
16244 : 0 : NULL, srvinfo->rolname,
16245 : 0 : srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
16246 : :
16247 : : /* Handle the ACL */
16248 [ + + ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL)
16249 : 33 : dumpACL(fout, srvinfo->dobj.dumpId, InvalidDumpId,
16250 : : "FOREIGN SERVER", qsrvname, NULL, NULL,
16251 : 33 : NULL, srvinfo->rolname, &srvinfo->dacl);
16252 : :
16253 : : /* Dump user mappings */
16254 [ + - ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP)
16255 : 50 : dumpUserMappings(fout,
16256 : 50 : srvinfo->dobj.name, NULL,
16257 : 50 : srvinfo->rolname,
16258 : 50 : srvinfo->dobj.catId, srvinfo->dobj.dumpId);
16259 : :
16260 : 50 : PQclear(res);
16261 : :
16262 : 50 : pg_free(qsrvname);
16263 : :
16264 : 50 : destroyPQExpBuffer(q);
16265 : 50 : destroyPQExpBuffer(delq);
16266 : 50 : destroyPQExpBuffer(query);
16267 : : }
16268 : :
16269 : : /*
16270 : : * dumpUserMappings
16271 : : *
16272 : : * This routine is used to dump any user mappings associated with the
16273 : : * server handed to this routine. Should be called after ArchiveEntry()
16274 : : * for the server.
16275 : : */
16276 : : static void
16277 : 50 : dumpUserMappings(Archive *fout,
16278 : : const char *servername, const char *namespace,
16279 : : const char *owner,
16280 : : CatalogId catalogId, DumpId dumpId)
16281 : : {
16282 : : PQExpBuffer q;
16283 : : PQExpBuffer delq;
16284 : : PQExpBuffer query;
16285 : : PQExpBuffer tag;
16286 : : PGresult *res;
16287 : : int ntups;
16288 : : int i_usename;
16289 : : int i_umoptions;
16290 : : int i;
16291 : :
16292 : 50 : q = createPQExpBuffer();
16293 : 50 : tag = createPQExpBuffer();
16294 : 50 : delq = createPQExpBuffer();
16295 : 50 : query = createPQExpBuffer();
16296 : :
16297 : : /*
16298 : : * We read from the publicly accessible view pg_user_mappings, so as not
16299 : : * to fail if run by a non-superuser. Note that the view will show
16300 : : * umoptions as null if the user hasn't got privileges for the associated
16301 : : * server; this means that pg_dump will dump such a mapping, but with no
16302 : : * OPTIONS clause. A possible alternative is to skip such mappings
16303 : : * altogether, but it's not clear that that's an improvement.
16304 : : */
16305 : 50 : appendPQExpBuffer(query,
16306 : : "SELECT usename, "
16307 : : "array_to_string(ARRAY("
16308 : : "SELECT quote_ident(option_name) || ' ' || "
16309 : : "quote_literal(option_value) "
16310 : : "FROM pg_options_to_table(umoptions) "
16311 : : "ORDER BY option_name"
16312 : : "), E',\n ') AS umoptions "
16313 : : "FROM pg_user_mappings "
16314 : : "WHERE srvid = '%u' "
16315 : : "ORDER BY usename",
16316 : : catalogId.oid);
16317 : :
16318 : 50 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16319 : :
16320 : 50 : ntups = PQntuples(res);
16321 : 50 : i_usename = PQfnumber(res, "usename");
16322 : 50 : i_umoptions = PQfnumber(res, "umoptions");
16323 : :
16324 [ + + ]: 83 : for (i = 0; i < ntups; i++)
16325 : : {
16326 : : char *usename;
16327 : : char *umoptions;
16328 : :
16329 : 33 : usename = PQgetvalue(res, i, i_usename);
16330 : 33 : umoptions = PQgetvalue(res, i, i_umoptions);
16331 : :
16332 : 33 : resetPQExpBuffer(q);
16333 : 33 : appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
16334 : 33 : appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
16335 : :
16336 [ + - - + ]: 33 : if (umoptions && strlen(umoptions) > 0)
16337 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", umoptions);
16338 : :
16339 : 33 : appendPQExpBufferStr(q, ";\n");
16340 : :
16341 : 33 : resetPQExpBuffer(delq);
16342 : 33 : appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
16343 : 33 : appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
16344 : :
16345 : 33 : resetPQExpBuffer(tag);
16346 : 33 : appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
16347 : : usename, servername);
16348 : :
16349 : 33 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16350 : 33 : ARCHIVE_OPTS(.tag = tag->data,
16351 : : .namespace = namespace,
16352 : : .owner = owner,
16353 : : .description = "USER MAPPING",
16354 : : .section = SECTION_PRE_DATA,
16355 : : .createStmt = q->data,
16356 : : .dropStmt = delq->data));
16357 : : }
16358 : :
16359 : 50 : PQclear(res);
16360 : :
16361 : 50 : destroyPQExpBuffer(query);
16362 : 50 : destroyPQExpBuffer(delq);
16363 : 50 : destroyPQExpBuffer(tag);
16364 : 50 : destroyPQExpBuffer(q);
16365 : 50 : }
16366 : :
16367 : : /*
16368 : : * Write out default privileges information
16369 : : */
16370 : : static void
16371 : 170 : dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo)
16372 : : {
16373 : 170 : DumpOptions *dopt = fout->dopt;
16374 : : PQExpBuffer q;
16375 : : PQExpBuffer tag;
16376 : : const char *type;
16377 : :
16378 : : /* Do nothing if not dumping schema, or if we're skipping ACLs */
16379 [ + + + + ]: 170 : if (!dopt->dumpSchema || dopt->aclsSkip)
16380 : 30 : return;
16381 : :
16382 : 140 : q = createPQExpBuffer();
16383 : 140 : tag = createPQExpBuffer();
16384 : :
16385 [ + - + + : 140 : switch (daclinfo->defaclobjtype)
- - - ]
16386 : : {
16387 : 65 : case DEFACLOBJ_RELATION:
16388 : 65 : type = "TABLES";
16389 : 65 : break;
16390 : 0 : case DEFACLOBJ_SEQUENCE:
16391 : 0 : type = "SEQUENCES";
16392 : 0 : break;
16393 : 65 : case DEFACLOBJ_FUNCTION:
16394 : 65 : type = "FUNCTIONS";
16395 : 65 : break;
16396 : 10 : case DEFACLOBJ_TYPE:
16397 : 10 : type = "TYPES";
16398 : 10 : break;
16399 : 0 : case DEFACLOBJ_NAMESPACE:
16400 : 0 : type = "SCHEMAS";
16401 : 0 : break;
16402 : 0 : case DEFACLOBJ_LARGEOBJECT:
16403 : 0 : type = "LARGE OBJECTS";
16404 : 0 : break;
16405 : 0 : default:
16406 : : /* shouldn't get here */
16407 : 0 : pg_fatal("unrecognized object type in default privileges: %d",
16408 : : (int) daclinfo->defaclobjtype);
16409 : : type = ""; /* keep compiler quiet */
16410 : : }
16411 : :
16412 : 140 : appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
16413 : :
16414 : : /* build the actual command(s) for this tuple */
16415 [ - + ]: 140 : if (!buildDefaultACLCommands(type,
16416 : 140 : daclinfo->dobj.namespace != NULL ?
16417 : 66 : daclinfo->dobj.namespace->dobj.name : NULL,
16418 : 140 : daclinfo->dacl.acl,
16419 : 140 : daclinfo->dacl.acldefault,
16420 [ + + ]: 140 : daclinfo->defaclrole,
16421 : : fout->remoteVersion,
16422 : : q))
16423 : 0 : pg_fatal("could not parse default ACL list (%s)",
16424 : : daclinfo->dacl.acl);
16425 : :
16426 [ + - ]: 140 : if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
16427 : 140 : ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
16428 [ + + ]: 140 : ARCHIVE_OPTS(.tag = tag->data,
16429 : : .namespace = daclinfo->dobj.namespace ?
16430 : : daclinfo->dobj.namespace->dobj.name : NULL,
16431 : : .owner = daclinfo->defaclrole,
16432 : : .description = "DEFAULT ACL",
16433 : : .section = SECTION_POST_DATA,
16434 : : .createStmt = q->data));
16435 : :
16436 : 140 : destroyPQExpBuffer(tag);
16437 : 140 : destroyPQExpBuffer(q);
16438 : : }
16439 : :
16440 : : /*----------
16441 : : * Write out grant/revoke information
16442 : : *
16443 : : * 'objDumpId' is the dump ID of the underlying object.
16444 : : * 'altDumpId' can be a second dumpId that the ACL entry must also depend on,
16445 : : * or InvalidDumpId if there is no need for a second dependency.
16446 : : * 'type' must be one of
16447 : : * TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
16448 : : * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT.
16449 : : * 'name' is the formatted name of the object. Must be quoted etc. already.
16450 : : * 'subname' is the formatted name of the sub-object, if any. Must be quoted.
16451 : : * (Currently we assume that subname is only provided for table columns.)
16452 : : * 'nspname' is the namespace the object is in (NULL if none).
16453 : : * 'tag' is the tag to use for the ACL TOC entry; typically, this is NULL
16454 : : * to use the default for the object type.
16455 : : * 'owner' is the owner, NULL if there is no owner (for languages).
16456 : : * 'dacl' is the DumpableAcl struct for the object.
16457 : : *
16458 : : * Returns the dump ID assigned to the ACL TocEntry, or InvalidDumpId if
16459 : : * no ACL entry was created.
16460 : : *----------
16461 : : */
16462 : : static DumpId
16463 : 35376 : dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
16464 : : const char *type, const char *name, const char *subname,
16465 : : const char *nspname, const char *tag, const char *owner,
16466 : : const DumpableAcl *dacl)
16467 : : {
16468 : 35376 : DumpId aclDumpId = InvalidDumpId;
16469 : 35376 : DumpOptions *dopt = fout->dopt;
16470 : 35376 : const char *acls = dacl->acl;
16471 : 35376 : const char *acldefault = dacl->acldefault;
16472 : 35376 : char privtype = dacl->privtype;
16473 : 35376 : const char *initprivs = dacl->initprivs;
16474 : : const char *baseacls;
16475 : : PQExpBuffer sql;
16476 : :
16477 : : /* Do nothing if ACL dump is not enabled */
16478 [ + + ]: 35376 : if (dopt->aclsSkip)
16479 : 338 : return InvalidDumpId;
16480 : :
16481 : : /* --data-only skips ACLs *except* large object ACLs */
16482 [ + + + + ]: 35038 : if (!dopt->dumpSchema && strcmp(type, "LARGE OBJECT") != 0)
16483 : 1 : return InvalidDumpId;
16484 : :
16485 : 35037 : sql = createPQExpBuffer();
16486 : :
16487 : : /*
16488 : : * In binary upgrade mode, we don't run an extension's script but instead
16489 : : * dump out the objects independently and then recreate them. To preserve
16490 : : * any initial privileges which were set on extension objects, we need to
16491 : : * compute the set of GRANT and REVOKE commands necessary to get from the
16492 : : * default privileges of an object to its initial privileges as recorded
16493 : : * in pg_init_privs.
16494 : : *
16495 : : * At restore time, we apply these commands after having called
16496 : : * binary_upgrade_set_record_init_privs(true). That tells the backend to
16497 : : * copy the results into pg_init_privs. This is how we preserve the
16498 : : * contents of that catalog across binary upgrades.
16499 : : */
16500 [ + + + + : 35037 : if (dopt->binary_upgrade && privtype == 'e' &&
+ - ]
16501 [ + - ]: 13 : initprivs && *initprivs != '\0')
16502 : : {
16503 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
16504 [ - + ]: 13 : if (!buildACLCommands(name, subname, nspname, type,
16505 : : initprivs, acldefault, owner,
16506 : : "", fout->remoteVersion, sql))
16507 : 0 : pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
16508 : : initprivs, acldefault, name, type);
16509 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
16510 : : }
16511 : :
16512 : : /*
16513 : : * Now figure the GRANT and REVOKE commands needed to get to the object's
16514 : : * actual current ACL, starting from the initprivs if given, else from the
16515 : : * object-type-specific default. Also, while buildACLCommands will assume
16516 : : * that a NULL/empty acls string means it needn't do anything, what that
16517 : : * actually represents is the object-type-specific default; so we need to
16518 : : * substitute the acldefault string to get the right results in that case.
16519 : : */
16520 [ + + + + ]: 35037 : if (initprivs && *initprivs != '\0')
16521 : : {
16522 : 33178 : baseacls = initprivs;
16523 [ + - + + ]: 33178 : if (acls == NULL || *acls == '\0')
16524 : 17 : acls = acldefault;
16525 : : }
16526 : : else
16527 : 1859 : baseacls = acldefault;
16528 : :
16529 [ - + ]: 35037 : if (!buildACLCommands(name, subname, nspname, type,
16530 : : acls, baseacls, owner,
16531 : : "", fout->remoteVersion, sql))
16532 : 0 : pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
16533 : : acls, baseacls, name, type);
16534 : :
16535 [ + + ]: 35037 : if (sql->len > 0)
16536 : : {
16537 : 1905 : PQExpBuffer tagbuf = createPQExpBuffer();
16538 : : DumpId aclDeps[2];
16539 : 1905 : int nDeps = 0;
16540 : :
16541 [ - + ]: 1905 : if (tag)
16542 : 0 : appendPQExpBufferStr(tagbuf, tag);
16543 [ + + ]: 1905 : else if (subname)
16544 : 1111 : appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname);
16545 : : else
16546 : 794 : appendPQExpBuffer(tagbuf, "%s %s", type, name);
16547 : :
16548 : 1905 : aclDeps[nDeps++] = objDumpId;
16549 [ + + ]: 1905 : if (altDumpId != InvalidDumpId)
16550 : 1025 : aclDeps[nDeps++] = altDumpId;
16551 : :
16552 : 1905 : aclDumpId = createDumpId();
16553 : :
16554 : 1905 : ArchiveEntry(fout, nilCatalogId, aclDumpId,
16555 : 1905 : ARCHIVE_OPTS(.tag = tagbuf->data,
16556 : : .namespace = nspname,
16557 : : .owner = owner,
16558 : : .description = "ACL",
16559 : : .section = SECTION_NONE,
16560 : : .createStmt = sql->data,
16561 : : .deps = aclDeps,
16562 : : .nDeps = nDeps));
16563 : :
16564 : 1905 : destroyPQExpBuffer(tagbuf);
16565 : : }
16566 : :
16567 : 35037 : destroyPQExpBuffer(sql);
16568 : :
16569 : 35037 : return aclDumpId;
16570 : : }
16571 : :
16572 : : /*
16573 : : * dumpSecLabel
16574 : : *
16575 : : * This routine is used to dump any security labels associated with the
16576 : : * object handed to this routine. The routine takes the object type
16577 : : * and object name (ready to print, except for schema decoration), plus
16578 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
16579 : : * plus catalog ID and subid which are the lookup key for pg_seclabel,
16580 : : * plus the dump ID for the object (for setting a dependency).
16581 : : * If a matching pg_seclabel entry is found, it is dumped.
16582 : : *
16583 : : * Note: although this routine takes a dumpId for dependency purposes,
16584 : : * that purpose is just to mark the dependency in the emitted dump file
16585 : : * for possible future use by pg_restore. We do NOT use it for determining
16586 : : * ordering of the label in the dump file, because this routine is called
16587 : : * after dependency sorting occurs. This routine should be called just after
16588 : : * calling ArchiveEntry() for the specified object.
16589 : : */
16590 : : static void
16591 : 10 : dumpSecLabel(Archive *fout, const char *type, const char *name,
16592 : : const char *namespace, const char *owner,
16593 : : CatalogId catalogId, int subid, DumpId dumpId)
16594 : : {
16595 : 10 : DumpOptions *dopt = fout->dopt;
16596 : : SecLabelItem *labels;
16597 : : int nlabels;
16598 : : int i;
16599 : : PQExpBuffer query;
16600 : :
16601 : : /* do nothing, if --no-security-labels is supplied */
16602 [ - + ]: 10 : if (dopt->no_security_labels)
16603 : 0 : return;
16604 : :
16605 : : /*
16606 : : * Security labels are schema not data ... except large object labels are
16607 : : * data
16608 : : */
16609 [ - + ]: 10 : if (strcmp(type, "LARGE OBJECT") != 0)
16610 : : {
16611 [ # # ]: 0 : if (!dopt->dumpSchema)
16612 : 0 : return;
16613 : : }
16614 : : else
16615 : : {
16616 : : /* We do dump large object security labels in binary-upgrade mode */
16617 [ + - - + ]: 10 : if (!dopt->dumpData && !dopt->binary_upgrade)
16618 : 0 : return;
16619 : : }
16620 : :
16621 : : /* Search for security labels associated with catalogId, using table */
16622 : 10 : nlabels = findSecLabels(catalogId.tableoid, catalogId.oid, &labels);
16623 : :
16624 : 10 : query = createPQExpBuffer();
16625 : :
16626 [ + + ]: 15 : for (i = 0; i < nlabels; i++)
16627 : : {
16628 : : /*
16629 : : * Ignore label entries for which the subid doesn't match.
16630 : : */
16631 [ - + ]: 5 : if (labels[i].objsubid != subid)
16632 : 0 : continue;
16633 : :
16634 : 5 : appendPQExpBuffer(query,
16635 : : "SECURITY LABEL FOR %s ON %s ",
16636 : 5 : fmtId(labels[i].provider), type);
16637 [ - + - - ]: 5 : if (namespace && *namespace)
16638 : 0 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
16639 : 5 : appendPQExpBuffer(query, "%s IS ", name);
16640 : 5 : appendStringLiteralAH(query, labels[i].label, fout);
16641 : 5 : appendPQExpBufferStr(query, ";\n");
16642 : : }
16643 : :
16644 [ + + ]: 10 : if (query->len > 0)
16645 : : {
16646 : 5 : PQExpBuffer tag = createPQExpBuffer();
16647 : :
16648 : 5 : appendPQExpBuffer(tag, "%s %s", type, name);
16649 : 5 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16650 : 5 : ARCHIVE_OPTS(.tag = tag->data,
16651 : : .namespace = namespace,
16652 : : .owner = owner,
16653 : : .description = "SECURITY LABEL",
16654 : : .section = SECTION_NONE,
16655 : : .createStmt = query->data,
16656 : : .deps = &dumpId,
16657 : : .nDeps = 1));
16658 : 5 : destroyPQExpBuffer(tag);
16659 : : }
16660 : :
16661 : 10 : destroyPQExpBuffer(query);
16662 : : }
16663 : :
16664 : : /*
16665 : : * dumpTableSecLabel
16666 : : *
16667 : : * As above, but dump security label for both the specified table (or view)
16668 : : * and its columns.
16669 : : */
16670 : : static void
16671 : 0 : dumpTableSecLabel(Archive *fout, const TableInfo *tbinfo, const char *reltypename)
16672 : : {
16673 : 0 : DumpOptions *dopt = fout->dopt;
16674 : : SecLabelItem *labels;
16675 : : int nlabels;
16676 : : int i;
16677 : : PQExpBuffer query;
16678 : : PQExpBuffer target;
16679 : :
16680 : : /* do nothing, if --no-security-labels is supplied */
16681 [ # # ]: 0 : if (dopt->no_security_labels)
16682 : 0 : return;
16683 : :
16684 : : /* SecLabel are SCHEMA not data */
16685 [ # # ]: 0 : if (!dopt->dumpSchema)
16686 : 0 : return;
16687 : :
16688 : : /* Search for comments associated with relation, using table */
16689 : 0 : nlabels = findSecLabels(tbinfo->dobj.catId.tableoid,
16690 : 0 : tbinfo->dobj.catId.oid,
16691 : : &labels);
16692 : :
16693 : : /* If security labels exist, build SECURITY LABEL statements */
16694 [ # # ]: 0 : if (nlabels <= 0)
16695 : 0 : return;
16696 : :
16697 : 0 : query = createPQExpBuffer();
16698 : 0 : target = createPQExpBuffer();
16699 : :
16700 [ # # ]: 0 : for (i = 0; i < nlabels; i++)
16701 : : {
16702 : : const char *colname;
16703 : 0 : const char *provider = labels[i].provider;
16704 : 0 : const char *label = labels[i].label;
16705 : 0 : int objsubid = labels[i].objsubid;
16706 : :
16707 : 0 : resetPQExpBuffer(target);
16708 [ # # ]: 0 : if (objsubid == 0)
16709 : : {
16710 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16711 : 0 : fmtQualifiedDumpable(tbinfo));
16712 : : }
16713 : : else
16714 : : {
16715 : 0 : colname = getAttrName(objsubid, tbinfo);
16716 : : /* first fmtXXX result must be consumed before calling again */
16717 : 0 : appendPQExpBuffer(target, "COLUMN %s",
16718 : 0 : fmtQualifiedDumpable(tbinfo));
16719 : 0 : appendPQExpBuffer(target, ".%s", fmtId(colname));
16720 : : }
16721 : 0 : appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ",
16722 : : fmtId(provider), target->data);
16723 : 0 : appendStringLiteralAH(query, label, fout);
16724 : 0 : appendPQExpBufferStr(query, ";\n");
16725 : : }
16726 [ # # ]: 0 : if (query->len > 0)
16727 : : {
16728 : 0 : resetPQExpBuffer(target);
16729 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16730 : 0 : fmtId(tbinfo->dobj.name));
16731 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16732 : 0 : ARCHIVE_OPTS(.tag = target->data,
16733 : : .namespace = tbinfo->dobj.namespace->dobj.name,
16734 : : .owner = tbinfo->rolname,
16735 : : .description = "SECURITY LABEL",
16736 : : .section = SECTION_NONE,
16737 : : .createStmt = query->data,
16738 : : .deps = &(tbinfo->dobj.dumpId),
16739 : : .nDeps = 1));
16740 : : }
16741 : 0 : destroyPQExpBuffer(query);
16742 : 0 : destroyPQExpBuffer(target);
16743 : : }
16744 : :
16745 : : /*
16746 : : * findSecLabels
16747 : : *
16748 : : * Find the security label(s), if any, associated with the given object.
16749 : : * All the objsubid values associated with the given classoid/objoid are
16750 : : * found with one search.
16751 : : */
16752 : : static int
16753 : 10 : findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items)
16754 : : {
16755 : 10 : SecLabelItem *middle = NULL;
16756 : : SecLabelItem *low;
16757 : : SecLabelItem *high;
16758 : : int nmatch;
16759 : :
16760 [ - + ]: 10 : if (nseclabels <= 0) /* no labels, so no match is possible */
16761 : : {
16762 : 0 : *items = NULL;
16763 : 0 : return 0;
16764 : : }
16765 : :
16766 : : /*
16767 : : * Do binary search to find some item matching the object.
16768 : : */
16769 : 10 : low = &seclabels[0];
16770 : 10 : high = &seclabels[nseclabels - 1];
16771 [ + + ]: 15 : while (low <= high)
16772 : : {
16773 : 10 : middle = low + (high - low) / 2;
16774 : :
16775 [ - + ]: 10 : if (classoid < middle->classoid)
16776 : 0 : high = middle - 1;
16777 [ - + ]: 10 : else if (classoid > middle->classoid)
16778 : 0 : low = middle + 1;
16779 [ + + ]: 10 : else if (objoid < middle->objoid)
16780 : 5 : high = middle - 1;
16781 [ - + ]: 5 : else if (objoid > middle->objoid)
16782 : 0 : low = middle + 1;
16783 : : else
16784 : 5 : break; /* found a match */
16785 : : }
16786 : :
16787 [ + + ]: 10 : if (low > high) /* no matches */
16788 : : {
16789 : 5 : *items = NULL;
16790 : 5 : return 0;
16791 : : }
16792 : :
16793 : : /*
16794 : : * Now determine how many items match the object. The search loop
16795 : : * invariant still holds: only items between low and high inclusive could
16796 : : * match.
16797 : : */
16798 : 5 : nmatch = 1;
16799 [ - + ]: 5 : while (middle > low)
16800 : : {
16801 [ # # ]: 0 : if (classoid != middle[-1].classoid ||
16802 [ # # ]: 0 : objoid != middle[-1].objoid)
16803 : : break;
16804 : 0 : middle--;
16805 : 0 : nmatch++;
16806 : : }
16807 : :
16808 : 5 : *items = middle;
16809 : :
16810 : 5 : middle += nmatch;
16811 [ - + ]: 5 : while (middle <= high)
16812 : : {
16813 [ # # ]: 0 : if (classoid != middle->classoid ||
16814 [ # # ]: 0 : objoid != middle->objoid)
16815 : : break;
16816 : 0 : middle++;
16817 : 0 : nmatch++;
16818 : : }
16819 : :
16820 : 5 : return nmatch;
16821 : : }
16822 : :
16823 : : /*
16824 : : * collectSecLabels
16825 : : *
16826 : : * Construct a table of all security labels available for database objects;
16827 : : * also set the has-seclabel component flag for each relevant object.
16828 : : *
16829 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
16830 : : */
16831 : : static void
16832 : 215 : collectSecLabels(Archive *fout)
16833 : : {
16834 : : PGresult *res;
16835 : : PQExpBuffer query;
16836 : : int i_label;
16837 : : int i_provider;
16838 : : int i_classoid;
16839 : : int i_objoid;
16840 : : int i_objsubid;
16841 : : int ntups;
16842 : : int i;
16843 : : DumpableObject *dobj;
16844 : :
16845 : 215 : query = createPQExpBuffer();
16846 : :
16847 : 215 : appendPQExpBufferStr(query,
16848 : : "SELECT label, provider, classoid, objoid, objsubid "
16849 : : "FROM pg_catalog.pg_seclabels "
16850 : : "ORDER BY classoid, objoid, objsubid");
16851 : :
16852 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16853 : :
16854 : : /* Construct lookup table containing OIDs in numeric form */
16855 : 215 : i_label = PQfnumber(res, "label");
16856 : 215 : i_provider = PQfnumber(res, "provider");
16857 : 215 : i_classoid = PQfnumber(res, "classoid");
16858 : 215 : i_objoid = PQfnumber(res, "objoid");
16859 : 215 : i_objsubid = PQfnumber(res, "objsubid");
16860 : :
16861 : 215 : ntups = PQntuples(res);
16862 : :
16863 : 215 : seclabels = pg_malloc_array(SecLabelItem, ntups);
16864 : 215 : nseclabels = 0;
16865 : 215 : dobj = NULL;
16866 : :
16867 [ + + ]: 220 : for (i = 0; i < ntups; i++)
16868 : : {
16869 : : CatalogId objId;
16870 : : int subid;
16871 : :
16872 : 5 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
16873 : 5 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
16874 : 5 : subid = atoi(PQgetvalue(res, i, i_objsubid));
16875 : :
16876 : : /* We needn't remember labels that don't match any dumpable object */
16877 [ - + ]: 5 : if (dobj == NULL ||
16878 [ # # ]: 0 : dobj->catId.tableoid != objId.tableoid ||
16879 [ # # ]: 0 : dobj->catId.oid != objId.oid)
16880 : 5 : dobj = findObjectByCatalogId(objId);
16881 [ - + ]: 5 : if (dobj == NULL)
16882 : 0 : continue;
16883 : :
16884 : : /*
16885 : : * Labels on columns of composite types are linked to the type's
16886 : : * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
16887 : : * in the type's own DumpableObject.
16888 : : */
16889 [ - + - - ]: 5 : if (subid != 0 && dobj->objType == DO_TABLE &&
16890 [ # # ]: 0 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
16891 : 0 : {
16892 : : TypeInfo *cTypeInfo;
16893 : :
16894 : 0 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
16895 [ # # ]: 0 : if (cTypeInfo)
16896 : 0 : cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
16897 : : }
16898 : : else
16899 : 5 : dobj->components |= DUMP_COMPONENT_SECLABEL;
16900 : :
16901 : 5 : seclabels[nseclabels].label = pg_strdup(PQgetvalue(res, i, i_label));
16902 : 5 : seclabels[nseclabels].provider = pg_strdup(PQgetvalue(res, i, i_provider));
16903 : 5 : seclabels[nseclabels].classoid = objId.tableoid;
16904 : 5 : seclabels[nseclabels].objoid = objId.oid;
16905 : 5 : seclabels[nseclabels].objsubid = subid;
16906 : 5 : nseclabels++;
16907 : : }
16908 : :
16909 : 215 : PQclear(res);
16910 : 215 : destroyPQExpBuffer(query);
16911 : 215 : }
16912 : :
16913 : : /*
16914 : : * dumpTable
16915 : : * write out to fout the declarations (not data) of a user-defined table
16916 : : */
16917 : : static void
16918 : 36965 : dumpTable(Archive *fout, const TableInfo *tbinfo)
16919 : : {
16920 : 36965 : DumpOptions *dopt = fout->dopt;
16921 : 36965 : DumpId tableAclDumpId = InvalidDumpId;
16922 : : char *namecopy;
16923 : :
16924 : : /* Do nothing if not dumping schema */
16925 [ + + ]: 36965 : if (!dopt->dumpSchema)
16926 : 1645 : return;
16927 : :
16928 [ + + ]: 35320 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16929 : : {
16930 [ + + ]: 7199 : if (tbinfo->relkind == RELKIND_SEQUENCE)
16931 : 397 : dumpSequence(fout, tbinfo);
16932 : : else
16933 : 6802 : dumpTableSchema(fout, tbinfo);
16934 : : }
16935 : :
16936 : : /* Handle the ACL here */
16937 : 35320 : namecopy = pg_strdup(fmtId(tbinfo->dobj.name));
16938 [ + + ]: 35320 : if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
16939 : : {
16940 : 28874 : const char *objtype =
16941 [ + + ]: 28874 : (tbinfo->relkind == RELKIND_SEQUENCE) ? "SEQUENCE" : "TABLE";
16942 : :
16943 : : tableAclDumpId =
16944 : 28874 : dumpACL(fout, tbinfo->dobj.dumpId, InvalidDumpId,
16945 : : objtype, namecopy, NULL,
16946 : 28874 : tbinfo->dobj.namespace->dobj.name,
16947 : 28874 : NULL, tbinfo->rolname, &tbinfo->dacl);
16948 : : }
16949 : :
16950 : : /*
16951 : : * Handle column ACLs, if any. Note: we pull these with a separate query
16952 : : * rather than trying to fetch them during getTableAttrs, so that we won't
16953 : : * miss ACLs on system columns. Doing it this way also allows us to dump
16954 : : * ACLs for catalogs that we didn't mark "interesting" back in getTables.
16955 : : */
16956 [ + + + + ]: 35320 : if ((tbinfo->dobj.dump & DUMP_COMPONENT_ACL) && tbinfo->hascolumnACLs)
16957 : : {
16958 : 315 : PQExpBuffer query = createPQExpBuffer();
16959 : : PGresult *res;
16960 : : int i;
16961 : :
16962 [ + + ]: 315 : if (!fout->is_prepared[PREPQUERY_GETCOLUMNACLS])
16963 : : {
16964 : : /* Set up query for column ACLs */
16965 : 188 : appendPQExpBufferStr(query,
16966 : : "PREPARE getColumnACLs(pg_catalog.oid) AS\n");
16967 : :
16968 : : /*
16969 : : * In principle we should call acldefault('c', relowner) to get
16970 : : * the default ACL for a column. However, we don't currently
16971 : : * store the numeric OID of the relowner in TableInfo. We could
16972 : : * convert the owner name using regrole, but that creates a risk
16973 : : * of failure due to concurrent role renames. Given that the
16974 : : * default ACL for columns is empty and is likely to stay that
16975 : : * way, it's not worth extra cycles and risk to avoid hard-wiring
16976 : : * that knowledge here.
16977 : : */
16978 : 188 : appendPQExpBufferStr(query,
16979 : : "SELECT at.attname, "
16980 : : "at.attacl, "
16981 : : "'{}' AS acldefault, "
16982 : : "pip.privtype, pip.initprivs "
16983 : : "FROM pg_catalog.pg_attribute at "
16984 : : "LEFT JOIN pg_catalog.pg_init_privs pip ON "
16985 : : "(at.attrelid = pip.objoid "
16986 : : "AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass "
16987 : : "AND at.attnum = pip.objsubid) "
16988 : : "WHERE at.attrelid = $1 AND "
16989 : : "NOT at.attisdropped "
16990 : : "AND (at.attacl IS NOT NULL OR pip.initprivs IS NOT NULL) "
16991 : : "ORDER BY at.attnum");
16992 : :
16993 : 188 : ExecuteSqlStatement(fout, query->data);
16994 : :
16995 : 188 : fout->is_prepared[PREPQUERY_GETCOLUMNACLS] = true;
16996 : : }
16997 : :
16998 : 315 : printfPQExpBuffer(query,
16999 : : "EXECUTE getColumnACLs('%u')",
17000 : 315 : tbinfo->dobj.catId.oid);
17001 : :
17002 : 315 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17003 : :
17004 [ + + ]: 5866 : for (i = 0; i < PQntuples(res); i++)
17005 : : {
17006 : 5551 : char *attname = PQgetvalue(res, i, 0);
17007 : 5551 : char *attacl = PQgetvalue(res, i, 1);
17008 : 5551 : char *acldefault = PQgetvalue(res, i, 2);
17009 : 5551 : char privtype = *(PQgetvalue(res, i, 3));
17010 : 5551 : char *initprivs = PQgetvalue(res, i, 4);
17011 : : DumpableAcl coldacl;
17012 : : char *attnamecopy;
17013 : :
17014 : 5551 : coldacl.acl = attacl;
17015 : 5551 : coldacl.acldefault = acldefault;
17016 : 5551 : coldacl.privtype = privtype;
17017 : 5551 : coldacl.initprivs = initprivs;
17018 : 5551 : attnamecopy = pg_strdup(fmtId(attname));
17019 : :
17020 : : /*
17021 : : * Column's GRANT type is always TABLE. Each column ACL depends
17022 : : * on the table-level ACL, since we can restore column ACLs in
17023 : : * parallel but the table-level ACL has to be done first.
17024 : : */
17025 : 5551 : dumpACL(fout, tbinfo->dobj.dumpId, tableAclDumpId,
17026 : : "TABLE", namecopy, attnamecopy,
17027 : 5551 : tbinfo->dobj.namespace->dobj.name,
17028 : 5551 : NULL, tbinfo->rolname, &coldacl);
17029 : 5551 : pg_free(attnamecopy);
17030 : : }
17031 : 315 : PQclear(res);
17032 : 315 : destroyPQExpBuffer(query);
17033 : : }
17034 : :
17035 : 35320 : pg_free(namecopy);
17036 : : }
17037 : :
17038 : : /*
17039 : : * Create the AS clause for a view or materialized view. The semicolon is
17040 : : * stripped because a materialized view must add a WITH NO DATA clause.
17041 : : *
17042 : : * This returns a new buffer which must be freed by the caller.
17043 : : */
17044 : : static PQExpBuffer
17045 : 930 : createViewAsClause(Archive *fout, const TableInfo *tbinfo)
17046 : : {
17047 : 930 : PQExpBuffer query = createPQExpBuffer();
17048 : 930 : PQExpBuffer result = createPQExpBuffer();
17049 : : PGresult *res;
17050 : : int len;
17051 : :
17052 : : /* Fetch the view definition */
17053 : 930 : appendPQExpBuffer(query,
17054 : : "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
17055 : 930 : tbinfo->dobj.catId.oid);
17056 : :
17057 : 930 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17058 : :
17059 [ - + ]: 930 : if (PQntuples(res) != 1)
17060 : : {
17061 [ # # ]: 0 : if (PQntuples(res) < 1)
17062 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned no data",
17063 : : tbinfo->dobj.name);
17064 : : else
17065 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
17066 : : tbinfo->dobj.name);
17067 : : }
17068 : :
17069 : 930 : len = PQgetlength(res, 0, 0);
17070 : :
17071 [ - + ]: 930 : if (len == 0)
17072 : 0 : pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
17073 : : tbinfo->dobj.name);
17074 : :
17075 : : /* Strip off the trailing semicolon so that other things may follow. */
17076 : : Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
17077 : 930 : appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
17078 : :
17079 : 930 : PQclear(res);
17080 : 930 : destroyPQExpBuffer(query);
17081 : :
17082 : 930 : return result;
17083 : : }
17084 : :
17085 : : /*
17086 : : * Create a dummy AS clause for a view. This is used when the real view
17087 : : * definition has to be postponed because of circular dependencies.
17088 : : * We must duplicate the view's external properties -- column names and types
17089 : : * (including collation) -- so that it works for subsequent references.
17090 : : *
17091 : : * This returns a new buffer which must be freed by the caller.
17092 : : */
17093 : : static PQExpBuffer
17094 : 48 : createDummyViewAsClause(Archive *fout, const TableInfo *tbinfo)
17095 : : {
17096 : 48 : PQExpBuffer result = createPQExpBuffer();
17097 : : int j;
17098 : :
17099 : 48 : appendPQExpBufferStr(result, "SELECT");
17100 : :
17101 [ + + ]: 122 : for (j = 0; j < tbinfo->numatts; j++)
17102 : : {
17103 [ + + ]: 74 : if (j > 0)
17104 : 36 : appendPQExpBufferChar(result, ',');
17105 : 74 : appendPQExpBufferStr(result, "\n ");
17106 : :
17107 : 74 : appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
17108 : :
17109 : : /*
17110 : : * Must add collation if not default for the type, because CREATE OR
17111 : : * REPLACE VIEW won't change it
17112 : : */
17113 [ - + ]: 74 : if (OidIsValid(tbinfo->attcollation[j]))
17114 : : {
17115 : : CollInfo *coll;
17116 : :
17117 : 0 : coll = findCollationByOid(tbinfo->attcollation[j]);
17118 [ # # ]: 0 : if (coll)
17119 : 0 : appendPQExpBuffer(result, " COLLATE %s",
17120 : 0 : fmtQualifiedDumpable(coll));
17121 : : }
17122 : :
17123 : 74 : appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
17124 : : }
17125 : :
17126 : 48 : return result;
17127 : : }
17128 : :
17129 : : /*
17130 : : * dumpTableSchema
17131 : : * write the declaration (not data) of one user-defined table or view
17132 : : */
17133 : : static void
17134 : 6802 : dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
17135 : : {
17136 : 6802 : DumpOptions *dopt = fout->dopt;
17137 : 6802 : PQExpBuffer q = createPQExpBuffer();
17138 : 6802 : PQExpBuffer delq = createPQExpBuffer();
17139 : 6802 : PQExpBuffer extra = createPQExpBuffer();
17140 : : char *qrelname;
17141 : : char *qualrelname;
17142 : : int numParents;
17143 : : TableInfo **parents;
17144 : : int actual_atts; /* number of attrs in this CREATE statement */
17145 : : const char *reltypename;
17146 : : char *storage;
17147 : : int j,
17148 : : k;
17149 : :
17150 : : /* We had better have loaded per-column details about this table */
17151 : : Assert(tbinfo->interesting);
17152 : :
17153 : 6802 : qrelname = pg_strdup(fmtId(tbinfo->dobj.name));
17154 : 6802 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
17155 : :
17156 [ - + ]: 6802 : if (tbinfo->hasoids)
17157 : 0 : pg_log_warning("WITH OIDS is not supported anymore (table \"%s\")",
17158 : : qrelname);
17159 : :
17160 [ + + ]: 6802 : if (dopt->binary_upgrade)
17161 : : {
17162 : 925 : binary_upgrade_set_type_oids_by_rel(fout, q, tbinfo);
17163 : :
17164 : : /* only relations with a row type have versioned attributes */
17165 [ + + + + : 925 : if (RELKIND_HAS_ROWTYPE(tbinfo->relkind))
+ + + - +
+ + - ]
17166 : 925 : binary_upgrade_set_next_attribute_versions(q, tbinfo->numatts,
17167 : 925 : tbinfo->attversion);
17168 : : }
17169 : :
17170 : : /* Is it a table or a view? */
17171 [ + + ]: 6802 : if (tbinfo->relkind == RELKIND_VIEW)
17172 : : {
17173 : : PQExpBuffer result;
17174 : :
17175 : : /*
17176 : : * Note: keep this code in sync with the is_view case in dumpRule()
17177 : : */
17178 : :
17179 : 576 : reltypename = "VIEW";
17180 : :
17181 : 576 : appendPQExpBuffer(delq, "DROP VIEW %s;\n", qualrelname);
17182 : :
17183 [ + + ]: 576 : if (dopt->binary_upgrade)
17184 : 55 : binary_upgrade_set_pg_class_oids(fout, q,
17185 : 55 : tbinfo->dobj.catId.oid);
17186 : :
17187 : 576 : appendPQExpBuffer(q, "CREATE VIEW %s", qualrelname);
17188 : :
17189 [ + + ]: 576 : if (tbinfo->dummy_view)
17190 : 24 : result = createDummyViewAsClause(fout, tbinfo);
17191 : : else
17192 : : {
17193 [ + + ]: 552 : if (nonemptyReloptions(tbinfo->reloptions))
17194 : : {
17195 : 63 : appendPQExpBufferStr(q, " WITH (");
17196 : 63 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17197 : 63 : appendPQExpBufferChar(q, ')');
17198 : : }
17199 : 552 : result = createViewAsClause(fout, tbinfo);
17200 : : }
17201 : 576 : appendPQExpBuffer(q, " AS\n%s", result->data);
17202 : 576 : destroyPQExpBuffer(result);
17203 : :
17204 [ + + + - ]: 576 : if (tbinfo->checkoption != NULL && !tbinfo->dummy_view)
17205 : 34 : appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption);
17206 : 576 : appendPQExpBufferStr(q, ";\n");
17207 : : }
17208 : : else
17209 : : {
17210 : 6226 : char *partkeydef = NULL;
17211 : 6226 : char *ftoptions = NULL;
17212 : 6226 : char *srvname = NULL;
17213 : 6226 : const char *foreign = "";
17214 : :
17215 : : /*
17216 : : * Set reltypename, and collect any relkind-specific data that we
17217 : : * didn't fetch during getTables().
17218 : : */
17219 [ + + + + ]: 6226 : switch (tbinfo->relkind)
17220 : : {
17221 : 621 : case RELKIND_PARTITIONED_TABLE:
17222 : : {
17223 : 621 : PQExpBuffer query = createPQExpBuffer();
17224 : : PGresult *res;
17225 : :
17226 : 621 : reltypename = "TABLE";
17227 : :
17228 : : /* retrieve partition key definition */
17229 : 621 : appendPQExpBuffer(query,
17230 : : "SELECT pg_get_partkeydef('%u')",
17231 : 621 : tbinfo->dobj.catId.oid);
17232 : 621 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17233 : 621 : partkeydef = pg_strdup(PQgetvalue(res, 0, 0));
17234 : 621 : PQclear(res);
17235 : 621 : destroyPQExpBuffer(query);
17236 : 621 : break;
17237 : : }
17238 : 37 : case RELKIND_FOREIGN_TABLE:
17239 : : {
17240 : 37 : PQExpBuffer query = createPQExpBuffer();
17241 : : PGresult *res;
17242 : : int i_srvname;
17243 : : int i_ftoptions;
17244 : :
17245 : 37 : reltypename = "FOREIGN TABLE";
17246 : :
17247 : : /* retrieve name of foreign server and generic options */
17248 : 37 : appendPQExpBuffer(query,
17249 : : "SELECT fs.srvname, "
17250 : : "pg_catalog.array_to_string(ARRAY("
17251 : : "SELECT pg_catalog.quote_ident(option_name) || "
17252 : : "' ' || pg_catalog.quote_literal(option_value) "
17253 : : "FROM pg_catalog.pg_options_to_table(ftoptions) "
17254 : : "ORDER BY option_name"
17255 : : "), E',\n ') AS ftoptions "
17256 : : "FROM pg_catalog.pg_foreign_table ft "
17257 : : "JOIN pg_catalog.pg_foreign_server fs "
17258 : : "ON (fs.oid = ft.ftserver) "
17259 : : "WHERE ft.ftrelid = '%u'",
17260 : 37 : tbinfo->dobj.catId.oid);
17261 : 37 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17262 : 37 : i_srvname = PQfnumber(res, "srvname");
17263 : 37 : i_ftoptions = PQfnumber(res, "ftoptions");
17264 : 37 : srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
17265 : 37 : ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
17266 : 37 : PQclear(res);
17267 : 37 : destroyPQExpBuffer(query);
17268 : :
17269 : 37 : foreign = "FOREIGN ";
17270 : 37 : break;
17271 : : }
17272 : 354 : case RELKIND_MATVIEW:
17273 : 354 : reltypename = "MATERIALIZED VIEW";
17274 : 354 : break;
17275 : 5214 : default:
17276 : 5214 : reltypename = "TABLE";
17277 : 5214 : break;
17278 : : }
17279 : :
17280 : 6226 : numParents = tbinfo->numParents;
17281 : 6226 : parents = tbinfo->parents;
17282 : :
17283 : 6226 : appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname);
17284 : :
17285 [ + + ]: 6226 : if (dopt->binary_upgrade)
17286 : 870 : binary_upgrade_set_pg_class_oids(fout, q,
17287 : 870 : tbinfo->dobj.catId.oid);
17288 : :
17289 : : /*
17290 : : * PostgreSQL 18 has disabled UNLOGGED for partitioned tables, so
17291 : : * ignore it when dumping if it was set in this case.
17292 : : */
17293 : 6226 : appendPQExpBuffer(q, "CREATE %s%s %s",
17294 [ + + ]: 6226 : (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
17295 [ + - ]: 20 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ?
17296 : : "UNLOGGED " : "",
17297 : : reltypename,
17298 : : qualrelname);
17299 : :
17300 : : /*
17301 : : * Attach to type, if reloftype; except in case of a binary upgrade,
17302 : : * we dump the table normally and attach it to the type afterward.
17303 : : */
17304 [ + + + + ]: 6226 : if (OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade)
17305 : 24 : appendPQExpBuffer(q, " OF %s",
17306 : 24 : getFormattedTypeName(fout, tbinfo->reloftype,
17307 : : zeroIsError));
17308 : :
17309 [ + + ]: 6226 : if (tbinfo->relkind != RELKIND_MATVIEW)
17310 : : {
17311 : : /* Dump the attributes */
17312 : 5872 : actual_atts = 0;
17313 [ + + ]: 26920 : for (j = 0; j < tbinfo->numatts; j++)
17314 : : {
17315 : : /*
17316 : : * Normally, dump if it's locally defined in this table, and
17317 : : * not dropped. But for binary upgrade, we'll dump all the
17318 : : * columns, and then fix up the dropped and nonlocal cases
17319 : : * below.
17320 : : */
17321 [ + + ]: 21048 : if (shouldPrintColumn(dopt, tbinfo, j))
17322 : : {
17323 : : bool print_default;
17324 : : bool print_notnull;
17325 : :
17326 : : /*
17327 : : * Default value --- suppress if to be printed separately
17328 : : * or not at all.
17329 : : */
17330 : 40938 : print_default = (tbinfo->attrdefs[j] != NULL &&
17331 [ + + + + ]: 20997 : tbinfo->attrdefs[j]->dobj.dump &&
17332 [ + + ]: 1105 : !tbinfo->attrdefs[j]->separate);
17333 : :
17334 : : /*
17335 : : * Not Null constraint --- print it if it is locally
17336 : : * defined, or if binary upgrade. (In the latter case, we
17337 : : * reset conislocal below.)
17338 : : */
17339 [ + + ]: 22269 : print_notnull = (tbinfo->notnull_constrs[j] != NULL &&
17340 [ + + ]: 2377 : (tbinfo->notnull_islocal[j] ||
17341 [ + + ]: 655 : dopt->binary_upgrade ||
17342 [ + + ]: 565 : tbinfo->ispartition));
17343 : :
17344 : : /*
17345 : : * Skip column if fully defined by reloftype, except in
17346 : : * binary upgrade
17347 : : */
17348 [ + + ]: 19892 : if (OidIsValid(tbinfo->reloftype) &&
17349 [ + + + + ]: 53 : !print_default && !print_notnull &&
17350 [ + + ]: 33 : !dopt->binary_upgrade)
17351 : 24 : continue;
17352 : :
17353 : : /* Format properly if not first attr */
17354 [ + + ]: 19868 : if (actual_atts == 0)
17355 : 5497 : appendPQExpBufferStr(q, " (");
17356 : : else
17357 : 14371 : appendPQExpBufferChar(q, ',');
17358 : 19868 : appendPQExpBufferStr(q, "\n ");
17359 : 19868 : actual_atts++;
17360 : :
17361 : : /* Attribute name */
17362 : 19868 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j]));
17363 : :
17364 [ + + ]: 19868 : if (tbinfo->attisdropped[j])
17365 : : {
17366 : : /*
17367 : : * ALTER TABLE DROP COLUMN clears
17368 : : * pg_attribute.atttypid, so we will not have gotten a
17369 : : * valid type name; insert INTEGER as a stopgap. We'll
17370 : : * clean things up later.
17371 : : */
17372 : 94 : appendPQExpBufferStr(q, " INTEGER /* dummy */");
17373 : : /* and skip to the next column */
17374 : 94 : continue;
17375 : : }
17376 : :
17377 : : /*
17378 : : * Attribute type; print it except when creating a typed
17379 : : * table ('OF type_name'), but in binary-upgrade mode,
17380 : : * print it in that case too.
17381 : : */
17382 [ + + + + ]: 19774 : if (dopt->binary_upgrade || !OidIsValid(tbinfo->reloftype))
17383 : : {
17384 : 19758 : appendPQExpBuffer(q, " %s",
17385 : 19758 : tbinfo->atttypnames[j]);
17386 : : }
17387 : :
17388 [ + + ]: 19774 : if (print_default)
17389 : : {
17390 [ + + ]: 971 : if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED)
17391 : 328 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s) STORED",
17392 : 328 : tbinfo->attrdefs[j]->adef_expr);
17393 [ + + ]: 643 : else if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_VIRTUAL)
17394 : 262 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s)",
17395 : 262 : tbinfo->attrdefs[j]->adef_expr);
17396 : : else
17397 : 381 : appendPQExpBuffer(q, " DEFAULT %s",
17398 : 381 : tbinfo->attrdefs[j]->adef_expr);
17399 : : }
17400 : :
17401 [ + + ]: 19774 : if (print_notnull)
17402 : : {
17403 [ + + ]: 2344 : if (tbinfo->notnull_constrs[j][0] == '\0')
17404 : 1661 : appendPQExpBufferStr(q, " NOT NULL");
17405 : : else
17406 : 683 : appendPQExpBuffer(q, " CONSTRAINT %s NOT NULL",
17407 : 683 : fmtId(tbinfo->notnull_constrs[j]));
17408 : :
17409 [ + + ]: 2344 : if (tbinfo->notnull_noinh[j])
17410 : 35 : appendPQExpBufferStr(q, " NO INHERIT");
17411 : : }
17412 : :
17413 : : /* Add collation if not default for the type */
17414 [ + + ]: 19774 : if (OidIsValid(tbinfo->attcollation[j]))
17415 : : {
17416 : : CollInfo *coll;
17417 : :
17418 : 203 : coll = findCollationByOid(tbinfo->attcollation[j]);
17419 [ + - ]: 203 : if (coll)
17420 : 203 : appendPQExpBuffer(q, " COLLATE %s",
17421 : 203 : fmtQualifiedDumpable(coll));
17422 : : }
17423 : : }
17424 : :
17425 : : /*
17426 : : * On the other hand, if we choose not to print a column
17427 : : * (likely because it is created by inheritance), but the
17428 : : * column has a locally-defined not-null constraint, we need
17429 : : * to dump the constraint as a standalone object.
17430 : : *
17431 : : * This syntax isn't SQL-conforming, but if you wanted
17432 : : * standard output you wouldn't be creating non-standard
17433 : : * objects to begin with.
17434 : : */
17435 [ + + ]: 20930 : if (!shouldPrintColumn(dopt, tbinfo, j) &&
17436 [ + + ]: 1156 : !tbinfo->attisdropped[j] &&
17437 [ + + ]: 700 : tbinfo->notnull_constrs[j] != NULL &&
17438 [ + + ]: 216 : tbinfo->notnull_islocal[j])
17439 : : {
17440 : : /* Format properly if not first attr */
17441 [ + + ]: 94 : if (actual_atts == 0)
17442 : 90 : appendPQExpBufferStr(q, " (");
17443 : : else
17444 : 4 : appendPQExpBufferChar(q, ',');
17445 : 94 : appendPQExpBufferStr(q, "\n ");
17446 : 94 : actual_atts++;
17447 : :
17448 [ + + ]: 94 : if (tbinfo->notnull_constrs[j][0] != '\0')
17449 : 86 : appendPQExpBuffer(q, "CONSTRAINT %s ",
17450 : 86 : fmtId(tbinfo->notnull_constrs[j]));
17451 : 94 : appendPQExpBuffer(q, "NOT NULL %s",
17452 : 94 : fmtId(tbinfo->attnames[j]));
17453 : :
17454 [ + + ]: 94 : if (tbinfo->notnull_noinh[j])
17455 : 33 : appendPQExpBufferStr(q, " NO INHERIT");
17456 : : }
17457 : : }
17458 : :
17459 : : /*
17460 : : * Add non-inherited CHECK constraints, if any.
17461 : : *
17462 : : * For partitions, we need to include check constraints even if
17463 : : * they're not defined locally, because the ALTER TABLE ATTACH
17464 : : * PARTITION that we'll emit later expects the constraint to be
17465 : : * there. (No need to fix conislocal: ATTACH PARTITION does that)
17466 : : */
17467 [ + + ]: 6465 : for (j = 0; j < tbinfo->ncheck; j++)
17468 : : {
17469 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
17470 : :
17471 [ + + ]: 593 : if (constr->separate ||
17472 [ + + + + ]: 523 : (!constr->conislocal && !tbinfo->ispartition))
17473 : 109 : continue;
17474 : :
17475 [ + + ]: 484 : if (actual_atts == 0)
17476 : 16 : appendPQExpBufferStr(q, " (\n ");
17477 : : else
17478 : 468 : appendPQExpBufferStr(q, ",\n ");
17479 : :
17480 : 484 : appendPQExpBuffer(q, "CONSTRAINT %s ",
17481 : 484 : fmtId(constr->dobj.name));
17482 : 484 : appendPQExpBufferStr(q, constr->condef);
17483 : :
17484 : 484 : actual_atts++;
17485 : : }
17486 : :
17487 [ + + ]: 5872 : if (actual_atts)
17488 : 5603 : appendPQExpBufferStr(q, "\n)");
17489 [ + + - + ]: 269 : else if (!(OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade))
17490 : : {
17491 : : /*
17492 : : * No attributes? we must have a parenthesized attribute list,
17493 : : * even though empty, when not using the OF TYPE syntax.
17494 : : */
17495 : 257 : appendPQExpBufferStr(q, " (\n)");
17496 : : }
17497 : :
17498 : : /*
17499 : : * Emit the INHERITS clause (not for partitions), except in
17500 : : * binary-upgrade mode.
17501 : : */
17502 [ + + + + ]: 5872 : if (numParents > 0 && !tbinfo->ispartition &&
17503 [ + + ]: 536 : !dopt->binary_upgrade)
17504 : : {
17505 : 470 : appendPQExpBufferStr(q, "\nINHERITS (");
17506 [ + + ]: 1013 : for (k = 0; k < numParents; k++)
17507 : : {
17508 : 543 : TableInfo *parentRel = parents[k];
17509 : :
17510 [ + + ]: 543 : if (k > 0)
17511 : 73 : appendPQExpBufferStr(q, ", ");
17512 : 543 : appendPQExpBufferStr(q, fmtQualifiedDumpable(parentRel));
17513 : : }
17514 : 470 : appendPQExpBufferChar(q, ')');
17515 : : }
17516 : :
17517 [ + + ]: 5872 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
17518 : 621 : appendPQExpBuffer(q, "\nPARTITION BY %s", partkeydef);
17519 : :
17520 [ + + ]: 5872 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
17521 : 37 : appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
17522 : : }
17523 : :
17524 [ + + - + ]: 12297 : if (nonemptyReloptions(tbinfo->reloptions) ||
17525 : 6071 : nonemptyReloptions(tbinfo->toast_reloptions))
17526 : : {
17527 : 155 : bool addcomma = false;
17528 : :
17529 : 155 : appendPQExpBufferStr(q, "\nWITH (");
17530 [ + - ]: 155 : if (nonemptyReloptions(tbinfo->reloptions))
17531 : : {
17532 : 155 : addcomma = true;
17533 : 155 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17534 : : }
17535 [ + + ]: 155 : if (nonemptyReloptions(tbinfo->toast_reloptions))
17536 : : {
17537 [ + - ]: 5 : if (addcomma)
17538 : 5 : appendPQExpBufferStr(q, ", ");
17539 : 5 : appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.",
17540 : : fout);
17541 : : }
17542 : 155 : appendPQExpBufferChar(q, ')');
17543 : : }
17544 : :
17545 : : /* Dump generic options if any */
17546 [ + + + + ]: 6226 : if (ftoptions && ftoptions[0])
17547 : 34 : appendPQExpBuffer(q, "\nOPTIONS (\n %s\n)", ftoptions);
17548 : :
17549 : : /*
17550 : : * For materialized views, create the AS clause just like a view. At
17551 : : * this point, we always mark the view as not populated.
17552 : : */
17553 [ + + ]: 6226 : if (tbinfo->relkind == RELKIND_MATVIEW)
17554 : : {
17555 : : PQExpBuffer result;
17556 : :
17557 : 354 : result = createViewAsClause(fout, tbinfo);
17558 : 354 : appendPQExpBuffer(q, " AS\n%s\n WITH NO DATA;\n",
17559 : : result->data);
17560 : 354 : destroyPQExpBuffer(result);
17561 : : }
17562 : : else
17563 : 5872 : appendPQExpBufferStr(q, ";\n");
17564 : :
17565 : : /* Materialized views can depend on extensions */
17566 [ + + ]: 6226 : if (tbinfo->relkind == RELKIND_MATVIEW)
17567 : 354 : append_depends_on_extension(fout, q, &tbinfo->dobj,
17568 : : "pg_catalog.pg_class",
17569 : : "MATERIALIZED VIEW",
17570 : : qualrelname);
17571 : :
17572 : : /*
17573 : : * in binary upgrade mode, update the catalog with any missing values
17574 : : * that might be present.
17575 : : */
17576 [ + + ]: 6226 : if (dopt->binary_upgrade)
17577 : : {
17578 [ + + ]: 4143 : for (j = 0; j < tbinfo->numatts; j++)
17579 : : {
17580 [ + + ]: 3273 : if (tbinfo->attmissingval[j][0] != '\0')
17581 : : {
17582 : 3 : appendPQExpBufferStr(q, "\n-- set missing value.\n");
17583 : 3 : appendPQExpBufferStr(q,
17584 : : "SELECT pg_catalog.binary_upgrade_set_missing_value(");
17585 : 3 : appendStringLiteralAH(q, qualrelname, fout);
17586 : 3 : appendPQExpBufferStr(q, "::pg_catalog.regclass,");
17587 : 3 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17588 : 3 : appendPQExpBufferChar(q, ',');
17589 : 3 : appendStringLiteralAH(q, tbinfo->attmissingval[j], fout);
17590 : 3 : appendPQExpBufferStr(q, ");\n\n");
17591 : : }
17592 : : }
17593 : : }
17594 : :
17595 : : /*
17596 : : * To create binary-compatible heap files, we have to ensure the same
17597 : : * physical column order, including dropped columns, as in the
17598 : : * original. Therefore, we create dropped columns above and drop them
17599 : : * here, also updating their attlen/attalign values so that the
17600 : : * dropped column can be skipped properly. (We do not bother with
17601 : : * restoring the original attbyval setting.) Also, inheritance
17602 : : * relationships are set up by doing ALTER TABLE INHERIT rather than
17603 : : * using an INHERITS clause --- the latter would possibly mess up the
17604 : : * column order. That also means we have to take care about setting
17605 : : * attislocal correctly, plus fix up any inherited CHECK constraints.
17606 : : * Analogously, we set up typed tables using ALTER TABLE / OF here.
17607 : : *
17608 : : * We process foreign and partitioned tables here, even though they
17609 : : * lack heap storage, because they can participate in inheritance
17610 : : * relationships and we want this stuff to be consistent across the
17611 : : * inheritance tree. We can exclude indexes, toast tables, sequences
17612 : : * and matviews, even though they have storage, because we don't
17613 : : * support altering or dropping columns in them, nor can they be part
17614 : : * of inheritance trees.
17615 : : */
17616 [ + + ]: 6226 : if (dopt->binary_upgrade &&
17617 [ + + ]: 870 : (tbinfo->relkind == RELKIND_RELATION ||
17618 [ + + ]: 118 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
17619 [ + + ]: 116 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE))
17620 : : {
17621 : : bool firstitem;
17622 : : bool firstitem_extra;
17623 : :
17624 : : /*
17625 : : * Drop any dropped columns. Merge the pg_attribute manipulations
17626 : : * into a single SQL command, so that we don't cause repeated
17627 : : * relcache flushes on the target table. Otherwise we risk O(N^2)
17628 : : * relcache bloat while dropping N columns.
17629 : : */
17630 : 852 : resetPQExpBuffer(extra);
17631 : 852 : firstitem = true;
17632 [ + + ]: 4102 : for (j = 0; j < tbinfo->numatts; j++)
17633 : : {
17634 [ + + ]: 3250 : if (tbinfo->attisdropped[j])
17635 : : {
17636 [ + + ]: 94 : if (firstitem)
17637 : : {
17638 : 46 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped columns.\n"
17639 : : "UPDATE pg_catalog.pg_attribute\n"
17640 : : "SET attlen = v.dlen, "
17641 : : "attalign = v.dalign, "
17642 : : "attbyval = false\n"
17643 : : "FROM (VALUES ");
17644 : 46 : firstitem = false;
17645 : : }
17646 : : else
17647 : 48 : appendPQExpBufferStr(q, ",\n ");
17648 : 94 : appendPQExpBufferChar(q, '(');
17649 : 94 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17650 : 94 : appendPQExpBuffer(q, ", %d, '%c')",
17651 : 94 : tbinfo->attlen[j],
17652 : 94 : tbinfo->attalign[j]);
17653 : : /* The ALTER ... DROP COLUMN commands must come after */
17654 : 94 : appendPQExpBuffer(extra, "ALTER %sTABLE ONLY %s ",
17655 : : foreign, qualrelname);
17656 : 94 : appendPQExpBuffer(extra, "DROP COLUMN %s;\n",
17657 : 94 : fmtId(tbinfo->attnames[j]));
17658 : : }
17659 : : }
17660 [ + + ]: 852 : if (!firstitem)
17661 : : {
17662 : 46 : appendPQExpBufferStr(q, ") v(dname, dlen, dalign)\n"
17663 : : "WHERE attrelid = ");
17664 : 46 : appendStringLiteralAH(q, qualrelname, fout);
17665 : 46 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17666 : : " AND attname = v.dname;\n");
17667 : : /* Now we can issue the actual DROP COLUMN commands */
17668 : 46 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17669 : : }
17670 : :
17671 : : /*
17672 : : * Fix up inherited columns. As above, do the pg_attribute
17673 : : * manipulations in a single SQL command.
17674 : : */
17675 : 852 : firstitem = true;
17676 [ + + ]: 4102 : for (j = 0; j < tbinfo->numatts; j++)
17677 : : {
17678 [ + + ]: 3250 : if (!tbinfo->attisdropped[j] &&
17679 [ + + ]: 3156 : !tbinfo->attislocal[j])
17680 : : {
17681 [ + + ]: 644 : if (firstitem)
17682 : : {
17683 : 282 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited columns.\n");
17684 : 282 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n"
17685 : : "SET attislocal = false\n"
17686 : : "WHERE attrelid = ");
17687 : 282 : appendStringLiteralAH(q, qualrelname, fout);
17688 : 282 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17689 : : " AND attname IN (");
17690 : 282 : firstitem = false;
17691 : : }
17692 : : else
17693 : 362 : appendPQExpBufferStr(q, ", ");
17694 : 644 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17695 : : }
17696 : : }
17697 [ + + ]: 852 : if (!firstitem)
17698 : 282 : appendPQExpBufferStr(q, ");\n");
17699 : :
17700 : : /*
17701 : : * Fix up not-null constraints that come from inheritance. As
17702 : : * above, do the pg_constraint manipulations in a single SQL
17703 : : * command. (Actually, two in special cases, if we're doing an
17704 : : * upgrade from < 18).
17705 : : */
17706 : 852 : firstitem = true;
17707 : 852 : firstitem_extra = true;
17708 : 852 : resetPQExpBuffer(extra);
17709 [ + + ]: 4102 : for (j = 0; j < tbinfo->numatts; j++)
17710 : : {
17711 : : /*
17712 : : * If a not-null constraint comes from inheritance, reset
17713 : : * conislocal. The inhcount is fixed by ALTER TABLE INHERIT,
17714 : : * below. Special hack: in versions < 18, columns with no
17715 : : * local definition need their constraint to be matched by
17716 : : * column number in conkeys instead of by constraint name,
17717 : : * because the latter is not available. (We distinguish the
17718 : : * case because the constraint name is the empty string.)
17719 : : */
17720 [ + + ]: 3250 : if (tbinfo->notnull_constrs[j] != NULL &&
17721 [ + + ]: 304 : !tbinfo->notnull_islocal[j])
17722 : : {
17723 [ + + ]: 90 : if (tbinfo->notnull_constrs[j][0] != '\0')
17724 : : {
17725 [ + + ]: 77 : if (firstitem)
17726 : : {
17727 : 65 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
17728 : : "SET conislocal = false\n"
17729 : : "WHERE contype = 'n' AND conrelid = ");
17730 : 65 : appendStringLiteralAH(q, qualrelname, fout);
17731 : 65 : appendPQExpBufferStr(q, "::pg_catalog.regclass AND\n"
17732 : : "conname IN (");
17733 : 65 : firstitem = false;
17734 : : }
17735 : : else
17736 : 12 : appendPQExpBufferStr(q, ", ");
17737 : 77 : appendStringLiteralAH(q, tbinfo->notnull_constrs[j], fout);
17738 : : }
17739 : : else
17740 : : {
17741 [ + - ]: 13 : if (firstitem_extra)
17742 : : {
17743 : 13 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17744 : : "SET conislocal = false\n"
17745 : : "WHERE contype = 'n' AND conrelid = ");
17746 : 13 : appendStringLiteralAH(extra, qualrelname, fout);
17747 : 13 : appendPQExpBufferStr(extra, "::pg_catalog.regclass AND\n"
17748 : : "conkey IN (");
17749 : 13 : firstitem_extra = false;
17750 : : }
17751 : : else
17752 : 0 : appendPQExpBufferStr(extra, ", ");
17753 : 13 : appendPQExpBuffer(extra, "'{%d}'", j + 1);
17754 : : }
17755 : : }
17756 : : }
17757 [ + + ]: 852 : if (!firstitem)
17758 : 65 : appendPQExpBufferStr(q, ");\n");
17759 [ + + ]: 852 : if (!firstitem_extra)
17760 : 13 : appendPQExpBufferStr(extra, ");\n");
17761 : :
17762 [ + + ]: 852 : if (extra->len > 0)
17763 : 13 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17764 : :
17765 : : /*
17766 : : * Add inherited CHECK constraints, if any.
17767 : : *
17768 : : * For partitions, they were already dumped, and conislocal
17769 : : * doesn't need fixing.
17770 : : *
17771 : : * As above, issue only one direct manipulation of pg_constraint.
17772 : : * Although it is tempting to merge the ALTER ADD CONSTRAINT
17773 : : * commands into one as well, refrain for now due to concern about
17774 : : * possible backend memory bloat if there are many such
17775 : : * constraints.
17776 : : */
17777 : 852 : resetPQExpBuffer(extra);
17778 : 852 : firstitem = true;
17779 [ + + ]: 914 : for (k = 0; k < tbinfo->ncheck; k++)
17780 : : {
17781 : 62 : ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
17782 : :
17783 [ + + + + : 62 : if (constr->separate || constr->conislocal || tbinfo->ispartition)
+ + ]
17784 : 60 : continue;
17785 : :
17786 [ + - ]: 2 : if (firstitem)
17787 : 2 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraints.\n");
17788 : 2 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n",
17789 : : foreign, qualrelname,
17790 : 2 : fmtId(constr->dobj.name),
17791 : : constr->condef);
17792 : : /* Update pg_constraint after all the ALTER TABLEs */
17793 [ + - ]: 2 : if (firstitem)
17794 : : {
17795 : 2 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17796 : : "SET conislocal = false\n"
17797 : : "WHERE contype = 'c' AND conrelid = ");
17798 : 2 : appendStringLiteralAH(extra, qualrelname, fout);
17799 : 2 : appendPQExpBufferStr(extra, "::pg_catalog.regclass\n");
17800 : 2 : appendPQExpBufferStr(extra, " AND conname IN (");
17801 : 2 : firstitem = false;
17802 : : }
17803 : : else
17804 : 0 : appendPQExpBufferStr(extra, ", ");
17805 : 2 : appendStringLiteralAH(extra, constr->dobj.name, fout);
17806 : : }
17807 [ + + ]: 852 : if (!firstitem)
17808 : : {
17809 : 2 : appendPQExpBufferStr(extra, ");\n");
17810 : 2 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17811 : : }
17812 : :
17813 [ + + + + ]: 852 : if (numParents > 0 && !tbinfo->ispartition)
17814 : : {
17815 : 66 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
17816 [ + + ]: 143 : for (k = 0; k < numParents; k++)
17817 : : {
17818 : 77 : TableInfo *parentRel = parents[k];
17819 : :
17820 : 77 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s INHERIT %s;\n", foreign,
17821 : : qualrelname,
17822 : 77 : fmtQualifiedDumpable(parentRel));
17823 : : }
17824 : : }
17825 : :
17826 [ + + ]: 852 : if (OidIsValid(tbinfo->reloftype))
17827 : : {
17828 : 7 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n");
17829 : 7 : appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
17830 : : qualrelname,
17831 : 7 : getFormattedTypeName(fout, tbinfo->reloftype,
17832 : : zeroIsError));
17833 : : }
17834 : : }
17835 : :
17836 : : /*
17837 : : * In binary_upgrade mode, arrange to restore the old relfrozenxid and
17838 : : * relminmxid of all vacuumable relations. (While vacuum.c processes
17839 : : * TOAST tables semi-independently, here we see them only as children
17840 : : * of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the
17841 : : * child toast table is handled below.)
17842 : : */
17843 [ + + ]: 6226 : if (dopt->binary_upgrade &&
17844 [ + + ]: 870 : (tbinfo->relkind == RELKIND_RELATION ||
17845 [ + + ]: 118 : tbinfo->relkind == RELKIND_MATVIEW))
17846 : : {
17847 : 770 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
17848 : 770 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17849 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17850 : : "WHERE oid = ",
17851 : 770 : tbinfo->frozenxid, tbinfo->minmxid);
17852 : 770 : appendStringLiteralAH(q, qualrelname, fout);
17853 : 770 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17854 : :
17855 [ + + ]: 770 : if (tbinfo->toast_oid)
17856 : : {
17857 : : /*
17858 : : * The toast table will have the same OID at restore, so we
17859 : : * can safely target it by OID.
17860 : : */
17861 : 298 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
17862 : 298 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17863 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17864 : : "WHERE oid = '%u';\n",
17865 : 298 : tbinfo->toast_frozenxid,
17866 : 298 : tbinfo->toast_minmxid, tbinfo->toast_oid);
17867 : : }
17868 : : }
17869 : :
17870 : : /*
17871 : : * In binary_upgrade mode, restore matviews' populated status by
17872 : : * poking pg_class directly. This is pretty ugly, but we can't use
17873 : : * REFRESH MATERIALIZED VIEW since it's possible that some underlying
17874 : : * matview is not populated even though this matview is; in any case,
17875 : : * we want to transfer the matview's heap storage, not run REFRESH.
17876 : : */
17877 [ + + + + ]: 6226 : if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
17878 [ + + ]: 18 : tbinfo->relispopulated)
17879 : : {
17880 : 16 : appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n");
17881 : 16 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n"
17882 : : "SET relispopulated = 't'\n"
17883 : : "WHERE oid = ");
17884 : 16 : appendStringLiteralAH(q, qualrelname, fout);
17885 : 16 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17886 : : }
17887 : :
17888 : : /*
17889 : : * Dump additional per-column properties that we can't handle in the
17890 : : * main CREATE TABLE command.
17891 : : */
17892 [ + + ]: 27707 : for (j = 0; j < tbinfo->numatts; j++)
17893 : : {
17894 : : /* None of this applies to dropped columns */
17895 [ + + ]: 21481 : if (tbinfo->attisdropped[j])
17896 : 550 : continue;
17897 : :
17898 : : /*
17899 : : * Dump per-column statistics information. We only issue an ALTER
17900 : : * TABLE statement if the attstattarget entry for this column is
17901 : : * not the default value.
17902 : : */
17903 [ + + ]: 20931 : if (tbinfo->attstattarget[j] >= 0)
17904 : 34 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n",
17905 : : foreign, qualrelname,
17906 : 34 : fmtId(tbinfo->attnames[j]),
17907 : 34 : tbinfo->attstattarget[j]);
17908 : :
17909 : : /*
17910 : : * Dump per-column storage information. The statement is only
17911 : : * dumped if the storage has been changed from the type's default.
17912 : : */
17913 [ + + ]: 20931 : if (tbinfo->attstorage[j] != tbinfo->typstorage[j])
17914 : : {
17915 [ + + - + : 83 : switch (tbinfo->attstorage[j])
- ]
17916 : : {
17917 : 10 : case TYPSTORAGE_PLAIN:
17918 : 10 : storage = "PLAIN";
17919 : 10 : break;
17920 : 39 : case TYPSTORAGE_EXTERNAL:
17921 : 39 : storage = "EXTERNAL";
17922 : 39 : break;
17923 : 0 : case TYPSTORAGE_EXTENDED:
17924 : 0 : storage = "EXTENDED";
17925 : 0 : break;
17926 : 34 : case TYPSTORAGE_MAIN:
17927 : 34 : storage = "MAIN";
17928 : 34 : break;
17929 : 0 : default:
17930 : 0 : storage = NULL;
17931 : : }
17932 : :
17933 : : /*
17934 : : * Only dump the statement if it's a storage type we recognize
17935 : : */
17936 [ + - ]: 83 : if (storage != NULL)
17937 : 83 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n",
17938 : : foreign, qualrelname,
17939 : 83 : fmtId(tbinfo->attnames[j]),
17940 : : storage);
17941 : : }
17942 : :
17943 : : /*
17944 : : * Dump per-column compression, if it's been set.
17945 : : */
17946 [ + + ]: 20931 : if (!dopt->no_toast_compression)
17947 : : {
17948 : : const char *cmname;
17949 : :
17950 [ + + + ]: 20826 : switch (tbinfo->attcompression[j])
17951 : : {
17952 : 73 : case 'p':
17953 : 73 : cmname = "pglz";
17954 : 73 : break;
17955 : 39 : case 'l':
17956 : 39 : cmname = "lz4";
17957 : 39 : break;
17958 : 20714 : default:
17959 : 20714 : cmname = NULL;
17960 : 20714 : break;
17961 : : }
17962 : :
17963 [ + + ]: 20826 : if (cmname != NULL)
17964 : 112 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET COMPRESSION %s;\n",
17965 : : foreign, qualrelname,
17966 : 112 : fmtId(tbinfo->attnames[j]),
17967 : : cmname);
17968 : : }
17969 : :
17970 : : /*
17971 : : * Dump per-column attributes.
17972 : : */
17973 [ + + ]: 20931 : if (tbinfo->attoptions[j][0] != '\0')
17974 : 50 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n",
17975 : : foreign, qualrelname,
17976 : 50 : fmtId(tbinfo->attnames[j]),
17977 : 50 : tbinfo->attoptions[j]);
17978 : :
17979 : : /*
17980 : : * Dump per-column fdw options.
17981 : : */
17982 [ + + ]: 20931 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
17983 [ + + ]: 38 : tbinfo->attfdwoptions[j][0] != '\0')
17984 : 34 : appendPQExpBuffer(q,
17985 : : "ALTER FOREIGN TABLE ONLY %s ALTER COLUMN %s OPTIONS (\n"
17986 : : " %s\n"
17987 : : ");\n",
17988 : : qualrelname,
17989 : 34 : fmtId(tbinfo->attnames[j]),
17990 : 34 : tbinfo->attfdwoptions[j]);
17991 : : } /* end loop over columns */
17992 : :
17993 : 6226 : pg_free(partkeydef);
17994 : 6226 : pg_free(ftoptions);
17995 : 6226 : pg_free(srvname);
17996 : : }
17997 : :
17998 : : /*
17999 : : * dump properties we only have ALTER TABLE syntax for
18000 : : */
18001 [ + + ]: 6802 : if ((tbinfo->relkind == RELKIND_RELATION ||
18002 [ + + ]: 1588 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE ||
18003 [ + + ]: 967 : tbinfo->relkind == RELKIND_MATVIEW) &&
18004 [ + + ]: 6189 : tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT)
18005 : : {
18006 [ + - ]: 192 : if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX)
18007 : : {
18008 : : /* nothing to do, will be set when the index is dumped */
18009 : : }
18010 [ + - ]: 192 : else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING)
18011 : : {
18012 : 192 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n",
18013 : : qualrelname);
18014 : : }
18015 [ # # ]: 0 : else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL)
18016 : : {
18017 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n",
18018 : : qualrelname);
18019 : : }
18020 : : }
18021 : :
18022 [ + + ]: 6802 : if (tbinfo->forcerowsec)
18023 : 5 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n",
18024 : : qualrelname);
18025 : :
18026 [ + + ]: 6802 : if (dopt->binary_upgrade)
18027 : 925 : binary_upgrade_extension_member(q, &tbinfo->dobj,
18028 : : reltypename, qrelname,
18029 : 925 : tbinfo->dobj.namespace->dobj.name);
18030 : :
18031 [ + - ]: 6802 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18032 : : {
18033 : 6802 : char *tablespace = NULL;
18034 : 6802 : char *tableam = NULL;
18035 : :
18036 : : /*
18037 : : * _selectTablespace() relies on tablespace-enabled objects in the
18038 : : * default tablespace to have a tablespace of "" (empty string) versus
18039 : : * non-tablespace-enabled objects to have a tablespace of NULL.
18040 : : * getTables() sets tbinfo->reltablespace to "" for the default
18041 : : * tablespace (not NULL).
18042 : : */
18043 [ + + + - : 6802 : if (RELKIND_HAS_TABLESPACE(tbinfo->relkind))
+ - + - +
+ + + - +
+ - ]
18044 : 6189 : tablespace = tbinfo->reltablespace;
18045 : :
18046 [ + + + - : 6802 : if (RELKIND_HAS_TABLE_AM(tbinfo->relkind) ||
+ + ]
18047 [ + + ]: 1234 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
18048 : 6189 : tableam = tbinfo->amname;
18049 : :
18050 : 6802 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
18051 [ + + ]: 6802 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
18052 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18053 : : .tablespace = tablespace,
18054 : : .tableam = tableam,
18055 : : .relkind = tbinfo->relkind,
18056 : : .owner = tbinfo->rolname,
18057 : : .description = reltypename,
18058 : : .section = tbinfo->postponed_def ?
18059 : : SECTION_POST_DATA : SECTION_PRE_DATA,
18060 : : .createStmt = q->data,
18061 : : .dropStmt = delq->data));
18062 : : }
18063 : :
18064 : : /* Dump Table Comments */
18065 [ + + ]: 6802 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18066 : 78 : dumpTableComment(fout, tbinfo, reltypename);
18067 : :
18068 : : /* Dump Table Security Labels */
18069 [ - + ]: 6802 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
18070 : 0 : dumpTableSecLabel(fout, tbinfo, reltypename);
18071 : :
18072 : : /*
18073 : : * Dump comments for not-null constraints that aren't to be dumped
18074 : : * separately (those are processed by collectComments/dumpComment).
18075 : : */
18076 [ + - + - ]: 6802 : if (!fout->dopt->no_comments && dopt->dumpSchema &&
18077 [ + - ]: 6802 : fout->remoteVersion >= 180000)
18078 : : {
18079 : 6802 : PQExpBuffer comment = NULL;
18080 : 6802 : PQExpBuffer tag = NULL;
18081 : :
18082 [ + + ]: 31981 : for (j = 0; j < tbinfo->numatts; j++)
18083 : : {
18084 [ + + ]: 25179 : if (tbinfo->notnull_constrs[j] != NULL &&
18085 [ + + ]: 2593 : tbinfo->notnull_comment[j] != NULL)
18086 : : {
18087 [ + - ]: 44 : if (comment == NULL)
18088 : : {
18089 : 44 : comment = createPQExpBuffer();
18090 : 44 : tag = createPQExpBuffer();
18091 : : }
18092 : : else
18093 : : {
18094 : 0 : resetPQExpBuffer(comment);
18095 : 0 : resetPQExpBuffer(tag);
18096 : : }
18097 : :
18098 : 44 : appendPQExpBuffer(comment, "COMMENT ON CONSTRAINT %s ON %s IS ",
18099 : 44 : fmtId(tbinfo->notnull_constrs[j]), qualrelname);
18100 : 44 : appendStringLiteralAH(comment, tbinfo->notnull_comment[j], fout);
18101 : 44 : appendPQExpBufferStr(comment, ";\n");
18102 : :
18103 : 44 : appendPQExpBuffer(tag, "CONSTRAINT %s ON %s",
18104 : 44 : fmtId(tbinfo->notnull_constrs[j]), qrelname);
18105 : :
18106 : 44 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
18107 : 44 : ARCHIVE_OPTS(.tag = tag->data,
18108 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18109 : : .owner = tbinfo->rolname,
18110 : : .description = "COMMENT",
18111 : : .section = SECTION_NONE,
18112 : : .createStmt = comment->data,
18113 : : .deps = &(tbinfo->dobj.dumpId),
18114 : : .nDeps = 1));
18115 : : }
18116 : : }
18117 : :
18118 : 6802 : destroyPQExpBuffer(comment);
18119 : 6802 : destroyPQExpBuffer(tag);
18120 : : }
18121 : :
18122 : : /* Dump comments on inlined table constraints */
18123 [ + + ]: 7395 : for (j = 0; j < tbinfo->ncheck; j++)
18124 : : {
18125 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
18126 : :
18127 [ + + + + ]: 593 : if (constr->separate || !constr->conislocal)
18128 : 254 : continue;
18129 : :
18130 [ + + ]: 339 : if (constr->dobj.dump & DUMP_COMPONENT_COMMENT)
18131 : 39 : dumpTableConstraintComment(fout, constr);
18132 : : }
18133 : :
18134 : 6802 : destroyPQExpBuffer(q);
18135 : 6802 : destroyPQExpBuffer(delq);
18136 : 6802 : destroyPQExpBuffer(extra);
18137 : 6802 : pg_free(qrelname);
18138 : 6802 : pg_free(qualrelname);
18139 : 6802 : }
18140 : :
18141 : : /*
18142 : : * dumpTableAttach
18143 : : * write to fout the commands to attach a child partition
18144 : : *
18145 : : * Child partitions are always made by creating them separately
18146 : : * and then using ATTACH PARTITION, rather than using
18147 : : * CREATE TABLE ... PARTITION OF. This is important for preserving
18148 : : * any possible discrepancy in column layout, to allow assigning the
18149 : : * correct tablespace if different, and so that it's possible to restore
18150 : : * a partition without restoring its parent. (You'll get an error from
18151 : : * the ATTACH PARTITION command, but that can be ignored, or skipped
18152 : : * using "pg_restore -L" if you prefer.) The last point motivates
18153 : : * treating ATTACH PARTITION as a completely separate ArchiveEntry
18154 : : * rather than emitting it within the child partition's ArchiveEntry.
18155 : : */
18156 : : static void
18157 : 1475 : dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo)
18158 : : {
18159 : 1475 : DumpOptions *dopt = fout->dopt;
18160 : : PQExpBuffer q;
18161 : : PGresult *res;
18162 : : char *partbound;
18163 : :
18164 : : /* Do nothing if not dumping schema */
18165 [ + + ]: 1475 : if (!dopt->dumpSchema)
18166 : 57 : return;
18167 : :
18168 : 1418 : q = createPQExpBuffer();
18169 : :
18170 [ + + ]: 1418 : if (!fout->is_prepared[PREPQUERY_DUMPTABLEATTACH])
18171 : : {
18172 : : /* Set up query for partbound details */
18173 : 62 : appendPQExpBufferStr(q,
18174 : : "PREPARE dumpTableAttach(pg_catalog.oid) AS\n");
18175 : :
18176 : 62 : appendPQExpBufferStr(q,
18177 : : "SELECT pg_get_expr(c.relpartbound, c.oid) "
18178 : : "FROM pg_class c "
18179 : : "WHERE c.oid = $1");
18180 : :
18181 : 62 : ExecuteSqlStatement(fout, q->data);
18182 : :
18183 : 62 : fout->is_prepared[PREPQUERY_DUMPTABLEATTACH] = true;
18184 : : }
18185 : :
18186 : 1418 : printfPQExpBuffer(q,
18187 : : "EXECUTE dumpTableAttach('%u')",
18188 : 1418 : attachinfo->partitionTbl->dobj.catId.oid);
18189 : :
18190 : 1418 : res = ExecuteSqlQueryForSingleRow(fout, q->data);
18191 : 1418 : partbound = PQgetvalue(res, 0, 0);
18192 : :
18193 : : /* Perform ALTER TABLE on the parent */
18194 : 1418 : printfPQExpBuffer(q,
18195 : : "ALTER TABLE ONLY %s ",
18196 : 1418 : fmtQualifiedDumpable(attachinfo->parentTbl));
18197 : 1418 : appendPQExpBuffer(q,
18198 : : "ATTACH PARTITION %s %s;\n",
18199 : 1418 : fmtQualifiedDumpable(attachinfo->partitionTbl),
18200 : : partbound);
18201 : :
18202 : : /*
18203 : : * There is no point in creating a drop query as the drop is done by table
18204 : : * drop. (If you think to change this, see also _printTocEntry().)
18205 : : * Although this object doesn't really have ownership as such, set the
18206 : : * owner field anyway to ensure that the command is run by the correct
18207 : : * role at restore time.
18208 : : */
18209 : 1418 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18210 : 1418 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18211 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18212 : : .owner = attachinfo->partitionTbl->rolname,
18213 : : .description = "TABLE ATTACH",
18214 : : .section = SECTION_PRE_DATA,
18215 : : .createStmt = q->data));
18216 : :
18217 : 1418 : PQclear(res);
18218 : 1418 : destroyPQExpBuffer(q);
18219 : : }
18220 : :
18221 : : /*
18222 : : * dumpAttrDef --- dump an attribute's default-value declaration
18223 : : */
18224 : : static void
18225 : 1143 : dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo)
18226 : : {
18227 : 1143 : DumpOptions *dopt = fout->dopt;
18228 : 1143 : TableInfo *tbinfo = adinfo->adtable;
18229 : 1143 : int adnum = adinfo->adnum;
18230 : : PQExpBuffer q;
18231 : : PQExpBuffer delq;
18232 : : char *qualrelname;
18233 : : char *tag;
18234 : : char *foreign;
18235 : :
18236 : : /* Do nothing if not dumping schema */
18237 [ - + ]: 1143 : if (!dopt->dumpSchema)
18238 : 0 : return;
18239 : :
18240 : : /* Skip if not "separate"; it was dumped in the table's definition */
18241 [ + + ]: 1143 : if (!adinfo->separate)
18242 : 971 : return;
18243 : :
18244 : 172 : q = createPQExpBuffer();
18245 : 172 : delq = createPQExpBuffer();
18246 : :
18247 : 172 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
18248 : :
18249 [ - + ]: 172 : foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18250 : :
18251 : 172 : appendPQExpBuffer(q,
18252 : : "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
18253 : 172 : foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
18254 : 172 : adinfo->adef_expr);
18255 : :
18256 : 172 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
18257 : : foreign, qualrelname,
18258 : 172 : fmtId(tbinfo->attnames[adnum - 1]));
18259 : :
18260 : 172 : tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
18261 : :
18262 [ + - ]: 172 : if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18263 : 172 : ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
18264 : 172 : ARCHIVE_OPTS(.tag = tag,
18265 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18266 : : .owner = tbinfo->rolname,
18267 : : .description = "DEFAULT",
18268 : : .section = SECTION_PRE_DATA,
18269 : : .createStmt = q->data,
18270 : : .dropStmt = delq->data));
18271 : :
18272 : 172 : pfree(tag);
18273 : 172 : destroyPQExpBuffer(q);
18274 : 172 : destroyPQExpBuffer(delq);
18275 : 172 : pg_free(qualrelname);
18276 : : }
18277 : :
18278 : : /*
18279 : : * getAttrName: extract the correct name for an attribute
18280 : : *
18281 : : * The array tblInfo->attnames[] only provides names of user attributes;
18282 : : * if a system attribute number is supplied, we have to fake it.
18283 : : * We also do a little bit of bounds checking for safety's sake.
18284 : : */
18285 : : static const char *
18286 : 2111 : getAttrName(int attrnum, const TableInfo *tblInfo)
18287 : : {
18288 [ + - + - ]: 2111 : if (attrnum > 0 && attrnum <= tblInfo->numatts)
18289 : 2111 : return tblInfo->attnames[attrnum - 1];
18290 [ # # # # : 0 : switch (attrnum)
# # # ]
18291 : : {
18292 : 0 : case SelfItemPointerAttributeNumber:
18293 : 0 : return "ctid";
18294 : 0 : case MinTransactionIdAttributeNumber:
18295 : 0 : return "xmin";
18296 : 0 : case MinCommandIdAttributeNumber:
18297 : 0 : return "cmin";
18298 : 0 : case MaxTransactionIdAttributeNumber:
18299 : 0 : return "xmax";
18300 : 0 : case MaxCommandIdAttributeNumber:
18301 : 0 : return "cmax";
18302 : 0 : case TableOidAttributeNumber:
18303 : 0 : return "tableoid";
18304 : : }
18305 : 0 : pg_fatal("invalid column number %d for table \"%s\"",
18306 : : attrnum, tblInfo->dobj.name);
18307 : : return NULL; /* keep compiler quiet */
18308 : : }
18309 : :
18310 : : /*
18311 : : * appendIndexStatTargets
18312 : : * append ALTER INDEX ... SET STATISTICS commands for the per-column
18313 : : * statistics targets of an index (attstattarget), if any.
18314 : : */
18315 : : static void
18316 : 2656 : appendIndexStatTargets(PQExpBuffer q, const IndxInfo *indxinfo)
18317 : : {
18318 : 2656 : char **indstatcolsarray = NULL;
18319 : 2656 : char **indstatvalsarray = NULL;
18320 : 2656 : int nstatcols = 0;
18321 : 2656 : int nstatvals = 0;
18322 : :
18323 [ + + ]: 2656 : if (strlen(indxinfo->indstatcols) == 0 &&
18324 [ + - ]: 2588 : strlen(indxinfo->indstatvals) == 0)
18325 : 2588 : return;
18326 : :
18327 [ - + ]: 68 : if (!parsePGArray(indxinfo->indstatcols, &indstatcolsarray, &nstatcols))
18328 : 0 : pg_fatal("could not parse index statistic columns");
18329 [ - + ]: 68 : if (!parsePGArray(indxinfo->indstatvals, &indstatvalsarray, &nstatvals))
18330 : 0 : pg_fatal("could not parse index statistic values");
18331 [ - + ]: 68 : if (nstatcols != nstatvals)
18332 : 0 : pg_fatal("mismatched number of columns and values for index statistics");
18333 : :
18334 [ + + ]: 204 : for (int j = 0; j < nstatcols; j++)
18335 : : {
18336 : 136 : appendPQExpBuffer(q, "ALTER INDEX %s ",
18337 : 136 : fmtQualifiedDumpable(indxinfo));
18338 : :
18339 : : /*
18340 : : * Note that this is a column number, so no quotes should be used.
18341 : : */
18342 : 136 : appendPQExpBuffer(q, "ALTER COLUMN %s ",
18343 : 136 : indstatcolsarray[j]);
18344 : 136 : appendPQExpBuffer(q, "SET STATISTICS %s;\n",
18345 : 136 : indstatvalsarray[j]);
18346 : : }
18347 : :
18348 : 68 : free(indstatcolsarray);
18349 : 68 : free(indstatvalsarray);
18350 : : }
18351 : :
18352 : : /*
18353 : : * dumpIndex
18354 : : * write out to fout a user-defined index
18355 : : */
18356 : : static void
18357 : 2791 : dumpIndex(Archive *fout, const IndxInfo *indxinfo)
18358 : : {
18359 : 2791 : DumpOptions *dopt = fout->dopt;
18360 : 2791 : TableInfo *tbinfo = indxinfo->indextable;
18361 : 2791 : bool is_constraint = (indxinfo->indexconstraint != 0);
18362 : : PQExpBuffer q;
18363 : : PQExpBuffer delq;
18364 : : char *qindxname;
18365 : : char *qqindxname;
18366 : :
18367 : : /* Do nothing if not dumping schema */
18368 [ + + ]: 2791 : if (!dopt->dumpSchema)
18369 : 135 : return;
18370 : :
18371 : 2656 : q = createPQExpBuffer();
18372 : 2656 : delq = createPQExpBuffer();
18373 : :
18374 : 2656 : qindxname = pg_strdup(fmtId(indxinfo->dobj.name));
18375 : 2656 : qqindxname = pg_strdup(fmtQualifiedDumpable(indxinfo));
18376 : :
18377 : : /*
18378 : : * If there's an associated constraint, don't dump the index per se, but
18379 : : * do dump any comment for it. (This is safe because dependency ordering
18380 : : * will have ensured the constraint is emitted first.) Note that the
18381 : : * emitted comment has to be shown as depending on the constraint, not the
18382 : : * index, in such cases.
18383 : : */
18384 [ + + ]: 2656 : if (!is_constraint)
18385 : : {
18386 [ + + ]: 1132 : if (dopt->binary_upgrade)
18387 : 172 : binary_upgrade_set_pg_class_oids(fout, q,
18388 : 172 : indxinfo->dobj.catId.oid);
18389 : :
18390 : : /* Plain secondary index */
18391 : 1132 : appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
18392 : :
18393 : : /*
18394 : : * Append ALTER TABLE commands as needed to set properties that we
18395 : : * only have ALTER TABLE syntax for. Keep this in sync with the
18396 : : * similar code in dumpConstraint!
18397 : : */
18398 : :
18399 : : /* If the index is clustered, we need to record that. */
18400 [ + + ]: 1132 : if (indxinfo->indisclustered)
18401 : : {
18402 : 5 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
18403 : 5 : fmtQualifiedDumpable(tbinfo));
18404 : : /* index name is not qualified in this syntax */
18405 : 5 : appendPQExpBuffer(q, " ON %s;\n",
18406 : : qindxname);
18407 : : }
18408 : :
18409 : : /* Per-column statistics targets, if any */
18410 : 1132 : appendIndexStatTargets(q, indxinfo);
18411 : :
18412 : : /* Indexes can depend on extensions */
18413 : 1132 : append_depends_on_extension(fout, q, &indxinfo->dobj,
18414 : : "pg_catalog.pg_class",
18415 : : "INDEX", qqindxname);
18416 : :
18417 : : /* If the index defines identity, we need to record that. */
18418 [ - + ]: 1132 : if (indxinfo->indisreplident)
18419 : : {
18420 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
18421 : 0 : fmtQualifiedDumpable(tbinfo));
18422 : : /* index name is not qualified in this syntax */
18423 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
18424 : : qindxname);
18425 : : }
18426 : :
18427 : : /*
18428 : : * If this index is a member of a partitioned index, the backend will
18429 : : * not allow us to drop it separately, so don't try. It will go away
18430 : : * automatically when we drop either the index's table or the
18431 : : * partitioned index. (If, in a selective restore with --clean, we
18432 : : * drop neither of those, then this index will not be dropped either.
18433 : : * But that's fine, and even if you think it's not, the backend won't
18434 : : * let us do differently.)
18435 : : */
18436 [ + + ]: 1132 : if (indxinfo->parentidx == 0)
18437 : 912 : appendPQExpBuffer(delq, "DROP INDEX %s;\n", qqindxname);
18438 : :
18439 [ + - ]: 1132 : if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18440 : 1132 : ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
18441 : 1132 : ARCHIVE_OPTS(.tag = indxinfo->dobj.name,
18442 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18443 : : .tablespace = indxinfo->tablespace,
18444 : : .owner = tbinfo->rolname,
18445 : : .description = "INDEX",
18446 : : .section = SECTION_POST_DATA,
18447 : : .createStmt = q->data,
18448 : : .dropStmt = delq->data));
18449 : : }
18450 : :
18451 : : /* Dump Index Comments */
18452 [ + + ]: 2656 : if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18453 [ + + ]: 15 : dumpComment(fout, "INDEX", qindxname,
18454 : 15 : tbinfo->dobj.namespace->dobj.name,
18455 : : tbinfo->rolname,
18456 : : indxinfo->dobj.catId, 0,
18457 : : is_constraint ? indxinfo->indexconstraint :
18458 : : indxinfo->dobj.dumpId);
18459 : :
18460 : 2656 : destroyPQExpBuffer(q);
18461 : 2656 : destroyPQExpBuffer(delq);
18462 : 2656 : pg_free(qindxname);
18463 : 2656 : pg_free(qqindxname);
18464 : : }
18465 : :
18466 : : /*
18467 : : * dumpIndexAttach
18468 : : * write out to fout a partitioned-index attachment clause
18469 : : */
18470 : : static void
18471 : 625 : dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo)
18472 : : {
18473 : : /* Do nothing if not dumping schema */
18474 [ + + ]: 625 : if (!fout->dopt->dumpSchema)
18475 : 48 : return;
18476 : :
18477 [ + - ]: 577 : if (attachinfo->partitionIdx->dobj.dump & DUMP_COMPONENT_DEFINITION)
18478 : : {
18479 : 577 : PQExpBuffer q = createPQExpBuffer();
18480 : :
18481 : 577 : appendPQExpBuffer(q, "ALTER INDEX %s ",
18482 : 577 : fmtQualifiedDumpable(attachinfo->parentIdx));
18483 : 577 : appendPQExpBuffer(q, "ATTACH PARTITION %s;\n",
18484 : 577 : fmtQualifiedDumpable(attachinfo->partitionIdx));
18485 : :
18486 : : /*
18487 : : * There is no need for a dropStmt since the drop is done implicitly
18488 : : * when we drop either the index's table or the partitioned index.
18489 : : * Moreover, since there's no ALTER INDEX DETACH PARTITION command,
18490 : : * there's no way to do it anyway. (If you think to change this,
18491 : : * consider also what to do with --if-exists.)
18492 : : *
18493 : : * Although this object doesn't really have ownership as such, set the
18494 : : * owner field anyway to ensure that the command is run by the correct
18495 : : * role at restore time.
18496 : : */
18497 : 577 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18498 : 577 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18499 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18500 : : .owner = attachinfo->parentIdx->indextable->rolname,
18501 : : .description = "INDEX ATTACH",
18502 : : .section = SECTION_POST_DATA,
18503 : : .createStmt = q->data));
18504 : :
18505 : 577 : destroyPQExpBuffer(q);
18506 : : }
18507 : : }
18508 : :
18509 : : /*
18510 : : * dumpStatisticsExt
18511 : : * write out to fout an extended statistics object
18512 : : */
18513 : : static void
18514 : 196 : dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo)
18515 : : {
18516 : 196 : DumpOptions *dopt = fout->dopt;
18517 : : PQExpBuffer q;
18518 : : PQExpBuffer delq;
18519 : : PQExpBuffer query;
18520 : : char *qstatsextname;
18521 : : PGresult *res;
18522 : : char *stxdef;
18523 : :
18524 : : /* Do nothing if not dumping schema */
18525 [ + + ]: 196 : if (!dopt->dumpSchema)
18526 : 28 : return;
18527 : :
18528 : 168 : q = createPQExpBuffer();
18529 : 168 : delq = createPQExpBuffer();
18530 : 168 : query = createPQExpBuffer();
18531 : :
18532 : 168 : qstatsextname = pg_strdup(fmtId(statsextinfo->dobj.name));
18533 : :
18534 : 168 : appendPQExpBuffer(query, "SELECT "
18535 : : "pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)",
18536 : 168 : statsextinfo->dobj.catId.oid);
18537 : :
18538 : 168 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
18539 : :
18540 : 168 : stxdef = PQgetvalue(res, 0, 0);
18541 : :
18542 : : /* Result of pg_get_statisticsobjdef is complete except for semicolon */
18543 : 168 : appendPQExpBuffer(q, "%s;\n", stxdef);
18544 : :
18545 : : /*
18546 : : * We only issue an ALTER STATISTICS statement if the stxstattarget entry
18547 : : * for this statistics object is not the default value.
18548 : : */
18549 [ + + ]: 168 : if (statsextinfo->stattarget >= 0)
18550 : : {
18551 : 34 : appendPQExpBuffer(q, "ALTER STATISTICS %s ",
18552 : 34 : fmtQualifiedDumpable(statsextinfo));
18553 : 34 : appendPQExpBuffer(q, "SET STATISTICS %d;\n",
18554 : 34 : statsextinfo->stattarget);
18555 : : }
18556 : :
18557 : 168 : appendPQExpBuffer(delq, "DROP STATISTICS %s;\n",
18558 : 168 : fmtQualifiedDumpable(statsextinfo));
18559 : :
18560 [ + - ]: 168 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18561 : 168 : ArchiveEntry(fout, statsextinfo->dobj.catId,
18562 : 168 : statsextinfo->dobj.dumpId,
18563 : 168 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18564 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18565 : : .owner = statsextinfo->rolname,
18566 : : .description = "STATISTICS",
18567 : : .section = SECTION_POST_DATA,
18568 : : .createStmt = q->data,
18569 : : .dropStmt = delq->data));
18570 : :
18571 : : /* Dump Statistics Comments */
18572 [ - + ]: 168 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18573 : 0 : dumpComment(fout, "STATISTICS", qstatsextname,
18574 : 0 : statsextinfo->dobj.namespace->dobj.name,
18575 : 0 : statsextinfo->rolname,
18576 : : statsextinfo->dobj.catId, 0,
18577 : 0 : statsextinfo->dobj.dumpId);
18578 : :
18579 : 168 : PQclear(res);
18580 : 168 : destroyPQExpBuffer(q);
18581 : 168 : destroyPQExpBuffer(delq);
18582 : 168 : destroyPQExpBuffer(query);
18583 : 168 : pg_free(qstatsextname);
18584 : : }
18585 : :
18586 : : /*
18587 : : * dumpStatisticsExtStats
18588 : : * write out to fout the stats for an extended statistics object
18589 : : */
18590 : : static void
18591 : 196 : dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo)
18592 : : {
18593 : 196 : DumpOptions *dopt = fout->dopt;
18594 : : PQExpBuffer query;
18595 : : PGresult *res;
18596 : : int nstats;
18597 : :
18598 : : /* Do nothing if not dumping statistics */
18599 [ + + ]: 196 : if (!dopt->dumpStatistics)
18600 : 53 : return;
18601 : :
18602 [ + + ]: 143 : if (!fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS])
18603 : : {
18604 : 36 : PQExpBuffer pq = createPQExpBuffer();
18605 : :
18606 : : /*---------
18607 : : * Set up query for details about extended statistics objects.
18608 : : *
18609 : : * The query depends on the backend version:
18610 : : * - In v19 and newer versions, query directly the pg_stats_ext*
18611 : : * catalogs.
18612 : : * - In v18 and older versions, ndistinct and dependencies have a
18613 : : * different format that needs translation.
18614 : : * - In v14 and older versions, there is no inherited flag, hence
18615 : : * it is deduced from the relation's relkind.
18616 : : * - In v11 and older versions, there is no pg_stats_ext, hence
18617 : : * the logic joins pg_statistic_ext and pg_namespace.
18618 : : *---------
18619 : : */
18620 : :
18621 : 36 : appendPQExpBufferStr(pq,
18622 : : "PREPARE getExtStatsStats(pg_catalog.name, pg_catalog.name) AS\n"
18623 : : "SELECT ");
18624 : :
18625 : : /*
18626 : : * Versions 15 and newer have inherited stats.
18627 : : *
18628 : : * Create this column in all versions because we need to order by it
18629 : : * later.
18630 : : */
18631 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18632 : 36 : appendPQExpBufferStr(pq, "e.inherited, ");
18633 : : else
18634 : 0 : appendPQExpBufferStr(pq, "false AS inherited, ");
18635 : :
18636 : : /*--------
18637 : : * The ndistinct and dependencies formats changed in v19, so
18638 : : * everything before that needs to be translated.
18639 : : *
18640 : : * The ndistinct translation converts this kind of data:
18641 : : * {"3, 4": 11, "3, 6": 11, "4, 6": 11, "3, 4, 6": 11}
18642 : : *
18643 : : * to this:
18644 : : * [ {"attributes": [3,4], "ndistinct": 11},
18645 : : * {"attributes": [3,6], "ndistinct": 11},
18646 : : * {"attributes": [4,6], "ndistinct": 11},
18647 : : * {"attributes": [3,4,6], "ndistinct": 11} ]
18648 : : *
18649 : : * The dependencies translation converts this kind of data:
18650 : : * {"3 => 4": 1.000000, "3 => 6": 1.000000,
18651 : : * "4 => 6": 1.000000, "3, 4 => 6": 1.000000,
18652 : : * "3, 6 => 4": 1.000000}
18653 : : *
18654 : : * to this:
18655 : : * [ {"attributes": [3], "dependency": 4, "degree": 1.000000},
18656 : : * {"attributes": [3], "dependency": 6, "degree": 1.000000},
18657 : : * {"attributes": [4], "dependency": 6, "degree": 1.000000},
18658 : : * {"attributes": [3,4], "dependency": 6, "degree": 1.000000},
18659 : : * {"attributes": [3,6], "dependency": 4, "degree": 1.000000} ]
18660 : : *--------
18661 : : */
18662 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18663 : 36 : appendPQExpBufferStr(pq, "e.n_distinct, e.dependencies, ");
18664 : : else
18665 : 0 : appendPQExpBufferStr(pq,
18666 : : "( "
18667 : : "SELECT json_agg( "
18668 : : " json_build_object( "
18669 : : " '" PG_NDISTINCT_KEY_ATTRIBUTES "', "
18670 : : " string_to_array(kv.key, ', ')::integer[], "
18671 : : " '" PG_NDISTINCT_KEY_NDISTINCT "', "
18672 : : " kv.value::bigint )) "
18673 : : "FROM json_each_text(e.n_distinct::text::json) AS kv"
18674 : : ") AS n_distinct, "
18675 : : "( "
18676 : : "SELECT json_agg( "
18677 : : " json_build_object( "
18678 : : " '" PG_DEPENDENCIES_KEY_ATTRIBUTES "', "
18679 : : " string_to_array( "
18680 : : " split_part(kv.key, ' => ', 1), "
18681 : : " ', ')::integer[], "
18682 : : " '" PG_DEPENDENCIES_KEY_DEPENDENCY "', "
18683 : : " split_part(kv.key, ' => ', 2)::integer, "
18684 : : " '" PG_DEPENDENCIES_KEY_DEGREE "', "
18685 : : " kv.value::double precision )) "
18686 : : "FROM json_each_text(e.dependencies::text::json) AS kv "
18687 : : ") AS dependencies, ");
18688 : :
18689 : : /* MCV was introduced v13 */
18690 [ + - ]: 36 : if (fout->remoteVersion >= 130000)
18691 : 36 : appendPQExpBufferStr(pq,
18692 : : "e.most_common_vals, e.most_common_freqs, "
18693 : : "e.most_common_base_freqs, ");
18694 : : else
18695 : 0 : appendPQExpBufferStr(pq,
18696 : : "NULL AS most_common_vals, NULL AS most_common_freqs, "
18697 : : "NULL AS most_common_base_freqs, ");
18698 : :
18699 : : /* Expressions were introduced in v14 */
18700 [ + - ]: 36 : if (fout->remoteVersion >= 140000)
18701 : : {
18702 : : /*
18703 : : * There is no ordering column in pg_stats_ext_exprs. However, we
18704 : : * can rely on the unnesting of pg_statistic_ext_data.stxdexpr to
18705 : : * maintain the desired order of expression elements.
18706 : : */
18707 : 36 : appendPQExpBufferStr(pq,
18708 : : "( "
18709 : : "SELECT jsonb_pretty(jsonb_agg("
18710 : : "nullif(j.obj, '{}'::jsonb))) "
18711 : : "FROM pg_stats_ext_exprs AS ee "
18712 : : "CROSS JOIN LATERAL jsonb_strip_nulls("
18713 : : " jsonb_build_object( "
18714 : : " 'null_frac', ee.null_frac::text, "
18715 : : " 'avg_width', ee.avg_width::text, "
18716 : : " 'n_distinct', ee.n_distinct::text, "
18717 : : " 'most_common_vals', ee.most_common_vals::text, "
18718 : : " 'most_common_freqs', ee.most_common_freqs::text, "
18719 : : " 'histogram_bounds', ee.histogram_bounds::text, "
18720 : : " 'correlation', ee.correlation::text, "
18721 : : " 'most_common_elems', ee.most_common_elems::text, "
18722 : : " 'most_common_elem_freqs', ee.most_common_elem_freqs::text, "
18723 : : " 'elem_count_histogram', ee.elem_count_histogram::text");
18724 : :
18725 : : /* These three have been added to pg_stats_ext_exprs in v19. */
18726 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18727 : 36 : appendPQExpBufferStr(pq,
18728 : : ", "
18729 : : " 'range_length_histogram', ee.range_length_histogram::text, "
18730 : : " 'range_empty_frac', ee.range_empty_frac::text, "
18731 : : " 'range_bounds_histogram', ee.range_bounds_histogram::text");
18732 : :
18733 : 36 : appendPQExpBufferStr(pq,
18734 : : " )) AS j(obj)"
18735 : : "WHERE ee.statistics_schemaname = $1 "
18736 : : "AND ee.statistics_name = $2 ");
18737 : : /* Inherited expressions introduced in v15 */
18738 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18739 : 36 : appendPQExpBufferStr(pq, "AND ee.inherited = e.inherited");
18740 : :
18741 : 36 : appendPQExpBufferStr(pq, ") AS exprs ");
18742 : : }
18743 : : else
18744 : 0 : appendPQExpBufferStr(pq, "NULL AS exprs ");
18745 : :
18746 : : /* pg_stats_ext introduced in v12 */
18747 [ + - ]: 36 : if (fout->remoteVersion >= 120000)
18748 : 36 : appendPQExpBufferStr(pq,
18749 : : "FROM pg_catalog.pg_stats_ext AS e "
18750 : : "WHERE e.statistics_schemaname = $1 "
18751 : : "AND e.statistics_name = $2 ");
18752 : : else
18753 : 0 : appendPQExpBufferStr(pq,
18754 : : "FROM ( "
18755 : : "SELECT s.stxndistinct AS n_distinct, "
18756 : : " s.stxdependencies AS dependencies "
18757 : : "FROM pg_catalog.pg_statistic_ext AS s "
18758 : : "JOIN pg_catalog.pg_namespace AS n "
18759 : : "ON n.oid = s.stxnamespace "
18760 : : "WHERE n.nspname = $1 "
18761 : : "AND s.stxname = $2 "
18762 : : ") AS e ");
18763 : :
18764 : : /* we always have an inherited column, but it may be a constant */
18765 : 36 : appendPQExpBufferStr(pq, "ORDER BY inherited");
18766 : :
18767 : 36 : ExecuteSqlStatement(fout, pq->data);
18768 : :
18769 : 36 : fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS] = true;
18770 : :
18771 : 36 : destroyPQExpBuffer(pq);
18772 : : }
18773 : :
18774 : 143 : query = createPQExpBuffer();
18775 : :
18776 : 143 : appendPQExpBufferStr(query, "EXECUTE getExtStatsStats(");
18777 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.namespace->dobj.name, fout);
18778 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name, ");
18779 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.name, fout);
18780 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name)");
18781 : :
18782 : 143 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
18783 : :
18784 : 143 : destroyPQExpBuffer(query);
18785 : :
18786 : 143 : nstats = PQntuples(res);
18787 : :
18788 [ + + ]: 143 : if (nstats > 0)
18789 : : {
18790 : 39 : PQExpBuffer out = createPQExpBuffer();
18791 : :
18792 : 39 : int i_inherited = PQfnumber(res, "inherited");
18793 : 39 : int i_ndistinct = PQfnumber(res, "n_distinct");
18794 : 39 : int i_dependencies = PQfnumber(res, "dependencies");
18795 : 39 : int i_mcv = PQfnumber(res, "most_common_vals");
18796 : 39 : int i_mcf = PQfnumber(res, "most_common_freqs");
18797 : 39 : int i_mcbf = PQfnumber(res, "most_common_base_freqs");
18798 : 39 : int i_exprs = PQfnumber(res, "exprs");
18799 : :
18800 [ + + ]: 78 : for (int i = 0; i < nstats; i++)
18801 : : {
18802 : 39 : TableInfo *tbinfo = statsextinfo->stattable;
18803 : : const char *inherited;
18804 : :
18805 [ - + ]: 39 : if (PQgetisnull(res, i, i_inherited))
18806 : 0 : pg_fatal("inherited cannot be NULL");
18807 : :
18808 : : /*
18809 : : * v14 and older have no inherited flag; only partitioned tables
18810 : : * store inherited extended statistics there.
18811 : : */
18812 [ + - ]: 39 : if (fout->remoteVersion >= 150000)
18813 : 39 : inherited = PQgetvalue(res, i, i_inherited);
18814 : : else
18815 : 0 : inherited =
18816 [ # # ]: 0 : (tbinfo->relkind == RELKIND_PARTITIONED_TABLE) ? "t" : "f";
18817 : :
18818 : 39 : appendPQExpBufferStr(out,
18819 : : "SELECT * FROM pg_catalog.pg_restore_extended_stats(\n");
18820 : 39 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
18821 : : fout->remoteVersion);
18822 : :
18823 : : /* Relation information */
18824 : 39 : appendPQExpBufferStr(out, "\t'schemaname', ");
18825 : 39 : appendStringLiteralAH(out, tbinfo->dobj.namespace->dobj.name, fout);
18826 : 39 : appendPQExpBufferStr(out, ",\n\t'relname', ");
18827 : 39 : appendStringLiteralAH(out, tbinfo->dobj.name, fout);
18828 : :
18829 : : /* Extended statistics information */
18830 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_schemaname', ");
18831 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.namespace->dobj.name, fout);
18832 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_name', ");
18833 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.name, fout);
18834 : 39 : appendNamedArgument(out, fout, "inherited", "boolean", inherited);
18835 : :
18836 [ + + ]: 39 : if (!PQgetisnull(res, i, i_ndistinct))
18837 : 35 : appendNamedArgument(out, fout, "n_distinct", "pg_ndistinct",
18838 : 35 : PQgetvalue(res, i, i_ndistinct));
18839 : :
18840 [ + + ]: 39 : if (!PQgetisnull(res, i, i_dependencies))
18841 : 36 : appendNamedArgument(out, fout, "dependencies", "pg_dependencies",
18842 : 36 : PQgetvalue(res, i, i_dependencies));
18843 : :
18844 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcv))
18845 : 38 : appendNamedArgument(out, fout, "most_common_vals", "text[]",
18846 : 38 : PQgetvalue(res, i, i_mcv));
18847 : :
18848 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcf))
18849 : 38 : appendNamedArgument(out, fout, "most_common_freqs", "double precision[]",
18850 : 38 : PQgetvalue(res, i, i_mcf));
18851 : :
18852 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcbf))
18853 : 38 : appendNamedArgument(out, fout, "most_common_base_freqs", "double precision[]",
18854 : 38 : PQgetvalue(res, i, i_mcbf));
18855 : :
18856 [ + + ]: 39 : if (!PQgetisnull(res, i, i_exprs))
18857 : 36 : appendNamedArgument(out, fout, "exprs", "jsonb",
18858 : 36 : PQgetvalue(res, i, i_exprs));
18859 : :
18860 : 39 : appendPQExpBufferStr(out, "\n);\n");
18861 : : }
18862 : :
18863 : 39 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
18864 : 39 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18865 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18866 : : .owner = statsextinfo->rolname,
18867 : : .description = "EXTENDED STATISTICS DATA",
18868 : : .section = SECTION_POST_DATA,
18869 : : .createStmt = out->data,
18870 : : .deps = &statsextinfo->dobj.dumpId,
18871 : : .nDeps = 1));
18872 : 39 : destroyPQExpBuffer(out);
18873 : : }
18874 : 143 : PQclear(res);
18875 : : }
18876 : :
18877 : : /*
18878 : : * dumpConstraint
18879 : : * write out to fout a user-defined constraint
18880 : : */
18881 : : static void
18882 : 2634 : dumpConstraint(Archive *fout, const ConstraintInfo *coninfo)
18883 : : {
18884 : 2634 : DumpOptions *dopt = fout->dopt;
18885 : 2634 : TableInfo *tbinfo = coninfo->contable;
18886 : : PQExpBuffer q;
18887 : : PQExpBuffer delq;
18888 : 2634 : char *tag = NULL;
18889 : : char *foreign;
18890 : :
18891 : : /* Do nothing if not dumping schema */
18892 [ + + ]: 2634 : if (!dopt->dumpSchema)
18893 : 117 : return;
18894 : :
18895 : 2517 : q = createPQExpBuffer();
18896 : 2517 : delq = createPQExpBuffer();
18897 : :
18898 : 4866 : foreign = tbinfo &&
18899 [ + + - + ]: 2517 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18900 : :
18901 [ + + ]: 2517 : if (coninfo->contype == 'p' ||
18902 [ + + ]: 1278 : coninfo->contype == 'u' ||
18903 [ + + ]: 1047 : coninfo->contype == 'x')
18904 : 1524 : {
18905 : : /* Index-related constraint */
18906 : : IndxInfo *indxinfo;
18907 : : int k;
18908 : :
18909 : 1524 : indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
18910 : :
18911 [ - + ]: 1524 : if (indxinfo == NULL)
18912 : 0 : pg_fatal("missing index for constraint \"%s\"",
18913 : : coninfo->dobj.name);
18914 : :
18915 [ + + ]: 1524 : if (dopt->binary_upgrade)
18916 : 154 : binary_upgrade_set_pg_class_oids(fout, q,
18917 : : indxinfo->dobj.catId.oid);
18918 : :
18919 : 1524 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
18920 : 1524 : fmtQualifiedDumpable(tbinfo));
18921 : 1524 : appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
18922 : 1524 : fmtId(coninfo->dobj.name));
18923 : :
18924 [ + + ]: 1524 : if (coninfo->condef)
18925 : : {
18926 : : /* pg_get_constraintdef should have provided everything */
18927 : 54 : appendPQExpBuffer(q, "%s;\n", coninfo->condef);
18928 : : }
18929 : : else
18930 : : {
18931 : 1470 : appendPQExpBufferStr(q,
18932 [ + + ]: 1470 : coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
18933 : :
18934 : : /*
18935 : : * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
18936 : : * indexes. Being able to create this was fixed, but we need to
18937 : : * make the index distinct in order to be able to restore the
18938 : : * dump.
18939 : : */
18940 [ - + - - ]: 1470 : if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
18941 : 0 : appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
18942 : 1470 : appendPQExpBufferStr(q, " (");
18943 [ + + ]: 3541 : for (k = 0; k < indxinfo->indnkeyattrs; k++)
18944 : : {
18945 : 2071 : int indkey = indxinfo->indkeys[k];
18946 : : const char *attname;
18947 : :
18948 [ - + ]: 2071 : if (indkey == InvalidAttrNumber)
18949 : 0 : break;
18950 : 2071 : attname = getAttrName(indkey, tbinfo);
18951 : :
18952 [ + + ]: 2071 : appendPQExpBuffer(q, "%s%s",
18953 : : (k == 0) ? "" : ", ",
18954 : : fmtId(attname));
18955 : : }
18956 [ + + ]: 1470 : if (coninfo->conperiod)
18957 : 108 : appendPQExpBufferStr(q, " WITHOUT OVERLAPS");
18958 : :
18959 [ + + ]: 1470 : if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
18960 : 20 : appendPQExpBufferStr(q, ") INCLUDE (");
18961 : :
18962 [ + + ]: 1510 : for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
18963 : : {
18964 : 40 : int indkey = indxinfo->indkeys[k];
18965 : : const char *attname;
18966 : :
18967 [ - + ]: 40 : if (indkey == InvalidAttrNumber)
18968 : 0 : break;
18969 : 40 : attname = getAttrName(indkey, tbinfo);
18970 : :
18971 : 80 : appendPQExpBuffer(q, "%s%s",
18972 [ + + ]: 40 : (k == indxinfo->indnkeyattrs) ? "" : ", ",
18973 : : fmtId(attname));
18974 : : }
18975 : :
18976 : 1470 : appendPQExpBufferChar(q, ')');
18977 : :
18978 [ - + ]: 1470 : if (nonemptyReloptions(indxinfo->indreloptions))
18979 : : {
18980 : 0 : appendPQExpBufferStr(q, " WITH (");
18981 : 0 : appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
18982 : 0 : appendPQExpBufferChar(q, ')');
18983 : : }
18984 : :
18985 [ + + ]: 1470 : if (coninfo->condeferrable)
18986 : : {
18987 : 25 : appendPQExpBufferStr(q, " DEFERRABLE");
18988 [ + + ]: 25 : if (coninfo->condeferred)
18989 : 15 : appendPQExpBufferStr(q, " INITIALLY DEFERRED");
18990 : : }
18991 : :
18992 : 1470 : appendPQExpBufferStr(q, ";\n");
18993 : : }
18994 : :
18995 : : /*
18996 : : * Append ALTER TABLE commands as needed to set properties that we
18997 : : * only have ALTER TABLE syntax for. Keep this in sync with the
18998 : : * similar code in dumpIndex!
18999 : : */
19000 : :
19001 : : /* If the index is clustered, we need to record that. */
19002 [ + + ]: 1524 : if (indxinfo->indisclustered)
19003 : : {
19004 : 34 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
19005 : 34 : fmtQualifiedDumpable(tbinfo));
19006 : : /* index name is not qualified in this syntax */
19007 : 34 : appendPQExpBuffer(q, " ON %s;\n",
19008 : 34 : fmtId(indxinfo->dobj.name));
19009 : : }
19010 : :
19011 : : /* Per-column statistics targets, if any */
19012 : 1524 : appendIndexStatTargets(q, indxinfo);
19013 : :
19014 : : /* If the index defines identity, we need to record that. */
19015 [ - + ]: 1524 : if (indxinfo->indisreplident)
19016 : : {
19017 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
19018 : 0 : fmtQualifiedDumpable(tbinfo));
19019 : : /* index name is not qualified in this syntax */
19020 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
19021 : 0 : fmtId(indxinfo->dobj.name));
19022 : : }
19023 : :
19024 : : /* Indexes can depend on extensions */
19025 : 1524 : append_depends_on_extension(fout, q, &indxinfo->dobj,
19026 : : "pg_catalog.pg_class", "INDEX",
19027 : 1524 : fmtQualifiedDumpable(indxinfo));
19028 : :
19029 : 1524 : appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
19030 : 1524 : fmtQualifiedDumpable(tbinfo));
19031 : 1524 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19032 : 1524 : fmtId(coninfo->dobj.name));
19033 : :
19034 : 1524 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
19035 : :
19036 [ + - ]: 1524 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19037 : 1524 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19038 : 1524 : ARCHIVE_OPTS(.tag = tag,
19039 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19040 : : .tablespace = indxinfo->tablespace,
19041 : : .owner = tbinfo->rolname,
19042 : : .description = "CONSTRAINT",
19043 : : .section = SECTION_POST_DATA,
19044 : : .createStmt = q->data,
19045 : : .dropStmt = delq->data));
19046 : : }
19047 [ + + ]: 993 : else if (coninfo->contype == 'f')
19048 : : {
19049 : : char *only;
19050 : :
19051 : : /*
19052 : : * Foreign keys on partitioned tables are always declared as
19053 : : * inheriting to partitions; for all other cases, emit them as
19054 : : * applying ONLY directly to the named table, because that's how they
19055 : : * work for regular inherited tables.
19056 : : */
19057 [ + + ]: 163 : only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
19058 : :
19059 : : /*
19060 : : * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
19061 : : * current table data is not processed
19062 : : */
19063 : 163 : appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
19064 : 163 : only, fmtQualifiedDumpable(tbinfo));
19065 : 163 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19066 : 163 : fmtId(coninfo->dobj.name),
19067 : 163 : coninfo->condef);
19068 : :
19069 : 163 : appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
19070 : 163 : only, fmtQualifiedDumpable(tbinfo));
19071 : 163 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19072 : 163 : fmtId(coninfo->dobj.name));
19073 : :
19074 : 163 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
19075 : :
19076 [ + - ]: 163 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19077 : 163 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19078 : 163 : ARCHIVE_OPTS(.tag = tag,
19079 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19080 : : .owner = tbinfo->rolname,
19081 : : .description = "FK CONSTRAINT",
19082 : : .section = SECTION_POST_DATA,
19083 : : .createStmt = q->data,
19084 : : .dropStmt = delq->data));
19085 : : }
19086 [ + + + - : 830 : else if ((coninfo->contype == 'c' || coninfo->contype == 'n') && tbinfo)
+ + ]
19087 : : {
19088 : : /* CHECK or invalid not-null constraint on a table */
19089 : :
19090 : : /* Ignore if not to be dumped separately, or if it was inherited */
19091 [ + + + + ]: 662 : if (coninfo->separate && coninfo->conislocal)
19092 : : {
19093 : : const char *keyword;
19094 : :
19095 [ + + ]: 109 : if (coninfo->contype == 'c')
19096 : 45 : keyword = "CHECK CONSTRAINT";
19097 : : else
19098 : 64 : keyword = "CONSTRAINT";
19099 : :
19100 : : /* not ONLY since we want it to propagate to children */
19101 : 109 : appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
19102 : 109 : fmtQualifiedDumpable(tbinfo));
19103 : 109 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19104 : 109 : fmtId(coninfo->dobj.name),
19105 : 109 : coninfo->condef);
19106 : :
19107 : 109 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
19108 : 109 : fmtQualifiedDumpable(tbinfo));
19109 : 109 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19110 : 109 : fmtId(coninfo->dobj.name));
19111 : :
19112 : 109 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
19113 : :
19114 [ + - ]: 109 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19115 : 109 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19116 : 109 : ARCHIVE_OPTS(.tag = tag,
19117 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19118 : : .owner = tbinfo->rolname,
19119 : : .description = keyword,
19120 : : .section = SECTION_POST_DATA,
19121 : : .createStmt = q->data,
19122 : : .dropStmt = delq->data));
19123 : : }
19124 : : }
19125 [ + - ]: 168 : else if (tbinfo == NULL)
19126 : : {
19127 : : /* CHECK, NOT NULL constraint on a domain */
19128 : 168 : TypeInfo *tyinfo = coninfo->condomain;
19129 : :
19130 : : Assert(coninfo->contype == 'c' || coninfo->contype == 'n');
19131 : :
19132 : : /* Ignore if not to be dumped separately */
19133 [ + + ]: 168 : if (coninfo->separate)
19134 : : {
19135 : : const char *keyword;
19136 : :
19137 [ + - ]: 5 : if (coninfo->contype == 'c')
19138 : 5 : keyword = "CHECK CONSTRAINT";
19139 : : else
19140 : 0 : keyword = "CONSTRAINT";
19141 : :
19142 : 5 : appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
19143 : 5 : fmtQualifiedDumpable(tyinfo));
19144 : 5 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19145 : 5 : fmtId(coninfo->dobj.name),
19146 : 5 : coninfo->condef);
19147 : :
19148 : 5 : appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
19149 : 5 : fmtQualifiedDumpable(tyinfo));
19150 : 5 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19151 : 5 : fmtId(coninfo->dobj.name));
19152 : :
19153 : 5 : tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
19154 : :
19155 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19156 : 5 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19157 : 5 : ARCHIVE_OPTS(.tag = tag,
19158 : : .namespace = tyinfo->dobj.namespace->dobj.name,
19159 : : .owner = tyinfo->rolname,
19160 : : .description = keyword,
19161 : : .section = SECTION_POST_DATA,
19162 : : .createStmt = q->data,
19163 : : .dropStmt = delq->data));
19164 : :
19165 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19166 : : {
19167 : 5 : PQExpBuffer conprefix = createPQExpBuffer();
19168 : 5 : char *qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
19169 : :
19170 : 5 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
19171 : 5 : fmtId(coninfo->dobj.name));
19172 : :
19173 : 5 : dumpComment(fout, conprefix->data, qtypname,
19174 : 5 : tyinfo->dobj.namespace->dobj.name,
19175 : : tyinfo->rolname,
19176 : 5 : coninfo->dobj.catId, 0, coninfo->dobj.dumpId);
19177 : 5 : destroyPQExpBuffer(conprefix);
19178 : 5 : pg_free(qtypname);
19179 : : }
19180 : : }
19181 : : }
19182 : : else
19183 : : {
19184 : 0 : pg_fatal("unrecognized constraint type: %c",
19185 : : coninfo->contype);
19186 : : }
19187 : :
19188 : : /* Dump Constraint Comments --- only works for table constraints */
19189 [ + + + + ]: 2517 : if (tbinfo && coninfo->separate &&
19190 [ + + ]: 1826 : coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19191 : 49 : dumpTableConstraintComment(fout, coninfo);
19192 : :
19193 : 2517 : pfree(tag);
19194 : 2517 : destroyPQExpBuffer(q);
19195 : 2517 : destroyPQExpBuffer(delq);
19196 : : }
19197 : :
19198 : : /*
19199 : : * dumpTableConstraintComment --- dump a constraint's comment if any
19200 : : *
19201 : : * This is split out because we need the function in two different places
19202 : : * depending on whether the constraint is dumped as part of CREATE TABLE
19203 : : * or as a separate ALTER command.
19204 : : */
19205 : : static void
19206 : 88 : dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
19207 : : {
19208 : 88 : TableInfo *tbinfo = coninfo->contable;
19209 : 88 : PQExpBuffer conprefix = createPQExpBuffer();
19210 : : char *qtabname;
19211 : :
19212 : 88 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19213 : :
19214 : 88 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON",
19215 : 88 : fmtId(coninfo->dobj.name));
19216 : :
19217 [ + - ]: 88 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19218 : 88 : dumpComment(fout, conprefix->data, qtabname,
19219 : 88 : tbinfo->dobj.namespace->dobj.name,
19220 : : tbinfo->rolname,
19221 : : coninfo->dobj.catId, 0,
19222 [ + + ]: 88 : coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
19223 : :
19224 : 88 : destroyPQExpBuffer(conprefix);
19225 : 88 : pg_free(qtabname);
19226 : 88 : }
19227 : :
19228 : : static inline SeqType
19229 : 663 : parse_sequence_type(const char *name)
19230 : : {
19231 [ + - ]: 1475 : for (size_t i = 0; i < lengthof(SeqTypeNames); i++)
19232 : : {
19233 [ + + ]: 1475 : if (strcmp(SeqTypeNames[i], name) == 0)
19234 : 663 : return (SeqType) i;
19235 : : }
19236 : :
19237 : 0 : pg_fatal("unrecognized sequence type: %s", name);
19238 : : return (SeqType) 0; /* keep compiler quiet */
19239 : : }
19240 : :
19241 : : /*
19242 : : * bsearch() comparator for SequenceItem
19243 : : */
19244 : : static int
19245 : 3008 : SequenceItemCmp(const void *p1, const void *p2)
19246 : : {
19247 : 3008 : SequenceItem v1 = *((const SequenceItem *) p1);
19248 : 3008 : SequenceItem v2 = *((const SequenceItem *) p2);
19249 : :
19250 : 3008 : return pg_cmp_u32(v1.oid, v2.oid);
19251 : : }
19252 : :
19253 : : /*
19254 : : * collectSequences
19255 : : *
19256 : : * Construct a table of sequence information. This table is sorted by OID for
19257 : : * speed in lookup.
19258 : : */
19259 : : static void
19260 : 215 : collectSequences(Archive *fout)
19261 : : {
19262 : : PGresult *res;
19263 : : const char *query;
19264 : :
19265 : : /*
19266 : : * Since version 18, we can gather the sequence data in this query with
19267 : : * pg_get_sequence_data(), but we only do so for non-schema-only dumps.
19268 : : */
19269 [ + - ]: 215 : if (fout->remoteVersion < 180000 ||
19270 [ + + + + ]: 215 : (!fout->dopt->dumpData && !fout->dopt->sequence_data))
19271 : 9 : query = "SELECT seqrelid, format_type(seqtypid, NULL), "
19272 : : "seqstart, seqincrement, "
19273 : : "seqmax, seqmin, "
19274 : : "seqcache, seqcycle, "
19275 : : "NULL, 'f' "
19276 : : "FROM pg_catalog.pg_sequence "
19277 : : "ORDER BY seqrelid";
19278 : : else
19279 : 206 : query = "SELECT seqrelid, format_type(seqtypid, NULL), "
19280 : : "seqstart, seqincrement, "
19281 : : "seqmax, seqmin, "
19282 : : "seqcache, seqcycle, "
19283 : : "last_value, is_called "
19284 : : "FROM pg_catalog.pg_sequence, "
19285 : : "pg_get_sequence_data(seqrelid) "
19286 : : "ORDER BY seqrelid;";
19287 : :
19288 : 215 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
19289 : :
19290 : 215 : nsequences = PQntuples(res);
19291 : 215 : sequences = pg_malloc_array(SequenceItem, nsequences);
19292 : :
19293 [ + + ]: 878 : for (int i = 0; i < nsequences; i++)
19294 : : {
19295 : 663 : sequences[i].oid = atooid(PQgetvalue(res, i, 0));
19296 : 663 : sequences[i].seqtype = parse_sequence_type(PQgetvalue(res, i, 1));
19297 : 663 : sequences[i].startv = strtoi64(PQgetvalue(res, i, 2), NULL, 10);
19298 : 663 : sequences[i].incby = strtoi64(PQgetvalue(res, i, 3), NULL, 10);
19299 : 663 : sequences[i].maxv = strtoi64(PQgetvalue(res, i, 4), NULL, 10);
19300 : 663 : sequences[i].minv = strtoi64(PQgetvalue(res, i, 5), NULL, 10);
19301 : 663 : sequences[i].cache = strtoi64(PQgetvalue(res, i, 6), NULL, 10);
19302 : 663 : sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0);
19303 : 663 : sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10);
19304 : 663 : sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0);
19305 [ + + - + ]: 663 : sequences[i].null_seqtuple = (PQgetisnull(res, i, 8) || PQgetisnull(res, i, 9));
19306 : : }
19307 : :
19308 : 215 : PQclear(res);
19309 : 215 : }
19310 : :
19311 : : /*
19312 : : * dumpSequence
19313 : : * write the declaration (not data) of one user-defined sequence
19314 : : */
19315 : : static void
19316 : 397 : dumpSequence(Archive *fout, const TableInfo *tbinfo)
19317 : : {
19318 : 397 : DumpOptions *dopt = fout->dopt;
19319 : : SequenceItem *seq;
19320 : : bool is_ascending;
19321 : : int64 default_minv,
19322 : : default_maxv;
19323 : 397 : PQExpBuffer query = createPQExpBuffer();
19324 : 397 : PQExpBuffer delqry = createPQExpBuffer();
19325 : : char *qseqname;
19326 : 397 : TableInfo *owning_tab = NULL;
19327 : 397 : SequenceItem key = {0};
19328 : :
19329 : 397 : qseqname = pg_strdup(fmtId(tbinfo->dobj.name));
19330 : :
19331 : : /*
19332 : : * The sequence information is gathered in a sorted table before any calls
19333 : : * to dumpSequence(). See collectSequences() for more information.
19334 : : */
19335 : : Assert(sequences);
19336 : :
19337 : 397 : key.oid = tbinfo->dobj.catId.oid;
19338 : 397 : seq = bsearch(&key, sequences, nsequences,
19339 : : sizeof(SequenceItem), SequenceItemCmp);
19340 : :
19341 : : /* Calculate default limits for a sequence of this type */
19342 : 397 : is_ascending = (seq->incby >= 0);
19343 [ + + ]: 397 : if (seq->seqtype == SEQTYPE_SMALLINT)
19344 : : {
19345 [ + + ]: 25 : default_minv = is_ascending ? 1 : PG_INT16_MIN;
19346 [ + + ]: 25 : default_maxv = is_ascending ? PG_INT16_MAX : -1;
19347 : : }
19348 [ + + ]: 372 : else if (seq->seqtype == SEQTYPE_INTEGER)
19349 : : {
19350 [ + + ]: 306 : default_minv = is_ascending ? 1 : PG_INT32_MIN;
19351 [ + + ]: 306 : default_maxv = is_ascending ? PG_INT32_MAX : -1;
19352 : : }
19353 [ + - ]: 66 : else if (seq->seqtype == SEQTYPE_BIGINT)
19354 : : {
19355 [ + + ]: 66 : default_minv = is_ascending ? 1 : PG_INT64_MIN;
19356 [ + + ]: 66 : default_maxv = is_ascending ? PG_INT64_MAX : -1;
19357 : : }
19358 : : else
19359 : : {
19360 : 0 : pg_fatal("unrecognized sequence type: %d", seq->seqtype);
19361 : : default_minv = default_maxv = 0; /* keep compiler quiet */
19362 : : }
19363 : :
19364 : : /*
19365 : : * Identity sequences are not to be dropped separately.
19366 : : */
19367 [ + + ]: 397 : if (!tbinfo->is_identity_sequence)
19368 : : {
19369 : 237 : appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n",
19370 : 237 : fmtQualifiedDumpable(tbinfo));
19371 : : }
19372 : :
19373 : 397 : resetPQExpBuffer(query);
19374 : :
19375 [ + + ]: 397 : if (dopt->binary_upgrade)
19376 : : {
19377 : 66 : binary_upgrade_set_pg_class_oids(fout, query,
19378 : 66 : tbinfo->dobj.catId.oid);
19379 : :
19380 : : /*
19381 : : * In older PG versions a sequence will have a pg_type entry, but v14
19382 : : * and up don't use that, so don't attempt to preserve the type OID.
19383 : : */
19384 : : }
19385 : :
19386 [ + + ]: 397 : if (tbinfo->is_identity_sequence)
19387 : : {
19388 : 160 : owning_tab = findTableByOid(tbinfo->owning_tab);
19389 : :
19390 : 160 : appendPQExpBuffer(query,
19391 : : "ALTER TABLE %s ",
19392 : 160 : fmtQualifiedDumpable(owning_tab));
19393 : 160 : appendPQExpBuffer(query,
19394 : : "ALTER COLUMN %s ADD GENERATED ",
19395 : 160 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19396 [ + + ]: 160 : if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS)
19397 : 120 : appendPQExpBufferStr(query, "ALWAYS");
19398 [ + - ]: 40 : else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
19399 : 40 : appendPQExpBufferStr(query, "BY DEFAULT");
19400 : 160 : appendPQExpBuffer(query, " AS IDENTITY (\n SEQUENCE NAME %s\n",
19401 : 160 : fmtQualifiedDumpable(tbinfo));
19402 : :
19403 : : /*
19404 : : * Emit persistence option only if it's different from the owning
19405 : : * table's. This avoids using this new syntax unnecessarily.
19406 : : */
19407 [ + + ]: 160 : if (tbinfo->relpersistence != owning_tab->relpersistence)
19408 : 10 : appendPQExpBuffer(query, " %s\n",
19409 [ + + ]: 10 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19410 : : "UNLOGGED" : "LOGGED");
19411 : : }
19412 : : else
19413 : : {
19414 : 237 : appendPQExpBuffer(query,
19415 : : "CREATE %sSEQUENCE %s\n",
19416 [ + + ]: 237 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19417 : : "UNLOGGED " : "",
19418 : 237 : fmtQualifiedDumpable(tbinfo));
19419 : :
19420 [ + + ]: 237 : if (seq->seqtype != SEQTYPE_BIGINT)
19421 : 186 : appendPQExpBuffer(query, " AS %s\n", SeqTypeNames[seq->seqtype]);
19422 : : }
19423 : :
19424 : 397 : appendPQExpBuffer(query, " START WITH " INT64_FORMAT "\n", seq->startv);
19425 : :
19426 : 397 : appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT "\n", seq->incby);
19427 : :
19428 [ + + ]: 397 : if (seq->minv != default_minv)
19429 : 15 : appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT "\n", seq->minv);
19430 : : else
19431 : 382 : appendPQExpBufferStr(query, " NO MINVALUE\n");
19432 : :
19433 [ + + ]: 397 : if (seq->maxv != default_maxv)
19434 : 15 : appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT "\n", seq->maxv);
19435 : : else
19436 : 382 : appendPQExpBufferStr(query, " NO MAXVALUE\n");
19437 : :
19438 : 397 : appendPQExpBuffer(query,
19439 : : " CACHE " INT64_FORMAT "%s",
19440 [ + + ]: 397 : seq->cache, (seq->cycled ? "\n CYCLE" : ""));
19441 : :
19442 [ + + ]: 397 : if (tbinfo->is_identity_sequence)
19443 : 160 : appendPQExpBufferStr(query, "\n);\n");
19444 : : else
19445 : 237 : appendPQExpBufferStr(query, ";\n");
19446 : :
19447 : : /* binary_upgrade: no need to clear TOAST table oid */
19448 : :
19449 [ + + ]: 397 : if (dopt->binary_upgrade)
19450 : 66 : binary_upgrade_extension_member(query, &tbinfo->dobj,
19451 : : "SEQUENCE", qseqname,
19452 : 66 : tbinfo->dobj.namespace->dobj.name);
19453 : :
19454 [ + - ]: 397 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19455 : 397 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
19456 : 397 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19457 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19458 : : .owner = tbinfo->rolname,
19459 : : .description = "SEQUENCE",
19460 : : .section = SECTION_PRE_DATA,
19461 : : .createStmt = query->data,
19462 : : .dropStmt = delqry->data));
19463 : :
19464 : : /*
19465 : : * If the sequence is owned by a table column, emit the ALTER for it as a
19466 : : * separate TOC entry immediately following the sequence's own entry. It's
19467 : : * OK to do this rather than using full sorting logic, because the
19468 : : * dependency that tells us it's owned will have forced the table to be
19469 : : * created first. We can't just include the ALTER in the TOC entry
19470 : : * because it will fail if we haven't reassigned the sequence owner to
19471 : : * match the table's owner.
19472 : : *
19473 : : * We need not schema-qualify the table reference because both sequence
19474 : : * and table must be in the same schema.
19475 : : */
19476 [ + + + + ]: 397 : if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence)
19477 : : {
19478 : 141 : owning_tab = findTableByOid(tbinfo->owning_tab);
19479 : :
19480 [ - + ]: 141 : if (owning_tab == NULL)
19481 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
19482 : : tbinfo->owning_tab, tbinfo->dobj.catId.oid);
19483 : :
19484 [ + + ]: 141 : if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION)
19485 : : {
19486 : 139 : resetPQExpBuffer(query);
19487 : 139 : appendPQExpBuffer(query, "ALTER SEQUENCE %s",
19488 : 139 : fmtQualifiedDumpable(tbinfo));
19489 : 139 : appendPQExpBuffer(query, " OWNED BY %s",
19490 : 139 : fmtQualifiedDumpable(owning_tab));
19491 : 139 : appendPQExpBuffer(query, ".%s;\n",
19492 : 139 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19493 : :
19494 [ + - ]: 139 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19495 : 139 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19496 : 139 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19497 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19498 : : .owner = tbinfo->rolname,
19499 : : .description = "SEQUENCE OWNED BY",
19500 : : .section = SECTION_PRE_DATA,
19501 : : .createStmt = query->data,
19502 : : .deps = &(tbinfo->dobj.dumpId),
19503 : : .nDeps = 1));
19504 : : }
19505 : : }
19506 : :
19507 : : /* Dump Sequence Comments and Security Labels */
19508 [ - + ]: 397 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19509 : 0 : dumpComment(fout, "SEQUENCE", qseqname,
19510 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19511 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19512 : :
19513 [ - + ]: 397 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19514 : 0 : dumpSecLabel(fout, "SEQUENCE", qseqname,
19515 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19516 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19517 : :
19518 : 397 : destroyPQExpBuffer(query);
19519 : 397 : destroyPQExpBuffer(delqry);
19520 : 397 : pg_free(qseqname);
19521 : 397 : }
19522 : :
19523 : : /*
19524 : : * dumpSequenceData
19525 : : * write the data of one user-defined sequence
19526 : : */
19527 : : static void
19528 : 415 : dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo)
19529 : : {
19530 : 415 : TableInfo *tbinfo = tdinfo->tdtable;
19531 : : int64 last;
19532 : : bool called;
19533 : : PQExpBuffer query;
19534 : :
19535 : : /* needn't bother if not dumping sequence data */
19536 [ + + + + ]: 415 : if (!fout->dopt->dumpData && !fout->dopt->sequence_data)
19537 : 1 : return;
19538 : :
19539 : 414 : query = createPQExpBuffer();
19540 : :
19541 : : /*
19542 : : * For versions >= 18, the sequence information is gathered in the sorted
19543 : : * array before any calls to dumpSequenceData(). See collectSequences()
19544 : : * for more information.
19545 : : *
19546 : : * For older versions, we have to query the sequence relations
19547 : : * individually.
19548 : : */
19549 [ - + ]: 414 : if (fout->remoteVersion < 180000)
19550 : : {
19551 : : PGresult *res;
19552 : :
19553 : 0 : appendPQExpBuffer(query,
19554 : : "SELECT last_value, is_called FROM %s",
19555 : 0 : fmtQualifiedDumpable(tbinfo));
19556 : :
19557 : 0 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19558 : :
19559 [ # # ]: 0 : if (PQntuples(res) != 1)
19560 : 0 : pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)",
19561 : : "query to get data of sequence \"%s\" returned %d rows (expected 1)",
19562 : : PQntuples(res)),
19563 : : tbinfo->dobj.name, PQntuples(res));
19564 : :
19565 : 0 : last = strtoi64(PQgetvalue(res, 0, 0), NULL, 10);
19566 : 0 : called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
19567 : :
19568 : 0 : PQclear(res);
19569 : : }
19570 : : else
19571 : : {
19572 : 414 : SequenceItem key = {0};
19573 : : SequenceItem *entry;
19574 : :
19575 : : Assert(sequences);
19576 : : Assert(tbinfo->dobj.catId.oid);
19577 : :
19578 : 414 : key.oid = tbinfo->dobj.catId.oid;
19579 : 414 : entry = bsearch(&key, sequences, nsequences,
19580 : : sizeof(SequenceItem), SequenceItemCmp);
19581 : :
19582 [ - + ]: 414 : if (entry->null_seqtuple)
19583 : 0 : pg_fatal("failed to get data for sequence \"%s\"; user may lack "
19584 : : "SELECT privilege on the sequence or the sequence may "
19585 : : "have been concurrently dropped",
19586 : : tbinfo->dobj.name);
19587 : :
19588 : 414 : last = entry->last_value;
19589 : 414 : called = entry->is_called;
19590 : : }
19591 : :
19592 : 414 : resetPQExpBuffer(query);
19593 : 414 : appendPQExpBufferStr(query, "SELECT pg_catalog.setval(");
19594 : 414 : appendStringLiteralAH(query, fmtQualifiedDumpable(tbinfo), fout);
19595 [ + + ]: 414 : appendPQExpBuffer(query, ", " INT64_FORMAT ", %s);\n",
19596 : : last, (called ? "true" : "false"));
19597 : :
19598 [ + - ]: 414 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
19599 : 414 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19600 : 414 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19601 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19602 : : .owner = tbinfo->rolname,
19603 : : .description = "SEQUENCE SET",
19604 : : .section = SECTION_DATA,
19605 : : .createStmt = query->data,
19606 : : .deps = &(tbinfo->dobj.dumpId),
19607 : : .nDeps = 1));
19608 : :
19609 : 414 : destroyPQExpBuffer(query);
19610 : : }
19611 : :
19612 : : /*
19613 : : * dumpTrigger
19614 : : * write the declaration of one user-defined table trigger
19615 : : */
19616 : : static void
19617 : 535 : dumpTrigger(Archive *fout, const TriggerInfo *tginfo)
19618 : : {
19619 : 535 : DumpOptions *dopt = fout->dopt;
19620 : 535 : TableInfo *tbinfo = tginfo->tgtable;
19621 : : PQExpBuffer query;
19622 : : PQExpBuffer delqry;
19623 : : PQExpBuffer trigprefix;
19624 : : PQExpBuffer trigidentity;
19625 : : char *qtabname;
19626 : : char *tag;
19627 : :
19628 : : /* Do nothing if not dumping schema */
19629 [ + + ]: 535 : if (!dopt->dumpSchema)
19630 : 33 : return;
19631 : :
19632 : 502 : query = createPQExpBuffer();
19633 : 502 : delqry = createPQExpBuffer();
19634 : 502 : trigprefix = createPQExpBuffer();
19635 : 502 : trigidentity = createPQExpBuffer();
19636 : :
19637 : 502 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19638 : :
19639 : 502 : appendPQExpBuffer(trigidentity, "%s ", fmtId(tginfo->dobj.name));
19640 : 502 : appendPQExpBuffer(trigidentity, "ON %s", fmtQualifiedDumpable(tbinfo));
19641 : :
19642 : 502 : appendPQExpBuffer(query, "%s;\n", tginfo->tgdef);
19643 : 502 : appendPQExpBuffer(delqry, "DROP TRIGGER %s;\n", trigidentity->data);
19644 : :
19645 : : /* Triggers can depend on extensions */
19646 : 502 : append_depends_on_extension(fout, query, &tginfo->dobj,
19647 : : "pg_catalog.pg_trigger", "TRIGGER",
19648 : 502 : trigidentity->data);
19649 : :
19650 [ + + ]: 502 : if (tginfo->tgispartition)
19651 : : {
19652 : : Assert(tbinfo->ispartition);
19653 : :
19654 : : /*
19655 : : * Partition triggers only appear here because their 'tgenabled' flag
19656 : : * differs from its parent's. The trigger is created already, so
19657 : : * remove the CREATE and replace it with an ALTER. (Clear out the
19658 : : * DROP query too, so that pg_dump --create does not cause errors.)
19659 : : */
19660 : 115 : resetPQExpBuffer(query);
19661 : 115 : resetPQExpBuffer(delqry);
19662 : 115 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19663 [ - + ]: 115 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19664 : 115 : fmtQualifiedDumpable(tbinfo));
19665 [ + - + + : 115 : switch (tginfo->tgenabled)
- ]
19666 : : {
19667 : 40 : case 'f':
19668 : : case 'D':
19669 : 40 : appendPQExpBufferStr(query, "DISABLE");
19670 : 40 : break;
19671 : 0 : case 't':
19672 : : case 'O':
19673 : 0 : appendPQExpBufferStr(query, "ENABLE");
19674 : 0 : break;
19675 : 35 : case 'R':
19676 : 35 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19677 : 35 : break;
19678 : 40 : case 'A':
19679 : 40 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19680 : 40 : break;
19681 : : }
19682 : 115 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19683 : 115 : fmtId(tginfo->dobj.name));
19684 : : }
19685 [ + - - + ]: 387 : else if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
19686 : : {
19687 : 0 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19688 [ # # ]: 0 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19689 : 0 : fmtQualifiedDumpable(tbinfo));
19690 [ # # # # ]: 0 : switch (tginfo->tgenabled)
19691 : : {
19692 : 0 : case 'D':
19693 : : case 'f':
19694 : 0 : appendPQExpBufferStr(query, "DISABLE");
19695 : 0 : break;
19696 : 0 : case 'A':
19697 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19698 : 0 : break;
19699 : 0 : case 'R':
19700 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19701 : 0 : break;
19702 : 0 : default:
19703 : 0 : appendPQExpBufferStr(query, "ENABLE");
19704 : 0 : break;
19705 : : }
19706 : 0 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19707 : 0 : fmtId(tginfo->dobj.name));
19708 : : }
19709 : :
19710 : 502 : appendPQExpBuffer(trigprefix, "TRIGGER %s ON",
19711 : 502 : fmtId(tginfo->dobj.name));
19712 : :
19713 : 502 : tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name);
19714 : :
19715 [ + - ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19716 : 502 : ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
19717 : 502 : ARCHIVE_OPTS(.tag = tag,
19718 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19719 : : .owner = tbinfo->rolname,
19720 : : .description = "TRIGGER",
19721 : : .section = SECTION_POST_DATA,
19722 : : .createStmt = query->data,
19723 : : .dropStmt = delqry->data));
19724 : :
19725 [ - + ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19726 : 0 : dumpComment(fout, trigprefix->data, qtabname,
19727 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19728 : 0 : tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
19729 : :
19730 : 502 : pfree(tag);
19731 : 502 : destroyPQExpBuffer(query);
19732 : 502 : destroyPQExpBuffer(delqry);
19733 : 502 : destroyPQExpBuffer(trigprefix);
19734 : 502 : destroyPQExpBuffer(trigidentity);
19735 : 502 : pg_free(qtabname);
19736 : : }
19737 : :
19738 : : /*
19739 : : * dumpEventTrigger
19740 : : * write the declaration of one user-defined event trigger
19741 : : */
19742 : : static void
19743 : 44 : dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo)
19744 : : {
19745 : 44 : DumpOptions *dopt = fout->dopt;
19746 : : PQExpBuffer query;
19747 : : PQExpBuffer delqry;
19748 : : char *qevtname;
19749 : :
19750 : : /* Do nothing if not dumping schema */
19751 [ + + ]: 44 : if (!dopt->dumpSchema)
19752 : 6 : return;
19753 : :
19754 : 38 : query = createPQExpBuffer();
19755 : 38 : delqry = createPQExpBuffer();
19756 : :
19757 : 38 : qevtname = pg_strdup(fmtId(evtinfo->dobj.name));
19758 : :
19759 : 38 : appendPQExpBufferStr(query, "CREATE EVENT TRIGGER ");
19760 : 38 : appendPQExpBufferStr(query, qevtname);
19761 : 38 : appendPQExpBufferStr(query, " ON ");
19762 : 38 : appendPQExpBufferStr(query, fmtId(evtinfo->evtevent));
19763 : :
19764 [ + + ]: 38 : if (strcmp("", evtinfo->evttags) != 0)
19765 : : {
19766 : 5 : appendPQExpBufferStr(query, "\n WHEN TAG IN (");
19767 : 5 : appendPQExpBufferStr(query, evtinfo->evttags);
19768 : 5 : appendPQExpBufferChar(query, ')');
19769 : : }
19770 : :
19771 : 38 : appendPQExpBufferStr(query, "\n EXECUTE FUNCTION ");
19772 : 38 : appendPQExpBufferStr(query, evtinfo->evtfname);
19773 : 38 : appendPQExpBufferStr(query, "();\n");
19774 : :
19775 [ - + ]: 38 : if (evtinfo->evtenabled != 'O')
19776 : : {
19777 : 0 : appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ",
19778 : : qevtname);
19779 [ # # # # ]: 0 : switch (evtinfo->evtenabled)
19780 : : {
19781 : 0 : case 'D':
19782 : 0 : appendPQExpBufferStr(query, "DISABLE");
19783 : 0 : break;
19784 : 0 : case 'A':
19785 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19786 : 0 : break;
19787 : 0 : case 'R':
19788 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19789 : 0 : break;
19790 : 0 : default:
19791 : 0 : appendPQExpBufferStr(query, "ENABLE");
19792 : 0 : break;
19793 : : }
19794 : 0 : appendPQExpBufferStr(query, ";\n");
19795 : : }
19796 : :
19797 : 38 : appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n",
19798 : : qevtname);
19799 : :
19800 [ + + ]: 38 : if (dopt->binary_upgrade)
19801 : 2 : binary_upgrade_extension_member(query, &evtinfo->dobj,
19802 : : "EVENT TRIGGER", qevtname, NULL);
19803 : :
19804 [ + - ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19805 : 38 : ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId,
19806 : 38 : ARCHIVE_OPTS(.tag = evtinfo->dobj.name,
19807 : : .owner = evtinfo->evtowner,
19808 : : .description = "EVENT TRIGGER",
19809 : : .section = SECTION_POST_DATA,
19810 : : .createStmt = query->data,
19811 : : .dropStmt = delqry->data));
19812 : :
19813 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19814 : 0 : dumpComment(fout, "EVENT TRIGGER", qevtname,
19815 : 0 : NULL, evtinfo->evtowner,
19816 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19817 : :
19818 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19819 : 0 : dumpSecLabel(fout, "EVENT TRIGGER", qevtname,
19820 : 0 : NULL, evtinfo->evtowner,
19821 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19822 : :
19823 : 38 : destroyPQExpBuffer(query);
19824 : 38 : destroyPQExpBuffer(delqry);
19825 : 38 : pg_free(qevtname);
19826 : : }
19827 : :
19828 : : /*
19829 : : * dumpRule
19830 : : * Dump a rule
19831 : : */
19832 : : static void
19833 : 1201 : dumpRule(Archive *fout, const RuleInfo *rinfo)
19834 : : {
19835 : 1201 : DumpOptions *dopt = fout->dopt;
19836 : 1201 : TableInfo *tbinfo = rinfo->ruletable;
19837 : : bool is_view;
19838 : : PQExpBuffer query;
19839 : : PQExpBuffer cmd;
19840 : : PQExpBuffer delcmd;
19841 : : PQExpBuffer ruleprefix;
19842 : : char *qtabname;
19843 : : PGresult *res;
19844 : : char *tag;
19845 : :
19846 : : /* Do nothing if not dumping schema */
19847 [ + + ]: 1201 : if (!dopt->dumpSchema)
19848 : 70 : return;
19849 : :
19850 : : /*
19851 : : * If it is an ON SELECT rule that is created implicitly by CREATE VIEW,
19852 : : * we do not want to dump it as a separate object.
19853 : : */
19854 [ + + ]: 1131 : if (!rinfo->separate)
19855 : 906 : return;
19856 : :
19857 : : /*
19858 : : * If it's an ON SELECT rule, we want to print it as a view definition,
19859 : : * instead of a rule.
19860 : : */
19861 [ + + + - ]: 225 : is_view = (rinfo->ev_type == '1' && rinfo->is_instead);
19862 : :
19863 : 225 : query = createPQExpBuffer();
19864 : 225 : cmd = createPQExpBuffer();
19865 : 225 : delcmd = createPQExpBuffer();
19866 : 225 : ruleprefix = createPQExpBuffer();
19867 : :
19868 : 225 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19869 : :
19870 [ + + ]: 225 : if (is_view)
19871 : : {
19872 : : PQExpBuffer result;
19873 : :
19874 : : /*
19875 : : * We need OR REPLACE here because we'll be replacing a dummy view.
19876 : : * Otherwise this should look largely like the regular view dump code.
19877 : : */
19878 : 24 : appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s",
19879 : 24 : fmtQualifiedDumpable(tbinfo));
19880 [ - + ]: 24 : if (nonemptyReloptions(tbinfo->reloptions))
19881 : : {
19882 : 0 : appendPQExpBufferStr(cmd, " WITH (");
19883 : 0 : appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout);
19884 : 0 : appendPQExpBufferChar(cmd, ')');
19885 : : }
19886 : 24 : result = createViewAsClause(fout, tbinfo);
19887 : 24 : appendPQExpBuffer(cmd, " AS\n%s", result->data);
19888 : 24 : destroyPQExpBuffer(result);
19889 [ - + ]: 24 : if (tbinfo->checkoption != NULL)
19890 : 0 : appendPQExpBuffer(cmd, "\n WITH %s CHECK OPTION",
19891 : : tbinfo->checkoption);
19892 : 24 : appendPQExpBufferStr(cmd, ";\n");
19893 : : }
19894 : : else
19895 : : {
19896 : : /* In the rule case, just print pg_get_ruledef's result verbatim */
19897 : 201 : appendPQExpBuffer(query,
19898 : : "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)",
19899 : 201 : rinfo->dobj.catId.oid);
19900 : :
19901 : 201 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19902 : :
19903 [ - + ]: 201 : if (PQntuples(res) != 1)
19904 : 0 : pg_fatal("query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned",
19905 : : rinfo->dobj.name, tbinfo->dobj.name);
19906 : :
19907 : 201 : printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
19908 : :
19909 : 201 : PQclear(res);
19910 : : }
19911 : :
19912 : : /*
19913 : : * Add the command to alter the rules replication firing semantics if it
19914 : : * differs from the default.
19915 : : */
19916 [ + + ]: 225 : if (rinfo->ev_enabled != 'O')
19917 : : {
19918 : 15 : appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtQualifiedDumpable(tbinfo));
19919 [ - - + - ]: 15 : switch (rinfo->ev_enabled)
19920 : : {
19921 : 0 : case 'A':
19922 : 0 : appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n",
19923 : 0 : fmtId(rinfo->dobj.name));
19924 : 0 : break;
19925 : 0 : case 'R':
19926 : 0 : appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n",
19927 : 0 : fmtId(rinfo->dobj.name));
19928 : 0 : break;
19929 : 15 : case 'D':
19930 : 15 : appendPQExpBuffer(cmd, "DISABLE RULE %s;\n",
19931 : 15 : fmtId(rinfo->dobj.name));
19932 : 15 : break;
19933 : : }
19934 : : }
19935 : :
19936 [ + + ]: 225 : if (is_view)
19937 : : {
19938 : : /*
19939 : : * We can't DROP a view's ON SELECT rule. Instead, use CREATE OR
19940 : : * REPLACE VIEW to replace the rule with something with minimal
19941 : : * dependencies.
19942 : : */
19943 : : PQExpBuffer result;
19944 : :
19945 : 24 : appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s",
19946 : 24 : fmtQualifiedDumpable(tbinfo));
19947 : 24 : result = createDummyViewAsClause(fout, tbinfo);
19948 : 24 : appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data);
19949 : 24 : destroyPQExpBuffer(result);
19950 : : }
19951 : : else
19952 : : {
19953 : 201 : appendPQExpBuffer(delcmd, "DROP RULE %s ",
19954 : 201 : fmtId(rinfo->dobj.name));
19955 : 201 : appendPQExpBuffer(delcmd, "ON %s;\n",
19956 : 201 : fmtQualifiedDumpable(tbinfo));
19957 : : }
19958 : :
19959 : 225 : appendPQExpBuffer(ruleprefix, "RULE %s ON",
19960 : 225 : fmtId(rinfo->dobj.name));
19961 : :
19962 : 225 : tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name);
19963 : :
19964 [ + - ]: 225 : if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19965 : 225 : ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
19966 : 225 : ARCHIVE_OPTS(.tag = tag,
19967 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19968 : : .owner = tbinfo->rolname,
19969 : : .description = "RULE",
19970 : : .section = SECTION_POST_DATA,
19971 : : .createStmt = cmd->data,
19972 : : .dropStmt = delcmd->data));
19973 : :
19974 : : /* Dump rule comments */
19975 [ - + ]: 225 : if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19976 : 0 : dumpComment(fout, ruleprefix->data, qtabname,
19977 : 0 : tbinfo->dobj.namespace->dobj.name,
19978 : : tbinfo->rolname,
19979 : 0 : rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
19980 : :
19981 : 225 : pfree(tag);
19982 : 225 : destroyPQExpBuffer(query);
19983 : 225 : destroyPQExpBuffer(cmd);
19984 : 225 : destroyPQExpBuffer(delcmd);
19985 : 225 : destroyPQExpBuffer(ruleprefix);
19986 : 225 : pg_free(qtabname);
19987 : : }
19988 : :
19989 : : /*
19990 : : * getExtensionMembership --- obtain extension membership data
19991 : : *
19992 : : * We need to identify objects that are extension members as soon as they're
19993 : : * loaded, so that we can correctly determine whether they need to be dumped.
19994 : : * Generally speaking, extension member objects will get marked as *not* to
19995 : : * be dumped, as they will be recreated by the single CREATE EXTENSION
19996 : : * command. However, in binary upgrade mode we still need to dump the members
19997 : : * individually.
19998 : : */
19999 : : void
20000 : 216 : getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
20001 : : int numExtensions)
20002 : : {
20003 : : PQExpBuffer query;
20004 : : PGresult *res;
20005 : : int ntups,
20006 : : i;
20007 : : int i_classid,
20008 : : i_objid,
20009 : : i_refobjid;
20010 : : ExtensionInfo *ext;
20011 : :
20012 : : /* Nothing to do if no extensions */
20013 [ - + ]: 216 : if (numExtensions == 0)
20014 : 0 : return;
20015 : :
20016 : 216 : query = createPQExpBuffer();
20017 : :
20018 : : /* refclassid constraint is redundant but may speed the search */
20019 : 216 : appendPQExpBufferStr(query, "SELECT "
20020 : : "classid, objid, refobjid "
20021 : : "FROM pg_depend "
20022 : : "WHERE refclassid = 'pg_extension'::regclass "
20023 : : "AND deptype = 'e' "
20024 : : "ORDER BY 3");
20025 : :
20026 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20027 : :
20028 : 216 : ntups = PQntuples(res);
20029 : :
20030 : 216 : i_classid = PQfnumber(res, "classid");
20031 : 216 : i_objid = PQfnumber(res, "objid");
20032 : 216 : i_refobjid = PQfnumber(res, "refobjid");
20033 : :
20034 : : /*
20035 : : * Since we ordered the SELECT by referenced ID, we can expect that
20036 : : * multiple entries for the same extension will appear together; this
20037 : : * saves on searches.
20038 : : */
20039 : 216 : ext = NULL;
20040 : :
20041 [ + + ]: 1680 : for (i = 0; i < ntups; i++)
20042 : : {
20043 : : CatalogId objId;
20044 : : Oid extId;
20045 : :
20046 : 1464 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
20047 : 1464 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
20048 : 1464 : extId = atooid(PQgetvalue(res, i, i_refobjid));
20049 : :
20050 [ + + ]: 1464 : if (ext == NULL ||
20051 [ + + ]: 1248 : ext->dobj.catId.oid != extId)
20052 : 247 : ext = findExtensionByOid(extId);
20053 : :
20054 [ - + ]: 1464 : if (ext == NULL)
20055 : : {
20056 : : /* shouldn't happen */
20057 : 0 : pg_log_warning("could not find referenced extension %u", extId);
20058 : 0 : continue;
20059 : : }
20060 : :
20061 : 1464 : recordExtensionMembership(objId, ext);
20062 : : }
20063 : :
20064 : 216 : PQclear(res);
20065 : :
20066 : 216 : destroyPQExpBuffer(query);
20067 : : }
20068 : :
20069 : : /*
20070 : : * processExtensionTables --- deal with extension configuration tables
20071 : : *
20072 : : * There are two parts to this process:
20073 : : *
20074 : : * 1. Identify and create dump records for extension configuration tables.
20075 : : *
20076 : : * Extensions can mark tables as "configuration", which means that the user
20077 : : * is able and expected to modify those tables after the extension has been
20078 : : * loaded. For these tables, we dump out only the data- the structure is
20079 : : * expected to be handled at CREATE EXTENSION time, including any indexes or
20080 : : * foreign keys, which brings us to-
20081 : : *
20082 : : * 2. Record FK dependencies between configuration tables.
20083 : : *
20084 : : * Due to the FKs being created at CREATE EXTENSION time and therefore before
20085 : : * the data is loaded, we have to work out what the best order for reloading
20086 : : * the data is, to avoid FK violations when the tables are restored. This is
20087 : : * not perfect- we can't handle circular dependencies and if any exist they
20088 : : * will cause an invalid dump to be produced (though at least all of the data
20089 : : * is included for a user to manually restore). This is currently documented
20090 : : * but perhaps we can provide a better solution in the future.
20091 : : */
20092 : : void
20093 : 215 : processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
20094 : : int numExtensions)
20095 : : {
20096 : 215 : DumpOptions *dopt = fout->dopt;
20097 : : PQExpBuffer query;
20098 : : PGresult *res;
20099 : : int ntups,
20100 : : i;
20101 : : int i_conrelid,
20102 : : i_confrelid;
20103 : :
20104 : : /* Nothing to do if no extensions */
20105 [ - + ]: 215 : if (numExtensions == 0)
20106 : 0 : return;
20107 : :
20108 : : /*
20109 : : * Identify extension configuration tables and create TableDataInfo
20110 : : * objects for them, ensuring their data will be dumped even though the
20111 : : * tables themselves won't be.
20112 : : *
20113 : : * Note that we create TableDataInfo objects even in schema-only mode, ie,
20114 : : * user data in a configuration table is treated like schema data. This
20115 : : * seems appropriate since system data in a config table would get
20116 : : * reloaded by CREATE EXTENSION. If the extension is not listed in the
20117 : : * list of extensions to be included, none of its data is dumped.
20118 : : */
20119 [ + + ]: 461 : for (i = 0; i < numExtensions; i++)
20120 : : {
20121 : 246 : ExtensionInfo *curext = &(extinfo[i]);
20122 : 246 : char *extconfig = curext->extconfig;
20123 : 246 : char *extcondition = curext->extcondition;
20124 : 246 : char **extconfigarray = NULL;
20125 : 246 : char **extconditionarray = NULL;
20126 : 246 : int nconfigitems = 0;
20127 : 246 : int nconditionitems = 0;
20128 : :
20129 : : /*
20130 : : * Check if this extension is listed as to include in the dump. If
20131 : : * not, any table data associated with it is discarded.
20132 : : */
20133 [ + + ]: 246 : if (extension_include_oids.head != NULL &&
20134 [ + + ]: 8 : !simple_oid_list_member(&extension_include_oids,
20135 : : curext->dobj.catId.oid))
20136 : 6 : continue;
20137 : :
20138 : : /*
20139 : : * Check if this extension is listed as to exclude in the dump. If
20140 : : * yes, any table data associated with it is discarded.
20141 : : */
20142 [ + + + + ]: 246 : if (extension_exclude_oids.head != NULL &&
20143 : 4 : simple_oid_list_member(&extension_exclude_oids,
20144 : : curext->dobj.catId.oid))
20145 : 2 : continue;
20146 : :
20147 [ + + - + ]: 240 : if (strlen(extconfig) != 0 || strlen(extcondition) != 0)
20148 : : {
20149 : : int j;
20150 : :
20151 [ - + ]: 20 : if (!parsePGArray(extconfig, &extconfigarray, &nconfigitems))
20152 : 0 : pg_fatal("could not parse %s array", "extconfig");
20153 [ - + ]: 20 : if (!parsePGArray(extcondition, &extconditionarray, &nconditionitems))
20154 : 0 : pg_fatal("could not parse %s array", "extcondition");
20155 [ - + ]: 20 : if (nconfigitems != nconditionitems)
20156 : 0 : pg_fatal("mismatched number of configurations and conditions for extension");
20157 : :
20158 [ + + ]: 60 : for (j = 0; j < nconfigitems; j++)
20159 : : {
20160 : : TableInfo *configtbl;
20161 : 40 : Oid configtbloid = atooid(extconfigarray[j]);
20162 : 40 : bool dumpobj =
20163 : 40 : curext->dobj.dump & DUMP_COMPONENT_DEFINITION;
20164 : :
20165 : 40 : configtbl = findTableByOid(configtbloid);
20166 [ - + ]: 40 : if (configtbl == NULL)
20167 : 0 : continue;
20168 : :
20169 : : /*
20170 : : * Tables of not-to-be-dumped extensions shouldn't be dumped
20171 : : * unless the table or its schema is explicitly included
20172 : : */
20173 [ + + ]: 40 : if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION))
20174 : : {
20175 : : /* check table explicitly requested */
20176 [ - + - - ]: 2 : if (table_include_oids.head != NULL &&
20177 : 0 : simple_oid_list_member(&table_include_oids,
20178 : : configtbloid))
20179 : 0 : dumpobj = true;
20180 : :
20181 : : /* check table's schema explicitly requested */
20182 [ + - ]: 2 : if (configtbl->dobj.namespace->dobj.dump &
20183 : : DUMP_COMPONENT_DATA)
20184 : 2 : dumpobj = true;
20185 : : }
20186 : :
20187 : : /* check table excluded by an exclusion switch */
20188 [ + + + + ]: 44 : if (table_exclude_oids.head != NULL &&
20189 : 4 : simple_oid_list_member(&table_exclude_oids,
20190 : : configtbloid))
20191 : 1 : dumpobj = false;
20192 : :
20193 : : /* check schema excluded by an exclusion switch */
20194 [ - + ]: 40 : if (simple_oid_list_member(&schema_exclude_oids,
20195 : 40 : configtbl->dobj.namespace->dobj.catId.oid))
20196 : 0 : dumpobj = false;
20197 : :
20198 [ + + ]: 40 : if (dumpobj)
20199 : : {
20200 : 39 : makeTableDataInfo(dopt, configtbl);
20201 [ + - ]: 39 : if (configtbl->dataObj != NULL)
20202 : : {
20203 [ - + ]: 39 : if (strlen(extconditionarray[j]) > 0)
20204 : 0 : configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
20205 : : }
20206 : : }
20207 : : }
20208 : : }
20209 [ + + ]: 240 : if (extconfigarray)
20210 : 20 : free(extconfigarray);
20211 [ + + ]: 240 : if (extconditionarray)
20212 : 20 : free(extconditionarray);
20213 : : }
20214 : :
20215 : : /*
20216 : : * Now that all the TableDataInfo objects have been created for all the
20217 : : * extensions, check their FK dependencies and register them to try and
20218 : : * dump the data out in an order that they can be restored in.
20219 : : *
20220 : : * Note that this is not a problem for user tables as their FKs are
20221 : : * recreated after the data has been loaded.
20222 : : */
20223 : :
20224 : 215 : query = createPQExpBuffer();
20225 : :
20226 : 215 : printfPQExpBuffer(query,
20227 : : "SELECT conrelid, confrelid "
20228 : : "FROM pg_constraint "
20229 : : "JOIN pg_depend ON (objid = confrelid) "
20230 : : "WHERE contype = 'f' "
20231 : : "AND refclassid = 'pg_extension'::regclass "
20232 : : "AND classid = 'pg_class'::regclass;");
20233 : :
20234 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20235 : 215 : ntups = PQntuples(res);
20236 : :
20237 : 215 : i_conrelid = PQfnumber(res, "conrelid");
20238 : 215 : i_confrelid = PQfnumber(res, "confrelid");
20239 : :
20240 : : /* Now get the dependencies and register them */
20241 [ - + ]: 215 : for (i = 0; i < ntups; i++)
20242 : : {
20243 : : Oid conrelid,
20244 : : confrelid;
20245 : : TableInfo *reftable,
20246 : : *contable;
20247 : :
20248 : 0 : conrelid = atooid(PQgetvalue(res, i, i_conrelid));
20249 : 0 : confrelid = atooid(PQgetvalue(res, i, i_confrelid));
20250 : 0 : contable = findTableByOid(conrelid);
20251 : 0 : reftable = findTableByOid(confrelid);
20252 : :
20253 [ # # ]: 0 : if (reftable == NULL ||
20254 [ # # # # ]: 0 : reftable->dataObj == NULL ||
20255 : 0 : contable == NULL ||
20256 [ # # ]: 0 : contable->dataObj == NULL)
20257 : 0 : continue;
20258 : :
20259 : : /*
20260 : : * Make referencing TABLE_DATA object depend on the referenced table's
20261 : : * TABLE_DATA object.
20262 : : */
20263 : 0 : addObjectDependency(&contable->dataObj->dobj,
20264 : 0 : reftable->dataObj->dobj.dumpId);
20265 : : }
20266 : 215 : PQclear(res);
20267 : 215 : destroyPQExpBuffer(query);
20268 : : }
20269 : :
20270 : : /*
20271 : : * getDependencies --- obtain available dependency data
20272 : : */
20273 : : static void
20274 : 215 : getDependencies(Archive *fout)
20275 : : {
20276 : : PQExpBuffer query;
20277 : : PGresult *res;
20278 : : int ntups,
20279 : : i;
20280 : : int i_classid,
20281 : : i_objid,
20282 : : i_refclassid,
20283 : : i_refobjid,
20284 : : i_deptype;
20285 : : DumpableObject *dobj,
20286 : : *refdobj;
20287 : :
20288 : 215 : pg_log_info("reading dependency data");
20289 : :
20290 : 215 : query = createPQExpBuffer();
20291 : :
20292 : : /*
20293 : : * Messy query to collect the dependency data we need. Note that we
20294 : : * ignore the sub-object column, so that dependencies of or on a column
20295 : : * look the same as dependencies of or on a whole table.
20296 : : *
20297 : : * PIN dependencies aren't interesting, and EXTENSION dependencies were
20298 : : * already processed by getExtensionMembership.
20299 : : */
20300 : 215 : appendPQExpBufferStr(query, "SELECT "
20301 : : "classid, objid, refclassid, refobjid, deptype "
20302 : : "FROM pg_depend "
20303 : : "WHERE deptype != 'p' AND deptype != 'e'\n");
20304 : :
20305 : : /*
20306 : : * Since we don't treat pg_amop entries as separate DumpableObjects, we
20307 : : * have to translate their dependencies into dependencies of their parent
20308 : : * opfamily. Ignore internal dependencies though, as those will point to
20309 : : * their parent opclass, which we needn't consider here (and if we did,
20310 : : * it'd just result in circular dependencies). Also, "loose" opfamily
20311 : : * entries will have dependencies on their parent opfamily, which we
20312 : : * should drop since they'd likewise become useless self-dependencies.
20313 : : * (But be sure to keep deps on *other* opfamilies; see amopsortfamily.)
20314 : : */
20315 : 215 : appendPQExpBufferStr(query, "UNION ALL\n"
20316 : : "SELECT 'pg_opfamily'::regclass AS classid, amopfamily AS objid, refclassid, refobjid, deptype "
20317 : : "FROM pg_depend d, pg_amop o "
20318 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20319 : : "classid = 'pg_amop'::regclass AND objid = o.oid "
20320 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amopfamily = refobjid)\n");
20321 : :
20322 : : /* Likewise for pg_amproc entries */
20323 : 215 : appendPQExpBufferStr(query, "UNION ALL\n"
20324 : : "SELECT 'pg_opfamily'::regclass AS classid, amprocfamily AS objid, refclassid, refobjid, deptype "
20325 : : "FROM pg_depend d, pg_amproc p "
20326 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20327 : : "classid = 'pg_amproc'::regclass AND objid = p.oid "
20328 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amprocfamily = refobjid)\n");
20329 : :
20330 : : /* Sort the output for efficiency below */
20331 : 215 : appendPQExpBufferStr(query, "ORDER BY 1,2");
20332 : :
20333 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20334 : :
20335 : 215 : ntups = PQntuples(res);
20336 : :
20337 : 215 : i_classid = PQfnumber(res, "classid");
20338 : 215 : i_objid = PQfnumber(res, "objid");
20339 : 215 : i_refclassid = PQfnumber(res, "refclassid");
20340 : 215 : i_refobjid = PQfnumber(res, "refobjid");
20341 : 215 : i_deptype = PQfnumber(res, "deptype");
20342 : :
20343 : : /*
20344 : : * Since we ordered the SELECT by referencing ID, we can expect that
20345 : : * multiple entries for the same object will appear together; this saves
20346 : : * on searches.
20347 : : */
20348 : 215 : dobj = NULL;
20349 : :
20350 [ + + ]: 466987 : for (i = 0; i < ntups; i++)
20351 : : {
20352 : : CatalogId objId;
20353 : : CatalogId refobjId;
20354 : : char deptype;
20355 : :
20356 : 466772 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
20357 : 466772 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
20358 : 466772 : refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
20359 : 466772 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
20360 : 466772 : deptype = *(PQgetvalue(res, i, i_deptype));
20361 : :
20362 [ + + ]: 466772 : if (dobj == NULL ||
20363 [ + + ]: 439774 : dobj->catId.tableoid != objId.tableoid ||
20364 [ + + ]: 437432 : dobj->catId.oid != objId.oid)
20365 : 203491 : dobj = findObjectByCatalogId(objId);
20366 : :
20367 : : /*
20368 : : * Failure to find objects mentioned in pg_depend is not unexpected,
20369 : : * since for example we don't collect info about TOAST tables.
20370 : : */
20371 [ + + ]: 466772 : if (dobj == NULL)
20372 : : {
20373 : : #ifdef NOT_USED
20374 : : pg_log_warning("no referencing object %u %u",
20375 : : objId.tableoid, objId.oid);
20376 : : #endif
20377 : 27758 : continue;
20378 : : }
20379 : :
20380 : 439989 : refdobj = findObjectByCatalogId(refobjId);
20381 : :
20382 [ + + ]: 439989 : if (refdobj == NULL)
20383 : : {
20384 : : #ifdef NOT_USED
20385 : : pg_log_warning("no referenced object %u %u",
20386 : : refobjId.tableoid, refobjId.oid);
20387 : : #endif
20388 : 975 : continue;
20389 : : }
20390 : :
20391 : : /*
20392 : : * For 'x' dependencies, mark the object for later; we still add the
20393 : : * normal dependency, for possible ordering purposes. Currently
20394 : : * pg_dump_sort.c knows to put extensions ahead of all object types
20395 : : * that could possibly depend on them, but this is safer.
20396 : : */
20397 [ + + ]: 439014 : if (deptype == 'x')
20398 : 44 : dobj->depends_on_ext = true;
20399 : :
20400 : : /*
20401 : : * Ordinarily, table rowtypes have implicit dependencies on their
20402 : : * tables. However, for a composite type the implicit dependency goes
20403 : : * the other way in pg_depend; which is the right thing for DROP but
20404 : : * it doesn't produce the dependency ordering we need. So in that one
20405 : : * case, we reverse the direction of the dependency.
20406 : : */
20407 [ + + ]: 439014 : if (deptype == 'i' &&
20408 [ + + ]: 124904 : dobj->objType == DO_TABLE &&
20409 [ + + ]: 1398 : refdobj->objType == DO_TYPE)
20410 : 246 : addObjectDependency(refdobj, dobj->dumpId);
20411 : : else
20412 : : /* normal case */
20413 : 438768 : addObjectDependency(dobj, refdobj->dumpId);
20414 : : }
20415 : :
20416 : 215 : PQclear(res);
20417 : :
20418 : 215 : destroyPQExpBuffer(query);
20419 : 215 : }
20420 : :
20421 : :
20422 : : /*
20423 : : * createBoundaryObjects - create dummy DumpableObjects to represent
20424 : : * dump section boundaries.
20425 : : */
20426 : : static DumpableObject *
20427 : 215 : createBoundaryObjects(void)
20428 : : {
20429 : : DumpableObject *dobjs;
20430 : :
20431 : 215 : dobjs = pg_malloc_array(DumpableObject, 2);
20432 : :
20433 : 215 : dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
20434 : 215 : dobjs[0].catId = nilCatalogId;
20435 : 215 : AssignDumpId(dobjs + 0);
20436 : 215 : dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
20437 : :
20438 : 215 : dobjs[1].objType = DO_POST_DATA_BOUNDARY;
20439 : 215 : dobjs[1].catId = nilCatalogId;
20440 : 215 : AssignDumpId(dobjs + 1);
20441 : 215 : dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
20442 : :
20443 : 215 : return dobjs;
20444 : : }
20445 : :
20446 : : /*
20447 : : * addBoundaryDependencies - add dependencies as needed to enforce the dump
20448 : : * section boundaries.
20449 : : */
20450 : : static void
20451 : 215 : addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
20452 : : DumpableObject *boundaryObjs)
20453 : : {
20454 : 215 : DumpableObject *preDataBound = boundaryObjs + 0;
20455 : 215 : DumpableObject *postDataBound = boundaryObjs + 1;
20456 : : int i;
20457 : :
20458 [ + + ]: 811453 : for (i = 0; i < numObjs; i++)
20459 : : {
20460 : 811238 : DumpableObject *dobj = dobjs[i];
20461 : :
20462 : : /*
20463 : : * The classification of object types here must match the SECTION_xxx
20464 : : * values assigned during subsequent ArchiveEntry calls!
20465 : : */
20466 [ + + + + : 811238 : switch (dobj->objType)
+ + + +
- ]
20467 : : {
20468 : 758256 : case DO_NAMESPACE:
20469 : : case DO_EXTENSION:
20470 : : case DO_TYPE:
20471 : : case DO_SHELL_TYPE:
20472 : : case DO_FUNC:
20473 : : case DO_AGG:
20474 : : case DO_OPERATOR:
20475 : : case DO_ACCESS_METHOD:
20476 : : case DO_OPCLASS:
20477 : : case DO_OPFAMILY:
20478 : : case DO_COLLATION:
20479 : : case DO_CONVERSION:
20480 : : case DO_TABLE:
20481 : : case DO_TABLE_ATTACH:
20482 : : case DO_ATTRDEF:
20483 : : case DO_PROCLANG:
20484 : : case DO_CAST:
20485 : : case DO_DUMMY_TYPE:
20486 : : case DO_TSPARSER:
20487 : : case DO_TSDICT:
20488 : : case DO_TSTEMPLATE:
20489 : : case DO_TSCONFIG:
20490 : : case DO_FDW:
20491 : : case DO_FOREIGN_SERVER:
20492 : : case DO_TRANSFORM:
20493 : : /* Pre-data objects: must come before the pre-data boundary */
20494 : 758256 : addObjectDependency(preDataBound, dobj->dumpId);
20495 : 758256 : break;
20496 : 5229 : case DO_TABLE_DATA:
20497 : : case DO_SEQUENCE_SET:
20498 : : case DO_LARGE_OBJECT:
20499 : : case DO_LARGE_OBJECT_DATA:
20500 : : /* Data objects: must come between the boundaries */
20501 : 5229 : addObjectDependency(dobj, preDataBound->dumpId);
20502 : 5229 : addObjectDependency(postDataBound, dobj->dumpId);
20503 : 5229 : break;
20504 : 6325 : case DO_INDEX:
20505 : : case DO_INDEX_ATTACH:
20506 : : case DO_STATSEXT:
20507 : : case DO_REFRESH_MATVIEW:
20508 : : case DO_TRIGGER:
20509 : : case DO_EVENT_TRIGGER:
20510 : : case DO_DEFAULT_ACL:
20511 : : case DO_POLICY:
20512 : : case DO_PUBLICATION:
20513 : : case DO_PUBLICATION_REL:
20514 : : case DO_PUBLICATION_TABLE_IN_SCHEMA:
20515 : : case DO_SUBSCRIPTION:
20516 : : case DO_SUBSCRIPTION_REL:
20517 : : /* Post-data objects: must come after the post-data boundary */
20518 : 6325 : addObjectDependency(dobj, postDataBound->dumpId);
20519 : 6325 : break;
20520 : 34524 : case DO_RULE:
20521 : : /* Rules are post-data, but only if dumped separately */
20522 [ + + ]: 34524 : if (((RuleInfo *) dobj)->separate)
20523 : 703 : addObjectDependency(dobj, postDataBound->dumpId);
20524 : 34524 : break;
20525 : 2678 : case DO_CONSTRAINT:
20526 : : case DO_FK_CONSTRAINT:
20527 : : /* Constraints are post-data, but only if dumped separately */
20528 [ + + ]: 2678 : if (((ConstraintInfo *) dobj)->separate)
20529 : 1934 : addObjectDependency(dobj, postDataBound->dumpId);
20530 : 2678 : break;
20531 : 215 : case DO_PRE_DATA_BOUNDARY:
20532 : : /* nothing to do */
20533 : 215 : break;
20534 : 215 : case DO_POST_DATA_BOUNDARY:
20535 : : /* must come after the pre-data boundary */
20536 : 215 : addObjectDependency(dobj, preDataBound->dumpId);
20537 : 215 : break;
20538 : 3796 : case DO_REL_STATS:
20539 : : /* stats section varies by parent object type, DATA or POST */
20540 [ + + ]: 3796 : if (((RelStatsInfo *) dobj)->section == SECTION_DATA)
20541 : : {
20542 : 2497 : addObjectDependency(dobj, preDataBound->dumpId);
20543 : 2497 : addObjectDependency(postDataBound, dobj->dumpId);
20544 : : }
20545 : : else
20546 : 1299 : addObjectDependency(dobj, postDataBound->dumpId);
20547 : 3796 : break;
20548 : : }
20549 : : }
20550 : 215 : }
20551 : :
20552 : :
20553 : : /*
20554 : : * BuildArchiveDependencies - create dependency data for archive TOC entries
20555 : : *
20556 : : * The raw dependency data obtained by getDependencies() is not terribly
20557 : : * useful in an archive dump, because in many cases there are dependency
20558 : : * chains linking through objects that don't appear explicitly in the dump.
20559 : : * For example, a view will depend on its _RETURN rule while the _RETURN rule
20560 : : * will depend on other objects --- but the rule will not appear as a separate
20561 : : * object in the dump. We need to adjust the view's dependencies to include
20562 : : * whatever the rule depends on that is included in the dump.
20563 : : *
20564 : : * Just to make things more complicated, there are also "special" dependencies
20565 : : * such as the dependency of a TABLE DATA item on its TABLE, which we must
20566 : : * not rearrange because pg_restore knows that TABLE DATA only depends on
20567 : : * its table. In these cases we must leave the dependencies strictly as-is
20568 : : * even if they refer to not-to-be-dumped objects.
20569 : : *
20570 : : * To handle this, the convention is that "special" dependencies are created
20571 : : * during ArchiveEntry calls, and an archive TOC item that has any such
20572 : : * entries will not be touched here. Otherwise, we recursively search the
20573 : : * DumpableObject data structures to build the correct dependencies for each
20574 : : * archive TOC item.
20575 : : */
20576 : : static void
20577 : 73 : BuildArchiveDependencies(Archive *fout)
20578 : : {
20579 : 73 : ArchiveHandle *AH = (ArchiveHandle *) fout;
20580 : : TocEntry *te;
20581 : :
20582 : : /* Scan all TOC entries in the archive */
20583 [ + + ]: 8059 : for (te = AH->toc->next; te != AH->toc; te = te->next)
20584 : : {
20585 : : DumpableObject *dobj;
20586 : : DumpId *dependencies;
20587 : : int nDeps;
20588 : : int allocDeps;
20589 : :
20590 : : /* No need to process entries that will not be dumped */
20591 [ + + ]: 7986 : if (te->reqs == 0)
20592 : 3995 : continue;
20593 : : /* Ignore entries that already have "special" dependencies */
20594 [ + + ]: 7978 : if (te->nDeps > 0)
20595 : 3440 : continue;
20596 : : /* Otherwise, look up the item's original DumpableObject, if any */
20597 : 4538 : dobj = findObjectByDumpId(te->dumpId);
20598 [ + + ]: 4538 : if (dobj == NULL)
20599 : 430 : continue;
20600 : : /* No work if it has no dependencies */
20601 [ + + ]: 4108 : if (dobj->nDeps <= 0)
20602 : 117 : continue;
20603 : : /* Set up work array */
20604 : 3991 : allocDeps = 64;
20605 : 3991 : dependencies = pg_malloc_array(DumpId, allocDeps);
20606 : 3991 : nDeps = 0;
20607 : : /* Recursively find all dumpable dependencies */
20608 : 3991 : findDumpableDependencies(AH, dobj,
20609 : : &dependencies, &nDeps, &allocDeps);
20610 : : /* And save 'em ... */
20611 [ + + ]: 3991 : if (nDeps > 0)
20612 : : {
20613 : 3034 : dependencies = pg_realloc_array(dependencies, DumpId, nDeps);
20614 : 3034 : te->dependencies = dependencies;
20615 : 3034 : te->nDeps = nDeps;
20616 : : }
20617 : : else
20618 : 957 : pg_free(dependencies);
20619 : : }
20620 : 73 : }
20621 : :
20622 : : /* Recursive search subroutine for BuildArchiveDependencies */
20623 : : static void
20624 : 9398 : findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
20625 : : DumpId **dependencies, int *nDeps, int *allocDeps)
20626 : : {
20627 : : int i;
20628 : :
20629 : : /*
20630 : : * Ignore section boundary objects: if we search through them, we'll
20631 : : * report lots of bogus dependencies.
20632 : : */
20633 [ + + ]: 9398 : if (dobj->objType == DO_PRE_DATA_BOUNDARY ||
20634 [ + + ]: 9377 : dobj->objType == DO_POST_DATA_BOUNDARY)
20635 : 1662 : return;
20636 : :
20637 [ + + ]: 19461 : for (i = 0; i < dobj->nDeps; i++)
20638 : : {
20639 : 11725 : DumpId depid = dobj->dependencies[i];
20640 : :
20641 [ + + ]: 11725 : if (TocIDRequired(AH, depid) != 0)
20642 : : {
20643 : : /* Object will be dumped, so just reference it as a dependency */
20644 [ - + ]: 6318 : if (*nDeps >= *allocDeps)
20645 : : {
20646 : 0 : *allocDeps *= 2;
20647 : 0 : *dependencies = pg_realloc_array(*dependencies, DumpId, *allocDeps);
20648 : : }
20649 : 6318 : (*dependencies)[*nDeps] = depid;
20650 : 6318 : (*nDeps)++;
20651 : : }
20652 : : else
20653 : : {
20654 : : /*
20655 : : * Object will not be dumped, so recursively consider its deps. We
20656 : : * rely on the assumption that sortDumpableObjects already broke
20657 : : * any dependency loops, else we might recurse infinitely.
20658 : : */
20659 : 5407 : DumpableObject *otherdobj = findObjectByDumpId(depid);
20660 : :
20661 [ + - ]: 5407 : if (otherdobj)
20662 : 5407 : findDumpableDependencies(AH, otherdobj,
20663 : : dependencies, nDeps, allocDeps);
20664 : : }
20665 : : }
20666 : : }
20667 : :
20668 : :
20669 : : /*
20670 : : * getFormattedTypeName - retrieve a nicely-formatted type name for the
20671 : : * given type OID.
20672 : : *
20673 : : * This does not guarantee to schema-qualify the output, so it should not
20674 : : * be used to create the target object name for CREATE or ALTER commands.
20675 : : *
20676 : : * Note that the result is cached and must not be freed by the caller.
20677 : : */
20678 : : static const char *
20679 : 2380 : getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
20680 : : {
20681 : : TypeInfo *typeInfo;
20682 : : char *result;
20683 : : PQExpBuffer query;
20684 : : PGresult *res;
20685 : :
20686 [ - + ]: 2380 : if (oid == 0)
20687 : : {
20688 [ # # ]: 0 : if ((opts & zeroAsStar) != 0)
20689 : 0 : return "*";
20690 [ # # ]: 0 : else if ((opts & zeroAsNone) != 0)
20691 : 0 : return "NONE";
20692 : : }
20693 : :
20694 : : /* see if we have the result cached in the type's TypeInfo record */
20695 : 2380 : typeInfo = findTypeByOid(oid);
20696 [ + - + + ]: 2380 : if (typeInfo && typeInfo->ftypname)
20697 : 1898 : return typeInfo->ftypname;
20698 : :
20699 : 482 : query = createPQExpBuffer();
20700 : 482 : appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
20701 : : oid);
20702 : :
20703 : 482 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
20704 : :
20705 : : /* result of format_type is already quoted */
20706 : 482 : result = pg_strdup(PQgetvalue(res, 0, 0));
20707 : :
20708 : 482 : PQclear(res);
20709 : 482 : destroyPQExpBuffer(query);
20710 : :
20711 : : /*
20712 : : * Cache the result for re-use in later requests, if possible. If we
20713 : : * don't have a TypeInfo for the type, the string will be leaked once the
20714 : : * caller is done with it ... but that case really should not happen, so
20715 : : * leaking if it does seems acceptable.
20716 : : */
20717 [ + - ]: 482 : if (typeInfo)
20718 : 482 : typeInfo->ftypname = result;
20719 : :
20720 : 482 : return result;
20721 : : }
20722 : :
20723 : : /*
20724 : : * Return a column list clause for the given relation.
20725 : : *
20726 : : * Special case: if there are no undropped columns in the relation, return
20727 : : * "", not an invalid "()" column list.
20728 : : */
20729 : : static const char *
20730 : 8974 : fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer)
20731 : : {
20732 : 8974 : int numatts = ti->numatts;
20733 : 8974 : char **attnames = ti->attnames;
20734 : 8974 : bool *attisdropped = ti->attisdropped;
20735 : 8974 : char *attgenerated = ti->attgenerated;
20736 : : bool needComma;
20737 : : int i;
20738 : :
20739 : 8974 : appendPQExpBufferChar(buffer, '(');
20740 : 8974 : needComma = false;
20741 [ + + ]: 42688 : for (i = 0; i < numatts; i++)
20742 : : {
20743 [ + + ]: 33714 : if (attisdropped[i])
20744 : 784 : continue;
20745 [ + + ]: 32930 : if (attgenerated[i])
20746 : 1252 : continue;
20747 [ + + ]: 31678 : if (needComma)
20748 : 22974 : appendPQExpBufferStr(buffer, ", ");
20749 : 31678 : appendPQExpBufferStr(buffer, fmtId(attnames[i]));
20750 : 31678 : needComma = true;
20751 : : }
20752 : :
20753 [ + + ]: 8974 : if (!needComma)
20754 : 270 : return ""; /* no undropped columns */
20755 : :
20756 : 8704 : appendPQExpBufferChar(buffer, ')');
20757 : 8704 : return buffer->data;
20758 : : }
20759 : :
20760 : : /*
20761 : : * Check if a reloptions array is nonempty.
20762 : : */
20763 : : static bool
20764 : 14653 : nonemptyReloptions(const char *reloptions)
20765 : : {
20766 : : /* Don't want to print it if it's just "{}" */
20767 [ + - + + ]: 14653 : return (reloptions != NULL && strlen(reloptions) > 2);
20768 : : }
20769 : :
20770 : : /*
20771 : : * Format a reloptions array and append it to the given buffer.
20772 : : *
20773 : : * "prefix" is prepended to the option names; typically it's "" or "toast.".
20774 : : */
20775 : : static void
20776 : 223 : appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
20777 : : const char *prefix, Archive *fout)
20778 : : {
20779 : : bool res;
20780 : :
20781 : 223 : res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
20782 : 223 : fout->std_strings);
20783 [ - + ]: 223 : if (!res)
20784 : 0 : pg_log_warning("could not parse %s array", "reloptions");
20785 : 223 : }
20786 : :
20787 : : /*
20788 : : * read_dump_filters - retrieve object identifier patterns from file
20789 : : *
20790 : : * Parse the specified filter file for include and exclude patterns, and add
20791 : : * them to the relevant lists. If the filename is "-" then filters will be
20792 : : * read from STDIN rather than a file.
20793 : : */
20794 : : static void
20795 : 26 : read_dump_filters(const char *filename, DumpOptions *dopt)
20796 : : {
20797 : : FilterStateData fstate;
20798 : : char *objname;
20799 : : FilterCommandType comtype;
20800 : : FilterObjectType objtype;
20801 : :
20802 : 26 : filter_init(&fstate, filename, exit_nicely);
20803 : :
20804 [ + + ]: 84 : while (filter_read_item(&fstate, &objname, &comtype, &objtype))
20805 : : {
20806 [ + + ]: 33 : if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
20807 : : {
20808 [ - - + + : 17 : switch (objtype)
+ + + - ]
20809 : : {
20810 : 0 : case FILTER_OBJECT_TYPE_NONE:
20811 : 0 : break;
20812 : 0 : case FILTER_OBJECT_TYPE_DATABASE:
20813 : : case FILTER_OBJECT_TYPE_FUNCTION:
20814 : : case FILTER_OBJECT_TYPE_INDEX:
20815 : : case FILTER_OBJECT_TYPE_TABLE_DATA:
20816 : : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20817 : : case FILTER_OBJECT_TYPE_TRIGGER:
20818 : 0 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20819 : : "include",
20820 : : filter_object_type_name(objtype));
20821 : 0 : exit_nicely(1);
20822 : : break; /* unreachable */
20823 : :
20824 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20825 : 1 : simple_string_list_append(&extension_include_patterns, objname);
20826 : 1 : break;
20827 : 1 : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20828 : 1 : simple_string_list_append(&foreign_servers_include_patterns, objname);
20829 : 1 : break;
20830 : 1 : case FILTER_OBJECT_TYPE_SCHEMA:
20831 : 1 : simple_string_list_append(&schema_include_patterns, objname);
20832 : 1 : dopt->include_everything = false;
20833 : 1 : break;
20834 : 13 : case FILTER_OBJECT_TYPE_TABLE:
20835 : 13 : simple_string_list_append(&table_include_patterns, objname);
20836 : 13 : dopt->include_everything = false;
20837 : 13 : break;
20838 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20839 : 1 : simple_string_list_append(&table_include_patterns_and_children,
20840 : : objname);
20841 : 1 : dopt->include_everything = false;
20842 : 1 : break;
20843 : : }
20844 : : }
20845 [ + + ]: 16 : else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
20846 : : {
20847 [ - + + + : 9 : switch (objtype)
+ + + +
- ]
20848 : : {
20849 : 0 : case FILTER_OBJECT_TYPE_NONE:
20850 : 0 : break;
20851 : 1 : case FILTER_OBJECT_TYPE_DATABASE:
20852 : : case FILTER_OBJECT_TYPE_FUNCTION:
20853 : : case FILTER_OBJECT_TYPE_INDEX:
20854 : : case FILTER_OBJECT_TYPE_TRIGGER:
20855 : : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20856 : 1 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20857 : : "exclude",
20858 : : filter_object_type_name(objtype));
20859 : 1 : exit_nicely(1);
20860 : : break;
20861 : :
20862 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20863 : 1 : simple_string_list_append(&extension_exclude_patterns, objname);
20864 : 1 : break;
20865 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA:
20866 : 1 : simple_string_list_append(&tabledata_exclude_patterns,
20867 : : objname);
20868 : 1 : break;
20869 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20870 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
20871 : : objname);
20872 : 1 : break;
20873 : 2 : case FILTER_OBJECT_TYPE_SCHEMA:
20874 : 2 : simple_string_list_append(&schema_exclude_patterns, objname);
20875 : 2 : break;
20876 : 2 : case FILTER_OBJECT_TYPE_TABLE:
20877 : 2 : simple_string_list_append(&table_exclude_patterns, objname);
20878 : 2 : break;
20879 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20880 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
20881 : : objname);
20882 : 1 : break;
20883 : : }
20884 : : }
20885 : : else
20886 : : {
20887 : : Assert(comtype == FILTER_COMMAND_TYPE_NONE);
20888 : : Assert(objtype == FILTER_OBJECT_TYPE_NONE);
20889 : : }
20890 : :
20891 [ + + ]: 32 : if (objname)
20892 : 25 : free(objname);
20893 : : }
20894 : :
20895 : 22 : filter_free(&fstate);
20896 : 22 : }
|