Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_dump.c
4 : : * pg_dump is a utility for dumping out a postgres database
5 : : * into a script file.
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * pg_dump will read the system catalogs in a database and dump out a
11 : : * script that reproduces the schema in terms of SQL that is understood
12 : : * by PostgreSQL
13 : : *
14 : : * Note that pg_dump runs in a transaction-snapshot mode transaction,
15 : : * so it sees a consistent snapshot of the database including system
16 : : * catalogs. However, it relies in part on various specialized backend
17 : : * functions like pg_get_indexdef(), and those things tend to look at
18 : : * the currently committed state. So it is possible to get 'cache
19 : : * lookup failed' error if someone performs DDL changes while a dump is
20 : : * happening. The window for this sort of thing is from the acquisition
21 : : * of the transaction snapshot to getSchemaData() (when pg_dump acquires
22 : : * AccessShareLock on every table it intends to dump). It isn't very large,
23 : : * but it can happen.
24 : : *
25 : : * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
26 : : *
27 : : * IDENTIFICATION
28 : : * src/bin/pg_dump/pg_dump.c
29 : : *
30 : : *-------------------------------------------------------------------------
31 : : */
32 : : #include "postgres_fe.h"
33 : :
34 : : #include <unistd.h>
35 : : #include <ctype.h>
36 : : #include <limits.h>
37 : : #ifdef HAVE_TERMIOS_H
38 : : #include <termios.h>
39 : : #endif
40 : :
41 : : #include "access/attnum.h"
42 : : #include "access/sysattr.h"
43 : : #include "access/transam.h"
44 : : #include "catalog/pg_aggregate_d.h"
45 : : #include "catalog/pg_am_d.h"
46 : : #include "catalog/pg_attribute_d.h"
47 : : #include "catalog/pg_authid_d.h"
48 : : #include "catalog/pg_cast_d.h"
49 : : #include "catalog/pg_class_d.h"
50 : : #include "catalog/pg_constraint_d.h"
51 : : #include "catalog/pg_default_acl_d.h"
52 : : #include "catalog/pg_largeobject_d.h"
53 : : #include "catalog/pg_largeobject_metadata_d.h"
54 : : #include "catalog/pg_proc_d.h"
55 : : #include "catalog/pg_publication_d.h"
56 : : #include "catalog/pg_shdepend_d.h"
57 : : #include "catalog/pg_subscription_d.h"
58 : : #include "catalog/pg_type_d.h"
59 : : #include "common/connect.h"
60 : : #include "common/int.h"
61 : : #include "common/relpath.h"
62 : : #include "common/shortest_dec.h"
63 : : #include "compress_io.h"
64 : : #include "dumputils.h"
65 : : #include "fe_utils/option_utils.h"
66 : : #include "fe_utils/string_utils.h"
67 : : #include "filter.h"
68 : : #include "getopt_long.h"
69 : : #include "libpq/libpq-fs.h"
70 : : #include "parallel.h"
71 : : #include "pg_backup_db.h"
72 : : #include "pg_backup_utils.h"
73 : : #include "pg_dump.h"
74 : : #include "statistics/statistics_format.h"
75 : : #include "storage/block.h"
76 : :
77 : : typedef struct
78 : : {
79 : : Oid roleoid; /* role's OID */
80 : : const char *rolename; /* role's name */
81 : : } RoleNameItem;
82 : :
83 : : typedef struct
84 : : {
85 : : const char *descr; /* comment for an object */
86 : : Oid classoid; /* object class (catalog OID) */
87 : : Oid objoid; /* object OID */
88 : : int objsubid; /* subobject (table column #) */
89 : : } CommentItem;
90 : :
91 : : typedef struct
92 : : {
93 : : const char *provider; /* label provider of this security label */
94 : : const char *label; /* security label for an object */
95 : : Oid classoid; /* object class (catalog OID) */
96 : : Oid objoid; /* object OID */
97 : : int objsubid; /* subobject (table column #) */
98 : : } SecLabelItem;
99 : :
100 : : typedef struct
101 : : {
102 : : Oid oid; /* object OID */
103 : : char relkind; /* object kind */
104 : : RelFileNumber relfilenumber; /* object filenode */
105 : : Oid toast_oid; /* toast table OID */
106 : : RelFileNumber toast_relfilenumber; /* toast table filenode */
107 : : Oid toast_index_oid; /* toast table index OID */
108 : : RelFileNumber toast_index_relfilenumber; /* toast table index filenode */
109 : : } BinaryUpgradeClassOidItem;
110 : :
111 : : /* sequence types */
112 : : typedef enum SeqType
113 : : {
114 : : SEQTYPE_SMALLINT,
115 : : SEQTYPE_INTEGER,
116 : : SEQTYPE_BIGINT,
117 : : } SeqType;
118 : :
119 : : static const char *const SeqTypeNames[] =
120 : : {
121 : : [SEQTYPE_SMALLINT] = "smallint",
122 : : [SEQTYPE_INTEGER] = "integer",
123 : : [SEQTYPE_BIGINT] = "bigint",
124 : : };
125 : :
126 : : StaticAssertDecl(lengthof(SeqTypeNames) == (SEQTYPE_BIGINT + 1),
127 : : "array length mismatch");
128 : :
129 : : typedef struct
130 : : {
131 : : Oid oid; /* sequence OID */
132 : : SeqType seqtype; /* data type of sequence */
133 : : bool cycled; /* whether sequence cycles */
134 : : int64 minv; /* minimum value */
135 : : int64 maxv; /* maximum value */
136 : : int64 startv; /* start value */
137 : : int64 incby; /* increment value */
138 : : int64 cache; /* cache size */
139 : : int64 last_value; /* last value of sequence */
140 : : bool is_called; /* whether nextval advances before returning */
141 : : bool null_seqtuple; /* did pg_get_sequence_data return nulls? */
142 : : } SequenceItem;
143 : :
144 : : typedef enum OidOptions
145 : : {
146 : : zeroIsError = 1,
147 : : zeroAsStar = 2,
148 : : zeroAsNone = 4,
149 : : } OidOptions;
150 : :
151 : : /* global decls */
152 : : static bool dosync = true; /* Issue fsync() to make dump durable on disk. */
153 : :
154 : : static Oid g_last_builtin_oid; /* value of the last builtin oid */
155 : :
156 : : /* The specified names/patterns should to match at least one entity */
157 : : static int strict_names = 0;
158 : :
159 : : static pg_compress_algorithm compression_algorithm = PG_COMPRESSION_NONE;
160 : :
161 : : /*
162 : : * Object inclusion/exclusion lists
163 : : *
164 : : * The string lists record the patterns given by command-line switches,
165 : : * which we then convert to lists of OIDs of matching objects.
166 : : */
167 : : static SimpleStringList schema_include_patterns = {NULL, NULL};
168 : : static SimpleOidList schema_include_oids = {NULL, NULL};
169 : : static SimpleStringList schema_exclude_patterns = {NULL, NULL};
170 : : static SimpleOidList schema_exclude_oids = {NULL, NULL};
171 : :
172 : : static SimpleStringList table_include_patterns = {NULL, NULL};
173 : : static SimpleStringList table_include_patterns_and_children = {NULL, NULL};
174 : : static SimpleOidList table_include_oids = {NULL, NULL};
175 : : static SimpleStringList table_exclude_patterns = {NULL, NULL};
176 : : static SimpleStringList table_exclude_patterns_and_children = {NULL, NULL};
177 : : static SimpleOidList table_exclude_oids = {NULL, NULL};
178 : : static SimpleStringList tabledata_exclude_patterns = {NULL, NULL};
179 : : static SimpleStringList tabledata_exclude_patterns_and_children = {NULL, NULL};
180 : : static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
181 : :
182 : : static SimpleStringList foreign_servers_include_patterns = {NULL, NULL};
183 : : static SimpleOidList foreign_servers_include_oids = {NULL, NULL};
184 : :
185 : : static SimpleStringList extension_include_patterns = {NULL, NULL};
186 : : static SimpleOidList extension_include_oids = {NULL, NULL};
187 : :
188 : : static SimpleStringList extension_exclude_patterns = {NULL, NULL};
189 : : static SimpleOidList extension_exclude_oids = {NULL, NULL};
190 : :
191 : : static const CatalogId nilCatalogId = {0, 0};
192 : :
193 : : /* override for standard extra_float_digits setting */
194 : : static bool have_extra_float_digits = false;
195 : : static int extra_float_digits;
196 : :
197 : : /* sorted table of role names */
198 : : static RoleNameItem *rolenames = NULL;
199 : : static int nrolenames = 0;
200 : :
201 : : /* sorted table of comments */
202 : : static CommentItem *comments = NULL;
203 : : static int ncomments = 0;
204 : :
205 : : /* sorted table of security labels */
206 : : static SecLabelItem *seclabels = NULL;
207 : : static int nseclabels = 0;
208 : :
209 : : /* sorted table of pg_class information for binary upgrade */
210 : : static BinaryUpgradeClassOidItem *binaryUpgradeClassOids = NULL;
211 : : static int nbinaryUpgradeClassOids = 0;
212 : :
213 : : /* sorted table of sequences */
214 : : static SequenceItem *sequences = NULL;
215 : : static int nsequences = 0;
216 : :
217 : : /* Maximum number of relations to fetch in a fetchAttributeStats() call. */
218 : : #define MAX_ATTR_STATS_RELS 64
219 : :
220 : : /*
221 : : * The default number of rows per INSERT when
222 : : * --inserts is specified without --rows-per-insert
223 : : */
224 : : #define DUMP_DEFAULT_ROWS_PER_INSERT 1
225 : :
226 : : /*
227 : : * Maximum number of large objects to group into a single ArchiveEntry.
228 : : * At some point we might want to make this user-controllable, but for now
229 : : * a hard-wired setting will suffice.
230 : : */
231 : : #define MAX_BLOBS_PER_ARCHIVE_ENTRY 1000
232 : :
233 : : /*
234 : : * Macro for producing quoted, schema-qualified name of a dumpable object.
235 : : */
236 : : #define fmtQualifiedDumpable(obj) \
237 : : fmtQualifiedId((obj)->dobj.namespace->dobj.name, \
238 : : (obj)->dobj.name)
239 : :
240 : : static void help(const char *progname);
241 : : static void setup_connection(Archive *AH,
242 : : const char *dumpencoding, const char *dumpsnapshot,
243 : : char *use_role);
244 : : static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
245 : : static void expand_schema_name_patterns(Archive *fout,
246 : : SimpleStringList *patterns,
247 : : SimpleOidList *oids,
248 : : bool strict_names);
249 : : static void expand_extension_name_patterns(Archive *fout,
250 : : SimpleStringList *patterns,
251 : : SimpleOidList *oids,
252 : : bool strict_names);
253 : : static void expand_foreign_server_name_patterns(Archive *fout,
254 : : SimpleStringList *patterns,
255 : : SimpleOidList *oids);
256 : : static void expand_table_name_patterns(Archive *fout,
257 : : SimpleStringList *patterns,
258 : : SimpleOidList *oids,
259 : : bool strict_names,
260 : : bool with_child_tables);
261 : : static void prohibit_crossdb_refs(PGconn *conn, const char *dbname,
262 : : const char *pattern);
263 : :
264 : : static NamespaceInfo *findNamespace(Oid nsoid);
265 : : static void dumpTableData(Archive *fout, const TableDataInfo *tdinfo);
266 : : static void refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo);
267 : : static const char *getRoleName(const char *roleoid_str);
268 : : static void collectRoleNames(Archive *fout);
269 : : static void getAdditionalACLs(Archive *fout);
270 : : static void dumpCommentExtended(Archive *fout, const char *type,
271 : : const char *name, const char *namespace,
272 : : const char *owner, CatalogId catalogId,
273 : : int subid, DumpId dumpId,
274 : : const char *initdb_comment);
275 : : static inline void dumpComment(Archive *fout, const char *type,
276 : : const char *name, const char *namespace,
277 : : const char *owner, CatalogId catalogId,
278 : : int subid, DumpId dumpId);
279 : : static int findComments(Oid classoid, Oid objoid, CommentItem **items);
280 : : static void collectComments(Archive *fout);
281 : : static void dumpSecLabel(Archive *fout, const char *type, const char *name,
282 : : const char *namespace, const char *owner,
283 : : CatalogId catalogId, int subid, DumpId dumpId);
284 : : static int findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items);
285 : : static void collectSecLabels(Archive *fout);
286 : : static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
287 : : static void dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo);
288 : : static void dumpExtension(Archive *fout, const ExtensionInfo *extinfo);
289 : : static void dumpType(Archive *fout, const TypeInfo *tyinfo);
290 : : static void dumpBaseType(Archive *fout, const TypeInfo *tyinfo);
291 : : static void dumpEnumType(Archive *fout, const TypeInfo *tyinfo);
292 : : static void dumpRangeType(Archive *fout, const TypeInfo *tyinfo);
293 : : static void dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo);
294 : : static void dumpDomain(Archive *fout, const TypeInfo *tyinfo);
295 : : static void dumpCompositeType(Archive *fout, const TypeInfo *tyinfo);
296 : : static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
297 : : PGresult *res);
298 : : static void dumpShellType(Archive *fout, const ShellTypeInfo *stinfo);
299 : : static void dumpProcLang(Archive *fout, const ProcLangInfo *plang);
300 : : static void dumpFunc(Archive *fout, const FuncInfo *finfo);
301 : : static void dumpCast(Archive *fout, const CastInfo *cast);
302 : : static void dumpTransform(Archive *fout, const TransformInfo *transform);
303 : : static void dumpOpr(Archive *fout, const OprInfo *oprinfo);
304 : : static void dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo);
305 : : static void dumpOpclass(Archive *fout, const OpclassInfo *opcinfo);
306 : : static void dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo);
307 : : static void dumpCollation(Archive *fout, const CollInfo *collinfo);
308 : : static void dumpConversion(Archive *fout, const ConvInfo *convinfo);
309 : : static void dumpRule(Archive *fout, const RuleInfo *rinfo);
310 : : static void dumpAgg(Archive *fout, const AggInfo *agginfo);
311 : : static void dumpTrigger(Archive *fout, const TriggerInfo *tginfo);
312 : : static void dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo);
313 : : static void dumpTable(Archive *fout, const TableInfo *tbinfo);
314 : : static void dumpTableSchema(Archive *fout, const TableInfo *tbinfo);
315 : : static void dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo);
316 : : static void dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo);
317 : : static void collectSequences(Archive *fout);
318 : : static void dumpSequence(Archive *fout, const TableInfo *tbinfo);
319 : : static void dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo);
320 : : static void dumpIndex(Archive *fout, const IndxInfo *indxinfo);
321 : : static void dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo);
322 : : static void dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo);
323 : : static void dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo);
324 : : static void dumpConstraint(Archive *fout, const ConstraintInfo *coninfo);
325 : : static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo);
326 : : static void dumpTSParser(Archive *fout, const TSParserInfo *prsinfo);
327 : : static void dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo);
328 : : static void dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo);
329 : : static void dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo);
330 : : static void dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo);
331 : : static void dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo);
332 : : static void dumpUserMappings(Archive *fout,
333 : : const char *servername, const char *namespace,
334 : : const char *owner, CatalogId catalogId, DumpId dumpId);
335 : : static void dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo);
336 : :
337 : : static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
338 : : const char *type, const char *name, const char *subname,
339 : : const char *nspname, const char *tag, const char *owner,
340 : : const DumpableAcl *dacl);
341 : :
342 : : static void getDependencies(Archive *fout);
343 : : static void BuildArchiveDependencies(Archive *fout);
344 : : static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
345 : : DumpId **dependencies, int *nDeps, int *allocDeps);
346 : :
347 : : static DumpableObject *createBoundaryObjects(void);
348 : : static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
349 : : DumpableObject *boundaryObjs);
350 : :
351 : : static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx);
352 : : static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
353 : : static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
354 : : static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
355 : : static void buildMatViewRefreshDependencies(Archive *fout);
356 : : static void getTableDataFKConstraints(void);
357 : : static void determineNotNullFlags(Archive *fout, PGresult *res, int r,
358 : : TableInfo *tbinfo, int j,
359 : : int i_notnull_name,
360 : : int i_notnull_comment,
361 : : int i_notnull_invalidoid,
362 : : int i_notnull_noinherit,
363 : : int i_notnull_islocal,
364 : : PQExpBuffer *invalidnotnulloids);
365 : : static char *format_function_arguments(const FuncInfo *finfo, const char *funcargs,
366 : : bool is_agg);
367 : : static char *format_function_signature(Archive *fout,
368 : : const FuncInfo *finfo, bool honor_quotes);
369 : : static char *convertRegProcReference(const char *proc);
370 : : static char *getFormattedOperatorName(const char *oproid);
371 : : static char *convertTSFunction(Archive *fout, Oid funcOid);
372 : : static const char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
373 : : static void getLOs(Archive *fout);
374 : : static void dumpLO(Archive *fout, const LoInfo *loinfo);
375 : : static int dumpLOs(Archive *fout, const void *arg);
376 : : static void dumpPolicy(Archive *fout, const PolicyInfo *polinfo);
377 : : static void dumpPublication(Archive *fout, const PublicationInfo *pubinfo);
378 : : static void dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo);
379 : : static void dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo);
380 : : static void dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo);
381 : : static void dumpDatabase(Archive *fout);
382 : : static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
383 : : const char *dbname, Oid dboid);
384 : : static void dumpEncoding(Archive *AH);
385 : : static void dumpStdStrings(Archive *AH);
386 : : static void dumpSearchPath(Archive *AH);
387 : : static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
388 : : PQExpBuffer upgrade_buffer,
389 : : Oid pg_type_oid,
390 : : bool force_array_type,
391 : : bool include_multirange_type);
392 : : static void binary_upgrade_set_type_oids_by_rel(Archive *fout,
393 : : PQExpBuffer upgrade_buffer,
394 : : const TableInfo *tbinfo);
395 : : static void collectBinaryUpgradeClassOids(Archive *fout);
396 : : static void binary_upgrade_set_pg_class_oids(Archive *fout,
397 : : PQExpBuffer upgrade_buffer,
398 : : Oid pg_class_oid);
399 : : static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
400 : : const DumpableObject *dobj,
401 : : const char *objtype,
402 : : const char *objname,
403 : : const char *objnamespace);
404 : : static const char *getAttrName(int attrnum, const TableInfo *tblInfo);
405 : : static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
406 : : static bool nonemptyReloptions(const char *reloptions);
407 : : static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
408 : : const char *prefix, Archive *fout);
409 : : static char *get_synchronized_snapshot(Archive *fout);
410 : : static void set_restrict_relation_kind(Archive *AH, const char *value);
411 : : static void setupDumpWorker(Archive *AH);
412 : : static TableInfo *getRootTableInfo(const TableInfo *tbinfo);
413 : : static bool forcePartitionRootLoad(const TableInfo *tbinfo);
414 : : static void read_dump_filters(const char *filename, DumpOptions *dopt);
415 : :
416 : :
417 : : int
418 : 306 : main(int argc, char **argv)
419 : : {
420 : : int c;
421 : 306 : const char *filename = NULL;
422 : 306 : const char *format = "p";
423 : : TableInfo *tblinfo;
424 : : int numTables;
425 : : DumpableObject **dobjs;
426 : : int numObjs;
427 : : DumpableObject *boundaryObjs;
428 : : int i;
429 : : int optindex;
430 : : RestoreOptions *ropt;
431 : : Archive *fout; /* the script file */
432 : 306 : bool g_verbose = false;
433 : 306 : const char *dumpencoding = NULL;
434 : 306 : const char *dumpsnapshot = NULL;
435 : 306 : char *use_role = NULL;
436 : 306 : int numWorkers = 1;
437 : 306 : int plainText = 0;
438 : 306 : ArchiveFormat archiveFormat = archUnknown;
439 : : ArchiveMode archiveMode;
440 : 306 : pg_compress_specification compression_spec = {0};
441 : 306 : char *compression_detail = NULL;
442 : 306 : char *compression_algorithm_str = "none";
443 : 306 : char *error_detail = NULL;
444 : 306 : bool user_compression_defined = false;
445 : 306 : DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
446 : 306 : bool data_only = false;
447 : 306 : bool schema_only = false;
448 : 306 : bool statistics_only = false;
449 : 306 : bool with_statistics = false;
450 : 306 : bool no_data = false;
451 : 306 : bool no_schema = false;
452 : 306 : bool no_statistics = false;
453 : :
454 : : static DumpOptions dopt;
455 : :
456 : : static struct option long_options[] = {
457 : : {"data-only", no_argument, NULL, 'a'},
458 : : {"blobs", no_argument, NULL, 'b'},
459 : : {"large-objects", no_argument, NULL, 'b'},
460 : : {"no-blobs", no_argument, NULL, 'B'},
461 : : {"no-large-objects", no_argument, NULL, 'B'},
462 : : {"clean", no_argument, NULL, 'c'},
463 : : {"create", no_argument, NULL, 'C'},
464 : : {"dbname", required_argument, NULL, 'd'},
465 : : {"extension", required_argument, NULL, 'e'},
466 : : {"file", required_argument, NULL, 'f'},
467 : : {"format", required_argument, NULL, 'F'},
468 : : {"host", required_argument, NULL, 'h'},
469 : : {"jobs", 1, NULL, 'j'},
470 : : {"no-reconnect", no_argument, NULL, 'R'},
471 : : {"no-owner", no_argument, NULL, 'O'},
472 : : {"port", required_argument, NULL, 'p'},
473 : : {"schema", required_argument, NULL, 'n'},
474 : : {"exclude-schema", required_argument, NULL, 'N'},
475 : : {"schema-only", no_argument, NULL, 's'},
476 : : {"superuser", required_argument, NULL, 'S'},
477 : : {"table", required_argument, NULL, 't'},
478 : : {"exclude-table", required_argument, NULL, 'T'},
479 : : {"no-password", no_argument, NULL, 'w'},
480 : : {"password", no_argument, NULL, 'W'},
481 : : {"username", required_argument, NULL, 'U'},
482 : : {"verbose", no_argument, NULL, 'v'},
483 : : {"no-privileges", no_argument, NULL, 'x'},
484 : : {"no-acl", no_argument, NULL, 'x'},
485 : : {"compress", required_argument, NULL, 'Z'},
486 : : {"encoding", required_argument, NULL, 'E'},
487 : : {"help", no_argument, NULL, '?'},
488 : : {"version", no_argument, NULL, 'V'},
489 : :
490 : : /*
491 : : * the following options don't have an equivalent short option letter
492 : : */
493 : : {"attribute-inserts", no_argument, &dopt.column_inserts, 1},
494 : : {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1},
495 : : {"column-inserts", no_argument, &dopt.column_inserts, 1},
496 : : {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1},
497 : : {"disable-triggers", no_argument, &dopt.disable_triggers, 1},
498 : : {"enable-row-security", no_argument, &dopt.enable_row_security, 1},
499 : : {"exclude-table-data", required_argument, NULL, 4},
500 : : {"extra-float-digits", required_argument, NULL, 8},
501 : : {"if-exists", no_argument, &dopt.if_exists, 1},
502 : : {"inserts", no_argument, NULL, 9},
503 : : {"lock-wait-timeout", required_argument, NULL, 2},
504 : : {"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1},
505 : : {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
506 : : {"quote-all-identifiers", no_argument, "e_all_identifiers, 1},
507 : : {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
508 : : {"role", required_argument, NULL, 3},
509 : : {"section", required_argument, NULL, 5},
510 : : {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
511 : : {"snapshot", required_argument, NULL, 6},
512 : : {"statistics", no_argument, NULL, 22},
513 : : {"statistics-only", no_argument, NULL, 18},
514 : : {"strict-names", no_argument, &strict_names, 1},
515 : : {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1},
516 : : {"no-comments", no_argument, &dopt.no_comments, 1},
517 : : {"no-data", no_argument, NULL, 19},
518 : : {"no-policies", no_argument, &dopt.no_policies, 1},
519 : : {"no-publications", no_argument, &dopt.no_publications, 1},
520 : : {"no-schema", no_argument, NULL, 20},
521 : : {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
522 : : {"no-statistics", no_argument, NULL, 21},
523 : : {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
524 : : {"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
525 : : {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
526 : : {"no-sync", no_argument, NULL, 7},
527 : : {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
528 : : {"rows-per-insert", required_argument, NULL, 10},
529 : : {"include-foreign-data", required_argument, NULL, 11},
530 : : {"table-and-children", required_argument, NULL, 12},
531 : : {"exclude-table-and-children", required_argument, NULL, 13},
532 : : {"exclude-table-data-and-children", required_argument, NULL, 14},
533 : : {"sync-method", required_argument, NULL, 15},
534 : : {"filter", required_argument, NULL, 16},
535 : : {"exclude-extension", required_argument, NULL, 17},
536 : : {"sequence-data", no_argument, &dopt.sequence_data, 1},
537 : : {"restrict-key", required_argument, NULL, 25},
538 : :
539 : : {NULL, 0, NULL, 0}
540 : : };
541 : :
542 : 306 : pg_logging_init(argv[0]);
543 : 306 : pg_logging_set_level(PG_LOG_WARNING);
544 : 306 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
545 : :
546 : : /*
547 : : * Initialize what we need for parallel execution, especially for thread
548 : : * support on Windows.
549 : : */
550 : 306 : init_parallel_dump_utils();
551 : :
552 : 306 : progname = get_progname(argv[0]);
553 : :
554 [ + - ]: 306 : if (argc > 1)
555 : : {
556 [ + + - + ]: 306 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
557 : : {
558 : 1 : help(progname);
559 : 1 : exit_nicely(0);
560 : : }
561 [ + + + + ]: 305 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
562 : : {
563 : 67 : puts("pg_dump (PostgreSQL) " PG_VERSION);
564 : 67 : exit_nicely(0);
565 : : }
566 : : }
567 : :
568 : 238 : InitDumpOptions(&dopt);
569 : :
570 : 1376 : while ((c = getopt_long(argc, argv, "abBcCd:e:E:f:F:h:j:n:N:Op:RsS:t:T:U:vwWxXZ:",
571 [ + + ]: 1376 : long_options, &optindex)) != -1)
572 : : {
573 [ + + + + : 1146 : switch (c)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + - +
+ + + + +
+ + - + +
+ + + + +
+ + ]
574 : : {
575 : 9 : case 'a': /* Dump data only */
576 : 9 : data_only = true;
577 : 9 : break;
578 : :
579 : 1 : case 'b': /* Dump LOs */
580 : 1 : dopt.outputLOs = true;
581 : 1 : break;
582 : :
583 : 2 : case 'B': /* Don't dump LOs */
584 : 2 : dopt.dontOutputLOs = true;
585 : 2 : break;
586 : :
587 : 6 : case 'c': /* clean (i.e., drop) schema prior to create */
588 : 6 : dopt.outputClean = 1;
589 : 6 : break;
590 : :
591 : 29 : case 'C': /* Create DB */
592 : 29 : dopt.outputCreateDB = 1;
593 : 29 : break;
594 : :
595 : 5 : case 'd': /* database name */
596 : 5 : dopt.cparams.dbname = pg_strdup(optarg);
597 : 5 : break;
598 : :
599 : 4 : case 'e': /* include extension(s) */
600 : 4 : simple_string_list_append(&extension_include_patterns, optarg);
601 : 4 : dopt.include_everything = false;
602 : 4 : break;
603 : :
604 : 2 : case 'E': /* Dump encoding */
605 : 2 : dumpencoding = pg_strdup(optarg);
606 : 2 : break;
607 : :
608 : 199 : case 'f':
609 : 199 : filename = pg_strdup(optarg);
610 : 199 : break;
611 : :
612 : 118 : case 'F':
613 : 118 : format = pg_strdup(optarg);
614 : 118 : break;
615 : :
616 : 40 : case 'h': /* server host */
617 : 40 : dopt.cparams.pghost = pg_strdup(optarg);
618 : 40 : break;
619 : :
620 : 11 : case 'j': /* number of dump jobs */
621 [ + + ]: 11 : if (!option_parse_int(optarg, "-j/--jobs", 1,
622 : : PG_MAX_JOBS,
623 : : &numWorkers))
624 : 1 : exit_nicely(1);
625 : 10 : break;
626 : :
627 : 18 : case 'n': /* include schema(s) */
628 : 18 : simple_string_list_append(&schema_include_patterns, optarg);
629 : 18 : dopt.include_everything = false;
630 : 18 : break;
631 : :
632 : 1 : case 'N': /* exclude schema(s) */
633 : 1 : simple_string_list_append(&schema_exclude_patterns, optarg);
634 : 1 : break;
635 : :
636 : 2 : case 'O': /* Don't reconnect to match owner */
637 : 2 : dopt.outputNoOwner = 1;
638 : 2 : break;
639 : :
640 : 79 : case 'p': /* server port */
641 : 79 : dopt.cparams.pgport = pg_strdup(optarg);
642 : 79 : break;
643 : :
644 : 2 : case 'R':
645 : : /* no-op, still accepted for backwards compatibility */
646 : 2 : break;
647 : :
648 : 7 : case 's': /* dump schema only */
649 : 7 : schema_only = true;
650 : 7 : break;
651 : :
652 : 1 : case 'S': /* Username for superuser in plain text output */
653 : 1 : dopt.outputSuperuser = pg_strdup(optarg);
654 : 1 : break;
655 : :
656 : 8 : case 't': /* include table(s) */
657 : 8 : simple_string_list_append(&table_include_patterns, optarg);
658 : 8 : dopt.include_everything = false;
659 : 8 : break;
660 : :
661 : 4 : case 'T': /* exclude table(s) */
662 : 4 : simple_string_list_append(&table_exclude_patterns, optarg);
663 : 4 : break;
664 : :
665 : 42 : case 'U':
666 : 42 : dopt.cparams.username = pg_strdup(optarg);
667 : 42 : break;
668 : :
669 : 6 : case 'v': /* verbose */
670 : 6 : g_verbose = true;
671 : 6 : pg_logging_increase_verbosity();
672 : 6 : break;
673 : :
674 : 1 : case 'w':
675 : 1 : dopt.cparams.promptPassword = TRI_NO;
676 : 1 : break;
677 : :
678 : 0 : case 'W':
679 : 0 : dopt.cparams.promptPassword = TRI_YES;
680 : 0 : break;
681 : :
682 : 2 : case 'x': /* skip ACL dump */
683 : 2 : dopt.aclsSkip = true;
684 : 2 : break;
685 : :
686 : 13 : case 'Z': /* Compression */
687 : 13 : parse_compress_options(optarg, &compression_algorithm_str,
688 : : &compression_detail);
689 : 13 : user_compression_defined = true;
690 : 13 : break;
691 : :
692 : 147 : case 0:
693 : : /* This covers the long options. */
694 : 147 : break;
695 : :
696 : 2 : case 2: /* lock-wait-timeout */
697 : 2 : dopt.lockWaitTimeout = pg_strdup(optarg);
698 : 2 : break;
699 : :
700 : 3 : case 3: /* SET ROLE */
701 : 3 : use_role = pg_strdup(optarg);
702 : 3 : break;
703 : :
704 : 1 : case 4: /* exclude table(s) data */
705 : 1 : simple_string_list_append(&tabledata_exclude_patterns, optarg);
706 : 1 : break;
707 : :
708 : 6 : case 5: /* section */
709 : 6 : set_dump_section(optarg, &dopt.dumpSections);
710 : 6 : break;
711 : :
712 : 0 : case 6: /* snapshot */
713 : 0 : dumpsnapshot = pg_strdup(optarg);
714 : 0 : break;
715 : :
716 : 156 : case 7: /* no-sync */
717 : 156 : dosync = false;
718 : 156 : break;
719 : :
720 : 1 : case 8:
721 : 1 : have_extra_float_digits = true;
722 [ + - ]: 1 : if (!option_parse_int(optarg, "--extra-float-digits", -15, 3,
723 : : &extra_float_digits))
724 : 1 : exit_nicely(1);
725 : 0 : break;
726 : :
727 : 2 : case 9: /* inserts */
728 : :
729 : : /*
730 : : * dump_inserts also stores --rows-per-insert, careful not to
731 : : * overwrite that.
732 : : */
733 [ + - ]: 2 : if (dopt.dump_inserts == 0)
734 : 2 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
735 : 2 : break;
736 : :
737 : 2 : case 10: /* rows per insert */
738 [ + + ]: 2 : if (!option_parse_int(optarg, "--rows-per-insert", 1, INT_MAX,
739 : : &dopt.dump_inserts))
740 : 1 : exit_nicely(1);
741 : 1 : break;
742 : :
743 : 4 : case 11: /* include foreign data */
744 : 4 : simple_string_list_append(&foreign_servers_include_patterns,
745 : : optarg);
746 : 4 : break;
747 : :
748 : 1 : case 12: /* include table(s) and their children */
749 : 1 : simple_string_list_append(&table_include_patterns_and_children,
750 : : optarg);
751 : 1 : dopt.include_everything = false;
752 : 1 : break;
753 : :
754 : 1 : case 13: /* exclude table(s) and their children */
755 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
756 : : optarg);
757 : 1 : break;
758 : :
759 : 1 : case 14: /* exclude data of table(s) and children */
760 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
761 : : optarg);
762 : 1 : break;
763 : :
764 : 0 : case 15:
765 [ # # ]: 0 : if (!parse_sync_method(optarg, &sync_method))
766 : 0 : exit_nicely(1);
767 : 0 : break;
768 : :
769 : 26 : case 16: /* read object filters from file */
770 : 26 : read_dump_filters(optarg, &dopt);
771 : 22 : break;
772 : :
773 : 1 : case 17: /* exclude extension(s) */
774 : 1 : simple_string_list_append(&extension_exclude_patterns,
775 : : optarg);
776 : 1 : break;
777 : :
778 : 5 : case 18:
779 : 5 : statistics_only = true;
780 : 5 : break;
781 : :
782 : 42 : case 19:
783 : 42 : no_data = true;
784 : 42 : break;
785 : :
786 : 2 : case 20:
787 : 2 : no_schema = true;
788 : 2 : break;
789 : :
790 : 8 : case 21:
791 : 8 : no_statistics = true;
792 : 8 : break;
793 : :
794 : 96 : case 22:
795 : 96 : with_statistics = true;
796 : 96 : break;
797 : :
798 : 26 : case 25:
799 : 26 : dopt.restrict_key = pg_strdup(optarg);
800 : 26 : break;
801 : :
802 : 1 : default:
803 : : /* getopt_long already emitted a complaint */
804 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
805 : 1 : exit_nicely(1);
806 : : }
807 : : }
808 : :
809 : : /*
810 : : * Non-option argument specifies database name as long as it wasn't
811 : : * already specified with -d / --dbname
812 : : */
813 [ + + + - ]: 230 : if (optind < argc && dopt.cparams.dbname == NULL)
814 : 194 : dopt.cparams.dbname = argv[optind++];
815 : :
816 : : /* Complain if any arguments remain */
817 [ + + ]: 230 : if (optind < argc)
818 : : {
819 : 1 : pg_log_error("too many command-line arguments (first is \"%s\")",
820 : : argv[optind]);
821 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
822 : 1 : exit_nicely(1);
823 : : }
824 : :
825 : : /* --column-inserts implies --inserts */
826 [ + + + - ]: 229 : if (dopt.column_inserts && dopt.dump_inserts == 0)
827 : 1 : dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
828 : :
829 : : /* *-only options are incompatible with each other */
830 : 229 : check_mut_excl_opts(data_only, "-a/--data-only",
831 : : schema_only, "-s/--schema-only",
832 : : statistics_only, "--statistics-only");
833 : :
834 : : /* --no-* and *-only for same thing are incompatible */
835 : 226 : check_mut_excl_opts(data_only, "-a/--data-only",
836 : : no_data, "--no-data");
837 : 226 : check_mut_excl_opts(schema_only, "-s/--schema-only",
838 : : no_schema, "--no-schema");
839 : 226 : check_mut_excl_opts(statistics_only, "--statistics-only",
840 : : no_statistics, "--no-statistics");
841 : :
842 : : /* --statistics and --no-statistics are incompatible */
843 : 225 : check_mut_excl_opts(with_statistics, "--statistics",
844 : : no_statistics, "--no-statistics");
845 : :
846 : : /* --statistics is incompatible with *-only (except --statistics-only) */
847 : 225 : check_mut_excl_opts(with_statistics, "--statistics",
848 : : data_only, "-a/--data-only",
849 : : schema_only, "-s/--schema-only");
850 : :
851 : : /* --include-foreign-data is incompatible with --schema-only */
852 : 224 : check_mut_excl_opts(foreign_servers_include_patterns.head, "--include-foreign-data",
853 : : schema_only, "-s/--schema-only");
854 : :
855 [ + + + + ]: 223 : if (numWorkers > 1 && foreign_servers_include_patterns.head != NULL)
856 : 1 : pg_fatal("option %s is not supported with parallel backup",
857 : : "--include-foreign-data");
858 : :
859 : : /* --clean is incompatible with --data-only */
860 : 222 : check_mut_excl_opts(dopt.outputClean, "-c/--clean",
861 : : data_only, "-a/--data-only");
862 : :
863 [ + + + + ]: 221 : if (dopt.if_exists && !dopt.outputClean)
864 : 1 : pg_fatal("option %s requires option %s",
865 : : "--if-exists", "-c/--clean");
866 : :
867 : : /*
868 : : * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
869 : : * "--schema-only --no-schema", will have already caused an error in one
870 : : * of the checks above.
871 : : */
872 [ + + + + : 220 : dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) ||
- + ]
873 [ + - + + ]: 440 : data_only) && !no_data;
874 [ + + + + : 220 : dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) ||
- + ]
875 [ + - + + ]: 440 : schema_only) && !no_schema;
876 [ - - - - : 220 : dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) ||
+ + ]
877 [ - + + + : 440 : (statistics_only || with_statistics)) && !no_statistics;
+ - ]
878 : :
879 : :
880 : : /*
881 : : * --inserts are already implied above if --column-inserts or
882 : : * --rows-per-insert were specified.
883 : : */
884 [ + + + - ]: 220 : if (dopt.do_nothing && dopt.dump_inserts == 0)
885 : 1 : pg_fatal("option %s requires option %s, %s, or %s",
886 : : "--on-conflict-do-nothing",
887 : : "--inserts", "--rows-per-insert", "--column-inserts");
888 : :
889 : : /* Identify archive format to emit */
890 : 219 : archiveFormat = parseArchiveFormat(format, &archiveMode);
891 : :
892 : : /* archiveFormat specific setup */
893 [ + + ]: 218 : if (archiveFormat == archNull)
894 : : {
895 : 152 : plainText = 1;
896 : :
897 : : /*
898 : : * If you don't provide a restrict key, one will be appointed for you.
899 : : */
900 [ + + ]: 152 : if (!dopt.restrict_key)
901 : 126 : dopt.restrict_key = generate_restrict_key();
902 [ - + ]: 152 : if (!dopt.restrict_key)
903 : 0 : pg_fatal("could not generate restrict key");
904 [ - + ]: 152 : if (!valid_restrict_key(dopt.restrict_key))
905 : 0 : pg_fatal("invalid restrict key");
906 : : }
907 [ - + ]: 66 : else if (dopt.restrict_key)
908 : 0 : pg_fatal("option %s can only be used with %s",
909 : : "--restrict-key", "--format=plain");
910 : :
911 : : /*
912 : : * Custom and directory formats are compressed by default with gzip when
913 : : * available, not the others. If gzip is not available, no compression is
914 : : * done by default.
915 : : */
916 [ + + + + ]: 218 : if ((archiveFormat == archCustom || archiveFormat == archDirectory) &&
917 [ + + ]: 63 : !user_compression_defined)
918 : : {
919 : : #ifdef HAVE_LIBZ
920 : 57 : compression_algorithm_str = "gzip";
921 : : #else
922 : : compression_algorithm_str = "none";
923 : : #endif
924 : : }
925 : :
926 : : /*
927 : : * Compression options
928 : : */
929 [ + + ]: 218 : if (!parse_compress_algorithm(compression_algorithm_str,
930 : : &compression_algorithm))
931 : 1 : pg_fatal("unrecognized compression algorithm: \"%s\"",
932 : : compression_algorithm_str);
933 : :
934 : 217 : parse_compress_specification(compression_algorithm, compression_detail,
935 : : &compression_spec);
936 : 217 : error_detail = validate_compress_specification(&compression_spec);
937 [ + + ]: 217 : if (error_detail != NULL)
938 : 3 : pg_fatal("invalid compression specification: %s",
939 : : error_detail);
940 : :
941 : 214 : error_detail = supports_compression(compression_spec);
942 [ - + ]: 214 : if (error_detail != NULL)
943 : 0 : pg_fatal("%s", error_detail);
944 : :
945 : : /*
946 : : * Disable support for zstd workers for now - these are based on
947 : : * threading, and it's unclear how it interacts with parallel dumps on
948 : : * platforms where that relies on threads too (e.g. Windows).
949 : : */
950 [ - + ]: 214 : if (compression_spec.options & PG_COMPRESSION_OPTION_WORKERS)
951 : 0 : pg_log_warning("compression option \"%s\" is not currently supported by pg_dump",
952 : : "workers");
953 : :
954 : : /*
955 : : * If emitting an archive format, we always want to emit a DATABASE item,
956 : : * in case --create is specified at pg_restore time.
957 : : */
958 [ + + ]: 214 : if (!plainText)
959 : 66 : dopt.outputCreateDB = 1;
960 : :
961 : : /* Parallel backup only in the directory archive format so far */
962 [ + + + + ]: 214 : if (archiveFormat != archDirectory && numWorkers > 1)
963 : 1 : pg_fatal("parallel backup only supported by the directory format");
964 : :
965 : : /* Open the output file */
966 : 213 : fout = CreateArchive(filename, archiveFormat, compression_spec,
967 : : dosync, archiveMode, setupDumpWorker, sync_method);
968 : :
969 : : /* Make dump options accessible right away */
970 : 212 : SetArchiveOptions(fout, &dopt, NULL);
971 : :
972 : : /* Register the cleanup hook */
973 : 212 : on_exit_close_archive(fout);
974 : :
975 : : /* Let the archiver know how noisy to be */
976 : 212 : fout->verbose = g_verbose;
977 : :
978 : :
979 : : /*
980 : : * We allow the server to be back to 10, and up to any minor release of
981 : : * our own major version. (See also version check in pg_dumpall.c.)
982 : : */
983 : 212 : fout->minRemoteVersion = 100000;
984 : 212 : fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
985 : :
986 : 212 : fout->numWorkers = numWorkers;
987 : :
988 : : /*
989 : : * Open the database using the Archiver, so it knows about it. Errors mean
990 : : * death.
991 : : */
992 : 212 : ConnectDatabaseAhx(fout, &dopt.cparams, false);
993 : 210 : setup_connection(fout, dumpencoding, dumpsnapshot, use_role);
994 : :
995 : : /*
996 : : * On hot standbys, never try to dump unlogged table data, since it will
997 : : * just throw an error.
998 : : */
999 [ + + ]: 210 : if (fout->isStandby)
1000 : 4 : dopt.no_unlogged_table_data = true;
1001 : :
1002 : : /*
1003 : : * Find the last built-in OID, if needed (prior to 8.1)
1004 : : *
1005 : : * With 8.1 and above, we can just use FirstNormalObjectId - 1.
1006 : : */
1007 : 210 : g_last_builtin_oid = FirstNormalObjectId - 1;
1008 : :
1009 : 210 : pg_log_info("last built-in OID is %u", g_last_builtin_oid);
1010 : :
1011 : : /* Expand schema selection patterns into OID lists */
1012 [ + + ]: 210 : if (schema_include_patterns.head != NULL)
1013 : : {
1014 : 19 : expand_schema_name_patterns(fout, &schema_include_patterns,
1015 : : &schema_include_oids,
1016 : : strict_names);
1017 [ + + ]: 13 : if (schema_include_oids.head == NULL)
1018 : 1 : pg_fatal("no matching schemas were found");
1019 : : }
1020 : 203 : expand_schema_name_patterns(fout, &schema_exclude_patterns,
1021 : : &schema_exclude_oids,
1022 : : false);
1023 : : /* non-matching exclusion patterns aren't an error */
1024 : :
1025 : : /* Expand table selection patterns into OID lists */
1026 : 203 : expand_table_name_patterns(fout, &table_include_patterns,
1027 : : &table_include_oids,
1028 : : strict_names, false);
1029 : 198 : expand_table_name_patterns(fout, &table_include_patterns_and_children,
1030 : : &table_include_oids,
1031 : : strict_names, true);
1032 [ + + ]: 198 : if ((table_include_patterns.head != NULL ||
1033 [ + + ]: 187 : table_include_patterns_and_children.head != NULL) &&
1034 [ + + ]: 13 : table_include_oids.head == NULL)
1035 : 2 : pg_fatal("no matching tables were found");
1036 : :
1037 : 196 : expand_table_name_patterns(fout, &table_exclude_patterns,
1038 : : &table_exclude_oids,
1039 : : false, false);
1040 : 196 : expand_table_name_patterns(fout, &table_exclude_patterns_and_children,
1041 : : &table_exclude_oids,
1042 : : false, true);
1043 : :
1044 : 196 : expand_table_name_patterns(fout, &tabledata_exclude_patterns,
1045 : : &tabledata_exclude_oids,
1046 : : false, false);
1047 : 196 : expand_table_name_patterns(fout, &tabledata_exclude_patterns_and_children,
1048 : : &tabledata_exclude_oids,
1049 : : false, true);
1050 : :
1051 : 196 : expand_foreign_server_name_patterns(fout, &foreign_servers_include_patterns,
1052 : : &foreign_servers_include_oids);
1053 : :
1054 : : /* non-matching exclusion patterns aren't an error */
1055 : :
1056 : : /* Expand extension selection patterns into OID lists */
1057 [ + + ]: 195 : if (extension_include_patterns.head != NULL)
1058 : : {
1059 : 5 : expand_extension_name_patterns(fout, &extension_include_patterns,
1060 : : &extension_include_oids,
1061 : : strict_names);
1062 [ + + ]: 5 : if (extension_include_oids.head == NULL)
1063 : 1 : pg_fatal("no matching extensions were found");
1064 : : }
1065 : 194 : expand_extension_name_patterns(fout, &extension_exclude_patterns,
1066 : : &extension_exclude_oids,
1067 : : false);
1068 : : /* non-matching exclusion patterns aren't an error */
1069 : :
1070 : : /*
1071 : : * Dumping LOs is the default for dumps where an inclusion switch is not
1072 : : * used (an "include everything" dump). -B can be used to exclude LOs
1073 : : * from those dumps. -b can be used to include LOs even when an inclusion
1074 : : * switch is used.
1075 : : *
1076 : : * -s means "schema only" and LOs are data, not schema, so we never
1077 : : * include LOs when -s is used.
1078 : : */
1079 [ + + + + : 194 : if (dopt.include_everything && dopt.dumpData && !dopt.dontOutputLOs)
+ + ]
1080 : 122 : dopt.outputLOs = true;
1081 : :
1082 : : /*
1083 : : * Collect role names so we can map object owner OIDs to names.
1084 : : */
1085 : 194 : collectRoleNames(fout);
1086 : :
1087 : : /*
1088 : : * Now scan the database and create DumpableObject structs for all the
1089 : : * objects we intend to dump.
1090 : : */
1091 : 194 : tblinfo = getSchemaData(fout, &numTables);
1092 : :
1093 [ + + ]: 193 : if (dopt.dumpData)
1094 : : {
1095 : 146 : getTableData(&dopt, tblinfo, numTables, 0);
1096 : 146 : buildMatViewRefreshDependencies(fout);
1097 [ + + ]: 146 : if (!dopt.dumpSchema)
1098 : 7 : getTableDataFKConstraints();
1099 : : }
1100 : :
1101 [ + + + + ]: 193 : if (!dopt.dumpData && dopt.sequence_data)
1102 : 38 : getTableData(&dopt, tblinfo, numTables, RELKIND_SEQUENCE);
1103 : :
1104 : : /*
1105 : : * For binary upgrade mode, dump the pg_shdepend rows for large objects
1106 : : * and maybe even pg_largeobject_metadata (see comment below for details).
1107 : : * This is faster to restore than the equivalent set of large object
1108 : : * commands.
1109 : : */
1110 [ + + ]: 193 : if (dopt.binary_upgrade)
1111 : : {
1112 : : TableInfo *shdepend;
1113 : :
1114 : 42 : shdepend = findTableByOid(SharedDependRelationId);
1115 : 42 : makeTableDataInfo(&dopt, shdepend);
1116 : :
1117 : : /*
1118 : : * Only dump large object shdepend rows for this database.
1119 : : */
1120 : 42 : shdepend->dataObj->filtercond = "WHERE classid = 'pg_largeobject'::regclass "
1121 : : "AND dbid = (SELECT oid FROM pg_database "
1122 : : " WHERE datname = current_database())";
1123 : :
1124 : : /*
1125 : : * For binary upgrades from v16 and newer versions, we can copy
1126 : : * pg_largeobject_metadata's files from the old cluster, so we don't
1127 : : * need to dump its contents. pg_upgrade can't copy/link the files
1128 : : * from older versions because aclitem (needed by
1129 : : * pg_largeobject_metadata.lomacl) changed its storage format in v16.
1130 : : */
1131 [ - + ]: 42 : if (fout->remoteVersion < 160000)
1132 : : {
1133 : : TableInfo *lo_metadata;
1134 : :
1135 : 0 : lo_metadata = findTableByOid(LargeObjectMetadataRelationId);
1136 : 0 : makeTableDataInfo(&dopt, lo_metadata);
1137 : : }
1138 : : }
1139 : :
1140 : : /*
1141 : : * In binary-upgrade mode, we do not have to worry about the actual LO
1142 : : * data or the associated metadata that resides in the pg_largeobject and
1143 : : * pg_largeobject_metadata tables, respectively.
1144 : : *
1145 : : * However, we do need to collect LO information as there may be comments
1146 : : * or other information on LOs that we do need to dump out.
1147 : : */
1148 [ + + + + ]: 193 : if (dopt.outputLOs || dopt.binary_upgrade)
1149 : 164 : getLOs(fout);
1150 : :
1151 : : /*
1152 : : * Collect dependency data to assist in ordering the objects.
1153 : : */
1154 : 193 : getDependencies(fout);
1155 : :
1156 : : /*
1157 : : * Collect ACLs, comments, and security labels, if wanted.
1158 : : */
1159 [ + + ]: 193 : if (!dopt.aclsSkip)
1160 : 191 : getAdditionalACLs(fout);
1161 [ + - ]: 193 : if (!dopt.no_comments)
1162 : 193 : collectComments(fout);
1163 [ + - ]: 193 : if (!dopt.no_security_labels)
1164 : 193 : collectSecLabels(fout);
1165 : :
1166 : : /* For binary upgrade mode, collect required pg_class information. */
1167 [ + + ]: 193 : if (dopt.binary_upgrade)
1168 : 42 : collectBinaryUpgradeClassOids(fout);
1169 : :
1170 : : /* Collect sequence information. */
1171 : 193 : collectSequences(fout);
1172 : :
1173 : : /* Lastly, create dummy objects to represent the section boundaries */
1174 : 193 : boundaryObjs = createBoundaryObjects();
1175 : :
1176 : : /* Get pointers to all the known DumpableObjects */
1177 : 193 : getDumpableObjects(&dobjs, &numObjs);
1178 : :
1179 : : /*
1180 : : * Add dummy dependencies to enforce the dump section ordering.
1181 : : */
1182 : 193 : addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
1183 : :
1184 : : /*
1185 : : * Sort the objects into a safe dump order (no forward references).
1186 : : *
1187 : : * We rely on dependency information to help us determine a safe order, so
1188 : : * the initial sort is mostly for cosmetic purposes: we sort by name to
1189 : : * ensure that logically identical schemas will dump identically.
1190 : : */
1191 : 193 : sortDumpableObjectsByTypeName(dobjs, numObjs);
1192 : :
1193 : 193 : sortDumpableObjects(dobjs, numObjs,
1194 : 193 : boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
1195 : :
1196 : : /*
1197 : : * Create archive TOC entries for all the objects to be dumped, in a safe
1198 : : * order.
1199 : : */
1200 : :
1201 : : /*
1202 : : * First the special entries for ENCODING, STDSTRINGS, and SEARCHPATH.
1203 : : */
1204 : 193 : dumpEncoding(fout);
1205 : 193 : dumpStdStrings(fout);
1206 : 193 : dumpSearchPath(fout);
1207 : :
1208 : : /* The database items are always next, unless we don't want them at all */
1209 [ + + ]: 193 : if (dopt.outputCreateDB)
1210 : 94 : dumpDatabase(fout);
1211 : :
1212 : : /* Now the rearrangeable objects. */
1213 [ + + ]: 745710 : for (i = 0; i < numObjs; i++)
1214 : 745517 : dumpDumpableObject(fout, dobjs[i]);
1215 : :
1216 : : /*
1217 : : * Set up options info to ensure we dump what we want.
1218 : : */
1219 : 193 : ropt = NewRestoreOptions();
1220 : 193 : ropt->filename = filename;
1221 : :
1222 : : /* if you change this list, see dumpOptionsFromRestoreOptions */
1223 [ + + ]: 193 : ropt->cparams.dbname = dopt.cparams.dbname ? pg_strdup(dopt.cparams.dbname) : NULL;
1224 [ + + ]: 193 : ropt->cparams.pgport = dopt.cparams.pgport ? pg_strdup(dopt.cparams.pgport) : NULL;
1225 [ + + ]: 193 : ropt->cparams.pghost = dopt.cparams.pghost ? pg_strdup(dopt.cparams.pghost) : NULL;
1226 [ + + ]: 193 : ropt->cparams.username = dopt.cparams.username ? pg_strdup(dopt.cparams.username) : NULL;
1227 : 193 : ropt->cparams.promptPassword = dopt.cparams.promptPassword;
1228 : 193 : ropt->dropSchema = dopt.outputClean;
1229 : 193 : ropt->dumpData = dopt.dumpData;
1230 : 193 : ropt->dumpSchema = dopt.dumpSchema;
1231 : 193 : ropt->dumpStatistics = dopt.dumpStatistics;
1232 : 193 : ropt->if_exists = dopt.if_exists;
1233 : 193 : ropt->column_inserts = dopt.column_inserts;
1234 : 193 : ropt->dumpSections = dopt.dumpSections;
1235 : 193 : ropt->aclsSkip = dopt.aclsSkip;
1236 : 193 : ropt->superuser = dopt.outputSuperuser;
1237 : 193 : ropt->createDB = dopt.outputCreateDB;
1238 : 193 : ropt->noOwner = dopt.outputNoOwner;
1239 : 193 : ropt->noTableAm = dopt.outputNoTableAm;
1240 : 193 : ropt->noTablespace = dopt.outputNoTablespaces;
1241 : 193 : ropt->disable_triggers = dopt.disable_triggers;
1242 : 193 : ropt->use_setsessauth = dopt.use_setsessauth;
1243 : 193 : ropt->disable_dollar_quoting = dopt.disable_dollar_quoting;
1244 : 193 : ropt->dump_inserts = dopt.dump_inserts;
1245 : 193 : ropt->no_comments = dopt.no_comments;
1246 : 193 : ropt->no_policies = dopt.no_policies;
1247 : 193 : ropt->no_publications = dopt.no_publications;
1248 : 193 : ropt->no_security_labels = dopt.no_security_labels;
1249 : 193 : ropt->no_subscriptions = dopt.no_subscriptions;
1250 : 193 : ropt->lockWaitTimeout = dopt.lockWaitTimeout;
1251 : 193 : ropt->include_everything = dopt.include_everything;
1252 : 193 : ropt->enable_row_security = dopt.enable_row_security;
1253 : 193 : ropt->sequence_data = dopt.sequence_data;
1254 : 193 : ropt->binary_upgrade = dopt.binary_upgrade;
1255 [ + + ]: 193 : ropt->restrict_key = dopt.restrict_key ? pg_strdup(dopt.restrict_key) : NULL;
1256 : :
1257 : 193 : ropt->compression_spec = compression_spec;
1258 : :
1259 : 193 : ropt->suppressDumpWarnings = true; /* We've already shown them */
1260 : :
1261 : 193 : SetArchiveOptions(fout, &dopt, ropt);
1262 : :
1263 : : /* Mark which entries should be output */
1264 : 193 : ProcessArchiveRestoreOptions(fout);
1265 : :
1266 : : /*
1267 : : * The archive's TOC entries are now marked as to which ones will actually
1268 : : * be output, so we can set up their dependency lists properly. This isn't
1269 : : * necessary for plain-text output, though.
1270 : : */
1271 [ + + ]: 193 : if (!plainText)
1272 : 65 : BuildArchiveDependencies(fout);
1273 : :
1274 : : /*
1275 : : * And finally we can do the actual output.
1276 : : *
1277 : : * Note: for non-plain-text output formats, the output file is written
1278 : : * inside CloseArchive(). This is, um, bizarre; but not worth changing
1279 : : * right now.
1280 : : */
1281 [ + + ]: 193 : if (plainText)
1282 : 128 : RestoreArchive(fout);
1283 : :
1284 : 192 : CloseArchive(fout);
1285 : :
1286 : 192 : exit_nicely(0);
1287 : : }
1288 : :
1289 : :
1290 : : static void
1291 : 1 : help(const char *progname)
1292 : : {
1293 : 1 : printf(_("%s exports a PostgreSQL database as an SQL script or to other formats.\n\n"), progname);
1294 : 1 : printf(_("Usage:\n"));
1295 : 1 : printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
1296 : :
1297 : 1 : printf(_("\nGeneral options:\n"));
1298 : 1 : printf(_(" -f, --file=FILENAME output file or directory name\n"));
1299 : 1 : printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
1300 : : " plain text (default))\n"));
1301 : 1 : printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n"));
1302 : 1 : printf(_(" -v, --verbose verbose mode\n"));
1303 : 1 : printf(_(" -V, --version output version information, then exit\n"));
1304 : 1 : printf(_(" -Z, --compress=METHOD[:DETAIL]\n"
1305 : : " compress as specified\n"));
1306 : 1 : printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
1307 : 1 : printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
1308 : 1 : printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
1309 : 1 : printf(_(" -?, --help show this help, then exit\n"));
1310 : :
1311 : 1 : printf(_("\nOptions controlling the output content:\n"));
1312 : 1 : printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
1313 : 1 : printf(_(" -b, --large-objects include large objects in dump\n"));
1314 : 1 : printf(_(" --blobs (same as --large-objects, deprecated)\n"));
1315 : 1 : printf(_(" -B, --no-large-objects exclude large objects in dump\n"));
1316 : 1 : printf(_(" --no-blobs (same as --no-large-objects, deprecated)\n"));
1317 : 1 : printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
1318 : 1 : printf(_(" -C, --create include commands to create database in dump\n"));
1319 : 1 : printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n"));
1320 : 1 : printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
1321 : 1 : printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n"));
1322 : 1 : printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n"));
1323 : 1 : printf(_(" -O, --no-owner skip restoration of object ownership in\n"
1324 : : " plain-text format\n"));
1325 : 1 : printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
1326 : 1 : printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n"));
1327 : 1 : printf(_(" -t, --table=PATTERN dump only the specified table(s)\n"));
1328 : 1 : printf(_(" -T, --exclude-table=PATTERN do NOT dump the specified table(s)\n"));
1329 : 1 : printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
1330 : 1 : printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
1331 : 1 : printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
1332 : 1 : printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
1333 : 1 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
1334 : 1 : printf(_(" --enable-row-security enable row security (dump only content user has\n"
1335 : : " access to)\n"));
1336 : 1 : printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n"));
1337 : 1 : printf(_(" --exclude-table-and-children=PATTERN\n"
1338 : : " do NOT dump the specified table(s), including\n"
1339 : : " child and partition tables\n"));
1340 : 1 : printf(_(" --exclude-table-data=PATTERN do NOT dump data for the specified table(s)\n"));
1341 : 1 : printf(_(" --exclude-table-data-and-children=PATTERN\n"
1342 : : " do NOT dump data for the specified table(s),\n"
1343 : : " including child and partition tables\n"));
1344 : 1 : printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
1345 : 1 : printf(_(" --filter=FILENAME include or exclude objects and data from dump\n"
1346 : : " based on expressions in FILENAME\n"));
1347 : 1 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
1348 : 1 : printf(_(" --include-foreign-data=PATTERN\n"
1349 : : " include data of foreign tables on foreign\n"
1350 : : " servers matching PATTERN\n"));
1351 : 1 : printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
1352 : 1 : printf(_(" --load-via-partition-root load partitions via the root table\n"));
1353 : 1 : printf(_(" --no-comments do not dump comment commands\n"));
1354 : 1 : printf(_(" --no-data do not dump data\n"));
1355 : 1 : printf(_(" --no-policies do not dump row security policies\n"));
1356 : 1 : printf(_(" --no-publications do not dump publications\n"));
1357 : 1 : printf(_(" --no-schema do not dump schema\n"));
1358 : 1 : printf(_(" --no-security-labels do not dump security label assignments\n"));
1359 : 1 : printf(_(" --no-statistics do not dump statistics\n"));
1360 : 1 : printf(_(" --no-subscriptions do not dump subscriptions\n"));
1361 : 1 : printf(_(" --no-table-access-method do not dump table access methods\n"));
1362 : 1 : printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
1363 : 1 : printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
1364 : 1 : printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
1365 : 1 : printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
1366 : 1 : printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
1367 : 1 : printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
1368 : 1 : printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
1369 : 1 : printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
1370 : 1 : printf(_(" --sequence-data include sequence data in dump\n"));
1371 : 1 : printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
1372 : 1 : printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n"));
1373 : 1 : printf(_(" --statistics dump the statistics\n"));
1374 : 1 : printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
1375 : 1 : printf(_(" --strict-names require table and/or schema include patterns to\n"
1376 : : " match at least one entity each\n"));
1377 : 1 : printf(_(" --table-and-children=PATTERN dump only the specified table(s), including\n"
1378 : : " child and partition tables\n"));
1379 : 1 : printf(_(" --use-set-session-authorization\n"
1380 : : " use SET SESSION AUTHORIZATION commands instead of\n"
1381 : : " ALTER OWNER commands to set ownership\n"));
1382 : :
1383 : 1 : printf(_("\nConnection options:\n"));
1384 : 1 : printf(_(" -d, --dbname=DBNAME database to dump\n"));
1385 : 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
1386 : 1 : printf(_(" -p, --port=PORT database server port number\n"));
1387 : 1 : printf(_(" -U, --username=NAME connect as specified database user\n"));
1388 : 1 : printf(_(" -w, --no-password never prompt for password\n"));
1389 : 1 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
1390 : 1 : printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
1391 : :
1392 : 1 : printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
1393 : : "variable value is used.\n\n"));
1394 : 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1395 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1396 : 1 : }
1397 : :
1398 : : static void
1399 : 226 : setup_connection(Archive *AH, const char *dumpencoding,
1400 : : const char *dumpsnapshot, char *use_role)
1401 : : {
1402 : 226 : DumpOptions *dopt = AH->dopt;
1403 : 226 : PGconn *conn = GetConnection(AH);
1404 : :
1405 : 226 : PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL));
1406 : :
1407 : : /*
1408 : : * Set the client encoding if requested.
1409 : : */
1410 [ + + ]: 226 : if (dumpencoding)
1411 : : {
1412 [ - + ]: 18 : if (PQsetClientEncoding(conn, dumpencoding) < 0)
1413 : 0 : pg_fatal("invalid client encoding \"%s\" specified",
1414 : : dumpencoding);
1415 : : }
1416 : :
1417 : : /*
1418 : : * Force standard_conforming_strings on, just in case we are dumping from
1419 : : * an old server that has it disabled. Without this, literals in views,
1420 : : * expressions, etc, would be incorrect for modern servers.
1421 : : */
1422 : 226 : ExecuteSqlStatement(AH, "SET standard_conforming_strings = on");
1423 : :
1424 : : /*
1425 : : * And reflect that to AH->std_strings. You might think that we should
1426 : : * just delete that variable and the code that checks it, but that would
1427 : : * be problematic for pg_restore, which at least for now should still cope
1428 : : * with archives containing the other setting (cf. processStdStringsEntry
1429 : : * in pg_backup_archiver.c).
1430 : : */
1431 : 226 : AH->std_strings = true;
1432 : :
1433 : : /*
1434 : : * Get the active encoding, so we know how to escape strings.
1435 : : */
1436 : 226 : AH->encoding = PQclientEncoding(conn);
1437 : 226 : setFmtEncoding(AH->encoding);
1438 : :
1439 : : /*
1440 : : * Set the role if requested. In a parallel dump worker, we'll be passed
1441 : : * use_role == NULL, but AH->use_role is already set (if user specified it
1442 : : * originally) and we should use that.
1443 : : */
1444 [ + + + + ]: 226 : if (!use_role && AH->use_role)
1445 : 2 : use_role = AH->use_role;
1446 : :
1447 : : /* Set the role if requested */
1448 [ + + ]: 226 : if (use_role)
1449 : : {
1450 : 5 : PQExpBuffer query = createPQExpBuffer();
1451 : :
1452 : 5 : appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
1453 : 5 : ExecuteSqlStatement(AH, query->data);
1454 : 5 : destroyPQExpBuffer(query);
1455 : :
1456 : : /* save it for possible later use by parallel workers */
1457 [ + + ]: 5 : if (!AH->use_role)
1458 : 3 : AH->use_role = pg_strdup(use_role);
1459 : : }
1460 : :
1461 : : /* Set the datestyle to ISO to ensure the dump's portability */
1462 : 226 : ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
1463 : :
1464 : : /* Likewise, avoid using sql_standard intervalstyle */
1465 : 226 : ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
1466 : :
1467 : : /*
1468 : : * Use an explicitly specified extra_float_digits if it has been provided.
1469 : : * Otherwise, set extra_float_digits so that we can dump float data
1470 : : * exactly (given correctly implemented float I/O code, anyway).
1471 : : */
1472 [ - + ]: 226 : if (have_extra_float_digits)
1473 : : {
1474 : 0 : PQExpBuffer q = createPQExpBuffer();
1475 : :
1476 : 0 : appendPQExpBuffer(q, "SET extra_float_digits TO %d",
1477 : : extra_float_digits);
1478 : 0 : ExecuteSqlStatement(AH, q->data);
1479 : 0 : destroyPQExpBuffer(q);
1480 : : }
1481 : : else
1482 : 226 : ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
1483 : :
1484 : : /*
1485 : : * Disable synchronized scanning, to prevent unpredictable changes in row
1486 : : * ordering across a dump and reload.
1487 : : */
1488 : 226 : ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
1489 : :
1490 : : /*
1491 : : * Disable timeouts if supported.
1492 : : */
1493 : 226 : ExecuteSqlStatement(AH, "SET statement_timeout = 0");
1494 : 226 : ExecuteSqlStatement(AH, "SET lock_timeout = 0");
1495 : 226 : ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
1496 [ + - ]: 226 : if (AH->remoteVersion >= 170000)
1497 : 226 : ExecuteSqlStatement(AH, "SET transaction_timeout = 0");
1498 : :
1499 : : /*
1500 : : * Quote all identifiers, if requested.
1501 : : */
1502 [ + + ]: 226 : if (quote_all_identifiers)
1503 : 40 : ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1504 : :
1505 : : /*
1506 : : * Adjust row-security mode, if supported.
1507 : : */
1508 [ - + ]: 226 : if (dopt->enable_row_security)
1509 : 0 : ExecuteSqlStatement(AH, "SET row_security = on");
1510 : : else
1511 : 226 : ExecuteSqlStatement(AH, "SET row_security = off");
1512 : :
1513 : : /*
1514 : : * For security reasons, we restrict the expansion of non-system views and
1515 : : * access to foreign tables during the pg_dump process. This restriction
1516 : : * is adjusted when dumping foreign table data.
1517 : : */
1518 : 226 : set_restrict_relation_kind(AH, "view, foreign-table");
1519 : :
1520 : : /*
1521 : : * Initialize prepared-query state to "nothing prepared". We do this here
1522 : : * so that a parallel dump worker will have its own state.
1523 : : */
1524 : 226 : AH->is_prepared = pg_malloc0_array(bool, NUM_PREP_QUERIES);
1525 : :
1526 : : /*
1527 : : * Start transaction-snapshot mode transaction to dump consistent data.
1528 : : */
1529 : 226 : ExecuteSqlStatement(AH, "BEGIN");
1530 : :
1531 : : /*
1532 : : * To support the combination of serializable_deferrable with the jobs
1533 : : * option we use REPEATABLE READ for the worker connections that are
1534 : : * passed a snapshot. As long as the snapshot is acquired in a
1535 : : * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a
1536 : : * REPEATABLE READ transaction provides the appropriate integrity
1537 : : * guarantees. This is a kluge, but safe for back-patching.
1538 : : */
1539 [ - + - - ]: 226 : if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
1540 : 0 : ExecuteSqlStatement(AH,
1541 : : "SET TRANSACTION ISOLATION LEVEL "
1542 : : "SERIALIZABLE, READ ONLY, DEFERRABLE");
1543 : : else
1544 : 226 : ExecuteSqlStatement(AH,
1545 : : "SET TRANSACTION ISOLATION LEVEL "
1546 : : "REPEATABLE READ, READ ONLY");
1547 : :
1548 : : /*
1549 : : * If user specified a snapshot to use, select that. In a parallel dump
1550 : : * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id
1551 : : * is already set (if the server can handle it) and we should use that.
1552 : : */
1553 [ - + ]: 226 : if (dumpsnapshot)
1554 : 0 : AH->sync_snapshot_id = pg_strdup(dumpsnapshot);
1555 : :
1556 [ + + ]: 226 : if (AH->sync_snapshot_id)
1557 : : {
1558 : 16 : PQExpBuffer query = createPQExpBuffer();
1559 : :
1560 : 16 : appendPQExpBufferStr(query, "SET TRANSACTION SNAPSHOT ");
1561 : 16 : appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1562 : 16 : ExecuteSqlStatement(AH, query->data);
1563 : 16 : destroyPQExpBuffer(query);
1564 : : }
1565 [ + + ]: 210 : else if (AH->numWorkers > 1)
1566 : 8 : AH->sync_snapshot_id = get_synchronized_snapshot(AH);
1567 : 226 : }
1568 : :
1569 : : /* Set up connection for a parallel worker process */
1570 : : static void
1571 : 16 : setupDumpWorker(Archive *AH)
1572 : : {
1573 : : /*
1574 : : * We want to re-select all the same values the leader connection is
1575 : : * using. We'll have inherited directly-usable values in
1576 : : * AH->sync_snapshot_id and AH->use_role, but we need to translate the
1577 : : * inherited encoding value back to a string to pass to setup_connection.
1578 : : */
1579 : 16 : setup_connection(AH,
1580 : : pg_encoding_to_char(AH->encoding),
1581 : : NULL,
1582 : : NULL);
1583 : 16 : }
1584 : :
1585 : : static char *
1586 : 8 : get_synchronized_snapshot(Archive *fout)
1587 : : {
1588 : 8 : char *query = "SELECT pg_catalog.pg_export_snapshot()";
1589 : : char *result;
1590 : : PGresult *res;
1591 : :
1592 : 8 : res = ExecuteSqlQueryForSingleRow(fout, query);
1593 : 8 : result = pg_strdup(PQgetvalue(res, 0, 0));
1594 : 8 : PQclear(res);
1595 : :
1596 : 8 : return result;
1597 : : }
1598 : :
1599 : : static ArchiveFormat
1600 : 219 : parseArchiveFormat(const char *format, ArchiveMode *mode)
1601 : : {
1602 : : ArchiveFormat archiveFormat;
1603 : :
1604 : 219 : *mode = archModeWrite;
1605 : :
1606 [ + + - + ]: 219 : if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1607 : : {
1608 : : /* This is used by pg_dumpall, and is not documented */
1609 : 48 : archiveFormat = archNull;
1610 : 48 : *mode = archModeAppend;
1611 : : }
1612 [ - + ]: 171 : else if (pg_strcasecmp(format, "c") == 0)
1613 : 0 : archiveFormat = archCustom;
1614 [ + + ]: 171 : else if (pg_strcasecmp(format, "custom") == 0)
1615 : 53 : archiveFormat = archCustom;
1616 [ - + ]: 118 : else if (pg_strcasecmp(format, "d") == 0)
1617 : 0 : archiveFormat = archDirectory;
1618 [ + + ]: 118 : else if (pg_strcasecmp(format, "directory") == 0)
1619 : 10 : archiveFormat = archDirectory;
1620 [ + + ]: 108 : else if (pg_strcasecmp(format, "p") == 0)
1621 : 101 : archiveFormat = archNull;
1622 [ + + ]: 7 : else if (pg_strcasecmp(format, "plain") == 0)
1623 : 3 : archiveFormat = archNull;
1624 [ - + ]: 4 : else if (pg_strcasecmp(format, "t") == 0)
1625 : 0 : archiveFormat = archTar;
1626 [ + + ]: 4 : else if (pg_strcasecmp(format, "tar") == 0)
1627 : 3 : archiveFormat = archTar;
1628 : : else
1629 : 1 : pg_fatal("invalid output format \"%s\" specified", format);
1630 : 218 : return archiveFormat;
1631 : : }
1632 : :
1633 : : /*
1634 : : * Find the OIDs of all schemas matching the given list of patterns,
1635 : : * and append them to the given OID list.
1636 : : */
1637 : : static void
1638 : 222 : expand_schema_name_patterns(Archive *fout,
1639 : : SimpleStringList *patterns,
1640 : : SimpleOidList *oids,
1641 : : bool strict_names)
1642 : : {
1643 : : PQExpBuffer query;
1644 : : PGresult *res;
1645 : : SimpleStringListCell *cell;
1646 : : int i;
1647 : :
1648 [ + + ]: 222 : if (patterns->head == NULL)
1649 : 200 : return; /* nothing to do */
1650 : :
1651 : 22 : query = createPQExpBuffer();
1652 : :
1653 : : /*
1654 : : * The loop below runs multiple SELECTs might sometimes result in
1655 : : * duplicate entries in the OID list, but we don't care.
1656 : : */
1657 : :
1658 [ + + ]: 38 : for (cell = patterns->head; cell; cell = cell->next)
1659 : : {
1660 : : PQExpBufferData dbbuf;
1661 : : int dotcnt;
1662 : :
1663 : 22 : appendPQExpBufferStr(query,
1664 : : "SELECT oid FROM pg_catalog.pg_namespace n\n");
1665 : 22 : initPQExpBuffer(&dbbuf);
1666 : 22 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1667 : : false, NULL, "n.nspname", NULL, NULL, &dbbuf,
1668 : : &dotcnt);
1669 [ + + ]: 22 : if (dotcnt > 1)
1670 : 2 : pg_fatal("improper qualified name (too many dotted names): %s",
1671 : : cell->val);
1672 [ + + ]: 20 : else if (dotcnt == 1)
1673 : 3 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1674 : 17 : termPQExpBuffer(&dbbuf);
1675 : :
1676 : 17 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1677 [ + + + - ]: 17 : if (strict_names && PQntuples(res) == 0)
1678 : 1 : pg_fatal("no matching schemas were found for pattern \"%s\"", cell->val);
1679 : :
1680 [ + + ]: 31 : for (i = 0; i < PQntuples(res); i++)
1681 : : {
1682 : 15 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1683 : : }
1684 : :
1685 : 16 : PQclear(res);
1686 : 16 : resetPQExpBuffer(query);
1687 : : }
1688 : :
1689 : 16 : destroyPQExpBuffer(query);
1690 : : }
1691 : :
1692 : : /*
1693 : : * Find the OIDs of all extensions matching the given list of patterns,
1694 : : * and append them to the given OID list.
1695 : : */
1696 : : static void
1697 : 199 : expand_extension_name_patterns(Archive *fout,
1698 : : SimpleStringList *patterns,
1699 : : SimpleOidList *oids,
1700 : : bool strict_names)
1701 : : {
1702 : : PQExpBuffer query;
1703 : : PGresult *res;
1704 : : SimpleStringListCell *cell;
1705 : : int i;
1706 : :
1707 [ + + ]: 199 : if (patterns->head == NULL)
1708 : 192 : return; /* nothing to do */
1709 : :
1710 : 7 : query = createPQExpBuffer();
1711 : :
1712 : : /*
1713 : : * The loop below runs multiple SELECTs might sometimes result in
1714 : : * duplicate entries in the OID list, but we don't care.
1715 : : */
1716 [ + + ]: 14 : for (cell = patterns->head; cell; cell = cell->next)
1717 : : {
1718 : : int dotcnt;
1719 : :
1720 : 7 : appendPQExpBufferStr(query,
1721 : : "SELECT oid FROM pg_catalog.pg_extension e\n");
1722 : 7 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1723 : : false, NULL, "e.extname", NULL, NULL, NULL,
1724 : : &dotcnt);
1725 [ - + ]: 7 : if (dotcnt > 0)
1726 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1727 : : cell->val);
1728 : :
1729 : 7 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1730 [ - + - - ]: 7 : if (strict_names && PQntuples(res) == 0)
1731 : 0 : pg_fatal("no matching extensions were found for pattern \"%s\"", cell->val);
1732 : :
1733 [ + + ]: 13 : for (i = 0; i < PQntuples(res); i++)
1734 : : {
1735 : 6 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1736 : : }
1737 : :
1738 : 7 : PQclear(res);
1739 : 7 : resetPQExpBuffer(query);
1740 : : }
1741 : :
1742 : 7 : destroyPQExpBuffer(query);
1743 : : }
1744 : :
1745 : : /*
1746 : : * Find the OIDs of all foreign servers matching the given list of patterns,
1747 : : * and append them to the given OID list.
1748 : : */
1749 : : static void
1750 : 196 : expand_foreign_server_name_patterns(Archive *fout,
1751 : : SimpleStringList *patterns,
1752 : : SimpleOidList *oids)
1753 : : {
1754 : : PQExpBuffer query;
1755 : : PGresult *res;
1756 : : SimpleStringListCell *cell;
1757 : : int i;
1758 : :
1759 [ + + ]: 196 : if (patterns->head == NULL)
1760 : 193 : return; /* nothing to do */
1761 : :
1762 : 3 : query = createPQExpBuffer();
1763 : :
1764 : : /*
1765 : : * The loop below runs multiple SELECTs might sometimes result in
1766 : : * duplicate entries in the OID list, but we don't care.
1767 : : */
1768 : :
1769 [ + + ]: 5 : for (cell = patterns->head; cell; cell = cell->next)
1770 : : {
1771 : : int dotcnt;
1772 : :
1773 : 3 : appendPQExpBufferStr(query,
1774 : : "SELECT oid FROM pg_catalog.pg_foreign_server s\n");
1775 : 3 : processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1776 : : false, NULL, "s.srvname", NULL, NULL, NULL,
1777 : : &dotcnt);
1778 [ - + ]: 3 : if (dotcnt > 0)
1779 : 0 : pg_fatal("improper qualified name (too many dotted names): %s",
1780 : : cell->val);
1781 : :
1782 : 3 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1783 [ + + ]: 3 : if (PQntuples(res) == 0)
1784 : 1 : pg_fatal("no matching foreign servers were found for pattern \"%s\"", cell->val);
1785 : :
1786 [ + + ]: 4 : for (i = 0; i < PQntuples(res); i++)
1787 : 2 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1788 : :
1789 : 2 : PQclear(res);
1790 : 2 : resetPQExpBuffer(query);
1791 : : }
1792 : :
1793 : 2 : destroyPQExpBuffer(query);
1794 : : }
1795 : :
1796 : : /*
1797 : : * Find the OIDs of all tables matching the given list of patterns,
1798 : : * and append them to the given OID list. See also expand_dbname_patterns()
1799 : : * in pg_dumpall.c
1800 : : */
1801 : : static void
1802 : 1185 : expand_table_name_patterns(Archive *fout,
1803 : : SimpleStringList *patterns, SimpleOidList *oids,
1804 : : bool strict_names, bool with_child_tables)
1805 : : {
1806 : : PQExpBuffer query;
1807 : : PGresult *res;
1808 : : SimpleStringListCell *cell;
1809 : : int i;
1810 : :
1811 [ + + ]: 1185 : if (patterns->head == NULL)
1812 : 1156 : return; /* nothing to do */
1813 : :
1814 : 29 : query = createPQExpBuffer();
1815 : :
1816 : : /*
1817 : : * this might sometimes result in duplicate entries in the OID list, but
1818 : : * we don't care.
1819 : : */
1820 : :
1821 [ + + ]: 59 : for (cell = patterns->head; cell; cell = cell->next)
1822 : : {
1823 : : PQExpBufferData dbbuf;
1824 : : int dotcnt;
1825 : :
1826 : : /*
1827 : : * Query must remain ABSOLUTELY devoid of unqualified names. This
1828 : : * would be unnecessary given a pg_table_is_visible() variant taking a
1829 : : * search_path argument.
1830 : : *
1831 : : * For with_child_tables, we start with the basic query's results and
1832 : : * recursively search the inheritance tree to add child tables.
1833 : : */
1834 [ + + ]: 35 : if (with_child_tables)
1835 : : {
1836 : 6 : appendPQExpBufferStr(query, "WITH RECURSIVE partition_tree (relid) AS (\n");
1837 : : }
1838 : :
1839 : 35 : appendPQExpBuffer(query,
1840 : : "SELECT c.oid"
1841 : : "\nFROM pg_catalog.pg_class c"
1842 : : "\n LEFT JOIN pg_catalog.pg_namespace n"
1843 : : "\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace"
1844 : : "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY"
1845 : : "\n (array['%c', '%c', '%c', '%c', '%c', '%c', '%c'])\n",
1846 : : RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1847 : : RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
1848 : : RELKIND_PARTITIONED_TABLE, RELKIND_PROPGRAPH);
1849 : 35 : initPQExpBuffer(&dbbuf);
1850 : 35 : processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1851 : : false, "n.nspname", "c.relname", NULL,
1852 : : "pg_catalog.pg_table_is_visible(c.oid)", &dbbuf,
1853 : : &dotcnt);
1854 [ + + ]: 35 : if (dotcnt > 2)
1855 : 1 : pg_fatal("improper relation name (too many dotted names): %s",
1856 : : cell->val);
1857 [ + + ]: 34 : else if (dotcnt == 2)
1858 : 2 : prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
1859 : 32 : termPQExpBuffer(&dbbuf);
1860 : :
1861 [ + + ]: 32 : if (with_child_tables)
1862 : : {
1863 : 6 : appendPQExpBufferStr(query, "UNION"
1864 : : "\nSELECT i.inhrelid"
1865 : : "\nFROM partition_tree p"
1866 : : "\n JOIN pg_catalog.pg_inherits i"
1867 : : "\n ON p.relid OPERATOR(pg_catalog.=) i.inhparent"
1868 : : "\n)"
1869 : : "\nSELECT relid FROM partition_tree");
1870 : : }
1871 : :
1872 : 32 : ExecuteSqlStatement(fout, "RESET search_path");
1873 : 32 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1874 : 32 : PQclear(ExecuteSqlQueryForSingleRow(fout,
1875 : : ALWAYS_SECURE_SEARCH_PATH_SQL));
1876 [ + + + + ]: 32 : if (strict_names && PQntuples(res) == 0)
1877 : 2 : pg_fatal("no matching tables were found for pattern \"%s\"", cell->val);
1878 : :
1879 [ + + ]: 74 : for (i = 0; i < PQntuples(res); i++)
1880 : : {
1881 : 44 : simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1882 : : }
1883 : :
1884 : 30 : PQclear(res);
1885 : 30 : resetPQExpBuffer(query);
1886 : : }
1887 : :
1888 : 24 : destroyPQExpBuffer(query);
1889 : : }
1890 : :
1891 : : /*
1892 : : * Verifies that the connected database name matches the given database name,
1893 : : * and if not, dies with an error about the given pattern.
1894 : : *
1895 : : * The 'dbname' argument should be a literal name parsed from 'pattern'.
1896 : : */
1897 : : static void
1898 : 5 : prohibit_crossdb_refs(PGconn *conn, const char *dbname, const char *pattern)
1899 : : {
1900 : : const char *db;
1901 : :
1902 : 5 : db = PQdb(conn);
1903 [ - + ]: 5 : if (db == NULL)
1904 : 0 : pg_fatal("You are currently not connected to a database.");
1905 : :
1906 [ + - ]: 5 : if (strcmp(db, dbname) != 0)
1907 : 5 : pg_fatal("cross-database references are not implemented: %s",
1908 : : pattern);
1909 : 0 : }
1910 : :
1911 : : /*
1912 : : * checkExtensionMembership
1913 : : * Determine whether object is an extension member, and if so,
1914 : : * record an appropriate dependency and set the object's dump flag.
1915 : : *
1916 : : * It's important to call this for each object that could be an extension
1917 : : * member. Generally, we integrate this with determining the object's
1918 : : * to-be-dumped-ness, since extension membership overrides other rules for that.
1919 : : *
1920 : : * Returns true if object is an extension member, else false.
1921 : : */
1922 : : static bool
1923 : 629515 : checkExtensionMembership(DumpableObject *dobj, Archive *fout)
1924 : : {
1925 : 629515 : ExtensionInfo *ext = findOwningExtension(dobj->catId);
1926 : :
1927 [ + + ]: 629515 : if (ext == NULL)
1928 : 628690 : return false;
1929 : :
1930 : 825 : dobj->ext_member = true;
1931 : :
1932 : : /* Record dependency so that getDependencies needn't deal with that */
1933 : 825 : addObjectDependency(dobj, ext->dobj.dumpId);
1934 : :
1935 : : /*
1936 : : * Mark the member object to have any non-initial ACLs dumped. (Any
1937 : : * initial ACLs will be removed later, using data from pg_init_privs, so
1938 : : * that we'll dump only the delta from the extension's initial setup.)
1939 : : *
1940 : : * In binary upgrades, we still dump all components of the members
1941 : : * individually, since the idea is to exactly reproduce the database
1942 : : * contents rather than replace the extension contents with something
1943 : : * different.
1944 : : *
1945 : : * Note: it might be interesting someday to implement storage and delta
1946 : : * dumping of extension members' RLS policies and/or security labels.
1947 : : * However there is a pitfall for RLS policies: trying to dump them
1948 : : * requires getting a lock on their tables, and the calling user might not
1949 : : * have privileges for that. We need no lock to examine a table's ACLs,
1950 : : * so the current feature doesn't have a problem of that sort.
1951 : : */
1952 [ + + ]: 825 : if (fout->dopt->binary_upgrade)
1953 : 194 : dobj->dump = ext->dobj.dump;
1954 : : else
1955 : 631 : dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL);
1956 : :
1957 : 825 : return true;
1958 : : }
1959 : :
1960 : : /*
1961 : : * selectDumpableNamespace: policy-setting subroutine
1962 : : * Mark a namespace as to be dumped or not
1963 : : */
1964 : : static void
1965 : 1698 : selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout)
1966 : : {
1967 : : /*
1968 : : * DUMP_COMPONENT_DEFINITION typically implies a CREATE SCHEMA statement
1969 : : * and (for --clean) a DROP SCHEMA statement. (In the absence of
1970 : : * DUMP_COMPONENT_DEFINITION, this value is irrelevant.)
1971 : : */
1972 : 1698 : nsinfo->create = true;
1973 : :
1974 : : /*
1975 : : * If specific tables are being dumped, do not dump any complete
1976 : : * namespaces. If specific namespaces are being dumped, dump just those
1977 : : * namespaces. Otherwise, dump all non-system namespaces.
1978 : : */
1979 [ + + ]: 1698 : if (table_include_oids.head != NULL)
1980 : 61 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1981 [ + + ]: 1637 : else if (schema_include_oids.head != NULL)
1982 : 213 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump =
1983 : 213 : simple_oid_list_member(&schema_include_oids,
1984 : : nsinfo->dobj.catId.oid) ?
1985 [ + + ]: 213 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1986 [ + + ]: 1424 : else if (strcmp(nsinfo->dobj.name, "pg_catalog") == 0)
1987 : : {
1988 : : /*
1989 : : * We dump out any ACLs defined in pg_catalog, if they are interesting
1990 : : * (and not the original ACLs which were set at initdb time, see
1991 : : * pg_init_privs).
1992 : : */
1993 : 171 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
1994 : : }
1995 [ + + ]: 1253 : else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
1996 [ + + ]: 539 : strcmp(nsinfo->dobj.name, "information_schema") == 0)
1997 : : {
1998 : : /* Other system schemas don't get dumped */
1999 : 885 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2000 : : }
2001 [ + + ]: 368 : else if (strcmp(nsinfo->dobj.name, "public") == 0)
2002 : : {
2003 : : /*
2004 : : * The public schema is a strange beast that sits in a sort of
2005 : : * no-mans-land between being a system object and a user object.
2006 : : * CREATE SCHEMA would fail, so its DUMP_COMPONENT_DEFINITION is just
2007 : : * a comment and an indication of ownership. If the owner is the
2008 : : * default, omit that superfluous DUMP_COMPONENT_DEFINITION. Before
2009 : : * v15, the default owner was BOOTSTRAP_SUPERUSERID.
2010 : : */
2011 : 167 : nsinfo->create = false;
2012 : 167 : nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2013 [ + + ]: 167 : if (nsinfo->nspowner == ROLE_PG_DATABASE_OWNER)
2014 : 123 : nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION;
2015 : 167 : nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL;
2016 : :
2017 : : /*
2018 : : * Also, make like it has a comment even if it doesn't; this is so
2019 : : * that we'll emit a command to drop the comment, if appropriate.
2020 : : * (Without this, we'd not call dumpCommentExtended for it.)
2021 : : */
2022 : 167 : nsinfo->dobj.components |= DUMP_COMPONENT_COMMENT;
2023 : : }
2024 : : else
2025 : 201 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
2026 : :
2027 : : /*
2028 : : * In any case, a namespace can be excluded by an exclusion switch
2029 : : */
2030 [ + + + + ]: 2249 : if (nsinfo->dobj.dump_contains &&
2031 : 551 : simple_oid_list_member(&schema_exclude_oids,
2032 : : nsinfo->dobj.catId.oid))
2033 : 3 : nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
2034 : :
2035 : : /*
2036 : : * If the schema belongs to an extension, allow extension membership to
2037 : : * override the dump decision for the schema itself. However, this does
2038 : : * not change dump_contains, so this won't change what we do with objects
2039 : : * within the schema. (If they belong to the extension, they'll get
2040 : : * suppressed by it, otherwise not.)
2041 : : */
2042 : 1698 : (void) checkExtensionMembership(&nsinfo->dobj, fout);
2043 : 1698 : }
2044 : :
2045 : : /*
2046 : : * selectDumpableTable: policy-setting subroutine
2047 : : * Mark a table as to be dumped or not
2048 : : */
2049 : : static void
2050 : 55588 : selectDumpableTable(TableInfo *tbinfo, Archive *fout)
2051 : : {
2052 [ + + ]: 55588 : if (checkExtensionMembership(&tbinfo->dobj, fout))
2053 : 225 : return; /* extension membership overrides all else */
2054 : :
2055 : : /*
2056 : : * If specific tables are being dumped, dump just those tables; else, dump
2057 : : * according to the parent namespace's dump flag.
2058 : : */
2059 [ + + ]: 55363 : if (table_include_oids.head != NULL)
2060 : 5668 : tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
2061 : : tbinfo->dobj.catId.oid) ?
2062 [ + + ]: 2834 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2063 : : else
2064 : 52529 : tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains;
2065 : :
2066 : : /*
2067 : : * In any case, a table can be excluded by an exclusion switch
2068 : : */
2069 [ + + + + ]: 89943 : if (tbinfo->dobj.dump &&
2070 : 34580 : simple_oid_list_member(&table_exclude_oids,
2071 : : tbinfo->dobj.catId.oid))
2072 : 12 : tbinfo->dobj.dump = DUMP_COMPONENT_NONE;
2073 : : }
2074 : :
2075 : : /*
2076 : : * selectDumpableType: policy-setting subroutine
2077 : : * Mark a type as to be dumped or not
2078 : : *
2079 : : * If it's a table's rowtype or an autogenerated array type, we also apply a
2080 : : * special type code to facilitate sorting into the desired order. (We don't
2081 : : * want to consider those to be ordinary types because that would bring tables
2082 : : * up into the datatype part of the dump order.) We still set the object's
2083 : : * dump flag; that's not going to cause the dummy type to be dumped, but we
2084 : : * need it so that casts involving such types will be dumped correctly -- see
2085 : : * dumpCast. This means the flag should be set the same as for the underlying
2086 : : * object (the table or base type).
2087 : : */
2088 : : static void
2089 : 149348 : selectDumpableType(TypeInfo *tyinfo, Archive *fout)
2090 : : {
2091 : : /* skip complex types, except for standalone composite types */
2092 [ + + ]: 149348 : if (OidIsValid(tyinfo->typrelid) &&
2093 [ + + ]: 54686 : tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
2094 : : {
2095 : 54501 : TableInfo *tytable = findTableByOid(tyinfo->typrelid);
2096 : :
2097 : 54501 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2098 [ + - ]: 54501 : if (tytable != NULL)
2099 : 54501 : tyinfo->dobj.dump = tytable->dobj.dump;
2100 : : else
2101 : 0 : tyinfo->dobj.dump = DUMP_COMPONENT_NONE;
2102 : 54501 : return;
2103 : : }
2104 : :
2105 : : /* skip auto-generated array and multirange types */
2106 [ + + + + ]: 94847 : if (tyinfo->isArray || tyinfo->isMultirange)
2107 : : {
2108 : 73151 : tyinfo->dobj.objType = DO_DUMMY_TYPE;
2109 : :
2110 : : /*
2111 : : * Fall through to set the dump flag; we assume that the subsequent
2112 : : * rules will do the same thing as they would for the array's base
2113 : : * type or multirange's range type. (We cannot reliably look up the
2114 : : * base type here, since getTypes may not have processed it yet.)
2115 : : */
2116 : : }
2117 : :
2118 [ + + ]: 94847 : if (checkExtensionMembership(&tyinfo->dobj, fout))
2119 : 150 : return; /* extension membership overrides all else */
2120 : :
2121 : : /* Dump based on if the contents of the namespace are being dumped */
2122 : 94697 : tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains;
2123 : : }
2124 : :
2125 : : /*
2126 : : * selectDumpableDefaultACL: policy-setting subroutine
2127 : : * Mark a default ACL as to be dumped or not
2128 : : *
2129 : : * For per-schema default ACLs, dump if the schema is to be dumped.
2130 : : * Otherwise dump if we are dumping "everything". Note that dumpSchema
2131 : : * and aclsSkip are checked separately.
2132 : : */
2133 : : static void
2134 : 206 : selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt)
2135 : : {
2136 : : /* Default ACLs can't be extension members */
2137 : :
2138 [ + + ]: 206 : if (dinfo->dobj.namespace)
2139 : : /* default ACLs are considered part of the namespace */
2140 : 96 : dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains;
2141 : : else
2142 : 110 : dinfo->dobj.dump = dopt->include_everything ?
2143 [ + + ]: 110 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2144 : 206 : }
2145 : :
2146 : : /*
2147 : : * selectDumpableCast: policy-setting subroutine
2148 : : * Mark a cast as to be dumped or not
2149 : : *
2150 : : * Casts do not belong to any particular namespace (since they haven't got
2151 : : * names), nor do they have identifiable owners. To distinguish user-defined
2152 : : * casts from built-in ones, we must resort to checking whether the cast's
2153 : : * OID is in the range reserved for initdb.
2154 : : */
2155 : : static void
2156 : 46989 : selectDumpableCast(CastInfo *cast, Archive *fout)
2157 : : {
2158 [ - + ]: 46989 : if (checkExtensionMembership(&cast->dobj, fout))
2159 : 0 : return; /* extension membership overrides all else */
2160 : :
2161 : : /*
2162 : : * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
2163 : : * support ACLs currently.
2164 : : */
2165 [ + + ]: 46989 : if (cast->dobj.catId.oid <= g_last_builtin_oid)
2166 : 46899 : cast->dobj.dump = DUMP_COMPONENT_NONE;
2167 : : else
2168 : 90 : cast->dobj.dump = fout->dopt->include_everything ?
2169 [ + + ]: 90 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2170 : : }
2171 : :
2172 : : /*
2173 : : * selectDumpableProcLang: policy-setting subroutine
2174 : : * Mark a procedural language as to be dumped or not
2175 : : *
2176 : : * Procedural languages do not belong to any particular namespace. To
2177 : : * identify built-in languages, we must resort to checking whether the
2178 : : * language's OID is in the range reserved for initdb.
2179 : : */
2180 : : static void
2181 : 241 : selectDumpableProcLang(ProcLangInfo *plang, Archive *fout)
2182 : : {
2183 [ + + ]: 241 : if (checkExtensionMembership(&plang->dobj, fout))
2184 : 193 : return; /* extension membership overrides all else */
2185 : :
2186 : : /*
2187 : : * Only include procedural languages when we are dumping everything.
2188 : : *
2189 : : * For from-initdb procedural languages, only include ACLs, as we do for
2190 : : * the pg_catalog namespace. We need this because procedural languages do
2191 : : * not live in any namespace.
2192 : : */
2193 [ + + ]: 48 : if (!fout->dopt->include_everything)
2194 : 9 : plang->dobj.dump = DUMP_COMPONENT_NONE;
2195 : : else
2196 : : {
2197 [ - + ]: 39 : if (plang->dobj.catId.oid <= g_last_builtin_oid)
2198 : 0 : plang->dobj.dump = DUMP_COMPONENT_ACL;
2199 : : else
2200 : 39 : plang->dobj.dump = DUMP_COMPONENT_ALL;
2201 : : }
2202 : : }
2203 : :
2204 : : /*
2205 : : * selectDumpableAccessMethod: policy-setting subroutine
2206 : : * Mark an access method as to be dumped or not
2207 : : *
2208 : : * Access methods do not belong to any particular namespace. To identify
2209 : : * built-in access methods, we must resort to checking whether the
2210 : : * method's OID is in the range reserved for initdb.
2211 : : */
2212 : : static void
2213 : 1479 : selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout)
2214 : : {
2215 [ + + ]: 1479 : if (checkExtensionMembership(&method->dobj, fout))
2216 : 25 : return; /* extension membership overrides all else */
2217 : :
2218 : : /*
2219 : : * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
2220 : : * they do not support ACLs currently.
2221 : : */
2222 [ + + ]: 1454 : if (method->dobj.catId.oid <= g_last_builtin_oid)
2223 : 1351 : method->dobj.dump = DUMP_COMPONENT_NONE;
2224 : : else
2225 : 103 : method->dobj.dump = fout->dopt->include_everything ?
2226 [ + + ]: 103 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2227 : : }
2228 : :
2229 : : /*
2230 : : * selectDumpableExtension: policy-setting subroutine
2231 : : * Mark an extension as to be dumped or not
2232 : : *
2233 : : * Built-in extensions should be skipped except for checking ACLs, since we
2234 : : * assume those will already be installed in the target database. We identify
2235 : : * such extensions by their having OIDs in the range reserved for initdb.
2236 : : * We dump all user-added extensions by default. No extensions are dumped
2237 : : * if include_everything is false (i.e., a --schema or --table switch was
2238 : : * given), except if --extension specifies a list of extensions to dump.
2239 : : */
2240 : : static void
2241 : 225 : selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
2242 : : {
2243 : : /*
2244 : : * Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to
2245 : : * change permissions on their member objects, if they wish to, and have
2246 : : * those changes preserved.
2247 : : */
2248 [ + + ]: 225 : if (extinfo->dobj.catId.oid <= g_last_builtin_oid)
2249 : 194 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
2250 : : else
2251 : : {
2252 : : /* check if there is a list of extensions to dump */
2253 [ + + ]: 31 : if (extension_include_oids.head != NULL)
2254 : 4 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2255 : 4 : simple_oid_list_member(&extension_include_oids,
2256 : : extinfo->dobj.catId.oid) ?
2257 [ + + ]: 4 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2258 : : else
2259 : 27 : extinfo->dobj.dump = extinfo->dobj.dump_contains =
2260 : 27 : dopt->include_everything ?
2261 [ + + ]: 27 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2262 : :
2263 : : /* check that the extension is not explicitly excluded */
2264 [ + + + + ]: 58 : if (extinfo->dobj.dump &&
2265 : 27 : simple_oid_list_member(&extension_exclude_oids,
2266 : : extinfo->dobj.catId.oid))
2267 : 2 : extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE;
2268 : : }
2269 : 225 : }
2270 : :
2271 : : /*
2272 : : * selectDumpablePublicationObject: policy-setting subroutine
2273 : : * Mark a publication object as to be dumped or not
2274 : : *
2275 : : * A publication can have schemas and tables which have schemas, but those are
2276 : : * ignored in decision making, because publications are only dumped when we are
2277 : : * dumping everything.
2278 : : */
2279 : : static void
2280 : 502 : selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout)
2281 : : {
2282 [ - + ]: 502 : if (checkExtensionMembership(dobj, fout))
2283 : 0 : return; /* extension membership overrides all else */
2284 : :
2285 : 502 : dobj->dump = fout->dopt->include_everything ?
2286 [ + + ]: 502 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2287 : : }
2288 : :
2289 : : /*
2290 : : * selectDumpableStatisticsObject: policy-setting subroutine
2291 : : * Mark an extended statistics object as to be dumped or not
2292 : : *
2293 : : * We dump an extended statistics object if the schema it's in and the table
2294 : : * it's for are being dumped. (This'll need more thought if statistics
2295 : : * objects ever support cross-table stats.)
2296 : : */
2297 : : static void
2298 : 220 : selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout)
2299 : : {
2300 [ - + ]: 220 : if (checkExtensionMembership(&sobj->dobj, fout))
2301 : 0 : return; /* extension membership overrides all else */
2302 : :
2303 : 220 : sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains;
2304 [ + - ]: 220 : if (sobj->stattable == NULL ||
2305 [ + + ]: 220 : !(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION))
2306 : 35 : sobj->dobj.dump = DUMP_COMPONENT_NONE;
2307 : : }
2308 : :
2309 : : /*
2310 : : * selectDumpableObject: policy-setting subroutine
2311 : : * Mark a generic dumpable object as to be dumped or not
2312 : : *
2313 : : * Use this only for object types without a special-case routine above.
2314 : : */
2315 : : static void
2316 : 427951 : selectDumpableObject(DumpableObject *dobj, Archive *fout)
2317 : : {
2318 [ + + ]: 427951 : if (checkExtensionMembership(dobj, fout))
2319 : 207 : return; /* extension membership overrides all else */
2320 : :
2321 : : /*
2322 : : * Default policy is to dump if parent namespace is dumpable, or for
2323 : : * non-namespace-associated items, dump if we're dumping "everything".
2324 : : */
2325 [ + + ]: 427744 : if (dobj->namespace)
2326 : 426829 : dobj->dump = dobj->namespace->dobj.dump_contains;
2327 : : else
2328 : 915 : dobj->dump = fout->dopt->include_everything ?
2329 [ + + ]: 915 : DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2330 : : }
2331 : :
2332 : : /*
2333 : : * Dump a table's contents for loading using the COPY command
2334 : : * - this routine is called by the Archiver when it wants the table
2335 : : * to be dumped.
2336 : : */
2337 : : static int
2338 : 4438 : dumpTableData_copy(Archive *fout, const void *dcontext)
2339 : : {
2340 : 4438 : const TableDataInfo *tdinfo = dcontext;
2341 : 4438 : const TableInfo *tbinfo = tdinfo->tdtable;
2342 : 4438 : const char *classname = tbinfo->dobj.name;
2343 : 4438 : PQExpBuffer q = createPQExpBuffer();
2344 : :
2345 : : /*
2346 : : * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
2347 : : * which uses it already.
2348 : : */
2349 : 4438 : PQExpBuffer clistBuf = createPQExpBuffer();
2350 : 4438 : PGconn *conn = GetConnection(fout);
2351 : : PGresult *res;
2352 : : int ret;
2353 : : char *copybuf;
2354 : : const char *column_list;
2355 : :
2356 : 4438 : pg_log_info("dumping contents of table \"%s.%s\"",
2357 : : tbinfo->dobj.namespace->dobj.name, classname);
2358 : :
2359 : : /*
2360 : : * Specify the column list explicitly so that we have no possibility of
2361 : : * retrieving data in the wrong column order. (The default column
2362 : : * ordering of COPY will not be what we want in certain corner cases
2363 : : * involving ADD COLUMN and inheritance.)
2364 : : */
2365 : 4438 : column_list = fmtCopyColumnList(tbinfo, clistBuf);
2366 : :
2367 : : /*
2368 : : * Use COPY (SELECT ...) TO when dumping a foreign table's data, when a
2369 : : * filter condition was specified, and when in binary upgrade mode and
2370 : : * dumping an old pg_largeobject_metadata defined WITH OIDS. For other
2371 : : * cases a simple COPY suffices.
2372 : : */
2373 [ + + + + ]: 4438 : if (tdinfo->filtercond || tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
2374 [ - + - - ]: 4395 : (fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
2375 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
2376 : : {
2377 : : /* Temporary allows to access to foreign tables to dump data */
2378 [ + + ]: 43 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2379 : 1 : set_restrict_relation_kind(fout, "view");
2380 : :
2381 : 43 : appendPQExpBufferStr(q, "COPY (SELECT ");
2382 : : /* klugery to get rid of parens in column list */
2383 [ + - ]: 43 : if (strlen(column_list) > 2)
2384 : : {
2385 : 43 : appendPQExpBufferStr(q, column_list + 1);
2386 : 43 : q->data[q->len - 1] = ' ';
2387 : : }
2388 : : else
2389 : 0 : appendPQExpBufferStr(q, "* ");
2390 : :
2391 : 86 : appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
2392 : 43 : fmtQualifiedDumpable(tbinfo),
2393 [ + + ]: 43 : tdinfo->filtercond ? tdinfo->filtercond : "");
2394 : : }
2395 : : else
2396 : : {
2397 : 4395 : appendPQExpBuffer(q, "COPY %s %s TO stdout;",
2398 : 4395 : fmtQualifiedDumpable(tbinfo),
2399 : : column_list);
2400 : : }
2401 : 4438 : res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
2402 : 4437 : PQclear(res);
2403 : 4437 : destroyPQExpBuffer(clistBuf);
2404 : :
2405 : : for (;;)
2406 : : {
2407 : 1824336 : ret = PQgetCopyData(conn, ©buf, 0);
2408 : :
2409 [ + + ]: 1824336 : if (ret < 0)
2410 : 4437 : break; /* done or error */
2411 : :
2412 [ + - ]: 1819899 : if (copybuf)
2413 : : {
2414 : 1819899 : WriteData(fout, copybuf, ret);
2415 : 1819899 : PQfreemem(copybuf);
2416 : : }
2417 : :
2418 : : /* ----------
2419 : : * THROTTLE:
2420 : : *
2421 : : * There was considerable discussion in late July, 2000 regarding
2422 : : * slowing down pg_dump when backing up large tables. Users with both
2423 : : * slow & fast (multi-processor) machines experienced performance
2424 : : * degradation when doing a backup.
2425 : : *
2426 : : * Initial attempts based on sleeping for a number of ms for each ms
2427 : : * of work were deemed too complex, then a simple 'sleep in each loop'
2428 : : * implementation was suggested. The latter failed because the loop
2429 : : * was too tight. Finally, the following was implemented:
2430 : : *
2431 : : * If throttle is non-zero, then
2432 : : * See how long since the last sleep.
2433 : : * Work out how long to sleep (based on ratio).
2434 : : * If sleep is more than 100ms, then
2435 : : * sleep
2436 : : * reset timer
2437 : : * EndIf
2438 : : * EndIf
2439 : : *
2440 : : * where the throttle value was the number of ms to sleep per ms of
2441 : : * work. The calculation was done in each loop.
2442 : : *
2443 : : * Most of the hard work is done in the backend, and this solution
2444 : : * still did not work particularly well: on slow machines, the ratio
2445 : : * was 50:1, and on medium paced machines, 1:1, and on fast
2446 : : * multi-processor machines, it had little or no effect, for reasons
2447 : : * that were unclear.
2448 : : *
2449 : : * Further discussion ensued, and the proposal was dropped.
2450 : : *
2451 : : * For those people who want this feature, it can be implemented using
2452 : : * gettimeofday in each loop, calculating the time since last sleep,
2453 : : * multiplying that by the sleep ratio, then if the result is more
2454 : : * than a preset 'minimum sleep time' (say 100ms), call the 'select'
2455 : : * function to sleep for a subsecond period ie.
2456 : : *
2457 : : * select(0, NULL, NULL, NULL, &tvi);
2458 : : *
2459 : : * This will return after the interval specified in the structure tvi.
2460 : : * Finally, call gettimeofday again to save the 'last sleep time'.
2461 : : * ----------
2462 : : */
2463 : : }
2464 : 4437 : archprintf(fout, "\\.\n\n\n");
2465 : :
2466 [ - + ]: 4437 : if (ret == -2)
2467 : : {
2468 : : /* copy data transfer failed */
2469 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.", classname);
2470 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2471 : 0 : pg_log_error_detail("Command was: %s", q->data);
2472 : 0 : exit_nicely(1);
2473 : : }
2474 : :
2475 : : /* Check command status and return to normal libpq state */
2476 : 4437 : res = PQgetResult(conn);
2477 [ - + ]: 4437 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
2478 : : {
2479 : 0 : pg_log_error("Dumping the contents of table \"%s\" failed: PQgetResult() failed.", classname);
2480 : 0 : pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn));
2481 : 0 : pg_log_error_detail("Command was: %s", q->data);
2482 : 0 : exit_nicely(1);
2483 : : }
2484 : 4437 : PQclear(res);
2485 : :
2486 : : /* Do this to ensure we've pumped libpq back to idle state */
2487 [ - + ]: 4437 : if (PQgetResult(conn) != NULL)
2488 : 0 : pg_log_warning("unexpected extra results during COPY of table \"%s\"",
2489 : : classname);
2490 : :
2491 : 4437 : destroyPQExpBuffer(q);
2492 : :
2493 : : /* Revert back the setting */
2494 [ - + ]: 4437 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2495 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2496 : :
2497 : 4437 : return 1;
2498 : : }
2499 : :
2500 : : /*
2501 : : * Dump table data using INSERT commands.
2502 : : *
2503 : : * Caution: when we restore from an archive file direct to database, the
2504 : : * INSERT commands emitted by this function have to be parsed by
2505 : : * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments,
2506 : : * E'' strings, or dollar-quoted strings. So don't emit anything like that.
2507 : : */
2508 : : static int
2509 : 87 : dumpTableData_insert(Archive *fout, const void *dcontext)
2510 : : {
2511 : 87 : const TableDataInfo *tdinfo = dcontext;
2512 : 87 : const TableInfo *tbinfo = tdinfo->tdtable;
2513 : 87 : DumpOptions *dopt = fout->dopt;
2514 : 87 : PQExpBuffer q = createPQExpBuffer();
2515 : 87 : PQExpBuffer insertStmt = NULL;
2516 : : char *attgenerated;
2517 : : PGresult *res;
2518 : : int nfields,
2519 : : i;
2520 : 87 : int rows_per_statement = dopt->dump_inserts;
2521 : 87 : int rows_this_statement = 0;
2522 : :
2523 : : /* Temporary allows to access to foreign tables to dump data */
2524 [ - + ]: 87 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2525 : 0 : set_restrict_relation_kind(fout, "view");
2526 : :
2527 : : /*
2528 : : * If we're going to emit INSERTs with column names, the most efficient
2529 : : * way to deal with generated columns is to exclude them entirely. For
2530 : : * INSERTs without column names, we have to emit DEFAULT rather than the
2531 : : * actual column value --- but we can save a few cycles by fetching nulls
2532 : : * rather than the uninteresting-to-us value.
2533 : : */
2534 : 87 : attgenerated = pg_malloc_array(char, tbinfo->numatts);
2535 : 87 : appendPQExpBufferStr(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT ");
2536 : 87 : nfields = 0;
2537 [ + + ]: 269 : for (i = 0; i < tbinfo->numatts; i++)
2538 : : {
2539 [ + + ]: 182 : if (tbinfo->attisdropped[i])
2540 : 2 : continue;
2541 [ + + + + ]: 180 : if (tbinfo->attgenerated[i] && dopt->column_inserts)
2542 : 8 : continue;
2543 [ + + ]: 172 : if (nfields > 0)
2544 : 92 : appendPQExpBufferStr(q, ", ");
2545 [ + + ]: 172 : if (tbinfo->attgenerated[i])
2546 : 8 : appendPQExpBufferStr(q, "NULL");
2547 : : else
2548 : 164 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[i]));
2549 : 172 : attgenerated[nfields] = tbinfo->attgenerated[i];
2550 : 172 : nfields++;
2551 : : }
2552 : : /* Servers before 9.4 will complain about zero-column SELECT */
2553 [ + + ]: 87 : if (nfields == 0)
2554 : 7 : appendPQExpBufferStr(q, "NULL");
2555 : 87 : appendPQExpBuffer(q, " FROM ONLY %s",
2556 : 87 : fmtQualifiedDumpable(tbinfo));
2557 [ - + ]: 87 : if (tdinfo->filtercond)
2558 : 0 : appendPQExpBuffer(q, " %s", tdinfo->filtercond);
2559 : :
2560 : 87 : ExecuteSqlStatement(fout, q->data);
2561 : :
2562 : : while (1)
2563 : : {
2564 : 139 : res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
2565 : : PGRES_TUPLES_OK);
2566 : :
2567 : : /* cross-check field count, allowing for dummy NULL if any */
2568 [ + + + - ]: 139 : if (nfields != PQnfields(res) &&
2569 [ - + ]: 10 : !(nfields == 0 && PQnfields(res) == 1))
2570 : 0 : pg_fatal("wrong number of fields retrieved from table \"%s\"",
2571 : : tbinfo->dobj.name);
2572 : :
2573 : : /*
2574 : : * First time through, we build as much of the INSERT statement as
2575 : : * possible in "insertStmt", which we can then just print for each
2576 : : * statement. If the table happens to have zero dumpable columns then
2577 : : * this will be a complete statement, otherwise it will end in
2578 : : * "VALUES" and be ready to have the row's column values printed.
2579 : : */
2580 [ + + ]: 139 : if (insertStmt == NULL)
2581 : : {
2582 : : const TableInfo *targettab;
2583 : :
2584 : 87 : insertStmt = createPQExpBuffer();
2585 : :
2586 : : /*
2587 : : * When load-via-partition-root is set or forced, get the root
2588 : : * table name for the partition table, so that we can reload data
2589 : : * through the root table.
2590 : : */
2591 [ + + ]: 87 : if (tbinfo->ispartition &&
2592 [ + - + + ]: 48 : (dopt->load_via_partition_root ||
2593 : 24 : forcePartitionRootLoad(tbinfo)))
2594 : 7 : targettab = getRootTableInfo(tbinfo);
2595 : : else
2596 : 80 : targettab = tbinfo;
2597 : :
2598 : 87 : appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
2599 : 87 : fmtQualifiedDumpable(targettab));
2600 : :
2601 : : /* corner case for zero-column table */
2602 [ + + ]: 87 : if (nfields == 0)
2603 : : {
2604 : 7 : appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n");
2605 : : }
2606 : : else
2607 : : {
2608 : : /* append the list of column names if required */
2609 [ + + ]: 80 : if (dopt->column_inserts)
2610 : : {
2611 : 36 : appendPQExpBufferChar(insertStmt, '(');
2612 [ + + ]: 109 : for (int field = 0; field < nfields; field++)
2613 : : {
2614 [ + + ]: 73 : if (field > 0)
2615 : 37 : appendPQExpBufferStr(insertStmt, ", ");
2616 : 73 : appendPQExpBufferStr(insertStmt,
2617 : 73 : fmtId(PQfname(res, field)));
2618 : : }
2619 : 36 : appendPQExpBufferStr(insertStmt, ") ");
2620 : : }
2621 : :
2622 [ + + ]: 80 : if (tbinfo->needs_override)
2623 : 2 : appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE ");
2624 : :
2625 : 80 : appendPQExpBufferStr(insertStmt, "VALUES");
2626 : : }
2627 : : }
2628 : :
2629 [ + + ]: 3608 : for (int tuple = 0; tuple < PQntuples(res); tuple++)
2630 : : {
2631 : : /* Write the INSERT if not in the middle of a multi-row INSERT. */
2632 [ + + ]: 3469 : if (rows_this_statement == 0)
2633 : 3463 : archputs(insertStmt->data, fout);
2634 : :
2635 : : /*
2636 : : * If it is zero-column table then we've already written the
2637 : : * complete statement, which will mean we've disobeyed
2638 : : * --rows-per-insert when it's set greater than 1. We do support
2639 : : * a way to make this multi-row with: SELECT UNION ALL SELECT
2640 : : * UNION ALL ... but that's non-standard so we should avoid it
2641 : : * given that using INSERTs is mostly only ever needed for
2642 : : * cross-database exports.
2643 : : */
2644 [ + + ]: 3469 : if (nfields == 0)
2645 : 6 : continue;
2646 : :
2647 : : /* Emit a row heading */
2648 [ + + ]: 3463 : if (rows_per_statement == 1)
2649 : 3454 : archputs(" (", fout);
2650 [ + + ]: 9 : else if (rows_this_statement > 0)
2651 : 6 : archputs(",\n\t(", fout);
2652 : : else
2653 : 3 : archputs("\n\t(", fout);
2654 : :
2655 [ + + ]: 10445 : for (int field = 0; field < nfields; field++)
2656 : : {
2657 [ + + ]: 6982 : if (field > 0)
2658 : 3519 : archputs(", ", fout);
2659 [ + + ]: 6982 : if (attgenerated[field])
2660 : : {
2661 : 2 : archputs("DEFAULT", fout);
2662 : 2 : continue;
2663 : : }
2664 [ + + ]: 6980 : if (PQgetisnull(res, tuple, field))
2665 : : {
2666 : 83 : archputs("NULL", fout);
2667 : 83 : continue;
2668 : : }
2669 : :
2670 : : /* XXX This code is partially duplicated in ruleutils.c */
2671 [ + + + + ]: 6897 : switch (PQftype(res, field))
2672 : : {
2673 : 4869 : case INT2OID:
2674 : : case INT4OID:
2675 : : case INT8OID:
2676 : : case OIDOID:
2677 : : case FLOAT4OID:
2678 : : case FLOAT8OID:
2679 : : case NUMERICOID:
2680 : : {
2681 : : /*
2682 : : * These types are printed without quotes unless
2683 : : * they contain values that aren't accepted by the
2684 : : * scanner unquoted (e.g., 'NaN'). Note that
2685 : : * strtod() and friends might accept NaN, so we
2686 : : * can't use that to test.
2687 : : *
2688 : : * In reality we only need to defend against
2689 : : * infinity and NaN, so we need not get too crazy
2690 : : * about pattern matching here.
2691 : : */
2692 : 4869 : const char *s = PQgetvalue(res, tuple, field);
2693 : :
2694 [ + + ]: 4869 : if (strspn(s, "0123456789 +-eE.") == strlen(s))
2695 : 4867 : archputs(s, fout);
2696 : : else
2697 : 2 : archprintf(fout, "'%s'", s);
2698 : : }
2699 : 4869 : break;
2700 : :
2701 : 2 : case BITOID:
2702 : : case VARBITOID:
2703 : 2 : archprintf(fout, "B'%s'",
2704 : : PQgetvalue(res, tuple, field));
2705 : 2 : break;
2706 : :
2707 : 4 : case BOOLOID:
2708 [ + + ]: 4 : if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
2709 : 2 : archputs("true", fout);
2710 : : else
2711 : 2 : archputs("false", fout);
2712 : 4 : break;
2713 : :
2714 : 2022 : default:
2715 : : /* All other types are printed as string literals. */
2716 : 2022 : resetPQExpBuffer(q);
2717 : 2022 : appendStringLiteralAH(q,
2718 : : PQgetvalue(res, tuple, field),
2719 : : fout);
2720 : 2022 : archputs(q->data, fout);
2721 : 2022 : break;
2722 : : }
2723 : : }
2724 : :
2725 : : /* Terminate the row ... */
2726 : 3463 : archputs(")", fout);
2727 : :
2728 : : /* ... and the statement, if the target no. of rows is reached */
2729 [ + + ]: 3463 : if (++rows_this_statement >= rows_per_statement)
2730 : : {
2731 [ - + ]: 3456 : if (dopt->do_nothing)
2732 : 0 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2733 : : else
2734 : 3456 : archputs(";\n", fout);
2735 : : /* Reset the row counter */
2736 : 3456 : rows_this_statement = 0;
2737 : : }
2738 : : }
2739 : :
2740 [ + + ]: 139 : if (PQntuples(res) <= 0)
2741 : : {
2742 : 87 : PQclear(res);
2743 : 87 : break;
2744 : : }
2745 : 52 : PQclear(res);
2746 : : }
2747 : :
2748 : : /* Terminate any statements that didn't make the row count. */
2749 [ + + ]: 87 : if (rows_this_statement > 0)
2750 : : {
2751 [ - + ]: 1 : if (dopt->do_nothing)
2752 : 0 : archputs(" ON CONFLICT DO NOTHING;\n", fout);
2753 : : else
2754 : 1 : archputs(";\n", fout);
2755 : : }
2756 : :
2757 : 87 : archputs("\n\n", fout);
2758 : :
2759 : 87 : ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
2760 : :
2761 : 87 : destroyPQExpBuffer(q);
2762 [ + - ]: 87 : if (insertStmt != NULL)
2763 : 87 : destroyPQExpBuffer(insertStmt);
2764 : 87 : pg_free(attgenerated);
2765 : :
2766 : : /* Revert back the setting */
2767 [ - + ]: 87 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2768 : 0 : set_restrict_relation_kind(fout, "view, foreign-table");
2769 : :
2770 : 87 : return 1;
2771 : : }
2772 : :
2773 : : /*
2774 : : * getRootTableInfo:
2775 : : * get the root TableInfo for the given partition table.
2776 : : */
2777 : : static TableInfo *
2778 : 83 : getRootTableInfo(const TableInfo *tbinfo)
2779 : : {
2780 : : TableInfo *parentTbinfo;
2781 : :
2782 : : Assert(tbinfo->ispartition);
2783 : : Assert(tbinfo->numParents == 1);
2784 : :
2785 : 83 : parentTbinfo = tbinfo->parents[0];
2786 [ - + ]: 83 : while (parentTbinfo->ispartition)
2787 : : {
2788 : : Assert(parentTbinfo->numParents == 1);
2789 : 0 : parentTbinfo = parentTbinfo->parents[0];
2790 : : }
2791 : :
2792 : 83 : return parentTbinfo;
2793 : : }
2794 : :
2795 : : /*
2796 : : * forcePartitionRootLoad
2797 : : * Check if we must force load_via_partition_root for this partition.
2798 : : *
2799 : : * This is required if any level of ancestral partitioned table has an
2800 : : * unsafe partitioning scheme.
2801 : : */
2802 : : static bool
2803 : 1106 : forcePartitionRootLoad(const TableInfo *tbinfo)
2804 : : {
2805 : : TableInfo *parentTbinfo;
2806 : :
2807 : : Assert(tbinfo->ispartition);
2808 : : Assert(tbinfo->numParents == 1);
2809 : :
2810 : 1106 : parentTbinfo = tbinfo->parents[0];
2811 [ + + ]: 1106 : if (parentTbinfo->unsafe_partitions)
2812 : 83 : return true;
2813 [ + + ]: 1243 : while (parentTbinfo->ispartition)
2814 : : {
2815 : : Assert(parentTbinfo->numParents == 1);
2816 : 220 : parentTbinfo = parentTbinfo->parents[0];
2817 [ - + ]: 220 : if (parentTbinfo->unsafe_partitions)
2818 : 0 : return true;
2819 : : }
2820 : :
2821 : 1023 : return false;
2822 : : }
2823 : :
2824 : : /*
2825 : : * dumpTableData -
2826 : : * dump the contents of a single table
2827 : : *
2828 : : * Actually, this just makes an ArchiveEntry for the table contents.
2829 : : */
2830 : : static void
2831 : 4611 : dumpTableData(Archive *fout, const TableDataInfo *tdinfo)
2832 : : {
2833 : 4611 : DumpOptions *dopt = fout->dopt;
2834 : 4611 : const TableInfo *tbinfo = tdinfo->tdtable;
2835 : 4611 : PQExpBuffer copyBuf = createPQExpBuffer();
2836 : 4611 : PQExpBuffer clistBuf = createPQExpBuffer();
2837 : : DataDumperPtr dumpFn;
2838 : 4611 : char *tdDefn = NULL;
2839 : : char *copyStmt;
2840 : : const char *copyFrom;
2841 : :
2842 : : /* We had better have loaded per-column details about this table */
2843 : : Assert(tbinfo->interesting);
2844 : :
2845 : : /*
2846 : : * When load-via-partition-root is set or forced, get the root table name
2847 : : * for the partition table, so that we can reload data through the root
2848 : : * table. Then construct a comment to be inserted into the TOC entry's
2849 : : * defn field, so that such cases can be identified reliably.
2850 : : */
2851 [ + + ]: 4611 : if (tbinfo->ispartition &&
2852 [ + - + + ]: 2164 : (dopt->load_via_partition_root ||
2853 : 1082 : forcePartitionRootLoad(tbinfo)))
2854 : 76 : {
2855 : : const TableInfo *parentTbinfo;
2856 : : char *sanitized;
2857 : :
2858 : 76 : parentTbinfo = getRootTableInfo(tbinfo);
2859 : 76 : copyFrom = fmtQualifiedDumpable(parentTbinfo);
2860 : 76 : sanitized = sanitize_line(copyFrom, true);
2861 : 76 : printfPQExpBuffer(copyBuf, "-- load via partition root %s",
2862 : : sanitized);
2863 : 76 : free(sanitized);
2864 : 76 : tdDefn = pg_strdup(copyBuf->data);
2865 : : }
2866 : : else
2867 : 4535 : copyFrom = fmtQualifiedDumpable(tbinfo);
2868 : :
2869 [ + + ]: 4611 : if (dopt->dump_inserts == 0)
2870 : : {
2871 : : /* Dump/restore using COPY */
2872 : 4524 : dumpFn = dumpTableData_copy;
2873 : : /* must use 2 steps here 'cause fmtId is nonreentrant */
2874 : 4524 : printfPQExpBuffer(copyBuf, "COPY %s ",
2875 : : copyFrom);
2876 : 4524 : appendPQExpBuffer(copyBuf, "%s FROM stdin;\n",
2877 : : fmtCopyColumnList(tbinfo, clistBuf));
2878 : 4524 : copyStmt = copyBuf->data;
2879 : : }
2880 : : else
2881 : : {
2882 : : /* Restore using INSERT */
2883 : 87 : dumpFn = dumpTableData_insert;
2884 : 87 : copyStmt = NULL;
2885 : : }
2886 : :
2887 : : /*
2888 : : * Note: although the TableDataInfo is a full DumpableObject, we treat its
2889 : : * dependency on its table as "special" and pass it to ArchiveEntry now.
2890 : : * See comments for BuildArchiveDependencies.
2891 : : */
2892 [ + - ]: 4611 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2893 : : {
2894 : : TocEntry *te;
2895 : :
2896 : 4611 : te = ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
2897 : 4611 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
2898 : : .namespace = tbinfo->dobj.namespace->dobj.name,
2899 : : .owner = tbinfo->rolname,
2900 : : .description = "TABLE DATA",
2901 : : .section = SECTION_DATA,
2902 : : .createStmt = tdDefn,
2903 : : .copyStmt = copyStmt,
2904 : : .deps = &(tbinfo->dobj.dumpId),
2905 : : .nDeps = 1,
2906 : : .dumpFn = dumpFn,
2907 : : .dumpArg = tdinfo));
2908 : :
2909 : : /*
2910 : : * Set the TocEntry's dataLength in case we are doing a parallel dump
2911 : : * and want to order dump jobs by table size. We choose to measure
2912 : : * dataLength in table pages (including TOAST pages) during dump, so
2913 : : * no scaling is needed.
2914 : : *
2915 : : * However, relpages is declared as "integer" in pg_class, and hence
2916 : : * also in TableInfo, but it's really BlockNumber a/k/a unsigned int.
2917 : : * Cast so that we get the right interpretation of table sizes
2918 : : * exceeding INT_MAX pages.
2919 : : */
2920 : 4611 : te->dataLength = (BlockNumber) tbinfo->relpages;
2921 : 4611 : te->dataLength += (BlockNumber) tbinfo->toastpages;
2922 : :
2923 : : /*
2924 : : * If pgoff_t is only 32 bits wide, the above refinement is useless,
2925 : : * and instead we'd better worry about integer overflow. Clamp to
2926 : : * INT_MAX if the correct result exceeds that.
2927 : : */
2928 : : if (sizeof(te->dataLength) == 4 &&
2929 : : (tbinfo->relpages < 0 || tbinfo->toastpages < 0 ||
2930 : : te->dataLength < 0))
2931 : : te->dataLength = INT_MAX;
2932 : : }
2933 : :
2934 : 4611 : destroyPQExpBuffer(copyBuf);
2935 : 4611 : destroyPQExpBuffer(clistBuf);
2936 : 4611 : }
2937 : :
2938 : : /*
2939 : : * refreshMatViewData -
2940 : : * load or refresh the contents of a single materialized view
2941 : : *
2942 : : * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
2943 : : * statement.
2944 : : */
2945 : : static void
2946 : 363 : refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo)
2947 : : {
2948 : 363 : TableInfo *tbinfo = tdinfo->tdtable;
2949 : : PQExpBuffer q;
2950 : :
2951 : : /* If the materialized view is not flagged as populated, skip this. */
2952 [ + + ]: 363 : if (!tbinfo->relispopulated)
2953 : 72 : return;
2954 : :
2955 : 291 : q = createPQExpBuffer();
2956 : :
2957 : 291 : appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
2958 : 291 : fmtQualifiedDumpable(tbinfo));
2959 : :
2960 [ + - ]: 291 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2961 : 291 : ArchiveEntry(fout,
2962 : : tdinfo->dobj.catId, /* catalog ID */
2963 : 291 : tdinfo->dobj.dumpId, /* dump ID */
2964 : 291 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
2965 : : .namespace = tbinfo->dobj.namespace->dobj.name,
2966 : : .owner = tbinfo->rolname,
2967 : : .description = "MATERIALIZED VIEW DATA",
2968 : : .section = SECTION_POST_DATA,
2969 : : .createStmt = q->data,
2970 : : .deps = tdinfo->dobj.dependencies,
2971 : : .nDeps = tdinfo->dobj.nDeps));
2972 : :
2973 : 291 : destroyPQExpBuffer(q);
2974 : : }
2975 : :
2976 : : /*
2977 : : * getTableData -
2978 : : * set up dumpable objects representing the contents of tables
2979 : : */
2980 : : static void
2981 : 184 : getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
2982 : : {
2983 : : int i;
2984 : :
2985 [ + + ]: 53286 : for (i = 0; i < numTables; i++)
2986 : : {
2987 [ + + + + ]: 53102 : if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
2988 [ + + ]: 1015 : (!relkind || tblinfo[i].relkind == relkind))
2989 : 6493 : makeTableDataInfo(dopt, &(tblinfo[i]));
2990 : : }
2991 : 184 : }
2992 : :
2993 : : /*
2994 : : * Make a dumpable object for the data of this specific table
2995 : : *
2996 : : * Note: we make a TableDataInfo if and only if we are going to dump the
2997 : : * table data; the "dump" field in such objects isn't very interesting.
2998 : : */
2999 : : static void
3000 : 6574 : makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo)
3001 : : {
3002 : : TableDataInfo *tdinfo;
3003 : :
3004 : : /*
3005 : : * Nothing to do if we already decided to dump the table. This will
3006 : : * happen for "config" tables.
3007 : : */
3008 [ + + ]: 6574 : if (tbinfo->dataObj != NULL)
3009 : 1 : return;
3010 : :
3011 : : /* Skip property graphs (no data to dump) */
3012 [ + + ]: 6573 : if (tbinfo->relkind == RELKIND_PROPGRAPH)
3013 : 92 : return;
3014 : : /* Skip VIEWs (no data to dump) */
3015 [ + + ]: 6481 : if (tbinfo->relkind == RELKIND_VIEW)
3016 : 520 : return;
3017 : : /* Skip FOREIGN TABLEs (no data to dump) unless requested explicitly */
3018 [ + + ]: 5961 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
3019 [ + + ]: 40 : (foreign_servers_include_oids.head == NULL ||
3020 [ + + ]: 4 : !simple_oid_list_member(&foreign_servers_include_oids,
3021 : : tbinfo->foreign_server)))
3022 : 39 : return;
3023 : : /* Skip partitioned tables (data in partitions) */
3024 [ + + ]: 5922 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
3025 : 523 : return;
3026 : :
3027 : : /* Don't dump data in unlogged tables, if so requested */
3028 [ + + ]: 5399 : if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
3029 [ + + ]: 41 : dopt->no_unlogged_table_data)
3030 : 18 : return;
3031 : :
3032 : : /* Check that the data is not explicitly excluded */
3033 [ + + ]: 5381 : if (simple_oid_list_member(&tabledata_exclude_oids,
3034 : : tbinfo->dobj.catId.oid))
3035 : 8 : return;
3036 : :
3037 : : /* OK, let's dump it */
3038 : 5373 : tdinfo = pg_malloc_object(TableDataInfo);
3039 : :
3040 [ + + ]: 5373 : if (tbinfo->relkind == RELKIND_MATVIEW)
3041 : 363 : tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
3042 [ + + ]: 5010 : else if (tbinfo->relkind == RELKIND_SEQUENCE)
3043 : 399 : tdinfo->dobj.objType = DO_SEQUENCE_SET;
3044 : : else
3045 : 4611 : tdinfo->dobj.objType = DO_TABLE_DATA;
3046 : :
3047 : : /*
3048 : : * Note: use tableoid 0 so that this object won't be mistaken for
3049 : : * something that pg_depend entries apply to.
3050 : : */
3051 : 5373 : tdinfo->dobj.catId.tableoid = 0;
3052 : 5373 : tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
3053 : 5373 : AssignDumpId(&tdinfo->dobj);
3054 : 5373 : tdinfo->dobj.name = tbinfo->dobj.name;
3055 : 5373 : tdinfo->dobj.namespace = tbinfo->dobj.namespace;
3056 : 5373 : tdinfo->tdtable = tbinfo;
3057 : 5373 : tdinfo->filtercond = NULL; /* might get set later */
3058 : 5373 : addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
3059 : :
3060 : : /* A TableDataInfo contains data, of course */
3061 : 5373 : tdinfo->dobj.components |= DUMP_COMPONENT_DATA;
3062 : :
3063 : 5373 : tbinfo->dataObj = tdinfo;
3064 : :
3065 : : /*
3066 : : * Materialized view statistics must be restored after the data, because
3067 : : * REFRESH MATERIALIZED VIEW replaces the storage and resets the stats.
3068 : : *
3069 : : * The dependency is added here because the statistics objects are created
3070 : : * first.
3071 : : */
3072 [ + + + + ]: 5373 : if (tbinfo->relkind == RELKIND_MATVIEW && tbinfo->stats != NULL)
3073 : : {
3074 : 286 : tbinfo->stats->section = SECTION_POST_DATA;
3075 : 286 : addObjectDependency(&tbinfo->stats->dobj, tdinfo->dobj.dumpId);
3076 : : }
3077 : :
3078 : : /* Make sure that we'll collect per-column info for this table. */
3079 : 5373 : tbinfo->interesting = true;
3080 : : }
3081 : :
3082 : : /*
3083 : : * The refresh for a materialized view must be dependent on the refresh for
3084 : : * any materialized view that this one is dependent on.
3085 : : *
3086 : : * This must be called after all the objects are created, but before they are
3087 : : * sorted.
3088 : : */
3089 : : static void
3090 : 146 : buildMatViewRefreshDependencies(Archive *fout)
3091 : : {
3092 : : PQExpBuffer query;
3093 : : PGresult *res;
3094 : : int ntups,
3095 : : i;
3096 : : int i_classid,
3097 : : i_objid,
3098 : : i_refobjid;
3099 : :
3100 : 146 : query = createPQExpBuffer();
3101 : :
3102 : 146 : appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
3103 : : "( "
3104 : : "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
3105 : : "FROM pg_depend d1 "
3106 : : "JOIN pg_class c1 ON c1.oid = d1.objid "
3107 : : "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
3108 : : " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
3109 : : "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
3110 : : "AND d2.objid = r1.oid "
3111 : : "AND d2.refobjid <> d1.objid "
3112 : : "JOIN pg_class c2 ON c2.oid = d2.refobjid "
3113 : : "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3114 : : CppAsString2(RELKIND_VIEW) ") "
3115 : : "WHERE d1.classid = 'pg_class'::regclass "
3116 : : "UNION "
3117 : : "SELECT w.objid, d3.refobjid, c3.relkind "
3118 : : "FROM w "
3119 : : "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
3120 : : "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
3121 : : "AND d3.objid = r3.oid "
3122 : : "AND d3.refobjid <> w.refobjid "
3123 : : "JOIN pg_class c3 ON c3.oid = d3.refobjid "
3124 : : "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
3125 : : CppAsString2(RELKIND_VIEW) ") "
3126 : : ") "
3127 : : "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
3128 : : "FROM w "
3129 : : "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
3130 : :
3131 : 146 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3132 : :
3133 : 146 : ntups = PQntuples(res);
3134 : :
3135 : 146 : i_classid = PQfnumber(res, "classid");
3136 : 146 : i_objid = PQfnumber(res, "objid");
3137 : 146 : i_refobjid = PQfnumber(res, "refobjid");
3138 : :
3139 [ + + ]: 422 : for (i = 0; i < ntups; i++)
3140 : : {
3141 : : CatalogId objId;
3142 : : CatalogId refobjId;
3143 : : DumpableObject *dobj;
3144 : : DumpableObject *refdobj;
3145 : : TableInfo *tbinfo;
3146 : : TableInfo *reftbinfo;
3147 : :
3148 : 276 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
3149 : 276 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
3150 : 276 : refobjId.tableoid = objId.tableoid;
3151 : 276 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
3152 : :
3153 : 276 : dobj = findObjectByCatalogId(objId);
3154 [ - + ]: 276 : if (dobj == NULL)
3155 : 48 : continue;
3156 : :
3157 : : Assert(dobj->objType == DO_TABLE);
3158 : 276 : tbinfo = (TableInfo *) dobj;
3159 : : Assert(tbinfo->relkind == RELKIND_MATVIEW);
3160 : 276 : dobj = (DumpableObject *) tbinfo->dataObj;
3161 [ + + ]: 276 : if (dobj == NULL)
3162 : 48 : continue;
3163 : : Assert(dobj->objType == DO_REFRESH_MATVIEW);
3164 : :
3165 : 228 : refdobj = findObjectByCatalogId(refobjId);
3166 [ - + ]: 228 : if (refdobj == NULL)
3167 : 0 : continue;
3168 : :
3169 : : Assert(refdobj->objType == DO_TABLE);
3170 : 228 : reftbinfo = (TableInfo *) refdobj;
3171 : : Assert(reftbinfo->relkind == RELKIND_MATVIEW);
3172 : 228 : refdobj = (DumpableObject *) reftbinfo->dataObj;
3173 [ - + ]: 228 : if (refdobj == NULL)
3174 : 0 : continue;
3175 : : Assert(refdobj->objType == DO_REFRESH_MATVIEW);
3176 : :
3177 : 228 : addObjectDependency(dobj, refdobj->dumpId);
3178 : :
3179 [ + + ]: 228 : if (!reftbinfo->relispopulated)
3180 : 36 : tbinfo->relispopulated = false;
3181 : : }
3182 : :
3183 : 146 : PQclear(res);
3184 : :
3185 : 146 : destroyPQExpBuffer(query);
3186 : 146 : }
3187 : :
3188 : : /*
3189 : : * getTableDataFKConstraints -
3190 : : * add dump-order dependencies reflecting foreign key constraints
3191 : : *
3192 : : * This code is executed only in a data-only dump --- in schema+data dumps
3193 : : * we handle foreign key issues by not creating the FK constraints until
3194 : : * after the data is loaded. In a data-only dump, however, we want to
3195 : : * order the table data objects in such a way that a table's referenced
3196 : : * tables are restored first. (In the presence of circular references or
3197 : : * self-references this may be impossible; we'll detect and complain about
3198 : : * that during the dependency sorting step.)
3199 : : */
3200 : : static void
3201 : 7 : getTableDataFKConstraints(void)
3202 : : {
3203 : : DumpableObject **dobjs;
3204 : : int numObjs;
3205 : : int i;
3206 : :
3207 : : /* Search through all the dumpable objects for FK constraints */
3208 : 7 : getDumpableObjects(&dobjs, &numObjs);
3209 [ + + ]: 26824 : for (i = 0; i < numObjs; i++)
3210 : : {
3211 [ + + ]: 26817 : if (dobjs[i]->objType == DO_FK_CONSTRAINT)
3212 : : {
3213 : 8 : ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
3214 : : TableInfo *ftable;
3215 : :
3216 : : /* Not interesting unless both tables are to be dumped */
3217 [ + - ]: 8 : if (cinfo->contable == NULL ||
3218 [ + + ]: 8 : cinfo->contable->dataObj == NULL)
3219 : 4 : continue;
3220 : 4 : ftable = findTableByOid(cinfo->confrelid);
3221 [ + - ]: 4 : if (ftable == NULL ||
3222 [ - + ]: 4 : ftable->dataObj == NULL)
3223 : 0 : continue;
3224 : :
3225 : : /*
3226 : : * Okay, make referencing table's TABLE_DATA object depend on the
3227 : : * referenced table's TABLE_DATA object.
3228 : : */
3229 : 4 : addObjectDependency(&cinfo->contable->dataObj->dobj,
3230 : 4 : ftable->dataObj->dobj.dumpId);
3231 : : }
3232 : : }
3233 : 7 : free(dobjs);
3234 : 7 : }
3235 : :
3236 : :
3237 : : /*
3238 : : * dumpDatabase:
3239 : : * dump the database definition
3240 : : */
3241 : : static void
3242 : 94 : dumpDatabase(Archive *fout)
3243 : : {
3244 : 94 : DumpOptions *dopt = fout->dopt;
3245 : 94 : PQExpBuffer dbQry = createPQExpBuffer();
3246 : 94 : PQExpBuffer delQry = createPQExpBuffer();
3247 : 94 : PQExpBuffer creaQry = createPQExpBuffer();
3248 : 94 : PQExpBuffer labelq = createPQExpBuffer();
3249 : 94 : PGconn *conn = GetConnection(fout);
3250 : : PGresult *res;
3251 : : int i_tableoid,
3252 : : i_oid,
3253 : : i_datname,
3254 : : i_datdba,
3255 : : i_encoding,
3256 : : i_datlocprovider,
3257 : : i_collate,
3258 : : i_ctype,
3259 : : i_datlocale,
3260 : : i_daticurules,
3261 : : i_frozenxid,
3262 : : i_minmxid,
3263 : : i_datacl,
3264 : : i_acldefault,
3265 : : i_datistemplate,
3266 : : i_datconnlimit,
3267 : : i_datcollversion,
3268 : : i_tablespace;
3269 : : CatalogId dbCatId;
3270 : : DumpId dbDumpId;
3271 : : DumpableAcl dbdacl;
3272 : : const char *datname,
3273 : : *dba,
3274 : : *encoding,
3275 : : *datlocprovider,
3276 : : *collate,
3277 : : *ctype,
3278 : : *locale,
3279 : : *icurules,
3280 : : *datistemplate,
3281 : : *datconnlimit,
3282 : : *tablespace;
3283 : : uint32 frozenxid,
3284 : : minmxid;
3285 : : char *qdatname;
3286 : :
3287 : 94 : pg_log_info("saving database definition");
3288 : :
3289 : : /*
3290 : : * Fetch the database-level properties for this database.
3291 : : */
3292 : 94 : appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, "
3293 : : "datdba, "
3294 : : "pg_encoding_to_char(encoding) AS encoding, "
3295 : : "datcollate, datctype, datfrozenxid, "
3296 : : "datacl, acldefault('d', datdba) AS acldefault, "
3297 : : "datistemplate, datconnlimit, ");
3298 : 94 : appendPQExpBufferStr(dbQry, "datminmxid, ");
3299 [ + - ]: 94 : if (fout->remoteVersion >= 170000)
3300 : 94 : appendPQExpBufferStr(dbQry, "datlocprovider, datlocale, datcollversion, ");
3301 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
3302 : 0 : appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale AS datlocale, datcollversion, ");
3303 : : else
3304 : 0 : appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS datlocale, NULL AS datcollversion, ");
3305 [ + - ]: 94 : if (fout->remoteVersion >= 160000)
3306 : 94 : appendPQExpBufferStr(dbQry, "daticurules, ");
3307 : : else
3308 : 0 : appendPQExpBufferStr(dbQry, "NULL AS daticurules, ");
3309 : 94 : appendPQExpBufferStr(dbQry,
3310 : : "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
3311 : : "shobj_description(oid, 'pg_database') AS description "
3312 : : "FROM pg_database "
3313 : : "WHERE datname = current_database()");
3314 : :
3315 : 94 : res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
3316 : :
3317 : 94 : i_tableoid = PQfnumber(res, "tableoid");
3318 : 94 : i_oid = PQfnumber(res, "oid");
3319 : 94 : i_datname = PQfnumber(res, "datname");
3320 : 94 : i_datdba = PQfnumber(res, "datdba");
3321 : 94 : i_encoding = PQfnumber(res, "encoding");
3322 : 94 : i_datlocprovider = PQfnumber(res, "datlocprovider");
3323 : 94 : i_collate = PQfnumber(res, "datcollate");
3324 : 94 : i_ctype = PQfnumber(res, "datctype");
3325 : 94 : i_datlocale = PQfnumber(res, "datlocale");
3326 : 94 : i_daticurules = PQfnumber(res, "daticurules");
3327 : 94 : i_frozenxid = PQfnumber(res, "datfrozenxid");
3328 : 94 : i_minmxid = PQfnumber(res, "datminmxid");
3329 : 94 : i_datacl = PQfnumber(res, "datacl");
3330 : 94 : i_acldefault = PQfnumber(res, "acldefault");
3331 : 94 : i_datistemplate = PQfnumber(res, "datistemplate");
3332 : 94 : i_datconnlimit = PQfnumber(res, "datconnlimit");
3333 : 94 : i_datcollversion = PQfnumber(res, "datcollversion");
3334 : 94 : i_tablespace = PQfnumber(res, "tablespace");
3335 : :
3336 : 94 : dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
3337 : 94 : dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
3338 : 94 : datname = PQgetvalue(res, 0, i_datname);
3339 : 94 : dba = getRoleName(PQgetvalue(res, 0, i_datdba));
3340 : 94 : encoding = PQgetvalue(res, 0, i_encoding);
3341 : 94 : datlocprovider = PQgetvalue(res, 0, i_datlocprovider);
3342 : 94 : collate = PQgetvalue(res, 0, i_collate);
3343 : 94 : ctype = PQgetvalue(res, 0, i_ctype);
3344 [ + + ]: 94 : if (!PQgetisnull(res, 0, i_datlocale))
3345 : 14 : locale = PQgetvalue(res, 0, i_datlocale);
3346 : : else
3347 : 80 : locale = NULL;
3348 [ - + ]: 94 : if (!PQgetisnull(res, 0, i_daticurules))
3349 : 0 : icurules = PQgetvalue(res, 0, i_daticurules);
3350 : : else
3351 : 94 : icurules = NULL;
3352 : 94 : frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
3353 : 94 : minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
3354 : 94 : dbdacl.acl = PQgetvalue(res, 0, i_datacl);
3355 : 94 : dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault);
3356 : 94 : datistemplate = PQgetvalue(res, 0, i_datistemplate);
3357 : 94 : datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
3358 : 94 : tablespace = PQgetvalue(res, 0, i_tablespace);
3359 : :
3360 : 94 : qdatname = pg_strdup(fmtId(datname));
3361 : :
3362 : : /*
3363 : : * Prepare the CREATE DATABASE command. We must specify OID (if we want
3364 : : * to preserve that), as well as the encoding, locale, and tablespace
3365 : : * since those can't be altered later. Other DB properties are left to
3366 : : * the DATABASE PROPERTIES entry, so that they can be applied after
3367 : : * reconnecting to the target DB.
3368 : : *
3369 : : * For binary upgrade, we use the FILE_COPY strategy because testing has
3370 : : * shown it to be faster. When the server is in binary upgrade mode, it
3371 : : * will also skip the checkpoints this strategy ordinarily performs.
3372 : : */
3373 [ + + ]: 94 : if (dopt->binary_upgrade)
3374 : : {
3375 : 41 : appendPQExpBuffer(creaQry,
3376 : : "CREATE DATABASE %s WITH TEMPLATE = template0 "
3377 : : "OID = %u STRATEGY = FILE_COPY",
3378 : : qdatname, dbCatId.oid);
3379 : : }
3380 : : else
3381 : : {
3382 : 53 : appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
3383 : : qdatname);
3384 : : }
3385 [ + - ]: 94 : if (strlen(encoding) > 0)
3386 : : {
3387 : 94 : appendPQExpBufferStr(creaQry, " ENCODING = ");
3388 : 94 : appendStringLiteralAH(creaQry, encoding, fout);
3389 : : }
3390 : :
3391 : 94 : appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = ");
3392 [ + + ]: 94 : if (datlocprovider[0] == 'b')
3393 : 14 : appendPQExpBufferStr(creaQry, "builtin");
3394 [ + - ]: 80 : else if (datlocprovider[0] == 'c')
3395 : 80 : appendPQExpBufferStr(creaQry, "libc");
3396 [ # # ]: 0 : else if (datlocprovider[0] == 'i')
3397 : 0 : appendPQExpBufferStr(creaQry, "icu");
3398 : : else
3399 : 0 : pg_fatal("unrecognized locale provider: %s",
3400 : : datlocprovider);
3401 : :
3402 [ + - + - ]: 94 : if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
3403 : : {
3404 : 94 : appendPQExpBufferStr(creaQry, " LOCALE = ");
3405 : 94 : appendStringLiteralAH(creaQry, collate, fout);
3406 : : }
3407 : : else
3408 : : {
3409 [ # # ]: 0 : if (strlen(collate) > 0)
3410 : : {
3411 : 0 : appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
3412 : 0 : appendStringLiteralAH(creaQry, collate, fout);
3413 : : }
3414 [ # # ]: 0 : if (strlen(ctype) > 0)
3415 : : {
3416 : 0 : appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
3417 : 0 : appendStringLiteralAH(creaQry, ctype, fout);
3418 : : }
3419 : : }
3420 [ + + ]: 94 : if (locale)
3421 : : {
3422 [ + - ]: 14 : if (datlocprovider[0] == 'b')
3423 : 14 : appendPQExpBufferStr(creaQry, " BUILTIN_LOCALE = ");
3424 : : else
3425 : 0 : appendPQExpBufferStr(creaQry, " ICU_LOCALE = ");
3426 : :
3427 : 14 : appendStringLiteralAH(creaQry, locale, fout);
3428 : : }
3429 : :
3430 [ - + ]: 94 : if (icurules)
3431 : : {
3432 : 0 : appendPQExpBufferStr(creaQry, " ICU_RULES = ");
3433 : 0 : appendStringLiteralAH(creaQry, icurules, fout);
3434 : : }
3435 : :
3436 : : /*
3437 : : * For binary upgrade, carry over the collation version. For normal
3438 : : * dump/restore, omit the version, so that it is computed upon restore.
3439 : : */
3440 [ + + ]: 94 : if (dopt->binary_upgrade)
3441 : : {
3442 [ + - ]: 41 : if (!PQgetisnull(res, 0, i_datcollversion))
3443 : : {
3444 : 41 : appendPQExpBufferStr(creaQry, " COLLATION_VERSION = ");
3445 : 41 : appendStringLiteralAH(creaQry,
3446 : : PQgetvalue(res, 0, i_datcollversion),
3447 : : fout);
3448 : : }
3449 : : }
3450 : :
3451 : : /*
3452 : : * Note: looking at dopt->outputNoTablespaces here is completely the wrong
3453 : : * thing; the decision whether to specify a tablespace should be left till
3454 : : * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
3455 : : * label the DATABASE entry with the tablespace and let the normal
3456 : : * tablespace selection logic work ... but CREATE DATABASE doesn't pay
3457 : : * attention to default_tablespace, so that won't work.
3458 : : */
3459 [ + - + + ]: 94 : if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
3460 [ + - ]: 5 : !dopt->outputNoTablespaces)
3461 : 5 : appendPQExpBuffer(creaQry, " TABLESPACE = %s",
3462 : : fmtId(tablespace));
3463 : 94 : appendPQExpBufferStr(creaQry, ";\n");
3464 : :
3465 : 94 : appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
3466 : : qdatname);
3467 : :
3468 : 94 : dbDumpId = createDumpId();
3469 : :
3470 : 94 : ArchiveEntry(fout,
3471 : : dbCatId, /* catalog ID */
3472 : : dbDumpId, /* dump ID */
3473 : 94 : ARCHIVE_OPTS(.tag = datname,
3474 : : .owner = dba,
3475 : : .description = "DATABASE",
3476 : : .section = SECTION_PRE_DATA,
3477 : : .createStmt = creaQry->data,
3478 : : .dropStmt = delQry->data));
3479 : :
3480 : : /* Compute correct tag for archive entry */
3481 : 94 : appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
3482 : :
3483 : : /* Dump DB comment if any */
3484 : : {
3485 : : /*
3486 : : * 8.2 and up keep comments on shared objects in a shared table, so we
3487 : : * cannot use the dumpComment() code used for other database objects.
3488 : : * Be careful that the ArchiveEntry parameters match that function.
3489 : : */
3490 : 94 : char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
3491 : :
3492 [ + - + + : 94 : if (comment && *comment && !dopt->no_comments)
+ - ]
3493 : : {
3494 : 49 : resetPQExpBuffer(dbQry);
3495 : :
3496 : : /*
3497 : : * Generates warning when loaded into a differently-named
3498 : : * database.
3499 : : */
3500 : 49 : appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
3501 : 49 : appendStringLiteralAH(dbQry, comment, fout);
3502 : 49 : appendPQExpBufferStr(dbQry, ";\n");
3503 : :
3504 : 49 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3505 : 49 : ARCHIVE_OPTS(.tag = labelq->data,
3506 : : .owner = dba,
3507 : : .description = "COMMENT",
3508 : : .section = SECTION_NONE,
3509 : : .createStmt = dbQry->data,
3510 : : .deps = &dbDumpId,
3511 : : .nDeps = 1));
3512 : : }
3513 : : }
3514 : :
3515 : : /* Dump DB security label, if enabled */
3516 [ + - ]: 94 : if (!dopt->no_security_labels)
3517 : : {
3518 : : PGresult *shres;
3519 : : PQExpBuffer seclabelQry;
3520 : :
3521 : 94 : seclabelQry = createPQExpBuffer();
3522 : :
3523 : 94 : buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
3524 : 94 : shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
3525 : 94 : resetPQExpBuffer(seclabelQry);
3526 : 94 : emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
3527 [ - + ]: 94 : if (seclabelQry->len > 0)
3528 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3529 : 0 : ARCHIVE_OPTS(.tag = labelq->data,
3530 : : .owner = dba,
3531 : : .description = "SECURITY LABEL",
3532 : : .section = SECTION_NONE,
3533 : : .createStmt = seclabelQry->data,
3534 : : .deps = &dbDumpId,
3535 : : .nDeps = 1));
3536 : 94 : destroyPQExpBuffer(seclabelQry);
3537 : 94 : PQclear(shres);
3538 : : }
3539 : :
3540 : : /*
3541 : : * Dump ACL if any. Note that we do not support initial privileges
3542 : : * (pg_init_privs) on databases.
3543 : : */
3544 : 94 : dbdacl.privtype = 0;
3545 : 94 : dbdacl.initprivs = NULL;
3546 : :
3547 : 94 : dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE",
3548 : : qdatname, NULL, NULL,
3549 : : NULL, dba, &dbdacl);
3550 : :
3551 : : /*
3552 : : * Now construct a DATABASE PROPERTIES archive entry to restore any
3553 : : * non-default database-level properties. (The reason this must be
3554 : : * separate is that we cannot put any additional commands into the TOC
3555 : : * entry that has CREATE DATABASE. pg_restore would execute such a group
3556 : : * in an implicit transaction block, and the backend won't allow CREATE
3557 : : * DATABASE in that context.)
3558 : : */
3559 : 94 : resetPQExpBuffer(creaQry);
3560 : 94 : resetPQExpBuffer(delQry);
3561 : :
3562 [ + - - + ]: 94 : if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
3563 : 0 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
3564 : : qdatname, datconnlimit);
3565 : :
3566 [ + + ]: 94 : if (strcmp(datistemplate, "t") == 0)
3567 : : {
3568 : 13 : appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
3569 : : qdatname);
3570 : :
3571 : : /*
3572 : : * The backend won't accept DROP DATABASE on a template database. We
3573 : : * can deal with that by removing the template marking before the DROP
3574 : : * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
3575 : : * since no such command is currently supported, fake it with a direct
3576 : : * UPDATE on pg_database.
3577 : : */
3578 : 13 : appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
3579 : : "SET datistemplate = false WHERE datname = ");
3580 : 13 : appendStringLiteralAH(delQry, datname, fout);
3581 : 13 : appendPQExpBufferStr(delQry, ";\n");
3582 : : }
3583 : :
3584 : : /*
3585 : : * We do not restore pg_database.dathasloginevt because it is set
3586 : : * automatically on login event trigger creation.
3587 : : */
3588 : :
3589 : : /* Add database-specific SET options */
3590 : 94 : dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
3591 : :
3592 : : /*
3593 : : * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
3594 : : * entry, too, for lack of a better place.
3595 : : */
3596 [ + + ]: 94 : if (dopt->binary_upgrade)
3597 : : {
3598 : 41 : appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
3599 : 41 : appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
3600 : : "SET datfrozenxid = '%u', datminmxid = '%u'\n"
3601 : : "WHERE datname = ",
3602 : : frozenxid, minmxid);
3603 : 41 : appendStringLiteralAH(creaQry, datname, fout);
3604 : 41 : appendPQExpBufferStr(creaQry, ";\n");
3605 : : }
3606 : :
3607 [ + + ]: 94 : if (creaQry->len > 0)
3608 : 45 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3609 : 45 : ARCHIVE_OPTS(.tag = datname,
3610 : : .owner = dba,
3611 : : .description = "DATABASE PROPERTIES",
3612 : : .section = SECTION_PRE_DATA,
3613 : : .createStmt = creaQry->data,
3614 : : .dropStmt = delQry->data,
3615 : : .deps = &dbDumpId));
3616 : :
3617 : : /*
3618 : : * pg_largeobject comes from the old system intact, so set its
3619 : : * relfrozenxids, relminmxids and relfilenode.
3620 : : *
3621 : : * pg_largeobject_metadata also comes from the old system intact for
3622 : : * upgrades from v16 and newer, so set its relfrozenxids, relminmxids, and
3623 : : * relfilenode, too. pg_upgrade can't copy/link the files from older
3624 : : * versions because aclitem (needed by pg_largeobject_metadata.lomacl)
3625 : : * changed its storage format in v16.
3626 : : */
3627 [ + + ]: 94 : if (dopt->binary_upgrade)
3628 : : {
3629 : : PGresult *lo_res;
3630 : 41 : PQExpBuffer loFrozenQry = createPQExpBuffer();
3631 : 41 : PQExpBuffer loOutQry = createPQExpBuffer();
3632 : 41 : PQExpBuffer lomOutQry = createPQExpBuffer();
3633 : 41 : PQExpBuffer loHorizonQry = createPQExpBuffer();
3634 : 41 : PQExpBuffer lomHorizonQry = createPQExpBuffer();
3635 : : int ii_relfrozenxid,
3636 : : ii_relfilenode,
3637 : : ii_oid,
3638 : : ii_relminmxid;
3639 : :
3640 : 41 : appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n"
3641 : : "FROM pg_catalog.pg_class\n"
3642 : : "WHERE oid IN (%u, %u, %u, %u);\n",
3643 : : LargeObjectRelationId, LargeObjectLOidPNIndexId,
3644 : : LargeObjectMetadataRelationId, LargeObjectMetadataOidIndexId);
3645 : :
3646 : 41 : lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
3647 : :
3648 : 41 : ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
3649 : 41 : ii_relminmxid = PQfnumber(lo_res, "relminmxid");
3650 : 41 : ii_relfilenode = PQfnumber(lo_res, "relfilenode");
3651 : 41 : ii_oid = PQfnumber(lo_res, "oid");
3652 : :
3653 : 41 : appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
3654 : 41 : appendPQExpBufferStr(lomHorizonQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n");
3655 : 41 : appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n");
3656 : 41 : appendPQExpBufferStr(lomOutQry, "\n-- For binary upgrade, preserve pg_largeobject_metadata and index relfilenodes\n");
3657 [ + + ]: 205 : for (int i = 0; i < PQntuples(lo_res); ++i)
3658 : : {
3659 : : Oid oid;
3660 : : RelFileNumber relfilenumber;
3661 : : PQExpBuffer horizonQry;
3662 : : PQExpBuffer outQry;
3663 : :
3664 : 164 : oid = atooid(PQgetvalue(lo_res, i, ii_oid));
3665 : 164 : relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode));
3666 : :
3667 [ + + + + ]: 164 : if (oid == LargeObjectRelationId ||
3668 : : oid == LargeObjectLOidPNIndexId)
3669 : : {
3670 : 82 : horizonQry = loHorizonQry;
3671 : 82 : outQry = loOutQry;
3672 : : }
3673 : : else
3674 : : {
3675 : 82 : horizonQry = lomHorizonQry;
3676 : 82 : outQry = lomOutQry;
3677 : : }
3678 : :
3679 : 164 : appendPQExpBuffer(horizonQry, "UPDATE pg_catalog.pg_class\n"
3680 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
3681 : : "WHERE oid = %u;\n",
3682 : 164 : atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)),
3683 : 164 : atooid(PQgetvalue(lo_res, i, ii_relminmxid)),
3684 : 164 : atooid(PQgetvalue(lo_res, i, ii_oid)));
3685 : :
3686 [ + + + + ]: 164 : if (oid == LargeObjectRelationId ||
3687 : : oid == LargeObjectMetadataRelationId)
3688 : 82 : appendPQExpBuffer(outQry,
3689 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
3690 : : relfilenumber);
3691 [ + + + - ]: 82 : else if (oid == LargeObjectLOidPNIndexId ||
3692 : : oid == LargeObjectMetadataOidIndexId)
3693 : 82 : appendPQExpBuffer(outQry,
3694 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
3695 : : relfilenumber);
3696 : : }
3697 : :
3698 : 41 : appendPQExpBufferStr(loOutQry,
3699 : : "TRUNCATE pg_catalog.pg_largeobject;\n");
3700 : 41 : appendPQExpBufferStr(lomOutQry,
3701 : : "TRUNCATE pg_catalog.pg_largeobject_metadata;\n");
3702 : :
3703 : 41 : appendPQExpBufferStr(loOutQry, loHorizonQry->data);
3704 : 41 : appendPQExpBufferStr(lomOutQry, lomHorizonQry->data);
3705 : :
3706 : 41 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3707 : 41 : ARCHIVE_OPTS(.tag = "pg_largeobject",
3708 : : .description = "pg_largeobject",
3709 : : .section = SECTION_PRE_DATA,
3710 : : .createStmt = loOutQry->data));
3711 : :
3712 [ + - ]: 41 : if (fout->remoteVersion >= 160000)
3713 : 41 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
3714 : 41 : ARCHIVE_OPTS(.tag = "pg_largeobject_metadata",
3715 : : .description = "pg_largeobject_metadata",
3716 : : .section = SECTION_PRE_DATA,
3717 : : .createStmt = lomOutQry->data));
3718 : :
3719 : 41 : PQclear(lo_res);
3720 : :
3721 : 41 : destroyPQExpBuffer(loFrozenQry);
3722 : 41 : destroyPQExpBuffer(loHorizonQry);
3723 : 41 : destroyPQExpBuffer(lomHorizonQry);
3724 : 41 : destroyPQExpBuffer(loOutQry);
3725 : 41 : destroyPQExpBuffer(lomOutQry);
3726 : : }
3727 : :
3728 : 94 : PQclear(res);
3729 : :
3730 : 94 : pg_free(qdatname);
3731 : 94 : destroyPQExpBuffer(dbQry);
3732 : 94 : destroyPQExpBuffer(delQry);
3733 : 94 : destroyPQExpBuffer(creaQry);
3734 : 94 : destroyPQExpBuffer(labelq);
3735 : 94 : }
3736 : :
3737 : : /*
3738 : : * Collect any database-specific or role-and-database-specific SET options
3739 : : * for this database, and append them to outbuf.
3740 : : */
3741 : : static void
3742 : 94 : dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
3743 : : const char *dbname, Oid dboid)
3744 : : {
3745 : 94 : PGconn *conn = GetConnection(AH);
3746 : 94 : PQExpBuffer buf = createPQExpBuffer();
3747 : : PGresult *res;
3748 : :
3749 : : /* First collect database-specific options */
3750 : 94 : printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3751 : : "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3752 : : dboid);
3753 : :
3754 : 94 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3755 : :
3756 [ + + ]: 124 : for (int i = 0; i < PQntuples(res); i++)
3757 : 30 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 0),
3758 : : "DATABASE", dbname, NULL, NULL,
3759 : : outbuf);
3760 : :
3761 : 94 : PQclear(res);
3762 : :
3763 : : /* Now look for role-and-database-specific options */
3764 : 94 : printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3765 : : "FROM pg_db_role_setting s, pg_roles r "
3766 : : "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3767 : : dboid);
3768 : :
3769 : 94 : res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3770 : :
3771 [ - + ]: 94 : for (int i = 0; i < PQntuples(res); i++)
3772 : 0 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 1),
3773 : 0 : "ROLE", PQgetvalue(res, i, 0),
3774 : : "DATABASE", dbname,
3775 : : outbuf);
3776 : :
3777 : 94 : PQclear(res);
3778 : :
3779 : 94 : destroyPQExpBuffer(buf);
3780 : 94 : }
3781 : :
3782 : : /*
3783 : : * dumpEncoding: put the correct encoding into the archive
3784 : : */
3785 : : static void
3786 : 193 : dumpEncoding(Archive *AH)
3787 : : {
3788 : 193 : const char *encname = pg_encoding_to_char(AH->encoding);
3789 : 193 : PQExpBuffer qry = createPQExpBuffer();
3790 : :
3791 : 193 : pg_log_info("saving encoding = %s", encname);
3792 : :
3793 : 193 : appendPQExpBufferStr(qry, "SET client_encoding = ");
3794 : 193 : appendStringLiteralAH(qry, encname, AH);
3795 : 193 : appendPQExpBufferStr(qry, ";\n");
3796 : :
3797 : 193 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3798 : 193 : ARCHIVE_OPTS(.tag = "ENCODING",
3799 : : .description = "ENCODING",
3800 : : .section = SECTION_PRE_DATA,
3801 : : .createStmt = qry->data));
3802 : :
3803 : 193 : destroyPQExpBuffer(qry);
3804 : 193 : }
3805 : :
3806 : :
3807 : : /*
3808 : : * dumpStdStrings: put the correct escape string behavior into the archive
3809 : : */
3810 : : static void
3811 : 193 : dumpStdStrings(Archive *AH)
3812 : : {
3813 [ + - ]: 193 : const char *stdstrings = AH->std_strings ? "on" : "off";
3814 : 193 : PQExpBuffer qry = createPQExpBuffer();
3815 : :
3816 : 193 : pg_log_info("saving \"standard_conforming_strings = %s\"",
3817 : : stdstrings);
3818 : :
3819 : 193 : appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
3820 : : stdstrings);
3821 : :
3822 : 193 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3823 : 193 : ARCHIVE_OPTS(.tag = "STDSTRINGS",
3824 : : .description = "STDSTRINGS",
3825 : : .section = SECTION_PRE_DATA,
3826 : : .createStmt = qry->data));
3827 : :
3828 : 193 : destroyPQExpBuffer(qry);
3829 : 193 : }
3830 : :
3831 : : /*
3832 : : * dumpSearchPath: record the active search_path in the archive
3833 : : */
3834 : : static void
3835 : 193 : dumpSearchPath(Archive *AH)
3836 : : {
3837 : 193 : PQExpBuffer qry = createPQExpBuffer();
3838 : 193 : PQExpBuffer path = createPQExpBuffer();
3839 : : PGresult *res;
3840 : 193 : char **schemanames = NULL;
3841 : 193 : int nschemanames = 0;
3842 : : int i;
3843 : :
3844 : : /*
3845 : : * We use the result of current_schemas(), not the search_path GUC,
3846 : : * because that might contain wildcards such as "$user", which won't
3847 : : * necessarily have the same value during restore. Also, this way avoids
3848 : : * listing schemas that may appear in search_path but not actually exist,
3849 : : * which seems like a prudent exclusion.
3850 : : */
3851 : 193 : res = ExecuteSqlQueryForSingleRow(AH,
3852 : : "SELECT pg_catalog.current_schemas(false)");
3853 : :
3854 [ - + ]: 193 : if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames))
3855 : 0 : pg_fatal("could not parse result of current_schemas()");
3856 : :
3857 : : /*
3858 : : * We use set_config(), not a simple "SET search_path" command, because
3859 : : * the latter has less-clean behavior if the search path is empty. While
3860 : : * that's likely to get fixed at some point, it seems like a good idea to
3861 : : * be as backwards-compatible as possible in what we put into archives.
3862 : : */
3863 [ - + ]: 193 : for (i = 0; i < nschemanames; i++)
3864 : : {
3865 [ # # ]: 0 : if (i > 0)
3866 : 0 : appendPQExpBufferStr(path, ", ");
3867 : 0 : appendPQExpBufferStr(path, fmtId(schemanames[i]));
3868 : : }
3869 : :
3870 : 193 : appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', ");
3871 : 193 : appendStringLiteralAH(qry, path->data, AH);
3872 : 193 : appendPQExpBufferStr(qry, ", false);\n");
3873 : :
3874 : 193 : pg_log_info("saving \"search_path = %s\"", path->data);
3875 : :
3876 : 193 : ArchiveEntry(AH, nilCatalogId, createDumpId(),
3877 : 193 : ARCHIVE_OPTS(.tag = "SEARCHPATH",
3878 : : .description = "SEARCHPATH",
3879 : : .section = SECTION_PRE_DATA,
3880 : : .createStmt = qry->data));
3881 : :
3882 : : /* Also save it in AH->searchpath, in case we're doing plain text dump */
3883 : 193 : AH->searchpath = pg_strdup(qry->data);
3884 : :
3885 : 193 : free(schemanames);
3886 : 193 : PQclear(res);
3887 : 193 : destroyPQExpBuffer(qry);
3888 : 193 : destroyPQExpBuffer(path);
3889 : 193 : }
3890 : :
3891 : :
3892 : : /*
3893 : : * getLOs:
3894 : : * Collect schema-level data about large objects
3895 : : */
3896 : : static void
3897 : 164 : getLOs(Archive *fout)
3898 : : {
3899 : 164 : DumpOptions *dopt = fout->dopt;
3900 : 164 : PQExpBuffer loQry = createPQExpBuffer();
3901 : : PGresult *res;
3902 : : int ntups;
3903 : : int i;
3904 : : int n;
3905 : : int i_oid;
3906 : : int i_lomowner;
3907 : : int i_lomacl;
3908 : : int i_acldefault;
3909 : :
3910 : 164 : pg_log_info("reading large objects");
3911 : :
3912 : : /*
3913 : : * Fetch LO OIDs and owner/ACL data. Order the data so that all the blobs
3914 : : * with the same owner/ACL appear together.
3915 : : */
3916 : 164 : appendPQExpBufferStr(loQry,
3917 : : "SELECT oid, lomowner, lomacl, "
3918 : : "acldefault('L', lomowner) AS acldefault "
3919 : : "FROM pg_largeobject_metadata ");
3920 : :
3921 : : /*
3922 : : * For binary upgrades, we transfer pg_largeobject_metadata via COPY or by
3923 : : * copying/linking its files from the old cluster. On such upgrades, we
3924 : : * only need to consider large objects that have comments or security
3925 : : * labels, since we still restore those objects via COMMENT/SECURITY LABEL
3926 : : * commands.
3927 : : */
3928 [ + + ]: 164 : if (dopt->binary_upgrade)
3929 : 42 : appendPQExpBufferStr(loQry,
3930 : : "WHERE oid IN "
3931 : : "(SELECT objoid FROM pg_description "
3932 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) " "
3933 : : "UNION SELECT objoid FROM pg_seclabel "
3934 : : "WHERE classoid = " CppAsString2(LargeObjectRelationId) ") ");
3935 : :
3936 : 164 : appendPQExpBufferStr(loQry,
3937 : : "ORDER BY lomowner, lomacl::pg_catalog.text, oid");
3938 : :
3939 : 164 : res = ExecuteSqlQuery(fout, loQry->data, PGRES_TUPLES_OK);
3940 : :
3941 : 164 : i_oid = PQfnumber(res, "oid");
3942 : 164 : i_lomowner = PQfnumber(res, "lomowner");
3943 : 164 : i_lomacl = PQfnumber(res, "lomacl");
3944 : 164 : i_acldefault = PQfnumber(res, "acldefault");
3945 : :
3946 : 164 : ntups = PQntuples(res);
3947 : :
3948 : : /*
3949 : : * Group the blobs into suitably-sized groups that have the same owner and
3950 : : * ACL setting, and build a metadata and a data DumpableObject for each
3951 : : * group. (If we supported initprivs for blobs, we'd have to insist that
3952 : : * groups also share initprivs settings, since the DumpableObject only has
3953 : : * room for one.) i is the index of the first tuple in the current group,
3954 : : * and n is the number of tuples we include in the group.
3955 : : */
3956 [ + + ]: 252 : for (i = 0; i < ntups; i += n)
3957 : : {
3958 : 88 : Oid thisoid = atooid(PQgetvalue(res, i, i_oid));
3959 : 88 : char *thisowner = PQgetvalue(res, i, i_lomowner);
3960 : 88 : char *thisacl = PQgetvalue(res, i, i_lomacl);
3961 : : LoInfo *loinfo;
3962 : : DumpableObject *lodata;
3963 : : char namebuf[64];
3964 : :
3965 : : /* Scan to find first tuple not to be included in group */
3966 : 88 : n = 1;
3967 [ + - + + ]: 102 : while (n < MAX_BLOBS_PER_ARCHIVE_ENTRY && i + n < ntups)
3968 : : {
3969 [ + - ]: 49 : if (strcmp(thisowner, PQgetvalue(res, i + n, i_lomowner)) != 0 ||
3970 [ + + ]: 49 : strcmp(thisacl, PQgetvalue(res, i + n, i_lomacl)) != 0)
3971 : : break;
3972 : 14 : n++;
3973 : : }
3974 : :
3975 : : /* Build the metadata DumpableObject */
3976 : 88 : loinfo = (LoInfo *) pg_malloc(offsetof(LoInfo, looids) + n * sizeof(Oid));
3977 : :
3978 : 88 : loinfo->dobj.objType = DO_LARGE_OBJECT;
3979 : 88 : loinfo->dobj.catId.tableoid = LargeObjectRelationId;
3980 : 88 : loinfo->dobj.catId.oid = thisoid;
3981 : 88 : AssignDumpId(&loinfo->dobj);
3982 : :
3983 [ + + ]: 88 : if (n > 1)
3984 : 10 : snprintf(namebuf, sizeof(namebuf), "%u..%u", thisoid,
3985 : 10 : atooid(PQgetvalue(res, i + n - 1, i_oid)));
3986 : : else
3987 : 78 : snprintf(namebuf, sizeof(namebuf), "%u", thisoid);
3988 : 88 : loinfo->dobj.name = pg_strdup(namebuf);
3989 : 88 : loinfo->dacl.acl = pg_strdup(thisacl);
3990 : 88 : loinfo->dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
3991 : 88 : loinfo->dacl.privtype = 0;
3992 : 88 : loinfo->dacl.initprivs = NULL;
3993 : 88 : loinfo->rolname = getRoleName(thisowner);
3994 : 88 : loinfo->numlos = n;
3995 : 88 : loinfo->looids[0] = thisoid;
3996 : : /* Collect OIDs of the remaining blobs in this group */
3997 [ + + ]: 102 : for (int k = 1; k < n; k++)
3998 : : {
3999 : : CatalogId extraID;
4000 : :
4001 : 14 : loinfo->looids[k] = atooid(PQgetvalue(res, i + k, i_oid));
4002 : :
4003 : : /* Make sure we can look up loinfo by any of the blobs' OIDs */
4004 : 14 : extraID.tableoid = LargeObjectRelationId;
4005 : 14 : extraID.oid = loinfo->looids[k];
4006 : 14 : recordAdditionalCatalogID(extraID, &loinfo->dobj);
4007 : : }
4008 : :
4009 : : /* LOs have data */
4010 : 88 : loinfo->dobj.components |= DUMP_COMPONENT_DATA;
4011 : :
4012 : : /* Mark whether LO group has a non-empty ACL */
4013 [ + + ]: 88 : if (!PQgetisnull(res, i, i_lomacl))
4014 : 36 : loinfo->dobj.components |= DUMP_COMPONENT_ACL;
4015 : :
4016 : : /*
4017 : : * In binary upgrade mode, pg_largeobject and pg_largeobject_metadata
4018 : : * are transferred via COPY or by copying/linking the files from the
4019 : : * old cluster. Thus, we do not need to dump LO data, definitions, or
4020 : : * ACLs.
4021 : : */
4022 [ + + ]: 88 : if (dopt->binary_upgrade)
4023 : 7 : loinfo->dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL | DUMP_COMPONENT_DEFINITION);
4024 : :
4025 : : /*
4026 : : * Create a "BLOBS" data item for the group, too. This is just a
4027 : : * placeholder for sorting; it carries no data now.
4028 : : */
4029 : 88 : lodata = pg_malloc_object(DumpableObject);
4030 : 88 : lodata->objType = DO_LARGE_OBJECT_DATA;
4031 : 88 : lodata->catId = nilCatalogId;
4032 : 88 : AssignDumpId(lodata);
4033 : 88 : lodata->name = pg_strdup(namebuf);
4034 : 88 : lodata->components |= DUMP_COMPONENT_DATA;
4035 : : /* Set up explicit dependency from data to metadata */
4036 : 88 : lodata->dependencies = pg_malloc_object(DumpId);
4037 : 88 : lodata->dependencies[0] = loinfo->dobj.dumpId;
4038 : 88 : lodata->nDeps = lodata->allocDeps = 1;
4039 : : }
4040 : :
4041 : 164 : PQclear(res);
4042 : 164 : destroyPQExpBuffer(loQry);
4043 : 164 : }
4044 : :
4045 : : /*
4046 : : * dumpLO
4047 : : *
4048 : : * dump the definition (metadata) of the given large object group
4049 : : */
4050 : : static void
4051 : 88 : dumpLO(Archive *fout, const LoInfo *loinfo)
4052 : : {
4053 : 88 : PQExpBuffer cquery = createPQExpBuffer();
4054 : :
4055 : : /*
4056 : : * The "definition" is just a newline-separated list of OIDs. We need to
4057 : : * put something into the dropStmt too, but it can just be a comment.
4058 : : */
4059 [ + + ]: 190 : for (int i = 0; i < loinfo->numlos; i++)
4060 : 102 : appendPQExpBuffer(cquery, "%u\n", loinfo->looids[i]);
4061 : :
4062 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4063 : 81 : ArchiveEntry(fout, loinfo->dobj.catId, loinfo->dobj.dumpId,
4064 : 81 : ARCHIVE_OPTS(.tag = loinfo->dobj.name,
4065 : : .owner = loinfo->rolname,
4066 : : .description = "BLOB METADATA",
4067 : : .section = SECTION_DATA,
4068 : : .createStmt = cquery->data,
4069 : : .dropStmt = "-- dummy"));
4070 : :
4071 : : /*
4072 : : * Dump per-blob comments and seclabels if any. We assume these are rare
4073 : : * enough that it's okay to generate retail TOC entries for them.
4074 : : */
4075 [ + + ]: 88 : if (loinfo->dobj.dump & (DUMP_COMPONENT_COMMENT |
4076 : : DUMP_COMPONENT_SECLABEL))
4077 : : {
4078 [ + + ]: 106 : for (int i = 0; i < loinfo->numlos; i++)
4079 : : {
4080 : : CatalogId catId;
4081 : : char namebuf[32];
4082 : :
4083 : : /* Build identifying info for this blob */
4084 : 60 : catId.tableoid = loinfo->dobj.catId.tableoid;
4085 : 60 : catId.oid = loinfo->looids[i];
4086 : 60 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[i]);
4087 : :
4088 [ + - ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4089 : 60 : dumpComment(fout, "LARGE OBJECT", namebuf,
4090 : 60 : NULL, loinfo->rolname,
4091 : 60 : catId, 0, loinfo->dobj.dumpId);
4092 : :
4093 [ + + ]: 60 : if (loinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4094 : 10 : dumpSecLabel(fout, "LARGE OBJECT", namebuf,
4095 : 10 : NULL, loinfo->rolname,
4096 : 10 : catId, 0, loinfo->dobj.dumpId);
4097 : : }
4098 : : }
4099 : :
4100 : : /*
4101 : : * Dump the ACLs if any (remember that all blobs in the group will have
4102 : : * the same ACL). If there's just one blob, dump a simple ACL entry; if
4103 : : * there's more, make a "LARGE OBJECTS" entry that really contains only
4104 : : * the ACL for the first blob. _printTocEntry() will be cued by the tag
4105 : : * string to emit a mutated version for each blob.
4106 : : */
4107 [ + + ]: 88 : if (loinfo->dobj.dump & DUMP_COMPONENT_ACL)
4108 : : {
4109 : : char namebuf[32];
4110 : :
4111 : : /* Build identifying info for the first blob */
4112 : 35 : snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[0]);
4113 : :
4114 [ - + ]: 35 : if (loinfo->numlos > 1)
4115 : : {
4116 : : char tagbuf[64];
4117 : :
4118 : 0 : snprintf(tagbuf, sizeof(tagbuf), "LARGE OBJECTS %u..%u",
4119 : 0 : loinfo->looids[0], loinfo->looids[loinfo->numlos - 1]);
4120 : :
4121 : 0 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4122 : : "LARGE OBJECT", namebuf, NULL, NULL,
4123 : 0 : tagbuf, loinfo->rolname, &loinfo->dacl);
4124 : : }
4125 : : else
4126 : : {
4127 : 35 : dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId,
4128 : : "LARGE OBJECT", namebuf, NULL, NULL,
4129 : 35 : NULL, loinfo->rolname, &loinfo->dacl);
4130 : : }
4131 : : }
4132 : :
4133 : 88 : destroyPQExpBuffer(cquery);
4134 : 88 : }
4135 : :
4136 : : /*
4137 : : * dumpLOs:
4138 : : * dump the data contents of the large objects in the given group
4139 : : */
4140 : : static int
4141 : 77 : dumpLOs(Archive *fout, const void *arg)
4142 : : {
4143 : 77 : const LoInfo *loinfo = (const LoInfo *) arg;
4144 : 77 : PGconn *conn = GetConnection(fout);
4145 : : char buf[LOBBUFSIZE];
4146 : :
4147 : 77 : pg_log_info("saving large objects \"%s\"", loinfo->dobj.name);
4148 : :
4149 [ + + ]: 162 : for (int i = 0; i < loinfo->numlos; i++)
4150 : : {
4151 : 85 : Oid loOid = loinfo->looids[i];
4152 : : int loFd;
4153 : : int cnt;
4154 : :
4155 : : /* Open the LO */
4156 : 85 : loFd = lo_open(conn, loOid, INV_READ);
4157 [ - + ]: 85 : if (loFd == -1)
4158 : 0 : pg_fatal("could not open large object %u: %s",
4159 : : loOid, PQerrorMessage(conn));
4160 : :
4161 : 85 : StartLO(fout, loOid);
4162 : :
4163 : : /* Now read it in chunks, sending data to archive */
4164 : : do
4165 : : {
4166 : 133 : cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
4167 [ - + ]: 133 : if (cnt < 0)
4168 : 0 : pg_fatal("error reading large object %u: %s",
4169 : : loOid, PQerrorMessage(conn));
4170 : :
4171 : 133 : WriteData(fout, buf, cnt);
4172 [ + + ]: 133 : } while (cnt > 0);
4173 : :
4174 : 85 : lo_close(conn, loFd);
4175 : :
4176 : 85 : EndLO(fout, loOid);
4177 : : }
4178 : :
4179 : 77 : return 1;
4180 : : }
4181 : :
4182 : : /*
4183 : : * getPolicies
4184 : : * get information about all RLS policies on dumpable tables.
4185 : : */
4186 : : void
4187 : 193 : getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
4188 : : {
4189 : 193 : DumpOptions *dopt = fout->dopt;
4190 : : PQExpBuffer query;
4191 : : PQExpBuffer tbloids;
4192 : : PGresult *res;
4193 : : PolicyInfo *polinfo;
4194 : : int i_oid;
4195 : : int i_tableoid;
4196 : : int i_polrelid;
4197 : : int i_polname;
4198 : : int i_polcmd;
4199 : : int i_polpermissive;
4200 : : int i_polroles;
4201 : : int i_polqual;
4202 : : int i_polwithcheck;
4203 : : int i,
4204 : : j,
4205 : : ntups;
4206 : :
4207 : : /* Skip if --no-policies was specified */
4208 [ + + ]: 193 : if (dopt->no_policies)
4209 : 1 : return;
4210 : :
4211 : 192 : query = createPQExpBuffer();
4212 : 192 : tbloids = createPQExpBuffer();
4213 : :
4214 : : /*
4215 : : * Identify tables of interest, and check which ones have RLS enabled.
4216 : : */
4217 : 192 : appendPQExpBufferChar(tbloids, '{');
4218 [ + + ]: 55376 : for (i = 0; i < numTables; i++)
4219 : : {
4220 : 55184 : TableInfo *tbinfo = &tblinfo[i];
4221 : :
4222 : : /* Ignore row security on tables not to be dumped */
4223 [ + + ]: 55184 : if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
4224 : 47567 : continue;
4225 : :
4226 : : /* It can't have RLS or policies if it's not a table */
4227 [ + + ]: 7617 : if (tbinfo->relkind != RELKIND_RELATION &&
4228 [ + + ]: 2172 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
4229 : 1540 : continue;
4230 : :
4231 : : /* Add it to the list of table OIDs to be probed below */
4232 [ + + ]: 6077 : if (tbloids->len > 1) /* do we have more than the '{'? */
4233 : 5952 : appendPQExpBufferChar(tbloids, ',');
4234 : 6077 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
4235 : :
4236 : : /* Is RLS enabled? (That's separate from whether it has policies) */
4237 [ + + ]: 6077 : if (tbinfo->rowsec)
4238 : : {
4239 : 66 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4240 : :
4241 : : /*
4242 : : * We represent RLS being enabled on a table by creating a
4243 : : * PolicyInfo object with null polname.
4244 : : *
4245 : : * Note: use tableoid 0 so that this object won't be mistaken for
4246 : : * something that pg_depend entries apply to.
4247 : : */
4248 : 66 : polinfo = pg_malloc_object(PolicyInfo);
4249 : 66 : polinfo->dobj.objType = DO_POLICY;
4250 : 66 : polinfo->dobj.catId.tableoid = 0;
4251 : 66 : polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
4252 : 66 : AssignDumpId(&polinfo->dobj);
4253 : 66 : polinfo->dobj.namespace = tbinfo->dobj.namespace;
4254 : 66 : polinfo->dobj.name = pg_strdup(tbinfo->dobj.name);
4255 : 66 : polinfo->poltable = tbinfo;
4256 : 66 : polinfo->polname = NULL;
4257 : 66 : polinfo->polcmd = '\0';
4258 : 66 : polinfo->polpermissive = 0;
4259 : 66 : polinfo->polroles = NULL;
4260 : 66 : polinfo->polqual = NULL;
4261 : 66 : polinfo->polwithcheck = NULL;
4262 : : }
4263 : : }
4264 : 192 : appendPQExpBufferChar(tbloids, '}');
4265 : :
4266 : : /*
4267 : : * Now, read all RLS policies belonging to the tables of interest, and
4268 : : * create PolicyInfo objects for them. (Note that we must filter the
4269 : : * results server-side not locally, because we dare not apply pg_get_expr
4270 : : * to tables we don't have lock on.)
4271 : : */
4272 : 192 : pg_log_info("reading row-level security policies");
4273 : :
4274 : 192 : printfPQExpBuffer(query,
4275 : : "SELECT pol.oid, pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
4276 : 192 : appendPQExpBufferStr(query, "pol.polpermissive, ");
4277 : 192 : appendPQExpBuffer(query,
4278 : : "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
4279 : : " 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, "
4280 : : "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
4281 : : "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
4282 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
4283 : : "JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)",
4284 : : tbloids->data);
4285 : :
4286 : 192 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4287 : :
4288 : 192 : ntups = PQntuples(res);
4289 [ + + ]: 192 : if (ntups > 0)
4290 : : {
4291 : 46 : i_oid = PQfnumber(res, "oid");
4292 : 46 : i_tableoid = PQfnumber(res, "tableoid");
4293 : 46 : i_polrelid = PQfnumber(res, "polrelid");
4294 : 46 : i_polname = PQfnumber(res, "polname");
4295 : 46 : i_polcmd = PQfnumber(res, "polcmd");
4296 : 46 : i_polpermissive = PQfnumber(res, "polpermissive");
4297 : 46 : i_polroles = PQfnumber(res, "polroles");
4298 : 46 : i_polqual = PQfnumber(res, "polqual");
4299 : 46 : i_polwithcheck = PQfnumber(res, "polwithcheck");
4300 : :
4301 : 46 : polinfo = pg_malloc_array(PolicyInfo, ntups);
4302 : :
4303 [ + + ]: 337 : for (j = 0; j < ntups; j++)
4304 : : {
4305 : 291 : Oid polrelid = atooid(PQgetvalue(res, j, i_polrelid));
4306 : 291 : TableInfo *tbinfo = findTableByOid(polrelid);
4307 : :
4308 : 291 : tbinfo->dobj.components |= DUMP_COMPONENT_POLICY;
4309 : :
4310 : 291 : polinfo[j].dobj.objType = DO_POLICY;
4311 : 291 : polinfo[j].dobj.catId.tableoid =
4312 : 291 : atooid(PQgetvalue(res, j, i_tableoid));
4313 : 291 : polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
4314 : 291 : AssignDumpId(&polinfo[j].dobj);
4315 : 291 : polinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4316 : 291 : polinfo[j].poltable = tbinfo;
4317 : 291 : polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname));
4318 : 291 : polinfo[j].dobj.name = pg_strdup(polinfo[j].polname);
4319 : :
4320 : 291 : polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd));
4321 : 291 : polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't';
4322 : :
4323 [ + + ]: 291 : if (PQgetisnull(res, j, i_polroles))
4324 : 127 : polinfo[j].polroles = NULL;
4325 : : else
4326 : 164 : polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles));
4327 : :
4328 [ + + ]: 291 : if (PQgetisnull(res, j, i_polqual))
4329 : 41 : polinfo[j].polqual = NULL;
4330 : : else
4331 : 250 : polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual));
4332 : :
4333 [ + + ]: 291 : if (PQgetisnull(res, j, i_polwithcheck))
4334 : 153 : polinfo[j].polwithcheck = NULL;
4335 : : else
4336 : 138 : polinfo[j].polwithcheck
4337 : 138 : = pg_strdup(PQgetvalue(res, j, i_polwithcheck));
4338 : : }
4339 : : }
4340 : :
4341 : 192 : PQclear(res);
4342 : :
4343 : 192 : destroyPQExpBuffer(query);
4344 : 192 : destroyPQExpBuffer(tbloids);
4345 : : }
4346 : :
4347 : : /*
4348 : : * dumpPolicy
4349 : : * dump the definition of the given policy
4350 : : */
4351 : : static void
4352 : 357 : dumpPolicy(Archive *fout, const PolicyInfo *polinfo)
4353 : : {
4354 : 357 : DumpOptions *dopt = fout->dopt;
4355 : 357 : TableInfo *tbinfo = polinfo->poltable;
4356 : : PQExpBuffer query;
4357 : : PQExpBuffer delqry;
4358 : : PQExpBuffer polprefix;
4359 : : char *qtabname;
4360 : : const char *cmd;
4361 : : char *tag;
4362 : :
4363 : : /* Do nothing if not dumping schema */
4364 [ + + ]: 357 : if (!dopt->dumpSchema)
4365 : 56 : return;
4366 : :
4367 : : /*
4368 : : * If polname is NULL, then this record is just indicating that ROW LEVEL
4369 : : * SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE
4370 : : * ROW LEVEL SECURITY.
4371 : : */
4372 [ + + ]: 301 : if (polinfo->polname == NULL)
4373 : : {
4374 : 58 : query = createPQExpBuffer();
4375 : :
4376 : 58 : appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;",
4377 : 58 : fmtQualifiedDumpable(tbinfo));
4378 : :
4379 : : /*
4380 : : * We must emit the ROW SECURITY object's dependency on its table
4381 : : * explicitly, because it will not match anything in pg_depend (unlike
4382 : : * the case for other PolicyInfo objects).
4383 : : */
4384 [ + - ]: 58 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4385 : 58 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4386 : 58 : ARCHIVE_OPTS(.tag = polinfo->dobj.name,
4387 : : .namespace = polinfo->dobj.namespace->dobj.name,
4388 : : .owner = tbinfo->rolname,
4389 : : .description = "ROW SECURITY",
4390 : : .section = SECTION_POST_DATA,
4391 : : .createStmt = query->data,
4392 : : .deps = &(tbinfo->dobj.dumpId),
4393 : : .nDeps = 1));
4394 : :
4395 : 58 : destroyPQExpBuffer(query);
4396 : 58 : return;
4397 : : }
4398 : :
4399 [ + + ]: 243 : if (polinfo->polcmd == '*')
4400 : 81 : cmd = "";
4401 [ + + ]: 162 : else if (polinfo->polcmd == 'r')
4402 : 43 : cmd = " FOR SELECT";
4403 [ + + ]: 119 : else if (polinfo->polcmd == 'a')
4404 : 33 : cmd = " FOR INSERT";
4405 [ + + ]: 86 : else if (polinfo->polcmd == 'w')
4406 : 43 : cmd = " FOR UPDATE";
4407 [ + - ]: 43 : else if (polinfo->polcmd == 'd')
4408 : 43 : cmd = " FOR DELETE";
4409 : : else
4410 : 0 : pg_fatal("unexpected policy command type: %c",
4411 : : polinfo->polcmd);
4412 : :
4413 : 243 : query = createPQExpBuffer();
4414 : 243 : delqry = createPQExpBuffer();
4415 : 243 : polprefix = createPQExpBuffer();
4416 : :
4417 : 243 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
4418 : :
4419 : 243 : appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname));
4420 : :
4421 : 243 : appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo),
4422 [ + + ]: 243 : !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd);
4423 : :
4424 [ + + ]: 243 : if (polinfo->polroles != NULL)
4425 : 132 : appendPQExpBuffer(query, " TO %s", polinfo->polroles);
4426 : :
4427 [ + + ]: 243 : if (polinfo->polqual != NULL)
4428 : 210 : appendPQExpBuffer(query, " USING (%s)", polinfo->polqual);
4429 : :
4430 [ + + ]: 243 : if (polinfo->polwithcheck != NULL)
4431 : 114 : appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck);
4432 : :
4433 : 243 : appendPQExpBufferStr(query, ";\n");
4434 : :
4435 : 243 : appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname));
4436 : 243 : appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo));
4437 : :
4438 : 243 : appendPQExpBuffer(polprefix, "POLICY %s ON",
4439 : 243 : fmtId(polinfo->polname));
4440 : :
4441 : 243 : tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
4442 : :
4443 [ + - ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4444 : 243 : ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
4445 : 243 : ARCHIVE_OPTS(.tag = tag,
4446 : : .namespace = polinfo->dobj.namespace->dobj.name,
4447 : : .owner = tbinfo->rolname,
4448 : : .description = "POLICY",
4449 : : .section = SECTION_POST_DATA,
4450 : : .createStmt = query->data,
4451 : : .dropStmt = delqry->data));
4452 : :
4453 [ + + ]: 243 : if (polinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4454 : 33 : dumpComment(fout, polprefix->data, qtabname,
4455 : 33 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
4456 : 33 : polinfo->dobj.catId, 0, polinfo->dobj.dumpId);
4457 : :
4458 : 243 : pfree(tag);
4459 : 243 : destroyPQExpBuffer(query);
4460 : 243 : destroyPQExpBuffer(delqry);
4461 : 243 : destroyPQExpBuffer(polprefix);
4462 : 243 : pg_free(qtabname);
4463 : : }
4464 : :
4465 : : /*
4466 : : * getPublications
4467 : : * get information about publications
4468 : : */
4469 : : void
4470 : 193 : getPublications(Archive *fout)
4471 : : {
4472 : 193 : DumpOptions *dopt = fout->dopt;
4473 : : PQExpBuffer query;
4474 : : PGresult *res;
4475 : : PublicationInfo *pubinfo;
4476 : : int i_tableoid;
4477 : : int i_oid;
4478 : : int i_pubname;
4479 : : int i_pubowner;
4480 : : int i_puballtables;
4481 : : int i_puballsequences;
4482 : : int i_pubinsert;
4483 : : int i_pubupdate;
4484 : : int i_pubdelete;
4485 : : int i_pubtruncate;
4486 : : int i_pubviaroot;
4487 : : int i_pubgencols;
4488 : : int i,
4489 : : ntups;
4490 : :
4491 [ - + ]: 193 : if (dopt->no_publications)
4492 : 0 : return;
4493 : :
4494 : 193 : query = createPQExpBuffer();
4495 : :
4496 : : /* Get the publications. */
4497 : 193 : appendPQExpBufferStr(query, "SELECT p.tableoid, p.oid, p.pubname, "
4498 : : "p.pubowner, p.puballtables, p.pubinsert, "
4499 : : "p.pubupdate, p.pubdelete, ");
4500 : :
4501 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
4502 : 193 : appendPQExpBufferStr(query, "p.pubtruncate, ");
4503 : : else
4504 : 0 : appendPQExpBufferStr(query, "false AS pubtruncate, ");
4505 : :
4506 [ + - ]: 193 : if (fout->remoteVersion >= 130000)
4507 : 193 : appendPQExpBufferStr(query, "p.pubviaroot, ");
4508 : : else
4509 : 0 : appendPQExpBufferStr(query, "false AS pubviaroot, ");
4510 : :
4511 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
4512 : 193 : appendPQExpBufferStr(query, "p.pubgencols, ");
4513 : : else
4514 : 0 : appendPQExpBuffer(query, "'%c' AS pubgencols, ", PUBLISH_GENCOLS_NONE);
4515 : :
4516 [ + - ]: 193 : if (fout->remoteVersion >= 190000)
4517 : 193 : appendPQExpBufferStr(query, "p.puballsequences ");
4518 : : else
4519 : 0 : appendPQExpBufferStr(query, "false AS puballsequences ");
4520 : :
4521 : 193 : appendPQExpBufferStr(query, "FROM pg_publication p");
4522 : :
4523 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4524 : :
4525 : 193 : ntups = PQntuples(res);
4526 : :
4527 [ + + ]: 193 : if (ntups == 0)
4528 : 137 : goto cleanup;
4529 : :
4530 : 56 : i_tableoid = PQfnumber(res, "tableoid");
4531 : 56 : i_oid = PQfnumber(res, "oid");
4532 : 56 : i_pubname = PQfnumber(res, "pubname");
4533 : 56 : i_pubowner = PQfnumber(res, "pubowner");
4534 : 56 : i_puballtables = PQfnumber(res, "puballtables");
4535 : 56 : i_puballsequences = PQfnumber(res, "puballsequences");
4536 : 56 : i_pubinsert = PQfnumber(res, "pubinsert");
4537 : 56 : i_pubupdate = PQfnumber(res, "pubupdate");
4538 : 56 : i_pubdelete = PQfnumber(res, "pubdelete");
4539 : 56 : i_pubtruncate = PQfnumber(res, "pubtruncate");
4540 : 56 : i_pubviaroot = PQfnumber(res, "pubviaroot");
4541 : 56 : i_pubgencols = PQfnumber(res, "pubgencols");
4542 : :
4543 : 56 : pubinfo = pg_malloc_array(PublicationInfo, ntups);
4544 : :
4545 [ + + ]: 572 : for (i = 0; i < ntups; i++)
4546 : : {
4547 : 516 : pubinfo[i].dobj.objType = DO_PUBLICATION;
4548 : 516 : pubinfo[i].dobj.catId.tableoid =
4549 : 516 : atooid(PQgetvalue(res, i, i_tableoid));
4550 : 516 : pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4551 : 516 : AssignDumpId(&pubinfo[i].dobj);
4552 : 516 : pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname));
4553 : 516 : pubinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_pubowner));
4554 : 516 : pubinfo[i].puballtables =
4555 : 516 : (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0);
4556 : 516 : pubinfo[i].puballsequences =
4557 : 516 : (strcmp(PQgetvalue(res, i, i_puballsequences), "t") == 0);
4558 : 516 : pubinfo[i].pubinsert =
4559 : 516 : (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0);
4560 : 516 : pubinfo[i].pubupdate =
4561 : 516 : (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0);
4562 : 516 : pubinfo[i].pubdelete =
4563 : 516 : (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
4564 : 516 : pubinfo[i].pubtruncate =
4565 : 516 : (strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0);
4566 : 516 : pubinfo[i].pubviaroot =
4567 : 516 : (strcmp(PQgetvalue(res, i, i_pubviaroot), "t") == 0);
4568 : 516 : pubinfo[i].pubgencols_type =
4569 : 516 : *(PQgetvalue(res, i, i_pubgencols));
4570 : 516 : pubinfo[i].except_tables = (SimplePtrList)
4571 : : {
4572 : : NULL, NULL
4573 : : };
4574 : :
4575 : : /* Decide whether we want to dump it */
4576 : 516 : selectDumpableObject(&(pubinfo[i].dobj), fout);
4577 : :
4578 : : /*
4579 : : * Get the list of tables for publications specified in the EXCEPT
4580 : : * TABLE clause.
4581 : : *
4582 : : * Although individual table entries in EXCEPT list could be stored in
4583 : : * PublicationRelInfo, dumpPublicationTable cannot be used to emit
4584 : : * them, because there is no ALTER PUBLICATION ... ADD command to add
4585 : : * individual table entries to the EXCEPT list.
4586 : : *
4587 : : * Therefore, the approach is to dump the complete EXCEPT list in a
4588 : : * single CREATE PUBLICATION statement. PublicationInfo is used to
4589 : : * collect this information, which is then emitted by
4590 : : * dumpPublication().
4591 : : */
4592 [ + - ]: 516 : if (fout->remoteVersion >= 190000)
4593 : : {
4594 : : int ntbls;
4595 : : PGresult *res_tbls;
4596 : :
4597 : 516 : resetPQExpBuffer(query);
4598 : 516 : appendPQExpBuffer(query,
4599 : : "SELECT prrelid\n"
4600 : : "FROM pg_catalog.pg_publication_rel\n"
4601 : : "WHERE prpubid = %u AND prexcept",
4602 : 516 : pubinfo[i].dobj.catId.oid);
4603 : :
4604 : 516 : res_tbls = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4605 : :
4606 : 516 : ntbls = PQntuples(res_tbls);
4607 : :
4608 [ + + ]: 756 : for (int j = 0; j < ntbls; j++)
4609 : : {
4610 : : Oid prrelid;
4611 : : TableInfo *tbinfo;
4612 : :
4613 : 240 : prrelid = atooid(PQgetvalue(res_tbls, j, 0));
4614 : :
4615 : 240 : tbinfo = findTableByOid(prrelid);
4616 : :
4617 [ + - ]: 240 : if (tbinfo != NULL)
4618 : 240 : simple_ptr_list_append(&pubinfo[i].except_tables, tbinfo);
4619 : : }
4620 : :
4621 : 516 : PQclear(res_tbls);
4622 : : }
4623 : : }
4624 : :
4625 : 56 : cleanup:
4626 : 193 : PQclear(res);
4627 : :
4628 : 193 : destroyPQExpBuffer(query);
4629 : : }
4630 : :
4631 : : /*
4632 : : * dumpPublication
4633 : : * dump the definition of the given publication
4634 : : */
4635 : : static void
4636 : 416 : dumpPublication(Archive *fout, const PublicationInfo *pubinfo)
4637 : : {
4638 : 416 : DumpOptions *dopt = fout->dopt;
4639 : : PQExpBuffer delq;
4640 : : PQExpBuffer query;
4641 : : char *qpubname;
4642 : 416 : bool first = true;
4643 : :
4644 : : /* Do nothing if not dumping schema */
4645 [ + + ]: 416 : if (!dopt->dumpSchema)
4646 : 60 : return;
4647 : :
4648 : 356 : delq = createPQExpBuffer();
4649 : 356 : query = createPQExpBuffer();
4650 : :
4651 : 356 : qpubname = pg_strdup(fmtId(pubinfo->dobj.name));
4652 : :
4653 : 356 : appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n",
4654 : : qpubname);
4655 : :
4656 : 356 : appendPQExpBuffer(query, "CREATE PUBLICATION %s",
4657 : : qpubname);
4658 : :
4659 [ + + ]: 356 : if (pubinfo->puballtables)
4660 : : {
4661 : 166 : int n_except = 0;
4662 : :
4663 : 166 : appendPQExpBufferStr(query, " FOR ALL TABLES");
4664 : :
4665 : : /* Include EXCEPT (TABLE) clause if there are except_tables. */
4666 [ + + ]: 331 : for (SimplePtrListCell *cell = pubinfo->except_tables.head; cell; cell = cell->next)
4667 : : {
4668 : 165 : TableInfo *tbinfo = (TableInfo *) cell->ptr;
4669 : :
4670 [ + + ]: 165 : if (++n_except == 1)
4671 : 99 : appendPQExpBufferStr(query, " EXCEPT (");
4672 : : else
4673 : 66 : appendPQExpBufferStr(query, ", ");
4674 : 165 : appendPQExpBuffer(query, "TABLE ONLY %s", fmtQualifiedDumpable(tbinfo));
4675 : : }
4676 [ + + ]: 166 : if (n_except > 0)
4677 : 99 : appendPQExpBufferChar(query, ')');
4678 : :
4679 [ + + ]: 166 : if (pubinfo->puballsequences)
4680 : 33 : appendPQExpBufferStr(query, ", ALL SEQUENCES");
4681 : : }
4682 [ + + ]: 190 : else if (pubinfo->puballsequences)
4683 : 33 : appendPQExpBufferStr(query, " FOR ALL SEQUENCES");
4684 : :
4685 : 356 : appendPQExpBufferStr(query, " WITH (publish = '");
4686 [ + + ]: 356 : if (pubinfo->pubinsert)
4687 : : {
4688 : 290 : appendPQExpBufferStr(query, "insert");
4689 : 290 : first = false;
4690 : : }
4691 : :
4692 [ + + ]: 356 : if (pubinfo->pubupdate)
4693 : : {
4694 [ + - ]: 290 : if (!first)
4695 : 290 : appendPQExpBufferStr(query, ", ");
4696 : :
4697 : 290 : appendPQExpBufferStr(query, "update");
4698 : 290 : first = false;
4699 : : }
4700 : :
4701 [ + + ]: 356 : if (pubinfo->pubdelete)
4702 : : {
4703 [ + - ]: 290 : if (!first)
4704 : 290 : appendPQExpBufferStr(query, ", ");
4705 : :
4706 : 290 : appendPQExpBufferStr(query, "delete");
4707 : 290 : first = false;
4708 : : }
4709 : :
4710 [ + + ]: 356 : if (pubinfo->pubtruncate)
4711 : : {
4712 [ + - ]: 290 : if (!first)
4713 : 290 : appendPQExpBufferStr(query, ", ");
4714 : :
4715 : 290 : appendPQExpBufferStr(query, "truncate");
4716 : 290 : first = false;
4717 : : }
4718 : :
4719 : 356 : appendPQExpBufferChar(query, '\'');
4720 : :
4721 [ + + ]: 356 : if (pubinfo->pubviaroot)
4722 : 5 : appendPQExpBufferStr(query, ", publish_via_partition_root = true");
4723 : :
4724 [ + + ]: 356 : if (pubinfo->pubgencols_type == PUBLISH_GENCOLS_STORED)
4725 : 33 : appendPQExpBufferStr(query, ", publish_generated_columns = stored");
4726 : :
4727 : 356 : appendPQExpBufferStr(query, ");\n");
4728 : :
4729 [ + - ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4730 : 356 : ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
4731 : 356 : ARCHIVE_OPTS(.tag = pubinfo->dobj.name,
4732 : : .owner = pubinfo->rolname,
4733 : : .description = "PUBLICATION",
4734 : : .section = SECTION_POST_DATA,
4735 : : .createStmt = query->data,
4736 : : .dropStmt = delq->data));
4737 : :
4738 [ + + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4739 : 33 : dumpComment(fout, "PUBLICATION", qpubname,
4740 : 33 : NULL, pubinfo->rolname,
4741 : 33 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4742 : :
4743 [ - + ]: 356 : if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4744 : 0 : dumpSecLabel(fout, "PUBLICATION", qpubname,
4745 : 0 : NULL, pubinfo->rolname,
4746 : 0 : pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
4747 : :
4748 : 356 : destroyPQExpBuffer(delq);
4749 : 356 : destroyPQExpBuffer(query);
4750 : 356 : pg_free(qpubname);
4751 : : }
4752 : :
4753 : : /*
4754 : : * getPublicationNamespaces
4755 : : * get information about publication membership for dumpable schemas.
4756 : : */
4757 : : void
4758 : 193 : getPublicationNamespaces(Archive *fout)
4759 : : {
4760 : : PQExpBuffer query;
4761 : : PGresult *res;
4762 : : PublicationSchemaInfo *pubsinfo;
4763 : 193 : DumpOptions *dopt = fout->dopt;
4764 : : int i_tableoid;
4765 : : int i_oid;
4766 : : int i_pnpubid;
4767 : : int i_pnnspid;
4768 : : int i,
4769 : : j,
4770 : : ntups;
4771 : :
4772 [ + - - + ]: 193 : if (dopt->no_publications || fout->remoteVersion < 150000)
4773 : 0 : return;
4774 : :
4775 : 193 : query = createPQExpBuffer();
4776 : :
4777 : : /* Collect all publication membership info. */
4778 : 193 : appendPQExpBufferStr(query,
4779 : : "SELECT tableoid, oid, pnpubid, pnnspid "
4780 : : "FROM pg_catalog.pg_publication_namespace");
4781 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4782 : :
4783 : 193 : ntups = PQntuples(res);
4784 : :
4785 : 193 : i_tableoid = PQfnumber(res, "tableoid");
4786 : 193 : i_oid = PQfnumber(res, "oid");
4787 : 193 : i_pnpubid = PQfnumber(res, "pnpubid");
4788 : 193 : i_pnnspid = PQfnumber(res, "pnnspid");
4789 : :
4790 : : /* this allocation may be more than we need */
4791 : 193 : pubsinfo = pg_malloc_array(PublicationSchemaInfo, ntups);
4792 : 193 : j = 0;
4793 : :
4794 [ + + ]: 324 : for (i = 0; i < ntups; i++)
4795 : : {
4796 : 131 : Oid pnpubid = atooid(PQgetvalue(res, i, i_pnpubid));
4797 : 131 : Oid pnnspid = atooid(PQgetvalue(res, i, i_pnnspid));
4798 : : PublicationInfo *pubinfo;
4799 : : NamespaceInfo *nspinfo;
4800 : :
4801 : : /*
4802 : : * Ignore any entries for which we aren't interested in either the
4803 : : * publication or the rel.
4804 : : */
4805 : 131 : pubinfo = findPublicationByOid(pnpubid);
4806 [ - + ]: 131 : if (pubinfo == NULL)
4807 : 0 : continue;
4808 : 131 : nspinfo = findNamespaceByOid(pnnspid);
4809 [ - + ]: 131 : if (nspinfo == NULL)
4810 : 0 : continue;
4811 : :
4812 : : /* OK, make a DumpableObject for this relationship */
4813 : 131 : pubsinfo[j].dobj.objType = DO_PUBLICATION_TABLE_IN_SCHEMA;
4814 : 131 : pubsinfo[j].dobj.catId.tableoid =
4815 : 131 : atooid(PQgetvalue(res, i, i_tableoid));
4816 : 131 : pubsinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4817 : 131 : AssignDumpId(&pubsinfo[j].dobj);
4818 : 131 : pubsinfo[j].dobj.namespace = nspinfo->dobj.namespace;
4819 : 131 : pubsinfo[j].dobj.name = nspinfo->dobj.name;
4820 : 131 : pubsinfo[j].publication = pubinfo;
4821 : 131 : pubsinfo[j].pubschema = nspinfo;
4822 : :
4823 : : /* Decide whether we want to dump it */
4824 : 131 : selectDumpablePublicationObject(&(pubsinfo[j].dobj), fout);
4825 : :
4826 : 131 : j++;
4827 : : }
4828 : :
4829 : 193 : PQclear(res);
4830 : 193 : destroyPQExpBuffer(query);
4831 : : }
4832 : :
4833 : : /*
4834 : : * getPublicationTables
4835 : : * get information about publication membership for dumpable tables.
4836 : : */
4837 : : void
4838 : 193 : getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
4839 : : {
4840 : : PQExpBuffer query;
4841 : : PGresult *res;
4842 : : PublicationRelInfo *pubrinfo;
4843 : 193 : DumpOptions *dopt = fout->dopt;
4844 : : int i_tableoid;
4845 : : int i_oid;
4846 : : int i_prpubid;
4847 : : int i_prrelid;
4848 : : int i_prrelqual;
4849 : : int i_prattrs;
4850 : : int i,
4851 : : j,
4852 : : ntups;
4853 : :
4854 [ - + ]: 193 : if (dopt->no_publications)
4855 : 0 : return;
4856 : :
4857 : 193 : query = createPQExpBuffer();
4858 : :
4859 : : /* Collect all publication membership info. */
4860 [ + - ]: 193 : if (fout->remoteVersion >= 150000)
4861 : : {
4862 : 193 : appendPQExpBufferStr(query,
4863 : : "SELECT tableoid, oid, prpubid, prrelid, "
4864 : : "pg_catalog.pg_get_expr(prqual, prrelid) AS prrelqual, "
4865 : : "(CASE\n"
4866 : : " WHEN pr.prattrs IS NOT NULL THEN\n"
4867 : : " (SELECT array_agg(attname)\n"
4868 : : " FROM\n"
4869 : : " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n"
4870 : : " pg_catalog.pg_attribute\n"
4871 : : " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n"
4872 : : " ELSE NULL END) prattrs "
4873 : : "FROM pg_catalog.pg_publication_rel pr");
4874 [ + - ]: 193 : if (fout->remoteVersion >= 190000)
4875 : 193 : appendPQExpBufferStr(query, " WHERE NOT pr.prexcept");
4876 : : }
4877 : : else
4878 : 0 : appendPQExpBufferStr(query,
4879 : : "SELECT tableoid, oid, prpubid, prrelid, "
4880 : : "NULL AS prrelqual, NULL AS prattrs "
4881 : : "FROM pg_catalog.pg_publication_rel");
4882 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4883 : :
4884 : 193 : ntups = PQntuples(res);
4885 : :
4886 : 193 : i_tableoid = PQfnumber(res, "tableoid");
4887 : 193 : i_oid = PQfnumber(res, "oid");
4888 : 193 : i_prpubid = PQfnumber(res, "prpubid");
4889 : 193 : i_prrelid = PQfnumber(res, "prrelid");
4890 : 193 : i_prrelqual = PQfnumber(res, "prrelqual");
4891 : 193 : i_prattrs = PQfnumber(res, "prattrs");
4892 : :
4893 : : /* this allocation may be more than we need */
4894 : 193 : pubrinfo = pg_malloc_array(PublicationRelInfo, ntups);
4895 : 193 : j = 0;
4896 : :
4897 [ + + ]: 564 : for (i = 0; i < ntups; i++)
4898 : : {
4899 : 371 : Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid));
4900 : 371 : Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid));
4901 : : PublicationInfo *pubinfo;
4902 : : TableInfo *tbinfo;
4903 : :
4904 : : /*
4905 : : * Ignore any entries for which we aren't interested in either the
4906 : : * publication or the rel.
4907 : : */
4908 : 371 : pubinfo = findPublicationByOid(prpubid);
4909 [ - + ]: 371 : if (pubinfo == NULL)
4910 : 0 : continue;
4911 : 371 : tbinfo = findTableByOid(prrelid);
4912 [ - + ]: 371 : if (tbinfo == NULL)
4913 : 0 : continue;
4914 : :
4915 : : /* OK, make a DumpableObject for this relationship */
4916 : 371 : pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
4917 : 371 : pubrinfo[j].dobj.catId.tableoid =
4918 : 371 : atooid(PQgetvalue(res, i, i_tableoid));
4919 : 371 : pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4920 : 371 : AssignDumpId(&pubrinfo[j].dobj);
4921 : 371 : pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
4922 : 371 : pubrinfo[j].dobj.name = tbinfo->dobj.name;
4923 : 371 : pubrinfo[j].publication = pubinfo;
4924 : 371 : pubrinfo[j].pubtable = tbinfo;
4925 [ + + ]: 371 : if (PQgetisnull(res, i, i_prrelqual))
4926 : 206 : pubrinfo[j].pubrelqual = NULL;
4927 : : else
4928 : 165 : pubrinfo[j].pubrelqual = pg_strdup(PQgetvalue(res, i, i_prrelqual));
4929 : :
4930 [ + + ]: 371 : if (!PQgetisnull(res, i, i_prattrs))
4931 : : {
4932 : : char **attnames;
4933 : : int nattnames;
4934 : : PQExpBuffer attribs;
4935 : :
4936 [ - + ]: 117 : if (!parsePGArray(PQgetvalue(res, i, i_prattrs),
4937 : : &attnames, &nattnames))
4938 : 0 : pg_fatal("could not parse %s array", "prattrs");
4939 : 117 : attribs = createPQExpBuffer();
4940 [ + + ]: 337 : for (int k = 0; k < nattnames; k++)
4941 : : {
4942 [ + + ]: 220 : if (k > 0)
4943 : 103 : appendPQExpBufferStr(attribs, ", ");
4944 : :
4945 : 220 : appendPQExpBufferStr(attribs, fmtId(attnames[k]));
4946 : : }
4947 : 117 : pubrinfo[j].pubrattrs = attribs->data;
4948 : 117 : free(attribs); /* but not attribs->data */
4949 : 117 : free(attnames);
4950 : : }
4951 : : else
4952 : 254 : pubrinfo[j].pubrattrs = NULL;
4953 : :
4954 : : /* Decide whether we want to dump it */
4955 : 371 : selectDumpablePublicationObject(&(pubrinfo[j].dobj), fout);
4956 : :
4957 : 371 : j++;
4958 : : }
4959 : :
4960 : 193 : PQclear(res);
4961 : 193 : destroyPQExpBuffer(query);
4962 : : }
4963 : :
4964 : : /*
4965 : : * dumpPublicationNamespace
4966 : : * dump the definition of the given publication schema mapping.
4967 : : */
4968 : : static void
4969 : 103 : dumpPublicationNamespace(Archive *fout, const PublicationSchemaInfo *pubsinfo)
4970 : : {
4971 : 103 : DumpOptions *dopt = fout->dopt;
4972 : 103 : NamespaceInfo *schemainfo = pubsinfo->pubschema;
4973 : 103 : PublicationInfo *pubinfo = pubsinfo->publication;
4974 : : PQExpBuffer query;
4975 : : char *tag;
4976 : :
4977 : : /* Do nothing if not dumping schema */
4978 [ + + ]: 103 : if (!dopt->dumpSchema)
4979 : 12 : return;
4980 : :
4981 : 91 : tag = psprintf("%s %s", pubinfo->dobj.name, schemainfo->dobj.name);
4982 : :
4983 : 91 : query = createPQExpBuffer();
4984 : :
4985 : 91 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ", fmtId(pubinfo->dobj.name));
4986 : 91 : appendPQExpBuffer(query, "ADD TABLES IN SCHEMA %s;\n", fmtId(schemainfo->dobj.name));
4987 : :
4988 : : /*
4989 : : * There is no point in creating drop query as the drop is done by schema
4990 : : * drop.
4991 : : */
4992 [ + - ]: 91 : if (pubsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
4993 : 91 : ArchiveEntry(fout, pubsinfo->dobj.catId, pubsinfo->dobj.dumpId,
4994 : 91 : ARCHIVE_OPTS(.tag = tag,
4995 : : .namespace = schemainfo->dobj.name,
4996 : : .owner = pubinfo->rolname,
4997 : : .description = "PUBLICATION TABLES IN SCHEMA",
4998 : : .section = SECTION_POST_DATA,
4999 : : .createStmt = query->data));
5000 : :
5001 : : /* These objects can't currently have comments or seclabels */
5002 : :
5003 : 91 : pfree(tag);
5004 : 91 : destroyPQExpBuffer(query);
5005 : : }
5006 : :
5007 : : /*
5008 : : * dumpPublicationTable
5009 : : * dump the definition of the given publication table mapping
5010 : : */
5011 : : static void
5012 : 298 : dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo)
5013 : : {
5014 : 298 : DumpOptions *dopt = fout->dopt;
5015 : 298 : PublicationInfo *pubinfo = pubrinfo->publication;
5016 : 298 : TableInfo *tbinfo = pubrinfo->pubtable;
5017 : : PQExpBuffer query;
5018 : : char *tag;
5019 : :
5020 : : /* Do nothing if not dumping schema */
5021 [ + + ]: 298 : if (!dopt->dumpSchema)
5022 : 42 : return;
5023 : :
5024 : 256 : tag = psprintf("%s %s", pubinfo->dobj.name, tbinfo->dobj.name);
5025 : :
5026 : 256 : query = createPQExpBuffer();
5027 : :
5028 : 256 : appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
5029 : 256 : fmtId(pubinfo->dobj.name));
5030 : 256 : appendPQExpBuffer(query, " %s",
5031 : 256 : fmtQualifiedDumpable(tbinfo));
5032 : :
5033 [ + + ]: 256 : if (pubrinfo->pubrattrs)
5034 : 81 : appendPQExpBuffer(query, " (%s)", pubrinfo->pubrattrs);
5035 : :
5036 [ + + ]: 256 : if (pubrinfo->pubrelqual)
5037 : : {
5038 : : /*
5039 : : * It's necessary to add parentheses around the expression because
5040 : : * pg_get_expr won't supply the parentheses for things like WHERE
5041 : : * TRUE.
5042 : : */
5043 : 114 : appendPQExpBuffer(query, " WHERE (%s)", pubrinfo->pubrelqual);
5044 : : }
5045 : 256 : appendPQExpBufferStr(query, ";\n");
5046 : :
5047 : : /*
5048 : : * There is no point in creating a drop query as the drop is done by table
5049 : : * drop. (If you think to change this, see also _printTocEntry().)
5050 : : * Although this object doesn't really have ownership as such, set the
5051 : : * owner field anyway to ensure that the command is run by the correct
5052 : : * role at restore time.
5053 : : */
5054 [ + - ]: 256 : if (pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5055 : 256 : ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
5056 : 256 : ARCHIVE_OPTS(.tag = tag,
5057 : : .namespace = tbinfo->dobj.namespace->dobj.name,
5058 : : .owner = pubinfo->rolname,
5059 : : .description = "PUBLICATION TABLE",
5060 : : .section = SECTION_POST_DATA,
5061 : : .createStmt = query->data));
5062 : :
5063 : : /* These objects can't currently have comments or seclabels */
5064 : :
5065 : 256 : pfree(tag);
5066 : 256 : destroyPQExpBuffer(query);
5067 : : }
5068 : :
5069 : : /*
5070 : : * Is the currently connected user a superuser?
5071 : : */
5072 : : static bool
5073 : 192 : is_superuser(Archive *fout)
5074 : : {
5075 : 192 : ArchiveHandle *AH = (ArchiveHandle *) fout;
5076 : : const char *val;
5077 : :
5078 : 192 : val = PQparameterStatus(AH->connection, "is_superuser");
5079 : :
5080 [ + - + + ]: 192 : if (val && strcmp(val, "on") == 0)
5081 : 189 : return true;
5082 : :
5083 : 3 : return false;
5084 : : }
5085 : :
5086 : : /*
5087 : : * Set the given value to restrict_nonsystem_relation_kind value. Since
5088 : : * restrict_nonsystem_relation_kind is introduced in minor version releases,
5089 : : * the setting query is effective only where available.
5090 : : */
5091 : : static void
5092 : 227 : set_restrict_relation_kind(Archive *AH, const char *value)
5093 : : {
5094 : 227 : PQExpBuffer query = createPQExpBuffer();
5095 : : PGresult *res;
5096 : :
5097 : 227 : appendPQExpBuffer(query,
5098 : : "SELECT set_config(name, '%s', false) "
5099 : : "FROM pg_settings "
5100 : : "WHERE name = 'restrict_nonsystem_relation_kind'",
5101 : : value);
5102 : 227 : res = ExecuteSqlQuery(AH, query->data, PGRES_TUPLES_OK);
5103 : :
5104 : 227 : PQclear(res);
5105 : 227 : destroyPQExpBuffer(query);
5106 : 227 : }
5107 : :
5108 : : /*
5109 : : * getSubscriptions
5110 : : * get information about subscriptions
5111 : : */
5112 : : void
5113 : 193 : getSubscriptions(Archive *fout)
5114 : : {
5115 : 193 : DumpOptions *dopt = fout->dopt;
5116 : : PQExpBuffer query;
5117 : : PGresult *res;
5118 : : SubscriptionInfo *subinfo;
5119 : : int i_tableoid;
5120 : : int i_oid;
5121 : : int i_subname;
5122 : : int i_subowner;
5123 : : int i_subbinary;
5124 : : int i_substream;
5125 : : int i_subtwophasestate;
5126 : : int i_subdisableonerr;
5127 : : int i_subpasswordrequired;
5128 : : int i_subrunasowner;
5129 : : int i_subservername;
5130 : : int i_subconninfo;
5131 : : int i_subslotname;
5132 : : int i_subsynccommit;
5133 : : int i_subwalrcvtimeout;
5134 : : int i_subpublications;
5135 : : int i_suborigin;
5136 : : int i_suboriginremotelsn;
5137 : : int i_subenabled;
5138 : : int i_subfailover;
5139 : : int i_subretaindeadtuples;
5140 : : int i_submaxretention;
5141 : : int i,
5142 : : ntups;
5143 : :
5144 [ + + ]: 193 : if (dopt->no_subscriptions)
5145 : 1 : return;
5146 : :
5147 [ + + ]: 192 : if (!is_superuser(fout))
5148 : : {
5149 : : int n;
5150 : :
5151 : 3 : res = ExecuteSqlQuery(fout,
5152 : : "SELECT count(*) FROM pg_subscription "
5153 : : "WHERE subdbid = (SELECT oid FROM pg_database"
5154 : : " WHERE datname = current_database())",
5155 : : PGRES_TUPLES_OK);
5156 : 3 : n = atoi(PQgetvalue(res, 0, 0));
5157 [ + + ]: 3 : if (n > 0)
5158 : 2 : pg_log_warning("subscriptions not dumped because current user is not a superuser");
5159 : 3 : PQclear(res);
5160 : 3 : return;
5161 : : }
5162 : :
5163 : 189 : query = createPQExpBuffer();
5164 : :
5165 : : /* Get the subscriptions in current database. */
5166 : 189 : appendPQExpBufferStr(query,
5167 : : "SELECT s.tableoid, s.oid, s.subname,\n"
5168 : : " s.subowner,\n"
5169 : : " s.subconninfo, s.subslotname, s.subsynccommit,\n"
5170 : : " s.subpublications,\n");
5171 : :
5172 [ + - ]: 189 : if (fout->remoteVersion >= 140000)
5173 : 189 : appendPQExpBufferStr(query, " s.subbinary,\n");
5174 : : else
5175 : 0 : appendPQExpBufferStr(query, " false AS subbinary,\n");
5176 : :
5177 [ + - ]: 189 : if (fout->remoteVersion >= 140000)
5178 : 189 : appendPQExpBufferStr(query, " s.substream,\n");
5179 : : else
5180 : 0 : appendPQExpBufferStr(query, " 'f' AS substream,\n");
5181 : :
5182 [ + - ]: 189 : if (fout->remoteVersion >= 150000)
5183 : 189 : appendPQExpBufferStr(query,
5184 : : " s.subtwophasestate,\n"
5185 : : " s.subdisableonerr,\n");
5186 : : else
5187 : 0 : appendPQExpBuffer(query,
5188 : : " '%c' AS subtwophasestate,\n"
5189 : : " false AS subdisableonerr,\n",
5190 : : LOGICALREP_TWOPHASE_STATE_DISABLED);
5191 : :
5192 [ + - ]: 189 : if (fout->remoteVersion >= 160000)
5193 : 189 : appendPQExpBufferStr(query,
5194 : : " s.subpasswordrequired,\n"
5195 : : " s.subrunasowner,\n"
5196 : : " s.suborigin,\n");
5197 : : else
5198 : 0 : appendPQExpBuffer(query,
5199 : : " 't' AS subpasswordrequired,\n"
5200 : : " 't' AS subrunasowner,\n"
5201 : : " '%s' AS suborigin,\n",
5202 : : LOGICALREP_ORIGIN_ANY);
5203 : :
5204 [ + + + - ]: 189 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5205 : 42 : appendPQExpBufferStr(query, " o.remote_lsn AS suboriginremotelsn,\n"
5206 : : " s.subenabled,\n");
5207 : : else
5208 : 147 : appendPQExpBufferStr(query, " NULL AS suboriginremotelsn,\n"
5209 : : " false AS subenabled,\n");
5210 : :
5211 [ + - ]: 189 : if (fout->remoteVersion >= 170000)
5212 : 189 : appendPQExpBufferStr(query,
5213 : : " s.subfailover,\n");
5214 : : else
5215 : 0 : appendPQExpBufferStr(query,
5216 : : " false AS subfailover,\n");
5217 : :
5218 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5219 : 189 : appendPQExpBufferStr(query,
5220 : : " s.subretaindeadtuples,\n");
5221 : : else
5222 : 0 : appendPQExpBufferStr(query,
5223 : : " false AS subretaindeadtuples,\n");
5224 : :
5225 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5226 : 189 : appendPQExpBufferStr(query,
5227 : : " s.submaxretention,\n");
5228 : : else
5229 : 0 : appendPQExpBufferStr(query, " 0 AS submaxretention,\n");
5230 : :
5231 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5232 : 189 : appendPQExpBufferStr(query,
5233 : : " s.subwalrcvtimeout,\n");
5234 : : else
5235 : 0 : appendPQExpBufferStr(query,
5236 : : " '-1' AS subwalrcvtimeout,\n");
5237 : :
5238 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5239 : 189 : appendPQExpBufferStr(query, " fs.srvname AS subservername\n");
5240 : : else
5241 : 0 : appendPQExpBufferStr(query, " NULL AS subservername\n");
5242 : :
5243 : 189 : appendPQExpBufferStr(query,
5244 : : "FROM pg_subscription s\n");
5245 : :
5246 [ + - ]: 189 : if (fout->remoteVersion >= 190000)
5247 : 189 : appendPQExpBufferStr(query,
5248 : : "LEFT JOIN pg_catalog.pg_foreign_server fs \n"
5249 : : " ON fs.oid = s.subserver \n");
5250 : :
5251 [ + + + - ]: 189 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5252 : 42 : appendPQExpBufferStr(query,
5253 : : "LEFT JOIN pg_catalog.pg_replication_origin_status o \n"
5254 : : " ON o.external_id = 'pg_' || s.oid::text \n");
5255 : :
5256 : 189 : appendPQExpBufferStr(query,
5257 : : "WHERE s.subdbid = (SELECT oid FROM pg_database\n"
5258 : : " WHERE datname = current_database())");
5259 : :
5260 : 189 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5261 : :
5262 : 189 : ntups = PQntuples(res);
5263 : :
5264 : : /*
5265 : : * Get subscription fields. We don't include subskiplsn in the dump as
5266 : : * after restoring the dump this value may no longer be relevant.
5267 : : */
5268 : 189 : i_tableoid = PQfnumber(res, "tableoid");
5269 : 189 : i_oid = PQfnumber(res, "oid");
5270 : 189 : i_subname = PQfnumber(res, "subname");
5271 : 189 : i_subowner = PQfnumber(res, "subowner");
5272 : 189 : i_subenabled = PQfnumber(res, "subenabled");
5273 : 189 : i_subbinary = PQfnumber(res, "subbinary");
5274 : 189 : i_substream = PQfnumber(res, "substream");
5275 : 189 : i_subtwophasestate = PQfnumber(res, "subtwophasestate");
5276 : 189 : i_subdisableonerr = PQfnumber(res, "subdisableonerr");
5277 : 189 : i_subpasswordrequired = PQfnumber(res, "subpasswordrequired");
5278 : 189 : i_subrunasowner = PQfnumber(res, "subrunasowner");
5279 : 189 : i_subfailover = PQfnumber(res, "subfailover");
5280 : 189 : i_subretaindeadtuples = PQfnumber(res, "subretaindeadtuples");
5281 : 189 : i_submaxretention = PQfnumber(res, "submaxretention");
5282 : 189 : i_subservername = PQfnumber(res, "subservername");
5283 : 189 : i_subconninfo = PQfnumber(res, "subconninfo");
5284 : 189 : i_subslotname = PQfnumber(res, "subslotname");
5285 : 189 : i_subsynccommit = PQfnumber(res, "subsynccommit");
5286 : 189 : i_subwalrcvtimeout = PQfnumber(res, "subwalrcvtimeout");
5287 : 189 : i_subpublications = PQfnumber(res, "subpublications");
5288 : 189 : i_suborigin = PQfnumber(res, "suborigin");
5289 : 189 : i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn");
5290 : :
5291 : 189 : subinfo = pg_malloc_array(SubscriptionInfo, ntups);
5292 : :
5293 [ + + ]: 326 : for (i = 0; i < ntups; i++)
5294 : : {
5295 : 137 : subinfo[i].dobj.objType = DO_SUBSCRIPTION;
5296 : 137 : subinfo[i].dobj.catId.tableoid =
5297 : 137 : atooid(PQgetvalue(res, i, i_tableoid));
5298 : 137 : subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5299 : 137 : AssignDumpId(&subinfo[i].dobj);
5300 : 137 : subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
5301 : 137 : subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner));
5302 : :
5303 : 137 : subinfo[i].subenabled =
5304 : 137 : (strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0);
5305 [ + - ]: 137 : if (PQgetisnull(res, i, i_subservername))
5306 : 137 : subinfo[i].subservername = NULL;
5307 : : else
5308 : 0 : subinfo[i].subservername = pg_strdup(PQgetvalue(res, i, i_subservername));
5309 : 137 : subinfo[i].subbinary =
5310 : 137 : (strcmp(PQgetvalue(res, i, i_subbinary), "t") == 0);
5311 : 137 : subinfo[i].substream = *(PQgetvalue(res, i, i_substream));
5312 : 137 : subinfo[i].subtwophasestate = *(PQgetvalue(res, i, i_subtwophasestate));
5313 : 137 : subinfo[i].subdisableonerr =
5314 : 137 : (strcmp(PQgetvalue(res, i, i_subdisableonerr), "t") == 0);
5315 : 137 : subinfo[i].subpasswordrequired =
5316 : 137 : (strcmp(PQgetvalue(res, i, i_subpasswordrequired), "t") == 0);
5317 : 137 : subinfo[i].subrunasowner =
5318 : 137 : (strcmp(PQgetvalue(res, i, i_subrunasowner), "t") == 0);
5319 : 137 : subinfo[i].subfailover =
5320 : 137 : (strcmp(PQgetvalue(res, i, i_subfailover), "t") == 0);
5321 : 137 : subinfo[i].subretaindeadtuples =
5322 : 137 : (strcmp(PQgetvalue(res, i, i_subretaindeadtuples), "t") == 0);
5323 : 137 : subinfo[i].submaxretention =
5324 : 137 : atoi(PQgetvalue(res, i, i_submaxretention));
5325 [ - + ]: 137 : if (PQgetisnull(res, i, i_subconninfo))
5326 : 0 : subinfo[i].subconninfo = NULL;
5327 : : else
5328 : 137 : subinfo[i].subconninfo =
5329 : 137 : pg_strdup(PQgetvalue(res, i, i_subconninfo));
5330 [ - + ]: 137 : if (PQgetisnull(res, i, i_subslotname))
5331 : 0 : subinfo[i].subslotname = NULL;
5332 : : else
5333 : 137 : subinfo[i].subslotname =
5334 : 137 : pg_strdup(PQgetvalue(res, i, i_subslotname));
5335 : 274 : subinfo[i].subsynccommit =
5336 : 137 : pg_strdup(PQgetvalue(res, i, i_subsynccommit));
5337 : 274 : subinfo[i].subwalrcvtimeout =
5338 : 137 : pg_strdup(PQgetvalue(res, i, i_subwalrcvtimeout));
5339 : 274 : subinfo[i].subpublications =
5340 : 137 : pg_strdup(PQgetvalue(res, i, i_subpublications));
5341 : 137 : subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin));
5342 [ + + ]: 137 : if (PQgetisnull(res, i, i_suboriginremotelsn))
5343 : 136 : subinfo[i].suboriginremotelsn = NULL;
5344 : : else
5345 : 1 : subinfo[i].suboriginremotelsn =
5346 : 1 : pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn));
5347 : :
5348 : : /* Decide whether we want to dump it */
5349 : 137 : selectDumpableObject(&(subinfo[i].dobj), fout);
5350 : : }
5351 : 189 : PQclear(res);
5352 : :
5353 : 189 : destroyPQExpBuffer(query);
5354 : : }
5355 : :
5356 : : /*
5357 : : * getSubscriptionRelations
5358 : : * Get information about subscription membership for dumpable relations. This
5359 : : * will be used only in binary-upgrade mode for PG17 or later versions.
5360 : : */
5361 : : void
5362 : 193 : getSubscriptionRelations(Archive *fout)
5363 : : {
5364 : 193 : DumpOptions *dopt = fout->dopt;
5365 : 193 : SubscriptionInfo *subinfo = NULL;
5366 : : SubRelInfo *subrinfo;
5367 : : PGresult *res;
5368 : : int i_srsubid;
5369 : : int i_srrelid;
5370 : : int i_srsubstate;
5371 : : int i_srsublsn;
5372 : : int ntups;
5373 : 193 : Oid last_srsubid = InvalidOid;
5374 : :
5375 [ + + + + ]: 193 : if (dopt->no_subscriptions || !dopt->binary_upgrade ||
5376 [ - + ]: 42 : fout->remoteVersion < 170000)
5377 : 151 : return;
5378 : :
5379 : 42 : res = ExecuteSqlQuery(fout,
5380 : : "SELECT srsubid, srrelid, srsubstate, srsublsn "
5381 : : "FROM pg_catalog.pg_subscription_rel "
5382 : : "ORDER BY srsubid",
5383 : : PGRES_TUPLES_OK);
5384 : 42 : ntups = PQntuples(res);
5385 [ + + ]: 42 : if (ntups == 0)
5386 : 41 : goto cleanup;
5387 : :
5388 : : /* Get pg_subscription_rel attributes */
5389 : 1 : i_srsubid = PQfnumber(res, "srsubid");
5390 : 1 : i_srrelid = PQfnumber(res, "srrelid");
5391 : 1 : i_srsubstate = PQfnumber(res, "srsubstate");
5392 : 1 : i_srsublsn = PQfnumber(res, "srsublsn");
5393 : :
5394 : 1 : subrinfo = pg_malloc_array(SubRelInfo, ntups);
5395 [ + + ]: 4 : for (int i = 0; i < ntups; i++)
5396 : : {
5397 : 3 : Oid cur_srsubid = atooid(PQgetvalue(res, i, i_srsubid));
5398 : 3 : Oid relid = atooid(PQgetvalue(res, i, i_srrelid));
5399 : : TableInfo *tblinfo;
5400 : :
5401 : : /*
5402 : : * If we switched to a new subscription, check if the subscription
5403 : : * exists.
5404 : : */
5405 [ + + ]: 3 : if (cur_srsubid != last_srsubid)
5406 : : {
5407 : 2 : subinfo = findSubscriptionByOid(cur_srsubid);
5408 [ - + ]: 2 : if (subinfo == NULL)
5409 : 0 : pg_fatal("subscription with OID %u does not exist", cur_srsubid);
5410 : :
5411 : 2 : last_srsubid = cur_srsubid;
5412 : : }
5413 : :
5414 : 3 : tblinfo = findTableByOid(relid);
5415 [ - + ]: 3 : if (tblinfo == NULL)
5416 : 0 : pg_fatal("failed sanity check, relation with OID %u not found",
5417 : : relid);
5418 : :
5419 : : /* OK, make a DumpableObject for this relationship */
5420 : 3 : subrinfo[i].dobj.objType = DO_SUBSCRIPTION_REL;
5421 : 3 : subrinfo[i].dobj.catId.tableoid = relid;
5422 : 3 : subrinfo[i].dobj.catId.oid = cur_srsubid;
5423 : 3 : AssignDumpId(&subrinfo[i].dobj);
5424 : 3 : subrinfo[i].dobj.namespace = tblinfo->dobj.namespace;
5425 : 3 : subrinfo[i].dobj.name = tblinfo->dobj.name;
5426 : 3 : subrinfo[i].subinfo = subinfo;
5427 : 3 : subrinfo[i].tblinfo = tblinfo;
5428 : 3 : subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0];
5429 [ + + ]: 3 : if (PQgetisnull(res, i, i_srsublsn))
5430 : 1 : subrinfo[i].srsublsn = NULL;
5431 : : else
5432 : 2 : subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn));
5433 : :
5434 : : /* Decide whether we want to dump it */
5435 : 3 : selectDumpableObject(&(subrinfo[i].dobj), fout);
5436 : : }
5437 : :
5438 : 1 : cleanup:
5439 : 42 : PQclear(res);
5440 : : }
5441 : :
5442 : : /*
5443 : : * dumpSubscriptionTable
5444 : : * Dump the definition of the given subscription table mapping. This will be
5445 : : * used only in binary-upgrade mode for PG17 or later versions.
5446 : : */
5447 : : static void
5448 : 3 : dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
5449 : : {
5450 : 3 : DumpOptions *dopt = fout->dopt;
5451 : 3 : SubscriptionInfo *subinfo = subrinfo->subinfo;
5452 : : PQExpBuffer query;
5453 : : char *tag;
5454 : :
5455 : : /* Do nothing if not dumping schema */
5456 [ - + ]: 3 : if (!dopt->dumpSchema)
5457 : 0 : return;
5458 : :
5459 : : Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000);
5460 : :
5461 : 3 : tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->tblinfo->dobj.name);
5462 : :
5463 : 3 : query = createPQExpBuffer();
5464 : :
5465 [ + - ]: 3 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5466 : : {
5467 : : /*
5468 : : * binary_upgrade_add_sub_rel_state will add the subscription relation
5469 : : * to pg_subscription_rel table. This will be used only in
5470 : : * binary-upgrade mode.
5471 : : */
5472 : 3 : appendPQExpBufferStr(query,
5473 : : "\n-- For binary upgrade, must preserve the subscriber table.\n");
5474 : 3 : appendPQExpBufferStr(query,
5475 : : "SELECT pg_catalog.binary_upgrade_add_sub_rel_state(");
5476 : 3 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5477 : 3 : appendPQExpBuffer(query,
5478 : : ", %u, '%c'",
5479 : 3 : subrinfo->tblinfo->dobj.catId.oid,
5480 : 3 : subrinfo->srsubstate);
5481 : :
5482 [ + + + - ]: 3 : if (subrinfo->srsublsn && subrinfo->srsublsn[0] != '\0')
5483 : 2 : appendPQExpBuffer(query, ", '%s'", subrinfo->srsublsn);
5484 : : else
5485 : 1 : appendPQExpBufferStr(query, ", NULL");
5486 : :
5487 : 3 : appendPQExpBufferStr(query, ");\n");
5488 : : }
5489 : :
5490 : : /*
5491 : : * There is no point in creating a drop query as the drop is done by table
5492 : : * drop. (If you think to change this, see also _printTocEntry().)
5493 : : * Although this object doesn't really have ownership as such, set the
5494 : : * owner field anyway to ensure that the command is run by the correct
5495 : : * role at restore time.
5496 : : */
5497 [ + - ]: 3 : if (subrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5498 : 3 : ArchiveEntry(fout, subrinfo->dobj.catId, subrinfo->dobj.dumpId,
5499 : 3 : ARCHIVE_OPTS(.tag = tag,
5500 : : .namespace = subrinfo->tblinfo->dobj.namespace->dobj.name,
5501 : : .owner = subinfo->rolname,
5502 : : .description = "SUBSCRIPTION TABLE",
5503 : : .section = SECTION_POST_DATA,
5504 : : .createStmt = query->data));
5505 : :
5506 : : /* These objects can't currently have comments or seclabels */
5507 : :
5508 : 3 : pfree(tag);
5509 : 3 : destroyPQExpBuffer(query);
5510 : : }
5511 : :
5512 : : /*
5513 : : * dumpSubscription
5514 : : * dump the definition of the given subscription
5515 : : */
5516 : : static void
5517 : 116 : dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo)
5518 : : {
5519 : 116 : DumpOptions *dopt = fout->dopt;
5520 : : PQExpBuffer delq;
5521 : : PQExpBuffer query;
5522 : : PQExpBuffer publications;
5523 : : char *qsubname;
5524 : 116 : char **pubnames = NULL;
5525 : 116 : int npubnames = 0;
5526 : : int i;
5527 : :
5528 : : /* Do nothing if not dumping schema */
5529 [ + + ]: 116 : if (!dopt->dumpSchema)
5530 : 18 : return;
5531 : :
5532 : 98 : delq = createPQExpBuffer();
5533 : 98 : query = createPQExpBuffer();
5534 : :
5535 : 98 : qsubname = pg_strdup(fmtId(subinfo->dobj.name));
5536 : :
5537 : 98 : appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n",
5538 : : qsubname);
5539 : :
5540 : 98 : appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s ",
5541 : : qsubname);
5542 [ - + ]: 98 : if (subinfo->subservername)
5543 : : {
5544 : 0 : appendPQExpBuffer(query, "SERVER %s", fmtId(subinfo->subservername));
5545 : : }
5546 : : else
5547 : : {
5548 : 98 : appendPQExpBufferStr(query, "CONNECTION ");
5549 : 98 : appendStringLiteralAH(query, subinfo->subconninfo, fout);
5550 : : }
5551 : :
5552 : : /* Build list of quoted publications and append them to query. */
5553 [ - + ]: 98 : if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames))
5554 : 0 : pg_fatal("could not parse %s array", "subpublications");
5555 : :
5556 : 98 : publications = createPQExpBuffer();
5557 [ + + ]: 196 : for (i = 0; i < npubnames; i++)
5558 : : {
5559 [ - + ]: 98 : if (i > 0)
5560 : 0 : appendPQExpBufferStr(publications, ", ");
5561 : :
5562 : 98 : appendPQExpBufferStr(publications, fmtId(pubnames[i]));
5563 : : }
5564 : :
5565 : 98 : appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data);
5566 [ + - ]: 98 : if (subinfo->subslotname)
5567 : 98 : appendStringLiteralAH(query, subinfo->subslotname, fout);
5568 : : else
5569 : 0 : appendPQExpBufferStr(query, "NONE");
5570 : :
5571 [ - + ]: 98 : if (subinfo->subbinary)
5572 : 0 : appendPQExpBufferStr(query, ", binary = true");
5573 : :
5574 [ + + ]: 98 : if (subinfo->substream == LOGICALREP_STREAM_ON)
5575 : 32 : appendPQExpBufferStr(query, ", streaming = on");
5576 [ + + ]: 66 : else if (subinfo->substream == LOGICALREP_STREAM_PARALLEL)
5577 : 34 : appendPQExpBufferStr(query, ", streaming = parallel");
5578 : : else
5579 : 32 : appendPQExpBufferStr(query, ", streaming = off");
5580 : :
5581 [ - + ]: 98 : if (subinfo->subtwophasestate != LOGICALREP_TWOPHASE_STATE_DISABLED)
5582 : 0 : appendPQExpBufferStr(query, ", two_phase = on");
5583 : :
5584 [ - + ]: 98 : if (subinfo->subdisableonerr)
5585 : 0 : appendPQExpBufferStr(query, ", disable_on_error = true");
5586 : :
5587 [ - + ]: 98 : if (!subinfo->subpasswordrequired)
5588 : 0 : appendPQExpBufferStr(query, ", password_required = false");
5589 : :
5590 [ - + ]: 98 : if (subinfo->subrunasowner)
5591 : 0 : appendPQExpBufferStr(query, ", run_as_owner = true");
5592 : :
5593 [ + + ]: 98 : if (subinfo->subfailover)
5594 : 1 : appendPQExpBufferStr(query, ", failover = true");
5595 : :
5596 [ + + ]: 98 : if (subinfo->subretaindeadtuples)
5597 : 1 : appendPQExpBufferStr(query, ", retain_dead_tuples = true");
5598 : :
5599 [ - + ]: 98 : if (subinfo->submaxretention)
5600 : 0 : appendPQExpBuffer(query, ", max_retention_duration = %d", subinfo->submaxretention);
5601 : :
5602 [ - + ]: 98 : if (strcmp(subinfo->subsynccommit, "off") != 0)
5603 : 0 : appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));
5604 : :
5605 [ - + ]: 98 : if (strcmp(subinfo->subwalrcvtimeout, "-1") != 0)
5606 : 0 : appendPQExpBuffer(query, ", wal_receiver_timeout = %s", fmtId(subinfo->subwalrcvtimeout));
5607 : :
5608 [ + + ]: 98 : if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0)
5609 : 32 : appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin);
5610 : :
5611 : 98 : appendPQExpBufferStr(query, ");\n");
5612 : :
5613 : : /*
5614 : : * In binary-upgrade mode, we allow the replication to continue after the
5615 : : * upgrade.
5616 : : */
5617 [ + + + - ]: 98 : if (dopt->binary_upgrade && fout->remoteVersion >= 170000)
5618 : : {
5619 [ + + ]: 5 : if (subinfo->suboriginremotelsn)
5620 : : {
5621 : : /*
5622 : : * Preserve the remote_lsn for the subscriber's replication
5623 : : * origin. This value is required to start the replication from
5624 : : * the position before the upgrade. This value will be stale if
5625 : : * the publisher gets upgraded before the subscriber node.
5626 : : * However, this shouldn't be a problem as the upgrade of the
5627 : : * publisher ensures that all the transactions were replicated
5628 : : * before upgrading it.
5629 : : */
5630 : 1 : appendPQExpBufferStr(query,
5631 : : "\n-- For binary upgrade, must preserve the remote_lsn for the subscriber's replication origin.\n");
5632 : 1 : appendPQExpBufferStr(query,
5633 : : "SELECT pg_catalog.binary_upgrade_replorigin_advance(");
5634 : 1 : appendStringLiteralAH(query, subinfo->dobj.name, fout);
5635 : 1 : appendPQExpBuffer(query, ", '%s');\n", subinfo->suboriginremotelsn);
5636 : : }
5637 : :
5638 [ + + ]: 5 : if (subinfo->subenabled)
5639 : : {
5640 : : /*
5641 : : * Enable the subscription to allow the replication to continue
5642 : : * after the upgrade.
5643 : : */
5644 : 1 : appendPQExpBufferStr(query,
5645 : : "\n-- For binary upgrade, must preserve the subscriber's running state.\n");
5646 : 1 : appendPQExpBuffer(query, "ALTER SUBSCRIPTION %s ENABLE;\n", qsubname);
5647 : : }
5648 : : }
5649 : :
5650 [ + - ]: 98 : if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
5651 : 98 : ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
5652 : 98 : ARCHIVE_OPTS(.tag = subinfo->dobj.name,
5653 : : .owner = subinfo->rolname,
5654 : : .description = "SUBSCRIPTION",
5655 : : .section = SECTION_POST_DATA,
5656 : : .createStmt = query->data,
5657 : : .dropStmt = delq->data));
5658 : :
5659 [ + + ]: 98 : if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
5660 : 32 : dumpComment(fout, "SUBSCRIPTION", qsubname,
5661 : 32 : NULL, subinfo->rolname,
5662 : 32 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5663 : :
5664 [ - + ]: 98 : if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
5665 : 0 : dumpSecLabel(fout, "SUBSCRIPTION", qsubname,
5666 : 0 : NULL, subinfo->rolname,
5667 : 0 : subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
5668 : :
5669 : 98 : destroyPQExpBuffer(publications);
5670 : 98 : free(pubnames);
5671 : :
5672 : 98 : destroyPQExpBuffer(delq);
5673 : 98 : destroyPQExpBuffer(query);
5674 : 98 : pg_free(qsubname);
5675 : : }
5676 : :
5677 : : /*
5678 : : * Given a "create query", append as many ALTER ... DEPENDS ON EXTENSION as
5679 : : * the object needs.
5680 : : */
5681 : : static void
5682 : 5431 : append_depends_on_extension(Archive *fout,
5683 : : PQExpBuffer create,
5684 : : const DumpableObject *dobj,
5685 : : const char *catalog,
5686 : : const char *keyword,
5687 : : const char *objname)
5688 : : {
5689 [ + + ]: 5431 : if (dobj->depends_on_ext)
5690 : : {
5691 : : char *nm;
5692 : : PGresult *res;
5693 : : PQExpBuffer query;
5694 : : int ntups;
5695 : : int i_extname;
5696 : : int i;
5697 : :
5698 : : /* dodge fmtId() non-reentrancy */
5699 : 42 : nm = pg_strdup(objname);
5700 : :
5701 : 42 : query = createPQExpBuffer();
5702 : 42 : appendPQExpBuffer(query,
5703 : : "SELECT e.extname "
5704 : : "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
5705 : : "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
5706 : : "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
5707 : : "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
5708 : : catalog,
5709 : 42 : dobj->catId.oid);
5710 : 42 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5711 : 42 : ntups = PQntuples(res);
5712 : 42 : i_extname = PQfnumber(res, "extname");
5713 [ + + ]: 84 : for (i = 0; i < ntups; i++)
5714 : : {
5715 : 42 : appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
5716 : : keyword, nm,
5717 : 42 : fmtId(PQgetvalue(res, i, i_extname)));
5718 : : }
5719 : :
5720 : 42 : PQclear(res);
5721 : 42 : destroyPQExpBuffer(query);
5722 : 42 : pg_free(nm);
5723 : : }
5724 : 5431 : }
5725 : :
5726 : : static Oid
5727 : 0 : get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
5728 : : {
5729 : : /*
5730 : : * If the old version didn't assign an array type, but the new version
5731 : : * does, we must select an unused type OID to assign. This currently only
5732 : : * happens for domains, when upgrading pre-v11 to v11 and up.
5733 : : *
5734 : : * Note: local state here is kind of ugly, but we must have some, since we
5735 : : * mustn't choose the same unused OID more than once.
5736 : : */
5737 : : static Oid next_possible_free_oid = FirstNormalObjectId;
5738 : : PGresult *res;
5739 : : bool is_dup;
5740 : :
5741 : : do
5742 : : {
5743 : 0 : ++next_possible_free_oid;
5744 : 0 : printfPQExpBuffer(upgrade_query,
5745 : : "SELECT EXISTS(SELECT 1 "
5746 : : "FROM pg_catalog.pg_type "
5747 : : "WHERE oid = '%u'::pg_catalog.oid);",
5748 : : next_possible_free_oid);
5749 : 0 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5750 : 0 : is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
5751 : 0 : PQclear(res);
5752 [ # # ]: 0 : } while (is_dup);
5753 : :
5754 : 0 : return next_possible_free_oid;
5755 : : }
5756 : :
5757 : : static void
5758 : 1019 : binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
5759 : : PQExpBuffer upgrade_buffer,
5760 : : Oid pg_type_oid,
5761 : : bool force_array_type,
5762 : : bool include_multirange_type)
5763 : : {
5764 : 1019 : PQExpBuffer upgrade_query = createPQExpBuffer();
5765 : : PGresult *res;
5766 : : Oid pg_type_array_oid;
5767 : : Oid pg_type_multirange_oid;
5768 : : Oid pg_type_multirange_array_oid;
5769 : : TypeInfo *tinfo;
5770 : :
5771 : 1019 : appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
5772 : 1019 : appendPQExpBuffer(upgrade_buffer,
5773 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5774 : : pg_type_oid);
5775 : :
5776 : 1019 : tinfo = findTypeByOid(pg_type_oid);
5777 [ + - ]: 1019 : if (tinfo)
5778 : 1019 : pg_type_array_oid = tinfo->typarray;
5779 : : else
5780 : 0 : pg_type_array_oid = InvalidOid;
5781 : :
5782 [ + + - + ]: 1019 : if (!OidIsValid(pg_type_array_oid) && force_array_type)
5783 : 0 : pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5784 : :
5785 [ + + ]: 1019 : if (OidIsValid(pg_type_array_oid))
5786 : : {
5787 : 1017 : appendPQExpBufferStr(upgrade_buffer,
5788 : : "\n-- For binary upgrade, must preserve pg_type array oid\n");
5789 : 1017 : appendPQExpBuffer(upgrade_buffer,
5790 : : "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5791 : : pg_type_array_oid);
5792 : : }
5793 : :
5794 : : /*
5795 : : * Pre-set the multirange type oid and its own array type oid.
5796 : : */
5797 [ + + ]: 1019 : if (include_multirange_type)
5798 : : {
5799 [ + - ]: 9 : if (fout->remoteVersion >= 140000)
5800 : : {
5801 : 9 : printfPQExpBuffer(upgrade_query,
5802 : : "SELECT t.oid, t.typarray "
5803 : : "FROM pg_catalog.pg_type t "
5804 : : "JOIN pg_catalog.pg_range r "
5805 : : "ON t.oid = r.rngmultitypid "
5806 : : "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
5807 : : pg_type_oid);
5808 : :
5809 : 9 : res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5810 : :
5811 : 9 : pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
5812 : 9 : pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5813 : :
5814 : 9 : PQclear(res);
5815 : : }
5816 : : else
5817 : : {
5818 : 0 : pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5819 : 0 : pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5820 : : }
5821 : :
5822 : 9 : appendPQExpBufferStr(upgrade_buffer,
5823 : : "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
5824 : 9 : appendPQExpBuffer(upgrade_buffer,
5825 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5826 : : pg_type_multirange_oid);
5827 : 9 : appendPQExpBufferStr(upgrade_buffer,
5828 : : "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
5829 : 9 : appendPQExpBuffer(upgrade_buffer,
5830 : : "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5831 : : pg_type_multirange_array_oid);
5832 : : }
5833 : :
5834 : 1019 : destroyPQExpBuffer(upgrade_query);
5835 : 1019 : }
5836 : :
5837 : : static void
5838 : 954 : binary_upgrade_set_type_oids_by_rel(Archive *fout,
5839 : : PQExpBuffer upgrade_buffer,
5840 : : const TableInfo *tbinfo)
5841 : : {
5842 : 954 : Oid pg_type_oid = tbinfo->reltype;
5843 : :
5844 [ + + ]: 954 : if (OidIsValid(pg_type_oid))
5845 : 939 : binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5846 : : pg_type_oid, false, false);
5847 : 954 : }
5848 : :
5849 : : /*
5850 : : * bsearch() comparator for BinaryUpgradeClassOidItem
5851 : : */
5852 : : static int
5853 : 13872 : BinaryUpgradeClassOidItemCmp(const void *p1, const void *p2)
5854 : : {
5855 : 13872 : BinaryUpgradeClassOidItem v1 = *((const BinaryUpgradeClassOidItem *) p1);
5856 : 13872 : BinaryUpgradeClassOidItem v2 = *((const BinaryUpgradeClassOidItem *) p2);
5857 : :
5858 : 13872 : return pg_cmp_u32(v1.oid, v2.oid);
5859 : : }
5860 : :
5861 : : /*
5862 : : * collectBinaryUpgradeClassOids
5863 : : *
5864 : : * Construct a table of pg_class information required for
5865 : : * binary_upgrade_set_pg_class_oids(). The table is sorted by OID for speed in
5866 : : * lookup.
5867 : : */
5868 : : static void
5869 : 42 : collectBinaryUpgradeClassOids(Archive *fout)
5870 : : {
5871 : : PGresult *res;
5872 : : const char *query;
5873 : :
5874 : 42 : query = "SELECT c.oid, c.relkind, c.relfilenode, c.reltoastrelid, "
5875 : : "ct.relfilenode, i.indexrelid, cti.relfilenode "
5876 : : "FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index i "
5877 : : "ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5878 : : "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5879 : : "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5880 : : "ORDER BY c.oid;";
5881 : :
5882 : 42 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
5883 : :
5884 : 42 : nbinaryUpgradeClassOids = PQntuples(res);
5885 : 42 : binaryUpgradeClassOids =
5886 : 42 : pg_malloc_array(BinaryUpgradeClassOidItem, nbinaryUpgradeClassOids);
5887 : :
5888 [ + + ]: 21042 : for (int i = 0; i < nbinaryUpgradeClassOids; i++)
5889 : : {
5890 : 21000 : binaryUpgradeClassOids[i].oid = atooid(PQgetvalue(res, i, 0));
5891 : 21000 : binaryUpgradeClassOids[i].relkind = *PQgetvalue(res, i, 1);
5892 : 21000 : binaryUpgradeClassOids[i].relfilenumber = atooid(PQgetvalue(res, i, 2));
5893 : 21000 : binaryUpgradeClassOids[i].toast_oid = atooid(PQgetvalue(res, i, 3));
5894 : 21000 : binaryUpgradeClassOids[i].toast_relfilenumber = atooid(PQgetvalue(res, i, 4));
5895 : 21000 : binaryUpgradeClassOids[i].toast_index_oid = atooid(PQgetvalue(res, i, 5));
5896 : 21000 : binaryUpgradeClassOids[i].toast_index_relfilenumber = atooid(PQgetvalue(res, i, 6));
5897 : : }
5898 : :
5899 : 42 : PQclear(res);
5900 : 42 : }
5901 : :
5902 : : static void
5903 : 1380 : binary_upgrade_set_pg_class_oids(Archive *fout,
5904 : : PQExpBuffer upgrade_buffer, Oid pg_class_oid)
5905 : : {
5906 : 1380 : BinaryUpgradeClassOidItem key = {0};
5907 : : BinaryUpgradeClassOidItem *entry;
5908 : :
5909 : : Assert(binaryUpgradeClassOids);
5910 : :
5911 : : /*
5912 : : * Preserve the OID and relfilenumber of the table, table's index, table's
5913 : : * toast table and toast table's index if any.
5914 : : *
5915 : : * One complexity is that the current table definition might not require
5916 : : * the creation of a TOAST table, but the old database might have a TOAST
5917 : : * table that was created earlier, before some wide columns were dropped.
5918 : : * By setting the TOAST oid we force creation of the TOAST heap and index
5919 : : * by the new backend, so we can copy the files during binary upgrade
5920 : : * without worrying about this case.
5921 : : */
5922 : 1380 : key.oid = pg_class_oid;
5923 : 1380 : entry = bsearch(&key, binaryUpgradeClassOids, nbinaryUpgradeClassOids,
5924 : : sizeof(BinaryUpgradeClassOidItem),
5925 : : BinaryUpgradeClassOidItemCmp);
5926 : :
5927 : 1380 : appendPQExpBufferStr(upgrade_buffer,
5928 : : "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
5929 : :
5930 [ + + ]: 1380 : if (entry->relkind != RELKIND_INDEX &&
5931 [ + + ]: 1069 : entry->relkind != RELKIND_PARTITIONED_INDEX)
5932 : : {
5933 : 1038 : appendPQExpBuffer(upgrade_buffer,
5934 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5935 : : pg_class_oid);
5936 : :
5937 : : /*
5938 : : * Not every relation has storage. Also, in a pre-v12 database,
5939 : : * partitioned tables have a relfilenumber, which should not be
5940 : : * preserved when upgrading.
5941 : : */
5942 [ + + ]: 1038 : if (RelFileNumberIsValid(entry->relfilenumber) &&
5943 [ + - ]: 850 : entry->relkind != RELKIND_PARTITIONED_TABLE)
5944 : 850 : appendPQExpBuffer(upgrade_buffer,
5945 : : "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
5946 : : entry->relfilenumber);
5947 : :
5948 : : /*
5949 : : * In a pre-v12 database, partitioned tables might be marked as having
5950 : : * toast tables, but we should ignore them if so.
5951 : : */
5952 [ + + ]: 1038 : if (OidIsValid(entry->toast_oid) &&
5953 [ + - ]: 297 : entry->relkind != RELKIND_PARTITIONED_TABLE)
5954 : : {
5955 : 297 : appendPQExpBuffer(upgrade_buffer,
5956 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
5957 : : entry->toast_oid);
5958 : 297 : appendPQExpBuffer(upgrade_buffer,
5959 : : "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
5960 : : entry->toast_relfilenumber);
5961 : :
5962 : : /* every toast table has an index */
5963 : 297 : appendPQExpBuffer(upgrade_buffer,
5964 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5965 : : entry->toast_index_oid);
5966 : 297 : appendPQExpBuffer(upgrade_buffer,
5967 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5968 : : entry->toast_index_relfilenumber);
5969 : : }
5970 : : }
5971 : : else
5972 : : {
5973 : : /* Preserve the OID and relfilenumber of the index */
5974 : 342 : appendPQExpBuffer(upgrade_buffer,
5975 : : "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5976 : : pg_class_oid);
5977 : 342 : appendPQExpBuffer(upgrade_buffer,
5978 : : "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5979 : : entry->relfilenumber);
5980 : : }
5981 : :
5982 : 1380 : appendPQExpBufferChar(upgrade_buffer, '\n');
5983 : 1380 : }
5984 : :
5985 : : /*
5986 : : * If the DumpableObject is a member of an extension, add a suitable
5987 : : * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
5988 : : *
5989 : : * For somewhat historical reasons, objname should already be quoted,
5990 : : * but not objnamespace (if any).
5991 : : */
5992 : : static void
5993 : 1627 : binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
5994 : : const DumpableObject *dobj,
5995 : : const char *objtype,
5996 : : const char *objname,
5997 : : const char *objnamespace)
5998 : : {
5999 : 1627 : DumpableObject *extobj = NULL;
6000 : : int i;
6001 : :
6002 [ + + ]: 1627 : if (!dobj->ext_member)
6003 : 1605 : return;
6004 : :
6005 : : /*
6006 : : * Find the parent extension. We could avoid this search if we wanted to
6007 : : * add a link field to DumpableObject, but the space costs of that would
6008 : : * be considerable. We assume that member objects could only have a
6009 : : * direct dependency on their own extension, not any others.
6010 : : */
6011 [ + - ]: 22 : for (i = 0; i < dobj->nDeps; i++)
6012 : : {
6013 : 22 : extobj = findObjectByDumpId(dobj->dependencies[i]);
6014 [ + - + - ]: 22 : if (extobj && extobj->objType == DO_EXTENSION)
6015 : 22 : break;
6016 : 0 : extobj = NULL;
6017 : : }
6018 [ - + ]: 22 : if (extobj == NULL)
6019 : 0 : pg_fatal("could not find parent extension for %s %s",
6020 : : objtype, objname);
6021 : :
6022 : 22 : appendPQExpBufferStr(upgrade_buffer,
6023 : : "\n-- For binary upgrade, handle extension membership the hard way\n");
6024 : 22 : appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
6025 : 22 : fmtId(extobj->name),
6026 : : objtype);
6027 [ + + + - ]: 22 : if (objnamespace && *objnamespace)
6028 : 19 : appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
6029 : 22 : appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
6030 : : }
6031 : :
6032 : : /*
6033 : : * getNamespaces:
6034 : : * get information about all namespaces in the system catalogs
6035 : : */
6036 : : void
6037 : 194 : getNamespaces(Archive *fout)
6038 : : {
6039 : : PGresult *res;
6040 : : int ntups;
6041 : : int i;
6042 : : PQExpBuffer query;
6043 : : NamespaceInfo *nsinfo;
6044 : : int i_tableoid;
6045 : : int i_oid;
6046 : : int i_nspname;
6047 : : int i_nspowner;
6048 : : int i_nspacl;
6049 : : int i_acldefault;
6050 : :
6051 : 194 : query = createPQExpBuffer();
6052 : :
6053 : : /*
6054 : : * we fetch all namespaces including system ones, so that every object we
6055 : : * read in can be linked to a containing namespace.
6056 : : */
6057 : 194 : appendPQExpBufferStr(query, "SELECT n.tableoid, n.oid, n.nspname, "
6058 : : "n.nspowner, "
6059 : : "n.nspacl, "
6060 : : "acldefault('n', n.nspowner) AS acldefault "
6061 : : "FROM pg_namespace n");
6062 : :
6063 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6064 : :
6065 : 194 : ntups = PQntuples(res);
6066 : :
6067 : 194 : nsinfo = pg_malloc_array(NamespaceInfo, ntups);
6068 : :
6069 : 194 : i_tableoid = PQfnumber(res, "tableoid");
6070 : 194 : i_oid = PQfnumber(res, "oid");
6071 : 194 : i_nspname = PQfnumber(res, "nspname");
6072 : 194 : i_nspowner = PQfnumber(res, "nspowner");
6073 : 194 : i_nspacl = PQfnumber(res, "nspacl");
6074 : 194 : i_acldefault = PQfnumber(res, "acldefault");
6075 : :
6076 [ + + ]: 1892 : for (i = 0; i < ntups; i++)
6077 : : {
6078 : : const char *nspowner;
6079 : :
6080 : 1698 : nsinfo[i].dobj.objType = DO_NAMESPACE;
6081 : 1698 : nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6082 : 1698 : nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6083 : 1698 : AssignDumpId(&nsinfo[i].dobj);
6084 : 1698 : nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
6085 : 1698 : nsinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_nspacl));
6086 : 1698 : nsinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6087 : 1698 : nsinfo[i].dacl.privtype = 0;
6088 : 1698 : nsinfo[i].dacl.initprivs = NULL;
6089 : 1698 : nspowner = PQgetvalue(res, i, i_nspowner);
6090 : 1698 : nsinfo[i].nspowner = atooid(nspowner);
6091 : 1698 : nsinfo[i].rolname = getRoleName(nspowner);
6092 : :
6093 : : /* Decide whether to dump this namespace */
6094 : 1698 : selectDumpableNamespace(&nsinfo[i], fout);
6095 : :
6096 : : /* Mark whether namespace has an ACL */
6097 [ + + ]: 1698 : if (!PQgetisnull(res, i, i_nspacl))
6098 : 869 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6099 : :
6100 : : /*
6101 : : * We ignore any pg_init_privs.initprivs entry for the public schema
6102 : : * and assume a predetermined default, for several reasons. First,
6103 : : * dropping and recreating the schema removes its pg_init_privs entry,
6104 : : * but an empty destination database starts with this ACL nonetheless.
6105 : : * Second, we support dump/reload of public schema ownership changes.
6106 : : * ALTER SCHEMA OWNER filters nspacl through aclnewowner(), but
6107 : : * initprivs continues to reflect the initial owner. Hence,
6108 : : * synthesize the value that nspacl will have after the restore's
6109 : : * ALTER SCHEMA OWNER. Third, this makes the destination database
6110 : : * match the source's ACL, even if the latter was an initdb-default
6111 : : * ACL, which changed in v15. An upgrade pulls in changes to most
6112 : : * system object ACLs that the DBA had not customized. We've made the
6113 : : * public schema depart from that, because changing its ACL so easily
6114 : : * breaks applications.
6115 : : */
6116 [ + + ]: 1698 : if (strcmp(nsinfo[i].dobj.name, "public") == 0)
6117 : : {
6118 : 190 : PQExpBuffer aclarray = createPQExpBuffer();
6119 : 190 : PQExpBuffer aclitem = createPQExpBuffer();
6120 : :
6121 : : /* Standard ACL as of v15 is {owner=UC/owner,=U/owner} */
6122 : 190 : appendPQExpBufferChar(aclarray, '{');
6123 : 190 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6124 : 190 : appendPQExpBufferStr(aclitem, "=UC/");
6125 : 190 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6126 : 190 : appendPGArray(aclarray, aclitem->data);
6127 : 190 : resetPQExpBuffer(aclitem);
6128 : 190 : appendPQExpBufferStr(aclitem, "=U/");
6129 : 190 : quoteAclUserName(aclitem, nsinfo[i].rolname);
6130 : 190 : appendPGArray(aclarray, aclitem->data);
6131 : 190 : appendPQExpBufferChar(aclarray, '}');
6132 : :
6133 : 190 : nsinfo[i].dacl.privtype = 'i';
6134 : 190 : nsinfo[i].dacl.initprivs = pstrdup(aclarray->data);
6135 : 190 : nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6136 : :
6137 : 190 : destroyPQExpBuffer(aclarray);
6138 : 190 : destroyPQExpBuffer(aclitem);
6139 : : }
6140 : : }
6141 : :
6142 : 194 : PQclear(res);
6143 : 194 : destroyPQExpBuffer(query);
6144 : 194 : }
6145 : :
6146 : : /*
6147 : : * findNamespace:
6148 : : * given a namespace OID, look up the info read by getNamespaces
6149 : : */
6150 : : static NamespaceInfo *
6151 : 632471 : findNamespace(Oid nsoid)
6152 : : {
6153 : : NamespaceInfo *nsinfo;
6154 : :
6155 : 632471 : nsinfo = findNamespaceByOid(nsoid);
6156 [ - + ]: 632471 : if (nsinfo == NULL)
6157 : 0 : pg_fatal("schema with OID %u does not exist", nsoid);
6158 : 632471 : return nsinfo;
6159 : : }
6160 : :
6161 : : /*
6162 : : * getExtensions:
6163 : : * read all extensions in the system catalogs and return them in the
6164 : : * ExtensionInfo* structure
6165 : : *
6166 : : * numExtensions is set to the number of extensions read in
6167 : : */
6168 : : ExtensionInfo *
6169 : 194 : getExtensions(Archive *fout, int *numExtensions)
6170 : : {
6171 : 194 : DumpOptions *dopt = fout->dopt;
6172 : : PGresult *res;
6173 : : int ntups;
6174 : : int i;
6175 : : PQExpBuffer query;
6176 : 194 : ExtensionInfo *extinfo = NULL;
6177 : : int i_tableoid;
6178 : : int i_oid;
6179 : : int i_extname;
6180 : : int i_nspname;
6181 : : int i_extrelocatable;
6182 : : int i_extversion;
6183 : : int i_extconfig;
6184 : : int i_extcondition;
6185 : :
6186 : 194 : query = createPQExpBuffer();
6187 : :
6188 : 194 : appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, "
6189 : : "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
6190 : : "FROM pg_extension x "
6191 : : "JOIN pg_namespace n ON n.oid = x.extnamespace");
6192 : :
6193 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6194 : :
6195 : 194 : ntups = PQntuples(res);
6196 [ - + ]: 194 : if (ntups == 0)
6197 : 0 : goto cleanup;
6198 : :
6199 : 194 : extinfo = pg_malloc_array(ExtensionInfo, ntups);
6200 : :
6201 : 194 : i_tableoid = PQfnumber(res, "tableoid");
6202 : 194 : i_oid = PQfnumber(res, "oid");
6203 : 194 : i_extname = PQfnumber(res, "extname");
6204 : 194 : i_nspname = PQfnumber(res, "nspname");
6205 : 194 : i_extrelocatable = PQfnumber(res, "extrelocatable");
6206 : 194 : i_extversion = PQfnumber(res, "extversion");
6207 : 194 : i_extconfig = PQfnumber(res, "extconfig");
6208 : 194 : i_extcondition = PQfnumber(res, "extcondition");
6209 : :
6210 [ + + ]: 419 : for (i = 0; i < ntups; i++)
6211 : : {
6212 : 225 : extinfo[i].dobj.objType = DO_EXTENSION;
6213 : 225 : extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6214 : 225 : extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6215 : 225 : AssignDumpId(&extinfo[i].dobj);
6216 : 225 : extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
6217 : 225 : extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
6218 : 225 : extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
6219 : 225 : extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
6220 : 225 : extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
6221 : 225 : extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
6222 : :
6223 : : /* Decide whether we want to dump it */
6224 : 225 : selectDumpableExtension(&(extinfo[i]), dopt);
6225 : : }
6226 : :
6227 : 194 : cleanup:
6228 : 194 : PQclear(res);
6229 : 194 : destroyPQExpBuffer(query);
6230 : :
6231 : 194 : *numExtensions = ntups;
6232 : :
6233 : 194 : return extinfo;
6234 : : }
6235 : :
6236 : : /*
6237 : : * getTypes:
6238 : : * get information about all types in the system catalogs
6239 : : *
6240 : : * NB: this must run after getFuncs() because we assume we can do
6241 : : * findFuncByOid().
6242 : : */
6243 : : void
6244 : 193 : getTypes(Archive *fout)
6245 : : {
6246 : : PGresult *res;
6247 : : int ntups;
6248 : : int i;
6249 : 193 : PQExpBuffer query = createPQExpBuffer();
6250 : : TypeInfo *tyinfo;
6251 : : ShellTypeInfo *stinfo;
6252 : : int i_tableoid;
6253 : : int i_oid;
6254 : : int i_typname;
6255 : : int i_typnamespace;
6256 : : int i_typacl;
6257 : : int i_acldefault;
6258 : : int i_typowner;
6259 : : int i_typelem;
6260 : : int i_typrelid;
6261 : : int i_typrelkind;
6262 : : int i_typtype;
6263 : : int i_typisdefined;
6264 : : int i_isarray;
6265 : : int i_typarray;
6266 : :
6267 : : /*
6268 : : * we include even the built-in types because those may be used as array
6269 : : * elements by user-defined types
6270 : : *
6271 : : * we filter out the built-in types when we dump out the types
6272 : : *
6273 : : * same approach for undefined (shell) types and array types
6274 : : *
6275 : : * Note: as of 8.3 we can reliably detect whether a type is an
6276 : : * auto-generated array type by checking the element type's typarray.
6277 : : * (Before that the test is capable of generating false positives.) We
6278 : : * still check for name beginning with '_', though, so as to avoid the
6279 : : * cost of the subselect probe for all standard types. This would have to
6280 : : * be revisited if the backend ever allows renaming of array types.
6281 : : */
6282 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, typname, "
6283 : : "typnamespace, typacl, "
6284 : : "acldefault('T', typowner) AS acldefault, "
6285 : : "typowner, "
6286 : : "typelem, typrelid, typarray, "
6287 : : "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
6288 : : "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
6289 : : "typtype, typisdefined, "
6290 : : "typname[0] = '_' AND typelem != 0 AND "
6291 : : "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
6292 : : "FROM pg_type");
6293 : :
6294 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6295 : :
6296 : 193 : ntups = PQntuples(res);
6297 : :
6298 : 193 : tyinfo = pg_malloc_array(TypeInfo, ntups);
6299 : :
6300 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6301 : 193 : i_oid = PQfnumber(res, "oid");
6302 : 193 : i_typname = PQfnumber(res, "typname");
6303 : 193 : i_typnamespace = PQfnumber(res, "typnamespace");
6304 : 193 : i_typacl = PQfnumber(res, "typacl");
6305 : 193 : i_acldefault = PQfnumber(res, "acldefault");
6306 : 193 : i_typowner = PQfnumber(res, "typowner");
6307 : 193 : i_typelem = PQfnumber(res, "typelem");
6308 : 193 : i_typrelid = PQfnumber(res, "typrelid");
6309 : 193 : i_typrelkind = PQfnumber(res, "typrelkind");
6310 : 193 : i_typtype = PQfnumber(res, "typtype");
6311 : 193 : i_typisdefined = PQfnumber(res, "typisdefined");
6312 : 193 : i_isarray = PQfnumber(res, "isarray");
6313 : 193 : i_typarray = PQfnumber(res, "typarray");
6314 : :
6315 [ + + ]: 149541 : for (i = 0; i < ntups; i++)
6316 : : {
6317 : 149348 : tyinfo[i].dobj.objType = DO_TYPE;
6318 : 149348 : tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6319 : 149348 : tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6320 : 149348 : AssignDumpId(&tyinfo[i].dobj);
6321 : 149348 : tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
6322 : 298696 : tyinfo[i].dobj.namespace =
6323 : 149348 : findNamespace(atooid(PQgetvalue(res, i, i_typnamespace)));
6324 : 149348 : tyinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_typacl));
6325 : 149348 : tyinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6326 : 149348 : tyinfo[i].dacl.privtype = 0;
6327 : 149348 : tyinfo[i].dacl.initprivs = NULL;
6328 : 149348 : tyinfo[i].ftypname = NULL; /* may get filled later */
6329 : 149348 : tyinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_typowner));
6330 : 149348 : tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
6331 : 149348 : tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
6332 : 149348 : tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
6333 : 149348 : tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
6334 : 149348 : tyinfo[i].shellType = NULL;
6335 : :
6336 [ + + ]: 149348 : if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
6337 : 149293 : tyinfo[i].isDefined = true;
6338 : : else
6339 : 55 : tyinfo[i].isDefined = false;
6340 : :
6341 [ + + ]: 149348 : if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
6342 : 71848 : tyinfo[i].isArray = true;
6343 : : else
6344 : 77500 : tyinfo[i].isArray = false;
6345 : :
6346 : 149348 : tyinfo[i].typarray = atooid(PQgetvalue(res, i, i_typarray));
6347 : :
6348 [ + + ]: 149348 : if (tyinfo[i].typtype == TYPTYPE_MULTIRANGE)
6349 : 1303 : tyinfo[i].isMultirange = true;
6350 : : else
6351 : 148045 : tyinfo[i].isMultirange = false;
6352 : :
6353 : : /* Decide whether we want to dump it */
6354 : 149348 : selectDumpableType(&tyinfo[i], fout);
6355 : :
6356 : : /* Mark whether type has an ACL */
6357 [ + + ]: 149348 : if (!PQgetisnull(res, i, i_typacl))
6358 : 217 : tyinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
6359 : :
6360 : : /*
6361 : : * If it's a domain, fetch info about its constraints, if any
6362 : : */
6363 : 149348 : tyinfo[i].nDomChecks = 0;
6364 : 149348 : tyinfo[i].domChecks = NULL;
6365 : 149348 : tyinfo[i].notnull = NULL;
6366 [ + + ]: 149348 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6367 [ + + ]: 16356 : tyinfo[i].typtype == TYPTYPE_DOMAIN)
6368 : 181 : getDomainConstraints(fout, &(tyinfo[i]));
6369 : :
6370 : : /*
6371 : : * If it's a base type, make a DumpableObject representing a shell
6372 : : * definition of the type. We will need to dump that ahead of the I/O
6373 : : * functions for the type. Similarly, range types need a shell
6374 : : * definition in case they have a canonicalize function.
6375 : : *
6376 : : * Note: the shell type doesn't have a catId. You might think it
6377 : : * should copy the base type's catId, but then it might capture the
6378 : : * pg_depend entries for the type, which we don't want.
6379 : : */
6380 [ + + ]: 149348 : if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6381 [ + + ]: 16356 : (tyinfo[i].typtype == TYPTYPE_BASE ||
6382 [ + + ]: 7960 : tyinfo[i].typtype == TYPTYPE_RANGE))
6383 : : {
6384 : 8531 : stinfo = pg_malloc_object(ShellTypeInfo);
6385 : 8531 : stinfo->dobj.objType = DO_SHELL_TYPE;
6386 : 8531 : stinfo->dobj.catId = nilCatalogId;
6387 : 8531 : AssignDumpId(&stinfo->dobj);
6388 : 8531 : stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
6389 : 8531 : stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
6390 : 8531 : stinfo->baseType = &(tyinfo[i]);
6391 : 8531 : tyinfo[i].shellType = stinfo;
6392 : :
6393 : : /*
6394 : : * Initially mark the shell type as not to be dumped. We'll only
6395 : : * dump it if the I/O or canonicalize functions need to be dumped;
6396 : : * this is taken care of while sorting dependencies.
6397 : : */
6398 : 8531 : stinfo->dobj.dump = DUMP_COMPONENT_NONE;
6399 : : }
6400 : : }
6401 : :
6402 : 193 : PQclear(res);
6403 : :
6404 : 193 : destroyPQExpBuffer(query);
6405 : 193 : }
6406 : :
6407 : : /*
6408 : : * getOperators:
6409 : : * get information about all operators in the system catalogs
6410 : : */
6411 : : void
6412 : 193 : getOperators(Archive *fout)
6413 : : {
6414 : : PGresult *res;
6415 : : int ntups;
6416 : : int i;
6417 : 193 : PQExpBuffer query = createPQExpBuffer();
6418 : : OprInfo *oprinfo;
6419 : : int i_tableoid;
6420 : : int i_oid;
6421 : : int i_oprname;
6422 : : int i_oprnamespace;
6423 : : int i_oprowner;
6424 : : int i_oprkind;
6425 : : int i_oprleft;
6426 : : int i_oprright;
6427 : : int i_oprcode;
6428 : :
6429 : : /*
6430 : : * find all operators, including builtin operators; we filter out
6431 : : * system-defined operators at dump-out time.
6432 : : */
6433 : :
6434 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, oprname, "
6435 : : "oprnamespace, "
6436 : : "oprowner, "
6437 : : "oprkind, "
6438 : : "oprleft, "
6439 : : "oprright, "
6440 : : "oprcode::oid AS oprcode "
6441 : : "FROM pg_operator");
6442 : :
6443 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6444 : :
6445 : 193 : ntups = PQntuples(res);
6446 : :
6447 : 193 : oprinfo = pg_malloc_array(OprInfo, ntups);
6448 : :
6449 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6450 : 193 : i_oid = PQfnumber(res, "oid");
6451 : 193 : i_oprname = PQfnumber(res, "oprname");
6452 : 193 : i_oprnamespace = PQfnumber(res, "oprnamespace");
6453 : 193 : i_oprowner = PQfnumber(res, "oprowner");
6454 : 193 : i_oprkind = PQfnumber(res, "oprkind");
6455 : 193 : i_oprleft = PQfnumber(res, "oprleft");
6456 : 193 : i_oprright = PQfnumber(res, "oprright");
6457 : 193 : i_oprcode = PQfnumber(res, "oprcode");
6458 : :
6459 [ + + ]: 155703 : for (i = 0; i < ntups; i++)
6460 : : {
6461 : 155510 : oprinfo[i].dobj.objType = DO_OPERATOR;
6462 : 155510 : oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6463 : 155510 : oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6464 : 155510 : AssignDumpId(&oprinfo[i].dobj);
6465 : 155510 : oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
6466 : 311020 : oprinfo[i].dobj.namespace =
6467 : 155510 : findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)));
6468 : 155510 : oprinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_oprowner));
6469 : 155510 : oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
6470 : 155510 : oprinfo[i].oprleft = atooid(PQgetvalue(res, i, i_oprleft));
6471 : 155510 : oprinfo[i].oprright = atooid(PQgetvalue(res, i, i_oprright));
6472 : 155510 : oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
6473 : :
6474 : : /* Decide whether we want to dump it */
6475 : 155510 : selectDumpableObject(&(oprinfo[i].dobj), fout);
6476 : : }
6477 : :
6478 : 193 : PQclear(res);
6479 : :
6480 : 193 : destroyPQExpBuffer(query);
6481 : 193 : }
6482 : :
6483 : : /*
6484 : : * getCollations:
6485 : : * get information about all collations in the system catalogs
6486 : : */
6487 : : void
6488 : 193 : getCollations(Archive *fout)
6489 : : {
6490 : : PGresult *res;
6491 : : int ntups;
6492 : : int i;
6493 : : PQExpBuffer query;
6494 : : CollInfo *collinfo;
6495 : : int i_tableoid;
6496 : : int i_oid;
6497 : : int i_collname;
6498 : : int i_collnamespace;
6499 : : int i_collowner;
6500 : : int i_collencoding;
6501 : :
6502 : 193 : query = createPQExpBuffer();
6503 : :
6504 : : /*
6505 : : * find all collations, including builtin collations; we filter out
6506 : : * system-defined collations at dump-out time.
6507 : : */
6508 : :
6509 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, collname, "
6510 : : "collnamespace, "
6511 : : "collowner, "
6512 : : "collencoding "
6513 : : "FROM pg_collation");
6514 : :
6515 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6516 : :
6517 : 193 : ntups = PQntuples(res);
6518 : :
6519 : 193 : collinfo = pg_malloc_array(CollInfo, ntups);
6520 : :
6521 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6522 : 193 : i_oid = PQfnumber(res, "oid");
6523 : 193 : i_collname = PQfnumber(res, "collname");
6524 : 193 : i_collnamespace = PQfnumber(res, "collnamespace");
6525 : 193 : i_collowner = PQfnumber(res, "collowner");
6526 : 193 : i_collencoding = PQfnumber(res, "collencoding");
6527 : :
6528 [ + + ]: 170150 : for (i = 0; i < ntups; i++)
6529 : : {
6530 : 169957 : collinfo[i].dobj.objType = DO_COLLATION;
6531 : 169957 : collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6532 : 169957 : collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6533 : 169957 : AssignDumpId(&collinfo[i].dobj);
6534 : 169957 : collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
6535 : 339914 : collinfo[i].dobj.namespace =
6536 : 169957 : findNamespace(atooid(PQgetvalue(res, i, i_collnamespace)));
6537 : 169957 : collinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_collowner));
6538 : 169957 : collinfo[i].collencoding = atoi(PQgetvalue(res, i, i_collencoding));
6539 : :
6540 : : /* Decide whether we want to dump it */
6541 : 169957 : selectDumpableObject(&(collinfo[i].dobj), fout);
6542 : : }
6543 : :
6544 : 193 : PQclear(res);
6545 : :
6546 : 193 : destroyPQExpBuffer(query);
6547 : 193 : }
6548 : :
6549 : : /*
6550 : : * getConversions:
6551 : : * get information about all conversions in the system catalogs
6552 : : */
6553 : : void
6554 : 193 : getConversions(Archive *fout)
6555 : : {
6556 : : PGresult *res;
6557 : : int ntups;
6558 : : int i;
6559 : : PQExpBuffer query;
6560 : : ConvInfo *convinfo;
6561 : : int i_tableoid;
6562 : : int i_oid;
6563 : : int i_conname;
6564 : : int i_connamespace;
6565 : : int i_conowner;
6566 : :
6567 : 193 : query = createPQExpBuffer();
6568 : :
6569 : : /*
6570 : : * find all conversions, including builtin conversions; we filter out
6571 : : * system-defined conversions at dump-out time.
6572 : : */
6573 : :
6574 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, conname, "
6575 : : "connamespace, "
6576 : : "conowner "
6577 : : "FROM pg_conversion");
6578 : :
6579 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6580 : :
6581 : 193 : ntups = PQntuples(res);
6582 : :
6583 : 193 : convinfo = pg_malloc_array(ConvInfo, ntups);
6584 : :
6585 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6586 : 193 : i_oid = PQfnumber(res, "oid");
6587 : 193 : i_conname = PQfnumber(res, "conname");
6588 : 193 : i_connamespace = PQfnumber(res, "connamespace");
6589 : 193 : i_conowner = PQfnumber(res, "conowner");
6590 : :
6591 [ + + ]: 19155 : for (i = 0; i < ntups; i++)
6592 : : {
6593 : 18962 : convinfo[i].dobj.objType = DO_CONVERSION;
6594 : 18962 : convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6595 : 18962 : convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6596 : 18962 : AssignDumpId(&convinfo[i].dobj);
6597 : 18962 : convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
6598 : 37924 : convinfo[i].dobj.namespace =
6599 : 18962 : findNamespace(atooid(PQgetvalue(res, i, i_connamespace)));
6600 : 18962 : convinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_conowner));
6601 : :
6602 : : /* Decide whether we want to dump it */
6603 : 18962 : selectDumpableObject(&(convinfo[i].dobj), fout);
6604 : : }
6605 : :
6606 : 193 : PQclear(res);
6607 : :
6608 : 193 : destroyPQExpBuffer(query);
6609 : 193 : }
6610 : :
6611 : : /*
6612 : : * getAccessMethods:
6613 : : * get information about all user-defined access methods
6614 : : */
6615 : : void
6616 : 193 : getAccessMethods(Archive *fout)
6617 : : {
6618 : : PGresult *res;
6619 : : int ntups;
6620 : : int i;
6621 : : PQExpBuffer query;
6622 : : AccessMethodInfo *aminfo;
6623 : : int i_tableoid;
6624 : : int i_oid;
6625 : : int i_amname;
6626 : : int i_amhandler;
6627 : : int i_amtype;
6628 : :
6629 : 193 : query = createPQExpBuffer();
6630 : :
6631 : : /*
6632 : : * Select all access methods from pg_am table.
6633 : : */
6634 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, ");
6635 : 193 : appendPQExpBufferStr(query,
6636 : : "amtype, "
6637 : : "amhandler::pg_catalog.regproc AS amhandler ");
6638 : 193 : appendPQExpBufferStr(query, "FROM pg_am");
6639 : :
6640 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6641 : :
6642 : 193 : ntups = PQntuples(res);
6643 : :
6644 : 193 : aminfo = pg_malloc_array(AccessMethodInfo, ntups);
6645 : :
6646 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6647 : 193 : i_oid = PQfnumber(res, "oid");
6648 : 193 : i_amname = PQfnumber(res, "amname");
6649 : 193 : i_amhandler = PQfnumber(res, "amhandler");
6650 : 193 : i_amtype = PQfnumber(res, "amtype");
6651 : :
6652 [ + + ]: 1672 : for (i = 0; i < ntups; i++)
6653 : : {
6654 : 1479 : aminfo[i].dobj.objType = DO_ACCESS_METHOD;
6655 : 1479 : aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6656 : 1479 : aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6657 : 1479 : AssignDumpId(&aminfo[i].dobj);
6658 : 1479 : aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname));
6659 : 1479 : aminfo[i].dobj.namespace = NULL;
6660 : 1479 : aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler));
6661 : 1479 : aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype));
6662 : :
6663 : : /* Decide whether we want to dump it */
6664 : 1479 : selectDumpableAccessMethod(&(aminfo[i]), fout);
6665 : : }
6666 : :
6667 : 193 : PQclear(res);
6668 : :
6669 : 193 : destroyPQExpBuffer(query);
6670 : 193 : }
6671 : :
6672 : :
6673 : : /*
6674 : : * getOpclasses:
6675 : : * get information about all opclasses in the system catalogs
6676 : : */
6677 : : void
6678 : 193 : getOpclasses(Archive *fout)
6679 : : {
6680 : : PGresult *res;
6681 : : int ntups;
6682 : : int i;
6683 : 193 : PQExpBuffer query = createPQExpBuffer();
6684 : : OpclassInfo *opcinfo;
6685 : : int i_tableoid;
6686 : : int i_oid;
6687 : : int i_opcmethod;
6688 : : int i_opcname;
6689 : : int i_opcnamespace;
6690 : : int i_opcowner;
6691 : :
6692 : : /*
6693 : : * find all opclasses, including builtin opclasses; we filter out
6694 : : * system-defined opclasses at dump-out time.
6695 : : */
6696 : :
6697 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opcmethod, opcname, "
6698 : : "opcnamespace, "
6699 : : "opcowner "
6700 : : "FROM pg_opclass");
6701 : :
6702 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6703 : :
6704 : 193 : ntups = PQntuples(res);
6705 : :
6706 : 193 : opcinfo = pg_malloc_array(OpclassInfo, ntups);
6707 : :
6708 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6709 : 193 : i_oid = PQfnumber(res, "oid");
6710 : 193 : i_opcmethod = PQfnumber(res, "opcmethod");
6711 : 193 : i_opcname = PQfnumber(res, "opcname");
6712 : 193 : i_opcnamespace = PQfnumber(res, "opcnamespace");
6713 : 193 : i_opcowner = PQfnumber(res, "opcowner");
6714 : :
6715 [ + + ]: 34905 : for (i = 0; i < ntups; i++)
6716 : : {
6717 : 34712 : opcinfo[i].dobj.objType = DO_OPCLASS;
6718 : 34712 : opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6719 : 34712 : opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6720 : 34712 : AssignDumpId(&opcinfo[i].dobj);
6721 : 34712 : opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
6722 : 69424 : opcinfo[i].dobj.namespace =
6723 : 34712 : findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace)));
6724 : 34712 : opcinfo[i].opcmethod = atooid(PQgetvalue(res, i, i_opcmethod));
6725 : 34712 : opcinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opcowner));
6726 : :
6727 : : /* Decide whether we want to dump it */
6728 : 34712 : selectDumpableObject(&(opcinfo[i].dobj), fout);
6729 : : }
6730 : :
6731 : 193 : PQclear(res);
6732 : :
6733 : 193 : destroyPQExpBuffer(query);
6734 : 193 : }
6735 : :
6736 : : /*
6737 : : * getOpfamilies:
6738 : : * get information about all opfamilies in the system catalogs
6739 : : */
6740 : : void
6741 : 193 : getOpfamilies(Archive *fout)
6742 : : {
6743 : : PGresult *res;
6744 : : int ntups;
6745 : : int i;
6746 : : PQExpBuffer query;
6747 : : OpfamilyInfo *opfinfo;
6748 : : int i_tableoid;
6749 : : int i_oid;
6750 : : int i_opfmethod;
6751 : : int i_opfname;
6752 : : int i_opfnamespace;
6753 : : int i_opfowner;
6754 : :
6755 : 193 : query = createPQExpBuffer();
6756 : :
6757 : : /*
6758 : : * find all opfamilies, including builtin opfamilies; we filter out
6759 : : * system-defined opfamilies at dump-out time.
6760 : : */
6761 : :
6762 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, opfmethod, opfname, "
6763 : : "opfnamespace, "
6764 : : "opfowner "
6765 : : "FROM pg_opfamily");
6766 : :
6767 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6768 : :
6769 : 193 : ntups = PQntuples(res);
6770 : :
6771 : 193 : opfinfo = pg_malloc_array(OpfamilyInfo, ntups);
6772 : :
6773 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6774 : 193 : i_oid = PQfnumber(res, "oid");
6775 : 193 : i_opfname = PQfnumber(res, "opfname");
6776 : 193 : i_opfmethod = PQfnumber(res, "opfmethod");
6777 : 193 : i_opfnamespace = PQfnumber(res, "opfnamespace");
6778 : 193 : i_opfowner = PQfnumber(res, "opfowner");
6779 : :
6780 [ + + ]: 28902 : for (i = 0; i < ntups; i++)
6781 : : {
6782 : 28709 : opfinfo[i].dobj.objType = DO_OPFAMILY;
6783 : 28709 : opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6784 : 28709 : opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6785 : 28709 : AssignDumpId(&opfinfo[i].dobj);
6786 : 28709 : opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
6787 : 57418 : opfinfo[i].dobj.namespace =
6788 : 28709 : findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace)));
6789 : 28709 : opfinfo[i].opfmethod = atooid(PQgetvalue(res, i, i_opfmethod));
6790 : 28709 : opfinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opfowner));
6791 : :
6792 : : /* Decide whether we want to dump it */
6793 : 28709 : selectDumpableObject(&(opfinfo[i].dobj), fout);
6794 : : }
6795 : :
6796 : 193 : PQclear(res);
6797 : :
6798 : 193 : destroyPQExpBuffer(query);
6799 : 193 : }
6800 : :
6801 : : /*
6802 : : * getAggregates:
6803 : : * get information about all user-defined aggregates in the system catalogs
6804 : : */
6805 : : void
6806 : 193 : getAggregates(Archive *fout)
6807 : : {
6808 : 193 : DumpOptions *dopt = fout->dopt;
6809 : : PGresult *res;
6810 : : int ntups;
6811 : : int i;
6812 : 193 : PQExpBuffer query = createPQExpBuffer();
6813 : : AggInfo *agginfo;
6814 : : int i_tableoid;
6815 : : int i_oid;
6816 : : int i_aggname;
6817 : : int i_aggnamespace;
6818 : : int i_pronargs;
6819 : : int i_proargtypes;
6820 : : int i_proowner;
6821 : : int i_aggacl;
6822 : : int i_acldefault;
6823 : : const char *agg_check;
6824 : :
6825 : : /*
6826 : : * Find all interesting aggregates. See comment in getFuncs() for the
6827 : : * rationale behind the filtering logic.
6828 : : */
6829 : 386 : agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'"
6830 [ + - ]: 193 : : "p.proisagg");
6831 : :
6832 : 193 : appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, "
6833 : : "p.proname AS aggname, "
6834 : : "p.pronamespace AS aggnamespace, "
6835 : : "p.pronargs, p.proargtypes, "
6836 : : "p.proowner, "
6837 : : "p.proacl AS aggacl, "
6838 : : "acldefault('f', p.proowner) AS acldefault "
6839 : : "FROM pg_proc p "
6840 : : "LEFT JOIN pg_init_privs pip ON "
6841 : : "(p.oid = pip.objoid "
6842 : : "AND pip.classoid = 'pg_proc'::regclass "
6843 : : "AND pip.objsubid = 0) "
6844 : : "WHERE %s AND ("
6845 : : "p.pronamespace != "
6846 : : "(SELECT oid FROM pg_namespace "
6847 : : "WHERE nspname = 'pg_catalog') OR "
6848 : : "p.proacl IS DISTINCT FROM pip.initprivs",
6849 : : agg_check);
6850 [ + + ]: 193 : if (dopt->binary_upgrade)
6851 : 42 : appendPQExpBufferStr(query,
6852 : : " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
6853 : : "classid = 'pg_proc'::regclass AND "
6854 : : "objid = p.oid AND "
6855 : : "refclassid = 'pg_extension'::regclass AND "
6856 : : "deptype = 'e')");
6857 : 193 : appendPQExpBufferChar(query, ')');
6858 : :
6859 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6860 : :
6861 : 193 : ntups = PQntuples(res);
6862 : :
6863 : 193 : agginfo = pg_malloc_array(AggInfo, ntups);
6864 : :
6865 : 193 : i_tableoid = PQfnumber(res, "tableoid");
6866 : 193 : i_oid = PQfnumber(res, "oid");
6867 : 193 : i_aggname = PQfnumber(res, "aggname");
6868 : 193 : i_aggnamespace = PQfnumber(res, "aggnamespace");
6869 : 193 : i_pronargs = PQfnumber(res, "pronargs");
6870 : 193 : i_proargtypes = PQfnumber(res, "proargtypes");
6871 : 193 : i_proowner = PQfnumber(res, "proowner");
6872 : 193 : i_aggacl = PQfnumber(res, "aggacl");
6873 : 193 : i_acldefault = PQfnumber(res, "acldefault");
6874 : :
6875 [ + + ]: 595 : for (i = 0; i < ntups; i++)
6876 : : {
6877 : 402 : agginfo[i].aggfn.dobj.objType = DO_AGG;
6878 : 402 : agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6879 : 402 : agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6880 : 402 : AssignDumpId(&agginfo[i].aggfn.dobj);
6881 : 402 : agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
6882 : 804 : agginfo[i].aggfn.dobj.namespace =
6883 : 402 : findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace)));
6884 : 402 : agginfo[i].aggfn.dacl.acl = pg_strdup(PQgetvalue(res, i, i_aggacl));
6885 : 402 : agginfo[i].aggfn.dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
6886 : 402 : agginfo[i].aggfn.dacl.privtype = 0;
6887 : 402 : agginfo[i].aggfn.dacl.initprivs = NULL;
6888 : 402 : agginfo[i].aggfn.rolname = getRoleName(PQgetvalue(res, i, i_proowner));
6889 : 402 : agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
6890 : 402 : agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */
6891 : 402 : agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs));
6892 [ + + ]: 402 : if (agginfo[i].aggfn.nargs == 0)
6893 : 56 : agginfo[i].aggfn.argtypes = NULL;
6894 : : else
6895 : 346 : agginfo[i].aggfn.argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
6896 : 346 : agginfo[i].aggfn.nargs);
6897 : 402 : agginfo[i].aggfn.postponed_def = false; /* might get set during sort */
6898 : :
6899 : : /* Decide whether we want to dump it */
6900 : 402 : selectDumpableObject(&(agginfo[i].aggfn.dobj), fout);
6901 : :
6902 : : /* Mark whether aggregate has an ACL */
6903 [ + + ]: 402 : if (!PQgetisnull(res, i, i_aggacl))
6904 : 25 : agginfo[i].aggfn.dobj.components |= DUMP_COMPONENT_ACL;
6905 : : }
6906 : :
6907 : 193 : PQclear(res);
6908 : :
6909 : 193 : destroyPQExpBuffer(query);
6910 : 193 : }
6911 : :
6912 : : /*
6913 : : * getFuncs:
6914 : : * get information about all user-defined functions in the system catalogs
6915 : : */
6916 : : void
6917 : 193 : getFuncs(Archive *fout)
6918 : : {
6919 : 193 : DumpOptions *dopt = fout->dopt;
6920 : : PGresult *res;
6921 : : int ntups;
6922 : : int i;
6923 : 193 : PQExpBuffer query = createPQExpBuffer();
6924 : : FuncInfo *finfo;
6925 : : int i_tableoid;
6926 : : int i_oid;
6927 : : int i_proname;
6928 : : int i_pronamespace;
6929 : : int i_proowner;
6930 : : int i_prolang;
6931 : : int i_pronargs;
6932 : : int i_proargtypes;
6933 : : int i_prorettype;
6934 : : int i_proacl;
6935 : : int i_acldefault;
6936 : : const char *not_agg_check;
6937 : :
6938 : : /*
6939 : : * Find all interesting functions. This is a bit complicated:
6940 : : *
6941 : : * 1. Always exclude aggregates; those are handled elsewhere.
6942 : : *
6943 : : * 2. Always exclude functions that are internally dependent on something
6944 : : * else, since presumably those will be created as a result of creating
6945 : : * the something else. This currently acts only to suppress constructor
6946 : : * functions for range types. Note this is OK only because the
6947 : : * constructors don't have any dependencies the range type doesn't have;
6948 : : * otherwise we might not get creation ordering correct.
6949 : : *
6950 : : * 3. Otherwise, we normally exclude functions in pg_catalog. However, if
6951 : : * they're members of extensions and we are in binary-upgrade mode then
6952 : : * include them, since we want to dump extension members individually in
6953 : : * that mode. Also, if they are used by casts or transforms then we need
6954 : : * to gather the information about them, though they won't be dumped if
6955 : : * they are built-in. Also, include functions in pg_catalog if they have
6956 : : * an ACL different from what's shown in pg_init_privs (so we have to join
6957 : : * to pg_init_privs; annoying).
6958 : : */
6959 : 386 : not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'"
6960 [ + - ]: 193 : : "NOT p.proisagg");
6961 : :
6962 : 193 : appendPQExpBuffer(query,
6963 : : "SELECT p.tableoid, p.oid, p.proname, p.prolang, "
6964 : : "p.pronargs, p.proargtypes, p.prorettype, "
6965 : : "p.proacl, "
6966 : : "acldefault('f', p.proowner) AS acldefault, "
6967 : : "p.pronamespace, "
6968 : : "p.proowner "
6969 : : "FROM pg_proc p "
6970 : : "LEFT JOIN pg_init_privs pip ON "
6971 : : "(p.oid = pip.objoid "
6972 : : "AND pip.classoid = 'pg_proc'::regclass "
6973 : : "AND pip.objsubid = 0) "
6974 : : "WHERE %s"
6975 : : "\n AND NOT EXISTS (SELECT 1 FROM pg_depend "
6976 : : "WHERE classid = 'pg_proc'::regclass AND "
6977 : : "objid = p.oid AND deptype = 'i')"
6978 : : "\n AND ("
6979 : : "\n pronamespace != "
6980 : : "(SELECT oid FROM pg_namespace "
6981 : : "WHERE nspname = 'pg_catalog')"
6982 : : "\n OR EXISTS (SELECT 1 FROM pg_cast"
6983 : : "\n WHERE pg_cast.oid > %u "
6984 : : "\n AND p.oid = pg_cast.castfunc)"
6985 : : "\n OR EXISTS (SELECT 1 FROM pg_transform"
6986 : : "\n WHERE pg_transform.oid > %u AND "
6987 : : "\n (p.oid = pg_transform.trffromsql"
6988 : : "\n OR p.oid = pg_transform.trftosql))",
6989 : : not_agg_check,
6990 : : g_last_builtin_oid,
6991 : : g_last_builtin_oid);
6992 [ + + ]: 193 : if (dopt->binary_upgrade)
6993 : 42 : appendPQExpBufferStr(query,
6994 : : "\n OR EXISTS(SELECT 1 FROM pg_depend WHERE "
6995 : : "classid = 'pg_proc'::regclass AND "
6996 : : "objid = p.oid AND "
6997 : : "refclassid = 'pg_extension'::regclass AND "
6998 : : "deptype = 'e')");
6999 : 193 : appendPQExpBufferStr(query,
7000 : : "\n OR p.proacl IS DISTINCT FROM pip.initprivs");
7001 : 193 : appendPQExpBufferChar(query, ')');
7002 : :
7003 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7004 : :
7005 : 193 : ntups = PQntuples(res);
7006 : :
7007 : 193 : finfo = pg_malloc0_array(FuncInfo, ntups);
7008 : :
7009 : 193 : i_tableoid = PQfnumber(res, "tableoid");
7010 : 193 : i_oid = PQfnumber(res, "oid");
7011 : 193 : i_proname = PQfnumber(res, "proname");
7012 : 193 : i_pronamespace = PQfnumber(res, "pronamespace");
7013 : 193 : i_proowner = PQfnumber(res, "proowner");
7014 : 193 : i_prolang = PQfnumber(res, "prolang");
7015 : 193 : i_pronargs = PQfnumber(res, "pronargs");
7016 : 193 : i_proargtypes = PQfnumber(res, "proargtypes");
7017 : 193 : i_prorettype = PQfnumber(res, "prorettype");
7018 : 193 : i_proacl = PQfnumber(res, "proacl");
7019 : 193 : i_acldefault = PQfnumber(res, "acldefault");
7020 : :
7021 [ + + ]: 5181 : for (i = 0; i < ntups; i++)
7022 : : {
7023 : 4988 : finfo[i].dobj.objType = DO_FUNC;
7024 : 4988 : finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7025 : 4988 : finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7026 : 4988 : AssignDumpId(&finfo[i].dobj);
7027 : 4988 : finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
7028 : 9976 : finfo[i].dobj.namespace =
7029 : 4988 : findNamespace(atooid(PQgetvalue(res, i, i_pronamespace)));
7030 : 4988 : finfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_proacl));
7031 : 4988 : finfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7032 : 4988 : finfo[i].dacl.privtype = 0;
7033 : 4988 : finfo[i].dacl.initprivs = NULL;
7034 : 4988 : finfo[i].rolname = getRoleName(PQgetvalue(res, i, i_proowner));
7035 : 4988 : finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
7036 : 4988 : finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
7037 : 4988 : finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
7038 [ + + ]: 4988 : if (finfo[i].nargs == 0)
7039 : 1123 : finfo[i].argtypes = NULL;
7040 : : else
7041 : 3865 : finfo[i].argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes),
7042 : 3865 : finfo[i].nargs);
7043 : 4988 : finfo[i].postponed_def = false; /* might get set during sort */
7044 : :
7045 : : /* Decide whether we want to dump it */
7046 : 4988 : selectDumpableObject(&(finfo[i].dobj), fout);
7047 : :
7048 : : /* Mark whether function has an ACL */
7049 [ + + ]: 4988 : if (!PQgetisnull(res, i, i_proacl))
7050 : 146 : finfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7051 : : }
7052 : :
7053 : 193 : PQclear(res);
7054 : :
7055 : 193 : destroyPQExpBuffer(query);
7056 : 193 : }
7057 : :
7058 : : /*
7059 : : * getRelationStatistics
7060 : : * register the statistics object as a dependent of the relation.
7061 : : *
7062 : : * reltuples is passed as a string to avoid complexities in converting from/to
7063 : : * floating point.
7064 : : */
7065 : : static RelStatsInfo *
7066 : 10591 : getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
7067 : : char *reltuples, int32 relallvisible,
7068 : : int32 relallfrozen, char relkind,
7069 : : char **indAttNames, int nindAttNames)
7070 : : {
7071 [ + + ]: 10591 : if (!fout->dopt->dumpStatistics)
7072 : 6600 : return NULL;
7073 : :
7074 [ + + + + ]: 3991 : if ((relkind == RELKIND_RELATION) ||
7075 [ + + ]: 1662 : (relkind == RELKIND_PARTITIONED_TABLE) ||
7076 [ + + ]: 1002 : (relkind == RELKIND_INDEX) ||
7077 [ + + ]: 667 : (relkind == RELKIND_PARTITIONED_INDEX) ||
7078 [ + + ]: 337 : (relkind == RELKIND_MATVIEW ||
7079 : : relkind == RELKIND_FOREIGN_TABLE))
7080 : : {
7081 : 3689 : RelStatsInfo *info = pg_malloc0_object(RelStatsInfo);
7082 : 3689 : DumpableObject *dobj = &info->dobj;
7083 : :
7084 : 3689 : dobj->objType = DO_REL_STATS;
7085 : 3689 : dobj->catId.tableoid = 0;
7086 : 3689 : dobj->catId.oid = 0;
7087 : 3689 : AssignDumpId(dobj);
7088 : 3689 : dobj->dependencies = pg_malloc_object(DumpId);
7089 : 3689 : dobj->dependencies[0] = rel->dumpId;
7090 : 3689 : dobj->nDeps = 1;
7091 : 3689 : dobj->allocDeps = 1;
7092 : 3689 : dobj->components |= DUMP_COMPONENT_STATISTICS;
7093 : 3689 : dobj->name = pg_strdup(rel->name);
7094 : 3689 : dobj->namespace = rel->namespace;
7095 : 3689 : info->relid = rel->catId.oid;
7096 : 3689 : info->relpages = relpages;
7097 : 3689 : info->reltuples = pstrdup(reltuples);
7098 : 3689 : info->relallvisible = relallvisible;
7099 : 3689 : info->relallfrozen = relallfrozen;
7100 : 3689 : info->relkind = relkind;
7101 : 3689 : info->indAttNames = indAttNames;
7102 : 3689 : info->nindAttNames = nindAttNames;
7103 : :
7104 : : /*
7105 : : * Ordinarily, stats go in SECTION_DATA for tables and
7106 : : * SECTION_POST_DATA for indexes.
7107 : : *
7108 : : * However, the section may be updated later for materialized view
7109 : : * stats. REFRESH MATERIALIZED VIEW replaces the storage and resets
7110 : : * the stats, so the stats must be restored after the data. Also, the
7111 : : * materialized view definition may be postponed to SECTION_POST_DATA
7112 : : * (see repairMatViewBoundaryMultiLoop()).
7113 : : */
7114 [ + + - ]: 3689 : switch (info->relkind)
7115 : : {
7116 : 2694 : case RELKIND_RELATION:
7117 : : case RELKIND_PARTITIONED_TABLE:
7118 : : case RELKIND_MATVIEW:
7119 : : case RELKIND_FOREIGN_TABLE:
7120 : 2694 : info->section = SECTION_DATA;
7121 : 2694 : break;
7122 : 995 : case RELKIND_INDEX:
7123 : : case RELKIND_PARTITIONED_INDEX:
7124 : 995 : info->section = SECTION_POST_DATA;
7125 : 995 : break;
7126 : 0 : default:
7127 : 0 : pg_fatal("cannot dump statistics for relation kind \"%c\"",
7128 : : info->relkind);
7129 : : }
7130 : :
7131 : 3689 : return info;
7132 : : }
7133 : 302 : return NULL;
7134 : : }
7135 : :
7136 : : /*
7137 : : * getTables
7138 : : * read all the tables (no indexes) in the system catalogs,
7139 : : * and return them as an array of TableInfo structures
7140 : : *
7141 : : * *numTables is set to the number of tables read in
7142 : : */
7143 : : TableInfo *
7144 : 194 : getTables(Archive *fout, int *numTables)
7145 : : {
7146 : 194 : DumpOptions *dopt = fout->dopt;
7147 : : PGresult *res;
7148 : : int ntups;
7149 : : int i;
7150 : 194 : PQExpBuffer query = createPQExpBuffer();
7151 : : TableInfo *tblinfo;
7152 : : int i_reltableoid;
7153 : : int i_reloid;
7154 : : int i_relname;
7155 : : int i_relnamespace;
7156 : : int i_relkind;
7157 : : int i_reltype;
7158 : : int i_relowner;
7159 : : int i_relchecks;
7160 : : int i_relhasindex;
7161 : : int i_relhasrules;
7162 : : int i_relpages;
7163 : : int i_reltuples;
7164 : : int i_relallvisible;
7165 : : int i_relallfrozen;
7166 : : int i_toastpages;
7167 : : int i_owning_tab;
7168 : : int i_owning_col;
7169 : : int i_reltablespace;
7170 : : int i_relhasoids;
7171 : : int i_relhastriggers;
7172 : : int i_relpersistence;
7173 : : int i_relispopulated;
7174 : : int i_relreplident;
7175 : : int i_relrowsec;
7176 : : int i_relforcerowsec;
7177 : : int i_relfrozenxid;
7178 : : int i_toastfrozenxid;
7179 : : int i_toastoid;
7180 : : int i_relminmxid;
7181 : : int i_toastminmxid;
7182 : : int i_reloptions;
7183 : : int i_checkoption;
7184 : : int i_toastreloptions;
7185 : : int i_reloftype;
7186 : : int i_foreignserver;
7187 : : int i_amname;
7188 : : int i_is_identity_sequence;
7189 : : int i_relacl;
7190 : : int i_acldefault;
7191 : : int i_ispartition;
7192 : :
7193 : : /*
7194 : : * Find all the tables and table-like objects.
7195 : : *
7196 : : * We must fetch all tables in this phase because otherwise we cannot
7197 : : * correctly identify inherited columns, owned sequences, etc.
7198 : : *
7199 : : * We include system catalogs, so that we can work if a user table is
7200 : : * defined to inherit from a system catalog (pretty weird, but...)
7201 : : *
7202 : : * Note: in this phase we should collect only a minimal amount of
7203 : : * information about each table, basically just enough to decide if it is
7204 : : * interesting. In particular, since we do not yet have lock on any user
7205 : : * table, we MUST NOT invoke any server-side data collection functions
7206 : : * (for instance, pg_get_partkeydef()). Those are likely to fail or give
7207 : : * wrong answers if any concurrent DDL is happening.
7208 : : */
7209 : :
7210 : 194 : appendPQExpBufferStr(query,
7211 : : "SELECT c.tableoid, c.oid, c.relname, "
7212 : : "c.relnamespace, c.relkind, c.reltype, "
7213 : : "c.relowner, "
7214 : : "c.relchecks, "
7215 : : "c.relhasindex, c.relhasrules, c.relpages, "
7216 : : "c.reltuples, c.relallvisible, ");
7217 : :
7218 [ + - ]: 194 : if (fout->remoteVersion >= 180000)
7219 : 194 : appendPQExpBufferStr(query, "c.relallfrozen, ");
7220 : : else
7221 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7222 : :
7223 : 194 : appendPQExpBufferStr(query,
7224 : : "c.relhastriggers, c.relpersistence, "
7225 : : "c.reloftype, "
7226 : : "c.relacl, "
7227 : : "acldefault(CASE"
7228 : : " WHEN c.relkind = " CppAsString2(RELKIND_PROPGRAPH));
7229 : : /* 19beta1 didn't support acldefault('g'), so we'll fix that below */
7230 : 194 : appendPQExpBufferStr(query,
7231 [ + - ]: 194 : fout->remoteVersion >= 200000 ?
7232 : : " THEN 'g'::\"char\"" :
7233 : : " THEN NULL");
7234 : 194 : appendPQExpBufferStr(query,
7235 : : " WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
7236 : : " THEN 's'::\"char\""
7237 : : " ELSE 'r'::\"char\" END, c.relowner) AS acldefault, "
7238 : : "CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN "
7239 : : "(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) "
7240 : : "ELSE 0 END AS foreignserver, "
7241 : : "c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, "
7242 : : "tc.oid AS toid, "
7243 : : "tc.relpages AS toastpages, "
7244 : : "tc.reloptions AS toast_reloptions, "
7245 : : "d.refobjid AS owning_tab, "
7246 : : "d.refobjsubid AS owning_col, "
7247 : : "tsp.spcname AS reltablespace, ");
7248 : :
7249 [ + - ]: 194 : if (fout->remoteVersion >= 120000)
7250 : 194 : appendPQExpBufferStr(query,
7251 : : "false AS relhasoids, ");
7252 : : else
7253 : 0 : appendPQExpBufferStr(query,
7254 : : "c.relhasoids, ");
7255 : :
7256 : 194 : appendPQExpBufferStr(query,
7257 : : "c.relispopulated, ");
7258 : :
7259 : 194 : appendPQExpBufferStr(query,
7260 : : "c.relreplident, ");
7261 : :
7262 : 194 : appendPQExpBufferStr(query,
7263 : : "c.relrowsecurity, c.relforcerowsecurity, ");
7264 : :
7265 : 194 : appendPQExpBufferStr(query,
7266 : : "c.relminmxid, tc.relminmxid AS tminmxid, ");
7267 : :
7268 : 194 : appendPQExpBufferStr(query,
7269 : : "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
7270 : : "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
7271 : : "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, ");
7272 : :
7273 : 194 : appendPQExpBufferStr(query,
7274 : : "am.amname, ");
7275 : :
7276 : 194 : appendPQExpBufferStr(query,
7277 : : "(d.deptype = 'i') IS TRUE AS is_identity_sequence, ");
7278 : :
7279 : 194 : appendPQExpBufferStr(query,
7280 : : "c.relispartition AS ispartition ");
7281 : :
7282 : : /*
7283 : : * Left join to pg_depend to pick up dependency info linking sequences to
7284 : : * their owning column, if any (note this dependency is AUTO except for
7285 : : * identity sequences, where it's INTERNAL). Also join to pg_tablespace to
7286 : : * collect the spcname.
7287 : : */
7288 : 194 : appendPQExpBufferStr(query,
7289 : : "\nFROM pg_class c\n"
7290 : : "LEFT JOIN pg_depend d ON "
7291 : : "(c.relkind = " CppAsString2(RELKIND_SEQUENCE) " AND "
7292 : : "d.classid = 'pg_class'::regclass AND d.objid = c.oid AND "
7293 : : "d.objsubid = 0 AND "
7294 : : "d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i'))\n"
7295 : : "LEFT JOIN pg_tablespace tsp ON (tsp.oid = c.reltablespace)\n");
7296 : :
7297 : : /*
7298 : : * Left join to pg_am to pick up the amname.
7299 : : */
7300 : 194 : appendPQExpBufferStr(query,
7301 : : "LEFT JOIN pg_am am ON (c.relam = am.oid)\n");
7302 : :
7303 : : /*
7304 : : * We purposefully ignore toast OIDs for partitioned tables; the reason is
7305 : : * that versions 10 and 11 have them, but later versions do not, so
7306 : : * emitting them causes the upgrade to fail.
7307 : : */
7308 : 194 : appendPQExpBufferStr(query,
7309 : : "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid"
7310 : : " AND tc.relkind = " CppAsString2(RELKIND_TOASTVALUE)
7311 : : " AND c.relkind <> " CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
7312 : :
7313 : : /*
7314 : : * Restrict to interesting relkinds (in particular, not indexes). Not all
7315 : : * relkinds are possible in older servers, but it's not worth the trouble
7316 : : * to emit a version-dependent list.
7317 : : *
7318 : : * Composite-type table entries won't be dumped as such, but we have to
7319 : : * make a DumpableObject for them so that we can track dependencies of the
7320 : : * composite type (pg_depend entries for columns of the composite type
7321 : : * link to the pg_class entry not the pg_type entry).
7322 : : */
7323 : 194 : appendPQExpBufferStr(query,
7324 : : "WHERE c.relkind IN ("
7325 : : CppAsString2(RELKIND_RELATION) ", "
7326 : : CppAsString2(RELKIND_SEQUENCE) ", "
7327 : : CppAsString2(RELKIND_VIEW) ", "
7328 : : CppAsString2(RELKIND_COMPOSITE_TYPE) ", "
7329 : : CppAsString2(RELKIND_MATVIEW) ", "
7330 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
7331 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
7332 : : CppAsString2(RELKIND_PROPGRAPH) ")\n"
7333 : : "ORDER BY c.oid");
7334 : :
7335 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7336 : :
7337 : 194 : ntups = PQntuples(res);
7338 : :
7339 : 194 : *numTables = ntups;
7340 : :
7341 : : /*
7342 : : * Extract data from result and lock dumpable tables. We do the locking
7343 : : * before anything else, to minimize the window wherein a table could
7344 : : * disappear under us.
7345 : : *
7346 : : * Note that we have to save info about all tables here, even when dumping
7347 : : * only one, because we don't yet know which tables might be inheritance
7348 : : * ancestors of the target table.
7349 : : */
7350 : 194 : tblinfo = pg_malloc0_array(TableInfo, ntups);
7351 : :
7352 : 194 : i_reltableoid = PQfnumber(res, "tableoid");
7353 : 194 : i_reloid = PQfnumber(res, "oid");
7354 : 194 : i_relname = PQfnumber(res, "relname");
7355 : 194 : i_relnamespace = PQfnumber(res, "relnamespace");
7356 : 194 : i_relkind = PQfnumber(res, "relkind");
7357 : 194 : i_reltype = PQfnumber(res, "reltype");
7358 : 194 : i_relowner = PQfnumber(res, "relowner");
7359 : 194 : i_relchecks = PQfnumber(res, "relchecks");
7360 : 194 : i_relhasindex = PQfnumber(res, "relhasindex");
7361 : 194 : i_relhasrules = PQfnumber(res, "relhasrules");
7362 : 194 : i_relpages = PQfnumber(res, "relpages");
7363 : 194 : i_reltuples = PQfnumber(res, "reltuples");
7364 : 194 : i_relallvisible = PQfnumber(res, "relallvisible");
7365 : 194 : i_relallfrozen = PQfnumber(res, "relallfrozen");
7366 : 194 : i_toastpages = PQfnumber(res, "toastpages");
7367 : 194 : i_owning_tab = PQfnumber(res, "owning_tab");
7368 : 194 : i_owning_col = PQfnumber(res, "owning_col");
7369 : 194 : i_reltablespace = PQfnumber(res, "reltablespace");
7370 : 194 : i_relhasoids = PQfnumber(res, "relhasoids");
7371 : 194 : i_relhastriggers = PQfnumber(res, "relhastriggers");
7372 : 194 : i_relpersistence = PQfnumber(res, "relpersistence");
7373 : 194 : i_relispopulated = PQfnumber(res, "relispopulated");
7374 : 194 : i_relreplident = PQfnumber(res, "relreplident");
7375 : 194 : i_relrowsec = PQfnumber(res, "relrowsecurity");
7376 : 194 : i_relforcerowsec = PQfnumber(res, "relforcerowsecurity");
7377 : 194 : i_relfrozenxid = PQfnumber(res, "relfrozenxid");
7378 : 194 : i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
7379 : 194 : i_toastoid = PQfnumber(res, "toid");
7380 : 194 : i_relminmxid = PQfnumber(res, "relminmxid");
7381 : 194 : i_toastminmxid = PQfnumber(res, "tminmxid");
7382 : 194 : i_reloptions = PQfnumber(res, "reloptions");
7383 : 194 : i_checkoption = PQfnumber(res, "checkoption");
7384 : 194 : i_toastreloptions = PQfnumber(res, "toast_reloptions");
7385 : 194 : i_reloftype = PQfnumber(res, "reloftype");
7386 : 194 : i_foreignserver = PQfnumber(res, "foreignserver");
7387 : 194 : i_amname = PQfnumber(res, "amname");
7388 : 194 : i_is_identity_sequence = PQfnumber(res, "is_identity_sequence");
7389 : 194 : i_relacl = PQfnumber(res, "relacl");
7390 : 194 : i_acldefault = PQfnumber(res, "acldefault");
7391 : 194 : i_ispartition = PQfnumber(res, "ispartition");
7392 : :
7393 [ + + ]: 194 : if (dopt->lockWaitTimeout)
7394 : : {
7395 : : /*
7396 : : * Arrange to fail instead of waiting forever for a table lock.
7397 : : *
7398 : : * NB: this coding assumes that the only queries issued within the
7399 : : * following loop are LOCK TABLEs; else the timeout may be undesirably
7400 : : * applied to other things too.
7401 : : */
7402 : 2 : resetPQExpBuffer(query);
7403 : 2 : appendPQExpBufferStr(query, "SET statement_timeout = ");
7404 : 2 : appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout));
7405 : 2 : ExecuteSqlStatement(fout, query->data);
7406 : : }
7407 : :
7408 : 194 : resetPQExpBuffer(query);
7409 : :
7410 [ + + ]: 55968 : for (i = 0; i < ntups; i++)
7411 : : {
7412 : 55774 : int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible));
7413 : 55774 : int32 relallfrozen = atoi(PQgetvalue(res, i, i_relallfrozen));
7414 : :
7415 : 55774 : tblinfo[i].dobj.objType = DO_TABLE;
7416 : 55774 : tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
7417 : 55774 : tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
7418 : 55774 : AssignDumpId(&tblinfo[i].dobj);
7419 : 55774 : tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
7420 : 111548 : tblinfo[i].dobj.namespace =
7421 : 55774 : findNamespace(atooid(PQgetvalue(res, i, i_relnamespace)));
7422 : 55774 : tblinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_relacl));
7423 : : /* acldefault computed below */
7424 : 55774 : tblinfo[i].dacl.privtype = 0;
7425 : 55774 : tblinfo[i].dacl.initprivs = NULL;
7426 : 55774 : tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
7427 : 55774 : tblinfo[i].reltype = atooid(PQgetvalue(res, i, i_reltype));
7428 : 55774 : tblinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_relowner));
7429 : 55774 : tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
7430 : 55774 : tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
7431 : 55774 : tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
7432 : 55774 : tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages));
7433 [ + + ]: 55774 : if (PQgetisnull(res, i, i_toastpages))
7434 : 45289 : tblinfo[i].toastpages = 0;
7435 : : else
7436 : 10485 : tblinfo[i].toastpages = atoi(PQgetvalue(res, i, i_toastpages));
7437 [ + + ]: 55774 : if (PQgetisnull(res, i, i_owning_tab))
7438 : : {
7439 : 55350 : tblinfo[i].owning_tab = InvalidOid;
7440 : 55350 : tblinfo[i].owning_col = 0;
7441 : : }
7442 : : else
7443 : : {
7444 : 424 : tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
7445 : 424 : tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
7446 : : }
7447 : 55774 : tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace));
7448 : 55774 : tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
7449 : 55774 : tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
7450 : 55774 : tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence));
7451 : 55774 : tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0);
7452 : 55774 : tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident));
7453 : 55774 : tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0);
7454 : 55774 : tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0);
7455 : 55774 : tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
7456 : 55774 : tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
7457 : 55774 : tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
7458 : 55774 : tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid));
7459 : 55774 : tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid));
7460 : 55774 : tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions));
7461 [ + + ]: 55774 : if (PQgetisnull(res, i, i_checkoption))
7462 : 55725 : tblinfo[i].checkoption = NULL;
7463 : : else
7464 : 49 : tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption));
7465 : 55774 : tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions));
7466 : 55774 : tblinfo[i].reloftype = atooid(PQgetvalue(res, i, i_reloftype));
7467 : 55774 : tblinfo[i].foreign_server = atooid(PQgetvalue(res, i, i_foreignserver));
7468 [ + + ]: 55774 : if (PQgetisnull(res, i, i_amname))
7469 : 33901 : tblinfo[i].amname = NULL;
7470 : : else
7471 : 21873 : tblinfo[i].amname = pg_strdup(PQgetvalue(res, i, i_amname));
7472 : 55774 : tblinfo[i].is_identity_sequence = (strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0);
7473 : 55774 : tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0);
7474 : :
7475 [ + + ]: 55774 : if (tblinfo[i].relkind == RELKIND_PROPGRAPH &&
7476 [ - + ]: 147 : !(fout->remoteVersion >= 200000))
7477 : 0 : {
7478 : 0 : PQExpBuffer aclarray = createPQExpBuffer();
7479 : 0 : PQExpBuffer aclitem = createPQExpBuffer();
7480 : :
7481 : : /* Standard ACL as of v19 is {owner=r/owner} */
7482 : 0 : appendPQExpBufferChar(aclarray, '{');
7483 : 0 : quoteAclUserName(aclitem, tblinfo[i].rolname);
7484 : 0 : appendPQExpBufferStr(aclitem, "=r/");
7485 : 0 : quoteAclUserName(aclitem, tblinfo[i].rolname);
7486 : 0 : appendPGArray(aclarray, aclitem->data);
7487 : 0 : appendPQExpBufferChar(aclarray, '}');
7488 : :
7489 : 0 : tblinfo[i].dacl.acldefault = pstrdup(aclarray->data);
7490 : :
7491 : 0 : destroyPQExpBuffer(aclarray);
7492 : 0 : destroyPQExpBuffer(aclitem);
7493 : : }
7494 : : else
7495 : 55774 : tblinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
7496 : :
7497 : : /* other fields were zeroed above */
7498 : :
7499 : : /*
7500 : : * Decide whether we want to dump this table.
7501 : : */
7502 [ + + ]: 55774 : if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
7503 : 186 : tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE;
7504 : : else
7505 : 55588 : selectDumpableTable(&tblinfo[i], fout);
7506 : :
7507 : : /*
7508 : : * Now, consider the table "interesting" if we need to dump its
7509 : : * definition, data or its statistics. Later on, we'll skip a lot of
7510 : : * data collection for uninteresting tables.
7511 : : *
7512 : : * Note: the "interesting" flag will also be set by flagInhTables for
7513 : : * parents of interesting tables, so that we collect necessary
7514 : : * inheritance info even when the parents are not themselves being
7515 : : * dumped. This is the main reason why we need an "interesting" flag
7516 : : * that's separate from the components-to-dump bitmask.
7517 : : */
7518 : 55774 : tblinfo[i].interesting = (tblinfo[i].dobj.dump &
7519 : : (DUMP_COMPONENT_DEFINITION |
7520 : : DUMP_COMPONENT_DATA |
7521 : 55774 : DUMP_COMPONENT_STATISTICS)) != 0;
7522 : :
7523 : 55774 : tblinfo[i].dummy_view = false; /* might get set during sort */
7524 : 55774 : tblinfo[i].postponed_def = false; /* might get set during sort */
7525 : :
7526 : : /* Tables have data */
7527 : 55774 : tblinfo[i].dobj.components |= DUMP_COMPONENT_DATA;
7528 : :
7529 : : /* Mark whether table has an ACL */
7530 [ + + ]: 55774 : if (!PQgetisnull(res, i, i_relacl))
7531 : 45103 : tblinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
7532 : 55774 : tblinfo[i].hascolumnACLs = false; /* may get set later */
7533 : :
7534 : : /* Add statistics */
7535 [ + + ]: 55774 : if (tblinfo[i].interesting)
7536 : : {
7537 : : RelStatsInfo *stats;
7538 : :
7539 : 15460 : stats = getRelationStatistics(fout, &tblinfo[i].dobj,
7540 : 7730 : tblinfo[i].relpages,
7541 : : PQgetvalue(res, i, i_reltuples),
7542 : : relallvisible, relallfrozen,
7543 : 7730 : tblinfo[i].relkind, NULL, 0);
7544 [ + + ]: 7730 : if (tblinfo[i].relkind == RELKIND_MATVIEW)
7545 : 425 : tblinfo[i].stats = stats;
7546 : : }
7547 : :
7548 : : /*
7549 : : * Read-lock target tables to make sure they aren't DROPPED or altered
7550 : : * in schema before we get around to dumping them.
7551 : : *
7552 : : * Note that we don't explicitly lock parents of the target tables; we
7553 : : * assume our lock on the child is enough to prevent schema
7554 : : * alterations to parent tables.
7555 : : *
7556 : : * NOTE: it'd be kinda nice to lock other relations too, not only
7557 : : * plain or partitioned tables, but the backend doesn't presently
7558 : : * allow that.
7559 : : *
7560 : : * We only need to lock the table for certain components; see
7561 : : * pg_dump.h
7562 : : */
7563 [ + + ]: 55774 : if ((tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK) &&
7564 [ + + ]: 7730 : (tblinfo[i].relkind == RELKIND_RELATION ||
7565 [ + + ]: 2207 : tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE))
7566 : : {
7567 : : /*
7568 : : * Tables are locked in batches. When dumping from a remote
7569 : : * server this can save a significant amount of time by reducing
7570 : : * the number of round trips.
7571 : : */
7572 [ + + ]: 6163 : if (query->len == 0)
7573 : 127 : appendPQExpBuffer(query, "LOCK TABLE %s",
7574 : 127 : fmtQualifiedDumpable(&tblinfo[i]));
7575 : : else
7576 : : {
7577 : 6036 : appendPQExpBuffer(query, ", %s",
7578 : 6036 : fmtQualifiedDumpable(&tblinfo[i]));
7579 : :
7580 : : /* Arbitrarily end a batch when query length reaches 100K. */
7581 [ - + ]: 6036 : if (query->len >= 100000)
7582 : : {
7583 : : /* Lock another batch of tables. */
7584 : 0 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7585 : 0 : ExecuteSqlStatement(fout, query->data);
7586 : 0 : resetPQExpBuffer(query);
7587 : : }
7588 : : }
7589 : : }
7590 : : }
7591 : :
7592 [ + + ]: 194 : if (query->len != 0)
7593 : : {
7594 : : /* Lock the tables in the last batch. */
7595 : 127 : appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
7596 : 127 : ExecuteSqlStatement(fout, query->data);
7597 : : }
7598 : :
7599 [ + + ]: 193 : if (dopt->lockWaitTimeout)
7600 : : {
7601 : 2 : ExecuteSqlStatement(fout, "SET statement_timeout = 0");
7602 : : }
7603 : :
7604 : 193 : PQclear(res);
7605 : :
7606 : 193 : destroyPQExpBuffer(query);
7607 : :
7608 : 193 : return tblinfo;
7609 : : }
7610 : :
7611 : : /*
7612 : : * getOwnedSeqs
7613 : : * identify owned sequences and mark them as dumpable if owning table is
7614 : : *
7615 : : * We used to do this in getTables(), but it's better to do it after the
7616 : : * index used by findTableByOid() has been set up.
7617 : : */
7618 : : void
7619 : 193 : getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables)
7620 : : {
7621 : : int i;
7622 : :
7623 : : /*
7624 : : * Force sequences that are "owned" by table columns to be dumped whenever
7625 : : * their owning table is being dumped.
7626 : : */
7627 [ + + ]: 55672 : for (i = 0; i < numTables; i++)
7628 : : {
7629 : 55479 : TableInfo *seqinfo = &tblinfo[i];
7630 : : TableInfo *owning_tab;
7631 : :
7632 [ + + ]: 55479 : if (!OidIsValid(seqinfo->owning_tab))
7633 : 55058 : continue; /* not an owned sequence */
7634 : :
7635 : 421 : owning_tab = findTableByOid(seqinfo->owning_tab);
7636 [ - + ]: 421 : if (owning_tab == NULL)
7637 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
7638 : : seqinfo->owning_tab, seqinfo->dobj.catId.oid);
7639 : :
7640 : : /*
7641 : : * For an identity sequence, dump exactly the same components for the
7642 : : * sequence as for the owning table. This is important because we
7643 : : * treat the identity sequence as an integral part of the table. For
7644 : : * example, there is not any DDL command that allows creation of such
7645 : : * a sequence independently of the table.
7646 : : *
7647 : : * For other owned sequences such as serial sequences, we need to dump
7648 : : * the components that are being dumped for the table and any
7649 : : * components that the sequence is explicitly marked with.
7650 : : *
7651 : : * We can't simply use the set of components which are being dumped
7652 : : * for the table as the table might be in an extension (and only the
7653 : : * non-extension components, eg: ACLs if changed, security labels, and
7654 : : * policies, are being dumped) while the sequence is not (and
7655 : : * therefore the definition and other components should also be
7656 : : * dumped).
7657 : : *
7658 : : * If the sequence is part of the extension then it should be properly
7659 : : * marked by checkExtensionMembership() and this will be a no-op as
7660 : : * the table will be equivalently marked.
7661 : : */
7662 [ + + ]: 421 : if (seqinfo->is_identity_sequence)
7663 : 202 : seqinfo->dobj.dump = owning_tab->dobj.dump;
7664 : : else
7665 : 219 : seqinfo->dobj.dump |= owning_tab->dobj.dump;
7666 : :
7667 : : /* Make sure that necessary data is available if we're dumping it */
7668 [ + + ]: 421 : if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE)
7669 : : {
7670 : 325 : seqinfo->interesting = true;
7671 : 325 : owning_tab->interesting = true;
7672 : : }
7673 : : }
7674 : 193 : }
7675 : :
7676 : : /*
7677 : : * getInherits
7678 : : * read all the inheritance information
7679 : : * from the system catalogs return them in the InhInfo* structure
7680 : : *
7681 : : * numInherits is set to the number of pairs read in
7682 : : */
7683 : : InhInfo *
7684 : 193 : getInherits(Archive *fout, int *numInherits)
7685 : : {
7686 : : PGresult *res;
7687 : : int ntups;
7688 : : int i;
7689 : 193 : PQExpBuffer query = createPQExpBuffer();
7690 : : InhInfo *inhinfo;
7691 : :
7692 : : int i_inhrelid;
7693 : : int i_inhparent;
7694 : :
7695 : : /* find all the inheritance information */
7696 : 193 : appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits");
7697 : :
7698 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7699 : :
7700 : 193 : ntups = PQntuples(res);
7701 : :
7702 : 193 : *numInherits = ntups;
7703 : :
7704 : 193 : inhinfo = pg_malloc_array(InhInfo, ntups);
7705 : :
7706 : 193 : i_inhrelid = PQfnumber(res, "inhrelid");
7707 : 193 : i_inhparent = PQfnumber(res, "inhparent");
7708 : :
7709 [ + + ]: 3835 : for (i = 0; i < ntups; i++)
7710 : : {
7711 : 3642 : inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
7712 : 3642 : inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
7713 : : }
7714 : :
7715 : 193 : PQclear(res);
7716 : :
7717 : 193 : destroyPQExpBuffer(query);
7718 : :
7719 : 193 : return inhinfo;
7720 : : }
7721 : :
7722 : : /*
7723 : : * getPartitioningInfo
7724 : : * get information about partitioning
7725 : : *
7726 : : * For the most part, we only collect partitioning info about tables we
7727 : : * intend to dump. However, this function has to consider all partitioned
7728 : : * tables in the database, because we need to know about parents of partitions
7729 : : * we are going to dump even if the parents themselves won't be dumped.
7730 : : *
7731 : : * Specifically, what we need to know is whether each partitioned table
7732 : : * has an "unsafe" partitioning scheme that requires us to force
7733 : : * load-via-partition-root mode for its children. Currently the only case
7734 : : * for which we force that is hash partitioning on enum columns, since the
7735 : : * hash codes depend on enum value OIDs which won't be replicated across
7736 : : * dump-and-reload. There are other cases in which load-via-partition-root
7737 : : * might be necessary, but we expect users to cope with them.
7738 : : */
7739 : : void
7740 : 193 : getPartitioningInfo(Archive *fout)
7741 : : {
7742 : : PQExpBuffer query;
7743 : : PGresult *res;
7744 : : int ntups;
7745 : :
7746 : : /* hash partitioning didn't exist before v11 */
7747 [ - + ]: 193 : if (fout->remoteVersion < 110000)
7748 : 0 : return;
7749 : : /* needn't bother if not dumping data */
7750 [ + + ]: 193 : if (!fout->dopt->dumpData)
7751 : 47 : return;
7752 : :
7753 : 146 : query = createPQExpBuffer();
7754 : :
7755 : : /*
7756 : : * Unsafe partitioning schemes are exactly those for which hash enum_ops
7757 : : * appears among the partition opclasses. We needn't check partstrat.
7758 : : *
7759 : : * Note that this query may well retrieve info about tables we aren't
7760 : : * going to dump and hence have no lock on. That's okay since we need not
7761 : : * invoke any unsafe server-side functions.
7762 : : */
7763 : 146 : appendPQExpBufferStr(query,
7764 : : "SELECT partrelid FROM pg_partitioned_table WHERE\n"
7765 : : "(SELECT c.oid FROM pg_opclass c JOIN pg_am a "
7766 : : "ON c.opcmethod = a.oid\n"
7767 : : "WHERE opcname = 'enum_ops' "
7768 : : "AND opcnamespace = 'pg_catalog'::regnamespace "
7769 : : "AND amname = 'hash') = ANY(partclass)");
7770 : :
7771 : 146 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7772 : :
7773 : 146 : ntups = PQntuples(res);
7774 : :
7775 [ + + ]: 191 : for (int i = 0; i < ntups; i++)
7776 : : {
7777 : 45 : Oid tabrelid = atooid(PQgetvalue(res, i, 0));
7778 : : TableInfo *tbinfo;
7779 : :
7780 : 45 : tbinfo = findTableByOid(tabrelid);
7781 [ - + ]: 45 : if (tbinfo == NULL)
7782 : 0 : pg_fatal("failed sanity check, table OID %u appearing in pg_partitioned_table not found",
7783 : : tabrelid);
7784 : 45 : tbinfo->unsafe_partitions = true;
7785 : : }
7786 : :
7787 : 146 : PQclear(res);
7788 : :
7789 : 146 : destroyPQExpBuffer(query);
7790 : : }
7791 : :
7792 : : /*
7793 : : * getIndexes
7794 : : * get information about every index on a dumpable table
7795 : : *
7796 : : * Note: index data is not returned directly to the caller, but it
7797 : : * does get entered into the DumpableObject tables.
7798 : : */
7799 : : void
7800 : 193 : getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
7801 : : {
7802 : 193 : PQExpBuffer query = createPQExpBuffer();
7803 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
7804 : : PGresult *res;
7805 : : int ntups;
7806 : : int curtblindx;
7807 : : IndxInfo *indxinfo;
7808 : : int i_tableoid,
7809 : : i_oid,
7810 : : i_indrelid,
7811 : : i_indexname,
7812 : : i_relpages,
7813 : : i_reltuples,
7814 : : i_relallvisible,
7815 : : i_relallfrozen,
7816 : : i_parentidx,
7817 : : i_indexdef,
7818 : : i_indnkeyatts,
7819 : : i_indnatts,
7820 : : i_indkey,
7821 : : i_indisclustered,
7822 : : i_indisreplident,
7823 : : i_indnullsnotdistinct,
7824 : : i_contype,
7825 : : i_conname,
7826 : : i_condeferrable,
7827 : : i_condeferred,
7828 : : i_conperiod,
7829 : : i_contableoid,
7830 : : i_conoid,
7831 : : i_condef,
7832 : : i_indattnames,
7833 : : i_tablespace,
7834 : : i_indreloptions,
7835 : : i_indstatcols,
7836 : : i_indstatvals;
7837 : :
7838 : : /*
7839 : : * We want to perform just one query against pg_index. However, we
7840 : : * mustn't try to select every row of the catalog and then sort it out on
7841 : : * the client side, because some of the server-side functions we need
7842 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
7843 : : * build an array of the OIDs of tables we care about (and now have lock
7844 : : * on!), and use a WHERE clause to constrain which rows are selected.
7845 : : */
7846 : 193 : appendPQExpBufferChar(tbloids, '{');
7847 [ + + ]: 55672 : for (int i = 0; i < numTables; i++)
7848 : : {
7849 : 55479 : TableInfo *tbinfo = &tblinfo[i];
7850 : :
7851 [ + + ]: 55479 : if (!tbinfo->hasindex)
7852 : 39503 : continue;
7853 : :
7854 : : /*
7855 : : * We can ignore indexes of uninteresting tables.
7856 : : */
7857 [ + + ]: 15976 : if (!tbinfo->interesting)
7858 : 13761 : continue;
7859 : :
7860 : : /* OK, we need info for this table */
7861 [ + + ]: 2215 : if (tbloids->len > 1) /* do we have more than the '{'? */
7862 : 2133 : appendPQExpBufferChar(tbloids, ',');
7863 : 2215 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
7864 : : }
7865 : 193 : appendPQExpBufferChar(tbloids, '}');
7866 : :
7867 : 193 : appendPQExpBufferStr(query,
7868 : : "SELECT t.tableoid, t.oid, i.indrelid, "
7869 : : "t.relname AS indexname, "
7870 : : "t.relpages, t.reltuples, t.relallvisible, ");
7871 : :
7872 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
7873 : 193 : appendPQExpBufferStr(query, "t.relallfrozen, ");
7874 : : else
7875 : 0 : appendPQExpBufferStr(query, "0 AS relallfrozen, ");
7876 : :
7877 : 193 : appendPQExpBufferStr(query,
7878 : : "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
7879 : : "i.indkey, i.indisclustered, "
7880 : : "c.contype, c.conname, "
7881 : : "c.condeferrable, c.condeferred, "
7882 : : "c.tableoid AS contableoid, "
7883 : : "c.oid AS conoid, "
7884 : : "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
7885 : : "CASE WHEN i.indexprs IS NOT NULL THEN "
7886 : : "(SELECT pg_catalog.array_agg(attname ORDER BY attnum)"
7887 : : " FROM pg_catalog.pg_attribute "
7888 : : " WHERE attrelid = i.indexrelid) "
7889 : : "ELSE NULL END AS indattnames, "
7890 : : "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
7891 : : "t.reloptions AS indreloptions, ");
7892 : :
7893 : :
7894 : 193 : appendPQExpBufferStr(query,
7895 : : "i.indisreplident, ");
7896 : :
7897 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
7898 : 193 : appendPQExpBufferStr(query,
7899 : : "inh.inhparent AS parentidx, "
7900 : : "i.indnkeyatts AS indnkeyatts, "
7901 : : "i.indnatts AS indnatts, "
7902 : : "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) "
7903 : : " FROM pg_catalog.pg_attribute "
7904 : : " WHERE attrelid = i.indexrelid AND "
7905 : : " attstattarget >= 0) AS indstatcols, "
7906 : : "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) "
7907 : : " FROM pg_catalog.pg_attribute "
7908 : : " WHERE attrelid = i.indexrelid AND "
7909 : : " attstattarget >= 0) AS indstatvals, ");
7910 : : else
7911 : 0 : appendPQExpBufferStr(query,
7912 : : "0 AS parentidx, "
7913 : : "i.indnatts AS indnkeyatts, "
7914 : : "i.indnatts AS indnatts, "
7915 : : "'' AS indstatcols, "
7916 : : "'' AS indstatvals, ");
7917 : :
7918 [ + - ]: 193 : if (fout->remoteVersion >= 150000)
7919 : 193 : appendPQExpBufferStr(query,
7920 : : "i.indnullsnotdistinct, ");
7921 : : else
7922 : 0 : appendPQExpBufferStr(query,
7923 : : "false AS indnullsnotdistinct, ");
7924 : :
7925 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
7926 : 193 : appendPQExpBufferStr(query,
7927 : : "c.conperiod ");
7928 : : else
7929 : 0 : appendPQExpBufferStr(query,
7930 : : "NULL AS conperiod ");
7931 : :
7932 : : /*
7933 : : * The point of the messy-looking outer join is to find a constraint that
7934 : : * is related by an internal dependency link to the index. If we find one,
7935 : : * create a CONSTRAINT entry linked to the INDEX entry. We assume an
7936 : : * index won't have more than one internal dependency.
7937 : : *
7938 : : * Note: the check on conrelid is redundant, but useful because that
7939 : : * column is indexed while conindid is not.
7940 : : */
7941 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
7942 : : {
7943 : 193 : appendPQExpBuffer(query,
7944 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
7945 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
7946 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
7947 : : "JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) "
7948 : : "LEFT JOIN pg_catalog.pg_constraint c "
7949 : : "ON (i.indrelid = c.conrelid AND "
7950 : : "i.indexrelid = c.conindid AND "
7951 : : "c.contype IN ('p','u','x')) "
7952 : : "LEFT JOIN pg_catalog.pg_inherits inh "
7953 : : "ON (inh.inhrelid = indexrelid) "
7954 : : "WHERE (i.indisvalid OR t2.relkind = 'p') "
7955 : : "AND i.indisready "
7956 : : "ORDER BY i.indrelid, indexname",
7957 : : tbloids->data);
7958 : : }
7959 : : else
7960 : : {
7961 : : /*
7962 : : * the test on indisready is necessary in 9.2, and harmless in
7963 : : * earlier/later versions
7964 : : */
7965 : 0 : appendPQExpBuffer(query,
7966 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
7967 : : "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
7968 : : "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
7969 : : "LEFT JOIN pg_catalog.pg_constraint c "
7970 : : "ON (i.indrelid = c.conrelid AND "
7971 : : "i.indexrelid = c.conindid AND "
7972 : : "c.contype IN ('p','u','x')) "
7973 : : "WHERE i.indisvalid AND i.indisready "
7974 : : "ORDER BY i.indrelid, indexname",
7975 : : tbloids->data);
7976 : : }
7977 : :
7978 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7979 : :
7980 : 193 : ntups = PQntuples(res);
7981 : :
7982 : 193 : i_tableoid = PQfnumber(res, "tableoid");
7983 : 193 : i_oid = PQfnumber(res, "oid");
7984 : 193 : i_indrelid = PQfnumber(res, "indrelid");
7985 : 193 : i_indexname = PQfnumber(res, "indexname");
7986 : 193 : i_relpages = PQfnumber(res, "relpages");
7987 : 193 : i_reltuples = PQfnumber(res, "reltuples");
7988 : 193 : i_relallvisible = PQfnumber(res, "relallvisible");
7989 : 193 : i_relallfrozen = PQfnumber(res, "relallfrozen");
7990 : 193 : i_parentidx = PQfnumber(res, "parentidx");
7991 : 193 : i_indexdef = PQfnumber(res, "indexdef");
7992 : 193 : i_indnkeyatts = PQfnumber(res, "indnkeyatts");
7993 : 193 : i_indnatts = PQfnumber(res, "indnatts");
7994 : 193 : i_indkey = PQfnumber(res, "indkey");
7995 : 193 : i_indisclustered = PQfnumber(res, "indisclustered");
7996 : 193 : i_indisreplident = PQfnumber(res, "indisreplident");
7997 : 193 : i_indnullsnotdistinct = PQfnumber(res, "indnullsnotdistinct");
7998 : 193 : i_contype = PQfnumber(res, "contype");
7999 : 193 : i_conname = PQfnumber(res, "conname");
8000 : 193 : i_condeferrable = PQfnumber(res, "condeferrable");
8001 : 193 : i_condeferred = PQfnumber(res, "condeferred");
8002 : 193 : i_conperiod = PQfnumber(res, "conperiod");
8003 : 193 : i_contableoid = PQfnumber(res, "contableoid");
8004 : 193 : i_conoid = PQfnumber(res, "conoid");
8005 : 193 : i_condef = PQfnumber(res, "condef");
8006 : 193 : i_indattnames = PQfnumber(res, "indattnames");
8007 : 193 : i_tablespace = PQfnumber(res, "tablespace");
8008 : 193 : i_indreloptions = PQfnumber(res, "indreloptions");
8009 : 193 : i_indstatcols = PQfnumber(res, "indstatcols");
8010 : 193 : i_indstatvals = PQfnumber(res, "indstatvals");
8011 : :
8012 : 193 : indxinfo = pg_malloc_array(IndxInfo, ntups);
8013 : :
8014 : : /*
8015 : : * Outer loop iterates once per table, not once per row. Incrementing of
8016 : : * j is handled by the inner loop.
8017 : : */
8018 : 193 : curtblindx = -1;
8019 [ + + ]: 2388 : for (int j = 0; j < ntups;)
8020 : : {
8021 : 2195 : Oid indrelid = atooid(PQgetvalue(res, j, i_indrelid));
8022 : 2195 : TableInfo *tbinfo = NULL;
8023 : : int numinds;
8024 : :
8025 : : /* Count rows for this table */
8026 [ + + ]: 2861 : for (numinds = 1; numinds < ntups - j; numinds++)
8027 [ + + ]: 2779 : if (atooid(PQgetvalue(res, j + numinds, i_indrelid)) != indrelid)
8028 : 2113 : break;
8029 : :
8030 : : /*
8031 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8032 : : * order.
8033 : : */
8034 [ + - ]: 26214 : while (++curtblindx < numTables)
8035 : : {
8036 : 26214 : tbinfo = &tblinfo[curtblindx];
8037 [ + + ]: 26214 : if (tbinfo->dobj.catId.oid == indrelid)
8038 : 2195 : break;
8039 : : }
8040 [ - + ]: 2195 : if (curtblindx >= numTables)
8041 : 0 : pg_fatal("unrecognized table OID %u", indrelid);
8042 : : /* cross-check that we only got requested tables */
8043 [ + - ]: 2195 : if (!tbinfo->hasindex ||
8044 [ - + ]: 2195 : !tbinfo->interesting)
8045 : 0 : pg_fatal("unexpected index data for table \"%s\"",
8046 : : tbinfo->dobj.name);
8047 : :
8048 : : /* Save data for this table */
8049 : 2195 : tbinfo->indexes = indxinfo + j;
8050 : 2195 : tbinfo->numIndexes = numinds;
8051 : :
8052 [ + + ]: 5056 : for (int c = 0; c < numinds; c++, j++)
8053 : : {
8054 : : char contype;
8055 : : char indexkind;
8056 : 2861 : char **indAttNames = NULL;
8057 : 2861 : int nindAttNames = 0;
8058 : : RelStatsInfo *relstats;
8059 : 2861 : int32 relpages = atoi(PQgetvalue(res, j, i_relpages));
8060 : 2861 : int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible));
8061 : 2861 : int32 relallfrozen = atoi(PQgetvalue(res, j, i_relallfrozen));
8062 : :
8063 : 2861 : indxinfo[j].dobj.objType = DO_INDEX;
8064 : 2861 : indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8065 : 2861 : indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8066 : 2861 : AssignDumpId(&indxinfo[j].dobj);
8067 : 2861 : indxinfo[j].dobj.dump = tbinfo->dobj.dump;
8068 : 2861 : indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname));
8069 : 2861 : indxinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8070 : 2861 : indxinfo[j].indextable = tbinfo;
8071 : 2861 : indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef));
8072 : 2861 : indxinfo[j].indnkeyattrs = atoi(PQgetvalue(res, j, i_indnkeyatts));
8073 : 2861 : indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts));
8074 : 2861 : indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
8075 : 2861 : indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions));
8076 : 2861 : indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols));
8077 : 2861 : indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals));
8078 : 2861 : indxinfo[j].indkeys = parseIntArray(PQgetvalue(res, j, i_indkey),
8079 : 2861 : indxinfo[j].indnattrs);
8080 : 2861 : indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
8081 : 2861 : indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
8082 : 2861 : indxinfo[j].indnullsnotdistinct = (PQgetvalue(res, j, i_indnullsnotdistinct)[0] == 't');
8083 : 2861 : indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx));
8084 : 2861 : indxinfo[j].partattaches = (SimplePtrList)
8085 : : {
8086 : : NULL, NULL
8087 : : };
8088 : :
8089 [ + + ]: 2861 : if (indxinfo[j].parentidx == 0)
8090 : 2246 : indexkind = RELKIND_INDEX;
8091 : : else
8092 : 615 : indexkind = RELKIND_PARTITIONED_INDEX;
8093 : :
8094 [ + + ]: 2861 : if (!PQgetisnull(res, j, i_indattnames))
8095 : : {
8096 [ - + ]: 167 : if (!parsePGArray(PQgetvalue(res, j, i_indattnames),
8097 : : &indAttNames, &nindAttNames))
8098 : 0 : pg_fatal("could not parse %s array", "indattnames");
8099 : : }
8100 : :
8101 : 2861 : relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages,
8102 : : PQgetvalue(res, j, i_reltuples),
8103 : : relallvisible, relallfrozen, indexkind,
8104 : : indAttNames, nindAttNames);
8105 : :
8106 : 2861 : contype = *(PQgetvalue(res, j, i_contype));
8107 [ + + + + : 2861 : if (contype == 'p' || contype == 'u' || contype == 'x')
+ + ]
8108 : 1724 : {
8109 : : /*
8110 : : * If we found a constraint matching the index, create an
8111 : : * entry for it.
8112 : : */
8113 : : ConstraintInfo *constrinfo;
8114 : :
8115 : 1724 : constrinfo = pg_malloc_object(ConstraintInfo);
8116 : 1724 : constrinfo->dobj.objType = DO_CONSTRAINT;
8117 : 1724 : constrinfo->dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8118 : 1724 : constrinfo->dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8119 : 1724 : AssignDumpId(&constrinfo->dobj);
8120 : 1724 : constrinfo->dobj.dump = tbinfo->dobj.dump;
8121 : 1724 : constrinfo->dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8122 : 1724 : constrinfo->dobj.namespace = tbinfo->dobj.namespace;
8123 : 1724 : constrinfo->contable = tbinfo;
8124 : 1724 : constrinfo->condomain = NULL;
8125 : 1724 : constrinfo->contype = contype;
8126 [ + + ]: 1724 : if (contype == 'x')
8127 : 20 : constrinfo->condef = pg_strdup(PQgetvalue(res, j, i_condef));
8128 : : else
8129 : 1704 : constrinfo->condef = NULL;
8130 : 1724 : constrinfo->confrelid = InvalidOid;
8131 : 1724 : constrinfo->conindex = indxinfo[j].dobj.dumpId;
8132 : 1724 : constrinfo->condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't';
8133 : 1724 : constrinfo->condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't';
8134 : 1724 : constrinfo->conperiod = *(PQgetvalue(res, j, i_conperiod)) == 't';
8135 : 1724 : constrinfo->conislocal = true;
8136 : 1724 : constrinfo->separate = true;
8137 : :
8138 : 1724 : indxinfo[j].indexconstraint = constrinfo->dobj.dumpId;
8139 [ + + ]: 1724 : if (relstats != NULL)
8140 : 591 : addObjectDependency(&relstats->dobj, constrinfo->dobj.dumpId);
8141 : : }
8142 : : else
8143 : : {
8144 : : /* Plain secondary index */
8145 : 1137 : indxinfo[j].indexconstraint = 0;
8146 : : }
8147 : : }
8148 : : }
8149 : :
8150 : 193 : PQclear(res);
8151 : :
8152 : 193 : destroyPQExpBuffer(query);
8153 : 193 : destroyPQExpBuffer(tbloids);
8154 : 193 : }
8155 : :
8156 : : /*
8157 : : * getExtendedStatistics
8158 : : * get information about extended-statistics objects.
8159 : : *
8160 : : * Note: extended statistics data is not returned directly to the caller, but
8161 : : * it does get entered into the DumpableObject tables.
8162 : : */
8163 : : void
8164 : 193 : getExtendedStatistics(Archive *fout)
8165 : : {
8166 : : PQExpBuffer query;
8167 : : PGresult *res;
8168 : : StatsExtInfo *statsextinfo;
8169 : : int ntups;
8170 : : int i_tableoid;
8171 : : int i_oid;
8172 : : int i_stxname;
8173 : : int i_stxnamespace;
8174 : : int i_stxowner;
8175 : : int i_stxrelid;
8176 : : int i_stattarget;
8177 : : int i;
8178 : :
8179 : 193 : query = createPQExpBuffer();
8180 : :
8181 [ - + ]: 193 : if (fout->remoteVersion < 130000)
8182 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8183 : : "stxnamespace, stxowner, stxrelid, NULL AS stxstattarget "
8184 : : "FROM pg_catalog.pg_statistic_ext");
8185 : : else
8186 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
8187 : : "stxnamespace, stxowner, stxrelid, stxstattarget "
8188 : : "FROM pg_catalog.pg_statistic_ext");
8189 : :
8190 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8191 : :
8192 : 193 : ntups = PQntuples(res);
8193 : :
8194 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8195 : 193 : i_oid = PQfnumber(res, "oid");
8196 : 193 : i_stxname = PQfnumber(res, "stxname");
8197 : 193 : i_stxnamespace = PQfnumber(res, "stxnamespace");
8198 : 193 : i_stxowner = PQfnumber(res, "stxowner");
8199 : 193 : i_stxrelid = PQfnumber(res, "stxrelid");
8200 : 193 : i_stattarget = PQfnumber(res, "stxstattarget");
8201 : :
8202 : 193 : statsextinfo = pg_malloc_array(StatsExtInfo, ntups);
8203 : :
8204 [ + + ]: 413 : for (i = 0; i < ntups; i++)
8205 : : {
8206 : 220 : statsextinfo[i].dobj.objType = DO_STATSEXT;
8207 : 220 : statsextinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8208 : 220 : statsextinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8209 : 220 : AssignDumpId(&statsextinfo[i].dobj);
8210 : 220 : statsextinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_stxname));
8211 : 440 : statsextinfo[i].dobj.namespace =
8212 : 220 : findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace)));
8213 : 220 : statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner));
8214 : 440 : statsextinfo[i].stattable =
8215 : 220 : findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid)));
8216 [ + + ]: 220 : if (PQgetisnull(res, i, i_stattarget))
8217 : 172 : statsextinfo[i].stattarget = -1;
8218 : : else
8219 : 48 : statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget));
8220 : :
8221 : : /* Decide whether we want to dump it */
8222 : 220 : selectDumpableStatisticsObject(&(statsextinfo[i]), fout);
8223 : :
8224 [ + + ]: 220 : if (fout->dopt->dumpStatistics)
8225 : 164 : statsextinfo[i].dobj.components |= DUMP_COMPONENT_STATISTICS;
8226 : : }
8227 : :
8228 : 193 : PQclear(res);
8229 : 193 : destroyPQExpBuffer(query);
8230 : 193 : }
8231 : :
8232 : : /*
8233 : : * getConstraints
8234 : : *
8235 : : * Get info about constraints on dumpable tables.
8236 : : *
8237 : : * Currently handles foreign keys only.
8238 : : * Unique and primary key constraints are handled with indexes,
8239 : : * while check constraints are processed in getTableAttrs().
8240 : : */
8241 : : void
8242 : 193 : getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
8243 : : {
8244 : 193 : PQExpBuffer query = createPQExpBuffer();
8245 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
8246 : : PGresult *res;
8247 : : int ntups;
8248 : : int curtblindx;
8249 : 193 : TableInfo *tbinfo = NULL;
8250 : : ConstraintInfo *constrinfo;
8251 : : int i_contableoid,
8252 : : i_conoid,
8253 : : i_conrelid,
8254 : : i_conname,
8255 : : i_confrelid,
8256 : : i_conindid,
8257 : : i_condef;
8258 : :
8259 : : /*
8260 : : * We want to perform just one query against pg_constraint. However, we
8261 : : * mustn't try to select every row of the catalog and then sort it out on
8262 : : * the client side, because some of the server-side functions we need
8263 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8264 : : * build an array of the OIDs of tables we care about (and now have lock
8265 : : * on!), and use a WHERE clause to constrain which rows are selected.
8266 : : */
8267 : 193 : appendPQExpBufferChar(tbloids, '{');
8268 [ + + ]: 55672 : for (int i = 0; i < numTables; i++)
8269 : : {
8270 : 55479 : TableInfo *tinfo = &tblinfo[i];
8271 : :
8272 [ + + ]: 55479 : if (!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8273 : 47804 : continue;
8274 : :
8275 : : /* OK, we need info for this table */
8276 [ + + ]: 7675 : if (tbloids->len > 1) /* do we have more than the '{'? */
8277 : 7547 : appendPQExpBufferChar(tbloids, ',');
8278 : 7675 : appendPQExpBuffer(tbloids, "%u", tinfo->dobj.catId.oid);
8279 : : }
8280 : 193 : appendPQExpBufferChar(tbloids, '}');
8281 : :
8282 : 193 : appendPQExpBufferStr(query,
8283 : : "SELECT c.tableoid, c.oid, "
8284 : : "conrelid, conname, confrelid, ");
8285 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
8286 : 193 : appendPQExpBufferStr(query, "conindid, ");
8287 : : else
8288 : 0 : appendPQExpBufferStr(query, "0 AS conindid, ");
8289 : 193 : appendPQExpBuffer(query,
8290 : : "pg_catalog.pg_get_constraintdef(c.oid) AS condef\n"
8291 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8292 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
8293 : : "WHERE contype = 'f' ",
8294 : : tbloids->data);
8295 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
8296 : 193 : appendPQExpBufferStr(query,
8297 : : "AND conparentid = 0 ");
8298 : 193 : appendPQExpBufferStr(query,
8299 : : "ORDER BY conrelid, conname");
8300 : :
8301 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8302 : :
8303 : 193 : ntups = PQntuples(res);
8304 : :
8305 : 193 : i_contableoid = PQfnumber(res, "tableoid");
8306 : 193 : i_conoid = PQfnumber(res, "oid");
8307 : 193 : i_conrelid = PQfnumber(res, "conrelid");
8308 : 193 : i_conname = PQfnumber(res, "conname");
8309 : 193 : i_confrelid = PQfnumber(res, "confrelid");
8310 : 193 : i_conindid = PQfnumber(res, "conindid");
8311 : 193 : i_condef = PQfnumber(res, "condef");
8312 : :
8313 : 193 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8314 : :
8315 : 193 : curtblindx = -1;
8316 [ + + ]: 430 : for (int j = 0; j < ntups; j++)
8317 : : {
8318 : 237 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
8319 : : TableInfo *reftable;
8320 : :
8321 : : /*
8322 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8323 : : * order.
8324 : : */
8325 [ + + + + ]: 237 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != conrelid)
8326 : : {
8327 [ + - ]: 16498 : while (++curtblindx < numTables)
8328 : : {
8329 : 16498 : tbinfo = &tblinfo[curtblindx];
8330 [ + + ]: 16498 : if (tbinfo->dobj.catId.oid == conrelid)
8331 : 197 : break;
8332 : : }
8333 [ - + ]: 197 : if (curtblindx >= numTables)
8334 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
8335 : : }
8336 : :
8337 : 237 : constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
8338 : 237 : constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
8339 : 237 : constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
8340 : 237 : AssignDumpId(&constrinfo[j].dobj);
8341 : 237 : constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
8342 : 237 : constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
8343 : 237 : constrinfo[j].contable = tbinfo;
8344 : 237 : constrinfo[j].condomain = NULL;
8345 : 237 : constrinfo[j].contype = 'f';
8346 : 237 : constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
8347 : 237 : constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid));
8348 : 237 : constrinfo[j].conindex = 0;
8349 : 237 : constrinfo[j].condeferrable = false;
8350 : 237 : constrinfo[j].condeferred = false;
8351 : 237 : constrinfo[j].conislocal = true;
8352 : 237 : constrinfo[j].separate = true;
8353 : :
8354 : : /*
8355 : : * Restoring an FK that points to a partitioned table requires that
8356 : : * all partition indexes have been attached beforehand. Ensure that
8357 : : * happens by making the constraint depend on each index partition
8358 : : * attach object.
8359 : : */
8360 : 237 : reftable = findTableByOid(constrinfo[j].confrelid);
8361 [ + - + + ]: 237 : if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
8362 : : {
8363 : 30 : Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
8364 : :
8365 [ + - ]: 30 : if (indexOid != InvalidOid)
8366 : : {
8367 [ + - ]: 30 : for (int k = 0; k < reftable->numIndexes; k++)
8368 : : {
8369 : : IndxInfo *refidx;
8370 : :
8371 : : /* not our index? */
8372 [ - + ]: 30 : if (reftable->indexes[k].dobj.catId.oid != indexOid)
8373 : 0 : continue;
8374 : :
8375 : 30 : refidx = &reftable->indexes[k];
8376 : 30 : addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
8377 : 30 : break;
8378 : : }
8379 : : }
8380 : : }
8381 : : }
8382 : :
8383 : 193 : PQclear(res);
8384 : :
8385 : 193 : destroyPQExpBuffer(query);
8386 : 193 : destroyPQExpBuffer(tbloids);
8387 : 193 : }
8388 : :
8389 : : /*
8390 : : * addConstrChildIdxDeps
8391 : : *
8392 : : * Recursive subroutine for getConstraints
8393 : : *
8394 : : * Given an object representing a foreign key constraint and an index on the
8395 : : * partitioned table it references, mark the constraint object as dependent
8396 : : * on the DO_INDEX_ATTACH object of each index partition, recursively
8397 : : * drilling down to their partitions if any. This ensures that the FK is not
8398 : : * restored until the index is fully marked valid.
8399 : : */
8400 : : static void
8401 : 55 : addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
8402 : : {
8403 : : SimplePtrListCell *cell;
8404 : :
8405 : : Assert(dobj->objType == DO_FK_CONSTRAINT);
8406 : :
8407 [ + + ]: 185 : for (cell = refidx->partattaches.head; cell; cell = cell->next)
8408 : : {
8409 : 130 : IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
8410 : :
8411 : 130 : addObjectDependency(dobj, attach->dobj.dumpId);
8412 : :
8413 [ + + ]: 130 : if (attach->partitionIdx->partattaches.head != NULL)
8414 : 25 : addConstrChildIdxDeps(dobj, attach->partitionIdx);
8415 : : }
8416 : 55 : }
8417 : :
8418 : : /*
8419 : : * getDomainConstraints
8420 : : *
8421 : : * Get info about constraints on a domain.
8422 : : */
8423 : : static void
8424 : 181 : getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
8425 : : {
8426 : : ConstraintInfo *constrinfo;
8427 : 181 : PQExpBuffer query = createPQExpBuffer();
8428 : : PGresult *res;
8429 : : int i_tableoid,
8430 : : i_oid,
8431 : : i_conname,
8432 : : i_consrc,
8433 : : i_convalidated,
8434 : : i_contype;
8435 : : int ntups;
8436 : :
8437 [ + + ]: 181 : if (!fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS])
8438 : : {
8439 : : /*
8440 : : * Set up query for constraint-specific details. For servers 17 and
8441 : : * up, domains have constraints of type 'n' as well as 'c', otherwise
8442 : : * just the latter.
8443 : : */
8444 : 46 : appendPQExpBuffer(query,
8445 : : "PREPARE getDomainConstraints(pg_catalog.oid) AS\n"
8446 : : "SELECT tableoid, oid, conname, "
8447 : : "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8448 : : "convalidated, contype "
8449 : : "FROM pg_catalog.pg_constraint "
8450 : : "WHERE contypid = $1 AND contype IN (%s) "
8451 : : "ORDER BY conname",
8452 [ - + ]: 46 : fout->remoteVersion < 170000 ? "'c'" : "'c', 'n'");
8453 : :
8454 : 46 : ExecuteSqlStatement(fout, query->data);
8455 : :
8456 : 46 : fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS] = true;
8457 : : }
8458 : :
8459 : 181 : printfPQExpBuffer(query,
8460 : : "EXECUTE getDomainConstraints('%u')",
8461 : : tyinfo->dobj.catId.oid);
8462 : :
8463 : 181 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8464 : :
8465 : 181 : ntups = PQntuples(res);
8466 : :
8467 : 181 : i_tableoid = PQfnumber(res, "tableoid");
8468 : 181 : i_oid = PQfnumber(res, "oid");
8469 : 181 : i_conname = PQfnumber(res, "conname");
8470 : 181 : i_consrc = PQfnumber(res, "consrc");
8471 : 181 : i_convalidated = PQfnumber(res, "convalidated");
8472 : 181 : i_contype = PQfnumber(res, "contype");
8473 : :
8474 : 181 : constrinfo = pg_malloc_array(ConstraintInfo, ntups);
8475 : 181 : tyinfo->domChecks = constrinfo;
8476 : :
8477 : : /* 'i' tracks result rows; 'j' counts CHECK constraints */
8478 [ + + ]: 373 : for (int i = 0, j = 0; i < ntups; i++)
8479 : : {
8480 : 192 : bool validated = PQgetvalue(res, i, i_convalidated)[0] == 't';
8481 : 192 : char contype = (PQgetvalue(res, i, i_contype))[0];
8482 : : ConstraintInfo *constraint;
8483 : :
8484 [ + + ]: 192 : if (contype == CONSTRAINT_CHECK)
8485 : : {
8486 : 136 : constraint = &constrinfo[j++];
8487 : 136 : tyinfo->nDomChecks++;
8488 : : }
8489 : : else
8490 : : {
8491 : : Assert(contype == CONSTRAINT_NOTNULL);
8492 : : Assert(tyinfo->notnull == NULL);
8493 : : /* use last item in array for the not-null constraint */
8494 : 56 : tyinfo->notnull = &(constrinfo[ntups - 1]);
8495 : 56 : constraint = tyinfo->notnull;
8496 : : }
8497 : :
8498 : 192 : constraint->dobj.objType = DO_CONSTRAINT;
8499 : 192 : constraint->dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8500 : 192 : constraint->dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8501 : 192 : AssignDumpId(&(constraint->dobj));
8502 : 192 : constraint->dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
8503 : 192 : constraint->dobj.namespace = tyinfo->dobj.namespace;
8504 : 192 : constraint->contable = NULL;
8505 : 192 : constraint->condomain = tyinfo;
8506 : 192 : constraint->contype = contype;
8507 : 192 : constraint->condef = pg_strdup(PQgetvalue(res, i, i_consrc));
8508 : 192 : constraint->confrelid = InvalidOid;
8509 : 192 : constraint->conindex = 0;
8510 : 192 : constraint->condeferrable = false;
8511 : 192 : constraint->condeferred = false;
8512 : 192 : constraint->conislocal = true;
8513 : :
8514 : 192 : constraint->separate = !validated;
8515 : :
8516 : : /*
8517 : : * Make the domain depend on the constraint, ensuring it won't be
8518 : : * output till any constraint dependencies are OK. If the constraint
8519 : : * has not been validated, it's going to be dumped after the domain
8520 : : * anyway, so this doesn't matter.
8521 : : */
8522 [ + + ]: 192 : if (validated)
8523 : 187 : addObjectDependency(&tyinfo->dobj, constraint->dobj.dumpId);
8524 : : }
8525 : :
8526 : 181 : PQclear(res);
8527 : :
8528 : 181 : destroyPQExpBuffer(query);
8529 : 181 : }
8530 : :
8531 : : /*
8532 : : * getRules
8533 : : * get basic information about every rule in the system
8534 : : */
8535 : : void
8536 : 193 : getRules(Archive *fout)
8537 : : {
8538 : : PGresult *res;
8539 : : int ntups;
8540 : : int i;
8541 : 193 : PQExpBuffer query = createPQExpBuffer();
8542 : : RuleInfo *ruleinfo;
8543 : : int i_tableoid;
8544 : : int i_oid;
8545 : : int i_rulename;
8546 : : int i_ruletable;
8547 : : int i_ev_type;
8548 : : int i_is_instead;
8549 : : int i_ev_enabled;
8550 : :
8551 : 193 : appendPQExpBufferStr(query, "SELECT "
8552 : : "tableoid, oid, rulename, "
8553 : : "ev_class AS ruletable, ev_type, is_instead, "
8554 : : "ev_enabled "
8555 : : "FROM pg_rewrite "
8556 : : "ORDER BY oid");
8557 : :
8558 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8559 : :
8560 : 193 : ntups = PQntuples(res);
8561 : :
8562 : 193 : ruleinfo = pg_malloc_array(RuleInfo, ntups);
8563 : :
8564 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8565 : 193 : i_oid = PQfnumber(res, "oid");
8566 : 193 : i_rulename = PQfnumber(res, "rulename");
8567 : 193 : i_ruletable = PQfnumber(res, "ruletable");
8568 : 193 : i_ev_type = PQfnumber(res, "ev_type");
8569 : 193 : i_is_instead = PQfnumber(res, "is_instead");
8570 : 193 : i_ev_enabled = PQfnumber(res, "ev_enabled");
8571 : :
8572 [ + + ]: 33239 : for (i = 0; i < ntups; i++)
8573 : : {
8574 : : Oid ruletableoid;
8575 : :
8576 : 33046 : ruleinfo[i].dobj.objType = DO_RULE;
8577 : 33046 : ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8578 : 33046 : ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8579 : 33046 : AssignDumpId(&ruleinfo[i].dobj);
8580 : 33046 : ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename));
8581 : 33046 : ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
8582 : 33046 : ruleinfo[i].ruletable = findTableByOid(ruletableoid);
8583 [ - + ]: 33046 : if (ruleinfo[i].ruletable == NULL)
8584 : 0 : pg_fatal("failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found",
8585 : : ruletableoid, ruleinfo[i].dobj.catId.oid);
8586 : 33046 : ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace;
8587 : 33046 : ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump;
8588 : 33046 : ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
8589 : 33046 : ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
8590 : 33046 : ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled));
8591 [ + - ]: 33046 : if (ruleinfo[i].ruletable)
8592 : : {
8593 : : /*
8594 : : * If the table is a view or materialized view, force its ON
8595 : : * SELECT rule to be sorted before the view itself --- this
8596 : : * ensures that any dependencies for the rule affect the table's
8597 : : * positioning. Other rules are forced to appear after their
8598 : : * table.
8599 : : */
8600 [ + + ]: 33046 : if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW ||
8601 [ + + ]: 726 : ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) &&
8602 [ + + + - ]: 32815 : ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
8603 : : {
8604 : 32387 : addObjectDependency(&ruleinfo[i].ruletable->dobj,
8605 : 32387 : ruleinfo[i].dobj.dumpId);
8606 : : /* We'll merge the rule into CREATE VIEW, if possible */
8607 : 32387 : ruleinfo[i].separate = false;
8608 : : }
8609 : : else
8610 : : {
8611 : 659 : addObjectDependency(&ruleinfo[i].dobj,
8612 : 659 : ruleinfo[i].ruletable->dobj.dumpId);
8613 : 659 : ruleinfo[i].separate = true;
8614 : : }
8615 : : }
8616 : : else
8617 : 0 : ruleinfo[i].separate = true;
8618 : : }
8619 : :
8620 : 193 : PQclear(res);
8621 : :
8622 : 193 : destroyPQExpBuffer(query);
8623 : 193 : }
8624 : :
8625 : : /*
8626 : : * getTriggers
8627 : : * get information about every trigger on a dumpable table
8628 : : *
8629 : : * Note: trigger data is not returned directly to the caller, but it
8630 : : * does get entered into the DumpableObject tables.
8631 : : */
8632 : : void
8633 : 193 : getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
8634 : : {
8635 : 193 : PQExpBuffer query = createPQExpBuffer();
8636 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
8637 : : PGresult *res;
8638 : : int ntups;
8639 : : int curtblindx;
8640 : : TriggerInfo *tginfo;
8641 : : int i_tableoid,
8642 : : i_oid,
8643 : : i_tgrelid,
8644 : : i_tgname,
8645 : : i_tgenabled,
8646 : : i_tgispartition,
8647 : : i_tgdef;
8648 : :
8649 : : /*
8650 : : * We want to perform just one query against pg_trigger. However, we
8651 : : * mustn't try to select every row of the catalog and then sort it out on
8652 : : * the client side, because some of the server-side functions we need
8653 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
8654 : : * build an array of the OIDs of tables we care about (and now have lock
8655 : : * on!), and use a WHERE clause to constrain which rows are selected.
8656 : : */
8657 : 193 : appendPQExpBufferChar(tbloids, '{');
8658 [ + + ]: 55672 : for (int i = 0; i < numTables; i++)
8659 : : {
8660 : 55479 : TableInfo *tbinfo = &tblinfo[i];
8661 : :
8662 [ + + ]: 55479 : if (!tbinfo->hastriggers ||
8663 [ + + ]: 1260 : !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8664 : 54519 : continue;
8665 : :
8666 : : /* OK, we need info for this table */
8667 [ + + ]: 960 : if (tbloids->len > 1) /* do we have more than the '{'? */
8668 : 906 : appendPQExpBufferChar(tbloids, ',');
8669 : 960 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
8670 : : }
8671 : 193 : appendPQExpBufferChar(tbloids, '}');
8672 : :
8673 [ + - ]: 193 : if (fout->remoteVersion >= 150000)
8674 : : {
8675 : : /*
8676 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8677 : : * result in non-forward-compatible dumps of WHEN clauses due to
8678 : : * under-parenthesization.
8679 : : *
8680 : : * NB: We need to see partition triggers in case the tgenabled flag
8681 : : * has been changed from the parent.
8682 : : */
8683 : 193 : appendPQExpBuffer(query,
8684 : : "SELECT t.tgrelid, t.tgname, "
8685 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8686 : : "t.tgenabled, t.tableoid, t.oid, "
8687 : : "t.tgparentid <> 0 AS tgispartition\n"
8688 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8689 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8690 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8691 : : "WHERE ((NOT t.tgisinternal AND t.tgparentid = 0) "
8692 : : "OR t.tgenabled != u.tgenabled) "
8693 : : "ORDER BY t.tgrelid, t.tgname",
8694 : : tbloids->data);
8695 : : }
8696 [ # # ]: 0 : else if (fout->remoteVersion >= 130000)
8697 : : {
8698 : : /*
8699 : : * NB: think not to use pretty=true in pg_get_triggerdef. It could
8700 : : * result in non-forward-compatible dumps of WHEN clauses due to
8701 : : * under-parenthesization.
8702 : : *
8703 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8704 : : * tgenabled flag has been changed from the parent.
8705 : : */
8706 : 0 : appendPQExpBuffer(query,
8707 : : "SELECT t.tgrelid, t.tgname, "
8708 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8709 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition\n"
8710 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8711 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8712 : : "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) "
8713 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != u.tgenabled) "
8714 : : "ORDER BY t.tgrelid, t.tgname",
8715 : : tbloids->data);
8716 : : }
8717 [ # # ]: 0 : else if (fout->remoteVersion >= 110000)
8718 : : {
8719 : : /*
8720 : : * NB: We need to see tgisinternal triggers in partitions, in case the
8721 : : * tgenabled flag has been changed from the parent. No tgparentid in
8722 : : * version 11-12, so we have to match them via pg_depend.
8723 : : *
8724 : : * See above about pretty=true in pg_get_triggerdef.
8725 : : */
8726 : 0 : appendPQExpBuffer(query,
8727 : : "SELECT t.tgrelid, t.tgname, "
8728 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8729 : : "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition "
8730 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8731 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8732 : : "LEFT JOIN pg_catalog.pg_depend AS d ON "
8733 : : " d.classid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8734 : : " d.refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND "
8735 : : " d.objid = t.oid "
8736 : : "LEFT JOIN pg_catalog.pg_trigger AS pt ON pt.oid = refobjid "
8737 : : "WHERE (NOT t.tgisinternal OR t.tgenabled != pt.tgenabled) "
8738 : : "ORDER BY t.tgrelid, t.tgname",
8739 : : tbloids->data);
8740 : : }
8741 : : else
8742 : : {
8743 : : /* See above about pretty=true in pg_get_triggerdef */
8744 : 0 : appendPQExpBuffer(query,
8745 : : "SELECT t.tgrelid, t.tgname, "
8746 : : "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, "
8747 : : "t.tgenabled, false as tgispartition, "
8748 : : "t.tableoid, t.oid "
8749 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
8750 : : "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) "
8751 : : "WHERE NOT tgisinternal "
8752 : : "ORDER BY t.tgrelid, t.tgname",
8753 : : tbloids->data);
8754 : : }
8755 : :
8756 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8757 : :
8758 : 193 : ntups = PQntuples(res);
8759 : :
8760 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8761 : 193 : i_oid = PQfnumber(res, "oid");
8762 : 193 : i_tgrelid = PQfnumber(res, "tgrelid");
8763 : 193 : i_tgname = PQfnumber(res, "tgname");
8764 : 193 : i_tgenabled = PQfnumber(res, "tgenabled");
8765 : 193 : i_tgispartition = PQfnumber(res, "tgispartition");
8766 : 193 : i_tgdef = PQfnumber(res, "tgdef");
8767 : :
8768 : 193 : tginfo = pg_malloc_array(TriggerInfo, ntups);
8769 : :
8770 : : /*
8771 : : * Outer loop iterates once per table, not once per row. Incrementing of
8772 : : * j is handled by the inner loop.
8773 : : */
8774 : 193 : curtblindx = -1;
8775 [ + + ]: 511 : for (int j = 0; j < ntups;)
8776 : : {
8777 : 318 : Oid tgrelid = atooid(PQgetvalue(res, j, i_tgrelid));
8778 : 318 : TableInfo *tbinfo = NULL;
8779 : : int numtrigs;
8780 : :
8781 : : /* Count rows for this table */
8782 [ + + ]: 535 : for (numtrigs = 1; numtrigs < ntups - j; numtrigs++)
8783 [ + + ]: 481 : if (atooid(PQgetvalue(res, j + numtrigs, i_tgrelid)) != tgrelid)
8784 : 264 : break;
8785 : :
8786 : : /*
8787 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
8788 : : * order.
8789 : : */
8790 [ + - ]: 18313 : while (++curtblindx < numTables)
8791 : : {
8792 : 18313 : tbinfo = &tblinfo[curtblindx];
8793 [ + + ]: 18313 : if (tbinfo->dobj.catId.oid == tgrelid)
8794 : 318 : break;
8795 : : }
8796 [ - + ]: 318 : if (curtblindx >= numTables)
8797 : 0 : pg_fatal("unrecognized table OID %u", tgrelid);
8798 : :
8799 : : /* Save data for this table */
8800 : 318 : tbinfo->triggers = tginfo + j;
8801 : 318 : tbinfo->numTriggers = numtrigs;
8802 : :
8803 [ + + ]: 853 : for (int c = 0; c < numtrigs; c++, j++)
8804 : : {
8805 : 535 : tginfo[j].dobj.objType = DO_TRIGGER;
8806 : 535 : tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
8807 : 535 : tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
8808 : 535 : AssignDumpId(&tginfo[j].dobj);
8809 : 535 : tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname));
8810 : 535 : tginfo[j].dobj.namespace = tbinfo->dobj.namespace;
8811 : 535 : tginfo[j].tgtable = tbinfo;
8812 : 535 : tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled));
8813 : 535 : tginfo[j].tgispartition = *(PQgetvalue(res, j, i_tgispartition)) == 't';
8814 : 535 : tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef));
8815 : : }
8816 : : }
8817 : :
8818 : 193 : PQclear(res);
8819 : :
8820 : 193 : destroyPQExpBuffer(query);
8821 : 193 : destroyPQExpBuffer(tbloids);
8822 : 193 : }
8823 : :
8824 : : /*
8825 : : * getEventTriggers
8826 : : * get information about event triggers
8827 : : */
8828 : : void
8829 : 193 : getEventTriggers(Archive *fout)
8830 : : {
8831 : : int i;
8832 : : PQExpBuffer query;
8833 : : PGresult *res;
8834 : : EventTriggerInfo *evtinfo;
8835 : : int i_tableoid,
8836 : : i_oid,
8837 : : i_evtname,
8838 : : i_evtevent,
8839 : : i_evtowner,
8840 : : i_evttags,
8841 : : i_evtfname,
8842 : : i_evtenabled;
8843 : : int ntups;
8844 : :
8845 : 193 : query = createPQExpBuffer();
8846 : :
8847 : 193 : appendPQExpBufferStr(query,
8848 : : "SELECT e.tableoid, e.oid, evtname, evtenabled, "
8849 : : "evtevent, evtowner, "
8850 : : "array_to_string(array("
8851 : : "select quote_literal(x) "
8852 : : " from unnest(evttags) as t(x)), ', ') as evttags, "
8853 : : "e.evtfoid::regproc as evtfname "
8854 : : "FROM pg_event_trigger e "
8855 : : "ORDER BY e.oid");
8856 : :
8857 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8858 : :
8859 : 193 : ntups = PQntuples(res);
8860 : :
8861 : 193 : evtinfo = pg_malloc_array(EventTriggerInfo, ntups);
8862 : :
8863 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8864 : 193 : i_oid = PQfnumber(res, "oid");
8865 : 193 : i_evtname = PQfnumber(res, "evtname");
8866 : 193 : i_evtevent = PQfnumber(res, "evtevent");
8867 : 193 : i_evtowner = PQfnumber(res, "evtowner");
8868 : 193 : i_evttags = PQfnumber(res, "evttags");
8869 : 193 : i_evtfname = PQfnumber(res, "evtfname");
8870 : 193 : i_evtenabled = PQfnumber(res, "evtenabled");
8871 : :
8872 [ + + ]: 248 : for (i = 0; i < ntups; i++)
8873 : : {
8874 : 55 : evtinfo[i].dobj.objType = DO_EVENT_TRIGGER;
8875 : 55 : evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8876 : 55 : evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8877 : 55 : AssignDumpId(&evtinfo[i].dobj);
8878 : 55 : evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname));
8879 : 55 : evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname));
8880 : 55 : evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent));
8881 : 55 : evtinfo[i].evtowner = getRoleName(PQgetvalue(res, i, i_evtowner));
8882 : 55 : evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags));
8883 : 55 : evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname));
8884 : 55 : evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled));
8885 : :
8886 : : /* Decide whether we want to dump it */
8887 : 55 : selectDumpableObject(&(evtinfo[i].dobj), fout);
8888 : : }
8889 : :
8890 : 193 : PQclear(res);
8891 : :
8892 : 193 : destroyPQExpBuffer(query);
8893 : 193 : }
8894 : :
8895 : : /*
8896 : : * getProcLangs
8897 : : * get basic information about every procedural language in the system
8898 : : *
8899 : : * NB: this must run after getFuncs() because we assume we can do
8900 : : * findFuncByOid().
8901 : : */
8902 : : void
8903 : 193 : getProcLangs(Archive *fout)
8904 : : {
8905 : : PGresult *res;
8906 : : int ntups;
8907 : : int i;
8908 : 193 : PQExpBuffer query = createPQExpBuffer();
8909 : : ProcLangInfo *planginfo;
8910 : : int i_tableoid;
8911 : : int i_oid;
8912 : : int i_lanname;
8913 : : int i_lanpltrusted;
8914 : : int i_lanplcallfoid;
8915 : : int i_laninline;
8916 : : int i_lanvalidator;
8917 : : int i_lanacl;
8918 : : int i_acldefault;
8919 : : int i_lanowner;
8920 : :
8921 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
8922 : : "lanname, lanpltrusted, lanplcallfoid, "
8923 : : "laninline, lanvalidator, "
8924 : : "lanacl, "
8925 : : "acldefault('l', lanowner) AS acldefault, "
8926 : : "lanowner "
8927 : : "FROM pg_language "
8928 : : "WHERE lanispl "
8929 : : "ORDER BY oid");
8930 : :
8931 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8932 : :
8933 : 193 : ntups = PQntuples(res);
8934 : :
8935 : 193 : planginfo = pg_malloc_array(ProcLangInfo, ntups);
8936 : :
8937 : 193 : i_tableoid = PQfnumber(res, "tableoid");
8938 : 193 : i_oid = PQfnumber(res, "oid");
8939 : 193 : i_lanname = PQfnumber(res, "lanname");
8940 : 193 : i_lanpltrusted = PQfnumber(res, "lanpltrusted");
8941 : 193 : i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
8942 : 193 : i_laninline = PQfnumber(res, "laninline");
8943 : 193 : i_lanvalidator = PQfnumber(res, "lanvalidator");
8944 : 193 : i_lanacl = PQfnumber(res, "lanacl");
8945 : 193 : i_acldefault = PQfnumber(res, "acldefault");
8946 : 193 : i_lanowner = PQfnumber(res, "lanowner");
8947 : :
8948 [ + + ]: 434 : for (i = 0; i < ntups; i++)
8949 : : {
8950 : 241 : planginfo[i].dobj.objType = DO_PROCLANG;
8951 : 241 : planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8952 : 241 : planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8953 : 241 : AssignDumpId(&planginfo[i].dobj);
8954 : :
8955 : 241 : planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
8956 : 241 : planginfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_lanacl));
8957 : 241 : planginfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
8958 : 241 : planginfo[i].dacl.privtype = 0;
8959 : 241 : planginfo[i].dacl.initprivs = NULL;
8960 : 241 : planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
8961 : 241 : planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
8962 : 241 : planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
8963 : 241 : planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
8964 : 241 : planginfo[i].lanowner = getRoleName(PQgetvalue(res, i, i_lanowner));
8965 : :
8966 : : /* Decide whether we want to dump it */
8967 : 241 : selectDumpableProcLang(&(planginfo[i]), fout);
8968 : :
8969 : : /* Mark whether language has an ACL */
8970 [ + + ]: 241 : if (!PQgetisnull(res, i, i_lanacl))
8971 : 48 : planginfo[i].dobj.components |= DUMP_COMPONENT_ACL;
8972 : : }
8973 : :
8974 : 193 : PQclear(res);
8975 : :
8976 : 193 : destroyPQExpBuffer(query);
8977 : 193 : }
8978 : :
8979 : : /*
8980 : : * getCasts
8981 : : * get basic information about most casts in the system
8982 : : *
8983 : : * Skip casts from a range to its multirange, since we'll create those
8984 : : * automatically.
8985 : : */
8986 : : void
8987 : 193 : getCasts(Archive *fout)
8988 : : {
8989 : : PGresult *res;
8990 : : int ntups;
8991 : : int i;
8992 : 193 : PQExpBuffer query = createPQExpBuffer();
8993 : : CastInfo *castinfo;
8994 : : int i_tableoid;
8995 : : int i_oid;
8996 : : int i_castsource;
8997 : : int i_casttarget;
8998 : : int i_castfunc;
8999 : : int i_castcontext;
9000 : : int i_castmethod;
9001 : :
9002 [ + - ]: 193 : if (fout->remoteVersion >= 140000)
9003 : : {
9004 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9005 : : "castsource, casttarget, castfunc, castcontext, "
9006 : : "castmethod "
9007 : : "FROM pg_cast c "
9008 : : "WHERE NOT EXISTS ( "
9009 : : "SELECT 1 FROM pg_range r "
9010 : : "WHERE c.castsource = r.rngtypid "
9011 : : "AND c.casttarget = r.rngmultitypid "
9012 : : ") "
9013 : : "ORDER BY 3,4");
9014 : : }
9015 : : else
9016 : : {
9017 : 0 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9018 : : "castsource, casttarget, castfunc, castcontext, "
9019 : : "castmethod "
9020 : : "FROM pg_cast ORDER BY 3,4");
9021 : : }
9022 : :
9023 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9024 : :
9025 : 193 : ntups = PQntuples(res);
9026 : :
9027 : 193 : castinfo = pg_malloc_array(CastInfo, ntups);
9028 : :
9029 : 193 : i_tableoid = PQfnumber(res, "tableoid");
9030 : 193 : i_oid = PQfnumber(res, "oid");
9031 : 193 : i_castsource = PQfnumber(res, "castsource");
9032 : 193 : i_casttarget = PQfnumber(res, "casttarget");
9033 : 193 : i_castfunc = PQfnumber(res, "castfunc");
9034 : 193 : i_castcontext = PQfnumber(res, "castcontext");
9035 : 193 : i_castmethod = PQfnumber(res, "castmethod");
9036 : :
9037 [ + + ]: 47182 : for (i = 0; i < ntups; i++)
9038 : : {
9039 : : PQExpBufferData namebuf;
9040 : : TypeInfo *sTypeInfo;
9041 : : TypeInfo *tTypeInfo;
9042 : :
9043 : 46989 : castinfo[i].dobj.objType = DO_CAST;
9044 : 46989 : castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9045 : 46989 : castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9046 : 46989 : AssignDumpId(&castinfo[i].dobj);
9047 : 46989 : castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
9048 : 46989 : castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
9049 : 46989 : castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
9050 : 46989 : castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
9051 : 46989 : castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod));
9052 : :
9053 : : /*
9054 : : * Try to name cast as concatenation of typnames. This is only used
9055 : : * for purposes of sorting. If we fail to find either type, the name
9056 : : * will be an empty string.
9057 : : */
9058 : 46989 : initPQExpBuffer(&namebuf);
9059 : 46989 : sTypeInfo = findTypeByOid(castinfo[i].castsource);
9060 : 46989 : tTypeInfo = findTypeByOid(castinfo[i].casttarget);
9061 [ + - + - ]: 46989 : if (sTypeInfo && tTypeInfo)
9062 : 46989 : appendPQExpBuffer(&namebuf, "%s %s",
9063 : : sTypeInfo->dobj.name, tTypeInfo->dobj.name);
9064 : 46989 : castinfo[i].dobj.name = namebuf.data;
9065 : :
9066 : : /* Decide whether we want to dump it */
9067 : 46989 : selectDumpableCast(&(castinfo[i]), fout);
9068 : : }
9069 : :
9070 : 193 : PQclear(res);
9071 : :
9072 : 193 : destroyPQExpBuffer(query);
9073 : 193 : }
9074 : :
9075 : : static char *
9076 : 93 : get_language_name(Archive *fout, Oid langid)
9077 : : {
9078 : : PQExpBuffer query;
9079 : : PGresult *res;
9080 : : char *lanname;
9081 : :
9082 : 93 : query = createPQExpBuffer();
9083 : 93 : appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid);
9084 : 93 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
9085 : 93 : lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
9086 : 93 : destroyPQExpBuffer(query);
9087 : 93 : PQclear(res);
9088 : :
9089 : 93 : return lanname;
9090 : : }
9091 : :
9092 : : /*
9093 : : * getTransforms
9094 : : * get basic information about every transform in the system
9095 : : */
9096 : : void
9097 : 193 : getTransforms(Archive *fout)
9098 : : {
9099 : : PGresult *res;
9100 : : int ntups;
9101 : : int i;
9102 : : PQExpBuffer query;
9103 : : TransformInfo *transforminfo;
9104 : : int i_tableoid;
9105 : : int i_oid;
9106 : : int i_trftype;
9107 : : int i_trflang;
9108 : : int i_trffromsql;
9109 : : int i_trftosql;
9110 : :
9111 : 193 : query = createPQExpBuffer();
9112 : :
9113 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, "
9114 : : "trftype, trflang, trffromsql::oid, trftosql::oid "
9115 : : "FROM pg_transform "
9116 : : "ORDER BY 3,4");
9117 : :
9118 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9119 : :
9120 : 193 : ntups = PQntuples(res);
9121 : :
9122 : 193 : transforminfo = pg_malloc_array(TransformInfo, ntups);
9123 : :
9124 : 193 : i_tableoid = PQfnumber(res, "tableoid");
9125 : 193 : i_oid = PQfnumber(res, "oid");
9126 : 193 : i_trftype = PQfnumber(res, "trftype");
9127 : 193 : i_trflang = PQfnumber(res, "trflang");
9128 : 193 : i_trffromsql = PQfnumber(res, "trffromsql");
9129 : 193 : i_trftosql = PQfnumber(res, "trftosql");
9130 : :
9131 [ + + ]: 248 : for (i = 0; i < ntups; i++)
9132 : : {
9133 : : PQExpBufferData namebuf;
9134 : : TypeInfo *typeInfo;
9135 : : char *lanname;
9136 : :
9137 : 55 : transforminfo[i].dobj.objType = DO_TRANSFORM;
9138 : 55 : transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9139 : 55 : transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9140 : 55 : AssignDumpId(&transforminfo[i].dobj);
9141 : 55 : transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype));
9142 : 55 : transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang));
9143 : 55 : transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql));
9144 : 55 : transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql));
9145 : :
9146 : : /*
9147 : : * Try to name transform as concatenation of type and language name.
9148 : : * This is only used for purposes of sorting. If we fail to find
9149 : : * either, the name will be an empty string.
9150 : : */
9151 : 55 : initPQExpBuffer(&namebuf);
9152 : 55 : typeInfo = findTypeByOid(transforminfo[i].trftype);
9153 : 55 : lanname = get_language_name(fout, transforminfo[i].trflang);
9154 [ + - + - ]: 55 : if (typeInfo && lanname)
9155 : 55 : appendPQExpBuffer(&namebuf, "%s %s",
9156 : : typeInfo->dobj.name, lanname);
9157 : 55 : transforminfo[i].dobj.name = namebuf.data;
9158 : 55 : free(lanname);
9159 : :
9160 : : /* Decide whether we want to dump it */
9161 : 55 : selectDumpableObject(&(transforminfo[i].dobj), fout);
9162 : : }
9163 : :
9164 : 193 : PQclear(res);
9165 : :
9166 : 193 : destroyPQExpBuffer(query);
9167 : 193 : }
9168 : :
9169 : : /*
9170 : : * getTableAttrs -
9171 : : * for each interesting table, read info about its attributes
9172 : : * (names, types, default values, CHECK constraints, etc)
9173 : : *
9174 : : * modifies tblinfo
9175 : : */
9176 : : void
9177 : 193 : getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
9178 : : {
9179 : 193 : DumpOptions *dopt = fout->dopt;
9180 : 193 : PQExpBuffer q = createPQExpBuffer();
9181 : 193 : PQExpBuffer tbloids = createPQExpBuffer();
9182 : 193 : PQExpBuffer checkoids = createPQExpBuffer();
9183 : 193 : PQExpBuffer invalidnotnulloids = NULL;
9184 : : PGresult *res;
9185 : : int ntups;
9186 : : int curtblindx;
9187 : : int i_attrelid;
9188 : : int i_attnum;
9189 : : int i_attname;
9190 : : int i_atttypname;
9191 : : int i_attstattarget;
9192 : : int i_attstorage;
9193 : : int i_typstorage;
9194 : : int i_attidentity;
9195 : : int i_attgenerated;
9196 : : int i_attisdropped;
9197 : : int i_attlen;
9198 : : int i_attalign;
9199 : : int i_attislocal;
9200 : : int i_notnull_name;
9201 : : int i_notnull_comment;
9202 : : int i_notnull_noinherit;
9203 : : int i_notnull_islocal;
9204 : : int i_notnull_invalidoid;
9205 : : int i_attoptions;
9206 : : int i_attcollation;
9207 : : int i_attcompression;
9208 : : int i_attfdwoptions;
9209 : : int i_attmissingval;
9210 : : int i_atthasdef;
9211 : :
9212 : : /*
9213 : : * We want to perform just one query against pg_attribute, and then just
9214 : : * one against pg_attrdef (for DEFAULTs) and two against pg_constraint
9215 : : * (for CHECK constraints and for NOT NULL constraints). However, we
9216 : : * mustn't try to select every row of those catalogs and then sort it out
9217 : : * on the client side, because some of the server-side functions we need
9218 : : * would be unsafe to apply to tables we don't have lock on. Hence, we
9219 : : * build an array of the OIDs of tables we care about (and now have lock
9220 : : * on!), and use a WHERE clause to constrain which rows are selected.
9221 : : */
9222 : 193 : appendPQExpBufferChar(tbloids, '{');
9223 : 193 : appendPQExpBufferChar(checkoids, '{');
9224 [ + + ]: 55672 : for (int i = 0; i < numTables; i++)
9225 : : {
9226 : 55479 : TableInfo *tbinfo = &tblinfo[i];
9227 : :
9228 : : /* Don't bother to collect info for sequences */
9229 [ + + ]: 55479 : if (tbinfo->relkind == RELKIND_SEQUENCE)
9230 : 647 : continue;
9231 : :
9232 : : /*
9233 : : * Don't bother with uninteresting tables, either. For binary
9234 : : * upgrades, this is bypassed for pg_largeobject_metadata and
9235 : : * pg_shdepend so that the columns names are collected for the
9236 : : * corresponding COPY commands. Restoring the data for those catalogs
9237 : : * is faster than restoring the equivalent set of large object
9238 : : * commands.
9239 : : */
9240 [ + + ]: 54832 : if (!tbinfo->interesting &&
9241 [ + + ]: 47519 : !(fout->dopt->binary_upgrade &&
9242 [ + + ]: 9930 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9243 [ + + ]: 9888 : tbinfo->dobj.catId.oid == SharedDependRelationId)))
9244 : 47435 : continue;
9245 : :
9246 : : /* OK, we need info for this table */
9247 [ + + ]: 7397 : if (tbloids->len > 1) /* do we have more than the '{'? */
9248 : 7246 : appendPQExpBufferChar(tbloids, ',');
9249 : 7397 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9250 : :
9251 [ + + ]: 7397 : if (tbinfo->ncheck > 0)
9252 : : {
9253 : : /* Also make a list of the ones with check constraints */
9254 [ + + ]: 548 : if (checkoids->len > 1) /* do we have more than the '{'? */
9255 : 476 : appendPQExpBufferChar(checkoids, ',');
9256 : 548 : appendPQExpBuffer(checkoids, "%u", tbinfo->dobj.catId.oid);
9257 : : }
9258 : : }
9259 : 193 : appendPQExpBufferChar(tbloids, '}');
9260 : 193 : appendPQExpBufferChar(checkoids, '}');
9261 : :
9262 : : /*
9263 : : * Find all the user attributes and their types.
9264 : : *
9265 : : * Since we only want to dump COLLATE clauses for attributes whose
9266 : : * collation is different from their type's default, we use a CASE here to
9267 : : * suppress uninteresting attcollations cheaply.
9268 : : */
9269 : 193 : appendPQExpBufferStr(q,
9270 : : "SELECT\n"
9271 : : "a.attrelid,\n"
9272 : : "a.attnum,\n"
9273 : : "a.attname,\n"
9274 : : "a.attstattarget,\n"
9275 : : "a.attstorage,\n"
9276 : : "t.typstorage,\n"
9277 : : "a.atthasdef,\n"
9278 : : "a.attisdropped,\n"
9279 : : "a.attlen,\n"
9280 : : "a.attalign,\n"
9281 : : "a.attislocal,\n"
9282 : : "pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n"
9283 : : "array_to_string(a.attoptions, ', ') AS attoptions,\n"
9284 : : "CASE WHEN a.attcollation <> t.typcollation "
9285 : : "THEN a.attcollation ELSE 0 END AS attcollation,\n"
9286 : : "pg_catalog.array_to_string(ARRAY("
9287 : : "SELECT pg_catalog.quote_ident(option_name) || "
9288 : : "' ' || pg_catalog.quote_literal(option_value) "
9289 : : "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
9290 : : "ORDER BY option_name"
9291 : : "), E',\n ') AS attfdwoptions,\n");
9292 : :
9293 : : /*
9294 : : * Find out any NOT NULL markings for each column. In 18 and up we read
9295 : : * pg_constraint to obtain the constraint name, and for valid constraints
9296 : : * also pg_description to obtain its comment. notnull_noinherit is set
9297 : : * according to the NO INHERIT property. For versions prior to 18, we
9298 : : * store an empty string as the name when a constraint is marked as
9299 : : * attnotnull (this cues dumpTableSchema to print the NOT NULL clause
9300 : : * without a name); also, such cases are never NO INHERIT.
9301 : : *
9302 : : * For invalid constraints, we need to store their OIDs for processing
9303 : : * elsewhere, so we bring the pg_constraint.oid value when the constraint
9304 : : * is invalid, and NULL otherwise. Their comments are handled not here
9305 : : * but by collectComments, because they're their own dumpable object.
9306 : : *
9307 : : * We track in notnull_islocal whether the constraint was defined directly
9308 : : * in this table or via an ancestor, for binary upgrade. flagInhAttrs
9309 : : * might modify this later.
9310 : : */
9311 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
9312 : 193 : appendPQExpBufferStr(q,
9313 : : "co.conname AS notnull_name,\n"
9314 : : "CASE WHEN co.convalidated THEN pt.description"
9315 : : " ELSE NULL END AS notnull_comment,\n"
9316 : : "CASE WHEN NOT co.convalidated THEN co.oid "
9317 : : "ELSE NULL END AS notnull_invalidoid,\n"
9318 : : "co.connoinherit AS notnull_noinherit,\n"
9319 : : "co.conislocal AS notnull_islocal,\n");
9320 : : else
9321 : 0 : appendPQExpBufferStr(q,
9322 : : "CASE WHEN a.attnotnull THEN '' ELSE NULL END AS notnull_name,\n"
9323 : : "NULL AS notnull_comment,\n"
9324 : : "NULL AS notnull_invalidoid,\n"
9325 : : "false AS notnull_noinherit,\n"
9326 : : "CASE WHEN a.attislocal THEN true\n"
9327 : : " WHEN a.attnotnull AND NOT a.attislocal THEN true\n"
9328 : : " ELSE false\n"
9329 : : "END AS notnull_islocal,\n");
9330 : :
9331 [ + - ]: 193 : if (fout->remoteVersion >= 140000)
9332 : 193 : appendPQExpBufferStr(q,
9333 : : "a.attcompression AS attcompression,\n");
9334 : : else
9335 : 0 : appendPQExpBufferStr(q,
9336 : : "'' AS attcompression,\n");
9337 : :
9338 : 193 : appendPQExpBufferStr(q,
9339 : : "a.attidentity,\n");
9340 : :
9341 [ + - ]: 193 : if (fout->remoteVersion >= 110000)
9342 : 193 : appendPQExpBufferStr(q,
9343 : : "CASE WHEN a.atthasmissing AND NOT a.attisdropped "
9344 : : "THEN a.attmissingval ELSE null END AS attmissingval,\n");
9345 : : else
9346 : 0 : appendPQExpBufferStr(q,
9347 : : "NULL AS attmissingval,\n");
9348 : :
9349 [ + - ]: 193 : if (fout->remoteVersion >= 120000)
9350 : 193 : appendPQExpBufferStr(q,
9351 : : "a.attgenerated\n");
9352 : : else
9353 : 0 : appendPQExpBufferStr(q,
9354 : : "'' AS attgenerated\n");
9355 : :
9356 : : /* need left join to pg_type to not fail on dropped columns ... */
9357 : 193 : appendPQExpBuffer(q,
9358 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9359 : : "JOIN pg_catalog.pg_attribute a ON (src.tbloid = a.attrelid) "
9360 : : "LEFT JOIN pg_catalog.pg_type t "
9361 : : "ON (a.atttypid = t.oid)\n",
9362 : : tbloids->data);
9363 : :
9364 : : /*
9365 : : * In versions 18 and up, we need pg_constraint for explicit NOT NULL
9366 : : * entries and pg_description to get their comments.
9367 : : */
9368 [ + - ]: 193 : if (fout->remoteVersion >= 180000)
9369 : 193 : appendPQExpBufferStr(q,
9370 : : " LEFT JOIN pg_catalog.pg_constraint co ON "
9371 : : "(a.attrelid = co.conrelid\n"
9372 : : " AND co.contype = 'n' AND "
9373 : : "co.conkey = array[a.attnum])\n"
9374 : : " LEFT JOIN pg_catalog.pg_description pt ON "
9375 : : "(pt.classoid = co.tableoid AND pt.objoid = co.oid)\n");
9376 : :
9377 : 193 : appendPQExpBufferStr(q,
9378 : : "WHERE a.attnum > 0::pg_catalog.int2\n");
9379 : :
9380 : : /*
9381 : : * For binary upgrades from <v12, be sure to pick up
9382 : : * pg_largeobject_metadata's oid column.
9383 : : */
9384 [ + + - + ]: 193 : if (fout->dopt->binary_upgrade && fout->remoteVersion < 120000)
9385 : 0 : appendPQExpBufferStr(q,
9386 : : "OR (a.attnum = -2::pg_catalog.int2 AND src.tbloid = "
9387 : : CppAsString2(LargeObjectMetadataRelationId) ")\n");
9388 : :
9389 : 193 : appendPQExpBufferStr(q,
9390 : : "ORDER BY a.attrelid, a.attnum");
9391 : :
9392 : 193 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9393 : :
9394 : 193 : ntups = PQntuples(res);
9395 : :
9396 : 193 : i_attrelid = PQfnumber(res, "attrelid");
9397 : 193 : i_attnum = PQfnumber(res, "attnum");
9398 : 193 : i_attname = PQfnumber(res, "attname");
9399 : 193 : i_atttypname = PQfnumber(res, "atttypname");
9400 : 193 : i_attstattarget = PQfnumber(res, "attstattarget");
9401 : 193 : i_attstorage = PQfnumber(res, "attstorage");
9402 : 193 : i_typstorage = PQfnumber(res, "typstorage");
9403 : 193 : i_attidentity = PQfnumber(res, "attidentity");
9404 : 193 : i_attgenerated = PQfnumber(res, "attgenerated");
9405 : 193 : i_attisdropped = PQfnumber(res, "attisdropped");
9406 : 193 : i_attlen = PQfnumber(res, "attlen");
9407 : 193 : i_attalign = PQfnumber(res, "attalign");
9408 : 193 : i_attislocal = PQfnumber(res, "attislocal");
9409 : 193 : i_notnull_name = PQfnumber(res, "notnull_name");
9410 : 193 : i_notnull_comment = PQfnumber(res, "notnull_comment");
9411 : 193 : i_notnull_invalidoid = PQfnumber(res, "notnull_invalidoid");
9412 : 193 : i_notnull_noinherit = PQfnumber(res, "notnull_noinherit");
9413 : 193 : i_notnull_islocal = PQfnumber(res, "notnull_islocal");
9414 : 193 : i_attoptions = PQfnumber(res, "attoptions");
9415 : 193 : i_attcollation = PQfnumber(res, "attcollation");
9416 : 193 : i_attcompression = PQfnumber(res, "attcompression");
9417 : 193 : i_attfdwoptions = PQfnumber(res, "attfdwoptions");
9418 : 193 : i_attmissingval = PQfnumber(res, "attmissingval");
9419 : 193 : i_atthasdef = PQfnumber(res, "atthasdef");
9420 : :
9421 : : /* Within the next loop, we'll accumulate OIDs of tables with defaults */
9422 : 193 : resetPQExpBuffer(tbloids);
9423 : 193 : appendPQExpBufferChar(tbloids, '{');
9424 : :
9425 : : /*
9426 : : * Outer loop iterates once per table, not once per row. Incrementing of
9427 : : * r is handled by the inner loop.
9428 : : */
9429 : 193 : curtblindx = -1;
9430 [ + + ]: 7332 : for (int r = 0; r < ntups;)
9431 : : {
9432 : 7139 : Oid attrelid = atooid(PQgetvalue(res, r, i_attrelid));
9433 : 7139 : TableInfo *tbinfo = NULL;
9434 : : int numatts;
9435 : : bool hasdefaults;
9436 : :
9437 : : /* Count rows for this table */
9438 [ + + ]: 26599 : for (numatts = 1; numatts < ntups - r; numatts++)
9439 [ + + ]: 26451 : if (atooid(PQgetvalue(res, r + numatts, i_attrelid)) != attrelid)
9440 : 6991 : break;
9441 : :
9442 : : /*
9443 : : * Locate the associated TableInfo; we rely on tblinfo[] being in OID
9444 : : * order.
9445 : : */
9446 [ + - ]: 38287 : while (++curtblindx < numTables)
9447 : : {
9448 : 38287 : tbinfo = &tblinfo[curtblindx];
9449 [ + + ]: 38287 : if (tbinfo->dobj.catId.oid == attrelid)
9450 : 7139 : break;
9451 : : }
9452 [ - + ]: 7139 : if (curtblindx >= numTables)
9453 : 0 : pg_fatal("unrecognized table OID %u", attrelid);
9454 : : /* cross-check that we only got requested tables */
9455 [ + - ]: 7139 : if (tbinfo->relkind == RELKIND_SEQUENCE ||
9456 [ + + ]: 7139 : (!tbinfo->interesting &&
9457 [ + - ]: 84 : !(fout->dopt->binary_upgrade &&
9458 [ + + ]: 84 : (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId ||
9459 [ - + ]: 42 : tbinfo->dobj.catId.oid == SharedDependRelationId))))
9460 : 0 : pg_fatal("unexpected column data for table \"%s\"",
9461 : : tbinfo->dobj.name);
9462 : :
9463 : : /* Save data for this table */
9464 : 7139 : tbinfo->numatts = numatts;
9465 : 7139 : tbinfo->attnames = pg_malloc_array(char *, numatts);
9466 : 7139 : tbinfo->atttypnames = pg_malloc_array(char *, numatts);
9467 : 7139 : tbinfo->attstattarget = pg_malloc_array(int, numatts);
9468 : 7139 : tbinfo->attstorage = pg_malloc_array(char, numatts);
9469 : 7139 : tbinfo->typstorage = pg_malloc_array(char, numatts);
9470 : 7139 : tbinfo->attidentity = pg_malloc_array(char, numatts);
9471 : 7139 : tbinfo->attgenerated = pg_malloc_array(char, numatts);
9472 : 7139 : tbinfo->attisdropped = pg_malloc_array(bool, numatts);
9473 : 7139 : tbinfo->attlen = pg_malloc_array(int, numatts);
9474 : 7139 : tbinfo->attalign = pg_malloc_array(char, numatts);
9475 : 7139 : tbinfo->attislocal = pg_malloc_array(bool, numatts);
9476 : 7139 : tbinfo->attoptions = pg_malloc_array(char *, numatts);
9477 : 7139 : tbinfo->attcollation = pg_malloc_array(Oid, numatts);
9478 : 7139 : tbinfo->attcompression = pg_malloc_array(char, numatts);
9479 : 7139 : tbinfo->attfdwoptions = pg_malloc_array(char *, numatts);
9480 : 7139 : tbinfo->attmissingval = pg_malloc_array(char *, numatts);
9481 : 7139 : tbinfo->notnull_constrs = pg_malloc_array(char *, numatts);
9482 : 7139 : tbinfo->notnull_comment = pg_malloc_array(char *, numatts);
9483 : 7139 : tbinfo->notnull_invalid = pg_malloc_array(bool, numatts);
9484 : 7139 : tbinfo->notnull_noinh = pg_malloc_array(bool, numatts);
9485 : 7139 : tbinfo->notnull_islocal = pg_malloc_array(bool, numatts);
9486 : 7139 : tbinfo->attrdefs = pg_malloc_array(AttrDefInfo *, numatts);
9487 : 7139 : hasdefaults = false;
9488 : :
9489 [ + + ]: 33738 : for (int j = 0; j < numatts; j++, r++)
9490 : : {
9491 [ - + ]: 26599 : if (j + 1 != atoi(PQgetvalue(res, r, i_attnum)) &&
9492 [ # # # # ]: 0 : !(fout->dopt->binary_upgrade && fout->remoteVersion < 120000 &&
9493 [ # # ]: 0 : tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId))
9494 : 0 : pg_fatal("invalid column numbering in table \"%s\"",
9495 : : tbinfo->dobj.name);
9496 : 26599 : tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, r, i_attname));
9497 : 26599 : tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, r, i_atttypname));
9498 [ + + ]: 26599 : if (PQgetisnull(res, r, i_attstattarget))
9499 : 26556 : tbinfo->attstattarget[j] = -1;
9500 : : else
9501 : 43 : tbinfo->attstattarget[j] = atoi(PQgetvalue(res, r, i_attstattarget));
9502 : 26599 : tbinfo->attstorage[j] = *(PQgetvalue(res, r, i_attstorage));
9503 : 26599 : tbinfo->typstorage[j] = *(PQgetvalue(res, r, i_typstorage));
9504 : 26599 : tbinfo->attidentity[j] = *(PQgetvalue(res, r, i_attidentity));
9505 : 26599 : tbinfo->attgenerated[j] = *(PQgetvalue(res, r, i_attgenerated));
9506 [ + + + + ]: 26599 : tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
9507 : 26599 : tbinfo->attisdropped[j] = (PQgetvalue(res, r, i_attisdropped)[0] == 't');
9508 : 26599 : tbinfo->attlen[j] = atoi(PQgetvalue(res, r, i_attlen));
9509 : 26599 : tbinfo->attalign[j] = *(PQgetvalue(res, r, i_attalign));
9510 : 26599 : tbinfo->attislocal[j] = (PQgetvalue(res, r, i_attislocal)[0] == 't');
9511 : :
9512 : : /* Handle not-null constraint name and flags */
9513 : 26599 : determineNotNullFlags(fout, res, r,
9514 : : tbinfo, j,
9515 : : i_notnull_name,
9516 : : i_notnull_comment,
9517 : : i_notnull_invalidoid,
9518 : : i_notnull_noinherit,
9519 : : i_notnull_islocal,
9520 : : &invalidnotnulloids);
9521 : :
9522 : 26599 : tbinfo->notnull_comment[j] = PQgetisnull(res, r, i_notnull_comment) ?
9523 [ + + ]: 26599 : NULL : pg_strdup(PQgetvalue(res, r, i_notnull_comment));
9524 : 26599 : tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, r, i_attoptions));
9525 : 26599 : tbinfo->attcollation[j] = atooid(PQgetvalue(res, r, i_attcollation));
9526 : 26599 : tbinfo->attcompression[j] = *(PQgetvalue(res, r, i_attcompression));
9527 : 26599 : tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, r, i_attfdwoptions));
9528 : 26599 : tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, r, i_attmissingval));
9529 : 26599 : tbinfo->attrdefs[j] = NULL; /* fix below */
9530 [ + + ]: 26599 : if (PQgetvalue(res, r, i_atthasdef)[0] == 't')
9531 : 1376 : hasdefaults = true;
9532 : : }
9533 : :
9534 [ + + ]: 7139 : if (hasdefaults)
9535 : : {
9536 : : /* Collect OIDs of interesting tables that have defaults */
9537 [ + + ]: 1026 : if (tbloids->len > 1) /* do we have more than the '{'? */
9538 : 955 : appendPQExpBufferChar(tbloids, ',');
9539 : 1026 : appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
9540 : : }
9541 : : }
9542 : :
9543 : : /* If invalidnotnulloids has any data, finalize it */
9544 [ + + ]: 193 : if (invalidnotnulloids != NULL)
9545 : 46 : appendPQExpBufferChar(invalidnotnulloids, '}');
9546 : :
9547 : 193 : PQclear(res);
9548 : :
9549 : : /*
9550 : : * Now get info about column defaults. This is skipped for a data-only
9551 : : * dump, as it is only needed for table schemas.
9552 : : */
9553 [ + + + + ]: 193 : if (dopt->dumpSchema && tbloids->len > 1)
9554 : : {
9555 : : AttrDefInfo *attrdefs;
9556 : : int numDefaults;
9557 : 62 : TableInfo *tbinfo = NULL;
9558 : :
9559 : 62 : pg_log_info("finding table default expressions");
9560 : :
9561 : 62 : appendPQExpBufferChar(tbloids, '}');
9562 : :
9563 : 62 : printfPQExpBuffer(q, "SELECT a.tableoid, a.oid, adrelid, adnum, "
9564 : : "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc\n"
9565 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9566 : : "JOIN pg_catalog.pg_attrdef a ON (src.tbloid = a.adrelid)\n"
9567 : : "ORDER BY a.adrelid, a.adnum",
9568 : : tbloids->data);
9569 : :
9570 : 62 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9571 : :
9572 : 62 : numDefaults = PQntuples(res);
9573 : 62 : attrdefs = pg_malloc_array(AttrDefInfo, numDefaults);
9574 : :
9575 : 62 : curtblindx = -1;
9576 [ + + ]: 1329 : for (int j = 0; j < numDefaults; j++)
9577 : : {
9578 : 1267 : Oid adtableoid = atooid(PQgetvalue(res, j, 0));
9579 : 1267 : Oid adoid = atooid(PQgetvalue(res, j, 1));
9580 : 1267 : Oid adrelid = atooid(PQgetvalue(res, j, 2));
9581 : 1267 : int adnum = atoi(PQgetvalue(res, j, 3));
9582 : 1267 : char *adsrc = PQgetvalue(res, j, 4);
9583 : :
9584 : : /*
9585 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9586 : : * OID order.
9587 : : */
9588 [ + + + + ]: 1267 : if (tbinfo == NULL || tbinfo->dobj.catId.oid != adrelid)
9589 : : {
9590 [ + - ]: 20975 : while (++curtblindx < numTables)
9591 : : {
9592 : 20975 : tbinfo = &tblinfo[curtblindx];
9593 [ + + ]: 20975 : if (tbinfo->dobj.catId.oid == adrelid)
9594 : 951 : break;
9595 : : }
9596 [ - + ]: 951 : if (curtblindx >= numTables)
9597 : 0 : pg_fatal("unrecognized table OID %u", adrelid);
9598 : : }
9599 : :
9600 [ + - - + ]: 1267 : if (adnum <= 0 || adnum > tbinfo->numatts)
9601 : 0 : pg_fatal("invalid adnum value %d for table \"%s\"",
9602 : : adnum, tbinfo->dobj.name);
9603 : :
9604 : : /*
9605 : : * dropped columns shouldn't have defaults, but just in case,
9606 : : * ignore 'em
9607 : : */
9608 [ - + ]: 1267 : if (tbinfo->attisdropped[adnum - 1])
9609 : 0 : continue;
9610 : :
9611 : 1267 : attrdefs[j].dobj.objType = DO_ATTRDEF;
9612 : 1267 : attrdefs[j].dobj.catId.tableoid = adtableoid;
9613 : 1267 : attrdefs[j].dobj.catId.oid = adoid;
9614 : 1267 : AssignDumpId(&attrdefs[j].dobj);
9615 : 1267 : attrdefs[j].adtable = tbinfo;
9616 : 1267 : attrdefs[j].adnum = adnum;
9617 : 1267 : attrdefs[j].adef_expr = pg_strdup(adsrc);
9618 : :
9619 : 1267 : attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name);
9620 : 1267 : attrdefs[j].dobj.namespace = tbinfo->dobj.namespace;
9621 : :
9622 : 1267 : attrdefs[j].dobj.dump = tbinfo->dobj.dump;
9623 : :
9624 : : /*
9625 : : * Figure out whether the default/generation expression should be
9626 : : * dumped as part of the main CREATE TABLE (or similar) command or
9627 : : * as a separate ALTER TABLE (or similar) command. The preference
9628 : : * is to put it into the CREATE command, but in some cases that's
9629 : : * not possible.
9630 : : */
9631 [ + + ]: 1267 : if (tbinfo->attgenerated[adnum - 1])
9632 : : {
9633 : : /*
9634 : : * Column generation expressions cannot be dumped separately,
9635 : : * because there is no syntax for it. By setting separate to
9636 : : * false here we prevent the "default" from being processed as
9637 : : * its own dumpable object. Later, flagInhAttrs() will mark
9638 : : * it as not to be dumped at all, if possible (that is, if it
9639 : : * can be inherited from a parent).
9640 : : */
9641 : 722 : attrdefs[j].separate = false;
9642 : : }
9643 [ + + ]: 545 : else if (tbinfo->relkind == RELKIND_VIEW)
9644 : : {
9645 : : /*
9646 : : * Defaults on a VIEW must always be dumped as separate ALTER
9647 : : * TABLE commands.
9648 : : */
9649 : 34 : attrdefs[j].separate = true;
9650 : : }
9651 [ + + ]: 511 : else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1))
9652 : : {
9653 : : /* column will be suppressed, print default separately */
9654 : 4 : attrdefs[j].separate = true;
9655 : : }
9656 : : else
9657 : : {
9658 : 507 : attrdefs[j].separate = false;
9659 : : }
9660 : :
9661 [ + + ]: 1267 : if (!attrdefs[j].separate)
9662 : : {
9663 : : /*
9664 : : * Mark the default as needing to appear before the table, so
9665 : : * that any dependencies it has must be emitted before the
9666 : : * CREATE TABLE. If this is not possible, we'll change to
9667 : : * "separate" mode while sorting dependencies.
9668 : : */
9669 : 1229 : addObjectDependency(&tbinfo->dobj,
9670 : 1229 : attrdefs[j].dobj.dumpId);
9671 : : }
9672 : :
9673 : 1267 : tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
9674 : : }
9675 : :
9676 : 62 : PQclear(res);
9677 : : }
9678 : :
9679 : : /*
9680 : : * Get info about NOT NULL NOT VALID constraints. This is skipped for a
9681 : : * data-only dump, as it is only needed for table schemas.
9682 : : */
9683 [ + + + + ]: 193 : if (dopt->dumpSchema && invalidnotnulloids)
9684 : : {
9685 : : ConstraintInfo *constrs;
9686 : : int numConstrs;
9687 : : int i_tableoid;
9688 : : int i_oid;
9689 : : int i_conrelid;
9690 : : int i_conname;
9691 : : int i_consrc;
9692 : : int i_conislocal;
9693 : :
9694 : 39 : pg_log_info("finding invalid not-null constraints");
9695 : :
9696 : 39 : resetPQExpBuffer(q);
9697 : 39 : appendPQExpBuffer(q,
9698 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9699 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9700 : : "conislocal, convalidated "
9701 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(conoid)\n"
9702 : : "JOIN pg_catalog.pg_constraint c ON (src.conoid = c.oid)\n"
9703 : : "ORDER BY c.conrelid, c.conname",
9704 : 39 : invalidnotnulloids->data);
9705 : :
9706 : 39 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9707 : :
9708 : 39 : numConstrs = PQntuples(res);
9709 : 39 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9710 : :
9711 : 39 : i_tableoid = PQfnumber(res, "tableoid");
9712 : 39 : i_oid = PQfnumber(res, "oid");
9713 : 39 : i_conrelid = PQfnumber(res, "conrelid");
9714 : 39 : i_conname = PQfnumber(res, "conname");
9715 : 39 : i_consrc = PQfnumber(res, "consrc");
9716 : 39 : i_conislocal = PQfnumber(res, "conislocal");
9717 : :
9718 : : /* As above, this loop iterates once per table, not once per row */
9719 : 39 : curtblindx = -1;
9720 [ + + ]: 108 : for (int j = 0; j < numConstrs;)
9721 : : {
9722 : 69 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9723 : 69 : TableInfo *tbinfo = NULL;
9724 : : int numcons;
9725 : :
9726 : : /* Count rows for this table */
9727 [ + + ]: 69 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9728 [ + - ]: 30 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9729 : 30 : break;
9730 : :
9731 : : /*
9732 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9733 : : * OID order.
9734 : : */
9735 [ + - ]: 14191 : while (++curtblindx < numTables)
9736 : : {
9737 : 14191 : tbinfo = &tblinfo[curtblindx];
9738 [ + + ]: 14191 : if (tbinfo->dobj.catId.oid == conrelid)
9739 : 69 : break;
9740 : : }
9741 [ - + ]: 69 : if (curtblindx >= numTables)
9742 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9743 : :
9744 [ + + ]: 138 : for (int c = 0; c < numcons; c++, j++)
9745 : : {
9746 : 69 : constrs[j].dobj.objType = DO_CONSTRAINT;
9747 : 69 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9748 : 69 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9749 : 69 : AssignDumpId(&constrs[j].dobj);
9750 : 69 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9751 : 69 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9752 : 69 : constrs[j].contable = tbinfo;
9753 : 69 : constrs[j].condomain = NULL;
9754 : 69 : constrs[j].contype = 'n';
9755 : 69 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9756 : 69 : constrs[j].confrelid = InvalidOid;
9757 : 69 : constrs[j].conindex = 0;
9758 : 69 : constrs[j].condeferrable = false;
9759 : 69 : constrs[j].condeferred = false;
9760 : 69 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9761 : :
9762 : : /*
9763 : : * All invalid not-null constraints must be dumped separately,
9764 : : * because CREATE TABLE would not create them as invalid, and
9765 : : * also because they must be created after potentially
9766 : : * violating data has been loaded.
9767 : : */
9768 : 69 : constrs[j].separate = true;
9769 : :
9770 : 69 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9771 : : }
9772 : : }
9773 : 39 : PQclear(res);
9774 : : }
9775 : :
9776 : : /*
9777 : : * Get info about table CHECK constraints. This is skipped for a
9778 : : * data-only dump, as it is only needed for table schemas.
9779 : : */
9780 [ + + + + ]: 193 : if (dopt->dumpSchema && checkoids->len > 2)
9781 : : {
9782 : : ConstraintInfo *constrs;
9783 : : int numConstrs;
9784 : : int i_tableoid;
9785 : : int i_oid;
9786 : : int i_conrelid;
9787 : : int i_conname;
9788 : : int i_consrc;
9789 : : int i_conislocal;
9790 : : int i_convalidated;
9791 : :
9792 : 63 : pg_log_info("finding table check constraints");
9793 : :
9794 : 63 : resetPQExpBuffer(q);
9795 : 63 : appendPQExpBuffer(q,
9796 : : "SELECT c.tableoid, c.oid, conrelid, conname, "
9797 : : "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, "
9798 : : "conislocal, convalidated "
9799 : : "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
9800 : : "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n"
9801 : : "WHERE contype = 'c' "
9802 : : "ORDER BY c.conrelid, c.conname",
9803 : : checkoids->data);
9804 : :
9805 : 63 : res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
9806 : :
9807 : 63 : numConstrs = PQntuples(res);
9808 : 63 : constrs = pg_malloc_array(ConstraintInfo, numConstrs);
9809 : :
9810 : 63 : i_tableoid = PQfnumber(res, "tableoid");
9811 : 63 : i_oid = PQfnumber(res, "oid");
9812 : 63 : i_conrelid = PQfnumber(res, "conrelid");
9813 : 63 : i_conname = PQfnumber(res, "conname");
9814 : 63 : i_consrc = PQfnumber(res, "consrc");
9815 : 63 : i_conislocal = PQfnumber(res, "conislocal");
9816 : 63 : i_convalidated = PQfnumber(res, "convalidated");
9817 : :
9818 : : /* As above, this loop iterates once per table, not once per row */
9819 : 63 : curtblindx = -1;
9820 [ + + ]: 556 : for (int j = 0; j < numConstrs;)
9821 : : {
9822 : 493 : Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid));
9823 : 493 : TableInfo *tbinfo = NULL;
9824 : : int numcons;
9825 : :
9826 : : /* Count rows for this table */
9827 [ + + ]: 632 : for (numcons = 1; numcons < numConstrs - j; numcons++)
9828 [ + + ]: 569 : if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid)
9829 : 430 : break;
9830 : :
9831 : : /*
9832 : : * Locate the associated TableInfo; we rely on tblinfo[] being in
9833 : : * OID order.
9834 : : */
9835 [ + - ]: 20237 : while (++curtblindx < numTables)
9836 : : {
9837 : 20237 : tbinfo = &tblinfo[curtblindx];
9838 [ + + ]: 20237 : if (tbinfo->dobj.catId.oid == conrelid)
9839 : 493 : break;
9840 : : }
9841 [ - + ]: 493 : if (curtblindx >= numTables)
9842 : 0 : pg_fatal("unrecognized table OID %u", conrelid);
9843 : :
9844 [ - + ]: 493 : if (numcons != tbinfo->ncheck)
9845 : : {
9846 : 0 : pg_log_error(ngettext("expected %d check constraint on table \"%s\" but found %d",
9847 : : "expected %d check constraints on table \"%s\" but found %d",
9848 : : tbinfo->ncheck),
9849 : : tbinfo->ncheck, tbinfo->dobj.name, numcons);
9850 : 0 : pg_log_error_hint("The system catalogs might be corrupted.");
9851 : 0 : exit_nicely(1);
9852 : : }
9853 : :
9854 : 493 : tbinfo->checkexprs = constrs + j;
9855 : :
9856 [ + + ]: 1125 : for (int c = 0; c < numcons; c++, j++)
9857 : : {
9858 : 632 : bool validated = PQgetvalue(res, j, i_convalidated)[0] == 't';
9859 : :
9860 : 632 : constrs[j].dobj.objType = DO_CONSTRAINT;
9861 : 632 : constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
9862 : 632 : constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
9863 : 632 : AssignDumpId(&constrs[j].dobj);
9864 : 632 : constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
9865 : 632 : constrs[j].dobj.namespace = tbinfo->dobj.namespace;
9866 : 632 : constrs[j].contable = tbinfo;
9867 : 632 : constrs[j].condomain = NULL;
9868 : 632 : constrs[j].contype = 'c';
9869 : 632 : constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
9870 : 632 : constrs[j].confrelid = InvalidOid;
9871 : 632 : constrs[j].conindex = 0;
9872 : 632 : constrs[j].condeferrable = false;
9873 : 632 : constrs[j].condeferred = false;
9874 : 632 : constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');
9875 : :
9876 : : /*
9877 : : * An unvalidated constraint needs to be dumped separately, so
9878 : : * that potentially-violating existing data is loaded before
9879 : : * the constraint.
9880 : : */
9881 : 632 : constrs[j].separate = !validated;
9882 : :
9883 : 632 : constrs[j].dobj.dump = tbinfo->dobj.dump;
9884 : :
9885 : : /*
9886 : : * Mark the constraint as needing to appear before the table
9887 : : * --- this is so that any other dependencies of the
9888 : : * constraint will be emitted before we try to create the
9889 : : * table. If the constraint is to be dumped separately, it
9890 : : * will be dumped after data is loaded anyway, so don't do it.
9891 : : * (There's an automatic dependency in the opposite direction
9892 : : * anyway, so don't need to add one manually here.)
9893 : : */
9894 [ + + ]: 632 : if (!constrs[j].separate)
9895 : 567 : addObjectDependency(&tbinfo->dobj,
9896 : 567 : constrs[j].dobj.dumpId);
9897 : :
9898 : : /*
9899 : : * We will detect later whether the constraint must be split
9900 : : * out from the table definition.
9901 : : */
9902 : : }
9903 : : }
9904 : :
9905 : 63 : PQclear(res);
9906 : : }
9907 : :
9908 : 193 : destroyPQExpBuffer(q);
9909 : 193 : destroyPQExpBuffer(tbloids);
9910 : 193 : destroyPQExpBuffer(checkoids);
9911 : 193 : }
9912 : :
9913 : : /*
9914 : : * Based on the getTableAttrs query's row corresponding to one column, set
9915 : : * the name and flags to handle a not-null constraint for that column in
9916 : : * the tbinfo struct.
9917 : : *
9918 : : * Result row 'r' is for tbinfo's attribute 'j'.
9919 : : *
9920 : : * There are four possibilities:
9921 : : * 1) the column has no not-null constraints. In that case, ->notnull_constrs
9922 : : * (the constraint name) remains NULL.
9923 : : * 2) The column has a constraint with no name (this is the case when
9924 : : * constraints come from pre-18 servers). In this case, ->notnull_constrs
9925 : : * is set to the empty string; dumpTableSchema will print just "NOT NULL".
9926 : : * 3) The column has an invalid not-null constraint. This must be treated
9927 : : * as a separate object (because it must be created after the table data
9928 : : * is loaded). So we add its OID to invalidnotnulloids for processing
9929 : : * elsewhere and do nothing further with it here. We distinguish this
9930 : : * case because the "notnull_invalidoid" column has been set to a non-NULL
9931 : : * value, which is the constraint OID. Valid constraints have a null OID.
9932 : : * 4) The column has a constraint with a known name; in that case
9933 : : * notnull_constrs carries that name and dumpTableSchema will print
9934 : : * "CONSTRAINT the_name NOT NULL". However, if the name is the default
9935 : : * (table_column_not_null) and there's no comment on the constraint,
9936 : : * there's no need to print that name in the dump, so notnull_constrs
9937 : : * is set to the empty string and it behaves as case 2.
9938 : : *
9939 : : * In a child table that inherits from a parent already containing NOT NULL
9940 : : * constraints and the columns in the child don't have their own NOT NULL
9941 : : * declarations, we suppress printing constraints in the child: the
9942 : : * constraints are acquired at the point where the child is attached to the
9943 : : * parent. This is tracked in ->notnull_islocal; for servers pre-18 this is
9944 : : * set not here but in flagInhAttrs. That flag is also used when the
9945 : : * constraint was validated in a child but all its parent have it as NOT
9946 : : * VALID.
9947 : : *
9948 : : * Any of these constraints might have the NO INHERIT bit. If so we set
9949 : : * ->notnull_noinh and NO INHERIT will be printed by dumpTableSchema.
9950 : : *
9951 : : * In case 4 above, the name comparison is a bit of a hack; it actually fails
9952 : : * to do the right thing in all but the trivial case. However, the downside
9953 : : * of getting it wrong is simply that the name is printed rather than
9954 : : * suppressed, so it's not a big deal.
9955 : : *
9956 : : * invalidnotnulloids is expected to be given as NULL; if any invalid not-null
9957 : : * constraints are found, it is initialized and filled with the array of
9958 : : * OIDs of such constraints, for later processing.
9959 : : */
9960 : : static void
9961 : 26599 : determineNotNullFlags(Archive *fout, PGresult *res, int r,
9962 : : TableInfo *tbinfo, int j,
9963 : : int i_notnull_name,
9964 : : int i_notnull_comment,
9965 : : int i_notnull_invalidoid,
9966 : : int i_notnull_noinherit,
9967 : : int i_notnull_islocal,
9968 : : PQExpBuffer *invalidnotnulloids)
9969 : : {
9970 : 26599 : DumpOptions *dopt = fout->dopt;
9971 : :
9972 : : /*
9973 : : * If this not-null constraint is not valid, list its OID in
9974 : : * invalidnotnulloids and do nothing further. It'll be processed
9975 : : * elsewhere later.
9976 : : *
9977 : : * Because invalid not-null constraints are rare, we don't want to malloc
9978 : : * invalidnotnulloids until we're sure we're going it need it, which
9979 : : * happens here.
9980 : : */
9981 [ + + ]: 26599 : if (!PQgetisnull(res, r, i_notnull_invalidoid))
9982 : : {
9983 : 76 : char *constroid = PQgetvalue(res, r, i_notnull_invalidoid);
9984 : :
9985 [ + + ]: 76 : if (*invalidnotnulloids == NULL)
9986 : : {
9987 : 46 : *invalidnotnulloids = createPQExpBuffer();
9988 : 46 : appendPQExpBufferChar(*invalidnotnulloids, '{');
9989 : 46 : appendPQExpBufferStr(*invalidnotnulloids, constroid);
9990 : : }
9991 : : else
9992 : 30 : appendPQExpBuffer(*invalidnotnulloids, ",%s", constroid);
9993 : :
9994 : : /*
9995 : : * Track when a parent constraint is invalid for the cases where a
9996 : : * child constraint has been validated independenly.
9997 : : */
9998 : 76 : tbinfo->notnull_invalid[j] = true;
9999 : :
10000 : : /* nothing else to do */
10001 : 76 : tbinfo->notnull_constrs[j] = NULL;
10002 : 76 : return;
10003 : : }
10004 : :
10005 : : /*
10006 : : * notnull_noinh is straight from the query result. notnull_islocal also,
10007 : : * though flagInhAttrs may change that one later.
10008 : : */
10009 : 26523 : tbinfo->notnull_noinh[j] = PQgetvalue(res, r, i_notnull_noinherit)[0] == 't';
10010 : 26523 : tbinfo->notnull_islocal[j] = PQgetvalue(res, r, i_notnull_islocal)[0] == 't';
10011 : 26523 : tbinfo->notnull_invalid[j] = false;
10012 : :
10013 : : /*
10014 : : * Determine a constraint name to use. If the column is not marked not-
10015 : : * null, we set NULL which cues ... to do nothing. An empty string says
10016 : : * to print an unnamed NOT NULL, and anything else is a constraint name to
10017 : : * use.
10018 : : */
10019 [ - + ]: 26523 : if (fout->remoteVersion < 180000)
10020 : : {
10021 : : /*
10022 : : * < 18 doesn't have not-null names, so an unnamed constraint is
10023 : : * sufficient.
10024 : : */
10025 [ # # ]: 0 : if (PQgetisnull(res, r, i_notnull_name))
10026 : 0 : tbinfo->notnull_constrs[j] = NULL;
10027 : : else
10028 : 0 : tbinfo->notnull_constrs[j] = "";
10029 : : }
10030 : : else
10031 : : {
10032 [ + + ]: 26523 : if (PQgetisnull(res, r, i_notnull_name))
10033 : 23550 : tbinfo->notnull_constrs[j] = NULL;
10034 : : else
10035 : : {
10036 : : /*
10037 : : * In binary upgrade of inheritance child tables, must have a
10038 : : * constraint name that we can UPDATE later; same if there's a
10039 : : * comment on the constraint.
10040 : : */
10041 [ + + ]: 2973 : if ((dopt->binary_upgrade &&
10042 [ + + ]: 375 : !tbinfo->ispartition &&
10043 [ + + + + ]: 3239 : !tbinfo->notnull_islocal[j]) ||
10044 : 2952 : !PQgetisnull(res, r, i_notnull_comment))
10045 : : {
10046 : 70 : tbinfo->notnull_constrs[j] =
10047 : 70 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10048 : : }
10049 : : else
10050 : : {
10051 : : char *default_name;
10052 : :
10053 : : /* XXX should match ChooseConstraintName better */
10054 : 2903 : default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name,
10055 : 2903 : tbinfo->attnames[j]);
10056 [ + + ]: 2903 : if (strcmp(default_name,
10057 : 2903 : PQgetvalue(res, r, i_notnull_name)) == 0)
10058 : 1939 : tbinfo->notnull_constrs[j] = "";
10059 : : else
10060 : : {
10061 : 964 : tbinfo->notnull_constrs[j] =
10062 : 964 : pstrdup(PQgetvalue(res, r, i_notnull_name));
10063 : : }
10064 : 2903 : pfree(default_name);
10065 : : }
10066 : : }
10067 : : }
10068 : : }
10069 : :
10070 : : /*
10071 : : * Test whether a column should be printed as part of table's CREATE TABLE.
10072 : : * Column number is zero-based.
10073 : : *
10074 : : * Normally this is always true, but it's false for dropped columns, as well
10075 : : * as those that were inherited without any local definition. (If we print
10076 : : * such a column it will mistakenly get pg_attribute.attislocal set to true.)
10077 : : * For partitions, it's always true, because we want the partitions to be
10078 : : * created independently and ATTACH PARTITION used afterwards.
10079 : : *
10080 : : * In binary_upgrade mode, we must print all columns and fix the attislocal/
10081 : : * attisdropped state later, so as to keep control of the physical column
10082 : : * order.
10083 : : *
10084 : : * This function exists because there are scattered nonobvious places that
10085 : : * must be kept in sync with this decision.
10086 : : */
10087 : : bool
10088 : 42970 : shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
10089 : : {
10090 [ + + ]: 42970 : if (dopt->binary_upgrade)
10091 : 6626 : return true;
10092 [ + + ]: 36344 : if (tbinfo->attisdropped[colno])
10093 : 738 : return false;
10094 [ + + + + ]: 35606 : return (tbinfo->attislocal[colno] || tbinfo->ispartition);
10095 : : }
10096 : :
10097 : :
10098 : : /*
10099 : : * getTSParsers:
10100 : : * get information about all text search parsers in the system catalogs
10101 : : */
10102 : : void
10103 : 193 : getTSParsers(Archive *fout)
10104 : : {
10105 : : PGresult *res;
10106 : : int ntups;
10107 : : int i;
10108 : : PQExpBuffer query;
10109 : : TSParserInfo *prsinfo;
10110 : : int i_tableoid;
10111 : : int i_oid;
10112 : : int i_prsname;
10113 : : int i_prsnamespace;
10114 : : int i_prsstart;
10115 : : int i_prstoken;
10116 : : int i_prsend;
10117 : : int i_prsheadline;
10118 : : int i_prslextype;
10119 : :
10120 : 193 : query = createPQExpBuffer();
10121 : :
10122 : : /*
10123 : : * find all text search objects, including builtin ones; we filter out
10124 : : * system-defined objects at dump-out time.
10125 : : */
10126 : :
10127 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, "
10128 : : "prsstart::oid, prstoken::oid, "
10129 : : "prsend::oid, prsheadline::oid, prslextype::oid "
10130 : : "FROM pg_ts_parser");
10131 : :
10132 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10133 : :
10134 : 193 : ntups = PQntuples(res);
10135 : :
10136 : 193 : prsinfo = pg_malloc_array(TSParserInfo, ntups);
10137 : :
10138 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10139 : 193 : i_oid = PQfnumber(res, "oid");
10140 : 193 : i_prsname = PQfnumber(res, "prsname");
10141 : 193 : i_prsnamespace = PQfnumber(res, "prsnamespace");
10142 : 193 : i_prsstart = PQfnumber(res, "prsstart");
10143 : 193 : i_prstoken = PQfnumber(res, "prstoken");
10144 : 193 : i_prsend = PQfnumber(res, "prsend");
10145 : 193 : i_prsheadline = PQfnumber(res, "prsheadline");
10146 : 193 : i_prslextype = PQfnumber(res, "prslextype");
10147 : :
10148 [ + + ]: 434 : for (i = 0; i < ntups; i++)
10149 : : {
10150 : 241 : prsinfo[i].dobj.objType = DO_TSPARSER;
10151 : 241 : prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10152 : 241 : prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10153 : 241 : AssignDumpId(&prsinfo[i].dobj);
10154 : 241 : prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
10155 : 482 : prsinfo[i].dobj.namespace =
10156 : 241 : findNamespace(atooid(PQgetvalue(res, i, i_prsnamespace)));
10157 : 241 : prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
10158 : 241 : prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
10159 : 241 : prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
10160 : 241 : prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline));
10161 : 241 : prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype));
10162 : :
10163 : : /* Decide whether we want to dump it */
10164 : 241 : selectDumpableObject(&(prsinfo[i].dobj), fout);
10165 : : }
10166 : :
10167 : 193 : PQclear(res);
10168 : :
10169 : 193 : destroyPQExpBuffer(query);
10170 : 193 : }
10171 : :
10172 : : /*
10173 : : * getTSDictionaries:
10174 : : * get information about all text search dictionaries in the system catalogs
10175 : : */
10176 : : void
10177 : 193 : getTSDictionaries(Archive *fout)
10178 : : {
10179 : : PGresult *res;
10180 : : int ntups;
10181 : : int i;
10182 : : PQExpBuffer query;
10183 : : TSDictInfo *dictinfo;
10184 : : int i_tableoid;
10185 : : int i_oid;
10186 : : int i_dictname;
10187 : : int i_dictnamespace;
10188 : : int i_dictowner;
10189 : : int i_dicttemplate;
10190 : : int i_dictinitoption;
10191 : :
10192 : 193 : query = createPQExpBuffer();
10193 : :
10194 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, dictname, "
10195 : : "dictnamespace, dictowner, "
10196 : : "dicttemplate, dictinitoption "
10197 : : "FROM pg_ts_dict");
10198 : :
10199 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10200 : :
10201 : 193 : ntups = PQntuples(res);
10202 : :
10203 : 193 : dictinfo = pg_malloc_array(TSDictInfo, ntups);
10204 : :
10205 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10206 : 193 : i_oid = PQfnumber(res, "oid");
10207 : 193 : i_dictname = PQfnumber(res, "dictname");
10208 : 193 : i_dictnamespace = PQfnumber(res, "dictnamespace");
10209 : 193 : i_dictowner = PQfnumber(res, "dictowner");
10210 : 193 : i_dictinitoption = PQfnumber(res, "dictinitoption");
10211 : 193 : i_dicttemplate = PQfnumber(res, "dicttemplate");
10212 : :
10213 [ + + ]: 6480 : for (i = 0; i < ntups; i++)
10214 : : {
10215 : 6287 : dictinfo[i].dobj.objType = DO_TSDICT;
10216 : 6287 : dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10217 : 6287 : dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10218 : 6287 : AssignDumpId(&dictinfo[i].dobj);
10219 : 6287 : dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
10220 : 12574 : dictinfo[i].dobj.namespace =
10221 : 6287 : findNamespace(atooid(PQgetvalue(res, i, i_dictnamespace)));
10222 : 6287 : dictinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_dictowner));
10223 : 6287 : dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
10224 [ + + ]: 6287 : if (PQgetisnull(res, i, i_dictinitoption))
10225 : 241 : dictinfo[i].dictinitoption = NULL;
10226 : : else
10227 : 6046 : dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption));
10228 : :
10229 : : /* Decide whether we want to dump it */
10230 : 6287 : selectDumpableObject(&(dictinfo[i].dobj), fout);
10231 : : }
10232 : :
10233 : 193 : PQclear(res);
10234 : :
10235 : 193 : destroyPQExpBuffer(query);
10236 : 193 : }
10237 : :
10238 : : /*
10239 : : * getTSTemplates:
10240 : : * get information about all text search templates in the system catalogs
10241 : : */
10242 : : void
10243 : 193 : getTSTemplates(Archive *fout)
10244 : : {
10245 : : PGresult *res;
10246 : : int ntups;
10247 : : int i;
10248 : : PQExpBuffer query;
10249 : : TSTemplateInfo *tmplinfo;
10250 : : int i_tableoid;
10251 : : int i_oid;
10252 : : int i_tmplname;
10253 : : int i_tmplnamespace;
10254 : : int i_tmplinit;
10255 : : int i_tmpllexize;
10256 : :
10257 : 193 : query = createPQExpBuffer();
10258 : :
10259 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, "
10260 : : "tmplnamespace, tmplinit::oid, tmpllexize::oid "
10261 : : "FROM pg_ts_template");
10262 : :
10263 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10264 : :
10265 : 193 : ntups = PQntuples(res);
10266 : :
10267 : 193 : tmplinfo = pg_malloc_array(TSTemplateInfo, ntups);
10268 : :
10269 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10270 : 193 : i_oid = PQfnumber(res, "oid");
10271 : 193 : i_tmplname = PQfnumber(res, "tmplname");
10272 : 193 : i_tmplnamespace = PQfnumber(res, "tmplnamespace");
10273 : 193 : i_tmplinit = PQfnumber(res, "tmplinit");
10274 : 193 : i_tmpllexize = PQfnumber(res, "tmpllexize");
10275 : :
10276 [ + + ]: 1206 : for (i = 0; i < ntups; i++)
10277 : : {
10278 : 1013 : tmplinfo[i].dobj.objType = DO_TSTEMPLATE;
10279 : 1013 : tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10280 : 1013 : tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10281 : 1013 : AssignDumpId(&tmplinfo[i].dobj);
10282 : 1013 : tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
10283 : 2026 : tmplinfo[i].dobj.namespace =
10284 : 1013 : findNamespace(atooid(PQgetvalue(res, i, i_tmplnamespace)));
10285 : 1013 : tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
10286 : 1013 : tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
10287 : :
10288 : : /* Decide whether we want to dump it */
10289 : 1013 : selectDumpableObject(&(tmplinfo[i].dobj), fout);
10290 : : }
10291 : :
10292 : 193 : PQclear(res);
10293 : :
10294 : 193 : destroyPQExpBuffer(query);
10295 : 193 : }
10296 : :
10297 : : /*
10298 : : * getTSConfigurations:
10299 : : * get information about all text search configurations
10300 : : */
10301 : : void
10302 : 193 : getTSConfigurations(Archive *fout)
10303 : : {
10304 : : PGresult *res;
10305 : : int ntups;
10306 : : int i;
10307 : : PQExpBuffer query;
10308 : : TSConfigInfo *cfginfo;
10309 : : int i_tableoid;
10310 : : int i_oid;
10311 : : int i_cfgname;
10312 : : int i_cfgnamespace;
10313 : : int i_cfgowner;
10314 : : int i_cfgparser;
10315 : :
10316 : 193 : query = createPQExpBuffer();
10317 : :
10318 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, cfgname, "
10319 : : "cfgnamespace, cfgowner, cfgparser "
10320 : : "FROM pg_ts_config");
10321 : :
10322 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10323 : :
10324 : 193 : ntups = PQntuples(res);
10325 : :
10326 : 193 : cfginfo = pg_malloc_array(TSConfigInfo, ntups);
10327 : :
10328 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10329 : 193 : i_oid = PQfnumber(res, "oid");
10330 : 193 : i_cfgname = PQfnumber(res, "cfgname");
10331 : 193 : i_cfgnamespace = PQfnumber(res, "cfgnamespace");
10332 : 193 : i_cfgowner = PQfnumber(res, "cfgowner");
10333 : 193 : i_cfgparser = PQfnumber(res, "cfgparser");
10334 : :
10335 [ + + ]: 6445 : for (i = 0; i < ntups; i++)
10336 : : {
10337 : 6252 : cfginfo[i].dobj.objType = DO_TSCONFIG;
10338 : 6252 : cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10339 : 6252 : cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10340 : 6252 : AssignDumpId(&cfginfo[i].dobj);
10341 : 6252 : cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
10342 : 12504 : cfginfo[i].dobj.namespace =
10343 : 6252 : findNamespace(atooid(PQgetvalue(res, i, i_cfgnamespace)));
10344 : 6252 : cfginfo[i].rolname = getRoleName(PQgetvalue(res, i, i_cfgowner));
10345 : 6252 : cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
10346 : :
10347 : : /* Decide whether we want to dump it */
10348 : 6252 : selectDumpableObject(&(cfginfo[i].dobj), fout);
10349 : : }
10350 : :
10351 : 193 : PQclear(res);
10352 : :
10353 : 193 : destroyPQExpBuffer(query);
10354 : 193 : }
10355 : :
10356 : : /*
10357 : : * getForeignDataWrappers:
10358 : : * get information about all foreign-data wrappers in the system catalogs
10359 : : */
10360 : : void
10361 : 193 : getForeignDataWrappers(Archive *fout)
10362 : : {
10363 : : PGresult *res;
10364 : : int ntups;
10365 : : int i;
10366 : : PQExpBuffer query;
10367 : : FdwInfo *fdwinfo;
10368 : : int i_tableoid;
10369 : : int i_oid;
10370 : : int i_fdwname;
10371 : : int i_fdwowner;
10372 : : int i_fdwhandler;
10373 : : int i_fdwvalidator;
10374 : : int i_fdwconnection;
10375 : : int i_fdwacl;
10376 : : int i_acldefault;
10377 : : int i_fdwoptions;
10378 : :
10379 : 193 : query = createPQExpBuffer();
10380 : :
10381 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, fdwname, "
10382 : : "fdwowner, "
10383 : : "fdwhandler::pg_catalog.regproc, "
10384 : : "fdwvalidator::pg_catalog.regproc, ");
10385 : :
10386 [ + - ]: 193 : if (fout->remoteVersion >= 190000)
10387 : 193 : appendPQExpBufferStr(query, "fdwconnection::pg_catalog.regproc, ");
10388 : : else
10389 : 0 : appendPQExpBufferStr(query, "'-' AS fdwconnection, ");
10390 : :
10391 : 193 : appendPQExpBufferStr(query,
10392 : : "fdwacl, "
10393 : : "acldefault('F', fdwowner) AS acldefault, "
10394 : : "array_to_string(ARRAY("
10395 : : "SELECT quote_ident(option_name) || ' ' || "
10396 : : "quote_literal(option_value) "
10397 : : "FROM pg_options_to_table(fdwoptions) "
10398 : : "ORDER BY option_name"
10399 : : "), E',\n ') AS fdwoptions "
10400 : : "FROM pg_foreign_data_wrapper");
10401 : :
10402 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10403 : :
10404 : 193 : ntups = PQntuples(res);
10405 : :
10406 : 193 : fdwinfo = pg_malloc_array(FdwInfo, ntups);
10407 : :
10408 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10409 : 193 : i_oid = PQfnumber(res, "oid");
10410 : 193 : i_fdwname = PQfnumber(res, "fdwname");
10411 : 193 : i_fdwowner = PQfnumber(res, "fdwowner");
10412 : 193 : i_fdwhandler = PQfnumber(res, "fdwhandler");
10413 : 193 : i_fdwvalidator = PQfnumber(res, "fdwvalidator");
10414 : 193 : i_fdwconnection = PQfnumber(res, "fdwconnection");
10415 : 193 : i_fdwacl = PQfnumber(res, "fdwacl");
10416 : 193 : i_acldefault = PQfnumber(res, "acldefault");
10417 : 193 : i_fdwoptions = PQfnumber(res, "fdwoptions");
10418 : :
10419 [ + + ]: 267 : for (i = 0; i < ntups; i++)
10420 : : {
10421 : 74 : fdwinfo[i].dobj.objType = DO_FDW;
10422 : 74 : fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10423 : 74 : fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10424 : 74 : AssignDumpId(&fdwinfo[i].dobj);
10425 : 74 : fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname));
10426 : 74 : fdwinfo[i].dobj.namespace = NULL;
10427 : 74 : fdwinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_fdwacl));
10428 : 74 : fdwinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10429 : 74 : fdwinfo[i].dacl.privtype = 0;
10430 : 74 : fdwinfo[i].dacl.initprivs = NULL;
10431 : 74 : fdwinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_fdwowner));
10432 : 74 : fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler));
10433 : 74 : fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator));
10434 : 74 : fdwinfo[i].fdwconnection = pg_strdup(PQgetvalue(res, i, i_fdwconnection));
10435 : 74 : fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions));
10436 : :
10437 : : /* Decide whether we want to dump it */
10438 : 74 : selectDumpableObject(&(fdwinfo[i].dobj), fout);
10439 : :
10440 : : /* Mark whether FDW has an ACL */
10441 [ + + ]: 74 : if (!PQgetisnull(res, i, i_fdwacl))
10442 : 48 : fdwinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10443 : : }
10444 : :
10445 : 193 : PQclear(res);
10446 : :
10447 : 193 : destroyPQExpBuffer(query);
10448 : 193 : }
10449 : :
10450 : : /*
10451 : : * getForeignServers:
10452 : : * get information about all foreign servers in the system catalogs
10453 : : */
10454 : : void
10455 : 193 : getForeignServers(Archive *fout)
10456 : : {
10457 : : PGresult *res;
10458 : : int ntups;
10459 : : int i;
10460 : : PQExpBuffer query;
10461 : : ForeignServerInfo *srvinfo;
10462 : : int i_tableoid;
10463 : : int i_oid;
10464 : : int i_srvname;
10465 : : int i_srvowner;
10466 : : int i_srvfdw;
10467 : : int i_srvtype;
10468 : : int i_srvversion;
10469 : : int i_srvacl;
10470 : : int i_acldefault;
10471 : : int i_srvoptions;
10472 : :
10473 : 193 : query = createPQExpBuffer();
10474 : :
10475 : 193 : appendPQExpBufferStr(query, "SELECT tableoid, oid, srvname, "
10476 : : "srvowner, "
10477 : : "srvfdw, srvtype, srvversion, srvacl, "
10478 : : "acldefault('S', srvowner) AS acldefault, "
10479 : : "array_to_string(ARRAY("
10480 : : "SELECT quote_ident(option_name) || ' ' || "
10481 : : "quote_literal(option_value) "
10482 : : "FROM pg_options_to_table(srvoptions) "
10483 : : "ORDER BY option_name"
10484 : : "), E',\n ') AS srvoptions "
10485 : : "FROM pg_foreign_server");
10486 : :
10487 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10488 : :
10489 : 193 : ntups = PQntuples(res);
10490 : :
10491 : 193 : srvinfo = pg_malloc_array(ForeignServerInfo, ntups);
10492 : :
10493 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10494 : 193 : i_oid = PQfnumber(res, "oid");
10495 : 193 : i_srvname = PQfnumber(res, "srvname");
10496 : 193 : i_srvowner = PQfnumber(res, "srvowner");
10497 : 193 : i_srvfdw = PQfnumber(res, "srvfdw");
10498 : 193 : i_srvtype = PQfnumber(res, "srvtype");
10499 : 193 : i_srvversion = PQfnumber(res, "srvversion");
10500 : 193 : i_srvacl = PQfnumber(res, "srvacl");
10501 : 193 : i_acldefault = PQfnumber(res, "acldefault");
10502 : 193 : i_srvoptions = PQfnumber(res, "srvoptions");
10503 : :
10504 [ + + ]: 271 : for (i = 0; i < ntups; i++)
10505 : : {
10506 : 78 : srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
10507 : 78 : srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10508 : 78 : srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10509 : 78 : AssignDumpId(&srvinfo[i].dobj);
10510 : 78 : srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname));
10511 : 78 : srvinfo[i].dobj.namespace = NULL;
10512 : 78 : srvinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_srvacl));
10513 : 78 : srvinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10514 : 78 : srvinfo[i].dacl.privtype = 0;
10515 : 78 : srvinfo[i].dacl.initprivs = NULL;
10516 : 78 : srvinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_srvowner));
10517 : 78 : srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw));
10518 : 78 : srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype));
10519 : 78 : srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion));
10520 : 78 : srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions));
10521 : :
10522 : : /* Decide whether we want to dump it */
10523 : 78 : selectDumpableObject(&(srvinfo[i].dobj), fout);
10524 : :
10525 : : /* Servers have user mappings */
10526 : 78 : srvinfo[i].dobj.components |= DUMP_COMPONENT_USERMAP;
10527 : :
10528 : : /* Mark whether server has an ACL */
10529 [ + + ]: 78 : if (!PQgetisnull(res, i, i_srvacl))
10530 : 48 : srvinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10531 : : }
10532 : :
10533 : 193 : PQclear(res);
10534 : :
10535 : 193 : destroyPQExpBuffer(query);
10536 : 193 : }
10537 : :
10538 : : /*
10539 : : * getDefaultACLs:
10540 : : * get information about all default ACL information in the system catalogs
10541 : : */
10542 : : void
10543 : 193 : getDefaultACLs(Archive *fout)
10544 : : {
10545 : 193 : DumpOptions *dopt = fout->dopt;
10546 : : DefaultACLInfo *daclinfo;
10547 : : PQExpBuffer query;
10548 : : PGresult *res;
10549 : : int i_oid;
10550 : : int i_tableoid;
10551 : : int i_defaclrole;
10552 : : int i_defaclnamespace;
10553 : : int i_defaclobjtype;
10554 : : int i_defaclacl;
10555 : : int i_acldefault;
10556 : : int i,
10557 : : ntups;
10558 : :
10559 : 193 : query = createPQExpBuffer();
10560 : :
10561 : : /*
10562 : : * Global entries (with defaclnamespace=0) replace the hard-wired default
10563 : : * ACL for their object type. We should dump them as deltas from the
10564 : : * default ACL, since that will be used as a starting point for
10565 : : * interpreting the ALTER DEFAULT PRIVILEGES commands. On the other hand,
10566 : : * non-global entries can only add privileges not revoke them. We must
10567 : : * dump those as-is (i.e., as deltas from an empty ACL).
10568 : : *
10569 : : * We can use defaclobjtype as the object type for acldefault(), except
10570 : : * for the case of 'S' (DEFACLOBJ_SEQUENCE) which must be converted to
10571 : : * 's'.
10572 : : */
10573 : 193 : appendPQExpBufferStr(query,
10574 : : "SELECT oid, tableoid, "
10575 : : "defaclrole, "
10576 : : "defaclnamespace, "
10577 : : "defaclobjtype, "
10578 : : "defaclacl, "
10579 : : "CASE WHEN defaclnamespace = 0 THEN "
10580 : : "acldefault(CASE WHEN defaclobjtype = 'S' "
10581 : : "THEN 's'::\"char\" ELSE defaclobjtype END, "
10582 : : "defaclrole) ELSE '{}' END AS acldefault "
10583 : : "FROM pg_default_acl");
10584 : :
10585 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10586 : :
10587 : 193 : ntups = PQntuples(res);
10588 : :
10589 : 193 : daclinfo = pg_malloc_array(DefaultACLInfo, ntups);
10590 : :
10591 : 193 : i_oid = PQfnumber(res, "oid");
10592 : 193 : i_tableoid = PQfnumber(res, "tableoid");
10593 : 193 : i_defaclrole = PQfnumber(res, "defaclrole");
10594 : 193 : i_defaclnamespace = PQfnumber(res, "defaclnamespace");
10595 : 193 : i_defaclobjtype = PQfnumber(res, "defaclobjtype");
10596 : 193 : i_defaclacl = PQfnumber(res, "defaclacl");
10597 : 193 : i_acldefault = PQfnumber(res, "acldefault");
10598 : :
10599 [ + + ]: 399 : for (i = 0; i < ntups; i++)
10600 : : {
10601 : 206 : Oid nspid = atooid(PQgetvalue(res, i, i_defaclnamespace));
10602 : :
10603 : 206 : daclinfo[i].dobj.objType = DO_DEFAULT_ACL;
10604 : 206 : daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
10605 : 206 : daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
10606 : 206 : AssignDumpId(&daclinfo[i].dobj);
10607 : : /* cheesy ... is it worth coming up with a better object name? */
10608 : 206 : daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
10609 : :
10610 [ + + ]: 206 : if (nspid != InvalidOid)
10611 : 96 : daclinfo[i].dobj.namespace = findNamespace(nspid);
10612 : : else
10613 : 110 : daclinfo[i].dobj.namespace = NULL;
10614 : :
10615 : 206 : daclinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_defaclacl));
10616 : 206 : daclinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault));
10617 : 206 : daclinfo[i].dacl.privtype = 0;
10618 : 206 : daclinfo[i].dacl.initprivs = NULL;
10619 : 206 : daclinfo[i].defaclrole = getRoleName(PQgetvalue(res, i, i_defaclrole));
10620 : 206 : daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype));
10621 : :
10622 : : /* Default ACLs are ACLs, of course */
10623 : 206 : daclinfo[i].dobj.components |= DUMP_COMPONENT_ACL;
10624 : :
10625 : : /* Decide whether we want to dump it */
10626 : 206 : selectDumpableDefaultACL(&(daclinfo[i]), dopt);
10627 : : }
10628 : :
10629 : 193 : PQclear(res);
10630 : :
10631 : 193 : destroyPQExpBuffer(query);
10632 : 193 : }
10633 : :
10634 : : /*
10635 : : * getRoleName -- look up the name of a role, given its OID
10636 : : *
10637 : : * In current usage, we don't expect failures, so error out for a bad OID.
10638 : : */
10639 : : static const char *
10640 : 634308 : getRoleName(const char *roleoid_str)
10641 : : {
10642 : 634308 : Oid roleoid = atooid(roleoid_str);
10643 : :
10644 : : /*
10645 : : * Do binary search to find the appropriate item.
10646 : : */
10647 [ + - ]: 634308 : if (nrolenames > 0)
10648 : : {
10649 : 634308 : RoleNameItem *low = &rolenames[0];
10650 : 634308 : RoleNameItem *high = &rolenames[nrolenames - 1];
10651 : :
10652 [ + - ]: 2537226 : while (low <= high)
10653 : : {
10654 : 2537226 : RoleNameItem *middle = low + (high - low) / 2;
10655 : :
10656 [ + + ]: 2537226 : if (roleoid < middle->roleoid)
10657 : 1901715 : high = middle - 1;
10658 [ + + ]: 635511 : else if (roleoid > middle->roleoid)
10659 : 1203 : low = middle + 1;
10660 : : else
10661 : 634308 : return middle->rolename; /* found a match */
10662 : : }
10663 : : }
10664 : :
10665 : 0 : pg_fatal("role with OID %u does not exist", roleoid);
10666 : : return NULL; /* keep compiler quiet */
10667 : : }
10668 : :
10669 : : /*
10670 : : * collectRoleNames --
10671 : : *
10672 : : * Construct a table of all known roles.
10673 : : * The table is sorted by OID for speed in lookup.
10674 : : */
10675 : : static void
10676 : 194 : collectRoleNames(Archive *fout)
10677 : : {
10678 : : PGresult *res;
10679 : : const char *query;
10680 : : int i;
10681 : :
10682 : 194 : query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
10683 : :
10684 : 194 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
10685 : :
10686 : 194 : nrolenames = PQntuples(res);
10687 : :
10688 : 194 : rolenames = pg_malloc_array(RoleNameItem, nrolenames);
10689 : :
10690 [ + + ]: 3738 : for (i = 0; i < nrolenames; i++)
10691 : : {
10692 : 3544 : rolenames[i].roleoid = atooid(PQgetvalue(res, i, 0));
10693 : 3544 : rolenames[i].rolename = pg_strdup(PQgetvalue(res, i, 1));
10694 : : }
10695 : :
10696 : 194 : PQclear(res);
10697 : 194 : }
10698 : :
10699 : : /*
10700 : : * getAdditionalACLs
10701 : : *
10702 : : * We have now created all the DumpableObjects, and collected the ACL data
10703 : : * that appears in the directly-associated catalog entries. However, there's
10704 : : * more ACL-related info to collect. If any of a table's columns have ACLs,
10705 : : * we must set the TableInfo's DUMP_COMPONENT_ACL components flag, as well as
10706 : : * its hascolumnACLs flag (we won't store the ACLs themselves here, though).
10707 : : * Also, in versions having the pg_init_privs catalog, read that and load the
10708 : : * information into the relevant DumpableObjects.
10709 : : */
10710 : : static void
10711 : 191 : getAdditionalACLs(Archive *fout)
10712 : : {
10713 : 191 : PQExpBuffer query = createPQExpBuffer();
10714 : : PGresult *res;
10715 : : int ntups,
10716 : : i;
10717 : :
10718 : : /* Check for per-column ACLs */
10719 : 191 : appendPQExpBufferStr(query,
10720 : : "SELECT DISTINCT attrelid FROM pg_attribute "
10721 : : "WHERE attacl IS NOT NULL");
10722 : :
10723 : 191 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10724 : :
10725 : 191 : ntups = PQntuples(res);
10726 [ + + ]: 561 : for (i = 0; i < ntups; i++)
10727 : : {
10728 : 370 : Oid relid = atooid(PQgetvalue(res, i, 0));
10729 : : TableInfo *tblinfo;
10730 : :
10731 : 370 : tblinfo = findTableByOid(relid);
10732 : : /* OK to ignore tables we haven't got a DumpableObject for */
10733 [ + - ]: 370 : if (tblinfo)
10734 : : {
10735 : 370 : tblinfo->dobj.components |= DUMP_COMPONENT_ACL;
10736 : 370 : tblinfo->hascolumnACLs = true;
10737 : : }
10738 : : }
10739 : 191 : PQclear(res);
10740 : :
10741 : : /* Fetch initial-privileges data */
10742 : 191 : printfPQExpBuffer(query,
10743 : : "SELECT objoid, classoid, objsubid, privtype, initprivs "
10744 : : "FROM pg_init_privs");
10745 : :
10746 : 191 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10747 : :
10748 : 191 : ntups = PQntuples(res);
10749 [ + + ]: 49013 : for (i = 0; i < ntups; i++)
10750 : : {
10751 : 48822 : Oid objoid = atooid(PQgetvalue(res, i, 0));
10752 : 48822 : Oid classoid = atooid(PQgetvalue(res, i, 1));
10753 : 48822 : int objsubid = atoi(PQgetvalue(res, i, 2));
10754 : 48822 : char privtype = *(PQgetvalue(res, i, 3));
10755 : 48822 : char *initprivs = PQgetvalue(res, i, 4);
10756 : : CatalogId objId;
10757 : : DumpableObject *dobj;
10758 : :
10759 : 48822 : objId.tableoid = classoid;
10760 : 48822 : objId.oid = objoid;
10761 : 48822 : dobj = findObjectByCatalogId(objId);
10762 : : /* OK to ignore entries we haven't got a DumpableObject for */
10763 [ + + ]: 48822 : if (dobj)
10764 : : {
10765 : : /* Cope with sub-object initprivs */
10766 [ + + ]: 35499 : if (objsubid != 0)
10767 : : {
10768 [ + - ]: 4608 : if (dobj->objType == DO_TABLE)
10769 : : {
10770 : : /* For a column initprivs, set the table's ACL flags */
10771 : 4608 : dobj->components |= DUMP_COMPONENT_ACL;
10772 : 4608 : ((TableInfo *) dobj)->hascolumnACLs = true;
10773 : : }
10774 : : else
10775 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10776 : : classoid, objoid, objsubid);
10777 : 4795 : continue;
10778 : : }
10779 : :
10780 : : /*
10781 : : * We ignore any pg_init_privs.initprivs entry for the public
10782 : : * schema, as explained in getNamespaces().
10783 : : */
10784 [ + + ]: 30891 : if (dobj->objType == DO_NAMESPACE &&
10785 [ + + ]: 569 : strcmp(dobj->name, "public") == 0)
10786 : 187 : continue;
10787 : :
10788 : : /* Else it had better be of a type we think has ACLs */
10789 [ + + ]: 30704 : if (dobj->objType == DO_NAMESPACE ||
10790 [ + + ]: 30322 : dobj->objType == DO_TYPE ||
10791 [ + + ]: 30298 : dobj->objType == DO_FUNC ||
10792 [ + + ]: 30203 : dobj->objType == DO_AGG ||
10793 [ - + ]: 30179 : dobj->objType == DO_TABLE ||
10794 [ # # ]: 0 : dobj->objType == DO_PROCLANG ||
10795 [ # # ]: 0 : dobj->objType == DO_FDW ||
10796 [ # # ]: 0 : dobj->objType == DO_FOREIGN_SERVER)
10797 : 30704 : {
10798 : 30704 : DumpableObjectWithAcl *daobj = (DumpableObjectWithAcl *) dobj;
10799 : :
10800 : 30704 : daobj->dacl.privtype = privtype;
10801 : 30704 : daobj->dacl.initprivs = pstrdup(initprivs);
10802 : : }
10803 : : else
10804 : 0 : pg_log_warning("unsupported pg_init_privs entry: %u %u %d",
10805 : : classoid, objoid, objsubid);
10806 : : }
10807 : : }
10808 : 191 : PQclear(res);
10809 : :
10810 : 191 : destroyPQExpBuffer(query);
10811 : 191 : }
10812 : :
10813 : : /*
10814 : : * dumpCommentExtended --
10815 : : *
10816 : : * This routine is used to dump any comments associated with the
10817 : : * object handed to this routine. The routine takes the object type
10818 : : * and object name (ready to print, except for schema decoration), plus
10819 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
10820 : : * plus catalog ID and subid which are the lookup key for pg_description,
10821 : : * plus the dump ID for the object (for setting a dependency).
10822 : : * If a matching pg_description entry is found, it is dumped.
10823 : : *
10824 : : * Note: in some cases, such as comments for triggers and rules, the "type"
10825 : : * string really looks like, e.g., "TRIGGER name ON". This is a bit of a hack
10826 : : * but it doesn't seem worth complicating the API for all callers to make
10827 : : * it cleaner.
10828 : : *
10829 : : * Note: although this routine takes a dumpId for dependency purposes,
10830 : : * that purpose is just to mark the dependency in the emitted dump file
10831 : : * for possible future use by pg_restore. We do NOT use it for determining
10832 : : * ordering of the comment in the dump file, because this routine is called
10833 : : * after dependency sorting occurs. This routine should be called just after
10834 : : * calling ArchiveEntry() for the specified object.
10835 : : */
10836 : : static void
10837 : 6645 : dumpCommentExtended(Archive *fout, const char *type,
10838 : : const char *name, const char *namespace,
10839 : : const char *owner, CatalogId catalogId,
10840 : : int subid, DumpId dumpId,
10841 : : const char *initdb_comment)
10842 : : {
10843 : 6645 : DumpOptions *dopt = fout->dopt;
10844 : : CommentItem *comments;
10845 : : int ncomments;
10846 : :
10847 : : /* do nothing, if --no-comments is supplied */
10848 [ - + ]: 6645 : if (dopt->no_comments)
10849 : 0 : return;
10850 : :
10851 : : /* Comments are schema not data ... except LO comments are data */
10852 [ + + ]: 6645 : if (strcmp(type, "LARGE OBJECT") != 0)
10853 : : {
10854 [ - + ]: 6585 : if (!dopt->dumpSchema)
10855 : 0 : return;
10856 : : }
10857 : : else
10858 : : {
10859 : : /* We do dump LO comments in binary-upgrade mode */
10860 [ + + - + ]: 60 : if (!dopt->dumpData && !dopt->binary_upgrade)
10861 : 0 : return;
10862 : : }
10863 : :
10864 : : /* Search for comments associated with catalogId, using table */
10865 : 6645 : ncomments = findComments(catalogId.tableoid, catalogId.oid,
10866 : : &comments);
10867 : :
10868 : : /* Is there one matching the subid? */
10869 [ + + ]: 6645 : while (ncomments > 0)
10870 : : {
10871 [ + - ]: 6598 : if (comments->objsubid == subid)
10872 : 6598 : break;
10873 : 0 : comments++;
10874 : 0 : ncomments--;
10875 : : }
10876 : :
10877 [ + + ]: 6645 : if (initdb_comment != NULL)
10878 : : {
10879 : : static CommentItem empty_comment = {.descr = ""};
10880 : :
10881 : : /*
10882 : : * initdb creates this object with a comment. Skip dumping the
10883 : : * initdb-provided comment, which would complicate matters for
10884 : : * non-superuser use of pg_dump. When the DBA has removed initdb's
10885 : : * comment, replicate that.
10886 : : */
10887 [ + + ]: 117 : if (ncomments == 0)
10888 : : {
10889 : 4 : comments = &empty_comment;
10890 : 4 : ncomments = 1;
10891 : : }
10892 [ + - ]: 113 : else if (strcmp(comments->descr, initdb_comment) == 0)
10893 : 113 : ncomments = 0;
10894 : : }
10895 : :
10896 : : /* If a comment exists, build COMMENT ON statement */
10897 [ + + ]: 6645 : if (ncomments > 0)
10898 : : {
10899 : 6489 : PQExpBuffer query = createPQExpBuffer();
10900 : 6489 : PQExpBuffer tag = createPQExpBuffer();
10901 : :
10902 : 6489 : appendPQExpBuffer(query, "COMMENT ON %s ", type);
10903 [ + + + - ]: 6489 : if (namespace && *namespace)
10904 : 6300 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
10905 : 6489 : appendPQExpBuffer(query, "%s IS ", name);
10906 : 6489 : appendStringLiteralAH(query, comments->descr, fout);
10907 : 6489 : appendPQExpBufferStr(query, ";\n");
10908 : :
10909 : 6489 : appendPQExpBuffer(tag, "%s %s", type, name);
10910 : :
10911 : : /*
10912 : : * We mark comments as SECTION_NONE because they really belong in the
10913 : : * same section as their parent, whether that is pre-data or
10914 : : * post-data.
10915 : : */
10916 : 6489 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
10917 : 6489 : ARCHIVE_OPTS(.tag = tag->data,
10918 : : .namespace = namespace,
10919 : : .owner = owner,
10920 : : .description = "COMMENT",
10921 : : .section = SECTION_NONE,
10922 : : .createStmt = query->data,
10923 : : .deps = &dumpId,
10924 : : .nDeps = 1));
10925 : :
10926 : 6489 : destroyPQExpBuffer(query);
10927 : 6489 : destroyPQExpBuffer(tag);
10928 : : }
10929 : : }
10930 : :
10931 : : /*
10932 : : * dumpComment --
10933 : : *
10934 : : * Typical simplification of the above function.
10935 : : */
10936 : : static inline void
10937 : 6483 : dumpComment(Archive *fout, const char *type,
10938 : : const char *name, const char *namespace,
10939 : : const char *owner, CatalogId catalogId,
10940 : : int subid, DumpId dumpId)
10941 : : {
10942 : 6483 : dumpCommentExtended(fout, type, name, namespace, owner,
10943 : : catalogId, subid, dumpId, NULL);
10944 : 6483 : }
10945 : :
10946 : : /*
10947 : : * appendNamedArgument --
10948 : : *
10949 : : * Convenience routine for constructing parameters of the form:
10950 : : * 'paraname', 'value'::type
10951 : : */
10952 : : static void
10953 : 6394 : appendNamedArgument(PQExpBuffer out, Archive *fout, const char *argname,
10954 : : const char *argtype, const char *argval)
10955 : : {
10956 : 6394 : appendPQExpBufferStr(out, ",\n\t");
10957 : :
10958 : 6394 : appendStringLiteralAH(out, argname, fout);
10959 : 6394 : appendPQExpBufferStr(out, ", ");
10960 : :
10961 : 6394 : appendStringLiteralAH(out, argval, fout);
10962 : 6394 : appendPQExpBuffer(out, "::%s", argtype);
10963 : 6394 : }
10964 : :
10965 : : /*
10966 : : * fetchAttributeStats --
10967 : : *
10968 : : * Fetch next batch of attribute statistics for dumpRelationStats_dumper().
10969 : : */
10970 : : static PGresult *
10971 : 1104 : fetchAttributeStats(Archive *fout)
10972 : : {
10973 : 1104 : ArchiveHandle *AH = (ArchiveHandle *) fout;
10974 : 1104 : PQExpBuffer relids = createPQExpBuffer();
10975 : 1104 : PQExpBuffer nspnames = createPQExpBuffer();
10976 : 1104 : PQExpBuffer relnames = createPQExpBuffer();
10977 : 1104 : int count = 0;
10978 : 1104 : PGresult *res = NULL;
10979 : : static TocEntry *te;
10980 : : static bool restarted;
10981 : 1104 : int max_rels = MAX_ATTR_STATS_RELS;
10982 : :
10983 : : /* If we're just starting, set our TOC pointer. */
10984 [ + + ]: 1104 : if (!te)
10985 : 65 : te = AH->toc->next;
10986 : :
10987 : : /*
10988 : : * We can't easily avoid a second TOC scan for the tar format because it
10989 : : * writes restore.sql separately, which means we must execute the queries
10990 : : * twice. This feels risky, but there is no known reason it should
10991 : : * generate different output than the first pass. Even if it does, the
10992 : : * worst-case scenario is that restore.sql might have different statistics
10993 : : * data than the archive.
10994 : : */
10995 [ + + + + : 1104 : if (!restarted && te == AH->toc && AH->format == archTar)
+ + ]
10996 : : {
10997 : 1 : te = AH->toc->next;
10998 : 1 : restarted = true;
10999 : : }
11000 : :
11001 : 1104 : appendPQExpBufferChar(relids, '{');
11002 : 1104 : appendPQExpBufferChar(nspnames, '{');
11003 : 1104 : appendPQExpBufferChar(relnames, '{');
11004 : :
11005 : : /*
11006 : : * Scan the TOC for the next set of relevant stats entries. We assume
11007 : : * that statistics are dumped in the order they are listed in the TOC.
11008 : : * This is perhaps not the sturdiest assumption, so we verify it matches
11009 : : * reality in dumpRelationStats_dumper().
11010 : : */
11011 [ + + + + ]: 17298 : for (; te != AH->toc && count < max_rels; te = te->next)
11012 : : {
11013 [ + + ]: 16194 : if ((te->reqs & REQ_STATS) == 0 ||
11014 [ + + ]: 3655 : strcmp(te->desc, "STATISTICS DATA") != 0)
11015 : 12577 : continue;
11016 : :
11017 [ + - ]: 3617 : if (fout->remoteVersion >= 190000)
11018 : : {
11019 : 3617 : const RelStatsInfo *rsinfo = (const RelStatsInfo *) te->defnDumperArg;
11020 : : char relid[32];
11021 : :
11022 : 3617 : sprintf(relid, "%u", rsinfo->relid);
11023 : 3617 : appendPGArray(relids, relid);
11024 : : }
11025 : : else
11026 : : {
11027 : 0 : appendPGArray(nspnames, te->namespace);
11028 : 0 : appendPGArray(relnames, te->tag);
11029 : : }
11030 : :
11031 : 3617 : count++;
11032 : : }
11033 : :
11034 : 1104 : appendPQExpBufferChar(relids, '}');
11035 : 1104 : appendPQExpBufferChar(nspnames, '}');
11036 : 1104 : appendPQExpBufferChar(relnames, '}');
11037 : :
11038 : : /* Execute the query for the next batch of relations. */
11039 [ + + ]: 1104 : if (count > 0)
11040 : : {
11041 : 112 : PQExpBuffer query = createPQExpBuffer();
11042 : :
11043 : 112 : appendPQExpBufferStr(query, "EXECUTE getAttributeStats(");
11044 : :
11045 [ + - ]: 112 : if (fout->remoteVersion >= 190000)
11046 : : {
11047 : 112 : appendStringLiteralAH(query, relids->data, fout);
11048 : 112 : appendPQExpBufferStr(query, "::pg_catalog.oid[])");
11049 : : }
11050 : : else
11051 : : {
11052 : 0 : appendStringLiteralAH(query, nspnames->data, fout);
11053 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[],");
11054 : 0 : appendStringLiteralAH(query, relnames->data, fout);
11055 : 0 : appendPQExpBufferStr(query, "::pg_catalog.name[])");
11056 : : }
11057 : :
11058 : 112 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11059 : 112 : destroyPQExpBuffer(query);
11060 : : }
11061 : :
11062 : 1104 : destroyPQExpBuffer(relids);
11063 : 1104 : destroyPQExpBuffer(nspnames);
11064 : 1104 : destroyPQExpBuffer(relnames);
11065 : 1104 : return res;
11066 : : }
11067 : :
11068 : : /*
11069 : : * dumpRelationStats_dumper --
11070 : : *
11071 : : * Generate command to import stats into the relation on the new database.
11072 : : * This routine is called by the Archiver when it wants the statistics to be
11073 : : * dumped.
11074 : : */
11075 : : static char *
11076 : 3617 : dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te)
11077 : : {
11078 : 3617 : const RelStatsInfo *rsinfo = userArg;
11079 : : static PGresult *res;
11080 : : static int rownum;
11081 : : PQExpBuffer query;
11082 : : PQExpBufferData out_data;
11083 : 3617 : PQExpBuffer out = &out_data;
11084 : : int i_schemaname;
11085 : : int i_tablename;
11086 : : int i_attname;
11087 : : int i_inherited;
11088 : : int i_null_frac;
11089 : : int i_avg_width;
11090 : : int i_n_distinct;
11091 : : int i_most_common_vals;
11092 : : int i_most_common_freqs;
11093 : : int i_histogram_bounds;
11094 : : int i_correlation;
11095 : : int i_most_common_elems;
11096 : : int i_most_common_elem_freqs;
11097 : : int i_elem_count_histogram;
11098 : : int i_range_length_histogram;
11099 : : int i_range_empty_frac;
11100 : : int i_range_bounds_histogram;
11101 : : static TocEntry *expected_te;
11102 : :
11103 : : /*
11104 : : * fetchAttributeStats() assumes that the statistics are dumped in the
11105 : : * order they are listed in the TOC. We verify that here for safety.
11106 : : */
11107 [ + + ]: 3617 : if (!expected_te)
11108 : 65 : expected_te = ((ArchiveHandle *) fout)->toc;
11109 : :
11110 : 3617 : expected_te = expected_te->next;
11111 [ + + ]: 14141 : while ((expected_te->reqs & REQ_STATS) == 0 ||
11112 [ + + ]: 3618 : strcmp(expected_te->desc, "STATISTICS DATA") != 0)
11113 : 10524 : expected_te = expected_te->next;
11114 : :
11115 [ - + ]: 3617 : if (te != expected_te)
11116 : 0 : pg_fatal("statistics dumped out of order (current: %d %s %s, expected: %d %s %s)",
11117 : : te->dumpId, te->desc, te->tag,
11118 : : expected_te->dumpId, expected_te->desc, expected_te->tag);
11119 : :
11120 : 3617 : query = createPQExpBuffer();
11121 [ + + ]: 3617 : if (!fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS])
11122 : : {
11123 [ + - ]: 65 : if (fout->remoteVersion >= 190000)
11124 : 65 : appendPQExpBufferStr(query,
11125 : : "PREPARE getAttributeStats(pg_catalog.oid[]) AS\n");
11126 : : else
11127 : 0 : appendPQExpBufferStr(query,
11128 : : "PREPARE getAttributeStats(pg_catalog.name[], pg_catalog.name[]) AS\n");
11129 : :
11130 : 65 : appendPQExpBufferStr(query,
11131 : : "SELECT s.schemaname, s.tablename, s.attname, s.inherited, "
11132 : : "s.null_frac, s.avg_width, s.n_distinct, "
11133 : : "s.most_common_vals, s.most_common_freqs, "
11134 : : "s.histogram_bounds, s.correlation, "
11135 : : "s.most_common_elems, s.most_common_elem_freqs, "
11136 : : "s.elem_count_histogram, ");
11137 : :
11138 [ + - ]: 65 : if (fout->remoteVersion >= 170000)
11139 : 65 : appendPQExpBufferStr(query,
11140 : : "s.range_length_histogram, "
11141 : : "s.range_empty_frac, "
11142 : : "s.range_bounds_histogram ");
11143 : : else
11144 : 0 : appendPQExpBufferStr(query,
11145 : : "NULL AS range_length_histogram,"
11146 : : "NULL AS range_empty_frac,"
11147 : : "NULL AS range_bounds_histogram ");
11148 : :
11149 : : /*
11150 : : * The results must be in the order of the relations supplied in the
11151 : : * parameters to ensure we remain in sync as we walk through the TOC.
11152 : : *
11153 : : * For versions before 19, the redundant filter clause on s.tablename
11154 : : * = ANY(...) seems sufficient to convince the planner to use
11155 : : * pg_class_relname_nsp_index, which avoids a full scan of pg_stats.
11156 : : * In newer versions, pg_stats returns the table OIDs, eliminating the
11157 : : * need for that hack.
11158 : : */
11159 [ + - ]: 65 : if (fout->remoteVersion >= 190000)
11160 : 65 : appendPQExpBufferStr(query,
11161 : : "FROM pg_catalog.pg_stats s "
11162 : : "JOIN unnest($1) WITH ORDINALITY AS u (tableid, ord) "
11163 : : "ON s.tableid = u.tableid "
11164 : : "ORDER BY u.ord, s.attname, s.inherited");
11165 : : else
11166 : 0 : appendPQExpBufferStr(query,
11167 : : "FROM pg_catalog.pg_stats s "
11168 : : "JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) "
11169 : : "ON s.schemaname = u.schemaname "
11170 : : "AND s.tablename = u.tablename "
11171 : : "WHERE s.tablename = ANY($2) "
11172 : : "ORDER BY u.ord, s.attname, s.inherited");
11173 : :
11174 : 65 : ExecuteSqlStatement(fout, query->data);
11175 : :
11176 : 65 : fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS] = true;
11177 : 65 : resetPQExpBuffer(query);
11178 : : }
11179 : :
11180 : 3617 : initPQExpBuffer(out);
11181 : :
11182 : : /* restore relation stats */
11183 : 3617 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(\n");
11184 : 3617 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11185 : : fout->remoteVersion);
11186 : 3617 : appendPQExpBufferStr(out, "\t'schemaname', ");
11187 : 3617 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11188 : 3617 : appendPQExpBufferStr(out, ",\n");
11189 : 3617 : appendPQExpBufferStr(out, "\t'relname', ");
11190 : 3617 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11191 : 3617 : appendPQExpBufferStr(out, ",\n");
11192 : 3617 : appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages);
11193 : :
11194 : : /*
11195 : : * Before v14, a reltuples value of 0 was ambiguous: it could either mean
11196 : : * the relation is empty, or it could mean that it hadn't yet been
11197 : : * vacuumed or analyzed. (Newer versions use -1 for the latter case.)
11198 : : * This ambiguity allegedly can cause the planner to choose inefficient
11199 : : * plans after restoring to v18 or newer. To deal with this, let's just
11200 : : * set reltuples to -1 in that case.
11201 : : */
11202 [ - + - - ]: 3617 : if (fout->remoteVersion < 140000 && strcmp("0", rsinfo->reltuples) == 0)
11203 : 0 : appendPQExpBufferStr(out, "\t'reltuples', '-1'::real,\n");
11204 : : else
11205 : 3617 : appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples);
11206 : :
11207 : 3617 : appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer",
11208 : 3617 : rsinfo->relallvisible);
11209 : :
11210 [ + - ]: 3617 : if (fout->remoteVersion >= 180000)
11211 : 3617 : appendPQExpBuffer(out, ",\n\t'relallfrozen', '%d'::integer", rsinfo->relallfrozen);
11212 : :
11213 : 3617 : appendPQExpBufferStr(out, "\n);\n");
11214 : :
11215 : : /* Fetch the next batch of attribute statistics if needed. */
11216 [ + + ]: 3617 : if (rownum >= PQntuples(res))
11217 : : {
11218 : 1104 : PQclear(res);
11219 : 1104 : res = fetchAttributeStats(fout);
11220 : 1104 : rownum = 0;
11221 : : }
11222 : :
11223 : 3617 : i_schemaname = PQfnumber(res, "schemaname");
11224 : 3617 : i_tablename = PQfnumber(res, "tablename");
11225 : 3617 : i_attname = PQfnumber(res, "attname");
11226 : 3617 : i_inherited = PQfnumber(res, "inherited");
11227 : 3617 : i_null_frac = PQfnumber(res, "null_frac");
11228 : 3617 : i_avg_width = PQfnumber(res, "avg_width");
11229 : 3617 : i_n_distinct = PQfnumber(res, "n_distinct");
11230 : 3617 : i_most_common_vals = PQfnumber(res, "most_common_vals");
11231 : 3617 : i_most_common_freqs = PQfnumber(res, "most_common_freqs");
11232 : 3617 : i_histogram_bounds = PQfnumber(res, "histogram_bounds");
11233 : 3617 : i_correlation = PQfnumber(res, "correlation");
11234 : 3617 : i_most_common_elems = PQfnumber(res, "most_common_elems");
11235 : 3617 : i_most_common_elem_freqs = PQfnumber(res, "most_common_elem_freqs");
11236 : 3617 : i_elem_count_histogram = PQfnumber(res, "elem_count_histogram");
11237 : 3617 : i_range_length_histogram = PQfnumber(res, "range_length_histogram");
11238 : 3617 : i_range_empty_frac = PQfnumber(res, "range_empty_frac");
11239 : 3617 : i_range_bounds_histogram = PQfnumber(res, "range_bounds_histogram");
11240 : :
11241 : : /* restore attribute stats */
11242 [ + + ]: 4537 : for (; rownum < PQntuples(res); rownum++)
11243 : : {
11244 : : const char *attname;
11245 : :
11246 : : /* Stop if the next stat row in our cache isn't for this relation. */
11247 [ + + ]: 3433 : if (strcmp(te->tag, PQgetvalue(res, rownum, i_tablename)) != 0 ||
11248 [ + - ]: 920 : strcmp(te->namespace, PQgetvalue(res, rownum, i_schemaname)) != 0)
11249 : : break;
11250 : :
11251 : 920 : appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_attribute_stats(\n");
11252 : 920 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
11253 : : fout->remoteVersion);
11254 : 920 : appendPQExpBufferStr(out, "\t'schemaname', ");
11255 : 920 : appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout);
11256 : 920 : appendPQExpBufferStr(out, ",\n\t'relname', ");
11257 : 920 : appendStringLiteralAH(out, rsinfo->dobj.name, fout);
11258 : :
11259 [ - + ]: 920 : if (PQgetisnull(res, rownum, i_attname))
11260 : 0 : pg_fatal("unexpected null attname");
11261 : 920 : attname = PQgetvalue(res, rownum, i_attname);
11262 : :
11263 : : /*
11264 : : * Indexes look up attname in indAttNames to derive attnum, all others
11265 : : * use attname directly. We must specify attnum for indexes, since
11266 : : * their attnames are not necessarily stable across dump/reload.
11267 : : */
11268 [ + + ]: 920 : if (rsinfo->nindAttNames == 0)
11269 : : {
11270 : 880 : appendPQExpBufferStr(out, ",\n\t'attname', ");
11271 : 880 : appendStringLiteralAH(out, attname, fout);
11272 : : }
11273 : : else
11274 : : {
11275 : 40 : bool found = false;
11276 : :
11277 [ + - ]: 74 : for (int i = 0; i < rsinfo->nindAttNames; i++)
11278 : : {
11279 [ + + ]: 74 : if (strcmp(attname, rsinfo->indAttNames[i]) == 0)
11280 : : {
11281 : 40 : appendPQExpBuffer(out, ",\n\t'attnum', '%d'::smallint",
11282 : : i + 1);
11283 : 40 : found = true;
11284 : 40 : break;
11285 : : }
11286 : : }
11287 : :
11288 [ - + ]: 40 : if (!found)
11289 : 0 : pg_fatal("could not find index attname \"%s\"", attname);
11290 : : }
11291 : :
11292 [ + - ]: 920 : if (!PQgetisnull(res, rownum, i_inherited))
11293 : 920 : appendNamedArgument(out, fout, "inherited", "boolean",
11294 : 920 : PQgetvalue(res, rownum, i_inherited));
11295 [ + - ]: 920 : if (!PQgetisnull(res, rownum, i_null_frac))
11296 : 920 : appendNamedArgument(out, fout, "null_frac", "real",
11297 : 920 : PQgetvalue(res, rownum, i_null_frac));
11298 [ + - ]: 920 : if (!PQgetisnull(res, rownum, i_avg_width))
11299 : 920 : appendNamedArgument(out, fout, "avg_width", "integer",
11300 : 920 : PQgetvalue(res, rownum, i_avg_width));
11301 [ + - ]: 920 : if (!PQgetisnull(res, rownum, i_n_distinct))
11302 : 920 : appendNamedArgument(out, fout, "n_distinct", "real",
11303 : 920 : PQgetvalue(res, rownum, i_n_distinct));
11304 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_most_common_vals))
11305 : 471 : appendNamedArgument(out, fout, "most_common_vals", "text",
11306 : 471 : PQgetvalue(res, rownum, i_most_common_vals));
11307 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_most_common_freqs))
11308 : 471 : appendNamedArgument(out, fout, "most_common_freqs", "real[]",
11309 : 471 : PQgetvalue(res, rownum, i_most_common_freqs));
11310 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_histogram_bounds))
11311 : 599 : appendNamedArgument(out, fout, "histogram_bounds", "text",
11312 : 599 : PQgetvalue(res, rownum, i_histogram_bounds));
11313 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_correlation))
11314 : 878 : appendNamedArgument(out, fout, "correlation", "real",
11315 : 878 : PQgetvalue(res, rownum, i_correlation));
11316 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_most_common_elems))
11317 : 8 : appendNamedArgument(out, fout, "most_common_elems", "text",
11318 : 8 : PQgetvalue(res, rownum, i_most_common_elems));
11319 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_most_common_elem_freqs))
11320 : 8 : appendNamedArgument(out, fout, "most_common_elem_freqs", "real[]",
11321 : 8 : PQgetvalue(res, rownum, i_most_common_elem_freqs));
11322 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_elem_count_histogram))
11323 : 7 : appendNamedArgument(out, fout, "elem_count_histogram", "real[]",
11324 : 7 : PQgetvalue(res, rownum, i_elem_count_histogram));
11325 [ + - ]: 920 : if (fout->remoteVersion >= 170000)
11326 : : {
11327 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_range_length_histogram))
11328 : 4 : appendNamedArgument(out, fout, "range_length_histogram", "text",
11329 : 4 : PQgetvalue(res, rownum, i_range_length_histogram));
11330 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_range_empty_frac))
11331 : 4 : appendNamedArgument(out, fout, "range_empty_frac", "real",
11332 : 4 : PQgetvalue(res, rownum, i_range_empty_frac));
11333 [ + + ]: 920 : if (!PQgetisnull(res, rownum, i_range_bounds_histogram))
11334 : 4 : appendNamedArgument(out, fout, "range_bounds_histogram", "text",
11335 : 4 : PQgetvalue(res, rownum, i_range_bounds_histogram));
11336 : : }
11337 : 920 : appendPQExpBufferStr(out, "\n);\n");
11338 : : }
11339 : :
11340 : 3617 : destroyPQExpBuffer(query);
11341 : 3617 : return out->data;
11342 : : }
11343 : :
11344 : : /*
11345 : : * dumpRelationStats --
11346 : : *
11347 : : * Make an ArchiveEntry for the relation statistics. The Archiver will take
11348 : : * care of gathering the statistics and generating the restore commands when
11349 : : * they are needed.
11350 : : */
11351 : : static void
11352 : 3689 : dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
11353 : : {
11354 : 3689 : const DumpableObject *dobj = &rsinfo->dobj;
11355 : :
11356 : : /* nothing to do if we are not dumping statistics */
11357 [ - + ]: 3689 : if (!fout->dopt->dumpStatistics)
11358 : 0 : return;
11359 : :
11360 : 3689 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11361 : 3689 : ARCHIVE_OPTS(.tag = dobj->name,
11362 : : .namespace = dobj->namespace->dobj.name,
11363 : : .description = "STATISTICS DATA",
11364 : : .section = rsinfo->section,
11365 : : .defnFn = dumpRelationStats_dumper,
11366 : : .defnArg = rsinfo,
11367 : : .deps = dobj->dependencies,
11368 : : .nDeps = dobj->nDeps));
11369 : : }
11370 : :
11371 : : /*
11372 : : * dumpTableComment --
11373 : : *
11374 : : * As above, but dump comments for both the specified table (or view)
11375 : : * and its columns.
11376 : : */
11377 : : static void
11378 : 78 : dumpTableComment(Archive *fout, const TableInfo *tbinfo,
11379 : : const char *reltypename)
11380 : : {
11381 : 78 : DumpOptions *dopt = fout->dopt;
11382 : : CommentItem *comments;
11383 : : int ncomments;
11384 : : PQExpBuffer query;
11385 : : PQExpBuffer tag;
11386 : :
11387 : : /* do nothing, if --no-comments is supplied */
11388 [ - + ]: 78 : if (dopt->no_comments)
11389 : 0 : return;
11390 : :
11391 : : /* Comments are SCHEMA not data */
11392 [ - + ]: 78 : if (!dopt->dumpSchema)
11393 : 0 : return;
11394 : :
11395 : : /* Search for comments associated with relation, using table */
11396 : 78 : ncomments = findComments(tbinfo->dobj.catId.tableoid,
11397 : 78 : tbinfo->dobj.catId.oid,
11398 : : &comments);
11399 : :
11400 : : /* If comments exist, build COMMENT ON statements */
11401 [ - + ]: 78 : if (ncomments <= 0)
11402 : 0 : return;
11403 : :
11404 : 78 : query = createPQExpBuffer();
11405 : 78 : tag = createPQExpBuffer();
11406 : :
11407 [ + + ]: 224 : while (ncomments > 0)
11408 : : {
11409 : 146 : const char *descr = comments->descr;
11410 : 146 : int objsubid = comments->objsubid;
11411 : :
11412 [ + + ]: 146 : if (objsubid == 0)
11413 : : {
11414 : 34 : resetPQExpBuffer(tag);
11415 : 34 : appendPQExpBuffer(tag, "%s %s", reltypename,
11416 : 34 : fmtId(tbinfo->dobj.name));
11417 : :
11418 : 34 : resetPQExpBuffer(query);
11419 : 34 : appendPQExpBuffer(query, "COMMENT ON %s %s IS ", reltypename,
11420 : 34 : fmtQualifiedDumpable(tbinfo));
11421 : 34 : appendStringLiteralAH(query, descr, fout);
11422 : 34 : appendPQExpBufferStr(query, ";\n");
11423 : :
11424 : 34 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11425 : 34 : ARCHIVE_OPTS(.tag = tag->data,
11426 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11427 : : .owner = tbinfo->rolname,
11428 : : .description = "COMMENT",
11429 : : .section = SECTION_NONE,
11430 : : .createStmt = query->data,
11431 : : .deps = &(tbinfo->dobj.dumpId),
11432 : : .nDeps = 1));
11433 : : }
11434 [ + - + - ]: 112 : else if (objsubid > 0 && objsubid <= tbinfo->numatts)
11435 : : {
11436 : 112 : resetPQExpBuffer(tag);
11437 : 112 : appendPQExpBuffer(tag, "COLUMN %s.",
11438 : 112 : fmtId(tbinfo->dobj.name));
11439 : 112 : appendPQExpBufferStr(tag, fmtId(tbinfo->attnames[objsubid - 1]));
11440 : :
11441 : 112 : resetPQExpBuffer(query);
11442 : 112 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11443 : 112 : fmtQualifiedDumpable(tbinfo));
11444 : 112 : appendPQExpBuffer(query, "%s IS ",
11445 : 112 : fmtId(tbinfo->attnames[objsubid - 1]));
11446 : 112 : appendStringLiteralAH(query, descr, fout);
11447 : 112 : appendPQExpBufferStr(query, ";\n");
11448 : :
11449 : 112 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
11450 : 112 : ARCHIVE_OPTS(.tag = tag->data,
11451 : : .namespace = tbinfo->dobj.namespace->dobj.name,
11452 : : .owner = tbinfo->rolname,
11453 : : .description = "COMMENT",
11454 : : .section = SECTION_NONE,
11455 : : .createStmt = query->data,
11456 : : .deps = &(tbinfo->dobj.dumpId),
11457 : : .nDeps = 1));
11458 : : }
11459 : :
11460 : 146 : comments++;
11461 : 146 : ncomments--;
11462 : : }
11463 : :
11464 : 78 : destroyPQExpBuffer(query);
11465 : 78 : destroyPQExpBuffer(tag);
11466 : : }
11467 : :
11468 : : /*
11469 : : * findComments --
11470 : : *
11471 : : * Find the comment(s), if any, associated with the given object. All the
11472 : : * objsubid values associated with the given classoid/objoid are found with
11473 : : * one search.
11474 : : */
11475 : : static int
11476 : 6757 : findComments(Oid classoid, Oid objoid, CommentItem **items)
11477 : : {
11478 : 6757 : CommentItem *middle = NULL;
11479 : : CommentItem *low;
11480 : : CommentItem *high;
11481 : : int nmatch;
11482 : :
11483 : : /*
11484 : : * Do binary search to find some item matching the object.
11485 : : */
11486 : 6757 : low = &comments[0];
11487 : 6757 : high = &comments[ncomments - 1];
11488 [ + + ]: 67675 : while (low <= high)
11489 : : {
11490 : 67628 : middle = low + (high - low) / 2;
11491 : :
11492 [ + + ]: 67628 : if (classoid < middle->classoid)
11493 : 7221 : high = middle - 1;
11494 [ + + ]: 60407 : else if (classoid > middle->classoid)
11495 : 7315 : low = middle + 1;
11496 [ + + ]: 53092 : else if (objoid < middle->objoid)
11497 : 22504 : high = middle - 1;
11498 [ + + ]: 30588 : else if (objoid > middle->objoid)
11499 : 23878 : low = middle + 1;
11500 : : else
11501 : 6710 : break; /* found a match */
11502 : : }
11503 : :
11504 [ + + ]: 6757 : if (low > high) /* no matches */
11505 : : {
11506 : 47 : *items = NULL;
11507 : 47 : return 0;
11508 : : }
11509 : :
11510 : : /*
11511 : : * Now determine how many items match the object. The search loop
11512 : : * invariant still holds: only items between low and high inclusive could
11513 : : * match.
11514 : : */
11515 : 6710 : nmatch = 1;
11516 [ + + ]: 6766 : while (middle > low)
11517 : : {
11518 [ + + ]: 3262 : if (classoid != middle[-1].classoid ||
11519 [ + + ]: 3100 : objoid != middle[-1].objoid)
11520 : : break;
11521 : 56 : middle--;
11522 : 56 : nmatch++;
11523 : : }
11524 : :
11525 : 6710 : *items = middle;
11526 : :
11527 : 6710 : middle += nmatch;
11528 [ + + ]: 6722 : while (middle <= high)
11529 : : {
11530 [ + + ]: 3469 : if (classoid != middle->classoid ||
11531 [ + + ]: 3348 : objoid != middle->objoid)
11532 : : break;
11533 : 12 : middle++;
11534 : 12 : nmatch++;
11535 : : }
11536 : :
11537 : 6710 : return nmatch;
11538 : : }
11539 : :
11540 : : /*
11541 : : * collectComments --
11542 : : *
11543 : : * Construct a table of all comments available for database objects;
11544 : : * also set the has-comment component flag for each relevant object.
11545 : : *
11546 : : * We used to do per-object queries for the comments, but it's much faster
11547 : : * to pull them all over at once, and on most databases the memory cost
11548 : : * isn't high.
11549 : : *
11550 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
11551 : : */
11552 : : static void
11553 : 193 : collectComments(Archive *fout)
11554 : : {
11555 : : PGresult *res;
11556 : : PQExpBuffer query;
11557 : : int i_description;
11558 : : int i_classoid;
11559 : : int i_objoid;
11560 : : int i_objsubid;
11561 : : int ntups;
11562 : : int i;
11563 : : DumpableObject *dobj;
11564 : :
11565 : 193 : query = createPQExpBuffer();
11566 : :
11567 : 193 : appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
11568 : : "FROM pg_catalog.pg_description "
11569 : : "ORDER BY classoid, objoid, objsubid");
11570 : :
11571 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11572 : :
11573 : : /* Construct lookup table containing OIDs in numeric form */
11574 : :
11575 : 193 : i_description = PQfnumber(res, "description");
11576 : 193 : i_classoid = PQfnumber(res, "classoid");
11577 : 193 : i_objoid = PQfnumber(res, "objoid");
11578 : 193 : i_objsubid = PQfnumber(res, "objsubid");
11579 : :
11580 : 193 : ntups = PQntuples(res);
11581 : :
11582 : 193 : comments = pg_malloc_array(CommentItem, ntups);
11583 : 193 : ncomments = 0;
11584 : 193 : dobj = NULL;
11585 : :
11586 [ + + ]: 1043112 : for (i = 0; i < ntups; i++)
11587 : : {
11588 : : CatalogId objId;
11589 : : int subid;
11590 : :
11591 : 1042919 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
11592 : 1042919 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
11593 : 1042919 : subid = atoi(PQgetvalue(res, i, i_objsubid));
11594 : :
11595 : : /* We needn't remember comments that don't match any dumpable object */
11596 [ + + ]: 1042919 : if (dobj == NULL ||
11597 [ + + ]: 378529 : dobj->catId.tableoid != objId.tableoid ||
11598 [ + + ]: 376155 : dobj->catId.oid != objId.oid)
11599 : 1042823 : dobj = findObjectByCatalogId(objId);
11600 [ + + ]: 1042919 : if (dobj == NULL)
11601 : 664203 : continue;
11602 : :
11603 : : /*
11604 : : * Comments on columns of composite types are linked to the type's
11605 : : * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
11606 : : * in the type's own DumpableObject.
11607 : : */
11608 [ + + + - ]: 378716 : if (subid != 0 && dobj->objType == DO_TABLE &&
11609 [ + + ]: 206 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
11610 : 48 : {
11611 : : TypeInfo *cTypeInfo;
11612 : :
11613 : 48 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
11614 [ + - ]: 48 : if (cTypeInfo)
11615 : 48 : cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
11616 : : }
11617 : : else
11618 : 378668 : dobj->components |= DUMP_COMPONENT_COMMENT;
11619 : :
11620 : 378716 : comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
11621 : 378716 : comments[ncomments].classoid = objId.tableoid;
11622 : 378716 : comments[ncomments].objoid = objId.oid;
11623 : 378716 : comments[ncomments].objsubid = subid;
11624 : 378716 : ncomments++;
11625 : : }
11626 : :
11627 : 193 : PQclear(res);
11628 : 193 : destroyPQExpBuffer(query);
11629 : 193 : }
11630 : :
11631 : : /*
11632 : : * dumpDumpableObject
11633 : : *
11634 : : * This routine and its subsidiaries are responsible for creating
11635 : : * ArchiveEntries (TOC objects) for each object to be dumped.
11636 : : */
11637 : : static void
11638 : 745517 : dumpDumpableObject(Archive *fout, DumpableObject *dobj)
11639 : : {
11640 : : /*
11641 : : * Clear any dump-request bits for components that don't exist for this
11642 : : * object. (This makes it safe to initially use DUMP_COMPONENT_ALL as the
11643 : : * request for every kind of object.)
11644 : : */
11645 : 745517 : dobj->dump &= dobj->components;
11646 : :
11647 : : /* Now, short-circuit if there's nothing to be done here. */
11648 [ + + ]: 745517 : if (dobj->dump == 0)
11649 : 662211 : return;
11650 : :
11651 [ + + + + : 83306 : switch (dobj->objType)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + - ]
11652 : : {
11653 : 522 : case DO_NAMESPACE:
11654 : 522 : dumpNamespace(fout, (const NamespaceInfo *) dobj);
11655 : 522 : break;
11656 : 25 : case DO_EXTENSION:
11657 : 25 : dumpExtension(fout, (const ExtensionInfo *) dobj);
11658 : 25 : break;
11659 : 971 : case DO_TYPE:
11660 : 971 : dumpType(fout, (const TypeInfo *) dobj);
11661 : 971 : break;
11662 : 76 : case DO_SHELL_TYPE:
11663 : 76 : dumpShellType(fout, (const ShellTypeInfo *) dobj);
11664 : 76 : break;
11665 : 1922 : case DO_FUNC:
11666 : 1922 : dumpFunc(fout, (const FuncInfo *) dobj);
11667 : 1922 : break;
11668 : 295 : case DO_AGG:
11669 : 295 : dumpAgg(fout, (const AggInfo *) dobj);
11670 : 295 : break;
11671 : 2525 : case DO_OPERATOR:
11672 : 2525 : dumpOpr(fout, (const OprInfo *) dobj);
11673 : 2525 : break;
11674 : 84 : case DO_ACCESS_METHOD:
11675 : 84 : dumpAccessMethod(fout, (const AccessMethodInfo *) dobj);
11676 : 84 : break;
11677 : 675 : case DO_OPCLASS:
11678 : 675 : dumpOpclass(fout, (const OpclassInfo *) dobj);
11679 : 675 : break;
11680 : 561 : case DO_OPFAMILY:
11681 : 561 : dumpOpfamily(fout, (const OpfamilyInfo *) dobj);
11682 : 561 : break;
11683 : 2733 : case DO_COLLATION:
11684 : 2733 : dumpCollation(fout, (const CollInfo *) dobj);
11685 : 2733 : break;
11686 : 335 : case DO_CONVERSION:
11687 : 335 : dumpConversion(fout, (const ConvInfo *) dobj);
11688 : 335 : break;
11689 : 34527 : case DO_TABLE:
11690 : 34527 : dumpTable(fout, (const TableInfo *) dobj);
11691 : 34527 : break;
11692 : 1457 : case DO_TABLE_ATTACH:
11693 : 1457 : dumpTableAttach(fout, (const TableAttachInfo *) dobj);
11694 : 1457 : break;
11695 : 1121 : case DO_ATTRDEF:
11696 : 1121 : dumpAttrDef(fout, (const AttrDefInfo *) dobj);
11697 : 1121 : break;
11698 : 2852 : case DO_INDEX:
11699 : 2852 : dumpIndex(fout, (const IndxInfo *) dobj);
11700 : 2852 : break;
11701 : 615 : case DO_INDEX_ATTACH:
11702 : 615 : dumpIndexAttach(fout, (const IndexAttachInfo *) dobj);
11703 : 615 : break;
11704 : 183 : case DO_STATSEXT:
11705 : 183 : dumpStatisticsExt(fout, (const StatsExtInfo *) dobj);
11706 : 183 : dumpStatisticsExtStats(fout, (const StatsExtInfo *) dobj);
11707 : 183 : break;
11708 : 363 : case DO_REFRESH_MATVIEW:
11709 : 363 : refreshMatViewData(fout, (const TableDataInfo *) dobj);
11710 : 363 : break;
11711 : 1197 : case DO_RULE:
11712 : 1197 : dumpRule(fout, (const RuleInfo *) dobj);
11713 : 1197 : break;
11714 : 535 : case DO_TRIGGER:
11715 : 535 : dumpTrigger(fout, (const TriggerInfo *) dobj);
11716 : 535 : break;
11717 : 44 : case DO_EVENT_TRIGGER:
11718 : 44 : dumpEventTrigger(fout, (const EventTriggerInfo *) dobj);
11719 : 44 : break;
11720 : 2573 : case DO_CONSTRAINT:
11721 : 2573 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11722 : 2573 : break;
11723 : 237 : case DO_FK_CONSTRAINT:
11724 : 237 : dumpConstraint(fout, (const ConstraintInfo *) dobj);
11725 : 237 : break;
11726 : 87 : case DO_PROCLANG:
11727 : 87 : dumpProcLang(fout, (const ProcLangInfo *) dobj);
11728 : 87 : break;
11729 : 69 : case DO_CAST:
11730 : 69 : dumpCast(fout, (const CastInfo *) dobj);
11731 : 69 : break;
11732 : 44 : case DO_TRANSFORM:
11733 : 44 : dumpTransform(fout, (const TransformInfo *) dobj);
11734 : 44 : break;
11735 : 399 : case DO_SEQUENCE_SET:
11736 : 399 : dumpSequenceData(fout, (const TableDataInfo *) dobj);
11737 : 399 : break;
11738 : 4611 : case DO_TABLE_DATA:
11739 : 4611 : dumpTableData(fout, (const TableDataInfo *) dobj);
11740 : 4611 : break;
11741 : 15403 : case DO_DUMMY_TYPE:
11742 : : /* table rowtypes and array types are never dumped separately */
11743 : 15403 : break;
11744 : 44 : case DO_TSPARSER:
11745 : 44 : dumpTSParser(fout, (const TSParserInfo *) dobj);
11746 : 44 : break;
11747 : 182 : case DO_TSDICT:
11748 : 182 : dumpTSDictionary(fout, (const TSDictInfo *) dobj);
11749 : 182 : break;
11750 : 56 : case DO_TSTEMPLATE:
11751 : 56 : dumpTSTemplate(fout, (const TSTemplateInfo *) dobj);
11752 : 56 : break;
11753 : 157 : case DO_TSCONFIG:
11754 : 157 : dumpTSConfig(fout, (const TSConfigInfo *) dobj);
11755 : 157 : break;
11756 : 54 : case DO_FDW:
11757 : 54 : dumpForeignDataWrapper(fout, (const FdwInfo *) dobj);
11758 : 54 : break;
11759 : 58 : case DO_FOREIGN_SERVER:
11760 : 58 : dumpForeignServer(fout, (const ForeignServerInfo *) dobj);
11761 : 58 : break;
11762 : 170 : case DO_DEFAULT_ACL:
11763 : 170 : dumpDefaultACL(fout, (const DefaultACLInfo *) dobj);
11764 : 170 : break;
11765 : 88 : case DO_LARGE_OBJECT:
11766 : 88 : dumpLO(fout, (const LoInfo *) dobj);
11767 : 88 : break;
11768 : 88 : case DO_LARGE_OBJECT_DATA:
11769 [ + - ]: 88 : if (dobj->dump & DUMP_COMPONENT_DATA)
11770 : : {
11771 : : LoInfo *loinfo;
11772 : : TocEntry *te;
11773 : :
11774 : 88 : loinfo = (LoInfo *) findObjectByDumpId(dobj->dependencies[0]);
11775 [ - + ]: 88 : if (loinfo == NULL)
11776 : 0 : pg_fatal("missing metadata for large objects \"%s\"",
11777 : : dobj->name);
11778 : :
11779 : 88 : te = ArchiveEntry(fout, dobj->catId, dobj->dumpId,
11780 : 88 : ARCHIVE_OPTS(.tag = dobj->name,
11781 : : .owner = loinfo->rolname,
11782 : : .description = "BLOBS",
11783 : : .section = SECTION_DATA,
11784 : : .deps = dobj->dependencies,
11785 : : .nDeps = dobj->nDeps,
11786 : : .dumpFn = dumpLOs,
11787 : : .dumpArg = loinfo));
11788 : :
11789 : : /*
11790 : : * Set the TocEntry's dataLength in case we are doing a
11791 : : * parallel dump and want to order dump jobs by table size.
11792 : : * (We need some size estimate for every TocEntry with a
11793 : : * DataDumper function.) We don't currently have any cheap
11794 : : * way to estimate the size of LOs, but fortunately it doesn't
11795 : : * matter too much as long as we get large batches of LOs
11796 : : * processed reasonably early. Assume 8K per blob.
11797 : : */
11798 : 88 : te->dataLength = loinfo->numlos * (pgoff_t) 8192;
11799 : : }
11800 : 88 : break;
11801 : 357 : case DO_POLICY:
11802 : 357 : dumpPolicy(fout, (const PolicyInfo *) dobj);
11803 : 357 : break;
11804 : 416 : case DO_PUBLICATION:
11805 : 416 : dumpPublication(fout, (const PublicationInfo *) dobj);
11806 : 416 : break;
11807 : 298 : case DO_PUBLICATION_REL:
11808 : 298 : dumpPublicationTable(fout, (const PublicationRelInfo *) dobj);
11809 : 298 : break;
11810 : 103 : case DO_PUBLICATION_TABLE_IN_SCHEMA:
11811 : 103 : dumpPublicationNamespace(fout,
11812 : : (const PublicationSchemaInfo *) dobj);
11813 : 103 : break;
11814 : 116 : case DO_SUBSCRIPTION:
11815 : 116 : dumpSubscription(fout, (const SubscriptionInfo *) dobj);
11816 : 116 : break;
11817 : 3 : case DO_SUBSCRIPTION_REL:
11818 : 3 : dumpSubscriptionTable(fout, (const SubRelInfo *) dobj);
11819 : 3 : break;
11820 : 3689 : case DO_REL_STATS:
11821 : 3689 : dumpRelationStats(fout, (const RelStatsInfo *) dobj);
11822 : 3689 : break;
11823 : 386 : case DO_PRE_DATA_BOUNDARY:
11824 : : case DO_POST_DATA_BOUNDARY:
11825 : : /* never dumped, nothing to do */
11826 : 386 : break;
11827 : : }
11828 : : }
11829 : :
11830 : : /*
11831 : : * dumpNamespace
11832 : : * writes out to fout the queries to recreate a user-defined namespace
11833 : : */
11834 : : static void
11835 : 522 : dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo)
11836 : : {
11837 : 522 : DumpOptions *dopt = fout->dopt;
11838 : : PQExpBuffer q;
11839 : : PQExpBuffer delq;
11840 : : char *qnspname;
11841 : :
11842 : : /* Do nothing if not dumping schema */
11843 [ + + ]: 522 : if (!dopt->dumpSchema)
11844 : 29 : return;
11845 : :
11846 : 493 : q = createPQExpBuffer();
11847 : 493 : delq = createPQExpBuffer();
11848 : :
11849 : 493 : qnspname = pg_strdup(fmtId(nspinfo->dobj.name));
11850 : :
11851 [ + + ]: 493 : if (nspinfo->create)
11852 : : {
11853 : 336 : appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
11854 : 336 : appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname);
11855 : : }
11856 : : else
11857 : : {
11858 : : /* see selectDumpableNamespace() */
11859 : 157 : appendPQExpBufferStr(delq,
11860 : : "-- *not* dropping schema, since initdb creates it\n");
11861 : 157 : appendPQExpBufferStr(q,
11862 : : "-- *not* creating schema, since initdb creates it\n");
11863 : : }
11864 : :
11865 [ + + ]: 493 : if (dopt->binary_upgrade)
11866 : 106 : binary_upgrade_extension_member(q, &nspinfo->dobj,
11867 : : "SCHEMA", qnspname, NULL);
11868 : :
11869 [ + + ]: 493 : if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11870 : 211 : ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
11871 : 211 : ARCHIVE_OPTS(.tag = nspinfo->dobj.name,
11872 : : .owner = nspinfo->rolname,
11873 : : .description = "SCHEMA",
11874 : : .section = SECTION_PRE_DATA,
11875 : : .createStmt = q->data,
11876 : : .dropStmt = delq->data));
11877 : :
11878 : : /* Dump Schema Comments and Security Labels */
11879 [ + + ]: 493 : if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11880 : : {
11881 : 162 : const char *initdb_comment = NULL;
11882 : :
11883 [ + + + + ]: 162 : if (!nspinfo->create && strcmp(qnspname, "public") == 0)
11884 : 117 : initdb_comment = "standard public schema";
11885 : 162 : dumpCommentExtended(fout, "SCHEMA", qnspname,
11886 : 162 : NULL, nspinfo->rolname,
11887 : 162 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId,
11888 : : initdb_comment);
11889 : : }
11890 : :
11891 [ - + ]: 493 : if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11892 : 0 : dumpSecLabel(fout, "SCHEMA", qnspname,
11893 : 0 : NULL, nspinfo->rolname,
11894 : 0 : nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
11895 : :
11896 [ + + ]: 493 : if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL)
11897 : 389 : dumpACL(fout, nspinfo->dobj.dumpId, InvalidDumpId, "SCHEMA",
11898 : : qnspname, NULL, NULL,
11899 : 389 : NULL, nspinfo->rolname, &nspinfo->dacl);
11900 : :
11901 : 493 : pg_free(qnspname);
11902 : :
11903 : 493 : destroyPQExpBuffer(q);
11904 : 493 : destroyPQExpBuffer(delq);
11905 : : }
11906 : :
11907 : : /*
11908 : : * dumpExtension
11909 : : * writes out to fout the queries to recreate an extension
11910 : : */
11911 : : static void
11912 : 25 : dumpExtension(Archive *fout, const ExtensionInfo *extinfo)
11913 : : {
11914 : 25 : DumpOptions *dopt = fout->dopt;
11915 : : PQExpBuffer q;
11916 : : PQExpBuffer delq;
11917 : : char *qextname;
11918 : :
11919 : : /* Do nothing if not dumping schema */
11920 [ + + ]: 25 : if (!dopt->dumpSchema)
11921 : 1 : return;
11922 : :
11923 : 24 : q = createPQExpBuffer();
11924 : 24 : delq = createPQExpBuffer();
11925 : :
11926 : 24 : qextname = pg_strdup(fmtId(extinfo->dobj.name));
11927 : :
11928 : 24 : appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname);
11929 : :
11930 [ + + ]: 24 : if (!dopt->binary_upgrade)
11931 : : {
11932 : : /*
11933 : : * In a regular dump, we simply create the extension, intentionally
11934 : : * not specifying a version, so that the destination installation's
11935 : : * default version is used.
11936 : : *
11937 : : * Use of IF NOT EXISTS here is unlike our behavior for other object
11938 : : * types; but there are various scenarios in which it's convenient to
11939 : : * manually create the desired extension before restoring, so we
11940 : : * prefer to allow it to exist already.
11941 : : */
11942 : 17 : appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n",
11943 : 17 : qextname, fmtId(extinfo->namespace));
11944 : : }
11945 : : else
11946 : : {
11947 : : /*
11948 : : * In binary-upgrade mode, it's critical to reproduce the state of the
11949 : : * database exactly, so our procedure is to create an empty extension,
11950 : : * restore all the contained objects normally, and add them to the
11951 : : * extension one by one. This function performs just the first of
11952 : : * those steps. binary_upgrade_extension_member() takes care of
11953 : : * adding member objects as they're created.
11954 : : */
11955 : : int i;
11956 : : int n;
11957 : :
11958 : 7 : appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
11959 : :
11960 : : /*
11961 : : * We unconditionally create the extension, so we must drop it if it
11962 : : * exists. This could happen if the user deleted 'plpgsql' and then
11963 : : * readded it, causing its oid to be greater than g_last_builtin_oid.
11964 : : */
11965 : 7 : appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
11966 : :
11967 : 7 : appendPQExpBufferStr(q,
11968 : : "SELECT pg_catalog.binary_upgrade_create_empty_extension(");
11969 : 7 : appendStringLiteralAH(q, extinfo->dobj.name, fout);
11970 : 7 : appendPQExpBufferStr(q, ", ");
11971 : 7 : appendStringLiteralAH(q, extinfo->namespace, fout);
11972 : 7 : appendPQExpBufferStr(q, ", ");
11973 [ + - ]: 7 : appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false");
11974 : 7 : appendStringLiteralAH(q, extinfo->extversion, fout);
11975 : 7 : appendPQExpBufferStr(q, ", ");
11976 : :
11977 : : /*
11978 : : * Note that we're pushing extconfig (an OID array) back into
11979 : : * pg_extension exactly as-is. This is OK because pg_class OIDs are
11980 : : * preserved in binary upgrade.
11981 : : */
11982 [ + + ]: 7 : if (strlen(extinfo->extconfig) > 2)
11983 : 1 : appendStringLiteralAH(q, extinfo->extconfig, fout);
11984 : : else
11985 : 6 : appendPQExpBufferStr(q, "NULL");
11986 : 7 : appendPQExpBufferStr(q, ", ");
11987 [ + + ]: 7 : if (strlen(extinfo->extcondition) > 2)
11988 : 1 : appendStringLiteralAH(q, extinfo->extcondition, fout);
11989 : : else
11990 : 6 : appendPQExpBufferStr(q, "NULL");
11991 : 7 : appendPQExpBufferStr(q, ", ");
11992 : 7 : appendPQExpBufferStr(q, "ARRAY[");
11993 : 7 : n = 0;
11994 [ + + ]: 14 : for (i = 0; i < extinfo->dobj.nDeps; i++)
11995 : : {
11996 : : DumpableObject *extobj;
11997 : :
11998 : 7 : extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]);
11999 [ + - - + ]: 7 : if (extobj && extobj->objType == DO_EXTENSION)
12000 : : {
12001 [ # # ]: 0 : if (n++ > 0)
12002 : 0 : appendPQExpBufferChar(q, ',');
12003 : 0 : appendStringLiteralAH(q, extobj->name, fout);
12004 : : }
12005 : : }
12006 : 7 : appendPQExpBufferStr(q, "]::pg_catalog.text[]");
12007 : 7 : appendPQExpBufferStr(q, ");\n");
12008 : : }
12009 : :
12010 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12011 : 24 : ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId,
12012 : 24 : ARCHIVE_OPTS(.tag = extinfo->dobj.name,
12013 : : .description = "EXTENSION",
12014 : : .section = SECTION_PRE_DATA,
12015 : : .createStmt = q->data,
12016 : : .dropStmt = delq->data));
12017 : :
12018 : : /* Dump Extension Comments */
12019 [ + - ]: 24 : if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12020 : 24 : dumpComment(fout, "EXTENSION", qextname,
12021 : : NULL, "",
12022 : 24 : extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
12023 : :
12024 : 24 : pg_free(qextname);
12025 : :
12026 : 24 : destroyPQExpBuffer(q);
12027 : 24 : destroyPQExpBuffer(delq);
12028 : : }
12029 : :
12030 : : /*
12031 : : * dumpType
12032 : : * writes out to fout the queries to recreate a user-defined type
12033 : : */
12034 : : static void
12035 : 971 : dumpType(Archive *fout, const TypeInfo *tyinfo)
12036 : : {
12037 : 971 : DumpOptions *dopt = fout->dopt;
12038 : :
12039 : : /* Do nothing if not dumping schema */
12040 [ + + ]: 971 : if (!dopt->dumpSchema)
12041 : 56 : return;
12042 : :
12043 : : /* Dump out in proper style */
12044 [ + + ]: 915 : if (tyinfo->typtype == TYPTYPE_BASE)
12045 : 285 : dumpBaseType(fout, tyinfo);
12046 [ + + ]: 630 : else if (tyinfo->typtype == TYPTYPE_DOMAIN)
12047 : 174 : dumpDomain(fout, tyinfo);
12048 [ + + ]: 456 : else if (tyinfo->typtype == TYPTYPE_COMPOSITE)
12049 : 132 : dumpCompositeType(fout, tyinfo);
12050 [ + + ]: 324 : else if (tyinfo->typtype == TYPTYPE_ENUM)
12051 : 89 : dumpEnumType(fout, tyinfo);
12052 [ + + ]: 235 : else if (tyinfo->typtype == TYPTYPE_RANGE)
12053 : 121 : dumpRangeType(fout, tyinfo);
12054 [ + - + + ]: 114 : else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
12055 : 39 : dumpUndefinedType(fout, tyinfo);
12056 : : else
12057 : 75 : pg_log_warning("typtype of data type \"%s\" appears to be invalid",
12058 : : tyinfo->dobj.name);
12059 : : }
12060 : :
12061 : : /*
12062 : : * dumpEnumType
12063 : : * writes out to fout the queries to recreate a user-defined enum type
12064 : : */
12065 : : static void
12066 : 89 : dumpEnumType(Archive *fout, const TypeInfo *tyinfo)
12067 : : {
12068 : 89 : DumpOptions *dopt = fout->dopt;
12069 : 89 : PQExpBuffer q = createPQExpBuffer();
12070 : 89 : PQExpBuffer delq = createPQExpBuffer();
12071 : 89 : PQExpBuffer query = createPQExpBuffer();
12072 : : PGresult *res;
12073 : : int num,
12074 : : i;
12075 : : Oid enum_oid;
12076 : : char *qtypname;
12077 : : char *qualtypname;
12078 : : char *label;
12079 : : int i_enumlabel;
12080 : : int i_oid;
12081 : :
12082 [ + + ]: 89 : if (!fout->is_prepared[PREPQUERY_DUMPENUMTYPE])
12083 : : {
12084 : : /* Set up query for enum-specific details */
12085 : 42 : appendPQExpBufferStr(query,
12086 : : "PREPARE dumpEnumType(pg_catalog.oid) AS\n"
12087 : : "SELECT oid, enumlabel "
12088 : : "FROM pg_catalog.pg_enum "
12089 : : "WHERE enumtypid = $1 "
12090 : : "ORDER BY enumsortorder");
12091 : :
12092 : 42 : ExecuteSqlStatement(fout, query->data);
12093 : :
12094 : 42 : fout->is_prepared[PREPQUERY_DUMPENUMTYPE] = true;
12095 : : }
12096 : :
12097 : 89 : printfPQExpBuffer(query,
12098 : : "EXECUTE dumpEnumType('%u')",
12099 : 89 : tyinfo->dobj.catId.oid);
12100 : :
12101 : 89 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12102 : :
12103 : 89 : num = PQntuples(res);
12104 : :
12105 : 89 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12106 : 89 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12107 : :
12108 : : /*
12109 : : * CASCADE shouldn't be required here as for normal types since the I/O
12110 : : * functions are generic and do not get dropped.
12111 : : */
12112 : 89 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12113 : :
12114 [ + + ]: 89 : if (dopt->binary_upgrade)
12115 : 6 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12116 : 6 : tyinfo->dobj.catId.oid,
12117 : : false, false);
12118 : :
12119 : 89 : appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
12120 : : qualtypname);
12121 : :
12122 [ + + ]: 89 : if (!dopt->binary_upgrade)
12123 : : {
12124 : 83 : i_enumlabel = PQfnumber(res, "enumlabel");
12125 : :
12126 : : /* Labels with server-assigned oids */
12127 [ + + ]: 498 : for (i = 0; i < num; i++)
12128 : : {
12129 : 415 : label = PQgetvalue(res, i, i_enumlabel);
12130 [ + + ]: 415 : if (i > 0)
12131 : 332 : appendPQExpBufferChar(q, ',');
12132 : 415 : appendPQExpBufferStr(q, "\n ");
12133 : 415 : appendStringLiteralAH(q, label, fout);
12134 : : }
12135 : : }
12136 : :
12137 : 89 : appendPQExpBufferStr(q, "\n);\n");
12138 : :
12139 [ + + ]: 89 : if (dopt->binary_upgrade)
12140 : : {
12141 : 6 : i_oid = PQfnumber(res, "oid");
12142 : 6 : i_enumlabel = PQfnumber(res, "enumlabel");
12143 : :
12144 : : /* Labels with dump-assigned (preserved) oids */
12145 [ + + ]: 62 : for (i = 0; i < num; i++)
12146 : : {
12147 : 56 : enum_oid = atooid(PQgetvalue(res, i, i_oid));
12148 : 56 : label = PQgetvalue(res, i, i_enumlabel);
12149 : :
12150 [ + + ]: 56 : if (i == 0)
12151 : 6 : appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
12152 : 56 : appendPQExpBuffer(q,
12153 : : "SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n",
12154 : : enum_oid);
12155 : 56 : appendPQExpBuffer(q, "ALTER TYPE %s ADD VALUE ", qualtypname);
12156 : 56 : appendStringLiteralAH(q, label, fout);
12157 : 56 : appendPQExpBufferStr(q, ";\n\n");
12158 : : }
12159 : : }
12160 : :
12161 [ + + ]: 89 : if (dopt->binary_upgrade)
12162 : 6 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12163 : : "TYPE", qtypname,
12164 : 6 : tyinfo->dobj.namespace->dobj.name);
12165 : :
12166 [ + - ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12167 : 89 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12168 : 89 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12169 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12170 : : .owner = tyinfo->rolname,
12171 : : .description = "TYPE",
12172 : : .section = SECTION_PRE_DATA,
12173 : : .createStmt = q->data,
12174 : : .dropStmt = delq->data));
12175 : :
12176 : : /* Dump Type Comments and Security Labels */
12177 [ + + ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12178 : 34 : dumpComment(fout, "TYPE", qtypname,
12179 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12180 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12181 : :
12182 [ - + ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12183 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12184 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12185 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12186 : :
12187 [ + + ]: 89 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12188 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12189 : : qtypname, NULL,
12190 : 34 : tyinfo->dobj.namespace->dobj.name,
12191 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12192 : :
12193 : 89 : PQclear(res);
12194 : 89 : destroyPQExpBuffer(q);
12195 : 89 : destroyPQExpBuffer(delq);
12196 : 89 : destroyPQExpBuffer(query);
12197 : 89 : pg_free(qtypname);
12198 : 89 : pg_free(qualtypname);
12199 : 89 : }
12200 : :
12201 : : /*
12202 : : * dumpRangeType
12203 : : * writes out to fout the queries to recreate a user-defined range type
12204 : : */
12205 : : static void
12206 : 121 : dumpRangeType(Archive *fout, const TypeInfo *tyinfo)
12207 : : {
12208 : 121 : DumpOptions *dopt = fout->dopt;
12209 : 121 : PQExpBuffer q = createPQExpBuffer();
12210 : 121 : PQExpBuffer delq = createPQExpBuffer();
12211 : 121 : PQExpBuffer query = createPQExpBuffer();
12212 : : PGresult *res;
12213 : : Oid collationOid;
12214 : : char *qtypname;
12215 : : char *qualtypname;
12216 : : char *procname;
12217 : :
12218 [ + + ]: 121 : if (!fout->is_prepared[PREPQUERY_DUMPRANGETYPE])
12219 : : {
12220 : : /* Set up query for range-specific details */
12221 : 42 : appendPQExpBufferStr(query,
12222 : : "PREPARE dumpRangeType(pg_catalog.oid) AS\n");
12223 : :
12224 : 42 : appendPQExpBufferStr(query,
12225 : : "SELECT ");
12226 : :
12227 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12228 : 42 : appendPQExpBufferStr(query,
12229 : : "pg_catalog.format_type(rngmultitypid, NULL) AS rngmultitype, ");
12230 : : else
12231 : 0 : appendPQExpBufferStr(query,
12232 : : "NULL AS rngmultitype, ");
12233 : :
12234 : 42 : appendPQExpBufferStr(query,
12235 : : "pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
12236 : : "opc.opcname AS opcname, "
12237 : : "(SELECT nspname FROM pg_catalog.pg_namespace nsp "
12238 : : " WHERE nsp.oid = opc.opcnamespace) AS opcnsp, "
12239 : : "opc.opcdefault, "
12240 : : "CASE WHEN rngcollation = st.typcollation THEN 0 "
12241 : : " ELSE rngcollation END AS collation, "
12242 : : "rngcanonical, rngsubdiff "
12243 : : "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, "
12244 : : " pg_catalog.pg_opclass opc "
12245 : : "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND "
12246 : : "rngtypid = $1");
12247 : :
12248 : 42 : ExecuteSqlStatement(fout, query->data);
12249 : :
12250 : 42 : fout->is_prepared[PREPQUERY_DUMPRANGETYPE] = true;
12251 : : }
12252 : :
12253 : 121 : printfPQExpBuffer(query,
12254 : : "EXECUTE dumpRangeType('%u')",
12255 : 121 : tyinfo->dobj.catId.oid);
12256 : :
12257 : 121 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12258 : :
12259 : 121 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12260 : 121 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12261 : :
12262 : : /*
12263 : : * CASCADE shouldn't be required here as for normal types since the I/O
12264 : : * functions are generic and do not get dropped.
12265 : : */
12266 : 121 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12267 : :
12268 [ + + ]: 121 : if (dopt->binary_upgrade)
12269 : 9 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12270 : 9 : tyinfo->dobj.catId.oid,
12271 : : false, true);
12272 : :
12273 : 121 : appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
12274 : : qualtypname);
12275 : :
12276 : 121 : appendPQExpBuffer(q, "\n subtype = %s",
12277 : : PQgetvalue(res, 0, PQfnumber(res, "rngsubtype")));
12278 : :
12279 [ + - ]: 121 : if (!PQgetisnull(res, 0, PQfnumber(res, "rngmultitype")))
12280 : 121 : appendPQExpBuffer(q, ",\n multirange_type_name = %s",
12281 : : PQgetvalue(res, 0, PQfnumber(res, "rngmultitype")));
12282 : :
12283 : : /* print subtype_opclass only if not default for subtype */
12284 [ + + ]: 121 : if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't')
12285 : : {
12286 : 34 : char *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname"));
12287 : 34 : char *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp"));
12288 : :
12289 : 34 : appendPQExpBuffer(q, ",\n subtype_opclass = %s.",
12290 : : fmtId(nspname));
12291 : 34 : appendPQExpBufferStr(q, fmtId(opcname));
12292 : : }
12293 : :
12294 : 121 : collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation")));
12295 [ + + ]: 121 : if (OidIsValid(collationOid))
12296 : : {
12297 : 39 : CollInfo *coll = findCollationByOid(collationOid);
12298 : :
12299 [ + - ]: 39 : if (coll)
12300 : 39 : appendPQExpBuffer(q, ",\n collation = %s",
12301 : 39 : fmtQualifiedDumpable(coll));
12302 : : }
12303 : :
12304 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical"));
12305 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12306 : 9 : appendPQExpBuffer(q, ",\n canonical = %s", procname);
12307 : :
12308 : 121 : procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff"));
12309 [ + + ]: 121 : if (strcmp(procname, "-") != 0)
12310 : 23 : appendPQExpBuffer(q, ",\n subtype_diff = %s", procname);
12311 : :
12312 : 121 : appendPQExpBufferStr(q, "\n);\n");
12313 : :
12314 [ + + ]: 121 : if (dopt->binary_upgrade)
12315 : 9 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12316 : : "TYPE", qtypname,
12317 : 9 : tyinfo->dobj.namespace->dobj.name);
12318 : :
12319 [ + - ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12320 : 121 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12321 : 121 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12322 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12323 : : .owner = tyinfo->rolname,
12324 : : .description = "TYPE",
12325 : : .section = SECTION_PRE_DATA,
12326 : : .createStmt = q->data,
12327 : : .dropStmt = delq->data));
12328 : :
12329 : : /* Dump Type Comments and Security Labels */
12330 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12331 : 52 : dumpComment(fout, "TYPE", qtypname,
12332 : 52 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12333 : 52 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12334 : :
12335 [ - + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12336 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12337 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12338 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12339 : :
12340 [ + + ]: 121 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12341 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12342 : : qtypname, NULL,
12343 : 34 : tyinfo->dobj.namespace->dobj.name,
12344 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12345 : :
12346 : 121 : PQclear(res);
12347 : 121 : destroyPQExpBuffer(q);
12348 : 121 : destroyPQExpBuffer(delq);
12349 : 121 : destroyPQExpBuffer(query);
12350 : 121 : pg_free(qtypname);
12351 : 121 : pg_free(qualtypname);
12352 : 121 : }
12353 : :
12354 : : /*
12355 : : * dumpUndefinedType
12356 : : * writes out to fout the queries to recreate a !typisdefined type
12357 : : *
12358 : : * This is a shell type, but we use different terminology to distinguish
12359 : : * this case from where we have to emit a shell type definition to break
12360 : : * circular dependencies. An undefined type shouldn't ever have anything
12361 : : * depending on it.
12362 : : */
12363 : : static void
12364 : 39 : dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo)
12365 : : {
12366 : 39 : DumpOptions *dopt = fout->dopt;
12367 : 39 : PQExpBuffer q = createPQExpBuffer();
12368 : 39 : PQExpBuffer delq = createPQExpBuffer();
12369 : : char *qtypname;
12370 : : char *qualtypname;
12371 : :
12372 : 39 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12373 : 39 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12374 : :
12375 : 39 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
12376 : :
12377 [ + + ]: 39 : if (dopt->binary_upgrade)
12378 : 2 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12379 : 2 : tyinfo->dobj.catId.oid,
12380 : : false, false);
12381 : :
12382 : 39 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
12383 : : qualtypname);
12384 : :
12385 [ + + ]: 39 : if (dopt->binary_upgrade)
12386 : 2 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12387 : : "TYPE", qtypname,
12388 : 2 : tyinfo->dobj.namespace->dobj.name);
12389 : :
12390 [ + - ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12391 : 39 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12392 : 39 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12393 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12394 : : .owner = tyinfo->rolname,
12395 : : .description = "TYPE",
12396 : : .section = SECTION_PRE_DATA,
12397 : : .createStmt = q->data,
12398 : : .dropStmt = delq->data));
12399 : :
12400 : : /* Dump Type Comments and Security Labels */
12401 [ + + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12402 : 34 : dumpComment(fout, "TYPE", qtypname,
12403 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12404 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12405 : :
12406 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12407 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12408 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12409 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12410 : :
12411 [ - + ]: 39 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12412 : 0 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12413 : : qtypname, NULL,
12414 : 0 : tyinfo->dobj.namespace->dobj.name,
12415 : 0 : NULL, tyinfo->rolname, &tyinfo->dacl);
12416 : :
12417 : 39 : destroyPQExpBuffer(q);
12418 : 39 : destroyPQExpBuffer(delq);
12419 : 39 : pg_free(qtypname);
12420 : 39 : pg_free(qualtypname);
12421 : 39 : }
12422 : :
12423 : : /*
12424 : : * dumpBaseType
12425 : : * writes out to fout the queries to recreate a user-defined base type
12426 : : */
12427 : : static void
12428 : 285 : dumpBaseType(Archive *fout, const TypeInfo *tyinfo)
12429 : : {
12430 : 285 : DumpOptions *dopt = fout->dopt;
12431 : 285 : PQExpBuffer q = createPQExpBuffer();
12432 : 285 : PQExpBuffer delq = createPQExpBuffer();
12433 : 285 : PQExpBuffer query = createPQExpBuffer();
12434 : : PGresult *res;
12435 : : char *qtypname;
12436 : : char *qualtypname;
12437 : : char *typlen;
12438 : : char *typinput;
12439 : : char *typoutput;
12440 : : char *typreceive;
12441 : : char *typsend;
12442 : : char *typmodin;
12443 : : char *typmodout;
12444 : : char *typanalyze;
12445 : : char *typsubscript;
12446 : : Oid typreceiveoid;
12447 : : Oid typsendoid;
12448 : : Oid typmodinoid;
12449 : : Oid typmodoutoid;
12450 : : Oid typanalyzeoid;
12451 : : Oid typsubscriptoid;
12452 : : char *typcategory;
12453 : : char *typispreferred;
12454 : : char *typdelim;
12455 : : char *typbyval;
12456 : : char *typalign;
12457 : : char *typstorage;
12458 : : char *typcollatable;
12459 : : char *typdefault;
12460 : 285 : bool typdefault_is_literal = false;
12461 : :
12462 [ + + ]: 285 : if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
12463 : : {
12464 : : /* Set up query for type-specific details */
12465 : 42 : appendPQExpBufferStr(query,
12466 : : "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
12467 : : "SELECT typlen, "
12468 : : "typinput, typoutput, typreceive, typsend, "
12469 : : "typreceive::pg_catalog.oid AS typreceiveoid, "
12470 : : "typsend::pg_catalog.oid AS typsendoid, "
12471 : : "typanalyze, "
12472 : : "typanalyze::pg_catalog.oid AS typanalyzeoid, "
12473 : : "typdelim, typbyval, typalign, typstorage, "
12474 : : "typmodin, typmodout, "
12475 : : "typmodin::pg_catalog.oid AS typmodinoid, "
12476 : : "typmodout::pg_catalog.oid AS typmodoutoid, "
12477 : : "typcategory, typispreferred, "
12478 : : "(typcollation <> 0) AS typcollatable, "
12479 : : "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
12480 : :
12481 [ + - ]: 42 : if (fout->remoteVersion >= 140000)
12482 : 42 : appendPQExpBufferStr(query,
12483 : : "typsubscript, "
12484 : : "typsubscript::pg_catalog.oid AS typsubscriptoid ");
12485 : : else
12486 : 0 : appendPQExpBufferStr(query,
12487 : : "'-' AS typsubscript, 0 AS typsubscriptoid ");
12488 : :
12489 : 42 : appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
12490 : : "WHERE oid = $1");
12491 : :
12492 : 42 : ExecuteSqlStatement(fout, query->data);
12493 : :
12494 : 42 : fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
12495 : : }
12496 : :
12497 : 285 : printfPQExpBuffer(query,
12498 : : "EXECUTE dumpBaseType('%u')",
12499 : 285 : tyinfo->dobj.catId.oid);
12500 : :
12501 : 285 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12502 : :
12503 : 285 : typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
12504 : 285 : typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
12505 : 285 : typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
12506 : 285 : typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
12507 : 285 : typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
12508 : 285 : typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
12509 : 285 : typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
12510 : 285 : typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
12511 : 285 : typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
12512 : 285 : typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
12513 : 285 : typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
12514 : 285 : typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
12515 : 285 : typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
12516 : 285 : typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
12517 : 285 : typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
12518 : 285 : typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
12519 : 285 : typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
12520 : 285 : typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
12521 : 285 : typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
12522 : 285 : typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
12523 : 285 : typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
12524 : 285 : typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
12525 [ - + ]: 285 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12526 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12527 [ + + ]: 285 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12528 : : {
12529 : 44 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12530 : 44 : typdefault_is_literal = true; /* it needs quotes */
12531 : : }
12532 : : else
12533 : 241 : typdefault = NULL;
12534 : :
12535 : 285 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12536 : 285 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12537 : :
12538 : : /*
12539 : : * The reason we include CASCADE is that the circular dependency between
12540 : : * the type and its I/O functions makes it impossible to drop the type any
12541 : : * other way.
12542 : : */
12543 : 285 : appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
12544 : :
12545 : : /*
12546 : : * We might already have a shell type, but setting pg_type_oid is
12547 : : * harmless, and in any case we'd better set the array type OID.
12548 : : */
12549 [ + + ]: 285 : if (dopt->binary_upgrade)
12550 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12551 : 8 : tyinfo->dobj.catId.oid,
12552 : : false, false);
12553 : :
12554 : 285 : appendPQExpBuffer(q,
12555 : : "CREATE TYPE %s (\n"
12556 : : " INTERNALLENGTH = %s",
12557 : : qualtypname,
12558 [ + + ]: 285 : (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
12559 : :
12560 : : /* regproc result is sufficiently quoted already */
12561 : 285 : appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
12562 : 285 : appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
12563 [ + + ]: 285 : if (OidIsValid(typreceiveoid))
12564 : 210 : appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
12565 [ + + ]: 285 : if (OidIsValid(typsendoid))
12566 : 210 : appendPQExpBuffer(q, ",\n SEND = %s", typsend);
12567 [ + + ]: 285 : if (OidIsValid(typmodinoid))
12568 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
12569 [ + + ]: 285 : if (OidIsValid(typmodoutoid))
12570 : 35 : appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
12571 [ + + ]: 285 : if (OidIsValid(typanalyzeoid))
12572 : 3 : appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
12573 : :
12574 [ + + ]: 285 : if (strcmp(typcollatable, "t") == 0)
12575 : 30 : appendPQExpBufferStr(q, ",\n COLLATABLE = true");
12576 : :
12577 [ + + ]: 285 : if (typdefault != NULL)
12578 : : {
12579 : 44 : appendPQExpBufferStr(q, ",\n DEFAULT = ");
12580 [ + - ]: 44 : if (typdefault_is_literal)
12581 : 44 : appendStringLiteralAH(q, typdefault, fout);
12582 : : else
12583 : 0 : appendPQExpBufferStr(q, typdefault);
12584 : : }
12585 : :
12586 [ + + ]: 285 : if (OidIsValid(typsubscriptoid))
12587 : 29 : appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
12588 : :
12589 [ + + ]: 285 : if (OidIsValid(tyinfo->typelem))
12590 : 26 : appendPQExpBuffer(q, ",\n ELEMENT = %s",
12591 : 26 : getFormattedTypeName(fout, tyinfo->typelem,
12592 : : zeroIsError));
12593 : :
12594 [ + + ]: 285 : if (strcmp(typcategory, "U") != 0)
12595 : : {
12596 : 161 : appendPQExpBufferStr(q, ",\n CATEGORY = ");
12597 : 161 : appendStringLiteralAH(q, typcategory, fout);
12598 : : }
12599 : :
12600 [ + + ]: 285 : if (strcmp(typispreferred, "t") == 0)
12601 : 29 : appendPQExpBufferStr(q, ",\n PREFERRED = true");
12602 : :
12603 [ + - + + ]: 285 : if (typdelim && strcmp(typdelim, ",") != 0)
12604 : : {
12605 : 3 : appendPQExpBufferStr(q, ",\n DELIMITER = ");
12606 : 3 : appendStringLiteralAH(q, typdelim, fout);
12607 : : }
12608 : :
12609 [ + + ]: 285 : if (*typalign == TYPALIGN_CHAR)
12610 : 12 : appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
12611 [ + + ]: 273 : else if (*typalign == TYPALIGN_SHORT)
12612 : 6 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
12613 [ + + ]: 267 : else if (*typalign == TYPALIGN_INT)
12614 : 189 : appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
12615 [ + - ]: 78 : else if (*typalign == TYPALIGN_DOUBLE)
12616 : 78 : appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
12617 : :
12618 [ + + ]: 285 : if (*typstorage == TYPSTORAGE_PLAIN)
12619 : 210 : appendPQExpBufferStr(q, ",\n STORAGE = plain");
12620 [ - + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTERNAL)
12621 : 0 : appendPQExpBufferStr(q, ",\n STORAGE = external");
12622 [ + + ]: 75 : else if (*typstorage == TYPSTORAGE_EXTENDED)
12623 : 66 : appendPQExpBufferStr(q, ",\n STORAGE = extended");
12624 [ + - ]: 9 : else if (*typstorage == TYPSTORAGE_MAIN)
12625 : 9 : appendPQExpBufferStr(q, ",\n STORAGE = main");
12626 : :
12627 [ + + ]: 285 : if (strcmp(typbyval, "t") == 0)
12628 : 139 : appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
12629 : :
12630 : 285 : appendPQExpBufferStr(q, "\n);\n");
12631 : :
12632 [ + + ]: 285 : if (dopt->binary_upgrade)
12633 : 8 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12634 : : "TYPE", qtypname,
12635 : 8 : tyinfo->dobj.namespace->dobj.name);
12636 : :
12637 [ + - ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12638 : 285 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12639 : 285 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12640 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12641 : : .owner = tyinfo->rolname,
12642 : : .description = "TYPE",
12643 : : .section = SECTION_PRE_DATA,
12644 : : .createStmt = q->data,
12645 : : .dropStmt = delq->data));
12646 : :
12647 : : /* Dump Type Comments and Security Labels */
12648 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12649 : 250 : dumpComment(fout, "TYPE", qtypname,
12650 : 250 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12651 : 250 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12652 : :
12653 [ - + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12654 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
12655 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12656 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12657 : :
12658 [ + + ]: 285 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12659 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12660 : : qtypname, NULL,
12661 : 34 : tyinfo->dobj.namespace->dobj.name,
12662 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12663 : :
12664 : 285 : PQclear(res);
12665 : 285 : destroyPQExpBuffer(q);
12666 : 285 : destroyPQExpBuffer(delq);
12667 : 285 : destroyPQExpBuffer(query);
12668 : 285 : pg_free(qtypname);
12669 : 285 : pg_free(qualtypname);
12670 : 285 : }
12671 : :
12672 : : /*
12673 : : * dumpDomain
12674 : : * writes out to fout the queries to recreate a user-defined domain
12675 : : */
12676 : : static void
12677 : 174 : dumpDomain(Archive *fout, const TypeInfo *tyinfo)
12678 : : {
12679 : 174 : DumpOptions *dopt = fout->dopt;
12680 : 174 : PQExpBuffer q = createPQExpBuffer();
12681 : 174 : PQExpBuffer delq = createPQExpBuffer();
12682 : 174 : PQExpBuffer query = createPQExpBuffer();
12683 : : PGresult *res;
12684 : : int i;
12685 : : char *qtypname;
12686 : : char *qualtypname;
12687 : : char *typnotnull;
12688 : : char *typdefn;
12689 : : char *typdefault;
12690 : : Oid typcollation;
12691 : 174 : bool typdefault_is_literal = false;
12692 : :
12693 [ + + ]: 174 : if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN])
12694 : : {
12695 : : /* Set up query for domain-specific details */
12696 : 39 : appendPQExpBufferStr(query,
12697 : : "PREPARE dumpDomain(pg_catalog.oid) AS\n");
12698 : :
12699 : 39 : appendPQExpBufferStr(query, "SELECT t.typnotnull, "
12700 : : "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
12701 : : "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
12702 : : "t.typdefault, "
12703 : : "CASE WHEN t.typcollation <> u.typcollation "
12704 : : "THEN t.typcollation ELSE 0 END AS typcollation "
12705 : : "FROM pg_catalog.pg_type t "
12706 : : "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
12707 : : "WHERE t.oid = $1");
12708 : :
12709 : 39 : ExecuteSqlStatement(fout, query->data);
12710 : :
12711 : 39 : fout->is_prepared[PREPQUERY_DUMPDOMAIN] = true;
12712 : : }
12713 : :
12714 : 174 : printfPQExpBuffer(query,
12715 : : "EXECUTE dumpDomain('%u')",
12716 : 174 : tyinfo->dobj.catId.oid);
12717 : :
12718 : 174 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
12719 : :
12720 : 174 : typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
12721 : 174 : typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
12722 [ + + ]: 174 : if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
12723 : 39 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
12724 [ - + ]: 135 : else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
12725 : : {
12726 : 0 : typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
12727 : 0 : typdefault_is_literal = true; /* it needs quotes */
12728 : : }
12729 : : else
12730 : 135 : typdefault = NULL;
12731 : 174 : typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
12732 : :
12733 [ + + ]: 174 : if (dopt->binary_upgrade)
12734 : 29 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12735 : 29 : tyinfo->dobj.catId.oid,
12736 : : true, /* force array type */
12737 : : false); /* force multirange type */
12738 : :
12739 : 174 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12740 : 174 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12741 : :
12742 : 174 : appendPQExpBuffer(q,
12743 : : "CREATE DOMAIN %s AS %s",
12744 : : qualtypname,
12745 : : typdefn);
12746 : :
12747 : : /* Print collation only if different from base type's collation */
12748 [ + + ]: 174 : if (OidIsValid(typcollation))
12749 : : {
12750 : : CollInfo *coll;
12751 : :
12752 : 34 : coll = findCollationByOid(typcollation);
12753 [ + - ]: 34 : if (coll)
12754 : 34 : appendPQExpBuffer(q, " COLLATE %s", fmtQualifiedDumpable(coll));
12755 : : }
12756 : :
12757 : : /*
12758 : : * Print a not-null constraint if there's one. In servers older than 17
12759 : : * these don't have names, so just print it unadorned; in newer ones they
12760 : : * do, but most of the time it's going to be the standard generated one,
12761 : : * so omit the name in that case also.
12762 : : */
12763 [ + + ]: 174 : if (typnotnull[0] == 't')
12764 : : {
12765 [ + - - + ]: 49 : if (fout->remoteVersion < 170000 || tyinfo->notnull == NULL)
12766 : 0 : appendPQExpBufferStr(q, " NOT NULL");
12767 : : else
12768 : : {
12769 : 49 : ConstraintInfo *notnull = tyinfo->notnull;
12770 : :
12771 [ + - ]: 49 : if (!notnull->separate)
12772 : : {
12773 : : char *default_name;
12774 : :
12775 : : /* XXX should match ChooseConstraintName better */
12776 : 49 : default_name = psprintf("%s_not_null", tyinfo->dobj.name);
12777 : :
12778 [ + + ]: 49 : if (strcmp(default_name, notnull->dobj.name) == 0)
12779 : 15 : appendPQExpBufferStr(q, " NOT NULL");
12780 : : else
12781 : 34 : appendPQExpBuffer(q, " CONSTRAINT %s %s",
12782 : 34 : fmtId(notnull->dobj.name), notnull->condef);
12783 : 49 : pfree(default_name);
12784 : : }
12785 : : }
12786 : : }
12787 : :
12788 [ + + ]: 174 : if (typdefault != NULL)
12789 : : {
12790 : 39 : appendPQExpBufferStr(q, " DEFAULT ");
12791 [ - + ]: 39 : if (typdefault_is_literal)
12792 : 0 : appendStringLiteralAH(q, typdefault, fout);
12793 : : else
12794 : 39 : appendPQExpBufferStr(q, typdefault);
12795 : : }
12796 : :
12797 : 174 : PQclear(res);
12798 : :
12799 : : /*
12800 : : * Add any CHECK constraints for the domain
12801 : : */
12802 [ + + ]: 303 : for (i = 0; i < tyinfo->nDomChecks; i++)
12803 : : {
12804 : 129 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12805 : :
12806 [ + + + - ]: 129 : if (!domcheck->separate && domcheck->contype == 'c')
12807 : 124 : appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
12808 : 124 : fmtId(domcheck->dobj.name), domcheck->condef);
12809 : : }
12810 : :
12811 : 174 : appendPQExpBufferStr(q, ";\n");
12812 : :
12813 : 174 : appendPQExpBuffer(delq, "DROP DOMAIN %s;\n", qualtypname);
12814 : :
12815 [ + + ]: 174 : if (dopt->binary_upgrade)
12816 : 29 : binary_upgrade_extension_member(q, &tyinfo->dobj,
12817 : : "DOMAIN", qtypname,
12818 : 29 : tyinfo->dobj.namespace->dobj.name);
12819 : :
12820 [ + - ]: 174 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12821 : 174 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
12822 : 174 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
12823 : : .namespace = tyinfo->dobj.namespace->dobj.name,
12824 : : .owner = tyinfo->rolname,
12825 : : .description = "DOMAIN",
12826 : : .section = SECTION_PRE_DATA,
12827 : : .createStmt = q->data,
12828 : : .dropStmt = delq->data));
12829 : :
12830 : : /* Dump Domain Comments and Security Labels */
12831 [ - + ]: 174 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12832 : 0 : dumpComment(fout, "DOMAIN", qtypname,
12833 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12834 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12835 : :
12836 [ - + ]: 174 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
12837 : 0 : dumpSecLabel(fout, "DOMAIN", qtypname,
12838 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
12839 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
12840 : :
12841 [ + + ]: 174 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
12842 : 34 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
12843 : : qtypname, NULL,
12844 : 34 : tyinfo->dobj.namespace->dobj.name,
12845 : 34 : NULL, tyinfo->rolname, &tyinfo->dacl);
12846 : :
12847 : : /* Dump any per-constraint comments */
12848 [ + + ]: 303 : for (i = 0; i < tyinfo->nDomChecks; i++)
12849 : : {
12850 : 129 : ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
12851 : : PQExpBuffer conprefix;
12852 : :
12853 : : /* but only if the constraint itself was dumped here */
12854 [ + + ]: 129 : if (domcheck->separate)
12855 : 5 : continue;
12856 : :
12857 : 124 : conprefix = createPQExpBuffer();
12858 : 124 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12859 : 124 : fmtId(domcheck->dobj.name));
12860 : :
12861 [ + + ]: 124 : if (domcheck->dobj.dump & DUMP_COMPONENT_COMMENT)
12862 : 34 : dumpComment(fout, conprefix->data, qtypname,
12863 : 34 : tyinfo->dobj.namespace->dobj.name,
12864 : 34 : tyinfo->rolname,
12865 : 34 : domcheck->dobj.catId, 0, tyinfo->dobj.dumpId);
12866 : :
12867 : 124 : destroyPQExpBuffer(conprefix);
12868 : : }
12869 : :
12870 : : /*
12871 : : * And a comment on the not-null constraint, if there's one -- but only if
12872 : : * the constraint itself was dumped here
12873 : : */
12874 [ + + + - ]: 174 : if (tyinfo->notnull != NULL && !tyinfo->notnull->separate)
12875 : : {
12876 : 49 : PQExpBuffer conprefix = createPQExpBuffer();
12877 : :
12878 : 49 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
12879 : 49 : fmtId(tyinfo->notnull->dobj.name));
12880 : :
12881 [ + + ]: 49 : if (tyinfo->notnull->dobj.dump & DUMP_COMPONENT_COMMENT)
12882 : 34 : dumpComment(fout, conprefix->data, qtypname,
12883 : 34 : tyinfo->dobj.namespace->dobj.name,
12884 : 34 : tyinfo->rolname,
12885 : 34 : tyinfo->notnull->dobj.catId, 0, tyinfo->dobj.dumpId);
12886 : 49 : destroyPQExpBuffer(conprefix);
12887 : : }
12888 : :
12889 : 174 : destroyPQExpBuffer(q);
12890 : 174 : destroyPQExpBuffer(delq);
12891 : 174 : destroyPQExpBuffer(query);
12892 : 174 : pg_free(qtypname);
12893 : 174 : pg_free(qualtypname);
12894 : 174 : }
12895 : :
12896 : : /*
12897 : : * dumpCompositeType
12898 : : * writes out to fout the queries to recreate a user-defined stand-alone
12899 : : * composite type
12900 : : */
12901 : : static void
12902 : 132 : dumpCompositeType(Archive *fout, const TypeInfo *tyinfo)
12903 : : {
12904 : 132 : DumpOptions *dopt = fout->dopt;
12905 : 132 : PQExpBuffer q = createPQExpBuffer();
12906 : 132 : PQExpBuffer dropped = createPQExpBuffer();
12907 : 132 : PQExpBuffer delq = createPQExpBuffer();
12908 : 132 : PQExpBuffer query = createPQExpBuffer();
12909 : : PGresult *res;
12910 : : char *qtypname;
12911 : : char *qualtypname;
12912 : : int ntups;
12913 : : int i_attname;
12914 : : int i_atttypdefn;
12915 : : int i_attlen;
12916 : : int i_attalign;
12917 : : int i_attisdropped;
12918 : : int i_attcollation;
12919 : : int i;
12920 : : int actual_atts;
12921 : :
12922 [ + + ]: 132 : if (!fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE])
12923 : : {
12924 : : /*
12925 : : * Set up query for type-specific details.
12926 : : *
12927 : : * Since we only want to dump COLLATE clauses for attributes whose
12928 : : * collation is different from their type's default, we use a CASE
12929 : : * here to suppress uninteresting attcollations cheaply. atttypid
12930 : : * will be 0 for dropped columns; collation does not matter for those.
12931 : : */
12932 : 57 : appendPQExpBufferStr(query,
12933 : : "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
12934 : : "SELECT a.attname, a.attnum, "
12935 : : "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
12936 : : "a.attlen, a.attalign, a.attisdropped, "
12937 : : "CASE WHEN a.attcollation <> at.typcollation "
12938 : : "THEN a.attcollation ELSE 0 END AS attcollation "
12939 : : "FROM pg_catalog.pg_type ct "
12940 : : "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
12941 : : "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
12942 : : "WHERE ct.oid = $1 "
12943 : : "ORDER BY a.attnum");
12944 : :
12945 : 57 : ExecuteSqlStatement(fout, query->data);
12946 : :
12947 : 57 : fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE] = true;
12948 : : }
12949 : :
12950 : 132 : printfPQExpBuffer(query,
12951 : : "EXECUTE dumpCompositeType('%u')",
12952 : 132 : tyinfo->dobj.catId.oid);
12953 : :
12954 : 132 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12955 : :
12956 : 132 : ntups = PQntuples(res);
12957 : :
12958 : 132 : i_attname = PQfnumber(res, "attname");
12959 : 132 : i_atttypdefn = PQfnumber(res, "atttypdefn");
12960 : 132 : i_attlen = PQfnumber(res, "attlen");
12961 : 132 : i_attalign = PQfnumber(res, "attalign");
12962 : 132 : i_attisdropped = PQfnumber(res, "attisdropped");
12963 : 132 : i_attcollation = PQfnumber(res, "attcollation");
12964 : :
12965 [ + + ]: 132 : if (dopt->binary_upgrade)
12966 : : {
12967 : 18 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
12968 : 18 : tyinfo->dobj.catId.oid,
12969 : : false, false);
12970 : 18 : binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid);
12971 : : }
12972 : :
12973 : 132 : qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
12974 : 132 : qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
12975 : :
12976 : 132 : appendPQExpBuffer(q, "CREATE TYPE %s AS (",
12977 : : qualtypname);
12978 : :
12979 : 132 : actual_atts = 0;
12980 [ + + ]: 418 : for (i = 0; i < ntups; i++)
12981 : : {
12982 : : char *attname;
12983 : : char *atttypdefn;
12984 : : char *attlen;
12985 : : char *attalign;
12986 : : bool attisdropped;
12987 : : Oid attcollation;
12988 : :
12989 : 286 : attname = PQgetvalue(res, i, i_attname);
12990 : 286 : atttypdefn = PQgetvalue(res, i, i_atttypdefn);
12991 : 286 : attlen = PQgetvalue(res, i, i_attlen);
12992 : 286 : attalign = PQgetvalue(res, i, i_attalign);
12993 : 286 : attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
12994 : 286 : attcollation = atooid(PQgetvalue(res, i, i_attcollation));
12995 : :
12996 [ + + + + ]: 286 : if (attisdropped && !dopt->binary_upgrade)
12997 : 8 : continue;
12998 : :
12999 : : /* Format properly if not first attr */
13000 [ + + ]: 278 : if (actual_atts++ > 0)
13001 : 146 : appendPQExpBufferChar(q, ',');
13002 : 278 : appendPQExpBufferStr(q, "\n\t");
13003 : :
13004 [ + + ]: 278 : if (!attisdropped)
13005 : : {
13006 : 276 : appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
13007 : :
13008 : : /* Add collation if not default for the column type */
13009 [ - + ]: 276 : if (OidIsValid(attcollation))
13010 : : {
13011 : : CollInfo *coll;
13012 : :
13013 : 0 : coll = findCollationByOid(attcollation);
13014 [ # # ]: 0 : if (coll)
13015 : 0 : appendPQExpBuffer(q, " COLLATE %s",
13016 : 0 : fmtQualifiedDumpable(coll));
13017 : : }
13018 : : }
13019 : : else
13020 : : {
13021 : : /*
13022 : : * This is a dropped attribute and we're in binary_upgrade mode.
13023 : : * Insert a placeholder for it in the CREATE TYPE command, and set
13024 : : * length and alignment with direct UPDATE to the catalogs
13025 : : * afterwards. See similar code in dumpTableSchema().
13026 : : */
13027 : 2 : appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
13028 : :
13029 : : /* stash separately for insertion after the CREATE TYPE */
13030 : 2 : appendPQExpBufferStr(dropped,
13031 : : "\n-- For binary upgrade, recreate dropped column.\n");
13032 : 2 : appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
13033 : : "SET attlen = %s, "
13034 : : "attalign = '%s', attbyval = false\n"
13035 : : "WHERE attname = ", attlen, attalign);
13036 : 2 : appendStringLiteralAH(dropped, attname, fout);
13037 : 2 : appendPQExpBufferStr(dropped, "\n AND attrelid = ");
13038 : 2 : appendStringLiteralAH(dropped, qualtypname, fout);
13039 : 2 : appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
13040 : :
13041 : 2 : appendPQExpBuffer(dropped, "ALTER TYPE %s ",
13042 : : qualtypname);
13043 : 2 : appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
13044 : : fmtId(attname));
13045 : : }
13046 : : }
13047 : 132 : appendPQExpBufferStr(q, "\n);\n");
13048 : 132 : appendPQExpBufferStr(q, dropped->data);
13049 : :
13050 : 132 : appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
13051 : :
13052 [ + + ]: 132 : if (dopt->binary_upgrade)
13053 : 18 : binary_upgrade_extension_member(q, &tyinfo->dobj,
13054 : : "TYPE", qtypname,
13055 : 18 : tyinfo->dobj.namespace->dobj.name);
13056 : :
13057 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13058 : 115 : ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
13059 : 115 : ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
13060 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13061 : : .owner = tyinfo->rolname,
13062 : : .description = "TYPE",
13063 : : .section = SECTION_PRE_DATA,
13064 : : .createStmt = q->data,
13065 : : .dropStmt = delq->data));
13066 : :
13067 : :
13068 : : /* Dump Type Comments and Security Labels */
13069 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13070 : 34 : dumpComment(fout, "TYPE", qtypname,
13071 : 34 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13072 : 34 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13073 : :
13074 [ - + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13075 : 0 : dumpSecLabel(fout, "TYPE", qtypname,
13076 : 0 : tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
13077 : 0 : tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
13078 : :
13079 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
13080 : 18 : dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
13081 : : qtypname, NULL,
13082 : 18 : tyinfo->dobj.namespace->dobj.name,
13083 : 18 : NULL, tyinfo->rolname, &tyinfo->dacl);
13084 : :
13085 : : /* Dump any per-column comments */
13086 [ + + ]: 132 : if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13087 : 34 : dumpCompositeTypeColComments(fout, tyinfo, res);
13088 : :
13089 : 132 : PQclear(res);
13090 : 132 : destroyPQExpBuffer(q);
13091 : 132 : destroyPQExpBuffer(dropped);
13092 : 132 : destroyPQExpBuffer(delq);
13093 : 132 : destroyPQExpBuffer(query);
13094 : 132 : pg_free(qtypname);
13095 : 132 : pg_free(qualtypname);
13096 : 132 : }
13097 : :
13098 : : /*
13099 : : * dumpCompositeTypeColComments
13100 : : * writes out to fout the queries to recreate comments on the columns of
13101 : : * a user-defined stand-alone composite type.
13102 : : *
13103 : : * The caller has already made a query to collect the names and attnums
13104 : : * of the type's columns, so we just pass that result into here rather
13105 : : * than reading them again.
13106 : : */
13107 : : static void
13108 : 34 : dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo,
13109 : : PGresult *res)
13110 : : {
13111 : : CommentItem *comments;
13112 : : int ncomments;
13113 : : PQExpBuffer query;
13114 : : PQExpBuffer target;
13115 : : int i;
13116 : : int ntups;
13117 : : int i_attname;
13118 : : int i_attnum;
13119 : : int i_attisdropped;
13120 : :
13121 : : /* do nothing, if --no-comments is supplied */
13122 [ - + ]: 34 : if (fout->dopt->no_comments)
13123 : 0 : return;
13124 : :
13125 : : /* Search for comments associated with type's pg_class OID */
13126 : 34 : ncomments = findComments(RelationRelationId, tyinfo->typrelid,
13127 : : &comments);
13128 : :
13129 : : /* If no comments exist, we're done */
13130 [ - + ]: 34 : if (ncomments <= 0)
13131 : 0 : return;
13132 : :
13133 : : /* Build COMMENT ON statements */
13134 : 34 : query = createPQExpBuffer();
13135 : 34 : target = createPQExpBuffer();
13136 : :
13137 : 34 : ntups = PQntuples(res);
13138 : 34 : i_attnum = PQfnumber(res, "attnum");
13139 : 34 : i_attname = PQfnumber(res, "attname");
13140 : 34 : i_attisdropped = PQfnumber(res, "attisdropped");
13141 [ + + ]: 68 : while (ncomments > 0)
13142 : : {
13143 : : const char *attname;
13144 : :
13145 : 34 : attname = NULL;
13146 [ + - ]: 34 : for (i = 0; i < ntups; i++)
13147 : : {
13148 [ + - ]: 34 : if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
13149 [ + - ]: 34 : PQgetvalue(res, i, i_attisdropped)[0] != 't')
13150 : : {
13151 : 34 : attname = PQgetvalue(res, i, i_attname);
13152 : 34 : break;
13153 : : }
13154 : : }
13155 [ + - ]: 34 : if (attname) /* just in case we don't find it */
13156 : : {
13157 : 34 : const char *descr = comments->descr;
13158 : :
13159 : 34 : resetPQExpBuffer(target);
13160 : 34 : appendPQExpBuffer(target, "COLUMN %s.",
13161 : 34 : fmtId(tyinfo->dobj.name));
13162 : 34 : appendPQExpBufferStr(target, fmtId(attname));
13163 : :
13164 : 34 : resetPQExpBuffer(query);
13165 : 34 : appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
13166 : 34 : fmtQualifiedDumpable(tyinfo));
13167 : 34 : appendPQExpBuffer(query, "%s IS ", fmtId(attname));
13168 : 34 : appendStringLiteralAH(query, descr, fout);
13169 : 34 : appendPQExpBufferStr(query, ";\n");
13170 : :
13171 : 34 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
13172 : 34 : ARCHIVE_OPTS(.tag = target->data,
13173 : : .namespace = tyinfo->dobj.namespace->dobj.name,
13174 : : .owner = tyinfo->rolname,
13175 : : .description = "COMMENT",
13176 : : .section = SECTION_NONE,
13177 : : .createStmt = query->data,
13178 : : .deps = &(tyinfo->dobj.dumpId),
13179 : : .nDeps = 1));
13180 : : }
13181 : :
13182 : 34 : comments++;
13183 : 34 : ncomments--;
13184 : : }
13185 : :
13186 : 34 : destroyPQExpBuffer(query);
13187 : 34 : destroyPQExpBuffer(target);
13188 : : }
13189 : :
13190 : : /*
13191 : : * dumpShellType
13192 : : * writes out to fout the queries to create a shell type
13193 : : *
13194 : : * We dump a shell definition in advance of the I/O functions for the type.
13195 : : */
13196 : : static void
13197 : 76 : dumpShellType(Archive *fout, const ShellTypeInfo *stinfo)
13198 : : {
13199 : 76 : DumpOptions *dopt = fout->dopt;
13200 : : PQExpBuffer q;
13201 : :
13202 : : /* Do nothing if not dumping schema */
13203 [ + + ]: 76 : if (!dopt->dumpSchema)
13204 : 7 : return;
13205 : :
13206 : 69 : q = createPQExpBuffer();
13207 : :
13208 : : /*
13209 : : * Note the lack of a DROP command for the shell type; any required DROP
13210 : : * is driven off the base type entry, instead. This interacts with
13211 : : * _printTocEntry()'s use of the presence of a DROP command to decide
13212 : : * whether an entry needs an ALTER OWNER command. We don't want to alter
13213 : : * the shell type's owner immediately on creation; that should happen only
13214 : : * after it's filled in, otherwise the backend complains.
13215 : : */
13216 : :
13217 [ + + ]: 69 : if (dopt->binary_upgrade)
13218 : 8 : binary_upgrade_set_type_oids_by_type_oid(fout, q,
13219 : 8 : stinfo->baseType->dobj.catId.oid,
13220 : : false, false);
13221 : :
13222 : 69 : appendPQExpBuffer(q, "CREATE TYPE %s;\n",
13223 : 69 : fmtQualifiedDumpable(stinfo));
13224 : :
13225 [ + - ]: 69 : if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13226 : 69 : ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId,
13227 : 69 : ARCHIVE_OPTS(.tag = stinfo->dobj.name,
13228 : : .namespace = stinfo->dobj.namespace->dobj.name,
13229 : : .owner = stinfo->baseType->rolname,
13230 : : .description = "SHELL TYPE",
13231 : : .section = SECTION_PRE_DATA,
13232 : : .createStmt = q->data));
13233 : :
13234 : 69 : destroyPQExpBuffer(q);
13235 : : }
13236 : :
13237 : : /*
13238 : : * dumpProcLang
13239 : : * writes out to fout the queries to recreate a user-defined
13240 : : * procedural language
13241 : : */
13242 : : static void
13243 : 87 : dumpProcLang(Archive *fout, const ProcLangInfo *plang)
13244 : : {
13245 : 87 : DumpOptions *dopt = fout->dopt;
13246 : : PQExpBuffer defqry;
13247 : : PQExpBuffer delqry;
13248 : : bool useParams;
13249 : : char *qlanname;
13250 : : FuncInfo *funcInfo;
13251 : 87 : FuncInfo *inlineInfo = NULL;
13252 : 87 : FuncInfo *validatorInfo = NULL;
13253 : :
13254 : : /* Do nothing if not dumping schema */
13255 [ + + ]: 87 : if (!dopt->dumpSchema)
13256 : 14 : return;
13257 : :
13258 : : /*
13259 : : * Try to find the support function(s). It is not an error if we don't
13260 : : * find them --- if the functions are in the pg_catalog schema, as is
13261 : : * standard in 8.1 and up, then we won't have loaded them. (In this case
13262 : : * we will emit a parameterless CREATE LANGUAGE command, which will
13263 : : * require PL template knowledge in the backend to reload.)
13264 : : */
13265 : :
13266 : 73 : funcInfo = findFuncByOid(plang->lanplcallfoid);
13267 [ + + + + ]: 73 : if (funcInfo != NULL && !funcInfo->dobj.dump)
13268 : 2 : funcInfo = NULL; /* treat not-dumped same as not-found */
13269 : :
13270 [ + + ]: 73 : if (OidIsValid(plang->laninline))
13271 : : {
13272 : 40 : inlineInfo = findFuncByOid(plang->laninline);
13273 [ + + + - ]: 40 : if (inlineInfo != NULL && !inlineInfo->dobj.dump)
13274 : 1 : inlineInfo = NULL;
13275 : : }
13276 : :
13277 [ + + ]: 73 : if (OidIsValid(plang->lanvalidator))
13278 : : {
13279 : 40 : validatorInfo = findFuncByOid(plang->lanvalidator);
13280 [ + + + - ]: 40 : if (validatorInfo != NULL && !validatorInfo->dobj.dump)
13281 : 1 : validatorInfo = NULL;
13282 : : }
13283 : :
13284 : : /*
13285 : : * If the functions are dumpable then emit a complete CREATE LANGUAGE with
13286 : : * parameters. Otherwise, we'll write a parameterless command, which will
13287 : : * be interpreted as CREATE EXTENSION.
13288 : : */
13289 [ + - ]: 32 : useParams = (funcInfo != NULL &&
13290 [ + + + - : 137 : (inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
+ - ]
13291 [ + - ]: 32 : (validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
13292 : :
13293 : 73 : defqry = createPQExpBuffer();
13294 : 73 : delqry = createPQExpBuffer();
13295 : :
13296 : 73 : qlanname = pg_strdup(fmtId(plang->dobj.name));
13297 : :
13298 : 73 : appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
13299 : : qlanname);
13300 : :
13301 [ + + ]: 73 : if (useParams)
13302 : : {
13303 : 32 : appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
13304 [ - + ]: 32 : plang->lanpltrusted ? "TRUSTED " : "",
13305 : : qlanname);
13306 : 32 : appendPQExpBuffer(defqry, " HANDLER %s",
13307 : 32 : fmtQualifiedDumpable(funcInfo));
13308 [ - + ]: 32 : if (OidIsValid(plang->laninline))
13309 : 0 : appendPQExpBuffer(defqry, " INLINE %s",
13310 : 0 : fmtQualifiedDumpable(inlineInfo));
13311 [ - + ]: 32 : if (OidIsValid(plang->lanvalidator))
13312 : 0 : appendPQExpBuffer(defqry, " VALIDATOR %s",
13313 : 0 : fmtQualifiedDumpable(validatorInfo));
13314 : : }
13315 : : else
13316 : : {
13317 : : /*
13318 : : * If not dumping parameters, then use CREATE OR REPLACE so that the
13319 : : * command will not fail if the language is preinstalled in the target
13320 : : * database.
13321 : : *
13322 : : * Modern servers will interpret this as CREATE EXTENSION IF NOT
13323 : : * EXISTS; perhaps we should emit that instead? But it might just add
13324 : : * confusion.
13325 : : */
13326 : 41 : appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s",
13327 : : qlanname);
13328 : : }
13329 : 73 : appendPQExpBufferStr(defqry, ";\n");
13330 : :
13331 [ + + ]: 73 : if (dopt->binary_upgrade)
13332 : 2 : binary_upgrade_extension_member(defqry, &plang->dobj,
13333 : : "LANGUAGE", qlanname, NULL);
13334 : :
13335 [ + + ]: 73 : if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION)
13336 : 33 : ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
13337 : 33 : ARCHIVE_OPTS(.tag = plang->dobj.name,
13338 : : .owner = plang->lanowner,
13339 : : .description = "PROCEDURAL LANGUAGE",
13340 : : .section = SECTION_PRE_DATA,
13341 : : .createStmt = defqry->data,
13342 : : .dropStmt = delqry->data,
13343 : : ));
13344 : :
13345 : : /* Dump Proc Lang Comments and Security Labels */
13346 [ - + ]: 73 : if (plang->dobj.dump & DUMP_COMPONENT_COMMENT)
13347 : 0 : dumpComment(fout, "LANGUAGE", qlanname,
13348 : 0 : NULL, plang->lanowner,
13349 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13350 : :
13351 [ - + ]: 73 : if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL)
13352 : 0 : dumpSecLabel(fout, "LANGUAGE", qlanname,
13353 : 0 : NULL, plang->lanowner,
13354 : 0 : plang->dobj.catId, 0, plang->dobj.dumpId);
13355 : :
13356 [ + + + - ]: 73 : if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL)
13357 : 40 : dumpACL(fout, plang->dobj.dumpId, InvalidDumpId, "LANGUAGE",
13358 : : qlanname, NULL, NULL,
13359 : 40 : NULL, plang->lanowner, &plang->dacl);
13360 : :
13361 : 73 : pg_free(qlanname);
13362 : :
13363 : 73 : destroyPQExpBuffer(defqry);
13364 : 73 : destroyPQExpBuffer(delqry);
13365 : : }
13366 : :
13367 : : /*
13368 : : * format_function_arguments: generate function name and argument list
13369 : : *
13370 : : * This is used when we can rely on pg_get_function_arguments to format
13371 : : * the argument list. Note, however, that pg_get_function_arguments
13372 : : * does not special-case zero-argument aggregates.
13373 : : */
13374 : : static char *
13375 : 4278 : format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
13376 : : {
13377 : : PQExpBufferData fn;
13378 : :
13379 : 4278 : initPQExpBuffer(&fn);
13380 : 4278 : appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name));
13381 [ + + + + ]: 4278 : if (is_agg && finfo->nargs == 0)
13382 : 80 : appendPQExpBufferStr(&fn, "(*)");
13383 : : else
13384 : 4198 : appendPQExpBuffer(&fn, "(%s)", funcargs);
13385 : 4278 : return fn.data;
13386 : : }
13387 : :
13388 : : /*
13389 : : * format_function_signature: generate function name and argument list
13390 : : *
13391 : : * Only a minimal list of input argument types is generated; this is
13392 : : * sufficient to reference the function, but not to define it.
13393 : : *
13394 : : * If honor_quotes is false then the function name is never quoted.
13395 : : * This is appropriate for use in TOC tags, but not in SQL commands.
13396 : : */
13397 : : static char *
13398 : 2253 : format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
13399 : : {
13400 : : PQExpBufferData fn;
13401 : : int j;
13402 : :
13403 : 2253 : initPQExpBuffer(&fn);
13404 [ + + ]: 2253 : if (honor_quotes)
13405 : 401 : appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
13406 : : else
13407 : 1852 : appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
13408 [ + + ]: 4145 : for (j = 0; j < finfo->nargs; j++)
13409 : : {
13410 [ + + ]: 1892 : if (j > 0)
13411 : 452 : appendPQExpBufferStr(&fn, ", ");
13412 : :
13413 : 1892 : appendPQExpBufferStr(&fn,
13414 : 1892 : getFormattedTypeName(fout, finfo->argtypes[j],
13415 : : zeroIsError));
13416 : : }
13417 : 2253 : appendPQExpBufferChar(&fn, ')');
13418 : 2253 : return fn.data;
13419 : : }
13420 : :
13421 : :
13422 : : /*
13423 : : * dumpFunc:
13424 : : * dump out one function
13425 : : */
13426 : : static void
13427 : 1922 : dumpFunc(Archive *fout, const FuncInfo *finfo)
13428 : : {
13429 : 1922 : DumpOptions *dopt = fout->dopt;
13430 : : PQExpBuffer query;
13431 : : PQExpBuffer q;
13432 : : PQExpBuffer delqry;
13433 : : PQExpBuffer asPart;
13434 : : PGresult *res;
13435 : : char *funcsig; /* identity signature */
13436 : 1922 : char *funcfullsig = NULL; /* full signature */
13437 : : char *funcsig_tag;
13438 : : char *qual_funcsig;
13439 : : char *proretset;
13440 : : char *prosrc;
13441 : : char *probin;
13442 : : char *prosqlbody;
13443 : : char *funcargs;
13444 : : char *funciargs;
13445 : : char *funcresult;
13446 : : char *protrftypes;
13447 : : char *prokind;
13448 : : char *provolatile;
13449 : : char *proisstrict;
13450 : : char *prosecdef;
13451 : : char *proleakproof;
13452 : : char *proconfig;
13453 : : char *procost;
13454 : : char *prorows;
13455 : : char *prosupport;
13456 : : char *proparallel;
13457 : : char *lanname;
13458 : 1922 : char **configitems = NULL;
13459 : 1922 : int nconfigitems = 0;
13460 : : const char *keyword;
13461 : :
13462 : : /* Do nothing if not dumping schema */
13463 [ + + ]: 1922 : if (!dopt->dumpSchema)
13464 : 70 : return;
13465 : :
13466 : 1852 : query = createPQExpBuffer();
13467 : 1852 : q = createPQExpBuffer();
13468 : 1852 : delqry = createPQExpBuffer();
13469 : 1852 : asPart = createPQExpBuffer();
13470 : :
13471 [ + + ]: 1852 : if (!fout->is_prepared[PREPQUERY_DUMPFUNC])
13472 : : {
13473 : : /* Set up query for function-specific details */
13474 : 69 : appendPQExpBufferStr(query,
13475 : : "PREPARE dumpFunc(pg_catalog.oid) AS\n");
13476 : :
13477 : 69 : appendPQExpBufferStr(query,
13478 : : "SELECT\n"
13479 : : "proretset,\n"
13480 : : "prosrc,\n"
13481 : : "probin,\n"
13482 : : "provolatile,\n"
13483 : : "proisstrict,\n"
13484 : : "prosecdef,\n"
13485 : : "lanname,\n"
13486 : : "proconfig,\n"
13487 : : "procost,\n"
13488 : : "prorows,\n"
13489 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13490 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n"
13491 : : "pg_catalog.pg_get_function_result(p.oid) AS funcresult,\n"
13492 : : "proleakproof,\n");
13493 : :
13494 : 69 : appendPQExpBufferStr(query,
13495 : : "array_to_string(protrftypes, ' ') AS protrftypes,\n");
13496 : :
13497 : 69 : appendPQExpBufferStr(query,
13498 : : "proparallel,\n");
13499 : :
13500 [ + - ]: 69 : if (fout->remoteVersion >= 110000)
13501 : 69 : appendPQExpBufferStr(query,
13502 : : "prokind,\n");
13503 : : else
13504 : 0 : appendPQExpBufferStr(query,
13505 : : "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind,\n");
13506 : :
13507 [ + - ]: 69 : if (fout->remoteVersion >= 120000)
13508 : 69 : appendPQExpBufferStr(query,
13509 : : "prosupport,\n");
13510 : : else
13511 : 0 : appendPQExpBufferStr(query,
13512 : : "'-' AS prosupport,\n");
13513 : :
13514 [ + - ]: 69 : if (fout->remoteVersion >= 140000)
13515 : 69 : appendPQExpBufferStr(query,
13516 : : "pg_get_function_sqlbody(p.oid) AS prosqlbody\n");
13517 : : else
13518 : 0 : appendPQExpBufferStr(query,
13519 : : "NULL AS prosqlbody\n");
13520 : :
13521 : 69 : appendPQExpBufferStr(query,
13522 : : "FROM pg_catalog.pg_proc p, pg_catalog.pg_language l\n"
13523 : : "WHERE p.oid = $1 "
13524 : : "AND l.oid = p.prolang");
13525 : :
13526 : 69 : ExecuteSqlStatement(fout, query->data);
13527 : :
13528 : 69 : fout->is_prepared[PREPQUERY_DUMPFUNC] = true;
13529 : : }
13530 : :
13531 : 1852 : printfPQExpBuffer(query,
13532 : : "EXECUTE dumpFunc('%u')",
13533 : 1852 : finfo->dobj.catId.oid);
13534 : :
13535 : 1852 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
13536 : :
13537 : 1852 : proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
13538 [ + + ]: 1852 : if (PQgetisnull(res, 0, PQfnumber(res, "prosqlbody")))
13539 : : {
13540 : 1802 : prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
13541 : 1802 : probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
13542 : 1802 : prosqlbody = NULL;
13543 : : }
13544 : : else
13545 : : {
13546 : 50 : prosrc = NULL;
13547 : 50 : probin = NULL;
13548 : 50 : prosqlbody = PQgetvalue(res, 0, PQfnumber(res, "prosqlbody"));
13549 : : }
13550 : 1852 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13551 : 1852 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13552 : 1852 : funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult"));
13553 : 1852 : protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes"));
13554 : 1852 : prokind = PQgetvalue(res, 0, PQfnumber(res, "prokind"));
13555 : 1852 : provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
13556 : 1852 : proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
13557 : 1852 : prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
13558 : 1852 : proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof"));
13559 : 1852 : proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig"));
13560 : 1852 : procost = PQgetvalue(res, 0, PQfnumber(res, "procost"));
13561 : 1852 : prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows"));
13562 : 1852 : prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport"));
13563 : 1852 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13564 : 1852 : lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
13565 : :
13566 : : /*
13567 : : * See backend/commands/functioncmds.c for details of how the 'AS' clause
13568 : : * is used.
13569 : : */
13570 [ + + ]: 1852 : if (prosqlbody)
13571 : : {
13572 : 50 : appendPQExpBufferStr(asPart, prosqlbody);
13573 : : }
13574 [ + + ]: 1802 : else if (probin[0] != '\0')
13575 : : {
13576 : 160 : appendPQExpBufferStr(asPart, "AS ");
13577 : 160 : appendStringLiteralAH(asPart, probin, fout);
13578 [ + - ]: 160 : if (prosrc[0] != '\0')
13579 : : {
13580 : 160 : appendPQExpBufferStr(asPart, ", ");
13581 : :
13582 : : /*
13583 : : * where we have bin, use dollar quoting if allowed and src
13584 : : * contains quote or backslash; else use regular quoting.
13585 : : */
13586 [ + - ]: 160 : if (dopt->disable_dollar_quoting ||
13587 [ + - + - ]: 160 : (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL))
13588 : 160 : appendStringLiteralAH(asPart, prosrc, fout);
13589 : : else
13590 : 0 : appendStringLiteralDQ(asPart, prosrc, NULL);
13591 : : }
13592 : : }
13593 : : else
13594 : : {
13595 : 1642 : appendPQExpBufferStr(asPart, "AS ");
13596 : : /* with no bin, dollar quote src unconditionally if allowed */
13597 [ - + ]: 1642 : if (dopt->disable_dollar_quoting)
13598 : 0 : appendStringLiteralAH(asPart, prosrc, fout);
13599 : : else
13600 : 1642 : appendStringLiteralDQ(asPart, prosrc, NULL);
13601 : : }
13602 : :
13603 [ + + ]: 1852 : if (*proconfig)
13604 : : {
13605 [ - + ]: 15 : if (!parsePGArray(proconfig, &configitems, &nconfigitems))
13606 : 0 : pg_fatal("could not parse %s array", "proconfig");
13607 : : }
13608 : : else
13609 : : {
13610 : 1837 : configitems = NULL;
13611 : 1837 : nconfigitems = 0;
13612 : : }
13613 : :
13614 : 1852 : funcfullsig = format_function_arguments(finfo, funcargs, false);
13615 : 1852 : funcsig = format_function_arguments(finfo, funciargs, false);
13616 : :
13617 : 1852 : funcsig_tag = format_function_signature(fout, finfo, false);
13618 : :
13619 : 1852 : qual_funcsig = psprintf("%s.%s",
13620 : 1852 : fmtId(finfo->dobj.namespace->dobj.name),
13621 : : funcsig);
13622 : :
13623 [ + + ]: 1852 : if (prokind[0] == PROKIND_PROCEDURE)
13624 : 94 : keyword = "PROCEDURE";
13625 : : else
13626 : 1758 : keyword = "FUNCTION"; /* works for window functions too */
13627 : :
13628 : 1852 : appendPQExpBuffer(delqry, "DROP %s %s;\n",
13629 : : keyword, qual_funcsig);
13630 : :
13631 [ + - ]: 3704 : appendPQExpBuffer(q, "CREATE %s %s.%s",
13632 : : keyword,
13633 : 1852 : fmtId(finfo->dobj.namespace->dobj.name),
13634 : : funcfullsig ? funcfullsig :
13635 : : funcsig);
13636 : :
13637 [ + + ]: 1852 : if (prokind[0] == PROKIND_PROCEDURE)
13638 : : /* no result type to output */ ;
13639 [ + - ]: 1758 : else if (funcresult)
13640 : 1758 : appendPQExpBuffer(q, " RETURNS %s", funcresult);
13641 : : else
13642 : 0 : appendPQExpBuffer(q, " RETURNS %s%s",
13643 [ # # ]: 0 : (proretset[0] == 't') ? "SETOF " : "",
13644 : 0 : getFormattedTypeName(fout, finfo->prorettype,
13645 : : zeroIsError));
13646 : :
13647 : 1852 : appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname));
13648 : :
13649 [ + + ]: 1852 : if (*protrftypes)
13650 : : {
13651 : 5 : Oid *typeids = parseOidArray(protrftypes, -1);
13652 : :
13653 : 5 : appendPQExpBufferStr(q, " TRANSFORM ");
13654 [ + + ]: 10 : for (int i = 0; typeids[i]; i++)
13655 : : {
13656 [ - + ]: 5 : if (i != 0)
13657 : 0 : appendPQExpBufferStr(q, ", ");
13658 : 5 : appendPQExpBuffer(q, "FOR TYPE %s",
13659 : 5 : getFormattedTypeName(fout, typeids[i], zeroAsNone));
13660 : : }
13661 : :
13662 : 5 : pg_free(typeids);
13663 : : }
13664 : :
13665 [ + + ]: 1852 : if (prokind[0] == PROKIND_WINDOW)
13666 : 5 : appendPQExpBufferStr(q, " WINDOW");
13667 : :
13668 [ + + ]: 1852 : if (provolatile[0] != PROVOLATILE_VOLATILE)
13669 : : {
13670 [ + + ]: 355 : if (provolatile[0] == PROVOLATILE_IMMUTABLE)
13671 : 334 : appendPQExpBufferStr(q, " IMMUTABLE");
13672 [ + - ]: 21 : else if (provolatile[0] == PROVOLATILE_STABLE)
13673 : 21 : appendPQExpBufferStr(q, " STABLE");
13674 [ # # ]: 0 : else if (provolatile[0] != PROVOLATILE_VOLATILE)
13675 : 0 : pg_fatal("unrecognized provolatile value for function \"%s\"",
13676 : : finfo->dobj.name);
13677 : : }
13678 : :
13679 [ + + ]: 1852 : if (proisstrict[0] == 't')
13680 : 364 : appendPQExpBufferStr(q, " STRICT");
13681 : :
13682 [ - + ]: 1852 : if (prosecdef[0] == 't')
13683 : 0 : appendPQExpBufferStr(q, " SECURITY DEFINER");
13684 : :
13685 [ + + ]: 1852 : if (proleakproof[0] == 't')
13686 : 10 : appendPQExpBufferStr(q, " LEAKPROOF");
13687 : :
13688 : : /*
13689 : : * COST and ROWS are emitted only if present and not default, so as not to
13690 : : * break backwards-compatibility of the dump without need. Keep this code
13691 : : * in sync with the defaults in functioncmds.c.
13692 : : */
13693 [ + - ]: 1852 : if (strcmp(procost, "0") != 0)
13694 : : {
13695 [ + + + + ]: 1852 : if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0)
13696 : : {
13697 : : /* default cost is 1 */
13698 [ - + ]: 397 : if (strcmp(procost, "1") != 0)
13699 : 0 : appendPQExpBuffer(q, " COST %s", procost);
13700 : : }
13701 : : else
13702 : : {
13703 : : /* default cost is 100 */
13704 [ + + ]: 1455 : if (strcmp(procost, "100") != 0)
13705 : 11 : appendPQExpBuffer(q, " COST %s", procost);
13706 : : }
13707 : : }
13708 [ + + ]: 1852 : if (proretset[0] == 't' &&
13709 [ + - - + ]: 194 : strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0)
13710 : 0 : appendPQExpBuffer(q, " ROWS %s", prorows);
13711 : :
13712 [ + + ]: 1852 : if (strcmp(prosupport, "-") != 0)
13713 : : {
13714 : : /* We rely on regprocout to provide quoting and qualification */
13715 : 44 : appendPQExpBuffer(q, " SUPPORT %s", prosupport);
13716 : : }
13717 : :
13718 [ + + ]: 1852 : if (proparallel[0] != PROPARALLEL_UNSAFE)
13719 : : {
13720 [ + + ]: 120 : if (proparallel[0] == PROPARALLEL_SAFE)
13721 : 115 : appendPQExpBufferStr(q, " PARALLEL SAFE");
13722 [ + - ]: 5 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13723 : 5 : appendPQExpBufferStr(q, " PARALLEL RESTRICTED");
13724 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
13725 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
13726 : : finfo->dobj.name);
13727 : : }
13728 : :
13729 [ + + ]: 1892 : for (int i = 0; i < nconfigitems; i++)
13730 : : {
13731 : : /* we feel free to scribble on configitems[] here */
13732 : 40 : char *configitem = configitems[i];
13733 : : char *pos;
13734 : :
13735 : 40 : pos = strchr(configitem, '=');
13736 [ - + ]: 40 : if (pos == NULL)
13737 : 0 : continue;
13738 : 40 : *pos++ = '\0';
13739 : 40 : appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem));
13740 : :
13741 : : /*
13742 : : * Variables that are marked GUC_LIST_QUOTE were already fully quoted
13743 : : * by flatten_set_variable_args() before they were put into the
13744 : : * proconfig array. However, because the quoting rules used there
13745 : : * aren't exactly like SQL's, we have to break the list value apart
13746 : : * and then quote the elements as string literals. (The elements may
13747 : : * be double-quoted as-is, but we can't just feed them to the SQL
13748 : : * parser; it would do the wrong thing with elements that are
13749 : : * zero-length or longer than NAMEDATALEN.) Also, we need a special
13750 : : * case for empty lists.
13751 : : *
13752 : : * Variables that are not so marked should just be emitted as simple
13753 : : * string literals. If the variable is not known to
13754 : : * variable_is_guc_list_quote(), we'll do that; this makes it unsafe
13755 : : * to use GUC_LIST_QUOTE for extension variables.
13756 : : */
13757 [ + + ]: 40 : if (variable_is_guc_list_quote(configitem))
13758 : : {
13759 : : char **namelist;
13760 : : char **nameptr;
13761 : :
13762 : : /* Parse string into list of identifiers */
13763 : : /* this shouldn't fail really */
13764 [ + - ]: 15 : if (SplitGUCList(pos, ',', &namelist))
13765 : : {
13766 : : /* Special case: represent an empty list as NULL */
13767 [ + + ]: 15 : if (*namelist == NULL)
13768 : 5 : appendPQExpBufferStr(q, "NULL");
13769 [ + + ]: 40 : for (nameptr = namelist; *nameptr; nameptr++)
13770 : : {
13771 [ + + ]: 25 : if (nameptr != namelist)
13772 : 15 : appendPQExpBufferStr(q, ", ");
13773 : 25 : appendStringLiteralAH(q, *nameptr, fout);
13774 : : }
13775 : : }
13776 : 15 : pg_free(namelist);
13777 : : }
13778 : : else
13779 : 25 : appendStringLiteralAH(q, pos, fout);
13780 : : }
13781 : :
13782 : 1852 : appendPQExpBuffer(q, "\n %s;\n", asPart->data);
13783 : :
13784 : 1852 : append_depends_on_extension(fout, q, &finfo->dobj,
13785 : : "pg_catalog.pg_proc", keyword,
13786 : : qual_funcsig);
13787 : :
13788 [ + + ]: 1852 : if (dopt->binary_upgrade)
13789 : 309 : binary_upgrade_extension_member(q, &finfo->dobj,
13790 : : keyword, funcsig,
13791 : 309 : finfo->dobj.namespace->dobj.name);
13792 : :
13793 [ + + ]: 1852 : if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13794 : 1752 : ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
13795 [ + + ]: 1752 : ARCHIVE_OPTS(.tag = funcsig_tag,
13796 : : .namespace = finfo->dobj.namespace->dobj.name,
13797 : : .owner = finfo->rolname,
13798 : : .description = keyword,
13799 : : .section = finfo->postponed_def ?
13800 : : SECTION_POST_DATA : SECTION_PRE_DATA,
13801 : : .createStmt = q->data,
13802 : : .dropStmt = delqry->data));
13803 : :
13804 : : /* Dump Function Comments and Security Labels */
13805 [ + + ]: 1852 : if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13806 : 9 : dumpComment(fout, keyword, funcsig,
13807 : 9 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13808 : 9 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13809 : :
13810 [ - + ]: 1852 : if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
13811 : 0 : dumpSecLabel(fout, keyword, funcsig,
13812 : 0 : finfo->dobj.namespace->dobj.name, finfo->rolname,
13813 : 0 : finfo->dobj.catId, 0, finfo->dobj.dumpId);
13814 : :
13815 [ + + ]: 1852 : if (finfo->dobj.dump & DUMP_COMPONENT_ACL)
13816 : 104 : dumpACL(fout, finfo->dobj.dumpId, InvalidDumpId, keyword,
13817 : : funcsig, NULL,
13818 : 104 : finfo->dobj.namespace->dobj.name,
13819 : 104 : NULL, finfo->rolname, &finfo->dacl);
13820 : :
13821 : 1852 : PQclear(res);
13822 : :
13823 : 1852 : destroyPQExpBuffer(query);
13824 : 1852 : destroyPQExpBuffer(q);
13825 : 1852 : destroyPQExpBuffer(delqry);
13826 : 1852 : destroyPQExpBuffer(asPart);
13827 : 1852 : free(funcsig);
13828 : 1852 : free(funcfullsig);
13829 : 1852 : free(funcsig_tag);
13830 : 1852 : pfree(qual_funcsig);
13831 : 1852 : free(configitems);
13832 : : }
13833 : :
13834 : :
13835 : : /*
13836 : : * Dump a user-defined cast
13837 : : */
13838 : : static void
13839 : 69 : dumpCast(Archive *fout, const CastInfo *cast)
13840 : : {
13841 : 69 : DumpOptions *dopt = fout->dopt;
13842 : : PQExpBuffer defqry;
13843 : : PQExpBuffer delqry;
13844 : : PQExpBuffer labelq;
13845 : : PQExpBuffer castargs;
13846 : 69 : FuncInfo *funcInfo = NULL;
13847 : : const char *sourceType;
13848 : : const char *targetType;
13849 : :
13850 : : /* Do nothing if not dumping schema */
13851 [ + + ]: 69 : if (!dopt->dumpSchema)
13852 : 6 : return;
13853 : :
13854 : : /* Cannot dump if we don't have the cast function's info */
13855 [ + + ]: 63 : if (OidIsValid(cast->castfunc))
13856 : : {
13857 : 38 : funcInfo = findFuncByOid(cast->castfunc);
13858 [ - + ]: 38 : if (funcInfo == NULL)
13859 : 0 : pg_fatal("could not find function definition for function with OID %u",
13860 : : cast->castfunc);
13861 : : }
13862 : :
13863 : 63 : defqry = createPQExpBuffer();
13864 : 63 : delqry = createPQExpBuffer();
13865 : 63 : labelq = createPQExpBuffer();
13866 : 63 : castargs = createPQExpBuffer();
13867 : :
13868 : 63 : sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
13869 : 63 : targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
13870 : 63 : appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
13871 : : sourceType, targetType);
13872 : :
13873 : 63 : appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
13874 : : sourceType, targetType);
13875 : :
13876 [ + - + - ]: 63 : switch (cast->castmethod)
13877 : : {
13878 : 25 : case COERCION_METHOD_BINARY:
13879 : 25 : appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
13880 : 25 : break;
13881 : 0 : case COERCION_METHOD_INOUT:
13882 : 0 : appendPQExpBufferStr(defqry, "WITH INOUT");
13883 : 0 : break;
13884 : 38 : case COERCION_METHOD_FUNCTION:
13885 [ + - ]: 38 : if (funcInfo)
13886 : : {
13887 : 38 : char *fsig = format_function_signature(fout, funcInfo, true);
13888 : :
13889 : : /*
13890 : : * Always qualify the function name (format_function_signature
13891 : : * won't qualify it).
13892 : : */
13893 : 38 : appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
13894 : 38 : fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
13895 : 38 : free(fsig);
13896 : : }
13897 : : else
13898 : 0 : pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
13899 : 38 : break;
13900 : 0 : default:
13901 : 0 : pg_log_warning("bogus value in pg_cast.castmethod field");
13902 : : }
13903 : :
13904 [ + + ]: 63 : if (cast->castcontext == 'a')
13905 : 33 : appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
13906 [ + + ]: 30 : else if (cast->castcontext == 'i')
13907 : 10 : appendPQExpBufferStr(defqry, " AS IMPLICIT");
13908 : 63 : appendPQExpBufferStr(defqry, ";\n");
13909 : :
13910 : 63 : appendPQExpBuffer(labelq, "CAST (%s AS %s)",
13911 : : sourceType, targetType);
13912 : :
13913 : 63 : appendPQExpBuffer(castargs, "(%s AS %s)",
13914 : : sourceType, targetType);
13915 : :
13916 [ + + ]: 63 : if (dopt->binary_upgrade)
13917 : 7 : binary_upgrade_extension_member(defqry, &cast->dobj,
13918 : 7 : "CAST", castargs->data, NULL);
13919 : :
13920 [ + - ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
13921 : 63 : ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
13922 : 63 : ARCHIVE_OPTS(.tag = labelq->data,
13923 : : .description = "CAST",
13924 : : .section = SECTION_PRE_DATA,
13925 : : .createStmt = defqry->data,
13926 : : .dropStmt = delqry->data));
13927 : :
13928 : : /* Dump Cast Comments */
13929 [ - + ]: 63 : if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
13930 : 0 : dumpComment(fout, "CAST", castargs->data,
13931 : : NULL, "",
13932 : 0 : cast->dobj.catId, 0, cast->dobj.dumpId);
13933 : :
13934 : 63 : destroyPQExpBuffer(defqry);
13935 : 63 : destroyPQExpBuffer(delqry);
13936 : 63 : destroyPQExpBuffer(labelq);
13937 : 63 : destroyPQExpBuffer(castargs);
13938 : : }
13939 : :
13940 : : /*
13941 : : * Dump a transform
13942 : : */
13943 : : static void
13944 : 44 : dumpTransform(Archive *fout, const TransformInfo *transform)
13945 : : {
13946 : 44 : DumpOptions *dopt = fout->dopt;
13947 : : PQExpBuffer defqry;
13948 : : PQExpBuffer delqry;
13949 : : PQExpBuffer labelq;
13950 : : PQExpBuffer transformargs;
13951 : 44 : FuncInfo *fromsqlFuncInfo = NULL;
13952 : 44 : FuncInfo *tosqlFuncInfo = NULL;
13953 : : char *lanname;
13954 : : const char *transformType;
13955 : :
13956 : : /* Do nothing if not dumping schema */
13957 [ + + ]: 44 : if (!dopt->dumpSchema)
13958 : 6 : return;
13959 : :
13960 : : /* Cannot dump if we don't have the transform functions' info */
13961 [ + - ]: 38 : if (OidIsValid(transform->trffromsql))
13962 : : {
13963 : 38 : fromsqlFuncInfo = findFuncByOid(transform->trffromsql);
13964 [ - + ]: 38 : if (fromsqlFuncInfo == NULL)
13965 : 0 : pg_fatal("could not find function definition for function with OID %u",
13966 : : transform->trffromsql);
13967 : : }
13968 [ + - ]: 38 : if (OidIsValid(transform->trftosql))
13969 : : {
13970 : 38 : tosqlFuncInfo = findFuncByOid(transform->trftosql);
13971 [ - + ]: 38 : if (tosqlFuncInfo == NULL)
13972 : 0 : pg_fatal("could not find function definition for function with OID %u",
13973 : : transform->trftosql);
13974 : : }
13975 : :
13976 : 38 : defqry = createPQExpBuffer();
13977 : 38 : delqry = createPQExpBuffer();
13978 : 38 : labelq = createPQExpBuffer();
13979 : 38 : transformargs = createPQExpBuffer();
13980 : :
13981 : 38 : lanname = get_language_name(fout, transform->trflang);
13982 : 38 : transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone);
13983 : :
13984 : 38 : appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n",
13985 : : transformType, lanname);
13986 : :
13987 : 38 : appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (",
13988 : : transformType, lanname);
13989 : :
13990 [ - + - - ]: 38 : if (!transform->trffromsql && !transform->trftosql)
13991 : 0 : pg_log_warning("bogus transform definition, at least one of trffromsql and trftosql should be nonzero");
13992 : :
13993 [ + - ]: 38 : if (transform->trffromsql)
13994 : : {
13995 [ + - ]: 38 : if (fromsqlFuncInfo)
13996 : : {
13997 : 38 : char *fsig = format_function_signature(fout, fromsqlFuncInfo, true);
13998 : :
13999 : : /*
14000 : : * Always qualify the function name (format_function_signature
14001 : : * won't qualify it).
14002 : : */
14003 : 38 : appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s",
14004 : 38 : fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig);
14005 : 38 : free(fsig);
14006 : : }
14007 : : else
14008 : 0 : pg_log_warning("bogus value in pg_transform.trffromsql field");
14009 : : }
14010 : :
14011 [ + - ]: 38 : if (transform->trftosql)
14012 : : {
14013 [ + - ]: 38 : if (transform->trffromsql)
14014 : 38 : appendPQExpBufferStr(defqry, ", ");
14015 : :
14016 [ + - ]: 38 : if (tosqlFuncInfo)
14017 : : {
14018 : 38 : char *fsig = format_function_signature(fout, tosqlFuncInfo, true);
14019 : :
14020 : : /*
14021 : : * Always qualify the function name (format_function_signature
14022 : : * won't qualify it).
14023 : : */
14024 : 38 : appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s",
14025 : 38 : fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig);
14026 : 38 : free(fsig);
14027 : : }
14028 : : else
14029 : 0 : pg_log_warning("bogus value in pg_transform.trftosql field");
14030 : : }
14031 : :
14032 : 38 : appendPQExpBufferStr(defqry, ");\n");
14033 : :
14034 : 38 : appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s",
14035 : : transformType, lanname);
14036 : :
14037 : 38 : appendPQExpBuffer(transformargs, "FOR %s LANGUAGE %s",
14038 : : transformType, lanname);
14039 : :
14040 [ + + ]: 38 : if (dopt->binary_upgrade)
14041 : 2 : binary_upgrade_extension_member(defqry, &transform->dobj,
14042 : 2 : "TRANSFORM", transformargs->data, NULL);
14043 : :
14044 [ + - ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION)
14045 : 38 : ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId,
14046 : 38 : ARCHIVE_OPTS(.tag = labelq->data,
14047 : : .description = "TRANSFORM",
14048 : : .section = SECTION_PRE_DATA,
14049 : : .createStmt = defqry->data,
14050 : : .dropStmt = delqry->data,
14051 : : .deps = transform->dobj.dependencies,
14052 : : .nDeps = transform->dobj.nDeps));
14053 : :
14054 : : /* Dump Transform Comments */
14055 [ - + ]: 38 : if (transform->dobj.dump & DUMP_COMPONENT_COMMENT)
14056 : 0 : dumpComment(fout, "TRANSFORM", transformargs->data,
14057 : : NULL, "",
14058 : 0 : transform->dobj.catId, 0, transform->dobj.dumpId);
14059 : :
14060 : 38 : free(lanname);
14061 : 38 : destroyPQExpBuffer(defqry);
14062 : 38 : destroyPQExpBuffer(delqry);
14063 : 38 : destroyPQExpBuffer(labelq);
14064 : 38 : destroyPQExpBuffer(transformargs);
14065 : : }
14066 : :
14067 : :
14068 : : /*
14069 : : * dumpOpr
14070 : : * write out a single operator definition
14071 : : */
14072 : : static void
14073 : 2525 : dumpOpr(Archive *fout, const OprInfo *oprinfo)
14074 : : {
14075 : 2525 : DumpOptions *dopt = fout->dopt;
14076 : : PQExpBuffer query;
14077 : : PQExpBuffer q;
14078 : : PQExpBuffer delq;
14079 : : PQExpBuffer oprid;
14080 : : PQExpBuffer details;
14081 : : PGresult *res;
14082 : : int i_oprkind;
14083 : : int i_oprcode;
14084 : : int i_oprleft;
14085 : : int i_oprright;
14086 : : int i_oprcom;
14087 : : int i_oprnegate;
14088 : : int i_oprrest;
14089 : : int i_oprjoin;
14090 : : int i_oprcanmerge;
14091 : : int i_oprcanhash;
14092 : : char *oprkind;
14093 : : char *oprcode;
14094 : : char *oprleft;
14095 : : char *oprright;
14096 : : char *oprcom;
14097 : : char *oprnegate;
14098 : : char *oprrest;
14099 : : char *oprjoin;
14100 : : char *oprcanmerge;
14101 : : char *oprcanhash;
14102 : : char *oprregproc;
14103 : : char *oprref;
14104 : :
14105 : : /* Do nothing if not dumping schema */
14106 [ + + ]: 2525 : if (!dopt->dumpSchema)
14107 : 7 : return;
14108 : :
14109 : : /*
14110 : : * some operators are invalid because they were the result of user
14111 : : * defining operators before commutators exist
14112 : : */
14113 [ + + ]: 2518 : if (!OidIsValid(oprinfo->oprcode))
14114 : 14 : return;
14115 : :
14116 : 2504 : query = createPQExpBuffer();
14117 : 2504 : q = createPQExpBuffer();
14118 : 2504 : delq = createPQExpBuffer();
14119 : 2504 : oprid = createPQExpBuffer();
14120 : 2504 : details = createPQExpBuffer();
14121 : :
14122 [ + + ]: 2504 : if (!fout->is_prepared[PREPQUERY_DUMPOPR])
14123 : : {
14124 : : /* Set up query for operator-specific details */
14125 : 42 : appendPQExpBufferStr(query,
14126 : : "PREPARE dumpOpr(pg_catalog.oid) AS\n"
14127 : : "SELECT oprkind, "
14128 : : "oprcode::pg_catalog.regprocedure, "
14129 : : "oprleft::pg_catalog.regtype, "
14130 : : "oprright::pg_catalog.regtype, "
14131 : : "oprcom, "
14132 : : "oprnegate, "
14133 : : "oprrest::pg_catalog.regprocedure, "
14134 : : "oprjoin::pg_catalog.regprocedure, "
14135 : : "oprcanmerge, oprcanhash "
14136 : : "FROM pg_catalog.pg_operator "
14137 : : "WHERE oid = $1");
14138 : :
14139 : 42 : ExecuteSqlStatement(fout, query->data);
14140 : :
14141 : 42 : fout->is_prepared[PREPQUERY_DUMPOPR] = true;
14142 : : }
14143 : :
14144 : 2504 : printfPQExpBuffer(query,
14145 : : "EXECUTE dumpOpr('%u')",
14146 : 2504 : oprinfo->dobj.catId.oid);
14147 : :
14148 : 2504 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14149 : :
14150 : 2504 : i_oprkind = PQfnumber(res, "oprkind");
14151 : 2504 : i_oprcode = PQfnumber(res, "oprcode");
14152 : 2504 : i_oprleft = PQfnumber(res, "oprleft");
14153 : 2504 : i_oprright = PQfnumber(res, "oprright");
14154 : 2504 : i_oprcom = PQfnumber(res, "oprcom");
14155 : 2504 : i_oprnegate = PQfnumber(res, "oprnegate");
14156 : 2504 : i_oprrest = PQfnumber(res, "oprrest");
14157 : 2504 : i_oprjoin = PQfnumber(res, "oprjoin");
14158 : 2504 : i_oprcanmerge = PQfnumber(res, "oprcanmerge");
14159 : 2504 : i_oprcanhash = PQfnumber(res, "oprcanhash");
14160 : :
14161 : 2504 : oprkind = PQgetvalue(res, 0, i_oprkind);
14162 : 2504 : oprcode = PQgetvalue(res, 0, i_oprcode);
14163 : 2504 : oprleft = PQgetvalue(res, 0, i_oprleft);
14164 : 2504 : oprright = PQgetvalue(res, 0, i_oprright);
14165 : 2504 : oprcom = PQgetvalue(res, 0, i_oprcom);
14166 : 2504 : oprnegate = PQgetvalue(res, 0, i_oprnegate);
14167 : 2504 : oprrest = PQgetvalue(res, 0, i_oprrest);
14168 : 2504 : oprjoin = PQgetvalue(res, 0, i_oprjoin);
14169 : 2504 : oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
14170 : 2504 : oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
14171 : :
14172 : : /* In PG14 upwards postfix operator support does not exist anymore. */
14173 [ - + ]: 2504 : if (strcmp(oprkind, "r") == 0)
14174 : 0 : pg_log_warning("postfix operators are not supported anymore (operator \"%s\")",
14175 : : oprcode);
14176 : :
14177 : 2504 : oprregproc = convertRegProcReference(oprcode);
14178 [ + - ]: 2504 : if (oprregproc)
14179 : : {
14180 : 2504 : appendPQExpBuffer(details, " FUNCTION = %s", oprregproc);
14181 : 2504 : free(oprregproc);
14182 : : }
14183 : :
14184 : 2504 : appendPQExpBuffer(oprid, "%s (",
14185 : 2504 : oprinfo->dobj.name);
14186 : :
14187 : : /*
14188 : : * right unary means there's a left arg and left unary means there's a
14189 : : * right arg. (Although the "r" case is dead code for PG14 and later,
14190 : : * continue to support it in case we're dumping from an old server.)
14191 : : */
14192 [ + - ]: 2504 : if (strcmp(oprkind, "r") == 0 ||
14193 [ + + ]: 2504 : strcmp(oprkind, "b") == 0)
14194 : : {
14195 : 2361 : appendPQExpBuffer(details, ",\n LEFTARG = %s", oprleft);
14196 : 2361 : appendPQExpBufferStr(oprid, oprleft);
14197 : : }
14198 : : else
14199 : 143 : appendPQExpBufferStr(oprid, "NONE");
14200 : :
14201 [ + + ]: 2504 : if (strcmp(oprkind, "l") == 0 ||
14202 [ + - ]: 2361 : strcmp(oprkind, "b") == 0)
14203 : : {
14204 : 2504 : appendPQExpBuffer(details, ",\n RIGHTARG = %s", oprright);
14205 : 2504 : appendPQExpBuffer(oprid, ", %s)", oprright);
14206 : : }
14207 : : else
14208 : 0 : appendPQExpBufferStr(oprid, ", NONE)");
14209 : :
14210 : 2504 : oprref = getFormattedOperatorName(oprcom);
14211 [ + + ]: 2504 : if (oprref)
14212 : : {
14213 : 1679 : appendPQExpBuffer(details, ",\n COMMUTATOR = %s", oprref);
14214 : 1679 : free(oprref);
14215 : : }
14216 : :
14217 : 2504 : oprref = getFormattedOperatorName(oprnegate);
14218 [ + + ]: 2504 : if (oprref)
14219 : : {
14220 : 1181 : appendPQExpBuffer(details, ",\n NEGATOR = %s", oprref);
14221 : 1181 : free(oprref);
14222 : : }
14223 : :
14224 [ + + ]: 2504 : if (strcmp(oprcanmerge, "t") == 0)
14225 : 188 : appendPQExpBufferStr(details, ",\n MERGES");
14226 : :
14227 [ + + ]: 2504 : if (strcmp(oprcanhash, "t") == 0)
14228 : 141 : appendPQExpBufferStr(details, ",\n HASHES");
14229 : :
14230 : 2504 : oprregproc = convertRegProcReference(oprrest);
14231 [ + + ]: 2504 : if (oprregproc)
14232 : : {
14233 : 1532 : appendPQExpBuffer(details, ",\n RESTRICT = %s", oprregproc);
14234 : 1532 : free(oprregproc);
14235 : : }
14236 : :
14237 : 2504 : oprregproc = convertRegProcReference(oprjoin);
14238 [ + + ]: 2504 : if (oprregproc)
14239 : : {
14240 : 1532 : appendPQExpBuffer(details, ",\n JOIN = %s", oprregproc);
14241 : 1532 : free(oprregproc);
14242 : : }
14243 : :
14244 : 2504 : appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
14245 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14246 : : oprid->data);
14247 : :
14248 : 2504 : appendPQExpBuffer(q, "CREATE OPERATOR %s.%s (\n%s\n);\n",
14249 : 2504 : fmtId(oprinfo->dobj.namespace->dobj.name),
14250 : 2504 : oprinfo->dobj.name, details->data);
14251 : :
14252 [ + + ]: 2504 : if (dopt->binary_upgrade)
14253 : 12 : binary_upgrade_extension_member(q, &oprinfo->dobj,
14254 : 12 : "OPERATOR", oprid->data,
14255 : 12 : oprinfo->dobj.namespace->dobj.name);
14256 : :
14257 [ + - ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14258 : 2504 : ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
14259 : 2504 : ARCHIVE_OPTS(.tag = oprinfo->dobj.name,
14260 : : .namespace = oprinfo->dobj.namespace->dobj.name,
14261 : : .owner = oprinfo->rolname,
14262 : : .description = "OPERATOR",
14263 : : .section = SECTION_PRE_DATA,
14264 : : .createStmt = q->data,
14265 : : .dropStmt = delq->data));
14266 : :
14267 : : /* Dump Operator Comments */
14268 [ + + ]: 2504 : if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14269 : 2415 : dumpComment(fout, "OPERATOR", oprid->data,
14270 : 2415 : oprinfo->dobj.namespace->dobj.name, oprinfo->rolname,
14271 : 2415 : oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
14272 : :
14273 : 2504 : PQclear(res);
14274 : :
14275 : 2504 : destroyPQExpBuffer(query);
14276 : 2504 : destroyPQExpBuffer(q);
14277 : 2504 : destroyPQExpBuffer(delq);
14278 : 2504 : destroyPQExpBuffer(oprid);
14279 : 2504 : destroyPQExpBuffer(details);
14280 : : }
14281 : :
14282 : : /*
14283 : : * Convert a function reference obtained from pg_operator
14284 : : *
14285 : : * Returns allocated string of what to print, or NULL if function references
14286 : : * is InvalidOid. Returned string is expected to be free'd by the caller.
14287 : : *
14288 : : * The input is a REGPROCEDURE display; we have to strip the argument-types
14289 : : * part.
14290 : : */
14291 : : static char *
14292 : 7512 : convertRegProcReference(const char *proc)
14293 : : {
14294 : : char *name;
14295 : : char *paren;
14296 : : bool inquote;
14297 : :
14298 : : /* In all cases "-" means a null reference */
14299 [ + + ]: 7512 : if (strcmp(proc, "-") == 0)
14300 : 1944 : return NULL;
14301 : :
14302 : 5568 : name = pg_strdup(proc);
14303 : : /* find non-double-quoted left paren */
14304 : 5568 : inquote = false;
14305 [ + - ]: 67010 : for (paren = name; *paren; paren++)
14306 : : {
14307 [ + + + - ]: 67010 : if (*paren == '(' && !inquote)
14308 : : {
14309 : 5568 : *paren = '\0';
14310 : 5568 : break;
14311 : : }
14312 [ + + ]: 61442 : if (*paren == '"')
14313 : 50 : inquote = !inquote;
14314 : : }
14315 : 5568 : return name;
14316 : : }
14317 : :
14318 : : /*
14319 : : * getFormattedOperatorName - retrieve the operator name for the
14320 : : * given operator OID (presented in string form).
14321 : : *
14322 : : * Returns an allocated string, or NULL if the given OID is invalid.
14323 : : * Caller is responsible for free'ing result string.
14324 : : *
14325 : : * What we produce has the format "OPERATOR(schema.oprname)". This is only
14326 : : * useful in commands where the operator's argument types can be inferred from
14327 : : * context. We always schema-qualify the name, though. The predecessor to
14328 : : * this code tried to skip the schema qualification if possible, but that led
14329 : : * to wrong results in corner cases, such as if an operator and its negator
14330 : : * are in different schemas.
14331 : : */
14332 : : static char *
14333 : 5295 : getFormattedOperatorName(const char *oproid)
14334 : : {
14335 : : OprInfo *oprInfo;
14336 : :
14337 : : /* In all cases "0" means a null reference */
14338 [ + + ]: 5295 : if (strcmp(oproid, "0") == 0)
14339 : 2435 : return NULL;
14340 : :
14341 : 2860 : oprInfo = findOprByOid(atooid(oproid));
14342 [ - + ]: 2860 : if (oprInfo == NULL)
14343 : : {
14344 : 0 : pg_log_warning("could not find operator with OID %s",
14345 : : oproid);
14346 : 0 : return NULL;
14347 : : }
14348 : :
14349 : 2860 : return psprintf("OPERATOR(%s.%s)",
14350 : 2860 : fmtId(oprInfo->dobj.namespace->dobj.name),
14351 : : oprInfo->dobj.name);
14352 : : }
14353 : :
14354 : : /*
14355 : : * Convert a function OID obtained from pg_ts_parser or pg_ts_template
14356 : : *
14357 : : * It is sufficient to use REGPROC rather than REGPROCEDURE, since the
14358 : : * argument lists of these functions are predetermined. Note that the
14359 : : * caller should ensure we are in the proper schema, because the results
14360 : : * are search path dependent!
14361 : : */
14362 : : static char *
14363 : 215 : convertTSFunction(Archive *fout, Oid funcOid)
14364 : : {
14365 : : char *result;
14366 : : char query[128];
14367 : : PGresult *res;
14368 : :
14369 : 215 : snprintf(query, sizeof(query),
14370 : : "SELECT '%u'::pg_catalog.regproc", funcOid);
14371 : 215 : res = ExecuteSqlQueryForSingleRow(fout, query);
14372 : :
14373 : 215 : result = pg_strdup(PQgetvalue(res, 0, 0));
14374 : :
14375 : 215 : PQclear(res);
14376 : :
14377 : 215 : return result;
14378 : : }
14379 : :
14380 : : /*
14381 : : * dumpAccessMethod
14382 : : * write out a single access method definition
14383 : : */
14384 : : static void
14385 : 84 : dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo)
14386 : : {
14387 : 84 : DumpOptions *dopt = fout->dopt;
14388 : : PQExpBuffer q;
14389 : : PQExpBuffer delq;
14390 : : char *qamname;
14391 : :
14392 : : /* Do nothing if not dumping schema */
14393 [ + + ]: 84 : if (!dopt->dumpSchema)
14394 : 12 : return;
14395 : :
14396 : 72 : q = createPQExpBuffer();
14397 : 72 : delq = createPQExpBuffer();
14398 : :
14399 : 72 : qamname = pg_strdup(fmtId(aminfo->dobj.name));
14400 : :
14401 : 72 : appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
14402 : :
14403 [ + + - ]: 72 : switch (aminfo->amtype)
14404 : : {
14405 : 34 : case AMTYPE_INDEX:
14406 : 34 : appendPQExpBufferStr(q, "TYPE INDEX ");
14407 : 34 : break;
14408 : 38 : case AMTYPE_TABLE:
14409 : 38 : appendPQExpBufferStr(q, "TYPE TABLE ");
14410 : 38 : break;
14411 : 0 : default:
14412 : 0 : pg_log_warning("invalid type \"%c\" of access method \"%s\"",
14413 : : aminfo->amtype, qamname);
14414 : 0 : destroyPQExpBuffer(q);
14415 : 0 : destroyPQExpBuffer(delq);
14416 : 0 : pg_free(qamname);
14417 : 0 : return;
14418 : : }
14419 : :
14420 : 72 : appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
14421 : :
14422 : 72 : appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
14423 : : qamname);
14424 : :
14425 [ + + ]: 72 : if (dopt->binary_upgrade)
14426 : 4 : binary_upgrade_extension_member(q, &aminfo->dobj,
14427 : : "ACCESS METHOD", qamname, NULL);
14428 : :
14429 [ + - ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14430 : 72 : ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
14431 : 72 : ARCHIVE_OPTS(.tag = aminfo->dobj.name,
14432 : : .description = "ACCESS METHOD",
14433 : : .section = SECTION_PRE_DATA,
14434 : : .createStmt = q->data,
14435 : : .dropStmt = delq->data));
14436 : :
14437 : : /* Dump Access Method Comments */
14438 [ - + ]: 72 : if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14439 : 0 : dumpComment(fout, "ACCESS METHOD", qamname,
14440 : : NULL, "",
14441 : 0 : aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
14442 : :
14443 : 72 : destroyPQExpBuffer(q);
14444 : 72 : destroyPQExpBuffer(delq);
14445 : 72 : pg_free(qamname);
14446 : : }
14447 : :
14448 : : /*
14449 : : * dumpOpclass
14450 : : * write out a single operator class definition
14451 : : */
14452 : : static void
14453 : 675 : dumpOpclass(Archive *fout, const OpclassInfo *opcinfo)
14454 : : {
14455 : 675 : DumpOptions *dopt = fout->dopt;
14456 : : PQExpBuffer query;
14457 : : PQExpBuffer q;
14458 : : PQExpBuffer delq;
14459 : : PQExpBuffer nameusing;
14460 : : PGresult *res;
14461 : : int ntups;
14462 : : int i_opcintype;
14463 : : int i_opckeytype;
14464 : : int i_opcdefault;
14465 : : int i_opcfamily;
14466 : : int i_opcfamilyname;
14467 : : int i_opcfamilynsp;
14468 : : int i_amname;
14469 : : int i_amopstrategy;
14470 : : int i_amopopr;
14471 : : int i_sortfamily;
14472 : : int i_sortfamilynsp;
14473 : : int i_amprocnum;
14474 : : int i_amproc;
14475 : : int i_amproclefttype;
14476 : : int i_amprocrighttype;
14477 : : char *opcintype;
14478 : : char *opckeytype;
14479 : : char *opcdefault;
14480 : : char *opcfamily;
14481 : : char *opcfamilyname;
14482 : : char *opcfamilynsp;
14483 : : char *amname;
14484 : : char *amopstrategy;
14485 : : char *amopopr;
14486 : : char *sortfamily;
14487 : : char *sortfamilynsp;
14488 : : char *amprocnum;
14489 : : char *amproc;
14490 : : char *amproclefttype;
14491 : : char *amprocrighttype;
14492 : : bool needComma;
14493 : : int i;
14494 : :
14495 : : /* Do nothing if not dumping schema */
14496 [ + + ]: 675 : if (!dopt->dumpSchema)
14497 : 21 : return;
14498 : :
14499 : 654 : query = createPQExpBuffer();
14500 : 654 : q = createPQExpBuffer();
14501 : 654 : delq = createPQExpBuffer();
14502 : 654 : nameusing = createPQExpBuffer();
14503 : :
14504 : : /* Get additional fields from the pg_opclass row */
14505 : 654 : appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
14506 : : "opckeytype::pg_catalog.regtype, "
14507 : : "opcdefault, opcfamily, "
14508 : : "opfname AS opcfamilyname, "
14509 : : "nspname AS opcfamilynsp, "
14510 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
14511 : : "FROM pg_catalog.pg_opclass c "
14512 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
14513 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14514 : : "WHERE c.oid = '%u'::pg_catalog.oid",
14515 : 654 : opcinfo->dobj.catId.oid);
14516 : :
14517 : 654 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14518 : :
14519 : 654 : i_opcintype = PQfnumber(res, "opcintype");
14520 : 654 : i_opckeytype = PQfnumber(res, "opckeytype");
14521 : 654 : i_opcdefault = PQfnumber(res, "opcdefault");
14522 : 654 : i_opcfamily = PQfnumber(res, "opcfamily");
14523 : 654 : i_opcfamilyname = PQfnumber(res, "opcfamilyname");
14524 : 654 : i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
14525 : 654 : i_amname = PQfnumber(res, "amname");
14526 : :
14527 : : /* opcintype may still be needed after we PQclear res */
14528 : 654 : opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
14529 : 654 : opckeytype = PQgetvalue(res, 0, i_opckeytype);
14530 : 654 : opcdefault = PQgetvalue(res, 0, i_opcdefault);
14531 : : /* opcfamily will still be needed after we PQclear res */
14532 : 654 : opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily));
14533 : 654 : opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname);
14534 : 654 : opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
14535 : : /* amname will still be needed after we PQclear res */
14536 : 654 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14537 : :
14538 : 654 : appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
14539 : 654 : fmtQualifiedDumpable(opcinfo));
14540 : 654 : appendPQExpBuffer(delq, " USING %s;\n",
14541 : : fmtId(amname));
14542 : :
14543 : : /* Build the fixed portion of the CREATE command */
14544 : 654 : appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ",
14545 : 654 : fmtQualifiedDumpable(opcinfo));
14546 [ + + ]: 654 : if (strcmp(opcdefault, "t") == 0)
14547 : 366 : appendPQExpBufferStr(q, "DEFAULT ");
14548 : 654 : appendPQExpBuffer(q, "FOR TYPE %s USING %s",
14549 : : opcintype,
14550 : : fmtId(amname));
14551 [ + - ]: 654 : if (strlen(opcfamilyname) > 0)
14552 : : {
14553 : 654 : appendPQExpBufferStr(q, " FAMILY ");
14554 : 654 : appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
14555 : 654 : appendPQExpBufferStr(q, fmtId(opcfamilyname));
14556 : : }
14557 : 654 : appendPQExpBufferStr(q, " AS\n ");
14558 : :
14559 : 654 : needComma = false;
14560 : :
14561 [ + + ]: 654 : if (strcmp(opckeytype, "-") != 0)
14562 : : {
14563 : 252 : appendPQExpBuffer(q, "STORAGE %s",
14564 : : opckeytype);
14565 : 252 : needComma = true;
14566 : : }
14567 : :
14568 : 654 : PQclear(res);
14569 : :
14570 : : /*
14571 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
14572 : : *
14573 : : * Print only those opfamily members that are tied to the opclass by
14574 : : * pg_depend entries.
14575 : : */
14576 : 654 : resetPQExpBuffer(query);
14577 : 654 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14578 : : "amopopr::pg_catalog.regoperator, "
14579 : : "opfname AS sortfamily, "
14580 : : "nspname AS sortfamilynsp "
14581 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14582 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14583 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14584 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14585 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14586 : : "AND refobjid = '%u'::pg_catalog.oid "
14587 : : "AND amopfamily = '%s'::pg_catalog.oid "
14588 : : "ORDER BY amopstrategy",
14589 : 654 : opcinfo->dobj.catId.oid,
14590 : : opcfamily);
14591 : :
14592 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14593 : :
14594 : 654 : ntups = PQntuples(res);
14595 : :
14596 : 654 : i_amopstrategy = PQfnumber(res, "amopstrategy");
14597 : 654 : i_amopopr = PQfnumber(res, "amopopr");
14598 : 654 : i_sortfamily = PQfnumber(res, "sortfamily");
14599 : 654 : i_sortfamilynsp = PQfnumber(res, "sortfamilynsp");
14600 : :
14601 [ + + ]: 868 : for (i = 0; i < ntups; i++)
14602 : : {
14603 : 214 : amopstrategy = PQgetvalue(res, i, i_amopstrategy);
14604 : 214 : amopopr = PQgetvalue(res, i, i_amopopr);
14605 : 214 : sortfamily = PQgetvalue(res, i, i_sortfamily);
14606 : 214 : sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp);
14607 : :
14608 [ + + ]: 214 : if (needComma)
14609 : 136 : appendPQExpBufferStr(q, " ,\n ");
14610 : :
14611 : 214 : appendPQExpBuffer(q, "OPERATOR %s %s",
14612 : : amopstrategy, amopopr);
14613 : :
14614 [ - + ]: 214 : if (strlen(sortfamily) > 0)
14615 : : {
14616 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
14617 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
14618 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
14619 : : }
14620 : :
14621 : 214 : needComma = true;
14622 : : }
14623 : :
14624 : 654 : PQclear(res);
14625 : :
14626 : : /*
14627 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
14628 : : *
14629 : : * Print only those opfamily members that are tied to the opclass by
14630 : : * pg_depend entries.
14631 : : *
14632 : : * We print the amproclefttype/amprocrighttype even though in most cases
14633 : : * the backend could deduce the right values, because of the corner case
14634 : : * of a btree sort support function for a cross-type comparison.
14635 : : */
14636 : 654 : resetPQExpBuffer(query);
14637 : :
14638 : 654 : appendPQExpBuffer(query, "SELECT amprocnum, "
14639 : : "amproc::pg_catalog.regprocedure, "
14640 : : "amproclefttype::pg_catalog.regtype, "
14641 : : "amprocrighttype::pg_catalog.regtype "
14642 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14643 : : "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
14644 : : "AND refobjid = '%u'::pg_catalog.oid "
14645 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14646 : : "AND objid = ap.oid "
14647 : : "ORDER BY amprocnum",
14648 : 654 : opcinfo->dobj.catId.oid);
14649 : :
14650 : 654 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14651 : :
14652 : 654 : ntups = PQntuples(res);
14653 : :
14654 : 654 : i_amprocnum = PQfnumber(res, "amprocnum");
14655 : 654 : i_amproc = PQfnumber(res, "amproc");
14656 : 654 : i_amproclefttype = PQfnumber(res, "amproclefttype");
14657 : 654 : i_amprocrighttype = PQfnumber(res, "amprocrighttype");
14658 : :
14659 [ + + ]: 688 : for (i = 0; i < ntups; i++)
14660 : : {
14661 : 34 : amprocnum = PQgetvalue(res, i, i_amprocnum);
14662 : 34 : amproc = PQgetvalue(res, i, i_amproc);
14663 : 34 : amproclefttype = PQgetvalue(res, i, i_amproclefttype);
14664 : 34 : amprocrighttype = PQgetvalue(res, i, i_amprocrighttype);
14665 : :
14666 [ + - ]: 34 : if (needComma)
14667 : 34 : appendPQExpBufferStr(q, " ,\n ");
14668 : :
14669 : 34 : appendPQExpBuffer(q, "FUNCTION %s", amprocnum);
14670 : :
14671 [ + - + - ]: 34 : if (*amproclefttype && *amprocrighttype)
14672 : 34 : appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype);
14673 : :
14674 : 34 : appendPQExpBuffer(q, " %s", amproc);
14675 : :
14676 : 34 : needComma = true;
14677 : : }
14678 : :
14679 : 654 : PQclear(res);
14680 : :
14681 : : /*
14682 : : * If needComma is still false it means we haven't added anything after
14683 : : * the AS keyword. To avoid printing broken SQL, append a dummy STORAGE
14684 : : * clause with the same datatype. This isn't sanctioned by the
14685 : : * documentation, but actually DefineOpClass will treat it as a no-op.
14686 : : */
14687 [ + + ]: 654 : if (!needComma)
14688 : 324 : appendPQExpBuffer(q, "STORAGE %s", opcintype);
14689 : :
14690 : 654 : appendPQExpBufferStr(q, ";\n");
14691 : :
14692 : 654 : appendPQExpBufferStr(nameusing, fmtId(opcinfo->dobj.name));
14693 : 654 : appendPQExpBuffer(nameusing, " USING %s",
14694 : : fmtId(amname));
14695 : :
14696 [ + + ]: 654 : if (dopt->binary_upgrade)
14697 : 6 : binary_upgrade_extension_member(q, &opcinfo->dobj,
14698 : 6 : "OPERATOR CLASS", nameusing->data,
14699 : 6 : opcinfo->dobj.namespace->dobj.name);
14700 : :
14701 [ + - ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14702 : 654 : ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
14703 : 654 : ARCHIVE_OPTS(.tag = opcinfo->dobj.name,
14704 : : .namespace = opcinfo->dobj.namespace->dobj.name,
14705 : : .owner = opcinfo->rolname,
14706 : : .description = "OPERATOR CLASS",
14707 : : .section = SECTION_PRE_DATA,
14708 : : .createStmt = q->data,
14709 : : .dropStmt = delq->data));
14710 : :
14711 : : /* Dump Operator Class Comments */
14712 [ - + ]: 654 : if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14713 : 0 : dumpComment(fout, "OPERATOR CLASS", nameusing->data,
14714 : 0 : opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
14715 : 0 : opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
14716 : :
14717 : 654 : pg_free(opcintype);
14718 : 654 : pg_free(opcfamily);
14719 : 654 : pg_free(amname);
14720 : 654 : destroyPQExpBuffer(query);
14721 : 654 : destroyPQExpBuffer(q);
14722 : 654 : destroyPQExpBuffer(delq);
14723 : 654 : destroyPQExpBuffer(nameusing);
14724 : : }
14725 : :
14726 : : /*
14727 : : * dumpOpfamily
14728 : : * write out a single operator family definition
14729 : : *
14730 : : * Note: this also dumps any "loose" operator members that aren't bound to a
14731 : : * specific opclass within the opfamily.
14732 : : */
14733 : : static void
14734 : 561 : dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo)
14735 : : {
14736 : 561 : DumpOptions *dopt = fout->dopt;
14737 : : PQExpBuffer query;
14738 : : PQExpBuffer q;
14739 : : PQExpBuffer delq;
14740 : : PQExpBuffer nameusing;
14741 : : PGresult *res;
14742 : : PGresult *res_ops;
14743 : : PGresult *res_procs;
14744 : : int ntups;
14745 : : int i_amname;
14746 : : int i_amopstrategy;
14747 : : int i_amopopr;
14748 : : int i_sortfamily;
14749 : : int i_sortfamilynsp;
14750 : : int i_amprocnum;
14751 : : int i_amproc;
14752 : : int i_amproclefttype;
14753 : : int i_amprocrighttype;
14754 : : char *amname;
14755 : : char *amopstrategy;
14756 : : char *amopopr;
14757 : : char *sortfamily;
14758 : : char *sortfamilynsp;
14759 : : char *amprocnum;
14760 : : char *amproc;
14761 : : char *amproclefttype;
14762 : : char *amprocrighttype;
14763 : : bool needComma;
14764 : : int i;
14765 : :
14766 : : /* Do nothing if not dumping schema */
14767 [ + + ]: 561 : if (!dopt->dumpSchema)
14768 : 14 : return;
14769 : :
14770 : 547 : query = createPQExpBuffer();
14771 : 547 : q = createPQExpBuffer();
14772 : 547 : delq = createPQExpBuffer();
14773 : 547 : nameusing = createPQExpBuffer();
14774 : :
14775 : : /*
14776 : : * Fetch only those opfamily members that are tied directly to the
14777 : : * opfamily by pg_depend entries.
14778 : : */
14779 : 547 : appendPQExpBuffer(query, "SELECT amopstrategy, "
14780 : : "amopopr::pg_catalog.regoperator, "
14781 : : "opfname AS sortfamily, "
14782 : : "nspname AS sortfamilynsp "
14783 : : "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
14784 : : "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
14785 : : "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
14786 : : "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
14787 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14788 : : "AND refobjid = '%u'::pg_catalog.oid "
14789 : : "AND amopfamily = '%u'::pg_catalog.oid "
14790 : : "ORDER BY amopstrategy",
14791 : 547 : opfinfo->dobj.catId.oid,
14792 : 547 : opfinfo->dobj.catId.oid);
14793 : :
14794 : 547 : res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14795 : :
14796 : 547 : resetPQExpBuffer(query);
14797 : :
14798 : 547 : appendPQExpBuffer(query, "SELECT amprocnum, "
14799 : : "amproc::pg_catalog.regprocedure, "
14800 : : "amproclefttype::pg_catalog.regtype, "
14801 : : "amprocrighttype::pg_catalog.regtype "
14802 : : "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
14803 : : "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
14804 : : "AND refobjid = '%u'::pg_catalog.oid "
14805 : : "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
14806 : : "AND objid = ap.oid "
14807 : : "ORDER BY amprocnum",
14808 : 547 : opfinfo->dobj.catId.oid);
14809 : :
14810 : 547 : res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14811 : :
14812 : : /* Get additional fields from the pg_opfamily row */
14813 : 547 : resetPQExpBuffer(query);
14814 : :
14815 : 547 : appendPQExpBuffer(query, "SELECT "
14816 : : "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
14817 : : "FROM pg_catalog.pg_opfamily "
14818 : : "WHERE oid = '%u'::pg_catalog.oid",
14819 : 547 : opfinfo->dobj.catId.oid);
14820 : :
14821 : 547 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
14822 : :
14823 : 547 : i_amname = PQfnumber(res, "amname");
14824 : :
14825 : : /* amname will still be needed after we PQclear res */
14826 : 547 : amname = pg_strdup(PQgetvalue(res, 0, i_amname));
14827 : :
14828 : 547 : appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
14829 : 547 : fmtQualifiedDumpable(opfinfo));
14830 : 547 : appendPQExpBuffer(delq, " USING %s;\n",
14831 : : fmtId(amname));
14832 : :
14833 : : /* Build the fixed portion of the CREATE command */
14834 : 547 : appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
14835 : 547 : fmtQualifiedDumpable(opfinfo));
14836 : 547 : appendPQExpBuffer(q, " USING %s;\n",
14837 : : fmtId(amname));
14838 : :
14839 : 547 : PQclear(res);
14840 : :
14841 : : /* Do we need an ALTER to add loose members? */
14842 [ + + + + ]: 547 : if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
14843 : : {
14844 : 49 : appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
14845 : 49 : fmtQualifiedDumpable(opfinfo));
14846 : 49 : appendPQExpBuffer(q, " USING %s ADD\n ",
14847 : : fmtId(amname));
14848 : :
14849 : 49 : needComma = false;
14850 : :
14851 : : /*
14852 : : * Now fetch and print the OPERATOR entries (pg_amop rows).
14853 : : */
14854 : 49 : ntups = PQntuples(res_ops);
14855 : :
14856 : 49 : i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
14857 : 49 : i_amopopr = PQfnumber(res_ops, "amopopr");
14858 : 49 : i_sortfamily = PQfnumber(res_ops, "sortfamily");
14859 : 49 : i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp");
14860 : :
14861 [ + + ]: 219 : for (i = 0; i < ntups; i++)
14862 : : {
14863 : 170 : amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
14864 : 170 : amopopr = PQgetvalue(res_ops, i, i_amopopr);
14865 : 170 : sortfamily = PQgetvalue(res_ops, i, i_sortfamily);
14866 : 170 : sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp);
14867 : :
14868 [ + + ]: 170 : if (needComma)
14869 : 136 : appendPQExpBufferStr(q, " ,\n ");
14870 : :
14871 : 170 : appendPQExpBuffer(q, "OPERATOR %s %s",
14872 : : amopstrategy, amopopr);
14873 : :
14874 [ - + ]: 170 : if (strlen(sortfamily) > 0)
14875 : : {
14876 : 0 : appendPQExpBufferStr(q, " FOR ORDER BY ");
14877 : 0 : appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
14878 : 0 : appendPQExpBufferStr(q, fmtId(sortfamily));
14879 : : }
14880 : :
14881 : 170 : needComma = true;
14882 : : }
14883 : :
14884 : : /*
14885 : : * Now fetch and print the FUNCTION entries (pg_amproc rows).
14886 : : */
14887 : 49 : ntups = PQntuples(res_procs);
14888 : :
14889 : 49 : i_amprocnum = PQfnumber(res_procs, "amprocnum");
14890 : 49 : i_amproc = PQfnumber(res_procs, "amproc");
14891 : 49 : i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
14892 : 49 : i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
14893 : :
14894 [ + + ]: 234 : for (i = 0; i < ntups; i++)
14895 : : {
14896 : 185 : amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
14897 : 185 : amproc = PQgetvalue(res_procs, i, i_amproc);
14898 : 185 : amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
14899 : 185 : amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
14900 : :
14901 [ + + ]: 185 : if (needComma)
14902 : 170 : appendPQExpBufferStr(q, " ,\n ");
14903 : :
14904 : 185 : appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
14905 : : amprocnum, amproclefttype, amprocrighttype,
14906 : : amproc);
14907 : :
14908 : 185 : needComma = true;
14909 : : }
14910 : :
14911 : 49 : appendPQExpBufferStr(q, ";\n");
14912 : : }
14913 : :
14914 : 547 : appendPQExpBufferStr(nameusing, fmtId(opfinfo->dobj.name));
14915 : 547 : appendPQExpBuffer(nameusing, " USING %s",
14916 : : fmtId(amname));
14917 : :
14918 [ + + ]: 547 : if (dopt->binary_upgrade)
14919 : 9 : binary_upgrade_extension_member(q, &opfinfo->dobj,
14920 : 9 : "OPERATOR FAMILY", nameusing->data,
14921 : 9 : opfinfo->dobj.namespace->dobj.name);
14922 : :
14923 [ + - ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14924 : 547 : ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
14925 : 547 : ARCHIVE_OPTS(.tag = opfinfo->dobj.name,
14926 : : .namespace = opfinfo->dobj.namespace->dobj.name,
14927 : : .owner = opfinfo->rolname,
14928 : : .description = "OPERATOR FAMILY",
14929 : : .section = SECTION_PRE_DATA,
14930 : : .createStmt = q->data,
14931 : : .dropStmt = delq->data));
14932 : :
14933 : : /* Dump Operator Family Comments */
14934 [ - + ]: 547 : if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14935 : 0 : dumpComment(fout, "OPERATOR FAMILY", nameusing->data,
14936 : 0 : opfinfo->dobj.namespace->dobj.name, opfinfo->rolname,
14937 : 0 : opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
14938 : :
14939 : 547 : pg_free(amname);
14940 : 547 : PQclear(res_ops);
14941 : 547 : PQclear(res_procs);
14942 : 547 : destroyPQExpBuffer(query);
14943 : 547 : destroyPQExpBuffer(q);
14944 : 547 : destroyPQExpBuffer(delq);
14945 : 547 : destroyPQExpBuffer(nameusing);
14946 : : }
14947 : :
14948 : : /*
14949 : : * dumpCollation
14950 : : * write out a single collation definition
14951 : : */
14952 : : static void
14953 : 2733 : dumpCollation(Archive *fout, const CollInfo *collinfo)
14954 : : {
14955 : 2733 : DumpOptions *dopt = fout->dopt;
14956 : : PQExpBuffer query;
14957 : : PQExpBuffer q;
14958 : : PQExpBuffer delq;
14959 : : char *qcollname;
14960 : : PGresult *res;
14961 : : int i_collprovider;
14962 : : int i_collisdeterministic;
14963 : : int i_collcollate;
14964 : : int i_collctype;
14965 : : int i_colllocale;
14966 : : int i_collicurules;
14967 : : const char *collprovider;
14968 : : const char *collcollate;
14969 : : const char *collctype;
14970 : : const char *colllocale;
14971 : : const char *collicurules;
14972 : :
14973 : : /* Do nothing if not dumping schema */
14974 [ + + ]: 2733 : if (!dopt->dumpSchema)
14975 : 12 : return;
14976 : :
14977 : 2721 : query = createPQExpBuffer();
14978 : 2721 : q = createPQExpBuffer();
14979 : 2721 : delq = createPQExpBuffer();
14980 : :
14981 : 2721 : qcollname = pg_strdup(fmtId(collinfo->dobj.name));
14982 : :
14983 : : /* Get collation-specific details */
14984 : 2721 : appendPQExpBufferStr(query, "SELECT ");
14985 : :
14986 : 2721 : appendPQExpBufferStr(query,
14987 : : "collprovider, "
14988 : : "collversion, ");
14989 : :
14990 [ + - ]: 2721 : if (fout->remoteVersion >= 120000)
14991 : 2721 : appendPQExpBufferStr(query,
14992 : : "collisdeterministic, ");
14993 : : else
14994 : 0 : appendPQExpBufferStr(query,
14995 : : "true AS collisdeterministic, ");
14996 : :
14997 [ + - ]: 2721 : if (fout->remoteVersion >= 170000)
14998 : 2721 : appendPQExpBufferStr(query,
14999 : : "colllocale, ");
15000 [ # # ]: 0 : else if (fout->remoteVersion >= 150000)
15001 : 0 : appendPQExpBufferStr(query,
15002 : : "colliculocale AS colllocale, ");
15003 : : else
15004 : 0 : appendPQExpBufferStr(query,
15005 : : "NULL AS colllocale, ");
15006 : :
15007 [ + - ]: 2721 : if (fout->remoteVersion >= 160000)
15008 : 2721 : appendPQExpBufferStr(query,
15009 : : "collicurules, ");
15010 : : else
15011 : 0 : appendPQExpBufferStr(query,
15012 : : "NULL AS collicurules, ");
15013 : :
15014 : 2721 : appendPQExpBuffer(query,
15015 : : "collcollate, "
15016 : : "collctype "
15017 : : "FROM pg_catalog.pg_collation c "
15018 : : "WHERE c.oid = '%u'::pg_catalog.oid",
15019 : 2721 : collinfo->dobj.catId.oid);
15020 : :
15021 : 2721 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15022 : :
15023 : 2721 : i_collprovider = PQfnumber(res, "collprovider");
15024 : 2721 : i_collisdeterministic = PQfnumber(res, "collisdeterministic");
15025 : 2721 : i_collcollate = PQfnumber(res, "collcollate");
15026 : 2721 : i_collctype = PQfnumber(res, "collctype");
15027 : 2721 : i_colllocale = PQfnumber(res, "colllocale");
15028 : 2721 : i_collicurules = PQfnumber(res, "collicurules");
15029 : :
15030 : 2721 : collprovider = PQgetvalue(res, 0, i_collprovider);
15031 : :
15032 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collcollate))
15033 : 48 : collcollate = PQgetvalue(res, 0, i_collcollate);
15034 : : else
15035 : 2673 : collcollate = NULL;
15036 : :
15037 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_collctype))
15038 : 48 : collctype = PQgetvalue(res, 0, i_collctype);
15039 : : else
15040 : 2673 : collctype = NULL;
15041 : :
15042 : : /*
15043 : : * Before version 15, collcollate and collctype were of type NAME and
15044 : : * non-nullable. Treat empty strings as NULL for consistency.
15045 : : */
15046 [ - + ]: 2721 : if (fout->remoteVersion < 150000)
15047 : : {
15048 [ # # ]: 0 : if (collcollate[0] == '\0')
15049 : 0 : collcollate = NULL;
15050 [ # # ]: 0 : if (collctype[0] == '\0')
15051 : 0 : collctype = NULL;
15052 : : }
15053 : :
15054 [ + + ]: 2721 : if (!PQgetisnull(res, 0, i_colllocale))
15055 : 2670 : colllocale = PQgetvalue(res, 0, i_colllocale);
15056 : : else
15057 : 51 : colllocale = NULL;
15058 : :
15059 [ - + ]: 2721 : if (!PQgetisnull(res, 0, i_collicurules))
15060 : 0 : collicurules = PQgetvalue(res, 0, i_collicurules);
15061 : : else
15062 : 2721 : collicurules = NULL;
15063 : :
15064 : 2721 : appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
15065 : 2721 : fmtQualifiedDumpable(collinfo));
15066 : :
15067 : 2721 : appendPQExpBuffer(q, "CREATE COLLATION %s (",
15068 : 2721 : fmtQualifiedDumpable(collinfo));
15069 : :
15070 : 2721 : appendPQExpBufferStr(q, "provider = ");
15071 [ + + ]: 2721 : if (collprovider[0] == 'b')
15072 : 19 : appendPQExpBufferStr(q, "builtin");
15073 [ + + ]: 2702 : else if (collprovider[0] == 'c')
15074 : 48 : appendPQExpBufferStr(q, "libc");
15075 [ + + ]: 2654 : else if (collprovider[0] == 'i')
15076 : 2651 : appendPQExpBufferStr(q, "icu");
15077 [ + - ]: 3 : else if (collprovider[0] == 'd')
15078 : : /* to allow dumping pg_catalog; not accepted on input */
15079 : 3 : appendPQExpBufferStr(q, "default");
15080 : : else
15081 : 0 : pg_fatal("unrecognized collation provider: %s",
15082 : : collprovider);
15083 : :
15084 [ - + ]: 2721 : if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
15085 : 0 : appendPQExpBufferStr(q, ", deterministic = false");
15086 : :
15087 [ + + ]: 2721 : if (collprovider[0] == 'd')
15088 : : {
15089 [ + - + - : 3 : if (collcollate || collctype || colllocale || collicurules)
+ - - + ]
15090 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15091 : :
15092 : : /* no locale -- the default collation cannot be reloaded anyway */
15093 : : }
15094 [ + + ]: 2718 : else if (collprovider[0] == 'b')
15095 : : {
15096 [ + - + - : 19 : if (collcollate || collctype || !colllocale || collicurules)
+ - - + ]
15097 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15098 : :
15099 : 19 : appendPQExpBufferStr(q, ", locale = ");
15100 [ + - ]: 19 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15101 : : fout);
15102 : : }
15103 [ + + ]: 2699 : else if (collprovider[0] == 'i')
15104 : : {
15105 [ + - ]: 2651 : if (fout->remoteVersion >= 150000)
15106 : : {
15107 [ + - + - : 2651 : if (collcollate || collctype || !colllocale)
- + ]
15108 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15109 : :
15110 : 2651 : appendPQExpBufferStr(q, ", locale = ");
15111 [ + - ]: 2651 : appendStringLiteralAH(q, colllocale ? colllocale : "",
15112 : : fout);
15113 : : }
15114 : : else
15115 : : {
15116 [ # # # # : 0 : if (!collcollate || !collctype || colllocale ||
# # ]
15117 [ # # ]: 0 : strcmp(collcollate, collctype) != 0)
15118 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15119 : :
15120 : 0 : appendPQExpBufferStr(q, ", locale = ");
15121 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15122 : : }
15123 : :
15124 [ - + ]: 2651 : if (collicurules)
15125 : : {
15126 : 0 : appendPQExpBufferStr(q, ", rules = ");
15127 [ # # ]: 0 : appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
15128 : : }
15129 : : }
15130 [ + - ]: 48 : else if (collprovider[0] == 'c')
15131 : : {
15132 [ + - + - : 48 : if (colllocale || collicurules || !collcollate || !collctype)
+ - - + ]
15133 : 0 : pg_log_warning("invalid collation \"%s\"", qcollname);
15134 : :
15135 [ + - + - : 48 : if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
+ - ]
15136 : : {
15137 : 48 : appendPQExpBufferStr(q, ", locale = ");
15138 [ + - ]: 48 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15139 : : }
15140 : : else
15141 : : {
15142 : 0 : appendPQExpBufferStr(q, ", lc_collate = ");
15143 [ # # ]: 0 : appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
15144 : 0 : appendPQExpBufferStr(q, ", lc_ctype = ");
15145 [ # # ]: 0 : appendStringLiteralAH(q, collctype ? collctype : "", fout);
15146 : : }
15147 : : }
15148 : : else
15149 : 0 : pg_fatal("unrecognized collation provider: %s", collprovider);
15150 : :
15151 : : /*
15152 : : * For binary upgrade, carry over the collation version. For normal
15153 : : * dump/restore, omit the version, so that it is computed upon restore.
15154 : : */
15155 [ + + ]: 2721 : if (dopt->binary_upgrade)
15156 : : {
15157 : : int i_collversion;
15158 : :
15159 : 5 : i_collversion = PQfnumber(res, "collversion");
15160 [ + + ]: 5 : if (!PQgetisnull(res, 0, i_collversion))
15161 : : {
15162 : 4 : appendPQExpBufferStr(q, ", version = ");
15163 : 4 : appendStringLiteralAH(q,
15164 : : PQgetvalue(res, 0, i_collversion),
15165 : : fout);
15166 : : }
15167 : : }
15168 : :
15169 : 2721 : appendPQExpBufferStr(q, ");\n");
15170 : :
15171 [ + + ]: 2721 : if (dopt->binary_upgrade)
15172 : 5 : binary_upgrade_extension_member(q, &collinfo->dobj,
15173 : : "COLLATION", qcollname,
15174 : 5 : collinfo->dobj.namespace->dobj.name);
15175 : :
15176 [ + - ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15177 : 2721 : ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
15178 : 2721 : ARCHIVE_OPTS(.tag = collinfo->dobj.name,
15179 : : .namespace = collinfo->dobj.namespace->dobj.name,
15180 : : .owner = collinfo->rolname,
15181 : : .description = "COLLATION",
15182 : : .section = SECTION_PRE_DATA,
15183 : : .createStmt = q->data,
15184 : : .dropStmt = delq->data));
15185 : :
15186 : : /* Dump Collation Comments */
15187 [ + + ]: 2721 : if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15188 : 2613 : dumpComment(fout, "COLLATION", qcollname,
15189 : 2613 : collinfo->dobj.namespace->dobj.name, collinfo->rolname,
15190 : 2613 : collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
15191 : :
15192 : 2721 : PQclear(res);
15193 : :
15194 : 2721 : destroyPQExpBuffer(query);
15195 : 2721 : destroyPQExpBuffer(q);
15196 : 2721 : destroyPQExpBuffer(delq);
15197 : 2721 : pg_free(qcollname);
15198 : : }
15199 : :
15200 : : /*
15201 : : * dumpConversion
15202 : : * write out a single conversion definition
15203 : : */
15204 : : static void
15205 : 335 : dumpConversion(Archive *fout, const ConvInfo *convinfo)
15206 : : {
15207 : 335 : DumpOptions *dopt = fout->dopt;
15208 : : PQExpBuffer query;
15209 : : PQExpBuffer q;
15210 : : PQExpBuffer delq;
15211 : : char *qconvname;
15212 : : PGresult *res;
15213 : : int i_conforencoding;
15214 : : int i_contoencoding;
15215 : : int i_conproc;
15216 : : int i_condefault;
15217 : : const char *conforencoding;
15218 : : const char *contoencoding;
15219 : : const char *conproc;
15220 : : bool condefault;
15221 : :
15222 : : /* Do nothing if not dumping schema */
15223 [ + + ]: 335 : if (!dopt->dumpSchema)
15224 : 7 : return;
15225 : :
15226 : 328 : query = createPQExpBuffer();
15227 : 328 : q = createPQExpBuffer();
15228 : 328 : delq = createPQExpBuffer();
15229 : :
15230 : 328 : qconvname = pg_strdup(fmtId(convinfo->dobj.name));
15231 : :
15232 : : /* Get conversion-specific details */
15233 : 328 : appendPQExpBuffer(query, "SELECT "
15234 : : "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
15235 : : "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
15236 : : "conproc, condefault "
15237 : : "FROM pg_catalog.pg_conversion c "
15238 : : "WHERE c.oid = '%u'::pg_catalog.oid",
15239 : 328 : convinfo->dobj.catId.oid);
15240 : :
15241 : 328 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15242 : :
15243 : 328 : i_conforencoding = PQfnumber(res, "conforencoding");
15244 : 328 : i_contoencoding = PQfnumber(res, "contoencoding");
15245 : 328 : i_conproc = PQfnumber(res, "conproc");
15246 : 328 : i_condefault = PQfnumber(res, "condefault");
15247 : :
15248 : 328 : conforencoding = PQgetvalue(res, 0, i_conforencoding);
15249 : 328 : contoencoding = PQgetvalue(res, 0, i_contoencoding);
15250 : 328 : conproc = PQgetvalue(res, 0, i_conproc);
15251 : 328 : condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
15252 : :
15253 : 328 : appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
15254 : 328 : fmtQualifiedDumpable(convinfo));
15255 : :
15256 [ + - ]: 328 : appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
15257 : : (condefault) ? "DEFAULT " : "",
15258 : 328 : fmtQualifiedDumpable(convinfo));
15259 : 328 : appendStringLiteralAH(q, conforencoding, fout);
15260 : 328 : appendPQExpBufferStr(q, " TO ");
15261 : 328 : appendStringLiteralAH(q, contoencoding, fout);
15262 : : /* regproc output is already sufficiently quoted */
15263 : 328 : appendPQExpBuffer(q, " FROM %s;\n", conproc);
15264 : :
15265 [ + + ]: 328 : if (dopt->binary_upgrade)
15266 : 1 : binary_upgrade_extension_member(q, &convinfo->dobj,
15267 : : "CONVERSION", qconvname,
15268 : 1 : convinfo->dobj.namespace->dobj.name);
15269 : :
15270 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15271 : 328 : ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
15272 : 328 : ARCHIVE_OPTS(.tag = convinfo->dobj.name,
15273 : : .namespace = convinfo->dobj.namespace->dobj.name,
15274 : : .owner = convinfo->rolname,
15275 : : .description = "CONVERSION",
15276 : : .section = SECTION_PRE_DATA,
15277 : : .createStmt = q->data,
15278 : : .dropStmt = delq->data));
15279 : :
15280 : : /* Dump Conversion Comments */
15281 [ + - ]: 328 : if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15282 : 328 : dumpComment(fout, "CONVERSION", qconvname,
15283 : 328 : convinfo->dobj.namespace->dobj.name, convinfo->rolname,
15284 : 328 : convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
15285 : :
15286 : 328 : PQclear(res);
15287 : :
15288 : 328 : destroyPQExpBuffer(query);
15289 : 328 : destroyPQExpBuffer(q);
15290 : 328 : destroyPQExpBuffer(delq);
15291 : 328 : pg_free(qconvname);
15292 : : }
15293 : :
15294 : : /*
15295 : : * format_aggregate_signature: generate aggregate name and argument list
15296 : : *
15297 : : * The argument type names are qualified if needed. The aggregate name
15298 : : * is never qualified.
15299 : : */
15300 : : static char *
15301 : 287 : format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
15302 : : {
15303 : : PQExpBufferData buf;
15304 : : int j;
15305 : :
15306 : 287 : initPQExpBuffer(&buf);
15307 [ - + ]: 287 : if (honor_quotes)
15308 : 0 : appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name));
15309 : : else
15310 : 287 : appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name);
15311 : :
15312 [ + + ]: 287 : if (agginfo->aggfn.nargs == 0)
15313 : 40 : appendPQExpBufferStr(&buf, "(*)");
15314 : : else
15315 : : {
15316 : 247 : appendPQExpBufferChar(&buf, '(');
15317 [ + + ]: 539 : for (j = 0; j < agginfo->aggfn.nargs; j++)
15318 [ + + ]: 292 : appendPQExpBuffer(&buf, "%s%s",
15319 : : (j > 0) ? ", " : "",
15320 : : getFormattedTypeName(fout,
15321 : 292 : agginfo->aggfn.argtypes[j],
15322 : : zeroIsError));
15323 : 247 : appendPQExpBufferChar(&buf, ')');
15324 : : }
15325 : 287 : return buf.data;
15326 : : }
15327 : :
15328 : : /*
15329 : : * dumpAgg
15330 : : * write out a single aggregate definition
15331 : : */
15332 : : static void
15333 : 295 : dumpAgg(Archive *fout, const AggInfo *agginfo)
15334 : : {
15335 : 295 : DumpOptions *dopt = fout->dopt;
15336 : : PQExpBuffer query;
15337 : : PQExpBuffer q;
15338 : : PQExpBuffer delq;
15339 : : PQExpBuffer details;
15340 : : char *aggsig; /* identity signature */
15341 : 295 : char *aggfullsig = NULL; /* full signature */
15342 : : char *aggsig_tag;
15343 : : PGresult *res;
15344 : : int i_agginitval;
15345 : : int i_aggminitval;
15346 : : const char *aggtransfn;
15347 : : const char *aggfinalfn;
15348 : : const char *aggcombinefn;
15349 : : const char *aggserialfn;
15350 : : const char *aggdeserialfn;
15351 : : const char *aggmtransfn;
15352 : : const char *aggminvtransfn;
15353 : : const char *aggmfinalfn;
15354 : : bool aggfinalextra;
15355 : : bool aggmfinalextra;
15356 : : char aggfinalmodify;
15357 : : char aggmfinalmodify;
15358 : : const char *aggsortop;
15359 : : char *aggsortconvop;
15360 : : char aggkind;
15361 : : const char *aggtranstype;
15362 : : const char *aggtransspace;
15363 : : const char *aggmtranstype;
15364 : : const char *aggmtransspace;
15365 : : const char *agginitval;
15366 : : const char *aggminitval;
15367 : : const char *proparallel;
15368 : : char defaultfinalmodify;
15369 : :
15370 : : /* Do nothing if not dumping schema */
15371 [ + + ]: 295 : if (!dopt->dumpSchema)
15372 : 8 : return;
15373 : :
15374 : 287 : query = createPQExpBuffer();
15375 : 287 : q = createPQExpBuffer();
15376 : 287 : delq = createPQExpBuffer();
15377 : 287 : details = createPQExpBuffer();
15378 : :
15379 [ + + ]: 287 : if (!fout->is_prepared[PREPQUERY_DUMPAGG])
15380 : : {
15381 : : /* Set up query for aggregate-specific details */
15382 : 57 : appendPQExpBufferStr(query,
15383 : : "PREPARE dumpAgg(pg_catalog.oid) AS\n");
15384 : :
15385 : 57 : appendPQExpBufferStr(query,
15386 : : "SELECT "
15387 : : "aggtransfn,\n"
15388 : : "aggfinalfn,\n"
15389 : : "aggtranstype::pg_catalog.regtype,\n"
15390 : : "agginitval,\n"
15391 : : "aggsortop,\n"
15392 : : "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
15393 : : "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
15394 : :
15395 : 57 : appendPQExpBufferStr(query,
15396 : : "aggkind,\n"
15397 : : "aggmtransfn,\n"
15398 : : "aggminvtransfn,\n"
15399 : : "aggmfinalfn,\n"
15400 : : "aggmtranstype::pg_catalog.regtype,\n"
15401 : : "aggfinalextra,\n"
15402 : : "aggmfinalextra,\n"
15403 : : "aggtransspace,\n"
15404 : : "aggmtransspace,\n"
15405 : : "aggminitval,\n");
15406 : :
15407 : 57 : appendPQExpBufferStr(query,
15408 : : "aggcombinefn,\n"
15409 : : "aggserialfn,\n"
15410 : : "aggdeserialfn,\n"
15411 : : "proparallel,\n");
15412 : :
15413 [ + - ]: 57 : if (fout->remoteVersion >= 110000)
15414 : 57 : appendPQExpBufferStr(query,
15415 : : "aggfinalmodify,\n"
15416 : : "aggmfinalmodify\n");
15417 : : else
15418 : 0 : appendPQExpBufferStr(query,
15419 : : "'0' AS aggfinalmodify,\n"
15420 : : "'0' AS aggmfinalmodify\n");
15421 : :
15422 : 57 : appendPQExpBufferStr(query,
15423 : : "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
15424 : : "WHERE a.aggfnoid = p.oid "
15425 : : "AND p.oid = $1");
15426 : :
15427 : 57 : ExecuteSqlStatement(fout, query->data);
15428 : :
15429 : 57 : fout->is_prepared[PREPQUERY_DUMPAGG] = true;
15430 : : }
15431 : :
15432 : 287 : printfPQExpBuffer(query,
15433 : : "EXECUTE dumpAgg('%u')",
15434 : 287 : agginfo->aggfn.dobj.catId.oid);
15435 : :
15436 : 287 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15437 : :
15438 : 287 : i_agginitval = PQfnumber(res, "agginitval");
15439 : 287 : i_aggminitval = PQfnumber(res, "aggminitval");
15440 : :
15441 : 287 : aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
15442 : 287 : aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
15443 : 287 : aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
15444 : 287 : aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
15445 : 287 : aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
15446 : 287 : aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
15447 : 287 : aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
15448 : 287 : aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
15449 : 287 : aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
15450 : 287 : aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
15451 : 287 : aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
15452 : 287 : aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
15453 : 287 : aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
15454 : 287 : aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
15455 : 287 : aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
15456 : 287 : aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
15457 : 287 : aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
15458 : 287 : aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
15459 : 287 : agginitval = PQgetvalue(res, 0, i_agginitval);
15460 : 287 : aggminitval = PQgetvalue(res, 0, i_aggminitval);
15461 : 287 : proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
15462 : :
15463 : : {
15464 : : char *funcargs;
15465 : : char *funciargs;
15466 : :
15467 : 287 : funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
15468 : 287 : funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
15469 : 287 : aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
15470 : 287 : aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
15471 : : }
15472 : :
15473 : 287 : aggsig_tag = format_aggregate_signature(agginfo, fout, false);
15474 : :
15475 : : /* identify default modify flag for aggkind (must match DefineAggregate) */
15476 [ + + ]: 287 : defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
15477 : : /* replace omitted flags for old versions */
15478 [ - + ]: 287 : if (aggfinalmodify == '0')
15479 : 0 : aggfinalmodify = defaultfinalmodify;
15480 [ - + ]: 287 : if (aggmfinalmodify == '0')
15481 : 0 : aggmfinalmodify = defaultfinalmodify;
15482 : :
15483 : : /* regproc and regtype output is already sufficiently quoted */
15484 : 287 : appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
15485 : : aggtransfn, aggtranstype);
15486 : :
15487 [ + + ]: 287 : if (strcmp(aggtransspace, "0") != 0)
15488 : : {
15489 : 5 : appendPQExpBuffer(details, ",\n SSPACE = %s",
15490 : : aggtransspace);
15491 : : }
15492 : :
15493 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_agginitval))
15494 : : {
15495 : 209 : appendPQExpBufferStr(details, ",\n INITCOND = ");
15496 : 209 : appendStringLiteralAH(details, agginitval, fout);
15497 : : }
15498 : :
15499 [ + + ]: 287 : if (strcmp(aggfinalfn, "-") != 0)
15500 : : {
15501 : 134 : appendPQExpBuffer(details, ",\n FINALFUNC = %s",
15502 : : aggfinalfn);
15503 [ + + ]: 134 : if (aggfinalextra)
15504 : 10 : appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
15505 [ + + ]: 134 : if (aggfinalmodify != defaultfinalmodify)
15506 : : {
15507 [ - + - - ]: 34 : switch (aggfinalmodify)
15508 : : {
15509 : 0 : case AGGMODIFY_READ_ONLY:
15510 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
15511 : 0 : break;
15512 : 34 : case AGGMODIFY_SHAREABLE:
15513 : 34 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
15514 : 34 : break;
15515 : 0 : case AGGMODIFY_READ_WRITE:
15516 : 0 : appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
15517 : 0 : break;
15518 : 0 : default:
15519 : 0 : pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
15520 : : agginfo->aggfn.dobj.name);
15521 : : break;
15522 : : }
15523 : : }
15524 : : }
15525 : :
15526 [ - + ]: 287 : if (strcmp(aggcombinefn, "-") != 0)
15527 : 0 : appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
15528 : :
15529 [ - + ]: 287 : if (strcmp(aggserialfn, "-") != 0)
15530 : 0 : appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
15531 : :
15532 [ - + ]: 287 : if (strcmp(aggdeserialfn, "-") != 0)
15533 : 0 : appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
15534 : :
15535 [ + + ]: 287 : if (strcmp(aggmtransfn, "-") != 0)
15536 : : {
15537 : 30 : appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
15538 : : aggmtransfn,
15539 : : aggminvtransfn,
15540 : : aggmtranstype);
15541 : : }
15542 : :
15543 [ - + ]: 287 : if (strcmp(aggmtransspace, "0") != 0)
15544 : : {
15545 : 0 : appendPQExpBuffer(details, ",\n MSSPACE = %s",
15546 : : aggmtransspace);
15547 : : }
15548 : :
15549 [ + + ]: 287 : if (!PQgetisnull(res, 0, i_aggminitval))
15550 : : {
15551 : 10 : appendPQExpBufferStr(details, ",\n MINITCOND = ");
15552 : 10 : appendStringLiteralAH(details, aggminitval, fout);
15553 : : }
15554 : :
15555 [ - + ]: 287 : if (strcmp(aggmfinalfn, "-") != 0)
15556 : : {
15557 : 0 : appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
15558 : : aggmfinalfn);
15559 [ # # ]: 0 : if (aggmfinalextra)
15560 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
15561 [ # # ]: 0 : if (aggmfinalmodify != defaultfinalmodify)
15562 : : {
15563 [ # # # # ]: 0 : switch (aggmfinalmodify)
15564 : : {
15565 : 0 : case AGGMODIFY_READ_ONLY:
15566 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
15567 : 0 : break;
15568 : 0 : case AGGMODIFY_SHAREABLE:
15569 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
15570 : 0 : break;
15571 : 0 : case AGGMODIFY_READ_WRITE:
15572 : 0 : appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
15573 : 0 : break;
15574 : 0 : default:
15575 : 0 : pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
15576 : : agginfo->aggfn.dobj.name);
15577 : : break;
15578 : : }
15579 : : }
15580 : : }
15581 : :
15582 : 287 : aggsortconvop = getFormattedOperatorName(aggsortop);
15583 [ - + ]: 287 : if (aggsortconvop)
15584 : : {
15585 : 0 : appendPQExpBuffer(details, ",\n SORTOP = %s",
15586 : : aggsortconvop);
15587 : 0 : free(aggsortconvop);
15588 : : }
15589 : :
15590 [ + + ]: 287 : if (aggkind == AGGKIND_HYPOTHETICAL)
15591 : 5 : appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
15592 : :
15593 [ + + ]: 287 : if (proparallel[0] != PROPARALLEL_UNSAFE)
15594 : : {
15595 [ + - ]: 5 : if (proparallel[0] == PROPARALLEL_SAFE)
15596 : 5 : appendPQExpBufferStr(details, ",\n PARALLEL = safe");
15597 [ # # ]: 0 : else if (proparallel[0] == PROPARALLEL_RESTRICTED)
15598 : 0 : appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
15599 [ # # ]: 0 : else if (proparallel[0] != PROPARALLEL_UNSAFE)
15600 : 0 : pg_fatal("unrecognized proparallel value for function \"%s\"",
15601 : : agginfo->aggfn.dobj.name);
15602 : : }
15603 : :
15604 : 287 : appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
15605 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15606 : : aggsig);
15607 : :
15608 [ + - ]: 574 : appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
15609 : 287 : fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
15610 : : aggfullsig ? aggfullsig : aggsig, details->data);
15611 : :
15612 [ + + ]: 287 : if (dopt->binary_upgrade)
15613 : 49 : binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
15614 : : "AGGREGATE", aggsig,
15615 : 49 : agginfo->aggfn.dobj.namespace->dobj.name);
15616 : :
15617 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
15618 : 270 : ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
15619 : 270 : agginfo->aggfn.dobj.dumpId,
15620 : 270 : ARCHIVE_OPTS(.tag = aggsig_tag,
15621 : : .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
15622 : : .owner = agginfo->aggfn.rolname,
15623 : : .description = "AGGREGATE",
15624 : : .section = SECTION_PRE_DATA,
15625 : : .createStmt = q->data,
15626 : : .dropStmt = delq->data));
15627 : :
15628 : : /* Dump Aggregate Comments */
15629 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
15630 : 10 : dumpComment(fout, "AGGREGATE", aggsig,
15631 : 10 : agginfo->aggfn.dobj.namespace->dobj.name,
15632 : 10 : agginfo->aggfn.rolname,
15633 : 10 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15634 : :
15635 [ - + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
15636 : 0 : dumpSecLabel(fout, "AGGREGATE", aggsig,
15637 : 0 : agginfo->aggfn.dobj.namespace->dobj.name,
15638 : 0 : agginfo->aggfn.rolname,
15639 : 0 : agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
15640 : :
15641 : : /*
15642 : : * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
15643 : : * command look like a function's GRANT; in particular this affects the
15644 : : * syntax for zero-argument aggregates and ordered-set aggregates.
15645 : : */
15646 : 287 : free(aggsig);
15647 : :
15648 : 287 : aggsig = format_function_signature(fout, &agginfo->aggfn, true);
15649 : :
15650 [ + + ]: 287 : if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
15651 : 18 : dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
15652 : : "FUNCTION", aggsig, NULL,
15653 : 18 : agginfo->aggfn.dobj.namespace->dobj.name,
15654 : 18 : NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
15655 : :
15656 : 287 : free(aggsig);
15657 : 287 : free(aggfullsig);
15658 : 287 : free(aggsig_tag);
15659 : :
15660 : 287 : PQclear(res);
15661 : :
15662 : 287 : destroyPQExpBuffer(query);
15663 : 287 : destroyPQExpBuffer(q);
15664 : 287 : destroyPQExpBuffer(delq);
15665 : 287 : destroyPQExpBuffer(details);
15666 : : }
15667 : :
15668 : : /*
15669 : : * dumpTSParser
15670 : : * write out a single text search parser
15671 : : */
15672 : : static void
15673 : 44 : dumpTSParser(Archive *fout, const TSParserInfo *prsinfo)
15674 : : {
15675 : 44 : DumpOptions *dopt = fout->dopt;
15676 : : PQExpBuffer q;
15677 : : PQExpBuffer delq;
15678 : : char *qprsname;
15679 : :
15680 : : /* Do nothing if not dumping schema */
15681 [ + + ]: 44 : if (!dopt->dumpSchema)
15682 : 7 : return;
15683 : :
15684 : 37 : q = createPQExpBuffer();
15685 : 37 : delq = createPQExpBuffer();
15686 : :
15687 : 37 : qprsname = pg_strdup(fmtId(prsinfo->dobj.name));
15688 : :
15689 : 37 : appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n",
15690 : 37 : fmtQualifiedDumpable(prsinfo));
15691 : :
15692 : 37 : appendPQExpBuffer(q, " START = %s,\n",
15693 : 37 : convertTSFunction(fout, prsinfo->prsstart));
15694 : 37 : appendPQExpBuffer(q, " GETTOKEN = %s,\n",
15695 : 37 : convertTSFunction(fout, prsinfo->prstoken));
15696 : 37 : appendPQExpBuffer(q, " END = %s,\n",
15697 : 37 : convertTSFunction(fout, prsinfo->prsend));
15698 [ + + ]: 37 : if (prsinfo->prsheadline != InvalidOid)
15699 : 3 : appendPQExpBuffer(q, " HEADLINE = %s,\n",
15700 : 3 : convertTSFunction(fout, prsinfo->prsheadline));
15701 : 37 : appendPQExpBuffer(q, " LEXTYPES = %s );\n",
15702 : 37 : convertTSFunction(fout, prsinfo->prslextype));
15703 : :
15704 : 37 : appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s;\n",
15705 : 37 : fmtQualifiedDumpable(prsinfo));
15706 : :
15707 [ + + ]: 37 : if (dopt->binary_upgrade)
15708 : 1 : binary_upgrade_extension_member(q, &prsinfo->dobj,
15709 : : "TEXT SEARCH PARSER", qprsname,
15710 : 1 : prsinfo->dobj.namespace->dobj.name);
15711 : :
15712 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15713 : 37 : ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId,
15714 : 37 : ARCHIVE_OPTS(.tag = prsinfo->dobj.name,
15715 : : .namespace = prsinfo->dobj.namespace->dobj.name,
15716 : : .description = "TEXT SEARCH PARSER",
15717 : : .section = SECTION_PRE_DATA,
15718 : : .createStmt = q->data,
15719 : : .dropStmt = delq->data));
15720 : :
15721 : : /* Dump Parser Comments */
15722 [ + - ]: 37 : if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15723 : 37 : dumpComment(fout, "TEXT SEARCH PARSER", qprsname,
15724 : 37 : prsinfo->dobj.namespace->dobj.name, "",
15725 : 37 : prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId);
15726 : :
15727 : 37 : destroyPQExpBuffer(q);
15728 : 37 : destroyPQExpBuffer(delq);
15729 : 37 : pg_free(qprsname);
15730 : : }
15731 : :
15732 : : /*
15733 : : * dumpTSDictionary
15734 : : * write out a single text search dictionary
15735 : : */
15736 : : static void
15737 : 182 : dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo)
15738 : : {
15739 : 182 : DumpOptions *dopt = fout->dopt;
15740 : : PQExpBuffer q;
15741 : : PQExpBuffer delq;
15742 : : PQExpBuffer query;
15743 : : char *qdictname;
15744 : : PGresult *res;
15745 : : char *nspname;
15746 : : char *tmplname;
15747 : :
15748 : : /* Do nothing if not dumping schema */
15749 [ + + ]: 182 : if (!dopt->dumpSchema)
15750 : 7 : return;
15751 : :
15752 : 175 : q = createPQExpBuffer();
15753 : 175 : delq = createPQExpBuffer();
15754 : 175 : query = createPQExpBuffer();
15755 : :
15756 : 175 : qdictname = pg_strdup(fmtId(dictinfo->dobj.name));
15757 : :
15758 : : /* Fetch name and namespace of the dictionary's template */
15759 : 175 : appendPQExpBuffer(query, "SELECT nspname, tmplname "
15760 : : "FROM pg_ts_template p, pg_namespace n "
15761 : : "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
15762 : 175 : dictinfo->dicttemplate);
15763 : 175 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15764 : 175 : nspname = PQgetvalue(res, 0, 0);
15765 : 175 : tmplname = PQgetvalue(res, 0, 1);
15766 : :
15767 : 175 : appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n",
15768 : 175 : fmtQualifiedDumpable(dictinfo));
15769 : :
15770 : 175 : appendPQExpBufferStr(q, " TEMPLATE = ");
15771 : 175 : appendPQExpBuffer(q, "%s.", fmtId(nspname));
15772 : 175 : appendPQExpBufferStr(q, fmtId(tmplname));
15773 : :
15774 : 175 : PQclear(res);
15775 : :
15776 : : /* the dictinitoption can be dumped straight into the command */
15777 [ + + ]: 175 : if (dictinfo->dictinitoption)
15778 : 138 : appendPQExpBuffer(q, ",\n %s", dictinfo->dictinitoption);
15779 : :
15780 : 175 : appendPQExpBufferStr(q, " );\n");
15781 : :
15782 : 175 : appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s;\n",
15783 : 175 : fmtQualifiedDumpable(dictinfo));
15784 : :
15785 [ + + ]: 175 : if (dopt->binary_upgrade)
15786 : 10 : binary_upgrade_extension_member(q, &dictinfo->dobj,
15787 : : "TEXT SEARCH DICTIONARY", qdictname,
15788 : 10 : dictinfo->dobj.namespace->dobj.name);
15789 : :
15790 [ + - ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15791 : 175 : ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId,
15792 : 175 : ARCHIVE_OPTS(.tag = dictinfo->dobj.name,
15793 : : .namespace = dictinfo->dobj.namespace->dobj.name,
15794 : : .owner = dictinfo->rolname,
15795 : : .description = "TEXT SEARCH DICTIONARY",
15796 : : .section = SECTION_PRE_DATA,
15797 : : .createStmt = q->data,
15798 : : .dropStmt = delq->data));
15799 : :
15800 : : /* Dump Dictionary Comments */
15801 [ + + ]: 175 : if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15802 : 130 : dumpComment(fout, "TEXT SEARCH DICTIONARY", qdictname,
15803 : 130 : dictinfo->dobj.namespace->dobj.name, dictinfo->rolname,
15804 : 130 : dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId);
15805 : :
15806 : 175 : destroyPQExpBuffer(q);
15807 : 175 : destroyPQExpBuffer(delq);
15808 : 175 : destroyPQExpBuffer(query);
15809 : 175 : pg_free(qdictname);
15810 : : }
15811 : :
15812 : : /*
15813 : : * dumpTSTemplate
15814 : : * write out a single text search template
15815 : : */
15816 : : static void
15817 : 56 : dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo)
15818 : : {
15819 : 56 : DumpOptions *dopt = fout->dopt;
15820 : : PQExpBuffer q;
15821 : : PQExpBuffer delq;
15822 : : char *qtmplname;
15823 : :
15824 : : /* Do nothing if not dumping schema */
15825 [ + + ]: 56 : if (!dopt->dumpSchema)
15826 : 7 : return;
15827 : :
15828 : 49 : q = createPQExpBuffer();
15829 : 49 : delq = createPQExpBuffer();
15830 : :
15831 : 49 : qtmplname = pg_strdup(fmtId(tmplinfo->dobj.name));
15832 : :
15833 : 49 : appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n",
15834 : 49 : fmtQualifiedDumpable(tmplinfo));
15835 : :
15836 [ + + ]: 49 : if (tmplinfo->tmplinit != InvalidOid)
15837 : 15 : appendPQExpBuffer(q, " INIT = %s,\n",
15838 : 15 : convertTSFunction(fout, tmplinfo->tmplinit));
15839 : 49 : appendPQExpBuffer(q, " LEXIZE = %s );\n",
15840 : 49 : convertTSFunction(fout, tmplinfo->tmpllexize));
15841 : :
15842 : 49 : appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s;\n",
15843 : 49 : fmtQualifiedDumpable(tmplinfo));
15844 : :
15845 [ + + ]: 49 : if (dopt->binary_upgrade)
15846 : 1 : binary_upgrade_extension_member(q, &tmplinfo->dobj,
15847 : : "TEXT SEARCH TEMPLATE", qtmplname,
15848 : 1 : tmplinfo->dobj.namespace->dobj.name);
15849 : :
15850 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15851 : 49 : ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId,
15852 : 49 : ARCHIVE_OPTS(.tag = tmplinfo->dobj.name,
15853 : : .namespace = tmplinfo->dobj.namespace->dobj.name,
15854 : : .description = "TEXT SEARCH TEMPLATE",
15855 : : .section = SECTION_PRE_DATA,
15856 : : .createStmt = q->data,
15857 : : .dropStmt = delq->data));
15858 : :
15859 : : /* Dump Template Comments */
15860 [ + - ]: 49 : if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15861 : 49 : dumpComment(fout, "TEXT SEARCH TEMPLATE", qtmplname,
15862 : 49 : tmplinfo->dobj.namespace->dobj.name, "",
15863 : 49 : tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId);
15864 : :
15865 : 49 : destroyPQExpBuffer(q);
15866 : 49 : destroyPQExpBuffer(delq);
15867 : 49 : pg_free(qtmplname);
15868 : : }
15869 : :
15870 : : /*
15871 : : * dumpTSConfig
15872 : : * write out a single text search configuration
15873 : : */
15874 : : static void
15875 : 157 : dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo)
15876 : : {
15877 : 157 : DumpOptions *dopt = fout->dopt;
15878 : : PQExpBuffer q;
15879 : : PQExpBuffer delq;
15880 : : PQExpBuffer query;
15881 : : char *qcfgname;
15882 : : PGresult *res;
15883 : : char *nspname;
15884 : : char *prsname;
15885 : : int ntups,
15886 : : i;
15887 : : int i_tokenname;
15888 : : int i_dictname;
15889 : :
15890 : : /* Do nothing if not dumping schema */
15891 [ + + ]: 157 : if (!dopt->dumpSchema)
15892 : 7 : return;
15893 : :
15894 : 150 : q = createPQExpBuffer();
15895 : 150 : delq = createPQExpBuffer();
15896 : 150 : query = createPQExpBuffer();
15897 : :
15898 : 150 : qcfgname = pg_strdup(fmtId(cfginfo->dobj.name));
15899 : :
15900 : : /* Fetch name and namespace of the config's parser */
15901 : 150 : appendPQExpBuffer(query, "SELECT nspname, prsname "
15902 : : "FROM pg_ts_parser p, pg_namespace n "
15903 : : "WHERE p.oid = '%u' AND n.oid = prsnamespace",
15904 : 150 : cfginfo->cfgparser);
15905 : 150 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
15906 : 150 : nspname = PQgetvalue(res, 0, 0);
15907 : 150 : prsname = PQgetvalue(res, 0, 1);
15908 : :
15909 : 150 : appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n",
15910 : 150 : fmtQualifiedDumpable(cfginfo));
15911 : :
15912 : 150 : appendPQExpBuffer(q, " PARSER = %s.", fmtId(nspname));
15913 : 150 : appendPQExpBuffer(q, "%s );\n", fmtId(prsname));
15914 : :
15915 : 150 : PQclear(res);
15916 : :
15917 : 150 : resetPQExpBuffer(query);
15918 : 150 : appendPQExpBuffer(query,
15919 : : "SELECT\n"
15920 : : " ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n"
15921 : : " WHERE t.tokid = m.maptokentype ) AS tokenname,\n"
15922 : : " m.mapdict::pg_catalog.regdictionary AS dictname\n"
15923 : : "FROM pg_catalog.pg_ts_config_map AS m\n"
15924 : : "WHERE m.mapcfg = '%u'\n"
15925 : : "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
15926 : 150 : cfginfo->cfgparser, cfginfo->dobj.catId.oid);
15927 : :
15928 : 150 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15929 : 150 : ntups = PQntuples(res);
15930 : :
15931 : 150 : i_tokenname = PQfnumber(res, "tokenname");
15932 : 150 : i_dictname = PQfnumber(res, "dictname");
15933 : :
15934 [ + + ]: 3135 : for (i = 0; i < ntups; i++)
15935 : : {
15936 : 2985 : char *tokenname = PQgetvalue(res, i, i_tokenname);
15937 : 2985 : char *dictname = PQgetvalue(res, i, i_dictname);
15938 : :
15939 [ + + ]: 2985 : if (i == 0 ||
15940 [ + + ]: 2835 : strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0)
15941 : : {
15942 : : /* starting a new token type, so start a new command */
15943 [ + + ]: 2850 : if (i > 0)
15944 : 2700 : appendPQExpBufferStr(q, ";\n");
15945 : 2850 : appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n",
15946 : 2850 : fmtQualifiedDumpable(cfginfo));
15947 : : /* tokenname needs quoting, dictname does NOT */
15948 : 2850 : appendPQExpBuffer(q, " ADD MAPPING FOR %s WITH %s",
15949 : : fmtId(tokenname), dictname);
15950 : : }
15951 : : else
15952 : 135 : appendPQExpBuffer(q, ", %s", dictname);
15953 : : }
15954 : :
15955 [ + - ]: 150 : if (ntups > 0)
15956 : 150 : appendPQExpBufferStr(q, ";\n");
15957 : :
15958 : 150 : PQclear(res);
15959 : :
15960 : 150 : appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s;\n",
15961 : 150 : fmtQualifiedDumpable(cfginfo));
15962 : :
15963 [ + + ]: 150 : if (dopt->binary_upgrade)
15964 : 5 : binary_upgrade_extension_member(q, &cfginfo->dobj,
15965 : : "TEXT SEARCH CONFIGURATION", qcfgname,
15966 : 5 : cfginfo->dobj.namespace->dobj.name);
15967 : :
15968 [ + - ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15969 : 150 : ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId,
15970 : 150 : ARCHIVE_OPTS(.tag = cfginfo->dobj.name,
15971 : : .namespace = cfginfo->dobj.namespace->dobj.name,
15972 : : .owner = cfginfo->rolname,
15973 : : .description = "TEXT SEARCH CONFIGURATION",
15974 : : .section = SECTION_PRE_DATA,
15975 : : .createStmt = q->data,
15976 : : .dropStmt = delq->data));
15977 : :
15978 : : /* Dump Configuration Comments */
15979 [ + + ]: 150 : if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15980 : 130 : dumpComment(fout, "TEXT SEARCH CONFIGURATION", qcfgname,
15981 : 130 : cfginfo->dobj.namespace->dobj.name, cfginfo->rolname,
15982 : 130 : cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId);
15983 : :
15984 : 150 : destroyPQExpBuffer(q);
15985 : 150 : destroyPQExpBuffer(delq);
15986 : 150 : destroyPQExpBuffer(query);
15987 : 150 : pg_free(qcfgname);
15988 : : }
15989 : :
15990 : : /*
15991 : : * dumpForeignDataWrapper
15992 : : * write out a single foreign-data wrapper definition
15993 : : */
15994 : : static void
15995 : 54 : dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo)
15996 : : {
15997 : 54 : DumpOptions *dopt = fout->dopt;
15998 : : PQExpBuffer q;
15999 : : PQExpBuffer delq;
16000 : : char *qfdwname;
16001 : :
16002 : : /* Do nothing if not dumping schema */
16003 [ + + ]: 54 : if (!dopt->dumpSchema)
16004 : 7 : return;
16005 : :
16006 : 47 : q = createPQExpBuffer();
16007 : 47 : delq = createPQExpBuffer();
16008 : :
16009 : 47 : qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name));
16010 : :
16011 : 47 : appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s",
16012 : : qfdwname);
16013 : :
16014 [ - + ]: 47 : if (strcmp(fdwinfo->fdwhandler, "-") != 0)
16015 : 0 : appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler);
16016 : :
16017 [ - + ]: 47 : if (strcmp(fdwinfo->fdwvalidator, "-") != 0)
16018 : 0 : appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator);
16019 : :
16020 [ - + ]: 47 : if (strcmp(fdwinfo->fdwconnection, "-") != 0)
16021 : 0 : appendPQExpBuffer(q, " CONNECTION %s", fdwinfo->fdwconnection);
16022 : :
16023 [ - + ]: 47 : if (strlen(fdwinfo->fdwoptions) > 0)
16024 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", fdwinfo->fdwoptions);
16025 : :
16026 : 47 : appendPQExpBufferStr(q, ";\n");
16027 : :
16028 : 47 : appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n",
16029 : : qfdwname);
16030 : :
16031 [ + + ]: 47 : if (dopt->binary_upgrade)
16032 : 2 : binary_upgrade_extension_member(q, &fdwinfo->dobj,
16033 : : "FOREIGN DATA WRAPPER", qfdwname,
16034 : : NULL);
16035 : :
16036 [ + - ]: 47 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16037 : 47 : ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
16038 : 47 : ARCHIVE_OPTS(.tag = fdwinfo->dobj.name,
16039 : : .owner = fdwinfo->rolname,
16040 : : .description = "FOREIGN DATA WRAPPER",
16041 : : .section = SECTION_PRE_DATA,
16042 : : .createStmt = q->data,
16043 : : .dropStmt = delq->data));
16044 : :
16045 : : /* Dump Foreign Data Wrapper Comments */
16046 [ - + ]: 47 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16047 : 0 : dumpComment(fout, "FOREIGN DATA WRAPPER", qfdwname,
16048 : 0 : NULL, fdwinfo->rolname,
16049 : 0 : fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
16050 : :
16051 : : /* Handle the ACL */
16052 [ + + ]: 47 : if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL)
16053 : 33 : dumpACL(fout, fdwinfo->dobj.dumpId, InvalidDumpId,
16054 : : "FOREIGN DATA WRAPPER", qfdwname, NULL, NULL,
16055 : 33 : NULL, fdwinfo->rolname, &fdwinfo->dacl);
16056 : :
16057 : 47 : pg_free(qfdwname);
16058 : :
16059 : 47 : destroyPQExpBuffer(q);
16060 : 47 : destroyPQExpBuffer(delq);
16061 : : }
16062 : :
16063 : : /*
16064 : : * dumpForeignServer
16065 : : * write out a foreign server definition
16066 : : */
16067 : : static void
16068 : 58 : dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo)
16069 : : {
16070 : 58 : DumpOptions *dopt = fout->dopt;
16071 : : PQExpBuffer q;
16072 : : PQExpBuffer delq;
16073 : : PQExpBuffer query;
16074 : : PGresult *res;
16075 : : char *qsrvname;
16076 : : char *fdwname;
16077 : :
16078 : : /* Do nothing if not dumping schema */
16079 [ + + ]: 58 : if (!dopt->dumpSchema)
16080 : 9 : return;
16081 : :
16082 : 49 : q = createPQExpBuffer();
16083 : 49 : delq = createPQExpBuffer();
16084 : 49 : query = createPQExpBuffer();
16085 : :
16086 : 49 : qsrvname = pg_strdup(fmtId(srvinfo->dobj.name));
16087 : :
16088 : : /* look up the foreign-data wrapper */
16089 : 49 : appendPQExpBuffer(query, "SELECT fdwname "
16090 : : "FROM pg_foreign_data_wrapper w "
16091 : : "WHERE w.oid = '%u'",
16092 : 49 : srvinfo->srvfdw);
16093 : 49 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
16094 : 49 : fdwname = PQgetvalue(res, 0, 0);
16095 : :
16096 : 49 : appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
16097 [ + - - + ]: 49 : if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0)
16098 : : {
16099 : 0 : appendPQExpBufferStr(q, " TYPE ");
16100 : 0 : appendStringLiteralAH(q, srvinfo->srvtype, fout);
16101 : : }
16102 [ + - - + ]: 49 : if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0)
16103 : : {
16104 : 0 : appendPQExpBufferStr(q, " VERSION ");
16105 : 0 : appendStringLiteralAH(q, srvinfo->srvversion, fout);
16106 : : }
16107 : :
16108 : 49 : appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER ");
16109 : 49 : appendPQExpBufferStr(q, fmtId(fdwname));
16110 : :
16111 [ + - - + ]: 49 : if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0)
16112 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", srvinfo->srvoptions);
16113 : :
16114 : 49 : appendPQExpBufferStr(q, ";\n");
16115 : :
16116 : 49 : appendPQExpBuffer(delq, "DROP SERVER %s;\n",
16117 : : qsrvname);
16118 : :
16119 [ + + ]: 49 : if (dopt->binary_upgrade)
16120 : 2 : binary_upgrade_extension_member(q, &srvinfo->dobj,
16121 : : "SERVER", qsrvname, NULL);
16122 : :
16123 [ + - ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16124 : 49 : ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
16125 : 49 : ARCHIVE_OPTS(.tag = srvinfo->dobj.name,
16126 : : .owner = srvinfo->rolname,
16127 : : .description = "SERVER",
16128 : : .section = SECTION_PRE_DATA,
16129 : : .createStmt = q->data,
16130 : : .dropStmt = delq->data));
16131 : :
16132 : : /* Dump Foreign Server Comments */
16133 [ - + ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16134 : 0 : dumpComment(fout, "SERVER", qsrvname,
16135 : 0 : NULL, srvinfo->rolname,
16136 : 0 : srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
16137 : :
16138 : : /* Handle the ACL */
16139 [ + + ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL)
16140 : 33 : dumpACL(fout, srvinfo->dobj.dumpId, InvalidDumpId,
16141 : : "FOREIGN SERVER", qsrvname, NULL, NULL,
16142 : 33 : NULL, srvinfo->rolname, &srvinfo->dacl);
16143 : :
16144 : : /* Dump user mappings */
16145 [ + - ]: 49 : if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP)
16146 : 49 : dumpUserMappings(fout,
16147 : 49 : srvinfo->dobj.name, NULL,
16148 : 49 : srvinfo->rolname,
16149 : 49 : srvinfo->dobj.catId, srvinfo->dobj.dumpId);
16150 : :
16151 : 49 : PQclear(res);
16152 : :
16153 : 49 : pg_free(qsrvname);
16154 : :
16155 : 49 : destroyPQExpBuffer(q);
16156 : 49 : destroyPQExpBuffer(delq);
16157 : 49 : destroyPQExpBuffer(query);
16158 : : }
16159 : :
16160 : : /*
16161 : : * dumpUserMappings
16162 : : *
16163 : : * This routine is used to dump any user mappings associated with the
16164 : : * server handed to this routine. Should be called after ArchiveEntry()
16165 : : * for the server.
16166 : : */
16167 : : static void
16168 : 49 : dumpUserMappings(Archive *fout,
16169 : : const char *servername, const char *namespace,
16170 : : const char *owner,
16171 : : CatalogId catalogId, DumpId dumpId)
16172 : : {
16173 : : PQExpBuffer q;
16174 : : PQExpBuffer delq;
16175 : : PQExpBuffer query;
16176 : : PQExpBuffer tag;
16177 : : PGresult *res;
16178 : : int ntups;
16179 : : int i_usename;
16180 : : int i_umoptions;
16181 : : int i;
16182 : :
16183 : 49 : q = createPQExpBuffer();
16184 : 49 : tag = createPQExpBuffer();
16185 : 49 : delq = createPQExpBuffer();
16186 : 49 : query = createPQExpBuffer();
16187 : :
16188 : : /*
16189 : : * We read from the publicly accessible view pg_user_mappings, so as not
16190 : : * to fail if run by a non-superuser. Note that the view will show
16191 : : * umoptions as null if the user hasn't got privileges for the associated
16192 : : * server; this means that pg_dump will dump such a mapping, but with no
16193 : : * OPTIONS clause. A possible alternative is to skip such mappings
16194 : : * altogether, but it's not clear that that's an improvement.
16195 : : */
16196 : 49 : appendPQExpBuffer(query,
16197 : : "SELECT usename, "
16198 : : "array_to_string(ARRAY("
16199 : : "SELECT quote_ident(option_name) || ' ' || "
16200 : : "quote_literal(option_value) "
16201 : : "FROM pg_options_to_table(umoptions) "
16202 : : "ORDER BY option_name"
16203 : : "), E',\n ') AS umoptions "
16204 : : "FROM pg_user_mappings "
16205 : : "WHERE srvid = '%u' "
16206 : : "ORDER BY usename",
16207 : : catalogId.oid);
16208 : :
16209 : 49 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16210 : :
16211 : 49 : ntups = PQntuples(res);
16212 : 49 : i_usename = PQfnumber(res, "usename");
16213 : 49 : i_umoptions = PQfnumber(res, "umoptions");
16214 : :
16215 [ + + ]: 82 : for (i = 0; i < ntups; i++)
16216 : : {
16217 : : char *usename;
16218 : : char *umoptions;
16219 : :
16220 : 33 : usename = PQgetvalue(res, i, i_usename);
16221 : 33 : umoptions = PQgetvalue(res, i, i_umoptions);
16222 : :
16223 : 33 : resetPQExpBuffer(q);
16224 : 33 : appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
16225 : 33 : appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
16226 : :
16227 [ + - - + ]: 33 : if (umoptions && strlen(umoptions) > 0)
16228 : 0 : appendPQExpBuffer(q, " OPTIONS (\n %s\n)", umoptions);
16229 : :
16230 : 33 : appendPQExpBufferStr(q, ";\n");
16231 : :
16232 : 33 : resetPQExpBuffer(delq);
16233 : 33 : appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
16234 : 33 : appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
16235 : :
16236 : 33 : resetPQExpBuffer(tag);
16237 : 33 : appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
16238 : : usename, servername);
16239 : :
16240 : 33 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16241 : 33 : ARCHIVE_OPTS(.tag = tag->data,
16242 : : .namespace = namespace,
16243 : : .owner = owner,
16244 : : .description = "USER MAPPING",
16245 : : .section = SECTION_PRE_DATA,
16246 : : .createStmt = q->data,
16247 : : .dropStmt = delq->data));
16248 : : }
16249 : :
16250 : 49 : PQclear(res);
16251 : :
16252 : 49 : destroyPQExpBuffer(query);
16253 : 49 : destroyPQExpBuffer(delq);
16254 : 49 : destroyPQExpBuffer(tag);
16255 : 49 : destroyPQExpBuffer(q);
16256 : 49 : }
16257 : :
16258 : : /*
16259 : : * Write out default privileges information
16260 : : */
16261 : : static void
16262 : 170 : dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo)
16263 : : {
16264 : 170 : DumpOptions *dopt = fout->dopt;
16265 : : PQExpBuffer q;
16266 : : PQExpBuffer tag;
16267 : : const char *type;
16268 : :
16269 : : /* Do nothing if not dumping schema, or if we're skipping ACLs */
16270 [ + + + + ]: 170 : if (!dopt->dumpSchema || dopt->aclsSkip)
16271 : 30 : return;
16272 : :
16273 : 140 : q = createPQExpBuffer();
16274 : 140 : tag = createPQExpBuffer();
16275 : :
16276 [ + - + + : 140 : switch (daclinfo->defaclobjtype)
- - - ]
16277 : : {
16278 : 65 : case DEFACLOBJ_RELATION:
16279 : 65 : type = "TABLES";
16280 : 65 : break;
16281 : 0 : case DEFACLOBJ_SEQUENCE:
16282 : 0 : type = "SEQUENCES";
16283 : 0 : break;
16284 : 65 : case DEFACLOBJ_FUNCTION:
16285 : 65 : type = "FUNCTIONS";
16286 : 65 : break;
16287 : 10 : case DEFACLOBJ_TYPE:
16288 : 10 : type = "TYPES";
16289 : 10 : break;
16290 : 0 : case DEFACLOBJ_NAMESPACE:
16291 : 0 : type = "SCHEMAS";
16292 : 0 : break;
16293 : 0 : case DEFACLOBJ_LARGEOBJECT:
16294 : 0 : type = "LARGE OBJECTS";
16295 : 0 : break;
16296 : 0 : default:
16297 : : /* shouldn't get here */
16298 : 0 : pg_fatal("unrecognized object type in default privileges: %d",
16299 : : (int) daclinfo->defaclobjtype);
16300 : : type = ""; /* keep compiler quiet */
16301 : : }
16302 : :
16303 : 140 : appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
16304 : :
16305 : : /* build the actual command(s) for this tuple */
16306 [ - + ]: 140 : if (!buildDefaultACLCommands(type,
16307 : 140 : daclinfo->dobj.namespace != NULL ?
16308 : 66 : daclinfo->dobj.namespace->dobj.name : NULL,
16309 : 140 : daclinfo->dacl.acl,
16310 : 140 : daclinfo->dacl.acldefault,
16311 [ + + ]: 140 : daclinfo->defaclrole,
16312 : : fout->remoteVersion,
16313 : : q))
16314 : 0 : pg_fatal("could not parse default ACL list (%s)",
16315 : : daclinfo->dacl.acl);
16316 : :
16317 [ + - ]: 140 : if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
16318 : 140 : ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
16319 [ + + ]: 140 : ARCHIVE_OPTS(.tag = tag->data,
16320 : : .namespace = daclinfo->dobj.namespace ?
16321 : : daclinfo->dobj.namespace->dobj.name : NULL,
16322 : : .owner = daclinfo->defaclrole,
16323 : : .description = "DEFAULT ACL",
16324 : : .section = SECTION_POST_DATA,
16325 : : .createStmt = q->data));
16326 : :
16327 : 140 : destroyPQExpBuffer(tag);
16328 : 140 : destroyPQExpBuffer(q);
16329 : : }
16330 : :
16331 : : /*----------
16332 : : * Write out grant/revoke information
16333 : : *
16334 : : * 'objDumpId' is the dump ID of the underlying object.
16335 : : * 'altDumpId' can be a second dumpId that the ACL entry must also depend on,
16336 : : * or InvalidDumpId if there is no need for a second dependency.
16337 : : * 'type' must be one of
16338 : : * TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
16339 : : * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT.
16340 : : * 'name' is the formatted name of the object. Must be quoted etc. already.
16341 : : * 'subname' is the formatted name of the sub-object, if any. Must be quoted.
16342 : : * (Currently we assume that subname is only provided for table columns.)
16343 : : * 'nspname' is the namespace the object is in (NULL if none).
16344 : : * 'tag' is the tag to use for the ACL TOC entry; typically, this is NULL
16345 : : * to use the default for the object type.
16346 : : * 'owner' is the owner, NULL if there is no owner (for languages).
16347 : : * 'dacl' is the DumpableAcl struct for the object.
16348 : : *
16349 : : * Returns the dump ID assigned to the ACL TocEntry, or InvalidDumpId if
16350 : : * no ACL entry was created.
16351 : : *----------
16352 : : */
16353 : : static DumpId
16354 : 32342 : dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId,
16355 : : const char *type, const char *name, const char *subname,
16356 : : const char *nspname, const char *tag, const char *owner,
16357 : : const DumpableAcl *dacl)
16358 : : {
16359 : 32342 : DumpId aclDumpId = InvalidDumpId;
16360 : 32342 : DumpOptions *dopt = fout->dopt;
16361 : 32342 : const char *acls = dacl->acl;
16362 : 32342 : const char *acldefault = dacl->acldefault;
16363 : 32342 : char privtype = dacl->privtype;
16364 : 32342 : const char *initprivs = dacl->initprivs;
16365 : : const char *baseacls;
16366 : : PQExpBuffer sql;
16367 : :
16368 : : /* Do nothing if ACL dump is not enabled */
16369 [ + + ]: 32342 : if (dopt->aclsSkip)
16370 : 349 : return InvalidDumpId;
16371 : :
16372 : : /* --data-only skips ACLs *except* large object ACLs */
16373 [ + + + + ]: 31993 : if (!dopt->dumpSchema && strcmp(type, "LARGE OBJECT") != 0)
16374 : 1 : return InvalidDumpId;
16375 : :
16376 : 31992 : sql = createPQExpBuffer();
16377 : :
16378 : : /*
16379 : : * In binary upgrade mode, we don't run an extension's script but instead
16380 : : * dump out the objects independently and then recreate them. To preserve
16381 : : * any initial privileges which were set on extension objects, we need to
16382 : : * compute the set of GRANT and REVOKE commands necessary to get from the
16383 : : * default privileges of an object to its initial privileges as recorded
16384 : : * in pg_init_privs.
16385 : : *
16386 : : * At restore time, we apply these commands after having called
16387 : : * binary_upgrade_set_record_init_privs(true). That tells the backend to
16388 : : * copy the results into pg_init_privs. This is how we preserve the
16389 : : * contents of that catalog across binary upgrades.
16390 : : */
16391 [ + + + + : 31992 : if (dopt->binary_upgrade && privtype == 'e' &&
+ - ]
16392 [ + - ]: 13 : initprivs && *initprivs != '\0')
16393 : : {
16394 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
16395 [ - + ]: 13 : if (!buildACLCommands(name, subname, nspname, type,
16396 : : initprivs, acldefault, owner,
16397 : : "", fout->remoteVersion, sql))
16398 : 0 : pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
16399 : : initprivs, acldefault, name, type);
16400 : 13 : appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
16401 : : }
16402 : :
16403 : : /*
16404 : : * Now figure the GRANT and REVOKE commands needed to get to the object's
16405 : : * actual current ACL, starting from the initprivs if given, else from the
16406 : : * object-type-specific default. Also, while buildACLCommands will assume
16407 : : * that a NULL/empty acls string means it needn't do anything, what that
16408 : : * actually represents is the object-type-specific default; so we need to
16409 : : * substitute the acldefault string to get the right results in that case.
16410 : : */
16411 [ + + + + ]: 31992 : if (initprivs && *initprivs != '\0')
16412 : : {
16413 : 30077 : baseacls = initprivs;
16414 [ + - + + ]: 30077 : if (acls == NULL || *acls == '\0')
16415 : 17 : acls = acldefault;
16416 : : }
16417 : : else
16418 : 1915 : baseacls = acldefault;
16419 : :
16420 [ - + ]: 31992 : if (!buildACLCommands(name, subname, nspname, type,
16421 : : acls, baseacls, owner,
16422 : : "", fout->remoteVersion, sql))
16423 : 0 : pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
16424 : : acls, baseacls, name, type);
16425 : :
16426 [ + + ]: 31992 : if (sql->len > 0)
16427 : : {
16428 : 1972 : PQExpBuffer tagbuf = createPQExpBuffer();
16429 : : DumpId aclDeps[2];
16430 : 1972 : int nDeps = 0;
16431 : :
16432 [ - + ]: 1972 : if (tag)
16433 : 0 : appendPQExpBufferStr(tagbuf, tag);
16434 [ + + ]: 1972 : else if (subname)
16435 : 1111 : appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname);
16436 : : else
16437 : 861 : appendPQExpBuffer(tagbuf, "%s %s", type, name);
16438 : :
16439 : 1972 : aclDeps[nDeps++] = objDumpId;
16440 [ + + ]: 1972 : if (altDumpId != InvalidDumpId)
16441 : 1025 : aclDeps[nDeps++] = altDumpId;
16442 : :
16443 : 1972 : aclDumpId = createDumpId();
16444 : :
16445 : 1972 : ArchiveEntry(fout, nilCatalogId, aclDumpId,
16446 : 1972 : ARCHIVE_OPTS(.tag = tagbuf->data,
16447 : : .namespace = nspname,
16448 : : .owner = owner,
16449 : : .description = "ACL",
16450 : : .section = SECTION_NONE,
16451 : : .createStmt = sql->data,
16452 : : .deps = aclDeps,
16453 : : .nDeps = nDeps));
16454 : :
16455 : 1972 : destroyPQExpBuffer(tagbuf);
16456 : : }
16457 : :
16458 : 31992 : destroyPQExpBuffer(sql);
16459 : :
16460 : 31992 : return aclDumpId;
16461 : : }
16462 : :
16463 : : /*
16464 : : * dumpSecLabel
16465 : : *
16466 : : * This routine is used to dump any security labels associated with the
16467 : : * object handed to this routine. The routine takes the object type
16468 : : * and object name (ready to print, except for schema decoration), plus
16469 : : * the namespace and owner of the object (for labeling the ArchiveEntry),
16470 : : * plus catalog ID and subid which are the lookup key for pg_seclabel,
16471 : : * plus the dump ID for the object (for setting a dependency).
16472 : : * If a matching pg_seclabel entry is found, it is dumped.
16473 : : *
16474 : : * Note: although this routine takes a dumpId for dependency purposes,
16475 : : * that purpose is just to mark the dependency in the emitted dump file
16476 : : * for possible future use by pg_restore. We do NOT use it for determining
16477 : : * ordering of the label in the dump file, because this routine is called
16478 : : * after dependency sorting occurs. This routine should be called just after
16479 : : * calling ArchiveEntry() for the specified object.
16480 : : */
16481 : : static void
16482 : 10 : dumpSecLabel(Archive *fout, const char *type, const char *name,
16483 : : const char *namespace, const char *owner,
16484 : : CatalogId catalogId, int subid, DumpId dumpId)
16485 : : {
16486 : 10 : DumpOptions *dopt = fout->dopt;
16487 : : SecLabelItem *labels;
16488 : : int nlabels;
16489 : : int i;
16490 : : PQExpBuffer query;
16491 : :
16492 : : /* do nothing, if --no-security-labels is supplied */
16493 [ - + ]: 10 : if (dopt->no_security_labels)
16494 : 0 : return;
16495 : :
16496 : : /*
16497 : : * Security labels are schema not data ... except large object labels are
16498 : : * data
16499 : : */
16500 [ - + ]: 10 : if (strcmp(type, "LARGE OBJECT") != 0)
16501 : : {
16502 [ # # ]: 0 : if (!dopt->dumpSchema)
16503 : 0 : return;
16504 : : }
16505 : : else
16506 : : {
16507 : : /* We do dump large object security labels in binary-upgrade mode */
16508 [ + - - + ]: 10 : if (!dopt->dumpData && !dopt->binary_upgrade)
16509 : 0 : return;
16510 : : }
16511 : :
16512 : : /* Search for security labels associated with catalogId, using table */
16513 : 10 : nlabels = findSecLabels(catalogId.tableoid, catalogId.oid, &labels);
16514 : :
16515 : 10 : query = createPQExpBuffer();
16516 : :
16517 [ + + ]: 15 : for (i = 0; i < nlabels; i++)
16518 : : {
16519 : : /*
16520 : : * Ignore label entries for which the subid doesn't match.
16521 : : */
16522 [ - + ]: 5 : if (labels[i].objsubid != subid)
16523 : 0 : continue;
16524 : :
16525 : 5 : appendPQExpBuffer(query,
16526 : : "SECURITY LABEL FOR %s ON %s ",
16527 : 5 : fmtId(labels[i].provider), type);
16528 [ - + - - ]: 5 : if (namespace && *namespace)
16529 : 0 : appendPQExpBuffer(query, "%s.", fmtId(namespace));
16530 : 5 : appendPQExpBuffer(query, "%s IS ", name);
16531 : 5 : appendStringLiteralAH(query, labels[i].label, fout);
16532 : 5 : appendPQExpBufferStr(query, ";\n");
16533 : : }
16534 : :
16535 [ + + ]: 10 : if (query->len > 0)
16536 : : {
16537 : 5 : PQExpBuffer tag = createPQExpBuffer();
16538 : :
16539 : 5 : appendPQExpBuffer(tag, "%s %s", type, name);
16540 : 5 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16541 : 5 : ARCHIVE_OPTS(.tag = tag->data,
16542 : : .namespace = namespace,
16543 : : .owner = owner,
16544 : : .description = "SECURITY LABEL",
16545 : : .section = SECTION_NONE,
16546 : : .createStmt = query->data,
16547 : : .deps = &dumpId,
16548 : : .nDeps = 1));
16549 : 5 : destroyPQExpBuffer(tag);
16550 : : }
16551 : :
16552 : 10 : destroyPQExpBuffer(query);
16553 : : }
16554 : :
16555 : : /*
16556 : : * dumpTableSecLabel
16557 : : *
16558 : : * As above, but dump security label for both the specified table (or view)
16559 : : * and its columns.
16560 : : */
16561 : : static void
16562 : 0 : dumpTableSecLabel(Archive *fout, const TableInfo *tbinfo, const char *reltypename)
16563 : : {
16564 : 0 : DumpOptions *dopt = fout->dopt;
16565 : : SecLabelItem *labels;
16566 : : int nlabels;
16567 : : int i;
16568 : : PQExpBuffer query;
16569 : : PQExpBuffer target;
16570 : :
16571 : : /* do nothing, if --no-security-labels is supplied */
16572 [ # # ]: 0 : if (dopt->no_security_labels)
16573 : 0 : return;
16574 : :
16575 : : /* SecLabel are SCHEMA not data */
16576 [ # # ]: 0 : if (!dopt->dumpSchema)
16577 : 0 : return;
16578 : :
16579 : : /* Search for comments associated with relation, using table */
16580 : 0 : nlabels = findSecLabels(tbinfo->dobj.catId.tableoid,
16581 : 0 : tbinfo->dobj.catId.oid,
16582 : : &labels);
16583 : :
16584 : : /* If security labels exist, build SECURITY LABEL statements */
16585 [ # # ]: 0 : if (nlabels <= 0)
16586 : 0 : return;
16587 : :
16588 : 0 : query = createPQExpBuffer();
16589 : 0 : target = createPQExpBuffer();
16590 : :
16591 [ # # ]: 0 : for (i = 0; i < nlabels; i++)
16592 : : {
16593 : : const char *colname;
16594 : 0 : const char *provider = labels[i].provider;
16595 : 0 : const char *label = labels[i].label;
16596 : 0 : int objsubid = labels[i].objsubid;
16597 : :
16598 : 0 : resetPQExpBuffer(target);
16599 [ # # ]: 0 : if (objsubid == 0)
16600 : : {
16601 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16602 : 0 : fmtQualifiedDumpable(tbinfo));
16603 : : }
16604 : : else
16605 : : {
16606 : 0 : colname = getAttrName(objsubid, tbinfo);
16607 : : /* first fmtXXX result must be consumed before calling again */
16608 : 0 : appendPQExpBuffer(target, "COLUMN %s",
16609 : 0 : fmtQualifiedDumpable(tbinfo));
16610 : 0 : appendPQExpBuffer(target, ".%s", fmtId(colname));
16611 : : }
16612 : 0 : appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ",
16613 : : fmtId(provider), target->data);
16614 : 0 : appendStringLiteralAH(query, label, fout);
16615 : 0 : appendPQExpBufferStr(query, ";\n");
16616 : : }
16617 [ # # ]: 0 : if (query->len > 0)
16618 : : {
16619 : 0 : resetPQExpBuffer(target);
16620 : 0 : appendPQExpBuffer(target, "%s %s", reltypename,
16621 : 0 : fmtId(tbinfo->dobj.name));
16622 : 0 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
16623 : 0 : ARCHIVE_OPTS(.tag = target->data,
16624 : : .namespace = tbinfo->dobj.namespace->dobj.name,
16625 : : .owner = tbinfo->rolname,
16626 : : .description = "SECURITY LABEL",
16627 : : .section = SECTION_NONE,
16628 : : .createStmt = query->data,
16629 : : .deps = &(tbinfo->dobj.dumpId),
16630 : : .nDeps = 1));
16631 : : }
16632 : 0 : destroyPQExpBuffer(query);
16633 : 0 : destroyPQExpBuffer(target);
16634 : : }
16635 : :
16636 : : /*
16637 : : * findSecLabels
16638 : : *
16639 : : * Find the security label(s), if any, associated with the given object.
16640 : : * All the objsubid values associated with the given classoid/objoid are
16641 : : * found with one search.
16642 : : */
16643 : : static int
16644 : 10 : findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items)
16645 : : {
16646 : 10 : SecLabelItem *middle = NULL;
16647 : : SecLabelItem *low;
16648 : : SecLabelItem *high;
16649 : : int nmatch;
16650 : :
16651 [ - + ]: 10 : if (nseclabels <= 0) /* no labels, so no match is possible */
16652 : : {
16653 : 0 : *items = NULL;
16654 : 0 : return 0;
16655 : : }
16656 : :
16657 : : /*
16658 : : * Do binary search to find some item matching the object.
16659 : : */
16660 : 10 : low = &seclabels[0];
16661 : 10 : high = &seclabels[nseclabels - 1];
16662 [ + + ]: 15 : while (low <= high)
16663 : : {
16664 : 10 : middle = low + (high - low) / 2;
16665 : :
16666 [ - + ]: 10 : if (classoid < middle->classoid)
16667 : 0 : high = middle - 1;
16668 [ - + ]: 10 : else if (classoid > middle->classoid)
16669 : 0 : low = middle + 1;
16670 [ + + ]: 10 : else if (objoid < middle->objoid)
16671 : 5 : high = middle - 1;
16672 [ - + ]: 5 : else if (objoid > middle->objoid)
16673 : 0 : low = middle + 1;
16674 : : else
16675 : 5 : break; /* found a match */
16676 : : }
16677 : :
16678 [ + + ]: 10 : if (low > high) /* no matches */
16679 : : {
16680 : 5 : *items = NULL;
16681 : 5 : return 0;
16682 : : }
16683 : :
16684 : : /*
16685 : : * Now determine how many items match the object. The search loop
16686 : : * invariant still holds: only items between low and high inclusive could
16687 : : * match.
16688 : : */
16689 : 5 : nmatch = 1;
16690 [ - + ]: 5 : while (middle > low)
16691 : : {
16692 [ # # ]: 0 : if (classoid != middle[-1].classoid ||
16693 [ # # ]: 0 : objoid != middle[-1].objoid)
16694 : : break;
16695 : 0 : middle--;
16696 : 0 : nmatch++;
16697 : : }
16698 : :
16699 : 5 : *items = middle;
16700 : :
16701 : 5 : middle += nmatch;
16702 [ - + ]: 5 : while (middle <= high)
16703 : : {
16704 [ # # ]: 0 : if (classoid != middle->classoid ||
16705 [ # # ]: 0 : objoid != middle->objoid)
16706 : : break;
16707 : 0 : middle++;
16708 : 0 : nmatch++;
16709 : : }
16710 : :
16711 : 5 : return nmatch;
16712 : : }
16713 : :
16714 : : /*
16715 : : * collectSecLabels
16716 : : *
16717 : : * Construct a table of all security labels available for database objects;
16718 : : * also set the has-seclabel component flag for each relevant object.
16719 : : *
16720 : : * The table is sorted by classoid/objid/objsubid for speed in lookup.
16721 : : */
16722 : : static void
16723 : 193 : collectSecLabels(Archive *fout)
16724 : : {
16725 : : PGresult *res;
16726 : : PQExpBuffer query;
16727 : : int i_label;
16728 : : int i_provider;
16729 : : int i_classoid;
16730 : : int i_objoid;
16731 : : int i_objsubid;
16732 : : int ntups;
16733 : : int i;
16734 : : DumpableObject *dobj;
16735 : :
16736 : 193 : query = createPQExpBuffer();
16737 : :
16738 : 193 : appendPQExpBufferStr(query,
16739 : : "SELECT label, provider, classoid, objoid, objsubid "
16740 : : "FROM pg_catalog.pg_seclabels "
16741 : : "ORDER BY classoid, objoid, objsubid");
16742 : :
16743 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16744 : :
16745 : : /* Construct lookup table containing OIDs in numeric form */
16746 : 193 : i_label = PQfnumber(res, "label");
16747 : 193 : i_provider = PQfnumber(res, "provider");
16748 : 193 : i_classoid = PQfnumber(res, "classoid");
16749 : 193 : i_objoid = PQfnumber(res, "objoid");
16750 : 193 : i_objsubid = PQfnumber(res, "objsubid");
16751 : :
16752 : 193 : ntups = PQntuples(res);
16753 : :
16754 : 193 : seclabels = pg_malloc_array(SecLabelItem, ntups);
16755 : 193 : nseclabels = 0;
16756 : 193 : dobj = NULL;
16757 : :
16758 [ + + ]: 198 : for (i = 0; i < ntups; i++)
16759 : : {
16760 : : CatalogId objId;
16761 : : int subid;
16762 : :
16763 : 5 : objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
16764 : 5 : objId.oid = atooid(PQgetvalue(res, i, i_objoid));
16765 : 5 : subid = atoi(PQgetvalue(res, i, i_objsubid));
16766 : :
16767 : : /* We needn't remember labels that don't match any dumpable object */
16768 [ - + ]: 5 : if (dobj == NULL ||
16769 [ # # ]: 0 : dobj->catId.tableoid != objId.tableoid ||
16770 [ # # ]: 0 : dobj->catId.oid != objId.oid)
16771 : 5 : dobj = findObjectByCatalogId(objId);
16772 [ - + ]: 5 : if (dobj == NULL)
16773 : 0 : continue;
16774 : :
16775 : : /*
16776 : : * Labels on columns of composite types are linked to the type's
16777 : : * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
16778 : : * in the type's own DumpableObject.
16779 : : */
16780 [ - + - - ]: 5 : if (subid != 0 && dobj->objType == DO_TABLE &&
16781 [ # # ]: 0 : ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
16782 : 0 : {
16783 : : TypeInfo *cTypeInfo;
16784 : :
16785 : 0 : cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
16786 [ # # ]: 0 : if (cTypeInfo)
16787 : 0 : cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
16788 : : }
16789 : : else
16790 : 5 : dobj->components |= DUMP_COMPONENT_SECLABEL;
16791 : :
16792 : 5 : seclabels[nseclabels].label = pg_strdup(PQgetvalue(res, i, i_label));
16793 : 5 : seclabels[nseclabels].provider = pg_strdup(PQgetvalue(res, i, i_provider));
16794 : 5 : seclabels[nseclabels].classoid = objId.tableoid;
16795 : 5 : seclabels[nseclabels].objoid = objId.oid;
16796 : 5 : seclabels[nseclabels].objsubid = subid;
16797 : 5 : nseclabels++;
16798 : : }
16799 : :
16800 : 193 : PQclear(res);
16801 : 193 : destroyPQExpBuffer(query);
16802 : 193 : }
16803 : :
16804 : : /*
16805 : : * dumpTable
16806 : : * write out to fout the declarations (not data) of a user-defined table
16807 : : */
16808 : : static void
16809 : 34527 : dumpTable(Archive *fout, const TableInfo *tbinfo)
16810 : : {
16811 : 34527 : DumpOptions *dopt = fout->dopt;
16812 : 34527 : DumpId tableAclDumpId = InvalidDumpId;
16813 : : char *namecopy;
16814 : :
16815 : : /* Do nothing if not dumping schema */
16816 [ + + ]: 34527 : if (!dopt->dumpSchema)
16817 : 1671 : return;
16818 : :
16819 [ + + ]: 32856 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16820 : : {
16821 [ + + ]: 7269 : if (tbinfo->relkind == RELKIND_SEQUENCE)
16822 : 381 : dumpSequence(fout, tbinfo);
16823 : : else
16824 : 6888 : dumpTableSchema(fout, tbinfo);
16825 : : }
16826 : :
16827 : : /* Handle the ACL here */
16828 : 32856 : namecopy = pg_strdup(fmtId(tbinfo->dobj.name));
16829 [ + + ]: 32856 : if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
16830 : : {
16831 : : const char *objtype;
16832 : :
16833 [ + + + ]: 26419 : switch (tbinfo->relkind)
16834 : : {
16835 : 90 : case RELKIND_SEQUENCE:
16836 : 90 : objtype = "SEQUENCE";
16837 : 90 : break;
16838 : 39 : case RELKIND_PROPGRAPH:
16839 : 39 : objtype = "PROPERTY GRAPH";
16840 : 39 : break;
16841 : 26290 : default:
16842 : 26290 : objtype = "TABLE";
16843 : 26290 : break;
16844 : : }
16845 : :
16846 : : tableAclDumpId =
16847 : 26419 : dumpACL(fout, tbinfo->dobj.dumpId, InvalidDumpId,
16848 : : objtype, namecopy, NULL,
16849 : 26419 : tbinfo->dobj.namespace->dobj.name,
16850 : 26419 : NULL, tbinfo->rolname, &tbinfo->dacl);
16851 : : }
16852 : :
16853 : : /*
16854 : : * Handle column ACLs, if any. Note: we pull these with a separate query
16855 : : * rather than trying to fetch them during getTableAttrs, so that we won't
16856 : : * miss ACLs on system columns. Doing it this way also allows us to dump
16857 : : * ACLs for catalogs that we didn't mark "interesting" back in getTables.
16858 : : */
16859 [ + + + + ]: 32856 : if ((tbinfo->dobj.dump & DUMP_COMPONENT_ACL) && tbinfo->hascolumnACLs)
16860 : : {
16861 : 293 : PQExpBuffer query = createPQExpBuffer();
16862 : : PGresult *res;
16863 : : int i;
16864 : :
16865 [ + + ]: 293 : if (!fout->is_prepared[PREPQUERY_GETCOLUMNACLS])
16866 : : {
16867 : : /* Set up query for column ACLs */
16868 : 166 : appendPQExpBufferStr(query,
16869 : : "PREPARE getColumnACLs(pg_catalog.oid) AS\n");
16870 : :
16871 : : /*
16872 : : * In principle we should call acldefault('c', relowner) to get
16873 : : * the default ACL for a column. However, we don't currently
16874 : : * store the numeric OID of the relowner in TableInfo. We could
16875 : : * convert the owner name using regrole, but that creates a risk
16876 : : * of failure due to concurrent role renames. Given that the
16877 : : * default ACL for columns is empty and is likely to stay that
16878 : : * way, it's not worth extra cycles and risk to avoid hard-wiring
16879 : : * that knowledge here.
16880 : : */
16881 : 166 : appendPQExpBufferStr(query,
16882 : : "SELECT at.attname, "
16883 : : "at.attacl, "
16884 : : "'{}' AS acldefault, "
16885 : : "pip.privtype, pip.initprivs "
16886 : : "FROM pg_catalog.pg_attribute at "
16887 : : "LEFT JOIN pg_catalog.pg_init_privs pip ON "
16888 : : "(at.attrelid = pip.objoid "
16889 : : "AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass "
16890 : : "AND at.attnum = pip.objsubid) "
16891 : : "WHERE at.attrelid = $1 AND "
16892 : : "NOT at.attisdropped "
16893 : : "AND (at.attacl IS NOT NULL OR pip.initprivs IS NOT NULL) "
16894 : : "ORDER BY at.attnum");
16895 : :
16896 : 166 : ExecuteSqlStatement(fout, query->data);
16897 : :
16898 : 166 : fout->is_prepared[PREPQUERY_GETCOLUMNACLS] = true;
16899 : : }
16900 : :
16901 : 293 : printfPQExpBuffer(query,
16902 : : "EXECUTE getColumnACLs('%u')",
16903 : 293 : tbinfo->dobj.catId.oid);
16904 : :
16905 : 293 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16906 : :
16907 [ + + ]: 5316 : for (i = 0; i < PQntuples(res); i++)
16908 : : {
16909 : 5023 : char *attname = PQgetvalue(res, i, 0);
16910 : 5023 : char *attacl = PQgetvalue(res, i, 1);
16911 : 5023 : char *acldefault = PQgetvalue(res, i, 2);
16912 : 5023 : char privtype = *(PQgetvalue(res, i, 3));
16913 : 5023 : char *initprivs = PQgetvalue(res, i, 4);
16914 : : DumpableAcl coldacl;
16915 : : char *attnamecopy;
16916 : :
16917 : 5023 : coldacl.acl = attacl;
16918 : 5023 : coldacl.acldefault = acldefault;
16919 : 5023 : coldacl.privtype = privtype;
16920 : 5023 : coldacl.initprivs = initprivs;
16921 : 5023 : attnamecopy = pg_strdup(fmtId(attname));
16922 : :
16923 : : /*
16924 : : * Column's GRANT type is always TABLE. Each column ACL depends
16925 : : * on the table-level ACL, since we can restore column ACLs in
16926 : : * parallel but the table-level ACL has to be done first.
16927 : : */
16928 : 5023 : dumpACL(fout, tbinfo->dobj.dumpId, tableAclDumpId,
16929 : : "TABLE", namecopy, attnamecopy,
16930 : 5023 : tbinfo->dobj.namespace->dobj.name,
16931 : 5023 : NULL, tbinfo->rolname, &coldacl);
16932 : 5023 : pg_free(attnamecopy);
16933 : : }
16934 : 293 : PQclear(res);
16935 : 293 : destroyPQExpBuffer(query);
16936 : : }
16937 : :
16938 : 32856 : pg_free(namecopy);
16939 : : }
16940 : :
16941 : : /*
16942 : : * Create the AS clause for a view or materialized view. The semicolon is
16943 : : * stripped because a materialized view must add a WITH NO DATA clause.
16944 : : *
16945 : : * This returns a new buffer which must be freed by the caller.
16946 : : */
16947 : : static PQExpBuffer
16948 : 926 : createViewAsClause(Archive *fout, const TableInfo *tbinfo)
16949 : : {
16950 : 926 : PQExpBuffer query = createPQExpBuffer();
16951 : 926 : PQExpBuffer result = createPQExpBuffer();
16952 : : PGresult *res;
16953 : : int len;
16954 : :
16955 : : /* Fetch the view definition */
16956 : 926 : appendPQExpBuffer(query,
16957 : : "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
16958 : 926 : tbinfo->dobj.catId.oid);
16959 : :
16960 : 926 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16961 : :
16962 [ - + ]: 926 : if (PQntuples(res) != 1)
16963 : : {
16964 [ # # ]: 0 : if (PQntuples(res) < 1)
16965 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned no data",
16966 : : tbinfo->dobj.name);
16967 : : else
16968 : 0 : pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
16969 : : tbinfo->dobj.name);
16970 : : }
16971 : :
16972 : 926 : len = PQgetlength(res, 0, 0);
16973 : :
16974 [ - + ]: 926 : if (len == 0)
16975 : 0 : pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
16976 : : tbinfo->dobj.name);
16977 : :
16978 : : /* Strip off the trailing semicolon so that other things may follow. */
16979 : : Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
16980 : 926 : appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
16981 : :
16982 : 926 : PQclear(res);
16983 : 926 : destroyPQExpBuffer(query);
16984 : :
16985 : 926 : return result;
16986 : : }
16987 : :
16988 : : /*
16989 : : * Create a dummy AS clause for a view. This is used when the real view
16990 : : * definition has to be postponed because of circular dependencies.
16991 : : * We must duplicate the view's external properties -- column names and types
16992 : : * (including collation) -- so that it works for subsequent references.
16993 : : *
16994 : : * This returns a new buffer which must be freed by the caller.
16995 : : */
16996 : : static PQExpBuffer
16997 : 20 : createDummyViewAsClause(Archive *fout, const TableInfo *tbinfo)
16998 : : {
16999 : 20 : PQExpBuffer result = createPQExpBuffer();
17000 : : int j;
17001 : :
17002 : 20 : appendPQExpBufferStr(result, "SELECT");
17003 : :
17004 [ + + ]: 40 : for (j = 0; j < tbinfo->numatts; j++)
17005 : : {
17006 [ + + ]: 20 : if (j > 0)
17007 : 10 : appendPQExpBufferChar(result, ',');
17008 : 20 : appendPQExpBufferStr(result, "\n ");
17009 : :
17010 : 20 : appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
17011 : :
17012 : : /*
17013 : : * Must add collation if not default for the type, because CREATE OR
17014 : : * REPLACE VIEW won't change it
17015 : : */
17016 [ - + ]: 20 : if (OidIsValid(tbinfo->attcollation[j]))
17017 : : {
17018 : : CollInfo *coll;
17019 : :
17020 : 0 : coll = findCollationByOid(tbinfo->attcollation[j]);
17021 [ # # ]: 0 : if (coll)
17022 : 0 : appendPQExpBuffer(result, " COLLATE %s",
17023 : 0 : fmtQualifiedDumpable(coll));
17024 : : }
17025 : :
17026 : 20 : appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
17027 : : }
17028 : :
17029 : 20 : return result;
17030 : : }
17031 : :
17032 : : /*
17033 : : * dumpTableSchema
17034 : : * write the declaration (not data) of one user-defined table or view
17035 : : */
17036 : : static void
17037 : 6888 : dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
17038 : : {
17039 : 6888 : DumpOptions *dopt = fout->dopt;
17040 : 6888 : PQExpBuffer q = createPQExpBuffer();
17041 : 6888 : PQExpBuffer delq = createPQExpBuffer();
17042 : 6888 : PQExpBuffer extra = createPQExpBuffer();
17043 : : char *qrelname;
17044 : : char *qualrelname;
17045 : : int numParents;
17046 : : TableInfo **parents;
17047 : : int actual_atts; /* number of attrs in this CREATE statement */
17048 : : const char *reltypename;
17049 : : char *storage;
17050 : : int j,
17051 : : k;
17052 : :
17053 : : /* We had better have loaded per-column details about this table */
17054 : : Assert(tbinfo->interesting);
17055 : :
17056 : 6888 : qrelname = pg_strdup(fmtId(tbinfo->dobj.name));
17057 : 6888 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
17058 : :
17059 [ - + ]: 6888 : if (tbinfo->hasoids)
17060 : 0 : pg_log_warning("WITH OIDS is not supported anymore (table \"%s\")",
17061 : : qrelname);
17062 : :
17063 [ + + ]: 6888 : if (dopt->binary_upgrade)
17064 : 954 : binary_upgrade_set_type_oids_by_rel(fout, q, tbinfo);
17065 : :
17066 : : /* Is it a table or a view? */
17067 [ + + ]: 6888 : if (tbinfo->relkind == RELKIND_VIEW)
17068 : : {
17069 : : PQExpBuffer result;
17070 : :
17071 : : /*
17072 : : * Note: keep this code in sync with the is_view case in dumpRule()
17073 : : */
17074 : :
17075 : 573 : reltypename = "VIEW";
17076 : :
17077 [ + + ]: 573 : if (dopt->binary_upgrade)
17078 : 56 : binary_upgrade_set_pg_class_oids(fout, q,
17079 : 56 : tbinfo->dobj.catId.oid);
17080 : :
17081 : 573 : appendPQExpBuffer(q, "CREATE VIEW %s", qualrelname);
17082 : :
17083 [ + + ]: 573 : if (tbinfo->dummy_view)
17084 : 10 : result = createDummyViewAsClause(fout, tbinfo);
17085 : : else
17086 : : {
17087 [ + + ]: 563 : if (nonemptyReloptions(tbinfo->reloptions))
17088 : : {
17089 : 63 : appendPQExpBufferStr(q, " WITH (");
17090 : 63 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17091 : 63 : appendPQExpBufferChar(q, ')');
17092 : : }
17093 : 563 : result = createViewAsClause(fout, tbinfo);
17094 : : }
17095 : 573 : appendPQExpBuffer(q, " AS\n%s", result->data);
17096 : 573 : destroyPQExpBuffer(result);
17097 : :
17098 [ + + + - ]: 573 : if (tbinfo->checkoption != NULL && !tbinfo->dummy_view)
17099 : 34 : appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption);
17100 : 573 : appendPQExpBufferStr(q, ";\n");
17101 : : }
17102 [ + + ]: 6315 : else if (tbinfo->relkind == RELKIND_PROPGRAPH)
17103 : : {
17104 : 104 : PQExpBuffer query = createPQExpBuffer();
17105 : : PGresult *res;
17106 : : int len;
17107 : :
17108 : 104 : reltypename = "PROPERTY GRAPH";
17109 : :
17110 [ + + ]: 104 : if (dopt->binary_upgrade)
17111 : 15 : binary_upgrade_set_pg_class_oids(fout, q,
17112 : 15 : tbinfo->dobj.catId.oid);
17113 : :
17114 : 104 : appendPQExpBuffer(query,
17115 : : "SELECT pg_catalog.pg_get_propgraphdef('%u'::pg_catalog.oid) AS pgdef",
17116 : 104 : tbinfo->dobj.catId.oid);
17117 : :
17118 : 104 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17119 : :
17120 [ - + ]: 104 : if (PQntuples(res) != 1)
17121 : : {
17122 [ # # ]: 0 : if (PQntuples(res) < 1)
17123 : 0 : pg_fatal("query to obtain definition of property graph \"%s\" returned no data",
17124 : : tbinfo->dobj.name);
17125 : : else
17126 : 0 : pg_fatal("query to obtain definition of property graph \"%s\" returned more than one definition",
17127 : : tbinfo->dobj.name);
17128 : : }
17129 : :
17130 : 104 : len = PQgetlength(res, 0, 0);
17131 : :
17132 [ - + ]: 104 : if (len == 0)
17133 : 0 : pg_fatal("definition of property graph \"%s\" appears to be empty (length zero)",
17134 : : tbinfo->dobj.name);
17135 : :
17136 : 104 : appendPQExpBufferStr(q, PQgetvalue(res, 0, 0));
17137 : :
17138 : 104 : PQclear(res);
17139 : 104 : destroyPQExpBuffer(query);
17140 : :
17141 : 104 : appendPQExpBufferStr(q, ";\n");
17142 : : }
17143 : : else
17144 : : {
17145 : 6211 : char *partkeydef = NULL;
17146 : 6211 : char *ftoptions = NULL;
17147 : 6211 : char *srvname = NULL;
17148 : 6211 : const char *foreign = "";
17149 : :
17150 : : /*
17151 : : * Set reltypename, and collect any relkind-specific data that we
17152 : : * didn't fetch during getTables().
17153 : : */
17154 [ + + + + ]: 6211 : switch (tbinfo->relkind)
17155 : : {
17156 : 609 : case RELKIND_PARTITIONED_TABLE:
17157 : : {
17158 : 609 : PQExpBuffer query = createPQExpBuffer();
17159 : : PGresult *res;
17160 : :
17161 : 609 : reltypename = "TABLE";
17162 : :
17163 : : /* retrieve partition key definition */
17164 : 609 : appendPQExpBuffer(query,
17165 : : "SELECT pg_get_partkeydef('%u')",
17166 : 609 : tbinfo->dobj.catId.oid);
17167 : 609 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17168 : 609 : partkeydef = pg_strdup(PQgetvalue(res, 0, 0));
17169 : 609 : PQclear(res);
17170 : 609 : destroyPQExpBuffer(query);
17171 : 609 : break;
17172 : : }
17173 : 36 : case RELKIND_FOREIGN_TABLE:
17174 : : {
17175 : 36 : PQExpBuffer query = createPQExpBuffer();
17176 : : PGresult *res;
17177 : : int i_srvname;
17178 : : int i_ftoptions;
17179 : :
17180 : 36 : reltypename = "FOREIGN TABLE";
17181 : :
17182 : : /* retrieve name of foreign server and generic options */
17183 : 36 : appendPQExpBuffer(query,
17184 : : "SELECT fs.srvname, "
17185 : : "pg_catalog.array_to_string(ARRAY("
17186 : : "SELECT pg_catalog.quote_ident(option_name) || "
17187 : : "' ' || pg_catalog.quote_literal(option_value) "
17188 : : "FROM pg_catalog.pg_options_to_table(ftoptions) "
17189 : : "ORDER BY option_name"
17190 : : "), E',\n ') AS ftoptions "
17191 : : "FROM pg_catalog.pg_foreign_table ft "
17192 : : "JOIN pg_catalog.pg_foreign_server fs "
17193 : : "ON (fs.oid = ft.ftserver) "
17194 : : "WHERE ft.ftrelid = '%u'",
17195 : 36 : tbinfo->dobj.catId.oid);
17196 : 36 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
17197 : 36 : i_srvname = PQfnumber(res, "srvname");
17198 : 36 : i_ftoptions = PQfnumber(res, "ftoptions");
17199 : 36 : srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
17200 : 36 : ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
17201 : 36 : PQclear(res);
17202 : 36 : destroyPQExpBuffer(query);
17203 : :
17204 : 36 : foreign = "FOREIGN ";
17205 : 36 : break;
17206 : : }
17207 : 353 : case RELKIND_MATVIEW:
17208 : 353 : reltypename = "MATERIALIZED VIEW";
17209 : 353 : break;
17210 : 5213 : default:
17211 : 5213 : reltypename = "TABLE";
17212 : 5213 : break;
17213 : : }
17214 : :
17215 : 6211 : numParents = tbinfo->numParents;
17216 : 6211 : parents = tbinfo->parents;
17217 : :
17218 [ + + ]: 6211 : if (dopt->binary_upgrade)
17219 : 883 : binary_upgrade_set_pg_class_oids(fout, q,
17220 : 883 : tbinfo->dobj.catId.oid);
17221 : :
17222 : : /*
17223 : : * PostgreSQL 18 has disabled UNLOGGED for partitioned tables, so
17224 : : * ignore it when dumping if it was set in this case.
17225 : : */
17226 : 6211 : appendPQExpBuffer(q, "CREATE %s%s %s",
17227 [ + + ]: 6211 : (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
17228 [ + - ]: 20 : tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ?
17229 : : "UNLOGGED " : "",
17230 : : reltypename,
17231 : : qualrelname);
17232 : :
17233 : : /*
17234 : : * Attach to type, if reloftype; except in case of a binary upgrade,
17235 : : * we dump the table normally and attach it to the type afterward.
17236 : : */
17237 [ + + + + ]: 6211 : if (OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade)
17238 : 24 : appendPQExpBuffer(q, " OF %s",
17239 : 24 : getFormattedTypeName(fout, tbinfo->reloftype,
17240 : : zeroIsError));
17241 : :
17242 [ + + ]: 6211 : if (tbinfo->relkind != RELKIND_MATVIEW)
17243 : : {
17244 : : /* Dump the attributes */
17245 : 5858 : actual_atts = 0;
17246 [ + + ]: 27122 : for (j = 0; j < tbinfo->numatts; j++)
17247 : : {
17248 : : /*
17249 : : * Normally, dump if it's locally defined in this table, and
17250 : : * not dropped. But for binary upgrade, we'll dump all the
17251 : : * columns, and then fix up the dropped and nonlocal cases
17252 : : * below.
17253 : : */
17254 [ + + ]: 21264 : if (shouldPrintColumn(dopt, tbinfo, j))
17255 : : {
17256 : : bool print_default;
17257 : : bool print_notnull;
17258 : :
17259 : : /*
17260 : : * Default value --- suppress if to be printed separately
17261 : : * or not at all.
17262 : : */
17263 : 41410 : print_default = (tbinfo->attrdefs[j] != NULL &&
17264 [ + + + + ]: 21222 : tbinfo->attrdefs[j]->dobj.dump &&
17265 [ + + ]: 1083 : !tbinfo->attrdefs[j]->separate);
17266 : :
17267 : : /*
17268 : : * Not Null constraint --- print it if it is locally
17269 : : * defined, or if binary upgrade. (In the latter case, we
17270 : : * reset conislocal below.)
17271 : : */
17272 [ + + ]: 22644 : print_notnull = (tbinfo->notnull_constrs[j] != NULL &&
17273 [ + + ]: 2505 : (tbinfo->notnull_islocal[j] ||
17274 [ + + ]: 675 : dopt->binary_upgrade ||
17275 [ + + ]: 581 : tbinfo->ispartition));
17276 : :
17277 : : /*
17278 : : * Skip column if fully defined by reloftype, except in
17279 : : * binary upgrade
17280 : : */
17281 [ + + ]: 20139 : if (OidIsValid(tbinfo->reloftype) &&
17282 [ + + + + ]: 50 : !print_default && !print_notnull &&
17283 [ + + ]: 30 : !dopt->binary_upgrade)
17284 : 24 : continue;
17285 : :
17286 : : /* Format properly if not first attr */
17287 [ + + ]: 20115 : if (actual_atts == 0)
17288 : 5471 : appendPQExpBufferStr(q, " (");
17289 : : else
17290 : 14644 : appendPQExpBufferChar(q, ',');
17291 : 20115 : appendPQExpBufferStr(q, "\n ");
17292 : 20115 : actual_atts++;
17293 : :
17294 : : /* Attribute name */
17295 : 20115 : appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j]));
17296 : :
17297 [ + + ]: 20115 : if (tbinfo->attisdropped[j])
17298 : : {
17299 : : /*
17300 : : * ALTER TABLE DROP COLUMN clears
17301 : : * pg_attribute.atttypid, so we will not have gotten a
17302 : : * valid type name; insert INTEGER as a stopgap. We'll
17303 : : * clean things up later.
17304 : : */
17305 : 85 : appendPQExpBufferStr(q, " INTEGER /* dummy */");
17306 : : /* and skip to the next column */
17307 : 85 : continue;
17308 : : }
17309 : :
17310 : : /*
17311 : : * Attribute type; print it except when creating a typed
17312 : : * table ('OF type_name'), but in binary-upgrade mode,
17313 : : * print it in that case too.
17314 : : */
17315 [ + + + + ]: 20030 : if (dopt->binary_upgrade || !OidIsValid(tbinfo->reloftype))
17316 : : {
17317 : 20014 : appendPQExpBuffer(q, " %s",
17318 : 20014 : tbinfo->atttypnames[j]);
17319 : : }
17320 : :
17321 [ + + ]: 20030 : if (print_default)
17322 : : {
17323 [ + + ]: 949 : if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED)
17324 : 328 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s) STORED",
17325 : 328 : tbinfo->attrdefs[j]->adef_expr);
17326 [ + + ]: 621 : else if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_VIRTUAL)
17327 : 230 : appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s)",
17328 : 230 : tbinfo->attrdefs[j]->adef_expr);
17329 : : else
17330 : 391 : appendPQExpBuffer(q, " DEFAULT %s",
17331 : 391 : tbinfo->attrdefs[j]->adef_expr);
17332 : : }
17333 : :
17334 [ + + ]: 20030 : if (print_notnull)
17335 : : {
17336 [ + + ]: 2472 : if (tbinfo->notnull_constrs[j][0] == '\0')
17337 : 1769 : appendPQExpBufferStr(q, " NOT NULL");
17338 : : else
17339 : 703 : appendPQExpBuffer(q, " CONSTRAINT %s NOT NULL",
17340 : 703 : fmtId(tbinfo->notnull_constrs[j]));
17341 : :
17342 [ + + ]: 2472 : if (tbinfo->notnull_noinh[j])
17343 : 35 : appendPQExpBufferStr(q, " NO INHERIT");
17344 : : }
17345 : :
17346 : : /* Add collation if not default for the type */
17347 [ + + ]: 20030 : if (OidIsValid(tbinfo->attcollation[j]))
17348 : : {
17349 : : CollInfo *coll;
17350 : :
17351 : 216 : coll = findCollationByOid(tbinfo->attcollation[j]);
17352 [ + - ]: 216 : if (coll)
17353 : 216 : appendPQExpBuffer(q, " COLLATE %s",
17354 : 216 : fmtQualifiedDumpable(coll));
17355 : : }
17356 : : }
17357 : :
17358 : : /*
17359 : : * On the other hand, if we choose not to print a column
17360 : : * (likely because it is created by inheritance), but the
17361 : : * column has a locally-defined not-null constraint, we need
17362 : : * to dump the constraint as a standalone object.
17363 : : *
17364 : : * This syntax isn't SQL-conforming, but if you wanted
17365 : : * standard output you wouldn't be creating non-standard
17366 : : * objects to begin with.
17367 : : */
17368 [ + + ]: 21155 : if (!shouldPrintColumn(dopt, tbinfo, j) &&
17369 [ + + ]: 1125 : !tbinfo->attisdropped[j] &&
17370 [ + + ]: 756 : tbinfo->notnull_constrs[j] != NULL &&
17371 [ + + ]: 216 : tbinfo->notnull_islocal[j])
17372 : : {
17373 : : /* Format properly if not first attr */
17374 [ + + ]: 94 : if (actual_atts == 0)
17375 : 90 : appendPQExpBufferStr(q, " (");
17376 : : else
17377 : 4 : appendPQExpBufferChar(q, ',');
17378 : 94 : appendPQExpBufferStr(q, "\n ");
17379 : 94 : actual_atts++;
17380 : :
17381 [ + + ]: 94 : if (tbinfo->notnull_constrs[j][0] == '\0')
17382 : 8 : appendPQExpBuffer(q, "NOT NULL %s",
17383 : 8 : fmtId(tbinfo->attnames[j]));
17384 : : else
17385 : 172 : appendPQExpBuffer(q, "CONSTRAINT %s NOT NULL %s",
17386 : 86 : tbinfo->notnull_constrs[j],
17387 : 86 : fmtId(tbinfo->attnames[j]));
17388 : :
17389 [ + + ]: 94 : if (tbinfo->notnull_noinh[j])
17390 : 33 : appendPQExpBufferStr(q, " NO INHERIT");
17391 : : }
17392 : : }
17393 : :
17394 : : /*
17395 : : * Add non-inherited CHECK constraints, if any.
17396 : : *
17397 : : * For partitions, we need to include check constraints even if
17398 : : * they're not defined locally, because the ALTER TABLE ATTACH
17399 : : * PARTITION that we'll emit later expects the constraint to be
17400 : : * there. (No need to fix conislocal: ATTACH PARTITION does that)
17401 : : */
17402 [ + + ]: 6451 : for (j = 0; j < tbinfo->ncheck; j++)
17403 : : {
17404 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
17405 : :
17406 [ + + ]: 593 : if (constr->separate ||
17407 [ + + + + ]: 523 : (!constr->conislocal && !tbinfo->ispartition))
17408 : 109 : continue;
17409 : :
17410 [ + + ]: 484 : if (actual_atts == 0)
17411 : 16 : appendPQExpBufferStr(q, " (\n ");
17412 : : else
17413 : 468 : appendPQExpBufferStr(q, ",\n ");
17414 : :
17415 : 484 : appendPQExpBuffer(q, "CONSTRAINT %s ",
17416 : 484 : fmtId(constr->dobj.name));
17417 : 484 : appendPQExpBufferStr(q, constr->condef);
17418 : :
17419 : 484 : actual_atts++;
17420 : : }
17421 : :
17422 [ + + ]: 5858 : if (actual_atts)
17423 : 5577 : appendPQExpBufferStr(q, "\n)");
17424 [ + + - + ]: 281 : else if (!(OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade))
17425 : : {
17426 : : /*
17427 : : * No attributes? we must have a parenthesized attribute list,
17428 : : * even though empty, when not using the OF TYPE syntax.
17429 : : */
17430 : 269 : appendPQExpBufferStr(q, " (\n)");
17431 : : }
17432 : :
17433 : : /*
17434 : : * Emit the INHERITS clause (not for partitions), except in
17435 : : * binary-upgrade mode.
17436 : : */
17437 [ + + + + ]: 5858 : if (numParents > 0 && !tbinfo->ispartition &&
17438 [ + + ]: 555 : !dopt->binary_upgrade)
17439 : : {
17440 : 486 : appendPQExpBufferStr(q, "\nINHERITS (");
17441 [ + + ]: 1045 : for (k = 0; k < numParents; k++)
17442 : : {
17443 : 559 : TableInfo *parentRel = parents[k];
17444 : :
17445 [ + + ]: 559 : if (k > 0)
17446 : 73 : appendPQExpBufferStr(q, ", ");
17447 : 559 : appendPQExpBufferStr(q, fmtQualifiedDumpable(parentRel));
17448 : : }
17449 : 486 : appendPQExpBufferChar(q, ')');
17450 : : }
17451 : :
17452 [ + + ]: 5858 : if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
17453 : 609 : appendPQExpBuffer(q, "\nPARTITION BY %s", partkeydef);
17454 : :
17455 [ + + ]: 5858 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
17456 : 36 : appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
17457 : : }
17458 : :
17459 [ + + - + ]: 12267 : if (nonemptyReloptions(tbinfo->reloptions) ||
17460 : 6056 : nonemptyReloptions(tbinfo->toast_reloptions))
17461 : : {
17462 : 155 : bool addcomma = false;
17463 : :
17464 : 155 : appendPQExpBufferStr(q, "\nWITH (");
17465 [ + - ]: 155 : if (nonemptyReloptions(tbinfo->reloptions))
17466 : : {
17467 : 155 : addcomma = true;
17468 : 155 : appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
17469 : : }
17470 [ + + ]: 155 : if (nonemptyReloptions(tbinfo->toast_reloptions))
17471 : : {
17472 [ + - ]: 5 : if (addcomma)
17473 : 5 : appendPQExpBufferStr(q, ", ");
17474 : 5 : appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.",
17475 : : fout);
17476 : : }
17477 : 155 : appendPQExpBufferChar(q, ')');
17478 : : }
17479 : :
17480 : : /* Dump generic options if any */
17481 [ + + + + ]: 6211 : if (ftoptions && ftoptions[0])
17482 : 34 : appendPQExpBuffer(q, "\nOPTIONS (\n %s\n)", ftoptions);
17483 : :
17484 : : /*
17485 : : * For materialized views, create the AS clause just like a view. At
17486 : : * this point, we always mark the view as not populated.
17487 : : */
17488 [ + + ]: 6211 : if (tbinfo->relkind == RELKIND_MATVIEW)
17489 : : {
17490 : : PQExpBuffer result;
17491 : :
17492 : 353 : result = createViewAsClause(fout, tbinfo);
17493 : 353 : appendPQExpBuffer(q, " AS\n%s\n WITH NO DATA;\n",
17494 : : result->data);
17495 : 353 : destroyPQExpBuffer(result);
17496 : : }
17497 : : else
17498 : 5858 : appendPQExpBufferStr(q, ";\n");
17499 : :
17500 : : /* Materialized views can depend on extensions */
17501 [ + + ]: 6211 : if (tbinfo->relkind == RELKIND_MATVIEW)
17502 : 353 : append_depends_on_extension(fout, q, &tbinfo->dobj,
17503 : : "pg_catalog.pg_class",
17504 : : "MATERIALIZED VIEW",
17505 : : qualrelname);
17506 : :
17507 : : /*
17508 : : * in binary upgrade mode, update the catalog with any missing values
17509 : : * that might be present.
17510 : : */
17511 [ + + ]: 6211 : if (dopt->binary_upgrade)
17512 : : {
17513 [ + + ]: 4227 : for (j = 0; j < tbinfo->numatts; j++)
17514 : : {
17515 [ + + ]: 3344 : if (tbinfo->attmissingval[j][0] != '\0')
17516 : : {
17517 : 4 : appendPQExpBufferStr(q, "\n-- set missing value.\n");
17518 : 4 : appendPQExpBufferStr(q,
17519 : : "SELECT pg_catalog.binary_upgrade_set_missing_value(");
17520 : 4 : appendStringLiteralAH(q, qualrelname, fout);
17521 : 4 : appendPQExpBufferStr(q, "::pg_catalog.regclass,");
17522 : 4 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17523 : 4 : appendPQExpBufferChar(q, ',');
17524 : 4 : appendStringLiteralAH(q, tbinfo->attmissingval[j], fout);
17525 : 4 : appendPQExpBufferStr(q, ");\n\n");
17526 : : }
17527 : : }
17528 : : }
17529 : :
17530 : : /*
17531 : : * To create binary-compatible heap files, we have to ensure the same
17532 : : * physical column order, including dropped columns, as in the
17533 : : * original. Therefore, we create dropped columns above and drop them
17534 : : * here, also updating their attlen/attalign values so that the
17535 : : * dropped column can be skipped properly. (We do not bother with
17536 : : * restoring the original attbyval setting.) Also, inheritance
17537 : : * relationships are set up by doing ALTER TABLE INHERIT rather than
17538 : : * using an INHERITS clause --- the latter would possibly mess up the
17539 : : * column order. That also means we have to take care about setting
17540 : : * attislocal correctly, plus fix up any inherited CHECK constraints.
17541 : : * Analogously, we set up typed tables using ALTER TABLE / OF here.
17542 : : *
17543 : : * We process foreign and partitioned tables here, even though they
17544 : : * lack heap storage, because they can participate in inheritance
17545 : : * relationships and we want this stuff to be consistent across the
17546 : : * inheritance tree. We can exclude indexes, toast tables, sequences
17547 : : * and matviews, even though they have storage, because we don't
17548 : : * support altering or dropping columns in them, nor can they be part
17549 : : * of inheritance trees.
17550 : : */
17551 [ + + ]: 6211 : if (dopt->binary_upgrade &&
17552 [ + + ]: 883 : (tbinfo->relkind == RELKIND_RELATION ||
17553 [ + + ]: 116 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
17554 [ + + ]: 115 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE))
17555 : : {
17556 : : bool firstitem;
17557 : : bool firstitem_extra;
17558 : :
17559 : : /*
17560 : : * Drop any dropped columns. Merge the pg_attribute manipulations
17561 : : * into a single SQL command, so that we don't cause repeated
17562 : : * relcache flushes on the target table. Otherwise we risk O(N^2)
17563 : : * relcache bloat while dropping N columns.
17564 : : */
17565 : 866 : resetPQExpBuffer(extra);
17566 : 866 : firstitem = true;
17567 [ + + ]: 4189 : for (j = 0; j < tbinfo->numatts; j++)
17568 : : {
17569 [ + + ]: 3323 : if (tbinfo->attisdropped[j])
17570 : : {
17571 [ + + ]: 85 : if (firstitem)
17572 : : {
17573 : 39 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped columns.\n"
17574 : : "UPDATE pg_catalog.pg_attribute\n"
17575 : : "SET attlen = v.dlen, "
17576 : : "attalign = v.dalign, "
17577 : : "attbyval = false\n"
17578 : : "FROM (VALUES ");
17579 : 39 : firstitem = false;
17580 : : }
17581 : : else
17582 : 46 : appendPQExpBufferStr(q, ",\n ");
17583 : 85 : appendPQExpBufferChar(q, '(');
17584 : 85 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17585 : 85 : appendPQExpBuffer(q, ", %d, '%c')",
17586 : 85 : tbinfo->attlen[j],
17587 : 85 : tbinfo->attalign[j]);
17588 : : /* The ALTER ... DROP COLUMN commands must come after */
17589 : 85 : appendPQExpBuffer(extra, "ALTER %sTABLE ONLY %s ",
17590 : : foreign, qualrelname);
17591 : 85 : appendPQExpBuffer(extra, "DROP COLUMN %s;\n",
17592 : 85 : fmtId(tbinfo->attnames[j]));
17593 : : }
17594 : : }
17595 [ + + ]: 866 : if (!firstitem)
17596 : : {
17597 : 39 : appendPQExpBufferStr(q, ") v(dname, dlen, dalign)\n"
17598 : : "WHERE attrelid = ");
17599 : 39 : appendStringLiteralAH(q, qualrelname, fout);
17600 : 39 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17601 : : " AND attname = v.dname;\n");
17602 : : /* Now we can issue the actual DROP COLUMN commands */
17603 : 39 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17604 : : }
17605 : :
17606 : : /*
17607 : : * Fix up inherited columns. As above, do the pg_attribute
17608 : : * manipulations in a single SQL command.
17609 : : */
17610 : 866 : firstitem = true;
17611 [ + + ]: 4189 : for (j = 0; j < tbinfo->numatts; j++)
17612 : : {
17613 [ + + ]: 3323 : if (!tbinfo->attisdropped[j] &&
17614 [ + + ]: 3238 : !tbinfo->attislocal[j])
17615 : : {
17616 [ + + ]: 652 : if (firstitem)
17617 : : {
17618 : 283 : appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited columns.\n");
17619 : 283 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n"
17620 : : "SET attislocal = false\n"
17621 : : "WHERE attrelid = ");
17622 : 283 : appendStringLiteralAH(q, qualrelname, fout);
17623 : 283 : appendPQExpBufferStr(q, "::pg_catalog.regclass\n"
17624 : : " AND attname IN (");
17625 : 283 : firstitem = false;
17626 : : }
17627 : : else
17628 : 369 : appendPQExpBufferStr(q, ", ");
17629 : 652 : appendStringLiteralAH(q, tbinfo->attnames[j], fout);
17630 : : }
17631 : : }
17632 [ + + ]: 866 : if (!firstitem)
17633 : 283 : appendPQExpBufferStr(q, ");\n");
17634 : :
17635 : : /*
17636 : : * Fix up not-null constraints that come from inheritance. As
17637 : : * above, do the pg_constraint manipulations in a single SQL
17638 : : * command. (Actually, two in special cases, if we're doing an
17639 : : * upgrade from < 18).
17640 : : */
17641 : 866 : firstitem = true;
17642 : 866 : firstitem_extra = true;
17643 : 866 : resetPQExpBuffer(extra);
17644 [ + + ]: 4189 : for (j = 0; j < tbinfo->numatts; j++)
17645 : : {
17646 : : /*
17647 : : * If a not-null constraint comes from inheritance, reset
17648 : : * conislocal. The inhcount is fixed by ALTER TABLE INHERIT,
17649 : : * below. Special hack: in versions < 18, columns with no
17650 : : * local definition need their constraint to be matched by
17651 : : * column number in conkeys instead of by constraint name,
17652 : : * because the latter is not available. (We distinguish the
17653 : : * case because the constraint name is the empty string.)
17654 : : */
17655 [ + + ]: 3323 : if (tbinfo->notnull_constrs[j] != NULL &&
17656 [ + + ]: 333 : !tbinfo->notnull_islocal[j])
17657 : : {
17658 [ + + ]: 94 : if (tbinfo->notnull_constrs[j][0] != '\0')
17659 : : {
17660 [ + + ]: 81 : if (firstitem)
17661 : : {
17662 : 69 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
17663 : : "SET conislocal = false\n"
17664 : : "WHERE contype = 'n' AND conrelid = ");
17665 : 69 : appendStringLiteralAH(q, qualrelname, fout);
17666 : 69 : appendPQExpBufferStr(q, "::pg_catalog.regclass AND\n"
17667 : : "conname IN (");
17668 : 69 : firstitem = false;
17669 : : }
17670 : : else
17671 : 12 : appendPQExpBufferStr(q, ", ");
17672 : 81 : appendStringLiteralAH(q, tbinfo->notnull_constrs[j], fout);
17673 : : }
17674 : : else
17675 : : {
17676 [ + - ]: 13 : if (firstitem_extra)
17677 : : {
17678 : 13 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17679 : : "SET conislocal = false\n"
17680 : : "WHERE contype = 'n' AND conrelid = ");
17681 : 13 : appendStringLiteralAH(extra, qualrelname, fout);
17682 : 13 : appendPQExpBufferStr(extra, "::pg_catalog.regclass AND\n"
17683 : : "conkey IN (");
17684 : 13 : firstitem_extra = false;
17685 : : }
17686 : : else
17687 : 0 : appendPQExpBufferStr(extra, ", ");
17688 : 13 : appendPQExpBuffer(extra, "'{%d}'", j + 1);
17689 : : }
17690 : : }
17691 : : }
17692 [ + + ]: 866 : if (!firstitem)
17693 : 69 : appendPQExpBufferStr(q, ");\n");
17694 [ + + ]: 866 : if (!firstitem_extra)
17695 : 13 : appendPQExpBufferStr(extra, ");\n");
17696 : :
17697 [ + + ]: 866 : if (extra->len > 0)
17698 : 13 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17699 : :
17700 : : /*
17701 : : * Add inherited CHECK constraints, if any.
17702 : : *
17703 : : * For partitions, they were already dumped, and conislocal
17704 : : * doesn't need fixing.
17705 : : *
17706 : : * As above, issue only one direct manipulation of pg_constraint.
17707 : : * Although it is tempting to merge the ALTER ADD CONSTRAINT
17708 : : * commands into one as well, refrain for now due to concern about
17709 : : * possible backend memory bloat if there are many such
17710 : : * constraints.
17711 : : */
17712 : 866 : resetPQExpBuffer(extra);
17713 : 866 : firstitem = true;
17714 [ + + ]: 928 : for (k = 0; k < tbinfo->ncheck; k++)
17715 : : {
17716 : 62 : ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
17717 : :
17718 [ + + + + : 62 : if (constr->separate || constr->conislocal || tbinfo->ispartition)
+ + ]
17719 : 60 : continue;
17720 : :
17721 [ + - ]: 2 : if (firstitem)
17722 : 2 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraints.\n");
17723 : 2 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n",
17724 : : foreign, qualrelname,
17725 : 2 : fmtId(constr->dobj.name),
17726 : : constr->condef);
17727 : : /* Update pg_constraint after all the ALTER TABLEs */
17728 [ + - ]: 2 : if (firstitem)
17729 : : {
17730 : 2 : appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n"
17731 : : "SET conislocal = false\n"
17732 : : "WHERE contype = 'c' AND conrelid = ");
17733 : 2 : appendStringLiteralAH(extra, qualrelname, fout);
17734 : 2 : appendPQExpBufferStr(extra, "::pg_catalog.regclass\n");
17735 : 2 : appendPQExpBufferStr(extra, " AND conname IN (");
17736 : 2 : firstitem = false;
17737 : : }
17738 : : else
17739 : 0 : appendPQExpBufferStr(extra, ", ");
17740 : 2 : appendStringLiteralAH(extra, constr->dobj.name, fout);
17741 : : }
17742 [ + + ]: 866 : if (!firstitem)
17743 : : {
17744 : 2 : appendPQExpBufferStr(extra, ");\n");
17745 : 2 : appendBinaryPQExpBuffer(q, extra->data, extra->len);
17746 : : }
17747 : :
17748 [ + + + + ]: 866 : if (numParents > 0 && !tbinfo->ispartition)
17749 : : {
17750 : 69 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
17751 [ + + ]: 149 : for (k = 0; k < numParents; k++)
17752 : : {
17753 : 80 : TableInfo *parentRel = parents[k];
17754 : :
17755 : 80 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s INHERIT %s;\n", foreign,
17756 : : qualrelname,
17757 : 80 : fmtQualifiedDumpable(parentRel));
17758 : : }
17759 : : }
17760 : :
17761 [ + + ]: 866 : if (OidIsValid(tbinfo->reloftype))
17762 : : {
17763 : 6 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n");
17764 : 6 : appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
17765 : : qualrelname,
17766 : 6 : getFormattedTypeName(fout, tbinfo->reloftype,
17767 : : zeroIsError));
17768 : : }
17769 : : }
17770 : :
17771 : : /*
17772 : : * In binary_upgrade mode, arrange to restore the old relfrozenxid and
17773 : : * relminmxid of all vacuumable relations. (While vacuum.c processes
17774 : : * TOAST tables semi-independently, here we see them only as children
17775 : : * of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the
17776 : : * child toast table is handled below.)
17777 : : */
17778 [ + + ]: 6211 : if (dopt->binary_upgrade &&
17779 [ + + ]: 883 : (tbinfo->relkind == RELKIND_RELATION ||
17780 [ + + ]: 116 : tbinfo->relkind == RELKIND_MATVIEW))
17781 : : {
17782 : 784 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
17783 : 784 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17784 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17785 : : "WHERE oid = ",
17786 : 784 : tbinfo->frozenxid, tbinfo->minmxid);
17787 : 784 : appendStringLiteralAH(q, qualrelname, fout);
17788 : 784 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17789 : :
17790 [ + + ]: 784 : if (tbinfo->toast_oid)
17791 : : {
17792 : : /*
17793 : : * The toast table will have the same OID at restore, so we
17794 : : * can safely target it by OID.
17795 : : */
17796 : 297 : appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
17797 : 297 : appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
17798 : : "SET relfrozenxid = '%u', relminmxid = '%u'\n"
17799 : : "WHERE oid = '%u';\n",
17800 : 297 : tbinfo->toast_frozenxid,
17801 : 297 : tbinfo->toast_minmxid, tbinfo->toast_oid);
17802 : : }
17803 : : }
17804 : :
17805 : : /*
17806 : : * In binary_upgrade mode, restore matviews' populated status by
17807 : : * poking pg_class directly. This is pretty ugly, but we can't use
17808 : : * REFRESH MATERIALIZED VIEW since it's possible that some underlying
17809 : : * matview is not populated even though this matview is; in any case,
17810 : : * we want to transfer the matview's heap storage, not run REFRESH.
17811 : : */
17812 [ + + + + ]: 6211 : if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
17813 [ + + ]: 17 : tbinfo->relispopulated)
17814 : : {
17815 : 15 : appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n");
17816 : 15 : appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n"
17817 : : "SET relispopulated = 't'\n"
17818 : : "WHERE oid = ");
17819 : 15 : appendStringLiteralAH(q, qualrelname, fout);
17820 : 15 : appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
17821 : : }
17822 : :
17823 : : /*
17824 : : * Dump additional per-column properties that we can't handle in the
17825 : : * main CREATE TABLE command.
17826 : : */
17827 [ + + ]: 27906 : for (j = 0; j < tbinfo->numatts; j++)
17828 : : {
17829 : : /* None of this applies to dropped columns */
17830 [ + + ]: 21695 : if (tbinfo->attisdropped[j])
17831 : 454 : continue;
17832 : :
17833 : : /*
17834 : : * Dump per-column statistics information. We only issue an ALTER
17835 : : * TABLE statement if the attstattarget entry for this column is
17836 : : * not the default value.
17837 : : */
17838 [ + + ]: 21241 : if (tbinfo->attstattarget[j] >= 0)
17839 : 34 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n",
17840 : : foreign, qualrelname,
17841 : 34 : fmtId(tbinfo->attnames[j]),
17842 : 34 : tbinfo->attstattarget[j]);
17843 : :
17844 : : /*
17845 : : * Dump per-column storage information. The statement is only
17846 : : * dumped if the storage has been changed from the type's default.
17847 : : */
17848 [ + + ]: 21241 : if (tbinfo->attstorage[j] != tbinfo->typstorage[j])
17849 : : {
17850 [ + + - + : 83 : switch (tbinfo->attstorage[j])
- ]
17851 : : {
17852 : 10 : case TYPSTORAGE_PLAIN:
17853 : 10 : storage = "PLAIN";
17854 : 10 : break;
17855 : 39 : case TYPSTORAGE_EXTERNAL:
17856 : 39 : storage = "EXTERNAL";
17857 : 39 : break;
17858 : 0 : case TYPSTORAGE_EXTENDED:
17859 : 0 : storage = "EXTENDED";
17860 : 0 : break;
17861 : 34 : case TYPSTORAGE_MAIN:
17862 : 34 : storage = "MAIN";
17863 : 34 : break;
17864 : 0 : default:
17865 : 0 : storage = NULL;
17866 : : }
17867 : :
17868 : : /*
17869 : : * Only dump the statement if it's a storage type we recognize
17870 : : */
17871 [ + - ]: 83 : if (storage != NULL)
17872 : 83 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n",
17873 : : foreign, qualrelname,
17874 : 83 : fmtId(tbinfo->attnames[j]),
17875 : : storage);
17876 : : }
17877 : :
17878 : : /*
17879 : : * Dump per-column compression, if it's been set.
17880 : : */
17881 [ + + ]: 21241 : if (!dopt->no_toast_compression)
17882 : : {
17883 : : const char *cmname;
17884 : :
17885 [ + + + ]: 21141 : switch (tbinfo->attcompression[j])
17886 : : {
17887 : 73 : case 'p':
17888 : 73 : cmname = "pglz";
17889 : 73 : break;
17890 : 39 : case 'l':
17891 : 39 : cmname = "lz4";
17892 : 39 : break;
17893 : 21029 : default:
17894 : 21029 : cmname = NULL;
17895 : 21029 : break;
17896 : : }
17897 : :
17898 [ + + ]: 21141 : if (cmname != NULL)
17899 : 112 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET COMPRESSION %s;\n",
17900 : : foreign, qualrelname,
17901 : 112 : fmtId(tbinfo->attnames[j]),
17902 : : cmname);
17903 : : }
17904 : :
17905 : : /*
17906 : : * Dump per-column attributes.
17907 : : */
17908 [ + + ]: 21241 : if (tbinfo->attoptions[j][0] != '\0')
17909 : 34 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n",
17910 : : foreign, qualrelname,
17911 : 34 : fmtId(tbinfo->attnames[j]),
17912 : 34 : tbinfo->attoptions[j]);
17913 : :
17914 : : /*
17915 : : * Dump per-column fdw options.
17916 : : */
17917 [ + + ]: 21241 : if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
17918 [ + + ]: 36 : tbinfo->attfdwoptions[j][0] != '\0')
17919 : 34 : appendPQExpBuffer(q,
17920 : : "ALTER FOREIGN TABLE ONLY %s ALTER COLUMN %s OPTIONS (\n"
17921 : : " %s\n"
17922 : : ");\n",
17923 : : qualrelname,
17924 : 34 : fmtId(tbinfo->attnames[j]),
17925 : 34 : tbinfo->attfdwoptions[j]);
17926 : : } /* end loop over columns */
17927 : :
17928 : 6211 : pg_free(partkeydef);
17929 : 6211 : pg_free(ftoptions);
17930 : 6211 : pg_free(srvname);
17931 : : }
17932 : :
17933 : : /*
17934 : : * dump properties we only have ALTER TABLE syntax for
17935 : : */
17936 [ + + ]: 6888 : if ((tbinfo->relkind == RELKIND_RELATION ||
17937 [ + + ]: 1675 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE ||
17938 [ + + ]: 1066 : tbinfo->relkind == RELKIND_MATVIEW) &&
17939 [ + + ]: 6175 : tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT)
17940 : : {
17941 [ + - ]: 207 : if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX)
17942 : : {
17943 : : /* nothing to do, will be set when the index is dumped */
17944 : : }
17945 [ + - ]: 207 : else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING)
17946 : : {
17947 : 207 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n",
17948 : : qualrelname);
17949 : : }
17950 [ # # ]: 0 : else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL)
17951 : : {
17952 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n",
17953 : : qualrelname);
17954 : : }
17955 : : }
17956 : :
17957 [ + + ]: 6888 : if (tbinfo->forcerowsec)
17958 : 10 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n",
17959 : : qualrelname);
17960 : :
17961 : 6888 : appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname);
17962 : :
17963 [ + + ]: 6888 : if (dopt->binary_upgrade)
17964 : 954 : binary_upgrade_extension_member(q, &tbinfo->dobj,
17965 : : reltypename, qrelname,
17966 : 954 : tbinfo->dobj.namespace->dobj.name);
17967 : :
17968 [ + - ]: 6888 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17969 : : {
17970 : 6888 : char *tablespace = NULL;
17971 : 6888 : char *tableam = NULL;
17972 : :
17973 : : /*
17974 : : * _selectTablespace() relies on tablespace-enabled objects in the
17975 : : * default tablespace to have a tablespace of "" (empty string) versus
17976 : : * non-tablespace-enabled objects to have a tablespace of NULL.
17977 : : * getTables() sets tbinfo->reltablespace to "" for the default
17978 : : * tablespace (not NULL).
17979 : : */
17980 [ + + + - : 6888 : if (RELKIND_HAS_TABLESPACE(tbinfo->relkind))
+ - + - +
+ + + - +
+ - ]
17981 : 6175 : tablespace = tbinfo->reltablespace;
17982 : :
17983 [ + + + - : 6888 : if (RELKIND_HAS_TABLE_AM(tbinfo->relkind) ||
+ + ]
17984 [ + + ]: 1322 : tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
17985 : 6175 : tableam = tbinfo->amname;
17986 : :
17987 : 6888 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
17988 [ + + ]: 6888 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
17989 : : .namespace = tbinfo->dobj.namespace->dobj.name,
17990 : : .tablespace = tablespace,
17991 : : .tableam = tableam,
17992 : : .relkind = tbinfo->relkind,
17993 : : .owner = tbinfo->rolname,
17994 : : .description = reltypename,
17995 : : .section = tbinfo->postponed_def ?
17996 : : SECTION_POST_DATA : SECTION_PRE_DATA,
17997 : : .createStmt = q->data,
17998 : : .dropStmt = delq->data));
17999 : : }
18000 : :
18001 : : /* Dump Table Comments */
18002 [ + + ]: 6888 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18003 : 78 : dumpTableComment(fout, tbinfo, reltypename);
18004 : :
18005 : : /* Dump Table Security Labels */
18006 [ - + ]: 6888 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
18007 : 0 : dumpTableSecLabel(fout, tbinfo, reltypename);
18008 : :
18009 : : /*
18010 : : * Dump comments for not-null constraints that aren't to be dumped
18011 : : * separately (those are processed by collectComments/dumpComment).
18012 : : */
18013 [ + - + - ]: 6888 : if (!fout->dopt->no_comments && dopt->dumpSchema &&
18014 [ + - ]: 6888 : fout->remoteVersion >= 180000)
18015 : : {
18016 : 6888 : PQExpBuffer comment = NULL;
18017 : 6888 : PQExpBuffer tag = NULL;
18018 : :
18019 [ + + ]: 32289 : for (j = 0; j < tbinfo->numatts; j++)
18020 : : {
18021 [ + + ]: 25401 : if (tbinfo->notnull_constrs[j] != NULL &&
18022 [ + + ]: 2721 : tbinfo->notnull_comment[j] != NULL)
18023 : : {
18024 [ + - ]: 44 : if (comment == NULL)
18025 : : {
18026 : 44 : comment = createPQExpBuffer();
18027 : 44 : tag = createPQExpBuffer();
18028 : : }
18029 : : else
18030 : : {
18031 : 0 : resetPQExpBuffer(comment);
18032 : 0 : resetPQExpBuffer(tag);
18033 : : }
18034 : :
18035 : 44 : appendPQExpBuffer(comment, "COMMENT ON CONSTRAINT %s ON %s IS ",
18036 : 44 : fmtId(tbinfo->notnull_constrs[j]), qualrelname);
18037 : 44 : appendStringLiteralAH(comment, tbinfo->notnull_comment[j], fout);
18038 : 44 : appendPQExpBufferStr(comment, ";\n");
18039 : :
18040 : 44 : appendPQExpBuffer(tag, "CONSTRAINT %s ON %s",
18041 : 44 : fmtId(tbinfo->notnull_constrs[j]), qrelname);
18042 : :
18043 : 44 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
18044 : 44 : ARCHIVE_OPTS(.tag = tag->data,
18045 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18046 : : .owner = tbinfo->rolname,
18047 : : .description = "COMMENT",
18048 : : .section = SECTION_NONE,
18049 : : .createStmt = comment->data,
18050 : : .deps = &(tbinfo->dobj.dumpId),
18051 : : .nDeps = 1));
18052 : : }
18053 : : }
18054 : :
18055 : 6888 : destroyPQExpBuffer(comment);
18056 : 6888 : destroyPQExpBuffer(tag);
18057 : : }
18058 : :
18059 : : /* Dump comments on inlined table constraints */
18060 [ + + ]: 7481 : for (j = 0; j < tbinfo->ncheck; j++)
18061 : : {
18062 : 593 : ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
18063 : :
18064 [ + + + + ]: 593 : if (constr->separate || !constr->conislocal)
18065 : 254 : continue;
18066 : :
18067 [ + + ]: 339 : if (constr->dobj.dump & DUMP_COMPONENT_COMMENT)
18068 : 39 : dumpTableConstraintComment(fout, constr);
18069 : : }
18070 : :
18071 : 6888 : destroyPQExpBuffer(q);
18072 : 6888 : destroyPQExpBuffer(delq);
18073 : 6888 : destroyPQExpBuffer(extra);
18074 : 6888 : pg_free(qrelname);
18075 : 6888 : pg_free(qualrelname);
18076 : 6888 : }
18077 : :
18078 : : /*
18079 : : * dumpTableAttach
18080 : : * write to fout the commands to attach a child partition
18081 : : *
18082 : : * Child partitions are always made by creating them separately
18083 : : * and then using ATTACH PARTITION, rather than using
18084 : : * CREATE TABLE ... PARTITION OF. This is important for preserving
18085 : : * any possible discrepancy in column layout, to allow assigning the
18086 : : * correct tablespace if different, and so that it's possible to restore
18087 : : * a partition without restoring its parent. (You'll get an error from
18088 : : * the ATTACH PARTITION command, but that can be ignored, or skipped
18089 : : * using "pg_restore -L" if you prefer.) The last point motivates
18090 : : * treating ATTACH PARTITION as a completely separate ArchiveEntry
18091 : : * rather than emitting it within the child partition's ArchiveEntry.
18092 : : */
18093 : : static void
18094 : 1457 : dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo)
18095 : : {
18096 : 1457 : DumpOptions *dopt = fout->dopt;
18097 : : PQExpBuffer q;
18098 : : PGresult *res;
18099 : : char *partbound;
18100 : :
18101 : : /* Do nothing if not dumping schema */
18102 [ + + ]: 1457 : if (!dopt->dumpSchema)
18103 : 57 : return;
18104 : :
18105 : 1400 : q = createPQExpBuffer();
18106 : :
18107 [ + + ]: 1400 : if (!fout->is_prepared[PREPQUERY_DUMPTABLEATTACH])
18108 : : {
18109 : : /* Set up query for partbound details */
18110 : 45 : appendPQExpBufferStr(q,
18111 : : "PREPARE dumpTableAttach(pg_catalog.oid) AS\n");
18112 : :
18113 : 45 : appendPQExpBufferStr(q,
18114 : : "SELECT pg_get_expr(c.relpartbound, c.oid) "
18115 : : "FROM pg_class c "
18116 : : "WHERE c.oid = $1");
18117 : :
18118 : 45 : ExecuteSqlStatement(fout, q->data);
18119 : :
18120 : 45 : fout->is_prepared[PREPQUERY_DUMPTABLEATTACH] = true;
18121 : : }
18122 : :
18123 : 1400 : printfPQExpBuffer(q,
18124 : : "EXECUTE dumpTableAttach('%u')",
18125 : 1400 : attachinfo->partitionTbl->dobj.catId.oid);
18126 : :
18127 : 1400 : res = ExecuteSqlQueryForSingleRow(fout, q->data);
18128 : 1400 : partbound = PQgetvalue(res, 0, 0);
18129 : :
18130 : : /* Perform ALTER TABLE on the parent */
18131 : 1400 : printfPQExpBuffer(q,
18132 : : "ALTER TABLE ONLY %s ",
18133 : 1400 : fmtQualifiedDumpable(attachinfo->parentTbl));
18134 : 1400 : appendPQExpBuffer(q,
18135 : : "ATTACH PARTITION %s %s;\n",
18136 : 1400 : fmtQualifiedDumpable(attachinfo->partitionTbl),
18137 : : partbound);
18138 : :
18139 : : /*
18140 : : * There is no point in creating a drop query as the drop is done by table
18141 : : * drop. (If you think to change this, see also _printTocEntry().)
18142 : : * Although this object doesn't really have ownership as such, set the
18143 : : * owner field anyway to ensure that the command is run by the correct
18144 : : * role at restore time.
18145 : : */
18146 : 1400 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18147 : 1400 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18148 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18149 : : .owner = attachinfo->partitionTbl->rolname,
18150 : : .description = "TABLE ATTACH",
18151 : : .section = SECTION_PRE_DATA,
18152 : : .createStmt = q->data));
18153 : :
18154 : 1400 : PQclear(res);
18155 : 1400 : destroyPQExpBuffer(q);
18156 : : }
18157 : :
18158 : : /*
18159 : : * dumpAttrDef --- dump an attribute's default-value declaration
18160 : : */
18161 : : static void
18162 : 1121 : dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo)
18163 : : {
18164 : 1121 : DumpOptions *dopt = fout->dopt;
18165 : 1121 : TableInfo *tbinfo = adinfo->adtable;
18166 : 1121 : int adnum = adinfo->adnum;
18167 : : PQExpBuffer q;
18168 : : PQExpBuffer delq;
18169 : : char *qualrelname;
18170 : : char *tag;
18171 : : char *foreign;
18172 : :
18173 : : /* Do nothing if not dumping schema */
18174 [ - + ]: 1121 : if (!dopt->dumpSchema)
18175 : 0 : return;
18176 : :
18177 : : /* Skip if not "separate"; it was dumped in the table's definition */
18178 [ + + ]: 1121 : if (!adinfo->separate)
18179 : 949 : return;
18180 : :
18181 : 172 : q = createPQExpBuffer();
18182 : 172 : delq = createPQExpBuffer();
18183 : :
18184 : 172 : qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
18185 : :
18186 [ - + ]: 172 : foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18187 : :
18188 : 172 : appendPQExpBuffer(q,
18189 : : "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
18190 : 172 : foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
18191 : 172 : adinfo->adef_expr);
18192 : :
18193 : 172 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
18194 : : foreign, qualrelname,
18195 : 172 : fmtId(tbinfo->attnames[adnum - 1]));
18196 : :
18197 : 172 : tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
18198 : :
18199 [ + - ]: 172 : if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18200 : 172 : ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
18201 : 172 : ARCHIVE_OPTS(.tag = tag,
18202 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18203 : : .owner = tbinfo->rolname,
18204 : : .description = "DEFAULT",
18205 : : .section = SECTION_PRE_DATA,
18206 : : .createStmt = q->data,
18207 : : .dropStmt = delq->data));
18208 : :
18209 : 172 : pfree(tag);
18210 : 172 : destroyPQExpBuffer(q);
18211 : 172 : destroyPQExpBuffer(delq);
18212 : 172 : pg_free(qualrelname);
18213 : : }
18214 : :
18215 : : /*
18216 : : * getAttrName: extract the correct name for an attribute
18217 : : *
18218 : : * The array tblInfo->attnames[] only provides names of user attributes;
18219 : : * if a system attribute number is supplied, we have to fake it.
18220 : : * We also do a little bit of bounds checking for safety's sake.
18221 : : */
18222 : : static const char *
18223 : 2288 : getAttrName(int attrnum, const TableInfo *tblInfo)
18224 : : {
18225 [ + - + - ]: 2288 : if (attrnum > 0 && attrnum <= tblInfo->numatts)
18226 : 2288 : return tblInfo->attnames[attrnum - 1];
18227 [ # # # # : 0 : switch (attrnum)
# # # ]
18228 : : {
18229 : 0 : case SelfItemPointerAttributeNumber:
18230 : 0 : return "ctid";
18231 : 0 : case MinTransactionIdAttributeNumber:
18232 : 0 : return "xmin";
18233 : 0 : case MinCommandIdAttributeNumber:
18234 : 0 : return "cmin";
18235 : 0 : case MaxTransactionIdAttributeNumber:
18236 : 0 : return "xmax";
18237 : 0 : case MaxCommandIdAttributeNumber:
18238 : 0 : return "cmax";
18239 : 0 : case TableOidAttributeNumber:
18240 : 0 : return "tableoid";
18241 : : }
18242 : 0 : pg_fatal("invalid column number %d for table \"%s\"",
18243 : : attrnum, tblInfo->dobj.name);
18244 : : return NULL; /* keep compiler quiet */
18245 : : }
18246 : :
18247 : : /*
18248 : : * dumpIndex
18249 : : * write out to fout a user-defined index
18250 : : */
18251 : : static void
18252 : 2852 : dumpIndex(Archive *fout, const IndxInfo *indxinfo)
18253 : : {
18254 : 2852 : DumpOptions *dopt = fout->dopt;
18255 : 2852 : TableInfo *tbinfo = indxinfo->indextable;
18256 : 2852 : bool is_constraint = (indxinfo->indexconstraint != 0);
18257 : : PQExpBuffer q;
18258 : : PQExpBuffer delq;
18259 : : char *qindxname;
18260 : : char *qqindxname;
18261 : :
18262 : : /* Do nothing if not dumping schema */
18263 [ + + ]: 2852 : if (!dopt->dumpSchema)
18264 : 128 : return;
18265 : :
18266 : 2724 : q = createPQExpBuffer();
18267 : 2724 : delq = createPQExpBuffer();
18268 : :
18269 : 2724 : qindxname = pg_strdup(fmtId(indxinfo->dobj.name));
18270 : 2724 : qqindxname = pg_strdup(fmtQualifiedDumpable(indxinfo));
18271 : :
18272 : : /*
18273 : : * If there's an associated constraint, don't dump the index per se, but
18274 : : * do dump any comment for it. (This is safe because dependency ordering
18275 : : * will have ensured the constraint is emitted first.) Note that the
18276 : : * emitted comment has to be shown as depending on the constraint, not the
18277 : : * index, in such cases.
18278 : : */
18279 [ + + ]: 2724 : if (!is_constraint)
18280 : : {
18281 : 1087 : char *indstatcols = indxinfo->indstatcols;
18282 : 1087 : char *indstatvals = indxinfo->indstatvals;
18283 : 1087 : char **indstatcolsarray = NULL;
18284 : 1087 : char **indstatvalsarray = NULL;
18285 : 1087 : int nstatcols = 0;
18286 : 1087 : int nstatvals = 0;
18287 : :
18288 [ + + ]: 1087 : if (dopt->binary_upgrade)
18289 : 163 : binary_upgrade_set_pg_class_oids(fout, q,
18290 : 163 : indxinfo->dobj.catId.oid);
18291 : :
18292 : : /* Plain secondary index */
18293 : 1087 : appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
18294 : :
18295 : : /*
18296 : : * Append ALTER TABLE commands as needed to set properties that we
18297 : : * only have ALTER TABLE syntax for. Keep this in sync with the
18298 : : * similar code in dumpConstraint!
18299 : : */
18300 : :
18301 : : /* If the index is clustered, we need to record that. */
18302 [ + + ]: 1087 : if (indxinfo->indisclustered)
18303 : : {
18304 : 5 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
18305 : 5 : fmtQualifiedDumpable(tbinfo));
18306 : : /* index name is not qualified in this syntax */
18307 : 5 : appendPQExpBuffer(q, " ON %s;\n",
18308 : : qindxname);
18309 : : }
18310 : :
18311 : : /*
18312 : : * If the index has any statistics on some of its columns, generate
18313 : : * the associated ALTER INDEX queries.
18314 : : */
18315 [ + + - + ]: 1087 : if (strlen(indstatcols) != 0 || strlen(indstatvals) != 0)
18316 : : {
18317 : : int j;
18318 : :
18319 [ - + ]: 34 : if (!parsePGArray(indstatcols, &indstatcolsarray, &nstatcols))
18320 : 0 : pg_fatal("could not parse index statistic columns");
18321 [ - + ]: 34 : if (!parsePGArray(indstatvals, &indstatvalsarray, &nstatvals))
18322 : 0 : pg_fatal("could not parse index statistic values");
18323 [ - + ]: 34 : if (nstatcols != nstatvals)
18324 : 0 : pg_fatal("mismatched number of columns and values for index statistics");
18325 : :
18326 [ + + ]: 102 : for (j = 0; j < nstatcols; j++)
18327 : : {
18328 : 68 : appendPQExpBuffer(q, "ALTER INDEX %s ", qqindxname);
18329 : :
18330 : : /*
18331 : : * Note that this is a column number, so no quotes should be
18332 : : * used.
18333 : : */
18334 : 68 : appendPQExpBuffer(q, "ALTER COLUMN %s ",
18335 : 68 : indstatcolsarray[j]);
18336 : 68 : appendPQExpBuffer(q, "SET STATISTICS %s;\n",
18337 : 68 : indstatvalsarray[j]);
18338 : : }
18339 : : }
18340 : :
18341 : : /* Indexes can depend on extensions */
18342 : 1087 : append_depends_on_extension(fout, q, &indxinfo->dobj,
18343 : : "pg_catalog.pg_class",
18344 : : "INDEX", qqindxname);
18345 : :
18346 : : /* If the index defines identity, we need to record that. */
18347 [ - + ]: 1087 : if (indxinfo->indisreplident)
18348 : : {
18349 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
18350 : 0 : fmtQualifiedDumpable(tbinfo));
18351 : : /* index name is not qualified in this syntax */
18352 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
18353 : : qindxname);
18354 : : }
18355 : :
18356 : : /*
18357 : : * If this index is a member of a partitioned index, the backend will
18358 : : * not allow us to drop it separately, so don't try. It will go away
18359 : : * automatically when we drop either the index's table or the
18360 : : * partitioned index. (If, in a selective restore with --clean, we
18361 : : * drop neither of those, then this index will not be dropped either.
18362 : : * But that's fine, and even if you think it's not, the backend won't
18363 : : * let us do differently.)
18364 : : */
18365 [ + + ]: 1087 : if (indxinfo->parentidx == 0)
18366 : 897 : appendPQExpBuffer(delq, "DROP INDEX %s;\n", qqindxname);
18367 : :
18368 [ + - ]: 1087 : if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18369 : 1087 : ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
18370 : 1087 : ARCHIVE_OPTS(.tag = indxinfo->dobj.name,
18371 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18372 : : .tablespace = indxinfo->tablespace,
18373 : : .owner = tbinfo->rolname,
18374 : : .description = "INDEX",
18375 : : .section = SECTION_POST_DATA,
18376 : : .createStmt = q->data,
18377 : : .dropStmt = delq->data));
18378 : :
18379 : 1087 : free(indstatcolsarray);
18380 : 1087 : free(indstatvalsarray);
18381 : : }
18382 : :
18383 : : /* Dump Index Comments */
18384 [ + + ]: 2724 : if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18385 [ + + ]: 15 : dumpComment(fout, "INDEX", qindxname,
18386 : 15 : tbinfo->dobj.namespace->dobj.name,
18387 : : tbinfo->rolname,
18388 : : indxinfo->dobj.catId, 0,
18389 : : is_constraint ? indxinfo->indexconstraint :
18390 : : indxinfo->dobj.dumpId);
18391 : :
18392 : 2724 : destroyPQExpBuffer(q);
18393 : 2724 : destroyPQExpBuffer(delq);
18394 : 2724 : pg_free(qindxname);
18395 : 2724 : pg_free(qqindxname);
18396 : : }
18397 : :
18398 : : /*
18399 : : * dumpIndexAttach
18400 : : * write out to fout a partitioned-index attachment clause
18401 : : */
18402 : : static void
18403 : 615 : dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo)
18404 : : {
18405 : : /* Do nothing if not dumping schema */
18406 [ + + ]: 615 : if (!fout->dopt->dumpSchema)
18407 : 48 : return;
18408 : :
18409 [ + - ]: 567 : if (attachinfo->partitionIdx->dobj.dump & DUMP_COMPONENT_DEFINITION)
18410 : : {
18411 : 567 : PQExpBuffer q = createPQExpBuffer();
18412 : :
18413 : 567 : appendPQExpBuffer(q, "ALTER INDEX %s ",
18414 : 567 : fmtQualifiedDumpable(attachinfo->parentIdx));
18415 : 567 : appendPQExpBuffer(q, "ATTACH PARTITION %s;\n",
18416 : 567 : fmtQualifiedDumpable(attachinfo->partitionIdx));
18417 : :
18418 : : /*
18419 : : * There is no need for a dropStmt since the drop is done implicitly
18420 : : * when we drop either the index's table or the partitioned index.
18421 : : * Moreover, since there's no ALTER INDEX DETACH PARTITION command,
18422 : : * there's no way to do it anyway. (If you think to change this,
18423 : : * consider also what to do with --if-exists.)
18424 : : *
18425 : : * Although this object doesn't really have ownership as such, set the
18426 : : * owner field anyway to ensure that the command is run by the correct
18427 : : * role at restore time.
18428 : : */
18429 : 567 : ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
18430 : 567 : ARCHIVE_OPTS(.tag = attachinfo->dobj.name,
18431 : : .namespace = attachinfo->dobj.namespace->dobj.name,
18432 : : .owner = attachinfo->parentIdx->indextable->rolname,
18433 : : .description = "INDEX ATTACH",
18434 : : .section = SECTION_POST_DATA,
18435 : : .createStmt = q->data));
18436 : :
18437 : 567 : destroyPQExpBuffer(q);
18438 : : }
18439 : : }
18440 : :
18441 : : /*
18442 : : * dumpStatisticsExt
18443 : : * write out to fout an extended statistics object
18444 : : */
18445 : : static void
18446 : 183 : dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo)
18447 : : {
18448 : 183 : DumpOptions *dopt = fout->dopt;
18449 : : PQExpBuffer q;
18450 : : PQExpBuffer delq;
18451 : : PQExpBuffer query;
18452 : : char *qstatsextname;
18453 : : PGresult *res;
18454 : : char *stxdef;
18455 : :
18456 : : /* Do nothing if not dumping schema */
18457 [ + + ]: 183 : if (!dopt->dumpSchema)
18458 : 28 : return;
18459 : :
18460 : 155 : q = createPQExpBuffer();
18461 : 155 : delq = createPQExpBuffer();
18462 : 155 : query = createPQExpBuffer();
18463 : :
18464 : 155 : qstatsextname = pg_strdup(fmtId(statsextinfo->dobj.name));
18465 : :
18466 : 155 : appendPQExpBuffer(query, "SELECT "
18467 : : "pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)",
18468 : 155 : statsextinfo->dobj.catId.oid);
18469 : :
18470 : 155 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
18471 : :
18472 : 155 : stxdef = PQgetvalue(res, 0, 0);
18473 : :
18474 : : /* Result of pg_get_statisticsobjdef is complete except for semicolon */
18475 : 155 : appendPQExpBuffer(q, "%s;\n", stxdef);
18476 : :
18477 : : /*
18478 : : * We only issue an ALTER STATISTICS statement if the stxstattarget entry
18479 : : * for this statistics object is not the default value.
18480 : : */
18481 [ + + ]: 155 : if (statsextinfo->stattarget >= 0)
18482 : : {
18483 : 34 : appendPQExpBuffer(q, "ALTER STATISTICS %s ",
18484 : 34 : fmtQualifiedDumpable(statsextinfo));
18485 : 34 : appendPQExpBuffer(q, "SET STATISTICS %d;\n",
18486 : 34 : statsextinfo->stattarget);
18487 : : }
18488 : :
18489 : 155 : appendPQExpBuffer(delq, "DROP STATISTICS %s;\n",
18490 : 155 : fmtQualifiedDumpable(statsextinfo));
18491 : :
18492 [ + - ]: 155 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18493 : 155 : ArchiveEntry(fout, statsextinfo->dobj.catId,
18494 : 155 : statsextinfo->dobj.dumpId,
18495 : 155 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18496 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18497 : : .owner = statsextinfo->rolname,
18498 : : .description = "STATISTICS",
18499 : : .section = SECTION_POST_DATA,
18500 : : .createStmt = q->data,
18501 : : .dropStmt = delq->data));
18502 : :
18503 : : /* Dump Statistics Comments */
18504 [ - + ]: 155 : if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
18505 : 0 : dumpComment(fout, "STATISTICS", qstatsextname,
18506 : 0 : statsextinfo->dobj.namespace->dobj.name,
18507 : 0 : statsextinfo->rolname,
18508 : : statsextinfo->dobj.catId, 0,
18509 : 0 : statsextinfo->dobj.dumpId);
18510 : :
18511 : 155 : PQclear(res);
18512 : 155 : destroyPQExpBuffer(q);
18513 : 155 : destroyPQExpBuffer(delq);
18514 : 155 : destroyPQExpBuffer(query);
18515 : 155 : pg_free(qstatsextname);
18516 : : }
18517 : :
18518 : : /*
18519 : : * dumpStatisticsExtStats
18520 : : * write out to fout the stats for an extended statistics object
18521 : : */
18522 : : static void
18523 : 183 : dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo)
18524 : : {
18525 : 183 : DumpOptions *dopt = fout->dopt;
18526 : : PQExpBuffer query;
18527 : : PGresult *res;
18528 : : int nstats;
18529 : :
18530 : : /* Do nothing if not dumping statistics */
18531 [ + + ]: 183 : if (!dopt->dumpStatistics)
18532 : 40 : return;
18533 : :
18534 [ + + ]: 143 : if (!fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS])
18535 : : {
18536 : 36 : PQExpBuffer pq = createPQExpBuffer();
18537 : :
18538 : : /*---------
18539 : : * Set up query for details about extended statistics objects.
18540 : : *
18541 : : * The query depends on the backend version:
18542 : : * - In v19 and newer versions, query directly the pg_stats_ext*
18543 : : * catalogs.
18544 : : * - In v18 and older versions, ndistinct and dependencies have a
18545 : : * different format that needs translation.
18546 : : * - In v14 and older versions, inherited does not exist.
18547 : : * - In v11 and older versions, there is no pg_stats_ext, hence
18548 : : * the logic joins pg_statistic_ext and pg_namespace.
18549 : : *---------
18550 : : */
18551 : :
18552 : 36 : appendPQExpBufferStr(pq,
18553 : : "PREPARE getExtStatsStats(pg_catalog.name, pg_catalog.name) AS\n"
18554 : : "SELECT ");
18555 : :
18556 : : /*
18557 : : * Versions 15 and newer have inherited stats.
18558 : : *
18559 : : * Create this column in all versions because we need to order by it
18560 : : * later.
18561 : : */
18562 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18563 : 36 : appendPQExpBufferStr(pq, "e.inherited, ");
18564 : : else
18565 : 0 : appendPQExpBufferStr(pq, "false AS inherited, ");
18566 : :
18567 : : /*--------
18568 : : * The ndistinct and dependencies formats changed in v19, so
18569 : : * everything before that needs to be translated.
18570 : : *
18571 : : * The ndistinct translation converts this kind of data:
18572 : : * {"3, 4": 11, "3, 6": 11, "4, 6": 11, "3, 4, 6": 11}
18573 : : *
18574 : : * to this:
18575 : : * [ {"attributes": [3,4], "ndistinct": 11},
18576 : : * {"attributes": [3,6], "ndistinct": 11},
18577 : : * {"attributes": [4,6], "ndistinct": 11},
18578 : : * {"attributes": [3,4,6], "ndistinct": 11} ]
18579 : : *
18580 : : * The dependencies translation converts this kind of data:
18581 : : * {"3 => 4": 1.000000, "3 => 6": 1.000000,
18582 : : * "4 => 6": 1.000000, "3, 4 => 6": 1.000000,
18583 : : * "3, 6 => 4": 1.000000}
18584 : : *
18585 : : * to this:
18586 : : * [ {"attributes": [3], "dependency": 4, "degree": 1.000000},
18587 : : * {"attributes": [3], "dependency": 6, "degree": 1.000000},
18588 : : * {"attributes": [4], "dependency": 6, "degree": 1.000000},
18589 : : * {"attributes": [3,4], "dependency": 6, "degree": 1.000000},
18590 : : * {"attributes": [3,6], "dependency": 4, "degree": 1.000000} ]
18591 : : *--------
18592 : : */
18593 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18594 : 36 : appendPQExpBufferStr(pq, "e.n_distinct, e.dependencies, ");
18595 : : else
18596 : 0 : appendPQExpBufferStr(pq,
18597 : : "( "
18598 : : "SELECT json_agg( "
18599 : : " json_build_object( "
18600 : : " '" PG_NDISTINCT_KEY_ATTRIBUTES "', "
18601 : : " string_to_array(kv.key, ', ')::integer[], "
18602 : : " '" PG_NDISTINCT_KEY_NDISTINCT "', "
18603 : : " kv.value::bigint )) "
18604 : : "FROM json_each_text(e.n_distinct::text::json) AS kv"
18605 : : ") AS n_distinct, "
18606 : : "( "
18607 : : "SELECT json_agg( "
18608 : : " json_build_object( "
18609 : : " '" PG_DEPENDENCIES_KEY_ATTRIBUTES "', "
18610 : : " string_to_array( "
18611 : : " split_part(kv.key, ' => ', 1), "
18612 : : " ', ')::integer[], "
18613 : : " '" PG_DEPENDENCIES_KEY_DEPENDENCY "', "
18614 : : " split_part(kv.key, ' => ', 2)::integer, "
18615 : : " '" PG_DEPENDENCIES_KEY_DEGREE "', "
18616 : : " kv.value::double precision )) "
18617 : : "FROM json_each_text(e.dependencies::text::json) AS kv "
18618 : : ") AS dependencies, ");
18619 : :
18620 : : /* MCV was introduced v13 */
18621 [ + - ]: 36 : if (fout->remoteVersion >= 130000)
18622 : 36 : appendPQExpBufferStr(pq,
18623 : : "e.most_common_vals, e.most_common_freqs, "
18624 : : "e.most_common_base_freqs, ");
18625 : : else
18626 : 0 : appendPQExpBufferStr(pq,
18627 : : "NULL AS most_common_vals, NULL AS most_common_freqs, "
18628 : : "NULL AS most_common_base_freqs, ");
18629 : :
18630 : : /* Expressions were introduced in v14 */
18631 [ + - ]: 36 : if (fout->remoteVersion >= 140000)
18632 : : {
18633 : : /*
18634 : : * There is no ordering column in pg_stats_ext_exprs. However, we
18635 : : * can rely on the unnesting of pg_statistic_ext_data.stxdexpr to
18636 : : * maintain the desired order of expression elements.
18637 : : */
18638 : 36 : appendPQExpBufferStr(pq,
18639 : : "( "
18640 : : "SELECT jsonb_pretty(jsonb_agg("
18641 : : "nullif(j.obj, '{}'::jsonb))) "
18642 : : "FROM pg_stats_ext_exprs AS ee "
18643 : : "CROSS JOIN LATERAL jsonb_strip_nulls("
18644 : : " jsonb_build_object( "
18645 : : " 'null_frac', ee.null_frac::text, "
18646 : : " 'avg_width', ee.avg_width::text, "
18647 : : " 'n_distinct', ee.n_distinct::text, "
18648 : : " 'most_common_vals', ee.most_common_vals::text, "
18649 : : " 'most_common_freqs', ee.most_common_freqs::text, "
18650 : : " 'histogram_bounds', ee.histogram_bounds::text, "
18651 : : " 'correlation', ee.correlation::text, "
18652 : : " 'most_common_elems', ee.most_common_elems::text, "
18653 : : " 'most_common_elem_freqs', ee.most_common_elem_freqs::text, "
18654 : : " 'elem_count_histogram', ee.elem_count_histogram::text");
18655 : :
18656 : : /* These three have been added to pg_stats_ext_exprs in v19. */
18657 [ + - ]: 36 : if (fout->remoteVersion >= 190000)
18658 : 36 : appendPQExpBufferStr(pq,
18659 : : ", "
18660 : : " 'range_length_histogram', ee.range_length_histogram::text, "
18661 : : " 'range_empty_frac', ee.range_empty_frac::text, "
18662 : : " 'range_bounds_histogram', ee.range_bounds_histogram::text");
18663 : :
18664 : 36 : appendPQExpBufferStr(pq,
18665 : : " )) AS j(obj)"
18666 : : "WHERE ee.statistics_schemaname = $1 "
18667 : : "AND ee.statistics_name = $2 ");
18668 : : /* Inherited expressions introduced in v15 */
18669 [ + - ]: 36 : if (fout->remoteVersion >= 150000)
18670 : 36 : appendPQExpBufferStr(pq, "AND ee.inherited = e.inherited");
18671 : :
18672 : 36 : appendPQExpBufferStr(pq, ") AS exprs ");
18673 : : }
18674 : : else
18675 : 0 : appendPQExpBufferStr(pq, "NULL AS exprs ");
18676 : :
18677 : : /* pg_stats_ext introduced in v12 */
18678 [ + - ]: 36 : if (fout->remoteVersion >= 120000)
18679 : 36 : appendPQExpBufferStr(pq,
18680 : : "FROM pg_catalog.pg_stats_ext AS e "
18681 : : "WHERE e.statistics_schemaname = $1 "
18682 : : "AND e.statistics_name = $2 ");
18683 : : else
18684 : 0 : appendPQExpBufferStr(pq,
18685 : : "FROM ( "
18686 : : "SELECT s.stxndistinct AS n_distinct, "
18687 : : " s.stxdependencies AS dependencies "
18688 : : "FROM pg_catalog.pg_statistic_ext AS s "
18689 : : "JOIN pg_catalog.pg_namespace AS n "
18690 : : "ON n.oid = s.stxnamespace "
18691 : : "WHERE n.nspname = $1 "
18692 : : "AND s.stxname = $2 "
18693 : : ") AS e ");
18694 : :
18695 : : /* we always have an inherited column, but it may be a constant */
18696 : 36 : appendPQExpBufferStr(pq, "ORDER BY inherited");
18697 : :
18698 : 36 : ExecuteSqlStatement(fout, pq->data);
18699 : :
18700 : 36 : fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS] = true;
18701 : :
18702 : 36 : destroyPQExpBuffer(pq);
18703 : : }
18704 : :
18705 : 143 : query = createPQExpBuffer();
18706 : :
18707 : 143 : appendPQExpBufferStr(query, "EXECUTE getExtStatsStats(");
18708 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.namespace->dobj.name, fout);
18709 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name, ");
18710 : 143 : appendStringLiteralAH(query, statsextinfo->dobj.name, fout);
18711 : 143 : appendPQExpBufferStr(query, "::pg_catalog.name)");
18712 : :
18713 : 143 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
18714 : :
18715 : 143 : destroyPQExpBuffer(query);
18716 : :
18717 : 143 : nstats = PQntuples(res);
18718 : :
18719 [ + + ]: 143 : if (nstats > 0)
18720 : : {
18721 : 39 : PQExpBuffer out = createPQExpBuffer();
18722 : :
18723 : 39 : int i_inherited = PQfnumber(res, "inherited");
18724 : 39 : int i_ndistinct = PQfnumber(res, "n_distinct");
18725 : 39 : int i_dependencies = PQfnumber(res, "dependencies");
18726 : 39 : int i_mcv = PQfnumber(res, "most_common_vals");
18727 : 39 : int i_mcf = PQfnumber(res, "most_common_freqs");
18728 : 39 : int i_mcbf = PQfnumber(res, "most_common_base_freqs");
18729 : 39 : int i_exprs = PQfnumber(res, "exprs");
18730 : :
18731 [ + + ]: 78 : for (int i = 0; i < nstats; i++)
18732 : : {
18733 : 39 : TableInfo *tbinfo = statsextinfo->stattable;
18734 : :
18735 [ - + ]: 39 : if (PQgetisnull(res, i, i_inherited))
18736 : 0 : pg_fatal("inherited cannot be NULL");
18737 : :
18738 : 39 : appendPQExpBufferStr(out,
18739 : : "SELECT * FROM pg_catalog.pg_restore_extended_stats(\n");
18740 : 39 : appendPQExpBuffer(out, "\t'version', '%d'::integer,\n",
18741 : : fout->remoteVersion);
18742 : :
18743 : : /* Relation information */
18744 : 39 : appendPQExpBufferStr(out, "\t'schemaname', ");
18745 : 39 : appendStringLiteralAH(out, tbinfo->dobj.namespace->dobj.name, fout);
18746 : 39 : appendPQExpBufferStr(out, ",\n\t'relname', ");
18747 : 39 : appendStringLiteralAH(out, tbinfo->dobj.name, fout);
18748 : :
18749 : : /* Extended statistics information */
18750 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_schemaname', ");
18751 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.namespace->dobj.name, fout);
18752 : 39 : appendPQExpBufferStr(out, ",\n\t'statistics_name', ");
18753 : 39 : appendStringLiteralAH(out, statsextinfo->dobj.name, fout);
18754 : 39 : appendNamedArgument(out, fout, "inherited", "boolean",
18755 : 39 : PQgetvalue(res, i, i_inherited));
18756 : :
18757 [ + + ]: 39 : if (!PQgetisnull(res, i, i_ndistinct))
18758 : 35 : appendNamedArgument(out, fout, "n_distinct", "pg_ndistinct",
18759 : 35 : PQgetvalue(res, i, i_ndistinct));
18760 : :
18761 [ + + ]: 39 : if (!PQgetisnull(res, i, i_dependencies))
18762 : 36 : appendNamedArgument(out, fout, "dependencies", "pg_dependencies",
18763 : 36 : PQgetvalue(res, i, i_dependencies));
18764 : :
18765 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcv))
18766 : 38 : appendNamedArgument(out, fout, "most_common_vals", "text[]",
18767 : 38 : PQgetvalue(res, i, i_mcv));
18768 : :
18769 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcf))
18770 : 38 : appendNamedArgument(out, fout, "most_common_freqs", "double precision[]",
18771 : 38 : PQgetvalue(res, i, i_mcf));
18772 : :
18773 [ + + ]: 39 : if (!PQgetisnull(res, i, i_mcbf))
18774 : 38 : appendNamedArgument(out, fout, "most_common_base_freqs", "double precision[]",
18775 : 38 : PQgetvalue(res, i, i_mcbf));
18776 : :
18777 [ + + ]: 39 : if (!PQgetisnull(res, i, i_exprs))
18778 : 36 : appendNamedArgument(out, fout, "exprs", "jsonb",
18779 : 36 : PQgetvalue(res, i, i_exprs));
18780 : :
18781 : 39 : appendPQExpBufferStr(out, "\n);\n");
18782 : : }
18783 : :
18784 : 39 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
18785 : 39 : ARCHIVE_OPTS(.tag = statsextinfo->dobj.name,
18786 : : .namespace = statsextinfo->dobj.namespace->dobj.name,
18787 : : .owner = statsextinfo->rolname,
18788 : : .description = "EXTENDED STATISTICS DATA",
18789 : : .section = SECTION_POST_DATA,
18790 : : .createStmt = out->data,
18791 : : .deps = &statsextinfo->dobj.dumpId,
18792 : : .nDeps = 1));
18793 : 39 : destroyPQExpBuffer(out);
18794 : : }
18795 : 143 : PQclear(res);
18796 : : }
18797 : :
18798 : : /*
18799 : : * dumpConstraint
18800 : : * write out to fout a user-defined constraint
18801 : : */
18802 : : static void
18803 : 2810 : dumpConstraint(Archive *fout, const ConstraintInfo *coninfo)
18804 : : {
18805 : 2810 : DumpOptions *dopt = fout->dopt;
18806 : 2810 : TableInfo *tbinfo = coninfo->contable;
18807 : : PQExpBuffer q;
18808 : : PQExpBuffer delq;
18809 : 2810 : char *tag = NULL;
18810 : : char *foreign;
18811 : :
18812 : : /* Do nothing if not dumping schema */
18813 [ + + ]: 2810 : if (!dopt->dumpSchema)
18814 : 110 : return;
18815 : :
18816 : 2700 : q = createPQExpBuffer();
18817 : 2700 : delq = createPQExpBuffer();
18818 : :
18819 : 5222 : foreign = tbinfo &&
18820 [ + + - + ]: 2700 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
18821 : :
18822 [ + + ]: 2700 : if (coninfo->contype == 'p' ||
18823 [ + + ]: 1329 : coninfo->contype == 'u' ||
18824 [ + + ]: 1083 : coninfo->contype == 'x')
18825 : 1637 : {
18826 : : /* Index-related constraint */
18827 : : IndxInfo *indxinfo;
18828 : : int k;
18829 : :
18830 : 1637 : indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
18831 : :
18832 [ - + ]: 1637 : if (indxinfo == NULL)
18833 : 0 : pg_fatal("missing index for constraint \"%s\"",
18834 : : coninfo->dobj.name);
18835 : :
18836 [ + + ]: 1637 : if (dopt->binary_upgrade)
18837 : 179 : binary_upgrade_set_pg_class_oids(fout, q,
18838 : : indxinfo->dobj.catId.oid);
18839 : :
18840 : 1637 : appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
18841 : 1637 : fmtQualifiedDumpable(tbinfo));
18842 : 1637 : appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
18843 : 1637 : fmtId(coninfo->dobj.name));
18844 : :
18845 [ + + ]: 1637 : if (coninfo->condef)
18846 : : {
18847 : : /* pg_get_constraintdef should have provided everything */
18848 : 20 : appendPQExpBuffer(q, "%s;\n", coninfo->condef);
18849 : : }
18850 : : else
18851 : : {
18852 : 1617 : appendPQExpBufferStr(q,
18853 [ + + ]: 1617 : coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
18854 : :
18855 : : /*
18856 : : * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
18857 : : * indexes. Being able to create this was fixed, but we need to
18858 : : * make the index distinct in order to be able to restore the
18859 : : * dump.
18860 : : */
18861 [ - + - - ]: 1617 : if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
18862 : 0 : appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
18863 : 1617 : appendPQExpBufferStr(q, " (");
18864 [ + + ]: 3865 : for (k = 0; k < indxinfo->indnkeyattrs; k++)
18865 : : {
18866 : 2248 : int indkey = indxinfo->indkeys[k];
18867 : : const char *attname;
18868 : :
18869 [ - + ]: 2248 : if (indkey == InvalidAttrNumber)
18870 : 0 : break;
18871 : 2248 : attname = getAttrName(indkey, tbinfo);
18872 : :
18873 [ + + ]: 2248 : appendPQExpBuffer(q, "%s%s",
18874 : : (k == 0) ? "" : ", ",
18875 : : fmtId(attname));
18876 : : }
18877 [ + + ]: 1617 : if (coninfo->conperiod)
18878 : 113 : appendPQExpBufferStr(q, " WITHOUT OVERLAPS");
18879 : :
18880 [ + + ]: 1617 : if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
18881 : 20 : appendPQExpBufferStr(q, ") INCLUDE (");
18882 : :
18883 [ + + ]: 1657 : for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
18884 : : {
18885 : 40 : int indkey = indxinfo->indkeys[k];
18886 : : const char *attname;
18887 : :
18888 [ - + ]: 40 : if (indkey == InvalidAttrNumber)
18889 : 0 : break;
18890 : 40 : attname = getAttrName(indkey, tbinfo);
18891 : :
18892 : 80 : appendPQExpBuffer(q, "%s%s",
18893 [ + + ]: 40 : (k == indxinfo->indnkeyattrs) ? "" : ", ",
18894 : : fmtId(attname));
18895 : : }
18896 : :
18897 : 1617 : appendPQExpBufferChar(q, ')');
18898 : :
18899 [ - + ]: 1617 : if (nonemptyReloptions(indxinfo->indreloptions))
18900 : : {
18901 : 0 : appendPQExpBufferStr(q, " WITH (");
18902 : 0 : appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
18903 : 0 : appendPQExpBufferChar(q, ')');
18904 : : }
18905 : :
18906 [ + + ]: 1617 : if (coninfo->condeferrable)
18907 : : {
18908 : 25 : appendPQExpBufferStr(q, " DEFERRABLE");
18909 [ + + ]: 25 : if (coninfo->condeferred)
18910 : 15 : appendPQExpBufferStr(q, " INITIALLY DEFERRED");
18911 : : }
18912 : :
18913 : 1617 : appendPQExpBufferStr(q, ";\n");
18914 : : }
18915 : :
18916 : : /*
18917 : : * Append ALTER TABLE commands as needed to set properties that we
18918 : : * only have ALTER TABLE syntax for. Keep this in sync with the
18919 : : * similar code in dumpIndex!
18920 : : */
18921 : :
18922 : : /* If the index is clustered, we need to record that. */
18923 [ + + ]: 1637 : if (indxinfo->indisclustered)
18924 : : {
18925 : 34 : appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
18926 : 34 : fmtQualifiedDumpable(tbinfo));
18927 : : /* index name is not qualified in this syntax */
18928 : 34 : appendPQExpBuffer(q, " ON %s;\n",
18929 : 34 : fmtId(indxinfo->dobj.name));
18930 : : }
18931 : :
18932 : : /* If the index defines identity, we need to record that. */
18933 [ - + ]: 1637 : if (indxinfo->indisreplident)
18934 : : {
18935 : 0 : appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
18936 : 0 : fmtQualifiedDumpable(tbinfo));
18937 : : /* index name is not qualified in this syntax */
18938 : 0 : appendPQExpBuffer(q, " INDEX %s;\n",
18939 : 0 : fmtId(indxinfo->dobj.name));
18940 : : }
18941 : :
18942 : : /* Indexes can depend on extensions */
18943 : 1637 : append_depends_on_extension(fout, q, &indxinfo->dobj,
18944 : : "pg_catalog.pg_class", "INDEX",
18945 : 1637 : fmtQualifiedDumpable(indxinfo));
18946 : :
18947 : 1637 : appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
18948 : 1637 : fmtQualifiedDumpable(tbinfo));
18949 : 1637 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
18950 : 1637 : fmtId(coninfo->dobj.name));
18951 : :
18952 : 1637 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
18953 : :
18954 [ + - ]: 1637 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18955 : 1637 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
18956 : 1637 : ARCHIVE_OPTS(.tag = tag,
18957 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18958 : : .tablespace = indxinfo->tablespace,
18959 : : .owner = tbinfo->rolname,
18960 : : .description = "CONSTRAINT",
18961 : : .section = SECTION_POST_DATA,
18962 : : .createStmt = q->data,
18963 : : .dropStmt = delq->data));
18964 : : }
18965 [ + + ]: 1063 : else if (coninfo->contype == 'f')
18966 : : {
18967 : : char *only;
18968 : :
18969 : : /*
18970 : : * Foreign keys on partitioned tables are always declared as
18971 : : * inheriting to partitions; for all other cases, emit them as
18972 : : * applying ONLY directly to the named table, because that's how they
18973 : : * work for regular inherited tables.
18974 : : */
18975 [ + + ]: 223 : only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
18976 : :
18977 : : /*
18978 : : * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
18979 : : * current table data is not processed
18980 : : */
18981 : 223 : appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
18982 : 223 : only, fmtQualifiedDumpable(tbinfo));
18983 : 223 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
18984 : 223 : fmtId(coninfo->dobj.name),
18985 : 223 : coninfo->condef);
18986 : :
18987 : 223 : appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
18988 : 223 : only, fmtQualifiedDumpable(tbinfo));
18989 : 223 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
18990 : 223 : fmtId(coninfo->dobj.name));
18991 : :
18992 : 223 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
18993 : :
18994 [ + - ]: 223 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
18995 : 223 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
18996 : 223 : ARCHIVE_OPTS(.tag = tag,
18997 : : .namespace = tbinfo->dobj.namespace->dobj.name,
18998 : : .owner = tbinfo->rolname,
18999 : : .description = "FK CONSTRAINT",
19000 : : .section = SECTION_POST_DATA,
19001 : : .createStmt = q->data,
19002 : : .dropStmt = delq->data));
19003 : : }
19004 [ + + + - : 840 : else if ((coninfo->contype == 'c' || coninfo->contype == 'n') && tbinfo)
+ + ]
19005 : : {
19006 : : /* CHECK or invalid not-null constraint on a table */
19007 : :
19008 : : /* Ignore if not to be dumped separately, or if it was inherited */
19009 [ + + + + ]: 662 : if (coninfo->separate && coninfo->conislocal)
19010 : : {
19011 : : const char *keyword;
19012 : :
19013 [ + + ]: 109 : if (coninfo->contype == 'c')
19014 : 45 : keyword = "CHECK CONSTRAINT";
19015 : : else
19016 : 64 : keyword = "CONSTRAINT";
19017 : :
19018 : : /* not ONLY since we want it to propagate to children */
19019 : 109 : appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
19020 : 109 : fmtQualifiedDumpable(tbinfo));
19021 : 109 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19022 : 109 : fmtId(coninfo->dobj.name),
19023 : 109 : coninfo->condef);
19024 : :
19025 : 109 : appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
19026 : 109 : fmtQualifiedDumpable(tbinfo));
19027 : 109 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19028 : 109 : fmtId(coninfo->dobj.name));
19029 : :
19030 : 109 : tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
19031 : :
19032 [ + - ]: 109 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19033 : 109 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19034 : 109 : ARCHIVE_OPTS(.tag = tag,
19035 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19036 : : .owner = tbinfo->rolname,
19037 : : .description = keyword,
19038 : : .section = SECTION_POST_DATA,
19039 : : .createStmt = q->data,
19040 : : .dropStmt = delq->data));
19041 : : }
19042 : : }
19043 [ + - ]: 178 : else if (tbinfo == NULL)
19044 : : {
19045 : : /* CHECK, NOT NULL constraint on a domain */
19046 : 178 : TypeInfo *tyinfo = coninfo->condomain;
19047 : :
19048 : : Assert(coninfo->contype == 'c' || coninfo->contype == 'n');
19049 : :
19050 : : /* Ignore if not to be dumped separately */
19051 [ + + ]: 178 : if (coninfo->separate)
19052 : : {
19053 : : const char *keyword;
19054 : :
19055 [ + - ]: 5 : if (coninfo->contype == 'c')
19056 : 5 : keyword = "CHECK CONSTRAINT";
19057 : : else
19058 : 0 : keyword = "CONSTRAINT";
19059 : :
19060 : 5 : appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
19061 : 5 : fmtQualifiedDumpable(tyinfo));
19062 : 5 : appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
19063 : 5 : fmtId(coninfo->dobj.name),
19064 : 5 : coninfo->condef);
19065 : :
19066 : 5 : appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
19067 : 5 : fmtQualifiedDumpable(tyinfo));
19068 : 5 : appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
19069 : 5 : fmtId(coninfo->dobj.name));
19070 : :
19071 : 5 : tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
19072 : :
19073 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19074 : 5 : ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
19075 : 5 : ARCHIVE_OPTS(.tag = tag,
19076 : : .namespace = tyinfo->dobj.namespace->dobj.name,
19077 : : .owner = tyinfo->rolname,
19078 : : .description = keyword,
19079 : : .section = SECTION_POST_DATA,
19080 : : .createStmt = q->data,
19081 : : .dropStmt = delq->data));
19082 : :
19083 [ + - ]: 5 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19084 : : {
19085 : 5 : PQExpBuffer conprefix = createPQExpBuffer();
19086 : 5 : char *qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
19087 : :
19088 : 5 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
19089 : 5 : fmtId(coninfo->dobj.name));
19090 : :
19091 : 5 : dumpComment(fout, conprefix->data, qtypname,
19092 : 5 : tyinfo->dobj.namespace->dobj.name,
19093 : : tyinfo->rolname,
19094 : 5 : coninfo->dobj.catId, 0, coninfo->dobj.dumpId);
19095 : 5 : destroyPQExpBuffer(conprefix);
19096 : 5 : pg_free(qtypname);
19097 : : }
19098 : : }
19099 : : }
19100 : : else
19101 : : {
19102 : 0 : pg_fatal("unrecognized constraint type: %c",
19103 : : coninfo->contype);
19104 : : }
19105 : :
19106 : : /* Dump Constraint Comments --- only works for table constraints */
19107 [ + + + + ]: 2700 : if (tbinfo && coninfo->separate &&
19108 [ + + ]: 1999 : coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19109 : 49 : dumpTableConstraintComment(fout, coninfo);
19110 : :
19111 : 2700 : pfree(tag);
19112 : 2700 : destroyPQExpBuffer(q);
19113 : 2700 : destroyPQExpBuffer(delq);
19114 : : }
19115 : :
19116 : : /*
19117 : : * dumpTableConstraintComment --- dump a constraint's comment if any
19118 : : *
19119 : : * This is split out because we need the function in two different places
19120 : : * depending on whether the constraint is dumped as part of CREATE TABLE
19121 : : * or as a separate ALTER command.
19122 : : */
19123 : : static void
19124 : 88 : dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
19125 : : {
19126 : 88 : TableInfo *tbinfo = coninfo->contable;
19127 : 88 : PQExpBuffer conprefix = createPQExpBuffer();
19128 : : char *qtabname;
19129 : :
19130 : 88 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19131 : :
19132 : 88 : appendPQExpBuffer(conprefix, "CONSTRAINT %s ON",
19133 : 88 : fmtId(coninfo->dobj.name));
19134 : :
19135 [ + - ]: 88 : if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19136 : 88 : dumpComment(fout, conprefix->data, qtabname,
19137 : 88 : tbinfo->dobj.namespace->dobj.name,
19138 : : tbinfo->rolname,
19139 : : coninfo->dobj.catId, 0,
19140 [ + + ]: 88 : coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
19141 : :
19142 : 88 : destroyPQExpBuffer(conprefix);
19143 : 88 : pg_free(qtabname);
19144 : 88 : }
19145 : :
19146 : : static inline SeqType
19147 : 647 : parse_sequence_type(const char *name)
19148 : : {
19149 [ + - ]: 1443 : for (size_t i = 0; i < lengthof(SeqTypeNames); i++)
19150 : : {
19151 [ + + ]: 1443 : if (strcmp(SeqTypeNames[i], name) == 0)
19152 : 647 : return (SeqType) i;
19153 : : }
19154 : :
19155 : 0 : pg_fatal("unrecognized sequence type: %s", name);
19156 : : return (SeqType) 0; /* keep compiler quiet */
19157 : : }
19158 : :
19159 : : /*
19160 : : * bsearch() comparator for SequenceItem
19161 : : */
19162 : : static int
19163 : 2976 : SequenceItemCmp(const void *p1, const void *p2)
19164 : : {
19165 : 2976 : SequenceItem v1 = *((const SequenceItem *) p1);
19166 : 2976 : SequenceItem v2 = *((const SequenceItem *) p2);
19167 : :
19168 : 2976 : return pg_cmp_u32(v1.oid, v2.oid);
19169 : : }
19170 : :
19171 : : /*
19172 : : * collectSequences
19173 : : *
19174 : : * Construct a table of sequence information. This table is sorted by OID for
19175 : : * speed in lookup.
19176 : : */
19177 : : static void
19178 : 193 : collectSequences(Archive *fout)
19179 : : {
19180 : : PGresult *res;
19181 : : const char *query;
19182 : :
19183 : : /*
19184 : : * Since version 18, we can gather the sequence data in this query with
19185 : : * pg_get_sequence_data(), but we only do so for non-schema-only dumps.
19186 : : */
19187 [ + - ]: 193 : if (fout->remoteVersion < 180000 ||
19188 [ + + + + ]: 193 : (!fout->dopt->dumpData && !fout->dopt->sequence_data))
19189 : 9 : query = "SELECT seqrelid, format_type(seqtypid, NULL), "
19190 : : "seqstart, seqincrement, "
19191 : : "seqmax, seqmin, "
19192 : : "seqcache, seqcycle, "
19193 : : "NULL, 'f' "
19194 : : "FROM pg_catalog.pg_sequence "
19195 : : "ORDER BY seqrelid";
19196 : : else
19197 : 184 : query = "SELECT seqrelid, format_type(seqtypid, NULL), "
19198 : : "seqstart, seqincrement, "
19199 : : "seqmax, seqmin, "
19200 : : "seqcache, seqcycle, "
19201 : : "last_value, is_called "
19202 : : "FROM pg_catalog.pg_sequence, "
19203 : : "pg_get_sequence_data(seqrelid) "
19204 : : "ORDER BY seqrelid;";
19205 : :
19206 : 193 : res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
19207 : :
19208 : 193 : nsequences = PQntuples(res);
19209 : 193 : sequences = pg_malloc_array(SequenceItem, nsequences);
19210 : :
19211 [ + + ]: 840 : for (int i = 0; i < nsequences; i++)
19212 : : {
19213 : 647 : sequences[i].oid = atooid(PQgetvalue(res, i, 0));
19214 : 647 : sequences[i].seqtype = parse_sequence_type(PQgetvalue(res, i, 1));
19215 : 647 : sequences[i].startv = strtoi64(PQgetvalue(res, i, 2), NULL, 10);
19216 : 647 : sequences[i].incby = strtoi64(PQgetvalue(res, i, 3), NULL, 10);
19217 : 647 : sequences[i].maxv = strtoi64(PQgetvalue(res, i, 4), NULL, 10);
19218 : 647 : sequences[i].minv = strtoi64(PQgetvalue(res, i, 5), NULL, 10);
19219 : 647 : sequences[i].cache = strtoi64(PQgetvalue(res, i, 6), NULL, 10);
19220 : 647 : sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0);
19221 : 647 : sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10);
19222 : 647 : sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0);
19223 [ + + - + ]: 647 : sequences[i].null_seqtuple = (PQgetisnull(res, i, 8) || PQgetisnull(res, i, 9));
19224 : : }
19225 : :
19226 : 193 : PQclear(res);
19227 : 193 : }
19228 : :
19229 : : /*
19230 : : * dumpSequence
19231 : : * write the declaration (not data) of one user-defined sequence
19232 : : */
19233 : : static void
19234 : 381 : dumpSequence(Archive *fout, const TableInfo *tbinfo)
19235 : : {
19236 : 381 : DumpOptions *dopt = fout->dopt;
19237 : : SequenceItem *seq;
19238 : : bool is_ascending;
19239 : : int64 default_minv,
19240 : : default_maxv;
19241 : 381 : PQExpBuffer query = createPQExpBuffer();
19242 : 381 : PQExpBuffer delqry = createPQExpBuffer();
19243 : : char *qseqname;
19244 : 381 : TableInfo *owning_tab = NULL;
19245 : 381 : SequenceItem key = {0};
19246 : :
19247 : 381 : qseqname = pg_strdup(fmtId(tbinfo->dobj.name));
19248 : :
19249 : : /*
19250 : : * The sequence information is gathered in a sorted table before any calls
19251 : : * to dumpSequence(). See collectSequences() for more information.
19252 : : */
19253 : : Assert(sequences);
19254 : :
19255 : 381 : key.oid = tbinfo->dobj.catId.oid;
19256 : 381 : seq = bsearch(&key, sequences, nsequences,
19257 : : sizeof(SequenceItem), SequenceItemCmp);
19258 : :
19259 : : /* Calculate default limits for a sequence of this type */
19260 : 381 : is_ascending = (seq->incby >= 0);
19261 [ + + ]: 381 : if (seq->seqtype == SEQTYPE_SMALLINT)
19262 : : {
19263 [ + + ]: 25 : default_minv = is_ascending ? 1 : PG_INT16_MIN;
19264 [ + + ]: 25 : default_maxv = is_ascending ? PG_INT16_MAX : -1;
19265 : : }
19266 [ + + ]: 356 : else if (seq->seqtype == SEQTYPE_INTEGER)
19267 : : {
19268 [ + + ]: 290 : default_minv = is_ascending ? 1 : PG_INT32_MIN;
19269 [ + + ]: 290 : default_maxv = is_ascending ? PG_INT32_MAX : -1;
19270 : : }
19271 [ + - ]: 66 : else if (seq->seqtype == SEQTYPE_BIGINT)
19272 : : {
19273 [ + + ]: 66 : default_minv = is_ascending ? 1 : PG_INT64_MIN;
19274 [ + + ]: 66 : default_maxv = is_ascending ? PG_INT64_MAX : -1;
19275 : : }
19276 : : else
19277 : : {
19278 : 0 : pg_fatal("unrecognized sequence type: %d", seq->seqtype);
19279 : : default_minv = default_maxv = 0; /* keep compiler quiet */
19280 : : }
19281 : :
19282 : : /*
19283 : : * Identity sequences are not to be dropped separately.
19284 : : */
19285 [ + + ]: 381 : if (!tbinfo->is_identity_sequence)
19286 : : {
19287 : 237 : appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n",
19288 : 237 : fmtQualifiedDumpable(tbinfo));
19289 : : }
19290 : :
19291 : 381 : resetPQExpBuffer(query);
19292 : :
19293 [ + + ]: 381 : if (dopt->binary_upgrade)
19294 : : {
19295 : 66 : binary_upgrade_set_pg_class_oids(fout, query,
19296 : 66 : tbinfo->dobj.catId.oid);
19297 : :
19298 : : /*
19299 : : * In older PG versions a sequence will have a pg_type entry, but v14
19300 : : * and up don't use that, so don't attempt to preserve the type OID.
19301 : : */
19302 : : }
19303 : :
19304 [ + + ]: 381 : if (tbinfo->is_identity_sequence)
19305 : : {
19306 : 144 : owning_tab = findTableByOid(tbinfo->owning_tab);
19307 : :
19308 : 144 : appendPQExpBuffer(query,
19309 : : "ALTER TABLE %s ",
19310 : 144 : fmtQualifiedDumpable(owning_tab));
19311 : 144 : appendPQExpBuffer(query,
19312 : : "ALTER COLUMN %s ADD GENERATED ",
19313 : 144 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19314 [ + + ]: 144 : if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS)
19315 : 104 : appendPQExpBufferStr(query, "ALWAYS");
19316 [ + - ]: 40 : else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
19317 : 40 : appendPQExpBufferStr(query, "BY DEFAULT");
19318 : 144 : appendPQExpBuffer(query, " AS IDENTITY (\n SEQUENCE NAME %s\n",
19319 : 144 : fmtQualifiedDumpable(tbinfo));
19320 : :
19321 : : /*
19322 : : * Emit persistence option only if it's different from the owning
19323 : : * table's. This avoids using this new syntax unnecessarily.
19324 : : */
19325 [ + + ]: 144 : if (tbinfo->relpersistence != owning_tab->relpersistence)
19326 : 10 : appendPQExpBuffer(query, " %s\n",
19327 [ + + ]: 10 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19328 : : "UNLOGGED" : "LOGGED");
19329 : : }
19330 : : else
19331 : : {
19332 : 237 : appendPQExpBuffer(query,
19333 : : "CREATE %sSEQUENCE %s\n",
19334 [ + + ]: 237 : tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
19335 : : "UNLOGGED " : "",
19336 : 237 : fmtQualifiedDumpable(tbinfo));
19337 : :
19338 [ + + ]: 237 : if (seq->seqtype != SEQTYPE_BIGINT)
19339 : 186 : appendPQExpBuffer(query, " AS %s\n", SeqTypeNames[seq->seqtype]);
19340 : : }
19341 : :
19342 : 381 : appendPQExpBuffer(query, " START WITH " INT64_FORMAT "\n", seq->startv);
19343 : :
19344 : 381 : appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT "\n", seq->incby);
19345 : :
19346 [ + + ]: 381 : if (seq->minv != default_minv)
19347 : 15 : appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT "\n", seq->minv);
19348 : : else
19349 : 366 : appendPQExpBufferStr(query, " NO MINVALUE\n");
19350 : :
19351 [ + + ]: 381 : if (seq->maxv != default_maxv)
19352 : 15 : appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT "\n", seq->maxv);
19353 : : else
19354 : 366 : appendPQExpBufferStr(query, " NO MAXVALUE\n");
19355 : :
19356 : 381 : appendPQExpBuffer(query,
19357 : : " CACHE " INT64_FORMAT "%s",
19358 [ + + ]: 381 : seq->cache, (seq->cycled ? "\n CYCLE" : ""));
19359 : :
19360 [ + + ]: 381 : if (tbinfo->is_identity_sequence)
19361 : 144 : appendPQExpBufferStr(query, "\n);\n");
19362 : : else
19363 : 237 : appendPQExpBufferStr(query, ";\n");
19364 : :
19365 : : /* binary_upgrade: no need to clear TOAST table oid */
19366 : :
19367 [ + + ]: 381 : if (dopt->binary_upgrade)
19368 : 66 : binary_upgrade_extension_member(query, &tbinfo->dobj,
19369 : : "SEQUENCE", qseqname,
19370 : 66 : tbinfo->dobj.namespace->dobj.name);
19371 : :
19372 [ + - ]: 381 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19373 : 381 : ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
19374 : 381 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19375 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19376 : : .owner = tbinfo->rolname,
19377 : : .description = "SEQUENCE",
19378 : : .section = SECTION_PRE_DATA,
19379 : : .createStmt = query->data,
19380 : : .dropStmt = delqry->data));
19381 : :
19382 : : /*
19383 : : * If the sequence is owned by a table column, emit the ALTER for it as a
19384 : : * separate TOC entry immediately following the sequence's own entry. It's
19385 : : * OK to do this rather than using full sorting logic, because the
19386 : : * dependency that tells us it's owned will have forced the table to be
19387 : : * created first. We can't just include the ALTER in the TOC entry
19388 : : * because it will fail if we haven't reassigned the sequence owner to
19389 : : * match the table's owner.
19390 : : *
19391 : : * We need not schema-qualify the table reference because both sequence
19392 : : * and table must be in the same schema.
19393 : : */
19394 [ + + + + ]: 381 : if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence)
19395 : : {
19396 : 141 : owning_tab = findTableByOid(tbinfo->owning_tab);
19397 : :
19398 [ - + ]: 141 : if (owning_tab == NULL)
19399 : 0 : pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found",
19400 : : tbinfo->owning_tab, tbinfo->dobj.catId.oid);
19401 : :
19402 [ + + ]: 141 : if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION)
19403 : : {
19404 : 139 : resetPQExpBuffer(query);
19405 : 139 : appendPQExpBuffer(query, "ALTER SEQUENCE %s",
19406 : 139 : fmtQualifiedDumpable(tbinfo));
19407 : 139 : appendPQExpBuffer(query, " OWNED BY %s",
19408 : 139 : fmtQualifiedDumpable(owning_tab));
19409 : 139 : appendPQExpBuffer(query, ".%s;\n",
19410 : 139 : fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
19411 : :
19412 [ + - ]: 139 : if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19413 : 139 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19414 : 139 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19415 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19416 : : .owner = tbinfo->rolname,
19417 : : .description = "SEQUENCE OWNED BY",
19418 : : .section = SECTION_PRE_DATA,
19419 : : .createStmt = query->data,
19420 : : .deps = &(tbinfo->dobj.dumpId),
19421 : : .nDeps = 1));
19422 : : }
19423 : : }
19424 : :
19425 : : /* Dump Sequence Comments and Security Labels */
19426 [ - + ]: 381 : if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19427 : 0 : dumpComment(fout, "SEQUENCE", qseqname,
19428 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19429 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19430 : :
19431 [ - + ]: 381 : if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19432 : 0 : dumpSecLabel(fout, "SEQUENCE", qseqname,
19433 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19434 : 0 : tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
19435 : :
19436 : 381 : destroyPQExpBuffer(query);
19437 : 381 : destroyPQExpBuffer(delqry);
19438 : 381 : pg_free(qseqname);
19439 : 381 : }
19440 : :
19441 : : /*
19442 : : * dumpSequenceData
19443 : : * write the data of one user-defined sequence
19444 : : */
19445 : : static void
19446 : 399 : dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo)
19447 : : {
19448 : 399 : TableInfo *tbinfo = tdinfo->tdtable;
19449 : : int64 last;
19450 : : bool called;
19451 : : PQExpBuffer query;
19452 : :
19453 : : /* needn't bother if not dumping sequence data */
19454 [ + + + + ]: 399 : if (!fout->dopt->dumpData && !fout->dopt->sequence_data)
19455 : 1 : return;
19456 : :
19457 : 398 : query = createPQExpBuffer();
19458 : :
19459 : : /*
19460 : : * For versions >= 18, the sequence information is gathered in the sorted
19461 : : * array before any calls to dumpSequenceData(). See collectSequences()
19462 : : * for more information.
19463 : : *
19464 : : * For older versions, we have to query the sequence relations
19465 : : * individually.
19466 : : */
19467 [ - + ]: 398 : if (fout->remoteVersion < 180000)
19468 : : {
19469 : : PGresult *res;
19470 : :
19471 : 0 : appendPQExpBuffer(query,
19472 : : "SELECT last_value, is_called FROM %s",
19473 : 0 : fmtQualifiedDumpable(tbinfo));
19474 : :
19475 : 0 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19476 : :
19477 [ # # ]: 0 : if (PQntuples(res) != 1)
19478 : 0 : pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)",
19479 : : "query to get data of sequence \"%s\" returned %d rows (expected 1)",
19480 : : PQntuples(res)),
19481 : : tbinfo->dobj.name, PQntuples(res));
19482 : :
19483 : 0 : last = strtoi64(PQgetvalue(res, 0, 0), NULL, 10);
19484 : 0 : called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
19485 : :
19486 : 0 : PQclear(res);
19487 : : }
19488 : : else
19489 : : {
19490 : 398 : SequenceItem key = {0};
19491 : : SequenceItem *entry;
19492 : :
19493 : : Assert(sequences);
19494 : : Assert(tbinfo->dobj.catId.oid);
19495 : :
19496 : 398 : key.oid = tbinfo->dobj.catId.oid;
19497 : 398 : entry = bsearch(&key, sequences, nsequences,
19498 : : sizeof(SequenceItem), SequenceItemCmp);
19499 : :
19500 [ - + ]: 398 : if (entry->null_seqtuple)
19501 : 0 : pg_fatal("failed to get data for sequence \"%s\"; user may lack "
19502 : : "SELECT privilege on the sequence or the sequence may "
19503 : : "have been concurrently dropped",
19504 : : tbinfo->dobj.name);
19505 : :
19506 : 398 : last = entry->last_value;
19507 : 398 : called = entry->is_called;
19508 : : }
19509 : :
19510 : 398 : resetPQExpBuffer(query);
19511 : 398 : appendPQExpBufferStr(query, "SELECT pg_catalog.setval(");
19512 : 398 : appendStringLiteralAH(query, fmtQualifiedDumpable(tbinfo), fout);
19513 [ + + ]: 398 : appendPQExpBuffer(query, ", " INT64_FORMAT ", %s);\n",
19514 : : last, (called ? "true" : "false"));
19515 : :
19516 [ + - ]: 398 : if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
19517 : 398 : ArchiveEntry(fout, nilCatalogId, createDumpId(),
19518 : 398 : ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
19519 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19520 : : .owner = tbinfo->rolname,
19521 : : .description = "SEQUENCE SET",
19522 : : .section = SECTION_DATA,
19523 : : .createStmt = query->data,
19524 : : .deps = &(tbinfo->dobj.dumpId),
19525 : : .nDeps = 1));
19526 : :
19527 : 398 : destroyPQExpBuffer(query);
19528 : : }
19529 : :
19530 : : /*
19531 : : * dumpTrigger
19532 : : * write the declaration of one user-defined table trigger
19533 : : */
19534 : : static void
19535 : 535 : dumpTrigger(Archive *fout, const TriggerInfo *tginfo)
19536 : : {
19537 : 535 : DumpOptions *dopt = fout->dopt;
19538 : 535 : TableInfo *tbinfo = tginfo->tgtable;
19539 : : PQExpBuffer query;
19540 : : PQExpBuffer delqry;
19541 : : PQExpBuffer trigprefix;
19542 : : PQExpBuffer trigidentity;
19543 : : char *qtabname;
19544 : : char *tag;
19545 : :
19546 : : /* Do nothing if not dumping schema */
19547 [ + + ]: 535 : if (!dopt->dumpSchema)
19548 : 33 : return;
19549 : :
19550 : 502 : query = createPQExpBuffer();
19551 : 502 : delqry = createPQExpBuffer();
19552 : 502 : trigprefix = createPQExpBuffer();
19553 : 502 : trigidentity = createPQExpBuffer();
19554 : :
19555 : 502 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19556 : :
19557 : 502 : appendPQExpBuffer(trigidentity, "%s ", fmtId(tginfo->dobj.name));
19558 : 502 : appendPQExpBuffer(trigidentity, "ON %s", fmtQualifiedDumpable(tbinfo));
19559 : :
19560 : 502 : appendPQExpBuffer(query, "%s;\n", tginfo->tgdef);
19561 : 502 : appendPQExpBuffer(delqry, "DROP TRIGGER %s;\n", trigidentity->data);
19562 : :
19563 : : /* Triggers can depend on extensions */
19564 : 502 : append_depends_on_extension(fout, query, &tginfo->dobj,
19565 : : "pg_catalog.pg_trigger", "TRIGGER",
19566 : 502 : trigidentity->data);
19567 : :
19568 [ + + ]: 502 : if (tginfo->tgispartition)
19569 : : {
19570 : : Assert(tbinfo->ispartition);
19571 : :
19572 : : /*
19573 : : * Partition triggers only appear here because their 'tgenabled' flag
19574 : : * differs from its parent's. The trigger is created already, so
19575 : : * remove the CREATE and replace it with an ALTER. (Clear out the
19576 : : * DROP query too, so that pg_dump --create does not cause errors.)
19577 : : */
19578 : 115 : resetPQExpBuffer(query);
19579 : 115 : resetPQExpBuffer(delqry);
19580 : 115 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19581 [ - + ]: 115 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19582 : 115 : fmtQualifiedDumpable(tbinfo));
19583 [ + - + + : 115 : switch (tginfo->tgenabled)
- ]
19584 : : {
19585 : 40 : case 'f':
19586 : : case 'D':
19587 : 40 : appendPQExpBufferStr(query, "DISABLE");
19588 : 40 : break;
19589 : 0 : case 't':
19590 : : case 'O':
19591 : 0 : appendPQExpBufferStr(query, "ENABLE");
19592 : 0 : break;
19593 : 35 : case 'R':
19594 : 35 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19595 : 35 : break;
19596 : 40 : case 'A':
19597 : 40 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19598 : 40 : break;
19599 : : }
19600 : 115 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19601 : 115 : fmtId(tginfo->dobj.name));
19602 : : }
19603 [ + - - + ]: 387 : else if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
19604 : : {
19605 : 0 : appendPQExpBuffer(query, "\nALTER %sTABLE %s ",
19606 [ # # ]: 0 : tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "",
19607 : 0 : fmtQualifiedDumpable(tbinfo));
19608 [ # # # # ]: 0 : switch (tginfo->tgenabled)
19609 : : {
19610 : 0 : case 'D':
19611 : : case 'f':
19612 : 0 : appendPQExpBufferStr(query, "DISABLE");
19613 : 0 : break;
19614 : 0 : case 'A':
19615 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19616 : 0 : break;
19617 : 0 : case 'R':
19618 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19619 : 0 : break;
19620 : 0 : default:
19621 : 0 : appendPQExpBufferStr(query, "ENABLE");
19622 : 0 : break;
19623 : : }
19624 : 0 : appendPQExpBuffer(query, " TRIGGER %s;\n",
19625 : 0 : fmtId(tginfo->dobj.name));
19626 : : }
19627 : :
19628 : 502 : appendPQExpBuffer(trigprefix, "TRIGGER %s ON",
19629 : 502 : fmtId(tginfo->dobj.name));
19630 : :
19631 : 502 : tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name);
19632 : :
19633 [ + - ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19634 : 502 : ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
19635 : 502 : ARCHIVE_OPTS(.tag = tag,
19636 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19637 : : .owner = tbinfo->rolname,
19638 : : .description = "TRIGGER",
19639 : : .section = SECTION_POST_DATA,
19640 : : .createStmt = query->data,
19641 : : .dropStmt = delqry->data));
19642 : :
19643 [ - + ]: 502 : if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19644 : 0 : dumpComment(fout, trigprefix->data, qtabname,
19645 : 0 : tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
19646 : 0 : tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
19647 : :
19648 : 502 : pfree(tag);
19649 : 502 : destroyPQExpBuffer(query);
19650 : 502 : destroyPQExpBuffer(delqry);
19651 : 502 : destroyPQExpBuffer(trigprefix);
19652 : 502 : destroyPQExpBuffer(trigidentity);
19653 : 502 : pg_free(qtabname);
19654 : : }
19655 : :
19656 : : /*
19657 : : * dumpEventTrigger
19658 : : * write the declaration of one user-defined event trigger
19659 : : */
19660 : : static void
19661 : 44 : dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo)
19662 : : {
19663 : 44 : DumpOptions *dopt = fout->dopt;
19664 : : PQExpBuffer query;
19665 : : PQExpBuffer delqry;
19666 : : char *qevtname;
19667 : :
19668 : : /* Do nothing if not dumping schema */
19669 [ + + ]: 44 : if (!dopt->dumpSchema)
19670 : 6 : return;
19671 : :
19672 : 38 : query = createPQExpBuffer();
19673 : 38 : delqry = createPQExpBuffer();
19674 : :
19675 : 38 : qevtname = pg_strdup(fmtId(evtinfo->dobj.name));
19676 : :
19677 : 38 : appendPQExpBufferStr(query, "CREATE EVENT TRIGGER ");
19678 : 38 : appendPQExpBufferStr(query, qevtname);
19679 : 38 : appendPQExpBufferStr(query, " ON ");
19680 : 38 : appendPQExpBufferStr(query, fmtId(evtinfo->evtevent));
19681 : :
19682 [ + + ]: 38 : if (strcmp("", evtinfo->evttags) != 0)
19683 : : {
19684 : 5 : appendPQExpBufferStr(query, "\n WHEN TAG IN (");
19685 : 5 : appendPQExpBufferStr(query, evtinfo->evttags);
19686 : 5 : appendPQExpBufferChar(query, ')');
19687 : : }
19688 : :
19689 : 38 : appendPQExpBufferStr(query, "\n EXECUTE FUNCTION ");
19690 : 38 : appendPQExpBufferStr(query, evtinfo->evtfname);
19691 : 38 : appendPQExpBufferStr(query, "();\n");
19692 : :
19693 [ - + ]: 38 : if (evtinfo->evtenabled != 'O')
19694 : : {
19695 : 0 : appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ",
19696 : : qevtname);
19697 [ # # # # ]: 0 : switch (evtinfo->evtenabled)
19698 : : {
19699 : 0 : case 'D':
19700 : 0 : appendPQExpBufferStr(query, "DISABLE");
19701 : 0 : break;
19702 : 0 : case 'A':
19703 : 0 : appendPQExpBufferStr(query, "ENABLE ALWAYS");
19704 : 0 : break;
19705 : 0 : case 'R':
19706 : 0 : appendPQExpBufferStr(query, "ENABLE REPLICA");
19707 : 0 : break;
19708 : 0 : default:
19709 : 0 : appendPQExpBufferStr(query, "ENABLE");
19710 : 0 : break;
19711 : : }
19712 : 0 : appendPQExpBufferStr(query, ";\n");
19713 : : }
19714 : :
19715 : 38 : appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n",
19716 : : qevtname);
19717 : :
19718 [ + + ]: 38 : if (dopt->binary_upgrade)
19719 : 2 : binary_upgrade_extension_member(query, &evtinfo->dobj,
19720 : : "EVENT TRIGGER", qevtname, NULL);
19721 : :
19722 [ + - ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19723 : 38 : ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId,
19724 : 38 : ARCHIVE_OPTS(.tag = evtinfo->dobj.name,
19725 : : .owner = evtinfo->evtowner,
19726 : : .description = "EVENT TRIGGER",
19727 : : .section = SECTION_POST_DATA,
19728 : : .createStmt = query->data,
19729 : : .dropStmt = delqry->data));
19730 : :
19731 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19732 : 0 : dumpComment(fout, "EVENT TRIGGER", qevtname,
19733 : 0 : NULL, evtinfo->evtowner,
19734 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19735 : :
19736 [ - + ]: 38 : if (evtinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
19737 : 0 : dumpSecLabel(fout, "EVENT TRIGGER", qevtname,
19738 : 0 : NULL, evtinfo->evtowner,
19739 : 0 : evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
19740 : :
19741 : 38 : destroyPQExpBuffer(query);
19742 : 38 : destroyPQExpBuffer(delqry);
19743 : 38 : pg_free(qevtname);
19744 : : }
19745 : :
19746 : : /*
19747 : : * dumpRule
19748 : : * Dump a rule
19749 : : */
19750 : : static void
19751 : 1197 : dumpRule(Archive *fout, const RuleInfo *rinfo)
19752 : : {
19753 : 1197 : DumpOptions *dopt = fout->dopt;
19754 : 1197 : TableInfo *tbinfo = rinfo->ruletable;
19755 : : bool is_view;
19756 : : PQExpBuffer query;
19757 : : PQExpBuffer cmd;
19758 : : PQExpBuffer delcmd;
19759 : : PQExpBuffer ruleprefix;
19760 : : char *qtabname;
19761 : : PGresult *res;
19762 : : char *tag;
19763 : :
19764 : : /* Do nothing if not dumping schema */
19765 [ + + ]: 1197 : if (!dopt->dumpSchema)
19766 : 70 : return;
19767 : :
19768 : : /*
19769 : : * If it is an ON SELECT rule that is created implicitly by CREATE VIEW,
19770 : : * we do not want to dump it as a separate object.
19771 : : */
19772 [ + + ]: 1127 : if (!rinfo->separate)
19773 : 916 : return;
19774 : :
19775 : : /*
19776 : : * If it's an ON SELECT rule, we want to print it as a view definition,
19777 : : * instead of a rule.
19778 : : */
19779 [ + + + - ]: 211 : is_view = (rinfo->ev_type == '1' && rinfo->is_instead);
19780 : :
19781 : 211 : query = createPQExpBuffer();
19782 : 211 : cmd = createPQExpBuffer();
19783 : 211 : delcmd = createPQExpBuffer();
19784 : 211 : ruleprefix = createPQExpBuffer();
19785 : :
19786 : 211 : qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
19787 : :
19788 [ + + ]: 211 : if (is_view)
19789 : : {
19790 : : PQExpBuffer result;
19791 : :
19792 : : /*
19793 : : * We need OR REPLACE here because we'll be replacing a dummy view.
19794 : : * Otherwise this should look largely like the regular view dump code.
19795 : : */
19796 : 10 : appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s",
19797 : 10 : fmtQualifiedDumpable(tbinfo));
19798 [ - + ]: 10 : if (nonemptyReloptions(tbinfo->reloptions))
19799 : : {
19800 : 0 : appendPQExpBufferStr(cmd, " WITH (");
19801 : 0 : appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout);
19802 : 0 : appendPQExpBufferChar(cmd, ')');
19803 : : }
19804 : 10 : result = createViewAsClause(fout, tbinfo);
19805 : 10 : appendPQExpBuffer(cmd, " AS\n%s", result->data);
19806 : 10 : destroyPQExpBuffer(result);
19807 [ - + ]: 10 : if (tbinfo->checkoption != NULL)
19808 : 0 : appendPQExpBuffer(cmd, "\n WITH %s CHECK OPTION",
19809 : : tbinfo->checkoption);
19810 : 10 : appendPQExpBufferStr(cmd, ";\n");
19811 : : }
19812 : : else
19813 : : {
19814 : : /* In the rule case, just print pg_get_ruledef's result verbatim */
19815 : 201 : appendPQExpBuffer(query,
19816 : : "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)",
19817 : 201 : rinfo->dobj.catId.oid);
19818 : :
19819 : 201 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19820 : :
19821 [ - + ]: 201 : if (PQntuples(res) != 1)
19822 : 0 : pg_fatal("query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned",
19823 : : rinfo->dobj.name, tbinfo->dobj.name);
19824 : :
19825 : 201 : printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
19826 : :
19827 : 201 : PQclear(res);
19828 : : }
19829 : :
19830 : : /*
19831 : : * Add the command to alter the rules replication firing semantics if it
19832 : : * differs from the default.
19833 : : */
19834 [ + + ]: 211 : if (rinfo->ev_enabled != 'O')
19835 : : {
19836 : 15 : appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtQualifiedDumpable(tbinfo));
19837 [ - - + - ]: 15 : switch (rinfo->ev_enabled)
19838 : : {
19839 : 0 : case 'A':
19840 : 0 : appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n",
19841 : 0 : fmtId(rinfo->dobj.name));
19842 : 0 : break;
19843 : 0 : case 'R':
19844 : 0 : appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n",
19845 : 0 : fmtId(rinfo->dobj.name));
19846 : 0 : break;
19847 : 15 : case 'D':
19848 : 15 : appendPQExpBuffer(cmd, "DISABLE RULE %s;\n",
19849 : 15 : fmtId(rinfo->dobj.name));
19850 : 15 : break;
19851 : : }
19852 : : }
19853 : :
19854 [ + + ]: 211 : if (is_view)
19855 : : {
19856 : : /*
19857 : : * We can't DROP a view's ON SELECT rule. Instead, use CREATE OR
19858 : : * REPLACE VIEW to replace the rule with something with minimal
19859 : : * dependencies.
19860 : : */
19861 : : PQExpBuffer result;
19862 : :
19863 : 10 : appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s",
19864 : 10 : fmtQualifiedDumpable(tbinfo));
19865 : 10 : result = createDummyViewAsClause(fout, tbinfo);
19866 : 10 : appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data);
19867 : 10 : destroyPQExpBuffer(result);
19868 : : }
19869 : : else
19870 : : {
19871 : 201 : appendPQExpBuffer(delcmd, "DROP RULE %s ",
19872 : 201 : fmtId(rinfo->dobj.name));
19873 : 201 : appendPQExpBuffer(delcmd, "ON %s;\n",
19874 : 201 : fmtQualifiedDumpable(tbinfo));
19875 : : }
19876 : :
19877 : 211 : appendPQExpBuffer(ruleprefix, "RULE %s ON",
19878 : 211 : fmtId(rinfo->dobj.name));
19879 : :
19880 : 211 : tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name);
19881 : :
19882 [ + - ]: 211 : if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
19883 : 211 : ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
19884 : 211 : ARCHIVE_OPTS(.tag = tag,
19885 : : .namespace = tbinfo->dobj.namespace->dobj.name,
19886 : : .owner = tbinfo->rolname,
19887 : : .description = "RULE",
19888 : : .section = SECTION_POST_DATA,
19889 : : .createStmt = cmd->data,
19890 : : .dropStmt = delcmd->data));
19891 : :
19892 : : /* Dump rule comments */
19893 [ - + ]: 211 : if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
19894 : 0 : dumpComment(fout, ruleprefix->data, qtabname,
19895 : 0 : tbinfo->dobj.namespace->dobj.name,
19896 : : tbinfo->rolname,
19897 : 0 : rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
19898 : :
19899 : 211 : pfree(tag);
19900 : 211 : destroyPQExpBuffer(query);
19901 : 211 : destroyPQExpBuffer(cmd);
19902 : 211 : destroyPQExpBuffer(delcmd);
19903 : 211 : destroyPQExpBuffer(ruleprefix);
19904 : 211 : pg_free(qtabname);
19905 : : }
19906 : :
19907 : : /*
19908 : : * getExtensionMembership --- obtain extension membership data
19909 : : *
19910 : : * We need to identify objects that are extension members as soon as they're
19911 : : * loaded, so that we can correctly determine whether they need to be dumped.
19912 : : * Generally speaking, extension member objects will get marked as *not* to
19913 : : * be dumped, as they will be recreated by the single CREATE EXTENSION
19914 : : * command. However, in binary upgrade mode we still need to dump the members
19915 : : * individually.
19916 : : */
19917 : : void
19918 : 194 : getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
19919 : : int numExtensions)
19920 : : {
19921 : : PQExpBuffer query;
19922 : : PGresult *res;
19923 : : int ntups,
19924 : : i;
19925 : : int i_classid,
19926 : : i_objid,
19927 : : i_refobjid;
19928 : : ExtensionInfo *ext;
19929 : :
19930 : : /* Nothing to do if no extensions */
19931 [ - + ]: 194 : if (numExtensions == 0)
19932 : 0 : return;
19933 : :
19934 : 194 : query = createPQExpBuffer();
19935 : :
19936 : : /* refclassid constraint is redundant but may speed the search */
19937 : 194 : appendPQExpBufferStr(query, "SELECT "
19938 : : "classid, objid, refobjid "
19939 : : "FROM pg_depend "
19940 : : "WHERE refclassid = 'pg_extension'::regclass "
19941 : : "AND deptype = 'e' "
19942 : : "ORDER BY 3");
19943 : :
19944 : 194 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
19945 : :
19946 : 194 : ntups = PQntuples(res);
19947 : :
19948 : 194 : i_classid = PQfnumber(res, "classid");
19949 : 194 : i_objid = PQfnumber(res, "objid");
19950 : 194 : i_refobjid = PQfnumber(res, "refobjid");
19951 : :
19952 : : /*
19953 : : * Since we ordered the SELECT by referenced ID, we can expect that
19954 : : * multiple entries for the same extension will appear together; this
19955 : : * saves on searches.
19956 : : */
19957 : 194 : ext = NULL;
19958 : :
19959 [ + + ]: 1576 : for (i = 0; i < ntups; i++)
19960 : : {
19961 : : CatalogId objId;
19962 : : Oid extId;
19963 : :
19964 : 1382 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
19965 : 1382 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
19966 : 1382 : extId = atooid(PQgetvalue(res, i, i_refobjid));
19967 : :
19968 [ + + ]: 1382 : if (ext == NULL ||
19969 [ + + ]: 1188 : ext->dobj.catId.oid != extId)
19970 : 225 : ext = findExtensionByOid(extId);
19971 : :
19972 [ - + ]: 1382 : if (ext == NULL)
19973 : : {
19974 : : /* shouldn't happen */
19975 : 0 : pg_log_warning("could not find referenced extension %u", extId);
19976 : 0 : continue;
19977 : : }
19978 : :
19979 : 1382 : recordExtensionMembership(objId, ext);
19980 : : }
19981 : :
19982 : 194 : PQclear(res);
19983 : :
19984 : 194 : destroyPQExpBuffer(query);
19985 : : }
19986 : :
19987 : : /*
19988 : : * processExtensionTables --- deal with extension configuration tables
19989 : : *
19990 : : * There are two parts to this process:
19991 : : *
19992 : : * 1. Identify and create dump records for extension configuration tables.
19993 : : *
19994 : : * Extensions can mark tables as "configuration", which means that the user
19995 : : * is able and expected to modify those tables after the extension has been
19996 : : * loaded. For these tables, we dump out only the data- the structure is
19997 : : * expected to be handled at CREATE EXTENSION time, including any indexes or
19998 : : * foreign keys, which brings us to-
19999 : : *
20000 : : * 2. Record FK dependencies between configuration tables.
20001 : : *
20002 : : * Due to the FKs being created at CREATE EXTENSION time and therefore before
20003 : : * the data is loaded, we have to work out what the best order for reloading
20004 : : * the data is, to avoid FK violations when the tables are restored. This is
20005 : : * not perfect- we can't handle circular dependencies and if any exist they
20006 : : * will cause an invalid dump to be produced (though at least all of the data
20007 : : * is included for a user to manually restore). This is currently documented
20008 : : * but perhaps we can provide a better solution in the future.
20009 : : */
20010 : : void
20011 : 193 : processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
20012 : : int numExtensions)
20013 : : {
20014 : 193 : DumpOptions *dopt = fout->dopt;
20015 : : PQExpBuffer query;
20016 : : PGresult *res;
20017 : : int ntups,
20018 : : i;
20019 : : int i_conrelid,
20020 : : i_confrelid;
20021 : :
20022 : : /* Nothing to do if no extensions */
20023 [ - + ]: 193 : if (numExtensions == 0)
20024 : 0 : return;
20025 : :
20026 : : /*
20027 : : * Identify extension configuration tables and create TableDataInfo
20028 : : * objects for them, ensuring their data will be dumped even though the
20029 : : * tables themselves won't be.
20030 : : *
20031 : : * Note that we create TableDataInfo objects even in schema-only mode, ie,
20032 : : * user data in a configuration table is treated like schema data. This
20033 : : * seems appropriate since system data in a config table would get
20034 : : * reloaded by CREATE EXTENSION. If the extension is not listed in the
20035 : : * list of extensions to be included, none of its data is dumped.
20036 : : */
20037 [ + + ]: 417 : for (i = 0; i < numExtensions; i++)
20038 : : {
20039 : 224 : ExtensionInfo *curext = &(extinfo[i]);
20040 : 224 : char *extconfig = curext->extconfig;
20041 : 224 : char *extcondition = curext->extcondition;
20042 : 224 : char **extconfigarray = NULL;
20043 : 224 : char **extconditionarray = NULL;
20044 : 224 : int nconfigitems = 0;
20045 : 224 : int nconditionitems = 0;
20046 : :
20047 : : /*
20048 : : * Check if this extension is listed as to include in the dump. If
20049 : : * not, any table data associated with it is discarded.
20050 : : */
20051 [ + + ]: 224 : if (extension_include_oids.head != NULL &&
20052 [ + + ]: 8 : !simple_oid_list_member(&extension_include_oids,
20053 : : curext->dobj.catId.oid))
20054 : 6 : continue;
20055 : :
20056 : : /*
20057 : : * Check if this extension is listed as to exclude in the dump. If
20058 : : * yes, any table data associated with it is discarded.
20059 : : */
20060 [ + + + + ]: 224 : if (extension_exclude_oids.head != NULL &&
20061 : 4 : simple_oid_list_member(&extension_exclude_oids,
20062 : : curext->dobj.catId.oid))
20063 : 2 : continue;
20064 : :
20065 [ + + - + ]: 218 : if (strlen(extconfig) != 0 || strlen(extcondition) != 0)
20066 : : {
20067 : : int j;
20068 : :
20069 [ - + ]: 20 : if (!parsePGArray(extconfig, &extconfigarray, &nconfigitems))
20070 : 0 : pg_fatal("could not parse %s array", "extconfig");
20071 [ - + ]: 20 : if (!parsePGArray(extcondition, &extconditionarray, &nconditionitems))
20072 : 0 : pg_fatal("could not parse %s array", "extcondition");
20073 [ - + ]: 20 : if (nconfigitems != nconditionitems)
20074 : 0 : pg_fatal("mismatched number of configurations and conditions for extension");
20075 : :
20076 [ + + ]: 60 : for (j = 0; j < nconfigitems; j++)
20077 : : {
20078 : : TableInfo *configtbl;
20079 : 40 : Oid configtbloid = atooid(extconfigarray[j]);
20080 : 40 : bool dumpobj =
20081 : 40 : curext->dobj.dump & DUMP_COMPONENT_DEFINITION;
20082 : :
20083 : 40 : configtbl = findTableByOid(configtbloid);
20084 [ - + ]: 40 : if (configtbl == NULL)
20085 : 0 : continue;
20086 : :
20087 : : /*
20088 : : * Tables of not-to-be-dumped extensions shouldn't be dumped
20089 : : * unless the table or its schema is explicitly included
20090 : : */
20091 [ + + ]: 40 : if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION))
20092 : : {
20093 : : /* check table explicitly requested */
20094 [ - + - - ]: 2 : if (table_include_oids.head != NULL &&
20095 : 0 : simple_oid_list_member(&table_include_oids,
20096 : : configtbloid))
20097 : 0 : dumpobj = true;
20098 : :
20099 : : /* check table's schema explicitly requested */
20100 [ + - ]: 2 : if (configtbl->dobj.namespace->dobj.dump &
20101 : : DUMP_COMPONENT_DATA)
20102 : 2 : dumpobj = true;
20103 : : }
20104 : :
20105 : : /* check table excluded by an exclusion switch */
20106 [ + + + + ]: 44 : if (table_exclude_oids.head != NULL &&
20107 : 4 : simple_oid_list_member(&table_exclude_oids,
20108 : : configtbloid))
20109 : 1 : dumpobj = false;
20110 : :
20111 : : /* check schema excluded by an exclusion switch */
20112 [ - + ]: 40 : if (simple_oid_list_member(&schema_exclude_oids,
20113 : 40 : configtbl->dobj.namespace->dobj.catId.oid))
20114 : 0 : dumpobj = false;
20115 : :
20116 [ + + ]: 40 : if (dumpobj)
20117 : : {
20118 : 39 : makeTableDataInfo(dopt, configtbl);
20119 [ + - ]: 39 : if (configtbl->dataObj != NULL)
20120 : : {
20121 [ - + ]: 39 : if (strlen(extconditionarray[j]) > 0)
20122 : 0 : configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
20123 : : }
20124 : : }
20125 : : }
20126 : : }
20127 [ + + ]: 218 : if (extconfigarray)
20128 : 20 : free(extconfigarray);
20129 [ + + ]: 218 : if (extconditionarray)
20130 : 20 : free(extconditionarray);
20131 : : }
20132 : :
20133 : : /*
20134 : : * Now that all the TableDataInfo objects have been created for all the
20135 : : * extensions, check their FK dependencies and register them to try and
20136 : : * dump the data out in an order that they can be restored in.
20137 : : *
20138 : : * Note that this is not a problem for user tables as their FKs are
20139 : : * recreated after the data has been loaded.
20140 : : */
20141 : :
20142 : 193 : query = createPQExpBuffer();
20143 : :
20144 : 193 : printfPQExpBuffer(query,
20145 : : "SELECT conrelid, confrelid "
20146 : : "FROM pg_constraint "
20147 : : "JOIN pg_depend ON (objid = confrelid) "
20148 : : "WHERE contype = 'f' "
20149 : : "AND refclassid = 'pg_extension'::regclass "
20150 : : "AND classid = 'pg_class'::regclass;");
20151 : :
20152 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20153 : 193 : ntups = PQntuples(res);
20154 : :
20155 : 193 : i_conrelid = PQfnumber(res, "conrelid");
20156 : 193 : i_confrelid = PQfnumber(res, "confrelid");
20157 : :
20158 : : /* Now get the dependencies and register them */
20159 [ - + ]: 193 : for (i = 0; i < ntups; i++)
20160 : : {
20161 : : Oid conrelid,
20162 : : confrelid;
20163 : : TableInfo *reftable,
20164 : : *contable;
20165 : :
20166 : 0 : conrelid = atooid(PQgetvalue(res, i, i_conrelid));
20167 : 0 : confrelid = atooid(PQgetvalue(res, i, i_confrelid));
20168 : 0 : contable = findTableByOid(conrelid);
20169 : 0 : reftable = findTableByOid(confrelid);
20170 : :
20171 [ # # ]: 0 : if (reftable == NULL ||
20172 [ # # # # ]: 0 : reftable->dataObj == NULL ||
20173 : 0 : contable == NULL ||
20174 [ # # ]: 0 : contable->dataObj == NULL)
20175 : 0 : continue;
20176 : :
20177 : : /*
20178 : : * Make referencing TABLE_DATA object depend on the referenced table's
20179 : : * TABLE_DATA object.
20180 : : */
20181 : 0 : addObjectDependency(&contable->dataObj->dobj,
20182 : 0 : reftable->dataObj->dobj.dumpId);
20183 : : }
20184 : 193 : PQclear(res);
20185 : 193 : destroyPQExpBuffer(query);
20186 : : }
20187 : :
20188 : : /*
20189 : : * getDependencies --- obtain available dependency data
20190 : : */
20191 : : static void
20192 : 193 : getDependencies(Archive *fout)
20193 : : {
20194 : : PQExpBuffer query;
20195 : : PGresult *res;
20196 : : int ntups,
20197 : : i;
20198 : : int i_classid,
20199 : : i_objid,
20200 : : i_refclassid,
20201 : : i_refobjid,
20202 : : i_deptype;
20203 : : DumpableObject *dobj,
20204 : : *refdobj;
20205 : :
20206 : 193 : pg_log_info("reading dependency data");
20207 : :
20208 : 193 : query = createPQExpBuffer();
20209 : :
20210 : : /*
20211 : : * Messy query to collect the dependency data we need. Note that we
20212 : : * ignore the sub-object column, so that dependencies of or on a column
20213 : : * look the same as dependencies of or on a whole table.
20214 : : *
20215 : : * PIN dependencies aren't interesting, and EXTENSION dependencies were
20216 : : * already processed by getExtensionMembership.
20217 : : */
20218 : 193 : appendPQExpBufferStr(query, "SELECT "
20219 : : "classid, objid, refclassid, refobjid, deptype "
20220 : : "FROM pg_depend "
20221 : : "WHERE deptype != 'p' AND deptype != 'e'\n");
20222 : :
20223 : : /*
20224 : : * Since we don't treat pg_amop entries as separate DumpableObjects, we
20225 : : * have to translate their dependencies into dependencies of their parent
20226 : : * opfamily. Ignore internal dependencies though, as those will point to
20227 : : * their parent opclass, which we needn't consider here (and if we did,
20228 : : * it'd just result in circular dependencies). Also, "loose" opfamily
20229 : : * entries will have dependencies on their parent opfamily, which we
20230 : : * should drop since they'd likewise become useless self-dependencies.
20231 : : * (But be sure to keep deps on *other* opfamilies; see amopsortfamily.)
20232 : : */
20233 : 193 : appendPQExpBufferStr(query, "UNION ALL\n"
20234 : : "SELECT 'pg_opfamily'::regclass AS classid, amopfamily AS objid, refclassid, refobjid, deptype "
20235 : : "FROM pg_depend d, pg_amop o "
20236 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20237 : : "classid = 'pg_amop'::regclass AND objid = o.oid "
20238 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amopfamily = refobjid)\n");
20239 : :
20240 : : /* Likewise for pg_amproc entries */
20241 : 193 : appendPQExpBufferStr(query, "UNION ALL\n"
20242 : : "SELECT 'pg_opfamily'::regclass AS classid, amprocfamily AS objid, refclassid, refobjid, deptype "
20243 : : "FROM pg_depend d, pg_amproc p "
20244 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20245 : : "classid = 'pg_amproc'::regclass AND objid = p.oid "
20246 : : "AND NOT (refclassid = 'pg_opfamily'::regclass AND amprocfamily = refobjid)\n");
20247 : :
20248 : : /*
20249 : : * Translate dependencies of pg_propgraph_element entries into
20250 : : * dependencies of their parent pg_class entry.
20251 : : */
20252 [ + - ]: 193 : if (fout->remoteVersion >= 190000)
20253 : 193 : appendPQExpBufferStr(query, "UNION ALL\n"
20254 : : "SELECT 'pg_class'::regclass AS classid, pgepgid AS objid, refclassid, refobjid, deptype "
20255 : : "FROM pg_depend d, pg_propgraph_element pge "
20256 : : "WHERE deptype NOT IN ('p', 'e', 'i') AND "
20257 : : "classid = 'pg_propgraph_element'::regclass AND objid = pge.oid\n");
20258 : :
20259 : : /* Sort the output for efficiency below */
20260 : 193 : appendPQExpBufferStr(query, "ORDER BY 1,2");
20261 : :
20262 : 193 : res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
20263 : :
20264 : 193 : ntups = PQntuples(res);
20265 : :
20266 : 193 : i_classid = PQfnumber(res, "classid");
20267 : 193 : i_objid = PQfnumber(res, "objid");
20268 : 193 : i_refclassid = PQfnumber(res, "refclassid");
20269 : 193 : i_refobjid = PQfnumber(res, "refobjid");
20270 : 193 : i_deptype = PQfnumber(res, "deptype");
20271 : :
20272 : : /*
20273 : : * Since we ordered the SELECT by referencing ID, we can expect that
20274 : : * multiple entries for the same object will appear together; this saves
20275 : : * on searches.
20276 : : */
20277 : 193 : dobj = NULL;
20278 : :
20279 [ + + ]: 466118 : for (i = 0; i < ntups; i++)
20280 : : {
20281 : : CatalogId objId;
20282 : : CatalogId refobjId;
20283 : : char deptype;
20284 : :
20285 : 465925 : objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
20286 : 465925 : objId.oid = atooid(PQgetvalue(res, i, i_objid));
20287 : 465925 : refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
20288 : 465925 : refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
20289 : 465925 : deptype = *(PQgetvalue(res, i, i_deptype));
20290 : :
20291 [ + + ]: 465925 : if (dobj == NULL ||
20292 [ + + ]: 430666 : dobj->catId.tableoid != objId.tableoid ||
20293 [ + + ]: 428547 : dobj->catId.oid != objId.oid)
20294 : 205920 : dobj = findObjectByCatalogId(objId);
20295 : :
20296 : : /*
20297 : : * Failure to find objects mentioned in pg_depend is not unexpected,
20298 : : * since for example we don't collect info about TOAST tables.
20299 : : */
20300 [ + + ]: 465925 : if (dobj == NULL)
20301 : : {
20302 : : #ifdef NOT_USED
20303 : : pg_log_warning("no referencing object %u %u",
20304 : : objId.tableoid, objId.oid);
20305 : : #endif
20306 : 36406 : continue;
20307 : : }
20308 : :
20309 : 430852 : refdobj = findObjectByCatalogId(refobjId);
20310 : :
20311 [ + + ]: 430852 : if (refdobj == NULL)
20312 : : {
20313 : : #ifdef NOT_USED
20314 : : pg_log_warning("no referenced object %u %u",
20315 : : refobjId.tableoid, refobjId.oid);
20316 : : #endif
20317 : 1333 : continue;
20318 : : }
20319 : :
20320 : : /*
20321 : : * For 'x' dependencies, mark the object for later; we still add the
20322 : : * normal dependency, for possible ordering purposes. Currently
20323 : : * pg_dump_sort.c knows to put extensions ahead of all object types
20324 : : * that could possibly depend on them, but this is safer.
20325 : : */
20326 [ + + ]: 429519 : if (deptype == 'x')
20327 : 44 : dobj->depends_on_ext = true;
20328 : :
20329 : : /*
20330 : : * Ordinarily, table rowtypes have implicit dependencies on their
20331 : : * tables. However, for a composite type the implicit dependency goes
20332 : : * the other way in pg_depend; which is the right thing for DROP but
20333 : : * it doesn't produce the dependency ordering we need. So in that one
20334 : : * case, we reverse the direction of the dependency.
20335 : : */
20336 [ + + ]: 429519 : if (deptype == 'i' &&
20337 [ + + ]: 120204 : dobj->objType == DO_TABLE &&
20338 [ + + ]: 1311 : refdobj->objType == DO_TYPE)
20339 : 185 : addObjectDependency(refdobj, dobj->dumpId);
20340 : : else
20341 : : /* normal case */
20342 : 429334 : addObjectDependency(dobj, refdobj->dumpId);
20343 : : }
20344 : :
20345 : 193 : PQclear(res);
20346 : :
20347 : 193 : destroyPQExpBuffer(query);
20348 : 193 : }
20349 : :
20350 : :
20351 : : /*
20352 : : * createBoundaryObjects - create dummy DumpableObjects to represent
20353 : : * dump section boundaries.
20354 : : */
20355 : : static DumpableObject *
20356 : 193 : createBoundaryObjects(void)
20357 : : {
20358 : : DumpableObject *dobjs;
20359 : :
20360 : 193 : dobjs = pg_malloc_array(DumpableObject, 2);
20361 : :
20362 : 193 : dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
20363 : 193 : dobjs[0].catId = nilCatalogId;
20364 : 193 : AssignDumpId(dobjs + 0);
20365 : 193 : dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
20366 : :
20367 : 193 : dobjs[1].objType = DO_POST_DATA_BOUNDARY;
20368 : 193 : dobjs[1].catId = nilCatalogId;
20369 : 193 : AssignDumpId(dobjs + 1);
20370 : 193 : dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
20371 : :
20372 : 193 : return dobjs;
20373 : : }
20374 : :
20375 : : /*
20376 : : * addBoundaryDependencies - add dependencies as needed to enforce the dump
20377 : : * section boundaries.
20378 : : */
20379 : : static void
20380 : 193 : addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
20381 : : DumpableObject *boundaryObjs)
20382 : : {
20383 : 193 : DumpableObject *preDataBound = boundaryObjs + 0;
20384 : 193 : DumpableObject *postDataBound = boundaryObjs + 1;
20385 : : int i;
20386 : :
20387 [ + + ]: 745710 : for (i = 0; i < numObjs; i++)
20388 : : {
20389 : 745517 : DumpableObject *dobj = dobjs[i];
20390 : :
20391 : : /*
20392 : : * The classification of object types here must match the SECTION_xxx
20393 : : * values assigned during subsequent ArchiveEntry calls!
20394 : : */
20395 [ + + + + : 745517 : switch (dobj->objType)
+ + + +
- ]
20396 : : {
20397 : 693986 : case DO_NAMESPACE:
20398 : : case DO_EXTENSION:
20399 : : case DO_TYPE:
20400 : : case DO_SHELL_TYPE:
20401 : : case DO_FUNC:
20402 : : case DO_AGG:
20403 : : case DO_OPERATOR:
20404 : : case DO_ACCESS_METHOD:
20405 : : case DO_OPCLASS:
20406 : : case DO_OPFAMILY:
20407 : : case DO_COLLATION:
20408 : : case DO_CONVERSION:
20409 : : case DO_TABLE:
20410 : : case DO_TABLE_ATTACH:
20411 : : case DO_ATTRDEF:
20412 : : case DO_PROCLANG:
20413 : : case DO_CAST:
20414 : : case DO_DUMMY_TYPE:
20415 : : case DO_TSPARSER:
20416 : : case DO_TSDICT:
20417 : : case DO_TSTEMPLATE:
20418 : : case DO_TSCONFIG:
20419 : : case DO_FDW:
20420 : : case DO_FOREIGN_SERVER:
20421 : : case DO_TRANSFORM:
20422 : : /* Pre-data objects: must come before the pre-data boundary */
20423 : 693986 : addObjectDependency(preDataBound, dobj->dumpId);
20424 : 693986 : break;
20425 : 5186 : case DO_TABLE_DATA:
20426 : : case DO_SEQUENCE_SET:
20427 : : case DO_LARGE_OBJECT:
20428 : : case DO_LARGE_OBJECT_DATA:
20429 : : /* Data objects: must come between the boundaries */
20430 : 5186 : addObjectDependency(dobj, preDataBound->dumpId);
20431 : 5186 : addObjectDependency(postDataBound, dobj->dumpId);
20432 : 5186 : break;
20433 : 6370 : case DO_INDEX:
20434 : : case DO_INDEX_ATTACH:
20435 : : case DO_STATSEXT:
20436 : : case DO_REFRESH_MATVIEW:
20437 : : case DO_TRIGGER:
20438 : : case DO_EVENT_TRIGGER:
20439 : : case DO_DEFAULT_ACL:
20440 : : case DO_POLICY:
20441 : : case DO_PUBLICATION:
20442 : : case DO_PUBLICATION_REL:
20443 : : case DO_PUBLICATION_TABLE_IN_SCHEMA:
20444 : : case DO_SUBSCRIPTION:
20445 : : case DO_SUBSCRIPTION_REL:
20446 : : /* Post-data objects: must come after the post-data boundary */
20447 : 6370 : addObjectDependency(dobj, postDataBound->dumpId);
20448 : 6370 : break;
20449 : 33046 : case DO_RULE:
20450 : : /* Rules are post-data, but only if dumped separately */
20451 [ + + ]: 33046 : if (((RuleInfo *) dobj)->separate)
20452 : 659 : addObjectDependency(dobj, postDataBound->dumpId);
20453 : 33046 : break;
20454 : 2854 : case DO_CONSTRAINT:
20455 : : case DO_FK_CONSTRAINT:
20456 : : /* Constraints are post-data, but only if dumped separately */
20457 [ + + ]: 2854 : if (((ConstraintInfo *) dobj)->separate)
20458 : 2100 : addObjectDependency(dobj, postDataBound->dumpId);
20459 : 2854 : break;
20460 : 193 : case DO_PRE_DATA_BOUNDARY:
20461 : : /* nothing to do */
20462 : 193 : break;
20463 : 193 : case DO_POST_DATA_BOUNDARY:
20464 : : /* must come after the pre-data boundary */
20465 : 193 : addObjectDependency(dobj, preDataBound->dumpId);
20466 : 193 : break;
20467 : 3689 : case DO_REL_STATS:
20468 : : /* stats section varies by parent object type, DATA or POST */
20469 [ + + ]: 3689 : if (((RelStatsInfo *) dobj)->section == SECTION_DATA)
20470 : : {
20471 : 2408 : addObjectDependency(dobj, preDataBound->dumpId);
20472 : 2408 : addObjectDependency(postDataBound, dobj->dumpId);
20473 : : }
20474 : : else
20475 : 1281 : addObjectDependency(dobj, postDataBound->dumpId);
20476 : 3689 : break;
20477 : : }
20478 : : }
20479 : 193 : }
20480 : :
20481 : :
20482 : : /*
20483 : : * BuildArchiveDependencies - create dependency data for archive TOC entries
20484 : : *
20485 : : * The raw dependency data obtained by getDependencies() is not terribly
20486 : : * useful in an archive dump, because in many cases there are dependency
20487 : : * chains linking through objects that don't appear explicitly in the dump.
20488 : : * For example, a view will depend on its _RETURN rule while the _RETURN rule
20489 : : * will depend on other objects --- but the rule will not appear as a separate
20490 : : * object in the dump. We need to adjust the view's dependencies to include
20491 : : * whatever the rule depends on that is included in the dump.
20492 : : *
20493 : : * Just to make things more complicated, there are also "special" dependencies
20494 : : * such as the dependency of a TABLE DATA item on its TABLE, which we must
20495 : : * not rearrange because pg_restore knows that TABLE DATA only depends on
20496 : : * its table. In these cases we must leave the dependencies strictly as-is
20497 : : * even if they refer to not-to-be-dumped objects.
20498 : : *
20499 : : * To handle this, the convention is that "special" dependencies are created
20500 : : * during ArchiveEntry calls, and an archive TOC item that has any such
20501 : : * entries will not be touched here. Otherwise, we recursively search the
20502 : : * DumpableObject data structures to build the correct dependencies for each
20503 : : * archive TOC item.
20504 : : */
20505 : : static void
20506 : 65 : BuildArchiveDependencies(Archive *fout)
20507 : : {
20508 : 65 : ArchiveHandle *AH = (ArchiveHandle *) fout;
20509 : : TocEntry *te;
20510 : :
20511 : : /* Scan all TOC entries in the archive */
20512 [ + + ]: 7903 : for (te = AH->toc->next; te != AH->toc; te = te->next)
20513 : : {
20514 : : DumpableObject *dobj;
20515 : : DumpId *dependencies;
20516 : : int nDeps;
20517 : : int allocDeps;
20518 : :
20519 : : /* No need to process entries that will not be dumped */
20520 [ + + ]: 7838 : if (te->reqs == 0)
20521 : 3901 : continue;
20522 : : /* Ignore entries that already have "special" dependencies */
20523 [ + + ]: 7830 : if (te->nDeps > 0)
20524 : 3387 : continue;
20525 : : /* Otherwise, look up the item's original DumpableObject, if any */
20526 : 4443 : dobj = findObjectByDumpId(te->dumpId);
20527 [ + + ]: 4443 : if (dobj == NULL)
20528 : 392 : continue;
20529 : : /* No work if it has no dependencies */
20530 [ + + ]: 4051 : if (dobj->nDeps <= 0)
20531 : 114 : continue;
20532 : : /* Set up work array */
20533 : 3937 : allocDeps = 64;
20534 : 3937 : dependencies = pg_malloc_array(DumpId, allocDeps);
20535 : 3937 : nDeps = 0;
20536 : : /* Recursively find all dumpable dependencies */
20537 : 3937 : findDumpableDependencies(AH, dobj,
20538 : : &dependencies, &nDeps, &allocDeps);
20539 : : /* And save 'em ... */
20540 [ + + ]: 3937 : if (nDeps > 0)
20541 : : {
20542 : 3000 : dependencies = pg_realloc_array(dependencies, DumpId, nDeps);
20543 : 3000 : te->dependencies = dependencies;
20544 : 3000 : te->nDeps = nDeps;
20545 : : }
20546 : : else
20547 : 937 : pg_free(dependencies);
20548 : : }
20549 : 65 : }
20550 : :
20551 : : /* Recursive search subroutine for BuildArchiveDependencies */
20552 : : static void
20553 : 9372 : findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj,
20554 : : DumpId **dependencies, int *nDeps, int *allocDeps)
20555 : : {
20556 : : int i;
20557 : :
20558 : : /*
20559 : : * Ignore section boundary objects: if we search through them, we'll
20560 : : * report lots of bogus dependencies.
20561 : : */
20562 [ + + ]: 9372 : if (dobj->objType == DO_PRE_DATA_BOUNDARY ||
20563 [ + + ]: 9351 : dobj->objType == DO_POST_DATA_BOUNDARY)
20564 : 1684 : return;
20565 : :
20566 [ + + ]: 19545 : for (i = 0; i < dobj->nDeps; i++)
20567 : : {
20568 : 11857 : DumpId depid = dobj->dependencies[i];
20569 : :
20570 [ + + ]: 11857 : if (TocIDRequired(AH, depid) != 0)
20571 : : {
20572 : : /* Object will be dumped, so just reference it as a dependency */
20573 [ - + ]: 6422 : if (*nDeps >= *allocDeps)
20574 : : {
20575 : 0 : *allocDeps *= 2;
20576 : 0 : *dependencies = pg_realloc_array(*dependencies, DumpId, *allocDeps);
20577 : : }
20578 : 6422 : (*dependencies)[*nDeps] = depid;
20579 : 6422 : (*nDeps)++;
20580 : : }
20581 : : else
20582 : : {
20583 : : /*
20584 : : * Object will not be dumped, so recursively consider its deps. We
20585 : : * rely on the assumption that sortDumpableObjects already broke
20586 : : * any dependency loops, else we might recurse infinitely.
20587 : : */
20588 : 5435 : DumpableObject *otherdobj = findObjectByDumpId(depid);
20589 : :
20590 [ + - ]: 5435 : if (otherdobj)
20591 : 5435 : findDumpableDependencies(AH, otherdobj,
20592 : : dependencies, nDeps, allocDeps);
20593 : : }
20594 : : }
20595 : : }
20596 : :
20597 : :
20598 : : /*
20599 : : * getFormattedTypeName - retrieve a nicely-formatted type name for the
20600 : : * given type OID.
20601 : : *
20602 : : * This does not guarantee to schema-qualify the output, so it should not
20603 : : * be used to create the target object name for CREATE or ALTER commands.
20604 : : *
20605 : : * Note that the result is cached and must not be freed by the caller.
20606 : : */
20607 : : static const char *
20608 : 2409 : getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
20609 : : {
20610 : : TypeInfo *typeInfo;
20611 : : char *result;
20612 : : PQExpBuffer query;
20613 : : PGresult *res;
20614 : :
20615 [ - + ]: 2409 : if (oid == 0)
20616 : : {
20617 [ # # ]: 0 : if ((opts & zeroAsStar) != 0)
20618 : 0 : return "*";
20619 [ # # ]: 0 : else if ((opts & zeroAsNone) != 0)
20620 : 0 : return "NONE";
20621 : : }
20622 : :
20623 : : /* see if we have the result cached in the type's TypeInfo record */
20624 : 2409 : typeInfo = findTypeByOid(oid);
20625 [ + - + + ]: 2409 : if (typeInfo && typeInfo->ftypname)
20626 : 1918 : return typeInfo->ftypname;
20627 : :
20628 : 491 : query = createPQExpBuffer();
20629 : 491 : appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
20630 : : oid);
20631 : :
20632 : 491 : res = ExecuteSqlQueryForSingleRow(fout, query->data);
20633 : :
20634 : : /* result of format_type is already quoted */
20635 : 491 : result = pg_strdup(PQgetvalue(res, 0, 0));
20636 : :
20637 : 491 : PQclear(res);
20638 : 491 : destroyPQExpBuffer(query);
20639 : :
20640 : : /*
20641 : : * Cache the result for re-use in later requests, if possible. If we
20642 : : * don't have a TypeInfo for the type, the string will be leaked once the
20643 : : * caller is done with it ... but that case really should not happen, so
20644 : : * leaking if it does seems acceptable.
20645 : : */
20646 [ + - ]: 491 : if (typeInfo)
20647 : 491 : typeInfo->ftypname = result;
20648 : :
20649 : 491 : return result;
20650 : : }
20651 : :
20652 : : /*
20653 : : * Return a column list clause for the given relation.
20654 : : *
20655 : : * Special case: if there are no undropped columns in the relation, return
20656 : : * "", not an invalid "()" column list.
20657 : : */
20658 : : static const char *
20659 : 8962 : fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer)
20660 : : {
20661 : 8962 : int numatts = ti->numatts;
20662 : 8962 : char **attnames = ti->attnames;
20663 : 8962 : bool *attisdropped = ti->attisdropped;
20664 : 8962 : char *attgenerated = ti->attgenerated;
20665 : : bool needComma;
20666 : : int i;
20667 : :
20668 : 8962 : appendPQExpBufferChar(buffer, '(');
20669 : 8962 : needComma = false;
20670 [ + + ]: 42994 : for (i = 0; i < numatts; i++)
20671 : : {
20672 [ + + ]: 34032 : if (attisdropped[i])
20673 : 610 : continue;
20674 [ + + ]: 33422 : if (attgenerated[i])
20675 : 1200 : continue;
20676 [ + + ]: 32222 : if (needComma)
20677 : 23496 : appendPQExpBufferStr(buffer, ", ");
20678 : 32222 : appendPQExpBufferStr(buffer, fmtId(attnames[i]));
20679 : 32222 : needComma = true;
20680 : : }
20681 : :
20682 [ + + ]: 8962 : if (!needComma)
20683 : 236 : return ""; /* no undropped columns */
20684 : :
20685 : 8726 : appendPQExpBufferChar(buffer, ')');
20686 : 8726 : return buffer->data;
20687 : : }
20688 : :
20689 : : /*
20690 : : * Check if a reloptions array is nonempty.
20691 : : */
20692 : : static bool
20693 : 14767 : nonemptyReloptions(const char *reloptions)
20694 : : {
20695 : : /* Don't want to print it if it's just "{}" */
20696 [ + - + + ]: 14767 : return (reloptions != NULL && strlen(reloptions) > 2);
20697 : : }
20698 : :
20699 : : /*
20700 : : * Format a reloptions array and append it to the given buffer.
20701 : : *
20702 : : * "prefix" is prepended to the option names; typically it's "" or "toast.".
20703 : : */
20704 : : static void
20705 : 223 : appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
20706 : : const char *prefix, Archive *fout)
20707 : : {
20708 : : bool res;
20709 : :
20710 : 223 : res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
20711 : 223 : fout->std_strings);
20712 [ - + ]: 223 : if (!res)
20713 : 0 : pg_log_warning("could not parse %s array", "reloptions");
20714 : 223 : }
20715 : :
20716 : : /*
20717 : : * read_dump_filters - retrieve object identifier patterns from file
20718 : : *
20719 : : * Parse the specified filter file for include and exclude patterns, and add
20720 : : * them to the relevant lists. If the filename is "-" then filters will be
20721 : : * read from STDIN rather than a file.
20722 : : */
20723 : : static void
20724 : 26 : read_dump_filters(const char *filename, DumpOptions *dopt)
20725 : : {
20726 : : FilterStateData fstate;
20727 : : char *objname;
20728 : : FilterCommandType comtype;
20729 : : FilterObjectType objtype;
20730 : :
20731 : 26 : filter_init(&fstate, filename, exit_nicely);
20732 : :
20733 [ + + ]: 84 : while (filter_read_item(&fstate, &objname, &comtype, &objtype))
20734 : : {
20735 [ + + ]: 33 : if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
20736 : : {
20737 [ - - + + : 17 : switch (objtype)
+ + + - ]
20738 : : {
20739 : 0 : case FILTER_OBJECT_TYPE_NONE:
20740 : 0 : break;
20741 : 0 : case FILTER_OBJECT_TYPE_DATABASE:
20742 : : case FILTER_OBJECT_TYPE_FUNCTION:
20743 : : case FILTER_OBJECT_TYPE_INDEX:
20744 : : case FILTER_OBJECT_TYPE_TABLE_DATA:
20745 : : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20746 : : case FILTER_OBJECT_TYPE_TRIGGER:
20747 : 0 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20748 : : "include",
20749 : : filter_object_type_name(objtype));
20750 : 0 : exit_nicely(1);
20751 : : break; /* unreachable */
20752 : :
20753 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20754 : 1 : simple_string_list_append(&extension_include_patterns, objname);
20755 : 1 : break;
20756 : 1 : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20757 : 1 : simple_string_list_append(&foreign_servers_include_patterns, objname);
20758 : 1 : break;
20759 : 1 : case FILTER_OBJECT_TYPE_SCHEMA:
20760 : 1 : simple_string_list_append(&schema_include_patterns, objname);
20761 : 1 : dopt->include_everything = false;
20762 : 1 : break;
20763 : 13 : case FILTER_OBJECT_TYPE_TABLE:
20764 : 13 : simple_string_list_append(&table_include_patterns, objname);
20765 : 13 : dopt->include_everything = false;
20766 : 13 : break;
20767 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20768 : 1 : simple_string_list_append(&table_include_patterns_and_children,
20769 : : objname);
20770 : 1 : dopt->include_everything = false;
20771 : 1 : break;
20772 : : }
20773 : : }
20774 [ + + ]: 16 : else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
20775 : : {
20776 [ - + + + : 9 : switch (objtype)
+ + + +
- ]
20777 : : {
20778 : 0 : case FILTER_OBJECT_TYPE_NONE:
20779 : 0 : break;
20780 : 1 : case FILTER_OBJECT_TYPE_DATABASE:
20781 : : case FILTER_OBJECT_TYPE_FUNCTION:
20782 : : case FILTER_OBJECT_TYPE_INDEX:
20783 : : case FILTER_OBJECT_TYPE_TRIGGER:
20784 : : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
20785 : 1 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
20786 : : "exclude",
20787 : : filter_object_type_name(objtype));
20788 : 1 : exit_nicely(1);
20789 : : break;
20790 : :
20791 : 1 : case FILTER_OBJECT_TYPE_EXTENSION:
20792 : 1 : simple_string_list_append(&extension_exclude_patterns, objname);
20793 : 1 : break;
20794 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA:
20795 : 1 : simple_string_list_append(&tabledata_exclude_patterns,
20796 : : objname);
20797 : 1 : break;
20798 : 1 : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
20799 : 1 : simple_string_list_append(&tabledata_exclude_patterns_and_children,
20800 : : objname);
20801 : 1 : break;
20802 : 2 : case FILTER_OBJECT_TYPE_SCHEMA:
20803 : 2 : simple_string_list_append(&schema_exclude_patterns, objname);
20804 : 2 : break;
20805 : 2 : case FILTER_OBJECT_TYPE_TABLE:
20806 : 2 : simple_string_list_append(&table_exclude_patterns, objname);
20807 : 2 : break;
20808 : 1 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
20809 : 1 : simple_string_list_append(&table_exclude_patterns_and_children,
20810 : : objname);
20811 : 1 : break;
20812 : : }
20813 : : }
20814 : : else
20815 : : {
20816 : : Assert(comtype == FILTER_COMMAND_TYPE_NONE);
20817 : : Assert(objtype == FILTER_OBJECT_TYPE_NONE);
20818 : : }
20819 : :
20820 [ + + ]: 32 : if (objname)
20821 : 25 : free(objname);
20822 : : }
20823 : :
20824 : 22 : filter_free(&fstate);
20825 : 22 : }
|