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 "catalog/pg_aggregate_d.h"
45 : : #include "catalog/pg_am_d.h"
46 : : #include "catalog/pg_attribute_d.h"
47 : : #include "catalog/pg_authid_d.h"
48 : : #include "catalog/pg_cast_d.h"
49 : : #include "catalog/pg_class_d.h"
50 : : #include "catalog/pg_constraint_d.h"
51 : : #include "catalog/pg_default_acl_d.h"
52 : : #include "catalog/pg_largeobject_d.h"
53 : : #include "catalog/pg_largeobject_metadata_d.h"
54 : : #include "catalog/pg_proc_d.h"
55 : : #include "catalog/pg_publication_d.h"
56 : : #include "catalog/pg_shdepend_d.h"
57 : : #include "catalog/pg_subscription_d.h"
58 : : #include "catalog/pg_type_d.h"
59 : : #include "common/connect.h"
60 : : #include "common/int.h"
61 : : #include "common/relpath.h"
62 : : #include "common/shortest_dec.h"
63 : : #include "compress_io.h"
64 : : #include "dumputils.h"
65 : : #include "fe_utils/option_utils.h"
66 : : #include "fe_utils/string_utils.h"
67 : : #include "filter.h"
68 : : #include "getopt_long.h"
69 : : #include "libpq/libpq-fs.h"
70 : : #include "parallel.h"
71 : : #include "pg_backup_db.h"
72 : : #include "pg_backup_utils.h"
73 : : #include "pg_dump.h"
74 : : #include "statistics/statistics_format.h"
75 : : #include "storage/block.h"
76 : :
77 : : typedef struct
78 : : {
79 : : Oid roleoid; /* role's OID */
80 : : const char *rolename; /* role's name */
81 : : } RoleNameItem;
82 : :
83 : : typedef struct
84 : : {
85 : : const char *descr; /* comment for an object */
86 : : Oid classoid; /* object class (catalog OID) */
87 : : Oid objoid; /* object OID */
88 : : int objsubid; /* subobject (table column #) */
89 : : } CommentItem;
90 : :
91 : : typedef struct
92 : : {
93 : : const char *provider; /* label provider of this security label */
94 : : const char *label; /* security label for an object */
95 : : Oid classoid; /* object class (catalog OID) */
96 : : Oid objoid; /* object OID */
97 : : int objsubid; /* subobject (table column #) */
98 : : } SecLabelItem;
99 : :
100 : : typedef struct
101 : : {
102 : : Oid oid; /* object OID */
103 : : char relkind; /* object kind */
104 : : RelFileNumber relfilenumber; /* object filenode */
105 : : Oid toast_oid; /* toast table OID */
106 : : RelFileNumber toast_relfilenumber; /* toast table filenode */
107 : : Oid toast_chunk_id_typoid; /* type of chunk_id attribute */
108 : : Oid toast_index_oid; /* toast table index OID */
109 : : RelFileNumber toast_index_relfilenumber; /* toast table index filenode */
110 : : } BinaryUpgradeClassOidItem;
111 : :
112 : : /* sequence types */
113 : : typedef enum SeqType
114 : : {
115 : : SEQTYPE_SMALLINT,
116 : : SEQTYPE_INTEGER,
117 : : SEQTYPE_BIGINT,
118 : : } SeqType;
119 : :
120 : : static const char *const SeqTypeNames[] =
121 : : {
122 : : [SEQTYPE_SMALLINT] = "smallint",
123 : : [SEQTYPE_INTEGER] = "integer",
124 : : [SEQTYPE_BIGINT] = "bigint",
125 : : };
126 : :
127 : : StaticAssertDecl(lengthof(SeqTypeNames) == (SEQTYPE_BIGINT + 1),
128 : : "array length mismatch");
129 : :
130 : : typedef struct
131 : : {
132 : : Oid oid; /* sequence OID */
133 : : SeqType seqtype; /* data type of sequence */
134 : : bool cycled; /* whether sequence cycles */
135 : : int64 minv; /* minimum value */
136 : : int64 maxv; /* maximum value */
137 : : int64 startv; /* start value */
138 : : int64 incby; /* increment value */
139 : : int64 cache; /* cache size */
140 : : int64 last_value; /* last value of sequence */
141 : : bool is_called; /* whether nextval advances before returning */
142 : : bool null_seqtuple; /* did pg_get_sequence_data return nulls? */
143 : : } SequenceItem;
144 : :
145 : : typedef enum OidOptions
146 : : {
147 : : zeroIsError = 1,
148 : : zeroAsStar = 2,
149 : : zeroAsNone = 4,
150 : : } OidOptions;
151 : :
152 : : /* global decls */
153 : : static bool dosync = true; /* Issue fsync() to make dump durable on disk. */
154 : :
155 : : static Oid g_last_builtin_oid; /* value of the last builtin oid */
156 : :
157 : : /* The specified names/patterns should to match at least one entity */
158 : : static int strict_names = 0;
159 : :
160 : : static pg_compress_algorithm compression_algorithm = PG_COMPRESSION_NONE;
161 : :
162 : : /*
163 : : * Object inclusion/exclusion lists
164 : : *
165 : : * The string lists record the patterns given by command-line switches,
166 : : * which we then convert to lists of OIDs of matching objects.
167 : : */
168 : : static SimpleStringList schema_include_patterns = {NULL, NULL};
169 : : static SimpleOidList schema_include_oids = {NULL, NULL};
170 : : static SimpleStringList schema_exclude_patterns = {NULL, NULL};
171 : : static SimpleOidList schema_exclude_oids = {NULL, NULL};
172 : :
173 : : static SimpleStringList table_include_patterns = {NULL, NULL};
174 : : static SimpleStringList table_include_patterns_and_children = {NULL, NULL};
175 : : static SimpleOidList table_include_oids = {NULL, NULL};
176 : : static SimpleStringList table_exclude_patterns = {NULL, NULL};
177 : : static SimpleStringList table_exclude_patterns_and_children = {NULL, NULL};
178 : : static SimpleOidList table_exclude_oids = {NULL, NULL};
179 : : static SimpleStringList tabledata_exclude_patterns = {NULL, NULL};
180 : : static SimpleStringList tabledata_exclude_patterns_and_children = {NULL, NULL};
181 : : static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
182 : :
183 : : static SimpleStringList foreign_servers_include_patterns = {NULL, NULL};
184 : : static SimpleOidList foreign_servers_include_oids = {NULL, NULL};
185 : :
186 : : static SimpleStringList extension_include_patterns = {NULL, NULL};
187 : : static SimpleOidList extension_include_oids = {NULL, NULL};
188 : :
189 : : static SimpleStringList extension_exclude_patterns = {NULL, NULL};
190 : : static SimpleOidList extension_exclude_oids = {NULL, NULL};
191 : :
192 : : static const CatalogId nilCatalogId = {0, 0};
193 : :
194 : : /* override for standard extra_float_digits setting */
195 : : static bool have_extra_float_digits = false;
196 : : static int extra_float_digits;
197 : :
198 : : /* sorted table of role names */
199 : : static RoleNameItem *rolenames = NULL;
200 : : static int nrolenames = 0;
201 : :
202 : : /* sorted table of comments */
203 : : static CommentItem *comments = NULL;
204 : : static int ncomments = 0;
205 : :
206 : : /* sorted table of security labels */
207 : : static SecLabelItem *seclabels = NULL;
208 : : static int nseclabels = 0;
209 : :
210 : : /* sorted table of pg_class information for binary upgrade */
211 : : static BinaryUpgradeClassOidItem *binaryUpgradeClassOids = NULL;
212 : : static int nbinaryUpgradeClassOids = 0;
213 : :
214 : : /* sorted table of sequences */
215 : : static SequenceItem *sequences = NULL;
216 : : static int nsequences = 0;
217 : :
218 : : /* Maximum number of relations to fetch in a fetchAttributeStats() call. */
219 : : #define MAX_ATTR_STATS_RELS 64
220 : :
221 : : /*
222 : : * The default number of rows per INSERT when
223 : : * --inserts is specified without --rows-per-insert
224 : : */
225 : : #define DUMP_DEFAULT_ROWS_PER_INSERT 1
226 : :
227 : : /*
228 : : * Maximum number of large objects to group into a single ArchiveEntry.
229 : : * At some point we might want to make this user-controllable, but for now
230 : : * a hard-wired setting will suffice.
231 : : */
232 : : #define MAX_BLOBS_PER_ARCHIVE_ENTRY 1000
233 : :
234 : : /*
235 : : * Macro for producing quoted, schema-qualified name of a dumpable object.
236 : : */
237 : : #define fmtQualifiedDumpable(obj) \
238 : : fmtQualifiedId((obj)->dobj.namespace->dobj.name, \
239 : : (obj)->dobj.name)
240 : :
241 : : static void help(const char *progname);
242 : : static void setup_connection(Archive *AH,
243 : : const char *dumpencoding, const char *dumpsnapshot,
244 : : char *use_role);
245 : : static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
246 : : static void expand_schema_name_patterns(Archive *fout,
247 : : SimpleStringList *patterns,
248 : : SimpleOidList *oids,
249 : : bool strict_names);
250 : : static void expand_extension_name_patterns(Archive *fout,
251 : : SimpleStringList *patterns,
252 : : SimpleOidList *oids,
253 : : bool strict_names);
254 : : static void expand_foreign_server_name_patterns(Archive *fout,
255 : : SimpleStringList *patterns,
256 : : SimpleOidList *oids);
257 : : static void expand_table_name_patterns(Archive *fout,
258 : : SimpleStringList *patterns,
259 : : SimpleOidList *oids,
260 : : bool strict_names,
261 : : bool with_child_tables);
262 : : static void prohibit_crossdb_refs(PGconn *conn, const char *dbname,
263 : : const char *pattern);
264 : :
265 : : static NamespaceInfo *findNamespace(Oid nsoid);
266 : : static void dumpTableData(Archive *fout, const TableDataInfo *tdinfo);
267 : : static void refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo);
268 : : static const char *getRoleName(const char *roleoid_str);
269 : : static void collectRoleNames(Archive *fout);
270 : : static void getAdditionalACLs(Archive *fout);
271 : : static void dumpCommentExtended(Archive *fout, const char *type,
272 : : const char *name, const char *namespace,
273 : : const char *owner, CatalogId catalogId,
274 : : int subid, DumpId dumpId,
275 : : const char *initdb_comment);
276 : : static inline void dumpComment(Archive *fout, const char *type,
277 : : const char *name, const char *namespace,
278 : : const char *owner, CatalogId catalogId,
279 : : int subid, DumpId dumpId);
280 : : static int findComments(Oid classoid, Oid objoid, CommentItem **items);
281 : : static void collectComments(Archive *fout);
282 : : static void dumpSecLabel(Archive *fout, const char *type, const char *name,
283 : : const char *namespace, const char *owner,
284 : : CatalogId catalogId, int subid, DumpId dumpId);
285 : : static int findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items);
286 : : static void collectSecLabels(Archive *fout);
287 : : static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
288 : : static void dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo);
289 : : static void dumpExtension(Archive *fout, const ExtensionInfo *extinfo);
290 : : static void dumpType(Archive *fout, const TypeInfo *tyinfo);
291 : : static void dumpBaseType(Archive *fout, const TypeInfo *tyinfo);
292 : : static void dumpEnumType(Archive *fout, const TypeInfo *tyinfo);
293 : : static void dumpRangeType(Archive *fout, const TypeInfo *tyinfo);
294 : : static void dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo);
295 : : static void dumpDomain(Archive *fout, const TypeInfo *tyinfo);
296 : : static void dumpCompositeType(Archive *fout, const TypeInfo *tyinfo);
297 : : static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
298 : : PGresult *res);
299 : : static void dumpShellType(Archive *fout, const ShellTypeInfo *stinfo);
300 : : static void dumpProcLang(Archive *fout, const ProcLangInfo *plang);
301 : : static void dumpFunc(Archive *fout, const FuncInfo *finfo);
302 : : static void dumpCast(Archive *fout, const CastInfo *cast);
303 : : static void dumpTransform(Archive *fout, const TransformInfo *transform);
304 : : static void dumpOpr(Archive *fout, const OprInfo *oprinfo);
305 : : static void dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo);
306 : : static void dumpOpclass(Archive *fout, const OpclassInfo *opcinfo);
307 : : static void dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo);
308 : : static void dumpCollation(Archive *fout, const CollInfo *collinfo);
309 : : static void dumpConversion(Archive *fout, const ConvInfo *convinfo);
310 : : static void dumpRule(Archive *fout, const RuleInfo *rinfo);
311 : : static void dumpAgg(Archive *fout, const AggInfo *agginfo);
312 : : static void dumpTrigger(Archive *fout, const TriggerInfo *tginfo);
313 : : static void dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo);
314 : : static void dumpTable(Archive *fout, const TableInfo *tbinfo);
315 : : static void dumpTableSchema(Archive *fout, const TableInfo *tbinfo);
316 : : static void dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo);
317 : : static void dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo);
318 : : static void collectSequences(Archive *fout);
319 : : static void dumpSequence(Archive *fout, const TableInfo *tbinfo);
320 : : static void dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo);
321 : : static void dumpIndex(Archive *fout, const IndxInfo *indxinfo);
322 : : static void dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo);
323 : : static void dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo);
324 : : static void dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo);
325 : : static void dumpConstraint(Archive *fout, const ConstraintInfo *coninfo);
326 : : static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo);
327 : : static void dumpTSParser(Archive *fout, const TSParserInfo *prsinfo);
328 : : static void dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo);
329 : : static void dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo);
330 : : static void dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo);
331 : : static void dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo);
332 : : static void dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo);
333 : : static void dumpUserMappings(Archive *fout,
334 : : const char *servername, const char *namespace,
335 : : const char *owner, CatalogId catalogId, DumpId dumpId);
336 : : static void dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo);
337 : :
338 : : static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
339 : : const char *type, const char *name, const char *subname,
340 : : const char *nspname, const char *tag, const char *owner,
341 : : const DumpableAcl *dacl);
342 : :
343 : : static void getDependencies(Archive *fout);
344 : : static void BuildArchiveDependencies(Archive *fout);
345 : : static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
346 : : DumpId **dependencies, int *nDeps, int *allocDeps);
347 : :
348 : : static DumpableObject *createBoundaryObjects(void);
349 : : static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
350 : : DumpableObject *boundaryObjs);
351 : :
352 : : static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx);
353 : : static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
354 : : static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
355 : : static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
356 : : static void buildMatViewRefreshDependencies(Archive *fout);
357 : : static void getTableDataFKConstraints(void);
358 : : static void determineNotNullFlags(Archive *fout, PGresult *res, int r,
359 : : TableInfo *tbinfo, int j,
360 : : int i_notnull_name,
361 : : int i_notnull_comment,
362 : : int i_notnull_invalidoid,
363 : : int i_notnull_noinherit,
364 : : int i_notnull_islocal,
365 : : PQExpBuffer *invalidnotnulloids);
366 : : static char *format_function_arguments(const FuncInfo *finfo, const char *funcargs,
367 : : bool is_agg);
368 : : static char *format_function_signature(Archive *fout,
369 : : const FuncInfo *finfo, bool honor_quotes);
370 : : static char *convertRegProcReference(const char *proc);
371 : : static char *getFormattedOperatorName(const char *oproid);
372 : : static char *convertTSFunction(Archive *fout, Oid funcOid);
373 : : static const char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
374 : : static void getLOs(Archive *fout);
375 : : static void dumpLO(Archive *fout, const LoInfo *loinfo);
376 : : static int dumpLOs(Archive *fout, const void *arg);
377 : : static void dumpPolicy(Archive *fout, const PolicyInfo *polinfo);
378 : : static void dumpPublication(Archive *fout, const PublicationInfo *pubinfo);
379 : : static void dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo);
380 : : static void dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo);
381 : : static void dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo);
382 : : static void dumpDatabase(Archive *fout);
383 : : static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
384 : : const char *dbname, Oid dboid);
385 : : static void dumpEncoding(Archive *AH);
386 : : static void dumpStdStrings(Archive *AH);
387 : : static void dumpSearchPath(Archive *AH);
388 : : static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
389 : : PQExpBuffer upgrade_buffer,
390 : : Oid pg_type_oid,
391 : : bool force_array_type,
392 : : bool include_multirange_type);
393 : : static void binary_upgrade_set_type_oids_by_rel(Archive *fout,
394 : : PQExpBuffer upgrade_buffer,
395 : : const TableInfo *tbinfo);
396 : : static void collectBinaryUpgradeClassOids(Archive *fout);
397 : : static void binary_upgrade_set_pg_class_oids(Archive *fout,
398 : : PQExpBuffer upgrade_buffer,
399 : : Oid pg_class_oid);
400 : : static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
401 : : const DumpableObject *dobj,
402 : : const char *objtype,
403 : : const char *objname,
404 : : const char *objnamespace);
405 : : static const char *getAttrName(int attrnum, const TableInfo *tblInfo);
406 : : static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
407 : : static bool nonemptyReloptions(const char *reloptions);
408 : : static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
409 : : const char *prefix, Archive *fout);
410 : : static char *get_synchronized_snapshot(Archive *fout);
411 : : static void set_restrict_relation_kind(Archive *AH, const char *value);
412 : : static void setupDumpWorker(Archive *AH);
413 : : static TableInfo *getRootTableInfo(const TableInfo *tbinfo);
414 : : static bool forcePartitionRootLoad(const TableInfo *tbinfo);
415 : : static void read_dump_filters(const char *filename, DumpOptions *dopt);
416 : :
417 : :
418 : : int
419 : 308 : main(int argc, char **argv)
420 : : {
421 : : int c;
422 : 308 : const char *filename = NULL;
423 : 308 : const char *format = "p";
424 : : TableInfo *tblinfo;
425 : : int numTables;
426 : : DumpableObject **dobjs;
427 : : int numObjs;
428 : : DumpableObject *boundaryObjs;
429 : : int i;
430 : : int optindex;
431 : : RestoreOptions *ropt;
432 : : Archive *fout; /* the script file */
433 : 308 : bool g_verbose = false;
434 : 308 : const char *dumpencoding = NULL;
435 : 308 : const char *dumpsnapshot = NULL;
436 : 308 : char *use_role = NULL;
437 : 308 : int numWorkers = 1;
438 : 308 : int plainText = 0;
439 : 308 : ArchiveFormat archiveFormat = archUnknown;
440 : : ArchiveMode archiveMode;
441 : 308 : pg_compress_specification compression_spec = {0};
442 : 308 : char *compression_detail = NULL;
443 : 308 : char *compression_algorithm_str = "none";
444 : 308 : char *error_detail = NULL;
445 : 308 : bool user_compression_defined = false;
446 : 308 : DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
447 : 308 : bool data_only = false;
448 : 308 : bool schema_only = false;
449 : 308 : bool statistics_only = false;
450 : 308 : bool with_statistics = false;
451 : 308 : bool no_data = false;
452 : 308 : bool no_schema = false;
453 : 308 : bool no_statistics = false;
454 : :
455 : : static DumpOptions dopt;
456 : :
457 : : static struct option long_options[] = {
458 : : {"data-only", no_argument, NULL, 'a'},
459 : : {"blobs", no_argument, NULL, 'b'},
460 : : {"large-objects", no_argument, NULL, 'b'},
461 : : {"no-blobs", no_argument, NULL, 'B'},
462 : : {"no-large-objects", no_argument, NULL, 'B'},
463 : : {"clean", no_argument, NULL, 'c'},
464 : : {"create", no_argument, NULL, 'C'},
465 : : {"dbname", required_argument, NULL, 'd'},
466 : : {"extension", required_argument, NULL, 'e'},
467 : : {"file", required_argument, NULL, 'f'},
468 : : {"format", required_argument, NULL, 'F'},
469 : : {"host", required_argument, NULL, 'h'},
470 : : {"jobs", 1, NULL, 'j'},
471 : : {"no-reconnect", no_argument, NULL, 'R'},
472 : : {"no-owner", no_argument, NULL, 'O'},
473 : : {"port", required_argument, NULL, 'p'},
474 : : {"schema", required_argument, NULL, 'n'},
475 : : {"exclude-schema", required_argument, NULL, 'N'},
476 : : {"schema-only", no_argument, NULL, 's'},
477 : : {"superuser", required_argument, NULL, 'S'},
478 : : {"table", required_argument, NULL, 't'},
479 : : {"exclude-table", required_argument, NULL, 'T'},
480 : : {"no-password", no_argument, NULL, 'w'},
481 : : {"password", no_argument, NULL, 'W'},
482 : : {"username", required_argument, NULL, 'U'},
483 : : {"verbose", no_argument, NULL, 'v'},
484 : : {"no-privileges", no_argument, NULL, 'x'},
485 : : {"no-acl", no_argument, NULL, 'x'},
486 : : {"compress", required_argument, NULL, 'Z'},
487 : : {"encoding", required_argument, NULL, 'E'},
488 : : {"help", no_argument, NULL, '?'},
489 : : {"version", no_argument, NULL, 'V'},
490 : :
491 : : /*
492 : : * the following options don't have an equivalent short option letter
493 : : */
494 : : {"attribute-inserts", no_argument, &dopt.column_inserts, 1},
495 : : {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1},
496 : : {"column-inserts", no_argument, &dopt.column_inserts, 1},
497 : : {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1},
498 : : {"disable-triggers", no_argument, &dopt.disable_triggers, 1},
499 : : {"enable-row-security", no_argument, &dopt.enable_row_security, 1},
500 : : {"exclude-table-data", required_argument, NULL, 4},
501 : : {"extra-float-digits", required_argument, NULL, 8},
502 : : {"if-exists", no_argument, &dopt.if_exists, 1},
503 : : {"inserts", no_argument, NULL, 9},
504 : : {"lock-wait-timeout", required_argument, NULL, 2},
505 : : {"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1},
506 : : {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
507 : : {"quote-all-identifiers", no_argument, "e_all_identifiers, 1},
508 : : {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
509 : : {"role", required_argument, NULL, 3},
510 : : {"section", required_argument, NULL, 5},
511 : : {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
512 : : {"snapshot", required_argument, NULL, 6},
513 : : {"statistics", no_argument, NULL, 22},
514 : : {"statistics-only", no_argument, NULL, 18},
515 : : {"strict-names", no_argument, &strict_names, 1},
516 : : {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1},
517 : : {"no-comments", no_argument, &dopt.no_comments, 1},
518 : : {"no-data", no_argument, NULL, 19},
519 : : {"no-policies", no_argument, &dopt.no_policies, 1},
520 : : {"no-publications", no_argument, &dopt.no_publications, 1},
521 : : {"no-schema", no_argument, NULL, 20},
522 : : {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
523 : : {"no-statistics", no_argument, NULL, 21},
524 : : {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
525 : : {"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
526 : : {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
527 : : {"no-sync", no_argument, NULL, 7},
528 : : {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
529 : : {"rows-per-insert", required_argument, NULL, 10},
530 : : {"include-foreign-data", required_argument, NULL, 11},
531 : : {"table-and-children", required_argument, NULL, 12},
532 : : {"exclude-table-and-children", required_argument, NULL, 13},
533 : : {"exclude-table-data-and-children", required_argument, NULL, 14},
534 : : {"sync-method", required_argument, NULL, 15},
535 : : {"filter", required_argument, NULL, 16},
536 : : {"exclude-extension", required_argument, NULL, 17},
537 : : {"sequence-data", no_argument, &dopt.sequence_data, 1},
538 : : {"restrict-key", required_argument, NULL, 25},
539 : :
540 : : {NULL, 0, NULL, 0}
541 : : };
542 : :
543 : 308 : pg_logging_init(argv[0]);
544 : 308 : pg_logging_set_level(PG_LOG_WARNING);
545 : 308 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
546 : :
547 : : /*
548 : : * Initialize what we need for parallel execution, especially for thread
549 : : * support on Windows.
550 : : */
551 : 308 : init_parallel_dump_utils();
552 : :
553 : 308 : progname = get_progname(argv[0]);
554 : :
555 [ + - ]: 308 : if (argc > 1)
556 : : {
557 [ + + - + ]: 308 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
558 : : {
559 : 1 : help(progname);
560 : 1 : exit_nicely(0);
561 : : }
562 [ + + + + ]: 307 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
563 : : {
564 : 69 : puts("pg_dump (PostgreSQL) " PG_VERSION);
565 : 69 : exit_nicely(0);
566 : : }
567 : : }
568 : :
569 : 238 : InitDumpOptions(&dopt);
570 : :
571 : 1376 : while ((c = getopt_long(argc, argv, "abBcCd:e:E:f:F:h:j:n:N:Op:RsS:t:T:U:vwWxXZ:",
572 [ + + ]: 1376 : long_options, &optindex)) != -1)
573 : : {
574 [ + + + + : 1146 : switch (c)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + - +
+ + + + +
+ + - + +
+ + + + +
+ + ]
575 : : {
576 : 9 : case 'a': /* Dump data only */
577 : 9 : data_only = true;
578 : 9 : break;
579 : :
580 : 1 : case 'b': /* Dump LOs */
581 : 1 : dopt.outputLOs = true;
582 : 1 : break;
583 : :
584 : 2 : case 'B': /* Don't dump LOs */
585 : 2 : dopt.dontOutputLOs = true;
586 : 2 : break;
587 : :
588 : 6 : case 'c': /* clean (i.e., drop) schema prior to create */
589 : 6 : dopt.outputClean = 1;
590 : 6 : break;
591 : :
592 : 29 : case 'C': /* Create DB */
593 : 29 : dopt.outputCreateDB = 1;
594 : 29 : break;
595 : :
596 : 5 : case 'd': /* database name */
597 : 5 : dopt.cparams.dbname = pg_strdup(optarg);
598 : 5 : break;
599 : :
600 : 4 : case 'e': /* include extension(s) */
601 : 4 : simple_string_list_append(&extension_include_patterns, optarg);
602 : 4 : dopt.include_everything = false;
603 : 4 : break;
604 : :
605 : 2 : case 'E': /* Dump encoding */
606 : 2 : dumpencoding = pg_strdup(optarg);
607 : 2 : break;
608 : :
609 : 199 : case 'f':
610 : 199 : filename = pg_strdup(optarg);
611 : 199 : break;
612 : :
613 : 118 : case 'F':
614 : 118 : format = pg_strdup(optarg);
615 : 118 : break;
616 : :
617 : 40 : case 'h': /* server host */
618 : 40 : dopt.cparams.pghost = pg_strdup(optarg);
619 : 40 : break;
620 : :
621 : 11 : case 'j': /* number of dump jobs */
622 [ + + ]: 11 : if (!option_parse_int(optarg, "-j/--jobs", 1,
623 : : PG_MAX_JOBS,
624 : : &numWorkers))
625 : 1 : exit_nicely(1);
626 : 10 : break;
627 : :
628 : 18 : case 'n': /* include schema(s) */
629 : 18 : simple_string_list_append(&schema_include_patterns, optarg);
630 : 18 : dopt.include_everything = false;
631 : 18 : break;
632 : :
633 : 1 : case 'N': /* exclude schema(s) */
634 : 1 : simple_string_list_append(&schema_exclude_patterns, optarg);
635 : 1 : break;
636 : :
637 : 2 : case 'O': /* Don't reconnect to match owner */
638 : 2 : dopt.outputNoOwner = 1;
639 : 2 : break;
640 : :
641 : 79 : case 'p': /* server port */
642 : 79 : dopt.cparams.pgport = pg_strdup(optarg);
643 : 79 : break;
644 : :
645 : 2 : case 'R':
646 : : /* no-op, still accepted for backwards compatibility */
647 : 2 : break;
648 : :
649 : 7 : case 's': /* dump schema only */
650 : 7 : schema_only = true;
651 : 7 : break;
652 : :
653 : 1 : case 'S': /* Username for superuser in plain text output */
654 : 1 : dopt.outputSuperuser = pg_strdup(optarg);
655 : 1 : break;
656 : :
657 : 8 : case 't': /* include table(s) */
658 : 8 : simple_string_list_append(&table_include_patterns, optarg);
659 : 8 : dopt.include_everything = false;
660 : 8 : break;
661 : :
662 : 4 : case 'T': /* exclude table(s) */
663 : 4 : simple_string_list_append(&table_exclude_patterns, optarg);
664 : 4 : break;
665 : :
666 : 42 : case 'U':
667 : 42 : dopt.cparams.username = pg_strdup(optarg);
668 : 42 : break;
669 : :
670 : 6 : case 'v': /* verbose */
671 : 6 : g_verbose = true;
672 : 6 : pg_logging_increase_verbosity();
673 : 6 : break;
674 : :
675 : 1 : case 'w':
676 : 1 : dopt.cparams.promptPassword = TRI_NO;
677 : 1 : break;
678 : :
679 : 0 : case 'W':
680 : 0 : dopt.cparams.promptPassword = TRI_YES;
681 : 0 : break;
682 : :
683 : 2 : case 'x': /* skip ACL dump */
684 : 2 : dopt.aclsSkip = true;
685 : 2 : break;
686 : :
687 : 13 : case 'Z': /* Compression */
688 : 13 : parse_compress_options(optarg, &compression_algorithm_str,
689 : : &compression_detail);
690 : 13 : user_compression_defined = true;
691 : 13 : break;
692 : :
693 : 147 : case 0:
694 : : /* This covers the long options. */
695 : 147 : break;
696 : :
697 : 2 : case 2: /* lock-wait-timeout */
698 : 2 : dopt.lockWaitTimeout = pg_strdup(optarg);
699 : 2 : break;
700 : :
701 : 3 : case 3: /* SET ROLE */
702 : 3 : use_role = pg_strdup(optarg);
703 : 3 : break;
704 : :
705 : 1 : case 4: /* exclude table(s) data */
706 : 1 : simple_string_list_append(&tabledata_exclude_patterns, optarg);
707 : 1 : break;
708 : :
709 : 6 : case 5: /* section */
710 : 6 : set_dump_section(optarg, &dopt.dumpSections);
711 : 6 : break;
712 : :
713 : 0 : case 6: /* snapshot */
714 : 0 : dumpsnapshot = pg_strdup(optarg);
715 : 0 : break;
716 : :
717 : 156 : case 7: /* no-sync */
718 : 156 : dosync = false;
719 : 156 : break;
720 : :
721 : 1 : case 8:
722 : 1 : have_extra_float_digits = true;
723 [ + - ]: 1 : if (!option_parse_int(optarg, "--extra-float-digits", -15, 3,
724 : : &extra_float_digits))
725 : 1 : exit_nicely(1);
726 : 0 : break;
727 : :
728 : 2 : case 9: /* inserts */
729 : :
730 : : /*
731 : : * dump_inserts also stores --rows-per-insert, careful not to
732 : : * overwrite that.
733 : : */
734 [ + - ]: 2 : if (dopt.dump_inserts == 0)
735 : 2 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
736 : 2 : break;
737 : :
738 : 2 : case 10: /* rows per insert */
739 [ + + ]: 2 : if (!option_parse_int(optarg, "--rows-per-insert", 1, INT_MAX,
740 : : &dopt.dump_inserts))
741 : 1 : exit_nicely(1);
742 : 1 : break;
743 : :
744 : 4 : case 11: /* include foreign data */
745 : 4 : simple_string_list_append(&foreign_servers_include_patterns,
746 : : optarg);
747 : 4 : break;
748 : :
749 : 1 : case 12: /* include table(s) and their children */
750 : 1 : simple_string_list_append(&table_include_patterns_and_children,
751 : : optarg);
752 : 1 : dopt.include_everything = false;
753 : 1 : break;
754 : :
755 : 1 : case 13: /* exclude table(s) and their children */
756 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
757 : : optarg);
758 : 1 : break;
759 : :
760 : 1 : case 14: /* exclude data of table(s) and children */
761 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
762 : : optarg);
763 : 1 : break;
764 : :
765 : 0 : case 15:
766 [ # # ]: 0 : if (!parse_sync_method(optarg, &sync_method))
767 : 0 : exit_nicely(1);
768 : 0 : break;
769 : :
770 : 26 : case 16: /* read object filters from file */
771 : 26 : read_dump_filters(optarg, &dopt);
772 : 22 : break;
773 : :
774 : 1 : case 17: /* exclude extension(s) */
775 : 1 : simple_string_list_append(&extension_exclude_patterns,
776 : : optarg);
777 : 1 : break;
778 : :
779 : 5 : case 18:
780 : 5 : statistics_only = true;
781 : 5 : break;
782 : :
783 : 42 : case 19:
784 : 42 : no_data = true;
785 : 42 : break;
786 : :
787 : 2 : case 20:
788 : 2 : no_schema = true;
789 : 2 : break;
790 : :
791 : 8 : case 21:
792 : 8 : no_statistics = true;
793 : 8 : break;
794 : :
795 : 96 : case 22:
796 : 96 : with_statistics = true;
797 : 96 : break;
798 : :
799 : 26 : case 25:
800 : 26 : dopt.restrict_key = pg_strdup(optarg);
801 : 26 : break;
802 : :
803 : 1 : default:
804 : : /* getopt_long already emitted a complaint */
805 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
806 : 1 : exit_nicely(1);
807 : : }
808 : : }
809 : :
810 : : /*
811 : : * Non-option argument specifies database name as long as it wasn't
812 : : * already specified with -d / --dbname
813 : : */
814 [ + + + - ]: 230 : if (optind < argc && dopt.cparams.dbname == NULL)
815 : 194 : dopt.cparams.dbname = argv[optind++];
816 : :
817 : : /* Complain if any arguments remain */
818 [ + + ]: 230 : if (optind < argc)
819 : : {
820 : 1 : pg_log_error("too many command-line arguments (first is \"%s\")",
821 : : argv[optind]);
822 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
823 : 1 : exit_nicely(1);
824 : : }
825 : :
826 : : /* --column-inserts implies --inserts */
827 [ + + + - ]: 229 : if (dopt.column_inserts && dopt.dump_inserts == 0)
828 : 1 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
829 : :
830 : : /* *-only options are incompatible with each other */
831 : 229 : check_mut_excl_opts(data_only, "-a/--data-only",
832 : : schema_only, "-s/--schema-only",
833 : : statistics_only, "--statistics-only");
834 : :
835 : : /* --no-* and *-only for same thing are incompatible */
836 : 226 : check_mut_excl_opts(data_only, "-a/--data-only",
837 : : no_data, "--no-data");
838 : 226 : check_mut_excl_opts(schema_only, "-s/--schema-only",
839 : : no_schema, "--no-schema");
840 : 226 : check_mut_excl_opts(statistics_only, "--statistics-only",
841 : : no_statistics, "--no-statistics");
842 : :
843 : : /* --statistics and --no-statistics are incompatible */
844 : 225 : check_mut_excl_opts(with_statistics, "--statistics",
845 : : no_statistics, "--no-statistics");
846 : :
847 : : /* --statistics is incompatible with *-only (except --statistics-only) */
848 : 225 : check_mut_excl_opts(with_statistics, "--statistics",
849 : : data_only, "-a/--data-only",
850 : : schema_only, "-s/--schema-only");
851 : :
852 : : /* --include-foreign-data is incompatible with --schema-only */
853 : 224 : check_mut_excl_opts(foreign_servers_include_patterns.head, "--include-foreign-data",
854 : : schema_only, "-s/--schema-only");
855 : :
856 [ + + + + ]: 223 : if (numWorkers > 1 && foreign_servers_include_patterns.head != NULL)
857 : 1 : pg_fatal("option %s is not supported with parallel backup",
858 : : "--include-foreign-data");
859 : :
860 : : /* --clean is incompatible with --data-only */
861 : 222 : check_mut_excl_opts(dopt.outputClean, "-c/--clean",
862 : : data_only, "-a/--data-only");
863 : :
864 [ + + + + ]: 221 : if (dopt.if_exists && !dopt.outputClean)
865 : 1 : pg_fatal("option %s requires option %s",
866 : : "--if-exists", "-c/--clean");
867 : :
868 : : /*
869 : : * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
870 : : * "--schema-only --no-schema", will have already caused an error in one
871 : : * of the checks above.
872 : : */
873 [ + + + + : 220 : dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) ||
- + ]
874 [ + - + + ]: 440 : data_only) && !no_data;
875 [ + + + + : 220 : dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) ||
- + ]
876 [ + - + + ]: 440 : schema_only) && !no_schema;
877 [ - - - - : 220 : dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) ||
+ + ]
878 [ - + + + : 440 : (statistics_only || with_statistics)) && !no_statistics;
+ - ]
879 : :
880 : :
881 : : /*
882 : : * --inserts are already implied above if --column-inserts or
883 : : * --rows-per-insert were specified.
884 : : */
885 [ + + + - ]: 220 : if (dopt.do_nothing && dopt.dump_inserts == 0)
886 : 1 : pg_fatal("option %s requires option %s, %s, or %s",
887 : : "--on-conflict-do-nothing",
888 : : "--inserts", "--rows-per-insert", "--column-inserts");
889 : :
890 : : /* Identify archive format to emit */
891 : 219 : archiveFormat = parseArchiveFormat(format, &archiveMode);
892 : :
893 : : /* archiveFormat specific setup */
894 [ + + ]: 218 : if (archiveFormat == archNull)
895 : : {
896 : 152 : plainText = 1;
897 : :
898 : : /*
899 : : * If you don't provide a restrict key, one will be appointed for you.
900 : : */
901 [ + + ]: 152 : if (!dopt.restrict_key)
902 : 126 : dopt.restrict_key = generate_restrict_key();
903 [ - + ]: 152 : if (!dopt.restrict_key)
904 : 0 : pg_fatal("could not generate restrict key");
905 [ - + ]: 152 : if (!valid_restrict_key(dopt.restrict_key))
906 : 0 : pg_fatal("invalid restrict key");
907 : : }
908 [ - + ]: 66 : else if (dopt.restrict_key)
909 : 0 : pg_fatal("option %s can only be used with %s",
910 : : "--restrict-key", "--format=plain");
911 : :
912 : : /*
913 : : * Custom and directory formats are compressed by default with gzip when
914 : : * available, not the others. If gzip is not available, no compression is
915 : : * done by default.
916 : : */
917 [ + + + + ]: 218 : if ((archiveFormat == archCustom || archiveFormat == archDirectory) &&
918 [ + + ]: 63 : !user_compression_defined)
919 : : {
920 : : #ifdef HAVE_LIBZ
921 : 57 : compression_algorithm_str = "gzip";
922 : : #else
923 : : compression_algorithm_str = "none";
924 : : #endif
925 : : }
926 : :
927 : : /*
928 : : * Compression options
929 : : */
930 [ + + ]: 218 : if (!parse_compress_algorithm(compression_algorithm_str,
931 : : &compression_algorithm))
932 : 1 : pg_fatal("unrecognized compression algorithm: \"%s\"",
933 : : compression_algorithm_str);
934 : :
935 : 217 : parse_compress_specification(compression_algorithm, compression_detail,
936 : : &compression_spec);
937 : 217 : error_detail = validate_compress_specification(&compression_spec);
938 [ + + ]: 217 : if (error_detail != NULL)
939 : 3 : pg_fatal("invalid compression specification: %s",
940 : : error_detail);
941 : :
942 : 214 : error_detail = supports_compression(compression_spec);
943 [ - + ]: 214 : if (error_detail != NULL)
944 : 0 : pg_fatal("%s", error_detail);
945 : :
946 : : /*
947 : : * Disable support for zstd workers for now - these are based on
948 : : * threading, and it's unclear how it interacts with parallel dumps on
949 : : * platforms where that relies on threads too (e.g. Windows).
950 : : */
951 [ - + ]: 214 : if (compression_spec.options & PG_COMPRESSION_OPTION_WORKERS)
952 : 0 : pg_log_warning("compression option \"%s\" is not currently supported by pg_dump",
953 : : "workers");
954 : :
955 : : /*
956 : : * If emitting an archive format, we always want to emit a DATABASE item,
957 : : * in case --create is specified at pg_restore time.
958 : : */
959 [ + + ]: 214 : if (!plainText)
960 : 66 : dopt.outputCreateDB = 1;
961 : :
962 : : /* Parallel backup only in the directory archive format so far */
963 [ + + + + ]: 214 : if (archiveFormat != archDirectory && numWorkers > 1)
964 : 1 : pg_fatal("parallel backup only supported by the directory format");
965 : :
966 : : /* Open the output file */
967 : 213 : fout = CreateArchive(filename, archiveFormat, compression_spec,
968 : : dosync, archiveMode, setupDumpWorker, sync_method);
969 : :
970 : : /* Make dump options accessible right away */
971 : 212 : SetArchiveOptions(fout, &dopt, NULL);
972 : :
973 : : /* Register the cleanup hook */
974 : 212 : on_exit_close_archive(fout);
975 : :
976 : : /* Let the archiver know how noisy to be */
977 : 212 : fout->verbose = g_verbose;
978 : :
979 : :
980 : : /*
981 : : * We allow the server to be back to 10, and up to any minor release of
982 : : * our own major version. (See also version check in pg_dumpall.c.)
983 : : */
984 : 212 : fout->minRemoteVersion = 100000;
985 : 212 : fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
986 : :
987 : 212 : fout->numWorkers = numWorkers;
988 : :
989 : : /*
990 : : * Open the database using the Archiver, so it knows about it. Errors mean
991 : : * death.
992 : : */
993 : 212 : ConnectDatabaseAhx(fout, &dopt.cparams, false);
994 : 210 : setup_connection(fout, dumpencoding, dumpsnapshot, use_role);
995 : :
996 : : /*
997 : : * On hot standbys, never try to dump unlogged table data, since it will
998 : : * just throw an error.
999 : : */
1000 [ + + ]: 210 : if (fout->isStandby)
1001 : 4 : dopt.no_unlogged_table_data = true;
1002 : :
1003 : : /*
1004 : : * Find the last built-in OID, if needed (prior to 8.1)
1005 : : *
1006 : : * With 8.1 and above, we can just use FirstNormalObjectId - 1.
1007 : : */
1008 : 210 : g_last_builtin_oid = FirstNormalObjectId - 1;
1009 : :
1010 : 210 : pg_log_info("last built-in OID is %u", g_last_builtin_oid);
1011 : :
1012 : : /* Expand schema selection patterns into OID lists */
1013 [ + + ]: 210 : if (schema_include_patterns.head != NULL)
1014 : : {
1015 : 19 : expand_schema_name_patterns(fout, &schema_include_patterns,
1016 : : &schema_include_oids,
1017 : : strict_names);
1018 [ + + ]: 13 : if (schema_include_oids.head == NULL)
1019 : 1 : pg_fatal("no matching schemas were found");
1020 : : }
1021 : 203 : expand_schema_name_patterns(fout, &schema_exclude_patterns,
1022 : : &schema_exclude_oids,
1023 : : false);
1024 : : /* non-matching exclusion patterns aren't an error */
1025 : :
1026 : : /* Expand table selection patterns into OID lists */
1027 : 203 : expand_table_name_patterns(fout, &table_include_patterns,
1028 : : &table_include_oids,
1029 : : strict_names, false);
1030 : 198 : expand_table_name_patterns(fout, &table_include_patterns_and_children,
1031 : : &table_include_oids,
1032 : : strict_names, true);
1033 [ + + ]: 198 : if ((table_include_patterns.head != NULL ||
1034 [ + + ]: 187 : table_include_patterns_and_children.head != NULL) &&
1035 [ + + ]: 13 : table_include_oids.head == NULL)
1036 : 2 : pg_fatal("no matching tables were found");
1037 : :
1038 : 196 : expand_table_name_patterns(fout, &table_exclude_patterns,
1039 : : &table_exclude_oids,
1040 : : false, false);
1041 : 196 : expand_table_name_patterns(fout, &table_exclude_patterns_and_children,
1042 : : &table_exclude_oids,
1043 : : false, true);
1044 : :
1045 : 196 : expand_table_name_patterns(fout, &tabledata_exclude_patterns,
1046 : : &tabledata_exclude_oids,
1047 : : false, false);
1048 : 196 : expand_table_name_patterns(fout, &tabledata_exclude_patterns_and_children,
1049 : : &tabledata_exclude_oids,
1050 : : false, true);
1051 : :
1052 : 196 : expand_foreign_server_name_patterns(fout, &foreign_servers_include_patterns,
1053 : : &foreign_servers_include_oids);
1054 : :
1055 : : /* non-matching exclusion patterns aren't an error */
1056 : :
1057 : : /* Expand extension selection patterns into OID lists */
1058 [ + + ]: 195 : if (extension_include_patterns.head != NULL)
1059 : : {
1060 : 5 : expand_extension_name_patterns(fout, &extension_include_patterns,
1061 : : &extension_include_oids,
1062 : : strict_names);
1063 [ + + ]: 5 : if (extension_include_oids.head == NULL)
1064 : 1 : pg_fatal("no matching extensions were found");
1065 : : }
1066 : 194 : expand_extension_name_patterns(fout, &extension_exclude_patterns,
1067 : : &extension_exclude_oids,
1068 : : false);
1069 : : /* non-matching exclusion patterns aren't an error */
1070 : :
1071 : : /*
1072 : : * Dumping LOs is the default for dumps where an inclusion switch is not
1073 : : * used (an "include everything" dump). -B can be used to exclude LOs
1074 : : * from those dumps. -b can be used to include LOs even when an inclusion
1075 : : * switch is used.
1076 : : *
1077 : : * -s means "schema only" and LOs are data, not schema, so we never
1078 : : * include LOs when -s is used.
1079 : : */
1080 [ + + + + : 194 : if (dopt.include_everything && dopt.dumpData && !dopt.dontOutputLOs)
+ + ]
1081 : 122 : dopt.outputLOs = true;
1082 : :
1083 : : /*
1084 : : * Collect role names so we can map object owner OIDs to names.
1085 : : */
1086 : 194 : collectRoleNames(fout);
1087 : :
1088 : : /*
1089 : : * Now scan the database and create DumpableObject structs for all the
1090 : : * objects we intend to dump.
1091 : : */
1092 : 194 : tblinfo = getSchemaData(fout, &numTables);
1093 : :
1094 [ + + ]: 193 : if (dopt.dumpData)
1095 : : {
1096 : 146 : getTableData(&dopt, tblinfo, numTables, 0);
1097 : 146 : buildMatViewRefreshDependencies(fout);
1098 [ + + ]: 146 : if (!dopt.dumpSchema)
1099 : 7 : getTableDataFKConstraints();
1100 : : }
1101 : :
1102 [ + + + + ]: 193 : if (!dopt.dumpData && dopt.sequence_data)
1103 : 38 : getTableData(&dopt, tblinfo, numTables, RELKIND_SEQUENCE);
1104 : :
1105 : : /*
1106 : : * For binary upgrade mode, dump the pg_shdepend rows for large objects
1107 : : * and maybe even pg_largeobject_metadata (see comment below for details).
1108 : : * This is faster to restore than the equivalent set of large object
1109 : : * commands.
1110 : : */
1111 [ + + ]: 193 : if (dopt.binary_upgrade)
1112 : : {
1113 : : TableInfo *shdepend;
1114 : :
1115 : 42 : shdepend = findTableByOid(SharedDependRelationId);
1116 : 42 : makeTableDataInfo(&dopt, shdepend);
1117 : :
1118 : : /*
1119 : : * Only dump large object shdepend rows for this database.
1120 : : */
1121 : 42 : shdepend->dataObj->filtercond = "WHERE classid = 'pg_largeobject'::regclass "
1122 : : "AND dbid = (SELECT oid FROM pg_database "
1123 : : " WHERE datname = current_database())";
1124 : :
1125 : : /*
1126 : : * For binary upgrades from v16 and newer versions, we can copy
1127 : : * pg_largeobject_metadata's files from the old cluster, so we don't
1128 : : * need to dump its contents. pg_upgrade can't copy/link the files
1129 : : * from older versions because aclitem (needed by
1130 : : * pg_largeobject_metadata.lomacl) changed its storage format in v16.
1131 : : */
1132 [ - + ]: 42 : if (fout->remoteVersion < 160000)
1133 : : {
1134 : : TableInfo *lo_metadata;
1135 : :
1136 : 0 : lo_metadata = findTableByOid(LargeObjectMetadataRelationId);
1137 : 0 : makeTableDataInfo(&dopt, lo_metadata);
1138 : : }
1139 : : }
1140 : :
1141 : : /*
1142 : : * In binary-upgrade mode, we do not have to worry about the actual LO
1143 : : * data or the associated metadata that resides in the pg_largeobject and
1144 : : * pg_largeobject_metadata tables, respectively.
1145 : : *
1146 : : * However, we do need to collect LO information as there may be comments
1147 : : * or other information on LOs that we do need to dump out.
1148 : : */
1149 [ + + + + ]: 193 : if (dopt.outputLOs || dopt.binary_upgrade)
1150 : 164 : getLOs(fout);
1151 : :
1152 : : /*
1153 : : * Collect dependency data to assist in ordering the objects.
1154 : : */
1155 : 193 : getDependencies(fout);
1156 : :
1157 : : /*
1158 : : * Collect ACLs, comments, and security labels, if wanted.
1159 : : */
1160 [ + + ]: 193 : if (!dopt.aclsSkip)
1161 : 191 : getAdditionalACLs(fout);
1162 [ + - ]: 193 : if (!dopt.no_comments)
1163 : 193 : collectComments(fout);
1164 [ + - ]: 193 : if (!dopt.no_security_labels)
1165 : 193 : collectSecLabels(fout);
1166 : :
1167 : : /* For binary upgrade mode, collect required pg_class information. */
1168 [ + + ]: 193 : if (dopt.binary_upgrade)
1169 : 42 : collectBinaryUpgradeClassOids(fout);
1170 : :
1171 : : /* Collect sequence information. */
1172 : 193 : collectSequences(fout);
1173 : :
1174 : : /* Lastly, create dummy objects to represent the section boundaries */
1175 : 193 : boundaryObjs = createBoundaryObjects();
1176 : :
1177 : : /* Get pointers to all the known DumpableObjects */
1178 : 193 : getDumpableObjects(&dobjs, &numObjs);
1179 : :
1180 : : /*
1181 : : * Add dummy dependencies to enforce the dump section ordering.
1182 : : */
1183 : 193 : addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
1184 : :
1185 : : /*
1186 : : * Sort the objects into a safe dump order (no forward references).
1187 : : *
1188 : : * We rely on dependency information to help us determine a safe order, so
1189 : : * the initial sort is mostly for cosmetic purposes: we sort by name to
1190 : : * ensure that logically identical schemas will dump identically.
1191 : : */
1192 : 193 : sortDumpableObjectsByTypeName(dobjs, numObjs);
1193 : :
1194 : 193 : sortDumpableObjects(dobjs, numObjs,
1195 : 193 : boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
1196 : :
1197 : : /*
1198 : : * Create archive TOC entries for all the objects to be dumped, in a safe
1199 : : * order.
1200 : : */
1201 : :
1202 : : /*
1203 : : * First the special entries for ENCODING, STDSTRINGS, and SEARCHPATH.
1204 : : */
1205 : 193 : dumpEncoding(fout);
1206 : 193 : dumpStdStrings(fout);
1207 : 193 : dumpSearchPath(fout);
1208 : :
1209 : : /* The database items are always next, unless we don't want them at all */
1210 [ + + ]: 193 : if (dopt.outputCreateDB)
1211 : 94 : dumpDatabase(fout);
1212 : :
1213 : : /* Now the rearrangeable objects. */
1214 [ + + ]: 732956 : for (i = 0; i < numObjs; i++)
1215 : 732763 : dumpDumpableObject(fout, dobjs[i]);
1216 : :
1217 : : /*
1218 : : * Set up options info to ensure we dump what we want.
1219 : : */
1220 : 193 : ropt = NewRestoreOptions();
1221 : 193 : ropt->filename = filename;
1222 : :
1223 : : /* if you change this list, see dumpOptionsFromRestoreOptions */
1224 [ + + ]: 193 : ropt->cparams.dbname = dopt.cparams.dbname ? pg_strdup(dopt.cparams.dbname) : NULL;
1225 [ + + ]: 193 : ropt->cparams.pgport = dopt.cparams.pgport ? pg_strdup(dopt.cparams.pgport) : NULL;
1226 [ + + ]: 193 : ropt->cparams.pghost = dopt.cparams.pghost ? pg_strdup(dopt.cparams.pghost) : NULL;
1227 [ + + ]: 193 : ropt->cparams.username = dopt.cparams.username ? pg_strdup(dopt.cparams.username) : NULL;
1228 : 193 : ropt->cparams.promptPassword = dopt.cparams.promptPassword;
1229 : 193 : ropt->dropSchema = dopt.outputClean;
1230 : 193 : ropt->dumpData = dopt.dumpData;
1231 : 193 : ropt->dumpSchema = dopt.dumpSchema;
1232 : 193 : ropt->dumpStatistics = dopt.dumpStatistics;
1233 : 193 : ropt->if_exists = dopt.if_exists;
1234 : 193 : ropt->column_inserts = dopt.column_inserts;
1235 : 193 : ropt->dumpSections = dopt.dumpSections;
1236 : 193 : ropt->aclsSkip = dopt.aclsSkip;
1237 : 193 : ropt->superuser = dopt.outputSuperuser;
1238 : 193 : ropt->createDB = dopt.outputCreateDB;
1239 : 193 : ropt->noOwner = dopt.outputNoOwner;
1240 : 193 : ropt->noTableAm = dopt.outputNoTableAm;
1241 : 193 : ropt->noTablespace = dopt.outputNoTablespaces;
1242 : 193 : ropt->disable_triggers = dopt.disable_triggers;
1243 : 193 : ropt->use_setsessauth = dopt.use_setsessauth;
1244 : 193 : ropt->disable_dollar_quoting = dopt.disable_dollar_quoting;
1245 : 193 : ropt->dump_inserts = dopt.dump_inserts;
1246 : 193 : ropt->no_comments = dopt.no_comments;
1247 : 193 : ropt->no_policies = dopt.no_policies;
1248 : 193 : ropt->no_publications = dopt.no_publications;
1249 : 193 : ropt->no_security_labels = dopt.no_security_labels;
1250 : 193 : ropt->no_subscriptions = dopt.no_subscriptions;
1251 : 193 : ropt->lockWaitTimeout = dopt.lockWaitTimeout;
1252 : 193 : ropt->include_everything = dopt.include_everything;
1253 : 193 : ropt->enable_row_security = dopt.enable_row_security;
1254 : 193 : ropt->sequence_data = dopt.sequence_data;
1255 : 193 : ropt->binary_upgrade = dopt.binary_upgrade;
1256 [ + + ]: 193 : ropt->restrict_key = dopt.restrict_key ? pg_strdup(dopt.restrict_key) : NULL;
1257 : :
1258 : 193 : ropt->compression_spec = compression_spec;
1259 : :
1260 : 193 : ropt->suppressDumpWarnings = true; /* We've already shown them */
1261 : :
1262 : 193 : SetArchiveOptions(fout, &dopt, ropt);
1263 : :
1264 : : /* Mark which entries should be output */
1265 : 193 : ProcessArchiveRestoreOptions(fout);
1266 : :
1267 : : /*
1268 : : * The archive's TOC entries are now marked as to which ones will actually
1269 : : * be output, so we can set up their dependency lists properly. This isn't
1270 : : * necessary for plain-text output, though.
1271 : : */
1272 [ + + ]: 193 : if (!plainText)
1273 : 65 : BuildArchiveDependencies(fout);
1274 : :
1275 : : /*
1276 : : * And finally we can do the actual output.
1277 : : *
1278 : : * Note: for non-plain-text output formats, the output file is written
1279 : : * inside CloseArchive(). This is, um, bizarre; but not worth changing
1280 : : * right now.
1281 : : */
1282 [ + + ]: 193 : if (plainText)
1283 : 128 : RestoreArchive(fout);
1284 : :
1285 : 192 : CloseArchive(fout);
1286 : :
1287 : 192 : exit_nicely(0);
1288 : : }
1289 : :
1290 : :
1291 : : static void
1292 : 1 : help(const char *progname)
1293 : : {
1294 : 1 : printf(_("%s exports a PostgreSQL database as an SQL script or to other formats.\n\n"), progname);
1295 : 1 : printf(_("Usage:\n"));
1296 : 1 : printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
1297 : :
1298 : 1 : printf(_("\nGeneral options:\n"));
1299 : 1 : printf(_(" -f, --file=FILENAME output file or directory name\n"));
1300 : 1 : printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
1301 : : " plain text (default))\n"));
1302 : 1 : printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n"));
1303 : 1 : printf(_(" -v, --verbose verbose mode\n"));
1304 : 1 : printf(_(" -V, --version output version information, then exit\n"));
1305 : 1 : printf(_(" -Z, --compress=METHOD[:DETAIL]\n"
1306 : : " compress as specified\n"));
1307 : 1 : printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
1308 : 1 : printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
1309 : 1 : printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
1310 : 1 : printf(_(" -?, --help show this help, then exit\n"));
1311 : :
1312 : 1 : printf(_("\nOptions controlling the output content:\n"));
1313 : 1 : printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
1314 : 1 : printf(_(" -b, --large-objects include large objects in dump\n"));
1315 : 1 : printf(_(" --blobs (same as --large-objects, deprecated)\n"));
1316 : 1 : printf(_(" -B, --no-large-objects exclude large objects in dump\n"));
1317 : 1 : printf(_(" --no-blobs (same as --no-large-objects, deprecated)\n"));
1318 : 1 : printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
1319 : 1 : printf(_(" -C, --create include commands to create database in dump\n"));
1320 : 1 : printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n"));
1321 : 1 : printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
1322 : 1 : printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n"));
1323 : 1 : printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n"));
1324 : 1 : printf(_(" -O, --no-owner skip restoration of object ownership in\n"
1325 : : " plain-text format\n"));
1326 : 1 : printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
1327 : 1 : printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n"));
1328 : 1 : printf(_(" -t, --table=PATTERN dump only the specified table(s)\n"));
1329 : 1 : printf(_(" -T, --exclude-table=PATTERN do NOT dump the specified table(s)\n"));
1330 : 1 : printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
1331 : 1 : printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
1332 : 1 : printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
1333 : 1 : printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
1334 : 1 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
1335 : 1 : printf(_(" --enable-row-security enable row security (dump only content user has\n"
1336 : : " access to)\n"));
1337 : 1 : printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n"));
1338 : 1 : printf(_(" --exclude-table-and-children=PATTERN\n"
1339 : : " do NOT dump the specified table(s), including\n"
1340 : : " child and partition tables\n"));
1341 : 1 : printf(_(" --exclude-table-data=PATTERN do NOT dump data for the specified table(s)\n"));
1342 : 1 : printf(_(" --exclude-table-data-and-children=PATTERN\n"
1343 : : " do NOT dump data for the specified table(s),\n"
1344 : : " including child and partition tables\n"));
1345 : 1 : printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
1346 : 1 : printf(_(" --filter=FILENAME include or exclude objects and data from dump\n"
1347 : : " based on expressions in FILENAME\n"));
1348 : 1 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
1349 : 1 : printf(_(" --include-foreign-data=PATTERN\n"
1350 : : " include data of foreign tables on foreign\n"
1351 : : " servers matching PATTERN\n"));
1352 : 1 : printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
1353 : 1 : printf(_(" --load-via-partition-root load partitions via the root table\n"));
1354 : 1 : printf(_(" --no-comments do not dump comment commands\n"));
1355 : 1 : printf(_(" --no-data do not dump data\n"));
1356 : 1 : printf(_(" --no-policies do not dump row security policies\n"));
1357 : 1 : printf(_(" --no-publications do not dump publications\n"));
1358 : 1 : printf(_(" --no-schema do not dump schema\n"));
1359 : 1 : printf(_(" --no-security-labels do not dump security label assignments\n"));
1360 : 1 : printf(_(" --no-statistics do not dump statistics\n"));
1361 : 1 : printf(_(" --no-subscriptions do not dump subscriptions\n"));
1362 : 1 : printf(_(" --no-table-access-method do not dump table access methods\n"));
1363 : 1 : printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
1364 : 1 : printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
1365 : 1 : printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
1366 : 1 : printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
1367 : 1 : printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
1368 : 1 : printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
1369 : 1 : printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
1370 : 1 : printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
1371 : 1 : printf(_(" --sequence-data include sequence data in dump\n"));
1372 : 1 : printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
1373 : 1 : printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n"));
1374 : 1 : printf(_(" --statistics dump the statistics\n"));
1375 : 1 : printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
1376 : 1 : printf(_(" --strict-names require table and/or schema include patterns to\n"
1377 : : " match at least one entity each\n"));
1378 : 1 : printf(_(" --table-and-children=PATTERN dump only the specified table(s), including\n"
1379 : : " child and partition tables\n"));
1380 : 1 : printf(_(" --use-set-session-authorization\n"
1381 : : " use SET SESSION AUTHORIZATION commands instead of\n"
1382 : : " ALTER OWNER commands to set ownership\n"));
1383 : :
1384 : 1 : printf(_("\nConnection options:\n"));
1385 : 1 : printf(_(" -d, --dbname=DBNAME database to dump\n"));
1386 : 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
1387 : 1 : printf(_(" -p, --port=PORT database server port number\n"));
1388 : 1 : printf(_(" -U, --username=NAME connect as specified database user\n"));
1389 : 1 : printf(_(" -w, --no-password never prompt for password\n"));
1390 : 1 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
1391 : 1 : printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
1392 : :
1393 : 1 : printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
1394 : : "variable value is used.\n\n"));
1395 : 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1396 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1397 : 1 : }
1398 : :
1399 : : static void
1400 : 226 : setup_connection(Archive *AH, const char *dumpencoding,
1401 : : const char *dumpsnapshot, char *use_role)
1402 : : {
1403 : 226 : DumpOptions *dopt = AH->dopt;
1404 : 226 : PGconn *conn = GetConnection(AH);
1405 : :
1406 : 226 : PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL));
1407 : :
1408 : : /*
1409 : : * Set the client encoding if requested.
1410 : : */
1411 [ + + ]: 226 : if (dumpencoding)
1412 : : {
1413 [ - + ]: 18 : if (PQsetClientEncoding(conn, dumpencoding) < 0)
1414 : 0 : pg_fatal("invalid client encoding \"%s\" specified",
1415 : : dumpencoding);
1416 : : }
1417 : :
1418 : : /*
1419 : : * Force standard_conforming_strings on, just in case we are dumping from
1420 : : * an old server that has it disabled. Without this, literals in views,
1421 : : * expressions, etc, would be incorrect for modern servers.
1422 : : */
1423 : 226 : ExecuteSqlStatement(AH, "SET standard_conforming_strings = on");
1424 : :
1425 : : /*
1426 : : * And reflect that to AH->std_strings. You might think that we should
1427 : : * just delete that variable and the code that checks it, but that would
1428 : : * be problematic for pg_restore, which at least for now should still cope
1429 : : * with archives containing the other setting (cf. processStdStringsEntry
1430 : : * in pg_backup_archiver.c).
1431 : : */
1432 : 226 : AH->std_strings = true;
1433 : :
1434 : : /*
1435 : : * Get the active encoding, so we know how to escape strings.
1436 : : */
1437 : 226 : AH->encoding = PQclientEncoding(conn);
1438 : 226 : setFmtEncoding(AH->encoding);
1439 : :
1440 : : /*
1441 : : * Set the role if requested. In a parallel dump worker, we'll be passed
1442 : : * use_role == NULL, but AH->use_role is already set (if user specified it
1443 : : * originally) and we should use that.
1444 : : */
1445 [ + + + + ]: 226 : if (!use_role && AH->use_role)
1446 : 2 : use_role = AH->use_role;
1447 : :
1448 : : /* Set the role if requested */
1449 [ + + ]: 226 : if (use_role)
1450 : : {
1451 : 5 : PQExpBuffer query = createPQExpBuffer();
1452 : :
1453 : 5 : appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
1454 : 5 : ExecuteSqlStatement(AH, query->data);
1455 : 5 : destroyPQExpBuffer(query);
1456 : :
1457 : : /* save it for possible later use by parallel workers */
1458 [ + + ]: 5 : if (!AH->use_role)
1459 : 3 : AH->use_role = pg_strdup(use_role);
1460 : : }
1461 : :
1462 : : /* Set the datestyle to ISO to ensure the dump's portability */
1463 : 226 : ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
1464 : :
1465 : : /* Likewise, avoid using sql_standard intervalstyle */
1466 : 226 : ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
1467 : :
1468 : : /*
1469 : : * Use an explicitly specified extra_float_digits if it has been provided.
1470 : : * Otherwise, set extra_float_digits so that we can dump float data
1471 : : * exactly (given correctly implemented float I/O code, anyway).
1472 : : */
1473 [ - + ]: 226 : if (have_extra_float_digits)
1474 : : {
1475 : 0 : PQExpBuffer q = createPQExpBuffer();
1476 : :
1477 : 0 : appendPQExpBuffer(q, "SET extra_float_digits TO %d",
1478 : : extra_float_digits);
1479 : 0 : ExecuteSqlStatement(AH, q->data);
1480 : 0 : destroyPQExpBuffer(q);
1481 : : }
1482 : : else
1483 : 226 : ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
1484 : :
1485 : : /*
1486 : : * Disable synchronized scanning, to prevent unpredictable changes in row
1487 : : * ordering across a dump and reload.
1488 : : */
1489 : 226 : ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
1490 : :
1491 : : /*
1492 : : * Disable timeouts if supported.
1493 : : */
1494 : 226 : ExecuteSqlStatement(AH, "SET statement_timeout = 0");
1495 : 226 : ExecuteSqlStatement(AH, "SET lock_timeout = 0");
1496 : 226 : ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
1497 [ + - ]: 226 : if (AH->remoteVersion >= 170000)
1498 : 226 : ExecuteSqlStatement(AH, "SET transaction_timeout = 0");
1499 : :
1500 : : /*
1501 : : * Quote all identifiers, if requested.
1502 : : */
1503 [ + + ]: 226 : if (quote_all_identifiers)
1504 : 40 : ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1505 : :
1506 : : /*
1507 : : * Adjust row-security mode, if supported.
1508 : : */
1509 [ - + ]: 226 : if (dopt->enable_row_security)
1510 : 0 : ExecuteSqlStatement(AH, "SET row_security = on");
1511 : : else
1512 : 226 : ExecuteSqlStatement(AH, "SET row_security = off");
1513 : :
1514 : : /*
1515 : : * For security reasons, we restrict the expansion of non-system views and
1516 : : * access to foreign tables during the pg_dump process. This restriction
1517 : : * is adjusted when dumping foreign table data.
1518 : : */
1519 : 226 : set_restrict_relation_kind(AH, "view, foreign-table");
1520 : :
1521 : : /*
1522 : : * Initialize prepared-query state to "nothing prepared". We do this here
1523 : : * so that a parallel dump worker will have its own state.
1524 : : */
1525 : 226 : AH->is_prepared = pg_malloc0_array(bool, NUM_PREP_QUERIES);
1526 : :
1527 : : /*
1528 : : * Start transaction-snapshot mode transaction to dump consistent data.
1529 : : */
1530 : 226 : ExecuteSqlStatement(AH, "BEGIN");
1531 : :
1532 : : /*
1533 : : * To support the combination of serializable_deferrable with the jobs
1534 : : * option we use REPEATABLE READ for the worker connections that are
1535 : : * passed a snapshot. As long as the snapshot is acquired in a
1536 : : * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a
1537 : : * REPEATABLE READ transaction provides the appropriate integrity
1538 : : * guarantees. This is a kluge, but safe for back-patching.
1539 : : */
1540 [ - + - - ]: 226 : if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
1541 : 0 : ExecuteSqlStatement(AH,
1542 : : "SET TRANSACTION ISOLATION LEVEL "
1543 : : "SERIALIZABLE, READ ONLY, DEFERRABLE");
1544 : : else
1545 : 226 : ExecuteSqlStatement(AH,
1546 : : "SET TRANSACTION ISOLATION LEVEL "
1547 : : "REPEATABLE READ, READ ONLY");
1548 : :
1549 : : /*
1550 : : * If user specified a snapshot to use, select that. In a parallel dump
1551 : : * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id
1552 : : * is already set (if the server can handle it) and we should use that.
1553 : : */
1554 [ - + ]: 226 : if (dumpsnapshot)
1555 : 0 : AH->sync_snapshot_id = pg_strdup(dumpsnapshot);
1556 : :
1557 [ + + ]: 226 : if (AH->sync_snapshot_id)
1558 : : {
1559 : 16 : PQExpBuffer query = createPQExpBuffer();
1560 : :
1561 : 16 : appendPQExpBufferStr(query, "SET TRANSACTION SNAPSHOT ");
1562 : 16 : appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1563 : 16 : ExecuteSqlStatement(AH, query->data);
1564 : 16 : destroyPQExpBuffer(query);
1565 : : }
1566 [ + + ]: 210 : else if (AH->numWorkers > 1)
1567 : 8 : AH->sync_snapshot_id = get_synchronized_snapshot(AH);
1568 : 226 : }
1569 : :
1570 : : /* Set up connection for a parallel worker process */
1571 : : static void
1572 : 16 : setupDumpWorker(Archive *AH)
1573 : : {
1574 : : /*
1575 : : * We want to re-select all the same values the leader connection is
1576 : : * using. We'll have inherited directly-usable values in
1577 : : * AH->sync_snapshot_id and AH->use_role, but we need to translate the
1578 : : * inherited encoding value back to a string to pass to setup_connection.
1579 : : */
1580 : 16 : setup_connection(AH,
1581 : : pg_encoding_to_char(AH->encoding),
1582 : : NULL,
1583 : : NULL);
1584 : 16 : }
1585 : :
1586 : : static char *
1587 : 8 : get_synchronized_snapshot(Archive *fout)
1588 : : {
1589 : 8 : char *query = "SELECT pg_catalog.pg_export_snapshot()";
1590 : : char *result;
1591 : : PGresult *res;
1592 : :
1593 : 8 : res = ExecuteSqlQueryForSingleRow(fout, query);
1594 : 8 : result = pg_strdup(PQgetvalue(res, 0, 0));
1595 : 8 : PQclear(res);
1596 : :
1597 : 8 : return result;
1598 : : }
1599 : :
1600 : : static ArchiveFormat
1601 : 219 : parseArchiveFormat(const char *format, ArchiveMode *mode)
1602 : : {
1603 : : ArchiveFormat archiveFormat;
1604 : :
1605 : 219 : *mode = archModeWrite;
1606 : :
1607 [ + + - + ]: 219 : if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1608 : : {
1609 : : /* This is used by pg_dumpall, and is not documented */
1610 : 48 : archiveFormat = archNull;
1611 : 48 : *mode = archModeAppend;
1612 : : }
1613 [ - + ]: 171 : else if (pg_strcasecmp(format, "c") == 0)
1614 : 0 : archiveFormat = archCustom;
1615 [ + + ]: 171 : else if (pg_strcasecmp(format, "custom") == 0)
1616 : 53 : archiveFormat = archCustom;
1617 [ - + ]: 118 : else if (pg_strcasecmp(format, "d") == 0)
1618 : 0 : archiveFormat = archDirectory;
1619 [ + + ]: 118 : else if (pg_strcasecmp(format, "directory") == 0)
1620 : 10 : archiveFormat = archDirectory;
1621 [ + + ]: 108 : else if (pg_strcasecmp(format, "p") == 0)
1622 : 101 : archiveFormat = archNull;
1623 [ + + ]: 7 : else if (pg_strcasecmp(format, "plain") == 0)
1624 : 3 : archiveFormat = archNull;
1625 [ - + ]: 4 : else if (pg_strcasecmp(format, "t") == 0)
1626 : 0 : archiveFormat = archTar;
1627 [ + + ]: 4 : else if (pg_strcasecmp(format, "tar") == 0)
1628 : 3 : archiveFormat = archTar;
1629 : : else
1630 : 1 : pg_fatal("invalid output format \"%s\" specified", format);
1631 : 218 : return archiveFormat;
1632 : : }
1633 : :
1634 : : /*
1635 : : * Find the OIDs of all schemas matching the given list of patterns,
1636 : : * and append them to the given OID list.
1637 : : */
1638 : : static void
1639 : 222 : expand_schema_name_patterns(Archive *fout,
1640 : : SimpleStringList *patterns,
1641 : : SimpleOidList *oids,
1642 : : bool strict_names)
1643 : : {
1644 : : PQExpBuffer query;
1645 : : PGresult *res;
1646 : : SimpleStringListCell *cell;
1647 : : int i;
1648 : :
1649 [ + + ]: 222 : if (patterns->head == NULL)
1650 : 200 : return; /* nothing to do */
1651 : :
1652 : 22 : query = createPQExpBuffer();
1653 : :
1654 : : /*
1655 : : * The loop below runs multiple SELECTs might sometimes result in
1656 : : * duplicate entries in the OID list, but we don't care.
1657 : : */
1658 : :
1659 [ + + ]: 38 : for (cell = patterns->head; cell; cell = cell->next)
1660 : : {
1661 : : PQExpBufferData dbbuf;
1662 : : int dotcnt;
1663 : :
1664 : 22 : appendPQExpBufferStr(query,
1665 : : "SELECT oid FROM pg_catalog.pg_namespace n\n");
1666 : 22 : initPQExpBuffer(&dbbuf);
1667 : 22 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1668 : : false, NULL, "n.nspname", NULL, NULL, &dbbuf,
1669 : : &dotcnt);
1670 [ + + ]: 22 : if (dotcnt > 1)
1671 : 2 : pg_fatal("improper qualified name (too many dotted names): %s",
1672 : : cell->val);
1673 [ + + ]: 20 : else if (dotcnt == 1)
1674 : 3 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1675 : 17 : termPQExpBuffer(&dbbuf);
1676 : :
1677 : 17 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1678 [ + + + - ]: 17 : if (strict_names && PQntuples(res) == 0)
1679 : 1 : pg_fatal("no matching schemas were found for pattern \"%s\"", cell->val);
1680 : :
1681 [ + + ]: 31 : for (i = 0; i < PQntuples(res); i++)
1682 : : {
1683 : 15 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1684 : : }
1685 : :
1686 : 16 : PQclear(res);
1687 : 16 : resetPQExpBuffer(query);
1688 : : }
1689 : :
1690 : 16 : destroyPQExpBuffer(query);
1691 : : }
1692 : :
1693 : : /*
1694 : : * Find the OIDs of all extensions matching the given list of patterns,
1695 : : * and append them to the given OID list.
1696 : : */
1697 : : static void
1698 : 199 : expand_extension_name_patterns(Archive *fout,
1699 : : SimpleStringList *patterns,
1700 : : SimpleOidList *oids,
1701 : : bool strict_names)
1702 : : {
1703 : : PQExpBuffer query;
1704 : : PGresult *res;
1705 : : SimpleStringListCell *cell;
1706 : : int i;
1707 : :
1708 [ + + ]: 199 : if (patterns->head == NULL)
1709 : 192 : return; /* nothing to do */
1710 : :
1711 : 7 : query = createPQExpBuffer();
1712 : :
1713 : : /*
1714 : : * The loop below runs multiple SELECTs might sometimes result in
1715 : : * duplicate entries in the OID list, but we don't care.
1716 : : */
1717 [ + + ]: 14 : for (cell = patterns->head; cell; cell = cell->next)
1718 : : {
1719 : : int dotcnt;
1720 : :
1721 : 7 : appendPQExpBufferStr(query,
1722 : : "SELECT oid FROM pg_catalog.pg_extension e\n");
1723 : 7 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1724 : : false, NULL, "e.extname", NULL, NULL, NULL,
1725 : : &dotcnt);
1726 [ - + ]: 7 : if (dotcnt > 0)
1727 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1728 : : cell->val);
1729 : :
1730 : 7 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1731 [ - + - - ]: 7 : if (strict_names && PQntuples(res) == 0)
1732 : 0 : pg_fatal("no matching extensions were found for pattern \"%s\"", cell->val);
1733 : :
1734 [ + + ]: 13 : for (i = 0; i < PQntuples(res); i++)
1735 : : {
1736 : 6 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1737 : : }
1738 : :
1739 : 7 : PQclear(res);
1740 : 7 : resetPQExpBuffer(query);
1741 : : }
1742 : :
1743 : 7 : destroyPQExpBuffer(query);
1744 : : }
1745 : :
1746 : : /*
1747 : : * Find the OIDs of all foreign servers matching the given list of patterns,
1748 : : * and append them to the given OID list.
1749 : : */
1750 : : static void
1751 : 196 : expand_foreign_server_name_patterns(Archive *fout,
1752 : : SimpleStringList *patterns,
1753 : : SimpleOidList *oids)
1754 : : {
1755 : : PQExpBuffer query;
1756 : : PGresult *res;
1757 : : SimpleStringListCell *cell;
1758 : : int i;
1759 : :
1760 [ + + ]: 196 : if (patterns->head == NULL)
1761 : 193 : return; /* nothing to do */
1762 : :
1763 : 3 : query = createPQExpBuffer();
1764 : :
1765 : : /*
1766 : : * The loop below runs multiple SELECTs might sometimes result in
1767 : : * duplicate entries in the OID list, but we don't care.
1768 : : */
1769 : :
1770 [ + + ]: 5 : for (cell = patterns->head; cell; cell = cell->next)
1771 : : {
1772 : : int dotcnt;
1773 : :
1774 : 3 : appendPQExpBufferStr(query,
1775 : : "SELECT oid FROM pg_catalog.pg_foreign_server s\n");
1776 : 3 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1777 : : false, NULL, "s.srvname", NULL, NULL, NULL,
1778 : : &dotcnt);
1779 [ - + ]: 3 : if (dotcnt > 0)
1780 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1781 : : cell->val);
1782 : :
1783 : 3 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1784 [ + + ]: 3 : if (PQntuples(res) == 0)
1785 : 1 : pg_fatal("no matching foreign servers were found for pattern \"%s\"", cell->val);
1786 : :
1787 [ + + ]: 4 : for (i = 0; i < PQntuples(res); i++)
1788 : 2 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1789 : :
1790 : 2 : PQclear(res);
1791 : 2 : resetPQExpBuffer(query);
1792 : : }
1793 : :
1794 : 2 : destroyPQExpBuffer(query);
1795 : : }
1796 : :
1797 : : /*
1798 : : * Find the OIDs of all tables matching the given list of patterns,
1799 : : * and append them to the given OID list. See also expand_dbname_patterns()
1800 : : * in pg_dumpall.c
1801 : : */
1802 : : static void
1803 : 1185 : expand_table_name_patterns(Archive *fout,
1804 : : SimpleStringList *patterns, SimpleOidList *oids,
1805 : : bool strict_names, bool with_child_tables)
1806 : : {
1807 : : PQExpBuffer query;
1808 : : PGresult *res;
1809 : : SimpleStringListCell *cell;
1810 : : int i;
1811 : :
1812 [ + + ]: 1185 : if (patterns->head == NULL)
1813 : 1156 : return; /* nothing to do */
1814 : :
1815 : 29 : query = createPQExpBuffer();
1816 : :
1817 : : /*
1818 : : * this might sometimes result in duplicate entries in the OID list, but
1819 : : * we don't care.
1820 : : */
1821 : :
1822 [ + + ]: 59 : for (cell = patterns->head; cell; cell = cell->next)
1823 : : {
1824 : : PQExpBufferData dbbuf;
1825 : : int dotcnt;
1826 : :
1827 : : /*
1828 : : * Query must remain ABSOLUTELY devoid of unqualified names. This
1829 : : * would be unnecessary given a pg_table_is_visible() variant taking a
1830 : : * search_path argument.
1831 : : *
1832 : : * For with_child_tables, we start with the basic query's results and
1833 : : * recursively search the inheritance tree to add child tables.
1834 : : */
1835 [ + + ]: 35 : if (with_child_tables)
1836 : : {
1837 : 6 : appendPQExpBufferStr(query, "WITH RECURSIVE partition_tree (relid) AS (\n");
1838 : : }
1839 : :
1840 : 35 : appendPQExpBuffer(query,
1841 : : "SELECT c.oid"
1842 : : "\nFROM pg_catalog.pg_class c"
1843 : : "\n LEFT JOIN pg_catalog.pg_namespace n"
1844 : : "\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace"
1845 : : "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY"
1846 : : "\n (array['%c', '%c', '%c', '%c', '%c', '%c'])\n",
1847 : : RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1848 : : RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
1849 : : RELKIND_PARTITIONED_TABLE);
1850 : 35 : initPQExpBuffer(&dbbuf);
1851 : 35 : processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1852 : : false, "n.nspname", "c.relname", NULL,
1853 : : "pg_catalog.pg_table_is_visible(c.oid)", &dbbuf,
1854 : : &dotcnt);
1855 [ + + ]: 35 : if (dotcnt > 2)
1856 : 1 : pg_fatal("improper relation name (too many dotted names): %s",
1857 : : cell->val);
1858 [ + + ]: 34 : else if (dotcnt == 2)
1859 : 2 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1860 : 32 : termPQExpBuffer(&dbbuf);
1861 : :
1862 [ + + ]: 32 : if (with_child_tables)
1863 : : {
1864 : 6 : appendPQExpBufferStr(query, "UNION"
1865 : : "\nSELECT i.inhrelid"
1866 : : "\nFROM partition_tree p"
1867 : : "\n JOIN pg_catalog.pg_inherits i"
1868 : : "\n ON p.relid OPERATOR(pg_catalog.=) i.inhparent"
1869 : : "\n)"
1870 : : "\nSELECT relid FROM partition_tree");
1871 : : }
1872 : :
1873 : 32 : ExecuteSqlStatement(fout, "RESET search_path");
1874 : 32 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1875 : 32 : PQclear(ExecuteSqlQueryForSingleRow(fout,
1876 : : ALWAYS_SECURE_SEARCH_PATH_SQL));
1877 [ + + + + ]: 32 : if (strict_names && PQntuples(res) == 0)
1878 : 2 : pg_fatal("no matching tables were found for pattern \"%s\"", cell->val);
1879 : :
1880 [ + + ]: 74 : for (i = 0; i < PQntuples(res); i++)
1881 : : {
1882 : 44 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1883 : : }
1884 : :
1885 : 30 : PQclear(res);
1886 : 30 : resetPQExpBuffer(query);
1887 : : }
1888 : :
1889 : 24 : destroyPQExpBuffer(query);
1890 : : }
1891 : :
1892 : : /*
1893 : : * Verifies that the connected database name matches the given database name,
1894 : : * and if not, dies with an error about the given pattern.
1895 : : *
1896 : : * The 'dbname' argument should be a literal name parsed from 'pattern'.
1897 : : */
1898 : : static void
1899 : 5 : prohibit_crossdb_refs(PGconn *conn, const char *dbname, const char *pattern)
1900 : : {
1901 : : const char *db;
1902 : :
1903 : 5 : db = PQdb(conn);
1904 [ - + ]: 5 : if (db == NULL)
1905 : 0 : pg_fatal("You are currently not connected to a database.");
1906 : :
1907 [ + - ]: 5 : if (strcmp(db, dbname) != 0)
1908 : 5 : pg_fatal("cross-database references are not implemented: %s",
1909 : : pattern);
1910 : 0 : }
1911 : :
1912 : : /*
1913 : : * checkExtensionMembership
1914 : : * Determine whether object is an extension member, and if so,
1915 : : * record an appropriate dependency and set the object's dump flag.
1916 : : *
1917 : : * It's important to call this for each object that could be an extension
1918 : : * member. Generally, we integrate this with determining the object's
1919 : : * to-be-dumped-ness, since extension membership overrides other rules for that.
1920 : : *
1921 : : * Returns true if object is an extension member, else false.
1922 : : */
1923 : : static bool
1924 : 622794 : checkExtensionMembership(DumpableObject *dobj, Archive *fout)
1925 : : {
1926 : 622794 : ExtensionInfo *ext = findOwningExtension(dobj->catId);
1927 : :
1928 [ + + ]: 622794 : if (ext == NULL)
1929 : 621969 : return false;
1930 : :
1931 : 825 : dobj->ext_member = true;
1932 : :
1933 : : /* Record dependency so that getDependencies needn't deal with that */
1934 : 825 : addObjectDependency(dobj, ext->dobj.dumpId);
1935 : :
1936 : : /*
1937 : : * Mark the member object to have any non-initial ACLs dumped. (Any
1938 : : * initial ACLs will be removed later, using data from pg_init_privs, so
1939 : : * that we'll dump only the delta from the extension's initial setup.)
1940 : : *
1941 : : * In binary upgrades, we still dump all components of the members
1942 : : * individually, since the idea is to exactly reproduce the database
1943 : : * contents rather than replace the extension contents with something
1944 : : * different.
1945 : : *
1946 : : * Note: it might be interesting someday to implement storage and delta
1947 : : * dumping of extension members' RLS policies and/or security labels.
1948 : : * However there is a pitfall for RLS policies: trying to dump them
1949 : : * requires getting a lock on their tables, and the calling user might not
1950 : : * have privileges for that. We need no lock to examine a table's ACLs,
1951 : : * so the current feature doesn't have a problem of that sort.
1952 : : */
1953 [ + + ]: 825 : if (fout->dopt->binary_upgrade)
1954 : 194 : dobj->dump = ext->dobj.dump;
1955 : : else
1956 : 631 : dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL);
1957 : :
1958 : 825 : return true;
1959 : : }
1960 : :
1961 : : /*
1962 : : * selectDumpableNamespace: policy-setting subroutine
1963 : : * Mark a namespace as to be dumped or not
1964 : : */
1965 : : static void
1966 : 1678 : selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout)
1967 : : {
1968 : : /*
1969 : : * DUMP_COMPONENT_DEFINITION typically implies a CREATE SCHEMA statement
1970 : : * and (for --clean) a DROP SCHEMA statement. (In the absence of
1971 : : * DUMP_COMPONENT_DEFINITION, this value is irrelevant.)
1972 : : */
1973 : 1678 : nsinfo->create = true;
1974 : :
1975 : : /*
1976 : : * If specific tables are being dumped, do not dump any complete
1977 : : * namespaces. If specific namespaces are being dumped, dump just those
1978 : : * namespaces. Otherwise, dump all non-system namespaces.
1979 : : */
1980 [ + + ]: 1678 : if (table_include_oids.head != NULL)
1981 : 61 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1982 [ + + ]: 1617 : else if (schema_include_oids.head != NULL)
1983 : 205 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump =
1984 : 205 : simple_oid_list_member(&schema_include_oids,
1985 : : nsinfo->dobj.catId.oid) ?
1986 [ + + ]: 205 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1987 [ + + ]: 1412 : else if (strcmp(nsinfo->dobj.name, "pg_catalog") == 0)
1988 : : {
1989 : : /*
1990 : : * We dump out any ACLs defined in pg_catalog, if they are interesting
1991 : : * (and not the original ACLs which were set at initdb time, see
1992 : : * pg_init_privs).
1993 : : */
1994 : 171 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
1995 : : }
1996 [ + + ]: 1241 : else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
1997 [ + + ]: 519 : strcmp(nsinfo->dobj.name, "information_schema") == 0)
1998 : : {
1999 : : /* Other system schemas don't get dumped */
2000 : 893 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2001 : : }
2002 [ + + ]: 348 : else if (strcmp(nsinfo->dobj.name, "public") == 0)
2003 : : {
2004 : : /*
2005 : : * The public schema is a strange beast that sits in a sort of
2006 : : * no-mans-land between being a system object and a user object.
2007 : : * CREATE SCHEMA would fail, so its DUMP_COMPONENT_DEFINITION is just
2008 : : * a comment and an indication of ownership. If the owner is the
2009 : : * default, omit that superfluous DUMP_COMPONENT_DEFINITION. Before
2010 : : * v15, the default owner was BOOTSTRAP_SUPERUSERID.
2011 : : */
2012 : 167 : nsinfo->create = false;
2013 : 167 : nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2014 [ + + ]: 167 : if (nsinfo->nspowner == ROLE_PG_DATABASE_OWNER)
2015 : 123 : nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION;
2016 : 167 : nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL;
2017 : :
2018 : : /*
2019 : : * Also, make like it has a comment even if it doesn't; this is so
2020 : : * that we'll emit a command to drop the comment, if appropriate.
2021 : : * (Without this, we'd not call dumpCommentExtended for it.)
2022 : : */
2023 : 167 : nsinfo->dobj.components |= DUMP_COMPONENT_COMMENT;
2024 : : }
2025 : : else
2026 : 181 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2027 : :
2028 : : /*
2029 : : * In any case, a namespace can be excluded by an exclusion switch
2030 : : */
2031 [ + + + + ]: 2209 : if (nsinfo->dobj.dump_contains &&
2032 : 531 : simple_oid_list_member(&schema_exclude_oids,
2033 : : nsinfo->dobj.catId.oid))
2034 : 3 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2035 : :
2036 : : /*
2037 : : * If the schema belongs to an extension, allow extension membership to
2038 : : * override the dump decision for the schema itself. However, this does
2039 : : * not change dump_contains, so this won't change what we do with objects
2040 : : * within the schema. (If they belong to the extension, they'll get
2041 : : * suppressed by it, otherwise not.)
2042 : : */
2043 : 1678 : (void) checkExtensionMembership(&nsinfo->dobj, fout);
2044 : 1678 : }
2045 : :
2046 : : /*
2047 : : * selectDumpableTable: policy-setting subroutine
2048 : : * Mark a table as to be dumped or not
2049 : : */
2050 : : static void
2051 : 52202 : selectDumpableTable(TableInfo *tbinfo, Archive *fout)
2052 : : {
2053 [ + + ]: 52202 : if (checkExtensionMembership(&tbinfo->dobj, fout))
2054 : 225 : return; /* extension membership overrides all else */
2055 : :
2056 : : /*
2057 : : * If specific tables are being dumped, dump just those tables; else, dump
2058 : : * according to the parent namespace's dump flag.
2059 : : */
2060 [ + + ]: 51977 : if (table_include_oids.head != NULL)
2061 : 5332 : tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
2062 : : tbinfo->dobj.catId.oid) ?
2063 [ + + ]: 2666 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2064 : : else
2065 : 49311 : tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains;
2066 : :
2067 : : /*
2068 : : * In any case, a table can be excluded by an exclusion switch
2069 : : */
2070 [ + + + + ]: 85340 : if (tbinfo->dobj.dump &&
2071 : 33363 : simple_oid_list_member(&table_exclude_oids,
2072 : : tbinfo->dobj.catId.oid))
2073 : 12 : tbinfo->dobj.dump = DUMP_COMPONENT_NONE;
2074 : : }
2075 : :
2076 : : /*
2077 : : * selectDumpableType: policy-setting subroutine
2078 : : * Mark a type as to be dumped or not
2079 : : *
2080 : : * If it's a table's rowtype or an autogenerated array type, we also apply a
2081 : : * special type code to facilitate sorting into the desired order. (We don't
2082 : : * want to consider those to be ordinary types because that would bring tables
2083 : : * up into the datatype part of the dump order.) We still set the object's
2084 : : * dump flag; that's not going to cause the dummy type to be dumped, but we
2085 : : * need it so that casts involving such types will be dumped correctly -- see
2086 : : * dumpCast. This means the flag should be set the same as for the underlying
2087 : : * object (the table or base type).
2088 : : */
2089 : : static void
2090 : 142872 : selectDumpableType(TypeInfo *tyinfo, Archive *fout)
2091 : : {
2092 : : /* skip complex types, except for standalone composite types */
2093 [ + + ]: 142872 : if (OidIsValid(tyinfo->typrelid) &&
2094 [ + + ]: 51462 : tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
2095 : : {
2096 : 51277 : TableInfo *tytable = findTableByOid(tyinfo->typrelid);
2097 : :
2098 : 51277 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2099 [ + - ]: 51277 : if (tytable != NULL)
2100 : 51277 : tyinfo->dobj.dump = tytable->dobj.dump;
2101 : : else
2102 : 0 : tyinfo->dobj.dump = DUMP_COMPONENT_NONE;
2103 : 51277 : return;
2104 : : }
2105 : :
2106 : : /* skip auto-generated array and multirange types */
2107 [ + + + + ]: 91595 : if (tyinfo->isArray || tyinfo->isMultirange)
2108 : : {
2109 : 69913 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2110 : :
2111 : : /*
2112 : : * Fall through to set the dump flag; we assume that the subsequent
2113 : : * rules will do the same thing as they would for the array's base
2114 : : * type or multirange's range type. (We cannot reliably look up the
2115 : : * base type here, since getTypes may not have processed it yet.)
2116 : : */
2117 : : }
2118 : :
2119 [ + + ]: 91595 : if (checkExtensionMembership(&tyinfo->dobj, fout))
2120 : 150 : return; /* extension membership overrides all else */
2121 : :
2122 : : /* Dump based on if the contents of the namespace are being dumped */
2123 : 91445 : tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains;
2124 : : }
2125 : :
2126 : : /*
2127 : : * selectDumpableDefaultACL: policy-setting subroutine
2128 : : * Mark a default ACL as to be dumped or not
2129 : : *
2130 : : * For per-schema default ACLs, dump if the schema is to be dumped.
2131 : : * Otherwise dump if we are dumping "everything". Note that dumpSchema
2132 : : * and aclsSkip are checked separately.
2133 : : */
2134 : : static void
2135 : 206 : selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt)
2136 : : {
2137 : : /* Default ACLs can't be extension members */
2138 : :
2139 [ + + ]: 206 : if (dinfo->dobj.namespace)
2140 : : /* default ACLs are considered part of the namespace */
2141 : 96 : dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains;
2142 : : else
2143 : 110 : dinfo->dobj.dump = dopt->include_everything ?
2144 [ + + ]: 110 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2145 : 206 : }
2146 : :
2147 : : /*
2148 : : * selectDumpableCast: policy-setting subroutine
2149 : : * Mark a cast as to be dumped or not
2150 : : *
2151 : : * Casts do not belong to any particular namespace (since they haven't got
2152 : : * names), nor do they have identifiable owners. To distinguish user-defined
2153 : : * casts from built-in ones, we must resort to checking whether the cast's
2154 : : * OID is in the range reserved for initdb.
2155 : : */
2156 : : static void
2157 : 46989 : selectDumpableCast(CastInfo *cast, Archive *fout)
2158 : : {
2159 [ - + ]: 46989 : if (checkExtensionMembership(&cast->dobj, fout))
2160 : 0 : return; /* extension membership overrides all else */
2161 : :
2162 : : /*
2163 : : * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
2164 : : * support ACLs currently.
2165 : : */
2166 [ + + ]: 46989 : if (cast->dobj.catId.oid <= g_last_builtin_oid)
2167 : 46899 : cast->dobj.dump = DUMP_COMPONENT_NONE;
2168 : : else
2169 : 90 : cast->dobj.dump = fout->dopt->include_everything ?
2170 [ + + ]: 90 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2171 : : }
2172 : :
2173 : : /*
2174 : : * selectDumpableProcLang: policy-setting subroutine
2175 : : * Mark a procedural language as to be dumped or not
2176 : : *
2177 : : * Procedural languages do not belong to any particular namespace. To
2178 : : * identify built-in languages, we must resort to checking whether the
2179 : : * language's OID is in the range reserved for initdb.
2180 : : */
2181 : : static void
2182 : 241 : selectDumpableProcLang(ProcLangInfo *plang, Archive *fout)
2183 : : {
2184 [ + + ]: 241 : if (checkExtensionMembership(&plang->dobj, fout))
2185 : 193 : return; /* extension membership overrides all else */
2186 : :
2187 : : /*
2188 : : * Only include procedural languages when we are dumping everything.
2189 : : *
2190 : : * For from-initdb procedural languages, only include ACLs, as we do for
2191 : : * the pg_catalog namespace. We need this because procedural languages do
2192 : : * not live in any namespace.
2193 : : */
2194 [ + + ]: 48 : if (!fout->dopt->include_everything)
2195 : 9 : plang->dobj.dump = DUMP_COMPONENT_NONE;
2196 : : else
2197 : : {
2198 [ - + ]: 39 : if (plang->dobj.catId.oid <= g_last_builtin_oid)
2199 : 0 : plang->dobj.dump = DUMP_COMPONENT_ACL;
2200 : : else
2201 : 39 : plang->dobj.dump = DUMP_COMPONENT_ALL;
2202 : : }
2203 : : }
2204 : :
2205 : : /*
2206 : : * selectDumpableAccessMethod: policy-setting subroutine
2207 : : * Mark an access method as to be dumped or not
2208 : : *
2209 : : * Access methods do not belong to any particular namespace. To identify
2210 : : * built-in access methods, we must resort to checking whether the
2211 : : * method's OID is in the range reserved for initdb.
2212 : : */
2213 : : static void
2214 : 1479 : selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout)
2215 : : {
2216 [ + + ]: 1479 : if (checkExtensionMembership(&method->dobj, fout))
2217 : 25 : return; /* extension membership overrides all else */
2218 : :
2219 : : /*
2220 : : * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
2221 : : * they do not support ACLs currently.
2222 : : */
2223 [ + + ]: 1454 : if (method->dobj.catId.oid <= g_last_builtin_oid)
2224 : 1351 : method->dobj.dump = DUMP_COMPONENT_NONE;
2225 : : else
2226 : 103 : method->dobj.dump = fout->dopt->include_everything ?
2227 [ + + ]: 103 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2228 : : }
2229 : :
2230 : : /*
2231 : : * selectDumpableExtension: policy-setting subroutine
2232 : : * Mark an extension as to be dumped or not
2233 : : *
2234 : : * Built-in extensions should be skipped except for checking ACLs, since we
2235 : : * assume those will already be installed in the target database. We identify
2236 : : * such extensions by their having OIDs in the range reserved for initdb.
2237 : : * We dump all user-added extensions by default. No extensions are dumped
2238 : : * if include_everything is false (i.e., a --schema or --table switch was
2239 : : * given), except if --extension specifies a list of extensions to dump.
2240 : : */
2241 : : static void
2242 : 225 : selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
2243 : : {
2244 : : /*
2245 : : * Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to
2246 : : * change permissions on their member objects, if they wish to, and have
2247 : : * those changes preserved.
2248 : : */
2249 [ + + ]: 225 : if (extinfo->dobj.catId.oid <= g_last_builtin_oid)
2250 : 194 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
2251 : : else
2252 : : {
2253 : : /* check if there is a list of extensions to dump */
2254 [ + + ]: 31 : if (extension_include_oids.head != NULL)
2255 : 4 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2256 : 4 : simple_oid_list_member(&extension_include_oids,
2257 : : extinfo->dobj.catId.oid) ?
2258 [ + + ]: 4 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2259 : : else
2260 : 27 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2261 : 27 : dopt->include_everything ?
2262 [ + + ]: 27 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2263 : :
2264 : : /* check that the extension is not explicitly excluded */
2265 [ + + + + ]: 58 : if (extinfo->dobj.dump &&
2266 : 27 : simple_oid_list_member(&extension_exclude_oids,
2267 : : extinfo->dobj.catId.oid))
2268 : 2 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE;
2269 : : }
2270 : 225 : }
2271 : :
2272 : : /*
2273 : : * selectDumpablePublicationObject: policy-setting subroutine
2274 : : * Mark a publication object as to be dumped or not
2275 : : *
2276 : : * A publication can have schemas and tables which have schemas, but those are
2277 : : * ignored in decision making, because publications are only dumped when we are
2278 : : * dumping everything.
2279 : : */
2280 : : static void
2281 : 502 : selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout)
2282 : : {
2283 [ - + ]: 502 : if (checkExtensionMembership(dobj, fout))
2284 : 0 : return; /* extension membership overrides all else */
2285 : :
2286 : 502 : dobj->dump = fout->dopt->include_everything ?
2287 [ + + ]: 502 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2288 : : }
2289 : :
2290 : : /*
2291 : : * selectDumpableStatisticsObject: policy-setting subroutine
2292 : : * Mark an extended statistics object as to be dumped or not
2293 : : *
2294 : : * We dump an extended statistics object if the schema it's in and the table
2295 : : * it's for are being dumped. (This'll need more thought if statistics
2296 : : * objects ever support cross-table stats.)
2297 : : */
2298 : : static void
2299 : 220 : selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout)
2300 : : {
2301 [ - + ]: 220 : if (checkExtensionMembership(&sobj->dobj, fout))
2302 : 0 : return; /* extension membership overrides all else */
2303 : :
2304 : 220 : sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains;
2305 [ + - ]: 220 : if (sobj->stattable == NULL ||
2306 [ + + ]: 220 : !(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION))
2307 : 35 : sobj->dobj.dump = DUMP_COMPONENT_NONE;
2308 : : }
2309 : :
2310 : : /*
2311 : : * selectDumpableObject: policy-setting subroutine
2312 : : * Mark a generic dumpable object as to be dumped or not
2313 : : *
2314 : : * Use this only for object types without a special-case routine above.
2315 : : */
2316 : : static void
2317 : 427888 : selectDumpableObject(DumpableObject *dobj, Archive *fout)
2318 : : {
2319 [ + + ]: 427888 : if (checkExtensionMembership(dobj, fout))
2320 : 207 : return; /* extension membership overrides all else */
2321 : :
2322 : : /*
2323 : : * Default policy is to dump if parent namespace is dumpable, or for
2324 : : * non-namespace-associated items, dump if we're dumping "everything".
2325 : : */
2326 [ + + ]: 427681 : if (dobj->namespace)
2327 : 426766 : dobj->dump = dobj->namespace->dobj.dump_contains;
2328 : : else
2329 : 915 : dobj->dump = fout->dopt->include_everything ?
2330 [ + + ]: 915 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2331 : : }
2332 : :
2333 : : /*
2334 : : * Dump a table's contents for loading using the COPY command
2335 : : * - this routine is called by the Archiver when it wants the table
2336 : : * to be dumped.
2337 : : */
2338 : : static int
2339 : 4255 : dumpTableData_copy(Archive *fout, const void *dcontext)
2340 : : {
2341 : 4255 : const TableDataInfo *tdinfo = dcontext;
2342 : 4255 : const TableInfo *tbinfo = tdinfo->tdtable;
2343 : 4255 : const char *classname = tbinfo->dobj.name;
2344 : 4255 : PQExpBuffer q = createPQExpBuffer();
2345 : :
2346 : : /*
2347 : : * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
2348 : : * which uses it already.
2349 : : */
2350 : 4255 : PQExpBuffer clistBuf = createPQExpBuffer();
2351 : 4255 : PGconn *conn = GetConnection(fout);
2352 : : PGresult *res;
2353 : : int ret;
2354 : : char *copybuf;
2355 : : const char *column_list;
2356 : :
2357 : 4255 : pg_log_info("dumping contents of table \"%s.%s\"",
2358 : : tbinfo->dobj.namespace->dobj.name, classname);
2359 : :
2360 : : /*
2361 : : * Specify the column list explicitly so that we have no possibility of
2362 : : * retrieving data in the wrong column order. (The default column
2363 : : * ordering of COPY will not be what we want in certain corner cases
2364 : : * involving ADD COLUMN and inheritance.)
2365 : : */
2366 : 4255 : column_list = fmtCopyColumnList(tbinfo, clistBuf);
2367 : :
2368 : : /*
2369 : : * Use COPY (SELECT ...) TO when dumping a foreign table's data, when a
2370 : : * filter condition was specified, and when in binary upgrade mode and
2371 : : * dumping an old pg_largeobject_metadata defined WITH OIDS. For other
2372 : : * cases a simple COPY suffices.
2373 : : */
2374 [ + + + + ]: 4255 : if (tdinfo->filtercond || tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
2375 [ - + - - ]: 4212 : (fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
2376 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
2377 : : {
2378 : : /* Temporary allows to access to foreign tables to dump data */
2379 [ + + ]: 43 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2380 : 1 : set_restrict_relation_kind(fout, "view");
2381 : :
2382 : 43 : appendPQExpBufferStr(q, "COPY (SELECT ");
2383 : : /* klugery to get rid of parens in column list */
2384 [ + - ]: 43 : if (strlen(column_list) > 2)
2385 : : {
2386 : 43 : appendPQExpBufferStr(q, column_list + 1);
2387 : 43 : q->data[q->len - 1] = ' ';
2388 : : }
2389 : : else
2390 : 0 : appendPQExpBufferStr(q, "* ");
2391 : :
2392 : 86 : appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
2393 : 43 : fmtQualifiedDumpable(tbinfo),
2394 [ + + ]: 43 : tdinfo->filtercond ? tdinfo->filtercond : "");
2395 : : }
2396 : : else
2397 : : {
2398 : 4212 : appendPQExpBuffer(q, "COPY %s %s TO stdout;",
2399 : 4212 : fmtQualifiedDumpable(tbinfo),
2400 : : column_list);
2401 : : }
2402 : 4255 : res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
2403 : 4254 : PQclear(res);
2404 : 4254 : destroyPQExpBuffer(clistBuf);
2405 : :
2406 : : for (;;)
2407 : : {
2408 : 1820662 : ret = PQgetCopyData(conn, ©buf, 0);
2409 : :
2410 [ + + ]: 1820662 : if (ret < 0)
2411 : 4254 : break; /* done or error */
2412 : :
2413 [ + - ]: 1816408 : if (copybuf)
2414 : : {
2415 : 1816408 : WriteData(fout, copybuf, ret);
2416 : 1816408 : PQfreemem(copybuf);
2417 : : }
2418 : :
2419 : : /* ----------
2420 : : * THROTTLE:
2421 : : *
2422 : : * There was considerable discussion in late July, 2000 regarding
2423 : : * slowing down pg_dump when backing up large tables. Users with both
2424 : : * slow & fast (multi-processor) machines experienced performance
2425 : : * degradation when doing a backup.
2426 : : *
2427 : : * Initial attempts based on sleeping for a number of ms for each ms
2428 : : * of work were deemed too complex, then a simple 'sleep in each loop'
2429 : : * implementation was suggested. The latter failed because the loop
2430 : : * was too tight. Finally, the following was implemented:
2431 : : *
2432 : : * If throttle is non-zero, then
2433 : : * See how long since the last sleep.
2434 : : * Work out how long to sleep (based on ratio).
2435 : : * If sleep is more than 100ms, then
2436 : : * sleep
2437 : : * reset timer
2438 : : * EndIf
2439 : : * EndIf
2440 : : *
2441 : : * where the throttle value was the number of ms to sleep per ms of
2442 : : * work. The calculation was done in each loop.
2443 : : *
2444 : : * Most of the hard work is done in the backend, and this solution
2445 : : * still did not work particularly well: on slow machines, the ratio
2446 : : * was 50:1, and on medium paced machines, 1:1, and on fast
2447 : : * multi-processor machines, it had little or no effect, for reasons
2448 : : * that were unclear.
2449 : : *
2450 : : * Further discussion ensued, and the proposal was dropped.
2451 : : *
2452 : : * For those people who want this feature, it can be implemented using
2453 : : * gettimeofday in each loop, calculating the time since last sleep,
2454 : : * multiplying that by the sleep ratio, then if the result is more
2455 : : * than a preset 'minimum sleep time' (say 100ms), call the 'select'
2456 : : * function to sleep for a subsecond period ie.
2457 : : *
2458 : : * select(0, NULL, NULL, NULL, &tvi);
2459 : : *
2460 : : * This will return after the interval specified in the structure tvi.
2461 : : * Finally, call gettimeofday again to save the 'last sleep time'.
2462 : : * ----------
2463 : : */
2464 : : }
2465 : 4254 : archprintf(fout, "\\.\n\n\n");
2466 : :
2467 [ - + ]: 4254 : if (ret == -2)
2468 : : {
2469 : : /* copy data transfer failed */
2470 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.", classname);
2471 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2472 : 0 : pg_log_error_detail("Command was: %s", q->data);
2473 : 0 : exit_nicely(1);
2474 : : }
2475 : :
2476 : : /* Check command status and return to normal libpq state */
2477 : 4254 : res = PQgetResult(conn);
2478 [ - + ]: 4254 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
2479 : : {
2480 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetResult() failed.", classname);
2481 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2482 : 0 : pg_log_error_detail("Command was: %s", q->data);
2483 : 0 : exit_nicely(1);
2484 : : }
2485 : 4254 : PQclear(res);
2486 : :
2487 : : /* Do this to ensure we've pumped libpq back to idle state */
2488 [ - + ]: 4254 : if (PQgetResult(conn) != NULL)
2489 : 0 : pg_log_warning("unexpected extra results during COPY of table \"%s\"",
2490 : : classname);
2491 : :
2492 : 4254 : destroyPQExpBuffer(q);
2493 : :
2494 : : /* Revert back the setting */
2495 [ - + ]: 4254 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2496 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2497 : :
2498 : 4254 : return 1;
2499 : : }
2500 : :
2501 : : /*
2502 : : * Dump table data using INSERT commands.
2503 : : *
2504 : : * Caution: when we restore from an archive file direct to database, the
2505 : : * INSERT commands emitted by this function have to be parsed by
2506 : : * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments,
2507 : : * E'' strings, or dollar-quoted strings. So don't emit anything like that.
2508 : : */
2509 : : static int
2510 : 87 : dumpTableData_insert(Archive *fout, const void *dcontext)
2511 : : {
2512 : 87 : const TableDataInfo *tdinfo = dcontext;
2513 : 87 : const TableInfo *tbinfo = tdinfo->tdtable;
2514 : 87 : DumpOptions *dopt = fout->dopt;
2515 : 87 : PQExpBuffer q = createPQExpBuffer();
2516 : 87 : PQExpBuffer insertStmt = NULL;
2517 : : char *attgenerated;
2518 : : PGresult *res;
2519 : : int nfields,
2520 : : i;
2521 : 87 : int rows_per_statement = dopt->dump_inserts;
2522 : 87 : int rows_this_statement = 0;
2523 : :
2524 : : /* Temporary allows to access to foreign tables to dump data */
2525 [ - + ]: 87 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2526 : 0 : set_restrict_relation_kind(fout, "view");
2527 : :
2528 : : /*
2529 : : * If we're going to emit INSERTs with column names, the most efficient
2530 : : * way to deal with generated columns is to exclude them entirely. For
2531 : : * INSERTs without column names, we have to emit DEFAULT rather than the
2532 : : * actual column value --- but we can save a few cycles by fetching nulls
2533 : : * rather than the uninteresting-to-us value.
2534 : : */
2535 : 87 : attgenerated = pg_malloc_array(char, tbinfo->numatts);
2536 : 87 : appendPQExpBufferStr(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT ");
2537 : 87 : nfields = 0;
2538 [ + + ]: 269 : for (i = 0; i < tbinfo->numatts; i++)
2539 : : {
2540 [ + + ]: 182 : if (tbinfo->attisdropped[i])
2541 : 2 : continue;
2542 [ + + + + ]: 180 : if (tbinfo->attgenerated[i] && dopt->column_inserts)
2543 : 8 : continue;
2544 [ + + ]: 172 : if (nfields > 0)
2545 : 92 : appendPQExpBufferStr(q, ", ");
2546 [ + + ]: 172 : if (tbinfo->attgenerated[i])
2547 : 8 : appendPQExpBufferStr(q, "NULL");
2548 : : else
2549 : 164 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[i]));
2550 : 172 : attgenerated[nfields] = tbinfo->attgenerated[i];
2551 : 172 : nfields++;
2552 : : }
2553 : : /* Servers before 9.4 will complain about zero-column SELECT */
2554 [ + + ]: 87 : if (nfields == 0)
2555 : 7 : appendPQExpBufferStr(q, "NULL");
2556 : 87 : appendPQExpBuffer(q, " FROM ONLY %s",
2557 : 87 : fmtQualifiedDumpable(tbinfo));
2558 [ - + ]: 87 : if (tdinfo->filtercond)
2559 : 0 : appendPQExpBuffer(q, " %s", tdinfo->filtercond);
2560 : :
2561 : 87 : ExecuteSqlStatement(fout, q->data);
2562 : :
2563 : : while (1)
2564 : : {
2565 : 139 : res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
2566 : : PGRES_TUPLES_OK);
2567 : :
2568 : : /* cross-check field count, allowing for dummy NULL if any */
2569 [ + + + - ]: 139 : if (nfields != PQnfields(res) &&
2570 [ - + ]: 10 : !(nfields == 0 && PQnfields(res) == 1))
2571 : 0 : pg_fatal("wrong number of fields retrieved from table \"%s\"",
2572 : : tbinfo->dobj.name);
2573 : :
2574 : : /*
2575 : : * First time through, we build as much of the INSERT statement as
2576 : : * possible in "insertStmt", which we can then just print for each
2577 : : * statement. If the table happens to have zero dumpable columns then
2578 : : * this will be a complete statement, otherwise it will end in
2579 : : * "VALUES" and be ready to have the row's column values printed.
2580 : : */
2581 [ + + ]: 139 : if (insertStmt == NULL)
2582 : : {
2583 : : const TableInfo *targettab;
2584 : :
2585 : 87 : insertStmt = createPQExpBuffer();
2586 : :
2587 : : /*
2588 : : * When load-via-partition-root is set or forced, get the root
2589 : : * table name for the partition table, so that we can reload data
2590 : : * through the root table.
2591 : : */
2592 [ + + ]: 87 : if (tbinfo->ispartition &&
2593 [ + - + + ]: 48 : (dopt->load_via_partition_root ||
2594 : 24 : forcePartitionRootLoad(tbinfo)))
2595 : 7 : targettab = getRootTableInfo(tbinfo);
2596 : : else
2597 : 80 : targettab = tbinfo;
2598 : :
2599 : 87 : appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
2600 : 87 : fmtQualifiedDumpable(targettab));
2601 : :
2602 : : /* corner case for zero-column table */
2603 [ + + ]: 87 : if (nfields == 0)
2604 : : {
2605 : 7 : appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n");
2606 : : }
2607 : : else
2608 : : {
2609 : : /* append the list of column names if required */
2610 [ + + ]: 80 : if (dopt->column_inserts)
2611 : : {
2612 : 36 : appendPQExpBufferChar(insertStmt, '(');
2613 [ + + ]: 109 : for (int field = 0; field < nfields; field++)
2614 : : {
2615 [ + + ]: 73 : if (field > 0)
2616 : 37 : appendPQExpBufferStr(insertStmt, ", ");
2617 : 73 : appendPQExpBufferStr(insertStmt,
2618 : 73 : fmtId(PQfname(res, field)));
2619 : : }
2620 : 36 : appendPQExpBufferStr(insertStmt, ") ");
2621 : : }
2622 : :
2623 [ + + ]: 80 : if (tbinfo->needs_override)
2624 : 2 : appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE ");
2625 : :
2626 : 80 : appendPQExpBufferStr(insertStmt, "VALUES");
2627 : : }
2628 : : }
2629 : :
2630 [ + + ]: 3608 : for (int tuple = 0; tuple < PQntuples(res); tuple++)
2631 : : {
2632 : : /* Write the INSERT if not in the middle of a multi-row INSERT. */
2633 [ + + ]: 3469 : if (rows_this_statement == 0)
2634 : 3463 : archputs(insertStmt->data, fout);
2635 : :
2636 : : /*
2637 : : * If it is zero-column table then we've already written the
2638 : : * complete statement, which will mean we've disobeyed
2639 : : * --rows-per-insert when it's set greater than 1. We do support
2640 : : * a way to make this multi-row with: SELECT UNION ALL SELECT
2641 : : * UNION ALL ... but that's non-standard so we should avoid it
2642 : : * given that using INSERTs is mostly only ever needed for
2643 : : * cross-database exports.
2644 : : */
2645 [ + + ]: 3469 : if (nfields == 0)
2646 : 6 : continue;
2647 : :
2648 : : /* Emit a row heading */
2649 [ + + ]: 3463 : if (rows_per_statement == 1)
2650 : 3454 : archputs(" (", fout);
2651 [ + + ]: 9 : else if (rows_this_statement > 0)
2652 : 6 : archputs(",\n\t(", fout);
2653 : : else
2654 : 3 : archputs("\n\t(", fout);
2655 : :
2656 [ + + ]: 10445 : for (int field = 0; field < nfields; field++)
2657 : : {
2658 [ + + ]: 6982 : if (field > 0)
2659 : 3519 : archputs(", ", fout);
2660 [ + + ]: 6982 : if (attgenerated[field])
2661 : : {
2662 : 2 : archputs("DEFAULT", fout);
2663 : 2 : continue;
2664 : : }
2665 [ + + ]: 6980 : if (PQgetisnull(res, tuple, field))
2666 : : {
2667 : 83 : archputs("NULL", fout);
2668 : 83 : continue;
2669 : : }
2670 : :
2671 : : /* XXX This code is partially duplicated in ruleutils.c */
2672 [ + + + + ]: 6897 : switch (PQftype(res, field))
2673 : : {
2674 : 4869 : case INT2OID:
2675 : : case INT4OID:
2676 : : case INT8OID:
2677 : : case OIDOID:
2678 : : case FLOAT4OID:
2679 : : case FLOAT8OID:
2680 : : case NUMERICOID:
2681 : : {
2682 : : /*
2683 : : * These types are printed without quotes unless
2684 : : * they contain values that aren't accepted by the
2685 : : * scanner unquoted (e.g., 'NaN'). Note that
2686 : : * strtod() and friends might accept NaN, so we
2687 : : * can't use that to test.
2688 : : *
2689 : : * In reality we only need to defend against
2690 : : * infinity and NaN, so we need not get too crazy
2691 : : * about pattern matching here.
2692 : : */
2693 : 4869 : const char *s = PQgetvalue(res, tuple, field);
2694 : :
2695 [ + + ]: 4869 : if (strspn(s, "0123456789 +-eE.") == strlen(s))
2696 : 4867 : archputs(s, fout);
2697 : : else
2698 : 2 : archprintf(fout, "'%s'", s);
2699 : : }
2700 : 4869 : break;
2701 : :
2702 : 2 : case BITOID:
2703 : : case VARBITOID:
2704 : 2 : archprintf(fout, "B'%s'",
2705 : : PQgetvalue(res, tuple, field));
2706 : 2 : break;
2707 : :
2708 : 4 : case BOOLOID:
2709 [ + + ]: 4 : if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
2710 : 2 : archputs("true", fout);
2711 : : else
2712 : 2 : archputs("false", fout);
2713 : 4 : break;
2714 : :
2715 : 2022 : default:
2716 : : /* All other types are printed as string literals. */
2717 : 2022 : resetPQExpBuffer(q);
2718 : 2022 : appendStringLiteralAH(q,
2719 : : PQgetvalue(res, tuple, field),
2720 : : fout);
2721 : 2022 : archputs(q->data, fout);
2722 : 2022 : break;
2723 : : }
2724 : : }
2725 : :
2726 : : /* Terminate the row ... */
2727 : 3463 : archputs(")", fout);
2728 : :
2729 : : /* ... and the statement, if the target no. of rows is reached */
2730 [ + + ]: 3463 : if (++rows_this_statement >= rows_per_statement)
2731 : : {
2732 [ - + ]: 3456 : if (dopt->do_nothing)
2733 : 0 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2734 : : else
2735 : 3456 : archputs(";\n", fout);
2736 : : /* Reset the row counter */
2737 : 3456 : rows_this_statement = 0;
2738 : : }
2739 : : }
2740 : :
2741 [ + + ]: 139 : if (PQntuples(res) <= 0)
2742 : : {
2743 : 87 : PQclear(res);
2744 : 87 : break;
2745 : : }
2746 : 52 : PQclear(res);
2747 : : }
2748 : :
2749 : : /* Terminate any statements that didn't make the row count. */
2750 [ + + ]: 87 : if (rows_this_statement > 0)
2751 : : {
2752 [ - + ]: 1 : if (dopt->do_nothing)
2753 : 0 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2754 : : else
2755 : 1 : archputs(";\n", fout);
2756 : : }
2757 : :
2758 : 87 : archputs("\n\n", fout);
2759 : :
2760 : 87 : ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
2761 : :
2762 : 87 : destroyPQExpBuffer(q);
2763 [ + - ]: 87 : if (insertStmt != NULL)
2764 : 87 : destroyPQExpBuffer(insertStmt);
2765 : 87 : pg_free(attgenerated);
2766 : :
2767 : : /* Revert back the setting */
2768 [ - + ]: 87 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2769 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2770 : :
2771 : 87 : return 1;
2772 : : }
2773 : :
2774 : : /*
2775 : : * getRootTableInfo:
2776 : : * get the root TableInfo for the given partition table.
2777 : : */
2778 : : static TableInfo *
2779 : 83 : getRootTableInfo(const TableInfo *tbinfo)
2780 : : {
2781 : : TableInfo *parentTbinfo;
2782 : :
2783 : : Assert(tbinfo->ispartition);
2784 : : Assert(tbinfo->numParents == 1);
2785 : :
2786 : 83 : parentTbinfo = tbinfo->parents[0];
2787 [ - + ]: 83 : while (parentTbinfo->ispartition)
2788 : : {
2789 : : Assert(parentTbinfo->numParents == 1);
2790 : 0 : parentTbinfo = parentTbinfo->parents[0];
2791 : : }
2792 : :
2793 : 83 : return parentTbinfo;
2794 : : }
2795 : :
2796 : : /*
2797 : : * forcePartitionRootLoad
2798 : : * Check if we must force load_via_partition_root for this partition.
2799 : : *
2800 : : * This is required if any level of ancestral partitioned table has an
2801 : : * unsafe partitioning scheme.
2802 : : */
2803 : : static bool
2804 : 1106 : forcePartitionRootLoad(const TableInfo *tbinfo)
2805 : : {
2806 : : TableInfo *parentTbinfo;
2807 : :
2808 : : Assert(tbinfo->ispartition);
2809 : : Assert(tbinfo->numParents == 1);
2810 : :
2811 : 1106 : parentTbinfo = tbinfo->parents[0];
2812 [ + + ]: 1106 : if (parentTbinfo->unsafe_partitions)
2813 : 83 : return true;
2814 [ + + ]: 1243 : while (parentTbinfo->ispartition)
2815 : : {
2816 : : Assert(parentTbinfo->numParents == 1);
2817 : 220 : parentTbinfo = parentTbinfo->parents[0];
2818 [ - + ]: 220 : if (parentTbinfo->unsafe_partitions)
2819 : 0 : return true;
2820 : : }
2821 : :
2822 : 1023 : return false;
2823 : : }
2824 : :
2825 : : /*
2826 : : * dumpTableData -
2827 : : * dump the contents of a single table
2828 : : *
2829 : : * Actually, this just makes an ArchiveEntry for the table contents.
2830 : : */
2831 : : static void
2832 : 4428 : dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
2833 : : {
2834 : 4428 : DumpOptions *dopt = fout->dopt;
2835 : 4428 : const TableInfo *tbinfo = tdinfo->tdtable;
2836 : 4428 : PQExpBuffer copyBuf = createPQExpBuffer();
2837 : 4428 : PQExpBuffer clistBuf = createPQExpBuffer();
2838 : : DataDumperPtr dumpFn;
2839 : 4428 : char *tdDefn = NULL;
2840 : : char *copyStmt;
2841 : : const char *copyFrom;
2842 : :
2843 : : /* We had better have loaded per-column details about this table */
2844 : : Assert(tbinfo->interesting);
2845 : :
2846 : : /*
2847 : : * When load-via-partition-root is set or forced, get the root table name
2848 : : * for the partition table, so that we can reload data through the root
2849 : : * table. Then construct a comment to be inserted into the TOC entry's
2850 : : * defn field, so that such cases can be identified reliably.
2851 : : */
2852 [ + + ]: 4428 : if (tbinfo->ispartition &&
2853 [ + - + + ]: 2164 : (dopt->load_via_partition_root ||
2854 : 1082 : forcePartitionRootLoad(tbinfo)))
2855 : 76 : {
2856 : : const TableInfo *parentTbinfo;
2857 : : char *sanitized;
2858 : :
2859 : 76 : parentTbinfo = getRootTableInfo(tbinfo);
2860 : 76 : copyFrom = fmtQualifiedDumpable(parentTbinfo);
2861 : 76 : sanitized = sanitize_line(copyFrom, true);
2862 : 76 : printfPQExpBuffer(copyBuf, "-- load via partition root %s",
2863 : : sanitized);
2864 : 76 : free(sanitized);
2865 : 76 : tdDefn = pg_strdup(copyBuf->data);
2866 : : }
2867 : : else
2868 : 4352 : copyFrom = fmtQualifiedDumpable(tbinfo);
2869 : :
2870 [ + + ]: 4428 : if (dopt->dump_inserts == 0)
2871 : : {
2872 : : /* Dump/restore using COPY */
2873 : 4341 : dumpFn = dumpTableData_copy;
2874 : : /* must use 2 steps here 'cause fmtId is nonreentrant */
2875 : 4341 : printfPQExpBuffer(copyBuf, "COPY %s ",
2876 : : copyFrom);
2877 : 4341 : appendPQExpBuffer(copyBuf, "%s FROM stdin;\n",
2878 : : fmtCopyColumnList(tbinfo, clistBuf));
2879 : 4341 : copyStmt = copyBuf->data;
2880 : : }
2881 : : else
2882 : : {
2883 : : /* Restore using INSERT */
2884 : 87 : dumpFn = dumpTableData_insert;
2885 : 87 : copyStmt = NULL;
2886 : : }
2887 : :
2888 : : /*
2889 : : * Note: although the TableDataInfo is a full DumpableObject, we treat its
2890 : : * dependency on its table as "special" and pass it to ArchiveEntry now.
2891 : : * See comments for BuildArchiveDependencies.
2892 : : */
2893 [ + - ]: 4428 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2894 : : {
2895 : : TocEntry *te;
2896 : :
2897 : 4428 : te = ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
2898 : 4428 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
2899 : : .namespace = tbinfo->dobj.namespace->dobj.name,
2900 : : .owner = tbinfo->rolname,
2901 : : .description = "TABLE DATA",
2902 : : .section = SECTION_DATA,
2903 : : .createStmt = tdDefn,
2904 : : .copyStmt = copyStmt,
2905 : : .deps = &(tbinfo->dobj.dumpId),
2906 : : .nDeps = 1,
2907 : : .dumpFn = dumpFn,
2908 : : .dumpArg = tdinfo));
2909 : :
2910 : : /*
2911 : : * Set the TocEntry's dataLength in case we are doing a parallel dump
2912 : : * and want to order dump jobs by table size. We choose to measure
2913 : : * dataLength in table pages (including TOAST pages) during dump, so
2914 : : * no scaling is needed.
2915 : : *
2916 : : * However, relpages is declared as "integer" in pg_class, and hence
2917 : : * also in TableInfo, but it's really BlockNumber a/k/a unsigned int.
2918 : : * Cast so that we get the right interpretation of table sizes
2919 : : * exceeding INT_MAX pages.
2920 : : */
2921 : 4428 : te->dataLength = (BlockNumber) tbinfo->relpages;
2922 : 4428 : te->dataLength += (BlockNumber) tbinfo->toastpages;
2923 : :
2924 : : /*
2925 : : * If pgoff_t is only 32 bits wide, the above refinement is useless,
2926 : : * and instead we'd better worry about integer overflow. Clamp to
2927 : : * INT_MAX if the correct result exceeds that.
2928 : : */
2929 : : if (sizeof(te->dataLength) == 4 &&
2930 : : (tbinfo->relpages < 0 || tbinfo->toastpages < 0 ||
2931 : : te->dataLength < 0))
2932 : : te->dataLength = INT_MAX;
2933 : : }
2934 : :
2935 : 4428 : destroyPQExpBuffer(copyBuf);
2936 : 4428 : destroyPQExpBuffer(clistBuf);
2937 : 4428 : }
2938 : :
2939 : : /*
2940 : : * refreshMatViewData -
2941 : : * load or refresh the contents of a single materialized view
2942 : : *
2943 : : * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
2944 : : * statement.
2945 : : */
2946 : : static void
2947 : 363 : refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo)
2948 : : {
2949 : 363 : TableInfo *tbinfo = tdinfo->tdtable;
2950 : : PQExpBuffer q;
2951 : :
2952 : : /* If the materialized view is not flagged as populated, skip this. */
2953 [ + + ]: 363 : if (!tbinfo->relispopulated)
2954 : 72 : return;
2955 : :
2956 : 291 : q = createPQExpBuffer();
2957 : :
2958 : 291 : appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
2959 : 291 : fmtQualifiedDumpable(tbinfo));
2960 : :
2961 [ + - ]: 291 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2962 : 291 : ArchiveEntry(fout,
2963 : : tdinfo->dobj.catId, /* catalog ID */
2964 : 291 : tdinfo->dobj.dumpId, /* dump ID */
2965 : 291 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
2966 : : .namespace = tbinfo->dobj.namespace->dobj.name,
2967 : : .owner = tbinfo->rolname,
2968 : : .description = "MATERIALIZED VIEW DATA",
2969 : : .section = SECTION_POST_DATA,
2970 : : .createStmt = q->data,
2971 : : .deps = tdinfo->dobj.dependencies,
2972 : : .nDeps = tdinfo->dobj.nDeps));
2973 : :
2974 : 291 : destroyPQExpBuffer(q);
2975 : : }
2976 : :
2977 : : /*
2978 : : * getTableData -
2979 : : * set up dumpable objects representing the contents of tables
2980 : : */
2981 : : static void
2982 : 184 : getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
2983 : : {
2984 : : int i;
2985 : :
2986 [ + + ]: 50055 : for (i = 0; i < numTables; i++)
2987 : : {
2988 [ + + + + ]: 49871 : if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
2989 [ + + ]: 953 : (!relkind || tblinfo[i].relkind == relkind))
2990 : 6198 : makeTableDataInfo(dopt, &(tblinfo[i]));
2991 : : }
2992 : 184 : }
2993 : :
2994 : : /*
2995 : : * Make a dumpable object for the data of this specific table
2996 : : *
2997 : : * Note: we make a TableDataInfo if and only if we are going to dump the
2998 : : * table data; the "dump" field in such objects isn't very interesting.
2999 : : */
3000 : : static void
3001 : 6279 : makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo)
3002 : : {
3003 : : TableDataInfo *tdinfo;
3004 : :
3005 : : /*
3006 : : * Nothing to do if we already decided to dump the table. This will
3007 : : * happen for "config" tables.
3008 : : */
3009 [ + + ]: 6279 : if (tbinfo->dataObj != NULL)
3010 : 1 : return;
3011 : :
3012 : : /* Skip VIEWs (no data to dump) */
3013 [ + + ]: 6278 : if (tbinfo->relkind == RELKIND_VIEW)
3014 : 504 : return;
3015 : : /* Skip FOREIGN TABLEs (no data to dump) unless requested explicitly */
3016 [ + + ]: 5774 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
3017 [ + + ]: 40 : (foreign_servers_include_oids.head == NULL ||
3018 [ + + ]: 4 : !simple_oid_list_member(&foreign_servers_include_oids,
3019 : : tbinfo->foreign_server)))
3020 : 39 : return;
3021 : : /* Skip partitioned tables (data in partitions) */
3022 [ + + ]: 5735 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
3023 : 519 : return;
3024 : :
3025 : : /* Don't dump data in unlogged tables, if so requested */
3026 [ + + ]: 5216 : if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
3027 [ + + ]: 41 : dopt->no_unlogged_table_data)
3028 : 18 : return;
3029 : :
3030 : : /* Check that the data is not explicitly excluded */
3031 [ + + ]: 5198 : if (simple_oid_list_member(&tabledata_exclude_oids,
3032 : : tbinfo->dobj.catId.oid))
3033 : 8 : return;
3034 : :
3035 : : /* OK, let's dump it */
3036 : 5190 : tdinfo = pg_malloc_object(TableDataInfo);
3037 : :
3038 [ + + ]: 5190 : if (tbinfo->relkind == RELKIND_MATVIEW)
3039 : 363 : tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
3040 [ + + ]: 4827 : else if (tbinfo->relkind == RELKIND_SEQUENCE)
3041 : 399 : tdinfo->dobj.objType = DO_SEQUENCE_SET;
3042 : : else
3043 : 4428 : tdinfo->dobj.objType = DO_TABLE_DATA;
3044 : :
3045 : : /*
3046 : : * Note: use tableoid 0 so that this object won't be mistaken for
3047 : : * something that pg_depend entries apply to.
3048 : : */
3049 : 5190 : tdinfo->dobj.catId.tableoid = 0;
3050 : 5190 : tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
3051 : 5190 : AssignDumpId(&tdinfo->dobj);
3052 : 5190 : tdinfo->dobj.name = tbinfo->dobj.name;
3053 : 5190 : tdinfo->dobj.namespace = tbinfo->dobj.namespace;
3054 : 5190 : tdinfo->tdtable = tbinfo;
3055 : 5190 : tdinfo->filtercond = NULL; /* might get set later */
3056 : 5190 : addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
3057 : :
3058 : : /* A TableDataInfo contains data, of course */
3059 : 5190 : tdinfo->dobj.components |= DUMP_COMPONENT_DATA;
3060 : :
3061 : 5190 : tbinfo->dataObj = tdinfo;
3062 : :
3063 : : /*
3064 : : * Materialized view statistics must be restored after the data, because
3065 : : * REFRESH MATERIALIZED VIEW replaces the storage and resets the stats.
3066 : : *
3067 : : * The dependency is added here because the statistics objects are created
3068 : : * first.
3069 : : */
3070 [ + + + + ]: 5190 : if (tbinfo->relkind == RELKIND_MATVIEW && tbinfo->stats != NULL)
3071 : : {
3072 : 286 : tbinfo->stats->section = SECTION_POST_DATA;
3073 : 286 : addObjectDependency(&tbinfo->stats->dobj, tdinfo->dobj.dumpId);
3074 : : }
3075 : :
3076 : : /* Make sure that we'll collect per-column info for this table. */
3077 : 5190 : tbinfo->interesting = true;
3078 : : }
3079 : :
3080 : : /*
3081 : : * The refresh for a materialized view must be dependent on the refresh for
3082 : : * any materialized view that this one is dependent on.
3083 : : *
3084 : : * This must be called after all the objects are created, but before they are
3085 : : * sorted.
3086 : : */
3087 : : static void
3088 : 146 : buildMatViewRefreshDependencies(Archive *fout)
3089 : : {
3090 : : PQExpBuffer query;
3091 : : PGresult *res;
3092 : : int ntups,
3093 : : i;
3094 : : int i_classid,
3095 : : i_objid,
3096 : : i_refobjid;
3097 : :
3098 : 146 : query = createPQExpBuffer();
3099 : :
3100 : 146 : appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
3101 : : "( "
3102 : : "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
3103 : : "FROM pg_depend d1 "
3104 : : "JOIN pg_class c1 ON c1.oid = d1.objid "
3105 : : "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
3106 : : " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
3107 : : "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
3108 : : "AND d2.objid = r1.oid "
3109 : : "AND d2.refobjid <> d1.objid "
3110 : : "JOIN pg_class c2 ON c2.oid = d2.refobjid "
3111 : : "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3112 : : CppAsString2(RELKIND_VIEW) ") "
3113 : : "WHERE d1.classid = 'pg_class'::regclass "
3114 : : "UNION "
3115 : : "SELECT w.objid, d3.refobjid, c3.relkind "
3116 : : "FROM w "
3117 : : "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
3118 : : "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
3119 : : "AND d3.objid = r3.oid "
3120 : : "AND d3.refobjid <> w.refobjid "
3121 : : "JOIN pg_class c3 ON c3.oid = d3.refobjid "
3122 : : "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3123 : : CppAsString2(RELKIND_VIEW) ") "
3124 : : ") "
3125 : : "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
3126 : : "FROM w "
3127 : : "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
3128 : :
3129 : 146 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3130 : :
3131 : 146 : ntups = PQntuples(res);
3132 : :
3133 : 146 : i_classid = PQfnumber(res, "classid");
3134 : 146 : i_objid = PQfnumber(res, "objid");
3135 : 146 : i_refobjid = PQfnumber(res, "refobjid");
3136 : :
3137 [ + + ]: 422 : for (i = 0; i < ntups; i++)
3138 : : {
3139 : : CatalogId objId;
3140 : : CatalogId refobjId;
3141 : : DumpableObject *dobj;
3142 : : DumpableObject *refdobj;
3143 : : TableInfo *tbinfo;
3144 : : TableInfo *reftbinfo;
3145 : :
3146 : 276 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
3147 : 276 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
3148 : 276 : refobjId.tableoid = objId.tableoid;
3149 : 276 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
3150 : :
3151 : 276 : dobj = findObjectByCatalogId(objId);
3152 [ - + ]: 276 : if (dobj == NULL)
3153 : 48 : continue;
3154 : :
3155 : : Assert(dobj->objType == DO_TABLE);
3156 : 276 : tbinfo = (TableInfo *) dobj;
3157 : : Assert(tbinfo->relkind == RELKIND_MATVIEW);
3158 : 276 : dobj = (DumpableObject *) tbinfo->dataObj;
3159 [ + + ]: 276 : if (dobj == NULL)
3160 : 48 : continue;
3161 : : Assert(dobj->objType == DO_REFRESH_MATVIEW);
3162 : :
3163 : 228 : refdobj = findObjectByCatalogId(refobjId);
3164 [ - + ]: 228 : if (refdobj == NULL)
3165 : 0 : continue;
3166 : :
3167 : : Assert(refdobj->objType == DO_TABLE);
3168 : 228 : reftbinfo = (TableInfo *) refdobj;
3169 : : Assert(reftbinfo->relkind == RELKIND_MATVIEW);
3170 : 228 : refdobj = (DumpableObject *) reftbinfo->dataObj;
3171 [ - + ]: 228 : if (refdobj == NULL)
3172 : 0 : continue;
3173 : : Assert(refdobj->objType == DO_REFRESH_MATVIEW);
3174 : :
3175 : 228 : addObjectDependency(dobj, refdobj->dumpId);
3176 : :
3177 [ + + ]: 228 : if (!reftbinfo->relispopulated)
3178 : 36 : tbinfo->relispopulated = false;
3179 : : }
3180 : :
3181 : 146 : PQclear(res);
3182 : :
3183 : 146 : destroyPQExpBuffer(query);
3184 : 146 : }
3185 : :
3186 : : /*
3187 : : * getTableDataFKConstraints -
3188 : : * add dump-order dependencies reflecting foreign key constraints
3189 : : *
3190 : : * This code is executed only in a data-only dump --- in schema+data dumps
3191 : : * we handle foreign key issues by not creating the FK constraints until
3192 : : * after the data is loaded. In a data-only dump, however, we want to
3193 : : * order the table data objects in such a way that a table's referenced
3194 : : * tables are restored first. (In the presence of circular references or
3195 : : * self-references this may be impossible; we'll detect and complain about
3196 : : * that during the dependency sorting step.)
3197 : : */
3198 : : static void
3199 : 7 : getTableDataFKConstraints(void)
3200 : : {
3201 : : DumpableObject **dobjs;
3202 : : int numObjs;
3203 : : int i;
3204 : :
3205 : : /* Search through all the dumpable objects for FK constraints */
3206 : 7 : getDumpableObjects(&dobjs, &numObjs);
3207 [ + + ]: 26434 : for (i = 0; i < numObjs; i++)
3208 : : {
3209 [ + + ]: 26427 : if (dobjs[i]->objType == DO_FK_CONSTRAINT)
3210 : : {
3211 : 8 : ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
3212 : : TableInfo *ftable;
3213 : :
3214 : : /* Not interesting unless both tables are to be dumped */
3215 [ + - ]: 8 : if (cinfo->contable == NULL ||
3216 [ + + ]: 8 : cinfo->contable->dataObj == NULL)
3217 : 4 : continue;
3218 : 4 : ftable = findTableByOid(cinfo->confrelid);
3219 [ + - ]: 4 : if (ftable == NULL ||
3220 [ - + ]: 4 : ftable->dataObj == NULL)
3221 : 0 : continue;
3222 : :
3223 : : /*
3224 : : * Okay, make referencing table's TABLE_DATA object depend on the
3225 : : * referenced table's TABLE_DATA object.
3226 : : */
3227 : 4 : addObjectDependency(&cinfo->contable->dataObj->dobj,
3228 : 4 : ftable->dataObj->dobj.dumpId);
3229 : : }
3230 : : }
3231 : 7 : free(dobjs);
3232 : 7 : }
3233 : :
3234 : :
3235 : : /*
3236 : : * dumpDatabase:
3237 : : * dump the database definition
3238 : : */
3239 : : static void
3240 : 94 : dumpDatabase(Archive *fout)
3241 : : {
3242 : 94 : DumpOptions *dopt = fout->dopt;
3243 : 94 : PQExpBuffer dbQry = createPQExpBuffer();
3244 : 94 : PQExpBuffer delQry = createPQExpBuffer();
3245 : 94 : PQExpBuffer creaQry = createPQExpBuffer();
3246 : 94 : PQExpBuffer labelq = createPQExpBuffer();
3247 : 94 : PGconn *conn = GetConnection(fout);
3248 : : PGresult *res;
3249 : : int i_tableoid,
3250 : : i_oid,
3251 : : i_datname,
3252 : : i_datdba,
3253 : : i_encoding,
3254 : : i_datlocprovider,
3255 : : i_collate,
3256 : : i_ctype,
3257 : : i_datlocale,
3258 : : i_daticurules,
3259 : : i_frozenxid,
3260 : : i_minmxid,
3261 : : i_datacl,
3262 : : i_acldefault,
3263 : : i_datistemplate,
3264 : : i_datconnlimit,
3265 : : i_datcollversion,
3266 : : i_tablespace;
3267 : : CatalogId dbCatId;
3268 : : DumpId dbDumpId;
3269 : : DumpableAcl dbdacl;
3270 : : const char *datname,
3271 : : *dba,
3272 : : *encoding,
3273 : : *datlocprovider,
3274 : : *collate,
3275 : : *ctype,
3276 : : *locale,
3277 : : *icurules,
3278 : : *datistemplate,
3279 : : *datconnlimit,
3280 : : *tablespace;
3281 : : uint32 frozenxid,
3282 : : minmxid;
3283 : : char *qdatname;
3284 : :
3285 : 94 : pg_log_info("saving database definition");
3286 : :
3287 : : /*
3288 : : * Fetch the database-level properties for this database.
3289 : : */
3290 : 94 : appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, "
3291 : : "datdba, "
3292 : : "pg_encoding_to_char(encoding) AS encoding, "
3293 : : "datcollate, datctype, datfrozenxid, "
3294 : : "datacl, acldefault('d', datdba) AS acldefault, "
3295 : : "datistemplate, datconnlimit, ");
3296 : 94 : appendPQExpBufferStr(dbQry, "datminmxid, ");
3297 [ + - ]: 94 : if (fout->remoteVersion >= 170000)
3298 : 94 : appendPQExpBufferStr(dbQry, "datlocprovider, datlocale, datcollversion, ");
3299 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
3300 : 0 : appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale AS datlocale, datcollversion, ");
3301 : : else
3302 : 0 : appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS datlocale, NULL AS datcollversion, ");
3303 [ + - ]: 94 : if (fout->remoteVersion >= 160000)
3304 : 94 : appendPQExpBufferStr(dbQry, "daticurules, ");
3305 : : else
3306 : 0 : appendPQExpBufferStr(dbQry, "NULL AS daticurules, ");
3307 : 94 : appendPQExpBufferStr(dbQry,
3308 : : "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
3309 : : "shobj_description(oid, 'pg_database') AS description "
3310 : : "FROM pg_database "
3311 : : "WHERE datname = current_database()");
3312 : :
3313 : 94 : res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
3314 : :
3315 : 94 : i_tableoid = PQfnumber(res, "tableoid");
3316 : 94 : i_oid = PQfnumber(res, "oid");
3317 : 94 : i_datname = PQfnumber(res, "datname");
3318 : 94 : i_datdba = PQfnumber(res, "datdba");
3319 : 94 : i_encoding = PQfnumber(res, "encoding");
3320 : 94 : i_datlocprovider = PQfnumber(res, "datlocprovider");
3321 : 94 : i_collate = PQfnumber(res, "datcollate");
3322 : 94 : i_ctype = PQfnumber(res, "datctype");
3323 : 94 : i_datlocale = PQfnumber(res, "datlocale");
3324 : 94 : i_daticurules = PQfnumber(res, "daticurules");
3325 : 94 : i_frozenxid = PQfnumber(res, "datfrozenxid");
3326 : 94 : i_minmxid = PQfnumber(res, "datminmxid");
3327 : 94 : i_datacl = PQfnumber(res, "datacl");
3328 : 94 : i_acldefault = PQfnumber(res, "acldefault");
3329 : 94 : i_datistemplate = PQfnumber(res, "datistemplate");
3330 : 94 : i_datconnlimit = PQfnumber(res, "datconnlimit");
3331 : 94 : i_datcollversion = PQfnumber(res, "datcollversion");
3332 : 94 : i_tablespace = PQfnumber(res, "tablespace");
3333 : :
3334 : 94 : dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
3335 : 94 : dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
3336 : 94 : datname = PQgetvalue(res, 0, i_datname);
3337 : 94 : dba = getRoleName(PQgetvalue(res, 0, i_datdba));
3338 : 94 : encoding = PQgetvalue(res, 0, i_encoding);
3339 : 94 : datlocprovider = PQgetvalue(res, 0, i_datlocprovider);
3340 : 94 : collate = PQgetvalue(res, 0, i_collate);
3341 : 94 : ctype = PQgetvalue(res, 0, i_ctype);
3342 [ + + ]: 94 : if (!PQgetisnull(res, 0, i_datlocale))
3343 : 14 : locale = PQgetvalue(res, 0, i_datlocale);
3344 : : else
3345 : 80 : locale = NULL;
3346 [ - + ]: 94 : if (!PQgetisnull(res, 0, i_daticurules))
3347 : 0 : icurules = PQgetvalue(res, 0, i_daticurules);
3348 : : else
3349 : 94 : icurules = NULL;
3350 : 94 : frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
3351 : 94 : minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
3352 : 94 : dbdacl.acl = PQgetvalue(res, 0, i_datacl);
3353 : 94 : dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault);
3354 : 94 : datistemplate = PQgetvalue(res, 0, i_datistemplate);
3355 : 94 : datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
3356 : 94 : tablespace = PQgetvalue(res, 0, i_tablespace);
3357 : :
3358 : 94 : qdatname = pg_strdup(fmtId(datname));
3359 : :
3360 : : /*
3361 : : * Prepare the CREATE DATABASE command. We must specify OID (if we want
3362 : : * to preserve that), as well as the encoding, locale, and tablespace
3363 : : * since those can't be altered later. Other DB properties are left to
3364 : : * the DATABASE PROPERTIES entry, so that they can be applied after
3365 : : * reconnecting to the target DB.
3366 : : *
3367 : : * For binary upgrade, we use the FILE_COPY strategy because testing has
3368 : : * shown it to be faster. When the server is in binary upgrade mode, it
3369 : : * will also skip the checkpoints this strategy ordinarily performs.
3370 : : */
3371 [ + + ]: 94 : if (dopt->binary_upgrade)
3372 : : {
3373 : 41 : appendPQExpBuffer(creaQry,
3374 : : "CREATE DATABASE %s WITH TEMPLATE = template0 "
3375 : : "OID = %u STRATEGY = FILE_COPY",
3376 : : qdatname, dbCatId.oid);
3377 : : }
3378 : : else
3379 : : {
3380 : 53 : appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
3381 : : qdatname);
3382 : : }
3383 [ + - ]: 94 : if (strlen(encoding) > 0)
3384 : : {
3385 : 94 : appendPQExpBufferStr(creaQry, " ENCODING = ");
3386 : 94 : appendStringLiteralAH(creaQry, encoding, fout);
3387 : : }
3388 : :
3389 : 94 : appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = ");
3390 [ + + ]: 94 : if (datlocprovider[0] == 'b')
3391 : 14 : appendPQExpBufferStr(creaQry, "builtin");
3392 [ + - ]: 80 : else if (datlocprovider[0] == 'c')
3393 : 80 : appendPQExpBufferStr(creaQry, "libc");
3394 [ # # ]: 0 : else if (datlocprovider[0] == 'i')
3395 : 0 : appendPQExpBufferStr(creaQry, "icu");
3396 : : else
3397 : 0 : pg_fatal("unrecognized locale provider: %s",
3398 : : datlocprovider);
3399 : :
3400 [ + - + - ]: 94 : if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
3401 : : {
3402 : 94 : appendPQExpBufferStr(creaQry, " LOCALE = ");
3403 : 94 : appendStringLiteralAH(creaQry, collate, fout);
3404 : : }
3405 : : else
3406 : : {
3407 [ # # ]: 0 : if (strlen(collate) > 0)
3408 : : {
3409 : 0 : appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
3410 : 0 : appendStringLiteralAH(creaQry, collate, fout);
3411 : : }
3412 [ # # ]: 0 : if (strlen(ctype) > 0)
3413 : : {
3414 : 0 : appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
3415 : 0 : appendStringLiteralAH(creaQry, ctype, fout);
3416 : : }
3417 : : }
3418 [ + + ]: 94 : if (locale)
3419 : : {
3420 [ + - ]: 14 : if (datlocprovider[0] == 'b')
3421 : 14 : appendPQExpBufferStr(creaQry, " BUILTIN_LOCALE = ");
3422 : : else
3423 : 0 : appendPQExpBufferStr(creaQry, " ICU_LOCALE = ");
3424 : :
3425 : 14 : appendStringLiteralAH(creaQry, locale, fout);
3426 : : }
3427 : :
3428 [ - + ]: 94 : if (icurules)
3429 : : {
3430 : 0 : appendPQExpBufferStr(creaQry, " ICU_RULES = ");
3431 : 0 : appendStringLiteralAH(creaQry, icurules, fout);
3432 : : }
3433 : :
3434 : : /*
3435 : : * For binary upgrade, carry over the collation version. For normal
3436 : : * dump/restore, omit the version, so that it is computed upon restore.
3437 : : */
3438 [ + + ]: 94 : if (dopt->binary_upgrade)
3439 : : {
3440 [ + - ]: 41 : if (!PQgetisnull(res, 0, i_datcollversion))
3441 : : {
3442 : 41 : appendPQExpBufferStr(creaQry, " COLLATION_VERSION = ");
3443 : 41 : appendStringLiteralAH(creaQry,
3444 : : PQgetvalue(res, 0, i_datcollversion),
3445 : : fout);
3446 : : }
3447 : : }
3448 : :
3449 : : /*
3450 : : * Note: looking at dopt->outputNoTablespaces here is completely the wrong
3451 : : * thing; the decision whether to specify a tablespace should be left till
3452 : : * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
3453 : : * label the DATABASE entry with the tablespace and let the normal
3454 : : * tablespace selection logic work ... but CREATE DATABASE doesn't pay
3455 : : * attention to default_tablespace, so that won't work.
3456 : : */
3457 [ + - + + ]: 94 : if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
3458 [ + - ]: 5 : !dopt->outputNoTablespaces)
3459 : 5 : appendPQExpBuffer(creaQry, " TABLESPACE = %s",
3460 : : fmtId(tablespace));
3461 : 94 : appendPQExpBufferStr(creaQry, ";\n");
3462 : :
3463 : 94 : appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
3464 : : qdatname);
3465 : :
3466 : 94 : dbDumpId = createDumpId();
3467 : :
3468 : 94 : ArchiveEntry(fout,
3469 : : dbCatId, /* catalog ID */
3470 : : dbDumpId, /* dump ID */
3471 : 94 : ARCHIVE_OPTS(.tag = datname,
3472 : : .owner = dba,
3473 : : .description = "DATABASE",
3474 : : .section = SECTION_PRE_DATA,
3475 : : .createStmt = creaQry->data,
3476 : : .dropStmt = delQry->data));
3477 : :
3478 : : /* Compute correct tag for archive entry */
3479 : 94 : appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
3480 : :
3481 : : /* Dump DB comment if any */
3482 : : {
3483 : : /*
3484 : : * 8.2 and up keep comments on shared objects in a shared table, so we
3485 : : * cannot use the dumpComment() code used for other database objects.
3486 : : * Be careful that the ArchiveEntry parameters match that function.
3487 : : */
3488 : 94 : char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
3489 : :
3490 [ + - + + : 94 : if (comment && *comment && !dopt->no_comments)
+ - ]
3491 : : {
3492 : 49 : resetPQExpBuffer(dbQry);
3493 : :
3494 : : /*
3495 : : * Generates warning when loaded into a differently-named
3496 : : * database.
3497 : : */
3498 : 49 : appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
3499 : 49 : appendStringLiteralAH(dbQry, comment, fout);
3500 : 49 : appendPQExpBufferStr(dbQry, ";\n");
3501 : :
3502 : 49 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3503 : 49 : ARCHIVE_OPTS(.tag = labelq->data,
3504 : : .owner = dba,
3505 : : .description = "COMMENT",
3506 : : .section = SECTION_NONE,
3507 : : .createStmt = dbQry->data,
3508 : : .deps = &dbDumpId,
3509 : : .nDeps = 1));
3510 : : }
3511 : : }
3512 : :
3513 : : /* Dump DB security label, if enabled */
3514 [ + - ]: 94 : if (!dopt->no_security_labels)
3515 : : {
3516 : : PGresult *shres;
3517 : : PQExpBuffer seclabelQry;
3518 : :
3519 : 94 : seclabelQry = createPQExpBuffer();
3520 : :
3521 : 94 : buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
3522 : 94 : shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
3523 : 94 : resetPQExpBuffer(seclabelQry);
3524 : 94 : emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
3525 [ - + ]: 94 : if (seclabelQry->len > 0)
3526 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3527 : 0 : ARCHIVE_OPTS(.tag = labelq->data,
3528 : : .owner = dba,
3529 : : .description = "SECURITY LABEL",
3530 : : .section = SECTION_NONE,
3531 : : .createStmt = seclabelQry->data,
3532 : : .deps = &dbDumpId,
3533 : : .nDeps = 1));
3534 : 94 : destroyPQExpBuffer(seclabelQry);
3535 : 94 : PQclear(shres);
3536 : : }
3537 : :
3538 : : /*
3539 : : * Dump ACL if any. Note that we do not support initial privileges
3540 : : * (pg_init_privs) on databases.
3541 : : */
3542 : 94 : dbdacl.privtype = 0;
3543 : 94 : dbdacl.initprivs = NULL;
3544 : :
3545 : 94 : dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE",
3546 : : qdatname, NULL, NULL,
3547 : : NULL, dba, &dbdacl);
3548 : :
3549 : : /*
3550 : : * Now construct a DATABASE PROPERTIES archive entry to restore any
3551 : : * non-default database-level properties. (The reason this must be
3552 : : * separate is that we cannot put any additional commands into the TOC
3553 : : * entry that has CREATE DATABASE. pg_restore would execute such a group
3554 : : * in an implicit transaction block, and the backend won't allow CREATE
3555 : : * DATABASE in that context.)
3556 : : */
3557 : 94 : resetPQExpBuffer(creaQry);
3558 : 94 : resetPQExpBuffer(delQry);
3559 : :
3560 [ + - - + ]: 94 : if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
3561 : 0 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
3562 : : qdatname, datconnlimit);
3563 : :
3564 [ + + ]: 94 : if (strcmp(datistemplate, "t") == 0)
3565 : : {
3566 : 13 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
3567 : : qdatname);
3568 : :
3569 : : /*
3570 : : * The backend won't accept DROP DATABASE on a template database. We
3571 : : * can deal with that by removing the template marking before the DROP
3572 : : * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
3573 : : * since no such command is currently supported, fake it with a direct
3574 : : * UPDATE on pg_database.
3575 : : */
3576 : 13 : appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
3577 : : "SET datistemplate = false WHERE datname = ");
3578 : 13 : appendStringLiteralAH(delQry, datname, fout);
3579 : 13 : appendPQExpBufferStr(delQry, ";\n");
3580 : : }
3581 : :
3582 : : /*
3583 : : * We do not restore pg_database.dathasloginevt because it is set
3584 : : * automatically on login event trigger creation.
3585 : : */
3586 : :
3587 : : /* Add database-specific SET options */
3588 : 94 : dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
3589 : :
3590 : : /*
3591 : : * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
3592 : : * entry, too, for lack of a better place.
3593 : : */
3594 [ + + ]: 94 : if (dopt->binary_upgrade)
3595 : : {
3596 : 41 : appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
3597 : 41 : appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
3598 : : "SET datfrozenxid = '%u', datminmxid = '%u'\n"
3599 : : "WHERE datname = ",
3600 : : frozenxid, minmxid);
3601 : 41 : appendStringLiteralAH(creaQry, datname, fout);
3602 : 41 : appendPQExpBufferStr(creaQry, ";\n");
3603 : : }
3604 : :
3605 [ + + ]: 94 : if (creaQry->len > 0)
3606 : 45 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3607 : 45 : ARCHIVE_OPTS(.tag = datname,
3608 : : .owner = dba,
3609 : : .description = "DATABASE PROPERTIES",
3610 : : .section = SECTION_PRE_DATA,
3611 : : .createStmt = creaQry->data,
3612 : : .dropStmt = delQry->data,
3613 : : .deps = &dbDumpId));
3614 : :
3615 : : /*
3616 : : * pg_largeobject comes from the old system intact, so set its
3617 : : * relfrozenxids, relminmxids and relfilenode.
3618 : : *
3619 : : * pg_largeobject_metadata also comes from the old system intact for
3620 : : * upgrades from v16 and newer, so set its relfrozenxids, relminmxids, and
3621 : : * relfilenode, too. pg_upgrade can't copy/link the files from older
3622 : : * versions because aclitem (needed by pg_largeobject_metadata.lomacl)
3623 : : * changed its storage format in v16.
3624 : : */
3625 [ + + ]: 94 : if (dopt->binary_upgrade)
3626 : : {
3627 : : PGresult *lo_res;
3628 : 41 : PQExpBuffer loFrozenQry = createPQExpBuffer();
3629 : 41 : PQExpBuffer loOutQry = createPQExpBuffer();
3630 : 41 : PQExpBuffer lomOutQry = createPQExpBuffer();
3631 : 41 : PQExpBuffer loHorizonQry = createPQExpBuffer();
3632 : 41 : PQExpBuffer lomHorizonQry = createPQExpBuffer();
3633 : : int ii_relfrozenxid,
3634 : : ii_relfilenode,
3635 : : ii_oid,
3636 : : ii_relminmxid;
3637 : :
3638 : 41 : appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n"
3639 : : "FROM pg_catalog.pg_class\n"
3640 : : "WHERE oid IN (%u, %u, %u, %u);\n",
3641 : : LargeObjectRelationId, LargeObjectLOidPNIndexId,
3642 : : LargeObjectMetadataRelationId, LargeObjectMetadataOidIndexId);
3643 : :
3644 : 41 : lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
3645 : :
3646 : 41 : ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
3647 : 41 : ii_relminmxid = PQfnumber(lo_res, "relminmxid");
3648 : 41 : ii_relfilenode = PQfnumber(lo_res, "relfilenode");
3649 : 41 : ii_oid = PQfnumber(lo_res, "oid");
3650 : :
3651 : 41 : appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
3652 : 41 : appendPQExpBufferStr(lomHorizonQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n");
3653 : 41 : appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n");
3654 : 41 : appendPQExpBufferStr(lomOutQry, "\n-- For binary upgrade, preserve pg_largeobject_metadata and index relfilenodes\n");
3655 [ + + ]: 205 : for (int i = 0; i < PQntuples(lo_res); ++i)
3656 : : {
3657 : : Oid oid;
3658 : : RelFileNumber relfilenumber;
3659 : : PQExpBuffer horizonQry;
3660 : : PQExpBuffer outQry;
3661 : :
3662 : 164 : oid = atooid(PQgetvalue(lo_res, i, ii_oid));
3663 : 164 : relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode));
3664 : :
3665 [ + + + + ]: 164 : if (oid == LargeObjectRelationId ||
3666 : : oid == LargeObjectLOidPNIndexId)
3667 : : {
3668 : 82 : horizonQry = loHorizonQry;
3669 : 82 : outQry = loOutQry;
3670 : : }
3671 : : else
3672 : : {
3673 : 82 : horizonQry = lomHorizonQry;
3674 : 82 : outQry = lomOutQry;
3675 : : }
3676 : :
3677 : 164 : appendPQExpBuffer(horizonQry, "UPDATE pg_catalog.pg_class\n"
3678 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
3679 : : "WHERE oid = %u;\n",
3680 : 164 : atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)),
3681 : 164 : atooid(PQgetvalue(lo_res, i, ii_relminmxid)),
3682 : 164 : atooid(PQgetvalue(lo_res, i, ii_oid)));
3683 : :
3684 [ + + + + ]: 164 : if (oid == LargeObjectRelationId ||
3685 : : oid == LargeObjectMetadataRelationId)
3686 : 82 : appendPQExpBuffer(outQry,
3687 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
3688 : : relfilenumber);
3689 [ + + + - ]: 82 : else if (oid == LargeObjectLOidPNIndexId ||
3690 : : oid == LargeObjectMetadataOidIndexId)
3691 : 82 : appendPQExpBuffer(outQry,
3692 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
3693 : : relfilenumber);
3694 : : }
3695 : :
3696 : 41 : appendPQExpBufferStr(loOutQry,
3697 : : "TRUNCATE pg_catalog.pg_largeobject;\n");
3698 : 41 : appendPQExpBufferStr(lomOutQry,
3699 : : "TRUNCATE pg_catalog.pg_largeobject_metadata;\n");
3700 : :
3701 : 41 : appendPQExpBufferStr(loOutQry, loHorizonQry->data);
3702 : 41 : appendPQExpBufferStr(lomOutQry, lomHorizonQry->data);
3703 : :
3704 : 41 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3705 : 41 : ARCHIVE_OPTS(.tag = "pg_largeobject",
3706 : : .description = "pg_largeobject",
3707 : : .section = SECTION_PRE_DATA,
3708 : : .createStmt = loOutQry->data));
3709 : :
3710 [ + - ]: 41 : if (fout->remoteVersion >= 160000)
3711 : 41 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3712 : 41 : ARCHIVE_OPTS(.tag = "pg_largeobject_metadata",
3713 : : .description = "pg_largeobject_metadata",
3714 : : .section = SECTION_PRE_DATA,
3715 : : .createStmt = lomOutQry->data));
3716 : :
3717 : 41 : PQclear(lo_res);
3718 : :
3719 : 41 : destroyPQExpBuffer(loFrozenQry);
3720 : 41 : destroyPQExpBuffer(loHorizonQry);
3721 : 41 : destroyPQExpBuffer(lomHorizonQry);
3722 : 41 : destroyPQExpBuffer(loOutQry);
3723 : 41 : destroyPQExpBuffer(lomOutQry);
3724 : : }
3725 : :
3726 : 94 : PQclear(res);
3727 : :
3728 : 94 : pg_free(qdatname);
3729 : 94 : destroyPQExpBuffer(dbQry);
3730 : 94 : destroyPQExpBuffer(delQry);
3731 : 94 : destroyPQExpBuffer(creaQry);
3732 : 94 : destroyPQExpBuffer(labelq);
3733 : 94 : }
3734 : :
3735 : : /*
3736 : : * Collect any database-specific or role-and-database-specific SET options
3737 : : * for this database, and append them to outbuf.
3738 : : */
3739 : : static void
3740 : 94 : dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
3741 : : const char *dbname, Oid dboid)
3742 : : {
3743 : 94 : PGconn *conn = GetConnection(AH);
3744 : 94 : PQExpBuffer buf = createPQExpBuffer();
3745 : : PGresult *res;
3746 : :
3747 : : /* First collect database-specific options */
3748 : 94 : printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3749 : : "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3750 : : dboid);
3751 : :
3752 : 94 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3753 : :
3754 [ + + ]: 124 : for (int i = 0; i < PQntuples(res); i++)
3755 : 30 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 0),
3756 : : "DATABASE", dbname, NULL, NULL,
3757 : : outbuf);
3758 : :
3759 : 94 : PQclear(res);
3760 : :
3761 : : /* Now look for role-and-database-specific options */
3762 : 94 : printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3763 : : "FROM pg_db_role_setting s, pg_roles r "
3764 : : "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3765 : : dboid);
3766 : :
3767 : 94 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3768 : :
3769 [ - + ]: 94 : for (int i = 0; i < PQntuples(res); i++)
3770 : 0 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 1),
3771 : 0 : "ROLE", PQgetvalue(res, i, 0),
3772 : : "DATABASE", dbname,
3773 : : outbuf);
3774 : :
3775 : 94 : PQclear(res);
3776 : :
3777 : 94 : destroyPQExpBuffer(buf);
3778 : 94 : }
3779 : :
3780 : : /*
3781 : : * dumpEncoding: put the correct encoding into the archive
3782 : : */
3783 : : static void
3784 : 193 : dumpEncoding(Archive *AH)
3785 : : {
3786 : 193 : const char *encname = pg_encoding_to_char(AH->encoding);
3787 : 193 : PQExpBuffer qry = createPQExpBuffer();
3788 : :
3789 : 193 : pg_log_info("saving encoding = %s", encname);
3790 : :
3791 : 193 : appendPQExpBufferStr(qry, "SET client_encoding = ");
3792 : 193 : appendStringLiteralAH(qry, encname, AH);
3793 : 193 : appendPQExpBufferStr(qry, ";\n");
3794 : :
3795 : 193 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3796 : 193 : ARCHIVE_OPTS(.tag = "ENCODING",
3797 : : .description = "ENCODING",
3798 : : .section = SECTION_PRE_DATA,
3799 : : .createStmt = qry->data));
3800 : :
3801 : 193 : destroyPQExpBuffer(qry);
3802 : 193 : }
3803 : :
3804 : :
3805 : : /*
3806 : : * dumpStdStrings: put the correct escape string behavior into the archive
3807 : : */
3808 : : static void
3809 : 193 : dumpStdStrings(Archive *AH)
3810 : : {
3811 [ + - ]: 193 : const char *stdstrings = AH->std_strings ? "on" : "off";
3812 : 193 : PQExpBuffer qry = createPQExpBuffer();
3813 : :
3814 : 193 : pg_log_info("saving \"standard_conforming_strings = %s\"",
3815 : : stdstrings);
3816 : :
3817 : 193 : appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
3818 : : stdstrings);
3819 : :
3820 : 193 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3821 : 193 : ARCHIVE_OPTS(.tag = "STDSTRINGS",
3822 : : .description = "STDSTRINGS",
3823 : : .section = SECTION_PRE_DATA,
3824 : : .createStmt = qry->data));
3825 : :
3826 : 193 : destroyPQExpBuffer(qry);
3827 : 193 : }
3828 : :
3829 : : /*
3830 : : * dumpSearchPath: record the active search_path in the archive
3831 : : */
3832 : : static void
3833 : 193 : dumpSearchPath(Archive *AH)
3834 : : {
3835 : 193 : PQExpBuffer qry = createPQExpBuffer();
3836 : 193 : PQExpBuffer path = createPQExpBuffer();
3837 : : PGresult *res;
3838 : 193 : char **schemanames = NULL;
3839 : 193 : int nschemanames = 0;
3840 : : int i;
3841 : :
3842 : : /*
3843 : : * We use the result of current_schemas(), not the search_path GUC,
3844 : : * because that might contain wildcards such as "$user", which won't
3845 : : * necessarily have the same value during restore. Also, this way avoids
3846 : : * listing schemas that may appear in search_path but not actually exist,
3847 : : * which seems like a prudent exclusion.
3848 : : */
3849 : 193 : res = ExecuteSqlQueryForSingleRow(AH,
3850 : : "SELECT pg_catalog.current_schemas(false)");
3851 : :
3852 [ - + ]: 193 : if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames))
3853 : 0 : pg_fatal("could not parse result of current_schemas()");
3854 : :
3855 : : /*
3856 : : * We use set_config(), not a simple "SET search_path" command, because
3857 : : * the latter has less-clean behavior if the search path is empty. While
3858 : : * that's likely to get fixed at some point, it seems like a good idea to
3859 : : * be as backwards-compatible as possible in what we put into archives.
3860 : : */
3861 [ - + ]: 193 : for (i = 0; i < nschemanames; i++)
3862 : : {
3863 [ # # ]: 0 : if (i > 0)
3864 : 0 : appendPQExpBufferStr(path, ", ");
3865 : 0 : appendPQExpBufferStr(path, fmtId(schemanames[i]));
3866 : : }
3867 : :
3868 : 193 : appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', ");
3869 : 193 : appendStringLiteralAH(qry, path->data, AH);
3870 : 193 : appendPQExpBufferStr(qry, ", false);\n");
3871 : :
3872 : 193 : pg_log_info("saving \"search_path = %s\"", path->data);
3873 : :
3874 : 193 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3875 : 193 : ARCHIVE_OPTS(.tag = "SEARCHPATH",
3876 : : .description = "SEARCHPATH",
3877 : : .section = SECTION_PRE_DATA,
3878 : : .createStmt = qry->data));
3879 : :
3880 : : /* Also save it in AH->searchpath, in case we're doing plain text dump */
3881 : 193 : AH->searchpath = pg_strdup(qry->data);
3882 : :
3883 : 193 : free(schemanames);
3884 : 193 : PQclear(res);
3885 : 193 : destroyPQExpBuffer(qry);
3886 : 193 : destroyPQExpBuffer(path);
3887 : 193 : }
3888 : :
3889 : :
3890 : : /*
3891 : : * getLOs:
3892 : : * Collect schema-level data about large objects
3893 : : */
3894 : : static void
3895 : 164 : getLOs(Archive *fout)
3896 : : {
3897 : 164 : DumpOptions *dopt = fout->dopt;
3898 : 164 : PQExpBuffer loQry = createPQExpBuffer();
3899 : : PGresult *res;
3900 : : int ntups;
3901 : : int i;
3902 : : int n;
3903 : : int i_oid;
3904 : : int i_lomowner;
3905 : : int i_lomacl;
3906 : : int i_acldefault;
3907 : :
3908 : 164 : pg_log_info("reading large objects");
3909 : :
3910 : : /*
3911 : : * Fetch LO OIDs and owner/ACL data. Order the data so that all the blobs
3912 : : * with the same owner/ACL appear together.
3913 : : */
3914 : 164 : appendPQExpBufferStr(loQry,
3915 : : "SELECT oid, lomowner, lomacl, "
3916 : : "acldefault('L', lomowner) AS acldefault "
3917 : : "FROM pg_largeobject_metadata ");
3918 : :
3919 : : /*
3920 : : * For binary upgrades, we transfer pg_largeobject_metadata via COPY or by
3921 : : * copying/linking its files from the old cluster. On such upgrades, we
3922 : : * only need to consider large objects that have comments or security
3923 : : * labels, since we still restore those objects via COMMENT/SECURITY LABEL
3924 : : * commands.
3925 : : */
3926 [ + + ]: 164 : if (dopt->binary_upgrade)
3927 : 42 : appendPQExpBufferStr(loQry,
3928 : : "WHERE oid IN "
3929 : : "(SELECT objoid FROM pg_description "
3930 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) " "
3931 : : "UNION SELECT objoid FROM pg_seclabel "
3932 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) ") ");
3933 : :
3934 : 164 : appendPQExpBufferStr(loQry,
3935 : : "ORDER BY lomowner, lomacl::pg_catalog.text, oid");
3936 : :
3937 : 164 : res = ExecuteSqlQuery(fout, loQry->data, PGRES_TUPLES_OK);
3938 : :
3939 : 164 : i_oid = PQfnumber(res, "oid");
3940 : 164 : i_lomowner = PQfnumber(res, "lomowner");
3941 : 164 : i_lomacl = PQfnumber(res, "lomacl");
3942 : 164 : i_acldefault = PQfnumber(res, "acldefault");
3943 : :
3944 : 164 : ntups = PQntuples(res);
3945 : :
3946 : : /*
3947 : : * Group the blobs into suitably-sized groups that have the same owner and
3948 : : * ACL setting, and build a metadata and a data DumpableObject for each
3949 : : * group. (If we supported initprivs for blobs, we'd have to insist that
3950 : : * groups also share initprivs settings, since the DumpableObject only has
3951 : : * room for one.) i is the index of the first tuple in the current group,
3952 : : * and n is the number of tuples we include in the group.
3953 : : */
3954 [ + + ]: 252 : for (i = 0; i < ntups; i += n)
3955 : : {
3956 : 88 : Oid thisoid = atooid(PQgetvalue(res, i, i_oid));
3957 : 88 : char *thisowner = PQgetvalue(res, i, i_lomowner);
3958 : 88 : char *thisacl = PQgetvalue(res, i, i_lomacl);
3959 : : LoInfo *loinfo;
3960 : : DumpableObject *lodata;
3961 : : char namebuf[64];
3962 : :
3963 : : /* Scan to find first tuple not to be included in group */
3964 : 88 : n = 1;
3965 [ + - + + ]: 102 : while (n < MAX_BLOBS_PER_ARCHIVE_ENTRY && i + n < ntups)
3966 : : {
3967 [ + - ]: 49 : if (strcmp(thisowner, PQgetvalue(res, i + n, i_lomowner)) != 0 ||
3968 [ + + ]: 49 : strcmp(thisacl, PQgetvalue(res, i + n, i_lomacl)) != 0)
3969 : : break;
3970 : 14 : n++;
3971 : : }
3972 : :
3973 : : /* Build the metadata DumpableObject */
3974 : 88 : loinfo = (LoInfo *) pg_malloc(offsetof(LoInfo, looids) + n * sizeof(Oid));
3975 : :
3976 : 88 : loinfo->dobj.objType = DO_LARGE_OBJECT;
3977 : 88 : loinfo->dobj.catId.tableoid = LargeObjectRelationId;
3978 : 88 : loinfo->dobj.catId.oid = thisoid;
3979 : 88 : AssignDumpId(&loinfo->dobj);
3980 : :
3981 [ + + ]: 88 : if (n > 1)
3982 : 10 : snprintf(namebuf, sizeof(namebuf), "%u..%u", thisoid,
3983 : 10 : atooid(PQgetvalue(res, i + n - 1, i_oid)));
3984 : : else
3985 : 78 : snprintf(namebuf, sizeof(namebuf), "%u", thisoid);
3986 : 88 : loinfo->dobj.name = pg_strdup(namebuf);
3987 : 88 : loinfo->dacl.acl = pg_strdup(thisacl);
3988 : 88 : loinfo->dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
3989 : 88 : loinfo->dacl.privtype = 0;
3990 : 88 : loinfo->dacl.initprivs = NULL;
3991 : 88 : loinfo->rolname = getRoleName(thisowner);
3992 : 88 : loinfo->numlos = n;
3993 : 88 : loinfo->looids[0] = thisoid;
3994 : : /* Collect OIDs of the remaining blobs in this group */
3995 [ + + ]: 102 : for (int k = 1; k < n; k++)
3996 : : {
3997 : : CatalogId extraID;
3998 : :
3999 : 14 : loinfo->looids[k] = atooid(PQgetvalue(res, i + k, i_oid));
4000 : :
4001 : : /* Make sure we can look up loinfo by any of the blobs' OIDs */
4002 : 14 : extraID.tableoid = LargeObjectRelationId;
4003 : 14 : extraID.oid = loinfo->looids[k];
4004 : 14 : recordAdditionalCatalogID(extraID, &loinfo->dobj);
4005 : : }
4006 : :
4007 : : /* LOs have data */
4008 : 88 : loinfo->dobj.components |= DUMP_COMPONENT_DATA;
4009 : :
4010 : : /* Mark whether LO group has a non-empty ACL */
4011 [ + + ]: 88 : if (!PQgetisnull(res, i, i_lomacl))
4012 : 36 : loinfo->dobj.components |= DUMP_COMPONENT_ACL;
4013 : :
4014 : : /*
4015 : : * In binary upgrade mode, pg_largeobject and pg_largeobject_metadata
4016 : : * are transferred via COPY or by copying/linking the files from the
4017 : : * old cluster. Thus, we do not need to dump LO data, definitions, or
4018 : : * ACLs.
4019 : : */
4020 [ + + ]: 88 : if (dopt->binary_upgrade)
4021 : 7 : loinfo->dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL | DUMP_COMPONENT_DEFINITION);
4022 : :
4023 : : /*
4024 : : * Create a "BLOBS" data item for the group, too. This is just a
4025 : : * placeholder for sorting; it carries no data now.
4026 : : */
4027 : 88 : lodata = pg_malloc_object(DumpableObject);
4028 : 88 : lodata->objType = DO_LARGE_OBJECT_DATA;
4029 : 88 : lodata->catId = nilCatalogId;
4030 : 88 : AssignDumpId(lodata);
4031 : 88 : lodata->name = pg_strdup(namebuf);
4032 : 88 : lodata->components |= DUMP_COMPONENT_DATA;
4033 : : /* Set up explicit dependency from data to metadata */
4034 : 88 : lodata->dependencies = pg_malloc_object(DumpId);
4035 : 88 : lodata->dependencies[0] = loinfo->dobj.dumpId;
4036 : 88 : lodata->nDeps = lodata->allocDeps = 1;
4037 : : }
4038 : :
4039 : 164 : PQclear(res);
4040 : 164 : destroyPQExpBuffer(loQry);
4041 : 164 : }
4042 : :
4043 : : /*
4044 : : * dumpLO
4045 : : *
4046 : : * dump the definition (metadata) of the given large object group
4047 : : */
4048 : : static void
4049 : 88 : dumpLO(Archive *fout, const LoInfo *loinfo)
4050 : : {
4051 : 88 : PQExpBuffer cquery = createPQExpBuffer();
4052 : :
4053 : : /*
4054 : : * The "definition" is just a newline-separated list of OIDs. We need to
4055 : : * put something into the dropStmt too, but it can just be a comment.
4056 : : */
4057 [ + + ]: 190 : for (int i = 0; i < loinfo->numlos; i++)
4058 : 102 : appendPQExpBuffer(cquery, "%u\n", loinfo->looids[i]);
4059 : :
4060 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4061 : 81 : ArchiveEntry(fout, loinfo->dobj.catId, loinfo->dobj.dumpId,
4062 : 81 : ARCHIVE_OPTS(.tag = loinfo->dobj.name,
4063 : : .owner = loinfo->rolname,
4064 : : .description = "BLOB METADATA",
4065 : : .section = SECTION_DATA,
4066 : : .createStmt = cquery->data,
4067 : : .dropStmt = "-- dummy"));
4068 : :
4069 : : /*
4070 : : * Dump per-blob comments and seclabels if any. We assume these are rare
4071 : : * enough that it's okay to generate retail TOC entries for them.
4072 : : */
4073 [ + + ]: 88 : if (loinfo->dobj.dump & (DUMP_COMPONENT_COMMENT |
4074 : : DUMP_COMPONENT_SECLABEL))
4075 : : {
4076 [ + + ]: 106 : for (int i = 0; i < loinfo->numlos; i++)
4077 : : {
4078 : : CatalogId catId;
4079 : : char namebuf[32];
4080 : :
4081 : : /* Build identifying info for this blob */
4082 : 60 : catId.tableoid = loinfo->dobj.catId.tableoid;
4083 : 60 : catId.oid = loinfo->looids[i];
4084 : 60 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[i]);
4085 : :
4086 [ + - ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4087 : 60 : dumpComment(fout, "LARGE OBJECT", namebuf,
4088 : 60 : NULL, loinfo->rolname,
4089 : 60 : catId, 0, loinfo->dobj.dumpId);
4090 : :
4091 [ + + ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4092 : 10 : dumpSecLabel(fout, "LARGE OBJECT", namebuf,
4093 : 10 : NULL, loinfo->rolname,
4094 : 10 : catId, 0, loinfo->dobj.dumpId);
4095 : : }
4096 : : }
4097 : :
4098 : : /*
4099 : : * Dump the ACLs if any (remember that all blobs in the group will have
4100 : : * the same ACL). If there's just one blob, dump a simple ACL entry; if
4101 : : * there's more, make a "LARGE OBJECTS" entry that really contains only
4102 : : * the ACL for the first blob. _printTocEntry() will be cued by the tag
4103 : : * string to emit a mutated version for each blob.
4104 : : */
4105 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_ACL)
4106 : : {
4107 : : char namebuf[32];
4108 : :
4109 : : /* Build identifying info for the first blob */
4110 : 35 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[0]);
4111 : :
4112 [ - + ]: 35 : if (loinfo->numlos > 1)
4113 : : {
4114 : : char tagbuf[64];
4115 : :
4116 : 0 : snprintf(tagbuf, sizeof(tagbuf), "LARGE OBJECTS %u..%u",
4117 : 0 : loinfo->looids[0], loinfo->looids[loinfo->numlos - 1]);
4118 : :
4119 : 0 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4120 : : "LARGE OBJECT", namebuf, NULL, NULL,
4121 : 0 : tagbuf, loinfo->rolname, &loinfo->dacl);
4122 : : }
4123 : : else
4124 : : {
4125 : 35 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4126 : : "LARGE OBJECT", namebuf, NULL, NULL,
4127 : 35 : NULL, loinfo->rolname, &loinfo->dacl);
4128 : : }
4129 : : }
4130 : :
4131 : 88 : destroyPQExpBuffer(cquery);
4132 : 88 : }
4133 : :
4134 : : /*
4135 : : * dumpLOs:
4136 : : * dump the data contents of the large objects in the given group
4137 : : */
4138 : : static int
4139 : 77 : dumpLOs(Archive *fout, const void *arg)
4140 : : {
4141 : 77 : const LoInfo *loinfo = (const LoInfo *) arg;
4142 : 77 : PGconn *conn = GetConnection(fout);
4143 : : char buf[LOBBUFSIZE];
4144 : :
4145 : 77 : pg_log_info("saving large objects \"%s\"", loinfo->dobj.name);
4146 : :
4147 [ + + ]: 162 : for (int i = 0; i < loinfo->numlos; i++)
4148 : : {
4149 : 85 : Oid loOid = loinfo->looids[i];
4150 : : int loFd;
4151 : : int cnt;
4152 : :
4153 : : /* Open the LO */
4154 : 85 : loFd = lo_open(conn, loOid, INV_READ);
4155 [ - + ]: 85 : if (loFd == -1)
4156 : 0 : pg_fatal("could not open large object %u: %s",
4157 : : loOid, PQerrorMessage(conn));
4158 : :
4159 : 85 : StartLO(fout, loOid);
4160 : :
4161 : : /* Now read it in chunks, sending data to archive */
4162 : : do
4163 : : {
4164 : 133 : cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
4165 [ - + ]: 133 : if (cnt < 0)
4166 : 0 : pg_fatal("error reading large object %u: %s",
4167 : : loOid, PQerrorMessage(conn));
4168 : :
4169 : 133 : WriteData(fout, buf, cnt);
4170 [ + + ]: 133 : } while (cnt > 0);
4171 : :
4172 : 85 : lo_close(conn, loFd);
4173 : :
4174 : 85 : EndLO(fout, loOid);
4175 : : }
4176 : :
4177 : 77 : return 1;
4178 : : }
4179 : :
4180 : : /*
4181 : : * getPolicies
4182 : : * get information about all RLS policies on dumpable tables.
4183 : : */
4184 : : void
4185 : 193 : getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
4186 : : {
4187 : 193 : DumpOptions *dopt = fout->dopt;
4188 : : PQExpBuffer query;
4189 : : PQExpBuffer tbloids;
4190 : : PGresult *res;
4191 : : PolicyInfo *polinfo;
4192 : : int i_oid;
4193 : : int i_tableoid;
4194 : : int i_polrelid;
4195 : : int i_polname;
4196 : : int i_polcmd;
4197 : : int i_polpermissive;
4198 : : int i_polroles;
4199 : : int i_polqual;
4200 : : int i_polwithcheck;
4201 : : int i,
4202 : : j,
4203 : : ntups;
4204 : :
4205 : : /* Skip if --no-policies was specified */
4206 [ + + ]: 193 : if (dopt->no_policies)
4207 : 1 : return;
4208 : :
4209 : 192 : query = createPQExpBuffer();
4210 : 192 : tbloids = createPQExpBuffer();
4211 : :
4212 : : /*
4213 : : * Identify tables of interest, and check which ones have RLS enabled.
4214 : : */
4215 : 192 : appendPQExpBufferChar(tbloids, '{');
4216 [ + + ]: 52022 : for (i = 0; i < numTables; i++)
4217 : : {
4218 : 51830 : TableInfo *tbinfo = &tblinfo[i];
4219 : :
4220 : : /* Ignore row security on tables not to be dumped */
4221 [ + + ]: 51830 : if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
4222 : 44573 : continue;
4223 : :
4224 : : /* It can't have RLS or policies if it's not a table */
4225 [ + + ]: 7257 : if (tbinfo->relkind != RELKIND_RELATION &&
4226 [ + + ]: 2037 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
4227 : 1410 : continue;
4228 : :
4229 : : /* Add it to the list of table OIDs to be probed below */
4230 [ + + ]: 5847 : if (tbloids->len > 1) /* do we have more than the '{'? */
4231 : 5722 : appendPQExpBufferChar(tbloids, ',');
4232 : 5847 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
4233 : :
4234 : : /* Is RLS enabled? (That's separate from whether it has policies) */
4235 [ + + ]: 5847 : if (tbinfo->rowsec)
4236 : : {
4237 : 56 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4238 : :
4239 : : /*
4240 : : * We represent RLS being enabled on a table by creating a
4241 : : * PolicyInfo object with null polname.
4242 : : *
4243 : : * Note: use tableoid 0 so that this object won't be mistaken for
4244 : : * something that pg_depend entries apply to.
4245 : : */
4246 : 56 : polinfo = pg_malloc_object(PolicyInfo);
4247 : 56 : polinfo->dobj.objType = DO_POLICY;
4248 : 56 : polinfo->dobj.catId.tableoid = 0;
4249 : 56 : polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
4250 : 56 : AssignDumpId(&polinfo->dobj);
4251 : 56 : polinfo->dobj.namespace = tbinfo->dobj.namespace;
4252 : 56 : polinfo->dobj.name = pg_strdup(tbinfo->dobj.name);
4253 : 56 : polinfo->poltable = tbinfo;
4254 : 56 : polinfo->polname = NULL;
4255 : 56 : polinfo->polcmd = '\0';
4256 : 56 : polinfo->polpermissive = 0;
4257 : 56 : polinfo->polroles = NULL;
4258 : 56 : polinfo->polqual = NULL;
4259 : 56 : polinfo->polwithcheck = NULL;
4260 : : }
4261 : : }
4262 : 192 : appendPQExpBufferChar(tbloids, '}');
4263 : :
4264 : : /*
4265 : : * Now, read all RLS policies belonging to the tables of interest, and
4266 : : * create PolicyInfo objects for them. (Note that we must filter the
4267 : : * results server-side not locally, because we dare not apply pg_get_expr
4268 : : * to tables we don't have lock on.)
4269 : : */
4270 : 192 : pg_log_info("reading row-level security policies");
4271 : :
4272 : 192 : printfPQExpBuffer(query,
4273 : : "SELECT pol.oid, pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
4274 : 192 : appendPQExpBufferStr(query, "pol.polpermissive, ");
4275 : 192 : appendPQExpBuffer(query,
4276 : : "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
4277 : : " 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, "
4278 : : "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
4279 : : "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
4280 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
4281 : : "JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)",
4282 : : tbloids->data);
4283 : :
4284 : 192 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4285 : :
4286 : 192 : ntups = PQntuples(res);
4287 [ + + ]: 192 : if (ntups > 0)
4288 : : {
4289 : 46 : i_oid = PQfnumber(res, "oid");
4290 : 46 : i_tableoid = PQfnumber(res, "tableoid");
4291 : 46 : i_polrelid = PQfnumber(res, "polrelid");
4292 : 46 : i_polname = PQfnumber(res, "polname");
4293 : 46 : i_polcmd = PQfnumber(res, "polcmd");
4294 : 46 : i_polpermissive = PQfnumber(res, "polpermissive");
4295 : 46 : i_polroles = PQfnumber(res, "polroles");
4296 : 46 : i_polqual = PQfnumber(res, "polqual");
4297 : 46 : i_polwithcheck = PQfnumber(res, "polwithcheck");
4298 : :
4299 : 46 : polinfo = pg_malloc_array(PolicyInfo, ntups);
4300 : :
4301 [ + + ]: 337 : for (j = 0; j < ntups; j++)
4302 : : {
4303 : 291 : Oid polrelid = atooid(PQgetvalue(res, j, i_polrelid));
4304 : 291 : TableInfo *tbinfo = findTableByOid(polrelid);
4305 : :
4306 : 291 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4307 : :
4308 : 291 : polinfo[j].dobj.objType = DO_POLICY;
4309 : 291 : polinfo[j].dobj.catId.tableoid =
4310 : 291 : atooid(PQgetvalue(res, j, i_tableoid));
4311 : 291 : polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
4312 : 291 : AssignDumpId(&polinfo[j].dobj);
4313 : 291 : polinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4314 : 291 : polinfo[j].poltable = tbinfo;
4315 : 291 : polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname));
4316 : 291 : polinfo[j].dobj.name = pg_strdup(polinfo[j].polname);
4317 : :
4318 : 291 : polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd));
4319 : 291 : polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't';
4320 : :
4321 [ + + ]: 291 : if (PQgetisnull(res, j, i_polroles))
4322 : 127 : polinfo[j].polroles = NULL;
4323 : : else
4324 : 164 : polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles));
4325 : :
4326 [ + + ]: 291 : if (PQgetisnull(res, j, i_polqual))
4327 : 41 : polinfo[j].polqual = NULL;
4328 : : else
4329 : 250 : polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual));
4330 : :
4331 [ + + ]: 291 : if (PQgetisnull(res, j, i_polwithcheck))
4332 : 153 : polinfo[j].polwithcheck = NULL;
4333 : : else
4334 : 138 : polinfo[j].polwithcheck
4335 : 138 : = pg_strdup(PQgetvalue(res, j, i_polwithcheck));
4336 : : }
4337 : : }
4338 : :
4339 : 192 : PQclear(res);
4340 : :
4341 : 192 : destroyPQExpBuffer(query);
4342 : 192 : destroyPQExpBuffer(tbloids);
4343 : : }
4344 : :
4345 : : /*
4346 : : * dumpPolicy
4347 : : * dump the definition of the given policy
4348 : : */
4349 : : static void
4350 : 347 : dumpPolicy(Archive *fout, const PolicyInfo *polinfo)
4351 : : {
4352 : 347 : DumpOptions *dopt = fout->dopt;
4353 : 347 : TableInfo *tbinfo = polinfo->poltable;
4354 : : PQExpBuffer query;
4355 : : PQExpBuffer delqry;
4356 : : PQExpBuffer polprefix;
4357 : : char *qtabname;
4358 : : const char *cmd;
4359 : : char *tag;
4360 : :
4361 : : /* Do nothing if not dumping schema */
4362 [ + + ]: 347 : if (!dopt->dumpSchema)
4363 : 56 : return;
4364 : :
4365 : : /*
4366 : : * If polname is NULL, then this record is just indicating that ROW LEVEL
4367 : : * SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE
4368 : : * ROW LEVEL SECURITY.
4369 : : */
4370 [ + + ]: 291 : if (polinfo->polname == NULL)
4371 : : {
4372 : 48 : query = createPQExpBuffer();
4373 : :
4374 : 48 : appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;",
4375 : 48 : fmtQualifiedDumpable(tbinfo));
4376 : :
4377 : : /*
4378 : : * We must emit the ROW SECURITY object's dependency on its table
4379 : : * explicitly, because it will not match anything in pg_depend (unlike
4380 : : * the case for other PolicyInfo objects).
4381 : : */
4382 [ + - ]: 48 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4383 : 48 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4384 : 48 : ARCHIVE_OPTS(.tag = polinfo->dobj.name,
4385 : : .namespace = polinfo->dobj.namespace->dobj.name,
4386 : : .owner = tbinfo->rolname,
4387 : : .description = "ROW SECURITY",
4388 : : .section = SECTION_POST_DATA,
4389 : : .createStmt = query->data,
4390 : : .deps = &(tbinfo->dobj.dumpId),
4391 : : .nDeps = 1));
4392 : :
4393 : 48 : destroyPQExpBuffer(query);
4394 : 48 : return;
4395 : : }
4396 : :
4397 [ + + ]: 243 : if (polinfo->polcmd == '*')
4398 : 81 : cmd = "";
4399 [ + + ]: 162 : else if (polinfo->polcmd == 'r')
4400 : 43 : cmd = " FOR SELECT";
4401 [ + + ]: 119 : else if (polinfo->polcmd == 'a')
4402 : 33 : cmd = " FOR INSERT";
4403 [ + + ]: 86 : else if (polinfo->polcmd == 'w')
4404 : 43 : cmd = " FOR UPDATE";
4405 [ + - ]: 43 : else if (polinfo->polcmd == 'd')
4406 : 43 : cmd = " FOR DELETE";
4407 : : else
4408 : 0 : pg_fatal("unexpected policy command type: %c",
4409 : : polinfo->polcmd);
4410 : :
4411 : 243 : query = createPQExpBuffer();
4412 : 243 : delqry = createPQExpBuffer();
4413 : 243 : polprefix = createPQExpBuffer();
4414 : :
4415 : 243 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
4416 : :
4417 : 243 : appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname));
4418 : :
4419 : 243 : appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo),
4420 [ + + ]: 243 : !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd);
4421 : :
4422 [ + + ]: 243 : if (polinfo->polroles != NULL)
4423 : 132 : appendPQExpBuffer(query, " TO %s", polinfo->polroles);
4424 : :
4425 [ + + ]: 243 : if (polinfo->polqual != NULL)
4426 : 210 : appendPQExpBuffer(query, " USING (%s)", polinfo->polqual);
4427 : :
4428 [ + + ]: 243 : if (polinfo->polwithcheck != NULL)
4429 : 114 : appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck);
4430 : :
4431 : 243 : appendPQExpBufferStr(query, ";\n");
4432 : :
4433 : 243 : appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname));
4434 : 243 : appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo));
4435 : :
4436 : 243 : appendPQExpBuffer(polprefix, "POLICY %s ON",
4437 : 243 : fmtId(polinfo->polname));
4438 : :
4439 : 243 : tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
4440 : :
4441 [ + - ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4442 : 243 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4443 : 243 : ARCHIVE_OPTS(.tag = tag,
4444 : : .namespace = polinfo->dobj.namespace->dobj.name,
4445 : : .owner = tbinfo->rolname,
4446 : : .description = "POLICY",
4447 : : .section = SECTION_POST_DATA,
4448 : : .createStmt = query->data,
4449 : : .dropStmt = delqry->data));
4450 : :
4451 [ + + ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4452 : 33 : dumpComment(fout, polprefix->data, qtabname,
4453 : 33 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
4454 : 33 : polinfo->dobj.catId, 0, polinfo->dobj.dumpId);
4455 : :
4456 : 243 : pfree(tag);
4457 : 243 : destroyPQExpBuffer(query);
4458 : 243 : destroyPQExpBuffer(delqry);
4459 : 243 : destroyPQExpBuffer(polprefix);
4460 : 243 : pg_free(qtabname);
4461 : : }
4462 : :
4463 : : /*
4464 : : * getPublications
4465 : : * get information about publications
4466 : : */
4467 : : void
4468 : 193 : getPublications(Archive *fout)
4469 : : {
4470 : 193 : DumpOptions *dopt = fout->dopt;
4471 : : PQExpBuffer query;
4472 : : PGresult *res;
4473 : : PublicationInfo *pubinfo;
4474 : : int i_tableoid;
4475 : : int i_oid;
4476 : : int i_pubname;
4477 : : int i_pubowner;
4478 : : int i_puballtables;
4479 : : int i_puballsequences;
4480 : : int i_pubinsert;
4481 : : int i_pubupdate;
4482 : : int i_pubdelete;
4483 : : int i_pubtruncate;
4484 : : int i_pubviaroot;
4485 : : int i_pubgencols;
4486 : : int i,
4487 : : ntups;
4488 : :
4489 [ - + ]: 193 : if (dopt->no_publications)
4490 : 0 : return;
4491 : :
4492 : 193 : query = createPQExpBuffer();
4493 : :
4494 : : /* Get the publications. */
4495 : 193 : appendPQExpBufferStr(query, "SELECT p.tableoid, p.oid, p.pubname, "
4496 : : "p.pubowner, p.puballtables, p.pubinsert, "
4497 : : "p.pubupdate, p.pubdelete, ");
4498 : :
4499 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
4500 : 193 : appendPQExpBufferStr(query, "p.pubtruncate, ");
4501 : : else
4502 : 0 : appendPQExpBufferStr(query, "false AS pubtruncate, ");
4503 : :
4504 [ + - ]: 193 : if (fout->remoteVersion >= 130000)
4505 : 193 : appendPQExpBufferStr(query, "p.pubviaroot, ");
4506 : : else
4507 : 0 : appendPQExpBufferStr(query, "false AS pubviaroot, ");
4508 : :
4509 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
4510 : 193 : appendPQExpBufferStr(query, "p.pubgencols, ");
4511 : : else
4512 : 0 : appendPQExpBuffer(query, "'%c' AS pubgencols, ", PUBLISH_GENCOLS_NONE);
4513 : :
4514 [ + - ]: 193 : if (fout->remoteVersion >= 190000)
4515 : 193 : appendPQExpBufferStr(query, "p.puballsequences ");
4516 : : else
4517 : 0 : appendPQExpBufferStr(query, "false AS puballsequences ");
4518 : :
4519 : 193 : appendPQExpBufferStr(query, "FROM pg_publication p");
4520 : :
4521 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4522 : :
4523 : 193 : ntups = PQntuples(res);
4524 : :
4525 [ + + ]: 193 : if (ntups == 0)
4526 : 137 : goto cleanup;
4527 : :
4528 : 56 : i_tableoid = PQfnumber(res, "tableoid");
4529 : 56 : i_oid = PQfnumber(res, "oid");
4530 : 56 : i_pubname = PQfnumber(res, "pubname");
4531 : 56 : i_pubowner = PQfnumber(res, "pubowner");
4532 : 56 : i_puballtables = PQfnumber(res, "puballtables");
4533 : 56 : i_puballsequences = PQfnumber(res, "puballsequences");
4534 : 56 : i_pubinsert = PQfnumber(res, "pubinsert");
4535 : 56 : i_pubupdate = PQfnumber(res, "pubupdate");
4536 : 56 : i_pubdelete = PQfnumber(res, "pubdelete");
4537 : 56 : i_pubtruncate = PQfnumber(res, "pubtruncate");
4538 : 56 : i_pubviaroot = PQfnumber(res, "pubviaroot");
4539 : 56 : i_pubgencols = PQfnumber(res, "pubgencols");
4540 : :
4541 : 56 : pubinfo = pg_malloc_array(PublicationInfo, ntups);
4542 : :
4543 [ + + ]: 572 : for (i = 0; i < ntups; i++)
4544 : : {
4545 : 516 : pubinfo[i].dobj.objType = DO_PUBLICATION;
4546 : 516 : pubinfo[i].dobj.catId.tableoid =
4547 : 516 : atooid(PQgetvalue(res, i, i_tableoid));
4548 : 516 : pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4549 : 516 : AssignDumpId(&pubinfo[i].dobj);
4550 : 516 : pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname));
4551 : 516 : pubinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_pubowner));
4552 : 516 : pubinfo[i].puballtables =
4553 : 516 : (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0);
4554 : 516 : pubinfo[i].puballsequences =
4555 : 516 : (strcmp(PQgetvalue(res, i, i_puballsequences), "t") == 0);
4556 : 516 : pubinfo[i].pubinsert =
4557 : 516 : (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0);
4558 : 516 : pubinfo[i].pubupdate =
4559 : 516 : (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0);
4560 : 516 : pubinfo[i].pubdelete =
4561 : 516 : (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
4562 : 516 : pubinfo[i].pubtruncate =
4563 : 516 : (strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0);
4564 : 516 : pubinfo[i].pubviaroot =
4565 : 516 : (strcmp(PQgetvalue(res, i, i_pubviaroot), "t") == 0);
4566 : 516 : pubinfo[i].pubgencols_type =
4567 : 516 : *(PQgetvalue(res, i, i_pubgencols));
4568 : 516 : pubinfo[i].except_tables = (SimplePtrList)
4569 : : {
4570 : : NULL, NULL
4571 : : };
4572 : :
4573 : : /* Decide whether we want to dump it */
4574 : 516 : selectDumpableObject(&(pubinfo[i].dobj), fout);
4575 : :
4576 : : /*
4577 : : * Get the list of tables for publications specified in the EXCEPT
4578 : : * TABLE clause.
4579 : : *
4580 : : * Although individual table entries in EXCEPT list could be stored in
4581 : : * PublicationRelInfo, dumpPublicationTable cannot be used to emit
4582 : : * them, because there is no ALTER PUBLICATION ... ADD command to add
4583 : : * individual table entries to the EXCEPT list.
4584 : : *
4585 : : * Therefore, the approach is to dump the complete EXCEPT list in a
4586 : : * single CREATE PUBLICATION statement. PublicationInfo is used to
4587 : : * collect this information, which is then emitted by
4588 : : * dumpPublication().
4589 : : */
4590 [ + - ]: 516 : if (fout->remoteVersion >= 190000)
4591 : : {
4592 : : int ntbls;
4593 : : PGresult *res_tbls;
4594 : :
4595 : 516 : resetPQExpBuffer(query);
4596 : 516 : appendPQExpBuffer(query,
4597 : : "SELECT prrelid\n"
4598 : : "FROM pg_catalog.pg_publication_rel\n"
4599 : : "WHERE prpubid = %u AND prexcept",
4600 : 516 : pubinfo[i].dobj.catId.oid);
4601 : :
4602 : 516 : res_tbls = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4603 : :
4604 : 516 : ntbls = PQntuples(res_tbls);
4605 : :
4606 [ + + ]: 756 : for (int j = 0; j < ntbls; j++)
4607 : : {
4608 : : Oid prrelid;
4609 : : TableInfo *tbinfo;
4610 : :
4611 : 240 : prrelid = atooid(PQgetvalue(res_tbls, j, 0));
4612 : :
4613 : 240 : tbinfo = findTableByOid(prrelid);
4614 : :
4615 [ + - ]: 240 : if (tbinfo != NULL)
4616 : 240 : simple_ptr_list_append(&pubinfo[i].except_tables, tbinfo);
4617 : : }
4618 : :
4619 : 516 : PQclear(res_tbls);
4620 : : }
4621 : : }
4622 : :
4623 : 56 : cleanup:
4624 : 193 : PQclear(res);
4625 : :
4626 : 193 : destroyPQExpBuffer(query);
4627 : : }
4628 : :
4629 : : /*
4630 : : * dumpPublication
4631 : : * dump the definition of the given publication
4632 : : */
4633 : : static void
4634 : 416 : dumpPublication(Archive *fout, const PublicationInfo *pubinfo)
4635 : : {
4636 : 416 : DumpOptions *dopt = fout->dopt;
4637 : : PQExpBuffer delq;
4638 : : PQExpBuffer query;
4639 : : char *qpubname;
4640 : 416 : bool first = true;
4641 : :
4642 : : /* Do nothing if not dumping schema */
4643 [ + + ]: 416 : if (!dopt->dumpSchema)
4644 : 60 : return;
4645 : :
4646 : 356 : delq = createPQExpBuffer();
4647 : 356 : query = createPQExpBuffer();
4648 : :
4649 : 356 : qpubname = pg_strdup(fmtId(pubinfo->dobj.name));
4650 : :
4651 : 356 : appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n",
4652 : : qpubname);
4653 : :
4654 : 356 : appendPQExpBuffer(query, "CREATE PUBLICATION %s",
4655 : : qpubname);
4656 : :
4657 [ + + ]: 356 : if (pubinfo->puballtables)
4658 : : {
4659 : 166 : int n_except = 0;
4660 : :
4661 : 166 : appendPQExpBufferStr(query, " FOR ALL TABLES");
4662 : :
4663 : : /* Include EXCEPT (TABLE) clause if there are except_tables. */
4664 [ + + ]: 331 : for (SimplePtrListCell *cell = pubinfo->except_tables.head; cell; cell = cell->next)
4665 : : {
4666 : 165 : TableInfo *tbinfo = (TableInfo *) cell->ptr;
4667 : :
4668 [ + + ]: 165 : if (++n_except == 1)
4669 : 99 : appendPQExpBufferStr(query, " EXCEPT (");
4670 : : else
4671 : 66 : appendPQExpBufferStr(query, ", ");
4672 : 165 : appendPQExpBuffer(query, "TABLE ONLY %s", fmtQualifiedDumpable(tbinfo));
4673 : : }
4674 [ + + ]: 166 : if (n_except > 0)
4675 : 99 : appendPQExpBufferChar(query, ')');
4676 : :
4677 [ + + ]: 166 : if (pubinfo->puballsequences)
4678 : 33 : appendPQExpBufferStr(query, ", ALL SEQUENCES");
4679 : : }
4680 [ + + ]: 190 : else if (pubinfo->puballsequences)
4681 : 33 : appendPQExpBufferStr(query, " FOR ALL SEQUENCES");
4682 : :
4683 : 356 : appendPQExpBufferStr(query, " WITH (publish = '");
4684 [ + + ]: 356 : if (pubinfo->pubinsert)
4685 : : {
4686 : 290 : appendPQExpBufferStr(query, "insert");
4687 : 290 : first = false;
4688 : : }
4689 : :
4690 [ + + ]: 356 : if (pubinfo->pubupdate)
4691 : : {
4692 [ + - ]: 290 : if (!first)
4693 : 290 : appendPQExpBufferStr(query, ", ");
4694 : :
4695 : 290 : appendPQExpBufferStr(query, "update");
4696 : 290 : first = false;
4697 : : }
4698 : :
4699 [ + + ]: 356 : if (pubinfo->pubdelete)
4700 : : {
4701 [ + - ]: 290 : if (!first)
4702 : 290 : appendPQExpBufferStr(query, ", ");
4703 : :
4704 : 290 : appendPQExpBufferStr(query, "delete");
4705 : 290 : first = false;
4706 : : }
4707 : :
4708 [ + + ]: 356 : if (pubinfo->pubtruncate)
4709 : : {
4710 [ + - ]: 290 : if (!first)
4711 : 290 : appendPQExpBufferStr(query, ", ");
4712 : :
4713 : 290 : appendPQExpBufferStr(query, "truncate");
4714 : 290 : first = false;
4715 : : }
4716 : :
4717 : 356 : appendPQExpBufferChar(query, '\'');
4718 : :
4719 [ + + ]: 356 : if (pubinfo->pubviaroot)
4720 : 5 : appendPQExpBufferStr(query, ", publish_via_partition_root = true");
4721 : :
4722 [ + + ]: 356 : if (pubinfo->pubgencols_type == PUBLISH_GENCOLS_STORED)
4723 : 33 : appendPQExpBufferStr(query, ", publish_generated_columns = stored");
4724 : :
4725 : 356 : appendPQExpBufferStr(query, ");\n");
4726 : :
4727 [ + - ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4728 : 356 : ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
4729 : 356 : ARCHIVE_OPTS(.tag = pubinfo->dobj.name,
4730 : : .owner = pubinfo->rolname,
4731 : : .description = "PUBLICATION",
4732 : : .section = SECTION_POST_DATA,
4733 : : .createStmt = query->data,
4734 : : .dropStmt = delq->data));
4735 : :
4736 [ + + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4737 : 33 : dumpComment(fout, "PUBLICATION", qpubname,
4738 : 33 : NULL, pubinfo->rolname,
4739 : 33 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4740 : :
4741 [ - + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4742 : 0 : dumpSecLabel(fout, "PUBLICATION", qpubname,
4743 : 0 : NULL, pubinfo->rolname,
4744 : 0 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4745 : :
4746 : 356 : destroyPQExpBuffer(delq);
4747 : 356 : destroyPQExpBuffer(query);
4748 : 356 : pg_free(qpubname);
4749 : : }
4750 : :
4751 : : /*
4752 : : * getPublicationNamespaces
4753 : : * get information about publication membership for dumpable schemas.
4754 : : */
4755 : : void
4756 : 193 : getPublicationNamespaces(Archive *fout)
4757 : : {
4758 : : PQExpBuffer query;
4759 : : PGresult *res;
4760 : : PublicationSchemaInfo *pubsinfo;
4761 : 193 : DumpOptions *dopt = fout->dopt;
4762 : : int i_tableoid;
4763 : : int i_oid;
4764 : : int i_pnpubid;
4765 : : int i_pnnspid;
4766 : : int i,
4767 : : j,
4768 : : ntups;
4769 : :
4770 [ + - - + ]: 193 : if (dopt->no_publications || fout->remoteVersion < 150000)
4771 : 0 : return;
4772 : :
4773 : 193 : query = createPQExpBuffer();
4774 : :
4775 : : /* Collect all publication membership info. */
4776 : 193 : appendPQExpBufferStr(query,
4777 : : "SELECT tableoid, oid, pnpubid, pnnspid "
4778 : : "FROM pg_catalog.pg_publication_namespace");
4779 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4780 : :
4781 : 193 : ntups = PQntuples(res);
4782 : :
4783 : 193 : i_tableoid = PQfnumber(res, "tableoid");
4784 : 193 : i_oid = PQfnumber(res, "oid");
4785 : 193 : i_pnpubid = PQfnumber(res, "pnpubid");
4786 : 193 : i_pnnspid = PQfnumber(res, "pnnspid");
4787 : :
4788 : : /* this allocation may be more than we need */
4789 : 193 : pubsinfo = pg_malloc_array(PublicationSchemaInfo, ntups);
4790 : 193 : j = 0;
4791 : :
4792 [ + + ]: 324 : for (i = 0; i < ntups; i++)
4793 : : {
4794 : 131 : Oid pnpubid = atooid(PQgetvalue(res, i, i_pnpubid));
4795 : 131 : Oid pnnspid = atooid(PQgetvalue(res, i, i_pnnspid));
4796 : : PublicationInfo *pubinfo;
4797 : : NamespaceInfo *nspinfo;
4798 : :
4799 : : /*
4800 : : * Ignore any entries for which we aren't interested in either the
4801 : : * publication or the rel.
4802 : : */
4803 : 131 : pubinfo = findPublicationByOid(pnpubid);
4804 [ - + ]: 131 : if (pubinfo == NULL)
4805 : 0 : continue;
4806 : 131 : nspinfo = findNamespaceByOid(pnnspid);
4807 [ - + ]: 131 : if (nspinfo == NULL)
4808 : 0 : continue;
4809 : :
4810 : : /* OK, make a DumpableObject for this relationship */
4811 : 131 : pubsinfo[j].dobj.objType = DO_PUBLICATION_TABLE_IN_SCHEMA;
4812 : 131 : pubsinfo[j].dobj.catId.tableoid =
4813 : 131 : atooid(PQgetvalue(res, i, i_tableoid));
4814 : 131 : pubsinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4815 : 131 : AssignDumpId(&pubsinfo[j].dobj);
4816 : 131 : pubsinfo[j].dobj.namespace = nspinfo->dobj.namespace;
4817 : 131 : pubsinfo[j].dobj.name = nspinfo->dobj.name;
4818 : 131 : pubsinfo[j].publication = pubinfo;
4819 : 131 : pubsinfo[j].pubschema = nspinfo;
4820 : :
4821 : : /* Decide whether we want to dump it */
4822 : 131 : selectDumpablePublicationObject(&(pubsinfo[j].dobj), fout);
4823 : :
4824 : 131 : j++;
4825 : : }
4826 : :
4827 : 193 : PQclear(res);
4828 : 193 : destroyPQExpBuffer(query);
4829 : : }
4830 : :
4831 : : /*
4832 : : * getPublicationTables
4833 : : * get information about publication membership for dumpable tables.
4834 : : */
4835 : : void
4836 : 193 : getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
4837 : : {
4838 : : PQExpBuffer query;
4839 : : PGresult *res;
4840 : : PublicationRelInfo *pubrinfo;
4841 : 193 : DumpOptions *dopt = fout->dopt;
4842 : : int i_tableoid;
4843 : : int i_oid;
4844 : : int i_prpubid;
4845 : : int i_prrelid;
4846 : : int i_prrelqual;
4847 : : int i_prattrs;
4848 : : int i,
4849 : : j,
4850 : : ntups;
4851 : :
4852 [ - + ]: 193 : if (dopt->no_publications)
4853 : 0 : return;
4854 : :
4855 : 193 : query = createPQExpBuffer();
4856 : :
4857 : : /* Collect all publication membership info. */
4858 [ + - ]: 193 : if (fout->remoteVersion >= 150000)
4859 : : {
4860 : 193 : appendPQExpBufferStr(query,
4861 : : "SELECT tableoid, oid, prpubid, prrelid, "
4862 : : "pg_catalog.pg_get_expr(prqual, prrelid) AS prrelqual, "
4863 : : "(CASE\n"
4864 : : " WHEN pr.prattrs IS NOT NULL THEN\n"
4865 : : " (SELECT array_agg(attname)\n"
4866 : : " FROM\n"
4867 : : " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
4868 : : " pg_catalog.pg_attribute\n"
4869 : : " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
4870 : : " ELSE NULL END) prattrs "
4871 : : "FROM pg_catalog.pg_publication_rel pr");
4872 [ + - ]: 193 : if (fout->remoteVersion >= 190000)
4873 : 193 : appendPQExpBufferStr(query, " WHERE NOT pr.prexcept");
4874 : : }
4875 : : else
4876 : 0 : appendPQExpBufferStr(query,
4877 : : "SELECT tableoid, oid, prpubid, prrelid, "
4878 : : "NULL AS prrelqual, NULL AS prattrs "
4879 : : "FROM pg_catalog.pg_publication_rel");
4880 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4881 : :
4882 : 193 : ntups = PQntuples(res);
4883 : :
4884 : 193 : i_tableoid = PQfnumber(res, "tableoid");
4885 : 193 : i_oid = PQfnumber(res, "oid");
4886 : 193 : i_prpubid = PQfnumber(res, "prpubid");
4887 : 193 : i_prrelid = PQfnumber(res, "prrelid");
4888 : 193 : i_prrelqual = PQfnumber(res, "prrelqual");
4889 : 193 : i_prattrs = PQfnumber(res, "prattrs");
4890 : :
4891 : : /* this allocation may be more than we need */
4892 : 193 : pubrinfo = pg_malloc_array(PublicationRelInfo, ntups);
4893 : 193 : j = 0;
4894 : :
4895 [ + + ]: 564 : for (i = 0; i < ntups; i++)
4896 : : {
4897 : 371 : Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid));
4898 : 371 : Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid));
4899 : : PublicationInfo *pubinfo;
4900 : : TableInfo *tbinfo;
4901 : :
4902 : : /*
4903 : : * Ignore any entries for which we aren't interested in either the
4904 : : * publication or the rel.
4905 : : */
4906 : 371 : pubinfo = findPublicationByOid(prpubid);
4907 [ - + ]: 371 : if (pubinfo == NULL)
4908 : 0 : continue;
4909 : 371 : tbinfo = findTableByOid(prrelid);
4910 [ - + ]: 371 : if (tbinfo == NULL)
4911 : 0 : continue;
4912 : :
4913 : : /* OK, make a DumpableObject for this relationship */
4914 : 371 : pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
4915 : 371 : pubrinfo[j].dobj.catId.tableoid =
4916 : 371 : atooid(PQgetvalue(res, i, i_tableoid));
4917 : 371 : pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4918 : 371 : AssignDumpId(&pubrinfo[j].dobj);
4919 : 371 : pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4920 : 371 : pubrinfo[j].dobj.name = tbinfo->dobj.name;
4921 : 371 : pubrinfo[j].publication = pubinfo;
4922 : 371 : pubrinfo[j].pubtable = tbinfo;
4923 [ + + ]: 371 : if (PQgetisnull(res, i, i_prrelqual))
4924 : 206 : pubrinfo[j].pubrelqual = NULL;
4925 : : else
4926 : 165 : pubrinfo[j].pubrelqual = pg_strdup(PQgetvalue(res, i, i_prrelqual));
4927 : :
4928 [ + + ]: 371 : if (!PQgetisnull(res, i, i_prattrs))
4929 : : {
4930 : : char **attnames;
4931 : : int nattnames;
4932 : : PQExpBuffer attribs;
4933 : :
4934 [ - + ]: 117 : if (!parsePGArray(PQgetvalue(res, i, i_prattrs),
4935 : : &attnames, &nattnames))
4936 : 0 : pg_fatal("could not parse %s array", "prattrs");
4937 : 117 : attribs = createPQExpBuffer();
4938 [ + + ]: 337 : for (int k = 0; k < nattnames; k++)
4939 : : {
4940 [ + + ]: 220 : if (k > 0)
4941 : 103 : appendPQExpBufferStr(attribs, ", ");
4942 : :
4943 : 220 : appendPQExpBufferStr(attribs, fmtId(attnames[k]));
4944 : : }
4945 : 117 : pubrinfo[j].pubrattrs = attribs->data;
4946 : 117 : free(attribs); /* but not attribs->data */
4947 : 117 : free(attnames);
4948 : : }
4949 : : else
4950 : 254 : pubrinfo[j].pubrattrs = NULL;
4951 : :
4952 : : /* Decide whether we want to dump it */
4953 : 371 : selectDumpablePublicationObject(&(pubrinfo[j].dobj), fout);
4954 : :
4955 : 371 : j++;
4956 : : }
4957 : :
4958 : 193 : PQclear(res);
4959 : 193 : destroyPQExpBuffer(query);
4960 : : }
4961 : :
4962 : : /*
4963 : : * dumpPublicationNamespace
4964 : : * dump the definition of the given publication schema mapping.
4965 : : */
4966 : : static void
4967 : 103 : dumpPublicationNamespace(Archive *fout, const PublicationSchemaInfo *pubsinfo)
4968 : : {
4969 : 103 : DumpOptions *dopt = fout->dopt;
4970 : 103 : NamespaceInfo *schemainfo = pubsinfo->pubschema;
4971 : 103 : PublicationInfo *pubinfo = pubsinfo->publication;
4972 : : PQExpBuffer query;
4973 : : char *tag;
4974 : :
4975 : : /* Do nothing if not dumping schema */
4976 [ + + ]: 103 : if (!dopt->dumpSchema)
4977 : 12 : return;
4978 : :
4979 : 91 : tag = psprintf("%s %s", pubinfo->dobj.name, schemainfo->dobj.name);
4980 : :
4981 : 91 : query = createPQExpBuffer();
4982 : :
4983 : 91 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ", fmtId(pubinfo->dobj.name));
4984 : 91 : appendPQExpBuffer(query, "ADD TABLES IN SCHEMA %s;\n", fmtId(schemainfo->dobj.name));
4985 : :
4986 : : /*
4987 : : * There is no point in creating drop query as the drop is done by schema
4988 : : * drop.
4989 : : */
4990 [ + - ]: 91 : if (pubsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4991 : 91 : ArchiveEntry(fout, pubsinfo->dobj.catId, pubsinfo->dobj.dumpId,
4992 : 91 : ARCHIVE_OPTS(.tag = tag,
4993 : : .namespace = schemainfo->dobj.name,
4994 : : .owner = pubinfo->rolname,
4995 : : .description = "PUBLICATION TABLES IN SCHEMA",
4996 : : .section = SECTION_POST_DATA,
4997 : : .createStmt = query->data));
4998 : :
4999 : : /* These objects can't currently have comments or seclabels */
5000 : :
5001 : 91 : pfree(tag);
5002 : 91 : destroyPQExpBuffer(query);
5003 : : }
5004 : :
5005 : : /*
5006 : : * dumpPublicationTable
5007 : : * dump the definition of the given publication table mapping
5008 : : */
5009 : : static void
5010 : 298 : dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo)
5011 : : {
5012 : 298 : DumpOptions *dopt = fout->dopt;
5013 : 298 : PublicationInfo *pubinfo = pubrinfo->publication;
5014 : 298 : TableInfo *tbinfo = pubrinfo->pubtable;
5015 : : PQExpBuffer query;
5016 : : char *tag;
5017 : :
5018 : : /* Do nothing if not dumping schema */
5019 [ + + ]: 298 : if (!dopt->dumpSchema)
5020 : 42 : return;
5021 : :
5022 : 256 : tag = psprintf("%s %s", pubinfo->dobj.name, tbinfo->dobj.name);
5023 : :
5024 : 256 : query = createPQExpBuffer();
5025 : :
5026 : 256 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
5027 : 256 : fmtId(pubinfo->dobj.name));
5028 : 256 : appendPQExpBuffer(query, " %s",
5029 : 256 : fmtQualifiedDumpable(tbinfo));
5030 : :
5031 [ + + ]: 256 : if (pubrinfo->pubrattrs)
5032 : 81 : appendPQExpBuffer(query, " (%s)", pubrinfo->pubrattrs);
5033 : :
5034 [ + + ]: 256 : if (pubrinfo->pubrelqual)
5035 : : {
5036 : : /*
5037 : : * It's necessary to add parentheses around the expression because
5038 : : * pg_get_expr won't supply the parentheses for things like WHERE
5039 : : * TRUE.
5040 : : */
5041 : 114 : appendPQExpBuffer(query, " WHERE (%s)", pubrinfo->pubrelqual);
5042 : : }
5043 : 256 : appendPQExpBufferStr(query, ";\n");
5044 : :
5045 : : /*
5046 : : * There is no point in creating a drop query as the drop is done by table
5047 : : * drop. (If you think to change this, see also _printTocEntry().)
5048 : : * Although this object doesn't really have ownership as such, set the
5049 : : * owner field anyway to ensure that the command is run by the correct
5050 : : * role at restore time.
5051 : : */
5052 [ + - ]: 256 : if (pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5053 : 256 : ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
5054 : 256 : ARCHIVE_OPTS(.tag = tag,
5055 : : .namespace = tbinfo->dobj.namespace->dobj.name,
5056 : : .owner = pubinfo->rolname,
5057 : : .description = "PUBLICATION TABLE",
5058 : : .section = SECTION_POST_DATA,
5059 : : .createStmt = query->data));
5060 : :
5061 : : /* These objects can't currently have comments or seclabels */
5062 : :
5063 : 256 : pfree(tag);
5064 : 256 : destroyPQExpBuffer(query);
5065 : : }
5066 : :
5067 : : /*
5068 : : * Is the currently connected user a superuser?
5069 : : */
5070 : : static bool
5071 : 192 : is_superuser(Archive *fout)
5072 : : {
5073 : 192 : ArchiveHandle *AH = (ArchiveHandle *) fout;
5074 : : const char *val;
5075 : :
5076 : 192 : val = PQparameterStatus(AH->connection, "is_superuser");
5077 : :
5078 [ + - + + ]: 192 : if (val && strcmp(val, "on") == 0)
5079 : 189 : return true;
5080 : :
5081 : 3 : return false;
5082 : : }
5083 : :
5084 : : /*
5085 : : * Set the given value to restrict_nonsystem_relation_kind value. Since
5086 : : * restrict_nonsystem_relation_kind is introduced in minor version releases,
5087 : : * the setting query is effective only where available.
5088 : : */
5089 : : static void
5090 : 227 : set_restrict_relation_kind(Archive *AH, const char *value)
5091 : : {
5092 : 227 : PQExpBuffer query = createPQExpBuffer();
5093 : : PGresult *res;
5094 : :
5095 : 227 : appendPQExpBuffer(query,
5096 : : "SELECT set_config(name, '%s', false) "
5097 : : "FROM pg_settings "
5098 : : "WHERE name = 'restrict_nonsystem_relation_kind'",
5099 : : value);
5100 : 227 : res = ExecuteSqlQuery(AH, query->data, PGRES_TUPLES_OK);
5101 : :
5102 : 227 : PQclear(res);
5103 : 227 : destroyPQExpBuffer(query);
5104 : 227 : }
5105 : :
5106 : : /*
5107 : : * getSubscriptions
5108 : : * get information about subscriptions
5109 : : */
5110 : : void
5111 : 193 : getSubscriptions(Archive *fout)
5112 : : {
5113 : 193 : DumpOptions *dopt = fout->dopt;
5114 : : PQExpBuffer query;
5115 : : PGresult *res;
5116 : : SubscriptionInfo *subinfo;
5117 : : int i_tableoid;
5118 : : int i_oid;
5119 : : int i_subname;
5120 : : int i_subowner;
5121 : : int i_subbinary;
5122 : : int i_substream;
5123 : : int i_subtwophasestate;
5124 : : int i_subdisableonerr;
5125 : : int i_subpasswordrequired;
5126 : : int i_subrunasowner;
5127 : : int i_subservername;
5128 : : int i_subconninfo;
5129 : : int i_subslotname;
5130 : : int i_subsynccommit;
5131 : : int i_subwalrcvtimeout;
5132 : : int i_subpublications;
5133 : : int i_suborigin;
5134 : : int i_suboriginremotelsn;
5135 : : int i_subenabled;
5136 : : int i_subfailover;
5137 : : int i_subretaindeadtuples;
5138 : : int i_submaxretention;
5139 : : int i,
5140 : : ntups;
5141 : :
5142 [ + + ]: 193 : if (dopt->no_subscriptions)
5143 : 1 : return;
5144 : :
5145 [ + + ]: 192 : if (!is_superuser(fout))
5146 : : {
5147 : : int n;
5148 : :
5149 : 3 : res = ExecuteSqlQuery(fout,
5150 : : "SELECT count(*) FROM pg_subscription "
5151 : : "WHERE subdbid = (SELECT oid FROM pg_database"
5152 : : " WHERE datname = current_database())",
5153 : : PGRES_TUPLES_OK);
5154 : 3 : n = atoi(PQgetvalue(res, 0, 0));
5155 [ + + ]: 3 : if (n > 0)
5156 : 2 : pg_log_warning("subscriptions not dumped because current user is not a superuser");
5157 : 3 : PQclear(res);
5158 : 3 : return;
5159 : : }
5160 : :
5161 : 189 : query = createPQExpBuffer();
5162 : :
5163 : : /* Get the subscriptions in current database. */
5164 : 189 : appendPQExpBufferStr(query,
5165 : : "SELECT s.tableoid, s.oid, s.subname,\n"
5166 : : " s.subowner,\n"
5167 : : " s.subconninfo, s.subslotname, s.subsynccommit,\n"
5168 : : " s.subpublications,\n");
5169 : :
5170 [ + - ]: 189 : if (fout->remoteVersion >= 140000)
5171 : 189 : appendPQExpBufferStr(query, " s.subbinary,\n");
5172 : : else
5173 : 0 : appendPQExpBufferStr(query, " false AS subbinary,\n");
5174 : :
5175 [ + - ]: 189 : if (fout->remoteVersion >= 140000)
5176 : 189 : appendPQExpBufferStr(query, " s.substream,\n");
5177 : : else
5178 : 0 : appendPQExpBufferStr(query, " 'f' AS substream,\n");
5179 : :
5180 [ + - ]: 189 : if (fout->remoteVersion >= 150000)
5181 : 189 : appendPQExpBufferStr(query,
5182 : : " s.subtwophasestate,\n"
5183 : : " s.subdisableonerr,\n");
5184 : : else
5185 : 0 : appendPQExpBuffer(query,
5186 : : " '%c' AS subtwophasestate,\n"
5187 : : " false AS subdisableonerr,\n",
5188 : : LOGICALREP_TWOPHASE_STATE_DISABLED);
5189 : :
5190 [ + - ]: 189 : if (fout->remoteVersion >= 160000)
5191 : 189 : appendPQExpBufferStr(query,
5192 : : " s.subpasswordrequired,\n"
5193 : : " s.subrunasowner,\n"
5194 : : " s.suborigin,\n");
5195 : : else
5196 : 0 : appendPQExpBuffer(query,
5197 : : " 't' AS subpasswordrequired,\n"
5198 : : " 't' AS subrunasowner,\n"
5199 : : " '%s' AS suborigin,\n",
5200 : : LOGICALREP_ORIGIN_ANY);
5201 : :
5202 [ + + + - ]: 189 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5203 : 42 : appendPQExpBufferStr(query, " o.remote_lsn AS suboriginremotelsn,\n"
5204 : : " s.subenabled,\n");
5205 : : else
5206 : 147 : appendPQExpBufferStr(query, " NULL AS suboriginremotelsn,\n"
5207 : : " false AS subenabled,\n");
5208 : :
5209 [ + - ]: 189 : if (fout->remoteVersion >= 170000)
5210 : 189 : appendPQExpBufferStr(query,
5211 : : " s.subfailover,\n");
5212 : : else
5213 : 0 : appendPQExpBufferStr(query,
5214 : : " false AS subfailover,\n");
5215 : :
5216 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5217 : 189 : appendPQExpBufferStr(query,
5218 : : " s.subretaindeadtuples,\n");
5219 : : else
5220 : 0 : appendPQExpBufferStr(query,
5221 : : " false AS subretaindeadtuples,\n");
5222 : :
5223 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5224 : 189 : appendPQExpBufferStr(query,
5225 : : " s.submaxretention,\n");
5226 : : else
5227 : 0 : appendPQExpBufferStr(query, " 0 AS submaxretention,\n");
5228 : :
5229 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5230 : 189 : appendPQExpBufferStr(query,
5231 : : " s.subwalrcvtimeout,\n");
5232 : : else
5233 : 0 : appendPQExpBufferStr(query,
5234 : : " '-1' AS subwalrcvtimeout,\n");
5235 : :
5236 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5237 : 189 : appendPQExpBufferStr(query, " fs.srvname AS subservername\n");
5238 : : else
5239 : 0 : appendPQExpBufferStr(query, " NULL AS subservername\n");
5240 : :
5241 : 189 : appendPQExpBufferStr(query,
5242 : : "FROM pg_subscription s\n");
5243 : :
5244 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5245 : 189 : appendPQExpBufferStr(query,
5246 : : "LEFT JOIN pg_catalog.pg_foreign_server fs \n"
5247 : : " ON fs.oid = s.subserver \n");
5248 : :
5249 [ + + + - ]: 189 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5250 : 42 : appendPQExpBufferStr(query,
5251 : : "LEFT JOIN pg_catalog.pg_replication_origin_status o \n"
5252 : : " ON o.external_id = 'pg_' || s.oid::text \n");
5253 : :
5254 : 189 : appendPQExpBufferStr(query,
5255 : : "WHERE s.subdbid = (SELECT oid FROM pg_database\n"
5256 : : " WHERE datname = current_database())");
5257 : :
5258 : 189 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5259 : :
5260 : 189 : ntups = PQntuples(res);
5261 : :
5262 : : /*
5263 : : * Get subscription fields. We don't include subskiplsn in the dump as
5264 : : * after restoring the dump this value may no longer be relevant.
5265 : : */
5266 : 189 : i_tableoid = PQfnumber(res, "tableoid");
5267 : 189 : i_oid = PQfnumber(res, "oid");
5268 : 189 : i_subname = PQfnumber(res, "subname");
5269 : 189 : i_subowner = PQfnumber(res, "subowner");
5270 : 189 : i_subenabled = PQfnumber(res, "subenabled");
5271 : 189 : i_subbinary = PQfnumber(res, "subbinary");
5272 : 189 : i_substream = PQfnumber(res, "substream");
5273 : 189 : i_subtwophasestate = PQfnumber(res, "subtwophasestate");
5274 : 189 : i_subdisableonerr = PQfnumber(res, "subdisableonerr");
5275 : 189 : i_subpasswordrequired = PQfnumber(res, "subpasswordrequired");
5276 : 189 : i_subrunasowner = PQfnumber(res, "subrunasowner");
5277 : 189 : i_subfailover = PQfnumber(res, "subfailover");
5278 : 189 : i_subretaindeadtuples = PQfnumber(res, "subretaindeadtuples");
5279 : 189 : i_submaxretention = PQfnumber(res, "submaxretention");
5280 : 189 : i_subservername = PQfnumber(res, "subservername");
5281 : 189 : i_subconninfo = PQfnumber(res, "subconninfo");
5282 : 189 : i_subslotname = PQfnumber(res, "subslotname");
5283 : 189 : i_subsynccommit = PQfnumber(res, "subsynccommit");
5284 : 189 : i_subwalrcvtimeout = PQfnumber(res, "subwalrcvtimeout");
5285 : 189 : i_subpublications = PQfnumber(res, "subpublications");
5286 : 189 : i_suborigin = PQfnumber(res, "suborigin");
5287 : 189 : i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn");
5288 : :
5289 : 189 : subinfo = pg_malloc_array(SubscriptionInfo, ntups);
5290 : :
5291 [ + + ]: 326 : for (i = 0; i < ntups; i++)
5292 : : {
5293 : 137 : subinfo[i].dobj.objType = DO_SUBSCRIPTION;
5294 : 137 : subinfo[i].dobj.catId.tableoid =
5295 : 137 : atooid(PQgetvalue(res, i, i_tableoid));
5296 : 137 : subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5297 : 137 : AssignDumpId(&subinfo[i].dobj);
5298 : 137 : subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
5299 : 137 : subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner));
5300 : :
5301 : 137 : subinfo[i].subenabled =
5302 : 137 : (strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0);
5303 [ + - ]: 137 : if (PQgetisnull(res, i, i_subservername))
5304 : 137 : subinfo[i].subservername = NULL;
5305 : : else
5306 : 0 : subinfo[i].subservername = pg_strdup(PQgetvalue(res, i, i_subservername));
5307 : 137 : subinfo[i].subbinary =
5308 : 137 : (strcmp(PQgetvalue(res, i, i_subbinary), "t") == 0);
5309 : 137 : subinfo[i].substream = *(PQgetvalue(res, i, i_substream));
5310 : 137 : subinfo[i].subtwophasestate = *(PQgetvalue(res, i, i_subtwophasestate));
5311 : 137 : subinfo[i].subdisableonerr =
5312 : 137 : (strcmp(PQgetvalue(res, i, i_subdisableonerr), "t") == 0);
5313 : 137 : subinfo[i].subpasswordrequired =
5314 : 137 : (strcmp(PQgetvalue(res, i, i_subpasswordrequired), "t") == 0);
5315 : 137 : subinfo[i].subrunasowner =
5316 : 137 : (strcmp(PQgetvalue(res, i, i_subrunasowner), "t") == 0);
5317 : 137 : subinfo[i].subfailover =
5318 : 137 : (strcmp(PQgetvalue(res, i, i_subfailover), "t") == 0);
5319 : 137 : subinfo[i].subretaindeadtuples =
5320 : 137 : (strcmp(PQgetvalue(res, i, i_subretaindeadtuples), "t") == 0);
5321 : 137 : subinfo[i].submaxretention =
5322 : 137 : atoi(PQgetvalue(res, i, i_submaxretention));
5323 [ - + ]: 137 : if (PQgetisnull(res, i, i_subconninfo))
5324 : 0 : subinfo[i].subconninfo = NULL;
5325 : : else
5326 : 137 : subinfo[i].subconninfo =
5327 : 137 : pg_strdup(PQgetvalue(res, i, i_subconninfo));
5328 [ - + ]: 137 : if (PQgetisnull(res, i, i_subslotname))
5329 : 0 : subinfo[i].subslotname = NULL;
5330 : : else
5331 : 137 : subinfo[i].subslotname =
5332 : 137 : pg_strdup(PQgetvalue(res, i, i_subslotname));
5333 : 274 : subinfo[i].subsynccommit =
5334 : 137 : pg_strdup(PQgetvalue(res, i, i_subsynccommit));
5335 : 274 : subinfo[i].subwalrcvtimeout =
5336 : 137 : pg_strdup(PQgetvalue(res, i, i_subwalrcvtimeout));
5337 : 274 : subinfo[i].subpublications =
5338 : 137 : pg_strdup(PQgetvalue(res, i, i_subpublications));
5339 : 137 : subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin));
5340 [ + + ]: 137 : if (PQgetisnull(res, i, i_suboriginremotelsn))
5341 : 136 : subinfo[i].suboriginremotelsn = NULL;
5342 : : else
5343 : 1 : subinfo[i].suboriginremotelsn =
5344 : 1 : pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn));
5345 : :
5346 : : /* Decide whether we want to dump it */
5347 : 137 : selectDumpableObject(&(subinfo[i].dobj), fout);
5348 : : }
5349 : 189 : PQclear(res);
5350 : :
5351 : 189 : destroyPQExpBuffer(query);
5352 : : }
5353 : :
5354 : : /*
5355 : : * getSubscriptionRelations
5356 : : * Get information about subscription membership for dumpable relations. This
5357 : : * will be used only in binary-upgrade mode for PG17 or later versions.
5358 : : */
5359 : : void
5360 : 193 : getSubscriptionRelations(Archive *fout)
5361 : : {
5362 : 193 : DumpOptions *dopt = fout->dopt;
5363 : 193 : SubscriptionInfo *subinfo = NULL;
5364 : : SubRelInfo *subrinfo;
5365 : : PGresult *res;
5366 : : int i_srsubid;
5367 : : int i_srrelid;
5368 : : int i_srsubstate;
5369 : : int i_srsublsn;
5370 : : int ntups;
5371 : 193 : Oid last_srsubid = InvalidOid;
5372 : :
5373 [ + + + + ]: 193 : if (dopt->no_subscriptions || !dopt->binary_upgrade ||
5374 [ - + ]: 42 : fout->remoteVersion < 170000)
5375 : 151 : return;
5376 : :
5377 : 42 : res = ExecuteSqlQuery(fout,
5378 : : "SELECT srsubid, srrelid, srsubstate, srsublsn "
5379 : : "FROM pg_catalog.pg_subscription_rel "
5380 : : "ORDER BY srsubid",
5381 : : PGRES_TUPLES_OK);
5382 : 42 : ntups = PQntuples(res);
5383 [ + + ]: 42 : if (ntups == 0)
5384 : 41 : goto cleanup;
5385 : :
5386 : : /* Get pg_subscription_rel attributes */
5387 : 1 : i_srsubid = PQfnumber(res, "srsubid");
5388 : 1 : i_srrelid = PQfnumber(res, "srrelid");
5389 : 1 : i_srsubstate = PQfnumber(res, "srsubstate");
5390 : 1 : i_srsublsn = PQfnumber(res, "srsublsn");
5391 : :
5392 : 1 : subrinfo = pg_malloc_array(SubRelInfo, ntups);
5393 [ + + ]: 4 : for (int i = 0; i < ntups; i++)
5394 : : {
5395 : 3 : Oid cur_srsubid = atooid(PQgetvalue(res, i, i_srsubid));
5396 : 3 : Oid relid = atooid(PQgetvalue(res, i, i_srrelid));
5397 : : TableInfo *tblinfo;
5398 : :
5399 : : /*
5400 : : * If we switched to a new subscription, check if the subscription
5401 : : * exists.
5402 : : */
5403 [ + + ]: 3 : if (cur_srsubid != last_srsubid)
5404 : : {
5405 : 2 : subinfo = findSubscriptionByOid(cur_srsubid);
5406 [ - + ]: 2 : if (subinfo == NULL)
5407 : 0 : pg_fatal("subscription with OID %u does not exist", cur_srsubid);
5408 : :
5409 : 2 : last_srsubid = cur_srsubid;
5410 : : }
5411 : :
5412 : 3 : tblinfo = findTableByOid(relid);
5413 [ - + ]: 3 : if (tblinfo == NULL)
5414 : 0 : pg_fatal("failed sanity check, relation with OID %u not found",
5415 : : relid);
5416 : :
5417 : : /* OK, make a DumpableObject for this relationship */
5418 : 3 : subrinfo[i].dobj.objType = DO_SUBSCRIPTION_REL;
5419 : 3 : subrinfo[i].dobj.catId.tableoid = relid;
5420 : 3 : subrinfo[i].dobj.catId.oid = cur_srsubid;
5421 : 3 : AssignDumpId(&subrinfo[i].dobj);
5422 : 3 : subrinfo[i].dobj.namespace = tblinfo->dobj.namespace;
5423 : 3 : subrinfo[i].dobj.name = tblinfo->dobj.name;
5424 : 3 : subrinfo[i].subinfo = subinfo;
5425 : 3 : subrinfo[i].tblinfo = tblinfo;
5426 : 3 : subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0];
5427 [ + + ]: 3 : if (PQgetisnull(res, i, i_srsublsn))
5428 : 1 : subrinfo[i].srsublsn = NULL;
5429 : : else
5430 : 2 : subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn));
5431 : :
5432 : : /* Decide whether we want to dump it */
5433 : 3 : selectDumpableObject(&(subrinfo[i].dobj), fout);
5434 : : }
5435 : :
5436 : 1 : cleanup:
5437 : 42 : PQclear(res);
5438 : : }
5439 : :
5440 : : /*
5441 : : * dumpSubscriptionTable
5442 : : * Dump the definition of the given subscription table mapping. This will be
5443 : : * used only in binary-upgrade mode for PG17 or later versions.
5444 : : */
5445 : : static void
5446 : 3 : dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
5447 : : {
5448 : 3 : DumpOptions *dopt = fout->dopt;
5449 : 3 : SubscriptionInfo *subinfo = subrinfo->subinfo;
5450 : : PQExpBuffer query;
5451 : : char *tag;
5452 : :
5453 : : /* Do nothing if not dumping schema */
5454 [ - + ]: 3 : if (!dopt->dumpSchema)
5455 : 0 : return;
5456 : :
5457 : : Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000);
5458 : :
5459 : 3 : tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->tblinfo->dobj.name);
5460 : :
5461 : 3 : query = createPQExpBuffer();
5462 : :
5463 [ + - ]: 3 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5464 : : {
5465 : : /*
5466 : : * binary_upgrade_add_sub_rel_state will add the subscription relation
5467 : : * to pg_subscription_rel table. This will be used only in
5468 : : * binary-upgrade mode.
5469 : : */
5470 : 3 : appendPQExpBufferStr(query,
5471 : : "\n-- For binary upgrade, must preserve the subscriber table.\n");
5472 : 3 : appendPQExpBufferStr(query,
5473 : : "SELECT pg_catalog.binary_upgrade_add_sub_rel_state(");
5474 : 3 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5475 : 3 : appendPQExpBuffer(query,
5476 : : ", %u, '%c'",
5477 : 3 : subrinfo->tblinfo->dobj.catId.oid,
5478 : 3 : subrinfo->srsubstate);
5479 : :
5480 [ + + + - ]: 3 : if (subrinfo->srsublsn && subrinfo->srsublsn[0] != '\0')
5481 : 2 : appendPQExpBuffer(query, ", '%s'", subrinfo->srsublsn);
5482 : : else
5483 : 1 : appendPQExpBufferStr(query, ", NULL");
5484 : :
5485 : 3 : appendPQExpBufferStr(query, ");\n");
5486 : : }
5487 : :
5488 : : /*
5489 : : * There is no point in creating a drop query as the drop is done by table
5490 : : * drop. (If you think to change this, see also _printTocEntry().)
5491 : : * Although this object doesn't really have ownership as such, set the
5492 : : * owner field anyway to ensure that the command is run by the correct
5493 : : * role at restore time.
5494 : : */
5495 [ + - ]: 3 : if (subrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5496 : 3 : ArchiveEntry(fout, subrinfo->dobj.catId, subrinfo->dobj.dumpId,
5497 : 3 : ARCHIVE_OPTS(.tag = tag,
5498 : : .namespace = subrinfo->tblinfo->dobj.namespace->dobj.name,
5499 : : .owner = subinfo->rolname,
5500 : : .description = "SUBSCRIPTION TABLE",
5501 : : .section = SECTION_POST_DATA,
5502 : : .createStmt = query->data));
5503 : :
5504 : : /* These objects can't currently have comments or seclabels */
5505 : :
5506 : 3 : pfree(tag);
5507 : 3 : destroyPQExpBuffer(query);
5508 : : }
5509 : :
5510 : : /*
5511 : : * dumpSubscription
5512 : : * dump the definition of the given subscription
5513 : : */
5514 : : static void
5515 : 116 : dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
5516 : : {
5517 : 116 : DumpOptions *dopt = fout->dopt;
5518 : : PQExpBuffer delq;
5519 : : PQExpBuffer query;
5520 : : PQExpBuffer publications;
5521 : : char *qsubname;
5522 : 116 : char **pubnames = NULL;
5523 : 116 : int npubnames = 0;
5524 : : int i;
5525 : :
5526 : : /* Do nothing if not dumping schema */
5527 [ + + ]: 116 : if (!dopt->dumpSchema)
5528 : 18 : return;
5529 : :
5530 : 98 : delq = createPQExpBuffer();
5531 : 98 : query = createPQExpBuffer();
5532 : :
5533 : 98 : qsubname = pg_strdup(fmtId(subinfo->dobj.name));
5534 : :
5535 : 98 : appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n",
5536 : : qsubname);
5537 : :
5538 : 98 : appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s ",
5539 : : qsubname);
5540 [ - + ]: 98 : if (subinfo->subservername)
5541 : : {
5542 : 0 : appendPQExpBuffer(query, "SERVER %s", fmtId(subinfo->subservername));
5543 : : }
5544 : : else
5545 : : {
5546 : 98 : appendPQExpBufferStr(query, "CONNECTION ");
5547 : 98 : appendStringLiteralAH(query, subinfo->subconninfo, fout);
5548 : : }
5549 : :
5550 : : /* Build list of quoted publications and append them to query. */
5551 [ - + ]: 98 : if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames))
5552 : 0 : pg_fatal("could not parse %s array", "subpublications");
5553 : :
5554 : 98 : publications = createPQExpBuffer();
5555 [ + + ]: 196 : for (i = 0; i < npubnames; i++)
5556 : : {
5557 [ - + ]: 98 : if (i > 0)
5558 : 0 : appendPQExpBufferStr(publications, ", ");
5559 : :
5560 : 98 : appendPQExpBufferStr(publications, fmtId(pubnames[i]));
5561 : : }
5562 : :
5563 : 98 : appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data);
5564 [ + - ]: 98 : if (subinfo->subslotname)
5565 : 98 : appendStringLiteralAH(query, subinfo->subslotname, fout);
5566 : : else
5567 : 0 : appendPQExpBufferStr(query, "NONE");
5568 : :
5569 [ - + ]: 98 : if (subinfo->subbinary)
5570 : 0 : appendPQExpBufferStr(query, ", binary = true");
5571 : :
5572 [ + + ]: 98 : if (subinfo->substream == LOGICALREP_STREAM_ON)
5573 : 32 : appendPQExpBufferStr(query, ", streaming = on");
5574 [ + + ]: 66 : else if (subinfo->substream == LOGICALREP_STREAM_PARALLEL)
5575 : 34 : appendPQExpBufferStr(query, ", streaming = parallel");
5576 : : else
5577 : 32 : appendPQExpBufferStr(query, ", streaming = off");
5578 : :
5579 [ - + ]: 98 : if (subinfo->subtwophasestate != LOGICALREP_TWOPHASE_STATE_DISABLED)
5580 : 0 : appendPQExpBufferStr(query, ", two_phase = on");
5581 : :
5582 [ - + ]: 98 : if (subinfo->subdisableonerr)
5583 : 0 : appendPQExpBufferStr(query, ", disable_on_error = true");
5584 : :
5585 [ - + ]: 98 : if (!subinfo->subpasswordrequired)
5586 : 0 : appendPQExpBufferStr(query, ", password_required = false");
5587 : :
5588 [ - + ]: 98 : if (subinfo->subrunasowner)
5589 : 0 : appendPQExpBufferStr(query, ", run_as_owner = true");
5590 : :
5591 [ + + ]: 98 : if (subinfo->subfailover)
5592 : 1 : appendPQExpBufferStr(query, ", failover = true");
5593 : :
5594 [ + + ]: 98 : if (subinfo->subretaindeadtuples)
5595 : 1 : appendPQExpBufferStr(query, ", retain_dead_tuples = true");
5596 : :
5597 [ - + ]: 98 : if (subinfo->submaxretention)
5598 : 0 : appendPQExpBuffer(query, ", max_retention_duration = %d", subinfo->submaxretention);
5599 : :
5600 [ - + ]: 98 : if (strcmp(subinfo->subsynccommit, "off") != 0)
5601 : 0 : appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));
5602 : :
5603 [ - + ]: 98 : if (strcmp(subinfo->subwalrcvtimeout, "-1") != 0)
5604 : 0 : appendPQExpBuffer(query, ", wal_receiver_timeout = %s", fmtId(subinfo->subwalrcvtimeout));
5605 : :
5606 [ + + ]: 98 : if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0)
5607 : 32 : appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin);
5608 : :
5609 : 98 : appendPQExpBufferStr(query, ");\n");
5610 : :
5611 : : /*
5612 : : * In binary-upgrade mode, we allow the replication to continue after the
5613 : : * upgrade.
5614 : : */
5615 [ + + + - ]: 98 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5616 : : {
5617 [ + + ]: 5 : if (subinfo->suboriginremotelsn)
5618 : : {
5619 : : /*
5620 : : * Preserve the remote_lsn for the subscriber's replication
5621 : : * origin. This value is required to start the replication from
5622 : : * the position before the upgrade. This value will be stale if
5623 : : * the publisher gets upgraded before the subscriber node.
5624 : : * However, this shouldn't be a problem as the upgrade of the
5625 : : * publisher ensures that all the transactions were replicated
5626 : : * before upgrading it.
5627 : : */
5628 : 1 : appendPQExpBufferStr(query,
5629 : : "\n-- For binary upgrade, must preserve the remote_lsn for the subscriber's replication origin.\n");
5630 : 1 : appendPQExpBufferStr(query,
5631 : : "SELECT pg_catalog.binary_upgrade_replorigin_advance(");
5632 : 1 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5633 : 1 : appendPQExpBuffer(query, ", '%s');\n", subinfo->suboriginremotelsn);
5634 : : }
5635 : :
5636 [ + + ]: 5 : if (subinfo->subenabled)
5637 : : {
5638 : : /*
5639 : : * Enable the subscription to allow the replication to continue
5640 : : * after the upgrade.
5641 : : */
5642 : 1 : appendPQExpBufferStr(query,
5643 : : "\n-- For binary upgrade, must preserve the subscriber's running state.\n");
5644 : 1 : appendPQExpBuffer(query, "ALTER SUBSCRIPTION %s ENABLE;\n", qsubname);
5645 : : }
5646 : : }
5647 : :
5648 [ + - ]: 98 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5649 : 98 : ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
5650 : 98 : ARCHIVE_OPTS(.tag = subinfo->dobj.name,
5651 : : .owner = subinfo->rolname,
5652 : : .description = "SUBSCRIPTION",
5653 : : .section = SECTION_POST_DATA,
5654 : : .createStmt = query->data,
5655 : : .dropStmt = delq->data));
5656 : :
5657 [ + + ]: 98 : if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
5658 : 32 : dumpComment(fout, "SUBSCRIPTION", qsubname,
5659 : 32 : NULL, subinfo->rolname,
5660 : 32 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5661 : :
5662 [ - + ]: 98 : if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
5663 : 0 : dumpSecLabel(fout, "SUBSCRIPTION", qsubname,
5664 : 0 : NULL, subinfo->rolname,
5665 : 0 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5666 : :
5667 : 98 : destroyPQExpBuffer(publications);
5668 : 98 : free(pubnames);
5669 : :
5670 : 98 : destroyPQExpBuffer(delq);
5671 : 98 : destroyPQExpBuffer(query);
5672 : 98 : pg_free(qsubname);
5673 : : }
5674 : :
5675 : : /*
5676 : : * Given a "create query", append as many ALTER ... DEPENDS ON EXTENSION as
5677 : : * the object needs.
5678 : : */
5679 : : static void
5680 : 5263 : append_depends_on_extension(Archive *fout,
5681 : : PQExpBuffer create,
5682 : : const DumpableObject *dobj,
5683 : : const char *catalog,
5684 : : const char *keyword,
5685 : : const char *objname)
5686 : : {
5687 [ + + ]: 5263 : if (dobj->depends_on_ext)
5688 : : {
5689 : : char *nm;
5690 : : PGresult *res;
5691 : : PQExpBuffer query;
5692 : : int ntups;
5693 : : int i_extname;
5694 : : int i;
5695 : :
5696 : : /* dodge fmtId() non-reentrancy */
5697 : 42 : nm = pg_strdup(objname);
5698 : :
5699 : 42 : query = createPQExpBuffer();
5700 : 42 : appendPQExpBuffer(query,
5701 : : "SELECT e.extname "
5702 : : "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
5703 : : "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
5704 : : "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
5705 : : "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
5706 : : catalog,
5707 : 42 : dobj->catId.oid);
5708 : 42 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5709 : 42 : ntups = PQntuples(res);
5710 : 42 : i_extname = PQfnumber(res, "extname");
5711 [ + + ]: 84 : for (i = 0; i < ntups; i++)
5712 : : {
5713 : 42 : appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
5714 : : keyword, nm,
5715 : 42 : fmtId(PQgetvalue(res, i, i_extname)));
5716 : : }
5717 : :
5718 : 42 : PQclear(res);
5719 : 42 : destroyPQExpBuffer(query);
5720 : 42 : pg_free(nm);
5721 : : }
5722 : 5263 : }
5723 : :
5724 : : static Oid
5725 : 0 : get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
5726 : : {
5727 : : /*
5728 : : * If the old version didn't assign an array type, but the new version
5729 : : * does, we must select an unused type OID to assign. This currently only
5730 : : * happens for domains, when upgrading pre-v11 to v11 and up.
5731 : : *
5732 : : * Note: local state here is kind of ugly, but we must have some, since we
5733 : : * mustn't choose the same unused OID more than once.
5734 : : */
5735 : : static Oid next_possible_free_oid = FirstNormalObjectId;
5736 : : PGresult *res;
5737 : : bool is_dup;
5738 : :
5739 : : do
5740 : : {
5741 : 0 : ++next_possible_free_oid;
5742 : 0 : printfPQExpBuffer(upgrade_query,
5743 : : "SELECT EXISTS(SELECT 1 "
5744 : : "FROM pg_catalog.pg_type "
5745 : : "WHERE oid = '%u'::pg_catalog.oid);",
5746 : : next_possible_free_oid);
5747 : 0 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5748 : 0 : is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
5749 : 0 : PQclear(res);
5750 [ # # ]: 0 : } while (is_dup);
5751 : :
5752 : 0 : return next_possible_free_oid;
5753 : : }
5754 : :
5755 : : static void
5756 : 970 : binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
5757 : : PQExpBuffer upgrade_buffer,
5758 : : Oid pg_type_oid,
5759 : : bool force_array_type,
5760 : : bool include_multirange_type)
5761 : : {
5762 : 970 : PQExpBuffer upgrade_query = createPQExpBuffer();
5763 : : PGresult *res;
5764 : : Oid pg_type_array_oid;
5765 : : Oid pg_type_multirange_oid;
5766 : : Oid pg_type_multirange_array_oid;
5767 : : TypeInfo *tinfo;
5768 : :
5769 : 970 : appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
5770 : 970 : appendPQExpBuffer(upgrade_buffer,
5771 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5772 : : pg_type_oid);
5773 : :
5774 : 970 : tinfo = findTypeByOid(pg_type_oid);
5775 [ + - ]: 970 : if (tinfo)
5776 : 970 : pg_type_array_oid = tinfo->typarray;
5777 : : else
5778 : 0 : pg_type_array_oid = InvalidOid;
5779 : :
5780 [ + + - + ]: 970 : if (!OidIsValid(pg_type_array_oid) && force_array_type)
5781 : 0 : pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5782 : :
5783 [ + + ]: 970 : if (OidIsValid(pg_type_array_oid))
5784 : : {
5785 : 968 : appendPQExpBufferStr(upgrade_buffer,
5786 : : "\n-- For binary upgrade, must preserve pg_type array oid\n");
5787 : 968 : appendPQExpBuffer(upgrade_buffer,
5788 : : "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5789 : : pg_type_array_oid);
5790 : : }
5791 : :
5792 : : /*
5793 : : * Pre-set the multirange type oid and its own array type oid.
5794 : : */
5795 [ + + ]: 970 : if (include_multirange_type)
5796 : : {
5797 [ + - ]: 9 : if (fout->remoteVersion >= 140000)
5798 : : {
5799 : 9 : printfPQExpBuffer(upgrade_query,
5800 : : "SELECT t.oid, t.typarray "
5801 : : "FROM pg_catalog.pg_type t "
5802 : : "JOIN pg_catalog.pg_range r "
5803 : : "ON t.oid = r.rngmultitypid "
5804 : : "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
5805 : : pg_type_oid);
5806 : :
5807 : 9 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5808 : :
5809 : 9 : pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
5810 : 9 : pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5811 : :
5812 : 9 : PQclear(res);
5813 : : }
5814 : : else
5815 : : {
5816 : 0 : pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5817 : 0 : pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5818 : : }
5819 : :
5820 : 9 : appendPQExpBufferStr(upgrade_buffer,
5821 : : "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
5822 : 9 : appendPQExpBuffer(upgrade_buffer,
5823 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5824 : : pg_type_multirange_oid);
5825 : 9 : appendPQExpBufferStr(upgrade_buffer,
5826 : : "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
5827 : 9 : appendPQExpBuffer(upgrade_buffer,
5828 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5829 : : pg_type_multirange_array_oid);
5830 : : }
5831 : :
5832 : 970 : destroyPQExpBuffer(upgrade_query);
5833 : 970 : }
5834 : :
5835 : : static void
5836 : 892 : binary_upgrade_set_type_oids_by_rel(Archive *fout,
5837 : : PQExpBuffer upgrade_buffer,
5838 : : const TableInfo *tbinfo)
5839 : : {
5840 : 892 : Oid pg_type_oid = tbinfo->reltype;
5841 : :
5842 [ + - ]: 892 : if (OidIsValid(pg_type_oid))
5843 : 892 : binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5844 : : pg_type_oid, false, false);
5845 : 892 : }
5846 : :
5847 : : /*
5848 : : * bsearch() comparator for BinaryUpgradeClassOidItem
5849 : : */
5850 : : static int
5851 : 12894 : BinaryUpgradeClassOidItemCmp(const void *p1, const void *p2)
5852 : : {
5853 : 12894 : BinaryUpgradeClassOidItem v1 = *((const BinaryUpgradeClassOidItem *) p1);
5854 : 12894 : BinaryUpgradeClassOidItem v2 = *((const BinaryUpgradeClassOidItem *) p2);
5855 : :
5856 : 12894 : return pg_cmp_u32(v1.oid, v2.oid);
5857 : : }
5858 : :
5859 : : /*
5860 : : * collectBinaryUpgradeClassOids
5861 : : *
5862 : : * Construct a table of pg_class information required for
5863 : : * binary_upgrade_set_pg_class_oids(). The table is sorted by OID for speed in
5864 : : * lookup.
5865 : : */
5866 : : static void
5867 : 42 : collectBinaryUpgradeClassOids(Archive *fout)
5868 : : {
5869 : : PGresult *res;
5870 : : const char *query;
5871 : :
5872 : 42 : query = "SELECT c.oid, c.relkind, c.relfilenode, c.reltoastrelid, "
5873 : : "ct.relfilenode, i.indexrelid, cti.relfilenode, "
5874 : : "(SELECT a.atttypid FROM pg_attribute AS a "
5875 : : " WHERE a.attrelid = c.reltoastrelid AND attname = 'chunk_id'::text) "
5876 : : " AS toastchunktypid "
5877 : : "FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index i "
5878 : : "ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5879 : : "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5880 : : "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5881 : : "ORDER BY c.oid;";
5882 : :
5883 : 42 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
5884 : :
5885 : 42 : nbinaryUpgradeClassOids = PQntuples(res);
5886 : 42 : binaryUpgradeClassOids =
5887 : 42 : pg_malloc_array(BinaryUpgradeClassOidItem, nbinaryUpgradeClassOids);
5888 : :
5889 [ + + ]: 19678 : for (int i = 0; i < nbinaryUpgradeClassOids; i++)
5890 : : {
5891 : 19636 : binaryUpgradeClassOids[i].oid = atooid(PQgetvalue(res, i, 0));
5892 : 19636 : binaryUpgradeClassOids[i].relkind = *PQgetvalue(res, i, 1);
5893 : 19636 : binaryUpgradeClassOids[i].relfilenumber = atooid(PQgetvalue(res, i, 2));
5894 : 19636 : binaryUpgradeClassOids[i].toast_oid = atooid(PQgetvalue(res, i, 3));
5895 : 19636 : binaryUpgradeClassOids[i].toast_relfilenumber = atooid(PQgetvalue(res, i, 4));
5896 : 19636 : binaryUpgradeClassOids[i].toast_index_oid = atooid(PQgetvalue(res, i, 5));
5897 : 19636 : binaryUpgradeClassOids[i].toast_index_relfilenumber = atooid(PQgetvalue(res, i, 6));
5898 : 19636 : binaryUpgradeClassOids[i].toast_chunk_id_typoid = atooid(PQgetvalue(res, i, 7));
5899 : : }
5900 : :
5901 : 42 : PQclear(res);
5902 : 42 : }
5903 : :
5904 : : static void
5905 : 1300 : binary_upgrade_set_pg_class_oids(Archive *fout,
5906 : : PQExpBuffer upgrade_buffer, Oid pg_class_oid)
5907 : : {
5908 : 1300 : BinaryUpgradeClassOidItem key = {0};
5909 : : BinaryUpgradeClassOidItem *entry;
5910 : :
5911 : : Assert(binaryUpgradeClassOids);
5912 : :
5913 : : /*
5914 : : * Preserve the OID and relfilenumber of the table, table's index, table's
5915 : : * toast table, toast table's chunk type and toast table's index if any.
5916 : : *
5917 : : * One complexity is that the current table definition might not require
5918 : : * the creation of a TOAST table, but the old database might have a TOAST
5919 : : * table that was created earlier, before some wide columns were dropped.
5920 : : * By setting the TOAST oid we force creation of the TOAST heap and index
5921 : : * by the new backend, so we can copy the files during binary upgrade
5922 : : * without worrying about this case.
5923 : : */
5924 : 1300 : key.oid = pg_class_oid;
5925 : 1300 : entry = bsearch(&key, binaryUpgradeClassOids, nbinaryUpgradeClassOids,
5926 : : sizeof(BinaryUpgradeClassOidItem),
5927 : : BinaryUpgradeClassOidItemCmp);
5928 : :
5929 : 1300 : appendPQExpBufferStr(upgrade_buffer,
5930 : : "\n-- For binary upgrade, must preserve pg_class oids, toast chunk type oids and relfilenodes\n");
5931 : :
5932 [ + + ]: 1300 : if (entry->relkind != RELKIND_INDEX &&
5933 [ + + ]: 1007 : entry->relkind != RELKIND_PARTITIONED_INDEX)
5934 : : {
5935 : 976 : appendPQExpBuffer(upgrade_buffer,
5936 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5937 : : pg_class_oid);
5938 : :
5939 : : /*
5940 : : * Not every relation has storage. Also, in a pre-v12 database,
5941 : : * partitioned tables have a relfilenumber, which should not be
5942 : : * preserved when upgrading.
5943 : : */
5944 [ + + ]: 976 : if (RelFileNumberIsValid(entry->relfilenumber) &&
5945 [ + - ]: 808 : entry->relkind != RELKIND_PARTITIONED_TABLE)
5946 : 808 : appendPQExpBuffer(upgrade_buffer,
5947 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
5948 : : entry->relfilenumber);
5949 : :
5950 : : /*
5951 : : * In a pre-v12 database, partitioned tables might be marked as having
5952 : : * toast tables, but we should ignore them if so.
5953 : : */
5954 [ + + ]: 976 : if (OidIsValid(entry->toast_oid) &&
5955 [ + - ]: 285 : entry->relkind != RELKIND_PARTITIONED_TABLE)
5956 : : {
5957 : 285 : appendPQExpBuffer(upgrade_buffer,
5958 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
5959 : : entry->toast_oid);
5960 : 285 : appendPQExpBuffer(upgrade_buffer,
5961 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
5962 : : entry->toast_relfilenumber);
5963 : 285 : appendPQExpBuffer(upgrade_buffer,
5964 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_chunk_id_typoid('%u'::pg_catalog.oid);\n",
5965 : : entry->toast_chunk_id_typoid);
5966 : :
5967 : : /* every toast table has an index */
5968 : 285 : appendPQExpBuffer(upgrade_buffer,
5969 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5970 : : entry->toast_index_oid);
5971 : 285 : appendPQExpBuffer(upgrade_buffer,
5972 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5973 : : entry->toast_index_relfilenumber);
5974 : : }
5975 : : }
5976 : : else
5977 : : {
5978 : : /* Preserve the OID and relfilenumber of the index */
5979 : 324 : appendPQExpBuffer(upgrade_buffer,
5980 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5981 : : pg_class_oid);
5982 : 324 : appendPQExpBuffer(upgrade_buffer,
5983 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5984 : : entry->relfilenumber);
5985 : : }
5986 : :
5987 : 1300 : appendPQExpBufferChar(upgrade_buffer, '\n');
5988 : 1300 : }
5989 : :
5990 : : /*
5991 : : * If the DumpableObject is a member of an extension, add a suitable
5992 : : * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
5993 : : *
5994 : : * For somewhat historical reasons, objname should already be quoted,
5995 : : * but not objnamespace (if any).
5996 : : */
5997 : : static void
5998 : 1550 : binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
5999 : : const DumpableObject *dobj,
6000 : : const char *objtype,
6001 : : const char *objname,
6002 : : const char *objnamespace)
6003 : : {
6004 : 1550 : DumpableObject *extobj = NULL;
6005 : : int i;
6006 : :
6007 [ + + ]: 1550 : if (!dobj->ext_member)
6008 : 1528 : return;
6009 : :
6010 : : /*
6011 : : * Find the parent extension. We could avoid this search if we wanted to
6012 : : * add a link field to DumpableObject, but the space costs of that would
6013 : : * be considerable. We assume that member objects could only have a
6014 : : * direct dependency on their own extension, not any others.
6015 : : */
6016 [ + - ]: 22 : for (i = 0; i < dobj->nDeps; i++)
6017 : : {
6018 : 22 : extobj = findObjectByDumpId(dobj->dependencies[i]);
6019 [ + - + - ]: 22 : if (extobj && extobj->objType == DO_EXTENSION)
6020 : 22 : break;
6021 : 0 : extobj = NULL;
6022 : : }
6023 [ - + ]: 22 : if (extobj == NULL)
6024 : 0 : pg_fatal("could not find parent extension for %s %s",
6025 : : objtype, objname);
6026 : :
6027 : 22 : appendPQExpBufferStr(upgrade_buffer,
6028 : : "\n-- For binary upgrade, handle extension membership the hard way\n");
6029 : 22 : appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
6030 : 22 : fmtId(extobj->name),
6031 : : objtype);
6032 [ + + + - ]: 22 : if (objnamespace && *objnamespace)
6033 : 19 : appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
6034 : 22 : appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
6035 : : }
6036 : :
6037 : : /*
6038 : : * getNamespaces:
6039 : : * get information about all namespaces in the system catalogs
6040 : : */
6041 : : void
6042 : 194 : getNamespaces(Archive *fout)
6043 : : {
6044 : : PGresult *res;
6045 : : int ntups;
6046 : : int i;
6047 : : PQExpBuffer query;
6048 : : NamespaceInfo *nsinfo;
6049 : : int i_tableoid;
6050 : : int i_oid;
6051 : : int i_nspname;
6052 : : int i_nspowner;
6053 : : int i_nspacl;
6054 : : int i_acldefault;
6055 : :
6056 : 194 : query = createPQExpBuffer();
6057 : :
6058 : : /*
6059 : : * we fetch all namespaces including system ones, so that every object we
6060 : : * read in can be linked to a containing namespace.
6061 : : */
6062 : 194 : appendPQExpBufferStr(query, "SELECT n.tableoid, n.oid, n.nspname, "
6063 : : "n.nspowner, "
6064 : : "n.nspacl, "
6065 : : "acldefault('n', n.nspowner) AS acldefault "
6066 : : "FROM pg_namespace n");
6067 : :
6068 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6069 : :
6070 : 194 : ntups = PQntuples(res);
6071 : :
6072 : 194 : nsinfo = pg_malloc_array(NamespaceInfo, ntups);
6073 : :
6074 : 194 : i_tableoid = PQfnumber(res, "tableoid");
6075 : 194 : i_oid = PQfnumber(res, "oid");
6076 : 194 : i_nspname = PQfnumber(res, "nspname");
6077 : 194 : i_nspowner = PQfnumber(res, "nspowner");
6078 : 194 : i_nspacl = PQfnumber(res, "nspacl");
6079 : 194 : i_acldefault = PQfnumber(res, "acldefault");
6080 : :
6081 [ + + ]: 1872 : for (i = 0; i < ntups; i++)
6082 : : {
6083 : : const char *nspowner;
6084 : :
6085 : 1678 : nsinfo[i].dobj.objType = DO_NAMESPACE;
6086 : 1678 : nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6087 : 1678 : nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6088 : 1678 : AssignDumpId(&nsinfo[i].dobj);
6089 : 1678 : nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
6090 : 1678 : nsinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_nspacl));
6091 : 1678 : nsinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6092 : 1678 : nsinfo[i].dacl.privtype = 0;
6093 : 1678 : nsinfo[i].dacl.initprivs = NULL;
6094 : 1678 : nspowner = PQgetvalue(res, i, i_nspowner);
6095 : 1678 : nsinfo[i].nspowner = atooid(nspowner);
6096 : 1678 : nsinfo[i].rolname = getRoleName(nspowner);
6097 : :
6098 : : /* Decide whether to dump this namespace */
6099 : 1678 : selectDumpableNamespace(&nsinfo[i], fout);
6100 : :
6101 : : /* Mark whether namespace has an ACL */
6102 [ + + ]: 1678 : if (!PQgetisnull(res, i, i_nspacl))
6103 : 841 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6104 : :
6105 : : /*
6106 : : * We ignore any pg_init_privs.initprivs entry for the public schema
6107 : : * and assume a predetermined default, for several reasons. First,
6108 : : * dropping and recreating the schema removes its pg_init_privs entry,
6109 : : * but an empty destination database starts with this ACL nonetheless.
6110 : : * Second, we support dump/reload of public schema ownership changes.
6111 : : * ALTER SCHEMA OWNER filters nspacl through aclnewowner(), but
6112 : : * initprivs continues to reflect the initial owner. Hence,
6113 : : * synthesize the value that nspacl will have after the restore's
6114 : : * ALTER SCHEMA OWNER. Third, this makes the destination database
6115 : : * match the source's ACL, even if the latter was an initdb-default
6116 : : * ACL, which changed in v15. An upgrade pulls in changes to most
6117 : : * system object ACLs that the DBA had not customized. We've made the
6118 : : * public schema depart from that, because changing its ACL so easily
6119 : : * breaks applications.
6120 : : */
6121 [ + + ]: 1678 : if (strcmp(nsinfo[i].dobj.name, "public") == 0)
6122 : : {
6123 : 190 : PQExpBuffer aclarray = createPQExpBuffer();
6124 : 190 : PQExpBuffer aclitem = createPQExpBuffer();
6125 : :
6126 : : /* Standard ACL as of v15 is {owner=UC/owner,=U/owner} */
6127 : 190 : appendPQExpBufferChar(aclarray, '{');
6128 : 190 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6129 : 190 : appendPQExpBufferStr(aclitem, "=UC/");
6130 : 190 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6131 : 190 : appendPGArray(aclarray, aclitem->data);
6132 : 190 : resetPQExpBuffer(aclitem);
6133 : 190 : appendPQExpBufferStr(aclitem, "=U/");
6134 : 190 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6135 : 190 : appendPGArray(aclarray, aclitem->data);
6136 : 190 : appendPQExpBufferChar(aclarray, '}');
6137 : :
6138 : 190 : nsinfo[i].dacl.privtype = 'i';
6139 : 190 : nsinfo[i].dacl.initprivs = pstrdup(aclarray->data);
6140 : 190 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6141 : :
6142 : 190 : destroyPQExpBuffer(aclarray);
6143 : 190 : destroyPQExpBuffer(aclitem);
6144 : : }
6145 : : }
6146 : :
6147 : 194 : PQclear(res);
6148 : 194 : destroyPQExpBuffer(query);
6149 : 194 : }
6150 : :
6151 : : /*
6152 : : * findNamespace:
6153 : : * given a namespace OID, look up the info read by getNamespaces
6154 : : */
6155 : : static NamespaceInfo *
6156 : 622546 : findNamespace(Oid nsoid)
6157 : : {
6158 : : NamespaceInfo *nsinfo;
6159 : :
6160 : 622546 : nsinfo = findNamespaceByOid(nsoid);
6161 [ - + ]: 622546 : if (nsinfo == NULL)
6162 : 0 : pg_fatal("schema with OID %u does not exist", nsoid);
6163 : 622546 : return nsinfo;
6164 : : }
6165 : :
6166 : : /*
6167 : : * getExtensions:
6168 : : * read all extensions in the system catalogs and return them in the
6169 : : * ExtensionInfo* structure
6170 : : *
6171 : : * numExtensions is set to the number of extensions read in
6172 : : */
6173 : : ExtensionInfo *
6174 : 194 : getExtensions(Archive *fout, int *numExtensions)
6175 : : {
6176 : 194 : DumpOptions *dopt = fout->dopt;
6177 : : PGresult *res;
6178 : : int ntups;
6179 : : int i;
6180 : : PQExpBuffer query;
6181 : 194 : ExtensionInfo *extinfo = NULL;
6182 : : int i_tableoid;
6183 : : int i_oid;
6184 : : int i_extname;
6185 : : int i_nspname;
6186 : : int i_extrelocatable;
6187 : : int i_extversion;
6188 : : int i_extconfig;
6189 : : int i_extcondition;
6190 : :
6191 : 194 : query = createPQExpBuffer();
6192 : :
6193 : 194 : appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, "
6194 : : "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
6195 : : "FROM pg_extension x "
6196 : : "JOIN pg_namespace n ON n.oid = x.extnamespace");
6197 : :
6198 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6199 : :
6200 : 194 : ntups = PQntuples(res);
6201 [ - + ]: 194 : if (ntups == 0)
6202 : 0 : goto cleanup;
6203 : :
6204 : 194 : extinfo = pg_malloc_array(ExtensionInfo, ntups);
6205 : :
6206 : 194 : i_tableoid = PQfnumber(res, "tableoid");
6207 : 194 : i_oid = PQfnumber(res, "oid");
6208 : 194 : i_extname = PQfnumber(res, "extname");
6209 : 194 : i_nspname = PQfnumber(res, "nspname");
6210 : 194 : i_extrelocatable = PQfnumber(res, "extrelocatable");
6211 : 194 : i_extversion = PQfnumber(res, "extversion");
6212 : 194 : i_extconfig = PQfnumber(res, "extconfig");
6213 : 194 : i_extcondition = PQfnumber(res, "extcondition");
6214 : :
6215 [ + + ]: 419 : for (i = 0; i < ntups; i++)
6216 : : {
6217 : 225 : extinfo[i].dobj.objType = DO_EXTENSION;
6218 : 225 : extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6219 : 225 : extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6220 : 225 : AssignDumpId(&extinfo[i].dobj);
6221 : 225 : extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
6222 : 225 : extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
6223 : 225 : extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
6224 : 225 : extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
6225 : 225 : extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
6226 : 225 : extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
6227 : :
6228 : : /* Decide whether we want to dump it */
6229 : 225 : selectDumpableExtension(&(extinfo[i]), dopt);
6230 : : }
6231 : :
6232 : 194 : cleanup:
6233 : 194 : PQclear(res);
6234 : 194 : destroyPQExpBuffer(query);
6235 : :
6236 : 194 : *numExtensions = ntups;
6237 : :
6238 : 194 : return extinfo;
6239 : : }
6240 : :
6241 : : /*
6242 : : * getTypes:
6243 : : * get information about all types in the system catalogs
6244 : : *
6245 : : * NB: this must run after getFuncs() because we assume we can do
6246 : : * findFuncByOid().
6247 : : */
6248 : : void
6249 : 193 : getTypes(Archive *fout)
6250 : : {
6251 : : PGresult *res;
6252 : : int ntups;
6253 : : int i;
6254 : 193 : PQExpBuffer query = createPQExpBuffer();
6255 : : TypeInfo *tyinfo;
6256 : : ShellTypeInfo *stinfo;
6257 : : int i_tableoid;
6258 : : int i_oid;
6259 : : int i_typname;
6260 : : int i_typnamespace;
6261 : : int i_typacl;
6262 : : int i_acldefault;
6263 : : int i_typowner;
6264 : : int i_typelem;
6265 : : int i_typrelid;
6266 : : int i_typrelkind;
6267 : : int i_typtype;
6268 : : int i_typisdefined;
6269 : : int i_isarray;
6270 : : int i_typarray;
6271 : :
6272 : : /*
6273 : : * we include even the built-in types because those may be used as array
6274 : : * elements by user-defined types
6275 : : *
6276 : : * we filter out the built-in types when we dump out the types
6277 : : *
6278 : : * same approach for undefined (shell) types and array types
6279 : : *
6280 : : * Note: as of 8.3 we can reliably detect whether a type is an
6281 : : * auto-generated array type by checking the element type's typarray.
6282 : : * (Before that the test is capable of generating false positives.) We
6283 : : * still check for name beginning with '_', though, so as to avoid the
6284 : : * cost of the subselect probe for all standard types. This would have to
6285 : : * be revisited if the backend ever allows renaming of array types.
6286 : : */
6287 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, typname, "
6288 : : "typnamespace, typacl, "
6289 : : "acldefault('T', typowner) AS acldefault, "
6290 : : "typowner, "
6291 : : "typelem, typrelid, typarray, "
6292 : : "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
6293 : : "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
6294 : : "typtype, typisdefined, "
6295 : : "typname[0] = '_' AND typelem != 0 AND "
6296 : : "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
6297 : : "FROM pg_type");
6298 : :
6299 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6300 : :
6301 : 193 : ntups = PQntuples(res);
6302 : :
6303 : 193 : tyinfo = pg_malloc_array(TypeInfo, ntups);
6304 : :
6305 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6306 : 193 : i_oid = PQfnumber(res, "oid");
6307 : 193 : i_typname = PQfnumber(res, "typname");
6308 : 193 : i_typnamespace = PQfnumber(res, "typnamespace");
6309 : 193 : i_typacl = PQfnumber(res, "typacl");
6310 : 193 : i_acldefault = PQfnumber(res, "acldefault");
6311 : 193 : i_typowner = PQfnumber(res, "typowner");
6312 : 193 : i_typelem = PQfnumber(res, "typelem");
6313 : 193 : i_typrelid = PQfnumber(res, "typrelid");
6314 : 193 : i_typrelkind = PQfnumber(res, "typrelkind");
6315 : 193 : i_typtype = PQfnumber(res, "typtype");
6316 : 193 : i_typisdefined = PQfnumber(res, "typisdefined");
6317 : 193 : i_isarray = PQfnumber(res, "isarray");
6318 : 193 : i_typarray = PQfnumber(res, "typarray");
6319 : :
6320 [ + + ]: 143065 : for (i = 0; i < ntups; i++)
6321 : : {
6322 : 142872 : tyinfo[i].dobj.objType = DO_TYPE;
6323 : 142872 : tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6324 : 142872 : tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6325 : 142872 : AssignDumpId(&tyinfo[i].dobj);
6326 : 142872 : tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
6327 : 285744 : tyinfo[i].dobj.namespace =
6328 : 142872 : findNamespace(atooid(PQgetvalue(res, i, i_typnamespace)));
6329 : 142872 : tyinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_typacl));
6330 : 142872 : tyinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6331 : 142872 : tyinfo[i].dacl.privtype = 0;
6332 : 142872 : tyinfo[i].dacl.initprivs = NULL;
6333 : 142872 : tyinfo[i].ftypname = NULL; /* may get filled later */
6334 : 142872 : tyinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_typowner));
6335 : 142872 : tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
6336 : 142872 : tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
6337 : 142872 : tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
6338 : 142872 : tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
6339 : 142872 : tyinfo[i].shellType = NULL;
6340 : :
6341 [ + + ]: 142872 : if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
6342 : 142817 : tyinfo[i].isDefined = true;
6343 : : else
6344 : 55 : tyinfo[i].isDefined = false;
6345 : :
6346 [ + + ]: 142872 : if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
6347 : 68610 : tyinfo[i].isArray = true;
6348 : : else
6349 : 74262 : tyinfo[i].isArray = false;
6350 : :
6351 : 142872 : tyinfo[i].typarray = atooid(PQgetvalue(res, i, i_typarray));
6352 : :
6353 [ + + ]: 142872 : if (tyinfo[i].typtype == TYPTYPE_MULTIRANGE)
6354 : 1303 : tyinfo[i].isMultirange = true;
6355 : : else
6356 : 141569 : tyinfo[i].isMultirange = false;
6357 : :
6358 : : /* Decide whether we want to dump it */
6359 : 142872 : selectDumpableType(&tyinfo[i], fout);
6360 : :
6361 : : /* Mark whether type has an ACL */
6362 [ + + ]: 142872 : if (!PQgetisnull(res, i, i_typacl))
6363 : 217 : tyinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6364 : :
6365 : : /*
6366 : : * If it's a domain, fetch info about its constraints, if any
6367 : : */
6368 : 142872 : tyinfo[i].nDomChecks = 0;
6369 : 142872 : tyinfo[i].domChecks = NULL;
6370 : 142872 : tyinfo[i].notnull = NULL;
6371 [ + + ]: 142872 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6372 [ + + ]: 15836 : tyinfo[i].typtype == TYPTYPE_DOMAIN)
6373 : 171 : getDomainConstraints(fout, &(tyinfo[i]));
6374 : :
6375 : : /*
6376 : : * If it's a base type, make a DumpableObject representing a shell
6377 : : * definition of the type. We will need to dump that ahead of the I/O
6378 : : * functions for the type. Similarly, range types need a shell
6379 : : * definition in case they have a canonicalize function.
6380 : : *
6381 : : * Note: the shell type doesn't have a catId. You might think it
6382 : : * should copy the base type's catId, but then it might capture the
6383 : : * pg_depend entries for the type, which we don't want.
6384 : : */
6385 [ + + ]: 142872 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6386 [ + + ]: 15836 : (tyinfo[i].typtype == TYPTYPE_BASE ||
6387 [ + + ]: 7700 : tyinfo[i].typtype == TYPTYPE_RANGE))
6388 : : {
6389 : 8271 : stinfo = pg_malloc_object(ShellTypeInfo);
6390 : 8271 : stinfo->dobj.objType = DO_SHELL_TYPE;
6391 : 8271 : stinfo->dobj.catId = nilCatalogId;
6392 : 8271 : AssignDumpId(&stinfo->dobj);
6393 : 8271 : stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
6394 : 8271 : stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
6395 : 8271 : stinfo->baseType = &(tyinfo[i]);
6396 : 8271 : tyinfo[i].shellType = stinfo;
6397 : :
6398 : : /*
6399 : : * Initially mark the shell type as not to be dumped. We'll only
6400 : : * dump it if the I/O or canonicalize functions need to be dumped;
6401 : : * this is taken care of while sorting dependencies.
6402 : : */
6403 : 8271 : stinfo->dobj.dump = DUMP_COMPONENT_NONE;
6404 : : }
6405 : : }
6406 : :
6407 : 193 : PQclear(res);
6408 : :
6409 : 193 : destroyPQExpBuffer(query);
6410 : 193 : }
6411 : :
6412 : : /*
6413 : : * getOperators:
6414 : : * get information about all operators in the system catalogs
6415 : : */
6416 : : void
6417 : 193 : getOperators(Archive *fout)
6418 : : {
6419 : : PGresult *res;
6420 : : int ntups;
6421 : : int i;
6422 : 193 : PQExpBuffer query = createPQExpBuffer();
6423 : : OprInfo *oprinfo;
6424 : : int i_tableoid;
6425 : : int i_oid;
6426 : : int i_oprname;
6427 : : int i_oprnamespace;
6428 : : int i_oprowner;
6429 : : int i_oprkind;
6430 : : int i_oprleft;
6431 : : int i_oprright;
6432 : : int i_oprcode;
6433 : :
6434 : : /*
6435 : : * find all operators, including builtin operators; we filter out
6436 : : * system-defined operators at dump-out time.
6437 : : */
6438 : :
6439 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, oprname, "
6440 : : "oprnamespace, "
6441 : : "oprowner, "
6442 : : "oprkind, "
6443 : : "oprleft, "
6444 : : "oprright, "
6445 : : "oprcode::oid AS oprcode "
6446 : : "FROM pg_operator");
6447 : :
6448 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6449 : :
6450 : 193 : ntups = PQntuples(res);
6451 : :
6452 : 193 : oprinfo = pg_malloc_array(OprInfo, ntups);
6453 : :
6454 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6455 : 193 : i_oid = PQfnumber(res, "oid");
6456 : 193 : i_oprname = PQfnumber(res, "oprname");
6457 : 193 : i_oprnamespace = PQfnumber(res, "oprnamespace");
6458 : 193 : i_oprowner = PQfnumber(res, "oprowner");
6459 : 193 : i_oprkind = PQfnumber(res, "oprkind");
6460 : 193 : i_oprleft = PQfnumber(res, "oprleft");
6461 : 193 : i_oprright = PQfnumber(res, "oprright");
6462 : 193 : i_oprcode = PQfnumber(res, "oprcode");
6463 : :
6464 [ + + ]: 155703 : for (i = 0; i < ntups; i++)
6465 : : {
6466 : 155510 : oprinfo[i].dobj.objType = DO_OPERATOR;
6467 : 155510 : oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6468 : 155510 : oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6469 : 155510 : AssignDumpId(&oprinfo[i].dobj);
6470 : 155510 : oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
6471 : 311020 : oprinfo[i].dobj.namespace =
6472 : 155510 : findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)));
6473 : 155510 : oprinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_oprowner));
6474 : 155510 : oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
6475 : 155510 : oprinfo[i].oprleft = atooid(PQgetvalue(res, i, i_oprleft));
6476 : 155510 : oprinfo[i].oprright = atooid(PQgetvalue(res, i, i_oprright));
6477 : 155510 : oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
6478 : :
6479 : : /* Decide whether we want to dump it */
6480 : 155510 : selectDumpableObject(&(oprinfo[i].dobj), fout);
6481 : : }
6482 : :
6483 : 193 : PQclear(res);
6484 : :
6485 : 193 : destroyPQExpBuffer(query);
6486 : 193 : }
6487 : :
6488 : : /*
6489 : : * getCollations:
6490 : : * get information about all collations in the system catalogs
6491 : : */
6492 : : void
6493 : 193 : getCollations(Archive *fout)
6494 : : {
6495 : : PGresult *res;
6496 : : int ntups;
6497 : : int i;
6498 : : PQExpBuffer query;
6499 : : CollInfo *collinfo;
6500 : : int i_tableoid;
6501 : : int i_oid;
6502 : : int i_collname;
6503 : : int i_collnamespace;
6504 : : int i_collowner;
6505 : : int i_collencoding;
6506 : :
6507 : 193 : query = createPQExpBuffer();
6508 : :
6509 : : /*
6510 : : * find all collations, including builtin collations; we filter out
6511 : : * system-defined collations at dump-out time.
6512 : : */
6513 : :
6514 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, collname, "
6515 : : "collnamespace, "
6516 : : "collowner, "
6517 : : "collencoding "
6518 : : "FROM pg_collation");
6519 : :
6520 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6521 : :
6522 : 193 : ntups = PQntuples(res);
6523 : :
6524 : 193 : collinfo = pg_malloc_array(CollInfo, ntups);
6525 : :
6526 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6527 : 193 : i_oid = PQfnumber(res, "oid");
6528 : 193 : i_collname = PQfnumber(res, "collname");
6529 : 193 : i_collnamespace = PQfnumber(res, "collnamespace");
6530 : 193 : i_collowner = PQfnumber(res, "collowner");
6531 : 193 : i_collencoding = PQfnumber(res, "collencoding");
6532 : :
6533 [ + + ]: 170150 : for (i = 0; i < ntups; i++)
6534 : : {
6535 : 169957 : collinfo[i].dobj.objType = DO_COLLATION;
6536 : 169957 : collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6537 : 169957 : collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6538 : 169957 : AssignDumpId(&collinfo[i].dobj);
6539 : 169957 : collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
6540 : 339914 : collinfo[i].dobj.namespace =
6541 : 169957 : findNamespace(atooid(PQgetvalue(res, i, i_collnamespace)));
6542 : 169957 : collinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_collowner));
6543 : 169957 : collinfo[i].collencoding = atoi(PQgetvalue(res, i, i_collencoding));
6544 : :
6545 : : /* Decide whether we want to dump it */
6546 : 169957 : selectDumpableObject(&(collinfo[i].dobj), fout);
6547 : : }
6548 : :
6549 : 193 : PQclear(res);
6550 : :
6551 : 193 : destroyPQExpBuffer(query);
6552 : 193 : }
6553 : :
6554 : : /*
6555 : : * getConversions:
6556 : : * get information about all conversions in the system catalogs
6557 : : */
6558 : : void
6559 : 193 : getConversions(Archive *fout)
6560 : : {
6561 : : PGresult *res;
6562 : : int ntups;
6563 : : int i;
6564 : : PQExpBuffer query;
6565 : : ConvInfo *convinfo;
6566 : : int i_tableoid;
6567 : : int i_oid;
6568 : : int i_conname;
6569 : : int i_connamespace;
6570 : : int i_conowner;
6571 : :
6572 : 193 : query = createPQExpBuffer();
6573 : :
6574 : : /*
6575 : : * find all conversions, including builtin conversions; we filter out
6576 : : * system-defined conversions at dump-out time.
6577 : : */
6578 : :
6579 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, conname, "
6580 : : "connamespace, "
6581 : : "conowner "
6582 : : "FROM pg_conversion");
6583 : :
6584 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6585 : :
6586 : 193 : ntups = PQntuples(res);
6587 : :
6588 : 193 : convinfo = pg_malloc_array(ConvInfo, ntups);
6589 : :
6590 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6591 : 193 : i_oid = PQfnumber(res, "oid");
6592 : 193 : i_conname = PQfnumber(res, "conname");
6593 : 193 : i_connamespace = PQfnumber(res, "connamespace");
6594 : 193 : i_conowner = PQfnumber(res, "conowner");
6595 : :
6596 [ + + ]: 19155 : for (i = 0; i < ntups; i++)
6597 : : {
6598 : 18962 : convinfo[i].dobj.objType = DO_CONVERSION;
6599 : 18962 : convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6600 : 18962 : convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6601 : 18962 : AssignDumpId(&convinfo[i].dobj);
6602 : 18962 : convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
6603 : 37924 : convinfo[i].dobj.namespace =
6604 : 18962 : findNamespace(atooid(PQgetvalue(res, i, i_connamespace)));
6605 : 18962 : convinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_conowner));
6606 : :
6607 : : /* Decide whether we want to dump it */
6608 : 18962 : selectDumpableObject(&(convinfo[i].dobj), fout);
6609 : : }
6610 : :
6611 : 193 : PQclear(res);
6612 : :
6613 : 193 : destroyPQExpBuffer(query);
6614 : 193 : }
6615 : :
6616 : : /*
6617 : : * getAccessMethods:
6618 : : * get information about all user-defined access methods
6619 : : */
6620 : : void
6621 : 193 : getAccessMethods(Archive *fout)
6622 : : {
6623 : : PGresult *res;
6624 : : int ntups;
6625 : : int i;
6626 : : PQExpBuffer query;
6627 : : AccessMethodInfo *aminfo;
6628 : : int i_tableoid;
6629 : : int i_oid;
6630 : : int i_amname;
6631 : : int i_amhandler;
6632 : : int i_amtype;
6633 : :
6634 : 193 : query = createPQExpBuffer();
6635 : :
6636 : : /*
6637 : : * Select all access methods from pg_am table.
6638 : : */
6639 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, ");
6640 : 193 : appendPQExpBufferStr(query,
6641 : : "amtype, "
6642 : : "amhandler::pg_catalog.regproc AS amhandler ");
6643 : 193 : appendPQExpBufferStr(query, "FROM pg_am");
6644 : :
6645 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6646 : :
6647 : 193 : ntups = PQntuples(res);
6648 : :
6649 : 193 : aminfo = pg_malloc_array(AccessMethodInfo, ntups);
6650 : :
6651 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6652 : 193 : i_oid = PQfnumber(res, "oid");
6653 : 193 : i_amname = PQfnumber(res, "amname");
6654 : 193 : i_amhandler = PQfnumber(res, "amhandler");
6655 : 193 : i_amtype = PQfnumber(res, "amtype");
6656 : :
6657 [ + + ]: 1672 : for (i = 0; i < ntups; i++)
6658 : : {
6659 : 1479 : aminfo[i].dobj.objType = DO_ACCESS_METHOD;
6660 : 1479 : aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6661 : 1479 : aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6662 : 1479 : AssignDumpId(&aminfo[i].dobj);
6663 : 1479 : aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname));
6664 : 1479 : aminfo[i].dobj.namespace = NULL;
6665 : 1479 : aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler));
6666 : 1479 : aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype));
6667 : :
6668 : : /* Decide whether we want to dump it */
6669 : 1479 : selectDumpableAccessMethod(&(aminfo[i]), fout);
6670 : : }
6671 : :
6672 : 193 : PQclear(res);
6673 : :
6674 : 193 : destroyPQExpBuffer(query);
6675 : 193 : }
6676 : :
6677 : :
6678 : : /*
6679 : : * getOpclasses:
6680 : : * get information about all opclasses in the system catalogs
6681 : : */
6682 : : void
6683 : 193 : getOpclasses(Archive *fout)
6684 : : {
6685 : : PGresult *res;
6686 : : int ntups;
6687 : : int i;
6688 : 193 : PQExpBuffer query = createPQExpBuffer();
6689 : : OpclassInfo *opcinfo;
6690 : : int i_tableoid;
6691 : : int i_oid;
6692 : : int i_opcmethod;
6693 : : int i_opcname;
6694 : : int i_opcnamespace;
6695 : : int i_opcowner;
6696 : :
6697 : : /*
6698 : : * find all opclasses, including builtin opclasses; we filter out
6699 : : * system-defined opclasses at dump-out time.
6700 : : */
6701 : :
6702 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opcmethod, opcname, "
6703 : : "opcnamespace, "
6704 : : "opcowner "
6705 : : "FROM pg_opclass");
6706 : :
6707 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6708 : :
6709 : 193 : ntups = PQntuples(res);
6710 : :
6711 : 193 : opcinfo = pg_malloc_array(OpclassInfo, ntups);
6712 : :
6713 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6714 : 193 : i_oid = PQfnumber(res, "oid");
6715 : 193 : i_opcmethod = PQfnumber(res, "opcmethod");
6716 : 193 : i_opcname = PQfnumber(res, "opcname");
6717 : 193 : i_opcnamespace = PQfnumber(res, "opcnamespace");
6718 : 193 : i_opcowner = PQfnumber(res, "opcowner");
6719 : :
6720 [ + + ]: 34905 : for (i = 0; i < ntups; i++)
6721 : : {
6722 : 34712 : opcinfo[i].dobj.objType = DO_OPCLASS;
6723 : 34712 : opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6724 : 34712 : opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6725 : 34712 : AssignDumpId(&opcinfo[i].dobj);
6726 : 34712 : opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
6727 : 69424 : opcinfo[i].dobj.namespace =
6728 : 34712 : findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace)));
6729 : 34712 : opcinfo[i].opcmethod = atooid(PQgetvalue(res, i, i_opcmethod));
6730 : 34712 : opcinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opcowner));
6731 : :
6732 : : /* Decide whether we want to dump it */
6733 : 34712 : selectDumpableObject(&(opcinfo[i].dobj), fout);
6734 : : }
6735 : :
6736 : 193 : PQclear(res);
6737 : :
6738 : 193 : destroyPQExpBuffer(query);
6739 : 193 : }
6740 : :
6741 : : /*
6742 : : * getOpfamilies:
6743 : : * get information about all opfamilies in the system catalogs
6744 : : */
6745 : : void
6746 : 193 : getOpfamilies(Archive *fout)
6747 : : {
6748 : : PGresult *res;
6749 : : int ntups;
6750 : : int i;
6751 : : PQExpBuffer query;
6752 : : OpfamilyInfo *opfinfo;
6753 : : int i_tableoid;
6754 : : int i_oid;
6755 : : int i_opfmethod;
6756 : : int i_opfname;
6757 : : int i_opfnamespace;
6758 : : int i_opfowner;
6759 : :
6760 : 193 : query = createPQExpBuffer();
6761 : :
6762 : : /*
6763 : : * find all opfamilies, including builtin opfamilies; we filter out
6764 : : * system-defined opfamilies at dump-out time.
6765 : : */
6766 : :
6767 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opfmethod, opfname, "
6768 : : "opfnamespace, "
6769 : : "opfowner "
6770 : : "FROM pg_opfamily");
6771 : :
6772 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6773 : :
6774 : 193 : ntups = PQntuples(res);
6775 : :
6776 : 193 : opfinfo = pg_malloc_array(OpfamilyInfo, ntups);
6777 : :
6778 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6779 : 193 : i_oid = PQfnumber(res, "oid");
6780 : 193 : i_opfname = PQfnumber(res, "opfname");
6781 : 193 : i_opfmethod = PQfnumber(res, "opfmethod");
6782 : 193 : i_opfnamespace = PQfnumber(res, "opfnamespace");
6783 : 193 : i_opfowner = PQfnumber(res, "opfowner");
6784 : :
6785 [ + + ]: 28902 : for (i = 0; i < ntups; i++)
6786 : : {
6787 : 28709 : opfinfo[i].dobj.objType = DO_OPFAMILY;
6788 : 28709 : opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6789 : 28709 : opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6790 : 28709 : AssignDumpId(&opfinfo[i].dobj);
6791 : 28709 : opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
6792 : 57418 : opfinfo[i].dobj.namespace =
6793 : 28709 : findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace)));
6794 : 28709 : opfinfo[i].opfmethod = atooid(PQgetvalue(res, i, i_opfmethod));
6795 : 28709 : opfinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opfowner));
6796 : :
6797 : : /* Decide whether we want to dump it */
6798 : 28709 : selectDumpableObject(&(opfinfo[i].dobj), fout);
6799 : : }
6800 : :
6801 : 193 : PQclear(res);
6802 : :
6803 : 193 : destroyPQExpBuffer(query);
6804 : 193 : }
6805 : :
6806 : : /*
6807 : : * getAggregates:
6808 : : * get information about all user-defined aggregates in the system catalogs
6809 : : */
6810 : : void
6811 : 193 : getAggregates(Archive *fout)
6812 : : {
6813 : 193 : DumpOptions *dopt = fout->dopt;
6814 : : PGresult *res;
6815 : : int ntups;
6816 : : int i;
6817 : 193 : PQExpBuffer query = createPQExpBuffer();
6818 : : AggInfo *agginfo;
6819 : : int i_tableoid;
6820 : : int i_oid;
6821 : : int i_aggname;
6822 : : int i_aggnamespace;
6823 : : int i_pronargs;
6824 : : int i_proargtypes;
6825 : : int i_proowner;
6826 : : int i_aggacl;
6827 : : int i_acldefault;
6828 : : const char *agg_check;
6829 : :
6830 : : /*
6831 : : * Find all interesting aggregates. See comment in getFuncs() for the
6832 : : * rationale behind the filtering logic.
6833 : : */
6834 : 386 : agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'"
6835 [ + - ]: 193 : : "p.proisagg");
6836 : :
6837 : 193 : appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, "
6838 : : "p.proname AS aggname, "
6839 : : "p.pronamespace AS aggnamespace, "
6840 : : "p.pronargs, p.proargtypes, "
6841 : : "p.proowner, "
6842 : : "p.proacl AS aggacl, "
6843 : : "acldefault('f', p.proowner) AS acldefault "
6844 : : "FROM pg_proc p "
6845 : : "LEFT JOIN pg_init_privs pip ON "
6846 : : "(p.oid = pip.objoid "
6847 : : "AND pip.classoid = 'pg_proc'::regclass "
6848 : : "AND pip.objsubid = 0) "
6849 : : "WHERE %s AND ("
6850 : : "p.pronamespace != "
6851 : : "(SELECT oid FROM pg_namespace "
6852 : : "WHERE nspname = 'pg_catalog') OR "
6853 : : "p.proacl IS DISTINCT FROM pip.initprivs",
6854 : : agg_check);
6855 [ + + ]: 193 : if (dopt->binary_upgrade)
6856 : 42 : appendPQExpBufferStr(query,
6857 : : " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
6858 : : "classid = 'pg_proc'::regclass AND "
6859 : : "objid = p.oid AND "
6860 : : "refclassid = 'pg_extension'::regclass AND "
6861 : : "deptype = 'e')");
6862 : 193 : appendPQExpBufferChar(query, ')');
6863 : :
6864 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6865 : :
6866 : 193 : ntups = PQntuples(res);
6867 : :
6868 : 193 : agginfo = pg_malloc_array(AggInfo, ntups);
6869 : :
6870 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6871 : 193 : i_oid = PQfnumber(res, "oid");
6872 : 193 : i_aggname = PQfnumber(res, "aggname");
6873 : 193 : i_aggnamespace = PQfnumber(res, "aggnamespace");
6874 : 193 : i_pronargs = PQfnumber(res, "pronargs");
6875 : 193 : i_proargtypes = PQfnumber(res, "proargtypes");
6876 : 193 : i_proowner = PQfnumber(res, "proowner");
6877 : 193 : i_aggacl = PQfnumber(res, "aggacl");
6878 : 193 : i_acldefault = PQfnumber(res, "acldefault");
6879 : :
6880 [ + + ]: 595 : for (i = 0; i < ntups; i++)
6881 : : {
6882 : 402 : agginfo[i].aggfn.dobj.objType = DO_AGG;
6883 : 402 : agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6884 : 402 : agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6885 : 402 : AssignDumpId(&agginfo[i].aggfn.dobj);
6886 : 402 : agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
6887 : 804 : agginfo[i].aggfn.dobj.namespace =
6888 : 402 : findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace)));
6889 : 402 : agginfo[i].aggfn.dacl.acl = pg_strdup(PQgetvalue(res, i, i_aggacl));
6890 : 402 : agginfo[i].aggfn.dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6891 : 402 : agginfo[i].aggfn.dacl.privtype = 0;
6892 : 402 : agginfo[i].aggfn.dacl.initprivs = NULL;
6893 : 402 : agginfo[i].aggfn.rolname = getRoleName(PQgetvalue(res, i, i_proowner));
6894 : 402 : agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
6895 : 402 : agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */
6896 : 402 : agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs));
6897 [ + + ]: 402 : if (agginfo[i].aggfn.nargs == 0)
6898 : 56 : agginfo[i].aggfn.argtypes = NULL;
6899 : : else
6900 : 346 : agginfo[i].aggfn.argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
6901 : 346 : agginfo[i].aggfn.nargs);
6902 : 402 : agginfo[i].aggfn.postponed_def = false; /* might get set during sort */
6903 : :
6904 : : /* Decide whether we want to dump it */
6905 : 402 : selectDumpableObject(&(agginfo[i].aggfn.dobj), fout);
6906 : :
6907 : : /* Mark whether aggregate has an ACL */
6908 [ + + ]: 402 : if (!PQgetisnull(res, i, i_aggacl))
6909 : 25 : agginfo[i].aggfn.dobj.components |= DUMP_COMPONENT_ACL;
6910 : : }
6911 : :
6912 : 193 : PQclear(res);
6913 : :
6914 : 193 : destroyPQExpBuffer(query);
6915 : 193 : }
6916 : :
6917 : : /*
6918 : : * getFuncs:
6919 : : * get information about all user-defined functions in the system catalogs
6920 : : */
6921 : : void
6922 : 193 : getFuncs(Archive *fout)
6923 : : {
6924 : 193 : DumpOptions *dopt = fout->dopt;
6925 : : PGresult *res;
6926 : : int ntups;
6927 : : int i;
6928 : 193 : PQExpBuffer query = createPQExpBuffer();
6929 : : FuncInfo *finfo;
6930 : : int i_tableoid;
6931 : : int i_oid;
6932 : : int i_proname;
6933 : : int i_pronamespace;
6934 : : int i_proowner;
6935 : : int i_prolang;
6936 : : int i_pronargs;
6937 : : int i_proargtypes;
6938 : : int i_prorettype;
6939 : : int i_proacl;
6940 : : int i_acldefault;
6941 : : const char *not_agg_check;
6942 : :
6943 : : /*
6944 : : * Find all interesting functions. This is a bit complicated:
6945 : : *
6946 : : * 1. Always exclude aggregates; those are handled elsewhere.
6947 : : *
6948 : : * 2. Always exclude functions that are internally dependent on something
6949 : : * else, since presumably those will be created as a result of creating
6950 : : * the something else. This currently acts only to suppress constructor
6951 : : * functions for range types. Note this is OK only because the
6952 : : * constructors don't have any dependencies the range type doesn't have;
6953 : : * otherwise we might not get creation ordering correct.
6954 : : *
6955 : : * 3. Otherwise, we normally exclude functions in pg_catalog. However, if
6956 : : * they're members of extensions and we are in binary-upgrade mode then
6957 : : * include them, since we want to dump extension members individually in
6958 : : * that mode. Also, if they are used by casts or transforms then we need
6959 : : * to gather the information about them, though they won't be dumped if
6960 : : * they are built-in. Also, include functions in pg_catalog if they have
6961 : : * an ACL different from what's shown in pg_init_privs (so we have to join
6962 : : * to pg_init_privs; annoying).
6963 : : */
6964 : 386 : not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'"
6965 [ + - ]: 193 : : "NOT p.proisagg");
6966 : :
6967 : 193 : appendPQExpBuffer(query,
6968 : : "SELECT p.tableoid, p.oid, p.proname, p.prolang, "
6969 : : "p.pronargs, p.proargtypes, p.prorettype, "
6970 : : "p.proacl, "
6971 : : "acldefault('f', p.proowner) AS acldefault, "
6972 : : "p.pronamespace, "
6973 : : "p.proowner "
6974 : : "FROM pg_proc p "
6975 : : "LEFT JOIN pg_init_privs pip ON "
6976 : : "(p.oid = pip.objoid "
6977 : : "AND pip.classoid = 'pg_proc'::regclass "
6978 : : "AND pip.objsubid = 0) "
6979 : : "WHERE %s"
6980 : : "\n AND NOT EXISTS (SELECT 1 FROM pg_depend "
6981 : : "WHERE classid = 'pg_proc'::regclass AND "
6982 : : "objid = p.oid AND deptype = 'i')"
6983 : : "\n AND ("
6984 : : "\n pronamespace != "
6985 : : "(SELECT oid FROM pg_namespace "
6986 : : "WHERE nspname = 'pg_catalog')"
6987 : : "\n OR EXISTS (SELECT 1 FROM pg_cast"
6988 : : "\n WHERE pg_cast.oid > %u "
6989 : : "\n AND p.oid = pg_cast.castfunc)"
6990 : : "\n OR EXISTS (SELECT 1 FROM pg_transform"
6991 : : "\n WHERE pg_transform.oid > %u AND "
6992 : : "\n (p.oid = pg_transform.trffromsql"
6993 : : "\n OR p.oid = pg_transform.trftosql))",
6994 : : not_agg_check,
6995 : : g_last_builtin_oid,
6996 : : g_last_builtin_oid);
6997 [ + + ]: 193 : if (dopt->binary_upgrade)
6998 : 42 : appendPQExpBufferStr(query,
6999 : : "\n OR EXISTS(SELECT 1 FROM pg_depend WHERE "
7000 : : "classid = 'pg_proc'::regclass AND "
7001 : : "objid = p.oid AND "
7002 : : "refclassid = 'pg_extension'::regclass AND "
7003 : : "deptype = 'e')");
7004 : 193 : appendPQExpBufferStr(query,
7005 : : "\n OR p.proacl IS DISTINCT FROM pip.initprivs");
7006 : 193 : appendPQExpBufferChar(query, ')');
7007 : :
7008 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7009 : :
7010 : 193 : ntups = PQntuples(res);
7011 : :
7012 : 193 : finfo = pg_malloc0_array(FuncInfo, ntups);
7013 : :
7014 : 193 : i_tableoid = PQfnumber(res, "tableoid");
7015 : 193 : i_oid = PQfnumber(res, "oid");
7016 : 193 : i_proname = PQfnumber(res, "proname");
7017 : 193 : i_pronamespace = PQfnumber(res, "pronamespace");
7018 : 193 : i_proowner = PQfnumber(res, "proowner");
7019 : 193 : i_prolang = PQfnumber(res, "prolang");
7020 : 193 : i_pronargs = PQfnumber(res, "pronargs");
7021 : 193 : i_proargtypes = PQfnumber(res, "proargtypes");
7022 : 193 : i_prorettype = PQfnumber(res, "prorettype");
7023 : 193 : i_proacl = PQfnumber(res, "proacl");
7024 : 193 : i_acldefault = PQfnumber(res, "acldefault");
7025 : :
7026 [ + + ]: 5118 : for (i = 0; i < ntups; i++)
7027 : : {
7028 : 4925 : finfo[i].dobj.objType = DO_FUNC;
7029 : 4925 : finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7030 : 4925 : finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7031 : 4925 : AssignDumpId(&finfo[i].dobj);
7032 : 4925 : finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
7033 : 9850 : finfo[i].dobj.namespace =
7034 : 4925 : findNamespace(atooid(PQgetvalue(res, i, i_pronamespace)));
7035 : 4925 : finfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_proacl));
7036 : 4925 : finfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7037 : 4925 : finfo[i].dacl.privtype = 0;
7038 : 4925 : finfo[i].dacl.initprivs = NULL;
7039 : 4925 : finfo[i].rolname = getRoleName(PQgetvalue(res, i, i_proowner));
7040 : 4925 : finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
7041 : 4925 : finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
7042 : 4925 : finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
7043 [ + + ]: 4925 : if (finfo[i].nargs == 0)
7044 : 1102 : finfo[i].argtypes = NULL;
7045 : : else
7046 : 3823 : finfo[i].argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
7047 : 3823 : finfo[i].nargs);
7048 : 4925 : finfo[i].postponed_def = false; /* might get set during sort */
7049 : :
7050 : : /* Decide whether we want to dump it */
7051 : 4925 : selectDumpableObject(&(finfo[i].dobj), fout);
7052 : :
7053 : : /* Mark whether function has an ACL */
7054 [ + + ]: 4925 : if (!PQgetisnull(res, i, i_proacl))
7055 : 146 : finfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7056 : : }
7057 : :
7058 : 193 : PQclear(res);
7059 : :
7060 : 193 : destroyPQExpBuffer(query);
7061 : 193 : }
7062 : :
7063 : : /*
7064 : : * getRelationStatistics
7065 : : * register the statistics object as a dependent of the relation.
7066 : : *
7067 : : * reltuples is passed as a string to avoid complexities in converting from/to
7068 : : * floating point.
7069 : : */
7070 : : static RelStatsInfo *
7071 : 10106 : getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
7072 : : char *reltuples, int32 relallvisible,
7073 : : int32 relallfrozen, char relkind,
7074 : : char **indAttNames, int nindAttNames)
7075 : : {
7076 [ + + ]: 10106 : if (!fout->dopt->dumpStatistics)
7077 : 6229 : return NULL;
7078 : :
7079 [ + + + + ]: 3877 : if ((relkind == RELKIND_RELATION) ||
7080 [ + + ]: 1591 : (relkind == RELKIND_PARTITIONED_TABLE) ||
7081 [ + + ]: 951 : (relkind == RELKIND_INDEX) ||
7082 [ + + ]: 614 : (relkind == RELKIND_PARTITIONED_INDEX) ||
7083 [ + + ]: 284 : (relkind == RELKIND_MATVIEW ||
7084 : : relkind == RELKIND_FOREIGN_TABLE))
7085 : : {
7086 : 3628 : RelStatsInfo *info = pg_malloc0_object(RelStatsInfo);
7087 : 3628 : DumpableObject *dobj = &info->dobj;
7088 : :
7089 : 3628 : dobj->objType = DO_REL_STATS;
7090 : 3628 : dobj->catId.tableoid = 0;
7091 : 3628 : dobj->catId.oid = 0;
7092 : 3628 : AssignDumpId(dobj);
7093 : 3628 : dobj->dependencies = pg_malloc_object(DumpId);
7094 : 3628 : dobj->dependencies[0] = rel->dumpId;
7095 : 3628 : dobj->nDeps = 1;
7096 : 3628 : dobj->allocDeps = 1;
7097 : 3628 : dobj->components |= DUMP_COMPONENT_STATISTICS;
7098 : 3628 : dobj->name = pg_strdup(rel->name);
7099 : 3628 : dobj->namespace = rel->namespace;
7100 : 3628 : info->relid = rel->catId.oid;
7101 : 3628 : info->relpages = relpages;
7102 : 3628 : info->reltuples = pstrdup(reltuples);
7103 : 3628 : info->relallvisible = relallvisible;
7104 : 3628 : info->relallfrozen = relallfrozen;
7105 : 3628 : info->relkind = relkind;
7106 : 3628 : info->indAttNames = indAttNames;
7107 : 3628 : info->nindAttNames = nindAttNames;
7108 : :
7109 : : /*
7110 : : * Ordinarily, stats go in SECTION_DATA for tables and
7111 : : * SECTION_POST_DATA for indexes.
7112 : : *
7113 : : * However, the section may be updated later for materialized view
7114 : : * stats. REFRESH MATERIALIZED VIEW replaces the storage and resets
7115 : : * the stats, so the stats must be restored after the data. Also, the
7116 : : * materialized view definition may be postponed to SECTION_POST_DATA
7117 : : * (see repairMatViewBoundaryMultiLoop()).
7118 : : */
7119 [ + + - ]: 3628 : switch (info->relkind)
7120 : : {
7121 : 2651 : case RELKIND_RELATION:
7122 : : case RELKIND_PARTITIONED_TABLE:
7123 : : case RELKIND_MATVIEW:
7124 : : case RELKIND_FOREIGN_TABLE:
7125 : 2651 : info->section = SECTION_DATA;
7126 : 2651 : break;
7127 : 977 : case RELKIND_INDEX:
7128 : : case RELKIND_PARTITIONED_INDEX:
7129 : 977 : info->section = SECTION_POST_DATA;
7130 : 977 : break;
7131 : 0 : default:
7132 : 0 : pg_fatal("cannot dump statistics for relation kind \"%c\"",
7133 : : info->relkind);
7134 : : }
7135 : :
7136 : 3628 : return info;
7137 : : }
7138 : 249 : return NULL;
7139 : : }
7140 : :
7141 : : /*
7142 : : * getTables
7143 : : * read all the tables (no indexes) in the system catalogs,
7144 : : * and return them as an array of TableInfo structures
7145 : : *
7146 : : * *numTables is set to the number of tables read in
7147 : : */
7148 : : TableInfo *
7149 : 194 : getTables(Archive *fout, int *numTables)
7150 : : {
7151 : 194 : DumpOptions *dopt = fout->dopt;
7152 : : PGresult *res;
7153 : : int ntups;
7154 : : int i;
7155 : 194 : PQExpBuffer query = createPQExpBuffer();
7156 : : TableInfo *tblinfo;
7157 : : int i_reltableoid;
7158 : : int i_reloid;
7159 : : int i_relname;
7160 : : int i_relnamespace;
7161 : : int i_relkind;
7162 : : int i_reltype;
7163 : : int i_relowner;
7164 : : int i_relchecks;
7165 : : int i_relhasindex;
7166 : : int i_relhasrules;
7167 : : int i_relpages;
7168 : : int i_reltuples;
7169 : : int i_relallvisible;
7170 : : int i_relallfrozen;
7171 : : int i_toastpages;
7172 : : int i_owning_tab;
7173 : : int i_owning_col;
7174 : : int i_reltablespace;
7175 : : int i_relhasoids;
7176 : : int i_relhastriggers;
7177 : : int i_relpersistence;
7178 : : int i_relispopulated;
7179 : : int i_relreplident;
7180 : : int i_relrowsec;
7181 : : int i_relforcerowsec;
7182 : : int i_relfrozenxid;
7183 : : int i_toastfrozenxid;
7184 : : int i_toastoid;
7185 : : int i_relminmxid;
7186 : : int i_toastminmxid;
7187 : : int i_reloptions;
7188 : : int i_checkoption;
7189 : : int i_toastreloptions;
7190 : : int i_reloftype;
7191 : : int i_foreignserver;
7192 : : int i_amname;
7193 : : int i_is_identity_sequence;
7194 : : int i_relacl;
7195 : : int i_acldefault;
7196 : : int i_ispartition;
7197 : :
7198 : : /*
7199 : : * Find all the tables and table-like objects.
7200 : : *
7201 : : * We must fetch all tables in this phase because otherwise we cannot
7202 : : * correctly identify inherited columns, owned sequences, etc.
7203 : : *
7204 : : * We include system catalogs, so that we can work if a user table is
7205 : : * defined to inherit from a system catalog (pretty weird, but...)
7206 : : *
7207 : : * Note: in this phase we should collect only a minimal amount of
7208 : : * information about each table, basically just enough to decide if it is
7209 : : * interesting. In particular, since we do not yet have lock on any user
7210 : : * table, we MUST NOT invoke any server-side data collection functions
7211 : : * (for instance, pg_get_partkeydef()). Those are likely to fail or give
7212 : : * wrong answers if any concurrent DDL is happening.
7213 : : */
7214 : :
7215 : 194 : appendPQExpBufferStr(query,
7216 : : "SELECT c.tableoid, c.oid, c.relname, "
7217 : : "c.relnamespace, c.relkind, c.reltype, "
7218 : : "c.relowner, "
7219 : : "c.relchecks, "
7220 : : "c.relhasindex, c.relhasrules, c.relpages, "
7221 : : "c.reltuples, c.relallvisible, ");
7222 : :
7223 [ + - ]: 194 : if (fout->remoteVersion >= 180000)
7224 : 194 : appendPQExpBufferStr(query, "c.relallfrozen, ");
7225 : : else
7226 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7227 : :
7228 : 194 : appendPQExpBufferStr(query,
7229 : : "c.relhastriggers, c.relpersistence, "
7230 : : "c.reloftype, "
7231 : : "c.relacl, "
7232 : : "acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
7233 : : " THEN 's'::\"char\" ELSE 'r'::\"char\" END, c.relowner) AS acldefault, "
7234 : : "CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN "
7235 : : "(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) "
7236 : : "ELSE 0 END AS foreignserver, "
7237 : : "c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, "
7238 : : "tc.oid AS toid, "
7239 : : "tc.relpages AS toastpages, "
7240 : : "tc.reloptions AS toast_reloptions, "
7241 : : "d.refobjid AS owning_tab, "
7242 : : "d.refobjsubid AS owning_col, "
7243 : : "tsp.spcname AS reltablespace, ");
7244 : :
7245 [ + - ]: 194 : if (fout->remoteVersion >= 120000)
7246 : 194 : appendPQExpBufferStr(query,
7247 : : "false AS relhasoids, ");
7248 : : else
7249 : 0 : appendPQExpBufferStr(query,
7250 : : "c.relhasoids, ");
7251 : :
7252 : 194 : appendPQExpBufferStr(query,
7253 : : "c.relispopulated, ");
7254 : :
7255 : 194 : appendPQExpBufferStr(query,
7256 : : "c.relreplident, ");
7257 : :
7258 : 194 : appendPQExpBufferStr(query,
7259 : : "c.relrowsecurity, c.relforcerowsecurity, ");
7260 : :
7261 : 194 : appendPQExpBufferStr(query,
7262 : : "c.relminmxid, tc.relminmxid AS tminmxid, ");
7263 : :
7264 : 194 : appendPQExpBufferStr(query,
7265 : : "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
7266 : : "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
7267 : : "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, ");
7268 : :
7269 : 194 : appendPQExpBufferStr(query,
7270 : : "am.amname, ");
7271 : :
7272 : 194 : appendPQExpBufferStr(query,
7273 : : "(d.deptype = 'i') IS TRUE AS is_identity_sequence, ");
7274 : :
7275 : 194 : appendPQExpBufferStr(query,
7276 : : "c.relispartition AS ispartition ");
7277 : :
7278 : : /*
7279 : : * Left join to pg_depend to pick up dependency info linking sequences to
7280 : : * their owning column, if any (note this dependency is AUTO except for
7281 : : * identity sequences, where it's INTERNAL). Also join to pg_tablespace to
7282 : : * collect the spcname.
7283 : : */
7284 : 194 : appendPQExpBufferStr(query,
7285 : : "\nFROM pg_class c\n"
7286 : : "LEFT JOIN pg_depend d ON "
7287 : : "(c.relkind = " CppAsString2(RELKIND_SEQUENCE) " AND "
7288 : : "d.classid = 'pg_class'::regclass AND d.objid = c.oid AND "
7289 : : "d.objsubid = 0 AND "
7290 : : "d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i'))\n"
7291 : : "LEFT JOIN pg_tablespace tsp ON (tsp.oid = c.reltablespace)\n");
7292 : :
7293 : : /*
7294 : : * Left join to pg_am to pick up the amname.
7295 : : */
7296 : 194 : appendPQExpBufferStr(query,
7297 : : "LEFT JOIN pg_am am ON (c.relam = am.oid)\n");
7298 : :
7299 : : /*
7300 : : * We purposefully ignore toast OIDs for partitioned tables; the reason is
7301 : : * that versions 10 and 11 have them, but later versions do not, so
7302 : : * emitting them causes the upgrade to fail.
7303 : : */
7304 : 194 : appendPQExpBufferStr(query,
7305 : : "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid"
7306 : : " AND tc.relkind = " CppAsString2(RELKIND_TOASTVALUE)
7307 : : " AND c.relkind <> " CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
7308 : :
7309 : : /*
7310 : : * Restrict to interesting relkinds (in particular, not indexes). Not all
7311 : : * relkinds are possible in older servers, but it's not worth the trouble
7312 : : * to emit a version-dependent list.
7313 : : *
7314 : : * Composite-type table entries won't be dumped as such, but we have to
7315 : : * make a DumpableObject for them so that we can track dependencies of the
7316 : : * composite type (pg_depend entries for columns of the composite type
7317 : : * link to the pg_class entry not the pg_type entry).
7318 : : */
7319 : 194 : appendPQExpBufferStr(query,
7320 : : "WHERE c.relkind IN ("
7321 : : CppAsString2(RELKIND_RELATION) ", "
7322 : : CppAsString2(RELKIND_SEQUENCE) ", "
7323 : : CppAsString2(RELKIND_VIEW) ", "
7324 : : CppAsString2(RELKIND_COMPOSITE_TYPE) ", "
7325 : : CppAsString2(RELKIND_MATVIEW) ", "
7326 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
7327 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n"
7328 : : "ORDER BY c.oid");
7329 : :
7330 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7331 : :
7332 : 194 : ntups = PQntuples(res);
7333 : :
7334 : 194 : *numTables = ntups;
7335 : :
7336 : : /*
7337 : : * Extract data from result and lock dumpable tables. We do the locking
7338 : : * before anything else, to minimize the window wherein a table could
7339 : : * disappear under us.
7340 : : *
7341 : : * Note that we have to save info about all tables here, even when dumping
7342 : : * only one, because we don't yet know which tables might be inheritance
7343 : : * ancestors of the target table.
7344 : : */
7345 : 194 : tblinfo = pg_malloc0_array(TableInfo, ntups);
7346 : :
7347 : 194 : i_reltableoid = PQfnumber(res, "tableoid");
7348 : 194 : i_reloid = PQfnumber(res, "oid");
7349 : 194 : i_relname = PQfnumber(res, "relname");
7350 : 194 : i_relnamespace = PQfnumber(res, "relnamespace");
7351 : 194 : i_relkind = PQfnumber(res, "relkind");
7352 : 194 : i_reltype = PQfnumber(res, "reltype");
7353 : 194 : i_relowner = PQfnumber(res, "relowner");
7354 : 194 : i_relchecks = PQfnumber(res, "relchecks");
7355 : 194 : i_relhasindex = PQfnumber(res, "relhasindex");
7356 : 194 : i_relhasrules = PQfnumber(res, "relhasrules");
7357 : 194 : i_relpages = PQfnumber(res, "relpages");
7358 : 194 : i_reltuples = PQfnumber(res, "reltuples");
7359 : 194 : i_relallvisible = PQfnumber(res, "relallvisible");
7360 : 194 : i_relallfrozen = PQfnumber(res, "relallfrozen");
7361 : 194 : i_toastpages = PQfnumber(res, "toastpages");
7362 : 194 : i_owning_tab = PQfnumber(res, "owning_tab");
7363 : 194 : i_owning_col = PQfnumber(res, "owning_col");
7364 : 194 : i_reltablespace = PQfnumber(res, "reltablespace");
7365 : 194 : i_relhasoids = PQfnumber(res, "relhasoids");
7366 : 194 : i_relhastriggers = PQfnumber(res, "relhastriggers");
7367 : 194 : i_relpersistence = PQfnumber(res, "relpersistence");
7368 : 194 : i_relispopulated = PQfnumber(res, "relispopulated");
7369 : 194 : i_relreplident = PQfnumber(res, "relreplident");
7370 : 194 : i_relrowsec = PQfnumber(res, "relrowsecurity");
7371 : 194 : i_relforcerowsec = PQfnumber(res, "relforcerowsecurity");
7372 : 194 : i_relfrozenxid = PQfnumber(res, "relfrozenxid");
7373 : 194 : i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
7374 : 194 : i_toastoid = PQfnumber(res, "toid");
7375 : 194 : i_relminmxid = PQfnumber(res, "relminmxid");
7376 : 194 : i_toastminmxid = PQfnumber(res, "tminmxid");
7377 : 194 : i_reloptions = PQfnumber(res, "reloptions");
7378 : 194 : i_checkoption = PQfnumber(res, "checkoption");
7379 : 194 : i_toastreloptions = PQfnumber(res, "toast_reloptions");
7380 : 194 : i_reloftype = PQfnumber(res, "reloftype");
7381 : 194 : i_foreignserver = PQfnumber(res, "foreignserver");
7382 : 194 : i_amname = PQfnumber(res, "amname");
7383 : 194 : i_is_identity_sequence = PQfnumber(res, "is_identity_sequence");
7384 : 194 : i_relacl = PQfnumber(res, "relacl");
7385 : 194 : i_acldefault = PQfnumber(res, "acldefault");
7386 : 194 : i_ispartition = PQfnumber(res, "ispartition");
7387 : :
7388 [ + + ]: 194 : if (dopt->lockWaitTimeout)
7389 : : {
7390 : : /*
7391 : : * Arrange to fail instead of waiting forever for a table lock.
7392 : : *
7393 : : * NB: this coding assumes that the only queries issued within the
7394 : : * following loop are LOCK TABLEs; else the timeout may be undesirably
7395 : : * applied to other things too.
7396 : : */
7397 : 2 : resetPQExpBuffer(query);
7398 : 2 : appendPQExpBufferStr(query, "SET statement_timeout = ");
7399 : 2 : appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout));
7400 : 2 : ExecuteSqlStatement(fout, query->data);
7401 : : }
7402 : :
7403 : 194 : resetPQExpBuffer(query);
7404 : :
7405 [ + + ]: 52582 : for (i = 0; i < ntups; i++)
7406 : : {
7407 : 52388 : int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible));
7408 : 52388 : int32 relallfrozen = atoi(PQgetvalue(res, i, i_relallfrozen));
7409 : :
7410 : 52388 : tblinfo[i].dobj.objType = DO_TABLE;
7411 : 52388 : tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
7412 : 52388 : tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
7413 : 52388 : AssignDumpId(&tblinfo[i].dobj);
7414 : 52388 : tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
7415 : 104776 : tblinfo[i].dobj.namespace =
7416 : 52388 : findNamespace(atooid(PQgetvalue(res, i, i_relnamespace)));
7417 : 52388 : tblinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_relacl));
7418 : 52388 : tblinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7419 : 52388 : tblinfo[i].dacl.privtype = 0;
7420 : 52388 : tblinfo[i].dacl.initprivs = NULL;
7421 : 52388 : tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
7422 : 52388 : tblinfo[i].reltype = atooid(PQgetvalue(res, i, i_reltype));
7423 : 52388 : tblinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_relowner));
7424 : 52388 : tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
7425 : 52388 : tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
7426 : 52388 : tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
7427 : 52388 : tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages));
7428 [ + + ]: 52388 : if (PQgetisnull(res, i, i_toastpages))
7429 : 42375 : tblinfo[i].toastpages = 0;
7430 : : else
7431 : 10013 : tblinfo[i].toastpages = atoi(PQgetvalue(res, i, i_toastpages));
7432 [ + + ]: 52388 : if (PQgetisnull(res, i, i_owning_tab))
7433 : : {
7434 : 51964 : tblinfo[i].owning_tab = InvalidOid;
7435 : 51964 : tblinfo[i].owning_col = 0;
7436 : : }
7437 : : else
7438 : : {
7439 : 424 : tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
7440 : 424 : tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
7441 : : }
7442 : 52388 : tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace));
7443 : 52388 : tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
7444 : 52388 : tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
7445 : 52388 : tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence));
7446 : 52388 : tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0);
7447 : 52388 : tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident));
7448 : 52388 : tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0);
7449 : 52388 : tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0);
7450 : 52388 : tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
7451 : 52388 : tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
7452 : 52388 : tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
7453 : 52388 : tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid));
7454 : 52388 : tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid));
7455 : 52388 : tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions));
7456 [ + + ]: 52388 : if (PQgetisnull(res, i, i_checkoption))
7457 : 52339 : tblinfo[i].checkoption = NULL;
7458 : : else
7459 : 49 : tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption));
7460 : 52388 : tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions));
7461 : 52388 : tblinfo[i].reloftype = atooid(PQgetvalue(res, i, i_reloftype));
7462 : 52388 : tblinfo[i].foreign_server = atooid(PQgetvalue(res, i, i_foreignserver));
7463 [ + + ]: 52388 : if (PQgetisnull(res, i, i_amname))
7464 : 31779 : tblinfo[i].amname = NULL;
7465 : : else
7466 : 20609 : tblinfo[i].amname = pg_strdup(PQgetvalue(res, i, i_amname));
7467 : 52388 : tblinfo[i].is_identity_sequence = (strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0);
7468 : 52388 : tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0);
7469 : :
7470 : : /* other fields were zeroed above */
7471 : :
7472 : : /*
7473 : : * Decide whether we want to dump this table.
7474 : : */
7475 [ + + ]: 52388 : if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
7476 : 186 : tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE;
7477 : : else
7478 : 52202 : selectDumpableTable(&tblinfo[i], fout);
7479 : :
7480 : : /*
7481 : : * Now, consider the table "interesting" if we need to dump its
7482 : : * definition, data or its statistics. Later on, we'll skip a lot of
7483 : : * data collection for uninteresting tables.
7484 : : *
7485 : : * Note: the "interesting" flag will also be set by flagInhTables for
7486 : : * parents of interesting tables, so that we collect necessary
7487 : : * inheritance info even when the parents are not themselves being
7488 : : * dumped. This is the main reason why we need an "interesting" flag
7489 : : * that's separate from the components-to-dump bitmask.
7490 : : */
7491 : 52388 : tblinfo[i].interesting = (tblinfo[i].dobj.dump &
7492 : : (DUMP_COMPONENT_DEFINITION |
7493 : : DUMP_COMPONENT_DATA |
7494 : 52388 : DUMP_COMPONENT_STATISTICS)) != 0;
7495 : :
7496 : 52388 : tblinfo[i].dummy_view = false; /* might get set during sort */
7497 : 52388 : tblinfo[i].postponed_def = false; /* might get set during sort */
7498 : :
7499 : : /* Tables have data */
7500 : 52388 : tblinfo[i].dobj.components |= DUMP_COMPONENT_DATA;
7501 : :
7502 : : /* Mark whether table has an ACL */
7503 [ + + ]: 52388 : if (!PQgetisnull(res, i, i_relacl))
7504 : 42102 : tblinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7505 : 52388 : tblinfo[i].hascolumnACLs = false; /* may get set later */
7506 : :
7507 : : /* Add statistics */
7508 [ + + ]: 52388 : if (tblinfo[i].interesting)
7509 : : {
7510 : : RelStatsInfo *stats;
7511 : :
7512 : 14736 : stats = getRelationStatistics(fout, &tblinfo[i].dobj,
7513 : 7368 : tblinfo[i].relpages,
7514 : : PQgetvalue(res, i, i_reltuples),
7515 : : relallvisible, relallfrozen,
7516 : 7368 : tblinfo[i].relkind, NULL, 0);
7517 [ + + ]: 7368 : if (tblinfo[i].relkind == RELKIND_MATVIEW)
7518 : 425 : tblinfo[i].stats = stats;
7519 : : }
7520 : :
7521 : : /*
7522 : : * Read-lock target tables to make sure they aren't DROPPED or altered
7523 : : * in schema before we get around to dumping them.
7524 : : *
7525 : : * Note that we don't explicitly lock parents of the target tables; we
7526 : : * assume our lock on the child is enough to prevent schema
7527 : : * alterations to parent tables.
7528 : : *
7529 : : * NOTE: it'd be kinda nice to lock other relations too, not only
7530 : : * plain or partitioned tables, but the backend doesn't presently
7531 : : * allow that.
7532 : : *
7533 : : * We only need to lock the table for certain components; see
7534 : : * pg_dump.h
7535 : : */
7536 [ + + ]: 52388 : if ((tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK) &&
7537 [ + + ]: 7368 : (tblinfo[i].relkind == RELKIND_RELATION ||
7538 [ + + ]: 2070 : tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE))
7539 : : {
7540 : : /*
7541 : : * Tables are locked in batches. When dumping from a remote
7542 : : * server this can save a significant amount of time by reducing
7543 : : * the number of round trips.
7544 : : */
7545 [ + + ]: 5933 : if (query->len == 0)
7546 : 127 : appendPQExpBuffer(query, "LOCK TABLE %s",
7547 : 127 : fmtQualifiedDumpable(&tblinfo[i]));
7548 : : else
7549 : : {
7550 : 5806 : appendPQExpBuffer(query, ", %s",
7551 : 5806 : fmtQualifiedDumpable(&tblinfo[i]));
7552 : :
7553 : : /* Arbitrarily end a batch when query length reaches 100K. */
7554 [ - + ]: 5806 : if (query->len >= 100000)
7555 : : {
7556 : : /* Lock another batch of tables. */
7557 : 0 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7558 : 0 : ExecuteSqlStatement(fout, query->data);
7559 : 0 : resetPQExpBuffer(query);
7560 : : }
7561 : : }
7562 : : }
7563 : : }
7564 : :
7565 [ + + ]: 194 : if (query->len != 0)
7566 : : {
7567 : : /* Lock the tables in the last batch. */
7568 : 127 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7569 : 127 : ExecuteSqlStatement(fout, query->data);
7570 : : }
7571 : :
7572 [ + + ]: 193 : if (dopt->lockWaitTimeout)
7573 : : {
7574 : 2 : ExecuteSqlStatement(fout, "SET statement_timeout = 0");
7575 : : }
7576 : :
7577 : 193 : PQclear(res);
7578 : :
7579 : 193 : destroyPQExpBuffer(query);
7580 : :
7581 : 193 : return tblinfo;
7582 : : }
7583 : :
7584 : : /*
7585 : : * getOwnedSeqs
7586 : : * identify owned sequences and mark them as dumpable if owning table is
7587 : : *
7588 : : * We used to do this in getTables(), but it's better to do it after the
7589 : : * index used by findTableByOid() has been set up.
7590 : : */
7591 : : void
7592 : 193 : getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables)
7593 : : {
7594 : : int i;
7595 : :
7596 : : /*
7597 : : * Force sequences that are "owned" by table columns to be dumped whenever
7598 : : * their owning table is being dumped.
7599 : : */
7600 [ + + ]: 52302 : for (i = 0; i < numTables; i++)
7601 : : {
7602 : 52109 : TableInfo *seqinfo = &tblinfo[i];
7603 : : TableInfo *owning_tab;
7604 : :
7605 [ + + ]: 52109 : if (!OidIsValid(seqinfo->owning_tab))
7606 : 51688 : continue; /* not an owned sequence */
7607 : :
7608 : 421 : owning_tab = findTableByOid(seqinfo->owning_tab);
7609 [ - + ]: 421 : if (owning_tab == NULL)
7610 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
7611 : : seqinfo->owning_tab, seqinfo->dobj.catId.oid);
7612 : :
7613 : : /*
7614 : : * For an identity sequence, dump exactly the same components for the
7615 : : * sequence as for the owning table. This is important because we
7616 : : * treat the identity sequence as an integral part of the table. For
7617 : : * example, there is not any DDL command that allows creation of such
7618 : : * a sequence independently of the table.
7619 : : *
7620 : : * For other owned sequences such as serial sequences, we need to dump
7621 : : * the components that are being dumped for the table and any
7622 : : * components that the sequence is explicitly marked with.
7623 : : *
7624 : : * We can't simply use the set of components which are being dumped
7625 : : * for the table as the table might be in an extension (and only the
7626 : : * non-extension components, eg: ACLs if changed, security labels, and
7627 : : * policies, are being dumped) while the sequence is not (and
7628 : : * therefore the definition and other components should also be
7629 : : * dumped).
7630 : : *
7631 : : * If the sequence is part of the extension then it should be properly
7632 : : * marked by checkExtensionMembership() and this will be a no-op as
7633 : : * the table will be equivalently marked.
7634 : : */
7635 [ + + ]: 421 : if (seqinfo->is_identity_sequence)
7636 : 202 : seqinfo->dobj.dump = owning_tab->dobj.dump;
7637 : : else
7638 : 219 : seqinfo->dobj.dump |= owning_tab->dobj.dump;
7639 : :
7640 : : /* Make sure that necessary data is available if we're dumping it */
7641 [ + + ]: 421 : if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE)
7642 : : {
7643 : 325 : seqinfo->interesting = true;
7644 : 325 : owning_tab->interesting = true;
7645 : : }
7646 : : }
7647 : 193 : }
7648 : :
7649 : : /*
7650 : : * getInherits
7651 : : * read all the inheritance information
7652 : : * from the system catalogs return them in the InhInfo* structure
7653 : : *
7654 : : * numInherits is set to the number of pairs read in
7655 : : */
7656 : : InhInfo *
7657 : 193 : getInherits(Archive *fout, int *numInherits)
7658 : : {
7659 : : PGresult *res;
7660 : : int ntups;
7661 : : int i;
7662 : 193 : PQExpBuffer query = createPQExpBuffer();
7663 : : InhInfo *inhinfo;
7664 : :
7665 : : int i_inhrelid;
7666 : : int i_inhparent;
7667 : :
7668 : : /* find all the inheritance information */
7669 : 193 : appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits");
7670 : :
7671 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7672 : :
7673 : 193 : ntups = PQntuples(res);
7674 : :
7675 : 193 : *numInherits = ntups;
7676 : :
7677 : 193 : inhinfo = pg_malloc_array(InhInfo, ntups);
7678 : :
7679 : 193 : i_inhrelid = PQfnumber(res, "inhrelid");
7680 : 193 : i_inhparent = PQfnumber(res, "inhparent");
7681 : :
7682 [ + + ]: 3821 : for (i = 0; i < ntups; i++)
7683 : : {
7684 : 3628 : inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
7685 : 3628 : inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
7686 : : }
7687 : :
7688 : 193 : PQclear(res);
7689 : :
7690 : 193 : destroyPQExpBuffer(query);
7691 : :
7692 : 193 : return inhinfo;
7693 : : }
7694 : :
7695 : : /*
7696 : : * getPartitioningInfo
7697 : : * get information about partitioning
7698 : : *
7699 : : * For the most part, we only collect partitioning info about tables we
7700 : : * intend to dump. However, this function has to consider all partitioned
7701 : : * tables in the database, because we need to know about parents of partitions
7702 : : * we are going to dump even if the parents themselves won't be dumped.
7703 : : *
7704 : : * Specifically, what we need to know is whether each partitioned table
7705 : : * has an "unsafe" partitioning scheme that requires us to force
7706 : : * load-via-partition-root mode for its children. Currently the only case
7707 : : * for which we force that is hash partitioning on enum columns, since the
7708 : : * hash codes depend on enum value OIDs which won't be replicated across
7709 : : * dump-and-reload. There are other cases in which load-via-partition-root
7710 : : * might be necessary, but we expect users to cope with them.
7711 : : */
7712 : : void
7713 : 193 : getPartitioningInfo(Archive *fout)
7714 : : {
7715 : : PQExpBuffer query;
7716 : : PGresult *res;
7717 : : int ntups;
7718 : :
7719 : : /* hash partitioning didn't exist before v11 */
7720 [ - + ]: 193 : if (fout->remoteVersion < 110000)
7721 : 0 : return;
7722 : : /* needn't bother if not dumping data */
7723 [ + + ]: 193 : if (!fout->dopt->dumpData)
7724 : 47 : return;
7725 : :
7726 : 146 : query = createPQExpBuffer();
7727 : :
7728 : : /*
7729 : : * Unsafe partitioning schemes are exactly those for which hash enum_ops
7730 : : * appears among the partition opclasses. We needn't check partstrat.
7731 : : *
7732 : : * Note that this query may well retrieve info about tables we aren't
7733 : : * going to dump and hence have no lock on. That's okay since we need not
7734 : : * invoke any unsafe server-side functions.
7735 : : */
7736 : 146 : appendPQExpBufferStr(query,
7737 : : "SELECT partrelid FROM pg_partitioned_table WHERE\n"
7738 : : "(SELECT c.oid FROM pg_opclass c JOIN pg_am a "
7739 : : "ON c.opcmethod = a.oid\n"
7740 : : "WHERE opcname = 'enum_ops' "
7741 : : "AND opcnamespace = 'pg_catalog'::regnamespace "
7742 : : "AND amname = 'hash') = ANY(partclass)");
7743 : :
7744 : 146 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7745 : :
7746 : 146 : ntups = PQntuples(res);
7747 : :
7748 [ + + ]: 191 : for (int i = 0; i < ntups; i++)
7749 : : {
7750 : 45 : Oid tabrelid = atooid(PQgetvalue(res, i, 0));
7751 : : TableInfo *tbinfo;
7752 : :
7753 : 45 : tbinfo = findTableByOid(tabrelid);
7754 [ - + ]: 45 : if (tbinfo == NULL)
7755 : 0 : pg_fatal("failed sanity check, table OID %u appearing in pg_partitioned_table not found",
7756 : : tabrelid);
7757 : 45 : tbinfo->unsafe_partitions = true;
7758 : : }
7759 : :
7760 : 146 : PQclear(res);
7761 : :
7762 : 146 : destroyPQExpBuffer(query);
7763 : : }
7764 : :
7765 : : /*
7766 : : * getIndexes
7767 : : * get information about every index on a dumpable table
7768 : : *
7769 : : * Note: index data is not returned directly to the caller, but it
7770 : : * does get entered into the DumpableObject tables.
7771 : : */
7772 : : void
7773 : 193 : getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
7774 : : {
7775 : 193 : PQExpBuffer query = createPQExpBuffer();
7776 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
7777 : : PGresult *res;
7778 : : int ntups;
7779 : : int curtblindx;
7780 : : IndxInfo *indxinfo;
7781 : : int i_tableoid,
7782 : : i_oid,
7783 : : i_indrelid,
7784 : : i_indexname,
7785 : : i_relpages,
7786 : : i_reltuples,
7787 : : i_relallvisible,
7788 : : i_relallfrozen,
7789 : : i_parentidx,
7790 : : i_indexdef,
7791 : : i_indnkeyatts,
7792 : : i_indnatts,
7793 : : i_indkey,
7794 : : i_indisclustered,
7795 : : i_indisreplident,
7796 : : i_indnullsnotdistinct,
7797 : : i_contype,
7798 : : i_conname,
7799 : : i_condeferrable,
7800 : : i_condeferred,
7801 : : i_conperiod,
7802 : : i_contableoid,
7803 : : i_conoid,
7804 : : i_condef,
7805 : : i_indattnames,
7806 : : i_tablespace,
7807 : : i_indreloptions,
7808 : : i_indstatcols,
7809 : : i_indstatvals;
7810 : :
7811 : : /*
7812 : : * We want to perform just one query against pg_index. However, we
7813 : : * mustn't try to select every row of the catalog and then sort it out on
7814 : : * the client side, because some of the server-side functions we need
7815 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
7816 : : * build an array of the OIDs of tables we care about (and now have lock
7817 : : * on!), and use a WHERE clause to constrain which rows are selected.
7818 : : */
7819 : 193 : appendPQExpBufferChar(tbloids, '{');
7820 [ + + ]: 52302 : for (int i = 0; i < numTables; i++)
7821 : : {
7822 : 52109 : TableInfo *tbinfo = &tblinfo[i];
7823 : :
7824 [ + + ]: 52109 : if (!tbinfo->hasindex)
7825 : 37234 : continue;
7826 : :
7827 : : /*
7828 : : * We can ignore indexes of uninteresting tables.
7829 : : */
7830 [ + + ]: 14875 : if (!tbinfo->interesting)
7831 : 12771 : continue;
7832 : :
7833 : : /* OK, we need info for this table */
7834 [ + + ]: 2104 : if (tbloids->len > 1) /* do we have more than the '{'? */
7835 : 2022 : appendPQExpBufferChar(tbloids, ',');
7836 : 2104 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
7837 : : }
7838 : 193 : appendPQExpBufferChar(tbloids, '}');
7839 : :
7840 : 193 : appendPQExpBufferStr(query,
7841 : : "SELECT t.tableoid, t.oid, i.indrelid, "
7842 : : "t.relname AS indexname, "
7843 : : "t.relpages, t.reltuples, t.relallvisible, ");
7844 : :
7845 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
7846 : 193 : appendPQExpBufferStr(query, "t.relallfrozen, ");
7847 : : else
7848 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7849 : :
7850 : 193 : appendPQExpBufferStr(query,
7851 : : "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
7852 : : "i.indkey, i.indisclustered, "
7853 : : "c.contype, c.conname, "
7854 : : "c.condeferrable, c.condeferred, "
7855 : : "c.tableoid AS contableoid, "
7856 : : "c.oid AS conoid, "
7857 : : "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
7858 : : "CASE WHEN i.indexprs IS NOT NULL THEN "
7859 : : "(SELECT pg_catalog.array_agg(attname ORDER BY attnum)"
7860 : : " FROM pg_catalog.pg_attribute "
7861 : : " WHERE attrelid = i.indexrelid) "
7862 : : "ELSE NULL END AS indattnames, "
7863 : : "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
7864 : : "t.reloptions AS indreloptions, ");
7865 : :
7866 : :
7867 : 193 : appendPQExpBufferStr(query,
7868 : : "i.indisreplident, ");
7869 : :
7870 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
7871 : 193 : appendPQExpBufferStr(query,
7872 : : "inh.inhparent AS parentidx, "
7873 : : "i.indnkeyatts AS indnkeyatts, "
7874 : : "i.indnatts AS indnatts, "
7875 : : "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) "
7876 : : " FROM pg_catalog.pg_attribute "
7877 : : " WHERE attrelid = i.indexrelid AND "
7878 : : " attstattarget >= 0) AS indstatcols, "
7879 : : "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) "
7880 : : " FROM pg_catalog.pg_attribute "
7881 : : " WHERE attrelid = i.indexrelid AND "
7882 : : " attstattarget >= 0) AS indstatvals, ");
7883 : : else
7884 : 0 : appendPQExpBufferStr(query,
7885 : : "0 AS parentidx, "
7886 : : "i.indnatts AS indnkeyatts, "
7887 : : "i.indnatts AS indnatts, "
7888 : : "'' AS indstatcols, "
7889 : : "'' AS indstatvals, ");
7890 : :
7891 [ + - ]: 193 : if (fout->remoteVersion >= 150000)
7892 : 193 : appendPQExpBufferStr(query,
7893 : : "i.indnullsnotdistinct, ");
7894 : : else
7895 : 0 : appendPQExpBufferStr(query,
7896 : : "false AS indnullsnotdistinct, ");
7897 : :
7898 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
7899 : 193 : appendPQExpBufferStr(query,
7900 : : "c.conperiod ");
7901 : : else
7902 : 0 : appendPQExpBufferStr(query,
7903 : : "NULL AS conperiod ");
7904 : :
7905 : : /*
7906 : : * The point of the messy-looking outer join is to find a constraint that
7907 : : * is related by an internal dependency link to the index. If we find one,
7908 : : * create a CONSTRAINT entry linked to the INDEX entry. We assume an
7909 : : * index won't have more than one internal dependency.
7910 : : *
7911 : : * Note: the check on conrelid is redundant, but useful because that
7912 : : * column is indexed while conindid is not.
7913 : : */
7914 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
7915 : : {
7916 : 193 : appendPQExpBuffer(query,
7917 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
7918 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
7919 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
7920 : : "JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) "
7921 : : "LEFT JOIN pg_catalog.pg_constraint c "
7922 : : "ON (i.indrelid = c.conrelid AND "
7923 : : "i.indexrelid = c.conindid AND "
7924 : : "c.contype IN ('p','u','x')) "
7925 : : "LEFT JOIN pg_catalog.pg_inherits inh "
7926 : : "ON (inh.inhrelid = indexrelid) "
7927 : : "WHERE (i.indisvalid OR t2.relkind = 'p') "
7928 : : "AND i.indisready "
7929 : : "ORDER BY i.indrelid, indexname",
7930 : : tbloids->data);
7931 : : }
7932 : : else
7933 : : {
7934 : : /*
7935 : : * the test on indisready is necessary in 9.2, and harmless in
7936 : : * earlier/later versions
7937 : : */
7938 : 0 : appendPQExpBuffer(query,
7939 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
7940 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
7941 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
7942 : : "LEFT JOIN pg_catalog.pg_constraint c "
7943 : : "ON (i.indrelid = c.conrelid AND "
7944 : : "i.indexrelid = c.conindid AND "
7945 : : "c.contype IN ('p','u','x')) "
7946 : : "WHERE i.indisvalid AND i.indisready "
7947 : : "ORDER BY i.indrelid, indexname",
7948 : : tbloids->data);
7949 : : }
7950 : :
7951 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7952 : :
7953 : 193 : ntups = PQntuples(res);
7954 : :
7955 : 193 : i_tableoid = PQfnumber(res, "tableoid");
7956 : 193 : i_oid = PQfnumber(res, "oid");
7957 : 193 : i_indrelid = PQfnumber(res, "indrelid");
7958 : 193 : i_indexname = PQfnumber(res, "indexname");
7959 : 193 : i_relpages = PQfnumber(res, "relpages");
7960 : 193 : i_reltuples = PQfnumber(res, "reltuples");
7961 : 193 : i_relallvisible = PQfnumber(res, "relallvisible");
7962 : 193 : i_relallfrozen = PQfnumber(res, "relallfrozen");
7963 : 193 : i_parentidx = PQfnumber(res, "parentidx");
7964 : 193 : i_indexdef = PQfnumber(res, "indexdef");
7965 : 193 : i_indnkeyatts = PQfnumber(res, "indnkeyatts");
7966 : 193 : i_indnatts = PQfnumber(res, "indnatts");
7967 : 193 : i_indkey = PQfnumber(res, "indkey");
7968 : 193 : i_indisclustered = PQfnumber(res, "indisclustered");
7969 : 193 : i_indisreplident = PQfnumber(res, "indisreplident");
7970 : 193 : i_indnullsnotdistinct = PQfnumber(res, "indnullsnotdistinct");
7971 : 193 : i_contype = PQfnumber(res, "contype");
7972 : 193 : i_conname = PQfnumber(res, "conname");
7973 : 193 : i_condeferrable = PQfnumber(res, "condeferrable");
7974 : 193 : i_condeferred = PQfnumber(res, "condeferred");
7975 : 193 : i_conperiod = PQfnumber(res, "conperiod");
7976 : 193 : i_contableoid = PQfnumber(res, "contableoid");
7977 : 193 : i_conoid = PQfnumber(res, "conoid");
7978 : 193 : i_condef = PQfnumber(res, "condef");
7979 : 193 : i_indattnames = PQfnumber(res, "indattnames");
7980 : 193 : i_tablespace = PQfnumber(res, "tablespace");
7981 : 193 : i_indreloptions = PQfnumber(res, "indreloptions");
7982 : 193 : i_indstatcols = PQfnumber(res, "indstatcols");
7983 : 193 : i_indstatvals = PQfnumber(res, "indstatvals");
7984 : :
7985 : 193 : indxinfo = pg_malloc_array(IndxInfo, ntups);
7986 : :
7987 : : /*
7988 : : * Outer loop iterates once per table, not once per row. Incrementing of
7989 : : * j is handled by the inner loop.
7990 : : */
7991 : 193 : curtblindx = -1;
7992 [ + + ]: 2283 : for (int j = 0; j < ntups;)
7993 : : {
7994 : 2090 : Oid indrelid = atooid(PQgetvalue(res, j, i_indrelid));
7995 : 2090 : TableInfo *tbinfo = NULL;
7996 : : int numinds;
7997 : :
7998 : : /* Count rows for this table */
7999 [ + + ]: 2738 : for (numinds = 1; numinds < ntups - j; numinds++)
8000 [ + + ]: 2656 : if (atooid(PQgetvalue(res, j + numinds, i_indrelid)) != indrelid)
8001 : 2008 : break;
8002 : :
8003 : : /*
8004 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8005 : : * order.
8006 : : */
8007 [ + - ]: 24576 : while (++curtblindx < numTables)
8008 : : {
8009 : 24576 : tbinfo = &tblinfo[curtblindx];
8010 [ + + ]: 24576 : if (tbinfo->dobj.catId.oid == indrelid)
8011 : 2090 : break;
8012 : : }
8013 [ - + ]: 2090 : if (curtblindx >= numTables)
8014 : 0 : pg_fatal("unrecognized table OID %u", indrelid);
8015 : : /* cross-check that we only got requested tables */
8016 [ + - ]: 2090 : if (!tbinfo->hasindex ||
8017 [ - + ]: 2090 : !tbinfo->interesting)
8018 : 0 : pg_fatal("unexpected index data for table \"%s\"",
8019 : : tbinfo->dobj.name);
8020 : :
8021 : : /* Save data for this table */
8022 : 2090 : tbinfo->indexes = indxinfo + j;
8023 : 2090 : tbinfo->numIndexes = numinds;
8024 : :
8025 [ + + ]: 4828 : for (int c = 0; c < numinds; c++, j++)
8026 : : {
8027 : : char contype;
8028 : : char indexkind;
8029 : 2738 : char **indAttNames = NULL;
8030 : 2738 : int nindAttNames = 0;
8031 : : RelStatsInfo *relstats;
8032 : 2738 : int32 relpages = atoi(PQgetvalue(res, j, i_relpages));
8033 : 2738 : int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible));
8034 : 2738 : int32 relallfrozen = atoi(PQgetvalue(res, j, i_relallfrozen));
8035 : :
8036 : 2738 : indxinfo[j].dobj.objType = DO_INDEX;
8037 : 2738 : indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8038 : 2738 : indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8039 : 2738 : AssignDumpId(&indxinfo[j].dobj);
8040 : 2738 : indxinfo[j].dobj.dump = tbinfo->dobj.dump;
8041 : 2738 : indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname));
8042 : 2738 : indxinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8043 : 2738 : indxinfo[j].indextable = tbinfo;
8044 : 2738 : indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef));
8045 : 2738 : indxinfo[j].indnkeyattrs = atoi(PQgetvalue(res, j, i_indnkeyatts));
8046 : 2738 : indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts));
8047 : 2738 : indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
8048 : 2738 : indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions));
8049 : 2738 : indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols));
8050 : 2738 : indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals));
8051 : 2738 : indxinfo[j].indkeys = parseIntArray(PQgetvalue(res, j, i_indkey),
8052 : 2738 : indxinfo[j].indnattrs);
8053 : 2738 : indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
8054 : 2738 : indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
8055 : 2738 : indxinfo[j].indnullsnotdistinct = (PQgetvalue(res, j, i_indnullsnotdistinct)[0] == 't');
8056 : 2738 : indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx));
8057 : 2738 : indxinfo[j].partattaches = (SimplePtrList)
8058 : : {
8059 : : NULL, NULL
8060 : : };
8061 : :
8062 [ + + ]: 2738 : if (indxinfo[j].parentidx == 0)
8063 : 2113 : indexkind = RELKIND_INDEX;
8064 : : else
8065 : 625 : indexkind = RELKIND_PARTITIONED_INDEX;
8066 : :
8067 [ + + ]: 2738 : if (!PQgetisnull(res, j, i_indattnames))
8068 : : {
8069 [ - + ]: 167 : if (!parsePGArray(PQgetvalue(res, j, i_indattnames),
8070 : : &indAttNames, &nindAttNames))
8071 : 0 : pg_fatal("could not parse %s array", "indattnames");
8072 : : }
8073 : :
8074 : 2738 : relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages,
8075 : : PQgetvalue(res, j, i_reltuples),
8076 : : relallvisible, relallfrozen, indexkind,
8077 : : indAttNames, nindAttNames);
8078 : :
8079 : 2738 : contype = *(PQgetvalue(res, j, i_contype));
8080 [ + + + + : 2738 : if (contype == 'p' || contype == 'u' || contype == 'x')
+ + ]
8081 : 1564 : {
8082 : : /*
8083 : : * If we found a constraint matching the index, create an
8084 : : * entry for it.
8085 : : */
8086 : : ConstraintInfo *constrinfo;
8087 : :
8088 : 1564 : constrinfo = pg_malloc_object(ConstraintInfo);
8089 : 1564 : constrinfo->dobj.objType = DO_CONSTRAINT;
8090 : 1564 : constrinfo->dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8091 : 1564 : constrinfo->dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8092 : 1564 : AssignDumpId(&constrinfo->dobj);
8093 : 1564 : constrinfo->dobj.dump = tbinfo->dobj.dump;
8094 : 1564 : constrinfo->dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8095 : 1564 : constrinfo->dobj.namespace = tbinfo->dobj.namespace;
8096 : 1564 : constrinfo->contable = tbinfo;
8097 : 1564 : constrinfo->condomain = NULL;
8098 : 1564 : constrinfo->contype = contype;
8099 [ + + ]: 1564 : if (contype == 'x')
8100 : 20 : constrinfo->condef = pg_strdup(PQgetvalue(res, j, i_condef));
8101 : : else
8102 : 1544 : constrinfo->condef = NULL;
8103 : 1564 : constrinfo->confrelid = InvalidOid;
8104 : 1564 : constrinfo->conindex = indxinfo[j].dobj.dumpId;
8105 : 1564 : constrinfo->condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't';
8106 : 1564 : constrinfo->condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't';
8107 : 1564 : constrinfo->conperiod = *(PQgetvalue(res, j, i_conperiod)) == 't';
8108 : 1564 : constrinfo->conislocal = true;
8109 : 1564 : constrinfo->separate = true;
8110 : :
8111 : 1564 : indxinfo[j].indexconstraint = constrinfo->dobj.dumpId;
8112 [ + + ]: 1564 : if (relstats != NULL)
8113 : 565 : addObjectDependency(&relstats->dobj, constrinfo->dobj.dumpId);
8114 : : }
8115 : : else
8116 : : {
8117 : : /* Plain secondary index */
8118 : 1174 : indxinfo[j].indexconstraint = 0;
8119 : : }
8120 : : }
8121 : : }
8122 : :
8123 : 193 : PQclear(res);
8124 : :
8125 : 193 : destroyPQExpBuffer(query);
8126 : 193 : destroyPQExpBuffer(tbloids);
8127 : 193 : }
8128 : :
8129 : : /*
8130 : : * getExtendedStatistics
8131 : : * get information about extended-statistics objects.
8132 : : *
8133 : : * Note: extended statistics data is not returned directly to the caller, but
8134 : : * it does get entered into the DumpableObject tables.
8135 : : */
8136 : : void
8137 : 193 : getExtendedStatistics(Archive *fout)
8138 : : {
8139 : : PQExpBuffer query;
8140 : : PGresult *res;
8141 : : StatsExtInfo *statsextinfo;
8142 : : int ntups;
8143 : : int i_tableoid;
8144 : : int i_oid;
8145 : : int i_stxname;
8146 : : int i_stxnamespace;
8147 : : int i_stxowner;
8148 : : int i_stxrelid;
8149 : : int i_stattarget;
8150 : : int i;
8151 : :
8152 : 193 : query = createPQExpBuffer();
8153 : :
8154 [ - + ]: 193 : if (fout->remoteVersion < 130000)
8155 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8156 : : "stxnamespace, stxowner, stxrelid, NULL AS stxstattarget "
8157 : : "FROM pg_catalog.pg_statistic_ext");
8158 : : else
8159 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8160 : : "stxnamespace, stxowner, stxrelid, stxstattarget "
8161 : : "FROM pg_catalog.pg_statistic_ext");
8162 : :
8163 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8164 : :
8165 : 193 : ntups = PQntuples(res);
8166 : :
8167 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8168 : 193 : i_oid = PQfnumber(res, "oid");
8169 : 193 : i_stxname = PQfnumber(res, "stxname");
8170 : 193 : i_stxnamespace = PQfnumber(res, "stxnamespace");
8171 : 193 : i_stxowner = PQfnumber(res, "stxowner");
8172 : 193 : i_stxrelid = PQfnumber(res, "stxrelid");
8173 : 193 : i_stattarget = PQfnumber(res, "stxstattarget");
8174 : :
8175 : 193 : statsextinfo = pg_malloc_array(StatsExtInfo, ntups);
8176 : :
8177 [ + + ]: 413 : for (i = 0; i < ntups; i++)
8178 : : {
8179 : 220 : statsextinfo[i].dobj.objType = DO_STATSEXT;
8180 : 220 : statsextinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8181 : 220 : statsextinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8182 : 220 : AssignDumpId(&statsextinfo[i].dobj);
8183 : 220 : statsextinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_stxname));
8184 : 440 : statsextinfo[i].dobj.namespace =
8185 : 220 : findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace)));
8186 : 220 : statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner));
8187 : 440 : statsextinfo[i].stattable =
8188 : 220 : findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid)));
8189 [ + + ]: 220 : if (PQgetisnull(res, i, i_stattarget))
8190 : 172 : statsextinfo[i].stattarget = -1;
8191 : : else
8192 : 48 : statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget));
8193 : :
8194 : : /* Decide whether we want to dump it */
8195 : 220 : selectDumpableStatisticsObject(&(statsextinfo[i]), fout);
8196 : :
8197 [ + + ]: 220 : if (fout->dopt->dumpStatistics)
8198 : 164 : statsextinfo[i].dobj.components |= DUMP_COMPONENT_STATISTICS;
8199 : : }
8200 : :
8201 : 193 : PQclear(res);
8202 : 193 : destroyPQExpBuffer(query);
8203 : 193 : }
8204 : :
8205 : : /*
8206 : : * getConstraints
8207 : : *
8208 : : * Get info about constraints on dumpable tables.
8209 : : *
8210 : : * Currently handles foreign keys only.
8211 : : * Unique and primary key constraints are handled with indexes,
8212 : : * while check constraints are processed in getTableAttrs().
8213 : : */
8214 : : void
8215 : 193 : getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
8216 : : {
8217 : 193 : PQExpBuffer query = createPQExpBuffer();
8218 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
8219 : : PGresult *res;
8220 : : int ntups;
8221 : : int curtblindx;
8222 : 193 : TableInfo *tbinfo = NULL;
8223 : : ConstraintInfo *constrinfo;
8224 : : int i_contableoid,
8225 : : i_conoid,
8226 : : i_conrelid,
8227 : : i_conname,
8228 : : i_confrelid,
8229 : : i_conindid,
8230 : : i_condef;
8231 : :
8232 : : /*
8233 : : * We want to perform just one query against pg_constraint. However, we
8234 : : * mustn't try to select every row of the catalog and then sort it out on
8235 : : * the client side, because some of the server-side functions we need
8236 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8237 : : * build an array of the OIDs of tables we care about (and now have lock
8238 : : * on!), and use a WHERE clause to constrain which rows are selected.
8239 : : */
8240 : 193 : appendPQExpBufferChar(tbloids, '{');
8241 [ + + ]: 52302 : for (int i = 0; i < numTables; i++)
8242 : : {
8243 : 52109 : TableInfo *tinfo = &tblinfo[i];
8244 : :
8245 [ + + ]: 52109 : if (!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8246 : 44795 : continue;
8247 : :
8248 : : /* OK, we need info for this table */
8249 [ + + ]: 7314 : if (tbloids->len > 1) /* do we have more than the '{'? */
8250 : 7186 : appendPQExpBufferChar(tbloids, ',');
8251 : 7314 : appendPQExpBuffer(tbloids, "%u", tinfo->dobj.catId.oid);
8252 : : }
8253 : 193 : appendPQExpBufferChar(tbloids, '}');
8254 : :
8255 : 193 : appendPQExpBufferStr(query,
8256 : : "SELECT c.tableoid, c.oid, "
8257 : : "conrelid, conname, confrelid, ");
8258 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
8259 : 193 : appendPQExpBufferStr(query, "conindid, ");
8260 : : else
8261 : 0 : appendPQExpBufferStr(query, "0 AS conindid, ");
8262 : 193 : appendPQExpBuffer(query,
8263 : : "pg_catalog.pg_get_constraintdef(c.oid) AS condef\n"
8264 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8265 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
8266 : : "WHERE contype = 'f' ",
8267 : : tbloids->data);
8268 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
8269 : 193 : appendPQExpBufferStr(query,
8270 : : "AND conparentid = 0 ");
8271 : 193 : appendPQExpBufferStr(query,
8272 : : "ORDER BY conrelid, conname");
8273 : :
8274 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8275 : :
8276 : 193 : ntups = PQntuples(res);
8277 : :
8278 : 193 : i_contableoid = PQfnumber(res, "tableoid");
8279 : 193 : i_conoid = PQfnumber(res, "oid");
8280 : 193 : i_conrelid = PQfnumber(res, "conrelid");
8281 : 193 : i_conname = PQfnumber(res, "conname");
8282 : 193 : i_confrelid = PQfnumber(res, "confrelid");
8283 : 193 : i_conindid = PQfnumber(res, "conindid");
8284 : 193 : i_condef = PQfnumber(res, "condef");
8285 : :
8286 : 193 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8287 : :
8288 : 193 : curtblindx = -1;
8289 [ + + ]: 370 : for (int j = 0; j < ntups; j++)
8290 : : {
8291 : 177 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
8292 : : TableInfo *reftable;
8293 : :
8294 : : /*
8295 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8296 : : * order.
8297 : : */
8298 [ + + + + ]: 177 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != conrelid)
8299 : : {
8300 [ + - ]: 15501 : while (++curtblindx < numTables)
8301 : : {
8302 : 15501 : tbinfo = &tblinfo[curtblindx];
8303 [ + + ]: 15501 : if (tbinfo->dobj.catId.oid == conrelid)
8304 : 167 : break;
8305 : : }
8306 [ - + ]: 167 : if (curtblindx >= numTables)
8307 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
8308 : : }
8309 : :
8310 : 177 : constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
8311 : 177 : constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8312 : 177 : constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8313 : 177 : AssignDumpId(&constrinfo[j].dobj);
8314 : 177 : constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8315 : 177 : constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8316 : 177 : constrinfo[j].contable = tbinfo;
8317 : 177 : constrinfo[j].condomain = NULL;
8318 : 177 : constrinfo[j].contype = 'f';
8319 : 177 : constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
8320 : 177 : constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid));
8321 : 177 : constrinfo[j].conindex = 0;
8322 : 177 : constrinfo[j].condeferrable = false;
8323 : 177 : constrinfo[j].condeferred = false;
8324 : 177 : constrinfo[j].conislocal = true;
8325 : 177 : constrinfo[j].separate = true;
8326 : :
8327 : : /*
8328 : : * Restoring an FK that points to a partitioned table requires that
8329 : : * all partition indexes have been attached beforehand. Ensure that
8330 : : * happens by making the constraint depend on each index partition
8331 : : * attach object.
8332 : : */
8333 : 177 : reftable = findTableByOid(constrinfo[j].confrelid);
8334 [ + - + + ]: 177 : if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
8335 : : {
8336 : 20 : Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
8337 : :
8338 [ + - ]: 20 : if (indexOid != InvalidOid)
8339 : : {
8340 [ + - ]: 20 : for (int k = 0; k < reftable->numIndexes; k++)
8341 : : {
8342 : : IndxInfo *refidx;
8343 : :
8344 : : /* not our index? */
8345 [ - + ]: 20 : if (reftable->indexes[k].dobj.catId.oid != indexOid)
8346 : 0 : continue;
8347 : :
8348 : 20 : refidx = &reftable->indexes[k];
8349 : 20 : addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
8350 : 20 : break;
8351 : : }
8352 : : }
8353 : : }
8354 : : }
8355 : :
8356 : 193 : PQclear(res);
8357 : :
8358 : 193 : destroyPQExpBuffer(query);
8359 : 193 : destroyPQExpBuffer(tbloids);
8360 : 193 : }
8361 : :
8362 : : /*
8363 : : * addConstrChildIdxDeps
8364 : : *
8365 : : * Recursive subroutine for getConstraints
8366 : : *
8367 : : * Given an object representing a foreign key constraint and an index on the
8368 : : * partitioned table it references, mark the constraint object as dependent
8369 : : * on the DO_INDEX_ATTACH object of each index partition, recursively
8370 : : * drilling down to their partitions if any. This ensures that the FK is not
8371 : : * restored until the index is fully marked valid.
8372 : : */
8373 : : static void
8374 : 45 : addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
8375 : : {
8376 : : SimplePtrListCell *cell;
8377 : :
8378 : : Assert(dobj->objType == DO_FK_CONSTRAINT);
8379 : :
8380 [ + + ]: 155 : for (cell = refidx->partattaches.head; cell; cell = cell->next)
8381 : : {
8382 : 110 : IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
8383 : :
8384 : 110 : addObjectDependency(dobj, attach->dobj.dumpId);
8385 : :
8386 [ + + ]: 110 : if (attach->partitionIdx->partattaches.head != NULL)
8387 : 25 : addConstrChildIdxDeps(dobj, attach->partitionIdx);
8388 : : }
8389 : 45 : }
8390 : :
8391 : : /*
8392 : : * getDomainConstraints
8393 : : *
8394 : : * Get info about constraints on a domain.
8395 : : */
8396 : : static void
8397 : 171 : getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
8398 : : {
8399 : : ConstraintInfo *constrinfo;
8400 : 171 : PQExpBuffer query = createPQExpBuffer();
8401 : : PGresult *res;
8402 : : int i_tableoid,
8403 : : i_oid,
8404 : : i_conname,
8405 : : i_consrc,
8406 : : i_convalidated,
8407 : : i_contype;
8408 : : int ntups;
8409 : :
8410 [ + + ]: 171 : if (!fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS])
8411 : : {
8412 : : /*
8413 : : * Set up query for constraint-specific details. For servers 17 and
8414 : : * up, domains have constraints of type 'n' as well as 'c', otherwise
8415 : : * just the latter.
8416 : : */
8417 : 46 : appendPQExpBuffer(query,
8418 : : "PREPARE getDomainConstraints(pg_catalog.oid) AS\n"
8419 : : "SELECT tableoid, oid, conname, "
8420 : : "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8421 : : "convalidated, contype "
8422 : : "FROM pg_catalog.pg_constraint "
8423 : : "WHERE contypid = $1 AND contype IN (%s) "
8424 : : "ORDER BY conname",
8425 [ - + ]: 46 : fout->remoteVersion < 170000 ? "'c'" : "'c', 'n'");
8426 : :
8427 : 46 : ExecuteSqlStatement(fout, query->data);
8428 : :
8429 : 46 : fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS] = true;
8430 : : }
8431 : :
8432 : 171 : printfPQExpBuffer(query,
8433 : : "EXECUTE getDomainConstraints('%u')",
8434 : : tyinfo->dobj.catId.oid);
8435 : :
8436 : 171 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8437 : :
8438 : 171 : ntups = PQntuples(res);
8439 : :
8440 : 171 : i_tableoid = PQfnumber(res, "tableoid");
8441 : 171 : i_oid = PQfnumber(res, "oid");
8442 : 171 : i_conname = PQfnumber(res, "conname");
8443 : 171 : i_consrc = PQfnumber(res, "consrc");
8444 : 171 : i_convalidated = PQfnumber(res, "convalidated");
8445 : 171 : i_contype = PQfnumber(res, "contype");
8446 : :
8447 : 171 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8448 : 171 : tyinfo->domChecks = constrinfo;
8449 : :
8450 : : /* 'i' tracks result rows; 'j' counts CHECK constraints */
8451 [ + + ]: 353 : for (int i = 0, j = 0; i < ntups; i++)
8452 : : {
8453 : 182 : bool validated = PQgetvalue(res, i, i_convalidated)[0] == 't';
8454 : 182 : char contype = (PQgetvalue(res, i, i_contype))[0];
8455 : : ConstraintInfo *constraint;
8456 : :
8457 [ + + ]: 182 : if (contype == CONSTRAINT_CHECK)
8458 : : {
8459 : 126 : constraint = &constrinfo[j++];
8460 : 126 : tyinfo->nDomChecks++;
8461 : : }
8462 : : else
8463 : : {
8464 : : Assert(contype == CONSTRAINT_NOTNULL);
8465 : : Assert(tyinfo->notnull == NULL);
8466 : : /* use last item in array for the not-null constraint */
8467 : 56 : tyinfo->notnull = &(constrinfo[ntups - 1]);
8468 : 56 : constraint = tyinfo->notnull;
8469 : : }
8470 : :
8471 : 182 : constraint->dobj.objType = DO_CONSTRAINT;
8472 : 182 : constraint->dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8473 : 182 : constraint->dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8474 : 182 : AssignDumpId(&(constraint->dobj));
8475 : 182 : constraint->dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
8476 : 182 : constraint->dobj.namespace = tyinfo->dobj.namespace;
8477 : 182 : constraint->contable = NULL;
8478 : 182 : constraint->condomain = tyinfo;
8479 : 182 : constraint->contype = contype;
8480 : 182 : constraint->condef = pg_strdup(PQgetvalue(res, i, i_consrc));
8481 : 182 : constraint->confrelid = InvalidOid;
8482 : 182 : constraint->conindex = 0;
8483 : 182 : constraint->condeferrable = false;
8484 : 182 : constraint->condeferred = false;
8485 : 182 : constraint->conislocal = true;
8486 : :
8487 : 182 : constraint->separate = !validated;
8488 : :
8489 : : /*
8490 : : * Make the domain depend on the constraint, ensuring it won't be
8491 : : * output till any constraint dependencies are OK. If the constraint
8492 : : * has not been validated, it's going to be dumped after the domain
8493 : : * anyway, so this doesn't matter.
8494 : : */
8495 [ + + ]: 182 : if (validated)
8496 : 177 : addObjectDependency(&tyinfo->dobj, constraint->dobj.dumpId);
8497 : : }
8498 : :
8499 : 171 : PQclear(res);
8500 : :
8501 : 171 : destroyPQExpBuffer(query);
8502 : 171 : }
8503 : :
8504 : : /*
8505 : : * getRules
8506 : : * get basic information about every rule in the system
8507 : : */
8508 : : void
8509 : 193 : getRules(Archive *fout)
8510 : : {
8511 : : PGresult *res;
8512 : : int ntups;
8513 : : int i;
8514 : 193 : PQExpBuffer query = createPQExpBuffer();
8515 : : RuleInfo *ruleinfo;
8516 : : int i_tableoid;
8517 : : int i_oid;
8518 : : int i_rulename;
8519 : : int i_ruletable;
8520 : : int i_ev_type;
8521 : : int i_is_instead;
8522 : : int i_ev_enabled;
8523 : :
8524 : 193 : appendPQExpBufferStr(query, "SELECT "
8525 : : "tableoid, oid, rulename, "
8526 : : "ev_class AS ruletable, ev_type, is_instead, "
8527 : : "ev_enabled "
8528 : : "FROM pg_rewrite "
8529 : : "ORDER BY oid");
8530 : :
8531 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8532 : :
8533 : 193 : ntups = PQntuples(res);
8534 : :
8535 : 193 : ruleinfo = pg_malloc_array(RuleInfo, ntups);
8536 : :
8537 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8538 : 193 : i_oid = PQfnumber(res, "oid");
8539 : 193 : i_rulename = PQfnumber(res, "rulename");
8540 : 193 : i_ruletable = PQfnumber(res, "ruletable");
8541 : 193 : i_ev_type = PQfnumber(res, "ev_type");
8542 : 193 : i_is_instead = PQfnumber(res, "is_instead");
8543 : 193 : i_ev_enabled = PQfnumber(res, "ev_enabled");
8544 : :
8545 [ + + ]: 31281 : for (i = 0; i < ntups; i++)
8546 : : {
8547 : : Oid ruletableoid;
8548 : :
8549 : 31088 : ruleinfo[i].dobj.objType = DO_RULE;
8550 : 31088 : ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8551 : 31088 : ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8552 : 31088 : AssignDumpId(&ruleinfo[i].dobj);
8553 : 31088 : ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename));
8554 : 31088 : ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
8555 : 31088 : ruleinfo[i].ruletable = findTableByOid(ruletableoid);
8556 [ - + ]: 31088 : if (ruleinfo[i].ruletable == NULL)
8557 : 0 : pg_fatal("failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found",
8558 : : ruletableoid, ruleinfo[i].dobj.catId.oid);
8559 : 31088 : ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace;
8560 : 31088 : ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump;
8561 : 31088 : ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
8562 : 31088 : ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
8563 : 31088 : ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled));
8564 [ + - ]: 31088 : if (ruleinfo[i].ruletable)
8565 : : {
8566 : : /*
8567 : : * If the table is a view or materialized view, force its ON
8568 : : * SELECT rule to be sorted before the view itself --- this
8569 : : * ensures that any dependencies for the rule affect the table's
8570 : : * positioning. Other rules are forced to appear after their
8571 : : * table.
8572 : : */
8573 [ + + ]: 31088 : if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW ||
8574 [ + + ]: 726 : ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) &&
8575 [ + + + - ]: 30857 : ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
8576 : : {
8577 : 30429 : addObjectDependency(&ruleinfo[i].ruletable->dobj,
8578 : 30429 : ruleinfo[i].dobj.dumpId);
8579 : : /* We'll merge the rule into CREATE VIEW, if possible */
8580 : 30429 : ruleinfo[i].separate = false;
8581 : : }
8582 : : else
8583 : : {
8584 : 659 : addObjectDependency(&ruleinfo[i].dobj,
8585 : 659 : ruleinfo[i].ruletable->dobj.dumpId);
8586 : 659 : ruleinfo[i].separate = true;
8587 : : }
8588 : : }
8589 : : else
8590 : 0 : ruleinfo[i].separate = true;
8591 : : }
8592 : :
8593 : 193 : PQclear(res);
8594 : :
8595 : 193 : destroyPQExpBuffer(query);
8596 : 193 : }
8597 : :
8598 : : /*
8599 : : * getTriggers
8600 : : * get information about every trigger on a dumpable table
8601 : : *
8602 : : * Note: trigger data is not returned directly to the caller, but it
8603 : : * does get entered into the DumpableObject tables.
8604 : : */
8605 : : void
8606 : 193 : getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
8607 : : {
8608 : 193 : PQExpBuffer query = createPQExpBuffer();
8609 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
8610 : : PGresult *res;
8611 : : int ntups;
8612 : : int curtblindx;
8613 : : TriggerInfo *tginfo;
8614 : : int i_tableoid,
8615 : : i_oid,
8616 : : i_tgrelid,
8617 : : i_tgname,
8618 : : i_tgenabled,
8619 : : i_tgispartition,
8620 : : i_tgdef;
8621 : :
8622 : : /*
8623 : : * We want to perform just one query against pg_trigger. However, we
8624 : : * mustn't try to select every row of the catalog and then sort it out on
8625 : : * the client side, because some of the server-side functions we need
8626 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8627 : : * build an array of the OIDs of tables we care about (and now have lock
8628 : : * on!), and use a WHERE clause to constrain which rows are selected.
8629 : : */
8630 : 193 : appendPQExpBufferChar(tbloids, '{');
8631 [ + + ]: 52302 : for (int i = 0; i < numTables; i++)
8632 : : {
8633 : 52109 : TableInfo *tbinfo = &tblinfo[i];
8634 : :
8635 [ + + ]: 52109 : if (!tbinfo->hastriggers ||
8636 [ + + ]: 1141 : !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8637 : 51234 : continue;
8638 : :
8639 : : /* OK, we need info for this table */
8640 [ + + ]: 875 : if (tbloids->len > 1) /* do we have more than the '{'? */
8641 : 821 : appendPQExpBufferChar(tbloids, ',');
8642 : 875 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
8643 : : }
8644 : 193 : appendPQExpBufferChar(tbloids, '}');
8645 : :
8646 [ + - ]: 193 : if (fout->remoteVersion >= 150000)
8647 : : {
8648 : : /*
8649 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8650 : : * result in non-forward-compatible dumps of WHEN clauses due to
8651 : : * under-parenthesization.
8652 : : *
8653 : : * NB: We need to see partition triggers in case the tgenabled flag
8654 : : * has been changed from the parent.
8655 : : */
8656 : 193 : appendPQExpBuffer(query,
8657 : : "SELECT t.tgrelid, t.tgname, "
8658 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8659 : : "t.tgenabled, t.tableoid, t.oid, "
8660 : : "t.tgparentid <> 0 AS tgispartition\n"
8661 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8662 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8663 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8664 : : "WHERE ((NOT t.tgisinternal AND t.tgparentid = 0) "
8665 : : "OR t.tgenabled != u.tgenabled) "
8666 : : "ORDER BY t.tgrelid, t.tgname",
8667 : : tbloids->data);
8668 : : }
8669 [ # # ]: 0 : else if (fout->remoteVersion >= 130000)
8670 : : {
8671 : : /*
8672 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8673 : : * result in non-forward-compatible dumps of WHEN clauses due to
8674 : : * under-parenthesization.
8675 : : *
8676 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8677 : : * tgenabled flag has been changed from the parent.
8678 : : */
8679 : 0 : appendPQExpBuffer(query,
8680 : : "SELECT t.tgrelid, t.tgname, "
8681 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8682 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition\n"
8683 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8684 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8685 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8686 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != u.tgenabled) "
8687 : : "ORDER BY t.tgrelid, t.tgname",
8688 : : tbloids->data);
8689 : : }
8690 [ # # ]: 0 : else if (fout->remoteVersion >= 110000)
8691 : : {
8692 : : /*
8693 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8694 : : * tgenabled flag has been changed from the parent. No tgparentid in
8695 : : * version 11-12, so we have to match them via pg_depend.
8696 : : *
8697 : : * See above about pretty=true in pg_get_triggerdef.
8698 : : */
8699 : 0 : appendPQExpBuffer(query,
8700 : : "SELECT t.tgrelid, t.tgname, "
8701 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8702 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition "
8703 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8704 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8705 : : "LEFT JOIN pg_catalog.pg_depend AS d ON "
8706 : : " d.classid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8707 : : " d.refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8708 : : " d.objid = t.oid "
8709 : : "LEFT JOIN pg_catalog.pg_trigger AS pt ON pt.oid = refobjid "
8710 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != pt.tgenabled) "
8711 : : "ORDER BY t.tgrelid, t.tgname",
8712 : : tbloids->data);
8713 : : }
8714 : : else
8715 : : {
8716 : : /* See above about pretty=true in pg_get_triggerdef */
8717 : 0 : appendPQExpBuffer(query,
8718 : : "SELECT t.tgrelid, t.tgname, "
8719 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8720 : : "t.tgenabled, false as tgispartition, "
8721 : : "t.tableoid, t.oid "
8722 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8723 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8724 : : "WHERE NOT tgisinternal "
8725 : : "ORDER BY t.tgrelid, t.tgname",
8726 : : tbloids->data);
8727 : : }
8728 : :
8729 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8730 : :
8731 : 193 : ntups = PQntuples(res);
8732 : :
8733 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8734 : 193 : i_oid = PQfnumber(res, "oid");
8735 : 193 : i_tgrelid = PQfnumber(res, "tgrelid");
8736 : 193 : i_tgname = PQfnumber(res, "tgname");
8737 : 193 : i_tgenabled = PQfnumber(res, "tgenabled");
8738 : 193 : i_tgispartition = PQfnumber(res, "tgispartition");
8739 : 193 : i_tgdef = PQfnumber(res, "tgdef");
8740 : :
8741 : 193 : tginfo = pg_malloc_array(TriggerInfo, ntups);
8742 : :
8743 : : /*
8744 : : * Outer loop iterates once per table, not once per row. Incrementing of
8745 : : * j is handled by the inner loop.
8746 : : */
8747 : 193 : curtblindx = -1;
8748 [ + + ]: 511 : for (int j = 0; j < ntups;)
8749 : : {
8750 : 318 : Oid tgrelid = atooid(PQgetvalue(res, j, i_tgrelid));
8751 : 318 : TableInfo *tbinfo = NULL;
8752 : : int numtrigs;
8753 : :
8754 : : /* Count rows for this table */
8755 [ + + ]: 535 : for (numtrigs = 1; numtrigs < ntups - j; numtrigs++)
8756 [ + + ]: 481 : if (atooid(PQgetvalue(res, j + numtrigs, i_tgrelid)) != tgrelid)
8757 : 264 : break;
8758 : :
8759 : : /*
8760 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8761 : : * order.
8762 : : */
8763 [ + - ]: 17138 : while (++curtblindx < numTables)
8764 : : {
8765 : 17138 : tbinfo = &tblinfo[curtblindx];
8766 [ + + ]: 17138 : if (tbinfo->dobj.catId.oid == tgrelid)
8767 : 318 : break;
8768 : : }
8769 [ - + ]: 318 : if (curtblindx >= numTables)
8770 : 0 : pg_fatal("unrecognized table OID %u", tgrelid);
8771 : :
8772 : : /* Save data for this table */
8773 [ + + ]: 853 : for (int c = 0; c < numtrigs; c++, j++)
8774 : : {
8775 : 535 : tginfo[j].dobj.objType = DO_TRIGGER;
8776 : 535 : tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8777 : 535 : tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8778 : 535 : AssignDumpId(&tginfo[j].dobj);
8779 : 535 : tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname));
8780 : 535 : tginfo[j].dobj.namespace = tbinfo->dobj.namespace;
8781 : 535 : tginfo[j].tgtable = tbinfo;
8782 : 535 : tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled));
8783 : 535 : tginfo[j].tgispartition = *(PQgetvalue(res, j, i_tgispartition)) == 't';
8784 : 535 : tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef));
8785 : : }
8786 : : }
8787 : :
8788 : 193 : PQclear(res);
8789 : :
8790 : 193 : destroyPQExpBuffer(query);
8791 : 193 : destroyPQExpBuffer(tbloids);
8792 : 193 : }
8793 : :
8794 : : /*
8795 : : * getEventTriggers
8796 : : * get information about event triggers
8797 : : */
8798 : : void
8799 : 193 : getEventTriggers(Archive *fout)
8800 : : {
8801 : : int i;
8802 : : PQExpBuffer query;
8803 : : PGresult *res;
8804 : : EventTriggerInfo *evtinfo;
8805 : : int i_tableoid,
8806 : : i_oid,
8807 : : i_evtname,
8808 : : i_evtevent,
8809 : : i_evtowner,
8810 : : i_evttags,
8811 : : i_evtfname,
8812 : : i_evtenabled;
8813 : : int ntups;
8814 : :
8815 : 193 : query = createPQExpBuffer();
8816 : :
8817 : 193 : appendPQExpBufferStr(query,
8818 : : "SELECT e.tableoid, e.oid, evtname, evtenabled, "
8819 : : "evtevent, evtowner, "
8820 : : "array_to_string(array("
8821 : : "select quote_literal(x) "
8822 : : " from unnest(evttags) as t(x)), ', ') as evttags, "
8823 : : "e.evtfoid::regproc as evtfname "
8824 : : "FROM pg_event_trigger e "
8825 : : "ORDER BY e.oid");
8826 : :
8827 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8828 : :
8829 : 193 : ntups = PQntuples(res);
8830 : :
8831 : 193 : evtinfo = pg_malloc_array(EventTriggerInfo, ntups);
8832 : :
8833 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8834 : 193 : i_oid = PQfnumber(res, "oid");
8835 : 193 : i_evtname = PQfnumber(res, "evtname");
8836 : 193 : i_evtevent = PQfnumber(res, "evtevent");
8837 : 193 : i_evtowner = PQfnumber(res, "evtowner");
8838 : 193 : i_evttags = PQfnumber(res, "evttags");
8839 : 193 : i_evtfname = PQfnumber(res, "evtfname");
8840 : 193 : i_evtenabled = PQfnumber(res, "evtenabled");
8841 : :
8842 [ + + ]: 248 : for (i = 0; i < ntups; i++)
8843 : : {
8844 : 55 : evtinfo[i].dobj.objType = DO_EVENT_TRIGGER;
8845 : 55 : evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8846 : 55 : evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8847 : 55 : AssignDumpId(&evtinfo[i].dobj);
8848 : 55 : evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname));
8849 : 55 : evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname));
8850 : 55 : evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent));
8851 : 55 : evtinfo[i].evtowner = getRoleName(PQgetvalue(res, i, i_evtowner));
8852 : 55 : evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags));
8853 : 55 : evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname));
8854 : 55 : evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled));
8855 : :
8856 : : /* Decide whether we want to dump it */
8857 : 55 : selectDumpableObject(&(evtinfo[i].dobj), fout);
8858 : : }
8859 : :
8860 : 193 : PQclear(res);
8861 : :
8862 : 193 : destroyPQExpBuffer(query);
8863 : 193 : }
8864 : :
8865 : : /*
8866 : : * getProcLangs
8867 : : * get basic information about every procedural language in the system
8868 : : *
8869 : : * NB: this must run after getFuncs() because we assume we can do
8870 : : * findFuncByOid().
8871 : : */
8872 : : void
8873 : 193 : getProcLangs(Archive *fout)
8874 : : {
8875 : : PGresult *res;
8876 : : int ntups;
8877 : : int i;
8878 : 193 : PQExpBuffer query = createPQExpBuffer();
8879 : : ProcLangInfo *planginfo;
8880 : : int i_tableoid;
8881 : : int i_oid;
8882 : : int i_lanname;
8883 : : int i_lanpltrusted;
8884 : : int i_lanplcallfoid;
8885 : : int i_laninline;
8886 : : int i_lanvalidator;
8887 : : int i_lanacl;
8888 : : int i_acldefault;
8889 : : int i_lanowner;
8890 : :
8891 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
8892 : : "lanname, lanpltrusted, lanplcallfoid, "
8893 : : "laninline, lanvalidator, "
8894 : : "lanacl, "
8895 : : "acldefault('l', lanowner) AS acldefault, "
8896 : : "lanowner "
8897 : : "FROM pg_language "
8898 : : "WHERE lanispl "
8899 : : "ORDER BY oid");
8900 : :
8901 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8902 : :
8903 : 193 : ntups = PQntuples(res);
8904 : :
8905 : 193 : planginfo = pg_malloc_array(ProcLangInfo, ntups);
8906 : :
8907 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8908 : 193 : i_oid = PQfnumber(res, "oid");
8909 : 193 : i_lanname = PQfnumber(res, "lanname");
8910 : 193 : i_lanpltrusted = PQfnumber(res, "lanpltrusted");
8911 : 193 : i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
8912 : 193 : i_laninline = PQfnumber(res, "laninline");
8913 : 193 : i_lanvalidator = PQfnumber(res, "lanvalidator");
8914 : 193 : i_lanacl = PQfnumber(res, "lanacl");
8915 : 193 : i_acldefault = PQfnumber(res, "acldefault");
8916 : 193 : i_lanowner = PQfnumber(res, "lanowner");
8917 : :
8918 [ + + ]: 434 : for (i = 0; i < ntups; i++)
8919 : : {
8920 : 241 : planginfo[i].dobj.objType = DO_PROCLANG;
8921 : 241 : planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8922 : 241 : planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8923 : 241 : AssignDumpId(&planginfo[i].dobj);
8924 : :
8925 : 241 : planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
8926 : 241 : planginfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_lanacl));
8927 : 241 : planginfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
8928 : 241 : planginfo[i].dacl.privtype = 0;
8929 : 241 : planginfo[i].dacl.initprivs = NULL;
8930 : 241 : planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
8931 : 241 : planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
8932 : 241 : planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
8933 : 241 : planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
8934 : 241 : planginfo[i].lanowner = getRoleName(PQgetvalue(res, i, i_lanowner));
8935 : :
8936 : : /* Decide whether we want to dump it */
8937 : 241 : selectDumpableProcLang(&(planginfo[i]), fout);
8938 : :
8939 : : /* Mark whether language has an ACL */
8940 [ + + ]: 241 : if (!PQgetisnull(res, i, i_lanacl))
8941 : 48 : planginfo[i].dobj.components |= DUMP_COMPONENT_ACL;
8942 : : }
8943 : :
8944 : 193 : PQclear(res);
8945 : :
8946 : 193 : destroyPQExpBuffer(query);
8947 : 193 : }
8948 : :
8949 : : /*
8950 : : * getCasts
8951 : : * get basic information about most casts in the system
8952 : : *
8953 : : * Skip casts from a range to its multirange, since we'll create those
8954 : : * automatically.
8955 : : */
8956 : : void
8957 : 193 : getCasts(Archive *fout)
8958 : : {
8959 : : PGresult *res;
8960 : : int ntups;
8961 : : int i;
8962 : 193 : PQExpBuffer query = createPQExpBuffer();
8963 : : CastInfo *castinfo;
8964 : : int i_tableoid;
8965 : : int i_oid;
8966 : : int i_castsource;
8967 : : int i_casttarget;
8968 : : int i_castfunc;
8969 : : int i_castcontext;
8970 : : int i_castmethod;
8971 : :
8972 [ + - ]: 193 : if (fout->remoteVersion >= 140000)
8973 : : {
8974 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
8975 : : "castsource, casttarget, castfunc, castcontext, "
8976 : : "castmethod "
8977 : : "FROM pg_cast c "
8978 : : "WHERE NOT EXISTS ( "
8979 : : "SELECT 1 FROM pg_range r "
8980 : : "WHERE c.castsource = r.rngtypid "
8981 : : "AND c.casttarget = r.rngmultitypid "
8982 : : ") "
8983 : : "ORDER BY 3,4");
8984 : : }
8985 : : else
8986 : : {
8987 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
8988 : : "castsource, casttarget, castfunc, castcontext, "
8989 : : "castmethod "
8990 : : "FROM pg_cast ORDER BY 3,4");
8991 : : }
8992 : :
8993 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8994 : :
8995 : 193 : ntups = PQntuples(res);
8996 : :
8997 : 193 : castinfo = pg_malloc_array(CastInfo, ntups);
8998 : :
8999 : 193 : i_tableoid = PQfnumber(res, "tableoid");
9000 : 193 : i_oid = PQfnumber(res, "oid");
9001 : 193 : i_castsource = PQfnumber(res, "castsource");
9002 : 193 : i_casttarget = PQfnumber(res, "casttarget");
9003 : 193 : i_castfunc = PQfnumber(res, "castfunc");
9004 : 193 : i_castcontext = PQfnumber(res, "castcontext");
9005 : 193 : i_castmethod = PQfnumber(res, "castmethod");
9006 : :
9007 [ + + ]: 47182 : for (i = 0; i < ntups; i++)
9008 : : {
9009 : : PQExpBufferData namebuf;
9010 : : TypeInfo *sTypeInfo;
9011 : : TypeInfo *tTypeInfo;
9012 : :
9013 : 46989 : castinfo[i].dobj.objType = DO_CAST;
9014 : 46989 : castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9015 : 46989 : castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9016 : 46989 : AssignDumpId(&castinfo[i].dobj);
9017 : 46989 : castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
9018 : 46989 : castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
9019 : 46989 : castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
9020 : 46989 : castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
9021 : 46989 : castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod));
9022 : :
9023 : : /*
9024 : : * Try to name cast as concatenation of typnames. This is only used
9025 : : * for purposes of sorting. If we fail to find either type, the name
9026 : : * will be an empty string.
9027 : : */
9028 : 46989 : initPQExpBuffer(&namebuf);
9029 : 46989 : sTypeInfo = findTypeByOid(castinfo[i].castsource);
9030 : 46989 : tTypeInfo = findTypeByOid(castinfo[i].casttarget);
9031 [ + - + - ]: 46989 : if (sTypeInfo && tTypeInfo)
9032 : 46989 : appendPQExpBuffer(&namebuf, "%s %s",
9033 : : sTypeInfo->dobj.name, tTypeInfo->dobj.name);
9034 : 46989 : castinfo[i].dobj.name = namebuf.data;
9035 : :
9036 : : /* Decide whether we want to dump it */
9037 : 46989 : selectDumpableCast(&(castinfo[i]), fout);
9038 : : }
9039 : :
9040 : 193 : PQclear(res);
9041 : :
9042 : 193 : destroyPQExpBuffer(query);
9043 : 193 : }
9044 : :
9045 : : static char *
9046 : 93 : get_language_name(Archive *fout, Oid langid)
9047 : : {
9048 : : PQExpBuffer query;
9049 : : PGresult *res;
9050 : : char *lanname;
9051 : :
9052 : 93 : query = createPQExpBuffer();
9053 : 93 : appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid);
9054 : 93 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
9055 : 93 : lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
9056 : 93 : destroyPQExpBuffer(query);
9057 : 93 : PQclear(res);
9058 : :
9059 : 93 : return lanname;
9060 : : }
9061 : :
9062 : : /*
9063 : : * getTransforms
9064 : : * get basic information about every transform in the system
9065 : : */
9066 : : void
9067 : 193 : getTransforms(Archive *fout)
9068 : : {
9069 : : PGresult *res;
9070 : : int ntups;
9071 : : int i;
9072 : : PQExpBuffer query;
9073 : : TransformInfo *transforminfo;
9074 : : int i_tableoid;
9075 : : int i_oid;
9076 : : int i_trftype;
9077 : : int i_trflang;
9078 : : int i_trffromsql;
9079 : : int i_trftosql;
9080 : :
9081 : 193 : query = createPQExpBuffer();
9082 : :
9083 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9084 : : "trftype, trflang, trffromsql::oid, trftosql::oid "
9085 : : "FROM pg_transform "
9086 : : "ORDER BY 3,4");
9087 : :
9088 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9089 : :
9090 : 193 : ntups = PQntuples(res);
9091 : :
9092 : 193 : transforminfo = pg_malloc_array(TransformInfo, ntups);
9093 : :
9094 : 193 : i_tableoid = PQfnumber(res, "tableoid");
9095 : 193 : i_oid = PQfnumber(res, "oid");
9096 : 193 : i_trftype = PQfnumber(res, "trftype");
9097 : 193 : i_trflang = PQfnumber(res, "trflang");
9098 : 193 : i_trffromsql = PQfnumber(res, "trffromsql");
9099 : 193 : i_trftosql = PQfnumber(res, "trftosql");
9100 : :
9101 [ + + ]: 248 : for (i = 0; i < ntups; i++)
9102 : : {
9103 : : PQExpBufferData namebuf;
9104 : : TypeInfo *typeInfo;
9105 : : char *lanname;
9106 : :
9107 : 55 : transforminfo[i].dobj.objType = DO_TRANSFORM;
9108 : 55 : transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9109 : 55 : transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9110 : 55 : AssignDumpId(&transforminfo[i].dobj);
9111 : 55 : transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype));
9112 : 55 : transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang));
9113 : 55 : transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql));
9114 : 55 : transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql));
9115 : :
9116 : : /*
9117 : : * Try to name transform as concatenation of type and language name.
9118 : : * This is only used for purposes of sorting. If we fail to find
9119 : : * either, the name will be an empty string.
9120 : : */
9121 : 55 : initPQExpBuffer(&namebuf);
9122 : 55 : typeInfo = findTypeByOid(transforminfo[i].trftype);
9123 : 55 : lanname = get_language_name(fout, transforminfo[i].trflang);
9124 [ + - + - ]: 55 : if (typeInfo && lanname)
9125 : 55 : appendPQExpBuffer(&namebuf, "%s %s",
9126 : : typeInfo->dobj.name, lanname);
9127 : 55 : transforminfo[i].dobj.name = namebuf.data;
9128 : 55 : free(lanname);
9129 : :
9130 : : /* Decide whether we want to dump it */
9131 : 55 : selectDumpableObject(&(transforminfo[i].dobj), fout);
9132 : : }
9133 : :
9134 : 193 : PQclear(res);
9135 : :
9136 : 193 : destroyPQExpBuffer(query);
9137 : 193 : }
9138 : :
9139 : : /*
9140 : : * getTableAttrs -
9141 : : * for each interesting table, read info about its attributes
9142 : : * (names, types, default values, CHECK constraints, etc)
9143 : : *
9144 : : * modifies tblinfo
9145 : : */
9146 : : void
9147 : 193 : getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
9148 : : {
9149 : 193 : DumpOptions *dopt = fout->dopt;
9150 : 193 : PQExpBuffer q = createPQExpBuffer();
9151 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
9152 : 193 : PQExpBuffer checkoids = createPQExpBuffer();
9153 : 193 : PQExpBuffer invalidnotnulloids = NULL;
9154 : : PGresult *res;
9155 : : int ntups;
9156 : : int curtblindx;
9157 : : int i_attrelid;
9158 : : int i_attnum;
9159 : : int i_attname;
9160 : : int i_atttypname;
9161 : : int i_attstattarget;
9162 : : int i_attstorage;
9163 : : int i_typstorage;
9164 : : int i_attidentity;
9165 : : int i_attgenerated;
9166 : : int i_attisdropped;
9167 : : int i_attlen;
9168 : : int i_attalign;
9169 : : int i_attislocal;
9170 : : int i_notnull_name;
9171 : : int i_notnull_comment;
9172 : : int i_notnull_noinherit;
9173 : : int i_notnull_islocal;
9174 : : int i_notnull_invalidoid;
9175 : : int i_attoptions;
9176 : : int i_attcollation;
9177 : : int i_attcompression;
9178 : : int i_attfdwoptions;
9179 : : int i_attmissingval;
9180 : : int i_atthasdef;
9181 : :
9182 : : /*
9183 : : * We want to perform just one query against pg_attribute, and then just
9184 : : * one against pg_attrdef (for DEFAULTs) and two against pg_constraint
9185 : : * (for CHECK constraints and for NOT NULL constraints). However, we
9186 : : * mustn't try to select every row of those catalogs and then sort it out
9187 : : * on the client side, because some of the server-side functions we need
9188 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
9189 : : * build an array of the OIDs of tables we care about (and now have lock
9190 : : * on!), and use a WHERE clause to constrain which rows are selected.
9191 : : */
9192 : 193 : appendPQExpBufferChar(tbloids, '{');
9193 : 193 : appendPQExpBufferChar(checkoids, '{');
9194 [ + + ]: 52302 : for (int i = 0; i < numTables; i++)
9195 : : {
9196 : 52109 : TableInfo *tbinfo = &tblinfo[i];
9197 : :
9198 : : /* Don't bother to collect info for sequences */
9199 [ + + ]: 52109 : if (tbinfo->relkind == RELKIND_SEQUENCE)
9200 : 647 : continue;
9201 : :
9202 : : /*
9203 : : * Don't bother with uninteresting tables, either. For binary
9204 : : * upgrades, this is bypassed for pg_largeobject_metadata and
9205 : : * pg_shdepend so that the columns names are collected for the
9206 : : * corresponding COPY commands. Restoring the data for those catalogs
9207 : : * is faster than restoring the equivalent set of large object
9208 : : * commands.
9209 : : */
9210 [ + + ]: 51462 : if (!tbinfo->interesting &&
9211 [ + + ]: 44510 : !(fout->dopt->binary_upgrade &&
9212 [ + + ]: 9300 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9213 [ + + ]: 9258 : tbinfo->dobj.catId.oid == SharedDependRelationId)))
9214 : 44426 : continue;
9215 : :
9216 : : /* OK, we need info for this table */
9217 [ + + ]: 7036 : if (tbloids->len > 1) /* do we have more than the '{'? */
9218 : 6885 : appendPQExpBufferChar(tbloids, ',');
9219 : 7036 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9220 : :
9221 [ + + ]: 7036 : if (tbinfo->ncheck > 0)
9222 : : {
9223 : : /* Also make a list of the ones with check constraints */
9224 [ + + ]: 548 : if (checkoids->len > 1) /* do we have more than the '{'? */
9225 : 476 : appendPQExpBufferChar(checkoids, ',');
9226 : 548 : appendPQExpBuffer(checkoids, "%u", tbinfo->dobj.catId.oid);
9227 : : }
9228 : : }
9229 : 193 : appendPQExpBufferChar(tbloids, '}');
9230 : 193 : appendPQExpBufferChar(checkoids, '}');
9231 : :
9232 : : /*
9233 : : * Find all the user attributes and their types.
9234 : : *
9235 : : * Since we only want to dump COLLATE clauses for attributes whose
9236 : : * collation is different from their type's default, we use a CASE here to
9237 : : * suppress uninteresting attcollations cheaply.
9238 : : */
9239 : 193 : appendPQExpBufferStr(q,
9240 : : "SELECT\n"
9241 : : "a.attrelid,\n"
9242 : : "a.attnum,\n"
9243 : : "a.attname,\n"
9244 : : "a.attstattarget,\n"
9245 : : "a.attstorage,\n"
9246 : : "t.typstorage,\n"
9247 : : "a.atthasdef,\n"
9248 : : "a.attisdropped,\n"
9249 : : "a.attlen,\n"
9250 : : "a.attalign,\n"
9251 : : "a.attislocal,\n"
9252 : : "pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n"
9253 : : "array_to_string(a.attoptions, ', ') AS attoptions,\n"
9254 : : "CASE WHEN a.attcollation <> t.typcollation "
9255 : : "THEN a.attcollation ELSE 0 END AS attcollation,\n"
9256 : : "pg_catalog.array_to_string(ARRAY("
9257 : : "SELECT pg_catalog.quote_ident(option_name) || "
9258 : : "' ' || pg_catalog.quote_literal(option_value) "
9259 : : "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
9260 : : "ORDER BY option_name"
9261 : : "), E',\n ') AS attfdwoptions,\n");
9262 : :
9263 : : /*
9264 : : * Find out any NOT NULL markings for each column. In 18 and up we read
9265 : : * pg_constraint to obtain the constraint name, and for valid constraints
9266 : : * also pg_description to obtain its comment. notnull_noinherit is set
9267 : : * according to the NO INHERIT property. For versions prior to 18, we
9268 : : * store an empty string as the name when a constraint is marked as
9269 : : * attnotnull (this cues dumpTableSchema to print the NOT NULL clause
9270 : : * without a name); also, such cases are never NO INHERIT.
9271 : : *
9272 : : * For invalid constraints, we need to store their OIDs for processing
9273 : : * elsewhere, so we bring the pg_constraint.oid value when the constraint
9274 : : * is invalid, and NULL otherwise. Their comments are handled not here
9275 : : * but by collectComments, because they're their own dumpable object.
9276 : : *
9277 : : * We track in notnull_islocal whether the constraint was defined directly
9278 : : * in this table or via an ancestor, for binary upgrade. flagInhAttrs
9279 : : * might modify this later.
9280 : : */
9281 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
9282 : 193 : appendPQExpBufferStr(q,
9283 : : "co.conname AS notnull_name,\n"
9284 : : "CASE WHEN co.convalidated THEN pt.description"
9285 : : " ELSE NULL END AS notnull_comment,\n"
9286 : : "CASE WHEN NOT co.convalidated THEN co.oid "
9287 : : "ELSE NULL END AS notnull_invalidoid,\n"
9288 : : "co.connoinherit AS notnull_noinherit,\n"
9289 : : "co.conislocal AS notnull_islocal,\n");
9290 : : else
9291 : 0 : appendPQExpBufferStr(q,
9292 : : "CASE WHEN a.attnotnull THEN '' ELSE NULL END AS notnull_name,\n"
9293 : : "NULL AS notnull_comment,\n"
9294 : : "NULL AS notnull_invalidoid,\n"
9295 : : "false AS notnull_noinherit,\n"
9296 : : "CASE WHEN a.attislocal THEN true\n"
9297 : : " WHEN a.attnotnull AND NOT a.attislocal THEN true\n"
9298 : : " ELSE false\n"
9299 : : "END AS notnull_islocal,\n");
9300 : :
9301 [ + - ]: 193 : if (fout->remoteVersion >= 140000)
9302 : 193 : appendPQExpBufferStr(q,
9303 : : "a.attcompression AS attcompression,\n");
9304 : : else
9305 : 0 : appendPQExpBufferStr(q,
9306 : : "'' AS attcompression,\n");
9307 : :
9308 : 193 : appendPQExpBufferStr(q,
9309 : : "a.attidentity,\n");
9310 : :
9311 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
9312 : 193 : appendPQExpBufferStr(q,
9313 : : "CASE WHEN a.atthasmissing AND NOT a.attisdropped "
9314 : : "THEN a.attmissingval ELSE null END AS attmissingval,\n");
9315 : : else
9316 : 0 : appendPQExpBufferStr(q,
9317 : : "NULL AS attmissingval,\n");
9318 : :
9319 [ + - ]: 193 : if (fout->remoteVersion >= 120000)
9320 : 193 : appendPQExpBufferStr(q,
9321 : : "a.attgenerated\n");
9322 : : else
9323 : 0 : appendPQExpBufferStr(q,
9324 : : "'' AS attgenerated\n");
9325 : :
9326 : : /* need left join to pg_type to not fail on dropped columns ... */
9327 : 193 : appendPQExpBuffer(q,
9328 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9329 : : "JOIN pg_catalog.pg_attribute a ON (src.tbloid = a.attrelid) "
9330 : : "LEFT JOIN pg_catalog.pg_type t "
9331 : : "ON (a.atttypid = t.oid)\n",
9332 : : tbloids->data);
9333 : :
9334 : : /*
9335 : : * In versions 18 and up, we need pg_constraint for explicit NOT NULL
9336 : : * entries and pg_description to get their comments.
9337 : : */
9338 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
9339 : 193 : appendPQExpBufferStr(q,
9340 : : " LEFT JOIN pg_catalog.pg_constraint co ON "
9341 : : "(a.attrelid = co.conrelid\n"
9342 : : " AND co.contype = 'n' AND "
9343 : : "co.conkey = array[a.attnum])\n"
9344 : : " LEFT JOIN pg_catalog.pg_description pt ON "
9345 : : "(pt.classoid = co.tableoid AND pt.objoid = co.oid)\n");
9346 : :
9347 : 193 : appendPQExpBufferStr(q,
9348 : : "WHERE a.attnum > 0::pg_catalog.int2\n");
9349 : :
9350 : : /*
9351 : : * For binary upgrades from <v12, be sure to pick up
9352 : : * pg_largeobject_metadata's oid column.
9353 : : */
9354 [ + + - + ]: 193 : if (fout->dopt->binary_upgrade && fout->remoteVersion < 120000)
9355 : 0 : appendPQExpBufferStr(q,
9356 : : "OR (a.attnum = -2::pg_catalog.int2 AND src.tbloid = "
9357 : : CppAsString2(LargeObjectMetadataRelationId) ")\n");
9358 : :
9359 : 193 : appendPQExpBufferStr(q,
9360 : : "ORDER BY a.attrelid, a.attnum");
9361 : :
9362 : 193 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9363 : :
9364 : 193 : ntups = PQntuples(res);
9365 : :
9366 : 193 : i_attrelid = PQfnumber(res, "attrelid");
9367 : 193 : i_attnum = PQfnumber(res, "attnum");
9368 : 193 : i_attname = PQfnumber(res, "attname");
9369 : 193 : i_atttypname = PQfnumber(res, "atttypname");
9370 : 193 : i_attstattarget = PQfnumber(res, "attstattarget");
9371 : 193 : i_attstorage = PQfnumber(res, "attstorage");
9372 : 193 : i_typstorage = PQfnumber(res, "typstorage");
9373 : 193 : i_attidentity = PQfnumber(res, "attidentity");
9374 : 193 : i_attgenerated = PQfnumber(res, "attgenerated");
9375 : 193 : i_attisdropped = PQfnumber(res, "attisdropped");
9376 : 193 : i_attlen = PQfnumber(res, "attlen");
9377 : 193 : i_attalign = PQfnumber(res, "attalign");
9378 : 193 : i_attislocal = PQfnumber(res, "attislocal");
9379 : 193 : i_notnull_name = PQfnumber(res, "notnull_name");
9380 : 193 : i_notnull_comment = PQfnumber(res, "notnull_comment");
9381 : 193 : i_notnull_invalidoid = PQfnumber(res, "notnull_invalidoid");
9382 : 193 : i_notnull_noinherit = PQfnumber(res, "notnull_noinherit");
9383 : 193 : i_notnull_islocal = PQfnumber(res, "notnull_islocal");
9384 : 193 : i_attoptions = PQfnumber(res, "attoptions");
9385 : 193 : i_attcollation = PQfnumber(res, "attcollation");
9386 : 193 : i_attcompression = PQfnumber(res, "attcompression");
9387 : 193 : i_attfdwoptions = PQfnumber(res, "attfdwoptions");
9388 : 193 : i_attmissingval = PQfnumber(res, "attmissingval");
9389 : 193 : i_atthasdef = PQfnumber(res, "atthasdef");
9390 : :
9391 : : /* Within the next loop, we'll accumulate OIDs of tables with defaults */
9392 : 193 : resetPQExpBuffer(tbloids);
9393 : 193 : appendPQExpBufferChar(tbloids, '{');
9394 : :
9395 : : /*
9396 : : * Outer loop iterates once per table, not once per row. Incrementing of
9397 : : * r is handled by the inner loop.
9398 : : */
9399 : 193 : curtblindx = -1;
9400 [ + + ]: 7082 : for (int r = 0; r < ntups;)
9401 : : {
9402 : 6889 : Oid attrelid = atooid(PQgetvalue(res, r, i_attrelid));
9403 : 6889 : TableInfo *tbinfo = NULL;
9404 : : int numatts;
9405 : : bool hasdefaults;
9406 : :
9407 : : /* Count rows for this table */
9408 [ + + ]: 25734 : for (numatts = 1; numatts < ntups - r; numatts++)
9409 [ + + ]: 25586 : if (atooid(PQgetvalue(res, r + numatts, i_attrelid)) != attrelid)
9410 : 6741 : break;
9411 : :
9412 : : /*
9413 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
9414 : : * order.
9415 : : */
9416 [ + - ]: 36057 : while (++curtblindx < numTables)
9417 : : {
9418 : 36057 : tbinfo = &tblinfo[curtblindx];
9419 [ + + ]: 36057 : if (tbinfo->dobj.catId.oid == attrelid)
9420 : 6889 : break;
9421 : : }
9422 [ - + ]: 6889 : if (curtblindx >= numTables)
9423 : 0 : pg_fatal("unrecognized table OID %u", attrelid);
9424 : : /* cross-check that we only got requested tables */
9425 [ + - ]: 6889 : if (tbinfo->relkind == RELKIND_SEQUENCE ||
9426 [ + + ]: 6889 : (!tbinfo->interesting &&
9427 [ + - ]: 84 : !(fout->dopt->binary_upgrade &&
9428 [ + + ]: 84 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9429 [ - + ]: 42 : tbinfo->dobj.catId.oid == SharedDependRelationId))))
9430 : 0 : pg_fatal("unexpected column data for table \"%s\"",
9431 : : tbinfo->dobj.name);
9432 : :
9433 : : /* Save data for this table */
9434 : 6889 : tbinfo->numatts = numatts;
9435 : 6889 : tbinfo->attnames = pg_malloc_array(char *, numatts);
9436 : 6889 : tbinfo->atttypnames = pg_malloc_array(char *, numatts);
9437 : 6889 : tbinfo->attstattarget = pg_malloc_array(int, numatts);
9438 : 6889 : tbinfo->attstorage = pg_malloc_array(char, numatts);
9439 : 6889 : tbinfo->typstorage = pg_malloc_array(char, numatts);
9440 : 6889 : tbinfo->attidentity = pg_malloc_array(char, numatts);
9441 : 6889 : tbinfo->attgenerated = pg_malloc_array(char, numatts);
9442 : 6889 : tbinfo->attisdropped = pg_malloc_array(bool, numatts);
9443 : 6889 : tbinfo->attlen = pg_malloc_array(int, numatts);
9444 : 6889 : tbinfo->attalign = pg_malloc_array(char, numatts);
9445 : 6889 : tbinfo->attislocal = pg_malloc_array(bool, numatts);
9446 : 6889 : tbinfo->attoptions = pg_malloc_array(char *, numatts);
9447 : 6889 : tbinfo->attcollation = pg_malloc_array(Oid, numatts);
9448 : 6889 : tbinfo->attcompression = pg_malloc_array(char, numatts);
9449 : 6889 : tbinfo->attfdwoptions = pg_malloc_array(char *, numatts);
9450 : 6889 : tbinfo->attmissingval = pg_malloc_array(char *, numatts);
9451 : 6889 : tbinfo->notnull_constrs = pg_malloc_array(char *, numatts);
9452 : 6889 : tbinfo->notnull_comment = pg_malloc_array(char *, numatts);
9453 : 6889 : tbinfo->notnull_invalid = pg_malloc_array(bool, numatts);
9454 : 6889 : tbinfo->notnull_noinh = pg_malloc_array(bool, numatts);
9455 : 6889 : tbinfo->notnull_islocal = pg_malloc_array(bool, numatts);
9456 : 6889 : tbinfo->attrdefs = pg_malloc_array(AttrDefInfo *, numatts);
9457 : 6889 : hasdefaults = false;
9458 : :
9459 [ + + ]: 32623 : for (int j = 0; j < numatts; j++, r++)
9460 : : {
9461 [ - + ]: 25734 : if (j + 1 != atoi(PQgetvalue(res, r, i_attnum)) &&
9462 [ # # # # ]: 0 : !(fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
9463 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
9464 : 0 : pg_fatal("invalid column numbering in table \"%s\"",
9465 : : tbinfo->dobj.name);
9466 : 25734 : tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, r, i_attname));
9467 : 25734 : tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, r, i_atttypname));
9468 [ + + ]: 25734 : if (PQgetisnull(res, r, i_attstattarget))
9469 : 25691 : tbinfo->attstattarget[j] = -1;
9470 : : else
9471 : 43 : tbinfo->attstattarget[j] = atoi(PQgetvalue(res, r, i_attstattarget));
9472 : 25734 : tbinfo->attstorage[j] = *(PQgetvalue(res, r, i_attstorage));
9473 : 25734 : tbinfo->typstorage[j] = *(PQgetvalue(res, r, i_typstorage));
9474 : 25734 : tbinfo->attidentity[j] = *(PQgetvalue(res, r, i_attidentity));
9475 : 25734 : tbinfo->attgenerated[j] = *(PQgetvalue(res, r, i_attgenerated));
9476 [ + + + + ]: 25734 : tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
9477 : 25734 : tbinfo->attisdropped[j] = (PQgetvalue(res, r, i_attisdropped)[0] == 't');
9478 : 25734 : tbinfo->attlen[j] = atoi(PQgetvalue(res, r, i_attlen));
9479 : 25734 : tbinfo->attalign[j] = *(PQgetvalue(res, r, i_attalign));
9480 : 25734 : tbinfo->attislocal[j] = (PQgetvalue(res, r, i_attislocal)[0] == 't');
9481 : :
9482 : : /* Handle not-null constraint name and flags */
9483 : 25734 : determineNotNullFlags(fout, res, r,
9484 : : tbinfo, j,
9485 : : i_notnull_name,
9486 : : i_notnull_comment,
9487 : : i_notnull_invalidoid,
9488 : : i_notnull_noinherit,
9489 : : i_notnull_islocal,
9490 : : &invalidnotnulloids);
9491 : :
9492 : 25734 : tbinfo->notnull_comment[j] = PQgetisnull(res, r, i_notnull_comment) ?
9493 [ + + ]: 25734 : NULL : pg_strdup(PQgetvalue(res, r, i_notnull_comment));
9494 : 25734 : tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, r, i_attoptions));
9495 : 25734 : tbinfo->attcollation[j] = atooid(PQgetvalue(res, r, i_attcollation));
9496 : 25734 : tbinfo->attcompression[j] = *(PQgetvalue(res, r, i_attcompression));
9497 : 25734 : tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, r, i_attfdwoptions));
9498 : 25734 : tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, r, i_attmissingval));
9499 : 25734 : tbinfo->attrdefs[j] = NULL; /* fix below */
9500 [ + + ]: 25734 : if (PQgetvalue(res, r, i_atthasdef)[0] == 't')
9501 : 1366 : hasdefaults = true;
9502 : : }
9503 : :
9504 [ + + ]: 6889 : if (hasdefaults)
9505 : : {
9506 : : /* Collect OIDs of interesting tables that have defaults */
9507 [ + + ]: 1016 : if (tbloids->len > 1) /* do we have more than the '{'? */
9508 : 945 : appendPQExpBufferChar(tbloids, ',');
9509 : 1016 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9510 : : }
9511 : : }
9512 : :
9513 : : /* If invalidnotnulloids has any data, finalize it */
9514 [ + + ]: 193 : if (invalidnotnulloids != NULL)
9515 : 46 : appendPQExpBufferChar(invalidnotnulloids, '}');
9516 : :
9517 : 193 : PQclear(res);
9518 : :
9519 : : /*
9520 : : * Now get info about column defaults. This is skipped for a data-only
9521 : : * dump, as it is only needed for table schemas.
9522 : : */
9523 [ + + + + ]: 193 : if (dopt->dumpSchema && tbloids->len > 1)
9524 : : {
9525 : : AttrDefInfo *attrdefs;
9526 : : int numDefaults;
9527 : 62 : TableInfo *tbinfo = NULL;
9528 : :
9529 : 62 : pg_log_info("finding table default expressions");
9530 : :
9531 : 62 : appendPQExpBufferChar(tbloids, '}');
9532 : :
9533 : 62 : printfPQExpBuffer(q, "SELECT a.tableoid, a.oid, adrelid, adnum, "
9534 : : "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc\n"
9535 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9536 : : "JOIN pg_catalog.pg_attrdef a ON (src.tbloid = a.adrelid)\n"
9537 : : "ORDER BY a.adrelid, a.adnum",
9538 : : tbloids->data);
9539 : :
9540 : 62 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9541 : :
9542 : 62 : numDefaults = PQntuples(res);
9543 : 62 : attrdefs = pg_malloc_array(AttrDefInfo, numDefaults);
9544 : :
9545 : 62 : curtblindx = -1;
9546 [ + + ]: 1319 : for (int j = 0; j < numDefaults; j++)
9547 : : {
9548 : 1257 : Oid adtableoid = atooid(PQgetvalue(res, j, 0));
9549 : 1257 : Oid adoid = atooid(PQgetvalue(res, j, 1));
9550 : 1257 : Oid adrelid = atooid(PQgetvalue(res, j, 2));
9551 : 1257 : int adnum = atoi(PQgetvalue(res, j, 3));
9552 : 1257 : char *adsrc = PQgetvalue(res, j, 4);
9553 : :
9554 : : /*
9555 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9556 : : * OID order.
9557 : : */
9558 [ + + + + ]: 1257 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != adrelid)
9559 : : {
9560 [ + - ]: 19698 : while (++curtblindx < numTables)
9561 : : {
9562 : 19698 : tbinfo = &tblinfo[curtblindx];
9563 [ + + ]: 19698 : if (tbinfo->dobj.catId.oid == adrelid)
9564 : 941 : break;
9565 : : }
9566 [ - + ]: 941 : if (curtblindx >= numTables)
9567 : 0 : pg_fatal("unrecognized table OID %u", adrelid);
9568 : : }
9569 : :
9570 [ + - - + ]: 1257 : if (adnum <= 0 || adnum > tbinfo->numatts)
9571 : 0 : pg_fatal("invalid adnum value %d for table \"%s\"",
9572 : : adnum, tbinfo->dobj.name);
9573 : :
9574 : : /*
9575 : : * dropped columns shouldn't have defaults, but just in case,
9576 : : * ignore 'em
9577 : : */
9578 [ - + ]: 1257 : if (tbinfo->attisdropped[adnum - 1])
9579 : 0 : continue;
9580 : :
9581 : 1257 : attrdefs[j].dobj.objType = DO_ATTRDEF;
9582 : 1257 : attrdefs[j].dobj.catId.tableoid = adtableoid;
9583 : 1257 : attrdefs[j].dobj.catId.oid = adoid;
9584 : 1257 : AssignDumpId(&attrdefs[j].dobj);
9585 : 1257 : attrdefs[j].adtable = tbinfo;
9586 : 1257 : attrdefs[j].adnum = adnum;
9587 : 1257 : attrdefs[j].adef_expr = pg_strdup(adsrc);
9588 : :
9589 : 1257 : attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name);
9590 : 1257 : attrdefs[j].dobj.namespace = tbinfo->dobj.namespace;
9591 : :
9592 : 1257 : attrdefs[j].dobj.dump = tbinfo->dobj.dump;
9593 : :
9594 : : /*
9595 : : * Figure out whether the default/generation expression should be
9596 : : * dumped as part of the main CREATE TABLE (or similar) command or
9597 : : * as a separate ALTER TABLE (or similar) command. The preference
9598 : : * is to put it into the CREATE command, but in some cases that's
9599 : : * not possible.
9600 : : */
9601 [ + + ]: 1257 : if (tbinfo->attgenerated[adnum - 1])
9602 : : {
9603 : : /*
9604 : : * Column generation expressions cannot be dumped separately,
9605 : : * because there is no syntax for it. By setting separate to
9606 : : * false here we prevent the "default" from being processed as
9607 : : * its own dumpable object. Later, flagInhAttrs() will mark
9608 : : * it as not to be dumped at all, if possible (that is, if it
9609 : : * can be inherited from a parent).
9610 : : */
9611 : 722 : attrdefs[j].separate = false;
9612 : : }
9613 [ + + ]: 535 : else if (tbinfo->relkind == RELKIND_VIEW)
9614 : : {
9615 : : /*
9616 : : * Defaults on a VIEW must always be dumped as separate ALTER
9617 : : * TABLE commands.
9618 : : */
9619 : 34 : attrdefs[j].separate = true;
9620 : : }
9621 [ + + ]: 501 : else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1))
9622 : : {
9623 : : /* column will be suppressed, print default separately */
9624 : 4 : attrdefs[j].separate = true;
9625 : : }
9626 : : else
9627 : : {
9628 : 497 : attrdefs[j].separate = false;
9629 : : }
9630 : :
9631 [ + + ]: 1257 : if (!attrdefs[j].separate)
9632 : : {
9633 : : /*
9634 : : * Mark the default as needing to appear before the table, so
9635 : : * that any dependencies it has must be emitted before the
9636 : : * CREATE TABLE. If this is not possible, we'll change to
9637 : : * "separate" mode while sorting dependencies.
9638 : : */
9639 : 1219 : addObjectDependency(&tbinfo->dobj,
9640 : 1219 : attrdefs[j].dobj.dumpId);
9641 : : }
9642 : :
9643 : 1257 : tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
9644 : : }
9645 : :
9646 : 62 : PQclear(res);
9647 : : }
9648 : :
9649 : : /*
9650 : : * Get info about NOT NULL NOT VALID constraints. This is skipped for a
9651 : : * data-only dump, as it is only needed for table schemas.
9652 : : */
9653 [ + + + + ]: 193 : if (dopt->dumpSchema && invalidnotnulloids)
9654 : : {
9655 : : ConstraintInfo *constrs;
9656 : : int numConstrs;
9657 : : int i_tableoid;
9658 : : int i_oid;
9659 : : int i_conrelid;
9660 : : int i_conname;
9661 : : int i_consrc;
9662 : : int i_conislocal;
9663 : :
9664 : 39 : pg_log_info("finding invalid not-null constraints");
9665 : :
9666 : 39 : resetPQExpBuffer(q);
9667 : 39 : appendPQExpBuffer(q,
9668 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9669 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9670 : : "conislocal, convalidated "
9671 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(conoid)\n"
9672 : : "JOIN pg_catalog.pg_constraint c ON (src.conoid = c.oid)\n"
9673 : : "ORDER BY c.conrelid, c.conname",
9674 : 39 : invalidnotnulloids->data);
9675 : :
9676 : 39 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9677 : :
9678 : 39 : numConstrs = PQntuples(res);
9679 : 39 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9680 : :
9681 : 39 : i_tableoid = PQfnumber(res, "tableoid");
9682 : 39 : i_oid = PQfnumber(res, "oid");
9683 : 39 : i_conrelid = PQfnumber(res, "conrelid");
9684 : 39 : i_conname = PQfnumber(res, "conname");
9685 : 39 : i_consrc = PQfnumber(res, "consrc");
9686 : 39 : i_conislocal = PQfnumber(res, "conislocal");
9687 : :
9688 : : /* As above, this loop iterates once per table, not once per row */
9689 : 39 : curtblindx = -1;
9690 [ + + ]: 108 : for (int j = 0; j < numConstrs;)
9691 : : {
9692 : 69 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9693 : 69 : TableInfo *tbinfo = NULL;
9694 : : int numcons;
9695 : :
9696 : : /* Count rows for this table */
9697 [ + + ]: 69 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9698 [ + - ]: 30 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9699 : 30 : break;
9700 : :
9701 : : /*
9702 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9703 : : * OID order.
9704 : : */
9705 [ + - ]: 13529 : while (++curtblindx < numTables)
9706 : : {
9707 : 13529 : tbinfo = &tblinfo[curtblindx];
9708 [ + + ]: 13529 : if (tbinfo->dobj.catId.oid == conrelid)
9709 : 69 : break;
9710 : : }
9711 [ - + ]: 69 : if (curtblindx >= numTables)
9712 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9713 : :
9714 [ + + ]: 138 : for (int c = 0; c < numcons; c++, j++)
9715 : : {
9716 : 69 : constrs[j].dobj.objType = DO_CONSTRAINT;
9717 : 69 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9718 : 69 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9719 : 69 : AssignDumpId(&constrs[j].dobj);
9720 : 69 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9721 : 69 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9722 : 69 : constrs[j].contable = tbinfo;
9723 : 69 : constrs[j].condomain = NULL;
9724 : 69 : constrs[j].contype = 'n';
9725 : 69 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9726 : 69 : constrs[j].confrelid = InvalidOid;
9727 : 69 : constrs[j].conindex = 0;
9728 : 69 : constrs[j].condeferrable = false;
9729 : 69 : constrs[j].condeferred = false;
9730 : 69 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9731 : :
9732 : : /*
9733 : : * All invalid not-null constraints must be dumped separately,
9734 : : * because CREATE TABLE would not create them as invalid, and
9735 : : * also because they must be created after potentially
9736 : : * violating data has been loaded.
9737 : : */
9738 : 69 : constrs[j].separate = true;
9739 : :
9740 : 69 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9741 : : }
9742 : : }
9743 : 39 : PQclear(res);
9744 : : }
9745 : :
9746 : : /*
9747 : : * Get info about table CHECK constraints. This is skipped for a
9748 : : * data-only dump, as it is only needed for table schemas.
9749 : : */
9750 [ + + + + ]: 193 : if (dopt->dumpSchema && checkoids->len > 2)
9751 : : {
9752 : : ConstraintInfo *constrs;
9753 : : int numConstrs;
9754 : : int i_tableoid;
9755 : : int i_oid;
9756 : : int i_conrelid;
9757 : : int i_conname;
9758 : : int i_consrc;
9759 : : int i_conislocal;
9760 : : int i_convalidated;
9761 : :
9762 : 63 : pg_log_info("finding table check constraints");
9763 : :
9764 : 63 : resetPQExpBuffer(q);
9765 : 63 : appendPQExpBuffer(q,
9766 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9767 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9768 : : "conislocal, convalidated "
9769 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9770 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
9771 : : "WHERE contype = 'c' "
9772 : : "ORDER BY c.conrelid, c.conname",
9773 : : checkoids->data);
9774 : :
9775 : 63 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9776 : :
9777 : 63 : numConstrs = PQntuples(res);
9778 : 63 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9779 : :
9780 : 63 : i_tableoid = PQfnumber(res, "tableoid");
9781 : 63 : i_oid = PQfnumber(res, "oid");
9782 : 63 : i_conrelid = PQfnumber(res, "conrelid");
9783 : 63 : i_conname = PQfnumber(res, "conname");
9784 : 63 : i_consrc = PQfnumber(res, "consrc");
9785 : 63 : i_conislocal = PQfnumber(res, "conislocal");
9786 : 63 : i_convalidated = PQfnumber(res, "convalidated");
9787 : :
9788 : : /* As above, this loop iterates once per table, not once per row */
9789 : 63 : curtblindx = -1;
9790 [ + + ]: 556 : for (int j = 0; j < numConstrs;)
9791 : : {
9792 : 493 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9793 : 493 : TableInfo *tbinfo = NULL;
9794 : : int numcons;
9795 : :
9796 : : /* Count rows for this table */
9797 [ + + ]: 632 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9798 [ + + ]: 569 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9799 : 430 : break;
9800 : :
9801 : : /*
9802 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9803 : : * OID order.
9804 : : */
9805 [ + - ]: 18938 : while (++curtblindx < numTables)
9806 : : {
9807 : 18938 : tbinfo = &tblinfo[curtblindx];
9808 [ + + ]: 18938 : if (tbinfo->dobj.catId.oid == conrelid)
9809 : 493 : break;
9810 : : }
9811 [ - + ]: 493 : if (curtblindx >= numTables)
9812 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9813 : :
9814 [ - + ]: 493 : if (numcons != tbinfo->ncheck)
9815 : : {
9816 : 0 : pg_log_error(ngettext("expected %d check constraint on table \"%s\" but found %d",
9817 : : "expected %d check constraints on table \"%s\" but found %d",
9818 : : tbinfo->ncheck),
9819 : : tbinfo->ncheck, tbinfo->dobj.name, numcons);
9820 : 0 : pg_log_error_hint("The system catalogs might be corrupted.");
9821 : 0 : exit_nicely(1);
9822 : : }
9823 : :
9824 : 493 : tbinfo->checkexprs = constrs + j;
9825 : :
9826 [ + + ]: 1125 : for (int c = 0; c < numcons; c++, j++)
9827 : : {
9828 : 632 : bool validated = PQgetvalue(res, j, i_convalidated)[0] == 't';
9829 : :
9830 : 632 : constrs[j].dobj.objType = DO_CONSTRAINT;
9831 : 632 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9832 : 632 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9833 : 632 : AssignDumpId(&constrs[j].dobj);
9834 : 632 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9835 : 632 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9836 : 632 : constrs[j].contable = tbinfo;
9837 : 632 : constrs[j].condomain = NULL;
9838 : 632 : constrs[j].contype = 'c';
9839 : 632 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9840 : 632 : constrs[j].confrelid = InvalidOid;
9841 : 632 : constrs[j].conindex = 0;
9842 : 632 : constrs[j].condeferrable = false;
9843 : 632 : constrs[j].condeferred = false;
9844 : 632 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9845 : :
9846 : : /*
9847 : : * An unvalidated constraint needs to be dumped separately, so
9848 : : * that potentially-violating existing data is loaded before
9849 : : * the constraint.
9850 : : */
9851 : 632 : constrs[j].separate = !validated;
9852 : :
9853 : 632 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9854 : :
9855 : : /*
9856 : : * Mark the constraint as needing to appear before the table
9857 : : * --- this is so that any other dependencies of the
9858 : : * constraint will be emitted before we try to create the
9859 : : * table. If the constraint is to be dumped separately, it
9860 : : * will be dumped after data is loaded anyway, so don't do it.
9861 : : * (There's an automatic dependency in the opposite direction
9862 : : * anyway, so don't need to add one manually here.)
9863 : : */
9864 [ + + ]: 632 : if (!constrs[j].separate)
9865 : 567 : addObjectDependency(&tbinfo->dobj,
9866 : 567 : constrs[j].dobj.dumpId);
9867 : :
9868 : : /*
9869 : : * We will detect later whether the constraint must be split
9870 : : * out from the table definition.
9871 : : */
9872 : : }
9873 : : }
9874 : :
9875 : 63 : PQclear(res);
9876 : : }
9877 : :
9878 : 193 : destroyPQExpBuffer(q);
9879 : 193 : destroyPQExpBuffer(tbloids);
9880 : 193 : destroyPQExpBuffer(checkoids);
9881 : 193 : }
9882 : :
9883 : : /*
9884 : : * Based on the getTableAttrs query's row corresponding to one column, set
9885 : : * the name and flags to handle a not-null constraint for that column in
9886 : : * the tbinfo struct.
9887 : : *
9888 : : * Result row 'r' is for tbinfo's attribute 'j'.
9889 : : *
9890 : : * There are four possibilities:
9891 : : * 1) the column has no not-null constraints. In that case, ->notnull_constrs
9892 : : * (the constraint name) remains NULL.
9893 : : * 2) The column has a constraint with no name (this is the case when
9894 : : * constraints come from pre-18 servers). In this case, ->notnull_constrs
9895 : : * is set to the empty string; dumpTableSchema will print just "NOT NULL".
9896 : : * 3) The column has an invalid not-null constraint. This must be treated
9897 : : * as a separate object (because it must be created after the table data
9898 : : * is loaded). So we add its OID to invalidnotnulloids for processing
9899 : : * elsewhere and do nothing further with it here. We distinguish this
9900 : : * case because the "notnull_invalidoid" column has been set to a non-NULL
9901 : : * value, which is the constraint OID. Valid constraints have a null OID.
9902 : : * 4) The column has a constraint with a known name; in that case
9903 : : * notnull_constrs carries that name and dumpTableSchema will print
9904 : : * "CONSTRAINT the_name NOT NULL". However, if the name is the default
9905 : : * (table_column_not_null) and there's no comment on the constraint,
9906 : : * there's no need to print that name in the dump, so notnull_constrs
9907 : : * is set to the empty string and it behaves as case 2.
9908 : : *
9909 : : * In a child table that inherits from a parent already containing NOT NULL
9910 : : * constraints and the columns in the child don't have their own NOT NULL
9911 : : * declarations, we suppress printing constraints in the child: the
9912 : : * constraints are acquired at the point where the child is attached to the
9913 : : * parent. This is tracked in ->notnull_islocal; for servers pre-18 this is
9914 : : * set not here but in flagInhAttrs. That flag is also used when the
9915 : : * constraint was validated in a child but all its parent have it as NOT
9916 : : * VALID.
9917 : : *
9918 : : * Any of these constraints might have the NO INHERIT bit. If so we set
9919 : : * ->notnull_noinh and NO INHERIT will be printed by dumpTableSchema.
9920 : : *
9921 : : * In case 4 above, the name comparison is a bit of a hack; it actually fails
9922 : : * to do the right thing in all but the trivial case. However, the downside
9923 : : * of getting it wrong is simply that the name is printed rather than
9924 : : * suppressed, so it's not a big deal.
9925 : : *
9926 : : * invalidnotnulloids is expected to be given as NULL; if any invalid not-null
9927 : : * constraints are found, it is initialized and filled with the array of
9928 : : * OIDs of such constraints, for later processing.
9929 : : */
9930 : : static void
9931 : 25734 : determineNotNullFlags(Archive *fout, PGresult *res, int r,
9932 : : TableInfo *tbinfo, int j,
9933 : : int i_notnull_name,
9934 : : int i_notnull_comment,
9935 : : int i_notnull_invalidoid,
9936 : : int i_notnull_noinherit,
9937 : : int i_notnull_islocal,
9938 : : PQExpBuffer *invalidnotnulloids)
9939 : : {
9940 : 25734 : DumpOptions *dopt = fout->dopt;
9941 : :
9942 : : /*
9943 : : * If this not-null constraint is not valid, list its OID in
9944 : : * invalidnotnulloids and do nothing further. It'll be processed
9945 : : * elsewhere later.
9946 : : *
9947 : : * Because invalid not-null constraints are rare, we don't want to malloc
9948 : : * invalidnotnulloids until we're sure we're going it need it, which
9949 : : * happens here.
9950 : : */
9951 [ + + ]: 25734 : if (!PQgetisnull(res, r, i_notnull_invalidoid))
9952 : : {
9953 : 76 : char *constroid = PQgetvalue(res, r, i_notnull_invalidoid);
9954 : :
9955 [ + + ]: 76 : if (*invalidnotnulloids == NULL)
9956 : : {
9957 : 46 : *invalidnotnulloids = createPQExpBuffer();
9958 : 46 : appendPQExpBufferChar(*invalidnotnulloids, '{');
9959 : 46 : appendPQExpBufferStr(*invalidnotnulloids, constroid);
9960 : : }
9961 : : else
9962 : 30 : appendPQExpBuffer(*invalidnotnulloids, ",%s", constroid);
9963 : :
9964 : : /*
9965 : : * Track when a parent constraint is invalid for the cases where a
9966 : : * child constraint has been validated independenly.
9967 : : */
9968 : 76 : tbinfo->notnull_invalid[j] = true;
9969 : :
9970 : : /* nothing else to do */
9971 : 76 : tbinfo->notnull_constrs[j] = NULL;
9972 : 76 : return;
9973 : : }
9974 : :
9975 : : /*
9976 : : * notnull_noinh is straight from the query result. notnull_islocal also,
9977 : : * though flagInhAttrs may change that one later.
9978 : : */
9979 : 25658 : tbinfo->notnull_noinh[j] = PQgetvalue(res, r, i_notnull_noinherit)[0] == 't';
9980 : 25658 : tbinfo->notnull_islocal[j] = PQgetvalue(res, r, i_notnull_islocal)[0] == 't';
9981 : 25658 : tbinfo->notnull_invalid[j] = false;
9982 : :
9983 : : /*
9984 : : * Determine a constraint name to use. If the column is not marked not-
9985 : : * null, we set NULL which cues ... to do nothing. An empty string says
9986 : : * to print an unnamed NOT NULL, and anything else is a constraint name to
9987 : : * use.
9988 : : */
9989 [ - + ]: 25658 : if (fout->remoteVersion < 180000)
9990 : : {
9991 : : /*
9992 : : * < 18 doesn't have not-null names, so an unnamed constraint is
9993 : : * sufficient.
9994 : : */
9995 [ # # ]: 0 : if (PQgetisnull(res, r, i_notnull_name))
9996 : 0 : tbinfo->notnull_constrs[j] = NULL;
9997 : : else
9998 : 0 : tbinfo->notnull_constrs[j] = "";
9999 : : }
10000 : : else
10001 : : {
10002 [ + + ]: 25658 : if (PQgetisnull(res, r, i_notnull_name))
10003 : 22845 : tbinfo->notnull_constrs[j] = NULL;
10004 : : else
10005 : : {
10006 : : /*
10007 : : * In binary upgrade of inheritance child tables, must have a
10008 : : * constraint name that we can UPDATE later; same if there's a
10009 : : * comment on the constraint.
10010 : : */
10011 [ + + ]: 2813 : if ((dopt->binary_upgrade &&
10012 [ + + ]: 346 : !tbinfo->ispartition &&
10013 [ + + + + ]: 3054 : !tbinfo->notnull_islocal[j]) ||
10014 : 2792 : !PQgetisnull(res, r, i_notnull_comment))
10015 : : {
10016 : 70 : tbinfo->notnull_constrs[j] =
10017 : 70 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10018 : : }
10019 : : else
10020 : : {
10021 : : char *default_name;
10022 : :
10023 : : /* XXX should match ChooseConstraintName better */
10024 : 2743 : default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name,
10025 : 2743 : tbinfo->attnames[j]);
10026 [ + + ]: 2743 : if (strcmp(default_name,
10027 : 2743 : PQgetvalue(res, r, i_notnull_name)) == 0)
10028 : 1799 : tbinfo->notnull_constrs[j] = "";
10029 : : else
10030 : : {
10031 : 944 : tbinfo->notnull_constrs[j] =
10032 : 944 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10033 : : }
10034 : 2743 : pfree(default_name);
10035 : : }
10036 : : }
10037 : : }
10038 : : }
10039 : :
10040 : : /*
10041 : : * Test whether a column should be printed as part of table's CREATE TABLE.
10042 : : * Column number is zero-based.
10043 : : *
10044 : : * Normally this is always true, but it's false for dropped columns, as well
10045 : : * as those that were inherited without any local definition. (If we print
10046 : : * such a column it will mistakenly get pg_attribute.attislocal set to true.)
10047 : : * For partitions, it's always true, because we want the partitions to be
10048 : : * created independently and ATTACH PARTITION used afterwards.
10049 : : *
10050 : : * In binary_upgrade mode, we must print all columns and fix the attislocal/
10051 : : * attisdropped state later, so as to keep control of the physical column
10052 : : * order.
10053 : : *
10054 : : * This function exists because there are scattered nonobvious places that
10055 : : * must be kept in sync with this decision.
10056 : : */
10057 : : bool
10058 : 41361 : shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
10059 : : {
10060 [ + + ]: 41361 : if (dopt->binary_upgrade)
10061 : 6341 : return true;
10062 [ + + ]: 35020 : if (tbinfo->attisdropped[colno])
10063 : 730 : return false;
10064 [ + + + + ]: 34290 : return (tbinfo->attislocal[colno] || tbinfo->ispartition);
10065 : : }
10066 : :
10067 : :
10068 : : /*
10069 : : * getTSParsers:
10070 : : * get information about all text search parsers in the system catalogs
10071 : : */
10072 : : void
10073 : 193 : getTSParsers(Archive *fout)
10074 : : {
10075 : : PGresult *res;
10076 : : int ntups;
10077 : : int i;
10078 : : PQExpBuffer query;
10079 : : TSParserInfo *prsinfo;
10080 : : int i_tableoid;
10081 : : int i_oid;
10082 : : int i_prsname;
10083 : : int i_prsnamespace;
10084 : : int i_prsstart;
10085 : : int i_prstoken;
10086 : : int i_prsend;
10087 : : int i_prsheadline;
10088 : : int i_prslextype;
10089 : :
10090 : 193 : query = createPQExpBuffer();
10091 : :
10092 : : /*
10093 : : * find all text search objects, including builtin ones; we filter out
10094 : : * system-defined objects at dump-out time.
10095 : : */
10096 : :
10097 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, "
10098 : : "prsstart::oid, prstoken::oid, "
10099 : : "prsend::oid, prsheadline::oid, prslextype::oid "
10100 : : "FROM pg_ts_parser");
10101 : :
10102 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10103 : :
10104 : 193 : ntups = PQntuples(res);
10105 : :
10106 : 193 : prsinfo = pg_malloc_array(TSParserInfo, ntups);
10107 : :
10108 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10109 : 193 : i_oid = PQfnumber(res, "oid");
10110 : 193 : i_prsname = PQfnumber(res, "prsname");
10111 : 193 : i_prsnamespace = PQfnumber(res, "prsnamespace");
10112 : 193 : i_prsstart = PQfnumber(res, "prsstart");
10113 : 193 : i_prstoken = PQfnumber(res, "prstoken");
10114 : 193 : i_prsend = PQfnumber(res, "prsend");
10115 : 193 : i_prsheadline = PQfnumber(res, "prsheadline");
10116 : 193 : i_prslextype = PQfnumber(res, "prslextype");
10117 : :
10118 [ + + ]: 434 : for (i = 0; i < ntups; i++)
10119 : : {
10120 : 241 : prsinfo[i].dobj.objType = DO_TSPARSER;
10121 : 241 : prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10122 : 241 : prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10123 : 241 : AssignDumpId(&prsinfo[i].dobj);
10124 : 241 : prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
10125 : 482 : prsinfo[i].dobj.namespace =
10126 : 241 : findNamespace(atooid(PQgetvalue(res, i, i_prsnamespace)));
10127 : 241 : prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
10128 : 241 : prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
10129 : 241 : prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
10130 : 241 : prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline));
10131 : 241 : prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype));
10132 : :
10133 : : /* Decide whether we want to dump it */
10134 : 241 : selectDumpableObject(&(prsinfo[i].dobj), fout);
10135 : : }
10136 : :
10137 : 193 : PQclear(res);
10138 : :
10139 : 193 : destroyPQExpBuffer(query);
10140 : 193 : }
10141 : :
10142 : : /*
10143 : : * getTSDictionaries:
10144 : : * get information about all text search dictionaries in the system catalogs
10145 : : */
10146 : : void
10147 : 193 : getTSDictionaries(Archive *fout)
10148 : : {
10149 : : PGresult *res;
10150 : : int ntups;
10151 : : int i;
10152 : : PQExpBuffer query;
10153 : : TSDictInfo *dictinfo;
10154 : : int i_tableoid;
10155 : : int i_oid;
10156 : : int i_dictname;
10157 : : int i_dictnamespace;
10158 : : int i_dictowner;
10159 : : int i_dicttemplate;
10160 : : int i_dictinitoption;
10161 : :
10162 : 193 : query = createPQExpBuffer();
10163 : :
10164 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, dictname, "
10165 : : "dictnamespace, dictowner, "
10166 : : "dicttemplate, dictinitoption "
10167 : : "FROM pg_ts_dict");
10168 : :
10169 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10170 : :
10171 : 193 : ntups = PQntuples(res);
10172 : :
10173 : 193 : dictinfo = pg_malloc_array(TSDictInfo, ntups);
10174 : :
10175 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10176 : 193 : i_oid = PQfnumber(res, "oid");
10177 : 193 : i_dictname = PQfnumber(res, "dictname");
10178 : 193 : i_dictnamespace = PQfnumber(res, "dictnamespace");
10179 : 193 : i_dictowner = PQfnumber(res, "dictowner");
10180 : 193 : i_dictinitoption = PQfnumber(res, "dictinitoption");
10181 : 193 : i_dicttemplate = PQfnumber(res, "dicttemplate");
10182 : :
10183 [ + + ]: 6480 : for (i = 0; i < ntups; i++)
10184 : : {
10185 : 6287 : dictinfo[i].dobj.objType = DO_TSDICT;
10186 : 6287 : dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10187 : 6287 : dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10188 : 6287 : AssignDumpId(&dictinfo[i].dobj);
10189 : 6287 : dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
10190 : 12574 : dictinfo[i].dobj.namespace =
10191 : 6287 : findNamespace(atooid(PQgetvalue(res, i, i_dictnamespace)));
10192 : 6287 : dictinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_dictowner));
10193 : 6287 : dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
10194 [ + + ]: 6287 : if (PQgetisnull(res, i, i_dictinitoption))
10195 : 241 : dictinfo[i].dictinitoption = NULL;
10196 : : else
10197 : 6046 : dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption));
10198 : :
10199 : : /* Decide whether we want to dump it */
10200 : 6287 : selectDumpableObject(&(dictinfo[i].dobj), fout);
10201 : : }
10202 : :
10203 : 193 : PQclear(res);
10204 : :
10205 : 193 : destroyPQExpBuffer(query);
10206 : 193 : }
10207 : :
10208 : : /*
10209 : : * getTSTemplates:
10210 : : * get information about all text search templates in the system catalogs
10211 : : */
10212 : : void
10213 : 193 : getTSTemplates(Archive *fout)
10214 : : {
10215 : : PGresult *res;
10216 : : int ntups;
10217 : : int i;
10218 : : PQExpBuffer query;
10219 : : TSTemplateInfo *tmplinfo;
10220 : : int i_tableoid;
10221 : : int i_oid;
10222 : : int i_tmplname;
10223 : : int i_tmplnamespace;
10224 : : int i_tmplinit;
10225 : : int i_tmpllexize;
10226 : :
10227 : 193 : query = createPQExpBuffer();
10228 : :
10229 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, "
10230 : : "tmplnamespace, tmplinit::oid, tmpllexize::oid "
10231 : : "FROM pg_ts_template");
10232 : :
10233 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10234 : :
10235 : 193 : ntups = PQntuples(res);
10236 : :
10237 : 193 : tmplinfo = pg_malloc_array(TSTemplateInfo, ntups);
10238 : :
10239 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10240 : 193 : i_oid = PQfnumber(res, "oid");
10241 : 193 : i_tmplname = PQfnumber(res, "tmplname");
10242 : 193 : i_tmplnamespace = PQfnumber(res, "tmplnamespace");
10243 : 193 : i_tmplinit = PQfnumber(res, "tmplinit");
10244 : 193 : i_tmpllexize = PQfnumber(res, "tmpllexize");
10245 : :
10246 [ + + ]: 1206 : for (i = 0; i < ntups; i++)
10247 : : {
10248 : 1013 : tmplinfo[i].dobj.objType = DO_TSTEMPLATE;
10249 : 1013 : tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10250 : 1013 : tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10251 : 1013 : AssignDumpId(&tmplinfo[i].dobj);
10252 : 1013 : tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
10253 : 2026 : tmplinfo[i].dobj.namespace =
10254 : 1013 : findNamespace(atooid(PQgetvalue(res, i, i_tmplnamespace)));
10255 : 1013 : tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
10256 : 1013 : tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
10257 : :
10258 : : /* Decide whether we want to dump it */
10259 : 1013 : selectDumpableObject(&(tmplinfo[i].dobj), fout);
10260 : : }
10261 : :
10262 : 193 : PQclear(res);
10263 : :
10264 : 193 : destroyPQExpBuffer(query);
10265 : 193 : }
10266 : :
10267 : : /*
10268 : : * getTSConfigurations:
10269 : : * get information about all text search configurations
10270 : : */
10271 : : void
10272 : 193 : getTSConfigurations(Archive *fout)
10273 : : {
10274 : : PGresult *res;
10275 : : int ntups;
10276 : : int i;
10277 : : PQExpBuffer query;
10278 : : TSConfigInfo *cfginfo;
10279 : : int i_tableoid;
10280 : : int i_oid;
10281 : : int i_cfgname;
10282 : : int i_cfgnamespace;
10283 : : int i_cfgowner;
10284 : : int i_cfgparser;
10285 : :
10286 : 193 : query = createPQExpBuffer();
10287 : :
10288 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, cfgname, "
10289 : : "cfgnamespace, cfgowner, cfgparser "
10290 : : "FROM pg_ts_config");
10291 : :
10292 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10293 : :
10294 : 193 : ntups = PQntuples(res);
10295 : :
10296 : 193 : cfginfo = pg_malloc_array(TSConfigInfo, ntups);
10297 : :
10298 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10299 : 193 : i_oid = PQfnumber(res, "oid");
10300 : 193 : i_cfgname = PQfnumber(res, "cfgname");
10301 : 193 : i_cfgnamespace = PQfnumber(res, "cfgnamespace");
10302 : 193 : i_cfgowner = PQfnumber(res, "cfgowner");
10303 : 193 : i_cfgparser = PQfnumber(res, "cfgparser");
10304 : :
10305 [ + + ]: 6445 : for (i = 0; i < ntups; i++)
10306 : : {
10307 : 6252 : cfginfo[i].dobj.objType = DO_TSCONFIG;
10308 : 6252 : cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10309 : 6252 : cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10310 : 6252 : AssignDumpId(&cfginfo[i].dobj);
10311 : 6252 : cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
10312 : 12504 : cfginfo[i].dobj.namespace =
10313 : 6252 : findNamespace(atooid(PQgetvalue(res, i, i_cfgnamespace)));
10314 : 6252 : cfginfo[i].rolname = getRoleName(PQgetvalue(res, i, i_cfgowner));
10315 : 6252 : cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
10316 : :
10317 : : /* Decide whether we want to dump it */
10318 : 6252 : selectDumpableObject(&(cfginfo[i].dobj), fout);
10319 : : }
10320 : :
10321 : 193 : PQclear(res);
10322 : :
10323 : 193 : destroyPQExpBuffer(query);
10324 : 193 : }
10325 : :
10326 : : /*
10327 : : * getForeignDataWrappers:
10328 : : * get information about all foreign-data wrappers in the system catalogs
10329 : : */
10330 : : void
10331 : 193 : getForeignDataWrappers(Archive *fout)
10332 : : {
10333 : : PGresult *res;
10334 : : int ntups;
10335 : : int i;
10336 : : PQExpBuffer query;
10337 : : FdwInfo *fdwinfo;
10338 : : int i_tableoid;
10339 : : int i_oid;
10340 : : int i_fdwname;
10341 : : int i_fdwowner;
10342 : : int i_fdwhandler;
10343 : : int i_fdwvalidator;
10344 : : int i_fdwconnection;
10345 : : int i_fdwacl;
10346 : : int i_acldefault;
10347 : : int i_fdwoptions;
10348 : :
10349 : 193 : query = createPQExpBuffer();
10350 : :
10351 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, fdwname, "
10352 : : "fdwowner, "
10353 : : "fdwhandler::pg_catalog.regproc, "
10354 : : "fdwvalidator::pg_catalog.regproc, ");
10355 : :
10356 [ + - ]: 193 : if (fout->remoteVersion >= 190000)
10357 : 193 : appendPQExpBufferStr(query, "fdwconnection::pg_catalog.regproc, ");
10358 : : else
10359 : 0 : appendPQExpBufferStr(query, "'-' AS fdwconnection, ");
10360 : :
10361 : 193 : appendPQExpBufferStr(query,
10362 : : "fdwacl, "
10363 : : "acldefault('F', fdwowner) AS acldefault, "
10364 : : "array_to_string(ARRAY("
10365 : : "SELECT quote_ident(option_name) || ' ' || "
10366 : : "quote_literal(option_value) "
10367 : : "FROM pg_options_to_table(fdwoptions) "
10368 : : "ORDER BY option_name"
10369 : : "), E',\n ') AS fdwoptions "
10370 : : "FROM pg_foreign_data_wrapper");
10371 : :
10372 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10373 : :
10374 : 193 : ntups = PQntuples(res);
10375 : :
10376 : 193 : fdwinfo = pg_malloc_array(FdwInfo, ntups);
10377 : :
10378 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10379 : 193 : i_oid = PQfnumber(res, "oid");
10380 : 193 : i_fdwname = PQfnumber(res, "fdwname");
10381 : 193 : i_fdwowner = PQfnumber(res, "fdwowner");
10382 : 193 : i_fdwhandler = PQfnumber(res, "fdwhandler");
10383 : 193 : i_fdwvalidator = PQfnumber(res, "fdwvalidator");
10384 : 193 : i_fdwconnection = PQfnumber(res, "fdwconnection");
10385 : 193 : i_fdwacl = PQfnumber(res, "fdwacl");
10386 : 193 : i_acldefault = PQfnumber(res, "acldefault");
10387 : 193 : i_fdwoptions = PQfnumber(res, "fdwoptions");
10388 : :
10389 [ + + ]: 267 : for (i = 0; i < ntups; i++)
10390 : : {
10391 : 74 : fdwinfo[i].dobj.objType = DO_FDW;
10392 : 74 : fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10393 : 74 : fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10394 : 74 : AssignDumpId(&fdwinfo[i].dobj);
10395 : 74 : fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname));
10396 : 74 : fdwinfo[i].dobj.namespace = NULL;
10397 : 74 : fdwinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_fdwacl));
10398 : 74 : fdwinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10399 : 74 : fdwinfo[i].dacl.privtype = 0;
10400 : 74 : fdwinfo[i].dacl.initprivs = NULL;
10401 : 74 : fdwinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_fdwowner));
10402 : 74 : fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler));
10403 : 74 : fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator));
10404 : 74 : fdwinfo[i].fdwconnection = pg_strdup(PQgetvalue(res, i, i_fdwconnection));
10405 : 74 : fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions));
10406 : :
10407 : : /* Decide whether we want to dump it */
10408 : 74 : selectDumpableObject(&(fdwinfo[i].dobj), fout);
10409 : :
10410 : : /* Mark whether FDW has an ACL */
10411 [ + + ]: 74 : if (!PQgetisnull(res, i, i_fdwacl))
10412 : 48 : fdwinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10413 : : }
10414 : :
10415 : 193 : PQclear(res);
10416 : :
10417 : 193 : destroyPQExpBuffer(query);
10418 : 193 : }
10419 : :
10420 : : /*
10421 : : * getForeignServers:
10422 : : * get information about all foreign servers in the system catalogs
10423 : : */
10424 : : void
10425 : 193 : getForeignServers(Archive *fout)
10426 : : {
10427 : : PGresult *res;
10428 : : int ntups;
10429 : : int i;
10430 : : PQExpBuffer query;
10431 : : ForeignServerInfo *srvinfo;
10432 : : int i_tableoid;
10433 : : int i_oid;
10434 : : int i_srvname;
10435 : : int i_srvowner;
10436 : : int i_srvfdw;
10437 : : int i_srvtype;
10438 : : int i_srvversion;
10439 : : int i_srvacl;
10440 : : int i_acldefault;
10441 : : int i_srvoptions;
10442 : :
10443 : 193 : query = createPQExpBuffer();
10444 : :
10445 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, srvname, "
10446 : : "srvowner, "
10447 : : "srvfdw, srvtype, srvversion, srvacl, "
10448 : : "acldefault('S', srvowner) AS acldefault, "
10449 : : "array_to_string(ARRAY("
10450 : : "SELECT quote_ident(option_name) || ' ' || "
10451 : : "quote_literal(option_value) "
10452 : : "FROM pg_options_to_table(srvoptions) "
10453 : : "ORDER BY option_name"
10454 : : "), E',\n ') AS srvoptions "
10455 : : "FROM pg_foreign_server");
10456 : :
10457 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10458 : :
10459 : 193 : ntups = PQntuples(res);
10460 : :
10461 : 193 : srvinfo = pg_malloc_array(ForeignServerInfo, ntups);
10462 : :
10463 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10464 : 193 : i_oid = PQfnumber(res, "oid");
10465 : 193 : i_srvname = PQfnumber(res, "srvname");
10466 : 193 : i_srvowner = PQfnumber(res, "srvowner");
10467 : 193 : i_srvfdw = PQfnumber(res, "srvfdw");
10468 : 193 : i_srvtype = PQfnumber(res, "srvtype");
10469 : 193 : i_srvversion = PQfnumber(res, "srvversion");
10470 : 193 : i_srvacl = PQfnumber(res, "srvacl");
10471 : 193 : i_acldefault = PQfnumber(res, "acldefault");
10472 : 193 : i_srvoptions = PQfnumber(res, "srvoptions");
10473 : :
10474 [ + + ]: 271 : for (i = 0; i < ntups; i++)
10475 : : {
10476 : 78 : srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
10477 : 78 : srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10478 : 78 : srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10479 : 78 : AssignDumpId(&srvinfo[i].dobj);
10480 : 78 : srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname));
10481 : 78 : srvinfo[i].dobj.namespace = NULL;
10482 : 78 : srvinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_srvacl));
10483 : 78 : srvinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10484 : 78 : srvinfo[i].dacl.privtype = 0;
10485 : 78 : srvinfo[i].dacl.initprivs = NULL;
10486 : 78 : srvinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_srvowner));
10487 : 78 : srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw));
10488 : 78 : srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype));
10489 : 78 : srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion));
10490 : 78 : srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions));
10491 : :
10492 : : /* Decide whether we want to dump it */
10493 : 78 : selectDumpableObject(&(srvinfo[i].dobj), fout);
10494 : :
10495 : : /* Servers have user mappings */
10496 : 78 : srvinfo[i].dobj.components |= DUMP_COMPONENT_USERMAP;
10497 : :
10498 : : /* Mark whether server has an ACL */
10499 [ + + ]: 78 : if (!PQgetisnull(res, i, i_srvacl))
10500 : 48 : srvinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10501 : : }
10502 : :
10503 : 193 : PQclear(res);
10504 : :
10505 : 193 : destroyPQExpBuffer(query);
10506 : 193 : }
10507 : :
10508 : : /*
10509 : : * getDefaultACLs:
10510 : : * get information about all default ACL information in the system catalogs
10511 : : */
10512 : : void
10513 : 193 : getDefaultACLs(Archive *fout)
10514 : : {
10515 : 193 : DumpOptions *dopt = fout->dopt;
10516 : : DefaultACLInfo *daclinfo;
10517 : : PQExpBuffer query;
10518 : : PGresult *res;
10519 : : int i_oid;
10520 : : int i_tableoid;
10521 : : int i_defaclrole;
10522 : : int i_defaclnamespace;
10523 : : int i_defaclobjtype;
10524 : : int i_defaclacl;
10525 : : int i_acldefault;
10526 : : int i,
10527 : : ntups;
10528 : :
10529 : 193 : query = createPQExpBuffer();
10530 : :
10531 : : /*
10532 : : * Global entries (with defaclnamespace=0) replace the hard-wired default
10533 : : * ACL for their object type. We should dump them as deltas from the
10534 : : * default ACL, since that will be used as a starting point for
10535 : : * interpreting the ALTER DEFAULT PRIVILEGES commands. On the other hand,
10536 : : * non-global entries can only add privileges not revoke them. We must
10537 : : * dump those as-is (i.e., as deltas from an empty ACL).
10538 : : *
10539 : : * We can use defaclobjtype as the object type for acldefault(), except
10540 : : * for the case of 'S' (DEFACLOBJ_SEQUENCE) which must be converted to
10541 : : * 's'.
10542 : : */
10543 : 193 : appendPQExpBufferStr(query,
10544 : : "SELECT oid, tableoid, "
10545 : : "defaclrole, "
10546 : : "defaclnamespace, "
10547 : : "defaclobjtype, "
10548 : : "defaclacl, "
10549 : : "CASE WHEN defaclnamespace = 0 THEN "
10550 : : "acldefault(CASE WHEN defaclobjtype = 'S' "
10551 : : "THEN 's'::\"char\" ELSE defaclobjtype END, "
10552 : : "defaclrole) ELSE '{}' END AS acldefault "
10553 : : "FROM pg_default_acl");
10554 : :
10555 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10556 : :
10557 : 193 : ntups = PQntuples(res);
10558 : :
10559 : 193 : daclinfo = pg_malloc_array(DefaultACLInfo, ntups);
10560 : :
10561 : 193 : i_oid = PQfnumber(res, "oid");
10562 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10563 : 193 : i_defaclrole = PQfnumber(res, "defaclrole");
10564 : 193 : i_defaclnamespace = PQfnumber(res, "defaclnamespace");
10565 : 193 : i_defaclobjtype = PQfnumber(res, "defaclobjtype");
10566 : 193 : i_defaclacl = PQfnumber(res, "defaclacl");
10567 : 193 : i_acldefault = PQfnumber(res, "acldefault");
10568 : :
10569 [ + + ]: 399 : for (i = 0; i < ntups; i++)
10570 : : {
10571 : 206 : Oid nspid = atooid(PQgetvalue(res, i, i_defaclnamespace));
10572 : :
10573 : 206 : daclinfo[i].dobj.objType = DO_DEFAULT_ACL;
10574 : 206 : daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10575 : 206 : daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10576 : 206 : AssignDumpId(&daclinfo[i].dobj);
10577 : : /* cheesy ... is it worth coming up with a better object name? */
10578 : 206 : daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
10579 : :
10580 [ + + ]: 206 : if (nspid != InvalidOid)
10581 : 96 : daclinfo[i].dobj.namespace = findNamespace(nspid);
10582 : : else
10583 : 110 : daclinfo[i].dobj.namespace = NULL;
10584 : :
10585 : 206 : daclinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_defaclacl));
10586 : 206 : daclinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10587 : 206 : daclinfo[i].dacl.privtype = 0;
10588 : 206 : daclinfo[i].dacl.initprivs = NULL;
10589 : 206 : daclinfo[i].defaclrole = getRoleName(PQgetvalue(res, i, i_defaclrole));
10590 : 206 : daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype));
10591 : :
10592 : : /* Default ACLs are ACLs, of course */
10593 : 206 : daclinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10594 : :
10595 : : /* Decide whether we want to dump it */
10596 : 206 : selectDumpableDefaultACL(&(daclinfo[i]), dopt);
10597 : : }
10598 : :
10599 : 193 : PQclear(res);
10600 : :
10601 : 193 : destroyPQExpBuffer(query);
10602 : 193 : }
10603 : :
10604 : : /*
10605 : : * getRoleName -- look up the name of a role, given its OID
10606 : : *
10607 : : * In current usage, we don't expect failures, so error out for a bad OID.
10608 : : */
10609 : : static const char *
10610 : 624363 : getRoleName(const char *roleoid_str)
10611 : : {
10612 : 624363 : Oid roleoid = atooid(roleoid_str);
10613 : :
10614 : : /*
10615 : : * Do binary search to find the appropriate item.
10616 : : */
10617 [ + - ]: 624363 : if (nrolenames > 0)
10618 : : {
10619 : 624363 : RoleNameItem *low = &rolenames[0];
10620 : 624363 : RoleNameItem *high = &rolenames[nrolenames - 1];
10621 : :
10622 [ + - ]: 2497633 : while (low <= high)
10623 : : {
10624 : 2497633 : RoleNameItem *middle = low + (high - low) / 2;
10625 : :
10626 [ + + ]: 2497633 : if (roleoid < middle->roleoid)
10627 : 1871880 : high = middle - 1;
10628 [ + + ]: 625753 : else if (roleoid > middle->roleoid)
10629 : 1390 : low = middle + 1;
10630 : : else
10631 : 624363 : return middle->rolename; /* found a match */
10632 : : }
10633 : : }
10634 : :
10635 : 0 : pg_fatal("role with OID %u does not exist", roleoid);
10636 : : return NULL; /* keep compiler quiet */
10637 : : }
10638 : :
10639 : : /*
10640 : : * collectRoleNames --
10641 : : *
10642 : : * Construct a table of all known roles.
10643 : : * The table is sorted by OID for speed in lookup.
10644 : : */
10645 : : static void
10646 : 194 : collectRoleNames(Archive *fout)
10647 : : {
10648 : : PGresult *res;
10649 : : const char *query;
10650 : : int i;
10651 : :
10652 : 194 : query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
10653 : :
10654 : 194 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
10655 : :
10656 : 194 : nrolenames = PQntuples(res);
10657 : :
10658 : 194 : rolenames = pg_malloc_array(RoleNameItem, nrolenames);
10659 : :
10660 [ + + ]: 3738 : for (i = 0; i < nrolenames; i++)
10661 : : {
10662 : 3544 : rolenames[i].roleoid = atooid(PQgetvalue(res, i, 0));
10663 : 3544 : rolenames[i].rolename = pg_strdup(PQgetvalue(res, i, 1));
10664 : : }
10665 : :
10666 : 194 : PQclear(res);
10667 : 194 : }
10668 : :
10669 : : /*
10670 : : * getAdditionalACLs
10671 : : *
10672 : : * We have now created all the DumpableObjects, and collected the ACL data
10673 : : * that appears in the directly-associated catalog entries. However, there's
10674 : : * more ACL-related info to collect. If any of a table's columns have ACLs,
10675 : : * we must set the TableInfo's DUMP_COMPONENT_ACL components flag, as well as
10676 : : * its hascolumnACLs flag (we won't store the ACLs themselves here, though).
10677 : : * Also, in versions having the pg_init_privs catalog, read that and load the
10678 : : * information into the relevant DumpableObjects.
10679 : : */
10680 : : static void
10681 : 191 : getAdditionalACLs(Archive *fout)
10682 : : {
10683 : 191 : PQExpBuffer query = createPQExpBuffer();
10684 : : PGresult *res;
10685 : : int ntups,
10686 : : i;
10687 : :
10688 : : /* Check for per-column ACLs */
10689 : 191 : appendPQExpBufferStr(query,
10690 : : "SELECT DISTINCT attrelid FROM pg_attribute "
10691 : : "WHERE attacl IS NOT NULL");
10692 : :
10693 : 191 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10694 : :
10695 : 191 : ntups = PQntuples(res);
10696 [ + + ]: 561 : for (i = 0; i < ntups; i++)
10697 : : {
10698 : 370 : Oid relid = atooid(PQgetvalue(res, i, 0));
10699 : : TableInfo *tblinfo;
10700 : :
10701 : 370 : tblinfo = findTableByOid(relid);
10702 : : /* OK to ignore tables we haven't got a DumpableObject for */
10703 [ + - ]: 370 : if (tblinfo)
10704 : : {
10705 : 370 : tblinfo->dobj.components |= DUMP_COMPONENT_ACL;
10706 : 370 : tblinfo->hascolumnACLs = true;
10707 : : }
10708 : : }
10709 : 191 : PQclear(res);
10710 : :
10711 : : /* Fetch initial-privileges data */
10712 : 191 : printfPQExpBuffer(query,
10713 : : "SELECT objoid, classoid, objsubid, privtype, initprivs "
10714 : : "FROM pg_init_privs");
10715 : :
10716 : 191 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10717 : :
10718 : 191 : ntups = PQntuples(res);
10719 [ + + ]: 48058 : for (i = 0; i < ntups; i++)
10720 : : {
10721 : 47867 : Oid objoid = atooid(PQgetvalue(res, i, 0));
10722 : 47867 : Oid classoid = atooid(PQgetvalue(res, i, 1));
10723 : 47867 : int objsubid = atoi(PQgetvalue(res, i, 2));
10724 : 47867 : char privtype = *(PQgetvalue(res, i, 3));
10725 : 47867 : char *initprivs = PQgetvalue(res, i, 4);
10726 : : CatalogId objId;
10727 : : DumpableObject *dobj;
10728 : :
10729 : 47867 : objId.tableoid = classoid;
10730 : 47867 : objId.oid = objoid;
10731 : 47867 : dobj = findObjectByCatalogId(objId);
10732 : : /* OK to ignore entries we haven't got a DumpableObject for */
10733 [ + + ]: 47867 : if (dobj)
10734 : : {
10735 : : /* Cope with sub-object initprivs */
10736 [ + + ]: 34544 : if (objsubid != 0)
10737 : : {
10738 [ + - ]: 4608 : if (dobj->objType == DO_TABLE)
10739 : : {
10740 : : /* For a column initprivs, set the table's ACL flags */
10741 : 4608 : dobj->components |= DUMP_COMPONENT_ACL;
10742 : 4608 : ((TableInfo *) dobj)->hascolumnACLs = true;
10743 : : }
10744 : : else
10745 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10746 : : classoid, objoid, objsubid);
10747 : 4795 : continue;
10748 : : }
10749 : :
10750 : : /*
10751 : : * We ignore any pg_init_privs.initprivs entry for the public
10752 : : * schema, as explained in getNamespaces().
10753 : : */
10754 [ + + ]: 29936 : if (dobj->objType == DO_NAMESPACE &&
10755 [ + + ]: 569 : strcmp(dobj->name, "public") == 0)
10756 : 187 : continue;
10757 : :
10758 : : /* Else it had better be of a type we think has ACLs */
10759 [ + + ]: 29749 : if (dobj->objType == DO_NAMESPACE ||
10760 [ + + ]: 29367 : dobj->objType == DO_TYPE ||
10761 [ + + ]: 29343 : dobj->objType == DO_FUNC ||
10762 [ + + ]: 29248 : dobj->objType == DO_AGG ||
10763 [ - + ]: 29224 : dobj->objType == DO_TABLE ||
10764 [ # # ]: 0 : dobj->objType == DO_PROCLANG ||
10765 [ # # ]: 0 : dobj->objType == DO_FDW ||
10766 [ # # ]: 0 : dobj->objType == DO_FOREIGN_SERVER)
10767 : 29749 : {
10768 : 29749 : DumpableObjectWithAcl *daobj = (DumpableObjectWithAcl *) dobj;
10769 : :
10770 : 29749 : daobj->dacl.privtype = privtype;
10771 : 29749 : daobj->dacl.initprivs = pstrdup(initprivs);
10772 : : }
10773 : : else
10774 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10775 : : classoid, objoid, objsubid);
10776 : : }
10777 : : }
10778 : 191 : PQclear(res);
10779 : :
10780 : 191 : destroyPQExpBuffer(query);
10781 : 191 : }
10782 : :
10783 : : /*
10784 : : * dumpCommentExtended --
10785 : : *
10786 : : * This routine is used to dump any comments associated with the
10787 : : * object handed to this routine. The routine takes the object type
10788 : : * and object name (ready to print, except for schema decoration), plus
10789 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
10790 : : * plus catalog ID and subid which are the lookup key for pg_description,
10791 : : * plus the dump ID for the object (for setting a dependency).
10792 : : * If a matching pg_description entry is found, it is dumped.
10793 : : *
10794 : : * Note: in some cases, such as comments for triggers and rules, the "type"
10795 : : * string really looks like, e.g., "TRIGGER name ON". This is a bit of a hack
10796 : : * but it doesn't seem worth complicating the API for all callers to make
10797 : : * it cleaner.
10798 : : *
10799 : : * Note: although this routine takes a dumpId for dependency purposes,
10800 : : * that purpose is just to mark the dependency in the emitted dump file
10801 : : * for possible future use by pg_restore. We do NOT use it for determining
10802 : : * ordering of the comment in the dump file, because this routine is called
10803 : : * after dependency sorting occurs. This routine should be called just after
10804 : : * calling ArchiveEntry() for the specified object.
10805 : : */
10806 : : static void
10807 : 6645 : dumpCommentExtended(Archive *fout, const char *type,
10808 : : const char *name, const char *namespace,
10809 : : const char *owner, CatalogId catalogId,
10810 : : int subid, DumpId dumpId,
10811 : : const char *initdb_comment)
10812 : : {
10813 : 6645 : DumpOptions *dopt = fout->dopt;
10814 : : CommentItem *comments;
10815 : : int ncomments;
10816 : :
10817 : : /* do nothing, if --no-comments is supplied */
10818 [ - + ]: 6645 : if (dopt->no_comments)
10819 : 0 : return;
10820 : :
10821 : : /* Comments are schema not data ... except LO comments are data */
10822 [ + + ]: 6645 : if (strcmp(type, "LARGE OBJECT") != 0)
10823 : : {
10824 [ - + ]: 6585 : if (!dopt->dumpSchema)
10825 : 0 : return;
10826 : : }
10827 : : else
10828 : : {
10829 : : /* We do dump LO comments in binary-upgrade mode */
10830 [ + + - + ]: 60 : if (!dopt->dumpData && !dopt->binary_upgrade)
10831 : 0 : return;
10832 : : }
10833 : :
10834 : : /* Search for comments associated with catalogId, using table */
10835 : 6645 : ncomments = findComments(catalogId.tableoid, catalogId.oid,
10836 : : &comments);
10837 : :
10838 : : /* Is there one matching the subid? */
10839 [ + + ]: 6645 : while (ncomments > 0)
10840 : : {
10841 [ + - ]: 6598 : if (comments->objsubid == subid)
10842 : 6598 : break;
10843 : 0 : comments++;
10844 : 0 : ncomments--;
10845 : : }
10846 : :
10847 [ + + ]: 6645 : if (initdb_comment != NULL)
10848 : : {
10849 : : static CommentItem empty_comment = {.descr = ""};
10850 : :
10851 : : /*
10852 : : * initdb creates this object with a comment. Skip dumping the
10853 : : * initdb-provided comment, which would complicate matters for
10854 : : * non-superuser use of pg_dump. When the DBA has removed initdb's
10855 : : * comment, replicate that.
10856 : : */
10857 [ + + ]: 117 : if (ncomments == 0)
10858 : : {
10859 : 4 : comments = &empty_comment;
10860 : 4 : ncomments = 1;
10861 : : }
10862 [ + - ]: 113 : else if (strcmp(comments->descr, initdb_comment) == 0)
10863 : 113 : ncomments = 0;
10864 : : }
10865 : :
10866 : : /* If a comment exists, build COMMENT ON statement */
10867 [ + + ]: 6645 : if (ncomments > 0)
10868 : : {
10869 : 6489 : PQExpBuffer query = createPQExpBuffer();
10870 : 6489 : PQExpBuffer tag = createPQExpBuffer();
10871 : :
10872 : 6489 : appendPQExpBuffer(query, "COMMENT ON %s ", type);
10873 [ + + + - ]: 6489 : if (namespace && *namespace)
10874 : 6300 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
10875 : 6489 : appendPQExpBuffer(query, "%s IS ", name);
10876 : 6489 : appendStringLiteralAH(query, comments->descr, fout);
10877 : 6489 : appendPQExpBufferStr(query, ";\n");
10878 : :
10879 : 6489 : appendPQExpBuffer(tag, "%s %s", type, name);
10880 : :
10881 : : /*
10882 : : * We mark comments as SECTION_NONE because they really belong in the
10883 : : * same section as their parent, whether that is pre-data or
10884 : : * post-data.
10885 : : */
10886 : 6489 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
10887 : 6489 : ARCHIVE_OPTS(.tag = tag->data,
10888 : : .namespace = namespace,
10889 : : .owner = owner,
10890 : : .description = "COMMENT",
10891 : : .section = SECTION_NONE,
10892 : : .createStmt = query->data,
10893 : : .deps = &dumpId,
10894 : : .nDeps = 1));
10895 : :
10896 : 6489 : destroyPQExpBuffer(query);
10897 : 6489 : destroyPQExpBuffer(tag);
10898 : : }
10899 : : }
10900 : :
10901 : : /*
10902 : : * dumpComment --
10903 : : *
10904 : : * Typical simplification of the above function.
10905 : : */
10906 : : static inline void
10907 : 6483 : dumpComment(Archive *fout, const char *type,
10908 : : const char *name, const char *namespace,
10909 : : const char *owner, CatalogId catalogId,
10910 : : int subid, DumpId dumpId)
10911 : : {
10912 : 6483 : dumpCommentExtended(fout, type, name, namespace, owner,
10913 : : catalogId, subid, dumpId, NULL);
10914 : 6483 : }
10915 : :
10916 : : /*
10917 : : * appendNamedArgument --
10918 : : *
10919 : : * Convenience routine for constructing parameters of the form:
10920 : : * 'paraname', 'value'::type
10921 : : */
10922 : : static void
10923 : 6570 : appendNamedArgument(PQExpBuffer out, Archive *fout, const char *argname,
10924 : : const char *argtype, const char *argval)
10925 : : {
10926 : 6570 : appendPQExpBufferStr(out, ",\n\t");
10927 : :
10928 : 6570 : appendStringLiteralAH(out, argname, fout);
10929 : 6570 : appendPQExpBufferStr(out, ", ");
10930 : :
10931 : 6570 : appendStringLiteralAH(out, argval, fout);
10932 : 6570 : appendPQExpBuffer(out, "::%s", argtype);
10933 : 6570 : }
10934 : :
10935 : : /*
10936 : : * fetchAttributeStats --
10937 : : *
10938 : : * Fetch next batch of attribute statistics for dumpRelationStats_dumper().
10939 : : */
10940 : : static PGresult *
10941 : 1110 : fetchAttributeStats(Archive *fout)
10942 : : {
10943 : 1110 : ArchiveHandle *AH = (ArchiveHandle *) fout;
10944 : 1110 : PQExpBuffer relids = createPQExpBuffer();
10945 : 1110 : PQExpBuffer nspnames = createPQExpBuffer();
10946 : 1110 : PQExpBuffer relnames = createPQExpBuffer();
10947 : 1110 : int count = 0;
10948 : 1110 : PGresult *res = NULL;
10949 : : static TocEntry *te;
10950 : : static bool restarted;
10951 : 1110 : int max_rels = MAX_ATTR_STATS_RELS;
10952 : :
10953 : : /* If we're just starting, set our TOC pointer. */
10954 [ + + ]: 1110 : if (!te)
10955 : 65 : te = AH->toc->next;
10956 : :
10957 : : /*
10958 : : * We can't easily avoid a second TOC scan for the tar format because it
10959 : : * writes restore.sql separately, which means we must execute the queries
10960 : : * twice. This feels risky, but there is no known reason it should
10961 : : * generate different output than the first pass. Even if it does, the
10962 : : * worst-case scenario is that restore.sql might have different statistics
10963 : : * data than the archive.
10964 : : */
10965 [ + + + + : 1110 : if (!restarted && te == AH->toc && AH->format == archTar)
+ + ]
10966 : : {
10967 : 1 : te = AH->toc->next;
10968 : 1 : restarted = true;
10969 : : }
10970 : :
10971 : 1110 : appendPQExpBufferChar(relids, '{');
10972 : 1110 : appendPQExpBufferChar(nspnames, '{');
10973 : 1110 : appendPQExpBufferChar(relnames, '{');
10974 : :
10975 : : /*
10976 : : * Scan the TOC for the next set of relevant stats entries. We assume
10977 : : * that statistics are dumped in the order they are listed in the TOC.
10978 : : * This is perhaps not the sturdiest assumption, so we verify it matches
10979 : : * reality in dumpRelationStats_dumper().
10980 : : */
10981 [ + + + + ]: 17066 : for (; te != AH->toc && count < max_rels; te = te->next)
10982 : : {
10983 [ + + ]: 15956 : if ((te->reqs & REQ_STATS) == 0 ||
10984 [ + + ]: 3594 : strcmp(te->desc, "STATISTICS DATA") != 0)
10985 : 12400 : continue;
10986 : :
10987 [ + - ]: 3556 : if (fout->remoteVersion >= 190000)
10988 : : {
10989 : 3556 : const RelStatsInfo *rsinfo = (const RelStatsInfo *) te->defnDumperArg;
10990 : : char relid[32];
10991 : :
10992 : 3556 : sprintf(relid, "%u", rsinfo->relid);
10993 : 3556 : appendPGArray(relids, relid);
10994 : : }
10995 : : else
10996 : : {
10997 : 0 : appendPGArray(nspnames, te->namespace);
10998 : 0 : appendPGArray(relnames, te->tag);
10999 : : }
11000 : :
11001 : 3556 : count++;
11002 : : }
11003 : :
11004 : 1110 : appendPQExpBufferChar(relids, '}');
11005 : 1110 : appendPQExpBufferChar(nspnames, '}');
11006 : 1110 : appendPQExpBufferChar(relnames, '}');
11007 : :
11008 : : /* Execute the query for the next batch of relations. */
11009 [ + + ]: 1110 : if (count > 0)
11010 : : {
11011 : 111 : PQExpBuffer query = createPQExpBuffer();
11012 : :
11013 : 111 : appendPQExpBufferStr(query, "EXECUTE getAttributeStats(");
11014 : :
11015 [ + - ]: 111 : if (fout->remoteVersion >= 190000)
11016 : : {
11017 : 111 : appendStringLiteralAH(query, relids->data, fout);
11018 : 111 : appendPQExpBufferStr(query, "::pg_catalog.oid[])");
11019 : : }
11020 : : else
11021 : : {
11022 : 0 : appendStringLiteralAH(query, nspnames->data, fout);
11023 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[],");
11024 : 0 : appendStringLiteralAH(query, relnames->data, fout);
11025 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[])");
11026 : : }
11027 : :
11028 : 111 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11029 : 111 : destroyPQExpBuffer(query);
11030 : : }
11031 : :
11032 : 1110 : destroyPQExpBuffer(relids);
11033 : 1110 : destroyPQExpBuffer(nspnames);
11034 : 1110 : destroyPQExpBuffer(relnames);
11035 : 1110 : return res;
11036 : : }
11037 : :
11038 : : /*
11039 : : * dumpRelationStats_dumper --
11040 : : *
11041 : : * Generate command to import stats into the relation on the new database.
11042 : : * This routine is called by the Archiver when it wants the statistics to be
11043 : : * dumped.
11044 : : */
11045 : : static char *
11046 : 3556 : dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te)
11047 : : {
11048 : 3556 : const RelStatsInfo *rsinfo = userArg;
11049 : : static PGresult *res;
11050 : : static int rownum;
11051 : : PQExpBuffer query;
11052 : : PQExpBufferData out_data;
11053 : 3556 : PQExpBuffer out = &out_data;
11054 : : int i_schemaname;
11055 : : int i_tablename;
11056 : : int i_attname;
11057 : : int i_inherited;
11058 : : int i_null_frac;
11059 : : int i_avg_width;
11060 : : int i_n_distinct;
11061 : : int i_most_common_vals;
11062 : : int i_most_common_freqs;
11063 : : int i_histogram_bounds;
11064 : : int i_correlation;
11065 : : int i_most_common_elems;
11066 : : int i_most_common_elem_freqs;
11067 : : int i_elem_count_histogram;
11068 : : int i_range_length_histogram;
11069 : : int i_range_empty_frac;
11070 : : int i_range_bounds_histogram;
11071 : : static TocEntry *expected_te;
11072 : :
11073 : : /*
11074 : : * fetchAttributeStats() assumes that the statistics are dumped in the
11075 : : * order they are listed in the TOC. We verify that here for safety.
11076 : : */
11077 [ + + ]: 3556 : if (!expected_te)
11078 : 65 : expected_te = ((ArchiveHandle *) fout)->toc;
11079 : :
11080 : 3556 : expected_te = expected_te->next;
11081 [ + + ]: 13915 : while ((expected_te->reqs & REQ_STATS) == 0 ||
11082 [ + + ]: 3557 : strcmp(expected_te->desc, "STATISTICS DATA") != 0)
11083 : 10359 : expected_te = expected_te->next;
11084 : :
11085 [ - + ]: 3556 : if (te != expected_te)
11086 : 0 : pg_fatal("statistics dumped out of order (current: %d %s %s, expected: %d %s %s)",
11087 : : te->dumpId, te->desc, te->tag,
11088 : : expected_te->dumpId, expected_te->desc, expected_te->tag);
11089 : :
11090 : 3556 : query = createPQExpBuffer();
11091 [ + + ]: 3556 : if (!fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS])
11092 : : {
11093 [ + - ]: 65 : if (fout->remoteVersion >= 190000)
11094 : 65 : appendPQExpBufferStr(query,
11095 : : "PREPARE getAttributeStats(pg_catalog.oid[]) AS\n");
11096 : : else
11097 : 0 : appendPQExpBufferStr(query,
11098 : : "PREPARE getAttributeStats(pg_catalog.name[], pg_catalog.name[]) AS\n");
11099 : :
11100 : 65 : appendPQExpBufferStr(query,
11101 : : "SELECT s.schemaname, s.tablename, s.attname, s.inherited, "
11102 : : "s.null_frac, s.avg_width, s.n_distinct, "
11103 : : "s.most_common_vals, s.most_common_freqs, "
11104 : : "s.histogram_bounds, s.correlation, "
11105 : : "s.most_common_elems, s.most_common_elem_freqs, "
11106 : : "s.elem_count_histogram, ");
11107 : :
11108 [ + - ]: 65 : if (fout->remoteVersion >= 170000)
11109 : 65 : appendPQExpBufferStr(query,
11110 : : "s.range_length_histogram, "
11111 : : "s.range_empty_frac, "
11112 : : "s.range_bounds_histogram ");
11113 : : else
11114 : 0 : appendPQExpBufferStr(query,
11115 : : "NULL AS range_length_histogram,"
11116 : : "NULL AS range_empty_frac,"
11117 : : "NULL AS range_bounds_histogram ");
11118 : :
11119 : : /*
11120 : : * The results must be in the order of the relations supplied in the
11121 : : * parameters to ensure we remain in sync as we walk through the TOC.
11122 : : * The redundant filter clause seems sufficient to convince the
11123 : : * planner to use pg_class_relname_nsp_index, which avoids a full scan
11124 : : * of pg_stats. This may not work for all versions.
11125 : : */
11126 [ + - ]: 65 : if (fout->remoteVersion >= 190000)
11127 : 65 : appendPQExpBufferStr(query,
11128 : : "FROM pg_catalog.pg_stats s "
11129 : : "JOIN unnest($1) WITH ORDINALITY AS u (tableid, ord) "
11130 : : "ON s.tableid = u.tableid "
11131 : : "WHERE s.tableid = ANY($1) "
11132 : : "ORDER BY u.ord, s.attname, s.inherited");
11133 : : else
11134 : 0 : appendPQExpBufferStr(query,
11135 : : "FROM pg_catalog.pg_stats s "
11136 : : "JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) "
11137 : : "ON s.schemaname = u.schemaname "
11138 : : "AND s.tablename = u.tablename "
11139 : : "WHERE s.tablename = ANY($2) "
11140 : : "ORDER BY u.ord, s.attname, s.inherited");
11141 : :
11142 : 65 : ExecuteSqlStatement(fout, query->data);
11143 : :
11144 : 65 : fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS] = true;
11145 : 65 : resetPQExpBuffer(query);
11146 : : }
11147 : :
11148 : 3556 : initPQExpBuffer(out);
11149 : :
11150 : : /* restore relation stats */
11151 : 3556 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(\n");
11152 : 3556 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11153 : : fout->remoteVersion);
11154 : 3556 : appendPQExpBufferStr(out, "\t'schemaname', ");
11155 : 3556 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11156 : 3556 : appendPQExpBufferStr(out, ",\n");
11157 : 3556 : appendPQExpBufferStr(out, "\t'relname', ");
11158 : 3556 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11159 : 3556 : appendPQExpBufferStr(out, ",\n");
11160 : 3556 : appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages);
11161 : :
11162 : : /*
11163 : : * Before v14, a reltuples value of 0 was ambiguous: it could either mean
11164 : : * the relation is empty, or it could mean that it hadn't yet been
11165 : : * vacuumed or analyzed. (Newer versions use -1 for the latter case.)
11166 : : * This ambiguity allegedly can cause the planner to choose inefficient
11167 : : * plans after restoring to v18 or newer. To deal with this, let's just
11168 : : * set reltuples to -1 in that case.
11169 : : */
11170 [ - + - - ]: 3556 : if (fout->remoteVersion < 140000 && strcmp("0", rsinfo->reltuples) == 0)
11171 : 0 : appendPQExpBufferStr(out, "\t'reltuples', '-1'::real,\n");
11172 : : else
11173 : 3556 : appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples);
11174 : :
11175 : 3556 : appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer",
11176 : 3556 : rsinfo->relallvisible);
11177 : :
11178 [ + - ]: 3556 : if (fout->remoteVersion >= 180000)
11179 : 3556 : appendPQExpBuffer(out, ",\n\t'relallfrozen', '%d'::integer", rsinfo->relallfrozen);
11180 : :
11181 : 3556 : appendPQExpBufferStr(out, "\n);\n");
11182 : :
11183 : : /* Fetch the next batch of attribute statistics if needed. */
11184 [ + + ]: 3556 : if (rownum >= PQntuples(res))
11185 : : {
11186 : 1110 : PQclear(res);
11187 : 1110 : res = fetchAttributeStats(fout);
11188 : 1110 : rownum = 0;
11189 : : }
11190 : :
11191 : 3556 : i_schemaname = PQfnumber(res, "schemaname");
11192 : 3556 : i_tablename = PQfnumber(res, "tablename");
11193 : 3556 : i_attname = PQfnumber(res, "attname");
11194 : 3556 : i_inherited = PQfnumber(res, "inherited");
11195 : 3556 : i_null_frac = PQfnumber(res, "null_frac");
11196 : 3556 : i_avg_width = PQfnumber(res, "avg_width");
11197 : 3556 : i_n_distinct = PQfnumber(res, "n_distinct");
11198 : 3556 : i_most_common_vals = PQfnumber(res, "most_common_vals");
11199 : 3556 : i_most_common_freqs = PQfnumber(res, "most_common_freqs");
11200 : 3556 : i_histogram_bounds = PQfnumber(res, "histogram_bounds");
11201 : 3556 : i_correlation = PQfnumber(res, "correlation");
11202 : 3556 : i_most_common_elems = PQfnumber(res, "most_common_elems");
11203 : 3556 : i_most_common_elem_freqs = PQfnumber(res, "most_common_elem_freqs");
11204 : 3556 : i_elem_count_histogram = PQfnumber(res, "elem_count_histogram");
11205 : 3556 : i_range_length_histogram = PQfnumber(res, "range_length_histogram");
11206 : 3556 : i_range_empty_frac = PQfnumber(res, "range_empty_frac");
11207 : 3556 : i_range_bounds_histogram = PQfnumber(res, "range_bounds_histogram");
11208 : :
11209 : : /* restore attribute stats */
11210 [ + + ]: 4501 : for (; rownum < PQntuples(res); rownum++)
11211 : : {
11212 : : const char *attname;
11213 : :
11214 : : /* Stop if the next stat row in our cache isn't for this relation. */
11215 [ + + ]: 3391 : if (strcmp(te->tag, PQgetvalue(res, rownum, i_tablename)) != 0 ||
11216 [ + - ]: 945 : strcmp(te->namespace, PQgetvalue(res, rownum, i_schemaname)) != 0)
11217 : : break;
11218 : :
11219 : 945 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_attribute_stats(\n");
11220 : 945 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11221 : : fout->remoteVersion);
11222 : 945 : appendPQExpBufferStr(out, "\t'schemaname', ");
11223 : 945 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11224 : 945 : appendPQExpBufferStr(out, ",\n\t'relname', ");
11225 : 945 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11226 : :
11227 [ - + ]: 945 : if (PQgetisnull(res, rownum, i_attname))
11228 : 0 : pg_fatal("unexpected null attname");
11229 : 945 : attname = PQgetvalue(res, rownum, i_attname);
11230 : :
11231 : : /*
11232 : : * Indexes look up attname in indAttNames to derive attnum, all others
11233 : : * use attname directly. We must specify attnum for indexes, since
11234 : : * their attnames are not necessarily stable across dump/reload.
11235 : : */
11236 [ + + ]: 945 : if (rsinfo->nindAttNames == 0)
11237 : : {
11238 : 905 : appendPQExpBufferStr(out, ",\n\t'attname', ");
11239 : 905 : appendStringLiteralAH(out, attname, fout);
11240 : : }
11241 : : else
11242 : : {
11243 : 40 : bool found = false;
11244 : :
11245 [ + - ]: 74 : for (int i = 0; i < rsinfo->nindAttNames; i++)
11246 : : {
11247 [ + + ]: 74 : if (strcmp(attname, rsinfo->indAttNames[i]) == 0)
11248 : : {
11249 : 40 : appendPQExpBuffer(out, ",\n\t'attnum', '%d'::smallint",
11250 : : i + 1);
11251 : 40 : found = true;
11252 : 40 : break;
11253 : : }
11254 : : }
11255 : :
11256 [ - + ]: 40 : if (!found)
11257 : 0 : pg_fatal("could not find index attname \"%s\"", attname);
11258 : : }
11259 : :
11260 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_inherited))
11261 : 945 : appendNamedArgument(out, fout, "inherited", "boolean",
11262 : 945 : PQgetvalue(res, rownum, i_inherited));
11263 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_null_frac))
11264 : 945 : appendNamedArgument(out, fout, "null_frac", "real",
11265 : 945 : PQgetvalue(res, rownum, i_null_frac));
11266 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_avg_width))
11267 : 945 : appendNamedArgument(out, fout, "avg_width", "integer",
11268 : 945 : PQgetvalue(res, rownum, i_avg_width));
11269 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_n_distinct))
11270 : 945 : appendNamedArgument(out, fout, "n_distinct", "real",
11271 : 945 : PQgetvalue(res, rownum, i_n_distinct));
11272 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_vals))
11273 : 493 : appendNamedArgument(out, fout, "most_common_vals", "text",
11274 : 493 : PQgetvalue(res, rownum, i_most_common_vals));
11275 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_freqs))
11276 : 493 : appendNamedArgument(out, fout, "most_common_freqs", "real[]",
11277 : 493 : PQgetvalue(res, rownum, i_most_common_freqs));
11278 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_histogram_bounds))
11279 : 606 : appendNamedArgument(out, fout, "histogram_bounds", "text",
11280 : 606 : PQgetvalue(res, rownum, i_histogram_bounds));
11281 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_correlation))
11282 : 903 : appendNamedArgument(out, fout, "correlation", "real",
11283 : 903 : PQgetvalue(res, rownum, i_correlation));
11284 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_elems))
11285 : 8 : appendNamedArgument(out, fout, "most_common_elems", "text",
11286 : 8 : PQgetvalue(res, rownum, i_most_common_elems));
11287 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_elem_freqs))
11288 : 8 : appendNamedArgument(out, fout, "most_common_elem_freqs", "real[]",
11289 : 8 : PQgetvalue(res, rownum, i_most_common_elem_freqs));
11290 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_elem_count_histogram))
11291 : 7 : appendNamedArgument(out, fout, "elem_count_histogram", "real[]",
11292 : 7 : PQgetvalue(res, rownum, i_elem_count_histogram));
11293 [ + - ]: 945 : if (fout->remoteVersion >= 170000)
11294 : : {
11295 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_length_histogram))
11296 : 4 : appendNamedArgument(out, fout, "range_length_histogram", "text",
11297 : 4 : PQgetvalue(res, rownum, i_range_length_histogram));
11298 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_empty_frac))
11299 : 4 : appendNamedArgument(out, fout, "range_empty_frac", "real",
11300 : 4 : PQgetvalue(res, rownum, i_range_empty_frac));
11301 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_bounds_histogram))
11302 : 4 : appendNamedArgument(out, fout, "range_bounds_histogram", "text",
11303 : 4 : PQgetvalue(res, rownum, i_range_bounds_histogram));
11304 : : }
11305 : 945 : appendPQExpBufferStr(out, "\n);\n");
11306 : : }
11307 : :
11308 : 3556 : destroyPQExpBuffer(query);
11309 : 3556 : return out->data;
11310 : : }
11311 : :
11312 : : /*
11313 : : * dumpRelationStats --
11314 : : *
11315 : : * Make an ArchiveEntry for the relation statistics. The Archiver will take
11316 : : * care of gathering the statistics and generating the restore commands when
11317 : : * they are needed.
11318 : : */
11319 : : static void
11320 : 3628 : dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
11321 : : {
11322 : 3628 : const DumpableObject *dobj = &rsinfo->dobj;
11323 : :
11324 : : /* nothing to do if we are not dumping statistics */
11325 [ - + ]: 3628 : if (!fout->dopt->dumpStatistics)
11326 : 0 : return;
11327 : :
11328 : 3628 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11329 : 3628 : ARCHIVE_OPTS(.tag = dobj->name,
11330 : : .namespace = dobj->namespace->dobj.name,
11331 : : .description = "STATISTICS DATA",
11332 : : .section = rsinfo->section,
11333 : : .defnFn = dumpRelationStats_dumper,
11334 : : .defnArg = rsinfo,
11335 : : .deps = dobj->dependencies,
11336 : : .nDeps = dobj->nDeps));
11337 : : }
11338 : :
11339 : : /*
11340 : : * dumpTableComment --
11341 : : *
11342 : : * As above, but dump comments for both the specified table (or view)
11343 : : * and its columns.
11344 : : */
11345 : : static void
11346 : 78 : dumpTableComment(Archive *fout, const TableInfo *tbinfo,
11347 : : const char *reltypename)
11348 : : {
11349 : 78 : DumpOptions *dopt = fout->dopt;
11350 : : CommentItem *comments;
11351 : : int ncomments;
11352 : : PQExpBuffer query;
11353 : : PQExpBuffer tag;
11354 : :
11355 : : /* do nothing, if --no-comments is supplied */
11356 [ - + ]: 78 : if (dopt->no_comments)
11357 : 0 : return;
11358 : :
11359 : : /* Comments are SCHEMA not data */
11360 [ - + ]: 78 : if (!dopt->dumpSchema)
11361 : 0 : return;
11362 : :
11363 : : /* Search for comments associated with relation, using table */
11364 : 78 : ncomments = findComments(tbinfo->dobj.catId.tableoid,
11365 : 78 : tbinfo->dobj.catId.oid,
11366 : : &comments);
11367 : :
11368 : : /* If comments exist, build COMMENT ON statements */
11369 [ - + ]: 78 : if (ncomments <= 0)
11370 : 0 : return;
11371 : :
11372 : 78 : query = createPQExpBuffer();
11373 : 78 : tag = createPQExpBuffer();
11374 : :
11375 [ + + ]: 224 : while (ncomments > 0)
11376 : : {
11377 : 146 : const char *descr = comments->descr;
11378 : 146 : int objsubid = comments->objsubid;
11379 : :
11380 [ + + ]: 146 : if (objsubid == 0)
11381 : : {
11382 : 34 : resetPQExpBuffer(tag);
11383 : 34 : appendPQExpBuffer(tag, "%s %s", reltypename,
11384 : 34 : fmtId(tbinfo->dobj.name));
11385 : :
11386 : 34 : resetPQExpBuffer(query);
11387 : 34 : appendPQExpBuffer(query, "COMMENT ON %s %s IS ", reltypename,
11388 : 34 : fmtQualifiedDumpable(tbinfo));
11389 : 34 : appendStringLiteralAH(query, descr, fout);
11390 : 34 : appendPQExpBufferStr(query, ";\n");
11391 : :
11392 : 34 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11393 : 34 : ARCHIVE_OPTS(.tag = tag->data,
11394 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11395 : : .owner = tbinfo->rolname,
11396 : : .description = "COMMENT",
11397 : : .section = SECTION_NONE,
11398 : : .createStmt = query->data,
11399 : : .deps = &(tbinfo->dobj.dumpId),
11400 : : .nDeps = 1));
11401 : : }
11402 [ + - + - ]: 112 : else if (objsubid > 0 && objsubid <= tbinfo->numatts)
11403 : : {
11404 : 112 : resetPQExpBuffer(tag);
11405 : 112 : appendPQExpBuffer(tag, "COLUMN %s.",
11406 : 112 : fmtId(tbinfo->dobj.name));
11407 : 112 : appendPQExpBufferStr(tag, fmtId(tbinfo->attnames[objsubid - 1]));
11408 : :
11409 : 112 : resetPQExpBuffer(query);
11410 : 112 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11411 : 112 : fmtQualifiedDumpable(tbinfo));
11412 : 112 : appendPQExpBuffer(query, "%s IS ",
11413 : 112 : fmtId(tbinfo->attnames[objsubid - 1]));
11414 : 112 : appendStringLiteralAH(query, descr, fout);
11415 : 112 : appendPQExpBufferStr(query, ";\n");
11416 : :
11417 : 112 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11418 : 112 : ARCHIVE_OPTS(.tag = tag->data,
11419 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11420 : : .owner = tbinfo->rolname,
11421 : : .description = "COMMENT",
11422 : : .section = SECTION_NONE,
11423 : : .createStmt = query->data,
11424 : : .deps = &(tbinfo->dobj.dumpId),
11425 : : .nDeps = 1));
11426 : : }
11427 : :
11428 : 146 : comments++;
11429 : 146 : ncomments--;
11430 : : }
11431 : :
11432 : 78 : destroyPQExpBuffer(query);
11433 : 78 : destroyPQExpBuffer(tag);
11434 : : }
11435 : :
11436 : : /*
11437 : : * findComments --
11438 : : *
11439 : : * Find the comment(s), if any, associated with the given object. All the
11440 : : * objsubid values associated with the given classoid/objoid are found with
11441 : : * one search.
11442 : : */
11443 : : static int
11444 : 6757 : findComments(Oid classoid, Oid objoid, CommentItem **items)
11445 : : {
11446 : 6757 : CommentItem *middle = NULL;
11447 : : CommentItem *low;
11448 : : CommentItem *high;
11449 : : int nmatch;
11450 : :
11451 : : /*
11452 : : * Do binary search to find some item matching the object.
11453 : : */
11454 : 6757 : low = &comments[0];
11455 : 6757 : high = &comments[ncomments - 1];
11456 [ + + ]: 67675 : while (low <= high)
11457 : : {
11458 : 67628 : middle = low + (high - low) / 2;
11459 : :
11460 [ + + ]: 67628 : if (classoid < middle->classoid)
11461 : 7221 : high = middle - 1;
11462 [ + + ]: 60407 : else if (classoid > middle->classoid)
11463 : 7315 : low = middle + 1;
11464 [ + + ]: 53092 : else if (objoid < middle->objoid)
11465 : 22504 : high = middle - 1;
11466 [ + + ]: 30588 : else if (objoid > middle->objoid)
11467 : 23878 : low = middle + 1;
11468 : : else
11469 : 6710 : break; /* found a match */
11470 : : }
11471 : :
11472 [ + + ]: 6757 : if (low > high) /* no matches */
11473 : : {
11474 : 47 : *items = NULL;
11475 : 47 : return 0;
11476 : : }
11477 : :
11478 : : /*
11479 : : * Now determine how many items match the object. The search loop
11480 : : * invariant still holds: only items between low and high inclusive could
11481 : : * match.
11482 : : */
11483 : 6710 : nmatch = 1;
11484 [ + + ]: 6766 : while (middle > low)
11485 : : {
11486 [ + + ]: 3262 : if (classoid != middle[-1].classoid ||
11487 [ + + ]: 3100 : objoid != middle[-1].objoid)
11488 : : break;
11489 : 56 : middle--;
11490 : 56 : nmatch++;
11491 : : }
11492 : :
11493 : 6710 : *items = middle;
11494 : :
11495 : 6710 : middle += nmatch;
11496 [ + + ]: 6722 : while (middle <= high)
11497 : : {
11498 [ + + ]: 3469 : if (classoid != middle->classoid ||
11499 [ + + ]: 3348 : objoid != middle->objoid)
11500 : : break;
11501 : 12 : middle++;
11502 : 12 : nmatch++;
11503 : : }
11504 : :
11505 : 6710 : return nmatch;
11506 : : }
11507 : :
11508 : : /*
11509 : : * collectComments --
11510 : : *
11511 : : * Construct a table of all comments available for database objects;
11512 : : * also set the has-comment component flag for each relevant object.
11513 : : *
11514 : : * We used to do per-object queries for the comments, but it's much faster
11515 : : * to pull them all over at once, and on most databases the memory cost
11516 : : * isn't high.
11517 : : *
11518 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
11519 : : */
11520 : : static void
11521 : 193 : collectComments(Archive *fout)
11522 : : {
11523 : : PGresult *res;
11524 : : PQExpBuffer query;
11525 : : int i_description;
11526 : : int i_classoid;
11527 : : int i_objoid;
11528 : : int i_objsubid;
11529 : : int ntups;
11530 : : int i;
11531 : : DumpableObject *dobj;
11532 : :
11533 : 193 : query = createPQExpBuffer();
11534 : :
11535 : 193 : appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
11536 : : "FROM pg_catalog.pg_description "
11537 : : "ORDER BY classoid, objoid, objsubid");
11538 : :
11539 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11540 : :
11541 : : /* Construct lookup table containing OIDs in numeric form */
11542 : :
11543 : 193 : i_description = PQfnumber(res, "description");
11544 : 193 : i_classoid = PQfnumber(res, "classoid");
11545 : 193 : i_objoid = PQfnumber(res, "objoid");
11546 : 193 : i_objsubid = PQfnumber(res, "objsubid");
11547 : :
11548 : 193 : ntups = PQntuples(res);
11549 : :
11550 : 193 : comments = pg_malloc_array(CommentItem, ntups);
11551 : 193 : ncomments = 0;
11552 : 193 : dobj = NULL;
11553 : :
11554 [ + + ]: 1043112 : for (i = 0; i < ntups; i++)
11555 : : {
11556 : : CatalogId objId;
11557 : : int subid;
11558 : :
11559 : 1042919 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
11560 : 1042919 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
11561 : 1042919 : subid = atoi(PQgetvalue(res, i, i_objsubid));
11562 : :
11563 : : /* We needn't remember comments that don't match any dumpable object */
11564 [ + + ]: 1042919 : if (dobj == NULL ||
11565 [ + + ]: 378529 : dobj->catId.tableoid != objId.tableoid ||
11566 [ + + ]: 376155 : dobj->catId.oid != objId.oid)
11567 : 1042823 : dobj = findObjectByCatalogId(objId);
11568 [ + + ]: 1042919 : if (dobj == NULL)
11569 : 664203 : continue;
11570 : :
11571 : : /*
11572 : : * Comments on columns of composite types are linked to the type's
11573 : : * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
11574 : : * in the type's own DumpableObject.
11575 : : */
11576 [ + + + - ]: 378716 : if (subid != 0 && dobj->objType == DO_TABLE &&
11577 [ + + ]: 206 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
11578 : 48 : {
11579 : : TypeInfo *cTypeInfo;
11580 : :
11581 : 48 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
11582 [ + - ]: 48 : if (cTypeInfo)
11583 : 48 : cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
11584 : : }
11585 : : else
11586 : 378668 : dobj->components |= DUMP_COMPONENT_COMMENT;
11587 : :
11588 : 378716 : comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
11589 : 378716 : comments[ncomments].classoid = objId.tableoid;
11590 : 378716 : comments[ncomments].objoid = objId.oid;
11591 : 378716 : comments[ncomments].objsubid = subid;
11592 : 378716 : ncomments++;
11593 : : }
11594 : :
11595 : 193 : PQclear(res);
11596 : 193 : destroyPQExpBuffer(query);
11597 : 193 : }
11598 : :
11599 : : /*
11600 : : * dumpDumpableObject
11601 : : *
11602 : : * This routine and its subsidiaries are responsible for creating
11603 : : * ArchiveEntries (TOC objects) for each object to be dumped.
11604 : : */
11605 : : static void
11606 : 732763 : dumpDumpableObject(Archive *fout, DumpableObject *dobj)
11607 : : {
11608 : : /*
11609 : : * Clear any dump-request bits for components that don't exist for this
11610 : : * object. (This makes it safe to initially use DUMP_COMPONENT_ALL as the
11611 : : * request for every kind of object.)
11612 : : */
11613 : 732763 : dobj->dump &= dobj->components;
11614 : :
11615 : : /* Now, short-circuit if there's nothing to be done here. */
11616 [ + + ]: 732763 : if (dobj->dump == 0)
11617 : 651880 : return;
11618 : :
11619 [ + + + + : 80883 : switch (dobj->objType)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + - ]
11620 : : {
11621 : 502 : case DO_NAMESPACE:
11622 : 502 : dumpNamespace(fout, (const NamespaceInfo *) dobj);
11623 : 502 : break;
11624 : 25 : case DO_EXTENSION:
11625 : 25 : dumpExtension(fout, (const ExtensionInfo *) dobj);
11626 : 25 : break;
11627 : 961 : case DO_TYPE:
11628 : 961 : dumpType(fout, (const TypeInfo *) dobj);
11629 : 961 : break;
11630 : 76 : case DO_SHELL_TYPE:
11631 : 76 : dumpShellType(fout, (const ShellTypeInfo *) dobj);
11632 : 76 : break;
11633 : 1877 : case DO_FUNC:
11634 : 1877 : dumpFunc(fout, (const FuncInfo *) dobj);
11635 : 1877 : break;
11636 : 295 : case DO_AGG:
11637 : 295 : dumpAgg(fout, (const AggInfo *) dobj);
11638 : 295 : break;
11639 : 2525 : case DO_OPERATOR:
11640 : 2525 : dumpOpr(fout, (const OprInfo *) dobj);
11641 : 2525 : break;
11642 : 84 : case DO_ACCESS_METHOD:
11643 : 84 : dumpAccessMethod(fout, (const AccessMethodInfo *) dobj);
11644 : 84 : break;
11645 : 675 : case DO_OPCLASS:
11646 : 675 : dumpOpclass(fout, (const OpclassInfo *) dobj);
11647 : 675 : break;
11648 : 561 : case DO_OPFAMILY:
11649 : 561 : dumpOpfamily(fout, (const OpfamilyInfo *) dobj);
11650 : 561 : break;
11651 : 2733 : case DO_COLLATION:
11652 : 2733 : dumpCollation(fout, (const CollInfo *) dobj);
11653 : 2733 : break;
11654 : 335 : case DO_CONVERSION:
11655 : 335 : dumpConversion(fout, (const ConvInfo *) dobj);
11656 : 335 : break;
11657 : 33316 : case DO_TABLE:
11658 : 33316 : dumpTable(fout, (const TableInfo *) dobj);
11659 : 33316 : break;
11660 : 1457 : case DO_TABLE_ATTACH:
11661 : 1457 : dumpTableAttach(fout, (const TableAttachInfo *) dobj);
11662 : 1457 : break;
11663 : 1111 : case DO_ATTRDEF:
11664 : 1111 : dumpAttrDef(fout, (const AttrDefInfo *) dobj);
11665 : 1111 : break;
11666 : 2729 : case DO_INDEX:
11667 : 2729 : dumpIndex(fout, (const IndxInfo *) dobj);
11668 : 2729 : break;
11669 : 625 : case DO_INDEX_ATTACH:
11670 : 625 : dumpIndexAttach(fout, (const IndexAttachInfo *) dobj);
11671 : 625 : break;
11672 : 183 : case DO_STATSEXT:
11673 : 183 : dumpStatisticsExt(fout, (const StatsExtInfo *) dobj);
11674 : 183 : dumpStatisticsExtStats(fout, (const StatsExtInfo *) dobj);
11675 : 183 : break;
11676 : 363 : case DO_REFRESH_MATVIEW:
11677 : 363 : refreshMatViewData(fout, (const TableDataInfo *) dobj);
11678 : 363 : break;
11679 : 1177 : case DO_RULE:
11680 : 1177 : dumpRule(fout, (const RuleInfo *) dobj);
11681 : 1177 : break;
11682 : 535 : case DO_TRIGGER:
11683 : 535 : dumpTrigger(fout, (const TriggerInfo *) dobj);
11684 : 535 : break;
11685 : 44 : case DO_EVENT_TRIGGER:
11686 : 44 : dumpEventTrigger(fout, (const EventTriggerInfo *) dobj);
11687 : 44 : break;
11688 : 2403 : case DO_CONSTRAINT:
11689 : 2403 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11690 : 2403 : break;
11691 : 177 : case DO_FK_CONSTRAINT:
11692 : 177 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11693 : 177 : break;
11694 : 87 : case DO_PROCLANG:
11695 : 87 : dumpProcLang(fout, (const ProcLangInfo *) dobj);
11696 : 87 : break;
11697 : 69 : case DO_CAST:
11698 : 69 : dumpCast(fout, (const CastInfo *) dobj);
11699 : 69 : break;
11700 : 44 : case DO_TRANSFORM:
11701 : 44 : dumpTransform(fout, (const TransformInfo *) dobj);
11702 : 44 : break;
11703 : 399 : case DO_SEQUENCE_SET:
11704 : 399 : dumpSequenceData(fout, (const TableDataInfo *) dobj);
11705 : 399 : break;
11706 : 4428 : case DO_TABLE_DATA:
11707 : 4428 : dumpTableData(fout, (const TableDataInfo *) dobj);
11708 : 4428 : break;
11709 : 14893 : case DO_DUMMY_TYPE:
11710 : : /* table rowtypes and array types are never dumped separately */
11711 : 14893 : break;
11712 : 44 : case DO_TSPARSER:
11713 : 44 : dumpTSParser(fout, (const TSParserInfo *) dobj);
11714 : 44 : break;
11715 : 182 : case DO_TSDICT:
11716 : 182 : dumpTSDictionary(fout, (const TSDictInfo *) dobj);
11717 : 182 : break;
11718 : 56 : case DO_TSTEMPLATE:
11719 : 56 : dumpTSTemplate(fout, (const TSTemplateInfo *) dobj);
11720 : 56 : break;
11721 : 157 : case DO_TSCONFIG:
11722 : 157 : dumpTSConfig(fout, (const TSConfigInfo *) dobj);
11723 : 157 : break;
11724 : 54 : case DO_FDW:
11725 : 54 : dumpForeignDataWrapper(fout, (const FdwInfo *) dobj);
11726 : 54 : break;
11727 : 58 : case DO_FOREIGN_SERVER:
11728 : 58 : dumpForeignServer(fout, (const ForeignServerInfo *) dobj);
11729 : 58 : break;
11730 : 170 : case DO_DEFAULT_ACL:
11731 : 170 : dumpDefaultACL(fout, (const DefaultACLInfo *) dobj);
11732 : 170 : break;
11733 : 88 : case DO_LARGE_OBJECT:
11734 : 88 : dumpLO(fout, (const LoInfo *) dobj);
11735 : 88 : break;
11736 : 88 : case DO_LARGE_OBJECT_DATA:
11737 [ + - ]: 88 : if (dobj->dump & DUMP_COMPONENT_DATA)
11738 : : {
11739 : : LoInfo *loinfo;
11740 : : TocEntry *te;
11741 : :
11742 : 88 : loinfo = (LoInfo *) findObjectByDumpId(dobj->dependencies[0]);
11743 [ - + ]: 88 : if (loinfo == NULL)
11744 : 0 : pg_fatal("missing metadata for large objects \"%s\"",
11745 : : dobj->name);
11746 : :
11747 : 88 : te = ArchiveEntry(fout, dobj->catId, dobj->dumpId,
11748 : 88 : ARCHIVE_OPTS(.tag = dobj->name,
11749 : : .owner = loinfo->rolname,
11750 : : .description = "BLOBS",
11751 : : .section = SECTION_DATA,
11752 : : .deps = dobj->dependencies,
11753 : : .nDeps = dobj->nDeps,
11754 : : .dumpFn = dumpLOs,
11755 : : .dumpArg = loinfo));
11756 : :
11757 : : /*
11758 : : * Set the TocEntry's dataLength in case we are doing a
11759 : : * parallel dump and want to order dump jobs by table size.
11760 : : * (We need some size estimate for every TocEntry with a
11761 : : * DataDumper function.) We don't currently have any cheap
11762 : : * way to estimate the size of LOs, but fortunately it doesn't
11763 : : * matter too much as long as we get large batches of LOs
11764 : : * processed reasonably early. Assume 8K per blob.
11765 : : */
11766 : 88 : te->dataLength = loinfo->numlos * (pgoff_t) 8192;
11767 : : }
11768 : 88 : break;
11769 : 347 : case DO_POLICY:
11770 : 347 : dumpPolicy(fout, (const PolicyInfo *) dobj);
11771 : 347 : break;
11772 : 416 : case DO_PUBLICATION:
11773 : 416 : dumpPublication(fout, (const PublicationInfo *) dobj);
11774 : 416 : break;
11775 : 298 : case DO_PUBLICATION_REL:
11776 : 298 : dumpPublicationTable(fout, (const PublicationRelInfo *) dobj);
11777 : 298 : break;
11778 : 103 : case DO_PUBLICATION_TABLE_IN_SCHEMA:
11779 : 103 : dumpPublicationNamespace(fout,
11780 : : (const PublicationSchemaInfo *) dobj);
11781 : 103 : break;
11782 : 116 : case DO_SUBSCRIPTION:
11783 : 116 : dumpSubscription(fout, (const SubscriptionInfo *) dobj);
11784 : 116 : break;
11785 : 3 : case DO_SUBSCRIPTION_REL:
11786 : 3 : dumpSubscriptionTable(fout, (const SubRelInfo *) dobj);
11787 : 3 : break;
11788 : 3628 : case DO_REL_STATS:
11789 : 3628 : dumpRelationStats(fout, (const RelStatsInfo *) dobj);
11790 : 3628 : break;
11791 : 386 : case DO_PRE_DATA_BOUNDARY:
11792 : : case DO_POST_DATA_BOUNDARY:
11793 : : /* never dumped, nothing to do */
11794 : 386 : break;
11795 : : }
11796 : : }
11797 : :
11798 : : /*
11799 : : * dumpNamespace
11800 : : * writes out to fout the queries to recreate a user-defined namespace
11801 : : */
11802 : : static void
11803 : 502 : dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo)
11804 : : {
11805 : 502 : DumpOptions *dopt = fout->dopt;
11806 : : PQExpBuffer q;
11807 : : PQExpBuffer delq;
11808 : : char *qnspname;
11809 : :
11810 : : /* Do nothing if not dumping schema */
11811 [ + + ]: 502 : if (!dopt->dumpSchema)
11812 : 29 : return;
11813 : :
11814 : 473 : q = createPQExpBuffer();
11815 : 473 : delq = createPQExpBuffer();
11816 : :
11817 : 473 : qnspname = pg_strdup(fmtId(nspinfo->dobj.name));
11818 : :
11819 [ + + ]: 473 : if (nspinfo->create)
11820 : : {
11821 : 316 : appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
11822 : 316 : appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname);
11823 : : }
11824 : : else
11825 : : {
11826 : : /* see selectDumpableNamespace() */
11827 : 157 : appendPQExpBufferStr(delq,
11828 : : "-- *not* dropping schema, since initdb creates it\n");
11829 : 157 : appendPQExpBufferStr(q,
11830 : : "-- *not* creating schema, since initdb creates it\n");
11831 : : }
11832 : :
11833 [ + + ]: 473 : if (dopt->binary_upgrade)
11834 : 102 : binary_upgrade_extension_member(q, &nspinfo->dobj,
11835 : : "SCHEMA", qnspname, NULL);
11836 : :
11837 [ + + ]: 473 : if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11838 : 191 : ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
11839 : 191 : ARCHIVE_OPTS(.tag = nspinfo->dobj.name,
11840 : : .owner = nspinfo->rolname,
11841 : : .description = "SCHEMA",
11842 : : .section = SECTION_PRE_DATA,
11843 : : .createStmt = q->data,
11844 : : .dropStmt = delq->data));
11845 : :
11846 : : /* Dump Schema Comments and Security Labels */
11847 [ + + ]: 473 : if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11848 : : {
11849 : 162 : const char *initdb_comment = NULL;
11850 : :
11851 [ + + + + ]: 162 : if (!nspinfo->create && strcmp(qnspname, "public") == 0)
11852 : 117 : initdb_comment = "standard public schema";
11853 : 162 : dumpCommentExtended(fout, "SCHEMA", qnspname,
11854 : 162 : NULL, nspinfo->rolname,
11855 : 162 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId,
11856 : : initdb_comment);
11857 : : }
11858 : :
11859 [ - + ]: 473 : if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11860 : 0 : dumpSecLabel(fout, "SCHEMA", qnspname,
11861 : 0 : NULL, nspinfo->rolname,
11862 : 0 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
11863 : :
11864 [ + + ]: 473 : if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL)
11865 : 369 : dumpACL(fout, nspinfo->dobj.dumpId, InvalidDumpId, "SCHEMA",
11866 : : qnspname, NULL, NULL,
11867 : 369 : NULL, nspinfo->rolname, &nspinfo->dacl);
11868 : :
11869 : 473 : pg_free(qnspname);
11870 : :
11871 : 473 : destroyPQExpBuffer(q);
11872 : 473 : destroyPQExpBuffer(delq);
11873 : : }
11874 : :
11875 : : /*
11876 : : * dumpExtension
11877 : : * writes out to fout the queries to recreate an extension
11878 : : */
11879 : : static void
11880 : 25 : dumpExtension(Archive *fout, const ExtensionInfo *extinfo)
11881 : : {
11882 : 25 : DumpOptions *dopt = fout->dopt;
11883 : : PQExpBuffer q;
11884 : : PQExpBuffer delq;
11885 : : char *qextname;
11886 : :
11887 : : /* Do nothing if not dumping schema */
11888 [ + + ]: 25 : if (!dopt->dumpSchema)
11889 : 1 : return;
11890 : :
11891 : 24 : q = createPQExpBuffer();
11892 : 24 : delq = createPQExpBuffer();
11893 : :
11894 : 24 : qextname = pg_strdup(fmtId(extinfo->dobj.name));
11895 : :
11896 : 24 : appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname);
11897 : :
11898 [ + + ]: 24 : if (!dopt->binary_upgrade)
11899 : : {
11900 : : /*
11901 : : * In a regular dump, we simply create the extension, intentionally
11902 : : * not specifying a version, so that the destination installation's
11903 : : * default version is used.
11904 : : *
11905 : : * Use of IF NOT EXISTS here is unlike our behavior for other object
11906 : : * types; but there are various scenarios in which it's convenient to
11907 : : * manually create the desired extension before restoring, so we
11908 : : * prefer to allow it to exist already.
11909 : : */
11910 : 17 : appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n",
11911 : 17 : qextname, fmtId(extinfo->namespace));
11912 : : }
11913 : : else
11914 : : {
11915 : : /*
11916 : : * In binary-upgrade mode, it's critical to reproduce the state of the
11917 : : * database exactly, so our procedure is to create an empty extension,
11918 : : * restore all the contained objects normally, and add them to the
11919 : : * extension one by one. This function performs just the first of
11920 : : * those steps. binary_upgrade_extension_member() takes care of
11921 : : * adding member objects as they're created.
11922 : : */
11923 : : int i;
11924 : : int n;
11925 : :
11926 : 7 : appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
11927 : :
11928 : : /*
11929 : : * We unconditionally create the extension, so we must drop it if it
11930 : : * exists. This could happen if the user deleted 'plpgsql' and then
11931 : : * readded it, causing its oid to be greater than g_last_builtin_oid.
11932 : : */
11933 : 7 : appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
11934 : :
11935 : 7 : appendPQExpBufferStr(q,
11936 : : "SELECT pg_catalog.binary_upgrade_create_empty_extension(");
11937 : 7 : appendStringLiteralAH(q, extinfo->dobj.name, fout);
11938 : 7 : appendPQExpBufferStr(q, ", ");
11939 : 7 : appendStringLiteralAH(q, extinfo->namespace, fout);
11940 : 7 : appendPQExpBufferStr(q, ", ");
11941 [ + - ]: 7 : appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false");
11942 : 7 : appendStringLiteralAH(q, extinfo->extversion, fout);
11943 : 7 : appendPQExpBufferStr(q, ", ");
11944 : :
11945 : : /*
11946 : : * Note that we're pushing extconfig (an OID array) back into
11947 : : * pg_extension exactly as-is. This is OK because pg_class OIDs are
11948 : : * preserved in binary upgrade.
11949 : : */
11950 [ + + ]: 7 : if (strlen(extinfo->extconfig) > 2)
11951 : 1 : appendStringLiteralAH(q, extinfo->extconfig, fout);
11952 : : else
11953 : 6 : appendPQExpBufferStr(q, "NULL");
11954 : 7 : appendPQExpBufferStr(q, ", ");
11955 [ + + ]: 7 : if (strlen(extinfo->extcondition) > 2)
11956 : 1 : appendStringLiteralAH(q, extinfo->extcondition, fout);
11957 : : else
11958 : 6 : appendPQExpBufferStr(q, "NULL");
11959 : 7 : appendPQExpBufferStr(q, ", ");
11960 : 7 : appendPQExpBufferStr(q, "ARRAY[");
11961 : 7 : n = 0;
11962 [ + + ]: 14 : for (i = 0; i < extinfo->dobj.nDeps; i++)
11963 : : {
11964 : : DumpableObject *extobj;
11965 : :
11966 : 7 : extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]);
11967 [ + - - + ]: 7 : if (extobj && extobj->objType == DO_EXTENSION)
11968 : : {
11969 [ # # ]: 0 : if (n++ > 0)
11970 : 0 : appendPQExpBufferChar(q, ',');
11971 : 0 : appendStringLiteralAH(q, extobj->name, fout);
11972 : : }
11973 : : }
11974 : 7 : appendPQExpBufferStr(q, "]::pg_catalog.text[]");
11975 : 7 : appendPQExpBufferStr(q, ");\n");
11976 : : }
11977 : :
11978 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11979 : 24 : ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId,
11980 : 24 : ARCHIVE_OPTS(.tag = extinfo->dobj.name,
11981 : : .description = "EXTENSION",
11982 : : .section = SECTION_PRE_DATA,
11983 : : .createStmt = q->data,
11984 : : .dropStmt = delq->data));
11985 : :
11986 : : /* Dump Extension Comments */
11987 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11988 : 24 : dumpComment(fout, "EXTENSION", qextname,
11989 : : NULL, "",
11990 : 24 : extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
11991 : :
11992 : 24 : pg_free(qextname);
11993 : :
11994 : 24 : destroyPQExpBuffer(q);
11995 : 24 : destroyPQExpBuffer(delq);
11996 : : }
11997 : :
11998 : : /*
11999 : : * dumpType
12000 : : * writes out to fout the queries to recreate a user-defined type
12001 : : */
12002 : : static void
12003 : 961 : dumpType(Archive *fout, const TypeInfo *tyinfo)
12004 : : {
12005 : 961 : DumpOptions *dopt = fout->dopt;
12006 : :
12007 : : /* Do nothing if not dumping schema */
12008 [ + + ]: 961 : if (!dopt->dumpSchema)
12009 : 56 : return;
12010 : :
12011 : : /* Dump out in proper style */
12012 [ + + ]: 905 : if (tyinfo->typtype == TYPTYPE_BASE)
12013 : 285 : dumpBaseType(fout, tyinfo);
12014 [ + + ]: 620 : else if (tyinfo->typtype == TYPTYPE_DOMAIN)
12015 : 164 : dumpDomain(fout, tyinfo);
12016 [ + + ]: 456 : else if (tyinfo->typtype == TYPTYPE_COMPOSITE)
12017 : 132 : dumpCompositeType(fout, tyinfo);
12018 [ + + ]: 324 : else if (tyinfo->typtype == TYPTYPE_ENUM)
12019 : 89 : dumpEnumType(fout, tyinfo);
12020 [ + + ]: 235 : else if (tyinfo->typtype == TYPTYPE_RANGE)
12021 : 121 : dumpRangeType(fout, tyinfo);
12022 [ + - + + ]: 114 : else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
12023 : 39 : dumpUndefinedType(fout, tyinfo);
12024 : : else
12025 : 75 : pg_log_warning("typtype of data type \"%s\" appears to be invalid",
12026 : : tyinfo->dobj.name);
12027 : : }
12028 : :
12029 : : /*
12030 : : * dumpEnumType
12031 : : * writes out to fout the queries to recreate a user-defined enum type
12032 : : */
12033 : : static void
12034 : 89 : dumpEnumType(Archive *fout, const TypeInfo *tyinfo)
12035 : : {
12036 : 89 : DumpOptions *dopt = fout->dopt;
12037 : 89 : PQExpBuffer q = createPQExpBuffer();
12038 : 89 : PQExpBuffer delq = createPQExpBuffer();
12039 : 89 : PQExpBuffer query = createPQExpBuffer();
12040 : : PGresult *res;
12041 : : int num,
12042 : : i;
12043 : : Oid enum_oid;
12044 : : char *qtypname;
12045 : : char *qualtypname;
12046 : : char *label;
12047 : : int i_enumlabel;
12048 : : int i_oid;
12049 : :
12050 [ + + ]: 89 : if (!fout->is_prepared[PREPQUERY_DUMPENUMTYPE])
12051 : : {
12052 : : /* Set up query for enum-specific details */
12053 : 42 : appendPQExpBufferStr(query,
12054 : : "PREPARE dumpEnumType(pg_catalog.oid) AS\n"
12055 : : "SELECT oid, enumlabel "
12056 : : "FROM pg_catalog.pg_enum "
12057 : : "WHERE enumtypid = $1 "
12058 : : "ORDER BY enumsortorder");
12059 : :
12060 : 42 : ExecuteSqlStatement(fout, query->data);
12061 : :
12062 : 42 : fout->is_prepared[PREPQUERY_DUMPENUMTYPE] = true;
12063 : : }
12064 : :
12065 : 89 : printfPQExpBuffer(query,
12066 : : "EXECUTE dumpEnumType('%u')",
12067 : 89 : tyinfo->dobj.catId.oid);
12068 : :
12069 : 89 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12070 : :
12071 : 89 : num = PQntuples(res);
12072 : :
12073 : 89 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12074 : 89 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12075 : :
12076 : : /*
12077 : : * CASCADE shouldn't be required here as for normal types since the I/O
12078 : : * functions are generic and do not get dropped.
12079 : : */
12080 : 89 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12081 : :
12082 [ + + ]: 89 : if (dopt->binary_upgrade)
12083 : 6 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12084 : 6 : tyinfo->dobj.catId.oid,
12085 : : false, false);
12086 : :
12087 : 89 : appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
12088 : : qualtypname);
12089 : :
12090 [ + + ]: 89 : if (!dopt->binary_upgrade)
12091 : : {
12092 : 83 : i_enumlabel = PQfnumber(res, "enumlabel");
12093 : :
12094 : : /* Labels with server-assigned oids */
12095 [ + + ]: 498 : for (i = 0; i < num; i++)
12096 : : {
12097 : 415 : label = PQgetvalue(res, i, i_enumlabel);
12098 [ + + ]: 415 : if (i > 0)
12099 : 332 : appendPQExpBufferChar(q, ',');
12100 : 415 : appendPQExpBufferStr(q, "\n ");
12101 : 415 : appendStringLiteralAH(q, label, fout);
12102 : : }
12103 : : }
12104 : :
12105 : 89 : appendPQExpBufferStr(q, "\n);\n");
12106 : :
12107 [ + + ]: 89 : if (dopt->binary_upgrade)
12108 : : {
12109 : 6 : i_oid = PQfnumber(res, "oid");
12110 : 6 : i_enumlabel = PQfnumber(res, "enumlabel");
12111 : :
12112 : : /* Labels with dump-assigned (preserved) oids */
12113 [ + + ]: 62 : for (i = 0; i < num; i++)
12114 : : {
12115 : 56 : enum_oid = atooid(PQgetvalue(res, i, i_oid));
12116 : 56 : label = PQgetvalue(res, i, i_enumlabel);
12117 : :
12118 [ + + ]: 56 : if (i == 0)
12119 : 6 : appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
12120 : 56 : appendPQExpBuffer(q,
12121 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n",
12122 : : enum_oid);
12123 : 56 : appendPQExpBuffer(q, "ALTER TYPE %s ADD VALUE ", qualtypname);
12124 : 56 : appendStringLiteralAH(q, label, fout);
12125 : 56 : appendPQExpBufferStr(q, ";\n\n");
12126 : : }
12127 : : }
12128 : :
12129 [ + + ]: 89 : if (dopt->binary_upgrade)
12130 : 6 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12131 : : "TYPE", qtypname,
12132 : 6 : tyinfo->dobj.namespace->dobj.name);
12133 : :
12134 [ + - ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12135 : 89 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12136 : 89 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12137 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12138 : : .owner = tyinfo->rolname,
12139 : : .description = "TYPE",
12140 : : .section = SECTION_PRE_DATA,
12141 : : .createStmt = q->data,
12142 : : .dropStmt = delq->data));
12143 : :
12144 : : /* Dump Type Comments and Security Labels */
12145 [ + + ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12146 : 34 : dumpComment(fout, "TYPE", qtypname,
12147 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12148 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12149 : :
12150 [ - + ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12151 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12152 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12153 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12154 : :
12155 [ + + ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12156 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12157 : : qtypname, NULL,
12158 : 34 : tyinfo->dobj.namespace->dobj.name,
12159 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12160 : :
12161 : 89 : PQclear(res);
12162 : 89 : destroyPQExpBuffer(q);
12163 : 89 : destroyPQExpBuffer(delq);
12164 : 89 : destroyPQExpBuffer(query);
12165 : 89 : pg_free(qtypname);
12166 : 89 : pg_free(qualtypname);
12167 : 89 : }
12168 : :
12169 : : /*
12170 : : * dumpRangeType
12171 : : * writes out to fout the queries to recreate a user-defined range type
12172 : : */
12173 : : static void
12174 : 121 : dumpRangeType(Archive *fout, const TypeInfo *tyinfo)
12175 : : {
12176 : 121 : DumpOptions *dopt = fout->dopt;
12177 : 121 : PQExpBuffer q = createPQExpBuffer();
12178 : 121 : PQExpBuffer delq = createPQExpBuffer();
12179 : 121 : PQExpBuffer query = createPQExpBuffer();
12180 : : PGresult *res;
12181 : : Oid collationOid;
12182 : : char *qtypname;
12183 : : char *qualtypname;
12184 : : char *procname;
12185 : :
12186 [ + + ]: 121 : if (!fout->is_prepared[PREPQUERY_DUMPRANGETYPE])
12187 : : {
12188 : : /* Set up query for range-specific details */
12189 : 42 : appendPQExpBufferStr(query,
12190 : : "PREPARE dumpRangeType(pg_catalog.oid) AS\n");
12191 : :
12192 : 42 : appendPQExpBufferStr(query,
12193 : : "SELECT ");
12194 : :
12195 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12196 : 42 : appendPQExpBufferStr(query,
12197 : : "pg_catalog.format_type(rngmultitypid, NULL) AS rngmultitype, ");
12198 : : else
12199 : 0 : appendPQExpBufferStr(query,
12200 : : "NULL AS rngmultitype, ");
12201 : :
12202 : 42 : appendPQExpBufferStr(query,
12203 : : "pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
12204 : : "opc.opcname AS opcname, "
12205 : : "(SELECT nspname FROM pg_catalog.pg_namespace nsp "
12206 : : " WHERE nsp.oid = opc.opcnamespace) AS opcnsp, "
12207 : : "opc.opcdefault, "
12208 : : "CASE WHEN rngcollation = st.typcollation THEN 0 "
12209 : : " ELSE rngcollation END AS collation, "
12210 : : "rngcanonical, rngsubdiff "
12211 : : "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, "
12212 : : " pg_catalog.pg_opclass opc "
12213 : : "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND "
12214 : : "rngtypid = $1");
12215 : :
12216 : 42 : ExecuteSqlStatement(fout, query->data);
12217 : :
12218 : 42 : fout->is_prepared[PREPQUERY_DUMPRANGETYPE] = true;
12219 : : }
12220 : :
12221 : 121 : printfPQExpBuffer(query,
12222 : : "EXECUTE dumpRangeType('%u')",
12223 : 121 : tyinfo->dobj.catId.oid);
12224 : :
12225 : 121 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12226 : :
12227 : 121 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12228 : 121 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12229 : :
12230 : : /*
12231 : : * CASCADE shouldn't be required here as for normal types since the I/O
12232 : : * functions are generic and do not get dropped.
12233 : : */
12234 : 121 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12235 : :
12236 [ + + ]: 121 : if (dopt->binary_upgrade)
12237 : 9 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12238 : 9 : tyinfo->dobj.catId.oid,
12239 : : false, true);
12240 : :
12241 : 121 : appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
12242 : : qualtypname);
12243 : :
12244 : 121 : appendPQExpBuffer(q, "\n subtype = %s",
12245 : : PQgetvalue(res, 0, PQfnumber(res, "rngsubtype")));
12246 : :
12247 [ + - ]: 121 : if (!PQgetisnull(res, 0, PQfnumber(res, "rngmultitype")))
12248 : 121 : appendPQExpBuffer(q, ",\n multirange_type_name = %s",
12249 : : PQgetvalue(res, 0, PQfnumber(res, "rngmultitype")));
12250 : :
12251 : : /* print subtype_opclass only if not default for subtype */
12252 [ + + ]: 121 : if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't')
12253 : : {
12254 : 34 : char *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname"));
12255 : 34 : char *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp"));
12256 : :
12257 : 34 : appendPQExpBuffer(q, ",\n subtype_opclass = %s.",
12258 : : fmtId(nspname));
12259 : 34 : appendPQExpBufferStr(q, fmtId(opcname));
12260 : : }
12261 : :
12262 : 121 : collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation")));
12263 [ + + ]: 121 : if (OidIsValid(collationOid))
12264 : : {
12265 : 39 : CollInfo *coll = findCollationByOid(collationOid);
12266 : :
12267 [ + - ]: 39 : if (coll)
12268 : 39 : appendPQExpBuffer(q, ",\n collation = %s",
12269 : 39 : fmtQualifiedDumpable(coll));
12270 : : }
12271 : :
12272 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical"));
12273 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12274 : 9 : appendPQExpBuffer(q, ",\n canonical = %s", procname);
12275 : :
12276 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff"));
12277 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12278 : 23 : appendPQExpBuffer(q, ",\n subtype_diff = %s", procname);
12279 : :
12280 : 121 : appendPQExpBufferStr(q, "\n);\n");
12281 : :
12282 [ + + ]: 121 : if (dopt->binary_upgrade)
12283 : 9 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12284 : : "TYPE", qtypname,
12285 : 9 : tyinfo->dobj.namespace->dobj.name);
12286 : :
12287 [ + - ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12288 : 121 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12289 : 121 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12290 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12291 : : .owner = tyinfo->rolname,
12292 : : .description = "TYPE",
12293 : : .section = SECTION_PRE_DATA,
12294 : : .createStmt = q->data,
12295 : : .dropStmt = delq->data));
12296 : :
12297 : : /* Dump Type Comments and Security Labels */
12298 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12299 : 52 : dumpComment(fout, "TYPE", qtypname,
12300 : 52 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12301 : 52 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12302 : :
12303 [ - + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12304 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12305 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12306 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12307 : :
12308 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12309 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12310 : : qtypname, NULL,
12311 : 34 : tyinfo->dobj.namespace->dobj.name,
12312 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12313 : :
12314 : 121 : PQclear(res);
12315 : 121 : destroyPQExpBuffer(q);
12316 : 121 : destroyPQExpBuffer(delq);
12317 : 121 : destroyPQExpBuffer(query);
12318 : 121 : pg_free(qtypname);
12319 : 121 : pg_free(qualtypname);
12320 : 121 : }
12321 : :
12322 : : /*
12323 : : * dumpUndefinedType
12324 : : * writes out to fout the queries to recreate a !typisdefined type
12325 : : *
12326 : : * This is a shell type, but we use different terminology to distinguish
12327 : : * this case from where we have to emit a shell type definition to break
12328 : : * circular dependencies. An undefined type shouldn't ever have anything
12329 : : * depending on it.
12330 : : */
12331 : : static void
12332 : 39 : dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo)
12333 : : {
12334 : 39 : DumpOptions *dopt = fout->dopt;
12335 : 39 : PQExpBuffer q = createPQExpBuffer();
12336 : 39 : PQExpBuffer delq = createPQExpBuffer();
12337 : : char *qtypname;
12338 : : char *qualtypname;
12339 : :
12340 : 39 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12341 : 39 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12342 : :
12343 : 39 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12344 : :
12345 [ + + ]: 39 : if (dopt->binary_upgrade)
12346 : 2 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12347 : 2 : tyinfo->dobj.catId.oid,
12348 : : false, false);
12349 : :
12350 : 39 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
12351 : : qualtypname);
12352 : :
12353 [ + + ]: 39 : if (dopt->binary_upgrade)
12354 : 2 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12355 : : "TYPE", qtypname,
12356 : 2 : tyinfo->dobj.namespace->dobj.name);
12357 : :
12358 [ + - ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12359 : 39 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12360 : 39 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12361 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12362 : : .owner = tyinfo->rolname,
12363 : : .description = "TYPE",
12364 : : .section = SECTION_PRE_DATA,
12365 : : .createStmt = q->data,
12366 : : .dropStmt = delq->data));
12367 : :
12368 : : /* Dump Type Comments and Security Labels */
12369 [ + + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12370 : 34 : dumpComment(fout, "TYPE", qtypname,
12371 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12372 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12373 : :
12374 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12375 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12376 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12377 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12378 : :
12379 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12380 : 0 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12381 : : qtypname, NULL,
12382 : 0 : tyinfo->dobj.namespace->dobj.name,
12383 : 0 : NULL, tyinfo->rolname, &tyinfo->dacl);
12384 : :
12385 : 39 : destroyPQExpBuffer(q);
12386 : 39 : destroyPQExpBuffer(delq);
12387 : 39 : pg_free(qtypname);
12388 : 39 : pg_free(qualtypname);
12389 : 39 : }
12390 : :
12391 : : /*
12392 : : * dumpBaseType
12393 : : * writes out to fout the queries to recreate a user-defined base type
12394 : : */
12395 : : static void
12396 : 285 : dumpBaseType(Archive *fout, const TypeInfo *tyinfo)
12397 : : {
12398 : 285 : DumpOptions *dopt = fout->dopt;
12399 : 285 : PQExpBuffer q = createPQExpBuffer();
12400 : 285 : PQExpBuffer delq = createPQExpBuffer();
12401 : 285 : PQExpBuffer query = createPQExpBuffer();
12402 : : PGresult *res;
12403 : : char *qtypname;
12404 : : char *qualtypname;
12405 : : char *typlen;
12406 : : char *typinput;
12407 : : char *typoutput;
12408 : : char *typreceive;
12409 : : char *typsend;
12410 : : char *typmodin;
12411 : : char *typmodout;
12412 : : char *typanalyze;
12413 : : char *typsubscript;
12414 : : Oid typreceiveoid;
12415 : : Oid typsendoid;
12416 : : Oid typmodinoid;
12417 : : Oid typmodoutoid;
12418 : : Oid typanalyzeoid;
12419 : : Oid typsubscriptoid;
12420 : : char *typcategory;
12421 : : char *typispreferred;
12422 : : char *typdelim;
12423 : : char *typbyval;
12424 : : char *typalign;
12425 : : char *typstorage;
12426 : : char *typcollatable;
12427 : : char *typdefault;
12428 : 285 : bool typdefault_is_literal = false;
12429 : :
12430 [ + + ]: 285 : if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
12431 : : {
12432 : : /* Set up query for type-specific details */
12433 : 42 : appendPQExpBufferStr(query,
12434 : : "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
12435 : : "SELECT typlen, "
12436 : : "typinput, typoutput, typreceive, typsend, "
12437 : : "typreceive::pg_catalog.oid AS typreceiveoid, "
12438 : : "typsend::pg_catalog.oid AS typsendoid, "
12439 : : "typanalyze, "
12440 : : "typanalyze::pg_catalog.oid AS typanalyzeoid, "
12441 : : "typdelim, typbyval, typalign, typstorage, "
12442 : : "typmodin, typmodout, "
12443 : : "typmodin::pg_catalog.oid AS typmodinoid, "
12444 : : "typmodout::pg_catalog.oid AS typmodoutoid, "
12445 : : "typcategory, typispreferred, "
12446 : : "(typcollation <> 0) AS typcollatable, "
12447 : : "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
12448 : :
12449 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12450 : 42 : appendPQExpBufferStr(query,
12451 : : "typsubscript, "
12452 : : "typsubscript::pg_catalog.oid AS typsubscriptoid ");
12453 : : else
12454 : 0 : appendPQExpBufferStr(query,
12455 : : "'-' AS typsubscript, 0 AS typsubscriptoid ");
12456 : :
12457 : 42 : appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
12458 : : "WHERE oid = $1");
12459 : :
12460 : 42 : ExecuteSqlStatement(fout, query->data);
12461 : :
12462 : 42 : fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
12463 : : }
12464 : :
12465 : 285 : printfPQExpBuffer(query,
12466 : : "EXECUTE dumpBaseType('%u')",
12467 : 285 : tyinfo->dobj.catId.oid);
12468 : :
12469 : 285 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12470 : :
12471 : 285 : typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
12472 : 285 : typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
12473 : 285 : typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
12474 : 285 : typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
12475 : 285 : typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
12476 : 285 : typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
12477 : 285 : typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
12478 : 285 : typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
12479 : 285 : typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
12480 : 285 : typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
12481 : 285 : typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
12482 : 285 : typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
12483 : 285 : typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
12484 : 285 : typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
12485 : 285 : typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
12486 : 285 : typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
12487 : 285 : typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
12488 : 285 : typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
12489 : 285 : typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
12490 : 285 : typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
12491 : 285 : typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
12492 : 285 : typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
12493 [ - + ]: 285 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12494 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12495 [ + + ]: 285 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12496 : : {
12497 : 44 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12498 : 44 : typdefault_is_literal = true; /* it needs quotes */
12499 : : }
12500 : : else
12501 : 241 : typdefault = NULL;
12502 : :
12503 : 285 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12504 : 285 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12505 : :
12506 : : /*
12507 : : * The reason we include CASCADE is that the circular dependency between
12508 : : * the type and its I/O functions makes it impossible to drop the type any
12509 : : * other way.
12510 : : */
12511 : 285 : appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
12512 : :
12513 : : /*
12514 : : * We might already have a shell type, but setting pg_type_oid is
12515 : : * harmless, and in any case we'd better set the array type OID.
12516 : : */
12517 [ + + ]: 285 : if (dopt->binary_upgrade)
12518 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12519 : 8 : tyinfo->dobj.catId.oid,
12520 : : false, false);
12521 : :
12522 : 285 : appendPQExpBuffer(q,
12523 : : "CREATE TYPE %s (\n"
12524 : : " INTERNALLENGTH = %s",
12525 : : qualtypname,
12526 [ + + ]: 285 : (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
12527 : :
12528 : : /* regproc result is sufficiently quoted already */
12529 : 285 : appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
12530 : 285 : appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
12531 [ + + ]: 285 : if (OidIsValid(typreceiveoid))
12532 : 210 : appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
12533 [ + + ]: 285 : if (OidIsValid(typsendoid))
12534 : 210 : appendPQExpBuffer(q, ",\n SEND = %s", typsend);
12535 [ + + ]: 285 : if (OidIsValid(typmodinoid))
12536 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
12537 [ + + ]: 285 : if (OidIsValid(typmodoutoid))
12538 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
12539 [ + + ]: 285 : if (OidIsValid(typanalyzeoid))
12540 : 3 : appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
12541 : :
12542 [ + + ]: 285 : if (strcmp(typcollatable, "t") == 0)
12543 : 30 : appendPQExpBufferStr(q, ",\n COLLATABLE = true");
12544 : :
12545 [ + + ]: 285 : if (typdefault != NULL)
12546 : : {
12547 : 44 : appendPQExpBufferStr(q, ",\n DEFAULT = ");
12548 [ + - ]: 44 : if (typdefault_is_literal)
12549 : 44 : appendStringLiteralAH(q, typdefault, fout);
12550 : : else
12551 : 0 : appendPQExpBufferStr(q, typdefault);
12552 : : }
12553 : :
12554 [ + + ]: 285 : if (OidIsValid(typsubscriptoid))
12555 : 29 : appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
12556 : :
12557 [ + + ]: 285 : if (OidIsValid(tyinfo->typelem))
12558 : 26 : appendPQExpBuffer(q, ",\n ELEMENT = %s",
12559 : 26 : getFormattedTypeName(fout, tyinfo->typelem,
12560 : : zeroIsError));
12561 : :
12562 [ + + ]: 285 : if (strcmp(typcategory, "U") != 0)
12563 : : {
12564 : 161 : appendPQExpBufferStr(q, ",\n CATEGORY = ");
12565 : 161 : appendStringLiteralAH(q, typcategory, fout);
12566 : : }
12567 : :
12568 [ + + ]: 285 : if (strcmp(typispreferred, "t") == 0)
12569 : 29 : appendPQExpBufferStr(q, ",\n PREFERRED = true");
12570 : :
12571 [ + - + + ]: 285 : if (typdelim && strcmp(typdelim, ",") != 0)
12572 : : {
12573 : 3 : appendPQExpBufferStr(q, ",\n DELIMITER = ");
12574 : 3 : appendStringLiteralAH(q, typdelim, fout);
12575 : : }
12576 : :
12577 [ + + ]: 285 : if (*typalign == TYPALIGN_CHAR)
12578 : 12 : appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
12579 [ + + ]: 273 : else if (*typalign == TYPALIGN_SHORT)
12580 : 6 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
12581 [ + + ]: 267 : else if (*typalign == TYPALIGN_INT)
12582 : 189 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
12583 [ + - ]: 78 : else if (*typalign == TYPALIGN_DOUBLE)
12584 : 78 : appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
12585 : :
12586 [ + + ]: 285 : if (*typstorage == TYPSTORAGE_PLAIN)
12587 : 210 : appendPQExpBufferStr(q, ",\n STORAGE = plain");
12588 [ - + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTERNAL)
12589 : 0 : appendPQExpBufferStr(q, ",\n STORAGE = external");
12590 [ + + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTENDED)
12591 : 66 : appendPQExpBufferStr(q, ",\n STORAGE = extended");
12592 [ + - ]: 9 : else if (*typstorage == TYPSTORAGE_MAIN)
12593 : 9 : appendPQExpBufferStr(q, ",\n STORAGE = main");
12594 : :
12595 [ + + ]: 285 : if (strcmp(typbyval, "t") == 0)
12596 : 139 : appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
12597 : :
12598 : 285 : appendPQExpBufferStr(q, "\n);\n");
12599 : :
12600 [ + + ]: 285 : if (dopt->binary_upgrade)
12601 : 8 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12602 : : "TYPE", qtypname,
12603 : 8 : tyinfo->dobj.namespace->dobj.name);
12604 : :
12605 [ + - ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12606 : 285 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12607 : 285 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12608 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12609 : : .owner = tyinfo->rolname,
12610 : : .description = "TYPE",
12611 : : .section = SECTION_PRE_DATA,
12612 : : .createStmt = q->data,
12613 : : .dropStmt = delq->data));
12614 : :
12615 : : /* Dump Type Comments and Security Labels */
12616 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12617 : 250 : dumpComment(fout, "TYPE", qtypname,
12618 : 250 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12619 : 250 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12620 : :
12621 [ - + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12622 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12623 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12624 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12625 : :
12626 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12627 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12628 : : qtypname, NULL,
12629 : 34 : tyinfo->dobj.namespace->dobj.name,
12630 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12631 : :
12632 : 285 : PQclear(res);
12633 : 285 : destroyPQExpBuffer(q);
12634 : 285 : destroyPQExpBuffer(delq);
12635 : 285 : destroyPQExpBuffer(query);
12636 : 285 : pg_free(qtypname);
12637 : 285 : pg_free(qualtypname);
12638 : 285 : }
12639 : :
12640 : : /*
12641 : : * dumpDomain
12642 : : * writes out to fout the queries to recreate a user-defined domain
12643 : : */
12644 : : static void
12645 : 164 : dumpDomain(Archive *fout, const TypeInfo *tyinfo)
12646 : : {
12647 : 164 : DumpOptions *dopt = fout->dopt;
12648 : 164 : PQExpBuffer q = createPQExpBuffer();
12649 : 164 : PQExpBuffer delq = createPQExpBuffer();
12650 : 164 : PQExpBuffer query = createPQExpBuffer();
12651 : : PGresult *res;
12652 : : int i;
12653 : : char *qtypname;
12654 : : char *qualtypname;
12655 : : char *typnotnull;
12656 : : char *typdefn;
12657 : : char *typdefault;
12658 : : Oid typcollation;
12659 : 164 : bool typdefault_is_literal = false;
12660 : :
12661 [ + + ]: 164 : if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN])
12662 : : {
12663 : : /* Set up query for domain-specific details */
12664 : 39 : appendPQExpBufferStr(query,
12665 : : "PREPARE dumpDomain(pg_catalog.oid) AS\n");
12666 : :
12667 : 39 : appendPQExpBufferStr(query, "SELECT t.typnotnull, "
12668 : : "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
12669 : : "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
12670 : : "t.typdefault, "
12671 : : "CASE WHEN t.typcollation <> u.typcollation "
12672 : : "THEN t.typcollation ELSE 0 END AS typcollation "
12673 : : "FROM pg_catalog.pg_type t "
12674 : : "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
12675 : : "WHERE t.oid = $1");
12676 : :
12677 : 39 : ExecuteSqlStatement(fout, query->data);
12678 : :
12679 : 39 : fout->is_prepared[PREPQUERY_DUMPDOMAIN] = true;
12680 : : }
12681 : :
12682 : 164 : printfPQExpBuffer(query,
12683 : : "EXECUTE dumpDomain('%u')",
12684 : 164 : tyinfo->dobj.catId.oid);
12685 : :
12686 : 164 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12687 : :
12688 : 164 : typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
12689 : 164 : typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
12690 [ + + ]: 164 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12691 : 39 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12692 [ - + ]: 125 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12693 : : {
12694 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12695 : 0 : typdefault_is_literal = true; /* it needs quotes */
12696 : : }
12697 : : else
12698 : 125 : typdefault = NULL;
12699 : 164 : typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
12700 : :
12701 [ + + ]: 164 : if (dopt->binary_upgrade)
12702 : 27 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12703 : 27 : tyinfo->dobj.catId.oid,
12704 : : true, /* force array type */
12705 : : false); /* force multirange type */
12706 : :
12707 : 164 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12708 : 164 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12709 : :
12710 : 164 : appendPQExpBuffer(q,
12711 : : "CREATE DOMAIN %s AS %s",
12712 : : qualtypname,
12713 : : typdefn);
12714 : :
12715 : : /* Print collation only if different from base type's collation */
12716 [ + + ]: 164 : if (OidIsValid(typcollation))
12717 : : {
12718 : : CollInfo *coll;
12719 : :
12720 : 34 : coll = findCollationByOid(typcollation);
12721 [ + - ]: 34 : if (coll)
12722 : 34 : appendPQExpBuffer(q, " COLLATE %s", fmtQualifiedDumpable(coll));
12723 : : }
12724 : :
12725 : : /*
12726 : : * Print a not-null constraint if there's one. In servers older than 17
12727 : : * these don't have names, so just print it unadorned; in newer ones they
12728 : : * do, but most of the time it's going to be the standard generated one,
12729 : : * so omit the name in that case also.
12730 : : */
12731 [ + + ]: 164 : if (typnotnull[0] == 't')
12732 : : {
12733 [ + - - + ]: 49 : if (fout->remoteVersion < 170000 || tyinfo->notnull == NULL)
12734 : 0 : appendPQExpBufferStr(q, " NOT NULL");
12735 : : else
12736 : : {
12737 : 49 : ConstraintInfo *notnull = tyinfo->notnull;
12738 : :
12739 [ + - ]: 49 : if (!notnull->separate)
12740 : : {
12741 : : char *default_name;
12742 : :
12743 : : /* XXX should match ChooseConstraintName better */
12744 : 49 : default_name = psprintf("%s_not_null", tyinfo->dobj.name);
12745 : :
12746 [ + + ]: 49 : if (strcmp(default_name, notnull->dobj.name) == 0)
12747 : 15 : appendPQExpBufferStr(q, " NOT NULL");
12748 : : else
12749 : 34 : appendPQExpBuffer(q, " CONSTRAINT %s %s",
12750 : 34 : fmtId(notnull->dobj.name), notnull->condef);
12751 : 49 : pfree(default_name);
12752 : : }
12753 : : }
12754 : : }
12755 : :
12756 [ + + ]: 164 : if (typdefault != NULL)
12757 : : {
12758 : 39 : appendPQExpBufferStr(q, " DEFAULT ");
12759 [ - + ]: 39 : if (typdefault_is_literal)
12760 : 0 : appendStringLiteralAH(q, typdefault, fout);
12761 : : else
12762 : 39 : appendPQExpBufferStr(q, typdefault);
12763 : : }
12764 : :
12765 : 164 : PQclear(res);
12766 : :
12767 : : /*
12768 : : * Add any CHECK constraints for the domain
12769 : : */
12770 [ + + ]: 283 : for (i = 0; i < tyinfo->nDomChecks; i++)
12771 : : {
12772 : 119 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12773 : :
12774 [ + + + - ]: 119 : if (!domcheck->separate && domcheck->contype == 'c')
12775 : 114 : appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
12776 : 114 : fmtId(domcheck->dobj.name), domcheck->condef);
12777 : : }
12778 : :
12779 : 164 : appendPQExpBufferStr(q, ";\n");
12780 : :
12781 : 164 : appendPQExpBuffer(delq, "DROP DOMAIN %s;\n", qualtypname);
12782 : :
12783 [ + + ]: 164 : if (dopt->binary_upgrade)
12784 : 27 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12785 : : "DOMAIN", qtypname,
12786 : 27 : tyinfo->dobj.namespace->dobj.name);
12787 : :
12788 [ + - ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12789 : 164 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12790 : 164 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12791 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12792 : : .owner = tyinfo->rolname,
12793 : : .description = "DOMAIN",
12794 : : .section = SECTION_PRE_DATA,
12795 : : .createStmt = q->data,
12796 : : .dropStmt = delq->data));
12797 : :
12798 : : /* Dump Domain Comments and Security Labels */
12799 [ - + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12800 : 0 : dumpComment(fout, "DOMAIN", qtypname,
12801 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12802 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12803 : :
12804 [ - + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12805 : 0 : dumpSecLabel(fout, "DOMAIN", qtypname,
12806 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12807 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12808 : :
12809 [ + + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12810 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12811 : : qtypname, NULL,
12812 : 34 : tyinfo->dobj.namespace->dobj.name,
12813 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12814 : :
12815 : : /* Dump any per-constraint comments */
12816 [ + + ]: 283 : for (i = 0; i < tyinfo->nDomChecks; i++)
12817 : : {
12818 : 119 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12819 : : PQExpBuffer conprefix;
12820 : :
12821 : : /* but only if the constraint itself was dumped here */
12822 [ + + ]: 119 : if (domcheck->separate)
12823 : 5 : continue;
12824 : :
12825 : 114 : conprefix = createPQExpBuffer();
12826 : 114 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12827 : 114 : fmtId(domcheck->dobj.name));
12828 : :
12829 [ + + ]: 114 : if (domcheck->dobj.dump & DUMP_COMPONENT_COMMENT)
12830 : 34 : dumpComment(fout, conprefix->data, qtypname,
12831 : 34 : tyinfo->dobj.namespace->dobj.name,
12832 : 34 : tyinfo->rolname,
12833 : 34 : domcheck->dobj.catId, 0, tyinfo->dobj.dumpId);
12834 : :
12835 : 114 : destroyPQExpBuffer(conprefix);
12836 : : }
12837 : :
12838 : : /*
12839 : : * And a comment on the not-null constraint, if there's one -- but only if
12840 : : * the constraint itself was dumped here
12841 : : */
12842 [ + + + - ]: 164 : if (tyinfo->notnull != NULL && !tyinfo->notnull->separate)
12843 : : {
12844 : 49 : PQExpBuffer conprefix = createPQExpBuffer();
12845 : :
12846 : 49 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12847 : 49 : fmtId(tyinfo->notnull->dobj.name));
12848 : :
12849 [ + + ]: 49 : if (tyinfo->notnull->dobj.dump & DUMP_COMPONENT_COMMENT)
12850 : 34 : dumpComment(fout, conprefix->data, qtypname,
12851 : 34 : tyinfo->dobj.namespace->dobj.name,
12852 : 34 : tyinfo->rolname,
12853 : 34 : tyinfo->notnull->dobj.catId, 0, tyinfo->dobj.dumpId);
12854 : 49 : destroyPQExpBuffer(conprefix);
12855 : : }
12856 : :
12857 : 164 : destroyPQExpBuffer(q);
12858 : 164 : destroyPQExpBuffer(delq);
12859 : 164 : destroyPQExpBuffer(query);
12860 : 164 : pg_free(qtypname);
12861 : 164 : pg_free(qualtypname);
12862 : 164 : }
12863 : :
12864 : : /*
12865 : : * dumpCompositeType
12866 : : * writes out to fout the queries to recreate a user-defined stand-alone
12867 : : * composite type
12868 : : */
12869 : : static void
12870 : 132 : dumpCompositeType(Archive *fout, const TypeInfo *tyinfo)
12871 : : {
12872 : 132 : DumpOptions *dopt = fout->dopt;
12873 : 132 : PQExpBuffer q = createPQExpBuffer();
12874 : 132 : PQExpBuffer dropped = createPQExpBuffer();
12875 : 132 : PQExpBuffer delq = createPQExpBuffer();
12876 : 132 : PQExpBuffer query = createPQExpBuffer();
12877 : : PGresult *res;
12878 : : char *qtypname;
12879 : : char *qualtypname;
12880 : : int ntups;
12881 : : int i_attname;
12882 : : int i_atttypdefn;
12883 : : int i_attlen;
12884 : : int i_attalign;
12885 : : int i_attisdropped;
12886 : : int i_attcollation;
12887 : : int i;
12888 : : int actual_atts;
12889 : :
12890 [ + + ]: 132 : if (!fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE])
12891 : : {
12892 : : /*
12893 : : * Set up query for type-specific details.
12894 : : *
12895 : : * Since we only want to dump COLLATE clauses for attributes whose
12896 : : * collation is different from their type's default, we use a CASE
12897 : : * here to suppress uninteresting attcollations cheaply. atttypid
12898 : : * will be 0 for dropped columns; collation does not matter for those.
12899 : : */
12900 : 57 : appendPQExpBufferStr(query,
12901 : : "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
12902 : : "SELECT a.attname, a.attnum, "
12903 : : "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
12904 : : "a.attlen, a.attalign, a.attisdropped, "
12905 : : "CASE WHEN a.attcollation <> at.typcollation "
12906 : : "THEN a.attcollation ELSE 0 END AS attcollation "
12907 : : "FROM pg_catalog.pg_type ct "
12908 : : "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
12909 : : "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
12910 : : "WHERE ct.oid = $1 "
12911 : : "ORDER BY a.attnum");
12912 : :
12913 : 57 : ExecuteSqlStatement(fout, query->data);
12914 : :
12915 : 57 : fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE] = true;
12916 : : }
12917 : :
12918 : 132 : printfPQExpBuffer(query,
12919 : : "EXECUTE dumpCompositeType('%u')",
12920 : 132 : tyinfo->dobj.catId.oid);
12921 : :
12922 : 132 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12923 : :
12924 : 132 : ntups = PQntuples(res);
12925 : :
12926 : 132 : i_attname = PQfnumber(res, "attname");
12927 : 132 : i_atttypdefn = PQfnumber(res, "atttypdefn");
12928 : 132 : i_attlen = PQfnumber(res, "attlen");
12929 : 132 : i_attalign = PQfnumber(res, "attalign");
12930 : 132 : i_attisdropped = PQfnumber(res, "attisdropped");
12931 : 132 : i_attcollation = PQfnumber(res, "attcollation");
12932 : :
12933 [ + + ]: 132 : if (dopt->binary_upgrade)
12934 : : {
12935 : 18 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12936 : 18 : tyinfo->dobj.catId.oid,
12937 : : false, false);
12938 : 18 : binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid);
12939 : : }
12940 : :
12941 : 132 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12942 : 132 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12943 : :
12944 : 132 : appendPQExpBuffer(q, "CREATE TYPE %s AS (",
12945 : : qualtypname);
12946 : :
12947 : 132 : actual_atts = 0;
12948 [ + + ]: 418 : for (i = 0; i < ntups; i++)
12949 : : {
12950 : : char *attname;
12951 : : char *atttypdefn;
12952 : : char *attlen;
12953 : : char *attalign;
12954 : : bool attisdropped;
12955 : : Oid attcollation;
12956 : :
12957 : 286 : attname = PQgetvalue(res, i, i_attname);
12958 : 286 : atttypdefn = PQgetvalue(res, i, i_atttypdefn);
12959 : 286 : attlen = PQgetvalue(res, i, i_attlen);
12960 : 286 : attalign = PQgetvalue(res, i, i_attalign);
12961 : 286 : attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
12962 : 286 : attcollation = atooid(PQgetvalue(res, i, i_attcollation));
12963 : :
12964 [ + + + + ]: 286 : if (attisdropped && !dopt->binary_upgrade)
12965 : 8 : continue;
12966 : :
12967 : : /* Format properly if not first attr */
12968 [ + + ]: 278 : if (actual_atts++ > 0)
12969 : 146 : appendPQExpBufferChar(q, ',');
12970 : 278 : appendPQExpBufferStr(q, "\n\t");
12971 : :
12972 [ + + ]: 278 : if (!attisdropped)
12973 : : {
12974 : 276 : appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
12975 : :
12976 : : /* Add collation if not default for the column type */
12977 [ - + ]: 276 : if (OidIsValid(attcollation))
12978 : : {
12979 : : CollInfo *coll;
12980 : :
12981 : 0 : coll = findCollationByOid(attcollation);
12982 [ # # ]: 0 : if (coll)
12983 : 0 : appendPQExpBuffer(q, " COLLATE %s",
12984 : 0 : fmtQualifiedDumpable(coll));
12985 : : }
12986 : : }
12987 : : else
12988 : : {
12989 : : /*
12990 : : * This is a dropped attribute and we're in binary_upgrade mode.
12991 : : * Insert a placeholder for it in the CREATE TYPE command, and set
12992 : : * length and alignment with direct UPDATE to the catalogs
12993 : : * afterwards. See similar code in dumpTableSchema().
12994 : : */
12995 : 2 : appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
12996 : :
12997 : : /* stash separately for insertion after the CREATE TYPE */
12998 : 2 : appendPQExpBufferStr(dropped,
12999 : : "\n-- For binary upgrade, recreate dropped column.\n");
13000 : 2 : appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
13001 : : "SET attlen = %s, "
13002 : : "attalign = '%s', attbyval = false\n"
13003 : : "WHERE attname = ", attlen, attalign);
13004 : 2 : appendStringLiteralAH(dropped, attname, fout);
13005 : 2 : appendPQExpBufferStr(dropped, "\n AND attrelid = ");
13006 : 2 : appendStringLiteralAH(dropped, qualtypname, fout);
13007 : 2 : appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
13008 : :
13009 : 2 : appendPQExpBuffer(dropped, "ALTER TYPE %s ",
13010 : : qualtypname);
13011 : 2 : appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
13012 : : fmtId(attname));
13013 : : }
13014 : : }
13015 : 132 : appendPQExpBufferStr(q, "\n);\n");
13016 : 132 : appendPQExpBufferStr(q, dropped->data);
13017 : :
13018 : 132 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
13019 : :
13020 [ + + ]: 132 : if (dopt->binary_upgrade)
13021 : 18 : binary_upgrade_extension_member(q, &tyinfo->dobj,
13022 : : "TYPE", qtypname,
13023 : 18 : tyinfo->dobj.namespace->dobj.name);
13024 : :
13025 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13026 : 115 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
13027 : 115 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
13028 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13029 : : .owner = tyinfo->rolname,
13030 : : .description = "TYPE",
13031 : : .section = SECTION_PRE_DATA,
13032 : : .createStmt = q->data,
13033 : : .dropStmt = delq->data));
13034 : :
13035 : :
13036 : : /* Dump Type Comments and Security Labels */
13037 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13038 : 34 : dumpComment(fout, "TYPE", qtypname,
13039 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13040 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13041 : :
13042 [ - + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13043 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
13044 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13045 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13046 : :
13047 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
13048 : 18 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
13049 : : qtypname, NULL,
13050 : 18 : tyinfo->dobj.namespace->dobj.name,
13051 : 18 : NULL, tyinfo->rolname, &tyinfo->dacl);
13052 : :
13053 : : /* Dump any per-column comments */
13054 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13055 : 34 : dumpCompositeTypeColComments(fout, tyinfo, res);
13056 : :
13057 : 132 : PQclear(res);
13058 : 132 : destroyPQExpBuffer(q);
13059 : 132 : destroyPQExpBuffer(dropped);
13060 : 132 : destroyPQExpBuffer(delq);
13061 : 132 : destroyPQExpBuffer(query);
13062 : 132 : pg_free(qtypname);
13063 : 132 : pg_free(qualtypname);
13064 : 132 : }
13065 : :
13066 : : /*
13067 : : * dumpCompositeTypeColComments
13068 : : * writes out to fout the queries to recreate comments on the columns of
13069 : : * a user-defined stand-alone composite type.
13070 : : *
13071 : : * The caller has already made a query to collect the names and attnums
13072 : : * of the type's columns, so we just pass that result into here rather
13073 : : * than reading them again.
13074 : : */
13075 : : static void
13076 : 34 : dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
13077 : : PGresult *res)
13078 : : {
13079 : : CommentItem *comments;
13080 : : int ncomments;
13081 : : PQExpBuffer query;
13082 : : PQExpBuffer target;
13083 : : int i;
13084 : : int ntups;
13085 : : int i_attname;
13086 : : int i_attnum;
13087 : : int i_attisdropped;
13088 : :
13089 : : /* do nothing, if --no-comments is supplied */
13090 [ - + ]: 34 : if (fout->dopt->no_comments)
13091 : 0 : return;
13092 : :
13093 : : /* Search for comments associated with type's pg_class OID */
13094 : 34 : ncomments = findComments(RelationRelationId, tyinfo->typrelid,
13095 : : &comments);
13096 : :
13097 : : /* If no comments exist, we're done */
13098 [ - + ]: 34 : if (ncomments <= 0)
13099 : 0 : return;
13100 : :
13101 : : /* Build COMMENT ON statements */
13102 : 34 : query = createPQExpBuffer();
13103 : 34 : target = createPQExpBuffer();
13104 : :
13105 : 34 : ntups = PQntuples(res);
13106 : 34 : i_attnum = PQfnumber(res, "attnum");
13107 : 34 : i_attname = PQfnumber(res, "attname");
13108 : 34 : i_attisdropped = PQfnumber(res, "attisdropped");
13109 [ + + ]: 68 : while (ncomments > 0)
13110 : : {
13111 : : const char *attname;
13112 : :
13113 : 34 : attname = NULL;
13114 [ + - ]: 34 : for (i = 0; i < ntups; i++)
13115 : : {
13116 [ + - ]: 34 : if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
13117 [ + - ]: 34 : PQgetvalue(res, i, i_attisdropped)[0] != 't')
13118 : : {
13119 : 34 : attname = PQgetvalue(res, i, i_attname);
13120 : 34 : break;
13121 : : }
13122 : : }
13123 [ + - ]: 34 : if (attname) /* just in case we don't find it */
13124 : : {
13125 : 34 : const char *descr = comments->descr;
13126 : :
13127 : 34 : resetPQExpBuffer(target);
13128 : 34 : appendPQExpBuffer(target, "COLUMN %s.",
13129 : 34 : fmtId(tyinfo->dobj.name));
13130 : 34 : appendPQExpBufferStr(target, fmtId(attname));
13131 : :
13132 : 34 : resetPQExpBuffer(query);
13133 : 34 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
13134 : 34 : fmtQualifiedDumpable(tyinfo));
13135 : 34 : appendPQExpBuffer(query, "%s IS ", fmtId(attname));
13136 : 34 : appendStringLiteralAH(query, descr, fout);
13137 : 34 : appendPQExpBufferStr(query, ";\n");
13138 : :
13139 : 34 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
13140 : 34 : ARCHIVE_OPTS(.tag = target->data,
13141 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13142 : : .owner = tyinfo->rolname,
13143 : : .description = "COMMENT",
13144 : : .section = SECTION_NONE,
13145 : : .createStmt = query->data,
13146 : : .deps = &(tyinfo->dobj.dumpId),
13147 : : .nDeps = 1));
13148 : : }
13149 : :
13150 : 34 : comments++;
13151 : 34 : ncomments--;
13152 : : }
13153 : :
13154 : 34 : destroyPQExpBuffer(query);
13155 : 34 : destroyPQExpBuffer(target);
13156 : : }
13157 : :
13158 : : /*
13159 : : * dumpShellType
13160 : : * writes out to fout the queries to create a shell type
13161 : : *
13162 : : * We dump a shell definition in advance of the I/O functions for the type.
13163 : : */
13164 : : static void
13165 : 76 : dumpShellType(Archive *fout, const ShellTypeInfo *stinfo)
13166 : : {
13167 : 76 : DumpOptions *dopt = fout->dopt;
13168 : : PQExpBuffer q;
13169 : :
13170 : : /* Do nothing if not dumping schema */
13171 [ + + ]: 76 : if (!dopt->dumpSchema)
13172 : 7 : return;
13173 : :
13174 : 69 : q = createPQExpBuffer();
13175 : :
13176 : : /*
13177 : : * Note the lack of a DROP command for the shell type; any required DROP
13178 : : * is driven off the base type entry, instead. This interacts with
13179 : : * _printTocEntry()'s use of the presence of a DROP command to decide
13180 : : * whether an entry needs an ALTER OWNER command. We don't want to alter
13181 : : * the shell type's owner immediately on creation; that should happen only
13182 : : * after it's filled in, otherwise the backend complains.
13183 : : */
13184 : :
13185 [ + + ]: 69 : if (dopt->binary_upgrade)
13186 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
13187 : 8 : stinfo->baseType->dobj.catId.oid,
13188 : : false, false);
13189 : :
13190 : 69 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
13191 : 69 : fmtQualifiedDumpable(stinfo));
13192 : :
13193 [ + - ]: 69 : if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13194 : 69 : ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId,
13195 : 69 : ARCHIVE_OPTS(.tag = stinfo->dobj.name,
13196 : : .namespace = stinfo->dobj.namespace->dobj.name,
13197 : : .owner = stinfo->baseType->rolname,
13198 : : .description = "SHELL TYPE",
13199 : : .section = SECTION_PRE_DATA,
13200 : : .createStmt = q->data));
13201 : :
13202 : 69 : destroyPQExpBuffer(q);
13203 : : }
13204 : :
13205 : : /*
13206 : : * dumpProcLang
13207 : : * writes out to fout the queries to recreate a user-defined
13208 : : * procedural language
13209 : : */
13210 : : static void
13211 : 87 : dumpProcLang(Archive *fout, const ProcLangInfo *plang)
13212 : : {
13213 : 87 : DumpOptions *dopt = fout->dopt;
13214 : : PQExpBuffer defqry;
13215 : : PQExpBuffer delqry;
13216 : : bool useParams;
13217 : : char *qlanname;
13218 : : FuncInfo *funcInfo;
13219 : 87 : FuncInfo *inlineInfo = NULL;
13220 : 87 : FuncInfo *validatorInfo = NULL;
13221 : :
13222 : : /* Do nothing if not dumping schema */
13223 [ + + ]: 87 : if (!dopt->dumpSchema)
13224 : 14 : return;
13225 : :
13226 : : /*
13227 : : * Try to find the support function(s). It is not an error if we don't
13228 : : * find them --- if the functions are in the pg_catalog schema, as is
13229 : : * standard in 8.1 and up, then we won't have loaded them. (In this case
13230 : : * we will emit a parameterless CREATE LANGUAGE command, which will
13231 : : * require PL template knowledge in the backend to reload.)
13232 : : */
13233 : :
13234 : 73 : funcInfo = findFuncByOid(plang->lanplcallfoid);
13235 [ + + + + ]: 73 : if (funcInfo != NULL && !funcInfo->dobj.dump)
13236 : 2 : funcInfo = NULL; /* treat not-dumped same as not-found */
13237 : :
13238 [ + + ]: 73 : if (OidIsValid(plang->laninline))
13239 : : {
13240 : 40 : inlineInfo = findFuncByOid(plang->laninline);
13241 [ + + + - ]: 40 : if (inlineInfo != NULL && !inlineInfo->dobj.dump)
13242 : 1 : inlineInfo = NULL;
13243 : : }
13244 : :
13245 [ + + ]: 73 : if (OidIsValid(plang->lanvalidator))
13246 : : {
13247 : 40 : validatorInfo = findFuncByOid(plang->lanvalidator);
13248 [ + + + - ]: 40 : if (validatorInfo != NULL && !validatorInfo->dobj.dump)
13249 : 1 : validatorInfo = NULL;
13250 : : }
13251 : :
13252 : : /*
13253 : : * If the functions are dumpable then emit a complete CREATE LANGUAGE with
13254 : : * parameters. Otherwise, we'll write a parameterless command, which will
13255 : : * be interpreted as CREATE EXTENSION.
13256 : : */
13257 [ + - ]: 32 : useParams = (funcInfo != NULL &&
13258 [ + + + - : 137 : (inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
+ - ]
13259 [ + - ]: 32 : (validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
13260 : :
13261 : 73 : defqry = createPQExpBuffer();
13262 : 73 : delqry = createPQExpBuffer();
13263 : :
13264 : 73 : qlanname = pg_strdup(fmtId(plang->dobj.name));
13265 : :
13266 : 73 : appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
13267 : : qlanname);
13268 : :
13269 [ + + ]: 73 : if (useParams)
13270 : : {
13271 : 32 : appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
13272 [ - + ]: 32 : plang->lanpltrusted ? "TRUSTED " : "",
13273 : : qlanname);
13274 : 32 : appendPQExpBuffer(defqry, " HANDLER %s",
13275 : 32 : fmtQualifiedDumpable(funcInfo));
13276 [ - + ]: 32 : if (OidIsValid(plang->laninline))
13277 : 0 : appendPQExpBuffer(defqry, " INLINE %s",
13278 : 0 : fmtQualifiedDumpable(inlineInfo));
13279 [ - + ]: 32 : if (OidIsValid(plang->lanvalidator))
13280 : 0 : appendPQExpBuffer(defqry, " VALIDATOR %s",
13281 : 0 : fmtQualifiedDumpable(validatorInfo));
13282 : : }
13283 : : else
13284 : : {
13285 : : /*
13286 : : * If not dumping parameters, then use CREATE OR REPLACE so that the
13287 : : * command will not fail if the language is preinstalled in the target
13288 : : * database.
13289 : : *
13290 : : * Modern servers will interpret this as CREATE EXTENSION IF NOT
13291 : : * EXISTS; perhaps we should emit that instead? But it might just add
13292 : : * confusion.
13293 : : */
13294 : 41 : appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s",
13295 : : qlanname);
13296 : : }
13297 : 73 : appendPQExpBufferStr(defqry, ";\n");
13298 : :
13299 [ + + ]: 73 : if (dopt->binary_upgrade)
13300 : 2 : binary_upgrade_extension_member(defqry, &plang->dobj,
13301 : : "LANGUAGE", qlanname, NULL);
13302 : :
13303 [ + + ]: 73 : if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION)
13304 : 33 : ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
13305 : 33 : ARCHIVE_OPTS(.tag = plang->dobj.name,
13306 : : .owner = plang->lanowner,
13307 : : .description = "PROCEDURAL LANGUAGE",
13308 : : .section = SECTION_PRE_DATA,
13309 : : .createStmt = defqry->data,
13310 : : .dropStmt = delqry->data,
13311 : : ));
13312 : :
13313 : : /* Dump Proc Lang Comments and Security Labels */
13314 [ - + ]: 73 : if (plang->dobj.dump & DUMP_COMPONENT_COMMENT)
13315 : 0 : dumpComment(fout, "LANGUAGE", qlanname,
13316 : 0 : NULL, plang->lanowner,
13317 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13318 : :
13319 [ - + ]: 73 : if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL)
13320 : 0 : dumpSecLabel(fout, "LANGUAGE", qlanname,
13321 : 0 : NULL, plang->lanowner,
13322 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13323 : :
13324 [ + + + - ]: 73 : if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL)
13325 : 40 : dumpACL(fout, plang->dobj.dumpId, InvalidDumpId, "LANGUAGE",
13326 : : qlanname, NULL, NULL,
13327 : 40 : NULL, plang->lanowner, &plang->dacl);
13328 : :
13329 : 73 : pg_free(qlanname);
13330 : :
13331 : 73 : destroyPQExpBuffer(defqry);
13332 : 73 : destroyPQExpBuffer(delqry);
13333 : : }
13334 : :
13335 : : /*
13336 : : * format_function_arguments: generate function name and argument list
13337 : : *
13338 : : * This is used when we can rely on pg_get_function_arguments to format
13339 : : * the argument list. Note, however, that pg_get_function_arguments
13340 : : * does not special-case zero-argument aggregates.
13341 : : */
13342 : : static char *
13343 : 4188 : format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
13344 : : {
13345 : : PQExpBufferData fn;
13346 : :
13347 : 4188 : initPQExpBuffer(&fn);
13348 : 4188 : appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name));
13349 [ + + + + ]: 4188 : if (is_agg && finfo->nargs == 0)
13350 : 80 : appendPQExpBufferStr(&fn, "(*)");
13351 : : else
13352 : 4108 : appendPQExpBuffer(&fn, "(%s)", funcargs);
13353 : 4188 : return fn.data;
13354 : : }
13355 : :
13356 : : /*
13357 : : * format_function_signature: generate function name and argument list
13358 : : *
13359 : : * Only a minimal list of input argument types is generated; this is
13360 : : * sufficient to reference the function, but not to define it.
13361 : : *
13362 : : * If honor_quotes is false then the function name is never quoted.
13363 : : * This is appropriate for use in TOC tags, but not in SQL commands.
13364 : : */
13365 : : static char *
13366 : 2208 : format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
13367 : : {
13368 : : PQExpBufferData fn;
13369 : : int j;
13370 : :
13371 : 2208 : initPQExpBuffer(&fn);
13372 [ + + ]: 2208 : if (honor_quotes)
13373 : 401 : appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
13374 : : else
13375 : 1807 : appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
13376 [ + + ]: 4050 : for (j = 0; j < finfo->nargs; j++)
13377 : : {
13378 [ + + ]: 1842 : if (j > 0)
13379 : 432 : appendPQExpBufferStr(&fn, ", ");
13380 : :
13381 : 1842 : appendPQExpBufferStr(&fn,
13382 : 1842 : getFormattedTypeName(fout, finfo->argtypes[j],
13383 : : zeroIsError));
13384 : : }
13385 : 2208 : appendPQExpBufferChar(&fn, ')');
13386 : 2208 : return fn.data;
13387 : : }
13388 : :
13389 : :
13390 : : /*
13391 : : * dumpFunc:
13392 : : * dump out one function
13393 : : */
13394 : : static void
13395 : 1877 : dumpFunc(Archive *fout, const FuncInfo *finfo)
13396 : : {
13397 : 1877 : DumpOptions *dopt = fout->dopt;
13398 : : PQExpBuffer query;
13399 : : PQExpBuffer q;
13400 : : PQExpBuffer delqry;
13401 : : PQExpBuffer asPart;
13402 : : PGresult *res;
13403 : : char *funcsig; /* identity signature */
13404 : 1877 : char *funcfullsig = NULL; /* full signature */
13405 : : char *funcsig_tag;
13406 : : char *qual_funcsig;
13407 : : char *proretset;
13408 : : char *prosrc;
13409 : : char *probin;
13410 : : char *prosqlbody;
13411 : : char *funcargs;
13412 : : char *funciargs;
13413 : : char *funcresult;
13414 : : char *protrftypes;
13415 : : char *prokind;
13416 : : char *provolatile;
13417 : : char *proisstrict;
13418 : : char *prosecdef;
13419 : : char *proleakproof;
13420 : : char *proconfig;
13421 : : char *procost;
13422 : : char *prorows;
13423 : : char *prosupport;
13424 : : char *proparallel;
13425 : : char *lanname;
13426 : 1877 : char **configitems = NULL;
13427 : 1877 : int nconfigitems = 0;
13428 : : const char *keyword;
13429 : :
13430 : : /* Do nothing if not dumping schema */
13431 [ + + ]: 1877 : if (!dopt->dumpSchema)
13432 : 70 : return;
13433 : :
13434 : 1807 : query = createPQExpBuffer();
13435 : 1807 : q = createPQExpBuffer();
13436 : 1807 : delqry = createPQExpBuffer();
13437 : 1807 : asPart = createPQExpBuffer();
13438 : :
13439 [ + + ]: 1807 : if (!fout->is_prepared[PREPQUERY_DUMPFUNC])
13440 : : {
13441 : : /* Set up query for function-specific details */
13442 : 69 : appendPQExpBufferStr(query,
13443 : : "PREPARE dumpFunc(pg_catalog.oid) AS\n");
13444 : :
13445 : 69 : appendPQExpBufferStr(query,
13446 : : "SELECT\n"
13447 : : "proretset,\n"
13448 : : "prosrc,\n"
13449 : : "probin,\n"
13450 : : "provolatile,\n"
13451 : : "proisstrict,\n"
13452 : : "prosecdef,\n"
13453 : : "lanname,\n"
13454 : : "proconfig,\n"
13455 : : "procost,\n"
13456 : : "prorows,\n"
13457 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13458 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n"
13459 : : "pg_catalog.pg_get_function_result(p.oid) AS funcresult,\n"
13460 : : "proleakproof,\n");
13461 : :
13462 : 69 : appendPQExpBufferStr(query,
13463 : : "array_to_string(protrftypes, ' ') AS protrftypes,\n");
13464 : :
13465 : 69 : appendPQExpBufferStr(query,
13466 : : "proparallel,\n");
13467 : :
13468 [ + - ]: 69 : if (fout->remoteVersion >= 110000)
13469 : 69 : appendPQExpBufferStr(query,
13470 : : "prokind,\n");
13471 : : else
13472 : 0 : appendPQExpBufferStr(query,
13473 : : "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind,\n");
13474 : :
13475 [ + - ]: 69 : if (fout->remoteVersion >= 120000)
13476 : 69 : appendPQExpBufferStr(query,
13477 : : "prosupport,\n");
13478 : : else
13479 : 0 : appendPQExpBufferStr(query,
13480 : : "'-' AS prosupport,\n");
13481 : :
13482 [ + - ]: 69 : if (fout->remoteVersion >= 140000)
13483 : 69 : appendPQExpBufferStr(query,
13484 : : "pg_get_function_sqlbody(p.oid) AS prosqlbody\n");
13485 : : else
13486 : 0 : appendPQExpBufferStr(query,
13487 : : "NULL AS prosqlbody\n");
13488 : :
13489 : 69 : appendPQExpBufferStr(query,
13490 : : "FROM pg_catalog.pg_proc p, pg_catalog.pg_language l\n"
13491 : : "WHERE p.oid = $1 "
13492 : : "AND l.oid = p.prolang");
13493 : :
13494 : 69 : ExecuteSqlStatement(fout, query->data);
13495 : :
13496 : 69 : fout->is_prepared[PREPQUERY_DUMPFUNC] = true;
13497 : : }
13498 : :
13499 : 1807 : printfPQExpBuffer(query,
13500 : : "EXECUTE dumpFunc('%u')",
13501 : 1807 : finfo->dobj.catId.oid);
13502 : :
13503 : 1807 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
13504 : :
13505 : 1807 : proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
13506 [ + + ]: 1807 : if (PQgetisnull(res, 0, PQfnumber(res, "prosqlbody")))
13507 : : {
13508 : 1757 : prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
13509 : 1757 : probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
13510 : 1757 : prosqlbody = NULL;
13511 : : }
13512 : : else
13513 : : {
13514 : 50 : prosrc = NULL;
13515 : 50 : probin = NULL;
13516 : 50 : prosqlbody = PQgetvalue(res, 0, PQfnumber(res, "prosqlbody"));
13517 : : }
13518 : 1807 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13519 : 1807 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13520 : 1807 : funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult"));
13521 : 1807 : protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes"));
13522 : 1807 : prokind = PQgetvalue(res, 0, PQfnumber(res, "prokind"));
13523 : 1807 : provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
13524 : 1807 : proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
13525 : 1807 : prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
13526 : 1807 : proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof"));
13527 : 1807 : proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig"));
13528 : 1807 : procost = PQgetvalue(res, 0, PQfnumber(res, "procost"));
13529 : 1807 : prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows"));
13530 : 1807 : prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport"));
13531 : 1807 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13532 : 1807 : lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
13533 : :
13534 : : /*
13535 : : * See backend/commands/functioncmds.c for details of how the 'AS' clause
13536 : : * is used.
13537 : : */
13538 [ + + ]: 1807 : if (prosqlbody)
13539 : : {
13540 : 50 : appendPQExpBufferStr(asPart, prosqlbody);
13541 : : }
13542 [ + + ]: 1757 : else if (probin[0] != '\0')
13543 : : {
13544 : 165 : appendPQExpBufferStr(asPart, "AS ");
13545 : 165 : appendStringLiteralAH(asPart, probin, fout);
13546 [ + - ]: 165 : if (prosrc[0] != '\0')
13547 : : {
13548 : 165 : appendPQExpBufferStr(asPart, ", ");
13549 : :
13550 : : /*
13551 : : * where we have bin, use dollar quoting if allowed and src
13552 : : * contains quote or backslash; else use regular quoting.
13553 : : */
13554 [ + - ]: 165 : if (dopt->disable_dollar_quoting ||
13555 [ + - + - ]: 165 : (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL))
13556 : 165 : appendStringLiteralAH(asPart, prosrc, fout);
13557 : : else
13558 : 0 : appendStringLiteralDQ(asPart, prosrc, NULL);
13559 : : }
13560 : : }
13561 : : else
13562 : : {
13563 : 1592 : appendPQExpBufferStr(asPart, "AS ");
13564 : : /* with no bin, dollar quote src unconditionally if allowed */
13565 [ - + ]: 1592 : if (dopt->disable_dollar_quoting)
13566 : 0 : appendStringLiteralAH(asPart, prosrc, fout);
13567 : : else
13568 : 1592 : appendStringLiteralDQ(asPart, prosrc, NULL);
13569 : : }
13570 : :
13571 [ + + ]: 1807 : if (*proconfig)
13572 : : {
13573 [ - + ]: 15 : if (!parsePGArray(proconfig, &configitems, &nconfigitems))
13574 : 0 : pg_fatal("could not parse %s array", "proconfig");
13575 : : }
13576 : : else
13577 : : {
13578 : 1792 : configitems = NULL;
13579 : 1792 : nconfigitems = 0;
13580 : : }
13581 : :
13582 : 1807 : funcfullsig = format_function_arguments(finfo, funcargs, false);
13583 : 1807 : funcsig = format_function_arguments(finfo, funciargs, false);
13584 : :
13585 : 1807 : funcsig_tag = format_function_signature(fout, finfo, false);
13586 : :
13587 : 1807 : qual_funcsig = psprintf("%s.%s",
13588 : 1807 : fmtId(finfo->dobj.namespace->dobj.name),
13589 : : funcsig);
13590 : :
13591 [ + + ]: 1807 : if (prokind[0] == PROKIND_PROCEDURE)
13592 : 94 : keyword = "PROCEDURE";
13593 : : else
13594 : 1713 : keyword = "FUNCTION"; /* works for window functions too */
13595 : :
13596 : 1807 : appendPQExpBuffer(delqry, "DROP %s %s;\n",
13597 : : keyword, qual_funcsig);
13598 : :
13599 [ + - ]: 3614 : appendPQExpBuffer(q, "CREATE %s %s.%s",
13600 : : keyword,
13601 : 1807 : fmtId(finfo->dobj.namespace->dobj.name),
13602 : : funcfullsig ? funcfullsig :
13603 : : funcsig);
13604 : :
13605 [ + + ]: 1807 : if (prokind[0] == PROKIND_PROCEDURE)
13606 : : /* no result type to output */ ;
13607 [ + - ]: 1713 : else if (funcresult)
13608 : 1713 : appendPQExpBuffer(q, " RETURNS %s", funcresult);
13609 : : else
13610 : 0 : appendPQExpBuffer(q, " RETURNS %s%s",
13611 [ # # ]: 0 : (proretset[0] == 't') ? "SETOF " : "",
13612 : 0 : getFormattedTypeName(fout, finfo->prorettype,
13613 : : zeroIsError));
13614 : :
13615 : 1807 : appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname));
13616 : :
13617 [ + + ]: 1807 : if (*protrftypes)
13618 : : {
13619 : 5 : Oid *typeids = parseOidArray(protrftypes, -1);
13620 : :
13621 : 5 : appendPQExpBufferStr(q, " TRANSFORM ");
13622 [ + + ]: 10 : for (int i = 0; typeids[i]; i++)
13623 : : {
13624 [ - + ]: 5 : if (i != 0)
13625 : 0 : appendPQExpBufferStr(q, ", ");
13626 : 5 : appendPQExpBuffer(q, "FOR TYPE %s",
13627 : 5 : getFormattedTypeName(fout, typeids[i], zeroAsNone));
13628 : : }
13629 : :
13630 : 5 : pg_free(typeids);
13631 : : }
13632 : :
13633 [ + + ]: 1807 : if (prokind[0] == PROKIND_WINDOW)
13634 : 5 : appendPQExpBufferStr(q, " WINDOW");
13635 : :
13636 [ + + ]: 1807 : if (provolatile[0] != PROVOLATILE_VOLATILE)
13637 : : {
13638 [ + + ]: 355 : if (provolatile[0] == PROVOLATILE_IMMUTABLE)
13639 : 334 : appendPQExpBufferStr(q, " IMMUTABLE");
13640 [ + - ]: 21 : else if (provolatile[0] == PROVOLATILE_STABLE)
13641 : 21 : appendPQExpBufferStr(q, " STABLE");
13642 [ # # ]: 0 : else if (provolatile[0] != PROVOLATILE_VOLATILE)
13643 : 0 : pg_fatal("unrecognized provolatile value for function \"%s\"",
13644 : : finfo->dobj.name);
13645 : : }
13646 : :
13647 [ + + ]: 1807 : if (proisstrict[0] == 't')
13648 : 369 : appendPQExpBufferStr(q, " STRICT");
13649 : :
13650 [ - + ]: 1807 : if (prosecdef[0] == 't')
13651 : 0 : appendPQExpBufferStr(q, " SECURITY DEFINER");
13652 : :
13653 [ + + ]: 1807 : if (proleakproof[0] == 't')
13654 : 10 : appendPQExpBufferStr(q, " LEAKPROOF");
13655 : :
13656 : : /*
13657 : : * COST and ROWS are emitted only if present and not default, so as not to
13658 : : * break backwards-compatibility of the dump without need. Keep this code
13659 : : * in sync with the defaults in functioncmds.c.
13660 : : */
13661 [ + - ]: 1807 : if (strcmp(procost, "0") != 0)
13662 : : {
13663 [ + + + + ]: 1807 : if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0)
13664 : : {
13665 : : /* default cost is 1 */
13666 [ - + ]: 402 : if (strcmp(procost, "1") != 0)
13667 : 0 : appendPQExpBuffer(q, " COST %s", procost);
13668 : : }
13669 : : else
13670 : : {
13671 : : /* default cost is 100 */
13672 [ + + ]: 1405 : if (strcmp(procost, "100") != 0)
13673 : 6 : appendPQExpBuffer(q, " COST %s", procost);
13674 : : }
13675 : : }
13676 [ + + ]: 1807 : if (proretset[0] == 't' &&
13677 [ + - - + ]: 189 : strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0)
13678 : 0 : appendPQExpBuffer(q, " ROWS %s", prorows);
13679 : :
13680 [ + + ]: 1807 : if (strcmp(prosupport, "-") != 0)
13681 : : {
13682 : : /* We rely on regprocout to provide quoting and qualification */
13683 : 44 : appendPQExpBuffer(q, " SUPPORT %s", prosupport);
13684 : : }
13685 : :
13686 [ + + ]: 1807 : if (proparallel[0] != PROPARALLEL_UNSAFE)
13687 : : {
13688 [ + + ]: 120 : if (proparallel[0] == PROPARALLEL_SAFE)
13689 : 115 : appendPQExpBufferStr(q, " PARALLEL SAFE");
13690 [ + - ]: 5 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13691 : 5 : appendPQExpBufferStr(q, " PARALLEL RESTRICTED");
13692 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
13693 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
13694 : : finfo->dobj.name);
13695 : : }
13696 : :
13697 [ + + ]: 1847 : for (int i = 0; i < nconfigitems; i++)
13698 : : {
13699 : : /* we feel free to scribble on configitems[] here */
13700 : 40 : char *configitem = configitems[i];
13701 : : char *pos;
13702 : :
13703 : 40 : pos = strchr(configitem, '=');
13704 [ - + ]: 40 : if (pos == NULL)
13705 : 0 : continue;
13706 : 40 : *pos++ = '\0';
13707 : 40 : appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem));
13708 : :
13709 : : /*
13710 : : * Variables that are marked GUC_LIST_QUOTE were already fully quoted
13711 : : * by flatten_set_variable_args() before they were put into the
13712 : : * proconfig array. However, because the quoting rules used there
13713 : : * aren't exactly like SQL's, we have to break the list value apart
13714 : : * and then quote the elements as string literals. (The elements may
13715 : : * be double-quoted as-is, but we can't just feed them to the SQL
13716 : : * parser; it would do the wrong thing with elements that are
13717 : : * zero-length or longer than NAMEDATALEN.) Also, we need a special
13718 : : * case for empty lists.
13719 : : *
13720 : : * Variables that are not so marked should just be emitted as simple
13721 : : * string literals. If the variable is not known to
13722 : : * variable_is_guc_list_quote(), we'll do that; this makes it unsafe
13723 : : * to use GUC_LIST_QUOTE for extension variables.
13724 : : */
13725 [ + + ]: 40 : if (variable_is_guc_list_quote(configitem))
13726 : : {
13727 : : char **namelist;
13728 : : char **nameptr;
13729 : :
13730 : : /* Parse string into list of identifiers */
13731 : : /* this shouldn't fail really */
13732 [ + - ]: 15 : if (SplitGUCList(pos, ',', &namelist))
13733 : : {
13734 : : /* Special case: represent an empty list as NULL */
13735 [ + + ]: 15 : if (*namelist == NULL)
13736 : 5 : appendPQExpBufferStr(q, "NULL");
13737 [ + + ]: 40 : for (nameptr = namelist; *nameptr; nameptr++)
13738 : : {
13739 [ + + ]: 25 : if (nameptr != namelist)
13740 : 15 : appendPQExpBufferStr(q, ", ");
13741 : 25 : appendStringLiteralAH(q, *nameptr, fout);
13742 : : }
13743 : : }
13744 : 15 : pg_free(namelist);
13745 : : }
13746 : : else
13747 : 25 : appendStringLiteralAH(q, pos, fout);
13748 : : }
13749 : :
13750 : 1807 : appendPQExpBuffer(q, "\n %s;\n", asPart->data);
13751 : :
13752 : 1807 : append_depends_on_extension(fout, q, &finfo->dobj,
13753 : : "pg_catalog.pg_proc", keyword,
13754 : : qual_funcsig);
13755 : :
13756 [ + + ]: 1807 : if (dopt->binary_upgrade)
13757 : 300 : binary_upgrade_extension_member(q, &finfo->dobj,
13758 : : keyword, funcsig,
13759 : 300 : finfo->dobj.namespace->dobj.name);
13760 : :
13761 [ + + ]: 1807 : if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13762 : 1707 : ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
13763 [ + + ]: 1707 : ARCHIVE_OPTS(.tag = funcsig_tag,
13764 : : .namespace = finfo->dobj.namespace->dobj.name,
13765 : : .owner = finfo->rolname,
13766 : : .description = keyword,
13767 : : .section = finfo->postponed_def ?
13768 : : SECTION_POST_DATA : SECTION_PRE_DATA,
13769 : : .createStmt = q->data,
13770 : : .dropStmt = delqry->data));
13771 : :
13772 : : /* Dump Function Comments and Security Labels */
13773 [ + + ]: 1807 : if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13774 : 9 : dumpComment(fout, keyword, funcsig,
13775 : 9 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13776 : 9 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13777 : :
13778 [ - + ]: 1807 : if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13779 : 0 : dumpSecLabel(fout, keyword, funcsig,
13780 : 0 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13781 : 0 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13782 : :
13783 [ + + ]: 1807 : if (finfo->dobj.dump & DUMP_COMPONENT_ACL)
13784 : 104 : dumpACL(fout, finfo->dobj.dumpId, InvalidDumpId, keyword,
13785 : : funcsig, NULL,
13786 : 104 : finfo->dobj.namespace->dobj.name,
13787 : 104 : NULL, finfo->rolname, &finfo->dacl);
13788 : :
13789 : 1807 : PQclear(res);
13790 : :
13791 : 1807 : destroyPQExpBuffer(query);
13792 : 1807 : destroyPQExpBuffer(q);
13793 : 1807 : destroyPQExpBuffer(delqry);
13794 : 1807 : destroyPQExpBuffer(asPart);
13795 : 1807 : free(funcsig);
13796 : 1807 : free(funcfullsig);
13797 : 1807 : free(funcsig_tag);
13798 : 1807 : pfree(qual_funcsig);
13799 : 1807 : free(configitems);
13800 : : }
13801 : :
13802 : :
13803 : : /*
13804 : : * Dump a user-defined cast
13805 : : */
13806 : : static void
13807 : 69 : dumpCast(Archive *fout, const CastInfo *cast)
13808 : : {
13809 : 69 : DumpOptions *dopt = fout->dopt;
13810 : : PQExpBuffer defqry;
13811 : : PQExpBuffer delqry;
13812 : : PQExpBuffer labelq;
13813 : : PQExpBuffer castargs;
13814 : 69 : FuncInfo *funcInfo = NULL;
13815 : : const char *sourceType;
13816 : : const char *targetType;
13817 : :
13818 : : /* Do nothing if not dumping schema */
13819 [ + + ]: 69 : if (!dopt->dumpSchema)
13820 : 6 : return;
13821 : :
13822 : : /* Cannot dump if we don't have the cast function's info */
13823 [ + + ]: 63 : if (OidIsValid(cast->castfunc))
13824 : : {
13825 : 38 : funcInfo = findFuncByOid(cast->castfunc);
13826 [ - + ]: 38 : if (funcInfo == NULL)
13827 : 0 : pg_fatal("could not find function definition for function with OID %u",
13828 : : cast->castfunc);
13829 : : }
13830 : :
13831 : 63 : defqry = createPQExpBuffer();
13832 : 63 : delqry = createPQExpBuffer();
13833 : 63 : labelq = createPQExpBuffer();
13834 : 63 : castargs = createPQExpBuffer();
13835 : :
13836 : 63 : sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
13837 : 63 : targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
13838 : 63 : appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
13839 : : sourceType, targetType);
13840 : :
13841 : 63 : appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
13842 : : sourceType, targetType);
13843 : :
13844 [ + - + - ]: 63 : switch (cast->castmethod)
13845 : : {
13846 : 25 : case COERCION_METHOD_BINARY:
13847 : 25 : appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
13848 : 25 : break;
13849 : 0 : case COERCION_METHOD_INOUT:
13850 : 0 : appendPQExpBufferStr(defqry, "WITH INOUT");
13851 : 0 : break;
13852 : 38 : case COERCION_METHOD_FUNCTION:
13853 [ + - ]: 38 : if (funcInfo)
13854 : : {
13855 : 38 : char *fsig = format_function_signature(fout, funcInfo, true);
13856 : :
13857 : : /*
13858 : : * Always qualify the function name (format_function_signature
13859 : : * won't qualify it).
13860 : : */
13861 : 38 : appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
13862 : 38 : fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
13863 : 38 : free(fsig);
13864 : : }
13865 : : else
13866 : 0 : pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
13867 : 38 : break;
13868 : 0 : default:
13869 : 0 : pg_log_warning("bogus value in pg_cast.castmethod field");
13870 : : }
13871 : :
13872 [ + + ]: 63 : if (cast->castcontext == 'a')
13873 : 33 : appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
13874 [ + + ]: 30 : else if (cast->castcontext == 'i')
13875 : 10 : appendPQExpBufferStr(defqry, " AS IMPLICIT");
13876 : 63 : appendPQExpBufferStr(defqry, ";\n");
13877 : :
13878 : 63 : appendPQExpBuffer(labelq, "CAST (%s AS %s)",
13879 : : sourceType, targetType);
13880 : :
13881 : 63 : appendPQExpBuffer(castargs, "(%s AS %s)",
13882 : : sourceType, targetType);
13883 : :
13884 [ + + ]: 63 : if (dopt->binary_upgrade)
13885 : 7 : binary_upgrade_extension_member(defqry, &cast->dobj,
13886 : 7 : "CAST", castargs->data, NULL);
13887 : :
13888 [ + - ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
13889 : 63 : ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
13890 : 63 : ARCHIVE_OPTS(.tag = labelq->data,
13891 : : .description = "CAST",
13892 : : .section = SECTION_PRE_DATA,
13893 : : .createStmt = defqry->data,
13894 : : .dropStmt = delqry->data));
13895 : :
13896 : : /* Dump Cast Comments */
13897 [ - + ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
13898 : 0 : dumpComment(fout, "CAST", castargs->data,
13899 : : NULL, "",
13900 : 0 : cast->dobj.catId, 0, cast->dobj.dumpId);
13901 : :
13902 : 63 : destroyPQExpBuffer(defqry);
13903 : 63 : destroyPQExpBuffer(delqry);
13904 : 63 : destroyPQExpBuffer(labelq);
13905 : 63 : destroyPQExpBuffer(castargs);
13906 : : }
13907 : :
13908 : : /*
13909 : : * Dump a transform
13910 : : */
13911 : : static void
13912 : 44 : dumpTransform(Archive *fout, const TransformInfo *transform)
13913 : : {
13914 : 44 : DumpOptions *dopt = fout->dopt;
13915 : : PQExpBuffer defqry;
13916 : : PQExpBuffer delqry;
13917 : : PQExpBuffer labelq;
13918 : : PQExpBuffer transformargs;
13919 : 44 : FuncInfo *fromsqlFuncInfo = NULL;
13920 : 44 : FuncInfo *tosqlFuncInfo = NULL;
13921 : : char *lanname;
13922 : : const char *transformType;
13923 : :
13924 : : /* Do nothing if not dumping schema */
13925 [ + + ]: 44 : if (!dopt->dumpSchema)
13926 : 6 : return;
13927 : :
13928 : : /* Cannot dump if we don't have the transform functions' info */
13929 [ + - ]: 38 : if (OidIsValid(transform->trffromsql))
13930 : : {
13931 : 38 : fromsqlFuncInfo = findFuncByOid(transform->trffromsql);
13932 [ - + ]: 38 : if (fromsqlFuncInfo == NULL)
13933 : 0 : pg_fatal("could not find function definition for function with OID %u",
13934 : : transform->trffromsql);
13935 : : }
13936 [ + - ]: 38 : if (OidIsValid(transform->trftosql))
13937 : : {
13938 : 38 : tosqlFuncInfo = findFuncByOid(transform->trftosql);
13939 [ - + ]: 38 : if (tosqlFuncInfo == NULL)
13940 : 0 : pg_fatal("could not find function definition for function with OID %u",
13941 : : transform->trftosql);
13942 : : }
13943 : :
13944 : 38 : defqry = createPQExpBuffer();
13945 : 38 : delqry = createPQExpBuffer();
13946 : 38 : labelq = createPQExpBuffer();
13947 : 38 : transformargs = createPQExpBuffer();
13948 : :
13949 : 38 : lanname = get_language_name(fout, transform->trflang);
13950 : 38 : transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone);
13951 : :
13952 : 38 : appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n",
13953 : : transformType, lanname);
13954 : :
13955 : 38 : appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (",
13956 : : transformType, lanname);
13957 : :
13958 [ - + - - ]: 38 : if (!transform->trffromsql && !transform->trftosql)
13959 : 0 : pg_log_warning("bogus transform definition, at least one of trffromsql and trftosql should be nonzero");
13960 : :
13961 [ + - ]: 38 : if (transform->trffromsql)
13962 : : {
13963 [ + - ]: 38 : if (fromsqlFuncInfo)
13964 : : {
13965 : 38 : char *fsig = format_function_signature(fout, fromsqlFuncInfo, true);
13966 : :
13967 : : /*
13968 : : * Always qualify the function name (format_function_signature
13969 : : * won't qualify it).
13970 : : */
13971 : 38 : appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s",
13972 : 38 : fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig);
13973 : 38 : free(fsig);
13974 : : }
13975 : : else
13976 : 0 : pg_log_warning("bogus value in pg_transform.trffromsql field");
13977 : : }
13978 : :
13979 [ + - ]: 38 : if (transform->trftosql)
13980 : : {
13981 [ + - ]: 38 : if (transform->trffromsql)
13982 : 38 : appendPQExpBufferStr(defqry, ", ");
13983 : :
13984 [ + - ]: 38 : if (tosqlFuncInfo)
13985 : : {
13986 : 38 : char *fsig = format_function_signature(fout, tosqlFuncInfo, true);
13987 : :
13988 : : /*
13989 : : * Always qualify the function name (format_function_signature
13990 : : * won't qualify it).
13991 : : */
13992 : 38 : appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s",
13993 : 38 : fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig);
13994 : 38 : free(fsig);
13995 : : }
13996 : : else
13997 : 0 : pg_log_warning("bogus value in pg_transform.trftosql field");
13998 : : }
13999 : :
14000 : 38 : appendPQExpBufferStr(defqry, ");\n");
14001 : :
14002 : 38 : appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s",
14003 : : transformType, lanname);
14004 : :
14005 : 38 : appendPQExpBuffer(transformargs, "FOR %s LANGUAGE %s",
14006 : : transformType, lanname);
14007 : :
14008 [ + + ]: 38 : if (dopt->binary_upgrade)
14009 : 2 : binary_upgrade_extension_member(defqry, &transform->dobj,
14010 : 2 : "TRANSFORM", transformargs->data, NULL);
14011 : :
14012 [ + - ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION)
14013 : 38 : ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId,
14014 : 38 : ARCHIVE_OPTS(.tag = labelq->data,
14015 : : .description = "TRANSFORM",
14016 : : .section = SECTION_PRE_DATA,
14017 : : .createStmt = defqry->data,
14018 : : .dropStmt = delqry->data,
14019 : : .deps = transform->dobj.dependencies,
14020 : : .nDeps = transform->dobj.nDeps));
14021 : :
14022 : : /* Dump Transform Comments */
14023 [ - + ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_COMMENT)
14024 : 0 : dumpComment(fout, "TRANSFORM", transformargs->data,
14025 : : NULL, "",
14026 : 0 : transform->dobj.catId, 0, transform->dobj.dumpId);
14027 : :
14028 : 38 : free(lanname);
14029 : 38 : destroyPQExpBuffer(defqry);
14030 : 38 : destroyPQExpBuffer(delqry);
14031 : 38 : destroyPQExpBuffer(labelq);
14032 : 38 : destroyPQExpBuffer(transformargs);
14033 : : }
14034 : :
14035 : :
14036 : : /*
14037 : : * dumpOpr
14038 : : * write out a single operator definition
14039 : : */
14040 : : static void
14041 : 2525 : dumpOpr(Archive *fout, const OprInfo *oprinfo)
14042 : : {
14043 : 2525 : DumpOptions *dopt = fout->dopt;
14044 : : PQExpBuffer query;
14045 : : PQExpBuffer q;
14046 : : PQExpBuffer delq;
14047 : : PQExpBuffer oprid;
14048 : : PQExpBuffer details;
14049 : : PGresult *res;
14050 : : int i_oprkind;
14051 : : int i_oprcode;
14052 : : int i_oprleft;
14053 : : int i_oprright;
14054 : : int i_oprcom;
14055 : : int i_oprnegate;
14056 : : int i_oprrest;
14057 : : int i_oprjoin;
14058 : : int i_oprcanmerge;
14059 : : int i_oprcanhash;
14060 : : char *oprkind;
14061 : : char *oprcode;
14062 : : char *oprleft;
14063 : : char *oprright;
14064 : : char *oprcom;
14065 : : char *oprnegate;
14066 : : char *oprrest;
14067 : : char *oprjoin;
14068 : : char *oprcanmerge;
14069 : : char *oprcanhash;
14070 : : char *oprregproc;
14071 : : char *oprref;
14072 : :
14073 : : /* Do nothing if not dumping schema */
14074 [ + + ]: 2525 : if (!dopt->dumpSchema)
14075 : 7 : return;
14076 : :
14077 : : /*
14078 : : * some operators are invalid because they were the result of user
14079 : : * defining operators before commutators exist
14080 : : */
14081 [ + + ]: 2518 : if (!OidIsValid(oprinfo->oprcode))
14082 : 14 : return;
14083 : :
14084 : 2504 : query = createPQExpBuffer();
14085 : 2504 : q = createPQExpBuffer();
14086 : 2504 : delq = createPQExpBuffer();
14087 : 2504 : oprid = createPQExpBuffer();
14088 : 2504 : details = createPQExpBuffer();
14089 : :
14090 [ + + ]: 2504 : if (!fout->is_prepared[PREPQUERY_DUMPOPR])
14091 : : {
14092 : : /* Set up query for operator-specific details */
14093 : 42 : appendPQExpBufferStr(query,
14094 : : "PREPARE dumpOpr(pg_catalog.oid) AS\n"
14095 : : "SELECT oprkind, "
14096 : : "oprcode::pg_catalog.regprocedure, "
14097 : : "oprleft::pg_catalog.regtype, "
14098 : : "oprright::pg_catalog.regtype, "
14099 : : "oprcom, "
14100 : : "oprnegate, "
14101 : : "oprrest::pg_catalog.regprocedure, "
14102 : : "oprjoin::pg_catalog.regprocedure, "
14103 : : "oprcanmerge, oprcanhash "
14104 : : "FROM pg_catalog.pg_operator "
14105 : : "WHERE oid = $1");
14106 : :
14107 : 42 : ExecuteSqlStatement(fout, query->data);
14108 : :
14109 : 42 : fout->is_prepared[PREPQUERY_DUMPOPR] = true;
14110 : : }
14111 : :
14112 : 2504 : printfPQExpBuffer(query,
14113 : : "EXECUTE dumpOpr('%u')",
14114 : 2504 : oprinfo->dobj.catId.oid);
14115 : :
14116 : 2504 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14117 : :
14118 : 2504 : i_oprkind = PQfnumber(res, "oprkind");
14119 : 2504 : i_oprcode = PQfnumber(res, "oprcode");
14120 : 2504 : i_oprleft = PQfnumber(res, "oprleft");
14121 : 2504 : i_oprright = PQfnumber(res, "oprright");
14122 : 2504 : i_oprcom = PQfnumber(res, "oprcom");
14123 : 2504 : i_oprnegate = PQfnumber(res, "oprnegate");
14124 : 2504 : i_oprrest = PQfnumber(res, "oprrest");
14125 : 2504 : i_oprjoin = PQfnumber(res, "oprjoin");
14126 : 2504 : i_oprcanmerge = PQfnumber(res, "oprcanmerge");
14127 : 2504 : i_oprcanhash = PQfnumber(res, "oprcanhash");
14128 : :
14129 : 2504 : oprkind = PQgetvalue(res, 0, i_oprkind);
14130 : 2504 : oprcode = PQgetvalue(res, 0, i_oprcode);
14131 : 2504 : oprleft = PQgetvalue(res, 0, i_oprleft);
14132 : 2504 : oprright = PQgetvalue(res, 0, i_oprright);
14133 : 2504 : oprcom = PQgetvalue(res, 0, i_oprcom);
14134 : 2504 : oprnegate = PQgetvalue(res, 0, i_oprnegate);
14135 : 2504 : oprrest = PQgetvalue(res, 0, i_oprrest);
14136 : 2504 : oprjoin = PQgetvalue(res, 0, i_oprjoin);
14137 : 2504 : oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
14138 : 2504 : oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
14139 : :
14140 : : /* In PG14 upwards postfix operator support does not exist anymore. */
14141 [ - + ]: 2504 : if (strcmp(oprkind, "r") == 0)
14142 : 0 : pg_log_warning("postfix operators are not supported anymore (operator \"%s\")",
14143 : : oprcode);
14144 : :
14145 : 2504 : oprregproc = convertRegProcReference(oprcode);
14146 [ + - ]: 2504 : if (oprregproc)
14147 : : {
14148 : 2504 : appendPQExpBuffer(details, " FUNCTION = %s", oprregproc);
14149 : 2504 : free(oprregproc);
14150 : : }
14151 : :
14152 : 2504 : appendPQExpBuffer(oprid, "%s (",
14153 : 2504 : oprinfo->dobj.name);
14154 : :
14155 : : /*
14156 : : * right unary means there's a left arg and left unary means there's a
14157 : : * right arg. (Although the "r" case is dead code for PG14 and later,
14158 : : * continue to support it in case we're dumping from an old server.)
14159 : : */
14160 [ + - ]: 2504 : if (strcmp(oprkind, "r") == 0 ||
14161 [ + + ]: 2504 : strcmp(oprkind, "b") == 0)
14162 : : {
14163 : 2361 : appendPQExpBuffer(details, ",\n LEFTARG = %s", oprleft);
14164 : 2361 : appendPQExpBufferStr(oprid, oprleft);
14165 : : }
14166 : : else
14167 : 143 : appendPQExpBufferStr(oprid, "NONE");
14168 : :
14169 [ + + ]: 2504 : if (strcmp(oprkind, "l") == 0 ||
14170 [ + - ]: 2361 : strcmp(oprkind, "b") == 0)
14171 : : {
14172 : 2504 : appendPQExpBuffer(details, ",\n RIGHTARG = %s", oprright);
14173 : 2504 : appendPQExpBuffer(oprid, ", %s)", oprright);
14174 : : }
14175 : : else
14176 : 0 : appendPQExpBufferStr(oprid, ", NONE)");
14177 : :
14178 : 2504 : oprref = getFormattedOperatorName(oprcom);
14179 [ + + ]: 2504 : if (oprref)
14180 : : {
14181 : 1679 : appendPQExpBuffer(details, ",\n COMMUTATOR = %s", oprref);
14182 : 1679 : free(oprref);
14183 : : }
14184 : :
14185 : 2504 : oprref = getFormattedOperatorName(oprnegate);
14186 [ + + ]: 2504 : if (oprref)
14187 : : {
14188 : 1181 : appendPQExpBuffer(details, ",\n NEGATOR = %s", oprref);
14189 : 1181 : free(oprref);
14190 : : }
14191 : :
14192 [ + + ]: 2504 : if (strcmp(oprcanmerge, "t") == 0)
14193 : 188 : appendPQExpBufferStr(details, ",\n MERGES");
14194 : :
14195 [ + + ]: 2504 : if (strcmp(oprcanhash, "t") == 0)
14196 : 141 : appendPQExpBufferStr(details, ",\n HASHES");
14197 : :
14198 : 2504 : oprregproc = convertRegProcReference(oprrest);
14199 [ + + ]: 2504 : if (oprregproc)
14200 : : {
14201 : 1532 : appendPQExpBuffer(details, ",\n RESTRICT = %s", oprregproc);
14202 : 1532 : free(oprregproc);
14203 : : }
14204 : :
14205 : 2504 : oprregproc = convertRegProcReference(oprjoin);
14206 [ + + ]: 2504 : if (oprregproc)
14207 : : {
14208 : 1532 : appendPQExpBuffer(details, ",\n JOIN = %s", oprregproc);
14209 : 1532 : free(oprregproc);
14210 : : }
14211 : :
14212 : 2504 : appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
14213 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14214 : : oprid->data);
14215 : :
14216 : 2504 : appendPQExpBuffer(q, "CREATE OPERATOR %s.%s (\n%s\n);\n",
14217 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14218 : 2504 : oprinfo->dobj.name, details->data);
14219 : :
14220 [ + + ]: 2504 : if (dopt->binary_upgrade)
14221 : 12 : binary_upgrade_extension_member(q, &oprinfo->dobj,
14222 : 12 : "OPERATOR", oprid->data,
14223 : 12 : oprinfo->dobj.namespace->dobj.name);
14224 : :
14225 [ + - ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14226 : 2504 : ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
14227 : 2504 : ARCHIVE_OPTS(.tag = oprinfo->dobj.name,
14228 : : .namespace = oprinfo->dobj.namespace->dobj.name,
14229 : : .owner = oprinfo->rolname,
14230 : : .description = "OPERATOR",
14231 : : .section = SECTION_PRE_DATA,
14232 : : .createStmt = q->data,
14233 : : .dropStmt = delq->data));
14234 : :
14235 : : /* Dump Operator Comments */
14236 [ + + ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14237 : 2415 : dumpComment(fout, "OPERATOR", oprid->data,
14238 : 2415 : oprinfo->dobj.namespace->dobj.name, oprinfo->rolname,
14239 : 2415 : oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
14240 : :
14241 : 2504 : PQclear(res);
14242 : :
14243 : 2504 : destroyPQExpBuffer(query);
14244 : 2504 : destroyPQExpBuffer(q);
14245 : 2504 : destroyPQExpBuffer(delq);
14246 : 2504 : destroyPQExpBuffer(oprid);
14247 : 2504 : destroyPQExpBuffer(details);
14248 : : }
14249 : :
14250 : : /*
14251 : : * Convert a function reference obtained from pg_operator
14252 : : *
14253 : : * Returns allocated string of what to print, or NULL if function references
14254 : : * is InvalidOid. Returned string is expected to be free'd by the caller.
14255 : : *
14256 : : * The input is a REGPROCEDURE display; we have to strip the argument-types
14257 : : * part.
14258 : : */
14259 : : static char *
14260 : 7512 : convertRegProcReference(const char *proc)
14261 : : {
14262 : : char *name;
14263 : : char *paren;
14264 : : bool inquote;
14265 : :
14266 : : /* In all cases "-" means a null reference */
14267 [ + + ]: 7512 : if (strcmp(proc, "-") == 0)
14268 : 1944 : return NULL;
14269 : :
14270 : 5568 : name = pg_strdup(proc);
14271 : : /* find non-double-quoted left paren */
14272 : 5568 : inquote = false;
14273 [ + - ]: 67010 : for (paren = name; *paren; paren++)
14274 : : {
14275 [ + + + - ]: 67010 : if (*paren == '(' && !inquote)
14276 : : {
14277 : 5568 : *paren = '\0';
14278 : 5568 : break;
14279 : : }
14280 [ + + ]: 61442 : if (*paren == '"')
14281 : 50 : inquote = !inquote;
14282 : : }
14283 : 5568 : return name;
14284 : : }
14285 : :
14286 : : /*
14287 : : * getFormattedOperatorName - retrieve the operator name for the
14288 : : * given operator OID (presented in string form).
14289 : : *
14290 : : * Returns an allocated string, or NULL if the given OID is invalid.
14291 : : * Caller is responsible for free'ing result string.
14292 : : *
14293 : : * What we produce has the format "OPERATOR(schema.oprname)". This is only
14294 : : * useful in commands where the operator's argument types can be inferred from
14295 : : * context. We always schema-qualify the name, though. The predecessor to
14296 : : * this code tried to skip the schema qualification if possible, but that led
14297 : : * to wrong results in corner cases, such as if an operator and its negator
14298 : : * are in different schemas.
14299 : : */
14300 : : static char *
14301 : 5295 : getFormattedOperatorName(const char *oproid)
14302 : : {
14303 : : OprInfo *oprInfo;
14304 : :
14305 : : /* In all cases "0" means a null reference */
14306 [ + + ]: 5295 : if (strcmp(oproid, "0") == 0)
14307 : 2435 : return NULL;
14308 : :
14309 : 2860 : oprInfo = findOprByOid(atooid(oproid));
14310 [ - + ]: 2860 : if (oprInfo == NULL)
14311 : : {
14312 : 0 : pg_log_warning("could not find operator with OID %s",
14313 : : oproid);
14314 : 0 : return NULL;
14315 : : }
14316 : :
14317 : 2860 : return psprintf("OPERATOR(%s.%s)",
14318 : 2860 : fmtId(oprInfo->dobj.namespace->dobj.name),
14319 : : oprInfo->dobj.name);
14320 : : }
14321 : :
14322 : : /*
14323 : : * Convert a function OID obtained from pg_ts_parser or pg_ts_template
14324 : : *
14325 : : * It is sufficient to use REGPROC rather than REGPROCEDURE, since the
14326 : : * argument lists of these functions are predetermined. Note that the
14327 : : * caller should ensure we are in the proper schema, because the results
14328 : : * are search path dependent!
14329 : : */
14330 : : static char *
14331 : 215 : convertTSFunction(Archive *fout, Oid funcOid)
14332 : : {
14333 : : char *result;
14334 : : char query[128];
14335 : : PGresult *res;
14336 : :
14337 : 215 : snprintf(query, sizeof(query),
14338 : : "SELECT '%u'::pg_catalog.regproc", funcOid);
14339 : 215 : res = ExecuteSqlQueryForSingleRow(fout, query);
14340 : :
14341 : 215 : result = pg_strdup(PQgetvalue(res, 0, 0));
14342 : :
14343 : 215 : PQclear(res);
14344 : :
14345 : 215 : return result;
14346 : : }
14347 : :
14348 : : /*
14349 : : * dumpAccessMethod
14350 : : * write out a single access method definition
14351 : : */
14352 : : static void
14353 : 84 : dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo)
14354 : : {
14355 : 84 : DumpOptions *dopt = fout->dopt;
14356 : : PQExpBuffer q;
14357 : : PQExpBuffer delq;
14358 : : char *qamname;
14359 : :
14360 : : /* Do nothing if not dumping schema */
14361 [ + + ]: 84 : if (!dopt->dumpSchema)
14362 : 12 : return;
14363 : :
14364 : 72 : q = createPQExpBuffer();
14365 : 72 : delq = createPQExpBuffer();
14366 : :
14367 : 72 : qamname = pg_strdup(fmtId(aminfo->dobj.name));
14368 : :
14369 : 72 : appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
14370 : :
14371 [ + + - ]: 72 : switch (aminfo->amtype)
14372 : : {
14373 : 34 : case AMTYPE_INDEX:
14374 : 34 : appendPQExpBufferStr(q, "TYPE INDEX ");
14375 : 34 : break;
14376 : 38 : case AMTYPE_TABLE:
14377 : 38 : appendPQExpBufferStr(q, "TYPE TABLE ");
14378 : 38 : break;
14379 : 0 : default:
14380 : 0 : pg_log_warning("invalid type \"%c\" of access method \"%s\"",
14381 : : aminfo->amtype, qamname);
14382 : 0 : destroyPQExpBuffer(q);
14383 : 0 : destroyPQExpBuffer(delq);
14384 : 0 : pg_free(qamname);
14385 : 0 : return;
14386 : : }
14387 : :
14388 : 72 : appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
14389 : :
14390 : 72 : appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
14391 : : qamname);
14392 : :
14393 [ + + ]: 72 : if (dopt->binary_upgrade)
14394 : 4 : binary_upgrade_extension_member(q, &aminfo->dobj,
14395 : : "ACCESS METHOD", qamname, NULL);
14396 : :
14397 [ + - ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14398 : 72 : ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
14399 : 72 : ARCHIVE_OPTS(.tag = aminfo->dobj.name,
14400 : : .description = "ACCESS METHOD",
14401 : : .section = SECTION_PRE_DATA,
14402 : : .createStmt = q->data,
14403 : : .dropStmt = delq->data));
14404 : :
14405 : : /* Dump Access Method Comments */
14406 [ - + ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14407 : 0 : dumpComment(fout, "ACCESS METHOD", qamname,
14408 : : NULL, "",
14409 : 0 : aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
14410 : :
14411 : 72 : destroyPQExpBuffer(q);
14412 : 72 : destroyPQExpBuffer(delq);
14413 : 72 : pg_free(qamname);
14414 : : }
14415 : :
14416 : : /*
14417 : : * dumpOpclass
14418 : : * write out a single operator class definition
14419 : : */
14420 : : static void
14421 : 675 : dumpOpclass(Archive *fout, const OpclassInfo *opcinfo)
14422 : : {
14423 : 675 : DumpOptions *dopt = fout->dopt;
14424 : : PQExpBuffer query;
14425 : : PQExpBuffer q;
14426 : : PQExpBuffer delq;
14427 : : PQExpBuffer nameusing;
14428 : : PGresult *res;
14429 : : int ntups;
14430 : : int i_opcintype;
14431 : : int i_opckeytype;
14432 : : int i_opcdefault;
14433 : : int i_opcfamily;
14434 : : int i_opcfamilyname;
14435 : : int i_opcfamilynsp;
14436 : : int i_amname;
14437 : : int i_amopstrategy;
14438 : : int i_amopopr;
14439 : : int i_sortfamily;
14440 : : int i_sortfamilynsp;
14441 : : int i_amprocnum;
14442 : : int i_amproc;
14443 : : int i_amproclefttype;
14444 : : int i_amprocrighttype;
14445 : : char *opcintype;
14446 : : char *opckeytype;
14447 : : char *opcdefault;
14448 : : char *opcfamily;
14449 : : char *opcfamilyname;
14450 : : char *opcfamilynsp;
14451 : : char *amname;
14452 : : char *amopstrategy;
14453 : : char *amopopr;
14454 : : char *sortfamily;
14455 : : char *sortfamilynsp;
14456 : : char *amprocnum;
14457 : : char *amproc;
14458 : : char *amproclefttype;
14459 : : char *amprocrighttype;
14460 : : bool needComma;
14461 : : int i;
14462 : :
14463 : : /* Do nothing if not dumping schema */
14464 [ + + ]: 675 : if (!dopt->dumpSchema)
14465 : 21 : return;
14466 : :
14467 : 654 : query = createPQExpBuffer();
14468 : 654 : q = createPQExpBuffer();
14469 : 654 : delq = createPQExpBuffer();
14470 : 654 : nameusing = createPQExpBuffer();
14471 : :
14472 : : /* Get additional fields from the pg_opclass row */
14473 : 654 : appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
14474 : : "opckeytype::pg_catalog.regtype, "
14475 : : "opcdefault, opcfamily, "
14476 : : "opfname AS opcfamilyname, "
14477 : : "nspname AS opcfamilynsp, "
14478 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
14479 : : "FROM pg_catalog.pg_opclass c "
14480 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
14481 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14482 : : "WHERE c.oid = '%u'::pg_catalog.oid",
14483 : 654 : opcinfo->dobj.catId.oid);
14484 : :
14485 : 654 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14486 : :
14487 : 654 : i_opcintype = PQfnumber(res, "opcintype");
14488 : 654 : i_opckeytype = PQfnumber(res, "opckeytype");
14489 : 654 : i_opcdefault = PQfnumber(res, "opcdefault");
14490 : 654 : i_opcfamily = PQfnumber(res, "opcfamily");
14491 : 654 : i_opcfamilyname = PQfnumber(res, "opcfamilyname");
14492 : 654 : i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
14493 : 654 : i_amname = PQfnumber(res, "amname");
14494 : :
14495 : : /* opcintype may still be needed after we PQclear res */
14496 : 654 : opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
14497 : 654 : opckeytype = PQgetvalue(res, 0, i_opckeytype);
14498 : 654 : opcdefault = PQgetvalue(res, 0, i_opcdefault);
14499 : : /* opcfamily will still be needed after we PQclear res */
14500 : 654 : opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily));
14501 : 654 : opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname);
14502 : 654 : opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
14503 : : /* amname will still be needed after we PQclear res */
14504 : 654 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14505 : :
14506 : 654 : appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
14507 : 654 : fmtQualifiedDumpable(opcinfo));
14508 : 654 : appendPQExpBuffer(delq, " USING %s;\n",
14509 : : fmtId(amname));
14510 : :
14511 : : /* Build the fixed portion of the CREATE command */
14512 : 654 : appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ",
14513 : 654 : fmtQualifiedDumpable(opcinfo));
14514 [ + + ]: 654 : if (strcmp(opcdefault, "t") == 0)
14515 : 366 : appendPQExpBufferStr(q, "DEFAULT ");
14516 : 654 : appendPQExpBuffer(q, "FOR TYPE %s USING %s",
14517 : : opcintype,
14518 : : fmtId(amname));
14519 [ + - ]: 654 : if (strlen(opcfamilyname) > 0)
14520 : : {
14521 : 654 : appendPQExpBufferStr(q, " FAMILY ");
14522 : 654 : appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
14523 : 654 : appendPQExpBufferStr(q, fmtId(opcfamilyname));
14524 : : }
14525 : 654 : appendPQExpBufferStr(q, " AS\n ");
14526 : :
14527 : 654 : needComma = false;
14528 : :
14529 [ + + ]: 654 : if (strcmp(opckeytype, "-") != 0)
14530 : : {
14531 : 252 : appendPQExpBuffer(q, "STORAGE %s",
14532 : : opckeytype);
14533 : 252 : needComma = true;
14534 : : }
14535 : :
14536 : 654 : PQclear(res);
14537 : :
14538 : : /*
14539 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
14540 : : *
14541 : : * Print only those opfamily members that are tied to the opclass by
14542 : : * pg_depend entries.
14543 : : */
14544 : 654 : resetPQExpBuffer(query);
14545 : 654 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14546 : : "amopopr::pg_catalog.regoperator, "
14547 : : "opfname AS sortfamily, "
14548 : : "nspname AS sortfamilynsp "
14549 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14550 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14551 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14552 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14553 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14554 : : "AND refobjid = '%u'::pg_catalog.oid "
14555 : : "AND amopfamily = '%s'::pg_catalog.oid "
14556 : : "ORDER BY amopstrategy",
14557 : 654 : opcinfo->dobj.catId.oid,
14558 : : opcfamily);
14559 : :
14560 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14561 : :
14562 : 654 : ntups = PQntuples(res);
14563 : :
14564 : 654 : i_amopstrategy = PQfnumber(res, "amopstrategy");
14565 : 654 : i_amopopr = PQfnumber(res, "amopopr");
14566 : 654 : i_sortfamily = PQfnumber(res, "sortfamily");
14567 : 654 : i_sortfamilynsp = PQfnumber(res, "sortfamilynsp");
14568 : :
14569 [ + + ]: 868 : for (i = 0; i < ntups; i++)
14570 : : {
14571 : 214 : amopstrategy = PQgetvalue(res, i, i_amopstrategy);
14572 : 214 : amopopr = PQgetvalue(res, i, i_amopopr);
14573 : 214 : sortfamily = PQgetvalue(res, i, i_sortfamily);
14574 : 214 : sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp);
14575 : :
14576 [ + + ]: 214 : if (needComma)
14577 : 136 : appendPQExpBufferStr(q, " ,\n ");
14578 : :
14579 : 214 : appendPQExpBuffer(q, "OPERATOR %s %s",
14580 : : amopstrategy, amopopr);
14581 : :
14582 [ - + ]: 214 : if (strlen(sortfamily) > 0)
14583 : : {
14584 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
14585 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
14586 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
14587 : : }
14588 : :
14589 : 214 : needComma = true;
14590 : : }
14591 : :
14592 : 654 : PQclear(res);
14593 : :
14594 : : /*
14595 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
14596 : : *
14597 : : * Print only those opfamily members that are tied to the opclass by
14598 : : * pg_depend entries.
14599 : : *
14600 : : * We print the amproclefttype/amprocrighttype even though in most cases
14601 : : * the backend could deduce the right values, because of the corner case
14602 : : * of a btree sort support function for a cross-type comparison.
14603 : : */
14604 : 654 : resetPQExpBuffer(query);
14605 : :
14606 : 654 : appendPQExpBuffer(query, "SELECT amprocnum, "
14607 : : "amproc::pg_catalog.regprocedure, "
14608 : : "amproclefttype::pg_catalog.regtype, "
14609 : : "amprocrighttype::pg_catalog.regtype "
14610 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14611 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14612 : : "AND refobjid = '%u'::pg_catalog.oid "
14613 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14614 : : "AND objid = ap.oid "
14615 : : "ORDER BY amprocnum",
14616 : 654 : opcinfo->dobj.catId.oid);
14617 : :
14618 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14619 : :
14620 : 654 : ntups = PQntuples(res);
14621 : :
14622 : 654 : i_amprocnum = PQfnumber(res, "amprocnum");
14623 : 654 : i_amproc = PQfnumber(res, "amproc");
14624 : 654 : i_amproclefttype = PQfnumber(res, "amproclefttype");
14625 : 654 : i_amprocrighttype = PQfnumber(res, "amprocrighttype");
14626 : :
14627 [ + + ]: 688 : for (i = 0; i < ntups; i++)
14628 : : {
14629 : 34 : amprocnum = PQgetvalue(res, i, i_amprocnum);
14630 : 34 : amproc = PQgetvalue(res, i, i_amproc);
14631 : 34 : amproclefttype = PQgetvalue(res, i, i_amproclefttype);
14632 : 34 : amprocrighttype = PQgetvalue(res, i, i_amprocrighttype);
14633 : :
14634 [ + - ]: 34 : if (needComma)
14635 : 34 : appendPQExpBufferStr(q, " ,\n ");
14636 : :
14637 : 34 : appendPQExpBuffer(q, "FUNCTION %s", amprocnum);
14638 : :
14639 [ + - + - ]: 34 : if (*amproclefttype && *amprocrighttype)
14640 : 34 : appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype);
14641 : :
14642 : 34 : appendPQExpBuffer(q, " %s", amproc);
14643 : :
14644 : 34 : needComma = true;
14645 : : }
14646 : :
14647 : 654 : PQclear(res);
14648 : :
14649 : : /*
14650 : : * If needComma is still false it means we haven't added anything after
14651 : : * the AS keyword. To avoid printing broken SQL, append a dummy STORAGE
14652 : : * clause with the same datatype. This isn't sanctioned by the
14653 : : * documentation, but actually DefineOpClass will treat it as a no-op.
14654 : : */
14655 [ + + ]: 654 : if (!needComma)
14656 : 324 : appendPQExpBuffer(q, "STORAGE %s", opcintype);
14657 : :
14658 : 654 : appendPQExpBufferStr(q, ";\n");
14659 : :
14660 : 654 : appendPQExpBufferStr(nameusing, fmtId(opcinfo->dobj.name));
14661 : 654 : appendPQExpBuffer(nameusing, " USING %s",
14662 : : fmtId(amname));
14663 : :
14664 [ + + ]: 654 : if (dopt->binary_upgrade)
14665 : 6 : binary_upgrade_extension_member(q, &opcinfo->dobj,
14666 : 6 : "OPERATOR CLASS", nameusing->data,
14667 : 6 : opcinfo->dobj.namespace->dobj.name);
14668 : :
14669 [ + - ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14670 : 654 : ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
14671 : 654 : ARCHIVE_OPTS(.tag = opcinfo->dobj.name,
14672 : : .namespace = opcinfo->dobj.namespace->dobj.name,
14673 : : .owner = opcinfo->rolname,
14674 : : .description = "OPERATOR CLASS",
14675 : : .section = SECTION_PRE_DATA,
14676 : : .createStmt = q->data,
14677 : : .dropStmt = delq->data));
14678 : :
14679 : : /* Dump Operator Class Comments */
14680 [ - + ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14681 : 0 : dumpComment(fout, "OPERATOR CLASS", nameusing->data,
14682 : 0 : opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
14683 : 0 : opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
14684 : :
14685 : 654 : pg_free(opcintype);
14686 : 654 : pg_free(opcfamily);
14687 : 654 : pg_free(amname);
14688 : 654 : destroyPQExpBuffer(query);
14689 : 654 : destroyPQExpBuffer(q);
14690 : 654 : destroyPQExpBuffer(delq);
14691 : 654 : destroyPQExpBuffer(nameusing);
14692 : : }
14693 : :
14694 : : /*
14695 : : * dumpOpfamily
14696 : : * write out a single operator family definition
14697 : : *
14698 : : * Note: this also dumps any "loose" operator members that aren't bound to a
14699 : : * specific opclass within the opfamily.
14700 : : */
14701 : : static void
14702 : 561 : dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo)
14703 : : {
14704 : 561 : DumpOptions *dopt = fout->dopt;
14705 : : PQExpBuffer query;
14706 : : PQExpBuffer q;
14707 : : PQExpBuffer delq;
14708 : : PQExpBuffer nameusing;
14709 : : PGresult *res;
14710 : : PGresult *res_ops;
14711 : : PGresult *res_procs;
14712 : : int ntups;
14713 : : int i_amname;
14714 : : int i_amopstrategy;
14715 : : int i_amopopr;
14716 : : int i_sortfamily;
14717 : : int i_sortfamilynsp;
14718 : : int i_amprocnum;
14719 : : int i_amproc;
14720 : : int i_amproclefttype;
14721 : : int i_amprocrighttype;
14722 : : char *amname;
14723 : : char *amopstrategy;
14724 : : char *amopopr;
14725 : : char *sortfamily;
14726 : : char *sortfamilynsp;
14727 : : char *amprocnum;
14728 : : char *amproc;
14729 : : char *amproclefttype;
14730 : : char *amprocrighttype;
14731 : : bool needComma;
14732 : : int i;
14733 : :
14734 : : /* Do nothing if not dumping schema */
14735 [ + + ]: 561 : if (!dopt->dumpSchema)
14736 : 14 : return;
14737 : :
14738 : 547 : query = createPQExpBuffer();
14739 : 547 : q = createPQExpBuffer();
14740 : 547 : delq = createPQExpBuffer();
14741 : 547 : nameusing = createPQExpBuffer();
14742 : :
14743 : : /*
14744 : : * Fetch only those opfamily members that are tied directly to the
14745 : : * opfamily by pg_depend entries.
14746 : : */
14747 : 547 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14748 : : "amopopr::pg_catalog.regoperator, "
14749 : : "opfname AS sortfamily, "
14750 : : "nspname AS sortfamilynsp "
14751 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14752 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14753 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14754 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14755 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14756 : : "AND refobjid = '%u'::pg_catalog.oid "
14757 : : "AND amopfamily = '%u'::pg_catalog.oid "
14758 : : "ORDER BY amopstrategy",
14759 : 547 : opfinfo->dobj.catId.oid,
14760 : 547 : opfinfo->dobj.catId.oid);
14761 : :
14762 : 547 : res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14763 : :
14764 : 547 : resetPQExpBuffer(query);
14765 : :
14766 : 547 : appendPQExpBuffer(query, "SELECT amprocnum, "
14767 : : "amproc::pg_catalog.regprocedure, "
14768 : : "amproclefttype::pg_catalog.regtype, "
14769 : : "amprocrighttype::pg_catalog.regtype "
14770 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14771 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14772 : : "AND refobjid = '%u'::pg_catalog.oid "
14773 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14774 : : "AND objid = ap.oid "
14775 : : "ORDER BY amprocnum",
14776 : 547 : opfinfo->dobj.catId.oid);
14777 : :
14778 : 547 : res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14779 : :
14780 : : /* Get additional fields from the pg_opfamily row */
14781 : 547 : resetPQExpBuffer(query);
14782 : :
14783 : 547 : appendPQExpBuffer(query, "SELECT "
14784 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
14785 : : "FROM pg_catalog.pg_opfamily "
14786 : : "WHERE oid = '%u'::pg_catalog.oid",
14787 : 547 : opfinfo->dobj.catId.oid);
14788 : :
14789 : 547 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14790 : :
14791 : 547 : i_amname = PQfnumber(res, "amname");
14792 : :
14793 : : /* amname will still be needed after we PQclear res */
14794 : 547 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14795 : :
14796 : 547 : appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
14797 : 547 : fmtQualifiedDumpable(opfinfo));
14798 : 547 : appendPQExpBuffer(delq, " USING %s;\n",
14799 : : fmtId(amname));
14800 : :
14801 : : /* Build the fixed portion of the CREATE command */
14802 : 547 : appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
14803 : 547 : fmtQualifiedDumpable(opfinfo));
14804 : 547 : appendPQExpBuffer(q, " USING %s;\n",
14805 : : fmtId(amname));
14806 : :
14807 : 547 : PQclear(res);
14808 : :
14809 : : /* Do we need an ALTER to add loose members? */
14810 [ + + + + ]: 547 : if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
14811 : : {
14812 : 49 : appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
14813 : 49 : fmtQualifiedDumpable(opfinfo));
14814 : 49 : appendPQExpBuffer(q, " USING %s ADD\n ",
14815 : : fmtId(amname));
14816 : :
14817 : 49 : needComma = false;
14818 : :
14819 : : /*
14820 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
14821 : : */
14822 : 49 : ntups = PQntuples(res_ops);
14823 : :
14824 : 49 : i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
14825 : 49 : i_amopopr = PQfnumber(res_ops, "amopopr");
14826 : 49 : i_sortfamily = PQfnumber(res_ops, "sortfamily");
14827 : 49 : i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp");
14828 : :
14829 [ + + ]: 219 : for (i = 0; i < ntups; i++)
14830 : : {
14831 : 170 : amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
14832 : 170 : amopopr = PQgetvalue(res_ops, i, i_amopopr);
14833 : 170 : sortfamily = PQgetvalue(res_ops, i, i_sortfamily);
14834 : 170 : sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp);
14835 : :
14836 [ + + ]: 170 : if (needComma)
14837 : 136 : appendPQExpBufferStr(q, " ,\n ");
14838 : :
14839 : 170 : appendPQExpBuffer(q, "OPERATOR %s %s",
14840 : : amopstrategy, amopopr);
14841 : :
14842 [ - + ]: 170 : if (strlen(sortfamily) > 0)
14843 : : {
14844 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
14845 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
14846 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
14847 : : }
14848 : :
14849 : 170 : needComma = true;
14850 : : }
14851 : :
14852 : : /*
14853 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
14854 : : */
14855 : 49 : ntups = PQntuples(res_procs);
14856 : :
14857 : 49 : i_amprocnum = PQfnumber(res_procs, "amprocnum");
14858 : 49 : i_amproc = PQfnumber(res_procs, "amproc");
14859 : 49 : i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
14860 : 49 : i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
14861 : :
14862 [ + + ]: 234 : for (i = 0; i < ntups; i++)
14863 : : {
14864 : 185 : amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
14865 : 185 : amproc = PQgetvalue(res_procs, i, i_amproc);
14866 : 185 : amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
14867 : 185 : amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
14868 : :
14869 [ + + ]: 185 : if (needComma)
14870 : 170 : appendPQExpBufferStr(q, " ,\n ");
14871 : :
14872 : 185 : appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
14873 : : amprocnum, amproclefttype, amprocrighttype,
14874 : : amproc);
14875 : :
14876 : 185 : needComma = true;
14877 : : }
14878 : :
14879 : 49 : appendPQExpBufferStr(q, ";\n");
14880 : : }
14881 : :
14882 : 547 : appendPQExpBufferStr(nameusing, fmtId(opfinfo->dobj.name));
14883 : 547 : appendPQExpBuffer(nameusing, " USING %s",
14884 : : fmtId(amname));
14885 : :
14886 [ + + ]: 547 : if (dopt->binary_upgrade)
14887 : 9 : binary_upgrade_extension_member(q, &opfinfo->dobj,
14888 : 9 : "OPERATOR FAMILY", nameusing->data,
14889 : 9 : opfinfo->dobj.namespace->dobj.name);
14890 : :
14891 [ + - ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14892 : 547 : ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
14893 : 547 : ARCHIVE_OPTS(.tag = opfinfo->dobj.name,
14894 : : .namespace = opfinfo->dobj.namespace->dobj.name,
14895 : : .owner = opfinfo->rolname,
14896 : : .description = "OPERATOR FAMILY",
14897 : : .section = SECTION_PRE_DATA,
14898 : : .createStmt = q->data,
14899 : : .dropStmt = delq->data));
14900 : :
14901 : : /* Dump Operator Family Comments */
14902 [ - + ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14903 : 0 : dumpComment(fout, "OPERATOR FAMILY", nameusing->data,
14904 : 0 : opfinfo->dobj.namespace->dobj.name, opfinfo->rolname,
14905 : 0 : opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
14906 : :
14907 : 547 : pg_free(amname);
14908 : 547 : PQclear(res_ops);
14909 : 547 : PQclear(res_procs);
14910 : 547 : destroyPQExpBuffer(query);
14911 : 547 : destroyPQExpBuffer(q);
14912 : 547 : destroyPQExpBuffer(delq);
14913 : 547 : destroyPQExpBuffer(nameusing);
14914 : : }
14915 : :
14916 : : /*
14917 : : * dumpCollation
14918 : : * write out a single collation definition
14919 : : */
14920 : : static void
14921 : 2733 : dumpCollation(Archive *fout, const CollInfo *collinfo)
14922 : : {
14923 : 2733 : DumpOptions *dopt = fout->dopt;
14924 : : PQExpBuffer query;
14925 : : PQExpBuffer q;
14926 : : PQExpBuffer delq;
14927 : : char *qcollname;
14928 : : PGresult *res;
14929 : : int i_collprovider;
14930 : : int i_collisdeterministic;
14931 : : int i_collcollate;
14932 : : int i_collctype;
14933 : : int i_colllocale;
14934 : : int i_collicurules;
14935 : : const char *collprovider;
14936 : : const char *collcollate;
14937 : : const char *collctype;
14938 : : const char *colllocale;
14939 : : const char *collicurules;
14940 : :
14941 : : /* Do nothing if not dumping schema */
14942 [ + + ]: 2733 : if (!dopt->dumpSchema)
14943 : 12 : return;
14944 : :
14945 : 2721 : query = createPQExpBuffer();
14946 : 2721 : q = createPQExpBuffer();
14947 : 2721 : delq = createPQExpBuffer();
14948 : :
14949 : 2721 : qcollname = pg_strdup(fmtId(collinfo->dobj.name));
14950 : :
14951 : : /* Get collation-specific details */
14952 : 2721 : appendPQExpBufferStr(query, "SELECT ");
14953 : :
14954 : 2721 : appendPQExpBufferStr(query,
14955 : : "collprovider, "
14956 : : "collversion, ");
14957 : :
14958 [ + - ]: 2721 : if (fout->remoteVersion >= 120000)
14959 : 2721 : appendPQExpBufferStr(query,
14960 : : "collisdeterministic, ");
14961 : : else
14962 : 0 : appendPQExpBufferStr(query,
14963 : : "true AS collisdeterministic, ");
14964 : :
14965 [ + - ]: 2721 : if (fout->remoteVersion >= 170000)
14966 : 2721 : appendPQExpBufferStr(query,
14967 : : "colllocale, ");
14968 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
14969 : 0 : appendPQExpBufferStr(query,
14970 : : "colliculocale AS colllocale, ");
14971 : : else
14972 : 0 : appendPQExpBufferStr(query,
14973 : : "NULL AS colllocale, ");
14974 : :
14975 [ + - ]: 2721 : if (fout->remoteVersion >= 160000)
14976 : 2721 : appendPQExpBufferStr(query,
14977 : : "collicurules, ");
14978 : : else
14979 : 0 : appendPQExpBufferStr(query,
14980 : : "NULL AS collicurules, ");
14981 : :
14982 : 2721 : appendPQExpBuffer(query,
14983 : : "collcollate, "
14984 : : "collctype "
14985 : : "FROM pg_catalog.pg_collation c "
14986 : : "WHERE c.oid = '%u'::pg_catalog.oid",
14987 : 2721 : collinfo->dobj.catId.oid);
14988 : :
14989 : 2721 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14990 : :
14991 : 2721 : i_collprovider = PQfnumber(res, "collprovider");
14992 : 2721 : i_collisdeterministic = PQfnumber(res, "collisdeterministic");
14993 : 2721 : i_collcollate = PQfnumber(res, "collcollate");
14994 : 2721 : i_collctype = PQfnumber(res, "collctype");
14995 : 2721 : i_colllocale = PQfnumber(res, "colllocale");
14996 : 2721 : i_collicurules = PQfnumber(res, "collicurules");
14997 : :
14998 : 2721 : collprovider = PQgetvalue(res, 0, i_collprovider);
14999 : :
15000 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collcollate))
15001 : 48 : collcollate = PQgetvalue(res, 0, i_collcollate);
15002 : : else
15003 : 2673 : collcollate = NULL;
15004 : :
15005 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collctype))
15006 : 48 : collctype = PQgetvalue(res, 0, i_collctype);
15007 : : else
15008 : 2673 : collctype = NULL;
15009 : :
15010 : : /*
15011 : : * Before version 15, collcollate and collctype were of type NAME and
15012 : : * non-nullable. Treat empty strings as NULL for consistency.
15013 : : */
15014 [ - + ]: 2721 : if (fout->remoteVersion < 150000)
15015 : : {
15016 [ # # ]: 0 : if (collcollate[0] == '\0')
15017 : 0 : collcollate = NULL;
15018 [ # # ]: 0 : if (collctype[0] == '\0')
15019 : 0 : collctype = NULL;
15020 : : }
15021 : :
15022 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_colllocale))
15023 : 2670 : colllocale = PQgetvalue(res, 0, i_colllocale);
15024 : : else
15025 : 51 : colllocale = NULL;
15026 : :
15027 [ - + ]: 2721 : if (!PQgetisnull(res, 0, i_collicurules))
15028 : 0 : collicurules = PQgetvalue(res, 0, i_collicurules);
15029 : : else
15030 : 2721 : collicurules = NULL;
15031 : :
15032 : 2721 : appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
15033 : 2721 : fmtQualifiedDumpable(collinfo));
15034 : :
15035 : 2721 : appendPQExpBuffer(q, "CREATE COLLATION %s (",
15036 : 2721 : fmtQualifiedDumpable(collinfo));
15037 : :
15038 : 2721 : appendPQExpBufferStr(q, "provider = ");
15039 [ + + ]: 2721 : if (collprovider[0] == 'b')
15040 : 19 : appendPQExpBufferStr(q, "builtin");
15041 [ + + ]: 2702 : else if (collprovider[0] == 'c')
15042 : 48 : appendPQExpBufferStr(q, "libc");
15043 [ + + ]: 2654 : else if (collprovider[0] == 'i')
15044 : 2651 : appendPQExpBufferStr(q, "icu");
15045 [ + - ]: 3 : else if (collprovider[0] == 'd')
15046 : : /* to allow dumping pg_catalog; not accepted on input */
15047 : 3 : appendPQExpBufferStr(q, "default");
15048 : : else
15049 : 0 : pg_fatal("unrecognized collation provider: %s",
15050 : : collprovider);
15051 : :
15052 [ - + ]: 2721 : if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
15053 : 0 : appendPQExpBufferStr(q, ", deterministic = false");
15054 : :
15055 [ + + ]: 2721 : if (collprovider[0] == 'd')
15056 : : {
15057 [ + - + - : 3 : if (collcollate || collctype || colllocale || collicurules)
+ - - + ]
15058 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15059 : :
15060 : : /* no locale -- the default collation cannot be reloaded anyway */
15061 : : }
15062 [ + + ]: 2718 : else if (collprovider[0] == 'b')
15063 : : {
15064 [ + - + - : 19 : if (collcollate || collctype || !colllocale || collicurules)
+ - - + ]
15065 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15066 : :
15067 : 19 : appendPQExpBufferStr(q, ", locale = ");
15068 [ + - ]: 19 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15069 : : fout);
15070 : : }
15071 [ + + ]: 2699 : else if (collprovider[0] == 'i')
15072 : : {
15073 [ + - ]: 2651 : if (fout->remoteVersion >= 150000)
15074 : : {
15075 [ + - + - : 2651 : if (collcollate || collctype || !colllocale)
- + ]
15076 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15077 : :
15078 : 2651 : appendPQExpBufferStr(q, ", locale = ");
15079 [ + - ]: 2651 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15080 : : fout);
15081 : : }
15082 : : else
15083 : : {
15084 [ # # # # : 0 : if (!collcollate || !collctype || colllocale ||
# # ]
15085 [ # # ]: 0 : strcmp(collcollate, collctype) != 0)
15086 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15087 : :
15088 : 0 : appendPQExpBufferStr(q, ", locale = ");
15089 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15090 : : }
15091 : :
15092 [ - + ]: 2651 : if (collicurules)
15093 : : {
15094 : 0 : appendPQExpBufferStr(q, ", rules = ");
15095 [ # # ]: 0 : appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
15096 : : }
15097 : : }
15098 [ + - ]: 48 : else if (collprovider[0] == 'c')
15099 : : {
15100 [ + - + - : 48 : if (colllocale || collicurules || !collcollate || !collctype)
+ - - + ]
15101 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15102 : :
15103 [ + - + - : 48 : if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
+ - ]
15104 : : {
15105 : 48 : appendPQExpBufferStr(q, ", locale = ");
15106 [ + - ]: 48 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15107 : : }
15108 : : else
15109 : : {
15110 : 0 : appendPQExpBufferStr(q, ", lc_collate = ");
15111 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15112 : 0 : appendPQExpBufferStr(q, ", lc_ctype = ");
15113 [ # # ]: 0 : appendStringLiteralAH(q, collctype ? collctype : "", fout);
15114 : : }
15115 : : }
15116 : : else
15117 : 0 : pg_fatal("unrecognized collation provider: %s", collprovider);
15118 : :
15119 : : /*
15120 : : * For binary upgrade, carry over the collation version. For normal
15121 : : * dump/restore, omit the version, so that it is computed upon restore.
15122 : : */
15123 [ + + ]: 2721 : if (dopt->binary_upgrade)
15124 : : {
15125 : : int i_collversion;
15126 : :
15127 : 5 : i_collversion = PQfnumber(res, "collversion");
15128 [ + + ]: 5 : if (!PQgetisnull(res, 0, i_collversion))
15129 : : {
15130 : 4 : appendPQExpBufferStr(q, ", version = ");
15131 : 4 : appendStringLiteralAH(q,
15132 : : PQgetvalue(res, 0, i_collversion),
15133 : : fout);
15134 : : }
15135 : : }
15136 : :
15137 : 2721 : appendPQExpBufferStr(q, ");\n");
15138 : :
15139 [ + + ]: 2721 : if (dopt->binary_upgrade)
15140 : 5 : binary_upgrade_extension_member(q, &collinfo->dobj,
15141 : : "COLLATION", qcollname,
15142 : 5 : collinfo->dobj.namespace->dobj.name);
15143 : :
15144 [ + - ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15145 : 2721 : ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
15146 : 2721 : ARCHIVE_OPTS(.tag = collinfo->dobj.name,
15147 : : .namespace = collinfo->dobj.namespace->dobj.name,
15148 : : .owner = collinfo->rolname,
15149 : : .description = "COLLATION",
15150 : : .section = SECTION_PRE_DATA,
15151 : : .createStmt = q->data,
15152 : : .dropStmt = delq->data));
15153 : :
15154 : : /* Dump Collation Comments */
15155 [ + + ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15156 : 2613 : dumpComment(fout, "COLLATION", qcollname,
15157 : 2613 : collinfo->dobj.namespace->dobj.name, collinfo->rolname,
15158 : 2613 : collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
15159 : :
15160 : 2721 : PQclear(res);
15161 : :
15162 : 2721 : destroyPQExpBuffer(query);
15163 : 2721 : destroyPQExpBuffer(q);
15164 : 2721 : destroyPQExpBuffer(delq);
15165 : 2721 : pg_free(qcollname);
15166 : : }
15167 : :
15168 : : /*
15169 : : * dumpConversion
15170 : : * write out a single conversion definition
15171 : : */
15172 : : static void
15173 : 335 : dumpConversion(Archive *fout, const ConvInfo *convinfo)
15174 : : {
15175 : 335 : DumpOptions *dopt = fout->dopt;
15176 : : PQExpBuffer query;
15177 : : PQExpBuffer q;
15178 : : PQExpBuffer delq;
15179 : : char *qconvname;
15180 : : PGresult *res;
15181 : : int i_conforencoding;
15182 : : int i_contoencoding;
15183 : : int i_conproc;
15184 : : int i_condefault;
15185 : : const char *conforencoding;
15186 : : const char *contoencoding;
15187 : : const char *conproc;
15188 : : bool condefault;
15189 : :
15190 : : /* Do nothing if not dumping schema */
15191 [ + + ]: 335 : if (!dopt->dumpSchema)
15192 : 7 : return;
15193 : :
15194 : 328 : query = createPQExpBuffer();
15195 : 328 : q = createPQExpBuffer();
15196 : 328 : delq = createPQExpBuffer();
15197 : :
15198 : 328 : qconvname = pg_strdup(fmtId(convinfo->dobj.name));
15199 : :
15200 : : /* Get conversion-specific details */
15201 : 328 : appendPQExpBuffer(query, "SELECT "
15202 : : "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
15203 : : "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
15204 : : "conproc, condefault "
15205 : : "FROM pg_catalog.pg_conversion c "
15206 : : "WHERE c.oid = '%u'::pg_catalog.oid",
15207 : 328 : convinfo->dobj.catId.oid);
15208 : :
15209 : 328 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15210 : :
15211 : 328 : i_conforencoding = PQfnumber(res, "conforencoding");
15212 : 328 : i_contoencoding = PQfnumber(res, "contoencoding");
15213 : 328 : i_conproc = PQfnumber(res, "conproc");
15214 : 328 : i_condefault = PQfnumber(res, "condefault");
15215 : :
15216 : 328 : conforencoding = PQgetvalue(res, 0, i_conforencoding);
15217 : 328 : contoencoding = PQgetvalue(res, 0, i_contoencoding);
15218 : 328 : conproc = PQgetvalue(res, 0, i_conproc);
15219 : 328 : condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
15220 : :
15221 : 328 : appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
15222 : 328 : fmtQualifiedDumpable(convinfo));
15223 : :
15224 [ + - ]: 328 : appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
15225 : : (condefault) ? "DEFAULT " : "",
15226 : 328 : fmtQualifiedDumpable(convinfo));
15227 : 328 : appendStringLiteralAH(q, conforencoding, fout);
15228 : 328 : appendPQExpBufferStr(q, " TO ");
15229 : 328 : appendStringLiteralAH(q, contoencoding, fout);
15230 : : /* regproc output is already sufficiently quoted */
15231 : 328 : appendPQExpBuffer(q, " FROM %s;\n", conproc);
15232 : :
15233 [ + + ]: 328 : if (dopt->binary_upgrade)
15234 : 1 : binary_upgrade_extension_member(q, &convinfo->dobj,
15235 : : "CONVERSION", qconvname,
15236 : 1 : convinfo->dobj.namespace->dobj.name);
15237 : :
15238 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15239 : 328 : ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
15240 : 328 : ARCHIVE_OPTS(.tag = convinfo->dobj.name,
15241 : : .namespace = convinfo->dobj.namespace->dobj.name,
15242 : : .owner = convinfo->rolname,
15243 : : .description = "CONVERSION",
15244 : : .section = SECTION_PRE_DATA,
15245 : : .createStmt = q->data,
15246 : : .dropStmt = delq->data));
15247 : :
15248 : : /* Dump Conversion Comments */
15249 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15250 : 328 : dumpComment(fout, "CONVERSION", qconvname,
15251 : 328 : convinfo->dobj.namespace->dobj.name, convinfo->rolname,
15252 : 328 : convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
15253 : :
15254 : 328 : PQclear(res);
15255 : :
15256 : 328 : destroyPQExpBuffer(query);
15257 : 328 : destroyPQExpBuffer(q);
15258 : 328 : destroyPQExpBuffer(delq);
15259 : 328 : pg_free(qconvname);
15260 : : }
15261 : :
15262 : : /*
15263 : : * format_aggregate_signature: generate aggregate name and argument list
15264 : : *
15265 : : * The argument type names are qualified if needed. The aggregate name
15266 : : * is never qualified.
15267 : : */
15268 : : static char *
15269 : 287 : format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
15270 : : {
15271 : : PQExpBufferData buf;
15272 : : int j;
15273 : :
15274 : 287 : initPQExpBuffer(&buf);
15275 [ - + ]: 287 : if (honor_quotes)
15276 : 0 : appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name));
15277 : : else
15278 : 287 : appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name);
15279 : :
15280 [ + + ]: 287 : if (agginfo->aggfn.nargs == 0)
15281 : 40 : appendPQExpBufferStr(&buf, "(*)");
15282 : : else
15283 : : {
15284 : 247 : appendPQExpBufferChar(&buf, '(');
15285 [ + + ]: 539 : for (j = 0; j < agginfo->aggfn.nargs; j++)
15286 [ + + ]: 292 : appendPQExpBuffer(&buf, "%s%s",
15287 : : (j > 0) ? ", " : "",
15288 : : getFormattedTypeName(fout,
15289 : 292 : agginfo->aggfn.argtypes[j],
15290 : : zeroIsError));
15291 : 247 : appendPQExpBufferChar(&buf, ')');
15292 : : }
15293 : 287 : return buf.data;
15294 : : }
15295 : :
15296 : : /*
15297 : : * dumpAgg
15298 : : * write out a single aggregate definition
15299 : : */
15300 : : static void
15301 : 295 : dumpAgg(Archive *fout, const AggInfo *agginfo)
15302 : : {
15303 : 295 : DumpOptions *dopt = fout->dopt;
15304 : : PQExpBuffer query;
15305 : : PQExpBuffer q;
15306 : : PQExpBuffer delq;
15307 : : PQExpBuffer details;
15308 : : char *aggsig; /* identity signature */
15309 : 295 : char *aggfullsig = NULL; /* full signature */
15310 : : char *aggsig_tag;
15311 : : PGresult *res;
15312 : : int i_agginitval;
15313 : : int i_aggminitval;
15314 : : const char *aggtransfn;
15315 : : const char *aggfinalfn;
15316 : : const char *aggcombinefn;
15317 : : const char *aggserialfn;
15318 : : const char *aggdeserialfn;
15319 : : const char *aggmtransfn;
15320 : : const char *aggminvtransfn;
15321 : : const char *aggmfinalfn;
15322 : : bool aggfinalextra;
15323 : : bool aggmfinalextra;
15324 : : char aggfinalmodify;
15325 : : char aggmfinalmodify;
15326 : : const char *aggsortop;
15327 : : char *aggsortconvop;
15328 : : char aggkind;
15329 : : const char *aggtranstype;
15330 : : const char *aggtransspace;
15331 : : const char *aggmtranstype;
15332 : : const char *aggmtransspace;
15333 : : const char *agginitval;
15334 : : const char *aggminitval;
15335 : : const char *proparallel;
15336 : : const char *prosupport;
15337 : : char defaultfinalmodify;
15338 : :
15339 : : /* Do nothing if not dumping schema */
15340 [ + + ]: 295 : if (!dopt->dumpSchema)
15341 : 8 : return;
15342 : :
15343 : 287 : query = createPQExpBuffer();
15344 : 287 : q = createPQExpBuffer();
15345 : 287 : delq = createPQExpBuffer();
15346 : 287 : details = createPQExpBuffer();
15347 : :
15348 [ + + ]: 287 : if (!fout->is_prepared[PREPQUERY_DUMPAGG])
15349 : : {
15350 : : /* Set up query for aggregate-specific details */
15351 : 57 : appendPQExpBufferStr(query,
15352 : : "PREPARE dumpAgg(pg_catalog.oid) AS\n");
15353 : :
15354 : 57 : appendPQExpBufferStr(query,
15355 : : "SELECT "
15356 : : "aggtransfn,\n"
15357 : : "aggfinalfn,\n"
15358 : : "aggtranstype::pg_catalog.regtype,\n"
15359 : : "agginitval,\n"
15360 : : "aggsortop,\n"
15361 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
15362 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
15363 : :
15364 : 57 : appendPQExpBufferStr(query,
15365 : : "aggkind,\n"
15366 : : "aggmtransfn,\n"
15367 : : "aggminvtransfn,\n"
15368 : : "aggmfinalfn,\n"
15369 : : "aggmtranstype::pg_catalog.regtype,\n"
15370 : : "aggfinalextra,\n"
15371 : : "aggmfinalextra,\n"
15372 : : "aggtransspace,\n"
15373 : : "aggmtransspace,\n"
15374 : : "aggminitval,\n");
15375 : :
15376 : 57 : appendPQExpBufferStr(query,
15377 : : "aggcombinefn,\n"
15378 : : "aggserialfn,\n"
15379 : : "aggdeserialfn,\n"
15380 : : "proparallel,\n");
15381 : :
15382 [ + - ]: 57 : if (fout->remoteVersion >= 110000)
15383 : 57 : appendPQExpBufferStr(query,
15384 : : "aggfinalmodify,\n"
15385 : : "aggmfinalmodify,\n");
15386 : : else
15387 : 0 : appendPQExpBufferStr(query,
15388 : : "'0' AS aggfinalmodify,\n"
15389 : : "'0' AS aggmfinalmodify,\n");
15390 : :
15391 [ + - ]: 57 : if (fout->remoteVersion >= 120000)
15392 : 57 : appendPQExpBufferStr(query,
15393 : : "prosupport\n");
15394 : : else
15395 : 0 : appendPQExpBufferStr(query,
15396 : : "'-' AS prosupport\n");
15397 : :
15398 : 57 : appendPQExpBufferStr(query,
15399 : : "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
15400 : : "WHERE a.aggfnoid = p.oid "
15401 : : "AND p.oid = $1");
15402 : :
15403 : 57 : ExecuteSqlStatement(fout, query->data);
15404 : :
15405 : 57 : fout->is_prepared[PREPQUERY_DUMPAGG] = true;
15406 : : }
15407 : :
15408 : 287 : printfPQExpBuffer(query,
15409 : : "EXECUTE dumpAgg('%u')",
15410 : 287 : agginfo->aggfn.dobj.catId.oid);
15411 : :
15412 : 287 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15413 : :
15414 : 287 : i_agginitval = PQfnumber(res, "agginitval");
15415 : 287 : i_aggminitval = PQfnumber(res, "aggminitval");
15416 : :
15417 : 287 : aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
15418 : 287 : aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
15419 : 287 : aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
15420 : 287 : aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
15421 : 287 : aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
15422 : 287 : aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
15423 : 287 : aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
15424 : 287 : aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
15425 : 287 : aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
15426 : 287 : aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
15427 : 287 : aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
15428 : 287 : aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
15429 : 287 : aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
15430 : 287 : aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
15431 : 287 : aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
15432 : 287 : aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
15433 : 287 : aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
15434 : 287 : aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
15435 : 287 : agginitval = PQgetvalue(res, 0, i_agginitval);
15436 : 287 : aggminitval = PQgetvalue(res, 0, i_aggminitval);
15437 : 287 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
15438 : 287 : prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport"));
15439 : :
15440 : : {
15441 : : char *funcargs;
15442 : : char *funciargs;
15443 : :
15444 : 287 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
15445 : 287 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
15446 : 287 : aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
15447 : 287 : aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
15448 : : }
15449 : :
15450 : 287 : aggsig_tag = format_aggregate_signature(agginfo, fout, false);
15451 : :
15452 : : /* identify default modify flag for aggkind (must match DefineAggregate) */
15453 [ + + ]: 287 : defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
15454 : : /* replace omitted flags for old versions */
15455 [ - + ]: 287 : if (aggfinalmodify == '0')
15456 : 0 : aggfinalmodify = defaultfinalmodify;
15457 [ - + ]: 287 : if (aggmfinalmodify == '0')
15458 : 0 : aggmfinalmodify = defaultfinalmodify;
15459 : :
15460 : : /* regproc and regtype output is already sufficiently quoted */
15461 : 287 : appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
15462 : : aggtransfn, aggtranstype);
15463 : :
15464 [ + + ]: 287 : if (strcmp(aggtransspace, "0") != 0)
15465 : : {
15466 : 5 : appendPQExpBuffer(details, ",\n SSPACE = %s",
15467 : : aggtransspace);
15468 : : }
15469 : :
15470 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_agginitval))
15471 : : {
15472 : 209 : appendPQExpBufferStr(details, ",\n INITCOND = ");
15473 : 209 : appendStringLiteralAH(details, agginitval, fout);
15474 : : }
15475 : :
15476 [ + + ]: 287 : if (strcmp(aggfinalfn, "-") != 0)
15477 : : {
15478 : 134 : appendPQExpBuffer(details, ",\n FINALFUNC = %s",
15479 : : aggfinalfn);
15480 [ + + ]: 134 : if (aggfinalextra)
15481 : 10 : appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
15482 [ + + ]: 134 : if (aggfinalmodify != defaultfinalmodify)
15483 : : {
15484 [ - + - - ]: 34 : switch (aggfinalmodify)
15485 : : {
15486 : 0 : case AGGMODIFY_READ_ONLY:
15487 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
15488 : 0 : break;
15489 : 34 : case AGGMODIFY_SHAREABLE:
15490 : 34 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
15491 : 34 : break;
15492 : 0 : case AGGMODIFY_READ_WRITE:
15493 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
15494 : 0 : break;
15495 : 0 : default:
15496 : 0 : pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
15497 : : agginfo->aggfn.dobj.name);
15498 : : break;
15499 : : }
15500 : : }
15501 : : }
15502 : :
15503 [ - + ]: 287 : if (strcmp(aggcombinefn, "-") != 0)
15504 : 0 : appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
15505 : :
15506 [ - + ]: 287 : if (strcmp(aggserialfn, "-") != 0)
15507 : 0 : appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
15508 : :
15509 [ - + ]: 287 : if (strcmp(aggdeserialfn, "-") != 0)
15510 : 0 : appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
15511 : :
15512 [ + + ]: 287 : if (strcmp(aggmtransfn, "-") != 0)
15513 : : {
15514 : 30 : appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
15515 : : aggmtransfn,
15516 : : aggminvtransfn,
15517 : : aggmtranstype);
15518 : : }
15519 : :
15520 [ - + ]: 287 : if (strcmp(aggmtransspace, "0") != 0)
15521 : : {
15522 : 0 : appendPQExpBuffer(details, ",\n MSSPACE = %s",
15523 : : aggmtransspace);
15524 : : }
15525 : :
15526 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_aggminitval))
15527 : : {
15528 : 10 : appendPQExpBufferStr(details, ",\n MINITCOND = ");
15529 : 10 : appendStringLiteralAH(details, aggminitval, fout);
15530 : : }
15531 : :
15532 [ - + ]: 287 : if (strcmp(aggmfinalfn, "-") != 0)
15533 : : {
15534 : 0 : appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
15535 : : aggmfinalfn);
15536 [ # # ]: 0 : if (aggmfinalextra)
15537 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
15538 [ # # ]: 0 : if (aggmfinalmodify != defaultfinalmodify)
15539 : : {
15540 [ # # # # ]: 0 : switch (aggmfinalmodify)
15541 : : {
15542 : 0 : case AGGMODIFY_READ_ONLY:
15543 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
15544 : 0 : break;
15545 : 0 : case AGGMODIFY_SHAREABLE:
15546 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
15547 : 0 : break;
15548 : 0 : case AGGMODIFY_READ_WRITE:
15549 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
15550 : 0 : break;
15551 : 0 : default:
15552 : 0 : pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
15553 : : agginfo->aggfn.dobj.name);
15554 : : break;
15555 : : }
15556 : : }
15557 : : }
15558 : :
15559 : 287 : aggsortconvop = getFormattedOperatorName(aggsortop);
15560 [ - + ]: 287 : if (aggsortconvop)
15561 : : {
15562 : 0 : appendPQExpBuffer(details, ",\n SORTOP = %s",
15563 : : aggsortconvop);
15564 : 0 : free(aggsortconvop);
15565 : : }
15566 : :
15567 [ + + ]: 287 : if (strcmp(prosupport, "-") != 0)
15568 : : {
15569 : 5 : appendPQExpBuffer(details, ",\n SUPPORT = %s", prosupport);
15570 : : }
15571 : :
15572 [ + + ]: 287 : if (aggkind == AGGKIND_HYPOTHETICAL)
15573 : 5 : appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
15574 : :
15575 [ + + ]: 287 : if (proparallel[0] != PROPARALLEL_UNSAFE)
15576 : : {
15577 [ + - ]: 5 : if (proparallel[0] == PROPARALLEL_SAFE)
15578 : 5 : appendPQExpBufferStr(details, ",\n PARALLEL = safe");
15579 [ # # ]: 0 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
15580 : 0 : appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
15581 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
15582 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
15583 : : agginfo->aggfn.dobj.name);
15584 : : }
15585 : :
15586 : 287 : appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
15587 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15588 : : aggsig);
15589 : :
15590 [ + - ]: 574 : appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
15591 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15592 : : aggfullsig ? aggfullsig : aggsig, details->data);
15593 : :
15594 [ + + ]: 287 : if (dopt->binary_upgrade)
15595 : 49 : binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
15596 : : "AGGREGATE", aggsig,
15597 : 49 : agginfo->aggfn.dobj.namespace->dobj.name);
15598 : :
15599 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
15600 : 270 : ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
15601 : 270 : agginfo->aggfn.dobj.dumpId,
15602 : 270 : ARCHIVE_OPTS(.tag = aggsig_tag,
15603 : : .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
15604 : : .owner = agginfo->aggfn.rolname,
15605 : : .description = "AGGREGATE",
15606 : : .section = SECTION_PRE_DATA,
15607 : : .createStmt = q->data,
15608 : : .dropStmt = delq->data));
15609 : :
15610 : : /* Dump Aggregate Comments */
15611 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
15612 : 10 : dumpComment(fout, "AGGREGATE", aggsig,
15613 : 10 : agginfo->aggfn.dobj.namespace->dobj.name,
15614 : 10 : agginfo->aggfn.rolname,
15615 : 10 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15616 : :
15617 [ - + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
15618 : 0 : dumpSecLabel(fout, "AGGREGATE", aggsig,
15619 : 0 : agginfo->aggfn.dobj.namespace->dobj.name,
15620 : 0 : agginfo->aggfn.rolname,
15621 : 0 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15622 : :
15623 : : /*
15624 : : * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
15625 : : * command look like a function's GRANT; in particular this affects the
15626 : : * syntax for zero-argument aggregates and ordered-set aggregates.
15627 : : */
15628 : 287 : free(aggsig);
15629 : :
15630 : 287 : aggsig = format_function_signature(fout, &agginfo->aggfn, true);
15631 : :
15632 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
15633 : 18 : dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
15634 : : "FUNCTION", aggsig, NULL,
15635 : 18 : agginfo->aggfn.dobj.namespace->dobj.name,
15636 : 18 : NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
15637 : :
15638 : 287 : free(aggsig);
15639 : 287 : free(aggfullsig);
15640 : 287 : free(aggsig_tag);
15641 : :
15642 : 287 : PQclear(res);
15643 : :
15644 : 287 : destroyPQExpBuffer(query);
15645 : 287 : destroyPQExpBuffer(q);
15646 : 287 : destroyPQExpBuffer(delq);
15647 : 287 : destroyPQExpBuffer(details);
15648 : : }
15649 : :
15650 : : /*
15651 : : * dumpTSParser
15652 : : * write out a single text search parser
15653 : : */
15654 : : static void
15655 : 44 : dumpTSParser(Archive *fout, const TSParserInfo *prsinfo)
15656 : : {
15657 : 44 : DumpOptions *dopt = fout->dopt;
15658 : : PQExpBuffer q;
15659 : : PQExpBuffer delq;
15660 : : char *qprsname;
15661 : :
15662 : : /* Do nothing if not dumping schema */
15663 [ + + ]: 44 : if (!dopt->dumpSchema)
15664 : 7 : return;
15665 : :
15666 : 37 : q = createPQExpBuffer();
15667 : 37 : delq = createPQExpBuffer();
15668 : :
15669 : 37 : qprsname = pg_strdup(fmtId(prsinfo->dobj.name));
15670 : :
15671 : 37 : appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n",
15672 : 37 : fmtQualifiedDumpable(prsinfo));
15673 : :
15674 : 37 : appendPQExpBuffer(q, " START = %s,\n",
15675 : 37 : convertTSFunction(fout, prsinfo->prsstart));
15676 : 37 : appendPQExpBuffer(q, " GETTOKEN = %s,\n",
15677 : 37 : convertTSFunction(fout, prsinfo->prstoken));
15678 : 37 : appendPQExpBuffer(q, " END = %s,\n",
15679 : 37 : convertTSFunction(fout, prsinfo->prsend));
15680 [ + + ]: 37 : if (prsinfo->prsheadline != InvalidOid)
15681 : 3 : appendPQExpBuffer(q, " HEADLINE = %s,\n",
15682 : 3 : convertTSFunction(fout, prsinfo->prsheadline));
15683 : 37 : appendPQExpBuffer(q, " LEXTYPES = %s );\n",
15684 : 37 : convertTSFunction(fout, prsinfo->prslextype));
15685 : :
15686 : 37 : appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s;\n",
15687 : 37 : fmtQualifiedDumpable(prsinfo));
15688 : :
15689 [ + + ]: 37 : if (dopt->binary_upgrade)
15690 : 1 : binary_upgrade_extension_member(q, &prsinfo->dobj,
15691 : : "TEXT SEARCH PARSER", qprsname,
15692 : 1 : prsinfo->dobj.namespace->dobj.name);
15693 : :
15694 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15695 : 37 : ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId,
15696 : 37 : ARCHIVE_OPTS(.tag = prsinfo->dobj.name,
15697 : : .namespace = prsinfo->dobj.namespace->dobj.name,
15698 : : .description = "TEXT SEARCH PARSER",
15699 : : .section = SECTION_PRE_DATA,
15700 : : .createStmt = q->data,
15701 : : .dropStmt = delq->data));
15702 : :
15703 : : /* Dump Parser Comments */
15704 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15705 : 37 : dumpComment(fout, "TEXT SEARCH PARSER", qprsname,
15706 : 37 : prsinfo->dobj.namespace->dobj.name, "",
15707 : 37 : prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId);
15708 : :
15709 : 37 : destroyPQExpBuffer(q);
15710 : 37 : destroyPQExpBuffer(delq);
15711 : 37 : pg_free(qprsname);
15712 : : }
15713 : :
15714 : : /*
15715 : : * dumpTSDictionary
15716 : : * write out a single text search dictionary
15717 : : */
15718 : : static void
15719 : 182 : dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo)
15720 : : {
15721 : 182 : DumpOptions *dopt = fout->dopt;
15722 : : PQExpBuffer q;
15723 : : PQExpBuffer delq;
15724 : : PQExpBuffer query;
15725 : : char *qdictname;
15726 : : PGresult *res;
15727 : : char *nspname;
15728 : : char *tmplname;
15729 : :
15730 : : /* Do nothing if not dumping schema */
15731 [ + + ]: 182 : if (!dopt->dumpSchema)
15732 : 7 : return;
15733 : :
15734 : 175 : q = createPQExpBuffer();
15735 : 175 : delq = createPQExpBuffer();
15736 : 175 : query = createPQExpBuffer();
15737 : :
15738 : 175 : qdictname = pg_strdup(fmtId(dictinfo->dobj.name));
15739 : :
15740 : : /* Fetch name and namespace of the dictionary's template */
15741 : 175 : appendPQExpBuffer(query, "SELECT nspname, tmplname "
15742 : : "FROM pg_ts_template p, pg_namespace n "
15743 : : "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
15744 : 175 : dictinfo->dicttemplate);
15745 : 175 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15746 : 175 : nspname = PQgetvalue(res, 0, 0);
15747 : 175 : tmplname = PQgetvalue(res, 0, 1);
15748 : :
15749 : 175 : appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n",
15750 : 175 : fmtQualifiedDumpable(dictinfo));
15751 : :
15752 : 175 : appendPQExpBufferStr(q, " TEMPLATE = ");
15753 : 175 : appendPQExpBuffer(q, "%s.", fmtId(nspname));
15754 : 175 : appendPQExpBufferStr(q, fmtId(tmplname));
15755 : :
15756 : 175 : PQclear(res);
15757 : :
15758 : : /* the dictinitoption can be dumped straight into the command */
15759 [ + + ]: 175 : if (dictinfo->dictinitoption)
15760 : 138 : appendPQExpBuffer(q, ",\n %s", dictinfo->dictinitoption);
15761 : :
15762 : 175 : appendPQExpBufferStr(q, " );\n");
15763 : :
15764 : 175 : appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s;\n",
15765 : 175 : fmtQualifiedDumpable(dictinfo));
15766 : :
15767 [ + + ]: 175 : if (dopt->binary_upgrade)
15768 : 10 : binary_upgrade_extension_member(q, &dictinfo->dobj,
15769 : : "TEXT SEARCH DICTIONARY", qdictname,
15770 : 10 : dictinfo->dobj.namespace->dobj.name);
15771 : :
15772 [ + - ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15773 : 175 : ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId,
15774 : 175 : ARCHIVE_OPTS(.tag = dictinfo->dobj.name,
15775 : : .namespace = dictinfo->dobj.namespace->dobj.name,
15776 : : .owner = dictinfo->rolname,
15777 : : .description = "TEXT SEARCH DICTIONARY",
15778 : : .section = SECTION_PRE_DATA,
15779 : : .createStmt = q->data,
15780 : : .dropStmt = delq->data));
15781 : :
15782 : : /* Dump Dictionary Comments */
15783 [ + + ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15784 : 130 : dumpComment(fout, "TEXT SEARCH DICTIONARY", qdictname,
15785 : 130 : dictinfo->dobj.namespace->dobj.name, dictinfo->rolname,
15786 : 130 : dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId);
15787 : :
15788 : 175 : destroyPQExpBuffer(q);
15789 : 175 : destroyPQExpBuffer(delq);
15790 : 175 : destroyPQExpBuffer(query);
15791 : 175 : pg_free(qdictname);
15792 : : }
15793 : :
15794 : : /*
15795 : : * dumpTSTemplate
15796 : : * write out a single text search template
15797 : : */
15798 : : static void
15799 : 56 : dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo)
15800 : : {
15801 : 56 : DumpOptions *dopt = fout->dopt;
15802 : : PQExpBuffer q;
15803 : : PQExpBuffer delq;
15804 : : char *qtmplname;
15805 : :
15806 : : /* Do nothing if not dumping schema */
15807 [ + + ]: 56 : if (!dopt->dumpSchema)
15808 : 7 : return;
15809 : :
15810 : 49 : q = createPQExpBuffer();
15811 : 49 : delq = createPQExpBuffer();
15812 : :
15813 : 49 : qtmplname = pg_strdup(fmtId(tmplinfo->dobj.name));
15814 : :
15815 : 49 : appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n",
15816 : 49 : fmtQualifiedDumpable(tmplinfo));
15817 : :
15818 [ + + ]: 49 : if (tmplinfo->tmplinit != InvalidOid)
15819 : 15 : appendPQExpBuffer(q, " INIT = %s,\n",
15820 : 15 : convertTSFunction(fout, tmplinfo->tmplinit));
15821 : 49 : appendPQExpBuffer(q, " LEXIZE = %s );\n",
15822 : 49 : convertTSFunction(fout, tmplinfo->tmpllexize));
15823 : :
15824 : 49 : appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s;\n",
15825 : 49 : fmtQualifiedDumpable(tmplinfo));
15826 : :
15827 [ + + ]: 49 : if (dopt->binary_upgrade)
15828 : 1 : binary_upgrade_extension_member(q, &tmplinfo->dobj,
15829 : : "TEXT SEARCH TEMPLATE", qtmplname,
15830 : 1 : tmplinfo->dobj.namespace->dobj.name);
15831 : :
15832 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15833 : 49 : ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId,
15834 : 49 : ARCHIVE_OPTS(.tag = tmplinfo->dobj.name,
15835 : : .namespace = tmplinfo->dobj.namespace->dobj.name,
15836 : : .description = "TEXT SEARCH TEMPLATE",
15837 : : .section = SECTION_PRE_DATA,
15838 : : .createStmt = q->data,
15839 : : .dropStmt = delq->data));
15840 : :
15841 : : /* Dump Template Comments */
15842 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15843 : 49 : dumpComment(fout, "TEXT SEARCH TEMPLATE", qtmplname,
15844 : 49 : tmplinfo->dobj.namespace->dobj.name, "",
15845 : 49 : tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId);
15846 : :
15847 : 49 : destroyPQExpBuffer(q);
15848 : 49 : destroyPQExpBuffer(delq);
15849 : 49 : pg_free(qtmplname);
15850 : : }
15851 : :
15852 : : /*
15853 : : * dumpTSConfig
15854 : : * write out a single text search configuration
15855 : : */
15856 : : static void
15857 : 157 : dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo)
15858 : : {
15859 : 157 : DumpOptions *dopt = fout->dopt;
15860 : : PQExpBuffer q;
15861 : : PQExpBuffer delq;
15862 : : PQExpBuffer query;
15863 : : char *qcfgname;
15864 : : PGresult *res;
15865 : : char *nspname;
15866 : : char *prsname;
15867 : : int ntups,
15868 : : i;
15869 : : int i_tokenname;
15870 : : int i_dictname;
15871 : :
15872 : : /* Do nothing if not dumping schema */
15873 [ + + ]: 157 : if (!dopt->dumpSchema)
15874 : 7 : return;
15875 : :
15876 : 150 : q = createPQExpBuffer();
15877 : 150 : delq = createPQExpBuffer();
15878 : 150 : query = createPQExpBuffer();
15879 : :
15880 : 150 : qcfgname = pg_strdup(fmtId(cfginfo->dobj.name));
15881 : :
15882 : : /* Fetch name and namespace of the config's parser */
15883 : 150 : appendPQExpBuffer(query, "SELECT nspname, prsname "
15884 : : "FROM pg_ts_parser p, pg_namespace n "
15885 : : "WHERE p.oid = '%u' AND n.oid = prsnamespace",
15886 : 150 : cfginfo->cfgparser);
15887 : 150 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15888 : 150 : nspname = PQgetvalue(res, 0, 0);
15889 : 150 : prsname = PQgetvalue(res, 0, 1);
15890 : :
15891 : 150 : appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n",
15892 : 150 : fmtQualifiedDumpable(cfginfo));
15893 : :
15894 : 150 : appendPQExpBuffer(q, " PARSER = %s.", fmtId(nspname));
15895 : 150 : appendPQExpBuffer(q, "%s );\n", fmtId(prsname));
15896 : :
15897 : 150 : PQclear(res);
15898 : :
15899 : 150 : resetPQExpBuffer(query);
15900 : 150 : appendPQExpBuffer(query,
15901 : : "SELECT\n"
15902 : : " ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n"
15903 : : " WHERE t.tokid = m.maptokentype ) AS tokenname,\n"
15904 : : " m.mapdict::pg_catalog.regdictionary AS dictname\n"
15905 : : "FROM pg_catalog.pg_ts_config_map AS m\n"
15906 : : "WHERE m.mapcfg = '%u'\n"
15907 : : "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
15908 : 150 : cfginfo->cfgparser, cfginfo->dobj.catId.oid);
15909 : :
15910 : 150 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15911 : 150 : ntups = PQntuples(res);
15912 : :
15913 : 150 : i_tokenname = PQfnumber(res, "tokenname");
15914 : 150 : i_dictname = PQfnumber(res, "dictname");
15915 : :
15916 [ + + ]: 3135 : for (i = 0; i < ntups; i++)
15917 : : {
15918 : 2985 : char *tokenname = PQgetvalue(res, i, i_tokenname);
15919 : 2985 : char *dictname = PQgetvalue(res, i, i_dictname);
15920 : :
15921 [ + + ]: 2985 : if (i == 0 ||
15922 [ + + ]: 2835 : strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0)
15923 : : {
15924 : : /* starting a new token type, so start a new command */
15925 [ + + ]: 2850 : if (i > 0)
15926 : 2700 : appendPQExpBufferStr(q, ";\n");
15927 : 2850 : appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n",
15928 : 2850 : fmtQualifiedDumpable(cfginfo));
15929 : : /* tokenname needs quoting, dictname does NOT */
15930 : 2850 : appendPQExpBuffer(q, " ADD MAPPING FOR %s WITH %s",
15931 : : fmtId(tokenname), dictname);
15932 : : }
15933 : : else
15934 : 135 : appendPQExpBuffer(q, ", %s", dictname);
15935 : : }
15936 : :
15937 [ + - ]: 150 : if (ntups > 0)
15938 : 150 : appendPQExpBufferStr(q, ";\n");
15939 : :
15940 : 150 : PQclear(res);
15941 : :
15942 : 150 : appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s;\n",
15943 : 150 : fmtQualifiedDumpable(cfginfo));
15944 : :
15945 [ + + ]: 150 : if (dopt->binary_upgrade)
15946 : 5 : binary_upgrade_extension_member(q, &cfginfo->dobj,
15947 : : "TEXT SEARCH CONFIGURATION", qcfgname,
15948 : 5 : cfginfo->dobj.namespace->dobj.name);
15949 : :
15950 [ + - ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15951 : 150 : ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId,
15952 : 150 : ARCHIVE_OPTS(.tag = cfginfo->dobj.name,
15953 : : .namespace = cfginfo->dobj.namespace->dobj.name,
15954 : : .owner = cfginfo->rolname,
15955 : : .description = "TEXT SEARCH CONFIGURATION",
15956 : : .section = SECTION_PRE_DATA,
15957 : : .createStmt = q->data,
15958 : : .dropStmt = delq->data));
15959 : :
15960 : : /* Dump Configuration Comments */
15961 [ + + ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15962 : 130 : dumpComment(fout, "TEXT SEARCH CONFIGURATION", qcfgname,
15963 : 130 : cfginfo->dobj.namespace->dobj.name, cfginfo->rolname,
15964 : 130 : cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId);
15965 : :
15966 : 150 : destroyPQExpBuffer(q);
15967 : 150 : destroyPQExpBuffer(delq);
15968 : 150 : destroyPQExpBuffer(query);
15969 : 150 : pg_free(qcfgname);
15970 : : }
15971 : :
15972 : : /*
15973 : : * dumpForeignDataWrapper
15974 : : * write out a single foreign-data wrapper definition
15975 : : */
15976 : : static void
15977 : 54 : dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo)
15978 : : {
15979 : 54 : DumpOptions *dopt = fout->dopt;
15980 : : PQExpBuffer q;
15981 : : PQExpBuffer delq;
15982 : : char *qfdwname;
15983 : :
15984 : : /* Do nothing if not dumping schema */
15985 [ + + ]: 54 : if (!dopt->dumpSchema)
15986 : 7 : return;
15987 : :
15988 : 47 : q = createPQExpBuffer();
15989 : 47 : delq = createPQExpBuffer();
15990 : :
15991 : 47 : qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name));
15992 : :
15993 : 47 : appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s",
15994 : : qfdwname);
15995 : :
15996 [ - + ]: 47 : if (strcmp(fdwinfo->fdwhandler, "-") != 0)
15997 : 0 : appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler);
15998 : :
15999 [ - + ]: 47 : if (strcmp(fdwinfo->fdwvalidator, "-") != 0)
16000 : 0 : appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator);
16001 : :
16002 [ - + ]: 47 : if (strcmp(fdwinfo->fdwconnection, "-") != 0)
16003 : 0 : appendPQExpBuffer(q, " CONNECTION %s", fdwinfo->fdwconnection);
16004 : :
16005 [ - + ]: 47 : if (strlen(fdwinfo->fdwoptions) > 0)
16006 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", fdwinfo->fdwoptions);
16007 : :
16008 : 47 : appendPQExpBufferStr(q, ";\n");
16009 : :
16010 : 47 : appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n",
16011 : : qfdwname);
16012 : :
16013 [ + + ]: 47 : if (dopt->binary_upgrade)
16014 : 2 : binary_upgrade_extension_member(q, &fdwinfo->dobj,
16015 : : "FOREIGN DATA WRAPPER", qfdwname,
16016 : : NULL);
16017 : :
16018 [ + - ]: 47 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16019 : 47 : ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
16020 : 47 : ARCHIVE_OPTS(.tag = fdwinfo->dobj.name,
16021 : : .owner = fdwinfo->rolname,
16022 : : .description = "FOREIGN DATA WRAPPER",
16023 : : .section = SECTION_PRE_DATA,
16024 : : .createStmt = q->data,
16025 : : .dropStmt = delq->data));
16026 : :
16027 : : /* Dump Foreign Data Wrapper Comments */
16028 [ - + ]: 47 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16029 : 0 : dumpComment(fout, "FOREIGN DATA WRAPPER", qfdwname,
16030 : 0 : NULL, fdwinfo->rolname,
16031 : 0 : fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
16032 : :
16033 : : /* Handle the ACL */
16034 [ + + ]: 47 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL)
16035 : 33 : dumpACL(fout, fdwinfo->dobj.dumpId, InvalidDumpId,
16036 : : "FOREIGN DATA WRAPPER", qfdwname, NULL, NULL,
16037 : 33 : NULL, fdwinfo->rolname, &fdwinfo->dacl);
16038 : :
16039 : 47 : pg_free(qfdwname);
16040 : :
16041 : 47 : destroyPQExpBuffer(q);
16042 : 47 : destroyPQExpBuffer(delq);
16043 : : }
16044 : :
16045 : : /*
16046 : : * dumpForeignServer
16047 : : * write out a foreign server definition
16048 : : */
16049 : : static void
16050 : 58 : dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo)
16051 : : {
16052 : 58 : DumpOptions *dopt = fout->dopt;
16053 : : PQExpBuffer q;
16054 : : PQExpBuffer delq;
16055 : : PQExpBuffer query;
16056 : : PGresult *res;
16057 : : char *qsrvname;
16058 : : char *fdwname;
16059 : :
16060 : : /* Do nothing if not dumping schema */
16061 [ + + ]: 58 : if (!dopt->dumpSchema)
16062 : 9 : return;
16063 : :
16064 : 49 : q = createPQExpBuffer();
16065 : 49 : delq = createPQExpBuffer();
16066 : 49 : query = createPQExpBuffer();
16067 : :
16068 : 49 : qsrvname = pg_strdup(fmtId(srvinfo->dobj.name));
16069 : :
16070 : : /* look up the foreign-data wrapper */
16071 : 49 : appendPQExpBuffer(query, "SELECT fdwname "
16072 : : "FROM pg_foreign_data_wrapper w "
16073 : : "WHERE w.oid = '%u'",
16074 : 49 : srvinfo->srvfdw);
16075 : 49 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
16076 : 49 : fdwname = PQgetvalue(res, 0, 0);
16077 : :
16078 : 49 : appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
16079 [ + - - + ]: 49 : if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0)
16080 : : {
16081 : 0 : appendPQExpBufferStr(q, " TYPE ");
16082 : 0 : appendStringLiteralAH(q, srvinfo->srvtype, fout);
16083 : : }
16084 [ + - - + ]: 49 : if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0)
16085 : : {
16086 : 0 : appendPQExpBufferStr(q, " VERSION ");
16087 : 0 : appendStringLiteralAH(q, srvinfo->srvversion, fout);
16088 : : }
16089 : :
16090 : 49 : appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER ");
16091 : 49 : appendPQExpBufferStr(q, fmtId(fdwname));
16092 : :
16093 [ + - - + ]: 49 : if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0)
16094 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", srvinfo->srvoptions);
16095 : :
16096 : 49 : appendPQExpBufferStr(q, ";\n");
16097 : :
16098 : 49 : appendPQExpBuffer(delq, "DROP SERVER %s;\n",
16099 : : qsrvname);
16100 : :
16101 [ + + ]: 49 : if (dopt->binary_upgrade)
16102 : 2 : binary_upgrade_extension_member(q, &srvinfo->dobj,
16103 : : "SERVER", qsrvname, NULL);
16104 : :
16105 [ + - ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16106 : 49 : ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
16107 : 49 : ARCHIVE_OPTS(.tag = srvinfo->dobj.name,
16108 : : .owner = srvinfo->rolname,
16109 : : .description = "SERVER",
16110 : : .section = SECTION_PRE_DATA,
16111 : : .createStmt = q->data,
16112 : : .dropStmt = delq->data));
16113 : :
16114 : : /* Dump Foreign Server Comments */
16115 [ - + ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16116 : 0 : dumpComment(fout, "SERVER", qsrvname,
16117 : 0 : NULL, srvinfo->rolname,
16118 : 0 : srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
16119 : :
16120 : : /* Handle the ACL */
16121 [ + + ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL)
16122 : 33 : dumpACL(fout, srvinfo->dobj.dumpId, InvalidDumpId,
16123 : : "FOREIGN SERVER", qsrvname, NULL, NULL,
16124 : 33 : NULL, srvinfo->rolname, &srvinfo->dacl);
16125 : :
16126 : : /* Dump user mappings */
16127 [ + - ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP)
16128 : 49 : dumpUserMappings(fout,
16129 : 49 : srvinfo->dobj.name, NULL,
16130 : 49 : srvinfo->rolname,
16131 : 49 : srvinfo->dobj.catId, srvinfo->dobj.dumpId);
16132 : :
16133 : 49 : PQclear(res);
16134 : :
16135 : 49 : pg_free(qsrvname);
16136 : :
16137 : 49 : destroyPQExpBuffer(q);
16138 : 49 : destroyPQExpBuffer(delq);
16139 : 49 : destroyPQExpBuffer(query);
16140 : : }
16141 : :
16142 : : /*
16143 : : * dumpUserMappings
16144 : : *
16145 : : * This routine is used to dump any user mappings associated with the
16146 : : * server handed to this routine. Should be called after ArchiveEntry()
16147 : : * for the server.
16148 : : */
16149 : : static void
16150 : 49 : dumpUserMappings(Archive *fout,
16151 : : const char *servername, const char *namespace,
16152 : : const char *owner,
16153 : : CatalogId catalogId, DumpId dumpId)
16154 : : {
16155 : : PQExpBuffer q;
16156 : : PQExpBuffer delq;
16157 : : PQExpBuffer query;
16158 : : PQExpBuffer tag;
16159 : : PGresult *res;
16160 : : int ntups;
16161 : : int i_usename;
16162 : : int i_umoptions;
16163 : : int i;
16164 : :
16165 : 49 : q = createPQExpBuffer();
16166 : 49 : tag = createPQExpBuffer();
16167 : 49 : delq = createPQExpBuffer();
16168 : 49 : query = createPQExpBuffer();
16169 : :
16170 : : /*
16171 : : * We read from the publicly accessible view pg_user_mappings, so as not
16172 : : * to fail if run by a non-superuser. Note that the view will show
16173 : : * umoptions as null if the user hasn't got privileges for the associated
16174 : : * server; this means that pg_dump will dump such a mapping, but with no
16175 : : * OPTIONS clause. A possible alternative is to skip such mappings
16176 : : * altogether, but it's not clear that that's an improvement.
16177 : : */
16178 : 49 : appendPQExpBuffer(query,
16179 : : "SELECT usename, "
16180 : : "array_to_string(ARRAY("
16181 : : "SELECT quote_ident(option_name) || ' ' || "
16182 : : "quote_literal(option_value) "
16183 : : "FROM pg_options_to_table(umoptions) "
16184 : : "ORDER BY option_name"
16185 : : "), E',\n ') AS umoptions "
16186 : : "FROM pg_user_mappings "
16187 : : "WHERE srvid = '%u' "
16188 : : "ORDER BY usename",
16189 : : catalogId.oid);
16190 : :
16191 : 49 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16192 : :
16193 : 49 : ntups = PQntuples(res);
16194 : 49 : i_usename = PQfnumber(res, "usename");
16195 : 49 : i_umoptions = PQfnumber(res, "umoptions");
16196 : :
16197 [ + + ]: 82 : for (i = 0; i < ntups; i++)
16198 : : {
16199 : : char *usename;
16200 : : char *umoptions;
16201 : :
16202 : 33 : usename = PQgetvalue(res, i, i_usename);
16203 : 33 : umoptions = PQgetvalue(res, i, i_umoptions);
16204 : :
16205 : 33 : resetPQExpBuffer(q);
16206 : 33 : appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
16207 : 33 : appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
16208 : :
16209 [ + - - + ]: 33 : if (umoptions && strlen(umoptions) > 0)
16210 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", umoptions);
16211 : :
16212 : 33 : appendPQExpBufferStr(q, ";\n");
16213 : :
16214 : 33 : resetPQExpBuffer(delq);
16215 : 33 : appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
16216 : 33 : appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
16217 : :
16218 : 33 : resetPQExpBuffer(tag);
16219 : 33 : appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
16220 : : usename, servername);
16221 : :
16222 : 33 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16223 : 33 : ARCHIVE_OPTS(.tag = tag->data,
16224 : : .namespace = namespace,
16225 : : .owner = owner,
16226 : : .description = "USER MAPPING",
16227 : : .section = SECTION_PRE_DATA,
16228 : : .createStmt = q->data,
16229 : : .dropStmt = delq->data));
16230 : : }
16231 : :
16232 : 49 : PQclear(res);
16233 : :
16234 : 49 : destroyPQExpBuffer(query);
16235 : 49 : destroyPQExpBuffer(delq);
16236 : 49 : destroyPQExpBuffer(tag);
16237 : 49 : destroyPQExpBuffer(q);
16238 : 49 : }
16239 : :
16240 : : /*
16241 : : * Write out default privileges information
16242 : : */
16243 : : static void
16244 : 170 : dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo)
16245 : : {
16246 : 170 : DumpOptions *dopt = fout->dopt;
16247 : : PQExpBuffer q;
16248 : : PQExpBuffer tag;
16249 : : const char *type;
16250 : :
16251 : : /* Do nothing if not dumping schema, or if we're skipping ACLs */
16252 [ + + + + ]: 170 : if (!dopt->dumpSchema || dopt->aclsSkip)
16253 : 30 : return;
16254 : :
16255 : 140 : q = createPQExpBuffer();
16256 : 140 : tag = createPQExpBuffer();
16257 : :
16258 [ + - + + : 140 : switch (daclinfo->defaclobjtype)
- - - ]
16259 : : {
16260 : 65 : case DEFACLOBJ_RELATION:
16261 : 65 : type = "TABLES";
16262 : 65 : break;
16263 : 0 : case DEFACLOBJ_SEQUENCE:
16264 : 0 : type = "SEQUENCES";
16265 : 0 : break;
16266 : 65 : case DEFACLOBJ_FUNCTION:
16267 : 65 : type = "FUNCTIONS";
16268 : 65 : break;
16269 : 10 : case DEFACLOBJ_TYPE:
16270 : 10 : type = "TYPES";
16271 : 10 : break;
16272 : 0 : case DEFACLOBJ_NAMESPACE:
16273 : 0 : type = "SCHEMAS";
16274 : 0 : break;
16275 : 0 : case DEFACLOBJ_LARGEOBJECT:
16276 : 0 : type = "LARGE OBJECTS";
16277 : 0 : break;
16278 : 0 : default:
16279 : : /* shouldn't get here */
16280 : 0 : pg_fatal("unrecognized object type in default privileges: %d",
16281 : : (int) daclinfo->defaclobjtype);
16282 : : type = ""; /* keep compiler quiet */
16283 : : }
16284 : :
16285 : 140 : appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
16286 : :
16287 : : /* build the actual command(s) for this tuple */
16288 [ - + ]: 140 : if (!buildDefaultACLCommands(type,
16289 : 140 : daclinfo->dobj.namespace != NULL ?
16290 : 66 : daclinfo->dobj.namespace->dobj.name : NULL,
16291 : 140 : daclinfo->dacl.acl,
16292 : 140 : daclinfo->dacl.acldefault,
16293 [ + + ]: 140 : daclinfo->defaclrole,
16294 : : fout->remoteVersion,
16295 : : q))
16296 : 0 : pg_fatal("could not parse default ACL list (%s)",
16297 : : daclinfo->dacl.acl);
16298 : :
16299 [ + - ]: 140 : if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
16300 : 140 : ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
16301 [ + + ]: 140 : ARCHIVE_OPTS(.tag = tag->data,
16302 : : .namespace = daclinfo->dobj.namespace ?
16303 : : daclinfo->dobj.namespace->dobj.name : NULL,
16304 : : .owner = daclinfo->defaclrole,
16305 : : .description = "DEFAULT ACL",
16306 : : .section = SECTION_POST_DATA,
16307 : : .createStmt = q->data));
16308 : :
16309 : 140 : destroyPQExpBuffer(tag);
16310 : 140 : destroyPQExpBuffer(q);
16311 : : }
16312 : :
16313 : : /*----------
16314 : : * Write out grant/revoke information
16315 : : *
16316 : : * 'objDumpId' is the dump ID of the underlying object.
16317 : : * 'altDumpId' can be a second dumpId that the ACL entry must also depend on,
16318 : : * or InvalidDumpId if there is no need for a second dependency.
16319 : : * 'type' must be one of
16320 : : * TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
16321 : : * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT.
16322 : : * 'name' is the formatted name of the object. Must be quoted etc. already.
16323 : : * 'subname' is the formatted name of the sub-object, if any. Must be quoted.
16324 : : * (Currently we assume that subname is only provided for table columns.)
16325 : : * 'nspname' is the namespace the object is in (NULL if none).
16326 : : * 'tag' is the tag to use for the ACL TOC entry; typically, this is NULL
16327 : : * to use the default for the object type.
16328 : : * 'owner' is the owner, NULL if there is no owner (for languages).
16329 : : * 'dacl' is the DumpableAcl struct for the object.
16330 : : *
16331 : : * Returns the dump ID assigned to the ACL TocEntry, or InvalidDumpId if
16332 : : * no ACL entry was created.
16333 : : *----------
16334 : : */
16335 : : static DumpId
16336 : 31433 : dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
16337 : : const char *type, const char *name, const char *subname,
16338 : : const char *nspname, const char *tag, const char *owner,
16339 : : const DumpableAcl *dacl)
16340 : : {
16341 : 31433 : DumpId aclDumpId = InvalidDumpId;
16342 : 31433 : DumpOptions *dopt = fout->dopt;
16343 : 31433 : const char *acls = dacl->acl;
16344 : 31433 : const char *acldefault = dacl->acldefault;
16345 : 31433 : char privtype = dacl->privtype;
16346 : 31433 : const char *initprivs = dacl->initprivs;
16347 : : const char *baseacls;
16348 : : PQExpBuffer sql;
16349 : :
16350 : : /* Do nothing if ACL dump is not enabled */
16351 [ + + ]: 31433 : if (dopt->aclsSkip)
16352 : 338 : return InvalidDumpId;
16353 : :
16354 : : /* --data-only skips ACLs *except* large object ACLs */
16355 [ + + + + ]: 31095 : if (!dopt->dumpSchema && strcmp(type, "LARGE OBJECT") != 0)
16356 : 1 : return InvalidDumpId;
16357 : :
16358 : 31094 : sql = createPQExpBuffer();
16359 : :
16360 : : /*
16361 : : * In binary upgrade mode, we don't run an extension's script but instead
16362 : : * dump out the objects independently and then recreate them. To preserve
16363 : : * any initial privileges which were set on extension objects, we need to
16364 : : * compute the set of GRANT and REVOKE commands necessary to get from the
16365 : : * default privileges of an object to its initial privileges as recorded
16366 : : * in pg_init_privs.
16367 : : *
16368 : : * At restore time, we apply these commands after having called
16369 : : * binary_upgrade_set_record_init_privs(true). That tells the backend to
16370 : : * copy the results into pg_init_privs. This is how we preserve the
16371 : : * contents of that catalog across binary upgrades.
16372 : : */
16373 [ + + + + : 31094 : if (dopt->binary_upgrade && privtype == 'e' &&
+ - ]
16374 [ + - ]: 13 : initprivs && *initprivs != '\0')
16375 : : {
16376 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
16377 [ - + ]: 13 : if (!buildACLCommands(name, subname, nspname, type,
16378 : : initprivs, acldefault, owner,
16379 : : "", fout->remoteVersion, sql))
16380 : 0 : pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
16381 : : initprivs, acldefault, name, type);
16382 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
16383 : : }
16384 : :
16385 : : /*
16386 : : * Now figure the GRANT and REVOKE commands needed to get to the object's
16387 : : * actual current ACL, starting from the initprivs if given, else from the
16388 : : * object-type-specific default. Also, while buildACLCommands will assume
16389 : : * that a NULL/empty acls string means it needn't do anything, what that
16390 : : * actually represents is the object-type-specific default; so we need to
16391 : : * substitute the acldefault string to get the right results in that case.
16392 : : */
16393 [ + + + + ]: 31094 : if (initprivs && *initprivs != '\0')
16394 : : {
16395 : 29262 : baseacls = initprivs;
16396 [ + - + + ]: 29262 : if (acls == NULL || *acls == '\0')
16397 : 17 : acls = acldefault;
16398 : : }
16399 : : else
16400 : 1832 : baseacls = acldefault;
16401 : :
16402 [ - + ]: 31094 : if (!buildACLCommands(name, subname, nspname, type,
16403 : : acls, baseacls, owner,
16404 : : "", fout->remoteVersion, sql))
16405 : 0 : pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
16406 : : acls, baseacls, name, type);
16407 : :
16408 [ + + ]: 31094 : if (sql->len > 0)
16409 : : {
16410 : 1889 : PQExpBuffer tagbuf = createPQExpBuffer();
16411 : : DumpId aclDeps[2];
16412 : 1889 : int nDeps = 0;
16413 : :
16414 [ - + ]: 1889 : if (tag)
16415 : 0 : appendPQExpBufferStr(tagbuf, tag);
16416 [ + + ]: 1889 : else if (subname)
16417 : 1111 : appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname);
16418 : : else
16419 : 778 : appendPQExpBuffer(tagbuf, "%s %s", type, name);
16420 : :
16421 : 1889 : aclDeps[nDeps++] = objDumpId;
16422 [ + + ]: 1889 : if (altDumpId != InvalidDumpId)
16423 : 1025 : aclDeps[nDeps++] = altDumpId;
16424 : :
16425 : 1889 : aclDumpId = createDumpId();
16426 : :
16427 : 1889 : ArchiveEntry(fout, nilCatalogId, aclDumpId,
16428 : 1889 : ARCHIVE_OPTS(.tag = tagbuf->data,
16429 : : .namespace = nspname,
16430 : : .owner = owner,
16431 : : .description = "ACL",
16432 : : .section = SECTION_NONE,
16433 : : .createStmt = sql->data,
16434 : : .deps = aclDeps,
16435 : : .nDeps = nDeps));
16436 : :
16437 : 1889 : destroyPQExpBuffer(tagbuf);
16438 : : }
16439 : :
16440 : 31094 : destroyPQExpBuffer(sql);
16441 : :
16442 : 31094 : return aclDumpId;
16443 : : }
16444 : :
16445 : : /*
16446 : : * dumpSecLabel
16447 : : *
16448 : : * This routine is used to dump any security labels associated with the
16449 : : * object handed to this routine. The routine takes the object type
16450 : : * and object name (ready to print, except for schema decoration), plus
16451 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
16452 : : * plus catalog ID and subid which are the lookup key for pg_seclabel,
16453 : : * plus the dump ID for the object (for setting a dependency).
16454 : : * If a matching pg_seclabel entry is found, it is dumped.
16455 : : *
16456 : : * Note: although this routine takes a dumpId for dependency purposes,
16457 : : * that purpose is just to mark the dependency in the emitted dump file
16458 : : * for possible future use by pg_restore. We do NOT use it for determining
16459 : : * ordering of the label in the dump file, because this routine is called
16460 : : * after dependency sorting occurs. This routine should be called just after
16461 : : * calling ArchiveEntry() for the specified object.
16462 : : */
16463 : : static void
16464 : 10 : dumpSecLabel(Archive *fout, const char *type, const char *name,
16465 : : const char *namespace, const char *owner,
16466 : : CatalogId catalogId, int subid, DumpId dumpId)
16467 : : {
16468 : 10 : DumpOptions *dopt = fout->dopt;
16469 : : SecLabelItem *labels;
16470 : : int nlabels;
16471 : : int i;
16472 : : PQExpBuffer query;
16473 : :
16474 : : /* do nothing, if --no-security-labels is supplied */
16475 [ - + ]: 10 : if (dopt->no_security_labels)
16476 : 0 : return;
16477 : :
16478 : : /*
16479 : : * Security labels are schema not data ... except large object labels are
16480 : : * data
16481 : : */
16482 [ - + ]: 10 : if (strcmp(type, "LARGE OBJECT") != 0)
16483 : : {
16484 [ # # ]: 0 : if (!dopt->dumpSchema)
16485 : 0 : return;
16486 : : }
16487 : : else
16488 : : {
16489 : : /* We do dump large object security labels in binary-upgrade mode */
16490 [ + - - + ]: 10 : if (!dopt->dumpData && !dopt->binary_upgrade)
16491 : 0 : return;
16492 : : }
16493 : :
16494 : : /* Search for security labels associated with catalogId, using table */
16495 : 10 : nlabels = findSecLabels(catalogId.tableoid, catalogId.oid, &labels);
16496 : :
16497 : 10 : query = createPQExpBuffer();
16498 : :
16499 [ + + ]: 15 : for (i = 0; i < nlabels; i++)
16500 : : {
16501 : : /*
16502 : : * Ignore label entries for which the subid doesn't match.
16503 : : */
16504 [ - + ]: 5 : if (labels[i].objsubid != subid)
16505 : 0 : continue;
16506 : :
16507 : 5 : appendPQExpBuffer(query,
16508 : : "SECURITY LABEL FOR %s ON %s ",
16509 : 5 : fmtId(labels[i].provider), type);
16510 [ - + - - ]: 5 : if (namespace && *namespace)
16511 : 0 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
16512 : 5 : appendPQExpBuffer(query, "%s IS ", name);
16513 : 5 : appendStringLiteralAH(query, labels[i].label, fout);
16514 : 5 : appendPQExpBufferStr(query, ";\n");
16515 : : }
16516 : :
16517 [ + + ]: 10 : if (query->len > 0)
16518 : : {
16519 : 5 : PQExpBuffer tag = createPQExpBuffer();
16520 : :
16521 : 5 : appendPQExpBuffer(tag, "%s %s", type, name);
16522 : 5 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16523 : 5 : ARCHIVE_OPTS(.tag = tag->data,
16524 : : .namespace = namespace,
16525 : : .owner = owner,
16526 : : .description = "SECURITY LABEL",
16527 : : .section = SECTION_NONE,
16528 : : .createStmt = query->data,
16529 : : .deps = &dumpId,
16530 : : .nDeps = 1));
16531 : 5 : destroyPQExpBuffer(tag);
16532 : : }
16533 : :
16534 : 10 : destroyPQExpBuffer(query);
16535 : : }
16536 : :
16537 : : /*
16538 : : * dumpTableSecLabel
16539 : : *
16540 : : * As above, but dump security label for both the specified table (or view)
16541 : : * and its columns.
16542 : : */
16543 : : static void
16544 : 0 : dumpTableSecLabel(Archive *fout, const TableInfo *tbinfo, const char *reltypename)
16545 : : {
16546 : 0 : DumpOptions *dopt = fout->dopt;
16547 : : SecLabelItem *labels;
16548 : : int nlabels;
16549 : : int i;
16550 : : PQExpBuffer query;
16551 : : PQExpBuffer target;
16552 : :
16553 : : /* do nothing, if --no-security-labels is supplied */
16554 [ # # ]: 0 : if (dopt->no_security_labels)
16555 : 0 : return;
16556 : :
16557 : : /* SecLabel are SCHEMA not data */
16558 [ # # ]: 0 : if (!dopt->dumpSchema)
16559 : 0 : return;
16560 : :
16561 : : /* Search for comments associated with relation, using table */
16562 : 0 : nlabels = findSecLabels(tbinfo->dobj.catId.tableoid,
16563 : 0 : tbinfo->dobj.catId.oid,
16564 : : &labels);
16565 : :
16566 : : /* If security labels exist, build SECURITY LABEL statements */
16567 [ # # ]: 0 : if (nlabels <= 0)
16568 : 0 : return;
16569 : :
16570 : 0 : query = createPQExpBuffer();
16571 : 0 : target = createPQExpBuffer();
16572 : :
16573 [ # # ]: 0 : for (i = 0; i < nlabels; i++)
16574 : : {
16575 : : const char *colname;
16576 : 0 : const char *provider = labels[i].provider;
16577 : 0 : const char *label = labels[i].label;
16578 : 0 : int objsubid = labels[i].objsubid;
16579 : :
16580 : 0 : resetPQExpBuffer(target);
16581 [ # # ]: 0 : if (objsubid == 0)
16582 : : {
16583 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16584 : 0 : fmtQualifiedDumpable(tbinfo));
16585 : : }
16586 : : else
16587 : : {
16588 : 0 : colname = getAttrName(objsubid, tbinfo);
16589 : : /* first fmtXXX result must be consumed before calling again */
16590 : 0 : appendPQExpBuffer(target, "COLUMN %s",
16591 : 0 : fmtQualifiedDumpable(tbinfo));
16592 : 0 : appendPQExpBuffer(target, ".%s", fmtId(colname));
16593 : : }
16594 : 0 : appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ",
16595 : : fmtId(provider), target->data);
16596 : 0 : appendStringLiteralAH(query, label, fout);
16597 : 0 : appendPQExpBufferStr(query, ";\n");
16598 : : }
16599 [ # # ]: 0 : if (query->len > 0)
16600 : : {
16601 : 0 : resetPQExpBuffer(target);
16602 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16603 : 0 : fmtId(tbinfo->dobj.name));
16604 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16605 : 0 : ARCHIVE_OPTS(.tag = target->data,
16606 : : .namespace = tbinfo->dobj.namespace->dobj.name,
16607 : : .owner = tbinfo->rolname,
16608 : : .description = "SECURITY LABEL",
16609 : : .section = SECTION_NONE,
16610 : : .createStmt = query->data,
16611 : : .deps = &(tbinfo->dobj.dumpId),
16612 : : .nDeps = 1));
16613 : : }
16614 : 0 : destroyPQExpBuffer(query);
16615 : 0 : destroyPQExpBuffer(target);
16616 : : }
16617 : :
16618 : : /*
16619 : : * findSecLabels
16620 : : *
16621 : : * Find the security label(s), if any, associated with the given object.
16622 : : * All the objsubid values associated with the given classoid/objoid are
16623 : : * found with one search.
16624 : : */
16625 : : static int
16626 : 10 : findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items)
16627 : : {
16628 : 10 : SecLabelItem *middle = NULL;
16629 : : SecLabelItem *low;
16630 : : SecLabelItem *high;
16631 : : int nmatch;
16632 : :
16633 [ - + ]: 10 : if (nseclabels <= 0) /* no labels, so no match is possible */
16634 : : {
16635 : 0 : *items = NULL;
16636 : 0 : return 0;
16637 : : }
16638 : :
16639 : : /*
16640 : : * Do binary search to find some item matching the object.
16641 : : */
16642 : 10 : low = &seclabels[0];
16643 : 10 : high = &seclabels[nseclabels - 1];
16644 [ + + ]: 15 : while (low <= high)
16645 : : {
16646 : 10 : middle = low + (high - low) / 2;
16647 : :
16648 [ - + ]: 10 : if (classoid < middle->classoid)
16649 : 0 : high = middle - 1;
16650 [ - + ]: 10 : else if (classoid > middle->classoid)
16651 : 0 : low = middle + 1;
16652 [ + + ]: 10 : else if (objoid < middle->objoid)
16653 : 5 : high = middle - 1;
16654 [ - + ]: 5 : else if (objoid > middle->objoid)
16655 : 0 : low = middle + 1;
16656 : : else
16657 : 5 : break; /* found a match */
16658 : : }
16659 : :
16660 [ + + ]: 10 : if (low > high) /* no matches */
16661 : : {
16662 : 5 : *items = NULL;
16663 : 5 : return 0;
16664 : : }
16665 : :
16666 : : /*
16667 : : * Now determine how many items match the object. The search loop
16668 : : * invariant still holds: only items between low and high inclusive could
16669 : : * match.
16670 : : */
16671 : 5 : nmatch = 1;
16672 [ - + ]: 5 : while (middle > low)
16673 : : {
16674 [ # # ]: 0 : if (classoid != middle[-1].classoid ||
16675 [ # # ]: 0 : objoid != middle[-1].objoid)
16676 : : break;
16677 : 0 : middle--;
16678 : 0 : nmatch++;
16679 : : }
16680 : :
16681 : 5 : *items = middle;
16682 : :
16683 : 5 : middle += nmatch;
16684 [ - + ]: 5 : while (middle <= high)
16685 : : {
16686 [ # # ]: 0 : if (classoid != middle->classoid ||
16687 [ # # ]: 0 : objoid != middle->objoid)
16688 : : break;
16689 : 0 : middle++;
16690 : 0 : nmatch++;
16691 : : }
16692 : :
16693 : 5 : return nmatch;
16694 : : }
16695 : :
16696 : : /*
16697 : : * collectSecLabels
16698 : : *
16699 : : * Construct a table of all security labels available for database objects;
16700 : : * also set the has-seclabel component flag for each relevant object.
16701 : : *
16702 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
16703 : : */
16704 : : static void
16705 : 193 : collectSecLabels(Archive *fout)
16706 : : {
16707 : : PGresult *res;
16708 : : PQExpBuffer query;
16709 : : int i_label;
16710 : : int i_provider;
16711 : : int i_classoid;
16712 : : int i_objoid;
16713 : : int i_objsubid;
16714 : : int ntups;
16715 : : int i;
16716 : : DumpableObject *dobj;
16717 : :
16718 : 193 : query = createPQExpBuffer();
16719 : :
16720 : 193 : appendPQExpBufferStr(query,
16721 : : "SELECT label, provider, classoid, objoid, objsubid "
16722 : : "FROM pg_catalog.pg_seclabels "
16723 : : "ORDER BY classoid, objoid, objsubid");
16724 : :
16725 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16726 : :
16727 : : /* Construct lookup table containing OIDs in numeric form */
16728 : 193 : i_label = PQfnumber(res, "label");
16729 : 193 : i_provider = PQfnumber(res, "provider");
16730 : 193 : i_classoid = PQfnumber(res, "classoid");
16731 : 193 : i_objoid = PQfnumber(res, "objoid");
16732 : 193 : i_objsubid = PQfnumber(res, "objsubid");
16733 : :
16734 : 193 : ntups = PQntuples(res);
16735 : :
16736 : 193 : seclabels = pg_malloc_array(SecLabelItem, ntups);
16737 : 193 : nseclabels = 0;
16738 : 193 : dobj = NULL;
16739 : :
16740 [ + + ]: 198 : for (i = 0; i < ntups; i++)
16741 : : {
16742 : : CatalogId objId;
16743 : : int subid;
16744 : :
16745 : 5 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
16746 : 5 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
16747 : 5 : subid = atoi(PQgetvalue(res, i, i_objsubid));
16748 : :
16749 : : /* We needn't remember labels that don't match any dumpable object */
16750 [ - + ]: 5 : if (dobj == NULL ||
16751 [ # # ]: 0 : dobj->catId.tableoid != objId.tableoid ||
16752 [ # # ]: 0 : dobj->catId.oid != objId.oid)
16753 : 5 : dobj = findObjectByCatalogId(objId);
16754 [ - + ]: 5 : if (dobj == NULL)
16755 : 0 : continue;
16756 : :
16757 : : /*
16758 : : * Labels on columns of composite types are linked to the type's
16759 : : * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
16760 : : * in the type's own DumpableObject.
16761 : : */
16762 [ - + - - ]: 5 : if (subid != 0 && dobj->objType == DO_TABLE &&
16763 [ # # ]: 0 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
16764 : 0 : {
16765 : : TypeInfo *cTypeInfo;
16766 : :
16767 : 0 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
16768 [ # # ]: 0 : if (cTypeInfo)
16769 : 0 : cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
16770 : : }
16771 : : else
16772 : 5 : dobj->components |= DUMP_COMPONENT_SECLABEL;
16773 : :
16774 : 5 : seclabels[nseclabels].label = pg_strdup(PQgetvalue(res, i, i_label));
16775 : 5 : seclabels[nseclabels].provider = pg_strdup(PQgetvalue(res, i, i_provider));
16776 : 5 : seclabels[nseclabels].classoid = objId.tableoid;
16777 : 5 : seclabels[nseclabels].objoid = objId.oid;
16778 : 5 : seclabels[nseclabels].objsubid = subid;
16779 : 5 : nseclabels++;
16780 : : }
16781 : :
16782 : 193 : PQclear(res);
16783 : 193 : destroyPQExpBuffer(query);
16784 : 193 : }
16785 : :
16786 : : /*
16787 : : * dumpTable
16788 : : * write out to fout the declarations (not data) of a user-defined table
16789 : : */
16790 : : static void
16791 : 33316 : dumpTable(Archive *fout, const TableInfo *tbinfo)
16792 : : {
16793 : 33316 : DumpOptions *dopt = fout->dopt;
16794 : 33316 : DumpId tableAclDumpId = InvalidDumpId;
16795 : : char *namecopy;
16796 : :
16797 : : /* Do nothing if not dumping schema */
16798 [ + + ]: 33316 : if (!dopt->dumpSchema)
16799 : 1624 : return;
16800 : :
16801 [ + + ]: 31692 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16802 : : {
16803 [ + + ]: 6915 : if (tbinfo->relkind == RELKIND_SEQUENCE)
16804 : 381 : dumpSequence(fout, tbinfo);
16805 : : else
16806 : 6534 : dumpTableSchema(fout, tbinfo);
16807 : : }
16808 : :
16809 : : /* Handle the ACL here */
16810 : 31692 : namecopy = pg_strdup(fmtId(tbinfo->dobj.name));
16811 [ + + ]: 31692 : if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
16812 : : {
16813 : 25530 : const char *objtype =
16814 [ + + ]: 25530 : (tbinfo->relkind == RELKIND_SEQUENCE) ? "SEQUENCE" : "TABLE";
16815 : :
16816 : : tableAclDumpId =
16817 : 25530 : dumpACL(fout, tbinfo->dobj.dumpId, InvalidDumpId,
16818 : : objtype, namecopy, NULL,
16819 : 25530 : tbinfo->dobj.namespace->dobj.name,
16820 : 25530 : NULL, tbinfo->rolname, &tbinfo->dacl);
16821 : : }
16822 : :
16823 : : /*
16824 : : * Handle column ACLs, if any. Note: we pull these with a separate query
16825 : : * rather than trying to fetch them during getTableAttrs, so that we won't
16826 : : * miss ACLs on system columns. Doing it this way also allows us to dump
16827 : : * ACLs for catalogs that we didn't mark "interesting" back in getTables.
16828 : : */
16829 [ + + + + ]: 31692 : if ((tbinfo->dobj.dump & DUMP_COMPONENT_ACL) && tbinfo->hascolumnACLs)
16830 : : {
16831 : 293 : PQExpBuffer query = createPQExpBuffer();
16832 : : PGresult *res;
16833 : : int i;
16834 : :
16835 [ + + ]: 293 : if (!fout->is_prepared[PREPQUERY_GETCOLUMNACLS])
16836 : : {
16837 : : /* Set up query for column ACLs */
16838 : 166 : appendPQExpBufferStr(query,
16839 : : "PREPARE getColumnACLs(pg_catalog.oid) AS\n");
16840 : :
16841 : : /*
16842 : : * In principle we should call acldefault('c', relowner) to get
16843 : : * the default ACL for a column. However, we don't currently
16844 : : * store the numeric OID of the relowner in TableInfo. We could
16845 : : * convert the owner name using regrole, but that creates a risk
16846 : : * of failure due to concurrent role renames. Given that the
16847 : : * default ACL for columns is empty and is likely to stay that
16848 : : * way, it's not worth extra cycles and risk to avoid hard-wiring
16849 : : * that knowledge here.
16850 : : */
16851 : 166 : appendPQExpBufferStr(query,
16852 : : "SELECT at.attname, "
16853 : : "at.attacl, "
16854 : : "'{}' AS acldefault, "
16855 : : "pip.privtype, pip.initprivs "
16856 : : "FROM pg_catalog.pg_attribute at "
16857 : : "LEFT JOIN pg_catalog.pg_init_privs pip ON "
16858 : : "(at.attrelid = pip.objoid "
16859 : : "AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass "
16860 : : "AND at.attnum = pip.objsubid) "
16861 : : "WHERE at.attrelid = $1 AND "
16862 : : "NOT at.attisdropped "
16863 : : "AND (at.attacl IS NOT NULL OR pip.initprivs IS NOT NULL) "
16864 : : "ORDER BY at.attnum");
16865 : :
16866 : 166 : ExecuteSqlStatement(fout, query->data);
16867 : :
16868 : 166 : fout->is_prepared[PREPQUERY_GETCOLUMNACLS] = true;
16869 : : }
16870 : :
16871 : 293 : printfPQExpBuffer(query,
16872 : : "EXECUTE getColumnACLs('%u')",
16873 : 293 : tbinfo->dobj.catId.oid);
16874 : :
16875 : 293 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16876 : :
16877 [ + + ]: 5316 : for (i = 0; i < PQntuples(res); i++)
16878 : : {
16879 : 5023 : char *attname = PQgetvalue(res, i, 0);
16880 : 5023 : char *attacl = PQgetvalue(res, i, 1);
16881 : 5023 : char *acldefault = PQgetvalue(res, i, 2);
16882 : 5023 : char privtype = *(PQgetvalue(res, i, 3));
16883 : 5023 : char *initprivs = PQgetvalue(res, i, 4);
16884 : : DumpableAcl coldacl;
16885 : : char *attnamecopy;
16886 : :
16887 : 5023 : coldacl.acl = attacl;
16888 : 5023 : coldacl.acldefault = acldefault;
16889 : 5023 : coldacl.privtype = privtype;
16890 : 5023 : coldacl.initprivs = initprivs;
16891 : 5023 : attnamecopy = pg_strdup(fmtId(attname));
16892 : :
16893 : : /*
16894 : : * Column's GRANT type is always TABLE. Each column ACL depends
16895 : : * on the table-level ACL, since we can restore column ACLs in
16896 : : * parallel but the table-level ACL has to be done first.
16897 : : */
16898 : 5023 : dumpACL(fout, tbinfo->dobj.dumpId, tableAclDumpId,
16899 : : "TABLE", namecopy, attnamecopy,
16900 : 5023 : tbinfo->dobj.namespace->dobj.name,
16901 : 5023 : NULL, tbinfo->rolname, &coldacl);
16902 : 5023 : pg_free(attnamecopy);
16903 : : }
16904 : 293 : PQclear(res);
16905 : 293 : destroyPQExpBuffer(query);
16906 : : }
16907 : :
16908 : 31692 : pg_free(namecopy);
16909 : : }
16910 : :
16911 : : /*
16912 : : * Create the AS clause for a view or materialized view. The semicolon is
16913 : : * stripped because a materialized view must add a WITH NO DATA clause.
16914 : : *
16915 : : * This returns a new buffer which must be freed by the caller.
16916 : : */
16917 : : static PQExpBuffer
16918 : 906 : createViewAsClause(Archive *fout, const TableInfo *tbinfo)
16919 : : {
16920 : 906 : PQExpBuffer query = createPQExpBuffer();
16921 : 906 : PQExpBuffer result = createPQExpBuffer();
16922 : : PGresult *res;
16923 : : int len;
16924 : :
16925 : : /* Fetch the view definition */
16926 : 906 : appendPQExpBuffer(query,
16927 : : "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
16928 : 906 : tbinfo->dobj.catId.oid);
16929 : :
16930 : 906 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16931 : :
16932 [ - + ]: 906 : if (PQntuples(res) != 1)
16933 : : {
16934 [ # # ]: 0 : if (PQntuples(res) < 1)
16935 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned no data",
16936 : : tbinfo->dobj.name);
16937 : : else
16938 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
16939 : : tbinfo->dobj.name);
16940 : : }
16941 : :
16942 : 906 : len = PQgetlength(res, 0, 0);
16943 : :
16944 [ - + ]: 906 : if (len == 0)
16945 : 0 : pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
16946 : : tbinfo->dobj.name);
16947 : :
16948 : : /* Strip off the trailing semicolon so that other things may follow. */
16949 : : Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
16950 : 906 : appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
16951 : :
16952 : 906 : PQclear(res);
16953 : 906 : destroyPQExpBuffer(query);
16954 : :
16955 : 906 : return result;
16956 : : }
16957 : :
16958 : : /*
16959 : : * Create a dummy AS clause for a view. This is used when the real view
16960 : : * definition has to be postponed because of circular dependencies.
16961 : : * We must duplicate the view's external properties -- column names and types
16962 : : * (including collation) -- so that it works for subsequent references.
16963 : : *
16964 : : * This returns a new buffer which must be freed by the caller.
16965 : : */
16966 : : static PQExpBuffer
16967 : 20 : createDummyViewAsClause(Archive *fout, const TableInfo *tbinfo)
16968 : : {
16969 : 20 : PQExpBuffer result = createPQExpBuffer();
16970 : : int j;
16971 : :
16972 : 20 : appendPQExpBufferStr(result, "SELECT");
16973 : :
16974 [ + + ]: 40 : for (j = 0; j < tbinfo->numatts; j++)
16975 : : {
16976 [ + + ]: 20 : if (j > 0)
16977 : 10 : appendPQExpBufferChar(result, ',');
16978 : 20 : appendPQExpBufferStr(result, "\n ");
16979 : :
16980 : 20 : appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
16981 : :
16982 : : /*
16983 : : * Must add collation if not default for the type, because CREATE OR
16984 : : * REPLACE VIEW won't change it
16985 : : */
16986 [ - + ]: 20 : if (OidIsValid(tbinfo->attcollation[j]))
16987 : : {
16988 : : CollInfo *coll;
16989 : :
16990 : 0 : coll = findCollationByOid(tbinfo->attcollation[j]);
16991 [ # # ]: 0 : if (coll)
16992 : 0 : appendPQExpBuffer(result, " COLLATE %s",
16993 : 0 : fmtQualifiedDumpable(coll));
16994 : : }
16995 : :
16996 : 20 : appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
16997 : : }
16998 : :
16999 : 20 : return result;
17000 : : }
17001 : :
17002 : : /*
17003 : : * dumpTableSchema
17004 : : * write the declaration (not data) of one user-defined table or view
17005 : : */
17006 : : static void
17007 : 6534 : dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
17008 : : {
17009 : 6534 : DumpOptions *dopt = fout->dopt;
17010 : 6534 : PQExpBuffer q = createPQExpBuffer();
17011 : 6534 : PQExpBuffer delq = createPQExpBuffer();
17012 : 6534 : PQExpBuffer extra = createPQExpBuffer();
17013 : : char *qrelname;
17014 : : char *qualrelname;
17015 : : int numParents;
17016 : : TableInfo **parents;
17017 : : int actual_atts; /* number of attrs in this CREATE statement */
17018 : : const char *reltypename;
17019 : : char *storage;
17020 : : int j,
17021 : : k;
17022 : :
17023 : : /* We had better have loaded per-column details about this table */
17024 : : Assert(tbinfo->interesting);
17025 : :
17026 : 6534 : qrelname = pg_strdup(fmtId(tbinfo->dobj.name));
17027 : 6534 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
17028 : :
17029 [ - + ]: 6534 : if (tbinfo->hasoids)
17030 : 0 : pg_log_warning("WITH OIDS is not supported anymore (table \"%s\")",
17031 : : qrelname);
17032 : :
17033 [ + + ]: 6534 : if (dopt->binary_upgrade)
17034 : 892 : binary_upgrade_set_type_oids_by_rel(fout, q, tbinfo);
17035 : :
17036 : : /* Is it a table or a view? */
17037 [ + + ]: 6534 : if (tbinfo->relkind == RELKIND_VIEW)
17038 : : {
17039 : : PQExpBuffer result;
17040 : :
17041 : : /*
17042 : : * Note: keep this code in sync with the is_view case in dumpRule()
17043 : : */
17044 : :
17045 : 553 : reltypename = "VIEW";
17046 : :
17047 : 553 : appendPQExpBuffer(delq, "DROP VIEW %s;\n", qualrelname);
17048 : :
17049 [ + + ]: 553 : if (dopt->binary_upgrade)
17050 : 52 : binary_upgrade_set_pg_class_oids(fout, q,
17051 : 52 : tbinfo->dobj.catId.oid);
17052 : :
17053 : 553 : appendPQExpBuffer(q, "CREATE VIEW %s", qualrelname);
17054 : :
17055 [ + + ]: 553 : if (tbinfo->dummy_view)
17056 : 10 : result = createDummyViewAsClause(fout, tbinfo);
17057 : : else
17058 : : {
17059 [ + + ]: 543 : if (nonemptyReloptions(tbinfo->reloptions))
17060 : : {
17061 : 63 : appendPQExpBufferStr(q, " WITH (");
17062 : 63 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17063 : 63 : appendPQExpBufferChar(q, ')');
17064 : : }
17065 : 543 : result = createViewAsClause(fout, tbinfo);
17066 : : }
17067 : 553 : appendPQExpBuffer(q, " AS\n%s", result->data);
17068 : 553 : destroyPQExpBuffer(result);
17069 : :
17070 [ + + + - ]: 553 : if (tbinfo->checkoption != NULL && !tbinfo->dummy_view)
17071 : 34 : appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption);
17072 : 553 : appendPQExpBufferStr(q, ";\n");
17073 : : }
17074 : : else
17075 : : {
17076 : 5981 : char *partkeydef = NULL;
17077 : 5981 : char *ftoptions = NULL;
17078 : 5981 : char *srvname = NULL;
17079 : 5981 : const char *foreign = "";
17080 : :
17081 : : /*
17082 : : * Set reltypename, and collect any relkind-specific data that we
17083 : : * didn't fetch during getTables().
17084 : : */
17085 [ + + + + ]: 5981 : switch (tbinfo->relkind)
17086 : : {
17087 : 604 : case RELKIND_PARTITIONED_TABLE:
17088 : : {
17089 : 604 : PQExpBuffer query = createPQExpBuffer();
17090 : : PGresult *res;
17091 : :
17092 : 604 : reltypename = "TABLE";
17093 : :
17094 : : /* retrieve partition key definition */
17095 : 604 : appendPQExpBuffer(query,
17096 : : "SELECT pg_get_partkeydef('%u')",
17097 : 604 : tbinfo->dobj.catId.oid);
17098 : 604 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17099 : 604 : partkeydef = pg_strdup(PQgetvalue(res, 0, 0));
17100 : 604 : PQclear(res);
17101 : 604 : destroyPQExpBuffer(query);
17102 : 604 : break;
17103 : : }
17104 : 36 : case RELKIND_FOREIGN_TABLE:
17105 : : {
17106 : 36 : PQExpBuffer query = createPQExpBuffer();
17107 : : PGresult *res;
17108 : : int i_srvname;
17109 : : int i_ftoptions;
17110 : :
17111 : 36 : reltypename = "FOREIGN TABLE";
17112 : :
17113 : : /* retrieve name of foreign server and generic options */
17114 : 36 : appendPQExpBuffer(query,
17115 : : "SELECT fs.srvname, "
17116 : : "pg_catalog.array_to_string(ARRAY("
17117 : : "SELECT pg_catalog.quote_ident(option_name) || "
17118 : : "' ' || pg_catalog.quote_literal(option_value) "
17119 : : "FROM pg_catalog.pg_options_to_table(ftoptions) "
17120 : : "ORDER BY option_name"
17121 : : "), E',\n ') AS ftoptions "
17122 : : "FROM pg_catalog.pg_foreign_table ft "
17123 : : "JOIN pg_catalog.pg_foreign_server fs "
17124 : : "ON (fs.oid = ft.ftserver) "
17125 : : "WHERE ft.ftrelid = '%u'",
17126 : 36 : tbinfo->dobj.catId.oid);
17127 : 36 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17128 : 36 : i_srvname = PQfnumber(res, "srvname");
17129 : 36 : i_ftoptions = PQfnumber(res, "ftoptions");
17130 : 36 : srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
17131 : 36 : ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
17132 : 36 : PQclear(res);
17133 : 36 : destroyPQExpBuffer(query);
17134 : :
17135 : 36 : foreign = "FOREIGN ";
17136 : 36 : break;
17137 : : }
17138 : 353 : case RELKIND_MATVIEW:
17139 : 353 : reltypename = "MATERIALIZED VIEW";
17140 : 353 : break;
17141 : 4988 : default:
17142 : 4988 : reltypename = "TABLE";
17143 : 4988 : break;
17144 : : }
17145 : :
17146 : 5981 : numParents = tbinfo->numParents;
17147 : 5981 : parents = tbinfo->parents;
17148 : :
17149 : 5981 : appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname);
17150 : :
17151 [ + + ]: 5981 : if (dopt->binary_upgrade)
17152 : 840 : binary_upgrade_set_pg_class_oids(fout, q,
17153 : 840 : tbinfo->dobj.catId.oid);
17154 : :
17155 : : /*
17156 : : * PostgreSQL 18 has disabled UNLOGGED for partitioned tables, so
17157 : : * ignore it when dumping if it was set in this case.
17158 : : */
17159 : 5981 : appendPQExpBuffer(q, "CREATE %s%s %s",
17160 [ + + ]: 5981 : (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
17161 [ + - ]: 20 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ?
17162 : : "UNLOGGED " : "",
17163 : : reltypename,
17164 : : qualrelname);
17165 : :
17166 : : /*
17167 : : * Attach to type, if reloftype; except in case of a binary upgrade,
17168 : : * we dump the table normally and attach it to the type afterward.
17169 : : */
17170 [ + + + + ]: 5981 : if (OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade)
17171 : 24 : appendPQExpBuffer(q, " OF %s",
17172 : 24 : getFormattedTypeName(fout, tbinfo->reloftype,
17173 : : zeroIsError));
17174 : :
17175 [ + + ]: 5981 : if (tbinfo->relkind != RELKIND_MATVIEW)
17176 : : {
17177 : : /* Dump the attributes */
17178 : 5628 : actual_atts = 0;
17179 [ + + ]: 26092 : for (j = 0; j < tbinfo->numatts; j++)
17180 : : {
17181 : : /*
17182 : : * Normally, dump if it's locally defined in this table, and
17183 : : * not dropped. But for binary upgrade, we'll dump all the
17184 : : * columns, and then fix up the dropped and nonlocal cases
17185 : : * below.
17186 : : */
17187 [ + + ]: 20464 : if (shouldPrintColumn(dopt, tbinfo, j))
17188 : : {
17189 : : bool print_default;
17190 : : bool print_notnull;
17191 : :
17192 : : /*
17193 : : * Default value --- suppress if to be printed separately
17194 : : * or not at all.
17195 : : */
17196 : 39920 : print_default = (tbinfo->attrdefs[j] != NULL &&
17197 [ + + + + ]: 20472 : tbinfo->attrdefs[j]->dobj.dump &&
17198 [ + + ]: 1073 : !tbinfo->attrdefs[j]->separate);
17199 : :
17200 : : /*
17201 : : * Not Null constraint --- print it if it is locally
17202 : : * defined, or if binary upgrade. (In the latter case, we
17203 : : * reset conislocal below.)
17204 : : */
17205 [ + + ]: 21744 : print_notnull = (tbinfo->notnull_constrs[j] != NULL &&
17206 [ + + ]: 2345 : (tbinfo->notnull_islocal[j] ||
17207 [ + + ]: 655 : dopt->binary_upgrade ||
17208 [ + + ]: 565 : tbinfo->ispartition));
17209 : :
17210 : : /*
17211 : : * Skip column if fully defined by reloftype, except in
17212 : : * binary upgrade
17213 : : */
17214 [ + + ]: 19399 : if (OidIsValid(tbinfo->reloftype) &&
17215 [ + + + + ]: 50 : !print_default && !print_notnull &&
17216 [ + + ]: 30 : !dopt->binary_upgrade)
17217 : 24 : continue;
17218 : :
17219 : : /* Format properly if not first attr */
17220 [ + + ]: 19375 : if (actual_atts == 0)
17221 : 5257 : appendPQExpBufferStr(q, " (");
17222 : : else
17223 : 14118 : appendPQExpBufferChar(q, ',');
17224 : 19375 : appendPQExpBufferStr(q, "\n ");
17225 : 19375 : actual_atts++;
17226 : :
17227 : : /* Attribute name */
17228 : 19375 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j]));
17229 : :
17230 [ + + ]: 19375 : if (tbinfo->attisdropped[j])
17231 : : {
17232 : : /*
17233 : : * ALTER TABLE DROP COLUMN clears
17234 : : * pg_attribute.atttypid, so we will not have gotten a
17235 : : * valid type name; insert INTEGER as a stopgap. We'll
17236 : : * clean things up later.
17237 : : */
17238 : 84 : appendPQExpBufferStr(q, " INTEGER /* dummy */");
17239 : : /* and skip to the next column */
17240 : 84 : continue;
17241 : : }
17242 : :
17243 : : /*
17244 : : * Attribute type; print it except when creating a typed
17245 : : * table ('OF type_name'), but in binary-upgrade mode,
17246 : : * print it in that case too.
17247 : : */
17248 [ + + + + ]: 19291 : if (dopt->binary_upgrade || !OidIsValid(tbinfo->reloftype))
17249 : : {
17250 : 19275 : appendPQExpBuffer(q, " %s",
17251 : 19275 : tbinfo->atttypnames[j]);
17252 : : }
17253 : :
17254 [ + + ]: 19291 : if (print_default)
17255 : : {
17256 [ + + ]: 939 : if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED)
17257 : 328 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s) STORED",
17258 : 328 : tbinfo->attrdefs[j]->adef_expr);
17259 [ + + ]: 611 : else if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_VIRTUAL)
17260 : 230 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s)",
17261 : 230 : tbinfo->attrdefs[j]->adef_expr);
17262 : : else
17263 : 381 : appendPQExpBuffer(q, " DEFAULT %s",
17264 : 381 : tbinfo->attrdefs[j]->adef_expr);
17265 : : }
17266 : :
17267 [ + + ]: 19291 : if (print_notnull)
17268 : : {
17269 [ + + ]: 2312 : if (tbinfo->notnull_constrs[j][0] == '\0')
17270 : 1629 : appendPQExpBufferStr(q, " NOT NULL");
17271 : : else
17272 : 683 : appendPQExpBuffer(q, " CONSTRAINT %s NOT NULL",
17273 : 683 : fmtId(tbinfo->notnull_constrs[j]));
17274 : :
17275 [ + + ]: 2312 : if (tbinfo->notnull_noinh[j])
17276 : 35 : appendPQExpBufferStr(q, " NO INHERIT");
17277 : : }
17278 : :
17279 : : /* Add collation if not default for the type */
17280 [ + + ]: 19291 : if (OidIsValid(tbinfo->attcollation[j]))
17281 : : {
17282 : : CollInfo *coll;
17283 : :
17284 : 203 : coll = findCollationByOid(tbinfo->attcollation[j]);
17285 [ + - ]: 203 : if (coll)
17286 : 203 : appendPQExpBuffer(q, " COLLATE %s",
17287 : 203 : fmtQualifiedDumpable(coll));
17288 : : }
17289 : : }
17290 : :
17291 : : /*
17292 : : * On the other hand, if we choose not to print a column
17293 : : * (likely because it is created by inheritance), but the
17294 : : * column has a locally-defined not-null constraint, we need
17295 : : * to dump the constraint as a standalone object.
17296 : : *
17297 : : * This syntax isn't SQL-conforming, but if you wanted
17298 : : * standard output you wouldn't be creating non-standard
17299 : : * objects to begin with.
17300 : : */
17301 [ + + ]: 20356 : if (!shouldPrintColumn(dopt, tbinfo, j) &&
17302 [ + + ]: 1065 : !tbinfo->attisdropped[j] &&
17303 [ + + ]: 700 : tbinfo->notnull_constrs[j] != NULL &&
17304 [ + + ]: 216 : tbinfo->notnull_islocal[j])
17305 : : {
17306 : : /* Format properly if not first attr */
17307 [ + + ]: 94 : if (actual_atts == 0)
17308 : 90 : appendPQExpBufferStr(q, " (");
17309 : : else
17310 : 4 : appendPQExpBufferChar(q, ',');
17311 : 94 : appendPQExpBufferStr(q, "\n ");
17312 : 94 : actual_atts++;
17313 : :
17314 [ + + ]: 94 : if (tbinfo->notnull_constrs[j][0] == '\0')
17315 : 8 : appendPQExpBuffer(q, "NOT NULL %s",
17316 : 8 : fmtId(tbinfo->attnames[j]));
17317 : : else
17318 : 172 : appendPQExpBuffer(q, "CONSTRAINT %s NOT NULL %s",
17319 : 86 : tbinfo->notnull_constrs[j],
17320 : 86 : fmtId(tbinfo->attnames[j]));
17321 : :
17322 [ + + ]: 94 : if (tbinfo->notnull_noinh[j])
17323 : 33 : appendPQExpBufferStr(q, " NO INHERIT");
17324 : : }
17325 : : }
17326 : :
17327 : : /*
17328 : : * Add non-inherited CHECK constraints, if any.
17329 : : *
17330 : : * For partitions, we need to include check constraints even if
17331 : : * they're not defined locally, because the ALTER TABLE ATTACH
17332 : : * PARTITION that we'll emit later expects the constraint to be
17333 : : * there. (No need to fix conislocal: ATTACH PARTITION does that)
17334 : : */
17335 [ + + ]: 6221 : for (j = 0; j < tbinfo->ncheck; j++)
17336 : : {
17337 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
17338 : :
17339 [ + + ]: 593 : if (constr->separate ||
17340 [ + + + + ]: 523 : (!constr->conislocal && !tbinfo->ispartition))
17341 : 109 : continue;
17342 : :
17343 [ + + ]: 484 : if (actual_atts == 0)
17344 : 16 : appendPQExpBufferStr(q, " (\n ");
17345 : : else
17346 : 468 : appendPQExpBufferStr(q, ",\n ");
17347 : :
17348 : 484 : appendPQExpBuffer(q, "CONSTRAINT %s ",
17349 : 484 : fmtId(constr->dobj.name));
17350 : 484 : appendPQExpBufferStr(q, constr->condef);
17351 : :
17352 : 484 : actual_atts++;
17353 : : }
17354 : :
17355 [ + + ]: 5628 : if (actual_atts)
17356 : 5363 : appendPQExpBufferStr(q, "\n)");
17357 [ + + - + ]: 265 : else if (!(OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade))
17358 : : {
17359 : : /*
17360 : : * No attributes? we must have a parenthesized attribute list,
17361 : : * even though empty, when not using the OF TYPE syntax.
17362 : : */
17363 : 253 : appendPQExpBufferStr(q, " (\n)");
17364 : : }
17365 : :
17366 : : /*
17367 : : * Emit the INHERITS clause (not for partitions), except in
17368 : : * binary-upgrade mode.
17369 : : */
17370 [ + + + + ]: 5628 : if (numParents > 0 && !tbinfo->ispartition &&
17371 [ + + ]: 535 : !dopt->binary_upgrade)
17372 : : {
17373 : 470 : appendPQExpBufferStr(q, "\nINHERITS (");
17374 [ + + ]: 1013 : for (k = 0; k < numParents; k++)
17375 : : {
17376 : 543 : TableInfo *parentRel = parents[k];
17377 : :
17378 [ + + ]: 543 : if (k > 0)
17379 : 73 : appendPQExpBufferStr(q, ", ");
17380 : 543 : appendPQExpBufferStr(q, fmtQualifiedDumpable(parentRel));
17381 : : }
17382 : 470 : appendPQExpBufferChar(q, ')');
17383 : : }
17384 : :
17385 [ + + ]: 5628 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
17386 : 604 : appendPQExpBuffer(q, "\nPARTITION BY %s", partkeydef);
17387 : :
17388 [ + + ]: 5628 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
17389 : 36 : appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
17390 : : }
17391 : :
17392 [ + + - + ]: 11807 : if (nonemptyReloptions(tbinfo->reloptions) ||
17393 : 5826 : nonemptyReloptions(tbinfo->toast_reloptions))
17394 : : {
17395 : 155 : bool addcomma = false;
17396 : :
17397 : 155 : appendPQExpBufferStr(q, "\nWITH (");
17398 [ + - ]: 155 : if (nonemptyReloptions(tbinfo->reloptions))
17399 : : {
17400 : 155 : addcomma = true;
17401 : 155 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17402 : : }
17403 [ + + ]: 155 : if (nonemptyReloptions(tbinfo->toast_reloptions))
17404 : : {
17405 [ + - ]: 5 : if (addcomma)
17406 : 5 : appendPQExpBufferStr(q, ", ");
17407 : 5 : appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.",
17408 : : fout);
17409 : : }
17410 : 155 : appendPQExpBufferChar(q, ')');
17411 : : }
17412 : :
17413 : : /* Dump generic options if any */
17414 [ + + + + ]: 5981 : if (ftoptions && ftoptions[0])
17415 : 34 : appendPQExpBuffer(q, "\nOPTIONS (\n %s\n)", ftoptions);
17416 : :
17417 : : /*
17418 : : * For materialized views, create the AS clause just like a view. At
17419 : : * this point, we always mark the view as not populated.
17420 : : */
17421 [ + + ]: 5981 : if (tbinfo->relkind == RELKIND_MATVIEW)
17422 : : {
17423 : : PQExpBuffer result;
17424 : :
17425 : 353 : result = createViewAsClause(fout, tbinfo);
17426 : 353 : appendPQExpBuffer(q, " AS\n%s\n WITH NO DATA;\n",
17427 : : result->data);
17428 : 353 : destroyPQExpBuffer(result);
17429 : : }
17430 : : else
17431 : 5628 : appendPQExpBufferStr(q, ";\n");
17432 : :
17433 : : /* Materialized views can depend on extensions */
17434 [ + + ]: 5981 : if (tbinfo->relkind == RELKIND_MATVIEW)
17435 : 353 : append_depends_on_extension(fout, q, &tbinfo->dobj,
17436 : : "pg_catalog.pg_class",
17437 : : "MATERIALIZED VIEW",
17438 : : qualrelname);
17439 : :
17440 : : /*
17441 : : * in binary upgrade mode, update the catalog with any missing values
17442 : : * that might be present.
17443 : : */
17444 [ + + ]: 5981 : if (dopt->binary_upgrade)
17445 : : {
17446 [ + + ]: 4042 : for (j = 0; j < tbinfo->numatts; j++)
17447 : : {
17448 [ + + ]: 3202 : if (tbinfo->attmissingval[j][0] != '\0')
17449 : : {
17450 : 3 : appendPQExpBufferStr(q, "\n-- set missing value.\n");
17451 : 3 : appendPQExpBufferStr(q,
17452 : : "SELECT pg_catalog.binary_upgrade_set_missing_value(");
17453 : 3 : appendStringLiteralAH(q, qualrelname, fout);
17454 : 3 : appendPQExpBufferStr(q, "::pg_catalog.regclass,");
17455 : 3 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17456 : 3 : appendPQExpBufferChar(q, ',');
17457 : 3 : appendStringLiteralAH(q, tbinfo->attmissingval[j], fout);
17458 : 3 : appendPQExpBufferStr(q, ");\n\n");
17459 : : }
17460 : : }
17461 : : }
17462 : :
17463 : : /*
17464 : : * To create binary-compatible heap files, we have to ensure the same
17465 : : * physical column order, including dropped columns, as in the
17466 : : * original. Therefore, we create dropped columns above and drop them
17467 : : * here, also updating their attlen/attalign values so that the
17468 : : * dropped column can be skipped properly. (We do not bother with
17469 : : * restoring the original attbyval setting.) Also, inheritance
17470 : : * relationships are set up by doing ALTER TABLE INHERIT rather than
17471 : : * using an INHERITS clause --- the latter would possibly mess up the
17472 : : * column order. That also means we have to take care about setting
17473 : : * attislocal correctly, plus fix up any inherited CHECK constraints.
17474 : : * Analogously, we set up typed tables using ALTER TABLE / OF here.
17475 : : *
17476 : : * We process foreign and partitioned tables here, even though they
17477 : : * lack heap storage, because they can participate in inheritance
17478 : : * relationships and we want this stuff to be consistent across the
17479 : : * inheritance tree. We can exclude indexes, toast tables, sequences
17480 : : * and matviews, even though they have storage, because we don't
17481 : : * support altering or dropping columns in them, nor can they be part
17482 : : * of inheritance trees.
17483 : : */
17484 [ + + ]: 5981 : if (dopt->binary_upgrade &&
17485 [ + + ]: 840 : (tbinfo->relkind == RELKIND_RELATION ||
17486 [ + + ]: 115 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
17487 [ + + ]: 114 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE))
17488 : : {
17489 : : bool firstitem;
17490 : : bool firstitem_extra;
17491 : :
17492 : : /*
17493 : : * Drop any dropped columns. Merge the pg_attribute manipulations
17494 : : * into a single SQL command, so that we don't cause repeated
17495 : : * relcache flushes on the target table. Otherwise we risk O(N^2)
17496 : : * relcache bloat while dropping N columns.
17497 : : */
17498 : 823 : resetPQExpBuffer(extra);
17499 : 823 : firstitem = true;
17500 [ + + ]: 4004 : for (j = 0; j < tbinfo->numatts; j++)
17501 : : {
17502 [ + + ]: 3181 : if (tbinfo->attisdropped[j])
17503 : : {
17504 [ + + ]: 84 : if (firstitem)
17505 : : {
17506 : 38 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped columns.\n"
17507 : : "UPDATE pg_catalog.pg_attribute\n"
17508 : : "SET attlen = v.dlen, "
17509 : : "attalign = v.dalign, "
17510 : : "attbyval = false\n"
17511 : : "FROM (VALUES ");
17512 : 38 : firstitem = false;
17513 : : }
17514 : : else
17515 : 46 : appendPQExpBufferStr(q, ",\n ");
17516 : 84 : appendPQExpBufferChar(q, '(');
17517 : 84 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17518 : 84 : appendPQExpBuffer(q, ", %d, '%c')",
17519 : 84 : tbinfo->attlen[j],
17520 : 84 : tbinfo->attalign[j]);
17521 : : /* The ALTER ... DROP COLUMN commands must come after */
17522 : 84 : appendPQExpBuffer(extra, "ALTER %sTABLE ONLY %s ",
17523 : : foreign, qualrelname);
17524 : 84 : appendPQExpBuffer(extra, "DROP COLUMN %s;\n",
17525 : 84 : fmtId(tbinfo->attnames[j]));
17526 : : }
17527 : : }
17528 [ + + ]: 823 : if (!firstitem)
17529 : : {
17530 : 38 : appendPQExpBufferStr(q, ") v(dname, dlen, dalign)\n"
17531 : : "WHERE attrelid = ");
17532 : 38 : appendStringLiteralAH(q, qualrelname, fout);
17533 : 38 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17534 : : " AND attname = v.dname;\n");
17535 : : /* Now we can issue the actual DROP COLUMN commands */
17536 : 38 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17537 : : }
17538 : :
17539 : : /*
17540 : : * Fix up inherited columns. As above, do the pg_attribute
17541 : : * manipulations in a single SQL command.
17542 : : */
17543 : 823 : firstitem = true;
17544 [ + + ]: 4004 : for (j = 0; j < tbinfo->numatts; j++)
17545 : : {
17546 [ + + ]: 3181 : if (!tbinfo->attisdropped[j] &&
17547 [ + + ]: 3097 : !tbinfo->attislocal[j])
17548 : : {
17549 [ + + ]: 636 : if (firstitem)
17550 : : {
17551 : 279 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited columns.\n");
17552 : 279 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n"
17553 : : "SET attislocal = false\n"
17554 : : "WHERE attrelid = ");
17555 : 279 : appendStringLiteralAH(q, qualrelname, fout);
17556 : 279 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17557 : : " AND attname IN (");
17558 : 279 : firstitem = false;
17559 : : }
17560 : : else
17561 : 357 : appendPQExpBufferStr(q, ", ");
17562 : 636 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17563 : : }
17564 : : }
17565 [ + + ]: 823 : if (!firstitem)
17566 : 279 : appendPQExpBufferStr(q, ");\n");
17567 : :
17568 : : /*
17569 : : * Fix up not-null constraints that come from inheritance. As
17570 : : * above, do the pg_constraint manipulations in a single SQL
17571 : : * command. (Actually, two in special cases, if we're doing an
17572 : : * upgrade from < 18).
17573 : : */
17574 : 823 : firstitem = true;
17575 : 823 : firstitem_extra = true;
17576 : 823 : resetPQExpBuffer(extra);
17577 [ + + ]: 4004 : for (j = 0; j < tbinfo->numatts; j++)
17578 : : {
17579 : : /*
17580 : : * If a not-null constraint comes from inheritance, reset
17581 : : * conislocal. The inhcount is fixed by ALTER TABLE INHERIT,
17582 : : * below. Special hack: in versions < 18, columns with no
17583 : : * local definition need their constraint to be matched by
17584 : : * column number in conkeys instead of by constraint name,
17585 : : * because the latter is not available. (We distinguish the
17586 : : * case because the constraint name is the empty string.)
17587 : : */
17588 [ + + ]: 3181 : if (tbinfo->notnull_constrs[j] != NULL &&
17589 [ + + ]: 304 : !tbinfo->notnull_islocal[j])
17590 : : {
17591 [ + + ]: 90 : if (tbinfo->notnull_constrs[j][0] != '\0')
17592 : : {
17593 [ + + ]: 77 : if (firstitem)
17594 : : {
17595 : 65 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
17596 : : "SET conislocal = false\n"
17597 : : "WHERE contype = 'n' AND conrelid = ");
17598 : 65 : appendStringLiteralAH(q, qualrelname, fout);
17599 : 65 : appendPQExpBufferStr(q, "::pg_catalog.regclass AND\n"
17600 : : "conname IN (");
17601 : 65 : firstitem = false;
17602 : : }
17603 : : else
17604 : 12 : appendPQExpBufferStr(q, ", ");
17605 : 77 : appendStringLiteralAH(q, tbinfo->notnull_constrs[j], fout);
17606 : : }
17607 : : else
17608 : : {
17609 [ + - ]: 13 : if (firstitem_extra)
17610 : : {
17611 : 13 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17612 : : "SET conislocal = false\n"
17613 : : "WHERE contype = 'n' AND conrelid = ");
17614 : 13 : appendStringLiteralAH(extra, qualrelname, fout);
17615 : 13 : appendPQExpBufferStr(extra, "::pg_catalog.regclass AND\n"
17616 : : "conkey IN (");
17617 : 13 : firstitem_extra = false;
17618 : : }
17619 : : else
17620 : 0 : appendPQExpBufferStr(extra, ", ");
17621 : 13 : appendPQExpBuffer(extra, "'{%d}'", j + 1);
17622 : : }
17623 : : }
17624 : : }
17625 [ + + ]: 823 : if (!firstitem)
17626 : 65 : appendPQExpBufferStr(q, ");\n");
17627 [ + + ]: 823 : if (!firstitem_extra)
17628 : 13 : appendPQExpBufferStr(extra, ");\n");
17629 : :
17630 [ + + ]: 823 : if (extra->len > 0)
17631 : 13 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17632 : :
17633 : : /*
17634 : : * Add inherited CHECK constraints, if any.
17635 : : *
17636 : : * For partitions, they were already dumped, and conislocal
17637 : : * doesn't need fixing.
17638 : : *
17639 : : * As above, issue only one direct manipulation of pg_constraint.
17640 : : * Although it is tempting to merge the ALTER ADD CONSTRAINT
17641 : : * commands into one as well, refrain for now due to concern about
17642 : : * possible backend memory bloat if there are many such
17643 : : * constraints.
17644 : : */
17645 : 823 : resetPQExpBuffer(extra);
17646 : 823 : firstitem = true;
17647 [ + + ]: 885 : for (k = 0; k < tbinfo->ncheck; k++)
17648 : : {
17649 : 62 : ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
17650 : :
17651 [ + + + + : 62 : if (constr->separate || constr->conislocal || tbinfo->ispartition)
+ + ]
17652 : 60 : continue;
17653 : :
17654 [ + - ]: 2 : if (firstitem)
17655 : 2 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraints.\n");
17656 : 2 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n",
17657 : : foreign, qualrelname,
17658 : 2 : fmtId(constr->dobj.name),
17659 : : constr->condef);
17660 : : /* Update pg_constraint after all the ALTER TABLEs */
17661 [ + - ]: 2 : if (firstitem)
17662 : : {
17663 : 2 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17664 : : "SET conislocal = false\n"
17665 : : "WHERE contype = 'c' AND conrelid = ");
17666 : 2 : appendStringLiteralAH(extra, qualrelname, fout);
17667 : 2 : appendPQExpBufferStr(extra, "::pg_catalog.regclass\n");
17668 : 2 : appendPQExpBufferStr(extra, " AND conname IN (");
17669 : 2 : firstitem = false;
17670 : : }
17671 : : else
17672 : 0 : appendPQExpBufferStr(extra, ", ");
17673 : 2 : appendStringLiteralAH(extra, constr->dobj.name, fout);
17674 : : }
17675 [ + + ]: 823 : if (!firstitem)
17676 : : {
17677 : 2 : appendPQExpBufferStr(extra, ");\n");
17678 : 2 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17679 : : }
17680 : :
17681 [ + + + + ]: 823 : if (numParents > 0 && !tbinfo->ispartition)
17682 : : {
17683 : 65 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
17684 [ + + ]: 141 : for (k = 0; k < numParents; k++)
17685 : : {
17686 : 76 : TableInfo *parentRel = parents[k];
17687 : :
17688 : 76 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s INHERIT %s;\n", foreign,
17689 : : qualrelname,
17690 : 76 : fmtQualifiedDumpable(parentRel));
17691 : : }
17692 : : }
17693 : :
17694 [ + + ]: 823 : if (OidIsValid(tbinfo->reloftype))
17695 : : {
17696 : 6 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n");
17697 : 6 : appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
17698 : : qualrelname,
17699 : 6 : getFormattedTypeName(fout, tbinfo->reloftype,
17700 : : zeroIsError));
17701 : : }
17702 : : }
17703 : :
17704 : : /*
17705 : : * In binary_upgrade mode, arrange to restore the old relfrozenxid and
17706 : : * relminmxid of all vacuumable relations. (While vacuum.c processes
17707 : : * TOAST tables semi-independently, here we see them only as children
17708 : : * of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the
17709 : : * child toast table is handled below.)
17710 : : */
17711 [ + + ]: 5981 : if (dopt->binary_upgrade &&
17712 [ + + ]: 840 : (tbinfo->relkind == RELKIND_RELATION ||
17713 [ + + ]: 115 : tbinfo->relkind == RELKIND_MATVIEW))
17714 : : {
17715 : 742 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
17716 : 742 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17717 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17718 : : "WHERE oid = ",
17719 : 742 : tbinfo->frozenxid, tbinfo->minmxid);
17720 : 742 : appendStringLiteralAH(q, qualrelname, fout);
17721 : 742 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17722 : :
17723 [ + + ]: 742 : if (tbinfo->toast_oid)
17724 : : {
17725 : : /*
17726 : : * The toast table will have the same OID at restore, so we
17727 : : * can safely target it by OID.
17728 : : */
17729 : 285 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
17730 : 285 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17731 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17732 : : "WHERE oid = '%u';\n",
17733 : 285 : tbinfo->toast_frozenxid,
17734 : 285 : tbinfo->toast_minmxid, tbinfo->toast_oid);
17735 : : }
17736 : : }
17737 : :
17738 : : /*
17739 : : * In binary_upgrade mode, restore matviews' populated status by
17740 : : * poking pg_class directly. This is pretty ugly, but we can't use
17741 : : * REFRESH MATERIALIZED VIEW since it's possible that some underlying
17742 : : * matview is not populated even though this matview is; in any case,
17743 : : * we want to transfer the matview's heap storage, not run REFRESH.
17744 : : */
17745 [ + + + + ]: 5981 : if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
17746 [ + + ]: 17 : tbinfo->relispopulated)
17747 : : {
17748 : 15 : appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n");
17749 : 15 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n"
17750 : : "SET relispopulated = 't'\n"
17751 : : "WHERE oid = ");
17752 : 15 : appendStringLiteralAH(q, qualrelname, fout);
17753 : 15 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17754 : : }
17755 : :
17756 : : /*
17757 : : * Dump additional per-column properties that we can't handle in the
17758 : : * main CREATE TABLE command.
17759 : : */
17760 [ + + ]: 26876 : for (j = 0; j < tbinfo->numatts; j++)
17761 : : {
17762 : : /* None of this applies to dropped columns */
17763 [ + + ]: 20895 : if (tbinfo->attisdropped[j])
17764 : 449 : continue;
17765 : :
17766 : : /*
17767 : : * Dump per-column statistics information. We only issue an ALTER
17768 : : * TABLE statement if the attstattarget entry for this column is
17769 : : * not the default value.
17770 : : */
17771 [ + + ]: 20446 : if (tbinfo->attstattarget[j] >= 0)
17772 : 34 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n",
17773 : : foreign, qualrelname,
17774 : 34 : fmtId(tbinfo->attnames[j]),
17775 : 34 : tbinfo->attstattarget[j]);
17776 : :
17777 : : /*
17778 : : * Dump per-column storage information. The statement is only
17779 : : * dumped if the storage has been changed from the type's default.
17780 : : */
17781 [ + + ]: 20446 : if (tbinfo->attstorage[j] != tbinfo->typstorage[j])
17782 : : {
17783 [ + + - + : 83 : switch (tbinfo->attstorage[j])
- ]
17784 : : {
17785 : 10 : case TYPSTORAGE_PLAIN:
17786 : 10 : storage = "PLAIN";
17787 : 10 : break;
17788 : 39 : case TYPSTORAGE_EXTERNAL:
17789 : 39 : storage = "EXTERNAL";
17790 : 39 : break;
17791 : 0 : case TYPSTORAGE_EXTENDED:
17792 : 0 : storage = "EXTENDED";
17793 : 0 : break;
17794 : 34 : case TYPSTORAGE_MAIN:
17795 : 34 : storage = "MAIN";
17796 : 34 : break;
17797 : 0 : default:
17798 : 0 : storage = NULL;
17799 : : }
17800 : :
17801 : : /*
17802 : : * Only dump the statement if it's a storage type we recognize
17803 : : */
17804 [ + - ]: 83 : if (storage != NULL)
17805 : 83 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n",
17806 : : foreign, qualrelname,
17807 : 83 : fmtId(tbinfo->attnames[j]),
17808 : : storage);
17809 : : }
17810 : :
17811 : : /*
17812 : : * Dump per-column compression, if it's been set.
17813 : : */
17814 [ + + ]: 20446 : if (!dopt->no_toast_compression)
17815 : : {
17816 : : const char *cmname;
17817 : :
17818 [ + + + ]: 20346 : switch (tbinfo->attcompression[j])
17819 : : {
17820 : 73 : case 'p':
17821 : 73 : cmname = "pglz";
17822 : 73 : break;
17823 : 39 : case 'l':
17824 : 39 : cmname = "lz4";
17825 : 39 : break;
17826 : 20234 : default:
17827 : 20234 : cmname = NULL;
17828 : 20234 : break;
17829 : : }
17830 : :
17831 [ + + ]: 20346 : if (cmname != NULL)
17832 : 112 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET COMPRESSION %s;\n",
17833 : : foreign, qualrelname,
17834 : 112 : fmtId(tbinfo->attnames[j]),
17835 : : cmname);
17836 : : }
17837 : :
17838 : : /*
17839 : : * Dump per-column attributes.
17840 : : */
17841 [ + + ]: 20446 : if (tbinfo->attoptions[j][0] != '\0')
17842 : 34 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n",
17843 : : foreign, qualrelname,
17844 : 34 : fmtId(tbinfo->attnames[j]),
17845 : 34 : tbinfo->attoptions[j]);
17846 : :
17847 : : /*
17848 : : * Dump per-column fdw options.
17849 : : */
17850 [ + + ]: 20446 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
17851 [ + + ]: 36 : tbinfo->attfdwoptions[j][0] != '\0')
17852 : 34 : appendPQExpBuffer(q,
17853 : : "ALTER FOREIGN TABLE ONLY %s ALTER COLUMN %s OPTIONS (\n"
17854 : : " %s\n"
17855 : : ");\n",
17856 : : qualrelname,
17857 : 34 : fmtId(tbinfo->attnames[j]),
17858 : 34 : tbinfo->attfdwoptions[j]);
17859 : : } /* end loop over columns */
17860 : :
17861 : 5981 : pg_free(partkeydef);
17862 : 5981 : pg_free(ftoptions);
17863 : 5981 : pg_free(srvname);
17864 : : }
17865 : :
17866 : : /*
17867 : : * dump properties we only have ALTER TABLE syntax for
17868 : : */
17869 [ + + ]: 6534 : if ((tbinfo->relkind == RELKIND_RELATION ||
17870 [ + + ]: 1546 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE ||
17871 [ + + ]: 942 : tbinfo->relkind == RELKIND_MATVIEW) &&
17872 [ + + ]: 5945 : tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT)
17873 : : {
17874 [ + - ]: 192 : if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX)
17875 : : {
17876 : : /* nothing to do, will be set when the index is dumped */
17877 : : }
17878 [ + - ]: 192 : else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING)
17879 : : {
17880 : 192 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n",
17881 : : qualrelname);
17882 : : }
17883 [ # # ]: 0 : else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL)
17884 : : {
17885 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n",
17886 : : qualrelname);
17887 : : }
17888 : : }
17889 : :
17890 [ + + ]: 6534 : if (tbinfo->forcerowsec)
17891 : 5 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n",
17892 : : qualrelname);
17893 : :
17894 [ + + ]: 6534 : if (dopt->binary_upgrade)
17895 : 892 : binary_upgrade_extension_member(q, &tbinfo->dobj,
17896 : : reltypename, qrelname,
17897 : 892 : tbinfo->dobj.namespace->dobj.name);
17898 : :
17899 [ + - ]: 6534 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17900 : : {
17901 : 6534 : char *tablespace = NULL;
17902 : 6534 : char *tableam = NULL;
17903 : :
17904 : : /*
17905 : : * _selectTablespace() relies on tablespace-enabled objects in the
17906 : : * default tablespace to have a tablespace of "" (empty string) versus
17907 : : * non-tablespace-enabled objects to have a tablespace of NULL.
17908 : : * getTables() sets tbinfo->reltablespace to "" for the default
17909 : : * tablespace (not NULL).
17910 : : */
17911 [ + + + - : 6534 : if (RELKIND_HAS_TABLESPACE(tbinfo->relkind))
+ - + - +
+ + + - +
+ - ]
17912 : 5945 : tablespace = tbinfo->reltablespace;
17913 : :
17914 [ + + + - : 6534 : if (RELKIND_HAS_TABLE_AM(tbinfo->relkind) ||
+ + ]
17915 [ + + ]: 1193 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
17916 : 5945 : tableam = tbinfo->amname;
17917 : :
17918 : 6534 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
17919 [ + + ]: 6534 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
17920 : : .namespace = tbinfo->dobj.namespace->dobj.name,
17921 : : .tablespace = tablespace,
17922 : : .tableam = tableam,
17923 : : .relkind = tbinfo->relkind,
17924 : : .owner = tbinfo->rolname,
17925 : : .description = reltypename,
17926 : : .section = tbinfo->postponed_def ?
17927 : : SECTION_POST_DATA : SECTION_PRE_DATA,
17928 : : .createStmt = q->data,
17929 : : .dropStmt = delq->data));
17930 : : }
17931 : :
17932 : : /* Dump Table Comments */
17933 [ + + ]: 6534 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17934 : 78 : dumpTableComment(fout, tbinfo, reltypename);
17935 : :
17936 : : /* Dump Table Security Labels */
17937 [ - + ]: 6534 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
17938 : 0 : dumpTableSecLabel(fout, tbinfo, reltypename);
17939 : :
17940 : : /*
17941 : : * Dump comments for not-null constraints that aren't to be dumped
17942 : : * separately (those are processed by collectComments/dumpComment).
17943 : : */
17944 [ + - + - ]: 6534 : if (!fout->dopt->no_comments && dopt->dumpSchema &&
17945 [ + - ]: 6534 : fout->remoteVersion >= 180000)
17946 : : {
17947 : 6534 : PQExpBuffer comment = NULL;
17948 : 6534 : PQExpBuffer tag = NULL;
17949 : :
17950 [ + + ]: 31070 : for (j = 0; j < tbinfo->numatts; j++)
17951 : : {
17952 [ + + ]: 24536 : if (tbinfo->notnull_constrs[j] != NULL &&
17953 [ + + ]: 2561 : tbinfo->notnull_comment[j] != NULL)
17954 : : {
17955 [ + - ]: 44 : if (comment == NULL)
17956 : : {
17957 : 44 : comment = createPQExpBuffer();
17958 : 44 : tag = createPQExpBuffer();
17959 : : }
17960 : : else
17961 : : {
17962 : 0 : resetPQExpBuffer(comment);
17963 : 0 : resetPQExpBuffer(tag);
17964 : : }
17965 : :
17966 : 44 : appendPQExpBuffer(comment, "COMMENT ON CONSTRAINT %s ON %s IS ",
17967 : 44 : fmtId(tbinfo->notnull_constrs[j]), qualrelname);
17968 : 44 : appendStringLiteralAH(comment, tbinfo->notnull_comment[j], fout);
17969 : 44 : appendPQExpBufferStr(comment, ";\n");
17970 : :
17971 : 44 : appendPQExpBuffer(tag, "CONSTRAINT %s ON %s",
17972 : 44 : fmtId(tbinfo->notnull_constrs[j]), qrelname);
17973 : :
17974 : 44 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
17975 : 44 : ARCHIVE_OPTS(.tag = tag->data,
17976 : : .namespace = tbinfo->dobj.namespace->dobj.name,
17977 : : .owner = tbinfo->rolname,
17978 : : .description = "COMMENT",
17979 : : .section = SECTION_NONE,
17980 : : .createStmt = comment->data,
17981 : : .deps = &(tbinfo->dobj.dumpId),
17982 : : .nDeps = 1));
17983 : : }
17984 : : }
17985 : :
17986 : 6534 : destroyPQExpBuffer(comment);
17987 : 6534 : destroyPQExpBuffer(tag);
17988 : : }
17989 : :
17990 : : /* Dump comments on inlined table constraints */
17991 [ + + ]: 7127 : for (j = 0; j < tbinfo->ncheck; j++)
17992 : : {
17993 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
17994 : :
17995 [ + + + + ]: 593 : if (constr->separate || !constr->conislocal)
17996 : 254 : continue;
17997 : :
17998 [ + + ]: 339 : if (constr->dobj.dump & DUMP_COMPONENT_COMMENT)
17999 : 39 : dumpTableConstraintComment(fout, constr);
18000 : : }
18001 : :
18002 : 6534 : destroyPQExpBuffer(q);
18003 : 6534 : destroyPQExpBuffer(delq);
18004 : 6534 : destroyPQExpBuffer(extra);
18005 : 6534 : pg_free(qrelname);
18006 : 6534 : pg_free(qualrelname);
18007 : 6534 : }
18008 : :
18009 : : /*
18010 : : * dumpTableAttach
18011 : : * write to fout the commands to attach a child partition
18012 : : *
18013 : : * Child partitions are always made by creating them separately
18014 : : * and then using ATTACH PARTITION, rather than using
18015 : : * CREATE TABLE ... PARTITION OF. This is important for preserving
18016 : : * any possible discrepancy in column layout, to allow assigning the
18017 : : * correct tablespace if different, and so that it's possible to restore
18018 : : * a partition without restoring its parent. (You'll get an error from
18019 : : * the ATTACH PARTITION command, but that can be ignored, or skipped
18020 : : * using "pg_restore -L" if you prefer.) The last point motivates
18021 : : * treating ATTACH PARTITION as a completely separate ArchiveEntry
18022 : : * rather than emitting it within the child partition's ArchiveEntry.
18023 : : */
18024 : : static void
18025 : 1457 : dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo)
18026 : : {
18027 : 1457 : DumpOptions *dopt = fout->dopt;
18028 : : PQExpBuffer q;
18029 : : PGresult *res;
18030 : : char *partbound;
18031 : :
18032 : : /* Do nothing if not dumping schema */
18033 [ + + ]: 1457 : if (!dopt->dumpSchema)
18034 : 57 : return;
18035 : :
18036 : 1400 : q = createPQExpBuffer();
18037 : :
18038 [ + + ]: 1400 : if (!fout->is_prepared[PREPQUERY_DUMPTABLEATTACH])
18039 : : {
18040 : : /* Set up query for partbound details */
18041 : 45 : appendPQExpBufferStr(q,
18042 : : "PREPARE dumpTableAttach(pg_catalog.oid) AS\n");
18043 : :
18044 : 45 : appendPQExpBufferStr(q,
18045 : : "SELECT pg_get_expr(c.relpartbound, c.oid) "
18046 : : "FROM pg_class c "
18047 : : "WHERE c.oid = $1");
18048 : :
18049 : 45 : ExecuteSqlStatement(fout, q->data);
18050 : :
18051 : 45 : fout->is_prepared[PREPQUERY_DUMPTABLEATTACH] = true;
18052 : : }
18053 : :
18054 : 1400 : printfPQExpBuffer(q,
18055 : : "EXECUTE dumpTableAttach('%u')",
18056 : 1400 : attachinfo->partitionTbl->dobj.catId.oid);
18057 : :
18058 : 1400 : res = ExecuteSqlQueryForSingleRow(fout, q->data);
18059 : 1400 : partbound = PQgetvalue(res, 0, 0);
18060 : :
18061 : : /* Perform ALTER TABLE on the parent */
18062 : 1400 : printfPQExpBuffer(q,
18063 : : "ALTER TABLE ONLY %s ",
18064 : 1400 : fmtQualifiedDumpable(attachinfo->parentTbl));
18065 : 1400 : appendPQExpBuffer(q,
18066 : : "ATTACH PARTITION %s %s;\n",
18067 : 1400 : fmtQualifiedDumpable(attachinfo->partitionTbl),
18068 : : partbound);
18069 : :
18070 : : /*
18071 : : * There is no point in creating a drop query as the drop is done by table
18072 : : * drop. (If you think to change this, see also _printTocEntry().)
18073 : : * Although this object doesn't really have ownership as such, set the
18074 : : * owner field anyway to ensure that the command is run by the correct
18075 : : * role at restore time.
18076 : : */
18077 : 1400 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18078 : 1400 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18079 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18080 : : .owner = attachinfo->partitionTbl->rolname,
18081 : : .description = "TABLE ATTACH",
18082 : : .section = SECTION_PRE_DATA,
18083 : : .createStmt = q->data));
18084 : :
18085 : 1400 : PQclear(res);
18086 : 1400 : destroyPQExpBuffer(q);
18087 : : }
18088 : :
18089 : : /*
18090 : : * dumpAttrDef --- dump an attribute's default-value declaration
18091 : : */
18092 : : static void
18093 : 1111 : dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo)
18094 : : {
18095 : 1111 : DumpOptions *dopt = fout->dopt;
18096 : 1111 : TableInfo *tbinfo = adinfo->adtable;
18097 : 1111 : int adnum = adinfo->adnum;
18098 : : PQExpBuffer q;
18099 : : PQExpBuffer delq;
18100 : : char *qualrelname;
18101 : : char *tag;
18102 : : char *foreign;
18103 : :
18104 : : /* Do nothing if not dumping schema */
18105 [ - + ]: 1111 : if (!dopt->dumpSchema)
18106 : 0 : return;
18107 : :
18108 : : /* Skip if not "separate"; it was dumped in the table's definition */
18109 [ + + ]: 1111 : if (!adinfo->separate)
18110 : 939 : return;
18111 : :
18112 : 172 : q = createPQExpBuffer();
18113 : 172 : delq = createPQExpBuffer();
18114 : :
18115 : 172 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
18116 : :
18117 [ - + ]: 172 : foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18118 : :
18119 : 172 : appendPQExpBuffer(q,
18120 : : "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
18121 : 172 : foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
18122 : 172 : adinfo->adef_expr);
18123 : :
18124 : 172 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
18125 : : foreign, qualrelname,
18126 : 172 : fmtId(tbinfo->attnames[adnum - 1]));
18127 : :
18128 : 172 : tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
18129 : :
18130 [ + - ]: 172 : if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18131 : 172 : ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
18132 : 172 : ARCHIVE_OPTS(.tag = tag,
18133 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18134 : : .owner = tbinfo->rolname,
18135 : : .description = "DEFAULT",
18136 : : .section = SECTION_PRE_DATA,
18137 : : .createStmt = q->data,
18138 : : .dropStmt = delq->data));
18139 : :
18140 : 172 : pfree(tag);
18141 : 172 : destroyPQExpBuffer(q);
18142 : 172 : destroyPQExpBuffer(delq);
18143 : 172 : pg_free(qualrelname);
18144 : : }
18145 : :
18146 : : /*
18147 : : * getAttrName: extract the correct name for an attribute
18148 : : *
18149 : : * The array tblInfo->attnames[] only provides names of user attributes;
18150 : : * if a system attribute number is supplied, we have to fake it.
18151 : : * We also do a little bit of bounds checking for safety's sake.
18152 : : */
18153 : : static const char *
18154 : 2098 : getAttrName(int attrnum, const TableInfo *tblInfo)
18155 : : {
18156 [ + - + - ]: 2098 : if (attrnum > 0 && attrnum <= tblInfo->numatts)
18157 : 2098 : return tblInfo->attnames[attrnum - 1];
18158 [ # # # # : 0 : switch (attrnum)
# # # ]
18159 : : {
18160 : 0 : case SelfItemPointerAttributeNumber:
18161 : 0 : return "ctid";
18162 : 0 : case MinTransactionIdAttributeNumber:
18163 : 0 : return "xmin";
18164 : 0 : case MinCommandIdAttributeNumber:
18165 : 0 : return "cmin";
18166 : 0 : case MaxTransactionIdAttributeNumber:
18167 : 0 : return "xmax";
18168 : 0 : case MaxCommandIdAttributeNumber:
18169 : 0 : return "cmax";
18170 : 0 : case TableOidAttributeNumber:
18171 : 0 : return "tableoid";
18172 : : }
18173 : 0 : pg_fatal("invalid column number %d for table \"%s\"",
18174 : : attrnum, tblInfo->dobj.name);
18175 : : return NULL; /* keep compiler quiet */
18176 : : }
18177 : :
18178 : : /*
18179 : : * dumpIndex
18180 : : * write out to fout a user-defined index
18181 : : */
18182 : : static void
18183 : 2729 : dumpIndex(Archive *fout, const IndxInfo *indxinfo)
18184 : : {
18185 : 2729 : DumpOptions *dopt = fout->dopt;
18186 : 2729 : TableInfo *tbinfo = indxinfo->indextable;
18187 : 2729 : bool is_constraint = (indxinfo->indexconstraint != 0);
18188 : : PQExpBuffer q;
18189 : : PQExpBuffer delq;
18190 : : char *qindxname;
18191 : : char *qqindxname;
18192 : :
18193 : : /* Do nothing if not dumping schema */
18194 [ + + ]: 2729 : if (!dopt->dumpSchema)
18195 : 128 : return;
18196 : :
18197 : 2601 : q = createPQExpBuffer();
18198 : 2601 : delq = createPQExpBuffer();
18199 : :
18200 : 2601 : qindxname = pg_strdup(fmtId(indxinfo->dobj.name));
18201 : 2601 : qqindxname = pg_strdup(fmtQualifiedDumpable(indxinfo));
18202 : :
18203 : : /*
18204 : : * If there's an associated constraint, don't dump the index per se, but
18205 : : * do dump any comment for it. (This is safe because dependency ordering
18206 : : * will have ensured the constraint is emitted first.) Note that the
18207 : : * emitted comment has to be shown as depending on the constraint, not the
18208 : : * index, in such cases.
18209 : : */
18210 [ + + ]: 2601 : if (!is_constraint)
18211 : : {
18212 : 1124 : char *indstatcols = indxinfo->indstatcols;
18213 : 1124 : char *indstatvals = indxinfo->indstatvals;
18214 : 1124 : char **indstatcolsarray = NULL;
18215 : 1124 : char **indstatvalsarray = NULL;
18216 : 1124 : int nstatcols = 0;
18217 : 1124 : int nstatvals = 0;
18218 : :
18219 [ + + ]: 1124 : if (dopt->binary_upgrade)
18220 : 171 : binary_upgrade_set_pg_class_oids(fout, q,
18221 : 171 : indxinfo->dobj.catId.oid);
18222 : :
18223 : : /* Plain secondary index */
18224 : 1124 : appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
18225 : :
18226 : : /*
18227 : : * Append ALTER TABLE commands as needed to set properties that we
18228 : : * only have ALTER TABLE syntax for. Keep this in sync with the
18229 : : * similar code in dumpConstraint!
18230 : : */
18231 : :
18232 : : /* If the index is clustered, we need to record that. */
18233 [ + + ]: 1124 : if (indxinfo->indisclustered)
18234 : : {
18235 : 5 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
18236 : 5 : fmtQualifiedDumpable(tbinfo));
18237 : : /* index name is not qualified in this syntax */
18238 : 5 : appendPQExpBuffer(q, " ON %s;\n",
18239 : : qindxname);
18240 : : }
18241 : :
18242 : : /*
18243 : : * If the index has any statistics on some of its columns, generate
18244 : : * the associated ALTER INDEX queries.
18245 : : */
18246 [ + + - + ]: 1124 : if (strlen(indstatcols) != 0 || strlen(indstatvals) != 0)
18247 : : {
18248 : : int j;
18249 : :
18250 [ - + ]: 34 : if (!parsePGArray(indstatcols, &indstatcolsarray, &nstatcols))
18251 : 0 : pg_fatal("could not parse index statistic columns");
18252 [ - + ]: 34 : if (!parsePGArray(indstatvals, &indstatvalsarray, &nstatvals))
18253 : 0 : pg_fatal("could not parse index statistic values");
18254 [ - + ]: 34 : if (nstatcols != nstatvals)
18255 : 0 : pg_fatal("mismatched number of columns and values for index statistics");
18256 : :
18257 [ + + ]: 102 : for (j = 0; j < nstatcols; j++)
18258 : : {
18259 : 68 : appendPQExpBuffer(q, "ALTER INDEX %s ", qqindxname);
18260 : :
18261 : : /*
18262 : : * Note that this is a column number, so no quotes should be
18263 : : * used.
18264 : : */
18265 : 68 : appendPQExpBuffer(q, "ALTER COLUMN %s ",
18266 : 68 : indstatcolsarray[j]);
18267 : 68 : appendPQExpBuffer(q, "SET STATISTICS %s;\n",
18268 : 68 : indstatvalsarray[j]);
18269 : : }
18270 : : }
18271 : :
18272 : : /* Indexes can depend on extensions */
18273 : 1124 : append_depends_on_extension(fout, q, &indxinfo->dobj,
18274 : : "pg_catalog.pg_class",
18275 : : "INDEX", qqindxname);
18276 : :
18277 : : /* If the index defines identity, we need to record that. */
18278 [ - + ]: 1124 : if (indxinfo->indisreplident)
18279 : : {
18280 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
18281 : 0 : fmtQualifiedDumpable(tbinfo));
18282 : : /* index name is not qualified in this syntax */
18283 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
18284 : : qindxname);
18285 : : }
18286 : :
18287 : : /*
18288 : : * If this index is a member of a partitioned index, the backend will
18289 : : * not allow us to drop it separately, so don't try. It will go away
18290 : : * automatically when we drop either the index's table or the
18291 : : * partitioned index. (If, in a selective restore with --clean, we
18292 : : * drop neither of those, then this index will not be dropped either.
18293 : : * But that's fine, and even if you think it's not, the backend won't
18294 : : * let us do differently.)
18295 : : */
18296 [ + + ]: 1124 : if (indxinfo->parentidx == 0)
18297 : 904 : appendPQExpBuffer(delq, "DROP INDEX %s;\n", qqindxname);
18298 : :
18299 [ + - ]: 1124 : if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18300 : 1124 : ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
18301 : 1124 : ARCHIVE_OPTS(.tag = indxinfo->dobj.name,
18302 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18303 : : .tablespace = indxinfo->tablespace,
18304 : : .owner = tbinfo->rolname,
18305 : : .description = "INDEX",
18306 : : .section = SECTION_POST_DATA,
18307 : : .createStmt = q->data,
18308 : : .dropStmt = delq->data));
18309 : :
18310 : 1124 : free(indstatcolsarray);
18311 : 1124 : free(indstatvalsarray);
18312 : : }
18313 : :
18314 : : /* Dump Index Comments */
18315 [ + + ]: 2601 : if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18316 [ + + ]: 15 : dumpComment(fout, "INDEX", qindxname,
18317 : 15 : tbinfo->dobj.namespace->dobj.name,
18318 : : tbinfo->rolname,
18319 : : indxinfo->dobj.catId, 0,
18320 : : is_constraint ? indxinfo->indexconstraint :
18321 : : indxinfo->dobj.dumpId);
18322 : :
18323 : 2601 : destroyPQExpBuffer(q);
18324 : 2601 : destroyPQExpBuffer(delq);
18325 : 2601 : pg_free(qindxname);
18326 : 2601 : pg_free(qqindxname);
18327 : : }
18328 : :
18329 : : /*
18330 : : * dumpIndexAttach
18331 : : * write out to fout a partitioned-index attachment clause
18332 : : */
18333 : : static void
18334 : 625 : dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo)
18335 : : {
18336 : : /* Do nothing if not dumping schema */
18337 [ + + ]: 625 : if (!fout->dopt->dumpSchema)
18338 : 48 : return;
18339 : :
18340 [ + - ]: 577 : if (attachinfo->partitionIdx->dobj.dump & DUMP_COMPONENT_DEFINITION)
18341 : : {
18342 : 577 : PQExpBuffer q = createPQExpBuffer();
18343 : :
18344 : 577 : appendPQExpBuffer(q, "ALTER INDEX %s ",
18345 : 577 : fmtQualifiedDumpable(attachinfo->parentIdx));
18346 : 577 : appendPQExpBuffer(q, "ATTACH PARTITION %s;\n",
18347 : 577 : fmtQualifiedDumpable(attachinfo->partitionIdx));
18348 : :
18349 : : /*
18350 : : * There is no need for a dropStmt since the drop is done implicitly
18351 : : * when we drop either the index's table or the partitioned index.
18352 : : * Moreover, since there's no ALTER INDEX DETACH PARTITION command,
18353 : : * there's no way to do it anyway. (If you think to change this,
18354 : : * consider also what to do with --if-exists.)
18355 : : *
18356 : : * Although this object doesn't really have ownership as such, set the
18357 : : * owner field anyway to ensure that the command is run by the correct
18358 : : * role at restore time.
18359 : : */
18360 : 577 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18361 : 577 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18362 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18363 : : .owner = attachinfo->parentIdx->indextable->rolname,
18364 : : .description = "INDEX ATTACH",
18365 : : .section = SECTION_POST_DATA,
18366 : : .createStmt = q->data));
18367 : :
18368 : 577 : destroyPQExpBuffer(q);
18369 : : }
18370 : : }
18371 : :
18372 : : /*
18373 : : * dumpStatisticsExt
18374 : : * write out to fout an extended statistics object
18375 : : */
18376 : : static void
18377 : 183 : dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo)
18378 : : {
18379 : 183 : DumpOptions *dopt = fout->dopt;
18380 : : PQExpBuffer q;
18381 : : PQExpBuffer delq;
18382 : : PQExpBuffer query;
18383 : : char *qstatsextname;
18384 : : PGresult *res;
18385 : : char *stxdef;
18386 : :
18387 : : /* Do nothing if not dumping schema */
18388 [ + + ]: 183 : if (!dopt->dumpSchema)
18389 : 28 : return;
18390 : :
18391 : 155 : q = createPQExpBuffer();
18392 : 155 : delq = createPQExpBuffer();
18393 : 155 : query = createPQExpBuffer();
18394 : :
18395 : 155 : qstatsextname = pg_strdup(fmtId(statsextinfo->dobj.name));
18396 : :
18397 : 155 : appendPQExpBuffer(query, "SELECT "
18398 : : "pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)",
18399 : 155 : statsextinfo->dobj.catId.oid);
18400 : :
18401 : 155 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
18402 : :
18403 : 155 : stxdef = PQgetvalue(res, 0, 0);
18404 : :
18405 : : /* Result of pg_get_statisticsobjdef is complete except for semicolon */
18406 : 155 : appendPQExpBuffer(q, "%s;\n", stxdef);
18407 : :
18408 : : /*
18409 : : * We only issue an ALTER STATISTICS statement if the stxstattarget entry
18410 : : * for this statistics object is not the default value.
18411 : : */
18412 [ + + ]: 155 : if (statsextinfo->stattarget >= 0)
18413 : : {
18414 : 34 : appendPQExpBuffer(q, "ALTER STATISTICS %s ",
18415 : 34 : fmtQualifiedDumpable(statsextinfo));
18416 : 34 : appendPQExpBuffer(q, "SET STATISTICS %d;\n",
18417 : 34 : statsextinfo->stattarget);
18418 : : }
18419 : :
18420 : 155 : appendPQExpBuffer(delq, "DROP STATISTICS %s;\n",
18421 : 155 : fmtQualifiedDumpable(statsextinfo));
18422 : :
18423 [ + - ]: 155 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18424 : 155 : ArchiveEntry(fout, statsextinfo->dobj.catId,
18425 : 155 : statsextinfo->dobj.dumpId,
18426 : 155 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18427 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18428 : : .owner = statsextinfo->rolname,
18429 : : .description = "STATISTICS",
18430 : : .section = SECTION_POST_DATA,
18431 : : .createStmt = q->data,
18432 : : .dropStmt = delq->data));
18433 : :
18434 : : /* Dump Statistics Comments */
18435 [ - + ]: 155 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18436 : 0 : dumpComment(fout, "STATISTICS", qstatsextname,
18437 : 0 : statsextinfo->dobj.namespace->dobj.name,
18438 : 0 : statsextinfo->rolname,
18439 : : statsextinfo->dobj.catId, 0,
18440 : 0 : statsextinfo->dobj.dumpId);
18441 : :
18442 : 155 : PQclear(res);
18443 : 155 : destroyPQExpBuffer(q);
18444 : 155 : destroyPQExpBuffer(delq);
18445 : 155 : destroyPQExpBuffer(query);
18446 : 155 : pg_free(qstatsextname);
18447 : : }
18448 : :
18449 : : /*
18450 : : * dumpStatisticsExtStats
18451 : : * write out to fout the stats for an extended statistics object
18452 : : */
18453 : : static void
18454 : 183 : dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo)
18455 : : {
18456 : 183 : DumpOptions *dopt = fout->dopt;
18457 : : PQExpBuffer query;
18458 : : PGresult *res;
18459 : : int nstats;
18460 : :
18461 : : /* Do nothing if not dumping statistics */
18462 [ + + ]: 183 : if (!dopt->dumpStatistics)
18463 : 40 : return;
18464 : :
18465 [ + + ]: 143 : if (!fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS])
18466 : : {
18467 : 36 : PQExpBuffer pq = createPQExpBuffer();
18468 : :
18469 : : /*---------
18470 : : * Set up query for details about extended statistics objects.
18471 : : *
18472 : : * The query depends on the backend version:
18473 : : * - In v19 and newer versions, query directly the pg_stats_ext*
18474 : : * catalogs.
18475 : : * - In v18 and older versions, ndistinct and dependencies have a
18476 : : * different format that needs translation.
18477 : : * - In v14 and older versions, inherited does not exist.
18478 : : * - In v11 and older versions, there is no pg_stats_ext, hence
18479 : : * the logic joins pg_statistic_ext and pg_namespace.
18480 : : *---------
18481 : : */
18482 : :
18483 : 36 : appendPQExpBufferStr(pq,
18484 : : "PREPARE getExtStatsStats(pg_catalog.name, pg_catalog.name) AS\n"
18485 : : "SELECT ");
18486 : :
18487 : : /*
18488 : : * Versions 15 and newer have inherited stats.
18489 : : *
18490 : : * Create this column in all versions because we need to order by it
18491 : : * later.
18492 : : */
18493 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18494 : 36 : appendPQExpBufferStr(pq, "e.inherited, ");
18495 : : else
18496 : 0 : appendPQExpBufferStr(pq, "false AS inherited, ");
18497 : :
18498 : : /*--------
18499 : : * The ndistinct and dependencies formats changed in v19, so
18500 : : * everything before that needs to be translated.
18501 : : *
18502 : : * The ndistinct translation converts this kind of data:
18503 : : * {"3, 4": 11, "3, 6": 11, "4, 6": 11, "3, 4, 6": 11}
18504 : : *
18505 : : * to this:
18506 : : * [ {"attributes": [3,4], "ndistinct": 11},
18507 : : * {"attributes": [3,6], "ndistinct": 11},
18508 : : * {"attributes": [4,6], "ndistinct": 11},
18509 : : * {"attributes": [3,4,6], "ndistinct": 11} ]
18510 : : *
18511 : : * The dependencies translation converts this kind of data:
18512 : : * {"3 => 4": 1.000000, "3 => 6": 1.000000,
18513 : : * "4 => 6": 1.000000, "3, 4 => 6": 1.000000,
18514 : : * "3, 6 => 4": 1.000000}
18515 : : *
18516 : : * to this:
18517 : : * [ {"attributes": [3], "dependency": 4, "degree": 1.000000},
18518 : : * {"attributes": [3], "dependency": 6, "degree": 1.000000},
18519 : : * {"attributes": [4], "dependency": 6, "degree": 1.000000},
18520 : : * {"attributes": [3,4], "dependency": 6, "degree": 1.000000},
18521 : : * {"attributes": [3,6], "dependency": 4, "degree": 1.000000} ]
18522 : : *--------
18523 : : */
18524 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18525 : 36 : appendPQExpBufferStr(pq, "e.n_distinct, e.dependencies, ");
18526 : : else
18527 : 0 : appendPQExpBufferStr(pq,
18528 : : "( "
18529 : : "SELECT json_agg( "
18530 : : " json_build_object( "
18531 : : " '" PG_NDISTINCT_KEY_ATTRIBUTES "', "
18532 : : " string_to_array(kv.key, ', ')::integer[], "
18533 : : " '" PG_NDISTINCT_KEY_NDISTINCT "', "
18534 : : " kv.value::bigint )) "
18535 : : "FROM json_each_text(e.n_distinct::text::json) AS kv"
18536 : : ") AS n_distinct, "
18537 : : "( "
18538 : : "SELECT json_agg( "
18539 : : " json_build_object( "
18540 : : " '" PG_DEPENDENCIES_KEY_ATTRIBUTES "', "
18541 : : " string_to_array( "
18542 : : " split_part(kv.key, ' => ', 1), "
18543 : : " ', ')::integer[], "
18544 : : " '" PG_DEPENDENCIES_KEY_DEPENDENCY "', "
18545 : : " split_part(kv.key, ' => ', 2)::integer, "
18546 : : " '" PG_DEPENDENCIES_KEY_DEGREE "', "
18547 : : " kv.value::double precision )) "
18548 : : "FROM json_each_text(e.dependencies::text::json) AS kv "
18549 : : ") AS dependencies, ");
18550 : :
18551 : : /* MCV was introduced v13 */
18552 [ + - ]: 36 : if (fout->remoteVersion >= 130000)
18553 : 36 : appendPQExpBufferStr(pq,
18554 : : "e.most_common_vals, e.most_common_freqs, "
18555 : : "e.most_common_base_freqs, ");
18556 : : else
18557 : 0 : appendPQExpBufferStr(pq,
18558 : : "NULL AS most_common_vals, NULL AS most_common_freqs, "
18559 : : "NULL AS most_common_base_freqs, ");
18560 : :
18561 : : /* Expressions were introduced in v14 */
18562 [ + - ]: 36 : if (fout->remoteVersion >= 140000)
18563 : : {
18564 : : /*
18565 : : * There is no ordering column in pg_stats_ext_exprs. However, we
18566 : : * can rely on the unnesting of pg_statistic_ext_data.stxdexpr to
18567 : : * maintain the desired order of expression elements.
18568 : : */
18569 : 36 : appendPQExpBufferStr(pq,
18570 : : "( "
18571 : : "SELECT jsonb_pretty(jsonb_agg("
18572 : : "nullif(j.obj, '{}'::jsonb))) "
18573 : : "FROM pg_stats_ext_exprs AS ee "
18574 : : "CROSS JOIN LATERAL jsonb_strip_nulls("
18575 : : " jsonb_build_object( "
18576 : : " 'null_frac', ee.null_frac::text, "
18577 : : " 'avg_width', ee.avg_width::text, "
18578 : : " 'n_distinct', ee.n_distinct::text, "
18579 : : " 'most_common_vals', ee.most_common_vals::text, "
18580 : : " 'most_common_freqs', ee.most_common_freqs::text, "
18581 : : " 'histogram_bounds', ee.histogram_bounds::text, "
18582 : : " 'correlation', ee.correlation::text, "
18583 : : " 'most_common_elems', ee.most_common_elems::text, "
18584 : : " 'most_common_elem_freqs', ee.most_common_elem_freqs::text, "
18585 : : " 'elem_count_histogram', ee.elem_count_histogram::text");
18586 : :
18587 : : /* These three have been added to pg_stats_ext_exprs in v19. */
18588 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18589 : 36 : appendPQExpBufferStr(pq,
18590 : : ", "
18591 : : " 'range_length_histogram', ee.range_length_histogram::text, "
18592 : : " 'range_empty_frac', ee.range_empty_frac::text, "
18593 : : " 'range_bounds_histogram', ee.range_bounds_histogram::text");
18594 : :
18595 : 36 : appendPQExpBufferStr(pq,
18596 : : " )) AS j(obj)"
18597 : : "WHERE ee.statistics_schemaname = $1 "
18598 : : "AND ee.statistics_name = $2 ");
18599 : : /* Inherited expressions introduced in v15 */
18600 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18601 : 36 : appendPQExpBufferStr(pq, "AND ee.inherited = e.inherited");
18602 : :
18603 : 36 : appendPQExpBufferStr(pq, ") AS exprs ");
18604 : : }
18605 : : else
18606 : 0 : appendPQExpBufferStr(pq, "NULL AS exprs ");
18607 : :
18608 : : /* pg_stats_ext introduced in v12 */
18609 [ + - ]: 36 : if (fout->remoteVersion >= 120000)
18610 : 36 : appendPQExpBufferStr(pq,
18611 : : "FROM pg_catalog.pg_stats_ext AS e "
18612 : : "WHERE e.statistics_schemaname = $1 "
18613 : : "AND e.statistics_name = $2 ");
18614 : : else
18615 : 0 : appendPQExpBufferStr(pq,
18616 : : "FROM ( "
18617 : : "SELECT s.stxndistinct AS n_distinct, "
18618 : : " s.stxdependencies AS dependencies "
18619 : : "FROM pg_catalog.pg_statistic_ext AS s "
18620 : : "JOIN pg_catalog.pg_namespace AS n "
18621 : : "ON n.oid = s.stxnamespace "
18622 : : "WHERE n.nspname = $1 "
18623 : : "AND s.stxname = $2 "
18624 : : ") AS e ");
18625 : :
18626 : : /* we always have an inherited column, but it may be a constant */
18627 : 36 : appendPQExpBufferStr(pq, "ORDER BY inherited");
18628 : :
18629 : 36 : ExecuteSqlStatement(fout, pq->data);
18630 : :
18631 : 36 : fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS] = true;
18632 : :
18633 : 36 : destroyPQExpBuffer(pq);
18634 : : }
18635 : :
18636 : 143 : query = createPQExpBuffer();
18637 : :
18638 : 143 : appendPQExpBufferStr(query, "EXECUTE getExtStatsStats(");
18639 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.namespace->dobj.name, fout);
18640 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name, ");
18641 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.name, fout);
18642 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name)");
18643 : :
18644 : 143 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
18645 : :
18646 : 143 : destroyPQExpBuffer(query);
18647 : :
18648 : 143 : nstats = PQntuples(res);
18649 : :
18650 [ + + ]: 143 : if (nstats > 0)
18651 : : {
18652 : 39 : PQExpBuffer out = createPQExpBuffer();
18653 : :
18654 : 39 : int i_inherited = PQfnumber(res, "inherited");
18655 : 39 : int i_ndistinct = PQfnumber(res, "n_distinct");
18656 : 39 : int i_dependencies = PQfnumber(res, "dependencies");
18657 : 39 : int i_mcv = PQfnumber(res, "most_common_vals");
18658 : 39 : int i_mcf = PQfnumber(res, "most_common_freqs");
18659 : 39 : int i_mcbf = PQfnumber(res, "most_common_base_freqs");
18660 : 39 : int i_exprs = PQfnumber(res, "exprs");
18661 : :
18662 [ + + ]: 78 : for (int i = 0; i < nstats; i++)
18663 : : {
18664 : 39 : TableInfo *tbinfo = statsextinfo->stattable;
18665 : :
18666 [ - + ]: 39 : if (PQgetisnull(res, i, i_inherited))
18667 : 0 : pg_fatal("inherited cannot be NULL");
18668 : :
18669 : 39 : appendPQExpBufferStr(out,
18670 : : "SELECT * FROM pg_catalog.pg_restore_extended_stats(\n");
18671 : 39 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
18672 : : fout->remoteVersion);
18673 : :
18674 : : /* Relation information */
18675 : 39 : appendPQExpBufferStr(out, "\t'schemaname', ");
18676 : 39 : appendStringLiteralAH(out, tbinfo->dobj.namespace->dobj.name, fout);
18677 : 39 : appendPQExpBufferStr(out, ",\n\t'relname', ");
18678 : 39 : appendStringLiteralAH(out, tbinfo->dobj.name, fout);
18679 : :
18680 : : /* Extended statistics information */
18681 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_schemaname', ");
18682 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.namespace->dobj.name, fout);
18683 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_name', ");
18684 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.name, fout);
18685 : 39 : appendNamedArgument(out, fout, "inherited", "boolean",
18686 : 39 : PQgetvalue(res, i, i_inherited));
18687 : :
18688 [ + + ]: 39 : if (!PQgetisnull(res, i, i_ndistinct))
18689 : 35 : appendNamedArgument(out, fout, "n_distinct", "pg_ndistinct",
18690 : 35 : PQgetvalue(res, i, i_ndistinct));
18691 : :
18692 [ + + ]: 39 : if (!PQgetisnull(res, i, i_dependencies))
18693 : 36 : appendNamedArgument(out, fout, "dependencies", "pg_dependencies",
18694 : 36 : PQgetvalue(res, i, i_dependencies));
18695 : :
18696 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcv))
18697 : 38 : appendNamedArgument(out, fout, "most_common_vals", "text[]",
18698 : 38 : PQgetvalue(res, i, i_mcv));
18699 : :
18700 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcf))
18701 : 38 : appendNamedArgument(out, fout, "most_common_freqs", "double precision[]",
18702 : 38 : PQgetvalue(res, i, i_mcf));
18703 : :
18704 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcbf))
18705 : 38 : appendNamedArgument(out, fout, "most_common_base_freqs", "double precision[]",
18706 : 38 : PQgetvalue(res, i, i_mcbf));
18707 : :
18708 [ + + ]: 39 : if (!PQgetisnull(res, i, i_exprs))
18709 : 36 : appendNamedArgument(out, fout, "exprs", "jsonb",
18710 : 36 : PQgetvalue(res, i, i_exprs));
18711 : :
18712 : 39 : appendPQExpBufferStr(out, "\n);\n");
18713 : : }
18714 : :
18715 : 39 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
18716 : 39 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18717 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18718 : : .owner = statsextinfo->rolname,
18719 : : .description = "EXTENDED STATISTICS DATA",
18720 : : .section = SECTION_POST_DATA,
18721 : : .createStmt = out->data,
18722 : : .deps = &statsextinfo->dobj.dumpId,
18723 : : .nDeps = 1));
18724 : 39 : destroyPQExpBuffer(out);
18725 : : }
18726 : 143 : PQclear(res);
18727 : : }
18728 : :
18729 : : /*
18730 : : * dumpConstraint
18731 : : * write out to fout a user-defined constraint
18732 : : */
18733 : : static void
18734 : 2580 : dumpConstraint(Archive *fout, const ConstraintInfo *coninfo)
18735 : : {
18736 : 2580 : DumpOptions *dopt = fout->dopt;
18737 : 2580 : TableInfo *tbinfo = coninfo->contable;
18738 : : PQExpBuffer q;
18739 : : PQExpBuffer delq;
18740 : 2580 : char *tag = NULL;
18741 : : char *foreign;
18742 : :
18743 : : /* Do nothing if not dumping schema */
18744 [ + + ]: 2580 : if (!dopt->dumpSchema)
18745 : 110 : return;
18746 : :
18747 : 2470 : q = createPQExpBuffer();
18748 : 2470 : delq = createPQExpBuffer();
18749 : :
18750 : 4772 : foreign = tbinfo &&
18751 [ + + - + ]: 2470 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18752 : :
18753 [ + + ]: 2470 : if (coninfo->contype == 'p' ||
18754 [ + + ]: 1244 : coninfo->contype == 'u' ||
18755 [ + + ]: 1013 : coninfo->contype == 'x')
18756 : 1477 : {
18757 : : /* Index-related constraint */
18758 : : IndxInfo *indxinfo;
18759 : : int k;
18760 : :
18761 : 1477 : indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
18762 : :
18763 [ - + ]: 1477 : if (indxinfo == NULL)
18764 : 0 : pg_fatal("missing index for constraint \"%s\"",
18765 : : coninfo->dobj.name);
18766 : :
18767 [ + + ]: 1477 : if (dopt->binary_upgrade)
18768 : 153 : binary_upgrade_set_pg_class_oids(fout, q,
18769 : : indxinfo->dobj.catId.oid);
18770 : :
18771 : 1477 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
18772 : 1477 : fmtQualifiedDumpable(tbinfo));
18773 : 1477 : appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
18774 : 1477 : fmtId(coninfo->dobj.name));
18775 : :
18776 [ + + ]: 1477 : if (coninfo->condef)
18777 : : {
18778 : : /* pg_get_constraintdef should have provided everything */
18779 : 20 : appendPQExpBuffer(q, "%s;\n", coninfo->condef);
18780 : : }
18781 : : else
18782 : : {
18783 : 1457 : appendPQExpBufferStr(q,
18784 [ + + ]: 1457 : coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
18785 : :
18786 : : /*
18787 : : * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
18788 : : * indexes. Being able to create this was fixed, but we need to
18789 : : * make the index distinct in order to be able to restore the
18790 : : * dump.
18791 : : */
18792 [ - + - - ]: 1457 : if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
18793 : 0 : appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
18794 : 1457 : appendPQExpBufferStr(q, " (");
18795 [ + + ]: 3515 : for (k = 0; k < indxinfo->indnkeyattrs; k++)
18796 : : {
18797 : 2058 : int indkey = indxinfo->indkeys[k];
18798 : : const char *attname;
18799 : :
18800 [ - + ]: 2058 : if (indkey == InvalidAttrNumber)
18801 : 0 : break;
18802 : 2058 : attname = getAttrName(indkey, tbinfo);
18803 : :
18804 [ + + ]: 2058 : appendPQExpBuffer(q, "%s%s",
18805 : : (k == 0) ? "" : ", ",
18806 : : fmtId(attname));
18807 : : }
18808 [ + + ]: 1457 : if (coninfo->conperiod)
18809 : 108 : appendPQExpBufferStr(q, " WITHOUT OVERLAPS");
18810 : :
18811 [ + + ]: 1457 : if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
18812 : 20 : appendPQExpBufferStr(q, ") INCLUDE (");
18813 : :
18814 [ + + ]: 1497 : for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
18815 : : {
18816 : 40 : int indkey = indxinfo->indkeys[k];
18817 : : const char *attname;
18818 : :
18819 [ - + ]: 40 : if (indkey == InvalidAttrNumber)
18820 : 0 : break;
18821 : 40 : attname = getAttrName(indkey, tbinfo);
18822 : :
18823 : 80 : appendPQExpBuffer(q, "%s%s",
18824 [ + + ]: 40 : (k == indxinfo->indnkeyattrs) ? "" : ", ",
18825 : : fmtId(attname));
18826 : : }
18827 : :
18828 : 1457 : appendPQExpBufferChar(q, ')');
18829 : :
18830 [ - + ]: 1457 : if (nonemptyReloptions(indxinfo->indreloptions))
18831 : : {
18832 : 0 : appendPQExpBufferStr(q, " WITH (");
18833 : 0 : appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
18834 : 0 : appendPQExpBufferChar(q, ')');
18835 : : }
18836 : :
18837 [ + + ]: 1457 : if (coninfo->condeferrable)
18838 : : {
18839 : 25 : appendPQExpBufferStr(q, " DEFERRABLE");
18840 [ + + ]: 25 : if (coninfo->condeferred)
18841 : 15 : appendPQExpBufferStr(q, " INITIALLY DEFERRED");
18842 : : }
18843 : :
18844 : 1457 : appendPQExpBufferStr(q, ";\n");
18845 : : }
18846 : :
18847 : : /*
18848 : : * Append ALTER TABLE commands as needed to set properties that we
18849 : : * only have ALTER TABLE syntax for. Keep this in sync with the
18850 : : * similar code in dumpIndex!
18851 : : */
18852 : :
18853 : : /* If the index is clustered, we need to record that. */
18854 [ + + ]: 1477 : if (indxinfo->indisclustered)
18855 : : {
18856 : 34 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
18857 : 34 : fmtQualifiedDumpable(tbinfo));
18858 : : /* index name is not qualified in this syntax */
18859 : 34 : appendPQExpBuffer(q, " ON %s;\n",
18860 : 34 : fmtId(indxinfo->dobj.name));
18861 : : }
18862 : :
18863 : : /* If the index defines identity, we need to record that. */
18864 [ - + ]: 1477 : if (indxinfo->indisreplident)
18865 : : {
18866 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
18867 : 0 : fmtQualifiedDumpable(tbinfo));
18868 : : /* index name is not qualified in this syntax */
18869 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
18870 : 0 : fmtId(indxinfo->dobj.name));
18871 : : }
18872 : :
18873 : : /* Indexes can depend on extensions */
18874 : 1477 : append_depends_on_extension(fout, q, &indxinfo->dobj,
18875 : : "pg_catalog.pg_class", "INDEX",
18876 : 1477 : fmtQualifiedDumpable(indxinfo));
18877 : :
18878 : 1477 : appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
18879 : 1477 : fmtQualifiedDumpable(tbinfo));
18880 : 1477 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
18881 : 1477 : fmtId(coninfo->dobj.name));
18882 : :
18883 : 1477 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
18884 : :
18885 [ + - ]: 1477 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18886 : 1477 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
18887 : 1477 : ARCHIVE_OPTS(.tag = tag,
18888 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18889 : : .tablespace = indxinfo->tablespace,
18890 : : .owner = tbinfo->rolname,
18891 : : .description = "CONSTRAINT",
18892 : : .section = SECTION_POST_DATA,
18893 : : .createStmt = q->data,
18894 : : .dropStmt = delq->data));
18895 : : }
18896 [ + + ]: 993 : else if (coninfo->contype == 'f')
18897 : : {
18898 : : char *only;
18899 : :
18900 : : /*
18901 : : * Foreign keys on partitioned tables are always declared as
18902 : : * inheriting to partitions; for all other cases, emit them as
18903 : : * applying ONLY directly to the named table, because that's how they
18904 : : * work for regular inherited tables.
18905 : : */
18906 [ + + ]: 163 : only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
18907 : :
18908 : : /*
18909 : : * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
18910 : : * current table data is not processed
18911 : : */
18912 : 163 : appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
18913 : 163 : only, fmtQualifiedDumpable(tbinfo));
18914 : 163 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
18915 : 163 : fmtId(coninfo->dobj.name),
18916 : 163 : coninfo->condef);
18917 : :
18918 : 163 : appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
18919 : 163 : only, fmtQualifiedDumpable(tbinfo));
18920 : 163 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
18921 : 163 : fmtId(coninfo->dobj.name));
18922 : :
18923 : 163 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
18924 : :
18925 [ + - ]: 163 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18926 : 163 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
18927 : 163 : ARCHIVE_OPTS(.tag = tag,
18928 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18929 : : .owner = tbinfo->rolname,
18930 : : .description = "FK CONSTRAINT",
18931 : : .section = SECTION_POST_DATA,
18932 : : .createStmt = q->data,
18933 : : .dropStmt = delq->data));
18934 : : }
18935 [ + + + - : 830 : else if ((coninfo->contype == 'c' || coninfo->contype == 'n') && tbinfo)
+ + ]
18936 : : {
18937 : : /* CHECK or invalid not-null constraint on a table */
18938 : :
18939 : : /* Ignore if not to be dumped separately, or if it was inherited */
18940 [ + + + + ]: 662 : if (coninfo->separate && coninfo->conislocal)
18941 : : {
18942 : : const char *keyword;
18943 : :
18944 [ + + ]: 109 : if (coninfo->contype == 'c')
18945 : 45 : keyword = "CHECK CONSTRAINT";
18946 : : else
18947 : 64 : keyword = "CONSTRAINT";
18948 : :
18949 : : /* not ONLY since we want it to propagate to children */
18950 : 109 : appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
18951 : 109 : fmtQualifiedDumpable(tbinfo));
18952 : 109 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
18953 : 109 : fmtId(coninfo->dobj.name),
18954 : 109 : coninfo->condef);
18955 : :
18956 : 109 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
18957 : 109 : fmtQualifiedDumpable(tbinfo));
18958 : 109 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
18959 : 109 : fmtId(coninfo->dobj.name));
18960 : :
18961 : 109 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
18962 : :
18963 [ + - ]: 109 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18964 : 109 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
18965 : 109 : ARCHIVE_OPTS(.tag = tag,
18966 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18967 : : .owner = tbinfo->rolname,
18968 : : .description = keyword,
18969 : : .section = SECTION_POST_DATA,
18970 : : .createStmt = q->data,
18971 : : .dropStmt = delq->data));
18972 : : }
18973 : : }
18974 [ + - ]: 168 : else if (tbinfo == NULL)
18975 : : {
18976 : : /* CHECK, NOT NULL constraint on a domain */
18977 : 168 : TypeInfo *tyinfo = coninfo->condomain;
18978 : :
18979 : : Assert(coninfo->contype == 'c' || coninfo->contype == 'n');
18980 : :
18981 : : /* Ignore if not to be dumped separately */
18982 [ + + ]: 168 : if (coninfo->separate)
18983 : : {
18984 : : const char *keyword;
18985 : :
18986 [ + - ]: 5 : if (coninfo->contype == 'c')
18987 : 5 : keyword = "CHECK CONSTRAINT";
18988 : : else
18989 : 0 : keyword = "CONSTRAINT";
18990 : :
18991 : 5 : appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
18992 : 5 : fmtQualifiedDumpable(tyinfo));
18993 : 5 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
18994 : 5 : fmtId(coninfo->dobj.name),
18995 : 5 : coninfo->condef);
18996 : :
18997 : 5 : appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
18998 : 5 : fmtQualifiedDumpable(tyinfo));
18999 : 5 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19000 : 5 : fmtId(coninfo->dobj.name));
19001 : :
19002 : 5 : tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
19003 : :
19004 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19005 : 5 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19006 : 5 : ARCHIVE_OPTS(.tag = tag,
19007 : : .namespace = tyinfo->dobj.namespace->dobj.name,
19008 : : .owner = tyinfo->rolname,
19009 : : .description = keyword,
19010 : : .section = SECTION_POST_DATA,
19011 : : .createStmt = q->data,
19012 : : .dropStmt = delq->data));
19013 : :
19014 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19015 : : {
19016 : 5 : PQExpBuffer conprefix = createPQExpBuffer();
19017 : 5 : char *qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
19018 : :
19019 : 5 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
19020 : 5 : fmtId(coninfo->dobj.name));
19021 : :
19022 : 5 : dumpComment(fout, conprefix->data, qtypname,
19023 : 5 : tyinfo->dobj.namespace->dobj.name,
19024 : : tyinfo->rolname,
19025 : 5 : coninfo->dobj.catId, 0, coninfo->dobj.dumpId);
19026 : 5 : destroyPQExpBuffer(conprefix);
19027 : 5 : pg_free(qtypname);
19028 : : }
19029 : : }
19030 : : }
19031 : : else
19032 : : {
19033 : 0 : pg_fatal("unrecognized constraint type: %c",
19034 : : coninfo->contype);
19035 : : }
19036 : :
19037 : : /* Dump Constraint Comments --- only works for table constraints */
19038 [ + + + + ]: 2470 : if (tbinfo && coninfo->separate &&
19039 [ + + ]: 1779 : coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19040 : 49 : dumpTableConstraintComment(fout, coninfo);
19041 : :
19042 : 2470 : pfree(tag);
19043 : 2470 : destroyPQExpBuffer(q);
19044 : 2470 : destroyPQExpBuffer(delq);
19045 : : }
19046 : :
19047 : : /*
19048 : : * dumpTableConstraintComment --- dump a constraint's comment if any
19049 : : *
19050 : : * This is split out because we need the function in two different places
19051 : : * depending on whether the constraint is dumped as part of CREATE TABLE
19052 : : * or as a separate ALTER command.
19053 : : */
19054 : : static void
19055 : 88 : dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
19056 : : {
19057 : 88 : TableInfo *tbinfo = coninfo->contable;
19058 : 88 : PQExpBuffer conprefix = createPQExpBuffer();
19059 : : char *qtabname;
19060 : :
19061 : 88 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19062 : :
19063 : 88 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON",
19064 : 88 : fmtId(coninfo->dobj.name));
19065 : :
19066 [ + - ]: 88 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19067 : 88 : dumpComment(fout, conprefix->data, qtabname,
19068 : 88 : tbinfo->dobj.namespace->dobj.name,
19069 : : tbinfo->rolname,
19070 : : coninfo->dobj.catId, 0,
19071 [ + + ]: 88 : coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
19072 : :
19073 : 88 : destroyPQExpBuffer(conprefix);
19074 : 88 : pg_free(qtabname);
19075 : 88 : }
19076 : :
19077 : : static inline SeqType
19078 : 647 : parse_sequence_type(const char *name)
19079 : : {
19080 [ + - ]: 1443 : for (size_t i = 0; i < lengthof(SeqTypeNames); i++)
19081 : : {
19082 [ + + ]: 1443 : if (strcmp(SeqTypeNames[i], name) == 0)
19083 : 647 : return (SeqType) i;
19084 : : }
19085 : :
19086 : 0 : pg_fatal("unrecognized sequence type: %s", name);
19087 : : return (SeqType) 0; /* keep compiler quiet */
19088 : : }
19089 : :
19090 : : /*
19091 : : * bsearch() comparator for SequenceItem
19092 : : */
19093 : : static int
19094 : 2976 : SequenceItemCmp(const void *p1, const void *p2)
19095 : : {
19096 : 2976 : SequenceItem v1 = *((const SequenceItem *) p1);
19097 : 2976 : SequenceItem v2 = *((const SequenceItem *) p2);
19098 : :
19099 : 2976 : return pg_cmp_u32(v1.oid, v2.oid);
19100 : : }
19101 : :
19102 : : /*
19103 : : * collectSequences
19104 : : *
19105 : : * Construct a table of sequence information. This table is sorted by OID for
19106 : : * speed in lookup.
19107 : : */
19108 : : static void
19109 : 193 : collectSequences(Archive *fout)
19110 : : {
19111 : : PGresult *res;
19112 : : const char *query;
19113 : :
19114 : : /*
19115 : : * Since version 18, we can gather the sequence data in this query with
19116 : : * pg_get_sequence_data(), but we only do so for non-schema-only dumps.
19117 : : */
19118 [ + - ]: 193 : if (fout->remoteVersion < 180000 ||
19119 [ + + + + ]: 193 : (!fout->dopt->dumpData && !fout->dopt->sequence_data))
19120 : 9 : query = "SELECT seqrelid, format_type(seqtypid, NULL), "
19121 : : "seqstart, seqincrement, "
19122 : : "seqmax, seqmin, "
19123 : : "seqcache, seqcycle, "
19124 : : "NULL, 'f' "
19125 : : "FROM pg_catalog.pg_sequence "
19126 : : "ORDER BY seqrelid";
19127 : : else
19128 : 184 : query = "SELECT seqrelid, format_type(seqtypid, NULL), "
19129 : : "seqstart, seqincrement, "
19130 : : "seqmax, seqmin, "
19131 : : "seqcache, seqcycle, "
19132 : : "last_value, is_called "
19133 : : "FROM pg_catalog.pg_sequence, "
19134 : : "pg_get_sequence_data(seqrelid) "
19135 : : "ORDER BY seqrelid;";
19136 : :
19137 : 193 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
19138 : :
19139 : 193 : nsequences = PQntuples(res);
19140 : 193 : sequences = pg_malloc_array(SequenceItem, nsequences);
19141 : :
19142 [ + + ]: 840 : for (int i = 0; i < nsequences; i++)
19143 : : {
19144 : 647 : sequences[i].oid = atooid(PQgetvalue(res, i, 0));
19145 : 647 : sequences[i].seqtype = parse_sequence_type(PQgetvalue(res, i, 1));
19146 : 647 : sequences[i].startv = strtoi64(PQgetvalue(res, i, 2), NULL, 10);
19147 : 647 : sequences[i].incby = strtoi64(PQgetvalue(res, i, 3), NULL, 10);
19148 : 647 : sequences[i].maxv = strtoi64(PQgetvalue(res, i, 4), NULL, 10);
19149 : 647 : sequences[i].minv = strtoi64(PQgetvalue(res, i, 5), NULL, 10);
19150 : 647 : sequences[i].cache = strtoi64(PQgetvalue(res, i, 6), NULL, 10);
19151 : 647 : sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0);
19152 : 647 : sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10);
19153 : 647 : sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0);
19154 [ + + - + ]: 647 : sequences[i].null_seqtuple = (PQgetisnull(res, i, 8) || PQgetisnull(res, i, 9));
19155 : : }
19156 : :
19157 : 193 : PQclear(res);
19158 : 193 : }
19159 : :
19160 : : /*
19161 : : * dumpSequence
19162 : : * write the declaration (not data) of one user-defined sequence
19163 : : */
19164 : : static void
19165 : 381 : dumpSequence(Archive *fout, const TableInfo *tbinfo)
19166 : : {
19167 : 381 : DumpOptions *dopt = fout->dopt;
19168 : : SequenceItem *seq;
19169 : : bool is_ascending;
19170 : : int64 default_minv,
19171 : : default_maxv;
19172 : 381 : PQExpBuffer query = createPQExpBuffer();
19173 : 381 : PQExpBuffer delqry = createPQExpBuffer();
19174 : : char *qseqname;
19175 : 381 : TableInfo *owning_tab = NULL;
19176 : 381 : SequenceItem key = {0};
19177 : :
19178 : 381 : qseqname = pg_strdup(fmtId(tbinfo->dobj.name));
19179 : :
19180 : : /*
19181 : : * The sequence information is gathered in a sorted table before any calls
19182 : : * to dumpSequence(). See collectSequences() for more information.
19183 : : */
19184 : : Assert(sequences);
19185 : :
19186 : 381 : key.oid = tbinfo->dobj.catId.oid;
19187 : 381 : seq = bsearch(&key, sequences, nsequences,
19188 : : sizeof(SequenceItem), SequenceItemCmp);
19189 : :
19190 : : /* Calculate default limits for a sequence of this type */
19191 : 381 : is_ascending = (seq->incby >= 0);
19192 [ + + ]: 381 : if (seq->seqtype == SEQTYPE_SMALLINT)
19193 : : {
19194 [ + + ]: 25 : default_minv = is_ascending ? 1 : PG_INT16_MIN;
19195 [ + + ]: 25 : default_maxv = is_ascending ? PG_INT16_MAX : -1;
19196 : : }
19197 [ + + ]: 356 : else if (seq->seqtype == SEQTYPE_INTEGER)
19198 : : {
19199 [ + + ]: 290 : default_minv = is_ascending ? 1 : PG_INT32_MIN;
19200 [ + + ]: 290 : default_maxv = is_ascending ? PG_INT32_MAX : -1;
19201 : : }
19202 [ + - ]: 66 : else if (seq->seqtype == SEQTYPE_BIGINT)
19203 : : {
19204 [ + + ]: 66 : default_minv = is_ascending ? 1 : PG_INT64_MIN;
19205 [ + + ]: 66 : default_maxv = is_ascending ? PG_INT64_MAX : -1;
19206 : : }
19207 : : else
19208 : : {
19209 : 0 : pg_fatal("unrecognized sequence type: %d", seq->seqtype);
19210 : : default_minv = default_maxv = 0; /* keep compiler quiet */
19211 : : }
19212 : :
19213 : : /*
19214 : : * Identity sequences are not to be dropped separately.
19215 : : */
19216 [ + + ]: 381 : if (!tbinfo->is_identity_sequence)
19217 : : {
19218 : 237 : appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n",
19219 : 237 : fmtQualifiedDumpable(tbinfo));
19220 : : }
19221 : :
19222 : 381 : resetPQExpBuffer(query);
19223 : :
19224 [ + + ]: 381 : if (dopt->binary_upgrade)
19225 : : {
19226 : 66 : binary_upgrade_set_pg_class_oids(fout, query,
19227 : 66 : tbinfo->dobj.catId.oid);
19228 : :
19229 : : /*
19230 : : * In older PG versions a sequence will have a pg_type entry, but v14
19231 : : * and up don't use that, so don't attempt to preserve the type OID.
19232 : : */
19233 : : }
19234 : :
19235 [ + + ]: 381 : if (tbinfo->is_identity_sequence)
19236 : : {
19237 : 144 : owning_tab = findTableByOid(tbinfo->owning_tab);
19238 : :
19239 : 144 : appendPQExpBuffer(query,
19240 : : "ALTER TABLE %s ",
19241 : 144 : fmtQualifiedDumpable(owning_tab));
19242 : 144 : appendPQExpBuffer(query,
19243 : : "ALTER COLUMN %s ADD GENERATED ",
19244 : 144 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19245 [ + + ]: 144 : if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS)
19246 : 104 : appendPQExpBufferStr(query, "ALWAYS");
19247 [ + - ]: 40 : else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
19248 : 40 : appendPQExpBufferStr(query, "BY DEFAULT");
19249 : 144 : appendPQExpBuffer(query, " AS IDENTITY (\n SEQUENCE NAME %s\n",
19250 : 144 : fmtQualifiedDumpable(tbinfo));
19251 : :
19252 : : /*
19253 : : * Emit persistence option only if it's different from the owning
19254 : : * table's. This avoids using this new syntax unnecessarily.
19255 : : */
19256 [ + + ]: 144 : if (tbinfo->relpersistence != owning_tab->relpersistence)
19257 : 10 : appendPQExpBuffer(query, " %s\n",
19258 [ + + ]: 10 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19259 : : "UNLOGGED" : "LOGGED");
19260 : : }
19261 : : else
19262 : : {
19263 : 237 : appendPQExpBuffer(query,
19264 : : "CREATE %sSEQUENCE %s\n",
19265 [ + + ]: 237 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19266 : : "UNLOGGED " : "",
19267 : 237 : fmtQualifiedDumpable(tbinfo));
19268 : :
19269 [ + + ]: 237 : if (seq->seqtype != SEQTYPE_BIGINT)
19270 : 186 : appendPQExpBuffer(query, " AS %s\n", SeqTypeNames[seq->seqtype]);
19271 : : }
19272 : :
19273 : 381 : appendPQExpBuffer(query, " START WITH " INT64_FORMAT "\n", seq->startv);
19274 : :
19275 : 381 : appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT "\n", seq->incby);
19276 : :
19277 [ + + ]: 381 : if (seq->minv != default_minv)
19278 : 15 : appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT "\n", seq->minv);
19279 : : else
19280 : 366 : appendPQExpBufferStr(query, " NO MINVALUE\n");
19281 : :
19282 [ + + ]: 381 : if (seq->maxv != default_maxv)
19283 : 15 : appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT "\n", seq->maxv);
19284 : : else
19285 : 366 : appendPQExpBufferStr(query, " NO MAXVALUE\n");
19286 : :
19287 : 381 : appendPQExpBuffer(query,
19288 : : " CACHE " INT64_FORMAT "%s",
19289 [ + + ]: 381 : seq->cache, (seq->cycled ? "\n CYCLE" : ""));
19290 : :
19291 [ + + ]: 381 : if (tbinfo->is_identity_sequence)
19292 : 144 : appendPQExpBufferStr(query, "\n);\n");
19293 : : else
19294 : 237 : appendPQExpBufferStr(query, ";\n");
19295 : :
19296 : : /* binary_upgrade: no need to clear TOAST table oid */
19297 : :
19298 [ + + ]: 381 : if (dopt->binary_upgrade)
19299 : 66 : binary_upgrade_extension_member(query, &tbinfo->dobj,
19300 : : "SEQUENCE", qseqname,
19301 : 66 : tbinfo->dobj.namespace->dobj.name);
19302 : :
19303 [ + - ]: 381 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19304 : 381 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
19305 : 381 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19306 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19307 : : .owner = tbinfo->rolname,
19308 : : .description = "SEQUENCE",
19309 : : .section = SECTION_PRE_DATA,
19310 : : .createStmt = query->data,
19311 : : .dropStmt = delqry->data));
19312 : :
19313 : : /*
19314 : : * If the sequence is owned by a table column, emit the ALTER for it as a
19315 : : * separate TOC entry immediately following the sequence's own entry. It's
19316 : : * OK to do this rather than using full sorting logic, because the
19317 : : * dependency that tells us it's owned will have forced the table to be
19318 : : * created first. We can't just include the ALTER in the TOC entry
19319 : : * because it will fail if we haven't reassigned the sequence owner to
19320 : : * match the table's owner.
19321 : : *
19322 : : * We need not schema-qualify the table reference because both sequence
19323 : : * and table must be in the same schema.
19324 : : */
19325 [ + + + + ]: 381 : if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence)
19326 : : {
19327 : 141 : owning_tab = findTableByOid(tbinfo->owning_tab);
19328 : :
19329 [ - + ]: 141 : if (owning_tab == NULL)
19330 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
19331 : : tbinfo->owning_tab, tbinfo->dobj.catId.oid);
19332 : :
19333 [ + + ]: 141 : if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION)
19334 : : {
19335 : 139 : resetPQExpBuffer(query);
19336 : 139 : appendPQExpBuffer(query, "ALTER SEQUENCE %s",
19337 : 139 : fmtQualifiedDumpable(tbinfo));
19338 : 139 : appendPQExpBuffer(query, " OWNED BY %s",
19339 : 139 : fmtQualifiedDumpable(owning_tab));
19340 : 139 : appendPQExpBuffer(query, ".%s;\n",
19341 : 139 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19342 : :
19343 [ + - ]: 139 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19344 : 139 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19345 : 139 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19346 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19347 : : .owner = tbinfo->rolname,
19348 : : .description = "SEQUENCE OWNED BY",
19349 : : .section = SECTION_PRE_DATA,
19350 : : .createStmt = query->data,
19351 : : .deps = &(tbinfo->dobj.dumpId),
19352 : : .nDeps = 1));
19353 : : }
19354 : : }
19355 : :
19356 : : /* Dump Sequence Comments and Security Labels */
19357 [ - + ]: 381 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19358 : 0 : dumpComment(fout, "SEQUENCE", qseqname,
19359 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19360 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19361 : :
19362 [ - + ]: 381 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19363 : 0 : dumpSecLabel(fout, "SEQUENCE", qseqname,
19364 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19365 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19366 : :
19367 : 381 : destroyPQExpBuffer(query);
19368 : 381 : destroyPQExpBuffer(delqry);
19369 : 381 : pg_free(qseqname);
19370 : 381 : }
19371 : :
19372 : : /*
19373 : : * dumpSequenceData
19374 : : * write the data of one user-defined sequence
19375 : : */
19376 : : static void
19377 : 399 : dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo)
19378 : : {
19379 : 399 : TableInfo *tbinfo = tdinfo->tdtable;
19380 : : int64 last;
19381 : : bool called;
19382 : : PQExpBuffer query;
19383 : :
19384 : : /* needn't bother if not dumping sequence data */
19385 [ + + + + ]: 399 : if (!fout->dopt->dumpData && !fout->dopt->sequence_data)
19386 : 1 : return;
19387 : :
19388 : 398 : query = createPQExpBuffer();
19389 : :
19390 : : /*
19391 : : * For versions >= 18, the sequence information is gathered in the sorted
19392 : : * array before any calls to dumpSequenceData(). See collectSequences()
19393 : : * for more information.
19394 : : *
19395 : : * For older versions, we have to query the sequence relations
19396 : : * individually.
19397 : : */
19398 [ - + ]: 398 : if (fout->remoteVersion < 180000)
19399 : : {
19400 : : PGresult *res;
19401 : :
19402 : 0 : appendPQExpBuffer(query,
19403 : : "SELECT last_value, is_called FROM %s",
19404 : 0 : fmtQualifiedDumpable(tbinfo));
19405 : :
19406 : 0 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19407 : :
19408 [ # # ]: 0 : if (PQntuples(res) != 1)
19409 : 0 : pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)",
19410 : : "query to get data of sequence \"%s\" returned %d rows (expected 1)",
19411 : : PQntuples(res)),
19412 : : tbinfo->dobj.name, PQntuples(res));
19413 : :
19414 : 0 : last = strtoi64(PQgetvalue(res, 0, 0), NULL, 10);
19415 : 0 : called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
19416 : :
19417 : 0 : PQclear(res);
19418 : : }
19419 : : else
19420 : : {
19421 : 398 : SequenceItem key = {0};
19422 : : SequenceItem *entry;
19423 : :
19424 : : Assert(sequences);
19425 : : Assert(tbinfo->dobj.catId.oid);
19426 : :
19427 : 398 : key.oid = tbinfo->dobj.catId.oid;
19428 : 398 : entry = bsearch(&key, sequences, nsequences,
19429 : : sizeof(SequenceItem), SequenceItemCmp);
19430 : :
19431 [ - + ]: 398 : if (entry->null_seqtuple)
19432 : 0 : pg_fatal("failed to get data for sequence \"%s\"; user may lack "
19433 : : "SELECT privilege on the sequence or the sequence may "
19434 : : "have been concurrently dropped",
19435 : : tbinfo->dobj.name);
19436 : :
19437 : 398 : last = entry->last_value;
19438 : 398 : called = entry->is_called;
19439 : : }
19440 : :
19441 : 398 : resetPQExpBuffer(query);
19442 : 398 : appendPQExpBufferStr(query, "SELECT pg_catalog.setval(");
19443 : 398 : appendStringLiteralAH(query, fmtQualifiedDumpable(tbinfo), fout);
19444 [ + + ]: 398 : appendPQExpBuffer(query, ", " INT64_FORMAT ", %s);\n",
19445 : : last, (called ? "true" : "false"));
19446 : :
19447 [ + - ]: 398 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
19448 : 398 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19449 : 398 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19450 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19451 : : .owner = tbinfo->rolname,
19452 : : .description = "SEQUENCE SET",
19453 : : .section = SECTION_DATA,
19454 : : .createStmt = query->data,
19455 : : .deps = &(tbinfo->dobj.dumpId),
19456 : : .nDeps = 1));
19457 : :
19458 : 398 : destroyPQExpBuffer(query);
19459 : : }
19460 : :
19461 : : /*
19462 : : * dumpTrigger
19463 : : * write the declaration of one user-defined table trigger
19464 : : */
19465 : : static void
19466 : 535 : dumpTrigger(Archive *fout, const TriggerInfo *tginfo)
19467 : : {
19468 : 535 : DumpOptions *dopt = fout->dopt;
19469 : 535 : TableInfo *tbinfo = tginfo->tgtable;
19470 : : PQExpBuffer query;
19471 : : PQExpBuffer delqry;
19472 : : PQExpBuffer trigprefix;
19473 : : PQExpBuffer trigidentity;
19474 : : char *qtabname;
19475 : : char *tag;
19476 : :
19477 : : /* Do nothing if not dumping schema */
19478 [ + + ]: 535 : if (!dopt->dumpSchema)
19479 : 33 : return;
19480 : :
19481 : 502 : query = createPQExpBuffer();
19482 : 502 : delqry = createPQExpBuffer();
19483 : 502 : trigprefix = createPQExpBuffer();
19484 : 502 : trigidentity = createPQExpBuffer();
19485 : :
19486 : 502 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19487 : :
19488 : 502 : appendPQExpBuffer(trigidentity, "%s ", fmtId(tginfo->dobj.name));
19489 : 502 : appendPQExpBuffer(trigidentity, "ON %s", fmtQualifiedDumpable(tbinfo));
19490 : :
19491 : 502 : appendPQExpBuffer(query, "%s;\n", tginfo->tgdef);
19492 : 502 : appendPQExpBuffer(delqry, "DROP TRIGGER %s;\n", trigidentity->data);
19493 : :
19494 : : /* Triggers can depend on extensions */
19495 : 502 : append_depends_on_extension(fout, query, &tginfo->dobj,
19496 : : "pg_catalog.pg_trigger", "TRIGGER",
19497 : 502 : trigidentity->data);
19498 : :
19499 [ + + ]: 502 : if (tginfo->tgispartition)
19500 : : {
19501 : : Assert(tbinfo->ispartition);
19502 : :
19503 : : /*
19504 : : * Partition triggers only appear here because their 'tgenabled' flag
19505 : : * differs from its parent's. The trigger is created already, so
19506 : : * remove the CREATE and replace it with an ALTER. (Clear out the
19507 : : * DROP query too, so that pg_dump --create does not cause errors.)
19508 : : */
19509 : 115 : resetPQExpBuffer(query);
19510 : 115 : resetPQExpBuffer(delqry);
19511 : 115 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19512 [ - + ]: 115 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19513 : 115 : fmtQualifiedDumpable(tbinfo));
19514 [ + - + + : 115 : switch (tginfo->tgenabled)
- ]
19515 : : {
19516 : 40 : case 'f':
19517 : : case 'D':
19518 : 40 : appendPQExpBufferStr(query, "DISABLE");
19519 : 40 : break;
19520 : 0 : case 't':
19521 : : case 'O':
19522 : 0 : appendPQExpBufferStr(query, "ENABLE");
19523 : 0 : break;
19524 : 35 : case 'R':
19525 : 35 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19526 : 35 : break;
19527 : 40 : case 'A':
19528 : 40 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19529 : 40 : break;
19530 : : }
19531 : 115 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19532 : 115 : fmtId(tginfo->dobj.name));
19533 : : }
19534 [ + - - + ]: 387 : else if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
19535 : : {
19536 : 0 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19537 [ # # ]: 0 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19538 : 0 : fmtQualifiedDumpable(tbinfo));
19539 [ # # # # ]: 0 : switch (tginfo->tgenabled)
19540 : : {
19541 : 0 : case 'D':
19542 : : case 'f':
19543 : 0 : appendPQExpBufferStr(query, "DISABLE");
19544 : 0 : break;
19545 : 0 : case 'A':
19546 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19547 : 0 : break;
19548 : 0 : case 'R':
19549 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19550 : 0 : break;
19551 : 0 : default:
19552 : 0 : appendPQExpBufferStr(query, "ENABLE");
19553 : 0 : break;
19554 : : }
19555 : 0 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19556 : 0 : fmtId(tginfo->dobj.name));
19557 : : }
19558 : :
19559 : 502 : appendPQExpBuffer(trigprefix, "TRIGGER %s ON",
19560 : 502 : fmtId(tginfo->dobj.name));
19561 : :
19562 : 502 : tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name);
19563 : :
19564 [ + - ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19565 : 502 : ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
19566 : 502 : ARCHIVE_OPTS(.tag = tag,
19567 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19568 : : .owner = tbinfo->rolname,
19569 : : .description = "TRIGGER",
19570 : : .section = SECTION_POST_DATA,
19571 : : .createStmt = query->data,
19572 : : .dropStmt = delqry->data));
19573 : :
19574 [ - + ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19575 : 0 : dumpComment(fout, trigprefix->data, qtabname,
19576 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19577 : 0 : tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
19578 : :
19579 : 502 : pfree(tag);
19580 : 502 : destroyPQExpBuffer(query);
19581 : 502 : destroyPQExpBuffer(delqry);
19582 : 502 : destroyPQExpBuffer(trigprefix);
19583 : 502 : destroyPQExpBuffer(trigidentity);
19584 : 502 : pg_free(qtabname);
19585 : : }
19586 : :
19587 : : /*
19588 : : * dumpEventTrigger
19589 : : * write the declaration of one user-defined event trigger
19590 : : */
19591 : : static void
19592 : 44 : dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo)
19593 : : {
19594 : 44 : DumpOptions *dopt = fout->dopt;
19595 : : PQExpBuffer query;
19596 : : PQExpBuffer delqry;
19597 : : char *qevtname;
19598 : :
19599 : : /* Do nothing if not dumping schema */
19600 [ + + ]: 44 : if (!dopt->dumpSchema)
19601 : 6 : return;
19602 : :
19603 : 38 : query = createPQExpBuffer();
19604 : 38 : delqry = createPQExpBuffer();
19605 : :
19606 : 38 : qevtname = pg_strdup(fmtId(evtinfo->dobj.name));
19607 : :
19608 : 38 : appendPQExpBufferStr(query, "CREATE EVENT TRIGGER ");
19609 : 38 : appendPQExpBufferStr(query, qevtname);
19610 : 38 : appendPQExpBufferStr(query, " ON ");
19611 : 38 : appendPQExpBufferStr(query, fmtId(evtinfo->evtevent));
19612 : :
19613 [ + + ]: 38 : if (strcmp("", evtinfo->evttags) != 0)
19614 : : {
19615 : 5 : appendPQExpBufferStr(query, "\n WHEN TAG IN (");
19616 : 5 : appendPQExpBufferStr(query, evtinfo->evttags);
19617 : 5 : appendPQExpBufferChar(query, ')');
19618 : : }
19619 : :
19620 : 38 : appendPQExpBufferStr(query, "\n EXECUTE FUNCTION ");
19621 : 38 : appendPQExpBufferStr(query, evtinfo->evtfname);
19622 : 38 : appendPQExpBufferStr(query, "();\n");
19623 : :
19624 [ - + ]: 38 : if (evtinfo->evtenabled != 'O')
19625 : : {
19626 : 0 : appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ",
19627 : : qevtname);
19628 [ # # # # ]: 0 : switch (evtinfo->evtenabled)
19629 : : {
19630 : 0 : case 'D':
19631 : 0 : appendPQExpBufferStr(query, "DISABLE");
19632 : 0 : break;
19633 : 0 : case 'A':
19634 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19635 : 0 : break;
19636 : 0 : case 'R':
19637 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19638 : 0 : break;
19639 : 0 : default:
19640 : 0 : appendPQExpBufferStr(query, "ENABLE");
19641 : 0 : break;
19642 : : }
19643 : 0 : appendPQExpBufferStr(query, ";\n");
19644 : : }
19645 : :
19646 : 38 : appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n",
19647 : : qevtname);
19648 : :
19649 [ + + ]: 38 : if (dopt->binary_upgrade)
19650 : 2 : binary_upgrade_extension_member(query, &evtinfo->dobj,
19651 : : "EVENT TRIGGER", qevtname, NULL);
19652 : :
19653 [ + - ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19654 : 38 : ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId,
19655 : 38 : ARCHIVE_OPTS(.tag = evtinfo->dobj.name,
19656 : : .owner = evtinfo->evtowner,
19657 : : .description = "EVENT TRIGGER",
19658 : : .section = SECTION_POST_DATA,
19659 : : .createStmt = query->data,
19660 : : .dropStmt = delqry->data));
19661 : :
19662 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19663 : 0 : dumpComment(fout, "EVENT TRIGGER", qevtname,
19664 : 0 : NULL, evtinfo->evtowner,
19665 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19666 : :
19667 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19668 : 0 : dumpSecLabel(fout, "EVENT TRIGGER", qevtname,
19669 : 0 : NULL, evtinfo->evtowner,
19670 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19671 : :
19672 : 38 : destroyPQExpBuffer(query);
19673 : 38 : destroyPQExpBuffer(delqry);
19674 : 38 : pg_free(qevtname);
19675 : : }
19676 : :
19677 : : /*
19678 : : * dumpRule
19679 : : * Dump a rule
19680 : : */
19681 : : static void
19682 : 1177 : dumpRule(Archive *fout, const RuleInfo *rinfo)
19683 : : {
19684 : 1177 : DumpOptions *dopt = fout->dopt;
19685 : 1177 : TableInfo *tbinfo = rinfo->ruletable;
19686 : : bool is_view;
19687 : : PQExpBuffer query;
19688 : : PQExpBuffer cmd;
19689 : : PQExpBuffer delcmd;
19690 : : PQExpBuffer ruleprefix;
19691 : : char *qtabname;
19692 : : PGresult *res;
19693 : : char *tag;
19694 : :
19695 : : /* Do nothing if not dumping schema */
19696 [ + + ]: 1177 : if (!dopt->dumpSchema)
19697 : 70 : return;
19698 : :
19699 : : /*
19700 : : * If it is an ON SELECT rule that is created implicitly by CREATE VIEW,
19701 : : * we do not want to dump it as a separate object.
19702 : : */
19703 [ + + ]: 1107 : if (!rinfo->separate)
19704 : 896 : return;
19705 : :
19706 : : /*
19707 : : * If it's an ON SELECT rule, we want to print it as a view definition,
19708 : : * instead of a rule.
19709 : : */
19710 [ + + + - ]: 211 : is_view = (rinfo->ev_type == '1' && rinfo->is_instead);
19711 : :
19712 : 211 : query = createPQExpBuffer();
19713 : 211 : cmd = createPQExpBuffer();
19714 : 211 : delcmd = createPQExpBuffer();
19715 : 211 : ruleprefix = createPQExpBuffer();
19716 : :
19717 : 211 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19718 : :
19719 [ + + ]: 211 : if (is_view)
19720 : : {
19721 : : PQExpBuffer result;
19722 : :
19723 : : /*
19724 : : * We need OR REPLACE here because we'll be replacing a dummy view.
19725 : : * Otherwise this should look largely like the regular view dump code.
19726 : : */
19727 : 10 : appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s",
19728 : 10 : fmtQualifiedDumpable(tbinfo));
19729 [ - + ]: 10 : if (nonemptyReloptions(tbinfo->reloptions))
19730 : : {
19731 : 0 : appendPQExpBufferStr(cmd, " WITH (");
19732 : 0 : appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout);
19733 : 0 : appendPQExpBufferChar(cmd, ')');
19734 : : }
19735 : 10 : result = createViewAsClause(fout, tbinfo);
19736 : 10 : appendPQExpBuffer(cmd, " AS\n%s", result->data);
19737 : 10 : destroyPQExpBuffer(result);
19738 [ - + ]: 10 : if (tbinfo->checkoption != NULL)
19739 : 0 : appendPQExpBuffer(cmd, "\n WITH %s CHECK OPTION",
19740 : : tbinfo->checkoption);
19741 : 10 : appendPQExpBufferStr(cmd, ";\n");
19742 : : }
19743 : : else
19744 : : {
19745 : : /* In the rule case, just print pg_get_ruledef's result verbatim */
19746 : 201 : appendPQExpBuffer(query,
19747 : : "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)",
19748 : 201 : rinfo->dobj.catId.oid);
19749 : :
19750 : 201 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19751 : :
19752 [ - + ]: 201 : if (PQntuples(res) != 1)
19753 : 0 : pg_fatal("query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned",
19754 : : rinfo->dobj.name, tbinfo->dobj.name);
19755 : :
19756 : 201 : printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
19757 : :
19758 : 201 : PQclear(res);
19759 : : }
19760 : :
19761 : : /*
19762 : : * Add the command to alter the rules replication firing semantics if it
19763 : : * differs from the default.
19764 : : */
19765 [ + + ]: 211 : if (rinfo->ev_enabled != 'O')
19766 : : {
19767 : 15 : appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtQualifiedDumpable(tbinfo));
19768 [ - - + - ]: 15 : switch (rinfo->ev_enabled)
19769 : : {
19770 : 0 : case 'A':
19771 : 0 : appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n",
19772 : 0 : fmtId(rinfo->dobj.name));
19773 : 0 : break;
19774 : 0 : case 'R':
19775 : 0 : appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n",
19776 : 0 : fmtId(rinfo->dobj.name));
19777 : 0 : break;
19778 : 15 : case 'D':
19779 : 15 : appendPQExpBuffer(cmd, "DISABLE RULE %s;\n",
19780 : 15 : fmtId(rinfo->dobj.name));
19781 : 15 : break;
19782 : : }
19783 : : }
19784 : :
19785 [ + + ]: 211 : if (is_view)
19786 : : {
19787 : : /*
19788 : : * We can't DROP a view's ON SELECT rule. Instead, use CREATE OR
19789 : : * REPLACE VIEW to replace the rule with something with minimal
19790 : : * dependencies.
19791 : : */
19792 : : PQExpBuffer result;
19793 : :
19794 : 10 : appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s",
19795 : 10 : fmtQualifiedDumpable(tbinfo));
19796 : 10 : result = createDummyViewAsClause(fout, tbinfo);
19797 : 10 : appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data);
19798 : 10 : destroyPQExpBuffer(result);
19799 : : }
19800 : : else
19801 : : {
19802 : 201 : appendPQExpBuffer(delcmd, "DROP RULE %s ",
19803 : 201 : fmtId(rinfo->dobj.name));
19804 : 201 : appendPQExpBuffer(delcmd, "ON %s;\n",
19805 : 201 : fmtQualifiedDumpable(tbinfo));
19806 : : }
19807 : :
19808 : 211 : appendPQExpBuffer(ruleprefix, "RULE %s ON",
19809 : 211 : fmtId(rinfo->dobj.name));
19810 : :
19811 : 211 : tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name);
19812 : :
19813 [ + - ]: 211 : if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19814 : 211 : ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
19815 : 211 : ARCHIVE_OPTS(.tag = tag,
19816 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19817 : : .owner = tbinfo->rolname,
19818 : : .description = "RULE",
19819 : : .section = SECTION_POST_DATA,
19820 : : .createStmt = cmd->data,
19821 : : .dropStmt = delcmd->data));
19822 : :
19823 : : /* Dump rule comments */
19824 [ - + ]: 211 : if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19825 : 0 : dumpComment(fout, ruleprefix->data, qtabname,
19826 : 0 : tbinfo->dobj.namespace->dobj.name,
19827 : : tbinfo->rolname,
19828 : 0 : rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
19829 : :
19830 : 211 : pfree(tag);
19831 : 211 : destroyPQExpBuffer(query);
19832 : 211 : destroyPQExpBuffer(cmd);
19833 : 211 : destroyPQExpBuffer(delcmd);
19834 : 211 : destroyPQExpBuffer(ruleprefix);
19835 : 211 : pg_free(qtabname);
19836 : : }
19837 : :
19838 : : /*
19839 : : * getExtensionMembership --- obtain extension membership data
19840 : : *
19841 : : * We need to identify objects that are extension members as soon as they're
19842 : : * loaded, so that we can correctly determine whether they need to be dumped.
19843 : : * Generally speaking, extension member objects will get marked as *not* to
19844 : : * be dumped, as they will be recreated by the single CREATE EXTENSION
19845 : : * command. However, in binary upgrade mode we still need to dump the members
19846 : : * individually.
19847 : : */
19848 : : void
19849 : 194 : getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
19850 : : int numExtensions)
19851 : : {
19852 : : PQExpBuffer query;
19853 : : PGresult *res;
19854 : : int ntups,
19855 : : i;
19856 : : int i_classid,
19857 : : i_objid,
19858 : : i_refobjid;
19859 : : ExtensionInfo *ext;
19860 : :
19861 : : /* Nothing to do if no extensions */
19862 [ - + ]: 194 : if (numExtensions == 0)
19863 : 0 : return;
19864 : :
19865 : 194 : query = createPQExpBuffer();
19866 : :
19867 : : /* refclassid constraint is redundant but may speed the search */
19868 : 194 : appendPQExpBufferStr(query, "SELECT "
19869 : : "classid, objid, refobjid "
19870 : : "FROM pg_depend "
19871 : : "WHERE refclassid = 'pg_extension'::regclass "
19872 : : "AND deptype = 'e' "
19873 : : "ORDER BY 3");
19874 : :
19875 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19876 : :
19877 : 194 : ntups = PQntuples(res);
19878 : :
19879 : 194 : i_classid = PQfnumber(res, "classid");
19880 : 194 : i_objid = PQfnumber(res, "objid");
19881 : 194 : i_refobjid = PQfnumber(res, "refobjid");
19882 : :
19883 : : /*
19884 : : * Since we ordered the SELECT by referenced ID, we can expect that
19885 : : * multiple entries for the same extension will appear together; this
19886 : : * saves on searches.
19887 : : */
19888 : 194 : ext = NULL;
19889 : :
19890 [ + + ]: 1576 : for (i = 0; i < ntups; i++)
19891 : : {
19892 : : CatalogId objId;
19893 : : Oid extId;
19894 : :
19895 : 1382 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
19896 : 1382 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
19897 : 1382 : extId = atooid(PQgetvalue(res, i, i_refobjid));
19898 : :
19899 [ + + ]: 1382 : if (ext == NULL ||
19900 [ + + ]: 1188 : ext->dobj.catId.oid != extId)
19901 : 225 : ext = findExtensionByOid(extId);
19902 : :
19903 [ - + ]: 1382 : if (ext == NULL)
19904 : : {
19905 : : /* shouldn't happen */
19906 : 0 : pg_log_warning("could not find referenced extension %u", extId);
19907 : 0 : continue;
19908 : : }
19909 : :
19910 : 1382 : recordExtensionMembership(objId, ext);
19911 : : }
19912 : :
19913 : 194 : PQclear(res);
19914 : :
19915 : 194 : destroyPQExpBuffer(query);
19916 : : }
19917 : :
19918 : : /*
19919 : : * processExtensionTables --- deal with extension configuration tables
19920 : : *
19921 : : * There are two parts to this process:
19922 : : *
19923 : : * 1. Identify and create dump records for extension configuration tables.
19924 : : *
19925 : : * Extensions can mark tables as "configuration", which means that the user
19926 : : * is able and expected to modify those tables after the extension has been
19927 : : * loaded. For these tables, we dump out only the data- the structure is
19928 : : * expected to be handled at CREATE EXTENSION time, including any indexes or
19929 : : * foreign keys, which brings us to-
19930 : : *
19931 : : * 2. Record FK dependencies between configuration tables.
19932 : : *
19933 : : * Due to the FKs being created at CREATE EXTENSION time and therefore before
19934 : : * the data is loaded, we have to work out what the best order for reloading
19935 : : * the data is, to avoid FK violations when the tables are restored. This is
19936 : : * not perfect- we can't handle circular dependencies and if any exist they
19937 : : * will cause an invalid dump to be produced (though at least all of the data
19938 : : * is included for a user to manually restore). This is currently documented
19939 : : * but perhaps we can provide a better solution in the future.
19940 : : */
19941 : : void
19942 : 193 : processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
19943 : : int numExtensions)
19944 : : {
19945 : 193 : DumpOptions *dopt = fout->dopt;
19946 : : PQExpBuffer query;
19947 : : PGresult *res;
19948 : : int ntups,
19949 : : i;
19950 : : int i_conrelid,
19951 : : i_confrelid;
19952 : :
19953 : : /* Nothing to do if no extensions */
19954 [ - + ]: 193 : if (numExtensions == 0)
19955 : 0 : return;
19956 : :
19957 : : /*
19958 : : * Identify extension configuration tables and create TableDataInfo
19959 : : * objects for them, ensuring their data will be dumped even though the
19960 : : * tables themselves won't be.
19961 : : *
19962 : : * Note that we create TableDataInfo objects even in schema-only mode, ie,
19963 : : * user data in a configuration table is treated like schema data. This
19964 : : * seems appropriate since system data in a config table would get
19965 : : * reloaded by CREATE EXTENSION. If the extension is not listed in the
19966 : : * list of extensions to be included, none of its data is dumped.
19967 : : */
19968 [ + + ]: 417 : for (i = 0; i < numExtensions; i++)
19969 : : {
19970 : 224 : ExtensionInfo *curext = &(extinfo[i]);
19971 : 224 : char *extconfig = curext->extconfig;
19972 : 224 : char *extcondition = curext->extcondition;
19973 : 224 : char **extconfigarray = NULL;
19974 : 224 : char **extconditionarray = NULL;
19975 : 224 : int nconfigitems = 0;
19976 : 224 : int nconditionitems = 0;
19977 : :
19978 : : /*
19979 : : * Check if this extension is listed as to include in the dump. If
19980 : : * not, any table data associated with it is discarded.
19981 : : */
19982 [ + + ]: 224 : if (extension_include_oids.head != NULL &&
19983 [ + + ]: 8 : !simple_oid_list_member(&extension_include_oids,
19984 : : curext->dobj.catId.oid))
19985 : 6 : continue;
19986 : :
19987 : : /*
19988 : : * Check if this extension is listed as to exclude in the dump. If
19989 : : * yes, any table data associated with it is discarded.
19990 : : */
19991 [ + + + + ]: 224 : if (extension_exclude_oids.head != NULL &&
19992 : 4 : simple_oid_list_member(&extension_exclude_oids,
19993 : : curext->dobj.catId.oid))
19994 : 2 : continue;
19995 : :
19996 [ + + - + ]: 218 : if (strlen(extconfig) != 0 || strlen(extcondition) != 0)
19997 : : {
19998 : : int j;
19999 : :
20000 [ - + ]: 20 : if (!parsePGArray(extconfig, &extconfigarray, &nconfigitems))
20001 : 0 : pg_fatal("could not parse %s array", "extconfig");
20002 [ - + ]: 20 : if (!parsePGArray(extcondition, &extconditionarray, &nconditionitems))
20003 : 0 : pg_fatal("could not parse %s array", "extcondition");
20004 [ - + ]: 20 : if (nconfigitems != nconditionitems)
20005 : 0 : pg_fatal("mismatched number of configurations and conditions for extension");
20006 : :
20007 [ + + ]: 60 : for (j = 0; j < nconfigitems; j++)
20008 : : {
20009 : : TableInfo *configtbl;
20010 : 40 : Oid configtbloid = atooid(extconfigarray[j]);
20011 : 40 : bool dumpobj =
20012 : 40 : curext->dobj.dump & DUMP_COMPONENT_DEFINITION;
20013 : :
20014 : 40 : configtbl = findTableByOid(configtbloid);
20015 [ - + ]: 40 : if (configtbl == NULL)
20016 : 0 : continue;
20017 : :
20018 : : /*
20019 : : * Tables of not-to-be-dumped extensions shouldn't be dumped
20020 : : * unless the table or its schema is explicitly included
20021 : : */
20022 [ + + ]: 40 : if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION))
20023 : : {
20024 : : /* check table explicitly requested */
20025 [ - + - - ]: 2 : if (table_include_oids.head != NULL &&
20026 : 0 : simple_oid_list_member(&table_include_oids,
20027 : : configtbloid))
20028 : 0 : dumpobj = true;
20029 : :
20030 : : /* check table's schema explicitly requested */
20031 [ + - ]: 2 : if (configtbl->dobj.namespace->dobj.dump &
20032 : : DUMP_COMPONENT_DATA)
20033 : 2 : dumpobj = true;
20034 : : }
20035 : :
20036 : : /* check table excluded by an exclusion switch */
20037 [ + + + + ]: 44 : if (table_exclude_oids.head != NULL &&
20038 : 4 : simple_oid_list_member(&table_exclude_oids,
20039 : : configtbloid))
20040 : 1 : dumpobj = false;
20041 : :
20042 : : /* check schema excluded by an exclusion switch */
20043 [ - + ]: 40 : if (simple_oid_list_member(&schema_exclude_oids,
20044 : 40 : configtbl->dobj.namespace->dobj.catId.oid))
20045 : 0 : dumpobj = false;
20046 : :
20047 [ + + ]: 40 : if (dumpobj)
20048 : : {
20049 : 39 : makeTableDataInfo(dopt, configtbl);
20050 [ + - ]: 39 : if (configtbl->dataObj != NULL)
20051 : : {
20052 [ - + ]: 39 : if (strlen(extconditionarray[j]) > 0)
20053 : 0 : configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
20054 : : }
20055 : : }
20056 : : }
20057 : : }
20058 [ + + ]: 218 : if (extconfigarray)
20059 : 20 : free(extconfigarray);
20060 [ + + ]: 218 : if (extconditionarray)
20061 : 20 : free(extconditionarray);
20062 : : }
20063 : :
20064 : : /*
20065 : : * Now that all the TableDataInfo objects have been created for all the
20066 : : * extensions, check their FK dependencies and register them to try and
20067 : : * dump the data out in an order that they can be restored in.
20068 : : *
20069 : : * Note that this is not a problem for user tables as their FKs are
20070 : : * recreated after the data has been loaded.
20071 : : */
20072 : :
20073 : 193 : query = createPQExpBuffer();
20074 : :
20075 : 193 : printfPQExpBuffer(query,
20076 : : "SELECT conrelid, confrelid "
20077 : : "FROM pg_constraint "
20078 : : "JOIN pg_depend ON (objid = confrelid) "
20079 : : "WHERE contype = 'f' "
20080 : : "AND refclassid = 'pg_extension'::regclass "
20081 : : "AND classid = 'pg_class'::regclass;");
20082 : :
20083 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20084 : 193 : ntups = PQntuples(res);
20085 : :
20086 : 193 : i_conrelid = PQfnumber(res, "conrelid");
20087 : 193 : i_confrelid = PQfnumber(res, "confrelid");
20088 : :
20089 : : /* Now get the dependencies and register them */
20090 [ - + ]: 193 : for (i = 0; i < ntups; i++)
20091 : : {
20092 : : Oid conrelid,
20093 : : confrelid;
20094 : : TableInfo *reftable,
20095 : : *contable;
20096 : :
20097 : 0 : conrelid = atooid(PQgetvalue(res, i, i_conrelid));
20098 : 0 : confrelid = atooid(PQgetvalue(res, i, i_confrelid));
20099 : 0 : contable = findTableByOid(conrelid);
20100 : 0 : reftable = findTableByOid(confrelid);
20101 : :
20102 [ # # ]: 0 : if (reftable == NULL ||
20103 [ # # # # ]: 0 : reftable->dataObj == NULL ||
20104 : 0 : contable == NULL ||
20105 [ # # ]: 0 : contable->dataObj == NULL)
20106 : 0 : continue;
20107 : :
20108 : : /*
20109 : : * Make referencing TABLE_DATA object depend on the referenced table's
20110 : : * TABLE_DATA object.
20111 : : */
20112 : 0 : addObjectDependency(&contable->dataObj->dobj,
20113 : 0 : reftable->dataObj->dobj.dumpId);
20114 : : }
20115 : 193 : PQclear(res);
20116 : 193 : destroyPQExpBuffer(query);
20117 : : }
20118 : :
20119 : : /*
20120 : : * getDependencies --- obtain available dependency data
20121 : : */
20122 : : static void
20123 : 193 : getDependencies(Archive *fout)
20124 : : {
20125 : : PQExpBuffer query;
20126 : : PGresult *res;
20127 : : int ntups,
20128 : : i;
20129 : : int i_classid,
20130 : : i_objid,
20131 : : i_refclassid,
20132 : : i_refobjid,
20133 : : i_deptype;
20134 : : DumpableObject *dobj,
20135 : : *refdobj;
20136 : :
20137 : 193 : pg_log_info("reading dependency data");
20138 : :
20139 : 193 : query = createPQExpBuffer();
20140 : :
20141 : : /*
20142 : : * Messy query to collect the dependency data we need. Note that we
20143 : : * ignore the sub-object column, so that dependencies of or on a column
20144 : : * look the same as dependencies of or on a whole table.
20145 : : *
20146 : : * PIN dependencies aren't interesting, and EXTENSION dependencies were
20147 : : * already processed by getExtensionMembership.
20148 : : */
20149 : 193 : appendPQExpBufferStr(query, "SELECT "
20150 : : "classid, objid, refclassid, refobjid, deptype "
20151 : : "FROM pg_depend "
20152 : : "WHERE deptype != 'p' AND deptype != 'e'\n");
20153 : :
20154 : : /*
20155 : : * Since we don't treat pg_amop entries as separate DumpableObjects, we
20156 : : * have to translate their dependencies into dependencies of their parent
20157 : : * opfamily. Ignore internal dependencies though, as those will point to
20158 : : * their parent opclass, which we needn't consider here (and if we did,
20159 : : * it'd just result in circular dependencies). Also, "loose" opfamily
20160 : : * entries will have dependencies on their parent opfamily, which we
20161 : : * should drop since they'd likewise become useless self-dependencies.
20162 : : * (But be sure to keep deps on *other* opfamilies; see amopsortfamily.)
20163 : : */
20164 : 193 : appendPQExpBufferStr(query, "UNION ALL\n"
20165 : : "SELECT 'pg_opfamily'::regclass AS classid, amopfamily AS objid, refclassid, refobjid, deptype "
20166 : : "FROM pg_depend d, pg_amop o "
20167 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20168 : : "classid = 'pg_amop'::regclass AND objid = o.oid "
20169 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amopfamily = refobjid)\n");
20170 : :
20171 : : /* Likewise for pg_amproc entries */
20172 : 193 : appendPQExpBufferStr(query, "UNION ALL\n"
20173 : : "SELECT 'pg_opfamily'::regclass AS classid, amprocfamily AS objid, refclassid, refobjid, deptype "
20174 : : "FROM pg_depend d, pg_amproc p "
20175 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20176 : : "classid = 'pg_amproc'::regclass AND objid = p.oid "
20177 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amprocfamily = refobjid)\n");
20178 : :
20179 : : /* Sort the output for efficiency below */
20180 : 193 : appendPQExpBufferStr(query, "ORDER BY 1,2");
20181 : :
20182 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20183 : :
20184 : 193 : ntups = PQntuples(res);
20185 : :
20186 : 193 : i_classid = PQfnumber(res, "classid");
20187 : 193 : i_objid = PQfnumber(res, "objid");
20188 : 193 : i_refclassid = PQfnumber(res, "refclassid");
20189 : 193 : i_refobjid = PQfnumber(res, "refobjid");
20190 : 193 : i_deptype = PQfnumber(res, "deptype");
20191 : :
20192 : : /*
20193 : : * Since we ordered the SELECT by referencing ID, we can expect that
20194 : : * multiple entries for the same object will appear together; this saves
20195 : : * on searches.
20196 : : */
20197 : 193 : dobj = NULL;
20198 : :
20199 [ + + ]: 425861 : for (i = 0; i < ntups; i++)
20200 : : {
20201 : : CatalogId objId;
20202 : : CatalogId refobjId;
20203 : : char deptype;
20204 : :
20205 : 425668 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
20206 : 425668 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
20207 : 425668 : refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
20208 : 425668 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
20209 : 425668 : deptype = *(PQgetvalue(res, i, i_deptype));
20210 : :
20211 [ + + ]: 425668 : if (dobj == NULL ||
20212 [ + + ]: 399384 : dobj->catId.tableoid != objId.tableoid ||
20213 [ + + ]: 397272 : dobj->catId.oid != objId.oid)
20214 : 187666 : dobj = findObjectByCatalogId(objId);
20215 : :
20216 : : /*
20217 : : * Failure to find objects mentioned in pg_depend is not unexpected,
20218 : : * since for example we don't collect info about TOAST tables.
20219 : : */
20220 [ + + ]: 425668 : if (dobj == NULL)
20221 : : {
20222 : : #ifdef NOT_USED
20223 : : pg_log_warning("no referencing object %u %u",
20224 : : objId.tableoid, objId.oid);
20225 : : #endif
20226 : 26962 : continue;
20227 : : }
20228 : :
20229 : 399577 : refdobj = findObjectByCatalogId(refobjId);
20230 : :
20231 [ + + ]: 399577 : if (refdobj == NULL)
20232 : : {
20233 : : #ifdef NOT_USED
20234 : : pg_log_warning("no referenced object %u %u",
20235 : : refobjId.tableoid, refobjId.oid);
20236 : : #endif
20237 : 871 : continue;
20238 : : }
20239 : :
20240 : : /*
20241 : : * For 'x' dependencies, mark the object for later; we still add the
20242 : : * normal dependency, for possible ordering purposes. Currently
20243 : : * pg_dump_sort.c knows to put extensions ahead of all object types
20244 : : * that could possibly depend on them, but this is safer.
20245 : : */
20246 [ + + ]: 398706 : if (deptype == 'x')
20247 : 44 : dobj->depends_on_ext = true;
20248 : :
20249 : : /*
20250 : : * Ordinarily, table rowtypes have implicit dependencies on their
20251 : : * tables. However, for a composite type the implicit dependency goes
20252 : : * the other way in pg_depend; which is the right thing for DROP but
20253 : : * it doesn't produce the dependency ordering we need. So in that one
20254 : : * case, we reverse the direction of the dependency.
20255 : : */
20256 [ + + ]: 398706 : if (deptype == 'i' &&
20257 [ + + ]: 113577 : dobj->objType == DO_TABLE &&
20258 [ + + ]: 1304 : refdobj->objType == DO_TYPE)
20259 : 185 : addObjectDependency(refdobj, dobj->dumpId);
20260 : : else
20261 : : /* normal case */
20262 : 398521 : addObjectDependency(dobj, refdobj->dumpId);
20263 : : }
20264 : :
20265 : 193 : PQclear(res);
20266 : :
20267 : 193 : destroyPQExpBuffer(query);
20268 : 193 : }
20269 : :
20270 : :
20271 : : /*
20272 : : * createBoundaryObjects - create dummy DumpableObjects to represent
20273 : : * dump section boundaries.
20274 : : */
20275 : : static DumpableObject *
20276 : 193 : createBoundaryObjects(void)
20277 : : {
20278 : : DumpableObject *dobjs;
20279 : :
20280 : 193 : dobjs = pg_malloc_array(DumpableObject, 2);
20281 : :
20282 : 193 : dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
20283 : 193 : dobjs[0].catId = nilCatalogId;
20284 : 193 : AssignDumpId(dobjs + 0);
20285 : 193 : dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
20286 : :
20287 : 193 : dobjs[1].objType = DO_POST_DATA_BOUNDARY;
20288 : 193 : dobjs[1].catId = nilCatalogId;
20289 : 193 : AssignDumpId(dobjs + 1);
20290 : 193 : dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
20291 : :
20292 : 193 : return dobjs;
20293 : : }
20294 : :
20295 : : /*
20296 : : * addBoundaryDependencies - add dependencies as needed to enforce the dump
20297 : : * section boundaries.
20298 : : */
20299 : : static void
20300 : 193 : addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
20301 : : DumpableObject *boundaryObjs)
20302 : : {
20303 : 193 : DumpableObject *preDataBound = boundaryObjs + 0;
20304 : 193 : DumpableObject *postDataBound = boundaryObjs + 1;
20305 : : int i;
20306 : :
20307 [ + + ]: 732956 : for (i = 0; i < numObjs; i++)
20308 : : {
20309 : 732763 : DumpableObject *dobj = dobjs[i];
20310 : :
20311 : : /*
20312 : : * The classification of object types here must match the SECTION_xxx
20313 : : * values assigned during subsequent ArchiveEntry calls!
20314 : : */
20315 [ + + + + : 732763 : switch (dobj->objType)
+ + + +
- ]
20316 : : {
20317 : 683787 : case DO_NAMESPACE:
20318 : : case DO_EXTENSION:
20319 : : case DO_TYPE:
20320 : : case DO_SHELL_TYPE:
20321 : : case DO_FUNC:
20322 : : case DO_AGG:
20323 : : case DO_OPERATOR:
20324 : : case DO_ACCESS_METHOD:
20325 : : case DO_OPCLASS:
20326 : : case DO_OPFAMILY:
20327 : : case DO_COLLATION:
20328 : : case DO_CONVERSION:
20329 : : case DO_TABLE:
20330 : : case DO_TABLE_ATTACH:
20331 : : case DO_ATTRDEF:
20332 : : case DO_PROCLANG:
20333 : : case DO_CAST:
20334 : : case DO_DUMMY_TYPE:
20335 : : case DO_TSPARSER:
20336 : : case DO_TSDICT:
20337 : : case DO_TSTEMPLATE:
20338 : : case DO_TSCONFIG:
20339 : : case DO_FDW:
20340 : : case DO_FOREIGN_SERVER:
20341 : : case DO_TRANSFORM:
20342 : : /* Pre-data objects: must come before the pre-data boundary */
20343 : 683787 : addObjectDependency(preDataBound, dobj->dumpId);
20344 : 683787 : break;
20345 : 5003 : case DO_TABLE_DATA:
20346 : : case DO_SEQUENCE_SET:
20347 : : case DO_LARGE_OBJECT:
20348 : : case DO_LARGE_OBJECT_DATA:
20349 : : /* Data objects: must come between the boundaries */
20350 : 5003 : addObjectDependency(dobj, preDataBound->dumpId);
20351 : 5003 : addObjectDependency(postDataBound, dobj->dumpId);
20352 : 5003 : break;
20353 : 6247 : case DO_INDEX:
20354 : : case DO_INDEX_ATTACH:
20355 : : case DO_STATSEXT:
20356 : : case DO_REFRESH_MATVIEW:
20357 : : case DO_TRIGGER:
20358 : : case DO_EVENT_TRIGGER:
20359 : : case DO_DEFAULT_ACL:
20360 : : case DO_POLICY:
20361 : : case DO_PUBLICATION:
20362 : : case DO_PUBLICATION_REL:
20363 : : case DO_PUBLICATION_TABLE_IN_SCHEMA:
20364 : : case DO_SUBSCRIPTION:
20365 : : case DO_SUBSCRIPTION_REL:
20366 : : /* Post-data objects: must come after the post-data boundary */
20367 : 6247 : addObjectDependency(dobj, postDataBound->dumpId);
20368 : 6247 : break;
20369 : 31088 : case DO_RULE:
20370 : : /* Rules are post-data, but only if dumped separately */
20371 [ + + ]: 31088 : if (((RuleInfo *) dobj)->separate)
20372 : 659 : addObjectDependency(dobj, postDataBound->dumpId);
20373 : 31088 : break;
20374 : 2624 : case DO_CONSTRAINT:
20375 : : case DO_FK_CONSTRAINT:
20376 : : /* Constraints are post-data, but only if dumped separately */
20377 [ + + ]: 2624 : if (((ConstraintInfo *) dobj)->separate)
20378 : 1880 : addObjectDependency(dobj, postDataBound->dumpId);
20379 : 2624 : break;
20380 : 193 : case DO_PRE_DATA_BOUNDARY:
20381 : : /* nothing to do */
20382 : 193 : break;
20383 : 193 : case DO_POST_DATA_BOUNDARY:
20384 : : /* must come after the pre-data boundary */
20385 : 193 : addObjectDependency(dobj, preDataBound->dumpId);
20386 : 193 : break;
20387 : 3628 : case DO_REL_STATS:
20388 : : /* stats section varies by parent object type, DATA or POST */
20389 [ + + ]: 3628 : if (((RelStatsInfo *) dobj)->section == SECTION_DATA)
20390 : : {
20391 : 2365 : addObjectDependency(dobj, preDataBound->dumpId);
20392 : 2365 : addObjectDependency(postDataBound, dobj->dumpId);
20393 : : }
20394 : : else
20395 : 1263 : addObjectDependency(dobj, postDataBound->dumpId);
20396 : 3628 : break;
20397 : : }
20398 : : }
20399 : 193 : }
20400 : :
20401 : :
20402 : : /*
20403 : : * BuildArchiveDependencies - create dependency data for archive TOC entries
20404 : : *
20405 : : * The raw dependency data obtained by getDependencies() is not terribly
20406 : : * useful in an archive dump, because in many cases there are dependency
20407 : : * chains linking through objects that don't appear explicitly in the dump.
20408 : : * For example, a view will depend on its _RETURN rule while the _RETURN rule
20409 : : * will depend on other objects --- but the rule will not appear as a separate
20410 : : * object in the dump. We need to adjust the view's dependencies to include
20411 : : * whatever the rule depends on that is included in the dump.
20412 : : *
20413 : : * Just to make things more complicated, there are also "special" dependencies
20414 : : * such as the dependency of a TABLE DATA item on its TABLE, which we must
20415 : : * not rearrange because pg_restore knows that TABLE DATA only depends on
20416 : : * its table. In these cases we must leave the dependencies strictly as-is
20417 : : * even if they refer to not-to-be-dumped objects.
20418 : : *
20419 : : * To handle this, the convention is that "special" dependencies are created
20420 : : * during ArchiveEntry calls, and an archive TOC item that has any such
20421 : : * entries will not be touched here. Otherwise, we recursively search the
20422 : : * DumpableObject data structures to build the correct dependencies for each
20423 : : * archive TOC item.
20424 : : */
20425 : : static void
20426 : 65 : BuildArchiveDependencies(Archive *fout)
20427 : : {
20428 : 65 : ArchiveHandle *AH = (ArchiveHandle *) fout;
20429 : : TocEntry *te;
20430 : :
20431 : : /* Scan all TOC entries in the archive */
20432 [ + + ]: 7708 : for (te = AH->toc->next; te != AH->toc; te = te->next)
20433 : : {
20434 : : DumpableObject *dobj;
20435 : : DumpId *dependencies;
20436 : : int nDeps;
20437 : : int allocDeps;
20438 : :
20439 : : /* No need to process entries that will not be dumped */
20440 [ + + ]: 7643 : if (te->reqs == 0)
20441 : 3815 : continue;
20442 : : /* Ignore entries that already have "special" dependencies */
20443 [ + + ]: 7635 : if (te->nDeps > 0)
20444 : 3305 : continue;
20445 : : /* Otherwise, look up the item's original DumpableObject, if any */
20446 : 4330 : dobj = findObjectByDumpId(te->dumpId);
20447 [ + + ]: 4330 : if (dobj == NULL)
20448 : 392 : continue;
20449 : : /* No work if it has no dependencies */
20450 [ + + ]: 3938 : if (dobj->nDeps <= 0)
20451 : 110 : continue;
20452 : : /* Set up work array */
20453 : 3828 : allocDeps = 64;
20454 : 3828 : dependencies = pg_malloc_array(DumpId, allocDeps);
20455 : 3828 : nDeps = 0;
20456 : : /* Recursively find all dumpable dependencies */
20457 : 3828 : findDumpableDependencies(AH, dobj,
20458 : : &dependencies, &nDeps, &allocDeps);
20459 : : /* And save 'em ... */
20460 [ + + ]: 3828 : if (nDeps > 0)
20461 : : {
20462 : 2896 : dependencies = pg_realloc_array(dependencies, DumpId, nDeps);
20463 : 2896 : te->dependencies = dependencies;
20464 : 2896 : te->nDeps = nDeps;
20465 : : }
20466 : : else
20467 : 932 : pg_free(dependencies);
20468 : : }
20469 : 65 : }
20470 : :
20471 : : /* Recursive search subroutine for BuildArchiveDependencies */
20472 : : static void
20473 : 9122 : findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
20474 : : DumpId **dependencies, int *nDeps, int *allocDeps)
20475 : : {
20476 : : int i;
20477 : :
20478 : : /*
20479 : : * Ignore section boundary objects: if we search through them, we'll
20480 : : * report lots of bogus dependencies.
20481 : : */
20482 [ + + ]: 9122 : if (dobj->objType == DO_PRE_DATA_BOUNDARY ||
20483 [ + + ]: 9101 : dobj->objType == DO_POST_DATA_BOUNDARY)
20484 : 1632 : return;
20485 : :
20486 [ + + ]: 18895 : for (i = 0; i < dobj->nDeps; i++)
20487 : : {
20488 : 11405 : DumpId depid = dobj->dependencies[i];
20489 : :
20490 [ + + ]: 11405 : if (TocIDRequired(AH, depid) != 0)
20491 : : {
20492 : : /* Object will be dumped, so just reference it as a dependency */
20493 [ - + ]: 6111 : if (*nDeps >= *allocDeps)
20494 : : {
20495 : 0 : *allocDeps *= 2;
20496 : 0 : *dependencies = pg_realloc_array(*dependencies, DumpId, *allocDeps);
20497 : : }
20498 : 6111 : (*dependencies)[*nDeps] = depid;
20499 : 6111 : (*nDeps)++;
20500 : : }
20501 : : else
20502 : : {
20503 : : /*
20504 : : * Object will not be dumped, so recursively consider its deps. We
20505 : : * rely on the assumption that sortDumpableObjects already broke
20506 : : * any dependency loops, else we might recurse infinitely.
20507 : : */
20508 : 5294 : DumpableObject *otherdobj = findObjectByDumpId(depid);
20509 : :
20510 [ + - ]: 5294 : if (otherdobj)
20511 : 5294 : findDumpableDependencies(AH, otherdobj,
20512 : : dependencies, nDeps, allocDeps);
20513 : : }
20514 : : }
20515 : : }
20516 : :
20517 : :
20518 : : /*
20519 : : * getFormattedTypeName - retrieve a nicely-formatted type name for the
20520 : : * given type OID.
20521 : : *
20522 : : * This does not guarantee to schema-qualify the output, so it should not
20523 : : * be used to create the target object name for CREATE or ALTER commands.
20524 : : *
20525 : : * Note that the result is cached and must not be freed by the caller.
20526 : : */
20527 : : static const char *
20528 : 2359 : getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
20529 : : {
20530 : : TypeInfo *typeInfo;
20531 : : char *result;
20532 : : PQExpBuffer query;
20533 : : PGresult *res;
20534 : :
20535 [ - + ]: 2359 : if (oid == 0)
20536 : : {
20537 [ # # ]: 0 : if ((opts & zeroAsStar) != 0)
20538 : 0 : return "*";
20539 [ # # ]: 0 : else if ((opts & zeroAsNone) != 0)
20540 : 0 : return "NONE";
20541 : : }
20542 : :
20543 : : /* see if we have the result cached in the type's TypeInfo record */
20544 : 2359 : typeInfo = findTypeByOid(oid);
20545 [ + - + + ]: 2359 : if (typeInfo && typeInfo->ftypname)
20546 : 1878 : return typeInfo->ftypname;
20547 : :
20548 : 481 : query = createPQExpBuffer();
20549 : 481 : appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
20550 : : oid);
20551 : :
20552 : 481 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
20553 : :
20554 : : /* result of format_type is already quoted */
20555 : 481 : result = pg_strdup(PQgetvalue(res, 0, 0));
20556 : :
20557 : 481 : PQclear(res);
20558 : 481 : destroyPQExpBuffer(query);
20559 : :
20560 : : /*
20561 : : * Cache the result for re-use in later requests, if possible. If we
20562 : : * don't have a TypeInfo for the type, the string will be leaked once the
20563 : : * caller is done with it ... but that case really should not happen, so
20564 : : * leaking if it does seems acceptable.
20565 : : */
20566 [ + - ]: 481 : if (typeInfo)
20567 : 481 : typeInfo->ftypname = result;
20568 : :
20569 : 481 : return result;
20570 : : }
20571 : :
20572 : : /*
20573 : : * Return a column list clause for the given relation.
20574 : : *
20575 : : * Special case: if there are no undropped columns in the relation, return
20576 : : * "", not an invalid "()" column list.
20577 : : */
20578 : : static const char *
20579 : 8596 : fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer)
20580 : : {
20581 : 8596 : int numatts = ti->numatts;
20582 : 8596 : char **attnames = ti->attnames;
20583 : 8596 : bool *attisdropped = ti->attisdropped;
20584 : 8596 : char *attgenerated = ti->attgenerated;
20585 : : bool needComma;
20586 : : int i;
20587 : :
20588 : 8596 : appendPQExpBufferChar(buffer, '(');
20589 : 8596 : needComma = false;
20590 [ + + ]: 41344 : for (i = 0; i < numatts; i++)
20591 : : {
20592 [ + + ]: 32748 : if (attisdropped[i])
20593 : 602 : continue;
20594 [ + + ]: 32146 : if (attgenerated[i])
20595 : 1200 : continue;
20596 [ + + ]: 30946 : if (needComma)
20597 : 22586 : appendPQExpBufferStr(buffer, ", ");
20598 : 30946 : appendPQExpBufferStr(buffer, fmtId(attnames[i]));
20599 : 30946 : needComma = true;
20600 : : }
20601 : :
20602 [ + + ]: 8596 : if (!needComma)
20603 : 236 : return ""; /* no undropped columns */
20604 : :
20605 : 8360 : appendPQExpBufferChar(buffer, ')');
20606 : 8360 : return buffer->data;
20607 : : }
20608 : :
20609 : : /*
20610 : : * Check if a reloptions array is nonempty.
20611 : : */
20612 : : static bool
20613 : 14127 : nonemptyReloptions(const char *reloptions)
20614 : : {
20615 : : /* Don't want to print it if it's just "{}" */
20616 [ + - + + ]: 14127 : return (reloptions != NULL && strlen(reloptions) > 2);
20617 : : }
20618 : :
20619 : : /*
20620 : : * Format a reloptions array and append it to the given buffer.
20621 : : *
20622 : : * "prefix" is prepended to the option names; typically it's "" or "toast.".
20623 : : */
20624 : : static void
20625 : 223 : appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
20626 : : const char *prefix, Archive *fout)
20627 : : {
20628 : : bool res;
20629 : :
20630 : 223 : res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
20631 : 223 : fout->std_strings);
20632 [ - + ]: 223 : if (!res)
20633 : 0 : pg_log_warning("could not parse %s array", "reloptions");
20634 : 223 : }
20635 : :
20636 : : /*
20637 : : * read_dump_filters - retrieve object identifier patterns from file
20638 : : *
20639 : : * Parse the specified filter file for include and exclude patterns, and add
20640 : : * them to the relevant lists. If the filename is "-" then filters will be
20641 : : * read from STDIN rather than a file.
20642 : : */
20643 : : static void
20644 : 26 : read_dump_filters(const char *filename, DumpOptions *dopt)
20645 : : {
20646 : : FilterStateData fstate;
20647 : : char *objname;
20648 : : FilterCommandType comtype;
20649 : : FilterObjectType objtype;
20650 : :
20651 : 26 : filter_init(&fstate, filename, exit_nicely);
20652 : :
20653 [ + + ]: 84 : while (filter_read_item(&fstate, &objname, &comtype, &objtype))
20654 : : {
20655 [ + + ]: 33 : if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
20656 : : {
20657 [ - - + + : 17 : switch (objtype)
+ + + - ]
20658 : : {
20659 : 0 : case FILTER_OBJECT_TYPE_NONE:
20660 : 0 : break;
20661 : 0 : case FILTER_OBJECT_TYPE_DATABASE:
20662 : : case FILTER_OBJECT_TYPE_FUNCTION:
20663 : : case FILTER_OBJECT_TYPE_INDEX:
20664 : : case FILTER_OBJECT_TYPE_TABLE_DATA:
20665 : : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20666 : : case FILTER_OBJECT_TYPE_TRIGGER:
20667 : 0 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20668 : : "include",
20669 : : filter_object_type_name(objtype));
20670 : 0 : exit_nicely(1);
20671 : : break; /* unreachable */
20672 : :
20673 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20674 : 1 : simple_string_list_append(&extension_include_patterns, objname);
20675 : 1 : break;
20676 : 1 : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20677 : 1 : simple_string_list_append(&foreign_servers_include_patterns, objname);
20678 : 1 : break;
20679 : 1 : case FILTER_OBJECT_TYPE_SCHEMA:
20680 : 1 : simple_string_list_append(&schema_include_patterns, objname);
20681 : 1 : dopt->include_everything = false;
20682 : 1 : break;
20683 : 13 : case FILTER_OBJECT_TYPE_TABLE:
20684 : 13 : simple_string_list_append(&table_include_patterns, objname);
20685 : 13 : dopt->include_everything = false;
20686 : 13 : break;
20687 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20688 : 1 : simple_string_list_append(&table_include_patterns_and_children,
20689 : : objname);
20690 : 1 : dopt->include_everything = false;
20691 : 1 : break;
20692 : : }
20693 : : }
20694 [ + + ]: 16 : else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
20695 : : {
20696 [ - + + + : 9 : switch (objtype)
+ + + +
- ]
20697 : : {
20698 : 0 : case FILTER_OBJECT_TYPE_NONE:
20699 : 0 : break;
20700 : 1 : case FILTER_OBJECT_TYPE_DATABASE:
20701 : : case FILTER_OBJECT_TYPE_FUNCTION:
20702 : : case FILTER_OBJECT_TYPE_INDEX:
20703 : : case FILTER_OBJECT_TYPE_TRIGGER:
20704 : : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20705 : 1 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20706 : : "exclude",
20707 : : filter_object_type_name(objtype));
20708 : 1 : exit_nicely(1);
20709 : : break;
20710 : :
20711 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20712 : 1 : simple_string_list_append(&extension_exclude_patterns, objname);
20713 : 1 : break;
20714 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA:
20715 : 1 : simple_string_list_append(&tabledata_exclude_patterns,
20716 : : objname);
20717 : 1 : break;
20718 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20719 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
20720 : : objname);
20721 : 1 : break;
20722 : 2 : case FILTER_OBJECT_TYPE_SCHEMA:
20723 : 2 : simple_string_list_append(&schema_exclude_patterns, objname);
20724 : 2 : break;
20725 : 2 : case FILTER_OBJECT_TYPE_TABLE:
20726 : 2 : simple_string_list_append(&table_exclude_patterns, objname);
20727 : 2 : break;
20728 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20729 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
20730 : : objname);
20731 : 1 : break;
20732 : : }
20733 : : }
20734 : : else
20735 : : {
20736 : : Assert(comtype == FILTER_COMMAND_TYPE_NONE);
20737 : : Assert(objtype == FILTER_OBJECT_TYPE_NONE);
20738 : : }
20739 : :
20740 [ + + ]: 32 : if (objname)
20741 : 25 : free(objname);
20742 : : }
20743 : :
20744 : 22 : filter_free(&fstate);
20745 : 22 : }
|