Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_dump.c
4 : : * pg_dump is a utility for dumping out a postgres database
5 : : * into a script file.
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * pg_dump will read the system catalogs in a database and dump out a
11 : : * script that reproduces the schema in terms of SQL that is understood
12 : : * by PostgreSQL
13 : : *
14 : : * Note that pg_dump runs in a transaction-snapshot mode transaction,
15 : : * so it sees a consistent snapshot of the database including system
16 : : * catalogs. However, it relies in part on various specialized backend
17 : : * functions like pg_get_indexdef(), and those things tend to look at
18 : : * the currently committed state. So it is possible to get 'cache
19 : : * lookup failed' error if someone performs DDL changes while a dump is
20 : : * happening. The window for this sort of thing is from the acquisition
21 : : * of the transaction snapshot to getSchemaData() (when pg_dump acquires
22 : : * AccessShareLock on every table it intends to dump). It isn't very large,
23 : : * but it can happen.
24 : : *
25 : : * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
26 : : *
27 : : * IDENTIFICATION
28 : : * src/bin/pg_dump/pg_dump.c
29 : : *
30 : : *-------------------------------------------------------------------------
31 : : */
32 : : #include "postgres_fe.h"
33 : :
34 : : #include <unistd.h>
35 : : #include <ctype.h>
36 : : #include <limits.h>
37 : : #ifdef HAVE_TERMIOS_H
38 : : #include <termios.h>
39 : : #endif
40 : :
41 : : #include "access/attnum.h"
42 : : #include "access/sysattr.h"
43 : : #include "access/transam.h"
44 : : #include "access/xlogdefs.h"
45 : : #include "catalog/pg_aggregate_d.h"
46 : : #include "catalog/pg_am_d.h"
47 : : #include "catalog/pg_attribute_d.h"
48 : : #include "catalog/pg_authid_d.h"
49 : : #include "catalog/pg_cast_d.h"
50 : : #include "catalog/pg_class_d.h"
51 : : #include "catalog/pg_constraint_d.h"
52 : : #include "catalog/pg_default_acl_d.h"
53 : : #include "catalog/pg_largeobject_d.h"
54 : : #include "catalog/pg_largeobject_metadata_d.h"
55 : : #include "catalog/pg_proc_d.h"
56 : : #include "catalog/pg_publication_d.h"
57 : : #include "catalog/pg_shdepend_d.h"
58 : : #include "catalog/pg_subscription_d.h"
59 : : #include "catalog/pg_type_d.h"
60 : : #include "common/connect.h"
61 : : #include "common/int.h"
62 : : #include "common/relpath.h"
63 : : #include "common/shortest_dec.h"
64 : : #include "compress_io.h"
65 : : #include "dumputils.h"
66 : : #include "fe_utils/option_utils.h"
67 : : #include "fe_utils/string_utils.h"
68 : : #include "filter.h"
69 : : #include "getopt_long.h"
70 : : #include "libpq/libpq-fs.h"
71 : : #include "parallel.h"
72 : : #include "pg_backup_db.h"
73 : : #include "pg_backup_utils.h"
74 : : #include "pg_dump.h"
75 : : #include "statistics/statistics_format.h"
76 : : #include "storage/block.h"
77 : :
78 : : typedef struct
79 : : {
80 : : Oid roleoid; /* role's OID */
81 : : const char *rolename; /* role's name */
82 : : } RoleNameItem;
83 : :
84 : : typedef struct
85 : : {
86 : : const char *descr; /* comment for an object */
87 : : Oid classoid; /* object class (catalog OID) */
88 : : Oid objoid; /* object OID */
89 : : int objsubid; /* subobject (table column #) */
90 : : } CommentItem;
91 : :
92 : : typedef struct
93 : : {
94 : : const char *provider; /* label provider of this security label */
95 : : const char *label; /* security label for an object */
96 : : Oid classoid; /* object class (catalog OID) */
97 : : Oid objoid; /* object OID */
98 : : int objsubid; /* subobject (table column #) */
99 : : } SecLabelItem;
100 : :
101 : : typedef struct
102 : : {
103 : : Oid oid; /* object OID */
104 : : char relkind; /* object kind */
105 : : RelFileNumber relfilenumber; /* object filenode */
106 : : Oid toast_oid; /* toast table OID */
107 : : RelFileNumber toast_relfilenumber; /* toast table filenode */
108 : : Oid toast_chunk_id_typoid; /* type of chunk_id attribute */
109 : : Oid toast_index_oid; /* toast table index OID */
110 : : RelFileNumber toast_index_relfilenumber; /* toast table index filenode */
111 : : } BinaryUpgradeClassOidItem;
112 : :
113 : : /* sequence types */
114 : : typedef enum SeqType
115 : : {
116 : : SEQTYPE_SMALLINT,
117 : : SEQTYPE_INTEGER,
118 : : SEQTYPE_BIGINT,
119 : : } SeqType;
120 : :
121 : : static const char *const SeqTypeNames[] =
122 : : {
123 : : [SEQTYPE_SMALLINT] = "smallint",
124 : : [SEQTYPE_INTEGER] = "integer",
125 : : [SEQTYPE_BIGINT] = "bigint",
126 : : };
127 : :
128 : : StaticAssertDecl(lengthof(SeqTypeNames) == (SEQTYPE_BIGINT + 1),
129 : : "array length mismatch");
130 : :
131 : : typedef struct
132 : : {
133 : : Oid oid; /* sequence OID */
134 : : SeqType seqtype; /* data type of sequence */
135 : : bool cycled; /* whether sequence cycles */
136 : : int64 minv; /* minimum value */
137 : : int64 maxv; /* maximum value */
138 : : int64 startv; /* start value */
139 : : int64 incby; /* increment value */
140 : : int64 cache; /* cache size */
141 : : int64 last_value; /* last value of sequence */
142 : : bool is_called; /* whether nextval advances before returning */
143 : : bool null_seqtuple; /* did we get nulls for the sequence data? */
144 : : } SequenceItem;
145 : :
146 : : typedef enum OidOptions
147 : : {
148 : : zeroIsError = 1,
149 : : zeroAsStar = 2,
150 : : zeroAsNone = 4,
151 : : } OidOptions;
152 : :
153 : : /* global decls */
154 : : static bool dosync = true; /* Issue fsync() to make dump durable on disk. */
155 : :
156 : : static Oid g_last_builtin_oid; /* value of the last builtin oid */
157 : :
158 : : /* The specified names/patterns should to match at least one entity */
159 : : static int strict_names = 0;
160 : :
161 : : static pg_compress_algorithm compression_algorithm = PG_COMPRESSION_NONE;
162 : :
163 : : /*
164 : : * Object inclusion/exclusion lists
165 : : *
166 : : * The string lists record the patterns given by command-line switches,
167 : : * which we then convert to lists of OIDs of matching objects.
168 : : */
169 : : static SimpleStringList schema_include_patterns = {NULL, NULL};
170 : : static SimpleOidList schema_include_oids = {NULL, NULL};
171 : : static SimpleStringList schema_exclude_patterns = {NULL, NULL};
172 : : static SimpleOidList schema_exclude_oids = {NULL, NULL};
173 : :
174 : : static SimpleStringList table_include_patterns = {NULL, NULL};
175 : : static SimpleStringList table_include_patterns_and_children = {NULL, NULL};
176 : : static SimpleOidList table_include_oids = {NULL, NULL};
177 : : static SimpleStringList table_exclude_patterns = {NULL, NULL};
178 : : static SimpleStringList table_exclude_patterns_and_children = {NULL, NULL};
179 : : static SimpleOidList table_exclude_oids = {NULL, NULL};
180 : : static SimpleStringList tabledata_exclude_patterns = {NULL, NULL};
181 : : static SimpleStringList tabledata_exclude_patterns_and_children = {NULL, NULL};
182 : : static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
183 : :
184 : : static SimpleStringList foreign_servers_include_patterns = {NULL, NULL};
185 : : static SimpleOidList foreign_servers_include_oids = {NULL, NULL};
186 : :
187 : : static SimpleStringList extension_include_patterns = {NULL, NULL};
188 : : static SimpleOidList extension_include_oids = {NULL, NULL};
189 : :
190 : : static SimpleStringList extension_exclude_patterns = {NULL, NULL};
191 : : static SimpleOidList extension_exclude_oids = {NULL, NULL};
192 : :
193 : : static const CatalogId nilCatalogId = {0, 0};
194 : :
195 : : /* override for standard extra_float_digits setting */
196 : : static bool have_extra_float_digits = false;
197 : : static int extra_float_digits;
198 : :
199 : : /* sorted table of role names */
200 : : static RoleNameItem *rolenames = NULL;
201 : : static int nrolenames = 0;
202 : :
203 : : /* sorted table of comments */
204 : : static CommentItem *comments = NULL;
205 : : static int ncomments = 0;
206 : :
207 : : /* sorted table of security labels */
208 : : static SecLabelItem *seclabels = NULL;
209 : : static int nseclabels = 0;
210 : :
211 : : /* sorted table of pg_class information for binary upgrade */
212 : : static BinaryUpgradeClassOidItem *binaryUpgradeClassOids = NULL;
213 : : static int nbinaryUpgradeClassOids = 0;
214 : :
215 : : /* sorted table of sequences */
216 : : static SequenceItem *sequences = NULL;
217 : : static int nsequences = 0;
218 : :
219 : : /* Maximum number of relations to fetch in a fetchAttributeStats() call. */
220 : : #define MAX_ATTR_STATS_RELS 64
221 : :
222 : : /*
223 : : * The default number of rows per INSERT when
224 : : * --inserts is specified without --rows-per-insert
225 : : */
226 : : #define DUMP_DEFAULT_ROWS_PER_INSERT 1
227 : :
228 : : /*
229 : : * Maximum number of large objects to group into a single ArchiveEntry.
230 : : * At some point we might want to make this user-controllable, but for now
231 : : * a hard-wired setting will suffice.
232 : : */
233 : : #define MAX_BLOBS_PER_ARCHIVE_ENTRY 1000
234 : :
235 : : /*
236 : : * Macro for producing quoted, schema-qualified name of a dumpable object.
237 : : */
238 : : #define fmtQualifiedDumpable(obj) \
239 : : fmtQualifiedId((obj)->dobj.namespace->dobj.name, \
240 : : (obj)->dobj.name)
241 : :
242 : : static void help(const char *progname);
243 : : static void setup_connection(Archive *AH,
244 : : const char *dumpencoding, const char *dumpsnapshot,
245 : : char *use_role);
246 : : static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
247 : : static void expand_schema_name_patterns(Archive *fout,
248 : : SimpleStringList *patterns,
249 : : SimpleOidList *oids,
250 : : bool strict_names);
251 : : static void expand_extension_name_patterns(Archive *fout,
252 : : SimpleStringList *patterns,
253 : : SimpleOidList *oids,
254 : : bool strict_names);
255 : : static void expand_foreign_server_name_patterns(Archive *fout,
256 : : SimpleStringList *patterns,
257 : : SimpleOidList *oids);
258 : : static void expand_table_name_patterns(Archive *fout,
259 : : SimpleStringList *patterns,
260 : : SimpleOidList *oids,
261 : : bool strict_names,
262 : : bool with_child_tables);
263 : : static void prohibit_crossdb_refs(PGconn *conn, const char *dbname,
264 : : const char *pattern);
265 : :
266 : : static NamespaceInfo *findNamespace(Oid nsoid);
267 : : static void dumpTableData(Archive *fout, const TableDataInfo *tdinfo);
268 : : static void refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo);
269 : : static const char *getRoleName(const char *roleoid_str);
270 : : static void collectRoleNames(Archive *fout);
271 : : static void getAdditionalACLs(Archive *fout);
272 : : static void dumpCommentExtended(Archive *fout, const char *type,
273 : : const char *name, const char *namespace,
274 : : const char *owner, CatalogId catalogId,
275 : : int subid, DumpId dumpId,
276 : : const char *initdb_comment);
277 : : static inline void dumpComment(Archive *fout, const char *type,
278 : : const char *name, const char *namespace,
279 : : const char *owner, CatalogId catalogId,
280 : : int subid, DumpId dumpId);
281 : : static int findComments(Oid classoid, Oid objoid, CommentItem **items);
282 : : static void collectComments(Archive *fout);
283 : : static void dumpSecLabel(Archive *fout, const char *type, const char *name,
284 : : const char *namespace, const char *owner,
285 : : CatalogId catalogId, int subid, DumpId dumpId);
286 : : static int findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items);
287 : : static void collectSecLabels(Archive *fout);
288 : : static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
289 : : static void dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo);
290 : : static void dumpExtension(Archive *fout, const ExtensionInfo *extinfo);
291 : : static void dumpType(Archive *fout, const TypeInfo *tyinfo);
292 : : static void dumpBaseType(Archive *fout, const TypeInfo *tyinfo);
293 : : static void dumpEnumType(Archive *fout, const TypeInfo *tyinfo);
294 : : static void dumpRangeType(Archive *fout, const TypeInfo *tyinfo);
295 : : static void dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo);
296 : : static void dumpDomain(Archive *fout, const TypeInfo *tyinfo);
297 : : static void dumpCompositeType(Archive *fout, const TypeInfo *tyinfo);
298 : : static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
299 : : PGresult *res);
300 : : static void dumpShellType(Archive *fout, const ShellTypeInfo *stinfo);
301 : : static void dumpProcLang(Archive *fout, const ProcLangInfo *plang);
302 : : static void dumpFunc(Archive *fout, const FuncInfo *finfo);
303 : : static void dumpCast(Archive *fout, const CastInfo *cast);
304 : : static void dumpTransform(Archive *fout, const TransformInfo *transform);
305 : : static void dumpOpr(Archive *fout, const OprInfo *oprinfo);
306 : : static void dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo);
307 : : static void dumpOpclass(Archive *fout, const OpclassInfo *opcinfo);
308 : : static void dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo);
309 : : static void dumpCollation(Archive *fout, const CollInfo *collinfo);
310 : : static void dumpConversion(Archive *fout, const ConvInfo *convinfo);
311 : : static void dumpRule(Archive *fout, const RuleInfo *rinfo);
312 : : static void dumpAgg(Archive *fout, const AggInfo *agginfo);
313 : : static void dumpTrigger(Archive *fout, const TriggerInfo *tginfo);
314 : : static void dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo);
315 : : static void dumpTable(Archive *fout, const TableInfo *tbinfo);
316 : : static void dumpTableSchema(Archive *fout, const TableInfo *tbinfo);
317 : : static void dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo);
318 : : static void dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo);
319 : : static void collectSequences(Archive *fout, TableInfo *tblinfo, int numTables);
320 : : static void dumpSequence(Archive *fout, const TableInfo *tbinfo);
321 : : static void dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo);
322 : : static void dumpIndex(Archive *fout, const IndxInfo *indxinfo);
323 : : static void dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo);
324 : : static void dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo);
325 : : static void dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo);
326 : : static void dumpConstraint(Archive *fout, const ConstraintInfo *coninfo);
327 : : static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo);
328 : : static void dumpTSParser(Archive *fout, const TSParserInfo *prsinfo);
329 : : static void dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo);
330 : : static void dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo);
331 : : static void dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo);
332 : : static void dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo);
333 : : static void dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo);
334 : : static void dumpUserMappings(Archive *fout,
335 : : const char *servername, const char *namespace,
336 : : const char *owner, CatalogId catalogId, DumpId dumpId);
337 : : static void dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo);
338 : :
339 : : static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
340 : : const char *type, const char *name, const char *subname,
341 : : const char *nspname, const char *tag, const char *owner,
342 : : const DumpableAcl *dacl);
343 : :
344 : : static void getDependencies(Archive *fout);
345 : : static void BuildArchiveDependencies(Archive *fout);
346 : : static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
347 : : DumpId **dependencies, int *nDeps, int *allocDeps);
348 : :
349 : : static DumpableObject *createBoundaryObjects(void);
350 : : static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
351 : : DumpableObject *boundaryObjs);
352 : :
353 : : static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx);
354 : : static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
355 : : static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
356 : : static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
357 : : static void buildMatViewRefreshDependencies(Archive *fout);
358 : : static void getTableDataFKConstraints(void);
359 : : static void determineNotNullFlags(Archive *fout, PGresult *res, int r,
360 : : TableInfo *tbinfo, int j,
361 : : int i_notnull_name,
362 : : int i_notnull_comment,
363 : : int i_notnull_invalidoid,
364 : : int i_notnull_noinherit,
365 : : int i_notnull_islocal,
366 : : PQExpBuffer *invalidnotnulloids);
367 : : static char *format_function_arguments(const FuncInfo *finfo, const char *funcargs,
368 : : bool is_agg);
369 : : static char *format_function_signature(Archive *fout,
370 : : const FuncInfo *finfo, bool honor_quotes);
371 : : static char *convertRegProcReference(const char *proc);
372 : : static char *getFormattedOperatorName(const char *oproid);
373 : : static char *convertTSFunction(Archive *fout, Oid funcOid);
374 : : static const char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
375 : : static void getLOs(Archive *fout);
376 : : static void dumpLO(Archive *fout, const LoInfo *loinfo);
377 : : static int dumpLOs(Archive *fout, const void *arg);
378 : : static void dumpPolicy(Archive *fout, const PolicyInfo *polinfo);
379 : : static void dumpPublication(Archive *fout, const PublicationInfo *pubinfo);
380 : : static void dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo);
381 : : static void dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo);
382 : : static void dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo);
383 : : static void dumpDatabase(Archive *fout);
384 : : static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
385 : : const char *dbname, Oid dboid);
386 : : static void dumpEncoding(Archive *AH);
387 : : static void dumpStdStrings(Archive *AH);
388 : : static void dumpSearchPath(Archive *AH);
389 : : static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
390 : : PQExpBuffer upgrade_buffer,
391 : : Oid pg_type_oid,
392 : : bool force_array_type,
393 : : bool include_multirange_type);
394 : : static void binary_upgrade_set_type_oids_by_rel(Archive *fout,
395 : : PQExpBuffer upgrade_buffer,
396 : : const TableInfo *tbinfo);
397 : : static void collectBinaryUpgradeClassOids(Archive *fout);
398 : : static void binary_upgrade_set_pg_class_oids(Archive *fout,
399 : : PQExpBuffer upgrade_buffer,
400 : : Oid pg_class_oid);
401 : : static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
402 : : const DumpableObject *dobj,
403 : : const char *objtype,
404 : : const char *objname,
405 : : const char *objnamespace);
406 : : static void binary_upgrade_set_next_attribute_versions(PQExpBuffer upgrade_buffer,
407 : : int numatts,
408 : : const uint64 *attversions);
409 : : static const char *getAttrName(int attrnum, const TableInfo *tblInfo);
410 : : static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
411 : : static bool nonemptyReloptions(const char *reloptions);
412 : : static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
413 : : const char *prefix, Archive *fout);
414 : : static char *get_synchronized_snapshot(Archive *fout);
415 : : static void set_restrict_relation_kind(Archive *AH, const char *value);
416 : : static void setupDumpWorker(Archive *AH);
417 : : static TableInfo *getRootTableInfo(const TableInfo *tbinfo);
418 : : static bool forcePartitionRootLoad(const TableInfo *tbinfo);
419 : : static void read_dump_filters(const char *filename, DumpOptions *dopt);
420 : :
421 : :
422 : : int
423 : 338 : main(int argc, char **argv)
424 : : {
425 : : int c;
426 : 338 : const char *filename = NULL;
427 : 338 : const char *format = "p";
428 : : TableInfo *tblinfo;
429 : : int numTables;
430 : : DumpableObject **dobjs;
431 : : int numObjs;
432 : : DumpableObject *boundaryObjs;
433 : : int i;
434 : : int optindex;
435 : : RestoreOptions *ropt;
436 : : Archive *fout; /* the script file */
437 : 338 : bool g_verbose = false;
438 : 338 : const char *dumpencoding = NULL;
439 : 338 : const char *dumpsnapshot = NULL;
440 : 338 : char *use_role = NULL;
441 : 338 : int numWorkers = 1;
442 : 338 : int plainText = 0;
443 : 338 : ArchiveFormat archiveFormat = archUnknown;
444 : : ArchiveMode archiveMode;
445 : 338 : pg_compress_specification compression_spec = {0};
446 : 338 : char *compression_detail = NULL;
447 : 338 : char *compression_algorithm_str = "none";
448 : 338 : char *error_detail = NULL;
449 : 338 : bool user_compression_defined = false;
450 : 338 : DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
451 : 338 : bool data_only = false;
452 : 338 : bool schema_only = false;
453 : 338 : bool statistics_only = false;
454 : 338 : bool with_statistics = false;
455 : 338 : bool no_data = false;
456 : 338 : bool no_schema = false;
457 : 338 : bool no_statistics = false;
458 : :
459 : : static DumpOptions dopt;
460 : :
461 : : static struct option long_options[] = {
462 : : {"data-only", no_argument, NULL, 'a'},
463 : : {"blobs", no_argument, NULL, 'b'},
464 : : {"large-objects", no_argument, NULL, 'b'},
465 : : {"no-blobs", no_argument, NULL, 'B'},
466 : : {"no-large-objects", no_argument, NULL, 'B'},
467 : : {"clean", no_argument, NULL, 'c'},
468 : : {"create", no_argument, NULL, 'C'},
469 : : {"dbname", required_argument, NULL, 'd'},
470 : : {"extension", required_argument, NULL, 'e'},
471 : : {"file", required_argument, NULL, 'f'},
472 : : {"format", required_argument, NULL, 'F'},
473 : : {"host", required_argument, NULL, 'h'},
474 : : {"jobs", 1, NULL, 'j'},
475 : : {"no-reconnect", no_argument, NULL, 'R'},
476 : : {"no-owner", no_argument, NULL, 'O'},
477 : : {"port", required_argument, NULL, 'p'},
478 : : {"schema", required_argument, NULL, 'n'},
479 : : {"exclude-schema", required_argument, NULL, 'N'},
480 : : {"schema-only", no_argument, NULL, 's'},
481 : : {"superuser", required_argument, NULL, 'S'},
482 : : {"table", required_argument, NULL, 't'},
483 : : {"exclude-table", required_argument, NULL, 'T'},
484 : : {"no-password", no_argument, NULL, 'w'},
485 : : {"password", no_argument, NULL, 'W'},
486 : : {"username", required_argument, NULL, 'U'},
487 : : {"verbose", no_argument, NULL, 'v'},
488 : : {"no-privileges", no_argument, NULL, 'x'},
489 : : {"no-acl", no_argument, NULL, 'x'},
490 : : {"compress", required_argument, NULL, 'Z'},
491 : : {"encoding", required_argument, NULL, 'E'},
492 : : {"help", no_argument, NULL, '?'},
493 : : {"version", no_argument, NULL, 'V'},
494 : :
495 : : /*
496 : : * the following options don't have an equivalent short option letter
497 : : */
498 : : {"attribute-inserts", no_argument, &dopt.column_inserts, 1},
499 : : {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1},
500 : : {"column-inserts", no_argument, &dopt.column_inserts, 1},
501 : : {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1},
502 : : {"disable-triggers", no_argument, &dopt.disable_triggers, 1},
503 : : {"enable-row-security", no_argument, &dopt.enable_row_security, 1},
504 : : {"exclude-table-data", required_argument, NULL, 4},
505 : : {"extra-float-digits", required_argument, NULL, 8},
506 : : {"if-exists", no_argument, &dopt.if_exists, 1},
507 : : {"inserts", no_argument, NULL, 9},
508 : : {"lock-wait-timeout", required_argument, NULL, 2},
509 : : {"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1},
510 : : {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
511 : : {"quote-all-identifiers", no_argument, "e_all_identifiers, 1},
512 : : {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
513 : : {"role", required_argument, NULL, 3},
514 : : {"section", required_argument, NULL, 5},
515 : : {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
516 : : {"snapshot", required_argument, NULL, 6},
517 : : {"statistics", no_argument, NULL, 22},
518 : : {"statistics-only", no_argument, NULL, 18},
519 : : {"strict-names", no_argument, &strict_names, 1},
520 : : {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1},
521 : : {"no-comments", no_argument, &dopt.no_comments, 1},
522 : : {"no-data", no_argument, NULL, 19},
523 : : {"no-policies", no_argument, &dopt.no_policies, 1},
524 : : {"no-publications", no_argument, &dopt.no_publications, 1},
525 : : {"no-schema", no_argument, NULL, 20},
526 : : {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
527 : : {"no-statistics", no_argument, NULL, 21},
528 : : {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
529 : : {"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
530 : : {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
531 : : {"no-sync", no_argument, NULL, 7},
532 : : {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
533 : : {"rows-per-insert", required_argument, NULL, 10},
534 : : {"include-foreign-data", required_argument, NULL, 11},
535 : : {"table-and-children", required_argument, NULL, 12},
536 : : {"exclude-table-and-children", required_argument, NULL, 13},
537 : : {"exclude-table-data-and-children", required_argument, NULL, 14},
538 : : {"sync-method", required_argument, NULL, 15},
539 : : {"filter", required_argument, NULL, 16},
540 : : {"exclude-extension", required_argument, NULL, 17},
541 : : {"sequence-data", no_argument, &dopt.sequence_data, 1},
542 : : {"restrict-key", required_argument, NULL, 25},
543 : :
544 : : {NULL, 0, NULL, 0}
545 : : };
546 : :
547 : 338 : pg_logging_init(argv[0]);
548 : 338 : pg_logging_set_level(PG_LOG_WARNING);
549 : 338 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
550 : :
551 : : /*
552 : : * Initialize what we need for parallel execution, especially for thread
553 : : * support on Windows.
554 : : */
555 : 338 : init_parallel_dump_utils();
556 : :
557 : 338 : progname = get_progname(argv[0]);
558 : :
559 [ + - ]: 338 : if (argc > 1)
560 : : {
561 [ + + - + ]: 338 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
562 : : {
563 : 1 : help(progname);
564 : 1 : exit_nicely(0);
565 : : }
566 [ + + + + ]: 337 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
567 : : {
568 : 77 : puts("pg_dump (PostgreSQL) " PG_VERSION);
569 : 77 : exit_nicely(0);
570 : : }
571 : : }
572 : :
573 : 260 : InitDumpOptions(&dopt);
574 : :
575 : 1498 : while ((c = getopt_long(argc, argv, "abBcCd:e:E:f:F:h:j:n:N:Op:RsS:t:T:U:vwWxXZ:",
576 [ + + ]: 1498 : long_options, &optindex)) != -1)
577 : : {
578 [ + + + + : 1246 : switch (c)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + - +
+ + + + +
+ + - + +
+ + + + +
+ + ]
579 : : {
580 : 9 : case 'a': /* Dump data only */
581 : 9 : data_only = true;
582 : 9 : break;
583 : :
584 : 1 : case 'b': /* Dump LOs */
585 : 1 : dopt.outputLOs = true;
586 : 1 : break;
587 : :
588 : 2 : case 'B': /* Don't dump LOs */
589 : 2 : dopt.dontOutputLOs = true;
590 : 2 : break;
591 : :
592 : 18 : case 'c': /* clean (i.e., drop) schema prior to create */
593 : 18 : dopt.outputClean = 1;
594 : 18 : break;
595 : :
596 : 35 : case 'C': /* Create DB */
597 : 35 : dopt.outputCreateDB = 1;
598 : 35 : break;
599 : :
600 : 5 : case 'd': /* database name */
601 : 5 : dopt.cparams.dbname = pg_strdup(optarg);
602 : 5 : break;
603 : :
604 : 4 : case 'e': /* include extension(s) */
605 : 4 : simple_string_list_append(&extension_include_patterns, optarg);
606 : 4 : dopt.include_everything = false;
607 : 4 : break;
608 : :
609 : 3 : case 'E': /* Dump encoding */
610 : 3 : dumpencoding = pg_strdup(optarg);
611 : 3 : break;
612 : :
613 : 213 : case 'f':
614 : 213 : filename = pg_strdup(optarg);
615 : 213 : break;
616 : :
617 : 132 : case 'F':
618 : 132 : format = pg_strdup(optarg);
619 : 132 : break;
620 : :
621 : 42 : case 'h': /* server host */
622 : 42 : dopt.cparams.pghost = pg_strdup(optarg);
623 : 42 : break;
624 : :
625 : 11 : case 'j': /* number of dump jobs */
626 [ + + ]: 11 : if (!option_parse_int(optarg, "-j/--jobs", 1,
627 : : PG_MAX_JOBS,
628 : : &numWorkers))
629 : 1 : exit_nicely(1);
630 : 10 : break;
631 : :
632 : 18 : case 'n': /* include schema(s) */
633 : 18 : simple_string_list_append(&schema_include_patterns, optarg);
634 : 18 : dopt.include_everything = false;
635 : 18 : break;
636 : :
637 : 1 : case 'N': /* exclude schema(s) */
638 : 1 : simple_string_list_append(&schema_exclude_patterns, optarg);
639 : 1 : break;
640 : :
641 : 2 : case 'O': /* Don't reconnect to match owner */
642 : 2 : dopt.outputNoOwner = 1;
643 : 2 : break;
644 : :
645 : 81 : case 'p': /* server port */
646 : 81 : dopt.cparams.pgport = pg_strdup(optarg);
647 : 81 : break;
648 : :
649 : 2 : case 'R':
650 : : /* no-op, still accepted for backwards compatibility */
651 : 2 : break;
652 : :
653 : 7 : case 's': /* dump schema only */
654 : 7 : schema_only = true;
655 : 7 : break;
656 : :
657 : 1 : case 'S': /* Username for superuser in plain text output */
658 : 1 : dopt.outputSuperuser = pg_strdup(optarg);
659 : 1 : break;
660 : :
661 : 8 : case 't': /* include table(s) */
662 : 8 : simple_string_list_append(&table_include_patterns, optarg);
663 : 8 : dopt.include_everything = false;
664 : 8 : break;
665 : :
666 : 4 : case 'T': /* exclude table(s) */
667 : 4 : simple_string_list_append(&table_exclude_patterns, optarg);
668 : 4 : break;
669 : :
670 : 44 : case 'U':
671 : 44 : dopt.cparams.username = pg_strdup(optarg);
672 : 44 : break;
673 : :
674 : 12 : case 'v': /* verbose */
675 : 12 : g_verbose = true;
676 : 12 : pg_logging_increase_verbosity();
677 : 12 : break;
678 : :
679 : 1 : case 'w':
680 : 1 : dopt.cparams.promptPassword = TRI_NO;
681 : 1 : break;
682 : :
683 : 0 : case 'W':
684 : 0 : dopt.cparams.promptPassword = TRI_YES;
685 : 0 : break;
686 : :
687 : 2 : case 'x': /* skip ACL dump */
688 : 2 : dopt.aclsSkip = true;
689 : 2 : break;
690 : :
691 : 13 : case 'Z': /* Compression */
692 : 13 : parse_compress_options(optarg, &compression_algorithm_str,
693 : : &compression_detail);
694 : 13 : user_compression_defined = true;
695 : 13 : break;
696 : :
697 : 155 : case 0:
698 : : /* This covers the long options. */
699 : 155 : break;
700 : :
701 : 2 : case 2: /* lock-wait-timeout */
702 : 2 : dopt.lockWaitTimeout = pg_strdup(optarg);
703 : 2 : break;
704 : :
705 : 6 : case 3: /* SET ROLE */
706 : 6 : use_role = pg_strdup(optarg);
707 : 6 : break;
708 : :
709 : 1 : case 4: /* exclude table(s) data */
710 : 1 : simple_string_list_append(&tabledata_exclude_patterns, optarg);
711 : 1 : break;
712 : :
713 : 6 : case 5: /* section */
714 : 6 : set_dump_section(optarg, &dopt.dumpSections);
715 : 6 : break;
716 : :
717 : 0 : case 6: /* snapshot */
718 : 0 : dumpsnapshot = pg_strdup(optarg);
719 : 0 : break;
720 : :
721 : 170 : case 7: /* no-sync */
722 : 170 : dosync = false;
723 : 170 : break;
724 : :
725 : 1 : case 8:
726 : 1 : have_extra_float_digits = true;
727 [ + - ]: 1 : if (!option_parse_int(optarg, "--extra-float-digits", -15, 3,
728 : : &extra_float_digits))
729 : 1 : exit_nicely(1);
730 : 0 : break;
731 : :
732 : 4 : case 9: /* inserts */
733 : :
734 : : /*
735 : : * dump_inserts also stores --rows-per-insert, careful not to
736 : : * overwrite that.
737 : : */
738 [ + - ]: 4 : if (dopt.dump_inserts == 0)
739 : 4 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
740 : 4 : break;
741 : :
742 : 4 : case 10: /* rows per insert */
743 [ + + ]: 4 : if (!option_parse_int(optarg, "--rows-per-insert", 1, INT_MAX,
744 : : &dopt.dump_inserts))
745 : 1 : exit_nicely(1);
746 : 3 : break;
747 : :
748 : 4 : case 11: /* include foreign data */
749 : 4 : simple_string_list_append(&foreign_servers_include_patterns,
750 : : optarg);
751 : 4 : break;
752 : :
753 : 1 : case 12: /* include table(s) and their children */
754 : 1 : simple_string_list_append(&table_include_patterns_and_children,
755 : : optarg);
756 : 1 : dopt.include_everything = false;
757 : 1 : break;
758 : :
759 : 1 : case 13: /* exclude table(s) and their children */
760 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
761 : : optarg);
762 : 1 : break;
763 : :
764 : 1 : case 14: /* exclude data of table(s) and children */
765 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
766 : : optarg);
767 : 1 : break;
768 : :
769 : 0 : case 15:
770 [ # # ]: 0 : if (!parse_sync_method(optarg, &sync_method))
771 : 0 : exit_nicely(1);
772 : 0 : break;
773 : :
774 : 26 : case 16: /* read object filters from file */
775 : 26 : read_dump_filters(optarg, &dopt);
776 : 22 : break;
777 : :
778 : 1 : case 17: /* exclude extension(s) */
779 : 1 : simple_string_list_append(&extension_exclude_patterns,
780 : : optarg);
781 : 1 : break;
782 : :
783 : 5 : case 18:
784 : 5 : statistics_only = true;
785 : 5 : break;
786 : :
787 : 44 : case 19:
788 : 44 : no_data = true;
789 : 44 : break;
790 : :
791 : 2 : case 20:
792 : 2 : no_schema = true;
793 : 2 : break;
794 : :
795 : 8 : case 21:
796 : 8 : no_statistics = true;
797 : 8 : break;
798 : :
799 : 98 : case 22:
800 : 98 : with_statistics = true;
801 : 98 : break;
802 : :
803 : 34 : case 25:
804 : 34 : dopt.restrict_key = pg_strdup(optarg);
805 : 34 : break;
806 : :
807 : 1 : default:
808 : : /* getopt_long already emitted a complaint */
809 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
810 : 1 : exit_nicely(1);
811 : : }
812 : : }
813 : :
814 : : /*
815 : : * Non-option argument specifies database name as long as it wasn't
816 : : * already specified with -d / --dbname
817 : : */
818 [ + + + - ]: 252 : if (optind < argc && dopt.cparams.dbname == NULL)
819 : 216 : dopt.cparams.dbname = argv[optind++];
820 : :
821 : : /* Complain if any arguments remain */
822 [ + + ]: 252 : if (optind < argc)
823 : : {
824 : 1 : pg_log_error("too many command-line arguments (first is \"%s\")",
825 : : argv[optind]);
826 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
827 : 1 : exit_nicely(1);
828 : : }
829 : :
830 : : /* --column-inserts implies --inserts */
831 [ + + + + ]: 251 : if (dopt.column_inserts && dopt.dump_inserts == 0)
832 : 1 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
833 : :
834 : : /* *-only options are incompatible with each other */
835 : 251 : check_mut_excl_opts(data_only, "-a/--data-only",
836 : : schema_only, "-s/--schema-only",
837 : : statistics_only, "--statistics-only");
838 : :
839 : : /* --no-* and *-only for same thing are incompatible */
840 : 248 : check_mut_excl_opts(data_only, "-a/--data-only",
841 : : no_data, "--no-data");
842 : 248 : check_mut_excl_opts(schema_only, "-s/--schema-only",
843 : : no_schema, "--no-schema");
844 : 248 : check_mut_excl_opts(statistics_only, "--statistics-only",
845 : : no_statistics, "--no-statistics");
846 : :
847 : : /* --statistics and --no-statistics are incompatible */
848 : 247 : check_mut_excl_opts(with_statistics, "--statistics",
849 : : no_statistics, "--no-statistics");
850 : :
851 : : /* --statistics is incompatible with *-only (except --statistics-only) */
852 : 247 : check_mut_excl_opts(with_statistics, "--statistics",
853 : : data_only, "-a/--data-only",
854 : : schema_only, "-s/--schema-only");
855 : :
856 : : /* --include-foreign-data is incompatible with --schema-only */
857 : 246 : check_mut_excl_opts(foreign_servers_include_patterns.head != NULL, "--include-foreign-data",
858 : : schema_only, "-s/--schema-only");
859 : :
860 [ + + + + ]: 245 : if (numWorkers > 1 && foreign_servers_include_patterns.head != NULL)
861 : 1 : pg_fatal("option %s is not supported with parallel backup",
862 : : "--include-foreign-data");
863 : :
864 : : /* --clean is incompatible with --data-only */
865 : 244 : check_mut_excl_opts(dopt.outputClean, "-c/--clean",
866 : : data_only, "-a/--data-only");
867 : :
868 [ + + + + ]: 243 : if (dopt.if_exists && !dopt.outputClean)
869 : 1 : pg_fatal("option %s requires option %s",
870 : : "--if-exists", "-c/--clean");
871 : :
872 : : /*
873 : : * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
874 : : * "--schema-only --no-schema", will have already caused an error in one
875 : : * of the checks above.
876 : : */
877 [ + + + + : 242 : dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) ||
- + ]
878 [ + - + + ]: 484 : data_only) && !no_data;
879 [ + + + + : 242 : dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) ||
- + ]
880 [ + - + + ]: 484 : schema_only) && !no_schema;
881 [ - - - - : 242 : dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) ||
+ + ]
882 [ - + + + : 484 : (statistics_only || with_statistics)) && !no_statistics;
+ - ]
883 : :
884 : :
885 : : /*
886 : : * --inserts are already implied above if --column-inserts or
887 : : * --rows-per-insert were specified.
888 : : */
889 [ + + + + ]: 242 : if (dopt.do_nothing && dopt.dump_inserts == 0)
890 : 1 : pg_fatal("option %s requires option %s, %s, or %s",
891 : : "--on-conflict-do-nothing",
892 : : "--inserts", "--rows-per-insert", "--column-inserts");
893 : :
894 : : /* Identify archive format to emit */
895 : 241 : archiveFormat = parseArchiveFormat(format, &archiveMode);
896 : :
897 : : /* archiveFormat specific setup */
898 [ + + ]: 240 : if (archiveFormat == archNull)
899 : 166 : plainText = 1;
900 [ + + + + ]: 240 : if (archiveFormat == archNull ||
901 : : archiveFormat == archTar)
902 : : {
903 : : /*
904 : : * If you don't provide a restrict key, one will be appointed for you.
905 : : * tar format uses this for its undocumented restore.sql file.
906 : : */
907 [ + + ]: 171 : if (!dopt.restrict_key)
908 : 137 : dopt.restrict_key = generate_restrict_key();
909 [ - + ]: 171 : if (!dopt.restrict_key)
910 : 0 : pg_fatal("could not generate restrict key");
911 [ - + ]: 171 : if (!valid_restrict_key(dopt.restrict_key))
912 : 0 : pg_fatal("invalid restrict key");
913 : : }
914 [ - + ]: 69 : else if (dopt.restrict_key)
915 : 0 : pg_fatal("option %s can only be used with %s or %s",
916 : : "--restrict-key", "--format=plain", "--format=tar");
917 : :
918 : : /*
919 : : * Custom and directory formats are compressed by default with gzip when
920 : : * available, not the others. If gzip is not available, no compression is
921 : : * done by default.
922 : : */
923 [ + + + + ]: 240 : if ((archiveFormat == archCustom || archiveFormat == archDirectory) &&
924 [ + + ]: 69 : !user_compression_defined)
925 : : {
926 : : #ifdef HAVE_LIBZ
927 : 63 : compression_algorithm_str = "gzip";
928 : : #else
929 : : compression_algorithm_str = "none";
930 : : #endif
931 : : }
932 : :
933 : : /*
934 : : * Compression options
935 : : */
936 [ + + ]: 240 : if (!parse_compress_algorithm(compression_algorithm_str,
937 : : &compression_algorithm))
938 : 1 : pg_fatal("unrecognized compression algorithm: \"%s\"",
939 : : compression_algorithm_str);
940 : :
941 : 239 : parse_compress_specification(compression_algorithm, compression_detail,
942 : : &compression_spec);
943 : 239 : error_detail = validate_compress_specification(&compression_spec);
944 [ + + ]: 239 : if (error_detail != NULL)
945 : 3 : pg_fatal("invalid compression specification: %s",
946 : : error_detail);
947 : :
948 : 236 : error_detail = supports_compression(compression_spec);
949 [ - + ]: 236 : if (error_detail != NULL)
950 : 0 : pg_fatal("%s", error_detail);
951 : :
952 : : /*
953 : : * Disable support for zstd workers for now - these are based on
954 : : * threading, and it's unclear how it interacts with parallel dumps on
955 : : * platforms where that relies on threads too (e.g. Windows).
956 : : */
957 [ - + ]: 236 : if (compression_spec.options & PG_COMPRESSION_OPTION_WORKERS)
958 : 0 : pg_log_warning("compression option \"%s\" is not currently supported by pg_dump",
959 : : "workers");
960 : :
961 : : /*
962 : : * If emitting an archive format, we always want to emit a DATABASE item,
963 : : * in case --create is specified at pg_restore time.
964 : : */
965 [ + + ]: 236 : if (!plainText)
966 : 74 : dopt.outputCreateDB = 1;
967 : :
968 : : /* Parallel backup only in the directory archive format so far */
969 [ + + + + ]: 236 : if (archiveFormat != archDirectory && numWorkers > 1)
970 : 1 : pg_fatal("parallel backup only supported by the directory format");
971 : :
972 : : /* Open the output file */
973 : 235 : fout = CreateArchive(filename, archiveFormat, compression_spec,
974 : : dosync, archiveMode, setupDumpWorker, sync_method);
975 : :
976 : : /* Make dump options accessible right away */
977 : 234 : SetArchiveOptions(fout, &dopt, NULL);
978 : :
979 : : /* Register the cleanup hook */
980 : 234 : on_exit_close_archive(fout);
981 : :
982 : : /* Let the archiver know how noisy to be */
983 : 234 : fout->verbose = g_verbose;
984 : :
985 : :
986 : : /*
987 : : * We allow the server to be back to 10, and up to any minor release of
988 : : * our own major version. (See also version check in pg_dumpall.c.)
989 : : */
990 : 234 : fout->minRemoteVersion = 100000;
991 : 234 : fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
992 : :
993 : 234 : fout->numWorkers = numWorkers;
994 : :
995 : : /*
996 : : * Open the database using the Archiver, so it knows about it. Errors mean
997 : : * death.
998 : : */
999 : 234 : ConnectDatabaseAhx(fout, &dopt.cparams, false);
1000 : 232 : setup_connection(fout, dumpencoding, dumpsnapshot, use_role);
1001 : :
1002 : : /*
1003 : : * On hot standbys, never try to dump unlogged table data, since it will
1004 : : * just throw an error.
1005 : : */
1006 [ + + ]: 232 : if (fout->isStandby)
1007 : 4 : dopt.no_unlogged_table_data = true;
1008 : :
1009 : : /*
1010 : : * Find the last built-in OID, if needed (prior to 8.1)
1011 : : *
1012 : : * With 8.1 and above, we can just use FirstNormalObjectId - 1.
1013 : : */
1014 : 232 : g_last_builtin_oid = FirstNormalObjectId - 1;
1015 : :
1016 : 232 : pg_log_info("last built-in OID is %u", g_last_builtin_oid);
1017 : :
1018 : : /* Expand schema selection patterns into OID lists */
1019 [ + + ]: 232 : if (schema_include_patterns.head != NULL)
1020 : : {
1021 : 19 : expand_schema_name_patterns(fout, &schema_include_patterns,
1022 : : &schema_include_oids,
1023 : : strict_names);
1024 [ + + ]: 13 : if (schema_include_oids.head == NULL)
1025 : 1 : pg_fatal("no matching schemas were found");
1026 : : }
1027 : 225 : expand_schema_name_patterns(fout, &schema_exclude_patterns,
1028 : : &schema_exclude_oids,
1029 : : false);
1030 : : /* non-matching exclusion patterns aren't an error */
1031 : :
1032 : : /* Expand table selection patterns into OID lists */
1033 : 225 : expand_table_name_patterns(fout, &table_include_patterns,
1034 : : &table_include_oids,
1035 : : strict_names, false);
1036 : 220 : expand_table_name_patterns(fout, &table_include_patterns_and_children,
1037 : : &table_include_oids,
1038 : : strict_names, true);
1039 [ + + ]: 220 : if ((table_include_patterns.head != NULL ||
1040 [ + + ]: 209 : table_include_patterns_and_children.head != NULL) &&
1041 [ + + ]: 13 : table_include_oids.head == NULL)
1042 : 2 : pg_fatal("no matching tables were found");
1043 : :
1044 : 218 : expand_table_name_patterns(fout, &table_exclude_patterns,
1045 : : &table_exclude_oids,
1046 : : false, false);
1047 : 218 : expand_table_name_patterns(fout, &table_exclude_patterns_and_children,
1048 : : &table_exclude_oids,
1049 : : false, true);
1050 : :
1051 : 218 : expand_table_name_patterns(fout, &tabledata_exclude_patterns,
1052 : : &tabledata_exclude_oids,
1053 : : false, false);
1054 : 218 : expand_table_name_patterns(fout, &tabledata_exclude_patterns_and_children,
1055 : : &tabledata_exclude_oids,
1056 : : false, true);
1057 : :
1058 : 218 : expand_foreign_server_name_patterns(fout, &foreign_servers_include_patterns,
1059 : : &foreign_servers_include_oids);
1060 : :
1061 : : /* non-matching exclusion patterns aren't an error */
1062 : :
1063 : : /* Expand extension selection patterns into OID lists */
1064 [ + + ]: 217 : if (extension_include_patterns.head != NULL)
1065 : : {
1066 : 5 : expand_extension_name_patterns(fout, &extension_include_patterns,
1067 : : &extension_include_oids,
1068 : : strict_names);
1069 [ + + ]: 5 : if (extension_include_oids.head == NULL)
1070 : 1 : pg_fatal("no matching extensions were found");
1071 : : }
1072 : 216 : expand_extension_name_patterns(fout, &extension_exclude_patterns,
1073 : : &extension_exclude_oids,
1074 : : false);
1075 : : /* non-matching exclusion patterns aren't an error */
1076 : :
1077 : : /*
1078 : : * Dumping LOs is the default for dumps where an inclusion switch is not
1079 : : * used (an "include everything" dump). -B can be used to exclude LOs
1080 : : * from those dumps. -b can be used to include LOs even when an inclusion
1081 : : * switch is used.
1082 : : *
1083 : : * -s means "schema only" and LOs are data, not schema, so we never
1084 : : * include LOs when -s is used.
1085 : : */
1086 [ + + + + : 216 : if (dopt.include_everything && dopt.dumpData && !dopt.dontOutputLOs)
+ + ]
1087 : 142 : dopt.outputLOs = true;
1088 : :
1089 : : /*
1090 : : * Collect role names so we can map object owner OIDs to names.
1091 : : */
1092 : 216 : collectRoleNames(fout);
1093 : :
1094 : : /*
1095 : : * Now scan the database and create DumpableObject structs for all the
1096 : : * objects we intend to dump.
1097 : : */
1098 : 216 : tblinfo = getSchemaData(fout, &numTables);
1099 : :
1100 [ + + ]: 215 : if (dopt.dumpData)
1101 : : {
1102 : 166 : getTableData(&dopt, tblinfo, numTables, 0);
1103 : 166 : buildMatViewRefreshDependencies(fout);
1104 [ + + ]: 166 : if (!dopt.dumpSchema)
1105 : 7 : getTableDataFKConstraints();
1106 : : }
1107 : :
1108 [ + + + + ]: 215 : if (!dopt.dumpData && dopt.sequence_data)
1109 : 40 : getTableData(&dopt, tblinfo, numTables, RELKIND_SEQUENCE);
1110 : :
1111 : : /*
1112 : : * For binary upgrade mode, dump the pg_shdepend rows for large objects
1113 : : * and maybe even pg_largeobject_metadata (see comment below for details).
1114 : : * This is faster to restore than the equivalent set of large object
1115 : : * commands.
1116 : : */
1117 [ + + ]: 215 : if (dopt.binary_upgrade)
1118 : : {
1119 : : TableInfo *shdepend;
1120 : :
1121 : 44 : shdepend = findTableByOid(SharedDependRelationId);
1122 : 44 : makeTableDataInfo(&dopt, shdepend);
1123 : :
1124 : : /*
1125 : : * Only dump large object shdepend rows for this database.
1126 : : */
1127 : 44 : shdepend->dataObj->filtercond = "WHERE classid = 'pg_largeobject'::regclass "
1128 : : "AND dbid = (SELECT oid FROM pg_database "
1129 : : " WHERE datname = current_database())";
1130 : :
1131 : : /*
1132 : : * For binary upgrades from v16 and newer versions, we can copy
1133 : : * pg_largeobject_metadata's files from the old cluster, so we don't
1134 : : * need to dump its contents. pg_upgrade can't copy/link the files
1135 : : * from older versions because aclitem (needed by
1136 : : * pg_largeobject_metadata.lomacl) changed its storage format in v16.
1137 : : */
1138 [ - + ]: 44 : if (fout->remoteVersion < 160000)
1139 : : {
1140 : : TableInfo *lo_metadata;
1141 : :
1142 : 0 : lo_metadata = findTableByOid(LargeObjectMetadataRelationId);
1143 : 0 : makeTableDataInfo(&dopt, lo_metadata);
1144 : : }
1145 : : }
1146 : :
1147 : : /*
1148 : : * In binary-upgrade mode, we do not have to worry about the actual LO
1149 : : * data or the associated metadata that resides in the pg_largeobject and
1150 : : * pg_largeobject_metadata tables, respectively.
1151 : : *
1152 : : * However, we do need to collect LO information as there may be comments
1153 : : * or other information on LOs that we do need to dump out.
1154 : : */
1155 [ + + + + ]: 215 : if (dopt.outputLOs || dopt.binary_upgrade)
1156 : 186 : getLOs(fout);
1157 : :
1158 : : /*
1159 : : * Collect dependency data to assist in ordering the objects.
1160 : : */
1161 : 215 : getDependencies(fout);
1162 : :
1163 : : /*
1164 : : * Collect ACLs, comments, and security labels, if wanted.
1165 : : */
1166 [ + + ]: 215 : if (!dopt.aclsSkip)
1167 : 213 : getAdditionalACLs(fout);
1168 [ + - ]: 215 : if (!dopt.no_comments)
1169 : 215 : collectComments(fout);
1170 [ + - ]: 215 : if (!dopt.no_security_labels)
1171 : 215 : collectSecLabels(fout);
1172 : :
1173 : : /* For binary upgrade mode, collect required pg_class information. */
1174 [ + + ]: 215 : if (dopt.binary_upgrade)
1175 : 44 : collectBinaryUpgradeClassOids(fout);
1176 : :
1177 : : /* Collect sequence information. */
1178 : 215 : collectSequences(fout, tblinfo, numTables);
1179 : :
1180 : : /* Lastly, create dummy objects to represent the section boundaries */
1181 : 215 : boundaryObjs = createBoundaryObjects();
1182 : :
1183 : : /* Get pointers to all the known DumpableObjects */
1184 : 215 : getDumpableObjects(&dobjs, &numObjs);
1185 : :
1186 : : /*
1187 : : * Add dummy dependencies to enforce the dump section ordering.
1188 : : */
1189 : 215 : addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
1190 : :
1191 : : /*
1192 : : * Sort the objects into a safe dump order (no forward references).
1193 : : *
1194 : : * We rely on dependency information to help us determine a safe order, so
1195 : : * the initial sort is mostly for cosmetic purposes: we sort by name to
1196 : : * ensure that logically identical schemas will dump identically.
1197 : : */
1198 : 215 : sortDumpableObjectsByTypeName(dobjs, numObjs);
1199 : :
1200 : 215 : sortDumpableObjects(dobjs, numObjs,
1201 : 215 : boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
1202 : :
1203 : : /*
1204 : : * Create archive TOC entries for all the objects to be dumped, in a safe
1205 : : * order.
1206 : : */
1207 : :
1208 : : /*
1209 : : * First the special entries for ENCODING, STDSTRINGS, and SEARCHPATH.
1210 : : */
1211 : 215 : dumpEncoding(fout);
1212 : 215 : dumpStdStrings(fout);
1213 : 215 : dumpSearchPath(fout);
1214 : :
1215 : : /* The database items are always next, unless we don't want them at all */
1216 [ + + ]: 215 : if (dopt.outputCreateDB)
1217 : 108 : dumpDatabase(fout);
1218 : :
1219 : : /* Now the rearrangeable objects. */
1220 [ + + ]: 811792 : for (i = 0; i < numObjs; i++)
1221 : 811577 : dumpDumpableObject(fout, dobjs[i]);
1222 : :
1223 : : /*
1224 : : * Set up options info to ensure we dump what we want.
1225 : : */
1226 : 215 : ropt = NewRestoreOptions();
1227 : 215 : ropt->filename = filename;
1228 : :
1229 : : /* if you change this list, see dumpOptionsFromRestoreOptions */
1230 [ + + ]: 215 : ropt->cparams.dbname = dopt.cparams.dbname ? pg_strdup(dopt.cparams.dbname) : NULL;
1231 [ + + ]: 215 : ropt->cparams.pgport = dopt.cparams.pgport ? pg_strdup(dopt.cparams.pgport) : NULL;
1232 [ + + ]: 215 : ropt->cparams.pghost = dopt.cparams.pghost ? pg_strdup(dopt.cparams.pghost) : NULL;
1233 [ + + ]: 215 : ropt->cparams.username = dopt.cparams.username ? pg_strdup(dopt.cparams.username) : NULL;
1234 : 215 : ropt->cparams.promptPassword = dopt.cparams.promptPassword;
1235 : 215 : ropt->dropSchema = dopt.outputClean;
1236 : 215 : ropt->dumpData = dopt.dumpData;
1237 : 215 : ropt->dumpSchema = dopt.dumpSchema;
1238 : 215 : ropt->dumpStatistics = dopt.dumpStatistics;
1239 : 215 : ropt->if_exists = dopt.if_exists;
1240 : 215 : ropt->column_inserts = dopt.column_inserts;
1241 : 215 : ropt->dumpSections = dopt.dumpSections;
1242 : 215 : ropt->aclsSkip = dopt.aclsSkip;
1243 : 215 : ropt->superuser = dopt.outputSuperuser;
1244 : 215 : ropt->createDB = dopt.outputCreateDB;
1245 : 215 : ropt->noOwner = dopt.outputNoOwner;
1246 : 215 : ropt->noTableAm = dopt.outputNoTableAm;
1247 : 215 : ropt->noTablespace = dopt.outputNoTablespaces;
1248 : 215 : ropt->disable_triggers = dopt.disable_triggers;
1249 : 215 : ropt->use_setsessauth = dopt.use_setsessauth;
1250 : 215 : ropt->disable_dollar_quoting = dopt.disable_dollar_quoting;
1251 : 215 : ropt->dump_inserts = dopt.dump_inserts;
1252 : 215 : ropt->no_comments = dopt.no_comments;
1253 : 215 : ropt->no_policies = dopt.no_policies;
1254 : 215 : ropt->no_publications = dopt.no_publications;
1255 : 215 : ropt->no_security_labels = dopt.no_security_labels;
1256 : 215 : ropt->no_subscriptions = dopt.no_subscriptions;
1257 : 215 : ropt->lockWaitTimeout = dopt.lockWaitTimeout;
1258 : 215 : ropt->include_everything = dopt.include_everything;
1259 : 215 : ropt->enable_row_security = dopt.enable_row_security;
1260 : 215 : ropt->sequence_data = dopt.sequence_data;
1261 : 215 : ropt->binary_upgrade = dopt.binary_upgrade;
1262 [ + + ]: 215 : ropt->restrict_key = dopt.restrict_key ? pg_strdup(dopt.restrict_key) : NULL;
1263 : :
1264 : 215 : ropt->compression_spec = compression_spec;
1265 : :
1266 : 215 : ropt->suppressDumpWarnings = true; /* We've already shown them */
1267 : :
1268 : 215 : SetArchiveOptions(fout, &dopt, ropt);
1269 : :
1270 : : /* Mark which entries should be output */
1271 : 215 : ProcessArchiveRestoreOptions(fout);
1272 : :
1273 : : /*
1274 : : * The archive's TOC entries are now marked as to which ones will actually
1275 : : * be output, so we can set up their dependency lists properly. This isn't
1276 : : * necessary for plain-text output, though.
1277 : : */
1278 [ + + ]: 215 : if (!plainText)
1279 : 73 : BuildArchiveDependencies(fout);
1280 : :
1281 : : /*
1282 : : * And finally we can do the actual output.
1283 : : *
1284 : : * Note: for non-plain-text output formats, the output file is written
1285 : : * inside CloseArchive(). This is, um, bizarre; but not worth changing
1286 : : * right now.
1287 : : */
1288 [ + + ]: 215 : if (plainText)
1289 : 142 : RestoreArchive(fout);
1290 : :
1291 : 214 : CloseArchive(fout);
1292 : :
1293 : 214 : exit_nicely(0);
1294 : : }
1295 : :
1296 : :
1297 : : static void
1298 : 1 : help(const char *progname)
1299 : : {
1300 : 1 : printf(_("%s exports a PostgreSQL database as an SQL script or to other formats.\n\n"), progname);
1301 : 1 : printf(_("Usage:\n"));
1302 : 1 : printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
1303 : :
1304 : 1 : printf(_("\nGeneral options:\n"));
1305 : 1 : printf(_(" -f, --file=FILENAME output file or directory name\n"));
1306 : 1 : printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
1307 : : " plain text (default))\n"));
1308 : 1 : printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n"));
1309 : 1 : printf(_(" -v, --verbose verbose mode\n"));
1310 : 1 : printf(_(" -V, --version output version information, then exit\n"));
1311 : 1 : printf(_(" -Z, --compress=METHOD[:DETAIL]\n"
1312 : : " compress as specified\n"));
1313 : 1 : printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
1314 : 1 : printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
1315 : 1 : printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
1316 : 1 : printf(_(" -?, --help show this help, then exit\n"));
1317 : :
1318 : 1 : printf(_("\nOptions controlling the output content:\n"));
1319 : 1 : printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
1320 : 1 : printf(_(" -b, --large-objects include large objects in dump\n"));
1321 : 1 : printf(_(" --blobs (same as --large-objects, deprecated)\n"));
1322 : 1 : printf(_(" -B, --no-large-objects exclude large objects in dump\n"));
1323 : 1 : printf(_(" --no-blobs (same as --no-large-objects, deprecated)\n"));
1324 : 1 : printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
1325 : 1 : printf(_(" -C, --create include commands to create database in dump\n"));
1326 : 1 : printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n"));
1327 : 1 : printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
1328 : 1 : printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n"));
1329 : 1 : printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n"));
1330 : 1 : printf(_(" -O, --no-owner skip restoration of object ownership in\n"
1331 : : " plain-text format\n"));
1332 : 1 : printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
1333 : 1 : printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n"));
1334 : 1 : printf(_(" -t, --table=PATTERN dump only the specified table(s)\n"));
1335 : 1 : printf(_(" -T, --exclude-table=PATTERN do NOT dump the specified table(s)\n"));
1336 : 1 : printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
1337 : 1 : printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
1338 : 1 : printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
1339 : 1 : printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
1340 : 1 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
1341 : 1 : printf(_(" --enable-row-security enable row security (dump only content user has\n"
1342 : : " access to)\n"));
1343 : 1 : printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n"));
1344 : 1 : printf(_(" --exclude-table-and-children=PATTERN\n"
1345 : : " do NOT dump the specified table(s), including\n"
1346 : : " child and partition tables\n"));
1347 : 1 : printf(_(" --exclude-table-data=PATTERN do NOT dump data for the specified table(s)\n"));
1348 : 1 : printf(_(" --exclude-table-data-and-children=PATTERN\n"
1349 : : " do NOT dump data for the specified table(s),\n"
1350 : : " including child and partition tables\n"));
1351 : 1 : printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
1352 : 1 : printf(_(" --filter=FILENAME include or exclude objects and data from dump\n"
1353 : : " based on expressions in FILENAME\n"));
1354 : 1 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
1355 : 1 : printf(_(" --include-foreign-data=PATTERN\n"
1356 : : " include data of foreign tables on foreign\n"
1357 : : " servers matching PATTERN\n"));
1358 : 1 : printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
1359 : 1 : printf(_(" --load-via-partition-root load partitions via the root table\n"));
1360 : 1 : printf(_(" --no-comments do not dump comment commands\n"));
1361 : 1 : printf(_(" --no-data do not dump data\n"));
1362 : 1 : printf(_(" --no-policies do not dump row security policies\n"));
1363 : 1 : printf(_(" --no-publications do not dump publications\n"));
1364 : 1 : printf(_(" --no-schema do not dump schema\n"));
1365 : 1 : printf(_(" --no-security-labels do not dump security label assignments\n"));
1366 : 1 : printf(_(" --no-statistics do not dump statistics\n"));
1367 : 1 : printf(_(" --no-subscriptions do not dump subscriptions\n"));
1368 : 1 : printf(_(" --no-table-access-method do not dump table access methods\n"));
1369 : 1 : printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
1370 : 1 : printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
1371 : 1 : printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
1372 : 1 : printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
1373 : 1 : printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
1374 : 1 : printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
1375 : 1 : printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
1376 : 1 : printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
1377 : 1 : printf(_(" --sequence-data include sequence data in dump\n"));
1378 : 1 : printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
1379 : 1 : printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n"));
1380 : 1 : printf(_(" --statistics dump the statistics\n"));
1381 : 1 : printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
1382 : 1 : printf(_(" --strict-names require table and/or schema include patterns to\n"
1383 : : " match at least one entity each\n"));
1384 : 1 : printf(_(" --table-and-children=PATTERN dump only the specified table(s), including\n"
1385 : : " child and partition tables\n"));
1386 : 1 : printf(_(" --use-set-session-authorization\n"
1387 : : " use SET SESSION AUTHORIZATION commands instead of\n"
1388 : : " ALTER OWNER commands to set ownership\n"));
1389 : :
1390 : 1 : printf(_("\nConnection options:\n"));
1391 : 1 : printf(_(" -d, --dbname=DBNAME database to dump\n"));
1392 : 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
1393 : 1 : printf(_(" -p, --port=PORT database server port number\n"));
1394 : 1 : printf(_(" -U, --username=NAME connect as specified database user\n"));
1395 : 1 : printf(_(" -w, --no-password never prompt for password\n"));
1396 : 1 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
1397 : 1 : printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
1398 : :
1399 : 1 : printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
1400 : : "variable value is used.\n\n"));
1401 : 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1402 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1403 : 1 : }
1404 : :
1405 : : static void
1406 : 248 : setup_connection(Archive *AH, const char *dumpencoding,
1407 : : const char *dumpsnapshot, char *use_role)
1408 : : {
1409 : 248 : DumpOptions *dopt = AH->dopt;
1410 : 248 : PGconn *conn = GetConnection(AH);
1411 : :
1412 : 248 : PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL));
1413 : :
1414 : : /*
1415 : : * Set the client encoding if requested.
1416 : : */
1417 [ + + ]: 248 : if (dumpencoding)
1418 : : {
1419 [ - + ]: 19 : if (PQsetClientEncoding(conn, dumpencoding) < 0)
1420 : 0 : pg_fatal("invalid client encoding \"%s\" specified",
1421 : : dumpencoding);
1422 : : }
1423 : :
1424 : : /*
1425 : : * Force standard_conforming_strings on, just in case we are dumping from
1426 : : * an old server that has it disabled. Without this, literals in views,
1427 : : * expressions, etc, would be incorrect for modern servers.
1428 : : */
1429 : 248 : ExecuteSqlStatement(AH, "SET standard_conforming_strings = on");
1430 : :
1431 : : /*
1432 : : * And reflect that to AH->std_strings. You might think that we should
1433 : : * just delete that variable and the code that checks it, but that would
1434 : : * be problematic for pg_restore, which at least for now should still cope
1435 : : * with archives containing the other setting (cf. processStdStringsEntry
1436 : : * in pg_backup_archiver.c).
1437 : : */
1438 : 248 : AH->std_strings = true;
1439 : :
1440 : : /*
1441 : : * Get the active encoding, so we know how to escape strings.
1442 : : */
1443 : 248 : AH->encoding = PQclientEncoding(conn);
1444 : 248 : setFmtEncoding(AH->encoding);
1445 : :
1446 : : /*
1447 : : * Set the role if requested. In a parallel dump worker, we'll be passed
1448 : : * use_role == NULL, but AH->use_role is already set (if user specified it
1449 : : * originally) and we should use that.
1450 : : */
1451 [ + + + + ]: 248 : if (!use_role && AH->use_role)
1452 : 2 : use_role = AH->use_role;
1453 : :
1454 : : /* Set the role if requested */
1455 [ + + ]: 248 : if (use_role)
1456 : : {
1457 : 8 : PQExpBuffer query = createPQExpBuffer();
1458 : :
1459 : 8 : appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
1460 : 8 : ExecuteSqlStatement(AH, query->data);
1461 : 8 : destroyPQExpBuffer(query);
1462 : :
1463 : : /* save it for possible later use by parallel workers */
1464 [ + + ]: 8 : if (!AH->use_role)
1465 : 6 : AH->use_role = pg_strdup(use_role);
1466 : : }
1467 : :
1468 : : /* Set the datestyle to ISO to ensure the dump's portability */
1469 : 248 : ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
1470 : :
1471 : : /* Likewise, avoid using sql_standard intervalstyle */
1472 : 248 : ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
1473 : :
1474 : : /*
1475 : : * Use an explicitly specified extra_float_digits if it has been provided.
1476 : : * Otherwise, set extra_float_digits so that we can dump float data
1477 : : * exactly (given correctly implemented float I/O code, anyway).
1478 : : */
1479 [ - + ]: 248 : if (have_extra_float_digits)
1480 : : {
1481 : 0 : PQExpBuffer q = createPQExpBuffer();
1482 : :
1483 : 0 : appendPQExpBuffer(q, "SET extra_float_digits TO %d",
1484 : : extra_float_digits);
1485 : 0 : ExecuteSqlStatement(AH, q->data);
1486 : 0 : destroyPQExpBuffer(q);
1487 : : }
1488 : : else
1489 : 248 : ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
1490 : :
1491 : : /*
1492 : : * Disable synchronized scanning, to prevent unpredictable changes in row
1493 : : * ordering across a dump and reload.
1494 : : */
1495 : 248 : ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
1496 : :
1497 : : /*
1498 : : * Disable timeouts if supported.
1499 : : */
1500 : 248 : ExecuteSqlStatement(AH, "SET statement_timeout = 0");
1501 : 248 : ExecuteSqlStatement(AH, "SET lock_timeout = 0");
1502 : 248 : ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
1503 [ + - ]: 248 : if (AH->remoteVersion >= 170000)
1504 : 248 : ExecuteSqlStatement(AH, "SET transaction_timeout = 0");
1505 : :
1506 : : /*
1507 : : * Quote all identifiers, if requested.
1508 : : */
1509 [ + + ]: 248 : if (quote_all_identifiers)
1510 : 42 : ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1511 : :
1512 : : /*
1513 : : * Adjust row-security mode, if supported.
1514 : : */
1515 [ - + ]: 248 : if (dopt->enable_row_security)
1516 : 0 : ExecuteSqlStatement(AH, "SET row_security = on");
1517 : : else
1518 : 248 : ExecuteSqlStatement(AH, "SET row_security = off");
1519 : :
1520 : : /*
1521 : : * For security reasons, we restrict the expansion of non-system views and
1522 : : * access to foreign tables during the pg_dump process. This restriction
1523 : : * is adjusted when dumping foreign table data.
1524 : : */
1525 : 248 : set_restrict_relation_kind(AH, "view, foreign-table");
1526 : :
1527 : : /*
1528 : : * Initialize prepared-query state to "nothing prepared". We do this here
1529 : : * so that a parallel dump worker will have its own state.
1530 : : */
1531 : 248 : AH->is_prepared = pg_malloc0_array(bool, NUM_PREP_QUERIES);
1532 : :
1533 : : /*
1534 : : * Start transaction-snapshot mode transaction to dump consistent data.
1535 : : */
1536 : 248 : ExecuteSqlStatement(AH, "BEGIN");
1537 : :
1538 : : /*
1539 : : * To support the combination of serializable_deferrable with the jobs
1540 : : * option we use REPEATABLE READ for the worker connections that are
1541 : : * passed a snapshot. As long as the snapshot is acquired in a
1542 : : * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a
1543 : : * REPEATABLE READ transaction provides the appropriate integrity
1544 : : * guarantees. This is a kluge, but safe for back-patching.
1545 : : */
1546 [ - + - - ]: 248 : if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
1547 : 0 : ExecuteSqlStatement(AH,
1548 : : "SET TRANSACTION ISOLATION LEVEL "
1549 : : "SERIALIZABLE, READ ONLY, DEFERRABLE");
1550 : : else
1551 : 248 : ExecuteSqlStatement(AH,
1552 : : "SET TRANSACTION ISOLATION LEVEL "
1553 : : "REPEATABLE READ, READ ONLY");
1554 : :
1555 : : /*
1556 : : * If user specified a snapshot to use, select that. In a parallel dump
1557 : : * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id
1558 : : * is already set (if the server can handle it) and we should use that.
1559 : : */
1560 [ - + ]: 248 : if (dumpsnapshot)
1561 : 0 : AH->sync_snapshot_id = pg_strdup(dumpsnapshot);
1562 : :
1563 [ + + ]: 248 : if (AH->sync_snapshot_id)
1564 : : {
1565 : 16 : PQExpBuffer query = createPQExpBuffer();
1566 : :
1567 : 16 : appendPQExpBufferStr(query, "SET TRANSACTION SNAPSHOT ");
1568 : 16 : appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1569 : 16 : ExecuteSqlStatement(AH, query->data);
1570 : 16 : destroyPQExpBuffer(query);
1571 : : }
1572 [ + + ]: 232 : else if (AH->numWorkers > 1)
1573 : 8 : AH->sync_snapshot_id = get_synchronized_snapshot(AH);
1574 : 248 : }
1575 : :
1576 : : /* Set up connection for a parallel worker process */
1577 : : static void
1578 : 16 : setupDumpWorker(Archive *AH)
1579 : : {
1580 : : /*
1581 : : * We want to re-select all the same values the leader connection is
1582 : : * using. We'll have inherited directly-usable values in
1583 : : * AH->sync_snapshot_id and AH->use_role, but we need to translate the
1584 : : * inherited encoding value back to a string to pass to setup_connection.
1585 : : */
1586 : 16 : setup_connection(AH,
1587 : : pg_encoding_to_char(AH->encoding),
1588 : : NULL,
1589 : : NULL);
1590 : 16 : }
1591 : :
1592 : : static char *
1593 : 8 : get_synchronized_snapshot(Archive *fout)
1594 : : {
1595 : 8 : char *query = "SELECT pg_catalog.pg_export_snapshot()";
1596 : : char *result;
1597 : : PGresult *res;
1598 : :
1599 : 8 : res = ExecuteSqlQueryForSingleRow(fout, query);
1600 : 8 : result = pg_strdup(PQgetvalue(res, 0, 0));
1601 : 8 : PQclear(res);
1602 : :
1603 : 8 : return result;
1604 : : }
1605 : :
1606 : : static ArchiveFormat
1607 : 241 : parseArchiveFormat(const char *format, ArchiveMode *mode)
1608 : : {
1609 : : ArchiveFormat archiveFormat;
1610 : :
1611 : 241 : *mode = archModeWrite;
1612 : :
1613 [ + + - + ]: 241 : if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1614 : : {
1615 : : /* This is used by pg_dumpall, and is not documented */
1616 : 54 : archiveFormat = archNull;
1617 : 54 : *mode = archModeAppend;
1618 : : }
1619 [ - + ]: 187 : else if (pg_strcasecmp(format, "c") == 0)
1620 : 0 : archiveFormat = archCustom;
1621 [ + + ]: 187 : else if (pg_strcasecmp(format, "custom") == 0)
1622 : 57 : archiveFormat = archCustom;
1623 [ - + ]: 130 : else if (pg_strcasecmp(format, "d") == 0)
1624 : 0 : archiveFormat = archDirectory;
1625 [ + + ]: 130 : else if (pg_strcasecmp(format, "directory") == 0)
1626 : 12 : archiveFormat = archDirectory;
1627 [ + + ]: 118 : else if (pg_strcasecmp(format, "p") == 0)
1628 : 109 : archiveFormat = archNull;
1629 [ + + ]: 9 : else if (pg_strcasecmp(format, "plain") == 0)
1630 : 3 : archiveFormat = archNull;
1631 [ - + ]: 6 : else if (pg_strcasecmp(format, "t") == 0)
1632 : 0 : archiveFormat = archTar;
1633 [ + + ]: 6 : else if (pg_strcasecmp(format, "tar") == 0)
1634 : 5 : archiveFormat = archTar;
1635 : : else
1636 : 1 : pg_fatal("invalid output format \"%s\" specified", format);
1637 : 240 : return archiveFormat;
1638 : : }
1639 : :
1640 : : /*
1641 : : * Find the OIDs of all schemas matching the given list of patterns,
1642 : : * and append them to the given OID list.
1643 : : */
1644 : : static void
1645 : 244 : expand_schema_name_patterns(Archive *fout,
1646 : : SimpleStringList *patterns,
1647 : : SimpleOidList *oids,
1648 : : bool strict_names)
1649 : : {
1650 : : PQExpBuffer query;
1651 : : PGresult *res;
1652 : : SimpleStringListCell *cell;
1653 : : int i;
1654 : :
1655 [ + + ]: 244 : if (patterns->head == NULL)
1656 : 222 : return; /* nothing to do */
1657 : :
1658 : 22 : query = createPQExpBuffer();
1659 : :
1660 : : /*
1661 : : * The loop below runs multiple SELECTs might sometimes result in
1662 : : * duplicate entries in the OID list, but we don't care.
1663 : : */
1664 : :
1665 [ + + ]: 38 : for (cell = patterns->head; cell; cell = cell->next)
1666 : : {
1667 : : PQExpBufferData dbbuf;
1668 : : int dotcnt;
1669 : :
1670 : : /*
1671 : : * The pg_conflict schema, which holds the conflict log tables, is not
1672 : : * selectable. The tables are dumped only by pg_upgrade, along with
1673 : : * the subscriptions.
1674 : : */
1675 : 22 : appendPQExpBufferStr(query,
1676 : : "SELECT oid FROM pg_catalog.pg_namespace n\n"
1677 : : "WHERE n.nspname OPERATOR(pg_catalog.<>) 'pg_conflict'\n");
1678 : 22 : initPQExpBuffer(&dbbuf);
1679 : 22 : processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1680 : : false, NULL, "n.nspname", NULL, NULL, &dbbuf,
1681 : : &dotcnt);
1682 [ + + ]: 22 : if (dotcnt > 1)
1683 : 2 : pg_fatal("improper qualified name (too many dotted names): %s",
1684 : : cell->val);
1685 [ + + ]: 20 : else if (dotcnt == 1)
1686 : 3 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1687 : 17 : termPQExpBuffer(&dbbuf);
1688 : :
1689 : 17 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1690 [ + + + - ]: 17 : if (strict_names && PQntuples(res) == 0)
1691 : 1 : pg_fatal("no matching schemas were found for pattern \"%s\"", cell->val);
1692 : :
1693 [ + + ]: 31 : for (i = 0; i < PQntuples(res); i++)
1694 : : {
1695 : 15 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1696 : : }
1697 : :
1698 : 16 : PQclear(res);
1699 : 16 : resetPQExpBuffer(query);
1700 : : }
1701 : :
1702 : 16 : destroyPQExpBuffer(query);
1703 : : }
1704 : :
1705 : : /*
1706 : : * Find the OIDs of all extensions matching the given list of patterns,
1707 : : * and append them to the given OID list.
1708 : : */
1709 : : static void
1710 : 221 : expand_extension_name_patterns(Archive *fout,
1711 : : SimpleStringList *patterns,
1712 : : SimpleOidList *oids,
1713 : : bool strict_names)
1714 : : {
1715 : : PQExpBuffer query;
1716 : : PGresult *res;
1717 : : SimpleStringListCell *cell;
1718 : : int i;
1719 : :
1720 [ + + ]: 221 : if (patterns->head == NULL)
1721 : 214 : return; /* nothing to do */
1722 : :
1723 : 7 : query = createPQExpBuffer();
1724 : :
1725 : : /*
1726 : : * The loop below runs multiple SELECTs might sometimes result in
1727 : : * duplicate entries in the OID list, but we don't care.
1728 : : */
1729 [ + + ]: 14 : for (cell = patterns->head; cell; cell = cell->next)
1730 : : {
1731 : : int dotcnt;
1732 : :
1733 : 7 : appendPQExpBufferStr(query,
1734 : : "SELECT oid FROM pg_catalog.pg_extension e\n");
1735 : 7 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1736 : : false, NULL, "e.extname", NULL, NULL, NULL,
1737 : : &dotcnt);
1738 [ - + ]: 7 : if (dotcnt > 0)
1739 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1740 : : cell->val);
1741 : :
1742 : 7 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1743 [ - + - - ]: 7 : if (strict_names && PQntuples(res) == 0)
1744 : 0 : pg_fatal("no matching extensions were found for pattern \"%s\"", cell->val);
1745 : :
1746 [ + + ]: 13 : for (i = 0; i < PQntuples(res); i++)
1747 : : {
1748 : 6 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1749 : : }
1750 : :
1751 : 7 : PQclear(res);
1752 : 7 : resetPQExpBuffer(query);
1753 : : }
1754 : :
1755 : 7 : destroyPQExpBuffer(query);
1756 : : }
1757 : :
1758 : : /*
1759 : : * Find the OIDs of all foreign servers matching the given list of patterns,
1760 : : * and append them to the given OID list.
1761 : : */
1762 : : static void
1763 : 218 : expand_foreign_server_name_patterns(Archive *fout,
1764 : : SimpleStringList *patterns,
1765 : : SimpleOidList *oids)
1766 : : {
1767 : : PQExpBuffer query;
1768 : : PGresult *res;
1769 : : SimpleStringListCell *cell;
1770 : : int i;
1771 : :
1772 [ + + ]: 218 : if (patterns->head == NULL)
1773 : 215 : return; /* nothing to do */
1774 : :
1775 : 3 : query = createPQExpBuffer();
1776 : :
1777 : : /*
1778 : : * The loop below runs multiple SELECTs might sometimes result in
1779 : : * duplicate entries in the OID list, but we don't care.
1780 : : */
1781 : :
1782 [ + + ]: 5 : for (cell = patterns->head; cell; cell = cell->next)
1783 : : {
1784 : : int dotcnt;
1785 : :
1786 : 3 : appendPQExpBufferStr(query,
1787 : : "SELECT oid FROM pg_catalog.pg_foreign_server s\n");
1788 : 3 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1789 : : false, NULL, "s.srvname", NULL, NULL, NULL,
1790 : : &dotcnt);
1791 [ - + ]: 3 : if (dotcnt > 0)
1792 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1793 : : cell->val);
1794 : :
1795 : 3 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1796 [ + + ]: 3 : if (PQntuples(res) == 0)
1797 : 1 : pg_fatal("no matching foreign servers were found for pattern \"%s\"", cell->val);
1798 : :
1799 [ + + ]: 4 : for (i = 0; i < PQntuples(res); i++)
1800 : 2 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1801 : :
1802 : 2 : PQclear(res);
1803 : 2 : resetPQExpBuffer(query);
1804 : : }
1805 : :
1806 : 2 : destroyPQExpBuffer(query);
1807 : : }
1808 : :
1809 : : /*
1810 : : * Find the OIDs of all tables matching the given list of patterns,
1811 : : * and append them to the given OID list. See also expand_dbname_patterns()
1812 : : * in pg_dumpall.c
1813 : : */
1814 : : static void
1815 : 1317 : expand_table_name_patterns(Archive *fout,
1816 : : SimpleStringList *patterns, SimpleOidList *oids,
1817 : : bool strict_names, bool with_child_tables)
1818 : : {
1819 : : PQExpBuffer query;
1820 : : PGresult *res;
1821 : : SimpleStringListCell *cell;
1822 : : int i;
1823 : :
1824 [ + + ]: 1317 : if (patterns->head == NULL)
1825 : 1288 : return; /* nothing to do */
1826 : :
1827 : 29 : query = createPQExpBuffer();
1828 : :
1829 : : /*
1830 : : * this might sometimes result in duplicate entries in the OID list, but
1831 : : * we don't care.
1832 : : */
1833 : :
1834 [ + + ]: 59 : for (cell = patterns->head; cell; cell = cell->next)
1835 : : {
1836 : : PQExpBufferData dbbuf;
1837 : : int dotcnt;
1838 : :
1839 : : /*
1840 : : * Query must remain ABSOLUTELY devoid of unqualified names. This
1841 : : * would be unnecessary given a pg_table_is_visible() variant taking a
1842 : : * search_path argument.
1843 : : *
1844 : : * Conflict log tables, which are in the pg_conflict schema, are not
1845 : : * selectable, as they are dumped only by pg_upgrade, along with the
1846 : : * subscriptions.
1847 : : *
1848 : : * For with_child_tables, we start with the basic query's results and
1849 : : * recursively search the inheritance tree to add child tables.
1850 : : */
1851 [ + + ]: 35 : if (with_child_tables)
1852 : : {
1853 : 6 : appendPQExpBufferStr(query, "WITH RECURSIVE partition_tree (relid) AS (\n");
1854 : : }
1855 : :
1856 : 35 : appendPQExpBuffer(query,
1857 : : "SELECT c.oid"
1858 : : "\nFROM pg_catalog.pg_class c"
1859 : : "\n LEFT JOIN pg_catalog.pg_namespace n"
1860 : : "\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace"
1861 : : "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY"
1862 : : "\n (array['%c', '%c', '%c', '%c', '%c', '%c'])"
1863 : : "\n AND n.nspname OPERATOR(pg_catalog.<>) 'pg_conflict'\n",
1864 : : RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1865 : : RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
1866 : : RELKIND_PARTITIONED_TABLE);
1867 : 35 : initPQExpBuffer(&dbbuf);
1868 : 35 : processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1869 : : false, "n.nspname", "c.relname", NULL,
1870 : : "pg_catalog.pg_table_is_visible(c.oid)", &dbbuf,
1871 : : &dotcnt);
1872 [ + + ]: 35 : if (dotcnt > 2)
1873 : 1 : pg_fatal("improper relation name (too many dotted names): %s",
1874 : : cell->val);
1875 [ + + ]: 34 : else if (dotcnt == 2)
1876 : 2 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1877 : 32 : termPQExpBuffer(&dbbuf);
1878 : :
1879 [ + + ]: 32 : if (with_child_tables)
1880 : : {
1881 : 6 : appendPQExpBufferStr(query, "UNION"
1882 : : "\nSELECT i.inhrelid"
1883 : : "\nFROM partition_tree p"
1884 : : "\n JOIN pg_catalog.pg_inherits i"
1885 : : "\n ON p.relid OPERATOR(pg_catalog.=) i.inhparent"
1886 : : "\n)"
1887 : : "\nSELECT relid FROM partition_tree");
1888 : : }
1889 : :
1890 : 32 : ExecuteSqlStatement(fout, "RESET search_path");
1891 : 32 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1892 : 32 : PQclear(ExecuteSqlQueryForSingleRow(fout,
1893 : : ALWAYS_SECURE_SEARCH_PATH_SQL));
1894 [ + + + + ]: 32 : if (strict_names && PQntuples(res) == 0)
1895 : 2 : pg_fatal("no matching tables were found for pattern \"%s\"", cell->val);
1896 : :
1897 [ + + ]: 74 : for (i = 0; i < PQntuples(res); i++)
1898 : : {
1899 : 44 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1900 : : }
1901 : :
1902 : 30 : PQclear(res);
1903 : 30 : resetPQExpBuffer(query);
1904 : : }
1905 : :
1906 : 24 : destroyPQExpBuffer(query);
1907 : : }
1908 : :
1909 : : /*
1910 : : * Verifies that the connected database name matches the given database name,
1911 : : * and if not, dies with an error about the given pattern.
1912 : : *
1913 : : * The 'dbname' argument should be a literal name parsed from 'pattern'.
1914 : : */
1915 : : static void
1916 : 5 : prohibit_crossdb_refs(PGconn *conn, const char *dbname, const char *pattern)
1917 : : {
1918 : : const char *db;
1919 : :
1920 : 5 : db = PQdb(conn);
1921 [ - + ]: 5 : if (db == NULL)
1922 : 0 : pg_fatal("You are currently not connected to a database.");
1923 : :
1924 [ + - ]: 5 : if (strcmp(db, dbname) != 0)
1925 : 5 : pg_fatal("cross-database references are not implemented: %s",
1926 : : pattern);
1927 : 0 : }
1928 : :
1929 : : /*
1930 : : * checkExtensionMembership
1931 : : * Determine whether object is an extension member, and if so,
1932 : : * record an appropriate dependency and set the object's dump flag.
1933 : : *
1934 : : * It's important to call this for each object that could be an extension
1935 : : * member. Generally, we integrate this with determining the object's
1936 : : * to-be-dumped-ness, since extension membership overrides other rules for that.
1937 : : *
1938 : : * Returns true if object is an extension member, else false.
1939 : : */
1940 : : static bool
1941 : 691835 : checkExtensionMembership(DumpableObject *dobj, Archive *fout)
1942 : : {
1943 : 691835 : ExtensionInfo *ext = findOwningExtension(dobj->catId);
1944 : :
1945 [ + + ]: 691835 : if (ext == NULL)
1946 : 690988 : return false;
1947 : :
1948 : 847 : dobj->ext_member = true;
1949 : :
1950 : : /* Record dependency so that getDependencies needn't deal with that */
1951 : 847 : addObjectDependency(dobj, ext->dobj.dumpId);
1952 : :
1953 : : /*
1954 : : * Mark the member object to have any non-initial ACLs dumped. (Any
1955 : : * initial ACLs will be removed later, using data from pg_init_privs, so
1956 : : * that we'll dump only the delta from the extension's initial setup.)
1957 : : *
1958 : : * In binary upgrades, we still dump all components of the members
1959 : : * individually, since the idea is to exactly reproduce the database
1960 : : * contents rather than replace the extension contents with something
1961 : : * different.
1962 : : *
1963 : : * Note: it might be interesting someday to implement storage and delta
1964 : : * dumping of extension members' RLS policies and/or security labels.
1965 : : * However there is a pitfall for RLS policies: trying to dump them
1966 : : * requires getting a lock on their tables, and the calling user might not
1967 : : * have privileges for that. We need no lock to examine a table's ACLs,
1968 : : * so the current feature doesn't have a problem of that sort.
1969 : : */
1970 [ + + ]: 847 : if (fout->dopt->binary_upgrade)
1971 : 202 : dobj->dump = ext->dobj.dump;
1972 : : else
1973 : 645 : dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL);
1974 : :
1975 : 847 : return true;
1976 : : }
1977 : :
1978 : : /*
1979 : : * selectDumpableNamespace: policy-setting subroutine
1980 : : * Mark a namespace as to be dumped or not
1981 : : */
1982 : : static void
1983 : 1772 : selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout)
1984 : : {
1985 : : /*
1986 : : * DUMP_COMPONENT_DEFINITION typically implies a CREATE SCHEMA statement
1987 : : * and (for --clean) a DROP SCHEMA statement. (In the absence of
1988 : : * DUMP_COMPONENT_DEFINITION, this value is irrelevant.)
1989 : : */
1990 : 1772 : nsinfo->create = true;
1991 : :
1992 : : /*
1993 : : * If specific tables are being dumped, do not dump any complete
1994 : : * namespaces. If specific namespaces are being dumped, dump just those
1995 : : * namespaces. Otherwise, dump all non-system namespaces.
1996 : : */
1997 [ + + ]: 1772 : if (table_include_oids.head != NULL)
1998 : 61 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1999 [ + + ]: 1711 : else if (schema_include_oids.head != NULL)
2000 : 205 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump =
2001 : 205 : simple_oid_list_member(&schema_include_oids,
2002 : : nsinfo->dobj.catId.oid) ?
2003 [ + + ]: 205 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2004 [ + + ]: 1506 : else if (strcmp(nsinfo->dobj.name, "pg_catalog") == 0)
2005 : : {
2006 : : /*
2007 : : * We dump out any ACLs defined in pg_catalog, if they are interesting
2008 : : * (and not the original ACLs which were set at initdb time, see
2009 : : * pg_init_privs).
2010 : : */
2011 : 193 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
2012 : : }
2013 [ + + ]: 1313 : else if (strcmp(nsinfo->dobj.name, "pg_conflict") == 0)
2014 : : {
2015 : : /*
2016 : : * The pg_conflict schema holds the conflict log tables of the
2017 : : * subscriptions. In binary-upgrade mode, which only pg_upgrade uses,
2018 : : * dump the tables with their privileges, comments, and security
2019 : : * labels so that CREATE SUBSCRIPTION can link to them, and dump only
2020 : : * the ACL of the schema itself, as for pg_catalog. In other modes,
2021 : : * CREATE SUBSCRIPTION creates the tables again, so dump nothing.
2022 : : */
2023 [ + + ]: 193 : if (fout->dopt->binary_upgrade)
2024 : : {
2025 : 44 : nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
2026 : 44 : nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL;
2027 : : }
2028 : : else
2029 : 149 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2030 : : }
2031 [ + + ]: 1120 : else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
2032 [ + + ]: 563 : strcmp(nsinfo->dobj.name, "information_schema") == 0)
2033 : : {
2034 : : /* Other system schemas don't get dumped */
2035 : 750 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2036 : : }
2037 [ + + ]: 370 : else if (strcmp(nsinfo->dobj.name, "public") == 0)
2038 : : {
2039 : : /*
2040 : : * The public schema is a strange beast that sits in a sort of
2041 : : * no-mans-land between being a system object and a user object.
2042 : : * CREATE SCHEMA would fail, so its DUMP_COMPONENT_DEFINITION is just
2043 : : * a comment and an indication of ownership. If the owner is the
2044 : : * default, omit that superfluous DUMP_COMPONENT_DEFINITION. Before
2045 : : * v15, the default owner was BOOTSTRAP_SUPERUSERID.
2046 : : */
2047 : 189 : nsinfo->create = false;
2048 : 189 : nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2049 [ + + ]: 189 : if (nsinfo->nspowner == ROLE_PG_DATABASE_OWNER)
2050 : 129 : nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION;
2051 : 189 : nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL;
2052 : :
2053 : : /*
2054 : : * Also, make like it has a comment even if it doesn't; this is so
2055 : : * that we'll emit a command to drop the comment, if appropriate.
2056 : : * (Without this, we'd not call dumpCommentExtended for it.)
2057 : : */
2058 : 189 : nsinfo->dobj.components |= DUMP_COMPONENT_COMMENT;
2059 : : }
2060 : : else
2061 : 181 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2062 : :
2063 : : /*
2064 : : * In any case, a namespace can be excluded by an exclusion switch
2065 : : */
2066 [ + + + + ]: 2391 : if (nsinfo->dobj.dump_contains &&
2067 : 619 : simple_oid_list_member(&schema_exclude_oids,
2068 : : nsinfo->dobj.catId.oid))
2069 : 3 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2070 : :
2071 : : /*
2072 : : * If the schema belongs to an extension, allow extension membership to
2073 : : * override the dump decision for the schema itself. However, this does
2074 : : * not change dump_contains, so this won't change what we do with objects
2075 : : * within the schema. (If they belong to the extension, they'll get
2076 : : * suppressed by it, otherwise not.)
2077 : : */
2078 : 1772 : (void) checkExtensionMembership(&nsinfo->dobj, fout);
2079 : 1772 : }
2080 : :
2081 : : /*
2082 : : * selectDumpableTable: policy-setting subroutine
2083 : : * Mark a table as to be dumped or not
2084 : : */
2085 : : static void
2086 : 57513 : selectDumpableTable(TableInfo *tbinfo, Archive *fout)
2087 : : {
2088 [ + + ]: 57513 : if (checkExtensionMembership(&tbinfo->dobj, fout))
2089 : 225 : return; /* extension membership overrides all else */
2090 : :
2091 : : /*
2092 : : * If specific tables are being dumped, dump just those tables; else, dump
2093 : : * according to the parent namespace's dump flag.
2094 : : */
2095 [ + + ]: 57288 : if (table_include_oids.head != NULL)
2096 : 5362 : tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
2097 : : tbinfo->dobj.catId.oid) ?
2098 [ + + ]: 2681 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2099 : : else
2100 : 54607 : tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains;
2101 : :
2102 : : /*
2103 : : * In any case, a table can be excluded by an exclusion switch
2104 : : */
2105 [ + + + + ]: 94307 : if (tbinfo->dobj.dump &&
2106 : 37019 : simple_oid_list_member(&table_exclude_oids,
2107 : : tbinfo->dobj.catId.oid))
2108 : 12 : tbinfo->dobj.dump = DUMP_COMPONENT_NONE;
2109 : : }
2110 : :
2111 : : /*
2112 : : * selectDumpableType: policy-setting subroutine
2113 : : * Mark a type as to be dumped or not
2114 : : *
2115 : : * If it's a table's rowtype or an autogenerated array type, we also apply a
2116 : : * special type code to facilitate sorting into the desired order. (We don't
2117 : : * want to consider those to be ordinary types because that would bring tables
2118 : : * up into the datatype part of the dump order.) We still set the object's
2119 : : * dump flag; that's not going to cause the dummy type to be dumped, but we
2120 : : * need it so that casts involving such types will be dumped correctly -- see
2121 : : * dumpCast. This means the flag should be set the same as for the underlying
2122 : : * object (the table or base type).
2123 : : */
2124 : : static void
2125 : 157984 : selectDumpableType(TypeInfo *tyinfo, Archive *fout)
2126 : : {
2127 : : /* skip complex types, except for standalone composite types */
2128 [ + + ]: 157984 : if (OidIsValid(tyinfo->typrelid) &&
2129 [ + + ]: 56813 : tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
2130 : : {
2131 : 56567 : TableInfo *tytable = findTableByOid(tyinfo->typrelid);
2132 : :
2133 : 56567 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2134 [ + - ]: 56567 : if (tytable != NULL)
2135 : 56567 : tyinfo->dobj.dump = tytable->dobj.dump;
2136 : : else
2137 : 0 : tyinfo->dobj.dump = DUMP_COMPONENT_NONE;
2138 : 56567 : return;
2139 : : }
2140 : :
2141 : : /* skip auto-generated array and multirange types */
2142 [ + + + + ]: 101417 : if (tyinfo->isArray || tyinfo->isMultirange)
2143 : : {
2144 : 77282 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2145 : :
2146 : : /*
2147 : : * Fall through to set the dump flag; we assume that the subsequent
2148 : : * rules will do the same thing as they would for the array's base
2149 : : * type or multirange's range type. (We cannot reliably look up the
2150 : : * base type here, since getTypes may not have processed it yet.)
2151 : : */
2152 : : }
2153 : :
2154 [ + + ]: 101417 : if (checkExtensionMembership(&tyinfo->dobj, fout))
2155 : 150 : return; /* extension membership overrides all else */
2156 : :
2157 : : /* Dump based on if the contents of the namespace are being dumped */
2158 : 101267 : tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains;
2159 : : }
2160 : :
2161 : : /*
2162 : : * selectDumpableDefaultACL: policy-setting subroutine
2163 : : * Mark a default ACL as to be dumped or not
2164 : : *
2165 : : * For per-schema default ACLs, dump if the schema is to be dumped.
2166 : : * Otherwise dump if we are dumping "everything". Note that dumpSchema
2167 : : * and aclsSkip are checked separately.
2168 : : */
2169 : : static void
2170 : 206 : selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt)
2171 : : {
2172 : : /* Default ACLs can't be extension members */
2173 : :
2174 [ + + ]: 206 : if (dinfo->dobj.namespace)
2175 : : /* default ACLs are considered part of the namespace */
2176 : 96 : dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains;
2177 : : else
2178 : 110 : dinfo->dobj.dump = dopt->include_everything ?
2179 [ + + ]: 110 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2180 : 206 : }
2181 : :
2182 : : /*
2183 : : * selectDumpableCast: policy-setting subroutine
2184 : : * Mark a cast as to be dumped or not
2185 : : *
2186 : : * Casts do not belong to any particular namespace (since they haven't got
2187 : : * names), nor do they have identifiable owners. To distinguish user-defined
2188 : : * casts from built-in ones, we must resort to checking whether the cast's
2189 : : * OID is in the range reserved for initdb.
2190 : : */
2191 : : static void
2192 : 52335 : selectDumpableCast(CastInfo *cast, Archive *fout)
2193 : : {
2194 [ - + ]: 52335 : if (checkExtensionMembership(&cast->dobj, fout))
2195 : 0 : return; /* extension membership overrides all else */
2196 : :
2197 : : /*
2198 : : * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
2199 : : * support ACLs currently.
2200 : : */
2201 [ + + ]: 52335 : if (cast->dobj.catId.oid <= g_last_builtin_oid)
2202 : 52245 : cast->dobj.dump = DUMP_COMPONENT_NONE;
2203 : : else
2204 : 90 : cast->dobj.dump = fout->dopt->include_everything ?
2205 [ + + ]: 90 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2206 : : }
2207 : :
2208 : : /*
2209 : : * selectDumpableProcLang: policy-setting subroutine
2210 : : * Mark a procedural language as to be dumped or not
2211 : : *
2212 : : * Procedural languages do not belong to any particular namespace. To
2213 : : * identify built-in languages, we must resort to checking whether the
2214 : : * language's OID is in the range reserved for initdb.
2215 : : */
2216 : : static void
2217 : 263 : selectDumpableProcLang(ProcLangInfo *plang, Archive *fout)
2218 : : {
2219 [ + + ]: 263 : if (checkExtensionMembership(&plang->dobj, fout))
2220 : 209 : return; /* extension membership overrides all else */
2221 : :
2222 : : /*
2223 : : * Only include procedural languages when we are dumping everything.
2224 : : *
2225 : : * For from-initdb procedural languages, only include ACLs, as we do for
2226 : : * the pg_catalog namespace. We need this because procedural languages do
2227 : : * not live in any namespace.
2228 : : */
2229 [ + + ]: 54 : if (!fout->dopt->include_everything)
2230 : 9 : plang->dobj.dump = DUMP_COMPONENT_NONE;
2231 : : else
2232 : : {
2233 [ + + ]: 45 : if (plang->dobj.catId.oid <= g_last_builtin_oid)
2234 : 6 : plang->dobj.dump = DUMP_COMPONENT_ACL;
2235 : : else
2236 : 39 : plang->dobj.dump = DUMP_COMPONENT_ALL;
2237 : : }
2238 : : }
2239 : :
2240 : : /*
2241 : : * selectDumpableAccessMethod: policy-setting subroutine
2242 : : * Mark an access method as to be dumped or not
2243 : : *
2244 : : * Access methods do not belong to any particular namespace. To identify
2245 : : * built-in access methods, we must resort to checking whether the
2246 : : * method's OID is in the range reserved for initdb.
2247 : : */
2248 : : static void
2249 : 1633 : selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout)
2250 : : {
2251 [ + + ]: 1633 : if (checkExtensionMembership(&method->dobj, fout))
2252 : 25 : return; /* extension membership overrides all else */
2253 : :
2254 : : /*
2255 : : * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
2256 : : * they do not support ACLs currently.
2257 : : */
2258 [ + + ]: 1608 : if (method->dobj.catId.oid <= g_last_builtin_oid)
2259 : 1505 : method->dobj.dump = DUMP_COMPONENT_NONE;
2260 : : else
2261 : 103 : method->dobj.dump = fout->dopt->include_everything ?
2262 [ + + ]: 103 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2263 : : }
2264 : :
2265 : : /*
2266 : : * selectDumpableExtension: policy-setting subroutine
2267 : : * Mark an extension as to be dumped or not
2268 : : *
2269 : : * Built-in extensions should be skipped except for checking ACLs, since we
2270 : : * assume those will already be installed in the target database. We identify
2271 : : * such extensions by their having OIDs in the range reserved for initdb.
2272 : : * We dump all user-added extensions by default. No extensions are dumped
2273 : : * if include_everything is false (i.e., a --schema or --table switch was
2274 : : * given), except if --extension specifies a list of extensions to dump.
2275 : : */
2276 : : static void
2277 : 247 : selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
2278 : : {
2279 : : /*
2280 : : * Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to
2281 : : * change permissions on their member objects, if they wish to, and have
2282 : : * those changes preserved.
2283 : : */
2284 [ + + ]: 247 : if (extinfo->dobj.catId.oid <= g_last_builtin_oid)
2285 : 216 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
2286 : : else
2287 : : {
2288 : : /* check if there is a list of extensions to dump */
2289 [ + + ]: 31 : if (extension_include_oids.head != NULL)
2290 : 4 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2291 : 4 : simple_oid_list_member(&extension_include_oids,
2292 : : extinfo->dobj.catId.oid) ?
2293 [ + + ]: 4 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2294 : : else
2295 : 27 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2296 : 27 : dopt->include_everything ?
2297 [ + + ]: 27 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2298 : :
2299 : : /* check that the extension is not explicitly excluded */
2300 [ + + + + ]: 58 : if (extinfo->dobj.dump &&
2301 : 27 : simple_oid_list_member(&extension_exclude_oids,
2302 : : extinfo->dobj.catId.oid))
2303 : 2 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE;
2304 : : }
2305 : 247 : }
2306 : :
2307 : : /*
2308 : : * selectDumpablePublicationObject: policy-setting subroutine
2309 : : * Mark a publication object as to be dumped or not
2310 : : *
2311 : : * A publication can have schemas and tables which have schemas, but those are
2312 : : * ignored in decision making, because publications are only dumped when we are
2313 : : * dumping everything.
2314 : : */
2315 : : static void
2316 : 502 : selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout)
2317 : : {
2318 [ - + ]: 502 : if (checkExtensionMembership(dobj, fout))
2319 : 0 : return; /* extension membership overrides all else */
2320 : :
2321 : 502 : dobj->dump = fout->dopt->include_everything ?
2322 [ + + ]: 502 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2323 : : }
2324 : :
2325 : : /*
2326 : : * selectDumpableStatisticsObject: policy-setting subroutine
2327 : : * Mark an extended statistics object as to be dumped or not
2328 : : *
2329 : : * We dump an extended statistics object if the schema it's in and the table
2330 : : * it's for are being dumped. (This'll need more thought if statistics
2331 : : * objects ever support cross-table stats.)
2332 : : */
2333 : : static void
2334 : 233 : selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout)
2335 : : {
2336 [ - + ]: 233 : if (checkExtensionMembership(&sobj->dobj, fout))
2337 : 0 : return; /* extension membership overrides all else */
2338 : :
2339 : 233 : sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains;
2340 [ + - ]: 233 : if (sobj->stattable == NULL ||
2341 [ + + ]: 233 : !(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION))
2342 : 35 : sobj->dobj.dump = DUMP_COMPONENT_NONE;
2343 : : }
2344 : :
2345 : : /*
2346 : : * selectDumpableObject: policy-setting subroutine
2347 : : * Mark a generic dumpable object as to be dumped or not
2348 : : *
2349 : : * Use this only for object types without a special-case routine above.
2350 : : */
2351 : : static void
2352 : 476167 : selectDumpableObject(DumpableObject *dobj, Archive *fout)
2353 : : {
2354 [ + + ]: 476167 : if (checkExtensionMembership(dobj, fout))
2355 : 213 : return; /* extension membership overrides all else */
2356 : :
2357 : : /*
2358 : : * Default policy is to dump if parent namespace is dumpable, or for
2359 : : * non-namespace-associated items, dump if we're dumping "everything".
2360 : : */
2361 [ + + ]: 475954 : if (dobj->namespace)
2362 : 474989 : dobj->dump = dobj->namespace->dobj.dump_contains;
2363 : : else
2364 : 965 : dobj->dump = fout->dopt->include_everything ?
2365 [ + + ]: 965 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2366 : : }
2367 : :
2368 : : /*
2369 : : * Dump a table's contents for loading using the COPY command
2370 : : * - this routine is called by the Archiver when it wants the table
2371 : : * to be dumped.
2372 : : */
2373 : : static int
2374 : 4441 : dumpTableData_copy(Archive *fout, const void *dcontext)
2375 : : {
2376 : 4441 : const TableDataInfo *tdinfo = dcontext;
2377 : 4441 : const TableInfo *tbinfo = tdinfo->tdtable;
2378 : 4441 : const char *classname = tbinfo->dobj.name;
2379 : 4441 : PQExpBuffer q = createPQExpBuffer();
2380 : :
2381 : : /*
2382 : : * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
2383 : : * which uses it already.
2384 : : */
2385 : 4441 : PQExpBuffer clistBuf = createPQExpBuffer();
2386 : 4441 : PGconn *conn = GetConnection(fout);
2387 : : PGresult *res;
2388 : : int ret;
2389 : : char *copybuf;
2390 : : const char *column_list;
2391 : :
2392 : 4441 : pg_log_info("dumping contents of table \"%s.%s\"",
2393 : : tbinfo->dobj.namespace->dobj.name, classname);
2394 : :
2395 : : /*
2396 : : * Specify the column list explicitly so that we have no possibility of
2397 : : * retrieving data in the wrong column order. (The default column
2398 : : * ordering of COPY will not be what we want in certain corner cases
2399 : : * involving ADD COLUMN and inheritance.)
2400 : : */
2401 : 4441 : column_list = fmtCopyColumnList(tbinfo, clistBuf);
2402 : :
2403 : : /*
2404 : : * Use COPY (SELECT ...) TO when dumping a foreign table's data, when a
2405 : : * filter condition was specified, and when in binary upgrade mode and
2406 : : * dumping an old pg_largeobject_metadata defined WITH OIDS. For other
2407 : : * cases a simple COPY suffices.
2408 : : */
2409 [ + + + + ]: 4441 : if (tdinfo->filtercond || tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
2410 [ - + - - ]: 4396 : (fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
2411 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
2412 : : {
2413 : : /* Temporary allows to access to foreign tables to dump data */
2414 [ + + ]: 45 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2415 : 1 : set_restrict_relation_kind(fout, "view");
2416 : :
2417 : 45 : appendPQExpBufferStr(q, "COPY (SELECT ");
2418 : : /* klugery to get rid of parens in column list */
2419 [ + - ]: 45 : if (strlen(column_list) > 2)
2420 : : {
2421 : 45 : appendPQExpBufferStr(q, column_list + 1);
2422 : 45 : q->data[q->len - 1] = ' ';
2423 : : }
2424 : : else
2425 : 0 : appendPQExpBufferStr(q, "* ");
2426 : :
2427 : 90 : appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
2428 : 45 : fmtQualifiedDumpable(tbinfo),
2429 [ + + ]: 45 : tdinfo->filtercond ? tdinfo->filtercond : "");
2430 : : }
2431 : : else
2432 : : {
2433 : 4396 : appendPQExpBuffer(q, "COPY %s %s TO stdout;",
2434 : 4396 : fmtQualifiedDumpable(tbinfo),
2435 : : column_list);
2436 : : }
2437 : 4441 : res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
2438 : 4440 : PQclear(res);
2439 : 4440 : destroyPQExpBuffer(clistBuf);
2440 : :
2441 : : for (;;)
2442 : : {
2443 : 1822929 : ret = PQgetCopyData(conn, ©buf, 0);
2444 : :
2445 [ + + ]: 1822929 : if (ret < 0)
2446 : 4440 : break; /* done or error */
2447 : :
2448 [ + - ]: 1818489 : if (copybuf)
2449 : : {
2450 : 1818489 : WriteData(fout, copybuf, ret);
2451 : 1818489 : PQfreemem(copybuf);
2452 : : }
2453 : :
2454 : : /* ----------
2455 : : * THROTTLE:
2456 : : *
2457 : : * There was considerable discussion in late July, 2000 regarding
2458 : : * slowing down pg_dump when backing up large tables. Users with both
2459 : : * slow & fast (multi-processor) machines experienced performance
2460 : : * degradation when doing a backup.
2461 : : *
2462 : : * Initial attempts based on sleeping for a number of ms for each ms
2463 : : * of work were deemed too complex, then a simple 'sleep in each loop'
2464 : : * implementation was suggested. The latter failed because the loop
2465 : : * was too tight. Finally, the following was implemented:
2466 : : *
2467 : : * If throttle is non-zero, then
2468 : : * See how long since the last sleep.
2469 : : * Work out how long to sleep (based on ratio).
2470 : : * If sleep is more than 100ms, then
2471 : : * sleep
2472 : : * reset timer
2473 : : * EndIf
2474 : : * EndIf
2475 : : *
2476 : : * where the throttle value was the number of ms to sleep per ms of
2477 : : * work. The calculation was done in each loop.
2478 : : *
2479 : : * Most of the hard work is done in the backend, and this solution
2480 : : * still did not work particularly well: on slow machines, the ratio
2481 : : * was 50:1, and on medium paced machines, 1:1, and on fast
2482 : : * multi-processor machines, it had little or no effect, for reasons
2483 : : * that were unclear.
2484 : : *
2485 : : * Further discussion ensued, and the proposal was dropped.
2486 : : *
2487 : : * For those people who want this feature, it can be implemented using
2488 : : * gettimeofday in each loop, calculating the time since last sleep,
2489 : : * multiplying that by the sleep ratio, then if the result is more
2490 : : * than a preset 'minimum sleep time' (say 100ms), call the 'select'
2491 : : * function to sleep for a subsecond period ie.
2492 : : *
2493 : : * select(0, NULL, NULL, NULL, &tvi);
2494 : : *
2495 : : * This will return after the interval specified in the structure tvi.
2496 : : * Finally, call gettimeofday again to save the 'last sleep time'.
2497 : : * ----------
2498 : : */
2499 : : }
2500 : 4440 : archprintf(fout, "\\.\n\n\n");
2501 : :
2502 [ - + ]: 4440 : if (ret == -2)
2503 : : {
2504 : : /* copy data transfer failed */
2505 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.", classname);
2506 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2507 : 0 : pg_log_error_detail("Command was: %s", q->data);
2508 : 0 : exit_nicely(1);
2509 : : }
2510 : :
2511 : : /* Check command status and return to normal libpq state */
2512 : 4440 : res = PQgetResult(conn);
2513 [ - + ]: 4440 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
2514 : : {
2515 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetResult() failed.", classname);
2516 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2517 : 0 : pg_log_error_detail("Command was: %s", q->data);
2518 : 0 : exit_nicely(1);
2519 : : }
2520 : 4440 : PQclear(res);
2521 : :
2522 : : /* Do this to ensure we've pumped libpq back to idle state */
2523 [ - + ]: 4440 : if (PQgetResult(conn) != NULL)
2524 : 0 : pg_log_warning("unexpected extra results during COPY of table \"%s\"",
2525 : : classname);
2526 : :
2527 : 4440 : destroyPQExpBuffer(q);
2528 : :
2529 : : /* Revert back the setting */
2530 [ - + ]: 4440 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2531 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2532 : :
2533 : 4440 : return 1;
2534 : : }
2535 : :
2536 : : /*
2537 : : * Dump table data using INSERT commands.
2538 : : *
2539 : : * Caution: when we restore from an archive file direct to database, the
2540 : : * INSERT commands emitted by this function have to be parsed by
2541 : : * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments,
2542 : : * E'' strings, or dollar-quoted strings. So don't emit anything like that.
2543 : : */
2544 : : static int
2545 : 105 : dumpTableData_insert(Archive *fout, const void *dcontext)
2546 : : {
2547 : 105 : const TableDataInfo *tdinfo = dcontext;
2548 : 105 : const TableInfo *tbinfo = tdinfo->tdtable;
2549 : 105 : DumpOptions *dopt = fout->dopt;
2550 : 105 : PQExpBuffer q = createPQExpBuffer();
2551 : 105 : PQExpBuffer insertStmt = NULL;
2552 : : char *attgenerated;
2553 : : PGresult *res;
2554 : : int nfields,
2555 : : i;
2556 : 105 : int rows_per_statement = dopt->dump_inserts;
2557 : 105 : int rows_this_statement = 0;
2558 : :
2559 : : /* Temporary allows to access to foreign tables to dump data */
2560 [ - + ]: 105 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2561 : 0 : set_restrict_relation_kind(fout, "view");
2562 : :
2563 : : /*
2564 : : * If we're going to emit INSERTs with column names, the most efficient
2565 : : * way to deal with generated columns is to exclude them entirely. For
2566 : : * INSERTs without column names, we have to emit DEFAULT rather than the
2567 : : * actual column value --- but we can save a few cycles by fetching nulls
2568 : : * rather than the uninteresting-to-us value.
2569 : : */
2570 : 105 : attgenerated = pg_malloc_array(char, tbinfo->numatts);
2571 : 105 : appendPQExpBufferStr(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT ");
2572 : 105 : nfields = 0;
2573 [ + + ]: 331 : for (i = 0; i < tbinfo->numatts; i++)
2574 : : {
2575 [ + + ]: 226 : if (tbinfo->attisdropped[i])
2576 : 6 : continue;
2577 [ + + + + ]: 220 : if (tbinfo->attgenerated[i] && dopt->column_inserts)
2578 : 10 : continue;
2579 [ + + ]: 210 : if (nfields > 0)
2580 : 113 : appendPQExpBufferStr(q, ", ");
2581 [ + + ]: 210 : if (tbinfo->attgenerated[i])
2582 : 12 : appendPQExpBufferStr(q, "NULL");
2583 : : else
2584 : 198 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[i]));
2585 : 210 : attgenerated[nfields] = tbinfo->attgenerated[i];
2586 : 210 : nfields++;
2587 : : }
2588 : : /* Servers before 9.4 will complain about zero-column SELECT */
2589 [ + + ]: 105 : if (nfields == 0)
2590 : 8 : appendPQExpBufferStr(q, "NULL");
2591 : 105 : appendPQExpBuffer(q, " FROM ONLY %s",
2592 : 105 : fmtQualifiedDumpable(tbinfo));
2593 [ - + ]: 105 : if (tdinfo->filtercond)
2594 : 0 : appendPQExpBuffer(q, " %s", tdinfo->filtercond);
2595 : :
2596 : 105 : ExecuteSqlStatement(fout, q->data);
2597 : :
2598 : : while (1)
2599 : : {
2600 : 168 : res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
2601 : : PGRES_TUPLES_OK);
2602 : :
2603 : : /* cross-check field count, allowing for dummy NULL if any */
2604 [ + + + - ]: 168 : if (nfields != PQnfields(res) &&
2605 [ - + ]: 12 : !(nfields == 0 && PQnfields(res) == 1))
2606 : 0 : pg_fatal("wrong number of fields retrieved from table \"%s\"",
2607 : : tbinfo->dobj.name);
2608 : :
2609 : : /*
2610 : : * First time through, we build as much of the INSERT statement as
2611 : : * possible in "insertStmt", which we can then just print for each
2612 : : * statement. If the table happens to have zero dumpable columns then
2613 : : * this will be a complete statement, otherwise it will end in
2614 : : * "VALUES" and be ready to have the row's column values printed.
2615 : : */
2616 [ + + ]: 168 : if (insertStmt == NULL)
2617 : : {
2618 : : const TableInfo *targettab;
2619 : :
2620 : 105 : insertStmt = createPQExpBuffer();
2621 : :
2622 : : /*
2623 : : * When load-via-partition-root is set or forced, get the root
2624 : : * table name for the partition table, so that we can reload data
2625 : : * through the root table.
2626 : : */
2627 [ + + ]: 105 : if (tbinfo->ispartition &&
2628 [ + - + + ]: 54 : (dopt->load_via_partition_root ||
2629 : 27 : forcePartitionRootLoad(tbinfo)))
2630 : 10 : targettab = getRootTableInfo(tbinfo);
2631 : : else
2632 : 95 : targettab = tbinfo;
2633 : :
2634 : 105 : appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
2635 : 105 : fmtQualifiedDumpable(targettab));
2636 : :
2637 : : /* corner case for zero-column table */
2638 [ + + ]: 105 : if (nfields == 0)
2639 : : {
2640 : 8 : appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n");
2641 : : }
2642 : : else
2643 : : {
2644 : : /* append the list of column names if required */
2645 [ + + ]: 97 : if (dopt->column_inserts)
2646 : : {
2647 : 42 : appendPQExpBufferChar(insertStmt, '(');
2648 [ + + ]: 128 : for (int field = 0; field < nfields; field++)
2649 : : {
2650 [ + + ]: 86 : if (field > 0)
2651 : 44 : appendPQExpBufferStr(insertStmt, ", ");
2652 : 86 : appendPQExpBufferStr(insertStmt,
2653 : 86 : fmtId(PQfname(res, field)));
2654 : : }
2655 : 42 : appendPQExpBufferStr(insertStmt, ") ");
2656 : : }
2657 : :
2658 [ + + ]: 97 : if (tbinfo->needs_override)
2659 : 5 : appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE ");
2660 : :
2661 : 97 : appendPQExpBufferStr(insertStmt, "VALUES");
2662 : : }
2663 : : }
2664 : :
2665 [ + + ]: 3660 : for (int tuple = 0; tuple < PQntuples(res); tuple++)
2666 : : {
2667 : : /* Write the INSERT if not in the middle of a multi-row INSERT. */
2668 [ + + ]: 3492 : if (rows_this_statement == 0)
2669 : 3482 : archputs(insertStmt->data, fout);
2670 : :
2671 : : /*
2672 : : * If it is zero-column table then we've already written the
2673 : : * complete statement, which will mean we've disobeyed
2674 : : * --rows-per-insert when it's set greater than 1. We do support
2675 : : * a way to make this multi-row with: SELECT UNION ALL SELECT
2676 : : * UNION ALL ... but that's non-standard so we should avoid it
2677 : : * given that using INSERTs is mostly only ever needed for
2678 : : * cross-database exports.
2679 : : */
2680 [ + + ]: 3492 : if (nfields == 0)
2681 : 7 : continue;
2682 : :
2683 : : /* Emit a row heading */
2684 [ + + ]: 3485 : if (rows_per_statement == 1)
2685 : 3463 : archputs(" (", fout);
2686 [ + + ]: 22 : else if (rows_this_statement > 0)
2687 : 10 : archputs(",\n\t(", fout);
2688 : : else
2689 : 12 : archputs("\n\t(", fout);
2690 : :
2691 [ + + ]: 10531 : for (int field = 0; field < nfields; field++)
2692 : : {
2693 [ + + ]: 7046 : if (field > 0)
2694 : 3561 : archputs(", ", fout);
2695 [ + + ]: 7046 : if (attgenerated[field])
2696 : : {
2697 : 10 : archputs("DEFAULT", fout);
2698 : 10 : continue;
2699 : : }
2700 [ + + ]: 7036 : if (PQgetisnull(res, tuple, field))
2701 : : {
2702 : 86 : archputs("NULL", fout);
2703 : 86 : continue;
2704 : : }
2705 : :
2706 : : /* XXX This code is partially duplicated in ruleutils.c */
2707 [ + + + + ]: 6950 : switch (PQftype(res, field))
2708 : : {
2709 : 4887 : case INT2OID:
2710 : : case INT4OID:
2711 : : case INT8OID:
2712 : : case OIDOID:
2713 : : case FLOAT4OID:
2714 : : case FLOAT8OID:
2715 : : case NUMERICOID:
2716 : : {
2717 : : /*
2718 : : * These types are printed without quotes unless
2719 : : * they contain values that aren't accepted by the
2720 : : * scanner unquoted (e.g., 'NaN'). Note that
2721 : : * strtod() and friends might accept NaN, so we
2722 : : * can't use that to test.
2723 : : *
2724 : : * In reality we only need to defend against
2725 : : * infinity and NaN, so we need not get too crazy
2726 : : * about pattern matching here.
2727 : : */
2728 : 4887 : const char *s = PQgetvalue(res, tuple, field);
2729 : :
2730 [ + + ]: 4887 : if (strspn(s, "0123456789 +-eE.") == strlen(s))
2731 : 4882 : archputs(s, fout);
2732 : : else
2733 : 5 : archprintf(fout, "'%s'", s);
2734 : : }
2735 : 4887 : break;
2736 : :
2737 : 8 : case BITOID:
2738 : : case VARBITOID:
2739 : 8 : archprintf(fout, "B'%s'",
2740 : : PQgetvalue(res, tuple, field));
2741 : 8 : break;
2742 : :
2743 : 13 : case BOOLOID:
2744 [ + + ]: 13 : if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
2745 : 8 : archputs("true", fout);
2746 : : else
2747 : 5 : archputs("false", fout);
2748 : 13 : break;
2749 : :
2750 : 2042 : default:
2751 : : /* All other types are printed as string literals. */
2752 : 2042 : resetPQExpBuffer(q);
2753 : 2042 : appendStringLiteralAH(q,
2754 : : PQgetvalue(res, tuple, field),
2755 : : fout);
2756 : 2042 : archputs(q->data, fout);
2757 : 2042 : break;
2758 : : }
2759 : : }
2760 : :
2761 : : /* Terminate the row ... */
2762 : 3485 : archputs(")", fout);
2763 : :
2764 : : /* ... and the statement, if the target no. of rows is reached */
2765 [ + + ]: 3485 : if (++rows_this_statement >= rows_per_statement)
2766 : : {
2767 [ + + ]: 3469 : if (dopt->do_nothing)
2768 : 2 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2769 : : else
2770 : 3467 : archputs(";\n", fout);
2771 : : /* Reset the row counter */
2772 : 3469 : rows_this_statement = 0;
2773 : : }
2774 : : }
2775 : :
2776 [ + + ]: 168 : if (PQntuples(res) <= 0)
2777 : : {
2778 : 105 : PQclear(res);
2779 : 105 : break;
2780 : : }
2781 : 63 : PQclear(res);
2782 : : }
2783 : :
2784 : : /* Terminate any statements that didn't make the row count. */
2785 [ + + ]: 105 : if (rows_this_statement > 0)
2786 : : {
2787 [ + + ]: 6 : if (dopt->do_nothing)
2788 : 3 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2789 : : else
2790 : 3 : archputs(";\n", fout);
2791 : : }
2792 : :
2793 : 105 : archputs("\n\n", fout);
2794 : :
2795 : 105 : ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
2796 : :
2797 : 105 : destroyPQExpBuffer(q);
2798 [ + - ]: 105 : if (insertStmt != NULL)
2799 : 105 : destroyPQExpBuffer(insertStmt);
2800 : 105 : pg_free(attgenerated);
2801 : :
2802 : : /* Revert back the setting */
2803 [ - + ]: 105 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2804 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2805 : :
2806 : 105 : return 1;
2807 : : }
2808 : :
2809 : : /*
2810 : : * getRootTableInfo:
2811 : : * get the root TableInfo for the given partition table.
2812 : : */
2813 : : static TableInfo *
2814 : 102 : getRootTableInfo(const TableInfo *tbinfo)
2815 : : {
2816 : : TableInfo *parentTbinfo;
2817 : :
2818 : : Assert(tbinfo->ispartition);
2819 : : Assert(tbinfo->numParents == 1);
2820 : :
2821 : 102 : parentTbinfo = tbinfo->parents[0];
2822 [ - + ]: 102 : while (parentTbinfo->ispartition)
2823 : : {
2824 : : Assert(parentTbinfo->numParents == 1);
2825 : 0 : parentTbinfo = parentTbinfo->parents[0];
2826 : : }
2827 : :
2828 : 102 : return parentTbinfo;
2829 : : }
2830 : :
2831 : : /*
2832 : : * forcePartitionRootLoad
2833 : : * Check if we must force load_via_partition_root for this partition.
2834 : : *
2835 : : * This is required if any level of ancestral partitioned table has an
2836 : : * unsafe partitioning scheme.
2837 : : */
2838 : : static bool
2839 : 1125 : forcePartitionRootLoad(const TableInfo *tbinfo)
2840 : : {
2841 : : TableInfo *parentTbinfo;
2842 : :
2843 : : Assert(tbinfo->ispartition);
2844 : : Assert(tbinfo->numParents == 1);
2845 : :
2846 : 1125 : parentTbinfo = tbinfo->parents[0];
2847 [ + + ]: 1125 : if (parentTbinfo->unsafe_partitions)
2848 : 102 : return true;
2849 [ + + ]: 1243 : while (parentTbinfo->ispartition)
2850 : : {
2851 : : Assert(parentTbinfo->numParents == 1);
2852 : 220 : parentTbinfo = parentTbinfo->parents[0];
2853 [ - + ]: 220 : if (parentTbinfo->unsafe_partitions)
2854 : 0 : return true;
2855 : : }
2856 : :
2857 : 1023 : return false;
2858 : : }
2859 : :
2860 : : /*
2861 : : * dumpTableData -
2862 : : * dump the contents of a single table
2863 : : *
2864 : : * Actually, this just makes an ArchiveEntry for the table contents.
2865 : : */
2866 : : static void
2867 : 4638 : dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
2868 : : {
2869 : 4638 : DumpOptions *dopt = fout->dopt;
2870 : 4638 : const TableInfo *tbinfo = tdinfo->tdtable;
2871 : 4638 : PQExpBuffer copyBuf = createPQExpBuffer();
2872 : 4638 : PQExpBuffer clistBuf = createPQExpBuffer();
2873 : : DataDumperPtr dumpFn;
2874 : 4638 : char *tdDefn = NULL;
2875 : : char *copyStmt;
2876 : : const char *copyFrom;
2877 : :
2878 : : /* We had better have loaded per-column details about this table */
2879 : : Assert(tbinfo->interesting);
2880 : :
2881 : : /*
2882 : : * When load-via-partition-root is set or forced, get the root table name
2883 : : * for the partition table, so that we can reload data through the root
2884 : : * table. Then construct a comment to be inserted into the TOC entry's
2885 : : * defn field, so that such cases can be identified reliably.
2886 : : */
2887 [ + + ]: 4638 : if (tbinfo->ispartition &&
2888 [ + - + + ]: 2196 : (dopt->load_via_partition_root ||
2889 : 1098 : forcePartitionRootLoad(tbinfo)))
2890 : 92 : {
2891 : : const TableInfo *parentTbinfo;
2892 : : char *sanitized;
2893 : :
2894 : 92 : parentTbinfo = getRootTableInfo(tbinfo);
2895 : 92 : copyFrom = fmtQualifiedDumpable(parentTbinfo);
2896 : 92 : sanitized = sanitize_line(copyFrom, true);
2897 : 92 : printfPQExpBuffer(copyBuf, "-- load via partition root %s",
2898 : : sanitized);
2899 : 92 : free(sanitized);
2900 : 92 : tdDefn = pg_strdup(copyBuf->data);
2901 : : }
2902 : : else
2903 : 4546 : copyFrom = fmtQualifiedDumpable(tbinfo);
2904 : :
2905 [ + + ]: 4638 : if (dopt->dump_inserts == 0)
2906 : : {
2907 : : /* Dump/restore using COPY */
2908 : 4533 : dumpFn = dumpTableData_copy;
2909 : : /* must use 2 steps here 'cause fmtId is nonreentrant */
2910 : 4533 : printfPQExpBuffer(copyBuf, "COPY %s ",
2911 : : copyFrom);
2912 : 4533 : appendPQExpBuffer(copyBuf, "%s FROM stdin;\n",
2913 : : fmtCopyColumnList(tbinfo, clistBuf));
2914 : 4533 : copyStmt = copyBuf->data;
2915 : : }
2916 : : else
2917 : : {
2918 : : /* Restore using INSERT */
2919 : 105 : dumpFn = dumpTableData_insert;
2920 : 105 : copyStmt = NULL;
2921 : : }
2922 : :
2923 : : /*
2924 : : * Note: although the TableDataInfo is a full DumpableObject, we treat its
2925 : : * dependency on its table as "special" and pass it to ArchiveEntry now.
2926 : : * See comments for BuildArchiveDependencies.
2927 : : */
2928 [ + - ]: 4638 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2929 : : {
2930 : : TocEntry *te;
2931 : :
2932 : 4638 : te = ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
2933 : 4638 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
2934 : : .namespace = tbinfo->dobj.namespace->dobj.name,
2935 : : .owner = tbinfo->rolname,
2936 : : .description = "TABLE DATA",
2937 : : .section = SECTION_DATA,
2938 : : .createStmt = tdDefn,
2939 : : .copyStmt = copyStmt,
2940 : : .deps = &(tbinfo->dobj.dumpId),
2941 : : .nDeps = 1,
2942 : : .dumpFn = dumpFn,
2943 : : .dumpArg = tdinfo));
2944 : :
2945 : : /*
2946 : : * Set the TocEntry's dataLength in case we are doing a parallel dump
2947 : : * and want to order dump jobs by table size. We choose to measure
2948 : : * dataLength in table pages (including TOAST pages) during dump, so
2949 : : * no scaling is needed.
2950 : : *
2951 : : * However, relpages is declared as "integer" in pg_class, and hence
2952 : : * also in TableInfo, but it's really BlockNumber a/k/a unsigned int.
2953 : : * Cast so that we get the right interpretation of table sizes
2954 : : * exceeding INT_MAX pages.
2955 : : */
2956 : 4638 : te->dataLength = (BlockNumber) tbinfo->relpages;
2957 : 4638 : te->dataLength += (BlockNumber) tbinfo->toastpages;
2958 : :
2959 : : /*
2960 : : * If pgoff_t is only 32 bits wide, the above refinement is useless,
2961 : : * and instead we'd better worry about integer overflow. Clamp to
2962 : : * INT_MAX if the correct result exceeds that.
2963 : : */
2964 : : if (sizeof(te->dataLength) == 4 &&
2965 : : (tbinfo->relpages < 0 || tbinfo->toastpages < 0 ||
2966 : : te->dataLength < 0))
2967 : : te->dataLength = INT_MAX;
2968 : : }
2969 : :
2970 : 4638 : destroyPQExpBuffer(copyBuf);
2971 : 4638 : destroyPQExpBuffer(clistBuf);
2972 : 4638 : }
2973 : :
2974 : : /*
2975 : : * refreshMatViewData -
2976 : : * load or refresh the contents of a single materialized view
2977 : : *
2978 : : * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
2979 : : * statement.
2980 : : */
2981 : : static void
2982 : 363 : refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo)
2983 : : {
2984 : 363 : TableInfo *tbinfo = tdinfo->tdtable;
2985 : : PQExpBuffer q;
2986 : :
2987 : : /* If the materialized view is not flagged as populated, skip this. */
2988 [ + + ]: 363 : if (!tbinfo->relispopulated)
2989 : 72 : return;
2990 : :
2991 : 291 : q = createPQExpBuffer();
2992 : :
2993 : 291 : appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
2994 : 291 : fmtQualifiedDumpable(tbinfo));
2995 : :
2996 [ + - ]: 291 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2997 : 291 : ArchiveEntry(fout,
2998 : : tdinfo->dobj.catId, /* catalog ID */
2999 : 291 : tdinfo->dobj.dumpId, /* dump ID */
3000 : 291 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
3001 : : .namespace = tbinfo->dobj.namespace->dobj.name,
3002 : : .owner = tbinfo->rolname,
3003 : : .description = "MATERIALIZED VIEW DATA",
3004 : : .section = SECTION_POST_DATA,
3005 : : .createStmt = q->data,
3006 : : .deps = tdinfo->dobj.dependencies,
3007 : : .nDeps = tdinfo->dobj.nDeps));
3008 : :
3009 : 291 : destroyPQExpBuffer(q);
3010 : : }
3011 : :
3012 : : /*
3013 : : * getTableData -
3014 : : * set up dumpable objects representing the contents of tables
3015 : : */
3016 : : static void
3017 : 206 : getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
3018 : : {
3019 : : int i;
3020 : :
3021 [ + + ]: 55420 : for (i = 0; i < numTables; i++)
3022 : : {
3023 [ + + + + ]: 55214 : if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
3024 [ + + ]: 990 : (!relkind || tblinfo[i].relkind == relkind))
3025 : 6458 : makeTableDataInfo(dopt, &(tblinfo[i]));
3026 : : }
3027 : 206 : }
3028 : :
3029 : : /*
3030 : : * Make a dumpable object for the data of this specific table
3031 : : *
3032 : : * Note: we make a TableDataInfo if and only if we are going to dump the
3033 : : * table data; the "dump" field in such objects isn't very interesting.
3034 : : */
3035 : : static void
3036 : 6541 : makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo)
3037 : : {
3038 : : TableDataInfo *tdinfo;
3039 : :
3040 : : /*
3041 : : * Nothing to do if we already decided to dump the table. This will
3042 : : * happen for "config" tables.
3043 : : */
3044 [ + + ]: 6541 : if (tbinfo->dataObj != NULL)
3045 : 1 : return;
3046 : :
3047 : : /* Skip VIEWs (no data to dump) */
3048 [ + + ]: 6540 : if (tbinfo->relkind == RELKIND_VIEW)
3049 : 524 : return;
3050 : : /* Skip FOREIGN TABLEs (no data to dump) unless requested explicitly */
3051 [ + + ]: 6016 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
3052 [ + + ]: 40 : (foreign_servers_include_oids.head == NULL ||
3053 [ + + ]: 4 : !simple_oid_list_member(&foreign_servers_include_oids,
3054 : : tbinfo->foreign_server)))
3055 : 39 : return;
3056 : : /* Skip partitioned tables (data in partitions) */
3057 [ + + ]: 5977 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
3058 : 535 : return;
3059 : :
3060 : : /* Don't dump data in unlogged tables, if so requested */
3061 [ + + ]: 5442 : if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
3062 [ + + ]: 41 : dopt->no_unlogged_table_data)
3063 : 18 : return;
3064 : :
3065 : : /* Check that the data is not explicitly excluded */
3066 [ + + ]: 5424 : if (simple_oid_list_member(&tabledata_exclude_oids,
3067 : : tbinfo->dobj.catId.oid))
3068 : 8 : return;
3069 : :
3070 : : /* OK, let's dump it */
3071 : 5416 : tdinfo = pg_malloc_object(TableDataInfo);
3072 : :
3073 [ + + ]: 5416 : if (tbinfo->relkind == RELKIND_MATVIEW)
3074 : 363 : tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
3075 [ + + ]: 5053 : else if (tbinfo->relkind == RELKIND_SEQUENCE)
3076 : 415 : tdinfo->dobj.objType = DO_SEQUENCE_SET;
3077 : : else
3078 : 4638 : tdinfo->dobj.objType = DO_TABLE_DATA;
3079 : :
3080 : : /*
3081 : : * Note: use tableoid 0 so that this object won't be mistaken for
3082 : : * something that pg_depend entries apply to.
3083 : : */
3084 : 5416 : tdinfo->dobj.catId.tableoid = 0;
3085 : 5416 : tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
3086 : 5416 : AssignDumpId(&tdinfo->dobj);
3087 : 5416 : tdinfo->dobj.name = tbinfo->dobj.name;
3088 : 5416 : tdinfo->dobj.namespace = tbinfo->dobj.namespace;
3089 : 5416 : tdinfo->tdtable = tbinfo;
3090 : 5416 : tdinfo->filtercond = NULL; /* might get set later */
3091 : 5416 : addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
3092 : :
3093 : : /* A TableDataInfo contains data, of course */
3094 : 5416 : tdinfo->dobj.components |= DUMP_COMPONENT_DATA;
3095 : :
3096 : 5416 : tbinfo->dataObj = tdinfo;
3097 : :
3098 : : /*
3099 : : * Materialized view statistics must be restored after the data, because
3100 : : * REFRESH MATERIALIZED VIEW replaces the storage and resets the stats.
3101 : : *
3102 : : * The dependency is added here because the statistics objects are created
3103 : : * first.
3104 : : */
3105 [ + + + + ]: 5416 : if (tbinfo->relkind == RELKIND_MATVIEW && tbinfo->stats != NULL)
3106 : : {
3107 : 286 : tbinfo->stats->section = SECTION_POST_DATA;
3108 : 286 : addObjectDependency(&tbinfo->stats->dobj, tdinfo->dobj.dumpId);
3109 : : }
3110 : :
3111 : : /* Make sure that we'll collect per-column info for this table. */
3112 : 5416 : tbinfo->interesting = true;
3113 : : }
3114 : :
3115 : : /*
3116 : : * The refresh for a materialized view must be dependent on the refresh for
3117 : : * any materialized view that this one is dependent on.
3118 : : *
3119 : : * This must be called after all the objects are created, but before they are
3120 : : * sorted.
3121 : : */
3122 : : static void
3123 : 166 : buildMatViewRefreshDependencies(Archive *fout)
3124 : : {
3125 : : PQExpBuffer query;
3126 : : PGresult *res;
3127 : : int ntups,
3128 : : i;
3129 : : int i_classid,
3130 : : i_objid,
3131 : : i_refobjid;
3132 : :
3133 : 166 : query = createPQExpBuffer();
3134 : :
3135 : 166 : appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
3136 : : "( "
3137 : : "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
3138 : : "FROM pg_depend d1 "
3139 : : "JOIN pg_class c1 ON c1.oid = d1.objid "
3140 : : "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
3141 : : " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
3142 : : "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
3143 : : "AND d2.objid = r1.oid "
3144 : : "AND d2.refobjid <> d1.objid "
3145 : : "JOIN pg_class c2 ON c2.oid = d2.refobjid "
3146 : : "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3147 : : CppAsString2(RELKIND_VIEW) ") "
3148 : : "WHERE d1.classid = 'pg_class'::regclass "
3149 : : "UNION "
3150 : : "SELECT w.objid, d3.refobjid, c3.relkind "
3151 : : "FROM w "
3152 : : "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
3153 : : "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
3154 : : "AND d3.objid = r3.oid "
3155 : : "AND d3.refobjid <> w.refobjid "
3156 : : "JOIN pg_class c3 ON c3.oid = d3.refobjid "
3157 : : "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3158 : : CppAsString2(RELKIND_VIEW) ") "
3159 : : ") "
3160 : : "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
3161 : : "FROM w "
3162 : : "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
3163 : :
3164 : 166 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3165 : :
3166 : 166 : ntups = PQntuples(res);
3167 : :
3168 : 166 : i_classid = PQfnumber(res, "classid");
3169 : 166 : i_objid = PQfnumber(res, "objid");
3170 : 166 : i_refobjid = PQfnumber(res, "refobjid");
3171 : :
3172 [ + + ]: 442 : for (i = 0; i < ntups; i++)
3173 : : {
3174 : : CatalogId objId;
3175 : : CatalogId refobjId;
3176 : : DumpableObject *dobj;
3177 : : DumpableObject *refdobj;
3178 : : TableInfo *tbinfo;
3179 : : TableInfo *reftbinfo;
3180 : :
3181 : 276 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
3182 : 276 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
3183 : 276 : refobjId.tableoid = objId.tableoid;
3184 : 276 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
3185 : :
3186 : 276 : dobj = findObjectByCatalogId(objId);
3187 [ - + ]: 276 : if (dobj == NULL)
3188 : 48 : continue;
3189 : :
3190 : : Assert(dobj->objType == DO_TABLE);
3191 : 276 : tbinfo = (TableInfo *) dobj;
3192 : : Assert(tbinfo->relkind == RELKIND_MATVIEW);
3193 : 276 : dobj = (DumpableObject *) tbinfo->dataObj;
3194 [ + + ]: 276 : if (dobj == NULL)
3195 : 48 : continue;
3196 : : Assert(dobj->objType == DO_REFRESH_MATVIEW);
3197 : :
3198 : 228 : refdobj = findObjectByCatalogId(refobjId);
3199 [ - + ]: 228 : if (refdobj == NULL)
3200 : 0 : continue;
3201 : :
3202 : : Assert(refdobj->objType == DO_TABLE);
3203 : 228 : reftbinfo = (TableInfo *) refdobj;
3204 : : Assert(reftbinfo->relkind == RELKIND_MATVIEW);
3205 : 228 : refdobj = (DumpableObject *) reftbinfo->dataObj;
3206 [ - + ]: 228 : if (refdobj == NULL)
3207 : 0 : continue;
3208 : : Assert(refdobj->objType == DO_REFRESH_MATVIEW);
3209 : :
3210 : 228 : addObjectDependency(dobj, refdobj->dumpId);
3211 : :
3212 [ + + ]: 228 : if (!reftbinfo->relispopulated)
3213 : 36 : tbinfo->relispopulated = false;
3214 : : }
3215 : :
3216 : 166 : PQclear(res);
3217 : :
3218 : 166 : destroyPQExpBuffer(query);
3219 : 166 : }
3220 : :
3221 : : /*
3222 : : * getTableDataFKConstraints -
3223 : : * add dump-order dependencies reflecting foreign key constraints
3224 : : *
3225 : : * This code is executed only in a data-only dump --- in schema+data dumps
3226 : : * we handle foreign key issues by not creating the FK constraints until
3227 : : * after the data is loaded. In a data-only dump, however, we want to
3228 : : * order the table data objects in such a way that a table's referenced
3229 : : * tables are restored first. (In the presence of circular references or
3230 : : * self-references this may be impossible; we'll detect and complain about
3231 : : * that during the dependency sorting step.)
3232 : : */
3233 : : static void
3234 : 7 : getTableDataFKConstraints(void)
3235 : : {
3236 : : DumpableObject **dobjs;
3237 : : int numObjs;
3238 : : int i;
3239 : :
3240 : : /* Search through all the dumpable objects for FK constraints */
3241 : 7 : getDumpableObjects(&dobjs, &numObjs);
3242 [ + + ]: 26569 : for (i = 0; i < numObjs; i++)
3243 : : {
3244 [ + + ]: 26562 : if (dobjs[i]->objType == DO_FK_CONSTRAINT)
3245 : : {
3246 : 8 : ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
3247 : : TableInfo *ftable;
3248 : :
3249 : : /* Not interesting unless both tables are to be dumped */
3250 [ + - ]: 8 : if (cinfo->contable == NULL ||
3251 [ + + ]: 8 : cinfo->contable->dataObj == NULL)
3252 : 4 : continue;
3253 : 4 : ftable = findTableByOid(cinfo->confrelid);
3254 [ + - ]: 4 : if (ftable == NULL ||
3255 [ - + ]: 4 : ftable->dataObj == NULL)
3256 : 0 : continue;
3257 : :
3258 : : /*
3259 : : * Okay, make referencing table's TABLE_DATA object depend on the
3260 : : * referenced table's TABLE_DATA object.
3261 : : */
3262 : 4 : addObjectDependency(&cinfo->contable->dataObj->dobj,
3263 : 4 : ftable->dataObj->dobj.dumpId);
3264 : : }
3265 : : }
3266 : 7 : free(dobjs);
3267 : 7 : }
3268 : :
3269 : :
3270 : : /*
3271 : : * dumpDatabase:
3272 : : * dump the database definition
3273 : : */
3274 : : static void
3275 : 108 : dumpDatabase(Archive *fout)
3276 : : {
3277 : 108 : DumpOptions *dopt = fout->dopt;
3278 : 108 : PQExpBuffer dbQry = createPQExpBuffer();
3279 : 108 : PQExpBuffer delQry = createPQExpBuffer();
3280 : 108 : PQExpBuffer creaQry = createPQExpBuffer();
3281 : 108 : PQExpBuffer labelq = createPQExpBuffer();
3282 : 108 : PGconn *conn = GetConnection(fout);
3283 : : PGresult *res;
3284 : : int i_tableoid,
3285 : : i_oid,
3286 : : i_datname,
3287 : : i_datdba,
3288 : : i_encoding,
3289 : : i_datlocprovider,
3290 : : i_collate,
3291 : : i_ctype,
3292 : : i_datlocale,
3293 : : i_daticurules,
3294 : : i_frozenxid,
3295 : : i_minmxid,
3296 : : i_datacl,
3297 : : i_acldefault,
3298 : : i_datistemplate,
3299 : : i_datconnlimit,
3300 : : i_datcollversion,
3301 : : i_tablespace;
3302 : : CatalogId dbCatId;
3303 : : DumpId dbDumpId;
3304 : : DumpableAcl dbdacl;
3305 : : const char *datname,
3306 : : *dba,
3307 : : *encoding,
3308 : : *datlocprovider,
3309 : : *collate,
3310 : : *ctype,
3311 : : *locale,
3312 : : *icurules,
3313 : : *datistemplate,
3314 : : *datconnlimit,
3315 : : *tablespace;
3316 : : uint32 frozenxid,
3317 : : minmxid;
3318 : : char *qdatname;
3319 : :
3320 : 108 : pg_log_info("saving database definition");
3321 : :
3322 : : /*
3323 : : * Fetch the database-level properties for this database.
3324 : : */
3325 : 108 : appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, "
3326 : : "datdba, "
3327 : : "pg_encoding_to_char(encoding) AS encoding, "
3328 : : "datcollate, datctype, datfrozenxid, "
3329 : : "datacl, acldefault('d', datdba) AS acldefault, "
3330 : : "datistemplate, datconnlimit, ");
3331 : 108 : appendPQExpBufferStr(dbQry, "datminmxid, ");
3332 [ + - ]: 108 : if (fout->remoteVersion >= 170000)
3333 : 108 : appendPQExpBufferStr(dbQry, "datlocprovider, datlocale, datcollversion, ");
3334 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
3335 : 0 : appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale AS datlocale, datcollversion, ");
3336 : : else
3337 : 0 : appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS datlocale, NULL AS datcollversion, ");
3338 [ + - ]: 108 : if (fout->remoteVersion >= 160000)
3339 : 108 : appendPQExpBufferStr(dbQry, "daticurules, ");
3340 : : else
3341 : 0 : appendPQExpBufferStr(dbQry, "NULL AS daticurules, ");
3342 : 108 : appendPQExpBufferStr(dbQry,
3343 : : "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
3344 : : "shobj_description(oid, 'pg_database') AS description "
3345 : : "FROM pg_database "
3346 : : "WHERE datname = current_database()");
3347 : :
3348 : 108 : res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
3349 : :
3350 : 108 : i_tableoid = PQfnumber(res, "tableoid");
3351 : 108 : i_oid = PQfnumber(res, "oid");
3352 : 108 : i_datname = PQfnumber(res, "datname");
3353 : 108 : i_datdba = PQfnumber(res, "datdba");
3354 : 108 : i_encoding = PQfnumber(res, "encoding");
3355 : 108 : i_datlocprovider = PQfnumber(res, "datlocprovider");
3356 : 108 : i_collate = PQfnumber(res, "datcollate");
3357 : 108 : i_ctype = PQfnumber(res, "datctype");
3358 : 108 : i_datlocale = PQfnumber(res, "datlocale");
3359 : 108 : i_daticurules = PQfnumber(res, "daticurules");
3360 : 108 : i_frozenxid = PQfnumber(res, "datfrozenxid");
3361 : 108 : i_minmxid = PQfnumber(res, "datminmxid");
3362 : 108 : i_datacl = PQfnumber(res, "datacl");
3363 : 108 : i_acldefault = PQfnumber(res, "acldefault");
3364 : 108 : i_datistemplate = PQfnumber(res, "datistemplate");
3365 : 108 : i_datconnlimit = PQfnumber(res, "datconnlimit");
3366 : 108 : i_datcollversion = PQfnumber(res, "datcollversion");
3367 : 108 : i_tablespace = PQfnumber(res, "tablespace");
3368 : :
3369 : 108 : dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
3370 : 108 : dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
3371 : 108 : datname = PQgetvalue(res, 0, i_datname);
3372 : 108 : dba = getRoleName(PQgetvalue(res, 0, i_datdba));
3373 : 108 : encoding = PQgetvalue(res, 0, i_encoding);
3374 : 108 : datlocprovider = PQgetvalue(res, 0, i_datlocprovider);
3375 : 108 : collate = PQgetvalue(res, 0, i_collate);
3376 : 108 : ctype = PQgetvalue(res, 0, i_ctype);
3377 [ + + ]: 108 : if (!PQgetisnull(res, 0, i_datlocale))
3378 : 14 : locale = PQgetvalue(res, 0, i_datlocale);
3379 : : else
3380 : 94 : locale = NULL;
3381 [ - + ]: 108 : if (!PQgetisnull(res, 0, i_daticurules))
3382 : 0 : icurules = PQgetvalue(res, 0, i_daticurules);
3383 : : else
3384 : 108 : icurules = NULL;
3385 : 108 : frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
3386 : 108 : minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
3387 : 108 : dbdacl.acl = PQgetvalue(res, 0, i_datacl);
3388 : 108 : dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault);
3389 : 108 : datistemplate = PQgetvalue(res, 0, i_datistemplate);
3390 : 108 : datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
3391 : 108 : tablespace = PQgetvalue(res, 0, i_tablespace);
3392 : :
3393 : 108 : qdatname = pg_strdup(fmtId(datname));
3394 : :
3395 : : /*
3396 : : * Prepare the CREATE DATABASE command. We must specify OID (if we want
3397 : : * to preserve that), as well as the encoding, locale, and tablespace
3398 : : * since those can't be altered later. Other DB properties are left to
3399 : : * the DATABASE PROPERTIES entry, so that they can be applied after
3400 : : * reconnecting to the target DB.
3401 : : *
3402 : : * For binary upgrade, we use the FILE_COPY strategy because testing has
3403 : : * shown it to be faster. When the server is in binary upgrade mode, it
3404 : : * will also skip the checkpoints this strategy ordinarily performs.
3405 : : */
3406 [ + + ]: 108 : if (dopt->binary_upgrade)
3407 : : {
3408 : 43 : appendPQExpBuffer(creaQry,
3409 : : "CREATE DATABASE %s WITH TEMPLATE = template0 "
3410 : : "OID = %u STRATEGY = FILE_COPY",
3411 : : qdatname, dbCatId.oid);
3412 : : }
3413 : : else
3414 : : {
3415 : 65 : appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
3416 : : qdatname);
3417 : : }
3418 [ + - ]: 108 : if (strlen(encoding) > 0)
3419 : : {
3420 : 108 : appendPQExpBufferStr(creaQry, " ENCODING = ");
3421 : 108 : appendStringLiteralAH(creaQry, encoding, fout);
3422 : : }
3423 : :
3424 : 108 : appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = ");
3425 [ + + ]: 108 : if (datlocprovider[0] == 'b')
3426 : 14 : appendPQExpBufferStr(creaQry, "builtin");
3427 [ + - ]: 94 : else if (datlocprovider[0] == 'c')
3428 : 94 : appendPQExpBufferStr(creaQry, "libc");
3429 [ # # ]: 0 : else if (datlocprovider[0] == 'i')
3430 : 0 : appendPQExpBufferStr(creaQry, "icu");
3431 : : else
3432 : 0 : pg_fatal("unrecognized locale provider: %s",
3433 : : datlocprovider);
3434 : :
3435 [ + - + - ]: 108 : if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
3436 : : {
3437 : 108 : appendPQExpBufferStr(creaQry, " LOCALE = ");
3438 : 108 : appendStringLiteralAH(creaQry, collate, fout);
3439 : : }
3440 : : else
3441 : : {
3442 [ # # ]: 0 : if (strlen(collate) > 0)
3443 : : {
3444 : 0 : appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
3445 : 0 : appendStringLiteralAH(creaQry, collate, fout);
3446 : : }
3447 [ # # ]: 0 : if (strlen(ctype) > 0)
3448 : : {
3449 : 0 : appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
3450 : 0 : appendStringLiteralAH(creaQry, ctype, fout);
3451 : : }
3452 : : }
3453 [ + + ]: 108 : if (locale)
3454 : : {
3455 [ + - ]: 14 : if (datlocprovider[0] == 'b')
3456 : 14 : appendPQExpBufferStr(creaQry, " BUILTIN_LOCALE = ");
3457 : : else
3458 : 0 : appendPQExpBufferStr(creaQry, " ICU_LOCALE = ");
3459 : :
3460 : 14 : appendStringLiteralAH(creaQry, locale, fout);
3461 : : }
3462 : :
3463 [ - + ]: 108 : if (icurules)
3464 : : {
3465 : 0 : appendPQExpBufferStr(creaQry, " ICU_RULES = ");
3466 : 0 : appendStringLiteralAH(creaQry, icurules, fout);
3467 : : }
3468 : :
3469 : : /*
3470 : : * For binary upgrade, carry over the collation version. For normal
3471 : : * dump/restore, omit the version, so that it is computed upon restore.
3472 : : */
3473 [ + + ]: 108 : if (dopt->binary_upgrade)
3474 : : {
3475 [ + - ]: 43 : if (!PQgetisnull(res, 0, i_datcollversion))
3476 : : {
3477 : 43 : appendPQExpBufferStr(creaQry, " COLLATION_VERSION = ");
3478 : 43 : appendStringLiteralAH(creaQry,
3479 : : PQgetvalue(res, 0, i_datcollversion),
3480 : : fout);
3481 : : }
3482 : : }
3483 : :
3484 : : /*
3485 : : * Note: looking at dopt->outputNoTablespaces here is completely the wrong
3486 : : * thing; the decision whether to specify a tablespace should be left till
3487 : : * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
3488 : : * label the DATABASE entry with the tablespace and let the normal
3489 : : * tablespace selection logic work ... but CREATE DATABASE doesn't pay
3490 : : * attention to default_tablespace, so that won't work.
3491 : : */
3492 [ + - + + ]: 108 : if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
3493 [ + - ]: 5 : !dopt->outputNoTablespaces)
3494 : 5 : appendPQExpBuffer(creaQry, " TABLESPACE = %s",
3495 : : fmtId(tablespace));
3496 : 108 : appendPQExpBufferStr(creaQry, ";\n");
3497 : :
3498 : 108 : appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
3499 : : qdatname);
3500 : :
3501 : 108 : dbDumpId = createDumpId();
3502 : :
3503 : 108 : ArchiveEntry(fout,
3504 : : dbCatId, /* catalog ID */
3505 : : dbDumpId, /* dump ID */
3506 : 108 : ARCHIVE_OPTS(.tag = datname,
3507 : : .owner = dba,
3508 : : .description = "DATABASE",
3509 : : .section = SECTION_PRE_DATA,
3510 : : .createStmt = creaQry->data,
3511 : : .dropStmt = delQry->data));
3512 : :
3513 : : /* Compute correct tag for archive entry */
3514 : 108 : appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
3515 : :
3516 : : /* Dump DB comment if any */
3517 : : {
3518 : : /*
3519 : : * 8.2 and up keep comments on shared objects in a shared table, so we
3520 : : * cannot use the dumpComment() code used for other database objects.
3521 : : * Be careful that the ArchiveEntry parameters match that function.
3522 : : */
3523 : 108 : char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
3524 : :
3525 [ + - + + : 108 : if (comment && *comment && !dopt->no_comments)
+ - ]
3526 : : {
3527 : 55 : resetPQExpBuffer(dbQry);
3528 : :
3529 : : /*
3530 : : * Generates warning when loaded into a differently-named
3531 : : * database.
3532 : : */
3533 : 55 : appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
3534 : 55 : appendStringLiteralAH(dbQry, comment, fout);
3535 : 55 : appendPQExpBufferStr(dbQry, ";\n");
3536 : :
3537 : 55 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3538 : 55 : ARCHIVE_OPTS(.tag = labelq->data,
3539 : : .owner = dba,
3540 : : .description = "COMMENT",
3541 : : .section = SECTION_NONE,
3542 : : .createStmt = dbQry->data,
3543 : : .deps = &dbDumpId,
3544 : : .nDeps = 1));
3545 : : }
3546 : : }
3547 : :
3548 : : /* Dump DB security label, if enabled */
3549 [ + - ]: 108 : if (!dopt->no_security_labels)
3550 : : {
3551 : : PGresult *shres;
3552 : : PQExpBuffer seclabelQry;
3553 : :
3554 : 108 : seclabelQry = createPQExpBuffer();
3555 : :
3556 : 108 : buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
3557 : 108 : shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
3558 : 108 : resetPQExpBuffer(seclabelQry);
3559 : 108 : emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
3560 [ - + ]: 108 : if (seclabelQry->len > 0)
3561 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3562 : 0 : ARCHIVE_OPTS(.tag = labelq->data,
3563 : : .owner = dba,
3564 : : .description = "SECURITY LABEL",
3565 : : .section = SECTION_NONE,
3566 : : .createStmt = seclabelQry->data,
3567 : : .deps = &dbDumpId,
3568 : : .nDeps = 1));
3569 : 108 : destroyPQExpBuffer(seclabelQry);
3570 : 108 : PQclear(shres);
3571 : : }
3572 : :
3573 : : /*
3574 : : * Dump ACL if any. Note that we do not support initial privileges
3575 : : * (pg_init_privs) on databases.
3576 : : */
3577 : 108 : dbdacl.privtype = 0;
3578 : 108 : dbdacl.initprivs = NULL;
3579 : :
3580 : 108 : dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE",
3581 : : qdatname, NULL, NULL,
3582 : : NULL, dba, &dbdacl);
3583 : :
3584 : : /*
3585 : : * Now construct a DATABASE PROPERTIES archive entry to restore any
3586 : : * non-default database-level properties. (The reason this must be
3587 : : * separate is that we cannot put any additional commands into the TOC
3588 : : * entry that has CREATE DATABASE. pg_restore would execute such a group
3589 : : * in an implicit transaction block, and the backend won't allow CREATE
3590 : : * DATABASE in that context.)
3591 : : */
3592 : 108 : resetPQExpBuffer(creaQry);
3593 : 108 : resetPQExpBuffer(delQry);
3594 : :
3595 [ + - - + ]: 108 : if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
3596 : 0 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
3597 : : qdatname, datconnlimit);
3598 : :
3599 [ + + ]: 108 : if (strcmp(datistemplate, "t") == 0)
3600 : : {
3601 : 16 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
3602 : : qdatname);
3603 : :
3604 : : /*
3605 : : * The backend won't accept DROP DATABASE on a template database. We
3606 : : * can deal with that by removing the template marking before the DROP
3607 : : * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
3608 : : * since no such command is currently supported, fake it with a direct
3609 : : * UPDATE on pg_database.
3610 : : */
3611 : 16 : appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
3612 : : "SET datistemplate = false WHERE datname = ");
3613 : 16 : appendStringLiteralAH(delQry, datname, fout);
3614 : 16 : appendPQExpBufferStr(delQry, ";\n");
3615 : : }
3616 : :
3617 : : /*
3618 : : * We do not restore pg_database.dathasloginevt because it is set
3619 : : * automatically on login event trigger creation.
3620 : : */
3621 : :
3622 : : /* Add database-specific SET options */
3623 : 108 : dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
3624 : :
3625 : : /*
3626 : : * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
3627 : : * entry, too, for lack of a better place.
3628 : : */
3629 [ + + ]: 108 : if (dopt->binary_upgrade)
3630 : : {
3631 : 43 : appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
3632 : 43 : appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
3633 : : "SET datfrozenxid = '%u', datminmxid = '%u'\n"
3634 : : "WHERE datname = ",
3635 : : frozenxid, minmxid);
3636 : 43 : appendStringLiteralAH(creaQry, datname, fout);
3637 : 43 : appendPQExpBufferStr(creaQry, ";\n");
3638 : : }
3639 : :
3640 [ + + ]: 108 : if (creaQry->len > 0)
3641 : 49 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3642 : 49 : ARCHIVE_OPTS(.tag = datname,
3643 : : .owner = dba,
3644 : : .description = "DATABASE PROPERTIES",
3645 : : .section = SECTION_PRE_DATA,
3646 : : .createStmt = creaQry->data,
3647 : : .dropStmt = delQry->data,
3648 : : .deps = &dbDumpId));
3649 : :
3650 : : /*
3651 : : * pg_largeobject comes from the old system intact, so set its
3652 : : * relfrozenxids, relminmxids and relfilenode.
3653 : : *
3654 : : * pg_largeobject_metadata also comes from the old system intact for
3655 : : * upgrades from v16 and newer, so set its relfrozenxids, relminmxids, and
3656 : : * relfilenode, too. pg_upgrade can't copy/link the files from older
3657 : : * versions because aclitem (needed by pg_largeobject_metadata.lomacl)
3658 : : * changed its storage format in v16.
3659 : : */
3660 [ + + ]: 108 : if (dopt->binary_upgrade)
3661 : : {
3662 : : PGresult *lo_res;
3663 : 43 : PQExpBuffer loFrozenQry = createPQExpBuffer();
3664 : 43 : PQExpBuffer loOutQry = createPQExpBuffer();
3665 : 43 : PQExpBuffer lomOutQry = createPQExpBuffer();
3666 : 43 : PQExpBuffer loHorizonQry = createPQExpBuffer();
3667 : 43 : PQExpBuffer lomHorizonQry = createPQExpBuffer();
3668 : : int ii_relfrozenxid,
3669 : : ii_relfilenode,
3670 : : ii_oid,
3671 : : ii_relminmxid;
3672 : :
3673 : 43 : appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n"
3674 : : "FROM pg_catalog.pg_class\n"
3675 : : "WHERE oid IN (%u, %u, %u, %u);\n",
3676 : : LargeObjectRelationId, LargeObjectLOidPNIndexId,
3677 : : LargeObjectMetadataRelationId, LargeObjectMetadataOidIndexId);
3678 : :
3679 : 43 : lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
3680 : :
3681 : 43 : ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
3682 : 43 : ii_relminmxid = PQfnumber(lo_res, "relminmxid");
3683 : 43 : ii_relfilenode = PQfnumber(lo_res, "relfilenode");
3684 : 43 : ii_oid = PQfnumber(lo_res, "oid");
3685 : :
3686 : 43 : appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
3687 : 43 : appendPQExpBufferStr(lomHorizonQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n");
3688 : 43 : appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n");
3689 : 43 : appendPQExpBufferStr(lomOutQry, "\n-- For binary upgrade, preserve pg_largeobject_metadata and index relfilenodes\n");
3690 [ + + ]: 215 : for (int i = 0; i < PQntuples(lo_res); ++i)
3691 : : {
3692 : : Oid oid;
3693 : : RelFileNumber relfilenumber;
3694 : : PQExpBuffer horizonQry;
3695 : : PQExpBuffer outQry;
3696 : :
3697 : 172 : oid = atooid(PQgetvalue(lo_res, i, ii_oid));
3698 : 172 : relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode));
3699 : :
3700 [ + + + + ]: 172 : if (oid == LargeObjectRelationId ||
3701 : : oid == LargeObjectLOidPNIndexId)
3702 : : {
3703 : 86 : horizonQry = loHorizonQry;
3704 : 86 : outQry = loOutQry;
3705 : : }
3706 : : else
3707 : : {
3708 : 86 : horizonQry = lomHorizonQry;
3709 : 86 : outQry = lomOutQry;
3710 : : }
3711 : :
3712 : 172 : appendPQExpBuffer(horizonQry, "UPDATE pg_catalog.pg_class\n"
3713 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
3714 : : "WHERE oid = %u;\n",
3715 : 172 : atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)),
3716 : 172 : atooid(PQgetvalue(lo_res, i, ii_relminmxid)),
3717 : 172 : atooid(PQgetvalue(lo_res, i, ii_oid)));
3718 : :
3719 [ + + + + ]: 172 : if (oid == LargeObjectRelationId ||
3720 : : oid == LargeObjectMetadataRelationId)
3721 : 86 : appendPQExpBuffer(outQry,
3722 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
3723 : : relfilenumber);
3724 [ + + + - ]: 86 : else if (oid == LargeObjectLOidPNIndexId ||
3725 : : oid == LargeObjectMetadataOidIndexId)
3726 : 86 : appendPQExpBuffer(outQry,
3727 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
3728 : : relfilenumber);
3729 : : }
3730 : :
3731 : 43 : appendPQExpBufferStr(loOutQry,
3732 : : "TRUNCATE pg_catalog.pg_largeobject;\n");
3733 : 43 : appendPQExpBufferStr(lomOutQry,
3734 : : "TRUNCATE pg_catalog.pg_largeobject_metadata;\n");
3735 : :
3736 : 43 : appendPQExpBufferStr(loOutQry, loHorizonQry->data);
3737 : 43 : appendPQExpBufferStr(lomOutQry, lomHorizonQry->data);
3738 : :
3739 : 43 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3740 : 43 : ARCHIVE_OPTS(.tag = "pg_largeobject",
3741 : : .description = "pg_largeobject",
3742 : : .section = SECTION_PRE_DATA,
3743 : : .createStmt = loOutQry->data));
3744 : :
3745 [ + - ]: 43 : if (fout->remoteVersion >= 160000)
3746 : 43 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3747 : 43 : ARCHIVE_OPTS(.tag = "pg_largeobject_metadata",
3748 : : .description = "pg_largeobject_metadata",
3749 : : .section = SECTION_PRE_DATA,
3750 : : .createStmt = lomOutQry->data));
3751 : :
3752 : 43 : PQclear(lo_res);
3753 : :
3754 : 43 : destroyPQExpBuffer(loFrozenQry);
3755 : 43 : destroyPQExpBuffer(loHorizonQry);
3756 : 43 : destroyPQExpBuffer(lomHorizonQry);
3757 : 43 : destroyPQExpBuffer(loOutQry);
3758 : 43 : destroyPQExpBuffer(lomOutQry);
3759 : : }
3760 : :
3761 : 108 : PQclear(res);
3762 : :
3763 : 108 : pg_free(qdatname);
3764 : 108 : destroyPQExpBuffer(dbQry);
3765 : 108 : destroyPQExpBuffer(delQry);
3766 : 108 : destroyPQExpBuffer(creaQry);
3767 : 108 : destroyPQExpBuffer(labelq);
3768 : 108 : }
3769 : :
3770 : : /*
3771 : : * Collect any database-specific or role-and-database-specific SET options
3772 : : * for this database, and append them to outbuf.
3773 : : */
3774 : : static void
3775 : 108 : dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
3776 : : const char *dbname, Oid dboid)
3777 : : {
3778 : 108 : PGconn *conn = GetConnection(AH);
3779 : 108 : PQExpBuffer buf = createPQExpBuffer();
3780 : : PGresult *res;
3781 : :
3782 : : /* First collect database-specific options */
3783 : 108 : printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3784 : : "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3785 : : dboid);
3786 : :
3787 : 108 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3788 : :
3789 [ + + ]: 138 : for (int i = 0; i < PQntuples(res); i++)
3790 : 30 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 0),
3791 : : "DATABASE", dbname, NULL, NULL,
3792 : : outbuf);
3793 : :
3794 : 108 : PQclear(res);
3795 : :
3796 : : /* Now look for role-and-database-specific options */
3797 : 108 : printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3798 : : "FROM pg_db_role_setting s, pg_roles r "
3799 : : "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3800 : : dboid);
3801 : :
3802 : 108 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3803 : :
3804 [ - + ]: 108 : for (int i = 0; i < PQntuples(res); i++)
3805 : 0 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 1),
3806 : 0 : "ROLE", PQgetvalue(res, i, 0),
3807 : : "DATABASE", dbname,
3808 : : outbuf);
3809 : :
3810 : 108 : PQclear(res);
3811 : :
3812 : 108 : destroyPQExpBuffer(buf);
3813 : 108 : }
3814 : :
3815 : : /*
3816 : : * dumpEncoding: put the correct encoding into the archive
3817 : : */
3818 : : static void
3819 : 215 : dumpEncoding(Archive *AH)
3820 : : {
3821 : 215 : const char *encname = pg_encoding_to_char(AH->encoding);
3822 : 215 : PQExpBuffer qry = createPQExpBuffer();
3823 : :
3824 : 215 : pg_log_info("saving encoding = %s", encname);
3825 : :
3826 : 215 : appendPQExpBufferStr(qry, "SET client_encoding = ");
3827 : 215 : appendStringLiteralAH(qry, encname, AH);
3828 : 215 : appendPQExpBufferStr(qry, ";\n");
3829 : :
3830 : 215 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3831 : 215 : ARCHIVE_OPTS(.tag = "ENCODING",
3832 : : .description = "ENCODING",
3833 : : .section = SECTION_PRE_DATA,
3834 : : .createStmt = qry->data));
3835 : :
3836 : 215 : destroyPQExpBuffer(qry);
3837 : 215 : }
3838 : :
3839 : :
3840 : : /*
3841 : : * dumpStdStrings: put the correct escape string behavior into the archive
3842 : : */
3843 : : static void
3844 : 215 : dumpStdStrings(Archive *AH)
3845 : : {
3846 [ + - ]: 215 : const char *stdstrings = AH->std_strings ? "on" : "off";
3847 : 215 : PQExpBuffer qry = createPQExpBuffer();
3848 : :
3849 : 215 : pg_log_info("saving \"standard_conforming_strings = %s\"",
3850 : : stdstrings);
3851 : :
3852 : 215 : appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
3853 : : stdstrings);
3854 : :
3855 : 215 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3856 : 215 : ARCHIVE_OPTS(.tag = "STDSTRINGS",
3857 : : .description = "STDSTRINGS",
3858 : : .section = SECTION_PRE_DATA,
3859 : : .createStmt = qry->data));
3860 : :
3861 : 215 : destroyPQExpBuffer(qry);
3862 : 215 : }
3863 : :
3864 : : /*
3865 : : * dumpSearchPath: record the active search_path in the archive
3866 : : */
3867 : : static void
3868 : 215 : dumpSearchPath(Archive *AH)
3869 : : {
3870 : 215 : PQExpBuffer qry = createPQExpBuffer();
3871 : 215 : PQExpBuffer path = createPQExpBuffer();
3872 : : PGresult *res;
3873 : 215 : char **schemanames = NULL;
3874 : 215 : int nschemanames = 0;
3875 : : int i;
3876 : :
3877 : : /*
3878 : : * We use the result of current_schemas(), not the search_path GUC,
3879 : : * because that might contain wildcards such as "$user", which won't
3880 : : * necessarily have the same value during restore. Also, this way avoids
3881 : : * listing schemas that may appear in search_path but not actually exist,
3882 : : * which seems like a prudent exclusion.
3883 : : */
3884 : 215 : res = ExecuteSqlQueryForSingleRow(AH,
3885 : : "SELECT pg_catalog.current_schemas(false)");
3886 : :
3887 [ - + ]: 215 : if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames))
3888 : 0 : pg_fatal("could not parse result of current_schemas()");
3889 : :
3890 : : /*
3891 : : * We use set_config(), not a simple "SET search_path" command, because
3892 : : * the latter has less-clean behavior if the search path is empty. While
3893 : : * that's likely to get fixed at some point, it seems like a good idea to
3894 : : * be as backwards-compatible as possible in what we put into archives.
3895 : : */
3896 [ - + ]: 215 : for (i = 0; i < nschemanames; i++)
3897 : : {
3898 [ # # ]: 0 : if (i > 0)
3899 : 0 : appendPQExpBufferStr(path, ", ");
3900 : 0 : appendPQExpBufferStr(path, fmtId(schemanames[i]));
3901 : : }
3902 : :
3903 : 215 : appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', ");
3904 : 215 : appendStringLiteralAH(qry, path->data, AH);
3905 : 215 : appendPQExpBufferStr(qry, ", false);\n");
3906 : :
3907 : 215 : pg_log_info("saving \"search_path = %s\"", path->data);
3908 : :
3909 : 215 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3910 : 215 : ARCHIVE_OPTS(.tag = "SEARCHPATH",
3911 : : .description = "SEARCHPATH",
3912 : : .section = SECTION_PRE_DATA,
3913 : : .createStmt = qry->data));
3914 : :
3915 : : /* Also save it in AH->searchpath, in case we're doing plain text dump */
3916 : 215 : AH->searchpath = pg_strdup(qry->data);
3917 : :
3918 : 215 : free(schemanames);
3919 : 215 : PQclear(res);
3920 : 215 : destroyPQExpBuffer(qry);
3921 : 215 : destroyPQExpBuffer(path);
3922 : 215 : }
3923 : :
3924 : :
3925 : : /*
3926 : : * getLOs:
3927 : : * Collect schema-level data about large objects
3928 : : */
3929 : : static void
3930 : 186 : getLOs(Archive *fout)
3931 : : {
3932 : 186 : DumpOptions *dopt = fout->dopt;
3933 : 186 : PQExpBuffer loQry = createPQExpBuffer();
3934 : : PGresult *res;
3935 : : int ntups;
3936 : : int i;
3937 : : int n;
3938 : : int i_oid;
3939 : : int i_lomowner;
3940 : : int i_lomacl;
3941 : : int i_acldefault;
3942 : :
3943 : 186 : pg_log_info("reading large objects");
3944 : :
3945 : : /*
3946 : : * Fetch LO OIDs and owner/ACL data. Order the data so that all the blobs
3947 : : * with the same owner/ACL appear together.
3948 : : */
3949 : 186 : appendPQExpBufferStr(loQry,
3950 : : "SELECT oid, lomowner, lomacl, "
3951 : : "acldefault('L', lomowner) AS acldefault "
3952 : : "FROM pg_largeobject_metadata ");
3953 : :
3954 : : /*
3955 : : * For binary upgrades, we transfer pg_largeobject_metadata via COPY or by
3956 : : * copying/linking its files from the old cluster. On such upgrades, we
3957 : : * only need to consider large objects that have comments or security
3958 : : * labels, since we still restore those objects via COMMENT/SECURITY LABEL
3959 : : * commands.
3960 : : */
3961 [ + + ]: 186 : if (dopt->binary_upgrade)
3962 : 44 : appendPQExpBufferStr(loQry,
3963 : : "WHERE oid IN "
3964 : : "(SELECT objoid FROM pg_description "
3965 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) " "
3966 : : "UNION SELECT objoid FROM pg_seclabel "
3967 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) ") ");
3968 : :
3969 : 186 : appendPQExpBufferStr(loQry,
3970 : : "ORDER BY lomowner, lomacl::pg_catalog.text, oid");
3971 : :
3972 : 186 : res = ExecuteSqlQuery(fout, loQry->data, PGRES_TUPLES_OK);
3973 : :
3974 : 186 : i_oid = PQfnumber(res, "oid");
3975 : 186 : i_lomowner = PQfnumber(res, "lomowner");
3976 : 186 : i_lomacl = PQfnumber(res, "lomacl");
3977 : 186 : i_acldefault = PQfnumber(res, "acldefault");
3978 : :
3979 : 186 : ntups = PQntuples(res);
3980 : :
3981 : : /*
3982 : : * Group the blobs into suitably-sized groups that have the same owner and
3983 : : * ACL setting, and build a metadata and a data DumpableObject for each
3984 : : * group. (If we supported initprivs for blobs, we'd have to insist that
3985 : : * groups also share initprivs settings, since the DumpableObject only has
3986 : : * room for one.) i is the index of the first tuple in the current group,
3987 : : * and n is the number of tuples we include in the group.
3988 : : */
3989 [ + + ]: 274 : for (i = 0; i < ntups; i += n)
3990 : : {
3991 : 88 : Oid thisoid = atooid(PQgetvalue(res, i, i_oid));
3992 : 88 : char *thisowner = PQgetvalue(res, i, i_lomowner);
3993 : 88 : char *thisacl = PQgetvalue(res, i, i_lomacl);
3994 : : LoInfo *loinfo;
3995 : : DumpableObject *lodata;
3996 : : char namebuf[64];
3997 : :
3998 : : /* Scan to find first tuple not to be included in group */
3999 : 88 : n = 1;
4000 [ + - + + ]: 102 : while (n < MAX_BLOBS_PER_ARCHIVE_ENTRY && i + n < ntups)
4001 : : {
4002 [ + - ]: 49 : if (strcmp(thisowner, PQgetvalue(res, i + n, i_lomowner)) != 0 ||
4003 [ + + ]: 49 : strcmp(thisacl, PQgetvalue(res, i + n, i_lomacl)) != 0)
4004 : : break;
4005 : 14 : n++;
4006 : : }
4007 : :
4008 : : /* Build the metadata DumpableObject */
4009 : 88 : loinfo = (LoInfo *) pg_malloc(offsetof(LoInfo, looids) + n * sizeof(Oid));
4010 : :
4011 : 88 : loinfo->dobj.objType = DO_LARGE_OBJECT;
4012 : 88 : loinfo->dobj.catId.tableoid = LargeObjectRelationId;
4013 : 88 : loinfo->dobj.catId.oid = thisoid;
4014 : 88 : AssignDumpId(&loinfo->dobj);
4015 : :
4016 [ + + ]: 88 : if (n > 1)
4017 : 10 : snprintf(namebuf, sizeof(namebuf), "%u..%u", thisoid,
4018 : 10 : atooid(PQgetvalue(res, i + n - 1, i_oid)));
4019 : : else
4020 : 78 : snprintf(namebuf, sizeof(namebuf), "%u", thisoid);
4021 : 88 : loinfo->dobj.name = pg_strdup(namebuf);
4022 : 88 : loinfo->dacl.acl = pg_strdup(thisacl);
4023 : 88 : loinfo->dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
4024 : 88 : loinfo->dacl.privtype = 0;
4025 : 88 : loinfo->dacl.initprivs = NULL;
4026 : 88 : loinfo->rolname = getRoleName(thisowner);
4027 : 88 : loinfo->numlos = n;
4028 : 88 : loinfo->looids[0] = thisoid;
4029 : : /* Collect OIDs of the remaining blobs in this group */
4030 [ + + ]: 102 : for (int k = 1; k < n; k++)
4031 : : {
4032 : : CatalogId extraID;
4033 : :
4034 : 14 : loinfo->looids[k] = atooid(PQgetvalue(res, i + k, i_oid));
4035 : :
4036 : : /* Make sure we can look up loinfo by any of the blobs' OIDs */
4037 : 14 : extraID.tableoid = LargeObjectRelationId;
4038 : 14 : extraID.oid = loinfo->looids[k];
4039 : 14 : recordAdditionalCatalogID(extraID, &loinfo->dobj);
4040 : : }
4041 : :
4042 : : /* LOs have data */
4043 : 88 : loinfo->dobj.components |= DUMP_COMPONENT_DATA;
4044 : :
4045 : : /* Mark whether LO group has a non-empty ACL */
4046 [ + + ]: 88 : if (!PQgetisnull(res, i, i_lomacl))
4047 : 36 : loinfo->dobj.components |= DUMP_COMPONENT_ACL;
4048 : :
4049 : : /*
4050 : : * In binary upgrade mode, pg_largeobject and pg_largeobject_metadata
4051 : : * are transferred via COPY or by copying/linking the files from the
4052 : : * old cluster. Thus, we do not need to dump LO data, definitions, or
4053 : : * ACLs.
4054 : : */
4055 [ + + ]: 88 : if (dopt->binary_upgrade)
4056 : 7 : loinfo->dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL | DUMP_COMPONENT_DEFINITION);
4057 : :
4058 : : /*
4059 : : * Create a "BLOBS" data item for the group, too. This is just a
4060 : : * placeholder for sorting; it carries no data now.
4061 : : */
4062 : 88 : lodata = pg_malloc_object(DumpableObject);
4063 : 88 : lodata->objType = DO_LARGE_OBJECT_DATA;
4064 : 88 : lodata->catId = nilCatalogId;
4065 : 88 : AssignDumpId(lodata);
4066 : 88 : lodata->name = pg_strdup(namebuf);
4067 : 88 : lodata->components |= DUMP_COMPONENT_DATA;
4068 : : /* Set up explicit dependency from data to metadata */
4069 : 88 : lodata->dependencies = pg_malloc_object(DumpId);
4070 : 88 : lodata->dependencies[0] = loinfo->dobj.dumpId;
4071 : 88 : lodata->nDeps = lodata->allocDeps = 1;
4072 : : }
4073 : :
4074 : 186 : PQclear(res);
4075 : 186 : destroyPQExpBuffer(loQry);
4076 : 186 : }
4077 : :
4078 : : /*
4079 : : * dumpLO
4080 : : *
4081 : : * dump the definition (metadata) of the given large object group
4082 : : */
4083 : : static void
4084 : 88 : dumpLO(Archive *fout, const LoInfo *loinfo)
4085 : : {
4086 : 88 : PQExpBuffer cquery = createPQExpBuffer();
4087 : :
4088 : : /*
4089 : : * The "definition" is just a newline-separated list of OIDs. We need to
4090 : : * put something into the dropStmt too, but it can just be a comment.
4091 : : */
4092 [ + + ]: 190 : for (int i = 0; i < loinfo->numlos; i++)
4093 : 102 : appendPQExpBuffer(cquery, "%u\n", loinfo->looids[i]);
4094 : :
4095 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4096 : 81 : ArchiveEntry(fout, loinfo->dobj.catId, loinfo->dobj.dumpId,
4097 : 81 : ARCHIVE_OPTS(.tag = loinfo->dobj.name,
4098 : : .owner = loinfo->rolname,
4099 : : .description = "BLOB METADATA",
4100 : : .section = SECTION_DATA,
4101 : : .createStmt = cquery->data,
4102 : : .dropStmt = "-- dummy"));
4103 : :
4104 : : /*
4105 : : * Dump per-blob comments and seclabels if any. We assume these are rare
4106 : : * enough that it's okay to generate retail TOC entries for them.
4107 : : */
4108 [ + + ]: 88 : if (loinfo->dobj.dump & (DUMP_COMPONENT_COMMENT |
4109 : : DUMP_COMPONENT_SECLABEL))
4110 : : {
4111 [ + + ]: 106 : for (int i = 0; i < loinfo->numlos; i++)
4112 : : {
4113 : : CatalogId catId;
4114 : : char namebuf[32];
4115 : :
4116 : : /* Build identifying info for this blob */
4117 : 60 : catId.tableoid = loinfo->dobj.catId.tableoid;
4118 : 60 : catId.oid = loinfo->looids[i];
4119 : 60 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[i]);
4120 : :
4121 [ + - ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4122 : 60 : dumpComment(fout, "LARGE OBJECT", namebuf,
4123 : 60 : NULL, loinfo->rolname,
4124 : 60 : catId, 0, loinfo->dobj.dumpId);
4125 : :
4126 [ + + ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4127 : 10 : dumpSecLabel(fout, "LARGE OBJECT", namebuf,
4128 : 10 : NULL, loinfo->rolname,
4129 : 10 : catId, 0, loinfo->dobj.dumpId);
4130 : : }
4131 : : }
4132 : :
4133 : : /*
4134 : : * Dump the ACLs if any (remember that all blobs in the group will have
4135 : : * the same ACL). If there's just one blob, dump a simple ACL entry; if
4136 : : * there's more, make a "LARGE OBJECTS" entry that really contains only
4137 : : * the ACL for the first blob. _printTocEntry() will be cued by the tag
4138 : : * string to emit a mutated version for each blob.
4139 : : */
4140 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_ACL)
4141 : : {
4142 : : char namebuf[32];
4143 : :
4144 : : /* Build identifying info for the first blob */
4145 : 35 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[0]);
4146 : :
4147 [ - + ]: 35 : if (loinfo->numlos > 1)
4148 : : {
4149 : : char tagbuf[64];
4150 : :
4151 : 0 : snprintf(tagbuf, sizeof(tagbuf), "LARGE OBJECTS %u..%u",
4152 : 0 : loinfo->looids[0], loinfo->looids[loinfo->numlos - 1]);
4153 : :
4154 : 0 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4155 : : "LARGE OBJECT", namebuf, NULL, NULL,
4156 : 0 : tagbuf, loinfo->rolname, &loinfo->dacl);
4157 : : }
4158 : : else
4159 : : {
4160 : 35 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4161 : : "LARGE OBJECT", namebuf, NULL, NULL,
4162 : 35 : NULL, loinfo->rolname, &loinfo->dacl);
4163 : : }
4164 : : }
4165 : :
4166 : 88 : destroyPQExpBuffer(cquery);
4167 : 88 : }
4168 : :
4169 : : /*
4170 : : * dumpLOs:
4171 : : * dump the data contents of the large objects in the given group
4172 : : */
4173 : : static int
4174 : 77 : dumpLOs(Archive *fout, const void *arg)
4175 : : {
4176 : 77 : const LoInfo *loinfo = (const LoInfo *) arg;
4177 : 77 : PGconn *conn = GetConnection(fout);
4178 : : char buf[LOBBUFSIZE];
4179 : :
4180 : 77 : pg_log_info("saving large objects \"%s\"", loinfo->dobj.name);
4181 : :
4182 [ + + ]: 162 : for (int i = 0; i < loinfo->numlos; i++)
4183 : : {
4184 : 85 : Oid loOid = loinfo->looids[i];
4185 : : int loFd;
4186 : : int cnt;
4187 : :
4188 : : /* Open the LO */
4189 : 85 : loFd = lo_open(conn, loOid, INV_READ);
4190 [ - + ]: 85 : if (loFd == -1)
4191 : 0 : pg_fatal("could not open large object %u: %s",
4192 : : loOid, PQerrorMessage(conn));
4193 : :
4194 : 85 : StartLO(fout, loOid);
4195 : :
4196 : : /* Now read it in chunks, sending data to archive */
4197 : : do
4198 : : {
4199 : 133 : cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
4200 [ - + ]: 133 : if (cnt < 0)
4201 : 0 : pg_fatal("error reading large object %u: %s",
4202 : : loOid, PQerrorMessage(conn));
4203 : :
4204 : 133 : WriteData(fout, buf, cnt);
4205 [ + + ]: 133 : } while (cnt > 0);
4206 : :
4207 : 85 : lo_close(conn, loFd);
4208 : :
4209 : 85 : EndLO(fout, loOid);
4210 : : }
4211 : :
4212 : 77 : return 1;
4213 : : }
4214 : :
4215 : : /*
4216 : : * getPolicies
4217 : : * get information about all RLS policies on dumpable tables.
4218 : : */
4219 : : void
4220 : 215 : getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
4221 : : {
4222 : 215 : DumpOptions *dopt = fout->dopt;
4223 : : PQExpBuffer query;
4224 : : PQExpBuffer tbloids;
4225 : : PGresult *res;
4226 : : PolicyInfo *polinfo;
4227 : : int i_oid;
4228 : : int i_tableoid;
4229 : : int i_polrelid;
4230 : : int i_polname;
4231 : : int i_polcmd;
4232 : : int i_polpermissive;
4233 : : int i_polroles;
4234 : : int i_polqual;
4235 : : int i_polwithcheck;
4236 : : int i,
4237 : : j,
4238 : : ntups;
4239 : :
4240 : : /* Skip if --no-policies was specified */
4241 [ + + ]: 215 : if (dopt->no_policies)
4242 : 1 : return;
4243 : :
4244 : 214 : query = createPQExpBuffer();
4245 : 214 : tbloids = createPQExpBuffer();
4246 : :
4247 : : /*
4248 : : * Identify tables of interest, and check which ones have RLS enabled.
4249 : : */
4250 : 214 : appendPQExpBufferChar(tbloids, '{');
4251 [ + + ]: 57405 : for (i = 0; i < numTables; i++)
4252 : : {
4253 : 57191 : TableInfo *tbinfo = &tblinfo[i];
4254 : :
4255 : : /* Ignore row security on tables not to be dumped */
4256 [ + + ]: 57191 : if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
4257 : 49628 : continue;
4258 : :
4259 : : /* It can't have RLS or policies if it's not a table */
4260 [ + + ]: 7563 : if (tbinfo->relkind != RELKIND_RELATION &&
4261 [ + + ]: 2095 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
4262 : 1451 : continue;
4263 : :
4264 : : /* Add it to the list of table OIDs to be probed below */
4265 [ + + ]: 6112 : if (tbloids->len > 1) /* do we have more than the '{'? */
4266 : 5970 : appendPQExpBufferChar(tbloids, ',');
4267 : 6112 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
4268 : :
4269 : : /* Is RLS enabled? (That's separate from whether it has policies) */
4270 [ + + ]: 6112 : if (tbinfo->rowsec)
4271 : : {
4272 : 56 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4273 : :
4274 : : /*
4275 : : * We represent RLS being enabled on a table by creating a
4276 : : * PolicyInfo object with null polname.
4277 : : *
4278 : : * Note: use tableoid 0 so that this object won't be mistaken for
4279 : : * something that pg_depend entries apply to.
4280 : : */
4281 : 56 : polinfo = pg_malloc_object(PolicyInfo);
4282 : 56 : polinfo->dobj.objType = DO_POLICY;
4283 : 56 : polinfo->dobj.catId.tableoid = 0;
4284 : 56 : polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
4285 : 56 : AssignDumpId(&polinfo->dobj);
4286 : 56 : polinfo->dobj.namespace = tbinfo->dobj.namespace;
4287 : 56 : polinfo->dobj.name = pg_strdup(tbinfo->dobj.name);
4288 : 56 : polinfo->poltable = tbinfo;
4289 : 56 : polinfo->polname = NULL;
4290 : 56 : polinfo->polcmd = '\0';
4291 : 56 : polinfo->polpermissive = 0;
4292 : 56 : polinfo->polroles = NULL;
4293 : 56 : polinfo->polqual = NULL;
4294 : 56 : polinfo->polwithcheck = NULL;
4295 : : }
4296 : : }
4297 : 214 : appendPQExpBufferChar(tbloids, '}');
4298 : :
4299 : : /*
4300 : : * Now, read all RLS policies belonging to the tables of interest, and
4301 : : * create PolicyInfo objects for them. (Note that we must filter the
4302 : : * results server-side not locally, because we dare not apply pg_get_expr
4303 : : * to tables we don't have lock on.)
4304 : : */
4305 : 214 : pg_log_info("reading row-level security policies");
4306 : :
4307 : 214 : printfPQExpBuffer(query,
4308 : : "SELECT pol.oid, pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
4309 : 214 : appendPQExpBufferStr(query, "pol.polpermissive, ");
4310 : 214 : appendPQExpBuffer(query,
4311 : : "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
4312 : : " 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, "
4313 : : "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
4314 : : "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
4315 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
4316 : : "JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)",
4317 : : tbloids->data);
4318 : :
4319 : 214 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4320 : :
4321 : 214 : ntups = PQntuples(res);
4322 [ + + ]: 214 : if (ntups > 0)
4323 : : {
4324 : 46 : i_oid = PQfnumber(res, "oid");
4325 : 46 : i_tableoid = PQfnumber(res, "tableoid");
4326 : 46 : i_polrelid = PQfnumber(res, "polrelid");
4327 : 46 : i_polname = PQfnumber(res, "polname");
4328 : 46 : i_polcmd = PQfnumber(res, "polcmd");
4329 : 46 : i_polpermissive = PQfnumber(res, "polpermissive");
4330 : 46 : i_polroles = PQfnumber(res, "polroles");
4331 : 46 : i_polqual = PQfnumber(res, "polqual");
4332 : 46 : i_polwithcheck = PQfnumber(res, "polwithcheck");
4333 : :
4334 : 46 : polinfo = pg_malloc_array(PolicyInfo, ntups);
4335 : :
4336 [ + + ]: 337 : for (j = 0; j < ntups; j++)
4337 : : {
4338 : 291 : Oid polrelid = atooid(PQgetvalue(res, j, i_polrelid));
4339 : 291 : TableInfo *tbinfo = findTableByOid(polrelid);
4340 : :
4341 : 291 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4342 : :
4343 : 291 : polinfo[j].dobj.objType = DO_POLICY;
4344 : 291 : polinfo[j].dobj.catId.tableoid =
4345 : 291 : atooid(PQgetvalue(res, j, i_tableoid));
4346 : 291 : polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
4347 : 291 : AssignDumpId(&polinfo[j].dobj);
4348 : 291 : polinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4349 : 291 : polinfo[j].poltable = tbinfo;
4350 : 291 : polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname));
4351 : 291 : polinfo[j].dobj.name = pg_strdup(polinfo[j].polname);
4352 : :
4353 : 291 : polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd));
4354 : 291 : polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't';
4355 : :
4356 [ + + ]: 291 : if (PQgetisnull(res, j, i_polroles))
4357 : 127 : polinfo[j].polroles = NULL;
4358 : : else
4359 : 164 : polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles));
4360 : :
4361 [ + + ]: 291 : if (PQgetisnull(res, j, i_polqual))
4362 : 41 : polinfo[j].polqual = NULL;
4363 : : else
4364 : 250 : polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual));
4365 : :
4366 [ + + ]: 291 : if (PQgetisnull(res, j, i_polwithcheck))
4367 : 153 : polinfo[j].polwithcheck = NULL;
4368 : : else
4369 : 138 : polinfo[j].polwithcheck
4370 : 138 : = pg_strdup(PQgetvalue(res, j, i_polwithcheck));
4371 : : }
4372 : : }
4373 : :
4374 : 214 : PQclear(res);
4375 : :
4376 : 214 : destroyPQExpBuffer(query);
4377 : 214 : destroyPQExpBuffer(tbloids);
4378 : : }
4379 : :
4380 : : /*
4381 : : * dumpPolicy
4382 : : * dump the definition of the given policy
4383 : : */
4384 : : static void
4385 : 347 : dumpPolicy(Archive *fout, const PolicyInfo *polinfo)
4386 : : {
4387 : 347 : DumpOptions *dopt = fout->dopt;
4388 : 347 : TableInfo *tbinfo = polinfo->poltable;
4389 : : PQExpBuffer query;
4390 : : PQExpBuffer delqry;
4391 : : PQExpBuffer polprefix;
4392 : : char *qtabname;
4393 : : const char *cmd;
4394 : : char *tag;
4395 : :
4396 : : /* Do nothing if not dumping schema */
4397 [ + + ]: 347 : if (!dopt->dumpSchema)
4398 : 56 : return;
4399 : :
4400 : : /*
4401 : : * If polname is NULL, then this record is just indicating that ROW LEVEL
4402 : : * SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE
4403 : : * ROW LEVEL SECURITY.
4404 : : */
4405 [ + + ]: 291 : if (polinfo->polname == NULL)
4406 : : {
4407 : 48 : query = createPQExpBuffer();
4408 : :
4409 : 48 : appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;",
4410 : 48 : fmtQualifiedDumpable(tbinfo));
4411 : :
4412 : : /*
4413 : : * We must emit the ROW SECURITY object's dependency on its table
4414 : : * explicitly, because it will not match anything in pg_depend (unlike
4415 : : * the case for other PolicyInfo objects).
4416 : : */
4417 [ + - ]: 48 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4418 : 48 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4419 : 48 : ARCHIVE_OPTS(.tag = polinfo->dobj.name,
4420 : : .namespace = polinfo->dobj.namespace->dobj.name,
4421 : : .owner = tbinfo->rolname,
4422 : : .description = "ROW SECURITY",
4423 : : .section = SECTION_POST_DATA,
4424 : : .createStmt = query->data,
4425 : : .deps = &(tbinfo->dobj.dumpId),
4426 : : .nDeps = 1));
4427 : :
4428 : 48 : destroyPQExpBuffer(query);
4429 : 48 : return;
4430 : : }
4431 : :
4432 [ + + ]: 243 : if (polinfo->polcmd == '*')
4433 : 81 : cmd = "";
4434 [ + + ]: 162 : else if (polinfo->polcmd == 'r')
4435 : 43 : cmd = " FOR SELECT";
4436 [ + + ]: 119 : else if (polinfo->polcmd == 'a')
4437 : 33 : cmd = " FOR INSERT";
4438 [ + + ]: 86 : else if (polinfo->polcmd == 'w')
4439 : 43 : cmd = " FOR UPDATE";
4440 [ + - ]: 43 : else if (polinfo->polcmd == 'd')
4441 : 43 : cmd = " FOR DELETE";
4442 : : else
4443 : 0 : pg_fatal("unexpected policy command type: %c",
4444 : : polinfo->polcmd);
4445 : :
4446 : 243 : query = createPQExpBuffer();
4447 : 243 : delqry = createPQExpBuffer();
4448 : 243 : polprefix = createPQExpBuffer();
4449 : :
4450 : 243 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
4451 : :
4452 : 243 : appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname));
4453 : :
4454 : 243 : appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo),
4455 [ + + ]: 243 : !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd);
4456 : :
4457 [ + + ]: 243 : if (polinfo->polroles != NULL)
4458 : 132 : appendPQExpBuffer(query, " TO %s", polinfo->polroles);
4459 : :
4460 [ + + ]: 243 : if (polinfo->polqual != NULL)
4461 : 210 : appendPQExpBuffer(query, " USING (%s)", polinfo->polqual);
4462 : :
4463 [ + + ]: 243 : if (polinfo->polwithcheck != NULL)
4464 : 114 : appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck);
4465 : :
4466 : 243 : appendPQExpBufferStr(query, ";\n");
4467 : :
4468 : 243 : appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname));
4469 : 243 : appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo));
4470 : :
4471 : 243 : appendPQExpBuffer(polprefix, "POLICY %s ON",
4472 : 243 : fmtId(polinfo->polname));
4473 : :
4474 : 243 : tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
4475 : :
4476 [ + - ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4477 : 243 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4478 : 243 : ARCHIVE_OPTS(.tag = tag,
4479 : : .namespace = polinfo->dobj.namespace->dobj.name,
4480 : : .owner = tbinfo->rolname,
4481 : : .description = "POLICY",
4482 : : .section = SECTION_POST_DATA,
4483 : : .createStmt = query->data,
4484 : : .dropStmt = delqry->data));
4485 : :
4486 [ + + ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4487 : 33 : dumpComment(fout, polprefix->data, qtabname,
4488 : 33 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
4489 : 33 : polinfo->dobj.catId, 0, polinfo->dobj.dumpId);
4490 : :
4491 : 243 : pfree(tag);
4492 : 243 : destroyPQExpBuffer(query);
4493 : 243 : destroyPQExpBuffer(delqry);
4494 : 243 : destroyPQExpBuffer(polprefix);
4495 : 243 : pg_free(qtabname);
4496 : : }
4497 : :
4498 : : /*
4499 : : * getPublications
4500 : : * get information about publications
4501 : : */
4502 : : void
4503 : 215 : getPublications(Archive *fout)
4504 : : {
4505 : 215 : DumpOptions *dopt = fout->dopt;
4506 : : PQExpBuffer query;
4507 : : PGresult *res;
4508 : : PublicationInfo *pubinfo;
4509 : : int i_tableoid;
4510 : : int i_oid;
4511 : : int i_pubname;
4512 : : int i_pubowner;
4513 : : int i_puballtables;
4514 : : int i_puballsequences;
4515 : : int i_pubinsert;
4516 : : int i_pubupdate;
4517 : : int i_pubdelete;
4518 : : int i_pubtruncate;
4519 : : int i_pubviaroot;
4520 : : int i_pubgencols;
4521 : : int i,
4522 : : ntups;
4523 : :
4524 [ - + ]: 215 : if (dopt->no_publications)
4525 : 0 : return;
4526 : :
4527 : 215 : query = createPQExpBuffer();
4528 : :
4529 : : /* Get the publications. */
4530 : 215 : appendPQExpBufferStr(query, "SELECT p.tableoid, p.oid, p.pubname, "
4531 : : "p.pubowner, p.puballtables, p.pubinsert, "
4532 : : "p.pubupdate, p.pubdelete, ");
4533 : :
4534 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
4535 : 215 : appendPQExpBufferStr(query, "p.pubtruncate, ");
4536 : : else
4537 : 0 : appendPQExpBufferStr(query, "false AS pubtruncate, ");
4538 : :
4539 [ + - ]: 215 : if (fout->remoteVersion >= 130000)
4540 : 215 : appendPQExpBufferStr(query, "p.pubviaroot, ");
4541 : : else
4542 : 0 : appendPQExpBufferStr(query, "false AS pubviaroot, ");
4543 : :
4544 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
4545 : 215 : appendPQExpBufferStr(query, "p.pubgencols, ");
4546 : : else
4547 : 0 : appendPQExpBuffer(query, "'%c' AS pubgencols, ", PUBLISH_GENCOLS_NONE);
4548 : :
4549 [ + - ]: 215 : if (fout->remoteVersion >= 190000)
4550 : 215 : appendPQExpBufferStr(query, "p.puballsequences ");
4551 : : else
4552 : 0 : appendPQExpBufferStr(query, "false AS puballsequences ");
4553 : :
4554 : 215 : appendPQExpBufferStr(query, "FROM pg_publication p");
4555 : :
4556 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4557 : :
4558 : 215 : ntups = PQntuples(res);
4559 : :
4560 [ + + ]: 215 : if (ntups == 0)
4561 : 159 : goto cleanup;
4562 : :
4563 : 56 : i_tableoid = PQfnumber(res, "tableoid");
4564 : 56 : i_oid = PQfnumber(res, "oid");
4565 : 56 : i_pubname = PQfnumber(res, "pubname");
4566 : 56 : i_pubowner = PQfnumber(res, "pubowner");
4567 : 56 : i_puballtables = PQfnumber(res, "puballtables");
4568 : 56 : i_puballsequences = PQfnumber(res, "puballsequences");
4569 : 56 : i_pubinsert = PQfnumber(res, "pubinsert");
4570 : 56 : i_pubupdate = PQfnumber(res, "pubupdate");
4571 : 56 : i_pubdelete = PQfnumber(res, "pubdelete");
4572 : 56 : i_pubtruncate = PQfnumber(res, "pubtruncate");
4573 : 56 : i_pubviaroot = PQfnumber(res, "pubviaroot");
4574 : 56 : i_pubgencols = PQfnumber(res, "pubgencols");
4575 : :
4576 : 56 : pubinfo = pg_malloc_array(PublicationInfo, ntups);
4577 : :
4578 [ + + ]: 572 : for (i = 0; i < ntups; i++)
4579 : : {
4580 : 516 : pubinfo[i].dobj.objType = DO_PUBLICATION;
4581 : 516 : pubinfo[i].dobj.catId.tableoid =
4582 : 516 : atooid(PQgetvalue(res, i, i_tableoid));
4583 : 516 : pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4584 : 516 : AssignDumpId(&pubinfo[i].dobj);
4585 : 516 : pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname));
4586 : 516 : pubinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_pubowner));
4587 : 516 : pubinfo[i].puballtables =
4588 : 516 : (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0);
4589 : 516 : pubinfo[i].puballsequences =
4590 : 516 : (strcmp(PQgetvalue(res, i, i_puballsequences), "t") == 0);
4591 : 516 : pubinfo[i].pubinsert =
4592 : 516 : (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0);
4593 : 516 : pubinfo[i].pubupdate =
4594 : 516 : (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0);
4595 : 516 : pubinfo[i].pubdelete =
4596 : 516 : (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
4597 : 516 : pubinfo[i].pubtruncate =
4598 : 516 : (strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0);
4599 : 516 : pubinfo[i].pubviaroot =
4600 : 516 : (strcmp(PQgetvalue(res, i, i_pubviaroot), "t") == 0);
4601 : 516 : pubinfo[i].pubgencols_type =
4602 : 516 : *(PQgetvalue(res, i, i_pubgencols));
4603 : 516 : pubinfo[i].except_tables = (SimplePtrList)
4604 : : {
4605 : : NULL, NULL
4606 : : };
4607 : :
4608 : : /* Decide whether we want to dump it */
4609 : 516 : selectDumpableObject(&(pubinfo[i].dobj), fout);
4610 : :
4611 : : /*
4612 : : * Get the list of tables for publications specified in the EXCEPT
4613 : : * TABLE clause.
4614 : : *
4615 : : * Although individual table entries in EXCEPT list could be stored in
4616 : : * PublicationRelInfo, dumpPublicationTable cannot be used to emit
4617 : : * them, because there is no ALTER PUBLICATION ... ADD command to add
4618 : : * individual table entries to the EXCEPT list.
4619 : : *
4620 : : * Therefore, the approach is to dump the complete EXCEPT list in a
4621 : : * single CREATE PUBLICATION statement. PublicationInfo is used to
4622 : : * collect this information, which is then emitted by
4623 : : * dumpPublication().
4624 : : */
4625 [ + - ]: 516 : if (fout->remoteVersion >= 190000)
4626 : : {
4627 : : int ntbls;
4628 : : PGresult *res_tbls;
4629 : :
4630 : 516 : resetPQExpBuffer(query);
4631 : 516 : appendPQExpBuffer(query,
4632 : : "SELECT prrelid\n"
4633 : : "FROM pg_catalog.pg_publication_rel\n"
4634 : : "WHERE prpubid = %u AND prexcept",
4635 : 516 : pubinfo[i].dobj.catId.oid);
4636 : :
4637 : 516 : res_tbls = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4638 : :
4639 : 516 : ntbls = PQntuples(res_tbls);
4640 : :
4641 [ + + ]: 756 : for (int j = 0; j < ntbls; j++)
4642 : : {
4643 : : Oid prrelid;
4644 : : TableInfo *tbinfo;
4645 : :
4646 : 240 : prrelid = atooid(PQgetvalue(res_tbls, j, 0));
4647 : :
4648 : 240 : tbinfo = findTableByOid(prrelid);
4649 : :
4650 [ + - ]: 240 : if (tbinfo != NULL)
4651 : 240 : simple_ptr_list_append(&pubinfo[i].except_tables, tbinfo);
4652 : : }
4653 : :
4654 : 516 : PQclear(res_tbls);
4655 : : }
4656 : : }
4657 : :
4658 : 56 : cleanup:
4659 : 215 : PQclear(res);
4660 : :
4661 : 215 : destroyPQExpBuffer(query);
4662 : : }
4663 : :
4664 : : /*
4665 : : * dumpPublication
4666 : : * dump the definition of the given publication
4667 : : */
4668 : : static void
4669 : 416 : dumpPublication(Archive *fout, const PublicationInfo *pubinfo)
4670 : : {
4671 : 416 : DumpOptions *dopt = fout->dopt;
4672 : : PQExpBuffer delq;
4673 : : PQExpBuffer query;
4674 : : char *qpubname;
4675 : 416 : bool first = true;
4676 : :
4677 : : /* Do nothing if not dumping schema */
4678 [ + + ]: 416 : if (!dopt->dumpSchema)
4679 : 60 : return;
4680 : :
4681 : 356 : delq = createPQExpBuffer();
4682 : 356 : query = createPQExpBuffer();
4683 : :
4684 : 356 : qpubname = pg_strdup(fmtId(pubinfo->dobj.name));
4685 : :
4686 : 356 : appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n",
4687 : : qpubname);
4688 : :
4689 : 356 : appendPQExpBuffer(query, "CREATE PUBLICATION %s",
4690 : : qpubname);
4691 : :
4692 [ + + ]: 356 : if (pubinfo->puballtables)
4693 : : {
4694 : 166 : int n_except = 0;
4695 : :
4696 : 166 : appendPQExpBufferStr(query, " FOR ALL TABLES");
4697 : :
4698 : : /* Include EXCEPT (TABLE) clause if there are except_tables. */
4699 [ + + ]: 331 : for (SimplePtrListCell *cell = pubinfo->except_tables.head; cell; cell = cell->next)
4700 : : {
4701 : 165 : TableInfo *tbinfo = (TableInfo *) cell->ptr;
4702 : :
4703 [ + + ]: 165 : if (++n_except == 1)
4704 : 99 : appendPQExpBufferStr(query, " EXCEPT (");
4705 : : else
4706 : 66 : appendPQExpBufferStr(query, ", ");
4707 : 165 : appendPQExpBuffer(query, "TABLE ONLY %s", fmtQualifiedDumpable(tbinfo));
4708 : : }
4709 [ + + ]: 166 : if (n_except > 0)
4710 : 99 : appendPQExpBufferChar(query, ')');
4711 : :
4712 [ + + ]: 166 : if (pubinfo->puballsequences)
4713 : 33 : appendPQExpBufferStr(query, ", ALL SEQUENCES");
4714 : : }
4715 [ + + ]: 190 : else if (pubinfo->puballsequences)
4716 : 33 : appendPQExpBufferStr(query, " FOR ALL SEQUENCES");
4717 : :
4718 : 356 : appendPQExpBufferStr(query, " WITH (publish = '");
4719 [ + + ]: 356 : if (pubinfo->pubinsert)
4720 : : {
4721 : 290 : appendPQExpBufferStr(query, "insert");
4722 : 290 : first = false;
4723 : : }
4724 : :
4725 [ + + ]: 356 : if (pubinfo->pubupdate)
4726 : : {
4727 [ + - ]: 290 : if (!first)
4728 : 290 : appendPQExpBufferStr(query, ", ");
4729 : :
4730 : 290 : appendPQExpBufferStr(query, "update");
4731 : 290 : first = false;
4732 : : }
4733 : :
4734 [ + + ]: 356 : if (pubinfo->pubdelete)
4735 : : {
4736 [ + - ]: 290 : if (!first)
4737 : 290 : appendPQExpBufferStr(query, ", ");
4738 : :
4739 : 290 : appendPQExpBufferStr(query, "delete");
4740 : 290 : first = false;
4741 : : }
4742 : :
4743 [ + + ]: 356 : if (pubinfo->pubtruncate)
4744 : : {
4745 [ + - ]: 290 : if (!first)
4746 : 290 : appendPQExpBufferStr(query, ", ");
4747 : :
4748 : 290 : appendPQExpBufferStr(query, "truncate");
4749 : 290 : first = false;
4750 : : }
4751 : :
4752 : 356 : appendPQExpBufferChar(query, '\'');
4753 : :
4754 [ + + ]: 356 : if (pubinfo->pubviaroot)
4755 : 5 : appendPQExpBufferStr(query, ", publish_via_partition_root = true");
4756 : :
4757 [ + + ]: 356 : if (pubinfo->pubgencols_type == PUBLISH_GENCOLS_STORED)
4758 : 33 : appendPQExpBufferStr(query, ", publish_generated_columns = stored");
4759 : :
4760 : 356 : appendPQExpBufferStr(query, ");\n");
4761 : :
4762 [ + - ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4763 : 356 : ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
4764 : 356 : ARCHIVE_OPTS(.tag = pubinfo->dobj.name,
4765 : : .owner = pubinfo->rolname,
4766 : : .description = "PUBLICATION",
4767 : : .section = SECTION_POST_DATA,
4768 : : .createStmt = query->data,
4769 : : .dropStmt = delq->data));
4770 : :
4771 [ + + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4772 : 33 : dumpComment(fout, "PUBLICATION", qpubname,
4773 : 33 : NULL, pubinfo->rolname,
4774 : 33 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4775 : :
4776 [ - + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4777 : 0 : dumpSecLabel(fout, "PUBLICATION", qpubname,
4778 : 0 : NULL, pubinfo->rolname,
4779 : 0 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4780 : :
4781 : 356 : destroyPQExpBuffer(delq);
4782 : 356 : destroyPQExpBuffer(query);
4783 : 356 : pg_free(qpubname);
4784 : : }
4785 : :
4786 : : /*
4787 : : * getPublicationNamespaces
4788 : : * get information about publication membership for dumpable schemas.
4789 : : */
4790 : : void
4791 : 215 : getPublicationNamespaces(Archive *fout)
4792 : : {
4793 : : PQExpBuffer query;
4794 : : PGresult *res;
4795 : : PublicationSchemaInfo *pubsinfo;
4796 : 215 : DumpOptions *dopt = fout->dopt;
4797 : : int i_tableoid;
4798 : : int i_oid;
4799 : : int i_pnpubid;
4800 : : int i_pnnspid;
4801 : : int i,
4802 : : j,
4803 : : ntups;
4804 : :
4805 [ + - - + ]: 215 : if (dopt->no_publications || fout->remoteVersion < 150000)
4806 : 0 : return;
4807 : :
4808 : 215 : query = createPQExpBuffer();
4809 : :
4810 : : /* Collect all publication membership info. */
4811 : 215 : appendPQExpBufferStr(query,
4812 : : "SELECT tableoid, oid, pnpubid, pnnspid "
4813 : : "FROM pg_catalog.pg_publication_namespace");
4814 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4815 : :
4816 : 215 : ntups = PQntuples(res);
4817 : :
4818 : 215 : i_tableoid = PQfnumber(res, "tableoid");
4819 : 215 : i_oid = PQfnumber(res, "oid");
4820 : 215 : i_pnpubid = PQfnumber(res, "pnpubid");
4821 : 215 : i_pnnspid = PQfnumber(res, "pnnspid");
4822 : :
4823 : : /* this allocation may be more than we need */
4824 : 215 : pubsinfo = pg_malloc_array(PublicationSchemaInfo, ntups);
4825 : 215 : j = 0;
4826 : :
4827 [ + + ]: 346 : for (i = 0; i < ntups; i++)
4828 : : {
4829 : 131 : Oid pnpubid = atooid(PQgetvalue(res, i, i_pnpubid));
4830 : 131 : Oid pnnspid = atooid(PQgetvalue(res, i, i_pnnspid));
4831 : : PublicationInfo *pubinfo;
4832 : : NamespaceInfo *nspinfo;
4833 : :
4834 : : /*
4835 : : * Ignore any entries for which we aren't interested in either the
4836 : : * publication or the rel.
4837 : : */
4838 : 131 : pubinfo = findPublicationByOid(pnpubid);
4839 [ - + ]: 131 : if (pubinfo == NULL)
4840 : 0 : continue;
4841 : 131 : nspinfo = findNamespaceByOid(pnnspid);
4842 [ - + ]: 131 : if (nspinfo == NULL)
4843 : 0 : continue;
4844 : :
4845 : : /* OK, make a DumpableObject for this relationship */
4846 : 131 : pubsinfo[j].dobj.objType = DO_PUBLICATION_TABLE_IN_SCHEMA;
4847 : 131 : pubsinfo[j].dobj.catId.tableoid =
4848 : 131 : atooid(PQgetvalue(res, i, i_tableoid));
4849 : 131 : pubsinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4850 : 131 : AssignDumpId(&pubsinfo[j].dobj);
4851 : 131 : pubsinfo[j].dobj.namespace = nspinfo->dobj.namespace;
4852 : 131 : pubsinfo[j].dobj.name = nspinfo->dobj.name;
4853 : 131 : pubsinfo[j].publication = pubinfo;
4854 : 131 : pubsinfo[j].pubschema = nspinfo;
4855 : :
4856 : : /* Decide whether we want to dump it */
4857 : 131 : selectDumpablePublicationObject(&(pubsinfo[j].dobj), fout);
4858 : :
4859 : 131 : j++;
4860 : : }
4861 : :
4862 : 215 : PQclear(res);
4863 : 215 : destroyPQExpBuffer(query);
4864 : : }
4865 : :
4866 : : /*
4867 : : * getPublicationTables
4868 : : * get information about publication membership for dumpable tables.
4869 : : */
4870 : : void
4871 : 215 : getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
4872 : : {
4873 : : PQExpBuffer query;
4874 : : PGresult *res;
4875 : : PublicationRelInfo *pubrinfo;
4876 : 215 : DumpOptions *dopt = fout->dopt;
4877 : : int i_tableoid;
4878 : : int i_oid;
4879 : : int i_prpubid;
4880 : : int i_prrelid;
4881 : : int i_prrelqual;
4882 : : int i_prattrs;
4883 : : int i,
4884 : : j,
4885 : : ntups;
4886 : :
4887 [ - + ]: 215 : if (dopt->no_publications)
4888 : 0 : return;
4889 : :
4890 : 215 : query = createPQExpBuffer();
4891 : :
4892 : : /* Collect all publication membership info. */
4893 [ + - ]: 215 : if (fout->remoteVersion >= 150000)
4894 : : {
4895 : 215 : appendPQExpBufferStr(query,
4896 : : "SELECT tableoid, oid, prpubid, prrelid, "
4897 : : "pg_catalog.pg_get_expr(prqual, prrelid) AS prrelqual, "
4898 : : "(CASE\n"
4899 : : " WHEN pr.prattrs IS NOT NULL THEN\n"
4900 : : " (SELECT array_agg(attname)\n"
4901 : : " FROM\n"
4902 : : " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
4903 : : " pg_catalog.pg_attribute\n"
4904 : : " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
4905 : : " ELSE NULL END) prattrs "
4906 : : "FROM pg_catalog.pg_publication_rel pr");
4907 [ + - ]: 215 : if (fout->remoteVersion >= 190000)
4908 : 215 : appendPQExpBufferStr(query, " WHERE NOT pr.prexcept");
4909 : : }
4910 : : else
4911 : 0 : appendPQExpBufferStr(query,
4912 : : "SELECT tableoid, oid, prpubid, prrelid, "
4913 : : "NULL AS prrelqual, NULL AS prattrs "
4914 : : "FROM pg_catalog.pg_publication_rel");
4915 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4916 : :
4917 : 215 : ntups = PQntuples(res);
4918 : :
4919 : 215 : i_tableoid = PQfnumber(res, "tableoid");
4920 : 215 : i_oid = PQfnumber(res, "oid");
4921 : 215 : i_prpubid = PQfnumber(res, "prpubid");
4922 : 215 : i_prrelid = PQfnumber(res, "prrelid");
4923 : 215 : i_prrelqual = PQfnumber(res, "prrelqual");
4924 : 215 : i_prattrs = PQfnumber(res, "prattrs");
4925 : :
4926 : : /* this allocation may be more than we need */
4927 : 215 : pubrinfo = pg_malloc_array(PublicationRelInfo, ntups);
4928 : 215 : j = 0;
4929 : :
4930 [ + + ]: 586 : for (i = 0; i < ntups; i++)
4931 : : {
4932 : 371 : Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid));
4933 : 371 : Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid));
4934 : : PublicationInfo *pubinfo;
4935 : : TableInfo *tbinfo;
4936 : :
4937 : : /*
4938 : : * Ignore any entries for which we aren't interested in either the
4939 : : * publication or the rel.
4940 : : */
4941 : 371 : pubinfo = findPublicationByOid(prpubid);
4942 [ - + ]: 371 : if (pubinfo == NULL)
4943 : 0 : continue;
4944 : 371 : tbinfo = findTableByOid(prrelid);
4945 [ - + ]: 371 : if (tbinfo == NULL)
4946 : 0 : continue;
4947 : :
4948 : : /* OK, make a DumpableObject for this relationship */
4949 : 371 : pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
4950 : 371 : pubrinfo[j].dobj.catId.tableoid =
4951 : 371 : atooid(PQgetvalue(res, i, i_tableoid));
4952 : 371 : pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4953 : 371 : AssignDumpId(&pubrinfo[j].dobj);
4954 : 371 : pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4955 : 371 : pubrinfo[j].dobj.name = tbinfo->dobj.name;
4956 : 371 : pubrinfo[j].publication = pubinfo;
4957 : 371 : pubrinfo[j].pubtable = tbinfo;
4958 [ + + ]: 371 : if (PQgetisnull(res, i, i_prrelqual))
4959 : 206 : pubrinfo[j].pubrelqual = NULL;
4960 : : else
4961 : 165 : pubrinfo[j].pubrelqual = pg_strdup(PQgetvalue(res, i, i_prrelqual));
4962 : :
4963 [ + + ]: 371 : if (!PQgetisnull(res, i, i_prattrs))
4964 : : {
4965 : : char **attnames;
4966 : : int nattnames;
4967 : : PQExpBuffer attribs;
4968 : :
4969 [ - + ]: 117 : if (!parsePGArray(PQgetvalue(res, i, i_prattrs),
4970 : : &attnames, &nattnames))
4971 : 0 : pg_fatal("could not parse %s array", "prattrs");
4972 : 117 : attribs = createPQExpBuffer();
4973 [ + + ]: 337 : for (int k = 0; k < nattnames; k++)
4974 : : {
4975 [ + + ]: 220 : if (k > 0)
4976 : 103 : appendPQExpBufferStr(attribs, ", ");
4977 : :
4978 : 220 : appendPQExpBufferStr(attribs, fmtId(attnames[k]));
4979 : : }
4980 : 117 : pubrinfo[j].pubrattrs = attribs->data;
4981 : 117 : free(attribs); /* but not attribs->data */
4982 : 117 : free(attnames);
4983 : : }
4984 : : else
4985 : 254 : pubrinfo[j].pubrattrs = NULL;
4986 : :
4987 : : /* Decide whether we want to dump it */
4988 : 371 : selectDumpablePublicationObject(&(pubrinfo[j].dobj), fout);
4989 : :
4990 : 371 : j++;
4991 : : }
4992 : :
4993 : 215 : PQclear(res);
4994 : 215 : destroyPQExpBuffer(query);
4995 : : }
4996 : :
4997 : : /*
4998 : : * dumpPublicationNamespace
4999 : : * dump the definition of the given publication schema mapping.
5000 : : */
5001 : : static void
5002 : 103 : dumpPublicationNamespace(Archive *fout, const PublicationSchemaInfo *pubsinfo)
5003 : : {
5004 : 103 : DumpOptions *dopt = fout->dopt;
5005 : 103 : NamespaceInfo *schemainfo = pubsinfo->pubschema;
5006 : 103 : PublicationInfo *pubinfo = pubsinfo->publication;
5007 : : PQExpBuffer query;
5008 : : char *tag;
5009 : :
5010 : : /* Do nothing if not dumping schema */
5011 [ + + ]: 103 : if (!dopt->dumpSchema)
5012 : 12 : return;
5013 : :
5014 : 91 : tag = psprintf("%s %s", pubinfo->dobj.name, schemainfo->dobj.name);
5015 : :
5016 : 91 : query = createPQExpBuffer();
5017 : :
5018 : 91 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ", fmtId(pubinfo->dobj.name));
5019 : 91 : appendPQExpBuffer(query, "ADD TABLES IN SCHEMA %s;\n", fmtId(schemainfo->dobj.name));
5020 : :
5021 : : /*
5022 : : * There is no point in creating drop query as the drop is done by schema
5023 : : * drop.
5024 : : */
5025 [ + - ]: 91 : if (pubsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5026 : 91 : ArchiveEntry(fout, pubsinfo->dobj.catId, pubsinfo->dobj.dumpId,
5027 : 91 : ARCHIVE_OPTS(.tag = tag,
5028 : : .namespace = schemainfo->dobj.name,
5029 : : .owner = pubinfo->rolname,
5030 : : .description = "PUBLICATION TABLES IN SCHEMA",
5031 : : .section = SECTION_POST_DATA,
5032 : : .createStmt = query->data));
5033 : :
5034 : : /* These objects can't currently have comments or seclabels */
5035 : :
5036 : 91 : pfree(tag);
5037 : 91 : destroyPQExpBuffer(query);
5038 : : }
5039 : :
5040 : : /*
5041 : : * dumpPublicationTable
5042 : : * dump the definition of the given publication table mapping
5043 : : */
5044 : : static void
5045 : 298 : dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo)
5046 : : {
5047 : 298 : DumpOptions *dopt = fout->dopt;
5048 : 298 : PublicationInfo *pubinfo = pubrinfo->publication;
5049 : 298 : TableInfo *tbinfo = pubrinfo->pubtable;
5050 : : PQExpBuffer query;
5051 : : char *tag;
5052 : :
5053 : : /* Do nothing if not dumping schema */
5054 [ + + ]: 298 : if (!dopt->dumpSchema)
5055 : 42 : return;
5056 : :
5057 : 256 : tag = psprintf("%s %s", pubinfo->dobj.name, tbinfo->dobj.name);
5058 : :
5059 : 256 : query = createPQExpBuffer();
5060 : :
5061 : 256 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
5062 : 256 : fmtId(pubinfo->dobj.name));
5063 : 256 : appendPQExpBuffer(query, " %s",
5064 : 256 : fmtQualifiedDumpable(tbinfo));
5065 : :
5066 [ + + ]: 256 : if (pubrinfo->pubrattrs)
5067 : 81 : appendPQExpBuffer(query, " (%s)", pubrinfo->pubrattrs);
5068 : :
5069 [ + + ]: 256 : if (pubrinfo->pubrelqual)
5070 : : {
5071 : : /*
5072 : : * It's necessary to add parentheses around the expression because
5073 : : * pg_get_expr won't supply the parentheses for things like WHERE
5074 : : * TRUE.
5075 : : */
5076 : 114 : appendPQExpBuffer(query, " WHERE (%s)", pubrinfo->pubrelqual);
5077 : : }
5078 : 256 : appendPQExpBufferStr(query, ";\n");
5079 : :
5080 : : /*
5081 : : * There is no point in creating a drop query as the drop is done by table
5082 : : * drop. (If you think to change this, see also _printTocEntry().)
5083 : : * Although this object doesn't really have ownership as such, set the
5084 : : * owner field anyway to ensure that the command is run by the correct
5085 : : * role at restore time.
5086 : : */
5087 [ + - ]: 256 : if (pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5088 : 256 : ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
5089 : 256 : ARCHIVE_OPTS(.tag = tag,
5090 : : .namespace = tbinfo->dobj.namespace->dobj.name,
5091 : : .owner = pubinfo->rolname,
5092 : : .description = "PUBLICATION TABLE",
5093 : : .section = SECTION_POST_DATA,
5094 : : .createStmt = query->data));
5095 : :
5096 : : /* These objects can't currently have comments or seclabels */
5097 : :
5098 : 256 : pfree(tag);
5099 : 256 : destroyPQExpBuffer(query);
5100 : : }
5101 : :
5102 : : /*
5103 : : * Is the currently connected user a superuser?
5104 : : */
5105 : : static bool
5106 : 214 : is_superuser(Archive *fout)
5107 : : {
5108 : 214 : ArchiveHandle *AH = (ArchiveHandle *) fout;
5109 : : const char *val;
5110 : :
5111 : 214 : val = PQparameterStatus(AH->connection, "is_superuser");
5112 : :
5113 [ + - + + ]: 214 : if (val && strcmp(val, "on") == 0)
5114 : 211 : return true;
5115 : :
5116 : 3 : return false;
5117 : : }
5118 : :
5119 : : /*
5120 : : * Set the given value to restrict_nonsystem_relation_kind value. Since
5121 : : * restrict_nonsystem_relation_kind is introduced in minor version releases,
5122 : : * the setting query is effective only where available.
5123 : : */
5124 : : static void
5125 : 249 : set_restrict_relation_kind(Archive *AH, const char *value)
5126 : : {
5127 : 249 : PQExpBuffer query = createPQExpBuffer();
5128 : : PGresult *res;
5129 : :
5130 : 249 : appendPQExpBuffer(query,
5131 : : "SELECT set_config(name, '%s', false) "
5132 : : "FROM pg_settings "
5133 : : "WHERE name = 'restrict_nonsystem_relation_kind'",
5134 : : value);
5135 : 249 : res = ExecuteSqlQuery(AH, query->data, PGRES_TUPLES_OK);
5136 : :
5137 : 249 : PQclear(res);
5138 : 249 : destroyPQExpBuffer(query);
5139 : 249 : }
5140 : :
5141 : : /*
5142 : : * getSubscriptions
5143 : : * get information about subscriptions
5144 : : */
5145 : : void
5146 : 215 : getSubscriptions(Archive *fout)
5147 : : {
5148 : 215 : DumpOptions *dopt = fout->dopt;
5149 : : PQExpBuffer query;
5150 : : PGresult *res;
5151 : : SubscriptionInfo *subinfo;
5152 : : int i_tableoid;
5153 : : int i_oid;
5154 : : int i_subname;
5155 : : int i_subowner;
5156 : : int i_subbinary;
5157 : : int i_substream;
5158 : : int i_subtwophasestate;
5159 : : int i_subdisableonerr;
5160 : : int i_subpasswordrequired;
5161 : : int i_subrunasowner;
5162 : : int i_subservername;
5163 : : int i_subconninfo;
5164 : : int i_subslotname;
5165 : : int i_subsynccommit;
5166 : : int i_subwalrcvtimeout;
5167 : : int i_subpublications;
5168 : : int i_suborigin;
5169 : : int i_suboriginremotelsn;
5170 : : int i_subenabled;
5171 : : int i_subfailover;
5172 : : int i_subretaindeadtuples;
5173 : : int i_submaxretention;
5174 : : int i_subconflictlogdest;
5175 : : int i,
5176 : : ntups;
5177 : :
5178 [ + + ]: 215 : if (dopt->no_subscriptions)
5179 : 1 : return;
5180 : :
5181 [ + + ]: 214 : if (!is_superuser(fout))
5182 : : {
5183 : : int n;
5184 : :
5185 : 3 : res = ExecuteSqlQuery(fout,
5186 : : "SELECT count(*) FROM pg_subscription "
5187 : : "WHERE subdbid = (SELECT oid FROM pg_database"
5188 : : " WHERE datname = current_database())",
5189 : : PGRES_TUPLES_OK);
5190 : 3 : n = atoi(PQgetvalue(res, 0, 0));
5191 [ + + ]: 3 : if (n > 0)
5192 : 2 : pg_log_warning("subscriptions not dumped because current user is not a superuser");
5193 : 3 : PQclear(res);
5194 : 3 : return;
5195 : : }
5196 : :
5197 : 211 : query = createPQExpBuffer();
5198 : :
5199 : : /* Get the subscriptions in current database. */
5200 : 211 : appendPQExpBufferStr(query,
5201 : : "SELECT s.tableoid, s.oid, s.subname,\n"
5202 : : " s.subowner,\n"
5203 : : " s.subconninfo, s.subslotname, s.subsynccommit,\n"
5204 : : " s.subpublications,\n");
5205 : :
5206 [ + - ]: 211 : if (fout->remoteVersion >= 140000)
5207 : 211 : appendPQExpBufferStr(query, " s.subbinary,\n");
5208 : : else
5209 : 0 : appendPQExpBufferStr(query, " false AS subbinary,\n");
5210 : :
5211 [ + - ]: 211 : if (fout->remoteVersion >= 140000)
5212 : 211 : appendPQExpBufferStr(query, " s.substream,\n");
5213 : : else
5214 : 0 : appendPQExpBufferStr(query, " 'f' AS substream,\n");
5215 : :
5216 [ + - ]: 211 : if (fout->remoteVersion >= 150000)
5217 : 211 : appendPQExpBufferStr(query,
5218 : : " s.subtwophasestate,\n"
5219 : : " s.subdisableonerr,\n");
5220 : : else
5221 : 0 : appendPQExpBuffer(query,
5222 : : " '%c' AS subtwophasestate,\n"
5223 : : " false AS subdisableonerr,\n",
5224 : : LOGICALREP_TWOPHASE_STATE_DISABLED);
5225 : :
5226 [ + - ]: 211 : if (fout->remoteVersion >= 160000)
5227 : 211 : appendPQExpBufferStr(query,
5228 : : " s.subpasswordrequired,\n"
5229 : : " s.subrunasowner,\n"
5230 : : " s.suborigin,\n");
5231 : : else
5232 : 0 : appendPQExpBuffer(query,
5233 : : " 't' AS subpasswordrequired,\n"
5234 : : " 't' AS subrunasowner,\n"
5235 : : " '%s' AS suborigin,\n",
5236 : : LOGICALREP_ORIGIN_ANY);
5237 : :
5238 [ + + + - ]: 211 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5239 : 44 : appendPQExpBufferStr(query, " o.remote_lsn AS suboriginremotelsn,\n"
5240 : : " s.subenabled,\n");
5241 : : else
5242 : 167 : appendPQExpBufferStr(query, " NULL AS suboriginremotelsn,\n"
5243 : : " false AS subenabled,\n");
5244 : :
5245 [ + - ]: 211 : if (fout->remoteVersion >= 170000)
5246 : 211 : appendPQExpBufferStr(query,
5247 : : " s.subfailover,\n");
5248 : : else
5249 : 0 : appendPQExpBufferStr(query,
5250 : : " false AS subfailover,\n");
5251 : :
5252 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5253 : 211 : appendPQExpBufferStr(query,
5254 : : " s.subretaindeadtuples,\n");
5255 : : else
5256 : 0 : appendPQExpBufferStr(query,
5257 : : " false AS subretaindeadtuples,\n");
5258 : :
5259 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5260 : 211 : appendPQExpBufferStr(query,
5261 : : " s.submaxretention,\n");
5262 : : else
5263 : 0 : appendPQExpBufferStr(query, " 0 AS submaxretention,\n");
5264 : :
5265 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5266 : 211 : appendPQExpBufferStr(query,
5267 : : " s.subwalrcvtimeout,\n");
5268 : : else
5269 : 0 : appendPQExpBufferStr(query,
5270 : : " '-1' AS subwalrcvtimeout,\n");
5271 : :
5272 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5273 : 211 : appendPQExpBufferStr(query, " fs.srvname AS subservername,\n");
5274 : : else
5275 : 0 : appendPQExpBufferStr(query, " NULL AS subservername,\n");
5276 : :
5277 [ + - ]: 211 : if (fout->remoteVersion >= 200000)
5278 : 211 : appendPQExpBufferStr(query,
5279 : : " s.subconflictlogdest\n");
5280 : : else
5281 : 0 : appendPQExpBufferStr(query,
5282 : : " 'log' AS subconflictlogdest\n");
5283 : :
5284 : 211 : appendPQExpBufferStr(query,
5285 : : "FROM pg_subscription s\n");
5286 : :
5287 [ + - ]: 211 : if (fout->remoteVersion >= 190000)
5288 : 211 : appendPQExpBufferStr(query,
5289 : : "LEFT JOIN pg_catalog.pg_foreign_server fs \n"
5290 : : " ON fs.oid = s.subserver \n");
5291 : :
5292 [ + + + - ]: 211 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5293 : 44 : appendPQExpBufferStr(query,
5294 : : "LEFT JOIN pg_catalog.pg_replication_origin_status o \n"
5295 : : " ON o.external_id = 'pg_' || s.oid::text \n");
5296 : :
5297 : 211 : appendPQExpBufferStr(query,
5298 : : "WHERE s.subdbid = (SELECT oid FROM pg_database\n"
5299 : : " WHERE datname = current_database())");
5300 : :
5301 : 211 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5302 : :
5303 : 211 : ntups = PQntuples(res);
5304 : :
5305 : : /*
5306 : : * Get subscription fields. We don't include subskiplsn in the dump as
5307 : : * after restoring the dump this value may no longer be relevant.
5308 : : */
5309 : 211 : i_tableoid = PQfnumber(res, "tableoid");
5310 : 211 : i_oid = PQfnumber(res, "oid");
5311 : 211 : i_subname = PQfnumber(res, "subname");
5312 : 211 : i_subowner = PQfnumber(res, "subowner");
5313 : 211 : i_subenabled = PQfnumber(res, "subenabled");
5314 : 211 : i_subbinary = PQfnumber(res, "subbinary");
5315 : 211 : i_substream = PQfnumber(res, "substream");
5316 : 211 : i_subtwophasestate = PQfnumber(res, "subtwophasestate");
5317 : 211 : i_subdisableonerr = PQfnumber(res, "subdisableonerr");
5318 : 211 : i_subpasswordrequired = PQfnumber(res, "subpasswordrequired");
5319 : 211 : i_subrunasowner = PQfnumber(res, "subrunasowner");
5320 : 211 : i_subfailover = PQfnumber(res, "subfailover");
5321 : 211 : i_subretaindeadtuples = PQfnumber(res, "subretaindeadtuples");
5322 : 211 : i_submaxretention = PQfnumber(res, "submaxretention");
5323 : 211 : i_subservername = PQfnumber(res, "subservername");
5324 : 211 : i_subconninfo = PQfnumber(res, "subconninfo");
5325 : 211 : i_subslotname = PQfnumber(res, "subslotname");
5326 : 211 : i_subsynccommit = PQfnumber(res, "subsynccommit");
5327 : 211 : i_subwalrcvtimeout = PQfnumber(res, "subwalrcvtimeout");
5328 : 211 : i_subpublications = PQfnumber(res, "subpublications");
5329 : 211 : i_suborigin = PQfnumber(res, "suborigin");
5330 : 211 : i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn");
5331 : 211 : i_subconflictlogdest = PQfnumber(res, "subconflictlogdest");
5332 : :
5333 : 211 : subinfo = pg_malloc_array(SubscriptionInfo, ntups);
5334 : :
5335 [ + + ]: 396 : for (i = 0; i < ntups; i++)
5336 : : {
5337 : 185 : subinfo[i].dobj.objType = DO_SUBSCRIPTION;
5338 : 185 : subinfo[i].dobj.catId.tableoid =
5339 : 185 : atooid(PQgetvalue(res, i, i_tableoid));
5340 : 185 : subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5341 : 185 : AssignDumpId(&subinfo[i].dobj);
5342 : 185 : subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
5343 : 185 : subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner));
5344 : :
5345 : 185 : subinfo[i].subenabled =
5346 : 185 : (strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0);
5347 [ + - ]: 185 : if (PQgetisnull(res, i, i_subservername))
5348 : 185 : subinfo[i].subservername = NULL;
5349 : : else
5350 : 0 : subinfo[i].subservername = pg_strdup(PQgetvalue(res, i, i_subservername));
5351 : 185 : subinfo[i].subbinary =
5352 : 185 : (strcmp(PQgetvalue(res, i, i_subbinary), "t") == 0);
5353 : 185 : subinfo[i].substream = *(PQgetvalue(res, i, i_substream));
5354 : 185 : subinfo[i].subtwophasestate = *(PQgetvalue(res, i, i_subtwophasestate));
5355 : 185 : subinfo[i].subdisableonerr =
5356 : 185 : (strcmp(PQgetvalue(res, i, i_subdisableonerr), "t") == 0);
5357 : 185 : subinfo[i].subpasswordrequired =
5358 : 185 : (strcmp(PQgetvalue(res, i, i_subpasswordrequired), "t") == 0);
5359 : 185 : subinfo[i].subrunasowner =
5360 : 185 : (strcmp(PQgetvalue(res, i, i_subrunasowner), "t") == 0);
5361 : 185 : subinfo[i].subfailover =
5362 : 185 : (strcmp(PQgetvalue(res, i, i_subfailover), "t") == 0);
5363 : 185 : subinfo[i].subretaindeadtuples =
5364 : 185 : (strcmp(PQgetvalue(res, i, i_subretaindeadtuples), "t") == 0);
5365 : 185 : subinfo[i].submaxretention =
5366 : 185 : atoi(PQgetvalue(res, i, i_submaxretention));
5367 [ - + ]: 185 : if (PQgetisnull(res, i, i_subconninfo))
5368 : 0 : subinfo[i].subconninfo = NULL;
5369 : : else
5370 : 185 : subinfo[i].subconninfo =
5371 : 185 : pg_strdup(PQgetvalue(res, i, i_subconninfo));
5372 [ - + ]: 185 : if (PQgetisnull(res, i, i_subslotname))
5373 : 0 : subinfo[i].subslotname = NULL;
5374 : : else
5375 : 185 : subinfo[i].subslotname =
5376 : 185 : pg_strdup(PQgetvalue(res, i, i_subslotname));
5377 : 370 : subinfo[i].subsynccommit =
5378 : 185 : pg_strdup(PQgetvalue(res, i, i_subsynccommit));
5379 : 370 : subinfo[i].subwalrcvtimeout =
5380 : 185 : pg_strdup(PQgetvalue(res, i, i_subwalrcvtimeout));
5381 : 370 : subinfo[i].subpublications =
5382 : 185 : pg_strdup(PQgetvalue(res, i, i_subpublications));
5383 : 185 : subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin));
5384 [ + + ]: 185 : if (PQgetisnull(res, i, i_suboriginremotelsn))
5385 : 183 : subinfo[i].suboriginremotelsn = NULL;
5386 : : else
5387 : 2 : subinfo[i].suboriginremotelsn =
5388 : 2 : pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn));
5389 : :
5390 : 370 : subinfo[i].subconflictlogdest =
5391 : 185 : pg_strdup(PQgetvalue(res, i, i_subconflictlogdest));
5392 : :
5393 : : /* Decide whether we want to dump it */
5394 : 185 : selectDumpableObject(&(subinfo[i].dobj), fout);
5395 : : }
5396 : 211 : PQclear(res);
5397 : :
5398 : 211 : destroyPQExpBuffer(query);
5399 : : }
5400 : :
5401 : : /*
5402 : : * getSubscriptionRelations
5403 : : * Get information about subscription membership for dumpable relations. This
5404 : : * will be used only in binary-upgrade mode for PG17 or later versions.
5405 : : */
5406 : : void
5407 : 215 : getSubscriptionRelations(Archive *fout)
5408 : : {
5409 : 215 : DumpOptions *dopt = fout->dopt;
5410 : 215 : SubscriptionInfo *subinfo = NULL;
5411 : : SubRelInfo *subrinfo;
5412 : : PGresult *res;
5413 : : int i_srsubid;
5414 : : int i_srrelid;
5415 : : int i_srsubstate;
5416 : : int i_srsublsn;
5417 : : int ntups;
5418 : 215 : Oid last_srsubid = InvalidOid;
5419 : :
5420 [ + + + + ]: 215 : if (dopt->no_subscriptions || !dopt->binary_upgrade ||
5421 [ - + ]: 44 : fout->remoteVersion < 170000)
5422 : 171 : return;
5423 : :
5424 : 44 : res = ExecuteSqlQuery(fout,
5425 : : "SELECT srsubid, srrelid, srsubstate, srsublsn "
5426 : : "FROM pg_catalog.pg_subscription_rel "
5427 : : "ORDER BY srsubid",
5428 : : PGRES_TUPLES_OK);
5429 : 44 : ntups = PQntuples(res);
5430 [ + + ]: 44 : if (ntups == 0)
5431 : 43 : goto cleanup;
5432 : :
5433 : : /* Get pg_subscription_rel attributes */
5434 : 1 : i_srsubid = PQfnumber(res, "srsubid");
5435 : 1 : i_srrelid = PQfnumber(res, "srrelid");
5436 : 1 : i_srsubstate = PQfnumber(res, "srsubstate");
5437 : 1 : i_srsublsn = PQfnumber(res, "srsublsn");
5438 : :
5439 : 1 : subrinfo = pg_malloc_array(SubRelInfo, ntups);
5440 [ + + ]: 4 : for (int i = 0; i < ntups; i++)
5441 : : {
5442 : 3 : Oid cur_srsubid = atooid(PQgetvalue(res, i, i_srsubid));
5443 : 3 : Oid relid = atooid(PQgetvalue(res, i, i_srrelid));
5444 : : TableInfo *tblinfo;
5445 : :
5446 : : /*
5447 : : * If we switched to a new subscription, check if the subscription
5448 : : * exists.
5449 : : */
5450 [ + + ]: 3 : if (cur_srsubid != last_srsubid)
5451 : : {
5452 : 2 : subinfo = findSubscriptionByOid(cur_srsubid);
5453 [ - + ]: 2 : if (subinfo == NULL)
5454 : 0 : pg_fatal("subscription with OID %u does not exist", cur_srsubid);
5455 : :
5456 : 2 : last_srsubid = cur_srsubid;
5457 : : }
5458 : :
5459 : 3 : tblinfo = findTableByOid(relid);
5460 [ - + ]: 3 : if (tblinfo == NULL)
5461 : 0 : pg_fatal("failed sanity check, relation with OID %u not found",
5462 : : relid);
5463 : :
5464 : : /* OK, make a DumpableObject for this relationship */
5465 : 3 : subrinfo[i].dobj.objType = DO_SUBSCRIPTION_REL;
5466 : 3 : subrinfo[i].dobj.catId.tableoid = relid;
5467 : 3 : subrinfo[i].dobj.catId.oid = cur_srsubid;
5468 : 3 : AssignDumpId(&subrinfo[i].dobj);
5469 : 3 : subrinfo[i].dobj.namespace = tblinfo->dobj.namespace;
5470 : 3 : subrinfo[i].dobj.name = tblinfo->dobj.name;
5471 : 3 : subrinfo[i].subinfo = subinfo;
5472 : 3 : subrinfo[i].tblinfo = tblinfo;
5473 : 3 : subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0];
5474 [ + + ]: 3 : if (PQgetisnull(res, i, i_srsublsn))
5475 : 1 : subrinfo[i].srsublsn = NULL;
5476 : : else
5477 : 2 : subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn));
5478 : :
5479 : : /* Decide whether we want to dump it */
5480 : 3 : selectDumpableObject(&(subrinfo[i].dobj), fout);
5481 : : }
5482 : :
5483 : 1 : cleanup:
5484 : 44 : PQclear(res);
5485 : : }
5486 : :
5487 : : /*
5488 : : * dumpSubscriptionTable
5489 : : * Dump the definition of the given subscription table mapping. This will be
5490 : : * used only in binary-upgrade mode for PG17 or later versions.
5491 : : */
5492 : : static void
5493 : 3 : dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
5494 : : {
5495 : 3 : DumpOptions *dopt = fout->dopt;
5496 : 3 : SubscriptionInfo *subinfo = subrinfo->subinfo;
5497 : : PQExpBuffer query;
5498 : : char *tag;
5499 : :
5500 : : /* Do nothing if not dumping schema */
5501 [ - + ]: 3 : if (!dopt->dumpSchema)
5502 : 0 : return;
5503 : :
5504 : : Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000);
5505 : :
5506 : 3 : tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->tblinfo->dobj.name);
5507 : :
5508 : 3 : query = createPQExpBuffer();
5509 : :
5510 [ + - ]: 3 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5511 : : {
5512 : : /*
5513 : : * binary_upgrade_add_sub_rel_state will add the subscription relation
5514 : : * to pg_subscription_rel table. This will be used only in
5515 : : * binary-upgrade mode.
5516 : : */
5517 : 3 : appendPQExpBufferStr(query,
5518 : : "\n-- For binary upgrade, must preserve the subscriber table.\n");
5519 : 3 : appendPQExpBufferStr(query,
5520 : : "SELECT pg_catalog.binary_upgrade_add_sub_rel_state(");
5521 : 3 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5522 : 3 : appendPQExpBuffer(query,
5523 : : ", %u, '%c'",
5524 : 3 : subrinfo->tblinfo->dobj.catId.oid,
5525 : 3 : subrinfo->srsubstate);
5526 : :
5527 [ + + + - ]: 3 : if (subrinfo->srsublsn && subrinfo->srsublsn[0] != '\0')
5528 : 2 : appendPQExpBuffer(query, ", '%s'", subrinfo->srsublsn);
5529 : : else
5530 : 1 : appendPQExpBufferStr(query, ", NULL");
5531 : :
5532 : 3 : appendPQExpBufferStr(query, ");\n");
5533 : : }
5534 : :
5535 : : /*
5536 : : * There is no point in creating a drop query as the drop is done by table
5537 : : * drop. (If you think to change this, see also _printTocEntry().)
5538 : : * Although this object doesn't really have ownership as such, set the
5539 : : * owner field anyway to ensure that the command is run by the correct
5540 : : * role at restore time.
5541 : : */
5542 [ + - ]: 3 : if (subrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5543 : 3 : ArchiveEntry(fout, subrinfo->dobj.catId, subrinfo->dobj.dumpId,
5544 : 3 : ARCHIVE_OPTS(.tag = tag,
5545 : : .namespace = subrinfo->tblinfo->dobj.namespace->dobj.name,
5546 : : .owner = subinfo->rolname,
5547 : : .description = "SUBSCRIPTION TABLE",
5548 : : .section = SECTION_POST_DATA,
5549 : : .createStmt = query->data));
5550 : :
5551 : : /* These objects can't currently have comments or seclabels */
5552 : :
5553 : 3 : pfree(tag);
5554 : 3 : destroyPQExpBuffer(query);
5555 : : }
5556 : :
5557 : : /*
5558 : : * dumpSubscription
5559 : : * dump the definition of the given subscription
5560 : : */
5561 : : static void
5562 : 157 : dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
5563 : : {
5564 : 157 : DumpOptions *dopt = fout->dopt;
5565 : : PQExpBuffer delq;
5566 : : PQExpBuffer query;
5567 : : PQExpBuffer publications;
5568 : : char *qsubname;
5569 : 157 : char **pubnames = NULL;
5570 : 157 : int npubnames = 0;
5571 : : int i;
5572 : :
5573 : : /* Do nothing if not dumping schema */
5574 [ + + ]: 157 : if (!dopt->dumpSchema)
5575 : 24 : return;
5576 : :
5577 : 133 : delq = createPQExpBuffer();
5578 : 133 : query = createPQExpBuffer();
5579 : :
5580 : 133 : qsubname = pg_strdup(fmtId(subinfo->dobj.name));
5581 : :
5582 : 133 : appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n",
5583 : : qsubname);
5584 : :
5585 [ + + ]: 133 : if (dopt->binary_upgrade)
5586 : : {
5587 : 7 : appendPQExpBufferStr(query, "\n-- For binary upgrade, must preserve pg_subscription.oid\n");
5588 : 7 : appendPQExpBuffer(query,
5589 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_subscription_oid('%u'::pg_catalog.oid);\n\n",
5590 : 7 : subinfo->dobj.catId.oid);
5591 : : }
5592 : :
5593 : 133 : appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s ",
5594 : : qsubname);
5595 [ - + ]: 133 : if (subinfo->subservername)
5596 : : {
5597 : 0 : appendPQExpBuffer(query, "SERVER %s", fmtId(subinfo->subservername));
5598 : : }
5599 : : else
5600 : : {
5601 : 133 : appendPQExpBufferStr(query, "CONNECTION ");
5602 : 133 : appendStringLiteralAH(query, subinfo->subconninfo, fout);
5603 : : }
5604 : :
5605 : : /* Build list of quoted publications and append them to query. */
5606 [ - + ]: 133 : if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames))
5607 : 0 : pg_fatal("could not parse %s array", "subpublications");
5608 : :
5609 : 133 : publications = createPQExpBuffer();
5610 [ + + ]: 266 : for (i = 0; i < npubnames; i++)
5611 : : {
5612 [ - + ]: 133 : if (i > 0)
5613 : 0 : appendPQExpBufferStr(publications, ", ");
5614 : :
5615 : 133 : appendPQExpBufferStr(publications, fmtId(pubnames[i]));
5616 : : }
5617 : :
5618 : 133 : appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data);
5619 [ + - ]: 133 : if (subinfo->subslotname)
5620 : 133 : appendStringLiteralAH(query, subinfo->subslotname, fout);
5621 : : else
5622 : 0 : appendPQExpBufferStr(query, "NONE");
5623 : :
5624 [ - + ]: 133 : if (subinfo->subbinary)
5625 : 0 : appendPQExpBufferStr(query, ", binary = true");
5626 : :
5627 [ + + ]: 133 : if (subinfo->substream == LOGICALREP_STREAM_ON)
5628 : 32 : appendPQExpBufferStr(query, ", streaming = on");
5629 [ + + ]: 101 : else if (subinfo->substream == LOGICALREP_STREAM_PARALLEL)
5630 : 69 : appendPQExpBufferStr(query, ", streaming = parallel");
5631 : : else
5632 : 32 : appendPQExpBufferStr(query, ", streaming = off");
5633 : :
5634 [ - + ]: 133 : if (subinfo->subtwophasestate != LOGICALREP_TWOPHASE_STATE_DISABLED)
5635 : 0 : appendPQExpBufferStr(query, ", two_phase = on");
5636 : :
5637 [ - + ]: 133 : if (subinfo->subdisableonerr)
5638 : 0 : appendPQExpBufferStr(query, ", disable_on_error = true");
5639 : :
5640 [ - + ]: 133 : if (!subinfo->subpasswordrequired)
5641 : 0 : appendPQExpBufferStr(query, ", password_required = false");
5642 : :
5643 [ - + ]: 133 : if (subinfo->subrunasowner)
5644 : 0 : appendPQExpBufferStr(query, ", run_as_owner = true");
5645 : :
5646 [ + + ]: 133 : if (subinfo->subfailover)
5647 : 1 : appendPQExpBufferStr(query, ", failover = true");
5648 : :
5649 [ + + ]: 133 : if (subinfo->subretaindeadtuples)
5650 : 1 : appendPQExpBufferStr(query, ", retain_dead_tuples = true");
5651 : :
5652 [ - + ]: 133 : if (subinfo->submaxretention)
5653 : 0 : appendPQExpBuffer(query, ", max_retention_duration = %d", subinfo->submaxretention);
5654 : :
5655 [ - + ]: 133 : if (strcmp(subinfo->subsynccommit, "off") != 0)
5656 : 0 : appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));
5657 : :
5658 [ - + ]: 133 : if (strcmp(subinfo->subwalrcvtimeout, "-1") != 0)
5659 : 0 : appendPQExpBuffer(query, ", wal_receiver_timeout = %s", fmtId(subinfo->subwalrcvtimeout));
5660 : :
5661 [ + + ]: 133 : if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0)
5662 : 32 : appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin);
5663 : :
5664 [ + + ]: 133 : if (pg_strcasecmp(subinfo->subconflictlogdest, "log") != 0)
5665 : 66 : appendPQExpBuffer(query, ", conflict_log_destination = %s",
5666 : 66 : subinfo->subconflictlogdest);
5667 : :
5668 : 133 : appendPQExpBufferStr(query, ");\n");
5669 : :
5670 : : /*
5671 : : * In binary-upgrade mode, we allow the replication to continue after the
5672 : : * upgrade.
5673 : : */
5674 [ + + + - ]: 133 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5675 : : {
5676 [ + + ]: 7 : if (subinfo->suboriginremotelsn)
5677 : : {
5678 : : /*
5679 : : * Preserve the remote_lsn for the subscriber's replication
5680 : : * origin. This value is required to start the replication from
5681 : : * the position before the upgrade. This value will be stale if
5682 : : * the publisher gets upgraded before the subscriber node.
5683 : : * However, this shouldn't be a problem as the upgrade of the
5684 : : * publisher ensures that all the transactions were replicated
5685 : : * before upgrading it.
5686 : : */
5687 : 2 : appendPQExpBufferStr(query,
5688 : : "\n-- For binary upgrade, must preserve the remote_lsn for the subscriber's replication origin.\n");
5689 : 2 : appendPQExpBufferStr(query,
5690 : : "SELECT pg_catalog.binary_upgrade_replorigin_advance(");
5691 : 2 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5692 : 2 : appendPQExpBuffer(query, ", '%s');\n", subinfo->suboriginremotelsn);
5693 : : }
5694 : :
5695 [ + + ]: 7 : if (subinfo->subenabled)
5696 : : {
5697 : : /*
5698 : : * Enable the subscription to allow the replication to continue
5699 : : * after the upgrade.
5700 : : */
5701 : 2 : appendPQExpBufferStr(query,
5702 : : "\n-- For binary upgrade, must preserve the subscriber's running state.\n");
5703 : 2 : appendPQExpBuffer(query, "ALTER SUBSCRIPTION %s ENABLE;\n", qsubname);
5704 : : }
5705 : : }
5706 : :
5707 [ + - ]: 133 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5708 : 133 : ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
5709 : 133 : ARCHIVE_OPTS(.tag = subinfo->dobj.name,
5710 : : .owner = subinfo->rolname,
5711 : : .description = "SUBSCRIPTION",
5712 : : .section = SECTION_POST_DATA,
5713 : : .createStmt = query->data,
5714 : : .dropStmt = delq->data));
5715 : :
5716 [ + + ]: 133 : if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
5717 : 32 : dumpComment(fout, "SUBSCRIPTION", qsubname,
5718 : 32 : NULL, subinfo->rolname,
5719 : 32 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5720 : :
5721 [ - + ]: 133 : if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
5722 : 0 : dumpSecLabel(fout, "SUBSCRIPTION", qsubname,
5723 : 0 : NULL, subinfo->rolname,
5724 : 0 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5725 : :
5726 : 133 : destroyPQExpBuffer(publications);
5727 : 133 : free(pubnames);
5728 : :
5729 : 133 : destroyPQExpBuffer(delq);
5730 : 133 : destroyPQExpBuffer(query);
5731 : 133 : pg_free(qsubname);
5732 : : }
5733 : :
5734 : : /*
5735 : : * Given a "create query", append as many ALTER ... DEPENDS ON EXTENSION as
5736 : : * the object needs.
5737 : : */
5738 : : static void
5739 : 5334 : append_depends_on_extension(Archive *fout,
5740 : : PQExpBuffer create,
5741 : : const DumpableObject *dobj,
5742 : : const char *catalog,
5743 : : const char *keyword,
5744 : : const char *objname)
5745 : : {
5746 [ + + ]: 5334 : if (dobj->depends_on_ext)
5747 : : {
5748 : : char *nm;
5749 : : PGresult *res;
5750 : : PQExpBuffer query;
5751 : : int ntups;
5752 : : int i_extname;
5753 : : int i;
5754 : :
5755 : : /* dodge fmtId() non-reentrancy */
5756 : 42 : nm = pg_strdup(objname);
5757 : :
5758 : 42 : query = createPQExpBuffer();
5759 : 42 : appendPQExpBuffer(query,
5760 : : "SELECT e.extname "
5761 : : "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
5762 : : "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
5763 : : "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
5764 : : "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
5765 : : catalog,
5766 : 42 : dobj->catId.oid);
5767 : 42 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5768 : 42 : ntups = PQntuples(res);
5769 : 42 : i_extname = PQfnumber(res, "extname");
5770 [ + + ]: 84 : for (i = 0; i < ntups; i++)
5771 : : {
5772 : 42 : appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
5773 : : keyword, nm,
5774 : 42 : fmtId(PQgetvalue(res, i, i_extname)));
5775 : : }
5776 : :
5777 : 42 : PQclear(res);
5778 : 42 : destroyPQExpBuffer(query);
5779 : 42 : pg_free(nm);
5780 : : }
5781 : 5334 : }
5782 : :
5783 : : static Oid
5784 : 0 : get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
5785 : : {
5786 : : /*
5787 : : * If the old version didn't assign an array type, but the new version
5788 : : * does, we must select an unused type OID to assign. This currently only
5789 : : * happens for domains, when upgrading pre-v11 to v11 and up.
5790 : : *
5791 : : * Note: local state here is kind of ugly, but we must have some, since we
5792 : : * mustn't choose the same unused OID more than once.
5793 : : */
5794 : : static Oid next_possible_free_oid = FirstNormalObjectId;
5795 : : PGresult *res;
5796 : : bool is_dup;
5797 : :
5798 : : do
5799 : : {
5800 : 0 : ++next_possible_free_oid;
5801 : 0 : printfPQExpBuffer(upgrade_query,
5802 : : "SELECT EXISTS(SELECT 1 "
5803 : : "FROM pg_catalog.pg_type "
5804 : : "WHERE oid = '%u'::pg_catalog.oid);",
5805 : : next_possible_free_oid);
5806 : 0 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5807 : 0 : is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
5808 : 0 : PQclear(res);
5809 [ # # ]: 0 : } while (is_dup);
5810 : :
5811 : 0 : return next_possible_free_oid;
5812 : : }
5813 : :
5814 : : static void
5815 : 1015 : binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
5816 : : PQExpBuffer upgrade_buffer,
5817 : : Oid pg_type_oid,
5818 : : bool force_array_type,
5819 : : bool include_multirange_type)
5820 : : {
5821 : 1015 : PQExpBuffer upgrade_query = createPQExpBuffer();
5822 : : PGresult *res;
5823 : : Oid pg_type_array_oid;
5824 : : Oid pg_type_multirange_oid;
5825 : : Oid pg_type_multirange_array_oid;
5826 : : TypeInfo *tinfo;
5827 : :
5828 : 1015 : appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
5829 : 1015 : appendPQExpBuffer(upgrade_buffer,
5830 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5831 : : pg_type_oid);
5832 : :
5833 : 1015 : tinfo = findTypeByOid(pg_type_oid);
5834 [ + - ]: 1015 : if (tinfo)
5835 : 1015 : pg_type_array_oid = tinfo->typarray;
5836 : : else
5837 : 0 : pg_type_array_oid = InvalidOid;
5838 : :
5839 [ + + - + ]: 1015 : if (!OidIsValid(pg_type_array_oid) && force_array_type)
5840 : 0 : pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5841 : :
5842 [ + + ]: 1015 : if (OidIsValid(pg_type_array_oid))
5843 : : {
5844 : 1013 : appendPQExpBufferStr(upgrade_buffer,
5845 : : "\n-- For binary upgrade, must preserve pg_type array oid\n");
5846 : 1013 : appendPQExpBuffer(upgrade_buffer,
5847 : : "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5848 : : pg_type_array_oid);
5849 : : }
5850 : :
5851 : : /*
5852 : : * Pre-set the multirange type oid and its own array type oid.
5853 : : */
5854 [ + + ]: 1015 : if (include_multirange_type)
5855 : : {
5856 [ + - ]: 9 : if (fout->remoteVersion >= 140000)
5857 : : {
5858 : 9 : printfPQExpBuffer(upgrade_query,
5859 : : "SELECT t.oid, t.typarray "
5860 : : "FROM pg_catalog.pg_type t "
5861 : : "JOIN pg_catalog.pg_range r "
5862 : : "ON t.oid = r.rngmultitypid "
5863 : : "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
5864 : : pg_type_oid);
5865 : :
5866 : 9 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5867 : :
5868 : 9 : pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
5869 : 9 : pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5870 : :
5871 : 9 : PQclear(res);
5872 : : }
5873 : : else
5874 : : {
5875 : 0 : pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5876 : 0 : pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5877 : : }
5878 : :
5879 : 9 : appendPQExpBufferStr(upgrade_buffer,
5880 : : "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
5881 : 9 : appendPQExpBuffer(upgrade_buffer,
5882 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5883 : : pg_type_multirange_oid);
5884 : 9 : appendPQExpBufferStr(upgrade_buffer,
5885 : : "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
5886 : 9 : appendPQExpBuffer(upgrade_buffer,
5887 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5888 : : pg_type_multirange_array_oid);
5889 : : }
5890 : :
5891 : 1015 : destroyPQExpBuffer(upgrade_query);
5892 : 1015 : }
5893 : :
5894 : : static void
5895 : 929 : binary_upgrade_set_type_oids_by_rel(Archive *fout,
5896 : : PQExpBuffer upgrade_buffer,
5897 : : const TableInfo *tbinfo)
5898 : : {
5899 : 929 : Oid pg_type_oid = tbinfo->reltype;
5900 : :
5901 [ + - ]: 929 : if (OidIsValid(pg_type_oid))
5902 : 929 : binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5903 : : pg_type_oid, false, false);
5904 : 929 : }
5905 : :
5906 : : /*
5907 : : * bsearch() comparator for BinaryUpgradeClassOidItem
5908 : : */
5909 : : static int
5910 : 13328 : BinaryUpgradeClassOidItemCmp(const void *p1, const void *p2)
5911 : : {
5912 : 13328 : BinaryUpgradeClassOidItem v1 = *((const BinaryUpgradeClassOidItem *) p1);
5913 : 13328 : BinaryUpgradeClassOidItem v2 = *((const BinaryUpgradeClassOidItem *) p2);
5914 : :
5915 : 13328 : return pg_cmp_u32(v1.oid, v2.oid);
5916 : : }
5917 : :
5918 : : /*
5919 : : * collectBinaryUpgradeClassOids
5920 : : *
5921 : : * Construct a table of pg_class information required for
5922 : : * binary_upgrade_set_pg_class_oids(). The table is sorted by OID for speed in
5923 : : * lookup.
5924 : : */
5925 : : static void
5926 : 44 : collectBinaryUpgradeClassOids(Archive *fout)
5927 : : {
5928 : : PGresult *res;
5929 : : const char *query;
5930 : :
5931 : 44 : query = "SELECT c.oid, c.relkind, c.relfilenode, c.reltoastrelid, "
5932 : : "ct.relfilenode, i.indexrelid, cti.relfilenode, "
5933 : : "(SELECT a.atttypid FROM pg_attribute AS a "
5934 : : " WHERE a.attrelid = c.reltoastrelid AND attname = 'chunk_id'::text) "
5935 : : " AS toastchunktypid "
5936 : : "FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index i "
5937 : : "ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5938 : : "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5939 : : "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5940 : : "ORDER BY c.oid;";
5941 : :
5942 : 44 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
5943 : :
5944 : 44 : nbinaryUpgradeClassOids = PQntuples(res);
5945 : 44 : binaryUpgradeClassOids =
5946 : 44 : pg_malloc_array(BinaryUpgradeClassOidItem, nbinaryUpgradeClassOids);
5947 : :
5948 [ + + ]: 20651 : for (int i = 0; i < nbinaryUpgradeClassOids; i++)
5949 : : {
5950 : 20607 : binaryUpgradeClassOids[i].oid = atooid(PQgetvalue(res, i, 0));
5951 : 20607 : binaryUpgradeClassOids[i].relkind = *PQgetvalue(res, i, 1);
5952 : 20607 : binaryUpgradeClassOids[i].relfilenumber = atooid(PQgetvalue(res, i, 2));
5953 : 20607 : binaryUpgradeClassOids[i].toast_oid = atooid(PQgetvalue(res, i, 3));
5954 : 20607 : binaryUpgradeClassOids[i].toast_relfilenumber = atooid(PQgetvalue(res, i, 4));
5955 : 20607 : binaryUpgradeClassOids[i].toast_index_oid = atooid(PQgetvalue(res, i, 5));
5956 : 20607 : binaryUpgradeClassOids[i].toast_index_relfilenumber = atooid(PQgetvalue(res, i, 6));
5957 : 20607 : binaryUpgradeClassOids[i].toast_chunk_id_typoid = atooid(PQgetvalue(res, i, 7));
5958 : : }
5959 : :
5960 : 44 : PQclear(res);
5961 : 44 : }
5962 : :
5963 : : static void
5964 : 1347 : binary_upgrade_set_pg_class_oids(Archive *fout,
5965 : : PQExpBuffer upgrade_buffer, Oid pg_class_oid)
5966 : : {
5967 : 1347 : BinaryUpgradeClassOidItem key = {0};
5968 : : BinaryUpgradeClassOidItem *entry;
5969 : :
5970 : : Assert(binaryUpgradeClassOids);
5971 : :
5972 : : /*
5973 : : * Preserve the OID and relfilenumber of the table, table's index, table's
5974 : : * toast table, toast table's chunk type and toast table's index if any.
5975 : : *
5976 : : * One complexity is that the current table definition might not require
5977 : : * the creation of a TOAST table, but the old database might have a TOAST
5978 : : * table that was created earlier, before some wide columns were dropped.
5979 : : * By setting the TOAST oid we force creation of the TOAST heap and index
5980 : : * by the new backend, so we can copy the files during binary upgrade
5981 : : * without worrying about this case.
5982 : : */
5983 : 1347 : key.oid = pg_class_oid;
5984 : 1347 : entry = bsearch(&key, binaryUpgradeClassOids, nbinaryUpgradeClassOids,
5985 : : sizeof(BinaryUpgradeClassOidItem),
5986 : : BinaryUpgradeClassOidItemCmp);
5987 : :
5988 : 1347 : appendPQExpBufferStr(upgrade_buffer,
5989 : : "\n-- For binary upgrade, must preserve pg_class oids, toast chunk type oids and relfilenodes\n");
5990 : :
5991 [ + + ]: 1347 : if (entry->relkind != RELKIND_INDEX &&
5992 [ + + ]: 1052 : entry->relkind != RELKIND_PARTITIONED_INDEX)
5993 : : {
5994 : 1021 : appendPQExpBuffer(upgrade_buffer,
5995 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5996 : : pg_class_oid);
5997 : :
5998 : : /*
5999 : : * Not every relation has storage. Also, in a pre-v12 database,
6000 : : * partitioned tables have a relfilenumber, which should not be
6001 : : * preserved when upgrading.
6002 : : */
6003 [ + + ]: 1021 : if (RelFileNumberIsValid(entry->relfilenumber) &&
6004 [ + - ]: 840 : entry->relkind != RELKIND_PARTITIONED_TABLE)
6005 : 840 : appendPQExpBuffer(upgrade_buffer,
6006 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
6007 : : entry->relfilenumber);
6008 : :
6009 : : /*
6010 : : * In a pre-v12 database, partitioned tables might be marked as having
6011 : : * toast tables, but we should ignore them if so.
6012 : : */
6013 [ + + ]: 1021 : if (OidIsValid(entry->toast_oid) &&
6014 [ + - ]: 302 : entry->relkind != RELKIND_PARTITIONED_TABLE)
6015 : : {
6016 : 302 : appendPQExpBuffer(upgrade_buffer,
6017 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
6018 : : entry->toast_oid);
6019 : 302 : appendPQExpBuffer(upgrade_buffer,
6020 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
6021 : : entry->toast_relfilenumber);
6022 : 302 : appendPQExpBuffer(upgrade_buffer,
6023 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_chunk_id_typoid('%u'::pg_catalog.oid);\n",
6024 : : entry->toast_chunk_id_typoid);
6025 : :
6026 : : /* every toast table has an index */
6027 : 302 : appendPQExpBuffer(upgrade_buffer,
6028 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
6029 : : entry->toast_index_oid);
6030 : 302 : appendPQExpBuffer(upgrade_buffer,
6031 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
6032 : : entry->toast_index_relfilenumber);
6033 : : }
6034 : : }
6035 : : else
6036 : : {
6037 : : /* Preserve the OID and relfilenumber of the index */
6038 : 326 : appendPQExpBuffer(upgrade_buffer,
6039 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
6040 : : pg_class_oid);
6041 : 326 : appendPQExpBuffer(upgrade_buffer,
6042 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
6043 : : entry->relfilenumber);
6044 : : }
6045 : :
6046 : 1347 : appendPQExpBufferChar(upgrade_buffer, '\n');
6047 : 1347 : }
6048 : :
6049 : : /*
6050 : : * Emit SQL to pre-set the attribute versions for the next relation to be
6051 : : * created.
6052 : : *
6053 : : * attversions[j] is the version of attribute j+1, or InvalidXLogRecPtr if it
6054 : : * was unversioned in the source cluster. This needs to include dropped
6055 : : * attributes.
6056 : : *
6057 : : * The version has to be set as part of the CREATE itself, rather than be
6058 : : * fixed up afterwards, as some CREATE statements can contain expressions that
6059 : : * can embed a row type constant of the relation being created. If the
6060 : : * attribute versions were to be updated subsequently, the embedded datums
6061 : : * would be considered "out-of-date" and throw errors on use.
6062 : : */
6063 : : static void
6064 : 955 : binary_upgrade_set_next_attribute_versions(PQExpBuffer upgrade_buffer,
6065 : : int numatts,
6066 : : const uint64 *attversions)
6067 : : {
6068 : 955 : appendPQExpBufferStr(upgrade_buffer,
6069 : : "\n-- For binary upgrade, must preserve the attribute versions\n"
6070 : : "SELECT pg_catalog.binary_upgrade_set_next_attribute_versions('{");
6071 : :
6072 [ + + ]: 4509 : for (int j = 0; j < numatts; j++)
6073 : 3554 : appendPQExpBuffer(upgrade_buffer, "%s%X/%X",
6074 : : j > 0 ? "," : "",
6075 [ + + ]: 3554 : LSN_FORMAT_ARGS(attversions[j]));
6076 : :
6077 : 955 : appendPQExpBufferStr(upgrade_buffer, "}'::pg_catalog.pg_lsn[]);\n\n");
6078 : 955 : }
6079 : :
6080 : : /*
6081 : : * If the DumpableObject is a member of an extension, add a suitable
6082 : : * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
6083 : : *
6084 : : * For somewhat historical reasons, objname should already be quoted,
6085 : : * but not objnamespace (if any).
6086 : : */
6087 : : static void
6088 : 1648 : binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
6089 : : const DumpableObject *dobj,
6090 : : const char *objtype,
6091 : : const char *objname,
6092 : : const char *objnamespace)
6093 : : {
6094 : 1648 : DumpableObject *extobj = NULL;
6095 : : int i;
6096 : :
6097 [ + + ]: 1648 : if (!dobj->ext_member)
6098 : 1626 : return;
6099 : :
6100 : : /*
6101 : : * Find the parent extension. We could avoid this search if we wanted to
6102 : : * add a link field to DumpableObject, but the space costs of that would
6103 : : * be considerable. We assume that member objects could only have a
6104 : : * direct dependency on their own extension, not any others.
6105 : : */
6106 [ + - ]: 22 : for (i = 0; i < dobj->nDeps; i++)
6107 : : {
6108 : 22 : extobj = findObjectByDumpId(dobj->dependencies[i]);
6109 [ + - + - ]: 22 : if (extobj && extobj->objType == DO_EXTENSION)
6110 : 22 : break;
6111 : 0 : extobj = NULL;
6112 : : }
6113 [ - + ]: 22 : if (extobj == NULL)
6114 : 0 : pg_fatal("could not find parent extension for %s %s",
6115 : : objtype, objname);
6116 : :
6117 : 22 : appendPQExpBufferStr(upgrade_buffer,
6118 : : "\n-- For binary upgrade, handle extension membership the hard way\n");
6119 : 22 : appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
6120 : 22 : fmtId(extobj->name),
6121 : : objtype);
6122 [ + + + - ]: 22 : if (objnamespace && *objnamespace)
6123 : 19 : appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
6124 : 22 : appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
6125 : : }
6126 : :
6127 : : /*
6128 : : * getNamespaces:
6129 : : * get information about all namespaces in the system catalogs
6130 : : */
6131 : : void
6132 : 216 : getNamespaces(Archive *fout)
6133 : : {
6134 : : PGresult *res;
6135 : : int ntups;
6136 : : int i;
6137 : : PQExpBuffer query;
6138 : : NamespaceInfo *nsinfo;
6139 : : int i_tableoid;
6140 : : int i_oid;
6141 : : int i_nspname;
6142 : : int i_nspowner;
6143 : : int i_nspacl;
6144 : : int i_acldefault;
6145 : :
6146 : 216 : query = createPQExpBuffer();
6147 : :
6148 : : /*
6149 : : * we fetch all namespaces including system ones, so that every object we
6150 : : * read in can be linked to a containing namespace.
6151 : : */
6152 : 216 : appendPQExpBufferStr(query, "SELECT n.tableoid, n.oid, n.nspname, "
6153 : : "n.nspowner, "
6154 : : "n.nspacl, "
6155 : : "acldefault('n', n.nspowner) AS acldefault "
6156 : : "FROM pg_namespace n");
6157 : :
6158 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6159 : :
6160 : 216 : ntups = PQntuples(res);
6161 : :
6162 : 216 : nsinfo = pg_malloc_array(NamespaceInfo, ntups);
6163 : :
6164 : 216 : i_tableoid = PQfnumber(res, "tableoid");
6165 : 216 : i_oid = PQfnumber(res, "oid");
6166 : 216 : i_nspname = PQfnumber(res, "nspname");
6167 : 216 : i_nspowner = PQfnumber(res, "nspowner");
6168 : 216 : i_nspacl = PQfnumber(res, "nspacl");
6169 : 216 : i_acldefault = PQfnumber(res, "acldefault");
6170 : :
6171 [ + + ]: 1988 : for (i = 0; i < ntups; i++)
6172 : : {
6173 : : const char *nspowner;
6174 : :
6175 : 1772 : nsinfo[i].dobj.objType = DO_NAMESPACE;
6176 : 1772 : nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6177 : 1772 : nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6178 : 1772 : AssignDumpId(&nsinfo[i].dobj);
6179 : 1772 : nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
6180 : 1772 : nsinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_nspacl));
6181 : 1772 : nsinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6182 : 1772 : nsinfo[i].dacl.privtype = 0;
6183 : 1772 : nsinfo[i].dacl.initprivs = NULL;
6184 : 1772 : nspowner = PQgetvalue(res, i, i_nspowner);
6185 : 1772 : nsinfo[i].nspowner = atooid(nspowner);
6186 : 1772 : nsinfo[i].rolname = getRoleName(nspowner);
6187 : :
6188 : : /* Decide whether to dump this namespace */
6189 : 1772 : selectDumpableNamespace(&nsinfo[i], fout);
6190 : :
6191 : : /* Mark whether namespace has an ACL */
6192 [ + + ]: 1772 : if (!PQgetisnull(res, i, i_nspacl))
6193 : 929 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6194 : :
6195 : : /*
6196 : : * We ignore any pg_init_privs.initprivs entry for the public schema
6197 : : * and assume a predetermined default, for several reasons. First,
6198 : : * dropping and recreating the schema removes its pg_init_privs entry,
6199 : : * but an empty destination database starts with this ACL nonetheless.
6200 : : * Second, we support dump/reload of public schema ownership changes.
6201 : : * ALTER SCHEMA OWNER filters nspacl through aclnewowner(), but
6202 : : * initprivs continues to reflect the initial owner. Hence,
6203 : : * synthesize the value that nspacl will have after the restore's
6204 : : * ALTER SCHEMA OWNER. Third, this makes the destination database
6205 : : * match the source's ACL, even if the latter was an initdb-default
6206 : : * ACL, which changed in v15. An upgrade pulls in changes to most
6207 : : * system object ACLs that the DBA had not customized. We've made the
6208 : : * public schema depart from that, because changing its ACL so easily
6209 : : * breaks applications.
6210 : : */
6211 [ + + ]: 1772 : if (strcmp(nsinfo[i].dobj.name, "public") == 0)
6212 : : {
6213 : 212 : PQExpBuffer aclarray = createPQExpBuffer();
6214 : 212 : PQExpBuffer aclitem = createPQExpBuffer();
6215 : :
6216 : : /* Standard ACL as of v15 is {owner=UC/owner,=U/owner} */
6217 : 212 : appendPQExpBufferChar(aclarray, '{');
6218 : 212 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6219 : 212 : appendPQExpBufferStr(aclitem, "=UC/");
6220 : 212 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6221 : 212 : appendPGArray(aclarray, aclitem->data);
6222 : 212 : resetPQExpBuffer(aclitem);
6223 : 212 : appendPQExpBufferStr(aclitem, "=U/");
6224 : 212 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6225 : 212 : appendPGArray(aclarray, aclitem->data);
6226 : 212 : appendPQExpBufferChar(aclarray, '}');
6227 : :
6228 : 212 : nsinfo[i].dacl.privtype = 'i';
6229 : 212 : nsinfo[i].dacl.initprivs = pstrdup(aclarray->data);
6230 : 212 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6231 : :
6232 : 212 : destroyPQExpBuffer(aclarray);
6233 : 212 : destroyPQExpBuffer(aclitem);
6234 : : }
6235 : : }
6236 : :
6237 : 216 : PQclear(res);
6238 : 216 : destroyPQExpBuffer(query);
6239 : 216 : }
6240 : :
6241 : : /*
6242 : : * findNamespace:
6243 : : * given a namespace OID, look up the info read by getNamespaces
6244 : : */
6245 : : static NamespaceInfo *
6246 : 691273 : findNamespace(Oid nsoid)
6247 : : {
6248 : : NamespaceInfo *nsinfo;
6249 : :
6250 : 691273 : nsinfo = findNamespaceByOid(nsoid);
6251 [ - + ]: 691273 : if (nsinfo == NULL)
6252 : 0 : pg_fatal("schema with OID %u does not exist", nsoid);
6253 : 691273 : return nsinfo;
6254 : : }
6255 : :
6256 : : /*
6257 : : * getExtensions:
6258 : : * read all extensions in the system catalogs and return them in the
6259 : : * ExtensionInfo* structure
6260 : : *
6261 : : * numExtensions is set to the number of extensions read in
6262 : : */
6263 : : ExtensionInfo *
6264 : 216 : getExtensions(Archive *fout, int *numExtensions)
6265 : : {
6266 : 216 : DumpOptions *dopt = fout->dopt;
6267 : : PGresult *res;
6268 : : int ntups;
6269 : : int i;
6270 : : PQExpBuffer query;
6271 : 216 : ExtensionInfo *extinfo = NULL;
6272 : : int i_tableoid;
6273 : : int i_oid;
6274 : : int i_extname;
6275 : : int i_nspname;
6276 : : int i_extrelocatable;
6277 : : int i_extversion;
6278 : : int i_extconfig;
6279 : : int i_extcondition;
6280 : :
6281 : 216 : query = createPQExpBuffer();
6282 : :
6283 : 216 : appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, "
6284 : : "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
6285 : : "FROM pg_extension x "
6286 : : "JOIN pg_namespace n ON n.oid = x.extnamespace");
6287 : :
6288 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6289 : :
6290 : 216 : ntups = PQntuples(res);
6291 [ - + ]: 216 : if (ntups == 0)
6292 : 0 : goto cleanup;
6293 : :
6294 : 216 : extinfo = pg_malloc_array(ExtensionInfo, ntups);
6295 : :
6296 : 216 : i_tableoid = PQfnumber(res, "tableoid");
6297 : 216 : i_oid = PQfnumber(res, "oid");
6298 : 216 : i_extname = PQfnumber(res, "extname");
6299 : 216 : i_nspname = PQfnumber(res, "nspname");
6300 : 216 : i_extrelocatable = PQfnumber(res, "extrelocatable");
6301 : 216 : i_extversion = PQfnumber(res, "extversion");
6302 : 216 : i_extconfig = PQfnumber(res, "extconfig");
6303 : 216 : i_extcondition = PQfnumber(res, "extcondition");
6304 : :
6305 [ + + ]: 463 : for (i = 0; i < ntups; i++)
6306 : : {
6307 : 247 : extinfo[i].dobj.objType = DO_EXTENSION;
6308 : 247 : extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6309 : 247 : extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6310 : 247 : AssignDumpId(&extinfo[i].dobj);
6311 : 247 : extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
6312 : 247 : extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
6313 : 247 : extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
6314 : 247 : extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
6315 : 247 : extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
6316 : 247 : extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
6317 : :
6318 : : /* Decide whether we want to dump it */
6319 : 247 : selectDumpableExtension(&(extinfo[i]), dopt);
6320 : : }
6321 : :
6322 : 216 : cleanup:
6323 : 216 : PQclear(res);
6324 : 216 : destroyPQExpBuffer(query);
6325 : :
6326 : 216 : *numExtensions = ntups;
6327 : :
6328 : 216 : return extinfo;
6329 : : }
6330 : :
6331 : : /*
6332 : : * getTypes:
6333 : : * get information about all types in the system catalogs
6334 : : *
6335 : : * NB: this must run after getFuncs() because we assume we can do
6336 : : * findFuncByOid().
6337 : : */
6338 : : void
6339 : 215 : getTypes(Archive *fout)
6340 : : {
6341 : : PGresult *res;
6342 : : int ntups;
6343 : : int i;
6344 : 215 : PQExpBuffer query = createPQExpBuffer();
6345 : : TypeInfo *tyinfo;
6346 : : ShellTypeInfo *stinfo;
6347 : : int i_tableoid;
6348 : : int i_oid;
6349 : : int i_typname;
6350 : : int i_typnamespace;
6351 : : int i_typacl;
6352 : : int i_acldefault;
6353 : : int i_typowner;
6354 : : int i_typelem;
6355 : : int i_typrelid;
6356 : : int i_typrelkind;
6357 : : int i_typtype;
6358 : : int i_typisdefined;
6359 : : int i_isarray;
6360 : : int i_typarray;
6361 : :
6362 : : /*
6363 : : * we include even the built-in types because those may be used as array
6364 : : * elements by user-defined types
6365 : : *
6366 : : * we filter out the built-in types when we dump out the types
6367 : : *
6368 : : * same approach for undefined (shell) types and array types
6369 : : *
6370 : : * Note: as of 8.3 we can reliably detect whether a type is an
6371 : : * auto-generated array type by checking the element type's typarray.
6372 : : * (Before that the test is capable of generating false positives.) We
6373 : : * still check for name beginning with '_', though, so as to avoid the
6374 : : * cost of the subselect probe for all standard types. This would have to
6375 : : * be revisited if the backend ever allows renaming of array types.
6376 : : */
6377 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, typname, "
6378 : : "typnamespace, typacl, "
6379 : : "acldefault('T', typowner) AS acldefault, "
6380 : : "typowner, "
6381 : : "typelem, typrelid, typarray, "
6382 : : "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
6383 : : "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
6384 : : "typtype, typisdefined, "
6385 : : "typname[0] = '_' AND typelem != 0 AND "
6386 : : "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
6387 : : "FROM pg_type");
6388 : :
6389 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6390 : :
6391 : 215 : ntups = PQntuples(res);
6392 : :
6393 : 215 : tyinfo = pg_malloc_array(TypeInfo, ntups);
6394 : :
6395 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6396 : 215 : i_oid = PQfnumber(res, "oid");
6397 : 215 : i_typname = PQfnumber(res, "typname");
6398 : 215 : i_typnamespace = PQfnumber(res, "typnamespace");
6399 : 215 : i_typacl = PQfnumber(res, "typacl");
6400 : 215 : i_acldefault = PQfnumber(res, "acldefault");
6401 : 215 : i_typowner = PQfnumber(res, "typowner");
6402 : 215 : i_typelem = PQfnumber(res, "typelem");
6403 : 215 : i_typrelid = PQfnumber(res, "typrelid");
6404 : 215 : i_typrelkind = PQfnumber(res, "typrelkind");
6405 : 215 : i_typtype = PQfnumber(res, "typtype");
6406 : 215 : i_typisdefined = PQfnumber(res, "typisdefined");
6407 : 215 : i_isarray = PQfnumber(res, "isarray");
6408 : 215 : i_typarray = PQfnumber(res, "typarray");
6409 : :
6410 [ + + ]: 158199 : for (i = 0; i < ntups; i++)
6411 : : {
6412 : 157984 : tyinfo[i].dobj.objType = DO_TYPE;
6413 : 157984 : tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6414 : 157984 : tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6415 : 157984 : AssignDumpId(&tyinfo[i].dobj);
6416 : 157984 : tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
6417 : 315968 : tyinfo[i].dobj.namespace =
6418 : 157984 : findNamespace(atooid(PQgetvalue(res, i, i_typnamespace)));
6419 : 157984 : tyinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_typacl));
6420 : 157984 : tyinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6421 : 157984 : tyinfo[i].dacl.privtype = 0;
6422 : 157984 : tyinfo[i].dacl.initprivs = NULL;
6423 : 157984 : tyinfo[i].ftypname = NULL; /* may get filled later */
6424 : 157984 : tyinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_typowner));
6425 : 157984 : tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
6426 : 157984 : tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
6427 : 157984 : tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
6428 : 157984 : tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
6429 : 157984 : tyinfo[i].shellType = NULL;
6430 : :
6431 [ + + ]: 157984 : if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
6432 : 157929 : tyinfo[i].isDefined = true;
6433 : : else
6434 : 55 : tyinfo[i].isDefined = false;
6435 : :
6436 [ + + ]: 157984 : if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
6437 : 75847 : tyinfo[i].isArray = true;
6438 : : else
6439 : 82137 : tyinfo[i].isArray = false;
6440 : :
6441 : 157984 : tyinfo[i].typarray = atooid(PQgetvalue(res, i, i_typarray));
6442 : :
6443 [ + + ]: 157984 : if (tyinfo[i].typtype == TYPTYPE_MULTIRANGE)
6444 : 1435 : tyinfo[i].isMultirange = true;
6445 : : else
6446 : 156549 : tyinfo[i].isMultirange = false;
6447 : :
6448 : : /* Decide whether we want to dump it */
6449 : 157984 : selectDumpableType(&tyinfo[i], fout);
6450 : :
6451 : : /* Mark whether type has an ACL */
6452 [ + + ]: 157984 : if (!PQgetisnull(res, i, i_typacl))
6453 : 217 : tyinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6454 : :
6455 : : /*
6456 : : * If it's a domain, fetch info about its constraints, if any
6457 : : */
6458 : 157984 : tyinfo[i].nDomChecks = 0;
6459 : 157984 : tyinfo[i].domChecks = NULL;
6460 : 157984 : tyinfo[i].notnull = NULL;
6461 [ + + ]: 157984 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6462 [ + + ]: 16558 : tyinfo[i].typtype == TYPTYPE_DOMAIN)
6463 : 171 : getDomainConstraints(fout, &(tyinfo[i]));
6464 : :
6465 : : /*
6466 : : * If it's a base type, make a DumpableObject representing a shell
6467 : : * definition of the type. We will need to dump that ahead of the I/O
6468 : : * functions for the type. Similarly, range types need a shell
6469 : : * definition in case they have a canonicalize function.
6470 : : *
6471 : : * Note: the shell type doesn't have a catId. You might think it
6472 : : * should copy the base type's catId, but then it might capture the
6473 : : * pg_depend entries for the type, which we don't want.
6474 : : */
6475 [ + + ]: 157984 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6476 [ + + ]: 16558 : (tyinfo[i].typtype == TYPTYPE_BASE ||
6477 [ + + ]: 8061 : tyinfo[i].typtype == TYPTYPE_RANGE))
6478 : : {
6479 : 8632 : stinfo = pg_malloc_object(ShellTypeInfo);
6480 : 8632 : stinfo->dobj.objType = DO_SHELL_TYPE;
6481 : 8632 : stinfo->dobj.catId = nilCatalogId;
6482 : 8632 : AssignDumpId(&stinfo->dobj);
6483 : 8632 : stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
6484 : 8632 : stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
6485 : 8632 : stinfo->baseType = &(tyinfo[i]);
6486 : 8632 : tyinfo[i].shellType = stinfo;
6487 : :
6488 : : /*
6489 : : * Initially mark the shell type as not to be dumped. We'll only
6490 : : * dump it if the I/O or canonicalize functions need to be dumped;
6491 : : * this is taken care of while sorting dependencies.
6492 : : */
6493 : 8632 : stinfo->dobj.dump = DUMP_COMPONENT_NONE;
6494 : : }
6495 : : }
6496 : :
6497 : 215 : PQclear(res);
6498 : :
6499 : 215 : destroyPQExpBuffer(query);
6500 : 215 : }
6501 : :
6502 : : /*
6503 : : * getOperators:
6504 : : * get information about all operators in the system catalogs
6505 : : */
6506 : : void
6507 : 215 : getOperators(Archive *fout)
6508 : : {
6509 : : PGresult *res;
6510 : : int ntups;
6511 : : int i;
6512 : 215 : PQExpBuffer query = createPQExpBuffer();
6513 : : OprInfo *oprinfo;
6514 : : int i_tableoid;
6515 : : int i_oid;
6516 : : int i_oprname;
6517 : : int i_oprnamespace;
6518 : : int i_oprowner;
6519 : : int i_oprkind;
6520 : : int i_oprleft;
6521 : : int i_oprright;
6522 : : int i_oprcode;
6523 : :
6524 : : /*
6525 : : * find all operators, including builtin operators; we filter out
6526 : : * system-defined operators at dump-out time.
6527 : : */
6528 : :
6529 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, oprname, "
6530 : : "oprnamespace, "
6531 : : "oprowner, "
6532 : : "oprkind, "
6533 : : "oprleft, "
6534 : : "oprright, "
6535 : : "oprcode::oid AS oprcode "
6536 : : "FROM pg_operator");
6537 : :
6538 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6539 : :
6540 : 215 : ntups = PQntuples(res);
6541 : :
6542 : 215 : oprinfo = pg_malloc_array(OprInfo, ntups);
6543 : :
6544 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6545 : 215 : i_oid = PQfnumber(res, "oid");
6546 : 215 : i_oprname = PQfnumber(res, "oprname");
6547 : 215 : i_oprnamespace = PQfnumber(res, "oprnamespace");
6548 : 215 : i_oprowner = PQfnumber(res, "oprowner");
6549 : 215 : i_oprkind = PQfnumber(res, "oprkind");
6550 : 215 : i_oprleft = PQfnumber(res, "oprleft");
6551 : 215 : i_oprright = PQfnumber(res, "oprright");
6552 : 215 : i_oprcode = PQfnumber(res, "oprcode");
6553 : :
6554 [ + + ]: 173435 : for (i = 0; i < ntups; i++)
6555 : : {
6556 : 173220 : oprinfo[i].dobj.objType = DO_OPERATOR;
6557 : 173220 : oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6558 : 173220 : oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6559 : 173220 : AssignDumpId(&oprinfo[i].dobj);
6560 : 173220 : oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
6561 : 346440 : oprinfo[i].dobj.namespace =
6562 : 173220 : findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)));
6563 : 173220 : oprinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_oprowner));
6564 : 173220 : oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
6565 : 173220 : oprinfo[i].oprleft = atooid(PQgetvalue(res, i, i_oprleft));
6566 : 173220 : oprinfo[i].oprright = atooid(PQgetvalue(res, i, i_oprright));
6567 : 173220 : oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
6568 : :
6569 : : /* Decide whether we want to dump it */
6570 : 173220 : selectDumpableObject(&(oprinfo[i].dobj), fout);
6571 : : }
6572 : :
6573 : 215 : PQclear(res);
6574 : :
6575 : 215 : destroyPQExpBuffer(query);
6576 : 215 : }
6577 : :
6578 : : /*
6579 : : * getCollations:
6580 : : * get information about all collations in the system catalogs
6581 : : */
6582 : : void
6583 : 215 : getCollations(Archive *fout)
6584 : : {
6585 : : PGresult *res;
6586 : : int ntups;
6587 : : int i;
6588 : : PQExpBuffer query;
6589 : : CollInfo *collinfo;
6590 : : int i_tableoid;
6591 : : int i_oid;
6592 : : int i_collname;
6593 : : int i_collnamespace;
6594 : : int i_collowner;
6595 : : int i_collencoding;
6596 : :
6597 : 215 : query = createPQExpBuffer();
6598 : :
6599 : : /*
6600 : : * find all collations, including builtin collations; we filter out
6601 : : * system-defined collations at dump-out time.
6602 : : */
6603 : :
6604 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, collname, "
6605 : : "collnamespace, "
6606 : : "collowner, "
6607 : : "collencoding "
6608 : : "FROM pg_collation");
6609 : :
6610 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6611 : :
6612 : 215 : ntups = PQntuples(res);
6613 : :
6614 : 215 : collinfo = pg_malloc_array(CollInfo, ntups);
6615 : :
6616 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6617 : 215 : i_oid = PQfnumber(res, "oid");
6618 : 215 : i_collname = PQfnumber(res, "collname");
6619 : 215 : i_collnamespace = PQfnumber(res, "collnamespace");
6620 : 215 : i_collowner = PQfnumber(res, "collowner");
6621 : 215 : i_collencoding = PQfnumber(res, "collencoding");
6622 : :
6623 [ + + ]: 189532 : for (i = 0; i < ntups; i++)
6624 : : {
6625 : 189317 : collinfo[i].dobj.objType = DO_COLLATION;
6626 : 189317 : collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6627 : 189317 : collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6628 : 189317 : AssignDumpId(&collinfo[i].dobj);
6629 : 189317 : collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
6630 : 378634 : collinfo[i].dobj.namespace =
6631 : 189317 : findNamespace(atooid(PQgetvalue(res, i, i_collnamespace)));
6632 : 189317 : collinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_collowner));
6633 : 189317 : collinfo[i].collencoding = atoi(PQgetvalue(res, i, i_collencoding));
6634 : :
6635 : : /* Decide whether we want to dump it */
6636 : 189317 : selectDumpableObject(&(collinfo[i].dobj), fout);
6637 : : }
6638 : :
6639 : 215 : PQclear(res);
6640 : :
6641 : 215 : destroyPQExpBuffer(query);
6642 : 215 : }
6643 : :
6644 : : /*
6645 : : * getConversions:
6646 : : * get information about all conversions in the system catalogs
6647 : : */
6648 : : void
6649 : 215 : getConversions(Archive *fout)
6650 : : {
6651 : : PGresult *res;
6652 : : int ntups;
6653 : : int i;
6654 : : PQExpBuffer query;
6655 : : ConvInfo *convinfo;
6656 : : int i_tableoid;
6657 : : int i_oid;
6658 : : int i_conname;
6659 : : int i_connamespace;
6660 : : int i_conowner;
6661 : :
6662 : 215 : query = createPQExpBuffer();
6663 : :
6664 : : /*
6665 : : * find all conversions, including builtin conversions; we filter out
6666 : : * system-defined conversions at dump-out time.
6667 : : */
6668 : :
6669 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, conname, "
6670 : : "connamespace, "
6671 : : "conowner "
6672 : : "FROM pg_conversion");
6673 : :
6674 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6675 : :
6676 : 215 : ntups = PQntuples(res);
6677 : :
6678 : 215 : convinfo = pg_malloc_array(ConvInfo, ntups);
6679 : :
6680 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6681 : 215 : i_oid = PQfnumber(res, "oid");
6682 : 215 : i_conname = PQfnumber(res, "conname");
6683 : 215 : i_connamespace = PQfnumber(res, "connamespace");
6684 : 215 : i_conowner = PQfnumber(res, "conowner");
6685 : :
6686 [ + + ]: 21333 : for (i = 0; i < ntups; i++)
6687 : : {
6688 : 21118 : convinfo[i].dobj.objType = DO_CONVERSION;
6689 : 21118 : convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6690 : 21118 : convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6691 : 21118 : AssignDumpId(&convinfo[i].dobj);
6692 : 21118 : convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
6693 : 42236 : convinfo[i].dobj.namespace =
6694 : 21118 : findNamespace(atooid(PQgetvalue(res, i, i_connamespace)));
6695 : 21118 : convinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_conowner));
6696 : :
6697 : : /* Decide whether we want to dump it */
6698 : 21118 : selectDumpableObject(&(convinfo[i].dobj), fout);
6699 : : }
6700 : :
6701 : 215 : PQclear(res);
6702 : :
6703 : 215 : destroyPQExpBuffer(query);
6704 : 215 : }
6705 : :
6706 : : /*
6707 : : * getAccessMethods:
6708 : : * get information about all user-defined access methods
6709 : : */
6710 : : void
6711 : 215 : getAccessMethods(Archive *fout)
6712 : : {
6713 : : PGresult *res;
6714 : : int ntups;
6715 : : int i;
6716 : : PQExpBuffer query;
6717 : : AccessMethodInfo *aminfo;
6718 : : int i_tableoid;
6719 : : int i_oid;
6720 : : int i_amname;
6721 : : int i_amhandler;
6722 : : int i_amtype;
6723 : :
6724 : 215 : query = createPQExpBuffer();
6725 : :
6726 : : /*
6727 : : * Select all access methods from pg_am table.
6728 : : */
6729 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, ");
6730 : 215 : appendPQExpBufferStr(query,
6731 : : "amtype, "
6732 : : "amhandler::pg_catalog.regproc AS amhandler ");
6733 : 215 : appendPQExpBufferStr(query, "FROM pg_am");
6734 : :
6735 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6736 : :
6737 : 215 : ntups = PQntuples(res);
6738 : :
6739 : 215 : aminfo = pg_malloc_array(AccessMethodInfo, ntups);
6740 : :
6741 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6742 : 215 : i_oid = PQfnumber(res, "oid");
6743 : 215 : i_amname = PQfnumber(res, "amname");
6744 : 215 : i_amhandler = PQfnumber(res, "amhandler");
6745 : 215 : i_amtype = PQfnumber(res, "amtype");
6746 : :
6747 [ + + ]: 1848 : for (i = 0; i < ntups; i++)
6748 : : {
6749 : 1633 : aminfo[i].dobj.objType = DO_ACCESS_METHOD;
6750 : 1633 : aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6751 : 1633 : aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6752 : 1633 : AssignDumpId(&aminfo[i].dobj);
6753 : 1633 : aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname));
6754 : 1633 : aminfo[i].dobj.namespace = NULL;
6755 : 1633 : aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler));
6756 : 1633 : aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype));
6757 : :
6758 : : /* Decide whether we want to dump it */
6759 : 1633 : selectDumpableAccessMethod(&(aminfo[i]), fout);
6760 : : }
6761 : :
6762 : 215 : PQclear(res);
6763 : :
6764 : 215 : destroyPQExpBuffer(query);
6765 : 215 : }
6766 : :
6767 : :
6768 : : /*
6769 : : * getOpclasses:
6770 : : * get information about all opclasses in the system catalogs
6771 : : */
6772 : : void
6773 : 215 : getOpclasses(Archive *fout)
6774 : : {
6775 : : PGresult *res;
6776 : : int ntups;
6777 : : int i;
6778 : 215 : PQExpBuffer query = createPQExpBuffer();
6779 : : OpclassInfo *opcinfo;
6780 : : int i_tableoid;
6781 : : int i_oid;
6782 : : int i_opcmethod;
6783 : : int i_opcname;
6784 : : int i_opcnamespace;
6785 : : int i_opcowner;
6786 : :
6787 : : /*
6788 : : * find all opclasses, including builtin opclasses; we filter out
6789 : : * system-defined opclasses at dump-out time.
6790 : : */
6791 : :
6792 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opcmethod, opcname, "
6793 : : "opcnamespace, "
6794 : : "opcowner "
6795 : : "FROM pg_opclass");
6796 : :
6797 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6798 : :
6799 : 215 : ntups = PQntuples(res);
6800 : :
6801 : 215 : opcinfo = pg_malloc_array(OpclassInfo, ntups);
6802 : :
6803 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6804 : 215 : i_oid = PQfnumber(res, "oid");
6805 : 215 : i_opcmethod = PQfnumber(res, "opcmethod");
6806 : 215 : i_opcname = PQfnumber(res, "opcname");
6807 : 215 : i_opcnamespace = PQfnumber(res, "opcnamespace");
6808 : 215 : i_opcowner = PQfnumber(res, "opcowner");
6809 : :
6810 [ + + ]: 38865 : for (i = 0; i < ntups; i++)
6811 : : {
6812 : 38650 : opcinfo[i].dobj.objType = DO_OPCLASS;
6813 : 38650 : opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6814 : 38650 : opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6815 : 38650 : AssignDumpId(&opcinfo[i].dobj);
6816 : 38650 : opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
6817 : 77300 : opcinfo[i].dobj.namespace =
6818 : 38650 : findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace)));
6819 : 38650 : opcinfo[i].opcmethod = atooid(PQgetvalue(res, i, i_opcmethod));
6820 : 38650 : opcinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opcowner));
6821 : :
6822 : : /* Decide whether we want to dump it */
6823 : 38650 : selectDumpableObject(&(opcinfo[i].dobj), fout);
6824 : : }
6825 : :
6826 : 215 : PQclear(res);
6827 : :
6828 : 215 : destroyPQExpBuffer(query);
6829 : 215 : }
6830 : :
6831 : : /*
6832 : : * getOpfamilies:
6833 : : * get information about all opfamilies in the system catalogs
6834 : : */
6835 : : void
6836 : 215 : getOpfamilies(Archive *fout)
6837 : : {
6838 : : PGresult *res;
6839 : : int ntups;
6840 : : int i;
6841 : : PQExpBuffer query;
6842 : : OpfamilyInfo *opfinfo;
6843 : : int i_tableoid;
6844 : : int i_oid;
6845 : : int i_opfmethod;
6846 : : int i_opfname;
6847 : : int i_opfnamespace;
6848 : : int i_opfowner;
6849 : :
6850 : 215 : query = createPQExpBuffer();
6851 : :
6852 : : /*
6853 : : * find all opfamilies, including builtin opfamilies; we filter out
6854 : : * system-defined opfamilies at dump-out time.
6855 : : */
6856 : :
6857 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opfmethod, opfname, "
6858 : : "opfnamespace, "
6859 : : "opfowner "
6860 : : "FROM pg_opfamily");
6861 : :
6862 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6863 : :
6864 : 215 : ntups = PQntuples(res);
6865 : :
6866 : 215 : opfinfo = pg_malloc_array(OpfamilyInfo, ntups);
6867 : :
6868 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6869 : 215 : i_oid = PQfnumber(res, "oid");
6870 : 215 : i_opfname = PQfnumber(res, "opfname");
6871 : 215 : i_opfmethod = PQfnumber(res, "opfmethod");
6872 : 215 : i_opfnamespace = PQfnumber(res, "opfnamespace");
6873 : 215 : i_opfowner = PQfnumber(res, "opfowner");
6874 : :
6875 [ + + ]: 32180 : for (i = 0; i < ntups; i++)
6876 : : {
6877 : 31965 : opfinfo[i].dobj.objType = DO_OPFAMILY;
6878 : 31965 : opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6879 : 31965 : opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6880 : 31965 : AssignDumpId(&opfinfo[i].dobj);
6881 : 31965 : opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
6882 : 63930 : opfinfo[i].dobj.namespace =
6883 : 31965 : findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace)));
6884 : 31965 : opfinfo[i].opfmethod = atooid(PQgetvalue(res, i, i_opfmethod));
6885 : 31965 : opfinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opfowner));
6886 : :
6887 : : /* Decide whether we want to dump it */
6888 : 31965 : selectDumpableObject(&(opfinfo[i].dobj), fout);
6889 : : }
6890 : :
6891 : 215 : PQclear(res);
6892 : :
6893 : 215 : destroyPQExpBuffer(query);
6894 : 215 : }
6895 : :
6896 : : /*
6897 : : * getAggregates:
6898 : : * get information about all user-defined aggregates in the system catalogs
6899 : : */
6900 : : void
6901 : 215 : getAggregates(Archive *fout)
6902 : : {
6903 : 215 : DumpOptions *dopt = fout->dopt;
6904 : : PGresult *res;
6905 : : int ntups;
6906 : : int i;
6907 : 215 : PQExpBuffer query = createPQExpBuffer();
6908 : : AggInfo *agginfo;
6909 : : int i_tableoid;
6910 : : int i_oid;
6911 : : int i_aggname;
6912 : : int i_aggnamespace;
6913 : : int i_pronargs;
6914 : : int i_proargtypes;
6915 : : int i_proowner;
6916 : : int i_aggacl;
6917 : : int i_acldefault;
6918 : : const char *agg_check;
6919 : :
6920 : : /*
6921 : : * Find all interesting aggregates. See comment in getFuncs() for the
6922 : : * rationale behind the filtering logic.
6923 : : */
6924 : 430 : agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'"
6925 [ + - ]: 215 : : "p.proisagg");
6926 : :
6927 : 215 : appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, "
6928 : : "p.proname AS aggname, "
6929 : : "p.pronamespace AS aggnamespace, "
6930 : : "p.pronargs, p.proargtypes, "
6931 : : "p.proowner, "
6932 : : "p.proacl AS aggacl, "
6933 : : "acldefault('f', p.proowner) AS acldefault "
6934 : : "FROM pg_proc p "
6935 : : "LEFT JOIN pg_init_privs pip ON "
6936 : : "(p.oid = pip.objoid "
6937 : : "AND pip.classoid = 'pg_proc'::regclass "
6938 : : "AND pip.objsubid = 0) "
6939 : : "WHERE %s AND ("
6940 : : "p.pronamespace != "
6941 : : "(SELECT oid FROM pg_namespace "
6942 : : "WHERE nspname = 'pg_catalog') OR "
6943 : : "p.proacl IS DISTINCT FROM pip.initprivs",
6944 : : agg_check);
6945 [ + + ]: 215 : if (dopt->binary_upgrade)
6946 : 44 : appendPQExpBufferStr(query,
6947 : : " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
6948 : : "classid = 'pg_proc'::regclass AND "
6949 : : "objid = p.oid AND "
6950 : : "refclassid = 'pg_extension'::regclass AND "
6951 : : "deptype = 'e')");
6952 : 215 : appendPQExpBufferChar(query, ')');
6953 : :
6954 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6955 : :
6956 : 215 : ntups = PQntuples(res);
6957 : :
6958 : 215 : agginfo = pg_malloc_array(AggInfo, ntups);
6959 : :
6960 : 215 : i_tableoid = PQfnumber(res, "tableoid");
6961 : 215 : i_oid = PQfnumber(res, "oid");
6962 : 215 : i_aggname = PQfnumber(res, "aggname");
6963 : 215 : i_aggnamespace = PQfnumber(res, "aggnamespace");
6964 : 215 : i_pronargs = PQfnumber(res, "pronargs");
6965 : 215 : i_proargtypes = PQfnumber(res, "proargtypes");
6966 : 215 : i_proowner = PQfnumber(res, "proowner");
6967 : 215 : i_aggacl = PQfnumber(res, "aggacl");
6968 : 215 : i_acldefault = PQfnumber(res, "acldefault");
6969 : :
6970 [ + + ]: 617 : for (i = 0; i < ntups; i++)
6971 : : {
6972 : 402 : agginfo[i].aggfn.dobj.objType = DO_AGG;
6973 : 402 : agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6974 : 402 : agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6975 : 402 : AssignDumpId(&agginfo[i].aggfn.dobj);
6976 : 402 : agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
6977 : 804 : agginfo[i].aggfn.dobj.namespace =
6978 : 402 : findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace)));
6979 : 402 : agginfo[i].aggfn.dacl.acl = pg_strdup(PQgetvalue(res, i, i_aggacl));
6980 : 402 : agginfo[i].aggfn.dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6981 : 402 : agginfo[i].aggfn.dacl.privtype = 0;
6982 : 402 : agginfo[i].aggfn.dacl.initprivs = NULL;
6983 : 402 : agginfo[i].aggfn.rolname = getRoleName(PQgetvalue(res, i, i_proowner));
6984 : 402 : agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
6985 : 402 : agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */
6986 : 402 : agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs));
6987 [ + + ]: 402 : if (agginfo[i].aggfn.nargs == 0)
6988 : 56 : agginfo[i].aggfn.argtypes = NULL;
6989 : : else
6990 : 346 : agginfo[i].aggfn.argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
6991 : 346 : agginfo[i].aggfn.nargs);
6992 : 402 : agginfo[i].aggfn.postponed_def = false; /* might get set during sort */
6993 : :
6994 : : /* Decide whether we want to dump it */
6995 : 402 : selectDumpableObject(&(agginfo[i].aggfn.dobj), fout);
6996 : :
6997 : : /* Mark whether aggregate has an ACL */
6998 [ + + ]: 402 : if (!PQgetisnull(res, i, i_aggacl))
6999 : 25 : agginfo[i].aggfn.dobj.components |= DUMP_COMPONENT_ACL;
7000 : : }
7001 : :
7002 : 215 : PQclear(res);
7003 : :
7004 : 215 : destroyPQExpBuffer(query);
7005 : 215 : }
7006 : :
7007 : : /*
7008 : : * getFuncs:
7009 : : * get information about all user-defined functions in the system catalogs
7010 : : */
7011 : : void
7012 : 215 : getFuncs(Archive *fout)
7013 : : {
7014 : 215 : DumpOptions *dopt = fout->dopt;
7015 : : PGresult *res;
7016 : : int ntups;
7017 : : int i;
7018 : 215 : PQExpBuffer query = createPQExpBuffer();
7019 : : FuncInfo *finfo;
7020 : : int i_tableoid;
7021 : : int i_oid;
7022 : : int i_proname;
7023 : : int i_pronamespace;
7024 : : int i_proowner;
7025 : : int i_prolang;
7026 : : int i_pronargs;
7027 : : int i_proargtypes;
7028 : : int i_prorettype;
7029 : : int i_proacl;
7030 : : int i_acldefault;
7031 : : const char *not_agg_check;
7032 : :
7033 : : /*
7034 : : * Find all interesting functions. This is a bit complicated:
7035 : : *
7036 : : * 1. Always exclude aggregates; those are handled elsewhere.
7037 : : *
7038 : : * 2. Always exclude functions that are internally dependent on something
7039 : : * else, since presumably those will be created as a result of creating
7040 : : * the something else. This currently acts only to suppress constructor
7041 : : * functions for range types. Note this is OK only because the
7042 : : * constructors don't have any dependencies the range type doesn't have;
7043 : : * otherwise we might not get creation ordering correct.
7044 : : *
7045 : : * 3. Otherwise, we normally exclude functions in pg_catalog. However, if
7046 : : * they're members of extensions and we are in binary-upgrade mode then
7047 : : * include them, since we want to dump extension members individually in
7048 : : * that mode. Also, if they are used by casts or transforms then we need
7049 : : * to gather the information about them, though they won't be dumped if
7050 : : * they are built-in. Also, include functions in pg_catalog if they have
7051 : : * an ACL different from what's shown in pg_init_privs (so we have to join
7052 : : * to pg_init_privs; annoying).
7053 : : */
7054 : 430 : not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'"
7055 [ + - ]: 215 : : "NOT p.proisagg");
7056 : :
7057 : 215 : appendPQExpBuffer(query,
7058 : : "SELECT p.tableoid, p.oid, p.proname, p.prolang, "
7059 : : "p.pronargs, p.proargtypes, p.prorettype, "
7060 : : "p.proacl, "
7061 : : "acldefault('f', p.proowner) AS acldefault, "
7062 : : "p.pronamespace, "
7063 : : "p.proowner "
7064 : : "FROM pg_proc p "
7065 : : "LEFT JOIN pg_init_privs pip ON "
7066 : : "(p.oid = pip.objoid "
7067 : : "AND pip.classoid = 'pg_proc'::regclass "
7068 : : "AND pip.objsubid = 0) "
7069 : : "WHERE %s"
7070 : : "\n AND NOT EXISTS (SELECT 1 FROM pg_depend "
7071 : : "WHERE classid = 'pg_proc'::regclass AND "
7072 : : "objid = p.oid AND deptype = 'i')"
7073 : : "\n AND ("
7074 : : "\n pronamespace != "
7075 : : "(SELECT oid FROM pg_namespace "
7076 : : "WHERE nspname = 'pg_catalog')"
7077 : : "\n OR EXISTS (SELECT 1 FROM pg_cast"
7078 : : "\n WHERE pg_cast.oid > %u "
7079 : : "\n AND p.oid = pg_cast.castfunc)"
7080 : : "\n OR EXISTS (SELECT 1 FROM pg_transform"
7081 : : "\n WHERE pg_transform.oid > %u AND "
7082 : : "\n (p.oid = pg_transform.trffromsql"
7083 : : "\n OR p.oid = pg_transform.trftosql))",
7084 : : not_agg_check,
7085 : : g_last_builtin_oid,
7086 : : g_last_builtin_oid);
7087 [ + + ]: 215 : if (dopt->binary_upgrade)
7088 : 44 : appendPQExpBufferStr(query,
7089 : : "\n OR EXISTS(SELECT 1 FROM pg_depend WHERE "
7090 : : "classid = 'pg_proc'::regclass AND "
7091 : : "objid = p.oid AND "
7092 : : "refclassid = 'pg_extension'::regclass AND "
7093 : : "deptype = 'e')");
7094 : 215 : appendPQExpBufferStr(query,
7095 : : "\n OR p.proacl IS DISTINCT FROM pip.initprivs");
7096 : 215 : appendPQExpBufferChar(query, ')');
7097 : :
7098 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7099 : :
7100 : 215 : ntups = PQntuples(res);
7101 : :
7102 : 215 : finfo = pg_malloc0_array(FuncInfo, ntups);
7103 : :
7104 : 215 : i_tableoid = PQfnumber(res, "tableoid");
7105 : 215 : i_oid = PQfnumber(res, "oid");
7106 : 215 : i_proname = PQfnumber(res, "proname");
7107 : 215 : i_pronamespace = PQfnumber(res, "pronamespace");
7108 : 215 : i_proowner = PQfnumber(res, "proowner");
7109 : 215 : i_prolang = PQfnumber(res, "prolang");
7110 : 215 : i_pronargs = PQfnumber(res, "pronargs");
7111 : 215 : i_proargtypes = PQfnumber(res, "proargtypes");
7112 : 215 : i_prorettype = PQfnumber(res, "prorettype");
7113 : 215 : i_proacl = PQfnumber(res, "proacl");
7114 : 215 : i_acldefault = PQfnumber(res, "acldefault");
7115 : :
7116 [ + + ]: 5409 : for (i = 0; i < ntups; i++)
7117 : : {
7118 : 5194 : finfo[i].dobj.objType = DO_FUNC;
7119 : 5194 : finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7120 : 5194 : finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7121 : 5194 : AssignDumpId(&finfo[i].dobj);
7122 : 5194 : finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
7123 : 10388 : finfo[i].dobj.namespace =
7124 : 5194 : findNamespace(atooid(PQgetvalue(res, i, i_pronamespace)));
7125 : 5194 : finfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_proacl));
7126 : 5194 : finfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7127 : 5194 : finfo[i].dacl.privtype = 0;
7128 : 5194 : finfo[i].dacl.initprivs = NULL;
7129 : 5194 : finfo[i].rolname = getRoleName(PQgetvalue(res, i, i_proowner));
7130 : 5194 : finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
7131 : 5194 : finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
7132 : 5194 : finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
7133 [ + + ]: 5194 : if (finfo[i].nargs == 0)
7134 : 1104 : finfo[i].argtypes = NULL;
7135 : : else
7136 : 4090 : finfo[i].argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
7137 : 4090 : finfo[i].nargs);
7138 : 5194 : finfo[i].postponed_def = false; /* might get set during sort */
7139 : :
7140 : : /* Decide whether we want to dump it */
7141 : 5194 : selectDumpableObject(&(finfo[i].dobj), fout);
7142 : :
7143 : : /* Mark whether function has an ACL */
7144 [ + + ]: 5194 : if (!PQgetisnull(res, i, i_proacl))
7145 : 146 : finfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7146 : : }
7147 : :
7148 : 215 : PQclear(res);
7149 : :
7150 : 215 : destroyPQExpBuffer(query);
7151 : 215 : }
7152 : :
7153 : : /*
7154 : : * getRelationStatistics
7155 : : * register the statistics object as a dependent of the relation.
7156 : : *
7157 : : * reltuples is passed as a string to avoid complexities in converting from/to
7158 : : * floating point.
7159 : : */
7160 : : static RelStatsInfo *
7161 : 10480 : getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
7162 : : char *reltuples, int32 relallvisible,
7163 : : int32 relallfrozen, char relkind,
7164 : : char **indAttNames, int nindAttNames)
7165 : : {
7166 [ + + ]: 10480 : if (!fout->dopt->dumpStatistics)
7167 : 6428 : return NULL;
7168 : :
7169 [ + + + + ]: 4052 : if ((relkind == RELKIND_RELATION) ||
7170 [ + + ]: 1632 : (relkind == RELKIND_PARTITIONED_TABLE) ||
7171 [ + + ]: 956 : (relkind == RELKIND_INDEX) ||
7172 [ + + ]: 619 : (relkind == RELKIND_PARTITIONED_INDEX) ||
7173 [ + + ]: 288 : (relkind == RELKIND_MATVIEW ||
7174 : : relkind == RELKIND_FOREIGN_TABLE))
7175 : : {
7176 : 3800 : RelStatsInfo *info = pg_malloc0_object(RelStatsInfo);
7177 : 3800 : DumpableObject *dobj = &info->dobj;
7178 : :
7179 : 3800 : dobj->objType = DO_REL_STATS;
7180 : 3800 : dobj->catId.tableoid = 0;
7181 : 3800 : dobj->catId.oid = 0;
7182 : 3800 : AssignDumpId(dobj);
7183 : 3800 : dobj->dependencies = pg_malloc_object(DumpId);
7184 : 3800 : dobj->dependencies[0] = rel->dumpId;
7185 : 3800 : dobj->nDeps = 1;
7186 : 3800 : dobj->allocDeps = 1;
7187 : 3800 : dobj->components |= DUMP_COMPONENT_STATISTICS;
7188 : 3800 : dobj->name = pg_strdup(rel->name);
7189 : 3800 : dobj->namespace = rel->namespace;
7190 : 3800 : info->relid = rel->catId.oid;
7191 : 3800 : info->relpages = relpages;
7192 : 3800 : info->reltuples = pstrdup(reltuples);
7193 : 3800 : info->relallvisible = relallvisible;
7194 : 3800 : info->relallfrozen = relallfrozen;
7195 : 3800 : info->relkind = relkind;
7196 : 3800 : info->indAttNames = indAttNames;
7197 : 3800 : info->nindAttNames = nindAttNames;
7198 : :
7199 : : /*
7200 : : * Ordinarily, stats go in SECTION_DATA for tables and
7201 : : * SECTION_POST_DATA for indexes.
7202 : : *
7203 : : * However, the section may be updated later for materialized view
7204 : : * stats. REFRESH MATERIALIZED VIEW replaces the storage and resets
7205 : : * the stats, so the stats must be restored after the data. Also, the
7206 : : * materialized view definition may be postponed to SECTION_POST_DATA
7207 : : * (see repairMatViewBoundaryMultiLoop()).
7208 : : */
7209 [ + + - ]: 3800 : switch (info->relkind)
7210 : : {
7211 : 2787 : case RELKIND_RELATION:
7212 : : case RELKIND_PARTITIONED_TABLE:
7213 : : case RELKIND_MATVIEW:
7214 : : case RELKIND_FOREIGN_TABLE:
7215 : 2787 : info->section = SECTION_DATA;
7216 : 2787 : break;
7217 : 1013 : case RELKIND_INDEX:
7218 : : case RELKIND_PARTITIONED_INDEX:
7219 : 1013 : info->section = SECTION_POST_DATA;
7220 : 1013 : break;
7221 : 0 : default:
7222 : 0 : pg_fatal("cannot dump statistics for relation kind \"%c\"",
7223 : : info->relkind);
7224 : : }
7225 : :
7226 : 3800 : return info;
7227 : : }
7228 : 252 : return NULL;
7229 : : }
7230 : :
7231 : : /*
7232 : : * getTables
7233 : : * read all the tables (no indexes) in the system catalogs,
7234 : : * and return them as an array of TableInfo structures
7235 : : *
7236 : : * *numTables is set to the number of tables read in
7237 : : */
7238 : : TableInfo *
7239 : 216 : getTables(Archive *fout, int *numTables)
7240 : : {
7241 : 216 : DumpOptions *dopt = fout->dopt;
7242 : : PGresult *res;
7243 : : int ntups;
7244 : : int i;
7245 : 216 : PQExpBuffer query = createPQExpBuffer();
7246 : : TableInfo *tblinfo;
7247 : : int i_reltableoid;
7248 : : int i_reloid;
7249 : : int i_relname;
7250 : : int i_relnamespace;
7251 : : int i_relkind;
7252 : : int i_reltype;
7253 : : int i_relowner;
7254 : : int i_relchecks;
7255 : : int i_relhasindex;
7256 : : int i_relhasrules;
7257 : : int i_relpages;
7258 : : int i_reltuples;
7259 : : int i_relallvisible;
7260 : : int i_relallfrozen;
7261 : : int i_toastpages;
7262 : : int i_owning_tab;
7263 : : int i_owning_col;
7264 : : int i_reltablespace;
7265 : : int i_relhasoids;
7266 : : int i_relhastriggers;
7267 : : int i_relpersistence;
7268 : : int i_relispopulated;
7269 : : int i_relreplident;
7270 : : int i_relrowsec;
7271 : : int i_relforcerowsec;
7272 : : int i_relfrozenxid;
7273 : : int i_toastfrozenxid;
7274 : : int i_toastoid;
7275 : : int i_relminmxid;
7276 : : int i_toastminmxid;
7277 : : int i_reloptions;
7278 : : int i_checkoption;
7279 : : int i_toastreloptions;
7280 : : int i_reloftype;
7281 : : int i_foreignserver;
7282 : : int i_amname;
7283 : : int i_is_identity_sequence;
7284 : : int i_relacl;
7285 : : int i_acldefault;
7286 : : int i_ispartition;
7287 : :
7288 : : /*
7289 : : * Find all the tables and table-like objects.
7290 : : *
7291 : : * We must fetch all tables in this phase because otherwise we cannot
7292 : : * correctly identify inherited columns, owned sequences, etc.
7293 : : *
7294 : : * We include system catalogs, so that we can work if a user table is
7295 : : * defined to inherit from a system catalog (pretty weird, but...)
7296 : : *
7297 : : * Note: in this phase we should collect only a minimal amount of
7298 : : * information about each table, basically just enough to decide if it is
7299 : : * interesting. In particular, since we do not yet have lock on any user
7300 : : * table, we MUST NOT invoke any server-side data collection functions
7301 : : * (for instance, pg_get_partkeydef()). Those are likely to fail or give
7302 : : * wrong answers if any concurrent DDL is happening.
7303 : : */
7304 : :
7305 : 216 : appendPQExpBufferStr(query,
7306 : : "SELECT c.tableoid, c.oid, c.relname, "
7307 : : "c.relnamespace, c.relkind, c.reltype, "
7308 : : "c.relowner, "
7309 : : "c.relchecks, "
7310 : : "c.relhasindex, c.relhasrules, c.relpages, "
7311 : : "c.reltuples, c.relallvisible, ");
7312 : :
7313 [ + - ]: 216 : if (fout->remoteVersion >= 180000)
7314 : 216 : appendPQExpBufferStr(query, "c.relallfrozen, ");
7315 : : else
7316 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7317 : :
7318 : 216 : appendPQExpBufferStr(query,
7319 : : "c.relhastriggers, c.relpersistence, "
7320 : : "c.reloftype, "
7321 : : "c.relacl, "
7322 : : "acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
7323 : : " THEN 's'::\"char\" ELSE 'r'::\"char\" END, c.relowner) AS acldefault, "
7324 : : "CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN "
7325 : : "(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) "
7326 : : "ELSE 0 END AS foreignserver, "
7327 : : "c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, "
7328 : : "tc.oid AS toid, "
7329 : : "tc.relpages AS toastpages, "
7330 : : "tc.reloptions AS toast_reloptions, "
7331 : : "d.refobjid AS owning_tab, "
7332 : : "d.refobjsubid AS owning_col, "
7333 : : "tsp.spcname AS reltablespace, ");
7334 : :
7335 [ + - ]: 216 : if (fout->remoteVersion >= 120000)
7336 : 216 : appendPQExpBufferStr(query,
7337 : : "false AS relhasoids, ");
7338 : : else
7339 : 0 : appendPQExpBufferStr(query,
7340 : : "c.relhasoids, ");
7341 : :
7342 : 216 : appendPQExpBufferStr(query,
7343 : : "c.relispopulated, ");
7344 : :
7345 : 216 : appendPQExpBufferStr(query,
7346 : : "c.relreplident, ");
7347 : :
7348 : 216 : appendPQExpBufferStr(query,
7349 : : "c.relrowsecurity, c.relforcerowsecurity, ");
7350 : :
7351 : 216 : appendPQExpBufferStr(query,
7352 : : "c.relminmxid, tc.relminmxid AS tminmxid, ");
7353 : :
7354 : 216 : appendPQExpBufferStr(query,
7355 : : "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
7356 : : "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
7357 : : "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, ");
7358 : :
7359 : 216 : appendPQExpBufferStr(query,
7360 : : "am.amname, ");
7361 : :
7362 : 216 : appendPQExpBufferStr(query,
7363 : : "(d.deptype = 'i') IS TRUE AS is_identity_sequence, ");
7364 : :
7365 : 216 : appendPQExpBufferStr(query,
7366 : : "c.relispartition AS ispartition ");
7367 : :
7368 : : /*
7369 : : * Left join to pg_depend to pick up dependency info linking sequences to
7370 : : * their owning column, if any (note this dependency is AUTO except for
7371 : : * identity sequences, where it's INTERNAL). Also join to pg_tablespace to
7372 : : * collect the spcname.
7373 : : */
7374 : 216 : appendPQExpBufferStr(query,
7375 : : "\nFROM pg_class c\n"
7376 : : "LEFT JOIN pg_depend d ON "
7377 : : "(c.relkind = " CppAsString2(RELKIND_SEQUENCE) " AND "
7378 : : "d.classid = 'pg_class'::regclass AND d.objid = c.oid AND "
7379 : : "d.objsubid = 0 AND "
7380 : : "d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i'))\n"
7381 : : "LEFT JOIN pg_tablespace tsp ON (tsp.oid = c.reltablespace)\n");
7382 : :
7383 : : /*
7384 : : * Left join to pg_am to pick up the amname.
7385 : : */
7386 : 216 : appendPQExpBufferStr(query,
7387 : : "LEFT JOIN pg_am am ON (c.relam = am.oid)\n");
7388 : :
7389 : : /*
7390 : : * We purposefully ignore toast OIDs for partitioned tables; the reason is
7391 : : * that versions 10 and 11 have them, but later versions do not, so
7392 : : * emitting them causes the upgrade to fail.
7393 : : */
7394 : 216 : appendPQExpBufferStr(query,
7395 : : "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid"
7396 : : " AND tc.relkind = " CppAsString2(RELKIND_TOASTVALUE)
7397 : : " AND c.relkind <> " CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
7398 : :
7399 : : /*
7400 : : * Restrict to interesting relkinds (in particular, not indexes). Not all
7401 : : * relkinds are possible in older servers, but it's not worth the trouble
7402 : : * to emit a version-dependent list.
7403 : : *
7404 : : * Composite-type table entries won't be dumped as such, but we have to
7405 : : * make a DumpableObject for them so that we can track dependencies of the
7406 : : * composite type (pg_depend entries for columns of the composite type
7407 : : * link to the pg_class entry not the pg_type entry).
7408 : : */
7409 : 216 : appendPQExpBufferStr(query,
7410 : : "WHERE c.relkind IN ("
7411 : : CppAsString2(RELKIND_RELATION) ", "
7412 : : CppAsString2(RELKIND_SEQUENCE) ", "
7413 : : CppAsString2(RELKIND_VIEW) ", "
7414 : : CppAsString2(RELKIND_COMPOSITE_TYPE) ", "
7415 : : CppAsString2(RELKIND_MATVIEW) ", "
7416 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
7417 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n"
7418 : : "ORDER BY c.oid");
7419 : :
7420 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7421 : :
7422 : 216 : ntups = PQntuples(res);
7423 : :
7424 : 216 : *numTables = ntups;
7425 : :
7426 : : /*
7427 : : * Extract data from result and lock dumpable tables. We do the locking
7428 : : * before anything else, to minimize the window wherein a table could
7429 : : * disappear under us.
7430 : : *
7431 : : * Note that we have to save info about all tables here, even when dumping
7432 : : * only one, because we don't yet know which tables might be inheritance
7433 : : * ancestors of the target table.
7434 : : */
7435 : 216 : tblinfo = pg_malloc0_array(TableInfo, ntups);
7436 : :
7437 : 216 : i_reltableoid = PQfnumber(res, "tableoid");
7438 : 216 : i_reloid = PQfnumber(res, "oid");
7439 : 216 : i_relname = PQfnumber(res, "relname");
7440 : 216 : i_relnamespace = PQfnumber(res, "relnamespace");
7441 : 216 : i_relkind = PQfnumber(res, "relkind");
7442 : 216 : i_reltype = PQfnumber(res, "reltype");
7443 : 216 : i_relowner = PQfnumber(res, "relowner");
7444 : 216 : i_relchecks = PQfnumber(res, "relchecks");
7445 : 216 : i_relhasindex = PQfnumber(res, "relhasindex");
7446 : 216 : i_relhasrules = PQfnumber(res, "relhasrules");
7447 : 216 : i_relpages = PQfnumber(res, "relpages");
7448 : 216 : i_reltuples = PQfnumber(res, "reltuples");
7449 : 216 : i_relallvisible = PQfnumber(res, "relallvisible");
7450 : 216 : i_relallfrozen = PQfnumber(res, "relallfrozen");
7451 : 216 : i_toastpages = PQfnumber(res, "toastpages");
7452 : 216 : i_owning_tab = PQfnumber(res, "owning_tab");
7453 : 216 : i_owning_col = PQfnumber(res, "owning_col");
7454 : 216 : i_reltablespace = PQfnumber(res, "reltablespace");
7455 : 216 : i_relhasoids = PQfnumber(res, "relhasoids");
7456 : 216 : i_relhastriggers = PQfnumber(res, "relhastriggers");
7457 : 216 : i_relpersistence = PQfnumber(res, "relpersistence");
7458 : 216 : i_relispopulated = PQfnumber(res, "relispopulated");
7459 : 216 : i_relreplident = PQfnumber(res, "relreplident");
7460 : 216 : i_relrowsec = PQfnumber(res, "relrowsecurity");
7461 : 216 : i_relforcerowsec = PQfnumber(res, "relforcerowsecurity");
7462 : 216 : i_relfrozenxid = PQfnumber(res, "relfrozenxid");
7463 : 216 : i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
7464 : 216 : i_toastoid = PQfnumber(res, "toid");
7465 : 216 : i_relminmxid = PQfnumber(res, "relminmxid");
7466 : 216 : i_toastminmxid = PQfnumber(res, "tminmxid");
7467 : 216 : i_reloptions = PQfnumber(res, "reloptions");
7468 : 216 : i_checkoption = PQfnumber(res, "checkoption");
7469 : 216 : i_toastreloptions = PQfnumber(res, "toast_reloptions");
7470 : 216 : i_reloftype = PQfnumber(res, "reloftype");
7471 : 216 : i_foreignserver = PQfnumber(res, "foreignserver");
7472 : 216 : i_amname = PQfnumber(res, "amname");
7473 : 216 : i_is_identity_sequence = PQfnumber(res, "is_identity_sequence");
7474 : 216 : i_relacl = PQfnumber(res, "relacl");
7475 : 216 : i_acldefault = PQfnumber(res, "acldefault");
7476 : 216 : i_ispartition = PQfnumber(res, "ispartition");
7477 : :
7478 [ + + ]: 216 : if (dopt->lockWaitTimeout)
7479 : : {
7480 : : /*
7481 : : * Arrange to fail instead of waiting forever for a table lock.
7482 : : *
7483 : : * NB: this coding assumes that the only queries issued within the
7484 : : * following loop are LOCK TABLEs; else the timeout may be undesirably
7485 : : * applied to other things too.
7486 : : */
7487 : 2 : resetPQExpBuffer(query);
7488 : 2 : appendPQExpBufferStr(query, "SET statement_timeout = ");
7489 : 2 : appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout));
7490 : 2 : ExecuteSqlStatement(fout, query->data);
7491 : : }
7492 : :
7493 : 216 : resetPQExpBuffer(query);
7494 : :
7495 [ + + ]: 57977 : for (i = 0; i < ntups; i++)
7496 : : {
7497 : 57761 : int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible));
7498 : 57761 : int32 relallfrozen = atoi(PQgetvalue(res, i, i_relallfrozen));
7499 : :
7500 : 57761 : tblinfo[i].dobj.objType = DO_TABLE;
7501 : 57761 : tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
7502 : 57761 : tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
7503 : 57761 : AssignDumpId(&tblinfo[i].dobj);
7504 : 57761 : tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
7505 : 115522 : tblinfo[i].dobj.namespace =
7506 : 57761 : findNamespace(atooid(PQgetvalue(res, i, i_relnamespace)));
7507 : 57761 : tblinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_relacl));
7508 : 57761 : tblinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7509 : 57761 : tblinfo[i].dacl.privtype = 0;
7510 : 57761 : tblinfo[i].dacl.initprivs = NULL;
7511 : 57761 : tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
7512 : 57761 : tblinfo[i].reltype = atooid(PQgetvalue(res, i, i_reltype));
7513 : 57761 : tblinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_relowner));
7514 : 57761 : tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
7515 : 57761 : tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
7516 : 57761 : tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
7517 : 57761 : tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages));
7518 [ + + ]: 57761 : if (PQgetisnull(res, i, i_toastpages))
7519 : 46683 : tblinfo[i].toastpages = 0;
7520 : : else
7521 : 11078 : tblinfo[i].toastpages = atoi(PQgetvalue(res, i, i_toastpages));
7522 [ + + ]: 57761 : if (PQgetisnull(res, i, i_owning_tab))
7523 : : {
7524 : 57321 : tblinfo[i].owning_tab = InvalidOid;
7525 : 57321 : tblinfo[i].owning_col = 0;
7526 : : }
7527 : : else
7528 : : {
7529 : 440 : tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
7530 : 440 : tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
7531 : : }
7532 : 57761 : tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace));
7533 : 57761 : tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
7534 : 57761 : tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
7535 : 57761 : tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence));
7536 : 57761 : tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0);
7537 : 57761 : tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident));
7538 : 57761 : tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0);
7539 : 57761 : tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0);
7540 : 57761 : tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
7541 : 57761 : tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
7542 : 57761 : tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
7543 : 57761 : tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid));
7544 : 57761 : tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid));
7545 : 57761 : tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions));
7546 [ + + ]: 57761 : if (PQgetisnull(res, i, i_checkoption))
7547 : 57712 : tblinfo[i].checkoption = NULL;
7548 : : else
7549 : 49 : tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption));
7550 : 57761 : tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions));
7551 : 57761 : tblinfo[i].reloftype = atooid(PQgetvalue(res, i, i_reloftype));
7552 : 57761 : tblinfo[i].foreign_server = atooid(PQgetvalue(res, i, i_foreignserver));
7553 [ + + ]: 57761 : if (PQgetisnull(res, i, i_amname))
7554 : 35266 : tblinfo[i].amname = NULL;
7555 : : else
7556 : 22495 : tblinfo[i].amname = pg_strdup(PQgetvalue(res, i, i_amname));
7557 : 57761 : tblinfo[i].is_identity_sequence = (strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0);
7558 : 57761 : tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0);
7559 : :
7560 : : /* other fields were zeroed above */
7561 : :
7562 : : /*
7563 : : * Decide whether we want to dump this table.
7564 : : */
7565 [ + + ]: 57761 : if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
7566 : 248 : tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE;
7567 : : else
7568 : 57513 : selectDumpableTable(&tblinfo[i], fout);
7569 : :
7570 : : /*
7571 : : * Now, consider the table "interesting" if we need to dump its
7572 : : * definition, data or its statistics. Later on, we'll skip a lot of
7573 : : * data collection for uninteresting tables.
7574 : : *
7575 : : * Note: the "interesting" flag will also be set by flagInhTables for
7576 : : * parents of interesting tables, so that we collect necessary
7577 : : * inheritance info even when the parents are not themselves being
7578 : : * dumped. This is the main reason why we need an "interesting" flag
7579 : : * that's separate from the components-to-dump bitmask.
7580 : : */
7581 : 57761 : tblinfo[i].interesting = (tblinfo[i].dobj.dump &
7582 : : (DUMP_COMPONENT_DEFINITION |
7583 : : DUMP_COMPONENT_DATA |
7584 : 57761 : DUMP_COMPONENT_STATISTICS)) != 0;
7585 : :
7586 : 57761 : tblinfo[i].dummy_view = false; /* might get set during sort */
7587 : 57761 : tblinfo[i].postponed_def = false; /* might get set during sort */
7588 : :
7589 : : /* Tables have data */
7590 : 57761 : tblinfo[i].dobj.components |= DUMP_COMPONENT_DATA;
7591 : :
7592 : : /* Mark whether table has an ACL */
7593 [ + + ]: 57761 : if (!PQgetisnull(res, i, i_relacl))
7594 : 46811 : tblinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7595 : 57761 : tblinfo[i].hascolumnACLs = false; /* may get set later */
7596 : :
7597 : : /* Add statistics */
7598 [ + + ]: 57761 : if (tblinfo[i].interesting)
7599 : : {
7600 : : RelStatsInfo *stats;
7601 : :
7602 : 15360 : stats = getRelationStatistics(fout, &tblinfo[i].dobj,
7603 : 7680 : tblinfo[i].relpages,
7604 : : PQgetvalue(res, i, i_reltuples),
7605 : : relallvisible, relallfrozen,
7606 : 7680 : tblinfo[i].relkind, NULL, 0);
7607 [ + + ]: 7680 : if (tblinfo[i].relkind == RELKIND_MATVIEW)
7608 : 426 : tblinfo[i].stats = stats;
7609 : : }
7610 : :
7611 : : /*
7612 : : * Read-lock target tables to make sure they aren't DROPPED or altered
7613 : : * in schema before we get around to dumping them.
7614 : : *
7615 : : * Note that we don't explicitly lock parents of the target tables; we
7616 : : * assume our lock on the child is enough to prevent schema
7617 : : * alterations to parent tables.
7618 : : *
7619 : : * NOTE: it'd be kinda nice to lock other relations too, not only
7620 : : * plain or partitioned tables, but the backend doesn't presently
7621 : : * allow that.
7622 : : *
7623 : : * We only need to lock the table for certain components; see
7624 : : * pg_dump.h
7625 : : */
7626 [ + + ]: 57761 : if ((tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK) &&
7627 [ + + ]: 7680 : (tblinfo[i].relkind == RELKIND_RELATION ||
7628 [ + + ]: 2128 : tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE))
7629 : : {
7630 : : /*
7631 : : * Tables are locked in batches. When dumping from a remote
7632 : : * server this can save a significant amount of time by reducing
7633 : : * the number of round trips.
7634 : : */
7635 [ + + ]: 6204 : if (query->len == 0)
7636 : 144 : appendPQExpBuffer(query, "LOCK TABLE %s",
7637 : 144 : fmtQualifiedDumpable(&tblinfo[i]));
7638 : : else
7639 : : {
7640 : 6060 : appendPQExpBuffer(query, ", %s",
7641 : 6060 : fmtQualifiedDumpable(&tblinfo[i]));
7642 : :
7643 : : /* Arbitrarily end a batch when query length reaches 100K. */
7644 [ - + ]: 6060 : if (query->len >= 100000)
7645 : : {
7646 : : /* Lock another batch of tables. */
7647 : 0 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7648 : 0 : ExecuteSqlStatement(fout, query->data);
7649 : 0 : resetPQExpBuffer(query);
7650 : : }
7651 : : }
7652 : : }
7653 : : }
7654 : :
7655 [ + + ]: 216 : if (query->len != 0)
7656 : : {
7657 : : /* Lock the tables in the last batch. */
7658 : 144 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7659 : 144 : ExecuteSqlStatement(fout, query->data);
7660 : : }
7661 : :
7662 [ + + ]: 215 : if (dopt->lockWaitTimeout)
7663 : : {
7664 : 2 : ExecuteSqlStatement(fout, "SET statement_timeout = 0");
7665 : : }
7666 : :
7667 : 215 : PQclear(res);
7668 : :
7669 : 215 : destroyPQExpBuffer(query);
7670 : :
7671 : 215 : return tblinfo;
7672 : : }
7673 : :
7674 : : /*
7675 : : * getOwnedSeqs
7676 : : * identify owned sequences and mark them as dumpable if owning table is
7677 : : *
7678 : : * We used to do this in getTables(), but it's better to do it after the
7679 : : * index used by findTableByOid() has been set up.
7680 : : */
7681 : : void
7682 : 215 : getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables)
7683 : : {
7684 : : int i;
7685 : :
7686 : : /*
7687 : : * Force sequences that are "owned" by table columns to be dumped whenever
7688 : : * their owning table is being dumped.
7689 : : */
7690 [ + + ]: 57691 : for (i = 0; i < numTables; i++)
7691 : : {
7692 : 57476 : TableInfo *seqinfo = &tblinfo[i];
7693 : : TableInfo *owning_tab;
7694 : :
7695 [ + + ]: 57476 : if (!OidIsValid(seqinfo->owning_tab))
7696 : 57039 : continue; /* not an owned sequence */
7697 : :
7698 : 437 : owning_tab = findTableByOid(seqinfo->owning_tab);
7699 [ - + ]: 437 : if (owning_tab == NULL)
7700 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
7701 : : seqinfo->owning_tab, seqinfo->dobj.catId.oid);
7702 : :
7703 : : /*
7704 : : * For an identity sequence, dump exactly the same components for the
7705 : : * sequence as for the owning table. This is important because we
7706 : : * treat the identity sequence as an integral part of the table. For
7707 : : * example, there is not any DDL command that allows creation of such
7708 : : * a sequence independently of the table.
7709 : : *
7710 : : * For other owned sequences such as serial sequences, we need to dump
7711 : : * the components that are being dumped for the table and any
7712 : : * components that the sequence is explicitly marked with.
7713 : : *
7714 : : * We can't simply use the set of components which are being dumped
7715 : : * for the table as the table might be in an extension (and only the
7716 : : * non-extension components, eg: ACLs if changed, security labels, and
7717 : : * policies, are being dumped) while the sequence is not (and
7718 : : * therefore the definition and other components should also be
7719 : : * dumped).
7720 : : *
7721 : : * If the sequence is part of the extension then it should be properly
7722 : : * marked by checkExtensionMembership() and this will be a no-op as
7723 : : * the table will be equivalently marked.
7724 : : */
7725 [ + + ]: 437 : if (seqinfo->is_identity_sequence)
7726 : 218 : seqinfo->dobj.dump = owning_tab->dobj.dump;
7727 : : else
7728 : 219 : seqinfo->dobj.dump |= owning_tab->dobj.dump;
7729 : :
7730 : : /* Make sure that necessary data is available if we're dumping it */
7731 [ + + ]: 437 : if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE)
7732 : : {
7733 : 341 : seqinfo->interesting = true;
7734 : 341 : owning_tab->interesting = true;
7735 : : }
7736 : : }
7737 : 215 : }
7738 : :
7739 : : /*
7740 : : * getInherits
7741 : : * read all the inheritance information
7742 : : * from the system catalogs return them in the InhInfo* structure
7743 : : *
7744 : : * numInherits is set to the number of pairs read in
7745 : : */
7746 : : InhInfo *
7747 : 215 : getInherits(Archive *fout, int *numInherits)
7748 : : {
7749 : : PGresult *res;
7750 : : int ntups;
7751 : : int i;
7752 : 215 : PQExpBuffer query = createPQExpBuffer();
7753 : : InhInfo *inhinfo;
7754 : :
7755 : : int i_inhrelid;
7756 : : int i_inhparent;
7757 : :
7758 : : /* find all the inheritance information */
7759 : 215 : appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits");
7760 : :
7761 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7762 : :
7763 : 215 : ntups = PQntuples(res);
7764 : :
7765 : 215 : *numInherits = ntups;
7766 : :
7767 : 215 : inhinfo = pg_malloc_array(InhInfo, ntups);
7768 : :
7769 : 215 : i_inhrelid = PQfnumber(res, "inhrelid");
7770 : 215 : i_inhparent = PQfnumber(res, "inhparent");
7771 : :
7772 [ + + ]: 3862 : for (i = 0; i < ntups; i++)
7773 : : {
7774 : 3647 : inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
7775 : 3647 : inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
7776 : : }
7777 : :
7778 : 215 : PQclear(res);
7779 : :
7780 : 215 : destroyPQExpBuffer(query);
7781 : :
7782 : 215 : return inhinfo;
7783 : : }
7784 : :
7785 : : /*
7786 : : * getPartitioningInfo
7787 : : * get information about partitioning
7788 : : *
7789 : : * For the most part, we only collect partitioning info about tables we
7790 : : * intend to dump. However, this function has to consider all partitioned
7791 : : * tables in the database, because we need to know about parents of partitions
7792 : : * we are going to dump even if the parents themselves won't be dumped.
7793 : : *
7794 : : * Specifically, what we need to know is whether each partitioned table
7795 : : * has an "unsafe" partitioning scheme that requires us to force
7796 : : * load-via-partition-root mode for its children. Currently the only case
7797 : : * for which we force that is hash partitioning on enum columns, since the
7798 : : * hash codes depend on enum value OIDs which won't be replicated across
7799 : : * dump-and-reload. There are other cases in which load-via-partition-root
7800 : : * might be necessary, but we expect users to cope with them.
7801 : : */
7802 : : void
7803 : 215 : getPartitioningInfo(Archive *fout)
7804 : : {
7805 : : PQExpBuffer query;
7806 : : PGresult *res;
7807 : : int ntups;
7808 : :
7809 : : /* hash partitioning didn't exist before v11 */
7810 [ - + ]: 215 : if (fout->remoteVersion < 110000)
7811 : 0 : return;
7812 : : /* needn't bother if not dumping data */
7813 [ + + ]: 215 : if (!fout->dopt->dumpData)
7814 : 49 : return;
7815 : :
7816 : 166 : query = createPQExpBuffer();
7817 : :
7818 : : /*
7819 : : * Unsafe partitioning schemes are exactly those for which hash enum_ops
7820 : : * appears among the partition opclasses. We needn't check partstrat.
7821 : : *
7822 : : * Note that this query may well retrieve info about tables we aren't
7823 : : * going to dump and hence have no lock on. That's okay since we need not
7824 : : * invoke any unsafe server-side functions.
7825 : : */
7826 : 166 : appendPQExpBufferStr(query,
7827 : : "SELECT partrelid FROM pg_partitioned_table WHERE\n"
7828 : : "(SELECT c.oid FROM pg_opclass c JOIN pg_am a "
7829 : : "ON c.opcmethod = a.oid\n"
7830 : : "WHERE opcname = 'enum_ops' "
7831 : : "AND opcnamespace = 'pg_catalog'::regnamespace "
7832 : : "AND amname = 'hash') = ANY(partclass)");
7833 : :
7834 : 166 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7835 : :
7836 : 166 : ntups = PQntuples(res);
7837 : :
7838 [ + + ]: 227 : for (int i = 0; i < ntups; i++)
7839 : : {
7840 : 61 : Oid tabrelid = atooid(PQgetvalue(res, i, 0));
7841 : : TableInfo *tbinfo;
7842 : :
7843 : 61 : tbinfo = findTableByOid(tabrelid);
7844 [ - + ]: 61 : if (tbinfo == NULL)
7845 : 0 : pg_fatal("failed sanity check, table OID %u appearing in pg_partitioned_table not found",
7846 : : tabrelid);
7847 : 61 : tbinfo->unsafe_partitions = true;
7848 : : }
7849 : :
7850 : 166 : PQclear(res);
7851 : :
7852 : 166 : destroyPQExpBuffer(query);
7853 : : }
7854 : :
7855 : : /*
7856 : : * getIndexes
7857 : : * get information about every index on a dumpable table
7858 : : *
7859 : : * Note: index data is not returned directly to the caller, but it
7860 : : * does get entered into the DumpableObject tables.
7861 : : */
7862 : : void
7863 : 215 : getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
7864 : : {
7865 : 215 : PQExpBuffer query = createPQExpBuffer();
7866 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
7867 : : PGresult *res;
7868 : : int ntups;
7869 : : int curtblindx;
7870 : : IndxInfo *indxinfo;
7871 : : int i_tableoid,
7872 : : i_oid,
7873 : : i_indrelid,
7874 : : i_indexname,
7875 : : i_relpages,
7876 : : i_reltuples,
7877 : : i_relallvisible,
7878 : : i_relallfrozen,
7879 : : i_parentidx,
7880 : : i_indexdef,
7881 : : i_indnkeyatts,
7882 : : i_indnatts,
7883 : : i_indkey,
7884 : : i_indisclustered,
7885 : : i_indisreplident,
7886 : : i_indnullsnotdistinct,
7887 : : i_contype,
7888 : : i_conname,
7889 : : i_condeferrable,
7890 : : i_condeferred,
7891 : : i_conperiod,
7892 : : i_contableoid,
7893 : : i_conoid,
7894 : : i_condef,
7895 : : i_indattnames,
7896 : : i_tablespace,
7897 : : i_indreloptions,
7898 : : i_indstatcols,
7899 : : i_indstatvals;
7900 : :
7901 : : /*
7902 : : * We want to perform just one query against pg_index. However, we
7903 : : * mustn't try to select every row of the catalog and then sort it out on
7904 : : * the client side, because some of the server-side functions we need
7905 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
7906 : : * build an array of the OIDs of tables we care about (and now have lock
7907 : : * on!), and use a WHERE clause to constrain which rows are selected.
7908 : : */
7909 : 215 : appendPQExpBufferChar(tbloids, '{');
7910 [ + + ]: 57691 : for (int i = 0; i < numTables; i++)
7911 : : {
7912 : 57476 : TableInfo *tbinfo = &tblinfo[i];
7913 : :
7914 [ + + ]: 57476 : if (!tbinfo->hasindex)
7915 : 41115 : continue;
7916 : :
7917 : : /*
7918 : : * We can ignore indexes of uninteresting tables.
7919 : : */
7920 [ + + ]: 16361 : if (!tbinfo->interesting)
7921 : 14192 : continue;
7922 : :
7923 : : /* OK, we need info for this table */
7924 [ + + ]: 2169 : if (tbloids->len > 1) /* do we have more than the '{'? */
7925 : 2074 : appendPQExpBufferChar(tbloids, ',');
7926 : 2169 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
7927 : : }
7928 : 215 : appendPQExpBufferChar(tbloids, '}');
7929 : :
7930 : 215 : appendPQExpBufferStr(query,
7931 : : "SELECT t.tableoid, t.oid, i.indrelid, "
7932 : : "t.relname AS indexname, "
7933 : : "t.relpages, t.reltuples, t.relallvisible, ");
7934 : :
7935 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
7936 : 215 : appendPQExpBufferStr(query, "t.relallfrozen, ");
7937 : : else
7938 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7939 : :
7940 : 215 : appendPQExpBufferStr(query,
7941 : : "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
7942 : : "i.indkey, i.indisclustered, "
7943 : : "c.contype, c.conname, "
7944 : : "c.condeferrable, c.condeferred, "
7945 : : "c.tableoid AS contableoid, "
7946 : : "c.oid AS conoid, "
7947 : : "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
7948 : : "CASE WHEN i.indexprs IS NOT NULL THEN "
7949 : : "(SELECT pg_catalog.array_agg(attname ORDER BY attnum)"
7950 : : " FROM pg_catalog.pg_attribute "
7951 : : " WHERE attrelid = i.indexrelid) "
7952 : : "ELSE NULL END AS indattnames, "
7953 : : "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
7954 : : "t.reloptions AS indreloptions, ");
7955 : :
7956 : :
7957 : 215 : appendPQExpBufferStr(query,
7958 : : "i.indisreplident, ");
7959 : :
7960 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
7961 : 215 : appendPQExpBufferStr(query,
7962 : : "inh.inhparent AS parentidx, "
7963 : : "i.indnkeyatts AS indnkeyatts, "
7964 : : "i.indnatts AS indnatts, "
7965 : : "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) "
7966 : : " FROM pg_catalog.pg_attribute "
7967 : : " WHERE attrelid = i.indexrelid AND "
7968 : : " attstattarget >= 0) AS indstatcols, "
7969 : : "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) "
7970 : : " FROM pg_catalog.pg_attribute "
7971 : : " WHERE attrelid = i.indexrelid AND "
7972 : : " attstattarget >= 0) AS indstatvals, ");
7973 : : else
7974 : 0 : appendPQExpBufferStr(query,
7975 : : "0 AS parentidx, "
7976 : : "i.indnatts AS indnkeyatts, "
7977 : : "i.indnatts AS indnatts, "
7978 : : "'' AS indstatcols, "
7979 : : "'' AS indstatvals, ");
7980 : :
7981 [ + - ]: 215 : if (fout->remoteVersion >= 150000)
7982 : 215 : appendPQExpBufferStr(query,
7983 : : "i.indnullsnotdistinct, ");
7984 : : else
7985 : 0 : appendPQExpBufferStr(query,
7986 : : "false AS indnullsnotdistinct, ");
7987 : :
7988 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
7989 : 215 : appendPQExpBufferStr(query,
7990 : : "c.conperiod ");
7991 : : else
7992 : 0 : appendPQExpBufferStr(query,
7993 : : "NULL AS conperiod ");
7994 : :
7995 : : /*
7996 : : * The point of the messy-looking outer join is to find a constraint that
7997 : : * is related by an internal dependency link to the index. If we find one,
7998 : : * create a CONSTRAINT entry linked to the INDEX entry. We assume an
7999 : : * index won't have more than one internal dependency.
8000 : : *
8001 : : * Note: the check on conrelid is redundant, but useful because that
8002 : : * column is indexed while conindid is not.
8003 : : */
8004 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
8005 : : {
8006 : 215 : appendPQExpBuffer(query,
8007 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8008 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
8009 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
8010 : : "JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) "
8011 : : "LEFT JOIN pg_catalog.pg_constraint c "
8012 : : "ON (i.indrelid = c.conrelid AND "
8013 : : "i.indexrelid = c.conindid AND "
8014 : : "c.contype IN ('p','u','x')) "
8015 : : "LEFT JOIN pg_catalog.pg_inherits inh "
8016 : : "ON (inh.inhrelid = indexrelid) "
8017 : : "WHERE (i.indisvalid OR t2.relkind = 'p') "
8018 : : "AND i.indisready "
8019 : : "ORDER BY i.indrelid, indexname",
8020 : : tbloids->data);
8021 : : }
8022 : : else
8023 : : {
8024 : : /*
8025 : : * the test on indisready is necessary in 9.2, and harmless in
8026 : : * earlier/later versions
8027 : : */
8028 : 0 : appendPQExpBuffer(query,
8029 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8030 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
8031 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
8032 : : "LEFT JOIN pg_catalog.pg_constraint c "
8033 : : "ON (i.indrelid = c.conrelid AND "
8034 : : "i.indexrelid = c.conindid AND "
8035 : : "c.contype IN ('p','u','x')) "
8036 : : "WHERE i.indisvalid AND i.indisready "
8037 : : "ORDER BY i.indrelid, indexname",
8038 : : tbloids->data);
8039 : : }
8040 : :
8041 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8042 : :
8043 : 215 : ntups = PQntuples(res);
8044 : :
8045 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8046 : 215 : i_oid = PQfnumber(res, "oid");
8047 : 215 : i_indrelid = PQfnumber(res, "indrelid");
8048 : 215 : i_indexname = PQfnumber(res, "indexname");
8049 : 215 : i_relpages = PQfnumber(res, "relpages");
8050 : 215 : i_reltuples = PQfnumber(res, "reltuples");
8051 : 215 : i_relallvisible = PQfnumber(res, "relallvisible");
8052 : 215 : i_relallfrozen = PQfnumber(res, "relallfrozen");
8053 : 215 : i_parentidx = PQfnumber(res, "parentidx");
8054 : 215 : i_indexdef = PQfnumber(res, "indexdef");
8055 : 215 : i_indnkeyatts = PQfnumber(res, "indnkeyatts");
8056 : 215 : i_indnatts = PQfnumber(res, "indnatts");
8057 : 215 : i_indkey = PQfnumber(res, "indkey");
8058 : 215 : i_indisclustered = PQfnumber(res, "indisclustered");
8059 : 215 : i_indisreplident = PQfnumber(res, "indisreplident");
8060 : 215 : i_indnullsnotdistinct = PQfnumber(res, "indnullsnotdistinct");
8061 : 215 : i_contype = PQfnumber(res, "contype");
8062 : 215 : i_conname = PQfnumber(res, "conname");
8063 : 215 : i_condeferrable = PQfnumber(res, "condeferrable");
8064 : 215 : i_condeferred = PQfnumber(res, "condeferred");
8065 : 215 : i_conperiod = PQfnumber(res, "conperiod");
8066 : 215 : i_contableoid = PQfnumber(res, "contableoid");
8067 : 215 : i_conoid = PQfnumber(res, "conoid");
8068 : 215 : i_condef = PQfnumber(res, "condef");
8069 : 215 : i_indattnames = PQfnumber(res, "indattnames");
8070 : 215 : i_tablespace = PQfnumber(res, "tablespace");
8071 : 215 : i_indreloptions = PQfnumber(res, "indreloptions");
8072 : 215 : i_indstatcols = PQfnumber(res, "indstatcols");
8073 : 215 : i_indstatvals = PQfnumber(res, "indstatvals");
8074 : :
8075 : 215 : indxinfo = pg_malloc_array(IndxInfo, ntups);
8076 : :
8077 : : /*
8078 : : * Outer loop iterates once per table, not once per row. Incrementing of
8079 : : * j is handled by the inner loop.
8080 : : */
8081 : 215 : curtblindx = -1;
8082 [ + + ]: 2364 : for (int j = 0; j < ntups;)
8083 : : {
8084 : 2149 : Oid indrelid = atooid(PQgetvalue(res, j, i_indrelid));
8085 : 2149 : TableInfo *tbinfo = NULL;
8086 : : int numinds;
8087 : :
8088 : : /* Count rows for this table */
8089 [ + + ]: 2800 : for (numinds = 1; numinds < ntups - j; numinds++)
8090 [ + + ]: 2705 : if (atooid(PQgetvalue(res, j + numinds, i_indrelid)) != indrelid)
8091 : 2054 : break;
8092 : :
8093 : : /*
8094 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8095 : : * order.
8096 : : */
8097 [ + - ]: 27991 : while (++curtblindx < numTables)
8098 : : {
8099 : 27991 : tbinfo = &tblinfo[curtblindx];
8100 [ + + ]: 27991 : if (tbinfo->dobj.catId.oid == indrelid)
8101 : 2149 : break;
8102 : : }
8103 [ - + ]: 2149 : if (curtblindx >= numTables)
8104 : 0 : pg_fatal("unrecognized table OID %u", indrelid);
8105 : : /* cross-check that we only got requested tables */
8106 [ + - ]: 2149 : if (!tbinfo->hasindex ||
8107 [ - + ]: 2149 : !tbinfo->interesting)
8108 : 0 : pg_fatal("unexpected index data for table \"%s\"",
8109 : : tbinfo->dobj.name);
8110 : :
8111 : : /* Save data for this table */
8112 : 2149 : tbinfo->indexes = indxinfo + j;
8113 : 2149 : tbinfo->numIndexes = numinds;
8114 : :
8115 [ + + ]: 4949 : for (int c = 0; c < numinds; c++, j++)
8116 : : {
8117 : : char contype;
8118 : : char indexkind;
8119 : 2800 : char **indAttNames = NULL;
8120 : 2800 : int nindAttNames = 0;
8121 : : RelStatsInfo *relstats;
8122 : 2800 : int32 relpages = atoi(PQgetvalue(res, j, i_relpages));
8123 : 2800 : int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible));
8124 : 2800 : int32 relallfrozen = atoi(PQgetvalue(res, j, i_relallfrozen));
8125 : :
8126 : 2800 : indxinfo[j].dobj.objType = DO_INDEX;
8127 : 2800 : indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8128 : 2800 : indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8129 : 2800 : AssignDumpId(&indxinfo[j].dobj);
8130 : 2800 : indxinfo[j].dobj.dump = tbinfo->dobj.dump;
8131 : 2800 : indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname));
8132 : 2800 : indxinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8133 : 2800 : indxinfo[j].indextable = tbinfo;
8134 : 2800 : indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef));
8135 : 2800 : indxinfo[j].indnkeyattrs = atoi(PQgetvalue(res, j, i_indnkeyatts));
8136 : 2800 : indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts));
8137 : 2800 : indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
8138 : 2800 : indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions));
8139 : 2800 : indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols));
8140 : 2800 : indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals));
8141 : 2800 : indxinfo[j].indkeys = parseIntArray(PQgetvalue(res, j, i_indkey),
8142 : 2800 : indxinfo[j].indnattrs);
8143 : 2800 : indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
8144 : 2800 : indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
8145 : 2800 : indxinfo[j].indnullsnotdistinct = (PQgetvalue(res, j, i_indnullsnotdistinct)[0] == 't');
8146 : 2800 : indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx));
8147 : 2800 : indxinfo[j].partattaches = (SimplePtrList)
8148 : : {
8149 : : NULL, NULL
8150 : : };
8151 : :
8152 [ + + ]: 2800 : if (indxinfo[j].parentidx == 0)
8153 : 2175 : indexkind = RELKIND_INDEX;
8154 : : else
8155 : 625 : indexkind = RELKIND_PARTITIONED_INDEX;
8156 : :
8157 [ + + ]: 2800 : if (!PQgetisnull(res, j, i_indattnames))
8158 : : {
8159 [ - + ]: 208 : if (!parsePGArray(PQgetvalue(res, j, i_indattnames),
8160 : : &indAttNames, &nindAttNames))
8161 : 0 : pg_fatal("could not parse %s array", "indattnames");
8162 : : }
8163 : :
8164 : 2800 : relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages,
8165 : : PQgetvalue(res, j, i_reltuples),
8166 : : relallvisible, relallfrozen, indexkind,
8167 : : indAttNames, nindAttNames);
8168 : :
8169 : 2800 : contype = *(PQgetvalue(res, j, i_contype));
8170 [ + + + + : 2800 : if (contype == 'p' || contype == 'u' || contype == 'x')
+ + ]
8171 : 1618 : {
8172 : : /*
8173 : : * If we found a constraint matching the index, create an
8174 : : * entry for it.
8175 : : */
8176 : : ConstraintInfo *constrinfo;
8177 : :
8178 : 1618 : constrinfo = pg_malloc_object(ConstraintInfo);
8179 : 1618 : constrinfo->dobj.objType = DO_CONSTRAINT;
8180 : 1618 : constrinfo->dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8181 : 1618 : constrinfo->dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8182 : 1618 : AssignDumpId(&constrinfo->dobj);
8183 : 1618 : constrinfo->dobj.dump = tbinfo->dobj.dump;
8184 : 1618 : constrinfo->dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8185 : 1618 : constrinfo->dobj.namespace = tbinfo->dobj.namespace;
8186 : 1618 : constrinfo->contable = tbinfo;
8187 : 1618 : constrinfo->condomain = NULL;
8188 : 1618 : constrinfo->contype = contype;
8189 [ + + ]: 1618 : if (contype == 'x')
8190 : 61 : constrinfo->condef = pg_strdup(PQgetvalue(res, j, i_condef));
8191 : : else
8192 : 1557 : constrinfo->condef = NULL;
8193 : 1618 : constrinfo->confrelid = InvalidOid;
8194 : 1618 : constrinfo->conindex = indxinfo[j].dobj.dumpId;
8195 : 1618 : constrinfo->condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't';
8196 : 1618 : constrinfo->condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't';
8197 : 1618 : constrinfo->conperiod = *(PQgetvalue(res, j, i_conperiod)) == 't';
8198 : 1618 : constrinfo->conislocal = true;
8199 : 1618 : constrinfo->separate = true;
8200 : :
8201 : 1618 : indxinfo[j].indexconstraint = constrinfo->dobj.dumpId;
8202 [ + + ]: 1618 : if (relstats != NULL)
8203 : 600 : addObjectDependency(&relstats->dobj, constrinfo->dobj.dumpId);
8204 : : }
8205 : : else
8206 : : {
8207 : : /* Plain secondary index */
8208 : 1182 : indxinfo[j].indexconstraint = 0;
8209 : : }
8210 : : }
8211 : : }
8212 : :
8213 : 215 : PQclear(res);
8214 : :
8215 : 215 : destroyPQExpBuffer(query);
8216 : 215 : destroyPQExpBuffer(tbloids);
8217 : 215 : }
8218 : :
8219 : : /*
8220 : : * getExtendedStatistics
8221 : : * get information about extended-statistics objects.
8222 : : *
8223 : : * Note: extended statistics data is not returned directly to the caller, but
8224 : : * it does get entered into the DumpableObject tables.
8225 : : */
8226 : : void
8227 : 215 : getExtendedStatistics(Archive *fout)
8228 : : {
8229 : : PQExpBuffer query;
8230 : : PGresult *res;
8231 : : StatsExtInfo *statsextinfo;
8232 : : int ntups;
8233 : : int i_tableoid;
8234 : : int i_oid;
8235 : : int i_stxname;
8236 : : int i_stxnamespace;
8237 : : int i_stxowner;
8238 : : int i_stxrelid;
8239 : : int i_stattarget;
8240 : : int i;
8241 : :
8242 : 215 : query = createPQExpBuffer();
8243 : :
8244 [ - + ]: 215 : if (fout->remoteVersion < 130000)
8245 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8246 : : "stxnamespace, stxowner, stxrelid, NULL AS stxstattarget "
8247 : : "FROM pg_catalog.pg_statistic_ext");
8248 : : else
8249 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8250 : : "stxnamespace, stxowner, stxrelid, stxstattarget "
8251 : : "FROM pg_catalog.pg_statistic_ext");
8252 : :
8253 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8254 : :
8255 : 215 : ntups = PQntuples(res);
8256 : :
8257 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8258 : 215 : i_oid = PQfnumber(res, "oid");
8259 : 215 : i_stxname = PQfnumber(res, "stxname");
8260 : 215 : i_stxnamespace = PQfnumber(res, "stxnamespace");
8261 : 215 : i_stxowner = PQfnumber(res, "stxowner");
8262 : 215 : i_stxrelid = PQfnumber(res, "stxrelid");
8263 : 215 : i_stattarget = PQfnumber(res, "stxstattarget");
8264 : :
8265 : 215 : statsextinfo = pg_malloc_array(StatsExtInfo, ntups);
8266 : :
8267 [ + + ]: 448 : for (i = 0; i < ntups; i++)
8268 : : {
8269 : 233 : statsextinfo[i].dobj.objType = DO_STATSEXT;
8270 : 233 : statsextinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8271 : 233 : statsextinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8272 : 233 : AssignDumpId(&statsextinfo[i].dobj);
8273 : 233 : statsextinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_stxname));
8274 : 466 : statsextinfo[i].dobj.namespace =
8275 : 233 : findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace)));
8276 : 233 : statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner));
8277 : 466 : statsextinfo[i].stattable =
8278 : 233 : findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid)));
8279 [ + + ]: 233 : if (PQgetisnull(res, i, i_stattarget))
8280 : 185 : statsextinfo[i].stattarget = -1;
8281 : : else
8282 : 48 : statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget));
8283 : :
8284 : : /* Decide whether we want to dump it */
8285 : 233 : selectDumpableStatisticsObject(&(statsextinfo[i]), fout);
8286 : :
8287 [ + + ]: 233 : if (fout->dopt->dumpStatistics)
8288 : 164 : statsextinfo[i].dobj.components |= DUMP_COMPONENT_STATISTICS;
8289 : : }
8290 : :
8291 : 215 : PQclear(res);
8292 : 215 : destroyPQExpBuffer(query);
8293 : 215 : }
8294 : :
8295 : : /*
8296 : : * getConstraints
8297 : : *
8298 : : * Get info about constraints on dumpable tables.
8299 : : *
8300 : : * Currently handles foreign keys only.
8301 : : * Unique and primary key constraints are handled with indexes,
8302 : : * while check constraints are processed in getTableAttrs().
8303 : : */
8304 : : void
8305 : 215 : getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
8306 : : {
8307 : 215 : PQExpBuffer query = createPQExpBuffer();
8308 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
8309 : : PGresult *res;
8310 : : int ntups;
8311 : : int curtblindx;
8312 : 215 : TableInfo *tbinfo = NULL;
8313 : : ConstraintInfo *constrinfo;
8314 : : int i_contableoid,
8315 : : i_conoid,
8316 : : i_conrelid,
8317 : : i_conname,
8318 : : i_confrelid,
8319 : : i_conindid,
8320 : : i_condef;
8321 : :
8322 : : /*
8323 : : * We want to perform just one query against pg_constraint. However, we
8324 : : * mustn't try to select every row of the catalog and then sort it out on
8325 : : * the client side, because some of the server-side functions we need
8326 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8327 : : * build an array of the OIDs of tables we care about (and now have lock
8328 : : * on!), and use a WHERE clause to constrain which rows are selected.
8329 : : */
8330 : 215 : appendPQExpBufferChar(tbloids, '{');
8331 [ + + ]: 57691 : for (int i = 0; i < numTables; i++)
8332 : : {
8333 : 57476 : TableInfo *tinfo = &tblinfo[i];
8334 : :
8335 [ + + ]: 57476 : if (!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8336 : 49853 : continue;
8337 : :
8338 : : /* OK, we need info for this table */
8339 [ + + ]: 7623 : if (tbloids->len > 1) /* do we have more than the '{'? */
8340 : 7478 : appendPQExpBufferChar(tbloids, ',');
8341 : 7623 : appendPQExpBuffer(tbloids, "%u", tinfo->dobj.catId.oid);
8342 : : }
8343 : 215 : appendPQExpBufferChar(tbloids, '}');
8344 : :
8345 : 215 : appendPQExpBufferStr(query,
8346 : : "SELECT c.tableoid, c.oid, "
8347 : : "conrelid, conname, confrelid, ");
8348 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
8349 : 215 : appendPQExpBufferStr(query, "conindid, ");
8350 : : else
8351 : 0 : appendPQExpBufferStr(query, "0 AS conindid, ");
8352 : 215 : appendPQExpBuffer(query,
8353 : : "pg_catalog.pg_get_constraintdef(c.oid) AS condef\n"
8354 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8355 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
8356 : : "WHERE contype = 'f' ",
8357 : : tbloids->data);
8358 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
8359 : 215 : appendPQExpBufferStr(query,
8360 : : "AND conparentid = 0 ");
8361 : 215 : appendPQExpBufferStr(query,
8362 : : "ORDER BY conrelid, conname");
8363 : :
8364 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8365 : :
8366 : 215 : ntups = PQntuples(res);
8367 : :
8368 : 215 : i_contableoid = PQfnumber(res, "tableoid");
8369 : 215 : i_conoid = PQfnumber(res, "oid");
8370 : 215 : i_conrelid = PQfnumber(res, "conrelid");
8371 : 215 : i_conname = PQfnumber(res, "conname");
8372 : 215 : i_confrelid = PQfnumber(res, "confrelid");
8373 : 215 : i_conindid = PQfnumber(res, "conindid");
8374 : 215 : i_condef = PQfnumber(res, "condef");
8375 : :
8376 : 215 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8377 : :
8378 : 215 : curtblindx = -1;
8379 [ + + ]: 392 : for (int j = 0; j < ntups; j++)
8380 : : {
8381 : 177 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
8382 : : TableInfo *reftable;
8383 : :
8384 : : /*
8385 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8386 : : * order.
8387 : : */
8388 [ + + + + ]: 177 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != conrelid)
8389 : : {
8390 [ + - ]: 15599 : while (++curtblindx < numTables)
8391 : : {
8392 : 15599 : tbinfo = &tblinfo[curtblindx];
8393 [ + + ]: 15599 : if (tbinfo->dobj.catId.oid == conrelid)
8394 : 167 : break;
8395 : : }
8396 [ - + ]: 167 : if (curtblindx >= numTables)
8397 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
8398 : : }
8399 : :
8400 : 177 : constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
8401 : 177 : constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8402 : 177 : constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8403 : 177 : AssignDumpId(&constrinfo[j].dobj);
8404 : 177 : constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8405 : 177 : constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8406 : 177 : constrinfo[j].contable = tbinfo;
8407 : 177 : constrinfo[j].condomain = NULL;
8408 : 177 : constrinfo[j].contype = 'f';
8409 : 177 : constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
8410 : 177 : constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid));
8411 : 177 : constrinfo[j].conindex = 0;
8412 : 177 : constrinfo[j].condeferrable = false;
8413 : 177 : constrinfo[j].condeferred = false;
8414 : 177 : constrinfo[j].conislocal = true;
8415 : 177 : constrinfo[j].separate = true;
8416 : :
8417 : : /*
8418 : : * Restoring an FK that points to a partitioned table requires that
8419 : : * all partition indexes have been attached beforehand. Ensure that
8420 : : * happens by making the constraint depend on each index partition
8421 : : * attach object.
8422 : : */
8423 : 177 : reftable = findTableByOid(constrinfo[j].confrelid);
8424 [ + - + + ]: 177 : if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
8425 : : {
8426 : 20 : Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
8427 : :
8428 [ + - ]: 20 : if (indexOid != InvalidOid)
8429 : : {
8430 [ + - ]: 20 : for (int k = 0; k < reftable->numIndexes; k++)
8431 : : {
8432 : : IndxInfo *refidx;
8433 : :
8434 : : /* not our index? */
8435 [ - + ]: 20 : if (reftable->indexes[k].dobj.catId.oid != indexOid)
8436 : 0 : continue;
8437 : :
8438 : 20 : refidx = &reftable->indexes[k];
8439 : 20 : addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
8440 : 20 : break;
8441 : : }
8442 : : }
8443 : : }
8444 : : }
8445 : :
8446 : 215 : PQclear(res);
8447 : :
8448 : 215 : destroyPQExpBuffer(query);
8449 : 215 : destroyPQExpBuffer(tbloids);
8450 : 215 : }
8451 : :
8452 : : /*
8453 : : * addConstrChildIdxDeps
8454 : : *
8455 : : * Recursive subroutine for getConstraints
8456 : : *
8457 : : * Given an object representing a foreign key constraint and an index on the
8458 : : * partitioned table it references, mark the constraint object as dependent
8459 : : * on the DO_INDEX_ATTACH object of each index partition, recursively
8460 : : * drilling down to their partitions if any. This ensures that the FK is not
8461 : : * restored until the index is fully marked valid.
8462 : : */
8463 : : static void
8464 : 45 : addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
8465 : : {
8466 : : SimplePtrListCell *cell;
8467 : :
8468 : : Assert(dobj->objType == DO_FK_CONSTRAINT);
8469 : :
8470 [ + + ]: 155 : for (cell = refidx->partattaches.head; cell; cell = cell->next)
8471 : : {
8472 : 110 : IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
8473 : :
8474 : 110 : addObjectDependency(dobj, attach->dobj.dumpId);
8475 : :
8476 [ + + ]: 110 : if (attach->partitionIdx->partattaches.head != NULL)
8477 : 25 : addConstrChildIdxDeps(dobj, attach->partitionIdx);
8478 : : }
8479 : 45 : }
8480 : :
8481 : : /*
8482 : : * getDomainConstraints
8483 : : *
8484 : : * Get info about constraints on a domain.
8485 : : */
8486 : : static void
8487 : 171 : getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
8488 : : {
8489 : : ConstraintInfo *constrinfo;
8490 : 171 : PQExpBuffer query = createPQExpBuffer();
8491 : : PGresult *res;
8492 : : int i_tableoid,
8493 : : i_oid,
8494 : : i_conname,
8495 : : i_consrc,
8496 : : i_convalidated,
8497 : : i_contype;
8498 : : int ntups;
8499 : :
8500 [ + + ]: 171 : if (!fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS])
8501 : : {
8502 : : /*
8503 : : * Set up query for constraint-specific details. For servers 17 and
8504 : : * up, domains have constraints of type 'n' as well as 'c', otherwise
8505 : : * just the latter.
8506 : : */
8507 : 46 : appendPQExpBuffer(query,
8508 : : "PREPARE getDomainConstraints(pg_catalog.oid) AS\n"
8509 : : "SELECT tableoid, oid, conname, "
8510 : : "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8511 : : "convalidated, contype "
8512 : : "FROM pg_catalog.pg_constraint "
8513 : : "WHERE contypid = $1 AND contype IN (%s) "
8514 : : "ORDER BY conname",
8515 [ - + ]: 46 : fout->remoteVersion < 170000 ? "'c'" : "'c', 'n'");
8516 : :
8517 : 46 : ExecuteSqlStatement(fout, query->data);
8518 : :
8519 : 46 : fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS] = true;
8520 : : }
8521 : :
8522 : 171 : printfPQExpBuffer(query,
8523 : : "EXECUTE getDomainConstraints('%u')",
8524 : : tyinfo->dobj.catId.oid);
8525 : :
8526 : 171 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8527 : :
8528 : 171 : ntups = PQntuples(res);
8529 : :
8530 : 171 : i_tableoid = PQfnumber(res, "tableoid");
8531 : 171 : i_oid = PQfnumber(res, "oid");
8532 : 171 : i_conname = PQfnumber(res, "conname");
8533 : 171 : i_consrc = PQfnumber(res, "consrc");
8534 : 171 : i_convalidated = PQfnumber(res, "convalidated");
8535 : 171 : i_contype = PQfnumber(res, "contype");
8536 : :
8537 : 171 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8538 : 171 : tyinfo->domChecks = constrinfo;
8539 : :
8540 : : /* 'i' tracks result rows; 'j' counts CHECK constraints */
8541 [ + + ]: 353 : for (int i = 0, j = 0; i < ntups; i++)
8542 : : {
8543 : 182 : bool validated = PQgetvalue(res, i, i_convalidated)[0] == 't';
8544 : 182 : char contype = (PQgetvalue(res, i, i_contype))[0];
8545 : : ConstraintInfo *constraint;
8546 : :
8547 [ + + ]: 182 : if (contype == CONSTRAINT_CHECK)
8548 : : {
8549 : 126 : constraint = &constrinfo[j++];
8550 : 126 : tyinfo->nDomChecks++;
8551 : : }
8552 : : else
8553 : : {
8554 : : Assert(contype == CONSTRAINT_NOTNULL);
8555 : : Assert(tyinfo->notnull == NULL);
8556 : : /* use last item in array for the not-null constraint */
8557 : 56 : tyinfo->notnull = &(constrinfo[ntups - 1]);
8558 : 56 : constraint = tyinfo->notnull;
8559 : : }
8560 : :
8561 : 182 : constraint->dobj.objType = DO_CONSTRAINT;
8562 : 182 : constraint->dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8563 : 182 : constraint->dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8564 : 182 : AssignDumpId(&(constraint->dobj));
8565 : 182 : constraint->dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
8566 : 182 : constraint->dobj.namespace = tyinfo->dobj.namespace;
8567 : 182 : constraint->contable = NULL;
8568 : 182 : constraint->condomain = tyinfo;
8569 : 182 : constraint->contype = contype;
8570 : 182 : constraint->condef = pg_strdup(PQgetvalue(res, i, i_consrc));
8571 : 182 : constraint->confrelid = InvalidOid;
8572 : 182 : constraint->conindex = 0;
8573 : 182 : constraint->condeferrable = false;
8574 : 182 : constraint->condeferred = false;
8575 : 182 : constraint->conislocal = true;
8576 : :
8577 : 182 : constraint->separate = !validated;
8578 : :
8579 : : /*
8580 : : * Make the domain depend on the constraint, ensuring it won't be
8581 : : * output till any constraint dependencies are OK. If the constraint
8582 : : * has not been validated, it's going to be dumped after the domain
8583 : : * anyway, so this doesn't matter.
8584 : : */
8585 [ + + ]: 182 : if (validated)
8586 : 177 : addObjectDependency(&tyinfo->dobj, constraint->dobj.dumpId);
8587 : : }
8588 : :
8589 : 171 : PQclear(res);
8590 : :
8591 : 171 : destroyPQExpBuffer(query);
8592 : 171 : }
8593 : :
8594 : : /*
8595 : : * getRules
8596 : : * get basic information about every rule in the system
8597 : : */
8598 : : void
8599 : 215 : getRules(Archive *fout)
8600 : : {
8601 : : PGresult *res;
8602 : : int ntups;
8603 : : int i;
8604 : 215 : PQExpBuffer query = createPQExpBuffer();
8605 : : RuleInfo *ruleinfo;
8606 : : int i_tableoid;
8607 : : int i_oid;
8608 : : int i_rulename;
8609 : : int i_ruletable;
8610 : : int i_ev_type;
8611 : : int i_is_instead;
8612 : : int i_ev_enabled;
8613 : :
8614 : 215 : appendPQExpBufferStr(query, "SELECT "
8615 : : "tableoid, oid, rulename, "
8616 : : "ev_class AS ruletable, ev_type, is_instead, "
8617 : : "ev_enabled "
8618 : : "FROM pg_rewrite "
8619 : : "ORDER BY oid");
8620 : :
8621 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8622 : :
8623 : 215 : ntups = PQntuples(res);
8624 : :
8625 : 215 : ruleinfo = pg_malloc_array(RuleInfo, ntups);
8626 : :
8627 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8628 : 215 : i_oid = PQfnumber(res, "oid");
8629 : 215 : i_rulename = PQfnumber(res, "rulename");
8630 : 215 : i_ruletable = PQfnumber(res, "ruletable");
8631 : 215 : i_ev_type = PQfnumber(res, "ev_type");
8632 : 215 : i_is_instead = PQfnumber(res, "is_instead");
8633 : 215 : i_ev_enabled = PQfnumber(res, "ev_enabled");
8634 : :
8635 [ + + ]: 34739 : for (i = 0; i < ntups; i++)
8636 : : {
8637 : : Oid ruletableoid;
8638 : :
8639 : 34524 : ruleinfo[i].dobj.objType = DO_RULE;
8640 : 34524 : ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8641 : 34524 : ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8642 : 34524 : AssignDumpId(&ruleinfo[i].dobj);
8643 : 34524 : ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename));
8644 : 34524 : ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
8645 : 34524 : ruleinfo[i].ruletable = findTableByOid(ruletableoid);
8646 [ - + ]: 34524 : if (ruleinfo[i].ruletable == NULL)
8647 : 0 : pg_fatal("failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found",
8648 : : ruletableoid, ruleinfo[i].dobj.catId.oid);
8649 : 34524 : ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace;
8650 : 34524 : ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump;
8651 : 34524 : ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
8652 : 34524 : ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
8653 : 34524 : ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled));
8654 [ + - ]: 34524 : if (ruleinfo[i].ruletable)
8655 : : {
8656 : : /*
8657 : : * If the table is a view or materialized view, force its ON
8658 : : * SELECT rule to be sorted before the view itself --- this
8659 : : * ensures that any dependencies for the rule affect the table's
8660 : : * positioning. Other rules are forced to appear after their
8661 : : * table.
8662 : : */
8663 [ + + ]: 34524 : if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW ||
8664 [ + + ]: 727 : ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) &&
8665 [ + + + - ]: 34293 : ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
8666 : : {
8667 : 33821 : addObjectDependency(&ruleinfo[i].ruletable->dobj,
8668 : 33821 : ruleinfo[i].dobj.dumpId);
8669 : : /* We'll merge the rule into CREATE VIEW, if possible */
8670 : 33821 : ruleinfo[i].separate = false;
8671 : : }
8672 : : else
8673 : : {
8674 : 703 : addObjectDependency(&ruleinfo[i].dobj,
8675 : 703 : ruleinfo[i].ruletable->dobj.dumpId);
8676 : 703 : ruleinfo[i].separate = true;
8677 : : }
8678 : : }
8679 : : else
8680 : 0 : ruleinfo[i].separate = true;
8681 : : }
8682 : :
8683 : 215 : PQclear(res);
8684 : :
8685 : 215 : destroyPQExpBuffer(query);
8686 : 215 : }
8687 : :
8688 : : /*
8689 : : * getTriggers
8690 : : * get information about every trigger on a dumpable table
8691 : : *
8692 : : * Note: trigger data is not returned directly to the caller, but it
8693 : : * does get entered into the DumpableObject tables.
8694 : : */
8695 : : void
8696 : 215 : getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
8697 : : {
8698 : 215 : PQExpBuffer query = createPQExpBuffer();
8699 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
8700 : : PGresult *res;
8701 : : int ntups;
8702 : : int curtblindx;
8703 : : TriggerInfo *tginfo;
8704 : : int i_tableoid,
8705 : : i_oid,
8706 : : i_tgrelid,
8707 : : i_tgname,
8708 : : i_tgenabled,
8709 : : i_tgispartition,
8710 : : i_tgdef;
8711 : :
8712 : : /*
8713 : : * We want to perform just one query against pg_trigger. However, we
8714 : : * mustn't try to select every row of the catalog and then sort it out on
8715 : : * the client side, because some of the server-side functions we need
8716 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8717 : : * build an array of the OIDs of tables we care about (and now have lock
8718 : : * on!), and use a WHERE clause to constrain which rows are selected.
8719 : : */
8720 : 215 : appendPQExpBufferChar(tbloids, '{');
8721 [ + + ]: 57691 : for (int i = 0; i < numTables; i++)
8722 : : {
8723 : 57476 : TableInfo *tbinfo = &tblinfo[i];
8724 : :
8725 [ + + ]: 57476 : if (!tbinfo->hastriggers ||
8726 [ + + ]: 1141 : !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8727 : 56601 : continue;
8728 : :
8729 : : /* OK, we need info for this table */
8730 [ + + ]: 875 : if (tbloids->len > 1) /* do we have more than the '{'? */
8731 : 821 : appendPQExpBufferChar(tbloids, ',');
8732 : 875 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
8733 : : }
8734 : 215 : appendPQExpBufferChar(tbloids, '}');
8735 : :
8736 [ + - ]: 215 : if (fout->remoteVersion >= 150000)
8737 : : {
8738 : : /*
8739 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8740 : : * result in non-forward-compatible dumps of WHEN clauses due to
8741 : : * under-parenthesization.
8742 : : *
8743 : : * NB: We need to see partition triggers in case the tgenabled flag
8744 : : * has been changed from the parent.
8745 : : */
8746 : 215 : appendPQExpBuffer(query,
8747 : : "SELECT t.tgrelid, t.tgname, "
8748 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8749 : : "t.tgenabled, t.tableoid, t.oid, "
8750 : : "t.tgparentid <> 0 AS tgispartition\n"
8751 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8752 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8753 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8754 : : "WHERE ((NOT t.tgisinternal AND t.tgparentid = 0) "
8755 : : "OR t.tgenabled != u.tgenabled) "
8756 : : "ORDER BY t.tgrelid, t.tgname",
8757 : : tbloids->data);
8758 : : }
8759 [ # # ]: 0 : else if (fout->remoteVersion >= 130000)
8760 : : {
8761 : : /*
8762 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8763 : : * result in non-forward-compatible dumps of WHEN clauses due to
8764 : : * under-parenthesization.
8765 : : *
8766 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8767 : : * tgenabled flag has been changed from the parent.
8768 : : */
8769 : 0 : appendPQExpBuffer(query,
8770 : : "SELECT t.tgrelid, t.tgname, "
8771 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8772 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition\n"
8773 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8774 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8775 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8776 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != u.tgenabled) "
8777 : : "ORDER BY t.tgrelid, t.tgname",
8778 : : tbloids->data);
8779 : : }
8780 [ # # ]: 0 : else if (fout->remoteVersion >= 110000)
8781 : : {
8782 : : /*
8783 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8784 : : * tgenabled flag has been changed from the parent. No tgparentid in
8785 : : * version 11-12, so we have to match them via pg_depend.
8786 : : *
8787 : : * See above about pretty=true in pg_get_triggerdef.
8788 : : */
8789 : 0 : appendPQExpBuffer(query,
8790 : : "SELECT t.tgrelid, t.tgname, "
8791 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8792 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition "
8793 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8794 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8795 : : "LEFT JOIN pg_catalog.pg_depend AS d ON "
8796 : : " d.classid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8797 : : " d.refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8798 : : " d.objid = t.oid "
8799 : : "LEFT JOIN pg_catalog.pg_trigger AS pt ON pt.oid = refobjid "
8800 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != pt.tgenabled) "
8801 : : "ORDER BY t.tgrelid, t.tgname",
8802 : : tbloids->data);
8803 : : }
8804 : : else
8805 : : {
8806 : : /* See above about pretty=true in pg_get_triggerdef */
8807 : 0 : appendPQExpBuffer(query,
8808 : : "SELECT t.tgrelid, t.tgname, "
8809 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8810 : : "t.tgenabled, false as tgispartition, "
8811 : : "t.tableoid, t.oid "
8812 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8813 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8814 : : "WHERE NOT tgisinternal "
8815 : : "ORDER BY t.tgrelid, t.tgname",
8816 : : tbloids->data);
8817 : : }
8818 : :
8819 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8820 : :
8821 : 215 : ntups = PQntuples(res);
8822 : :
8823 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8824 : 215 : i_oid = PQfnumber(res, "oid");
8825 : 215 : i_tgrelid = PQfnumber(res, "tgrelid");
8826 : 215 : i_tgname = PQfnumber(res, "tgname");
8827 : 215 : i_tgenabled = PQfnumber(res, "tgenabled");
8828 : 215 : i_tgispartition = PQfnumber(res, "tgispartition");
8829 : 215 : i_tgdef = PQfnumber(res, "tgdef");
8830 : :
8831 : 215 : tginfo = pg_malloc_array(TriggerInfo, ntups);
8832 : :
8833 : : /*
8834 : : * Outer loop iterates once per table, not once per row. Incrementing of
8835 : : * j is handled by the inner loop.
8836 : : */
8837 : 215 : curtblindx = -1;
8838 [ + + ]: 533 : for (int j = 0; j < ntups;)
8839 : : {
8840 : 318 : Oid tgrelid = atooid(PQgetvalue(res, j, i_tgrelid));
8841 : 318 : TableInfo *tbinfo = NULL;
8842 : : int numtrigs;
8843 : :
8844 : : /* Count rows for this table */
8845 [ + + ]: 535 : for (numtrigs = 1; numtrigs < ntups - j; numtrigs++)
8846 [ + + ]: 481 : if (atooid(PQgetvalue(res, j + numtrigs, i_tgrelid)) != tgrelid)
8847 : 264 : break;
8848 : :
8849 : : /*
8850 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8851 : : * order.
8852 : : */
8853 [ + - ]: 17416 : while (++curtblindx < numTables)
8854 : : {
8855 : 17416 : tbinfo = &tblinfo[curtblindx];
8856 [ + + ]: 17416 : if (tbinfo->dobj.catId.oid == tgrelid)
8857 : 318 : break;
8858 : : }
8859 [ - + ]: 318 : if (curtblindx >= numTables)
8860 : 0 : pg_fatal("unrecognized table OID %u", tgrelid);
8861 : :
8862 : : /* Save data for this table */
8863 [ + + ]: 853 : for (int c = 0; c < numtrigs; c++, j++)
8864 : : {
8865 : 535 : tginfo[j].dobj.objType = DO_TRIGGER;
8866 : 535 : tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8867 : 535 : tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8868 : 535 : AssignDumpId(&tginfo[j].dobj);
8869 : 535 : tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname));
8870 : 535 : tginfo[j].dobj.namespace = tbinfo->dobj.namespace;
8871 : 535 : tginfo[j].tgtable = tbinfo;
8872 : 535 : tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled));
8873 : 535 : tginfo[j].tgispartition = *(PQgetvalue(res, j, i_tgispartition)) == 't';
8874 : 535 : tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef));
8875 : : }
8876 : : }
8877 : :
8878 : 215 : PQclear(res);
8879 : :
8880 : 215 : destroyPQExpBuffer(query);
8881 : 215 : destroyPQExpBuffer(tbloids);
8882 : 215 : }
8883 : :
8884 : : /*
8885 : : * getEventTriggers
8886 : : * get information about event triggers
8887 : : */
8888 : : void
8889 : 215 : getEventTriggers(Archive *fout)
8890 : : {
8891 : : int i;
8892 : : PQExpBuffer query;
8893 : : PGresult *res;
8894 : : EventTriggerInfo *evtinfo;
8895 : : int i_tableoid,
8896 : : i_oid,
8897 : : i_evtname,
8898 : : i_evtevent,
8899 : : i_evtowner,
8900 : : i_evttags,
8901 : : i_evtfname,
8902 : : i_evtenabled;
8903 : : int ntups;
8904 : :
8905 : 215 : query = createPQExpBuffer();
8906 : :
8907 : 215 : appendPQExpBufferStr(query,
8908 : : "SELECT e.tableoid, e.oid, evtname, evtenabled, "
8909 : : "evtevent, evtowner, "
8910 : : "array_to_string(array("
8911 : : "select quote_literal(x) "
8912 : : " from unnest(evttags) as t(x)), ', ') as evttags, "
8913 : : "e.evtfoid::regproc as evtfname "
8914 : : "FROM pg_event_trigger e "
8915 : : "ORDER BY e.oid");
8916 : :
8917 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8918 : :
8919 : 215 : ntups = PQntuples(res);
8920 : :
8921 : 215 : evtinfo = pg_malloc_array(EventTriggerInfo, ntups);
8922 : :
8923 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8924 : 215 : i_oid = PQfnumber(res, "oid");
8925 : 215 : i_evtname = PQfnumber(res, "evtname");
8926 : 215 : i_evtevent = PQfnumber(res, "evtevent");
8927 : 215 : i_evtowner = PQfnumber(res, "evtowner");
8928 : 215 : i_evttags = PQfnumber(res, "evttags");
8929 : 215 : i_evtfname = PQfnumber(res, "evtfname");
8930 : 215 : i_evtenabled = PQfnumber(res, "evtenabled");
8931 : :
8932 [ + + ]: 270 : for (i = 0; i < ntups; i++)
8933 : : {
8934 : 55 : evtinfo[i].dobj.objType = DO_EVENT_TRIGGER;
8935 : 55 : evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8936 : 55 : evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8937 : 55 : AssignDumpId(&evtinfo[i].dobj);
8938 : 55 : evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname));
8939 : 55 : evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname));
8940 : 55 : evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent));
8941 : 55 : evtinfo[i].evtowner = getRoleName(PQgetvalue(res, i, i_evtowner));
8942 : 55 : evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags));
8943 : 55 : evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname));
8944 : 55 : evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled));
8945 : :
8946 : : /* Decide whether we want to dump it */
8947 : 55 : selectDumpableObject(&(evtinfo[i].dobj), fout);
8948 : : }
8949 : :
8950 : 215 : PQclear(res);
8951 : :
8952 : 215 : destroyPQExpBuffer(query);
8953 : 215 : }
8954 : :
8955 : : /*
8956 : : * getProcLangs
8957 : : * get basic information about every procedural language in the system
8958 : : *
8959 : : * NB: this must run after getFuncs() because we assume we can do
8960 : : * findFuncByOid().
8961 : : */
8962 : : void
8963 : 215 : getProcLangs(Archive *fout)
8964 : : {
8965 : : PGresult *res;
8966 : : int ntups;
8967 : : int i;
8968 : 215 : PQExpBuffer query = createPQExpBuffer();
8969 : : ProcLangInfo *planginfo;
8970 : : int i_tableoid;
8971 : : int i_oid;
8972 : : int i_lanname;
8973 : : int i_lanpltrusted;
8974 : : int i_lanplcallfoid;
8975 : : int i_laninline;
8976 : : int i_lanvalidator;
8977 : : int i_lanacl;
8978 : : int i_acldefault;
8979 : : int i_lanowner;
8980 : :
8981 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
8982 : : "lanname, lanpltrusted, lanplcallfoid, "
8983 : : "laninline, lanvalidator, "
8984 : : "lanacl, "
8985 : : "acldefault('l', lanowner) AS acldefault, "
8986 : : "lanowner "
8987 : : "FROM pg_language "
8988 : : "WHERE lanispl "
8989 : : "ORDER BY oid");
8990 : :
8991 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8992 : :
8993 : 215 : ntups = PQntuples(res);
8994 : :
8995 : 215 : planginfo = pg_malloc_array(ProcLangInfo, ntups);
8996 : :
8997 : 215 : i_tableoid = PQfnumber(res, "tableoid");
8998 : 215 : i_oid = PQfnumber(res, "oid");
8999 : 215 : i_lanname = PQfnumber(res, "lanname");
9000 : 215 : i_lanpltrusted = PQfnumber(res, "lanpltrusted");
9001 : 215 : i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
9002 : 215 : i_laninline = PQfnumber(res, "laninline");
9003 : 215 : i_lanvalidator = PQfnumber(res, "lanvalidator");
9004 : 215 : i_lanacl = PQfnumber(res, "lanacl");
9005 : 215 : i_acldefault = PQfnumber(res, "acldefault");
9006 : 215 : i_lanowner = PQfnumber(res, "lanowner");
9007 : :
9008 [ + + ]: 478 : for (i = 0; i < ntups; i++)
9009 : : {
9010 : 263 : planginfo[i].dobj.objType = DO_PROCLANG;
9011 : 263 : planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9012 : 263 : planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9013 : 263 : AssignDumpId(&planginfo[i].dobj);
9014 : :
9015 : 263 : planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
9016 : 263 : planginfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_lanacl));
9017 : 263 : planginfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
9018 : 263 : planginfo[i].dacl.privtype = 0;
9019 : 263 : planginfo[i].dacl.initprivs = NULL;
9020 : 263 : planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
9021 : 263 : planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
9022 : 263 : planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
9023 : 263 : planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
9024 : 263 : planginfo[i].lanowner = getRoleName(PQgetvalue(res, i, i_lanowner));
9025 : :
9026 : : /* Decide whether we want to dump it */
9027 : 263 : selectDumpableProcLang(&(planginfo[i]), fout);
9028 : :
9029 : : /* Mark whether language has an ACL */
9030 [ + + ]: 263 : if (!PQgetisnull(res, i, i_lanacl))
9031 : 61 : planginfo[i].dobj.components |= DUMP_COMPONENT_ACL;
9032 : : }
9033 : :
9034 : 215 : PQclear(res);
9035 : :
9036 : 215 : destroyPQExpBuffer(query);
9037 : 215 : }
9038 : :
9039 : : /*
9040 : : * getCasts
9041 : : * get basic information about most casts in the system
9042 : : *
9043 : : * Skip casts from a range to its multirange, since we'll create those
9044 : : * automatically.
9045 : : */
9046 : : void
9047 : 215 : getCasts(Archive *fout)
9048 : : {
9049 : : PGresult *res;
9050 : : int ntups;
9051 : : int i;
9052 : 215 : PQExpBuffer query = createPQExpBuffer();
9053 : : CastInfo *castinfo;
9054 : : int i_tableoid;
9055 : : int i_oid;
9056 : : int i_castsource;
9057 : : int i_casttarget;
9058 : : int i_castfunc;
9059 : : int i_castcontext;
9060 : : int i_castmethod;
9061 : :
9062 [ + - ]: 215 : if (fout->remoteVersion >= 140000)
9063 : : {
9064 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9065 : : "castsource, casttarget, castfunc, castcontext, "
9066 : : "castmethod "
9067 : : "FROM pg_cast c "
9068 : : "WHERE NOT EXISTS ( "
9069 : : "SELECT 1 FROM pg_range r "
9070 : : "WHERE c.castsource = r.rngtypid "
9071 : : "AND c.casttarget = r.rngmultitypid "
9072 : : ") "
9073 : : "ORDER BY 3,4");
9074 : : }
9075 : : else
9076 : : {
9077 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9078 : : "castsource, casttarget, castfunc, castcontext, "
9079 : : "castmethod "
9080 : : "FROM pg_cast ORDER BY 3,4");
9081 : : }
9082 : :
9083 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9084 : :
9085 : 215 : ntups = PQntuples(res);
9086 : :
9087 : 215 : castinfo = pg_malloc_array(CastInfo, ntups);
9088 : :
9089 : 215 : i_tableoid = PQfnumber(res, "tableoid");
9090 : 215 : i_oid = PQfnumber(res, "oid");
9091 : 215 : i_castsource = PQfnumber(res, "castsource");
9092 : 215 : i_casttarget = PQfnumber(res, "casttarget");
9093 : 215 : i_castfunc = PQfnumber(res, "castfunc");
9094 : 215 : i_castcontext = PQfnumber(res, "castcontext");
9095 : 215 : i_castmethod = PQfnumber(res, "castmethod");
9096 : :
9097 [ + + ]: 52550 : for (i = 0; i < ntups; i++)
9098 : : {
9099 : : PQExpBufferData namebuf;
9100 : : TypeInfo *sTypeInfo;
9101 : : TypeInfo *tTypeInfo;
9102 : :
9103 : 52335 : castinfo[i].dobj.objType = DO_CAST;
9104 : 52335 : castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9105 : 52335 : castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9106 : 52335 : AssignDumpId(&castinfo[i].dobj);
9107 : 52335 : castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
9108 : 52335 : castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
9109 : 52335 : castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
9110 : 52335 : castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
9111 : 52335 : castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod));
9112 : :
9113 : : /*
9114 : : * Try to name cast as concatenation of typnames. This is only used
9115 : : * for purposes of sorting. If we fail to find either type, the name
9116 : : * will be an empty string.
9117 : : */
9118 : 52335 : initPQExpBuffer(&namebuf);
9119 : 52335 : sTypeInfo = findTypeByOid(castinfo[i].castsource);
9120 : 52335 : tTypeInfo = findTypeByOid(castinfo[i].casttarget);
9121 [ + - + - ]: 52335 : if (sTypeInfo && tTypeInfo)
9122 : 52335 : appendPQExpBuffer(&namebuf, "%s %s",
9123 : : sTypeInfo->dobj.name, tTypeInfo->dobj.name);
9124 : 52335 : castinfo[i].dobj.name = namebuf.data;
9125 : :
9126 : : /* Decide whether we want to dump it */
9127 : 52335 : selectDumpableCast(&(castinfo[i]), fout);
9128 : : }
9129 : :
9130 : 215 : PQclear(res);
9131 : :
9132 : 215 : destroyPQExpBuffer(query);
9133 : 215 : }
9134 : :
9135 : : static char *
9136 : 93 : get_language_name(Archive *fout, Oid langid)
9137 : : {
9138 : : PQExpBuffer query;
9139 : : PGresult *res;
9140 : : char *lanname;
9141 : :
9142 : 93 : query = createPQExpBuffer();
9143 : 93 : appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid);
9144 : 93 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
9145 : 93 : lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
9146 : 93 : destroyPQExpBuffer(query);
9147 : 93 : PQclear(res);
9148 : :
9149 : 93 : return lanname;
9150 : : }
9151 : :
9152 : : /*
9153 : : * getTransforms
9154 : : * get basic information about every transform in the system
9155 : : */
9156 : : void
9157 : 215 : getTransforms(Archive *fout)
9158 : : {
9159 : : PGresult *res;
9160 : : int ntups;
9161 : : int i;
9162 : : PQExpBuffer query;
9163 : : TransformInfo *transforminfo;
9164 : : int i_tableoid;
9165 : : int i_oid;
9166 : : int i_trftype;
9167 : : int i_trflang;
9168 : : int i_trffromsql;
9169 : : int i_trftosql;
9170 : :
9171 : 215 : query = createPQExpBuffer();
9172 : :
9173 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9174 : : "trftype, trflang, trffromsql::oid, trftosql::oid "
9175 : : "FROM pg_transform "
9176 : : "ORDER BY 3,4");
9177 : :
9178 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9179 : :
9180 : 215 : ntups = PQntuples(res);
9181 : :
9182 : 215 : transforminfo = pg_malloc_array(TransformInfo, ntups);
9183 : :
9184 : 215 : i_tableoid = PQfnumber(res, "tableoid");
9185 : 215 : i_oid = PQfnumber(res, "oid");
9186 : 215 : i_trftype = PQfnumber(res, "trftype");
9187 : 215 : i_trflang = PQfnumber(res, "trflang");
9188 : 215 : i_trffromsql = PQfnumber(res, "trffromsql");
9189 : 215 : i_trftosql = PQfnumber(res, "trftosql");
9190 : :
9191 [ + + ]: 270 : for (i = 0; i < ntups; i++)
9192 : : {
9193 : : PQExpBufferData namebuf;
9194 : : TypeInfo *typeInfo;
9195 : : char *lanname;
9196 : :
9197 : 55 : transforminfo[i].dobj.objType = DO_TRANSFORM;
9198 : 55 : transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9199 : 55 : transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9200 : 55 : AssignDumpId(&transforminfo[i].dobj);
9201 : 55 : transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype));
9202 : 55 : transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang));
9203 : 55 : transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql));
9204 : 55 : transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql));
9205 : :
9206 : : /*
9207 : : * Try to name transform as concatenation of type and language name.
9208 : : * This is only used for purposes of sorting. If we fail to find
9209 : : * either, the name will be an empty string.
9210 : : */
9211 : 55 : initPQExpBuffer(&namebuf);
9212 : 55 : typeInfo = findTypeByOid(transforminfo[i].trftype);
9213 : 55 : lanname = get_language_name(fout, transforminfo[i].trflang);
9214 [ + - + - ]: 55 : if (typeInfo && lanname)
9215 : 55 : appendPQExpBuffer(&namebuf, "%s %s",
9216 : : typeInfo->dobj.name, lanname);
9217 : 55 : transforminfo[i].dobj.name = namebuf.data;
9218 : 55 : free(lanname);
9219 : :
9220 : : /* Decide whether we want to dump it */
9221 : 55 : selectDumpableObject(&(transforminfo[i].dobj), fout);
9222 : : }
9223 : :
9224 : 215 : PQclear(res);
9225 : :
9226 : 215 : destroyPQExpBuffer(query);
9227 : 215 : }
9228 : :
9229 : : /*
9230 : : * getTableAttrs -
9231 : : * for each interesting table, read info about its attributes
9232 : : * (names, types, default values, CHECK constraints, etc)
9233 : : *
9234 : : * modifies tblinfo
9235 : : */
9236 : : void
9237 : 215 : getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
9238 : : {
9239 : 215 : DumpOptions *dopt = fout->dopt;
9240 : 215 : PQExpBuffer q = createPQExpBuffer();
9241 : 215 : PQExpBuffer tbloids = createPQExpBuffer();
9242 : 215 : PQExpBuffer checkoids = createPQExpBuffer();
9243 : 215 : PQExpBuffer invalidnotnulloids = NULL;
9244 : : PGresult *res;
9245 : : int ntups;
9246 : : int curtblindx;
9247 : : int i_attrelid;
9248 : : int i_attnum;
9249 : : int i_attname;
9250 : : int i_atttypname;
9251 : : int i_attstattarget;
9252 : : int i_attstorage;
9253 : : int i_typstorage;
9254 : : int i_attidentity;
9255 : : int i_attgenerated;
9256 : : int i_attisdropped;
9257 : : int i_attlen;
9258 : : int i_attalign;
9259 : : int i_attislocal;
9260 : : int i_notnull_name;
9261 : : int i_notnull_comment;
9262 : : int i_notnull_noinherit;
9263 : : int i_notnull_islocal;
9264 : : int i_notnull_invalidoid;
9265 : : int i_attoptions;
9266 : : int i_attversion;
9267 : : int i_attcollation;
9268 : : int i_attcompression;
9269 : : int i_attfdwoptions;
9270 : : int i_attmissingval;
9271 : : int i_atthasdef;
9272 : :
9273 : : /*
9274 : : * We want to perform just one query against pg_attribute, and then just
9275 : : * one against pg_attrdef (for DEFAULTs) and two against pg_constraint
9276 : : * (for CHECK constraints and for NOT NULL constraints). However, we
9277 : : * mustn't try to select every row of those catalogs and then sort it out
9278 : : * on the client side, because some of the server-side functions we need
9279 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
9280 : : * build an array of the OIDs of tables we care about (and now have lock
9281 : : * on!), and use a WHERE clause to constrain which rows are selected.
9282 : : */
9283 : 215 : appendPQExpBufferChar(tbloids, '{');
9284 : 215 : appendPQExpBufferChar(checkoids, '{');
9285 [ + + ]: 57691 : for (int i = 0; i < numTables; i++)
9286 : : {
9287 : 57476 : TableInfo *tbinfo = &tblinfo[i];
9288 : :
9289 : : /* Don't bother to collect info for sequences */
9290 [ + + ]: 57476 : if (tbinfo->relkind == RELKIND_SEQUENCE)
9291 : 663 : continue;
9292 : :
9293 : : /*
9294 : : * Don't bother with uninteresting tables, either. For binary
9295 : : * upgrades, this is bypassed for pg_largeobject_metadata and
9296 : : * pg_shdepend so that the columns names are collected for the
9297 : : * corresponding COPY commands. Restoring the data for those catalogs
9298 : : * is faster than restoring the equivalent set of large object
9299 : : * commands.
9300 : : */
9301 [ + + ]: 56813 : if (!tbinfo->interesting &&
9302 [ + + ]: 49568 : !(fout->dopt->binary_upgrade &&
9303 [ + + ]: 9750 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9304 [ + + ]: 9706 : tbinfo->dobj.catId.oid == SharedDependRelationId)))
9305 : 49480 : continue;
9306 : :
9307 : : /* OK, we need info for this table */
9308 [ + + ]: 7333 : if (tbloids->len > 1) /* do we have more than the '{'? */
9309 : 7164 : appendPQExpBufferChar(tbloids, ',');
9310 : 7333 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9311 : :
9312 [ + + ]: 7333 : if (tbinfo->ncheck > 0)
9313 : : {
9314 : : /* Also make a list of the ones with check constraints */
9315 [ + + ]: 548 : if (checkoids->len > 1) /* do we have more than the '{'? */
9316 : 476 : appendPQExpBufferChar(checkoids, ',');
9317 : 548 : appendPQExpBuffer(checkoids, "%u", tbinfo->dobj.catId.oid);
9318 : : }
9319 : : }
9320 : 215 : appendPQExpBufferChar(tbloids, '}');
9321 : 215 : appendPQExpBufferChar(checkoids, '}');
9322 : :
9323 : : /*
9324 : : * Find all the user attributes and their types.
9325 : : *
9326 : : * Since we only want to dump COLLATE clauses for attributes whose
9327 : : * collation is different from their type's default, we use a CASE here to
9328 : : * suppress uninteresting attcollations cheaply.
9329 : : */
9330 : 215 : appendPQExpBufferStr(q,
9331 : : "SELECT\n"
9332 : : "a.attrelid,\n"
9333 : : "a.attnum,\n"
9334 : : "a.attname,\n"
9335 : : "a.attstattarget,\n"
9336 : : "a.attstorage,\n"
9337 : : "t.typstorage,\n"
9338 : : "a.atthasdef,\n"
9339 : : "a.attisdropped,\n"
9340 : : "a.attlen,\n"
9341 : : "a.attalign,\n"
9342 : : "a.attislocal,\n"
9343 : : "pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n");
9344 : :
9345 : : /*
9346 : : * Read attoptions and attversion.
9347 : : *
9348 : : * Attversion is stored as a special attoptions value in the
9349 : : * back-branches. In that case we need to remove the special attoptions
9350 : : * value.
9351 : : *
9352 : : * Attribute versioning may also have been backpatched to versions older
9353 : : * than the ones it was backpatched to upstream, so do this for all server
9354 : : * versions, without using functions missing in old versions (like
9355 : : * starts_with()).
9356 : : */
9357 [ + - ]: 215 : if (fout->remoteVersion >= 200000)
9358 : 215 : appendPQExpBufferStr(q,
9359 : : "array_to_string(a.attoptions, ', ') AS attoptions,\n"
9360 : : "a.attversion - '0/0' AS attversion,\n");
9361 : : else
9362 : 0 : appendPQExpBuffer(q,
9363 : : "array_to_string(ARRAY(\n"
9364 : : " SELECT o FROM unnest(a.attoptions) o\n"
9365 : : " WHERE substr(o, 1, %2$zu) <> '%1$s='\n"
9366 : : "), ', ') AS attoptions,\n"
9367 : : "(\n"
9368 : : " SELECT substr(o, %2$zu + 1)\n"
9369 : : " FROM unnest(a.attoptions) o\n"
9370 : : " WHERE substr(o, 1, %2$zu) = '%1$s='\n"
9371 : : ") AS attversion,\n",
9372 : : ATTRIBUTE_VERSION_ATTOPTION,
9373 : : strlen(ATTRIBUTE_VERSION_ATTOPTION) + 1);
9374 : :
9375 : 215 : appendPQExpBufferStr(q,
9376 : : "CASE WHEN a.attcollation <> t.typcollation "
9377 : : "THEN a.attcollation ELSE 0 END AS attcollation,\n"
9378 : : "pg_catalog.array_to_string(ARRAY("
9379 : : "SELECT pg_catalog.quote_ident(option_name) || "
9380 : : "' ' || pg_catalog.quote_literal(option_value) "
9381 : : "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
9382 : : "ORDER BY option_name"
9383 : : "), E',\n ') AS attfdwoptions,\n");
9384 : :
9385 : : /*
9386 : : * Find out any NOT NULL markings for each column. In 18 and up we read
9387 : : * pg_constraint to obtain the constraint name, and for valid constraints
9388 : : * also pg_description to obtain its comment. notnull_noinherit is set
9389 : : * according to the NO INHERIT property. For versions prior to 18, we
9390 : : * store an empty string as the name when a constraint is marked as
9391 : : * attnotnull (this cues dumpTableSchema to print the NOT NULL clause
9392 : : * without a name); also, such cases are never NO INHERIT.
9393 : : *
9394 : : * For invalid constraints, we need to store their OIDs for processing
9395 : : * elsewhere, so we bring the pg_constraint.oid value when the constraint
9396 : : * is invalid, and NULL otherwise. Their comments are handled not here
9397 : : * but by collectComments, because they're their own dumpable object.
9398 : : *
9399 : : * We track in notnull_islocal whether the constraint was defined directly
9400 : : * in this table or via an ancestor, for binary upgrade. flagInhAttrs
9401 : : * might modify this later.
9402 : : */
9403 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
9404 : 215 : appendPQExpBufferStr(q,
9405 : : "co.conname AS notnull_name,\n"
9406 : : "CASE WHEN co.convalidated THEN pt.description"
9407 : : " ELSE NULL END AS notnull_comment,\n"
9408 : : "CASE WHEN NOT co.convalidated THEN co.oid "
9409 : : "ELSE NULL END AS notnull_invalidoid,\n"
9410 : : "co.connoinherit AS notnull_noinherit,\n"
9411 : : "co.conislocal AS notnull_islocal,\n");
9412 : : else
9413 : 0 : appendPQExpBufferStr(q,
9414 : : "CASE WHEN a.attnotnull THEN '' ELSE NULL END AS notnull_name,\n"
9415 : : "NULL AS notnull_comment,\n"
9416 : : "NULL AS notnull_invalidoid,\n"
9417 : : "false AS notnull_noinherit,\n"
9418 : : "CASE WHEN a.attislocal THEN true\n"
9419 : : " WHEN a.attnotnull AND NOT a.attislocal THEN true\n"
9420 : : " ELSE false\n"
9421 : : "END AS notnull_islocal,\n");
9422 : :
9423 [ + - ]: 215 : if (fout->remoteVersion >= 140000)
9424 : 215 : appendPQExpBufferStr(q,
9425 : : "a.attcompression AS attcompression,\n");
9426 : : else
9427 : 0 : appendPQExpBufferStr(q,
9428 : : "'' AS attcompression,\n");
9429 : :
9430 : 215 : appendPQExpBufferStr(q,
9431 : : "a.attidentity,\n");
9432 : :
9433 [ + - ]: 215 : if (fout->remoteVersion >= 110000)
9434 : 215 : appendPQExpBufferStr(q,
9435 : : "CASE WHEN a.atthasmissing AND NOT a.attisdropped "
9436 : : "THEN a.attmissingval ELSE null END AS attmissingval,\n");
9437 : : else
9438 : 0 : appendPQExpBufferStr(q,
9439 : : "NULL AS attmissingval,\n");
9440 : :
9441 [ + - ]: 215 : if (fout->remoteVersion >= 120000)
9442 : 215 : appendPQExpBufferStr(q,
9443 : : "a.attgenerated\n");
9444 : : else
9445 : 0 : appendPQExpBufferStr(q,
9446 : : "'' AS attgenerated\n");
9447 : :
9448 : : /* need left join to pg_type to not fail on dropped columns ... */
9449 : 215 : appendPQExpBuffer(q,
9450 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9451 : : "JOIN pg_catalog.pg_attribute a ON (src.tbloid = a.attrelid) "
9452 : : "LEFT JOIN pg_catalog.pg_type t "
9453 : : "ON (a.atttypid = t.oid)\n",
9454 : : tbloids->data);
9455 : :
9456 : : /*
9457 : : * In versions 18 and up, we need pg_constraint for explicit NOT NULL
9458 : : * entries and pg_description to get their comments.
9459 : : */
9460 [ + - ]: 215 : if (fout->remoteVersion >= 180000)
9461 : 215 : appendPQExpBufferStr(q,
9462 : : " LEFT JOIN pg_catalog.pg_constraint co ON "
9463 : : "(a.attrelid = co.conrelid\n"
9464 : : " AND co.contype = 'n' AND "
9465 : : "co.conkey = array[a.attnum])\n"
9466 : : " LEFT JOIN pg_catalog.pg_description pt ON "
9467 : : "(pt.classoid = co.tableoid AND pt.objoid = co.oid)\n");
9468 : :
9469 : 215 : appendPQExpBufferStr(q,
9470 : : "WHERE a.attnum > 0::pg_catalog.int2\n");
9471 : :
9472 : : /*
9473 : : * For binary upgrades from <v12, be sure to pick up
9474 : : * pg_largeobject_metadata's oid column.
9475 : : */
9476 [ + + - + ]: 215 : if (fout->dopt->binary_upgrade && fout->remoteVersion < 120000)
9477 : 0 : appendPQExpBufferStr(q,
9478 : : "OR (a.attnum = -2::pg_catalog.int2 AND src.tbloid = "
9479 : : CppAsString2(LargeObjectMetadataRelationId) ")\n");
9480 : :
9481 : 215 : appendPQExpBufferStr(q,
9482 : : "ORDER BY a.attrelid, a.attnum");
9483 : :
9484 : 215 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9485 : :
9486 : 215 : ntups = PQntuples(res);
9487 : :
9488 : 215 : i_attrelid = PQfnumber(res, "attrelid");
9489 : 215 : i_attnum = PQfnumber(res, "attnum");
9490 : 215 : i_attname = PQfnumber(res, "attname");
9491 : 215 : i_atttypname = PQfnumber(res, "atttypname");
9492 : 215 : i_attstattarget = PQfnumber(res, "attstattarget");
9493 : 215 : i_attstorage = PQfnumber(res, "attstorage");
9494 : 215 : i_typstorage = PQfnumber(res, "typstorage");
9495 : 215 : i_attidentity = PQfnumber(res, "attidentity");
9496 : 215 : i_attgenerated = PQfnumber(res, "attgenerated");
9497 : 215 : i_attisdropped = PQfnumber(res, "attisdropped");
9498 : 215 : i_attlen = PQfnumber(res, "attlen");
9499 : 215 : i_attalign = PQfnumber(res, "attalign");
9500 : 215 : i_attislocal = PQfnumber(res, "attislocal");
9501 : 215 : i_notnull_name = PQfnumber(res, "notnull_name");
9502 : 215 : i_notnull_comment = PQfnumber(res, "notnull_comment");
9503 : 215 : i_notnull_invalidoid = PQfnumber(res, "notnull_invalidoid");
9504 : 215 : i_notnull_noinherit = PQfnumber(res, "notnull_noinherit");
9505 : 215 : i_notnull_islocal = PQfnumber(res, "notnull_islocal");
9506 : 215 : i_attoptions = PQfnumber(res, "attoptions");
9507 : 215 : i_attversion = PQfnumber(res, "attversion");
9508 : 215 : i_attcollation = PQfnumber(res, "attcollation");
9509 : 215 : i_attcompression = PQfnumber(res, "attcompression");
9510 : 215 : i_attfdwoptions = PQfnumber(res, "attfdwoptions");
9511 : 215 : i_attmissingval = PQfnumber(res, "attmissingval");
9512 : 215 : i_atthasdef = PQfnumber(res, "atthasdef");
9513 : :
9514 : : /* Within the next loop, we'll accumulate OIDs of tables with defaults */
9515 : 215 : resetPQExpBuffer(tbloids);
9516 : 215 : appendPQExpBufferChar(tbloids, '{');
9517 : :
9518 : : /*
9519 : : * Outer loop iterates once per table, not once per row. Incrementing of
9520 : : * r is handled by the inner loop.
9521 : : */
9522 : 215 : curtblindx = -1;
9523 [ + + ]: 7401 : for (int r = 0; r < ntups;)
9524 : : {
9525 : 7186 : Oid attrelid = atooid(PQgetvalue(res, r, i_attrelid));
9526 : 7186 : TableInfo *tbinfo = NULL;
9527 : : int numatts;
9528 : : bool hasdefaults;
9529 : :
9530 : : /* Count rows for this table */
9531 [ + + ]: 26494 : for (numatts = 1; numatts < ntups - r; numatts++)
9532 [ + + ]: 26328 : if (atooid(PQgetvalue(res, r + numatts, i_attrelid)) != attrelid)
9533 : 7020 : break;
9534 : :
9535 : : /*
9536 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
9537 : : * order.
9538 : : */
9539 [ + - ]: 40319 : while (++curtblindx < numTables)
9540 : : {
9541 : 40319 : tbinfo = &tblinfo[curtblindx];
9542 [ + + ]: 40319 : if (tbinfo->dobj.catId.oid == attrelid)
9543 : 7186 : break;
9544 : : }
9545 [ - + ]: 7186 : if (curtblindx >= numTables)
9546 : 0 : pg_fatal("unrecognized table OID %u", attrelid);
9547 : : /* cross-check that we only got requested tables */
9548 [ + - ]: 7186 : if (tbinfo->relkind == RELKIND_SEQUENCE ||
9549 [ + + ]: 7186 : (!tbinfo->interesting &&
9550 [ + - ]: 88 : !(fout->dopt->binary_upgrade &&
9551 [ + + ]: 88 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9552 [ - + ]: 44 : tbinfo->dobj.catId.oid == SharedDependRelationId))))
9553 : 0 : pg_fatal("unexpected column data for table \"%s\"",
9554 : : tbinfo->dobj.name);
9555 : :
9556 : : /* Save data for this table */
9557 : 7186 : tbinfo->numatts = numatts;
9558 : 7186 : tbinfo->attnames = pg_malloc_array(char *, numatts);
9559 : 7186 : tbinfo->atttypnames = pg_malloc_array(char *, numatts);
9560 : 7186 : tbinfo->attstattarget = pg_malloc_array(int, numatts);
9561 : 7186 : tbinfo->attstorage = pg_malloc_array(char, numatts);
9562 : 7186 : tbinfo->typstorage = pg_malloc_array(char, numatts);
9563 : 7186 : tbinfo->attidentity = pg_malloc_array(char, numatts);
9564 : 7186 : tbinfo->attgenerated = pg_malloc_array(char, numatts);
9565 : 7186 : tbinfo->attisdropped = pg_malloc_array(bool, numatts);
9566 : 7186 : tbinfo->attlen = pg_malloc_array(int, numatts);
9567 : 7186 : tbinfo->attalign = pg_malloc_array(char, numatts);
9568 : 7186 : tbinfo->attislocal = pg_malloc_array(bool, numatts);
9569 : 7186 : tbinfo->attoptions = pg_malloc_array(char *, numatts);
9570 : 7186 : tbinfo->attversion = pg_malloc_array(uint64, numatts);
9571 : 7186 : tbinfo->attcollation = pg_malloc_array(Oid, numatts);
9572 : 7186 : tbinfo->attcompression = pg_malloc_array(char, numatts);
9573 : 7186 : tbinfo->attfdwoptions = pg_malloc_array(char *, numatts);
9574 : 7186 : tbinfo->attmissingval = pg_malloc_array(char *, numatts);
9575 : 7186 : tbinfo->notnull_constrs = pg_malloc_array(char *, numatts);
9576 : 7186 : tbinfo->notnull_comment = pg_malloc_array(char *, numatts);
9577 : 7186 : tbinfo->notnull_invalid = pg_malloc_array(bool, numatts);
9578 : 7186 : tbinfo->notnull_noinh = pg_malloc_array(bool, numatts);
9579 : 7186 : tbinfo->notnull_islocal = pg_malloc_array(bool, numatts);
9580 : 7186 : tbinfo->attrdefs = pg_malloc_array(AttrDefInfo *, numatts);
9581 : 7186 : hasdefaults = false;
9582 : :
9583 [ + + ]: 33680 : for (int j = 0; j < numatts; j++, r++)
9584 : : {
9585 [ - + ]: 26494 : if (j + 1 != atoi(PQgetvalue(res, r, i_attnum)) &&
9586 [ # # # # ]: 0 : !(fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
9587 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
9588 : 0 : pg_fatal("invalid column numbering in table \"%s\"",
9589 : : tbinfo->dobj.name);
9590 : 26494 : tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, r, i_attname));
9591 : 26494 : tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, r, i_atttypname));
9592 [ + + ]: 26494 : if (PQgetisnull(res, r, i_attstattarget))
9593 : 26451 : tbinfo->attstattarget[j] = -1;
9594 : : else
9595 : 43 : tbinfo->attstattarget[j] = atoi(PQgetvalue(res, r, i_attstattarget));
9596 : 26494 : tbinfo->attstorage[j] = *(PQgetvalue(res, r, i_attstorage));
9597 : 26494 : tbinfo->typstorage[j] = *(PQgetvalue(res, r, i_typstorage));
9598 : 26494 : tbinfo->attidentity[j] = *(PQgetvalue(res, r, i_attidentity));
9599 : 26494 : tbinfo->attgenerated[j] = *(PQgetvalue(res, r, i_attgenerated));
9600 : 26494 : tbinfo->attisdropped[j] = (PQgetvalue(res, r, i_attisdropped)[0] == 't');
9601 : :
9602 : : /*
9603 : : * ALTER TABLE DROP COLUMN does not clear attidentity, so we must
9604 : : * ignore dropped columns here or we'd emit a spurious OVERRIDING
9605 : : * SYSTEM VALUE clause for the table's data.
9606 : : */
9607 [ + + ]: 26494 : if (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS &&
9608 [ + + ]: 178 : !tbinfo->attisdropped[j])
9609 : 137 : tbinfo->needs_override = true;
9610 : :
9611 : 26494 : tbinfo->attlen[j] = atoi(PQgetvalue(res, r, i_attlen));
9612 : 26494 : tbinfo->attalign[j] = *(PQgetvalue(res, r, i_attalign));
9613 : 26494 : tbinfo->attislocal[j] = (PQgetvalue(res, r, i_attislocal)[0] == 't');
9614 : :
9615 : : /* Handle not-null constraint name and flags */
9616 : 26494 : determineNotNullFlags(fout, res, r,
9617 : : tbinfo, j,
9618 : : i_notnull_name,
9619 : : i_notnull_comment,
9620 : : i_notnull_invalidoid,
9621 : : i_notnull_noinherit,
9622 : : i_notnull_islocal,
9623 : : &invalidnotnulloids);
9624 : :
9625 : 26494 : tbinfo->notnull_comment[j] = PQgetisnull(res, r, i_notnull_comment) ?
9626 [ + + ]: 26494 : NULL : pg_strdup(PQgetvalue(res, r, i_notnull_comment));
9627 : 26494 : tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, r, i_attoptions));
9628 [ - + ]: 26494 : if (PQgetisnull(res, r, i_attversion))
9629 : 0 : tbinfo->attversion[j] = 0;
9630 : : else
9631 : 26494 : tbinfo->attversion[j] = strtou64(PQgetvalue(res, r, i_attversion), NULL, 10);
9632 : 26494 : tbinfo->attcollation[j] = atooid(PQgetvalue(res, r, i_attcollation));
9633 : 26494 : tbinfo->attcompression[j] = *(PQgetvalue(res, r, i_attcompression));
9634 : 26494 : tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, r, i_attfdwoptions));
9635 : 26494 : tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, r, i_attmissingval));
9636 : 26494 : tbinfo->attrdefs[j] = NULL; /* fix below */
9637 [ + + ]: 26494 : if (PQgetvalue(res, r, i_atthasdef)[0] == 't')
9638 : 1398 : hasdefaults = true;
9639 : : }
9640 : :
9641 [ + + ]: 7186 : if (hasdefaults)
9642 : : {
9643 : : /* Collect OIDs of interesting tables that have defaults */
9644 [ + + ]: 1048 : if (tbloids->len > 1) /* do we have more than the '{'? */
9645 : 961 : appendPQExpBufferChar(tbloids, ',');
9646 : 1048 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9647 : : }
9648 : : }
9649 : :
9650 : : /* If invalidnotnulloids has any data, finalize it */
9651 [ + + ]: 215 : if (invalidnotnulloids != NULL)
9652 : 46 : appendPQExpBufferChar(invalidnotnulloids, '}');
9653 : :
9654 : 215 : PQclear(res);
9655 : :
9656 : : /*
9657 : : * Now get info about column defaults. This is skipped for a data-only
9658 : : * dump, as it is only needed for table schemas.
9659 : : */
9660 [ + + + + ]: 215 : if (dopt->dumpSchema && tbloids->len > 1)
9661 : : {
9662 : : AttrDefInfo *attrdefs;
9663 : : int numDefaults;
9664 : 78 : TableInfo *tbinfo = NULL;
9665 : :
9666 : 78 : pg_log_info("finding table default expressions");
9667 : :
9668 : 78 : appendPQExpBufferChar(tbloids, '}');
9669 : :
9670 : 78 : printfPQExpBuffer(q, "SELECT a.tableoid, a.oid, adrelid, adnum, "
9671 : : "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc\n"
9672 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9673 : : "JOIN pg_catalog.pg_attrdef a ON (src.tbloid = a.adrelid)\n"
9674 : : "ORDER BY a.adrelid, a.adnum",
9675 : : tbloids->data);
9676 : :
9677 : 78 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9678 : :
9679 : 78 : numDefaults = PQntuples(res);
9680 : 78 : attrdefs = pg_malloc_array(AttrDefInfo, numDefaults);
9681 : :
9682 : 78 : curtblindx = -1;
9683 [ + + ]: 1367 : for (int j = 0; j < numDefaults; j++)
9684 : : {
9685 : 1289 : Oid adtableoid = atooid(PQgetvalue(res, j, 0));
9686 : 1289 : Oid adoid = atooid(PQgetvalue(res, j, 1));
9687 : 1289 : Oid adrelid = atooid(PQgetvalue(res, j, 2));
9688 : 1289 : int adnum = atoi(PQgetvalue(res, j, 3));
9689 : 1289 : char *adsrc = PQgetvalue(res, j, 4);
9690 : :
9691 : : /*
9692 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9693 : : * OID order.
9694 : : */
9695 [ + + + + ]: 1289 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != adrelid)
9696 : : {
9697 [ + - ]: 23558 : while (++curtblindx < numTables)
9698 : : {
9699 : 23558 : tbinfo = &tblinfo[curtblindx];
9700 [ + + ]: 23558 : if (tbinfo->dobj.catId.oid == adrelid)
9701 : 973 : break;
9702 : : }
9703 [ - + ]: 973 : if (curtblindx >= numTables)
9704 : 0 : pg_fatal("unrecognized table OID %u", adrelid);
9705 : : }
9706 : :
9707 [ + - - + ]: 1289 : if (adnum <= 0 || adnum > tbinfo->numatts)
9708 : 0 : pg_fatal("invalid adnum value %d for table \"%s\"",
9709 : : adnum, tbinfo->dobj.name);
9710 : :
9711 : : /*
9712 : : * dropped columns shouldn't have defaults, but just in case,
9713 : : * ignore 'em
9714 : : */
9715 [ - + ]: 1289 : if (tbinfo->attisdropped[adnum - 1])
9716 : 0 : continue;
9717 : :
9718 : 1289 : attrdefs[j].dobj.objType = DO_ATTRDEF;
9719 : 1289 : attrdefs[j].dobj.catId.tableoid = adtableoid;
9720 : 1289 : attrdefs[j].dobj.catId.oid = adoid;
9721 : 1289 : AssignDumpId(&attrdefs[j].dobj);
9722 : 1289 : attrdefs[j].adtable = tbinfo;
9723 : 1289 : attrdefs[j].adnum = adnum;
9724 : 1289 : attrdefs[j].adef_expr = pg_strdup(adsrc);
9725 : :
9726 : 1289 : attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name);
9727 : 1289 : attrdefs[j].dobj.namespace = tbinfo->dobj.namespace;
9728 : :
9729 : 1289 : attrdefs[j].dobj.dump = tbinfo->dobj.dump;
9730 : :
9731 : : /*
9732 : : * Figure out whether the default/generation expression should be
9733 : : * dumped as part of the main CREATE TABLE (or similar) command or
9734 : : * as a separate ALTER TABLE (or similar) command. The preference
9735 : : * is to put it into the CREATE command, but in some cases that's
9736 : : * not possible.
9737 : : */
9738 [ + + ]: 1289 : if (tbinfo->attgenerated[adnum - 1])
9739 : : {
9740 : : /*
9741 : : * Column generation expressions cannot be dumped separately,
9742 : : * because there is no syntax for it. By setting separate to
9743 : : * false here we prevent the "default" from being processed as
9744 : : * its own dumpable object. Later, flagInhAttrs() will mark
9745 : : * it as not to be dumped at all, if possible (that is, if it
9746 : : * can be inherited from a parent).
9747 : : */
9748 : 754 : attrdefs[j].separate = false;
9749 : : }
9750 [ + + ]: 535 : else if (tbinfo->relkind == RELKIND_VIEW)
9751 : : {
9752 : : /*
9753 : : * Defaults on a VIEW must always be dumped as separate ALTER
9754 : : * TABLE commands.
9755 : : */
9756 : 34 : attrdefs[j].separate = true;
9757 : : }
9758 [ + + ]: 501 : else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1))
9759 : : {
9760 : : /* column will be suppressed, print default separately */
9761 : 4 : attrdefs[j].separate = true;
9762 : : }
9763 : : else
9764 : : {
9765 : 497 : attrdefs[j].separate = false;
9766 : : }
9767 : :
9768 [ + + ]: 1289 : if (!attrdefs[j].separate)
9769 : : {
9770 : : /*
9771 : : * Mark the default as needing to appear before the table, so
9772 : : * that any dependencies it has must be emitted before the
9773 : : * CREATE TABLE. If this is not possible, we'll change to
9774 : : * "separate" mode while sorting dependencies.
9775 : : */
9776 : 1251 : addObjectDependency(&tbinfo->dobj,
9777 : 1251 : attrdefs[j].dobj.dumpId);
9778 : : }
9779 : :
9780 : 1289 : tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
9781 : : }
9782 : :
9783 : 78 : PQclear(res);
9784 : : }
9785 : :
9786 : : /*
9787 : : * Get info about NOT NULL NOT VALID constraints. This is skipped for a
9788 : : * data-only dump, as it is only needed for table schemas.
9789 : : */
9790 [ + + + + ]: 215 : if (dopt->dumpSchema && invalidnotnulloids)
9791 : : {
9792 : : ConstraintInfo *constrs;
9793 : : int numConstrs;
9794 : : int i_tableoid;
9795 : : int i_oid;
9796 : : int i_conrelid;
9797 : : int i_conname;
9798 : : int i_consrc;
9799 : : int i_conislocal;
9800 : :
9801 : 39 : pg_log_info("finding invalid not-null constraints");
9802 : :
9803 : 39 : resetPQExpBuffer(q);
9804 : 39 : appendPQExpBuffer(q,
9805 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9806 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9807 : : "conislocal, convalidated "
9808 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(conoid)\n"
9809 : : "JOIN pg_catalog.pg_constraint c ON (src.conoid = c.oid)\n"
9810 : : "ORDER BY c.conrelid, c.conname",
9811 : 39 : invalidnotnulloids->data);
9812 : :
9813 : 39 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9814 : :
9815 : 39 : numConstrs = PQntuples(res);
9816 : 39 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9817 : :
9818 : 39 : i_tableoid = PQfnumber(res, "tableoid");
9819 : 39 : i_oid = PQfnumber(res, "oid");
9820 : 39 : i_conrelid = PQfnumber(res, "conrelid");
9821 : 39 : i_conname = PQfnumber(res, "conname");
9822 : 39 : i_consrc = PQfnumber(res, "consrc");
9823 : 39 : i_conislocal = PQfnumber(res, "conislocal");
9824 : :
9825 : : /* As above, this loop iterates once per table, not once per row */
9826 : 39 : curtblindx = -1;
9827 [ + + ]: 108 : for (int j = 0; j < numConstrs;)
9828 : : {
9829 : 69 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9830 : 69 : TableInfo *tbinfo = NULL;
9831 : : int numcons;
9832 : :
9833 : : /* Count rows for this table */
9834 [ + + ]: 69 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9835 [ + - ]: 30 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9836 : 30 : break;
9837 : :
9838 : : /*
9839 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9840 : : * OID order.
9841 : : */
9842 [ + - ]: 13618 : while (++curtblindx < numTables)
9843 : : {
9844 : 13618 : tbinfo = &tblinfo[curtblindx];
9845 [ + + ]: 13618 : if (tbinfo->dobj.catId.oid == conrelid)
9846 : 69 : break;
9847 : : }
9848 [ - + ]: 69 : if (curtblindx >= numTables)
9849 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9850 : :
9851 [ + + ]: 138 : for (int c = 0; c < numcons; c++, j++)
9852 : : {
9853 : 69 : constrs[j].dobj.objType = DO_CONSTRAINT;
9854 : 69 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9855 : 69 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9856 : 69 : AssignDumpId(&constrs[j].dobj);
9857 : 69 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9858 : 69 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9859 : 69 : constrs[j].contable = tbinfo;
9860 : 69 : constrs[j].condomain = NULL;
9861 : 69 : constrs[j].contype = 'n';
9862 : 69 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9863 : 69 : constrs[j].confrelid = InvalidOid;
9864 : 69 : constrs[j].conindex = 0;
9865 : 69 : constrs[j].condeferrable = false;
9866 : 69 : constrs[j].condeferred = false;
9867 : 69 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9868 : :
9869 : : /*
9870 : : * All invalid not-null constraints must be dumped separately,
9871 : : * because CREATE TABLE would not create them as invalid, and
9872 : : * also because they must be created after potentially
9873 : : * violating data has been loaded.
9874 : : */
9875 : 69 : constrs[j].separate = true;
9876 : :
9877 : 69 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9878 : : }
9879 : : }
9880 : 39 : PQclear(res);
9881 : : }
9882 : :
9883 : : /*
9884 : : * Get info about table CHECK constraints. This is skipped for a
9885 : : * data-only dump, as it is only needed for table schemas.
9886 : : */
9887 [ + + + + ]: 215 : if (dopt->dumpSchema && checkoids->len > 2)
9888 : : {
9889 : : ConstraintInfo *constrs;
9890 : : int numConstrs;
9891 : : int i_tableoid;
9892 : : int i_oid;
9893 : : int i_conrelid;
9894 : : int i_conname;
9895 : : int i_consrc;
9896 : : int i_conislocal;
9897 : : int i_convalidated;
9898 : :
9899 : 63 : pg_log_info("finding table check constraints");
9900 : :
9901 : 63 : resetPQExpBuffer(q);
9902 : 63 : appendPQExpBuffer(q,
9903 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9904 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9905 : : "conislocal, convalidated "
9906 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9907 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
9908 : : "WHERE contype = 'c' "
9909 : : "ORDER BY c.conrelid, c.conname",
9910 : : checkoids->data);
9911 : :
9912 : 63 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9913 : :
9914 : 63 : numConstrs = PQntuples(res);
9915 : 63 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9916 : :
9917 : 63 : i_tableoid = PQfnumber(res, "tableoid");
9918 : 63 : i_oid = PQfnumber(res, "oid");
9919 : 63 : i_conrelid = PQfnumber(res, "conrelid");
9920 : 63 : i_conname = PQfnumber(res, "conname");
9921 : 63 : i_consrc = PQfnumber(res, "consrc");
9922 : 63 : i_conislocal = PQfnumber(res, "conislocal");
9923 : 63 : i_convalidated = PQfnumber(res, "convalidated");
9924 : :
9925 : : /* As above, this loop iterates once per table, not once per row */
9926 : 63 : curtblindx = -1;
9927 [ + + ]: 556 : for (int j = 0; j < numConstrs;)
9928 : : {
9929 : 493 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9930 : 493 : TableInfo *tbinfo = NULL;
9931 : : int numcons;
9932 : :
9933 : : /* Count rows for this table */
9934 [ + + ]: 632 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9935 [ + + ]: 569 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9936 : 430 : break;
9937 : :
9938 : : /*
9939 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9940 : : * OID order.
9941 : : */
9942 [ + - ]: 19184 : while (++curtblindx < numTables)
9943 : : {
9944 : 19184 : tbinfo = &tblinfo[curtblindx];
9945 [ + + ]: 19184 : if (tbinfo->dobj.catId.oid == conrelid)
9946 : 493 : break;
9947 : : }
9948 [ - + ]: 493 : if (curtblindx >= numTables)
9949 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9950 : :
9951 [ - + ]: 493 : if (numcons != tbinfo->ncheck)
9952 : : {
9953 : 0 : pg_log_error(ngettext("expected %d check constraint on table \"%s\" but found %d",
9954 : : "expected %d check constraints on table \"%s\" but found %d",
9955 : : tbinfo->ncheck),
9956 : : tbinfo->ncheck, tbinfo->dobj.name, numcons);
9957 : 0 : pg_log_error_hint("The system catalogs might be corrupted.");
9958 : 0 : exit_nicely(1);
9959 : : }
9960 : :
9961 : 493 : tbinfo->checkexprs = constrs + j;
9962 : :
9963 [ + + ]: 1125 : for (int c = 0; c < numcons; c++, j++)
9964 : : {
9965 : 632 : bool validated = PQgetvalue(res, j, i_convalidated)[0] == 't';
9966 : :
9967 : 632 : constrs[j].dobj.objType = DO_CONSTRAINT;
9968 : 632 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9969 : 632 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9970 : 632 : AssignDumpId(&constrs[j].dobj);
9971 : 632 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9972 : 632 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9973 : 632 : constrs[j].contable = tbinfo;
9974 : 632 : constrs[j].condomain = NULL;
9975 : 632 : constrs[j].contype = 'c';
9976 : 632 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9977 : 632 : constrs[j].confrelid = InvalidOid;
9978 : 632 : constrs[j].conindex = 0;
9979 : 632 : constrs[j].condeferrable = false;
9980 : 632 : constrs[j].condeferred = false;
9981 : 632 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9982 : :
9983 : : /*
9984 : : * An unvalidated constraint needs to be dumped separately, so
9985 : : * that potentially-violating existing data is loaded before
9986 : : * the constraint.
9987 : : */
9988 : 632 : constrs[j].separate = !validated;
9989 : :
9990 : 632 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9991 : :
9992 : : /*
9993 : : * Mark the constraint as needing to appear before the table
9994 : : * --- this is so that any other dependencies of the
9995 : : * constraint will be emitted before we try to create the
9996 : : * table. If the constraint is to be dumped separately, it
9997 : : * will be dumped after data is loaded anyway, so don't do it.
9998 : : * (There's an automatic dependency in the opposite direction
9999 : : * anyway, so don't need to add one manually here.)
10000 : : */
10001 [ + + ]: 632 : if (!constrs[j].separate)
10002 : 567 : addObjectDependency(&tbinfo->dobj,
10003 : 567 : constrs[j].dobj.dumpId);
10004 : :
10005 : : /*
10006 : : * We will detect later whether the constraint must be split
10007 : : * out from the table definition.
10008 : : */
10009 : : }
10010 : : }
10011 : :
10012 : 63 : PQclear(res);
10013 : : }
10014 : :
10015 : 215 : destroyPQExpBuffer(q);
10016 : 215 : destroyPQExpBuffer(tbloids);
10017 : 215 : destroyPQExpBuffer(checkoids);
10018 : 215 : }
10019 : :
10020 : : /*
10021 : : * Based on the getTableAttrs query's row corresponding to one column, set
10022 : : * the name and flags to handle a not-null constraint for that column in
10023 : : * the tbinfo struct.
10024 : : *
10025 : : * Result row 'r' is for tbinfo's attribute 'j'.
10026 : : *
10027 : : * There are four possibilities:
10028 : : * 1) the column has no not-null constraints. In that case, ->notnull_constrs
10029 : : * (the constraint name) remains NULL.
10030 : : * 2) The column has a constraint with no name (this is the case when
10031 : : * constraints come from pre-18 servers). In this case, ->notnull_constrs
10032 : : * is set to the empty string; dumpTableSchema will print just "NOT NULL".
10033 : : * 3) The column has an invalid not-null constraint. This must be treated
10034 : : * as a separate object (because it must be created after the table data
10035 : : * is loaded). So we add its OID to invalidnotnulloids for processing
10036 : : * elsewhere and do nothing further with it here. We distinguish this
10037 : : * case because the "notnull_invalidoid" column has been set to a non-NULL
10038 : : * value, which is the constraint OID. Valid constraints have a null OID.
10039 : : * 4) The column has a constraint with a known name; in that case
10040 : : * notnull_constrs carries that name and dumpTableSchema will print
10041 : : * "CONSTRAINT the_name NOT NULL". However, if the name is the default
10042 : : * (table_column_not_null) and there's no comment on the constraint,
10043 : : * there's no need to print that name in the dump, so notnull_constrs
10044 : : * is set to the empty string and it behaves as case 2.
10045 : : *
10046 : : * In a child table that inherits from a parent already containing NOT NULL
10047 : : * constraints and the columns in the child don't have their own NOT NULL
10048 : : * declarations, we suppress printing constraints in the child: the
10049 : : * constraints are acquired at the point where the child is attached to the
10050 : : * parent. This is tracked in ->notnull_islocal; for servers pre-18 this is
10051 : : * set not here but in flagInhAttrs. That flag is also used when the
10052 : : * constraint was validated in a child but all its parent have it as NOT
10053 : : * VALID.
10054 : : *
10055 : : * Any of these constraints might have the NO INHERIT bit. If so we set
10056 : : * ->notnull_noinh and NO INHERIT will be printed by dumpTableSchema.
10057 : : *
10058 : : * In case 4 above, the name comparison is a bit of a hack; it actually fails
10059 : : * to do the right thing in all but the trivial case. However, the downside
10060 : : * of getting it wrong is simply that the name is printed rather than
10061 : : * suppressed, so it's not a big deal.
10062 : : *
10063 : : * invalidnotnulloids is expected to be given as NULL; if any invalid not-null
10064 : : * constraints are found, it is initialized and filled with the array of
10065 : : * OIDs of such constraints, for later processing.
10066 : : */
10067 : : static void
10068 : 26494 : determineNotNullFlags(Archive *fout, PGresult *res, int r,
10069 : : TableInfo *tbinfo, int j,
10070 : : int i_notnull_name,
10071 : : int i_notnull_comment,
10072 : : int i_notnull_invalidoid,
10073 : : int i_notnull_noinherit,
10074 : : int i_notnull_islocal,
10075 : : PQExpBuffer *invalidnotnulloids)
10076 : : {
10077 : 26494 : DumpOptions *dopt = fout->dopt;
10078 : :
10079 : : /*
10080 : : * If this not-null constraint is not valid, list its OID in
10081 : : * invalidnotnulloids and do nothing further. It'll be processed
10082 : : * elsewhere later.
10083 : : *
10084 : : * Because invalid not-null constraints are rare, we don't want to malloc
10085 : : * invalidnotnulloids until we're sure we're going it need it, which
10086 : : * happens here.
10087 : : */
10088 [ + + ]: 26494 : if (!PQgetisnull(res, r, i_notnull_invalidoid))
10089 : : {
10090 : 76 : char *constroid = PQgetvalue(res, r, i_notnull_invalidoid);
10091 : :
10092 [ + + ]: 76 : if (*invalidnotnulloids == NULL)
10093 : : {
10094 : 46 : *invalidnotnulloids = createPQExpBuffer();
10095 : 46 : appendPQExpBufferChar(*invalidnotnulloids, '{');
10096 : 46 : appendPQExpBufferStr(*invalidnotnulloids, constroid);
10097 : : }
10098 : : else
10099 : 30 : appendPQExpBuffer(*invalidnotnulloids, ",%s", constroid);
10100 : :
10101 : : /*
10102 : : * Track when a parent constraint is invalid for the cases where a
10103 : : * child constraint has been validated independenly.
10104 : : */
10105 : 76 : tbinfo->notnull_invalid[j] = true;
10106 : :
10107 : : /* nothing else to do */
10108 : 76 : tbinfo->notnull_constrs[j] = NULL;
10109 : 76 : return;
10110 : : }
10111 : :
10112 : : /*
10113 : : * notnull_noinh is straight from the query result. notnull_islocal also,
10114 : : * though flagInhAttrs may change that one later.
10115 : : */
10116 : 26418 : tbinfo->notnull_noinh[j] = PQgetvalue(res, r, i_notnull_noinherit)[0] == 't';
10117 : 26418 : tbinfo->notnull_islocal[j] = PQgetvalue(res, r, i_notnull_islocal)[0] == 't';
10118 : 26418 : tbinfo->notnull_invalid[j] = false;
10119 : :
10120 : : /*
10121 : : * Determine a constraint name to use. If the column is not marked not-
10122 : : * null, we set NULL which cues ... to do nothing. An empty string says
10123 : : * to print an unnamed NOT NULL, and anything else is a constraint name to
10124 : : * use.
10125 : : */
10126 [ - + ]: 26418 : if (fout->remoteVersion < 180000)
10127 : : {
10128 : : /*
10129 : : * < 18 doesn't have not-null names, so an unnamed constraint is
10130 : : * sufficient.
10131 : : */
10132 [ # # ]: 0 : if (PQgetisnull(res, r, i_notnull_name))
10133 : 0 : tbinfo->notnull_constrs[j] = NULL;
10134 : : else
10135 : 0 : tbinfo->notnull_constrs[j] = "";
10136 : : }
10137 : : else
10138 : : {
10139 [ + + ]: 26418 : if (PQgetisnull(res, r, i_notnull_name))
10140 : 23571 : tbinfo->notnull_constrs[j] = NULL;
10141 : : else
10142 : : {
10143 : : /*
10144 : : * In binary upgrade of inheritance child tables, must have a
10145 : : * constraint name that we can UPDATE later; same if there's a
10146 : : * comment on the constraint.
10147 : : */
10148 [ + + ]: 2847 : if ((dopt->binary_upgrade &&
10149 [ + + ]: 348 : !tbinfo->ispartition &&
10150 [ + + + + ]: 3090 : !tbinfo->notnull_islocal[j]) ||
10151 : 2826 : !PQgetisnull(res, r, i_notnull_comment))
10152 : : {
10153 : 70 : tbinfo->notnull_constrs[j] =
10154 : 70 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10155 : : }
10156 : : else
10157 : : {
10158 : : char *default_name;
10159 : :
10160 : : /* XXX should match ChooseConstraintName better */
10161 : 2777 : default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name,
10162 : 2777 : tbinfo->attnames[j]);
10163 [ + + ]: 2777 : if (strcmp(default_name,
10164 : 2777 : PQgetvalue(res, r, i_notnull_name)) == 0)
10165 : 1833 : tbinfo->notnull_constrs[j] = "";
10166 : : else
10167 : : {
10168 : 944 : tbinfo->notnull_constrs[j] =
10169 : 944 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10170 : : }
10171 : 2777 : pfree(default_name);
10172 : : }
10173 : : }
10174 : : }
10175 : : }
10176 : :
10177 : : /*
10178 : : * Test whether a column should be printed as part of table's CREATE TABLE.
10179 : : * Column number is zero-based.
10180 : : *
10181 : : * Normally this is always true, but it's false for dropped columns, as well
10182 : : * as those that were inherited without any local definition. (If we print
10183 : : * such a column it will mistakenly get pg_attribute.attislocal set to true.)
10184 : : * For partitions, it's always true, because we want the partitions to be
10185 : : * created independently and ATTACH PARTITION used afterwards.
10186 : : *
10187 : : * In binary_upgrade mode, we must print all columns and fix the attislocal/
10188 : : * attisdropped state later, so as to keep control of the physical column
10189 : : * order.
10190 : : *
10191 : : * This function exists because there are scattered nonobvious places that
10192 : : * must be kept in sync with this decision.
10193 : : */
10194 : : bool
10195 : 42615 : shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
10196 : : {
10197 [ + + ]: 42615 : if (dopt->binary_upgrade)
10198 : 6565 : return true;
10199 [ + + ]: 36050 : if (tbinfo->attisdropped[colno])
10200 : 912 : return false;
10201 [ + + + + ]: 35138 : return (tbinfo->attislocal[colno] || tbinfo->ispartition);
10202 : : }
10203 : :
10204 : :
10205 : : /*
10206 : : * getTSParsers:
10207 : : * get information about all text search parsers in the system catalogs
10208 : : */
10209 : : void
10210 : 215 : getTSParsers(Archive *fout)
10211 : : {
10212 : : PGresult *res;
10213 : : int ntups;
10214 : : int i;
10215 : : PQExpBuffer query;
10216 : : TSParserInfo *prsinfo;
10217 : : int i_tableoid;
10218 : : int i_oid;
10219 : : int i_prsname;
10220 : : int i_prsnamespace;
10221 : : int i_prsstart;
10222 : : int i_prstoken;
10223 : : int i_prsend;
10224 : : int i_prsheadline;
10225 : : int i_prslextype;
10226 : :
10227 : 215 : query = createPQExpBuffer();
10228 : :
10229 : : /*
10230 : : * find all text search objects, including builtin ones; we filter out
10231 : : * system-defined objects at dump-out time.
10232 : : */
10233 : :
10234 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, "
10235 : : "prsstart::oid, prstoken::oid, "
10236 : : "prsend::oid, prsheadline::oid, prslextype::oid "
10237 : : "FROM pg_ts_parser");
10238 : :
10239 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10240 : :
10241 : 215 : ntups = PQntuples(res);
10242 : :
10243 : 215 : prsinfo = pg_malloc_array(TSParserInfo, ntups);
10244 : :
10245 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10246 : 215 : i_oid = PQfnumber(res, "oid");
10247 : 215 : i_prsname = PQfnumber(res, "prsname");
10248 : 215 : i_prsnamespace = PQfnumber(res, "prsnamespace");
10249 : 215 : i_prsstart = PQfnumber(res, "prsstart");
10250 : 215 : i_prstoken = PQfnumber(res, "prstoken");
10251 : 215 : i_prsend = PQfnumber(res, "prsend");
10252 : 215 : i_prsheadline = PQfnumber(res, "prsheadline");
10253 : 215 : i_prslextype = PQfnumber(res, "prslextype");
10254 : :
10255 [ + + ]: 478 : for (i = 0; i < ntups; i++)
10256 : : {
10257 : 263 : prsinfo[i].dobj.objType = DO_TSPARSER;
10258 : 263 : prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10259 : 263 : prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10260 : 263 : AssignDumpId(&prsinfo[i].dobj);
10261 : 263 : prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
10262 : 526 : prsinfo[i].dobj.namespace =
10263 : 263 : findNamespace(atooid(PQgetvalue(res, i, i_prsnamespace)));
10264 : 263 : prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
10265 : 263 : prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
10266 : 263 : prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
10267 : 263 : prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline));
10268 : 263 : prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype));
10269 : :
10270 : : /* Decide whether we want to dump it */
10271 : 263 : selectDumpableObject(&(prsinfo[i].dobj), fout);
10272 : : }
10273 : :
10274 : 215 : PQclear(res);
10275 : :
10276 : 215 : destroyPQExpBuffer(query);
10277 : 215 : }
10278 : :
10279 : : /*
10280 : : * getTSDictionaries:
10281 : : * get information about all text search dictionaries in the system catalogs
10282 : : */
10283 : : void
10284 : 215 : getTSDictionaries(Archive *fout)
10285 : : {
10286 : : PGresult *res;
10287 : : int ntups;
10288 : : int i;
10289 : : PQExpBuffer query;
10290 : : TSDictInfo *dictinfo;
10291 : : int i_tableoid;
10292 : : int i_oid;
10293 : : int i_dictname;
10294 : : int i_dictnamespace;
10295 : : int i_dictowner;
10296 : : int i_dicttemplate;
10297 : : int i_dictinitoption;
10298 : :
10299 : 215 : query = createPQExpBuffer();
10300 : :
10301 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, dictname, "
10302 : : "dictnamespace, dictowner, "
10303 : : "dicttemplate, dictinitoption "
10304 : : "FROM pg_ts_dict");
10305 : :
10306 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10307 : :
10308 : 215 : ntups = PQntuples(res);
10309 : :
10310 : 215 : dictinfo = pg_malloc_array(TSDictInfo, ntups);
10311 : :
10312 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10313 : 215 : i_oid = PQfnumber(res, "oid");
10314 : 215 : i_dictname = PQfnumber(res, "dictname");
10315 : 215 : i_dictnamespace = PQfnumber(res, "dictnamespace");
10316 : 215 : i_dictowner = PQfnumber(res, "dictowner");
10317 : 215 : i_dictinitoption = PQfnumber(res, "dictinitoption");
10318 : 215 : i_dicttemplate = PQfnumber(res, "dicttemplate");
10319 : :
10320 [ + + ]: 7206 : for (i = 0; i < ntups; i++)
10321 : : {
10322 : 6991 : dictinfo[i].dobj.objType = DO_TSDICT;
10323 : 6991 : dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10324 : 6991 : dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10325 : 6991 : AssignDumpId(&dictinfo[i].dobj);
10326 : 6991 : dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
10327 : 13982 : dictinfo[i].dobj.namespace =
10328 : 6991 : findNamespace(atooid(PQgetvalue(res, i, i_dictnamespace)));
10329 : 6991 : dictinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_dictowner));
10330 : 6991 : dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
10331 [ + + ]: 6991 : if (PQgetisnull(res, i, i_dictinitoption))
10332 : 263 : dictinfo[i].dictinitoption = NULL;
10333 : : else
10334 : 6728 : dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption));
10335 : :
10336 : : /* Decide whether we want to dump it */
10337 : 6991 : selectDumpableObject(&(dictinfo[i].dobj), fout);
10338 : : }
10339 : :
10340 : 215 : PQclear(res);
10341 : :
10342 : 215 : destroyPQExpBuffer(query);
10343 : 215 : }
10344 : :
10345 : : /*
10346 : : * getTSTemplates:
10347 : : * get information about all text search templates in the system catalogs
10348 : : */
10349 : : void
10350 : 215 : getTSTemplates(Archive *fout)
10351 : : {
10352 : : PGresult *res;
10353 : : int ntups;
10354 : : int i;
10355 : : PQExpBuffer query;
10356 : : TSTemplateInfo *tmplinfo;
10357 : : int i_tableoid;
10358 : : int i_oid;
10359 : : int i_tmplname;
10360 : : int i_tmplnamespace;
10361 : : int i_tmplinit;
10362 : : int i_tmpllexize;
10363 : :
10364 : 215 : query = createPQExpBuffer();
10365 : :
10366 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, "
10367 : : "tmplnamespace, tmplinit::oid, tmpllexize::oid "
10368 : : "FROM pg_ts_template");
10369 : :
10370 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10371 : :
10372 : 215 : ntups = PQntuples(res);
10373 : :
10374 : 215 : tmplinfo = pg_malloc_array(TSTemplateInfo, ntups);
10375 : :
10376 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10377 : 215 : i_oid = PQfnumber(res, "oid");
10378 : 215 : i_tmplname = PQfnumber(res, "tmplname");
10379 : 215 : i_tmplnamespace = PQfnumber(res, "tmplnamespace");
10380 : 215 : i_tmplinit = PQfnumber(res, "tmplinit");
10381 : 215 : i_tmpllexize = PQfnumber(res, "tmpllexize");
10382 : :
10383 [ + + ]: 1338 : for (i = 0; i < ntups; i++)
10384 : : {
10385 : 1123 : tmplinfo[i].dobj.objType = DO_TSTEMPLATE;
10386 : 1123 : tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10387 : 1123 : tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10388 : 1123 : AssignDumpId(&tmplinfo[i].dobj);
10389 : 1123 : tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
10390 : 2246 : tmplinfo[i].dobj.namespace =
10391 : 1123 : findNamespace(atooid(PQgetvalue(res, i, i_tmplnamespace)));
10392 : 1123 : tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
10393 : 1123 : tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
10394 : :
10395 : : /* Decide whether we want to dump it */
10396 : 1123 : selectDumpableObject(&(tmplinfo[i].dobj), fout);
10397 : : }
10398 : :
10399 : 215 : PQclear(res);
10400 : :
10401 : 215 : destroyPQExpBuffer(query);
10402 : 215 : }
10403 : :
10404 : : /*
10405 : : * getTSConfigurations:
10406 : : * get information about all text search configurations
10407 : : */
10408 : : void
10409 : 215 : getTSConfigurations(Archive *fout)
10410 : : {
10411 : : PGresult *res;
10412 : : int ntups;
10413 : : int i;
10414 : : PQExpBuffer query;
10415 : : TSConfigInfo *cfginfo;
10416 : : int i_tableoid;
10417 : : int i_oid;
10418 : : int i_cfgname;
10419 : : int i_cfgnamespace;
10420 : : int i_cfgowner;
10421 : : int i_cfgparser;
10422 : :
10423 : 215 : query = createPQExpBuffer();
10424 : :
10425 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, cfgname, "
10426 : : "cfgnamespace, cfgowner, cfgparser "
10427 : : "FROM pg_ts_config");
10428 : :
10429 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10430 : :
10431 : 215 : ntups = PQntuples(res);
10432 : :
10433 : 215 : cfginfo = pg_malloc_array(TSConfigInfo, ntups);
10434 : :
10435 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10436 : 215 : i_oid = PQfnumber(res, "oid");
10437 : 215 : i_cfgname = PQfnumber(res, "cfgname");
10438 : 215 : i_cfgnamespace = PQfnumber(res, "cfgnamespace");
10439 : 215 : i_cfgowner = PQfnumber(res, "cfgowner");
10440 : 215 : i_cfgparser = PQfnumber(res, "cfgparser");
10441 : :
10442 [ + + ]: 7171 : for (i = 0; i < ntups; i++)
10443 : : {
10444 : 6956 : cfginfo[i].dobj.objType = DO_TSCONFIG;
10445 : 6956 : cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10446 : 6956 : cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10447 : 6956 : AssignDumpId(&cfginfo[i].dobj);
10448 : 6956 : cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
10449 : 13912 : cfginfo[i].dobj.namespace =
10450 : 6956 : findNamespace(atooid(PQgetvalue(res, i, i_cfgnamespace)));
10451 : 6956 : cfginfo[i].rolname = getRoleName(PQgetvalue(res, i, i_cfgowner));
10452 : 6956 : cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
10453 : :
10454 : : /* Decide whether we want to dump it */
10455 : 6956 : selectDumpableObject(&(cfginfo[i].dobj), fout);
10456 : : }
10457 : :
10458 : 215 : PQclear(res);
10459 : :
10460 : 215 : destroyPQExpBuffer(query);
10461 : 215 : }
10462 : :
10463 : : /*
10464 : : * getForeignDataWrappers:
10465 : : * get information about all foreign-data wrappers in the system catalogs
10466 : : */
10467 : : void
10468 : 215 : getForeignDataWrappers(Archive *fout)
10469 : : {
10470 : : PGresult *res;
10471 : : int ntups;
10472 : : int i;
10473 : : PQExpBuffer query;
10474 : : FdwInfo *fdwinfo;
10475 : : int i_tableoid;
10476 : : int i_oid;
10477 : : int i_fdwname;
10478 : : int i_fdwowner;
10479 : : int i_fdwhandler;
10480 : : int i_fdwvalidator;
10481 : : int i_fdwconnection;
10482 : : int i_fdwacl;
10483 : : int i_acldefault;
10484 : : int i_fdwoptions;
10485 : :
10486 : 215 : query = createPQExpBuffer();
10487 : :
10488 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, fdwname, "
10489 : : "fdwowner, "
10490 : : "fdwhandler::pg_catalog.regproc, "
10491 : : "fdwvalidator::pg_catalog.regproc, ");
10492 : :
10493 [ + - ]: 215 : if (fout->remoteVersion >= 190000)
10494 : 215 : appendPQExpBufferStr(query, "fdwconnection::pg_catalog.regproc, ");
10495 : : else
10496 : 0 : appendPQExpBufferStr(query, "'-' AS fdwconnection, ");
10497 : :
10498 : 215 : appendPQExpBufferStr(query,
10499 : : "fdwacl, "
10500 : : "acldefault('F', fdwowner) AS acldefault, "
10501 : : "array_to_string(ARRAY("
10502 : : "SELECT quote_ident(option_name) || ' ' || "
10503 : : "quote_literal(option_value) "
10504 : : "FROM pg_options_to_table(fdwoptions) "
10505 : : "ORDER BY option_name"
10506 : : "), E',\n ') AS fdwoptions "
10507 : : "FROM pg_foreign_data_wrapper");
10508 : :
10509 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10510 : :
10511 : 215 : ntups = PQntuples(res);
10512 : :
10513 : 215 : fdwinfo = pg_malloc_array(FdwInfo, ntups);
10514 : :
10515 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10516 : 215 : i_oid = PQfnumber(res, "oid");
10517 : 215 : i_fdwname = PQfnumber(res, "fdwname");
10518 : 215 : i_fdwowner = PQfnumber(res, "fdwowner");
10519 : 215 : i_fdwhandler = PQfnumber(res, "fdwhandler");
10520 : 215 : i_fdwvalidator = PQfnumber(res, "fdwvalidator");
10521 : 215 : i_fdwconnection = PQfnumber(res, "fdwconnection");
10522 : 215 : i_fdwacl = PQfnumber(res, "fdwacl");
10523 : 215 : i_acldefault = PQfnumber(res, "acldefault");
10524 : 215 : i_fdwoptions = PQfnumber(res, "fdwoptions");
10525 : :
10526 [ + + ]: 290 : for (i = 0; i < ntups; i++)
10527 : : {
10528 : 75 : fdwinfo[i].dobj.objType = DO_FDW;
10529 : 75 : fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10530 : 75 : fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10531 : 75 : AssignDumpId(&fdwinfo[i].dobj);
10532 : 75 : fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname));
10533 : 75 : fdwinfo[i].dobj.namespace = NULL;
10534 : 75 : fdwinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_fdwacl));
10535 : 75 : fdwinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10536 : 75 : fdwinfo[i].dacl.privtype = 0;
10537 : 75 : fdwinfo[i].dacl.initprivs = NULL;
10538 : 75 : fdwinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_fdwowner));
10539 : 75 : fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler));
10540 : 75 : fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator));
10541 : 75 : fdwinfo[i].fdwconnection = pg_strdup(PQgetvalue(res, i, i_fdwconnection));
10542 : 75 : fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions));
10543 : :
10544 : : /* Decide whether we want to dump it */
10545 : 75 : selectDumpableObject(&(fdwinfo[i].dobj), fout);
10546 : :
10547 : : /* Mark whether FDW has an ACL */
10548 [ + + ]: 75 : if (!PQgetisnull(res, i, i_fdwacl))
10549 : 48 : fdwinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10550 : : }
10551 : :
10552 : 215 : PQclear(res);
10553 : :
10554 : 215 : destroyPQExpBuffer(query);
10555 : 215 : }
10556 : :
10557 : : /*
10558 : : * getForeignServers:
10559 : : * get information about all foreign servers in the system catalogs
10560 : : */
10561 : : void
10562 : 215 : getForeignServers(Archive *fout)
10563 : : {
10564 : : PGresult *res;
10565 : : int ntups;
10566 : : int i;
10567 : : PQExpBuffer query;
10568 : : ForeignServerInfo *srvinfo;
10569 : : int i_tableoid;
10570 : : int i_oid;
10571 : : int i_srvname;
10572 : : int i_srvowner;
10573 : : int i_srvfdw;
10574 : : int i_srvtype;
10575 : : int i_srvversion;
10576 : : int i_srvacl;
10577 : : int i_acldefault;
10578 : : int i_srvoptions;
10579 : :
10580 : 215 : query = createPQExpBuffer();
10581 : :
10582 : 215 : appendPQExpBufferStr(query, "SELECT tableoid, oid, srvname, "
10583 : : "srvowner, "
10584 : : "srvfdw, srvtype, srvversion, srvacl, "
10585 : : "acldefault('S', srvowner) AS acldefault, "
10586 : : "array_to_string(ARRAY("
10587 : : "SELECT quote_ident(option_name) || ' ' || "
10588 : : "quote_literal(option_value) "
10589 : : "FROM pg_options_to_table(srvoptions) "
10590 : : "ORDER BY option_name"
10591 : : "), E',\n ') AS srvoptions "
10592 : : "FROM pg_foreign_server");
10593 : :
10594 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10595 : :
10596 : 215 : ntups = PQntuples(res);
10597 : :
10598 : 215 : srvinfo = pg_malloc_array(ForeignServerInfo, ntups);
10599 : :
10600 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10601 : 215 : i_oid = PQfnumber(res, "oid");
10602 : 215 : i_srvname = PQfnumber(res, "srvname");
10603 : 215 : i_srvowner = PQfnumber(res, "srvowner");
10604 : 215 : i_srvfdw = PQfnumber(res, "srvfdw");
10605 : 215 : i_srvtype = PQfnumber(res, "srvtype");
10606 : 215 : i_srvversion = PQfnumber(res, "srvversion");
10607 : 215 : i_srvacl = PQfnumber(res, "srvacl");
10608 : 215 : i_acldefault = PQfnumber(res, "acldefault");
10609 : 215 : i_srvoptions = PQfnumber(res, "srvoptions");
10610 : :
10611 [ + + ]: 294 : for (i = 0; i < ntups; i++)
10612 : : {
10613 : 79 : srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
10614 : 79 : srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10615 : 79 : srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10616 : 79 : AssignDumpId(&srvinfo[i].dobj);
10617 : 79 : srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname));
10618 : 79 : srvinfo[i].dobj.namespace = NULL;
10619 : 79 : srvinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_srvacl));
10620 : 79 : srvinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10621 : 79 : srvinfo[i].dacl.privtype = 0;
10622 : 79 : srvinfo[i].dacl.initprivs = NULL;
10623 : 79 : srvinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_srvowner));
10624 : 79 : srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw));
10625 : 79 : srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype));
10626 : 79 : srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion));
10627 : 79 : srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions));
10628 : :
10629 : : /* Decide whether we want to dump it */
10630 : 79 : selectDumpableObject(&(srvinfo[i].dobj), fout);
10631 : :
10632 : : /* Servers have user mappings */
10633 : 79 : srvinfo[i].dobj.components |= DUMP_COMPONENT_USERMAP;
10634 : :
10635 : : /* Mark whether server has an ACL */
10636 [ + + ]: 79 : if (!PQgetisnull(res, i, i_srvacl))
10637 : 48 : srvinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10638 : : }
10639 : :
10640 : 215 : PQclear(res);
10641 : :
10642 : 215 : destroyPQExpBuffer(query);
10643 : 215 : }
10644 : :
10645 : : /*
10646 : : * getDefaultACLs:
10647 : : * get information about all default ACL information in the system catalogs
10648 : : */
10649 : : void
10650 : 215 : getDefaultACLs(Archive *fout)
10651 : : {
10652 : 215 : DumpOptions *dopt = fout->dopt;
10653 : : DefaultACLInfo *daclinfo;
10654 : : PQExpBuffer query;
10655 : : PGresult *res;
10656 : : int i_oid;
10657 : : int i_tableoid;
10658 : : int i_defaclrole;
10659 : : int i_defaclnamespace;
10660 : : int i_defaclobjtype;
10661 : : int i_defaclacl;
10662 : : int i_acldefault;
10663 : : int i,
10664 : : ntups;
10665 : :
10666 : 215 : query = createPQExpBuffer();
10667 : :
10668 : : /*
10669 : : * Global entries (with defaclnamespace=0) replace the hard-wired default
10670 : : * ACL for their object type. We should dump them as deltas from the
10671 : : * default ACL, since that will be used as a starting point for
10672 : : * interpreting the ALTER DEFAULT PRIVILEGES commands. On the other hand,
10673 : : * non-global entries can only add privileges not revoke them. We must
10674 : : * dump those as-is (i.e., as deltas from an empty ACL).
10675 : : *
10676 : : * We can use defaclobjtype as the object type for acldefault(), except
10677 : : * for the case of 'S' (DEFACLOBJ_SEQUENCE) which must be converted to
10678 : : * 's'.
10679 : : */
10680 : 215 : appendPQExpBufferStr(query,
10681 : : "SELECT oid, tableoid, "
10682 : : "defaclrole, "
10683 : : "defaclnamespace, "
10684 : : "defaclobjtype, "
10685 : : "defaclacl, "
10686 : : "CASE WHEN defaclnamespace = 0 THEN "
10687 : : "acldefault(CASE WHEN defaclobjtype = 'S' "
10688 : : "THEN 's'::\"char\" ELSE defaclobjtype END, "
10689 : : "defaclrole) ELSE '{}' END AS acldefault "
10690 : : "FROM pg_default_acl");
10691 : :
10692 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10693 : :
10694 : 215 : ntups = PQntuples(res);
10695 : :
10696 : 215 : daclinfo = pg_malloc_array(DefaultACLInfo, ntups);
10697 : :
10698 : 215 : i_oid = PQfnumber(res, "oid");
10699 : 215 : i_tableoid = PQfnumber(res, "tableoid");
10700 : 215 : i_defaclrole = PQfnumber(res, "defaclrole");
10701 : 215 : i_defaclnamespace = PQfnumber(res, "defaclnamespace");
10702 : 215 : i_defaclobjtype = PQfnumber(res, "defaclobjtype");
10703 : 215 : i_defaclacl = PQfnumber(res, "defaclacl");
10704 : 215 : i_acldefault = PQfnumber(res, "acldefault");
10705 : :
10706 [ + + ]: 421 : for (i = 0; i < ntups; i++)
10707 : : {
10708 : 206 : Oid nspid = atooid(PQgetvalue(res, i, i_defaclnamespace));
10709 : :
10710 : 206 : daclinfo[i].dobj.objType = DO_DEFAULT_ACL;
10711 : 206 : daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10712 : 206 : daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10713 : 206 : AssignDumpId(&daclinfo[i].dobj);
10714 : : /* cheesy ... is it worth coming up with a better object name? */
10715 : 206 : daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
10716 : :
10717 [ + + ]: 206 : if (nspid != InvalidOid)
10718 : 96 : daclinfo[i].dobj.namespace = findNamespace(nspid);
10719 : : else
10720 : 110 : daclinfo[i].dobj.namespace = NULL;
10721 : :
10722 : 206 : daclinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_defaclacl));
10723 : 206 : daclinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10724 : 206 : daclinfo[i].dacl.privtype = 0;
10725 : 206 : daclinfo[i].dacl.initprivs = NULL;
10726 : 206 : daclinfo[i].defaclrole = getRoleName(PQgetvalue(res, i, i_defaclrole));
10727 : 206 : daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype));
10728 : :
10729 : : /* Default ACLs are ACLs, of course */
10730 : 206 : daclinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10731 : :
10732 : : /* Decide whether we want to dump it */
10733 : 206 : selectDumpableDefaultACL(&(daclinfo[i]), dopt);
10734 : : }
10735 : :
10736 : 215 : PQclear(res);
10737 : :
10738 : 215 : destroyPQExpBuffer(query);
10739 : 215 : }
10740 : :
10741 : : /*
10742 : : * getRoleName -- look up the name of a role, given its OID
10743 : : *
10744 : : * In current usage, we don't expect failures, so error out for a bad OID.
10745 : : */
10746 : : static const char *
10747 : 693138 : getRoleName(const char *roleoid_str)
10748 : : {
10749 : 693138 : Oid roleoid = atooid(roleoid_str);
10750 : :
10751 : : /*
10752 : : * Do binary search to find the appropriate item.
10753 : : */
10754 [ + - ]: 693138 : if (nrolenames > 0)
10755 : : {
10756 : 693138 : RoleNameItem *low = &rolenames[0];
10757 : 693138 : RoleNameItem *high = &rolenames[nrolenames - 1];
10758 : :
10759 [ + - ]: 2772744 : while (low <= high)
10760 : : {
10761 : 2772744 : RoleNameItem *middle = low + (high - low) / 2;
10762 : :
10763 [ + + ]: 2772744 : if (roleoid < middle->roleoid)
10764 : 2078145 : high = middle - 1;
10765 [ + + ]: 694599 : else if (roleoid > middle->roleoid)
10766 : 1461 : low = middle + 1;
10767 : : else
10768 : 693138 : return middle->rolename; /* found a match */
10769 : : }
10770 : : }
10771 : :
10772 : 0 : pg_fatal("role with OID %u does not exist", roleoid);
10773 : : return NULL; /* keep compiler quiet */
10774 : : }
10775 : :
10776 : : /*
10777 : : * collectRoleNames --
10778 : : *
10779 : : * Construct a table of all known roles.
10780 : : * The table is sorted by OID for speed in lookup.
10781 : : */
10782 : : static void
10783 : 216 : collectRoleNames(Archive *fout)
10784 : : {
10785 : : PGresult *res;
10786 : : const char *query;
10787 : : int i;
10788 : :
10789 : 216 : query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
10790 : :
10791 : 216 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
10792 : :
10793 : 216 : nrolenames = PQntuples(res);
10794 : :
10795 : 216 : rolenames = pg_malloc_array(RoleNameItem, nrolenames);
10796 : :
10797 [ + + ]: 4162 : for (i = 0; i < nrolenames; i++)
10798 : : {
10799 : 3946 : rolenames[i].roleoid = atooid(PQgetvalue(res, i, 0));
10800 : 3946 : rolenames[i].rolename = pg_strdup(PQgetvalue(res, i, 1));
10801 : : }
10802 : :
10803 : 216 : PQclear(res);
10804 : 216 : }
10805 : :
10806 : : /*
10807 : : * getAdditionalACLs
10808 : : *
10809 : : * We have now created all the DumpableObjects, and collected the ACL data
10810 : : * that appears in the directly-associated catalog entries. However, there's
10811 : : * more ACL-related info to collect. If any of a table's columns have ACLs,
10812 : : * we must set the TableInfo's DUMP_COMPONENT_ACL components flag, as well as
10813 : : * its hascolumnACLs flag (we won't store the ACLs themselves here, though).
10814 : : * Also, in versions having the pg_init_privs catalog, read that and load the
10815 : : * information into the relevant DumpableObjects.
10816 : : */
10817 : : static void
10818 : 213 : getAdditionalACLs(Archive *fout)
10819 : : {
10820 : 213 : PQExpBuffer query = createPQExpBuffer();
10821 : : PGresult *res;
10822 : : int ntups,
10823 : : i;
10824 : :
10825 : : /* Check for per-column ACLs */
10826 : 213 : appendPQExpBufferStr(query,
10827 : : "SELECT DISTINCT attrelid FROM pg_attribute "
10828 : : "WHERE attacl IS NOT NULL");
10829 : :
10830 : 213 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10831 : :
10832 : 213 : ntups = PQntuples(res);
10833 [ + + ]: 605 : for (i = 0; i < ntups; i++)
10834 : : {
10835 : 392 : Oid relid = atooid(PQgetvalue(res, i, 0));
10836 : : TableInfo *tblinfo;
10837 : :
10838 : 392 : tblinfo = findTableByOid(relid);
10839 : : /* OK to ignore tables we haven't got a DumpableObject for */
10840 [ + - ]: 392 : if (tblinfo)
10841 : : {
10842 : 392 : tblinfo->dobj.components |= DUMP_COMPONENT_ACL;
10843 : 392 : tblinfo->hascolumnACLs = true;
10844 : : }
10845 : : }
10846 : 213 : PQclear(res);
10847 : :
10848 : : /* Fetch initial-privileges data */
10849 : 213 : printfPQExpBuffer(query,
10850 : : "SELECT objoid, classoid, objsubid, privtype, initprivs "
10851 : : "FROM pg_init_privs");
10852 : :
10853 : 213 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10854 : :
10855 : 213 : ntups = PQntuples(res);
10856 [ + + ]: 53558 : for (i = 0; i < ntups; i++)
10857 : : {
10858 : 53345 : Oid objoid = atooid(PQgetvalue(res, i, 0));
10859 : 53345 : Oid classoid = atooid(PQgetvalue(res, i, 1));
10860 : 53345 : int objsubid = atoi(PQgetvalue(res, i, 2));
10861 : 53345 : char privtype = *(PQgetvalue(res, i, 3));
10862 : 53345 : char *initprivs = PQgetvalue(res, i, 4);
10863 : : CatalogId objId;
10864 : : DumpableObject *dobj;
10865 : :
10866 : 53345 : objId.tableoid = classoid;
10867 : 53345 : objId.oid = objoid;
10868 : 53345 : dobj = findObjectByCatalogId(objId);
10869 : : /* OK to ignore entries we haven't got a DumpableObject for */
10870 [ + + ]: 53345 : if (dobj)
10871 : : {
10872 : : /* Cope with sub-object initprivs */
10873 [ + + ]: 38482 : if (objsubid != 0)
10874 : : {
10875 [ + - ]: 5136 : if (dobj->objType == DO_TABLE)
10876 : : {
10877 : : /* For a column initprivs, set the table's ACL flags */
10878 : 5136 : dobj->components |= DUMP_COMPONENT_ACL;
10879 : 5136 : ((TableInfo *) dobj)->hascolumnACLs = true;
10880 : : }
10881 : : else
10882 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10883 : : classoid, objoid, objsubid);
10884 : 5345 : continue;
10885 : : }
10886 : :
10887 : : /*
10888 : : * We ignore any pg_init_privs.initprivs entry for the public
10889 : : * schema, as explained in getNamespaces().
10890 : : */
10891 [ + + ]: 33346 : if (dobj->objType == DO_NAMESPACE &&
10892 [ + + ]: 635 : strcmp(dobj->name, "public") == 0)
10893 : 209 : continue;
10894 : :
10895 : : /* Else it had better be of a type we think has ACLs */
10896 [ + + ]: 33137 : if (dobj->objType == DO_NAMESPACE ||
10897 [ + + ]: 32711 : dobj->objType == DO_TYPE ||
10898 [ + + ]: 32687 : dobj->objType == DO_FUNC ||
10899 [ + + ]: 32592 : dobj->objType == DO_AGG ||
10900 [ - + ]: 32568 : dobj->objType == DO_TABLE ||
10901 [ # # ]: 0 : dobj->objType == DO_PROCLANG ||
10902 [ # # ]: 0 : dobj->objType == DO_FDW ||
10903 [ # # ]: 0 : dobj->objType == DO_FOREIGN_SERVER)
10904 : 33137 : {
10905 : 33137 : DumpableObjectWithAcl *daobj = (DumpableObjectWithAcl *) dobj;
10906 : :
10907 : 33137 : daobj->dacl.privtype = privtype;
10908 : 33137 : daobj->dacl.initprivs = pstrdup(initprivs);
10909 : : }
10910 : : else
10911 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10912 : : classoid, objoid, objsubid);
10913 : : }
10914 : : }
10915 : 213 : PQclear(res);
10916 : :
10917 : 213 : destroyPQExpBuffer(query);
10918 : 213 : }
10919 : :
10920 : : /*
10921 : : * dumpCommentExtended --
10922 : : *
10923 : : * This routine is used to dump any comments associated with the
10924 : : * object handed to this routine. The routine takes the object type
10925 : : * and object name (ready to print, except for schema decoration), plus
10926 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
10927 : : * plus catalog ID and subid which are the lookup key for pg_description,
10928 : : * plus the dump ID for the object (for setting a dependency).
10929 : : * If a matching pg_description entry is found, it is dumped.
10930 : : *
10931 : : * Note: in some cases, such as comments for triggers and rules, the "type"
10932 : : * string really looks like, e.g., "TRIGGER name ON". This is a bit of a hack
10933 : : * but it doesn't seem worth complicating the API for all callers to make
10934 : : * it cleaner.
10935 : : *
10936 : : * Note: although this routine takes a dumpId for dependency purposes,
10937 : : * that purpose is just to mark the dependency in the emitted dump file
10938 : : * for possible future use by pg_restore. We do NOT use it for determining
10939 : : * ordering of the comment in the dump file, because this routine is called
10940 : : * after dependency sorting occurs. This routine should be called just after
10941 : : * calling ArchiveEntry() for the specified object.
10942 : : */
10943 : : static void
10944 : 6667 : dumpCommentExtended(Archive *fout, const char *type,
10945 : : const char *name, const char *namespace,
10946 : : const char *owner, CatalogId catalogId,
10947 : : int subid, DumpId dumpId,
10948 : : const char *initdb_comment)
10949 : : {
10950 : 6667 : DumpOptions *dopt = fout->dopt;
10951 : : CommentItem *comments;
10952 : : int ncomments;
10953 : :
10954 : : /* do nothing, if --no-comments is supplied */
10955 [ - + ]: 6667 : if (dopt->no_comments)
10956 : 0 : return;
10957 : :
10958 : : /* Comments are schema not data ... except LO comments are data */
10959 [ + + ]: 6667 : if (strcmp(type, "LARGE OBJECT") != 0)
10960 : : {
10961 [ - + ]: 6607 : if (!dopt->dumpSchema)
10962 : 0 : return;
10963 : : }
10964 : : else
10965 : : {
10966 : : /* We do dump LO comments in binary-upgrade mode */
10967 [ + + - + ]: 60 : if (!dopt->dumpData && !dopt->binary_upgrade)
10968 : 0 : return;
10969 : : }
10970 : :
10971 : : /* Search for comments associated with catalogId, using table */
10972 : 6667 : ncomments = findComments(catalogId.tableoid, catalogId.oid,
10973 : : &comments);
10974 : :
10975 : : /* Is there one matching the subid? */
10976 [ + + ]: 6667 : while (ncomments > 0)
10977 : : {
10978 [ + - ]: 6620 : if (comments->objsubid == subid)
10979 : 6620 : break;
10980 : 0 : comments++;
10981 : 0 : ncomments--;
10982 : : }
10983 : :
10984 [ + + ]: 6667 : if (initdb_comment != NULL)
10985 : : {
10986 : : static CommentItem empty_comment = {.descr = ""};
10987 : :
10988 : : /*
10989 : : * initdb creates this object with a comment. Skip dumping the
10990 : : * initdb-provided comment, which would complicate matters for
10991 : : * non-superuser use of pg_dump. When the DBA has removed initdb's
10992 : : * comment, replicate that.
10993 : : */
10994 [ + + ]: 137 : if (ncomments == 0)
10995 : : {
10996 : 4 : comments = &empty_comment;
10997 : 4 : ncomments = 1;
10998 : : }
10999 [ + - ]: 133 : else if (strcmp(comments->descr, initdb_comment) == 0)
11000 : 133 : ncomments = 0;
11001 : : }
11002 : :
11003 : : /* If a comment exists, build COMMENT ON statement */
11004 [ + + ]: 6667 : if (ncomments > 0)
11005 : : {
11006 : 6491 : PQExpBuffer query = createPQExpBuffer();
11007 : 6491 : PQExpBuffer tag = createPQExpBuffer();
11008 : :
11009 : 6491 : appendPQExpBuffer(query, "COMMENT ON %s ", type);
11010 [ + + + - ]: 6491 : if (namespace && *namespace)
11011 : 6300 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
11012 : 6491 : appendPQExpBuffer(query, "%s IS ", name);
11013 : 6491 : appendStringLiteralAH(query, comments->descr, fout);
11014 : 6491 : appendPQExpBufferStr(query, ";\n");
11015 : :
11016 : 6491 : appendPQExpBuffer(tag, "%s %s", type, name);
11017 : :
11018 : : /*
11019 : : * We mark comments as SECTION_NONE because they really belong in the
11020 : : * same section as their parent, whether that is pre-data or
11021 : : * post-data.
11022 : : */
11023 : 6491 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11024 : 6491 : ARCHIVE_OPTS(.tag = tag->data,
11025 : : .namespace = namespace,
11026 : : .owner = owner,
11027 : : .description = "COMMENT",
11028 : : .section = SECTION_NONE,
11029 : : .createStmt = query->data,
11030 : : .deps = &dumpId,
11031 : : .nDeps = 1));
11032 : :
11033 : 6491 : destroyPQExpBuffer(query);
11034 : 6491 : destroyPQExpBuffer(tag);
11035 : : }
11036 : : }
11037 : :
11038 : : /*
11039 : : * dumpComment --
11040 : : *
11041 : : * Typical simplification of the above function.
11042 : : */
11043 : : static inline void
11044 : 6483 : dumpComment(Archive *fout, const char *type,
11045 : : const char *name, const char *namespace,
11046 : : const char *owner, CatalogId catalogId,
11047 : : int subid, DumpId dumpId)
11048 : : {
11049 : 6483 : dumpCommentExtended(fout, type, name, namespace, owner,
11050 : : catalogId, subid, dumpId, NULL);
11051 : 6483 : }
11052 : :
11053 : : /*
11054 : : * appendNamedArgument --
11055 : : *
11056 : : * Convenience routine for constructing parameters of the form:
11057 : : * 'paraname', 'value'::type
11058 : : */
11059 : : static void
11060 : 6565 : appendNamedArgument(PQExpBuffer out, Archive *fout, const char *argname,
11061 : : const char *argtype, const char *argval)
11062 : : {
11063 : 6565 : appendPQExpBufferStr(out, ",\n\t");
11064 : :
11065 : 6565 : appendStringLiteralAH(out, argname, fout);
11066 : 6565 : appendPQExpBufferStr(out, ", ");
11067 : :
11068 : 6565 : appendStringLiteralAH(out, argval, fout);
11069 : 6565 : appendPQExpBuffer(out, "::%s", argtype);
11070 : 6565 : }
11071 : :
11072 : : /*
11073 : : * fetchAttributeStats --
11074 : : *
11075 : : * Fetch next batch of attribute statistics for dumpRelationStats_dumper().
11076 : : */
11077 : : static PGresult *
11078 : 1138 : fetchAttributeStats(Archive *fout)
11079 : : {
11080 : 1138 : ArchiveHandle *AH = (ArchiveHandle *) fout;
11081 : 1138 : PQExpBuffer relids = createPQExpBuffer();
11082 : 1138 : PQExpBuffer nspnames = createPQExpBuffer();
11083 : 1138 : PQExpBuffer relnames = createPQExpBuffer();
11084 : 1138 : int count = 0;
11085 : 1138 : PGresult *res = NULL;
11086 : : static TocEntry *te;
11087 : : static bool restarted;
11088 : 1138 : int max_rels = MAX_ATTR_STATS_RELS;
11089 : :
11090 : : /* If we're just starting, set our TOC pointer. */
11091 [ + + ]: 1138 : if (!te)
11092 : 66 : te = AH->toc->next;
11093 : :
11094 : : /*
11095 : : * We can't easily avoid a second TOC scan for the tar format because it
11096 : : * writes restore.sql separately, which means we must execute the queries
11097 : : * twice. This feels risky, but there is no known reason it should
11098 : : * generate different output than the first pass. Even if it does, the
11099 : : * worst-case scenario is that restore.sql might have different statistics
11100 : : * data than the archive.
11101 : : */
11102 [ + + + + : 1138 : if (!restarted && te == AH->toc && AH->format == archTar)
+ + ]
11103 : : {
11104 : 1 : te = AH->toc->next;
11105 : 1 : restarted = true;
11106 : : }
11107 : :
11108 : 1138 : appendPQExpBufferChar(relids, '{');
11109 : 1138 : appendPQExpBufferChar(nspnames, '{');
11110 : 1138 : appendPQExpBufferChar(relnames, '{');
11111 : :
11112 : : /*
11113 : : * Scan the TOC for the next set of relevant stats entries. We assume
11114 : : * that statistics are dumped in the order they are listed in the TOC.
11115 : : * This is perhaps not the sturdiest assumption, so we verify it matches
11116 : : * reality in dumpRelationStats_dumper().
11117 : : */
11118 [ + + + + ]: 17607 : for (; te != AH->toc && count < max_rels; te = te->next)
11119 : : {
11120 [ + + ]: 16469 : if ((te->reqs & REQ_STATS) == 0 ||
11121 [ + + ]: 3762 : strcmp(te->desc, "STATISTICS DATA") != 0)
11122 : 12745 : continue;
11123 : :
11124 [ + - ]: 3724 : if (fout->remoteVersion >= 190000)
11125 : : {
11126 : 3724 : const RelStatsInfo *rsinfo = (const RelStatsInfo *) te->defnDumperArg;
11127 : : char relid[32];
11128 : :
11129 : 3724 : sprintf(relid, "%u", rsinfo->relid);
11130 : 3724 : appendPGArray(relids, relid);
11131 : : }
11132 : : else
11133 : : {
11134 : 0 : appendPGArray(nspnames, te->namespace);
11135 : 0 : appendPGArray(relnames, te->tag);
11136 : : }
11137 : :
11138 : 3724 : count++;
11139 : : }
11140 : :
11141 : 1138 : appendPQExpBufferChar(relids, '}');
11142 : 1138 : appendPQExpBufferChar(nspnames, '}');
11143 : 1138 : appendPQExpBufferChar(relnames, '}');
11144 : :
11145 : : /* Execute the query for the next batch of relations. */
11146 [ + + ]: 1138 : if (count > 0)
11147 : : {
11148 : 112 : PQExpBuffer query = createPQExpBuffer();
11149 : :
11150 : 112 : appendPQExpBufferStr(query, "EXECUTE getAttributeStats(");
11151 : :
11152 [ + - ]: 112 : if (fout->remoteVersion >= 190000)
11153 : : {
11154 : 112 : appendStringLiteralAH(query, relids->data, fout);
11155 : 112 : appendPQExpBufferStr(query, "::pg_catalog.oid[])");
11156 : : }
11157 : : else
11158 : : {
11159 : 0 : appendStringLiteralAH(query, nspnames->data, fout);
11160 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[],");
11161 : 0 : appendStringLiteralAH(query, relnames->data, fout);
11162 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[])");
11163 : : }
11164 : :
11165 : 112 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11166 : 112 : destroyPQExpBuffer(query);
11167 : : }
11168 : :
11169 : 1138 : destroyPQExpBuffer(relids);
11170 : 1138 : destroyPQExpBuffer(nspnames);
11171 : 1138 : destroyPQExpBuffer(relnames);
11172 : 1138 : return res;
11173 : : }
11174 : :
11175 : : /*
11176 : : * dumpRelationStats_dumper --
11177 : : *
11178 : : * Generate command to import stats into the relation on the new database.
11179 : : * This routine is called by the Archiver when it wants the statistics to be
11180 : : * dumped.
11181 : : */
11182 : : static char *
11183 : 3724 : dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te)
11184 : : {
11185 : 3724 : const RelStatsInfo *rsinfo = userArg;
11186 : : static PGresult *res;
11187 : : static int rownum;
11188 : : PQExpBuffer query;
11189 : : PQExpBufferData out_data;
11190 : 3724 : PQExpBuffer out = &out_data;
11191 : : int i_schemaname;
11192 : : int i_tablename;
11193 : : int i_attname;
11194 : : int i_inherited;
11195 : : int i_null_frac;
11196 : : int i_avg_width;
11197 : : int i_n_distinct;
11198 : : int i_most_common_vals;
11199 : : int i_most_common_freqs;
11200 : : int i_histogram_bounds;
11201 : : int i_correlation;
11202 : : int i_most_common_elems;
11203 : : int i_most_common_elem_freqs;
11204 : : int i_elem_count_histogram;
11205 : : int i_range_length_histogram;
11206 : : int i_range_empty_frac;
11207 : : int i_range_bounds_histogram;
11208 : : static TocEntry *expected_te;
11209 : :
11210 : : /*
11211 : : * fetchAttributeStats() assumes that the statistics are dumped in the
11212 : : * order they are listed in the TOC. We verify that here for safety.
11213 : : */
11214 [ + + ]: 3724 : if (!expected_te)
11215 : 66 : expected_te = ((ArchiveHandle *) fout)->toc;
11216 : :
11217 : 3724 : expected_te = expected_te->next;
11218 [ + + ]: 14396 : while ((expected_te->reqs & REQ_STATS) == 0 ||
11219 [ + + ]: 3725 : strcmp(expected_te->desc, "STATISTICS DATA") != 0)
11220 : 10672 : expected_te = expected_te->next;
11221 : :
11222 [ - + ]: 3724 : if (te != expected_te)
11223 : 0 : pg_fatal("statistics dumped out of order (current: %d %s %s, expected: %d %s %s)",
11224 : : te->dumpId, te->desc, te->tag,
11225 : : expected_te->dumpId, expected_te->desc, expected_te->tag);
11226 : :
11227 : 3724 : query = createPQExpBuffer();
11228 [ + + ]: 3724 : if (!fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS])
11229 : : {
11230 [ + - ]: 66 : if (fout->remoteVersion >= 190000)
11231 : 66 : appendPQExpBufferStr(query,
11232 : : "PREPARE getAttributeStats(pg_catalog.oid[]) AS\n");
11233 : : else
11234 : 0 : appendPQExpBufferStr(query,
11235 : : "PREPARE getAttributeStats(pg_catalog.name[], pg_catalog.name[]) AS\n");
11236 : :
11237 : 66 : appendPQExpBufferStr(query,
11238 : : "SELECT s.schemaname, s.tablename, s.attname, s.inherited, "
11239 : : "s.null_frac, s.avg_width, s.n_distinct, "
11240 : : "s.most_common_vals, s.most_common_freqs, "
11241 : : "s.histogram_bounds, s.correlation, "
11242 : : "s.most_common_elems, s.most_common_elem_freqs, "
11243 : : "s.elem_count_histogram, ");
11244 : :
11245 [ + - ]: 66 : if (fout->remoteVersion >= 170000)
11246 : 66 : appendPQExpBufferStr(query,
11247 : : "s.range_length_histogram, "
11248 : : "s.range_empty_frac, "
11249 : : "s.range_bounds_histogram ");
11250 : : else
11251 : 0 : appendPQExpBufferStr(query,
11252 : : "NULL AS range_length_histogram,"
11253 : : "NULL AS range_empty_frac,"
11254 : : "NULL AS range_bounds_histogram ");
11255 : :
11256 : : /*
11257 : : * The results must be in the order of the relations supplied in the
11258 : : * parameters to ensure we remain in sync as we walk through the TOC.
11259 : : * The redundant filter clause seems sufficient to convince the
11260 : : * planner to use pg_class_relname_nsp_index, which avoids a full scan
11261 : : * of pg_stats. This may not work for all versions.
11262 : : */
11263 [ + - ]: 66 : if (fout->remoteVersion >= 190000)
11264 : 66 : appendPQExpBufferStr(query,
11265 : : "FROM pg_catalog.pg_stats s "
11266 : : "JOIN unnest($1) WITH ORDINALITY AS u (tableid, ord) "
11267 : : "ON s.tableid = u.tableid "
11268 : : "WHERE s.tableid = ANY($1) "
11269 : : "ORDER BY u.ord, s.attname, s.inherited");
11270 : : else
11271 : 0 : appendPQExpBufferStr(query,
11272 : : "FROM pg_catalog.pg_stats s "
11273 : : "JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) "
11274 : : "ON s.schemaname = u.schemaname "
11275 : : "AND s.tablename = u.tablename "
11276 : : "WHERE s.tablename = ANY($2) "
11277 : : "ORDER BY u.ord, s.attname, s.inherited");
11278 : :
11279 : 66 : ExecuteSqlStatement(fout, query->data);
11280 : :
11281 : 66 : fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS] = true;
11282 : 66 : resetPQExpBuffer(query);
11283 : : }
11284 : :
11285 : 3724 : initPQExpBuffer(out);
11286 : :
11287 : : /* restore relation stats */
11288 : 3724 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(\n");
11289 : 3724 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11290 : : fout->remoteVersion);
11291 : 3724 : appendPQExpBufferStr(out, "\t'schemaname', ");
11292 : 3724 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11293 : 3724 : appendPQExpBufferStr(out, ",\n");
11294 : 3724 : appendPQExpBufferStr(out, "\t'relname', ");
11295 : 3724 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11296 : 3724 : appendPQExpBufferStr(out, ",\n");
11297 : 3724 : appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages);
11298 : :
11299 : : /*
11300 : : * Before v14, a reltuples value of 0 was ambiguous: it could either mean
11301 : : * the relation is empty, or it could mean that it hadn't yet been
11302 : : * vacuumed or analyzed. (Newer versions use -1 for the latter case.)
11303 : : * This ambiguity allegedly can cause the planner to choose inefficient
11304 : : * plans after restoring to v18 or newer. To deal with this, let's just
11305 : : * set reltuples to -1 in that case.
11306 : : */
11307 [ - + - - ]: 3724 : if (fout->remoteVersion < 140000 && strcmp("0", rsinfo->reltuples) == 0)
11308 : 0 : appendPQExpBufferStr(out, "\t'reltuples', '-1'::real,\n");
11309 : : else
11310 : 3724 : appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples);
11311 : :
11312 : 3724 : appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer",
11313 : 3724 : rsinfo->relallvisible);
11314 : :
11315 [ + - ]: 3724 : if (fout->remoteVersion >= 180000)
11316 : 3724 : appendPQExpBuffer(out, ",\n\t'relallfrozen', '%d'::integer", rsinfo->relallfrozen);
11317 : :
11318 : 3724 : appendPQExpBufferStr(out, "\n);\n");
11319 : :
11320 : : /* Fetch the next batch of attribute statistics if needed. */
11321 [ + + ]: 3724 : if (rownum >= PQntuples(res))
11322 : : {
11323 : 1138 : PQclear(res);
11324 : 1138 : res = fetchAttributeStats(fout);
11325 : 1138 : rownum = 0;
11326 : : }
11327 : :
11328 : 3724 : i_schemaname = PQfnumber(res, "schemaname");
11329 : 3724 : i_tablename = PQfnumber(res, "tablename");
11330 : 3724 : i_attname = PQfnumber(res, "attname");
11331 : 3724 : i_inherited = PQfnumber(res, "inherited");
11332 : 3724 : i_null_frac = PQfnumber(res, "null_frac");
11333 : 3724 : i_avg_width = PQfnumber(res, "avg_width");
11334 : 3724 : i_n_distinct = PQfnumber(res, "n_distinct");
11335 : 3724 : i_most_common_vals = PQfnumber(res, "most_common_vals");
11336 : 3724 : i_most_common_freqs = PQfnumber(res, "most_common_freqs");
11337 : 3724 : i_histogram_bounds = PQfnumber(res, "histogram_bounds");
11338 : 3724 : i_correlation = PQfnumber(res, "correlation");
11339 : 3724 : i_most_common_elems = PQfnumber(res, "most_common_elems");
11340 : 3724 : i_most_common_elem_freqs = PQfnumber(res, "most_common_elem_freqs");
11341 : 3724 : i_elem_count_histogram = PQfnumber(res, "elem_count_histogram");
11342 : 3724 : i_range_length_histogram = PQfnumber(res, "range_length_histogram");
11343 : 3724 : i_range_empty_frac = PQfnumber(res, "range_empty_frac");
11344 : 3724 : i_range_bounds_histogram = PQfnumber(res, "range_bounds_histogram");
11345 : :
11346 : : /* restore attribute stats */
11347 [ + + ]: 4669 : for (; rownum < PQntuples(res); rownum++)
11348 : : {
11349 : : const char *attname;
11350 : :
11351 : : /* Stop if the next stat row in our cache isn't for this relation. */
11352 [ + + ]: 3531 : if (strcmp(te->tag, PQgetvalue(res, rownum, i_tablename)) != 0 ||
11353 [ + - ]: 945 : strcmp(te->namespace, PQgetvalue(res, rownum, i_schemaname)) != 0)
11354 : : break;
11355 : :
11356 : 945 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_attribute_stats(\n");
11357 : 945 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11358 : : fout->remoteVersion);
11359 : 945 : appendPQExpBufferStr(out, "\t'schemaname', ");
11360 : 945 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11361 : 945 : appendPQExpBufferStr(out, ",\n\t'relname', ");
11362 : 945 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11363 : :
11364 [ - + ]: 945 : if (PQgetisnull(res, rownum, i_attname))
11365 : 0 : pg_fatal("unexpected null attname");
11366 : 945 : attname = PQgetvalue(res, rownum, i_attname);
11367 : :
11368 : : /*
11369 : : * Indexes look up attname in indAttNames to derive attnum, all others
11370 : : * use attname directly. We must specify attnum for indexes, since
11371 : : * their attnames are not necessarily stable across dump/reload.
11372 : : */
11373 [ + + ]: 945 : if (rsinfo->nindAttNames == 0)
11374 : : {
11375 : 905 : appendPQExpBufferStr(out, ",\n\t'attname', ");
11376 : 905 : appendStringLiteralAH(out, attname, fout);
11377 : : }
11378 : : else
11379 : : {
11380 : 40 : bool found = false;
11381 : :
11382 [ + - ]: 74 : for (int i = 0; i < rsinfo->nindAttNames; i++)
11383 : : {
11384 [ + + ]: 74 : if (strcmp(attname, rsinfo->indAttNames[i]) == 0)
11385 : : {
11386 : 40 : appendPQExpBuffer(out, ",\n\t'attnum', '%d'::smallint",
11387 : : i + 1);
11388 : 40 : found = true;
11389 : 40 : break;
11390 : : }
11391 : : }
11392 : :
11393 [ - + ]: 40 : if (!found)
11394 : 0 : pg_fatal("could not find index attname \"%s\"", attname);
11395 : : }
11396 : :
11397 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_inherited))
11398 : 945 : appendNamedArgument(out, fout, "inherited", "boolean",
11399 : 945 : PQgetvalue(res, rownum, i_inherited));
11400 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_null_frac))
11401 : 945 : appendNamedArgument(out, fout, "null_frac", "real",
11402 : 945 : PQgetvalue(res, rownum, i_null_frac));
11403 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_avg_width))
11404 : 945 : appendNamedArgument(out, fout, "avg_width", "integer",
11405 : 945 : PQgetvalue(res, rownum, i_avg_width));
11406 [ + - ]: 945 : if (!PQgetisnull(res, rownum, i_n_distinct))
11407 : 945 : appendNamedArgument(out, fout, "n_distinct", "real",
11408 : 945 : PQgetvalue(res, rownum, i_n_distinct));
11409 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_vals))
11410 : 492 : appendNamedArgument(out, fout, "most_common_vals", "text",
11411 : 492 : PQgetvalue(res, rownum, i_most_common_vals));
11412 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_freqs))
11413 : 492 : appendNamedArgument(out, fout, "most_common_freqs", "real[]",
11414 : 492 : PQgetvalue(res, rownum, i_most_common_freqs));
11415 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_histogram_bounds))
11416 : 604 : appendNamedArgument(out, fout, "histogram_bounds", "text",
11417 : 604 : PQgetvalue(res, rownum, i_histogram_bounds));
11418 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_correlation))
11419 : 902 : appendNamedArgument(out, fout, "correlation", "real",
11420 : 902 : PQgetvalue(res, rownum, i_correlation));
11421 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_elems))
11422 : 8 : appendNamedArgument(out, fout, "most_common_elems", "text",
11423 : 8 : PQgetvalue(res, rownum, i_most_common_elems));
11424 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_most_common_elem_freqs))
11425 : 8 : appendNamedArgument(out, fout, "most_common_elem_freqs", "real[]",
11426 : 8 : PQgetvalue(res, rownum, i_most_common_elem_freqs));
11427 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_elem_count_histogram))
11428 : 7 : appendNamedArgument(out, fout, "elem_count_histogram", "real[]",
11429 : 7 : PQgetvalue(res, rownum, i_elem_count_histogram));
11430 [ + - ]: 945 : if (fout->remoteVersion >= 170000)
11431 : : {
11432 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_length_histogram))
11433 : 4 : appendNamedArgument(out, fout, "range_length_histogram", "text",
11434 : 4 : PQgetvalue(res, rownum, i_range_length_histogram));
11435 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_empty_frac))
11436 : 4 : appendNamedArgument(out, fout, "range_empty_frac", "real",
11437 : 4 : PQgetvalue(res, rownum, i_range_empty_frac));
11438 [ + + ]: 945 : if (!PQgetisnull(res, rownum, i_range_bounds_histogram))
11439 : 4 : appendNamedArgument(out, fout, "range_bounds_histogram", "text",
11440 : 4 : PQgetvalue(res, rownum, i_range_bounds_histogram));
11441 : : }
11442 : 945 : appendPQExpBufferStr(out, "\n);\n");
11443 : : }
11444 : :
11445 : 3724 : destroyPQExpBuffer(query);
11446 : 3724 : return out->data;
11447 : : }
11448 : :
11449 : : /*
11450 : : * dumpRelationStats --
11451 : : *
11452 : : * Make an ArchiveEntry for the relation statistics. The Archiver will take
11453 : : * care of gathering the statistics and generating the restore commands when
11454 : : * they are needed.
11455 : : */
11456 : : static void
11457 : 3800 : dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
11458 : : {
11459 : 3800 : const DumpableObject *dobj = &rsinfo->dobj;
11460 : :
11461 : : /* nothing to do if we are not dumping statistics */
11462 [ - + ]: 3800 : if (!fout->dopt->dumpStatistics)
11463 : 0 : return;
11464 : :
11465 : 3800 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11466 : 3800 : ARCHIVE_OPTS(.tag = dobj->name,
11467 : : .namespace = dobj->namespace->dobj.name,
11468 : : .description = "STATISTICS DATA",
11469 : : .section = rsinfo->section,
11470 : : .defnFn = dumpRelationStats_dumper,
11471 : : .defnArg = rsinfo,
11472 : : .deps = dobj->dependencies,
11473 : : .nDeps = dobj->nDeps));
11474 : : }
11475 : :
11476 : : /*
11477 : : * dumpTableComment --
11478 : : *
11479 : : * As above, but dump comments for both the specified table (or view)
11480 : : * and its columns.
11481 : : */
11482 : : static void
11483 : 79 : dumpTableComment(Archive *fout, const TableInfo *tbinfo,
11484 : : const char *reltypename)
11485 : : {
11486 : 79 : DumpOptions *dopt = fout->dopt;
11487 : : CommentItem *comments;
11488 : : int ncomments;
11489 : : PQExpBuffer query;
11490 : : PQExpBuffer tag;
11491 : :
11492 : : /* do nothing, if --no-comments is supplied */
11493 [ - + ]: 79 : if (dopt->no_comments)
11494 : 0 : return;
11495 : :
11496 : : /* Comments are SCHEMA not data */
11497 [ - + ]: 79 : if (!dopt->dumpSchema)
11498 : 0 : return;
11499 : :
11500 : : /* Search for comments associated with relation, using table */
11501 : 79 : ncomments = findComments(tbinfo->dobj.catId.tableoid,
11502 : 79 : tbinfo->dobj.catId.oid,
11503 : : &comments);
11504 : :
11505 : : /* If comments exist, build COMMENT ON statements */
11506 [ - + ]: 79 : if (ncomments <= 0)
11507 : 0 : return;
11508 : :
11509 : 79 : query = createPQExpBuffer();
11510 : 79 : tag = createPQExpBuffer();
11511 : :
11512 [ + + ]: 226 : while (ncomments > 0)
11513 : : {
11514 : 147 : const char *descr = comments->descr;
11515 : 147 : int objsubid = comments->objsubid;
11516 : :
11517 [ + + ]: 147 : if (objsubid == 0)
11518 : : {
11519 : 35 : resetPQExpBuffer(tag);
11520 : 35 : appendPQExpBuffer(tag, "%s %s", reltypename,
11521 : 35 : fmtId(tbinfo->dobj.name));
11522 : :
11523 : 35 : resetPQExpBuffer(query);
11524 : 35 : appendPQExpBuffer(query, "COMMENT ON %s %s IS ", reltypename,
11525 : 35 : fmtQualifiedDumpable(tbinfo));
11526 : 35 : appendStringLiteralAH(query, descr, fout);
11527 : 35 : appendPQExpBufferStr(query, ";\n");
11528 : :
11529 : 35 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11530 : 35 : ARCHIVE_OPTS(.tag = tag->data,
11531 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11532 : : .owner = tbinfo->rolname,
11533 : : .description = "COMMENT",
11534 : : .section = SECTION_NONE,
11535 : : .createStmt = query->data,
11536 : : .deps = &(tbinfo->dobj.dumpId),
11537 : : .nDeps = 1));
11538 : : }
11539 [ + - + - ]: 112 : else if (objsubid > 0 && objsubid <= tbinfo->numatts)
11540 : : {
11541 : 112 : resetPQExpBuffer(tag);
11542 : 112 : appendPQExpBuffer(tag, "COLUMN %s.",
11543 : 112 : fmtId(tbinfo->dobj.name));
11544 : 112 : appendPQExpBufferStr(tag, fmtId(tbinfo->attnames[objsubid - 1]));
11545 : :
11546 : 112 : resetPQExpBuffer(query);
11547 : 112 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11548 : 112 : fmtQualifiedDumpable(tbinfo));
11549 : 112 : appendPQExpBuffer(query, "%s IS ",
11550 : 112 : fmtId(tbinfo->attnames[objsubid - 1]));
11551 : 112 : appendStringLiteralAH(query, descr, fout);
11552 : 112 : appendPQExpBufferStr(query, ";\n");
11553 : :
11554 : 112 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11555 : 112 : ARCHIVE_OPTS(.tag = tag->data,
11556 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11557 : : .owner = tbinfo->rolname,
11558 : : .description = "COMMENT",
11559 : : .section = SECTION_NONE,
11560 : : .createStmt = query->data,
11561 : : .deps = &(tbinfo->dobj.dumpId),
11562 : : .nDeps = 1));
11563 : : }
11564 : :
11565 : 147 : comments++;
11566 : 147 : ncomments--;
11567 : : }
11568 : :
11569 : 79 : destroyPQExpBuffer(query);
11570 : 79 : destroyPQExpBuffer(tag);
11571 : : }
11572 : :
11573 : : /*
11574 : : * findComments --
11575 : : *
11576 : : * Find the comment(s), if any, associated with the given object. All the
11577 : : * objsubid values associated with the given classoid/objoid are found with
11578 : : * one search.
11579 : : */
11580 : : static int
11581 : 6780 : findComments(Oid classoid, Oid objoid, CommentItem **items)
11582 : : {
11583 : 6780 : CommentItem *middle = NULL;
11584 : : CommentItem *low;
11585 : : CommentItem *high;
11586 : : int nmatch;
11587 : :
11588 : : /*
11589 : : * Do binary search to find some item matching the object.
11590 : : */
11591 : 6780 : low = &comments[0];
11592 : 6780 : high = &comments[ncomments - 1];
11593 [ + + ]: 67905 : while (low <= high)
11594 : : {
11595 : 67858 : middle = low + (high - low) / 2;
11596 : :
11597 [ + + ]: 67858 : if (classoid < middle->classoid)
11598 : 7335 : high = middle - 1;
11599 [ + + ]: 60523 : else if (classoid > middle->classoid)
11600 : 7385 : low = middle + 1;
11601 [ + + ]: 53138 : else if (objoid < middle->objoid)
11602 : 22505 : high = middle - 1;
11603 [ + + ]: 30633 : else if (objoid > middle->objoid)
11604 : 23900 : low = middle + 1;
11605 : : else
11606 : 6733 : break; /* found a match */
11607 : : }
11608 : :
11609 [ + + ]: 6780 : if (low > high) /* no matches */
11610 : : {
11611 : 47 : *items = NULL;
11612 : 47 : return 0;
11613 : : }
11614 : :
11615 : : /*
11616 : : * Now determine how many items match the object. The search loop
11617 : : * invariant still holds: only items between low and high inclusive could
11618 : : * match.
11619 : : */
11620 : 6733 : nmatch = 1;
11621 [ + + ]: 6789 : while (middle > low)
11622 : : {
11623 [ + + ]: 3284 : if (classoid != middle[-1].classoid ||
11624 [ + + ]: 3121 : objoid != middle[-1].objoid)
11625 : : break;
11626 : 56 : middle--;
11627 : 56 : nmatch++;
11628 : : }
11629 : :
11630 : 6733 : *items = middle;
11631 : :
11632 : 6733 : middle += nmatch;
11633 [ + + ]: 6745 : while (middle <= high)
11634 : : {
11635 [ + + ]: 3491 : if (classoid != middle->classoid ||
11636 [ + + ]: 3369 : objoid != middle->objoid)
11637 : : break;
11638 : 12 : middle++;
11639 : 12 : nmatch++;
11640 : : }
11641 : :
11642 : 6733 : return nmatch;
11643 : : }
11644 : :
11645 : : /*
11646 : : * collectComments --
11647 : : *
11648 : : * Construct a table of all comments available for database objects;
11649 : : * also set the has-comment component flag for each relevant object.
11650 : : *
11651 : : * We used to do per-object queries for the comments, but it's much faster
11652 : : * to pull them all over at once, and on most databases the memory cost
11653 : : * isn't high.
11654 : : *
11655 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
11656 : : */
11657 : : static void
11658 : 215 : collectComments(Archive *fout)
11659 : : {
11660 : : PGresult *res;
11661 : : PQExpBuffer query;
11662 : : int i_description;
11663 : : int i_classoid;
11664 : : int i_objoid;
11665 : : int i_objsubid;
11666 : : int ntups;
11667 : : int i;
11668 : : DumpableObject *dobj;
11669 : :
11670 : 215 : query = createPQExpBuffer();
11671 : :
11672 : 215 : appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
11673 : : "FROM pg_catalog.pg_description "
11674 : : "ORDER BY classoid, objoid, objsubid");
11675 : :
11676 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11677 : :
11678 : : /* Construct lookup table containing OIDs in numeric form */
11679 : :
11680 : 215 : i_description = PQfnumber(res, "description");
11681 : 215 : i_classoid = PQfnumber(res, "classoid");
11682 : 215 : i_objoid = PQfnumber(res, "objoid");
11683 : 215 : i_objsubid = PQfnumber(res, "objsubid");
11684 : :
11685 : 215 : ntups = PQntuples(res);
11686 : :
11687 : 215 : comments = pg_malloc_array(CommentItem, ntups);
11688 : 215 : ncomments = 0;
11689 : 215 : dobj = NULL;
11690 : :
11691 [ + + ]: 1162514 : for (i = 0; i < ntups; i++)
11692 : : {
11693 : : CatalogId objId;
11694 : : int subid;
11695 : :
11696 : 1162299 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
11697 : 1162299 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
11698 : 1162299 : subid = atoi(PQgetvalue(res, i, i_objsubid));
11699 : :
11700 : : /* We needn't remember comments that don't match any dumpable object */
11701 [ + + ]: 1162299 : if (dobj == NULL ||
11702 [ + + ]: 421518 : dobj->catId.tableoid != objId.tableoid ||
11703 [ + + ]: 418901 : dobj->catId.oid != objId.oid)
11704 : 1162203 : dobj = findObjectByCatalogId(objId);
11705 [ + + ]: 1162299 : if (dobj == NULL)
11706 : 740572 : continue;
11707 : :
11708 : : /*
11709 : : * Comments on columns of composite types are linked to the type's
11710 : : * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
11711 : : * in the type's own DumpableObject.
11712 : : */
11713 [ + + + - ]: 421727 : if (subid != 0 && dobj->objType == DO_TABLE &&
11714 [ + + ]: 206 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
11715 : 48 : {
11716 : : TypeInfo *cTypeInfo;
11717 : :
11718 : 48 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
11719 [ + - ]: 48 : if (cTypeInfo)
11720 : 48 : cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
11721 : : }
11722 : : else
11723 : 421679 : dobj->components |= DUMP_COMPONENT_COMMENT;
11724 : :
11725 : 421727 : comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
11726 : 421727 : comments[ncomments].classoid = objId.tableoid;
11727 : 421727 : comments[ncomments].objoid = objId.oid;
11728 : 421727 : comments[ncomments].objsubid = subid;
11729 : 421727 : ncomments++;
11730 : : }
11731 : :
11732 : 215 : PQclear(res);
11733 : 215 : destroyPQExpBuffer(query);
11734 : 215 : }
11735 : :
11736 : : /*
11737 : : * dumpDumpableObject
11738 : : *
11739 : : * This routine and its subsidiaries are responsible for creating
11740 : : * ArchiveEntries (TOC objects) for each object to be dumped.
11741 : : */
11742 : : static void
11743 : 811577 : dumpDumpableObject(Archive *fout, DumpableObject *dobj)
11744 : : {
11745 : : /*
11746 : : * Clear any dump-request bits for components that don't exist for this
11747 : : * object. (This makes it safe to initially use DUMP_COMPONENT_ALL as the
11748 : : * request for every kind of object.)
11749 : : */
11750 : 811577 : dobj->dump &= dobj->components;
11751 : :
11752 : : /* Now, short-circuit if there's nothing to be done here. */
11753 [ + + ]: 811577 : if (dobj->dump == 0)
11754 : 725515 : return;
11755 : :
11756 [ + + + + : 86062 : switch (dobj->objType)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + - ]
11757 : : {
11758 : 590 : case DO_NAMESPACE:
11759 : 590 : dumpNamespace(fout, (const NamespaceInfo *) dobj);
11760 : 590 : break;
11761 : 25 : case DO_EXTENSION:
11762 : 25 : dumpExtension(fout, (const ExtensionInfo *) dobj);
11763 : 25 : break;
11764 : 1029 : case DO_TYPE:
11765 : 1029 : dumpType(fout, (const TypeInfo *) dobj);
11766 : 1029 : break;
11767 : 76 : case DO_SHELL_TYPE:
11768 : 76 : dumpShellType(fout, (const ShellTypeInfo *) dobj);
11769 : 76 : break;
11770 : 1892 : case DO_FUNC:
11771 : 1892 : dumpFunc(fout, (const FuncInfo *) dobj);
11772 : 1892 : break;
11773 : 295 : case DO_AGG:
11774 : 295 : dumpAgg(fout, (const AggInfo *) dobj);
11775 : 295 : break;
11776 : 2525 : case DO_OPERATOR:
11777 : 2525 : dumpOpr(fout, (const OprInfo *) dobj);
11778 : 2525 : break;
11779 : 84 : case DO_ACCESS_METHOD:
11780 : 84 : dumpAccessMethod(fout, (const AccessMethodInfo *) dobj);
11781 : 84 : break;
11782 : 675 : case DO_OPCLASS:
11783 : 675 : dumpOpclass(fout, (const OpclassInfo *) dobj);
11784 : 675 : break;
11785 : 561 : case DO_OPFAMILY:
11786 : 561 : dumpOpfamily(fout, (const OpfamilyInfo *) dobj);
11787 : 561 : break;
11788 : 2733 : case DO_COLLATION:
11789 : 2733 : dumpCollation(fout, (const CollInfo *) dobj);
11790 : 2733 : break;
11791 : 335 : case DO_CONVERSION:
11792 : 335 : dumpConversion(fout, (const ConvInfo *) dobj);
11793 : 335 : break;
11794 : 36969 : case DO_TABLE:
11795 : 36969 : dumpTable(fout, (const TableInfo *) dobj);
11796 : 36969 : break;
11797 : 1475 : case DO_TABLE_ATTACH:
11798 : 1475 : dumpTableAttach(fout, (const TableAttachInfo *) dobj);
11799 : 1475 : break;
11800 : 1143 : case DO_ATTRDEF:
11801 : 1143 : dumpAttrDef(fout, (const AttrDefInfo *) dobj);
11802 : 1143 : break;
11803 : 2791 : case DO_INDEX:
11804 : 2791 : dumpIndex(fout, (const IndxInfo *) dobj);
11805 : 2791 : break;
11806 : 625 : case DO_INDEX_ATTACH:
11807 : 625 : dumpIndexAttach(fout, (const IndexAttachInfo *) dobj);
11808 : 625 : break;
11809 : 196 : case DO_STATSEXT:
11810 : 196 : dumpStatisticsExt(fout, (const StatsExtInfo *) dobj);
11811 : 196 : dumpStatisticsExtStats(fout, (const StatsExtInfo *) dobj);
11812 : 196 : break;
11813 : 363 : case DO_REFRESH_MATVIEW:
11814 : 363 : refreshMatViewData(fout, (const TableDataInfo *) dobj);
11815 : 363 : break;
11816 : 1201 : case DO_RULE:
11817 : 1201 : dumpRule(fout, (const RuleInfo *) dobj);
11818 : 1201 : break;
11819 : 535 : case DO_TRIGGER:
11820 : 535 : dumpTrigger(fout, (const TriggerInfo *) dobj);
11821 : 535 : break;
11822 : 44 : case DO_EVENT_TRIGGER:
11823 : 44 : dumpEventTrigger(fout, (const EventTriggerInfo *) dobj);
11824 : 44 : break;
11825 : 2457 : case DO_CONSTRAINT:
11826 : 2457 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11827 : 2457 : break;
11828 : 177 : case DO_FK_CONSTRAINT:
11829 : 177 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11830 : 177 : break;
11831 : 100 : case DO_PROCLANG:
11832 : 100 : dumpProcLang(fout, (const ProcLangInfo *) dobj);
11833 : 100 : break;
11834 : 69 : case DO_CAST:
11835 : 69 : dumpCast(fout, (const CastInfo *) dobj);
11836 : 69 : break;
11837 : 44 : case DO_TRANSFORM:
11838 : 44 : dumpTransform(fout, (const TransformInfo *) dobj);
11839 : 44 : break;
11840 : 415 : case DO_SEQUENCE_SET:
11841 : 415 : dumpSequenceData(fout, (const TableDataInfo *) dobj);
11842 : 415 : break;
11843 : 4638 : case DO_TABLE_DATA:
11844 : 4638 : dumpTableData(fout, (const TableDataInfo *) dobj);
11845 : 4638 : break;
11846 : 15547 : case DO_DUMMY_TYPE:
11847 : : /* table rowtypes and array types are never dumped separately */
11848 : 15547 : break;
11849 : 44 : case DO_TSPARSER:
11850 : 44 : dumpTSParser(fout, (const TSParserInfo *) dobj);
11851 : 44 : break;
11852 : 182 : case DO_TSDICT:
11853 : 182 : dumpTSDictionary(fout, (const TSDictInfo *) dobj);
11854 : 182 : break;
11855 : 56 : case DO_TSTEMPLATE:
11856 : 56 : dumpTSTemplate(fout, (const TSTemplateInfo *) dobj);
11857 : 56 : break;
11858 : 157 : case DO_TSCONFIG:
11859 : 157 : dumpTSConfig(fout, (const TSConfigInfo *) dobj);
11860 : 157 : break;
11861 : 55 : case DO_FDW:
11862 : 55 : dumpForeignDataWrapper(fout, (const FdwInfo *) dobj);
11863 : 55 : break;
11864 : 59 : case DO_FOREIGN_SERVER:
11865 : 59 : dumpForeignServer(fout, (const ForeignServerInfo *) dobj);
11866 : 59 : break;
11867 : 170 : case DO_DEFAULT_ACL:
11868 : 170 : dumpDefaultACL(fout, (const DefaultACLInfo *) dobj);
11869 : 170 : break;
11870 : 88 : case DO_LARGE_OBJECT:
11871 : 88 : dumpLO(fout, (const LoInfo *) dobj);
11872 : 88 : break;
11873 : 88 : case DO_LARGE_OBJECT_DATA:
11874 [ + - ]: 88 : if (dobj->dump & DUMP_COMPONENT_DATA)
11875 : : {
11876 : : LoInfo *loinfo;
11877 : : TocEntry *te;
11878 : :
11879 : 88 : loinfo = (LoInfo *) findObjectByDumpId(dobj->dependencies[0]);
11880 [ - + ]: 88 : if (loinfo == NULL)
11881 : 0 : pg_fatal("missing metadata for large objects \"%s\"",
11882 : : dobj->name);
11883 : :
11884 : 88 : te = ArchiveEntry(fout, dobj->catId, dobj->dumpId,
11885 : 88 : ARCHIVE_OPTS(.tag = dobj->name,
11886 : : .owner = loinfo->rolname,
11887 : : .description = "BLOBS",
11888 : : .section = SECTION_DATA,
11889 : : .deps = dobj->dependencies,
11890 : : .nDeps = dobj->nDeps,
11891 : : .dumpFn = dumpLOs,
11892 : : .dumpArg = loinfo));
11893 : :
11894 : : /*
11895 : : * Set the TocEntry's dataLength in case we are doing a
11896 : : * parallel dump and want to order dump jobs by table size.
11897 : : * (We need some size estimate for every TocEntry with a
11898 : : * DataDumper function.) We don't currently have any cheap
11899 : : * way to estimate the size of LOs, but fortunately it doesn't
11900 : : * matter too much as long as we get large batches of LOs
11901 : : * processed reasonably early. Assume 8K per blob.
11902 : : */
11903 : 88 : te->dataLength = loinfo->numlos * (pgoff_t) 8192;
11904 : : }
11905 : 88 : break;
11906 : 347 : case DO_POLICY:
11907 : 347 : dumpPolicy(fout, (const PolicyInfo *) dobj);
11908 : 347 : break;
11909 : 416 : case DO_PUBLICATION:
11910 : 416 : dumpPublication(fout, (const PublicationInfo *) dobj);
11911 : 416 : break;
11912 : 298 : case DO_PUBLICATION_REL:
11913 : 298 : dumpPublicationTable(fout, (const PublicationRelInfo *) dobj);
11914 : 298 : break;
11915 : 103 : case DO_PUBLICATION_TABLE_IN_SCHEMA:
11916 : 103 : dumpPublicationNamespace(fout,
11917 : : (const PublicationSchemaInfo *) dobj);
11918 : 103 : break;
11919 : 157 : case DO_SUBSCRIPTION:
11920 : 157 : dumpSubscription(fout, (const SubscriptionInfo *) dobj);
11921 : 157 : break;
11922 : 3 : case DO_SUBSCRIPTION_REL:
11923 : 3 : dumpSubscriptionTable(fout, (const SubRelInfo *) dobj);
11924 : 3 : break;
11925 : 3800 : case DO_REL_STATS:
11926 : 3800 : dumpRelationStats(fout, (const RelStatsInfo *) dobj);
11927 : 3800 : break;
11928 : 430 : case DO_PRE_DATA_BOUNDARY:
11929 : : case DO_POST_DATA_BOUNDARY:
11930 : : /* never dumped, nothing to do */
11931 : 430 : break;
11932 : : }
11933 : : }
11934 : :
11935 : : /*
11936 : : * dumpNamespace
11937 : : * writes out to fout the queries to recreate a user-defined namespace
11938 : : */
11939 : : static void
11940 : 590 : dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo)
11941 : : {
11942 : 590 : DumpOptions *dopt = fout->dopt;
11943 : : PQExpBuffer q;
11944 : : PQExpBuffer delq;
11945 : : char *qnspname;
11946 : :
11947 : : /* Do nothing if not dumping schema */
11948 [ + + ]: 590 : if (!dopt->dumpSchema)
11949 : 29 : return;
11950 : :
11951 : 561 : q = createPQExpBuffer();
11952 : 561 : delq = createPQExpBuffer();
11953 : :
11954 : 561 : qnspname = pg_strdup(fmtId(nspinfo->dobj.name));
11955 : :
11956 [ + + ]: 561 : if (nspinfo->create)
11957 : : {
11958 : 382 : appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
11959 : 382 : appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname);
11960 : : }
11961 : : else
11962 : : {
11963 : : /* see selectDumpableNamespace() */
11964 : 179 : appendPQExpBufferStr(delq,
11965 : : "-- *not* dropping schema, since initdb creates it\n");
11966 : 179 : appendPQExpBufferStr(q,
11967 : : "-- *not* creating schema, since initdb creates it\n");
11968 : : }
11969 : :
11970 [ + + ]: 561 : if (dopt->binary_upgrade)
11971 : 150 : binary_upgrade_extension_member(q, &nspinfo->dobj,
11972 : : "SCHEMA", qnspname, NULL);
11973 : :
11974 [ + + ]: 561 : if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11975 : 207 : ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
11976 : 207 : ARCHIVE_OPTS(.tag = nspinfo->dobj.name,
11977 : : .owner = nspinfo->rolname,
11978 : : .description = "SCHEMA",
11979 : : .section = SECTION_PRE_DATA,
11980 : : .createStmt = q->data,
11981 : : .dropStmt = delq->data));
11982 : :
11983 : : /* Dump Schema Comments and Security Labels */
11984 [ + + ]: 561 : if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11985 : : {
11986 : 184 : const char *initdb_comment = NULL;
11987 : :
11988 [ + + + + ]: 184 : if (!nspinfo->create && strcmp(qnspname, "public") == 0)
11989 : 137 : initdb_comment = "standard public schema";
11990 : 184 : dumpCommentExtended(fout, "SCHEMA", qnspname,
11991 : 184 : NULL, nspinfo->rolname,
11992 : 184 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId,
11993 : : initdb_comment);
11994 : : }
11995 : :
11996 [ - + ]: 561 : if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11997 : 0 : dumpSecLabel(fout, "SCHEMA", qnspname,
11998 : 0 : NULL, nspinfo->rolname,
11999 : 0 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
12000 : :
12001 [ + + ]: 561 : if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL)
12002 : 457 : dumpACL(fout, nspinfo->dobj.dumpId, InvalidDumpId, "SCHEMA",
12003 : : qnspname, NULL, NULL,
12004 : 457 : NULL, nspinfo->rolname, &nspinfo->dacl);
12005 : :
12006 : 561 : pg_free(qnspname);
12007 : :
12008 : 561 : destroyPQExpBuffer(q);
12009 : 561 : destroyPQExpBuffer(delq);
12010 : : }
12011 : :
12012 : : /*
12013 : : * dumpExtension
12014 : : * writes out to fout the queries to recreate an extension
12015 : : */
12016 : : static void
12017 : 25 : dumpExtension(Archive *fout, const ExtensionInfo *extinfo)
12018 : : {
12019 : 25 : DumpOptions *dopt = fout->dopt;
12020 : : PQExpBuffer q;
12021 : : PQExpBuffer delq;
12022 : : char *qextname;
12023 : :
12024 : : /* Do nothing if not dumping schema */
12025 [ + + ]: 25 : if (!dopt->dumpSchema)
12026 : 1 : return;
12027 : :
12028 : 24 : q = createPQExpBuffer();
12029 : 24 : delq = createPQExpBuffer();
12030 : :
12031 : 24 : qextname = pg_strdup(fmtId(extinfo->dobj.name));
12032 : :
12033 : 24 : appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname);
12034 : :
12035 [ + + ]: 24 : if (!dopt->binary_upgrade)
12036 : : {
12037 : : /*
12038 : : * In a regular dump, we simply create the extension, intentionally
12039 : : * not specifying a version, so that the destination installation's
12040 : : * default version is used.
12041 : : *
12042 : : * Use of IF NOT EXISTS here is unlike our behavior for other object
12043 : : * types; but there are various scenarios in which it's convenient to
12044 : : * manually create the desired extension before restoring, so we
12045 : : * prefer to allow it to exist already.
12046 : : */
12047 : 17 : appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n",
12048 : 17 : qextname, fmtId(extinfo->namespace));
12049 : : }
12050 : : else
12051 : : {
12052 : : /*
12053 : : * In binary-upgrade mode, it's critical to reproduce the state of the
12054 : : * database exactly, so our procedure is to create an empty extension,
12055 : : * restore all the contained objects normally, and add them to the
12056 : : * extension one by one. This function performs just the first of
12057 : : * those steps. binary_upgrade_extension_member() takes care of
12058 : : * adding member objects as they're created.
12059 : : */
12060 : : int i;
12061 : : int n;
12062 : :
12063 : 7 : appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
12064 : :
12065 : : /*
12066 : : * We unconditionally create the extension, so we must drop it if it
12067 : : * exists. This could happen if the user deleted 'plpgsql' and then
12068 : : * readded it, causing its oid to be greater than g_last_builtin_oid.
12069 : : */
12070 : 7 : appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
12071 : :
12072 : 7 : appendPQExpBufferStr(q,
12073 : : "SELECT pg_catalog.binary_upgrade_create_empty_extension(");
12074 : 7 : appendStringLiteralAH(q, extinfo->dobj.name, fout);
12075 : 7 : appendPQExpBufferStr(q, ", ");
12076 : 7 : appendStringLiteralAH(q, extinfo->namespace, fout);
12077 : 7 : appendPQExpBufferStr(q, ", ");
12078 [ + - ]: 7 : appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false");
12079 : 7 : appendStringLiteralAH(q, extinfo->extversion, fout);
12080 : 7 : appendPQExpBufferStr(q, ", ");
12081 : :
12082 : : /*
12083 : : * Note that we're pushing extconfig (an OID array) back into
12084 : : * pg_extension exactly as-is. This is OK because pg_class OIDs are
12085 : : * preserved in binary upgrade.
12086 : : */
12087 [ + + ]: 7 : if (strlen(extinfo->extconfig) > 2)
12088 : 1 : appendStringLiteralAH(q, extinfo->extconfig, fout);
12089 : : else
12090 : 6 : appendPQExpBufferStr(q, "NULL");
12091 : 7 : appendPQExpBufferStr(q, ", ");
12092 [ + + ]: 7 : if (strlen(extinfo->extcondition) > 2)
12093 : 1 : appendStringLiteralAH(q, extinfo->extcondition, fout);
12094 : : else
12095 : 6 : appendPQExpBufferStr(q, "NULL");
12096 : 7 : appendPQExpBufferStr(q, ", ");
12097 : 7 : appendPQExpBufferStr(q, "ARRAY[");
12098 : 7 : n = 0;
12099 [ + + ]: 14 : for (i = 0; i < extinfo->dobj.nDeps; i++)
12100 : : {
12101 : : DumpableObject *extobj;
12102 : :
12103 : 7 : extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]);
12104 [ + - - + ]: 7 : if (extobj && extobj->objType == DO_EXTENSION)
12105 : : {
12106 [ # # ]: 0 : if (n++ > 0)
12107 : 0 : appendPQExpBufferChar(q, ',');
12108 : 0 : appendStringLiteralAH(q, extobj->name, fout);
12109 : : }
12110 : : }
12111 : 7 : appendPQExpBufferStr(q, "]::pg_catalog.text[]");
12112 : 7 : appendPQExpBufferStr(q, ");\n");
12113 : : }
12114 : :
12115 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12116 : 24 : ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId,
12117 : 24 : ARCHIVE_OPTS(.tag = extinfo->dobj.name,
12118 : : .description = "EXTENSION",
12119 : : .section = SECTION_PRE_DATA,
12120 : : .createStmt = q->data,
12121 : : .dropStmt = delq->data));
12122 : :
12123 : : /* Dump Extension Comments */
12124 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12125 : 24 : dumpComment(fout, "EXTENSION", qextname,
12126 : : NULL, "",
12127 : 24 : extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
12128 : :
12129 : 24 : pg_free(qextname);
12130 : :
12131 : 24 : destroyPQExpBuffer(q);
12132 : 24 : destroyPQExpBuffer(delq);
12133 : : }
12134 : :
12135 : : /*
12136 : : * dumpType
12137 : : * writes out to fout the queries to recreate a user-defined type
12138 : : */
12139 : : static void
12140 : 1029 : dumpType(Archive *fout, const TypeInfo *tyinfo)
12141 : : {
12142 : 1029 : DumpOptions *dopt = fout->dopt;
12143 : :
12144 : : /* Do nothing if not dumping schema */
12145 [ + + ]: 1029 : if (!dopt->dumpSchema)
12146 : 63 : return;
12147 : :
12148 : : /* Dump out in proper style */
12149 [ + + ]: 966 : if (tyinfo->typtype == TYPTYPE_BASE)
12150 : 285 : dumpBaseType(fout, tyinfo);
12151 [ + + ]: 681 : else if (tyinfo->typtype == TYPTYPE_DOMAIN)
12152 : 164 : dumpDomain(fout, tyinfo);
12153 [ + + ]: 517 : else if (tyinfo->typtype == TYPTYPE_COMPOSITE)
12154 : 177 : dumpCompositeType(fout, tyinfo);
12155 [ + + ]: 340 : else if (tyinfo->typtype == TYPTYPE_ENUM)
12156 : 105 : dumpEnumType(fout, tyinfo);
12157 [ + + ]: 235 : else if (tyinfo->typtype == TYPTYPE_RANGE)
12158 : 121 : dumpRangeType(fout, tyinfo);
12159 [ + - + + ]: 114 : else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
12160 : 39 : dumpUndefinedType(fout, tyinfo);
12161 : : else
12162 : 75 : pg_log_warning("typtype of data type \"%s\" appears to be invalid",
12163 : : tyinfo->dobj.name);
12164 : : }
12165 : :
12166 : : /*
12167 : : * dumpEnumType
12168 : : * writes out to fout the queries to recreate a user-defined enum type
12169 : : */
12170 : : static void
12171 : 105 : dumpEnumType(Archive *fout, const TypeInfo *tyinfo)
12172 : : {
12173 : 105 : DumpOptions *dopt = fout->dopt;
12174 : 105 : PQExpBuffer q = createPQExpBuffer();
12175 : 105 : PQExpBuffer delq = createPQExpBuffer();
12176 : 105 : PQExpBuffer query = createPQExpBuffer();
12177 : : PGresult *res;
12178 : : int num,
12179 : : i;
12180 : : Oid enum_oid;
12181 : : char *qtypname;
12182 : : char *qualtypname;
12183 : : char *label;
12184 : : int i_enumlabel;
12185 : : int i_oid;
12186 : :
12187 [ + + ]: 105 : if (!fout->is_prepared[PREPQUERY_DUMPENUMTYPE])
12188 : : {
12189 : : /* Set up query for enum-specific details */
12190 : 58 : appendPQExpBufferStr(query,
12191 : : "PREPARE dumpEnumType(pg_catalog.oid) AS\n"
12192 : : "SELECT oid, enumlabel "
12193 : : "FROM pg_catalog.pg_enum "
12194 : : "WHERE enumtypid = $1 "
12195 : : "ORDER BY enumsortorder");
12196 : :
12197 : 58 : ExecuteSqlStatement(fout, query->data);
12198 : :
12199 : 58 : fout->is_prepared[PREPQUERY_DUMPENUMTYPE] = true;
12200 : : }
12201 : :
12202 : 105 : printfPQExpBuffer(query,
12203 : : "EXECUTE dumpEnumType('%u')",
12204 : 105 : tyinfo->dobj.catId.oid);
12205 : :
12206 : 105 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12207 : :
12208 : 105 : num = PQntuples(res);
12209 : :
12210 : 105 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12211 : 105 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12212 : :
12213 : : /*
12214 : : * CASCADE shouldn't be required here as for normal types since the I/O
12215 : : * functions are generic and do not get dropped.
12216 : : */
12217 : 105 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12218 : :
12219 [ + + ]: 105 : if (dopt->binary_upgrade)
12220 : 6 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12221 : 6 : tyinfo->dobj.catId.oid,
12222 : : false, false);
12223 : :
12224 : 105 : appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
12225 : : qualtypname);
12226 : :
12227 [ + + ]: 105 : if (!dopt->binary_upgrade)
12228 : : {
12229 : 99 : i_enumlabel = PQfnumber(res, "enumlabel");
12230 : :
12231 : : /* Labels with server-assigned oids */
12232 [ + + ]: 562 : for (i = 0; i < num; i++)
12233 : : {
12234 : 463 : label = PQgetvalue(res, i, i_enumlabel);
12235 [ + + ]: 463 : if (i > 0)
12236 : 364 : appendPQExpBufferChar(q, ',');
12237 : 463 : appendPQExpBufferStr(q, "\n ");
12238 : 463 : appendStringLiteralAH(q, label, fout);
12239 : : }
12240 : : }
12241 : :
12242 : 105 : appendPQExpBufferStr(q, "\n);\n");
12243 : :
12244 [ + + ]: 105 : if (dopt->binary_upgrade)
12245 : : {
12246 : 6 : i_oid = PQfnumber(res, "oid");
12247 : 6 : i_enumlabel = PQfnumber(res, "enumlabel");
12248 : :
12249 : : /* Labels with dump-assigned (preserved) oids */
12250 [ + + ]: 62 : for (i = 0; i < num; i++)
12251 : : {
12252 : 56 : enum_oid = atooid(PQgetvalue(res, i, i_oid));
12253 : 56 : label = PQgetvalue(res, i, i_enumlabel);
12254 : :
12255 [ + + ]: 56 : if (i == 0)
12256 : 6 : appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
12257 : 56 : appendPQExpBuffer(q,
12258 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n",
12259 : : enum_oid);
12260 : 56 : appendPQExpBuffer(q, "ALTER TYPE %s ADD VALUE ", qualtypname);
12261 : 56 : appendStringLiteralAH(q, label, fout);
12262 : 56 : appendPQExpBufferStr(q, ";\n\n");
12263 : : }
12264 : : }
12265 : :
12266 [ + + ]: 105 : if (dopt->binary_upgrade)
12267 : 6 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12268 : : "TYPE", qtypname,
12269 : 6 : tyinfo->dobj.namespace->dobj.name);
12270 : :
12271 [ + - ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12272 : 105 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12273 : 105 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12274 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12275 : : .owner = tyinfo->rolname,
12276 : : .description = "TYPE",
12277 : : .section = SECTION_PRE_DATA,
12278 : : .createStmt = q->data,
12279 : : .dropStmt = delq->data));
12280 : :
12281 : : /* Dump Type Comments and Security Labels */
12282 [ + + ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12283 : 34 : dumpComment(fout, "TYPE", qtypname,
12284 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12285 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12286 : :
12287 [ - + ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12288 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12289 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12290 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12291 : :
12292 [ + + ]: 105 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12293 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12294 : : qtypname, NULL,
12295 : 34 : tyinfo->dobj.namespace->dobj.name,
12296 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12297 : :
12298 : 105 : PQclear(res);
12299 : 105 : destroyPQExpBuffer(q);
12300 : 105 : destroyPQExpBuffer(delq);
12301 : 105 : destroyPQExpBuffer(query);
12302 : 105 : pg_free(qtypname);
12303 : 105 : pg_free(qualtypname);
12304 : 105 : }
12305 : :
12306 : : /*
12307 : : * dumpRangeType
12308 : : * writes out to fout the queries to recreate a user-defined range type
12309 : : */
12310 : : static void
12311 : 121 : dumpRangeType(Archive *fout, const TypeInfo *tyinfo)
12312 : : {
12313 : 121 : DumpOptions *dopt = fout->dopt;
12314 : 121 : PQExpBuffer q = createPQExpBuffer();
12315 : 121 : PQExpBuffer delq = createPQExpBuffer();
12316 : 121 : PQExpBuffer query = createPQExpBuffer();
12317 : : PGresult *res;
12318 : : Oid collationOid;
12319 : : char *qtypname;
12320 : : char *qualtypname;
12321 : : char *procname;
12322 : :
12323 [ + + ]: 121 : if (!fout->is_prepared[PREPQUERY_DUMPRANGETYPE])
12324 : : {
12325 : : /* Set up query for range-specific details */
12326 : 42 : appendPQExpBufferStr(query,
12327 : : "PREPARE dumpRangeType(pg_catalog.oid) AS\n");
12328 : :
12329 : 42 : appendPQExpBufferStr(query,
12330 : : "SELECT ");
12331 : :
12332 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12333 : 42 : appendPQExpBufferStr(query,
12334 : : "pg_catalog.format_type(rngmultitypid, NULL) AS rngmultitype, ");
12335 : : else
12336 : 0 : appendPQExpBufferStr(query,
12337 : : "NULL AS rngmultitype, ");
12338 : :
12339 : 42 : appendPQExpBufferStr(query,
12340 : : "pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
12341 : : "opc.opcname AS opcname, "
12342 : : "(SELECT nspname FROM pg_catalog.pg_namespace nsp "
12343 : : " WHERE nsp.oid = opc.opcnamespace) AS opcnsp, "
12344 : : "opc.opcdefault, "
12345 : : "CASE WHEN rngcollation = st.typcollation THEN 0 "
12346 : : " ELSE rngcollation END AS collation, "
12347 : : "rngcanonical, rngsubdiff "
12348 : : "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, "
12349 : : " pg_catalog.pg_opclass opc "
12350 : : "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND "
12351 : : "rngtypid = $1");
12352 : :
12353 : 42 : ExecuteSqlStatement(fout, query->data);
12354 : :
12355 : 42 : fout->is_prepared[PREPQUERY_DUMPRANGETYPE] = true;
12356 : : }
12357 : :
12358 : 121 : printfPQExpBuffer(query,
12359 : : "EXECUTE dumpRangeType('%u')",
12360 : 121 : tyinfo->dobj.catId.oid);
12361 : :
12362 : 121 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12363 : :
12364 : 121 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12365 : 121 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12366 : :
12367 : : /*
12368 : : * CASCADE shouldn't be required here as for normal types since the I/O
12369 : : * functions are generic and do not get dropped.
12370 : : */
12371 : 121 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12372 : :
12373 [ + + ]: 121 : if (dopt->binary_upgrade)
12374 : 9 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12375 : 9 : tyinfo->dobj.catId.oid,
12376 : : false, true);
12377 : :
12378 : 121 : appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
12379 : : qualtypname);
12380 : :
12381 : 121 : appendPQExpBuffer(q, "\n subtype = %s",
12382 : : PQgetvalue(res, 0, PQfnumber(res, "rngsubtype")));
12383 : :
12384 [ + - ]: 121 : if (!PQgetisnull(res, 0, PQfnumber(res, "rngmultitype")))
12385 : 121 : appendPQExpBuffer(q, ",\n multirange_type_name = %s",
12386 : : PQgetvalue(res, 0, PQfnumber(res, "rngmultitype")));
12387 : :
12388 : : /* print subtype_opclass only if not default for subtype */
12389 [ + + ]: 121 : if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't')
12390 : : {
12391 : 34 : char *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname"));
12392 : 34 : char *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp"));
12393 : :
12394 : 34 : appendPQExpBuffer(q, ",\n subtype_opclass = %s.",
12395 : : fmtId(nspname));
12396 : 34 : appendPQExpBufferStr(q, fmtId(opcname));
12397 : : }
12398 : :
12399 : 121 : collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation")));
12400 [ + + ]: 121 : if (OidIsValid(collationOid))
12401 : : {
12402 : 39 : CollInfo *coll = findCollationByOid(collationOid);
12403 : :
12404 [ + - ]: 39 : if (coll)
12405 : 39 : appendPQExpBuffer(q, ",\n collation = %s",
12406 : 39 : fmtQualifiedDumpable(coll));
12407 : : }
12408 : :
12409 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical"));
12410 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12411 : 9 : appendPQExpBuffer(q, ",\n canonical = %s", procname);
12412 : :
12413 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff"));
12414 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12415 : 23 : appendPQExpBuffer(q, ",\n subtype_diff = %s", procname);
12416 : :
12417 : 121 : appendPQExpBufferStr(q, "\n);\n");
12418 : :
12419 [ + + ]: 121 : if (dopt->binary_upgrade)
12420 : 9 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12421 : : "TYPE", qtypname,
12422 : 9 : tyinfo->dobj.namespace->dobj.name);
12423 : :
12424 [ + - ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12425 : 121 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12426 : 121 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12427 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12428 : : .owner = tyinfo->rolname,
12429 : : .description = "TYPE",
12430 : : .section = SECTION_PRE_DATA,
12431 : : .createStmt = q->data,
12432 : : .dropStmt = delq->data));
12433 : :
12434 : : /* Dump Type Comments and Security Labels */
12435 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12436 : 52 : dumpComment(fout, "TYPE", qtypname,
12437 : 52 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12438 : 52 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12439 : :
12440 [ - + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12441 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12442 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12443 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12444 : :
12445 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12446 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12447 : : qtypname, NULL,
12448 : 34 : tyinfo->dobj.namespace->dobj.name,
12449 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12450 : :
12451 : 121 : PQclear(res);
12452 : 121 : destroyPQExpBuffer(q);
12453 : 121 : destroyPQExpBuffer(delq);
12454 : 121 : destroyPQExpBuffer(query);
12455 : 121 : pg_free(qtypname);
12456 : 121 : pg_free(qualtypname);
12457 : 121 : }
12458 : :
12459 : : /*
12460 : : * dumpUndefinedType
12461 : : * writes out to fout the queries to recreate a !typisdefined type
12462 : : *
12463 : : * This is a shell type, but we use different terminology to distinguish
12464 : : * this case from where we have to emit a shell type definition to break
12465 : : * circular dependencies. An undefined type shouldn't ever have anything
12466 : : * depending on it.
12467 : : */
12468 : : static void
12469 : 39 : dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo)
12470 : : {
12471 : 39 : DumpOptions *dopt = fout->dopt;
12472 : 39 : PQExpBuffer q = createPQExpBuffer();
12473 : 39 : PQExpBuffer delq = createPQExpBuffer();
12474 : : char *qtypname;
12475 : : char *qualtypname;
12476 : :
12477 : 39 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12478 : 39 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12479 : :
12480 : 39 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12481 : :
12482 [ + + ]: 39 : if (dopt->binary_upgrade)
12483 : 2 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12484 : 2 : tyinfo->dobj.catId.oid,
12485 : : false, false);
12486 : :
12487 : 39 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
12488 : : qualtypname);
12489 : :
12490 [ + + ]: 39 : if (dopt->binary_upgrade)
12491 : 2 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12492 : : "TYPE", qtypname,
12493 : 2 : tyinfo->dobj.namespace->dobj.name);
12494 : :
12495 [ + - ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12496 : 39 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12497 : 39 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12498 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12499 : : .owner = tyinfo->rolname,
12500 : : .description = "TYPE",
12501 : : .section = SECTION_PRE_DATA,
12502 : : .createStmt = q->data,
12503 : : .dropStmt = delq->data));
12504 : :
12505 : : /* Dump Type Comments and Security Labels */
12506 [ + + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12507 : 34 : dumpComment(fout, "TYPE", qtypname,
12508 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12509 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12510 : :
12511 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12512 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12513 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12514 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12515 : :
12516 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12517 : 0 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12518 : : qtypname, NULL,
12519 : 0 : tyinfo->dobj.namespace->dobj.name,
12520 : 0 : NULL, tyinfo->rolname, &tyinfo->dacl);
12521 : :
12522 : 39 : destroyPQExpBuffer(q);
12523 : 39 : destroyPQExpBuffer(delq);
12524 : 39 : pg_free(qtypname);
12525 : 39 : pg_free(qualtypname);
12526 : 39 : }
12527 : :
12528 : : /*
12529 : : * dumpBaseType
12530 : : * writes out to fout the queries to recreate a user-defined base type
12531 : : */
12532 : : static void
12533 : 285 : dumpBaseType(Archive *fout, const TypeInfo *tyinfo)
12534 : : {
12535 : 285 : DumpOptions *dopt = fout->dopt;
12536 : 285 : PQExpBuffer q = createPQExpBuffer();
12537 : 285 : PQExpBuffer delq = createPQExpBuffer();
12538 : 285 : PQExpBuffer query = createPQExpBuffer();
12539 : : PGresult *res;
12540 : : char *qtypname;
12541 : : char *qualtypname;
12542 : : char *typlen;
12543 : : char *typinput;
12544 : : char *typoutput;
12545 : : char *typreceive;
12546 : : char *typsend;
12547 : : char *typmodin;
12548 : : char *typmodout;
12549 : : char *typanalyze;
12550 : : char *typsubscript;
12551 : : Oid typreceiveoid;
12552 : : Oid typsendoid;
12553 : : Oid typmodinoid;
12554 : : Oid typmodoutoid;
12555 : : Oid typanalyzeoid;
12556 : : Oid typsubscriptoid;
12557 : : char *typcategory;
12558 : : char *typispreferred;
12559 : : char *typdelim;
12560 : : char *typbyval;
12561 : : char *typalign;
12562 : : char *typstorage;
12563 : : char *typcollatable;
12564 : : char *typdefault;
12565 : 285 : bool typdefault_is_literal = false;
12566 : :
12567 [ + + ]: 285 : if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
12568 : : {
12569 : : /* Set up query for type-specific details */
12570 : 42 : appendPQExpBufferStr(query,
12571 : : "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
12572 : : "SELECT typlen, "
12573 : : "typinput, typoutput, typreceive, typsend, "
12574 : : "typreceive::pg_catalog.oid AS typreceiveoid, "
12575 : : "typsend::pg_catalog.oid AS typsendoid, "
12576 : : "typanalyze, "
12577 : : "typanalyze::pg_catalog.oid AS typanalyzeoid, "
12578 : : "typdelim, typbyval, typalign, typstorage, "
12579 : : "typmodin, typmodout, "
12580 : : "typmodin::pg_catalog.oid AS typmodinoid, "
12581 : : "typmodout::pg_catalog.oid AS typmodoutoid, "
12582 : : "typcategory, typispreferred, "
12583 : : "(typcollation <> 0) AS typcollatable, "
12584 : : "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
12585 : :
12586 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12587 : 42 : appendPQExpBufferStr(query,
12588 : : "typsubscript, "
12589 : : "typsubscript::pg_catalog.oid AS typsubscriptoid ");
12590 : : else
12591 : 0 : appendPQExpBufferStr(query,
12592 : : "'-' AS typsubscript, 0 AS typsubscriptoid ");
12593 : :
12594 : 42 : appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
12595 : : "WHERE oid = $1");
12596 : :
12597 : 42 : ExecuteSqlStatement(fout, query->data);
12598 : :
12599 : 42 : fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
12600 : : }
12601 : :
12602 : 285 : printfPQExpBuffer(query,
12603 : : "EXECUTE dumpBaseType('%u')",
12604 : 285 : tyinfo->dobj.catId.oid);
12605 : :
12606 : 285 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12607 : :
12608 : 285 : typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
12609 : 285 : typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
12610 : 285 : typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
12611 : 285 : typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
12612 : 285 : typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
12613 : 285 : typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
12614 : 285 : typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
12615 : 285 : typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
12616 : 285 : typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
12617 : 285 : typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
12618 : 285 : typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
12619 : 285 : typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
12620 : 285 : typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
12621 : 285 : typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
12622 : 285 : typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
12623 : 285 : typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
12624 : 285 : typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
12625 : 285 : typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
12626 : 285 : typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
12627 : 285 : typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
12628 : 285 : typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
12629 : 285 : typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
12630 [ - + ]: 285 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12631 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12632 [ + + ]: 285 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12633 : : {
12634 : 44 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12635 : 44 : typdefault_is_literal = true; /* it needs quotes */
12636 : : }
12637 : : else
12638 : 241 : typdefault = NULL;
12639 : :
12640 : 285 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12641 : 285 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12642 : :
12643 : : /*
12644 : : * The reason we include CASCADE is that the circular dependency between
12645 : : * the type and its I/O functions makes it impossible to drop the type any
12646 : : * other way.
12647 : : */
12648 : 285 : appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
12649 : :
12650 : : /*
12651 : : * We might already have a shell type, but setting pg_type_oid is
12652 : : * harmless, and in any case we'd better set the array type OID.
12653 : : */
12654 [ + + ]: 285 : if (dopt->binary_upgrade)
12655 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12656 : 8 : tyinfo->dobj.catId.oid,
12657 : : false, false);
12658 : :
12659 : 285 : appendPQExpBuffer(q,
12660 : : "CREATE TYPE %s (\n"
12661 : : " INTERNALLENGTH = %s",
12662 : : qualtypname,
12663 [ + + ]: 285 : (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
12664 : :
12665 : : /* regproc result is sufficiently quoted already */
12666 : 285 : appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
12667 : 285 : appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
12668 [ + + ]: 285 : if (OidIsValid(typreceiveoid))
12669 : 210 : appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
12670 [ + + ]: 285 : if (OidIsValid(typsendoid))
12671 : 210 : appendPQExpBuffer(q, ",\n SEND = %s", typsend);
12672 [ + + ]: 285 : if (OidIsValid(typmodinoid))
12673 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
12674 [ + + ]: 285 : if (OidIsValid(typmodoutoid))
12675 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
12676 [ + + ]: 285 : if (OidIsValid(typanalyzeoid))
12677 : 3 : appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
12678 : :
12679 [ + + ]: 285 : if (strcmp(typcollatable, "t") == 0)
12680 : 30 : appendPQExpBufferStr(q, ",\n COLLATABLE = true");
12681 : :
12682 [ + + ]: 285 : if (typdefault != NULL)
12683 : : {
12684 : 44 : appendPQExpBufferStr(q, ",\n DEFAULT = ");
12685 [ + - ]: 44 : if (typdefault_is_literal)
12686 : 44 : appendStringLiteralAH(q, typdefault, fout);
12687 : : else
12688 : 0 : appendPQExpBufferStr(q, typdefault);
12689 : : }
12690 : :
12691 [ + + ]: 285 : if (OidIsValid(typsubscriptoid))
12692 : 29 : appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
12693 : :
12694 [ + + ]: 285 : if (OidIsValid(tyinfo->typelem))
12695 : 26 : appendPQExpBuffer(q, ",\n ELEMENT = %s",
12696 : 26 : getFormattedTypeName(fout, tyinfo->typelem,
12697 : : zeroIsError));
12698 : :
12699 [ + + ]: 285 : if (strcmp(typcategory, "U") != 0)
12700 : : {
12701 : 161 : appendPQExpBufferStr(q, ",\n CATEGORY = ");
12702 : 161 : appendStringLiteralAH(q, typcategory, fout);
12703 : : }
12704 : :
12705 [ + + ]: 285 : if (strcmp(typispreferred, "t") == 0)
12706 : 29 : appendPQExpBufferStr(q, ",\n PREFERRED = true");
12707 : :
12708 [ + - + + ]: 285 : if (typdelim && strcmp(typdelim, ",") != 0)
12709 : : {
12710 : 3 : appendPQExpBufferStr(q, ",\n DELIMITER = ");
12711 : 3 : appendStringLiteralAH(q, typdelim, fout);
12712 : : }
12713 : :
12714 [ + + ]: 285 : if (*typalign == TYPALIGN_CHAR)
12715 : 12 : appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
12716 [ + + ]: 273 : else if (*typalign == TYPALIGN_SHORT)
12717 : 6 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
12718 [ + + ]: 267 : else if (*typalign == TYPALIGN_INT)
12719 : 189 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
12720 [ + - ]: 78 : else if (*typalign == TYPALIGN_DOUBLE)
12721 : 78 : appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
12722 : :
12723 [ + + ]: 285 : if (*typstorage == TYPSTORAGE_PLAIN)
12724 : 210 : appendPQExpBufferStr(q, ",\n STORAGE = plain");
12725 [ - + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTERNAL)
12726 : 0 : appendPQExpBufferStr(q, ",\n STORAGE = external");
12727 [ + + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTENDED)
12728 : 66 : appendPQExpBufferStr(q, ",\n STORAGE = extended");
12729 [ + - ]: 9 : else if (*typstorage == TYPSTORAGE_MAIN)
12730 : 9 : appendPQExpBufferStr(q, ",\n STORAGE = main");
12731 : :
12732 [ + + ]: 285 : if (strcmp(typbyval, "t") == 0)
12733 : 139 : appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
12734 : :
12735 : 285 : appendPQExpBufferStr(q, "\n);\n");
12736 : :
12737 [ + + ]: 285 : if (dopt->binary_upgrade)
12738 : 8 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12739 : : "TYPE", qtypname,
12740 : 8 : tyinfo->dobj.namespace->dobj.name);
12741 : :
12742 [ + - ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12743 : 285 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12744 : 285 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12745 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12746 : : .owner = tyinfo->rolname,
12747 : : .description = "TYPE",
12748 : : .section = SECTION_PRE_DATA,
12749 : : .createStmt = q->data,
12750 : : .dropStmt = delq->data));
12751 : :
12752 : : /* Dump Type Comments and Security Labels */
12753 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12754 : 250 : dumpComment(fout, "TYPE", qtypname,
12755 : 250 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12756 : 250 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12757 : :
12758 [ - + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12759 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12760 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12761 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12762 : :
12763 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12764 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12765 : : qtypname, NULL,
12766 : 34 : tyinfo->dobj.namespace->dobj.name,
12767 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12768 : :
12769 : 285 : PQclear(res);
12770 : 285 : destroyPQExpBuffer(q);
12771 : 285 : destroyPQExpBuffer(delq);
12772 : 285 : destroyPQExpBuffer(query);
12773 : 285 : pg_free(qtypname);
12774 : 285 : pg_free(qualtypname);
12775 : 285 : }
12776 : :
12777 : : /*
12778 : : * dumpDomain
12779 : : * writes out to fout the queries to recreate a user-defined domain
12780 : : */
12781 : : static void
12782 : 164 : dumpDomain(Archive *fout, const TypeInfo *tyinfo)
12783 : : {
12784 : 164 : DumpOptions *dopt = fout->dopt;
12785 : 164 : PQExpBuffer q = createPQExpBuffer();
12786 : 164 : PQExpBuffer delq = createPQExpBuffer();
12787 : 164 : PQExpBuffer query = createPQExpBuffer();
12788 : : PGresult *res;
12789 : : int i;
12790 : : char *qtypname;
12791 : : char *qualtypname;
12792 : : char *typnotnull;
12793 : : char *typdefn;
12794 : : char *typdefault;
12795 : : Oid typcollation;
12796 : 164 : bool typdefault_is_literal = false;
12797 : :
12798 [ + + ]: 164 : if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN])
12799 : : {
12800 : : /* Set up query for domain-specific details */
12801 : 39 : appendPQExpBufferStr(query,
12802 : : "PREPARE dumpDomain(pg_catalog.oid) AS\n");
12803 : :
12804 : 39 : appendPQExpBufferStr(query, "SELECT t.typnotnull, "
12805 : : "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
12806 : : "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
12807 : : "t.typdefault, "
12808 : : "CASE WHEN t.typcollation <> u.typcollation "
12809 : : "THEN t.typcollation ELSE 0 END AS typcollation "
12810 : : "FROM pg_catalog.pg_type t "
12811 : : "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
12812 : : "WHERE t.oid = $1");
12813 : :
12814 : 39 : ExecuteSqlStatement(fout, query->data);
12815 : :
12816 : 39 : fout->is_prepared[PREPQUERY_DUMPDOMAIN] = true;
12817 : : }
12818 : :
12819 : 164 : printfPQExpBuffer(query,
12820 : : "EXECUTE dumpDomain('%u')",
12821 : 164 : tyinfo->dobj.catId.oid);
12822 : :
12823 : 164 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12824 : :
12825 : 164 : typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
12826 : 164 : typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
12827 [ + + ]: 164 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12828 : 39 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12829 [ - + ]: 125 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12830 : : {
12831 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12832 : 0 : typdefault_is_literal = true; /* it needs quotes */
12833 : : }
12834 : : else
12835 : 125 : typdefault = NULL;
12836 : 164 : typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
12837 : :
12838 [ + + ]: 164 : if (dopt->binary_upgrade)
12839 : 27 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12840 : 27 : tyinfo->dobj.catId.oid,
12841 : : true, /* force array type */
12842 : : false); /* force multirange type */
12843 : :
12844 : 164 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12845 : 164 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12846 : :
12847 : 164 : appendPQExpBuffer(q,
12848 : : "CREATE DOMAIN %s AS %s",
12849 : : qualtypname,
12850 : : typdefn);
12851 : :
12852 : : /* Print collation only if different from base type's collation */
12853 [ + + ]: 164 : if (OidIsValid(typcollation))
12854 : : {
12855 : : CollInfo *coll;
12856 : :
12857 : 34 : coll = findCollationByOid(typcollation);
12858 [ + - ]: 34 : if (coll)
12859 : 34 : appendPQExpBuffer(q, " COLLATE %s", fmtQualifiedDumpable(coll));
12860 : : }
12861 : :
12862 : : /*
12863 : : * Print a not-null constraint if there's one. In servers older than 17
12864 : : * these don't have names, so just print it unadorned; in newer ones they
12865 : : * do, but most of the time it's going to be the standard generated one,
12866 : : * so omit the name in that case also.
12867 : : */
12868 [ + + ]: 164 : if (typnotnull[0] == 't')
12869 : : {
12870 [ + - - + ]: 49 : if (fout->remoteVersion < 170000 || tyinfo->notnull == NULL)
12871 : 0 : appendPQExpBufferStr(q, " NOT NULL");
12872 : : else
12873 : : {
12874 : 49 : ConstraintInfo *notnull = tyinfo->notnull;
12875 : :
12876 [ + - ]: 49 : if (!notnull->separate)
12877 : : {
12878 : : char *default_name;
12879 : :
12880 : : /* XXX should match ChooseConstraintName better */
12881 : 49 : default_name = psprintf("%s_not_null", tyinfo->dobj.name);
12882 : :
12883 [ + + ]: 49 : if (strcmp(default_name, notnull->dobj.name) == 0)
12884 : 15 : appendPQExpBufferStr(q, " NOT NULL");
12885 : : else
12886 : 34 : appendPQExpBuffer(q, " CONSTRAINT %s %s",
12887 : 34 : fmtId(notnull->dobj.name), notnull->condef);
12888 : 49 : pfree(default_name);
12889 : : }
12890 : : }
12891 : : }
12892 : :
12893 [ + + ]: 164 : if (typdefault != NULL)
12894 : : {
12895 : 39 : appendPQExpBufferStr(q, " DEFAULT ");
12896 [ - + ]: 39 : if (typdefault_is_literal)
12897 : 0 : appendStringLiteralAH(q, typdefault, fout);
12898 : : else
12899 : 39 : appendPQExpBufferStr(q, typdefault);
12900 : : }
12901 : :
12902 : 164 : PQclear(res);
12903 : :
12904 : : /*
12905 : : * Add any CHECK constraints for the domain
12906 : : */
12907 [ + + ]: 283 : for (i = 0; i < tyinfo->nDomChecks; i++)
12908 : : {
12909 : 119 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12910 : :
12911 [ + + + - ]: 119 : if (!domcheck->separate && domcheck->contype == 'c')
12912 : 114 : appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
12913 : 114 : fmtId(domcheck->dobj.name), domcheck->condef);
12914 : : }
12915 : :
12916 : 164 : appendPQExpBufferStr(q, ";\n");
12917 : :
12918 : 164 : appendPQExpBuffer(delq, "DROP DOMAIN %s;\n", qualtypname);
12919 : :
12920 [ + + ]: 164 : if (dopt->binary_upgrade)
12921 : 27 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12922 : : "DOMAIN", qtypname,
12923 : 27 : tyinfo->dobj.namespace->dobj.name);
12924 : :
12925 [ + - ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12926 : 164 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12927 : 164 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12928 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12929 : : .owner = tyinfo->rolname,
12930 : : .description = "DOMAIN",
12931 : : .section = SECTION_PRE_DATA,
12932 : : .createStmt = q->data,
12933 : : .dropStmt = delq->data));
12934 : :
12935 : : /* Dump Domain Comments and Security Labels */
12936 [ - + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12937 : 0 : dumpComment(fout, "DOMAIN", qtypname,
12938 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12939 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12940 : :
12941 [ - + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12942 : 0 : dumpSecLabel(fout, "DOMAIN", qtypname,
12943 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12944 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12945 : :
12946 [ + + ]: 164 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12947 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12948 : : qtypname, NULL,
12949 : 34 : tyinfo->dobj.namespace->dobj.name,
12950 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12951 : :
12952 : : /* Dump any per-constraint comments */
12953 [ + + ]: 283 : for (i = 0; i < tyinfo->nDomChecks; i++)
12954 : : {
12955 : 119 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12956 : : PQExpBuffer conprefix;
12957 : :
12958 : : /* but only if the constraint itself was dumped here */
12959 [ + + ]: 119 : if (domcheck->separate)
12960 : 5 : continue;
12961 : :
12962 : 114 : conprefix = createPQExpBuffer();
12963 : 114 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12964 : 114 : fmtId(domcheck->dobj.name));
12965 : :
12966 [ + + ]: 114 : if (domcheck->dobj.dump & DUMP_COMPONENT_COMMENT)
12967 : 34 : dumpComment(fout, conprefix->data, qtypname,
12968 : 34 : tyinfo->dobj.namespace->dobj.name,
12969 : 34 : tyinfo->rolname,
12970 : 34 : domcheck->dobj.catId, 0, tyinfo->dobj.dumpId);
12971 : :
12972 : 114 : destroyPQExpBuffer(conprefix);
12973 : : }
12974 : :
12975 : : /*
12976 : : * And a comment on the not-null constraint, if there's one -- but only if
12977 : : * the constraint itself was dumped here
12978 : : */
12979 [ + + + - ]: 164 : if (tyinfo->notnull != NULL && !tyinfo->notnull->separate)
12980 : : {
12981 : 49 : PQExpBuffer conprefix = createPQExpBuffer();
12982 : :
12983 : 49 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12984 : 49 : fmtId(tyinfo->notnull->dobj.name));
12985 : :
12986 [ + + ]: 49 : if (tyinfo->notnull->dobj.dump & DUMP_COMPONENT_COMMENT)
12987 : 34 : dumpComment(fout, conprefix->data, qtypname,
12988 : 34 : tyinfo->dobj.namespace->dobj.name,
12989 : 34 : tyinfo->rolname,
12990 : 34 : tyinfo->notnull->dobj.catId, 0, tyinfo->dobj.dumpId);
12991 : 49 : destroyPQExpBuffer(conprefix);
12992 : : }
12993 : :
12994 : 164 : destroyPQExpBuffer(q);
12995 : 164 : destroyPQExpBuffer(delq);
12996 : 164 : destroyPQExpBuffer(query);
12997 : 164 : pg_free(qtypname);
12998 : 164 : pg_free(qualtypname);
12999 : 164 : }
13000 : :
13001 : : /*
13002 : : * dumpCompositeType
13003 : : * writes out to fout the queries to recreate a user-defined stand-alone
13004 : : * composite type
13005 : : */
13006 : : static void
13007 : 177 : dumpCompositeType(Archive *fout, const TypeInfo *tyinfo)
13008 : : {
13009 : 177 : DumpOptions *dopt = fout->dopt;
13010 : 177 : PQExpBuffer q = createPQExpBuffer();
13011 : 177 : PQExpBuffer dropped = createPQExpBuffer();
13012 : 177 : PQExpBuffer delq = createPQExpBuffer();
13013 : 177 : PQExpBuffer query = createPQExpBuffer();
13014 : : PGresult *res;
13015 : : char *qtypname;
13016 : : char *qualtypname;
13017 : : int ntups;
13018 : : int i_attname;
13019 : : int i_atttypdefn;
13020 : : int i_attlen;
13021 : : int i_attalign;
13022 : : int i_attisdropped;
13023 : : int i_attcollation;
13024 : : int i_attnum;
13025 : : int i_attversion;
13026 : : int i;
13027 : : int actual_atts;
13028 : 177 : uint64 *attversions = NULL;
13029 : :
13030 [ + + ]: 177 : if (!fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE])
13031 : : {
13032 : : /*
13033 : : * Set up query for type-specific details.
13034 : : *
13035 : : * Since we only want to dump COLLATE clauses for attributes whose
13036 : : * collation is different from their type's default, we use a CASE
13037 : : * here to suppress uninteresting attcollations cheaply. atttypid
13038 : : * will be 0 for dropped columns; collation does not matter for those.
13039 : : */
13040 : 58 : appendPQExpBufferStr(query,
13041 : : "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
13042 : : "SELECT a.attname, a.attnum, "
13043 : : "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
13044 : : "a.attlen, a.attalign, a.attisdropped, "
13045 : : "CASE WHEN a.attcollation <> at.typcollation "
13046 : : "THEN a.attcollation ELSE 0 END AS attcollation, ");
13047 : :
13048 : : /*
13049 : : * See similar code in getTableAttrs().
13050 : : *
13051 : : * XXX: This query is non-trivial enough to make deduplicating with
13052 : : * getTableAttrs() worthwhile. But ISTM the proper way for that is to
13053 : : * deduplicate all of getTableAttrs() and dumpCompositeType().
13054 : : */
13055 [ + - ]: 58 : if (fout->remoteVersion >= 200000)
13056 : 58 : appendPQExpBufferStr(query,
13057 : : "a.attversion - '0/0' AS attversion ");
13058 : : else
13059 : 0 : appendPQExpBuffer(query,
13060 : : "(\n"
13061 : : " SELECT substr(o, %2$zu + 1)\n"
13062 : : " FROM unnest(a.attoptions) o\n"
13063 : : " WHERE substr(o, 1, %2$zu) = '%1$s=') AS attversion\n",
13064 : : ATTRIBUTE_VERSION_ATTOPTION,
13065 : : strlen(ATTRIBUTE_VERSION_ATTOPTION) + 1);
13066 : :
13067 : 58 : appendPQExpBufferStr(query,
13068 : : "FROM pg_catalog.pg_type ct "
13069 : : "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
13070 : : "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
13071 : : "WHERE ct.oid = $1 "
13072 : : "ORDER BY a.attnum");
13073 : :
13074 : 58 : ExecuteSqlStatement(fout, query->data);
13075 : :
13076 : 58 : fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE] = true;
13077 : : }
13078 : :
13079 : 177 : printfPQExpBuffer(query,
13080 : : "EXECUTE dumpCompositeType('%u')",
13081 : 177 : tyinfo->dobj.catId.oid);
13082 : :
13083 : 177 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
13084 : :
13085 : 177 : ntups = PQntuples(res);
13086 : :
13087 : 177 : i_attname = PQfnumber(res, "attname");
13088 : 177 : i_atttypdefn = PQfnumber(res, "atttypdefn");
13089 : 177 : i_attlen = PQfnumber(res, "attlen");
13090 : 177 : i_attalign = PQfnumber(res, "attalign");
13091 : 177 : i_attisdropped = PQfnumber(res, "attisdropped");
13092 : 177 : i_attcollation = PQfnumber(res, "attcollation");
13093 : 177 : i_attnum = PQfnumber(res, "attnum");
13094 : 177 : i_attversion = PQfnumber(res, "attversion");
13095 : :
13096 [ + + ]: 177 : if (dopt->binary_upgrade)
13097 : : {
13098 : : /*
13099 : : * Collect the attribute versions in a pass of their own, so that they
13100 : : * can be emitted before the CREATE TYPE, like in dumpTableSchema().
13101 : : */
13102 : 26 : attversions = pg_malloc0_array(uint64, ntups);
13103 [ + + ]: 86 : for (i = 0; i < ntups; i++)
13104 : : {
13105 : 60 : int attnum = atoi(PQgetvalue(res, i, i_attnum));
13106 : :
13107 [ + - + - : 120 : if (attnum >= 1 && attnum <= ntups &&
+ - ]
13108 : 60 : !PQgetisnull(res, i, i_attversion))
13109 : 60 : attversions[attnum - 1] = strtou64(PQgetvalue(res, i, i_attversion), NULL, 10);
13110 : : }
13111 : :
13112 : 26 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
13113 : 26 : tyinfo->dobj.catId.oid,
13114 : : false, false);
13115 : 26 : binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid);
13116 : 26 : binary_upgrade_set_next_attribute_versions(q, ntups, attversions);
13117 : : }
13118 : :
13119 : 177 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
13120 : 177 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
13121 : :
13122 : 177 : appendPQExpBuffer(q, "CREATE TYPE %s AS (",
13123 : : qualtypname);
13124 : :
13125 : 177 : actual_atts = 0;
13126 [ + + ]: 587 : for (i = 0; i < ntups; i++)
13127 : : {
13128 : : char *attname;
13129 : : char *atttypdefn;
13130 : : char *attlen;
13131 : : char *attalign;
13132 : : bool attisdropped;
13133 : : Oid attcollation;
13134 : :
13135 : 410 : attname = PQgetvalue(res, i, i_attname);
13136 : 410 : atttypdefn = PQgetvalue(res, i, i_atttypdefn);
13137 : 410 : attlen = PQgetvalue(res, i, i_attlen);
13138 : 410 : attalign = PQgetvalue(res, i, i_attalign);
13139 : 410 : attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
13140 : 410 : attcollation = atooid(PQgetvalue(res, i, i_attcollation));
13141 : :
13142 [ + + + + ]: 410 : if (attisdropped && !dopt->binary_upgrade)
13143 : 41 : continue;
13144 : :
13145 : : /* Format properly if not first attr */
13146 [ + + ]: 369 : if (actual_atts++ > 0)
13147 : 193 : appendPQExpBufferChar(q, ',');
13148 : 369 : appendPQExpBufferStr(q, "\n\t");
13149 : :
13150 [ + + ]: 369 : if (!attisdropped)
13151 : : {
13152 : 365 : appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
13153 : :
13154 : : /* Add collation if not default for the column type */
13155 [ - + ]: 365 : if (OidIsValid(attcollation))
13156 : : {
13157 : : CollInfo *coll;
13158 : :
13159 : 0 : coll = findCollationByOid(attcollation);
13160 [ # # ]: 0 : if (coll)
13161 : 0 : appendPQExpBuffer(q, " COLLATE %s",
13162 : 0 : fmtQualifiedDumpable(coll));
13163 : : }
13164 : : }
13165 : : else
13166 : : {
13167 : : /*
13168 : : * This is a dropped attribute and we're in binary_upgrade mode.
13169 : : * Insert a placeholder for it in the CREATE TYPE command, and set
13170 : : * length and alignment with direct UPDATE to the catalogs
13171 : : * afterwards. See similar code in dumpTableSchema().
13172 : : */
13173 : 4 : appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
13174 : :
13175 : : /* stash separately for insertion after the CREATE TYPE */
13176 : 4 : appendPQExpBufferStr(dropped,
13177 : : "\n-- For binary upgrade, recreate dropped column.\n");
13178 : 4 : appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
13179 : : "SET attlen = %s, "
13180 : : "attalign = '%s', attbyval = false\n"
13181 : : "WHERE attname = ", attlen, attalign);
13182 : 4 : appendStringLiteralAH(dropped, attname, fout);
13183 : 4 : appendPQExpBufferStr(dropped, "\n AND attrelid = ");
13184 : 4 : appendStringLiteralAH(dropped, qualtypname, fout);
13185 : 4 : appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
13186 : :
13187 : 4 : appendPQExpBuffer(dropped, "ALTER TYPE %s ",
13188 : : qualtypname);
13189 : 4 : appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
13190 : : fmtId(attname));
13191 : : }
13192 : : }
13193 : 177 : appendPQExpBufferStr(q, "\n);\n");
13194 : 177 : appendPQExpBufferStr(q, dropped->data);
13195 : :
13196 : 177 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
13197 : :
13198 [ + + ]: 177 : if (dopt->binary_upgrade)
13199 : 26 : binary_upgrade_extension_member(q, &tyinfo->dobj,
13200 : : "TYPE", qtypname,
13201 : 26 : tyinfo->dobj.namespace->dobj.name);
13202 : :
13203 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13204 : 160 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
13205 : 160 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
13206 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13207 : : .owner = tyinfo->rolname,
13208 : : .description = "TYPE",
13209 : : .section = SECTION_PRE_DATA,
13210 : : .createStmt = q->data,
13211 : : .dropStmt = delq->data));
13212 : :
13213 : :
13214 : : /* Dump Type Comments and Security Labels */
13215 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13216 : 34 : dumpComment(fout, "TYPE", qtypname,
13217 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13218 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13219 : :
13220 [ - + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13221 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
13222 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13223 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13224 : :
13225 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
13226 : 18 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
13227 : : qtypname, NULL,
13228 : 18 : tyinfo->dobj.namespace->dobj.name,
13229 : 18 : NULL, tyinfo->rolname, &tyinfo->dacl);
13230 : :
13231 : : /* Dump any per-column comments */
13232 [ + + ]: 177 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13233 : 34 : dumpCompositeTypeColComments(fout, tyinfo, res);
13234 : :
13235 : 177 : PQclear(res);
13236 : 177 : destroyPQExpBuffer(q);
13237 : 177 : destroyPQExpBuffer(dropped);
13238 [ + + ]: 177 : if (attversions != NULL)
13239 : 26 : pg_free(attversions);
13240 : 177 : destroyPQExpBuffer(delq);
13241 : 177 : destroyPQExpBuffer(query);
13242 : 177 : pg_free(qtypname);
13243 : 177 : pg_free(qualtypname);
13244 : 177 : }
13245 : :
13246 : : /*
13247 : : * dumpCompositeTypeColComments
13248 : : * writes out to fout the queries to recreate comments on the columns of
13249 : : * a user-defined stand-alone composite type.
13250 : : *
13251 : : * The caller has already made a query to collect the names and attnums
13252 : : * of the type's columns, so we just pass that result into here rather
13253 : : * than reading them again.
13254 : : */
13255 : : static void
13256 : 34 : dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
13257 : : PGresult *res)
13258 : : {
13259 : : CommentItem *comments;
13260 : : int ncomments;
13261 : : PQExpBuffer query;
13262 : : PQExpBuffer target;
13263 : : int i;
13264 : : int ntups;
13265 : : int i_attname;
13266 : : int i_attnum;
13267 : : int i_attisdropped;
13268 : :
13269 : : /* do nothing, if --no-comments is supplied */
13270 [ - + ]: 34 : if (fout->dopt->no_comments)
13271 : 0 : return;
13272 : :
13273 : : /* Search for comments associated with type's pg_class OID */
13274 : 34 : ncomments = findComments(RelationRelationId, tyinfo->typrelid,
13275 : : &comments);
13276 : :
13277 : : /* If no comments exist, we're done */
13278 [ - + ]: 34 : if (ncomments <= 0)
13279 : 0 : return;
13280 : :
13281 : : /* Build COMMENT ON statements */
13282 : 34 : query = createPQExpBuffer();
13283 : 34 : target = createPQExpBuffer();
13284 : :
13285 : 34 : ntups = PQntuples(res);
13286 : 34 : i_attnum = PQfnumber(res, "attnum");
13287 : 34 : i_attname = PQfnumber(res, "attname");
13288 : 34 : i_attisdropped = PQfnumber(res, "attisdropped");
13289 [ + + ]: 68 : while (ncomments > 0)
13290 : : {
13291 : : const char *attname;
13292 : :
13293 : 34 : attname = NULL;
13294 [ + - ]: 34 : for (i = 0; i < ntups; i++)
13295 : : {
13296 [ + - ]: 34 : if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
13297 [ + - ]: 34 : PQgetvalue(res, i, i_attisdropped)[0] != 't')
13298 : : {
13299 : 34 : attname = PQgetvalue(res, i, i_attname);
13300 : 34 : break;
13301 : : }
13302 : : }
13303 [ + - ]: 34 : if (attname) /* just in case we don't find it */
13304 : : {
13305 : 34 : const char *descr = comments->descr;
13306 : :
13307 : 34 : resetPQExpBuffer(target);
13308 : 34 : appendPQExpBuffer(target, "COLUMN %s.",
13309 : 34 : fmtId(tyinfo->dobj.name));
13310 : 34 : appendPQExpBufferStr(target, fmtId(attname));
13311 : :
13312 : 34 : resetPQExpBuffer(query);
13313 : 34 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
13314 : 34 : fmtQualifiedDumpable(tyinfo));
13315 : 34 : appendPQExpBuffer(query, "%s IS ", fmtId(attname));
13316 : 34 : appendStringLiteralAH(query, descr, fout);
13317 : 34 : appendPQExpBufferStr(query, ";\n");
13318 : :
13319 : 34 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
13320 : 34 : ARCHIVE_OPTS(.tag = target->data,
13321 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13322 : : .owner = tyinfo->rolname,
13323 : : .description = "COMMENT",
13324 : : .section = SECTION_NONE,
13325 : : .createStmt = query->data,
13326 : : .deps = &(tyinfo->dobj.dumpId),
13327 : : .nDeps = 1));
13328 : : }
13329 : :
13330 : 34 : comments++;
13331 : 34 : ncomments--;
13332 : : }
13333 : :
13334 : 34 : destroyPQExpBuffer(query);
13335 : 34 : destroyPQExpBuffer(target);
13336 : : }
13337 : :
13338 : : /*
13339 : : * dumpShellType
13340 : : * writes out to fout the queries to create a shell type
13341 : : *
13342 : : * We dump a shell definition in advance of the I/O functions for the type.
13343 : : */
13344 : : static void
13345 : 76 : dumpShellType(Archive *fout, const ShellTypeInfo *stinfo)
13346 : : {
13347 : 76 : DumpOptions *dopt = fout->dopt;
13348 : : PQExpBuffer q;
13349 : :
13350 : : /* Do nothing if not dumping schema */
13351 [ + + ]: 76 : if (!dopt->dumpSchema)
13352 : 7 : return;
13353 : :
13354 : 69 : q = createPQExpBuffer();
13355 : :
13356 : : /*
13357 : : * Note the lack of a DROP command for the shell type; any required DROP
13358 : : * is driven off the base type entry, instead. This interacts with
13359 : : * _printTocEntry()'s use of the presence of a DROP command to decide
13360 : : * whether an entry needs an ALTER OWNER command. We don't want to alter
13361 : : * the shell type's owner immediately on creation; that should happen only
13362 : : * after it's filled in, otherwise the backend complains.
13363 : : */
13364 : :
13365 [ + + ]: 69 : if (dopt->binary_upgrade)
13366 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
13367 : 8 : stinfo->baseType->dobj.catId.oid,
13368 : : false, false);
13369 : :
13370 : 69 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
13371 : 69 : fmtQualifiedDumpable(stinfo));
13372 : :
13373 [ + - ]: 69 : if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13374 : 69 : ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId,
13375 : 69 : ARCHIVE_OPTS(.tag = stinfo->dobj.name,
13376 : : .namespace = stinfo->dobj.namespace->dobj.name,
13377 : : .owner = stinfo->baseType->rolname,
13378 : : .description = "SHELL TYPE",
13379 : : .section = SECTION_PRE_DATA,
13380 : : .createStmt = q->data));
13381 : :
13382 : 69 : destroyPQExpBuffer(q);
13383 : : }
13384 : :
13385 : : /*
13386 : : * dumpProcLang
13387 : : * writes out to fout the queries to recreate a user-defined
13388 : : * procedural language
13389 : : */
13390 : : static void
13391 : 100 : dumpProcLang(Archive *fout, const ProcLangInfo *plang)
13392 : : {
13393 : 100 : DumpOptions *dopt = fout->dopt;
13394 : : PQExpBuffer defqry;
13395 : : PQExpBuffer delqry;
13396 : : bool useParams;
13397 : : char *qlanname;
13398 : : FuncInfo *funcInfo;
13399 : 100 : FuncInfo *inlineInfo = NULL;
13400 : 100 : FuncInfo *validatorInfo = NULL;
13401 : :
13402 : : /* Do nothing if not dumping schema */
13403 [ + + ]: 100 : if (!dopt->dumpSchema)
13404 : 14 : return;
13405 : :
13406 : : /*
13407 : : * Try to find the support function(s). It is not an error if we don't
13408 : : * find them --- if the functions are in the pg_catalog schema, as is
13409 : : * standard in 8.1 and up, then we won't have loaded them. (In this case
13410 : : * we will emit a parameterless CREATE LANGUAGE command, which will
13411 : : * require PL template knowledge in the backend to reload.)
13412 : : */
13413 : :
13414 : 86 : funcInfo = findFuncByOid(plang->lanplcallfoid);
13415 [ + + + + ]: 86 : if (funcInfo != NULL && !funcInfo->dobj.dump)
13416 : 2 : funcInfo = NULL; /* treat not-dumped same as not-found */
13417 : :
13418 [ + + ]: 86 : if (OidIsValid(plang->laninline))
13419 : : {
13420 : 53 : inlineInfo = findFuncByOid(plang->laninline);
13421 [ + + + - ]: 53 : if (inlineInfo != NULL && !inlineInfo->dobj.dump)
13422 : 1 : inlineInfo = NULL;
13423 : : }
13424 : :
13425 [ + + ]: 86 : if (OidIsValid(plang->lanvalidator))
13426 : : {
13427 : 53 : validatorInfo = findFuncByOid(plang->lanvalidator);
13428 [ + + + - ]: 53 : if (validatorInfo != NULL && !validatorInfo->dobj.dump)
13429 : 1 : validatorInfo = NULL;
13430 : : }
13431 : :
13432 : : /*
13433 : : * If the functions are dumpable then emit a complete CREATE LANGUAGE with
13434 : : * parameters. Otherwise, we'll write a parameterless command, which will
13435 : : * be interpreted as CREATE EXTENSION.
13436 : : */
13437 [ + - ]: 32 : useParams = (funcInfo != NULL &&
13438 [ + + + - : 150 : (inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
+ - ]
13439 [ + - ]: 32 : (validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
13440 : :
13441 : 86 : defqry = createPQExpBuffer();
13442 : 86 : delqry = createPQExpBuffer();
13443 : :
13444 : 86 : qlanname = pg_strdup(fmtId(plang->dobj.name));
13445 : :
13446 : 86 : appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
13447 : : qlanname);
13448 : :
13449 [ + + ]: 86 : if (useParams)
13450 : : {
13451 : 32 : appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
13452 [ - + ]: 32 : plang->lanpltrusted ? "TRUSTED " : "",
13453 : : qlanname);
13454 : 32 : appendPQExpBuffer(defqry, " HANDLER %s",
13455 : 32 : fmtQualifiedDumpable(funcInfo));
13456 [ - + ]: 32 : if (OidIsValid(plang->laninline))
13457 : 0 : appendPQExpBuffer(defqry, " INLINE %s",
13458 : 0 : fmtQualifiedDumpable(inlineInfo));
13459 [ - + ]: 32 : if (OidIsValid(plang->lanvalidator))
13460 : 0 : appendPQExpBuffer(defqry, " VALIDATOR %s",
13461 : 0 : fmtQualifiedDumpable(validatorInfo));
13462 : : }
13463 : : else
13464 : : {
13465 : : /*
13466 : : * If not dumping parameters, then use CREATE OR REPLACE so that the
13467 : : * command will not fail if the language is preinstalled in the target
13468 : : * database.
13469 : : *
13470 : : * Modern servers will interpret this as CREATE EXTENSION IF NOT
13471 : : * EXISTS; perhaps we should emit that instead? But it might just add
13472 : : * confusion.
13473 : : */
13474 : 54 : appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s",
13475 : : qlanname);
13476 : : }
13477 : 86 : appendPQExpBufferStr(defqry, ";\n");
13478 : :
13479 [ + + ]: 86 : if (dopt->binary_upgrade)
13480 : 2 : binary_upgrade_extension_member(defqry, &plang->dobj,
13481 : : "LANGUAGE", qlanname, NULL);
13482 : :
13483 [ + + ]: 86 : if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION)
13484 : 33 : ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
13485 : 33 : ARCHIVE_OPTS(.tag = plang->dobj.name,
13486 : : .owner = plang->lanowner,
13487 : : .description = "PROCEDURAL LANGUAGE",
13488 : : .section = SECTION_PRE_DATA,
13489 : : .createStmt = defqry->data,
13490 : : .dropStmt = delqry->data,
13491 : : ));
13492 : :
13493 : : /* Dump Proc Lang Comments and Security Labels */
13494 [ - + ]: 86 : if (plang->dobj.dump & DUMP_COMPONENT_COMMENT)
13495 : 0 : dumpComment(fout, "LANGUAGE", qlanname,
13496 : 0 : NULL, plang->lanowner,
13497 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13498 : :
13499 [ - + ]: 86 : if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL)
13500 : 0 : dumpSecLabel(fout, "LANGUAGE", qlanname,
13501 : 0 : NULL, plang->lanowner,
13502 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13503 : :
13504 [ + + + - ]: 86 : if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL)
13505 : 53 : dumpACL(fout, plang->dobj.dumpId, InvalidDumpId, "LANGUAGE",
13506 : : qlanname, NULL, NULL,
13507 : 53 : NULL, plang->lanowner, &plang->dacl);
13508 : :
13509 : 86 : pg_free(qlanname);
13510 : :
13511 : 86 : destroyPQExpBuffer(defqry);
13512 : 86 : destroyPQExpBuffer(delqry);
13513 : : }
13514 : :
13515 : : /*
13516 : : * format_function_arguments: generate function name and argument list
13517 : : *
13518 : : * This is used when we can rely on pg_get_function_arguments to format
13519 : : * the argument list. Note, however, that pg_get_function_arguments
13520 : : * does not special-case zero-argument aggregates.
13521 : : */
13522 : : static char *
13523 : 4218 : format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
13524 : : {
13525 : : PQExpBufferData fn;
13526 : :
13527 : 4218 : initPQExpBuffer(&fn);
13528 : 4218 : appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name));
13529 [ + + + + ]: 4218 : if (is_agg && finfo->nargs == 0)
13530 : 80 : appendPQExpBufferStr(&fn, "(*)");
13531 : : else
13532 : 4138 : appendPQExpBuffer(&fn, "(%s)", funcargs);
13533 : 4218 : return fn.data;
13534 : : }
13535 : :
13536 : : /*
13537 : : * format_function_signature: generate function name and argument list
13538 : : *
13539 : : * Only a minimal list of input argument types is generated; this is
13540 : : * sufficient to reference the function, but not to define it.
13541 : : *
13542 : : * If honor_quotes is false then the function name is never quoted.
13543 : : * This is appropriate for use in TOC tags, but not in SQL commands.
13544 : : */
13545 : : static char *
13546 : 2223 : format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
13547 : : {
13548 : : PQExpBufferData fn;
13549 : : int j;
13550 : :
13551 : 2223 : initPQExpBuffer(&fn);
13552 [ + + ]: 2223 : if (honor_quotes)
13553 : 401 : appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
13554 : : else
13555 : 1822 : appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
13556 [ + + ]: 4085 : for (j = 0; j < finfo->nargs; j++)
13557 : : {
13558 [ + + ]: 1862 : if (j > 0)
13559 : 437 : appendPQExpBufferStr(&fn, ", ");
13560 : :
13561 : 1862 : appendPQExpBufferStr(&fn,
13562 : 1862 : getFormattedTypeName(fout, finfo->argtypes[j],
13563 : : zeroIsError));
13564 : : }
13565 : 2223 : appendPQExpBufferChar(&fn, ')');
13566 : 2223 : return fn.data;
13567 : : }
13568 : :
13569 : :
13570 : : /*
13571 : : * dumpFunc:
13572 : : * dump out one function
13573 : : */
13574 : : static void
13575 : 1892 : dumpFunc(Archive *fout, const FuncInfo *finfo)
13576 : : {
13577 : 1892 : DumpOptions *dopt = fout->dopt;
13578 : : PQExpBuffer query;
13579 : : PQExpBuffer q;
13580 : : PQExpBuffer delqry;
13581 : : PQExpBuffer asPart;
13582 : : PGresult *res;
13583 : : char *funcsig; /* identity signature */
13584 : 1892 : char *funcfullsig = NULL; /* full signature */
13585 : : char *funcsig_tag;
13586 : : char *qual_funcsig;
13587 : : char *proretset;
13588 : : char *prosrc;
13589 : : char *probin;
13590 : : char *prosqlbody;
13591 : : char *funcargs;
13592 : : char *funciargs;
13593 : : char *funcresult;
13594 : : char *protrftypes;
13595 : : char *prokind;
13596 : : char *provolatile;
13597 : : char *proisstrict;
13598 : : char *prosecdef;
13599 : : char *proleakproof;
13600 : : char *proconfig;
13601 : : char *procost;
13602 : : char *prorows;
13603 : : char *prosupport;
13604 : : char *proparallel;
13605 : : char *lanname;
13606 : 1892 : char **configitems = NULL;
13607 : 1892 : int nconfigitems = 0;
13608 : : const char *keyword;
13609 : :
13610 : : /* Do nothing if not dumping schema */
13611 [ + + ]: 1892 : if (!dopt->dumpSchema)
13612 : 70 : return;
13613 : :
13614 : 1822 : query = createPQExpBuffer();
13615 : 1822 : q = createPQExpBuffer();
13616 : 1822 : delqry = createPQExpBuffer();
13617 : 1822 : asPart = createPQExpBuffer();
13618 : :
13619 [ + + ]: 1822 : if (!fout->is_prepared[PREPQUERY_DUMPFUNC])
13620 : : {
13621 : : /* Set up query for function-specific details */
13622 : 69 : appendPQExpBufferStr(query,
13623 : : "PREPARE dumpFunc(pg_catalog.oid) AS\n");
13624 : :
13625 : 69 : appendPQExpBufferStr(query,
13626 : : "SELECT\n"
13627 : : "proretset,\n"
13628 : : "prosrc,\n"
13629 : : "probin,\n"
13630 : : "provolatile,\n"
13631 : : "proisstrict,\n"
13632 : : "prosecdef,\n"
13633 : : "lanname,\n"
13634 : : "proconfig,\n"
13635 : : "procost,\n"
13636 : : "prorows,\n"
13637 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13638 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n"
13639 : : "pg_catalog.pg_get_function_result(p.oid) AS funcresult,\n"
13640 : : "proleakproof,\n");
13641 : :
13642 : 69 : appendPQExpBufferStr(query,
13643 : : "array_to_string(protrftypes, ' ') AS protrftypes,\n");
13644 : :
13645 : 69 : appendPQExpBufferStr(query,
13646 : : "proparallel,\n");
13647 : :
13648 [ + - ]: 69 : if (fout->remoteVersion >= 110000)
13649 : 69 : appendPQExpBufferStr(query,
13650 : : "prokind,\n");
13651 : : else
13652 : 0 : appendPQExpBufferStr(query,
13653 : : "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind,\n");
13654 : :
13655 [ + - ]: 69 : if (fout->remoteVersion >= 120000)
13656 : 69 : appendPQExpBufferStr(query,
13657 : : "prosupport,\n");
13658 : : else
13659 : 0 : appendPQExpBufferStr(query,
13660 : : "'-' AS prosupport,\n");
13661 : :
13662 [ + - ]: 69 : if (fout->remoteVersion >= 140000)
13663 : 69 : appendPQExpBufferStr(query,
13664 : : "pg_get_function_sqlbody(p.oid) AS prosqlbody\n");
13665 : : else
13666 : 0 : appendPQExpBufferStr(query,
13667 : : "NULL AS prosqlbody\n");
13668 : :
13669 : 69 : appendPQExpBufferStr(query,
13670 : : "FROM pg_catalog.pg_proc p, pg_catalog.pg_language l\n"
13671 : : "WHERE p.oid = $1 "
13672 : : "AND l.oid = p.prolang");
13673 : :
13674 : 69 : ExecuteSqlStatement(fout, query->data);
13675 : :
13676 : 69 : fout->is_prepared[PREPQUERY_DUMPFUNC] = true;
13677 : : }
13678 : :
13679 : 1822 : printfPQExpBuffer(query,
13680 : : "EXECUTE dumpFunc('%u')",
13681 : 1822 : finfo->dobj.catId.oid);
13682 : :
13683 : 1822 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
13684 : :
13685 : 1822 : proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
13686 [ + + ]: 1822 : if (PQgetisnull(res, 0, PQfnumber(res, "prosqlbody")))
13687 : : {
13688 : 1772 : prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
13689 : 1772 : probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
13690 : 1772 : prosqlbody = NULL;
13691 : : }
13692 : : else
13693 : : {
13694 : 50 : prosrc = NULL;
13695 : 50 : probin = NULL;
13696 : 50 : prosqlbody = PQgetvalue(res, 0, PQfnumber(res, "prosqlbody"));
13697 : : }
13698 : 1822 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13699 : 1822 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13700 : 1822 : funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult"));
13701 : 1822 : protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes"));
13702 : 1822 : prokind = PQgetvalue(res, 0, PQfnumber(res, "prokind"));
13703 : 1822 : provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
13704 : 1822 : proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
13705 : 1822 : prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
13706 : 1822 : proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof"));
13707 : 1822 : proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig"));
13708 : 1822 : procost = PQgetvalue(res, 0, PQfnumber(res, "procost"));
13709 : 1822 : prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows"));
13710 : 1822 : prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport"));
13711 : 1822 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13712 : 1822 : lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
13713 : :
13714 : : /*
13715 : : * See backend/commands/functioncmds.c for details of how the 'AS' clause
13716 : : * is used.
13717 : : */
13718 [ + + ]: 1822 : if (prosqlbody)
13719 : : {
13720 : 50 : appendPQExpBufferStr(asPart, prosqlbody);
13721 : : }
13722 [ + + ]: 1772 : else if (probin[0] != '\0')
13723 : : {
13724 : 180 : appendPQExpBufferStr(asPart, "AS ");
13725 : 180 : appendStringLiteralAH(asPart, probin, fout);
13726 [ + - ]: 180 : if (prosrc[0] != '\0')
13727 : : {
13728 : 180 : appendPQExpBufferStr(asPart, ", ");
13729 : :
13730 : : /*
13731 : : * where we have bin, use dollar quoting if allowed and src
13732 : : * contains quote or backslash; else use regular quoting.
13733 : : */
13734 [ + - ]: 180 : if (dopt->disable_dollar_quoting ||
13735 [ + - + - ]: 180 : (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL))
13736 : 180 : appendStringLiteralAH(asPart, prosrc, fout);
13737 : : else
13738 : 0 : appendStringLiteralDQ(asPart, prosrc, NULL);
13739 : : }
13740 : : }
13741 : : else
13742 : : {
13743 : 1592 : appendPQExpBufferStr(asPart, "AS ");
13744 : : /* with no bin, dollar quote src unconditionally if allowed */
13745 [ - + ]: 1592 : if (dopt->disable_dollar_quoting)
13746 : 0 : appendStringLiteralAH(asPart, prosrc, fout);
13747 : : else
13748 : 1592 : appendStringLiteralDQ(asPart, prosrc, NULL);
13749 : : }
13750 : :
13751 [ + + ]: 1822 : if (*proconfig)
13752 : : {
13753 [ - + ]: 15 : if (!parsePGArray(proconfig, &configitems, &nconfigitems))
13754 : 0 : pg_fatal("could not parse %s array", "proconfig");
13755 : : }
13756 : : else
13757 : : {
13758 : 1807 : configitems = NULL;
13759 : 1807 : nconfigitems = 0;
13760 : : }
13761 : :
13762 : 1822 : funcfullsig = format_function_arguments(finfo, funcargs, false);
13763 : 1822 : funcsig = format_function_arguments(finfo, funciargs, false);
13764 : :
13765 : 1822 : funcsig_tag = format_function_signature(fout, finfo, false);
13766 : :
13767 : 1822 : qual_funcsig = psprintf("%s.%s",
13768 : 1822 : fmtId(finfo->dobj.namespace->dobj.name),
13769 : : funcsig);
13770 : :
13771 [ + + ]: 1822 : if (prokind[0] == PROKIND_PROCEDURE)
13772 : 94 : keyword = "PROCEDURE";
13773 : : else
13774 : 1728 : keyword = "FUNCTION"; /* works for window functions too */
13775 : :
13776 : 1822 : appendPQExpBuffer(delqry, "DROP %s %s;\n",
13777 : : keyword, qual_funcsig);
13778 : :
13779 [ + - ]: 3644 : appendPQExpBuffer(q, "CREATE %s %s.%s",
13780 : : keyword,
13781 : 1822 : fmtId(finfo->dobj.namespace->dobj.name),
13782 : : funcfullsig ? funcfullsig :
13783 : : funcsig);
13784 : :
13785 [ + + ]: 1822 : if (prokind[0] == PROKIND_PROCEDURE)
13786 : : /* no result type to output */ ;
13787 [ + - ]: 1728 : else if (funcresult)
13788 : 1728 : appendPQExpBuffer(q, " RETURNS %s", funcresult);
13789 : : else
13790 : 0 : appendPQExpBuffer(q, " RETURNS %s%s",
13791 [ # # ]: 0 : (proretset[0] == 't') ? "SETOF " : "",
13792 : 0 : getFormattedTypeName(fout, finfo->prorettype,
13793 : : zeroIsError));
13794 : :
13795 : 1822 : appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname));
13796 : :
13797 [ + + ]: 1822 : if (*protrftypes)
13798 : : {
13799 : 5 : Oid *typeids = parseOidArray(protrftypes, -1);
13800 : :
13801 : 5 : appendPQExpBufferStr(q, " TRANSFORM ");
13802 [ + + ]: 10 : for (int i = 0; typeids[i]; i++)
13803 : : {
13804 [ - + ]: 5 : if (i != 0)
13805 : 0 : appendPQExpBufferStr(q, ", ");
13806 : 5 : appendPQExpBuffer(q, "FOR TYPE %s",
13807 : 5 : getFormattedTypeName(fout, typeids[i], zeroAsNone));
13808 : : }
13809 : :
13810 : 5 : pg_free(typeids);
13811 : : }
13812 : :
13813 [ + + ]: 1822 : if (prokind[0] == PROKIND_WINDOW)
13814 : 5 : appendPQExpBufferStr(q, " WINDOW");
13815 : :
13816 [ + + ]: 1822 : if (provolatile[0] != PROVOLATILE_VOLATILE)
13817 : : {
13818 [ + + ]: 355 : if (provolatile[0] == PROVOLATILE_IMMUTABLE)
13819 : 334 : appendPQExpBufferStr(q, " IMMUTABLE");
13820 [ + - ]: 21 : else if (provolatile[0] == PROVOLATILE_STABLE)
13821 : 21 : appendPQExpBufferStr(q, " STABLE");
13822 [ # # ]: 0 : else if (provolatile[0] != PROVOLATILE_VOLATILE)
13823 : 0 : pg_fatal("unrecognized provolatile value for function \"%s\"",
13824 : : finfo->dobj.name);
13825 : : }
13826 : :
13827 [ + + ]: 1822 : if (proisstrict[0] == 't')
13828 : 384 : appendPQExpBufferStr(q, " STRICT");
13829 : :
13830 [ - + ]: 1822 : if (prosecdef[0] == 't')
13831 : 0 : appendPQExpBufferStr(q, " SECURITY DEFINER");
13832 : :
13833 [ + + ]: 1822 : if (proleakproof[0] == 't')
13834 : 10 : appendPQExpBufferStr(q, " LEAKPROOF");
13835 : :
13836 : : /*
13837 : : * COST and ROWS are emitted only if present and not default, so as not to
13838 : : * break backwards-compatibility of the dump without need. Keep this code
13839 : : * in sync with the defaults in functioncmds.c.
13840 : : */
13841 [ + - ]: 1822 : if (strcmp(procost, "0") != 0)
13842 : : {
13843 [ + + + + ]: 1822 : if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0)
13844 : : {
13845 : : /* default cost is 1 */
13846 [ - + ]: 417 : if (strcmp(procost, "1") != 0)
13847 : 0 : appendPQExpBuffer(q, " COST %s", procost);
13848 : : }
13849 : : else
13850 : : {
13851 : : /* default cost is 100 */
13852 [ + + ]: 1405 : if (strcmp(procost, "100") != 0)
13853 : 6 : appendPQExpBuffer(q, " COST %s", procost);
13854 : : }
13855 : : }
13856 [ + + ]: 1822 : if (proretset[0] == 't' &&
13857 [ + - - + ]: 189 : strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0)
13858 : 0 : appendPQExpBuffer(q, " ROWS %s", prorows);
13859 : :
13860 [ + + ]: 1822 : if (strcmp(prosupport, "-") != 0)
13861 : : {
13862 : : /* We rely on regprocout to provide quoting and qualification */
13863 : 44 : appendPQExpBuffer(q, " SUPPORT %s", prosupport);
13864 : : }
13865 : :
13866 [ + + ]: 1822 : if (proparallel[0] != PROPARALLEL_UNSAFE)
13867 : : {
13868 [ + + ]: 120 : if (proparallel[0] == PROPARALLEL_SAFE)
13869 : 115 : appendPQExpBufferStr(q, " PARALLEL SAFE");
13870 [ + - ]: 5 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13871 : 5 : appendPQExpBufferStr(q, " PARALLEL RESTRICTED");
13872 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
13873 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
13874 : : finfo->dobj.name);
13875 : : }
13876 : :
13877 [ + + ]: 1862 : for (int i = 0; i < nconfigitems; i++)
13878 : : {
13879 : : /* we feel free to scribble on configitems[] here */
13880 : 40 : char *configitem = configitems[i];
13881 : : char *pos;
13882 : :
13883 : 40 : pos = strchr(configitem, '=');
13884 [ - + ]: 40 : if (pos == NULL)
13885 : 0 : continue;
13886 : 40 : *pos++ = '\0';
13887 : 40 : appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem));
13888 : :
13889 : : /*
13890 : : * Variables that are marked GUC_LIST_QUOTE were already fully quoted
13891 : : * by flatten_set_variable_args() before they were put into the
13892 : : * proconfig array. However, because the quoting rules used there
13893 : : * aren't exactly like SQL's, we have to break the list value apart
13894 : : * and then quote the elements as string literals. (The elements may
13895 : : * be double-quoted as-is, but we can't just feed them to the SQL
13896 : : * parser; it would do the wrong thing with elements that are
13897 : : * zero-length or longer than NAMEDATALEN.) Also, we need a special
13898 : : * case for empty lists.
13899 : : *
13900 : : * Variables that are not so marked should just be emitted as simple
13901 : : * string literals. If the variable is not known to
13902 : : * variable_is_guc_list_quote(), we'll do that; this makes it unsafe
13903 : : * to use GUC_LIST_QUOTE for extension variables.
13904 : : */
13905 [ + + ]: 40 : if (variable_is_guc_list_quote(configitem))
13906 : : {
13907 : : char **namelist;
13908 : : char **nameptr;
13909 : :
13910 : : /* Parse string into list of identifiers */
13911 : : /* this shouldn't fail really */
13912 [ + - ]: 15 : if (SplitGUCList(pos, ',', &namelist))
13913 : : {
13914 : : /* Special case: represent an empty list as NULL */
13915 [ + + ]: 15 : if (*namelist == NULL)
13916 : 5 : appendPQExpBufferStr(q, "NULL");
13917 [ + + ]: 40 : for (nameptr = namelist; *nameptr; nameptr++)
13918 : : {
13919 [ + + ]: 25 : if (nameptr != namelist)
13920 : 15 : appendPQExpBufferStr(q, ", ");
13921 : 25 : appendStringLiteralAH(q, *nameptr, fout);
13922 : : }
13923 : : }
13924 : 15 : pg_free(namelist);
13925 : : }
13926 : : else
13927 : 25 : appendStringLiteralAH(q, pos, fout);
13928 : : }
13929 : :
13930 : 1822 : appendPQExpBuffer(q, "\n %s;\n", asPart->data);
13931 : :
13932 : 1822 : append_depends_on_extension(fout, q, &finfo->dobj,
13933 : : "pg_catalog.pg_proc", keyword,
13934 : : qual_funcsig);
13935 : :
13936 [ + + ]: 1822 : if (dopt->binary_upgrade)
13937 : 303 : binary_upgrade_extension_member(q, &finfo->dobj,
13938 : : keyword, funcsig,
13939 : 303 : finfo->dobj.namespace->dobj.name);
13940 : :
13941 [ + + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13942 : 1722 : ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
13943 [ + + ]: 1722 : ARCHIVE_OPTS(.tag = funcsig_tag,
13944 : : .namespace = finfo->dobj.namespace->dobj.name,
13945 : : .owner = finfo->rolname,
13946 : : .description = keyword,
13947 : : .section = finfo->postponed_def ?
13948 : : SECTION_POST_DATA : SECTION_PRE_DATA,
13949 : : .createStmt = q->data,
13950 : : .dropStmt = delqry->data));
13951 : :
13952 : : /* Dump Function Comments and Security Labels */
13953 [ + + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13954 : 9 : dumpComment(fout, keyword, funcsig,
13955 : 9 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13956 : 9 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13957 : :
13958 [ - + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13959 : 0 : dumpSecLabel(fout, keyword, funcsig,
13960 : 0 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13961 : 0 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13962 : :
13963 [ + + ]: 1822 : if (finfo->dobj.dump & DUMP_COMPONENT_ACL)
13964 : 104 : dumpACL(fout, finfo->dobj.dumpId, InvalidDumpId, keyword,
13965 : : funcsig, NULL,
13966 : 104 : finfo->dobj.namespace->dobj.name,
13967 : 104 : NULL, finfo->rolname, &finfo->dacl);
13968 : :
13969 : 1822 : PQclear(res);
13970 : :
13971 : 1822 : destroyPQExpBuffer(query);
13972 : 1822 : destroyPQExpBuffer(q);
13973 : 1822 : destroyPQExpBuffer(delqry);
13974 : 1822 : destroyPQExpBuffer(asPart);
13975 : 1822 : free(funcsig);
13976 : 1822 : free(funcfullsig);
13977 : 1822 : free(funcsig_tag);
13978 : 1822 : pfree(qual_funcsig);
13979 : 1822 : free(configitems);
13980 : : }
13981 : :
13982 : :
13983 : : /*
13984 : : * Dump a user-defined cast
13985 : : */
13986 : : static void
13987 : 69 : dumpCast(Archive *fout, const CastInfo *cast)
13988 : : {
13989 : 69 : DumpOptions *dopt = fout->dopt;
13990 : : PQExpBuffer defqry;
13991 : : PQExpBuffer delqry;
13992 : : PQExpBuffer labelq;
13993 : : PQExpBuffer castargs;
13994 : 69 : FuncInfo *funcInfo = NULL;
13995 : : const char *sourceType;
13996 : : const char *targetType;
13997 : :
13998 : : /* Do nothing if not dumping schema */
13999 [ + + ]: 69 : if (!dopt->dumpSchema)
14000 : 6 : return;
14001 : :
14002 : : /* Cannot dump if we don't have the cast function's info */
14003 [ + + ]: 63 : if (OidIsValid(cast->castfunc))
14004 : : {
14005 : 38 : funcInfo = findFuncByOid(cast->castfunc);
14006 [ - + ]: 38 : if (funcInfo == NULL)
14007 : 0 : pg_fatal("could not find function definition for function with OID %u",
14008 : : cast->castfunc);
14009 : : }
14010 : :
14011 : 63 : defqry = createPQExpBuffer();
14012 : 63 : delqry = createPQExpBuffer();
14013 : 63 : labelq = createPQExpBuffer();
14014 : 63 : castargs = createPQExpBuffer();
14015 : :
14016 : 63 : sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
14017 : 63 : targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
14018 : 63 : appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
14019 : : sourceType, targetType);
14020 : :
14021 : 63 : appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
14022 : : sourceType, targetType);
14023 : :
14024 [ + - + - ]: 63 : switch (cast->castmethod)
14025 : : {
14026 : 25 : case COERCION_METHOD_BINARY:
14027 : 25 : appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
14028 : 25 : break;
14029 : 0 : case COERCION_METHOD_INOUT:
14030 : 0 : appendPQExpBufferStr(defqry, "WITH INOUT");
14031 : 0 : break;
14032 : 38 : case COERCION_METHOD_FUNCTION:
14033 [ + - ]: 38 : if (funcInfo)
14034 : : {
14035 : 38 : char *fsig = format_function_signature(fout, funcInfo, true);
14036 : :
14037 : : /*
14038 : : * Always qualify the function name (format_function_signature
14039 : : * won't qualify it).
14040 : : */
14041 : 38 : appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
14042 : 38 : fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
14043 : 38 : free(fsig);
14044 : : }
14045 : : else
14046 : 0 : pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
14047 : 38 : break;
14048 : 0 : default:
14049 : 0 : pg_log_warning("bogus value in pg_cast.castmethod field");
14050 : : }
14051 : :
14052 [ + + ]: 63 : if (cast->castcontext == 'a')
14053 : 33 : appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
14054 [ + + ]: 30 : else if (cast->castcontext == 'i')
14055 : 10 : appendPQExpBufferStr(defqry, " AS IMPLICIT");
14056 : 63 : appendPQExpBufferStr(defqry, ";\n");
14057 : :
14058 : 63 : appendPQExpBuffer(labelq, "CAST (%s AS %s)",
14059 : : sourceType, targetType);
14060 : :
14061 : 63 : appendPQExpBuffer(castargs, "(%s AS %s)",
14062 : : sourceType, targetType);
14063 : :
14064 [ + + ]: 63 : if (dopt->binary_upgrade)
14065 : 7 : binary_upgrade_extension_member(defqry, &cast->dobj,
14066 : 7 : "CAST", castargs->data, NULL);
14067 : :
14068 [ + - ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
14069 : 63 : ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
14070 : 63 : ARCHIVE_OPTS(.tag = labelq->data,
14071 : : .description = "CAST",
14072 : : .section = SECTION_PRE_DATA,
14073 : : .createStmt = defqry->data,
14074 : : .dropStmt = delqry->data));
14075 : :
14076 : : /* Dump Cast Comments */
14077 [ - + ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
14078 : 0 : dumpComment(fout, "CAST", castargs->data,
14079 : : NULL, "",
14080 : 0 : cast->dobj.catId, 0, cast->dobj.dumpId);
14081 : :
14082 : 63 : destroyPQExpBuffer(defqry);
14083 : 63 : destroyPQExpBuffer(delqry);
14084 : 63 : destroyPQExpBuffer(labelq);
14085 : 63 : destroyPQExpBuffer(castargs);
14086 : : }
14087 : :
14088 : : /*
14089 : : * Dump a transform
14090 : : */
14091 : : static void
14092 : 44 : dumpTransform(Archive *fout, const TransformInfo *transform)
14093 : : {
14094 : 44 : DumpOptions *dopt = fout->dopt;
14095 : : PQExpBuffer defqry;
14096 : : PQExpBuffer delqry;
14097 : : PQExpBuffer labelq;
14098 : : PQExpBuffer transformargs;
14099 : 44 : FuncInfo *fromsqlFuncInfo = NULL;
14100 : 44 : FuncInfo *tosqlFuncInfo = NULL;
14101 : : char *lanname;
14102 : : const char *transformType;
14103 : :
14104 : : /* Do nothing if not dumping schema */
14105 [ + + ]: 44 : if (!dopt->dumpSchema)
14106 : 6 : return;
14107 : :
14108 : : /* Cannot dump if we don't have the transform functions' info */
14109 [ + - ]: 38 : if (OidIsValid(transform->trffromsql))
14110 : : {
14111 : 38 : fromsqlFuncInfo = findFuncByOid(transform->trffromsql);
14112 [ - + ]: 38 : if (fromsqlFuncInfo == NULL)
14113 : 0 : pg_fatal("could not find function definition for function with OID %u",
14114 : : transform->trffromsql);
14115 : : }
14116 [ + - ]: 38 : if (OidIsValid(transform->trftosql))
14117 : : {
14118 : 38 : tosqlFuncInfo = findFuncByOid(transform->trftosql);
14119 [ - + ]: 38 : if (tosqlFuncInfo == NULL)
14120 : 0 : pg_fatal("could not find function definition for function with OID %u",
14121 : : transform->trftosql);
14122 : : }
14123 : :
14124 : 38 : defqry = createPQExpBuffer();
14125 : 38 : delqry = createPQExpBuffer();
14126 : 38 : labelq = createPQExpBuffer();
14127 : 38 : transformargs = createPQExpBuffer();
14128 : :
14129 : 38 : lanname = get_language_name(fout, transform->trflang);
14130 : 38 : transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone);
14131 : :
14132 : 38 : appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n",
14133 : : transformType, lanname);
14134 : :
14135 : 38 : appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (",
14136 : : transformType, lanname);
14137 : :
14138 [ - + - - ]: 38 : if (!transform->trffromsql && !transform->trftosql)
14139 : 0 : pg_log_warning("bogus transform definition, at least one of trffromsql and trftosql should be nonzero");
14140 : :
14141 [ + - ]: 38 : if (transform->trffromsql)
14142 : : {
14143 [ + - ]: 38 : if (fromsqlFuncInfo)
14144 : : {
14145 : 38 : char *fsig = format_function_signature(fout, fromsqlFuncInfo, true);
14146 : :
14147 : : /*
14148 : : * Always qualify the function name (format_function_signature
14149 : : * won't qualify it).
14150 : : */
14151 : 38 : appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s",
14152 : 38 : fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig);
14153 : 38 : free(fsig);
14154 : : }
14155 : : else
14156 : 0 : pg_log_warning("bogus value in pg_transform.trffromsql field");
14157 : : }
14158 : :
14159 [ + - ]: 38 : if (transform->trftosql)
14160 : : {
14161 [ + - ]: 38 : if (transform->trffromsql)
14162 : 38 : appendPQExpBufferStr(defqry, ", ");
14163 : :
14164 [ + - ]: 38 : if (tosqlFuncInfo)
14165 : : {
14166 : 38 : char *fsig = format_function_signature(fout, tosqlFuncInfo, true);
14167 : :
14168 : : /*
14169 : : * Always qualify the function name (format_function_signature
14170 : : * won't qualify it).
14171 : : */
14172 : 38 : appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s",
14173 : 38 : fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig);
14174 : 38 : free(fsig);
14175 : : }
14176 : : else
14177 : 0 : pg_log_warning("bogus value in pg_transform.trftosql field");
14178 : : }
14179 : :
14180 : 38 : appendPQExpBufferStr(defqry, ");\n");
14181 : :
14182 : 38 : appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s",
14183 : : transformType, lanname);
14184 : :
14185 : 38 : appendPQExpBuffer(transformargs, "FOR %s LANGUAGE %s",
14186 : : transformType, lanname);
14187 : :
14188 [ + + ]: 38 : if (dopt->binary_upgrade)
14189 : 2 : binary_upgrade_extension_member(defqry, &transform->dobj,
14190 : 2 : "TRANSFORM", transformargs->data, NULL);
14191 : :
14192 [ + - ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION)
14193 : 38 : ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId,
14194 : 38 : ARCHIVE_OPTS(.tag = labelq->data,
14195 : : .description = "TRANSFORM",
14196 : : .section = SECTION_PRE_DATA,
14197 : : .createStmt = defqry->data,
14198 : : .dropStmt = delqry->data,
14199 : : .deps = transform->dobj.dependencies,
14200 : : .nDeps = transform->dobj.nDeps));
14201 : :
14202 : : /* Dump Transform Comments */
14203 [ - + ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_COMMENT)
14204 : 0 : dumpComment(fout, "TRANSFORM", transformargs->data,
14205 : : NULL, "",
14206 : 0 : transform->dobj.catId, 0, transform->dobj.dumpId);
14207 : :
14208 : 38 : free(lanname);
14209 : 38 : destroyPQExpBuffer(defqry);
14210 : 38 : destroyPQExpBuffer(delqry);
14211 : 38 : destroyPQExpBuffer(labelq);
14212 : 38 : destroyPQExpBuffer(transformargs);
14213 : : }
14214 : :
14215 : :
14216 : : /*
14217 : : * dumpOpr
14218 : : * write out a single operator definition
14219 : : */
14220 : : static void
14221 : 2525 : dumpOpr(Archive *fout, const OprInfo *oprinfo)
14222 : : {
14223 : 2525 : DumpOptions *dopt = fout->dopt;
14224 : : PQExpBuffer query;
14225 : : PQExpBuffer q;
14226 : : PQExpBuffer delq;
14227 : : PQExpBuffer oprid;
14228 : : PQExpBuffer details;
14229 : : PGresult *res;
14230 : : int i_oprkind;
14231 : : int i_oprcode;
14232 : : int i_oprleft;
14233 : : int i_oprright;
14234 : : int i_oprcom;
14235 : : int i_oprnegate;
14236 : : int i_oprrest;
14237 : : int i_oprjoin;
14238 : : int i_oprcanmerge;
14239 : : int i_oprcanhash;
14240 : : char *oprkind;
14241 : : char *oprcode;
14242 : : char *oprleft;
14243 : : char *oprright;
14244 : : char *oprcom;
14245 : : char *oprnegate;
14246 : : char *oprrest;
14247 : : char *oprjoin;
14248 : : char *oprcanmerge;
14249 : : char *oprcanhash;
14250 : : char *oprregproc;
14251 : : char *oprref;
14252 : :
14253 : : /* Do nothing if not dumping schema */
14254 [ + + ]: 2525 : if (!dopt->dumpSchema)
14255 : 7 : return;
14256 : :
14257 : : /*
14258 : : * some operators are invalid because they were the result of user
14259 : : * defining operators before commutators exist
14260 : : */
14261 [ + + ]: 2518 : if (!OidIsValid(oprinfo->oprcode))
14262 : 14 : return;
14263 : :
14264 : 2504 : query = createPQExpBuffer();
14265 : 2504 : q = createPQExpBuffer();
14266 : 2504 : delq = createPQExpBuffer();
14267 : 2504 : oprid = createPQExpBuffer();
14268 : 2504 : details = createPQExpBuffer();
14269 : :
14270 [ + + ]: 2504 : if (!fout->is_prepared[PREPQUERY_DUMPOPR])
14271 : : {
14272 : : /* Set up query for operator-specific details */
14273 : 42 : appendPQExpBufferStr(query,
14274 : : "PREPARE dumpOpr(pg_catalog.oid) AS\n"
14275 : : "SELECT oprkind, "
14276 : : "oprcode::pg_catalog.regprocedure, "
14277 : : "oprleft::pg_catalog.regtype, "
14278 : : "oprright::pg_catalog.regtype, "
14279 : : "oprcom, "
14280 : : "oprnegate, "
14281 : : "oprrest::pg_catalog.regprocedure, "
14282 : : "oprjoin::pg_catalog.regprocedure, "
14283 : : "oprcanmerge, oprcanhash "
14284 : : "FROM pg_catalog.pg_operator "
14285 : : "WHERE oid = $1");
14286 : :
14287 : 42 : ExecuteSqlStatement(fout, query->data);
14288 : :
14289 : 42 : fout->is_prepared[PREPQUERY_DUMPOPR] = true;
14290 : : }
14291 : :
14292 : 2504 : printfPQExpBuffer(query,
14293 : : "EXECUTE dumpOpr('%u')",
14294 : 2504 : oprinfo->dobj.catId.oid);
14295 : :
14296 : 2504 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14297 : :
14298 : 2504 : i_oprkind = PQfnumber(res, "oprkind");
14299 : 2504 : i_oprcode = PQfnumber(res, "oprcode");
14300 : 2504 : i_oprleft = PQfnumber(res, "oprleft");
14301 : 2504 : i_oprright = PQfnumber(res, "oprright");
14302 : 2504 : i_oprcom = PQfnumber(res, "oprcom");
14303 : 2504 : i_oprnegate = PQfnumber(res, "oprnegate");
14304 : 2504 : i_oprrest = PQfnumber(res, "oprrest");
14305 : 2504 : i_oprjoin = PQfnumber(res, "oprjoin");
14306 : 2504 : i_oprcanmerge = PQfnumber(res, "oprcanmerge");
14307 : 2504 : i_oprcanhash = PQfnumber(res, "oprcanhash");
14308 : :
14309 : 2504 : oprkind = PQgetvalue(res, 0, i_oprkind);
14310 : 2504 : oprcode = PQgetvalue(res, 0, i_oprcode);
14311 : 2504 : oprleft = PQgetvalue(res, 0, i_oprleft);
14312 : 2504 : oprright = PQgetvalue(res, 0, i_oprright);
14313 : 2504 : oprcom = PQgetvalue(res, 0, i_oprcom);
14314 : 2504 : oprnegate = PQgetvalue(res, 0, i_oprnegate);
14315 : 2504 : oprrest = PQgetvalue(res, 0, i_oprrest);
14316 : 2504 : oprjoin = PQgetvalue(res, 0, i_oprjoin);
14317 : 2504 : oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
14318 : 2504 : oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
14319 : :
14320 : : /* In PG14 upwards postfix operator support does not exist anymore. */
14321 [ - + ]: 2504 : if (strcmp(oprkind, "r") == 0)
14322 : 0 : pg_log_warning("postfix operators are not supported anymore (operator \"%s\")",
14323 : : oprcode);
14324 : :
14325 : 2504 : oprregproc = convertRegProcReference(oprcode);
14326 [ + - ]: 2504 : if (oprregproc)
14327 : : {
14328 : 2504 : appendPQExpBuffer(details, " FUNCTION = %s", oprregproc);
14329 : 2504 : free(oprregproc);
14330 : : }
14331 : :
14332 : 2504 : appendPQExpBuffer(oprid, "%s (",
14333 : 2504 : oprinfo->dobj.name);
14334 : :
14335 : : /*
14336 : : * right unary means there's a left arg and left unary means there's a
14337 : : * right arg. (Although the "r" case is dead code for PG14 and later,
14338 : : * continue to support it in case we're dumping from an old server.)
14339 : : */
14340 [ + - ]: 2504 : if (strcmp(oprkind, "r") == 0 ||
14341 [ + + ]: 2504 : strcmp(oprkind, "b") == 0)
14342 : : {
14343 : 2361 : appendPQExpBuffer(details, ",\n LEFTARG = %s", oprleft);
14344 : 2361 : appendPQExpBufferStr(oprid, oprleft);
14345 : : }
14346 : : else
14347 : 143 : appendPQExpBufferStr(oprid, "NONE");
14348 : :
14349 [ + + ]: 2504 : if (strcmp(oprkind, "l") == 0 ||
14350 [ + - ]: 2361 : strcmp(oprkind, "b") == 0)
14351 : : {
14352 : 2504 : appendPQExpBuffer(details, ",\n RIGHTARG = %s", oprright);
14353 : 2504 : appendPQExpBuffer(oprid, ", %s)", oprright);
14354 : : }
14355 : : else
14356 : 0 : appendPQExpBufferStr(oprid, ", NONE)");
14357 : :
14358 : 2504 : oprref = getFormattedOperatorName(oprcom);
14359 [ + + ]: 2504 : if (oprref)
14360 : : {
14361 : 1679 : appendPQExpBuffer(details, ",\n COMMUTATOR = %s", oprref);
14362 : 1679 : free(oprref);
14363 : : }
14364 : :
14365 : 2504 : oprref = getFormattedOperatorName(oprnegate);
14366 [ + + ]: 2504 : if (oprref)
14367 : : {
14368 : 1181 : appendPQExpBuffer(details, ",\n NEGATOR = %s", oprref);
14369 : 1181 : free(oprref);
14370 : : }
14371 : :
14372 [ + + ]: 2504 : if (strcmp(oprcanmerge, "t") == 0)
14373 : 188 : appendPQExpBufferStr(details, ",\n MERGES");
14374 : :
14375 [ + + ]: 2504 : if (strcmp(oprcanhash, "t") == 0)
14376 : 141 : appendPQExpBufferStr(details, ",\n HASHES");
14377 : :
14378 : 2504 : oprregproc = convertRegProcReference(oprrest);
14379 [ + + ]: 2504 : if (oprregproc)
14380 : : {
14381 : 1532 : appendPQExpBuffer(details, ",\n RESTRICT = %s", oprregproc);
14382 : 1532 : free(oprregproc);
14383 : : }
14384 : :
14385 : 2504 : oprregproc = convertRegProcReference(oprjoin);
14386 [ + + ]: 2504 : if (oprregproc)
14387 : : {
14388 : 1532 : appendPQExpBuffer(details, ",\n JOIN = %s", oprregproc);
14389 : 1532 : free(oprregproc);
14390 : : }
14391 : :
14392 : 2504 : appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
14393 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14394 : : oprid->data);
14395 : :
14396 : 2504 : appendPQExpBuffer(q, "CREATE OPERATOR %s.%s (\n%s\n);\n",
14397 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14398 : 2504 : oprinfo->dobj.name, details->data);
14399 : :
14400 [ + + ]: 2504 : if (dopt->binary_upgrade)
14401 : 12 : binary_upgrade_extension_member(q, &oprinfo->dobj,
14402 : 12 : "OPERATOR", oprid->data,
14403 : 12 : oprinfo->dobj.namespace->dobj.name);
14404 : :
14405 [ + - ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14406 : 2504 : ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
14407 : 2504 : ARCHIVE_OPTS(.tag = oprinfo->dobj.name,
14408 : : .namespace = oprinfo->dobj.namespace->dobj.name,
14409 : : .owner = oprinfo->rolname,
14410 : : .description = "OPERATOR",
14411 : : .section = SECTION_PRE_DATA,
14412 : : .createStmt = q->data,
14413 : : .dropStmt = delq->data));
14414 : :
14415 : : /* Dump Operator Comments */
14416 [ + + ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14417 : 2415 : dumpComment(fout, "OPERATOR", oprid->data,
14418 : 2415 : oprinfo->dobj.namespace->dobj.name, oprinfo->rolname,
14419 : 2415 : oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
14420 : :
14421 : 2504 : PQclear(res);
14422 : :
14423 : 2504 : destroyPQExpBuffer(query);
14424 : 2504 : destroyPQExpBuffer(q);
14425 : 2504 : destroyPQExpBuffer(delq);
14426 : 2504 : destroyPQExpBuffer(oprid);
14427 : 2504 : destroyPQExpBuffer(details);
14428 : : }
14429 : :
14430 : : /*
14431 : : * Convert a function reference obtained from pg_operator
14432 : : *
14433 : : * Returns allocated string of what to print, or NULL if function references
14434 : : * is InvalidOid. Returned string is expected to be free'd by the caller.
14435 : : *
14436 : : * The input is a REGPROCEDURE display; we have to strip the argument-types
14437 : : * part.
14438 : : */
14439 : : static char *
14440 : 7512 : convertRegProcReference(const char *proc)
14441 : : {
14442 : : char *name;
14443 : : char *paren;
14444 : : bool inquote;
14445 : :
14446 : : /* In all cases "-" means a null reference */
14447 [ + + ]: 7512 : if (strcmp(proc, "-") == 0)
14448 : 1944 : return NULL;
14449 : :
14450 : 5568 : name = pg_strdup(proc);
14451 : : /* find non-double-quoted left paren */
14452 : 5568 : inquote = false;
14453 [ + - ]: 67010 : for (paren = name; *paren; paren++)
14454 : : {
14455 [ + + + - ]: 67010 : if (*paren == '(' && !inquote)
14456 : : {
14457 : 5568 : *paren = '\0';
14458 : 5568 : break;
14459 : : }
14460 [ + + ]: 61442 : if (*paren == '"')
14461 : 50 : inquote = !inquote;
14462 : : }
14463 : 5568 : return name;
14464 : : }
14465 : :
14466 : : /*
14467 : : * getFormattedOperatorName - retrieve the operator name for the
14468 : : * given operator OID (presented in string form).
14469 : : *
14470 : : * Returns an allocated string, or NULL if the given OID is invalid.
14471 : : * Caller is responsible for free'ing result string.
14472 : : *
14473 : : * What we produce has the format "OPERATOR(schema.oprname)". This is only
14474 : : * useful in commands where the operator's argument types can be inferred from
14475 : : * context. We always schema-qualify the name, though. The predecessor to
14476 : : * this code tried to skip the schema qualification if possible, but that led
14477 : : * to wrong results in corner cases, such as if an operator and its negator
14478 : : * are in different schemas.
14479 : : */
14480 : : static char *
14481 : 5295 : getFormattedOperatorName(const char *oproid)
14482 : : {
14483 : : OprInfo *oprInfo;
14484 : :
14485 : : /* In all cases "0" means a null reference */
14486 [ + + ]: 5295 : if (strcmp(oproid, "0") == 0)
14487 : 2435 : return NULL;
14488 : :
14489 : 2860 : oprInfo = findOprByOid(atooid(oproid));
14490 [ - + ]: 2860 : if (oprInfo == NULL)
14491 : : {
14492 : 0 : pg_log_warning("could not find operator with OID %s",
14493 : : oproid);
14494 : 0 : return NULL;
14495 : : }
14496 : :
14497 : 2860 : return psprintf("OPERATOR(%s.%s)",
14498 : 2860 : fmtId(oprInfo->dobj.namespace->dobj.name),
14499 : : oprInfo->dobj.name);
14500 : : }
14501 : :
14502 : : /*
14503 : : * Convert a function OID obtained from pg_ts_parser or pg_ts_template
14504 : : *
14505 : : * It is sufficient to use REGPROC rather than REGPROCEDURE, since the
14506 : : * argument lists of these functions are predetermined. Note that the
14507 : : * caller should ensure we are in the proper schema, because the results
14508 : : * are search path dependent!
14509 : : */
14510 : : static char *
14511 : 215 : convertTSFunction(Archive *fout, Oid funcOid)
14512 : : {
14513 : : char *result;
14514 : : char query[128];
14515 : : PGresult *res;
14516 : :
14517 : 215 : snprintf(query, sizeof(query),
14518 : : "SELECT '%u'::pg_catalog.regproc", funcOid);
14519 : 215 : res = ExecuteSqlQueryForSingleRow(fout, query);
14520 : :
14521 : 215 : result = pg_strdup(PQgetvalue(res, 0, 0));
14522 : :
14523 : 215 : PQclear(res);
14524 : :
14525 : 215 : return result;
14526 : : }
14527 : :
14528 : : /*
14529 : : * dumpAccessMethod
14530 : : * write out a single access method definition
14531 : : */
14532 : : static void
14533 : 84 : dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo)
14534 : : {
14535 : 84 : DumpOptions *dopt = fout->dopt;
14536 : : PQExpBuffer q;
14537 : : PQExpBuffer delq;
14538 : : char *qamname;
14539 : :
14540 : : /* Do nothing if not dumping schema */
14541 [ + + ]: 84 : if (!dopt->dumpSchema)
14542 : 12 : return;
14543 : :
14544 : 72 : q = createPQExpBuffer();
14545 : 72 : delq = createPQExpBuffer();
14546 : :
14547 : 72 : qamname = pg_strdup(fmtId(aminfo->dobj.name));
14548 : :
14549 : 72 : appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
14550 : :
14551 [ + + - ]: 72 : switch (aminfo->amtype)
14552 : : {
14553 : 34 : case AMTYPE_INDEX:
14554 : 34 : appendPQExpBufferStr(q, "TYPE INDEX ");
14555 : 34 : break;
14556 : 38 : case AMTYPE_TABLE:
14557 : 38 : appendPQExpBufferStr(q, "TYPE TABLE ");
14558 : 38 : break;
14559 : 0 : default:
14560 : 0 : pg_log_warning("invalid type \"%c\" of access method \"%s\"",
14561 : : aminfo->amtype, qamname);
14562 : 0 : destroyPQExpBuffer(q);
14563 : 0 : destroyPQExpBuffer(delq);
14564 : 0 : pg_free(qamname);
14565 : 0 : return;
14566 : : }
14567 : :
14568 : 72 : appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
14569 : :
14570 : 72 : appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
14571 : : qamname);
14572 : :
14573 [ + + ]: 72 : if (dopt->binary_upgrade)
14574 : 4 : binary_upgrade_extension_member(q, &aminfo->dobj,
14575 : : "ACCESS METHOD", qamname, NULL);
14576 : :
14577 [ + - ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14578 : 72 : ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
14579 : 72 : ARCHIVE_OPTS(.tag = aminfo->dobj.name,
14580 : : .description = "ACCESS METHOD",
14581 : : .section = SECTION_PRE_DATA,
14582 : : .createStmt = q->data,
14583 : : .dropStmt = delq->data));
14584 : :
14585 : : /* Dump Access Method Comments */
14586 [ - + ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14587 : 0 : dumpComment(fout, "ACCESS METHOD", qamname,
14588 : : NULL, "",
14589 : 0 : aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
14590 : :
14591 : 72 : destroyPQExpBuffer(q);
14592 : 72 : destroyPQExpBuffer(delq);
14593 : 72 : pg_free(qamname);
14594 : : }
14595 : :
14596 : : /*
14597 : : * dumpOpclass
14598 : : * write out a single operator class definition
14599 : : */
14600 : : static void
14601 : 675 : dumpOpclass(Archive *fout, const OpclassInfo *opcinfo)
14602 : : {
14603 : 675 : DumpOptions *dopt = fout->dopt;
14604 : : PQExpBuffer query;
14605 : : PQExpBuffer q;
14606 : : PQExpBuffer delq;
14607 : : PQExpBuffer nameusing;
14608 : : PGresult *res;
14609 : : int ntups;
14610 : : int i_opcintype;
14611 : : int i_opckeytype;
14612 : : int i_opcdefault;
14613 : : int i_opcfamily;
14614 : : int i_opcfamilyname;
14615 : : int i_opcfamilynsp;
14616 : : int i_amname;
14617 : : int i_amopstrategy;
14618 : : int i_amopopr;
14619 : : int i_sortfamily;
14620 : : int i_sortfamilynsp;
14621 : : int i_amprocnum;
14622 : : int i_amproc;
14623 : : int i_amproclefttype;
14624 : : int i_amprocrighttype;
14625 : : char *opcintype;
14626 : : char *opckeytype;
14627 : : char *opcdefault;
14628 : : char *opcfamily;
14629 : : char *opcfamilyname;
14630 : : char *opcfamilynsp;
14631 : : char *amname;
14632 : : char *amopstrategy;
14633 : : char *amopopr;
14634 : : char *sortfamily;
14635 : : char *sortfamilynsp;
14636 : : char *amprocnum;
14637 : : char *amproc;
14638 : : char *amproclefttype;
14639 : : char *amprocrighttype;
14640 : : bool needComma;
14641 : : int i;
14642 : :
14643 : : /* Do nothing if not dumping schema */
14644 [ + + ]: 675 : if (!dopt->dumpSchema)
14645 : 21 : return;
14646 : :
14647 : 654 : query = createPQExpBuffer();
14648 : 654 : q = createPQExpBuffer();
14649 : 654 : delq = createPQExpBuffer();
14650 : 654 : nameusing = createPQExpBuffer();
14651 : :
14652 : : /* Get additional fields from the pg_opclass row */
14653 : 654 : appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
14654 : : "opckeytype::pg_catalog.regtype, "
14655 : : "opcdefault, opcfamily, "
14656 : : "opfname AS opcfamilyname, "
14657 : : "nspname AS opcfamilynsp, "
14658 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
14659 : : "FROM pg_catalog.pg_opclass c "
14660 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
14661 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14662 : : "WHERE c.oid = '%u'::pg_catalog.oid",
14663 : 654 : opcinfo->dobj.catId.oid);
14664 : :
14665 : 654 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14666 : :
14667 : 654 : i_opcintype = PQfnumber(res, "opcintype");
14668 : 654 : i_opckeytype = PQfnumber(res, "opckeytype");
14669 : 654 : i_opcdefault = PQfnumber(res, "opcdefault");
14670 : 654 : i_opcfamily = PQfnumber(res, "opcfamily");
14671 : 654 : i_opcfamilyname = PQfnumber(res, "opcfamilyname");
14672 : 654 : i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
14673 : 654 : i_amname = PQfnumber(res, "amname");
14674 : :
14675 : : /* opcintype may still be needed after we PQclear res */
14676 : 654 : opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
14677 : 654 : opckeytype = PQgetvalue(res, 0, i_opckeytype);
14678 : 654 : opcdefault = PQgetvalue(res, 0, i_opcdefault);
14679 : : /* opcfamily will still be needed after we PQclear res */
14680 : 654 : opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily));
14681 : 654 : opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname);
14682 : 654 : opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
14683 : : /* amname will still be needed after we PQclear res */
14684 : 654 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14685 : :
14686 : 654 : appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
14687 : 654 : fmtQualifiedDumpable(opcinfo));
14688 : 654 : appendPQExpBuffer(delq, " USING %s;\n",
14689 : : fmtId(amname));
14690 : :
14691 : : /* Build the fixed portion of the CREATE command */
14692 : 654 : appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ",
14693 : 654 : fmtQualifiedDumpable(opcinfo));
14694 [ + + ]: 654 : if (strcmp(opcdefault, "t") == 0)
14695 : 366 : appendPQExpBufferStr(q, "DEFAULT ");
14696 : 654 : appendPQExpBuffer(q, "FOR TYPE %s USING %s",
14697 : : opcintype,
14698 : : fmtId(amname));
14699 [ + - ]: 654 : if (strlen(opcfamilyname) > 0)
14700 : : {
14701 : 654 : appendPQExpBufferStr(q, " FAMILY ");
14702 : 654 : appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
14703 : 654 : appendPQExpBufferStr(q, fmtId(opcfamilyname));
14704 : : }
14705 : 654 : appendPQExpBufferStr(q, " AS\n ");
14706 : :
14707 : 654 : needComma = false;
14708 : :
14709 [ + + ]: 654 : if (strcmp(opckeytype, "-") != 0)
14710 : : {
14711 : 252 : appendPQExpBuffer(q, "STORAGE %s",
14712 : : opckeytype);
14713 : 252 : needComma = true;
14714 : : }
14715 : :
14716 : 654 : PQclear(res);
14717 : :
14718 : : /*
14719 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
14720 : : *
14721 : : * Print only those opfamily members that are tied to the opclass by
14722 : : * pg_depend entries.
14723 : : */
14724 : 654 : resetPQExpBuffer(query);
14725 : 654 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14726 : : "amopopr::pg_catalog.regoperator, "
14727 : : "opfname AS sortfamily, "
14728 : : "nspname AS sortfamilynsp "
14729 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14730 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14731 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14732 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14733 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14734 : : "AND refobjid = '%u'::pg_catalog.oid "
14735 : : "AND amopfamily = '%s'::pg_catalog.oid "
14736 : : "ORDER BY amopstrategy",
14737 : 654 : opcinfo->dobj.catId.oid,
14738 : : opcfamily);
14739 : :
14740 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14741 : :
14742 : 654 : ntups = PQntuples(res);
14743 : :
14744 : 654 : i_amopstrategy = PQfnumber(res, "amopstrategy");
14745 : 654 : i_amopopr = PQfnumber(res, "amopopr");
14746 : 654 : i_sortfamily = PQfnumber(res, "sortfamily");
14747 : 654 : i_sortfamilynsp = PQfnumber(res, "sortfamilynsp");
14748 : :
14749 [ + + ]: 868 : for (i = 0; i < ntups; i++)
14750 : : {
14751 : 214 : amopstrategy = PQgetvalue(res, i, i_amopstrategy);
14752 : 214 : amopopr = PQgetvalue(res, i, i_amopopr);
14753 : 214 : sortfamily = PQgetvalue(res, i, i_sortfamily);
14754 : 214 : sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp);
14755 : :
14756 [ + + ]: 214 : if (needComma)
14757 : 136 : appendPQExpBufferStr(q, " ,\n ");
14758 : :
14759 : 214 : appendPQExpBuffer(q, "OPERATOR %s %s",
14760 : : amopstrategy, amopopr);
14761 : :
14762 [ - + ]: 214 : if (strlen(sortfamily) > 0)
14763 : : {
14764 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
14765 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
14766 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
14767 : : }
14768 : :
14769 : 214 : needComma = true;
14770 : : }
14771 : :
14772 : 654 : PQclear(res);
14773 : :
14774 : : /*
14775 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
14776 : : *
14777 : : * Print only those opfamily members that are tied to the opclass by
14778 : : * pg_depend entries.
14779 : : *
14780 : : * We print the amproclefttype/amprocrighttype even though in most cases
14781 : : * the backend could deduce the right values, because of the corner case
14782 : : * of a btree sort support function for a cross-type comparison.
14783 : : */
14784 : 654 : resetPQExpBuffer(query);
14785 : :
14786 : 654 : appendPQExpBuffer(query, "SELECT amprocnum, "
14787 : : "amproc::pg_catalog.regprocedure, "
14788 : : "amproclefttype::pg_catalog.regtype, "
14789 : : "amprocrighttype::pg_catalog.regtype "
14790 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14791 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14792 : : "AND refobjid = '%u'::pg_catalog.oid "
14793 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14794 : : "AND objid = ap.oid "
14795 : : "ORDER BY amprocnum",
14796 : 654 : opcinfo->dobj.catId.oid);
14797 : :
14798 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14799 : :
14800 : 654 : ntups = PQntuples(res);
14801 : :
14802 : 654 : i_amprocnum = PQfnumber(res, "amprocnum");
14803 : 654 : i_amproc = PQfnumber(res, "amproc");
14804 : 654 : i_amproclefttype = PQfnumber(res, "amproclefttype");
14805 : 654 : i_amprocrighttype = PQfnumber(res, "amprocrighttype");
14806 : :
14807 [ + + ]: 688 : for (i = 0; i < ntups; i++)
14808 : : {
14809 : 34 : amprocnum = PQgetvalue(res, i, i_amprocnum);
14810 : 34 : amproc = PQgetvalue(res, i, i_amproc);
14811 : 34 : amproclefttype = PQgetvalue(res, i, i_amproclefttype);
14812 : 34 : amprocrighttype = PQgetvalue(res, i, i_amprocrighttype);
14813 : :
14814 [ + - ]: 34 : if (needComma)
14815 : 34 : appendPQExpBufferStr(q, " ,\n ");
14816 : :
14817 : 34 : appendPQExpBuffer(q, "FUNCTION %s", amprocnum);
14818 : :
14819 [ + - + - ]: 34 : if (*amproclefttype && *amprocrighttype)
14820 : 34 : appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype);
14821 : :
14822 : 34 : appendPQExpBuffer(q, " %s", amproc);
14823 : :
14824 : 34 : needComma = true;
14825 : : }
14826 : :
14827 : 654 : PQclear(res);
14828 : :
14829 : : /*
14830 : : * If needComma is still false it means we haven't added anything after
14831 : : * the AS keyword. To avoid printing broken SQL, append a dummy STORAGE
14832 : : * clause with the same datatype. This isn't sanctioned by the
14833 : : * documentation, but actually DefineOpClass will treat it as a no-op.
14834 : : */
14835 [ + + ]: 654 : if (!needComma)
14836 : 324 : appendPQExpBuffer(q, "STORAGE %s", opcintype);
14837 : :
14838 : 654 : appendPQExpBufferStr(q, ";\n");
14839 : :
14840 : 654 : appendPQExpBufferStr(nameusing, fmtId(opcinfo->dobj.name));
14841 : 654 : appendPQExpBuffer(nameusing, " USING %s",
14842 : : fmtId(amname));
14843 : :
14844 [ + + ]: 654 : if (dopt->binary_upgrade)
14845 : 6 : binary_upgrade_extension_member(q, &opcinfo->dobj,
14846 : 6 : "OPERATOR CLASS", nameusing->data,
14847 : 6 : opcinfo->dobj.namespace->dobj.name);
14848 : :
14849 [ + - ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14850 : 654 : ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
14851 : 654 : ARCHIVE_OPTS(.tag = opcinfo->dobj.name,
14852 : : .namespace = opcinfo->dobj.namespace->dobj.name,
14853 : : .owner = opcinfo->rolname,
14854 : : .description = "OPERATOR CLASS",
14855 : : .section = SECTION_PRE_DATA,
14856 : : .createStmt = q->data,
14857 : : .dropStmt = delq->data));
14858 : :
14859 : : /* Dump Operator Class Comments */
14860 [ - + ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14861 : 0 : dumpComment(fout, "OPERATOR CLASS", nameusing->data,
14862 : 0 : opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
14863 : 0 : opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
14864 : :
14865 : 654 : pg_free(opcintype);
14866 : 654 : pg_free(opcfamily);
14867 : 654 : pg_free(amname);
14868 : 654 : destroyPQExpBuffer(query);
14869 : 654 : destroyPQExpBuffer(q);
14870 : 654 : destroyPQExpBuffer(delq);
14871 : 654 : destroyPQExpBuffer(nameusing);
14872 : : }
14873 : :
14874 : : /*
14875 : : * dumpOpfamily
14876 : : * write out a single operator family definition
14877 : : *
14878 : : * Note: this also dumps any "loose" operator members that aren't bound to a
14879 : : * specific opclass within the opfamily.
14880 : : */
14881 : : static void
14882 : 561 : dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo)
14883 : : {
14884 : 561 : DumpOptions *dopt = fout->dopt;
14885 : : PQExpBuffer query;
14886 : : PQExpBuffer q;
14887 : : PQExpBuffer delq;
14888 : : PQExpBuffer nameusing;
14889 : : PGresult *res;
14890 : : PGresult *res_ops;
14891 : : PGresult *res_procs;
14892 : : int ntups;
14893 : : int i_amname;
14894 : : int i_amopstrategy;
14895 : : int i_amopopr;
14896 : : int i_sortfamily;
14897 : : int i_sortfamilynsp;
14898 : : int i_amprocnum;
14899 : : int i_amproc;
14900 : : int i_amproclefttype;
14901 : : int i_amprocrighttype;
14902 : : char *amname;
14903 : : char *amopstrategy;
14904 : : char *amopopr;
14905 : : char *sortfamily;
14906 : : char *sortfamilynsp;
14907 : : char *amprocnum;
14908 : : char *amproc;
14909 : : char *amproclefttype;
14910 : : char *amprocrighttype;
14911 : : bool needComma;
14912 : : int i;
14913 : :
14914 : : /* Do nothing if not dumping schema */
14915 [ + + ]: 561 : if (!dopt->dumpSchema)
14916 : 14 : return;
14917 : :
14918 : 547 : query = createPQExpBuffer();
14919 : 547 : q = createPQExpBuffer();
14920 : 547 : delq = createPQExpBuffer();
14921 : 547 : nameusing = createPQExpBuffer();
14922 : :
14923 : : /*
14924 : : * Fetch only those opfamily members that are tied directly to the
14925 : : * opfamily by pg_depend entries.
14926 : : */
14927 : 547 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14928 : : "amopopr::pg_catalog.regoperator, "
14929 : : "opfname AS sortfamily, "
14930 : : "nspname AS sortfamilynsp "
14931 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14932 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14933 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14934 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14935 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14936 : : "AND refobjid = '%u'::pg_catalog.oid "
14937 : : "AND amopfamily = '%u'::pg_catalog.oid "
14938 : : "ORDER BY amopstrategy",
14939 : 547 : opfinfo->dobj.catId.oid,
14940 : 547 : opfinfo->dobj.catId.oid);
14941 : :
14942 : 547 : res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14943 : :
14944 : 547 : resetPQExpBuffer(query);
14945 : :
14946 : 547 : appendPQExpBuffer(query, "SELECT amprocnum, "
14947 : : "amproc::pg_catalog.regprocedure, "
14948 : : "amproclefttype::pg_catalog.regtype, "
14949 : : "amprocrighttype::pg_catalog.regtype "
14950 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14951 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14952 : : "AND refobjid = '%u'::pg_catalog.oid "
14953 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14954 : : "AND objid = ap.oid "
14955 : : "ORDER BY amprocnum",
14956 : 547 : opfinfo->dobj.catId.oid);
14957 : :
14958 : 547 : res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14959 : :
14960 : : /* Get additional fields from the pg_opfamily row */
14961 : 547 : resetPQExpBuffer(query);
14962 : :
14963 : 547 : appendPQExpBuffer(query, "SELECT "
14964 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
14965 : : "FROM pg_catalog.pg_opfamily "
14966 : : "WHERE oid = '%u'::pg_catalog.oid",
14967 : 547 : opfinfo->dobj.catId.oid);
14968 : :
14969 : 547 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14970 : :
14971 : 547 : i_amname = PQfnumber(res, "amname");
14972 : :
14973 : : /* amname will still be needed after we PQclear res */
14974 : 547 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14975 : :
14976 : 547 : appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
14977 : 547 : fmtQualifiedDumpable(opfinfo));
14978 : 547 : appendPQExpBuffer(delq, " USING %s;\n",
14979 : : fmtId(amname));
14980 : :
14981 : : /* Build the fixed portion of the CREATE command */
14982 : 547 : appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
14983 : 547 : fmtQualifiedDumpable(opfinfo));
14984 : 547 : appendPQExpBuffer(q, " USING %s;\n",
14985 : : fmtId(amname));
14986 : :
14987 : 547 : PQclear(res);
14988 : :
14989 : : /* Do we need an ALTER to add loose members? */
14990 [ + + + + ]: 547 : if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
14991 : : {
14992 : 49 : appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
14993 : 49 : fmtQualifiedDumpable(opfinfo));
14994 : 49 : appendPQExpBuffer(q, " USING %s ADD\n ",
14995 : : fmtId(amname));
14996 : :
14997 : 49 : needComma = false;
14998 : :
14999 : : /*
15000 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
15001 : : */
15002 : 49 : ntups = PQntuples(res_ops);
15003 : :
15004 : 49 : i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
15005 : 49 : i_amopopr = PQfnumber(res_ops, "amopopr");
15006 : 49 : i_sortfamily = PQfnumber(res_ops, "sortfamily");
15007 : 49 : i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp");
15008 : :
15009 [ + + ]: 219 : for (i = 0; i < ntups; i++)
15010 : : {
15011 : 170 : amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
15012 : 170 : amopopr = PQgetvalue(res_ops, i, i_amopopr);
15013 : 170 : sortfamily = PQgetvalue(res_ops, i, i_sortfamily);
15014 : 170 : sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp);
15015 : :
15016 [ + + ]: 170 : if (needComma)
15017 : 136 : appendPQExpBufferStr(q, " ,\n ");
15018 : :
15019 : 170 : appendPQExpBuffer(q, "OPERATOR %s %s",
15020 : : amopstrategy, amopopr);
15021 : :
15022 [ - + ]: 170 : if (strlen(sortfamily) > 0)
15023 : : {
15024 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
15025 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
15026 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
15027 : : }
15028 : :
15029 : 170 : needComma = true;
15030 : : }
15031 : :
15032 : : /*
15033 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
15034 : : */
15035 : 49 : ntups = PQntuples(res_procs);
15036 : :
15037 : 49 : i_amprocnum = PQfnumber(res_procs, "amprocnum");
15038 : 49 : i_amproc = PQfnumber(res_procs, "amproc");
15039 : 49 : i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
15040 : 49 : i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
15041 : :
15042 [ + + ]: 234 : for (i = 0; i < ntups; i++)
15043 : : {
15044 : 185 : amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
15045 : 185 : amproc = PQgetvalue(res_procs, i, i_amproc);
15046 : 185 : amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
15047 : 185 : amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
15048 : :
15049 [ + + ]: 185 : if (needComma)
15050 : 170 : appendPQExpBufferStr(q, " ,\n ");
15051 : :
15052 : 185 : appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
15053 : : amprocnum, amproclefttype, amprocrighttype,
15054 : : amproc);
15055 : :
15056 : 185 : needComma = true;
15057 : : }
15058 : :
15059 : 49 : appendPQExpBufferStr(q, ";\n");
15060 : : }
15061 : :
15062 : 547 : appendPQExpBufferStr(nameusing, fmtId(opfinfo->dobj.name));
15063 : 547 : appendPQExpBuffer(nameusing, " USING %s",
15064 : : fmtId(amname));
15065 : :
15066 [ + + ]: 547 : if (dopt->binary_upgrade)
15067 : 9 : binary_upgrade_extension_member(q, &opfinfo->dobj,
15068 : 9 : "OPERATOR FAMILY", nameusing->data,
15069 : 9 : opfinfo->dobj.namespace->dobj.name);
15070 : :
15071 [ + - ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15072 : 547 : ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
15073 : 547 : ARCHIVE_OPTS(.tag = opfinfo->dobj.name,
15074 : : .namespace = opfinfo->dobj.namespace->dobj.name,
15075 : : .owner = opfinfo->rolname,
15076 : : .description = "OPERATOR FAMILY",
15077 : : .section = SECTION_PRE_DATA,
15078 : : .createStmt = q->data,
15079 : : .dropStmt = delq->data));
15080 : :
15081 : : /* Dump Operator Family Comments */
15082 [ - + ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15083 : 0 : dumpComment(fout, "OPERATOR FAMILY", nameusing->data,
15084 : 0 : opfinfo->dobj.namespace->dobj.name, opfinfo->rolname,
15085 : 0 : opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
15086 : :
15087 : 547 : pg_free(amname);
15088 : 547 : PQclear(res_ops);
15089 : 547 : PQclear(res_procs);
15090 : 547 : destroyPQExpBuffer(query);
15091 : 547 : destroyPQExpBuffer(q);
15092 : 547 : destroyPQExpBuffer(delq);
15093 : 547 : destroyPQExpBuffer(nameusing);
15094 : : }
15095 : :
15096 : : /*
15097 : : * dumpCollation
15098 : : * write out a single collation definition
15099 : : */
15100 : : static void
15101 : 2733 : dumpCollation(Archive *fout, const CollInfo *collinfo)
15102 : : {
15103 : 2733 : DumpOptions *dopt = fout->dopt;
15104 : : PQExpBuffer query;
15105 : : PQExpBuffer q;
15106 : : PQExpBuffer delq;
15107 : : char *qcollname;
15108 : : PGresult *res;
15109 : : int i_collprovider;
15110 : : int i_collisdeterministic;
15111 : : int i_collcollate;
15112 : : int i_collctype;
15113 : : int i_colllocale;
15114 : : int i_collicurules;
15115 : : const char *collprovider;
15116 : : const char *collcollate;
15117 : : const char *collctype;
15118 : : const char *colllocale;
15119 : : const char *collicurules;
15120 : :
15121 : : /* Do nothing if not dumping schema */
15122 [ + + ]: 2733 : if (!dopt->dumpSchema)
15123 : 12 : return;
15124 : :
15125 : 2721 : query = createPQExpBuffer();
15126 : 2721 : q = createPQExpBuffer();
15127 : 2721 : delq = createPQExpBuffer();
15128 : :
15129 : 2721 : qcollname = pg_strdup(fmtId(collinfo->dobj.name));
15130 : :
15131 : : /* Get collation-specific details */
15132 : 2721 : appendPQExpBufferStr(query, "SELECT ");
15133 : :
15134 : 2721 : appendPQExpBufferStr(query,
15135 : : "collprovider, "
15136 : : "collversion, ");
15137 : :
15138 [ + - ]: 2721 : if (fout->remoteVersion >= 120000)
15139 : 2721 : appendPQExpBufferStr(query,
15140 : : "collisdeterministic, ");
15141 : : else
15142 : 0 : appendPQExpBufferStr(query,
15143 : : "true AS collisdeterministic, ");
15144 : :
15145 [ + - ]: 2721 : if (fout->remoteVersion >= 170000)
15146 : 2721 : appendPQExpBufferStr(query,
15147 : : "colllocale, ");
15148 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
15149 : 0 : appendPQExpBufferStr(query,
15150 : : "colliculocale AS colllocale, ");
15151 : : else
15152 : 0 : appendPQExpBufferStr(query,
15153 : : "NULL AS colllocale, ");
15154 : :
15155 [ + - ]: 2721 : if (fout->remoteVersion >= 160000)
15156 : 2721 : appendPQExpBufferStr(query,
15157 : : "collicurules, ");
15158 : : else
15159 : 0 : appendPQExpBufferStr(query,
15160 : : "NULL AS collicurules, ");
15161 : :
15162 : 2721 : appendPQExpBuffer(query,
15163 : : "collcollate, "
15164 : : "collctype "
15165 : : "FROM pg_catalog.pg_collation c "
15166 : : "WHERE c.oid = '%u'::pg_catalog.oid",
15167 : 2721 : collinfo->dobj.catId.oid);
15168 : :
15169 : 2721 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15170 : :
15171 : 2721 : i_collprovider = PQfnumber(res, "collprovider");
15172 : 2721 : i_collisdeterministic = PQfnumber(res, "collisdeterministic");
15173 : 2721 : i_collcollate = PQfnumber(res, "collcollate");
15174 : 2721 : i_collctype = PQfnumber(res, "collctype");
15175 : 2721 : i_colllocale = PQfnumber(res, "colllocale");
15176 : 2721 : i_collicurules = PQfnumber(res, "collicurules");
15177 : :
15178 : 2721 : collprovider = PQgetvalue(res, 0, i_collprovider);
15179 : :
15180 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collcollate))
15181 : 48 : collcollate = PQgetvalue(res, 0, i_collcollate);
15182 : : else
15183 : 2673 : collcollate = NULL;
15184 : :
15185 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collctype))
15186 : 48 : collctype = PQgetvalue(res, 0, i_collctype);
15187 : : else
15188 : 2673 : collctype = NULL;
15189 : :
15190 : : /*
15191 : : * Before version 15, collcollate and collctype were of type NAME and
15192 : : * non-nullable. Treat empty strings as NULL for consistency.
15193 : : */
15194 [ - + ]: 2721 : if (fout->remoteVersion < 150000)
15195 : : {
15196 [ # # ]: 0 : if (collcollate[0] == '\0')
15197 : 0 : collcollate = NULL;
15198 [ # # ]: 0 : if (collctype[0] == '\0')
15199 : 0 : collctype = NULL;
15200 : : }
15201 : :
15202 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_colllocale))
15203 : 2670 : colllocale = PQgetvalue(res, 0, i_colllocale);
15204 : : else
15205 : 51 : colllocale = NULL;
15206 : :
15207 [ - + ]: 2721 : if (!PQgetisnull(res, 0, i_collicurules))
15208 : 0 : collicurules = PQgetvalue(res, 0, i_collicurules);
15209 : : else
15210 : 2721 : collicurules = NULL;
15211 : :
15212 : 2721 : appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
15213 : 2721 : fmtQualifiedDumpable(collinfo));
15214 : :
15215 : 2721 : appendPQExpBuffer(q, "CREATE COLLATION %s (",
15216 : 2721 : fmtQualifiedDumpable(collinfo));
15217 : :
15218 : 2721 : appendPQExpBufferStr(q, "provider = ");
15219 [ + + ]: 2721 : if (collprovider[0] == 'b')
15220 : 19 : appendPQExpBufferStr(q, "builtin");
15221 [ + + ]: 2702 : else if (collprovider[0] == 'c')
15222 : 48 : appendPQExpBufferStr(q, "libc");
15223 [ + + ]: 2654 : else if (collprovider[0] == 'i')
15224 : 2651 : appendPQExpBufferStr(q, "icu");
15225 [ + - ]: 3 : else if (collprovider[0] == 'd')
15226 : : /* to allow dumping pg_catalog; not accepted on input */
15227 : 3 : appendPQExpBufferStr(q, "default");
15228 : : else
15229 : 0 : pg_fatal("unrecognized collation provider: %s",
15230 : : collprovider);
15231 : :
15232 [ - + ]: 2721 : if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
15233 : 0 : appendPQExpBufferStr(q, ", deterministic = false");
15234 : :
15235 [ + + ]: 2721 : if (collprovider[0] == 'd')
15236 : : {
15237 [ + - + - : 3 : if (collcollate || collctype || colllocale || collicurules)
+ - - + ]
15238 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15239 : :
15240 : : /* no locale -- the default collation cannot be reloaded anyway */
15241 : : }
15242 [ + + ]: 2718 : else if (collprovider[0] == 'b')
15243 : : {
15244 [ + - + - : 19 : if (collcollate || collctype || !colllocale || collicurules)
+ - - + ]
15245 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15246 : :
15247 : 19 : appendPQExpBufferStr(q, ", locale = ");
15248 [ + - ]: 19 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15249 : : fout);
15250 : : }
15251 [ + + ]: 2699 : else if (collprovider[0] == 'i')
15252 : : {
15253 [ + - ]: 2651 : if (fout->remoteVersion >= 150000)
15254 : : {
15255 [ + - + - : 2651 : if (collcollate || collctype || !colllocale)
- + ]
15256 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15257 : :
15258 : 2651 : appendPQExpBufferStr(q, ", locale = ");
15259 [ + - ]: 2651 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15260 : : fout);
15261 : : }
15262 : : else
15263 : : {
15264 [ # # # # : 0 : if (!collcollate || !collctype || colllocale ||
# # ]
15265 [ # # ]: 0 : strcmp(collcollate, collctype) != 0)
15266 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15267 : :
15268 : 0 : appendPQExpBufferStr(q, ", locale = ");
15269 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15270 : : }
15271 : :
15272 [ - + ]: 2651 : if (collicurules)
15273 : : {
15274 : 0 : appendPQExpBufferStr(q, ", rules = ");
15275 [ # # ]: 0 : appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
15276 : : }
15277 : : }
15278 [ + - ]: 48 : else if (collprovider[0] == 'c')
15279 : : {
15280 [ + - + - : 48 : if (colllocale || collicurules || !collcollate || !collctype)
+ - - + ]
15281 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15282 : :
15283 [ + - + - : 48 : if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
+ - ]
15284 : : {
15285 : 48 : appendPQExpBufferStr(q, ", locale = ");
15286 [ + - ]: 48 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15287 : : }
15288 : : else
15289 : : {
15290 : 0 : appendPQExpBufferStr(q, ", lc_collate = ");
15291 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15292 : 0 : appendPQExpBufferStr(q, ", lc_ctype = ");
15293 [ # # ]: 0 : appendStringLiteralAH(q, collctype ? collctype : "", fout);
15294 : : }
15295 : : }
15296 : : else
15297 : 0 : pg_fatal("unrecognized collation provider: %s", collprovider);
15298 : :
15299 : : /*
15300 : : * For binary upgrade, carry over the collation version. For normal
15301 : : * dump/restore, omit the version, so that it is computed upon restore.
15302 : : */
15303 [ + + ]: 2721 : if (dopt->binary_upgrade)
15304 : : {
15305 : : int i_collversion;
15306 : :
15307 : 5 : i_collversion = PQfnumber(res, "collversion");
15308 [ + + ]: 5 : if (!PQgetisnull(res, 0, i_collversion))
15309 : : {
15310 : 4 : appendPQExpBufferStr(q, ", version = ");
15311 : 4 : appendStringLiteralAH(q,
15312 : : PQgetvalue(res, 0, i_collversion),
15313 : : fout);
15314 : : }
15315 : : }
15316 : :
15317 : 2721 : appendPQExpBufferStr(q, ");\n");
15318 : :
15319 [ + + ]: 2721 : if (dopt->binary_upgrade)
15320 : 5 : binary_upgrade_extension_member(q, &collinfo->dobj,
15321 : : "COLLATION", qcollname,
15322 : 5 : collinfo->dobj.namespace->dobj.name);
15323 : :
15324 [ + - ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15325 : 2721 : ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
15326 : 2721 : ARCHIVE_OPTS(.tag = collinfo->dobj.name,
15327 : : .namespace = collinfo->dobj.namespace->dobj.name,
15328 : : .owner = collinfo->rolname,
15329 : : .description = "COLLATION",
15330 : : .section = SECTION_PRE_DATA,
15331 : : .createStmt = q->data,
15332 : : .dropStmt = delq->data));
15333 : :
15334 : : /* Dump Collation Comments */
15335 [ + + ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15336 : 2613 : dumpComment(fout, "COLLATION", qcollname,
15337 : 2613 : collinfo->dobj.namespace->dobj.name, collinfo->rolname,
15338 : 2613 : collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
15339 : :
15340 : 2721 : PQclear(res);
15341 : :
15342 : 2721 : destroyPQExpBuffer(query);
15343 : 2721 : destroyPQExpBuffer(q);
15344 : 2721 : destroyPQExpBuffer(delq);
15345 : 2721 : pg_free(qcollname);
15346 : : }
15347 : :
15348 : : /*
15349 : : * dumpConversion
15350 : : * write out a single conversion definition
15351 : : */
15352 : : static void
15353 : 335 : dumpConversion(Archive *fout, const ConvInfo *convinfo)
15354 : : {
15355 : 335 : DumpOptions *dopt = fout->dopt;
15356 : : PQExpBuffer query;
15357 : : PQExpBuffer q;
15358 : : PQExpBuffer delq;
15359 : : char *qconvname;
15360 : : PGresult *res;
15361 : : int i_conforencoding;
15362 : : int i_contoencoding;
15363 : : int i_conproc;
15364 : : int i_condefault;
15365 : : const char *conforencoding;
15366 : : const char *contoencoding;
15367 : : const char *conproc;
15368 : : bool condefault;
15369 : :
15370 : : /* Do nothing if not dumping schema */
15371 [ + + ]: 335 : if (!dopt->dumpSchema)
15372 : 7 : return;
15373 : :
15374 : 328 : query = createPQExpBuffer();
15375 : 328 : q = createPQExpBuffer();
15376 : 328 : delq = createPQExpBuffer();
15377 : :
15378 : 328 : qconvname = pg_strdup(fmtId(convinfo->dobj.name));
15379 : :
15380 : : /* Get conversion-specific details */
15381 : 328 : appendPQExpBuffer(query, "SELECT "
15382 : : "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
15383 : : "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
15384 : : "conproc, condefault "
15385 : : "FROM pg_catalog.pg_conversion c "
15386 : : "WHERE c.oid = '%u'::pg_catalog.oid",
15387 : 328 : convinfo->dobj.catId.oid);
15388 : :
15389 : 328 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15390 : :
15391 : 328 : i_conforencoding = PQfnumber(res, "conforencoding");
15392 : 328 : i_contoencoding = PQfnumber(res, "contoencoding");
15393 : 328 : i_conproc = PQfnumber(res, "conproc");
15394 : 328 : i_condefault = PQfnumber(res, "condefault");
15395 : :
15396 : 328 : conforencoding = PQgetvalue(res, 0, i_conforencoding);
15397 : 328 : contoencoding = PQgetvalue(res, 0, i_contoencoding);
15398 : 328 : conproc = PQgetvalue(res, 0, i_conproc);
15399 : 328 : condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
15400 : :
15401 : 328 : appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
15402 : 328 : fmtQualifiedDumpable(convinfo));
15403 : :
15404 [ + - ]: 328 : appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
15405 : : (condefault) ? "DEFAULT " : "",
15406 : 328 : fmtQualifiedDumpable(convinfo));
15407 : 328 : appendStringLiteralAH(q, conforencoding, fout);
15408 : 328 : appendPQExpBufferStr(q, " TO ");
15409 : 328 : appendStringLiteralAH(q, contoencoding, fout);
15410 : : /* regproc output is already sufficiently quoted */
15411 : 328 : appendPQExpBuffer(q, " FROM %s;\n", conproc);
15412 : :
15413 [ + + ]: 328 : if (dopt->binary_upgrade)
15414 : 1 : binary_upgrade_extension_member(q, &convinfo->dobj,
15415 : : "CONVERSION", qconvname,
15416 : 1 : convinfo->dobj.namespace->dobj.name);
15417 : :
15418 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15419 : 328 : ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
15420 : 328 : ARCHIVE_OPTS(.tag = convinfo->dobj.name,
15421 : : .namespace = convinfo->dobj.namespace->dobj.name,
15422 : : .owner = convinfo->rolname,
15423 : : .description = "CONVERSION",
15424 : : .section = SECTION_PRE_DATA,
15425 : : .createStmt = q->data,
15426 : : .dropStmt = delq->data));
15427 : :
15428 : : /* Dump Conversion Comments */
15429 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15430 : 328 : dumpComment(fout, "CONVERSION", qconvname,
15431 : 328 : convinfo->dobj.namespace->dobj.name, convinfo->rolname,
15432 : 328 : convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
15433 : :
15434 : 328 : PQclear(res);
15435 : :
15436 : 328 : destroyPQExpBuffer(query);
15437 : 328 : destroyPQExpBuffer(q);
15438 : 328 : destroyPQExpBuffer(delq);
15439 : 328 : pg_free(qconvname);
15440 : : }
15441 : :
15442 : : /*
15443 : : * format_aggregate_signature: generate aggregate name and argument list
15444 : : *
15445 : : * The argument type names are qualified if needed. The aggregate name
15446 : : * is never qualified.
15447 : : */
15448 : : static char *
15449 : 287 : format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
15450 : : {
15451 : : PQExpBufferData buf;
15452 : : int j;
15453 : :
15454 : 287 : initPQExpBuffer(&buf);
15455 [ - + ]: 287 : if (honor_quotes)
15456 : 0 : appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name));
15457 : : else
15458 : 287 : appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name);
15459 : :
15460 [ + + ]: 287 : if (agginfo->aggfn.nargs == 0)
15461 : 40 : appendPQExpBufferStr(&buf, "(*)");
15462 : : else
15463 : : {
15464 : 247 : appendPQExpBufferChar(&buf, '(');
15465 [ + + ]: 539 : for (j = 0; j < agginfo->aggfn.nargs; j++)
15466 [ + + ]: 292 : appendPQExpBuffer(&buf, "%s%s",
15467 : : (j > 0) ? ", " : "",
15468 : : getFormattedTypeName(fout,
15469 : 292 : agginfo->aggfn.argtypes[j],
15470 : : zeroIsError));
15471 : 247 : appendPQExpBufferChar(&buf, ')');
15472 : : }
15473 : 287 : return buf.data;
15474 : : }
15475 : :
15476 : : /*
15477 : : * dumpAgg
15478 : : * write out a single aggregate definition
15479 : : */
15480 : : static void
15481 : 295 : dumpAgg(Archive *fout, const AggInfo *agginfo)
15482 : : {
15483 : 295 : DumpOptions *dopt = fout->dopt;
15484 : : PQExpBuffer query;
15485 : : PQExpBuffer q;
15486 : : PQExpBuffer delq;
15487 : : PQExpBuffer details;
15488 : : char *aggsig; /* identity signature */
15489 : 295 : char *aggfullsig = NULL; /* full signature */
15490 : : char *aggsig_tag;
15491 : : PGresult *res;
15492 : : int i_agginitval;
15493 : : int i_aggminitval;
15494 : : const char *aggtransfn;
15495 : : const char *aggfinalfn;
15496 : : const char *aggcombinefn;
15497 : : const char *aggserialfn;
15498 : : const char *aggdeserialfn;
15499 : : const char *aggmtransfn;
15500 : : const char *aggminvtransfn;
15501 : : const char *aggmfinalfn;
15502 : : bool aggfinalextra;
15503 : : bool aggmfinalextra;
15504 : : char aggfinalmodify;
15505 : : char aggmfinalmodify;
15506 : : const char *aggsortop;
15507 : : char *aggsortconvop;
15508 : : char aggkind;
15509 : : const char *aggtranstype;
15510 : : const char *aggtransspace;
15511 : : const char *aggmtranstype;
15512 : : const char *aggmtransspace;
15513 : : const char *agginitval;
15514 : : const char *aggminitval;
15515 : : const char *proparallel;
15516 : : const char *prosupport;
15517 : : char defaultfinalmodify;
15518 : :
15519 : : /* Do nothing if not dumping schema */
15520 [ + + ]: 295 : if (!dopt->dumpSchema)
15521 : 8 : return;
15522 : :
15523 : 287 : query = createPQExpBuffer();
15524 : 287 : q = createPQExpBuffer();
15525 : 287 : delq = createPQExpBuffer();
15526 : 287 : details = createPQExpBuffer();
15527 : :
15528 [ + + ]: 287 : if (!fout->is_prepared[PREPQUERY_DUMPAGG])
15529 : : {
15530 : : /* Set up query for aggregate-specific details */
15531 : 57 : appendPQExpBufferStr(query,
15532 : : "PREPARE dumpAgg(pg_catalog.oid) AS\n");
15533 : :
15534 : 57 : appendPQExpBufferStr(query,
15535 : : "SELECT "
15536 : : "aggtransfn,\n"
15537 : : "aggfinalfn,\n"
15538 : : "aggtranstype::pg_catalog.regtype,\n"
15539 : : "agginitval,\n"
15540 : : "aggsortop,\n"
15541 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
15542 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
15543 : :
15544 : 57 : appendPQExpBufferStr(query,
15545 : : "aggkind,\n"
15546 : : "aggmtransfn,\n"
15547 : : "aggminvtransfn,\n"
15548 : : "aggmfinalfn,\n"
15549 : : "aggmtranstype::pg_catalog.regtype,\n"
15550 : : "aggfinalextra,\n"
15551 : : "aggmfinalextra,\n"
15552 : : "aggtransspace,\n"
15553 : : "aggmtransspace,\n"
15554 : : "aggminitval,\n");
15555 : :
15556 : 57 : appendPQExpBufferStr(query,
15557 : : "aggcombinefn,\n"
15558 : : "aggserialfn,\n"
15559 : : "aggdeserialfn,\n"
15560 : : "proparallel,\n");
15561 : :
15562 [ + - ]: 57 : if (fout->remoteVersion >= 110000)
15563 : 57 : appendPQExpBufferStr(query,
15564 : : "aggfinalmodify,\n"
15565 : : "aggmfinalmodify,\n");
15566 : : else
15567 : 0 : appendPQExpBufferStr(query,
15568 : : "'0' AS aggfinalmodify,\n"
15569 : : "'0' AS aggmfinalmodify,\n");
15570 : :
15571 [ + - ]: 57 : if (fout->remoteVersion >= 120000)
15572 : 57 : appendPQExpBufferStr(query,
15573 : : "prosupport\n");
15574 : : else
15575 : 0 : appendPQExpBufferStr(query,
15576 : : "'-' AS prosupport\n");
15577 : :
15578 : 57 : appendPQExpBufferStr(query,
15579 : : "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
15580 : : "WHERE a.aggfnoid = p.oid "
15581 : : "AND p.oid = $1");
15582 : :
15583 : 57 : ExecuteSqlStatement(fout, query->data);
15584 : :
15585 : 57 : fout->is_prepared[PREPQUERY_DUMPAGG] = true;
15586 : : }
15587 : :
15588 : 287 : printfPQExpBuffer(query,
15589 : : "EXECUTE dumpAgg('%u')",
15590 : 287 : agginfo->aggfn.dobj.catId.oid);
15591 : :
15592 : 287 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15593 : :
15594 : 287 : i_agginitval = PQfnumber(res, "agginitval");
15595 : 287 : i_aggminitval = PQfnumber(res, "aggminitval");
15596 : :
15597 : 287 : aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
15598 : 287 : aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
15599 : 287 : aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
15600 : 287 : aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
15601 : 287 : aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
15602 : 287 : aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
15603 : 287 : aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
15604 : 287 : aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
15605 : 287 : aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
15606 : 287 : aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
15607 : 287 : aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
15608 : 287 : aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
15609 : 287 : aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
15610 : 287 : aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
15611 : 287 : aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
15612 : 287 : aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
15613 : 287 : aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
15614 : 287 : aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
15615 : 287 : agginitval = PQgetvalue(res, 0, i_agginitval);
15616 : 287 : aggminitval = PQgetvalue(res, 0, i_aggminitval);
15617 : 287 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
15618 : 287 : prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport"));
15619 : :
15620 : : {
15621 : : char *funcargs;
15622 : : char *funciargs;
15623 : :
15624 : 287 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
15625 : 287 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
15626 : 287 : aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
15627 : 287 : aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
15628 : : }
15629 : :
15630 : 287 : aggsig_tag = format_aggregate_signature(agginfo, fout, false);
15631 : :
15632 : : /* identify default modify flag for aggkind (must match DefineAggregate) */
15633 [ + + ]: 287 : defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
15634 : : /* replace omitted flags for old versions */
15635 [ - + ]: 287 : if (aggfinalmodify == '0')
15636 : 0 : aggfinalmodify = defaultfinalmodify;
15637 [ - + ]: 287 : if (aggmfinalmodify == '0')
15638 : 0 : aggmfinalmodify = defaultfinalmodify;
15639 : :
15640 : : /* regproc and regtype output is already sufficiently quoted */
15641 : 287 : appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
15642 : : aggtransfn, aggtranstype);
15643 : :
15644 [ + + ]: 287 : if (strcmp(aggtransspace, "0") != 0)
15645 : : {
15646 : 5 : appendPQExpBuffer(details, ",\n SSPACE = %s",
15647 : : aggtransspace);
15648 : : }
15649 : :
15650 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_agginitval))
15651 : : {
15652 : 209 : appendPQExpBufferStr(details, ",\n INITCOND = ");
15653 : 209 : appendStringLiteralAH(details, agginitval, fout);
15654 : : }
15655 : :
15656 [ + + ]: 287 : if (strcmp(aggfinalfn, "-") != 0)
15657 : : {
15658 : 134 : appendPQExpBuffer(details, ",\n FINALFUNC = %s",
15659 : : aggfinalfn);
15660 [ + + ]: 134 : if (aggfinalextra)
15661 : 10 : appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
15662 [ + + ]: 134 : if (aggfinalmodify != defaultfinalmodify)
15663 : : {
15664 [ - + - - ]: 34 : switch (aggfinalmodify)
15665 : : {
15666 : 0 : case AGGMODIFY_READ_ONLY:
15667 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
15668 : 0 : break;
15669 : 34 : case AGGMODIFY_SHAREABLE:
15670 : 34 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
15671 : 34 : break;
15672 : 0 : case AGGMODIFY_READ_WRITE:
15673 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
15674 : 0 : break;
15675 : 0 : default:
15676 : 0 : pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
15677 : : agginfo->aggfn.dobj.name);
15678 : : break;
15679 : : }
15680 : : }
15681 : : }
15682 : :
15683 [ - + ]: 287 : if (strcmp(aggcombinefn, "-") != 0)
15684 : 0 : appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
15685 : :
15686 [ - + ]: 287 : if (strcmp(aggserialfn, "-") != 0)
15687 : 0 : appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
15688 : :
15689 [ - + ]: 287 : if (strcmp(aggdeserialfn, "-") != 0)
15690 : 0 : appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
15691 : :
15692 [ + + ]: 287 : if (strcmp(aggmtransfn, "-") != 0)
15693 : : {
15694 : 30 : appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
15695 : : aggmtransfn,
15696 : : aggminvtransfn,
15697 : : aggmtranstype);
15698 : : }
15699 : :
15700 [ - + ]: 287 : if (strcmp(aggmtransspace, "0") != 0)
15701 : : {
15702 : 0 : appendPQExpBuffer(details, ",\n MSSPACE = %s",
15703 : : aggmtransspace);
15704 : : }
15705 : :
15706 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_aggminitval))
15707 : : {
15708 : 10 : appendPQExpBufferStr(details, ",\n MINITCOND = ");
15709 : 10 : appendStringLiteralAH(details, aggminitval, fout);
15710 : : }
15711 : :
15712 [ - + ]: 287 : if (strcmp(aggmfinalfn, "-") != 0)
15713 : : {
15714 : 0 : appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
15715 : : aggmfinalfn);
15716 [ # # ]: 0 : if (aggmfinalextra)
15717 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
15718 [ # # ]: 0 : if (aggmfinalmodify != defaultfinalmodify)
15719 : : {
15720 [ # # # # ]: 0 : switch (aggmfinalmodify)
15721 : : {
15722 : 0 : case AGGMODIFY_READ_ONLY:
15723 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
15724 : 0 : break;
15725 : 0 : case AGGMODIFY_SHAREABLE:
15726 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
15727 : 0 : break;
15728 : 0 : case AGGMODIFY_READ_WRITE:
15729 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
15730 : 0 : break;
15731 : 0 : default:
15732 : 0 : pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
15733 : : agginfo->aggfn.dobj.name);
15734 : : break;
15735 : : }
15736 : : }
15737 : : }
15738 : :
15739 : 287 : aggsortconvop = getFormattedOperatorName(aggsortop);
15740 [ - + ]: 287 : if (aggsortconvop)
15741 : : {
15742 : 0 : appendPQExpBuffer(details, ",\n SORTOP = %s",
15743 : : aggsortconvop);
15744 : 0 : free(aggsortconvop);
15745 : : }
15746 : :
15747 [ + + ]: 287 : if (strcmp(prosupport, "-") != 0)
15748 : : {
15749 : 5 : appendPQExpBuffer(details, ",\n SUPPORT = %s", prosupport);
15750 : : }
15751 : :
15752 [ + + ]: 287 : if (aggkind == AGGKIND_HYPOTHETICAL)
15753 : 5 : appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
15754 : :
15755 [ + + ]: 287 : if (proparallel[0] != PROPARALLEL_UNSAFE)
15756 : : {
15757 [ + - ]: 5 : if (proparallel[0] == PROPARALLEL_SAFE)
15758 : 5 : appendPQExpBufferStr(details, ",\n PARALLEL = safe");
15759 [ # # ]: 0 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
15760 : 0 : appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
15761 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
15762 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
15763 : : agginfo->aggfn.dobj.name);
15764 : : }
15765 : :
15766 : 287 : appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
15767 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15768 : : aggsig);
15769 : :
15770 [ + - ]: 574 : appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
15771 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15772 : : aggfullsig ? aggfullsig : aggsig, details->data);
15773 : :
15774 [ + + ]: 287 : if (dopt->binary_upgrade)
15775 : 49 : binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
15776 : : "AGGREGATE", aggsig,
15777 : 49 : agginfo->aggfn.dobj.namespace->dobj.name);
15778 : :
15779 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
15780 : 270 : ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
15781 : 270 : agginfo->aggfn.dobj.dumpId,
15782 : 270 : ARCHIVE_OPTS(.tag = aggsig_tag,
15783 : : .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
15784 : : .owner = agginfo->aggfn.rolname,
15785 : : .description = "AGGREGATE",
15786 : : .section = SECTION_PRE_DATA,
15787 : : .createStmt = q->data,
15788 : : .dropStmt = delq->data));
15789 : :
15790 : : /* Dump Aggregate Comments */
15791 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
15792 : 10 : dumpComment(fout, "AGGREGATE", aggsig,
15793 : 10 : agginfo->aggfn.dobj.namespace->dobj.name,
15794 : 10 : agginfo->aggfn.rolname,
15795 : 10 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15796 : :
15797 [ - + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
15798 : 0 : dumpSecLabel(fout, "AGGREGATE", aggsig,
15799 : 0 : agginfo->aggfn.dobj.namespace->dobj.name,
15800 : 0 : agginfo->aggfn.rolname,
15801 : 0 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15802 : :
15803 : : /*
15804 : : * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
15805 : : * command look like a function's GRANT; in particular this affects the
15806 : : * syntax for zero-argument aggregates and ordered-set aggregates.
15807 : : */
15808 : 287 : free(aggsig);
15809 : :
15810 : 287 : aggsig = format_function_signature(fout, &agginfo->aggfn, true);
15811 : :
15812 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
15813 : 18 : dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
15814 : : "FUNCTION", aggsig, NULL,
15815 : 18 : agginfo->aggfn.dobj.namespace->dobj.name,
15816 : 18 : NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
15817 : :
15818 : 287 : free(aggsig);
15819 : 287 : free(aggfullsig);
15820 : 287 : free(aggsig_tag);
15821 : :
15822 : 287 : PQclear(res);
15823 : :
15824 : 287 : destroyPQExpBuffer(query);
15825 : 287 : destroyPQExpBuffer(q);
15826 : 287 : destroyPQExpBuffer(delq);
15827 : 287 : destroyPQExpBuffer(details);
15828 : : }
15829 : :
15830 : : /*
15831 : : * dumpTSParser
15832 : : * write out a single text search parser
15833 : : */
15834 : : static void
15835 : 44 : dumpTSParser(Archive *fout, const TSParserInfo *prsinfo)
15836 : : {
15837 : 44 : DumpOptions *dopt = fout->dopt;
15838 : : PQExpBuffer q;
15839 : : PQExpBuffer delq;
15840 : : char *qprsname;
15841 : :
15842 : : /* Do nothing if not dumping schema */
15843 [ + + ]: 44 : if (!dopt->dumpSchema)
15844 : 7 : return;
15845 : :
15846 : 37 : q = createPQExpBuffer();
15847 : 37 : delq = createPQExpBuffer();
15848 : :
15849 : 37 : qprsname = pg_strdup(fmtId(prsinfo->dobj.name));
15850 : :
15851 : 37 : appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n",
15852 : 37 : fmtQualifiedDumpable(prsinfo));
15853 : :
15854 : 37 : appendPQExpBuffer(q, " START = %s,\n",
15855 : 37 : convertTSFunction(fout, prsinfo->prsstart));
15856 : 37 : appendPQExpBuffer(q, " GETTOKEN = %s,\n",
15857 : 37 : convertTSFunction(fout, prsinfo->prstoken));
15858 : 37 : appendPQExpBuffer(q, " END = %s,\n",
15859 : 37 : convertTSFunction(fout, prsinfo->prsend));
15860 [ + + ]: 37 : if (prsinfo->prsheadline != InvalidOid)
15861 : 3 : appendPQExpBuffer(q, " HEADLINE = %s,\n",
15862 : 3 : convertTSFunction(fout, prsinfo->prsheadline));
15863 : 37 : appendPQExpBuffer(q, " LEXTYPES = %s );\n",
15864 : 37 : convertTSFunction(fout, prsinfo->prslextype));
15865 : :
15866 : 37 : appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s;\n",
15867 : 37 : fmtQualifiedDumpable(prsinfo));
15868 : :
15869 [ + + ]: 37 : if (dopt->binary_upgrade)
15870 : 1 : binary_upgrade_extension_member(q, &prsinfo->dobj,
15871 : : "TEXT SEARCH PARSER", qprsname,
15872 : 1 : prsinfo->dobj.namespace->dobj.name);
15873 : :
15874 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15875 : 37 : ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId,
15876 : 37 : ARCHIVE_OPTS(.tag = prsinfo->dobj.name,
15877 : : .namespace = prsinfo->dobj.namespace->dobj.name,
15878 : : .description = "TEXT SEARCH PARSER",
15879 : : .section = SECTION_PRE_DATA,
15880 : : .createStmt = q->data,
15881 : : .dropStmt = delq->data));
15882 : :
15883 : : /* Dump Parser Comments */
15884 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15885 : 37 : dumpComment(fout, "TEXT SEARCH PARSER", qprsname,
15886 : 37 : prsinfo->dobj.namespace->dobj.name, "",
15887 : 37 : prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId);
15888 : :
15889 : 37 : destroyPQExpBuffer(q);
15890 : 37 : destroyPQExpBuffer(delq);
15891 : 37 : pg_free(qprsname);
15892 : : }
15893 : :
15894 : : /*
15895 : : * dumpTSDictionary
15896 : : * write out a single text search dictionary
15897 : : */
15898 : : static void
15899 : 182 : dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo)
15900 : : {
15901 : 182 : DumpOptions *dopt = fout->dopt;
15902 : : PQExpBuffer q;
15903 : : PQExpBuffer delq;
15904 : : PQExpBuffer query;
15905 : : char *qdictname;
15906 : : PGresult *res;
15907 : : char *nspname;
15908 : : char *tmplname;
15909 : :
15910 : : /* Do nothing if not dumping schema */
15911 [ + + ]: 182 : if (!dopt->dumpSchema)
15912 : 7 : return;
15913 : :
15914 : 175 : q = createPQExpBuffer();
15915 : 175 : delq = createPQExpBuffer();
15916 : 175 : query = createPQExpBuffer();
15917 : :
15918 : 175 : qdictname = pg_strdup(fmtId(dictinfo->dobj.name));
15919 : :
15920 : : /* Fetch name and namespace of the dictionary's template */
15921 : 175 : appendPQExpBuffer(query, "SELECT nspname, tmplname "
15922 : : "FROM pg_ts_template p, pg_namespace n "
15923 : : "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
15924 : 175 : dictinfo->dicttemplate);
15925 : 175 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15926 : 175 : nspname = PQgetvalue(res, 0, 0);
15927 : 175 : tmplname = PQgetvalue(res, 0, 1);
15928 : :
15929 : 175 : appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n",
15930 : 175 : fmtQualifiedDumpable(dictinfo));
15931 : :
15932 : 175 : appendPQExpBufferStr(q, " TEMPLATE = ");
15933 : 175 : appendPQExpBuffer(q, "%s.", fmtId(nspname));
15934 : 175 : appendPQExpBufferStr(q, fmtId(tmplname));
15935 : :
15936 : 175 : PQclear(res);
15937 : :
15938 : : /* the dictinitoption can be dumped straight into the command */
15939 [ + + ]: 175 : if (dictinfo->dictinitoption)
15940 : 138 : appendPQExpBuffer(q, ",\n %s", dictinfo->dictinitoption);
15941 : :
15942 : 175 : appendPQExpBufferStr(q, " );\n");
15943 : :
15944 : 175 : appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s;\n",
15945 : 175 : fmtQualifiedDumpable(dictinfo));
15946 : :
15947 [ + + ]: 175 : if (dopt->binary_upgrade)
15948 : 10 : binary_upgrade_extension_member(q, &dictinfo->dobj,
15949 : : "TEXT SEARCH DICTIONARY", qdictname,
15950 : 10 : dictinfo->dobj.namespace->dobj.name);
15951 : :
15952 [ + - ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15953 : 175 : ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId,
15954 : 175 : ARCHIVE_OPTS(.tag = dictinfo->dobj.name,
15955 : : .namespace = dictinfo->dobj.namespace->dobj.name,
15956 : : .owner = dictinfo->rolname,
15957 : : .description = "TEXT SEARCH DICTIONARY",
15958 : : .section = SECTION_PRE_DATA,
15959 : : .createStmt = q->data,
15960 : : .dropStmt = delq->data));
15961 : :
15962 : : /* Dump Dictionary Comments */
15963 [ + + ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15964 : 130 : dumpComment(fout, "TEXT SEARCH DICTIONARY", qdictname,
15965 : 130 : dictinfo->dobj.namespace->dobj.name, dictinfo->rolname,
15966 : 130 : dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId);
15967 : :
15968 : 175 : destroyPQExpBuffer(q);
15969 : 175 : destroyPQExpBuffer(delq);
15970 : 175 : destroyPQExpBuffer(query);
15971 : 175 : pg_free(qdictname);
15972 : : }
15973 : :
15974 : : /*
15975 : : * dumpTSTemplate
15976 : : * write out a single text search template
15977 : : */
15978 : : static void
15979 : 56 : dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo)
15980 : : {
15981 : 56 : DumpOptions *dopt = fout->dopt;
15982 : : PQExpBuffer q;
15983 : : PQExpBuffer delq;
15984 : : char *qtmplname;
15985 : :
15986 : : /* Do nothing if not dumping schema */
15987 [ + + ]: 56 : if (!dopt->dumpSchema)
15988 : 7 : return;
15989 : :
15990 : 49 : q = createPQExpBuffer();
15991 : 49 : delq = createPQExpBuffer();
15992 : :
15993 : 49 : qtmplname = pg_strdup(fmtId(tmplinfo->dobj.name));
15994 : :
15995 : 49 : appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n",
15996 : 49 : fmtQualifiedDumpable(tmplinfo));
15997 : :
15998 [ + + ]: 49 : if (tmplinfo->tmplinit != InvalidOid)
15999 : 15 : appendPQExpBuffer(q, " INIT = %s,\n",
16000 : 15 : convertTSFunction(fout, tmplinfo->tmplinit));
16001 : 49 : appendPQExpBuffer(q, " LEXIZE = %s );\n",
16002 : 49 : convertTSFunction(fout, tmplinfo->tmpllexize));
16003 : :
16004 : 49 : appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s;\n",
16005 : 49 : fmtQualifiedDumpable(tmplinfo));
16006 : :
16007 [ + + ]: 49 : if (dopt->binary_upgrade)
16008 : 1 : binary_upgrade_extension_member(q, &tmplinfo->dobj,
16009 : : "TEXT SEARCH TEMPLATE", qtmplname,
16010 : 1 : tmplinfo->dobj.namespace->dobj.name);
16011 : :
16012 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16013 : 49 : ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId,
16014 : 49 : ARCHIVE_OPTS(.tag = tmplinfo->dobj.name,
16015 : : .namespace = tmplinfo->dobj.namespace->dobj.name,
16016 : : .description = "TEXT SEARCH TEMPLATE",
16017 : : .section = SECTION_PRE_DATA,
16018 : : .createStmt = q->data,
16019 : : .dropStmt = delq->data));
16020 : :
16021 : : /* Dump Template Comments */
16022 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16023 : 49 : dumpComment(fout, "TEXT SEARCH TEMPLATE", qtmplname,
16024 : 49 : tmplinfo->dobj.namespace->dobj.name, "",
16025 : 49 : tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId);
16026 : :
16027 : 49 : destroyPQExpBuffer(q);
16028 : 49 : destroyPQExpBuffer(delq);
16029 : 49 : pg_free(qtmplname);
16030 : : }
16031 : :
16032 : : /*
16033 : : * dumpTSConfig
16034 : : * write out a single text search configuration
16035 : : */
16036 : : static void
16037 : 157 : dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo)
16038 : : {
16039 : 157 : DumpOptions *dopt = fout->dopt;
16040 : : PQExpBuffer q;
16041 : : PQExpBuffer delq;
16042 : : PQExpBuffer query;
16043 : : char *qcfgname;
16044 : : PGresult *res;
16045 : : char *nspname;
16046 : : char *prsname;
16047 : : int ntups,
16048 : : i;
16049 : : int i_tokenname;
16050 : : int i_dictname;
16051 : :
16052 : : /* Do nothing if not dumping schema */
16053 [ + + ]: 157 : if (!dopt->dumpSchema)
16054 : 7 : return;
16055 : :
16056 : 150 : q = createPQExpBuffer();
16057 : 150 : delq = createPQExpBuffer();
16058 : 150 : query = createPQExpBuffer();
16059 : :
16060 : 150 : qcfgname = pg_strdup(fmtId(cfginfo->dobj.name));
16061 : :
16062 : : /* Fetch name and namespace of the config's parser */
16063 : 150 : appendPQExpBuffer(query, "SELECT nspname, prsname "
16064 : : "FROM pg_ts_parser p, pg_namespace n "
16065 : : "WHERE p.oid = '%u' AND n.oid = prsnamespace",
16066 : 150 : cfginfo->cfgparser);
16067 : 150 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
16068 : 150 : nspname = PQgetvalue(res, 0, 0);
16069 : 150 : prsname = PQgetvalue(res, 0, 1);
16070 : :
16071 : 150 : appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n",
16072 : 150 : fmtQualifiedDumpable(cfginfo));
16073 : :
16074 : 150 : appendPQExpBuffer(q, " PARSER = %s.", fmtId(nspname));
16075 : 150 : appendPQExpBuffer(q, "%s );\n", fmtId(prsname));
16076 : :
16077 : 150 : PQclear(res);
16078 : :
16079 : 150 : resetPQExpBuffer(query);
16080 : 150 : appendPQExpBuffer(query,
16081 : : "SELECT\n"
16082 : : " ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n"
16083 : : " WHERE t.tokid = m.maptokentype ) AS tokenname,\n"
16084 : : " m.mapdict::pg_catalog.regdictionary AS dictname\n"
16085 : : "FROM pg_catalog.pg_ts_config_map AS m\n"
16086 : : "WHERE m.mapcfg = '%u'\n"
16087 : : "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
16088 : 150 : cfginfo->cfgparser, cfginfo->dobj.catId.oid);
16089 : :
16090 : 150 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16091 : 150 : ntups = PQntuples(res);
16092 : :
16093 : 150 : i_tokenname = PQfnumber(res, "tokenname");
16094 : 150 : i_dictname = PQfnumber(res, "dictname");
16095 : :
16096 [ + + ]: 3135 : for (i = 0; i < ntups; i++)
16097 : : {
16098 : 2985 : char *tokenname = PQgetvalue(res, i, i_tokenname);
16099 : 2985 : char *dictname = PQgetvalue(res, i, i_dictname);
16100 : :
16101 [ + + ]: 2985 : if (i == 0 ||
16102 [ + + ]: 2835 : strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0)
16103 : : {
16104 : : /* starting a new token type, so start a new command */
16105 [ + + ]: 2850 : if (i > 0)
16106 : 2700 : appendPQExpBufferStr(q, ";\n");
16107 : 2850 : appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n",
16108 : 2850 : fmtQualifiedDumpable(cfginfo));
16109 : : /* tokenname needs quoting, dictname does NOT */
16110 : 2850 : appendPQExpBuffer(q, " ADD MAPPING FOR %s WITH %s",
16111 : : fmtId(tokenname), dictname);
16112 : : }
16113 : : else
16114 : 135 : appendPQExpBuffer(q, ", %s", dictname);
16115 : : }
16116 : :
16117 [ + - ]: 150 : if (ntups > 0)
16118 : 150 : appendPQExpBufferStr(q, ";\n");
16119 : :
16120 : 150 : PQclear(res);
16121 : :
16122 : 150 : appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s;\n",
16123 : 150 : fmtQualifiedDumpable(cfginfo));
16124 : :
16125 [ + + ]: 150 : if (dopt->binary_upgrade)
16126 : 5 : binary_upgrade_extension_member(q, &cfginfo->dobj,
16127 : : "TEXT SEARCH CONFIGURATION", qcfgname,
16128 : 5 : cfginfo->dobj.namespace->dobj.name);
16129 : :
16130 [ + - ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16131 : 150 : ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId,
16132 : 150 : ARCHIVE_OPTS(.tag = cfginfo->dobj.name,
16133 : : .namespace = cfginfo->dobj.namespace->dobj.name,
16134 : : .owner = cfginfo->rolname,
16135 : : .description = "TEXT SEARCH CONFIGURATION",
16136 : : .section = SECTION_PRE_DATA,
16137 : : .createStmt = q->data,
16138 : : .dropStmt = delq->data));
16139 : :
16140 : : /* Dump Configuration Comments */
16141 [ + + ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16142 : 130 : dumpComment(fout, "TEXT SEARCH CONFIGURATION", qcfgname,
16143 : 130 : cfginfo->dobj.namespace->dobj.name, cfginfo->rolname,
16144 : 130 : cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId);
16145 : :
16146 : 150 : destroyPQExpBuffer(q);
16147 : 150 : destroyPQExpBuffer(delq);
16148 : 150 : destroyPQExpBuffer(query);
16149 : 150 : pg_free(qcfgname);
16150 : : }
16151 : :
16152 : : /*
16153 : : * dumpForeignDataWrapper
16154 : : * write out a single foreign-data wrapper definition
16155 : : */
16156 : : static void
16157 : 55 : dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo)
16158 : : {
16159 : 55 : DumpOptions *dopt = fout->dopt;
16160 : : PQExpBuffer q;
16161 : : PQExpBuffer delq;
16162 : : char *qfdwname;
16163 : :
16164 : : /* Do nothing if not dumping schema */
16165 [ + + ]: 55 : if (!dopt->dumpSchema)
16166 : 7 : return;
16167 : :
16168 : 48 : q = createPQExpBuffer();
16169 : 48 : delq = createPQExpBuffer();
16170 : :
16171 : 48 : qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name));
16172 : :
16173 : 48 : appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s",
16174 : : qfdwname);
16175 : :
16176 [ - + ]: 48 : if (strcmp(fdwinfo->fdwhandler, "-") != 0)
16177 : 0 : appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler);
16178 : :
16179 [ - + ]: 48 : if (strcmp(fdwinfo->fdwvalidator, "-") != 0)
16180 : 0 : appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator);
16181 : :
16182 [ - + ]: 48 : if (strcmp(fdwinfo->fdwconnection, "-") != 0)
16183 : 0 : appendPQExpBuffer(q, " CONNECTION %s", fdwinfo->fdwconnection);
16184 : :
16185 [ - + ]: 48 : if (strlen(fdwinfo->fdwoptions) > 0)
16186 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", fdwinfo->fdwoptions);
16187 : :
16188 : 48 : appendPQExpBufferStr(q, ";\n");
16189 : :
16190 : 48 : appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n",
16191 : : qfdwname);
16192 : :
16193 [ + + ]: 48 : if (dopt->binary_upgrade)
16194 : 3 : binary_upgrade_extension_member(q, &fdwinfo->dobj,
16195 : : "FOREIGN DATA WRAPPER", qfdwname,
16196 : : NULL);
16197 : :
16198 [ + - ]: 48 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16199 : 48 : ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
16200 : 48 : ARCHIVE_OPTS(.tag = fdwinfo->dobj.name,
16201 : : .owner = fdwinfo->rolname,
16202 : : .description = "FOREIGN DATA WRAPPER",
16203 : : .section = SECTION_PRE_DATA,
16204 : : .createStmt = q->data,
16205 : : .dropStmt = delq->data));
16206 : :
16207 : : /* Dump Foreign Data Wrapper Comments */
16208 [ - + ]: 48 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16209 : 0 : dumpComment(fout, "FOREIGN DATA WRAPPER", qfdwname,
16210 : 0 : NULL, fdwinfo->rolname,
16211 : 0 : fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
16212 : :
16213 : : /* Handle the ACL */
16214 [ + + ]: 48 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL)
16215 : 33 : dumpACL(fout, fdwinfo->dobj.dumpId, InvalidDumpId,
16216 : : "FOREIGN DATA WRAPPER", qfdwname, NULL, NULL,
16217 : 33 : NULL, fdwinfo->rolname, &fdwinfo->dacl);
16218 : :
16219 : 48 : pg_free(qfdwname);
16220 : :
16221 : 48 : destroyPQExpBuffer(q);
16222 : 48 : destroyPQExpBuffer(delq);
16223 : : }
16224 : :
16225 : : /*
16226 : : * dumpForeignServer
16227 : : * write out a foreign server definition
16228 : : */
16229 : : static void
16230 : 59 : dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo)
16231 : : {
16232 : 59 : DumpOptions *dopt = fout->dopt;
16233 : : PQExpBuffer q;
16234 : : PQExpBuffer delq;
16235 : : PQExpBuffer query;
16236 : : PGresult *res;
16237 : : char *qsrvname;
16238 : : char *fdwname;
16239 : :
16240 : : /* Do nothing if not dumping schema */
16241 [ + + ]: 59 : if (!dopt->dumpSchema)
16242 : 9 : return;
16243 : :
16244 : 50 : q = createPQExpBuffer();
16245 : 50 : delq = createPQExpBuffer();
16246 : 50 : query = createPQExpBuffer();
16247 : :
16248 : 50 : qsrvname = pg_strdup(fmtId(srvinfo->dobj.name));
16249 : :
16250 : : /* look up the foreign-data wrapper */
16251 : 50 : appendPQExpBuffer(query, "SELECT fdwname "
16252 : : "FROM pg_foreign_data_wrapper w "
16253 : : "WHERE w.oid = '%u'",
16254 : 50 : srvinfo->srvfdw);
16255 : 50 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
16256 : 50 : fdwname = PQgetvalue(res, 0, 0);
16257 : :
16258 : 50 : appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
16259 [ + - - + ]: 50 : if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0)
16260 : : {
16261 : 0 : appendPQExpBufferStr(q, " TYPE ");
16262 : 0 : appendStringLiteralAH(q, srvinfo->srvtype, fout);
16263 : : }
16264 [ + - - + ]: 50 : if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0)
16265 : : {
16266 : 0 : appendPQExpBufferStr(q, " VERSION ");
16267 : 0 : appendStringLiteralAH(q, srvinfo->srvversion, fout);
16268 : : }
16269 : :
16270 : 50 : appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER ");
16271 : 50 : appendPQExpBufferStr(q, fmtId(fdwname));
16272 : :
16273 [ + - - + ]: 50 : if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0)
16274 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", srvinfo->srvoptions);
16275 : :
16276 : 50 : appendPQExpBufferStr(q, ";\n");
16277 : :
16278 : 50 : appendPQExpBuffer(delq, "DROP SERVER %s;\n",
16279 : : qsrvname);
16280 : :
16281 [ + + ]: 50 : if (dopt->binary_upgrade)
16282 : 3 : binary_upgrade_extension_member(q, &srvinfo->dobj,
16283 : : "SERVER", qsrvname, NULL);
16284 : :
16285 [ + - ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16286 : 50 : ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
16287 : 50 : ARCHIVE_OPTS(.tag = srvinfo->dobj.name,
16288 : : .owner = srvinfo->rolname,
16289 : : .description = "SERVER",
16290 : : .section = SECTION_PRE_DATA,
16291 : : .createStmt = q->data,
16292 : : .dropStmt = delq->data));
16293 : :
16294 : : /* Dump Foreign Server Comments */
16295 [ - + ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16296 : 0 : dumpComment(fout, "SERVER", qsrvname,
16297 : 0 : NULL, srvinfo->rolname,
16298 : 0 : srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
16299 : :
16300 : : /* Handle the ACL */
16301 [ + + ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL)
16302 : 33 : dumpACL(fout, srvinfo->dobj.dumpId, InvalidDumpId,
16303 : : "FOREIGN SERVER", qsrvname, NULL, NULL,
16304 : 33 : NULL, srvinfo->rolname, &srvinfo->dacl);
16305 : :
16306 : : /* Dump user mappings */
16307 [ + - ]: 50 : if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP)
16308 : 50 : dumpUserMappings(fout,
16309 : 50 : srvinfo->dobj.name, NULL,
16310 : 50 : srvinfo->rolname,
16311 : 50 : srvinfo->dobj.catId, srvinfo->dobj.dumpId);
16312 : :
16313 : 50 : PQclear(res);
16314 : :
16315 : 50 : pg_free(qsrvname);
16316 : :
16317 : 50 : destroyPQExpBuffer(q);
16318 : 50 : destroyPQExpBuffer(delq);
16319 : 50 : destroyPQExpBuffer(query);
16320 : : }
16321 : :
16322 : : /*
16323 : : * dumpUserMappings
16324 : : *
16325 : : * This routine is used to dump any user mappings associated with the
16326 : : * server handed to this routine. Should be called after ArchiveEntry()
16327 : : * for the server.
16328 : : */
16329 : : static void
16330 : 50 : dumpUserMappings(Archive *fout,
16331 : : const char *servername, const char *namespace,
16332 : : const char *owner,
16333 : : CatalogId catalogId, DumpId dumpId)
16334 : : {
16335 : : PQExpBuffer q;
16336 : : PQExpBuffer delq;
16337 : : PQExpBuffer query;
16338 : : PQExpBuffer tag;
16339 : : PGresult *res;
16340 : : int ntups;
16341 : : int i_usename;
16342 : : int i_umoptions;
16343 : : int i;
16344 : :
16345 : 50 : q = createPQExpBuffer();
16346 : 50 : tag = createPQExpBuffer();
16347 : 50 : delq = createPQExpBuffer();
16348 : 50 : query = createPQExpBuffer();
16349 : :
16350 : : /*
16351 : : * We read from the publicly accessible view pg_user_mappings, so as not
16352 : : * to fail if run by a non-superuser. Note that the view will show
16353 : : * umoptions as null if the user hasn't got privileges for the associated
16354 : : * server; this means that pg_dump will dump such a mapping, but with no
16355 : : * OPTIONS clause. A possible alternative is to skip such mappings
16356 : : * altogether, but it's not clear that that's an improvement.
16357 : : */
16358 : 50 : appendPQExpBuffer(query,
16359 : : "SELECT usename, "
16360 : : "array_to_string(ARRAY("
16361 : : "SELECT quote_ident(option_name) || ' ' || "
16362 : : "quote_literal(option_value) "
16363 : : "FROM pg_options_to_table(umoptions) "
16364 : : "ORDER BY option_name"
16365 : : "), E',\n ') AS umoptions "
16366 : : "FROM pg_user_mappings "
16367 : : "WHERE srvid = '%u' "
16368 : : "ORDER BY usename",
16369 : : catalogId.oid);
16370 : :
16371 : 50 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16372 : :
16373 : 50 : ntups = PQntuples(res);
16374 : 50 : i_usename = PQfnumber(res, "usename");
16375 : 50 : i_umoptions = PQfnumber(res, "umoptions");
16376 : :
16377 [ + + ]: 83 : for (i = 0; i < ntups; i++)
16378 : : {
16379 : : char *usename;
16380 : : char *umoptions;
16381 : :
16382 : 33 : usename = PQgetvalue(res, i, i_usename);
16383 : 33 : umoptions = PQgetvalue(res, i, i_umoptions);
16384 : :
16385 : 33 : resetPQExpBuffer(q);
16386 : 33 : appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
16387 : 33 : appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
16388 : :
16389 [ + - - + ]: 33 : if (umoptions && strlen(umoptions) > 0)
16390 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", umoptions);
16391 : :
16392 : 33 : appendPQExpBufferStr(q, ";\n");
16393 : :
16394 : 33 : resetPQExpBuffer(delq);
16395 : 33 : appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
16396 : 33 : appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
16397 : :
16398 : 33 : resetPQExpBuffer(tag);
16399 : 33 : appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
16400 : : usename, servername);
16401 : :
16402 : 33 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16403 : 33 : ARCHIVE_OPTS(.tag = tag->data,
16404 : : .namespace = namespace,
16405 : : .owner = owner,
16406 : : .description = "USER MAPPING",
16407 : : .section = SECTION_PRE_DATA,
16408 : : .createStmt = q->data,
16409 : : .dropStmt = delq->data));
16410 : : }
16411 : :
16412 : 50 : PQclear(res);
16413 : :
16414 : 50 : destroyPQExpBuffer(query);
16415 : 50 : destroyPQExpBuffer(delq);
16416 : 50 : destroyPQExpBuffer(tag);
16417 : 50 : destroyPQExpBuffer(q);
16418 : 50 : }
16419 : :
16420 : : /*
16421 : : * Write out default privileges information
16422 : : */
16423 : : static void
16424 : 170 : dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo)
16425 : : {
16426 : 170 : DumpOptions *dopt = fout->dopt;
16427 : : PQExpBuffer q;
16428 : : PQExpBuffer tag;
16429 : : const char *type;
16430 : :
16431 : : /* Do nothing if not dumping schema, or if we're skipping ACLs */
16432 [ + + + + ]: 170 : if (!dopt->dumpSchema || dopt->aclsSkip)
16433 : 30 : return;
16434 : :
16435 : 140 : q = createPQExpBuffer();
16436 : 140 : tag = createPQExpBuffer();
16437 : :
16438 [ + - + + : 140 : switch (daclinfo->defaclobjtype)
- - - ]
16439 : : {
16440 : 65 : case DEFACLOBJ_RELATION:
16441 : 65 : type = "TABLES";
16442 : 65 : break;
16443 : 0 : case DEFACLOBJ_SEQUENCE:
16444 : 0 : type = "SEQUENCES";
16445 : 0 : break;
16446 : 65 : case DEFACLOBJ_FUNCTION:
16447 : 65 : type = "FUNCTIONS";
16448 : 65 : break;
16449 : 10 : case DEFACLOBJ_TYPE:
16450 : 10 : type = "TYPES";
16451 : 10 : break;
16452 : 0 : case DEFACLOBJ_NAMESPACE:
16453 : 0 : type = "SCHEMAS";
16454 : 0 : break;
16455 : 0 : case DEFACLOBJ_LARGEOBJECT:
16456 : 0 : type = "LARGE OBJECTS";
16457 : 0 : break;
16458 : 0 : default:
16459 : : /* shouldn't get here */
16460 : 0 : pg_fatal("unrecognized object type in default privileges: %d",
16461 : : (int) daclinfo->defaclobjtype);
16462 : : type = ""; /* keep compiler quiet */
16463 : : }
16464 : :
16465 : 140 : appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
16466 : :
16467 : : /* build the actual command(s) for this tuple */
16468 [ - + ]: 140 : if (!buildDefaultACLCommands(type,
16469 : 140 : daclinfo->dobj.namespace != NULL ?
16470 : 66 : daclinfo->dobj.namespace->dobj.name : NULL,
16471 : 140 : daclinfo->dacl.acl,
16472 : 140 : daclinfo->dacl.acldefault,
16473 [ + + ]: 140 : daclinfo->defaclrole,
16474 : : fout->remoteVersion,
16475 : : q))
16476 : 0 : pg_fatal("could not parse default ACL list (%s)",
16477 : : daclinfo->dacl.acl);
16478 : :
16479 [ + - ]: 140 : if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
16480 : 140 : ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
16481 [ + + ]: 140 : ARCHIVE_OPTS(.tag = tag->data,
16482 : : .namespace = daclinfo->dobj.namespace ?
16483 : : daclinfo->dobj.namespace->dobj.name : NULL,
16484 : : .owner = daclinfo->defaclrole,
16485 : : .description = "DEFAULT ACL",
16486 : : .section = SECTION_POST_DATA,
16487 : : .createStmt = q->data));
16488 : :
16489 : 140 : destroyPQExpBuffer(tag);
16490 : 140 : destroyPQExpBuffer(q);
16491 : : }
16492 : :
16493 : : /*----------
16494 : : * Write out grant/revoke information
16495 : : *
16496 : : * 'objDumpId' is the dump ID of the underlying object.
16497 : : * 'altDumpId' can be a second dumpId that the ACL entry must also depend on,
16498 : : * or InvalidDumpId if there is no need for a second dependency.
16499 : : * 'type' must be one of
16500 : : * TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
16501 : : * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT.
16502 : : * 'name' is the formatted name of the object. Must be quoted etc. already.
16503 : : * 'subname' is the formatted name of the sub-object, if any. Must be quoted.
16504 : : * (Currently we assume that subname is only provided for table columns.)
16505 : : * 'nspname' is the namespace the object is in (NULL if none).
16506 : : * 'tag' is the tag to use for the ACL TOC entry; typically, this is NULL
16507 : : * to use the default for the object type.
16508 : : * 'owner' is the owner, NULL if there is no owner (for languages).
16509 : : * 'dacl' is the DumpableAcl struct for the object.
16510 : : *
16511 : : * Returns the dump ID assigned to the ACL TocEntry, or InvalidDumpId if
16512 : : * no ACL entry was created.
16513 : : *----------
16514 : : */
16515 : : static DumpId
16516 : 35421 : dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
16517 : : const char *type, const char *name, const char *subname,
16518 : : const char *nspname, const char *tag, const char *owner,
16519 : : const DumpableAcl *dacl)
16520 : : {
16521 : 35421 : DumpId aclDumpId = InvalidDumpId;
16522 : 35421 : DumpOptions *dopt = fout->dopt;
16523 : 35421 : const char *acls = dacl->acl;
16524 : 35421 : const char *acldefault = dacl->acldefault;
16525 : 35421 : char privtype = dacl->privtype;
16526 : 35421 : const char *initprivs = dacl->initprivs;
16527 : : const char *baseacls;
16528 : : PQExpBuffer sql;
16529 : :
16530 : : /* Do nothing if ACL dump is not enabled */
16531 [ + + ]: 35421 : if (dopt->aclsSkip)
16532 : 338 : return InvalidDumpId;
16533 : :
16534 : : /* --data-only skips ACLs *except* large object ACLs */
16535 [ + + + + ]: 35083 : if (!dopt->dumpSchema && strcmp(type, "LARGE OBJECT") != 0)
16536 : 1 : return InvalidDumpId;
16537 : :
16538 : 35082 : sql = createPQExpBuffer();
16539 : :
16540 : : /*
16541 : : * In binary upgrade mode, we don't run an extension's script but instead
16542 : : * dump out the objects independently and then recreate them. To preserve
16543 : : * any initial privileges which were set on extension objects, we need to
16544 : : * compute the set of GRANT and REVOKE commands necessary to get from the
16545 : : * default privileges of an object to its initial privileges as recorded
16546 : : * in pg_init_privs.
16547 : : *
16548 : : * At restore time, we apply these commands after having called
16549 : : * binary_upgrade_set_record_init_privs(true). That tells the backend to
16550 : : * copy the results into pg_init_privs. This is how we preserve the
16551 : : * contents of that catalog across binary upgrades.
16552 : : */
16553 [ + + + + : 35082 : if (dopt->binary_upgrade && privtype == 'e' &&
+ - ]
16554 [ + - ]: 13 : initprivs && *initprivs != '\0')
16555 : : {
16556 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
16557 [ - + ]: 13 : if (!buildACLCommands(name, subname, nspname, type,
16558 : : initprivs, acldefault, owner,
16559 : : "", fout->remoteVersion, sql))
16560 : 0 : pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
16561 : : initprivs, acldefault, name, type);
16562 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
16563 : : }
16564 : :
16565 : : /*
16566 : : * Now figure the GRANT and REVOKE commands needed to get to the object's
16567 : : * actual current ACL, starting from the initprivs if given, else from the
16568 : : * object-type-specific default. Also, while buildACLCommands will assume
16569 : : * that a NULL/empty acls string means it needn't do anything, what that
16570 : : * actually represents is the object-type-specific default; so we need to
16571 : : * substitute the acldefault string to get the right results in that case.
16572 : : */
16573 [ + + + + ]: 35082 : if (initprivs && *initprivs != '\0')
16574 : : {
16575 : 33222 : baseacls = initprivs;
16576 [ + - + + ]: 33222 : if (acls == NULL || *acls == '\0')
16577 : 17 : acls = acldefault;
16578 : : }
16579 : : else
16580 : 1860 : baseacls = acldefault;
16581 : :
16582 [ - + ]: 35082 : if (!buildACLCommands(name, subname, nspname, type,
16583 : : acls, baseacls, owner,
16584 : : "", fout->remoteVersion, sql))
16585 : 0 : pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
16586 : : acls, baseacls, name, type);
16587 : :
16588 [ + + ]: 35082 : if (sql->len > 0)
16589 : : {
16590 : 1907 : PQExpBuffer tagbuf = createPQExpBuffer();
16591 : : DumpId aclDeps[2];
16592 : 1907 : int nDeps = 0;
16593 : :
16594 [ - + ]: 1907 : if (tag)
16595 : 0 : appendPQExpBufferStr(tagbuf, tag);
16596 [ + + ]: 1907 : else if (subname)
16597 : 1111 : appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname);
16598 : : else
16599 : 796 : appendPQExpBuffer(tagbuf, "%s %s", type, name);
16600 : :
16601 : 1907 : aclDeps[nDeps++] = objDumpId;
16602 [ + + ]: 1907 : if (altDumpId != InvalidDumpId)
16603 : 1025 : aclDeps[nDeps++] = altDumpId;
16604 : :
16605 : 1907 : aclDumpId = createDumpId();
16606 : :
16607 : 1907 : ArchiveEntry(fout, nilCatalogId, aclDumpId,
16608 : 1907 : ARCHIVE_OPTS(.tag = tagbuf->data,
16609 : : .namespace = nspname,
16610 : : .owner = owner,
16611 : : .description = "ACL",
16612 : : .section = SECTION_NONE,
16613 : : .createStmt = sql->data,
16614 : : .deps = aclDeps,
16615 : : .nDeps = nDeps));
16616 : :
16617 : 1907 : destroyPQExpBuffer(tagbuf);
16618 : : }
16619 : :
16620 : 35082 : destroyPQExpBuffer(sql);
16621 : :
16622 : 35082 : return aclDumpId;
16623 : : }
16624 : :
16625 : : /*
16626 : : * dumpSecLabel
16627 : : *
16628 : : * This routine is used to dump any security labels associated with the
16629 : : * object handed to this routine. The routine takes the object type
16630 : : * and object name (ready to print, except for schema decoration), plus
16631 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
16632 : : * plus catalog ID and subid which are the lookup key for pg_seclabel,
16633 : : * plus the dump ID for the object (for setting a dependency).
16634 : : * If a matching pg_seclabel entry is found, it is dumped.
16635 : : *
16636 : : * Note: although this routine takes a dumpId for dependency purposes,
16637 : : * that purpose is just to mark the dependency in the emitted dump file
16638 : : * for possible future use by pg_restore. We do NOT use it for determining
16639 : : * ordering of the label in the dump file, because this routine is called
16640 : : * after dependency sorting occurs. This routine should be called just after
16641 : : * calling ArchiveEntry() for the specified object.
16642 : : */
16643 : : static void
16644 : 10 : dumpSecLabel(Archive *fout, const char *type, const char *name,
16645 : : const char *namespace, const char *owner,
16646 : : CatalogId catalogId, int subid, DumpId dumpId)
16647 : : {
16648 : 10 : DumpOptions *dopt = fout->dopt;
16649 : : SecLabelItem *labels;
16650 : : int nlabels;
16651 : : int i;
16652 : : PQExpBuffer query;
16653 : :
16654 : : /* do nothing, if --no-security-labels is supplied */
16655 [ - + ]: 10 : if (dopt->no_security_labels)
16656 : 0 : return;
16657 : :
16658 : : /*
16659 : : * Security labels are schema not data ... except large object labels are
16660 : : * data
16661 : : */
16662 [ - + ]: 10 : if (strcmp(type, "LARGE OBJECT") != 0)
16663 : : {
16664 [ # # ]: 0 : if (!dopt->dumpSchema)
16665 : 0 : return;
16666 : : }
16667 : : else
16668 : : {
16669 : : /* We do dump large object security labels in binary-upgrade mode */
16670 [ + - - + ]: 10 : if (!dopt->dumpData && !dopt->binary_upgrade)
16671 : 0 : return;
16672 : : }
16673 : :
16674 : : /* Search for security labels associated with catalogId, using table */
16675 : 10 : nlabels = findSecLabels(catalogId.tableoid, catalogId.oid, &labels);
16676 : :
16677 : 10 : query = createPQExpBuffer();
16678 : :
16679 [ + + ]: 15 : for (i = 0; i < nlabels; i++)
16680 : : {
16681 : : /*
16682 : : * Ignore label entries for which the subid doesn't match.
16683 : : */
16684 [ - + ]: 5 : if (labels[i].objsubid != subid)
16685 : 0 : continue;
16686 : :
16687 : 5 : appendPQExpBuffer(query,
16688 : : "SECURITY LABEL FOR %s ON %s ",
16689 : 5 : fmtId(labels[i].provider), type);
16690 [ - + - - ]: 5 : if (namespace && *namespace)
16691 : 0 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
16692 : 5 : appendPQExpBuffer(query, "%s IS ", name);
16693 : 5 : appendStringLiteralAH(query, labels[i].label, fout);
16694 : 5 : appendPQExpBufferStr(query, ";\n");
16695 : : }
16696 : :
16697 [ + + ]: 10 : if (query->len > 0)
16698 : : {
16699 : 5 : PQExpBuffer tag = createPQExpBuffer();
16700 : :
16701 : 5 : appendPQExpBuffer(tag, "%s %s", type, name);
16702 : 5 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16703 : 5 : ARCHIVE_OPTS(.tag = tag->data,
16704 : : .namespace = namespace,
16705 : : .owner = owner,
16706 : : .description = "SECURITY LABEL",
16707 : : .section = SECTION_NONE,
16708 : : .createStmt = query->data,
16709 : : .deps = &dumpId,
16710 : : .nDeps = 1));
16711 : 5 : destroyPQExpBuffer(tag);
16712 : : }
16713 : :
16714 : 10 : destroyPQExpBuffer(query);
16715 : : }
16716 : :
16717 : : /*
16718 : : * dumpTableSecLabel
16719 : : *
16720 : : * As above, but dump security label for both the specified table (or view)
16721 : : * and its columns.
16722 : : */
16723 : : static void
16724 : 0 : dumpTableSecLabel(Archive *fout, const TableInfo *tbinfo, const char *reltypename)
16725 : : {
16726 : 0 : DumpOptions *dopt = fout->dopt;
16727 : : SecLabelItem *labels;
16728 : : int nlabels;
16729 : : int i;
16730 : : PQExpBuffer query;
16731 : : PQExpBuffer target;
16732 : :
16733 : : /* do nothing, if --no-security-labels is supplied */
16734 [ # # ]: 0 : if (dopt->no_security_labels)
16735 : 0 : return;
16736 : :
16737 : : /* SecLabel are SCHEMA not data */
16738 [ # # ]: 0 : if (!dopt->dumpSchema)
16739 : 0 : return;
16740 : :
16741 : : /* Search for comments associated with relation, using table */
16742 : 0 : nlabels = findSecLabels(tbinfo->dobj.catId.tableoid,
16743 : 0 : tbinfo->dobj.catId.oid,
16744 : : &labels);
16745 : :
16746 : : /* If security labels exist, build SECURITY LABEL statements */
16747 [ # # ]: 0 : if (nlabels <= 0)
16748 : 0 : return;
16749 : :
16750 : 0 : query = createPQExpBuffer();
16751 : 0 : target = createPQExpBuffer();
16752 : :
16753 [ # # ]: 0 : for (i = 0; i < nlabels; i++)
16754 : : {
16755 : : const char *colname;
16756 : 0 : const char *provider = labels[i].provider;
16757 : 0 : const char *label = labels[i].label;
16758 : 0 : int objsubid = labels[i].objsubid;
16759 : :
16760 : 0 : resetPQExpBuffer(target);
16761 [ # # ]: 0 : if (objsubid == 0)
16762 : : {
16763 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16764 : 0 : fmtQualifiedDumpable(tbinfo));
16765 : : }
16766 : : else
16767 : : {
16768 : 0 : colname = getAttrName(objsubid, tbinfo);
16769 : : /* first fmtXXX result must be consumed before calling again */
16770 : 0 : appendPQExpBuffer(target, "COLUMN %s",
16771 : 0 : fmtQualifiedDumpable(tbinfo));
16772 : 0 : appendPQExpBuffer(target, ".%s", fmtId(colname));
16773 : : }
16774 : 0 : appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ",
16775 : : fmtId(provider), target->data);
16776 : 0 : appendStringLiteralAH(query, label, fout);
16777 : 0 : appendPQExpBufferStr(query, ";\n");
16778 : : }
16779 [ # # ]: 0 : if (query->len > 0)
16780 : : {
16781 : 0 : resetPQExpBuffer(target);
16782 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16783 : 0 : fmtId(tbinfo->dobj.name));
16784 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16785 : 0 : ARCHIVE_OPTS(.tag = target->data,
16786 : : .namespace = tbinfo->dobj.namespace->dobj.name,
16787 : : .owner = tbinfo->rolname,
16788 : : .description = "SECURITY LABEL",
16789 : : .section = SECTION_NONE,
16790 : : .createStmt = query->data,
16791 : : .deps = &(tbinfo->dobj.dumpId),
16792 : : .nDeps = 1));
16793 : : }
16794 : 0 : destroyPQExpBuffer(query);
16795 : 0 : destroyPQExpBuffer(target);
16796 : : }
16797 : :
16798 : : /*
16799 : : * findSecLabels
16800 : : *
16801 : : * Find the security label(s), if any, associated with the given object.
16802 : : * All the objsubid values associated with the given classoid/objoid are
16803 : : * found with one search.
16804 : : */
16805 : : static int
16806 : 10 : findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items)
16807 : : {
16808 : 10 : SecLabelItem *middle = NULL;
16809 : : SecLabelItem *low;
16810 : : SecLabelItem *high;
16811 : : int nmatch;
16812 : :
16813 [ - + ]: 10 : if (nseclabels <= 0) /* no labels, so no match is possible */
16814 : : {
16815 : 0 : *items = NULL;
16816 : 0 : return 0;
16817 : : }
16818 : :
16819 : : /*
16820 : : * Do binary search to find some item matching the object.
16821 : : */
16822 : 10 : low = &seclabels[0];
16823 : 10 : high = &seclabels[nseclabels - 1];
16824 [ + + ]: 15 : while (low <= high)
16825 : : {
16826 : 10 : middle = low + (high - low) / 2;
16827 : :
16828 [ - + ]: 10 : if (classoid < middle->classoid)
16829 : 0 : high = middle - 1;
16830 [ - + ]: 10 : else if (classoid > middle->classoid)
16831 : 0 : low = middle + 1;
16832 [ + + ]: 10 : else if (objoid < middle->objoid)
16833 : 5 : high = middle - 1;
16834 [ - + ]: 5 : else if (objoid > middle->objoid)
16835 : 0 : low = middle + 1;
16836 : : else
16837 : 5 : break; /* found a match */
16838 : : }
16839 : :
16840 [ + + ]: 10 : if (low > high) /* no matches */
16841 : : {
16842 : 5 : *items = NULL;
16843 : 5 : return 0;
16844 : : }
16845 : :
16846 : : /*
16847 : : * Now determine how many items match the object. The search loop
16848 : : * invariant still holds: only items between low and high inclusive could
16849 : : * match.
16850 : : */
16851 : 5 : nmatch = 1;
16852 [ - + ]: 5 : while (middle > low)
16853 : : {
16854 [ # # ]: 0 : if (classoid != middle[-1].classoid ||
16855 [ # # ]: 0 : objoid != middle[-1].objoid)
16856 : : break;
16857 : 0 : middle--;
16858 : 0 : nmatch++;
16859 : : }
16860 : :
16861 : 5 : *items = middle;
16862 : :
16863 : 5 : middle += nmatch;
16864 [ - + ]: 5 : while (middle <= high)
16865 : : {
16866 [ # # ]: 0 : if (classoid != middle->classoid ||
16867 [ # # ]: 0 : objoid != middle->objoid)
16868 : : break;
16869 : 0 : middle++;
16870 : 0 : nmatch++;
16871 : : }
16872 : :
16873 : 5 : return nmatch;
16874 : : }
16875 : :
16876 : : /*
16877 : : * collectSecLabels
16878 : : *
16879 : : * Construct a table of all security labels available for database objects;
16880 : : * also set the has-seclabel component flag for each relevant object.
16881 : : *
16882 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
16883 : : */
16884 : : static void
16885 : 215 : collectSecLabels(Archive *fout)
16886 : : {
16887 : : PGresult *res;
16888 : : PQExpBuffer query;
16889 : : int i_label;
16890 : : int i_provider;
16891 : : int i_classoid;
16892 : : int i_objoid;
16893 : : int i_objsubid;
16894 : : int ntups;
16895 : : int i;
16896 : : DumpableObject *dobj;
16897 : :
16898 : 215 : query = createPQExpBuffer();
16899 : :
16900 : 215 : appendPQExpBufferStr(query,
16901 : : "SELECT label, provider, classoid, objoid, objsubid "
16902 : : "FROM pg_catalog.pg_seclabels "
16903 : : "ORDER BY classoid, objoid, objsubid");
16904 : :
16905 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16906 : :
16907 : : /* Construct lookup table containing OIDs in numeric form */
16908 : 215 : i_label = PQfnumber(res, "label");
16909 : 215 : i_provider = PQfnumber(res, "provider");
16910 : 215 : i_classoid = PQfnumber(res, "classoid");
16911 : 215 : i_objoid = PQfnumber(res, "objoid");
16912 : 215 : i_objsubid = PQfnumber(res, "objsubid");
16913 : :
16914 : 215 : ntups = PQntuples(res);
16915 : :
16916 : 215 : seclabels = pg_malloc_array(SecLabelItem, ntups);
16917 : 215 : nseclabels = 0;
16918 : 215 : dobj = NULL;
16919 : :
16920 [ + + ]: 220 : for (i = 0; i < ntups; i++)
16921 : : {
16922 : : CatalogId objId;
16923 : : int subid;
16924 : :
16925 : 5 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
16926 : 5 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
16927 : 5 : subid = atoi(PQgetvalue(res, i, i_objsubid));
16928 : :
16929 : : /* We needn't remember labels that don't match any dumpable object */
16930 [ - + ]: 5 : if (dobj == NULL ||
16931 [ # # ]: 0 : dobj->catId.tableoid != objId.tableoid ||
16932 [ # # ]: 0 : dobj->catId.oid != objId.oid)
16933 : 5 : dobj = findObjectByCatalogId(objId);
16934 [ - + ]: 5 : if (dobj == NULL)
16935 : 0 : continue;
16936 : :
16937 : : /*
16938 : : * Labels on columns of composite types are linked to the type's
16939 : : * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
16940 : : * in the type's own DumpableObject.
16941 : : */
16942 [ - + - - ]: 5 : if (subid != 0 && dobj->objType == DO_TABLE &&
16943 [ # # ]: 0 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
16944 : 0 : {
16945 : : TypeInfo *cTypeInfo;
16946 : :
16947 : 0 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
16948 [ # # ]: 0 : if (cTypeInfo)
16949 : 0 : cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
16950 : : }
16951 : : else
16952 : 5 : dobj->components |= DUMP_COMPONENT_SECLABEL;
16953 : :
16954 : 5 : seclabels[nseclabels].label = pg_strdup(PQgetvalue(res, i, i_label));
16955 : 5 : seclabels[nseclabels].provider = pg_strdup(PQgetvalue(res, i, i_provider));
16956 : 5 : seclabels[nseclabels].classoid = objId.tableoid;
16957 : 5 : seclabels[nseclabels].objoid = objId.oid;
16958 : 5 : seclabels[nseclabels].objsubid = subid;
16959 : 5 : nseclabels++;
16960 : : }
16961 : :
16962 : 215 : PQclear(res);
16963 : 215 : destroyPQExpBuffer(query);
16964 : 215 : }
16965 : :
16966 : : /*
16967 : : * dumpTable
16968 : : * write out to fout the declarations (not data) of a user-defined table
16969 : : */
16970 : : static void
16971 : 36969 : dumpTable(Archive *fout, const TableInfo *tbinfo)
16972 : : {
16973 : 36969 : DumpOptions *dopt = fout->dopt;
16974 : 36969 : DumpId tableAclDumpId = InvalidDumpId;
16975 : : char *namecopy;
16976 : :
16977 : : /* Do nothing if not dumping schema */
16978 [ + + ]: 36969 : if (!dopt->dumpSchema)
16979 : 1645 : return;
16980 : :
16981 [ + + ]: 35324 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16982 : : {
16983 [ + + ]: 7203 : if (tbinfo->relkind == RELKIND_SEQUENCE)
16984 : 397 : dumpSequence(fout, tbinfo);
16985 : : else
16986 : 6806 : dumpTableSchema(fout, tbinfo);
16987 : : }
16988 : :
16989 : : /* Handle the ACL here */
16990 : 35324 : namecopy = pg_strdup(fmtId(tbinfo->dobj.name));
16991 [ + + ]: 35324 : if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
16992 : : {
16993 : 28875 : const char *objtype =
16994 [ + + ]: 28875 : (tbinfo->relkind == RELKIND_SEQUENCE) ? "SEQUENCE" : "TABLE";
16995 : :
16996 : : tableAclDumpId =
16997 : 28875 : dumpACL(fout, tbinfo->dobj.dumpId, InvalidDumpId,
16998 : : objtype, namecopy, NULL,
16999 : 28875 : tbinfo->dobj.namespace->dobj.name,
17000 : 28875 : NULL, tbinfo->rolname, &tbinfo->dacl);
17001 : : }
17002 : :
17003 : : /*
17004 : : * Handle column ACLs, if any. Note: we pull these with a separate query
17005 : : * rather than trying to fetch them during getTableAttrs, so that we won't
17006 : : * miss ACLs on system columns. Doing it this way also allows us to dump
17007 : : * ACLs for catalogs that we didn't mark "interesting" back in getTables.
17008 : : */
17009 [ + + + + ]: 35324 : if ((tbinfo->dobj.dump & DUMP_COMPONENT_ACL) && tbinfo->hascolumnACLs)
17010 : : {
17011 : 315 : PQExpBuffer query = createPQExpBuffer();
17012 : : PGresult *res;
17013 : : int i;
17014 : :
17015 [ + + ]: 315 : if (!fout->is_prepared[PREPQUERY_GETCOLUMNACLS])
17016 : : {
17017 : : /* Set up query for column ACLs */
17018 : 188 : appendPQExpBufferStr(query,
17019 : : "PREPARE getColumnACLs(pg_catalog.oid) AS\n");
17020 : :
17021 : : /*
17022 : : * In principle we should call acldefault('c', relowner) to get
17023 : : * the default ACL for a column. However, we don't currently
17024 : : * store the numeric OID of the relowner in TableInfo. We could
17025 : : * convert the owner name using regrole, but that creates a risk
17026 : : * of failure due to concurrent role renames. Given that the
17027 : : * default ACL for columns is empty and is likely to stay that
17028 : : * way, it's not worth extra cycles and risk to avoid hard-wiring
17029 : : * that knowledge here.
17030 : : */
17031 : 188 : appendPQExpBufferStr(query,
17032 : : "SELECT at.attname, "
17033 : : "at.attacl, "
17034 : : "'{}' AS acldefault, "
17035 : : "pip.privtype, pip.initprivs "
17036 : : "FROM pg_catalog.pg_attribute at "
17037 : : "LEFT JOIN pg_catalog.pg_init_privs pip ON "
17038 : : "(at.attrelid = pip.objoid "
17039 : : "AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass "
17040 : : "AND at.attnum = pip.objsubid) "
17041 : : "WHERE at.attrelid = $1 AND "
17042 : : "NOT at.attisdropped "
17043 : : "AND (at.attacl IS NOT NULL OR pip.initprivs IS NOT NULL) "
17044 : : "ORDER BY at.attnum");
17045 : :
17046 : 188 : ExecuteSqlStatement(fout, query->data);
17047 : :
17048 : 188 : fout->is_prepared[PREPQUERY_GETCOLUMNACLS] = true;
17049 : : }
17050 : :
17051 : 315 : printfPQExpBuffer(query,
17052 : : "EXECUTE getColumnACLs('%u')",
17053 : 315 : tbinfo->dobj.catId.oid);
17054 : :
17055 : 315 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17056 : :
17057 [ + + ]: 5866 : for (i = 0; i < PQntuples(res); i++)
17058 : : {
17059 : 5551 : char *attname = PQgetvalue(res, i, 0);
17060 : 5551 : char *attacl = PQgetvalue(res, i, 1);
17061 : 5551 : char *acldefault = PQgetvalue(res, i, 2);
17062 : 5551 : char privtype = *(PQgetvalue(res, i, 3));
17063 : 5551 : char *initprivs = PQgetvalue(res, i, 4);
17064 : : DumpableAcl coldacl;
17065 : : char *attnamecopy;
17066 : :
17067 : 5551 : coldacl.acl = attacl;
17068 : 5551 : coldacl.acldefault = acldefault;
17069 : 5551 : coldacl.privtype = privtype;
17070 : 5551 : coldacl.initprivs = initprivs;
17071 : 5551 : attnamecopy = pg_strdup(fmtId(attname));
17072 : :
17073 : : /*
17074 : : * Column's GRANT type is always TABLE. Each column ACL depends
17075 : : * on the table-level ACL, since we can restore column ACLs in
17076 : : * parallel but the table-level ACL has to be done first.
17077 : : */
17078 : 5551 : dumpACL(fout, tbinfo->dobj.dumpId, tableAclDumpId,
17079 : : "TABLE", namecopy, attnamecopy,
17080 : 5551 : tbinfo->dobj.namespace->dobj.name,
17081 : 5551 : NULL, tbinfo->rolname, &coldacl);
17082 : 5551 : pg_free(attnamecopy);
17083 : : }
17084 : 315 : PQclear(res);
17085 : 315 : destroyPQExpBuffer(query);
17086 : : }
17087 : :
17088 : 35324 : pg_free(namecopy);
17089 : : }
17090 : :
17091 : : /*
17092 : : * Create the AS clause for a view or materialized view. The semicolon is
17093 : : * stripped because a materialized view must add a WITH NO DATA clause.
17094 : : *
17095 : : * This returns a new buffer which must be freed by the caller.
17096 : : */
17097 : : static PQExpBuffer
17098 : 930 : createViewAsClause(Archive *fout, const TableInfo *tbinfo)
17099 : : {
17100 : 930 : PQExpBuffer query = createPQExpBuffer();
17101 : 930 : PQExpBuffer result = createPQExpBuffer();
17102 : : PGresult *res;
17103 : : int len;
17104 : :
17105 : : /* Fetch the view definition */
17106 : 930 : appendPQExpBuffer(query,
17107 : : "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
17108 : 930 : tbinfo->dobj.catId.oid);
17109 : :
17110 : 930 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17111 : :
17112 [ - + ]: 930 : if (PQntuples(res) != 1)
17113 : : {
17114 [ # # ]: 0 : if (PQntuples(res) < 1)
17115 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned no data",
17116 : : tbinfo->dobj.name);
17117 : : else
17118 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
17119 : : tbinfo->dobj.name);
17120 : : }
17121 : :
17122 : 930 : len = PQgetlength(res, 0, 0);
17123 : :
17124 [ - + ]: 930 : if (len == 0)
17125 : 0 : pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
17126 : : tbinfo->dobj.name);
17127 : :
17128 : : /* Strip off the trailing semicolon so that other things may follow. */
17129 : : Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
17130 : 930 : appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
17131 : :
17132 : 930 : PQclear(res);
17133 : 930 : destroyPQExpBuffer(query);
17134 : :
17135 : 930 : return result;
17136 : : }
17137 : :
17138 : : /*
17139 : : * Create a dummy AS clause for a view. This is used when the real view
17140 : : * definition has to be postponed because of circular dependencies.
17141 : : * We must duplicate the view's external properties -- column names and types
17142 : : * (including collation) -- so that it works for subsequent references.
17143 : : *
17144 : : * This returns a new buffer which must be freed by the caller.
17145 : : */
17146 : : static PQExpBuffer
17147 : 48 : createDummyViewAsClause(Archive *fout, const TableInfo *tbinfo)
17148 : : {
17149 : 48 : PQExpBuffer result = createPQExpBuffer();
17150 : : int j;
17151 : :
17152 : 48 : appendPQExpBufferStr(result, "SELECT");
17153 : :
17154 [ + + ]: 122 : for (j = 0; j < tbinfo->numatts; j++)
17155 : : {
17156 [ + + ]: 74 : if (j > 0)
17157 : 36 : appendPQExpBufferChar(result, ',');
17158 : 74 : appendPQExpBufferStr(result, "\n ");
17159 : :
17160 : 74 : appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
17161 : :
17162 : : /*
17163 : : * Must add collation if not default for the type, because CREATE OR
17164 : : * REPLACE VIEW won't change it
17165 : : */
17166 [ - + ]: 74 : if (OidIsValid(tbinfo->attcollation[j]))
17167 : : {
17168 : : CollInfo *coll;
17169 : :
17170 : 0 : coll = findCollationByOid(tbinfo->attcollation[j]);
17171 [ # # ]: 0 : if (coll)
17172 : 0 : appendPQExpBuffer(result, " COLLATE %s",
17173 : 0 : fmtQualifiedDumpable(coll));
17174 : : }
17175 : :
17176 : 74 : appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
17177 : : }
17178 : :
17179 : 48 : return result;
17180 : : }
17181 : :
17182 : : /*
17183 : : * dumpTableSchema
17184 : : * write the declaration (not data) of one user-defined table or view
17185 : : */
17186 : : static void
17187 : 6806 : dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
17188 : : {
17189 : 6806 : DumpOptions *dopt = fout->dopt;
17190 : 6806 : PQExpBuffer q = createPQExpBuffer();
17191 : 6806 : PQExpBuffer delq = createPQExpBuffer();
17192 : 6806 : PQExpBuffer extra = createPQExpBuffer();
17193 : : char *qrelname;
17194 : : char *qualrelname;
17195 : : int numParents;
17196 : : TableInfo **parents;
17197 : : int actual_atts; /* number of attrs in this CREATE statement */
17198 : : const char *reltypename;
17199 : : char *storage;
17200 : : int j,
17201 : : k;
17202 : :
17203 : : /* We had better have loaded per-column details about this table */
17204 : : Assert(tbinfo->interesting);
17205 : :
17206 : 6806 : qrelname = pg_strdup(fmtId(tbinfo->dobj.name));
17207 : 6806 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
17208 : :
17209 [ - + ]: 6806 : if (tbinfo->hasoids)
17210 : 0 : pg_log_warning("WITH OIDS is not supported anymore (table \"%s\")",
17211 : : qrelname);
17212 : :
17213 [ + + ]: 6806 : if (dopt->binary_upgrade)
17214 : : {
17215 : 929 : binary_upgrade_set_type_oids_by_rel(fout, q, tbinfo);
17216 : :
17217 : : /* only relations with a row type have versioned attributes */
17218 [ + + + + : 929 : if (RELKIND_HAS_ROWTYPE(tbinfo->relkind))
+ + + - +
+ + - ]
17219 : 929 : binary_upgrade_set_next_attribute_versions(q, tbinfo->numatts,
17220 : 929 : tbinfo->attversion);
17221 : : }
17222 : :
17223 : : /* Is it a table or a view? */
17224 [ + + ]: 6806 : if (tbinfo->relkind == RELKIND_VIEW)
17225 : : {
17226 : : PQExpBuffer result;
17227 : :
17228 : : /*
17229 : : * Note: keep this code in sync with the is_view case in dumpRule()
17230 : : */
17231 : :
17232 : 576 : reltypename = "VIEW";
17233 : :
17234 : 576 : appendPQExpBuffer(delq, "DROP VIEW %s;\n", qualrelname);
17235 : :
17236 [ + + ]: 576 : if (dopt->binary_upgrade)
17237 : 55 : binary_upgrade_set_pg_class_oids(fout, q,
17238 : 55 : tbinfo->dobj.catId.oid);
17239 : :
17240 : 576 : appendPQExpBuffer(q, "CREATE VIEW %s", qualrelname);
17241 : :
17242 [ + + ]: 576 : if (tbinfo->dummy_view)
17243 : 24 : result = createDummyViewAsClause(fout, tbinfo);
17244 : : else
17245 : : {
17246 [ + + ]: 552 : if (nonemptyReloptions(tbinfo->reloptions))
17247 : : {
17248 : 63 : appendPQExpBufferStr(q, " WITH (");
17249 : 63 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17250 : 63 : appendPQExpBufferChar(q, ')');
17251 : : }
17252 : 552 : result = createViewAsClause(fout, tbinfo);
17253 : : }
17254 : 576 : appendPQExpBuffer(q, " AS\n%s", result->data);
17255 : 576 : destroyPQExpBuffer(result);
17256 : :
17257 [ + + + - ]: 576 : if (tbinfo->checkoption != NULL && !tbinfo->dummy_view)
17258 : 34 : appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption);
17259 : 576 : appendPQExpBufferStr(q, ";\n");
17260 : : }
17261 : : else
17262 : : {
17263 : 6230 : char *partkeydef = NULL;
17264 : 6230 : char *ftoptions = NULL;
17265 : 6230 : char *srvname = NULL;
17266 : 6230 : const char *foreign = "";
17267 : :
17268 : : /*
17269 : : * Set reltypename, and collect any relkind-specific data that we
17270 : : * didn't fetch during getTables().
17271 : : */
17272 [ + + + + ]: 6230 : switch (tbinfo->relkind)
17273 : : {
17274 : 621 : case RELKIND_PARTITIONED_TABLE:
17275 : : {
17276 : 621 : PQExpBuffer query = createPQExpBuffer();
17277 : : PGresult *res;
17278 : :
17279 : 621 : reltypename = "TABLE";
17280 : :
17281 : : /* retrieve partition key definition */
17282 : 621 : appendPQExpBuffer(query,
17283 : : "SELECT pg_get_partkeydef('%u')",
17284 : 621 : tbinfo->dobj.catId.oid);
17285 : 621 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17286 : 621 : partkeydef = pg_strdup(PQgetvalue(res, 0, 0));
17287 : 621 : PQclear(res);
17288 : 621 : destroyPQExpBuffer(query);
17289 : 621 : break;
17290 : : }
17291 : 37 : case RELKIND_FOREIGN_TABLE:
17292 : : {
17293 : 37 : PQExpBuffer query = createPQExpBuffer();
17294 : : PGresult *res;
17295 : : int i_srvname;
17296 : : int i_ftoptions;
17297 : :
17298 : 37 : reltypename = "FOREIGN TABLE";
17299 : :
17300 : : /* retrieve name of foreign server and generic options */
17301 : 37 : appendPQExpBuffer(query,
17302 : : "SELECT fs.srvname, "
17303 : : "pg_catalog.array_to_string(ARRAY("
17304 : : "SELECT pg_catalog.quote_ident(option_name) || "
17305 : : "' ' || pg_catalog.quote_literal(option_value) "
17306 : : "FROM pg_catalog.pg_options_to_table(ftoptions) "
17307 : : "ORDER BY option_name"
17308 : : "), E',\n ') AS ftoptions "
17309 : : "FROM pg_catalog.pg_foreign_table ft "
17310 : : "JOIN pg_catalog.pg_foreign_server fs "
17311 : : "ON (fs.oid = ft.ftserver) "
17312 : : "WHERE ft.ftrelid = '%u'",
17313 : 37 : tbinfo->dobj.catId.oid);
17314 : 37 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17315 : 37 : i_srvname = PQfnumber(res, "srvname");
17316 : 37 : i_ftoptions = PQfnumber(res, "ftoptions");
17317 : 37 : srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
17318 : 37 : ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
17319 : 37 : PQclear(res);
17320 : 37 : destroyPQExpBuffer(query);
17321 : :
17322 : 37 : foreign = "FOREIGN ";
17323 : 37 : break;
17324 : : }
17325 : 354 : case RELKIND_MATVIEW:
17326 : 354 : reltypename = "MATERIALIZED VIEW";
17327 : 354 : break;
17328 : 5218 : default:
17329 : 5218 : reltypename = "TABLE";
17330 : 5218 : break;
17331 : : }
17332 : :
17333 : 6230 : numParents = tbinfo->numParents;
17334 : 6230 : parents = tbinfo->parents;
17335 : :
17336 : 6230 : appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname);
17337 : :
17338 : : /*
17339 : : * This also keeps the OIDs and filenodes of a conflict log table, so
17340 : : * that pg_upgrade finds its files and pg_subscription's
17341 : : * subconflictlogrelid stays the same.
17342 : : */
17343 [ + + ]: 6230 : if (dopt->binary_upgrade)
17344 : 874 : binary_upgrade_set_pg_class_oids(fout, q,
17345 : 874 : tbinfo->dobj.catId.oid);
17346 : :
17347 : : /*
17348 : : * PostgreSQL 18 has disabled UNLOGGED for partitioned tables, so
17349 : : * ignore it when dumping if it was set in this case.
17350 : : */
17351 : 6230 : appendPQExpBuffer(q, "CREATE %s%s %s",
17352 [ + + ]: 6230 : (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
17353 [ + - ]: 20 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ?
17354 : : "UNLOGGED " : "",
17355 : : reltypename,
17356 : : qualrelname);
17357 : :
17358 : : /*
17359 : : * Attach to type, if reloftype; except in case of a binary upgrade,
17360 : : * we dump the table normally and attach it to the type afterward.
17361 : : */
17362 [ + + + + ]: 6230 : if (OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade)
17363 : 24 : appendPQExpBuffer(q, " OF %s",
17364 : 24 : getFormattedTypeName(fout, tbinfo->reloftype,
17365 : : zeroIsError));
17366 : :
17367 [ + + ]: 6230 : if (tbinfo->relkind != RELKIND_MATVIEW)
17368 : : {
17369 : : /* Dump the attributes */
17370 : 5876 : actual_atts = 0;
17371 [ + + ]: 26972 : for (j = 0; j < tbinfo->numatts; j++)
17372 : : {
17373 : : /*
17374 : : * Normally, dump if it's locally defined in this table, and
17375 : : * not dropped. But for binary upgrade, we'll dump all the
17376 : : * columns, and then fix up the dropped and nonlocal cases
17377 : : * below.
17378 : : */
17379 [ + + ]: 21096 : if (shouldPrintColumn(dopt, tbinfo, j))
17380 : : {
17381 : : bool print_default;
17382 : : bool print_notnull;
17383 : :
17384 : : /*
17385 : : * Default value --- suppress if to be printed separately
17386 : : * or not at all.
17387 : : */
17388 : 41034 : print_default = (tbinfo->attrdefs[j] != NULL &&
17389 [ + + + + ]: 21045 : tbinfo->attrdefs[j]->dobj.dump &&
17390 [ + + ]: 1105 : !tbinfo->attrdefs[j]->separate);
17391 : :
17392 : : /*
17393 : : * Not Null constraint --- print it if it is locally
17394 : : * defined, or if binary upgrade. (In the latter case, we
17395 : : * reset conislocal below.)
17396 : : */
17397 [ + + ]: 22317 : print_notnull = (tbinfo->notnull_constrs[j] != NULL &&
17398 [ + + ]: 2377 : (tbinfo->notnull_islocal[j] ||
17399 [ + + ]: 655 : dopt->binary_upgrade ||
17400 [ + + ]: 565 : tbinfo->ispartition));
17401 : :
17402 : : /*
17403 : : * Skip column if fully defined by reloftype, except in
17404 : : * binary upgrade
17405 : : */
17406 [ + + ]: 19940 : if (OidIsValid(tbinfo->reloftype) &&
17407 [ + + + + ]: 53 : !print_default && !print_notnull &&
17408 [ + + ]: 33 : !dopt->binary_upgrade)
17409 : 24 : continue;
17410 : :
17411 : : /* Format properly if not first attr */
17412 [ + + ]: 19916 : if (actual_atts == 0)
17413 : 5501 : appendPQExpBufferStr(q, " (");
17414 : : else
17415 : 14415 : appendPQExpBufferChar(q, ',');
17416 : 19916 : appendPQExpBufferStr(q, "\n ");
17417 : 19916 : actual_atts++;
17418 : :
17419 : : /* Attribute name */
17420 : 19916 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j]));
17421 : :
17422 [ + + ]: 19916 : if (tbinfo->attisdropped[j])
17423 : : {
17424 : : /*
17425 : : * ALTER TABLE DROP COLUMN clears
17426 : : * pg_attribute.atttypid, so we will not have gotten a
17427 : : * valid type name; insert INTEGER as a stopgap. We'll
17428 : : * clean things up later.
17429 : : */
17430 : 94 : appendPQExpBufferStr(q, " INTEGER /* dummy */");
17431 : : /* and skip to the next column */
17432 : 94 : continue;
17433 : : }
17434 : :
17435 : : /*
17436 : : * Attribute type; print it except when creating a typed
17437 : : * table ('OF type_name'), but in binary-upgrade mode,
17438 : : * print it in that case too.
17439 : : */
17440 [ + + + + ]: 19822 : if (dopt->binary_upgrade || !OidIsValid(tbinfo->reloftype))
17441 : : {
17442 : 19806 : appendPQExpBuffer(q, " %s",
17443 : 19806 : tbinfo->atttypnames[j]);
17444 : : }
17445 : :
17446 [ + + ]: 19822 : if (print_default)
17447 : : {
17448 [ + + ]: 971 : if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED)
17449 : 328 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s) STORED",
17450 : 328 : tbinfo->attrdefs[j]->adef_expr);
17451 [ + + ]: 643 : else if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_VIRTUAL)
17452 : 262 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s)",
17453 : 262 : tbinfo->attrdefs[j]->adef_expr);
17454 : : else
17455 : 381 : appendPQExpBuffer(q, " DEFAULT %s",
17456 : 381 : tbinfo->attrdefs[j]->adef_expr);
17457 : : }
17458 : :
17459 [ + + ]: 19822 : if (print_notnull)
17460 : : {
17461 [ + + ]: 2344 : if (tbinfo->notnull_constrs[j][0] == '\0')
17462 : 1661 : appendPQExpBufferStr(q, " NOT NULL");
17463 : : else
17464 : 683 : appendPQExpBuffer(q, " CONSTRAINT %s NOT NULL",
17465 : 683 : fmtId(tbinfo->notnull_constrs[j]));
17466 : :
17467 [ + + ]: 2344 : if (tbinfo->notnull_noinh[j])
17468 : 35 : appendPQExpBufferStr(q, " NO INHERIT");
17469 : : }
17470 : :
17471 : : /* Add collation if not default for the type */
17472 [ + + ]: 19822 : if (OidIsValid(tbinfo->attcollation[j]))
17473 : : {
17474 : : CollInfo *coll;
17475 : :
17476 : 203 : coll = findCollationByOid(tbinfo->attcollation[j]);
17477 [ + - ]: 203 : if (coll)
17478 : 203 : appendPQExpBuffer(q, " COLLATE %s",
17479 : 203 : fmtQualifiedDumpable(coll));
17480 : : }
17481 : : }
17482 : :
17483 : : /*
17484 : : * On the other hand, if we choose not to print a column
17485 : : * (likely because it is created by inheritance), but the
17486 : : * column has a locally-defined not-null constraint, we need
17487 : : * to dump the constraint as a standalone object.
17488 : : *
17489 : : * This syntax isn't SQL-conforming, but if you wanted
17490 : : * standard output you wouldn't be creating non-standard
17491 : : * objects to begin with.
17492 : : */
17493 [ + + ]: 20978 : if (!shouldPrintColumn(dopt, tbinfo, j) &&
17494 [ + + ]: 1156 : !tbinfo->attisdropped[j] &&
17495 [ + + ]: 700 : tbinfo->notnull_constrs[j] != NULL &&
17496 [ + + ]: 216 : tbinfo->notnull_islocal[j])
17497 : : {
17498 : : /* Format properly if not first attr */
17499 [ + + ]: 94 : if (actual_atts == 0)
17500 : 90 : appendPQExpBufferStr(q, " (");
17501 : : else
17502 : 4 : appendPQExpBufferChar(q, ',');
17503 : 94 : appendPQExpBufferStr(q, "\n ");
17504 : 94 : actual_atts++;
17505 : :
17506 [ + + ]: 94 : if (tbinfo->notnull_constrs[j][0] != '\0')
17507 : 86 : appendPQExpBuffer(q, "CONSTRAINT %s ",
17508 : 86 : fmtId(tbinfo->notnull_constrs[j]));
17509 : 94 : appendPQExpBuffer(q, "NOT NULL %s",
17510 : 94 : fmtId(tbinfo->attnames[j]));
17511 : :
17512 [ + + ]: 94 : if (tbinfo->notnull_noinh[j])
17513 : 33 : appendPQExpBufferStr(q, " NO INHERIT");
17514 : : }
17515 : : }
17516 : :
17517 : : /*
17518 : : * Add non-inherited CHECK constraints, if any.
17519 : : *
17520 : : * For partitions, we need to include check constraints even if
17521 : : * they're not defined locally, because the ALTER TABLE ATTACH
17522 : : * PARTITION that we'll emit later expects the constraint to be
17523 : : * there. (No need to fix conislocal: ATTACH PARTITION does that)
17524 : : */
17525 [ + + ]: 6469 : for (j = 0; j < tbinfo->ncheck; j++)
17526 : : {
17527 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
17528 : :
17529 [ + + ]: 593 : if (constr->separate ||
17530 [ + + + + ]: 523 : (!constr->conislocal && !tbinfo->ispartition))
17531 : 109 : continue;
17532 : :
17533 [ + + ]: 484 : if (actual_atts == 0)
17534 : 16 : appendPQExpBufferStr(q, " (\n ");
17535 : : else
17536 : 468 : appendPQExpBufferStr(q, ",\n ");
17537 : :
17538 : 484 : appendPQExpBuffer(q, "CONSTRAINT %s ",
17539 : 484 : fmtId(constr->dobj.name));
17540 : 484 : appendPQExpBufferStr(q, constr->condef);
17541 : :
17542 : 484 : actual_atts++;
17543 : : }
17544 : :
17545 [ + + ]: 5876 : if (actual_atts)
17546 : 5607 : appendPQExpBufferStr(q, "\n)");
17547 [ + + - + ]: 269 : else if (!(OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade))
17548 : : {
17549 : : /*
17550 : : * No attributes? we must have a parenthesized attribute list,
17551 : : * even though empty, when not using the OF TYPE syntax.
17552 : : */
17553 : 257 : appendPQExpBufferStr(q, " (\n)");
17554 : : }
17555 : :
17556 : : /*
17557 : : * Emit the INHERITS clause (not for partitions), except in
17558 : : * binary-upgrade mode.
17559 : : */
17560 [ + + + + ]: 5876 : if (numParents > 0 && !tbinfo->ispartition &&
17561 [ + + ]: 536 : !dopt->binary_upgrade)
17562 : : {
17563 : 470 : appendPQExpBufferStr(q, "\nINHERITS (");
17564 [ + + ]: 1013 : for (k = 0; k < numParents; k++)
17565 : : {
17566 : 543 : TableInfo *parentRel = parents[k];
17567 : :
17568 [ + + ]: 543 : if (k > 0)
17569 : 73 : appendPQExpBufferStr(q, ", ");
17570 : 543 : appendPQExpBufferStr(q, fmtQualifiedDumpable(parentRel));
17571 : : }
17572 : 470 : appendPQExpBufferChar(q, ')');
17573 : : }
17574 : :
17575 [ + + ]: 5876 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
17576 : 621 : appendPQExpBuffer(q, "\nPARTITION BY %s", partkeydef);
17577 : :
17578 [ + + ]: 5876 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
17579 : 37 : appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
17580 : : }
17581 : :
17582 [ + + - + ]: 12305 : if (nonemptyReloptions(tbinfo->reloptions) ||
17583 : 6075 : nonemptyReloptions(tbinfo->toast_reloptions))
17584 : : {
17585 : 155 : bool addcomma = false;
17586 : :
17587 : 155 : appendPQExpBufferStr(q, "\nWITH (");
17588 [ + - ]: 155 : if (nonemptyReloptions(tbinfo->reloptions))
17589 : : {
17590 : 155 : addcomma = true;
17591 : 155 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17592 : : }
17593 [ + + ]: 155 : if (nonemptyReloptions(tbinfo->toast_reloptions))
17594 : : {
17595 [ + - ]: 5 : if (addcomma)
17596 : 5 : appendPQExpBufferStr(q, ", ");
17597 : 5 : appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.",
17598 : : fout);
17599 : : }
17600 : 155 : appendPQExpBufferChar(q, ')');
17601 : : }
17602 : :
17603 : : /* Dump generic options if any */
17604 [ + + + + ]: 6230 : if (ftoptions && ftoptions[0])
17605 : 34 : appendPQExpBuffer(q, "\nOPTIONS (\n %s\n)", ftoptions);
17606 : :
17607 : : /*
17608 : : * For materialized views, create the AS clause just like a view. At
17609 : : * this point, we always mark the view as not populated.
17610 : : */
17611 [ + + ]: 6230 : if (tbinfo->relkind == RELKIND_MATVIEW)
17612 : : {
17613 : : PQExpBuffer result;
17614 : :
17615 : 354 : result = createViewAsClause(fout, tbinfo);
17616 : 354 : appendPQExpBuffer(q, " AS\n%s\n WITH NO DATA;\n",
17617 : : result->data);
17618 : 354 : destroyPQExpBuffer(result);
17619 : : }
17620 : : else
17621 : 5876 : appendPQExpBufferStr(q, ";\n");
17622 : :
17623 : : /* Materialized views can depend on extensions */
17624 [ + + ]: 6230 : if (tbinfo->relkind == RELKIND_MATVIEW)
17625 : 354 : append_depends_on_extension(fout, q, &tbinfo->dobj,
17626 : : "pg_catalog.pg_class",
17627 : : "MATERIALIZED VIEW",
17628 : : qualrelname);
17629 : :
17630 : : /*
17631 : : * in binary upgrade mode, update the catalog with any missing values
17632 : : * that might be present.
17633 : : */
17634 [ + + ]: 6230 : if (dopt->binary_upgrade)
17635 : : {
17636 [ + + ]: 4195 : for (j = 0; j < tbinfo->numatts; j++)
17637 : : {
17638 [ + + ]: 3321 : if (tbinfo->attmissingval[j][0] != '\0')
17639 : : {
17640 : 3 : appendPQExpBufferStr(q, "\n-- set missing value.\n");
17641 : 3 : appendPQExpBufferStr(q,
17642 : : "SELECT pg_catalog.binary_upgrade_set_missing_value(");
17643 : 3 : appendStringLiteralAH(q, qualrelname, fout);
17644 : 3 : appendPQExpBufferStr(q, "::pg_catalog.regclass,");
17645 : 3 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17646 : 3 : appendPQExpBufferChar(q, ',');
17647 : 3 : appendStringLiteralAH(q, tbinfo->attmissingval[j], fout);
17648 : 3 : appendPQExpBufferStr(q, ");\n\n");
17649 : : }
17650 : : }
17651 : : }
17652 : :
17653 : : /*
17654 : : * To create binary-compatible heap files, we have to ensure the same
17655 : : * physical column order, including dropped columns, as in the
17656 : : * original. Therefore, we create dropped columns above and drop them
17657 : : * here, also updating their attlen/attalign values so that the
17658 : : * dropped column can be skipped properly. (We do not bother with
17659 : : * restoring the original attbyval setting.) Also, inheritance
17660 : : * relationships are set up by doing ALTER TABLE INHERIT rather than
17661 : : * using an INHERITS clause --- the latter would possibly mess up the
17662 : : * column order. That also means we have to take care about setting
17663 : : * attislocal correctly, plus fix up any inherited CHECK constraints.
17664 : : * Analogously, we set up typed tables using ALTER TABLE / OF here.
17665 : : *
17666 : : * We process foreign and partitioned tables here, even though they
17667 : : * lack heap storage, because they can participate in inheritance
17668 : : * relationships and we want this stuff to be consistent across the
17669 : : * inheritance tree. We can exclude indexes, toast tables, sequences
17670 : : * and matviews, even though they have storage, because we don't
17671 : : * support altering or dropping columns in them, nor can they be part
17672 : : * of inheritance trees.
17673 : : */
17674 [ + + ]: 6230 : if (dopt->binary_upgrade &&
17675 [ + + ]: 874 : (tbinfo->relkind == RELKIND_RELATION ||
17676 [ + + ]: 118 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
17677 [ + + ]: 116 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE))
17678 : : {
17679 : : bool firstitem;
17680 : : bool firstitem_extra;
17681 : :
17682 : : /*
17683 : : * Drop any dropped columns. Merge the pg_attribute manipulations
17684 : : * into a single SQL command, so that we don't cause repeated
17685 : : * relcache flushes on the target table. Otherwise we risk O(N^2)
17686 : : * relcache bloat while dropping N columns.
17687 : : */
17688 : 856 : resetPQExpBuffer(extra);
17689 : 856 : firstitem = true;
17690 [ + + ]: 4154 : for (j = 0; j < tbinfo->numatts; j++)
17691 : : {
17692 [ + + ]: 3298 : if (tbinfo->attisdropped[j])
17693 : : {
17694 [ + + ]: 94 : if (firstitem)
17695 : : {
17696 : 46 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped columns.\n"
17697 : : "UPDATE pg_catalog.pg_attribute\n"
17698 : : "SET attlen = v.dlen, "
17699 : : "attalign = v.dalign, "
17700 : : "attbyval = false\n"
17701 : : "FROM (VALUES ");
17702 : 46 : firstitem = false;
17703 : : }
17704 : : else
17705 : 48 : appendPQExpBufferStr(q, ",\n ");
17706 : 94 : appendPQExpBufferChar(q, '(');
17707 : 94 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17708 : 94 : appendPQExpBuffer(q, ", %d, '%c')",
17709 : 94 : tbinfo->attlen[j],
17710 : 94 : tbinfo->attalign[j]);
17711 : : /* The ALTER ... DROP COLUMN commands must come after */
17712 : 94 : appendPQExpBuffer(extra, "ALTER %sTABLE ONLY %s ",
17713 : : foreign, qualrelname);
17714 : 94 : appendPQExpBuffer(extra, "DROP COLUMN %s;\n",
17715 : 94 : fmtId(tbinfo->attnames[j]));
17716 : : }
17717 : : }
17718 [ + + ]: 856 : if (!firstitem)
17719 : : {
17720 : 46 : appendPQExpBufferStr(q, ") v(dname, dlen, dalign)\n"
17721 : : "WHERE attrelid = ");
17722 : 46 : appendStringLiteralAH(q, qualrelname, fout);
17723 : 46 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17724 : : " AND attname = v.dname;\n");
17725 : : /* Now we can issue the actual DROP COLUMN commands */
17726 : 46 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17727 : : }
17728 : :
17729 : : /*
17730 : : * Fix up inherited columns. As above, do the pg_attribute
17731 : : * manipulations in a single SQL command.
17732 : : */
17733 : 856 : firstitem = true;
17734 [ + + ]: 4154 : for (j = 0; j < tbinfo->numatts; j++)
17735 : : {
17736 [ + + ]: 3298 : if (!tbinfo->attisdropped[j] &&
17737 [ + + ]: 3204 : !tbinfo->attislocal[j])
17738 : : {
17739 [ + + ]: 644 : if (firstitem)
17740 : : {
17741 : 282 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited columns.\n");
17742 : 282 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n"
17743 : : "SET attislocal = false\n"
17744 : : "WHERE attrelid = ");
17745 : 282 : appendStringLiteralAH(q, qualrelname, fout);
17746 : 282 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17747 : : " AND attname IN (");
17748 : 282 : firstitem = false;
17749 : : }
17750 : : else
17751 : 362 : appendPQExpBufferStr(q, ", ");
17752 : 644 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17753 : : }
17754 : : }
17755 [ + + ]: 856 : if (!firstitem)
17756 : 282 : appendPQExpBufferStr(q, ");\n");
17757 : :
17758 : : /*
17759 : : * Fix up not-null constraints that come from inheritance. As
17760 : : * above, do the pg_constraint manipulations in a single SQL
17761 : : * command. (Actually, two in special cases, if we're doing an
17762 : : * upgrade from < 18).
17763 : : */
17764 : 856 : firstitem = true;
17765 : 856 : firstitem_extra = true;
17766 : 856 : resetPQExpBuffer(extra);
17767 [ + + ]: 4154 : for (j = 0; j < tbinfo->numatts; j++)
17768 : : {
17769 : : /*
17770 : : * If a not-null constraint comes from inheritance, reset
17771 : : * conislocal. The inhcount is fixed by ALTER TABLE INHERIT,
17772 : : * below. Special hack: in versions < 18, columns with no
17773 : : * local definition need their constraint to be matched by
17774 : : * column number in conkeys instead of by constraint name,
17775 : : * because the latter is not available. (We distinguish the
17776 : : * case because the constraint name is the empty string.)
17777 : : */
17778 [ + + ]: 3298 : if (tbinfo->notnull_constrs[j] != NULL &&
17779 [ + + ]: 304 : !tbinfo->notnull_islocal[j])
17780 : : {
17781 [ + + ]: 90 : if (tbinfo->notnull_constrs[j][0] != '\0')
17782 : : {
17783 [ + + ]: 77 : if (firstitem)
17784 : : {
17785 : 65 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
17786 : : "SET conislocal = false\n"
17787 : : "WHERE contype = 'n' AND conrelid = ");
17788 : 65 : appendStringLiteralAH(q, qualrelname, fout);
17789 : 65 : appendPQExpBufferStr(q, "::pg_catalog.regclass AND\n"
17790 : : "conname IN (");
17791 : 65 : firstitem = false;
17792 : : }
17793 : : else
17794 : 12 : appendPQExpBufferStr(q, ", ");
17795 : 77 : appendStringLiteralAH(q, tbinfo->notnull_constrs[j], fout);
17796 : : }
17797 : : else
17798 : : {
17799 [ + - ]: 13 : if (firstitem_extra)
17800 : : {
17801 : 13 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17802 : : "SET conislocal = false\n"
17803 : : "WHERE contype = 'n' AND conrelid = ");
17804 : 13 : appendStringLiteralAH(extra, qualrelname, fout);
17805 : 13 : appendPQExpBufferStr(extra, "::pg_catalog.regclass AND\n"
17806 : : "conkey IN (");
17807 : 13 : firstitem_extra = false;
17808 : : }
17809 : : else
17810 : 0 : appendPQExpBufferStr(extra, ", ");
17811 : 13 : appendPQExpBuffer(extra, "'{%d}'", j + 1);
17812 : : }
17813 : : }
17814 : : }
17815 [ + + ]: 856 : if (!firstitem)
17816 : 65 : appendPQExpBufferStr(q, ");\n");
17817 [ + + ]: 856 : if (!firstitem_extra)
17818 : 13 : appendPQExpBufferStr(extra, ");\n");
17819 : :
17820 [ + + ]: 856 : if (extra->len > 0)
17821 : 13 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17822 : :
17823 : : /*
17824 : : * Add inherited CHECK constraints, if any.
17825 : : *
17826 : : * For partitions, they were already dumped, and conislocal
17827 : : * doesn't need fixing.
17828 : : *
17829 : : * As above, issue only one direct manipulation of pg_constraint.
17830 : : * Although it is tempting to merge the ALTER ADD CONSTRAINT
17831 : : * commands into one as well, refrain for now due to concern about
17832 : : * possible backend memory bloat if there are many such
17833 : : * constraints.
17834 : : */
17835 : 856 : resetPQExpBuffer(extra);
17836 : 856 : firstitem = true;
17837 [ + + ]: 918 : for (k = 0; k < tbinfo->ncheck; k++)
17838 : : {
17839 : 62 : ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
17840 : :
17841 [ + + + + : 62 : if (constr->separate || constr->conislocal || tbinfo->ispartition)
+ + ]
17842 : 60 : continue;
17843 : :
17844 [ + - ]: 2 : if (firstitem)
17845 : 2 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraints.\n");
17846 : 2 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n",
17847 : : foreign, qualrelname,
17848 : 2 : fmtId(constr->dobj.name),
17849 : : constr->condef);
17850 : : /* Update pg_constraint after all the ALTER TABLEs */
17851 [ + - ]: 2 : if (firstitem)
17852 : : {
17853 : 2 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17854 : : "SET conislocal = false\n"
17855 : : "WHERE contype = 'c' AND conrelid = ");
17856 : 2 : appendStringLiteralAH(extra, qualrelname, fout);
17857 : 2 : appendPQExpBufferStr(extra, "::pg_catalog.regclass\n");
17858 : 2 : appendPQExpBufferStr(extra, " AND conname IN (");
17859 : 2 : firstitem = false;
17860 : : }
17861 : : else
17862 : 0 : appendPQExpBufferStr(extra, ", ");
17863 : 2 : appendStringLiteralAH(extra, constr->dobj.name, fout);
17864 : : }
17865 [ + + ]: 856 : if (!firstitem)
17866 : : {
17867 : 2 : appendPQExpBufferStr(extra, ");\n");
17868 : 2 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17869 : : }
17870 : :
17871 [ + + + + ]: 856 : if (numParents > 0 && !tbinfo->ispartition)
17872 : : {
17873 : 66 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
17874 [ + + ]: 143 : for (k = 0; k < numParents; k++)
17875 : : {
17876 : 77 : TableInfo *parentRel = parents[k];
17877 : :
17878 : 77 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s INHERIT %s;\n", foreign,
17879 : : qualrelname,
17880 : 77 : fmtQualifiedDumpable(parentRel));
17881 : : }
17882 : : }
17883 : :
17884 [ + + ]: 856 : if (OidIsValid(tbinfo->reloftype))
17885 : : {
17886 : 7 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n");
17887 : 7 : appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
17888 : : qualrelname,
17889 : 7 : getFormattedTypeName(fout, tbinfo->reloftype,
17890 : : zeroIsError));
17891 : : }
17892 : : }
17893 : :
17894 : : /*
17895 : : * In binary_upgrade mode, arrange to restore the old relfrozenxid and
17896 : : * relminmxid of all vacuumable relations. (While vacuum.c processes
17897 : : * TOAST tables semi-independently, here we see them only as children
17898 : : * of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the
17899 : : * child toast table is handled below.)
17900 : : */
17901 [ + + ]: 6230 : if (dopt->binary_upgrade &&
17902 [ + + ]: 874 : (tbinfo->relkind == RELKIND_RELATION ||
17903 [ + + ]: 118 : tbinfo->relkind == RELKIND_MATVIEW))
17904 : : {
17905 : 774 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
17906 : 774 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17907 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17908 : : "WHERE oid = ",
17909 : 774 : tbinfo->frozenxid, tbinfo->minmxid);
17910 : 774 : appendStringLiteralAH(q, qualrelname, fout);
17911 : 774 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17912 : :
17913 [ + + ]: 774 : if (tbinfo->toast_oid)
17914 : : {
17915 : : /*
17916 : : * The toast table will have the same OID at restore, so we
17917 : : * can safely target it by OID.
17918 : : */
17919 : 302 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
17920 : 302 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17921 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17922 : : "WHERE oid = '%u';\n",
17923 : 302 : tbinfo->toast_frozenxid,
17924 : 302 : tbinfo->toast_minmxid, tbinfo->toast_oid);
17925 : : }
17926 : : }
17927 : :
17928 : : /*
17929 : : * In binary_upgrade mode, restore matviews' populated status by
17930 : : * poking pg_class directly. This is pretty ugly, but we can't use
17931 : : * REFRESH MATERIALIZED VIEW since it's possible that some underlying
17932 : : * matview is not populated even though this matview is; in any case,
17933 : : * we want to transfer the matview's heap storage, not run REFRESH.
17934 : : */
17935 [ + + + + ]: 6230 : if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
17936 [ + + ]: 18 : tbinfo->relispopulated)
17937 : : {
17938 : 16 : appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n");
17939 : 16 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n"
17940 : : "SET relispopulated = 't'\n"
17941 : : "WHERE oid = ");
17942 : 16 : appendStringLiteralAH(q, qualrelname, fout);
17943 : 16 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17944 : : }
17945 : :
17946 : : /*
17947 : : * Dump additional per-column properties that we can't handle in the
17948 : : * main CREATE TABLE command.
17949 : : */
17950 [ + + ]: 27759 : for (j = 0; j < tbinfo->numatts; j++)
17951 : : {
17952 : : /* None of this applies to dropped columns */
17953 [ + + ]: 21529 : if (tbinfo->attisdropped[j])
17954 : 550 : continue;
17955 : :
17956 : : /*
17957 : : * Dump per-column statistics information. We only issue an ALTER
17958 : : * TABLE statement if the attstattarget entry for this column is
17959 : : * not the default value.
17960 : : */
17961 [ + + ]: 20979 : if (tbinfo->attstattarget[j] >= 0)
17962 : 34 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n",
17963 : : foreign, qualrelname,
17964 : 34 : fmtId(tbinfo->attnames[j]),
17965 : 34 : tbinfo->attstattarget[j]);
17966 : :
17967 : : /*
17968 : : * Dump per-column storage information. The statement is only
17969 : : * dumped if the storage has been changed from the type's default.
17970 : : */
17971 [ + + ]: 20979 : if (tbinfo->attstorage[j] != tbinfo->typstorage[j])
17972 : : {
17973 [ + + - + : 83 : switch (tbinfo->attstorage[j])
- ]
17974 : : {
17975 : 10 : case TYPSTORAGE_PLAIN:
17976 : 10 : storage = "PLAIN";
17977 : 10 : break;
17978 : 39 : case TYPSTORAGE_EXTERNAL:
17979 : 39 : storage = "EXTERNAL";
17980 : 39 : break;
17981 : 0 : case TYPSTORAGE_EXTENDED:
17982 : 0 : storage = "EXTENDED";
17983 : 0 : break;
17984 : 34 : case TYPSTORAGE_MAIN:
17985 : 34 : storage = "MAIN";
17986 : 34 : break;
17987 : 0 : default:
17988 : 0 : storage = NULL;
17989 : : }
17990 : :
17991 : : /*
17992 : : * Only dump the statement if it's a storage type we recognize
17993 : : */
17994 [ + - ]: 83 : if (storage != NULL)
17995 : 83 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n",
17996 : : foreign, qualrelname,
17997 : 83 : fmtId(tbinfo->attnames[j]),
17998 : : storage);
17999 : : }
18000 : :
18001 : : /*
18002 : : * Dump per-column compression, if it's been set.
18003 : : */
18004 [ + + ]: 20979 : if (!dopt->no_toast_compression)
18005 : : {
18006 : : const char *cmname;
18007 : :
18008 [ + + + ]: 20874 : switch (tbinfo->attcompression[j])
18009 : : {
18010 : 73 : case 'p':
18011 : 73 : cmname = "pglz";
18012 : 73 : break;
18013 : 39 : case 'l':
18014 : 39 : cmname = "lz4";
18015 : 39 : break;
18016 : 20762 : default:
18017 : 20762 : cmname = NULL;
18018 : 20762 : break;
18019 : : }
18020 : :
18021 [ + + ]: 20874 : if (cmname != NULL)
18022 : 112 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET COMPRESSION %s;\n",
18023 : : foreign, qualrelname,
18024 : 112 : fmtId(tbinfo->attnames[j]),
18025 : : cmname);
18026 : : }
18027 : :
18028 : : /*
18029 : : * Dump per-column attributes.
18030 : : */
18031 [ + + ]: 20979 : if (tbinfo->attoptions[j][0] != '\0')
18032 : 50 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n",
18033 : : foreign, qualrelname,
18034 : 50 : fmtId(tbinfo->attnames[j]),
18035 : 50 : tbinfo->attoptions[j]);
18036 : :
18037 : : /*
18038 : : * Dump per-column fdw options.
18039 : : */
18040 [ + + ]: 20979 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
18041 [ + + ]: 38 : tbinfo->attfdwoptions[j][0] != '\0')
18042 : 34 : appendPQExpBuffer(q,
18043 : : "ALTER FOREIGN TABLE ONLY %s ALTER COLUMN %s OPTIONS (\n"
18044 : : " %s\n"
18045 : : ");\n",
18046 : : qualrelname,
18047 : 34 : fmtId(tbinfo->attnames[j]),
18048 : 34 : tbinfo->attfdwoptions[j]);
18049 : : } /* end loop over columns */
18050 : :
18051 : 6230 : pg_free(partkeydef);
18052 : 6230 : pg_free(ftoptions);
18053 : 6230 : pg_free(srvname);
18054 : : }
18055 : :
18056 : : /*
18057 : : * dump properties we only have ALTER TABLE syntax for
18058 : : */
18059 [ + + ]: 6806 : if ((tbinfo->relkind == RELKIND_RELATION ||
18060 [ + + ]: 1588 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE ||
18061 [ + + ]: 967 : tbinfo->relkind == RELKIND_MATVIEW) &&
18062 [ + + ]: 6193 : tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT)
18063 : : {
18064 [ + - ]: 192 : if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX)
18065 : : {
18066 : : /* nothing to do, will be set when the index is dumped */
18067 : : }
18068 [ + - ]: 192 : else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING)
18069 : : {
18070 : 192 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n",
18071 : : qualrelname);
18072 : : }
18073 [ # # ]: 0 : else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL)
18074 : : {
18075 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n",
18076 : : qualrelname);
18077 : : }
18078 : : }
18079 : :
18080 [ + + ]: 6806 : if (tbinfo->forcerowsec)
18081 : 5 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n",
18082 : : qualrelname);
18083 : :
18084 [ + + ]: 6806 : if (dopt->binary_upgrade)
18085 : 929 : binary_upgrade_extension_member(q, &tbinfo->dobj,
18086 : : reltypename, qrelname,
18087 : 929 : tbinfo->dobj.namespace->dobj.name);
18088 : :
18089 [ + - ]: 6806 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18090 : : {
18091 : 6806 : char *tablespace = NULL;
18092 : 6806 : char *tableam = NULL;
18093 : :
18094 : : /*
18095 : : * Do not emit "ALTER TABLE ... OWNER TO ..." for a conflict log
18096 : : * table, which is a table of the pg_conflict schema. Changing its
18097 : : * owner is not allowed. Its owner follows the owner of the
18098 : : * subscription, and is set when the ALTER SUBSCRIPTION ... OWNER TO
18099 : : * of the dump is restored.
18100 : : */
18101 : 6806 : bool conflict_log_table =
18102 : 6806 : strcmp(tbinfo->dobj.namespace->dobj.name, "pg_conflict") == 0;
18103 : :
18104 : : /*
18105 : : * _selectTablespace() relies on tablespace-enabled objects in the
18106 : : * default tablespace to have a tablespace of "" (empty string) versus
18107 : : * non-tablespace-enabled objects to have a tablespace of NULL.
18108 : : * getTables() sets tbinfo->reltablespace to "" for the default
18109 : : * tablespace (not NULL).
18110 : : */
18111 [ + + + - : 6806 : if (RELKIND_HAS_TABLESPACE(tbinfo->relkind))
+ - + - +
+ + + - +
+ - ]
18112 : 6193 : tablespace = tbinfo->reltablespace;
18113 : :
18114 [ + + + - : 6806 : if (RELKIND_HAS_TABLE_AM(tbinfo->relkind) ||
+ + ]
18115 [ + + ]: 1234 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
18116 : 6193 : tableam = tbinfo->amname;
18117 : :
18118 : 6806 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
18119 [ + + + + ]: 6806 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
18120 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18121 : : .tablespace = tablespace,
18122 : : .tableam = tableam,
18123 : : .relkind = tbinfo->relkind,
18124 : : .owner = conflict_log_table ? NULL : tbinfo->rolname,
18125 : : .description = reltypename,
18126 : : .section = tbinfo->postponed_def ?
18127 : : SECTION_POST_DATA : SECTION_PRE_DATA,
18128 : : .createStmt = q->data,
18129 : : .dropStmt = delq->data));
18130 : : }
18131 : :
18132 : : /* Dump Table Comments */
18133 [ + + ]: 6806 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18134 : 79 : dumpTableComment(fout, tbinfo, reltypename);
18135 : :
18136 : : /* Dump Table Security Labels */
18137 [ - + ]: 6806 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
18138 : 0 : dumpTableSecLabel(fout, tbinfo, reltypename);
18139 : :
18140 : : /*
18141 : : * Dump comments for not-null constraints that aren't to be dumped
18142 : : * separately (those are processed by collectComments/dumpComment).
18143 : : */
18144 [ + - + - ]: 6806 : if (!fout->dopt->no_comments && dopt->dumpSchema &&
18145 [ + - ]: 6806 : fout->remoteVersion >= 180000)
18146 : : {
18147 : 6806 : PQExpBuffer comment = NULL;
18148 : 6806 : PQExpBuffer tag = NULL;
18149 : :
18150 [ + + ]: 32033 : for (j = 0; j < tbinfo->numatts; j++)
18151 : : {
18152 [ + + ]: 25227 : if (tbinfo->notnull_constrs[j] != NULL &&
18153 [ + + ]: 2593 : tbinfo->notnull_comment[j] != NULL)
18154 : : {
18155 [ + - ]: 44 : if (comment == NULL)
18156 : : {
18157 : 44 : comment = createPQExpBuffer();
18158 : 44 : tag = createPQExpBuffer();
18159 : : }
18160 : : else
18161 : : {
18162 : 0 : resetPQExpBuffer(comment);
18163 : 0 : resetPQExpBuffer(tag);
18164 : : }
18165 : :
18166 : 44 : appendPQExpBuffer(comment, "COMMENT ON CONSTRAINT %s ON %s IS ",
18167 : 44 : fmtId(tbinfo->notnull_constrs[j]), qualrelname);
18168 : 44 : appendStringLiteralAH(comment, tbinfo->notnull_comment[j], fout);
18169 : 44 : appendPQExpBufferStr(comment, ";\n");
18170 : :
18171 : 44 : appendPQExpBuffer(tag, "CONSTRAINT %s ON %s",
18172 : 44 : fmtId(tbinfo->notnull_constrs[j]), qrelname);
18173 : :
18174 : 44 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
18175 : 44 : ARCHIVE_OPTS(.tag = tag->data,
18176 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18177 : : .owner = tbinfo->rolname,
18178 : : .description = "COMMENT",
18179 : : .section = SECTION_NONE,
18180 : : .createStmt = comment->data,
18181 : : .deps = &(tbinfo->dobj.dumpId),
18182 : : .nDeps = 1));
18183 : : }
18184 : : }
18185 : :
18186 : 6806 : destroyPQExpBuffer(comment);
18187 : 6806 : destroyPQExpBuffer(tag);
18188 : : }
18189 : :
18190 : : /* Dump comments on inlined table constraints */
18191 [ + + ]: 7399 : for (j = 0; j < tbinfo->ncheck; j++)
18192 : : {
18193 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
18194 : :
18195 [ + + + + ]: 593 : if (constr->separate || !constr->conislocal)
18196 : 254 : continue;
18197 : :
18198 [ + + ]: 339 : if (constr->dobj.dump & DUMP_COMPONENT_COMMENT)
18199 : 39 : dumpTableConstraintComment(fout, constr);
18200 : : }
18201 : :
18202 : 6806 : destroyPQExpBuffer(q);
18203 : 6806 : destroyPQExpBuffer(delq);
18204 : 6806 : destroyPQExpBuffer(extra);
18205 : 6806 : pg_free(qrelname);
18206 : 6806 : pg_free(qualrelname);
18207 : 6806 : }
18208 : :
18209 : : /*
18210 : : * dumpTableAttach
18211 : : * write to fout the commands to attach a child partition
18212 : : *
18213 : : * Child partitions are always made by creating them separately
18214 : : * and then using ATTACH PARTITION, rather than using
18215 : : * CREATE TABLE ... PARTITION OF. This is important for preserving
18216 : : * any possible discrepancy in column layout, to allow assigning the
18217 : : * correct tablespace if different, and so that it's possible to restore
18218 : : * a partition without restoring its parent. (You'll get an error from
18219 : : * the ATTACH PARTITION command, but that can be ignored, or skipped
18220 : : * using "pg_restore -L" if you prefer.) The last point motivates
18221 : : * treating ATTACH PARTITION as a completely separate ArchiveEntry
18222 : : * rather than emitting it within the child partition's ArchiveEntry.
18223 : : */
18224 : : static void
18225 : 1475 : dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo)
18226 : : {
18227 : 1475 : DumpOptions *dopt = fout->dopt;
18228 : : PQExpBuffer q;
18229 : : PGresult *res;
18230 : : char *partbound;
18231 : :
18232 : : /* Do nothing if not dumping schema */
18233 [ + + ]: 1475 : if (!dopt->dumpSchema)
18234 : 57 : return;
18235 : :
18236 : 1418 : q = createPQExpBuffer();
18237 : :
18238 [ + + ]: 1418 : if (!fout->is_prepared[PREPQUERY_DUMPTABLEATTACH])
18239 : : {
18240 : : /* Set up query for partbound details */
18241 : 62 : appendPQExpBufferStr(q,
18242 : : "PREPARE dumpTableAttach(pg_catalog.oid) AS\n");
18243 : :
18244 : 62 : appendPQExpBufferStr(q,
18245 : : "SELECT pg_get_expr(c.relpartbound, c.oid) "
18246 : : "FROM pg_class c "
18247 : : "WHERE c.oid = $1");
18248 : :
18249 : 62 : ExecuteSqlStatement(fout, q->data);
18250 : :
18251 : 62 : fout->is_prepared[PREPQUERY_DUMPTABLEATTACH] = true;
18252 : : }
18253 : :
18254 : 1418 : printfPQExpBuffer(q,
18255 : : "EXECUTE dumpTableAttach('%u')",
18256 : 1418 : attachinfo->partitionTbl->dobj.catId.oid);
18257 : :
18258 : 1418 : res = ExecuteSqlQueryForSingleRow(fout, q->data);
18259 : 1418 : partbound = PQgetvalue(res, 0, 0);
18260 : :
18261 : : /* Perform ALTER TABLE on the parent */
18262 : 1418 : printfPQExpBuffer(q,
18263 : : "ALTER TABLE ONLY %s ",
18264 : 1418 : fmtQualifiedDumpable(attachinfo->parentTbl));
18265 : 1418 : appendPQExpBuffer(q,
18266 : : "ATTACH PARTITION %s %s;\n",
18267 : 1418 : fmtQualifiedDumpable(attachinfo->partitionTbl),
18268 : : partbound);
18269 : :
18270 : : /*
18271 : : * There is no point in creating a drop query as the drop is done by table
18272 : : * drop. (If you think to change this, see also _printTocEntry().)
18273 : : * Although this object doesn't really have ownership as such, set the
18274 : : * owner field anyway to ensure that the command is run by the correct
18275 : : * role at restore time.
18276 : : */
18277 : 1418 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18278 : 1418 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18279 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18280 : : .owner = attachinfo->partitionTbl->rolname,
18281 : : .description = "TABLE ATTACH",
18282 : : .section = SECTION_PRE_DATA,
18283 : : .createStmt = q->data));
18284 : :
18285 : 1418 : PQclear(res);
18286 : 1418 : destroyPQExpBuffer(q);
18287 : : }
18288 : :
18289 : : /*
18290 : : * dumpAttrDef --- dump an attribute's default-value declaration
18291 : : */
18292 : : static void
18293 : 1143 : dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo)
18294 : : {
18295 : 1143 : DumpOptions *dopt = fout->dopt;
18296 : 1143 : TableInfo *tbinfo = adinfo->adtable;
18297 : 1143 : int adnum = adinfo->adnum;
18298 : : PQExpBuffer q;
18299 : : PQExpBuffer delq;
18300 : : char *qualrelname;
18301 : : char *tag;
18302 : : char *foreign;
18303 : :
18304 : : /* Do nothing if not dumping schema */
18305 [ - + ]: 1143 : if (!dopt->dumpSchema)
18306 : 0 : return;
18307 : :
18308 : : /* Skip if not "separate"; it was dumped in the table's definition */
18309 [ + + ]: 1143 : if (!adinfo->separate)
18310 : 971 : return;
18311 : :
18312 : 172 : q = createPQExpBuffer();
18313 : 172 : delq = createPQExpBuffer();
18314 : :
18315 : 172 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
18316 : :
18317 [ - + ]: 172 : foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18318 : :
18319 : 172 : appendPQExpBuffer(q,
18320 : : "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
18321 : 172 : foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
18322 : 172 : adinfo->adef_expr);
18323 : :
18324 : 172 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
18325 : : foreign, qualrelname,
18326 : 172 : fmtId(tbinfo->attnames[adnum - 1]));
18327 : :
18328 : 172 : tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
18329 : :
18330 [ + - ]: 172 : if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18331 : 172 : ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
18332 : 172 : ARCHIVE_OPTS(.tag = tag,
18333 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18334 : : .owner = tbinfo->rolname,
18335 : : .description = "DEFAULT",
18336 : : .section = SECTION_PRE_DATA,
18337 : : .createStmt = q->data,
18338 : : .dropStmt = delq->data));
18339 : :
18340 : 172 : pfree(tag);
18341 : 172 : destroyPQExpBuffer(q);
18342 : 172 : destroyPQExpBuffer(delq);
18343 : 172 : pg_free(qualrelname);
18344 : : }
18345 : :
18346 : : /*
18347 : : * getAttrName: extract the correct name for an attribute
18348 : : *
18349 : : * The array tblInfo->attnames[] only provides names of user attributes;
18350 : : * if a system attribute number is supplied, we have to fake it.
18351 : : * We also do a little bit of bounds checking for safety's sake.
18352 : : */
18353 : : static const char *
18354 : 2111 : getAttrName(int attrnum, const TableInfo *tblInfo)
18355 : : {
18356 [ + - + - ]: 2111 : if (attrnum > 0 && attrnum <= tblInfo->numatts)
18357 : 2111 : return tblInfo->attnames[attrnum - 1];
18358 [ # # # # : 0 : switch (attrnum)
# # # ]
18359 : : {
18360 : 0 : case SelfItemPointerAttributeNumber:
18361 : 0 : return "ctid";
18362 : 0 : case MinTransactionIdAttributeNumber:
18363 : 0 : return "xmin";
18364 : 0 : case MinCommandIdAttributeNumber:
18365 : 0 : return "cmin";
18366 : 0 : case MaxTransactionIdAttributeNumber:
18367 : 0 : return "xmax";
18368 : 0 : case MaxCommandIdAttributeNumber:
18369 : 0 : return "cmax";
18370 : 0 : case TableOidAttributeNumber:
18371 : 0 : return "tableoid";
18372 : : }
18373 : 0 : pg_fatal("invalid column number %d for table \"%s\"",
18374 : : attrnum, tblInfo->dobj.name);
18375 : : return NULL; /* keep compiler quiet */
18376 : : }
18377 : :
18378 : : /*
18379 : : * appendIndexStatTargets
18380 : : * append ALTER INDEX ... SET STATISTICS commands for the per-column
18381 : : * statistics targets of an index (attstattarget), if any.
18382 : : */
18383 : : static void
18384 : 2656 : appendIndexStatTargets(PQExpBuffer q, const IndxInfo *indxinfo)
18385 : : {
18386 : 2656 : char **indstatcolsarray = NULL;
18387 : 2656 : char **indstatvalsarray = NULL;
18388 : 2656 : int nstatcols = 0;
18389 : 2656 : int nstatvals = 0;
18390 : :
18391 [ + + ]: 2656 : if (strlen(indxinfo->indstatcols) == 0 &&
18392 [ + - ]: 2588 : strlen(indxinfo->indstatvals) == 0)
18393 : 2588 : return;
18394 : :
18395 [ - + ]: 68 : if (!parsePGArray(indxinfo->indstatcols, &indstatcolsarray, &nstatcols))
18396 : 0 : pg_fatal("could not parse index statistic columns");
18397 [ - + ]: 68 : if (!parsePGArray(indxinfo->indstatvals, &indstatvalsarray, &nstatvals))
18398 : 0 : pg_fatal("could not parse index statistic values");
18399 [ - + ]: 68 : if (nstatcols != nstatvals)
18400 : 0 : pg_fatal("mismatched number of columns and values for index statistics");
18401 : :
18402 [ + + ]: 204 : for (int j = 0; j < nstatcols; j++)
18403 : : {
18404 : 136 : appendPQExpBuffer(q, "ALTER INDEX %s ",
18405 : 136 : fmtQualifiedDumpable(indxinfo));
18406 : :
18407 : : /*
18408 : : * Note that this is a column number, so no quotes should be used.
18409 : : */
18410 : 136 : appendPQExpBuffer(q, "ALTER COLUMN %s ",
18411 : 136 : indstatcolsarray[j]);
18412 : 136 : appendPQExpBuffer(q, "SET STATISTICS %s;\n",
18413 : 136 : indstatvalsarray[j]);
18414 : : }
18415 : :
18416 : 68 : free(indstatcolsarray);
18417 : 68 : free(indstatvalsarray);
18418 : : }
18419 : :
18420 : : /*
18421 : : * dumpIndex
18422 : : * write out to fout a user-defined index
18423 : : */
18424 : : static void
18425 : 2791 : dumpIndex(Archive *fout, const IndxInfo *indxinfo)
18426 : : {
18427 : 2791 : DumpOptions *dopt = fout->dopt;
18428 : 2791 : TableInfo *tbinfo = indxinfo->indextable;
18429 : 2791 : bool is_constraint = (indxinfo->indexconstraint != 0);
18430 : : PQExpBuffer q;
18431 : : PQExpBuffer delq;
18432 : : char *qindxname;
18433 : : char *qqindxname;
18434 : :
18435 : : /* Do nothing if not dumping schema */
18436 [ + + ]: 2791 : if (!dopt->dumpSchema)
18437 : 135 : return;
18438 : :
18439 : 2656 : q = createPQExpBuffer();
18440 : 2656 : delq = createPQExpBuffer();
18441 : :
18442 : 2656 : qindxname = pg_strdup(fmtId(indxinfo->dobj.name));
18443 : 2656 : qqindxname = pg_strdup(fmtQualifiedDumpable(indxinfo));
18444 : :
18445 : : /*
18446 : : * If there's an associated constraint, don't dump the index per se, but
18447 : : * do dump any comment for it. (This is safe because dependency ordering
18448 : : * will have ensured the constraint is emitted first.) Note that the
18449 : : * emitted comment has to be shown as depending on the constraint, not the
18450 : : * index, in such cases.
18451 : : */
18452 [ + + ]: 2656 : if (!is_constraint)
18453 : : {
18454 [ + + ]: 1132 : if (dopt->binary_upgrade)
18455 : 172 : binary_upgrade_set_pg_class_oids(fout, q,
18456 : 172 : indxinfo->dobj.catId.oid);
18457 : :
18458 : : /* Plain secondary index */
18459 : 1132 : appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
18460 : :
18461 : : /*
18462 : : * Append ALTER TABLE commands as needed to set properties that we
18463 : : * only have ALTER TABLE syntax for. Keep this in sync with the
18464 : : * similar code in dumpConstraint!
18465 : : */
18466 : :
18467 : : /* If the index is clustered, we need to record that. */
18468 [ + + ]: 1132 : if (indxinfo->indisclustered)
18469 : : {
18470 : 5 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
18471 : 5 : fmtQualifiedDumpable(tbinfo));
18472 : : /* index name is not qualified in this syntax */
18473 : 5 : appendPQExpBuffer(q, " ON %s;\n",
18474 : : qindxname);
18475 : : }
18476 : :
18477 : : /* Per-column statistics targets, if any */
18478 : 1132 : appendIndexStatTargets(q, indxinfo);
18479 : :
18480 : : /* Indexes can depend on extensions */
18481 : 1132 : append_depends_on_extension(fout, q, &indxinfo->dobj,
18482 : : "pg_catalog.pg_class",
18483 : : "INDEX", qqindxname);
18484 : :
18485 : : /* If the index defines identity, we need to record that. */
18486 [ - + ]: 1132 : if (indxinfo->indisreplident)
18487 : : {
18488 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
18489 : 0 : fmtQualifiedDumpable(tbinfo));
18490 : : /* index name is not qualified in this syntax */
18491 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
18492 : : qindxname);
18493 : : }
18494 : :
18495 : : /*
18496 : : * If this index is a member of a partitioned index, the backend will
18497 : : * not allow us to drop it separately, so don't try. It will go away
18498 : : * automatically when we drop either the index's table or the
18499 : : * partitioned index. (If, in a selective restore with --clean, we
18500 : : * drop neither of those, then this index will not be dropped either.
18501 : : * But that's fine, and even if you think it's not, the backend won't
18502 : : * let us do differently.)
18503 : : */
18504 [ + + ]: 1132 : if (indxinfo->parentidx == 0)
18505 : 912 : appendPQExpBuffer(delq, "DROP INDEX %s;\n", qqindxname);
18506 : :
18507 [ + - ]: 1132 : if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18508 : 1132 : ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
18509 : 1132 : ARCHIVE_OPTS(.tag = indxinfo->dobj.name,
18510 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18511 : : .tablespace = indxinfo->tablespace,
18512 : : .owner = tbinfo->rolname,
18513 : : .description = "INDEX",
18514 : : .section = SECTION_POST_DATA,
18515 : : .createStmt = q->data,
18516 : : .dropStmt = delq->data));
18517 : : }
18518 : :
18519 : : /* Dump Index Comments */
18520 [ + + ]: 2656 : if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18521 [ + + ]: 15 : dumpComment(fout, "INDEX", qindxname,
18522 : 15 : tbinfo->dobj.namespace->dobj.name,
18523 : : tbinfo->rolname,
18524 : : indxinfo->dobj.catId, 0,
18525 : : is_constraint ? indxinfo->indexconstraint :
18526 : : indxinfo->dobj.dumpId);
18527 : :
18528 : 2656 : destroyPQExpBuffer(q);
18529 : 2656 : destroyPQExpBuffer(delq);
18530 : 2656 : pg_free(qindxname);
18531 : 2656 : pg_free(qqindxname);
18532 : : }
18533 : :
18534 : : /*
18535 : : * dumpIndexAttach
18536 : : * write out to fout a partitioned-index attachment clause
18537 : : */
18538 : : static void
18539 : 625 : dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo)
18540 : : {
18541 : : /* Do nothing if not dumping schema */
18542 [ + + ]: 625 : if (!fout->dopt->dumpSchema)
18543 : 48 : return;
18544 : :
18545 [ + - ]: 577 : if (attachinfo->partitionIdx->dobj.dump & DUMP_COMPONENT_DEFINITION)
18546 : : {
18547 : 577 : PQExpBuffer q = createPQExpBuffer();
18548 : :
18549 : 577 : appendPQExpBuffer(q, "ALTER INDEX %s ",
18550 : 577 : fmtQualifiedDumpable(attachinfo->parentIdx));
18551 : 577 : appendPQExpBuffer(q, "ATTACH PARTITION %s;\n",
18552 : 577 : fmtQualifiedDumpable(attachinfo->partitionIdx));
18553 : :
18554 : : /*
18555 : : * There is no need for a dropStmt since the drop is done implicitly
18556 : : * when we drop either the index's table or the partitioned index.
18557 : : * Moreover, since there's no ALTER INDEX DETACH PARTITION command,
18558 : : * there's no way to do it anyway. (If you think to change this,
18559 : : * consider also what to do with --if-exists.)
18560 : : *
18561 : : * Although this object doesn't really have ownership as such, set the
18562 : : * owner field anyway to ensure that the command is run by the correct
18563 : : * role at restore time.
18564 : : */
18565 : 577 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18566 : 577 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18567 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18568 : : .owner = attachinfo->parentIdx->indextable->rolname,
18569 : : .description = "INDEX ATTACH",
18570 : : .section = SECTION_POST_DATA,
18571 : : .createStmt = q->data));
18572 : :
18573 : 577 : destroyPQExpBuffer(q);
18574 : : }
18575 : : }
18576 : :
18577 : : /*
18578 : : * dumpStatisticsExt
18579 : : * write out to fout an extended statistics object
18580 : : */
18581 : : static void
18582 : 196 : dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo)
18583 : : {
18584 : 196 : DumpOptions *dopt = fout->dopt;
18585 : : PQExpBuffer q;
18586 : : PQExpBuffer delq;
18587 : : PQExpBuffer query;
18588 : : char *qstatsextname;
18589 : : PGresult *res;
18590 : : char *stxdef;
18591 : :
18592 : : /* Do nothing if not dumping schema */
18593 [ + + ]: 196 : if (!dopt->dumpSchema)
18594 : 28 : return;
18595 : :
18596 : 168 : q = createPQExpBuffer();
18597 : 168 : delq = createPQExpBuffer();
18598 : 168 : query = createPQExpBuffer();
18599 : :
18600 : 168 : qstatsextname = pg_strdup(fmtId(statsextinfo->dobj.name));
18601 : :
18602 : 168 : appendPQExpBuffer(query, "SELECT "
18603 : : "pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)",
18604 : 168 : statsextinfo->dobj.catId.oid);
18605 : :
18606 : 168 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
18607 : :
18608 : 168 : stxdef = PQgetvalue(res, 0, 0);
18609 : :
18610 : : /* Result of pg_get_statisticsobjdef is complete except for semicolon */
18611 : 168 : appendPQExpBuffer(q, "%s;\n", stxdef);
18612 : :
18613 : : /*
18614 : : * We only issue an ALTER STATISTICS statement if the stxstattarget entry
18615 : : * for this statistics object is not the default value.
18616 : : */
18617 [ + + ]: 168 : if (statsextinfo->stattarget >= 0)
18618 : : {
18619 : 34 : appendPQExpBuffer(q, "ALTER STATISTICS %s ",
18620 : 34 : fmtQualifiedDumpable(statsextinfo));
18621 : 34 : appendPQExpBuffer(q, "SET STATISTICS %d;\n",
18622 : 34 : statsextinfo->stattarget);
18623 : : }
18624 : :
18625 : 168 : appendPQExpBuffer(delq, "DROP STATISTICS %s;\n",
18626 : 168 : fmtQualifiedDumpable(statsextinfo));
18627 : :
18628 [ + - ]: 168 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18629 : 168 : ArchiveEntry(fout, statsextinfo->dobj.catId,
18630 : 168 : statsextinfo->dobj.dumpId,
18631 : 168 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18632 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18633 : : .owner = statsextinfo->rolname,
18634 : : .description = "STATISTICS",
18635 : : .section = SECTION_POST_DATA,
18636 : : .createStmt = q->data,
18637 : : .dropStmt = delq->data));
18638 : :
18639 : : /* Dump Statistics Comments */
18640 [ - + ]: 168 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18641 : 0 : dumpComment(fout, "STATISTICS", qstatsextname,
18642 : 0 : statsextinfo->dobj.namespace->dobj.name,
18643 : 0 : statsextinfo->rolname,
18644 : : statsextinfo->dobj.catId, 0,
18645 : 0 : statsextinfo->dobj.dumpId);
18646 : :
18647 : 168 : PQclear(res);
18648 : 168 : destroyPQExpBuffer(q);
18649 : 168 : destroyPQExpBuffer(delq);
18650 : 168 : destroyPQExpBuffer(query);
18651 : 168 : pg_free(qstatsextname);
18652 : : }
18653 : :
18654 : : /*
18655 : : * dumpStatisticsExtStats
18656 : : * write out to fout the stats for an extended statistics object
18657 : : */
18658 : : static void
18659 : 196 : dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo)
18660 : : {
18661 : 196 : DumpOptions *dopt = fout->dopt;
18662 : : PQExpBuffer query;
18663 : : PGresult *res;
18664 : : int nstats;
18665 : :
18666 : : /* Do nothing if not dumping statistics */
18667 [ + + ]: 196 : if (!dopt->dumpStatistics)
18668 : 53 : return;
18669 : :
18670 [ + + ]: 143 : if (!fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS])
18671 : : {
18672 : 36 : PQExpBuffer pq = createPQExpBuffer();
18673 : :
18674 : : /*---------
18675 : : * Set up query for details about extended statistics objects.
18676 : : *
18677 : : * The query depends on the backend version:
18678 : : * - In v19 and newer versions, query directly the pg_stats_ext*
18679 : : * catalogs.
18680 : : * - In v18 and older versions, ndistinct and dependencies have a
18681 : : * different format that needs translation.
18682 : : * - In v14 and older versions, there is no inherited flag, hence
18683 : : * it is deduced from the relation's relkind.
18684 : : * - In v11 and older versions, there is no pg_stats_ext, hence
18685 : : * the logic joins pg_statistic_ext and pg_namespace.
18686 : : *---------
18687 : : */
18688 : :
18689 : 36 : appendPQExpBufferStr(pq,
18690 : : "PREPARE getExtStatsStats(pg_catalog.name, pg_catalog.name) AS\n"
18691 : : "SELECT ");
18692 : :
18693 : : /*
18694 : : * Versions 15 and newer have inherited stats.
18695 : : *
18696 : : * Create this column in all versions because we need to order by it
18697 : : * later.
18698 : : */
18699 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18700 : 36 : appendPQExpBufferStr(pq, "e.inherited, ");
18701 : : else
18702 : 0 : appendPQExpBufferStr(pq, "false AS inherited, ");
18703 : :
18704 : : /*--------
18705 : : * The ndistinct and dependencies formats changed in v19, so
18706 : : * everything before that needs to be translated.
18707 : : *
18708 : : * The ndistinct translation converts this kind of data:
18709 : : * {"3, 4": 11, "3, 6": 11, "4, 6": 11, "3, 4, 6": 11}
18710 : : *
18711 : : * to this:
18712 : : * [ {"attributes": [3,4], "ndistinct": 11},
18713 : : * {"attributes": [3,6], "ndistinct": 11},
18714 : : * {"attributes": [4,6], "ndistinct": 11},
18715 : : * {"attributes": [3,4,6], "ndistinct": 11} ]
18716 : : *
18717 : : * The dependencies translation converts this kind of data:
18718 : : * {"3 => 4": 1.000000, "3 => 6": 1.000000,
18719 : : * "4 => 6": 1.000000, "3, 4 => 6": 1.000000,
18720 : : * "3, 6 => 4": 1.000000}
18721 : : *
18722 : : * to this:
18723 : : * [ {"attributes": [3], "dependency": 4, "degree": 1.000000},
18724 : : * {"attributes": [3], "dependency": 6, "degree": 1.000000},
18725 : : * {"attributes": [4], "dependency": 6, "degree": 1.000000},
18726 : : * {"attributes": [3,4], "dependency": 6, "degree": 1.000000},
18727 : : * {"attributes": [3,6], "dependency": 4, "degree": 1.000000} ]
18728 : : *--------
18729 : : */
18730 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18731 : 36 : appendPQExpBufferStr(pq, "e.n_distinct, e.dependencies, ");
18732 : : else
18733 : 0 : appendPQExpBufferStr(pq,
18734 : : "( "
18735 : : "SELECT json_agg( "
18736 : : " json_build_object( "
18737 : : " '" PG_NDISTINCT_KEY_ATTRIBUTES "', "
18738 : : " string_to_array(kv.key, ', ')::integer[], "
18739 : : " '" PG_NDISTINCT_KEY_NDISTINCT "', "
18740 : : " kv.value::bigint )) "
18741 : : "FROM json_each_text(e.n_distinct::text::json) AS kv"
18742 : : ") AS n_distinct, "
18743 : : "( "
18744 : : "SELECT json_agg( "
18745 : : " json_build_object( "
18746 : : " '" PG_DEPENDENCIES_KEY_ATTRIBUTES "', "
18747 : : " string_to_array( "
18748 : : " split_part(kv.key, ' => ', 1), "
18749 : : " ', ')::integer[], "
18750 : : " '" PG_DEPENDENCIES_KEY_DEPENDENCY "', "
18751 : : " split_part(kv.key, ' => ', 2)::integer, "
18752 : : " '" PG_DEPENDENCIES_KEY_DEGREE "', "
18753 : : " kv.value::double precision )) "
18754 : : "FROM json_each_text(e.dependencies::text::json) AS kv "
18755 : : ") AS dependencies, ");
18756 : :
18757 : : /* MCV was introduced v13 */
18758 [ + - ]: 36 : if (fout->remoteVersion >= 130000)
18759 : 36 : appendPQExpBufferStr(pq,
18760 : : "e.most_common_vals, e.most_common_freqs, "
18761 : : "e.most_common_base_freqs, ");
18762 : : else
18763 : 0 : appendPQExpBufferStr(pq,
18764 : : "NULL AS most_common_vals, NULL AS most_common_freqs, "
18765 : : "NULL AS most_common_base_freqs, ");
18766 : :
18767 : : /* Expressions were introduced in v14 */
18768 [ + - ]: 36 : if (fout->remoteVersion >= 140000)
18769 : : {
18770 : : /*
18771 : : * There is no ordering column in pg_stats_ext_exprs. However, we
18772 : : * can rely on the unnesting of pg_statistic_ext_data.stxdexpr to
18773 : : * maintain the desired order of expression elements.
18774 : : */
18775 : 36 : appendPQExpBufferStr(pq,
18776 : : "( "
18777 : : "SELECT jsonb_pretty(jsonb_agg("
18778 : : "nullif(j.obj, '{}'::jsonb))) "
18779 : : "FROM pg_stats_ext_exprs AS ee "
18780 : : "CROSS JOIN LATERAL jsonb_strip_nulls("
18781 : : " jsonb_build_object( "
18782 : : " 'null_frac', ee.null_frac::text, "
18783 : : " 'avg_width', ee.avg_width::text, "
18784 : : " 'n_distinct', ee.n_distinct::text, "
18785 : : " 'most_common_vals', ee.most_common_vals::text, "
18786 : : " 'most_common_freqs', ee.most_common_freqs::text, "
18787 : : " 'histogram_bounds', ee.histogram_bounds::text, "
18788 : : " 'correlation', ee.correlation::text, "
18789 : : " 'most_common_elems', ee.most_common_elems::text, "
18790 : : " 'most_common_elem_freqs', ee.most_common_elem_freqs::text, "
18791 : : " 'elem_count_histogram', ee.elem_count_histogram::text");
18792 : :
18793 : : /* These three have been added to pg_stats_ext_exprs in v19. */
18794 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18795 : 36 : appendPQExpBufferStr(pq,
18796 : : ", "
18797 : : " 'range_length_histogram', ee.range_length_histogram::text, "
18798 : : " 'range_empty_frac', ee.range_empty_frac::text, "
18799 : : " 'range_bounds_histogram', ee.range_bounds_histogram::text");
18800 : :
18801 : 36 : appendPQExpBufferStr(pq,
18802 : : " )) AS j(obj)"
18803 : : "WHERE ee.statistics_schemaname = $1 "
18804 : : "AND ee.statistics_name = $2 ");
18805 : : /* Inherited expressions introduced in v15 */
18806 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18807 : 36 : appendPQExpBufferStr(pq, "AND ee.inherited = e.inherited");
18808 : :
18809 : 36 : appendPQExpBufferStr(pq, ") AS exprs ");
18810 : : }
18811 : : else
18812 : 0 : appendPQExpBufferStr(pq, "NULL AS exprs ");
18813 : :
18814 : : /* pg_stats_ext introduced in v12 */
18815 [ + - ]: 36 : if (fout->remoteVersion >= 120000)
18816 : 36 : appendPQExpBufferStr(pq,
18817 : : "FROM pg_catalog.pg_stats_ext AS e "
18818 : : "WHERE e.statistics_schemaname = $1 "
18819 : : "AND e.statistics_name = $2 ");
18820 : : else
18821 : 0 : appendPQExpBufferStr(pq,
18822 : : "FROM ( "
18823 : : "SELECT s.stxndistinct AS n_distinct, "
18824 : : " s.stxdependencies AS dependencies "
18825 : : "FROM pg_catalog.pg_statistic_ext AS s "
18826 : : "JOIN pg_catalog.pg_namespace AS n "
18827 : : "ON n.oid = s.stxnamespace "
18828 : : "WHERE n.nspname = $1 "
18829 : : "AND s.stxname = $2 "
18830 : : ") AS e ");
18831 : :
18832 : : /* we always have an inherited column, but it may be a constant */
18833 : 36 : appendPQExpBufferStr(pq, "ORDER BY inherited");
18834 : :
18835 : 36 : ExecuteSqlStatement(fout, pq->data);
18836 : :
18837 : 36 : fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS] = true;
18838 : :
18839 : 36 : destroyPQExpBuffer(pq);
18840 : : }
18841 : :
18842 : 143 : query = createPQExpBuffer();
18843 : :
18844 : 143 : appendPQExpBufferStr(query, "EXECUTE getExtStatsStats(");
18845 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.namespace->dobj.name, fout);
18846 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name, ");
18847 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.name, fout);
18848 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name)");
18849 : :
18850 : 143 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
18851 : :
18852 : 143 : destroyPQExpBuffer(query);
18853 : :
18854 : 143 : nstats = PQntuples(res);
18855 : :
18856 [ + + ]: 143 : if (nstats > 0)
18857 : : {
18858 : 39 : PQExpBuffer out = createPQExpBuffer();
18859 : :
18860 : 39 : int i_inherited = PQfnumber(res, "inherited");
18861 : 39 : int i_ndistinct = PQfnumber(res, "n_distinct");
18862 : 39 : int i_dependencies = PQfnumber(res, "dependencies");
18863 : 39 : int i_mcv = PQfnumber(res, "most_common_vals");
18864 : 39 : int i_mcf = PQfnumber(res, "most_common_freqs");
18865 : 39 : int i_mcbf = PQfnumber(res, "most_common_base_freqs");
18866 : 39 : int i_exprs = PQfnumber(res, "exprs");
18867 : :
18868 [ + + ]: 78 : for (int i = 0; i < nstats; i++)
18869 : : {
18870 : 39 : TableInfo *tbinfo = statsextinfo->stattable;
18871 : : const char *inherited;
18872 : :
18873 [ - + ]: 39 : if (PQgetisnull(res, i, i_inherited))
18874 : 0 : pg_fatal("inherited cannot be NULL");
18875 : :
18876 : : /*
18877 : : * v14 and older have no inherited flag; only partitioned tables
18878 : : * store inherited extended statistics there.
18879 : : */
18880 [ + - ]: 39 : if (fout->remoteVersion >= 150000)
18881 : 39 : inherited = PQgetvalue(res, i, i_inherited);
18882 : : else
18883 : 0 : inherited =
18884 [ # # ]: 0 : (tbinfo->relkind == RELKIND_PARTITIONED_TABLE) ? "t" : "f";
18885 : :
18886 : 39 : appendPQExpBufferStr(out,
18887 : : "SELECT * FROM pg_catalog.pg_restore_extended_stats(\n");
18888 : 39 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
18889 : : fout->remoteVersion);
18890 : :
18891 : : /* Relation information */
18892 : 39 : appendPQExpBufferStr(out, "\t'schemaname', ");
18893 : 39 : appendStringLiteralAH(out, tbinfo->dobj.namespace->dobj.name, fout);
18894 : 39 : appendPQExpBufferStr(out, ",\n\t'relname', ");
18895 : 39 : appendStringLiteralAH(out, tbinfo->dobj.name, fout);
18896 : :
18897 : : /* Extended statistics information */
18898 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_schemaname', ");
18899 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.namespace->dobj.name, fout);
18900 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_name', ");
18901 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.name, fout);
18902 : 39 : appendNamedArgument(out, fout, "inherited", "boolean", inherited);
18903 : :
18904 [ + + ]: 39 : if (!PQgetisnull(res, i, i_ndistinct))
18905 : 35 : appendNamedArgument(out, fout, "n_distinct", "pg_ndistinct",
18906 : 35 : PQgetvalue(res, i, i_ndistinct));
18907 : :
18908 [ + + ]: 39 : if (!PQgetisnull(res, i, i_dependencies))
18909 : 36 : appendNamedArgument(out, fout, "dependencies", "pg_dependencies",
18910 : 36 : PQgetvalue(res, i, i_dependencies));
18911 : :
18912 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcv))
18913 : 38 : appendNamedArgument(out, fout, "most_common_vals", "text[]",
18914 : 38 : PQgetvalue(res, i, i_mcv));
18915 : :
18916 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcf))
18917 : 38 : appendNamedArgument(out, fout, "most_common_freqs", "double precision[]",
18918 : 38 : PQgetvalue(res, i, i_mcf));
18919 : :
18920 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcbf))
18921 : 38 : appendNamedArgument(out, fout, "most_common_base_freqs", "double precision[]",
18922 : 38 : PQgetvalue(res, i, i_mcbf));
18923 : :
18924 [ + + ]: 39 : if (!PQgetisnull(res, i, i_exprs))
18925 : 36 : appendNamedArgument(out, fout, "exprs", "jsonb",
18926 : 36 : PQgetvalue(res, i, i_exprs));
18927 : :
18928 : 39 : appendPQExpBufferStr(out, "\n);\n");
18929 : : }
18930 : :
18931 : 39 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
18932 : 39 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18933 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18934 : : .owner = statsextinfo->rolname,
18935 : : .description = "EXTENDED STATISTICS DATA",
18936 : : .section = SECTION_POST_DATA,
18937 : : .createStmt = out->data,
18938 : : .deps = &statsextinfo->dobj.dumpId,
18939 : : .nDeps = 1));
18940 : 39 : destroyPQExpBuffer(out);
18941 : : }
18942 : 143 : PQclear(res);
18943 : : }
18944 : :
18945 : : /*
18946 : : * dumpConstraint
18947 : : * write out to fout a user-defined constraint
18948 : : */
18949 : : static void
18950 : 2634 : dumpConstraint(Archive *fout, const ConstraintInfo *coninfo)
18951 : : {
18952 : 2634 : DumpOptions *dopt = fout->dopt;
18953 : 2634 : TableInfo *tbinfo = coninfo->contable;
18954 : : PQExpBuffer q;
18955 : : PQExpBuffer delq;
18956 : 2634 : char *tag = NULL;
18957 : : char *foreign;
18958 : :
18959 : : /* Do nothing if not dumping schema */
18960 [ + + ]: 2634 : if (!dopt->dumpSchema)
18961 : 117 : return;
18962 : :
18963 : 2517 : q = createPQExpBuffer();
18964 : 2517 : delq = createPQExpBuffer();
18965 : :
18966 : 4866 : foreign = tbinfo &&
18967 [ + + - + ]: 2517 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18968 : :
18969 [ + + ]: 2517 : if (coninfo->contype == 'p' ||
18970 [ + + ]: 1278 : coninfo->contype == 'u' ||
18971 [ + + ]: 1047 : coninfo->contype == 'x')
18972 : 1524 : {
18973 : : /* Index-related constraint */
18974 : : IndxInfo *indxinfo;
18975 : : int k;
18976 : :
18977 : 1524 : indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
18978 : :
18979 [ - + ]: 1524 : if (indxinfo == NULL)
18980 : 0 : pg_fatal("missing index for constraint \"%s\"",
18981 : : coninfo->dobj.name);
18982 : :
18983 [ + + ]: 1524 : if (dopt->binary_upgrade)
18984 : 154 : binary_upgrade_set_pg_class_oids(fout, q,
18985 : : indxinfo->dobj.catId.oid);
18986 : :
18987 : 1524 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
18988 : 1524 : fmtQualifiedDumpable(tbinfo));
18989 : 1524 : appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
18990 : 1524 : fmtId(coninfo->dobj.name));
18991 : :
18992 [ + + ]: 1524 : if (coninfo->condef)
18993 : : {
18994 : : /* pg_get_constraintdef should have provided everything */
18995 : 54 : appendPQExpBuffer(q, "%s;\n", coninfo->condef);
18996 : : }
18997 : : else
18998 : : {
18999 : 1470 : appendPQExpBufferStr(q,
19000 [ + + ]: 1470 : coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
19001 : :
19002 : : /*
19003 : : * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
19004 : : * indexes. Being able to create this was fixed, but we need to
19005 : : * make the index distinct in order to be able to restore the
19006 : : * dump.
19007 : : */
19008 [ - + - - ]: 1470 : if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
19009 : 0 : appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
19010 : 1470 : appendPQExpBufferStr(q, " (");
19011 [ + + ]: 3541 : for (k = 0; k < indxinfo->indnkeyattrs; k++)
19012 : : {
19013 : 2071 : int indkey = indxinfo->indkeys[k];
19014 : : const char *attname;
19015 : :
19016 [ - + ]: 2071 : if (indkey == InvalidAttrNumber)
19017 : 0 : break;
19018 : 2071 : attname = getAttrName(indkey, tbinfo);
19019 : :
19020 [ + + ]: 2071 : appendPQExpBuffer(q, "%s%s",
19021 : : (k == 0) ? "" : ", ",
19022 : : fmtId(attname));
19023 : : }
19024 [ + + ]: 1470 : if (coninfo->conperiod)
19025 : 108 : appendPQExpBufferStr(q, " WITHOUT OVERLAPS");
19026 : :
19027 [ + + ]: 1470 : if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
19028 : 20 : appendPQExpBufferStr(q, ") INCLUDE (");
19029 : :
19030 [ + + ]: 1510 : for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
19031 : : {
19032 : 40 : int indkey = indxinfo->indkeys[k];
19033 : : const char *attname;
19034 : :
19035 [ - + ]: 40 : if (indkey == InvalidAttrNumber)
19036 : 0 : break;
19037 : 40 : attname = getAttrName(indkey, tbinfo);
19038 : :
19039 : 80 : appendPQExpBuffer(q, "%s%s",
19040 [ + + ]: 40 : (k == indxinfo->indnkeyattrs) ? "" : ", ",
19041 : : fmtId(attname));
19042 : : }
19043 : :
19044 : 1470 : appendPQExpBufferChar(q, ')');
19045 : :
19046 [ - + ]: 1470 : if (nonemptyReloptions(indxinfo->indreloptions))
19047 : : {
19048 : 0 : appendPQExpBufferStr(q, " WITH (");
19049 : 0 : appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
19050 : 0 : appendPQExpBufferChar(q, ')');
19051 : : }
19052 : :
19053 [ + + ]: 1470 : if (coninfo->condeferrable)
19054 : : {
19055 : 25 : appendPQExpBufferStr(q, " DEFERRABLE");
19056 [ + + ]: 25 : if (coninfo->condeferred)
19057 : 15 : appendPQExpBufferStr(q, " INITIALLY DEFERRED");
19058 : : }
19059 : :
19060 : 1470 : appendPQExpBufferStr(q, ";\n");
19061 : : }
19062 : :
19063 : : /*
19064 : : * Append ALTER TABLE commands as needed to set properties that we
19065 : : * only have ALTER TABLE syntax for. Keep this in sync with the
19066 : : * similar code in dumpIndex!
19067 : : */
19068 : :
19069 : : /* If the index is clustered, we need to record that. */
19070 [ + + ]: 1524 : if (indxinfo->indisclustered)
19071 : : {
19072 : 34 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
19073 : 34 : fmtQualifiedDumpable(tbinfo));
19074 : : /* index name is not qualified in this syntax */
19075 : 34 : appendPQExpBuffer(q, " ON %s;\n",
19076 : 34 : fmtId(indxinfo->dobj.name));
19077 : : }
19078 : :
19079 : : /* Per-column statistics targets, if any */
19080 : 1524 : appendIndexStatTargets(q, indxinfo);
19081 : :
19082 : : /* If the index defines identity, we need to record that. */
19083 [ - + ]: 1524 : if (indxinfo->indisreplident)
19084 : : {
19085 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
19086 : 0 : fmtQualifiedDumpable(tbinfo));
19087 : : /* index name is not qualified in this syntax */
19088 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
19089 : 0 : fmtId(indxinfo->dobj.name));
19090 : : }
19091 : :
19092 : : /* Indexes can depend on extensions */
19093 : 1524 : append_depends_on_extension(fout, q, &indxinfo->dobj,
19094 : : "pg_catalog.pg_class", "INDEX",
19095 : 1524 : fmtQualifiedDumpable(indxinfo));
19096 : :
19097 : 1524 : appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
19098 : 1524 : fmtQualifiedDumpable(tbinfo));
19099 : 1524 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19100 : 1524 : fmtId(coninfo->dobj.name));
19101 : :
19102 : 1524 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
19103 : :
19104 [ + - ]: 1524 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19105 : 1524 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19106 : 1524 : ARCHIVE_OPTS(.tag = tag,
19107 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19108 : : .tablespace = indxinfo->tablespace,
19109 : : .owner = tbinfo->rolname,
19110 : : .description = "CONSTRAINT",
19111 : : .section = SECTION_POST_DATA,
19112 : : .createStmt = q->data,
19113 : : .dropStmt = delq->data));
19114 : : }
19115 [ + + ]: 993 : else if (coninfo->contype == 'f')
19116 : : {
19117 : : char *only;
19118 : :
19119 : : /*
19120 : : * Foreign keys on partitioned tables are always declared as
19121 : : * inheriting to partitions; for all other cases, emit them as
19122 : : * applying ONLY directly to the named table, because that's how they
19123 : : * work for regular inherited tables.
19124 : : */
19125 [ + + ]: 163 : only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
19126 : :
19127 : : /*
19128 : : * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
19129 : : * current table data is not processed
19130 : : */
19131 : 163 : appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
19132 : 163 : only, fmtQualifiedDumpable(tbinfo));
19133 : 163 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19134 : 163 : fmtId(coninfo->dobj.name),
19135 : 163 : coninfo->condef);
19136 : :
19137 : 163 : appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
19138 : 163 : only, fmtQualifiedDumpable(tbinfo));
19139 : 163 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19140 : 163 : fmtId(coninfo->dobj.name));
19141 : :
19142 : 163 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
19143 : :
19144 [ + - ]: 163 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19145 : 163 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19146 : 163 : ARCHIVE_OPTS(.tag = tag,
19147 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19148 : : .owner = tbinfo->rolname,
19149 : : .description = "FK CONSTRAINT",
19150 : : .section = SECTION_POST_DATA,
19151 : : .createStmt = q->data,
19152 : : .dropStmt = delq->data));
19153 : : }
19154 [ + + + - : 830 : else if ((coninfo->contype == 'c' || coninfo->contype == 'n') && tbinfo)
+ + ]
19155 : : {
19156 : : /* CHECK or invalid not-null constraint on a table */
19157 : :
19158 : : /* Ignore if not to be dumped separately, or if it was inherited */
19159 [ + + + + ]: 662 : if (coninfo->separate && coninfo->conislocal)
19160 : : {
19161 : : const char *keyword;
19162 : :
19163 [ + + ]: 109 : if (coninfo->contype == 'c')
19164 : 45 : keyword = "CHECK CONSTRAINT";
19165 : : else
19166 : 64 : keyword = "CONSTRAINT";
19167 : :
19168 : : /* not ONLY since we want it to propagate to children */
19169 : 109 : appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
19170 : 109 : fmtQualifiedDumpable(tbinfo));
19171 : 109 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19172 : 109 : fmtId(coninfo->dobj.name),
19173 : 109 : coninfo->condef);
19174 : :
19175 : 109 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
19176 : 109 : fmtQualifiedDumpable(tbinfo));
19177 : 109 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19178 : 109 : fmtId(coninfo->dobj.name));
19179 : :
19180 : 109 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
19181 : :
19182 [ + - ]: 109 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19183 : 109 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19184 : 109 : ARCHIVE_OPTS(.tag = tag,
19185 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19186 : : .owner = tbinfo->rolname,
19187 : : .description = keyword,
19188 : : .section = SECTION_POST_DATA,
19189 : : .createStmt = q->data,
19190 : : .dropStmt = delq->data));
19191 : : }
19192 : : }
19193 [ + - ]: 168 : else if (tbinfo == NULL)
19194 : : {
19195 : : /* CHECK, NOT NULL constraint on a domain */
19196 : 168 : TypeInfo *tyinfo = coninfo->condomain;
19197 : :
19198 : : Assert(coninfo->contype == 'c' || coninfo->contype == 'n');
19199 : :
19200 : : /* Ignore if not to be dumped separately */
19201 [ + + ]: 168 : if (coninfo->separate)
19202 : : {
19203 : : const char *keyword;
19204 : :
19205 [ + - ]: 5 : if (coninfo->contype == 'c')
19206 : 5 : keyword = "CHECK CONSTRAINT";
19207 : : else
19208 : 0 : keyword = "CONSTRAINT";
19209 : :
19210 : 5 : appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
19211 : 5 : fmtQualifiedDumpable(tyinfo));
19212 : 5 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19213 : 5 : fmtId(coninfo->dobj.name),
19214 : 5 : coninfo->condef);
19215 : :
19216 : 5 : appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
19217 : 5 : fmtQualifiedDumpable(tyinfo));
19218 : 5 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19219 : 5 : fmtId(coninfo->dobj.name));
19220 : :
19221 : 5 : tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
19222 : :
19223 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19224 : 5 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19225 : 5 : ARCHIVE_OPTS(.tag = tag,
19226 : : .namespace = tyinfo->dobj.namespace->dobj.name,
19227 : : .owner = tyinfo->rolname,
19228 : : .description = keyword,
19229 : : .section = SECTION_POST_DATA,
19230 : : .createStmt = q->data,
19231 : : .dropStmt = delq->data));
19232 : :
19233 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19234 : : {
19235 : 5 : PQExpBuffer conprefix = createPQExpBuffer();
19236 : 5 : char *qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
19237 : :
19238 : 5 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
19239 : 5 : fmtId(coninfo->dobj.name));
19240 : :
19241 : 5 : dumpComment(fout, conprefix->data, qtypname,
19242 : 5 : tyinfo->dobj.namespace->dobj.name,
19243 : : tyinfo->rolname,
19244 : 5 : coninfo->dobj.catId, 0, coninfo->dobj.dumpId);
19245 : 5 : destroyPQExpBuffer(conprefix);
19246 : 5 : pg_free(qtypname);
19247 : : }
19248 : : }
19249 : : }
19250 : : else
19251 : : {
19252 : 0 : pg_fatal("unrecognized constraint type: %c",
19253 : : coninfo->contype);
19254 : : }
19255 : :
19256 : : /* Dump Constraint Comments --- only works for table constraints */
19257 [ + + + + ]: 2517 : if (tbinfo && coninfo->separate &&
19258 [ + + ]: 1826 : coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19259 : 49 : dumpTableConstraintComment(fout, coninfo);
19260 : :
19261 : 2517 : pfree(tag);
19262 : 2517 : destroyPQExpBuffer(q);
19263 : 2517 : destroyPQExpBuffer(delq);
19264 : : }
19265 : :
19266 : : /*
19267 : : * dumpTableConstraintComment --- dump a constraint's comment if any
19268 : : *
19269 : : * This is split out because we need the function in two different places
19270 : : * depending on whether the constraint is dumped as part of CREATE TABLE
19271 : : * or as a separate ALTER command.
19272 : : */
19273 : : static void
19274 : 88 : dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
19275 : : {
19276 : 88 : TableInfo *tbinfo = coninfo->contable;
19277 : 88 : PQExpBuffer conprefix = createPQExpBuffer();
19278 : : char *qtabname;
19279 : :
19280 : 88 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19281 : :
19282 : 88 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON",
19283 : 88 : fmtId(coninfo->dobj.name));
19284 : :
19285 [ + - ]: 88 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19286 : 88 : dumpComment(fout, conprefix->data, qtabname,
19287 : 88 : tbinfo->dobj.namespace->dobj.name,
19288 : : tbinfo->rolname,
19289 : : coninfo->dobj.catId, 0,
19290 [ + + ]: 88 : coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
19291 : :
19292 : 88 : destroyPQExpBuffer(conprefix);
19293 : 88 : pg_free(qtabname);
19294 : 88 : }
19295 : :
19296 : : static inline SeqType
19297 : 663 : parse_sequence_type(const char *name)
19298 : : {
19299 [ + - ]: 1475 : for (size_t i = 0; i < lengthof(SeqTypeNames); i++)
19300 : : {
19301 [ + + ]: 1475 : if (strcmp(SeqTypeNames[i], name) == 0)
19302 : 663 : return (SeqType) i;
19303 : : }
19304 : :
19305 : 0 : pg_fatal("unrecognized sequence type: %s", name);
19306 : : return (SeqType) 0; /* keep compiler quiet */
19307 : : }
19308 : :
19309 : : /*
19310 : : * bsearch() comparator for SequenceItem
19311 : : */
19312 : : static int
19313 : 3008 : SequenceItemCmp(const void *p1, const void *p2)
19314 : : {
19315 : 3008 : SequenceItem v1 = *((const SequenceItem *) p1);
19316 : 3008 : SequenceItem v2 = *((const SequenceItem *) p2);
19317 : :
19318 : 3008 : return pg_cmp_u32(v1.oid, v2.oid);
19319 : : }
19320 : :
19321 : : /*
19322 : : * collectSequences
19323 : : *
19324 : : * Construct a table of sequence information. This table is sorted by OID for
19325 : : * speed in lookup.
19326 : : *
19327 : : * NB: This must be called after all TableDataInfo objects have been created,
19328 : : * since we only gather the data for sequences that have one.
19329 : : */
19330 : : static void
19331 : 215 : collectSequences(Archive *fout, TableInfo *tblinfo, int numTables)
19332 : : {
19333 : 215 : PQExpBuffer query = createPQExpBuffer();
19334 : : PGresult *res;
19335 : :
19336 : : /*
19337 : : * Since version 18, we can gather the sequence data in this query with
19338 : : * pg_get_sequence_data(), but we only do so for non-schema-only dumps.
19339 : : */
19340 [ + - ]: 215 : if (fout->remoteVersion < 180000 ||
19341 [ + + + + ]: 215 : (!fout->dopt->dumpData && !fout->dopt->sequence_data))
19342 : 9 : appendPQExpBufferStr(query,
19343 : : "SELECT seqrelid, format_type(seqtypid, NULL), "
19344 : : "seqstart, seqincrement, "
19345 : : "seqmax, seqmin, "
19346 : : "seqcache, seqcycle, "
19347 : : "NULL, 'f' "
19348 : : "FROM pg_catalog.pg_sequence "
19349 : : "ORDER BY seqrelid");
19350 : : else
19351 : : {
19352 : 206 : PQExpBuffer seqoids = createPQExpBuffer();
19353 : :
19354 : : /*
19355 : : * pg_get_sequence_data() must open, lock, and read each sequence, so
19356 : : * we only call it for the sequences whose data will be dumped.
19357 : : */
19358 : 206 : appendPQExpBufferChar(seqoids, '{');
19359 [ + + ]: 55420 : for (int i = 0; i < numTables; i++)
19360 : : {
19361 : 55214 : TableInfo *tbinfo = &tblinfo[i];
19362 : :
19363 [ + + + + ]: 55214 : if (tbinfo->relkind != RELKIND_SEQUENCE || tbinfo->dataObj == NULL)
19364 : 54800 : continue;
19365 : :
19366 [ + + ]: 414 : if (seqoids->len > 1) /* do we have more than the '{'? */
19367 : 328 : appendPQExpBufferChar(seqoids, ',');
19368 : 414 : appendPQExpBuffer(seqoids, "%u", tbinfo->dobj.catId.oid);
19369 : : }
19370 : 206 : appendPQExpBufferChar(seqoids, '}');
19371 : :
19372 : 206 : appendPQExpBuffer(query,
19373 : : "SELECT seqrelid, format_type(seqtypid, NULL), "
19374 : : "seqstart, seqincrement, "
19375 : : "seqmax, seqmin, "
19376 : : "seqcache, seqcycle, "
19377 : : "last_value, is_called "
19378 : : "FROM pg_catalog.pg_sequence "
19379 : : "LEFT JOIN (SELECT seqoid, last_value, is_called "
19380 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(seqoid), "
19381 : : "pg_get_sequence_data(seqoid)) d "
19382 : : "ON seqoid = seqrelid "
19383 : : "ORDER BY seqrelid",
19384 : : seqoids->data);
19385 : :
19386 : 206 : destroyPQExpBuffer(seqoids);
19387 : : }
19388 : :
19389 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19390 : :
19391 : 215 : nsequences = PQntuples(res);
19392 : 215 : sequences = pg_malloc_array(SequenceItem, nsequences);
19393 : :
19394 [ + + ]: 878 : for (int i = 0; i < nsequences; i++)
19395 : : {
19396 : 663 : sequences[i].oid = atooid(PQgetvalue(res, i, 0));
19397 : 663 : sequences[i].seqtype = parse_sequence_type(PQgetvalue(res, i, 1));
19398 : 663 : sequences[i].startv = strtoi64(PQgetvalue(res, i, 2), NULL, 10);
19399 : 663 : sequences[i].incby = strtoi64(PQgetvalue(res, i, 3), NULL, 10);
19400 : 663 : sequences[i].maxv = strtoi64(PQgetvalue(res, i, 4), NULL, 10);
19401 : 663 : sequences[i].minv = strtoi64(PQgetvalue(res, i, 5), NULL, 10);
19402 : 663 : sequences[i].cache = strtoi64(PQgetvalue(res, i, 6), NULL, 10);
19403 : 663 : sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0);
19404 : 663 : sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10);
19405 : 663 : sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0);
19406 [ + + - + ]: 663 : sequences[i].null_seqtuple = (PQgetisnull(res, i, 8) || PQgetisnull(res, i, 9));
19407 : : }
19408 : :
19409 : 215 : PQclear(res);
19410 : 215 : destroyPQExpBuffer(query);
19411 : 215 : }
19412 : :
19413 : : /*
19414 : : * dumpSequence
19415 : : * write the declaration (not data) of one user-defined sequence
19416 : : */
19417 : : static void
19418 : 397 : dumpSequence(Archive *fout, const TableInfo *tbinfo)
19419 : : {
19420 : 397 : DumpOptions *dopt = fout->dopt;
19421 : : SequenceItem *seq;
19422 : : bool is_ascending;
19423 : : int64 default_minv,
19424 : : default_maxv;
19425 : 397 : PQExpBuffer query = createPQExpBuffer();
19426 : 397 : PQExpBuffer delqry = createPQExpBuffer();
19427 : : char *qseqname;
19428 : 397 : TableInfo *owning_tab = NULL;
19429 : 397 : SequenceItem key = {0};
19430 : :
19431 : 397 : qseqname = pg_strdup(fmtId(tbinfo->dobj.name));
19432 : :
19433 : : /*
19434 : : * The sequence information is gathered in a sorted table before any calls
19435 : : * to dumpSequence(). See collectSequences() for more information.
19436 : : */
19437 : : Assert(sequences);
19438 : :
19439 : 397 : key.oid = tbinfo->dobj.catId.oid;
19440 : 397 : seq = bsearch(&key, sequences, nsequences,
19441 : : sizeof(SequenceItem), SequenceItemCmp);
19442 : :
19443 : : /* Calculate default limits for a sequence of this type */
19444 : 397 : is_ascending = (seq->incby >= 0);
19445 [ + + ]: 397 : if (seq->seqtype == SEQTYPE_SMALLINT)
19446 : : {
19447 [ + + ]: 25 : default_minv = is_ascending ? 1 : PG_INT16_MIN;
19448 [ + + ]: 25 : default_maxv = is_ascending ? PG_INT16_MAX : -1;
19449 : : }
19450 [ + + ]: 372 : else if (seq->seqtype == SEQTYPE_INTEGER)
19451 : : {
19452 [ + + ]: 306 : default_minv = is_ascending ? 1 : PG_INT32_MIN;
19453 [ + + ]: 306 : default_maxv = is_ascending ? PG_INT32_MAX : -1;
19454 : : }
19455 [ + - ]: 66 : else if (seq->seqtype == SEQTYPE_BIGINT)
19456 : : {
19457 [ + + ]: 66 : default_minv = is_ascending ? 1 : PG_INT64_MIN;
19458 [ + + ]: 66 : default_maxv = is_ascending ? PG_INT64_MAX : -1;
19459 : : }
19460 : : else
19461 : : {
19462 : 0 : pg_fatal("unrecognized sequence type: %d", seq->seqtype);
19463 : : default_minv = default_maxv = 0; /* keep compiler quiet */
19464 : : }
19465 : :
19466 : : /*
19467 : : * Identity sequences are not to be dropped separately.
19468 : : */
19469 [ + + ]: 397 : if (!tbinfo->is_identity_sequence)
19470 : : {
19471 : 237 : appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n",
19472 : 237 : fmtQualifiedDumpable(tbinfo));
19473 : : }
19474 : :
19475 : 397 : resetPQExpBuffer(query);
19476 : :
19477 [ + + ]: 397 : if (dopt->binary_upgrade)
19478 : : {
19479 : 66 : binary_upgrade_set_pg_class_oids(fout, query,
19480 : 66 : tbinfo->dobj.catId.oid);
19481 : :
19482 : : /*
19483 : : * In older PG versions a sequence will have a pg_type entry, but v14
19484 : : * and up don't use that, so don't attempt to preserve the type OID.
19485 : : */
19486 : : }
19487 : :
19488 [ + + ]: 397 : if (tbinfo->is_identity_sequence)
19489 : : {
19490 : 160 : owning_tab = findTableByOid(tbinfo->owning_tab);
19491 : :
19492 : 160 : appendPQExpBuffer(query,
19493 : : "ALTER TABLE %s ",
19494 : 160 : fmtQualifiedDumpable(owning_tab));
19495 : 160 : appendPQExpBuffer(query,
19496 : : "ALTER COLUMN %s ADD GENERATED ",
19497 : 160 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19498 [ + + ]: 160 : if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS)
19499 : 120 : appendPQExpBufferStr(query, "ALWAYS");
19500 [ + - ]: 40 : else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
19501 : 40 : appendPQExpBufferStr(query, "BY DEFAULT");
19502 : 160 : appendPQExpBuffer(query, " AS IDENTITY (\n SEQUENCE NAME %s\n",
19503 : 160 : fmtQualifiedDumpable(tbinfo));
19504 : :
19505 : : /*
19506 : : * Emit persistence option only if it's different from the owning
19507 : : * table's. This avoids using this new syntax unnecessarily.
19508 : : */
19509 [ + + ]: 160 : if (tbinfo->relpersistence != owning_tab->relpersistence)
19510 : 10 : appendPQExpBuffer(query, " %s\n",
19511 [ + + ]: 10 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19512 : : "UNLOGGED" : "LOGGED");
19513 : : }
19514 : : else
19515 : : {
19516 : 237 : appendPQExpBuffer(query,
19517 : : "CREATE %sSEQUENCE %s\n",
19518 [ + + ]: 237 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19519 : : "UNLOGGED " : "",
19520 : 237 : fmtQualifiedDumpable(tbinfo));
19521 : :
19522 [ + + ]: 237 : if (seq->seqtype != SEQTYPE_BIGINT)
19523 : 186 : appendPQExpBuffer(query, " AS %s\n", SeqTypeNames[seq->seqtype]);
19524 : : }
19525 : :
19526 : 397 : appendPQExpBuffer(query, " START WITH " INT64_FORMAT "\n", seq->startv);
19527 : :
19528 : 397 : appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT "\n", seq->incby);
19529 : :
19530 [ + + ]: 397 : if (seq->minv != default_minv)
19531 : 15 : appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT "\n", seq->minv);
19532 : : else
19533 : 382 : appendPQExpBufferStr(query, " NO MINVALUE\n");
19534 : :
19535 [ + + ]: 397 : if (seq->maxv != default_maxv)
19536 : 15 : appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT "\n", seq->maxv);
19537 : : else
19538 : 382 : appendPQExpBufferStr(query, " NO MAXVALUE\n");
19539 : :
19540 : 397 : appendPQExpBuffer(query,
19541 : : " CACHE " INT64_FORMAT "%s",
19542 [ + + ]: 397 : seq->cache, (seq->cycled ? "\n CYCLE" : ""));
19543 : :
19544 [ + + ]: 397 : if (tbinfo->is_identity_sequence)
19545 : 160 : appendPQExpBufferStr(query, "\n);\n");
19546 : : else
19547 : 237 : appendPQExpBufferStr(query, ";\n");
19548 : :
19549 : : /* binary_upgrade: no need to clear TOAST table oid */
19550 : :
19551 [ + + ]: 397 : if (dopt->binary_upgrade)
19552 : 66 : binary_upgrade_extension_member(query, &tbinfo->dobj,
19553 : : "SEQUENCE", qseqname,
19554 : 66 : tbinfo->dobj.namespace->dobj.name);
19555 : :
19556 [ + - ]: 397 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19557 : 397 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
19558 : 397 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19559 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19560 : : .owner = tbinfo->rolname,
19561 : : .description = "SEQUENCE",
19562 : : .section = SECTION_PRE_DATA,
19563 : : .createStmt = query->data,
19564 : : .dropStmt = delqry->data));
19565 : :
19566 : : /*
19567 : : * If the sequence is owned by a table column, emit the ALTER for it as a
19568 : : * separate TOC entry immediately following the sequence's own entry. It's
19569 : : * OK to do this rather than using full sorting logic, because the
19570 : : * dependency that tells us it's owned will have forced the table to be
19571 : : * created first. We can't just include the ALTER in the TOC entry
19572 : : * because it will fail if we haven't reassigned the sequence owner to
19573 : : * match the table's owner.
19574 : : *
19575 : : * We need not schema-qualify the table reference because both sequence
19576 : : * and table must be in the same schema.
19577 : : */
19578 [ + + + + ]: 397 : if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence)
19579 : : {
19580 : 141 : owning_tab = findTableByOid(tbinfo->owning_tab);
19581 : :
19582 [ - + ]: 141 : if (owning_tab == NULL)
19583 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
19584 : : tbinfo->owning_tab, tbinfo->dobj.catId.oid);
19585 : :
19586 [ + + ]: 141 : if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION)
19587 : : {
19588 : 139 : resetPQExpBuffer(query);
19589 : 139 : appendPQExpBuffer(query, "ALTER SEQUENCE %s",
19590 : 139 : fmtQualifiedDumpable(tbinfo));
19591 : 139 : appendPQExpBuffer(query, " OWNED BY %s",
19592 : 139 : fmtQualifiedDumpable(owning_tab));
19593 : 139 : appendPQExpBuffer(query, ".%s;\n",
19594 : 139 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19595 : :
19596 [ + - ]: 139 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19597 : 139 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19598 : 139 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19599 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19600 : : .owner = tbinfo->rolname,
19601 : : .description = "SEQUENCE OWNED BY",
19602 : : .section = SECTION_PRE_DATA,
19603 : : .createStmt = query->data,
19604 : : .deps = &(tbinfo->dobj.dumpId),
19605 : : .nDeps = 1));
19606 : : }
19607 : : }
19608 : :
19609 : : /* Dump Sequence Comments and Security Labels */
19610 [ - + ]: 397 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19611 : 0 : dumpComment(fout, "SEQUENCE", qseqname,
19612 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19613 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19614 : :
19615 [ - + ]: 397 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19616 : 0 : dumpSecLabel(fout, "SEQUENCE", qseqname,
19617 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19618 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19619 : :
19620 : 397 : destroyPQExpBuffer(query);
19621 : 397 : destroyPQExpBuffer(delqry);
19622 : 397 : pg_free(qseqname);
19623 : 397 : }
19624 : :
19625 : : /*
19626 : : * dumpSequenceData
19627 : : * write the data of one user-defined sequence
19628 : : */
19629 : : static void
19630 : 415 : dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo)
19631 : : {
19632 : 415 : TableInfo *tbinfo = tdinfo->tdtable;
19633 : : int64 last;
19634 : : bool called;
19635 : : PQExpBuffer query;
19636 : :
19637 : : /* needn't bother if not dumping sequence data */
19638 [ + + + + ]: 415 : if (!fout->dopt->dumpData && !fout->dopt->sequence_data)
19639 : 1 : return;
19640 : :
19641 : 414 : query = createPQExpBuffer();
19642 : :
19643 : : /*
19644 : : * For versions >= 18, the sequence information is gathered in the sorted
19645 : : * array before any calls to dumpSequenceData(). See collectSequences()
19646 : : * for more information.
19647 : : *
19648 : : * For older versions, we have to query the sequence relations
19649 : : * individually.
19650 : : */
19651 [ - + ]: 414 : if (fout->remoteVersion < 180000)
19652 : : {
19653 : : PGresult *res;
19654 : :
19655 : 0 : appendPQExpBuffer(query,
19656 : : "SELECT last_value, is_called FROM %s",
19657 : 0 : fmtQualifiedDumpable(tbinfo));
19658 : :
19659 : 0 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19660 : :
19661 [ # # ]: 0 : if (PQntuples(res) != 1)
19662 : 0 : pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)",
19663 : : "query to get data of sequence \"%s\" returned %d rows (expected 1)",
19664 : : PQntuples(res)),
19665 : : tbinfo->dobj.name, PQntuples(res));
19666 : :
19667 : 0 : last = strtoi64(PQgetvalue(res, 0, 0), NULL, 10);
19668 : 0 : called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
19669 : :
19670 : 0 : PQclear(res);
19671 : : }
19672 : : else
19673 : : {
19674 : 414 : SequenceItem key = {0};
19675 : : SequenceItem *entry;
19676 : :
19677 : : Assert(sequences);
19678 : : Assert(tbinfo->dobj.catId.oid);
19679 : :
19680 : 414 : key.oid = tbinfo->dobj.catId.oid;
19681 : 414 : entry = bsearch(&key, sequences, nsequences,
19682 : : sizeof(SequenceItem), SequenceItemCmp);
19683 : :
19684 [ - + ]: 414 : if (entry->null_seqtuple)
19685 : 0 : pg_fatal("failed to get data for sequence \"%s\"; user may lack "
19686 : : "SELECT privilege on the sequence or the sequence may "
19687 : : "have been concurrently dropped",
19688 : : tbinfo->dobj.name);
19689 : :
19690 : 414 : last = entry->last_value;
19691 : 414 : called = entry->is_called;
19692 : : }
19693 : :
19694 : 414 : resetPQExpBuffer(query);
19695 : 414 : appendPQExpBufferStr(query, "SELECT pg_catalog.setval(");
19696 : 414 : appendStringLiteralAH(query, fmtQualifiedDumpable(tbinfo), fout);
19697 [ + + ]: 414 : appendPQExpBuffer(query, ", " INT64_FORMAT ", %s);\n",
19698 : : last, (called ? "true" : "false"));
19699 : :
19700 [ + - ]: 414 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
19701 : 414 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19702 : 414 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19703 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19704 : : .owner = tbinfo->rolname,
19705 : : .description = "SEQUENCE SET",
19706 : : .section = SECTION_DATA,
19707 : : .createStmt = query->data,
19708 : : .deps = &(tbinfo->dobj.dumpId),
19709 : : .nDeps = 1));
19710 : :
19711 : 414 : destroyPQExpBuffer(query);
19712 : : }
19713 : :
19714 : : /*
19715 : : * dumpTrigger
19716 : : * write the declaration of one user-defined table trigger
19717 : : */
19718 : : static void
19719 : 535 : dumpTrigger(Archive *fout, const TriggerInfo *tginfo)
19720 : : {
19721 : 535 : DumpOptions *dopt = fout->dopt;
19722 : 535 : TableInfo *tbinfo = tginfo->tgtable;
19723 : : PQExpBuffer query;
19724 : : PQExpBuffer delqry;
19725 : : PQExpBuffer trigprefix;
19726 : : PQExpBuffer trigidentity;
19727 : : char *qtabname;
19728 : : char *tag;
19729 : :
19730 : : /* Do nothing if not dumping schema */
19731 [ + + ]: 535 : if (!dopt->dumpSchema)
19732 : 33 : return;
19733 : :
19734 : 502 : query = createPQExpBuffer();
19735 : 502 : delqry = createPQExpBuffer();
19736 : 502 : trigprefix = createPQExpBuffer();
19737 : 502 : trigidentity = createPQExpBuffer();
19738 : :
19739 : 502 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19740 : :
19741 : 502 : appendPQExpBuffer(trigidentity, "%s ", fmtId(tginfo->dobj.name));
19742 : 502 : appendPQExpBuffer(trigidentity, "ON %s", fmtQualifiedDumpable(tbinfo));
19743 : :
19744 : 502 : appendPQExpBuffer(query, "%s;\n", tginfo->tgdef);
19745 : 502 : appendPQExpBuffer(delqry, "DROP TRIGGER %s;\n", trigidentity->data);
19746 : :
19747 : : /* Triggers can depend on extensions */
19748 : 502 : append_depends_on_extension(fout, query, &tginfo->dobj,
19749 : : "pg_catalog.pg_trigger", "TRIGGER",
19750 : 502 : trigidentity->data);
19751 : :
19752 [ + + ]: 502 : if (tginfo->tgispartition)
19753 : : {
19754 : : Assert(tbinfo->ispartition);
19755 : :
19756 : : /*
19757 : : * Partition triggers only appear here because their 'tgenabled' flag
19758 : : * differs from its parent's. The trigger is created already, so
19759 : : * remove the CREATE and replace it with an ALTER. (Clear out the
19760 : : * DROP query too, so that pg_dump --create does not cause errors.)
19761 : : */
19762 : 115 : resetPQExpBuffer(query);
19763 : 115 : resetPQExpBuffer(delqry);
19764 : 115 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19765 [ - + ]: 115 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19766 : 115 : fmtQualifiedDumpable(tbinfo));
19767 [ + - + + : 115 : switch (tginfo->tgenabled)
- ]
19768 : : {
19769 : 40 : case 'f':
19770 : : case 'D':
19771 : 40 : appendPQExpBufferStr(query, "DISABLE");
19772 : 40 : break;
19773 : 0 : case 't':
19774 : : case 'O':
19775 : 0 : appendPQExpBufferStr(query, "ENABLE");
19776 : 0 : break;
19777 : 35 : case 'R':
19778 : 35 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19779 : 35 : break;
19780 : 40 : case 'A':
19781 : 40 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19782 : 40 : break;
19783 : : }
19784 : 115 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19785 : 115 : fmtId(tginfo->dobj.name));
19786 : : }
19787 [ + - - + ]: 387 : else if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
19788 : : {
19789 : 0 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19790 [ # # ]: 0 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19791 : 0 : fmtQualifiedDumpable(tbinfo));
19792 [ # # # # ]: 0 : switch (tginfo->tgenabled)
19793 : : {
19794 : 0 : case 'D':
19795 : : case 'f':
19796 : 0 : appendPQExpBufferStr(query, "DISABLE");
19797 : 0 : break;
19798 : 0 : case 'A':
19799 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19800 : 0 : break;
19801 : 0 : case 'R':
19802 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19803 : 0 : break;
19804 : 0 : default:
19805 : 0 : appendPQExpBufferStr(query, "ENABLE");
19806 : 0 : break;
19807 : : }
19808 : 0 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19809 : 0 : fmtId(tginfo->dobj.name));
19810 : : }
19811 : :
19812 : 502 : appendPQExpBuffer(trigprefix, "TRIGGER %s ON",
19813 : 502 : fmtId(tginfo->dobj.name));
19814 : :
19815 : 502 : tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name);
19816 : :
19817 [ + - ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19818 : 502 : ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
19819 : 502 : ARCHIVE_OPTS(.tag = tag,
19820 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19821 : : .owner = tbinfo->rolname,
19822 : : .description = "TRIGGER",
19823 : : .section = SECTION_POST_DATA,
19824 : : .createStmt = query->data,
19825 : : .dropStmt = delqry->data));
19826 : :
19827 [ - + ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19828 : 0 : dumpComment(fout, trigprefix->data, qtabname,
19829 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19830 : 0 : tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
19831 : :
19832 : 502 : pfree(tag);
19833 : 502 : destroyPQExpBuffer(query);
19834 : 502 : destroyPQExpBuffer(delqry);
19835 : 502 : destroyPQExpBuffer(trigprefix);
19836 : 502 : destroyPQExpBuffer(trigidentity);
19837 : 502 : pg_free(qtabname);
19838 : : }
19839 : :
19840 : : /*
19841 : : * dumpEventTrigger
19842 : : * write the declaration of one user-defined event trigger
19843 : : */
19844 : : static void
19845 : 44 : dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo)
19846 : : {
19847 : 44 : DumpOptions *dopt = fout->dopt;
19848 : : PQExpBuffer query;
19849 : : PQExpBuffer delqry;
19850 : : char *qevtname;
19851 : :
19852 : : /* Do nothing if not dumping schema */
19853 [ + + ]: 44 : if (!dopt->dumpSchema)
19854 : 6 : return;
19855 : :
19856 : 38 : query = createPQExpBuffer();
19857 : 38 : delqry = createPQExpBuffer();
19858 : :
19859 : 38 : qevtname = pg_strdup(fmtId(evtinfo->dobj.name));
19860 : :
19861 : 38 : appendPQExpBufferStr(query, "CREATE EVENT TRIGGER ");
19862 : 38 : appendPQExpBufferStr(query, qevtname);
19863 : 38 : appendPQExpBufferStr(query, " ON ");
19864 : 38 : appendPQExpBufferStr(query, fmtId(evtinfo->evtevent));
19865 : :
19866 [ + + ]: 38 : if (strcmp("", evtinfo->evttags) != 0)
19867 : : {
19868 : 5 : appendPQExpBufferStr(query, "\n WHEN TAG IN (");
19869 : 5 : appendPQExpBufferStr(query, evtinfo->evttags);
19870 : 5 : appendPQExpBufferChar(query, ')');
19871 : : }
19872 : :
19873 : 38 : appendPQExpBufferStr(query, "\n EXECUTE FUNCTION ");
19874 : 38 : appendPQExpBufferStr(query, evtinfo->evtfname);
19875 : 38 : appendPQExpBufferStr(query, "();\n");
19876 : :
19877 [ - + ]: 38 : if (evtinfo->evtenabled != 'O')
19878 : : {
19879 : 0 : appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ",
19880 : : qevtname);
19881 [ # # # # ]: 0 : switch (evtinfo->evtenabled)
19882 : : {
19883 : 0 : case 'D':
19884 : 0 : appendPQExpBufferStr(query, "DISABLE");
19885 : 0 : break;
19886 : 0 : case 'A':
19887 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19888 : 0 : break;
19889 : 0 : case 'R':
19890 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19891 : 0 : break;
19892 : 0 : default:
19893 : 0 : appendPQExpBufferStr(query, "ENABLE");
19894 : 0 : break;
19895 : : }
19896 : 0 : appendPQExpBufferStr(query, ";\n");
19897 : : }
19898 : :
19899 : 38 : appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n",
19900 : : qevtname);
19901 : :
19902 [ + + ]: 38 : if (dopt->binary_upgrade)
19903 : 2 : binary_upgrade_extension_member(query, &evtinfo->dobj,
19904 : : "EVENT TRIGGER", qevtname, NULL);
19905 : :
19906 [ + - ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19907 : 38 : ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId,
19908 : 38 : ARCHIVE_OPTS(.tag = evtinfo->dobj.name,
19909 : : .owner = evtinfo->evtowner,
19910 : : .description = "EVENT TRIGGER",
19911 : : .section = SECTION_POST_DATA,
19912 : : .createStmt = query->data,
19913 : : .dropStmt = delqry->data));
19914 : :
19915 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19916 : 0 : dumpComment(fout, "EVENT TRIGGER", qevtname,
19917 : 0 : NULL, evtinfo->evtowner,
19918 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19919 : :
19920 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19921 : 0 : dumpSecLabel(fout, "EVENT TRIGGER", qevtname,
19922 : 0 : NULL, evtinfo->evtowner,
19923 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19924 : :
19925 : 38 : destroyPQExpBuffer(query);
19926 : 38 : destroyPQExpBuffer(delqry);
19927 : 38 : pg_free(qevtname);
19928 : : }
19929 : :
19930 : : /*
19931 : : * dumpRule
19932 : : * Dump a rule
19933 : : */
19934 : : static void
19935 : 1201 : dumpRule(Archive *fout, const RuleInfo *rinfo)
19936 : : {
19937 : 1201 : DumpOptions *dopt = fout->dopt;
19938 : 1201 : TableInfo *tbinfo = rinfo->ruletable;
19939 : : bool is_view;
19940 : : PQExpBuffer query;
19941 : : PQExpBuffer cmd;
19942 : : PQExpBuffer delcmd;
19943 : : PQExpBuffer ruleprefix;
19944 : : char *qtabname;
19945 : : PGresult *res;
19946 : : char *tag;
19947 : :
19948 : : /* Do nothing if not dumping schema */
19949 [ + + ]: 1201 : if (!dopt->dumpSchema)
19950 : 70 : return;
19951 : :
19952 : : /*
19953 : : * If it is an ON SELECT rule that is created implicitly by CREATE VIEW,
19954 : : * we do not want to dump it as a separate object.
19955 : : */
19956 [ + + ]: 1131 : if (!rinfo->separate)
19957 : 906 : return;
19958 : :
19959 : : /*
19960 : : * If it's an ON SELECT rule, we want to print it as a view definition,
19961 : : * instead of a rule.
19962 : : */
19963 [ + + + - ]: 225 : is_view = (rinfo->ev_type == '1' && rinfo->is_instead);
19964 : :
19965 : 225 : query = createPQExpBuffer();
19966 : 225 : cmd = createPQExpBuffer();
19967 : 225 : delcmd = createPQExpBuffer();
19968 : 225 : ruleprefix = createPQExpBuffer();
19969 : :
19970 : 225 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19971 : :
19972 [ + + ]: 225 : if (is_view)
19973 : : {
19974 : : PQExpBuffer result;
19975 : :
19976 : : /*
19977 : : * We need OR REPLACE here because we'll be replacing a dummy view.
19978 : : * Otherwise this should look largely like the regular view dump code.
19979 : : */
19980 : 24 : appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s",
19981 : 24 : fmtQualifiedDumpable(tbinfo));
19982 [ - + ]: 24 : if (nonemptyReloptions(tbinfo->reloptions))
19983 : : {
19984 : 0 : appendPQExpBufferStr(cmd, " WITH (");
19985 : 0 : appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout);
19986 : 0 : appendPQExpBufferChar(cmd, ')');
19987 : : }
19988 : 24 : result = createViewAsClause(fout, tbinfo);
19989 : 24 : appendPQExpBuffer(cmd, " AS\n%s", result->data);
19990 : 24 : destroyPQExpBuffer(result);
19991 [ - + ]: 24 : if (tbinfo->checkoption != NULL)
19992 : 0 : appendPQExpBuffer(cmd, "\n WITH %s CHECK OPTION",
19993 : : tbinfo->checkoption);
19994 : 24 : appendPQExpBufferStr(cmd, ";\n");
19995 : : }
19996 : : else
19997 : : {
19998 : : /* In the rule case, just print pg_get_ruledef's result verbatim */
19999 : 201 : appendPQExpBuffer(query,
20000 : : "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)",
20001 : 201 : rinfo->dobj.catId.oid);
20002 : :
20003 : 201 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20004 : :
20005 [ - + ]: 201 : if (PQntuples(res) != 1)
20006 : 0 : pg_fatal("query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned",
20007 : : rinfo->dobj.name, tbinfo->dobj.name);
20008 : :
20009 : 201 : printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
20010 : :
20011 : 201 : PQclear(res);
20012 : : }
20013 : :
20014 : : /*
20015 : : * Add the command to alter the rules replication firing semantics if it
20016 : : * differs from the default.
20017 : : */
20018 [ + + ]: 225 : if (rinfo->ev_enabled != 'O')
20019 : : {
20020 : 15 : appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtQualifiedDumpable(tbinfo));
20021 [ - - + - ]: 15 : switch (rinfo->ev_enabled)
20022 : : {
20023 : 0 : case 'A':
20024 : 0 : appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n",
20025 : 0 : fmtId(rinfo->dobj.name));
20026 : 0 : break;
20027 : 0 : case 'R':
20028 : 0 : appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n",
20029 : 0 : fmtId(rinfo->dobj.name));
20030 : 0 : break;
20031 : 15 : case 'D':
20032 : 15 : appendPQExpBuffer(cmd, "DISABLE RULE %s;\n",
20033 : 15 : fmtId(rinfo->dobj.name));
20034 : 15 : break;
20035 : : }
20036 : : }
20037 : :
20038 [ + + ]: 225 : if (is_view)
20039 : : {
20040 : : /*
20041 : : * We can't DROP a view's ON SELECT rule. Instead, use CREATE OR
20042 : : * REPLACE VIEW to replace the rule with something with minimal
20043 : : * dependencies.
20044 : : */
20045 : : PQExpBuffer result;
20046 : :
20047 : 24 : appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s",
20048 : 24 : fmtQualifiedDumpable(tbinfo));
20049 : 24 : result = createDummyViewAsClause(fout, tbinfo);
20050 : 24 : appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data);
20051 : 24 : destroyPQExpBuffer(result);
20052 : : }
20053 : : else
20054 : : {
20055 : 201 : appendPQExpBuffer(delcmd, "DROP RULE %s ",
20056 : 201 : fmtId(rinfo->dobj.name));
20057 : 201 : appendPQExpBuffer(delcmd, "ON %s;\n",
20058 : 201 : fmtQualifiedDumpable(tbinfo));
20059 : : }
20060 : :
20061 : 225 : appendPQExpBuffer(ruleprefix, "RULE %s ON",
20062 : 225 : fmtId(rinfo->dobj.name));
20063 : :
20064 : 225 : tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name);
20065 : :
20066 [ + - ]: 225 : if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
20067 : 225 : ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
20068 : 225 : ARCHIVE_OPTS(.tag = tag,
20069 : : .namespace = tbinfo->dobj.namespace->dobj.name,
20070 : : .owner = tbinfo->rolname,
20071 : : .description = "RULE",
20072 : : .section = SECTION_POST_DATA,
20073 : : .createStmt = cmd->data,
20074 : : .dropStmt = delcmd->data));
20075 : :
20076 : : /* Dump rule comments */
20077 [ - + ]: 225 : if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
20078 : 0 : dumpComment(fout, ruleprefix->data, qtabname,
20079 : 0 : tbinfo->dobj.namespace->dobj.name,
20080 : : tbinfo->rolname,
20081 : 0 : rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
20082 : :
20083 : 225 : pfree(tag);
20084 : 225 : destroyPQExpBuffer(query);
20085 : 225 : destroyPQExpBuffer(cmd);
20086 : 225 : destroyPQExpBuffer(delcmd);
20087 : 225 : destroyPQExpBuffer(ruleprefix);
20088 : 225 : pg_free(qtabname);
20089 : : }
20090 : :
20091 : : /*
20092 : : * getExtensionMembership --- obtain extension membership data
20093 : : *
20094 : : * We need to identify objects that are extension members as soon as they're
20095 : : * loaded, so that we can correctly determine whether they need to be dumped.
20096 : : * Generally speaking, extension member objects will get marked as *not* to
20097 : : * be dumped, as they will be recreated by the single CREATE EXTENSION
20098 : : * command. However, in binary upgrade mode we still need to dump the members
20099 : : * individually.
20100 : : */
20101 : : void
20102 : 216 : getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
20103 : : int numExtensions)
20104 : : {
20105 : : PQExpBuffer query;
20106 : : PGresult *res;
20107 : : int ntups,
20108 : : i;
20109 : : int i_classid,
20110 : : i_objid,
20111 : : i_refobjid;
20112 : : ExtensionInfo *ext;
20113 : :
20114 : : /* Nothing to do if no extensions */
20115 [ - + ]: 216 : if (numExtensions == 0)
20116 : 0 : return;
20117 : :
20118 : 216 : query = createPQExpBuffer();
20119 : :
20120 : : /* refclassid constraint is redundant but may speed the search */
20121 : 216 : appendPQExpBufferStr(query, "SELECT "
20122 : : "classid, objid, refobjid "
20123 : : "FROM pg_depend "
20124 : : "WHERE refclassid = 'pg_extension'::regclass "
20125 : : "AND deptype = 'e' "
20126 : : "ORDER BY 3");
20127 : :
20128 : 216 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20129 : :
20130 : 216 : ntups = PQntuples(res);
20131 : :
20132 : 216 : i_classid = PQfnumber(res, "classid");
20133 : 216 : i_objid = PQfnumber(res, "objid");
20134 : 216 : i_refobjid = PQfnumber(res, "refobjid");
20135 : :
20136 : : /*
20137 : : * Since we ordered the SELECT by referenced ID, we can expect that
20138 : : * multiple entries for the same extension will appear together; this
20139 : : * saves on searches.
20140 : : */
20141 : 216 : ext = NULL;
20142 : :
20143 [ + + ]: 1680 : for (i = 0; i < ntups; i++)
20144 : : {
20145 : : CatalogId objId;
20146 : : Oid extId;
20147 : :
20148 : 1464 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
20149 : 1464 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
20150 : 1464 : extId = atooid(PQgetvalue(res, i, i_refobjid));
20151 : :
20152 [ + + ]: 1464 : if (ext == NULL ||
20153 [ + + ]: 1248 : ext->dobj.catId.oid != extId)
20154 : 247 : ext = findExtensionByOid(extId);
20155 : :
20156 [ - + ]: 1464 : if (ext == NULL)
20157 : : {
20158 : : /* shouldn't happen */
20159 : 0 : pg_log_warning("could not find referenced extension %u", extId);
20160 : 0 : continue;
20161 : : }
20162 : :
20163 : 1464 : recordExtensionMembership(objId, ext);
20164 : : }
20165 : :
20166 : 216 : PQclear(res);
20167 : :
20168 : 216 : destroyPQExpBuffer(query);
20169 : : }
20170 : :
20171 : : /*
20172 : : * processExtensionTables --- deal with extension configuration tables
20173 : : *
20174 : : * There are two parts to this process:
20175 : : *
20176 : : * 1. Identify and create dump records for extension configuration tables.
20177 : : *
20178 : : * Extensions can mark tables as "configuration", which means that the user
20179 : : * is able and expected to modify those tables after the extension has been
20180 : : * loaded. For these tables, we dump out only the data- the structure is
20181 : : * expected to be handled at CREATE EXTENSION time, including any indexes or
20182 : : * foreign keys, which brings us to-
20183 : : *
20184 : : * 2. Record FK dependencies between configuration tables.
20185 : : *
20186 : : * Due to the FKs being created at CREATE EXTENSION time and therefore before
20187 : : * the data is loaded, we have to work out what the best order for reloading
20188 : : * the data is, to avoid FK violations when the tables are restored. This is
20189 : : * not perfect- we can't handle circular dependencies and if any exist they
20190 : : * will cause an invalid dump to be produced (though at least all of the data
20191 : : * is included for a user to manually restore). This is currently documented
20192 : : * but perhaps we can provide a better solution in the future.
20193 : : */
20194 : : void
20195 : 215 : processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
20196 : : int numExtensions)
20197 : : {
20198 : 215 : DumpOptions *dopt = fout->dopt;
20199 : : PQExpBuffer query;
20200 : : PGresult *res;
20201 : : int ntups,
20202 : : i;
20203 : : int i_conrelid,
20204 : : i_confrelid;
20205 : :
20206 : : /* Nothing to do if no extensions */
20207 [ - + ]: 215 : if (numExtensions == 0)
20208 : 0 : return;
20209 : :
20210 : : /*
20211 : : * Identify extension configuration tables and create TableDataInfo
20212 : : * objects for them, ensuring their data will be dumped even though the
20213 : : * tables themselves won't be.
20214 : : *
20215 : : * Note that we create TableDataInfo objects even in schema-only mode, ie,
20216 : : * user data in a configuration table is treated like schema data. This
20217 : : * seems appropriate since system data in a config table would get
20218 : : * reloaded by CREATE EXTENSION. If the extension is not listed in the
20219 : : * list of extensions to be included, none of its data is dumped.
20220 : : */
20221 [ + + ]: 461 : for (i = 0; i < numExtensions; i++)
20222 : : {
20223 : 246 : ExtensionInfo *curext = &(extinfo[i]);
20224 : 246 : char *extconfig = curext->extconfig;
20225 : 246 : char *extcondition = curext->extcondition;
20226 : 246 : char **extconfigarray = NULL;
20227 : 246 : char **extconditionarray = NULL;
20228 : 246 : int nconfigitems = 0;
20229 : 246 : int nconditionitems = 0;
20230 : :
20231 : : /*
20232 : : * Check if this extension is listed as to include in the dump. If
20233 : : * not, any table data associated with it is discarded.
20234 : : */
20235 [ + + ]: 246 : if (extension_include_oids.head != NULL &&
20236 [ + + ]: 8 : !simple_oid_list_member(&extension_include_oids,
20237 : : curext->dobj.catId.oid))
20238 : 6 : continue;
20239 : :
20240 : : /*
20241 : : * Check if this extension is listed as to exclude in the dump. If
20242 : : * yes, any table data associated with it is discarded.
20243 : : */
20244 [ + + + + ]: 246 : if (extension_exclude_oids.head != NULL &&
20245 : 4 : simple_oid_list_member(&extension_exclude_oids,
20246 : : curext->dobj.catId.oid))
20247 : 2 : continue;
20248 : :
20249 [ + + - + ]: 240 : if (strlen(extconfig) != 0 || strlen(extcondition) != 0)
20250 : : {
20251 : : int j;
20252 : :
20253 [ - + ]: 20 : if (!parsePGArray(extconfig, &extconfigarray, &nconfigitems))
20254 : 0 : pg_fatal("could not parse %s array", "extconfig");
20255 [ - + ]: 20 : if (!parsePGArray(extcondition, &extconditionarray, &nconditionitems))
20256 : 0 : pg_fatal("could not parse %s array", "extcondition");
20257 [ - + ]: 20 : if (nconfigitems != nconditionitems)
20258 : 0 : pg_fatal("mismatched number of configurations and conditions for extension");
20259 : :
20260 [ + + ]: 60 : for (j = 0; j < nconfigitems; j++)
20261 : : {
20262 : : TableInfo *configtbl;
20263 : 40 : Oid configtbloid = atooid(extconfigarray[j]);
20264 : 40 : bool dumpobj =
20265 : 40 : curext->dobj.dump & DUMP_COMPONENT_DEFINITION;
20266 : :
20267 : 40 : configtbl = findTableByOid(configtbloid);
20268 [ - + ]: 40 : if (configtbl == NULL)
20269 : 0 : continue;
20270 : :
20271 : : /*
20272 : : * Tables of not-to-be-dumped extensions shouldn't be dumped
20273 : : * unless the table or its schema is explicitly included
20274 : : */
20275 [ + + ]: 40 : if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION))
20276 : : {
20277 : : /* check table explicitly requested */
20278 [ - + - - ]: 2 : if (table_include_oids.head != NULL &&
20279 : 0 : simple_oid_list_member(&table_include_oids,
20280 : : configtbloid))
20281 : 0 : dumpobj = true;
20282 : :
20283 : : /* check table's schema explicitly requested */
20284 [ + - ]: 2 : if (configtbl->dobj.namespace->dobj.dump &
20285 : : DUMP_COMPONENT_DATA)
20286 : 2 : dumpobj = true;
20287 : : }
20288 : :
20289 : : /* check table excluded by an exclusion switch */
20290 [ + + + + ]: 44 : if (table_exclude_oids.head != NULL &&
20291 : 4 : simple_oid_list_member(&table_exclude_oids,
20292 : : configtbloid))
20293 : 1 : dumpobj = false;
20294 : :
20295 : : /* check schema excluded by an exclusion switch */
20296 [ - + ]: 40 : if (simple_oid_list_member(&schema_exclude_oids,
20297 : 40 : configtbl->dobj.namespace->dobj.catId.oid))
20298 : 0 : dumpobj = false;
20299 : :
20300 [ + + ]: 40 : if (dumpobj)
20301 : : {
20302 : 39 : makeTableDataInfo(dopt, configtbl);
20303 [ + - ]: 39 : if (configtbl->dataObj != NULL)
20304 : : {
20305 [ - + ]: 39 : if (strlen(extconditionarray[j]) > 0)
20306 : 0 : configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
20307 : : }
20308 : : }
20309 : : }
20310 : : }
20311 [ + + ]: 240 : if (extconfigarray)
20312 : 20 : free(extconfigarray);
20313 [ + + ]: 240 : if (extconditionarray)
20314 : 20 : free(extconditionarray);
20315 : : }
20316 : :
20317 : : /*
20318 : : * Now that all the TableDataInfo objects have been created for all the
20319 : : * extensions, check their FK dependencies and register them to try and
20320 : : * dump the data out in an order that they can be restored in.
20321 : : *
20322 : : * Note that this is not a problem for user tables as their FKs are
20323 : : * recreated after the data has been loaded.
20324 : : */
20325 : :
20326 : 215 : query = createPQExpBuffer();
20327 : :
20328 : 215 : printfPQExpBuffer(query,
20329 : : "SELECT conrelid, confrelid "
20330 : : "FROM pg_constraint "
20331 : : "JOIN pg_depend ON (objid = confrelid) "
20332 : : "WHERE contype = 'f' "
20333 : : "AND refclassid = 'pg_extension'::regclass "
20334 : : "AND classid = 'pg_class'::regclass;");
20335 : :
20336 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20337 : 215 : ntups = PQntuples(res);
20338 : :
20339 : 215 : i_conrelid = PQfnumber(res, "conrelid");
20340 : 215 : i_confrelid = PQfnumber(res, "confrelid");
20341 : :
20342 : : /* Now get the dependencies and register them */
20343 [ - + ]: 215 : for (i = 0; i < ntups; i++)
20344 : : {
20345 : : Oid conrelid,
20346 : : confrelid;
20347 : : TableInfo *reftable,
20348 : : *contable;
20349 : :
20350 : 0 : conrelid = atooid(PQgetvalue(res, i, i_conrelid));
20351 : 0 : confrelid = atooid(PQgetvalue(res, i, i_confrelid));
20352 : 0 : contable = findTableByOid(conrelid);
20353 : 0 : reftable = findTableByOid(confrelid);
20354 : :
20355 [ # # ]: 0 : if (reftable == NULL ||
20356 [ # # # # ]: 0 : reftable->dataObj == NULL ||
20357 : 0 : contable == NULL ||
20358 [ # # ]: 0 : contable->dataObj == NULL)
20359 : 0 : continue;
20360 : :
20361 : : /*
20362 : : * Make referencing TABLE_DATA object depend on the referenced table's
20363 : : * TABLE_DATA object.
20364 : : */
20365 : 0 : addObjectDependency(&contable->dataObj->dobj,
20366 : 0 : reftable->dataObj->dobj.dumpId);
20367 : : }
20368 : 215 : PQclear(res);
20369 : 215 : destroyPQExpBuffer(query);
20370 : : }
20371 : :
20372 : : /*
20373 : : * getDependencies --- obtain available dependency data
20374 : : */
20375 : : static void
20376 : 215 : getDependencies(Archive *fout)
20377 : : {
20378 : : PQExpBuffer query;
20379 : : PGresult *res;
20380 : : int ntups,
20381 : : i;
20382 : : int i_classid,
20383 : : i_objid,
20384 : : i_refclassid,
20385 : : i_refobjid,
20386 : : i_deptype;
20387 : : DumpableObject *dobj,
20388 : : *refdobj;
20389 : :
20390 : 215 : pg_log_info("reading dependency data");
20391 : :
20392 : 215 : query = createPQExpBuffer();
20393 : :
20394 : : /*
20395 : : * Messy query to collect the dependency data we need. Note that we
20396 : : * ignore the sub-object column, so that dependencies of or on a column
20397 : : * look the same as dependencies of or on a whole table.
20398 : : *
20399 : : * PIN dependencies aren't interesting, and EXTENSION dependencies were
20400 : : * already processed by getExtensionMembership.
20401 : : */
20402 : 215 : appendPQExpBufferStr(query, "SELECT "
20403 : : "classid, objid, refclassid, refobjid, deptype "
20404 : : "FROM pg_depend "
20405 : : "WHERE deptype != 'p' AND deptype != 'e'\n");
20406 : :
20407 : : /*
20408 : : * Since we don't treat pg_amop entries as separate DumpableObjects, we
20409 : : * have to translate their dependencies into dependencies of their parent
20410 : : * opfamily. Ignore internal dependencies though, as those will point to
20411 : : * their parent opclass, which we needn't consider here (and if we did,
20412 : : * it'd just result in circular dependencies). Also, "loose" opfamily
20413 : : * entries will have dependencies on their parent opfamily, which we
20414 : : * should drop since they'd likewise become useless self-dependencies.
20415 : : * (But be sure to keep deps on *other* opfamilies; see amopsortfamily.)
20416 : : */
20417 : 215 : appendPQExpBufferStr(query, "UNION ALL\n"
20418 : : "SELECT 'pg_opfamily'::regclass AS classid, amopfamily AS objid, refclassid, refobjid, deptype "
20419 : : "FROM pg_depend d, pg_amop o "
20420 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20421 : : "classid = 'pg_amop'::regclass AND objid = o.oid "
20422 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amopfamily = refobjid)\n");
20423 : :
20424 : : /* Likewise for pg_amproc entries */
20425 : 215 : appendPQExpBufferStr(query, "UNION ALL\n"
20426 : : "SELECT 'pg_opfamily'::regclass AS classid, amprocfamily AS objid, refclassid, refobjid, deptype "
20427 : : "FROM pg_depend d, pg_amproc p "
20428 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20429 : : "classid = 'pg_amproc'::regclass AND objid = p.oid "
20430 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amprocfamily = refobjid)\n");
20431 : :
20432 : : /* Sort the output for efficiency below */
20433 : 215 : appendPQExpBufferStr(query, "ORDER BY 1,2");
20434 : :
20435 : 215 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20436 : :
20437 : 215 : ntups = PQntuples(res);
20438 : :
20439 : 215 : i_classid = PQfnumber(res, "classid");
20440 : 215 : i_objid = PQfnumber(res, "objid");
20441 : 215 : i_refclassid = PQfnumber(res, "refclassid");
20442 : 215 : i_refobjid = PQfnumber(res, "refobjid");
20443 : 215 : i_deptype = PQfnumber(res, "deptype");
20444 : :
20445 : : /*
20446 : : * Since we ordered the SELECT by referencing ID, we can expect that
20447 : : * multiple entries for the same object will appear together; this saves
20448 : : * on searches.
20449 : : */
20450 : 215 : dobj = NULL;
20451 : :
20452 [ + + ]: 467575 : for (i = 0; i < ntups; i++)
20453 : : {
20454 : : CatalogId objId;
20455 : : CatalogId refobjId;
20456 : : char deptype;
20457 : :
20458 : 467360 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
20459 : 467360 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
20460 : 467360 : refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
20461 : 467360 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
20462 : 467360 : deptype = *(PQgetvalue(res, i, i_deptype));
20463 : :
20464 [ + + ]: 467360 : if (dobj == NULL ||
20465 [ + + ]: 440068 : dobj->catId.tableoid != objId.tableoid ||
20466 [ + + ]: 437720 : dobj->catId.oid != objId.oid)
20467 : 204079 : dobj = findObjectByCatalogId(objId);
20468 : :
20469 : : /*
20470 : : * Failure to find objects mentioned in pg_depend is not unexpected,
20471 : : * since for example we don't collect info about TOAST tables.
20472 : : */
20473 [ + + ]: 467360 : if (dobj == NULL)
20474 : : {
20475 : : #ifdef NOT_USED
20476 : : pg_log_warning("no referencing object %u %u",
20477 : : objId.tableoid, objId.oid);
20478 : : #endif
20479 : 28058 : continue;
20480 : : }
20481 : :
20482 : 440283 : refdobj = findObjectByCatalogId(refobjId);
20483 : :
20484 [ + + ]: 440283 : if (refdobj == NULL)
20485 : : {
20486 : : #ifdef NOT_USED
20487 : : pg_log_warning("no referenced object %u %u",
20488 : : refobjId.tableoid, refobjId.oid);
20489 : : #endif
20490 : 981 : continue;
20491 : : }
20492 : :
20493 : : /*
20494 : : * For 'x' dependencies, mark the object for later; we still add the
20495 : : * normal dependency, for possible ordering purposes. Currently
20496 : : * pg_dump_sort.c knows to put extensions ahead of all object types
20497 : : * that could possibly depend on them, but this is safer.
20498 : : */
20499 [ + + ]: 439302 : if (deptype == 'x')
20500 : 44 : dobj->depends_on_ext = true;
20501 : :
20502 : : /*
20503 : : * Ordinarily, table rowtypes have implicit dependencies on their
20504 : : * tables. However, for a composite type the implicit dependency goes
20505 : : * the other way in pg_depend; which is the right thing for DROP but
20506 : : * it doesn't produce the dependency ordering we need. The same is
20507 : : * true for a subscription's conflict log table, which has an internal
20508 : : * dependency on its subscription for DROP, but must be dumped before
20509 : : * the subscription in binary-upgrade mode. So in those cases, we
20510 : : * reverse the direction of the dependency.
20511 : : */
20512 [ + + ]: 439302 : if (deptype == 'i' &&
20513 [ + + ]: 125192 : dobj->objType == DO_TABLE &&
20514 [ + + ]: 1490 : (refdobj->objType == DO_TYPE ||
20515 [ + + ]: 1244 : refdobj->objType == DO_SUBSCRIPTION))
20516 : 338 : addObjectDependency(refdobj, dobj->dumpId);
20517 : : else
20518 : : /* normal case */
20519 : 438964 : addObjectDependency(dobj, refdobj->dumpId);
20520 : : }
20521 : :
20522 : 215 : PQclear(res);
20523 : :
20524 : 215 : destroyPQExpBuffer(query);
20525 : 215 : }
20526 : :
20527 : :
20528 : : /*
20529 : : * createBoundaryObjects - create dummy DumpableObjects to represent
20530 : : * dump section boundaries.
20531 : : */
20532 : : static DumpableObject *
20533 : 215 : createBoundaryObjects(void)
20534 : : {
20535 : : DumpableObject *dobjs;
20536 : :
20537 : 215 : dobjs = pg_malloc_array(DumpableObject, 2);
20538 : :
20539 : 215 : dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
20540 : 215 : dobjs[0].catId = nilCatalogId;
20541 : 215 : AssignDumpId(dobjs + 0);
20542 : 215 : dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
20543 : :
20544 : 215 : dobjs[1].objType = DO_POST_DATA_BOUNDARY;
20545 : 215 : dobjs[1].catId = nilCatalogId;
20546 : 215 : AssignDumpId(dobjs + 1);
20547 : 215 : dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
20548 : :
20549 : 215 : return dobjs;
20550 : : }
20551 : :
20552 : : /*
20553 : : * addBoundaryDependencies - add dependencies as needed to enforce the dump
20554 : : * section boundaries.
20555 : : */
20556 : : static void
20557 : 215 : addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
20558 : : DumpableObject *boundaryObjs)
20559 : : {
20560 : 215 : DumpableObject *preDataBound = boundaryObjs + 0;
20561 : 215 : DumpableObject *postDataBound = boundaryObjs + 1;
20562 : : int i;
20563 : :
20564 [ + + ]: 811792 : for (i = 0; i < numObjs; i++)
20565 : : {
20566 : 811577 : DumpableObject *dobj = dobjs[i];
20567 : :
20568 : : /*
20569 : : * The classification of object types here must match the SECTION_xxx
20570 : : * values assigned during subsequent ArchiveEntry calls!
20571 : : */
20572 [ + + + + : 811577 : switch (dobj->objType)
+ + + +
- ]
20573 : : {
20574 : 758546 : case DO_NAMESPACE:
20575 : : case DO_EXTENSION:
20576 : : case DO_TYPE:
20577 : : case DO_SHELL_TYPE:
20578 : : case DO_FUNC:
20579 : : case DO_AGG:
20580 : : case DO_OPERATOR:
20581 : : case DO_ACCESS_METHOD:
20582 : : case DO_OPCLASS:
20583 : : case DO_OPFAMILY:
20584 : : case DO_COLLATION:
20585 : : case DO_CONVERSION:
20586 : : case DO_TABLE:
20587 : : case DO_TABLE_ATTACH:
20588 : : case DO_ATTRDEF:
20589 : : case DO_PROCLANG:
20590 : : case DO_CAST:
20591 : : case DO_DUMMY_TYPE:
20592 : : case DO_TSPARSER:
20593 : : case DO_TSDICT:
20594 : : case DO_TSTEMPLATE:
20595 : : case DO_TSCONFIG:
20596 : : case DO_FDW:
20597 : : case DO_FOREIGN_SERVER:
20598 : : case DO_TRANSFORM:
20599 : : /* Pre-data objects: must come before the pre-data boundary */
20600 : 758546 : addObjectDependency(preDataBound, dobj->dumpId);
20601 : 758546 : break;
20602 : 5229 : case DO_TABLE_DATA:
20603 : : case DO_SEQUENCE_SET:
20604 : : case DO_LARGE_OBJECT:
20605 : : case DO_LARGE_OBJECT_DATA:
20606 : : /* Data objects: must come between the boundaries */
20607 : 5229 : addObjectDependency(dobj, preDataBound->dumpId);
20608 : 5229 : addObjectDependency(postDataBound, dobj->dumpId);
20609 : 5229 : break;
20610 : 6370 : case DO_INDEX:
20611 : : case DO_INDEX_ATTACH:
20612 : : case DO_STATSEXT:
20613 : : case DO_REFRESH_MATVIEW:
20614 : : case DO_TRIGGER:
20615 : : case DO_EVENT_TRIGGER:
20616 : : case DO_DEFAULT_ACL:
20617 : : case DO_POLICY:
20618 : : case DO_PUBLICATION:
20619 : : case DO_PUBLICATION_REL:
20620 : : case DO_PUBLICATION_TABLE_IN_SCHEMA:
20621 : : case DO_SUBSCRIPTION:
20622 : : case DO_SUBSCRIPTION_REL:
20623 : : /* Post-data objects: must come after the post-data boundary */
20624 : 6370 : addObjectDependency(dobj, postDataBound->dumpId);
20625 : 6370 : break;
20626 : 34524 : case DO_RULE:
20627 : : /* Rules are post-data, but only if dumped separately */
20628 [ + + ]: 34524 : if (((RuleInfo *) dobj)->separate)
20629 : 703 : addObjectDependency(dobj, postDataBound->dumpId);
20630 : 34524 : break;
20631 : 2678 : case DO_CONSTRAINT:
20632 : : case DO_FK_CONSTRAINT:
20633 : : /* Constraints are post-data, but only if dumped separately */
20634 [ + + ]: 2678 : if (((ConstraintInfo *) dobj)->separate)
20635 : 1934 : addObjectDependency(dobj, postDataBound->dumpId);
20636 : 2678 : break;
20637 : 215 : case DO_PRE_DATA_BOUNDARY:
20638 : : /* nothing to do */
20639 : 215 : break;
20640 : 215 : case DO_POST_DATA_BOUNDARY:
20641 : : /* must come after the pre-data boundary */
20642 : 215 : addObjectDependency(dobj, preDataBound->dumpId);
20643 : 215 : break;
20644 : 3800 : case DO_REL_STATS:
20645 : : /* stats section varies by parent object type, DATA or POST */
20646 [ + + ]: 3800 : if (((RelStatsInfo *) dobj)->section == SECTION_DATA)
20647 : : {
20648 : 2501 : addObjectDependency(dobj, preDataBound->dumpId);
20649 : 2501 : addObjectDependency(postDataBound, dobj->dumpId);
20650 : : }
20651 : : else
20652 : 1299 : addObjectDependency(dobj, postDataBound->dumpId);
20653 : 3800 : break;
20654 : : }
20655 : : }
20656 : 215 : }
20657 : :
20658 : :
20659 : : /*
20660 : : * BuildArchiveDependencies - create dependency data for archive TOC entries
20661 : : *
20662 : : * The raw dependency data obtained by getDependencies() is not terribly
20663 : : * useful in an archive dump, because in many cases there are dependency
20664 : : * chains linking through objects that don't appear explicitly in the dump.
20665 : : * For example, a view will depend on its _RETURN rule while the _RETURN rule
20666 : : * will depend on other objects --- but the rule will not appear as a separate
20667 : : * object in the dump. We need to adjust the view's dependencies to include
20668 : : * whatever the rule depends on that is included in the dump.
20669 : : *
20670 : : * Just to make things more complicated, there are also "special" dependencies
20671 : : * such as the dependency of a TABLE DATA item on its TABLE, which we must
20672 : : * not rearrange because pg_restore knows that TABLE DATA only depends on
20673 : : * its table. In these cases we must leave the dependencies strictly as-is
20674 : : * even if they refer to not-to-be-dumped objects.
20675 : : *
20676 : : * To handle this, the convention is that "special" dependencies are created
20677 : : * during ArchiveEntry calls, and an archive TOC item that has any such
20678 : : * entries will not be touched here. Otherwise, we recursively search the
20679 : : * DumpableObject data structures to build the correct dependencies for each
20680 : : * archive TOC item.
20681 : : */
20682 : : static void
20683 : 73 : BuildArchiveDependencies(Archive *fout)
20684 : : {
20685 : 73 : ArchiveHandle *AH = (ArchiveHandle *) fout;
20686 : : TocEntry *te;
20687 : :
20688 : : /* Scan all TOC entries in the archive */
20689 [ + + ]: 8079 : for (te = AH->toc->next; te != AH->toc; te = te->next)
20690 : : {
20691 : : DumpableObject *dobj;
20692 : : DumpId *dependencies;
20693 : : int nDeps;
20694 : : int allocDeps;
20695 : :
20696 : : /* No need to process entries that will not be dumped */
20697 [ + + ]: 8006 : if (te->reqs == 0)
20698 : 4006 : continue;
20699 : : /* Ignore entries that already have "special" dependencies */
20700 [ + + ]: 7998 : if (te->nDeps > 0)
20701 : 3447 : continue;
20702 : : /* Otherwise, look up the item's original DumpableObject, if any */
20703 : 4551 : dobj = findObjectByDumpId(te->dumpId);
20704 [ + + ]: 4551 : if (dobj == NULL)
20705 : 430 : continue;
20706 : : /* No work if it has no dependencies */
20707 [ + + ]: 4121 : if (dobj->nDeps <= 0)
20708 : 121 : continue;
20709 : : /* Set up work array */
20710 : 4000 : allocDeps = 64;
20711 : 4000 : dependencies = pg_malloc_array(DumpId, allocDeps);
20712 : 4000 : nDeps = 0;
20713 : : /* Recursively find all dumpable dependencies */
20714 : 4000 : findDumpableDependencies(AH, dobj,
20715 : : &dependencies, &nDeps, &allocDeps);
20716 : : /* And save 'em ... */
20717 [ + + ]: 4000 : if (nDeps > 0)
20718 : : {
20719 : 3038 : dependencies = pg_realloc_array(dependencies, DumpId, nDeps);
20720 : 3038 : te->dependencies = dependencies;
20721 : 3038 : te->nDeps = nDeps;
20722 : : }
20723 : : else
20724 : 962 : pg_free(dependencies);
20725 : : }
20726 : 73 : }
20727 : :
20728 : : /* Recursive search subroutine for BuildArchiveDependencies */
20729 : : static void
20730 : 9432 : findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
20731 : : DumpId **dependencies, int *nDeps, int *allocDeps)
20732 : : {
20733 : : int i;
20734 : :
20735 : : /*
20736 : : * Ignore section boundary objects: if we search through them, we'll
20737 : : * report lots of bogus dependencies.
20738 : : */
20739 [ + + ]: 9432 : if (dobj->objType == DO_PRE_DATA_BOUNDARY ||
20740 [ + + ]: 9411 : dobj->objType == DO_POST_DATA_BOUNDARY)
20741 : 1671 : return;
20742 : :
20743 [ + + ]: 19515 : for (i = 0; i < dobj->nDeps; i++)
20744 : : {
20745 : 11754 : DumpId depid = dobj->dependencies[i];
20746 : :
20747 [ + + ]: 11754 : if (TocIDRequired(AH, depid) != 0)
20748 : : {
20749 : : /* Object will be dumped, so just reference it as a dependency */
20750 [ - + ]: 6322 : if (*nDeps >= *allocDeps)
20751 : : {
20752 : 0 : *allocDeps *= 2;
20753 : 0 : *dependencies = pg_realloc_array(*dependencies, DumpId, *allocDeps);
20754 : : }
20755 : 6322 : (*dependencies)[*nDeps] = depid;
20756 : 6322 : (*nDeps)++;
20757 : : }
20758 : : else
20759 : : {
20760 : : /*
20761 : : * Object will not be dumped, so recursively consider its deps. We
20762 : : * rely on the assumption that sortDumpableObjects already broke
20763 : : * any dependency loops, else we might recurse infinitely.
20764 : : */
20765 : 5432 : DumpableObject *otherdobj = findObjectByDumpId(depid);
20766 : :
20767 [ + - ]: 5432 : if (otherdobj)
20768 : 5432 : findDumpableDependencies(AH, otherdobj,
20769 : : dependencies, nDeps, allocDeps);
20770 : : }
20771 : : }
20772 : : }
20773 : :
20774 : :
20775 : : /*
20776 : : * getFormattedTypeName - retrieve a nicely-formatted type name for the
20777 : : * given type OID.
20778 : : *
20779 : : * This does not guarantee to schema-qualify the output, so it should not
20780 : : * be used to create the target object name for CREATE or ALTER commands.
20781 : : *
20782 : : * Note that the result is cached and must not be freed by the caller.
20783 : : */
20784 : : static const char *
20785 : 2380 : getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
20786 : : {
20787 : : TypeInfo *typeInfo;
20788 : : char *result;
20789 : : PQExpBuffer query;
20790 : : PGresult *res;
20791 : :
20792 [ - + ]: 2380 : if (oid == 0)
20793 : : {
20794 [ # # ]: 0 : if ((opts & zeroAsStar) != 0)
20795 : 0 : return "*";
20796 [ # # ]: 0 : else if ((opts & zeroAsNone) != 0)
20797 : 0 : return "NONE";
20798 : : }
20799 : :
20800 : : /* see if we have the result cached in the type's TypeInfo record */
20801 : 2380 : typeInfo = findTypeByOid(oid);
20802 [ + - + + ]: 2380 : if (typeInfo && typeInfo->ftypname)
20803 : 1898 : return typeInfo->ftypname;
20804 : :
20805 : 482 : query = createPQExpBuffer();
20806 : 482 : appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
20807 : : oid);
20808 : :
20809 : 482 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
20810 : :
20811 : : /* result of format_type is already quoted */
20812 : 482 : result = pg_strdup(PQgetvalue(res, 0, 0));
20813 : :
20814 : 482 : PQclear(res);
20815 : 482 : destroyPQExpBuffer(query);
20816 : :
20817 : : /*
20818 : : * Cache the result for re-use in later requests, if possible. If we
20819 : : * don't have a TypeInfo for the type, the string will be leaked once the
20820 : : * caller is done with it ... but that case really should not happen, so
20821 : : * leaking if it does seems acceptable.
20822 : : */
20823 [ + - ]: 482 : if (typeInfo)
20824 : 482 : typeInfo->ftypname = result;
20825 : :
20826 : 482 : return result;
20827 : : }
20828 : :
20829 : : /*
20830 : : * Return a column list clause for the given relation.
20831 : : *
20832 : : * Special case: if there are no undropped columns in the relation, return
20833 : : * "", not an invalid "()" column list.
20834 : : */
20835 : : static const char *
20836 : 8974 : fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer)
20837 : : {
20838 : 8974 : int numatts = ti->numatts;
20839 : 8974 : char **attnames = ti->attnames;
20840 : 8974 : bool *attisdropped = ti->attisdropped;
20841 : 8974 : char *attgenerated = ti->attgenerated;
20842 : : bool needComma;
20843 : : int i;
20844 : :
20845 : 8974 : appendPQExpBufferChar(buffer, '(');
20846 : 8974 : needComma = false;
20847 [ + + ]: 42688 : for (i = 0; i < numatts; i++)
20848 : : {
20849 [ + + ]: 33714 : if (attisdropped[i])
20850 : 784 : continue;
20851 [ + + ]: 32930 : if (attgenerated[i])
20852 : 1252 : continue;
20853 [ + + ]: 31678 : if (needComma)
20854 : 22974 : appendPQExpBufferStr(buffer, ", ");
20855 : 31678 : appendPQExpBufferStr(buffer, fmtId(attnames[i]));
20856 : 31678 : needComma = true;
20857 : : }
20858 : :
20859 [ + + ]: 8974 : if (!needComma)
20860 : 270 : return ""; /* no undropped columns */
20861 : :
20862 : 8704 : appendPQExpBufferChar(buffer, ')');
20863 : 8704 : return buffer->data;
20864 : : }
20865 : :
20866 : : /*
20867 : : * Check if a reloptions array is nonempty.
20868 : : */
20869 : : static bool
20870 : 14661 : nonemptyReloptions(const char *reloptions)
20871 : : {
20872 : : /* Don't want to print it if it's just "{}" */
20873 [ + - + + ]: 14661 : return (reloptions != NULL && strlen(reloptions) > 2);
20874 : : }
20875 : :
20876 : : /*
20877 : : * Format a reloptions array and append it to the given buffer.
20878 : : *
20879 : : * "prefix" is prepended to the option names; typically it's "" or "toast.".
20880 : : */
20881 : : static void
20882 : 223 : appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
20883 : : const char *prefix, Archive *fout)
20884 : : {
20885 : : bool res;
20886 : :
20887 : 223 : res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
20888 : 223 : fout->std_strings);
20889 [ - + ]: 223 : if (!res)
20890 : 0 : pg_log_warning("could not parse %s array", "reloptions");
20891 : 223 : }
20892 : :
20893 : : /*
20894 : : * read_dump_filters - retrieve object identifier patterns from file
20895 : : *
20896 : : * Parse the specified filter file for include and exclude patterns, and add
20897 : : * them to the relevant lists. If the filename is "-" then filters will be
20898 : : * read from STDIN rather than a file.
20899 : : */
20900 : : static void
20901 : 26 : read_dump_filters(const char *filename, DumpOptions *dopt)
20902 : : {
20903 : : FilterStateData fstate;
20904 : : char *objname;
20905 : : FilterCommandType comtype;
20906 : : FilterObjectType objtype;
20907 : :
20908 : 26 : filter_init(&fstate, filename, exit_nicely);
20909 : :
20910 [ + + ]: 84 : while (filter_read_item(&fstate, &objname, &comtype, &objtype))
20911 : : {
20912 [ + + ]: 33 : if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
20913 : : {
20914 [ - - + + : 17 : switch (objtype)
+ + + - ]
20915 : : {
20916 : 0 : case FILTER_OBJECT_TYPE_NONE:
20917 : 0 : break;
20918 : 0 : case FILTER_OBJECT_TYPE_DATABASE:
20919 : : case FILTER_OBJECT_TYPE_FUNCTION:
20920 : : case FILTER_OBJECT_TYPE_INDEX:
20921 : : case FILTER_OBJECT_TYPE_TABLE_DATA:
20922 : : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20923 : : case FILTER_OBJECT_TYPE_TRIGGER:
20924 : 0 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20925 : : "include",
20926 : : filter_object_type_name(objtype));
20927 : 0 : exit_nicely(1);
20928 : : break; /* unreachable */
20929 : :
20930 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20931 : 1 : simple_string_list_append(&extension_include_patterns, objname);
20932 : 1 : break;
20933 : 1 : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20934 : 1 : simple_string_list_append(&foreign_servers_include_patterns, objname);
20935 : 1 : break;
20936 : 1 : case FILTER_OBJECT_TYPE_SCHEMA:
20937 : 1 : simple_string_list_append(&schema_include_patterns, objname);
20938 : 1 : dopt->include_everything = false;
20939 : 1 : break;
20940 : 13 : case FILTER_OBJECT_TYPE_TABLE:
20941 : 13 : simple_string_list_append(&table_include_patterns, objname);
20942 : 13 : dopt->include_everything = false;
20943 : 13 : break;
20944 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20945 : 1 : simple_string_list_append(&table_include_patterns_and_children,
20946 : : objname);
20947 : 1 : dopt->include_everything = false;
20948 : 1 : break;
20949 : : }
20950 : : }
20951 [ + + ]: 16 : else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
20952 : : {
20953 [ - + + + : 9 : switch (objtype)
+ + + +
- ]
20954 : : {
20955 : 0 : case FILTER_OBJECT_TYPE_NONE:
20956 : 0 : break;
20957 : 1 : case FILTER_OBJECT_TYPE_DATABASE:
20958 : : case FILTER_OBJECT_TYPE_FUNCTION:
20959 : : case FILTER_OBJECT_TYPE_INDEX:
20960 : : case FILTER_OBJECT_TYPE_TRIGGER:
20961 : : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20962 : 1 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20963 : : "exclude",
20964 : : filter_object_type_name(objtype));
20965 : 1 : exit_nicely(1);
20966 : : break;
20967 : :
20968 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20969 : 1 : simple_string_list_append(&extension_exclude_patterns, objname);
20970 : 1 : break;
20971 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA:
20972 : 1 : simple_string_list_append(&tabledata_exclude_patterns,
20973 : : objname);
20974 : 1 : break;
20975 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20976 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
20977 : : objname);
20978 : 1 : break;
20979 : 2 : case FILTER_OBJECT_TYPE_SCHEMA:
20980 : 2 : simple_string_list_append(&schema_exclude_patterns, objname);
20981 : 2 : break;
20982 : 2 : case FILTER_OBJECT_TYPE_TABLE:
20983 : 2 : simple_string_list_append(&table_exclude_patterns, objname);
20984 : 2 : break;
20985 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20986 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
20987 : : objname);
20988 : 1 : break;
20989 : : }
20990 : : }
20991 : : else
20992 : : {
20993 : : Assert(comtype == FILTER_COMMAND_TYPE_NONE);
20994 : : Assert(objtype == FILTER_OBJECT_TYPE_NONE);
20995 : : }
20996 : :
20997 [ + + ]: 32 : if (objname)
20998 : 25 : free(objname);
20999 : : }
21000 : :
21001 : 22 : filter_free(&fstate);
21002 : 22 : }
|