| 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-2025, 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_proc_d.h" |
| 54 |
#include "catalog/pg_publication_d.h" |
| 55 |
#include "catalog/pg_subscription_d.h" |
| 56 |
#include "catalog/pg_type_d.h" |
| 57 |
#include "common/connect.h" |
| 58 |
#include "common/int.h" |
| 59 |
#include "common/relpath.h" |
| 60 |
#include "common/shortest_dec.h" |
| 61 |
#include "compress_io.h" |
| 62 |
#include "dumputils.h" |
| 63 |
#include "fe_utils/option_utils.h" |
| 64 |
#include "fe_utils/string_utils.h" |
| 65 |
#include "filter.h" |
| 66 |
#include "getopt_long.h" |
| 67 |
#include "libpq/libpq-fs.h" |
| 68 |
#include "parallel.h" |
| 69 |
#include "pg_backup_db.h" |
| 70 |
#include "pg_backup_utils.h" |
| 71 |
#include "pg_dump.h" |
| 72 |
#include "storage/block.h" |
| 73 |
|
| 74 |
typedef struct |
| 75 |
{ |
| 76 |
Oid roleoid; /* role's OID */ |
| 77 |
const char *rolename; /* role's name */ |
| 78 |
} RoleNameItem; |
| 79 |
|
| 80 |
typedef struct |
| 81 |
{ |
| 82 |
const char *descr; /* comment for an object */ |
| 83 |
Oid classoid; /* object class (catalog OID) */ |
| 84 |
Oid objoid; /* object OID */ |
| 85 |
int objsubid; /* subobject (table column #) */ |
| 86 |
} CommentItem; |
| 87 |
|
| 88 |
typedef struct |
| 89 |
{ |
| 90 |
const char *provider; /* label provider of this security label */ |
| 91 |
const char *label; /* security label for an object */ |
| 92 |
Oid classoid; /* object class (catalog OID) */ |
| 93 |
Oid objoid; /* object OID */ |
| 94 |
int objsubid; /* subobject (table column #) */ |
| 95 |
} SecLabelItem; |
| 96 |
|
| 97 |
typedef struct |
| 98 |
{ |
| 99 |
Oid oid; /* object OID */ |
| 100 |
char relkind; /* object kind */ |
| 101 |
RelFileNumber relfilenumber; /* object filenode */ |
| 102 |
Oid toast_oid; /* toast table OID */ |
| 103 |
RelFileNumber toast_relfilenumber; /* toast table filenode */ |
| 104 |
Oid toast_index_oid; /* toast table index OID */ |
| 105 |
RelFileNumber toast_index_relfilenumber; /* toast table index filenode */ |
| 106 |
} BinaryUpgradeClassOidItem; |
| 107 |
|
| 108 |
/* sequence types */ |
| 109 |
typedef enum SeqType |
| 110 |
{ |
| 111 |
SEQTYPE_SMALLINT, |
| 112 |
SEQTYPE_INTEGER, |
| 113 |
SEQTYPE_BIGINT, |
| 114 |
} SeqType; |
| 115 |
|
| 116 |
static const char *const SeqTypeNames[] = |
| 117 |
{ |
| 118 |
[SEQTYPE_SMALLINT] = "smallint", |
| 119 |
[SEQTYPE_INTEGER] = "integer", |
| 120 |
[SEQTYPE_BIGINT] = "bigint", |
| 121 |
}; |
| 122 |
|
| 123 |
StaticAssertDecl(lengthof(SeqTypeNames) == (SEQTYPE_BIGINT + 1), |
| 124 |
"array length mismatch"); |
| 125 |
|
| 126 |
typedef struct |
| 127 |
{ |
| 128 |
Oid oid; /* sequence OID */ |
| 129 |
SeqType seqtype; /* data type of sequence */ |
| 130 |
bool cycled; /* whether sequence cycles */ |
| 131 |
int64 minv; /* minimum value */ |
| 132 |
int64 maxv; /* maximum value */ |
| 133 |
int64 startv; /* start value */ |
| 134 |
int64 incby; /* increment value */ |
| 135 |
int64 cache; /* cache size */ |
| 136 |
int64 last_value; /* last value of sequence */ |
| 137 |
bool is_called; /* whether nextval advances before returning */ |
| 138 |
bool null_seqtuple; /* did pg_get_sequence_data return nulls? */ |
| 139 |
} SequenceItem; |
| 140 |
|
| 141 |
typedef enum OidOptions |
| 142 |
{ |
| 143 |
zeroIsError = 1, |
| 144 |
zeroAsStar = 2, |
| 145 |
zeroAsNone = 4, |
| 146 |
} OidOptions; |
| 147 |
|
| 148 |
/* global decls */ |
| 149 |
static bool dosync = true; /* Issue fsync() to make dump durable on disk. */ |
| 150 |
|
| 151 |
static Oid g_last_builtin_oid; /* value of the last builtin oid */ |
| 152 |
|
| 153 |
/* The specified names/patterns should to match at least one entity */ |
| 154 |
static int strict_names = 0; |
| 155 |
|
| 156 |
static pg_compress_algorithm compression_algorithm = PG_COMPRESSION_NONE; |
| 157 |
|
| 158 |
/* |
| 159 |
* Object inclusion/exclusion lists |
| 160 |
* |
| 161 |
* The string lists record the patterns given by command-line switches, |
| 162 |
* which we then convert to lists of OIDs of matching objects. |
| 163 |
*/ |
| 164 |
static SimpleStringList schema_include_patterns = {NULL, NULL}; |
| 165 |
static SimpleOidList schema_include_oids = {NULL, NULL}; |
| 166 |
static SimpleStringList schema_exclude_patterns = {NULL, NULL}; |
| 167 |
static SimpleOidList schema_exclude_oids = {NULL, NULL}; |
| 168 |
|
| 169 |
static SimpleStringList table_include_patterns = {NULL, NULL}; |
| 170 |
static SimpleStringList table_include_patterns_and_children = {NULL, NULL}; |
| 171 |
static SimpleOidList table_include_oids = {NULL, NULL}; |
| 172 |
static SimpleStringList table_exclude_patterns = {NULL, NULL}; |
| 173 |
static SimpleStringList table_exclude_patterns_and_children = {NULL, NULL}; |
| 174 |
static SimpleOidList table_exclude_oids = {NULL, NULL}; |
| 175 |
static SimpleStringList tabledata_exclude_patterns = {NULL, NULL}; |
| 176 |
static SimpleStringList tabledata_exclude_patterns_and_children = {NULL, NULL}; |
| 177 |
static SimpleOidList tabledata_exclude_oids = {NULL, NULL}; |
| 178 |
|
| 179 |
static SimpleStringList foreign_servers_include_patterns = {NULL, NULL}; |
| 180 |
static SimpleOidList foreign_servers_include_oids = {NULL, NULL}; |
| 181 |
|
| 182 |
static SimpleStringList extension_include_patterns = {NULL, NULL}; |
| 183 |
static SimpleOidList extension_include_oids = {NULL, NULL}; |
| 184 |
|
| 185 |
static SimpleStringList extension_exclude_patterns = {NULL, NULL}; |
| 186 |
static SimpleOidList extension_exclude_oids = {NULL, NULL}; |
| 187 |
|
| 188 |
static const CatalogId nilCatalogId = {0, 0}; |
| 189 |
|
| 190 |
/* override for standard extra_float_digits setting */ |
| 191 |
static bool have_extra_float_digits = false; |
| 192 |
static int extra_float_digits; |
| 193 |
|
| 194 |
/* sorted table of role names */ |
| 195 |
static RoleNameItem *rolenames = NULL; |
| 196 |
static int nrolenames = 0; |
| 197 |
|
| 198 |
/* sorted table of comments */ |
| 199 |
static CommentItem *comments = NULL; |
| 200 |
static int ncomments = 0; |
| 201 |
|
| 202 |
/* sorted table of security labels */ |
| 203 |
static SecLabelItem *seclabels = NULL; |
| 204 |
static int nseclabels = 0; |
| 205 |
|
| 206 |
/* sorted table of pg_class information for binary upgrade */ |
| 207 |
static BinaryUpgradeClassOidItem *binaryUpgradeClassOids = NULL; |
| 208 |
static int nbinaryUpgradeClassOids = 0; |
| 209 |
|
| 210 |
/* sorted table of sequences */ |
| 211 |
static SequenceItem *sequences = NULL; |
| 212 |
static int nsequences = 0; |
| 213 |
|
| 214 |
/* Maximum number of relations to fetch in a fetchAttributeStats() call. */ |
| 215 |
#define MAX_ATTR_STATS_RELS 64 |
| 216 |
|
| 217 |
/* |
| 218 |
* The default number of rows per INSERT when |
| 219 |
* --inserts is specified without --rows-per-insert |
| 220 |
*/ |
| 221 |
#define DUMP_DEFAULT_ROWS_PER_INSERT 1 |
| 222 |
|
| 223 |
/* |
| 224 |
* Maximum number of large objects to group into a single ArchiveEntry. |
| 225 |
* At some point we might want to make this user-controllable, but for now |
| 226 |
* a hard-wired setting will suffice. |
| 227 |
*/ |
| 228 |
#define MAX_BLOBS_PER_ARCHIVE_ENTRY 1000 |
| 229 |
|
| 230 |
/* |
| 231 |
* Macro for producing quoted, schema-qualified name of a dumpable object. |
| 232 |
*/ |
| 233 |
#define fmtQualifiedDumpable(obj) \ |
| 234 |
fmtQualifiedId((obj)->dobj.namespace->dobj.name, \ |
| 235 |
(obj)->dobj.name) |
| 236 |
|
| 237 |
static void help(const char *progname); |
| 238 |
static void setup_connection(Archive *AH, |
| 239 |
const char *dumpencoding, const char *dumpsnapshot, |
| 240 |
char *use_role); |
| 241 |
static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode); |
| 242 |
static void expand_schema_name_patterns(Archive *fout, |
| 243 |
SimpleStringList *patterns, |
| 244 |
SimpleOidList *oids, |
| 245 |
bool strict_names); |
| 246 |
static void expand_extension_name_patterns(Archive *fout, |
| 247 |
SimpleStringList *patterns, |
| 248 |
SimpleOidList *oids, |
| 249 |
bool strict_names); |
| 250 |
static void expand_foreign_server_name_patterns(Archive *fout, |
| 251 |
SimpleStringList *patterns, |
| 252 |
SimpleOidList *oids); |
| 253 |
static void expand_table_name_patterns(Archive *fout, |
| 254 |
SimpleStringList *patterns, |
| 255 |
SimpleOidList *oids, |
| 256 |
bool strict_names, |
| 257 |
bool with_child_tables); |
| 258 |
static void prohibit_crossdb_refs(PGconn *conn, const char *dbname, |
| 259 |
const char *pattern); |
| 260 |
|
| 261 |
static NamespaceInfo *findNamespace(Oid nsoid); |
| 262 |
static void dumpTableData(Archive *fout, const TableDataInfo *tdinfo); |
| 263 |
static void refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo); |
| 264 |
static const char *getRoleName(const char *roleoid_str); |
| 265 |
static void collectRoleNames(Archive *fout); |
| 266 |
static void getAdditionalACLs(Archive *fout); |
| 267 |
static void dumpCommentExtended(Archive *fout, const char *type, |
| 268 |
const char *name, const char *namespace, |
| 269 |
const char *owner, CatalogId catalogId, |
| 270 |
int subid, DumpId dumpId, |
| 271 |
const char *initdb_comment); |
| 272 |
static inline void dumpComment(Archive *fout, const char *type, |
| 273 |
const char *name, const char *namespace, |
| 274 |
const char *owner, CatalogId catalogId, |
| 275 |
int subid, DumpId dumpId); |
| 276 |
static int findComments(Oid classoid, Oid objoid, CommentItem **items); |
| 277 |
static void collectComments(Archive *fout); |
| 278 |
static void dumpSecLabel(Archive *fout, const char *type, const char *name, |
| 279 |
const char *namespace, const char *owner, |
| 280 |
CatalogId catalogId, int subid, DumpId dumpId); |
| 281 |
static int findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items); |
| 282 |
static void collectSecLabels(Archive *fout); |
| 283 |
static void dumpDumpableObject(Archive *fout, DumpableObject *dobj); |
| 284 |
static void dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo); |
| 285 |
static void dumpExtension(Archive *fout, const ExtensionInfo *extinfo); |
| 286 |
static void dumpType(Archive *fout, const TypeInfo *tyinfo); |
| 287 |
static void dumpBaseType(Archive *fout, const TypeInfo *tyinfo); |
| 288 |
static void dumpEnumType(Archive *fout, const TypeInfo *tyinfo); |
| 289 |
static void dumpRangeType(Archive *fout, const TypeInfo *tyinfo); |
| 290 |
static void dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo); |
| 291 |
static void dumpDomain(Archive *fout, const TypeInfo *tyinfo); |
| 292 |
static void dumpCompositeType(Archive *fout, const TypeInfo *tyinfo); |
| 293 |
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, |
| 294 |
PGresult *res); |
| 295 |
static void dumpShellType(Archive *fout, const ShellTypeInfo *stinfo); |
| 296 |
static void dumpProcLang(Archive *fout, const ProcLangInfo *plang); |
| 297 |
static void dumpFunc(Archive *fout, const FuncInfo *finfo); |
| 298 |
static void dumpCast(Archive *fout, const CastInfo *cast); |
| 299 |
static void dumpTransform(Archive *fout, const TransformInfo *transform); |
| 300 |
static void dumpOpr(Archive *fout, const OprInfo *oprinfo); |
| 301 |
static void dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo); |
| 302 |
static void dumpOpclass(Archive *fout, const OpclassInfo *opcinfo); |
| 303 |
static void dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo); |
| 304 |
static void dumpCollation(Archive *fout, const CollInfo *collinfo); |
| 305 |
static void dumpConversion(Archive *fout, const ConvInfo *convinfo); |
| 306 |
static void dumpRule(Archive *fout, const RuleInfo *rinfo); |
| 307 |
static void dumpAgg(Archive *fout, const AggInfo *agginfo); |
| 308 |
static void dumpTrigger(Archive *fout, const TriggerInfo *tginfo); |
| 309 |
static void dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo); |
| 310 |
static void dumpTable(Archive *fout, const TableInfo *tbinfo); |
| 311 |
static void dumpTableSchema(Archive *fout, const TableInfo *tbinfo); |
| 312 |
static void dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo); |
| 313 |
static void dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo); |
| 314 |
static void collectSequences(Archive *fout); |
| 315 |
static void dumpSequence(Archive *fout, const TableInfo *tbinfo); |
| 316 |
static void dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo); |
| 317 |
static void dumpIndex(Archive *fout, const IndxInfo *indxinfo); |
| 318 |
static void dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo); |
| 319 |
static void dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo); |
| 320 |
static void dumpConstraint(Archive *fout, const ConstraintInfo *coninfo); |
| 321 |
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo); |
| 322 |
static void dumpTSParser(Archive *fout, const TSParserInfo *prsinfo); |
| 323 |
static void dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo); |
| 324 |
static void dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo); |
| 325 |
static void dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo); |
| 326 |
static void dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo); |
| 327 |
static void dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo); |
| 328 |
static void dumpUserMappings(Archive *fout, |
| 329 |
const char *servername, const char *namespace, |
| 330 |
const char *owner, CatalogId catalogId, DumpId dumpId); |
| 331 |
static void dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo); |
| 332 |
|
| 333 |
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, |
| 334 |
const char *type, const char *name, const char *subname, |
| 335 |
const char *nspname, const char *tag, const char *owner, |
| 336 |
const DumpableAcl *dacl); |
| 337 |
|
| 338 |
static void getDependencies(Archive *fout); |
| 339 |
static void BuildArchiveDependencies(Archive *fout); |
| 340 |
static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, |
| 341 |
DumpId **dependencies, int *nDeps, int *allocDeps); |
| 342 |
|
| 343 |
static DumpableObject *createBoundaryObjects(void); |
| 344 |
static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs, |
| 345 |
DumpableObject *boundaryObjs); |
| 346 |
|
| 347 |
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx); |
| 348 |
static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo); |
| 349 |
static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind); |
| 350 |
static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo); |
| 351 |
static void buildMatViewRefreshDependencies(Archive *fout); |
| 352 |
static void getTableDataFKConstraints(void); |
| 353 |
static void determineNotNullFlags(Archive *fout, PGresult *res, int r, |
| 354 |
TableInfo *tbinfo, int j, |
| 355 |
int i_notnull_name, |
| 356 |
int i_notnull_comment, |
| 357 |
int i_notnull_invalidoid, |
| 358 |
int i_notnull_noinherit, |
| 359 |
int i_notnull_islocal, |
| 360 |
PQExpBuffer *invalidnotnulloids); |
| 361 |
static char *format_function_arguments(const FuncInfo *finfo, const char *funcargs, |
| 362 |
bool is_agg); |
| 363 |
static char *format_function_signature(Archive *fout, |
| 364 |
const FuncInfo *finfo, bool honor_quotes); |
| 365 |
static char *convertRegProcReference(const char *proc); |
| 366 |
static char *getFormattedOperatorName(const char *oproid); |
| 367 |
static char *convertTSFunction(Archive *fout, Oid funcOid); |
| 368 |
static const char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts); |
| 369 |
static void getLOs(Archive *fout); |
| 370 |
static void dumpLO(Archive *fout, const LoInfo *loinfo); |
| 371 |
static int dumpLOs(Archive *fout, const void *arg); |
| 372 |
static void dumpPolicy(Archive *fout, const PolicyInfo *polinfo); |
| 373 |
static void dumpPublication(Archive *fout, const PublicationInfo *pubinfo); |
| 374 |
static void dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo); |
| 375 |
static void dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo); |
| 376 |
static void dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo); |
| 377 |
static void dumpDatabase(Archive *fout); |
| 378 |
static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf, |
| 379 |
const char *dbname, Oid dboid); |
| 380 |
static void dumpEncoding(Archive *AH); |
| 381 |
static void dumpStdStrings(Archive *AH); |
| 382 |
static void dumpSearchPath(Archive *AH); |
| 383 |
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, |
| 384 |
PQExpBuffer upgrade_buffer, |
| 385 |
Oid pg_type_oid, |
| 386 |
bool force_array_type, |
| 387 |
bool include_multirange_type); |
| 388 |
static void binary_upgrade_set_type_oids_by_rel(Archive *fout, |
| 389 |
PQExpBuffer upgrade_buffer, |
| 390 |
const TableInfo *tbinfo); |
| 391 |
static void collectBinaryUpgradeClassOids(Archive *fout); |
| 392 |
static void binary_upgrade_set_pg_class_oids(Archive *fout, |
| 393 |
PQExpBuffer upgrade_buffer, |
| 394 |
Oid pg_class_oid); |
| 395 |
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, |
| 396 |
const DumpableObject *dobj, |
| 397 |
const char *objtype, |
| 398 |
const char *objname, |
| 399 |
const char *objnamespace); |
| 400 |
static const char *getAttrName(int attrnum, const TableInfo *tblInfo); |
| 401 |
static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer); |
| 402 |
static bool nonemptyReloptions(const char *reloptions); |
| 403 |
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, |
| 404 |
const char *prefix, Archive *fout); |
| 405 |
static char *get_synchronized_snapshot(Archive *fout); |
| 406 |
static void set_restrict_relation_kind(Archive *AH, const char *value); |
| 407 |
static void setupDumpWorker(Archive *AH); |
| 408 |
static TableInfo *getRootTableInfo(const TableInfo *tbinfo); |
| 409 |
static bool forcePartitionRootLoad(const TableInfo *tbinfo); |
| 410 |
static void read_dump_filters(const char *filename, DumpOptions *dopt); |
| 411 |
|
| 412 |
|
| 413 |
int |
| 414 |
main(int argc, char **argv) |
| 415 |
{ |
| 416 |
int c; |
| 417 |
const char *filename = NULL; |
| 418 |
const char *format = "p"; |
| 419 |
TableInfo *tblinfo; |
| 420 |
int numTables; |
| 421 |
DumpableObject **dobjs; |
| 422 |
int numObjs; |
| 423 |
DumpableObject *boundaryObjs; |
| 424 |
int i; |
| 425 |
int optindex; |
| 426 |
RestoreOptions *ropt; |
| 427 |
Archive *fout; /* the script file */ |
| 428 |
bool g_verbose = false; |
| 429 |
const char *dumpencoding = NULL; |
| 430 |
const char *dumpsnapshot = NULL; |
| 431 |
char *use_role = NULL; |
| 432 |
int numWorkers = 1; |
| 433 |
int plainText = 0; |
| 434 |
ArchiveFormat archiveFormat = archUnknown; |
| 435 |
ArchiveMode archiveMode; |
| 436 |
pg_compress_specification compression_spec = {0}; |
| 437 |
char *compression_detail = NULL; |
| 438 |
char *compression_algorithm_str = "none"; |
| 439 |
char *error_detail = NULL; |
| 440 |
bool user_compression_defined = false; |
| 441 |
DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC; |
| 442 |
bool data_only = false; |
| 443 |
bool schema_only = false; |
| 444 |
bool statistics_only = false; |
| 445 |
bool with_statistics = false; |
| 446 |
bool no_data = false; |
| 447 |
bool no_schema = false; |
| 448 |
bool no_statistics = false; |
| 449 |
|
| 450 |
static DumpOptions dopt; |
| 451 |
|
| 452 |
static struct option long_options[] = { |
| 453 |
{"data-only", no_argument, NULL, 'a'}, |
| 454 |
{"blobs", no_argument, NULL, 'b'}, |
| 455 |
{"large-objects", no_argument, NULL, 'b'}, |
| 456 |
{"no-blobs", no_argument, NULL, 'B'}, |
| 457 |
{"no-large-objects", no_argument, NULL, 'B'}, |
| 458 |
{"clean", no_argument, NULL, 'c'}, |
| 459 |
{"create", no_argument, NULL, 'C'}, |
| 460 |
{"dbname", required_argument, NULL, 'd'}, |
| 461 |
{"extension", required_argument, NULL, 'e'}, |
| 462 |
{"file", required_argument, NULL, 'f'}, |
| 463 |
{"format", required_argument, NULL, 'F'}, |
| 464 |
{"host", required_argument, NULL, 'h'}, |
| 465 |
{"jobs", 1, NULL, 'j'}, |
| 466 |
{"no-reconnect", no_argument, NULL, 'R'}, |
| 467 |
{"no-owner", no_argument, NULL, 'O'}, |
| 468 |
{"port", required_argument, NULL, 'p'}, |
| 469 |
{"schema", required_argument, NULL, 'n'}, |
| 470 |
{"exclude-schema", required_argument, NULL, 'N'}, |
| 471 |
{"schema-only", no_argument, NULL, 's'}, |
| 472 |
{"superuser", required_argument, NULL, 'S'}, |
| 473 |
{"table", required_argument, NULL, 't'}, |
| 474 |
{"exclude-table", required_argument, NULL, 'T'}, |
| 475 |
{"no-password", no_argument, NULL, 'w'}, |
| 476 |
{"password", no_argument, NULL, 'W'}, |
| 477 |
{"username", required_argument, NULL, 'U'}, |
| 478 |
{"verbose", no_argument, NULL, 'v'}, |
| 479 |
{"no-privileges", no_argument, NULL, 'x'}, |
| 480 |
{"no-acl", no_argument, NULL, 'x'}, |
| 481 |
{"compress", required_argument, NULL, 'Z'}, |
| 482 |
{"encoding", required_argument, NULL, 'E'}, |
| 483 |
{"help", no_argument, NULL, '?'}, |
| 484 |
{"version", no_argument, NULL, 'V'}, |
| 485 |
|
| 486 |
/* |
| 487 |
* the following options don't have an equivalent short option letter |
| 488 |
*/ |
| 489 |
{"attribute-inserts", no_argument, &dopt.column_inserts, 1}, |
| 490 |
{"binary-upgrade", no_argument, &dopt.binary_upgrade, 1}, |
| 491 |
{"column-inserts", no_argument, &dopt.column_inserts, 1}, |
| 492 |
{"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1}, |
| 493 |
{"disable-triggers", no_argument, &dopt.disable_triggers, 1}, |
| 494 |
{"enable-row-security", no_argument, &dopt.enable_row_security, 1}, |
| 495 |
{"exclude-table-data", required_argument, NULL, 4}, |
| 496 |
{"extra-float-digits", required_argument, NULL, 8}, |
| 497 |
{"if-exists", no_argument, &dopt.if_exists, 1}, |
| 498 |
{"inserts", no_argument, NULL, 9}, |
| 499 |
{"lock-wait-timeout", required_argument, NULL, 2}, |
| 500 |
{"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1}, |
| 501 |
{"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1}, |
| 502 |
{"quote-all-identifiers", no_argument, "e_all_identifiers, 1}, |
| 503 |
{"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1}, |
| 504 |
{"role", required_argument, NULL, 3}, |
| 505 |
{"section", required_argument, NULL, 5}, |
| 506 |
{"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1}, |
| 507 |
{"snapshot", required_argument, NULL, 6}, |
| 508 |
{"statistics", no_argument, NULL, 22}, |
| 509 |
{"statistics-only", no_argument, NULL, 18}, |
| 510 |
{"strict-names", no_argument, &strict_names, 1}, |
| 511 |
{"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1}, |
| 512 |
{"no-comments", no_argument, &dopt.no_comments, 1}, |
| 513 |
{"no-data", no_argument, NULL, 19}, |
| 514 |
{"no-policies", no_argument, &dopt.no_policies, 1}, |
| 515 |
{"no-publications", no_argument, &dopt.no_publications, 1}, |
| 516 |
{"no-schema", no_argument, NULL, 20}, |
| 517 |
{"no-security-labels", no_argument, &dopt.no_security_labels, 1}, |
| 518 |
{"no-statistics", no_argument, NULL, 21}, |
| 519 |
{"no-subscriptions", no_argument, &dopt.no_subscriptions, 1}, |
| 520 |
{"no-toast-compression", no_argument, &dopt.no_toast_compression, 1}, |
| 521 |
{"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1}, |
| 522 |
{"no-sync", no_argument, NULL, 7}, |
| 523 |
{"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1}, |
| 524 |
{"rows-per-insert", required_argument, NULL, 10}, |
| 525 |
{"include-foreign-data", required_argument, NULL, 11}, |
| 526 |
{"table-and-children", required_argument, NULL, 12}, |
| 527 |
{"exclude-table-and-children", required_argument, NULL, 13}, |
| 528 |
{"exclude-table-data-and-children", required_argument, NULL, 14}, |
| 529 |
{"sync-method", required_argument, NULL, 15}, |
| 530 |
{"filter", required_argument, NULL, 16}, |
| 531 |
{"exclude-extension", required_argument, NULL, 17}, |
| 532 |
{"sequence-data", no_argument, &dopt.sequence_data, 1}, |
| 533 |
{"restrict-key", required_argument, NULL, 25}, |
| 534 |
|
| 535 |
{NULL, 0, NULL, 0} |
| 536 |
}; |
| 537 |
|
| 538 |
pg_logging_init(argv[0]); |
| 539 |
pg_logging_set_level(PG_LOG_WARNING); |
| 540 |
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump")); |
| 541 |
|
| 542 |
/* |
| 543 |
* Initialize what we need for parallel execution, especially for thread |
| 544 |
* support on Windows. |
| 545 |
*/ |
| 546 |
init_parallel_dump_utils(); |
| 547 |
|
| 548 |
progname = get_progname(argv[0]); |
| 549 |
|
| 550 |
if (argc > 1) |
| 551 |
{ |
| 552 |
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) |
| 553 |
{ |
| 554 |
help(progname); |
| 555 |
exit_nicely(0); |
| 556 |
} |
| 557 |
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) |
| 558 |
{ |
| 559 |
puts("pg_dump (PostgreSQL) " PG_VERSION); |
| 560 |
exit_nicely(0); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
InitDumpOptions(&dopt); |
| 565 |
|
| 566 |
while ((c = getopt_long(argc, argv, "abBcCd:e:E:f:F:h:j:n:N:Op:RsS:t:T:U:vwWxXZ:", |
| 567 |
long_options, &optindex)) != -1) |
| 568 |
{ |
| 569 |
switch (c) |
| 570 |
{ |
| 571 |
case 'a': /* Dump data only */ |
| 572 |
data_only = true; |
| 573 |
break; |
| 574 |
|
| 575 |
case 'b': /* Dump LOs */ |
| 576 |
dopt.outputLOs = true; |
| 577 |
break; |
| 578 |
|
| 579 |
case 'B': /* Don't dump LOs */ |
| 580 |
dopt.dontOutputLOs = true; |
| 581 |
break; |
| 582 |
|
| 583 |
case 'c': /* clean (i.e., drop) schema prior to create */ |
| 584 |
dopt.outputClean = 1; |
| 585 |
break; |
| 586 |
|
| 587 |
case 'C': /* Create DB */ |
| 588 |
dopt.outputCreateDB = 1; |
| 589 |
break; |
| 590 |
|
| 591 |
case 'd': /* database name */ |
| 592 |
dopt.cparams.dbname = pg_strdup(optarg); |
| 593 |
break; |
| 594 |
|
| 595 |
case 'e': /* include extension(s) */ |
| 596 |
simple_string_list_append(&extension_include_patterns, optarg); |
| 597 |
dopt.include_everything = false; |
| 598 |
break; |
| 599 |
|
| 600 |
case 'E': /* Dump encoding */ |
| 601 |
dumpencoding = pg_strdup(optarg); |
| 602 |
break; |
| 603 |
|
| 604 |
case 'f': |
| 605 |
filename = pg_strdup(optarg); |
| 606 |
break; |
| 607 |
|
| 608 |
case 'F': |
| 609 |
format = pg_strdup(optarg); |
| 610 |
break; |
| 611 |
|
| 612 |
case 'h': /* server host */ |
| 613 |
dopt.cparams.pghost = pg_strdup(optarg); |
| 614 |
break; |
| 615 |
|
| 616 |
case 'j': /* number of dump jobs */ |
| 617 |
if (!option_parse_int(optarg, "-j/--jobs", 1, |
| 618 |
PG_MAX_JOBS, |
| 619 |
&numWorkers)) |
| 620 |
exit_nicely(1); |
| 621 |
break; |
| 622 |
|
| 623 |
case 'n': /* include schema(s) */ |
| 624 |
simple_string_list_append(&schema_include_patterns, optarg); |
| 625 |
dopt.include_everything = false; |
| 626 |
break; |
| 627 |
|
| 628 |
case 'N': /* exclude schema(s) */ |
| 629 |
simple_string_list_append(&schema_exclude_patterns, optarg); |
| 630 |
break; |
| 631 |
|
| 632 |
case 'O': /* Don't reconnect to match owner */ |
| 633 |
dopt.outputNoOwner = 1; |
| 634 |
break; |
| 635 |
|
| 636 |
case 'p': /* server port */ |
| 637 |
dopt.cparams.pgport = pg_strdup(optarg); |
| 638 |
break; |
| 639 |
|
| 640 |
case 'R': |
| 641 |
/* no-op, still accepted for backwards compatibility */ |
| 642 |
break; |
| 643 |
|
| 644 |
case 's': /* dump schema only */ |
| 645 |
schema_only = true; |
| 646 |
break; |
| 647 |
|
| 648 |
case 'S': /* Username for superuser in plain text output */ |
| 649 |
dopt.outputSuperuser = pg_strdup(optarg); |
| 650 |
break; |
| 651 |
|
| 652 |
case 't': /* include table(s) */ |
| 653 |
simple_string_list_append(&table_include_patterns, optarg); |
| 654 |
dopt.include_everything = false; |
| 655 |
break; |
| 656 |
|
| 657 |
case 'T': /* exclude table(s) */ |
| 658 |
simple_string_list_append(&table_exclude_patterns, optarg); |
| 659 |
break; |
| 660 |
|
| 661 |
case 'U': |
| 662 |
dopt.cparams.username = pg_strdup(optarg); |
| 663 |
break; |
| 664 |
|
| 665 |
case 'v': /* verbose */ |
| 666 |
g_verbose = true; |
| 667 |
pg_logging_increase_verbosity(); |
| 668 |
break; |
| 669 |
|
| 670 |
case 'w': |
| 671 |
dopt.cparams.promptPassword = TRI_NO; |
| 672 |
break; |
| 673 |
|
| 674 |
case 'W': |
| 675 |
dopt.cparams.promptPassword = TRI_YES; |
| 676 |
break; |
| 677 |
|
| 678 |
case 'x': /* skip ACL dump */ |
| 679 |
dopt.aclsSkip = true; |
| 680 |
break; |
| 681 |
|
| 682 |
case 'Z': /* Compression */ |
| 683 |
parse_compress_options(optarg, &compression_algorithm_str, |
| 684 |
&compression_detail); |
| 685 |
user_compression_defined = true; |
| 686 |
break; |
| 687 |
|
| 688 |
case 0: |
| 689 |
/* This covers the long options. */ |
| 690 |
break; |
| 691 |
|
| 692 |
case 2: /* lock-wait-timeout */ |
| 693 |
dopt.lockWaitTimeout = pg_strdup(optarg); |
| 694 |
break; |
| 695 |
|
| 696 |
case 3: /* SET ROLE */ |
| 697 |
use_role = pg_strdup(optarg); |
| 698 |
break; |
| 699 |
|
| 700 |
case 4: /* exclude table(s) data */ |
| 701 |
simple_string_list_append(&tabledata_exclude_patterns, optarg); |
| 702 |
break; |
| 703 |
|
| 704 |
case 5: /* section */ |
| 705 |
set_dump_section(optarg, &dopt.dumpSections); |
| 706 |
break; |
| 707 |
|
| 708 |
case 6: /* snapshot */ |
| 709 |
dumpsnapshot = pg_strdup(optarg); |
| 710 |
break; |
| 711 |
|
| 712 |
case 7: /* no-sync */ |
| 713 |
dosync = false; |
| 714 |
break; |
| 715 |
|
| 716 |
case 8: |
| 717 |
have_extra_float_digits = true; |
| 718 |
if (!option_parse_int(optarg, "--extra-float-digits", -15, 3, |
| 719 |
&extra_float_digits)) |
| 720 |
exit_nicely(1); |
| 721 |
break; |
| 722 |
|
| 723 |
case 9: /* inserts */ |
| 724 |
|
| 725 |
/* |
| 726 |
* dump_inserts also stores --rows-per-insert, careful not to |
| 727 |
* overwrite that. |
| 728 |
*/ |
| 729 |
if (dopt.dump_inserts == 0) |
| 730 |
dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT; |
| 731 |
break; |
| 732 |
|
| 733 |
case 10: /* rows per insert */ |
| 734 |
if (!option_parse_int(optarg, "--rows-per-insert", 1, INT_MAX, |
| 735 |
&dopt.dump_inserts)) |
| 736 |
exit_nicely(1); |
| 737 |
break; |
| 738 |
|
| 739 |
case 11: /* include foreign data */ |
| 740 |
simple_string_list_append(&foreign_servers_include_patterns, |
| 741 |
optarg); |
| 742 |
break; |
| 743 |
|
| 744 |
case 12: /* include table(s) and their children */ |
| 745 |
simple_string_list_append(&table_include_patterns_and_children, |
| 746 |
optarg); |
| 747 |
dopt.include_everything = false; |
| 748 |
break; |
| 749 |
|
| 750 |
case 13: /* exclude table(s) and their children */ |
| 751 |
simple_string_list_append(&table_exclude_patterns_and_children, |
| 752 |
optarg); |
| 753 |
break; |
| 754 |
|
| 755 |
case 14: /* exclude data of table(s) and children */ |
| 756 |
simple_string_list_append(&tabledata_exclude_patterns_and_children, |
| 757 |
optarg); |
| 758 |
break; |
| 759 |
|
| 760 |
case 15: |
| 761 |
if (!parse_sync_method(optarg, &sync_method)) |
| 762 |
exit_nicely(1); |
| 763 |
break; |
| 764 |
|
| 765 |
case 16: /* read object filters from file */ |
| 766 |
read_dump_filters(optarg, &dopt); |
| 767 |
break; |
| 768 |
|
| 769 |
case 17: /* exclude extension(s) */ |
| 770 |
simple_string_list_append(&extension_exclude_patterns, |
| 771 |
optarg); |
| 772 |
break; |
| 773 |
|
| 774 |
case 18: |
| 775 |
statistics_only = true; |
| 776 |
break; |
| 777 |
|
| 778 |
case 19: |
| 779 |
no_data = true; |
| 780 |
break; |
| 781 |
|
| 782 |
case 20: |
| 783 |
no_schema = true; |
| 784 |
break; |
| 785 |
|
| 786 |
case 21: |
| 787 |
no_statistics = true; |
| 788 |
break; |
| 789 |
|
| 790 |
case 22: |
| 791 |
with_statistics = true; |
| 792 |
break; |
| 793 |
|
| 794 |
case 25: |
| 795 |
dopt.restrict_key = pg_strdup(optarg); |
| 796 |
break; |
| 797 |
|
| 798 |
default: |
| 799 |
/* getopt_long already emitted a complaint */ |
| 800 |
pg_log_error_hint("Try \"%s --help\" for more information.", progname); |
| 801 |
exit_nicely(1); |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
/* |
| 806 |
* Non-option argument specifies database name as long as it wasn't |
| 807 |
* already specified with -d / --dbname |
| 808 |
*/ |
| 809 |
if (optind < argc && dopt.cparams.dbname == NULL) |
| 810 |
dopt.cparams.dbname = argv[optind++]; |
| 811 |
|
| 812 |
/* Complain if any arguments remain */ |
| 813 |
if (optind < argc) |
| 814 |
{ |
| 815 |
pg_log_error("too many command-line arguments (first is \"%s\")", |
| 816 |
argv[optind]); |
| 817 |
pg_log_error_hint("Try \"%s --help\" for more information.", progname); |
| 818 |
exit_nicely(1); |
| 819 |
} |
| 820 |
|
| 821 |
/* --column-inserts implies --inserts */ |
| 822 |
if (dopt.column_inserts && dopt.dump_inserts == 0) |
| 823 |
dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT; |
| 824 |
|
| 825 |
/* reject conflicting "-only" options */ |
| 826 |
if (data_only && schema_only) |
| 827 |
pg_fatal("options %s and %s cannot be used together", |
| 828 |
"-s/--schema-only", "-a/--data-only"); |
| 829 |
if (schema_only && statistics_only) |
| 830 |
pg_fatal("options %s and %s cannot be used together", |
| 831 |
"-s/--schema-only", "--statistics-only"); |
| 832 |
if (data_only && statistics_only) |
| 833 |
pg_fatal("options %s and %s cannot be used together", |
| 834 |
"-a/--data-only", "--statistics-only"); |
| 835 |
|
| 836 |
/* reject conflicting "-only" and "no-" options */ |
| 837 |
if (data_only && no_data) |
| 838 |
pg_fatal("options %s and %s cannot be used together", |
| 839 |
"-a/--data-only", "--no-data"); |
| 840 |
if (schema_only && no_schema) |
| 841 |
pg_fatal("options %s and %s cannot be used together", |
| 842 |
"-s/--schema-only", "--no-schema"); |
| 843 |
if (statistics_only && no_statistics) |
| 844 |
pg_fatal("options %s and %s cannot be used together", |
| 845 |
"--statistics-only", "--no-statistics"); |
| 846 |
|
| 847 |
/* reject conflicting "no-" options */ |
| 848 |
if (with_statistics && no_statistics) |
| 849 |
pg_fatal("options %s and %s cannot be used together", |
| 850 |
"--statistics", "--no-statistics"); |
| 851 |
|
| 852 |
/* reject conflicting "-only" options */ |
| 853 |
if (data_only && with_statistics) |
| 854 |
pg_fatal("options %s and %s cannot be used together", |
| 855 |
"-a/--data-only", "--statistics"); |
| 856 |
if (schema_only && with_statistics) |
| 857 |
pg_fatal("options %s and %s cannot be used together", |
| 858 |
"-s/--schema-only", "--statistics"); |
| 859 |
|
| 860 |
if (schema_only && foreign_servers_include_patterns.head != NULL) |
| 861 |
pg_fatal("options %s and %s cannot be used together", |
| 862 |
"-s/--schema-only", "--include-foreign-data"); |
| 863 |
|
| 864 |
if (numWorkers > 1 && foreign_servers_include_patterns.head != NULL) |
| 865 |
pg_fatal("option %s is not supported with parallel backup", |
| 866 |
"--include-foreign-data"); |
| 867 |
|
| 868 |
if (data_only && dopt.outputClean) |
| 869 |
pg_fatal("options %s and %s cannot be used together", |
| 870 |
"-c/--clean", "-a/--data-only"); |
| 871 |
|
| 872 |
if (dopt.if_exists && !dopt.outputClean) |
| 873 |
pg_fatal("option %s requires option %s", |
| 874 |
"--if-exists", "-c/--clean"); |
| 875 |
|
| 876 |
/* |
| 877 |
* Set derivative flags. Ambiguous or nonsensical combinations, e.g. |
| 878 |
* "--schema-only --no-schema", will have already caused an error in one |
| 879 |
* of the checks above. |
| 880 |
*/ |
| 881 |
dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) || |
| 882 |
data_only) && !no_data; |
| 883 |
dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) || |
| 884 |
schema_only) && !no_schema; |
| 885 |
dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) || |
| 886 |
(statistics_only || with_statistics)) && !no_statistics; |
| 887 |
|
| 888 |
|
| 889 |
/* |
| 890 |
* --inserts are already implied above if --column-inserts or |
| 891 |
* --rows-per-insert were specified. |
| 892 |
*/ |
| 893 |
if (dopt.do_nothing && dopt.dump_inserts == 0) |
| 894 |
pg_fatal("option %s requires option %s, %s, or %s", |
| 895 |
"--on-conflict-do-nothing", |
| 896 |
"--inserts", "--rows-per-insert", "--column-inserts"); |
| 897 |
|
| 898 |
/* Identify archive format to emit */ |
| 899 |
archiveFormat = parseArchiveFormat(format, &archiveMode); |
| 900 |
|
| 901 |
/* archiveFormat specific setup */ |
| 902 |
if (archiveFormat == archNull) |
| 903 |
{ |
| 904 |
plainText = 1; |
| 905 |
|
| 906 |
/* |
| 907 |
* If you don't provide a restrict key, one will be appointed for you. |
| 908 |
*/ |
| 909 |
if (!dopt.restrict_key) |
| 910 |
dopt.restrict_key = generate_restrict_key(); |
| 911 |
if (!dopt.restrict_key) |
| 912 |
pg_fatal("could not generate restrict key"); |
| 913 |
if (!valid_restrict_key(dopt.restrict_key)) |
| 914 |
pg_fatal("invalid restrict key"); |
| 915 |
} |
| 916 |
else if (dopt.restrict_key) |
| 917 |
pg_fatal("option %s can only be used with %s", |
| 918 |
"--restrict-key", "--format=plain"); |
| 919 |
|
| 920 |
/* |
| 921 |
* Custom and directory formats are compressed by default with gzip when |
| 922 |
* available, not the others. If gzip is not available, no compression is |
| 923 |
* done by default. |
| 924 |
*/ |
| 925 |
if ((archiveFormat == archCustom || archiveFormat == archDirectory) && |
| 926 |
!user_compression_defined) |
| 927 |
{ |
| 928 |
#ifdef HAVE_LIBZ |
| 929 |
compression_algorithm_str = "gzip"; |
| 930 |
#else |
| 931 |
compression_algorithm_str = "none"; |
| 932 |
#endif |
| 933 |
} |
| 934 |
|
| 935 |
/* |
| 936 |
* Compression options |
| 937 |
*/ |
| 938 |
if (!parse_compress_algorithm(compression_algorithm_str, |
| 939 |
&compression_algorithm)) |
| 940 |
pg_fatal("unrecognized compression algorithm: \"%s\"", |
| 941 |
compression_algorithm_str); |
| 942 |
|
| 943 |
parse_compress_specification(compression_algorithm, compression_detail, |
| 944 |
&compression_spec); |
| 945 |
error_detail = validate_compress_specification(&compression_spec); |
| 946 |
if (error_detail != NULL) |
| 947 |
pg_fatal("invalid compression specification: %s", |
| 948 |
error_detail); |
| 949 |
|
| 950 |
error_detail = supports_compression(compression_spec); |
| 951 |
if (error_detail != NULL) |
| 952 |
pg_fatal("%s", error_detail); |
| 953 |
|
| 954 |
/* |
| 955 |
* Disable support for zstd workers for now - these are based on |
| 956 |
* threading, and it's unclear how it interacts with parallel dumps on |
| 957 |
* platforms where that relies on threads too (e.g. Windows). |
| 958 |
*/ |
| 959 |
if (compression_spec.options & PG_COMPRESSION_OPTION_WORKERS) |
| 960 |
pg_log_warning("compression option \"%s\" is not currently supported by pg_dump", |
| 961 |
"workers"); |
| 962 |
|
| 963 |
/* |
| 964 |
* If emitting an archive format, we always want to emit a DATABASE item, |
| 965 |
* in case --create is specified at pg_restore time. |
| 966 |
*/ |
| 967 |
if (!plainText) |
| 968 |
dopt.outputCreateDB = 1; |
| 969 |
|
| 970 |
/* Parallel backup only in the directory archive format so far */ |
| 971 |
if (archiveFormat != archDirectory && numWorkers > 1) |
| 972 |
pg_fatal("parallel backup only supported by the directory format"); |
| 973 |
|
| 974 |
/* Open the output file */ |
| 975 |
fout = CreateArchive(filename, archiveFormat, compression_spec, |
| 976 |
dosync, archiveMode, setupDumpWorker, sync_method); |
| 977 |
|
| 978 |
/* Make dump options accessible right away */ |
| 979 |
SetArchiveOptions(fout, &dopt, NULL); |
| 980 |
|
| 981 |
/* Register the cleanup hook */ |
| 982 |
on_exit_close_archive(fout); |
| 983 |
|
| 984 |
/* Let the archiver know how noisy to be */ |
| 985 |
fout->verbose = g_verbose; |
| 986 |
|
| 987 |
|
| 988 |
/* |
| 989 |
* We allow the server to be back to 9.2, and up to any minor release of |
| 990 |
* our own major version. (See also version check in pg_dumpall.c.) |
| 991 |
*/ |
| 992 |
fout->minRemoteVersion = 90200; |
| 993 |
fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99; |
| 994 |
|
| 995 |
fout->numWorkers = numWorkers; |
| 996 |
|
| 997 |
/* |
| 998 |
* Open the database using the Archiver, so it knows about it. Errors mean |
| 999 |
* death. |
| 1000 |
*/ |
| 1001 |
ConnectDatabaseAhx(fout, &dopt.cparams, false); |
| 1002 |
setup_connection(fout, dumpencoding, dumpsnapshot, use_role); |
| 1003 |
|
| 1004 |
/* |
| 1005 |
* On hot standbys, never try to dump unlogged table data, since it will |
| 1006 |
* just throw an error. |
| 1007 |
*/ |
| 1008 |
if (fout->isStandby) |
| 1009 |
dopt.no_unlogged_table_data = true; |
| 1010 |
|
| 1011 |
/* |
| 1012 |
* Find the last built-in OID, if needed (prior to 8.1) |
| 1013 |
* |
| 1014 |
* With 8.1 and above, we can just use FirstNormalObjectId - 1. |
| 1015 |
*/ |
| 1016 |
g_last_builtin_oid = FirstNormalObjectId - 1; |
| 1017 |
|
| 1018 |
pg_log_info("last built-in OID is %u", g_last_builtin_oid); |
| 1019 |
|
| 1020 |
/* Expand schema selection patterns into OID lists */ |
| 1021 |
if (schema_include_patterns.head != NULL) |
| 1022 |
{ |
| 1023 |
expand_schema_name_patterns(fout, &schema_include_patterns, |
| 1024 |
&schema_include_oids, |
| 1025 |
strict_names); |
| 1026 |
if (schema_include_oids.head == NULL) |
| 1027 |
pg_fatal("no matching schemas were found"); |
| 1028 |
} |
| 1029 |
expand_schema_name_patterns(fout, &schema_exclude_patterns, |
| 1030 |
&schema_exclude_oids, |
| 1031 |
false); |
| 1032 |
/* non-matching exclusion patterns aren't an error */ |
| 1033 |
|
| 1034 |
/* Expand table selection patterns into OID lists */ |
| 1035 |
expand_table_name_patterns(fout, &table_include_patterns, |
| 1036 |
&table_include_oids, |
| 1037 |
strict_names, false); |
| 1038 |
expand_table_name_patterns(fout, &table_include_patterns_and_children, |
| 1039 |
&table_include_oids, |
| 1040 |
strict_names, true); |
| 1041 |
if ((table_include_patterns.head != NULL || |
| 1042 |
table_include_patterns_and_children.head != NULL) && |
| 1043 |
table_include_oids.head == NULL) |
| 1044 |
pg_fatal("no matching tables were found"); |
| 1045 |
|
| 1046 |
expand_table_name_patterns(fout, &table_exclude_patterns, |
| 1047 |
&table_exclude_oids, |
| 1048 |
false, false); |
| 1049 |
expand_table_name_patterns(fout, &table_exclude_patterns_and_children, |
| 1050 |
&table_exclude_oids, |
| 1051 |
false, true); |
| 1052 |
|
| 1053 |
expand_table_name_patterns(fout, &tabledata_exclude_patterns, |
| 1054 |
&tabledata_exclude_oids, |
| 1055 |
false, false); |
| 1056 |
expand_table_name_patterns(fout, &tabledata_exclude_patterns_and_children, |
| 1057 |
&tabledata_exclude_oids, |
| 1058 |
false, true); |
| 1059 |
|
| 1060 |
expand_foreign_server_name_patterns(fout, &foreign_servers_include_patterns, |
| 1061 |
&foreign_servers_include_oids); |
| 1062 |
|
| 1063 |
/* non-matching exclusion patterns aren't an error */ |
| 1064 |
|
| 1065 |
/* Expand extension selection patterns into OID lists */ |
| 1066 |
if (extension_include_patterns.head != NULL) |
| 1067 |
{ |
| 1068 |
expand_extension_name_patterns(fout, &extension_include_patterns, |
| 1069 |
&extension_include_oids, |
| 1070 |
strict_names); |
| 1071 |
if (extension_include_oids.head == NULL) |
| 1072 |
pg_fatal("no matching extensions were found"); |
| 1073 |
} |
| 1074 |
expand_extension_name_patterns(fout, &extension_exclude_patterns, |
| 1075 |
&extension_exclude_oids, |
| 1076 |
false); |
| 1077 |
/* non-matching exclusion patterns aren't an error */ |
| 1078 |
|
| 1079 |
/* |
| 1080 |
* Dumping LOs is the default for dumps where an inclusion switch is not |
| 1081 |
* used (an "include everything" dump). -B can be used to exclude LOs |
| 1082 |
* from those dumps. -b can be used to include LOs even when an inclusion |
| 1083 |
* switch is used. |
| 1084 |
* |
| 1085 |
* -s means "schema only" and LOs are data, not schema, so we never |
| 1086 |
* include LOs when -s is used. |
| 1087 |
*/ |
| 1088 |
if (dopt.include_everything && dopt.dumpData && !dopt.dontOutputLOs) |
| 1089 |
dopt.outputLOs = true; |
| 1090 |
|
| 1091 |
/* |
| 1092 |
* Collect role names so we can map object owner OIDs to names. |
| 1093 |
*/ |
| 1094 |
collectRoleNames(fout); |
| 1095 |
|
| 1096 |
/* |
| 1097 |
* Now scan the database and create DumpableObject structs for all the |
| 1098 |
* objects we intend to dump. |
| 1099 |
*/ |
| 1100 |
tblinfo = getSchemaData(fout, &numTables); |
| 1101 |
|
| 1102 |
if (dopt.dumpData) |
| 1103 |
{ |
| 1104 |
getTableData(&dopt, tblinfo, numTables, 0); |
| 1105 |
buildMatViewRefreshDependencies(fout); |
| 1106 |
if (!dopt.dumpSchema) |
| 1107 |
getTableDataFKConstraints(); |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (!dopt.dumpData && dopt.sequence_data) |
| 1111 |
getTableData(&dopt, tblinfo, numTables, RELKIND_SEQUENCE); |
| 1112 |
|
| 1113 |
/* |
| 1114 |
* In binary-upgrade mode, we do not have to worry about the actual LO |
| 1115 |
* data or the associated metadata that resides in the pg_largeobject and |
| 1116 |
* pg_largeobject_metadata tables, respectively. |
| 1117 |
* |
| 1118 |
* However, we do need to collect LO information as there may be comments |
| 1119 |
* or other information on LOs that we do need to dump out. |
| 1120 |
*/ |
| 1121 |
if (dopt.outputLOs || dopt.binary_upgrade) |
| 1122 |
getLOs(fout); |
| 1123 |
|
| 1124 |
/* |
| 1125 |
* Collect dependency data to assist in ordering the objects. |
| 1126 |
*/ |
| 1127 |
getDependencies(fout); |
| 1128 |
|
| 1129 |
/* |
| 1130 |
* Collect ACLs, comments, and security labels, if wanted. |
| 1131 |
*/ |
| 1132 |
if (!dopt.aclsSkip) |
| 1133 |
getAdditionalACLs(fout); |
| 1134 |
if (!dopt.no_comments) |
| 1135 |
collectComments(fout); |
| 1136 |
if (!dopt.no_security_labels) |
| 1137 |
collectSecLabels(fout); |
| 1138 |
|
| 1139 |
/* For binary upgrade mode, collect required pg_class information. */ |
| 1140 |
if (dopt.binary_upgrade) |
| 1141 |
collectBinaryUpgradeClassOids(fout); |
| 1142 |
|
| 1143 |
/* Collect sequence information. */ |
| 1144 |
collectSequences(fout); |
| 1145 |
|
| 1146 |
/* Lastly, create dummy objects to represent the section boundaries */ |
| 1147 |
boundaryObjs = createBoundaryObjects(); |
| 1148 |
|
| 1149 |
/* Get pointers to all the known DumpableObjects */ |
| 1150 |
getDumpableObjects(&dobjs, &numObjs); |
| 1151 |
|
| 1152 |
/* |
| 1153 |
* Add dummy dependencies to enforce the dump section ordering. |
| 1154 |
*/ |
| 1155 |
addBoundaryDependencies(dobjs, numObjs, boundaryObjs); |
| 1156 |
|
| 1157 |
/* |
| 1158 |
* Sort the objects into a safe dump order (no forward references). |
| 1159 |
* |
| 1160 |
* We rely on dependency information to help us determine a safe order, so |
| 1161 |
* the initial sort is mostly for cosmetic purposes: we sort by name to |
| 1162 |
* ensure that logically identical schemas will dump identically. |
| 1163 |
*/ |
| 1164 |
sortDumpableObjectsByTypeName(dobjs, numObjs); |
| 1165 |
|
| 1166 |
sortDumpableObjects(dobjs, numObjs, |
| 1167 |
boundaryObjs[0].dumpId, boundaryObjs[1].dumpId); |
| 1168 |
|
| 1169 |
/* |
| 1170 |
* Create archive TOC entries for all the objects to be dumped, in a safe |
| 1171 |
* order. |
| 1172 |
*/ |
| 1173 |
|
| 1174 |
/* |
| 1175 |
* First the special entries for ENCODING, STDSTRINGS, and SEARCHPATH. |
| 1176 |
*/ |
| 1177 |
dumpEncoding(fout); |
| 1178 |
dumpStdStrings(fout); |
| 1179 |
dumpSearchPath(fout); |
| 1180 |
|
| 1181 |
/* The database items are always next, unless we don't want them at all */ |
| 1182 |
if (dopt.outputCreateDB) |
| 1183 |
dumpDatabase(fout); |
| 1184 |
|
| 1185 |
/* Now the rearrangeable objects. */ |
| 1186 |
for (i = 0; i < numObjs; i++) |
| 1187 |
dumpDumpableObject(fout, dobjs[i]); |
| 1188 |
|
| 1189 |
/* |
| 1190 |
* Set up options info to ensure we dump what we want. |
| 1191 |
*/ |
| 1192 |
ropt = NewRestoreOptions(); |
| 1193 |
ropt->filename = filename; |
| 1194 |
|
| 1195 |
/* if you change this list, see dumpOptionsFromRestoreOptions */ |
| 1196 |
ropt->cparams.dbname = dopt.cparams.dbname ? pg_strdup(dopt.cparams.dbname) : NULL; |
| 1197 |
ropt->cparams.pgport = dopt.cparams.pgport ? pg_strdup(dopt.cparams.pgport) : NULL; |
| 1198 |
ropt->cparams.pghost = dopt.cparams.pghost ? pg_strdup(dopt.cparams.pghost) : NULL; |
| 1199 |
ropt->cparams.username = dopt.cparams.username ? pg_strdup(dopt.cparams.username) : NULL; |
| 1200 |
ropt->cparams.promptPassword = dopt.cparams.promptPassword; |
| 1201 |
ropt->dropSchema = dopt.outputClean; |
| 1202 |
ropt->dumpData = dopt.dumpData; |
| 1203 |
ropt->dumpSchema = dopt.dumpSchema; |
| 1204 |
ropt->dumpStatistics = dopt.dumpStatistics; |
| 1205 |
ropt->if_exists = dopt.if_exists; |
| 1206 |
ropt->column_inserts = dopt.column_inserts; |
| 1207 |
ropt->dumpSections = dopt.dumpSections; |
| 1208 |
ropt->aclsSkip = dopt.aclsSkip; |
| 1209 |
ropt->superuser = dopt.outputSuperuser; |
| 1210 |
ropt->createDB = dopt.outputCreateDB; |
| 1211 |
ropt->noOwner = dopt.outputNoOwner; |
| 1212 |
ropt->noTableAm = dopt.outputNoTableAm; |
| 1213 |
ropt->noTablespace = dopt.outputNoTablespaces; |
| 1214 |
ropt->disable_triggers = dopt.disable_triggers; |
| 1215 |
ropt->use_setsessauth = dopt.use_setsessauth; |
| 1216 |
ropt->disable_dollar_quoting = dopt.disable_dollar_quoting; |
| 1217 |
ropt->dump_inserts = dopt.dump_inserts; |
| 1218 |
ropt->no_comments = dopt.no_comments; |
| 1219 |
ropt->no_policies = dopt.no_policies; |
| 1220 |
ropt->no_publications = dopt.no_publications; |
| 1221 |
ropt->no_security_labels = dopt.no_security_labels; |
| 1222 |
ropt->no_subscriptions = dopt.no_subscriptions; |
| 1223 |
ropt->lockWaitTimeout = dopt.lockWaitTimeout; |
| 1224 |
ropt->include_everything = dopt.include_everything; |
| 1225 |
ropt->enable_row_security = dopt.enable_row_security; |
| 1226 |
ropt->sequence_data = dopt.sequence_data; |
| 1227 |
ropt->binary_upgrade = dopt.binary_upgrade; |
| 1228 |
ropt->restrict_key = dopt.restrict_key ? pg_strdup(dopt.restrict_key) : NULL; |
| 1229 |
|
| 1230 |
ropt->compression_spec = compression_spec; |
| 1231 |
|
| 1232 |
ropt->suppressDumpWarnings = true; /* We've already shown them */ |
| 1233 |
|
| 1234 |
SetArchiveOptions(fout, &dopt, ropt); |
| 1235 |
|
| 1236 |
/* Mark which entries should be output */ |
| 1237 |
ProcessArchiveRestoreOptions(fout); |
| 1238 |
|
| 1239 |
/* |
| 1240 |
* The archive's TOC entries are now marked as to which ones will actually |
| 1241 |
* be output, so we can set up their dependency lists properly. This isn't |
| 1242 |
* necessary for plain-text output, though. |
| 1243 |
*/ |
| 1244 |
if (!plainText) |
| 1245 |
BuildArchiveDependencies(fout); |
| 1246 |
|
| 1247 |
/* |
| 1248 |
* And finally we can do the actual output. |
| 1249 |
* |
| 1250 |
* Note: for non-plain-text output formats, the output file is written |
| 1251 |
* inside CloseArchive(). This is, um, bizarre; but not worth changing |
| 1252 |
* right now. |
| 1253 |
*/ |
| 1254 |
if (plainText) |
| 1255 |
RestoreArchive(fout); |
| 1256 |
|
| 1257 |
CloseArchive(fout); |
| 1258 |
|
| 1259 |
exit_nicely(0); |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
static void |
| 1264 |
help(const char *progname) |
| 1265 |
{ |
| 1266 |
printf(_("%s exports a PostgreSQL database as an SQL script or to other formats.\n\n"), progname); |
| 1267 |
printf(_("Usage:\n")); |
| 1268 |
printf(_(" %s [OPTION]... [DBNAME]\n"), progname); |
| 1269 |
|
| 1270 |
printf(_("\nGeneral options:\n")); |
| 1271 |
printf(_(" -f, --file=FILENAME output file or directory name\n")); |
| 1272 |
printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n" |
| 1273 |
" plain text (default))\n")); |
| 1274 |
printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n")); |
| 1275 |
printf(_(" -v, --verbose verbose mode\n")); |
| 1276 |
printf(_(" -V, --version output version information, then exit\n")); |
| 1277 |
printf(_(" -Z, --compress=METHOD[:DETAIL]\n" |
| 1278 |
" compress as specified\n")); |
| 1279 |
printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n")); |
| 1280 |
printf(_(" --no-sync do not wait for changes to be written safely to disk\n")); |
| 1281 |
printf(_(" --sync-method=METHOD set method for syncing files to disk\n")); |
| 1282 |
printf(_(" -?, --help show this help, then exit\n")); |
| 1283 |
|
| 1284 |
printf(_("\nOptions controlling the output content:\n")); |
| 1285 |
printf(_(" -a, --data-only dump only the data, not the schema or statistics\n")); |
| 1286 |
printf(_(" -b, --large-objects include large objects in dump\n")); |
| 1287 |
printf(_(" --blobs (same as --large-objects, deprecated)\n")); |
| 1288 |
printf(_(" -B, --no-large-objects exclude large objects in dump\n")); |
| 1289 |
printf(_(" --no-blobs (same as --no-large-objects, deprecated)\n")); |
| 1290 |
printf(_(" -c, --clean clean (drop) database objects before recreating\n")); |
| 1291 |
printf(_(" -C, --create include commands to create database in dump\n")); |
| 1292 |
printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n")); |
| 1293 |
printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n")); |
| 1294 |
printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n")); |
| 1295 |
printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n")); |
| 1296 |
printf(_(" -O, --no-owner skip restoration of object ownership in\n" |
| 1297 |
" plain-text format\n")); |
| 1298 |
printf(_(" -s, --schema-only dump only the schema, no data or statistics\n")); |
| 1299 |
printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n")); |
| 1300 |
printf(_(" -t, --table=PATTERN dump only the specified table(s)\n")); |
| 1301 |
printf(_(" -T, --exclude-table=PATTERN do NOT dump the specified table(s)\n")); |
| 1302 |
printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n")); |
| 1303 |
printf(_(" --binary-upgrade for use by upgrade utilities only\n")); |
| 1304 |
printf(_(" --column-inserts dump data as INSERT commands with column names\n")); |
| 1305 |
printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n")); |
| 1306 |
printf(_(" --disable-triggers disable triggers during data-only restore\n")); |
| 1307 |
printf(_(" --enable-row-security enable row security (dump only content user has\n" |
| 1308 |
" access to)\n")); |
| 1309 |
printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n")); |
| 1310 |
printf(_(" --exclude-table-and-children=PATTERN\n" |
| 1311 |
" do NOT dump the specified table(s), including\n" |
| 1312 |
" child and partition tables\n")); |
| 1313 |
printf(_(" --exclude-table-data=PATTERN do NOT dump data for the specified table(s)\n")); |
| 1314 |
printf(_(" --exclude-table-data-and-children=PATTERN\n" |
| 1315 |
" do NOT dump data for the specified table(s),\n" |
| 1316 |
" including child and partition tables\n")); |
| 1317 |
printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n")); |
| 1318 |
printf(_(" --filter=FILENAME include or exclude objects and data from dump\n" |
| 1319 |
" based on expressions in FILENAME\n")); |
| 1320 |
printf(_(" --if-exists use IF EXISTS when dropping objects\n")); |
| 1321 |
printf(_(" --include-foreign-data=PATTERN\n" |
| 1322 |
" include data of foreign tables on foreign\n" |
| 1323 |
" servers matching PATTERN\n")); |
| 1324 |
printf(_(" --inserts dump data as INSERT commands, rather than COPY\n")); |
| 1325 |
printf(_(" --load-via-partition-root load partitions via the root table\n")); |
| 1326 |
printf(_(" --no-comments do not dump comment commands\n")); |
| 1327 |
printf(_(" --no-data do not dump data\n")); |
| 1328 |
printf(_(" --no-policies do not dump row security policies\n")); |
| 1329 |
printf(_(" --no-publications do not dump publications\n")); |
| 1330 |
printf(_(" --no-schema do not dump schema\n")); |
| 1331 |
printf(_(" --no-security-labels do not dump security label assignments\n")); |
| 1332 |
printf(_(" --no-statistics do not dump statistics\n")); |
| 1333 |
printf(_(" --no-subscriptions do not dump subscriptions\n")); |
| 1334 |
printf(_(" --no-table-access-method do not dump table access methods\n")); |
| 1335 |
printf(_(" --no-tablespaces do not dump tablespace assignments\n")); |
| 1336 |
printf(_(" --no-toast-compression do not dump TOAST compression methods\n")); |
| 1337 |
printf(_(" --no-unlogged-table-data do not dump unlogged table data\n")); |
| 1338 |
printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n")); |
| 1339 |
printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n")); |
| 1340 |
printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n")); |
| 1341 |
printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n")); |
| 1342 |
printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n")); |
| 1343 |
printf(_(" --sequence-data include sequence data in dump\n")); |
| 1344 |
printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n")); |
| 1345 |
printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n")); |
| 1346 |
printf(_(" --statistics dump the statistics\n")); |
| 1347 |
printf(_(" --statistics-only dump only the statistics, not schema or data\n")); |
| 1348 |
printf(_(" --strict-names require table and/or schema include patterns to\n" |
| 1349 |
" match at least one entity each\n")); |
| 1350 |
printf(_(" --table-and-children=PATTERN dump only the specified table(s), including\n" |
| 1351 |
" child and partition tables\n")); |
| 1352 |
printf(_(" --use-set-session-authorization\n" |
| 1353 |
" use SET SESSION AUTHORIZATION commands instead of\n" |
| 1354 |
" ALTER OWNER commands to set ownership\n")); |
| 1355 |
|
| 1356 |
printf(_("\nConnection options:\n")); |
| 1357 |
printf(_(" -d, --dbname=DBNAME database to dump\n")); |
| 1358 |
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); |
| 1359 |
printf(_(" -p, --port=PORT database server port number\n")); |
| 1360 |
printf(_(" -U, --username=NAME connect as specified database user\n")); |
| 1361 |
printf(_(" -w, --no-password never prompt for password\n")); |
| 1362 |
printf(_(" -W, --password force password prompt (should happen automatically)\n")); |
| 1363 |
printf(_(" --role=ROLENAME do SET ROLE before dump\n")); |
| 1364 |
|
| 1365 |
printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n" |
| 1366 |
"variable value is used.\n\n")); |
| 1367 |
printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT); |
| 1368 |
printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL); |
| 1369 |
} |
| 1370 |
|
| 1371 |
static void |
| 1372 |
setup_connection(Archive *AH, const char *dumpencoding, |
| 1373 |
const char *dumpsnapshot, char *use_role) |
| 1374 |
{ |
| 1375 |
DumpOptions *dopt = AH->dopt; |
| 1376 |
PGconn *conn = GetConnection(AH); |
| 1377 |
const char *std_strings; |
| 1378 |
|
| 1379 |
PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL)); |
| 1380 |
|
| 1381 |
/* |
| 1382 |
* Set the client encoding if requested. |
| 1383 |
*/ |
| 1384 |
if (dumpencoding) |
| 1385 |
{ |
| 1386 |
if (PQsetClientEncoding(conn, dumpencoding) < 0) |
| 1387 |
pg_fatal("invalid client encoding \"%s\" specified", |
| 1388 |
dumpencoding); |
| 1389 |
} |
| 1390 |
|
| 1391 |
/* |
| 1392 |
* Get the active encoding and the standard_conforming_strings setting, so |
| 1393 |
* we know how to escape strings. |
| 1394 |
*/ |
| 1395 |
AH->encoding = PQclientEncoding(conn); |
| 1396 |
setFmtEncoding(AH->encoding); |
| 1397 |
|
| 1398 |
std_strings = PQparameterStatus(conn, "standard_conforming_strings"); |
| 1399 |
AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0); |
| 1400 |
|
| 1401 |
/* |
| 1402 |
* Set the role if requested. In a parallel dump worker, we'll be passed |
| 1403 |
* use_role == NULL, but AH->use_role is already set (if user specified it |
| 1404 |
* originally) and we should use that. |
| 1405 |
*/ |
| 1406 |
if (!use_role && AH->use_role) |
| 1407 |
use_role = AH->use_role; |
| 1408 |
|
| 1409 |
/* Set the role if requested */ |
| 1410 |
if (use_role) |
| 1411 |
{ |
| 1412 |
PQExpBuffer query = createPQExpBuffer(); |
| 1413 |
|
| 1414 |
appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role)); |
| 1415 |
ExecuteSqlStatement(AH, query->data); |
| 1416 |
destroyPQExpBuffer(query); |
| 1417 |
|
| 1418 |
/* save it for possible later use by parallel workers */ |
| 1419 |
if (!AH->use_role) |
| 1420 |
AH->use_role = pg_strdup(use_role); |
| 1421 |
} |
| 1422 |
|
| 1423 |
/* Set the datestyle to ISO to ensure the dump's portability */ |
| 1424 |
ExecuteSqlStatement(AH, "SET DATESTYLE = ISO"); |
| 1425 |
|
| 1426 |
/* Likewise, avoid using sql_standard intervalstyle */ |
| 1427 |
ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES"); |
| 1428 |
|
| 1429 |
/* |
| 1430 |
* Use an explicitly specified extra_float_digits if it has been provided. |
| 1431 |
* Otherwise, set extra_float_digits so that we can dump float data |
| 1432 |
* exactly (given correctly implemented float I/O code, anyway). |
| 1433 |
*/ |
| 1434 |
if (have_extra_float_digits) |
| 1435 |
{ |
| 1436 |
PQExpBuffer q = createPQExpBuffer(); |
| 1437 |
|
| 1438 |
appendPQExpBuffer(q, "SET extra_float_digits TO %d", |
| 1439 |
extra_float_digits); |
| 1440 |
ExecuteSqlStatement(AH, q->data); |
| 1441 |
destroyPQExpBuffer(q); |
| 1442 |
} |
| 1443 |
else |
| 1444 |
ExecuteSqlStatement(AH, "SET extra_float_digits TO 3"); |
| 1445 |
|
| 1446 |
/* |
| 1447 |
* Disable synchronized scanning, to prevent unpredictable changes in row |
| 1448 |
* ordering across a dump and reload. |
| 1449 |
*/ |
| 1450 |
ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off"); |
| 1451 |
|
| 1452 |
/* |
| 1453 |
* Disable timeouts if supported. |
| 1454 |
*/ |
| 1455 |
ExecuteSqlStatement(AH, "SET statement_timeout = 0"); |
| 1456 |
if (AH->remoteVersion >= 90300) |
| 1457 |
ExecuteSqlStatement(AH, "SET lock_timeout = 0"); |
| 1458 |
if (AH->remoteVersion >= 90600) |
| 1459 |
ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0"); |
| 1460 |
if (AH->remoteVersion >= 170000) |
| 1461 |
ExecuteSqlStatement(AH, "SET transaction_timeout = 0"); |
| 1462 |
|
| 1463 |
/* |
| 1464 |
* Quote all identifiers, if requested. |
| 1465 |
*/ |
| 1466 |
if (quote_all_identifiers) |
| 1467 |
ExecuteSqlStatement(AH, "SET quote_all_identifiers = true"); |
| 1468 |
|
| 1469 |
/* |
| 1470 |
* Adjust row-security mode, if supported. |
| 1471 |
*/ |
| 1472 |
if (AH->remoteVersion >= 90500) |
| 1473 |
{ |
| 1474 |
if (dopt->enable_row_security) |
| 1475 |
ExecuteSqlStatement(AH, "SET row_security = on"); |
| 1476 |
else |
| 1477 |
ExecuteSqlStatement(AH, "SET row_security = off"); |
| 1478 |
} |
| 1479 |
|
| 1480 |
/* |
| 1481 |
* For security reasons, we restrict the expansion of non-system views and |
| 1482 |
* access to foreign tables during the pg_dump process. This restriction |
| 1483 |
* is adjusted when dumping foreign table data. |
| 1484 |
*/ |
| 1485 |
set_restrict_relation_kind(AH, "view, foreign-table"); |
| 1486 |
|
| 1487 |
/* |
| 1488 |
* Initialize prepared-query state to "nothing prepared". We do this here |
| 1489 |
* so that a parallel dump worker will have its own state. |
| 1490 |
*/ |
| 1491 |
AH->is_prepared = (bool *) pg_malloc0(NUM_PREP_QUERIES * sizeof(bool)); |
| 1492 |
|
| 1493 |
/* |
| 1494 |
* Start transaction-snapshot mode transaction to dump consistent data. |
| 1495 |
*/ |
| 1496 |
ExecuteSqlStatement(AH, "BEGIN"); |
| 1497 |
|
| 1498 |
/* |
| 1499 |
* To support the combination of serializable_deferrable with the jobs |
| 1500 |
* option we use REPEATABLE READ for the worker connections that are |
| 1501 |
* passed a snapshot. As long as the snapshot is acquired in a |
| 1502 |
* SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a |
| 1503 |
* REPEATABLE READ transaction provides the appropriate integrity |
| 1504 |
* guarantees. This is a kluge, but safe for back-patching. |
| 1505 |
*/ |
| 1506 |
if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL) |
| 1507 |
ExecuteSqlStatement(AH, |
| 1508 |
"SET TRANSACTION ISOLATION LEVEL " |
| 1509 |
"SERIALIZABLE, READ ONLY, DEFERRABLE"); |
| 1510 |
else |
| 1511 |
ExecuteSqlStatement(AH, |
| 1512 |
"SET TRANSACTION ISOLATION LEVEL " |
| 1513 |
"REPEATABLE READ, READ ONLY"); |
| 1514 |
|
| 1515 |
/* |
| 1516 |
* If user specified a snapshot to use, select that. In a parallel dump |
| 1517 |
* worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id |
| 1518 |
* is already set (if the server can handle it) and we should use that. |
| 1519 |
*/ |
| 1520 |
if (dumpsnapshot) |
| 1521 |
AH->sync_snapshot_id = pg_strdup(dumpsnapshot); |
| 1522 |
|
| 1523 |
if (AH->sync_snapshot_id) |
| 1524 |
{ |
| 1525 |
PQExpBuffer query = createPQExpBuffer(); |
| 1526 |
|
| 1527 |
appendPQExpBufferStr(query, "SET TRANSACTION SNAPSHOT "); |
| 1528 |
appendStringLiteralConn(query, AH->sync_snapshot_id, conn); |
| 1529 |
ExecuteSqlStatement(AH, query->data); |
| 1530 |
destroyPQExpBuffer(query); |
| 1531 |
} |
| 1532 |
else if (AH->numWorkers > 1) |
| 1533 |
{ |
| 1534 |
if (AH->isStandby && AH->remoteVersion < 100000) |
| 1535 |
pg_fatal("parallel dumps from standby servers are not supported by this server version"); |
| 1536 |
AH->sync_snapshot_id = get_synchronized_snapshot(AH); |
| 1537 |
} |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* Set up connection for a parallel worker process */ |
| 1541 |
static void |
| 1542 |
setupDumpWorker(Archive *AH) |
| 1543 |
{ |
| 1544 |
/* |
| 1545 |
* We want to re-select all the same values the leader connection is |
| 1546 |
* using. We'll have inherited directly-usable values in |
| 1547 |
* AH->sync_snapshot_id and AH->use_role, but we need to translate the |
| 1548 |
* inherited encoding value back to a string to pass to setup_connection. |
| 1549 |
*/ |
| 1550 |
setup_connection(AH, |
| 1551 |
pg_encoding_to_char(AH->encoding), |
| 1552 |
NULL, |
| 1553 |
NULL); |
| 1554 |
} |
| 1555 |
|
| 1556 |
static char * |
| 1557 |
get_synchronized_snapshot(Archive *fout) |
| 1558 |
{ |
| 1559 |
char *query = "SELECT pg_catalog.pg_export_snapshot()"; |
| 1560 |
char *result; |
| 1561 |
PGresult *res; |
| 1562 |
|
| 1563 |
res = ExecuteSqlQueryForSingleRow(fout, query); |
| 1564 |
result = pg_strdup(PQgetvalue(res, 0, 0)); |
| 1565 |
PQclear(res); |
| 1566 |
|
| 1567 |
return result; |
| 1568 |
} |
| 1569 |
|
| 1570 |
static ArchiveFormat |
| 1571 |
parseArchiveFormat(const char *format, ArchiveMode *mode) |
| 1572 |
{ |
| 1573 |
ArchiveFormat archiveFormat; |
| 1574 |
|
| 1575 |
*mode = archModeWrite; |
| 1576 |
|
| 1577 |
if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0) |
| 1578 |
{ |
| 1579 |
/* This is used by pg_dumpall, and is not documented */ |
| 1580 |
archiveFormat = archNull; |
| 1581 |
*mode = archModeAppend; |
| 1582 |
} |
| 1583 |
else if (pg_strcasecmp(format, "c") == 0) |
| 1584 |
archiveFormat = archCustom; |
| 1585 |
else if (pg_strcasecmp(format, "custom") == 0) |
| 1586 |
archiveFormat = archCustom; |
| 1587 |
else if (pg_strcasecmp(format, "d") == 0) |
| 1588 |
archiveFormat = archDirectory; |
| 1589 |
else if (pg_strcasecmp(format, "directory") == 0) |
| 1590 |
archiveFormat = archDirectory; |
| 1591 |
else if (pg_strcasecmp(format, "p") == 0) |
| 1592 |
archiveFormat = archNull; |
| 1593 |
else if (pg_strcasecmp(format, "plain") == 0) |
| 1594 |
archiveFormat = archNull; |
| 1595 |
else if (pg_strcasecmp(format, "t") == 0) |
| 1596 |
archiveFormat = archTar; |
| 1597 |
else if (pg_strcasecmp(format, "tar") == 0) |
| 1598 |
archiveFormat = archTar; |
| 1599 |
else |
| 1600 |
pg_fatal("invalid output format \"%s\" specified", format); |
| 1601 |
return archiveFormat; |
| 1602 |
} |
| 1603 |
|
| 1604 |
/* |
| 1605 |
* Find the OIDs of all schemas matching the given list of patterns, |
| 1606 |
* and append them to the given OID list. |
| 1607 |
*/ |
| 1608 |
static void |
| 1609 |
expand_schema_name_patterns(Archive *fout, |
| 1610 |
SimpleStringList *patterns, |
| 1611 |
SimpleOidList *oids, |
| 1612 |
bool strict_names) |
| 1613 |
{ |
| 1614 |
PQExpBuffer query; |
| 1615 |
PGresult *res; |
| 1616 |
SimpleStringListCell *cell; |
| 1617 |
int i; |
| 1618 |
|
| 1619 |
if (patterns->head == NULL) |
| 1620 |
return; /* nothing to do */ |
| 1621 |
|
| 1622 |
query = createPQExpBuffer(); |
| 1623 |
|
| 1624 |
/* |
| 1625 |
* The loop below runs multiple SELECTs might sometimes result in |
| 1626 |
* duplicate entries in the OID list, but we don't care. |
| 1627 |
*/ |
| 1628 |
|
| 1629 |
for (cell = patterns->head; cell; cell = cell->next) |
| 1630 |
{ |
| 1631 |
PQExpBufferData dbbuf; |
| 1632 |
int dotcnt; |
| 1633 |
|
| 1634 |
appendPQExpBufferStr(query, |
| 1635 |
"SELECT oid FROM pg_catalog.pg_namespace n\n"); |
| 1636 |
initPQExpBuffer(&dbbuf); |
| 1637 |
processSQLNamePattern(GetConnection(fout), query, cell->val, false, |
| 1638 |
false, NULL, "n.nspname", NULL, NULL, &dbbuf, |
| 1639 |
&dotcnt); |
| 1640 |
if (dotcnt > 1) |
| 1641 |
pg_fatal("improper qualified name (too many dotted names): %s", |
| 1642 |
cell->val); |
| 1643 |
else if (dotcnt == 1) |
| 1644 |
prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val); |
| 1645 |
termPQExpBuffer(&dbbuf); |
| 1646 |
|
| 1647 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 1648 |
if (strict_names && PQntuples(res) == 0) |
| 1649 |
pg_fatal("no matching schemas were found for pattern \"%s\"", cell->val); |
| 1650 |
|
| 1651 |
for (i = 0; i < PQntuples(res); i++) |
| 1652 |
{ |
| 1653 |
simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); |
| 1654 |
} |
| 1655 |
|
| 1656 |
PQclear(res); |
| 1657 |
resetPQExpBuffer(query); |
| 1658 |
} |
| 1659 |
|
| 1660 |
destroyPQExpBuffer(query); |
| 1661 |
} |
| 1662 |
|
| 1663 |
/* |
| 1664 |
* Find the OIDs of all extensions matching the given list of patterns, |
| 1665 |
* and append them to the given OID list. |
| 1666 |
*/ |
| 1667 |
static void |
| 1668 |
expand_extension_name_patterns(Archive *fout, |
| 1669 |
SimpleStringList *patterns, |
| 1670 |
SimpleOidList *oids, |
| 1671 |
bool strict_names) |
| 1672 |
{ |
| 1673 |
PQExpBuffer query; |
| 1674 |
PGresult *res; |
| 1675 |
SimpleStringListCell *cell; |
| 1676 |
int i; |
| 1677 |
|
| 1678 |
if (patterns->head == NULL) |
| 1679 |
return; /* nothing to do */ |
| 1680 |
|
| 1681 |
query = createPQExpBuffer(); |
| 1682 |
|
| 1683 |
/* |
| 1684 |
* The loop below runs multiple SELECTs might sometimes result in |
| 1685 |
* duplicate entries in the OID list, but we don't care. |
| 1686 |
*/ |
| 1687 |
for (cell = patterns->head; cell; cell = cell->next) |
| 1688 |
{ |
| 1689 |
int dotcnt; |
| 1690 |
|
| 1691 |
appendPQExpBufferStr(query, |
| 1692 |
"SELECT oid FROM pg_catalog.pg_extension e\n"); |
| 1693 |
processSQLNamePattern(GetConnection(fout), query, cell->val, false, |
| 1694 |
false, NULL, "e.extname", NULL, NULL, NULL, |
| 1695 |
&dotcnt); |
| 1696 |
if (dotcnt > 0) |
| 1697 |
pg_fatal("improper qualified name (too many dotted names): %s", |
| 1698 |
cell->val); |
| 1699 |
|
| 1700 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 1701 |
if (strict_names && PQntuples(res) == 0) |
| 1702 |
pg_fatal("no matching extensions were found for pattern \"%s\"", cell->val); |
| 1703 |
|
| 1704 |
for (i = 0; i < PQntuples(res); i++) |
| 1705 |
{ |
| 1706 |
simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); |
| 1707 |
} |
| 1708 |
|
| 1709 |
PQclear(res); |
| 1710 |
resetPQExpBuffer(query); |
| 1711 |
} |
| 1712 |
|
| 1713 |
destroyPQExpBuffer(query); |
| 1714 |
} |
| 1715 |
|
| 1716 |
/* |
| 1717 |
* Find the OIDs of all foreign servers matching the given list of patterns, |
| 1718 |
* and append them to the given OID list. |
| 1719 |
*/ |
| 1720 |
static void |
| 1721 |
expand_foreign_server_name_patterns(Archive *fout, |
| 1722 |
SimpleStringList *patterns, |
| 1723 |
SimpleOidList *oids) |
| 1724 |
{ |
| 1725 |
PQExpBuffer query; |
| 1726 |
PGresult *res; |
| 1727 |
SimpleStringListCell *cell; |
| 1728 |
int i; |
| 1729 |
|
| 1730 |
if (patterns->head == NULL) |
| 1731 |
return; /* nothing to do */ |
| 1732 |
|
| 1733 |
query = createPQExpBuffer(); |
| 1734 |
|
| 1735 |
/* |
| 1736 |
* The loop below runs multiple SELECTs might sometimes result in |
| 1737 |
* duplicate entries in the OID list, but we don't care. |
| 1738 |
*/ |
| 1739 |
|
| 1740 |
for (cell = patterns->head; cell; cell = cell->next) |
| 1741 |
{ |
| 1742 |
int dotcnt; |
| 1743 |
|
| 1744 |
appendPQExpBufferStr(query, |
| 1745 |
"SELECT oid FROM pg_catalog.pg_foreign_server s\n"); |
| 1746 |
processSQLNamePattern(GetConnection(fout), query, cell->val, false, |
| 1747 |
false, NULL, "s.srvname", NULL, NULL, NULL, |
| 1748 |
&dotcnt); |
| 1749 |
if (dotcnt > 0) |
| 1750 |
pg_fatal("improper qualified name (too many dotted names): %s", |
| 1751 |
cell->val); |
| 1752 |
|
| 1753 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 1754 |
if (PQntuples(res) == 0) |
| 1755 |
pg_fatal("no matching foreign servers were found for pattern \"%s\"", cell->val); |
| 1756 |
|
| 1757 |
for (i = 0; i < PQntuples(res); i++) |
| 1758 |
simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); |
| 1759 |
|
| 1760 |
PQclear(res); |
| 1761 |
resetPQExpBuffer(query); |
| 1762 |
} |
| 1763 |
|
| 1764 |
destroyPQExpBuffer(query); |
| 1765 |
} |
| 1766 |
|
| 1767 |
/* |
| 1768 |
* Find the OIDs of all tables matching the given list of patterns, |
| 1769 |
* and append them to the given OID list. See also expand_dbname_patterns() |
| 1770 |
* in pg_dumpall.c |
| 1771 |
*/ |
| 1772 |
static void |
| 1773 |
expand_table_name_patterns(Archive *fout, |
| 1774 |
SimpleStringList *patterns, SimpleOidList *oids, |
| 1775 |
bool strict_names, bool with_child_tables) |
| 1776 |
{ |
| 1777 |
PQExpBuffer query; |
| 1778 |
PGresult *res; |
| 1779 |
SimpleStringListCell *cell; |
| 1780 |
int i; |
| 1781 |
|
| 1782 |
if (patterns->head == NULL) |
| 1783 |
return; /* nothing to do */ |
| 1784 |
|
| 1785 |
query = createPQExpBuffer(); |
| 1786 |
|
| 1787 |
/* |
| 1788 |
* this might sometimes result in duplicate entries in the OID list, but |
| 1789 |
* we don't care. |
| 1790 |
*/ |
| 1791 |
|
| 1792 |
for (cell = patterns->head; cell; cell = cell->next) |
| 1793 |
{ |
| 1794 |
PQExpBufferData dbbuf; |
| 1795 |
int dotcnt; |
| 1796 |
|
| 1797 |
/* |
| 1798 |
* Query must remain ABSOLUTELY devoid of unqualified names. This |
| 1799 |
* would be unnecessary given a pg_table_is_visible() variant taking a |
| 1800 |
* search_path argument. |
| 1801 |
* |
| 1802 |
* For with_child_tables, we start with the basic query's results and |
| 1803 |
* recursively search the inheritance tree to add child tables. |
| 1804 |
*/ |
| 1805 |
if (with_child_tables) |
| 1806 |
{ |
| 1807 |
appendPQExpBufferStr(query, "WITH RECURSIVE partition_tree (relid) AS (\n"); |
| 1808 |
} |
| 1809 |
|
| 1810 |
appendPQExpBuffer(query, |
| 1811 |
"SELECT c.oid" |
| 1812 |
"\nFROM pg_catalog.pg_class c" |
| 1813 |
"\n LEFT JOIN pg_catalog.pg_namespace n" |
| 1814 |
"\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace" |
| 1815 |
"\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY" |
| 1816 |
"\n (array['%c', '%c', '%c', '%c', '%c', '%c'])\n", |
| 1817 |
RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW, |
| 1818 |
RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE, |
| 1819 |
RELKIND_PARTITIONED_TABLE); |
| 1820 |
initPQExpBuffer(&dbbuf); |
| 1821 |
processSQLNamePattern(GetConnection(fout), query, cell->val, true, |
| 1822 |
false, "n.nspname", "c.relname", NULL, |
| 1823 |
"pg_catalog.pg_table_is_visible(c.oid)", &dbbuf, |
| 1824 |
&dotcnt); |
| 1825 |
if (dotcnt > 2) |
| 1826 |
pg_fatal("improper relation name (too many dotted names): %s", |
| 1827 |
cell->val); |
| 1828 |
else if (dotcnt == 2) |
| 1829 |
prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val); |
| 1830 |
termPQExpBuffer(&dbbuf); |
| 1831 |
|
| 1832 |
if (with_child_tables) |
| 1833 |
{ |
| 1834 |
appendPQExpBufferStr(query, "UNION" |
| 1835 |
"\nSELECT i.inhrelid" |
| 1836 |
"\nFROM partition_tree p" |
| 1837 |
"\n JOIN pg_catalog.pg_inherits i" |
| 1838 |
"\n ON p.relid OPERATOR(pg_catalog.=) i.inhparent" |
| 1839 |
"\n)" |
| 1840 |
"\nSELECT relid FROM partition_tree"); |
| 1841 |
} |
| 1842 |
|
| 1843 |
ExecuteSqlStatement(fout, "RESET search_path"); |
| 1844 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 1845 |
PQclear(ExecuteSqlQueryForSingleRow(fout, |
| 1846 |
ALWAYS_SECURE_SEARCH_PATH_SQL)); |
| 1847 |
if (strict_names && PQntuples(res) == 0) |
| 1848 |
pg_fatal("no matching tables were found for pattern \"%s\"", cell->val); |
| 1849 |
|
| 1850 |
for (i = 0; i < PQntuples(res); i++) |
| 1851 |
{ |
| 1852 |
simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); |
| 1853 |
} |
| 1854 |
|
| 1855 |
PQclear(res); |
| 1856 |
resetPQExpBuffer(query); |
| 1857 |
} |
| 1858 |
|
| 1859 |
destroyPQExpBuffer(query); |
| 1860 |
} |
| 1861 |
|
| 1862 |
/* |
| 1863 |
* Verifies that the connected database name matches the given database name, |
| 1864 |
* and if not, dies with an error about the given pattern. |
| 1865 |
* |
| 1866 |
* The 'dbname' argument should be a literal name parsed from 'pattern'. |
| 1867 |
*/ |
| 1868 |
static void |
| 1869 |
prohibit_crossdb_refs(PGconn *conn, const char *dbname, const char *pattern) |
| 1870 |
{ |
| 1871 |
const char *db; |
| 1872 |
|
| 1873 |
db = PQdb(conn); |
| 1874 |
if (db == NULL) |
| 1875 |
pg_fatal("You are currently not connected to a database."); |
| 1876 |
|
| 1877 |
if (strcmp(db, dbname) != 0) |
| 1878 |
pg_fatal("cross-database references are not implemented: %s", |
| 1879 |
pattern); |
| 1880 |
} |
| 1881 |
|
| 1882 |
/* |
| 1883 |
* checkExtensionMembership |
| 1884 |
* Determine whether object is an extension member, and if so, |
| 1885 |
* record an appropriate dependency and set the object's dump flag. |
| 1886 |
* |
| 1887 |
* It's important to call this for each object that could be an extension |
| 1888 |
* member. Generally, we integrate this with determining the object's |
| 1889 |
* to-be-dumped-ness, since extension membership overrides other rules for that. |
| 1890 |
* |
| 1891 |
* Returns true if object is an extension member, else false. |
| 1892 |
*/ |
| 1893 |
static bool |
| 1894 |
checkExtensionMembership(DumpableObject *dobj, Archive *fout) |
| 1895 |
{ |
| 1896 |
ExtensionInfo *ext = findOwningExtension(dobj->catId); |
| 1897 |
|
| 1898 |
if (ext == NULL) |
| 1899 |
return false; |
| 1900 |
|
| 1901 |
dobj->ext_member = true; |
| 1902 |
|
| 1903 |
/* Record dependency so that getDependencies needn't deal with that */ |
| 1904 |
addObjectDependency(dobj, ext->dobj.dumpId); |
| 1905 |
|
| 1906 |
/* |
| 1907 |
* In 9.6 and above, mark the member object to have any non-initial ACLs |
| 1908 |
* dumped. (Any initial ACLs will be removed later, using data from |
| 1909 |
* pg_init_privs, so that we'll dump only the delta from the extension's |
| 1910 |
* initial setup.) |
| 1911 |
* |
| 1912 |
* Prior to 9.6, we do not include any extension member components. |
| 1913 |
* |
| 1914 |
* In binary upgrades, we still dump all components of the members |
| 1915 |
* individually, since the idea is to exactly reproduce the database |
| 1916 |
* contents rather than replace the extension contents with something |
| 1917 |
* different. |
| 1918 |
* |
| 1919 |
* Note: it might be interesting someday to implement storage and delta |
| 1920 |
* dumping of extension members' RLS policies and/or security labels. |
| 1921 |
* However there is a pitfall for RLS policies: trying to dump them |
| 1922 |
* requires getting a lock on their tables, and the calling user might not |
| 1923 |
* have privileges for that. We need no lock to examine a table's ACLs, |
| 1924 |
* so the current feature doesn't have a problem of that sort. |
| 1925 |
*/ |
| 1926 |
if (fout->dopt->binary_upgrade) |
| 1927 |
dobj->dump = ext->dobj.dump; |
| 1928 |
else |
| 1929 |
{ |
| 1930 |
if (fout->remoteVersion < 90600) |
| 1931 |
dobj->dump = DUMP_COMPONENT_NONE; |
| 1932 |
else |
| 1933 |
dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL); |
| 1934 |
} |
| 1935 |
|
| 1936 |
return true; |
| 1937 |
} |
| 1938 |
|
| 1939 |
/* |
| 1940 |
* selectDumpableNamespace: policy-setting subroutine |
| 1941 |
* Mark a namespace as to be dumped or not |
| 1942 |
*/ |
| 1943 |
static void |
| 1944 |
selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout) |
| 1945 |
{ |
| 1946 |
/* |
| 1947 |
* DUMP_COMPONENT_DEFINITION typically implies a CREATE SCHEMA statement |
| 1948 |
* and (for --clean) a DROP SCHEMA statement. (In the absence of |
| 1949 |
* DUMP_COMPONENT_DEFINITION, this value is irrelevant.) |
| 1950 |
*/ |
| 1951 |
nsinfo->create = true; |
| 1952 |
|
| 1953 |
/* |
| 1954 |
* If specific tables are being dumped, do not dump any complete |
| 1955 |
* namespaces. If specific namespaces are being dumped, dump just those |
| 1956 |
* namespaces. Otherwise, dump all non-system namespaces. |
| 1957 |
*/ |
| 1958 |
if (table_include_oids.head != NULL) |
| 1959 |
nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE; |
| 1960 |
else if (schema_include_oids.head != NULL) |
| 1961 |
nsinfo->dobj.dump_contains = nsinfo->dobj.dump = |
| 1962 |
simple_oid_list_member(&schema_include_oids, |
| 1963 |
nsinfo->dobj.catId.oid) ? |
| 1964 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 1965 |
else if (fout->remoteVersion >= 90600 && |
| 1966 |
strcmp(nsinfo->dobj.name, "pg_catalog") == 0) |
| 1967 |
{ |
| 1968 |
/* |
| 1969 |
* In 9.6 and above, we dump out any ACLs defined in pg_catalog, if |
| 1970 |
* they are interesting (and not the original ACLs which were set at |
| 1971 |
* initdb time, see pg_init_privs). |
| 1972 |
*/ |
| 1973 |
nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL; |
| 1974 |
} |
| 1975 |
else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 || |
| 1976 |
strcmp(nsinfo->dobj.name, "information_schema") == 0) |
| 1977 |
{ |
| 1978 |
/* Other system schemas don't get dumped */ |
| 1979 |
nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE; |
| 1980 |
} |
| 1981 |
else if (strcmp(nsinfo->dobj.name, "public") == 0) |
| 1982 |
{ |
| 1983 |
/* |
| 1984 |
* The public schema is a strange beast that sits in a sort of |
| 1985 |
* no-mans-land between being a system object and a user object. |
| 1986 |
* CREATE SCHEMA would fail, so its DUMP_COMPONENT_DEFINITION is just |
| 1987 |
* a comment and an indication of ownership. If the owner is the |
| 1988 |
* default, omit that superfluous DUMP_COMPONENT_DEFINITION. Before |
| 1989 |
* v15, the default owner was BOOTSTRAP_SUPERUSERID. |
| 1990 |
*/ |
| 1991 |
nsinfo->create = false; |
| 1992 |
nsinfo->dobj.dump = DUMP_COMPONENT_ALL; |
| 1993 |
if (nsinfo->nspowner == ROLE_PG_DATABASE_OWNER) |
| 1994 |
nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION; |
| 1995 |
nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL; |
| 1996 |
|
| 1997 |
/* |
| 1998 |
* Also, make like it has a comment even if it doesn't; this is so |
| 1999 |
* that we'll emit a command to drop the comment, if appropriate. |
| 2000 |
* (Without this, we'd not call dumpCommentExtended for it.) |
| 2001 |
*/ |
| 2002 |
nsinfo->dobj.components |= DUMP_COMPONENT_COMMENT; |
| 2003 |
} |
| 2004 |
else |
| 2005 |
nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL; |
| 2006 |
|
| 2007 |
/* |
| 2008 |
* In any case, a namespace can be excluded by an exclusion switch |
| 2009 |
*/ |
| 2010 |
if (nsinfo->dobj.dump_contains && |
| 2011 |
simple_oid_list_member(&schema_exclude_oids, |
| 2012 |
nsinfo->dobj.catId.oid)) |
| 2013 |
nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE; |
| 2014 |
|
| 2015 |
/* |
| 2016 |
* If the schema belongs to an extension, allow extension membership to |
| 2017 |
* override the dump decision for the schema itself. However, this does |
| 2018 |
* not change dump_contains, so this won't change what we do with objects |
| 2019 |
* within the schema. (If they belong to the extension, they'll get |
| 2020 |
* suppressed by it, otherwise not.) |
| 2021 |
*/ |
| 2022 |
(void) checkExtensionMembership(&nsinfo->dobj, fout); |
| 2023 |
} |
| 2024 |
|
| 2025 |
/* |
| 2026 |
* selectDumpableTable: policy-setting subroutine |
| 2027 |
* Mark a table as to be dumped or not |
| 2028 |
*/ |
| 2029 |
static void |
| 2030 |
selectDumpableTable(TableInfo *tbinfo, Archive *fout) |
| 2031 |
{ |
| 2032 |
if (checkExtensionMembership(&tbinfo->dobj, fout)) |
| 2033 |
return; /* extension membership overrides all else */ |
| 2034 |
|
| 2035 |
/* |
| 2036 |
* If specific tables are being dumped, dump just those tables; else, dump |
| 2037 |
* according to the parent namespace's dump flag. |
| 2038 |
*/ |
| 2039 |
if (table_include_oids.head != NULL) |
| 2040 |
tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids, |
| 2041 |
tbinfo->dobj.catId.oid) ? |
| 2042 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2043 |
else |
| 2044 |
tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains; |
| 2045 |
|
| 2046 |
/* |
| 2047 |
* In any case, a table can be excluded by an exclusion switch |
| 2048 |
*/ |
| 2049 |
if (tbinfo->dobj.dump && |
| 2050 |
simple_oid_list_member(&table_exclude_oids, |
| 2051 |
tbinfo->dobj.catId.oid)) |
| 2052 |
tbinfo->dobj.dump = DUMP_COMPONENT_NONE; |
| 2053 |
} |
| 2054 |
|
| 2055 |
/* |
| 2056 |
* selectDumpableType: policy-setting subroutine |
| 2057 |
* Mark a type as to be dumped or not |
| 2058 |
* |
| 2059 |
* If it's a table's rowtype or an autogenerated array type, we also apply a |
| 2060 |
* special type code to facilitate sorting into the desired order. (We don't |
| 2061 |
* want to consider those to be ordinary types because that would bring tables |
| 2062 |
* up into the datatype part of the dump order.) We still set the object's |
| 2063 |
* dump flag; that's not going to cause the dummy type to be dumped, but we |
| 2064 |
* need it so that casts involving such types will be dumped correctly -- see |
| 2065 |
* dumpCast. This means the flag should be set the same as for the underlying |
| 2066 |
* object (the table or base type). |
| 2067 |
*/ |
| 2068 |
static void |
| 2069 |
selectDumpableType(TypeInfo *tyinfo, Archive *fout) |
| 2070 |
{ |
| 2071 |
/* skip complex types, except for standalone composite types */ |
| 2072 |
if (OidIsValid(tyinfo->typrelid) && |
| 2073 |
tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE) |
| 2074 |
{ |
| 2075 |
TableInfo *tytable = findTableByOid(tyinfo->typrelid); |
| 2076 |
|
| 2077 |
tyinfo->dobj.objType = DO_DUMMY_TYPE; |
| 2078 |
if (tytable != NULL) |
| 2079 |
tyinfo->dobj.dump = tytable->dobj.dump; |
| 2080 |
else |
| 2081 |
tyinfo->dobj.dump = DUMP_COMPONENT_NONE; |
| 2082 |
return; |
| 2083 |
} |
| 2084 |
|
| 2085 |
/* skip auto-generated array and multirange types */ |
| 2086 |
if (tyinfo->isArray || tyinfo->isMultirange) |
| 2087 |
{ |
| 2088 |
tyinfo->dobj.objType = DO_DUMMY_TYPE; |
| 2089 |
|
| 2090 |
/* |
| 2091 |
* Fall through to set the dump flag; we assume that the subsequent |
| 2092 |
* rules will do the same thing as they would for the array's base |
| 2093 |
* type or multirange's range type. (We cannot reliably look up the |
| 2094 |
* base type here, since getTypes may not have processed it yet.) |
| 2095 |
*/ |
| 2096 |
} |
| 2097 |
|
| 2098 |
if (checkExtensionMembership(&tyinfo->dobj, fout)) |
| 2099 |
return; /* extension membership overrides all else */ |
| 2100 |
|
| 2101 |
/* Dump based on if the contents of the namespace are being dumped */ |
| 2102 |
tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains; |
| 2103 |
} |
| 2104 |
|
| 2105 |
/* |
| 2106 |
* selectDumpableDefaultACL: policy-setting subroutine |
| 2107 |
* Mark a default ACL as to be dumped or not |
| 2108 |
* |
| 2109 |
* For per-schema default ACLs, dump if the schema is to be dumped. |
| 2110 |
* Otherwise dump if we are dumping "everything". Note that dumpSchema |
| 2111 |
* and aclsSkip are checked separately. |
| 2112 |
*/ |
| 2113 |
static void |
| 2114 |
selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt) |
| 2115 |
{ |
| 2116 |
/* Default ACLs can't be extension members */ |
| 2117 |
|
| 2118 |
if (dinfo->dobj.namespace) |
| 2119 |
/* default ACLs are considered part of the namespace */ |
| 2120 |
dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains; |
| 2121 |
else |
| 2122 |
dinfo->dobj.dump = dopt->include_everything ? |
| 2123 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2124 |
} |
| 2125 |
|
| 2126 |
/* |
| 2127 |
* selectDumpableCast: policy-setting subroutine |
| 2128 |
* Mark a cast as to be dumped or not |
| 2129 |
* |
| 2130 |
* Casts do not belong to any particular namespace (since they haven't got |
| 2131 |
* names), nor do they have identifiable owners. To distinguish user-defined |
| 2132 |
* casts from built-in ones, we must resort to checking whether the cast's |
| 2133 |
* OID is in the range reserved for initdb. |
| 2134 |
*/ |
| 2135 |
static void |
| 2136 |
selectDumpableCast(CastInfo *cast, Archive *fout) |
| 2137 |
{ |
| 2138 |
if (checkExtensionMembership(&cast->dobj, fout)) |
| 2139 |
return; /* extension membership overrides all else */ |
| 2140 |
|
| 2141 |
/* |
| 2142 |
* This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not |
| 2143 |
* support ACLs currently. |
| 2144 |
*/ |
| 2145 |
if (cast->dobj.catId.oid <= (Oid) g_last_builtin_oid) |
| 2146 |
cast->dobj.dump = DUMP_COMPONENT_NONE; |
| 2147 |
else |
| 2148 |
cast->dobj.dump = fout->dopt->include_everything ? |
| 2149 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2150 |
} |
| 2151 |
|
| 2152 |
/* |
| 2153 |
* selectDumpableProcLang: policy-setting subroutine |
| 2154 |
* Mark a procedural language as to be dumped or not |
| 2155 |
* |
| 2156 |
* Procedural languages do not belong to any particular namespace. To |
| 2157 |
* identify built-in languages, we must resort to checking whether the |
| 2158 |
* language's OID is in the range reserved for initdb. |
| 2159 |
*/ |
| 2160 |
static void |
| 2161 |
selectDumpableProcLang(ProcLangInfo *plang, Archive *fout) |
| 2162 |
{ |
| 2163 |
if (checkExtensionMembership(&plang->dobj, fout)) |
| 2164 |
return; /* extension membership overrides all else */ |
| 2165 |
|
| 2166 |
/* |
| 2167 |
* Only include procedural languages when we are dumping everything. |
| 2168 |
* |
| 2169 |
* For from-initdb procedural languages, only include ACLs, as we do for |
| 2170 |
* the pg_catalog namespace. We need this because procedural languages do |
| 2171 |
* not live in any namespace. |
| 2172 |
*/ |
| 2173 |
if (!fout->dopt->include_everything) |
| 2174 |
plang->dobj.dump = DUMP_COMPONENT_NONE; |
| 2175 |
else |
| 2176 |
{ |
| 2177 |
if (plang->dobj.catId.oid <= (Oid) g_last_builtin_oid) |
| 2178 |
plang->dobj.dump = fout->remoteVersion < 90600 ? |
| 2179 |
DUMP_COMPONENT_NONE : DUMP_COMPONENT_ACL; |
| 2180 |
else |
| 2181 |
plang->dobj.dump = DUMP_COMPONENT_ALL; |
| 2182 |
} |
| 2183 |
} |
| 2184 |
|
| 2185 |
/* |
| 2186 |
* selectDumpableAccessMethod: policy-setting subroutine |
| 2187 |
* Mark an access method as to be dumped or not |
| 2188 |
* |
| 2189 |
* Access methods do not belong to any particular namespace. To identify |
| 2190 |
* built-in access methods, we must resort to checking whether the |
| 2191 |
* method's OID is in the range reserved for initdb. |
| 2192 |
*/ |
| 2193 |
static void |
| 2194 |
selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout) |
| 2195 |
{ |
| 2196 |
/* see getAccessMethods() comment about v9.6. */ |
| 2197 |
if (fout->remoteVersion < 90600) |
| 2198 |
{ |
| 2199 |
method->dobj.dump = DUMP_COMPONENT_NONE; |
| 2200 |
return; |
| 2201 |
} |
| 2202 |
|
| 2203 |
if (checkExtensionMembership(&method->dobj, fout)) |
| 2204 |
return; /* extension membership overrides all else */ |
| 2205 |
|
| 2206 |
/* |
| 2207 |
* This would be DUMP_COMPONENT_ACL for from-initdb access methods, but |
| 2208 |
* they do not support ACLs currently. |
| 2209 |
*/ |
| 2210 |
if (method->dobj.catId.oid <= (Oid) g_last_builtin_oid) |
| 2211 |
method->dobj.dump = DUMP_COMPONENT_NONE; |
| 2212 |
else |
| 2213 |
method->dobj.dump = fout->dopt->include_everything ? |
| 2214 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2215 |
} |
| 2216 |
|
| 2217 |
/* |
| 2218 |
* selectDumpableExtension: policy-setting subroutine |
| 2219 |
* Mark an extension as to be dumped or not |
| 2220 |
* |
| 2221 |
* Built-in extensions should be skipped except for checking ACLs, since we |
| 2222 |
* assume those will already be installed in the target database. We identify |
| 2223 |
* such extensions by their having OIDs in the range reserved for initdb. |
| 2224 |
* We dump all user-added extensions by default. No extensions are dumped |
| 2225 |
* if include_everything is false (i.e., a --schema or --table switch was |
| 2226 |
* given), except if --extension specifies a list of extensions to dump. |
| 2227 |
*/ |
| 2228 |
static void |
| 2229 |
selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt) |
| 2230 |
{ |
| 2231 |
/* |
| 2232 |
* Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to |
| 2233 |
* change permissions on their member objects, if they wish to, and have |
| 2234 |
* those changes preserved. |
| 2235 |
*/ |
| 2236 |
if (extinfo->dobj.catId.oid <= (Oid) g_last_builtin_oid) |
| 2237 |
extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL; |
| 2238 |
else |
| 2239 |
{ |
| 2240 |
/* check if there is a list of extensions to dump */ |
| 2241 |
if (extension_include_oids.head != NULL) |
| 2242 |
extinfo->dobj.dump = extinfo->dobj.dump_contains = |
| 2243 |
simple_oid_list_member(&extension_include_oids, |
| 2244 |
extinfo->dobj.catId.oid) ? |
| 2245 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2246 |
else |
| 2247 |
extinfo->dobj.dump = extinfo->dobj.dump_contains = |
| 2248 |
dopt->include_everything ? |
| 2249 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2250 |
|
| 2251 |
/* check that the extension is not explicitly excluded */ |
| 2252 |
if (extinfo->dobj.dump && |
| 2253 |
simple_oid_list_member(&extension_exclude_oids, |
| 2254 |
extinfo->dobj.catId.oid)) |
| 2255 |
extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE; |
| 2256 |
} |
| 2257 |
} |
| 2258 |
|
| 2259 |
/* |
| 2260 |
* selectDumpablePublicationObject: policy-setting subroutine |
| 2261 |
* Mark a publication object as to be dumped or not |
| 2262 |
* |
| 2263 |
* A publication can have schemas and tables which have schemas, but those are |
| 2264 |
* ignored in decision making, because publications are only dumped when we are |
| 2265 |
* dumping everything. |
| 2266 |
*/ |
| 2267 |
static void |
| 2268 |
selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout) |
| 2269 |
{ |
| 2270 |
if (checkExtensionMembership(dobj, fout)) |
| 2271 |
return; /* extension membership overrides all else */ |
| 2272 |
|
| 2273 |
dobj->dump = fout->dopt->include_everything ? |
| 2274 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2275 |
} |
| 2276 |
|
| 2277 |
/* |
| 2278 |
* selectDumpableStatisticsObject: policy-setting subroutine |
| 2279 |
* Mark an extended statistics object as to be dumped or not |
| 2280 |
* |
| 2281 |
* We dump an extended statistics object if the schema it's in and the table |
| 2282 |
* it's for are being dumped. (This'll need more thought if statistics |
| 2283 |
* objects ever support cross-table stats.) |
| 2284 |
*/ |
| 2285 |
static void |
| 2286 |
selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout) |
| 2287 |
{ |
| 2288 |
if (checkExtensionMembership(&sobj->dobj, fout)) |
| 2289 |
return; /* extension membership overrides all else */ |
| 2290 |
|
| 2291 |
sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains; |
| 2292 |
if (sobj->stattable == NULL || |
| 2293 |
!(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION)) |
| 2294 |
sobj->dobj.dump = DUMP_COMPONENT_NONE; |
| 2295 |
} |
| 2296 |
|
| 2297 |
/* |
| 2298 |
* selectDumpableObject: policy-setting subroutine |
| 2299 |
* Mark a generic dumpable object as to be dumped or not |
| 2300 |
* |
| 2301 |
* Use this only for object types without a special-case routine above. |
| 2302 |
*/ |
| 2303 |
static void |
| 2304 |
selectDumpableObject(DumpableObject *dobj, Archive *fout) |
| 2305 |
{ |
| 2306 |
if (checkExtensionMembership(dobj, fout)) |
| 2307 |
return; /* extension membership overrides all else */ |
| 2308 |
|
| 2309 |
/* |
| 2310 |
* Default policy is to dump if parent namespace is dumpable, or for |
| 2311 |
* non-namespace-associated items, dump if we're dumping "everything". |
| 2312 |
*/ |
| 2313 |
if (dobj->namespace) |
| 2314 |
dobj->dump = dobj->namespace->dobj.dump_contains; |
| 2315 |
else |
| 2316 |
dobj->dump = fout->dopt->include_everything ? |
| 2317 |
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; |
| 2318 |
} |
| 2319 |
|
| 2320 |
/* |
| 2321 |
* Dump a table's contents for loading using the COPY command |
| 2322 |
* - this routine is called by the Archiver when it wants the table |
| 2323 |
* to be dumped. |
| 2324 |
*/ |
| 2325 |
static int |
| 2326 |
dumpTableData_copy(Archive *fout, const void *dcontext) |
| 2327 |
{ |
| 2328 |
TableDataInfo *tdinfo = (TableDataInfo *) dcontext; |
| 2329 |
TableInfo *tbinfo = tdinfo->tdtable; |
| 2330 |
const char *classname = tbinfo->dobj.name; |
| 2331 |
PQExpBuffer q = createPQExpBuffer(); |
| 2332 |
|
| 2333 |
/* |
| 2334 |
* Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId |
| 2335 |
* which uses it already. |
| 2336 |
*/ |
| 2337 |
PQExpBuffer clistBuf = createPQExpBuffer(); |
| 2338 |
PGconn *conn = GetConnection(fout); |
| 2339 |
PGresult *res; |
| 2340 |
int ret; |
| 2341 |
char *copybuf; |
| 2342 |
const char *column_list; |
| 2343 |
|
| 2344 |
pg_log_info("dumping contents of table \"%s.%s\"", |
| 2345 |
tbinfo->dobj.namespace->dobj.name, classname); |
| 2346 |
|
| 2347 |
/* |
| 2348 |
* Specify the column list explicitly so that we have no possibility of |
| 2349 |
* retrieving data in the wrong column order. (The default column |
| 2350 |
* ordering of COPY will not be what we want in certain corner cases |
| 2351 |
* involving ADD COLUMN and inheritance.) |
| 2352 |
*/ |
| 2353 |
column_list = fmtCopyColumnList(tbinfo, clistBuf); |
| 2354 |
|
| 2355 |
/* |
| 2356 |
* Use COPY (SELECT ...) TO when dumping a foreign table's data, and when |
| 2357 |
* a filter condition was specified. For other cases a simple COPY |
| 2358 |
* suffices. |
| 2359 |
*/ |
| 2360 |
if (tdinfo->filtercond || tbinfo->relkind == RELKIND_FOREIGN_TABLE) |
| 2361 |
{ |
| 2362 |
/* Temporary allows to access to foreign tables to dump data */ |
| 2363 |
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) |
| 2364 |
set_restrict_relation_kind(fout, "view"); |
| 2365 |
|
| 2366 |
appendPQExpBufferStr(q, "COPY (SELECT "); |
| 2367 |
/* klugery to get rid of parens in column list */ |
| 2368 |
if (strlen(column_list) > 2) |
| 2369 |
{ |
| 2370 |
appendPQExpBufferStr(q, column_list + 1); |
| 2371 |
q->data[q->len - 1] = ' '; |
| 2372 |
} |
| 2373 |
else |
| 2374 |
appendPQExpBufferStr(q, "* "); |
| 2375 |
|
| 2376 |
appendPQExpBuffer(q, "FROM %s %s) TO stdout;", |
| 2377 |
fmtQualifiedDumpable(tbinfo), |
| 2378 |
tdinfo->filtercond ? tdinfo->filtercond : ""); |
| 2379 |
} |
| 2380 |
else |
| 2381 |
{ |
| 2382 |
appendPQExpBuffer(q, "COPY %s %s TO stdout;", |
| 2383 |
fmtQualifiedDumpable(tbinfo), |
| 2384 |
column_list); |
| 2385 |
} |
| 2386 |
res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT); |
| 2387 |
PQclear(res); |
| 2388 |
destroyPQExpBuffer(clistBuf); |
| 2389 |
|
| 2390 |
for (;;) |
| 2391 |
{ |
| 2392 |
ret = PQgetCopyData(conn, ©buf, 0); |
| 2393 |
|
| 2394 |
if (ret < 0) |
| 2395 |
break; /* done or error */ |
| 2396 |
|
| 2397 |
if (copybuf) |
| 2398 |
{ |
| 2399 |
WriteData(fout, copybuf, ret); |
| 2400 |
PQfreemem(copybuf); |
| 2401 |
} |
| 2402 |
|
| 2403 |
/* ---------- |
| 2404 |
* THROTTLE: |
| 2405 |
* |
| 2406 |
* There was considerable discussion in late July, 2000 regarding |
| 2407 |
* slowing down pg_dump when backing up large tables. Users with both |
| 2408 |
* slow & fast (multi-processor) machines experienced performance |
| 2409 |
* degradation when doing a backup. |
| 2410 |
* |
| 2411 |
* Initial attempts based on sleeping for a number of ms for each ms |
| 2412 |
* of work were deemed too complex, then a simple 'sleep in each loop' |
| 2413 |
* implementation was suggested. The latter failed because the loop |
| 2414 |
* was too tight. Finally, the following was implemented: |
| 2415 |
* |
| 2416 |
* If throttle is non-zero, then |
| 2417 |
* See how long since the last sleep. |
| 2418 |
* Work out how long to sleep (based on ratio). |
| 2419 |
* If sleep is more than 100ms, then |
| 2420 |
* sleep |
| 2421 |
* reset timer |
| 2422 |
* EndIf |
| 2423 |
* EndIf |
| 2424 |
* |
| 2425 |
* where the throttle value was the number of ms to sleep per ms of |
| 2426 |
* work. The calculation was done in each loop. |
| 2427 |
* |
| 2428 |
* Most of the hard work is done in the backend, and this solution |
| 2429 |
* still did not work particularly well: on slow machines, the ratio |
| 2430 |
* was 50:1, and on medium paced machines, 1:1, and on fast |
| 2431 |
* multi-processor machines, it had little or no effect, for reasons |
| 2432 |
* that were unclear. |
| 2433 |
* |
| 2434 |
* Further discussion ensued, and the proposal was dropped. |
| 2435 |
* |
| 2436 |
* For those people who want this feature, it can be implemented using |
| 2437 |
* gettimeofday in each loop, calculating the time since last sleep, |
| 2438 |
* multiplying that by the sleep ratio, then if the result is more |
| 2439 |
* than a preset 'minimum sleep time' (say 100ms), call the 'select' |
| 2440 |
* function to sleep for a subsecond period ie. |
| 2441 |
* |
| 2442 |
* select(0, NULL, NULL, NULL, &tvi); |
| 2443 |
* |
| 2444 |
* This will return after the interval specified in the structure tvi. |
| 2445 |
* Finally, call gettimeofday again to save the 'last sleep time'. |
| 2446 |
* ---------- |
| 2447 |
*/ |
| 2448 |
} |
| 2449 |
archprintf(fout, "\\.\n\n\n"); |
| 2450 |
|
| 2451 |
if (ret == -2) |
| 2452 |
{ |
| 2453 |
/* copy data transfer failed */ |
| 2454 |
pg_log_error("Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.", classname); |
| 2455 |
pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn)); |
| 2456 |
pg_log_error_detail("Command was: %s", q->data); |
| 2457 |
exit_nicely(1); |
| 2458 |
} |
| 2459 |
|
| 2460 |
/* Check command status and return to normal libpq state */ |
| 2461 |
res = PQgetResult(conn); |
| 2462 |
if (PQresultStatus(res) != PGRES_COMMAND_OK) |
| 2463 |
{ |
| 2464 |
pg_log_error("Dumping the contents of table \"%s\" failed: PQgetResult() failed.", classname); |
| 2465 |
pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn)); |
| 2466 |
pg_log_error_detail("Command was: %s", q->data); |
| 2467 |
exit_nicely(1); |
| 2468 |
} |
| 2469 |
PQclear(res); |
| 2470 |
|
| 2471 |
/* Do this to ensure we've pumped libpq back to idle state */ |
| 2472 |
if (PQgetResult(conn) != NULL) |
| 2473 |
pg_log_warning("unexpected extra results during COPY of table \"%s\"", |
| 2474 |
classname); |
| 2475 |
|
| 2476 |
destroyPQExpBuffer(q); |
| 2477 |
|
| 2478 |
/* Revert back the setting */ |
| 2479 |
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) |
| 2480 |
set_restrict_relation_kind(fout, "view, foreign-table"); |
| 2481 |
|
| 2482 |
return 1; |
| 2483 |
} |
| 2484 |
|
| 2485 |
/* |
| 2486 |
* Dump table data using INSERT commands. |
| 2487 |
* |
| 2488 |
* Caution: when we restore from an archive file direct to database, the |
| 2489 |
* INSERT commands emitted by this function have to be parsed by |
| 2490 |
* pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments, |
| 2491 |
* E'' strings, or dollar-quoted strings. So don't emit anything like that. |
| 2492 |
*/ |
| 2493 |
static int |
| 2494 |
dumpTableData_insert(Archive *fout, const void *dcontext) |
| 2495 |
{ |
| 2496 |
TableDataInfo *tdinfo = (TableDataInfo *) dcontext; |
| 2497 |
TableInfo *tbinfo = tdinfo->tdtable; |
| 2498 |
DumpOptions *dopt = fout->dopt; |
| 2499 |
PQExpBuffer q = createPQExpBuffer(); |
| 2500 |
PQExpBuffer insertStmt = NULL; |
| 2501 |
char *attgenerated; |
| 2502 |
PGresult *res; |
| 2503 |
int nfields, |
| 2504 |
i; |
| 2505 |
int rows_per_statement = dopt->dump_inserts; |
| 2506 |
int rows_this_statement = 0; |
| 2507 |
|
| 2508 |
/* Temporary allows to access to foreign tables to dump data */ |
| 2509 |
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) |
| 2510 |
set_restrict_relation_kind(fout, "view"); |
| 2511 |
|
| 2512 |
/* |
| 2513 |
* If we're going to emit INSERTs with column names, the most efficient |
| 2514 |
* way to deal with generated columns is to exclude them entirely. For |
| 2515 |
* INSERTs without column names, we have to emit DEFAULT rather than the |
| 2516 |
* actual column value --- but we can save a few cycles by fetching nulls |
| 2517 |
* rather than the uninteresting-to-us value. |
| 2518 |
*/ |
| 2519 |
attgenerated = (char *) pg_malloc(tbinfo->numatts * sizeof(char)); |
| 2520 |
appendPQExpBufferStr(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT "); |
| 2521 |
nfields = 0; |
| 2522 |
for (i = 0; i < tbinfo->numatts; i++) |
| 2523 |
{ |
| 2524 |
if (tbinfo->attisdropped[i]) |
| 2525 |
continue; |
| 2526 |
if (tbinfo->attgenerated[i] && dopt->column_inserts) |
| 2527 |
continue; |
| 2528 |
if (nfields > 0) |
| 2529 |
appendPQExpBufferStr(q, ", "); |
| 2530 |
if (tbinfo->attgenerated[i]) |
| 2531 |
appendPQExpBufferStr(q, "NULL"); |
| 2532 |
else |
| 2533 |
appendPQExpBufferStr(q, fmtId(tbinfo->attnames[i])); |
| 2534 |
attgenerated[nfields] = tbinfo->attgenerated[i]; |
| 2535 |
nfields++; |
| 2536 |
} |
| 2537 |
/* Servers before 9.4 will complain about zero-column SELECT */ |
| 2538 |
if (nfields == 0) |
| 2539 |
appendPQExpBufferStr(q, "NULL"); |
| 2540 |
appendPQExpBuffer(q, " FROM ONLY %s", |
| 2541 |
fmtQualifiedDumpable(tbinfo)); |
| 2542 |
if (tdinfo->filtercond) |
| 2543 |
appendPQExpBuffer(q, " %s", tdinfo->filtercond); |
| 2544 |
|
| 2545 |
ExecuteSqlStatement(fout, q->data); |
| 2546 |
|
| 2547 |
while (1) |
| 2548 |
{ |
| 2549 |
res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor", |
| 2550 |
PGRES_TUPLES_OK); |
| 2551 |
|
| 2552 |
/* cross-check field count, allowing for dummy NULL if any */ |
| 2553 |
if (nfields != PQnfields(res) && |
| 2554 |
!(nfields == 0 && PQnfields(res) == 1)) |
| 2555 |
pg_fatal("wrong number of fields retrieved from table \"%s\"", |
| 2556 |
tbinfo->dobj.name); |
| 2557 |
|
| 2558 |
/* |
| 2559 |
* First time through, we build as much of the INSERT statement as |
| 2560 |
* possible in "insertStmt", which we can then just print for each |
| 2561 |
* statement. If the table happens to have zero dumpable columns then |
| 2562 |
* this will be a complete statement, otherwise it will end in |
| 2563 |
* "VALUES" and be ready to have the row's column values printed. |
| 2564 |
*/ |
| 2565 |
if (insertStmt == NULL) |
| 2566 |
{ |
| 2567 |
TableInfo *targettab; |
| 2568 |
|
| 2569 |
insertStmt = createPQExpBuffer(); |
| 2570 |
|
| 2571 |
/* |
| 2572 |
* When load-via-partition-root is set or forced, get the root |
| 2573 |
* table name for the partition table, so that we can reload data |
| 2574 |
* through the root table. |
| 2575 |
*/ |
| 2576 |
if (tbinfo->ispartition && |
| 2577 |
(dopt->load_via_partition_root || |
| 2578 |
forcePartitionRootLoad(tbinfo))) |
| 2579 |
targettab = getRootTableInfo(tbinfo); |
| 2580 |
else |
| 2581 |
targettab = tbinfo; |
| 2582 |
|
| 2583 |
appendPQExpBuffer(insertStmt, "INSERT INTO %s ", |
| 2584 |
fmtQualifiedDumpable(targettab)); |
| 2585 |
|
| 2586 |
/* corner case for zero-column table */ |
| 2587 |
if (nfields == 0) |
| 2588 |
{ |
| 2589 |
appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n"); |
| 2590 |
} |
| 2591 |
else |
| 2592 |
{ |
| 2593 |
/* append the list of column names if required */ |
| 2594 |
if (dopt->column_inserts) |
| 2595 |
{ |
| 2596 |
appendPQExpBufferChar(insertStmt, '('); |
| 2597 |
for (int field = 0; field < nfields; field++) |
| 2598 |
{ |
| 2599 |
if (field > 0) |
| 2600 |
appendPQExpBufferStr(insertStmt, ", "); |
| 2601 |
appendPQExpBufferStr(insertStmt, |
| 2602 |
fmtId(PQfname(res, field))); |
| 2603 |
} |
| 2604 |
appendPQExpBufferStr(insertStmt, ") "); |
| 2605 |
} |
| 2606 |
|
| 2607 |
if (tbinfo->needs_override) |
| 2608 |
appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE "); |
| 2609 |
|
| 2610 |
appendPQExpBufferStr(insertStmt, "VALUES"); |
| 2611 |
} |
| 2612 |
} |
| 2613 |
|
| 2614 |
for (int tuple = 0; tuple < PQntuples(res); tuple++) |
| 2615 |
{ |
| 2616 |
/* Write the INSERT if not in the middle of a multi-row INSERT. */ |
| 2617 |
if (rows_this_statement == 0) |
| 2618 |
archputs(insertStmt->data, fout); |
| 2619 |
|
| 2620 |
/* |
| 2621 |
* If it is zero-column table then we've already written the |
| 2622 |
* complete statement, which will mean we've disobeyed |
| 2623 |
* --rows-per-insert when it's set greater than 1. We do support |
| 2624 |
* a way to make this multi-row with: SELECT UNION ALL SELECT |
| 2625 |
* UNION ALL ... but that's non-standard so we should avoid it |
| 2626 |
* given that using INSERTs is mostly only ever needed for |
| 2627 |
* cross-database exports. |
| 2628 |
*/ |
| 2629 |
if (nfields == 0) |
| 2630 |
continue; |
| 2631 |
|
| 2632 |
/* Emit a row heading */ |
| 2633 |
if (rows_per_statement == 1) |
| 2634 |
archputs(" (", fout); |
| 2635 |
else if (rows_this_statement > 0) |
| 2636 |
archputs(",\n\t(", fout); |
| 2637 |
else |
| 2638 |
archputs("\n\t(", fout); |
| 2639 |
|
| 2640 |
for (int field = 0; field < nfields; field++) |
| 2641 |
{ |
| 2642 |
if (field > 0) |
| 2643 |
archputs(", ", fout); |
| 2644 |
if (attgenerated[field]) |
| 2645 |
{ |
| 2646 |
archputs("DEFAULT", fout); |
| 2647 |
continue; |
| 2648 |
} |
| 2649 |
if (PQgetisnull(res, tuple, field)) |
| 2650 |
{ |
| 2651 |
archputs("NULL", fout); |
| 2652 |
continue; |
| 2653 |
} |
| 2654 |
|
| 2655 |
/* XXX This code is partially duplicated in ruleutils.c */ |
| 2656 |
switch (PQftype(res, field)) |
| 2657 |
{ |
| 2658 |
case INT2OID: |
| 2659 |
case INT4OID: |
| 2660 |
case INT8OID: |
| 2661 |
case OIDOID: |
| 2662 |
case FLOAT4OID: |
| 2663 |
case FLOAT8OID: |
| 2664 |
case NUMERICOID: |
| 2665 |
{ |
| 2666 |
/* |
| 2667 |
* These types are printed without quotes unless |
| 2668 |
* they contain values that aren't accepted by the |
| 2669 |
* scanner unquoted (e.g., 'NaN'). Note that |
| 2670 |
* strtod() and friends might accept NaN, so we |
| 2671 |
* can't use that to test. |
| 2672 |
* |
| 2673 |
* In reality we only need to defend against |
| 2674 |
* infinity and NaN, so we need not get too crazy |
| 2675 |
* about pattern matching here. |
| 2676 |
*/ |
| 2677 |
const char *s = PQgetvalue(res, tuple, field); |
| 2678 |
|
| 2679 |
if (strspn(s, "0123456789 +-eE.") == strlen(s)) |
| 2680 |
archputs(s, fout); |
| 2681 |
else |
| 2682 |
archprintf(fout, "'%s'", s); |
| 2683 |
} |
| 2684 |
break; |
| 2685 |
|
| 2686 |
case BITOID: |
| 2687 |
case VARBITOID: |
| 2688 |
archprintf(fout, "B'%s'", |
| 2689 |
PQgetvalue(res, tuple, field)); |
| 2690 |
break; |
| 2691 |
|
| 2692 |
case BOOLOID: |
| 2693 |
if (strcmp(PQgetvalue(res, tuple, field), "t") == 0) |
| 2694 |
archputs("true", fout); |
| 2695 |
else |
| 2696 |
archputs("false", fout); |
| 2697 |
break; |
| 2698 |
|
| 2699 |
default: |
| 2700 |
/* All other types are printed as string literals. */ |
| 2701 |
resetPQExpBuffer(q); |
| 2702 |
appendStringLiteralAH(q, |
| 2703 |
PQgetvalue(res, tuple, field), |
| 2704 |
fout); |
| 2705 |
archputs(q->data, fout); |
| 2706 |
break; |
| 2707 |
} |
| 2708 |
} |
| 2709 |
|
| 2710 |
/* Terminate the row ... */ |
| 2711 |
archputs(")", fout); |
| 2712 |
|
| 2713 |
/* ... and the statement, if the target no. of rows is reached */ |
| 2714 |
if (++rows_this_statement >= rows_per_statement) |
| 2715 |
{ |
| 2716 |
if (dopt->do_nothing) |
| 2717 |
archputs(" ON CONFLICT DO NOTHING;\n", fout); |
| 2718 |
else |
| 2719 |
archputs(";\n", fout); |
| 2720 |
/* Reset the row counter */ |
| 2721 |
rows_this_statement = 0; |
| 2722 |
} |
| 2723 |
} |
| 2724 |
|
| 2725 |
if (PQntuples(res) <= 0) |
| 2726 |
{ |
| 2727 |
PQclear(res); |
| 2728 |
break; |
| 2729 |
} |
| 2730 |
PQclear(res); |
| 2731 |
} |
| 2732 |
|
| 2733 |
/* Terminate any statements that didn't make the row count. */ |
| 2734 |
if (rows_this_statement > 0) |
| 2735 |
{ |
| 2736 |
if (dopt->do_nothing) |
| 2737 |
archputs(" ON CONFLICT DO NOTHING;\n", fout); |
| 2738 |
else |
| 2739 |
archputs(";\n", fout); |
| 2740 |
} |
| 2741 |
|
| 2742 |
archputs("\n\n", fout); |
| 2743 |
|
| 2744 |
ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor"); |
| 2745 |
|
| 2746 |
destroyPQExpBuffer(q); |
| 2747 |
if (insertStmt != NULL) |
| 2748 |
destroyPQExpBuffer(insertStmt); |
| 2749 |
free(attgenerated); |
| 2750 |
|
| 2751 |
/* Revert back the setting */ |
| 2752 |
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) |
| 2753 |
set_restrict_relation_kind(fout, "view, foreign-table"); |
| 2754 |
|
| 2755 |
return 1; |
| 2756 |
} |
| 2757 |
|
| 2758 |
/* |
| 2759 |
* getRootTableInfo: |
| 2760 |
* get the root TableInfo for the given partition table. |
| 2761 |
*/ |
| 2762 |
static TableInfo * |
| 2763 |
getRootTableInfo(const TableInfo *tbinfo) |
| 2764 |
{ |
| 2765 |
TableInfo *parentTbinfo; |
| 2766 |
|
| 2767 |
Assert(tbinfo->ispartition); |
| 2768 |
Assert(tbinfo->numParents == 1); |
| 2769 |
|
| 2770 |
parentTbinfo = tbinfo->parents[0]; |
| 2771 |
while (parentTbinfo->ispartition) |
| 2772 |
{ |
| 2773 |
Assert(parentTbinfo->numParents == 1); |
| 2774 |
parentTbinfo = parentTbinfo->parents[0]; |
| 2775 |
} |
| 2776 |
|
| 2777 |
return parentTbinfo; |
| 2778 |
} |
| 2779 |
|
| 2780 |
/* |
| 2781 |
* forcePartitionRootLoad |
| 2782 |
* Check if we must force load_via_partition_root for this partition. |
| 2783 |
* |
| 2784 |
* This is required if any level of ancestral partitioned table has an |
| 2785 |
* unsafe partitioning scheme. |
| 2786 |
*/ |
| 2787 |
static bool |
| 2788 |
forcePartitionRootLoad(const TableInfo *tbinfo) |
| 2789 |
{ |
| 2790 |
TableInfo *parentTbinfo; |
| 2791 |
|
| 2792 |
Assert(tbinfo->ispartition); |
| 2793 |
Assert(tbinfo->numParents == 1); |
| 2794 |
|
| 2795 |
parentTbinfo = tbinfo->parents[0]; |
| 2796 |
if (parentTbinfo->unsafe_partitions) |
| 2797 |
return true; |
| 2798 |
while (parentTbinfo->ispartition) |
| 2799 |
{ |
| 2800 |
Assert(parentTbinfo->numParents == 1); |
| 2801 |
parentTbinfo = parentTbinfo->parents[0]; |
| 2802 |
if (parentTbinfo->unsafe_partitions) |
| 2803 |
return true; |
| 2804 |
} |
| 2805 |
|
| 2806 |
return false; |
| 2807 |
} |
| 2808 |
|
| 2809 |
/* |
| 2810 |
* dumpTableData - |
| 2811 |
* dump the contents of a single table |
| 2812 |
* |
| 2813 |
* Actually, this just makes an ArchiveEntry for the table contents. |
| 2814 |
*/ |
| 2815 |
static void |
| 2816 |
dumpTableData(Archive *fout, const TableDataInfo *tdinfo) |
| 2817 |
{ |
| 2818 |
DumpOptions *dopt = fout->dopt; |
| 2819 |
TableInfo *tbinfo = tdinfo->tdtable; |
| 2820 |
PQExpBuffer copyBuf = createPQExpBuffer(); |
| 2821 |
PQExpBuffer clistBuf = createPQExpBuffer(); |
| 2822 |
DataDumperPtr dumpFn; |
| 2823 |
char *tdDefn = NULL; |
| 2824 |
char *copyStmt; |
| 2825 |
const char *copyFrom; |
| 2826 |
|
| 2827 |
/* We had better have loaded per-column details about this table */ |
| 2828 |
Assert(tbinfo->interesting); |
| 2829 |
|
| 2830 |
/* |
| 2831 |
* When load-via-partition-root is set or forced, get the root table name |
| 2832 |
* for the partition table, so that we can reload data through the root |
| 2833 |
* table. Then construct a comment to be inserted into the TOC entry's |
| 2834 |
* defn field, so that such cases can be identified reliably. |
| 2835 |
*/ |
| 2836 |
if (tbinfo->ispartition && |
| 2837 |
(dopt->load_via_partition_root || |
| 2838 |
forcePartitionRootLoad(tbinfo))) |
| 2839 |
{ |
| 2840 |
TableInfo *parentTbinfo; |
| 2841 |
char *sanitized; |
| 2842 |
|
| 2843 |
parentTbinfo = getRootTableInfo(tbinfo); |
| 2844 |
copyFrom = fmtQualifiedDumpable(parentTbinfo); |
| 2845 |
sanitized = sanitize_line(copyFrom, true); |
| 2846 |
printfPQExpBuffer(copyBuf, "-- load via partition root %s", |
| 2847 |
sanitized); |
| 2848 |
free(sanitized); |
| 2849 |
tdDefn = pg_strdup(copyBuf->data); |
| 2850 |
} |
| 2851 |
else |
| 2852 |
copyFrom = fmtQualifiedDumpable(tbinfo); |
| 2853 |
|
| 2854 |
if (dopt->dump_inserts == 0) |
| 2855 |
{ |
| 2856 |
/* Dump/restore using COPY */ |
| 2857 |
dumpFn = dumpTableData_copy; |
| 2858 |
/* must use 2 steps here 'cause fmtId is nonreentrant */ |
| 2859 |
printfPQExpBuffer(copyBuf, "COPY %s ", |
| 2860 |
copyFrom); |
| 2861 |
appendPQExpBuffer(copyBuf, "%s FROM stdin;\n", |
| 2862 |
fmtCopyColumnList(tbinfo, clistBuf)); |
| 2863 |
copyStmt = copyBuf->data; |
| 2864 |
} |
| 2865 |
else |
| 2866 |
{ |
| 2867 |
/* Restore using INSERT */ |
| 2868 |
dumpFn = dumpTableData_insert; |
| 2869 |
copyStmt = NULL; |
| 2870 |
} |
| 2871 |
|
| 2872 |
/* |
| 2873 |
* Note: although the TableDataInfo is a full DumpableObject, we treat its |
| 2874 |
* dependency on its table as "special" and pass it to ArchiveEntry now. |
| 2875 |
* See comments for BuildArchiveDependencies. |
| 2876 |
*/ |
| 2877 |
if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA) |
| 2878 |
{ |
| 2879 |
TocEntry *te; |
| 2880 |
|
| 2881 |
te = ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId, |
| 2882 |
ARCHIVE_OPTS(.tag = tbinfo->dobj.name, |
| 2883 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 2884 |
.owner = tbinfo->rolname, |
| 2885 |
.description = "TABLE DATA", |
| 2886 |
.section = SECTION_DATA, |
| 2887 |
.createStmt = tdDefn, |
| 2888 |
.copyStmt = copyStmt, |
| 2889 |
.deps = &(tbinfo->dobj.dumpId), |
| 2890 |
.nDeps = 1, |
| 2891 |
.dumpFn = dumpFn, |
| 2892 |
.dumpArg = tdinfo)); |
| 2893 |
|
| 2894 |
/* |
| 2895 |
* Set the TocEntry's dataLength in case we are doing a parallel dump |
| 2896 |
* and want to order dump jobs by table size. We choose to measure |
| 2897 |
* dataLength in table pages (including TOAST pages) during dump, so |
| 2898 |
* no scaling is needed. |
| 2899 |
* |
| 2900 |
* However, relpages is declared as "integer" in pg_class, and hence |
| 2901 |
* also in TableInfo, but it's really BlockNumber a/k/a unsigned int. |
| 2902 |
* Cast so that we get the right interpretation of table sizes |
| 2903 |
* exceeding INT_MAX pages. |
| 2904 |
*/ |
| 2905 |
te->dataLength = (BlockNumber) tbinfo->relpages; |
| 2906 |
te->dataLength += (BlockNumber) tbinfo->toastpages; |
| 2907 |
|
| 2908 |
/* |
| 2909 |
* If pgoff_t is only 32 bits wide, the above refinement is useless, |
| 2910 |
* and instead we'd better worry about integer overflow. Clamp to |
| 2911 |
* INT_MAX if the correct result exceeds that. |
| 2912 |
*/ |
| 2913 |
if (sizeof(te->dataLength) == 4 && |
| 2914 |
(tbinfo->relpages < 0 || tbinfo->toastpages < 0 || |
| 2915 |
te->dataLength < 0)) |
| 2916 |
te->dataLength = INT_MAX; |
| 2917 |
} |
| 2918 |
|
| 2919 |
destroyPQExpBuffer(copyBuf); |
| 2920 |
destroyPQExpBuffer(clistBuf); |
| 2921 |
} |
| 2922 |
|
| 2923 |
/* |
| 2924 |
* refreshMatViewData - |
| 2925 |
* load or refresh the contents of a single materialized view |
| 2926 |
* |
| 2927 |
* Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW |
| 2928 |
* statement. |
| 2929 |
*/ |
| 2930 |
static void |
| 2931 |
refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo) |
| 2932 |
{ |
| 2933 |
TableInfo *tbinfo = tdinfo->tdtable; |
| 2934 |
PQExpBuffer q; |
| 2935 |
|
| 2936 |
/* If the materialized view is not flagged as populated, skip this. */ |
| 2937 |
if (!tbinfo->relispopulated) |
| 2938 |
return; |
| 2939 |
|
| 2940 |
q = createPQExpBuffer(); |
| 2941 |
|
| 2942 |
appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n", |
| 2943 |
fmtQualifiedDumpable(tbinfo)); |
| 2944 |
|
| 2945 |
if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA) |
| 2946 |
ArchiveEntry(fout, |
| 2947 |
tdinfo->dobj.catId, /* catalog ID */ |
| 2948 |
tdinfo->dobj.dumpId, /* dump ID */ |
| 2949 |
ARCHIVE_OPTS(.tag = tbinfo->dobj.name, |
| 2950 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 2951 |
.owner = tbinfo->rolname, |
| 2952 |
.description = "MATERIALIZED VIEW DATA", |
| 2953 |
.section = SECTION_POST_DATA, |
| 2954 |
.createStmt = q->data, |
| 2955 |
.deps = tdinfo->dobj.dependencies, |
| 2956 |
.nDeps = tdinfo->dobj.nDeps)); |
| 2957 |
|
| 2958 |
destroyPQExpBuffer(q); |
| 2959 |
} |
| 2960 |
|
| 2961 |
/* |
| 2962 |
* getTableData - |
| 2963 |
* set up dumpable objects representing the contents of tables |
| 2964 |
*/ |
| 2965 |
static void |
| 2966 |
getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind) |
| 2967 |
{ |
| 2968 |
int i; |
| 2969 |
|
| 2970 |
for (i = 0; i < numTables; i++) |
| 2971 |
{ |
| 2972 |
if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA && |
| 2973 |
(!relkind || tblinfo[i].relkind == relkind)) |
| 2974 |
makeTableDataInfo(dopt, &(tblinfo[i])); |
| 2975 |
} |
| 2976 |
} |
| 2977 |
|
| 2978 |
/* |
| 2979 |
* Make a dumpable object for the data of this specific table |
| 2980 |
* |
| 2981 |
* Note: we make a TableDataInfo if and only if we are going to dump the |
| 2982 |
* table data; the "dump" field in such objects isn't very interesting. |
| 2983 |
*/ |
| 2984 |
static void |
| 2985 |
makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo) |
| 2986 |
{ |
| 2987 |
TableDataInfo *tdinfo; |
| 2988 |
|
| 2989 |
/* |
| 2990 |
* Nothing to do if we already decided to dump the table. This will |
| 2991 |
* happen for "config" tables. |
| 2992 |
*/ |
| 2993 |
if (tbinfo->dataObj != NULL) |
| 2994 |
return; |
| 2995 |
|
| 2996 |
/* Skip VIEWs (no data to dump) */ |
| 2997 |
if (tbinfo->relkind == RELKIND_VIEW) |
| 2998 |
return; |
| 2999 |
/* Skip FOREIGN TABLEs (no data to dump) unless requested explicitly */ |
| 3000 |
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE && |
| 3001 |
(foreign_servers_include_oids.head == NULL || |
| 3002 |
!simple_oid_list_member(&foreign_servers_include_oids, |
| 3003 |
tbinfo->foreign_server))) |
| 3004 |
return; |
| 3005 |
/* Skip partitioned tables (data in partitions) */ |
| 3006 |
if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE) |
| 3007 |
return; |
| 3008 |
|
| 3009 |
/* Don't dump data in unlogged tables, if so requested */ |
| 3010 |
if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED && |
| 3011 |
dopt->no_unlogged_table_data) |
| 3012 |
return; |
| 3013 |
|
| 3014 |
/* Check that the data is not explicitly excluded */ |
| 3015 |
if (simple_oid_list_member(&tabledata_exclude_oids, |
| 3016 |
tbinfo->dobj.catId.oid)) |
| 3017 |
return; |
| 3018 |
|
| 3019 |
/* OK, let's dump it */ |
| 3020 |
tdinfo = (TableDataInfo *) pg_malloc(sizeof(TableDataInfo)); |
| 3021 |
|
| 3022 |
if (tbinfo->relkind == RELKIND_MATVIEW) |
| 3023 |
tdinfo->dobj.objType = DO_REFRESH_MATVIEW; |
| 3024 |
else if (tbinfo->relkind == RELKIND_SEQUENCE) |
| 3025 |
tdinfo->dobj.objType = DO_SEQUENCE_SET; |
| 3026 |
else |
| 3027 |
tdinfo->dobj.objType = DO_TABLE_DATA; |
| 3028 |
|
| 3029 |
/* |
| 3030 |
* Note: use tableoid 0 so that this object won't be mistaken for |
| 3031 |
* something that pg_depend entries apply to. |
| 3032 |
*/ |
| 3033 |
tdinfo->dobj.catId.tableoid = 0; |
| 3034 |
tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid; |
| 3035 |
AssignDumpId(&tdinfo->dobj); |
| 3036 |
tdinfo->dobj.name = tbinfo->dobj.name; |
| 3037 |
tdinfo->dobj.namespace = tbinfo->dobj.namespace; |
| 3038 |
tdinfo->tdtable = tbinfo; |
| 3039 |
tdinfo->filtercond = NULL; /* might get set later */ |
| 3040 |
addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId); |
| 3041 |
|
| 3042 |
/* A TableDataInfo contains data, of course */ |
| 3043 |
tdinfo->dobj.components |= DUMP_COMPONENT_DATA; |
| 3044 |
|
| 3045 |
tbinfo->dataObj = tdinfo; |
| 3046 |
|
| 3047 |
/* |
| 3048 |
* Materialized view statistics must be restored after the data, because |
| 3049 |
* REFRESH MATERIALIZED VIEW replaces the storage and resets the stats. |
| 3050 |
* |
| 3051 |
* The dependency is added here because the statistics objects are created |
| 3052 |
* first. |
| 3053 |
*/ |
| 3054 |
if (tbinfo->relkind == RELKIND_MATVIEW && tbinfo->stats != NULL) |
| 3055 |
{ |
| 3056 |
tbinfo->stats->section = SECTION_POST_DATA; |
| 3057 |
addObjectDependency(&tbinfo->stats->dobj, tdinfo->dobj.dumpId); |
| 3058 |
} |
| 3059 |
|
| 3060 |
/* Make sure that we'll collect per-column info for this table. */ |
| 3061 |
tbinfo->interesting = true; |
| 3062 |
} |
| 3063 |
|
| 3064 |
/* |
| 3065 |
* The refresh for a materialized view must be dependent on the refresh for |
| 3066 |
* any materialized view that this one is dependent on. |
| 3067 |
* |
| 3068 |
* This must be called after all the objects are created, but before they are |
| 3069 |
* sorted. |
| 3070 |
*/ |
| 3071 |
static void |
| 3072 |
buildMatViewRefreshDependencies(Archive *fout) |
| 3073 |
{ |
| 3074 |
PQExpBuffer query; |
| 3075 |
PGresult *res; |
| 3076 |
int ntups, |
| 3077 |
i; |
| 3078 |
int i_classid, |
| 3079 |
i_objid, |
| 3080 |
i_refobjid; |
| 3081 |
|
| 3082 |
/* No Mat Views before 9.3. */ |
| 3083 |
if (fout->remoteVersion < 90300) |
| 3084 |
return; |
| 3085 |
|
| 3086 |
query = createPQExpBuffer(); |
| 3087 |
|
| 3088 |
appendPQExpBufferStr(query, "WITH RECURSIVE w AS " |
| 3089 |
"( " |
| 3090 |
"SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind " |
| 3091 |
"FROM pg_depend d1 " |
| 3092 |
"JOIN pg_class c1 ON c1.oid = d1.objid " |
| 3093 |
"AND c1.relkind = " CppAsString2(RELKIND_MATVIEW) |
| 3094 |
" JOIN pg_rewrite r1 ON r1.ev_class = d1.objid " |
| 3095 |
"JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass " |
| 3096 |
"AND d2.objid = r1.oid " |
| 3097 |
"AND d2.refobjid <> d1.objid " |
| 3098 |
"JOIN pg_class c2 ON c2.oid = d2.refobjid " |
| 3099 |
"AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) "," |
| 3100 |
CppAsString2(RELKIND_VIEW) ") " |
| 3101 |
"WHERE d1.classid = 'pg_class'::regclass " |
| 3102 |
"UNION " |
| 3103 |
"SELECT w.objid, d3.refobjid, c3.relkind " |
| 3104 |
"FROM w " |
| 3105 |
"JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid " |
| 3106 |
"JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass " |
| 3107 |
"AND d3.objid = r3.oid " |
| 3108 |
"AND d3.refobjid <> w.refobjid " |
| 3109 |
"JOIN pg_class c3 ON c3.oid = d3.refobjid " |
| 3110 |
"AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) "," |
| 3111 |
CppAsString2(RELKIND_VIEW) ") " |
| 3112 |
") " |
| 3113 |
"SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid " |
| 3114 |
"FROM w " |
| 3115 |
"WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW)); |
| 3116 |
|
| 3117 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 3118 |
|
| 3119 |
ntups = PQntuples(res); |
| 3120 |
|
| 3121 |
i_classid = PQfnumber(res, "classid"); |
| 3122 |
i_objid = PQfnumber(res, "objid"); |
| 3123 |
i_refobjid = PQfnumber(res, "refobjid"); |
| 3124 |
|
| 3125 |
for (i = 0; i < ntups; i++) |
| 3126 |
{ |
| 3127 |
CatalogId objId; |
| 3128 |
CatalogId refobjId; |
| 3129 |
DumpableObject *dobj; |
| 3130 |
DumpableObject *refdobj; |
| 3131 |
TableInfo *tbinfo; |
| 3132 |
TableInfo *reftbinfo; |
| 3133 |
|
| 3134 |
objId.tableoid = atooid(PQgetvalue(res, i, i_classid)); |
| 3135 |
objId.oid = atooid(PQgetvalue(res, i, i_objid)); |
| 3136 |
refobjId.tableoid = objId.tableoid; |
| 3137 |
refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid)); |
| 3138 |
|
| 3139 |
dobj = findObjectByCatalogId(objId); |
| 3140 |
if (dobj == NULL) |
| 3141 |
continue; |
| 3142 |
|
| 3143 |
Assert(dobj->objType == DO_TABLE); |
| 3144 |
tbinfo = (TableInfo *) dobj; |
| 3145 |
Assert(tbinfo->relkind == RELKIND_MATVIEW); |
| 3146 |
dobj = (DumpableObject *) tbinfo->dataObj; |
| 3147 |
if (dobj == NULL) |
| 3148 |
continue; |
| 3149 |
Assert(dobj->objType == DO_REFRESH_MATVIEW); |
| 3150 |
|
| 3151 |
refdobj = findObjectByCatalogId(refobjId); |
| 3152 |
if (refdobj == NULL) |
| 3153 |
continue; |
| 3154 |
|
| 3155 |
Assert(refdobj->objType == DO_TABLE); |
| 3156 |
reftbinfo = (TableInfo *) refdobj; |
| 3157 |
Assert(reftbinfo->relkind == RELKIND_MATVIEW); |
| 3158 |
refdobj = (DumpableObject *) reftbinfo->dataObj; |
| 3159 |
if (refdobj == NULL) |
| 3160 |
continue; |
| 3161 |
Assert(refdobj->objType == DO_REFRESH_MATVIEW); |
| 3162 |
|
| 3163 |
addObjectDependency(dobj, refdobj->dumpId); |
| 3164 |
|
| 3165 |
if (!reftbinfo->relispopulated) |
| 3166 |
tbinfo->relispopulated = false; |
| 3167 |
} |
| 3168 |
|
| 3169 |
PQclear(res); |
| 3170 |
|
| 3171 |
destroyPQExpBuffer(query); |
| 3172 |
} |
| 3173 |
|
| 3174 |
/* |
| 3175 |
* getTableDataFKConstraints - |
| 3176 |
* add dump-order dependencies reflecting foreign key constraints |
| 3177 |
* |
| 3178 |
* This code is executed only in a data-only dump --- in schema+data dumps |
| 3179 |
* we handle foreign key issues by not creating the FK constraints until |
| 3180 |
* after the data is loaded. In a data-only dump, however, we want to |
| 3181 |
* order the table data objects in such a way that a table's referenced |
| 3182 |
* tables are restored first. (In the presence of circular references or |
| 3183 |
* self-references this may be impossible; we'll detect and complain about |
| 3184 |
* that during the dependency sorting step.) |
| 3185 |
*/ |
| 3186 |
static void |
| 3187 |
getTableDataFKConstraints(void) |
| 3188 |
{ |
| 3189 |
DumpableObject **dobjs; |
| 3190 |
int numObjs; |
| 3191 |
int i; |
| 3192 |
|
| 3193 |
/* Search through all the dumpable objects for FK constraints */ |
| 3194 |
getDumpableObjects(&dobjs, &numObjs); |
| 3195 |
for (i = 0; i < numObjs; i++) |
| 3196 |
{ |
| 3197 |
if (dobjs[i]->objType == DO_FK_CONSTRAINT) |
| 3198 |
{ |
| 3199 |
ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i]; |
| 3200 |
TableInfo *ftable; |
| 3201 |
|
| 3202 |
/* Not interesting unless both tables are to be dumped */ |
| 3203 |
if (cinfo->contable == NULL || |
| 3204 |
cinfo->contable->dataObj == NULL) |
| 3205 |
continue; |
| 3206 |
ftable = findTableByOid(cinfo->confrelid); |
| 3207 |
if (ftable == NULL || |
| 3208 |
ftable->dataObj == NULL) |
| 3209 |
continue; |
| 3210 |
|
| 3211 |
/* |
| 3212 |
* Okay, make referencing table's TABLE_DATA object depend on the |
| 3213 |
* referenced table's TABLE_DATA object. |
| 3214 |
*/ |
| 3215 |
addObjectDependency(&cinfo->contable->dataObj->dobj, |
| 3216 |
ftable->dataObj->dobj.dumpId); |
| 3217 |
} |
| 3218 |
} |
| 3219 |
free(dobjs); |
| 3220 |
} |
| 3221 |
|
| 3222 |
|
| 3223 |
/* |
| 3224 |
* dumpDatabase: |
| 3225 |
* dump the database definition |
| 3226 |
*/ |
| 3227 |
static void |
| 3228 |
dumpDatabase(Archive *fout) |
| 3229 |
{ |
| 3230 |
DumpOptions *dopt = fout->dopt; |
| 3231 |
PQExpBuffer dbQry = createPQExpBuffer(); |
| 3232 |
PQExpBuffer delQry = createPQExpBuffer(); |
| 3233 |
PQExpBuffer creaQry = createPQExpBuffer(); |
| 3234 |
PQExpBuffer labelq = createPQExpBuffer(); |
| 3235 |
PGconn *conn = GetConnection(fout); |
| 3236 |
PGresult *res; |
| 3237 |
int i_tableoid, |
| 3238 |
i_oid, |
| 3239 |
i_datname, |
| 3240 |
i_datdba, |
| 3241 |
i_encoding, |
| 3242 |
i_datlocprovider, |
| 3243 |
i_collate, |
| 3244 |
i_ctype, |
| 3245 |
i_datlocale, |
| 3246 |
i_daticurules, |
| 3247 |
i_frozenxid, |
| 3248 |
i_minmxid, |
| 3249 |
i_datacl, |
| 3250 |
i_acldefault, |
| 3251 |
i_datistemplate, |
| 3252 |
i_datconnlimit, |
| 3253 |
i_datcollversion, |
| 3254 |
i_tablespace; |
| 3255 |
CatalogId dbCatId; |
| 3256 |
DumpId dbDumpId; |
| 3257 |
DumpableAcl dbdacl; |
| 3258 |
const char *datname, |
| 3259 |
*dba, |
| 3260 |
*encoding, |
| 3261 |
*datlocprovider, |
| 3262 |
*collate, |
| 3263 |
*ctype, |
| 3264 |
*locale, |
| 3265 |
*icurules, |
| 3266 |
*datistemplate, |
| 3267 |
*datconnlimit, |
| 3268 |
*tablespace; |
| 3269 |
uint32 frozenxid, |
| 3270 |
minmxid; |
| 3271 |
char *qdatname; |
| 3272 |
|
| 3273 |
pg_log_info("saving database definition"); |
| 3274 |
|
| 3275 |
/* |
| 3276 |
* Fetch the database-level properties for this database. |
| 3277 |
*/ |
| 3278 |
appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, " |
| 3279 |
"datdba, " |
| 3280 |
"pg_encoding_to_char(encoding) AS encoding, " |
| 3281 |
"datcollate, datctype, datfrozenxid, " |
| 3282 |
"datacl, acldefault('d', datdba) AS acldefault, " |
| 3283 |
"datistemplate, datconnlimit, "); |
| 3284 |
if (fout->remoteVersion >= 90300) |
| 3285 |
appendPQExpBufferStr(dbQry, "datminmxid, "); |
| 3286 |
else |
| 3287 |
appendPQExpBufferStr(dbQry, "0 AS datminmxid, "); |
| 3288 |
if (fout->remoteVersion >= 170000) |
| 3289 |
appendPQExpBufferStr(dbQry, "datlocprovider, datlocale, datcollversion, "); |
| 3290 |
else if (fout->remoteVersion >= 150000) |
| 3291 |
appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale AS datlocale, datcollversion, "); |
| 3292 |
else |
| 3293 |
appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS datlocale, NULL AS datcollversion, "); |
| 3294 |
if (fout->remoteVersion >= 160000) |
| 3295 |
appendPQExpBufferStr(dbQry, "daticurules, "); |
| 3296 |
else |
| 3297 |
appendPQExpBufferStr(dbQry, "NULL AS daticurules, "); |
| 3298 |
appendPQExpBufferStr(dbQry, |
| 3299 |
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " |
| 3300 |
"shobj_description(oid, 'pg_database') AS description " |
| 3301 |
"FROM pg_database " |
| 3302 |
"WHERE datname = current_database()"); |
| 3303 |
|
| 3304 |
res = ExecuteSqlQueryForSingleRow(fout, dbQry->data); |
| 3305 |
|
| 3306 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 3307 |
i_oid = PQfnumber(res, "oid"); |
| 3308 |
i_datname = PQfnumber(res, "datname"); |
| 3309 |
i_datdba = PQfnumber(res, "datdba"); |
| 3310 |
i_encoding = PQfnumber(res, "encoding"); |
| 3311 |
i_datlocprovider = PQfnumber(res, "datlocprovider"); |
| 3312 |
i_collate = PQfnumber(res, "datcollate"); |
| 3313 |
i_ctype = PQfnumber(res, "datctype"); |
| 3314 |
i_datlocale = PQfnumber(res, "datlocale"); |
| 3315 |
i_daticurules = PQfnumber(res, "daticurules"); |
| 3316 |
i_frozenxid = PQfnumber(res, "datfrozenxid"); |
| 3317 |
i_minmxid = PQfnumber(res, "datminmxid"); |
| 3318 |
i_datacl = PQfnumber(res, "datacl"); |
| 3319 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 3320 |
i_datistemplate = PQfnumber(res, "datistemplate"); |
| 3321 |
i_datconnlimit = PQfnumber(res, "datconnlimit"); |
| 3322 |
i_datcollversion = PQfnumber(res, "datcollversion"); |
| 3323 |
i_tablespace = PQfnumber(res, "tablespace"); |
| 3324 |
|
| 3325 |
dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid)); |
| 3326 |
dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid)); |
| 3327 |
datname = PQgetvalue(res, 0, i_datname); |
| 3328 |
dba = getRoleName(PQgetvalue(res, 0, i_datdba)); |
| 3329 |
encoding = PQgetvalue(res, 0, i_encoding); |
| 3330 |
datlocprovider = PQgetvalue(res, 0, i_datlocprovider); |
| 3331 |
collate = PQgetvalue(res, 0, i_collate); |
| 3332 |
ctype = PQgetvalue(res, 0, i_ctype); |
| 3333 |
if (!PQgetisnull(res, 0, i_datlocale)) |
| 3334 |
locale = PQgetvalue(res, 0, i_datlocale); |
| 3335 |
else |
| 3336 |
locale = NULL; |
| 3337 |
if (!PQgetisnull(res, 0, i_daticurules)) |
| 3338 |
icurules = PQgetvalue(res, 0, i_daticurules); |
| 3339 |
else |
| 3340 |
icurules = NULL; |
| 3341 |
frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid)); |
| 3342 |
minmxid = atooid(PQgetvalue(res, 0, i_minmxid)); |
| 3343 |
dbdacl.acl = PQgetvalue(res, 0, i_datacl); |
| 3344 |
dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault); |
| 3345 |
datistemplate = PQgetvalue(res, 0, i_datistemplate); |
| 3346 |
datconnlimit = PQgetvalue(res, 0, i_datconnlimit); |
| 3347 |
tablespace = PQgetvalue(res, 0, i_tablespace); |
| 3348 |
|
| 3349 |
qdatname = pg_strdup(fmtId(datname)); |
| 3350 |
|
| 3351 |
/* |
| 3352 |
* Prepare the CREATE DATABASE command. We must specify OID (if we want |
| 3353 |
* to preserve that), as well as the encoding, locale, and tablespace |
| 3354 |
* since those can't be altered later. Other DB properties are left to |
| 3355 |
* the DATABASE PROPERTIES entry, so that they can be applied after |
| 3356 |
* reconnecting to the target DB. |
| 3357 |
* |
| 3358 |
* For binary upgrade, we use the FILE_COPY strategy because testing has |
| 3359 |
* shown it to be faster. When the server is in binary upgrade mode, it |
| 3360 |
* will also skip the checkpoints this strategy ordinarily performs. |
| 3361 |
*/ |
| 3362 |
if (dopt->binary_upgrade) |
| 3363 |
{ |
| 3364 |
appendPQExpBuffer(creaQry, |
| 3365 |
"CREATE DATABASE %s WITH TEMPLATE = template0 " |
| 3366 |
"OID = %u STRATEGY = FILE_COPY", |
| 3367 |
qdatname, dbCatId.oid); |
| 3368 |
} |
| 3369 |
else |
| 3370 |
{ |
| 3371 |
appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0", |
| 3372 |
qdatname); |
| 3373 |
} |
| 3374 |
if (strlen(encoding) > 0) |
| 3375 |
{ |
| 3376 |
appendPQExpBufferStr(creaQry, " ENCODING = "); |
| 3377 |
appendStringLiteralAH(creaQry, encoding, fout); |
| 3378 |
} |
| 3379 |
|
| 3380 |
appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = "); |
| 3381 |
if (datlocprovider[0] == 'b') |
| 3382 |
appendPQExpBufferStr(creaQry, "builtin"); |
| 3383 |
else if (datlocprovider[0] == 'c') |
| 3384 |
appendPQExpBufferStr(creaQry, "libc"); |
| 3385 |
else if (datlocprovider[0] == 'i') |
| 3386 |
appendPQExpBufferStr(creaQry, "icu"); |
| 3387 |
else |
| 3388 |
pg_fatal("unrecognized locale provider: %s", |
| 3389 |
datlocprovider); |
| 3390 |
|
| 3391 |
if (strlen(collate) > 0 && strcmp(collate, ctype) == 0) |
| 3392 |
{ |
| 3393 |
appendPQExpBufferStr(creaQry, " LOCALE = "); |
| 3394 |
appendStringLiteralAH(creaQry, collate, fout); |
| 3395 |
} |
| 3396 |
else |
| 3397 |
{ |
| 3398 |
if (strlen(collate) > 0) |
| 3399 |
{ |
| 3400 |
appendPQExpBufferStr(creaQry, " LC_COLLATE = "); |
| 3401 |
appendStringLiteralAH(creaQry, collate, fout); |
| 3402 |
} |
| 3403 |
if (strlen(ctype) > 0) |
| 3404 |
{ |
| 3405 |
appendPQExpBufferStr(creaQry, " LC_CTYPE = "); |
| 3406 |
appendStringLiteralAH(creaQry, ctype, fout); |
| 3407 |
} |
| 3408 |
} |
| 3409 |
if (locale) |
| 3410 |
{ |
| 3411 |
if (datlocprovider[0] == 'b') |
| 3412 |
appendPQExpBufferStr(creaQry, " BUILTIN_LOCALE = "); |
| 3413 |
else |
| 3414 |
appendPQExpBufferStr(creaQry, " ICU_LOCALE = "); |
| 3415 |
|
| 3416 |
appendStringLiteralAH(creaQry, locale, fout); |
| 3417 |
} |
| 3418 |
|
| 3419 |
if (icurules) |
| 3420 |
{ |
| 3421 |
appendPQExpBufferStr(creaQry, " ICU_RULES = "); |
| 3422 |
appendStringLiteralAH(creaQry, icurules, fout); |
| 3423 |
} |
| 3424 |
|
| 3425 |
/* |
| 3426 |
* For binary upgrade, carry over the collation version. For normal |
| 3427 |
* dump/restore, omit the version, so that it is computed upon restore. |
| 3428 |
*/ |
| 3429 |
if (dopt->binary_upgrade) |
| 3430 |
{ |
| 3431 |
if (!PQgetisnull(res, 0, i_datcollversion)) |
| 3432 |
{ |
| 3433 |
appendPQExpBufferStr(creaQry, " COLLATION_VERSION = "); |
| 3434 |
appendStringLiteralAH(creaQry, |
| 3435 |
PQgetvalue(res, 0, i_datcollversion), |
| 3436 |
fout); |
| 3437 |
} |
| 3438 |
} |
| 3439 |
|
| 3440 |
/* |
| 3441 |
* Note: looking at dopt->outputNoTablespaces here is completely the wrong |
| 3442 |
* thing; the decision whether to specify a tablespace should be left till |
| 3443 |
* pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd |
| 3444 |
* label the DATABASE entry with the tablespace and let the normal |
| 3445 |
* tablespace selection logic work ... but CREATE DATABASE doesn't pay |
| 3446 |
* attention to default_tablespace, so that won't work. |
| 3447 |
*/ |
| 3448 |
if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 && |
| 3449 |
!dopt->outputNoTablespaces) |
| 3450 |
appendPQExpBuffer(creaQry, " TABLESPACE = %s", |
| 3451 |
fmtId(tablespace)); |
| 3452 |
appendPQExpBufferStr(creaQry, ";\n"); |
| 3453 |
|
| 3454 |
appendPQExpBuffer(delQry, "DROP DATABASE %s;\n", |
| 3455 |
qdatname); |
| 3456 |
|
| 3457 |
dbDumpId = createDumpId(); |
| 3458 |
|
| 3459 |
ArchiveEntry(fout, |
| 3460 |
dbCatId, /* catalog ID */ |
| 3461 |
dbDumpId, /* dump ID */ |
| 3462 |
ARCHIVE_OPTS(.tag = datname, |
| 3463 |
.owner = dba, |
| 3464 |
.description = "DATABASE", |
| 3465 |
.section = SECTION_PRE_DATA, |
| 3466 |
.createStmt = creaQry->data, |
| 3467 |
.dropStmt = delQry->data)); |
| 3468 |
|
| 3469 |
/* Compute correct tag for archive entry */ |
| 3470 |
appendPQExpBuffer(labelq, "DATABASE %s", qdatname); |
| 3471 |
|
| 3472 |
/* Dump DB comment if any */ |
| 3473 |
{ |
| 3474 |
/* |
| 3475 |
* 8.2 and up keep comments on shared objects in a shared table, so we |
| 3476 |
* cannot use the dumpComment() code used for other database objects. |
| 3477 |
* Be careful that the ArchiveEntry parameters match that function. |
| 3478 |
*/ |
| 3479 |
char *comment = PQgetvalue(res, 0, PQfnumber(res, "description")); |
| 3480 |
|
| 3481 |
if (comment && *comment && !dopt->no_comments) |
| 3482 |
{ |
| 3483 |
resetPQExpBuffer(dbQry); |
| 3484 |
|
| 3485 |
/* |
| 3486 |
* Generates warning when loaded into a differently-named |
| 3487 |
* database. |
| 3488 |
*/ |
| 3489 |
appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname); |
| 3490 |
appendStringLiteralAH(dbQry, comment, fout); |
| 3491 |
appendPQExpBufferStr(dbQry, ";\n"); |
| 3492 |
|
| 3493 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 3494 |
ARCHIVE_OPTS(.tag = labelq->data, |
| 3495 |
.owner = dba, |
| 3496 |
.description = "COMMENT", |
| 3497 |
.section = SECTION_NONE, |
| 3498 |
.createStmt = dbQry->data, |
| 3499 |
.deps = &dbDumpId, |
| 3500 |
.nDeps = 1)); |
| 3501 |
} |
| 3502 |
} |
| 3503 |
|
| 3504 |
/* Dump DB security label, if enabled */ |
| 3505 |
if (!dopt->no_security_labels) |
| 3506 |
{ |
| 3507 |
PGresult *shres; |
| 3508 |
PQExpBuffer seclabelQry; |
| 3509 |
|
| 3510 |
seclabelQry = createPQExpBuffer(); |
| 3511 |
|
| 3512 |
buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry); |
| 3513 |
shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK); |
| 3514 |
resetPQExpBuffer(seclabelQry); |
| 3515 |
emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname); |
| 3516 |
if (seclabelQry->len > 0) |
| 3517 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 3518 |
ARCHIVE_OPTS(.tag = labelq->data, |
| 3519 |
.owner = dba, |
| 3520 |
.description = "SECURITY LABEL", |
| 3521 |
.section = SECTION_NONE, |
| 3522 |
.createStmt = seclabelQry->data, |
| 3523 |
.deps = &dbDumpId, |
| 3524 |
.nDeps = 1)); |
| 3525 |
destroyPQExpBuffer(seclabelQry); |
| 3526 |
PQclear(shres); |
| 3527 |
} |
| 3528 |
|
| 3529 |
/* |
| 3530 |
* Dump ACL if any. Note that we do not support initial privileges |
| 3531 |
* (pg_init_privs) on databases. |
| 3532 |
*/ |
| 3533 |
dbdacl.privtype = 0; |
| 3534 |
dbdacl.initprivs = NULL; |
| 3535 |
|
| 3536 |
dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE", |
| 3537 |
qdatname, NULL, NULL, |
| 3538 |
NULL, dba, &dbdacl); |
| 3539 |
|
| 3540 |
/* |
| 3541 |
* Now construct a DATABASE PROPERTIES archive entry to restore any |
| 3542 |
* non-default database-level properties. (The reason this must be |
| 3543 |
* separate is that we cannot put any additional commands into the TOC |
| 3544 |
* entry that has CREATE DATABASE. pg_restore would execute such a group |
| 3545 |
* in an implicit transaction block, and the backend won't allow CREATE |
| 3546 |
* DATABASE in that context.) |
| 3547 |
*/ |
| 3548 |
resetPQExpBuffer(creaQry); |
| 3549 |
resetPQExpBuffer(delQry); |
| 3550 |
|
| 3551 |
if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0) |
| 3552 |
appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n", |
| 3553 |
qdatname, datconnlimit); |
| 3554 |
|
| 3555 |
if (strcmp(datistemplate, "t") == 0) |
| 3556 |
{ |
| 3557 |
appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n", |
| 3558 |
qdatname); |
| 3559 |
|
| 3560 |
/* |
| 3561 |
* The backend won't accept DROP DATABASE on a template database. We |
| 3562 |
* can deal with that by removing the template marking before the DROP |
| 3563 |
* gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but |
| 3564 |
* since no such command is currently supported, fake it with a direct |
| 3565 |
* UPDATE on pg_database. |
| 3566 |
*/ |
| 3567 |
appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database " |
| 3568 |
"SET datistemplate = false WHERE datname = "); |
| 3569 |
appendStringLiteralAH(delQry, datname, fout); |
| 3570 |
appendPQExpBufferStr(delQry, ";\n"); |
| 3571 |
} |
| 3572 |
|
| 3573 |
/* |
| 3574 |
* We do not restore pg_database.dathasloginevt because it is set |
| 3575 |
* automatically on login event trigger creation. |
| 3576 |
*/ |
| 3577 |
|
| 3578 |
/* Add database-specific SET options */ |
| 3579 |
dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid); |
| 3580 |
|
| 3581 |
/* |
| 3582 |
* We stick this binary-upgrade query into the DATABASE PROPERTIES archive |
| 3583 |
* entry, too, for lack of a better place. |
| 3584 |
*/ |
| 3585 |
if (dopt->binary_upgrade) |
| 3586 |
{ |
| 3587 |
appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n"); |
| 3588 |
appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n" |
| 3589 |
"SET datfrozenxid = '%u', datminmxid = '%u'\n" |
| 3590 |
"WHERE datname = ", |
| 3591 |
frozenxid, minmxid); |
| 3592 |
appendStringLiteralAH(creaQry, datname, fout); |
| 3593 |
appendPQExpBufferStr(creaQry, ";\n"); |
| 3594 |
} |
| 3595 |
|
| 3596 |
if (creaQry->len > 0) |
| 3597 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 3598 |
ARCHIVE_OPTS(.tag = datname, |
| 3599 |
.owner = dba, |
| 3600 |
.description = "DATABASE PROPERTIES", |
| 3601 |
.section = SECTION_PRE_DATA, |
| 3602 |
.createStmt = creaQry->data, |
| 3603 |
.dropStmt = delQry->data, |
| 3604 |
.deps = &dbDumpId)); |
| 3605 |
|
| 3606 |
/* |
| 3607 |
* pg_largeobject comes from the old system intact, so set its |
| 3608 |
* relfrozenxids, relminmxids and relfilenode. |
| 3609 |
*/ |
| 3610 |
if (dopt->binary_upgrade) |
| 3611 |
{ |
| 3612 |
PGresult *lo_res; |
| 3613 |
PQExpBuffer loFrozenQry = createPQExpBuffer(); |
| 3614 |
PQExpBuffer loOutQry = createPQExpBuffer(); |
| 3615 |
PQExpBuffer loHorizonQry = createPQExpBuffer(); |
| 3616 |
int ii_relfrozenxid, |
| 3617 |
ii_relfilenode, |
| 3618 |
ii_oid, |
| 3619 |
ii_relminmxid; |
| 3620 |
|
| 3621 |
/* |
| 3622 |
* pg_largeobject |
| 3623 |
*/ |
| 3624 |
if (fout->remoteVersion >= 90300) |
| 3625 |
appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n" |
| 3626 |
"FROM pg_catalog.pg_class\n" |
| 3627 |
"WHERE oid IN (%u, %u);\n", |
| 3628 |
LargeObjectRelationId, LargeObjectLOidPNIndexId); |
| 3629 |
else |
| 3630 |
appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid, relfilenode, oid\n" |
| 3631 |
"FROM pg_catalog.pg_class\n" |
| 3632 |
"WHERE oid IN (%u, %u);\n", |
| 3633 |
LargeObjectRelationId, LargeObjectLOidPNIndexId); |
| 3634 |
|
| 3635 |
lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK); |
| 3636 |
|
| 3637 |
ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid"); |
| 3638 |
ii_relminmxid = PQfnumber(lo_res, "relminmxid"); |
| 3639 |
ii_relfilenode = PQfnumber(lo_res, "relfilenode"); |
| 3640 |
ii_oid = PQfnumber(lo_res, "oid"); |
| 3641 |
|
| 3642 |
appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n"); |
| 3643 |
appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n"); |
| 3644 |
for (int i = 0; i < PQntuples(lo_res); ++i) |
| 3645 |
{ |
| 3646 |
Oid oid; |
| 3647 |
RelFileNumber relfilenumber; |
| 3648 |
|
| 3649 |
appendPQExpBuffer(loHorizonQry, "UPDATE pg_catalog.pg_class\n" |
| 3650 |
"SET relfrozenxid = '%u', relminmxid = '%u'\n" |
| 3651 |
"WHERE oid = %u;\n", |
| 3652 |
atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)), |
| 3653 |
atooid(PQgetvalue(lo_res, i, ii_relminmxid)), |
| 3654 |
atooid(PQgetvalue(lo_res, i, ii_oid))); |
| 3655 |
|
| 3656 |
oid = atooid(PQgetvalue(lo_res, i, ii_oid)); |
| 3657 |
relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode)); |
| 3658 |
|
| 3659 |
if (oid == LargeObjectRelationId) |
| 3660 |
appendPQExpBuffer(loOutQry, |
| 3661 |
"SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n", |
| 3662 |
relfilenumber); |
| 3663 |
else if (oid == LargeObjectLOidPNIndexId) |
| 3664 |
appendPQExpBuffer(loOutQry, |
| 3665 |
"SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n", |
| 3666 |
relfilenumber); |
| 3667 |
} |
| 3668 |
|
| 3669 |
appendPQExpBufferStr(loOutQry, |
| 3670 |
"TRUNCATE pg_catalog.pg_largeobject;\n"); |
| 3671 |
appendPQExpBufferStr(loOutQry, loHorizonQry->data); |
| 3672 |
|
| 3673 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 3674 |
ARCHIVE_OPTS(.tag = "pg_largeobject", |
| 3675 |
.description = "pg_largeobject", |
| 3676 |
.section = SECTION_PRE_DATA, |
| 3677 |
.createStmt = loOutQry->data)); |
| 3678 |
|
| 3679 |
PQclear(lo_res); |
| 3680 |
|
| 3681 |
destroyPQExpBuffer(loFrozenQry); |
| 3682 |
destroyPQExpBuffer(loHorizonQry); |
| 3683 |
destroyPQExpBuffer(loOutQry); |
| 3684 |
} |
| 3685 |
|
| 3686 |
PQclear(res); |
| 3687 |
|
| 3688 |
free(qdatname); |
| 3689 |
destroyPQExpBuffer(dbQry); |
| 3690 |
destroyPQExpBuffer(delQry); |
| 3691 |
destroyPQExpBuffer(creaQry); |
| 3692 |
destroyPQExpBuffer(labelq); |
| 3693 |
} |
| 3694 |
|
| 3695 |
/* |
| 3696 |
* Collect any database-specific or role-and-database-specific SET options |
| 3697 |
* for this database, and append them to outbuf. |
| 3698 |
*/ |
| 3699 |
static void |
| 3700 |
dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf, |
| 3701 |
const char *dbname, Oid dboid) |
| 3702 |
{ |
| 3703 |
PGconn *conn = GetConnection(AH); |
| 3704 |
PQExpBuffer buf = createPQExpBuffer(); |
| 3705 |
PGresult *res; |
| 3706 |
|
| 3707 |
/* First collect database-specific options */ |
| 3708 |
printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting " |
| 3709 |
"WHERE setrole = 0 AND setdatabase = '%u'::oid", |
| 3710 |
dboid); |
| 3711 |
|
| 3712 |
res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK); |
| 3713 |
|
| 3714 |
for (int i = 0; i < PQntuples(res); i++) |
| 3715 |
makeAlterConfigCommand(conn, PQgetvalue(res, i, 0), |
| 3716 |
"DATABASE", dbname, NULL, NULL, |
| 3717 |
outbuf); |
| 3718 |
|
| 3719 |
PQclear(res); |
| 3720 |
|
| 3721 |
/* Now look for role-and-database-specific options */ |
| 3722 |
printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) " |
| 3723 |
"FROM pg_db_role_setting s, pg_roles r " |
| 3724 |
"WHERE setrole = r.oid AND setdatabase = '%u'::oid", |
| 3725 |
dboid); |
| 3726 |
|
| 3727 |
res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK); |
| 3728 |
|
| 3729 |
for (int i = 0; i < PQntuples(res); i++) |
| 3730 |
makeAlterConfigCommand(conn, PQgetvalue(res, i, 1), |
| 3731 |
"ROLE", PQgetvalue(res, i, 0), |
| 3732 |
"DATABASE", dbname, |
| 3733 |
outbuf); |
| 3734 |
|
| 3735 |
PQclear(res); |
| 3736 |
|
| 3737 |
destroyPQExpBuffer(buf); |
| 3738 |
} |
| 3739 |
|
| 3740 |
/* |
| 3741 |
* dumpEncoding: put the correct encoding into the archive |
| 3742 |
*/ |
| 3743 |
static void |
| 3744 |
dumpEncoding(Archive *AH) |
| 3745 |
{ |
| 3746 |
const char *encname = pg_encoding_to_char(AH->encoding); |
| 3747 |
PQExpBuffer qry = createPQExpBuffer(); |
| 3748 |
|
| 3749 |
pg_log_info("saving encoding = %s", encname); |
| 3750 |
|
| 3751 |
appendPQExpBufferStr(qry, "SET client_encoding = "); |
| 3752 |
appendStringLiteralAH(qry, encname, AH); |
| 3753 |
appendPQExpBufferStr(qry, ";\n"); |
| 3754 |
|
| 3755 |
ArchiveEntry(AH, nilCatalogId, createDumpId(), |
| 3756 |
ARCHIVE_OPTS(.tag = "ENCODING", |
| 3757 |
.description = "ENCODING", |
| 3758 |
.section = SECTION_PRE_DATA, |
| 3759 |
.createStmt = qry->data)); |
| 3760 |
|
| 3761 |
destroyPQExpBuffer(qry); |
| 3762 |
} |
| 3763 |
|
| 3764 |
|
| 3765 |
/* |
| 3766 |
* dumpStdStrings: put the correct escape string behavior into the archive |
| 3767 |
*/ |
| 3768 |
static void |
| 3769 |
dumpStdStrings(Archive *AH) |
| 3770 |
{ |
| 3771 |
const char *stdstrings = AH->std_strings ? "on" : "off"; |
| 3772 |
PQExpBuffer qry = createPQExpBuffer(); |
| 3773 |
|
| 3774 |
pg_log_info("saving \"standard_conforming_strings = %s\"", |
| 3775 |
stdstrings); |
| 3776 |
|
| 3777 |
appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n", |
| 3778 |
stdstrings); |
| 3779 |
|
| 3780 |
ArchiveEntry(AH, nilCatalogId, createDumpId(), |
| 3781 |
ARCHIVE_OPTS(.tag = "STDSTRINGS", |
| 3782 |
.description = "STDSTRINGS", |
| 3783 |
.section = SECTION_PRE_DATA, |
| 3784 |
.createStmt = qry->data)); |
| 3785 |
|
| 3786 |
destroyPQExpBuffer(qry); |
| 3787 |
} |
| 3788 |
|
| 3789 |
/* |
| 3790 |
* dumpSearchPath: record the active search_path in the archive |
| 3791 |
*/ |
| 3792 |
static void |
| 3793 |
dumpSearchPath(Archive *AH) |
| 3794 |
{ |
| 3795 |
PQExpBuffer qry = createPQExpBuffer(); |
| 3796 |
PQExpBuffer path = createPQExpBuffer(); |
| 3797 |
PGresult *res; |
| 3798 |
char **schemanames = NULL; |
| 3799 |
int nschemanames = 0; |
| 3800 |
int i; |
| 3801 |
|
| 3802 |
/* |
| 3803 |
* We use the result of current_schemas(), not the search_path GUC, |
| 3804 |
* because that might contain wildcards such as "$user", which won't |
| 3805 |
* necessarily have the same value during restore. Also, this way avoids |
| 3806 |
* listing schemas that may appear in search_path but not actually exist, |
| 3807 |
* which seems like a prudent exclusion. |
| 3808 |
*/ |
| 3809 |
res = ExecuteSqlQueryForSingleRow(AH, |
| 3810 |
"SELECT pg_catalog.current_schemas(false)"); |
| 3811 |
|
| 3812 |
if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames)) |
| 3813 |
pg_fatal("could not parse result of current_schemas()"); |
| 3814 |
|
| 3815 |
/* |
| 3816 |
* We use set_config(), not a simple "SET search_path" command, because |
| 3817 |
* the latter has less-clean behavior if the search path is empty. While |
| 3818 |
* that's likely to get fixed at some point, it seems like a good idea to |
| 3819 |
* be as backwards-compatible as possible in what we put into archives. |
| 3820 |
*/ |
| 3821 |
for (i = 0; i < nschemanames; i++) |
| 3822 |
{ |
| 3823 |
if (i > 0) |
| 3824 |
appendPQExpBufferStr(path, ", "); |
| 3825 |
appendPQExpBufferStr(path, fmtId(schemanames[i])); |
| 3826 |
} |
| 3827 |
|
| 3828 |
appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', "); |
| 3829 |
appendStringLiteralAH(qry, path->data, AH); |
| 3830 |
appendPQExpBufferStr(qry, ", false);\n"); |
| 3831 |
|
| 3832 |
pg_log_info("saving \"search_path = %s\"", path->data); |
| 3833 |
|
| 3834 |
ArchiveEntry(AH, nilCatalogId, createDumpId(), |
| 3835 |
ARCHIVE_OPTS(.tag = "SEARCHPATH", |
| 3836 |
.description = "SEARCHPATH", |
| 3837 |
.section = SECTION_PRE_DATA, |
| 3838 |
.createStmt = qry->data)); |
| 3839 |
|
| 3840 |
/* Also save it in AH->searchpath, in case we're doing plain text dump */ |
| 3841 |
AH->searchpath = pg_strdup(qry->data); |
| 3842 |
|
| 3843 |
free(schemanames); |
| 3844 |
PQclear(res); |
| 3845 |
destroyPQExpBuffer(qry); |
| 3846 |
destroyPQExpBuffer(path); |
| 3847 |
} |
| 3848 |
|
| 3849 |
|
| 3850 |
/* |
| 3851 |
* getLOs: |
| 3852 |
* Collect schema-level data about large objects |
| 3853 |
*/ |
| 3854 |
static void |
| 3855 |
getLOs(Archive *fout) |
| 3856 |
{ |
| 3857 |
DumpOptions *dopt = fout->dopt; |
| 3858 |
PQExpBuffer loQry = createPQExpBuffer(); |
| 3859 |
PGresult *res; |
| 3860 |
int ntups; |
| 3861 |
int i; |
| 3862 |
int n; |
| 3863 |
int i_oid; |
| 3864 |
int i_lomowner; |
| 3865 |
int i_lomacl; |
| 3866 |
int i_acldefault; |
| 3867 |
|
| 3868 |
pg_log_info("reading large objects"); |
| 3869 |
|
| 3870 |
/* |
| 3871 |
* Fetch LO OIDs and owner/ACL data. Order the data so that all the blobs |
| 3872 |
* with the same owner/ACL appear together. |
| 3873 |
*/ |
| 3874 |
appendPQExpBufferStr(loQry, |
| 3875 |
"SELECT oid, lomowner, lomacl, " |
| 3876 |
"acldefault('L', lomowner) AS acldefault " |
| 3877 |
"FROM pg_largeobject_metadata " |
| 3878 |
"ORDER BY lomowner, lomacl::pg_catalog.text, oid"); |
| 3879 |
|
| 3880 |
res = ExecuteSqlQuery(fout, loQry->data, PGRES_TUPLES_OK); |
| 3881 |
|
| 3882 |
i_oid = PQfnumber(res, "oid"); |
| 3883 |
i_lomowner = PQfnumber(res, "lomowner"); |
| 3884 |
i_lomacl = PQfnumber(res, "lomacl"); |
| 3885 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 3886 |
|
| 3887 |
ntups = PQntuples(res); |
| 3888 |
|
| 3889 |
/* |
| 3890 |
* Group the blobs into suitably-sized groups that have the same owner and |
| 3891 |
* ACL setting, and build a metadata and a data DumpableObject for each |
| 3892 |
* group. (If we supported initprivs for blobs, we'd have to insist that |
| 3893 |
* groups also share initprivs settings, since the DumpableObject only has |
| 3894 |
* room for one.) i is the index of the first tuple in the current group, |
| 3895 |
* and n is the number of tuples we include in the group. |
| 3896 |
*/ |
| 3897 |
for (i = 0; i < ntups; i += n) |
| 3898 |
{ |
| 3899 |
Oid thisoid = atooid(PQgetvalue(res, i, i_oid)); |
| 3900 |
char *thisowner = PQgetvalue(res, i, i_lomowner); |
| 3901 |
char *thisacl = PQgetvalue(res, i, i_lomacl); |
| 3902 |
LoInfo *loinfo; |
| 3903 |
DumpableObject *lodata; |
| 3904 |
char namebuf[64]; |
| 3905 |
|
| 3906 |
/* Scan to find first tuple not to be included in group */ |
| 3907 |
n = 1; |
| 3908 |
while (n < MAX_BLOBS_PER_ARCHIVE_ENTRY && i + n < ntups) |
| 3909 |
{ |
| 3910 |
if (strcmp(thisowner, PQgetvalue(res, i + n, i_lomowner)) != 0 || |
| 3911 |
strcmp(thisacl, PQgetvalue(res, i + n, i_lomacl)) != 0) |
| 3912 |
break; |
| 3913 |
n++; |
| 3914 |
} |
| 3915 |
|
| 3916 |
/* Build the metadata DumpableObject */ |
| 3917 |
loinfo = (LoInfo *) pg_malloc(offsetof(LoInfo, looids) + n * sizeof(Oid)); |
| 3918 |
|
| 3919 |
loinfo->dobj.objType = DO_LARGE_OBJECT; |
| 3920 |
loinfo->dobj.catId.tableoid = LargeObjectRelationId; |
| 3921 |
loinfo->dobj.catId.oid = thisoid; |
| 3922 |
AssignDumpId(&loinfo->dobj); |
| 3923 |
|
| 3924 |
if (n > 1) |
| 3925 |
snprintf(namebuf, sizeof(namebuf), "%u..%u", thisoid, |
| 3926 |
atooid(PQgetvalue(res, i + n - 1, i_oid))); |
| 3927 |
else |
| 3928 |
snprintf(namebuf, sizeof(namebuf), "%u", thisoid); |
| 3929 |
loinfo->dobj.name = pg_strdup(namebuf); |
| 3930 |
loinfo->dacl.acl = pg_strdup(thisacl); |
| 3931 |
loinfo->dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 3932 |
loinfo->dacl.privtype = 0; |
| 3933 |
loinfo->dacl.initprivs = NULL; |
| 3934 |
loinfo->rolname = getRoleName(thisowner); |
| 3935 |
loinfo->numlos = n; |
| 3936 |
loinfo->looids[0] = thisoid; |
| 3937 |
/* Collect OIDs of the remaining blobs in this group */ |
| 3938 |
for (int k = 1; k < n; k++) |
| 3939 |
{ |
| 3940 |
CatalogId extraID; |
| 3941 |
|
| 3942 |
loinfo->looids[k] = atooid(PQgetvalue(res, i + k, i_oid)); |
| 3943 |
|
| 3944 |
/* Make sure we can look up loinfo by any of the blobs' OIDs */ |
| 3945 |
extraID.tableoid = LargeObjectRelationId; |
| 3946 |
extraID.oid = loinfo->looids[k]; |
| 3947 |
recordAdditionalCatalogID(extraID, &loinfo->dobj); |
| 3948 |
} |
| 3949 |
|
| 3950 |
/* LOs have data */ |
| 3951 |
loinfo->dobj.components |= DUMP_COMPONENT_DATA; |
| 3952 |
|
| 3953 |
/* Mark whether LO group has a non-empty ACL */ |
| 3954 |
if (!PQgetisnull(res, i, i_lomacl)) |
| 3955 |
loinfo->dobj.components |= DUMP_COMPONENT_ACL; |
| 3956 |
|
| 3957 |
/* |
| 3958 |
* In binary-upgrade mode for LOs, we do *not* dump out the LO data, |
| 3959 |
* as it will be copied by pg_upgrade, which simply copies the |
| 3960 |
* pg_largeobject table. We *do* however dump out anything but the |
| 3961 |
* data, as pg_upgrade copies just pg_largeobject, but not |
| 3962 |
* pg_largeobject_metadata, after the dump is restored. |
| 3963 |
*/ |
| 3964 |
if (dopt->binary_upgrade) |
| 3965 |
loinfo->dobj.dump &= ~DUMP_COMPONENT_DATA; |
| 3966 |
|
| 3967 |
/* |
| 3968 |
* Create a "BLOBS" data item for the group, too. This is just a |
| 3969 |
* placeholder for sorting; it carries no data now. |
| 3970 |
*/ |
| 3971 |
lodata = (DumpableObject *) pg_malloc(sizeof(DumpableObject)); |
| 3972 |
lodata->objType = DO_LARGE_OBJECT_DATA; |
| 3973 |
lodata->catId = nilCatalogId; |
| 3974 |
AssignDumpId(lodata); |
| 3975 |
lodata->name = pg_strdup(namebuf); |
| 3976 |
lodata->components |= DUMP_COMPONENT_DATA; |
| 3977 |
/* Set up explicit dependency from data to metadata */ |
| 3978 |
lodata->dependencies = (DumpId *) pg_malloc(sizeof(DumpId)); |
| 3979 |
lodata->dependencies[0] = loinfo->dobj.dumpId; |
| 3980 |
lodata->nDeps = lodata->allocDeps = 1; |
| 3981 |
} |
| 3982 |
|
| 3983 |
PQclear(res); |
| 3984 |
destroyPQExpBuffer(loQry); |
| 3985 |
} |
| 3986 |
|
| 3987 |
/* |
| 3988 |
* dumpLO |
| 3989 |
* |
| 3990 |
* dump the definition (metadata) of the given large object group |
| 3991 |
*/ |
| 3992 |
static void |
| 3993 |
dumpLO(Archive *fout, const LoInfo *loinfo) |
| 3994 |
{ |
| 3995 |
PQExpBuffer cquery = createPQExpBuffer(); |
| 3996 |
|
| 3997 |
/* |
| 3998 |
* The "definition" is just a newline-separated list of OIDs. We need to |
| 3999 |
* put something into the dropStmt too, but it can just be a comment. |
| 4000 |
*/ |
| 4001 |
for (int i = 0; i < loinfo->numlos; i++) |
| 4002 |
appendPQExpBuffer(cquery, "%u\n", loinfo->looids[i]); |
| 4003 |
|
| 4004 |
if (loinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 4005 |
ArchiveEntry(fout, loinfo->dobj.catId, loinfo->dobj.dumpId, |
| 4006 |
ARCHIVE_OPTS(.tag = loinfo->dobj.name, |
| 4007 |
.owner = loinfo->rolname, |
| 4008 |
.description = "BLOB METADATA", |
| 4009 |
.section = SECTION_DATA, |
| 4010 |
.createStmt = cquery->data, |
| 4011 |
.dropStmt = "-- dummy")); |
| 4012 |
|
| 4013 |
/* |
| 4014 |
* Dump per-blob comments and seclabels if any. We assume these are rare |
| 4015 |
* enough that it's okay to generate retail TOC entries for them. |
| 4016 |
*/ |
| 4017 |
if (loinfo->dobj.dump & (DUMP_COMPONENT_COMMENT | |
| 4018 |
DUMP_COMPONENT_SECLABEL)) |
| 4019 |
{ |
| 4020 |
for (int i = 0; i < loinfo->numlos; i++) |
| 4021 |
{ |
| 4022 |
CatalogId catId; |
| 4023 |
char namebuf[32]; |
| 4024 |
|
| 4025 |
/* Build identifying info for this blob */ |
| 4026 |
catId.tableoid = loinfo->dobj.catId.tableoid; |
| 4027 |
catId.oid = loinfo->looids[i]; |
| 4028 |
snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[i]); |
| 4029 |
|
| 4030 |
if (loinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 4031 |
dumpComment(fout, "LARGE OBJECT", namebuf, |
| 4032 |
NULL, loinfo->rolname, |
| 4033 |
catId, 0, loinfo->dobj.dumpId); |
| 4034 |
|
| 4035 |
if (loinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 4036 |
dumpSecLabel(fout, "LARGE OBJECT", namebuf, |
| 4037 |
NULL, loinfo->rolname, |
| 4038 |
catId, 0, loinfo->dobj.dumpId); |
| 4039 |
} |
| 4040 |
} |
| 4041 |
|
| 4042 |
/* |
| 4043 |
* Dump the ACLs if any (remember that all blobs in the group will have |
| 4044 |
* the same ACL). If there's just one blob, dump a simple ACL entry; if |
| 4045 |
* there's more, make a "LARGE OBJECTS" entry that really contains only |
| 4046 |
* the ACL for the first blob. _printTocEntry() will be cued by the tag |
| 4047 |
* string to emit a mutated version for each blob. |
| 4048 |
*/ |
| 4049 |
if (loinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 4050 |
{ |
| 4051 |
char namebuf[32]; |
| 4052 |
|
| 4053 |
/* Build identifying info for the first blob */ |
| 4054 |
snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[0]); |
| 4055 |
|
| 4056 |
if (loinfo->numlos > 1) |
| 4057 |
{ |
| 4058 |
char tagbuf[64]; |
| 4059 |
|
| 4060 |
snprintf(tagbuf, sizeof(tagbuf), "LARGE OBJECTS %u..%u", |
| 4061 |
loinfo->looids[0], loinfo->looids[loinfo->numlos - 1]); |
| 4062 |
|
| 4063 |
dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId, |
| 4064 |
"LARGE OBJECT", namebuf, NULL, NULL, |
| 4065 |
tagbuf, loinfo->rolname, &loinfo->dacl); |
| 4066 |
} |
| 4067 |
else |
| 4068 |
{ |
| 4069 |
dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId, |
| 4070 |
"LARGE OBJECT", namebuf, NULL, NULL, |
| 4071 |
NULL, loinfo->rolname, &loinfo->dacl); |
| 4072 |
} |
| 4073 |
} |
| 4074 |
|
| 4075 |
destroyPQExpBuffer(cquery); |
| 4076 |
} |
| 4077 |
|
| 4078 |
/* |
| 4079 |
* dumpLOs: |
| 4080 |
* dump the data contents of the large objects in the given group |
| 4081 |
*/ |
| 4082 |
static int |
| 4083 |
dumpLOs(Archive *fout, const void *arg) |
| 4084 |
{ |
| 4085 |
const LoInfo *loinfo = (const LoInfo *) arg; |
| 4086 |
PGconn *conn = GetConnection(fout); |
| 4087 |
char buf[LOBBUFSIZE]; |
| 4088 |
|
| 4089 |
pg_log_info("saving large objects \"%s\"", loinfo->dobj.name); |
| 4090 |
|
| 4091 |
for (int i = 0; i < loinfo->numlos; i++) |
| 4092 |
{ |
| 4093 |
Oid loOid = loinfo->looids[i]; |
| 4094 |
int loFd; |
| 4095 |
int cnt; |
| 4096 |
|
| 4097 |
/* Open the LO */ |
| 4098 |
loFd = lo_open(conn, loOid, INV_READ); |
| 4099 |
if (loFd == -1) |
| 4100 |
pg_fatal("could not open large object %u: %s", |
| 4101 |
loOid, PQerrorMessage(conn)); |
| 4102 |
|
| 4103 |
StartLO(fout, loOid); |
| 4104 |
|
| 4105 |
/* Now read it in chunks, sending data to archive */ |
| 4106 |
do |
| 4107 |
{ |
| 4108 |
cnt = lo_read(conn, loFd, buf, LOBBUFSIZE); |
| 4109 |
if (cnt < 0) |
| 4110 |
pg_fatal("error reading large object %u: %s", |
| 4111 |
loOid, PQerrorMessage(conn)); |
| 4112 |
|
| 4113 |
WriteData(fout, buf, cnt); |
| 4114 |
} while (cnt > 0); |
| 4115 |
|
| 4116 |
lo_close(conn, loFd); |
| 4117 |
|
| 4118 |
EndLO(fout, loOid); |
| 4119 |
} |
| 4120 |
|
| 4121 |
return 1; |
| 4122 |
} |
| 4123 |
|
| 4124 |
/* |
| 4125 |
* getPolicies |
| 4126 |
* get information about all RLS policies on dumpable tables. |
| 4127 |
*/ |
| 4128 |
void |
| 4129 |
getPolicies(Archive *fout, TableInfo tblinfo[], int numTables) |
| 4130 |
{ |
| 4131 |
DumpOptions *dopt = fout->dopt; |
| 4132 |
PQExpBuffer query; |
| 4133 |
PQExpBuffer tbloids; |
| 4134 |
PGresult *res; |
| 4135 |
PolicyInfo *polinfo; |
| 4136 |
int i_oid; |
| 4137 |
int i_tableoid; |
| 4138 |
int i_polrelid; |
| 4139 |
int i_polname; |
| 4140 |
int i_polcmd; |
| 4141 |
int i_polpermissive; |
| 4142 |
int i_polroles; |
| 4143 |
int i_polqual; |
| 4144 |
int i_polwithcheck; |
| 4145 |
int i, |
| 4146 |
j, |
| 4147 |
ntups; |
| 4148 |
|
| 4149 |
/* No policies before 9.5 */ |
| 4150 |
if (fout->remoteVersion < 90500) |
| 4151 |
return; |
| 4152 |
|
| 4153 |
/* Skip if --no-policies was specified */ |
| 4154 |
if (dopt->no_policies) |
| 4155 |
return; |
| 4156 |
|
| 4157 |
query = createPQExpBuffer(); |
| 4158 |
tbloids = createPQExpBuffer(); |
| 4159 |
|
| 4160 |
/* |
| 4161 |
* Identify tables of interest, and check which ones have RLS enabled. |
| 4162 |
*/ |
| 4163 |
appendPQExpBufferChar(tbloids, '{'); |
| 4164 |
for (i = 0; i < numTables; i++) |
| 4165 |
{ |
| 4166 |
TableInfo *tbinfo = &tblinfo[i]; |
| 4167 |
|
| 4168 |
/* Ignore row security on tables not to be dumped */ |
| 4169 |
if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY)) |
| 4170 |
continue; |
| 4171 |
|
| 4172 |
/* It can't have RLS or policies if it's not a table */ |
| 4173 |
if (tbinfo->relkind != RELKIND_RELATION && |
| 4174 |
tbinfo->relkind != RELKIND_PARTITIONED_TABLE) |
| 4175 |
continue; |
| 4176 |
|
| 4177 |
/* Add it to the list of table OIDs to be probed below */ |
| 4178 |
if (tbloids->len > 1) /* do we have more than the '{'? */ |
| 4179 |
appendPQExpBufferChar(tbloids, ','); |
| 4180 |
appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); |
| 4181 |
|
| 4182 |
/* Is RLS enabled? (That's separate from whether it has policies) */ |
| 4183 |
if (tbinfo->rowsec) |
| 4184 |
{ |
| 4185 |
tbinfo->dobj.components |= DUMP_COMPONENT_POLICY; |
| 4186 |
|
| 4187 |
/* |
| 4188 |
* We represent RLS being enabled on a table by creating a |
| 4189 |
* PolicyInfo object with null polname. |
| 4190 |
* |
| 4191 |
* Note: use tableoid 0 so that this object won't be mistaken for |
| 4192 |
* something that pg_depend entries apply to. |
| 4193 |
*/ |
| 4194 |
polinfo = pg_malloc(sizeof(PolicyInfo)); |
| 4195 |
polinfo->dobj.objType = DO_POLICY; |
| 4196 |
polinfo->dobj.catId.tableoid = 0; |
| 4197 |
polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid; |
| 4198 |
AssignDumpId(&polinfo->dobj); |
| 4199 |
polinfo->dobj.namespace = tbinfo->dobj.namespace; |
| 4200 |
polinfo->dobj.name = pg_strdup(tbinfo->dobj.name); |
| 4201 |
polinfo->poltable = tbinfo; |
| 4202 |
polinfo->polname = NULL; |
| 4203 |
polinfo->polcmd = '\0'; |
| 4204 |
polinfo->polpermissive = 0; |
| 4205 |
polinfo->polroles = NULL; |
| 4206 |
polinfo->polqual = NULL; |
| 4207 |
polinfo->polwithcheck = NULL; |
| 4208 |
} |
| 4209 |
} |
| 4210 |
appendPQExpBufferChar(tbloids, '}'); |
| 4211 |
|
| 4212 |
/* |
| 4213 |
* Now, read all RLS policies belonging to the tables of interest, and |
| 4214 |
* create PolicyInfo objects for them. (Note that we must filter the |
| 4215 |
* results server-side not locally, because we dare not apply pg_get_expr |
| 4216 |
* to tables we don't have lock on.) |
| 4217 |
*/ |
| 4218 |
pg_log_info("reading row-level security policies"); |
| 4219 |
|
| 4220 |
printfPQExpBuffer(query, |
| 4221 |
"SELECT pol.oid, pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, "); |
| 4222 |
if (fout->remoteVersion >= 100000) |
| 4223 |
appendPQExpBufferStr(query, "pol.polpermissive, "); |
| 4224 |
else |
| 4225 |
appendPQExpBufferStr(query, "'t' as polpermissive, "); |
| 4226 |
appendPQExpBuffer(query, |
| 4227 |
"CASE WHEN pol.polroles = '{0}' THEN NULL ELSE " |
| 4228 |
" 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, " |
| 4229 |
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, " |
| 4230 |
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck " |
| 4231 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 4232 |
"JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)", |
| 4233 |
tbloids->data); |
| 4234 |
|
| 4235 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 4236 |
|
| 4237 |
ntups = PQntuples(res); |
| 4238 |
if (ntups > 0) |
| 4239 |
{ |
| 4240 |
i_oid = PQfnumber(res, "oid"); |
| 4241 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 4242 |
i_polrelid = PQfnumber(res, "polrelid"); |
| 4243 |
i_polname = PQfnumber(res, "polname"); |
| 4244 |
i_polcmd = PQfnumber(res, "polcmd"); |
| 4245 |
i_polpermissive = PQfnumber(res, "polpermissive"); |
| 4246 |
i_polroles = PQfnumber(res, "polroles"); |
| 4247 |
i_polqual = PQfnumber(res, "polqual"); |
| 4248 |
i_polwithcheck = PQfnumber(res, "polwithcheck"); |
| 4249 |
|
| 4250 |
polinfo = pg_malloc(ntups * sizeof(PolicyInfo)); |
| 4251 |
|
| 4252 |
for (j = 0; j < ntups; j++) |
| 4253 |
{ |
| 4254 |
Oid polrelid = atooid(PQgetvalue(res, j, i_polrelid)); |
| 4255 |
TableInfo *tbinfo = findTableByOid(polrelid); |
| 4256 |
|
| 4257 |
tbinfo->dobj.components |= DUMP_COMPONENT_POLICY; |
| 4258 |
|
| 4259 |
polinfo[j].dobj.objType = DO_POLICY; |
| 4260 |
polinfo[j].dobj.catId.tableoid = |
| 4261 |
atooid(PQgetvalue(res, j, i_tableoid)); |
| 4262 |
polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); |
| 4263 |
AssignDumpId(&polinfo[j].dobj); |
| 4264 |
polinfo[j].dobj.namespace = tbinfo->dobj.namespace; |
| 4265 |
polinfo[j].poltable = tbinfo; |
| 4266 |
polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname)); |
| 4267 |
polinfo[j].dobj.name = pg_strdup(polinfo[j].polname); |
| 4268 |
|
| 4269 |
polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd)); |
| 4270 |
polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't'; |
| 4271 |
|
| 4272 |
if (PQgetisnull(res, j, i_polroles)) |
| 4273 |
polinfo[j].polroles = NULL; |
| 4274 |
else |
| 4275 |
polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles)); |
| 4276 |
|
| 4277 |
if (PQgetisnull(res, j, i_polqual)) |
| 4278 |
polinfo[j].polqual = NULL; |
| 4279 |
else |
| 4280 |
polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual)); |
| 4281 |
|
| 4282 |
if (PQgetisnull(res, j, i_polwithcheck)) |
| 4283 |
polinfo[j].polwithcheck = NULL; |
| 4284 |
else |
| 4285 |
polinfo[j].polwithcheck |
| 4286 |
= pg_strdup(PQgetvalue(res, j, i_polwithcheck)); |
| 4287 |
} |
| 4288 |
} |
| 4289 |
|
| 4290 |
PQclear(res); |
| 4291 |
|
| 4292 |
destroyPQExpBuffer(query); |
| 4293 |
destroyPQExpBuffer(tbloids); |
| 4294 |
} |
| 4295 |
|
| 4296 |
/* |
| 4297 |
* dumpPolicy |
| 4298 |
* dump the definition of the given policy |
| 4299 |
*/ |
| 4300 |
static void |
| 4301 |
dumpPolicy(Archive *fout, const PolicyInfo *polinfo) |
| 4302 |
{ |
| 4303 |
DumpOptions *dopt = fout->dopt; |
| 4304 |
TableInfo *tbinfo = polinfo->poltable; |
| 4305 |
PQExpBuffer query; |
| 4306 |
PQExpBuffer delqry; |
| 4307 |
PQExpBuffer polprefix; |
| 4308 |
char *qtabname; |
| 4309 |
const char *cmd; |
| 4310 |
char *tag; |
| 4311 |
|
| 4312 |
/* Do nothing if not dumping schema */ |
| 4313 |
if (!dopt->dumpSchema) |
| 4314 |
return; |
| 4315 |
|
| 4316 |
/* |
| 4317 |
* If polname is NULL, then this record is just indicating that ROW LEVEL |
| 4318 |
* SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE |
| 4319 |
* ROW LEVEL SECURITY. |
| 4320 |
*/ |
| 4321 |
if (polinfo->polname == NULL) |
| 4322 |
{ |
| 4323 |
query = createPQExpBuffer(); |
| 4324 |
|
| 4325 |
appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;", |
| 4326 |
fmtQualifiedDumpable(tbinfo)); |
| 4327 |
|
| 4328 |
/* |
| 4329 |
* We must emit the ROW SECURITY object's dependency on its table |
| 4330 |
* explicitly, because it will not match anything in pg_depend (unlike |
| 4331 |
* the case for other PolicyInfo objects). |
| 4332 |
*/ |
| 4333 |
if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 4334 |
ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId, |
| 4335 |
ARCHIVE_OPTS(.tag = polinfo->dobj.name, |
| 4336 |
.namespace = polinfo->dobj.namespace->dobj.name, |
| 4337 |
.owner = tbinfo->rolname, |
| 4338 |
.description = "ROW SECURITY", |
| 4339 |
.section = SECTION_POST_DATA, |
| 4340 |
.createStmt = query->data, |
| 4341 |
.deps = &(tbinfo->dobj.dumpId), |
| 4342 |
.nDeps = 1)); |
| 4343 |
|
| 4344 |
destroyPQExpBuffer(query); |
| 4345 |
return; |
| 4346 |
} |
| 4347 |
|
| 4348 |
if (polinfo->polcmd == '*') |
| 4349 |
cmd = ""; |
| 4350 |
else if (polinfo->polcmd == 'r') |
| 4351 |
cmd = " FOR SELECT"; |
| 4352 |
else if (polinfo->polcmd == 'a') |
| 4353 |
cmd = " FOR INSERT"; |
| 4354 |
else if (polinfo->polcmd == 'w') |
| 4355 |
cmd = " FOR UPDATE"; |
| 4356 |
else if (polinfo->polcmd == 'd') |
| 4357 |
cmd = " FOR DELETE"; |
| 4358 |
else |
| 4359 |
pg_fatal("unexpected policy command type: %c", |
| 4360 |
polinfo->polcmd); |
| 4361 |
|
| 4362 |
query = createPQExpBuffer(); |
| 4363 |
delqry = createPQExpBuffer(); |
| 4364 |
polprefix = createPQExpBuffer(); |
| 4365 |
|
| 4366 |
qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); |
| 4367 |
|
| 4368 |
appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname)); |
| 4369 |
|
| 4370 |
appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo), |
| 4371 |
!polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd); |
| 4372 |
|
| 4373 |
if (polinfo->polroles != NULL) |
| 4374 |
appendPQExpBuffer(query, " TO %s", polinfo->polroles); |
| 4375 |
|
| 4376 |
if (polinfo->polqual != NULL) |
| 4377 |
appendPQExpBuffer(query, " USING (%s)", polinfo->polqual); |
| 4378 |
|
| 4379 |
if (polinfo->polwithcheck != NULL) |
| 4380 |
appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck); |
| 4381 |
|
| 4382 |
appendPQExpBufferStr(query, ";\n"); |
| 4383 |
|
| 4384 |
appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname)); |
| 4385 |
appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo)); |
| 4386 |
|
| 4387 |
appendPQExpBuffer(polprefix, "POLICY %s ON", |
| 4388 |
fmtId(polinfo->polname)); |
| 4389 |
|
| 4390 |
tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name); |
| 4391 |
|
| 4392 |
if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 4393 |
ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId, |
| 4394 |
ARCHIVE_OPTS(.tag = tag, |
| 4395 |
.namespace = polinfo->dobj.namespace->dobj.name, |
| 4396 |
.owner = tbinfo->rolname, |
| 4397 |
.description = "POLICY", |
| 4398 |
.section = SECTION_POST_DATA, |
| 4399 |
.createStmt = query->data, |
| 4400 |
.dropStmt = delqry->data)); |
| 4401 |
|
| 4402 |
if (polinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 4403 |
dumpComment(fout, polprefix->data, qtabname, |
| 4404 |
tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, |
| 4405 |
polinfo->dobj.catId, 0, polinfo->dobj.dumpId); |
| 4406 |
|
| 4407 |
free(tag); |
| 4408 |
destroyPQExpBuffer(query); |
| 4409 |
destroyPQExpBuffer(delqry); |
| 4410 |
destroyPQExpBuffer(polprefix); |
| 4411 |
free(qtabname); |
| 4412 |
} |
| 4413 |
|
| 4414 |
/* |
| 4415 |
* getPublications |
| 4416 |
* get information about publications |
| 4417 |
*/ |
| 4418 |
void |
| 4419 |
getPublications(Archive *fout) |
| 4420 |
{ |
| 4421 |
DumpOptions *dopt = fout->dopt; |
| 4422 |
PQExpBuffer query; |
| 4423 |
PGresult *res; |
| 4424 |
PublicationInfo *pubinfo; |
| 4425 |
int i_tableoid; |
| 4426 |
int i_oid; |
| 4427 |
int i_pubname; |
| 4428 |
int i_pubowner; |
| 4429 |
int i_puballtables; |
| 4430 |
int i_pubinsert; |
| 4431 |
int i_pubupdate; |
| 4432 |
int i_pubdelete; |
| 4433 |
int i_pubtruncate; |
| 4434 |
int i_pubviaroot; |
| 4435 |
int i_pubgencols; |
| 4436 |
int i, |
| 4437 |
ntups; |
| 4438 |
|
| 4439 |
if (dopt->no_publications || fout->remoteVersion < 100000) |
| 4440 |
return; |
| 4441 |
|
| 4442 |
query = createPQExpBuffer(); |
| 4443 |
|
| 4444 |
/* Get the publications. */ |
| 4445 |
appendPQExpBufferStr(query, "SELECT p.tableoid, p.oid, p.pubname, " |
| 4446 |
"p.pubowner, p.puballtables, p.pubinsert, " |
| 4447 |
"p.pubupdate, p.pubdelete, "); |
| 4448 |
|
| 4449 |
if (fout->remoteVersion >= 110000) |
| 4450 |
appendPQExpBufferStr(query, "p.pubtruncate, "); |
| 4451 |
else |
| 4452 |
appendPQExpBufferStr(query, "false AS pubtruncate, "); |
| 4453 |
|
| 4454 |
if (fout->remoteVersion >= 130000) |
| 4455 |
appendPQExpBufferStr(query, "p.pubviaroot, "); |
| 4456 |
else |
| 4457 |
appendPQExpBufferStr(query, "false AS pubviaroot, "); |
| 4458 |
|
| 4459 |
if (fout->remoteVersion >= 180000) |
| 4460 |
appendPQExpBufferStr(query, "p.pubgencols "); |
| 4461 |
else |
| 4462 |
appendPQExpBuffer(query, "'%c' AS pubgencols ", PUBLISH_GENCOLS_NONE); |
| 4463 |
|
| 4464 |
appendPQExpBufferStr(query, "FROM pg_publication p"); |
| 4465 |
|
| 4466 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 4467 |
|
| 4468 |
ntups = PQntuples(res); |
| 4469 |
|
| 4470 |
if (ntups == 0) |
| 4471 |
goto cleanup; |
| 4472 |
|
| 4473 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 4474 |
i_oid = PQfnumber(res, "oid"); |
| 4475 |
i_pubname = PQfnumber(res, "pubname"); |
| 4476 |
i_pubowner = PQfnumber(res, "pubowner"); |
| 4477 |
i_puballtables = PQfnumber(res, "puballtables"); |
| 4478 |
i_pubinsert = PQfnumber(res, "pubinsert"); |
| 4479 |
i_pubupdate = PQfnumber(res, "pubupdate"); |
| 4480 |
i_pubdelete = PQfnumber(res, "pubdelete"); |
| 4481 |
i_pubtruncate = PQfnumber(res, "pubtruncate"); |
| 4482 |
i_pubviaroot = PQfnumber(res, "pubviaroot"); |
| 4483 |
i_pubgencols = PQfnumber(res, "pubgencols"); |
| 4484 |
|
| 4485 |
pubinfo = pg_malloc(ntups * sizeof(PublicationInfo)); |
| 4486 |
|
| 4487 |
for (i = 0; i < ntups; i++) |
| 4488 |
{ |
| 4489 |
pubinfo[i].dobj.objType = DO_PUBLICATION; |
| 4490 |
pubinfo[i].dobj.catId.tableoid = |
| 4491 |
atooid(PQgetvalue(res, i, i_tableoid)); |
| 4492 |
pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 4493 |
AssignDumpId(&pubinfo[i].dobj); |
| 4494 |
pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname)); |
| 4495 |
pubinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_pubowner)); |
| 4496 |
pubinfo[i].puballtables = |
| 4497 |
(strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0); |
| 4498 |
pubinfo[i].pubinsert = |
| 4499 |
(strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0); |
| 4500 |
pubinfo[i].pubupdate = |
| 4501 |
(strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0); |
| 4502 |
pubinfo[i].pubdelete = |
| 4503 |
(strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0); |
| 4504 |
pubinfo[i].pubtruncate = |
| 4505 |
(strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0); |
| 4506 |
pubinfo[i].pubviaroot = |
| 4507 |
(strcmp(PQgetvalue(res, i, i_pubviaroot), "t") == 0); |
| 4508 |
pubinfo[i].pubgencols_type = |
| 4509 |
*(PQgetvalue(res, i, i_pubgencols)); |
| 4510 |
|
| 4511 |
/* Decide whether we want to dump it */ |
| 4512 |
selectDumpableObject(&(pubinfo[i].dobj), fout); |
| 4513 |
} |
| 4514 |
|
| 4515 |
cleanup: |
| 4516 |
PQclear(res); |
| 4517 |
|
| 4518 |
destroyPQExpBuffer(query); |
| 4519 |
} |
| 4520 |
|
| 4521 |
/* |
| 4522 |
* dumpPublication |
| 4523 |
* dump the definition of the given publication |
| 4524 |
*/ |
| 4525 |
static void |
| 4526 |
dumpPublication(Archive *fout, const PublicationInfo *pubinfo) |
| 4527 |
{ |
| 4528 |
DumpOptions *dopt = fout->dopt; |
| 4529 |
PQExpBuffer delq; |
| 4530 |
PQExpBuffer query; |
| 4531 |
char *qpubname; |
| 4532 |
bool first = true; |
| 4533 |
|
| 4534 |
/* Do nothing if not dumping schema */ |
| 4535 |
if (!dopt->dumpSchema) |
| 4536 |
return; |
| 4537 |
|
| 4538 |
delq = createPQExpBuffer(); |
| 4539 |
query = createPQExpBuffer(); |
| 4540 |
|
| 4541 |
qpubname = pg_strdup(fmtId(pubinfo->dobj.name)); |
| 4542 |
|
| 4543 |
appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n", |
| 4544 |
qpubname); |
| 4545 |
|
| 4546 |
appendPQExpBuffer(query, "CREATE PUBLICATION %s", |
| 4547 |
qpubname); |
| 4548 |
|
| 4549 |
if (pubinfo->puballtables) |
| 4550 |
appendPQExpBufferStr(query, " FOR ALL TABLES"); |
| 4551 |
|
| 4552 |
appendPQExpBufferStr(query, " WITH (publish = '"); |
| 4553 |
if (pubinfo->pubinsert) |
| 4554 |
{ |
| 4555 |
appendPQExpBufferStr(query, "insert"); |
| 4556 |
first = false; |
| 4557 |
} |
| 4558 |
|
| 4559 |
if (pubinfo->pubupdate) |
| 4560 |
{ |
| 4561 |
if (!first) |
| 4562 |
appendPQExpBufferStr(query, ", "); |
| 4563 |
|
| 4564 |
appendPQExpBufferStr(query, "update"); |
| 4565 |
first = false; |
| 4566 |
} |
| 4567 |
|
| 4568 |
if (pubinfo->pubdelete) |
| 4569 |
{ |
| 4570 |
if (!first) |
| 4571 |
appendPQExpBufferStr(query, ", "); |
| 4572 |
|
| 4573 |
appendPQExpBufferStr(query, "delete"); |
| 4574 |
first = false; |
| 4575 |
} |
| 4576 |
|
| 4577 |
if (pubinfo->pubtruncate) |
| 4578 |
{ |
| 4579 |
if (!first) |
| 4580 |
appendPQExpBufferStr(query, ", "); |
| 4581 |
|
| 4582 |
appendPQExpBufferStr(query, "truncate"); |
| 4583 |
first = false; |
| 4584 |
} |
| 4585 |
|
| 4586 |
appendPQExpBufferChar(query, '\''); |
| 4587 |
|
| 4588 |
if (pubinfo->pubviaroot) |
| 4589 |
appendPQExpBufferStr(query, ", publish_via_partition_root = true"); |
| 4590 |
|
| 4591 |
if (pubinfo->pubgencols_type == PUBLISH_GENCOLS_STORED) |
| 4592 |
appendPQExpBufferStr(query, ", publish_generated_columns = stored"); |
| 4593 |
|
| 4594 |
appendPQExpBufferStr(query, ");\n"); |
| 4595 |
|
| 4596 |
if (pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 4597 |
ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId, |
| 4598 |
ARCHIVE_OPTS(.tag = pubinfo->dobj.name, |
| 4599 |
.owner = pubinfo->rolname, |
| 4600 |
.description = "PUBLICATION", |
| 4601 |
.section = SECTION_POST_DATA, |
| 4602 |
.createStmt = query->data, |
| 4603 |
.dropStmt = delq->data)); |
| 4604 |
|
| 4605 |
if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 4606 |
dumpComment(fout, "PUBLICATION", qpubname, |
| 4607 |
NULL, pubinfo->rolname, |
| 4608 |
pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId); |
| 4609 |
|
| 4610 |
if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 4611 |
dumpSecLabel(fout, "PUBLICATION", qpubname, |
| 4612 |
NULL, pubinfo->rolname, |
| 4613 |
pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId); |
| 4614 |
|
| 4615 |
destroyPQExpBuffer(delq); |
| 4616 |
destroyPQExpBuffer(query); |
| 4617 |
free(qpubname); |
| 4618 |
} |
| 4619 |
|
| 4620 |
/* |
| 4621 |
* getPublicationNamespaces |
| 4622 |
* get information about publication membership for dumpable schemas. |
| 4623 |
*/ |
| 4624 |
void |
| 4625 |
getPublicationNamespaces(Archive *fout) |
| 4626 |
{ |
| 4627 |
PQExpBuffer query; |
| 4628 |
PGresult *res; |
| 4629 |
PublicationSchemaInfo *pubsinfo; |
| 4630 |
DumpOptions *dopt = fout->dopt; |
| 4631 |
int i_tableoid; |
| 4632 |
int i_oid; |
| 4633 |
int i_pnpubid; |
| 4634 |
int i_pnnspid; |
| 4635 |
int i, |
| 4636 |
j, |
| 4637 |
ntups; |
| 4638 |
|
| 4639 |
if (dopt->no_publications || fout->remoteVersion < 150000) |
| 4640 |
return; |
| 4641 |
|
| 4642 |
query = createPQExpBuffer(); |
| 4643 |
|
| 4644 |
/* Collect all publication membership info. */ |
| 4645 |
appendPQExpBufferStr(query, |
| 4646 |
"SELECT tableoid, oid, pnpubid, pnnspid " |
| 4647 |
"FROM pg_catalog.pg_publication_namespace"); |
| 4648 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 4649 |
|
| 4650 |
ntups = PQntuples(res); |
| 4651 |
|
| 4652 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 4653 |
i_oid = PQfnumber(res, "oid"); |
| 4654 |
i_pnpubid = PQfnumber(res, "pnpubid"); |
| 4655 |
i_pnnspid = PQfnumber(res, "pnnspid"); |
| 4656 |
|
| 4657 |
/* this allocation may be more than we need */ |
| 4658 |
pubsinfo = pg_malloc(ntups * sizeof(PublicationSchemaInfo)); |
| 4659 |
j = 0; |
| 4660 |
|
| 4661 |
for (i = 0; i < ntups; i++) |
| 4662 |
{ |
| 4663 |
Oid pnpubid = atooid(PQgetvalue(res, i, i_pnpubid)); |
| 4664 |
Oid pnnspid = atooid(PQgetvalue(res, i, i_pnnspid)); |
| 4665 |
PublicationInfo *pubinfo; |
| 4666 |
NamespaceInfo *nspinfo; |
| 4667 |
|
| 4668 |
/* |
| 4669 |
* Ignore any entries for which we aren't interested in either the |
| 4670 |
* publication or the rel. |
| 4671 |
*/ |
| 4672 |
pubinfo = findPublicationByOid(pnpubid); |
| 4673 |
if (pubinfo == NULL) |
| 4674 |
continue; |
| 4675 |
nspinfo = findNamespaceByOid(pnnspid); |
| 4676 |
if (nspinfo == NULL) |
| 4677 |
continue; |
| 4678 |
|
| 4679 |
/* OK, make a DumpableObject for this relationship */ |
| 4680 |
pubsinfo[j].dobj.objType = DO_PUBLICATION_TABLE_IN_SCHEMA; |
| 4681 |
pubsinfo[j].dobj.catId.tableoid = |
| 4682 |
atooid(PQgetvalue(res, i, i_tableoid)); |
| 4683 |
pubsinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 4684 |
AssignDumpId(&pubsinfo[j].dobj); |
| 4685 |
pubsinfo[j].dobj.namespace = nspinfo->dobj.namespace; |
| 4686 |
pubsinfo[j].dobj.name = nspinfo->dobj.name; |
| 4687 |
pubsinfo[j].publication = pubinfo; |
| 4688 |
pubsinfo[j].pubschema = nspinfo; |
| 4689 |
|
| 4690 |
/* Decide whether we want to dump it */ |
| 4691 |
selectDumpablePublicationObject(&(pubsinfo[j].dobj), fout); |
| 4692 |
|
| 4693 |
j++; |
| 4694 |
} |
| 4695 |
|
| 4696 |
PQclear(res); |
| 4697 |
destroyPQExpBuffer(query); |
| 4698 |
} |
| 4699 |
|
| 4700 |
/* |
| 4701 |
* getPublicationTables |
| 4702 |
* get information about publication membership for dumpable tables. |
| 4703 |
*/ |
| 4704 |
void |
| 4705 |
getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables) |
| 4706 |
{ |
| 4707 |
PQExpBuffer query; |
| 4708 |
PGresult *res; |
| 4709 |
PublicationRelInfo *pubrinfo; |
| 4710 |
DumpOptions *dopt = fout->dopt; |
| 4711 |
int i_tableoid; |
| 4712 |
int i_oid; |
| 4713 |
int i_prpubid; |
| 4714 |
int i_prrelid; |
| 4715 |
int i_prrelqual; |
| 4716 |
int i_prattrs; |
| 4717 |
int i, |
| 4718 |
j, |
| 4719 |
ntups; |
| 4720 |
|
| 4721 |
if (dopt->no_publications || fout->remoteVersion < 100000) |
| 4722 |
return; |
| 4723 |
|
| 4724 |
query = createPQExpBuffer(); |
| 4725 |
|
| 4726 |
/* Collect all publication membership info. */ |
| 4727 |
if (fout->remoteVersion >= 150000) |
| 4728 |
appendPQExpBufferStr(query, |
| 4729 |
"SELECT tableoid, oid, prpubid, prrelid, " |
| 4730 |
"pg_catalog.pg_get_expr(prqual, prrelid) AS prrelqual, " |
| 4731 |
"(CASE\n" |
| 4732 |
" WHEN pr.prattrs IS NOT NULL THEN\n" |
| 4733 |
" (SELECT array_agg(attname)\n" |
| 4734 |
" FROM\n" |
| 4735 |
" pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n" |
| 4736 |
" pg_catalog.pg_attribute\n" |
| 4737 |
" WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n" |
| 4738 |
" ELSE NULL END) prattrs " |
| 4739 |
"FROM pg_catalog.pg_publication_rel pr"); |
| 4740 |
else |
| 4741 |
appendPQExpBufferStr(query, |
| 4742 |
"SELECT tableoid, oid, prpubid, prrelid, " |
| 4743 |
"NULL AS prrelqual, NULL AS prattrs " |
| 4744 |
"FROM pg_catalog.pg_publication_rel"); |
| 4745 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 4746 |
|
| 4747 |
ntups = PQntuples(res); |
| 4748 |
|
| 4749 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 4750 |
i_oid = PQfnumber(res, "oid"); |
| 4751 |
i_prpubid = PQfnumber(res, "prpubid"); |
| 4752 |
i_prrelid = PQfnumber(res, "prrelid"); |
| 4753 |
i_prrelqual = PQfnumber(res, "prrelqual"); |
| 4754 |
i_prattrs = PQfnumber(res, "prattrs"); |
| 4755 |
|
| 4756 |
/* this allocation may be more than we need */ |
| 4757 |
pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo)); |
| 4758 |
j = 0; |
| 4759 |
|
| 4760 |
for (i = 0; i < ntups; i++) |
| 4761 |
{ |
| 4762 |
Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid)); |
| 4763 |
Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid)); |
| 4764 |
PublicationInfo *pubinfo; |
| 4765 |
TableInfo *tbinfo; |
| 4766 |
|
| 4767 |
/* |
| 4768 |
* Ignore any entries for which we aren't interested in either the |
| 4769 |
* publication or the rel. |
| 4770 |
*/ |
| 4771 |
pubinfo = findPublicationByOid(prpubid); |
| 4772 |
if (pubinfo == NULL) |
| 4773 |
continue; |
| 4774 |
tbinfo = findTableByOid(prrelid); |
| 4775 |
if (tbinfo == NULL) |
| 4776 |
continue; |
| 4777 |
|
| 4778 |
/* OK, make a DumpableObject for this relationship */ |
| 4779 |
pubrinfo[j].dobj.objType = DO_PUBLICATION_REL; |
| 4780 |
pubrinfo[j].dobj.catId.tableoid = |
| 4781 |
atooid(PQgetvalue(res, i, i_tableoid)); |
| 4782 |
pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 4783 |
AssignDumpId(&pubrinfo[j].dobj); |
| 4784 |
pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace; |
| 4785 |
pubrinfo[j].dobj.name = tbinfo->dobj.name; |
| 4786 |
pubrinfo[j].publication = pubinfo; |
| 4787 |
pubrinfo[j].pubtable = tbinfo; |
| 4788 |
if (PQgetisnull(res, i, i_prrelqual)) |
| 4789 |
pubrinfo[j].pubrelqual = NULL; |
| 4790 |
else |
| 4791 |
pubrinfo[j].pubrelqual = pg_strdup(PQgetvalue(res, i, i_prrelqual)); |
| 4792 |
|
| 4793 |
if (!PQgetisnull(res, i, i_prattrs)) |
| 4794 |
{ |
| 4795 |
char **attnames; |
| 4796 |
int nattnames; |
| 4797 |
PQExpBuffer attribs; |
| 4798 |
|
| 4799 |
if (!parsePGArray(PQgetvalue(res, i, i_prattrs), |
| 4800 |
&attnames, &nattnames)) |
| 4801 |
pg_fatal("could not parse %s array", "prattrs"); |
| 4802 |
attribs = createPQExpBuffer(); |
| 4803 |
for (int k = 0; k < nattnames; k++) |
| 4804 |
{ |
| 4805 |
if (k > 0) |
| 4806 |
appendPQExpBufferStr(attribs, ", "); |
| 4807 |
|
| 4808 |
appendPQExpBufferStr(attribs, fmtId(attnames[k])); |
| 4809 |
} |
| 4810 |
pubrinfo[j].pubrattrs = attribs->data; |
| 4811 |
free(attribs); /* but not attribs->data */ |
| 4812 |
free(attnames); |
| 4813 |
} |
| 4814 |
else |
| 4815 |
pubrinfo[j].pubrattrs = NULL; |
| 4816 |
|
| 4817 |
/* Decide whether we want to dump it */ |
| 4818 |
selectDumpablePublicationObject(&(pubrinfo[j].dobj), fout); |
| 4819 |
|
| 4820 |
j++; |
| 4821 |
} |
| 4822 |
|
| 4823 |
PQclear(res); |
| 4824 |
destroyPQExpBuffer(query); |
| 4825 |
} |
| 4826 |
|
| 4827 |
/* |
| 4828 |
* dumpPublicationNamespace |
| 4829 |
* dump the definition of the given publication schema mapping. |
| 4830 |
*/ |
| 4831 |
static void |
| 4832 |
dumpPublicationNamespace(Archive *fout, const PublicationSchemaInfo *pubsinfo) |
| 4833 |
{ |
| 4834 |
DumpOptions *dopt = fout->dopt; |
| 4835 |
NamespaceInfo *schemainfo = pubsinfo->pubschema; |
| 4836 |
PublicationInfo *pubinfo = pubsinfo->publication; |
| 4837 |
PQExpBuffer query; |
| 4838 |
char *tag; |
| 4839 |
|
| 4840 |
/* Do nothing if not dumping schema */ |
| 4841 |
if (!dopt->dumpSchema) |
| 4842 |
return; |
| 4843 |
|
| 4844 |
tag = psprintf("%s %s", pubinfo->dobj.name, schemainfo->dobj.name); |
| 4845 |
|
| 4846 |
query = createPQExpBuffer(); |
| 4847 |
|
| 4848 |
appendPQExpBuffer(query, "ALTER PUBLICATION %s ", fmtId(pubinfo->dobj.name)); |
| 4849 |
appendPQExpBuffer(query, "ADD TABLES IN SCHEMA %s;\n", fmtId(schemainfo->dobj.name)); |
| 4850 |
|
| 4851 |
/* |
| 4852 |
* There is no point in creating drop query as the drop is done by schema |
| 4853 |
* drop. |
| 4854 |
*/ |
| 4855 |
if (pubsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 4856 |
ArchiveEntry(fout, pubsinfo->dobj.catId, pubsinfo->dobj.dumpId, |
| 4857 |
ARCHIVE_OPTS(.tag = tag, |
| 4858 |
.namespace = schemainfo->dobj.name, |
| 4859 |
.owner = pubinfo->rolname, |
| 4860 |
.description = "PUBLICATION TABLES IN SCHEMA", |
| 4861 |
.section = SECTION_POST_DATA, |
| 4862 |
.createStmt = query->data)); |
| 4863 |
|
| 4864 |
/* These objects can't currently have comments or seclabels */ |
| 4865 |
|
| 4866 |
free(tag); |
| 4867 |
destroyPQExpBuffer(query); |
| 4868 |
} |
| 4869 |
|
| 4870 |
/* |
| 4871 |
* dumpPublicationTable |
| 4872 |
* dump the definition of the given publication table mapping |
| 4873 |
*/ |
| 4874 |
static void |
| 4875 |
dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo) |
| 4876 |
{ |
| 4877 |
DumpOptions *dopt = fout->dopt; |
| 4878 |
PublicationInfo *pubinfo = pubrinfo->publication; |
| 4879 |
TableInfo *tbinfo = pubrinfo->pubtable; |
| 4880 |
PQExpBuffer query; |
| 4881 |
char *tag; |
| 4882 |
|
| 4883 |
/* Do nothing if not dumping schema */ |
| 4884 |
if (!dopt->dumpSchema) |
| 4885 |
return; |
| 4886 |
|
| 4887 |
tag = psprintf("%s %s", pubinfo->dobj.name, tbinfo->dobj.name); |
| 4888 |
|
| 4889 |
query = createPQExpBuffer(); |
| 4890 |
|
| 4891 |
appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY", |
| 4892 |
fmtId(pubinfo->dobj.name)); |
| 4893 |
appendPQExpBuffer(query, " %s", |
| 4894 |
fmtQualifiedDumpable(tbinfo)); |
| 4895 |
|
| 4896 |
if (pubrinfo->pubrattrs) |
| 4897 |
appendPQExpBuffer(query, " (%s)", pubrinfo->pubrattrs); |
| 4898 |
|
| 4899 |
if (pubrinfo->pubrelqual) |
| 4900 |
{ |
| 4901 |
/* |
| 4902 |
* It's necessary to add parentheses around the expression because |
| 4903 |
* pg_get_expr won't supply the parentheses for things like WHERE |
| 4904 |
* TRUE. |
| 4905 |
*/ |
| 4906 |
appendPQExpBuffer(query, " WHERE (%s)", pubrinfo->pubrelqual); |
| 4907 |
} |
| 4908 |
appendPQExpBufferStr(query, ";\n"); |
| 4909 |
|
| 4910 |
/* |
| 4911 |
* There is no point in creating a drop query as the drop is done by table |
| 4912 |
* drop. (If you think to change this, see also _printTocEntry().) |
| 4913 |
* Although this object doesn't really have ownership as such, set the |
| 4914 |
* owner field anyway to ensure that the command is run by the correct |
| 4915 |
* role at restore time. |
| 4916 |
*/ |
| 4917 |
if (pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 4918 |
ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId, |
| 4919 |
ARCHIVE_OPTS(.tag = tag, |
| 4920 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 4921 |
.owner = pubinfo->rolname, |
| 4922 |
.description = "PUBLICATION TABLE", |
| 4923 |
.section = SECTION_POST_DATA, |
| 4924 |
.createStmt = query->data)); |
| 4925 |
|
| 4926 |
/* These objects can't currently have comments or seclabels */ |
| 4927 |
|
| 4928 |
free(tag); |
| 4929 |
destroyPQExpBuffer(query); |
| 4930 |
} |
| 4931 |
|
| 4932 |
/* |
| 4933 |
* Is the currently connected user a superuser? |
| 4934 |
*/ |
| 4935 |
static bool |
| 4936 |
is_superuser(Archive *fout) |
| 4937 |
{ |
| 4938 |
ArchiveHandle *AH = (ArchiveHandle *) fout; |
| 4939 |
const char *val; |
| 4940 |
|
| 4941 |
val = PQparameterStatus(AH->connection, "is_superuser"); |
| 4942 |
|
| 4943 |
if (val && strcmp(val, "on") == 0) |
| 4944 |
return true; |
| 4945 |
|
| 4946 |
return false; |
| 4947 |
} |
| 4948 |
|
| 4949 |
/* |
| 4950 |
* Set the given value to restrict_nonsystem_relation_kind value. Since |
| 4951 |
* restrict_nonsystem_relation_kind is introduced in minor version releases, |
| 4952 |
* the setting query is effective only where available. |
| 4953 |
*/ |
| 4954 |
static void |
| 4955 |
set_restrict_relation_kind(Archive *AH, const char *value) |
| 4956 |
{ |
| 4957 |
PQExpBuffer query = createPQExpBuffer(); |
| 4958 |
PGresult *res; |
| 4959 |
|
| 4960 |
appendPQExpBuffer(query, |
| 4961 |
"SELECT set_config(name, '%s', false) " |
| 4962 |
"FROM pg_settings " |
| 4963 |
"WHERE name = 'restrict_nonsystem_relation_kind'", |
| 4964 |
value); |
| 4965 |
res = ExecuteSqlQuery(AH, query->data, PGRES_TUPLES_OK); |
| 4966 |
|
| 4967 |
PQclear(res); |
| 4968 |
destroyPQExpBuffer(query); |
| 4969 |
} |
| 4970 |
|
| 4971 |
/* |
| 4972 |
* getSubscriptions |
| 4973 |
* get information about subscriptions |
| 4974 |
*/ |
| 4975 |
void |
| 4976 |
getSubscriptions(Archive *fout) |
| 4977 |
{ |
| 4978 |
DumpOptions *dopt = fout->dopt; |
| 4979 |
PQExpBuffer query; |
| 4980 |
PGresult *res; |
| 4981 |
SubscriptionInfo *subinfo; |
| 4982 |
int i_tableoid; |
| 4983 |
int i_oid; |
| 4984 |
int i_subname; |
| 4985 |
int i_subowner; |
| 4986 |
int i_subbinary; |
| 4987 |
int i_substream; |
| 4988 |
int i_subtwophasestate; |
| 4989 |
int i_subdisableonerr; |
| 4990 |
int i_subpasswordrequired; |
| 4991 |
int i_subrunasowner; |
| 4992 |
int i_subconninfo; |
| 4993 |
int i_subslotname; |
| 4994 |
int i_subsynccommit; |
| 4995 |
int i_subpublications; |
| 4996 |
int i_suborigin; |
| 4997 |
int i_suboriginremotelsn; |
| 4998 |
int i_subenabled; |
| 4999 |
int i_subfailover; |
| 5000 |
int i, |
| 5001 |
ntups; |
| 5002 |
|
| 5003 |
if (dopt->no_subscriptions || fout->remoteVersion < 100000) |
| 5004 |
return; |
| 5005 |
|
| 5006 |
if (!is_superuser(fout)) |
| 5007 |
{ |
| 5008 |
int n; |
| 5009 |
|
| 5010 |
res = ExecuteSqlQuery(fout, |
| 5011 |
"SELECT count(*) FROM pg_subscription " |
| 5012 |
"WHERE subdbid = (SELECT oid FROM pg_database" |
| 5013 |
" WHERE datname = current_database())", |
| 5014 |
PGRES_TUPLES_OK); |
| 5015 |
n = atoi(PQgetvalue(res, 0, 0)); |
| 5016 |
if (n > 0) |
| 5017 |
pg_log_warning("subscriptions not dumped because current user is not a superuser"); |
| 5018 |
PQclear(res); |
| 5019 |
return; |
| 5020 |
} |
| 5021 |
|
| 5022 |
query = createPQExpBuffer(); |
| 5023 |
|
| 5024 |
/* Get the subscriptions in current database. */ |
| 5025 |
appendPQExpBufferStr(query, |
| 5026 |
"SELECT s.tableoid, s.oid, s.subname,\n" |
| 5027 |
" s.subowner,\n" |
| 5028 |
" s.subconninfo, s.subslotname, s.subsynccommit,\n" |
| 5029 |
" s.subpublications,\n"); |
| 5030 |
|
| 5031 |
if (fout->remoteVersion >= 140000) |
| 5032 |
appendPQExpBufferStr(query, " s.subbinary,\n"); |
| 5033 |
else |
| 5034 |
appendPQExpBufferStr(query, " false AS subbinary,\n"); |
| 5035 |
|
| 5036 |
if (fout->remoteVersion >= 140000) |
| 5037 |
appendPQExpBufferStr(query, " s.substream,\n"); |
| 5038 |
else |
| 5039 |
appendPQExpBufferStr(query, " 'f' AS substream,\n"); |
| 5040 |
|
| 5041 |
if (fout->remoteVersion >= 150000) |
| 5042 |
appendPQExpBufferStr(query, |
| 5043 |
" s.subtwophasestate,\n" |
| 5044 |
" s.subdisableonerr,\n"); |
| 5045 |
else |
| 5046 |
appendPQExpBuffer(query, |
| 5047 |
" '%c' AS subtwophasestate,\n" |
| 5048 |
" false AS subdisableonerr,\n", |
| 5049 |
LOGICALREP_TWOPHASE_STATE_DISABLED); |
| 5050 |
|
| 5051 |
if (fout->remoteVersion >= 160000) |
| 5052 |
appendPQExpBufferStr(query, |
| 5053 |
" s.subpasswordrequired,\n" |
| 5054 |
" s.subrunasowner,\n" |
| 5055 |
" s.suborigin,\n"); |
| 5056 |
else |
| 5057 |
appendPQExpBuffer(query, |
| 5058 |
" 't' AS subpasswordrequired,\n" |
| 5059 |
" 't' AS subrunasowner,\n" |
| 5060 |
" '%s' AS suborigin,\n", |
| 5061 |
LOGICALREP_ORIGIN_ANY); |
| 5062 |
|
| 5063 |
if (dopt->binary_upgrade && fout->remoteVersion >= 170000) |
| 5064 |
appendPQExpBufferStr(query, " o.remote_lsn AS suboriginremotelsn,\n" |
| 5065 |
" s.subenabled,\n"); |
| 5066 |
else |
| 5067 |
appendPQExpBufferStr(query, " NULL AS suboriginremotelsn,\n" |
| 5068 |
" false AS subenabled,\n"); |
| 5069 |
|
| 5070 |
if (fout->remoteVersion >= 170000) |
| 5071 |
appendPQExpBufferStr(query, |
| 5072 |
" s.subfailover\n"); |
| 5073 |
else |
| 5074 |
appendPQExpBufferStr(query, |
| 5075 |
" false AS subfailover\n"); |
| 5076 |
|
| 5077 |
appendPQExpBufferStr(query, |
| 5078 |
"FROM pg_subscription s\n"); |
| 5079 |
|
| 5080 |
if (dopt->binary_upgrade && fout->remoteVersion >= 170000) |
| 5081 |
appendPQExpBufferStr(query, |
| 5082 |
"LEFT JOIN pg_catalog.pg_replication_origin_status o \n" |
| 5083 |
" ON o.external_id = 'pg_' || s.oid::text \n"); |
| 5084 |
|
| 5085 |
appendPQExpBufferStr(query, |
| 5086 |
"WHERE s.subdbid = (SELECT oid FROM pg_database\n" |
| 5087 |
" WHERE datname = current_database())"); |
| 5088 |
|
| 5089 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 5090 |
|
| 5091 |
ntups = PQntuples(res); |
| 5092 |
|
| 5093 |
/* |
| 5094 |
* Get subscription fields. We don't include subskiplsn in the dump as |
| 5095 |
* after restoring the dump this value may no longer be relevant. |
| 5096 |
*/ |
| 5097 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 5098 |
i_oid = PQfnumber(res, "oid"); |
| 5099 |
i_subname = PQfnumber(res, "subname"); |
| 5100 |
i_subowner = PQfnumber(res, "subowner"); |
| 5101 |
i_subenabled = PQfnumber(res, "subenabled"); |
| 5102 |
i_subbinary = PQfnumber(res, "subbinary"); |
| 5103 |
i_substream = PQfnumber(res, "substream"); |
| 5104 |
i_subtwophasestate = PQfnumber(res, "subtwophasestate"); |
| 5105 |
i_subdisableonerr = PQfnumber(res, "subdisableonerr"); |
| 5106 |
i_subpasswordrequired = PQfnumber(res, "subpasswordrequired"); |
| 5107 |
i_subrunasowner = PQfnumber(res, "subrunasowner"); |
| 5108 |
i_subfailover = PQfnumber(res, "subfailover"); |
| 5109 |
i_subconninfo = PQfnumber(res, "subconninfo"); |
| 5110 |
i_subslotname = PQfnumber(res, "subslotname"); |
| 5111 |
i_subsynccommit = PQfnumber(res, "subsynccommit"); |
| 5112 |
i_subpublications = PQfnumber(res, "subpublications"); |
| 5113 |
i_suborigin = PQfnumber(res, "suborigin"); |
| 5114 |
i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn"); |
| 5115 |
|
| 5116 |
subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo)); |
| 5117 |
|
| 5118 |
for (i = 0; i < ntups; i++) |
| 5119 |
{ |
| 5120 |
subinfo[i].dobj.objType = DO_SUBSCRIPTION; |
| 5121 |
subinfo[i].dobj.catId.tableoid = |
| 5122 |
atooid(PQgetvalue(res, i, i_tableoid)); |
| 5123 |
subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 5124 |
AssignDumpId(&subinfo[i].dobj); |
| 5125 |
subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname)); |
| 5126 |
subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner)); |
| 5127 |
|
| 5128 |
subinfo[i].subenabled = |
| 5129 |
(strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0); |
| 5130 |
subinfo[i].subbinary = |
| 5131 |
(strcmp(PQgetvalue(res, i, i_subbinary), "t") == 0); |
| 5132 |
subinfo[i].substream = *(PQgetvalue(res, i, i_substream)); |
| 5133 |
subinfo[i].subtwophasestate = *(PQgetvalue(res, i, i_subtwophasestate)); |
| 5134 |
subinfo[i].subdisableonerr = |
| 5135 |
(strcmp(PQgetvalue(res, i, i_subdisableonerr), "t") == 0); |
| 5136 |
subinfo[i].subpasswordrequired = |
| 5137 |
(strcmp(PQgetvalue(res, i, i_subpasswordrequired), "t") == 0); |
| 5138 |
subinfo[i].subrunasowner = |
| 5139 |
(strcmp(PQgetvalue(res, i, i_subrunasowner), "t") == 0); |
| 5140 |
subinfo[i].subfailover = |
| 5141 |
(strcmp(PQgetvalue(res, i, i_subfailover), "t") == 0); |
| 5142 |
subinfo[i].subconninfo = |
| 5143 |
pg_strdup(PQgetvalue(res, i, i_subconninfo)); |
| 5144 |
if (PQgetisnull(res, i, i_subslotname)) |
| 5145 |
subinfo[i].subslotname = NULL; |
| 5146 |
else |
| 5147 |
subinfo[i].subslotname = |
| 5148 |
pg_strdup(PQgetvalue(res, i, i_subslotname)); |
| 5149 |
subinfo[i].subsynccommit = |
| 5150 |
pg_strdup(PQgetvalue(res, i, i_subsynccommit)); |
| 5151 |
subinfo[i].subpublications = |
| 5152 |
pg_strdup(PQgetvalue(res, i, i_subpublications)); |
| 5153 |
subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin)); |
| 5154 |
if (PQgetisnull(res, i, i_suboriginremotelsn)) |
| 5155 |
subinfo[i].suboriginremotelsn = NULL; |
| 5156 |
else |
| 5157 |
subinfo[i].suboriginremotelsn = |
| 5158 |
pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn)); |
| 5159 |
|
| 5160 |
/* Decide whether we want to dump it */ |
| 5161 |
selectDumpableObject(&(subinfo[i].dobj), fout); |
| 5162 |
} |
| 5163 |
PQclear(res); |
| 5164 |
|
| 5165 |
destroyPQExpBuffer(query); |
| 5166 |
} |
| 5167 |
|
| 5168 |
/* |
| 5169 |
* getSubscriptionTables |
| 5170 |
* Get information about subscription membership for dumpable tables. This |
| 5171 |
* will be used only in binary-upgrade mode for PG17 or later versions. |
| 5172 |
*/ |
| 5173 |
void |
| 5174 |
getSubscriptionTables(Archive *fout) |
| 5175 |
{ |
| 5176 |
DumpOptions *dopt = fout->dopt; |
| 5177 |
SubscriptionInfo *subinfo = NULL; |
| 5178 |
SubRelInfo *subrinfo; |
| 5179 |
PGresult *res; |
| 5180 |
int i_srsubid; |
| 5181 |
int i_srrelid; |
| 5182 |
int i_srsubstate; |
| 5183 |
int i_srsublsn; |
| 5184 |
int ntups; |
| 5185 |
Oid last_srsubid = InvalidOid; |
| 5186 |
|
| 5187 |
if (dopt->no_subscriptions || !dopt->binary_upgrade || |
| 5188 |
fout->remoteVersion < 170000) |
| 5189 |
return; |
| 5190 |
|
| 5191 |
res = ExecuteSqlQuery(fout, |
| 5192 |
"SELECT srsubid, srrelid, srsubstate, srsublsn " |
| 5193 |
"FROM pg_catalog.pg_subscription_rel " |
| 5194 |
"ORDER BY srsubid", |
| 5195 |
PGRES_TUPLES_OK); |
| 5196 |
ntups = PQntuples(res); |
| 5197 |
if (ntups == 0) |
| 5198 |
goto cleanup; |
| 5199 |
|
| 5200 |
/* Get pg_subscription_rel attributes */ |
| 5201 |
i_srsubid = PQfnumber(res, "srsubid"); |
| 5202 |
i_srrelid = PQfnumber(res, "srrelid"); |
| 5203 |
i_srsubstate = PQfnumber(res, "srsubstate"); |
| 5204 |
i_srsublsn = PQfnumber(res, "srsublsn"); |
| 5205 |
|
| 5206 |
subrinfo = pg_malloc(ntups * sizeof(SubRelInfo)); |
| 5207 |
for (int i = 0; i < ntups; i++) |
| 5208 |
{ |
| 5209 |
Oid cur_srsubid = atooid(PQgetvalue(res, i, i_srsubid)); |
| 5210 |
Oid relid = atooid(PQgetvalue(res, i, i_srrelid)); |
| 5211 |
TableInfo *tblinfo; |
| 5212 |
|
| 5213 |
/* |
| 5214 |
* If we switched to a new subscription, check if the subscription |
| 5215 |
* exists. |
| 5216 |
*/ |
| 5217 |
if (cur_srsubid != last_srsubid) |
| 5218 |
{ |
| 5219 |
subinfo = findSubscriptionByOid(cur_srsubid); |
| 5220 |
if (subinfo == NULL) |
| 5221 |
pg_fatal("subscription with OID %u does not exist", cur_srsubid); |
| 5222 |
|
| 5223 |
last_srsubid = cur_srsubid; |
| 5224 |
} |
| 5225 |
|
| 5226 |
tblinfo = findTableByOid(relid); |
| 5227 |
if (tblinfo == NULL) |
| 5228 |
pg_fatal("failed sanity check, table with OID %u not found", |
| 5229 |
relid); |
| 5230 |
|
| 5231 |
/* OK, make a DumpableObject for this relationship */ |
| 5232 |
subrinfo[i].dobj.objType = DO_SUBSCRIPTION_REL; |
| 5233 |
subrinfo[i].dobj.catId.tableoid = relid; |
| 5234 |
subrinfo[i].dobj.catId.oid = cur_srsubid; |
| 5235 |
AssignDumpId(&subrinfo[i].dobj); |
| 5236 |
subrinfo[i].dobj.namespace = tblinfo->dobj.namespace; |
| 5237 |
subrinfo[i].dobj.name = tblinfo->dobj.name; |
| 5238 |
subrinfo[i].subinfo = subinfo; |
| 5239 |
subrinfo[i].tblinfo = tblinfo; |
| 5240 |
subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0]; |
| 5241 |
if (PQgetisnull(res, i, i_srsublsn)) |
| 5242 |
subrinfo[i].srsublsn = NULL; |
| 5243 |
else |
| 5244 |
subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn)); |
| 5245 |
|
| 5246 |
/* Decide whether we want to dump it */ |
| 5247 |
selectDumpableObject(&(subrinfo[i].dobj), fout); |
| 5248 |
} |
| 5249 |
|
| 5250 |
cleanup: |
| 5251 |
PQclear(res); |
| 5252 |
} |
| 5253 |
|
| 5254 |
/* |
| 5255 |
* dumpSubscriptionTable |
| 5256 |
* Dump the definition of the given subscription table mapping. This will be |
| 5257 |
* used only in binary-upgrade mode for PG17 or later versions. |
| 5258 |
*/ |
| 5259 |
static void |
| 5260 |
dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo) |
| 5261 |
{ |
| 5262 |
DumpOptions *dopt = fout->dopt; |
| 5263 |
SubscriptionInfo *subinfo = subrinfo->subinfo; |
| 5264 |
PQExpBuffer query; |
| 5265 |
char *tag; |
| 5266 |
|
| 5267 |
/* Do nothing if not dumping schema */ |
| 5268 |
if (!dopt->dumpSchema) |
| 5269 |
return; |
| 5270 |
|
| 5271 |
Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000); |
| 5272 |
|
| 5273 |
tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->tblinfo->dobj.name); |
| 5274 |
|
| 5275 |
query = createPQExpBuffer(); |
| 5276 |
|
| 5277 |
if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 5278 |
{ |
| 5279 |
/* |
| 5280 |
* binary_upgrade_add_sub_rel_state will add the subscription relation |
| 5281 |
* to pg_subscription_rel table. This will be used only in |
| 5282 |
* binary-upgrade mode. |
| 5283 |
*/ |
| 5284 |
appendPQExpBufferStr(query, |
| 5285 |
"\n-- For binary upgrade, must preserve the subscriber table.\n"); |
| 5286 |
appendPQExpBufferStr(query, |
| 5287 |
"SELECT pg_catalog.binary_upgrade_add_sub_rel_state("); |
| 5288 |
appendStringLiteralAH(query, subinfo->dobj.name, fout); |
| 5289 |
appendPQExpBuffer(query, |
| 5290 |
", %u, '%c'", |
| 5291 |
subrinfo->tblinfo->dobj.catId.oid, |
| 5292 |
subrinfo->srsubstate); |
| 5293 |
|
| 5294 |
if (subrinfo->srsublsn && subrinfo->srsublsn[0] != '\0') |
| 5295 |
appendPQExpBuffer(query, ", '%s'", subrinfo->srsublsn); |
| 5296 |
else |
| 5297 |
appendPQExpBufferStr(query, ", NULL"); |
| 5298 |
|
| 5299 |
appendPQExpBufferStr(query, ");\n"); |
| 5300 |
} |
| 5301 |
|
| 5302 |
/* |
| 5303 |
* There is no point in creating a drop query as the drop is done by table |
| 5304 |
* drop. (If you think to change this, see also _printTocEntry().) |
| 5305 |
* Although this object doesn't really have ownership as such, set the |
| 5306 |
* owner field anyway to ensure that the command is run by the correct |
| 5307 |
* role at restore time. |
| 5308 |
*/ |
| 5309 |
if (subrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 5310 |
ArchiveEntry(fout, subrinfo->dobj.catId, subrinfo->dobj.dumpId, |
| 5311 |
ARCHIVE_OPTS(.tag = tag, |
| 5312 |
.namespace = subrinfo->tblinfo->dobj.namespace->dobj.name, |
| 5313 |
.owner = subinfo->rolname, |
| 5314 |
.description = "SUBSCRIPTION TABLE", |
| 5315 |
.section = SECTION_POST_DATA, |
| 5316 |
.createStmt = query->data)); |
| 5317 |
|
| 5318 |
/* These objects can't currently have comments or seclabels */ |
| 5319 |
|
| 5320 |
free(tag); |
| 5321 |
destroyPQExpBuffer(query); |
| 5322 |
} |
| 5323 |
|
| 5324 |
/* |
| 5325 |
* dumpSubscription |
| 5326 |
* dump the definition of the given subscription |
| 5327 |
*/ |
| 5328 |
static void |
| 5329 |
dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo) |
| 5330 |
{ |
| 5331 |
DumpOptions *dopt = fout->dopt; |
| 5332 |
PQExpBuffer delq; |
| 5333 |
PQExpBuffer query; |
| 5334 |
PQExpBuffer publications; |
| 5335 |
char *qsubname; |
| 5336 |
char **pubnames = NULL; |
| 5337 |
int npubnames = 0; |
| 5338 |
int i; |
| 5339 |
|
| 5340 |
/* Do nothing if not dumping schema */ |
| 5341 |
if (!dopt->dumpSchema) |
| 5342 |
return; |
| 5343 |
|
| 5344 |
delq = createPQExpBuffer(); |
| 5345 |
query = createPQExpBuffer(); |
| 5346 |
|
| 5347 |
qsubname = pg_strdup(fmtId(subinfo->dobj.name)); |
| 5348 |
|
| 5349 |
appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n", |
| 5350 |
qsubname); |
| 5351 |
|
| 5352 |
appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s CONNECTION ", |
| 5353 |
qsubname); |
| 5354 |
appendStringLiteralAH(query, subinfo->subconninfo, fout); |
| 5355 |
|
| 5356 |
/* Build list of quoted publications and append them to query. */ |
| 5357 |
if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames)) |
| 5358 |
pg_fatal("could not parse %s array", "subpublications"); |
| 5359 |
|
| 5360 |
publications = createPQExpBuffer(); |
| 5361 |
for (i = 0; i < npubnames; i++) |
| 5362 |
{ |
| 5363 |
if (i > 0) |
| 5364 |
appendPQExpBufferStr(publications, ", "); |
| 5365 |
|
| 5366 |
appendPQExpBufferStr(publications, fmtId(pubnames[i])); |
| 5367 |
} |
| 5368 |
|
| 5369 |
appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data); |
| 5370 |
if (subinfo->subslotname) |
| 5371 |
appendStringLiteralAH(query, subinfo->subslotname, fout); |
| 5372 |
else |
| 5373 |
appendPQExpBufferStr(query, "NONE"); |
| 5374 |
|
| 5375 |
if (subinfo->subbinary) |
| 5376 |
appendPQExpBufferStr(query, ", binary = true"); |
| 5377 |
|
| 5378 |
if (subinfo->substream == LOGICALREP_STREAM_ON) |
| 5379 |
appendPQExpBufferStr(query, ", streaming = on"); |
| 5380 |
else if (subinfo->substream == LOGICALREP_STREAM_PARALLEL) |
| 5381 |
appendPQExpBufferStr(query, ", streaming = parallel"); |
| 5382 |
else |
| 5383 |
appendPQExpBufferStr(query, ", streaming = off"); |
| 5384 |
|
| 5385 |
if (subinfo->subtwophasestate != LOGICALREP_TWOPHASE_STATE_DISABLED) |
| 5386 |
appendPQExpBufferStr(query, ", two_phase = on"); |
| 5387 |
|
| 5388 |
if (subinfo->subdisableonerr) |
| 5389 |
appendPQExpBufferStr(query, ", disable_on_error = true"); |
| 5390 |
|
| 5391 |
if (!subinfo->subpasswordrequired) |
| 5392 |
appendPQExpBufferStr(query, ", password_required = false"); |
| 5393 |
|
| 5394 |
if (subinfo->subrunasowner) |
| 5395 |
appendPQExpBufferStr(query, ", run_as_owner = true"); |
| 5396 |
|
| 5397 |
if (subinfo->subfailover) |
| 5398 |
appendPQExpBufferStr(query, ", failover = true"); |
| 5399 |
|
| 5400 |
if (strcmp(subinfo->subsynccommit, "off") != 0) |
| 5401 |
appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit)); |
| 5402 |
|
| 5403 |
if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0) |
| 5404 |
appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin); |
| 5405 |
|
| 5406 |
appendPQExpBufferStr(query, ");\n"); |
| 5407 |
|
| 5408 |
/* |
| 5409 |
* In binary-upgrade mode, we allow the replication to continue after the |
| 5410 |
* upgrade. |
| 5411 |
*/ |
| 5412 |
if (dopt->binary_upgrade && fout->remoteVersion >= 170000) |
| 5413 |
{ |
| 5414 |
if (subinfo->suboriginremotelsn) |
| 5415 |
{ |
| 5416 |
/* |
| 5417 |
* Preserve the remote_lsn for the subscriber's replication |
| 5418 |
* origin. This value is required to start the replication from |
| 5419 |
* the position before the upgrade. This value will be stale if |
| 5420 |
* the publisher gets upgraded before the subscriber node. |
| 5421 |
* However, this shouldn't be a problem as the upgrade of the |
| 5422 |
* publisher ensures that all the transactions were replicated |
| 5423 |
* before upgrading it. |
| 5424 |
*/ |
| 5425 |
appendPQExpBufferStr(query, |
| 5426 |
"\n-- For binary upgrade, must preserve the remote_lsn for the subscriber's replication origin.\n"); |
| 5427 |
appendPQExpBufferStr(query, |
| 5428 |
"SELECT pg_catalog.binary_upgrade_replorigin_advance("); |
| 5429 |
appendStringLiteralAH(query, subinfo->dobj.name, fout); |
| 5430 |
appendPQExpBuffer(query, ", '%s');\n", subinfo->suboriginremotelsn); |
| 5431 |
} |
| 5432 |
|
| 5433 |
if (subinfo->subenabled) |
| 5434 |
{ |
| 5435 |
/* |
| 5436 |
* Enable the subscription to allow the replication to continue |
| 5437 |
* after the upgrade. |
| 5438 |
*/ |
| 5439 |
appendPQExpBufferStr(query, |
| 5440 |
"\n-- For binary upgrade, must preserve the subscriber's running state.\n"); |
| 5441 |
appendPQExpBuffer(query, "ALTER SUBSCRIPTION %s ENABLE;\n", qsubname); |
| 5442 |
} |
| 5443 |
} |
| 5444 |
|
| 5445 |
if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 5446 |
ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId, |
| 5447 |
ARCHIVE_OPTS(.tag = subinfo->dobj.name, |
| 5448 |
.owner = subinfo->rolname, |
| 5449 |
.description = "SUBSCRIPTION", |
| 5450 |
.section = SECTION_POST_DATA, |
| 5451 |
.createStmt = query->data, |
| 5452 |
.dropStmt = delq->data)); |
| 5453 |
|
| 5454 |
if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 5455 |
dumpComment(fout, "SUBSCRIPTION", qsubname, |
| 5456 |
NULL, subinfo->rolname, |
| 5457 |
subinfo->dobj.catId, 0, subinfo->dobj.dumpId); |
| 5458 |
|
| 5459 |
if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 5460 |
dumpSecLabel(fout, "SUBSCRIPTION", qsubname, |
| 5461 |
NULL, subinfo->rolname, |
| 5462 |
subinfo->dobj.catId, 0, subinfo->dobj.dumpId); |
| 5463 |
|
| 5464 |
destroyPQExpBuffer(publications); |
| 5465 |
free(pubnames); |
| 5466 |
|
| 5467 |
destroyPQExpBuffer(delq); |
| 5468 |
destroyPQExpBuffer(query); |
| 5469 |
free(qsubname); |
| 5470 |
} |
| 5471 |
|
| 5472 |
/* |
| 5473 |
* Given a "create query", append as many ALTER ... DEPENDS ON EXTENSION as |
| 5474 |
* the object needs. |
| 5475 |
*/ |
| 5476 |
static void |
| 5477 |
append_depends_on_extension(Archive *fout, |
| 5478 |
PQExpBuffer create, |
| 5479 |
const DumpableObject *dobj, |
| 5480 |
const char *catalog, |
| 5481 |
const char *keyword, |
| 5482 |
const char *objname) |
| 5483 |
{ |
| 5484 |
if (dobj->depends_on_ext) |
| 5485 |
{ |
| 5486 |
char *nm; |
| 5487 |
PGresult *res; |
| 5488 |
PQExpBuffer query; |
| 5489 |
int ntups; |
| 5490 |
int i_extname; |
| 5491 |
int i; |
| 5492 |
|
| 5493 |
/* dodge fmtId() non-reentrancy */ |
| 5494 |
nm = pg_strdup(objname); |
| 5495 |
|
| 5496 |
query = createPQExpBuffer(); |
| 5497 |
appendPQExpBuffer(query, |
| 5498 |
"SELECT e.extname " |
| 5499 |
"FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e " |
| 5500 |
"WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass " |
| 5501 |
"AND objid = '%u'::pg_catalog.oid AND deptype = 'x' " |
| 5502 |
"AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass", |
| 5503 |
catalog, |
| 5504 |
dobj->catId.oid); |
| 5505 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 5506 |
ntups = PQntuples(res); |
| 5507 |
i_extname = PQfnumber(res, "extname"); |
| 5508 |
for (i = 0; i < ntups; i++) |
| 5509 |
{ |
| 5510 |
appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;", |
| 5511 |
keyword, nm, |
| 5512 |
fmtId(PQgetvalue(res, i, i_extname))); |
| 5513 |
} |
| 5514 |
|
| 5515 |
PQclear(res); |
| 5516 |
destroyPQExpBuffer(query); |
| 5517 |
pg_free(nm); |
| 5518 |
} |
| 5519 |
} |
| 5520 |
|
| 5521 |
static Oid |
| 5522 |
get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query) |
| 5523 |
{ |
| 5524 |
/* |
| 5525 |
* If the old version didn't assign an array type, but the new version |
| 5526 |
* does, we must select an unused type OID to assign. This currently only |
| 5527 |
* happens for domains, when upgrading pre-v11 to v11 and up. |
| 5528 |
* |
| 5529 |
* Note: local state here is kind of ugly, but we must have some, since we |
| 5530 |
* mustn't choose the same unused OID more than once. |
| 5531 |
*/ |
| 5532 |
static Oid next_possible_free_oid = FirstNormalObjectId; |
| 5533 |
PGresult *res; |
| 5534 |
bool is_dup; |
| 5535 |
|
| 5536 |
do |
| 5537 |
{ |
| 5538 |
++next_possible_free_oid; |
| 5539 |
printfPQExpBuffer(upgrade_query, |
| 5540 |
"SELECT EXISTS(SELECT 1 " |
| 5541 |
"FROM pg_catalog.pg_type " |
| 5542 |
"WHERE oid = '%u'::pg_catalog.oid);", |
| 5543 |
next_possible_free_oid); |
| 5544 |
res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data); |
| 5545 |
is_dup = (PQgetvalue(res, 0, 0)[0] == 't'); |
| 5546 |
PQclear(res); |
| 5547 |
} while (is_dup); |
| 5548 |
|
| 5549 |
return next_possible_free_oid; |
| 5550 |
} |
| 5551 |
|
| 5552 |
static void |
| 5553 |
binary_upgrade_set_type_oids_by_type_oid(Archive *fout, |
| 5554 |
PQExpBuffer upgrade_buffer, |
| 5555 |
Oid pg_type_oid, |
| 5556 |
bool force_array_type, |
| 5557 |
bool include_multirange_type) |
| 5558 |
{ |
| 5559 |
PQExpBuffer upgrade_query = createPQExpBuffer(); |
| 5560 |
PGresult *res; |
| 5561 |
Oid pg_type_array_oid; |
| 5562 |
Oid pg_type_multirange_oid; |
| 5563 |
Oid pg_type_multirange_array_oid; |
| 5564 |
TypeInfo *tinfo; |
| 5565 |
|
| 5566 |
appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n"); |
| 5567 |
appendPQExpBuffer(upgrade_buffer, |
| 5568 |
"SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n", |
| 5569 |
pg_type_oid); |
| 5570 |
|
| 5571 |
tinfo = findTypeByOid(pg_type_oid); |
| 5572 |
if (tinfo) |
| 5573 |
pg_type_array_oid = tinfo->typarray; |
| 5574 |
else |
| 5575 |
pg_type_array_oid = InvalidOid; |
| 5576 |
|
| 5577 |
if (!OidIsValid(pg_type_array_oid) && force_array_type) |
| 5578 |
pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query); |
| 5579 |
|
| 5580 |
if (OidIsValid(pg_type_array_oid)) |
| 5581 |
{ |
| 5582 |
appendPQExpBufferStr(upgrade_buffer, |
| 5583 |
"\n-- For binary upgrade, must preserve pg_type array oid\n"); |
| 5584 |
appendPQExpBuffer(upgrade_buffer, |
| 5585 |
"SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n", |
| 5586 |
pg_type_array_oid); |
| 5587 |
} |
| 5588 |
|
| 5589 |
/* |
| 5590 |
* Pre-set the multirange type oid and its own array type oid. |
| 5591 |
*/ |
| 5592 |
if (include_multirange_type) |
| 5593 |
{ |
| 5594 |
if (fout->remoteVersion >= 140000) |
| 5595 |
{ |
| 5596 |
printfPQExpBuffer(upgrade_query, |
| 5597 |
"SELECT t.oid, t.typarray " |
| 5598 |
"FROM pg_catalog.pg_type t " |
| 5599 |
"JOIN pg_catalog.pg_range r " |
| 5600 |
"ON t.oid = r.rngmultitypid " |
| 5601 |
"WHERE r.rngtypid = '%u'::pg_catalog.oid;", |
| 5602 |
pg_type_oid); |
| 5603 |
|
| 5604 |
res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data); |
| 5605 |
|
| 5606 |
pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid"))); |
| 5607 |
pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray"))); |
| 5608 |
|
| 5609 |
PQclear(res); |
| 5610 |
} |
| 5611 |
else |
| 5612 |
{ |
| 5613 |
pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query); |
| 5614 |
pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query); |
| 5615 |
} |
| 5616 |
|
| 5617 |
appendPQExpBufferStr(upgrade_buffer, |
| 5618 |
"\n-- For binary upgrade, must preserve multirange pg_type oid\n"); |
| 5619 |
appendPQExpBuffer(upgrade_buffer, |
| 5620 |
"SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n", |
| 5621 |
pg_type_multirange_oid); |
| 5622 |
appendPQExpBufferStr(upgrade_buffer, |
| 5623 |
"\n-- For binary upgrade, must preserve multirange pg_type array oid\n"); |
| 5624 |
appendPQExpBuffer(upgrade_buffer, |
| 5625 |
"SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n", |
| 5626 |
pg_type_multirange_array_oid); |
| 5627 |
} |
| 5628 |
|
| 5629 |
destroyPQExpBuffer(upgrade_query); |
| 5630 |
} |
| 5631 |
|
| 5632 |
static void |
| 5633 |
binary_upgrade_set_type_oids_by_rel(Archive *fout, |
| 5634 |
PQExpBuffer upgrade_buffer, |
| 5635 |
const TableInfo *tbinfo) |
| 5636 |
{ |
| 5637 |
Oid pg_type_oid = tbinfo->reltype; |
| 5638 |
|
| 5639 |
if (OidIsValid(pg_type_oid)) |
| 5640 |
binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer, |
| 5641 |
pg_type_oid, false, false); |
| 5642 |
} |
| 5643 |
|
| 5644 |
/* |
| 5645 |
* bsearch() comparator for BinaryUpgradeClassOidItem |
| 5646 |
*/ |
| 5647 |
static int |
| 5648 |
BinaryUpgradeClassOidItemCmp(const void *p1, const void *p2) |
| 5649 |
{ |
| 5650 |
BinaryUpgradeClassOidItem v1 = *((const BinaryUpgradeClassOidItem *) p1); |
| 5651 |
BinaryUpgradeClassOidItem v2 = *((const BinaryUpgradeClassOidItem *) p2); |
| 5652 |
|
| 5653 |
return pg_cmp_u32(v1.oid, v2.oid); |
| 5654 |
} |
| 5655 |
|
| 5656 |
/* |
| 5657 |
* collectBinaryUpgradeClassOids |
| 5658 |
* |
| 5659 |
* Construct a table of pg_class information required for |
| 5660 |
* binary_upgrade_set_pg_class_oids(). The table is sorted by OID for speed in |
| 5661 |
* lookup. |
| 5662 |
*/ |
| 5663 |
static void |
| 5664 |
collectBinaryUpgradeClassOids(Archive *fout) |
| 5665 |
{ |
| 5666 |
PGresult *res; |
| 5667 |
const char *query; |
| 5668 |
|
| 5669 |
query = "SELECT c.oid, c.relkind, c.relfilenode, c.reltoastrelid, " |
| 5670 |
"ct.relfilenode, i.indexrelid, cti.relfilenode " |
| 5671 |
"FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index i " |
| 5672 |
"ON (c.reltoastrelid = i.indrelid AND i.indisvalid) " |
| 5673 |
"LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) " |
| 5674 |
"LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) " |
| 5675 |
"ORDER BY c.oid;"; |
| 5676 |
|
| 5677 |
res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK); |
| 5678 |
|
| 5679 |
nbinaryUpgradeClassOids = PQntuples(res); |
| 5680 |
binaryUpgradeClassOids = (BinaryUpgradeClassOidItem *) |
| 5681 |
pg_malloc(nbinaryUpgradeClassOids * sizeof(BinaryUpgradeClassOidItem)); |
| 5682 |
|
| 5683 |
for (int i = 0; i < nbinaryUpgradeClassOids; i++) |
| 5684 |
{ |
| 5685 |
binaryUpgradeClassOids[i].oid = atooid(PQgetvalue(res, i, 0)); |
| 5686 |
binaryUpgradeClassOids[i].relkind = *PQgetvalue(res, i, 1); |
| 5687 |
binaryUpgradeClassOids[i].relfilenumber = atooid(PQgetvalue(res, i, 2)); |
| 5688 |
binaryUpgradeClassOids[i].toast_oid = atooid(PQgetvalue(res, i, 3)); |
| 5689 |
binaryUpgradeClassOids[i].toast_relfilenumber = atooid(PQgetvalue(res, i, 4)); |
| 5690 |
binaryUpgradeClassOids[i].toast_index_oid = atooid(PQgetvalue(res, i, 5)); |
| 5691 |
binaryUpgradeClassOids[i].toast_index_relfilenumber = atooid(PQgetvalue(res, i, 6)); |
| 5692 |
} |
| 5693 |
|
| 5694 |
PQclear(res); |
| 5695 |
} |
| 5696 |
|
| 5697 |
static void |
| 5698 |
binary_upgrade_set_pg_class_oids(Archive *fout, |
| 5699 |
PQExpBuffer upgrade_buffer, Oid pg_class_oid) |
| 5700 |
{ |
| 5701 |
BinaryUpgradeClassOidItem key = {0}; |
| 5702 |
BinaryUpgradeClassOidItem *entry; |
| 5703 |
|
| 5704 |
Assert(binaryUpgradeClassOids); |
| 5705 |
|
| 5706 |
/* |
| 5707 |
* Preserve the OID and relfilenumber of the table, table's index, table's |
| 5708 |
* toast table and toast table's index if any. |
| 5709 |
* |
| 5710 |
* One complexity is that the current table definition might not require |
| 5711 |
* the creation of a TOAST table, but the old database might have a TOAST |
| 5712 |
* table that was created earlier, before some wide columns were dropped. |
| 5713 |
* By setting the TOAST oid we force creation of the TOAST heap and index |
| 5714 |
* by the new backend, so we can copy the files during binary upgrade |
| 5715 |
* without worrying about this case. |
| 5716 |
*/ |
| 5717 |
key.oid = pg_class_oid; |
| 5718 |
entry = bsearch(&key, binaryUpgradeClassOids, nbinaryUpgradeClassOids, |
| 5719 |
sizeof(BinaryUpgradeClassOidItem), |
| 5720 |
BinaryUpgradeClassOidItemCmp); |
| 5721 |
|
| 5722 |
appendPQExpBufferStr(upgrade_buffer, |
| 5723 |
"\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n"); |
| 5724 |
|
| 5725 |
if (entry->relkind != RELKIND_INDEX && |
| 5726 |
entry->relkind != RELKIND_PARTITIONED_INDEX) |
| 5727 |
{ |
| 5728 |
appendPQExpBuffer(upgrade_buffer, |
| 5729 |
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n", |
| 5730 |
pg_class_oid); |
| 5731 |
|
| 5732 |
/* |
| 5733 |
* Not every relation has storage. Also, in a pre-v12 database, |
| 5734 |
* partitioned tables have a relfilenumber, which should not be |
| 5735 |
* preserved when upgrading. |
| 5736 |
*/ |
| 5737 |
if (RelFileNumberIsValid(entry->relfilenumber) && |
| 5738 |
entry->relkind != RELKIND_PARTITIONED_TABLE) |
| 5739 |
appendPQExpBuffer(upgrade_buffer, |
| 5740 |
"SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n", |
| 5741 |
entry->relfilenumber); |
| 5742 |
|
| 5743 |
/* |
| 5744 |
* In a pre-v12 database, partitioned tables might be marked as having |
| 5745 |
* toast tables, but we should ignore them if so. |
| 5746 |
*/ |
| 5747 |
if (OidIsValid(entry->toast_oid) && |
| 5748 |
entry->relkind != RELKIND_PARTITIONED_TABLE) |
| 5749 |
{ |
| 5750 |
appendPQExpBuffer(upgrade_buffer, |
| 5751 |
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n", |
| 5752 |
entry->toast_oid); |
| 5753 |
appendPQExpBuffer(upgrade_buffer, |
| 5754 |
"SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n", |
| 5755 |
entry->toast_relfilenumber); |
| 5756 |
|
| 5757 |
/* every toast table has an index */ |
| 5758 |
appendPQExpBuffer(upgrade_buffer, |
| 5759 |
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n", |
| 5760 |
entry->toast_index_oid); |
| 5761 |
appendPQExpBuffer(upgrade_buffer, |
| 5762 |
"SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n", |
| 5763 |
entry->toast_index_relfilenumber); |
| 5764 |
} |
| 5765 |
} |
| 5766 |
else |
| 5767 |
{ |
| 5768 |
/* Preserve the OID and relfilenumber of the index */ |
| 5769 |
appendPQExpBuffer(upgrade_buffer, |
| 5770 |
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n", |
| 5771 |
pg_class_oid); |
| 5772 |
appendPQExpBuffer(upgrade_buffer, |
| 5773 |
"SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n", |
| 5774 |
entry->relfilenumber); |
| 5775 |
} |
| 5776 |
|
| 5777 |
appendPQExpBufferChar(upgrade_buffer, '\n'); |
| 5778 |
} |
| 5779 |
|
| 5780 |
/* |
| 5781 |
* If the DumpableObject is a member of an extension, add a suitable |
| 5782 |
* ALTER EXTENSION ADD command to the creation commands in upgrade_buffer. |
| 5783 |
* |
| 5784 |
* For somewhat historical reasons, objname should already be quoted, |
| 5785 |
* but not objnamespace (if any). |
| 5786 |
*/ |
| 5787 |
static void |
| 5788 |
binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, |
| 5789 |
const DumpableObject *dobj, |
| 5790 |
const char *objtype, |
| 5791 |
const char *objname, |
| 5792 |
const char *objnamespace) |
| 5793 |
{ |
| 5794 |
DumpableObject *extobj = NULL; |
| 5795 |
int i; |
| 5796 |
|
| 5797 |
if (!dobj->ext_member) |
| 5798 |
return; |
| 5799 |
|
| 5800 |
/* |
| 5801 |
* Find the parent extension. We could avoid this search if we wanted to |
| 5802 |
* add a link field to DumpableObject, but the space costs of that would |
| 5803 |
* be considerable. We assume that member objects could only have a |
| 5804 |
* direct dependency on their own extension, not any others. |
| 5805 |
*/ |
| 5806 |
for (i = 0; i < dobj->nDeps; i++) |
| 5807 |
{ |
| 5808 |
extobj = findObjectByDumpId(dobj->dependencies[i]); |
| 5809 |
if (extobj && extobj->objType == DO_EXTENSION) |
| 5810 |
break; |
| 5811 |
extobj = NULL; |
| 5812 |
} |
| 5813 |
if (extobj == NULL) |
| 5814 |
pg_fatal("could not find parent extension for %s %s", |
| 5815 |
objtype, objname); |
| 5816 |
|
| 5817 |
appendPQExpBufferStr(upgrade_buffer, |
| 5818 |
"\n-- For binary upgrade, handle extension membership the hard way\n"); |
| 5819 |
appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ", |
| 5820 |
fmtId(extobj->name), |
| 5821 |
objtype); |
| 5822 |
if (objnamespace && *objnamespace) |
| 5823 |
appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace)); |
| 5824 |
appendPQExpBuffer(upgrade_buffer, "%s;\n", objname); |
| 5825 |
} |
| 5826 |
|
| 5827 |
/* |
| 5828 |
* getNamespaces: |
| 5829 |
* get information about all namespaces in the system catalogs |
| 5830 |
*/ |
| 5831 |
void |
| 5832 |
getNamespaces(Archive *fout) |
| 5833 |
{ |
| 5834 |
PGresult *res; |
| 5835 |
int ntups; |
| 5836 |
int i; |
| 5837 |
PQExpBuffer query; |
| 5838 |
NamespaceInfo *nsinfo; |
| 5839 |
int i_tableoid; |
| 5840 |
int i_oid; |
| 5841 |
int i_nspname; |
| 5842 |
int i_nspowner; |
| 5843 |
int i_nspacl; |
| 5844 |
int i_acldefault; |
| 5845 |
|
| 5846 |
query = createPQExpBuffer(); |
| 5847 |
|
| 5848 |
/* |
| 5849 |
* we fetch all namespaces including system ones, so that every object we |
| 5850 |
* read in can be linked to a containing namespace. |
| 5851 |
*/ |
| 5852 |
appendPQExpBufferStr(query, "SELECT n.tableoid, n.oid, n.nspname, " |
| 5853 |
"n.nspowner, " |
| 5854 |
"n.nspacl, " |
| 5855 |
"acldefault('n', n.nspowner) AS acldefault " |
| 5856 |
"FROM pg_namespace n"); |
| 5857 |
|
| 5858 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 5859 |
|
| 5860 |
ntups = PQntuples(res); |
| 5861 |
|
| 5862 |
nsinfo = (NamespaceInfo *) pg_malloc(ntups * sizeof(NamespaceInfo)); |
| 5863 |
|
| 5864 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 5865 |
i_oid = PQfnumber(res, "oid"); |
| 5866 |
i_nspname = PQfnumber(res, "nspname"); |
| 5867 |
i_nspowner = PQfnumber(res, "nspowner"); |
| 5868 |
i_nspacl = PQfnumber(res, "nspacl"); |
| 5869 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 5870 |
|
| 5871 |
for (i = 0; i < ntups; i++) |
| 5872 |
{ |
| 5873 |
const char *nspowner; |
| 5874 |
|
| 5875 |
nsinfo[i].dobj.objType = DO_NAMESPACE; |
| 5876 |
nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 5877 |
nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 5878 |
AssignDumpId(&nsinfo[i].dobj); |
| 5879 |
nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname)); |
| 5880 |
nsinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_nspacl)); |
| 5881 |
nsinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 5882 |
nsinfo[i].dacl.privtype = 0; |
| 5883 |
nsinfo[i].dacl.initprivs = NULL; |
| 5884 |
nspowner = PQgetvalue(res, i, i_nspowner); |
| 5885 |
nsinfo[i].nspowner = atooid(nspowner); |
| 5886 |
nsinfo[i].rolname = getRoleName(nspowner); |
| 5887 |
|
| 5888 |
/* Decide whether to dump this namespace */ |
| 5889 |
selectDumpableNamespace(&nsinfo[i], fout); |
| 5890 |
|
| 5891 |
/* Mark whether namespace has an ACL */ |
| 5892 |
if (!PQgetisnull(res, i, i_nspacl)) |
| 5893 |
nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 5894 |
|
| 5895 |
/* |
| 5896 |
* We ignore any pg_init_privs.initprivs entry for the public schema |
| 5897 |
* and assume a predetermined default, for several reasons. First, |
| 5898 |
* dropping and recreating the schema removes its pg_init_privs entry, |
| 5899 |
* but an empty destination database starts with this ACL nonetheless. |
| 5900 |
* Second, we support dump/reload of public schema ownership changes. |
| 5901 |
* ALTER SCHEMA OWNER filters nspacl through aclnewowner(), but |
| 5902 |
* initprivs continues to reflect the initial owner. Hence, |
| 5903 |
* synthesize the value that nspacl will have after the restore's |
| 5904 |
* ALTER SCHEMA OWNER. Third, this makes the destination database |
| 5905 |
* match the source's ACL, even if the latter was an initdb-default |
| 5906 |
* ACL, which changed in v15. An upgrade pulls in changes to most |
| 5907 |
* system object ACLs that the DBA had not customized. We've made the |
| 5908 |
* public schema depart from that, because changing its ACL so easily |
| 5909 |
* breaks applications. |
| 5910 |
*/ |
| 5911 |
if (strcmp(nsinfo[i].dobj.name, "public") == 0) |
| 5912 |
{ |
| 5913 |
PQExpBuffer aclarray = createPQExpBuffer(); |
| 5914 |
PQExpBuffer aclitem = createPQExpBuffer(); |
| 5915 |
|
| 5916 |
/* Standard ACL as of v15 is {owner=UC/owner,=U/owner} */ |
| 5917 |
appendPQExpBufferChar(aclarray, '{'); |
| 5918 |
quoteAclUserName(aclitem, nsinfo[i].rolname); |
| 5919 |
appendPQExpBufferStr(aclitem, "=UC/"); |
| 5920 |
quoteAclUserName(aclitem, nsinfo[i].rolname); |
| 5921 |
appendPGArray(aclarray, aclitem->data); |
| 5922 |
resetPQExpBuffer(aclitem); |
| 5923 |
appendPQExpBufferStr(aclitem, "=U/"); |
| 5924 |
quoteAclUserName(aclitem, nsinfo[i].rolname); |
| 5925 |
appendPGArray(aclarray, aclitem->data); |
| 5926 |
appendPQExpBufferChar(aclarray, '}'); |
| 5927 |
|
| 5928 |
nsinfo[i].dacl.privtype = 'i'; |
| 5929 |
nsinfo[i].dacl.initprivs = pstrdup(aclarray->data); |
| 5930 |
nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 5931 |
|
| 5932 |
destroyPQExpBuffer(aclarray); |
| 5933 |
destroyPQExpBuffer(aclitem); |
| 5934 |
} |
| 5935 |
} |
| 5936 |
|
| 5937 |
PQclear(res); |
| 5938 |
destroyPQExpBuffer(query); |
| 5939 |
} |
| 5940 |
|
| 5941 |
/* |
| 5942 |
* findNamespace: |
| 5943 |
* given a namespace OID, look up the info read by getNamespaces |
| 5944 |
*/ |
| 5945 |
static NamespaceInfo * |
| 5946 |
findNamespace(Oid nsoid) |
| 5947 |
{ |
| 5948 |
NamespaceInfo *nsinfo; |
| 5949 |
|
| 5950 |
nsinfo = findNamespaceByOid(nsoid); |
| 5951 |
if (nsinfo == NULL) |
| 5952 |
pg_fatal("schema with OID %u does not exist", nsoid); |
| 5953 |
return nsinfo; |
| 5954 |
} |
| 5955 |
|
| 5956 |
/* |
| 5957 |
* getExtensions: |
| 5958 |
* read all extensions in the system catalogs and return them in the |
| 5959 |
* ExtensionInfo* structure |
| 5960 |
* |
| 5961 |
* numExtensions is set to the number of extensions read in |
| 5962 |
*/ |
| 5963 |
ExtensionInfo * |
| 5964 |
getExtensions(Archive *fout, int *numExtensions) |
| 5965 |
{ |
| 5966 |
DumpOptions *dopt = fout->dopt; |
| 5967 |
PGresult *res; |
| 5968 |
int ntups; |
| 5969 |
int i; |
| 5970 |
PQExpBuffer query; |
| 5971 |
ExtensionInfo *extinfo = NULL; |
| 5972 |
int i_tableoid; |
| 5973 |
int i_oid; |
| 5974 |
int i_extname; |
| 5975 |
int i_nspname; |
| 5976 |
int i_extrelocatable; |
| 5977 |
int i_extversion; |
| 5978 |
int i_extconfig; |
| 5979 |
int i_extcondition; |
| 5980 |
|
| 5981 |
query = createPQExpBuffer(); |
| 5982 |
|
| 5983 |
appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, " |
| 5984 |
"x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition " |
| 5985 |
"FROM pg_extension x " |
| 5986 |
"JOIN pg_namespace n ON n.oid = x.extnamespace"); |
| 5987 |
|
| 5988 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 5989 |
|
| 5990 |
ntups = PQntuples(res); |
| 5991 |
if (ntups == 0) |
| 5992 |
goto cleanup; |
| 5993 |
|
| 5994 |
extinfo = (ExtensionInfo *) pg_malloc(ntups * sizeof(ExtensionInfo)); |
| 5995 |
|
| 5996 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 5997 |
i_oid = PQfnumber(res, "oid"); |
| 5998 |
i_extname = PQfnumber(res, "extname"); |
| 5999 |
i_nspname = PQfnumber(res, "nspname"); |
| 6000 |
i_extrelocatable = PQfnumber(res, "extrelocatable"); |
| 6001 |
i_extversion = PQfnumber(res, "extversion"); |
| 6002 |
i_extconfig = PQfnumber(res, "extconfig"); |
| 6003 |
i_extcondition = PQfnumber(res, "extcondition"); |
| 6004 |
|
| 6005 |
for (i = 0; i < ntups; i++) |
| 6006 |
{ |
| 6007 |
extinfo[i].dobj.objType = DO_EXTENSION; |
| 6008 |
extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6009 |
extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6010 |
AssignDumpId(&extinfo[i].dobj); |
| 6011 |
extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname)); |
| 6012 |
extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname)); |
| 6013 |
extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't'; |
| 6014 |
extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion)); |
| 6015 |
extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig)); |
| 6016 |
extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition)); |
| 6017 |
|
| 6018 |
/* Decide whether we want to dump it */ |
| 6019 |
selectDumpableExtension(&(extinfo[i]), dopt); |
| 6020 |
} |
| 6021 |
|
| 6022 |
cleanup: |
| 6023 |
PQclear(res); |
| 6024 |
destroyPQExpBuffer(query); |
| 6025 |
|
| 6026 |
*numExtensions = ntups; |
| 6027 |
|
| 6028 |
return extinfo; |
| 6029 |
} |
| 6030 |
|
| 6031 |
/* |
| 6032 |
* getTypes: |
| 6033 |
* get information about all types in the system catalogs |
| 6034 |
* |
| 6035 |
* NB: this must run after getFuncs() because we assume we can do |
| 6036 |
* findFuncByOid(). |
| 6037 |
*/ |
| 6038 |
void |
| 6039 |
getTypes(Archive *fout) |
| 6040 |
{ |
| 6041 |
PGresult *res; |
| 6042 |
int ntups; |
| 6043 |
int i; |
| 6044 |
PQExpBuffer query = createPQExpBuffer(); |
| 6045 |
TypeInfo *tyinfo; |
| 6046 |
ShellTypeInfo *stinfo; |
| 6047 |
int i_tableoid; |
| 6048 |
int i_oid; |
| 6049 |
int i_typname; |
| 6050 |
int i_typnamespace; |
| 6051 |
int i_typacl; |
| 6052 |
int i_acldefault; |
| 6053 |
int i_typowner; |
| 6054 |
int i_typelem; |
| 6055 |
int i_typrelid; |
| 6056 |
int i_typrelkind; |
| 6057 |
int i_typtype; |
| 6058 |
int i_typisdefined; |
| 6059 |
int i_isarray; |
| 6060 |
int i_typarray; |
| 6061 |
|
| 6062 |
/* |
| 6063 |
* we include even the built-in types because those may be used as array |
| 6064 |
* elements by user-defined types |
| 6065 |
* |
| 6066 |
* we filter out the built-in types when we dump out the types |
| 6067 |
* |
| 6068 |
* same approach for undefined (shell) types and array types |
| 6069 |
* |
| 6070 |
* Note: as of 8.3 we can reliably detect whether a type is an |
| 6071 |
* auto-generated array type by checking the element type's typarray. |
| 6072 |
* (Before that the test is capable of generating false positives.) We |
| 6073 |
* still check for name beginning with '_', though, so as to avoid the |
| 6074 |
* cost of the subselect probe for all standard types. This would have to |
| 6075 |
* be revisited if the backend ever allows renaming of array types. |
| 6076 |
*/ |
| 6077 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, typname, " |
| 6078 |
"typnamespace, typacl, " |
| 6079 |
"acldefault('T', typowner) AS acldefault, " |
| 6080 |
"typowner, " |
| 6081 |
"typelem, typrelid, typarray, " |
| 6082 |
"CASE WHEN typrelid = 0 THEN ' '::\"char\" " |
| 6083 |
"ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, " |
| 6084 |
"typtype, typisdefined, " |
| 6085 |
"typname[0] = '_' AND typelem != 0 AND " |
| 6086 |
"(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray " |
| 6087 |
"FROM pg_type"); |
| 6088 |
|
| 6089 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6090 |
|
| 6091 |
ntups = PQntuples(res); |
| 6092 |
|
| 6093 |
tyinfo = (TypeInfo *) pg_malloc(ntups * sizeof(TypeInfo)); |
| 6094 |
|
| 6095 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6096 |
i_oid = PQfnumber(res, "oid"); |
| 6097 |
i_typname = PQfnumber(res, "typname"); |
| 6098 |
i_typnamespace = PQfnumber(res, "typnamespace"); |
| 6099 |
i_typacl = PQfnumber(res, "typacl"); |
| 6100 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 6101 |
i_typowner = PQfnumber(res, "typowner"); |
| 6102 |
i_typelem = PQfnumber(res, "typelem"); |
| 6103 |
i_typrelid = PQfnumber(res, "typrelid"); |
| 6104 |
i_typrelkind = PQfnumber(res, "typrelkind"); |
| 6105 |
i_typtype = PQfnumber(res, "typtype"); |
| 6106 |
i_typisdefined = PQfnumber(res, "typisdefined"); |
| 6107 |
i_isarray = PQfnumber(res, "isarray"); |
| 6108 |
i_typarray = PQfnumber(res, "typarray"); |
| 6109 |
|
| 6110 |
for (i = 0; i < ntups; i++) |
| 6111 |
{ |
| 6112 |
tyinfo[i].dobj.objType = DO_TYPE; |
| 6113 |
tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6114 |
tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6115 |
AssignDumpId(&tyinfo[i].dobj); |
| 6116 |
tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname)); |
| 6117 |
tyinfo[i].dobj.namespace = |
| 6118 |
findNamespace(atooid(PQgetvalue(res, i, i_typnamespace))); |
| 6119 |
tyinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_typacl)); |
| 6120 |
tyinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 6121 |
tyinfo[i].dacl.privtype = 0; |
| 6122 |
tyinfo[i].dacl.initprivs = NULL; |
| 6123 |
tyinfo[i].ftypname = NULL; /* may get filled later */ |
| 6124 |
tyinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_typowner)); |
| 6125 |
tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem)); |
| 6126 |
tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid)); |
| 6127 |
tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind); |
| 6128 |
tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype); |
| 6129 |
tyinfo[i].shellType = NULL; |
| 6130 |
|
| 6131 |
if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0) |
| 6132 |
tyinfo[i].isDefined = true; |
| 6133 |
else |
| 6134 |
tyinfo[i].isDefined = false; |
| 6135 |
|
| 6136 |
if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0) |
| 6137 |
tyinfo[i].isArray = true; |
| 6138 |
else |
| 6139 |
tyinfo[i].isArray = false; |
| 6140 |
|
| 6141 |
tyinfo[i].typarray = atooid(PQgetvalue(res, i, i_typarray)); |
| 6142 |
|
| 6143 |
if (tyinfo[i].typtype == TYPTYPE_MULTIRANGE) |
| 6144 |
tyinfo[i].isMultirange = true; |
| 6145 |
else |
| 6146 |
tyinfo[i].isMultirange = false; |
| 6147 |
|
| 6148 |
/* Decide whether we want to dump it */ |
| 6149 |
selectDumpableType(&tyinfo[i], fout); |
| 6150 |
|
| 6151 |
/* Mark whether type has an ACL */ |
| 6152 |
if (!PQgetisnull(res, i, i_typacl)) |
| 6153 |
tyinfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 6154 |
|
| 6155 |
/* |
| 6156 |
* If it's a domain, fetch info about its constraints, if any |
| 6157 |
*/ |
| 6158 |
tyinfo[i].nDomChecks = 0; |
| 6159 |
tyinfo[i].domChecks = NULL; |
| 6160 |
tyinfo[i].notnull = NULL; |
| 6161 |
if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) && |
| 6162 |
tyinfo[i].typtype == TYPTYPE_DOMAIN) |
| 6163 |
getDomainConstraints(fout, &(tyinfo[i])); |
| 6164 |
|
| 6165 |
/* |
| 6166 |
* If it's a base type, make a DumpableObject representing a shell |
| 6167 |
* definition of the type. We will need to dump that ahead of the I/O |
| 6168 |
* functions for the type. Similarly, range types need a shell |
| 6169 |
* definition in case they have a canonicalize function. |
| 6170 |
* |
| 6171 |
* Note: the shell type doesn't have a catId. You might think it |
| 6172 |
* should copy the base type's catId, but then it might capture the |
| 6173 |
* pg_depend entries for the type, which we don't want. |
| 6174 |
*/ |
| 6175 |
if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) && |
| 6176 |
(tyinfo[i].typtype == TYPTYPE_BASE || |
| 6177 |
tyinfo[i].typtype == TYPTYPE_RANGE)) |
| 6178 |
{ |
| 6179 |
stinfo = (ShellTypeInfo *) pg_malloc(sizeof(ShellTypeInfo)); |
| 6180 |
stinfo->dobj.objType = DO_SHELL_TYPE; |
| 6181 |
stinfo->dobj.catId = nilCatalogId; |
| 6182 |
AssignDumpId(&stinfo->dobj); |
| 6183 |
stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name); |
| 6184 |
stinfo->dobj.namespace = tyinfo[i].dobj.namespace; |
| 6185 |
stinfo->baseType = &(tyinfo[i]); |
| 6186 |
tyinfo[i].shellType = stinfo; |
| 6187 |
|
| 6188 |
/* |
| 6189 |
* Initially mark the shell type as not to be dumped. We'll only |
| 6190 |
* dump it if the I/O or canonicalize functions need to be dumped; |
| 6191 |
* this is taken care of while sorting dependencies. |
| 6192 |
*/ |
| 6193 |
stinfo->dobj.dump = DUMP_COMPONENT_NONE; |
| 6194 |
} |
| 6195 |
} |
| 6196 |
|
| 6197 |
PQclear(res); |
| 6198 |
|
| 6199 |
destroyPQExpBuffer(query); |
| 6200 |
} |
| 6201 |
|
| 6202 |
/* |
| 6203 |
* getOperators: |
| 6204 |
* get information about all operators in the system catalogs |
| 6205 |
*/ |
| 6206 |
void |
| 6207 |
getOperators(Archive *fout) |
| 6208 |
{ |
| 6209 |
PGresult *res; |
| 6210 |
int ntups; |
| 6211 |
int i; |
| 6212 |
PQExpBuffer query = createPQExpBuffer(); |
| 6213 |
OprInfo *oprinfo; |
| 6214 |
int i_tableoid; |
| 6215 |
int i_oid; |
| 6216 |
int i_oprname; |
| 6217 |
int i_oprnamespace; |
| 6218 |
int i_oprowner; |
| 6219 |
int i_oprkind; |
| 6220 |
int i_oprleft; |
| 6221 |
int i_oprright; |
| 6222 |
int i_oprcode; |
| 6223 |
|
| 6224 |
/* |
| 6225 |
* find all operators, including builtin operators; we filter out |
| 6226 |
* system-defined operators at dump-out time. |
| 6227 |
*/ |
| 6228 |
|
| 6229 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, oprname, " |
| 6230 |
"oprnamespace, " |
| 6231 |
"oprowner, " |
| 6232 |
"oprkind, " |
| 6233 |
"oprleft, " |
| 6234 |
"oprright, " |
| 6235 |
"oprcode::oid AS oprcode " |
| 6236 |
"FROM pg_operator"); |
| 6237 |
|
| 6238 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6239 |
|
| 6240 |
ntups = PQntuples(res); |
| 6241 |
|
| 6242 |
oprinfo = (OprInfo *) pg_malloc(ntups * sizeof(OprInfo)); |
| 6243 |
|
| 6244 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6245 |
i_oid = PQfnumber(res, "oid"); |
| 6246 |
i_oprname = PQfnumber(res, "oprname"); |
| 6247 |
i_oprnamespace = PQfnumber(res, "oprnamespace"); |
| 6248 |
i_oprowner = PQfnumber(res, "oprowner"); |
| 6249 |
i_oprkind = PQfnumber(res, "oprkind"); |
| 6250 |
i_oprleft = PQfnumber(res, "oprleft"); |
| 6251 |
i_oprright = PQfnumber(res, "oprright"); |
| 6252 |
i_oprcode = PQfnumber(res, "oprcode"); |
| 6253 |
|
| 6254 |
for (i = 0; i < ntups; i++) |
| 6255 |
{ |
| 6256 |
oprinfo[i].dobj.objType = DO_OPERATOR; |
| 6257 |
oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6258 |
oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6259 |
AssignDumpId(&oprinfo[i].dobj); |
| 6260 |
oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname)); |
| 6261 |
oprinfo[i].dobj.namespace = |
| 6262 |
findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace))); |
| 6263 |
oprinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_oprowner)); |
| 6264 |
oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0]; |
| 6265 |
oprinfo[i].oprleft = atooid(PQgetvalue(res, i, i_oprleft)); |
| 6266 |
oprinfo[i].oprright = atooid(PQgetvalue(res, i, i_oprright)); |
| 6267 |
oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode)); |
| 6268 |
|
| 6269 |
/* Decide whether we want to dump it */ |
| 6270 |
selectDumpableObject(&(oprinfo[i].dobj), fout); |
| 6271 |
} |
| 6272 |
|
| 6273 |
PQclear(res); |
| 6274 |
|
| 6275 |
destroyPQExpBuffer(query); |
| 6276 |
} |
| 6277 |
|
| 6278 |
/* |
| 6279 |
* getCollations: |
| 6280 |
* get information about all collations in the system catalogs |
| 6281 |
*/ |
| 6282 |
void |
| 6283 |
getCollations(Archive *fout) |
| 6284 |
{ |
| 6285 |
PGresult *res; |
| 6286 |
int ntups; |
| 6287 |
int i; |
| 6288 |
PQExpBuffer query; |
| 6289 |
CollInfo *collinfo; |
| 6290 |
int i_tableoid; |
| 6291 |
int i_oid; |
| 6292 |
int i_collname; |
| 6293 |
int i_collnamespace; |
| 6294 |
int i_collowner; |
| 6295 |
int i_collencoding; |
| 6296 |
|
| 6297 |
query = createPQExpBuffer(); |
| 6298 |
|
| 6299 |
/* |
| 6300 |
* find all collations, including builtin collations; we filter out |
| 6301 |
* system-defined collations at dump-out time. |
| 6302 |
*/ |
| 6303 |
|
| 6304 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, collname, " |
| 6305 |
"collnamespace, " |
| 6306 |
"collowner, " |
| 6307 |
"collencoding " |
| 6308 |
"FROM pg_collation"); |
| 6309 |
|
| 6310 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6311 |
|
| 6312 |
ntups = PQntuples(res); |
| 6313 |
|
| 6314 |
collinfo = (CollInfo *) pg_malloc(ntups * sizeof(CollInfo)); |
| 6315 |
|
| 6316 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6317 |
i_oid = PQfnumber(res, "oid"); |
| 6318 |
i_collname = PQfnumber(res, "collname"); |
| 6319 |
i_collnamespace = PQfnumber(res, "collnamespace"); |
| 6320 |
i_collowner = PQfnumber(res, "collowner"); |
| 6321 |
i_collencoding = PQfnumber(res, "collencoding"); |
| 6322 |
|
| 6323 |
for (i = 0; i < ntups; i++) |
| 6324 |
{ |
| 6325 |
collinfo[i].dobj.objType = DO_COLLATION; |
| 6326 |
collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6327 |
collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6328 |
AssignDumpId(&collinfo[i].dobj); |
| 6329 |
collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname)); |
| 6330 |
collinfo[i].dobj.namespace = |
| 6331 |
findNamespace(atooid(PQgetvalue(res, i, i_collnamespace))); |
| 6332 |
collinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_collowner)); |
| 6333 |
collinfo[i].collencoding = atoi(PQgetvalue(res, i, i_collencoding)); |
| 6334 |
|
| 6335 |
/* Decide whether we want to dump it */ |
| 6336 |
selectDumpableObject(&(collinfo[i].dobj), fout); |
| 6337 |
} |
| 6338 |
|
| 6339 |
PQclear(res); |
| 6340 |
|
| 6341 |
destroyPQExpBuffer(query); |
| 6342 |
} |
| 6343 |
|
| 6344 |
/* |
| 6345 |
* getConversions: |
| 6346 |
* get information about all conversions in the system catalogs |
| 6347 |
*/ |
| 6348 |
void |
| 6349 |
getConversions(Archive *fout) |
| 6350 |
{ |
| 6351 |
PGresult *res; |
| 6352 |
int ntups; |
| 6353 |
int i; |
| 6354 |
PQExpBuffer query; |
| 6355 |
ConvInfo *convinfo; |
| 6356 |
int i_tableoid; |
| 6357 |
int i_oid; |
| 6358 |
int i_conname; |
| 6359 |
int i_connamespace; |
| 6360 |
int i_conowner; |
| 6361 |
|
| 6362 |
query = createPQExpBuffer(); |
| 6363 |
|
| 6364 |
/* |
| 6365 |
* find all conversions, including builtin conversions; we filter out |
| 6366 |
* system-defined conversions at dump-out time. |
| 6367 |
*/ |
| 6368 |
|
| 6369 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, conname, " |
| 6370 |
"connamespace, " |
| 6371 |
"conowner " |
| 6372 |
"FROM pg_conversion"); |
| 6373 |
|
| 6374 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6375 |
|
| 6376 |
ntups = PQntuples(res); |
| 6377 |
|
| 6378 |
convinfo = (ConvInfo *) pg_malloc(ntups * sizeof(ConvInfo)); |
| 6379 |
|
| 6380 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6381 |
i_oid = PQfnumber(res, "oid"); |
| 6382 |
i_conname = PQfnumber(res, "conname"); |
| 6383 |
i_connamespace = PQfnumber(res, "connamespace"); |
| 6384 |
i_conowner = PQfnumber(res, "conowner"); |
| 6385 |
|
| 6386 |
for (i = 0; i < ntups; i++) |
| 6387 |
{ |
| 6388 |
convinfo[i].dobj.objType = DO_CONVERSION; |
| 6389 |
convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6390 |
convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6391 |
AssignDumpId(&convinfo[i].dobj); |
| 6392 |
convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname)); |
| 6393 |
convinfo[i].dobj.namespace = |
| 6394 |
findNamespace(atooid(PQgetvalue(res, i, i_connamespace))); |
| 6395 |
convinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_conowner)); |
| 6396 |
|
| 6397 |
/* Decide whether we want to dump it */ |
| 6398 |
selectDumpableObject(&(convinfo[i].dobj), fout); |
| 6399 |
} |
| 6400 |
|
| 6401 |
PQclear(res); |
| 6402 |
|
| 6403 |
destroyPQExpBuffer(query); |
| 6404 |
} |
| 6405 |
|
| 6406 |
/* |
| 6407 |
* getAccessMethods: |
| 6408 |
* get information about all user-defined access methods |
| 6409 |
*/ |
| 6410 |
void |
| 6411 |
getAccessMethods(Archive *fout) |
| 6412 |
{ |
| 6413 |
PGresult *res; |
| 6414 |
int ntups; |
| 6415 |
int i; |
| 6416 |
PQExpBuffer query; |
| 6417 |
AccessMethodInfo *aminfo; |
| 6418 |
int i_tableoid; |
| 6419 |
int i_oid; |
| 6420 |
int i_amname; |
| 6421 |
int i_amhandler; |
| 6422 |
int i_amtype; |
| 6423 |
|
| 6424 |
query = createPQExpBuffer(); |
| 6425 |
|
| 6426 |
/* |
| 6427 |
* Select all access methods from pg_am table. v9.6 introduced CREATE |
| 6428 |
* ACCESS METHOD, so earlier versions usually have only built-in access |
| 6429 |
* methods. v9.6 also changed the access method API, replacing dozens of |
| 6430 |
* pg_am columns with amhandler. Even if a user created an access method |
| 6431 |
* by "INSERT INTO pg_am", we have no way to translate pre-v9.6 pg_am |
| 6432 |
* columns to a v9.6+ CREATE ACCESS METHOD. Hence, before v9.6, read |
| 6433 |
* pg_am just to facilitate findAccessMethodByOid() providing the |
| 6434 |
* OID-to-name mapping. |
| 6435 |
*/ |
| 6436 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, "); |
| 6437 |
if (fout->remoteVersion >= 90600) |
| 6438 |
appendPQExpBufferStr(query, |
| 6439 |
"amtype, " |
| 6440 |
"amhandler::pg_catalog.regproc AS amhandler "); |
| 6441 |
else |
| 6442 |
appendPQExpBufferStr(query, |
| 6443 |
"'i'::pg_catalog.\"char\" AS amtype, " |
| 6444 |
"'-'::pg_catalog.regproc AS amhandler "); |
| 6445 |
appendPQExpBufferStr(query, "FROM pg_am"); |
| 6446 |
|
| 6447 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6448 |
|
| 6449 |
ntups = PQntuples(res); |
| 6450 |
|
| 6451 |
aminfo = (AccessMethodInfo *) pg_malloc(ntups * sizeof(AccessMethodInfo)); |
| 6452 |
|
| 6453 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6454 |
i_oid = PQfnumber(res, "oid"); |
| 6455 |
i_amname = PQfnumber(res, "amname"); |
| 6456 |
i_amhandler = PQfnumber(res, "amhandler"); |
| 6457 |
i_amtype = PQfnumber(res, "amtype"); |
| 6458 |
|
| 6459 |
for (i = 0; i < ntups; i++) |
| 6460 |
{ |
| 6461 |
aminfo[i].dobj.objType = DO_ACCESS_METHOD; |
| 6462 |
aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6463 |
aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6464 |
AssignDumpId(&aminfo[i].dobj); |
| 6465 |
aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname)); |
| 6466 |
aminfo[i].dobj.namespace = NULL; |
| 6467 |
aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler)); |
| 6468 |
aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype)); |
| 6469 |
|
| 6470 |
/* Decide whether we want to dump it */ |
| 6471 |
selectDumpableAccessMethod(&(aminfo[i]), fout); |
| 6472 |
} |
| 6473 |
|
| 6474 |
PQclear(res); |
| 6475 |
|
| 6476 |
destroyPQExpBuffer(query); |
| 6477 |
} |
| 6478 |
|
| 6479 |
|
| 6480 |
/* |
| 6481 |
* getOpclasses: |
| 6482 |
* get information about all opclasses in the system catalogs |
| 6483 |
*/ |
| 6484 |
void |
| 6485 |
getOpclasses(Archive *fout) |
| 6486 |
{ |
| 6487 |
PGresult *res; |
| 6488 |
int ntups; |
| 6489 |
int i; |
| 6490 |
PQExpBuffer query = createPQExpBuffer(); |
| 6491 |
OpclassInfo *opcinfo; |
| 6492 |
int i_tableoid; |
| 6493 |
int i_oid; |
| 6494 |
int i_opcmethod; |
| 6495 |
int i_opcname; |
| 6496 |
int i_opcnamespace; |
| 6497 |
int i_opcowner; |
| 6498 |
|
| 6499 |
/* |
| 6500 |
* find all opclasses, including builtin opclasses; we filter out |
| 6501 |
* system-defined opclasses at dump-out time. |
| 6502 |
*/ |
| 6503 |
|
| 6504 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, opcmethod, opcname, " |
| 6505 |
"opcnamespace, " |
| 6506 |
"opcowner " |
| 6507 |
"FROM pg_opclass"); |
| 6508 |
|
| 6509 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6510 |
|
| 6511 |
ntups = PQntuples(res); |
| 6512 |
|
| 6513 |
opcinfo = (OpclassInfo *) pg_malloc(ntups * sizeof(OpclassInfo)); |
| 6514 |
|
| 6515 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6516 |
i_oid = PQfnumber(res, "oid"); |
| 6517 |
i_opcmethod = PQfnumber(res, "opcmethod"); |
| 6518 |
i_opcname = PQfnumber(res, "opcname"); |
| 6519 |
i_opcnamespace = PQfnumber(res, "opcnamespace"); |
| 6520 |
i_opcowner = PQfnumber(res, "opcowner"); |
| 6521 |
|
| 6522 |
for (i = 0; i < ntups; i++) |
| 6523 |
{ |
| 6524 |
opcinfo[i].dobj.objType = DO_OPCLASS; |
| 6525 |
opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6526 |
opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6527 |
AssignDumpId(&opcinfo[i].dobj); |
| 6528 |
opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname)); |
| 6529 |
opcinfo[i].dobj.namespace = |
| 6530 |
findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace))); |
| 6531 |
opcinfo[i].opcmethod = atooid(PQgetvalue(res, i, i_opcmethod)); |
| 6532 |
opcinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opcowner)); |
| 6533 |
|
| 6534 |
/* Decide whether we want to dump it */ |
| 6535 |
selectDumpableObject(&(opcinfo[i].dobj), fout); |
| 6536 |
} |
| 6537 |
|
| 6538 |
PQclear(res); |
| 6539 |
|
| 6540 |
destroyPQExpBuffer(query); |
| 6541 |
} |
| 6542 |
|
| 6543 |
/* |
| 6544 |
* getOpfamilies: |
| 6545 |
* get information about all opfamilies in the system catalogs |
| 6546 |
*/ |
| 6547 |
void |
| 6548 |
getOpfamilies(Archive *fout) |
| 6549 |
{ |
| 6550 |
PGresult *res; |
| 6551 |
int ntups; |
| 6552 |
int i; |
| 6553 |
PQExpBuffer query; |
| 6554 |
OpfamilyInfo *opfinfo; |
| 6555 |
int i_tableoid; |
| 6556 |
int i_oid; |
| 6557 |
int i_opfmethod; |
| 6558 |
int i_opfname; |
| 6559 |
int i_opfnamespace; |
| 6560 |
int i_opfowner; |
| 6561 |
|
| 6562 |
query = createPQExpBuffer(); |
| 6563 |
|
| 6564 |
/* |
| 6565 |
* find all opfamilies, including builtin opfamilies; we filter out |
| 6566 |
* system-defined opfamilies at dump-out time. |
| 6567 |
*/ |
| 6568 |
|
| 6569 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, opfmethod, opfname, " |
| 6570 |
"opfnamespace, " |
| 6571 |
"opfowner " |
| 6572 |
"FROM pg_opfamily"); |
| 6573 |
|
| 6574 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6575 |
|
| 6576 |
ntups = PQntuples(res); |
| 6577 |
|
| 6578 |
opfinfo = (OpfamilyInfo *) pg_malloc(ntups * sizeof(OpfamilyInfo)); |
| 6579 |
|
| 6580 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6581 |
i_oid = PQfnumber(res, "oid"); |
| 6582 |
i_opfname = PQfnumber(res, "opfname"); |
| 6583 |
i_opfmethod = PQfnumber(res, "opfmethod"); |
| 6584 |
i_opfnamespace = PQfnumber(res, "opfnamespace"); |
| 6585 |
i_opfowner = PQfnumber(res, "opfowner"); |
| 6586 |
|
| 6587 |
for (i = 0; i < ntups; i++) |
| 6588 |
{ |
| 6589 |
opfinfo[i].dobj.objType = DO_OPFAMILY; |
| 6590 |
opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6591 |
opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6592 |
AssignDumpId(&opfinfo[i].dobj); |
| 6593 |
opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname)); |
| 6594 |
opfinfo[i].dobj.namespace = |
| 6595 |
findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace))); |
| 6596 |
opfinfo[i].opfmethod = atooid(PQgetvalue(res, i, i_opfmethod)); |
| 6597 |
opfinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opfowner)); |
| 6598 |
|
| 6599 |
/* Decide whether we want to dump it */ |
| 6600 |
selectDumpableObject(&(opfinfo[i].dobj), fout); |
| 6601 |
} |
| 6602 |
|
| 6603 |
PQclear(res); |
| 6604 |
|
| 6605 |
destroyPQExpBuffer(query); |
| 6606 |
} |
| 6607 |
|
| 6608 |
/* |
| 6609 |
* getAggregates: |
| 6610 |
* get information about all user-defined aggregates in the system catalogs |
| 6611 |
*/ |
| 6612 |
void |
| 6613 |
getAggregates(Archive *fout) |
| 6614 |
{ |
| 6615 |
DumpOptions *dopt = fout->dopt; |
| 6616 |
PGresult *res; |
| 6617 |
int ntups; |
| 6618 |
int i; |
| 6619 |
PQExpBuffer query = createPQExpBuffer(); |
| 6620 |
AggInfo *agginfo; |
| 6621 |
int i_tableoid; |
| 6622 |
int i_oid; |
| 6623 |
int i_aggname; |
| 6624 |
int i_aggnamespace; |
| 6625 |
int i_pronargs; |
| 6626 |
int i_proargtypes; |
| 6627 |
int i_proowner; |
| 6628 |
int i_aggacl; |
| 6629 |
int i_acldefault; |
| 6630 |
|
| 6631 |
/* |
| 6632 |
* Find all interesting aggregates. See comment in getFuncs() for the |
| 6633 |
* rationale behind the filtering logic. |
| 6634 |
*/ |
| 6635 |
if (fout->remoteVersion >= 90600) |
| 6636 |
{ |
| 6637 |
const char *agg_check; |
| 6638 |
|
| 6639 |
agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'" |
| 6640 |
: "p.proisagg"); |
| 6641 |
|
| 6642 |
appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, " |
| 6643 |
"p.proname AS aggname, " |
| 6644 |
"p.pronamespace AS aggnamespace, " |
| 6645 |
"p.pronargs, p.proargtypes, " |
| 6646 |
"p.proowner, " |
| 6647 |
"p.proacl AS aggacl, " |
| 6648 |
"acldefault('f', p.proowner) AS acldefault " |
| 6649 |
"FROM pg_proc p " |
| 6650 |
"LEFT JOIN pg_init_privs pip ON " |
| 6651 |
"(p.oid = pip.objoid " |
| 6652 |
"AND pip.classoid = 'pg_proc'::regclass " |
| 6653 |
"AND pip.objsubid = 0) " |
| 6654 |
"WHERE %s AND (" |
| 6655 |
"p.pronamespace != " |
| 6656 |
"(SELECT oid FROM pg_namespace " |
| 6657 |
"WHERE nspname = 'pg_catalog') OR " |
| 6658 |
"p.proacl IS DISTINCT FROM pip.initprivs", |
| 6659 |
agg_check); |
| 6660 |
if (dopt->binary_upgrade) |
| 6661 |
appendPQExpBufferStr(query, |
| 6662 |
" OR EXISTS(SELECT 1 FROM pg_depend WHERE " |
| 6663 |
"classid = 'pg_proc'::regclass AND " |
| 6664 |
"objid = p.oid AND " |
| 6665 |
"refclassid = 'pg_extension'::regclass AND " |
| 6666 |
"deptype = 'e')"); |
| 6667 |
appendPQExpBufferChar(query, ')'); |
| 6668 |
} |
| 6669 |
else |
| 6670 |
{ |
| 6671 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, proname AS aggname, " |
| 6672 |
"pronamespace AS aggnamespace, " |
| 6673 |
"pronargs, proargtypes, " |
| 6674 |
"proowner, " |
| 6675 |
"proacl AS aggacl, " |
| 6676 |
"acldefault('f', proowner) AS acldefault " |
| 6677 |
"FROM pg_proc p " |
| 6678 |
"WHERE proisagg AND (" |
| 6679 |
"pronamespace != " |
| 6680 |
"(SELECT oid FROM pg_namespace " |
| 6681 |
"WHERE nspname = 'pg_catalog')"); |
| 6682 |
if (dopt->binary_upgrade) |
| 6683 |
appendPQExpBufferStr(query, |
| 6684 |
" OR EXISTS(SELECT 1 FROM pg_depend WHERE " |
| 6685 |
"classid = 'pg_proc'::regclass AND " |
| 6686 |
"objid = p.oid AND " |
| 6687 |
"refclassid = 'pg_extension'::regclass AND " |
| 6688 |
"deptype = 'e')"); |
| 6689 |
appendPQExpBufferChar(query, ')'); |
| 6690 |
} |
| 6691 |
|
| 6692 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6693 |
|
| 6694 |
ntups = PQntuples(res); |
| 6695 |
|
| 6696 |
agginfo = (AggInfo *) pg_malloc(ntups * sizeof(AggInfo)); |
| 6697 |
|
| 6698 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6699 |
i_oid = PQfnumber(res, "oid"); |
| 6700 |
i_aggname = PQfnumber(res, "aggname"); |
| 6701 |
i_aggnamespace = PQfnumber(res, "aggnamespace"); |
| 6702 |
i_pronargs = PQfnumber(res, "pronargs"); |
| 6703 |
i_proargtypes = PQfnumber(res, "proargtypes"); |
| 6704 |
i_proowner = PQfnumber(res, "proowner"); |
| 6705 |
i_aggacl = PQfnumber(res, "aggacl"); |
| 6706 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 6707 |
|
| 6708 |
for (i = 0; i < ntups; i++) |
| 6709 |
{ |
| 6710 |
agginfo[i].aggfn.dobj.objType = DO_AGG; |
| 6711 |
agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6712 |
agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6713 |
AssignDumpId(&agginfo[i].aggfn.dobj); |
| 6714 |
agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname)); |
| 6715 |
agginfo[i].aggfn.dobj.namespace = |
| 6716 |
findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace))); |
| 6717 |
agginfo[i].aggfn.dacl.acl = pg_strdup(PQgetvalue(res, i, i_aggacl)); |
| 6718 |
agginfo[i].aggfn.dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 6719 |
agginfo[i].aggfn.dacl.privtype = 0; |
| 6720 |
agginfo[i].aggfn.dacl.initprivs = NULL; |
| 6721 |
agginfo[i].aggfn.rolname = getRoleName(PQgetvalue(res, i, i_proowner)); |
| 6722 |
agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */ |
| 6723 |
agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */ |
| 6724 |
agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs)); |
| 6725 |
if (agginfo[i].aggfn.nargs == 0) |
| 6726 |
agginfo[i].aggfn.argtypes = NULL; |
| 6727 |
else |
| 6728 |
agginfo[i].aggfn.argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes), |
| 6729 |
agginfo[i].aggfn.nargs); |
| 6730 |
agginfo[i].aggfn.postponed_def = false; /* might get set during sort */ |
| 6731 |
|
| 6732 |
/* Decide whether we want to dump it */ |
| 6733 |
selectDumpableObject(&(agginfo[i].aggfn.dobj), fout); |
| 6734 |
|
| 6735 |
/* Mark whether aggregate has an ACL */ |
| 6736 |
if (!PQgetisnull(res, i, i_aggacl)) |
| 6737 |
agginfo[i].aggfn.dobj.components |= DUMP_COMPONENT_ACL; |
| 6738 |
} |
| 6739 |
|
| 6740 |
PQclear(res); |
| 6741 |
|
| 6742 |
destroyPQExpBuffer(query); |
| 6743 |
} |
| 6744 |
|
| 6745 |
/* |
| 6746 |
* getFuncs: |
| 6747 |
* get information about all user-defined functions in the system catalogs |
| 6748 |
*/ |
| 6749 |
void |
| 6750 |
getFuncs(Archive *fout) |
| 6751 |
{ |
| 6752 |
DumpOptions *dopt = fout->dopt; |
| 6753 |
PGresult *res; |
| 6754 |
int ntups; |
| 6755 |
int i; |
| 6756 |
PQExpBuffer query = createPQExpBuffer(); |
| 6757 |
FuncInfo *finfo; |
| 6758 |
int i_tableoid; |
| 6759 |
int i_oid; |
| 6760 |
int i_proname; |
| 6761 |
int i_pronamespace; |
| 6762 |
int i_proowner; |
| 6763 |
int i_prolang; |
| 6764 |
int i_pronargs; |
| 6765 |
int i_proargtypes; |
| 6766 |
int i_prorettype; |
| 6767 |
int i_proacl; |
| 6768 |
int i_acldefault; |
| 6769 |
|
| 6770 |
/* |
| 6771 |
* Find all interesting functions. This is a bit complicated: |
| 6772 |
* |
| 6773 |
* 1. Always exclude aggregates; those are handled elsewhere. |
| 6774 |
* |
| 6775 |
* 2. Always exclude functions that are internally dependent on something |
| 6776 |
* else, since presumably those will be created as a result of creating |
| 6777 |
* the something else. This currently acts only to suppress constructor |
| 6778 |
* functions for range types. Note this is OK only because the |
| 6779 |
* constructors don't have any dependencies the range type doesn't have; |
| 6780 |
* otherwise we might not get creation ordering correct. |
| 6781 |
* |
| 6782 |
* 3. Otherwise, we normally exclude functions in pg_catalog. However, if |
| 6783 |
* they're members of extensions and we are in binary-upgrade mode then |
| 6784 |
* include them, since we want to dump extension members individually in |
| 6785 |
* that mode. Also, if they are used by casts or transforms then we need |
| 6786 |
* to gather the information about them, though they won't be dumped if |
| 6787 |
* they are built-in. Also, in 9.6 and up, include functions in |
| 6788 |
* pg_catalog if they have an ACL different from what's shown in |
| 6789 |
* pg_init_privs (so we have to join to pg_init_privs; annoying). |
| 6790 |
*/ |
| 6791 |
if (fout->remoteVersion >= 90600) |
| 6792 |
{ |
| 6793 |
const char *not_agg_check; |
| 6794 |
|
| 6795 |
not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'" |
| 6796 |
: "NOT p.proisagg"); |
| 6797 |
|
| 6798 |
appendPQExpBuffer(query, |
| 6799 |
"SELECT p.tableoid, p.oid, p.proname, p.prolang, " |
| 6800 |
"p.pronargs, p.proargtypes, p.prorettype, " |
| 6801 |
"p.proacl, " |
| 6802 |
"acldefault('f', p.proowner) AS acldefault, " |
| 6803 |
"p.pronamespace, " |
| 6804 |
"p.proowner " |
| 6805 |
"FROM pg_proc p " |
| 6806 |
"LEFT JOIN pg_init_privs pip ON " |
| 6807 |
"(p.oid = pip.objoid " |
| 6808 |
"AND pip.classoid = 'pg_proc'::regclass " |
| 6809 |
"AND pip.objsubid = 0) " |
| 6810 |
"WHERE %s" |
| 6811 |
"\n AND NOT EXISTS (SELECT 1 FROM pg_depend " |
| 6812 |
"WHERE classid = 'pg_proc'::regclass AND " |
| 6813 |
"objid = p.oid AND deptype = 'i')" |
| 6814 |
"\n AND (" |
| 6815 |
"\n pronamespace != " |
| 6816 |
"(SELECT oid FROM pg_namespace " |
| 6817 |
"WHERE nspname = 'pg_catalog')" |
| 6818 |
"\n OR EXISTS (SELECT 1 FROM pg_cast" |
| 6819 |
"\n WHERE pg_cast.oid > %u " |
| 6820 |
"\n AND p.oid = pg_cast.castfunc)" |
| 6821 |
"\n OR EXISTS (SELECT 1 FROM pg_transform" |
| 6822 |
"\n WHERE pg_transform.oid > %u AND " |
| 6823 |
"\n (p.oid = pg_transform.trffromsql" |
| 6824 |
"\n OR p.oid = pg_transform.trftosql))", |
| 6825 |
not_agg_check, |
| 6826 |
g_last_builtin_oid, |
| 6827 |
g_last_builtin_oid); |
| 6828 |
if (dopt->binary_upgrade) |
| 6829 |
appendPQExpBufferStr(query, |
| 6830 |
"\n OR EXISTS(SELECT 1 FROM pg_depend WHERE " |
| 6831 |
"classid = 'pg_proc'::regclass AND " |
| 6832 |
"objid = p.oid AND " |
| 6833 |
"refclassid = 'pg_extension'::regclass AND " |
| 6834 |
"deptype = 'e')"); |
| 6835 |
appendPQExpBufferStr(query, |
| 6836 |
"\n OR p.proacl IS DISTINCT FROM pip.initprivs"); |
| 6837 |
appendPQExpBufferChar(query, ')'); |
| 6838 |
} |
| 6839 |
else |
| 6840 |
{ |
| 6841 |
appendPQExpBuffer(query, |
| 6842 |
"SELECT tableoid, oid, proname, prolang, " |
| 6843 |
"pronargs, proargtypes, prorettype, proacl, " |
| 6844 |
"acldefault('f', proowner) AS acldefault, " |
| 6845 |
"pronamespace, " |
| 6846 |
"proowner " |
| 6847 |
"FROM pg_proc p " |
| 6848 |
"WHERE NOT proisagg" |
| 6849 |
"\n AND NOT EXISTS (SELECT 1 FROM pg_depend " |
| 6850 |
"WHERE classid = 'pg_proc'::regclass AND " |
| 6851 |
"objid = p.oid AND deptype = 'i')" |
| 6852 |
"\n AND (" |
| 6853 |
"\n pronamespace != " |
| 6854 |
"(SELECT oid FROM pg_namespace " |
| 6855 |
"WHERE nspname = 'pg_catalog')" |
| 6856 |
"\n OR EXISTS (SELECT 1 FROM pg_cast" |
| 6857 |
"\n WHERE pg_cast.oid > '%u'::oid" |
| 6858 |
"\n AND p.oid = pg_cast.castfunc)", |
| 6859 |
g_last_builtin_oid); |
| 6860 |
|
| 6861 |
if (fout->remoteVersion >= 90500) |
| 6862 |
appendPQExpBuffer(query, |
| 6863 |
"\n OR EXISTS (SELECT 1 FROM pg_transform" |
| 6864 |
"\n WHERE pg_transform.oid > '%u'::oid" |
| 6865 |
"\n AND (p.oid = pg_transform.trffromsql" |
| 6866 |
"\n OR p.oid = pg_transform.trftosql))", |
| 6867 |
g_last_builtin_oid); |
| 6868 |
|
| 6869 |
if (dopt->binary_upgrade) |
| 6870 |
appendPQExpBufferStr(query, |
| 6871 |
"\n OR EXISTS(SELECT 1 FROM pg_depend WHERE " |
| 6872 |
"classid = 'pg_proc'::regclass AND " |
| 6873 |
"objid = p.oid AND " |
| 6874 |
"refclassid = 'pg_extension'::regclass AND " |
| 6875 |
"deptype = 'e')"); |
| 6876 |
appendPQExpBufferChar(query, ')'); |
| 6877 |
} |
| 6878 |
|
| 6879 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 6880 |
|
| 6881 |
ntups = PQntuples(res); |
| 6882 |
|
| 6883 |
finfo = (FuncInfo *) pg_malloc0(ntups * sizeof(FuncInfo)); |
| 6884 |
|
| 6885 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 6886 |
i_oid = PQfnumber(res, "oid"); |
| 6887 |
i_proname = PQfnumber(res, "proname"); |
| 6888 |
i_pronamespace = PQfnumber(res, "pronamespace"); |
| 6889 |
i_proowner = PQfnumber(res, "proowner"); |
| 6890 |
i_prolang = PQfnumber(res, "prolang"); |
| 6891 |
i_pronargs = PQfnumber(res, "pronargs"); |
| 6892 |
i_proargtypes = PQfnumber(res, "proargtypes"); |
| 6893 |
i_prorettype = PQfnumber(res, "prorettype"); |
| 6894 |
i_proacl = PQfnumber(res, "proacl"); |
| 6895 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 6896 |
|
| 6897 |
for (i = 0; i < ntups; i++) |
| 6898 |
{ |
| 6899 |
finfo[i].dobj.objType = DO_FUNC; |
| 6900 |
finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 6901 |
finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 6902 |
AssignDumpId(&finfo[i].dobj); |
| 6903 |
finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname)); |
| 6904 |
finfo[i].dobj.namespace = |
| 6905 |
findNamespace(atooid(PQgetvalue(res, i, i_pronamespace))); |
| 6906 |
finfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_proacl)); |
| 6907 |
finfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 6908 |
finfo[i].dacl.privtype = 0; |
| 6909 |
finfo[i].dacl.initprivs = NULL; |
| 6910 |
finfo[i].rolname = getRoleName(PQgetvalue(res, i, i_proowner)); |
| 6911 |
finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang)); |
| 6912 |
finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype)); |
| 6913 |
finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs)); |
| 6914 |
if (finfo[i].nargs == 0) |
| 6915 |
finfo[i].argtypes = NULL; |
| 6916 |
else |
| 6917 |
finfo[i].argtypes = parseOidArray(PQgetvalue(res, i, i_proargtypes), |
| 6918 |
finfo[i].nargs); |
| 6919 |
finfo[i].postponed_def = false; /* might get set during sort */ |
| 6920 |
|
| 6921 |
/* Decide whether we want to dump it */ |
| 6922 |
selectDumpableObject(&(finfo[i].dobj), fout); |
| 6923 |
|
| 6924 |
/* Mark whether function has an ACL */ |
| 6925 |
if (!PQgetisnull(res, i, i_proacl)) |
| 6926 |
finfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 6927 |
} |
| 6928 |
|
| 6929 |
PQclear(res); |
| 6930 |
|
| 6931 |
destroyPQExpBuffer(query); |
| 6932 |
} |
| 6933 |
|
| 6934 |
/* |
| 6935 |
* getRelationStatistics |
| 6936 |
* register the statistics object as a dependent of the relation. |
| 6937 |
* |
| 6938 |
* reltuples is passed as a string to avoid complexities in converting from/to |
| 6939 |
* floating point. |
| 6940 |
*/ |
| 6941 |
static RelStatsInfo * |
| 6942 |
getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages, |
| 6943 |
char *reltuples, int32 relallvisible, |
| 6944 |
int32 relallfrozen, char relkind, |
| 6945 |
char **indAttNames, int nindAttNames) |
| 6946 |
{ |
| 6947 |
if (!fout->dopt->dumpStatistics) |
| 6948 |
return NULL; |
| 6949 |
|
| 6950 |
if ((relkind == RELKIND_RELATION) || |
| 6951 |
(relkind == RELKIND_PARTITIONED_TABLE) || |
| 6952 |
(relkind == RELKIND_INDEX) || |
| 6953 |
(relkind == RELKIND_PARTITIONED_INDEX) || |
| 6954 |
(relkind == RELKIND_MATVIEW || |
| 6955 |
relkind == RELKIND_FOREIGN_TABLE)) |
| 6956 |
{ |
| 6957 |
RelStatsInfo *info = pg_malloc0(sizeof(RelStatsInfo)); |
| 6958 |
DumpableObject *dobj = &info->dobj; |
| 6959 |
|
| 6960 |
dobj->objType = DO_REL_STATS; |
| 6961 |
dobj->catId.tableoid = 0; |
| 6962 |
dobj->catId.oid = 0; |
| 6963 |
AssignDumpId(dobj); |
| 6964 |
dobj->dependencies = (DumpId *) pg_malloc(sizeof(DumpId)); |
| 6965 |
dobj->dependencies[0] = rel->dumpId; |
| 6966 |
dobj->nDeps = 1; |
| 6967 |
dobj->allocDeps = 1; |
| 6968 |
dobj->components |= DUMP_COMPONENT_STATISTICS; |
| 6969 |
dobj->name = pg_strdup(rel->name); |
| 6970 |
dobj->namespace = rel->namespace; |
| 6971 |
info->relpages = relpages; |
| 6972 |
info->reltuples = pstrdup(reltuples); |
| 6973 |
info->relallvisible = relallvisible; |
| 6974 |
info->relallfrozen = relallfrozen; |
| 6975 |
info->relkind = relkind; |
| 6976 |
info->indAttNames = indAttNames; |
| 6977 |
info->nindAttNames = nindAttNames; |
| 6978 |
|
| 6979 |
/* |
| 6980 |
* Ordinarily, stats go in SECTION_DATA for tables and |
| 6981 |
* SECTION_POST_DATA for indexes. |
| 6982 |
* |
| 6983 |
* However, the section may be updated later for materialized view |
| 6984 |
* stats. REFRESH MATERIALIZED VIEW replaces the storage and resets |
| 6985 |
* the stats, so the stats must be restored after the data. Also, the |
| 6986 |
* materialized view definition may be postponed to SECTION_POST_DATA |
| 6987 |
* (see repairMatViewBoundaryMultiLoop()). |
| 6988 |
*/ |
| 6989 |
switch (info->relkind) |
| 6990 |
{ |
| 6991 |
case RELKIND_RELATION: |
| 6992 |
case RELKIND_PARTITIONED_TABLE: |
| 6993 |
case RELKIND_MATVIEW: |
| 6994 |
case RELKIND_FOREIGN_TABLE: |
| 6995 |
info->section = SECTION_DATA; |
| 6996 |
break; |
| 6997 |
case RELKIND_INDEX: |
| 6998 |
case RELKIND_PARTITIONED_INDEX: |
| 6999 |
info->section = SECTION_POST_DATA; |
| 7000 |
break; |
| 7001 |
default: |
| 7002 |
pg_fatal("cannot dump statistics for relation kind \"%c\"", |
| 7003 |
info->relkind); |
| 7004 |
} |
| 7005 |
|
| 7006 |
return info; |
| 7007 |
} |
| 7008 |
return NULL; |
| 7009 |
} |
| 7010 |
|
| 7011 |
/* |
| 7012 |
* getTables |
| 7013 |
* read all the tables (no indexes) in the system catalogs, |
| 7014 |
* and return them as an array of TableInfo structures |
| 7015 |
* |
| 7016 |
* *numTables is set to the number of tables read in |
| 7017 |
*/ |
| 7018 |
TableInfo * |
| 7019 |
getTables(Archive *fout, int *numTables) |
| 7020 |
{ |
| 7021 |
DumpOptions *dopt = fout->dopt; |
| 7022 |
PGresult *res; |
| 7023 |
int ntups; |
| 7024 |
int i; |
| 7025 |
PQExpBuffer query = createPQExpBuffer(); |
| 7026 |
TableInfo *tblinfo; |
| 7027 |
int i_reltableoid; |
| 7028 |
int i_reloid; |
| 7029 |
int i_relname; |
| 7030 |
int i_relnamespace; |
| 7031 |
int i_relkind; |
| 7032 |
int i_reltype; |
| 7033 |
int i_relowner; |
| 7034 |
int i_relchecks; |
| 7035 |
int i_relhasindex; |
| 7036 |
int i_relhasrules; |
| 7037 |
int i_relpages; |
| 7038 |
int i_reltuples; |
| 7039 |
int i_relallvisible; |
| 7040 |
int i_relallfrozen; |
| 7041 |
int i_toastpages; |
| 7042 |
int i_owning_tab; |
| 7043 |
int i_owning_col; |
| 7044 |
int i_reltablespace; |
| 7045 |
int i_relhasoids; |
| 7046 |
int i_relhastriggers; |
| 7047 |
int i_relpersistence; |
| 7048 |
int i_relispopulated; |
| 7049 |
int i_relreplident; |
| 7050 |
int i_relrowsec; |
| 7051 |
int i_relforcerowsec; |
| 7052 |
int i_relfrozenxid; |
| 7053 |
int i_toastfrozenxid; |
| 7054 |
int i_toastoid; |
| 7055 |
int i_relminmxid; |
| 7056 |
int i_toastminmxid; |
| 7057 |
int i_reloptions; |
| 7058 |
int i_checkoption; |
| 7059 |
int i_toastreloptions; |
| 7060 |
int i_reloftype; |
| 7061 |
int i_foreignserver; |
| 7062 |
int i_amname; |
| 7063 |
int i_is_identity_sequence; |
| 7064 |
int i_relacl; |
| 7065 |
int i_acldefault; |
| 7066 |
int i_ispartition; |
| 7067 |
|
| 7068 |
/* |
| 7069 |
* Find all the tables and table-like objects. |
| 7070 |
* |
| 7071 |
* We must fetch all tables in this phase because otherwise we cannot |
| 7072 |
* correctly identify inherited columns, owned sequences, etc. |
| 7073 |
* |
| 7074 |
* We include system catalogs, so that we can work if a user table is |
| 7075 |
* defined to inherit from a system catalog (pretty weird, but...) |
| 7076 |
* |
| 7077 |
* Note: in this phase we should collect only a minimal amount of |
| 7078 |
* information about each table, basically just enough to decide if it is |
| 7079 |
* interesting. In particular, since we do not yet have lock on any user |
| 7080 |
* table, we MUST NOT invoke any server-side data collection functions |
| 7081 |
* (for instance, pg_get_partkeydef()). Those are likely to fail or give |
| 7082 |
* wrong answers if any concurrent DDL is happening. |
| 7083 |
*/ |
| 7084 |
|
| 7085 |
appendPQExpBufferStr(query, |
| 7086 |
"SELECT c.tableoid, c.oid, c.relname, " |
| 7087 |
"c.relnamespace, c.relkind, c.reltype, " |
| 7088 |
"c.relowner, " |
| 7089 |
"c.relchecks, " |
| 7090 |
"c.relhasindex, c.relhasrules, c.relpages, " |
| 7091 |
"c.reltuples, c.relallvisible, "); |
| 7092 |
|
| 7093 |
if (fout->remoteVersion >= 180000) |
| 7094 |
appendPQExpBufferStr(query, "c.relallfrozen, "); |
| 7095 |
else |
| 7096 |
appendPQExpBufferStr(query, "0 AS relallfrozen, "); |
| 7097 |
|
| 7098 |
appendPQExpBufferStr(query, |
| 7099 |
"c.relhastriggers, c.relpersistence, " |
| 7100 |
"c.reloftype, " |
| 7101 |
"c.relacl, " |
| 7102 |
"acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE) |
| 7103 |
" THEN 's'::\"char\" ELSE 'r'::\"char\" END, c.relowner) AS acldefault, " |
| 7104 |
"CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN " |
| 7105 |
"(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) " |
| 7106 |
"ELSE 0 END AS foreignserver, " |
| 7107 |
"c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, " |
| 7108 |
"tc.oid AS toid, " |
| 7109 |
"tc.relpages AS toastpages, " |
| 7110 |
"tc.reloptions AS toast_reloptions, " |
| 7111 |
"d.refobjid AS owning_tab, " |
| 7112 |
"d.refobjsubid AS owning_col, " |
| 7113 |
"tsp.spcname AS reltablespace, "); |
| 7114 |
|
| 7115 |
if (fout->remoteVersion >= 120000) |
| 7116 |
appendPQExpBufferStr(query, |
| 7117 |
"false AS relhasoids, "); |
| 7118 |
else |
| 7119 |
appendPQExpBufferStr(query, |
| 7120 |
"c.relhasoids, "); |
| 7121 |
|
| 7122 |
if (fout->remoteVersion >= 90300) |
| 7123 |
appendPQExpBufferStr(query, |
| 7124 |
"c.relispopulated, "); |
| 7125 |
else |
| 7126 |
appendPQExpBufferStr(query, |
| 7127 |
"'t' as relispopulated, "); |
| 7128 |
|
| 7129 |
if (fout->remoteVersion >= 90400) |
| 7130 |
appendPQExpBufferStr(query, |
| 7131 |
"c.relreplident, "); |
| 7132 |
else |
| 7133 |
appendPQExpBufferStr(query, |
| 7134 |
"'d' AS relreplident, "); |
| 7135 |
|
| 7136 |
if (fout->remoteVersion >= 90500) |
| 7137 |
appendPQExpBufferStr(query, |
| 7138 |
"c.relrowsecurity, c.relforcerowsecurity, "); |
| 7139 |
else |
| 7140 |
appendPQExpBufferStr(query, |
| 7141 |
"false AS relrowsecurity, " |
| 7142 |
"false AS relforcerowsecurity, "); |
| 7143 |
|
| 7144 |
if (fout->remoteVersion >= 90300) |
| 7145 |
appendPQExpBufferStr(query, |
| 7146 |
"c.relminmxid, tc.relminmxid AS tminmxid, "); |
| 7147 |
else |
| 7148 |
appendPQExpBufferStr(query, |
| 7149 |
"0 AS relminmxid, 0 AS tminmxid, "); |
| 7150 |
|
| 7151 |
if (fout->remoteVersion >= 90300) |
| 7152 |
appendPQExpBufferStr(query, |
| 7153 |
"array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, " |
| 7154 |
"CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text " |
| 7155 |
"WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "); |
| 7156 |
else |
| 7157 |
appendPQExpBufferStr(query, |
| 7158 |
"c.reloptions, NULL AS checkoption, "); |
| 7159 |
|
| 7160 |
if (fout->remoteVersion >= 90600) |
| 7161 |
appendPQExpBufferStr(query, |
| 7162 |
"am.amname, "); |
| 7163 |
else |
| 7164 |
appendPQExpBufferStr(query, |
| 7165 |
"NULL AS amname, "); |
| 7166 |
|
| 7167 |
if (fout->remoteVersion >= 90600) |
| 7168 |
appendPQExpBufferStr(query, |
| 7169 |
"(d.deptype = 'i') IS TRUE AS is_identity_sequence, "); |
| 7170 |
else |
| 7171 |
appendPQExpBufferStr(query, |
| 7172 |
"false AS is_identity_sequence, "); |
| 7173 |
|
| 7174 |
if (fout->remoteVersion >= 100000) |
| 7175 |
appendPQExpBufferStr(query, |
| 7176 |
"c.relispartition AS ispartition "); |
| 7177 |
else |
| 7178 |
appendPQExpBufferStr(query, |
| 7179 |
"false AS ispartition "); |
| 7180 |
|
| 7181 |
/* |
| 7182 |
* Left join to pg_depend to pick up dependency info linking sequences to |
| 7183 |
* their owning column, if any (note this dependency is AUTO except for |
| 7184 |
* identity sequences, where it's INTERNAL). Also join to pg_tablespace to |
| 7185 |
* collect the spcname. |
| 7186 |
*/ |
| 7187 |
appendPQExpBufferStr(query, |
| 7188 |
"\nFROM pg_class c\n" |
| 7189 |
"LEFT JOIN pg_depend d ON " |
| 7190 |
"(c.relkind = " CppAsString2(RELKIND_SEQUENCE) " AND " |
| 7191 |
"d.classid = 'pg_class'::regclass AND d.objid = c.oid AND " |
| 7192 |
"d.objsubid = 0 AND " |
| 7193 |
"d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i'))\n" |
| 7194 |
"LEFT JOIN pg_tablespace tsp ON (tsp.oid = c.reltablespace)\n"); |
| 7195 |
|
| 7196 |
/* |
| 7197 |
* In 9.6 and up, left join to pg_am to pick up the amname. |
| 7198 |
*/ |
| 7199 |
if (fout->remoteVersion >= 90600) |
| 7200 |
appendPQExpBufferStr(query, |
| 7201 |
"LEFT JOIN pg_am am ON (c.relam = am.oid)\n"); |
| 7202 |
|
| 7203 |
/* |
| 7204 |
* We purposefully ignore toast OIDs for partitioned tables; the reason is |
| 7205 |
* that versions 10 and 11 have them, but later versions do not, so |
| 7206 |
* emitting them causes the upgrade to fail. |
| 7207 |
*/ |
| 7208 |
appendPQExpBufferStr(query, |
| 7209 |
"LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid" |
| 7210 |
" AND tc.relkind = " CppAsString2(RELKIND_TOASTVALUE) |
| 7211 |
" AND c.relkind <> " CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n"); |
| 7212 |
|
| 7213 |
/* |
| 7214 |
* Restrict to interesting relkinds (in particular, not indexes). Not all |
| 7215 |
* relkinds are possible in older servers, but it's not worth the trouble |
| 7216 |
* to emit a version-dependent list. |
| 7217 |
* |
| 7218 |
* Composite-type table entries won't be dumped as such, but we have to |
| 7219 |
* make a DumpableObject for them so that we can track dependencies of the |
| 7220 |
* composite type (pg_depend entries for columns of the composite type |
| 7221 |
* link to the pg_class entry not the pg_type entry). |
| 7222 |
*/ |
| 7223 |
appendPQExpBufferStr(query, |
| 7224 |
"WHERE c.relkind IN (" |
| 7225 |
CppAsString2(RELKIND_RELATION) ", " |
| 7226 |
CppAsString2(RELKIND_SEQUENCE) ", " |
| 7227 |
CppAsString2(RELKIND_VIEW) ", " |
| 7228 |
CppAsString2(RELKIND_COMPOSITE_TYPE) ", " |
| 7229 |
CppAsString2(RELKIND_MATVIEW) ", " |
| 7230 |
CppAsString2(RELKIND_FOREIGN_TABLE) ", " |
| 7231 |
CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n" |
| 7232 |
"ORDER BY c.oid"); |
| 7233 |
|
| 7234 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 7235 |
|
| 7236 |
ntups = PQntuples(res); |
| 7237 |
|
| 7238 |
*numTables = ntups; |
| 7239 |
|
| 7240 |
/* |
| 7241 |
* Extract data from result and lock dumpable tables. We do the locking |
| 7242 |
* before anything else, to minimize the window wherein a table could |
| 7243 |
* disappear under us. |
| 7244 |
* |
| 7245 |
* Note that we have to save info about all tables here, even when dumping |
| 7246 |
* only one, because we don't yet know which tables might be inheritance |
| 7247 |
* ancestors of the target table. |
| 7248 |
*/ |
| 7249 |
tblinfo = (TableInfo *) pg_malloc0(ntups * sizeof(TableInfo)); |
| 7250 |
|
| 7251 |
i_reltableoid = PQfnumber(res, "tableoid"); |
| 7252 |
i_reloid = PQfnumber(res, "oid"); |
| 7253 |
i_relname = PQfnumber(res, "relname"); |
| 7254 |
i_relnamespace = PQfnumber(res, "relnamespace"); |
| 7255 |
i_relkind = PQfnumber(res, "relkind"); |
| 7256 |
i_reltype = PQfnumber(res, "reltype"); |
| 7257 |
i_relowner = PQfnumber(res, "relowner"); |
| 7258 |
i_relchecks = PQfnumber(res, "relchecks"); |
| 7259 |
i_relhasindex = PQfnumber(res, "relhasindex"); |
| 7260 |
i_relhasrules = PQfnumber(res, "relhasrules"); |
| 7261 |
i_relpages = PQfnumber(res, "relpages"); |
| 7262 |
i_reltuples = PQfnumber(res, "reltuples"); |
| 7263 |
i_relallvisible = PQfnumber(res, "relallvisible"); |
| 7264 |
i_relallfrozen = PQfnumber(res, "relallfrozen"); |
| 7265 |
i_toastpages = PQfnumber(res, "toastpages"); |
| 7266 |
i_owning_tab = PQfnumber(res, "owning_tab"); |
| 7267 |
i_owning_col = PQfnumber(res, "owning_col"); |
| 7268 |
i_reltablespace = PQfnumber(res, "reltablespace"); |
| 7269 |
i_relhasoids = PQfnumber(res, "relhasoids"); |
| 7270 |
i_relhastriggers = PQfnumber(res, "relhastriggers"); |
| 7271 |
i_relpersistence = PQfnumber(res, "relpersistence"); |
| 7272 |
i_relispopulated = PQfnumber(res, "relispopulated"); |
| 7273 |
i_relreplident = PQfnumber(res, "relreplident"); |
| 7274 |
i_relrowsec = PQfnumber(res, "relrowsecurity"); |
| 7275 |
i_relforcerowsec = PQfnumber(res, "relforcerowsecurity"); |
| 7276 |
i_relfrozenxid = PQfnumber(res, "relfrozenxid"); |
| 7277 |
i_toastfrozenxid = PQfnumber(res, "tfrozenxid"); |
| 7278 |
i_toastoid = PQfnumber(res, "toid"); |
| 7279 |
i_relminmxid = PQfnumber(res, "relminmxid"); |
| 7280 |
i_toastminmxid = PQfnumber(res, "tminmxid"); |
| 7281 |
i_reloptions = PQfnumber(res, "reloptions"); |
| 7282 |
i_checkoption = PQfnumber(res, "checkoption"); |
| 7283 |
i_toastreloptions = PQfnumber(res, "toast_reloptions"); |
| 7284 |
i_reloftype = PQfnumber(res, "reloftype"); |
| 7285 |
i_foreignserver = PQfnumber(res, "foreignserver"); |
| 7286 |
i_amname = PQfnumber(res, "amname"); |
| 7287 |
i_is_identity_sequence = PQfnumber(res, "is_identity_sequence"); |
| 7288 |
i_relacl = PQfnumber(res, "relacl"); |
| 7289 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 7290 |
i_ispartition = PQfnumber(res, "ispartition"); |
| 7291 |
|
| 7292 |
if (dopt->lockWaitTimeout) |
| 7293 |
{ |
| 7294 |
/* |
| 7295 |
* Arrange to fail instead of waiting forever for a table lock. |
| 7296 |
* |
| 7297 |
* NB: this coding assumes that the only queries issued within the |
| 7298 |
* following loop are LOCK TABLEs; else the timeout may be undesirably |
| 7299 |
* applied to other things too. |
| 7300 |
*/ |
| 7301 |
resetPQExpBuffer(query); |
| 7302 |
appendPQExpBufferStr(query, "SET statement_timeout = "); |
| 7303 |
appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout)); |
| 7304 |
ExecuteSqlStatement(fout, query->data); |
| 7305 |
} |
| 7306 |
|
| 7307 |
resetPQExpBuffer(query); |
| 7308 |
|
| 7309 |
for (i = 0; i < ntups; i++) |
| 7310 |
{ |
| 7311 |
int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible)); |
| 7312 |
int32 relallfrozen = atoi(PQgetvalue(res, i, i_relallfrozen)); |
| 7313 |
|
| 7314 |
tblinfo[i].dobj.objType = DO_TABLE; |
| 7315 |
tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid)); |
| 7316 |
tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid)); |
| 7317 |
AssignDumpId(&tblinfo[i].dobj); |
| 7318 |
tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname)); |
| 7319 |
tblinfo[i].dobj.namespace = |
| 7320 |
findNamespace(atooid(PQgetvalue(res, i, i_relnamespace))); |
| 7321 |
tblinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_relacl)); |
| 7322 |
tblinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 7323 |
tblinfo[i].dacl.privtype = 0; |
| 7324 |
tblinfo[i].dacl.initprivs = NULL; |
| 7325 |
tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind)); |
| 7326 |
tblinfo[i].reltype = atooid(PQgetvalue(res, i, i_reltype)); |
| 7327 |
tblinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_relowner)); |
| 7328 |
tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks)); |
| 7329 |
tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0); |
| 7330 |
tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0); |
| 7331 |
tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages)); |
| 7332 |
if (PQgetisnull(res, i, i_toastpages)) |
| 7333 |
tblinfo[i].toastpages = 0; |
| 7334 |
else |
| 7335 |
tblinfo[i].toastpages = atoi(PQgetvalue(res, i, i_toastpages)); |
| 7336 |
if (PQgetisnull(res, i, i_owning_tab)) |
| 7337 |
{ |
| 7338 |
tblinfo[i].owning_tab = InvalidOid; |
| 7339 |
tblinfo[i].owning_col = 0; |
| 7340 |
} |
| 7341 |
else |
| 7342 |
{ |
| 7343 |
tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab)); |
| 7344 |
tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col)); |
| 7345 |
} |
| 7346 |
tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace)); |
| 7347 |
tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0); |
| 7348 |
tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0); |
| 7349 |
tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence)); |
| 7350 |
tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0); |
| 7351 |
tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident)); |
| 7352 |
tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0); |
| 7353 |
tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0); |
| 7354 |
tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid)); |
| 7355 |
tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid)); |
| 7356 |
tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid)); |
| 7357 |
tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid)); |
| 7358 |
tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid)); |
| 7359 |
tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions)); |
| 7360 |
if (PQgetisnull(res, i, i_checkoption)) |
| 7361 |
tblinfo[i].checkoption = NULL; |
| 7362 |
else |
| 7363 |
tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption)); |
| 7364 |
tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions)); |
| 7365 |
tblinfo[i].reloftype = atooid(PQgetvalue(res, i, i_reloftype)); |
| 7366 |
tblinfo[i].foreign_server = atooid(PQgetvalue(res, i, i_foreignserver)); |
| 7367 |
if (PQgetisnull(res, i, i_amname)) |
| 7368 |
tblinfo[i].amname = NULL; |
| 7369 |
else |
| 7370 |
tblinfo[i].amname = pg_strdup(PQgetvalue(res, i, i_amname)); |
| 7371 |
tblinfo[i].is_identity_sequence = (strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0); |
| 7372 |
tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0); |
| 7373 |
|
| 7374 |
/* other fields were zeroed above */ |
| 7375 |
|
| 7376 |
/* |
| 7377 |
* Decide whether we want to dump this table. |
| 7378 |
*/ |
| 7379 |
if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE) |
| 7380 |
tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE; |
| 7381 |
else |
| 7382 |
selectDumpableTable(&tblinfo[i], fout); |
| 7383 |
|
| 7384 |
/* |
| 7385 |
* Now, consider the table "interesting" if we need to dump its |
| 7386 |
* definition, data or its statistics. Later on, we'll skip a lot of |
| 7387 |
* data collection for uninteresting tables. |
| 7388 |
* |
| 7389 |
* Note: the "interesting" flag will also be set by flagInhTables for |
| 7390 |
* parents of interesting tables, so that we collect necessary |
| 7391 |
* inheritance info even when the parents are not themselves being |
| 7392 |
* dumped. This is the main reason why we need an "interesting" flag |
| 7393 |
* that's separate from the components-to-dump bitmask. |
| 7394 |
*/ |
| 7395 |
tblinfo[i].interesting = (tblinfo[i].dobj.dump & |
| 7396 |
(DUMP_COMPONENT_DEFINITION | |
| 7397 |
DUMP_COMPONENT_DATA | |
| 7398 |
DUMP_COMPONENT_STATISTICS)) != 0; |
| 7399 |
|
| 7400 |
tblinfo[i].dummy_view = false; /* might get set during sort */ |
| 7401 |
tblinfo[i].postponed_def = false; /* might get set during sort */ |
| 7402 |
|
| 7403 |
/* Tables have data */ |
| 7404 |
tblinfo[i].dobj.components |= DUMP_COMPONENT_DATA; |
| 7405 |
|
| 7406 |
/* Mark whether table has an ACL */ |
| 7407 |
if (!PQgetisnull(res, i, i_relacl)) |
| 7408 |
tblinfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 7409 |
tblinfo[i].hascolumnACLs = false; /* may get set later */ |
| 7410 |
|
| 7411 |
/* Add statistics */ |
| 7412 |
if (tblinfo[i].interesting) |
| 7413 |
{ |
| 7414 |
RelStatsInfo *stats; |
| 7415 |
|
| 7416 |
stats = getRelationStatistics(fout, &tblinfo[i].dobj, |
| 7417 |
tblinfo[i].relpages, |
| 7418 |
PQgetvalue(res, i, i_reltuples), |
| 7419 |
relallvisible, relallfrozen, |
| 7420 |
tblinfo[i].relkind, NULL, 0); |
| 7421 |
if (tblinfo[i].relkind == RELKIND_MATVIEW) |
| 7422 |
tblinfo[i].stats = stats; |
| 7423 |
} |
| 7424 |
|
| 7425 |
/* |
| 7426 |
* Read-lock target tables to make sure they aren't DROPPED or altered |
| 7427 |
* in schema before we get around to dumping them. |
| 7428 |
* |
| 7429 |
* Note that we don't explicitly lock parents of the target tables; we |
| 7430 |
* assume our lock on the child is enough to prevent schema |
| 7431 |
* alterations to parent tables. |
| 7432 |
* |
| 7433 |
* NOTE: it'd be kinda nice to lock other relations too, not only |
| 7434 |
* plain or partitioned tables, but the backend doesn't presently |
| 7435 |
* allow that. |
| 7436 |
* |
| 7437 |
* We only need to lock the table for certain components; see |
| 7438 |
* pg_dump.h |
| 7439 |
*/ |
| 7440 |
if ((tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK) && |
| 7441 |
(tblinfo[i].relkind == RELKIND_RELATION || |
| 7442 |
tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE)) |
| 7443 |
{ |
| 7444 |
/* |
| 7445 |
* Tables are locked in batches. When dumping from a remote |
| 7446 |
* server this can save a significant amount of time by reducing |
| 7447 |
* the number of round trips. |
| 7448 |
*/ |
| 7449 |
if (query->len == 0) |
| 7450 |
appendPQExpBuffer(query, "LOCK TABLE %s", |
| 7451 |
fmtQualifiedDumpable(&tblinfo[i])); |
| 7452 |
else |
| 7453 |
{ |
| 7454 |
appendPQExpBuffer(query, ", %s", |
| 7455 |
fmtQualifiedDumpable(&tblinfo[i])); |
| 7456 |
|
| 7457 |
/* Arbitrarily end a batch when query length reaches 100K. */ |
| 7458 |
if (query->len >= 100000) |
| 7459 |
{ |
| 7460 |
/* Lock another batch of tables. */ |
| 7461 |
appendPQExpBufferStr(query, " IN ACCESS SHARE MODE"); |
| 7462 |
ExecuteSqlStatement(fout, query->data); |
| 7463 |
resetPQExpBuffer(query); |
| 7464 |
} |
| 7465 |
} |
| 7466 |
} |
| 7467 |
} |
| 7468 |
|
| 7469 |
if (query->len != 0) |
| 7470 |
{ |
| 7471 |
/* Lock the tables in the last batch. */ |
| 7472 |
appendPQExpBufferStr(query, " IN ACCESS SHARE MODE"); |
| 7473 |
ExecuteSqlStatement(fout, query->data); |
| 7474 |
} |
| 7475 |
|
| 7476 |
if (dopt->lockWaitTimeout) |
| 7477 |
{ |
| 7478 |
ExecuteSqlStatement(fout, "SET statement_timeout = 0"); |
| 7479 |
} |
| 7480 |
|
| 7481 |
PQclear(res); |
| 7482 |
|
| 7483 |
destroyPQExpBuffer(query); |
| 7484 |
|
| 7485 |
return tblinfo; |
| 7486 |
} |
| 7487 |
|
| 7488 |
/* |
| 7489 |
* getOwnedSeqs |
| 7490 |
* identify owned sequences and mark them as dumpable if owning table is |
| 7491 |
* |
| 7492 |
* We used to do this in getTables(), but it's better to do it after the |
| 7493 |
* index used by findTableByOid() has been set up. |
| 7494 |
*/ |
| 7495 |
void |
| 7496 |
getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables) |
| 7497 |
{ |
| 7498 |
int i; |
| 7499 |
|
| 7500 |
/* |
| 7501 |
* Force sequences that are "owned" by table columns to be dumped whenever |
| 7502 |
* their owning table is being dumped. |
| 7503 |
*/ |
| 7504 |
for (i = 0; i < numTables; i++) |
| 7505 |
{ |
| 7506 |
TableInfo *seqinfo = &tblinfo[i]; |
| 7507 |
TableInfo *owning_tab; |
| 7508 |
|
| 7509 |
if (!OidIsValid(seqinfo->owning_tab)) |
| 7510 |
continue; /* not an owned sequence */ |
| 7511 |
|
| 7512 |
owning_tab = findTableByOid(seqinfo->owning_tab); |
| 7513 |
if (owning_tab == NULL) |
| 7514 |
pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found", |
| 7515 |
seqinfo->owning_tab, seqinfo->dobj.catId.oid); |
| 7516 |
|
| 7517 |
/* |
| 7518 |
* For an identity sequence, dump exactly the same components for the |
| 7519 |
* sequence as for the owning table. This is important because we |
| 7520 |
* treat the identity sequence as an integral part of the table. For |
| 7521 |
* example, there is not any DDL command that allows creation of such |
| 7522 |
* a sequence independently of the table. |
| 7523 |
* |
| 7524 |
* For other owned sequences such as serial sequences, we need to dump |
| 7525 |
* the components that are being dumped for the table and any |
| 7526 |
* components that the sequence is explicitly marked with. |
| 7527 |
* |
| 7528 |
* We can't simply use the set of components which are being dumped |
| 7529 |
* for the table as the table might be in an extension (and only the |
| 7530 |
* non-extension components, eg: ACLs if changed, security labels, and |
| 7531 |
* policies, are being dumped) while the sequence is not (and |
| 7532 |
* therefore the definition and other components should also be |
| 7533 |
* dumped). |
| 7534 |
* |
| 7535 |
* If the sequence is part of the extension then it should be properly |
| 7536 |
* marked by checkExtensionMembership() and this will be a no-op as |
| 7537 |
* the table will be equivalently marked. |
| 7538 |
*/ |
| 7539 |
if (seqinfo->is_identity_sequence) |
| 7540 |
seqinfo->dobj.dump = owning_tab->dobj.dump; |
| 7541 |
else |
| 7542 |
seqinfo->dobj.dump |= owning_tab->dobj.dump; |
| 7543 |
|
| 7544 |
/* Make sure that necessary data is available if we're dumping it */ |
| 7545 |
if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE) |
| 7546 |
{ |
| 7547 |
seqinfo->interesting = true; |
| 7548 |
owning_tab->interesting = true; |
| 7549 |
} |
| 7550 |
} |
| 7551 |
} |
| 7552 |
|
| 7553 |
/* |
| 7554 |
* getInherits |
| 7555 |
* read all the inheritance information |
| 7556 |
* from the system catalogs return them in the InhInfo* structure |
| 7557 |
* |
| 7558 |
* numInherits is set to the number of pairs read in |
| 7559 |
*/ |
| 7560 |
InhInfo * |
| 7561 |
getInherits(Archive *fout, int *numInherits) |
| 7562 |
{ |
| 7563 |
PGresult *res; |
| 7564 |
int ntups; |
| 7565 |
int i; |
| 7566 |
PQExpBuffer query = createPQExpBuffer(); |
| 7567 |
InhInfo *inhinfo; |
| 7568 |
|
| 7569 |
int i_inhrelid; |
| 7570 |
int i_inhparent; |
| 7571 |
|
| 7572 |
/* find all the inheritance information */ |
| 7573 |
appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits"); |
| 7574 |
|
| 7575 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 7576 |
|
| 7577 |
ntups = PQntuples(res); |
| 7578 |
|
| 7579 |
*numInherits = ntups; |
| 7580 |
|
| 7581 |
inhinfo = (InhInfo *) pg_malloc(ntups * sizeof(InhInfo)); |
| 7582 |
|
| 7583 |
i_inhrelid = PQfnumber(res, "inhrelid"); |
| 7584 |
i_inhparent = PQfnumber(res, "inhparent"); |
| 7585 |
|
| 7586 |
for (i = 0; i < ntups; i++) |
| 7587 |
{ |
| 7588 |
inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid)); |
| 7589 |
inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent)); |
| 7590 |
} |
| 7591 |
|
| 7592 |
PQclear(res); |
| 7593 |
|
| 7594 |
destroyPQExpBuffer(query); |
| 7595 |
|
| 7596 |
return inhinfo; |
| 7597 |
} |
| 7598 |
|
| 7599 |
/* |
| 7600 |
* getPartitioningInfo |
| 7601 |
* get information about partitioning |
| 7602 |
* |
| 7603 |
* For the most part, we only collect partitioning info about tables we |
| 7604 |
* intend to dump. However, this function has to consider all partitioned |
| 7605 |
* tables in the database, because we need to know about parents of partitions |
| 7606 |
* we are going to dump even if the parents themselves won't be dumped. |
| 7607 |
* |
| 7608 |
* Specifically, what we need to know is whether each partitioned table |
| 7609 |
* has an "unsafe" partitioning scheme that requires us to force |
| 7610 |
* load-via-partition-root mode for its children. Currently the only case |
| 7611 |
* for which we force that is hash partitioning on enum columns, since the |
| 7612 |
* hash codes depend on enum value OIDs which won't be replicated across |
| 7613 |
* dump-and-reload. There are other cases in which load-via-partition-root |
| 7614 |
* might be necessary, but we expect users to cope with them. |
| 7615 |
*/ |
| 7616 |
void |
| 7617 |
getPartitioningInfo(Archive *fout) |
| 7618 |
{ |
| 7619 |
PQExpBuffer query; |
| 7620 |
PGresult *res; |
| 7621 |
int ntups; |
| 7622 |
|
| 7623 |
/* hash partitioning didn't exist before v11 */ |
| 7624 |
if (fout->remoteVersion < 110000) |
| 7625 |
return; |
| 7626 |
/* needn't bother if not dumping data */ |
| 7627 |
if (!fout->dopt->dumpData) |
| 7628 |
return; |
| 7629 |
|
| 7630 |
query = createPQExpBuffer(); |
| 7631 |
|
| 7632 |
/* |
| 7633 |
* Unsafe partitioning schemes are exactly those for which hash enum_ops |
| 7634 |
* appears among the partition opclasses. We needn't check partstrat. |
| 7635 |
* |
| 7636 |
* Note that this query may well retrieve info about tables we aren't |
| 7637 |
* going to dump and hence have no lock on. That's okay since we need not |
| 7638 |
* invoke any unsafe server-side functions. |
| 7639 |
*/ |
| 7640 |
appendPQExpBufferStr(query, |
| 7641 |
"SELECT partrelid FROM pg_partitioned_table WHERE\n" |
| 7642 |
"(SELECT c.oid FROM pg_opclass c JOIN pg_am a " |
| 7643 |
"ON c.opcmethod = a.oid\n" |
| 7644 |
"WHERE opcname = 'enum_ops' " |
| 7645 |
"AND opcnamespace = 'pg_catalog'::regnamespace " |
| 7646 |
"AND amname = 'hash') = ANY(partclass)"); |
| 7647 |
|
| 7648 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 7649 |
|
| 7650 |
ntups = PQntuples(res); |
| 7651 |
|
| 7652 |
for (int i = 0; i < ntups; i++) |
| 7653 |
{ |
| 7654 |
Oid tabrelid = atooid(PQgetvalue(res, i, 0)); |
| 7655 |
TableInfo *tbinfo; |
| 7656 |
|
| 7657 |
tbinfo = findTableByOid(tabrelid); |
| 7658 |
if (tbinfo == NULL) |
| 7659 |
pg_fatal("failed sanity check, table OID %u appearing in pg_partitioned_table not found", |
| 7660 |
tabrelid); |
| 7661 |
tbinfo->unsafe_partitions = true; |
| 7662 |
} |
| 7663 |
|
| 7664 |
PQclear(res); |
| 7665 |
|
| 7666 |
destroyPQExpBuffer(query); |
| 7667 |
} |
| 7668 |
|
| 7669 |
/* |
| 7670 |
* getIndexes |
| 7671 |
* get information about every index on a dumpable table |
| 7672 |
* |
| 7673 |
* Note: index data is not returned directly to the caller, but it |
| 7674 |
* does get entered into the DumpableObject tables. |
| 7675 |
*/ |
| 7676 |
void |
| 7677 |
getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) |
| 7678 |
{ |
| 7679 |
PQExpBuffer query = createPQExpBuffer(); |
| 7680 |
PQExpBuffer tbloids = createPQExpBuffer(); |
| 7681 |
PGresult *res; |
| 7682 |
int ntups; |
| 7683 |
int curtblindx; |
| 7684 |
IndxInfo *indxinfo; |
| 7685 |
int i_tableoid, |
| 7686 |
i_oid, |
| 7687 |
i_indrelid, |
| 7688 |
i_indexname, |
| 7689 |
i_relpages, |
| 7690 |
i_reltuples, |
| 7691 |
i_relallvisible, |
| 7692 |
i_relallfrozen, |
| 7693 |
i_parentidx, |
| 7694 |
i_indexdef, |
| 7695 |
i_indnkeyatts, |
| 7696 |
i_indnatts, |
| 7697 |
i_indkey, |
| 7698 |
i_indisclustered, |
| 7699 |
i_indisreplident, |
| 7700 |
i_indnullsnotdistinct, |
| 7701 |
i_contype, |
| 7702 |
i_conname, |
| 7703 |
i_condeferrable, |
| 7704 |
i_condeferred, |
| 7705 |
i_conperiod, |
| 7706 |
i_contableoid, |
| 7707 |
i_conoid, |
| 7708 |
i_condef, |
| 7709 |
i_indattnames, |
| 7710 |
i_tablespace, |
| 7711 |
i_indreloptions, |
| 7712 |
i_indstatcols, |
| 7713 |
i_indstatvals; |
| 7714 |
|
| 7715 |
/* |
| 7716 |
* We want to perform just one query against pg_index. However, we |
| 7717 |
* mustn't try to select every row of the catalog and then sort it out on |
| 7718 |
* the client side, because some of the server-side functions we need |
| 7719 |
* would be unsafe to apply to tables we don't have lock on. Hence, we |
| 7720 |
* build an array of the OIDs of tables we care about (and now have lock |
| 7721 |
* on!), and use a WHERE clause to constrain which rows are selected. |
| 7722 |
*/ |
| 7723 |
appendPQExpBufferChar(tbloids, '{'); |
| 7724 |
for (int i = 0; i < numTables; i++) |
| 7725 |
{ |
| 7726 |
TableInfo *tbinfo = &tblinfo[i]; |
| 7727 |
|
| 7728 |
if (!tbinfo->hasindex) |
| 7729 |
continue; |
| 7730 |
|
| 7731 |
/* |
| 7732 |
* We can ignore indexes of uninteresting tables. |
| 7733 |
*/ |
| 7734 |
if (!tbinfo->interesting) |
| 7735 |
continue; |
| 7736 |
|
| 7737 |
/* OK, we need info for this table */ |
| 7738 |
if (tbloids->len > 1) /* do we have more than the '{'? */ |
| 7739 |
appendPQExpBufferChar(tbloids, ','); |
| 7740 |
appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); |
| 7741 |
} |
| 7742 |
appendPQExpBufferChar(tbloids, '}'); |
| 7743 |
|
| 7744 |
appendPQExpBufferStr(query, |
| 7745 |
"SELECT t.tableoid, t.oid, i.indrelid, " |
| 7746 |
"t.relname AS indexname, " |
| 7747 |
"t.relpages, t.reltuples, t.relallvisible, "); |
| 7748 |
|
| 7749 |
if (fout->remoteVersion >= 180000) |
| 7750 |
appendPQExpBufferStr(query, "t.relallfrozen, "); |
| 7751 |
else |
| 7752 |
appendPQExpBufferStr(query, "0 AS relallfrozen, "); |
| 7753 |
|
| 7754 |
appendPQExpBufferStr(query, |
| 7755 |
"pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, " |
| 7756 |
"i.indkey, i.indisclustered, " |
| 7757 |
"c.contype, c.conname, " |
| 7758 |
"c.condeferrable, c.condeferred, " |
| 7759 |
"c.tableoid AS contableoid, " |
| 7760 |
"c.oid AS conoid, " |
| 7761 |
"pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, " |
| 7762 |
"CASE WHEN i.indexprs IS NOT NULL THEN " |
| 7763 |
"(SELECT pg_catalog.array_agg(attname ORDER BY attnum)" |
| 7764 |
" FROM pg_catalog.pg_attribute " |
| 7765 |
" WHERE attrelid = i.indexrelid) " |
| 7766 |
"ELSE NULL END AS indattnames, " |
| 7767 |
"(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " |
| 7768 |
"t.reloptions AS indreloptions, "); |
| 7769 |
|
| 7770 |
|
| 7771 |
if (fout->remoteVersion >= 90400) |
| 7772 |
appendPQExpBufferStr(query, |
| 7773 |
"i.indisreplident, "); |
| 7774 |
else |
| 7775 |
appendPQExpBufferStr(query, |
| 7776 |
"false AS indisreplident, "); |
| 7777 |
|
| 7778 |
if (fout->remoteVersion >= 110000) |
| 7779 |
appendPQExpBufferStr(query, |
| 7780 |
"inh.inhparent AS parentidx, " |
| 7781 |
"i.indnkeyatts AS indnkeyatts, " |
| 7782 |
"i.indnatts AS indnatts, " |
| 7783 |
"(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) " |
| 7784 |
" FROM pg_catalog.pg_attribute " |
| 7785 |
" WHERE attrelid = i.indexrelid AND " |
| 7786 |
" attstattarget >= 0) AS indstatcols, " |
| 7787 |
"(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) " |
| 7788 |
" FROM pg_catalog.pg_attribute " |
| 7789 |
" WHERE attrelid = i.indexrelid AND " |
| 7790 |
" attstattarget >= 0) AS indstatvals, "); |
| 7791 |
else |
| 7792 |
appendPQExpBufferStr(query, |
| 7793 |
"0 AS parentidx, " |
| 7794 |
"i.indnatts AS indnkeyatts, " |
| 7795 |
"i.indnatts AS indnatts, " |
| 7796 |
"'' AS indstatcols, " |
| 7797 |
"'' AS indstatvals, "); |
| 7798 |
|
| 7799 |
if (fout->remoteVersion >= 150000) |
| 7800 |
appendPQExpBufferStr(query, |
| 7801 |
"i.indnullsnotdistinct, "); |
| 7802 |
else |
| 7803 |
appendPQExpBufferStr(query, |
| 7804 |
"false AS indnullsnotdistinct, "); |
| 7805 |
|
| 7806 |
if (fout->remoteVersion >= 180000) |
| 7807 |
appendPQExpBufferStr(query, |
| 7808 |
"c.conperiod "); |
| 7809 |
else |
| 7810 |
appendPQExpBufferStr(query, |
| 7811 |
"NULL AS conperiod "); |
| 7812 |
|
| 7813 |
/* |
| 7814 |
* The point of the messy-looking outer join is to find a constraint that |
| 7815 |
* is related by an internal dependency link to the index. If we find one, |
| 7816 |
* create a CONSTRAINT entry linked to the INDEX entry. We assume an |
| 7817 |
* index won't have more than one internal dependency. |
| 7818 |
* |
| 7819 |
* Note: the check on conrelid is redundant, but useful because that |
| 7820 |
* column is indexed while conindid is not. |
| 7821 |
*/ |
| 7822 |
if (fout->remoteVersion >= 110000) |
| 7823 |
{ |
| 7824 |
appendPQExpBuffer(query, |
| 7825 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 7826 |
"JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) " |
| 7827 |
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " |
| 7828 |
"JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) " |
| 7829 |
"LEFT JOIN pg_catalog.pg_constraint c " |
| 7830 |
"ON (i.indrelid = c.conrelid AND " |
| 7831 |
"i.indexrelid = c.conindid AND " |
| 7832 |
"c.contype IN ('p','u','x')) " |
| 7833 |
"LEFT JOIN pg_catalog.pg_inherits inh " |
| 7834 |
"ON (inh.inhrelid = indexrelid) " |
| 7835 |
"WHERE (i.indisvalid OR t2.relkind = 'p') " |
| 7836 |
"AND i.indisready " |
| 7837 |
"ORDER BY i.indrelid, indexname", |
| 7838 |
tbloids->data); |
| 7839 |
} |
| 7840 |
else |
| 7841 |
{ |
| 7842 |
/* |
| 7843 |
* the test on indisready is necessary in 9.2, and harmless in |
| 7844 |
* earlier/later versions |
| 7845 |
*/ |
| 7846 |
appendPQExpBuffer(query, |
| 7847 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 7848 |
"JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) " |
| 7849 |
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " |
| 7850 |
"LEFT JOIN pg_catalog.pg_constraint c " |
| 7851 |
"ON (i.indrelid = c.conrelid AND " |
| 7852 |
"i.indexrelid = c.conindid AND " |
| 7853 |
"c.contype IN ('p','u','x')) " |
| 7854 |
"WHERE i.indisvalid AND i.indisready " |
| 7855 |
"ORDER BY i.indrelid, indexname", |
| 7856 |
tbloids->data); |
| 7857 |
} |
| 7858 |
|
| 7859 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 7860 |
|
| 7861 |
ntups = PQntuples(res); |
| 7862 |
|
| 7863 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 7864 |
i_oid = PQfnumber(res, "oid"); |
| 7865 |
i_indrelid = PQfnumber(res, "indrelid"); |
| 7866 |
i_indexname = PQfnumber(res, "indexname"); |
| 7867 |
i_relpages = PQfnumber(res, "relpages"); |
| 7868 |
i_reltuples = PQfnumber(res, "reltuples"); |
| 7869 |
i_relallvisible = PQfnumber(res, "relallvisible"); |
| 7870 |
i_relallfrozen = PQfnumber(res, "relallfrozen"); |
| 7871 |
i_parentidx = PQfnumber(res, "parentidx"); |
| 7872 |
i_indexdef = PQfnumber(res, "indexdef"); |
| 7873 |
i_indnkeyatts = PQfnumber(res, "indnkeyatts"); |
| 7874 |
i_indnatts = PQfnumber(res, "indnatts"); |
| 7875 |
i_indkey = PQfnumber(res, "indkey"); |
| 7876 |
i_indisclustered = PQfnumber(res, "indisclustered"); |
| 7877 |
i_indisreplident = PQfnumber(res, "indisreplident"); |
| 7878 |
i_indnullsnotdistinct = PQfnumber(res, "indnullsnotdistinct"); |
| 7879 |
i_contype = PQfnumber(res, "contype"); |
| 7880 |
i_conname = PQfnumber(res, "conname"); |
| 7881 |
i_condeferrable = PQfnumber(res, "condeferrable"); |
| 7882 |
i_condeferred = PQfnumber(res, "condeferred"); |
| 7883 |
i_conperiod = PQfnumber(res, "conperiod"); |
| 7884 |
i_contableoid = PQfnumber(res, "contableoid"); |
| 7885 |
i_conoid = PQfnumber(res, "conoid"); |
| 7886 |
i_condef = PQfnumber(res, "condef"); |
| 7887 |
i_indattnames = PQfnumber(res, "indattnames"); |
| 7888 |
i_tablespace = PQfnumber(res, "tablespace"); |
| 7889 |
i_indreloptions = PQfnumber(res, "indreloptions"); |
| 7890 |
i_indstatcols = PQfnumber(res, "indstatcols"); |
| 7891 |
i_indstatvals = PQfnumber(res, "indstatvals"); |
| 7892 |
|
| 7893 |
indxinfo = (IndxInfo *) pg_malloc(ntups * sizeof(IndxInfo)); |
| 7894 |
|
| 7895 |
/* |
| 7896 |
* Outer loop iterates once per table, not once per row. Incrementing of |
| 7897 |
* j is handled by the inner loop. |
| 7898 |
*/ |
| 7899 |
curtblindx = -1; |
| 7900 |
for (int j = 0; j < ntups;) |
| 7901 |
{ |
| 7902 |
Oid indrelid = atooid(PQgetvalue(res, j, i_indrelid)); |
| 7903 |
TableInfo *tbinfo = NULL; |
| 7904 |
int numinds; |
| 7905 |
|
| 7906 |
/* Count rows for this table */ |
| 7907 |
for (numinds = 1; numinds < ntups - j; numinds++) |
| 7908 |
if (atooid(PQgetvalue(res, j + numinds, i_indrelid)) != indrelid) |
| 7909 |
break; |
| 7910 |
|
| 7911 |
/* |
| 7912 |
* Locate the associated TableInfo; we rely on tblinfo[] being in OID |
| 7913 |
* order. |
| 7914 |
*/ |
| 7915 |
while (++curtblindx < numTables) |
| 7916 |
{ |
| 7917 |
tbinfo = &tblinfo[curtblindx]; |
| 7918 |
if (tbinfo->dobj.catId.oid == indrelid) |
| 7919 |
break; |
| 7920 |
} |
| 7921 |
if (curtblindx >= numTables) |
| 7922 |
pg_fatal("unrecognized table OID %u", indrelid); |
| 7923 |
/* cross-check that we only got requested tables */ |
| 7924 |
if (!tbinfo->hasindex || |
| 7925 |
!tbinfo->interesting) |
| 7926 |
pg_fatal("unexpected index data for table \"%s\"", |
| 7927 |
tbinfo->dobj.name); |
| 7928 |
|
| 7929 |
/* Save data for this table */ |
| 7930 |
tbinfo->indexes = indxinfo + j; |
| 7931 |
tbinfo->numIndexes = numinds; |
| 7932 |
|
| 7933 |
for (int c = 0; c < numinds; c++, j++) |
| 7934 |
{ |
| 7935 |
char contype; |
| 7936 |
char indexkind; |
| 7937 |
char **indAttNames = NULL; |
| 7938 |
int nindAttNames = 0; |
| 7939 |
RelStatsInfo *relstats; |
| 7940 |
int32 relpages = atoi(PQgetvalue(res, j, i_relpages)); |
| 7941 |
int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible)); |
| 7942 |
int32 relallfrozen = atoi(PQgetvalue(res, j, i_relallfrozen)); |
| 7943 |
|
| 7944 |
indxinfo[j].dobj.objType = DO_INDEX; |
| 7945 |
indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); |
| 7946 |
indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); |
| 7947 |
AssignDumpId(&indxinfo[j].dobj); |
| 7948 |
indxinfo[j].dobj.dump = tbinfo->dobj.dump; |
| 7949 |
indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname)); |
| 7950 |
indxinfo[j].dobj.namespace = tbinfo->dobj.namespace; |
| 7951 |
indxinfo[j].indextable = tbinfo; |
| 7952 |
indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef)); |
| 7953 |
indxinfo[j].indnkeyattrs = atoi(PQgetvalue(res, j, i_indnkeyatts)); |
| 7954 |
indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts)); |
| 7955 |
indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace)); |
| 7956 |
indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions)); |
| 7957 |
indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols)); |
| 7958 |
indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals)); |
| 7959 |
indxinfo[j].indkeys = parseIntArray(PQgetvalue(res, j, i_indkey), |
| 7960 |
indxinfo[j].indnattrs); |
| 7961 |
indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't'); |
| 7962 |
indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't'); |
| 7963 |
indxinfo[j].indnullsnotdistinct = (PQgetvalue(res, j, i_indnullsnotdistinct)[0] == 't'); |
| 7964 |
indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx)); |
| 7965 |
indxinfo[j].partattaches = (SimplePtrList) |
| 7966 |
{ |
| 7967 |
NULL, NULL |
| 7968 |
}; |
| 7969 |
|
| 7970 |
if (indxinfo[j].parentidx == 0) |
| 7971 |
indexkind = RELKIND_INDEX; |
| 7972 |
else |
| 7973 |
indexkind = RELKIND_PARTITIONED_INDEX; |
| 7974 |
|
| 7975 |
if (!PQgetisnull(res, j, i_indattnames)) |
| 7976 |
{ |
| 7977 |
if (!parsePGArray(PQgetvalue(res, j, i_indattnames), |
| 7978 |
&indAttNames, &nindAttNames)) |
| 7979 |
pg_fatal("could not parse %s array", "indattnames"); |
| 7980 |
} |
| 7981 |
|
| 7982 |
relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages, |
| 7983 |
PQgetvalue(res, j, i_reltuples), |
| 7984 |
relallvisible, relallfrozen, indexkind, |
| 7985 |
indAttNames, nindAttNames); |
| 7986 |
|
| 7987 |
contype = *(PQgetvalue(res, j, i_contype)); |
| 7988 |
if (contype == 'p' || contype == 'u' || contype == 'x') |
| 7989 |
{ |
| 7990 |
/* |
| 7991 |
* If we found a constraint matching the index, create an |
| 7992 |
* entry for it. |
| 7993 |
*/ |
| 7994 |
ConstraintInfo *constrinfo; |
| 7995 |
|
| 7996 |
constrinfo = (ConstraintInfo *) pg_malloc(sizeof(ConstraintInfo)); |
| 7997 |
constrinfo->dobj.objType = DO_CONSTRAINT; |
| 7998 |
constrinfo->dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid)); |
| 7999 |
constrinfo->dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid)); |
| 8000 |
AssignDumpId(&constrinfo->dobj); |
| 8001 |
constrinfo->dobj.dump = tbinfo->dobj.dump; |
| 8002 |
constrinfo->dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); |
| 8003 |
constrinfo->dobj.namespace = tbinfo->dobj.namespace; |
| 8004 |
constrinfo->contable = tbinfo; |
| 8005 |
constrinfo->condomain = NULL; |
| 8006 |
constrinfo->contype = contype; |
| 8007 |
if (contype == 'x') |
| 8008 |
constrinfo->condef = pg_strdup(PQgetvalue(res, j, i_condef)); |
| 8009 |
else |
| 8010 |
constrinfo->condef = NULL; |
| 8011 |
constrinfo->confrelid = InvalidOid; |
| 8012 |
constrinfo->conindex = indxinfo[j].dobj.dumpId; |
| 8013 |
constrinfo->condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't'; |
| 8014 |
constrinfo->condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't'; |
| 8015 |
constrinfo->conperiod = *(PQgetvalue(res, j, i_conperiod)) == 't'; |
| 8016 |
constrinfo->conislocal = true; |
| 8017 |
constrinfo->separate = true; |
| 8018 |
|
| 8019 |
indxinfo[j].indexconstraint = constrinfo->dobj.dumpId; |
| 8020 |
if (relstats != NULL) |
| 8021 |
addObjectDependency(&relstats->dobj, constrinfo->dobj.dumpId); |
| 8022 |
} |
| 8023 |
else |
| 8024 |
{ |
| 8025 |
/* Plain secondary index */ |
| 8026 |
indxinfo[j].indexconstraint = 0; |
| 8027 |
} |
| 8028 |
} |
| 8029 |
} |
| 8030 |
|
| 8031 |
PQclear(res); |
| 8032 |
|
| 8033 |
destroyPQExpBuffer(query); |
| 8034 |
destroyPQExpBuffer(tbloids); |
| 8035 |
} |
| 8036 |
|
| 8037 |
/* |
| 8038 |
* getExtendedStatistics |
| 8039 |
* get information about extended-statistics objects. |
| 8040 |
* |
| 8041 |
* Note: extended statistics data is not returned directly to the caller, but |
| 8042 |
* it does get entered into the DumpableObject tables. |
| 8043 |
*/ |
| 8044 |
void |
| 8045 |
getExtendedStatistics(Archive *fout) |
| 8046 |
{ |
| 8047 |
PQExpBuffer query; |
| 8048 |
PGresult *res; |
| 8049 |
StatsExtInfo *statsextinfo; |
| 8050 |
int ntups; |
| 8051 |
int i_tableoid; |
| 8052 |
int i_oid; |
| 8053 |
int i_stxname; |
| 8054 |
int i_stxnamespace; |
| 8055 |
int i_stxowner; |
| 8056 |
int i_stxrelid; |
| 8057 |
int i_stattarget; |
| 8058 |
int i; |
| 8059 |
|
| 8060 |
/* Extended statistics were new in v10 */ |
| 8061 |
if (fout->remoteVersion < 100000) |
| 8062 |
return; |
| 8063 |
|
| 8064 |
query = createPQExpBuffer(); |
| 8065 |
|
| 8066 |
if (fout->remoteVersion < 130000) |
| 8067 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " |
| 8068 |
"stxnamespace, stxowner, stxrelid, NULL AS stxstattarget " |
| 8069 |
"FROM pg_catalog.pg_statistic_ext"); |
| 8070 |
else |
| 8071 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " |
| 8072 |
"stxnamespace, stxowner, stxrelid, stxstattarget " |
| 8073 |
"FROM pg_catalog.pg_statistic_ext"); |
| 8074 |
|
| 8075 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8076 |
|
| 8077 |
ntups = PQntuples(res); |
| 8078 |
|
| 8079 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 8080 |
i_oid = PQfnumber(res, "oid"); |
| 8081 |
i_stxname = PQfnumber(res, "stxname"); |
| 8082 |
i_stxnamespace = PQfnumber(res, "stxnamespace"); |
| 8083 |
i_stxowner = PQfnumber(res, "stxowner"); |
| 8084 |
i_stxrelid = PQfnumber(res, "stxrelid"); |
| 8085 |
i_stattarget = PQfnumber(res, "stxstattarget"); |
| 8086 |
|
| 8087 |
statsextinfo = (StatsExtInfo *) pg_malloc(ntups * sizeof(StatsExtInfo)); |
| 8088 |
|
| 8089 |
for (i = 0; i < ntups; i++) |
| 8090 |
{ |
| 8091 |
statsextinfo[i].dobj.objType = DO_STATSEXT; |
| 8092 |
statsextinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 8093 |
statsextinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 8094 |
AssignDumpId(&statsextinfo[i].dobj); |
| 8095 |
statsextinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_stxname)); |
| 8096 |
statsextinfo[i].dobj.namespace = |
| 8097 |
findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace))); |
| 8098 |
statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner)); |
| 8099 |
statsextinfo[i].stattable = |
| 8100 |
findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid))); |
| 8101 |
if (PQgetisnull(res, i, i_stattarget)) |
| 8102 |
statsextinfo[i].stattarget = -1; |
| 8103 |
else |
| 8104 |
statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget)); |
| 8105 |
|
| 8106 |
/* Decide whether we want to dump it */ |
| 8107 |
selectDumpableStatisticsObject(&(statsextinfo[i]), fout); |
| 8108 |
} |
| 8109 |
|
| 8110 |
PQclear(res); |
| 8111 |
destroyPQExpBuffer(query); |
| 8112 |
} |
| 8113 |
|
| 8114 |
/* |
| 8115 |
* getConstraints |
| 8116 |
* |
| 8117 |
* Get info about constraints on dumpable tables. |
| 8118 |
* |
| 8119 |
* Currently handles foreign keys only. |
| 8120 |
* Unique and primary key constraints are handled with indexes, |
| 8121 |
* while check constraints are processed in getTableAttrs(). |
| 8122 |
*/ |
| 8123 |
void |
| 8124 |
getConstraints(Archive *fout, TableInfo tblinfo[], int numTables) |
| 8125 |
{ |
| 8126 |
PQExpBuffer query = createPQExpBuffer(); |
| 8127 |
PQExpBuffer tbloids = createPQExpBuffer(); |
| 8128 |
PGresult *res; |
| 8129 |
int ntups; |
| 8130 |
int curtblindx; |
| 8131 |
TableInfo *tbinfo = NULL; |
| 8132 |
ConstraintInfo *constrinfo; |
| 8133 |
int i_contableoid, |
| 8134 |
i_conoid, |
| 8135 |
i_conrelid, |
| 8136 |
i_conname, |
| 8137 |
i_confrelid, |
| 8138 |
i_conindid, |
| 8139 |
i_condef; |
| 8140 |
|
| 8141 |
/* |
| 8142 |
* We want to perform just one query against pg_constraint. However, we |
| 8143 |
* mustn't try to select every row of the catalog and then sort it out on |
| 8144 |
* the client side, because some of the server-side functions we need |
| 8145 |
* would be unsafe to apply to tables we don't have lock on. Hence, we |
| 8146 |
* build an array of the OIDs of tables we care about (and now have lock |
| 8147 |
* on!), and use a WHERE clause to constrain which rows are selected. |
| 8148 |
*/ |
| 8149 |
appendPQExpBufferChar(tbloids, '{'); |
| 8150 |
for (int i = 0; i < numTables; i++) |
| 8151 |
{ |
| 8152 |
TableInfo *tinfo = &tblinfo[i]; |
| 8153 |
|
| 8154 |
if (!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)) |
| 8155 |
continue; |
| 8156 |
|
| 8157 |
/* OK, we need info for this table */ |
| 8158 |
if (tbloids->len > 1) /* do we have more than the '{'? */ |
| 8159 |
appendPQExpBufferChar(tbloids, ','); |
| 8160 |
appendPQExpBuffer(tbloids, "%u", tinfo->dobj.catId.oid); |
| 8161 |
} |
| 8162 |
appendPQExpBufferChar(tbloids, '}'); |
| 8163 |
|
| 8164 |
appendPQExpBufferStr(query, |
| 8165 |
"SELECT c.tableoid, c.oid, " |
| 8166 |
"conrelid, conname, confrelid, "); |
| 8167 |
if (fout->remoteVersion >= 110000) |
| 8168 |
appendPQExpBufferStr(query, "conindid, "); |
| 8169 |
else |
| 8170 |
appendPQExpBufferStr(query, "0 AS conindid, "); |
| 8171 |
appendPQExpBuffer(query, |
| 8172 |
"pg_catalog.pg_get_constraintdef(c.oid) AS condef\n" |
| 8173 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 8174 |
"JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n" |
| 8175 |
"WHERE contype = 'f' ", |
| 8176 |
tbloids->data); |
| 8177 |
if (fout->remoteVersion >= 110000) |
| 8178 |
appendPQExpBufferStr(query, |
| 8179 |
"AND conparentid = 0 "); |
| 8180 |
appendPQExpBufferStr(query, |
| 8181 |
"ORDER BY conrelid, conname"); |
| 8182 |
|
| 8183 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8184 |
|
| 8185 |
ntups = PQntuples(res); |
| 8186 |
|
| 8187 |
i_contableoid = PQfnumber(res, "tableoid"); |
| 8188 |
i_conoid = PQfnumber(res, "oid"); |
| 8189 |
i_conrelid = PQfnumber(res, "conrelid"); |
| 8190 |
i_conname = PQfnumber(res, "conname"); |
| 8191 |
i_confrelid = PQfnumber(res, "confrelid"); |
| 8192 |
i_conindid = PQfnumber(res, "conindid"); |
| 8193 |
i_condef = PQfnumber(res, "condef"); |
| 8194 |
|
| 8195 |
constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo)); |
| 8196 |
|
| 8197 |
curtblindx = -1; |
| 8198 |
for (int j = 0; j < ntups; j++) |
| 8199 |
{ |
| 8200 |
Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid)); |
| 8201 |
TableInfo *reftable; |
| 8202 |
|
| 8203 |
/* |
| 8204 |
* Locate the associated TableInfo; we rely on tblinfo[] being in OID |
| 8205 |
* order. |
| 8206 |
*/ |
| 8207 |
if (tbinfo == NULL || tbinfo->dobj.catId.oid != conrelid) |
| 8208 |
{ |
| 8209 |
while (++curtblindx < numTables) |
| 8210 |
{ |
| 8211 |
tbinfo = &tblinfo[curtblindx]; |
| 8212 |
if (tbinfo->dobj.catId.oid == conrelid) |
| 8213 |
break; |
| 8214 |
} |
| 8215 |
if (curtblindx >= numTables) |
| 8216 |
pg_fatal("unrecognized table OID %u", conrelid); |
| 8217 |
} |
| 8218 |
|
| 8219 |
constrinfo[j].dobj.objType = DO_FK_CONSTRAINT; |
| 8220 |
constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid)); |
| 8221 |
constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid)); |
| 8222 |
AssignDumpId(&constrinfo[j].dobj); |
| 8223 |
constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); |
| 8224 |
constrinfo[j].dobj.namespace = tbinfo->dobj.namespace; |
| 8225 |
constrinfo[j].contable = tbinfo; |
| 8226 |
constrinfo[j].condomain = NULL; |
| 8227 |
constrinfo[j].contype = 'f'; |
| 8228 |
constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef)); |
| 8229 |
constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid)); |
| 8230 |
constrinfo[j].conindex = 0; |
| 8231 |
constrinfo[j].condeferrable = false; |
| 8232 |
constrinfo[j].condeferred = false; |
| 8233 |
constrinfo[j].conislocal = true; |
| 8234 |
constrinfo[j].separate = true; |
| 8235 |
|
| 8236 |
/* |
| 8237 |
* Restoring an FK that points to a partitioned table requires that |
| 8238 |
* all partition indexes have been attached beforehand. Ensure that |
| 8239 |
* happens by making the constraint depend on each index partition |
| 8240 |
* attach object. |
| 8241 |
*/ |
| 8242 |
reftable = findTableByOid(constrinfo[j].confrelid); |
| 8243 |
if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE) |
| 8244 |
{ |
| 8245 |
Oid indexOid = atooid(PQgetvalue(res, j, i_conindid)); |
| 8246 |
|
| 8247 |
if (indexOid != InvalidOid) |
| 8248 |
{ |
| 8249 |
for (int k = 0; k < reftable->numIndexes; k++) |
| 8250 |
{ |
| 8251 |
IndxInfo *refidx; |
| 8252 |
|
| 8253 |
/* not our index? */ |
| 8254 |
if (reftable->indexes[k].dobj.catId.oid != indexOid) |
| 8255 |
continue; |
| 8256 |
|
| 8257 |
refidx = &reftable->indexes[k]; |
| 8258 |
addConstrChildIdxDeps(&constrinfo[j].dobj, refidx); |
| 8259 |
break; |
| 8260 |
} |
| 8261 |
} |
| 8262 |
} |
| 8263 |
} |
| 8264 |
|
| 8265 |
PQclear(res); |
| 8266 |
|
| 8267 |
destroyPQExpBuffer(query); |
| 8268 |
destroyPQExpBuffer(tbloids); |
| 8269 |
} |
| 8270 |
|
| 8271 |
/* |
| 8272 |
* addConstrChildIdxDeps |
| 8273 |
* |
| 8274 |
* Recursive subroutine for getConstraints |
| 8275 |
* |
| 8276 |
* Given an object representing a foreign key constraint and an index on the |
| 8277 |
* partitioned table it references, mark the constraint object as dependent |
| 8278 |
* on the DO_INDEX_ATTACH object of each index partition, recursively |
| 8279 |
* drilling down to their partitions if any. This ensures that the FK is not |
| 8280 |
* restored until the index is fully marked valid. |
| 8281 |
*/ |
| 8282 |
static void |
| 8283 |
addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx) |
| 8284 |
{ |
| 8285 |
SimplePtrListCell *cell; |
| 8286 |
|
| 8287 |
Assert(dobj->objType == DO_FK_CONSTRAINT); |
| 8288 |
|
| 8289 |
for (cell = refidx->partattaches.head; cell; cell = cell->next) |
| 8290 |
{ |
| 8291 |
IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr; |
| 8292 |
|
| 8293 |
addObjectDependency(dobj, attach->dobj.dumpId); |
| 8294 |
|
| 8295 |
if (attach->partitionIdx->partattaches.head != NULL) |
| 8296 |
addConstrChildIdxDeps(dobj, attach->partitionIdx); |
| 8297 |
} |
| 8298 |
} |
| 8299 |
|
| 8300 |
/* |
| 8301 |
* getDomainConstraints |
| 8302 |
* |
| 8303 |
* Get info about constraints on a domain. |
| 8304 |
*/ |
| 8305 |
static void |
| 8306 |
getDomainConstraints(Archive *fout, TypeInfo *tyinfo) |
| 8307 |
{ |
| 8308 |
ConstraintInfo *constrinfo; |
| 8309 |
PQExpBuffer query = createPQExpBuffer(); |
| 8310 |
PGresult *res; |
| 8311 |
int i_tableoid, |
| 8312 |
i_oid, |
| 8313 |
i_conname, |
| 8314 |
i_consrc, |
| 8315 |
i_convalidated, |
| 8316 |
i_contype; |
| 8317 |
int ntups; |
| 8318 |
|
| 8319 |
if (!fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS]) |
| 8320 |
{ |
| 8321 |
/* |
| 8322 |
* Set up query for constraint-specific details. For servers 17 and |
| 8323 |
* up, domains have constraints of type 'n' as well as 'c', otherwise |
| 8324 |
* just the latter. |
| 8325 |
*/ |
| 8326 |
appendPQExpBuffer(query, |
| 8327 |
"PREPARE getDomainConstraints(pg_catalog.oid) AS\n" |
| 8328 |
"SELECT tableoid, oid, conname, " |
| 8329 |
"pg_catalog.pg_get_constraintdef(oid) AS consrc, " |
| 8330 |
"convalidated, contype " |
| 8331 |
"FROM pg_catalog.pg_constraint " |
| 8332 |
"WHERE contypid = $1 AND contype IN (%s) " |
| 8333 |
"ORDER BY conname", |
| 8334 |
fout->remoteVersion < 170000 ? "'c'" : "'c', 'n'"); |
| 8335 |
|
| 8336 |
ExecuteSqlStatement(fout, query->data); |
| 8337 |
|
| 8338 |
fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS] = true; |
| 8339 |
} |
| 8340 |
|
| 8341 |
printfPQExpBuffer(query, |
| 8342 |
"EXECUTE getDomainConstraints('%u')", |
| 8343 |
tyinfo->dobj.catId.oid); |
| 8344 |
|
| 8345 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8346 |
|
| 8347 |
ntups = PQntuples(res); |
| 8348 |
|
| 8349 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 8350 |
i_oid = PQfnumber(res, "oid"); |
| 8351 |
i_conname = PQfnumber(res, "conname"); |
| 8352 |
i_consrc = PQfnumber(res, "consrc"); |
| 8353 |
i_convalidated = PQfnumber(res, "convalidated"); |
| 8354 |
i_contype = PQfnumber(res, "contype"); |
| 8355 |
|
| 8356 |
constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo)); |
| 8357 |
tyinfo->domChecks = constrinfo; |
| 8358 |
|
| 8359 |
/* 'i' tracks result rows; 'j' counts CHECK constraints */ |
| 8360 |
for (int i = 0, j = 0; i < ntups; i++) |
| 8361 |
{ |
| 8362 |
bool validated = PQgetvalue(res, i, i_convalidated)[0] == 't'; |
| 8363 |
char contype = (PQgetvalue(res, i, i_contype))[0]; |
| 8364 |
ConstraintInfo *constraint; |
| 8365 |
|
| 8366 |
if (contype == CONSTRAINT_CHECK) |
| 8367 |
{ |
| 8368 |
constraint = &constrinfo[j++]; |
| 8369 |
tyinfo->nDomChecks++; |
| 8370 |
} |
| 8371 |
else |
| 8372 |
{ |
| 8373 |
Assert(contype == CONSTRAINT_NOTNULL); |
| 8374 |
Assert(tyinfo->notnull == NULL); |
| 8375 |
/* use last item in array for the not-null constraint */ |
| 8376 |
tyinfo->notnull = &(constrinfo[ntups - 1]); |
| 8377 |
constraint = tyinfo->notnull; |
| 8378 |
} |
| 8379 |
|
| 8380 |
constraint->dobj.objType = DO_CONSTRAINT; |
| 8381 |
constraint->dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 8382 |
constraint->dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 8383 |
AssignDumpId(&(constraint->dobj)); |
| 8384 |
constraint->dobj.name = pg_strdup(PQgetvalue(res, i, i_conname)); |
| 8385 |
constraint->dobj.namespace = tyinfo->dobj.namespace; |
| 8386 |
constraint->contable = NULL; |
| 8387 |
constraint->condomain = tyinfo; |
| 8388 |
constraint->contype = contype; |
| 8389 |
constraint->condef = pg_strdup(PQgetvalue(res, i, i_consrc)); |
| 8390 |
constraint->confrelid = InvalidOid; |
| 8391 |
constraint->conindex = 0; |
| 8392 |
constraint->condeferrable = false; |
| 8393 |
constraint->condeferred = false; |
| 8394 |
constraint->conislocal = true; |
| 8395 |
|
| 8396 |
constraint->separate = !validated; |
| 8397 |
|
| 8398 |
/* |
| 8399 |
* Make the domain depend on the constraint, ensuring it won't be |
| 8400 |
* output till any constraint dependencies are OK. If the constraint |
| 8401 |
* has not been validated, it's going to be dumped after the domain |
| 8402 |
* anyway, so this doesn't matter. |
| 8403 |
*/ |
| 8404 |
if (validated) |
| 8405 |
addObjectDependency(&tyinfo->dobj, constraint->dobj.dumpId); |
| 8406 |
} |
| 8407 |
|
| 8408 |
PQclear(res); |
| 8409 |
|
| 8410 |
destroyPQExpBuffer(query); |
| 8411 |
} |
| 8412 |
|
| 8413 |
/* |
| 8414 |
* getRules |
| 8415 |
* get basic information about every rule in the system |
| 8416 |
*/ |
| 8417 |
void |
| 8418 |
getRules(Archive *fout) |
| 8419 |
{ |
| 8420 |
PGresult *res; |
| 8421 |
int ntups; |
| 8422 |
int i; |
| 8423 |
PQExpBuffer query = createPQExpBuffer(); |
| 8424 |
RuleInfo *ruleinfo; |
| 8425 |
int i_tableoid; |
| 8426 |
int i_oid; |
| 8427 |
int i_rulename; |
| 8428 |
int i_ruletable; |
| 8429 |
int i_ev_type; |
| 8430 |
int i_is_instead; |
| 8431 |
int i_ev_enabled; |
| 8432 |
|
| 8433 |
appendPQExpBufferStr(query, "SELECT " |
| 8434 |
"tableoid, oid, rulename, " |
| 8435 |
"ev_class AS ruletable, ev_type, is_instead, " |
| 8436 |
"ev_enabled " |
| 8437 |
"FROM pg_rewrite " |
| 8438 |
"ORDER BY oid"); |
| 8439 |
|
| 8440 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8441 |
|
| 8442 |
ntups = PQntuples(res); |
| 8443 |
|
| 8444 |
ruleinfo = (RuleInfo *) pg_malloc(ntups * sizeof(RuleInfo)); |
| 8445 |
|
| 8446 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 8447 |
i_oid = PQfnumber(res, "oid"); |
| 8448 |
i_rulename = PQfnumber(res, "rulename"); |
| 8449 |
i_ruletable = PQfnumber(res, "ruletable"); |
| 8450 |
i_ev_type = PQfnumber(res, "ev_type"); |
| 8451 |
i_is_instead = PQfnumber(res, "is_instead"); |
| 8452 |
i_ev_enabled = PQfnumber(res, "ev_enabled"); |
| 8453 |
|
| 8454 |
for (i = 0; i < ntups; i++) |
| 8455 |
{ |
| 8456 |
Oid ruletableoid; |
| 8457 |
|
| 8458 |
ruleinfo[i].dobj.objType = DO_RULE; |
| 8459 |
ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 8460 |
ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 8461 |
AssignDumpId(&ruleinfo[i].dobj); |
| 8462 |
ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename)); |
| 8463 |
ruletableoid = atooid(PQgetvalue(res, i, i_ruletable)); |
| 8464 |
ruleinfo[i].ruletable = findTableByOid(ruletableoid); |
| 8465 |
if (ruleinfo[i].ruletable == NULL) |
| 8466 |
pg_fatal("failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found", |
| 8467 |
ruletableoid, ruleinfo[i].dobj.catId.oid); |
| 8468 |
ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace; |
| 8469 |
ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump; |
| 8470 |
ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type)); |
| 8471 |
ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't'; |
| 8472 |
ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled)); |
| 8473 |
if (ruleinfo[i].ruletable) |
| 8474 |
{ |
| 8475 |
/* |
| 8476 |
* If the table is a view or materialized view, force its ON |
| 8477 |
* SELECT rule to be sorted before the view itself --- this |
| 8478 |
* ensures that any dependencies for the rule affect the table's |
| 8479 |
* positioning. Other rules are forced to appear after their |
| 8480 |
* table. |
| 8481 |
*/ |
| 8482 |
if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW || |
| 8483 |
ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) && |
| 8484 |
ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead) |
| 8485 |
{ |
| 8486 |
addObjectDependency(&ruleinfo[i].ruletable->dobj, |
| 8487 |
ruleinfo[i].dobj.dumpId); |
| 8488 |
/* We'll merge the rule into CREATE VIEW, if possible */ |
| 8489 |
ruleinfo[i].separate = false; |
| 8490 |
} |
| 8491 |
else |
| 8492 |
{ |
| 8493 |
addObjectDependency(&ruleinfo[i].dobj, |
| 8494 |
ruleinfo[i].ruletable->dobj.dumpId); |
| 8495 |
ruleinfo[i].separate = true; |
| 8496 |
} |
| 8497 |
} |
| 8498 |
else |
| 8499 |
ruleinfo[i].separate = true; |
| 8500 |
} |
| 8501 |
|
| 8502 |
PQclear(res); |
| 8503 |
|
| 8504 |
destroyPQExpBuffer(query); |
| 8505 |
} |
| 8506 |
|
| 8507 |
/* |
| 8508 |
* getTriggers |
| 8509 |
* get information about every trigger on a dumpable table |
| 8510 |
* |
| 8511 |
* Note: trigger data is not returned directly to the caller, but it |
| 8512 |
* does get entered into the DumpableObject tables. |
| 8513 |
*/ |
| 8514 |
void |
| 8515 |
getTriggers(Archive *fout, TableInfo tblinfo[], int numTables) |
| 8516 |
{ |
| 8517 |
PQExpBuffer query = createPQExpBuffer(); |
| 8518 |
PQExpBuffer tbloids = createPQExpBuffer(); |
| 8519 |
PGresult *res; |
| 8520 |
int ntups; |
| 8521 |
int curtblindx; |
| 8522 |
TriggerInfo *tginfo; |
| 8523 |
int i_tableoid, |
| 8524 |
i_oid, |
| 8525 |
i_tgrelid, |
| 8526 |
i_tgname, |
| 8527 |
i_tgenabled, |
| 8528 |
i_tgispartition, |
| 8529 |
i_tgdef; |
| 8530 |
|
| 8531 |
/* |
| 8532 |
* We want to perform just one query against pg_trigger. However, we |
| 8533 |
* mustn't try to select every row of the catalog and then sort it out on |
| 8534 |
* the client side, because some of the server-side functions we need |
| 8535 |
* would be unsafe to apply to tables we don't have lock on. Hence, we |
| 8536 |
* build an array of the OIDs of tables we care about (and now have lock |
| 8537 |
* on!), and use a WHERE clause to constrain which rows are selected. |
| 8538 |
*/ |
| 8539 |
appendPQExpBufferChar(tbloids, '{'); |
| 8540 |
for (int i = 0; i < numTables; i++) |
| 8541 |
{ |
| 8542 |
TableInfo *tbinfo = &tblinfo[i]; |
| 8543 |
|
| 8544 |
if (!tbinfo->hastriggers || |
| 8545 |
!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)) |
| 8546 |
continue; |
| 8547 |
|
| 8548 |
/* OK, we need info for this table */ |
| 8549 |
if (tbloids->len > 1) /* do we have more than the '{'? */ |
| 8550 |
appendPQExpBufferChar(tbloids, ','); |
| 8551 |
appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); |
| 8552 |
} |
| 8553 |
appendPQExpBufferChar(tbloids, '}'); |
| 8554 |
|
| 8555 |
if (fout->remoteVersion >= 150000) |
| 8556 |
{ |
| 8557 |
/* |
| 8558 |
* NB: think not to use pretty=true in pg_get_triggerdef. It could |
| 8559 |
* result in non-forward-compatible dumps of WHEN clauses due to |
| 8560 |
* under-parenthesization. |
| 8561 |
* |
| 8562 |
* NB: We need to see partition triggers in case the tgenabled flag |
| 8563 |
* has been changed from the parent. |
| 8564 |
*/ |
| 8565 |
appendPQExpBuffer(query, |
| 8566 |
"SELECT t.tgrelid, t.tgname, " |
| 8567 |
"pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " |
| 8568 |
"t.tgenabled, t.tableoid, t.oid, " |
| 8569 |
"t.tgparentid <> 0 AS tgispartition\n" |
| 8570 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 8571 |
"JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " |
| 8572 |
"LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) " |
| 8573 |
"WHERE ((NOT t.tgisinternal AND t.tgparentid = 0) " |
| 8574 |
"OR t.tgenabled != u.tgenabled) " |
| 8575 |
"ORDER BY t.tgrelid, t.tgname", |
| 8576 |
tbloids->data); |
| 8577 |
} |
| 8578 |
else if (fout->remoteVersion >= 130000) |
| 8579 |
{ |
| 8580 |
/* |
| 8581 |
* NB: think not to use pretty=true in pg_get_triggerdef. It could |
| 8582 |
* result in non-forward-compatible dumps of WHEN clauses due to |
| 8583 |
* under-parenthesization. |
| 8584 |
* |
| 8585 |
* NB: We need to see tgisinternal triggers in partitions, in case the |
| 8586 |
* tgenabled flag has been changed from the parent. |
| 8587 |
*/ |
| 8588 |
appendPQExpBuffer(query, |
| 8589 |
"SELECT t.tgrelid, t.tgname, " |
| 8590 |
"pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " |
| 8591 |
"t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition\n" |
| 8592 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 8593 |
"JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " |
| 8594 |
"LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) " |
| 8595 |
"WHERE (NOT t.tgisinternal OR t.tgenabled != u.tgenabled) " |
| 8596 |
"ORDER BY t.tgrelid, t.tgname", |
| 8597 |
tbloids->data); |
| 8598 |
} |
| 8599 |
else if (fout->remoteVersion >= 110000) |
| 8600 |
{ |
| 8601 |
/* |
| 8602 |
* NB: We need to see tgisinternal triggers in partitions, in case the |
| 8603 |
* tgenabled flag has been changed from the parent. No tgparentid in |
| 8604 |
* version 11-12, so we have to match them via pg_depend. |
| 8605 |
* |
| 8606 |
* See above about pretty=true in pg_get_triggerdef. |
| 8607 |
*/ |
| 8608 |
appendPQExpBuffer(query, |
| 8609 |
"SELECT t.tgrelid, t.tgname, " |
| 8610 |
"pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " |
| 8611 |
"t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition " |
| 8612 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 8613 |
"JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " |
| 8614 |
"LEFT JOIN pg_catalog.pg_depend AS d ON " |
| 8615 |
" d.classid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND " |
| 8616 |
" d.refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND " |
| 8617 |
" d.objid = t.oid " |
| 8618 |
"LEFT JOIN pg_catalog.pg_trigger AS pt ON pt.oid = refobjid " |
| 8619 |
"WHERE (NOT t.tgisinternal OR t.tgenabled != pt.tgenabled) " |
| 8620 |
"ORDER BY t.tgrelid, t.tgname", |
| 8621 |
tbloids->data); |
| 8622 |
} |
| 8623 |
else |
| 8624 |
{ |
| 8625 |
/* See above about pretty=true in pg_get_triggerdef */ |
| 8626 |
appendPQExpBuffer(query, |
| 8627 |
"SELECT t.tgrelid, t.tgname, " |
| 8628 |
"pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " |
| 8629 |
"t.tgenabled, false as tgispartition, " |
| 8630 |
"t.tableoid, t.oid " |
| 8631 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 8632 |
"JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " |
| 8633 |
"WHERE NOT tgisinternal " |
| 8634 |
"ORDER BY t.tgrelid, t.tgname", |
| 8635 |
tbloids->data); |
| 8636 |
} |
| 8637 |
|
| 8638 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8639 |
|
| 8640 |
ntups = PQntuples(res); |
| 8641 |
|
| 8642 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 8643 |
i_oid = PQfnumber(res, "oid"); |
| 8644 |
i_tgrelid = PQfnumber(res, "tgrelid"); |
| 8645 |
i_tgname = PQfnumber(res, "tgname"); |
| 8646 |
i_tgenabled = PQfnumber(res, "tgenabled"); |
| 8647 |
i_tgispartition = PQfnumber(res, "tgispartition"); |
| 8648 |
i_tgdef = PQfnumber(res, "tgdef"); |
| 8649 |
|
| 8650 |
tginfo = (TriggerInfo *) pg_malloc(ntups * sizeof(TriggerInfo)); |
| 8651 |
|
| 8652 |
/* |
| 8653 |
* Outer loop iterates once per table, not once per row. Incrementing of |
| 8654 |
* j is handled by the inner loop. |
| 8655 |
*/ |
| 8656 |
curtblindx = -1; |
| 8657 |
for (int j = 0; j < ntups;) |
| 8658 |
{ |
| 8659 |
Oid tgrelid = atooid(PQgetvalue(res, j, i_tgrelid)); |
| 8660 |
TableInfo *tbinfo = NULL; |
| 8661 |
int numtrigs; |
| 8662 |
|
| 8663 |
/* Count rows for this table */ |
| 8664 |
for (numtrigs = 1; numtrigs < ntups - j; numtrigs++) |
| 8665 |
if (atooid(PQgetvalue(res, j + numtrigs, i_tgrelid)) != tgrelid) |
| 8666 |
break; |
| 8667 |
|
| 8668 |
/* |
| 8669 |
* Locate the associated TableInfo; we rely on tblinfo[] being in OID |
| 8670 |
* order. |
| 8671 |
*/ |
| 8672 |
while (++curtblindx < numTables) |
| 8673 |
{ |
| 8674 |
tbinfo = &tblinfo[curtblindx]; |
| 8675 |
if (tbinfo->dobj.catId.oid == tgrelid) |
| 8676 |
break; |
| 8677 |
} |
| 8678 |
if (curtblindx >= numTables) |
| 8679 |
pg_fatal("unrecognized table OID %u", tgrelid); |
| 8680 |
|
| 8681 |
/* Save data for this table */ |
| 8682 |
tbinfo->triggers = tginfo + j; |
| 8683 |
tbinfo->numTriggers = numtrigs; |
| 8684 |
|
| 8685 |
for (int c = 0; c < numtrigs; c++, j++) |
| 8686 |
{ |
| 8687 |
tginfo[j].dobj.objType = DO_TRIGGER; |
| 8688 |
tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); |
| 8689 |
tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); |
| 8690 |
AssignDumpId(&tginfo[j].dobj); |
| 8691 |
tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname)); |
| 8692 |
tginfo[j].dobj.namespace = tbinfo->dobj.namespace; |
| 8693 |
tginfo[j].tgtable = tbinfo; |
| 8694 |
tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled)); |
| 8695 |
tginfo[j].tgispartition = *(PQgetvalue(res, j, i_tgispartition)) == 't'; |
| 8696 |
tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef)); |
| 8697 |
} |
| 8698 |
} |
| 8699 |
|
| 8700 |
PQclear(res); |
| 8701 |
|
| 8702 |
destroyPQExpBuffer(query); |
| 8703 |
destroyPQExpBuffer(tbloids); |
| 8704 |
} |
| 8705 |
|
| 8706 |
/* |
| 8707 |
* getEventTriggers |
| 8708 |
* get information about event triggers |
| 8709 |
*/ |
| 8710 |
void |
| 8711 |
getEventTriggers(Archive *fout) |
| 8712 |
{ |
| 8713 |
int i; |
| 8714 |
PQExpBuffer query; |
| 8715 |
PGresult *res; |
| 8716 |
EventTriggerInfo *evtinfo; |
| 8717 |
int i_tableoid, |
| 8718 |
i_oid, |
| 8719 |
i_evtname, |
| 8720 |
i_evtevent, |
| 8721 |
i_evtowner, |
| 8722 |
i_evttags, |
| 8723 |
i_evtfname, |
| 8724 |
i_evtenabled; |
| 8725 |
int ntups; |
| 8726 |
|
| 8727 |
/* Before 9.3, there are no event triggers */ |
| 8728 |
if (fout->remoteVersion < 90300) |
| 8729 |
return; |
| 8730 |
|
| 8731 |
query = createPQExpBuffer(); |
| 8732 |
|
| 8733 |
appendPQExpBufferStr(query, |
| 8734 |
"SELECT e.tableoid, e.oid, evtname, evtenabled, " |
| 8735 |
"evtevent, evtowner, " |
| 8736 |
"array_to_string(array(" |
| 8737 |
"select quote_literal(x) " |
| 8738 |
" from unnest(evttags) as t(x)), ', ') as evttags, " |
| 8739 |
"e.evtfoid::regproc as evtfname " |
| 8740 |
"FROM pg_event_trigger e " |
| 8741 |
"ORDER BY e.oid"); |
| 8742 |
|
| 8743 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8744 |
|
| 8745 |
ntups = PQntuples(res); |
| 8746 |
|
| 8747 |
evtinfo = (EventTriggerInfo *) pg_malloc(ntups * sizeof(EventTriggerInfo)); |
| 8748 |
|
| 8749 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 8750 |
i_oid = PQfnumber(res, "oid"); |
| 8751 |
i_evtname = PQfnumber(res, "evtname"); |
| 8752 |
i_evtevent = PQfnumber(res, "evtevent"); |
| 8753 |
i_evtowner = PQfnumber(res, "evtowner"); |
| 8754 |
i_evttags = PQfnumber(res, "evttags"); |
| 8755 |
i_evtfname = PQfnumber(res, "evtfname"); |
| 8756 |
i_evtenabled = PQfnumber(res, "evtenabled"); |
| 8757 |
|
| 8758 |
for (i = 0; i < ntups; i++) |
| 8759 |
{ |
| 8760 |
evtinfo[i].dobj.objType = DO_EVENT_TRIGGER; |
| 8761 |
evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 8762 |
evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 8763 |
AssignDumpId(&evtinfo[i].dobj); |
| 8764 |
evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname)); |
| 8765 |
evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname)); |
| 8766 |
evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent)); |
| 8767 |
evtinfo[i].evtowner = getRoleName(PQgetvalue(res, i, i_evtowner)); |
| 8768 |
evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags)); |
| 8769 |
evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname)); |
| 8770 |
evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled)); |
| 8771 |
|
| 8772 |
/* Decide whether we want to dump it */ |
| 8773 |
selectDumpableObject(&(evtinfo[i].dobj), fout); |
| 8774 |
} |
| 8775 |
|
| 8776 |
PQclear(res); |
| 8777 |
|
| 8778 |
destroyPQExpBuffer(query); |
| 8779 |
} |
| 8780 |
|
| 8781 |
/* |
| 8782 |
* getProcLangs |
| 8783 |
* get basic information about every procedural language in the system |
| 8784 |
* |
| 8785 |
* NB: this must run after getFuncs() because we assume we can do |
| 8786 |
* findFuncByOid(). |
| 8787 |
*/ |
| 8788 |
void |
| 8789 |
getProcLangs(Archive *fout) |
| 8790 |
{ |
| 8791 |
PGresult *res; |
| 8792 |
int ntups; |
| 8793 |
int i; |
| 8794 |
PQExpBuffer query = createPQExpBuffer(); |
| 8795 |
ProcLangInfo *planginfo; |
| 8796 |
int i_tableoid; |
| 8797 |
int i_oid; |
| 8798 |
int i_lanname; |
| 8799 |
int i_lanpltrusted; |
| 8800 |
int i_lanplcallfoid; |
| 8801 |
int i_laninline; |
| 8802 |
int i_lanvalidator; |
| 8803 |
int i_lanacl; |
| 8804 |
int i_acldefault; |
| 8805 |
int i_lanowner; |
| 8806 |
|
| 8807 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, " |
| 8808 |
"lanname, lanpltrusted, lanplcallfoid, " |
| 8809 |
"laninline, lanvalidator, " |
| 8810 |
"lanacl, " |
| 8811 |
"acldefault('l', lanowner) AS acldefault, " |
| 8812 |
"lanowner " |
| 8813 |
"FROM pg_language " |
| 8814 |
"WHERE lanispl " |
| 8815 |
"ORDER BY oid"); |
| 8816 |
|
| 8817 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8818 |
|
| 8819 |
ntups = PQntuples(res); |
| 8820 |
|
| 8821 |
planginfo = (ProcLangInfo *) pg_malloc(ntups * sizeof(ProcLangInfo)); |
| 8822 |
|
| 8823 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 8824 |
i_oid = PQfnumber(res, "oid"); |
| 8825 |
i_lanname = PQfnumber(res, "lanname"); |
| 8826 |
i_lanpltrusted = PQfnumber(res, "lanpltrusted"); |
| 8827 |
i_lanplcallfoid = PQfnumber(res, "lanplcallfoid"); |
| 8828 |
i_laninline = PQfnumber(res, "laninline"); |
| 8829 |
i_lanvalidator = PQfnumber(res, "lanvalidator"); |
| 8830 |
i_lanacl = PQfnumber(res, "lanacl"); |
| 8831 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 8832 |
i_lanowner = PQfnumber(res, "lanowner"); |
| 8833 |
|
| 8834 |
for (i = 0; i < ntups; i++) |
| 8835 |
{ |
| 8836 |
planginfo[i].dobj.objType = DO_PROCLANG; |
| 8837 |
planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 8838 |
planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 8839 |
AssignDumpId(&planginfo[i].dobj); |
| 8840 |
|
| 8841 |
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname)); |
| 8842 |
planginfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_lanacl)); |
| 8843 |
planginfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 8844 |
planginfo[i].dacl.privtype = 0; |
| 8845 |
planginfo[i].dacl.initprivs = NULL; |
| 8846 |
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't'; |
| 8847 |
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid)); |
| 8848 |
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline)); |
| 8849 |
planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator)); |
| 8850 |
planginfo[i].lanowner = getRoleName(PQgetvalue(res, i, i_lanowner)); |
| 8851 |
|
| 8852 |
/* Decide whether we want to dump it */ |
| 8853 |
selectDumpableProcLang(&(planginfo[i]), fout); |
| 8854 |
|
| 8855 |
/* Mark whether language has an ACL */ |
| 8856 |
if (!PQgetisnull(res, i, i_lanacl)) |
| 8857 |
planginfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 8858 |
} |
| 8859 |
|
| 8860 |
PQclear(res); |
| 8861 |
|
| 8862 |
destroyPQExpBuffer(query); |
| 8863 |
} |
| 8864 |
|
| 8865 |
/* |
| 8866 |
* getCasts |
| 8867 |
* get basic information about most casts in the system |
| 8868 |
* |
| 8869 |
* Skip casts from a range to its multirange, since we'll create those |
| 8870 |
* automatically. |
| 8871 |
*/ |
| 8872 |
void |
| 8873 |
getCasts(Archive *fout) |
| 8874 |
{ |
| 8875 |
PGresult *res; |
| 8876 |
int ntups; |
| 8877 |
int i; |
| 8878 |
PQExpBuffer query = createPQExpBuffer(); |
| 8879 |
CastInfo *castinfo; |
| 8880 |
int i_tableoid; |
| 8881 |
int i_oid; |
| 8882 |
int i_castsource; |
| 8883 |
int i_casttarget; |
| 8884 |
int i_castfunc; |
| 8885 |
int i_castcontext; |
| 8886 |
int i_castmethod; |
| 8887 |
|
| 8888 |
if (fout->remoteVersion >= 140000) |
| 8889 |
{ |
| 8890 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, " |
| 8891 |
"castsource, casttarget, castfunc, castcontext, " |
| 8892 |
"castmethod " |
| 8893 |
"FROM pg_cast c " |
| 8894 |
"WHERE NOT EXISTS ( " |
| 8895 |
"SELECT 1 FROM pg_range r " |
| 8896 |
"WHERE c.castsource = r.rngtypid " |
| 8897 |
"AND c.casttarget = r.rngmultitypid " |
| 8898 |
") " |
| 8899 |
"ORDER BY 3,4"); |
| 8900 |
} |
| 8901 |
else |
| 8902 |
{ |
| 8903 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, " |
| 8904 |
"castsource, casttarget, castfunc, castcontext, " |
| 8905 |
"castmethod " |
| 8906 |
"FROM pg_cast ORDER BY 3,4"); |
| 8907 |
} |
| 8908 |
|
| 8909 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 8910 |
|
| 8911 |
ntups = PQntuples(res); |
| 8912 |
|
| 8913 |
castinfo = (CastInfo *) pg_malloc(ntups * sizeof(CastInfo)); |
| 8914 |
|
| 8915 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 8916 |
i_oid = PQfnumber(res, "oid"); |
| 8917 |
i_castsource = PQfnumber(res, "castsource"); |
| 8918 |
i_casttarget = PQfnumber(res, "casttarget"); |
| 8919 |
i_castfunc = PQfnumber(res, "castfunc"); |
| 8920 |
i_castcontext = PQfnumber(res, "castcontext"); |
| 8921 |
i_castmethod = PQfnumber(res, "castmethod"); |
| 8922 |
|
| 8923 |
for (i = 0; i < ntups; i++) |
| 8924 |
{ |
| 8925 |
PQExpBufferData namebuf; |
| 8926 |
TypeInfo *sTypeInfo; |
| 8927 |
TypeInfo *tTypeInfo; |
| 8928 |
|
| 8929 |
castinfo[i].dobj.objType = DO_CAST; |
| 8930 |
castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 8931 |
castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 8932 |
AssignDumpId(&castinfo[i].dobj); |
| 8933 |
castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource)); |
| 8934 |
castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget)); |
| 8935 |
castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc)); |
| 8936 |
castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext)); |
| 8937 |
castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod)); |
| 8938 |
|
| 8939 |
/* |
| 8940 |
* Try to name cast as concatenation of typnames. This is only used |
| 8941 |
* for purposes of sorting. If we fail to find either type, the name |
| 8942 |
* will be an empty string. |
| 8943 |
*/ |
| 8944 |
initPQExpBuffer(&namebuf); |
| 8945 |
sTypeInfo = findTypeByOid(castinfo[i].castsource); |
| 8946 |
tTypeInfo = findTypeByOid(castinfo[i].casttarget); |
| 8947 |
if (sTypeInfo && tTypeInfo) |
| 8948 |
appendPQExpBuffer(&namebuf, "%s %s", |
| 8949 |
sTypeInfo->dobj.name, tTypeInfo->dobj.name); |
| 8950 |
castinfo[i].dobj.name = namebuf.data; |
| 8951 |
|
| 8952 |
/* Decide whether we want to dump it */ |
| 8953 |
selectDumpableCast(&(castinfo[i]), fout); |
| 8954 |
} |
| 8955 |
|
| 8956 |
PQclear(res); |
| 8957 |
|
| 8958 |
destroyPQExpBuffer(query); |
| 8959 |
} |
| 8960 |
|
| 8961 |
static char * |
| 8962 |
get_language_name(Archive *fout, Oid langid) |
| 8963 |
{ |
| 8964 |
PQExpBuffer query; |
| 8965 |
PGresult *res; |
| 8966 |
char *lanname; |
| 8967 |
|
| 8968 |
query = createPQExpBuffer(); |
| 8969 |
appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid); |
| 8970 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 8971 |
lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0))); |
| 8972 |
destroyPQExpBuffer(query); |
| 8973 |
PQclear(res); |
| 8974 |
|
| 8975 |
return lanname; |
| 8976 |
} |
| 8977 |
|
| 8978 |
/* |
| 8979 |
* getTransforms |
| 8980 |
* get basic information about every transform in the system |
| 8981 |
*/ |
| 8982 |
void |
| 8983 |
getTransforms(Archive *fout) |
| 8984 |
{ |
| 8985 |
PGresult *res; |
| 8986 |
int ntups; |
| 8987 |
int i; |
| 8988 |
PQExpBuffer query; |
| 8989 |
TransformInfo *transforminfo; |
| 8990 |
int i_tableoid; |
| 8991 |
int i_oid; |
| 8992 |
int i_trftype; |
| 8993 |
int i_trflang; |
| 8994 |
int i_trffromsql; |
| 8995 |
int i_trftosql; |
| 8996 |
|
| 8997 |
/* Transforms didn't exist pre-9.5 */ |
| 8998 |
if (fout->remoteVersion < 90500) |
| 8999 |
return; |
| 9000 |
|
| 9001 |
query = createPQExpBuffer(); |
| 9002 |
|
| 9003 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, " |
| 9004 |
"trftype, trflang, trffromsql::oid, trftosql::oid " |
| 9005 |
"FROM pg_transform " |
| 9006 |
"ORDER BY 3,4"); |
| 9007 |
|
| 9008 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 9009 |
|
| 9010 |
ntups = PQntuples(res); |
| 9011 |
|
| 9012 |
transforminfo = (TransformInfo *) pg_malloc(ntups * sizeof(TransformInfo)); |
| 9013 |
|
| 9014 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 9015 |
i_oid = PQfnumber(res, "oid"); |
| 9016 |
i_trftype = PQfnumber(res, "trftype"); |
| 9017 |
i_trflang = PQfnumber(res, "trflang"); |
| 9018 |
i_trffromsql = PQfnumber(res, "trffromsql"); |
| 9019 |
i_trftosql = PQfnumber(res, "trftosql"); |
| 9020 |
|
| 9021 |
for (i = 0; i < ntups; i++) |
| 9022 |
{ |
| 9023 |
PQExpBufferData namebuf; |
| 9024 |
TypeInfo *typeInfo; |
| 9025 |
char *lanname; |
| 9026 |
|
| 9027 |
transforminfo[i].dobj.objType = DO_TRANSFORM; |
| 9028 |
transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 9029 |
transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 9030 |
AssignDumpId(&transforminfo[i].dobj); |
| 9031 |
transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype)); |
| 9032 |
transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang)); |
| 9033 |
transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql)); |
| 9034 |
transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql)); |
| 9035 |
|
| 9036 |
/* |
| 9037 |
* Try to name transform as concatenation of type and language name. |
| 9038 |
* This is only used for purposes of sorting. If we fail to find |
| 9039 |
* either, the name will be an empty string. |
| 9040 |
*/ |
| 9041 |
initPQExpBuffer(&namebuf); |
| 9042 |
typeInfo = findTypeByOid(transforminfo[i].trftype); |
| 9043 |
lanname = get_language_name(fout, transforminfo[i].trflang); |
| 9044 |
if (typeInfo && lanname) |
| 9045 |
appendPQExpBuffer(&namebuf, "%s %s", |
| 9046 |
typeInfo->dobj.name, lanname); |
| 9047 |
transforminfo[i].dobj.name = namebuf.data; |
| 9048 |
free(lanname); |
| 9049 |
|
| 9050 |
/* Decide whether we want to dump it */ |
| 9051 |
selectDumpableObject(&(transforminfo[i].dobj), fout); |
| 9052 |
} |
| 9053 |
|
| 9054 |
PQclear(res); |
| 9055 |
|
| 9056 |
destroyPQExpBuffer(query); |
| 9057 |
} |
| 9058 |
|
| 9059 |
/* |
| 9060 |
* getTableAttrs - |
| 9061 |
* for each interesting table, read info about its attributes |
| 9062 |
* (names, types, default values, CHECK constraints, etc) |
| 9063 |
* |
| 9064 |
* modifies tblinfo |
| 9065 |
*/ |
| 9066 |
void |
| 9067 |
getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) |
| 9068 |
{ |
| 9069 |
DumpOptions *dopt = fout->dopt; |
| 9070 |
PQExpBuffer q = createPQExpBuffer(); |
| 9071 |
PQExpBuffer tbloids = createPQExpBuffer(); |
| 9072 |
PQExpBuffer checkoids = createPQExpBuffer(); |
| 9073 |
PQExpBuffer invalidnotnulloids = NULL; |
| 9074 |
PGresult *res; |
| 9075 |
int ntups; |
| 9076 |
int curtblindx; |
| 9077 |
int i_attrelid; |
| 9078 |
int i_attnum; |
| 9079 |
int i_attname; |
| 9080 |
int i_atttypname; |
| 9081 |
int i_attstattarget; |
| 9082 |
int i_attstorage; |
| 9083 |
int i_typstorage; |
| 9084 |
int i_attidentity; |
| 9085 |
int i_attgenerated; |
| 9086 |
int i_attisdropped; |
| 9087 |
int i_attlen; |
| 9088 |
int i_attalign; |
| 9089 |
int i_attislocal; |
| 9090 |
int i_notnull_name; |
| 9091 |
int i_notnull_comment; |
| 9092 |
int i_notnull_noinherit; |
| 9093 |
int i_notnull_islocal; |
| 9094 |
int i_notnull_invalidoid; |
| 9095 |
int i_attoptions; |
| 9096 |
int i_attcollation; |
| 9097 |
int i_attcompression; |
| 9098 |
int i_attfdwoptions; |
| 9099 |
int i_attmissingval; |
| 9100 |
int i_atthasdef; |
| 9101 |
|
| 9102 |
/* |
| 9103 |
* We want to perform just one query against pg_attribute, and then just |
| 9104 |
* one against pg_attrdef (for DEFAULTs) and two against pg_constraint |
| 9105 |
* (for CHECK constraints and for NOT NULL constraints). However, we |
| 9106 |
* mustn't try to select every row of those catalogs and then sort it out |
| 9107 |
* on the client side, because some of the server-side functions we need |
| 9108 |
* would be unsafe to apply to tables we don't have lock on. Hence, we |
| 9109 |
* build an array of the OIDs of tables we care about (and now have lock |
| 9110 |
* on!), and use a WHERE clause to constrain which rows are selected. |
| 9111 |
*/ |
| 9112 |
appendPQExpBufferChar(tbloids, '{'); |
| 9113 |
appendPQExpBufferChar(checkoids, '{'); |
| 9114 |
for (int i = 0; i < numTables; i++) |
| 9115 |
{ |
| 9116 |
TableInfo *tbinfo = &tblinfo[i]; |
| 9117 |
|
| 9118 |
/* Don't bother to collect info for sequences */ |
| 9119 |
if (tbinfo->relkind == RELKIND_SEQUENCE) |
| 9120 |
continue; |
| 9121 |
|
| 9122 |
/* Don't bother with uninteresting tables, either */ |
| 9123 |
if (!tbinfo->interesting) |
| 9124 |
continue; |
| 9125 |
|
| 9126 |
/* OK, we need info for this table */ |
| 9127 |
if (tbloids->len > 1) /* do we have more than the '{'? */ |
| 9128 |
appendPQExpBufferChar(tbloids, ','); |
| 9129 |
appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); |
| 9130 |
|
| 9131 |
if (tbinfo->ncheck > 0) |
| 9132 |
{ |
| 9133 |
/* Also make a list of the ones with check constraints */ |
| 9134 |
if (checkoids->len > 1) /* do we have more than the '{'? */ |
| 9135 |
appendPQExpBufferChar(checkoids, ','); |
| 9136 |
appendPQExpBuffer(checkoids, "%u", tbinfo->dobj.catId.oid); |
| 9137 |
} |
| 9138 |
} |
| 9139 |
appendPQExpBufferChar(tbloids, '}'); |
| 9140 |
appendPQExpBufferChar(checkoids, '}'); |
| 9141 |
|
| 9142 |
/* |
| 9143 |
* Find all the user attributes and their types. |
| 9144 |
* |
| 9145 |
* Since we only want to dump COLLATE clauses for attributes whose |
| 9146 |
* collation is different from their type's default, we use a CASE here to |
| 9147 |
* suppress uninteresting attcollations cheaply. |
| 9148 |
*/ |
| 9149 |
appendPQExpBufferStr(q, |
| 9150 |
"SELECT\n" |
| 9151 |
"a.attrelid,\n" |
| 9152 |
"a.attnum,\n" |
| 9153 |
"a.attname,\n" |
| 9154 |
"a.attstattarget,\n" |
| 9155 |
"a.attstorage,\n" |
| 9156 |
"t.typstorage,\n" |
| 9157 |
"a.atthasdef,\n" |
| 9158 |
"a.attisdropped,\n" |
| 9159 |
"a.attlen,\n" |
| 9160 |
"a.attalign,\n" |
| 9161 |
"a.attislocal,\n" |
| 9162 |
"pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n" |
| 9163 |
"array_to_string(a.attoptions, ', ') AS attoptions,\n" |
| 9164 |
"CASE WHEN a.attcollation <> t.typcollation " |
| 9165 |
"THEN a.attcollation ELSE 0 END AS attcollation,\n" |
| 9166 |
"pg_catalog.array_to_string(ARRAY(" |
| 9167 |
"SELECT pg_catalog.quote_ident(option_name) || " |
| 9168 |
"' ' || pg_catalog.quote_literal(option_value) " |
| 9169 |
"FROM pg_catalog.pg_options_to_table(attfdwoptions) " |
| 9170 |
"ORDER BY option_name" |
| 9171 |
"), E',\n ') AS attfdwoptions,\n"); |
| 9172 |
|
| 9173 |
/* |
| 9174 |
* Find out any NOT NULL markings for each column. In 18 and up we read |
| 9175 |
* pg_constraint to obtain the constraint name, and for valid constraints |
| 9176 |
* also pg_description to obtain its comment. notnull_noinherit is set |
| 9177 |
* according to the NO INHERIT property. For versions prior to 18, we |
| 9178 |
* store an empty string as the name when a constraint is marked as |
| 9179 |
* attnotnull (this cues dumpTableSchema to print the NOT NULL clause |
| 9180 |
* without a name); also, such cases are never NO INHERIT. |
| 9181 |
* |
| 9182 |
* For invalid constraints, we need to store their OIDs for processing |
| 9183 |
* elsewhere, so we bring the pg_constraint.oid value when the constraint |
| 9184 |
* is invalid, and NULL otherwise. Their comments are handled not here |
| 9185 |
* but by collectComments, because they're their own dumpable object. |
| 9186 |
* |
| 9187 |
* We track in notnull_islocal whether the constraint was defined directly |
| 9188 |
* in this table or via an ancestor, for binary upgrade. flagInhAttrs |
| 9189 |
* might modify this later. |
| 9190 |
*/ |
| 9191 |
if (fout->remoteVersion >= 180000) |
| 9192 |
appendPQExpBufferStr(q, |
| 9193 |
"co.conname AS notnull_name,\n" |
| 9194 |
"CASE WHEN co.convalidated THEN pt.description" |
| 9195 |
" ELSE NULL END AS notnull_comment,\n" |
| 9196 |
"CASE WHEN NOT co.convalidated THEN co.oid " |
| 9197 |
"ELSE NULL END AS notnull_invalidoid,\n" |
| 9198 |
"co.connoinherit AS notnull_noinherit,\n" |
| 9199 |
"co.conislocal AS notnull_islocal,\n"); |
| 9200 |
else |
| 9201 |
appendPQExpBufferStr(q, |
| 9202 |
"CASE WHEN a.attnotnull THEN '' ELSE NULL END AS notnull_name,\n" |
| 9203 |
"NULL AS notnull_comment,\n" |
| 9204 |
"NULL AS notnull_invalidoid,\n" |
| 9205 |
"false AS notnull_noinherit,\n" |
| 9206 |
"CASE WHEN a.attislocal THEN true\n" |
| 9207 |
" WHEN a.attnotnull AND NOT a.attislocal THEN true\n" |
| 9208 |
" ELSE false\n" |
| 9209 |
"END AS notnull_islocal,\n"); |
| 9210 |
|
| 9211 |
if (fout->remoteVersion >= 140000) |
| 9212 |
appendPQExpBufferStr(q, |
| 9213 |
"a.attcompression AS attcompression,\n"); |
| 9214 |
else |
| 9215 |
appendPQExpBufferStr(q, |
| 9216 |
"'' AS attcompression,\n"); |
| 9217 |
|
| 9218 |
if (fout->remoteVersion >= 100000) |
| 9219 |
appendPQExpBufferStr(q, |
| 9220 |
"a.attidentity,\n"); |
| 9221 |
else |
| 9222 |
appendPQExpBufferStr(q, |
| 9223 |
"'' AS attidentity,\n"); |
| 9224 |
|
| 9225 |
if (fout->remoteVersion >= 110000) |
| 9226 |
appendPQExpBufferStr(q, |
| 9227 |
"CASE WHEN a.atthasmissing AND NOT a.attisdropped " |
| 9228 |
"THEN a.attmissingval ELSE null END AS attmissingval,\n"); |
| 9229 |
else |
| 9230 |
appendPQExpBufferStr(q, |
| 9231 |
"NULL AS attmissingval,\n"); |
| 9232 |
|
| 9233 |
if (fout->remoteVersion >= 120000) |
| 9234 |
appendPQExpBufferStr(q, |
| 9235 |
"a.attgenerated\n"); |
| 9236 |
else |
| 9237 |
appendPQExpBufferStr(q, |
| 9238 |
"'' AS attgenerated\n"); |
| 9239 |
|
| 9240 |
/* need left join to pg_type to not fail on dropped columns ... */ |
| 9241 |
appendPQExpBuffer(q, |
| 9242 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 9243 |
"JOIN pg_catalog.pg_attribute a ON (src.tbloid = a.attrelid) " |
| 9244 |
"LEFT JOIN pg_catalog.pg_type t " |
| 9245 |
"ON (a.atttypid = t.oid)\n", |
| 9246 |
tbloids->data); |
| 9247 |
|
| 9248 |
/* |
| 9249 |
* In versions 18 and up, we need pg_constraint for explicit NOT NULL |
| 9250 |
* entries and pg_description to get their comments. |
| 9251 |
*/ |
| 9252 |
if (fout->remoteVersion >= 180000) |
| 9253 |
appendPQExpBufferStr(q, |
| 9254 |
" LEFT JOIN pg_catalog.pg_constraint co ON " |
| 9255 |
"(a.attrelid = co.conrelid\n" |
| 9256 |
" AND co.contype = 'n' AND " |
| 9257 |
"co.conkey = array[a.attnum])\n" |
| 9258 |
" LEFT JOIN pg_catalog.pg_description pt ON " |
| 9259 |
"(pt.classoid = co.tableoid AND pt.objoid = co.oid)\n"); |
| 9260 |
|
| 9261 |
appendPQExpBufferStr(q, |
| 9262 |
"WHERE a.attnum > 0::pg_catalog.int2\n" |
| 9263 |
"ORDER BY a.attrelid, a.attnum"); |
| 9264 |
|
| 9265 |
res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); |
| 9266 |
|
| 9267 |
ntups = PQntuples(res); |
| 9268 |
|
| 9269 |
i_attrelid = PQfnumber(res, "attrelid"); |
| 9270 |
i_attnum = PQfnumber(res, "attnum"); |
| 9271 |
i_attname = PQfnumber(res, "attname"); |
| 9272 |
i_atttypname = PQfnumber(res, "atttypname"); |
| 9273 |
i_attstattarget = PQfnumber(res, "attstattarget"); |
| 9274 |
i_attstorage = PQfnumber(res, "attstorage"); |
| 9275 |
i_typstorage = PQfnumber(res, "typstorage"); |
| 9276 |
i_attidentity = PQfnumber(res, "attidentity"); |
| 9277 |
i_attgenerated = PQfnumber(res, "attgenerated"); |
| 9278 |
i_attisdropped = PQfnumber(res, "attisdropped"); |
| 9279 |
i_attlen = PQfnumber(res, "attlen"); |
| 9280 |
i_attalign = PQfnumber(res, "attalign"); |
| 9281 |
i_attislocal = PQfnumber(res, "attislocal"); |
| 9282 |
i_notnull_name = PQfnumber(res, "notnull_name"); |
| 9283 |
i_notnull_comment = PQfnumber(res, "notnull_comment"); |
| 9284 |
i_notnull_invalidoid = PQfnumber(res, "notnull_invalidoid"); |
| 9285 |
i_notnull_noinherit = PQfnumber(res, "notnull_noinherit"); |
| 9286 |
i_notnull_islocal = PQfnumber(res, "notnull_islocal"); |
| 9287 |
i_attoptions = PQfnumber(res, "attoptions"); |
| 9288 |
i_attcollation = PQfnumber(res, "attcollation"); |
| 9289 |
i_attcompression = PQfnumber(res, "attcompression"); |
| 9290 |
i_attfdwoptions = PQfnumber(res, "attfdwoptions"); |
| 9291 |
i_attmissingval = PQfnumber(res, "attmissingval"); |
| 9292 |
i_atthasdef = PQfnumber(res, "atthasdef"); |
| 9293 |
|
| 9294 |
/* Within the next loop, we'll accumulate OIDs of tables with defaults */ |
| 9295 |
resetPQExpBuffer(tbloids); |
| 9296 |
appendPQExpBufferChar(tbloids, '{'); |
| 9297 |
|
| 9298 |
/* |
| 9299 |
* Outer loop iterates once per table, not once per row. Incrementing of |
| 9300 |
* r is handled by the inner loop. |
| 9301 |
*/ |
| 9302 |
curtblindx = -1; |
| 9303 |
for (int r = 0; r < ntups;) |
| 9304 |
{ |
| 9305 |
Oid attrelid = atooid(PQgetvalue(res, r, i_attrelid)); |
| 9306 |
TableInfo *tbinfo = NULL; |
| 9307 |
int numatts; |
| 9308 |
bool hasdefaults; |
| 9309 |
|
| 9310 |
/* Count rows for this table */ |
| 9311 |
for (numatts = 1; numatts < ntups - r; numatts++) |
| 9312 |
if (atooid(PQgetvalue(res, r + numatts, i_attrelid)) != attrelid) |
| 9313 |
break; |
| 9314 |
|
| 9315 |
/* |
| 9316 |
* Locate the associated TableInfo; we rely on tblinfo[] being in OID |
| 9317 |
* order. |
| 9318 |
*/ |
| 9319 |
while (++curtblindx < numTables) |
| 9320 |
{ |
| 9321 |
tbinfo = &tblinfo[curtblindx]; |
| 9322 |
if (tbinfo->dobj.catId.oid == attrelid) |
| 9323 |
break; |
| 9324 |
} |
| 9325 |
if (curtblindx >= numTables) |
| 9326 |
pg_fatal("unrecognized table OID %u", attrelid); |
| 9327 |
/* cross-check that we only got requested tables */ |
| 9328 |
if (tbinfo->relkind == RELKIND_SEQUENCE || |
| 9329 |
!tbinfo->interesting) |
| 9330 |
pg_fatal("unexpected column data for table \"%s\"", |
| 9331 |
tbinfo->dobj.name); |
| 9332 |
|
| 9333 |
/* Save data for this table */ |
| 9334 |
tbinfo->numatts = numatts; |
| 9335 |
tbinfo->attnames = (char **) pg_malloc(numatts * sizeof(char *)); |
| 9336 |
tbinfo->atttypnames = (char **) pg_malloc(numatts * sizeof(char *)); |
| 9337 |
tbinfo->attstattarget = (int *) pg_malloc(numatts * sizeof(int)); |
| 9338 |
tbinfo->attstorage = (char *) pg_malloc(numatts * sizeof(char)); |
| 9339 |
tbinfo->typstorage = (char *) pg_malloc(numatts * sizeof(char)); |
| 9340 |
tbinfo->attidentity = (char *) pg_malloc(numatts * sizeof(char)); |
| 9341 |
tbinfo->attgenerated = (char *) pg_malloc(numatts * sizeof(char)); |
| 9342 |
tbinfo->attisdropped = (bool *) pg_malloc(numatts * sizeof(bool)); |
| 9343 |
tbinfo->attlen = (int *) pg_malloc(numatts * sizeof(int)); |
| 9344 |
tbinfo->attalign = (char *) pg_malloc(numatts * sizeof(char)); |
| 9345 |
tbinfo->attislocal = (bool *) pg_malloc(numatts * sizeof(bool)); |
| 9346 |
tbinfo->attoptions = (char **) pg_malloc(numatts * sizeof(char *)); |
| 9347 |
tbinfo->attcollation = (Oid *) pg_malloc(numatts * sizeof(Oid)); |
| 9348 |
tbinfo->attcompression = (char *) pg_malloc(numatts * sizeof(char)); |
| 9349 |
tbinfo->attfdwoptions = (char **) pg_malloc(numatts * sizeof(char *)); |
| 9350 |
tbinfo->attmissingval = (char **) pg_malloc(numatts * sizeof(char *)); |
| 9351 |
tbinfo->notnull_constrs = (char **) pg_malloc(numatts * sizeof(char *)); |
| 9352 |
tbinfo->notnull_comment = (char **) pg_malloc(numatts * sizeof(char *)); |
| 9353 |
tbinfo->notnull_invalid = (bool *) pg_malloc(numatts * sizeof(bool)); |
| 9354 |
tbinfo->notnull_noinh = (bool *) pg_malloc(numatts * sizeof(bool)); |
| 9355 |
tbinfo->notnull_islocal = (bool *) pg_malloc(numatts * sizeof(bool)); |
| 9356 |
tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(numatts * sizeof(AttrDefInfo *)); |
| 9357 |
hasdefaults = false; |
| 9358 |
|
| 9359 |
for (int j = 0; j < numatts; j++, r++) |
| 9360 |
{ |
| 9361 |
if (j + 1 != atoi(PQgetvalue(res, r, i_attnum))) |
| 9362 |
pg_fatal("invalid column numbering in table \"%s\"", |
| 9363 |
tbinfo->dobj.name); |
| 9364 |
tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, r, i_attname)); |
| 9365 |
tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, r, i_atttypname)); |
| 9366 |
if (PQgetisnull(res, r, i_attstattarget)) |
| 9367 |
tbinfo->attstattarget[j] = -1; |
| 9368 |
else |
| 9369 |
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, r, i_attstattarget)); |
| 9370 |
tbinfo->attstorage[j] = *(PQgetvalue(res, r, i_attstorage)); |
| 9371 |
tbinfo->typstorage[j] = *(PQgetvalue(res, r, i_typstorage)); |
| 9372 |
tbinfo->attidentity[j] = *(PQgetvalue(res, r, i_attidentity)); |
| 9373 |
tbinfo->attgenerated[j] = *(PQgetvalue(res, r, i_attgenerated)); |
| 9374 |
tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS); |
| 9375 |
tbinfo->attisdropped[j] = (PQgetvalue(res, r, i_attisdropped)[0] == 't'); |
| 9376 |
tbinfo->attlen[j] = atoi(PQgetvalue(res, r, i_attlen)); |
| 9377 |
tbinfo->attalign[j] = *(PQgetvalue(res, r, i_attalign)); |
| 9378 |
tbinfo->attislocal[j] = (PQgetvalue(res, r, i_attislocal)[0] == 't'); |
| 9379 |
|
| 9380 |
/* Handle not-null constraint name and flags */ |
| 9381 |
determineNotNullFlags(fout, res, r, |
| 9382 |
tbinfo, j, |
| 9383 |
i_notnull_name, |
| 9384 |
i_notnull_comment, |
| 9385 |
i_notnull_invalidoid, |
| 9386 |
i_notnull_noinherit, |
| 9387 |
i_notnull_islocal, |
| 9388 |
&invalidnotnulloids); |
| 9389 |
|
| 9390 |
tbinfo->notnull_comment[j] = PQgetisnull(res, r, i_notnull_comment) ? |
| 9391 |
NULL : pg_strdup(PQgetvalue(res, r, i_notnull_comment)); |
| 9392 |
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, r, i_attoptions)); |
| 9393 |
tbinfo->attcollation[j] = atooid(PQgetvalue(res, r, i_attcollation)); |
| 9394 |
tbinfo->attcompression[j] = *(PQgetvalue(res, r, i_attcompression)); |
| 9395 |
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, r, i_attfdwoptions)); |
| 9396 |
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, r, i_attmissingval)); |
| 9397 |
tbinfo->attrdefs[j] = NULL; /* fix below */ |
| 9398 |
if (PQgetvalue(res, r, i_atthasdef)[0] == 't') |
| 9399 |
hasdefaults = true; |
| 9400 |
} |
| 9401 |
|
| 9402 |
if (hasdefaults) |
| 9403 |
{ |
| 9404 |
/* Collect OIDs of interesting tables that have defaults */ |
| 9405 |
if (tbloids->len > 1) /* do we have more than the '{'? */ |
| 9406 |
appendPQExpBufferChar(tbloids, ','); |
| 9407 |
appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); |
| 9408 |
} |
| 9409 |
} |
| 9410 |
|
| 9411 |
/* If invalidnotnulloids has any data, finalize it */ |
| 9412 |
if (invalidnotnulloids != NULL) |
| 9413 |
appendPQExpBufferChar(invalidnotnulloids, '}'); |
| 9414 |
|
| 9415 |
PQclear(res); |
| 9416 |
|
| 9417 |
/* |
| 9418 |
* Now get info about column defaults. This is skipped for a data-only |
| 9419 |
* dump, as it is only needed for table schemas. |
| 9420 |
*/ |
| 9421 |
if (dopt->dumpSchema && tbloids->len > 1) |
| 9422 |
{ |
| 9423 |
AttrDefInfo *attrdefs; |
| 9424 |
int numDefaults; |
| 9425 |
TableInfo *tbinfo = NULL; |
| 9426 |
|
| 9427 |
pg_log_info("finding table default expressions"); |
| 9428 |
|
| 9429 |
appendPQExpBufferChar(tbloids, '}'); |
| 9430 |
|
| 9431 |
printfPQExpBuffer(q, "SELECT a.tableoid, a.oid, adrelid, adnum, " |
| 9432 |
"pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc\n" |
| 9433 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 9434 |
"JOIN pg_catalog.pg_attrdef a ON (src.tbloid = a.adrelid)\n" |
| 9435 |
"ORDER BY a.adrelid, a.adnum", |
| 9436 |
tbloids->data); |
| 9437 |
|
| 9438 |
res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); |
| 9439 |
|
| 9440 |
numDefaults = PQntuples(res); |
| 9441 |
attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo)); |
| 9442 |
|
| 9443 |
curtblindx = -1; |
| 9444 |
for (int j = 0; j < numDefaults; j++) |
| 9445 |
{ |
| 9446 |
Oid adtableoid = atooid(PQgetvalue(res, j, 0)); |
| 9447 |
Oid adoid = atooid(PQgetvalue(res, j, 1)); |
| 9448 |
Oid adrelid = atooid(PQgetvalue(res, j, 2)); |
| 9449 |
int adnum = atoi(PQgetvalue(res, j, 3)); |
| 9450 |
char *adsrc = PQgetvalue(res, j, 4); |
| 9451 |
|
| 9452 |
/* |
| 9453 |
* Locate the associated TableInfo; we rely on tblinfo[] being in |
| 9454 |
* OID order. |
| 9455 |
*/ |
| 9456 |
if (tbinfo == NULL || tbinfo->dobj.catId.oid != adrelid) |
| 9457 |
{ |
| 9458 |
while (++curtblindx < numTables) |
| 9459 |
{ |
| 9460 |
tbinfo = &tblinfo[curtblindx]; |
| 9461 |
if (tbinfo->dobj.catId.oid == adrelid) |
| 9462 |
break; |
| 9463 |
} |
| 9464 |
if (curtblindx >= numTables) |
| 9465 |
pg_fatal("unrecognized table OID %u", adrelid); |
| 9466 |
} |
| 9467 |
|
| 9468 |
if (adnum <= 0 || adnum > tbinfo->numatts) |
| 9469 |
pg_fatal("invalid adnum value %d for table \"%s\"", |
| 9470 |
adnum, tbinfo->dobj.name); |
| 9471 |
|
| 9472 |
/* |
| 9473 |
* dropped columns shouldn't have defaults, but just in case, |
| 9474 |
* ignore 'em |
| 9475 |
*/ |
| 9476 |
if (tbinfo->attisdropped[adnum - 1]) |
| 9477 |
continue; |
| 9478 |
|
| 9479 |
attrdefs[j].dobj.objType = DO_ATTRDEF; |
| 9480 |
attrdefs[j].dobj.catId.tableoid = adtableoid; |
| 9481 |
attrdefs[j].dobj.catId.oid = adoid; |
| 9482 |
AssignDumpId(&attrdefs[j].dobj); |
| 9483 |
attrdefs[j].adtable = tbinfo; |
| 9484 |
attrdefs[j].adnum = adnum; |
| 9485 |
attrdefs[j].adef_expr = pg_strdup(adsrc); |
| 9486 |
|
| 9487 |
attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name); |
| 9488 |
attrdefs[j].dobj.namespace = tbinfo->dobj.namespace; |
| 9489 |
|
| 9490 |
attrdefs[j].dobj.dump = tbinfo->dobj.dump; |
| 9491 |
|
| 9492 |
/* |
| 9493 |
* Figure out whether the default/generation expression should be |
| 9494 |
* dumped as part of the main CREATE TABLE (or similar) command or |
| 9495 |
* as a separate ALTER TABLE (or similar) command. The preference |
| 9496 |
* is to put it into the CREATE command, but in some cases that's |
| 9497 |
* not possible. |
| 9498 |
*/ |
| 9499 |
if (tbinfo->attgenerated[adnum - 1]) |
| 9500 |
{ |
| 9501 |
/* |
| 9502 |
* Column generation expressions cannot be dumped separately, |
| 9503 |
* because there is no syntax for it. By setting separate to |
| 9504 |
* false here we prevent the "default" from being processed as |
| 9505 |
* its own dumpable object. Later, flagInhAttrs() will mark |
| 9506 |
* it as not to be dumped at all, if possible (that is, if it |
| 9507 |
* can be inherited from a parent). |
| 9508 |
*/ |
| 9509 |
attrdefs[j].separate = false; |
| 9510 |
} |
| 9511 |
else if (tbinfo->relkind == RELKIND_VIEW) |
| 9512 |
{ |
| 9513 |
/* |
| 9514 |
* Defaults on a VIEW must always be dumped as separate ALTER |
| 9515 |
* TABLE commands. |
| 9516 |
*/ |
| 9517 |
attrdefs[j].separate = true; |
| 9518 |
} |
| 9519 |
else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1)) |
| 9520 |
{ |
| 9521 |
/* column will be suppressed, print default separately */ |
| 9522 |
attrdefs[j].separate = true; |
| 9523 |
} |
| 9524 |
else |
| 9525 |
{ |
| 9526 |
attrdefs[j].separate = false; |
| 9527 |
} |
| 9528 |
|
| 9529 |
if (!attrdefs[j].separate) |
| 9530 |
{ |
| 9531 |
/* |
| 9532 |
* Mark the default as needing to appear before the table, so |
| 9533 |
* that any dependencies it has must be emitted before the |
| 9534 |
* CREATE TABLE. If this is not possible, we'll change to |
| 9535 |
* "separate" mode while sorting dependencies. |
| 9536 |
*/ |
| 9537 |
addObjectDependency(&tbinfo->dobj, |
| 9538 |
attrdefs[j].dobj.dumpId); |
| 9539 |
} |
| 9540 |
|
| 9541 |
tbinfo->attrdefs[adnum - 1] = &attrdefs[j]; |
| 9542 |
} |
| 9543 |
|
| 9544 |
PQclear(res); |
| 9545 |
} |
| 9546 |
|
| 9547 |
/* |
| 9548 |
* Get info about NOT NULL NOT VALID constraints. This is skipped for a |
| 9549 |
* data-only dump, as it is only needed for table schemas. |
| 9550 |
*/ |
| 9551 |
if (dopt->dumpSchema && invalidnotnulloids) |
| 9552 |
{ |
| 9553 |
ConstraintInfo *constrs; |
| 9554 |
int numConstrs; |
| 9555 |
int i_tableoid; |
| 9556 |
int i_oid; |
| 9557 |
int i_conrelid; |
| 9558 |
int i_conname; |
| 9559 |
int i_consrc; |
| 9560 |
int i_conislocal; |
| 9561 |
|
| 9562 |
pg_log_info("finding invalid not-null constraints"); |
| 9563 |
|
| 9564 |
resetPQExpBuffer(q); |
| 9565 |
appendPQExpBuffer(q, |
| 9566 |
"SELECT c.tableoid, c.oid, conrelid, conname, " |
| 9567 |
"pg_catalog.pg_get_constraintdef(c.oid) AS consrc, " |
| 9568 |
"conislocal, convalidated " |
| 9569 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(conoid)\n" |
| 9570 |
"JOIN pg_catalog.pg_constraint c ON (src.conoid = c.oid)\n" |
| 9571 |
"ORDER BY c.conrelid, c.conname", |
| 9572 |
invalidnotnulloids->data); |
| 9573 |
|
| 9574 |
res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); |
| 9575 |
|
| 9576 |
numConstrs = PQntuples(res); |
| 9577 |
constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo)); |
| 9578 |
|
| 9579 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 9580 |
i_oid = PQfnumber(res, "oid"); |
| 9581 |
i_conrelid = PQfnumber(res, "conrelid"); |
| 9582 |
i_conname = PQfnumber(res, "conname"); |
| 9583 |
i_consrc = PQfnumber(res, "consrc"); |
| 9584 |
i_conislocal = PQfnumber(res, "conislocal"); |
| 9585 |
|
| 9586 |
/* As above, this loop iterates once per table, not once per row */ |
| 9587 |
curtblindx = -1; |
| 9588 |
for (int j = 0; j < numConstrs;) |
| 9589 |
{ |
| 9590 |
Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid)); |
| 9591 |
TableInfo *tbinfo = NULL; |
| 9592 |
int numcons; |
| 9593 |
|
| 9594 |
/* Count rows for this table */ |
| 9595 |
for (numcons = 1; numcons < numConstrs - j; numcons++) |
| 9596 |
if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid) |
| 9597 |
break; |
| 9598 |
|
| 9599 |
/* |
| 9600 |
* Locate the associated TableInfo; we rely on tblinfo[] being in |
| 9601 |
* OID order. |
| 9602 |
*/ |
| 9603 |
while (++curtblindx < numTables) |
| 9604 |
{ |
| 9605 |
tbinfo = &tblinfo[curtblindx]; |
| 9606 |
if (tbinfo->dobj.catId.oid == conrelid) |
| 9607 |
break; |
| 9608 |
} |
| 9609 |
if (curtblindx >= numTables) |
| 9610 |
pg_fatal("unrecognized table OID %u", conrelid); |
| 9611 |
|
| 9612 |
for (int c = 0; c < numcons; c++, j++) |
| 9613 |
{ |
| 9614 |
constrs[j].dobj.objType = DO_CONSTRAINT; |
| 9615 |
constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); |
| 9616 |
constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); |
| 9617 |
AssignDumpId(&constrs[j].dobj); |
| 9618 |
constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); |
| 9619 |
constrs[j].dobj.namespace = tbinfo->dobj.namespace; |
| 9620 |
constrs[j].contable = tbinfo; |
| 9621 |
constrs[j].condomain = NULL; |
| 9622 |
constrs[j].contype = 'n'; |
| 9623 |
constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc)); |
| 9624 |
constrs[j].confrelid = InvalidOid; |
| 9625 |
constrs[j].conindex = 0; |
| 9626 |
constrs[j].condeferrable = false; |
| 9627 |
constrs[j].condeferred = false; |
| 9628 |
constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't'); |
| 9629 |
|
| 9630 |
/* |
| 9631 |
* All invalid not-null constraints must be dumped separately, |
| 9632 |
* because CREATE TABLE would not create them as invalid, and |
| 9633 |
* also because they must be created after potentially |
| 9634 |
* violating data has been loaded. |
| 9635 |
*/ |
| 9636 |
constrs[j].separate = true; |
| 9637 |
|
| 9638 |
constrs[j].dobj.dump = tbinfo->dobj.dump; |
| 9639 |
} |
| 9640 |
} |
| 9641 |
PQclear(res); |
| 9642 |
} |
| 9643 |
|
| 9644 |
/* |
| 9645 |
* Get info about table CHECK constraints. This is skipped for a |
| 9646 |
* data-only dump, as it is only needed for table schemas. |
| 9647 |
*/ |
| 9648 |
if (dopt->dumpSchema && checkoids->len > 2) |
| 9649 |
{ |
| 9650 |
ConstraintInfo *constrs; |
| 9651 |
int numConstrs; |
| 9652 |
int i_tableoid; |
| 9653 |
int i_oid; |
| 9654 |
int i_conrelid; |
| 9655 |
int i_conname; |
| 9656 |
int i_consrc; |
| 9657 |
int i_conislocal; |
| 9658 |
int i_convalidated; |
| 9659 |
|
| 9660 |
pg_log_info("finding table check constraints"); |
| 9661 |
|
| 9662 |
resetPQExpBuffer(q); |
| 9663 |
appendPQExpBuffer(q, |
| 9664 |
"SELECT c.tableoid, c.oid, conrelid, conname, " |
| 9665 |
"pg_catalog.pg_get_constraintdef(c.oid) AS consrc, " |
| 9666 |
"conislocal, convalidated " |
| 9667 |
"FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" |
| 9668 |
"JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n" |
| 9669 |
"WHERE contype = 'c' " |
| 9670 |
"ORDER BY c.conrelid, c.conname", |
| 9671 |
checkoids->data); |
| 9672 |
|
| 9673 |
res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); |
| 9674 |
|
| 9675 |
numConstrs = PQntuples(res); |
| 9676 |
constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo)); |
| 9677 |
|
| 9678 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 9679 |
i_oid = PQfnumber(res, "oid"); |
| 9680 |
i_conrelid = PQfnumber(res, "conrelid"); |
| 9681 |
i_conname = PQfnumber(res, "conname"); |
| 9682 |
i_consrc = PQfnumber(res, "consrc"); |
| 9683 |
i_conislocal = PQfnumber(res, "conislocal"); |
| 9684 |
i_convalidated = PQfnumber(res, "convalidated"); |
| 9685 |
|
| 9686 |
/* As above, this loop iterates once per table, not once per row */ |
| 9687 |
curtblindx = -1; |
| 9688 |
for (int j = 0; j < numConstrs;) |
| 9689 |
{ |
| 9690 |
Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid)); |
| 9691 |
TableInfo *tbinfo = NULL; |
| 9692 |
int numcons; |
| 9693 |
|
| 9694 |
/* Count rows for this table */ |
| 9695 |
for (numcons = 1; numcons < numConstrs - j; numcons++) |
| 9696 |
if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid) |
| 9697 |
break; |
| 9698 |
|
| 9699 |
/* |
| 9700 |
* Locate the associated TableInfo; we rely on tblinfo[] being in |
| 9701 |
* OID order. |
| 9702 |
*/ |
| 9703 |
while (++curtblindx < numTables) |
| 9704 |
{ |
| 9705 |
tbinfo = &tblinfo[curtblindx]; |
| 9706 |
if (tbinfo->dobj.catId.oid == conrelid) |
| 9707 |
break; |
| 9708 |
} |
| 9709 |
if (curtblindx >= numTables) |
| 9710 |
pg_fatal("unrecognized table OID %u", conrelid); |
| 9711 |
|
| 9712 |
if (numcons != tbinfo->ncheck) |
| 9713 |
{ |
| 9714 |
pg_log_error(ngettext("expected %d check constraint on table \"%s\" but found %d", |
| 9715 |
"expected %d check constraints on table \"%s\" but found %d", |
| 9716 |
tbinfo->ncheck), |
| 9717 |
tbinfo->ncheck, tbinfo->dobj.name, numcons); |
| 9718 |
pg_log_error_hint("The system catalogs might be corrupted."); |
| 9719 |
exit_nicely(1); |
| 9720 |
} |
| 9721 |
|
| 9722 |
tbinfo->checkexprs = constrs + j; |
| 9723 |
|
| 9724 |
for (int c = 0; c < numcons; c++, j++) |
| 9725 |
{ |
| 9726 |
bool validated = PQgetvalue(res, j, i_convalidated)[0] == 't'; |
| 9727 |
|
| 9728 |
constrs[j].dobj.objType = DO_CONSTRAINT; |
| 9729 |
constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); |
| 9730 |
constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); |
| 9731 |
AssignDumpId(&constrs[j].dobj); |
| 9732 |
constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); |
| 9733 |
constrs[j].dobj.namespace = tbinfo->dobj.namespace; |
| 9734 |
constrs[j].contable = tbinfo; |
| 9735 |
constrs[j].condomain = NULL; |
| 9736 |
constrs[j].contype = 'c'; |
| 9737 |
constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc)); |
| 9738 |
constrs[j].confrelid = InvalidOid; |
| 9739 |
constrs[j].conindex = 0; |
| 9740 |
constrs[j].condeferrable = false; |
| 9741 |
constrs[j].condeferred = false; |
| 9742 |
constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't'); |
| 9743 |
|
| 9744 |
/* |
| 9745 |
* An unvalidated constraint needs to be dumped separately, so |
| 9746 |
* that potentially-violating existing data is loaded before |
| 9747 |
* the constraint. |
| 9748 |
*/ |
| 9749 |
constrs[j].separate = !validated; |
| 9750 |
|
| 9751 |
constrs[j].dobj.dump = tbinfo->dobj.dump; |
| 9752 |
|
| 9753 |
/* |
| 9754 |
* Mark the constraint as needing to appear before the table |
| 9755 |
* --- this is so that any other dependencies of the |
| 9756 |
* constraint will be emitted before we try to create the |
| 9757 |
* table. If the constraint is to be dumped separately, it |
| 9758 |
* will be dumped after data is loaded anyway, so don't do it. |
| 9759 |
* (There's an automatic dependency in the opposite direction |
| 9760 |
* anyway, so don't need to add one manually here.) |
| 9761 |
*/ |
| 9762 |
if (!constrs[j].separate) |
| 9763 |
addObjectDependency(&tbinfo->dobj, |
| 9764 |
constrs[j].dobj.dumpId); |
| 9765 |
|
| 9766 |
/* |
| 9767 |
* We will detect later whether the constraint must be split |
| 9768 |
* out from the table definition. |
| 9769 |
*/ |
| 9770 |
} |
| 9771 |
} |
| 9772 |
|
| 9773 |
PQclear(res); |
| 9774 |
} |
| 9775 |
|
| 9776 |
destroyPQExpBuffer(q); |
| 9777 |
destroyPQExpBuffer(tbloids); |
| 9778 |
destroyPQExpBuffer(checkoids); |
| 9779 |
} |
| 9780 |
|
| 9781 |
/* |
| 9782 |
* Based on the getTableAttrs query's row corresponding to one column, set |
| 9783 |
* the name and flags to handle a not-null constraint for that column in |
| 9784 |
* the tbinfo struct. |
| 9785 |
* |
| 9786 |
* Result row 'r' is for tbinfo's attribute 'j'. |
| 9787 |
* |
| 9788 |
* There are four possibilities: |
| 9789 |
* 1) the column has no not-null constraints. In that case, ->notnull_constrs |
| 9790 |
* (the constraint name) remains NULL. |
| 9791 |
* 2) The column has a constraint with no name (this is the case when |
| 9792 |
* constraints come from pre-18 servers). In this case, ->notnull_constrs |
| 9793 |
* is set to the empty string; dumpTableSchema will print just "NOT NULL". |
| 9794 |
* 3) The column has an invalid not-null constraint. This must be treated |
| 9795 |
* as a separate object (because it must be created after the table data |
| 9796 |
* is loaded). So we add its OID to invalidnotnulloids for processing |
| 9797 |
* elsewhere and do nothing further with it here. We distinguish this |
| 9798 |
* case because the "notnull_invalidoid" column has been set to a non-NULL |
| 9799 |
* value, which is the constraint OID. Valid constraints have a null OID. |
| 9800 |
* 4) The column has a constraint with a known name; in that case |
| 9801 |
* notnull_constrs carries that name and dumpTableSchema will print |
| 9802 |
* "CONSTRAINT the_name NOT NULL". However, if the name is the default |
| 9803 |
* (table_column_not_null) and there's no comment on the constraint, |
| 9804 |
* there's no need to print that name in the dump, so notnull_constrs |
| 9805 |
* is set to the empty string and it behaves as case 2. |
| 9806 |
* |
| 9807 |
* In a child table that inherits from a parent already containing NOT NULL |
| 9808 |
* constraints and the columns in the child don't have their own NOT NULL |
| 9809 |
* declarations, we suppress printing constraints in the child: the |
| 9810 |
* constraints are acquired at the point where the child is attached to the |
| 9811 |
* parent. This is tracked in ->notnull_islocal; for servers pre-18 this is |
| 9812 |
* set not here but in flagInhAttrs. That flag is also used when the |
| 9813 |
* constraint was validated in a child but all its parent have it as NOT |
| 9814 |
* VALID. |
| 9815 |
* |
| 9816 |
* Any of these constraints might have the NO INHERIT bit. If so we set |
| 9817 |
* ->notnull_noinh and NO INHERIT will be printed by dumpTableSchema. |
| 9818 |
* |
| 9819 |
* In case 4 above, the name comparison is a bit of a hack; it actually fails |
| 9820 |
* to do the right thing in all but the trivial case. However, the downside |
| 9821 |
* of getting it wrong is simply that the name is printed rather than |
| 9822 |
* suppressed, so it's not a big deal. |
| 9823 |
* |
| 9824 |
* invalidnotnulloids is expected to be given as NULL; if any invalid not-null |
| 9825 |
* constraints are found, it is initialized and filled with the array of |
| 9826 |
* OIDs of such constraints, for later processing. |
| 9827 |
*/ |
| 9828 |
static void |
| 9829 |
determineNotNullFlags(Archive *fout, PGresult *res, int r, |
| 9830 |
TableInfo *tbinfo, int j, |
| 9831 |
int i_notnull_name, |
| 9832 |
int i_notnull_comment, |
| 9833 |
int i_notnull_invalidoid, |
| 9834 |
int i_notnull_noinherit, |
| 9835 |
int i_notnull_islocal, |
| 9836 |
PQExpBuffer *invalidnotnulloids) |
| 9837 |
{ |
| 9838 |
DumpOptions *dopt = fout->dopt; |
| 9839 |
|
| 9840 |
/* |
| 9841 |
* If this not-null constraint is not valid, list its OID in |
| 9842 |
* invalidnotnulloids and do nothing further. It'll be processed |
| 9843 |
* elsewhere later. |
| 9844 |
* |
| 9845 |
* Because invalid not-null constraints are rare, we don't want to malloc |
| 9846 |
* invalidnotnulloids until we're sure we're going it need it, which |
| 9847 |
* happens here. |
| 9848 |
*/ |
| 9849 |
if (!PQgetisnull(res, r, i_notnull_invalidoid)) |
| 9850 |
{ |
| 9851 |
char *constroid = PQgetvalue(res, r, i_notnull_invalidoid); |
| 9852 |
|
| 9853 |
if (*invalidnotnulloids == NULL) |
| 9854 |
{ |
| 9855 |
*invalidnotnulloids = createPQExpBuffer(); |
| 9856 |
appendPQExpBufferChar(*invalidnotnulloids, '{'); |
| 9857 |
appendPQExpBufferStr(*invalidnotnulloids, constroid); |
| 9858 |
} |
| 9859 |
else |
| 9860 |
appendPQExpBuffer(*invalidnotnulloids, ",%s", constroid); |
| 9861 |
|
| 9862 |
/* |
| 9863 |
* Track when a parent constraint is invalid for the cases where a |
| 9864 |
* child constraint has been validated independenly. |
| 9865 |
*/ |
| 9866 |
tbinfo->notnull_invalid[j] = true; |
| 9867 |
|
| 9868 |
/* nothing else to do */ |
| 9869 |
tbinfo->notnull_constrs[j] = NULL; |
| 9870 |
return; |
| 9871 |
} |
| 9872 |
|
| 9873 |
/* |
| 9874 |
* notnull_noinh is straight from the query result. notnull_islocal also, |
| 9875 |
* though flagInhAttrs may change that one later. |
| 9876 |
*/ |
| 9877 |
tbinfo->notnull_noinh[j] = PQgetvalue(res, r, i_notnull_noinherit)[0] == 't'; |
| 9878 |
tbinfo->notnull_islocal[j] = PQgetvalue(res, r, i_notnull_islocal)[0] == 't'; |
| 9879 |
tbinfo->notnull_invalid[j] = false; |
| 9880 |
|
| 9881 |
/* |
| 9882 |
* Determine a constraint name to use. If the column is not marked not- |
| 9883 |
* null, we set NULL which cues ... to do nothing. An empty string says |
| 9884 |
* to print an unnamed NOT NULL, and anything else is a constraint name to |
| 9885 |
* use. |
| 9886 |
*/ |
| 9887 |
if (fout->remoteVersion < 180000) |
| 9888 |
{ |
| 9889 |
/* |
| 9890 |
* < 18 doesn't have not-null names, so an unnamed constraint is |
| 9891 |
* sufficient. |
| 9892 |
*/ |
| 9893 |
if (PQgetisnull(res, r, i_notnull_name)) |
| 9894 |
tbinfo->notnull_constrs[j] = NULL; |
| 9895 |
else |
| 9896 |
tbinfo->notnull_constrs[j] = ""; |
| 9897 |
} |
| 9898 |
else |
| 9899 |
{ |
| 9900 |
if (PQgetisnull(res, r, i_notnull_name)) |
| 9901 |
tbinfo->notnull_constrs[j] = NULL; |
| 9902 |
else |
| 9903 |
{ |
| 9904 |
/* |
| 9905 |
* In binary upgrade of inheritance child tables, must have a |
| 9906 |
* constraint name that we can UPDATE later; same if there's a |
| 9907 |
* comment on the constraint. |
| 9908 |
*/ |
| 9909 |
if ((dopt->binary_upgrade && |
| 9910 |
!tbinfo->ispartition && |
| 9911 |
!tbinfo->notnull_islocal[j]) || |
| 9912 |
!PQgetisnull(res, r, i_notnull_comment)) |
| 9913 |
{ |
| 9914 |
tbinfo->notnull_constrs[j] = |
| 9915 |
pstrdup(PQgetvalue(res, r, i_notnull_name)); |
| 9916 |
} |
| 9917 |
else |
| 9918 |
{ |
| 9919 |
char *default_name; |
| 9920 |
|
| 9921 |
/* XXX should match ChooseConstraintName better */ |
| 9922 |
default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name, |
| 9923 |
tbinfo->attnames[j]); |
| 9924 |
if (strcmp(default_name, |
| 9925 |
PQgetvalue(res, r, i_notnull_name)) == 0) |
| 9926 |
tbinfo->notnull_constrs[j] = ""; |
| 9927 |
else |
| 9928 |
{ |
| 9929 |
tbinfo->notnull_constrs[j] = |
| 9930 |
pstrdup(PQgetvalue(res, r, i_notnull_name)); |
| 9931 |
} |
| 9932 |
free(default_name); |
| 9933 |
} |
| 9934 |
} |
| 9935 |
} |
| 9936 |
} |
| 9937 |
|
| 9938 |
/* |
| 9939 |
* Test whether a column should be printed as part of table's CREATE TABLE. |
| 9940 |
* Column number is zero-based. |
| 9941 |
* |
| 9942 |
* Normally this is always true, but it's false for dropped columns, as well |
| 9943 |
* as those that were inherited without any local definition. (If we print |
| 9944 |
* such a column it will mistakenly get pg_attribute.attislocal set to true.) |
| 9945 |
* For partitions, it's always true, because we want the partitions to be |
| 9946 |
* created independently and ATTACH PARTITION used afterwards. |
| 9947 |
* |
| 9948 |
* In binary_upgrade mode, we must print all columns and fix the attislocal/ |
| 9949 |
* attisdropped state later, so as to keep control of the physical column |
| 9950 |
* order. |
| 9951 |
* |
| 9952 |
* This function exists because there are scattered nonobvious places that |
| 9953 |
* must be kept in sync with this decision. |
| 9954 |
*/ |
| 9955 |
bool |
| 9956 |
shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno) |
| 9957 |
{ |
| 9958 |
if (dopt->binary_upgrade) |
| 9959 |
return true; |
| 9960 |
if (tbinfo->attisdropped[colno]) |
| 9961 |
return false; |
| 9962 |
return (tbinfo->attislocal[colno] || tbinfo->ispartition); |
| 9963 |
} |
| 9964 |
|
| 9965 |
|
| 9966 |
/* |
| 9967 |
* getTSParsers: |
| 9968 |
* get information about all text search parsers in the system catalogs |
| 9969 |
*/ |
| 9970 |
void |
| 9971 |
getTSParsers(Archive *fout) |
| 9972 |
{ |
| 9973 |
PGresult *res; |
| 9974 |
int ntups; |
| 9975 |
int i; |
| 9976 |
PQExpBuffer query; |
| 9977 |
TSParserInfo *prsinfo; |
| 9978 |
int i_tableoid; |
| 9979 |
int i_oid; |
| 9980 |
int i_prsname; |
| 9981 |
int i_prsnamespace; |
| 9982 |
int i_prsstart; |
| 9983 |
int i_prstoken; |
| 9984 |
int i_prsend; |
| 9985 |
int i_prsheadline; |
| 9986 |
int i_prslextype; |
| 9987 |
|
| 9988 |
query = createPQExpBuffer(); |
| 9989 |
|
| 9990 |
/* |
| 9991 |
* find all text search objects, including builtin ones; we filter out |
| 9992 |
* system-defined objects at dump-out time. |
| 9993 |
*/ |
| 9994 |
|
| 9995 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, " |
| 9996 |
"prsstart::oid, prstoken::oid, " |
| 9997 |
"prsend::oid, prsheadline::oid, prslextype::oid " |
| 9998 |
"FROM pg_ts_parser"); |
| 9999 |
|
| 10000 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10001 |
|
| 10002 |
ntups = PQntuples(res); |
| 10003 |
|
| 10004 |
prsinfo = (TSParserInfo *) pg_malloc(ntups * sizeof(TSParserInfo)); |
| 10005 |
|
| 10006 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 10007 |
i_oid = PQfnumber(res, "oid"); |
| 10008 |
i_prsname = PQfnumber(res, "prsname"); |
| 10009 |
i_prsnamespace = PQfnumber(res, "prsnamespace"); |
| 10010 |
i_prsstart = PQfnumber(res, "prsstart"); |
| 10011 |
i_prstoken = PQfnumber(res, "prstoken"); |
| 10012 |
i_prsend = PQfnumber(res, "prsend"); |
| 10013 |
i_prsheadline = PQfnumber(res, "prsheadline"); |
| 10014 |
i_prslextype = PQfnumber(res, "prslextype"); |
| 10015 |
|
| 10016 |
for (i = 0; i < ntups; i++) |
| 10017 |
{ |
| 10018 |
prsinfo[i].dobj.objType = DO_TSPARSER; |
| 10019 |
prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 10020 |
prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 10021 |
AssignDumpId(&prsinfo[i].dobj); |
| 10022 |
prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname)); |
| 10023 |
prsinfo[i].dobj.namespace = |
| 10024 |
findNamespace(atooid(PQgetvalue(res, i, i_prsnamespace))); |
| 10025 |
prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart)); |
| 10026 |
prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken)); |
| 10027 |
prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend)); |
| 10028 |
prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline)); |
| 10029 |
prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype)); |
| 10030 |
|
| 10031 |
/* Decide whether we want to dump it */ |
| 10032 |
selectDumpableObject(&(prsinfo[i].dobj), fout); |
| 10033 |
} |
| 10034 |
|
| 10035 |
PQclear(res); |
| 10036 |
|
| 10037 |
destroyPQExpBuffer(query); |
| 10038 |
} |
| 10039 |
|
| 10040 |
/* |
| 10041 |
* getTSDictionaries: |
| 10042 |
* get information about all text search dictionaries in the system catalogs |
| 10043 |
*/ |
| 10044 |
void |
| 10045 |
getTSDictionaries(Archive *fout) |
| 10046 |
{ |
| 10047 |
PGresult *res; |
| 10048 |
int ntups; |
| 10049 |
int i; |
| 10050 |
PQExpBuffer query; |
| 10051 |
TSDictInfo *dictinfo; |
| 10052 |
int i_tableoid; |
| 10053 |
int i_oid; |
| 10054 |
int i_dictname; |
| 10055 |
int i_dictnamespace; |
| 10056 |
int i_dictowner; |
| 10057 |
int i_dicttemplate; |
| 10058 |
int i_dictinitoption; |
| 10059 |
|
| 10060 |
query = createPQExpBuffer(); |
| 10061 |
|
| 10062 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, dictname, " |
| 10063 |
"dictnamespace, dictowner, " |
| 10064 |
"dicttemplate, dictinitoption " |
| 10065 |
"FROM pg_ts_dict"); |
| 10066 |
|
| 10067 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10068 |
|
| 10069 |
ntups = PQntuples(res); |
| 10070 |
|
| 10071 |
dictinfo = (TSDictInfo *) pg_malloc(ntups * sizeof(TSDictInfo)); |
| 10072 |
|
| 10073 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 10074 |
i_oid = PQfnumber(res, "oid"); |
| 10075 |
i_dictname = PQfnumber(res, "dictname"); |
| 10076 |
i_dictnamespace = PQfnumber(res, "dictnamespace"); |
| 10077 |
i_dictowner = PQfnumber(res, "dictowner"); |
| 10078 |
i_dictinitoption = PQfnumber(res, "dictinitoption"); |
| 10079 |
i_dicttemplate = PQfnumber(res, "dicttemplate"); |
| 10080 |
|
| 10081 |
for (i = 0; i < ntups; i++) |
| 10082 |
{ |
| 10083 |
dictinfo[i].dobj.objType = DO_TSDICT; |
| 10084 |
dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 10085 |
dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 10086 |
AssignDumpId(&dictinfo[i].dobj); |
| 10087 |
dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname)); |
| 10088 |
dictinfo[i].dobj.namespace = |
| 10089 |
findNamespace(atooid(PQgetvalue(res, i, i_dictnamespace))); |
| 10090 |
dictinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_dictowner)); |
| 10091 |
dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate)); |
| 10092 |
if (PQgetisnull(res, i, i_dictinitoption)) |
| 10093 |
dictinfo[i].dictinitoption = NULL; |
| 10094 |
else |
| 10095 |
dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption)); |
| 10096 |
|
| 10097 |
/* Decide whether we want to dump it */ |
| 10098 |
selectDumpableObject(&(dictinfo[i].dobj), fout); |
| 10099 |
} |
| 10100 |
|
| 10101 |
PQclear(res); |
| 10102 |
|
| 10103 |
destroyPQExpBuffer(query); |
| 10104 |
} |
| 10105 |
|
| 10106 |
/* |
| 10107 |
* getTSTemplates: |
| 10108 |
* get information about all text search templates in the system catalogs |
| 10109 |
*/ |
| 10110 |
void |
| 10111 |
getTSTemplates(Archive *fout) |
| 10112 |
{ |
| 10113 |
PGresult *res; |
| 10114 |
int ntups; |
| 10115 |
int i; |
| 10116 |
PQExpBuffer query; |
| 10117 |
TSTemplateInfo *tmplinfo; |
| 10118 |
int i_tableoid; |
| 10119 |
int i_oid; |
| 10120 |
int i_tmplname; |
| 10121 |
int i_tmplnamespace; |
| 10122 |
int i_tmplinit; |
| 10123 |
int i_tmpllexize; |
| 10124 |
|
| 10125 |
query = createPQExpBuffer(); |
| 10126 |
|
| 10127 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, " |
| 10128 |
"tmplnamespace, tmplinit::oid, tmpllexize::oid " |
| 10129 |
"FROM pg_ts_template"); |
| 10130 |
|
| 10131 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10132 |
|
| 10133 |
ntups = PQntuples(res); |
| 10134 |
|
| 10135 |
tmplinfo = (TSTemplateInfo *) pg_malloc(ntups * sizeof(TSTemplateInfo)); |
| 10136 |
|
| 10137 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 10138 |
i_oid = PQfnumber(res, "oid"); |
| 10139 |
i_tmplname = PQfnumber(res, "tmplname"); |
| 10140 |
i_tmplnamespace = PQfnumber(res, "tmplnamespace"); |
| 10141 |
i_tmplinit = PQfnumber(res, "tmplinit"); |
| 10142 |
i_tmpllexize = PQfnumber(res, "tmpllexize"); |
| 10143 |
|
| 10144 |
for (i = 0; i < ntups; i++) |
| 10145 |
{ |
| 10146 |
tmplinfo[i].dobj.objType = DO_TSTEMPLATE; |
| 10147 |
tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 10148 |
tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 10149 |
AssignDumpId(&tmplinfo[i].dobj); |
| 10150 |
tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname)); |
| 10151 |
tmplinfo[i].dobj.namespace = |
| 10152 |
findNamespace(atooid(PQgetvalue(res, i, i_tmplnamespace))); |
| 10153 |
tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit)); |
| 10154 |
tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize)); |
| 10155 |
|
| 10156 |
/* Decide whether we want to dump it */ |
| 10157 |
selectDumpableObject(&(tmplinfo[i].dobj), fout); |
| 10158 |
} |
| 10159 |
|
| 10160 |
PQclear(res); |
| 10161 |
|
| 10162 |
destroyPQExpBuffer(query); |
| 10163 |
} |
| 10164 |
|
| 10165 |
/* |
| 10166 |
* getTSConfigurations: |
| 10167 |
* get information about all text search configurations |
| 10168 |
*/ |
| 10169 |
void |
| 10170 |
getTSConfigurations(Archive *fout) |
| 10171 |
{ |
| 10172 |
PGresult *res; |
| 10173 |
int ntups; |
| 10174 |
int i; |
| 10175 |
PQExpBuffer query; |
| 10176 |
TSConfigInfo *cfginfo; |
| 10177 |
int i_tableoid; |
| 10178 |
int i_oid; |
| 10179 |
int i_cfgname; |
| 10180 |
int i_cfgnamespace; |
| 10181 |
int i_cfgowner; |
| 10182 |
int i_cfgparser; |
| 10183 |
|
| 10184 |
query = createPQExpBuffer(); |
| 10185 |
|
| 10186 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, cfgname, " |
| 10187 |
"cfgnamespace, cfgowner, cfgparser " |
| 10188 |
"FROM pg_ts_config"); |
| 10189 |
|
| 10190 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10191 |
|
| 10192 |
ntups = PQntuples(res); |
| 10193 |
|
| 10194 |
cfginfo = (TSConfigInfo *) pg_malloc(ntups * sizeof(TSConfigInfo)); |
| 10195 |
|
| 10196 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 10197 |
i_oid = PQfnumber(res, "oid"); |
| 10198 |
i_cfgname = PQfnumber(res, "cfgname"); |
| 10199 |
i_cfgnamespace = PQfnumber(res, "cfgnamespace"); |
| 10200 |
i_cfgowner = PQfnumber(res, "cfgowner"); |
| 10201 |
i_cfgparser = PQfnumber(res, "cfgparser"); |
| 10202 |
|
| 10203 |
for (i = 0; i < ntups; i++) |
| 10204 |
{ |
| 10205 |
cfginfo[i].dobj.objType = DO_TSCONFIG; |
| 10206 |
cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 10207 |
cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 10208 |
AssignDumpId(&cfginfo[i].dobj); |
| 10209 |
cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname)); |
| 10210 |
cfginfo[i].dobj.namespace = |
| 10211 |
findNamespace(atooid(PQgetvalue(res, i, i_cfgnamespace))); |
| 10212 |
cfginfo[i].rolname = getRoleName(PQgetvalue(res, i, i_cfgowner)); |
| 10213 |
cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser)); |
| 10214 |
|
| 10215 |
/* Decide whether we want to dump it */ |
| 10216 |
selectDumpableObject(&(cfginfo[i].dobj), fout); |
| 10217 |
} |
| 10218 |
|
| 10219 |
PQclear(res); |
| 10220 |
|
| 10221 |
destroyPQExpBuffer(query); |
| 10222 |
} |
| 10223 |
|
| 10224 |
/* |
| 10225 |
* getForeignDataWrappers: |
| 10226 |
* get information about all foreign-data wrappers in the system catalogs |
| 10227 |
*/ |
| 10228 |
void |
| 10229 |
getForeignDataWrappers(Archive *fout) |
| 10230 |
{ |
| 10231 |
PGresult *res; |
| 10232 |
int ntups; |
| 10233 |
int i; |
| 10234 |
PQExpBuffer query; |
| 10235 |
FdwInfo *fdwinfo; |
| 10236 |
int i_tableoid; |
| 10237 |
int i_oid; |
| 10238 |
int i_fdwname; |
| 10239 |
int i_fdwowner; |
| 10240 |
int i_fdwhandler; |
| 10241 |
int i_fdwvalidator; |
| 10242 |
int i_fdwacl; |
| 10243 |
int i_acldefault; |
| 10244 |
int i_fdwoptions; |
| 10245 |
|
| 10246 |
query = createPQExpBuffer(); |
| 10247 |
|
| 10248 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, fdwname, " |
| 10249 |
"fdwowner, " |
| 10250 |
"fdwhandler::pg_catalog.regproc, " |
| 10251 |
"fdwvalidator::pg_catalog.regproc, " |
| 10252 |
"fdwacl, " |
| 10253 |
"acldefault('F', fdwowner) AS acldefault, " |
| 10254 |
"array_to_string(ARRAY(" |
| 10255 |
"SELECT quote_ident(option_name) || ' ' || " |
| 10256 |
"quote_literal(option_value) " |
| 10257 |
"FROM pg_options_to_table(fdwoptions) " |
| 10258 |
"ORDER BY option_name" |
| 10259 |
"), E',\n ') AS fdwoptions " |
| 10260 |
"FROM pg_foreign_data_wrapper"); |
| 10261 |
|
| 10262 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10263 |
|
| 10264 |
ntups = PQntuples(res); |
| 10265 |
|
| 10266 |
fdwinfo = (FdwInfo *) pg_malloc(ntups * sizeof(FdwInfo)); |
| 10267 |
|
| 10268 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 10269 |
i_oid = PQfnumber(res, "oid"); |
| 10270 |
i_fdwname = PQfnumber(res, "fdwname"); |
| 10271 |
i_fdwowner = PQfnumber(res, "fdwowner"); |
| 10272 |
i_fdwhandler = PQfnumber(res, "fdwhandler"); |
| 10273 |
i_fdwvalidator = PQfnumber(res, "fdwvalidator"); |
| 10274 |
i_fdwacl = PQfnumber(res, "fdwacl"); |
| 10275 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 10276 |
i_fdwoptions = PQfnumber(res, "fdwoptions"); |
| 10277 |
|
| 10278 |
for (i = 0; i < ntups; i++) |
| 10279 |
{ |
| 10280 |
fdwinfo[i].dobj.objType = DO_FDW; |
| 10281 |
fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 10282 |
fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 10283 |
AssignDumpId(&fdwinfo[i].dobj); |
| 10284 |
fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname)); |
| 10285 |
fdwinfo[i].dobj.namespace = NULL; |
| 10286 |
fdwinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_fdwacl)); |
| 10287 |
fdwinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 10288 |
fdwinfo[i].dacl.privtype = 0; |
| 10289 |
fdwinfo[i].dacl.initprivs = NULL; |
| 10290 |
fdwinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_fdwowner)); |
| 10291 |
fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler)); |
| 10292 |
fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator)); |
| 10293 |
fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions)); |
| 10294 |
|
| 10295 |
/* Decide whether we want to dump it */ |
| 10296 |
selectDumpableObject(&(fdwinfo[i].dobj), fout); |
| 10297 |
|
| 10298 |
/* Mark whether FDW has an ACL */ |
| 10299 |
if (!PQgetisnull(res, i, i_fdwacl)) |
| 10300 |
fdwinfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 10301 |
} |
| 10302 |
|
| 10303 |
PQclear(res); |
| 10304 |
|
| 10305 |
destroyPQExpBuffer(query); |
| 10306 |
} |
| 10307 |
|
| 10308 |
/* |
| 10309 |
* getForeignServers: |
| 10310 |
* get information about all foreign servers in the system catalogs |
| 10311 |
*/ |
| 10312 |
void |
| 10313 |
getForeignServers(Archive *fout) |
| 10314 |
{ |
| 10315 |
PGresult *res; |
| 10316 |
int ntups; |
| 10317 |
int i; |
| 10318 |
PQExpBuffer query; |
| 10319 |
ForeignServerInfo *srvinfo; |
| 10320 |
int i_tableoid; |
| 10321 |
int i_oid; |
| 10322 |
int i_srvname; |
| 10323 |
int i_srvowner; |
| 10324 |
int i_srvfdw; |
| 10325 |
int i_srvtype; |
| 10326 |
int i_srvversion; |
| 10327 |
int i_srvacl; |
| 10328 |
int i_acldefault; |
| 10329 |
int i_srvoptions; |
| 10330 |
|
| 10331 |
query = createPQExpBuffer(); |
| 10332 |
|
| 10333 |
appendPQExpBufferStr(query, "SELECT tableoid, oid, srvname, " |
| 10334 |
"srvowner, " |
| 10335 |
"srvfdw, srvtype, srvversion, srvacl, " |
| 10336 |
"acldefault('S', srvowner) AS acldefault, " |
| 10337 |
"array_to_string(ARRAY(" |
| 10338 |
"SELECT quote_ident(option_name) || ' ' || " |
| 10339 |
"quote_literal(option_value) " |
| 10340 |
"FROM pg_options_to_table(srvoptions) " |
| 10341 |
"ORDER BY option_name" |
| 10342 |
"), E',\n ') AS srvoptions " |
| 10343 |
"FROM pg_foreign_server"); |
| 10344 |
|
| 10345 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10346 |
|
| 10347 |
ntups = PQntuples(res); |
| 10348 |
|
| 10349 |
srvinfo = (ForeignServerInfo *) pg_malloc(ntups * sizeof(ForeignServerInfo)); |
| 10350 |
|
| 10351 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 10352 |
i_oid = PQfnumber(res, "oid"); |
| 10353 |
i_srvname = PQfnumber(res, "srvname"); |
| 10354 |
i_srvowner = PQfnumber(res, "srvowner"); |
| 10355 |
i_srvfdw = PQfnumber(res, "srvfdw"); |
| 10356 |
i_srvtype = PQfnumber(res, "srvtype"); |
| 10357 |
i_srvversion = PQfnumber(res, "srvversion"); |
| 10358 |
i_srvacl = PQfnumber(res, "srvacl"); |
| 10359 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 10360 |
i_srvoptions = PQfnumber(res, "srvoptions"); |
| 10361 |
|
| 10362 |
for (i = 0; i < ntups; i++) |
| 10363 |
{ |
| 10364 |
srvinfo[i].dobj.objType = DO_FOREIGN_SERVER; |
| 10365 |
srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 10366 |
srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 10367 |
AssignDumpId(&srvinfo[i].dobj); |
| 10368 |
srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname)); |
| 10369 |
srvinfo[i].dobj.namespace = NULL; |
| 10370 |
srvinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_srvacl)); |
| 10371 |
srvinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 10372 |
srvinfo[i].dacl.privtype = 0; |
| 10373 |
srvinfo[i].dacl.initprivs = NULL; |
| 10374 |
srvinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_srvowner)); |
| 10375 |
srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw)); |
| 10376 |
srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype)); |
| 10377 |
srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion)); |
| 10378 |
srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions)); |
| 10379 |
|
| 10380 |
/* Decide whether we want to dump it */ |
| 10381 |
selectDumpableObject(&(srvinfo[i].dobj), fout); |
| 10382 |
|
| 10383 |
/* Servers have user mappings */ |
| 10384 |
srvinfo[i].dobj.components |= DUMP_COMPONENT_USERMAP; |
| 10385 |
|
| 10386 |
/* Mark whether server has an ACL */ |
| 10387 |
if (!PQgetisnull(res, i, i_srvacl)) |
| 10388 |
srvinfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 10389 |
} |
| 10390 |
|
| 10391 |
PQclear(res); |
| 10392 |
|
| 10393 |
destroyPQExpBuffer(query); |
| 10394 |
} |
| 10395 |
|
| 10396 |
/* |
| 10397 |
* getDefaultACLs: |
| 10398 |
* get information about all default ACL information in the system catalogs |
| 10399 |
*/ |
| 10400 |
void |
| 10401 |
getDefaultACLs(Archive *fout) |
| 10402 |
{ |
| 10403 |
DumpOptions *dopt = fout->dopt; |
| 10404 |
DefaultACLInfo *daclinfo; |
| 10405 |
PQExpBuffer query; |
| 10406 |
PGresult *res; |
| 10407 |
int i_oid; |
| 10408 |
int i_tableoid; |
| 10409 |
int i_defaclrole; |
| 10410 |
int i_defaclnamespace; |
| 10411 |
int i_defaclobjtype; |
| 10412 |
int i_defaclacl; |
| 10413 |
int i_acldefault; |
| 10414 |
int i, |
| 10415 |
ntups; |
| 10416 |
|
| 10417 |
query = createPQExpBuffer(); |
| 10418 |
|
| 10419 |
/* |
| 10420 |
* Global entries (with defaclnamespace=0) replace the hard-wired default |
| 10421 |
* ACL for their object type. We should dump them as deltas from the |
| 10422 |
* default ACL, since that will be used as a starting point for |
| 10423 |
* interpreting the ALTER DEFAULT PRIVILEGES commands. On the other hand, |
| 10424 |
* non-global entries can only add privileges not revoke them. We must |
| 10425 |
* dump those as-is (i.e., as deltas from an empty ACL). |
| 10426 |
* |
| 10427 |
* We can use defaclobjtype as the object type for acldefault(), except |
| 10428 |
* for the case of 'S' (DEFACLOBJ_SEQUENCE) which must be converted to |
| 10429 |
* 's'. |
| 10430 |
*/ |
| 10431 |
appendPQExpBufferStr(query, |
| 10432 |
"SELECT oid, tableoid, " |
| 10433 |
"defaclrole, " |
| 10434 |
"defaclnamespace, " |
| 10435 |
"defaclobjtype, " |
| 10436 |
"defaclacl, " |
| 10437 |
"CASE WHEN defaclnamespace = 0 THEN " |
| 10438 |
"acldefault(CASE WHEN defaclobjtype = 'S' " |
| 10439 |
"THEN 's'::\"char\" ELSE defaclobjtype END, " |
| 10440 |
"defaclrole) ELSE '{}' END AS acldefault " |
| 10441 |
"FROM pg_default_acl"); |
| 10442 |
|
| 10443 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10444 |
|
| 10445 |
ntups = PQntuples(res); |
| 10446 |
|
| 10447 |
daclinfo = (DefaultACLInfo *) pg_malloc(ntups * sizeof(DefaultACLInfo)); |
| 10448 |
|
| 10449 |
i_oid = PQfnumber(res, "oid"); |
| 10450 |
i_tableoid = PQfnumber(res, "tableoid"); |
| 10451 |
i_defaclrole = PQfnumber(res, "defaclrole"); |
| 10452 |
i_defaclnamespace = PQfnumber(res, "defaclnamespace"); |
| 10453 |
i_defaclobjtype = PQfnumber(res, "defaclobjtype"); |
| 10454 |
i_defaclacl = PQfnumber(res, "defaclacl"); |
| 10455 |
i_acldefault = PQfnumber(res, "acldefault"); |
| 10456 |
|
| 10457 |
for (i = 0; i < ntups; i++) |
| 10458 |
{ |
| 10459 |
Oid nspid = atooid(PQgetvalue(res, i, i_defaclnamespace)); |
| 10460 |
|
| 10461 |
daclinfo[i].dobj.objType = DO_DEFAULT_ACL; |
| 10462 |
daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); |
| 10463 |
daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); |
| 10464 |
AssignDumpId(&daclinfo[i].dobj); |
| 10465 |
/* cheesy ... is it worth coming up with a better object name? */ |
| 10466 |
daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype)); |
| 10467 |
|
| 10468 |
if (nspid != InvalidOid) |
| 10469 |
daclinfo[i].dobj.namespace = findNamespace(nspid); |
| 10470 |
else |
| 10471 |
daclinfo[i].dobj.namespace = NULL; |
| 10472 |
|
| 10473 |
daclinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_defaclacl)); |
| 10474 |
daclinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); |
| 10475 |
daclinfo[i].dacl.privtype = 0; |
| 10476 |
daclinfo[i].dacl.initprivs = NULL; |
| 10477 |
daclinfo[i].defaclrole = getRoleName(PQgetvalue(res, i, i_defaclrole)); |
| 10478 |
daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype)); |
| 10479 |
|
| 10480 |
/* Default ACLs are ACLs, of course */ |
| 10481 |
daclinfo[i].dobj.components |= DUMP_COMPONENT_ACL; |
| 10482 |
|
| 10483 |
/* Decide whether we want to dump it */ |
| 10484 |
selectDumpableDefaultACL(&(daclinfo[i]), dopt); |
| 10485 |
} |
| 10486 |
|
| 10487 |
PQclear(res); |
| 10488 |
|
| 10489 |
destroyPQExpBuffer(query); |
| 10490 |
} |
| 10491 |
|
| 10492 |
/* |
| 10493 |
* getRoleName -- look up the name of a role, given its OID |
| 10494 |
* |
| 10495 |
* In current usage, we don't expect failures, so error out for a bad OID. |
| 10496 |
*/ |
| 10497 |
static const char * |
| 10498 |
getRoleName(const char *roleoid_str) |
| 10499 |
{ |
| 10500 |
Oid roleoid = atooid(roleoid_str); |
| 10501 |
|
| 10502 |
/* |
| 10503 |
* Do binary search to find the appropriate item. |
| 10504 |
*/ |
| 10505 |
if (nrolenames > 0) |
| 10506 |
{ |
| 10507 |
RoleNameItem *low = &rolenames[0]; |
| 10508 |
RoleNameItem *high = &rolenames[nrolenames - 1]; |
| 10509 |
|
| 10510 |
while (low <= high) |
| 10511 |
{ |
| 10512 |
RoleNameItem *middle = low + (high - low) / 2; |
| 10513 |
|
| 10514 |
if (roleoid < middle->roleoid) |
| 10515 |
high = middle - 1; |
| 10516 |
else if (roleoid > middle->roleoid) |
| 10517 |
low = middle + 1; |
| 10518 |
else |
| 10519 |
return middle->rolename; /* found a match */ |
| 10520 |
} |
| 10521 |
} |
| 10522 |
|
| 10523 |
pg_fatal("role with OID %u does not exist", roleoid); |
| 10524 |
return NULL; /* keep compiler quiet */ |
| 10525 |
} |
| 10526 |
|
| 10527 |
/* |
| 10528 |
* collectRoleNames -- |
| 10529 |
* |
| 10530 |
* Construct a table of all known roles. |
| 10531 |
* The table is sorted by OID for speed in lookup. |
| 10532 |
*/ |
| 10533 |
static void |
| 10534 |
collectRoleNames(Archive *fout) |
| 10535 |
{ |
| 10536 |
PGresult *res; |
| 10537 |
const char *query; |
| 10538 |
int i; |
| 10539 |
|
| 10540 |
query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1"; |
| 10541 |
|
| 10542 |
res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK); |
| 10543 |
|
| 10544 |
nrolenames = PQntuples(res); |
| 10545 |
|
| 10546 |
rolenames = (RoleNameItem *) pg_malloc(nrolenames * sizeof(RoleNameItem)); |
| 10547 |
|
| 10548 |
for (i = 0; i < nrolenames; i++) |
| 10549 |
{ |
| 10550 |
rolenames[i].roleoid = atooid(PQgetvalue(res, i, 0)); |
| 10551 |
rolenames[i].rolename = pg_strdup(PQgetvalue(res, i, 1)); |
| 10552 |
} |
| 10553 |
|
| 10554 |
PQclear(res); |
| 10555 |
} |
| 10556 |
|
| 10557 |
/* |
| 10558 |
* getAdditionalACLs |
| 10559 |
* |
| 10560 |
* We have now created all the DumpableObjects, and collected the ACL data |
| 10561 |
* that appears in the directly-associated catalog entries. However, there's |
| 10562 |
* more ACL-related info to collect. If any of a table's columns have ACLs, |
| 10563 |
* we must set the TableInfo's DUMP_COMPONENT_ACL components flag, as well as |
| 10564 |
* its hascolumnACLs flag (we won't store the ACLs themselves here, though). |
| 10565 |
* Also, in versions having the pg_init_privs catalog, read that and load the |
| 10566 |
* information into the relevant DumpableObjects. |
| 10567 |
*/ |
| 10568 |
static void |
| 10569 |
getAdditionalACLs(Archive *fout) |
| 10570 |
{ |
| 10571 |
PQExpBuffer query = createPQExpBuffer(); |
| 10572 |
PGresult *res; |
| 10573 |
int ntups, |
| 10574 |
i; |
| 10575 |
|
| 10576 |
/* Check for per-column ACLs */ |
| 10577 |
appendPQExpBufferStr(query, |
| 10578 |
"SELECT DISTINCT attrelid FROM pg_attribute " |
| 10579 |
"WHERE attacl IS NOT NULL"); |
| 10580 |
|
| 10581 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10582 |
|
| 10583 |
ntups = PQntuples(res); |
| 10584 |
for (i = 0; i < ntups; i++) |
| 10585 |
{ |
| 10586 |
Oid relid = atooid(PQgetvalue(res, i, 0)); |
| 10587 |
TableInfo *tblinfo; |
| 10588 |
|
| 10589 |
tblinfo = findTableByOid(relid); |
| 10590 |
/* OK to ignore tables we haven't got a DumpableObject for */ |
| 10591 |
if (tblinfo) |
| 10592 |
{ |
| 10593 |
tblinfo->dobj.components |= DUMP_COMPONENT_ACL; |
| 10594 |
tblinfo->hascolumnACLs = true; |
| 10595 |
} |
| 10596 |
} |
| 10597 |
PQclear(res); |
| 10598 |
|
| 10599 |
/* Fetch initial-privileges data */ |
| 10600 |
if (fout->remoteVersion >= 90600) |
| 10601 |
{ |
| 10602 |
printfPQExpBuffer(query, |
| 10603 |
"SELECT objoid, classoid, objsubid, privtype, initprivs " |
| 10604 |
"FROM pg_init_privs"); |
| 10605 |
|
| 10606 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10607 |
|
| 10608 |
ntups = PQntuples(res); |
| 10609 |
for (i = 0; i < ntups; i++) |
| 10610 |
{ |
| 10611 |
Oid objoid = atooid(PQgetvalue(res, i, 0)); |
| 10612 |
Oid classoid = atooid(PQgetvalue(res, i, 1)); |
| 10613 |
int objsubid = atoi(PQgetvalue(res, i, 2)); |
| 10614 |
char privtype = *(PQgetvalue(res, i, 3)); |
| 10615 |
char *initprivs = PQgetvalue(res, i, 4); |
| 10616 |
CatalogId objId; |
| 10617 |
DumpableObject *dobj; |
| 10618 |
|
| 10619 |
objId.tableoid = classoid; |
| 10620 |
objId.oid = objoid; |
| 10621 |
dobj = findObjectByCatalogId(objId); |
| 10622 |
/* OK to ignore entries we haven't got a DumpableObject for */ |
| 10623 |
if (dobj) |
| 10624 |
{ |
| 10625 |
/* Cope with sub-object initprivs */ |
| 10626 |
if (objsubid != 0) |
| 10627 |
{ |
| 10628 |
if (dobj->objType == DO_TABLE) |
| 10629 |
{ |
| 10630 |
/* For a column initprivs, set the table's ACL flags */ |
| 10631 |
dobj->components |= DUMP_COMPONENT_ACL; |
| 10632 |
((TableInfo *) dobj)->hascolumnACLs = true; |
| 10633 |
} |
| 10634 |
else |
| 10635 |
pg_log_warning("unsupported pg_init_privs entry: %u %u %d", |
| 10636 |
classoid, objoid, objsubid); |
| 10637 |
continue; |
| 10638 |
} |
| 10639 |
|
| 10640 |
/* |
| 10641 |
* We ignore any pg_init_privs.initprivs entry for the public |
| 10642 |
* schema, as explained in getNamespaces(). |
| 10643 |
*/ |
| 10644 |
if (dobj->objType == DO_NAMESPACE && |
| 10645 |
strcmp(dobj->name, "public") == 0) |
| 10646 |
continue; |
| 10647 |
|
| 10648 |
/* Else it had better be of a type we think has ACLs */ |
| 10649 |
if (dobj->objType == DO_NAMESPACE || |
| 10650 |
dobj->objType == DO_TYPE || |
| 10651 |
dobj->objType == DO_FUNC || |
| 10652 |
dobj->objType == DO_AGG || |
| 10653 |
dobj->objType == DO_TABLE || |
| 10654 |
dobj->objType == DO_PROCLANG || |
| 10655 |
dobj->objType == DO_FDW || |
| 10656 |
dobj->objType == DO_FOREIGN_SERVER) |
| 10657 |
{ |
| 10658 |
DumpableObjectWithAcl *daobj = (DumpableObjectWithAcl *) dobj; |
| 10659 |
|
| 10660 |
daobj->dacl.privtype = privtype; |
| 10661 |
daobj->dacl.initprivs = pstrdup(initprivs); |
| 10662 |
} |
| 10663 |
else |
| 10664 |
pg_log_warning("unsupported pg_init_privs entry: %u %u %d", |
| 10665 |
classoid, objoid, objsubid); |
| 10666 |
} |
| 10667 |
} |
| 10668 |
PQclear(res); |
| 10669 |
} |
| 10670 |
|
| 10671 |
destroyPQExpBuffer(query); |
| 10672 |
} |
| 10673 |
|
| 10674 |
/* |
| 10675 |
* dumpCommentExtended -- |
| 10676 |
* |
| 10677 |
* This routine is used to dump any comments associated with the |
| 10678 |
* object handed to this routine. The routine takes the object type |
| 10679 |
* and object name (ready to print, except for schema decoration), plus |
| 10680 |
* the namespace and owner of the object (for labeling the ArchiveEntry), |
| 10681 |
* plus catalog ID and subid which are the lookup key for pg_description, |
| 10682 |
* plus the dump ID for the object (for setting a dependency). |
| 10683 |
* If a matching pg_description entry is found, it is dumped. |
| 10684 |
* |
| 10685 |
* Note: in some cases, such as comments for triggers and rules, the "type" |
| 10686 |
* string really looks like, e.g., "TRIGGER name ON". This is a bit of a hack |
| 10687 |
* but it doesn't seem worth complicating the API for all callers to make |
| 10688 |
* it cleaner. |
| 10689 |
* |
| 10690 |
* Note: although this routine takes a dumpId for dependency purposes, |
| 10691 |
* that purpose is just to mark the dependency in the emitted dump file |
| 10692 |
* for possible future use by pg_restore. We do NOT use it for determining |
| 10693 |
* ordering of the comment in the dump file, because this routine is called |
| 10694 |
* after dependency sorting occurs. This routine should be called just after |
| 10695 |
* calling ArchiveEntry() for the specified object. |
| 10696 |
*/ |
| 10697 |
static void |
| 10698 |
dumpCommentExtended(Archive *fout, const char *type, |
| 10699 |
const char *name, const char *namespace, |
| 10700 |
const char *owner, CatalogId catalogId, |
| 10701 |
int subid, DumpId dumpId, |
| 10702 |
const char *initdb_comment) |
| 10703 |
{ |
| 10704 |
DumpOptions *dopt = fout->dopt; |
| 10705 |
CommentItem *comments; |
| 10706 |
int ncomments; |
| 10707 |
|
| 10708 |
/* do nothing, if --no-comments is supplied */ |
| 10709 |
if (dopt->no_comments) |
| 10710 |
return; |
| 10711 |
|
| 10712 |
/* Comments are schema not data ... except LO comments are data */ |
| 10713 |
if (strcmp(type, "LARGE OBJECT") != 0) |
| 10714 |
{ |
| 10715 |
if (!dopt->dumpSchema) |
| 10716 |
return; |
| 10717 |
} |
| 10718 |
else |
| 10719 |
{ |
| 10720 |
/* We do dump LO comments in binary-upgrade mode */ |
| 10721 |
if (!dopt->dumpData && !dopt->binary_upgrade) |
| 10722 |
return; |
| 10723 |
} |
| 10724 |
|
| 10725 |
/* Search for comments associated with catalogId, using table */ |
| 10726 |
ncomments = findComments(catalogId.tableoid, catalogId.oid, |
| 10727 |
&comments); |
| 10728 |
|
| 10729 |
/* Is there one matching the subid? */ |
| 10730 |
while (ncomments > 0) |
| 10731 |
{ |
| 10732 |
if (comments->objsubid == subid) |
| 10733 |
break; |
| 10734 |
comments++; |
| 10735 |
ncomments--; |
| 10736 |
} |
| 10737 |
|
| 10738 |
if (initdb_comment != NULL) |
| 10739 |
{ |
| 10740 |
static CommentItem empty_comment = {.descr = ""}; |
| 10741 |
|
| 10742 |
/* |
| 10743 |
* initdb creates this object with a comment. Skip dumping the |
| 10744 |
* initdb-provided comment, which would complicate matters for |
| 10745 |
* non-superuser use of pg_dump. When the DBA has removed initdb's |
| 10746 |
* comment, replicate that. |
| 10747 |
*/ |
| 10748 |
if (ncomments == 0) |
| 10749 |
{ |
| 10750 |
comments = &empty_comment; |
| 10751 |
ncomments = 1; |
| 10752 |
} |
| 10753 |
else if (strcmp(comments->descr, initdb_comment) == 0) |
| 10754 |
ncomments = 0; |
| 10755 |
} |
| 10756 |
|
| 10757 |
/* If a comment exists, build COMMENT ON statement */ |
| 10758 |
if (ncomments > 0) |
| 10759 |
{ |
| 10760 |
PQExpBuffer query = createPQExpBuffer(); |
| 10761 |
PQExpBuffer tag = createPQExpBuffer(); |
| 10762 |
|
| 10763 |
appendPQExpBuffer(query, "COMMENT ON %s ", type); |
| 10764 |
if (namespace && *namespace) |
| 10765 |
appendPQExpBuffer(query, "%s.", fmtId(namespace)); |
| 10766 |
appendPQExpBuffer(query, "%s IS ", name); |
| 10767 |
appendStringLiteralAH(query, comments->descr, fout); |
| 10768 |
appendPQExpBufferStr(query, ";\n"); |
| 10769 |
|
| 10770 |
appendPQExpBuffer(tag, "%s %s", type, name); |
| 10771 |
|
| 10772 |
/* |
| 10773 |
* We mark comments as SECTION_NONE because they really belong in the |
| 10774 |
* same section as their parent, whether that is pre-data or |
| 10775 |
* post-data. |
| 10776 |
*/ |
| 10777 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 10778 |
ARCHIVE_OPTS(.tag = tag->data, |
| 10779 |
.namespace = namespace, |
| 10780 |
.owner = owner, |
| 10781 |
.description = "COMMENT", |
| 10782 |
.section = SECTION_NONE, |
| 10783 |
.createStmt = query->data, |
| 10784 |
.deps = &dumpId, |
| 10785 |
.nDeps = 1)); |
| 10786 |
|
| 10787 |
destroyPQExpBuffer(query); |
| 10788 |
destroyPQExpBuffer(tag); |
| 10789 |
} |
| 10790 |
} |
| 10791 |
|
| 10792 |
/* |
| 10793 |
* dumpComment -- |
| 10794 |
* |
| 10795 |
* Typical simplification of the above function. |
| 10796 |
*/ |
| 10797 |
static inline void |
| 10798 |
dumpComment(Archive *fout, const char *type, |
| 10799 |
const char *name, const char *namespace, |
| 10800 |
const char *owner, CatalogId catalogId, |
| 10801 |
int subid, DumpId dumpId) |
| 10802 |
{ |
| 10803 |
dumpCommentExtended(fout, type, name, namespace, owner, |
| 10804 |
catalogId, subid, dumpId, NULL); |
| 10805 |
} |
| 10806 |
|
| 10807 |
/* |
| 10808 |
* appendNamedArgument -- |
| 10809 |
* |
| 10810 |
* Convenience routine for constructing parameters of the form: |
| 10811 |
* 'paraname', 'value'::type |
| 10812 |
*/ |
| 10813 |
static void |
| 10814 |
appendNamedArgument(PQExpBuffer out, Archive *fout, const char *argname, |
| 10815 |
const char *argtype, const char *argval) |
| 10816 |
{ |
| 10817 |
appendPQExpBufferStr(out, ",\n\t"); |
| 10818 |
|
| 10819 |
appendStringLiteralAH(out, argname, fout); |
| 10820 |
appendPQExpBufferStr(out, ", "); |
| 10821 |
|
| 10822 |
appendStringLiteralAH(out, argval, fout); |
| 10823 |
appendPQExpBuffer(out, "::%s", argtype); |
| 10824 |
} |
| 10825 |
|
| 10826 |
/* |
| 10827 |
* fetchAttributeStats -- |
| 10828 |
* |
| 10829 |
* Fetch next batch of attribute statistics for dumpRelationStats_dumper(). |
| 10830 |
*/ |
| 10831 |
static PGresult * |
| 10832 |
fetchAttributeStats(Archive *fout) |
| 10833 |
{ |
| 10834 |
ArchiveHandle *AH = (ArchiveHandle *) fout; |
| 10835 |
PQExpBuffer nspnames = createPQExpBuffer(); |
| 10836 |
PQExpBuffer relnames = createPQExpBuffer(); |
| 10837 |
int count = 0; |
| 10838 |
PGresult *res = NULL; |
| 10839 |
static TocEntry *te; |
| 10840 |
static bool restarted; |
| 10841 |
int max_rels = MAX_ATTR_STATS_RELS; |
| 10842 |
|
| 10843 |
/* |
| 10844 |
* Our query for retrieving statistics for multiple relations uses WITH |
| 10845 |
* ORDINALITY and multi-argument UNNEST(), both of which were introduced |
| 10846 |
* in v9.4. For older versions, we resort to gathering statistics for a |
| 10847 |
* single relation at a time. |
| 10848 |
*/ |
| 10849 |
if (fout->remoteVersion < 90400) |
| 10850 |
max_rels = 1; |
| 10851 |
|
| 10852 |
/* If we're just starting, set our TOC pointer. */ |
| 10853 |
if (!te) |
| 10854 |
te = AH->toc->next; |
| 10855 |
|
| 10856 |
/* |
| 10857 |
* We can't easily avoid a second TOC scan for the tar format because it |
| 10858 |
* writes restore.sql separately, which means we must execute the queries |
| 10859 |
* twice. This feels risky, but there is no known reason it should |
| 10860 |
* generate different output than the first pass. Even if it does, the |
| 10861 |
* worst-case scenario is that restore.sql might have different statistics |
| 10862 |
* data than the archive. |
| 10863 |
*/ |
| 10864 |
if (!restarted && te == AH->toc && AH->format == archTar) |
| 10865 |
{ |
| 10866 |
te = AH->toc->next; |
| 10867 |
restarted = true; |
| 10868 |
} |
| 10869 |
|
| 10870 |
appendPQExpBufferChar(nspnames, '{'); |
| 10871 |
appendPQExpBufferChar(relnames, '{'); |
| 10872 |
|
| 10873 |
/* |
| 10874 |
* Scan the TOC for the next set of relevant stats entries. We assume |
| 10875 |
* that statistics are dumped in the order they are listed in the TOC. |
| 10876 |
* This is perhaps not the sturdiest assumption, so we verify it matches |
| 10877 |
* reality in dumpRelationStats_dumper(). |
| 10878 |
*/ |
| 10879 |
for (; te != AH->toc && count < max_rels; te = te->next) |
| 10880 |
{ |
| 10881 |
if ((te->reqs & REQ_STATS) != 0 && |
| 10882 |
strcmp(te->desc, "STATISTICS DATA") == 0) |
| 10883 |
{ |
| 10884 |
appendPGArray(nspnames, te->namespace); |
| 10885 |
appendPGArray(relnames, te->tag); |
| 10886 |
count++; |
| 10887 |
} |
| 10888 |
} |
| 10889 |
|
| 10890 |
appendPQExpBufferChar(nspnames, '}'); |
| 10891 |
appendPQExpBufferChar(relnames, '}'); |
| 10892 |
|
| 10893 |
/* Execute the query for the next batch of relations. */ |
| 10894 |
if (count > 0) |
| 10895 |
{ |
| 10896 |
PQExpBuffer query = createPQExpBuffer(); |
| 10897 |
|
| 10898 |
appendPQExpBufferStr(query, "EXECUTE getAttributeStats("); |
| 10899 |
appendStringLiteralAH(query, nspnames->data, fout); |
| 10900 |
appendPQExpBufferStr(query, "::pg_catalog.name[],"); |
| 10901 |
appendStringLiteralAH(query, relnames->data, fout); |
| 10902 |
appendPQExpBufferStr(query, "::pg_catalog.name[])"); |
| 10903 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 10904 |
destroyPQExpBuffer(query); |
| 10905 |
} |
| 10906 |
|
| 10907 |
destroyPQExpBuffer(nspnames); |
| 10908 |
destroyPQExpBuffer(relnames); |
| 10909 |
return res; |
| 10910 |
} |
| 10911 |
|
| 10912 |
/* |
| 10913 |
* dumpRelationStats_dumper -- |
| 10914 |
* |
| 10915 |
* Generate command to import stats into the relation on the new database. |
| 10916 |
* This routine is called by the Archiver when it wants the statistics to be |
| 10917 |
* dumped. |
| 10918 |
*/ |
| 10919 |
static char * |
| 10920 |
dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te) |
| 10921 |
{ |
| 10922 |
const RelStatsInfo *rsinfo = (RelStatsInfo *) userArg; |
| 10923 |
static PGresult *res; |
| 10924 |
static int rownum; |
| 10925 |
PQExpBuffer query; |
| 10926 |
PQExpBufferData out_data; |
| 10927 |
PQExpBuffer out = &out_data; |
| 10928 |
int i_schemaname; |
| 10929 |
int i_tablename; |
| 10930 |
int i_attname; |
| 10931 |
int i_inherited; |
| 10932 |
int i_null_frac; |
| 10933 |
int i_avg_width; |
| 10934 |
int i_n_distinct; |
| 10935 |
int i_most_common_vals; |
| 10936 |
int i_most_common_freqs; |
| 10937 |
int i_histogram_bounds; |
| 10938 |
int i_correlation; |
| 10939 |
int i_most_common_elems; |
| 10940 |
int i_most_common_elem_freqs; |
| 10941 |
int i_elem_count_histogram; |
| 10942 |
int i_range_length_histogram; |
| 10943 |
int i_range_empty_frac; |
| 10944 |
int i_range_bounds_histogram; |
| 10945 |
static TocEntry *expected_te; |
| 10946 |
|
| 10947 |
/* |
| 10948 |
* fetchAttributeStats() assumes that the statistics are dumped in the |
| 10949 |
* order they are listed in the TOC. We verify that here for safety. |
| 10950 |
*/ |
| 10951 |
if (!expected_te) |
| 10952 |
expected_te = ((ArchiveHandle *) fout)->toc; |
| 10953 |
|
| 10954 |
expected_te = expected_te->next; |
| 10955 |
while ((expected_te->reqs & REQ_STATS) == 0 || |
| 10956 |
strcmp(expected_te->desc, "STATISTICS DATA") != 0) |
| 10957 |
expected_te = expected_te->next; |
| 10958 |
|
| 10959 |
if (te != expected_te) |
| 10960 |
pg_fatal("statistics dumped out of order (current: %d %s %s, expected: %d %s %s)", |
| 10961 |
te->dumpId, te->desc, te->tag, |
| 10962 |
expected_te->dumpId, expected_te->desc, expected_te->tag); |
| 10963 |
|
| 10964 |
query = createPQExpBuffer(); |
| 10965 |
if (!fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS]) |
| 10966 |
{ |
| 10967 |
appendPQExpBufferStr(query, |
| 10968 |
"PREPARE getAttributeStats(pg_catalog.name[], pg_catalog.name[]) AS\n" |
| 10969 |
"SELECT s.schemaname, s.tablename, s.attname, s.inherited, " |
| 10970 |
"s.null_frac, s.avg_width, s.n_distinct, " |
| 10971 |
"s.most_common_vals, s.most_common_freqs, " |
| 10972 |
"s.histogram_bounds, s.correlation, " |
| 10973 |
"s.most_common_elems, s.most_common_elem_freqs, " |
| 10974 |
"s.elem_count_histogram, "); |
| 10975 |
|
| 10976 |
if (fout->remoteVersion >= 170000) |
| 10977 |
appendPQExpBufferStr(query, |
| 10978 |
"s.range_length_histogram, " |
| 10979 |
"s.range_empty_frac, " |
| 10980 |
"s.range_bounds_histogram "); |
| 10981 |
else |
| 10982 |
appendPQExpBufferStr(query, |
| 10983 |
"NULL AS range_length_histogram," |
| 10984 |
"NULL AS range_empty_frac," |
| 10985 |
"NULL AS range_bounds_histogram "); |
| 10986 |
|
| 10987 |
/* |
| 10988 |
* The results must be in the order of the relations supplied in the |
| 10989 |
* parameters to ensure we remain in sync as we walk through the TOC. |
| 10990 |
* The redundant filter clause on s.tablename = ANY(...) seems |
| 10991 |
* sufficient to convince the planner to use |
| 10992 |
* pg_class_relname_nsp_index, which avoids a full scan of pg_stats. |
| 10993 |
* This may not work for all versions. |
| 10994 |
* |
| 10995 |
* Our query for retrieving statistics for multiple relations uses |
| 10996 |
* WITH ORDINALITY and multi-argument UNNEST(), both of which were |
| 10997 |
* introduced in v9.4. For older versions, we resort to gathering |
| 10998 |
* statistics for a single relation at a time. |
| 10999 |
*/ |
| 11000 |
if (fout->remoteVersion >= 90400) |
| 11001 |
appendPQExpBufferStr(query, |
| 11002 |
"FROM pg_catalog.pg_stats s " |
| 11003 |
"JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) " |
| 11004 |
"ON s.schemaname = u.schemaname " |
| 11005 |
"AND s.tablename = u.tablename " |
| 11006 |
"WHERE s.tablename = ANY($2) " |
| 11007 |
"ORDER BY u.ord, s.attname, s.inherited"); |
| 11008 |
else |
| 11009 |
appendPQExpBufferStr(query, |
| 11010 |
"FROM pg_catalog.pg_stats s " |
| 11011 |
"WHERE s.schemaname = $1[1] " |
| 11012 |
"AND s.tablename = $2[1] " |
| 11013 |
"ORDER BY s.attname, s.inherited"); |
| 11014 |
|
| 11015 |
ExecuteSqlStatement(fout, query->data); |
| 11016 |
|
| 11017 |
fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS] = true; |
| 11018 |
resetPQExpBuffer(query); |
| 11019 |
} |
| 11020 |
|
| 11021 |
initPQExpBuffer(out); |
| 11022 |
|
| 11023 |
/* restore relation stats */ |
| 11024 |
appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(\n"); |
| 11025 |
appendPQExpBuffer(out, "\t'version', '%d'::integer,\n", |
| 11026 |
fout->remoteVersion); |
| 11027 |
appendPQExpBufferStr(out, "\t'schemaname', "); |
| 11028 |
appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout); |
| 11029 |
appendPQExpBufferStr(out, ",\n"); |
| 11030 |
appendPQExpBufferStr(out, "\t'relname', "); |
| 11031 |
appendStringLiteralAH(out, rsinfo->dobj.name, fout); |
| 11032 |
appendPQExpBufferStr(out, ",\n"); |
| 11033 |
appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages); |
| 11034 |
|
| 11035 |
/* |
| 11036 |
* Before v14, a reltuples value of 0 was ambiguous: it could either mean |
| 11037 |
* the relation is empty, or it could mean that it hadn't yet been |
| 11038 |
* vacuumed or analyzed. (Newer versions use -1 for the latter case.) |
| 11039 |
* This ambiguity allegedly can cause the planner to choose inefficient |
| 11040 |
* plans after restoring to v18 or newer. To deal with this, let's just |
| 11041 |
* set reltuples to -1 in that case. |
| 11042 |
*/ |
| 11043 |
if (fout->remoteVersion < 140000 && strcmp("0", rsinfo->reltuples) == 0) |
| 11044 |
appendPQExpBufferStr(out, "\t'reltuples', '-1'::real,\n"); |
| 11045 |
else |
| 11046 |
appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples); |
| 11047 |
|
| 11048 |
appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer", |
| 11049 |
rsinfo->relallvisible); |
| 11050 |
|
| 11051 |
if (fout->remoteVersion >= 180000) |
| 11052 |
appendPQExpBuffer(out, ",\n\t'relallfrozen', '%d'::integer", rsinfo->relallfrozen); |
| 11053 |
|
| 11054 |
appendPQExpBufferStr(out, "\n);\n"); |
| 11055 |
|
| 11056 |
/* Fetch the next batch of attribute statistics if needed. */ |
| 11057 |
if (rownum >= PQntuples(res)) |
| 11058 |
{ |
| 11059 |
PQclear(res); |
| 11060 |
res = fetchAttributeStats(fout); |
| 11061 |
rownum = 0; |
| 11062 |
} |
| 11063 |
|
| 11064 |
i_schemaname = PQfnumber(res, "schemaname"); |
| 11065 |
i_tablename = PQfnumber(res, "tablename"); |
| 11066 |
i_attname = PQfnumber(res, "attname"); |
| 11067 |
i_inherited = PQfnumber(res, "inherited"); |
| 11068 |
i_null_frac = PQfnumber(res, "null_frac"); |
| 11069 |
i_avg_width = PQfnumber(res, "avg_width"); |
| 11070 |
i_n_distinct = PQfnumber(res, "n_distinct"); |
| 11071 |
i_most_common_vals = PQfnumber(res, "most_common_vals"); |
| 11072 |
i_most_common_freqs = PQfnumber(res, "most_common_freqs"); |
| 11073 |
i_histogram_bounds = PQfnumber(res, "histogram_bounds"); |
| 11074 |
i_correlation = PQfnumber(res, "correlation"); |
| 11075 |
i_most_common_elems = PQfnumber(res, "most_common_elems"); |
| 11076 |
i_most_common_elem_freqs = PQfnumber(res, "most_common_elem_freqs"); |
| 11077 |
i_elem_count_histogram = PQfnumber(res, "elem_count_histogram"); |
| 11078 |
i_range_length_histogram = PQfnumber(res, "range_length_histogram"); |
| 11079 |
i_range_empty_frac = PQfnumber(res, "range_empty_frac"); |
| 11080 |
i_range_bounds_histogram = PQfnumber(res, "range_bounds_histogram"); |
| 11081 |
|
| 11082 |
/* restore attribute stats */ |
| 11083 |
for (; rownum < PQntuples(res); rownum++) |
| 11084 |
{ |
| 11085 |
const char *attname; |
| 11086 |
|
| 11087 |
/* Stop if the next stat row in our cache isn't for this relation. */ |
| 11088 |
if (strcmp(te->tag, PQgetvalue(res, rownum, i_tablename)) != 0 || |
| 11089 |
strcmp(te->namespace, PQgetvalue(res, rownum, i_schemaname)) != 0) |
| 11090 |
break; |
| 11091 |
|
| 11092 |
appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_attribute_stats(\n"); |
| 11093 |
appendPQExpBuffer(out, "\t'version', '%d'::integer,\n", |
| 11094 |
fout->remoteVersion); |
| 11095 |
appendPQExpBufferStr(out, "\t'schemaname', "); |
| 11096 |
appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout); |
| 11097 |
appendPQExpBufferStr(out, ",\n\t'relname', "); |
| 11098 |
appendStringLiteralAH(out, rsinfo->dobj.name, fout); |
| 11099 |
|
| 11100 |
if (PQgetisnull(res, rownum, i_attname)) |
| 11101 |
pg_fatal("unexpected null attname"); |
| 11102 |
attname = PQgetvalue(res, rownum, i_attname); |
| 11103 |
|
| 11104 |
/* |
| 11105 |
* Indexes look up attname in indAttNames to derive attnum, all others |
| 11106 |
* use attname directly. We must specify attnum for indexes, since |
| 11107 |
* their attnames are not necessarily stable across dump/reload. |
| 11108 |
*/ |
| 11109 |
if (rsinfo->nindAttNames == 0) |
| 11110 |
{ |
| 11111 |
appendPQExpBufferStr(out, ",\n\t'attname', "); |
| 11112 |
appendStringLiteralAH(out, attname, fout); |
| 11113 |
} |
| 11114 |
else |
| 11115 |
{ |
| 11116 |
bool found = false; |
| 11117 |
|
| 11118 |
for (int i = 0; i < rsinfo->nindAttNames; i++) |
| 11119 |
{ |
| 11120 |
if (strcmp(attname, rsinfo->indAttNames[i]) == 0) |
| 11121 |
{ |
| 11122 |
appendPQExpBuffer(out, ",\n\t'attnum', '%d'::smallint", |
| 11123 |
i + 1); |
| 11124 |
found = true; |
| 11125 |
break; |
| 11126 |
} |
| 11127 |
} |
| 11128 |
|
| 11129 |
if (!found) |
| 11130 |
pg_fatal("could not find index attname \"%s\"", attname); |
| 11131 |
} |
| 11132 |
|
| 11133 |
if (!PQgetisnull(res, rownum, i_inherited)) |
| 11134 |
appendNamedArgument(out, fout, "inherited", "boolean", |
| 11135 |
PQgetvalue(res, rownum, i_inherited)); |
| 11136 |
if (!PQgetisnull(res, rownum, i_null_frac)) |
| 11137 |
appendNamedArgument(out, fout, "null_frac", "real", |
| 11138 |
PQgetvalue(res, rownum, i_null_frac)); |
| 11139 |
if (!PQgetisnull(res, rownum, i_avg_width)) |
| 11140 |
appendNamedArgument(out, fout, "avg_width", "integer", |
| 11141 |
PQgetvalue(res, rownum, i_avg_width)); |
| 11142 |
if (!PQgetisnull(res, rownum, i_n_distinct)) |
| 11143 |
appendNamedArgument(out, fout, "n_distinct", "real", |
| 11144 |
PQgetvalue(res, rownum, i_n_distinct)); |
| 11145 |
if (!PQgetisnull(res, rownum, i_most_common_vals)) |
| 11146 |
appendNamedArgument(out, fout, "most_common_vals", "text", |
| 11147 |
PQgetvalue(res, rownum, i_most_common_vals)); |
| 11148 |
if (!PQgetisnull(res, rownum, i_most_common_freqs)) |
| 11149 |
appendNamedArgument(out, fout, "most_common_freqs", "real[]", |
| 11150 |
PQgetvalue(res, rownum, i_most_common_freqs)); |
| 11151 |
if (!PQgetisnull(res, rownum, i_histogram_bounds)) |
| 11152 |
appendNamedArgument(out, fout, "histogram_bounds", "text", |
| 11153 |
PQgetvalue(res, rownum, i_histogram_bounds)); |
| 11154 |
if (!PQgetisnull(res, rownum, i_correlation)) |
| 11155 |
appendNamedArgument(out, fout, "correlation", "real", |
| 11156 |
PQgetvalue(res, rownum, i_correlation)); |
| 11157 |
if (!PQgetisnull(res, rownum, i_most_common_elems)) |
| 11158 |
appendNamedArgument(out, fout, "most_common_elems", "text", |
| 11159 |
PQgetvalue(res, rownum, i_most_common_elems)); |
| 11160 |
if (!PQgetisnull(res, rownum, i_most_common_elem_freqs)) |
| 11161 |
appendNamedArgument(out, fout, "most_common_elem_freqs", "real[]", |
| 11162 |
PQgetvalue(res, rownum, i_most_common_elem_freqs)); |
| 11163 |
if (!PQgetisnull(res, rownum, i_elem_count_histogram)) |
| 11164 |
appendNamedArgument(out, fout, "elem_count_histogram", "real[]", |
| 11165 |
PQgetvalue(res, rownum, i_elem_count_histogram)); |
| 11166 |
if (fout->remoteVersion >= 170000) |
| 11167 |
{ |
| 11168 |
if (!PQgetisnull(res, rownum, i_range_length_histogram)) |
| 11169 |
appendNamedArgument(out, fout, "range_length_histogram", "text", |
| 11170 |
PQgetvalue(res, rownum, i_range_length_histogram)); |
| 11171 |
if (!PQgetisnull(res, rownum, i_range_empty_frac)) |
| 11172 |
appendNamedArgument(out, fout, "range_empty_frac", "real", |
| 11173 |
PQgetvalue(res, rownum, i_range_empty_frac)); |
| 11174 |
if (!PQgetisnull(res, rownum, i_range_bounds_histogram)) |
| 11175 |
appendNamedArgument(out, fout, "range_bounds_histogram", "text", |
| 11176 |
PQgetvalue(res, rownum, i_range_bounds_histogram)); |
| 11177 |
} |
| 11178 |
appendPQExpBufferStr(out, "\n);\n"); |
| 11179 |
} |
| 11180 |
|
| 11181 |
destroyPQExpBuffer(query); |
| 11182 |
return out->data; |
| 11183 |
} |
| 11184 |
|
| 11185 |
/* |
| 11186 |
* dumpRelationStats -- |
| 11187 |
* |
| 11188 |
* Make an ArchiveEntry for the relation statistics. The Archiver will take |
| 11189 |
* care of gathering the statistics and generating the restore commands when |
| 11190 |
* they are needed. |
| 11191 |
*/ |
| 11192 |
static void |
| 11193 |
dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo) |
| 11194 |
{ |
| 11195 |
const DumpableObject *dobj = &rsinfo->dobj; |
| 11196 |
|
| 11197 |
/* nothing to do if we are not dumping statistics */ |
| 11198 |
if (!fout->dopt->dumpStatistics) |
| 11199 |
return; |
| 11200 |
|
| 11201 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 11202 |
ARCHIVE_OPTS(.tag = dobj->name, |
| 11203 |
.namespace = dobj->namespace->dobj.name, |
| 11204 |
.description = "STATISTICS DATA", |
| 11205 |
.section = rsinfo->section, |
| 11206 |
.defnFn = dumpRelationStats_dumper, |
| 11207 |
.defnArg = rsinfo, |
| 11208 |
.deps = dobj->dependencies, |
| 11209 |
.nDeps = dobj->nDeps)); |
| 11210 |
} |
| 11211 |
|
| 11212 |
/* |
| 11213 |
* dumpTableComment -- |
| 11214 |
* |
| 11215 |
* As above, but dump comments for both the specified table (or view) |
| 11216 |
* and its columns. |
| 11217 |
*/ |
| 11218 |
static void |
| 11219 |
dumpTableComment(Archive *fout, const TableInfo *tbinfo, |
| 11220 |
const char *reltypename) |
| 11221 |
{ |
| 11222 |
DumpOptions *dopt = fout->dopt; |
| 11223 |
CommentItem *comments; |
| 11224 |
int ncomments; |
| 11225 |
PQExpBuffer query; |
| 11226 |
PQExpBuffer tag; |
| 11227 |
|
| 11228 |
/* do nothing, if --no-comments is supplied */ |
| 11229 |
if (dopt->no_comments) |
| 11230 |
return; |
| 11231 |
|
| 11232 |
/* Comments are SCHEMA not data */ |
| 11233 |
if (!dopt->dumpSchema) |
| 11234 |
return; |
| 11235 |
|
| 11236 |
/* Search for comments associated with relation, using table */ |
| 11237 |
ncomments = findComments(tbinfo->dobj.catId.tableoid, |
| 11238 |
tbinfo->dobj.catId.oid, |
| 11239 |
&comments); |
| 11240 |
|
| 11241 |
/* If comments exist, build COMMENT ON statements */ |
| 11242 |
if (ncomments <= 0) |
| 11243 |
return; |
| 11244 |
|
| 11245 |
query = createPQExpBuffer(); |
| 11246 |
tag = createPQExpBuffer(); |
| 11247 |
|
| 11248 |
while (ncomments > 0) |
| 11249 |
{ |
| 11250 |
const char *descr = comments->descr; |
| 11251 |
int objsubid = comments->objsubid; |
| 11252 |
|
| 11253 |
if (objsubid == 0) |
| 11254 |
{ |
| 11255 |
resetPQExpBuffer(tag); |
| 11256 |
appendPQExpBuffer(tag, "%s %s", reltypename, |
| 11257 |
fmtId(tbinfo->dobj.name)); |
| 11258 |
|
| 11259 |
resetPQExpBuffer(query); |
| 11260 |
appendPQExpBuffer(query, "COMMENT ON %s %s IS ", reltypename, |
| 11261 |
fmtQualifiedDumpable(tbinfo)); |
| 11262 |
appendStringLiteralAH(query, descr, fout); |
| 11263 |
appendPQExpBufferStr(query, ";\n"); |
| 11264 |
|
| 11265 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 11266 |
ARCHIVE_OPTS(.tag = tag->data, |
| 11267 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 11268 |
.owner = tbinfo->rolname, |
| 11269 |
.description = "COMMENT", |
| 11270 |
.section = SECTION_NONE, |
| 11271 |
.createStmt = query->data, |
| 11272 |
.deps = &(tbinfo->dobj.dumpId), |
| 11273 |
.nDeps = 1)); |
| 11274 |
} |
| 11275 |
else if (objsubid > 0 && objsubid <= tbinfo->numatts) |
| 11276 |
{ |
| 11277 |
resetPQExpBuffer(tag); |
| 11278 |
appendPQExpBuffer(tag, "COLUMN %s.", |
| 11279 |
fmtId(tbinfo->dobj.name)); |
| 11280 |
appendPQExpBufferStr(tag, fmtId(tbinfo->attnames[objsubid - 1])); |
| 11281 |
|
| 11282 |
resetPQExpBuffer(query); |
| 11283 |
appendPQExpBuffer(query, "COMMENT ON COLUMN %s.", |
| 11284 |
fmtQualifiedDumpable(tbinfo)); |
| 11285 |
appendPQExpBuffer(query, "%s IS ", |
| 11286 |
fmtId(tbinfo->attnames[objsubid - 1])); |
| 11287 |
appendStringLiteralAH(query, descr, fout); |
| 11288 |
appendPQExpBufferStr(query, ";\n"); |
| 11289 |
|
| 11290 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 11291 |
ARCHIVE_OPTS(.tag = tag->data, |
| 11292 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 11293 |
.owner = tbinfo->rolname, |
| 11294 |
.description = "COMMENT", |
| 11295 |
.section = SECTION_NONE, |
| 11296 |
.createStmt = query->data, |
| 11297 |
.deps = &(tbinfo->dobj.dumpId), |
| 11298 |
.nDeps = 1)); |
| 11299 |
} |
| 11300 |
|
| 11301 |
comments++; |
| 11302 |
ncomments--; |
| 11303 |
} |
| 11304 |
|
| 11305 |
destroyPQExpBuffer(query); |
| 11306 |
destroyPQExpBuffer(tag); |
| 11307 |
} |
| 11308 |
|
| 11309 |
/* |
| 11310 |
* findComments -- |
| 11311 |
* |
| 11312 |
* Find the comment(s), if any, associated with the given object. All the |
| 11313 |
* objsubid values associated with the given classoid/objoid are found with |
| 11314 |
* one search. |
| 11315 |
*/ |
| 11316 |
static int |
| 11317 |
findComments(Oid classoid, Oid objoid, CommentItem **items) |
| 11318 |
{ |
| 11319 |
CommentItem *middle = NULL; |
| 11320 |
CommentItem *low; |
| 11321 |
CommentItem *high; |
| 11322 |
int nmatch; |
| 11323 |
|
| 11324 |
/* |
| 11325 |
* Do binary search to find some item matching the object. |
| 11326 |
*/ |
| 11327 |
low = &comments[0]; |
| 11328 |
high = &comments[ncomments - 1]; |
| 11329 |
while (low <= high) |
| 11330 |
{ |
| 11331 |
middle = low + (high - low) / 2; |
| 11332 |
|
| 11333 |
if (classoid < middle->classoid) |
| 11334 |
high = middle - 1; |
| 11335 |
else if (classoid > middle->classoid) |
| 11336 |
low = middle + 1; |
| 11337 |
else if (objoid < middle->objoid) |
| 11338 |
high = middle - 1; |
| 11339 |
else if (objoid > middle->objoid) |
| 11340 |
low = middle + 1; |
| 11341 |
else |
| 11342 |
break; /* found a match */ |
| 11343 |
} |
| 11344 |
|
| 11345 |
if (low > high) /* no matches */ |
| 11346 |
{ |
| 11347 |
*items = NULL; |
| 11348 |
return 0; |
| 11349 |
} |
| 11350 |
|
| 11351 |
/* |
| 11352 |
* Now determine how many items match the object. The search loop |
| 11353 |
* invariant still holds: only items between low and high inclusive could |
| 11354 |
* match. |
| 11355 |
*/ |
| 11356 |
nmatch = 1; |
| 11357 |
while (middle > low) |
| 11358 |
{ |
| 11359 |
if (classoid != middle[-1].classoid || |
| 11360 |
objoid != middle[-1].objoid) |
| 11361 |
break; |
| 11362 |
middle--; |
| 11363 |
nmatch++; |
| 11364 |
} |
| 11365 |
|
| 11366 |
*items = middle; |
| 11367 |
|
| 11368 |
middle += nmatch; |
| 11369 |
while (middle <= high) |
| 11370 |
{ |
| 11371 |
if (classoid != middle->classoid || |
| 11372 |
objoid != middle->objoid) |
| 11373 |
break; |
| 11374 |
middle++; |
| 11375 |
nmatch++; |
| 11376 |
} |
| 11377 |
|
| 11378 |
return nmatch; |
| 11379 |
} |
| 11380 |
|
| 11381 |
/* |
| 11382 |
* collectComments -- |
| 11383 |
* |
| 11384 |
* Construct a table of all comments available for database objects; |
| 11385 |
* also set the has-comment component flag for each relevant object. |
| 11386 |
* |
| 11387 |
* We used to do per-object queries for the comments, but it's much faster |
| 11388 |
* to pull them all over at once, and on most databases the memory cost |
| 11389 |
* isn't high. |
| 11390 |
* |
| 11391 |
* The table is sorted by classoid/objid/objsubid for speed in lookup. |
| 11392 |
*/ |
| 11393 |
static void |
| 11394 |
collectComments(Archive *fout) |
| 11395 |
{ |
| 11396 |
PGresult *res; |
| 11397 |
PQExpBuffer query; |
| 11398 |
int i_description; |
| 11399 |
int i_classoid; |
| 11400 |
int i_objoid; |
| 11401 |
int i_objsubid; |
| 11402 |
int ntups; |
| 11403 |
int i; |
| 11404 |
DumpableObject *dobj; |
| 11405 |
|
| 11406 |
query = createPQExpBuffer(); |
| 11407 |
|
| 11408 |
appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid " |
| 11409 |
"FROM pg_catalog.pg_description " |
| 11410 |
"ORDER BY classoid, objoid, objsubid"); |
| 11411 |
|
| 11412 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 11413 |
|
| 11414 |
/* Construct lookup table containing OIDs in numeric form */ |
| 11415 |
|
| 11416 |
i_description = PQfnumber(res, "description"); |
| 11417 |
i_classoid = PQfnumber(res, "classoid"); |
| 11418 |
i_objoid = PQfnumber(res, "objoid"); |
| 11419 |
i_objsubid = PQfnumber(res, "objsubid"); |
| 11420 |
|
| 11421 |
ntups = PQntuples(res); |
| 11422 |
|
| 11423 |
comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem)); |
| 11424 |
ncomments = 0; |
| 11425 |
dobj = NULL; |
| 11426 |
|
| 11427 |
for (i = 0; i < ntups; i++) |
| 11428 |
{ |
| 11429 |
CatalogId objId; |
| 11430 |
int subid; |
| 11431 |
|
| 11432 |
objId.tableoid = atooid(PQgetvalue(res, i, i_classoid)); |
| 11433 |
objId.oid = atooid(PQgetvalue(res, i, i_objoid)); |
| 11434 |
subid = atoi(PQgetvalue(res, i, i_objsubid)); |
| 11435 |
|
| 11436 |
/* We needn't remember comments that don't match any dumpable object */ |
| 11437 |
if (dobj == NULL || |
| 11438 |
dobj->catId.tableoid != objId.tableoid || |
| 11439 |
dobj->catId.oid != objId.oid) |
| 11440 |
dobj = findObjectByCatalogId(objId); |
| 11441 |
if (dobj == NULL) |
| 11442 |
continue; |
| 11443 |
|
| 11444 |
/* |
| 11445 |
* Comments on columns of composite types are linked to the type's |
| 11446 |
* pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag |
| 11447 |
* in the type's own DumpableObject. |
| 11448 |
*/ |
| 11449 |
if (subid != 0 && dobj->objType == DO_TABLE && |
| 11450 |
((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE) |
| 11451 |
{ |
| 11452 |
TypeInfo *cTypeInfo; |
| 11453 |
|
| 11454 |
cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype); |
| 11455 |
if (cTypeInfo) |
| 11456 |
cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT; |
| 11457 |
} |
| 11458 |
else |
| 11459 |
dobj->components |= DUMP_COMPONENT_COMMENT; |
| 11460 |
|
| 11461 |
comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description)); |
| 11462 |
comments[ncomments].classoid = objId.tableoid; |
| 11463 |
comments[ncomments].objoid = objId.oid; |
| 11464 |
comments[ncomments].objsubid = subid; |
| 11465 |
ncomments++; |
| 11466 |
} |
| 11467 |
|
| 11468 |
PQclear(res); |
| 11469 |
destroyPQExpBuffer(query); |
| 11470 |
} |
| 11471 |
|
| 11472 |
/* |
| 11473 |
* dumpDumpableObject |
| 11474 |
* |
| 11475 |
* This routine and its subsidiaries are responsible for creating |
| 11476 |
* ArchiveEntries (TOC objects) for each object to be dumped. |
| 11477 |
*/ |
| 11478 |
static void |
| 11479 |
dumpDumpableObject(Archive *fout, DumpableObject *dobj) |
| 11480 |
{ |
| 11481 |
/* |
| 11482 |
* Clear any dump-request bits for components that don't exist for this |
| 11483 |
* object. (This makes it safe to initially use DUMP_COMPONENT_ALL as the |
| 11484 |
* request for every kind of object.) |
| 11485 |
*/ |
| 11486 |
dobj->dump &= dobj->components; |
| 11487 |
|
| 11488 |
/* Now, short-circuit if there's nothing to be done here. */ |
| 11489 |
if (dobj->dump == 0) |
| 11490 |
return; |
| 11491 |
|
| 11492 |
switch (dobj->objType) |
| 11493 |
{ |
| 11494 |
case DO_NAMESPACE: |
| 11495 |
dumpNamespace(fout, (const NamespaceInfo *) dobj); |
| 11496 |
break; |
| 11497 |
case DO_EXTENSION: |
| 11498 |
dumpExtension(fout, (const ExtensionInfo *) dobj); |
| 11499 |
break; |
| 11500 |
case DO_TYPE: |
| 11501 |
dumpType(fout, (const TypeInfo *) dobj); |
| 11502 |
break; |
| 11503 |
case DO_SHELL_TYPE: |
| 11504 |
dumpShellType(fout, (const ShellTypeInfo *) dobj); |
| 11505 |
break; |
| 11506 |
case DO_FUNC: |
| 11507 |
dumpFunc(fout, (const FuncInfo *) dobj); |
| 11508 |
break; |
| 11509 |
case DO_AGG: |
| 11510 |
dumpAgg(fout, (const AggInfo *) dobj); |
| 11511 |
break; |
| 11512 |
case DO_OPERATOR: |
| 11513 |
dumpOpr(fout, (const OprInfo *) dobj); |
| 11514 |
break; |
| 11515 |
case DO_ACCESS_METHOD: |
| 11516 |
dumpAccessMethod(fout, (const AccessMethodInfo *) dobj); |
| 11517 |
break; |
| 11518 |
case DO_OPCLASS: |
| 11519 |
dumpOpclass(fout, (const OpclassInfo *) dobj); |
| 11520 |
break; |
| 11521 |
case DO_OPFAMILY: |
| 11522 |
dumpOpfamily(fout, (const OpfamilyInfo *) dobj); |
| 11523 |
break; |
| 11524 |
case DO_COLLATION: |
| 11525 |
dumpCollation(fout, (const CollInfo *) dobj); |
| 11526 |
break; |
| 11527 |
case DO_CONVERSION: |
| 11528 |
dumpConversion(fout, (const ConvInfo *) dobj); |
| 11529 |
break; |
| 11530 |
case DO_TABLE: |
| 11531 |
dumpTable(fout, (const TableInfo *) dobj); |
| 11532 |
break; |
| 11533 |
case DO_TABLE_ATTACH: |
| 11534 |
dumpTableAttach(fout, (const TableAttachInfo *) dobj); |
| 11535 |
break; |
| 11536 |
case DO_ATTRDEF: |
| 11537 |
dumpAttrDef(fout, (const AttrDefInfo *) dobj); |
| 11538 |
break; |
| 11539 |
case DO_INDEX: |
| 11540 |
dumpIndex(fout, (const IndxInfo *) dobj); |
| 11541 |
break; |
| 11542 |
case DO_INDEX_ATTACH: |
| 11543 |
dumpIndexAttach(fout, (const IndexAttachInfo *) dobj); |
| 11544 |
break; |
| 11545 |
case DO_STATSEXT: |
| 11546 |
dumpStatisticsExt(fout, (const StatsExtInfo *) dobj); |
| 11547 |
break; |
| 11548 |
case DO_REFRESH_MATVIEW: |
| 11549 |
refreshMatViewData(fout, (const TableDataInfo *) dobj); |
| 11550 |
break; |
| 11551 |
case DO_RULE: |
| 11552 |
dumpRule(fout, (const RuleInfo *) dobj); |
| 11553 |
break; |
| 11554 |
case DO_TRIGGER: |
| 11555 |
dumpTrigger(fout, (const TriggerInfo *) dobj); |
| 11556 |
break; |
| 11557 |
case DO_EVENT_TRIGGER: |
| 11558 |
dumpEventTrigger(fout, (const EventTriggerInfo *) dobj); |
| 11559 |
break; |
| 11560 |
case DO_CONSTRAINT: |
| 11561 |
dumpConstraint(fout, (const ConstraintInfo *) dobj); |
| 11562 |
break; |
| 11563 |
case DO_FK_CONSTRAINT: |
| 11564 |
dumpConstraint(fout, (const ConstraintInfo *) dobj); |
| 11565 |
break; |
| 11566 |
case DO_PROCLANG: |
| 11567 |
dumpProcLang(fout, (const ProcLangInfo *) dobj); |
| 11568 |
break; |
| 11569 |
case DO_CAST: |
| 11570 |
dumpCast(fout, (const CastInfo *) dobj); |
| 11571 |
break; |
| 11572 |
case DO_TRANSFORM: |
| 11573 |
dumpTransform(fout, (const TransformInfo *) dobj); |
| 11574 |
break; |
| 11575 |
case DO_SEQUENCE_SET: |
| 11576 |
dumpSequenceData(fout, (const TableDataInfo *) dobj); |
| 11577 |
break; |
| 11578 |
case DO_TABLE_DATA: |
| 11579 |
dumpTableData(fout, (const TableDataInfo *) dobj); |
| 11580 |
break; |
| 11581 |
case DO_DUMMY_TYPE: |
| 11582 |
/* table rowtypes and array types are never dumped separately */ |
| 11583 |
break; |
| 11584 |
case DO_TSPARSER: |
| 11585 |
dumpTSParser(fout, (const TSParserInfo *) dobj); |
| 11586 |
break; |
| 11587 |
case DO_TSDICT: |
| 11588 |
dumpTSDictionary(fout, (const TSDictInfo *) dobj); |
| 11589 |
break; |
| 11590 |
case DO_TSTEMPLATE: |
| 11591 |
dumpTSTemplate(fout, (const TSTemplateInfo *) dobj); |
| 11592 |
break; |
| 11593 |
case DO_TSCONFIG: |
| 11594 |
dumpTSConfig(fout, (const TSConfigInfo *) dobj); |
| 11595 |
break; |
| 11596 |
case DO_FDW: |
| 11597 |
dumpForeignDataWrapper(fout, (const FdwInfo *) dobj); |
| 11598 |
break; |
| 11599 |
case DO_FOREIGN_SERVER: |
| 11600 |
dumpForeignServer(fout, (const ForeignServerInfo *) dobj); |
| 11601 |
break; |
| 11602 |
case DO_DEFAULT_ACL: |
| 11603 |
dumpDefaultACL(fout, (const DefaultACLInfo *) dobj); |
| 11604 |
break; |
| 11605 |
case DO_LARGE_OBJECT: |
| 11606 |
dumpLO(fout, (const LoInfo *) dobj); |
| 11607 |
break; |
| 11608 |
case DO_LARGE_OBJECT_DATA: |
| 11609 |
if (dobj->dump & DUMP_COMPONENT_DATA) |
| 11610 |
{ |
| 11611 |
LoInfo *loinfo; |
| 11612 |
TocEntry *te; |
| 11613 |
|
| 11614 |
loinfo = (LoInfo *) findObjectByDumpId(dobj->dependencies[0]); |
| 11615 |
if (loinfo == NULL) |
| 11616 |
pg_fatal("missing metadata for large objects \"%s\"", |
| 11617 |
dobj->name); |
| 11618 |
|
| 11619 |
te = ArchiveEntry(fout, dobj->catId, dobj->dumpId, |
| 11620 |
ARCHIVE_OPTS(.tag = dobj->name, |
| 11621 |
.owner = loinfo->rolname, |
| 11622 |
.description = "BLOBS", |
| 11623 |
.section = SECTION_DATA, |
| 11624 |
.deps = dobj->dependencies, |
| 11625 |
.nDeps = dobj->nDeps, |
| 11626 |
.dumpFn = dumpLOs, |
| 11627 |
.dumpArg = loinfo)); |
| 11628 |
|
| 11629 |
/* |
| 11630 |
* Set the TocEntry's dataLength in case we are doing a |
| 11631 |
* parallel dump and want to order dump jobs by table size. |
| 11632 |
* (We need some size estimate for every TocEntry with a |
| 11633 |
* DataDumper function.) We don't currently have any cheap |
| 11634 |
* way to estimate the size of LOs, but fortunately it doesn't |
| 11635 |
* matter too much as long as we get large batches of LOs |
| 11636 |
* processed reasonably early. Assume 8K per blob. |
| 11637 |
*/ |
| 11638 |
te->dataLength = loinfo->numlos * (pgoff_t) 8192; |
| 11639 |
} |
| 11640 |
break; |
| 11641 |
case DO_POLICY: |
| 11642 |
dumpPolicy(fout, (const PolicyInfo *) dobj); |
| 11643 |
break; |
| 11644 |
case DO_PUBLICATION: |
| 11645 |
dumpPublication(fout, (const PublicationInfo *) dobj); |
| 11646 |
break; |
| 11647 |
case DO_PUBLICATION_REL: |
| 11648 |
dumpPublicationTable(fout, (const PublicationRelInfo *) dobj); |
| 11649 |
break; |
| 11650 |
case DO_PUBLICATION_TABLE_IN_SCHEMA: |
| 11651 |
dumpPublicationNamespace(fout, |
| 11652 |
(const PublicationSchemaInfo *) dobj); |
| 11653 |
break; |
| 11654 |
case DO_SUBSCRIPTION: |
| 11655 |
dumpSubscription(fout, (const SubscriptionInfo *) dobj); |
| 11656 |
break; |
| 11657 |
case DO_SUBSCRIPTION_REL: |
| 11658 |
dumpSubscriptionTable(fout, (const SubRelInfo *) dobj); |
| 11659 |
break; |
| 11660 |
case DO_REL_STATS: |
| 11661 |
dumpRelationStats(fout, (const RelStatsInfo *) dobj); |
| 11662 |
break; |
| 11663 |
case DO_PRE_DATA_BOUNDARY: |
| 11664 |
case DO_POST_DATA_BOUNDARY: |
| 11665 |
/* never dumped, nothing to do */ |
| 11666 |
break; |
| 11667 |
} |
| 11668 |
} |
| 11669 |
|
| 11670 |
/* |
| 11671 |
* dumpNamespace |
| 11672 |
* writes out to fout the queries to recreate a user-defined namespace |
| 11673 |
*/ |
| 11674 |
static void |
| 11675 |
dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo) |
| 11676 |
{ |
| 11677 |
DumpOptions *dopt = fout->dopt; |
| 11678 |
PQExpBuffer q; |
| 11679 |
PQExpBuffer delq; |
| 11680 |
char *qnspname; |
| 11681 |
|
| 11682 |
/* Do nothing if not dumping schema */ |
| 11683 |
if (!dopt->dumpSchema) |
| 11684 |
return; |
| 11685 |
|
| 11686 |
q = createPQExpBuffer(); |
| 11687 |
delq = createPQExpBuffer(); |
| 11688 |
|
| 11689 |
qnspname = pg_strdup(fmtId(nspinfo->dobj.name)); |
| 11690 |
|
| 11691 |
if (nspinfo->create) |
| 11692 |
{ |
| 11693 |
appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname); |
| 11694 |
appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname); |
| 11695 |
} |
| 11696 |
else |
| 11697 |
{ |
| 11698 |
/* see selectDumpableNamespace() */ |
| 11699 |
appendPQExpBufferStr(delq, |
| 11700 |
"-- *not* dropping schema, since initdb creates it\n"); |
| 11701 |
appendPQExpBufferStr(q, |
| 11702 |
"-- *not* creating schema, since initdb creates it\n"); |
| 11703 |
} |
| 11704 |
|
| 11705 |
if (dopt->binary_upgrade) |
| 11706 |
binary_upgrade_extension_member(q, &nspinfo->dobj, |
| 11707 |
"SCHEMA", qnspname, NULL); |
| 11708 |
|
| 11709 |
if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 11710 |
ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId, |
| 11711 |
ARCHIVE_OPTS(.tag = nspinfo->dobj.name, |
| 11712 |
.owner = nspinfo->rolname, |
| 11713 |
.description = "SCHEMA", |
| 11714 |
.section = SECTION_PRE_DATA, |
| 11715 |
.createStmt = q->data, |
| 11716 |
.dropStmt = delq->data)); |
| 11717 |
|
| 11718 |
/* Dump Schema Comments and Security Labels */ |
| 11719 |
if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 11720 |
{ |
| 11721 |
const char *initdb_comment = NULL; |
| 11722 |
|
| 11723 |
if (!nspinfo->create && strcmp(qnspname, "public") == 0) |
| 11724 |
initdb_comment = "standard public schema"; |
| 11725 |
dumpCommentExtended(fout, "SCHEMA", qnspname, |
| 11726 |
NULL, nspinfo->rolname, |
| 11727 |
nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId, |
| 11728 |
initdb_comment); |
| 11729 |
} |
| 11730 |
|
| 11731 |
if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 11732 |
dumpSecLabel(fout, "SCHEMA", qnspname, |
| 11733 |
NULL, nspinfo->rolname, |
| 11734 |
nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId); |
| 11735 |
|
| 11736 |
if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 11737 |
dumpACL(fout, nspinfo->dobj.dumpId, InvalidDumpId, "SCHEMA", |
| 11738 |
qnspname, NULL, NULL, |
| 11739 |
NULL, nspinfo->rolname, &nspinfo->dacl); |
| 11740 |
|
| 11741 |
free(qnspname); |
| 11742 |
|
| 11743 |
destroyPQExpBuffer(q); |
| 11744 |
destroyPQExpBuffer(delq); |
| 11745 |
} |
| 11746 |
|
| 11747 |
/* |
| 11748 |
* dumpExtension |
| 11749 |
* writes out to fout the queries to recreate an extension |
| 11750 |
*/ |
| 11751 |
static void |
| 11752 |
dumpExtension(Archive *fout, const ExtensionInfo *extinfo) |
| 11753 |
{ |
| 11754 |
DumpOptions *dopt = fout->dopt; |
| 11755 |
PQExpBuffer q; |
| 11756 |
PQExpBuffer delq; |
| 11757 |
char *qextname; |
| 11758 |
|
| 11759 |
/* Do nothing if not dumping schema */ |
| 11760 |
if (!dopt->dumpSchema) |
| 11761 |
return; |
| 11762 |
|
| 11763 |
q = createPQExpBuffer(); |
| 11764 |
delq = createPQExpBuffer(); |
| 11765 |
|
| 11766 |
qextname = pg_strdup(fmtId(extinfo->dobj.name)); |
| 11767 |
|
| 11768 |
appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname); |
| 11769 |
|
| 11770 |
if (!dopt->binary_upgrade) |
| 11771 |
{ |
| 11772 |
/* |
| 11773 |
* In a regular dump, we simply create the extension, intentionally |
| 11774 |
* not specifying a version, so that the destination installation's |
| 11775 |
* default version is used. |
| 11776 |
* |
| 11777 |
* Use of IF NOT EXISTS here is unlike our behavior for other object |
| 11778 |
* types; but there are various scenarios in which it's convenient to |
| 11779 |
* manually create the desired extension before restoring, so we |
| 11780 |
* prefer to allow it to exist already. |
| 11781 |
*/ |
| 11782 |
appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n", |
| 11783 |
qextname, fmtId(extinfo->namespace)); |
| 11784 |
} |
| 11785 |
else |
| 11786 |
{ |
| 11787 |
/* |
| 11788 |
* In binary-upgrade mode, it's critical to reproduce the state of the |
| 11789 |
* database exactly, so our procedure is to create an empty extension, |
| 11790 |
* restore all the contained objects normally, and add them to the |
| 11791 |
* extension one by one. This function performs just the first of |
| 11792 |
* those steps. binary_upgrade_extension_member() takes care of |
| 11793 |
* adding member objects as they're created. |
| 11794 |
*/ |
| 11795 |
int i; |
| 11796 |
int n; |
| 11797 |
|
| 11798 |
appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n"); |
| 11799 |
|
| 11800 |
/* |
| 11801 |
* We unconditionally create the extension, so we must drop it if it |
| 11802 |
* exists. This could happen if the user deleted 'plpgsql' and then |
| 11803 |
* readded it, causing its oid to be greater than g_last_builtin_oid. |
| 11804 |
*/ |
| 11805 |
appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname); |
| 11806 |
|
| 11807 |
appendPQExpBufferStr(q, |
| 11808 |
"SELECT pg_catalog.binary_upgrade_create_empty_extension("); |
| 11809 |
appendStringLiteralAH(q, extinfo->dobj.name, fout); |
| 11810 |
appendPQExpBufferStr(q, ", "); |
| 11811 |
appendStringLiteralAH(q, extinfo->namespace, fout); |
| 11812 |
appendPQExpBufferStr(q, ", "); |
| 11813 |
appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false"); |
| 11814 |
appendStringLiteralAH(q, extinfo->extversion, fout); |
| 11815 |
appendPQExpBufferStr(q, ", "); |
| 11816 |
|
| 11817 |
/* |
| 11818 |
* Note that we're pushing extconfig (an OID array) back into |
| 11819 |
* pg_extension exactly as-is. This is OK because pg_class OIDs are |
| 11820 |
* preserved in binary upgrade. |
| 11821 |
*/ |
| 11822 |
if (strlen(extinfo->extconfig) > 2) |
| 11823 |
appendStringLiteralAH(q, extinfo->extconfig, fout); |
| 11824 |
else |
| 11825 |
appendPQExpBufferStr(q, "NULL"); |
| 11826 |
appendPQExpBufferStr(q, ", "); |
| 11827 |
if (strlen(extinfo->extcondition) > 2) |
| 11828 |
appendStringLiteralAH(q, extinfo->extcondition, fout); |
| 11829 |
else |
| 11830 |
appendPQExpBufferStr(q, "NULL"); |
| 11831 |
appendPQExpBufferStr(q, ", "); |
| 11832 |
appendPQExpBufferStr(q, "ARRAY["); |
| 11833 |
n = 0; |
| 11834 |
for (i = 0; i < extinfo->dobj.nDeps; i++) |
| 11835 |
{ |
| 11836 |
DumpableObject *extobj; |
| 11837 |
|
| 11838 |
extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]); |
| 11839 |
if (extobj && extobj->objType == DO_EXTENSION) |
| 11840 |
{ |
| 11841 |
if (n++ > 0) |
| 11842 |
appendPQExpBufferChar(q, ','); |
| 11843 |
appendStringLiteralAH(q, extobj->name, fout); |
| 11844 |
} |
| 11845 |
} |
| 11846 |
appendPQExpBufferStr(q, "]::pg_catalog.text[]"); |
| 11847 |
appendPQExpBufferStr(q, ");\n"); |
| 11848 |
} |
| 11849 |
|
| 11850 |
if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 11851 |
ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId, |
| 11852 |
ARCHIVE_OPTS(.tag = extinfo->dobj.name, |
| 11853 |
.description = "EXTENSION", |
| 11854 |
.section = SECTION_PRE_DATA, |
| 11855 |
.createStmt = q->data, |
| 11856 |
.dropStmt = delq->data)); |
| 11857 |
|
| 11858 |
/* Dump Extension Comments and Security Labels */ |
| 11859 |
if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 11860 |
dumpComment(fout, "EXTENSION", qextname, |
| 11861 |
NULL, "", |
| 11862 |
extinfo->dobj.catId, 0, extinfo->dobj.dumpId); |
| 11863 |
|
| 11864 |
if (extinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 11865 |
dumpSecLabel(fout, "EXTENSION", qextname, |
| 11866 |
NULL, "", |
| 11867 |
extinfo->dobj.catId, 0, extinfo->dobj.dumpId); |
| 11868 |
|
| 11869 |
free(qextname); |
| 11870 |
|
| 11871 |
destroyPQExpBuffer(q); |
| 11872 |
destroyPQExpBuffer(delq); |
| 11873 |
} |
| 11874 |
|
| 11875 |
/* |
| 11876 |
* dumpType |
| 11877 |
* writes out to fout the queries to recreate a user-defined type |
| 11878 |
*/ |
| 11879 |
static void |
| 11880 |
dumpType(Archive *fout, const TypeInfo *tyinfo) |
| 11881 |
{ |
| 11882 |
DumpOptions *dopt = fout->dopt; |
| 11883 |
|
| 11884 |
/* Do nothing if not dumping schema */ |
| 11885 |
if (!dopt->dumpSchema) |
| 11886 |
return; |
| 11887 |
|
| 11888 |
/* Dump out in proper style */ |
| 11889 |
if (tyinfo->typtype == TYPTYPE_BASE) |
| 11890 |
dumpBaseType(fout, tyinfo); |
| 11891 |
else if (tyinfo->typtype == TYPTYPE_DOMAIN) |
| 11892 |
dumpDomain(fout, tyinfo); |
| 11893 |
else if (tyinfo->typtype == TYPTYPE_COMPOSITE) |
| 11894 |
dumpCompositeType(fout, tyinfo); |
| 11895 |
else if (tyinfo->typtype == TYPTYPE_ENUM) |
| 11896 |
dumpEnumType(fout, tyinfo); |
| 11897 |
else if (tyinfo->typtype == TYPTYPE_RANGE) |
| 11898 |
dumpRangeType(fout, tyinfo); |
| 11899 |
else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined) |
| 11900 |
dumpUndefinedType(fout, tyinfo); |
| 11901 |
else |
| 11902 |
pg_log_warning("typtype of data type \"%s\" appears to be invalid", |
| 11903 |
tyinfo->dobj.name); |
| 11904 |
} |
| 11905 |
|
| 11906 |
/* |
| 11907 |
* dumpEnumType |
| 11908 |
* writes out to fout the queries to recreate a user-defined enum type |
| 11909 |
*/ |
| 11910 |
static void |
| 11911 |
dumpEnumType(Archive *fout, const TypeInfo *tyinfo) |
| 11912 |
{ |
| 11913 |
DumpOptions *dopt = fout->dopt; |
| 11914 |
PQExpBuffer q = createPQExpBuffer(); |
| 11915 |
PQExpBuffer delq = createPQExpBuffer(); |
| 11916 |
PQExpBuffer query = createPQExpBuffer(); |
| 11917 |
PGresult *res; |
| 11918 |
int num, |
| 11919 |
i; |
| 11920 |
Oid enum_oid; |
| 11921 |
char *qtypname; |
| 11922 |
char *qualtypname; |
| 11923 |
char *label; |
| 11924 |
int i_enumlabel; |
| 11925 |
int i_oid; |
| 11926 |
|
| 11927 |
if (!fout->is_prepared[PREPQUERY_DUMPENUMTYPE]) |
| 11928 |
{ |
| 11929 |
/* Set up query for enum-specific details */ |
| 11930 |
appendPQExpBufferStr(query, |
| 11931 |
"PREPARE dumpEnumType(pg_catalog.oid) AS\n" |
| 11932 |
"SELECT oid, enumlabel " |
| 11933 |
"FROM pg_catalog.pg_enum " |
| 11934 |
"WHERE enumtypid = $1 " |
| 11935 |
"ORDER BY enumsortorder"); |
| 11936 |
|
| 11937 |
ExecuteSqlStatement(fout, query->data); |
| 11938 |
|
| 11939 |
fout->is_prepared[PREPQUERY_DUMPENUMTYPE] = true; |
| 11940 |
} |
| 11941 |
|
| 11942 |
printfPQExpBuffer(query, |
| 11943 |
"EXECUTE dumpEnumType('%u')", |
| 11944 |
tyinfo->dobj.catId.oid); |
| 11945 |
|
| 11946 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 11947 |
|
| 11948 |
num = PQntuples(res); |
| 11949 |
|
| 11950 |
qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); |
| 11951 |
qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); |
| 11952 |
|
| 11953 |
/* |
| 11954 |
* CASCADE shouldn't be required here as for normal types since the I/O |
| 11955 |
* functions are generic and do not get dropped. |
| 11956 |
*/ |
| 11957 |
appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); |
| 11958 |
|
| 11959 |
if (dopt->binary_upgrade) |
| 11960 |
binary_upgrade_set_type_oids_by_type_oid(fout, q, |
| 11961 |
tyinfo->dobj.catId.oid, |
| 11962 |
false, false); |
| 11963 |
|
| 11964 |
appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (", |
| 11965 |
qualtypname); |
| 11966 |
|
| 11967 |
if (!dopt->binary_upgrade) |
| 11968 |
{ |
| 11969 |
i_enumlabel = PQfnumber(res, "enumlabel"); |
| 11970 |
|
| 11971 |
/* Labels with server-assigned oids */ |
| 11972 |
for (i = 0; i < num; i++) |
| 11973 |
{ |
| 11974 |
label = PQgetvalue(res, i, i_enumlabel); |
| 11975 |
if (i > 0) |
| 11976 |
appendPQExpBufferChar(q, ','); |
| 11977 |
appendPQExpBufferStr(q, "\n "); |
| 11978 |
appendStringLiteralAH(q, label, fout); |
| 11979 |
} |
| 11980 |
} |
| 11981 |
|
| 11982 |
appendPQExpBufferStr(q, "\n);\n"); |
| 11983 |
|
| 11984 |
if (dopt->binary_upgrade) |
| 11985 |
{ |
| 11986 |
i_oid = PQfnumber(res, "oid"); |
| 11987 |
i_enumlabel = PQfnumber(res, "enumlabel"); |
| 11988 |
|
| 11989 |
/* Labels with dump-assigned (preserved) oids */ |
| 11990 |
for (i = 0; i < num; i++) |
| 11991 |
{ |
| 11992 |
enum_oid = atooid(PQgetvalue(res, i, i_oid)); |
| 11993 |
label = PQgetvalue(res, i, i_enumlabel); |
| 11994 |
|
| 11995 |
if (i == 0) |
| 11996 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n"); |
| 11997 |
appendPQExpBuffer(q, |
| 11998 |
"SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n", |
| 11999 |
enum_oid); |
| 12000 |
appendPQExpBuffer(q, "ALTER TYPE %s ADD VALUE ", qualtypname); |
| 12001 |
appendStringLiteralAH(q, label, fout); |
| 12002 |
appendPQExpBufferStr(q, ";\n\n"); |
| 12003 |
} |
| 12004 |
} |
| 12005 |
|
| 12006 |
if (dopt->binary_upgrade) |
| 12007 |
binary_upgrade_extension_member(q, &tyinfo->dobj, |
| 12008 |
"TYPE", qtypname, |
| 12009 |
tyinfo->dobj.namespace->dobj.name); |
| 12010 |
|
| 12011 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 12012 |
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, |
| 12013 |
ARCHIVE_OPTS(.tag = tyinfo->dobj.name, |
| 12014 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 12015 |
.owner = tyinfo->rolname, |
| 12016 |
.description = "TYPE", |
| 12017 |
.section = SECTION_PRE_DATA, |
| 12018 |
.createStmt = q->data, |
| 12019 |
.dropStmt = delq->data)); |
| 12020 |
|
| 12021 |
/* Dump Type Comments and Security Labels */ |
| 12022 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12023 |
dumpComment(fout, "TYPE", qtypname, |
| 12024 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12025 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12026 |
|
| 12027 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 12028 |
dumpSecLabel(fout, "TYPE", qtypname, |
| 12029 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12030 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12031 |
|
| 12032 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 12033 |
dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", |
| 12034 |
qtypname, NULL, |
| 12035 |
tyinfo->dobj.namespace->dobj.name, |
| 12036 |
NULL, tyinfo->rolname, &tyinfo->dacl); |
| 12037 |
|
| 12038 |
PQclear(res); |
| 12039 |
destroyPQExpBuffer(q); |
| 12040 |
destroyPQExpBuffer(delq); |
| 12041 |
destroyPQExpBuffer(query); |
| 12042 |
free(qtypname); |
| 12043 |
free(qualtypname); |
| 12044 |
} |
| 12045 |
|
| 12046 |
/* |
| 12047 |
* dumpRangeType |
| 12048 |
* writes out to fout the queries to recreate a user-defined range type |
| 12049 |
*/ |
| 12050 |
static void |
| 12051 |
dumpRangeType(Archive *fout, const TypeInfo *tyinfo) |
| 12052 |
{ |
| 12053 |
DumpOptions *dopt = fout->dopt; |
| 12054 |
PQExpBuffer q = createPQExpBuffer(); |
| 12055 |
PQExpBuffer delq = createPQExpBuffer(); |
| 12056 |
PQExpBuffer query = createPQExpBuffer(); |
| 12057 |
PGresult *res; |
| 12058 |
Oid collationOid; |
| 12059 |
char *qtypname; |
| 12060 |
char *qualtypname; |
| 12061 |
char *procname; |
| 12062 |
|
| 12063 |
if (!fout->is_prepared[PREPQUERY_DUMPRANGETYPE]) |
| 12064 |
{ |
| 12065 |
/* Set up query for range-specific details */ |
| 12066 |
appendPQExpBufferStr(query, |
| 12067 |
"PREPARE dumpRangeType(pg_catalog.oid) AS\n"); |
| 12068 |
|
| 12069 |
appendPQExpBufferStr(query, |
| 12070 |
"SELECT "); |
| 12071 |
|
| 12072 |
if (fout->remoteVersion >= 140000) |
| 12073 |
appendPQExpBufferStr(query, |
| 12074 |
"pg_catalog.format_type(rngmultitypid, NULL) AS rngmultitype, "); |
| 12075 |
else |
| 12076 |
appendPQExpBufferStr(query, |
| 12077 |
"NULL AS rngmultitype, "); |
| 12078 |
|
| 12079 |
appendPQExpBufferStr(query, |
| 12080 |
"pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, " |
| 12081 |
"opc.opcname AS opcname, " |
| 12082 |
"(SELECT nspname FROM pg_catalog.pg_namespace nsp " |
| 12083 |
" WHERE nsp.oid = opc.opcnamespace) AS opcnsp, " |
| 12084 |
"opc.opcdefault, " |
| 12085 |
"CASE WHEN rngcollation = st.typcollation THEN 0 " |
| 12086 |
" ELSE rngcollation END AS collation, " |
| 12087 |
"rngcanonical, rngsubdiff " |
| 12088 |
"FROM pg_catalog.pg_range r, pg_catalog.pg_type st, " |
| 12089 |
" pg_catalog.pg_opclass opc " |
| 12090 |
"WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND " |
| 12091 |
"rngtypid = $1"); |
| 12092 |
|
| 12093 |
ExecuteSqlStatement(fout, query->data); |
| 12094 |
|
| 12095 |
fout->is_prepared[PREPQUERY_DUMPRANGETYPE] = true; |
| 12096 |
} |
| 12097 |
|
| 12098 |
printfPQExpBuffer(query, |
| 12099 |
"EXECUTE dumpRangeType('%u')", |
| 12100 |
tyinfo->dobj.catId.oid); |
| 12101 |
|
| 12102 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 12103 |
|
| 12104 |
qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); |
| 12105 |
qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); |
| 12106 |
|
| 12107 |
/* |
| 12108 |
* CASCADE shouldn't be required here as for normal types since the I/O |
| 12109 |
* functions are generic and do not get dropped. |
| 12110 |
*/ |
| 12111 |
appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); |
| 12112 |
|
| 12113 |
if (dopt->binary_upgrade) |
| 12114 |
binary_upgrade_set_type_oids_by_type_oid(fout, q, |
| 12115 |
tyinfo->dobj.catId.oid, |
| 12116 |
false, true); |
| 12117 |
|
| 12118 |
appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (", |
| 12119 |
qualtypname); |
| 12120 |
|
| 12121 |
appendPQExpBuffer(q, "\n subtype = %s", |
| 12122 |
PQgetvalue(res, 0, PQfnumber(res, "rngsubtype"))); |
| 12123 |
|
| 12124 |
if (!PQgetisnull(res, 0, PQfnumber(res, "rngmultitype"))) |
| 12125 |
appendPQExpBuffer(q, ",\n multirange_type_name = %s", |
| 12126 |
PQgetvalue(res, 0, PQfnumber(res, "rngmultitype"))); |
| 12127 |
|
| 12128 |
/* print subtype_opclass only if not default for subtype */ |
| 12129 |
if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't') |
| 12130 |
{ |
| 12131 |
char *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname")); |
| 12132 |
char *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp")); |
| 12133 |
|
| 12134 |
appendPQExpBuffer(q, ",\n subtype_opclass = %s.", |
| 12135 |
fmtId(nspname)); |
| 12136 |
appendPQExpBufferStr(q, fmtId(opcname)); |
| 12137 |
} |
| 12138 |
|
| 12139 |
collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation"))); |
| 12140 |
if (OidIsValid(collationOid)) |
| 12141 |
{ |
| 12142 |
CollInfo *coll = findCollationByOid(collationOid); |
| 12143 |
|
| 12144 |
if (coll) |
| 12145 |
appendPQExpBuffer(q, ",\n collation = %s", |
| 12146 |
fmtQualifiedDumpable(coll)); |
| 12147 |
} |
| 12148 |
|
| 12149 |
procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical")); |
| 12150 |
if (strcmp(procname, "-") != 0) |
| 12151 |
appendPQExpBuffer(q, ",\n canonical = %s", procname); |
| 12152 |
|
| 12153 |
procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff")); |
| 12154 |
if (strcmp(procname, "-") != 0) |
| 12155 |
appendPQExpBuffer(q, ",\n subtype_diff = %s", procname); |
| 12156 |
|
| 12157 |
appendPQExpBufferStr(q, "\n);\n"); |
| 12158 |
|
| 12159 |
if (dopt->binary_upgrade) |
| 12160 |
binary_upgrade_extension_member(q, &tyinfo->dobj, |
| 12161 |
"TYPE", qtypname, |
| 12162 |
tyinfo->dobj.namespace->dobj.name); |
| 12163 |
|
| 12164 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 12165 |
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, |
| 12166 |
ARCHIVE_OPTS(.tag = tyinfo->dobj.name, |
| 12167 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 12168 |
.owner = tyinfo->rolname, |
| 12169 |
.description = "TYPE", |
| 12170 |
.section = SECTION_PRE_DATA, |
| 12171 |
.createStmt = q->data, |
| 12172 |
.dropStmt = delq->data)); |
| 12173 |
|
| 12174 |
/* Dump Type Comments and Security Labels */ |
| 12175 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12176 |
dumpComment(fout, "TYPE", qtypname, |
| 12177 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12178 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12179 |
|
| 12180 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 12181 |
dumpSecLabel(fout, "TYPE", qtypname, |
| 12182 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12183 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12184 |
|
| 12185 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 12186 |
dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", |
| 12187 |
qtypname, NULL, |
| 12188 |
tyinfo->dobj.namespace->dobj.name, |
| 12189 |
NULL, tyinfo->rolname, &tyinfo->dacl); |
| 12190 |
|
| 12191 |
PQclear(res); |
| 12192 |
destroyPQExpBuffer(q); |
| 12193 |
destroyPQExpBuffer(delq); |
| 12194 |
destroyPQExpBuffer(query); |
| 12195 |
free(qtypname); |
| 12196 |
free(qualtypname); |
| 12197 |
} |
| 12198 |
|
| 12199 |
/* |
| 12200 |
* dumpUndefinedType |
| 12201 |
* writes out to fout the queries to recreate a !typisdefined type |
| 12202 |
* |
| 12203 |
* This is a shell type, but we use different terminology to distinguish |
| 12204 |
* this case from where we have to emit a shell type definition to break |
| 12205 |
* circular dependencies. An undefined type shouldn't ever have anything |
| 12206 |
* depending on it. |
| 12207 |
*/ |
| 12208 |
static void |
| 12209 |
dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo) |
| 12210 |
{ |
| 12211 |
DumpOptions *dopt = fout->dopt; |
| 12212 |
PQExpBuffer q = createPQExpBuffer(); |
| 12213 |
PQExpBuffer delq = createPQExpBuffer(); |
| 12214 |
char *qtypname; |
| 12215 |
char *qualtypname; |
| 12216 |
|
| 12217 |
qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); |
| 12218 |
qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); |
| 12219 |
|
| 12220 |
appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); |
| 12221 |
|
| 12222 |
if (dopt->binary_upgrade) |
| 12223 |
binary_upgrade_set_type_oids_by_type_oid(fout, q, |
| 12224 |
tyinfo->dobj.catId.oid, |
| 12225 |
false, false); |
| 12226 |
|
| 12227 |
appendPQExpBuffer(q, "CREATE TYPE %s;\n", |
| 12228 |
qualtypname); |
| 12229 |
|
| 12230 |
if (dopt->binary_upgrade) |
| 12231 |
binary_upgrade_extension_member(q, &tyinfo->dobj, |
| 12232 |
"TYPE", qtypname, |
| 12233 |
tyinfo->dobj.namespace->dobj.name); |
| 12234 |
|
| 12235 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 12236 |
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, |
| 12237 |
ARCHIVE_OPTS(.tag = tyinfo->dobj.name, |
| 12238 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 12239 |
.owner = tyinfo->rolname, |
| 12240 |
.description = "TYPE", |
| 12241 |
.section = SECTION_PRE_DATA, |
| 12242 |
.createStmt = q->data, |
| 12243 |
.dropStmt = delq->data)); |
| 12244 |
|
| 12245 |
/* Dump Type Comments and Security Labels */ |
| 12246 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12247 |
dumpComment(fout, "TYPE", qtypname, |
| 12248 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12249 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12250 |
|
| 12251 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 12252 |
dumpSecLabel(fout, "TYPE", qtypname, |
| 12253 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12254 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12255 |
|
| 12256 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 12257 |
dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", |
| 12258 |
qtypname, NULL, |
| 12259 |
tyinfo->dobj.namespace->dobj.name, |
| 12260 |
NULL, tyinfo->rolname, &tyinfo->dacl); |
| 12261 |
|
| 12262 |
destroyPQExpBuffer(q); |
| 12263 |
destroyPQExpBuffer(delq); |
| 12264 |
free(qtypname); |
| 12265 |
free(qualtypname); |
| 12266 |
} |
| 12267 |
|
| 12268 |
/* |
| 12269 |
* dumpBaseType |
| 12270 |
* writes out to fout the queries to recreate a user-defined base type |
| 12271 |
*/ |
| 12272 |
static void |
| 12273 |
dumpBaseType(Archive *fout, const TypeInfo *tyinfo) |
| 12274 |
{ |
| 12275 |
DumpOptions *dopt = fout->dopt; |
| 12276 |
PQExpBuffer q = createPQExpBuffer(); |
| 12277 |
PQExpBuffer delq = createPQExpBuffer(); |
| 12278 |
PQExpBuffer query = createPQExpBuffer(); |
| 12279 |
PGresult *res; |
| 12280 |
char *qtypname; |
| 12281 |
char *qualtypname; |
| 12282 |
char *typlen; |
| 12283 |
char *typinput; |
| 12284 |
char *typoutput; |
| 12285 |
char *typreceive; |
| 12286 |
char *typsend; |
| 12287 |
char *typmodin; |
| 12288 |
char *typmodout; |
| 12289 |
char *typanalyze; |
| 12290 |
char *typsubscript; |
| 12291 |
Oid typreceiveoid; |
| 12292 |
Oid typsendoid; |
| 12293 |
Oid typmodinoid; |
| 12294 |
Oid typmodoutoid; |
| 12295 |
Oid typanalyzeoid; |
| 12296 |
Oid typsubscriptoid; |
| 12297 |
char *typcategory; |
| 12298 |
char *typispreferred; |
| 12299 |
char *typdelim; |
| 12300 |
char *typbyval; |
| 12301 |
char *typalign; |
| 12302 |
char *typstorage; |
| 12303 |
char *typcollatable; |
| 12304 |
char *typdefault; |
| 12305 |
bool typdefault_is_literal = false; |
| 12306 |
|
| 12307 |
if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE]) |
| 12308 |
{ |
| 12309 |
/* Set up query for type-specific details */ |
| 12310 |
appendPQExpBufferStr(query, |
| 12311 |
"PREPARE dumpBaseType(pg_catalog.oid) AS\n" |
| 12312 |
"SELECT typlen, " |
| 12313 |
"typinput, typoutput, typreceive, typsend, " |
| 12314 |
"typreceive::pg_catalog.oid AS typreceiveoid, " |
| 12315 |
"typsend::pg_catalog.oid AS typsendoid, " |
| 12316 |
"typanalyze, " |
| 12317 |
"typanalyze::pg_catalog.oid AS typanalyzeoid, " |
| 12318 |
"typdelim, typbyval, typalign, typstorage, " |
| 12319 |
"typmodin, typmodout, " |
| 12320 |
"typmodin::pg_catalog.oid AS typmodinoid, " |
| 12321 |
"typmodout::pg_catalog.oid AS typmodoutoid, " |
| 12322 |
"typcategory, typispreferred, " |
| 12323 |
"(typcollation <> 0) AS typcollatable, " |
| 12324 |
"pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, "); |
| 12325 |
|
| 12326 |
if (fout->remoteVersion >= 140000) |
| 12327 |
appendPQExpBufferStr(query, |
| 12328 |
"typsubscript, " |
| 12329 |
"typsubscript::pg_catalog.oid AS typsubscriptoid "); |
| 12330 |
else |
| 12331 |
appendPQExpBufferStr(query, |
| 12332 |
"'-' AS typsubscript, 0 AS typsubscriptoid "); |
| 12333 |
|
| 12334 |
appendPQExpBufferStr(query, "FROM pg_catalog.pg_type " |
| 12335 |
"WHERE oid = $1"); |
| 12336 |
|
| 12337 |
ExecuteSqlStatement(fout, query->data); |
| 12338 |
|
| 12339 |
fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true; |
| 12340 |
} |
| 12341 |
|
| 12342 |
printfPQExpBuffer(query, |
| 12343 |
"EXECUTE dumpBaseType('%u')", |
| 12344 |
tyinfo->dobj.catId.oid); |
| 12345 |
|
| 12346 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 12347 |
|
| 12348 |
typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen")); |
| 12349 |
typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput")); |
| 12350 |
typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput")); |
| 12351 |
typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive")); |
| 12352 |
typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend")); |
| 12353 |
typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin")); |
| 12354 |
typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout")); |
| 12355 |
typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze")); |
| 12356 |
typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript")); |
| 12357 |
typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid"))); |
| 12358 |
typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid"))); |
| 12359 |
typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid"))); |
| 12360 |
typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid"))); |
| 12361 |
typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid"))); |
| 12362 |
typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid"))); |
| 12363 |
typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory")); |
| 12364 |
typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred")); |
| 12365 |
typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim")); |
| 12366 |
typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval")); |
| 12367 |
typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign")); |
| 12368 |
typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage")); |
| 12369 |
typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable")); |
| 12370 |
if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin"))) |
| 12371 |
typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin")); |
| 12372 |
else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault"))) |
| 12373 |
{ |
| 12374 |
typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault")); |
| 12375 |
typdefault_is_literal = true; /* it needs quotes */ |
| 12376 |
} |
| 12377 |
else |
| 12378 |
typdefault = NULL; |
| 12379 |
|
| 12380 |
qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); |
| 12381 |
qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); |
| 12382 |
|
| 12383 |
/* |
| 12384 |
* The reason we include CASCADE is that the circular dependency between |
| 12385 |
* the type and its I/O functions makes it impossible to drop the type any |
| 12386 |
* other way. |
| 12387 |
*/ |
| 12388 |
appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname); |
| 12389 |
|
| 12390 |
/* |
| 12391 |
* We might already have a shell type, but setting pg_type_oid is |
| 12392 |
* harmless, and in any case we'd better set the array type OID. |
| 12393 |
*/ |
| 12394 |
if (dopt->binary_upgrade) |
| 12395 |
binary_upgrade_set_type_oids_by_type_oid(fout, q, |
| 12396 |
tyinfo->dobj.catId.oid, |
| 12397 |
false, false); |
| 12398 |
|
| 12399 |
appendPQExpBuffer(q, |
| 12400 |
"CREATE TYPE %s (\n" |
| 12401 |
" INTERNALLENGTH = %s", |
| 12402 |
qualtypname, |
| 12403 |
(strcmp(typlen, "-1") == 0) ? "variable" : typlen); |
| 12404 |
|
| 12405 |
/* regproc result is sufficiently quoted already */ |
| 12406 |
appendPQExpBuffer(q, ",\n INPUT = %s", typinput); |
| 12407 |
appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput); |
| 12408 |
if (OidIsValid(typreceiveoid)) |
| 12409 |
appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive); |
| 12410 |
if (OidIsValid(typsendoid)) |
| 12411 |
appendPQExpBuffer(q, ",\n SEND = %s", typsend); |
| 12412 |
if (OidIsValid(typmodinoid)) |
| 12413 |
appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin); |
| 12414 |
if (OidIsValid(typmodoutoid)) |
| 12415 |
appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout); |
| 12416 |
if (OidIsValid(typanalyzeoid)) |
| 12417 |
appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze); |
| 12418 |
|
| 12419 |
if (strcmp(typcollatable, "t") == 0) |
| 12420 |
appendPQExpBufferStr(q, ",\n COLLATABLE = true"); |
| 12421 |
|
| 12422 |
if (typdefault != NULL) |
| 12423 |
{ |
| 12424 |
appendPQExpBufferStr(q, ",\n DEFAULT = "); |
| 12425 |
if (typdefault_is_literal) |
| 12426 |
appendStringLiteralAH(q, typdefault, fout); |
| 12427 |
else |
| 12428 |
appendPQExpBufferStr(q, typdefault); |
| 12429 |
} |
| 12430 |
|
| 12431 |
if (OidIsValid(typsubscriptoid)) |
| 12432 |
appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript); |
| 12433 |
|
| 12434 |
if (OidIsValid(tyinfo->typelem)) |
| 12435 |
appendPQExpBuffer(q, ",\n ELEMENT = %s", |
| 12436 |
getFormattedTypeName(fout, tyinfo->typelem, |
| 12437 |
zeroIsError)); |
| 12438 |
|
| 12439 |
if (strcmp(typcategory, "U") != 0) |
| 12440 |
{ |
| 12441 |
appendPQExpBufferStr(q, ",\n CATEGORY = "); |
| 12442 |
appendStringLiteralAH(q, typcategory, fout); |
| 12443 |
} |
| 12444 |
|
| 12445 |
if (strcmp(typispreferred, "t") == 0) |
| 12446 |
appendPQExpBufferStr(q, ",\n PREFERRED = true"); |
| 12447 |
|
| 12448 |
if (typdelim && strcmp(typdelim, ",") != 0) |
| 12449 |
{ |
| 12450 |
appendPQExpBufferStr(q, ",\n DELIMITER = "); |
| 12451 |
appendStringLiteralAH(q, typdelim, fout); |
| 12452 |
} |
| 12453 |
|
| 12454 |
if (*typalign == TYPALIGN_CHAR) |
| 12455 |
appendPQExpBufferStr(q, ",\n ALIGNMENT = char"); |
| 12456 |
else if (*typalign == TYPALIGN_SHORT) |
| 12457 |
appendPQExpBufferStr(q, ",\n ALIGNMENT = int2"); |
| 12458 |
else if (*typalign == TYPALIGN_INT) |
| 12459 |
appendPQExpBufferStr(q, ",\n ALIGNMENT = int4"); |
| 12460 |
else if (*typalign == TYPALIGN_DOUBLE) |
| 12461 |
appendPQExpBufferStr(q, ",\n ALIGNMENT = double"); |
| 12462 |
|
| 12463 |
if (*typstorage == TYPSTORAGE_PLAIN) |
| 12464 |
appendPQExpBufferStr(q, ",\n STORAGE = plain"); |
| 12465 |
else if (*typstorage == TYPSTORAGE_EXTERNAL) |
| 12466 |
appendPQExpBufferStr(q, ",\n STORAGE = external"); |
| 12467 |
else if (*typstorage == TYPSTORAGE_EXTENDED) |
| 12468 |
appendPQExpBufferStr(q, ",\n STORAGE = extended"); |
| 12469 |
else if (*typstorage == TYPSTORAGE_MAIN) |
| 12470 |
appendPQExpBufferStr(q, ",\n STORAGE = main"); |
| 12471 |
|
| 12472 |
if (strcmp(typbyval, "t") == 0) |
| 12473 |
appendPQExpBufferStr(q, ",\n PASSEDBYVALUE"); |
| 12474 |
|
| 12475 |
appendPQExpBufferStr(q, "\n);\n"); |
| 12476 |
|
| 12477 |
if (dopt->binary_upgrade) |
| 12478 |
binary_upgrade_extension_member(q, &tyinfo->dobj, |
| 12479 |
"TYPE", qtypname, |
| 12480 |
tyinfo->dobj.namespace->dobj.name); |
| 12481 |
|
| 12482 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 12483 |
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, |
| 12484 |
ARCHIVE_OPTS(.tag = tyinfo->dobj.name, |
| 12485 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 12486 |
.owner = tyinfo->rolname, |
| 12487 |
.description = "TYPE", |
| 12488 |
.section = SECTION_PRE_DATA, |
| 12489 |
.createStmt = q->data, |
| 12490 |
.dropStmt = delq->data)); |
| 12491 |
|
| 12492 |
/* Dump Type Comments and Security Labels */ |
| 12493 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12494 |
dumpComment(fout, "TYPE", qtypname, |
| 12495 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12496 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12497 |
|
| 12498 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 12499 |
dumpSecLabel(fout, "TYPE", qtypname, |
| 12500 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12501 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12502 |
|
| 12503 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 12504 |
dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", |
| 12505 |
qtypname, NULL, |
| 12506 |
tyinfo->dobj.namespace->dobj.name, |
| 12507 |
NULL, tyinfo->rolname, &tyinfo->dacl); |
| 12508 |
|
| 12509 |
PQclear(res); |
| 12510 |
destroyPQExpBuffer(q); |
| 12511 |
destroyPQExpBuffer(delq); |
| 12512 |
destroyPQExpBuffer(query); |
| 12513 |
free(qtypname); |
| 12514 |
free(qualtypname); |
| 12515 |
} |
| 12516 |
|
| 12517 |
/* |
| 12518 |
* dumpDomain |
| 12519 |
* writes out to fout the queries to recreate a user-defined domain |
| 12520 |
*/ |
| 12521 |
static void |
| 12522 |
dumpDomain(Archive *fout, const TypeInfo *tyinfo) |
| 12523 |
{ |
| 12524 |
DumpOptions *dopt = fout->dopt; |
| 12525 |
PQExpBuffer q = createPQExpBuffer(); |
| 12526 |
PQExpBuffer delq = createPQExpBuffer(); |
| 12527 |
PQExpBuffer query = createPQExpBuffer(); |
| 12528 |
PGresult *res; |
| 12529 |
int i; |
| 12530 |
char *qtypname; |
| 12531 |
char *qualtypname; |
| 12532 |
char *typnotnull; |
| 12533 |
char *typdefn; |
| 12534 |
char *typdefault; |
| 12535 |
Oid typcollation; |
| 12536 |
bool typdefault_is_literal = false; |
| 12537 |
|
| 12538 |
if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN]) |
| 12539 |
{ |
| 12540 |
/* Set up query for domain-specific details */ |
| 12541 |
appendPQExpBufferStr(query, |
| 12542 |
"PREPARE dumpDomain(pg_catalog.oid) AS\n"); |
| 12543 |
|
| 12544 |
appendPQExpBufferStr(query, "SELECT t.typnotnull, " |
| 12545 |
"pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, " |
| 12546 |
"pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, " |
| 12547 |
"t.typdefault, " |
| 12548 |
"CASE WHEN t.typcollation <> u.typcollation " |
| 12549 |
"THEN t.typcollation ELSE 0 END AS typcollation " |
| 12550 |
"FROM pg_catalog.pg_type t " |
| 12551 |
"LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) " |
| 12552 |
"WHERE t.oid = $1"); |
| 12553 |
|
| 12554 |
ExecuteSqlStatement(fout, query->data); |
| 12555 |
|
| 12556 |
fout->is_prepared[PREPQUERY_DUMPDOMAIN] = true; |
| 12557 |
} |
| 12558 |
|
| 12559 |
printfPQExpBuffer(query, |
| 12560 |
"EXECUTE dumpDomain('%u')", |
| 12561 |
tyinfo->dobj.catId.oid); |
| 12562 |
|
| 12563 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 12564 |
|
| 12565 |
typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull")); |
| 12566 |
typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn")); |
| 12567 |
if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin"))) |
| 12568 |
typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin")); |
| 12569 |
else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault"))) |
| 12570 |
{ |
| 12571 |
typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault")); |
| 12572 |
typdefault_is_literal = true; /* it needs quotes */ |
| 12573 |
} |
| 12574 |
else |
| 12575 |
typdefault = NULL; |
| 12576 |
typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation"))); |
| 12577 |
|
| 12578 |
if (dopt->binary_upgrade) |
| 12579 |
binary_upgrade_set_type_oids_by_type_oid(fout, q, |
| 12580 |
tyinfo->dobj.catId.oid, |
| 12581 |
true, /* force array type */ |
| 12582 |
false); /* force multirange type */ |
| 12583 |
|
| 12584 |
qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); |
| 12585 |
qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); |
| 12586 |
|
| 12587 |
appendPQExpBuffer(q, |
| 12588 |
"CREATE DOMAIN %s AS %s", |
| 12589 |
qualtypname, |
| 12590 |
typdefn); |
| 12591 |
|
| 12592 |
/* Print collation only if different from base type's collation */ |
| 12593 |
if (OidIsValid(typcollation)) |
| 12594 |
{ |
| 12595 |
CollInfo *coll; |
| 12596 |
|
| 12597 |
coll = findCollationByOid(typcollation); |
| 12598 |
if (coll) |
| 12599 |
appendPQExpBuffer(q, " COLLATE %s", fmtQualifiedDumpable(coll)); |
| 12600 |
} |
| 12601 |
|
| 12602 |
/* |
| 12603 |
* Print a not-null constraint if there's one. In servers older than 17 |
| 12604 |
* these don't have names, so just print it unadorned; in newer ones they |
| 12605 |
* do, but most of the time it's going to be the standard generated one, |
| 12606 |
* so omit the name in that case also. |
| 12607 |
*/ |
| 12608 |
if (typnotnull[0] == 't') |
| 12609 |
{ |
| 12610 |
if (fout->remoteVersion < 170000 || tyinfo->notnull == NULL) |
| 12611 |
appendPQExpBufferStr(q, " NOT NULL"); |
| 12612 |
else |
| 12613 |
{ |
| 12614 |
ConstraintInfo *notnull = tyinfo->notnull; |
| 12615 |
|
| 12616 |
if (!notnull->separate) |
| 12617 |
{ |
| 12618 |
char *default_name; |
| 12619 |
|
| 12620 |
/* XXX should match ChooseConstraintName better */ |
| 12621 |
default_name = psprintf("%s_not_null", tyinfo->dobj.name); |
| 12622 |
|
| 12623 |
if (strcmp(default_name, notnull->dobj.name) == 0) |
| 12624 |
appendPQExpBufferStr(q, " NOT NULL"); |
| 12625 |
else |
| 12626 |
appendPQExpBuffer(q, " CONSTRAINT %s %s", |
| 12627 |
fmtId(notnull->dobj.name), notnull->condef); |
| 12628 |
free(default_name); |
| 12629 |
} |
| 12630 |
} |
| 12631 |
} |
| 12632 |
|
| 12633 |
if (typdefault != NULL) |
| 12634 |
{ |
| 12635 |
appendPQExpBufferStr(q, " DEFAULT "); |
| 12636 |
if (typdefault_is_literal) |
| 12637 |
appendStringLiteralAH(q, typdefault, fout); |
| 12638 |
else |
| 12639 |
appendPQExpBufferStr(q, typdefault); |
| 12640 |
} |
| 12641 |
|
| 12642 |
PQclear(res); |
| 12643 |
|
| 12644 |
/* |
| 12645 |
* Add any CHECK constraints for the domain |
| 12646 |
*/ |
| 12647 |
for (i = 0; i < tyinfo->nDomChecks; i++) |
| 12648 |
{ |
| 12649 |
ConstraintInfo *domcheck = &(tyinfo->domChecks[i]); |
| 12650 |
|
| 12651 |
if (!domcheck->separate && domcheck->contype == 'c') |
| 12652 |
appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s", |
| 12653 |
fmtId(domcheck->dobj.name), domcheck->condef); |
| 12654 |
} |
| 12655 |
|
| 12656 |
appendPQExpBufferStr(q, ";\n"); |
| 12657 |
|
| 12658 |
appendPQExpBuffer(delq, "DROP DOMAIN %s;\n", qualtypname); |
| 12659 |
|
| 12660 |
if (dopt->binary_upgrade) |
| 12661 |
binary_upgrade_extension_member(q, &tyinfo->dobj, |
| 12662 |
"DOMAIN", qtypname, |
| 12663 |
tyinfo->dobj.namespace->dobj.name); |
| 12664 |
|
| 12665 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 12666 |
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, |
| 12667 |
ARCHIVE_OPTS(.tag = tyinfo->dobj.name, |
| 12668 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 12669 |
.owner = tyinfo->rolname, |
| 12670 |
.description = "DOMAIN", |
| 12671 |
.section = SECTION_PRE_DATA, |
| 12672 |
.createStmt = q->data, |
| 12673 |
.dropStmt = delq->data)); |
| 12674 |
|
| 12675 |
/* Dump Domain Comments and Security Labels */ |
| 12676 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12677 |
dumpComment(fout, "DOMAIN", qtypname, |
| 12678 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12679 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12680 |
|
| 12681 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 12682 |
dumpSecLabel(fout, "DOMAIN", qtypname, |
| 12683 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12684 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12685 |
|
| 12686 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 12687 |
dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", |
| 12688 |
qtypname, NULL, |
| 12689 |
tyinfo->dobj.namespace->dobj.name, |
| 12690 |
NULL, tyinfo->rolname, &tyinfo->dacl); |
| 12691 |
|
| 12692 |
/* Dump any per-constraint comments */ |
| 12693 |
for (i = 0; i < tyinfo->nDomChecks; i++) |
| 12694 |
{ |
| 12695 |
ConstraintInfo *domcheck = &(tyinfo->domChecks[i]); |
| 12696 |
PQExpBuffer conprefix; |
| 12697 |
|
| 12698 |
/* but only if the constraint itself was dumped here */ |
| 12699 |
if (domcheck->separate) |
| 12700 |
continue; |
| 12701 |
|
| 12702 |
conprefix = createPQExpBuffer(); |
| 12703 |
appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN", |
| 12704 |
fmtId(domcheck->dobj.name)); |
| 12705 |
|
| 12706 |
if (domcheck->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12707 |
dumpComment(fout, conprefix->data, qtypname, |
| 12708 |
tyinfo->dobj.namespace->dobj.name, |
| 12709 |
tyinfo->rolname, |
| 12710 |
domcheck->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12711 |
|
| 12712 |
destroyPQExpBuffer(conprefix); |
| 12713 |
} |
| 12714 |
|
| 12715 |
/* |
| 12716 |
* And a comment on the not-null constraint, if there's one -- but only if |
| 12717 |
* the constraint itself was dumped here |
| 12718 |
*/ |
| 12719 |
if (tyinfo->notnull != NULL && !tyinfo->notnull->separate) |
| 12720 |
{ |
| 12721 |
PQExpBuffer conprefix = createPQExpBuffer(); |
| 12722 |
|
| 12723 |
appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN", |
| 12724 |
fmtId(tyinfo->notnull->dobj.name)); |
| 12725 |
|
| 12726 |
if (tyinfo->notnull->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12727 |
dumpComment(fout, conprefix->data, qtypname, |
| 12728 |
tyinfo->dobj.namespace->dobj.name, |
| 12729 |
tyinfo->rolname, |
| 12730 |
tyinfo->notnull->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12731 |
destroyPQExpBuffer(conprefix); |
| 12732 |
} |
| 12733 |
|
| 12734 |
destroyPQExpBuffer(q); |
| 12735 |
destroyPQExpBuffer(delq); |
| 12736 |
destroyPQExpBuffer(query); |
| 12737 |
free(qtypname); |
| 12738 |
free(qualtypname); |
| 12739 |
} |
| 12740 |
|
| 12741 |
/* |
| 12742 |
* dumpCompositeType |
| 12743 |
* writes out to fout the queries to recreate a user-defined stand-alone |
| 12744 |
* composite type |
| 12745 |
*/ |
| 12746 |
static void |
| 12747 |
dumpCompositeType(Archive *fout, const TypeInfo *tyinfo) |
| 12748 |
{ |
| 12749 |
DumpOptions *dopt = fout->dopt; |
| 12750 |
PQExpBuffer q = createPQExpBuffer(); |
| 12751 |
PQExpBuffer dropped = createPQExpBuffer(); |
| 12752 |
PQExpBuffer delq = createPQExpBuffer(); |
| 12753 |
PQExpBuffer query = createPQExpBuffer(); |
| 12754 |
PGresult *res; |
| 12755 |
char *qtypname; |
| 12756 |
char *qualtypname; |
| 12757 |
int ntups; |
| 12758 |
int i_attname; |
| 12759 |
int i_atttypdefn; |
| 12760 |
int i_attlen; |
| 12761 |
int i_attalign; |
| 12762 |
int i_attisdropped; |
| 12763 |
int i_attcollation; |
| 12764 |
int i; |
| 12765 |
int actual_atts; |
| 12766 |
|
| 12767 |
if (!fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE]) |
| 12768 |
{ |
| 12769 |
/* |
| 12770 |
* Set up query for type-specific details. |
| 12771 |
* |
| 12772 |
* Since we only want to dump COLLATE clauses for attributes whose |
| 12773 |
* collation is different from their type's default, we use a CASE |
| 12774 |
* here to suppress uninteresting attcollations cheaply. atttypid |
| 12775 |
* will be 0 for dropped columns; collation does not matter for those. |
| 12776 |
*/ |
| 12777 |
appendPQExpBufferStr(query, |
| 12778 |
"PREPARE dumpCompositeType(pg_catalog.oid) AS\n" |
| 12779 |
"SELECT a.attname, a.attnum, " |
| 12780 |
"pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, " |
| 12781 |
"a.attlen, a.attalign, a.attisdropped, " |
| 12782 |
"CASE WHEN a.attcollation <> at.typcollation " |
| 12783 |
"THEN a.attcollation ELSE 0 END AS attcollation " |
| 12784 |
"FROM pg_catalog.pg_type ct " |
| 12785 |
"JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid " |
| 12786 |
"LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid " |
| 12787 |
"WHERE ct.oid = $1 " |
| 12788 |
"ORDER BY a.attnum"); |
| 12789 |
|
| 12790 |
ExecuteSqlStatement(fout, query->data); |
| 12791 |
|
| 12792 |
fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE] = true; |
| 12793 |
} |
| 12794 |
|
| 12795 |
printfPQExpBuffer(query, |
| 12796 |
"EXECUTE dumpCompositeType('%u')", |
| 12797 |
tyinfo->dobj.catId.oid); |
| 12798 |
|
| 12799 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 12800 |
|
| 12801 |
ntups = PQntuples(res); |
| 12802 |
|
| 12803 |
i_attname = PQfnumber(res, "attname"); |
| 12804 |
i_atttypdefn = PQfnumber(res, "atttypdefn"); |
| 12805 |
i_attlen = PQfnumber(res, "attlen"); |
| 12806 |
i_attalign = PQfnumber(res, "attalign"); |
| 12807 |
i_attisdropped = PQfnumber(res, "attisdropped"); |
| 12808 |
i_attcollation = PQfnumber(res, "attcollation"); |
| 12809 |
|
| 12810 |
if (dopt->binary_upgrade) |
| 12811 |
{ |
| 12812 |
binary_upgrade_set_type_oids_by_type_oid(fout, q, |
| 12813 |
tyinfo->dobj.catId.oid, |
| 12814 |
false, false); |
| 12815 |
binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid); |
| 12816 |
} |
| 12817 |
|
| 12818 |
qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); |
| 12819 |
qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); |
| 12820 |
|
| 12821 |
appendPQExpBuffer(q, "CREATE TYPE %s AS (", |
| 12822 |
qualtypname); |
| 12823 |
|
| 12824 |
actual_atts = 0; |
| 12825 |
for (i = 0; i < ntups; i++) |
| 12826 |
{ |
| 12827 |
char *attname; |
| 12828 |
char *atttypdefn; |
| 12829 |
char *attlen; |
| 12830 |
char *attalign; |
| 12831 |
bool attisdropped; |
| 12832 |
Oid attcollation; |
| 12833 |
|
| 12834 |
attname = PQgetvalue(res, i, i_attname); |
| 12835 |
atttypdefn = PQgetvalue(res, i, i_atttypdefn); |
| 12836 |
attlen = PQgetvalue(res, i, i_attlen); |
| 12837 |
attalign = PQgetvalue(res, i, i_attalign); |
| 12838 |
attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't'); |
| 12839 |
attcollation = atooid(PQgetvalue(res, i, i_attcollation)); |
| 12840 |
|
| 12841 |
if (attisdropped && !dopt->binary_upgrade) |
| 12842 |
continue; |
| 12843 |
|
| 12844 |
/* Format properly if not first attr */ |
| 12845 |
if (actual_atts++ > 0) |
| 12846 |
appendPQExpBufferChar(q, ','); |
| 12847 |
appendPQExpBufferStr(q, "\n\t"); |
| 12848 |
|
| 12849 |
if (!attisdropped) |
| 12850 |
{ |
| 12851 |
appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn); |
| 12852 |
|
| 12853 |
/* Add collation if not default for the column type */ |
| 12854 |
if (OidIsValid(attcollation)) |
| 12855 |
{ |
| 12856 |
CollInfo *coll; |
| 12857 |
|
| 12858 |
coll = findCollationByOid(attcollation); |
| 12859 |
if (coll) |
| 12860 |
appendPQExpBuffer(q, " COLLATE %s", |
| 12861 |
fmtQualifiedDumpable(coll)); |
| 12862 |
} |
| 12863 |
} |
| 12864 |
else |
| 12865 |
{ |
| 12866 |
/* |
| 12867 |
* This is a dropped attribute and we're in binary_upgrade mode. |
| 12868 |
* Insert a placeholder for it in the CREATE TYPE command, and set |
| 12869 |
* length and alignment with direct UPDATE to the catalogs |
| 12870 |
* afterwards. See similar code in dumpTableSchema(). |
| 12871 |
*/ |
| 12872 |
appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname)); |
| 12873 |
|
| 12874 |
/* stash separately for insertion after the CREATE TYPE */ |
| 12875 |
appendPQExpBufferStr(dropped, |
| 12876 |
"\n-- For binary upgrade, recreate dropped column.\n"); |
| 12877 |
appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n" |
| 12878 |
"SET attlen = %s, " |
| 12879 |
"attalign = '%s', attbyval = false\n" |
| 12880 |
"WHERE attname = ", attlen, attalign); |
| 12881 |
appendStringLiteralAH(dropped, attname, fout); |
| 12882 |
appendPQExpBufferStr(dropped, "\n AND attrelid = "); |
| 12883 |
appendStringLiteralAH(dropped, qualtypname, fout); |
| 12884 |
appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n"); |
| 12885 |
|
| 12886 |
appendPQExpBuffer(dropped, "ALTER TYPE %s ", |
| 12887 |
qualtypname); |
| 12888 |
appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n", |
| 12889 |
fmtId(attname)); |
| 12890 |
} |
| 12891 |
} |
| 12892 |
appendPQExpBufferStr(q, "\n);\n"); |
| 12893 |
appendPQExpBufferStr(q, dropped->data); |
| 12894 |
|
| 12895 |
appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); |
| 12896 |
|
| 12897 |
if (dopt->binary_upgrade) |
| 12898 |
binary_upgrade_extension_member(q, &tyinfo->dobj, |
| 12899 |
"TYPE", qtypname, |
| 12900 |
tyinfo->dobj.namespace->dobj.name); |
| 12901 |
|
| 12902 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 12903 |
ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, |
| 12904 |
ARCHIVE_OPTS(.tag = tyinfo->dobj.name, |
| 12905 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 12906 |
.owner = tyinfo->rolname, |
| 12907 |
.description = "TYPE", |
| 12908 |
.section = SECTION_PRE_DATA, |
| 12909 |
.createStmt = q->data, |
| 12910 |
.dropStmt = delq->data)); |
| 12911 |
|
| 12912 |
|
| 12913 |
/* Dump Type Comments and Security Labels */ |
| 12914 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12915 |
dumpComment(fout, "TYPE", qtypname, |
| 12916 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12917 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12918 |
|
| 12919 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 12920 |
dumpSecLabel(fout, "TYPE", qtypname, |
| 12921 |
tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, |
| 12922 |
tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); |
| 12923 |
|
| 12924 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 12925 |
dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", |
| 12926 |
qtypname, NULL, |
| 12927 |
tyinfo->dobj.namespace->dobj.name, |
| 12928 |
NULL, tyinfo->rolname, &tyinfo->dacl); |
| 12929 |
|
| 12930 |
/* Dump any per-column comments */ |
| 12931 |
if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 12932 |
dumpCompositeTypeColComments(fout, tyinfo, res); |
| 12933 |
|
| 12934 |
PQclear(res); |
| 12935 |
destroyPQExpBuffer(q); |
| 12936 |
destroyPQExpBuffer(dropped); |
| 12937 |
destroyPQExpBuffer(delq); |
| 12938 |
destroyPQExpBuffer(query); |
| 12939 |
free(qtypname); |
| 12940 |
free(qualtypname); |
| 12941 |
} |
| 12942 |
|
| 12943 |
/* |
| 12944 |
* dumpCompositeTypeColComments |
| 12945 |
* writes out to fout the queries to recreate comments on the columns of |
| 12946 |
* a user-defined stand-alone composite type. |
| 12947 |
* |
| 12948 |
* The caller has already made a query to collect the names and attnums |
| 12949 |
* of the type's columns, so we just pass that result into here rather |
| 12950 |
* than reading them again. |
| 12951 |
*/ |
| 12952 |
static void |
| 12953 |
dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, |
| 12954 |
PGresult *res) |
| 12955 |
{ |
| 12956 |
CommentItem *comments; |
| 12957 |
int ncomments; |
| 12958 |
PQExpBuffer query; |
| 12959 |
PQExpBuffer target; |
| 12960 |
int i; |
| 12961 |
int ntups; |
| 12962 |
int i_attname; |
| 12963 |
int i_attnum; |
| 12964 |
int i_attisdropped; |
| 12965 |
|
| 12966 |
/* do nothing, if --no-comments is supplied */ |
| 12967 |
if (fout->dopt->no_comments) |
| 12968 |
return; |
| 12969 |
|
| 12970 |
/* Search for comments associated with type's pg_class OID */ |
| 12971 |
ncomments = findComments(RelationRelationId, tyinfo->typrelid, |
| 12972 |
&comments); |
| 12973 |
|
| 12974 |
/* If no comments exist, we're done */ |
| 12975 |
if (ncomments <= 0) |
| 12976 |
return; |
| 12977 |
|
| 12978 |
/* Build COMMENT ON statements */ |
| 12979 |
query = createPQExpBuffer(); |
| 12980 |
target = createPQExpBuffer(); |
| 12981 |
|
| 12982 |
ntups = PQntuples(res); |
| 12983 |
i_attnum = PQfnumber(res, "attnum"); |
| 12984 |
i_attname = PQfnumber(res, "attname"); |
| 12985 |
i_attisdropped = PQfnumber(res, "attisdropped"); |
| 12986 |
while (ncomments > 0) |
| 12987 |
{ |
| 12988 |
const char *attname; |
| 12989 |
|
| 12990 |
attname = NULL; |
| 12991 |
for (i = 0; i < ntups; i++) |
| 12992 |
{ |
| 12993 |
if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid && |
| 12994 |
PQgetvalue(res, i, i_attisdropped)[0] != 't') |
| 12995 |
{ |
| 12996 |
attname = PQgetvalue(res, i, i_attname); |
| 12997 |
break; |
| 12998 |
} |
| 12999 |
} |
| 13000 |
if (attname) /* just in case we don't find it */ |
| 13001 |
{ |
| 13002 |
const char *descr = comments->descr; |
| 13003 |
|
| 13004 |
resetPQExpBuffer(target); |
| 13005 |
appendPQExpBuffer(target, "COLUMN %s.", |
| 13006 |
fmtId(tyinfo->dobj.name)); |
| 13007 |
appendPQExpBufferStr(target, fmtId(attname)); |
| 13008 |
|
| 13009 |
resetPQExpBuffer(query); |
| 13010 |
appendPQExpBuffer(query, "COMMENT ON COLUMN %s.", |
| 13011 |
fmtQualifiedDumpable(tyinfo)); |
| 13012 |
appendPQExpBuffer(query, "%s IS ", fmtId(attname)); |
| 13013 |
appendStringLiteralAH(query, descr, fout); |
| 13014 |
appendPQExpBufferStr(query, ";\n"); |
| 13015 |
|
| 13016 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 13017 |
ARCHIVE_OPTS(.tag = target->data, |
| 13018 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 13019 |
.owner = tyinfo->rolname, |
| 13020 |
.description = "COMMENT", |
| 13021 |
.section = SECTION_NONE, |
| 13022 |
.createStmt = query->data, |
| 13023 |
.deps = &(tyinfo->dobj.dumpId), |
| 13024 |
.nDeps = 1)); |
| 13025 |
} |
| 13026 |
|
| 13027 |
comments++; |
| 13028 |
ncomments--; |
| 13029 |
} |
| 13030 |
|
| 13031 |
destroyPQExpBuffer(query); |
| 13032 |
destroyPQExpBuffer(target); |
| 13033 |
} |
| 13034 |
|
| 13035 |
/* |
| 13036 |
* dumpShellType |
| 13037 |
* writes out to fout the queries to create a shell type |
| 13038 |
* |
| 13039 |
* We dump a shell definition in advance of the I/O functions for the type. |
| 13040 |
*/ |
| 13041 |
static void |
| 13042 |
dumpShellType(Archive *fout, const ShellTypeInfo *stinfo) |
| 13043 |
{ |
| 13044 |
DumpOptions *dopt = fout->dopt; |
| 13045 |
PQExpBuffer q; |
| 13046 |
|
| 13047 |
/* Do nothing if not dumping schema */ |
| 13048 |
if (!dopt->dumpSchema) |
| 13049 |
return; |
| 13050 |
|
| 13051 |
q = createPQExpBuffer(); |
| 13052 |
|
| 13053 |
/* |
| 13054 |
* Note the lack of a DROP command for the shell type; any required DROP |
| 13055 |
* is driven off the base type entry, instead. This interacts with |
| 13056 |
* _printTocEntry()'s use of the presence of a DROP command to decide |
| 13057 |
* whether an entry needs an ALTER OWNER command. We don't want to alter |
| 13058 |
* the shell type's owner immediately on creation; that should happen only |
| 13059 |
* after it's filled in, otherwise the backend complains. |
| 13060 |
*/ |
| 13061 |
|
| 13062 |
if (dopt->binary_upgrade) |
| 13063 |
binary_upgrade_set_type_oids_by_type_oid(fout, q, |
| 13064 |
stinfo->baseType->dobj.catId.oid, |
| 13065 |
false, false); |
| 13066 |
|
| 13067 |
appendPQExpBuffer(q, "CREATE TYPE %s;\n", |
| 13068 |
fmtQualifiedDumpable(stinfo)); |
| 13069 |
|
| 13070 |
if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 13071 |
ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId, |
| 13072 |
ARCHIVE_OPTS(.tag = stinfo->dobj.name, |
| 13073 |
.namespace = stinfo->dobj.namespace->dobj.name, |
| 13074 |
.owner = stinfo->baseType->rolname, |
| 13075 |
.description = "SHELL TYPE", |
| 13076 |
.section = SECTION_PRE_DATA, |
| 13077 |
.createStmt = q->data)); |
| 13078 |
|
| 13079 |
destroyPQExpBuffer(q); |
| 13080 |
} |
| 13081 |
|
| 13082 |
/* |
| 13083 |
* dumpProcLang |
| 13084 |
* writes out to fout the queries to recreate a user-defined |
| 13085 |
* procedural language |
| 13086 |
*/ |
| 13087 |
static void |
| 13088 |
dumpProcLang(Archive *fout, const ProcLangInfo *plang) |
| 13089 |
{ |
| 13090 |
DumpOptions *dopt = fout->dopt; |
| 13091 |
PQExpBuffer defqry; |
| 13092 |
PQExpBuffer delqry; |
| 13093 |
bool useParams; |
| 13094 |
char *qlanname; |
| 13095 |
FuncInfo *funcInfo; |
| 13096 |
FuncInfo *inlineInfo = NULL; |
| 13097 |
FuncInfo *validatorInfo = NULL; |
| 13098 |
|
| 13099 |
/* Do nothing if not dumping schema */ |
| 13100 |
if (!dopt->dumpSchema) |
| 13101 |
return; |
| 13102 |
|
| 13103 |
/* |
| 13104 |
* Try to find the support function(s). It is not an error if we don't |
| 13105 |
* find them --- if the functions are in the pg_catalog schema, as is |
| 13106 |
* standard in 8.1 and up, then we won't have loaded them. (In this case |
| 13107 |
* we will emit a parameterless CREATE LANGUAGE command, which will |
| 13108 |
* require PL template knowledge in the backend to reload.) |
| 13109 |
*/ |
| 13110 |
|
| 13111 |
funcInfo = findFuncByOid(plang->lanplcallfoid); |
| 13112 |
if (funcInfo != NULL && !funcInfo->dobj.dump) |
| 13113 |
funcInfo = NULL; /* treat not-dumped same as not-found */ |
| 13114 |
|
| 13115 |
if (OidIsValid(plang->laninline)) |
| 13116 |
{ |
| 13117 |
inlineInfo = findFuncByOid(plang->laninline); |
| 13118 |
if (inlineInfo != NULL && !inlineInfo->dobj.dump) |
| 13119 |
inlineInfo = NULL; |
| 13120 |
} |
| 13121 |
|
| 13122 |
if (OidIsValid(plang->lanvalidator)) |
| 13123 |
{ |
| 13124 |
validatorInfo = findFuncByOid(plang->lanvalidator); |
| 13125 |
if (validatorInfo != NULL && !validatorInfo->dobj.dump) |
| 13126 |
validatorInfo = NULL; |
| 13127 |
} |
| 13128 |
|
| 13129 |
/* |
| 13130 |
* If the functions are dumpable then emit a complete CREATE LANGUAGE with |
| 13131 |
* parameters. Otherwise, we'll write a parameterless command, which will |
| 13132 |
* be interpreted as CREATE EXTENSION. |
| 13133 |
*/ |
| 13134 |
useParams = (funcInfo != NULL && |
| 13135 |
(inlineInfo != NULL || !OidIsValid(plang->laninline)) && |
| 13136 |
(validatorInfo != NULL || !OidIsValid(plang->lanvalidator))); |
| 13137 |
|
| 13138 |
defqry = createPQExpBuffer(); |
| 13139 |
delqry = createPQExpBuffer(); |
| 13140 |
|
| 13141 |
qlanname = pg_strdup(fmtId(plang->dobj.name)); |
| 13142 |
|
| 13143 |
appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n", |
| 13144 |
qlanname); |
| 13145 |
|
| 13146 |
if (useParams) |
| 13147 |
{ |
| 13148 |
appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s", |
| 13149 |
plang->lanpltrusted ? "TRUSTED " : "", |
| 13150 |
qlanname); |
| 13151 |
appendPQExpBuffer(defqry, " HANDLER %s", |
| 13152 |
fmtQualifiedDumpable(funcInfo)); |
| 13153 |
if (OidIsValid(plang->laninline)) |
| 13154 |
appendPQExpBuffer(defqry, " INLINE %s", |
| 13155 |
fmtQualifiedDumpable(inlineInfo)); |
| 13156 |
if (OidIsValid(plang->lanvalidator)) |
| 13157 |
appendPQExpBuffer(defqry, " VALIDATOR %s", |
| 13158 |
fmtQualifiedDumpable(validatorInfo)); |
| 13159 |
} |
| 13160 |
else |
| 13161 |
{ |
| 13162 |
/* |
| 13163 |
* If not dumping parameters, then use CREATE OR REPLACE so that the |
| 13164 |
* command will not fail if the language is preinstalled in the target |
| 13165 |
* database. |
| 13166 |
* |
| 13167 |
* Modern servers will interpret this as CREATE EXTENSION IF NOT |
| 13168 |
* EXISTS; perhaps we should emit that instead? But it might just add |
| 13169 |
* confusion. |
| 13170 |
*/ |
| 13171 |
appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s", |
| 13172 |
qlanname); |
| 13173 |
} |
| 13174 |
appendPQExpBufferStr(defqry, ";\n"); |
| 13175 |
|
| 13176 |
if (dopt->binary_upgrade) |
| 13177 |
binary_upgrade_extension_member(defqry, &plang->dobj, |
| 13178 |
"LANGUAGE", qlanname, NULL); |
| 13179 |
|
| 13180 |
if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 13181 |
ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId, |
| 13182 |
ARCHIVE_OPTS(.tag = plang->dobj.name, |
| 13183 |
.owner = plang->lanowner, |
| 13184 |
.description = "PROCEDURAL LANGUAGE", |
| 13185 |
.section = SECTION_PRE_DATA, |
| 13186 |
.createStmt = defqry->data, |
| 13187 |
.dropStmt = delqry->data, |
| 13188 |
)); |
| 13189 |
|
| 13190 |
/* Dump Proc Lang Comments and Security Labels */ |
| 13191 |
if (plang->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 13192 |
dumpComment(fout, "LANGUAGE", qlanname, |
| 13193 |
NULL, plang->lanowner, |
| 13194 |
plang->dobj.catId, 0, plang->dobj.dumpId); |
| 13195 |
|
| 13196 |
if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 13197 |
dumpSecLabel(fout, "LANGUAGE", qlanname, |
| 13198 |
NULL, plang->lanowner, |
| 13199 |
plang->dobj.catId, 0, plang->dobj.dumpId); |
| 13200 |
|
| 13201 |
if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL) |
| 13202 |
dumpACL(fout, plang->dobj.dumpId, InvalidDumpId, "LANGUAGE", |
| 13203 |
qlanname, NULL, NULL, |
| 13204 |
NULL, plang->lanowner, &plang->dacl); |
| 13205 |
|
| 13206 |
free(qlanname); |
| 13207 |
|
| 13208 |
destroyPQExpBuffer(defqry); |
| 13209 |
destroyPQExpBuffer(delqry); |
| 13210 |
} |
| 13211 |
|
| 13212 |
/* |
| 13213 |
* format_function_arguments: generate function name and argument list |
| 13214 |
* |
| 13215 |
* This is used when we can rely on pg_get_function_arguments to format |
| 13216 |
* the argument list. Note, however, that pg_get_function_arguments |
| 13217 |
* does not special-case zero-argument aggregates. |
| 13218 |
*/ |
| 13219 |
static char * |
| 13220 |
format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg) |
| 13221 |
{ |
| 13222 |
PQExpBufferData fn; |
| 13223 |
|
| 13224 |
initPQExpBuffer(&fn); |
| 13225 |
appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name)); |
| 13226 |
if (is_agg && finfo->nargs == 0) |
| 13227 |
appendPQExpBufferStr(&fn, "(*)"); |
| 13228 |
else |
| 13229 |
appendPQExpBuffer(&fn, "(%s)", funcargs); |
| 13230 |
return fn.data; |
| 13231 |
} |
| 13232 |
|
| 13233 |
/* |
| 13234 |
* format_function_signature: generate function name and argument list |
| 13235 |
* |
| 13236 |
* Only a minimal list of input argument types is generated; this is |
| 13237 |
* sufficient to reference the function, but not to define it. |
| 13238 |
* |
| 13239 |
* If honor_quotes is false then the function name is never quoted. |
| 13240 |
* This is appropriate for use in TOC tags, but not in SQL commands. |
| 13241 |
*/ |
| 13242 |
static char * |
| 13243 |
format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes) |
| 13244 |
{ |
| 13245 |
PQExpBufferData fn; |
| 13246 |
int j; |
| 13247 |
|
| 13248 |
initPQExpBuffer(&fn); |
| 13249 |
if (honor_quotes) |
| 13250 |
appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name)); |
| 13251 |
else |
| 13252 |
appendPQExpBuffer(&fn, "%s(", finfo->dobj.name); |
| 13253 |
for (j = 0; j < finfo->nargs; j++) |
| 13254 |
{ |
| 13255 |
if (j > 0) |
| 13256 |
appendPQExpBufferStr(&fn, ", "); |
| 13257 |
|
| 13258 |
appendPQExpBufferStr(&fn, |
| 13259 |
getFormattedTypeName(fout, finfo->argtypes[j], |
| 13260 |
zeroIsError)); |
| 13261 |
} |
| 13262 |
appendPQExpBufferChar(&fn, ')'); |
| 13263 |
return fn.data; |
| 13264 |
} |
| 13265 |
|
| 13266 |
|
| 13267 |
/* |
| 13268 |
* dumpFunc: |
| 13269 |
* dump out one function |
| 13270 |
*/ |
| 13271 |
static void |
| 13272 |
dumpFunc(Archive *fout, const FuncInfo *finfo) |
| 13273 |
{ |
| 13274 |
DumpOptions *dopt = fout->dopt; |
| 13275 |
PQExpBuffer query; |
| 13276 |
PQExpBuffer q; |
| 13277 |
PQExpBuffer delqry; |
| 13278 |
PQExpBuffer asPart; |
| 13279 |
PGresult *res; |
| 13280 |
char *funcsig; /* identity signature */ |
| 13281 |
char *funcfullsig = NULL; /* full signature */ |
| 13282 |
char *funcsig_tag; |
| 13283 |
char *qual_funcsig; |
| 13284 |
char *proretset; |
| 13285 |
char *prosrc; |
| 13286 |
char *probin; |
| 13287 |
char *prosqlbody; |
| 13288 |
char *funcargs; |
| 13289 |
char *funciargs; |
| 13290 |
char *funcresult; |
| 13291 |
char *protrftypes; |
| 13292 |
char *prokind; |
| 13293 |
char *provolatile; |
| 13294 |
char *proisstrict; |
| 13295 |
char *prosecdef; |
| 13296 |
char *proleakproof; |
| 13297 |
char *proconfig; |
| 13298 |
char *procost; |
| 13299 |
char *prorows; |
| 13300 |
char *prosupport; |
| 13301 |
char *proparallel; |
| 13302 |
char *lanname; |
| 13303 |
char **configitems = NULL; |
| 13304 |
int nconfigitems = 0; |
| 13305 |
const char *keyword; |
| 13306 |
|
| 13307 |
/* Do nothing if not dumping schema */ |
| 13308 |
if (!dopt->dumpSchema) |
| 13309 |
return; |
| 13310 |
|
| 13311 |
query = createPQExpBuffer(); |
| 13312 |
q = createPQExpBuffer(); |
| 13313 |
delqry = createPQExpBuffer(); |
| 13314 |
asPart = createPQExpBuffer(); |
| 13315 |
|
| 13316 |
if (!fout->is_prepared[PREPQUERY_DUMPFUNC]) |
| 13317 |
{ |
| 13318 |
/* Set up query for function-specific details */ |
| 13319 |
appendPQExpBufferStr(query, |
| 13320 |
"PREPARE dumpFunc(pg_catalog.oid) AS\n"); |
| 13321 |
|
| 13322 |
appendPQExpBufferStr(query, |
| 13323 |
"SELECT\n" |
| 13324 |
"proretset,\n" |
| 13325 |
"prosrc,\n" |
| 13326 |
"probin,\n" |
| 13327 |
"provolatile,\n" |
| 13328 |
"proisstrict,\n" |
| 13329 |
"prosecdef,\n" |
| 13330 |
"lanname,\n" |
| 13331 |
"proconfig,\n" |
| 13332 |
"procost,\n" |
| 13333 |
"prorows,\n" |
| 13334 |
"pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n" |
| 13335 |
"pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n" |
| 13336 |
"pg_catalog.pg_get_function_result(p.oid) AS funcresult,\n" |
| 13337 |
"proleakproof,\n"); |
| 13338 |
|
| 13339 |
if (fout->remoteVersion >= 90500) |
| 13340 |
appendPQExpBufferStr(query, |
| 13341 |
"array_to_string(protrftypes, ' ') AS protrftypes,\n"); |
| 13342 |
else |
| 13343 |
appendPQExpBufferStr(query, |
| 13344 |
"NULL AS protrftypes,\n"); |
| 13345 |
|
| 13346 |
if (fout->remoteVersion >= 90600) |
| 13347 |
appendPQExpBufferStr(query, |
| 13348 |
"proparallel,\n"); |
| 13349 |
else |
| 13350 |
appendPQExpBufferStr(query, |
| 13351 |
"'u' AS proparallel,\n"); |
| 13352 |
|
| 13353 |
if (fout->remoteVersion >= 110000) |
| 13354 |
appendPQExpBufferStr(query, |
| 13355 |
"prokind,\n"); |
| 13356 |
else |
| 13357 |
appendPQExpBufferStr(query, |
| 13358 |
"CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind,\n"); |
| 13359 |
|
| 13360 |
if (fout->remoteVersion >= 120000) |
| 13361 |
appendPQExpBufferStr(query, |
| 13362 |
"prosupport,\n"); |
| 13363 |
else |
| 13364 |
appendPQExpBufferStr(query, |
| 13365 |
"'-' AS prosupport,\n"); |
| 13366 |
|
| 13367 |
if (fout->remoteVersion >= 140000) |
| 13368 |
appendPQExpBufferStr(query, |
| 13369 |
"pg_get_function_sqlbody(p.oid) AS prosqlbody\n"); |
| 13370 |
else |
| 13371 |
appendPQExpBufferStr(query, |
| 13372 |
"NULL AS prosqlbody\n"); |
| 13373 |
|
| 13374 |
appendPQExpBufferStr(query, |
| 13375 |
"FROM pg_catalog.pg_proc p, pg_catalog.pg_language l\n" |
| 13376 |
"WHERE p.oid = $1 " |
| 13377 |
"AND l.oid = p.prolang"); |
| 13378 |
|
| 13379 |
ExecuteSqlStatement(fout, query->data); |
| 13380 |
|
| 13381 |
fout->is_prepared[PREPQUERY_DUMPFUNC] = true; |
| 13382 |
} |
| 13383 |
|
| 13384 |
printfPQExpBuffer(query, |
| 13385 |
"EXECUTE dumpFunc('%u')", |
| 13386 |
finfo->dobj.catId.oid); |
| 13387 |
|
| 13388 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 13389 |
|
| 13390 |
proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset")); |
| 13391 |
if (PQgetisnull(res, 0, PQfnumber(res, "prosqlbody"))) |
| 13392 |
{ |
| 13393 |
prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc")); |
| 13394 |
probin = PQgetvalue(res, 0, PQfnumber(res, "probin")); |
| 13395 |
prosqlbody = NULL; |
| 13396 |
} |
| 13397 |
else |
| 13398 |
{ |
| 13399 |
prosrc = NULL; |
| 13400 |
probin = NULL; |
| 13401 |
prosqlbody = PQgetvalue(res, 0, PQfnumber(res, "prosqlbody")); |
| 13402 |
} |
| 13403 |
funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs")); |
| 13404 |
funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs")); |
| 13405 |
funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult")); |
| 13406 |
protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes")); |
| 13407 |
prokind = PQgetvalue(res, 0, PQfnumber(res, "prokind")); |
| 13408 |
provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile")); |
| 13409 |
proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict")); |
| 13410 |
prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef")); |
| 13411 |
proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof")); |
| 13412 |
proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig")); |
| 13413 |
procost = PQgetvalue(res, 0, PQfnumber(res, "procost")); |
| 13414 |
prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows")); |
| 13415 |
prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport")); |
| 13416 |
proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel")); |
| 13417 |
lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname")); |
| 13418 |
|
| 13419 |
/* |
| 13420 |
* See backend/commands/functioncmds.c for details of how the 'AS' clause |
| 13421 |
* is used. |
| 13422 |
*/ |
| 13423 |
if (prosqlbody) |
| 13424 |
{ |
| 13425 |
appendPQExpBufferStr(asPart, prosqlbody); |
| 13426 |
} |
| 13427 |
else if (probin[0] != '\0') |
| 13428 |
{ |
| 13429 |
appendPQExpBufferStr(asPart, "AS "); |
| 13430 |
appendStringLiteralAH(asPart, probin, fout); |
| 13431 |
if (prosrc[0] != '\0') |
| 13432 |
{ |
| 13433 |
appendPQExpBufferStr(asPart, ", "); |
| 13434 |
|
| 13435 |
/* |
| 13436 |
* where we have bin, use dollar quoting if allowed and src |
| 13437 |
* contains quote or backslash; else use regular quoting. |
| 13438 |
*/ |
| 13439 |
if (dopt->disable_dollar_quoting || |
| 13440 |
(strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL)) |
| 13441 |
appendStringLiteralAH(asPart, prosrc, fout); |
| 13442 |
else |
| 13443 |
appendStringLiteralDQ(asPart, prosrc, NULL); |
| 13444 |
} |
| 13445 |
} |
| 13446 |
else |
| 13447 |
{ |
| 13448 |
appendPQExpBufferStr(asPart, "AS "); |
| 13449 |
/* with no bin, dollar quote src unconditionally if allowed */ |
| 13450 |
if (dopt->disable_dollar_quoting) |
| 13451 |
appendStringLiteralAH(asPart, prosrc, fout); |
| 13452 |
else |
| 13453 |
appendStringLiteralDQ(asPart, prosrc, NULL); |
| 13454 |
} |
| 13455 |
|
| 13456 |
if (*proconfig) |
| 13457 |
{ |
| 13458 |
if (!parsePGArray(proconfig, &configitems, &nconfigitems)) |
| 13459 |
pg_fatal("could not parse %s array", "proconfig"); |
| 13460 |
} |
| 13461 |
else |
| 13462 |
{ |
| 13463 |
configitems = NULL; |
| 13464 |
nconfigitems = 0; |
| 13465 |
} |
| 13466 |
|
| 13467 |
funcfullsig = format_function_arguments(finfo, funcargs, false); |
| 13468 |
funcsig = format_function_arguments(finfo, funciargs, false); |
| 13469 |
|
| 13470 |
funcsig_tag = format_function_signature(fout, finfo, false); |
| 13471 |
|
| 13472 |
qual_funcsig = psprintf("%s.%s", |
| 13473 |
fmtId(finfo->dobj.namespace->dobj.name), |
| 13474 |
funcsig); |
| 13475 |
|
| 13476 |
if (prokind[0] == PROKIND_PROCEDURE) |
| 13477 |
keyword = "PROCEDURE"; |
| 13478 |
else |
| 13479 |
keyword = "FUNCTION"; /* works for window functions too */ |
| 13480 |
|
| 13481 |
appendPQExpBuffer(delqry, "DROP %s %s;\n", |
| 13482 |
keyword, qual_funcsig); |
| 13483 |
|
| 13484 |
appendPQExpBuffer(q, "CREATE %s %s.%s", |
| 13485 |
keyword, |
| 13486 |
fmtId(finfo->dobj.namespace->dobj.name), |
| 13487 |
funcfullsig ? funcfullsig : |
| 13488 |
funcsig); |
| 13489 |
|
| 13490 |
if (prokind[0] == PROKIND_PROCEDURE) |
| 13491 |
/* no result type to output */ ; |
| 13492 |
else if (funcresult) |
| 13493 |
appendPQExpBuffer(q, " RETURNS %s", funcresult); |
| 13494 |
else |
| 13495 |
appendPQExpBuffer(q, " RETURNS %s%s", |
| 13496 |
(proretset[0] == 't') ? "SETOF " : "", |
| 13497 |
getFormattedTypeName(fout, finfo->prorettype, |
| 13498 |
zeroIsError)); |
| 13499 |
|
| 13500 |
appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname)); |
| 13501 |
|
| 13502 |
if (*protrftypes) |
| 13503 |
{ |
| 13504 |
Oid *typeids = parseOidArray(protrftypes, -1); |
| 13505 |
|
| 13506 |
appendPQExpBufferStr(q, " TRANSFORM "); |
| 13507 |
for (int i = 0; typeids[i]; i++) |
| 13508 |
{ |
| 13509 |
if (i != 0) |
| 13510 |
appendPQExpBufferStr(q, ", "); |
| 13511 |
appendPQExpBuffer(q, "FOR TYPE %s", |
| 13512 |
getFormattedTypeName(fout, typeids[i], zeroAsNone)); |
| 13513 |
} |
| 13514 |
|
| 13515 |
free(typeids); |
| 13516 |
} |
| 13517 |
|
| 13518 |
if (prokind[0] == PROKIND_WINDOW) |
| 13519 |
appendPQExpBufferStr(q, " WINDOW"); |
| 13520 |
|
| 13521 |
if (provolatile[0] != PROVOLATILE_VOLATILE) |
| 13522 |
{ |
| 13523 |
if (provolatile[0] == PROVOLATILE_IMMUTABLE) |
| 13524 |
appendPQExpBufferStr(q, " IMMUTABLE"); |
| 13525 |
else if (provolatile[0] == PROVOLATILE_STABLE) |
| 13526 |
appendPQExpBufferStr(q, " STABLE"); |
| 13527 |
else if (provolatile[0] != PROVOLATILE_VOLATILE) |
| 13528 |
pg_fatal("unrecognized provolatile value for function \"%s\"", |
| 13529 |
finfo->dobj.name); |
| 13530 |
} |
| 13531 |
|
| 13532 |
if (proisstrict[0] == 't') |
| 13533 |
appendPQExpBufferStr(q, " STRICT"); |
| 13534 |
|
| 13535 |
if (prosecdef[0] == 't') |
| 13536 |
appendPQExpBufferStr(q, " SECURITY DEFINER"); |
| 13537 |
|
| 13538 |
if (proleakproof[0] == 't') |
| 13539 |
appendPQExpBufferStr(q, " LEAKPROOF"); |
| 13540 |
|
| 13541 |
/* |
| 13542 |
* COST and ROWS are emitted only if present and not default, so as not to |
| 13543 |
* break backwards-compatibility of the dump without need. Keep this code |
| 13544 |
* in sync with the defaults in functioncmds.c. |
| 13545 |
*/ |
| 13546 |
if (strcmp(procost, "0") != 0) |
| 13547 |
{ |
| 13548 |
if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0) |
| 13549 |
{ |
| 13550 |
/* default cost is 1 */ |
| 13551 |
if (strcmp(procost, "1") != 0) |
| 13552 |
appendPQExpBuffer(q, " COST %s", procost); |
| 13553 |
} |
| 13554 |
else |
| 13555 |
{ |
| 13556 |
/* default cost is 100 */ |
| 13557 |
if (strcmp(procost, "100") != 0) |
| 13558 |
appendPQExpBuffer(q, " COST %s", procost); |
| 13559 |
} |
| 13560 |
} |
| 13561 |
if (proretset[0] == 't' && |
| 13562 |
strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0) |
| 13563 |
appendPQExpBuffer(q, " ROWS %s", prorows); |
| 13564 |
|
| 13565 |
if (strcmp(prosupport, "-") != 0) |
| 13566 |
{ |
| 13567 |
/* We rely on regprocout to provide quoting and qualification */ |
| 13568 |
appendPQExpBuffer(q, " SUPPORT %s", prosupport); |
| 13569 |
} |
| 13570 |
|
| 13571 |
if (proparallel[0] != PROPARALLEL_UNSAFE) |
| 13572 |
{ |
| 13573 |
if (proparallel[0] == PROPARALLEL_SAFE) |
| 13574 |
appendPQExpBufferStr(q, " PARALLEL SAFE"); |
| 13575 |
else if (proparallel[0] == PROPARALLEL_RESTRICTED) |
| 13576 |
appendPQExpBufferStr(q, " PARALLEL RESTRICTED"); |
| 13577 |
else if (proparallel[0] != PROPARALLEL_UNSAFE) |
| 13578 |
pg_fatal("unrecognized proparallel value for function \"%s\"", |
| 13579 |
finfo->dobj.name); |
| 13580 |
} |
| 13581 |
|
| 13582 |
for (int i = 0; i < nconfigitems; i++) |
| 13583 |
{ |
| 13584 |
/* we feel free to scribble on configitems[] here */ |
| 13585 |
char *configitem = configitems[i]; |
| 13586 |
char *pos; |
| 13587 |
|
| 13588 |
pos = strchr(configitem, '='); |
| 13589 |
if (pos == NULL) |
| 13590 |
continue; |
| 13591 |
*pos++ = '\0'; |
| 13592 |
appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem)); |
| 13593 |
|
| 13594 |
/* |
| 13595 |
* Variables that are marked GUC_LIST_QUOTE were already fully quoted |
| 13596 |
* by flatten_set_variable_args() before they were put into the |
| 13597 |
* proconfig array. However, because the quoting rules used there |
| 13598 |
* aren't exactly like SQL's, we have to break the list value apart |
| 13599 |
* and then quote the elements as string literals. (The elements may |
| 13600 |
* be double-quoted as-is, but we can't just feed them to the SQL |
| 13601 |
* parser; it would do the wrong thing with elements that are |
| 13602 |
* zero-length or longer than NAMEDATALEN.) |
| 13603 |
* |
| 13604 |
* Variables that are not so marked should just be emitted as simple |
| 13605 |
* string literals. If the variable is not known to |
| 13606 |
* variable_is_guc_list_quote(), we'll do that; this makes it unsafe |
| 13607 |
* to use GUC_LIST_QUOTE for extension variables. |
| 13608 |
*/ |
| 13609 |
if (variable_is_guc_list_quote(configitem)) |
| 13610 |
{ |
| 13611 |
char **namelist; |
| 13612 |
char **nameptr; |
| 13613 |
|
| 13614 |
/* Parse string into list of identifiers */ |
| 13615 |
/* this shouldn't fail really */ |
| 13616 |
if (SplitGUCList(pos, ',', &namelist)) |
| 13617 |
{ |
| 13618 |
for (nameptr = namelist; *nameptr; nameptr++) |
| 13619 |
{ |
| 13620 |
if (nameptr != namelist) |
| 13621 |
appendPQExpBufferStr(q, ", "); |
| 13622 |
appendStringLiteralAH(q, *nameptr, fout); |
| 13623 |
} |
| 13624 |
} |
| 13625 |
pg_free(namelist); |
| 13626 |
} |
| 13627 |
else |
| 13628 |
appendStringLiteralAH(q, pos, fout); |
| 13629 |
} |
| 13630 |
|
| 13631 |
appendPQExpBuffer(q, "\n %s;\n", asPart->data); |
| 13632 |
|
| 13633 |
append_depends_on_extension(fout, q, &finfo->dobj, |
| 13634 |
"pg_catalog.pg_proc", keyword, |
| 13635 |
qual_funcsig); |
| 13636 |
|
| 13637 |
if (dopt->binary_upgrade) |
| 13638 |
binary_upgrade_extension_member(q, &finfo->dobj, |
| 13639 |
keyword, funcsig, |
| 13640 |
finfo->dobj.namespace->dobj.name); |
| 13641 |
|
| 13642 |
if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 13643 |
ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId, |
| 13644 |
ARCHIVE_OPTS(.tag = funcsig_tag, |
| 13645 |
.namespace = finfo->dobj.namespace->dobj.name, |
| 13646 |
.owner = finfo->rolname, |
| 13647 |
.description = keyword, |
| 13648 |
.section = finfo->postponed_def ? |
| 13649 |
SECTION_POST_DATA : SECTION_PRE_DATA, |
| 13650 |
.createStmt = q->data, |
| 13651 |
.dropStmt = delqry->data)); |
| 13652 |
|
| 13653 |
/* Dump Function Comments and Security Labels */ |
| 13654 |
if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 13655 |
dumpComment(fout, keyword, funcsig, |
| 13656 |
finfo->dobj.namespace->dobj.name, finfo->rolname, |
| 13657 |
finfo->dobj.catId, 0, finfo->dobj.dumpId); |
| 13658 |
|
| 13659 |
if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 13660 |
dumpSecLabel(fout, keyword, funcsig, |
| 13661 |
finfo->dobj.namespace->dobj.name, finfo->rolname, |
| 13662 |
finfo->dobj.catId, 0, finfo->dobj.dumpId); |
| 13663 |
|
| 13664 |
if (finfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 13665 |
dumpACL(fout, finfo->dobj.dumpId, InvalidDumpId, keyword, |
| 13666 |
funcsig, NULL, |
| 13667 |
finfo->dobj.namespace->dobj.name, |
| 13668 |
NULL, finfo->rolname, &finfo->dacl); |
| 13669 |
|
| 13670 |
PQclear(res); |
| 13671 |
|
| 13672 |
destroyPQExpBuffer(query); |
| 13673 |
destroyPQExpBuffer(q); |
| 13674 |
destroyPQExpBuffer(delqry); |
| 13675 |
destroyPQExpBuffer(asPart); |
| 13676 |
free(funcsig); |
| 13677 |
free(funcfullsig); |
| 13678 |
free(funcsig_tag); |
| 13679 |
free(qual_funcsig); |
| 13680 |
free(configitems); |
| 13681 |
} |
| 13682 |
|
| 13683 |
|
| 13684 |
/* |
| 13685 |
* Dump a user-defined cast |
| 13686 |
*/ |
| 13687 |
static void |
| 13688 |
dumpCast(Archive *fout, const CastInfo *cast) |
| 13689 |
{ |
| 13690 |
DumpOptions *dopt = fout->dopt; |
| 13691 |
PQExpBuffer defqry; |
| 13692 |
PQExpBuffer delqry; |
| 13693 |
PQExpBuffer labelq; |
| 13694 |
PQExpBuffer castargs; |
| 13695 |
FuncInfo *funcInfo = NULL; |
| 13696 |
const char *sourceType; |
| 13697 |
const char *targetType; |
| 13698 |
|
| 13699 |
/* Do nothing if not dumping schema */ |
| 13700 |
if (!dopt->dumpSchema) |
| 13701 |
return; |
| 13702 |
|
| 13703 |
/* Cannot dump if we don't have the cast function's info */ |
| 13704 |
if (OidIsValid(cast->castfunc)) |
| 13705 |
{ |
| 13706 |
funcInfo = findFuncByOid(cast->castfunc); |
| 13707 |
if (funcInfo == NULL) |
| 13708 |
pg_fatal("could not find function definition for function with OID %u", |
| 13709 |
cast->castfunc); |
| 13710 |
} |
| 13711 |
|
| 13712 |
defqry = createPQExpBuffer(); |
| 13713 |
delqry = createPQExpBuffer(); |
| 13714 |
labelq = createPQExpBuffer(); |
| 13715 |
castargs = createPQExpBuffer(); |
| 13716 |
|
| 13717 |
sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone); |
| 13718 |
targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone); |
| 13719 |
appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n", |
| 13720 |
sourceType, targetType); |
| 13721 |
|
| 13722 |
appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ", |
| 13723 |
sourceType, targetType); |
| 13724 |
|
| 13725 |
switch (cast->castmethod) |
| 13726 |
{ |
| 13727 |
case COERCION_METHOD_BINARY: |
| 13728 |
appendPQExpBufferStr(defqry, "WITHOUT FUNCTION"); |
| 13729 |
break; |
| 13730 |
case COERCION_METHOD_INOUT: |
| 13731 |
appendPQExpBufferStr(defqry, "WITH INOUT"); |
| 13732 |
break; |
| 13733 |
case COERCION_METHOD_FUNCTION: |
| 13734 |
if (funcInfo) |
| 13735 |
{ |
| 13736 |
char *fsig = format_function_signature(fout, funcInfo, true); |
| 13737 |
|
| 13738 |
/* |
| 13739 |
* Always qualify the function name (format_function_signature |
| 13740 |
* won't qualify it). |
| 13741 |
*/ |
| 13742 |
appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s", |
| 13743 |
fmtId(funcInfo->dobj.namespace->dobj.name), fsig); |
| 13744 |
free(fsig); |
| 13745 |
} |
| 13746 |
else |
| 13747 |
pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field"); |
| 13748 |
break; |
| 13749 |
default: |
| 13750 |
pg_log_warning("bogus value in pg_cast.castmethod field"); |
| 13751 |
} |
| 13752 |
|
| 13753 |
if (cast->castcontext == 'a') |
| 13754 |
appendPQExpBufferStr(defqry, " AS ASSIGNMENT"); |
| 13755 |
else if (cast->castcontext == 'i') |
| 13756 |
appendPQExpBufferStr(defqry, " AS IMPLICIT"); |
| 13757 |
appendPQExpBufferStr(defqry, ";\n"); |
| 13758 |
|
| 13759 |
appendPQExpBuffer(labelq, "CAST (%s AS %s)", |
| 13760 |
sourceType, targetType); |
| 13761 |
|
| 13762 |
appendPQExpBuffer(castargs, "(%s AS %s)", |
| 13763 |
sourceType, targetType); |
| 13764 |
|
| 13765 |
if (dopt->binary_upgrade) |
| 13766 |
binary_upgrade_extension_member(defqry, &cast->dobj, |
| 13767 |
"CAST", castargs->data, NULL); |
| 13768 |
|
| 13769 |
if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 13770 |
ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId, |
| 13771 |
ARCHIVE_OPTS(.tag = labelq->data, |
| 13772 |
.description = "CAST", |
| 13773 |
.section = SECTION_PRE_DATA, |
| 13774 |
.createStmt = defqry->data, |
| 13775 |
.dropStmt = delqry->data)); |
| 13776 |
|
| 13777 |
/* Dump Cast Comments */ |
| 13778 |
if (cast->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 13779 |
dumpComment(fout, "CAST", castargs->data, |
| 13780 |
NULL, "", |
| 13781 |
cast->dobj.catId, 0, cast->dobj.dumpId); |
| 13782 |
|
| 13783 |
destroyPQExpBuffer(defqry); |
| 13784 |
destroyPQExpBuffer(delqry); |
| 13785 |
destroyPQExpBuffer(labelq); |
| 13786 |
destroyPQExpBuffer(castargs); |
| 13787 |
} |
| 13788 |
|
| 13789 |
/* |
| 13790 |
* Dump a transform |
| 13791 |
*/ |
| 13792 |
static void |
| 13793 |
dumpTransform(Archive *fout, const TransformInfo *transform) |
| 13794 |
{ |
| 13795 |
DumpOptions *dopt = fout->dopt; |
| 13796 |
PQExpBuffer defqry; |
| 13797 |
PQExpBuffer delqry; |
| 13798 |
PQExpBuffer labelq; |
| 13799 |
PQExpBuffer transformargs; |
| 13800 |
FuncInfo *fromsqlFuncInfo = NULL; |
| 13801 |
FuncInfo *tosqlFuncInfo = NULL; |
| 13802 |
char *lanname; |
| 13803 |
const char *transformType; |
| 13804 |
|
| 13805 |
/* Do nothing if not dumping schema */ |
| 13806 |
if (!dopt->dumpSchema) |
| 13807 |
return; |
| 13808 |
|
| 13809 |
/* Cannot dump if we don't have the transform functions' info */ |
| 13810 |
if (OidIsValid(transform->trffromsql)) |
| 13811 |
{ |
| 13812 |
fromsqlFuncInfo = findFuncByOid(transform->trffromsql); |
| 13813 |
if (fromsqlFuncInfo == NULL) |
| 13814 |
pg_fatal("could not find function definition for function with OID %u", |
| 13815 |
transform->trffromsql); |
| 13816 |
} |
| 13817 |
if (OidIsValid(transform->trftosql)) |
| 13818 |
{ |
| 13819 |
tosqlFuncInfo = findFuncByOid(transform->trftosql); |
| 13820 |
if (tosqlFuncInfo == NULL) |
| 13821 |
pg_fatal("could not find function definition for function with OID %u", |
| 13822 |
transform->trftosql); |
| 13823 |
} |
| 13824 |
|
| 13825 |
defqry = createPQExpBuffer(); |
| 13826 |
delqry = createPQExpBuffer(); |
| 13827 |
labelq = createPQExpBuffer(); |
| 13828 |
transformargs = createPQExpBuffer(); |
| 13829 |
|
| 13830 |
lanname = get_language_name(fout, transform->trflang); |
| 13831 |
transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone); |
| 13832 |
|
| 13833 |
appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n", |
| 13834 |
transformType, lanname); |
| 13835 |
|
| 13836 |
appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (", |
| 13837 |
transformType, lanname); |
| 13838 |
|
| 13839 |
if (!transform->trffromsql && !transform->trftosql) |
| 13840 |
pg_log_warning("bogus transform definition, at least one of trffromsql and trftosql should be nonzero"); |
| 13841 |
|
| 13842 |
if (transform->trffromsql) |
| 13843 |
{ |
| 13844 |
if (fromsqlFuncInfo) |
| 13845 |
{ |
| 13846 |
char *fsig = format_function_signature(fout, fromsqlFuncInfo, true); |
| 13847 |
|
| 13848 |
/* |
| 13849 |
* Always qualify the function name (format_function_signature |
| 13850 |
* won't qualify it). |
| 13851 |
*/ |
| 13852 |
appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s", |
| 13853 |
fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig); |
| 13854 |
free(fsig); |
| 13855 |
} |
| 13856 |
else |
| 13857 |
pg_log_warning("bogus value in pg_transform.trffromsql field"); |
| 13858 |
} |
| 13859 |
|
| 13860 |
if (transform->trftosql) |
| 13861 |
{ |
| 13862 |
if (transform->trffromsql) |
| 13863 |
appendPQExpBufferStr(defqry, ", "); |
| 13864 |
|
| 13865 |
if (tosqlFuncInfo) |
| 13866 |
{ |
| 13867 |
char *fsig = format_function_signature(fout, tosqlFuncInfo, true); |
| 13868 |
|
| 13869 |
/* |
| 13870 |
* Always qualify the function name (format_function_signature |
| 13871 |
* won't qualify it). |
| 13872 |
*/ |
| 13873 |
appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s", |
| 13874 |
fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig); |
| 13875 |
free(fsig); |
| 13876 |
} |
| 13877 |
else |
| 13878 |
pg_log_warning("bogus value in pg_transform.trftosql field"); |
| 13879 |
} |
| 13880 |
|
| 13881 |
appendPQExpBufferStr(defqry, ");\n"); |
| 13882 |
|
| 13883 |
appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s", |
| 13884 |
transformType, lanname); |
| 13885 |
|
| 13886 |
appendPQExpBuffer(transformargs, "FOR %s LANGUAGE %s", |
| 13887 |
transformType, lanname); |
| 13888 |
|
| 13889 |
if (dopt->binary_upgrade) |
| 13890 |
binary_upgrade_extension_member(defqry, &transform->dobj, |
| 13891 |
"TRANSFORM", transformargs->data, NULL); |
| 13892 |
|
| 13893 |
if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 13894 |
ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId, |
| 13895 |
ARCHIVE_OPTS(.tag = labelq->data, |
| 13896 |
.description = "TRANSFORM", |
| 13897 |
.section = SECTION_PRE_DATA, |
| 13898 |
.createStmt = defqry->data, |
| 13899 |
.dropStmt = delqry->data, |
| 13900 |
.deps = transform->dobj.dependencies, |
| 13901 |
.nDeps = transform->dobj.nDeps)); |
| 13902 |
|
| 13903 |
/* Dump Transform Comments */ |
| 13904 |
if (transform->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 13905 |
dumpComment(fout, "TRANSFORM", transformargs->data, |
| 13906 |
NULL, "", |
| 13907 |
transform->dobj.catId, 0, transform->dobj.dumpId); |
| 13908 |
|
| 13909 |
free(lanname); |
| 13910 |
destroyPQExpBuffer(defqry); |
| 13911 |
destroyPQExpBuffer(delqry); |
| 13912 |
destroyPQExpBuffer(labelq); |
| 13913 |
destroyPQExpBuffer(transformargs); |
| 13914 |
} |
| 13915 |
|
| 13916 |
|
| 13917 |
/* |
| 13918 |
* dumpOpr |
| 13919 |
* write out a single operator definition |
| 13920 |
*/ |
| 13921 |
static void |
| 13922 |
dumpOpr(Archive *fout, const OprInfo *oprinfo) |
| 13923 |
{ |
| 13924 |
DumpOptions *dopt = fout->dopt; |
| 13925 |
PQExpBuffer query; |
| 13926 |
PQExpBuffer q; |
| 13927 |
PQExpBuffer delq; |
| 13928 |
PQExpBuffer oprid; |
| 13929 |
PQExpBuffer details; |
| 13930 |
PGresult *res; |
| 13931 |
int i_oprkind; |
| 13932 |
int i_oprcode; |
| 13933 |
int i_oprleft; |
| 13934 |
int i_oprright; |
| 13935 |
int i_oprcom; |
| 13936 |
int i_oprnegate; |
| 13937 |
int i_oprrest; |
| 13938 |
int i_oprjoin; |
| 13939 |
int i_oprcanmerge; |
| 13940 |
int i_oprcanhash; |
| 13941 |
char *oprkind; |
| 13942 |
char *oprcode; |
| 13943 |
char *oprleft; |
| 13944 |
char *oprright; |
| 13945 |
char *oprcom; |
| 13946 |
char *oprnegate; |
| 13947 |
char *oprrest; |
| 13948 |
char *oprjoin; |
| 13949 |
char *oprcanmerge; |
| 13950 |
char *oprcanhash; |
| 13951 |
char *oprregproc; |
| 13952 |
char *oprref; |
| 13953 |
|
| 13954 |
/* Do nothing if not dumping schema */ |
| 13955 |
if (!dopt->dumpSchema) |
| 13956 |
return; |
| 13957 |
|
| 13958 |
/* |
| 13959 |
* some operators are invalid because they were the result of user |
| 13960 |
* defining operators before commutators exist |
| 13961 |
*/ |
| 13962 |
if (!OidIsValid(oprinfo->oprcode)) |
| 13963 |
return; |
| 13964 |
|
| 13965 |
query = createPQExpBuffer(); |
| 13966 |
q = createPQExpBuffer(); |
| 13967 |
delq = createPQExpBuffer(); |
| 13968 |
oprid = createPQExpBuffer(); |
| 13969 |
details = createPQExpBuffer(); |
| 13970 |
|
| 13971 |
if (!fout->is_prepared[PREPQUERY_DUMPOPR]) |
| 13972 |
{ |
| 13973 |
/* Set up query for operator-specific details */ |
| 13974 |
appendPQExpBufferStr(query, |
| 13975 |
"PREPARE dumpOpr(pg_catalog.oid) AS\n" |
| 13976 |
"SELECT oprkind, " |
| 13977 |
"oprcode::pg_catalog.regprocedure, " |
| 13978 |
"oprleft::pg_catalog.regtype, " |
| 13979 |
"oprright::pg_catalog.regtype, " |
| 13980 |
"oprcom, " |
| 13981 |
"oprnegate, " |
| 13982 |
"oprrest::pg_catalog.regprocedure, " |
| 13983 |
"oprjoin::pg_catalog.regprocedure, " |
| 13984 |
"oprcanmerge, oprcanhash " |
| 13985 |
"FROM pg_catalog.pg_operator " |
| 13986 |
"WHERE oid = $1"); |
| 13987 |
|
| 13988 |
ExecuteSqlStatement(fout, query->data); |
| 13989 |
|
| 13990 |
fout->is_prepared[PREPQUERY_DUMPOPR] = true; |
| 13991 |
} |
| 13992 |
|
| 13993 |
printfPQExpBuffer(query, |
| 13994 |
"EXECUTE dumpOpr('%u')", |
| 13995 |
oprinfo->dobj.catId.oid); |
| 13996 |
|
| 13997 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 13998 |
|
| 13999 |
i_oprkind = PQfnumber(res, "oprkind"); |
| 14000 |
i_oprcode = PQfnumber(res, "oprcode"); |
| 14001 |
i_oprleft = PQfnumber(res, "oprleft"); |
| 14002 |
i_oprright = PQfnumber(res, "oprright"); |
| 14003 |
i_oprcom = PQfnumber(res, "oprcom"); |
| 14004 |
i_oprnegate = PQfnumber(res, "oprnegate"); |
| 14005 |
i_oprrest = PQfnumber(res, "oprrest"); |
| 14006 |
i_oprjoin = PQfnumber(res, "oprjoin"); |
| 14007 |
i_oprcanmerge = PQfnumber(res, "oprcanmerge"); |
| 14008 |
i_oprcanhash = PQfnumber(res, "oprcanhash"); |
| 14009 |
|
| 14010 |
oprkind = PQgetvalue(res, 0, i_oprkind); |
| 14011 |
oprcode = PQgetvalue(res, 0, i_oprcode); |
| 14012 |
oprleft = PQgetvalue(res, 0, i_oprleft); |
| 14013 |
oprright = PQgetvalue(res, 0, i_oprright); |
| 14014 |
oprcom = PQgetvalue(res, 0, i_oprcom); |
| 14015 |
oprnegate = PQgetvalue(res, 0, i_oprnegate); |
| 14016 |
oprrest = PQgetvalue(res, 0, i_oprrest); |
| 14017 |
oprjoin = PQgetvalue(res, 0, i_oprjoin); |
| 14018 |
oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge); |
| 14019 |
oprcanhash = PQgetvalue(res, 0, i_oprcanhash); |
| 14020 |
|
| 14021 |
/* In PG14 upwards postfix operator support does not exist anymore. */ |
| 14022 |
if (strcmp(oprkind, "r") == 0) |
| 14023 |
pg_log_warning("postfix operators are not supported anymore (operator \"%s\")", |
| 14024 |
oprcode); |
| 14025 |
|
| 14026 |
oprregproc = convertRegProcReference(oprcode); |
| 14027 |
if (oprregproc) |
| 14028 |
{ |
| 14029 |
appendPQExpBuffer(details, " FUNCTION = %s", oprregproc); |
| 14030 |
free(oprregproc); |
| 14031 |
} |
| 14032 |
|
| 14033 |
appendPQExpBuffer(oprid, "%s (", |
| 14034 |
oprinfo->dobj.name); |
| 14035 |
|
| 14036 |
/* |
| 14037 |
* right unary means there's a left arg and left unary means there's a |
| 14038 |
* right arg. (Although the "r" case is dead code for PG14 and later, |
| 14039 |
* continue to support it in case we're dumping from an old server.) |
| 14040 |
*/ |
| 14041 |
if (strcmp(oprkind, "r") == 0 || |
| 14042 |
strcmp(oprkind, "b") == 0) |
| 14043 |
{ |
| 14044 |
appendPQExpBuffer(details, ",\n LEFTARG = %s", oprleft); |
| 14045 |
appendPQExpBufferStr(oprid, oprleft); |
| 14046 |
} |
| 14047 |
else |
| 14048 |
appendPQExpBufferStr(oprid, "NONE"); |
| 14049 |
|
| 14050 |
if (strcmp(oprkind, "l") == 0 || |
| 14051 |
strcmp(oprkind, "b") == 0) |
| 14052 |
{ |
| 14053 |
appendPQExpBuffer(details, ",\n RIGHTARG = %s", oprright); |
| 14054 |
appendPQExpBuffer(oprid, ", %s)", oprright); |
| 14055 |
} |
| 14056 |
else |
| 14057 |
appendPQExpBufferStr(oprid, ", NONE)"); |
| 14058 |
|
| 14059 |
oprref = getFormattedOperatorName(oprcom); |
| 14060 |
if (oprref) |
| 14061 |
{ |
| 14062 |
appendPQExpBuffer(details, ",\n COMMUTATOR = %s", oprref); |
| 14063 |
free(oprref); |
| 14064 |
} |
| 14065 |
|
| 14066 |
oprref = getFormattedOperatorName(oprnegate); |
| 14067 |
if (oprref) |
| 14068 |
{ |
| 14069 |
appendPQExpBuffer(details, ",\n NEGATOR = %s", oprref); |
| 14070 |
free(oprref); |
| 14071 |
} |
| 14072 |
|
| 14073 |
if (strcmp(oprcanmerge, "t") == 0) |
| 14074 |
appendPQExpBufferStr(details, ",\n MERGES"); |
| 14075 |
|
| 14076 |
if (strcmp(oprcanhash, "t") == 0) |
| 14077 |
appendPQExpBufferStr(details, ",\n HASHES"); |
| 14078 |
|
| 14079 |
oprregproc = convertRegProcReference(oprrest); |
| 14080 |
if (oprregproc) |
| 14081 |
{ |
| 14082 |
appendPQExpBuffer(details, ",\n RESTRICT = %s", oprregproc); |
| 14083 |
free(oprregproc); |
| 14084 |
} |
| 14085 |
|
| 14086 |
oprregproc = convertRegProcReference(oprjoin); |
| 14087 |
if (oprregproc) |
| 14088 |
{ |
| 14089 |
appendPQExpBuffer(details, ",\n JOIN = %s", oprregproc); |
| 14090 |
free(oprregproc); |
| 14091 |
} |
| 14092 |
|
| 14093 |
appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n", |
| 14094 |
fmtId(oprinfo->dobj.namespace->dobj.name), |
| 14095 |
oprid->data); |
| 14096 |
|
| 14097 |
appendPQExpBuffer(q, "CREATE OPERATOR %s.%s (\n%s\n);\n", |
| 14098 |
fmtId(oprinfo->dobj.namespace->dobj.name), |
| 14099 |
oprinfo->dobj.name, details->data); |
| 14100 |
|
| 14101 |
if (dopt->binary_upgrade) |
| 14102 |
binary_upgrade_extension_member(q, &oprinfo->dobj, |
| 14103 |
"OPERATOR", oprid->data, |
| 14104 |
oprinfo->dobj.namespace->dobj.name); |
| 14105 |
|
| 14106 |
if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 14107 |
ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId, |
| 14108 |
ARCHIVE_OPTS(.tag = oprinfo->dobj.name, |
| 14109 |
.namespace = oprinfo->dobj.namespace->dobj.name, |
| 14110 |
.owner = oprinfo->rolname, |
| 14111 |
.description = "OPERATOR", |
| 14112 |
.section = SECTION_PRE_DATA, |
| 14113 |
.createStmt = q->data, |
| 14114 |
.dropStmt = delq->data)); |
| 14115 |
|
| 14116 |
/* Dump Operator Comments */ |
| 14117 |
if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 14118 |
dumpComment(fout, "OPERATOR", oprid->data, |
| 14119 |
oprinfo->dobj.namespace->dobj.name, oprinfo->rolname, |
| 14120 |
oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId); |
| 14121 |
|
| 14122 |
PQclear(res); |
| 14123 |
|
| 14124 |
destroyPQExpBuffer(query); |
| 14125 |
destroyPQExpBuffer(q); |
| 14126 |
destroyPQExpBuffer(delq); |
| 14127 |
destroyPQExpBuffer(oprid); |
| 14128 |
destroyPQExpBuffer(details); |
| 14129 |
} |
| 14130 |
|
| 14131 |
/* |
| 14132 |
* Convert a function reference obtained from pg_operator |
| 14133 |
* |
| 14134 |
* Returns allocated string of what to print, or NULL if function references |
| 14135 |
* is InvalidOid. Returned string is expected to be free'd by the caller. |
| 14136 |
* |
| 14137 |
* The input is a REGPROCEDURE display; we have to strip the argument-types |
| 14138 |
* part. |
| 14139 |
*/ |
| 14140 |
static char * |
| 14141 |
convertRegProcReference(const char *proc) |
| 14142 |
{ |
| 14143 |
char *name; |
| 14144 |
char *paren; |
| 14145 |
bool inquote; |
| 14146 |
|
| 14147 |
/* In all cases "-" means a null reference */ |
| 14148 |
if (strcmp(proc, "-") == 0) |
| 14149 |
return NULL; |
| 14150 |
|
| 14151 |
name = pg_strdup(proc); |
| 14152 |
/* find non-double-quoted left paren */ |
| 14153 |
inquote = false; |
| 14154 |
for (paren = name; *paren; paren++) |
| 14155 |
{ |
| 14156 |
if (*paren == '(' && !inquote) |
| 14157 |
{ |
| 14158 |
*paren = '\0'; |
| 14159 |
break; |
| 14160 |
} |
| 14161 |
if (*paren == '"') |
| 14162 |
inquote = !inquote; |
| 14163 |
} |
| 14164 |
return name; |
| 14165 |
} |
| 14166 |
|
| 14167 |
/* |
| 14168 |
* getFormattedOperatorName - retrieve the operator name for the |
| 14169 |
* given operator OID (presented in string form). |
| 14170 |
* |
| 14171 |
* Returns an allocated string, or NULL if the given OID is invalid. |
| 14172 |
* Caller is responsible for free'ing result string. |
| 14173 |
* |
| 14174 |
* What we produce has the format "OPERATOR(schema.oprname)". This is only |
| 14175 |
* useful in commands where the operator's argument types can be inferred from |
| 14176 |
* context. We always schema-qualify the name, though. The predecessor to |
| 14177 |
* this code tried to skip the schema qualification if possible, but that led |
| 14178 |
* to wrong results in corner cases, such as if an operator and its negator |
| 14179 |
* are in different schemas. |
| 14180 |
*/ |
| 14181 |
static char * |
| 14182 |
getFormattedOperatorName(const char *oproid) |
| 14183 |
{ |
| 14184 |
OprInfo *oprInfo; |
| 14185 |
|
| 14186 |
/* In all cases "0" means a null reference */ |
| 14187 |
if (strcmp(oproid, "0") == 0) |
| 14188 |
return NULL; |
| 14189 |
|
| 14190 |
oprInfo = findOprByOid(atooid(oproid)); |
| 14191 |
if (oprInfo == NULL) |
| 14192 |
{ |
| 14193 |
pg_log_warning("could not find operator with OID %s", |
| 14194 |
oproid); |
| 14195 |
return NULL; |
| 14196 |
} |
| 14197 |
|
| 14198 |
return psprintf("OPERATOR(%s.%s)", |
| 14199 |
fmtId(oprInfo->dobj.namespace->dobj.name), |
| 14200 |
oprInfo->dobj.name); |
| 14201 |
} |
| 14202 |
|
| 14203 |
/* |
| 14204 |
* Convert a function OID obtained from pg_ts_parser or pg_ts_template |
| 14205 |
* |
| 14206 |
* It is sufficient to use REGPROC rather than REGPROCEDURE, since the |
| 14207 |
* argument lists of these functions are predetermined. Note that the |
| 14208 |
* caller should ensure we are in the proper schema, because the results |
| 14209 |
* are search path dependent! |
| 14210 |
*/ |
| 14211 |
static char * |
| 14212 |
convertTSFunction(Archive *fout, Oid funcOid) |
| 14213 |
{ |
| 14214 |
char *result; |
| 14215 |
char query[128]; |
| 14216 |
PGresult *res; |
| 14217 |
|
| 14218 |
snprintf(query, sizeof(query), |
| 14219 |
"SELECT '%u'::pg_catalog.regproc", funcOid); |
| 14220 |
res = ExecuteSqlQueryForSingleRow(fout, query); |
| 14221 |
|
| 14222 |
result = pg_strdup(PQgetvalue(res, 0, 0)); |
| 14223 |
|
| 14224 |
PQclear(res); |
| 14225 |
|
| 14226 |
return result; |
| 14227 |
} |
| 14228 |
|
| 14229 |
/* |
| 14230 |
* dumpAccessMethod |
| 14231 |
* write out a single access method definition |
| 14232 |
*/ |
| 14233 |
static void |
| 14234 |
dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo) |
| 14235 |
{ |
| 14236 |
DumpOptions *dopt = fout->dopt; |
| 14237 |
PQExpBuffer q; |
| 14238 |
PQExpBuffer delq; |
| 14239 |
char *qamname; |
| 14240 |
|
| 14241 |
/* Do nothing if not dumping schema */ |
| 14242 |
if (!dopt->dumpSchema) |
| 14243 |
return; |
| 14244 |
|
| 14245 |
q = createPQExpBuffer(); |
| 14246 |
delq = createPQExpBuffer(); |
| 14247 |
|
| 14248 |
qamname = pg_strdup(fmtId(aminfo->dobj.name)); |
| 14249 |
|
| 14250 |
appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname); |
| 14251 |
|
| 14252 |
switch (aminfo->amtype) |
| 14253 |
{ |
| 14254 |
case AMTYPE_INDEX: |
| 14255 |
appendPQExpBufferStr(q, "TYPE INDEX "); |
| 14256 |
break; |
| 14257 |
case AMTYPE_TABLE: |
| 14258 |
appendPQExpBufferStr(q, "TYPE TABLE "); |
| 14259 |
break; |
| 14260 |
default: |
| 14261 |
pg_log_warning("invalid type \"%c\" of access method \"%s\"", |
| 14262 |
aminfo->amtype, qamname); |
| 14263 |
destroyPQExpBuffer(q); |
| 14264 |
destroyPQExpBuffer(delq); |
| 14265 |
free(qamname); |
| 14266 |
return; |
| 14267 |
} |
| 14268 |
|
| 14269 |
appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler); |
| 14270 |
|
| 14271 |
appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n", |
| 14272 |
qamname); |
| 14273 |
|
| 14274 |
if (dopt->binary_upgrade) |
| 14275 |
binary_upgrade_extension_member(q, &aminfo->dobj, |
| 14276 |
"ACCESS METHOD", qamname, NULL); |
| 14277 |
|
| 14278 |
if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 14279 |
ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId, |
| 14280 |
ARCHIVE_OPTS(.tag = aminfo->dobj.name, |
| 14281 |
.description = "ACCESS METHOD", |
| 14282 |
.section = SECTION_PRE_DATA, |
| 14283 |
.createStmt = q->data, |
| 14284 |
.dropStmt = delq->data)); |
| 14285 |
|
| 14286 |
/* Dump Access Method Comments */ |
| 14287 |
if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 14288 |
dumpComment(fout, "ACCESS METHOD", qamname, |
| 14289 |
NULL, "", |
| 14290 |
aminfo->dobj.catId, 0, aminfo->dobj.dumpId); |
| 14291 |
|
| 14292 |
destroyPQExpBuffer(q); |
| 14293 |
destroyPQExpBuffer(delq); |
| 14294 |
free(qamname); |
| 14295 |
} |
| 14296 |
|
| 14297 |
/* |
| 14298 |
* dumpOpclass |
| 14299 |
* write out a single operator class definition |
| 14300 |
*/ |
| 14301 |
static void |
| 14302 |
dumpOpclass(Archive *fout, const OpclassInfo *opcinfo) |
| 14303 |
{ |
| 14304 |
DumpOptions *dopt = fout->dopt; |
| 14305 |
PQExpBuffer query; |
| 14306 |
PQExpBuffer q; |
| 14307 |
PQExpBuffer delq; |
| 14308 |
PQExpBuffer nameusing; |
| 14309 |
PGresult *res; |
| 14310 |
int ntups; |
| 14311 |
int i_opcintype; |
| 14312 |
int i_opckeytype; |
| 14313 |
int i_opcdefault; |
| 14314 |
int i_opcfamily; |
| 14315 |
int i_opcfamilyname; |
| 14316 |
int i_opcfamilynsp; |
| 14317 |
int i_amname; |
| 14318 |
int i_amopstrategy; |
| 14319 |
int i_amopopr; |
| 14320 |
int i_sortfamily; |
| 14321 |
int i_sortfamilynsp; |
| 14322 |
int i_amprocnum; |
| 14323 |
int i_amproc; |
| 14324 |
int i_amproclefttype; |
| 14325 |
int i_amprocrighttype; |
| 14326 |
char *opcintype; |
| 14327 |
char *opckeytype; |
| 14328 |
char *opcdefault; |
| 14329 |
char *opcfamily; |
| 14330 |
char *opcfamilyname; |
| 14331 |
char *opcfamilynsp; |
| 14332 |
char *amname; |
| 14333 |
char *amopstrategy; |
| 14334 |
char *amopopr; |
| 14335 |
char *sortfamily; |
| 14336 |
char *sortfamilynsp; |
| 14337 |
char *amprocnum; |
| 14338 |
char *amproc; |
| 14339 |
char *amproclefttype; |
| 14340 |
char *amprocrighttype; |
| 14341 |
bool needComma; |
| 14342 |
int i; |
| 14343 |
|
| 14344 |
/* Do nothing if not dumping schema */ |
| 14345 |
if (!dopt->dumpSchema) |
| 14346 |
return; |
| 14347 |
|
| 14348 |
query = createPQExpBuffer(); |
| 14349 |
q = createPQExpBuffer(); |
| 14350 |
delq = createPQExpBuffer(); |
| 14351 |
nameusing = createPQExpBuffer(); |
| 14352 |
|
| 14353 |
/* Get additional fields from the pg_opclass row */ |
| 14354 |
appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, " |
| 14355 |
"opckeytype::pg_catalog.regtype, " |
| 14356 |
"opcdefault, opcfamily, " |
| 14357 |
"opfname AS opcfamilyname, " |
| 14358 |
"nspname AS opcfamilynsp, " |
| 14359 |
"(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname " |
| 14360 |
"FROM pg_catalog.pg_opclass c " |
| 14361 |
"LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily " |
| 14362 |
"LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace " |
| 14363 |
"WHERE c.oid = '%u'::pg_catalog.oid", |
| 14364 |
opcinfo->dobj.catId.oid); |
| 14365 |
|
| 14366 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 14367 |
|
| 14368 |
i_opcintype = PQfnumber(res, "opcintype"); |
| 14369 |
i_opckeytype = PQfnumber(res, "opckeytype"); |
| 14370 |
i_opcdefault = PQfnumber(res, "opcdefault"); |
| 14371 |
i_opcfamily = PQfnumber(res, "opcfamily"); |
| 14372 |
i_opcfamilyname = PQfnumber(res, "opcfamilyname"); |
| 14373 |
i_opcfamilynsp = PQfnumber(res, "opcfamilynsp"); |
| 14374 |
i_amname = PQfnumber(res, "amname"); |
| 14375 |
|
| 14376 |
/* opcintype may still be needed after we PQclear res */ |
| 14377 |
opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype)); |
| 14378 |
opckeytype = PQgetvalue(res, 0, i_opckeytype); |
| 14379 |
opcdefault = PQgetvalue(res, 0, i_opcdefault); |
| 14380 |
/* opcfamily will still be needed after we PQclear res */ |
| 14381 |
opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily)); |
| 14382 |
opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname); |
| 14383 |
opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp); |
| 14384 |
/* amname will still be needed after we PQclear res */ |
| 14385 |
amname = pg_strdup(PQgetvalue(res, 0, i_amname)); |
| 14386 |
|
| 14387 |
appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s", |
| 14388 |
fmtQualifiedDumpable(opcinfo)); |
| 14389 |
appendPQExpBuffer(delq, " USING %s;\n", |
| 14390 |
fmtId(amname)); |
| 14391 |
|
| 14392 |
/* Build the fixed portion of the CREATE command */ |
| 14393 |
appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ", |
| 14394 |
fmtQualifiedDumpable(opcinfo)); |
| 14395 |
if (strcmp(opcdefault, "t") == 0) |
| 14396 |
appendPQExpBufferStr(q, "DEFAULT "); |
| 14397 |
appendPQExpBuffer(q, "FOR TYPE %s USING %s", |
| 14398 |
opcintype, |
| 14399 |
fmtId(amname)); |
| 14400 |
if (strlen(opcfamilyname) > 0) |
| 14401 |
{ |
| 14402 |
appendPQExpBufferStr(q, " FAMILY "); |
| 14403 |
appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp)); |
| 14404 |
appendPQExpBufferStr(q, fmtId(opcfamilyname)); |
| 14405 |
} |
| 14406 |
appendPQExpBufferStr(q, " AS\n "); |
| 14407 |
|
| 14408 |
needComma = false; |
| 14409 |
|
| 14410 |
if (strcmp(opckeytype, "-") != 0) |
| 14411 |
{ |
| 14412 |
appendPQExpBuffer(q, "STORAGE %s", |
| 14413 |
opckeytype); |
| 14414 |
needComma = true; |
| 14415 |
} |
| 14416 |
|
| 14417 |
PQclear(res); |
| 14418 |
|
| 14419 |
/* |
| 14420 |
* Now fetch and print the OPERATOR entries (pg_amop rows). |
| 14421 |
* |
| 14422 |
* Print only those opfamily members that are tied to the opclass by |
| 14423 |
* pg_depend entries. |
| 14424 |
*/ |
| 14425 |
resetPQExpBuffer(query); |
| 14426 |
appendPQExpBuffer(query, "SELECT amopstrategy, " |
| 14427 |
"amopopr::pg_catalog.regoperator, " |
| 14428 |
"opfname AS sortfamily, " |
| 14429 |
"nspname AS sortfamilynsp " |
| 14430 |
"FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON " |
| 14431 |
"(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) " |
| 14432 |
"LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily " |
| 14433 |
"LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace " |
| 14434 |
"WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass " |
| 14435 |
"AND refobjid = '%u'::pg_catalog.oid " |
| 14436 |
"AND amopfamily = '%s'::pg_catalog.oid " |
| 14437 |
"ORDER BY amopstrategy", |
| 14438 |
opcinfo->dobj.catId.oid, |
| 14439 |
opcfamily); |
| 14440 |
|
| 14441 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 14442 |
|
| 14443 |
ntups = PQntuples(res); |
| 14444 |
|
| 14445 |
i_amopstrategy = PQfnumber(res, "amopstrategy"); |
| 14446 |
i_amopopr = PQfnumber(res, "amopopr"); |
| 14447 |
i_sortfamily = PQfnumber(res, "sortfamily"); |
| 14448 |
i_sortfamilynsp = PQfnumber(res, "sortfamilynsp"); |
| 14449 |
|
| 14450 |
for (i = 0; i < ntups; i++) |
| 14451 |
{ |
| 14452 |
amopstrategy = PQgetvalue(res, i, i_amopstrategy); |
| 14453 |
amopopr = PQgetvalue(res, i, i_amopopr); |
| 14454 |
sortfamily = PQgetvalue(res, i, i_sortfamily); |
| 14455 |
sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp); |
| 14456 |
|
| 14457 |
if (needComma) |
| 14458 |
appendPQExpBufferStr(q, " ,\n "); |
| 14459 |
|
| 14460 |
appendPQExpBuffer(q, "OPERATOR %s %s", |
| 14461 |
amopstrategy, amopopr); |
| 14462 |
|
| 14463 |
if (strlen(sortfamily) > 0) |
| 14464 |
{ |
| 14465 |
appendPQExpBufferStr(q, " FOR ORDER BY "); |
| 14466 |
appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp)); |
| 14467 |
appendPQExpBufferStr(q, fmtId(sortfamily)); |
| 14468 |
} |
| 14469 |
|
| 14470 |
needComma = true; |
| 14471 |
} |
| 14472 |
|
| 14473 |
PQclear(res); |
| 14474 |
|
| 14475 |
/* |
| 14476 |
* Now fetch and print the FUNCTION entries (pg_amproc rows). |
| 14477 |
* |
| 14478 |
* Print only those opfamily members that are tied to the opclass by |
| 14479 |
* pg_depend entries. |
| 14480 |
* |
| 14481 |
* We print the amproclefttype/amprocrighttype even though in most cases |
| 14482 |
* the backend could deduce the right values, because of the corner case |
| 14483 |
* of a btree sort support function for a cross-type comparison. |
| 14484 |
*/ |
| 14485 |
resetPQExpBuffer(query); |
| 14486 |
|
| 14487 |
appendPQExpBuffer(query, "SELECT amprocnum, " |
| 14488 |
"amproc::pg_catalog.regprocedure, " |
| 14489 |
"amproclefttype::pg_catalog.regtype, " |
| 14490 |
"amprocrighttype::pg_catalog.regtype " |
| 14491 |
"FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend " |
| 14492 |
"WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass " |
| 14493 |
"AND refobjid = '%u'::pg_catalog.oid " |
| 14494 |
"AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass " |
| 14495 |
"AND objid = ap.oid " |
| 14496 |
"ORDER BY amprocnum", |
| 14497 |
opcinfo->dobj.catId.oid); |
| 14498 |
|
| 14499 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 14500 |
|
| 14501 |
ntups = PQntuples(res); |
| 14502 |
|
| 14503 |
i_amprocnum = PQfnumber(res, "amprocnum"); |
| 14504 |
i_amproc = PQfnumber(res, "amproc"); |
| 14505 |
i_amproclefttype = PQfnumber(res, "amproclefttype"); |
| 14506 |
i_amprocrighttype = PQfnumber(res, "amprocrighttype"); |
| 14507 |
|
| 14508 |
for (i = 0; i < ntups; i++) |
| 14509 |
{ |
| 14510 |
amprocnum = PQgetvalue(res, i, i_amprocnum); |
| 14511 |
amproc = PQgetvalue(res, i, i_amproc); |
| 14512 |
amproclefttype = PQgetvalue(res, i, i_amproclefttype); |
| 14513 |
amprocrighttype = PQgetvalue(res, i, i_amprocrighttype); |
| 14514 |
|
| 14515 |
if (needComma) |
| 14516 |
appendPQExpBufferStr(q, " ,\n "); |
| 14517 |
|
| 14518 |
appendPQExpBuffer(q, "FUNCTION %s", amprocnum); |
| 14519 |
|
| 14520 |
if (*amproclefttype && *amprocrighttype) |
| 14521 |
appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype); |
| 14522 |
|
| 14523 |
appendPQExpBuffer(q, " %s", amproc); |
| 14524 |
|
| 14525 |
needComma = true; |
| 14526 |
} |
| 14527 |
|
| 14528 |
PQclear(res); |
| 14529 |
|
| 14530 |
/* |
| 14531 |
* If needComma is still false it means we haven't added anything after |
| 14532 |
* the AS keyword. To avoid printing broken SQL, append a dummy STORAGE |
| 14533 |
* clause with the same datatype. This isn't sanctioned by the |
| 14534 |
* documentation, but actually DefineOpClass will treat it as a no-op. |
| 14535 |
*/ |
| 14536 |
if (!needComma) |
| 14537 |
appendPQExpBuffer(q, "STORAGE %s", opcintype); |
| 14538 |
|
| 14539 |
appendPQExpBufferStr(q, ";\n"); |
| 14540 |
|
| 14541 |
appendPQExpBufferStr(nameusing, fmtId(opcinfo->dobj.name)); |
| 14542 |
appendPQExpBuffer(nameusing, " USING %s", |
| 14543 |
fmtId(amname)); |
| 14544 |
|
| 14545 |
if (dopt->binary_upgrade) |
| 14546 |
binary_upgrade_extension_member(q, &opcinfo->dobj, |
| 14547 |
"OPERATOR CLASS", nameusing->data, |
| 14548 |
opcinfo->dobj.namespace->dobj.name); |
| 14549 |
|
| 14550 |
if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 14551 |
ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId, |
| 14552 |
ARCHIVE_OPTS(.tag = opcinfo->dobj.name, |
| 14553 |
.namespace = opcinfo->dobj.namespace->dobj.name, |
| 14554 |
.owner = opcinfo->rolname, |
| 14555 |
.description = "OPERATOR CLASS", |
| 14556 |
.section = SECTION_PRE_DATA, |
| 14557 |
.createStmt = q->data, |
| 14558 |
.dropStmt = delq->data)); |
| 14559 |
|
| 14560 |
/* Dump Operator Class Comments */ |
| 14561 |
if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 14562 |
dumpComment(fout, "OPERATOR CLASS", nameusing->data, |
| 14563 |
opcinfo->dobj.namespace->dobj.name, opcinfo->rolname, |
| 14564 |
opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId); |
| 14565 |
|
| 14566 |
free(opcintype); |
| 14567 |
free(opcfamily); |
| 14568 |
free(amname); |
| 14569 |
destroyPQExpBuffer(query); |
| 14570 |
destroyPQExpBuffer(q); |
| 14571 |
destroyPQExpBuffer(delq); |
| 14572 |
destroyPQExpBuffer(nameusing); |
| 14573 |
} |
| 14574 |
|
| 14575 |
/* |
| 14576 |
* dumpOpfamily |
| 14577 |
* write out a single operator family definition |
| 14578 |
* |
| 14579 |
* Note: this also dumps any "loose" operator members that aren't bound to a |
| 14580 |
* specific opclass within the opfamily. |
| 14581 |
*/ |
| 14582 |
static void |
| 14583 |
dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo) |
| 14584 |
{ |
| 14585 |
DumpOptions *dopt = fout->dopt; |
| 14586 |
PQExpBuffer query; |
| 14587 |
PQExpBuffer q; |
| 14588 |
PQExpBuffer delq; |
| 14589 |
PQExpBuffer nameusing; |
| 14590 |
PGresult *res; |
| 14591 |
PGresult *res_ops; |
| 14592 |
PGresult *res_procs; |
| 14593 |
int ntups; |
| 14594 |
int i_amname; |
| 14595 |
int i_amopstrategy; |
| 14596 |
int i_amopopr; |
| 14597 |
int i_sortfamily; |
| 14598 |
int i_sortfamilynsp; |
| 14599 |
int i_amprocnum; |
| 14600 |
int i_amproc; |
| 14601 |
int i_amproclefttype; |
| 14602 |
int i_amprocrighttype; |
| 14603 |
char *amname; |
| 14604 |
char *amopstrategy; |
| 14605 |
char *amopopr; |
| 14606 |
char *sortfamily; |
| 14607 |
char *sortfamilynsp; |
| 14608 |
char *amprocnum; |
| 14609 |
char *amproc; |
| 14610 |
char *amproclefttype; |
| 14611 |
char *amprocrighttype; |
| 14612 |
bool needComma; |
| 14613 |
int i; |
| 14614 |
|
| 14615 |
/* Do nothing if not dumping schema */ |
| 14616 |
if (!dopt->dumpSchema) |
| 14617 |
return; |
| 14618 |
|
| 14619 |
query = createPQExpBuffer(); |
| 14620 |
q = createPQExpBuffer(); |
| 14621 |
delq = createPQExpBuffer(); |
| 14622 |
nameusing = createPQExpBuffer(); |
| 14623 |
|
| 14624 |
/* |
| 14625 |
* Fetch only those opfamily members that are tied directly to the |
| 14626 |
* opfamily by pg_depend entries. |
| 14627 |
*/ |
| 14628 |
appendPQExpBuffer(query, "SELECT amopstrategy, " |
| 14629 |
"amopopr::pg_catalog.regoperator, " |
| 14630 |
"opfname AS sortfamily, " |
| 14631 |
"nspname AS sortfamilynsp " |
| 14632 |
"FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON " |
| 14633 |
"(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) " |
| 14634 |
"LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily " |
| 14635 |
"LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace " |
| 14636 |
"WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass " |
| 14637 |
"AND refobjid = '%u'::pg_catalog.oid " |
| 14638 |
"AND amopfamily = '%u'::pg_catalog.oid " |
| 14639 |
"ORDER BY amopstrategy", |
| 14640 |
opfinfo->dobj.catId.oid, |
| 14641 |
opfinfo->dobj.catId.oid); |
| 14642 |
|
| 14643 |
res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 14644 |
|
| 14645 |
resetPQExpBuffer(query); |
| 14646 |
|
| 14647 |
appendPQExpBuffer(query, "SELECT amprocnum, " |
| 14648 |
"amproc::pg_catalog.regprocedure, " |
| 14649 |
"amproclefttype::pg_catalog.regtype, " |
| 14650 |
"amprocrighttype::pg_catalog.regtype " |
| 14651 |
"FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend " |
| 14652 |
"WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass " |
| 14653 |
"AND refobjid = '%u'::pg_catalog.oid " |
| 14654 |
"AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass " |
| 14655 |
"AND objid = ap.oid " |
| 14656 |
"ORDER BY amprocnum", |
| 14657 |
opfinfo->dobj.catId.oid); |
| 14658 |
|
| 14659 |
res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 14660 |
|
| 14661 |
/* Get additional fields from the pg_opfamily row */ |
| 14662 |
resetPQExpBuffer(query); |
| 14663 |
|
| 14664 |
appendPQExpBuffer(query, "SELECT " |
| 14665 |
"(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname " |
| 14666 |
"FROM pg_catalog.pg_opfamily " |
| 14667 |
"WHERE oid = '%u'::pg_catalog.oid", |
| 14668 |
opfinfo->dobj.catId.oid); |
| 14669 |
|
| 14670 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 14671 |
|
| 14672 |
i_amname = PQfnumber(res, "amname"); |
| 14673 |
|
| 14674 |
/* amname will still be needed after we PQclear res */ |
| 14675 |
amname = pg_strdup(PQgetvalue(res, 0, i_amname)); |
| 14676 |
|
| 14677 |
appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s", |
| 14678 |
fmtQualifiedDumpable(opfinfo)); |
| 14679 |
appendPQExpBuffer(delq, " USING %s;\n", |
| 14680 |
fmtId(amname)); |
| 14681 |
|
| 14682 |
/* Build the fixed portion of the CREATE command */ |
| 14683 |
appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s", |
| 14684 |
fmtQualifiedDumpable(opfinfo)); |
| 14685 |
appendPQExpBuffer(q, " USING %s;\n", |
| 14686 |
fmtId(amname)); |
| 14687 |
|
| 14688 |
PQclear(res); |
| 14689 |
|
| 14690 |
/* Do we need an ALTER to add loose members? */ |
| 14691 |
if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0) |
| 14692 |
{ |
| 14693 |
appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s", |
| 14694 |
fmtQualifiedDumpable(opfinfo)); |
| 14695 |
appendPQExpBuffer(q, " USING %s ADD\n ", |
| 14696 |
fmtId(amname)); |
| 14697 |
|
| 14698 |
needComma = false; |
| 14699 |
|
| 14700 |
/* |
| 14701 |
* Now fetch and print the OPERATOR entries (pg_amop rows). |
| 14702 |
*/ |
| 14703 |
ntups = PQntuples(res_ops); |
| 14704 |
|
| 14705 |
i_amopstrategy = PQfnumber(res_ops, "amopstrategy"); |
| 14706 |
i_amopopr = PQfnumber(res_ops, "amopopr"); |
| 14707 |
i_sortfamily = PQfnumber(res_ops, "sortfamily"); |
| 14708 |
i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp"); |
| 14709 |
|
| 14710 |
for (i = 0; i < ntups; i++) |
| 14711 |
{ |
| 14712 |
amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy); |
| 14713 |
amopopr = PQgetvalue(res_ops, i, i_amopopr); |
| 14714 |
sortfamily = PQgetvalue(res_ops, i, i_sortfamily); |
| 14715 |
sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp); |
| 14716 |
|
| 14717 |
if (needComma) |
| 14718 |
appendPQExpBufferStr(q, " ,\n "); |
| 14719 |
|
| 14720 |
appendPQExpBuffer(q, "OPERATOR %s %s", |
| 14721 |
amopstrategy, amopopr); |
| 14722 |
|
| 14723 |
if (strlen(sortfamily) > 0) |
| 14724 |
{ |
| 14725 |
appendPQExpBufferStr(q, " FOR ORDER BY "); |
| 14726 |
appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp)); |
| 14727 |
appendPQExpBufferStr(q, fmtId(sortfamily)); |
| 14728 |
} |
| 14729 |
|
| 14730 |
needComma = true; |
| 14731 |
} |
| 14732 |
|
| 14733 |
/* |
| 14734 |
* Now fetch and print the FUNCTION entries (pg_amproc rows). |
| 14735 |
*/ |
| 14736 |
ntups = PQntuples(res_procs); |
| 14737 |
|
| 14738 |
i_amprocnum = PQfnumber(res_procs, "amprocnum"); |
| 14739 |
i_amproc = PQfnumber(res_procs, "amproc"); |
| 14740 |
i_amproclefttype = PQfnumber(res_procs, "amproclefttype"); |
| 14741 |
i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype"); |
| 14742 |
|
| 14743 |
for (i = 0; i < ntups; i++) |
| 14744 |
{ |
| 14745 |
amprocnum = PQgetvalue(res_procs, i, i_amprocnum); |
| 14746 |
amproc = PQgetvalue(res_procs, i, i_amproc); |
| 14747 |
amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype); |
| 14748 |
amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype); |
| 14749 |
|
| 14750 |
if (needComma) |
| 14751 |
appendPQExpBufferStr(q, " ,\n "); |
| 14752 |
|
| 14753 |
appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s", |
| 14754 |
amprocnum, amproclefttype, amprocrighttype, |
| 14755 |
amproc); |
| 14756 |
|
| 14757 |
needComma = true; |
| 14758 |
} |
| 14759 |
|
| 14760 |
appendPQExpBufferStr(q, ";\n"); |
| 14761 |
} |
| 14762 |
|
| 14763 |
appendPQExpBufferStr(nameusing, fmtId(opfinfo->dobj.name)); |
| 14764 |
appendPQExpBuffer(nameusing, " USING %s", |
| 14765 |
fmtId(amname)); |
| 14766 |
|
| 14767 |
if (dopt->binary_upgrade) |
| 14768 |
binary_upgrade_extension_member(q, &opfinfo->dobj, |
| 14769 |
"OPERATOR FAMILY", nameusing->data, |
| 14770 |
opfinfo->dobj.namespace->dobj.name); |
| 14771 |
|
| 14772 |
if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 14773 |
ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId, |
| 14774 |
ARCHIVE_OPTS(.tag = opfinfo->dobj.name, |
| 14775 |
.namespace = opfinfo->dobj.namespace->dobj.name, |
| 14776 |
.owner = opfinfo->rolname, |
| 14777 |
.description = "OPERATOR FAMILY", |
| 14778 |
.section = SECTION_PRE_DATA, |
| 14779 |
.createStmt = q->data, |
| 14780 |
.dropStmt = delq->data)); |
| 14781 |
|
| 14782 |
/* Dump Operator Family Comments */ |
| 14783 |
if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 14784 |
dumpComment(fout, "OPERATOR FAMILY", nameusing->data, |
| 14785 |
opfinfo->dobj.namespace->dobj.name, opfinfo->rolname, |
| 14786 |
opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId); |
| 14787 |
|
| 14788 |
free(amname); |
| 14789 |
PQclear(res_ops); |
| 14790 |
PQclear(res_procs); |
| 14791 |
destroyPQExpBuffer(query); |
| 14792 |
destroyPQExpBuffer(q); |
| 14793 |
destroyPQExpBuffer(delq); |
| 14794 |
destroyPQExpBuffer(nameusing); |
| 14795 |
} |
| 14796 |
|
| 14797 |
/* |
| 14798 |
* dumpCollation |
| 14799 |
* write out a single collation definition |
| 14800 |
*/ |
| 14801 |
static void |
| 14802 |
dumpCollation(Archive *fout, const CollInfo *collinfo) |
| 14803 |
{ |
| 14804 |
DumpOptions *dopt = fout->dopt; |
| 14805 |
PQExpBuffer query; |
| 14806 |
PQExpBuffer q; |
| 14807 |
PQExpBuffer delq; |
| 14808 |
char *qcollname; |
| 14809 |
PGresult *res; |
| 14810 |
int i_collprovider; |
| 14811 |
int i_collisdeterministic; |
| 14812 |
int i_collcollate; |
| 14813 |
int i_collctype; |
| 14814 |
int i_colllocale; |
| 14815 |
int i_collicurules; |
| 14816 |
const char *collprovider; |
| 14817 |
const char *collcollate; |
| 14818 |
const char *collctype; |
| 14819 |
const char *colllocale; |
| 14820 |
const char *collicurules; |
| 14821 |
|
| 14822 |
/* Do nothing if not dumping schema */ |
| 14823 |
if (!dopt->dumpSchema) |
| 14824 |
return; |
| 14825 |
|
| 14826 |
query = createPQExpBuffer(); |
| 14827 |
q = createPQExpBuffer(); |
| 14828 |
delq = createPQExpBuffer(); |
| 14829 |
|
| 14830 |
qcollname = pg_strdup(fmtId(collinfo->dobj.name)); |
| 14831 |
|
| 14832 |
/* Get collation-specific details */ |
| 14833 |
appendPQExpBufferStr(query, "SELECT "); |
| 14834 |
|
| 14835 |
if (fout->remoteVersion >= 100000) |
| 14836 |
appendPQExpBufferStr(query, |
| 14837 |
"collprovider, " |
| 14838 |
"collversion, "); |
| 14839 |
else |
| 14840 |
appendPQExpBufferStr(query, |
| 14841 |
"'c' AS collprovider, " |
| 14842 |
"NULL AS collversion, "); |
| 14843 |
|
| 14844 |
if (fout->remoteVersion >= 120000) |
| 14845 |
appendPQExpBufferStr(query, |
| 14846 |
"collisdeterministic, "); |
| 14847 |
else |
| 14848 |
appendPQExpBufferStr(query, |
| 14849 |
"true AS collisdeterministic, "); |
| 14850 |
|
| 14851 |
if (fout->remoteVersion >= 170000) |
| 14852 |
appendPQExpBufferStr(query, |
| 14853 |
"colllocale, "); |
| 14854 |
else if (fout->remoteVersion >= 150000) |
| 14855 |
appendPQExpBufferStr(query, |
| 14856 |
"colliculocale AS colllocale, "); |
| 14857 |
else |
| 14858 |
appendPQExpBufferStr(query, |
| 14859 |
"NULL AS colllocale, "); |
| 14860 |
|
| 14861 |
if (fout->remoteVersion >= 160000) |
| 14862 |
appendPQExpBufferStr(query, |
| 14863 |
"collicurules, "); |
| 14864 |
else |
| 14865 |
appendPQExpBufferStr(query, |
| 14866 |
"NULL AS collicurules, "); |
| 14867 |
|
| 14868 |
appendPQExpBuffer(query, |
| 14869 |
"collcollate, " |
| 14870 |
"collctype " |
| 14871 |
"FROM pg_catalog.pg_collation c " |
| 14872 |
"WHERE c.oid = '%u'::pg_catalog.oid", |
| 14873 |
collinfo->dobj.catId.oid); |
| 14874 |
|
| 14875 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 14876 |
|
| 14877 |
i_collprovider = PQfnumber(res, "collprovider"); |
| 14878 |
i_collisdeterministic = PQfnumber(res, "collisdeterministic"); |
| 14879 |
i_collcollate = PQfnumber(res, "collcollate"); |
| 14880 |
i_collctype = PQfnumber(res, "collctype"); |
| 14881 |
i_colllocale = PQfnumber(res, "colllocale"); |
| 14882 |
i_collicurules = PQfnumber(res, "collicurules"); |
| 14883 |
|
| 14884 |
collprovider = PQgetvalue(res, 0, i_collprovider); |
| 14885 |
|
| 14886 |
if (!PQgetisnull(res, 0, i_collcollate)) |
| 14887 |
collcollate = PQgetvalue(res, 0, i_collcollate); |
| 14888 |
else |
| 14889 |
collcollate = NULL; |
| 14890 |
|
| 14891 |
if (!PQgetisnull(res, 0, i_collctype)) |
| 14892 |
collctype = PQgetvalue(res, 0, i_collctype); |
| 14893 |
else |
| 14894 |
collctype = NULL; |
| 14895 |
|
| 14896 |
/* |
| 14897 |
* Before version 15, collcollate and collctype were of type NAME and |
| 14898 |
* non-nullable. Treat empty strings as NULL for consistency. |
| 14899 |
*/ |
| 14900 |
if (fout->remoteVersion < 150000) |
| 14901 |
{ |
| 14902 |
if (collcollate[0] == '\0') |
| 14903 |
collcollate = NULL; |
| 14904 |
if (collctype[0] == '\0') |
| 14905 |
collctype = NULL; |
| 14906 |
} |
| 14907 |
|
| 14908 |
if (!PQgetisnull(res, 0, i_colllocale)) |
| 14909 |
colllocale = PQgetvalue(res, 0, i_colllocale); |
| 14910 |
else |
| 14911 |
colllocale = NULL; |
| 14912 |
|
| 14913 |
if (!PQgetisnull(res, 0, i_collicurules)) |
| 14914 |
collicurules = PQgetvalue(res, 0, i_collicurules); |
| 14915 |
else |
| 14916 |
collicurules = NULL; |
| 14917 |
|
| 14918 |
appendPQExpBuffer(delq, "DROP COLLATION %s;\n", |
| 14919 |
fmtQualifiedDumpable(collinfo)); |
| 14920 |
|
| 14921 |
appendPQExpBuffer(q, "CREATE COLLATION %s (", |
| 14922 |
fmtQualifiedDumpable(collinfo)); |
| 14923 |
|
| 14924 |
appendPQExpBufferStr(q, "provider = "); |
| 14925 |
if (collprovider[0] == 'b') |
| 14926 |
appendPQExpBufferStr(q, "builtin"); |
| 14927 |
else if (collprovider[0] == 'c') |
| 14928 |
appendPQExpBufferStr(q, "libc"); |
| 14929 |
else if (collprovider[0] == 'i') |
| 14930 |
appendPQExpBufferStr(q, "icu"); |
| 14931 |
else if (collprovider[0] == 'd') |
| 14932 |
/* to allow dumping pg_catalog; not accepted on input */ |
| 14933 |
appendPQExpBufferStr(q, "default"); |
| 14934 |
else |
| 14935 |
pg_fatal("unrecognized collation provider: %s", |
| 14936 |
collprovider); |
| 14937 |
|
| 14938 |
if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0) |
| 14939 |
appendPQExpBufferStr(q, ", deterministic = false"); |
| 14940 |
|
| 14941 |
if (collprovider[0] == 'd') |
| 14942 |
{ |
| 14943 |
if (collcollate || collctype || colllocale || collicurules) |
| 14944 |
pg_log_warning("invalid collation \"%s\"", qcollname); |
| 14945 |
|
| 14946 |
/* no locale -- the default collation cannot be reloaded anyway */ |
| 14947 |
} |
| 14948 |
else if (collprovider[0] == 'b') |
| 14949 |
{ |
| 14950 |
if (collcollate || collctype || !colllocale || collicurules) |
| 14951 |
pg_log_warning("invalid collation \"%s\"", qcollname); |
| 14952 |
|
| 14953 |
appendPQExpBufferStr(q, ", locale = "); |
| 14954 |
appendStringLiteralAH(q, colllocale ? colllocale : "", |
| 14955 |
fout); |
| 14956 |
} |
| 14957 |
else if (collprovider[0] == 'i') |
| 14958 |
{ |
| 14959 |
if (fout->remoteVersion >= 150000) |
| 14960 |
{ |
| 14961 |
if (collcollate || collctype || !colllocale) |
| 14962 |
pg_log_warning("invalid collation \"%s\"", qcollname); |
| 14963 |
|
| 14964 |
appendPQExpBufferStr(q, ", locale = "); |
| 14965 |
appendStringLiteralAH(q, colllocale ? colllocale : "", |
| 14966 |
fout); |
| 14967 |
} |
| 14968 |
else |
| 14969 |
{ |
| 14970 |
if (!collcollate || !collctype || colllocale || |
| 14971 |
strcmp(collcollate, collctype) != 0) |
| 14972 |
pg_log_warning("invalid collation \"%s\"", qcollname); |
| 14973 |
|
| 14974 |
appendPQExpBufferStr(q, ", locale = "); |
| 14975 |
appendStringLiteralAH(q, collcollate ? collcollate : "", fout); |
| 14976 |
} |
| 14977 |
|
| 14978 |
if (collicurules) |
| 14979 |
{ |
| 14980 |
appendPQExpBufferStr(q, ", rules = "); |
| 14981 |
appendStringLiteralAH(q, collicurules ? collicurules : "", fout); |
| 14982 |
} |
| 14983 |
} |
| 14984 |
else if (collprovider[0] == 'c') |
| 14985 |
{ |
| 14986 |
if (colllocale || collicurules || !collcollate || !collctype) |
| 14987 |
pg_log_warning("invalid collation \"%s\"", qcollname); |
| 14988 |
|
| 14989 |
if (collcollate && collctype && strcmp(collcollate, collctype) == 0) |
| 14990 |
{ |
| 14991 |
appendPQExpBufferStr(q, ", locale = "); |
| 14992 |
appendStringLiteralAH(q, collcollate ? collcollate : "", fout); |
| 14993 |
} |
| 14994 |
else |
| 14995 |
{ |
| 14996 |
appendPQExpBufferStr(q, ", lc_collate = "); |
| 14997 |
appendStringLiteralAH(q, collcollate ? collcollate : "", fout); |
| 14998 |
appendPQExpBufferStr(q, ", lc_ctype = "); |
| 14999 |
appendStringLiteralAH(q, collctype ? collctype : "", fout); |
| 15000 |
} |
| 15001 |
} |
| 15002 |
else |
| 15003 |
pg_fatal("unrecognized collation provider: %s", collprovider); |
| 15004 |
|
| 15005 |
/* |
| 15006 |
* For binary upgrade, carry over the collation version. For normal |
| 15007 |
* dump/restore, omit the version, so that it is computed upon restore. |
| 15008 |
*/ |
| 15009 |
if (dopt->binary_upgrade) |
| 15010 |
{ |
| 15011 |
int i_collversion; |
| 15012 |
|
| 15013 |
i_collversion = PQfnumber(res, "collversion"); |
| 15014 |
if (!PQgetisnull(res, 0, i_collversion)) |
| 15015 |
{ |
| 15016 |
appendPQExpBufferStr(q, ", version = "); |
| 15017 |
appendStringLiteralAH(q, |
| 15018 |
PQgetvalue(res, 0, i_collversion), |
| 15019 |
fout); |
| 15020 |
} |
| 15021 |
} |
| 15022 |
|
| 15023 |
appendPQExpBufferStr(q, ");\n"); |
| 15024 |
|
| 15025 |
if (dopt->binary_upgrade) |
| 15026 |
binary_upgrade_extension_member(q, &collinfo->dobj, |
| 15027 |
"COLLATION", qcollname, |
| 15028 |
collinfo->dobj.namespace->dobj.name); |
| 15029 |
|
| 15030 |
if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15031 |
ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId, |
| 15032 |
ARCHIVE_OPTS(.tag = collinfo->dobj.name, |
| 15033 |
.namespace = collinfo->dobj.namespace->dobj.name, |
| 15034 |
.owner = collinfo->rolname, |
| 15035 |
.description = "COLLATION", |
| 15036 |
.section = SECTION_PRE_DATA, |
| 15037 |
.createStmt = q->data, |
| 15038 |
.dropStmt = delq->data)); |
| 15039 |
|
| 15040 |
/* Dump Collation Comments */ |
| 15041 |
if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15042 |
dumpComment(fout, "COLLATION", qcollname, |
| 15043 |
collinfo->dobj.namespace->dobj.name, collinfo->rolname, |
| 15044 |
collinfo->dobj.catId, 0, collinfo->dobj.dumpId); |
| 15045 |
|
| 15046 |
PQclear(res); |
| 15047 |
|
| 15048 |
destroyPQExpBuffer(query); |
| 15049 |
destroyPQExpBuffer(q); |
| 15050 |
destroyPQExpBuffer(delq); |
| 15051 |
free(qcollname); |
| 15052 |
} |
| 15053 |
|
| 15054 |
/* |
| 15055 |
* dumpConversion |
| 15056 |
* write out a single conversion definition |
| 15057 |
*/ |
| 15058 |
static void |
| 15059 |
dumpConversion(Archive *fout, const ConvInfo *convinfo) |
| 15060 |
{ |
| 15061 |
DumpOptions *dopt = fout->dopt; |
| 15062 |
PQExpBuffer query; |
| 15063 |
PQExpBuffer q; |
| 15064 |
PQExpBuffer delq; |
| 15065 |
char *qconvname; |
| 15066 |
PGresult *res; |
| 15067 |
int i_conforencoding; |
| 15068 |
int i_contoencoding; |
| 15069 |
int i_conproc; |
| 15070 |
int i_condefault; |
| 15071 |
const char *conforencoding; |
| 15072 |
const char *contoencoding; |
| 15073 |
const char *conproc; |
| 15074 |
bool condefault; |
| 15075 |
|
| 15076 |
/* Do nothing if not dumping schema */ |
| 15077 |
if (!dopt->dumpSchema) |
| 15078 |
return; |
| 15079 |
|
| 15080 |
query = createPQExpBuffer(); |
| 15081 |
q = createPQExpBuffer(); |
| 15082 |
delq = createPQExpBuffer(); |
| 15083 |
|
| 15084 |
qconvname = pg_strdup(fmtId(convinfo->dobj.name)); |
| 15085 |
|
| 15086 |
/* Get conversion-specific details */ |
| 15087 |
appendPQExpBuffer(query, "SELECT " |
| 15088 |
"pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, " |
| 15089 |
"pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, " |
| 15090 |
"conproc, condefault " |
| 15091 |
"FROM pg_catalog.pg_conversion c " |
| 15092 |
"WHERE c.oid = '%u'::pg_catalog.oid", |
| 15093 |
convinfo->dobj.catId.oid); |
| 15094 |
|
| 15095 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 15096 |
|
| 15097 |
i_conforencoding = PQfnumber(res, "conforencoding"); |
| 15098 |
i_contoencoding = PQfnumber(res, "contoencoding"); |
| 15099 |
i_conproc = PQfnumber(res, "conproc"); |
| 15100 |
i_condefault = PQfnumber(res, "condefault"); |
| 15101 |
|
| 15102 |
conforencoding = PQgetvalue(res, 0, i_conforencoding); |
| 15103 |
contoencoding = PQgetvalue(res, 0, i_contoencoding); |
| 15104 |
conproc = PQgetvalue(res, 0, i_conproc); |
| 15105 |
condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't'); |
| 15106 |
|
| 15107 |
appendPQExpBuffer(delq, "DROP CONVERSION %s;\n", |
| 15108 |
fmtQualifiedDumpable(convinfo)); |
| 15109 |
|
| 15110 |
appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ", |
| 15111 |
(condefault) ? "DEFAULT " : "", |
| 15112 |
fmtQualifiedDumpable(convinfo)); |
| 15113 |
appendStringLiteralAH(q, conforencoding, fout); |
| 15114 |
appendPQExpBufferStr(q, " TO "); |
| 15115 |
appendStringLiteralAH(q, contoencoding, fout); |
| 15116 |
/* regproc output is already sufficiently quoted */ |
| 15117 |
appendPQExpBuffer(q, " FROM %s;\n", conproc); |
| 15118 |
|
| 15119 |
if (dopt->binary_upgrade) |
| 15120 |
binary_upgrade_extension_member(q, &convinfo->dobj, |
| 15121 |
"CONVERSION", qconvname, |
| 15122 |
convinfo->dobj.namespace->dobj.name); |
| 15123 |
|
| 15124 |
if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15125 |
ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId, |
| 15126 |
ARCHIVE_OPTS(.tag = convinfo->dobj.name, |
| 15127 |
.namespace = convinfo->dobj.namespace->dobj.name, |
| 15128 |
.owner = convinfo->rolname, |
| 15129 |
.description = "CONVERSION", |
| 15130 |
.section = SECTION_PRE_DATA, |
| 15131 |
.createStmt = q->data, |
| 15132 |
.dropStmt = delq->data)); |
| 15133 |
|
| 15134 |
/* Dump Conversion Comments */ |
| 15135 |
if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15136 |
dumpComment(fout, "CONVERSION", qconvname, |
| 15137 |
convinfo->dobj.namespace->dobj.name, convinfo->rolname, |
| 15138 |
convinfo->dobj.catId, 0, convinfo->dobj.dumpId); |
| 15139 |
|
| 15140 |
PQclear(res); |
| 15141 |
|
| 15142 |
destroyPQExpBuffer(query); |
| 15143 |
destroyPQExpBuffer(q); |
| 15144 |
destroyPQExpBuffer(delq); |
| 15145 |
free(qconvname); |
| 15146 |
} |
| 15147 |
|
| 15148 |
/* |
| 15149 |
* format_aggregate_signature: generate aggregate name and argument list |
| 15150 |
* |
| 15151 |
* The argument type names are qualified if needed. The aggregate name |
| 15152 |
* is never qualified. |
| 15153 |
*/ |
| 15154 |
static char * |
| 15155 |
format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes) |
| 15156 |
{ |
| 15157 |
PQExpBufferData buf; |
| 15158 |
int j; |
| 15159 |
|
| 15160 |
initPQExpBuffer(&buf); |
| 15161 |
if (honor_quotes) |
| 15162 |
appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name)); |
| 15163 |
else |
| 15164 |
appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name); |
| 15165 |
|
| 15166 |
if (agginfo->aggfn.nargs == 0) |
| 15167 |
appendPQExpBufferStr(&buf, "(*)"); |
| 15168 |
else |
| 15169 |
{ |
| 15170 |
appendPQExpBufferChar(&buf, '('); |
| 15171 |
for (j = 0; j < agginfo->aggfn.nargs; j++) |
| 15172 |
appendPQExpBuffer(&buf, "%s%s", |
| 15173 |
(j > 0) ? ", " : "", |
| 15174 |
getFormattedTypeName(fout, |
| 15175 |
agginfo->aggfn.argtypes[j], |
| 15176 |
zeroIsError)); |
| 15177 |
appendPQExpBufferChar(&buf, ')'); |
| 15178 |
} |
| 15179 |
return buf.data; |
| 15180 |
} |
| 15181 |
|
| 15182 |
/* |
| 15183 |
* dumpAgg |
| 15184 |
* write out a single aggregate definition |
| 15185 |
*/ |
| 15186 |
static void |
| 15187 |
dumpAgg(Archive *fout, const AggInfo *agginfo) |
| 15188 |
{ |
| 15189 |
DumpOptions *dopt = fout->dopt; |
| 15190 |
PQExpBuffer query; |
| 15191 |
PQExpBuffer q; |
| 15192 |
PQExpBuffer delq; |
| 15193 |
PQExpBuffer details; |
| 15194 |
char *aggsig; /* identity signature */ |
| 15195 |
char *aggfullsig = NULL; /* full signature */ |
| 15196 |
char *aggsig_tag; |
| 15197 |
PGresult *res; |
| 15198 |
int i_agginitval; |
| 15199 |
int i_aggminitval; |
| 15200 |
const char *aggtransfn; |
| 15201 |
const char *aggfinalfn; |
| 15202 |
const char *aggcombinefn; |
| 15203 |
const char *aggserialfn; |
| 15204 |
const char *aggdeserialfn; |
| 15205 |
const char *aggmtransfn; |
| 15206 |
const char *aggminvtransfn; |
| 15207 |
const char *aggmfinalfn; |
| 15208 |
bool aggfinalextra; |
| 15209 |
bool aggmfinalextra; |
| 15210 |
char aggfinalmodify; |
| 15211 |
char aggmfinalmodify; |
| 15212 |
const char *aggsortop; |
| 15213 |
char *aggsortconvop; |
| 15214 |
char aggkind; |
| 15215 |
const char *aggtranstype; |
| 15216 |
const char *aggtransspace; |
| 15217 |
const char *aggmtranstype; |
| 15218 |
const char *aggmtransspace; |
| 15219 |
const char *agginitval; |
| 15220 |
const char *aggminitval; |
| 15221 |
const char *proparallel; |
| 15222 |
char defaultfinalmodify; |
| 15223 |
|
| 15224 |
/* Do nothing if not dumping schema */ |
| 15225 |
if (!dopt->dumpSchema) |
| 15226 |
return; |
| 15227 |
|
| 15228 |
query = createPQExpBuffer(); |
| 15229 |
q = createPQExpBuffer(); |
| 15230 |
delq = createPQExpBuffer(); |
| 15231 |
details = createPQExpBuffer(); |
| 15232 |
|
| 15233 |
if (!fout->is_prepared[PREPQUERY_DUMPAGG]) |
| 15234 |
{ |
| 15235 |
/* Set up query for aggregate-specific details */ |
| 15236 |
appendPQExpBufferStr(query, |
| 15237 |
"PREPARE dumpAgg(pg_catalog.oid) AS\n"); |
| 15238 |
|
| 15239 |
appendPQExpBufferStr(query, |
| 15240 |
"SELECT " |
| 15241 |
"aggtransfn,\n" |
| 15242 |
"aggfinalfn,\n" |
| 15243 |
"aggtranstype::pg_catalog.regtype,\n" |
| 15244 |
"agginitval,\n" |
| 15245 |
"aggsortop,\n" |
| 15246 |
"pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n" |
| 15247 |
"pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n"); |
| 15248 |
|
| 15249 |
if (fout->remoteVersion >= 90400) |
| 15250 |
appendPQExpBufferStr(query, |
| 15251 |
"aggkind,\n" |
| 15252 |
"aggmtransfn,\n" |
| 15253 |
"aggminvtransfn,\n" |
| 15254 |
"aggmfinalfn,\n" |
| 15255 |
"aggmtranstype::pg_catalog.regtype,\n" |
| 15256 |
"aggfinalextra,\n" |
| 15257 |
"aggmfinalextra,\n" |
| 15258 |
"aggtransspace,\n" |
| 15259 |
"aggmtransspace,\n" |
| 15260 |
"aggminitval,\n"); |
| 15261 |
else |
| 15262 |
appendPQExpBufferStr(query, |
| 15263 |
"'n' AS aggkind,\n" |
| 15264 |
"'-' AS aggmtransfn,\n" |
| 15265 |
"'-' AS aggminvtransfn,\n" |
| 15266 |
"'-' AS aggmfinalfn,\n" |
| 15267 |
"0 AS aggmtranstype,\n" |
| 15268 |
"false AS aggfinalextra,\n" |
| 15269 |
"false AS aggmfinalextra,\n" |
| 15270 |
"0 AS aggtransspace,\n" |
| 15271 |
"0 AS aggmtransspace,\n" |
| 15272 |
"NULL AS aggminitval,\n"); |
| 15273 |
|
| 15274 |
if (fout->remoteVersion >= 90600) |
| 15275 |
appendPQExpBufferStr(query, |
| 15276 |
"aggcombinefn,\n" |
| 15277 |
"aggserialfn,\n" |
| 15278 |
"aggdeserialfn,\n" |
| 15279 |
"proparallel,\n"); |
| 15280 |
else |
| 15281 |
appendPQExpBufferStr(query, |
| 15282 |
"'-' AS aggcombinefn,\n" |
| 15283 |
"'-' AS aggserialfn,\n" |
| 15284 |
"'-' AS aggdeserialfn,\n" |
| 15285 |
"'u' AS proparallel,\n"); |
| 15286 |
|
| 15287 |
if (fout->remoteVersion >= 110000) |
| 15288 |
appendPQExpBufferStr(query, |
| 15289 |
"aggfinalmodify,\n" |
| 15290 |
"aggmfinalmodify\n"); |
| 15291 |
else |
| 15292 |
appendPQExpBufferStr(query, |
| 15293 |
"'0' AS aggfinalmodify,\n" |
| 15294 |
"'0' AS aggmfinalmodify\n"); |
| 15295 |
|
| 15296 |
appendPQExpBufferStr(query, |
| 15297 |
"FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p " |
| 15298 |
"WHERE a.aggfnoid = p.oid " |
| 15299 |
"AND p.oid = $1"); |
| 15300 |
|
| 15301 |
ExecuteSqlStatement(fout, query->data); |
| 15302 |
|
| 15303 |
fout->is_prepared[PREPQUERY_DUMPAGG] = true; |
| 15304 |
} |
| 15305 |
|
| 15306 |
printfPQExpBuffer(query, |
| 15307 |
"EXECUTE dumpAgg('%u')", |
| 15308 |
agginfo->aggfn.dobj.catId.oid); |
| 15309 |
|
| 15310 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 15311 |
|
| 15312 |
i_agginitval = PQfnumber(res, "agginitval"); |
| 15313 |
i_aggminitval = PQfnumber(res, "aggminitval"); |
| 15314 |
|
| 15315 |
aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn")); |
| 15316 |
aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn")); |
| 15317 |
aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn")); |
| 15318 |
aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn")); |
| 15319 |
aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn")); |
| 15320 |
aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn")); |
| 15321 |
aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn")); |
| 15322 |
aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn")); |
| 15323 |
aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't'); |
| 15324 |
aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't'); |
| 15325 |
aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0]; |
| 15326 |
aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0]; |
| 15327 |
aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop")); |
| 15328 |
aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0]; |
| 15329 |
aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype")); |
| 15330 |
aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace")); |
| 15331 |
aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype")); |
| 15332 |
aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace")); |
| 15333 |
agginitval = PQgetvalue(res, 0, i_agginitval); |
| 15334 |
aggminitval = PQgetvalue(res, 0, i_aggminitval); |
| 15335 |
proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel")); |
| 15336 |
|
| 15337 |
{ |
| 15338 |
char *funcargs; |
| 15339 |
char *funciargs; |
| 15340 |
|
| 15341 |
funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs")); |
| 15342 |
funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs")); |
| 15343 |
aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true); |
| 15344 |
aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true); |
| 15345 |
} |
| 15346 |
|
| 15347 |
aggsig_tag = format_aggregate_signature(agginfo, fout, false); |
| 15348 |
|
| 15349 |
/* identify default modify flag for aggkind (must match DefineAggregate) */ |
| 15350 |
defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE; |
| 15351 |
/* replace omitted flags for old versions */ |
| 15352 |
if (aggfinalmodify == '0') |
| 15353 |
aggfinalmodify = defaultfinalmodify; |
| 15354 |
if (aggmfinalmodify == '0') |
| 15355 |
aggmfinalmodify = defaultfinalmodify; |
| 15356 |
|
| 15357 |
/* regproc and regtype output is already sufficiently quoted */ |
| 15358 |
appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s", |
| 15359 |
aggtransfn, aggtranstype); |
| 15360 |
|
| 15361 |
if (strcmp(aggtransspace, "0") != 0) |
| 15362 |
{ |
| 15363 |
appendPQExpBuffer(details, ",\n SSPACE = %s", |
| 15364 |
aggtransspace); |
| 15365 |
} |
| 15366 |
|
| 15367 |
if (!PQgetisnull(res, 0, i_agginitval)) |
| 15368 |
{ |
| 15369 |
appendPQExpBufferStr(details, ",\n INITCOND = "); |
| 15370 |
appendStringLiteralAH(details, agginitval, fout); |
| 15371 |
} |
| 15372 |
|
| 15373 |
if (strcmp(aggfinalfn, "-") != 0) |
| 15374 |
{ |
| 15375 |
appendPQExpBuffer(details, ",\n FINALFUNC = %s", |
| 15376 |
aggfinalfn); |
| 15377 |
if (aggfinalextra) |
| 15378 |
appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA"); |
| 15379 |
if (aggfinalmodify != defaultfinalmodify) |
| 15380 |
{ |
| 15381 |
switch (aggfinalmodify) |
| 15382 |
{ |
| 15383 |
case AGGMODIFY_READ_ONLY: |
| 15384 |
appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY"); |
| 15385 |
break; |
| 15386 |
case AGGMODIFY_SHAREABLE: |
| 15387 |
appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE"); |
| 15388 |
break; |
| 15389 |
case AGGMODIFY_READ_WRITE: |
| 15390 |
appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE"); |
| 15391 |
break; |
| 15392 |
default: |
| 15393 |
pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"", |
| 15394 |
agginfo->aggfn.dobj.name); |
| 15395 |
break; |
| 15396 |
} |
| 15397 |
} |
| 15398 |
} |
| 15399 |
|
| 15400 |
if (strcmp(aggcombinefn, "-") != 0) |
| 15401 |
appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn); |
| 15402 |
|
| 15403 |
if (strcmp(aggserialfn, "-") != 0) |
| 15404 |
appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn); |
| 15405 |
|
| 15406 |
if (strcmp(aggdeserialfn, "-") != 0) |
| 15407 |
appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn); |
| 15408 |
|
| 15409 |
if (strcmp(aggmtransfn, "-") != 0) |
| 15410 |
{ |
| 15411 |
appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s", |
| 15412 |
aggmtransfn, |
| 15413 |
aggminvtransfn, |
| 15414 |
aggmtranstype); |
| 15415 |
} |
| 15416 |
|
| 15417 |
if (strcmp(aggmtransspace, "0") != 0) |
| 15418 |
{ |
| 15419 |
appendPQExpBuffer(details, ",\n MSSPACE = %s", |
| 15420 |
aggmtransspace); |
| 15421 |
} |
| 15422 |
|
| 15423 |
if (!PQgetisnull(res, 0, i_aggminitval)) |
| 15424 |
{ |
| 15425 |
appendPQExpBufferStr(details, ",\n MINITCOND = "); |
| 15426 |
appendStringLiteralAH(details, aggminitval, fout); |
| 15427 |
} |
| 15428 |
|
| 15429 |
if (strcmp(aggmfinalfn, "-") != 0) |
| 15430 |
{ |
| 15431 |
appendPQExpBuffer(details, ",\n MFINALFUNC = %s", |
| 15432 |
aggmfinalfn); |
| 15433 |
if (aggmfinalextra) |
| 15434 |
appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA"); |
| 15435 |
if (aggmfinalmodify != defaultfinalmodify) |
| 15436 |
{ |
| 15437 |
switch (aggmfinalmodify) |
| 15438 |
{ |
| 15439 |
case AGGMODIFY_READ_ONLY: |
| 15440 |
appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY"); |
| 15441 |
break; |
| 15442 |
case AGGMODIFY_SHAREABLE: |
| 15443 |
appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE"); |
| 15444 |
break; |
| 15445 |
case AGGMODIFY_READ_WRITE: |
| 15446 |
appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE"); |
| 15447 |
break; |
| 15448 |
default: |
| 15449 |
pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"", |
| 15450 |
agginfo->aggfn.dobj.name); |
| 15451 |
break; |
| 15452 |
} |
| 15453 |
} |
| 15454 |
} |
| 15455 |
|
| 15456 |
aggsortconvop = getFormattedOperatorName(aggsortop); |
| 15457 |
if (aggsortconvop) |
| 15458 |
{ |
| 15459 |
appendPQExpBuffer(details, ",\n SORTOP = %s", |
| 15460 |
aggsortconvop); |
| 15461 |
free(aggsortconvop); |
| 15462 |
} |
| 15463 |
|
| 15464 |
if (aggkind == AGGKIND_HYPOTHETICAL) |
| 15465 |
appendPQExpBufferStr(details, ",\n HYPOTHETICAL"); |
| 15466 |
|
| 15467 |
if (proparallel[0] != PROPARALLEL_UNSAFE) |
| 15468 |
{ |
| 15469 |
if (proparallel[0] == PROPARALLEL_SAFE) |
| 15470 |
appendPQExpBufferStr(details, ",\n PARALLEL = safe"); |
| 15471 |
else if (proparallel[0] == PROPARALLEL_RESTRICTED) |
| 15472 |
appendPQExpBufferStr(details, ",\n PARALLEL = restricted"); |
| 15473 |
else if (proparallel[0] != PROPARALLEL_UNSAFE) |
| 15474 |
pg_fatal("unrecognized proparallel value for function \"%s\"", |
| 15475 |
agginfo->aggfn.dobj.name); |
| 15476 |
} |
| 15477 |
|
| 15478 |
appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n", |
| 15479 |
fmtId(agginfo->aggfn.dobj.namespace->dobj.name), |
| 15480 |
aggsig); |
| 15481 |
|
| 15482 |
appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n", |
| 15483 |
fmtId(agginfo->aggfn.dobj.namespace->dobj.name), |
| 15484 |
aggfullsig ? aggfullsig : aggsig, details->data); |
| 15485 |
|
| 15486 |
if (dopt->binary_upgrade) |
| 15487 |
binary_upgrade_extension_member(q, &agginfo->aggfn.dobj, |
| 15488 |
"AGGREGATE", aggsig, |
| 15489 |
agginfo->aggfn.dobj.namespace->dobj.name); |
| 15490 |
|
| 15491 |
if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15492 |
ArchiveEntry(fout, agginfo->aggfn.dobj.catId, |
| 15493 |
agginfo->aggfn.dobj.dumpId, |
| 15494 |
ARCHIVE_OPTS(.tag = aggsig_tag, |
| 15495 |
.namespace = agginfo->aggfn.dobj.namespace->dobj.name, |
| 15496 |
.owner = agginfo->aggfn.rolname, |
| 15497 |
.description = "AGGREGATE", |
| 15498 |
.section = SECTION_PRE_DATA, |
| 15499 |
.createStmt = q->data, |
| 15500 |
.dropStmt = delq->data)); |
| 15501 |
|
| 15502 |
/* Dump Aggregate Comments */ |
| 15503 |
if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15504 |
dumpComment(fout, "AGGREGATE", aggsig, |
| 15505 |
agginfo->aggfn.dobj.namespace->dobj.name, |
| 15506 |
agginfo->aggfn.rolname, |
| 15507 |
agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId); |
| 15508 |
|
| 15509 |
if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 15510 |
dumpSecLabel(fout, "AGGREGATE", aggsig, |
| 15511 |
agginfo->aggfn.dobj.namespace->dobj.name, |
| 15512 |
agginfo->aggfn.rolname, |
| 15513 |
agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId); |
| 15514 |
|
| 15515 |
/* |
| 15516 |
* Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL |
| 15517 |
* command look like a function's GRANT; in particular this affects the |
| 15518 |
* syntax for zero-argument aggregates and ordered-set aggregates. |
| 15519 |
*/ |
| 15520 |
free(aggsig); |
| 15521 |
|
| 15522 |
aggsig = format_function_signature(fout, &agginfo->aggfn, true); |
| 15523 |
|
| 15524 |
if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL) |
| 15525 |
dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId, |
| 15526 |
"FUNCTION", aggsig, NULL, |
| 15527 |
agginfo->aggfn.dobj.namespace->dobj.name, |
| 15528 |
NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl); |
| 15529 |
|
| 15530 |
free(aggsig); |
| 15531 |
free(aggfullsig); |
| 15532 |
free(aggsig_tag); |
| 15533 |
|
| 15534 |
PQclear(res); |
| 15535 |
|
| 15536 |
destroyPQExpBuffer(query); |
| 15537 |
destroyPQExpBuffer(q); |
| 15538 |
destroyPQExpBuffer(delq); |
| 15539 |
destroyPQExpBuffer(details); |
| 15540 |
} |
| 15541 |
|
| 15542 |
/* |
| 15543 |
* dumpTSParser |
| 15544 |
* write out a single text search parser |
| 15545 |
*/ |
| 15546 |
static void |
| 15547 |
dumpTSParser(Archive *fout, const TSParserInfo *prsinfo) |
| 15548 |
{ |
| 15549 |
DumpOptions *dopt = fout->dopt; |
| 15550 |
PQExpBuffer q; |
| 15551 |
PQExpBuffer delq; |
| 15552 |
char *qprsname; |
| 15553 |
|
| 15554 |
/* Do nothing if not dumping schema */ |
| 15555 |
if (!dopt->dumpSchema) |
| 15556 |
return; |
| 15557 |
|
| 15558 |
q = createPQExpBuffer(); |
| 15559 |
delq = createPQExpBuffer(); |
| 15560 |
|
| 15561 |
qprsname = pg_strdup(fmtId(prsinfo->dobj.name)); |
| 15562 |
|
| 15563 |
appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n", |
| 15564 |
fmtQualifiedDumpable(prsinfo)); |
| 15565 |
|
| 15566 |
appendPQExpBuffer(q, " START = %s,\n", |
| 15567 |
convertTSFunction(fout, prsinfo->prsstart)); |
| 15568 |
appendPQExpBuffer(q, " GETTOKEN = %s,\n", |
| 15569 |
convertTSFunction(fout, prsinfo->prstoken)); |
| 15570 |
appendPQExpBuffer(q, " END = %s,\n", |
| 15571 |
convertTSFunction(fout, prsinfo->prsend)); |
| 15572 |
if (prsinfo->prsheadline != InvalidOid) |
| 15573 |
appendPQExpBuffer(q, " HEADLINE = %s,\n", |
| 15574 |
convertTSFunction(fout, prsinfo->prsheadline)); |
| 15575 |
appendPQExpBuffer(q, " LEXTYPES = %s );\n", |
| 15576 |
convertTSFunction(fout, prsinfo->prslextype)); |
| 15577 |
|
| 15578 |
appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s;\n", |
| 15579 |
fmtQualifiedDumpable(prsinfo)); |
| 15580 |
|
| 15581 |
if (dopt->binary_upgrade) |
| 15582 |
binary_upgrade_extension_member(q, &prsinfo->dobj, |
| 15583 |
"TEXT SEARCH PARSER", qprsname, |
| 15584 |
prsinfo->dobj.namespace->dobj.name); |
| 15585 |
|
| 15586 |
if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15587 |
ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId, |
| 15588 |
ARCHIVE_OPTS(.tag = prsinfo->dobj.name, |
| 15589 |
.namespace = prsinfo->dobj.namespace->dobj.name, |
| 15590 |
.description = "TEXT SEARCH PARSER", |
| 15591 |
.section = SECTION_PRE_DATA, |
| 15592 |
.createStmt = q->data, |
| 15593 |
.dropStmt = delq->data)); |
| 15594 |
|
| 15595 |
/* Dump Parser Comments */ |
| 15596 |
if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15597 |
dumpComment(fout, "TEXT SEARCH PARSER", qprsname, |
| 15598 |
prsinfo->dobj.namespace->dobj.name, "", |
| 15599 |
prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId); |
| 15600 |
|
| 15601 |
destroyPQExpBuffer(q); |
| 15602 |
destroyPQExpBuffer(delq); |
| 15603 |
free(qprsname); |
| 15604 |
} |
| 15605 |
|
| 15606 |
/* |
| 15607 |
* dumpTSDictionary |
| 15608 |
* write out a single text search dictionary |
| 15609 |
*/ |
| 15610 |
static void |
| 15611 |
dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo) |
| 15612 |
{ |
| 15613 |
DumpOptions *dopt = fout->dopt; |
| 15614 |
PQExpBuffer q; |
| 15615 |
PQExpBuffer delq; |
| 15616 |
PQExpBuffer query; |
| 15617 |
char *qdictname; |
| 15618 |
PGresult *res; |
| 15619 |
char *nspname; |
| 15620 |
char *tmplname; |
| 15621 |
|
| 15622 |
/* Do nothing if not dumping schema */ |
| 15623 |
if (!dopt->dumpSchema) |
| 15624 |
return; |
| 15625 |
|
| 15626 |
q = createPQExpBuffer(); |
| 15627 |
delq = createPQExpBuffer(); |
| 15628 |
query = createPQExpBuffer(); |
| 15629 |
|
| 15630 |
qdictname = pg_strdup(fmtId(dictinfo->dobj.name)); |
| 15631 |
|
| 15632 |
/* Fetch name and namespace of the dictionary's template */ |
| 15633 |
appendPQExpBuffer(query, "SELECT nspname, tmplname " |
| 15634 |
"FROM pg_ts_template p, pg_namespace n " |
| 15635 |
"WHERE p.oid = '%u' AND n.oid = tmplnamespace", |
| 15636 |
dictinfo->dicttemplate); |
| 15637 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 15638 |
nspname = PQgetvalue(res, 0, 0); |
| 15639 |
tmplname = PQgetvalue(res, 0, 1); |
| 15640 |
|
| 15641 |
appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n", |
| 15642 |
fmtQualifiedDumpable(dictinfo)); |
| 15643 |
|
| 15644 |
appendPQExpBufferStr(q, " TEMPLATE = "); |
| 15645 |
appendPQExpBuffer(q, "%s.", fmtId(nspname)); |
| 15646 |
appendPQExpBufferStr(q, fmtId(tmplname)); |
| 15647 |
|
| 15648 |
PQclear(res); |
| 15649 |
|
| 15650 |
/* the dictinitoption can be dumped straight into the command */ |
| 15651 |
if (dictinfo->dictinitoption) |
| 15652 |
appendPQExpBuffer(q, ",\n %s", dictinfo->dictinitoption); |
| 15653 |
|
| 15654 |
appendPQExpBufferStr(q, " );\n"); |
| 15655 |
|
| 15656 |
appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s;\n", |
| 15657 |
fmtQualifiedDumpable(dictinfo)); |
| 15658 |
|
| 15659 |
if (dopt->binary_upgrade) |
| 15660 |
binary_upgrade_extension_member(q, &dictinfo->dobj, |
| 15661 |
"TEXT SEARCH DICTIONARY", qdictname, |
| 15662 |
dictinfo->dobj.namespace->dobj.name); |
| 15663 |
|
| 15664 |
if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15665 |
ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId, |
| 15666 |
ARCHIVE_OPTS(.tag = dictinfo->dobj.name, |
| 15667 |
.namespace = dictinfo->dobj.namespace->dobj.name, |
| 15668 |
.owner = dictinfo->rolname, |
| 15669 |
.description = "TEXT SEARCH DICTIONARY", |
| 15670 |
.section = SECTION_PRE_DATA, |
| 15671 |
.createStmt = q->data, |
| 15672 |
.dropStmt = delq->data)); |
| 15673 |
|
| 15674 |
/* Dump Dictionary Comments */ |
| 15675 |
if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15676 |
dumpComment(fout, "TEXT SEARCH DICTIONARY", qdictname, |
| 15677 |
dictinfo->dobj.namespace->dobj.name, dictinfo->rolname, |
| 15678 |
dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId); |
| 15679 |
|
| 15680 |
destroyPQExpBuffer(q); |
| 15681 |
destroyPQExpBuffer(delq); |
| 15682 |
destroyPQExpBuffer(query); |
| 15683 |
free(qdictname); |
| 15684 |
} |
| 15685 |
|
| 15686 |
/* |
| 15687 |
* dumpTSTemplate |
| 15688 |
* write out a single text search template |
| 15689 |
*/ |
| 15690 |
static void |
| 15691 |
dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo) |
| 15692 |
{ |
| 15693 |
DumpOptions *dopt = fout->dopt; |
| 15694 |
PQExpBuffer q; |
| 15695 |
PQExpBuffer delq; |
| 15696 |
char *qtmplname; |
| 15697 |
|
| 15698 |
/* Do nothing if not dumping schema */ |
| 15699 |
if (!dopt->dumpSchema) |
| 15700 |
return; |
| 15701 |
|
| 15702 |
q = createPQExpBuffer(); |
| 15703 |
delq = createPQExpBuffer(); |
| 15704 |
|
| 15705 |
qtmplname = pg_strdup(fmtId(tmplinfo->dobj.name)); |
| 15706 |
|
| 15707 |
appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n", |
| 15708 |
fmtQualifiedDumpable(tmplinfo)); |
| 15709 |
|
| 15710 |
if (tmplinfo->tmplinit != InvalidOid) |
| 15711 |
appendPQExpBuffer(q, " INIT = %s,\n", |
| 15712 |
convertTSFunction(fout, tmplinfo->tmplinit)); |
| 15713 |
appendPQExpBuffer(q, " LEXIZE = %s );\n", |
| 15714 |
convertTSFunction(fout, tmplinfo->tmpllexize)); |
| 15715 |
|
| 15716 |
appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s;\n", |
| 15717 |
fmtQualifiedDumpable(tmplinfo)); |
| 15718 |
|
| 15719 |
if (dopt->binary_upgrade) |
| 15720 |
binary_upgrade_extension_member(q, &tmplinfo->dobj, |
| 15721 |
"TEXT SEARCH TEMPLATE", qtmplname, |
| 15722 |
tmplinfo->dobj.namespace->dobj.name); |
| 15723 |
|
| 15724 |
if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15725 |
ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId, |
| 15726 |
ARCHIVE_OPTS(.tag = tmplinfo->dobj.name, |
| 15727 |
.namespace = tmplinfo->dobj.namespace->dobj.name, |
| 15728 |
.description = "TEXT SEARCH TEMPLATE", |
| 15729 |
.section = SECTION_PRE_DATA, |
| 15730 |
.createStmt = q->data, |
| 15731 |
.dropStmt = delq->data)); |
| 15732 |
|
| 15733 |
/* Dump Template Comments */ |
| 15734 |
if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15735 |
dumpComment(fout, "TEXT SEARCH TEMPLATE", qtmplname, |
| 15736 |
tmplinfo->dobj.namespace->dobj.name, "", |
| 15737 |
tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId); |
| 15738 |
|
| 15739 |
destroyPQExpBuffer(q); |
| 15740 |
destroyPQExpBuffer(delq); |
| 15741 |
free(qtmplname); |
| 15742 |
} |
| 15743 |
|
| 15744 |
/* |
| 15745 |
* dumpTSConfig |
| 15746 |
* write out a single text search configuration |
| 15747 |
*/ |
| 15748 |
static void |
| 15749 |
dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo) |
| 15750 |
{ |
| 15751 |
DumpOptions *dopt = fout->dopt; |
| 15752 |
PQExpBuffer q; |
| 15753 |
PQExpBuffer delq; |
| 15754 |
PQExpBuffer query; |
| 15755 |
char *qcfgname; |
| 15756 |
PGresult *res; |
| 15757 |
char *nspname; |
| 15758 |
char *prsname; |
| 15759 |
int ntups, |
| 15760 |
i; |
| 15761 |
int i_tokenname; |
| 15762 |
int i_dictname; |
| 15763 |
|
| 15764 |
/* Do nothing if not dumping schema */ |
| 15765 |
if (!dopt->dumpSchema) |
| 15766 |
return; |
| 15767 |
|
| 15768 |
q = createPQExpBuffer(); |
| 15769 |
delq = createPQExpBuffer(); |
| 15770 |
query = createPQExpBuffer(); |
| 15771 |
|
| 15772 |
qcfgname = pg_strdup(fmtId(cfginfo->dobj.name)); |
| 15773 |
|
| 15774 |
/* Fetch name and namespace of the config's parser */ |
| 15775 |
appendPQExpBuffer(query, "SELECT nspname, prsname " |
| 15776 |
"FROM pg_ts_parser p, pg_namespace n " |
| 15777 |
"WHERE p.oid = '%u' AND n.oid = prsnamespace", |
| 15778 |
cfginfo->cfgparser); |
| 15779 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 15780 |
nspname = PQgetvalue(res, 0, 0); |
| 15781 |
prsname = PQgetvalue(res, 0, 1); |
| 15782 |
|
| 15783 |
appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n", |
| 15784 |
fmtQualifiedDumpable(cfginfo)); |
| 15785 |
|
| 15786 |
appendPQExpBuffer(q, " PARSER = %s.", fmtId(nspname)); |
| 15787 |
appendPQExpBuffer(q, "%s );\n", fmtId(prsname)); |
| 15788 |
|
| 15789 |
PQclear(res); |
| 15790 |
|
| 15791 |
resetPQExpBuffer(query); |
| 15792 |
appendPQExpBuffer(query, |
| 15793 |
"SELECT\n" |
| 15794 |
" ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n" |
| 15795 |
" WHERE t.tokid = m.maptokentype ) AS tokenname,\n" |
| 15796 |
" m.mapdict::pg_catalog.regdictionary AS dictname\n" |
| 15797 |
"FROM pg_catalog.pg_ts_config_map AS m\n" |
| 15798 |
"WHERE m.mapcfg = '%u'\n" |
| 15799 |
"ORDER BY m.mapcfg, m.maptokentype, m.mapseqno", |
| 15800 |
cfginfo->cfgparser, cfginfo->dobj.catId.oid); |
| 15801 |
|
| 15802 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 15803 |
ntups = PQntuples(res); |
| 15804 |
|
| 15805 |
i_tokenname = PQfnumber(res, "tokenname"); |
| 15806 |
i_dictname = PQfnumber(res, "dictname"); |
| 15807 |
|
| 15808 |
for (i = 0; i < ntups; i++) |
| 15809 |
{ |
| 15810 |
char *tokenname = PQgetvalue(res, i, i_tokenname); |
| 15811 |
char *dictname = PQgetvalue(res, i, i_dictname); |
| 15812 |
|
| 15813 |
if (i == 0 || |
| 15814 |
strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0) |
| 15815 |
{ |
| 15816 |
/* starting a new token type, so start a new command */ |
| 15817 |
if (i > 0) |
| 15818 |
appendPQExpBufferStr(q, ";\n"); |
| 15819 |
appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n", |
| 15820 |
fmtQualifiedDumpable(cfginfo)); |
| 15821 |
/* tokenname needs quoting, dictname does NOT */ |
| 15822 |
appendPQExpBuffer(q, " ADD MAPPING FOR %s WITH %s", |
| 15823 |
fmtId(tokenname), dictname); |
| 15824 |
} |
| 15825 |
else |
| 15826 |
appendPQExpBuffer(q, ", %s", dictname); |
| 15827 |
} |
| 15828 |
|
| 15829 |
if (ntups > 0) |
| 15830 |
appendPQExpBufferStr(q, ";\n"); |
| 15831 |
|
| 15832 |
PQclear(res); |
| 15833 |
|
| 15834 |
appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s;\n", |
| 15835 |
fmtQualifiedDumpable(cfginfo)); |
| 15836 |
|
| 15837 |
if (dopt->binary_upgrade) |
| 15838 |
binary_upgrade_extension_member(q, &cfginfo->dobj, |
| 15839 |
"TEXT SEARCH CONFIGURATION", qcfgname, |
| 15840 |
cfginfo->dobj.namespace->dobj.name); |
| 15841 |
|
| 15842 |
if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15843 |
ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId, |
| 15844 |
ARCHIVE_OPTS(.tag = cfginfo->dobj.name, |
| 15845 |
.namespace = cfginfo->dobj.namespace->dobj.name, |
| 15846 |
.owner = cfginfo->rolname, |
| 15847 |
.description = "TEXT SEARCH CONFIGURATION", |
| 15848 |
.section = SECTION_PRE_DATA, |
| 15849 |
.createStmt = q->data, |
| 15850 |
.dropStmt = delq->data)); |
| 15851 |
|
| 15852 |
/* Dump Configuration Comments */ |
| 15853 |
if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15854 |
dumpComment(fout, "TEXT SEARCH CONFIGURATION", qcfgname, |
| 15855 |
cfginfo->dobj.namespace->dobj.name, cfginfo->rolname, |
| 15856 |
cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId); |
| 15857 |
|
| 15858 |
destroyPQExpBuffer(q); |
| 15859 |
destroyPQExpBuffer(delq); |
| 15860 |
destroyPQExpBuffer(query); |
| 15861 |
free(qcfgname); |
| 15862 |
} |
| 15863 |
|
| 15864 |
/* |
| 15865 |
* dumpForeignDataWrapper |
| 15866 |
* write out a single foreign-data wrapper definition |
| 15867 |
*/ |
| 15868 |
static void |
| 15869 |
dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo) |
| 15870 |
{ |
| 15871 |
DumpOptions *dopt = fout->dopt; |
| 15872 |
PQExpBuffer q; |
| 15873 |
PQExpBuffer delq; |
| 15874 |
char *qfdwname; |
| 15875 |
|
| 15876 |
/* Do nothing if not dumping schema */ |
| 15877 |
if (!dopt->dumpSchema) |
| 15878 |
return; |
| 15879 |
|
| 15880 |
q = createPQExpBuffer(); |
| 15881 |
delq = createPQExpBuffer(); |
| 15882 |
|
| 15883 |
qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name)); |
| 15884 |
|
| 15885 |
appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s", |
| 15886 |
qfdwname); |
| 15887 |
|
| 15888 |
if (strcmp(fdwinfo->fdwhandler, "-") != 0) |
| 15889 |
appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler); |
| 15890 |
|
| 15891 |
if (strcmp(fdwinfo->fdwvalidator, "-") != 0) |
| 15892 |
appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator); |
| 15893 |
|
| 15894 |
if (strlen(fdwinfo->fdwoptions) > 0) |
| 15895 |
appendPQExpBuffer(q, " OPTIONS (\n %s\n)", fdwinfo->fdwoptions); |
| 15896 |
|
| 15897 |
appendPQExpBufferStr(q, ";\n"); |
| 15898 |
|
| 15899 |
appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n", |
| 15900 |
qfdwname); |
| 15901 |
|
| 15902 |
if (dopt->binary_upgrade) |
| 15903 |
binary_upgrade_extension_member(q, &fdwinfo->dobj, |
| 15904 |
"FOREIGN DATA WRAPPER", qfdwname, |
| 15905 |
NULL); |
| 15906 |
|
| 15907 |
if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15908 |
ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId, |
| 15909 |
ARCHIVE_OPTS(.tag = fdwinfo->dobj.name, |
| 15910 |
.owner = fdwinfo->rolname, |
| 15911 |
.description = "FOREIGN DATA WRAPPER", |
| 15912 |
.section = SECTION_PRE_DATA, |
| 15913 |
.createStmt = q->data, |
| 15914 |
.dropStmt = delq->data)); |
| 15915 |
|
| 15916 |
/* Dump Foreign Data Wrapper Comments */ |
| 15917 |
if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 15918 |
dumpComment(fout, "FOREIGN DATA WRAPPER", qfdwname, |
| 15919 |
NULL, fdwinfo->rolname, |
| 15920 |
fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId); |
| 15921 |
|
| 15922 |
/* Handle the ACL */ |
| 15923 |
if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 15924 |
dumpACL(fout, fdwinfo->dobj.dumpId, InvalidDumpId, |
| 15925 |
"FOREIGN DATA WRAPPER", qfdwname, NULL, NULL, |
| 15926 |
NULL, fdwinfo->rolname, &fdwinfo->dacl); |
| 15927 |
|
| 15928 |
free(qfdwname); |
| 15929 |
|
| 15930 |
destroyPQExpBuffer(q); |
| 15931 |
destroyPQExpBuffer(delq); |
| 15932 |
} |
| 15933 |
|
| 15934 |
/* |
| 15935 |
* dumpForeignServer |
| 15936 |
* write out a foreign server definition |
| 15937 |
*/ |
| 15938 |
static void |
| 15939 |
dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo) |
| 15940 |
{ |
| 15941 |
DumpOptions *dopt = fout->dopt; |
| 15942 |
PQExpBuffer q; |
| 15943 |
PQExpBuffer delq; |
| 15944 |
PQExpBuffer query; |
| 15945 |
PGresult *res; |
| 15946 |
char *qsrvname; |
| 15947 |
char *fdwname; |
| 15948 |
|
| 15949 |
/* Do nothing if not dumping schema */ |
| 15950 |
if (!dopt->dumpSchema) |
| 15951 |
return; |
| 15952 |
|
| 15953 |
q = createPQExpBuffer(); |
| 15954 |
delq = createPQExpBuffer(); |
| 15955 |
query = createPQExpBuffer(); |
| 15956 |
|
| 15957 |
qsrvname = pg_strdup(fmtId(srvinfo->dobj.name)); |
| 15958 |
|
| 15959 |
/* look up the foreign-data wrapper */ |
| 15960 |
appendPQExpBuffer(query, "SELECT fdwname " |
| 15961 |
"FROM pg_foreign_data_wrapper w " |
| 15962 |
"WHERE w.oid = '%u'", |
| 15963 |
srvinfo->srvfdw); |
| 15964 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 15965 |
fdwname = PQgetvalue(res, 0, 0); |
| 15966 |
|
| 15967 |
appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname); |
| 15968 |
if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0) |
| 15969 |
{ |
| 15970 |
appendPQExpBufferStr(q, " TYPE "); |
| 15971 |
appendStringLiteralAH(q, srvinfo->srvtype, fout); |
| 15972 |
} |
| 15973 |
if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0) |
| 15974 |
{ |
| 15975 |
appendPQExpBufferStr(q, " VERSION "); |
| 15976 |
appendStringLiteralAH(q, srvinfo->srvversion, fout); |
| 15977 |
} |
| 15978 |
|
| 15979 |
appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER "); |
| 15980 |
appendPQExpBufferStr(q, fmtId(fdwname)); |
| 15981 |
|
| 15982 |
if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0) |
| 15983 |
appendPQExpBuffer(q, " OPTIONS (\n %s\n)", srvinfo->srvoptions); |
| 15984 |
|
| 15985 |
appendPQExpBufferStr(q, ";\n"); |
| 15986 |
|
| 15987 |
appendPQExpBuffer(delq, "DROP SERVER %s;\n", |
| 15988 |
qsrvname); |
| 15989 |
|
| 15990 |
if (dopt->binary_upgrade) |
| 15991 |
binary_upgrade_extension_member(q, &srvinfo->dobj, |
| 15992 |
"SERVER", qsrvname, NULL); |
| 15993 |
|
| 15994 |
if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 15995 |
ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId, |
| 15996 |
ARCHIVE_OPTS(.tag = srvinfo->dobj.name, |
| 15997 |
.owner = srvinfo->rolname, |
| 15998 |
.description = "SERVER", |
| 15999 |
.section = SECTION_PRE_DATA, |
| 16000 |
.createStmt = q->data, |
| 16001 |
.dropStmt = delq->data)); |
| 16002 |
|
| 16003 |
/* Dump Foreign Server Comments */ |
| 16004 |
if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 16005 |
dumpComment(fout, "SERVER", qsrvname, |
| 16006 |
NULL, srvinfo->rolname, |
| 16007 |
srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId); |
| 16008 |
|
| 16009 |
/* Handle the ACL */ |
| 16010 |
if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 16011 |
dumpACL(fout, srvinfo->dobj.dumpId, InvalidDumpId, |
| 16012 |
"FOREIGN SERVER", qsrvname, NULL, NULL, |
| 16013 |
NULL, srvinfo->rolname, &srvinfo->dacl); |
| 16014 |
|
| 16015 |
/* Dump user mappings */ |
| 16016 |
if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP) |
| 16017 |
dumpUserMappings(fout, |
| 16018 |
srvinfo->dobj.name, NULL, |
| 16019 |
srvinfo->rolname, |
| 16020 |
srvinfo->dobj.catId, srvinfo->dobj.dumpId); |
| 16021 |
|
| 16022 |
PQclear(res); |
| 16023 |
|
| 16024 |
free(qsrvname); |
| 16025 |
|
| 16026 |
destroyPQExpBuffer(q); |
| 16027 |
destroyPQExpBuffer(delq); |
| 16028 |
destroyPQExpBuffer(query); |
| 16029 |
} |
| 16030 |
|
| 16031 |
/* |
| 16032 |
* dumpUserMappings |
| 16033 |
* |
| 16034 |
* This routine is used to dump any user mappings associated with the |
| 16035 |
* server handed to this routine. Should be called after ArchiveEntry() |
| 16036 |
* for the server. |
| 16037 |
*/ |
| 16038 |
static void |
| 16039 |
dumpUserMappings(Archive *fout, |
| 16040 |
const char *servername, const char *namespace, |
| 16041 |
const char *owner, |
| 16042 |
CatalogId catalogId, DumpId dumpId) |
| 16043 |
{ |
| 16044 |
PQExpBuffer q; |
| 16045 |
PQExpBuffer delq; |
| 16046 |
PQExpBuffer query; |
| 16047 |
PQExpBuffer tag; |
| 16048 |
PGresult *res; |
| 16049 |
int ntups; |
| 16050 |
int i_usename; |
| 16051 |
int i_umoptions; |
| 16052 |
int i; |
| 16053 |
|
| 16054 |
q = createPQExpBuffer(); |
| 16055 |
tag = createPQExpBuffer(); |
| 16056 |
delq = createPQExpBuffer(); |
| 16057 |
query = createPQExpBuffer(); |
| 16058 |
|
| 16059 |
/* |
| 16060 |
* We read from the publicly accessible view pg_user_mappings, so as not |
| 16061 |
* to fail if run by a non-superuser. Note that the view will show |
| 16062 |
* umoptions as null if the user hasn't got privileges for the associated |
| 16063 |
* server; this means that pg_dump will dump such a mapping, but with no |
| 16064 |
* OPTIONS clause. A possible alternative is to skip such mappings |
| 16065 |
* altogether, but it's not clear that that's an improvement. |
| 16066 |
*/ |
| 16067 |
appendPQExpBuffer(query, |
| 16068 |
"SELECT usename, " |
| 16069 |
"array_to_string(ARRAY(" |
| 16070 |
"SELECT quote_ident(option_name) || ' ' || " |
| 16071 |
"quote_literal(option_value) " |
| 16072 |
"FROM pg_options_to_table(umoptions) " |
| 16073 |
"ORDER BY option_name" |
| 16074 |
"), E',\n ') AS umoptions " |
| 16075 |
"FROM pg_user_mappings " |
| 16076 |
"WHERE srvid = '%u' " |
| 16077 |
"ORDER BY usename", |
| 16078 |
catalogId.oid); |
| 16079 |
|
| 16080 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 16081 |
|
| 16082 |
ntups = PQntuples(res); |
| 16083 |
i_usename = PQfnumber(res, "usename"); |
| 16084 |
i_umoptions = PQfnumber(res, "umoptions"); |
| 16085 |
|
| 16086 |
for (i = 0; i < ntups; i++) |
| 16087 |
{ |
| 16088 |
char *usename; |
| 16089 |
char *umoptions; |
| 16090 |
|
| 16091 |
usename = PQgetvalue(res, i, i_usename); |
| 16092 |
umoptions = PQgetvalue(res, i, i_umoptions); |
| 16093 |
|
| 16094 |
resetPQExpBuffer(q); |
| 16095 |
appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename)); |
| 16096 |
appendPQExpBuffer(q, " SERVER %s", fmtId(servername)); |
| 16097 |
|
| 16098 |
if (umoptions && strlen(umoptions) > 0) |
| 16099 |
appendPQExpBuffer(q, " OPTIONS (\n %s\n)", umoptions); |
| 16100 |
|
| 16101 |
appendPQExpBufferStr(q, ";\n"); |
| 16102 |
|
| 16103 |
resetPQExpBuffer(delq); |
| 16104 |
appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename)); |
| 16105 |
appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername)); |
| 16106 |
|
| 16107 |
resetPQExpBuffer(tag); |
| 16108 |
appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s", |
| 16109 |
usename, servername); |
| 16110 |
|
| 16111 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 16112 |
ARCHIVE_OPTS(.tag = tag->data, |
| 16113 |
.namespace = namespace, |
| 16114 |
.owner = owner, |
| 16115 |
.description = "USER MAPPING", |
| 16116 |
.section = SECTION_PRE_DATA, |
| 16117 |
.createStmt = q->data, |
| 16118 |
.dropStmt = delq->data)); |
| 16119 |
} |
| 16120 |
|
| 16121 |
PQclear(res); |
| 16122 |
|
| 16123 |
destroyPQExpBuffer(query); |
| 16124 |
destroyPQExpBuffer(delq); |
| 16125 |
destroyPQExpBuffer(tag); |
| 16126 |
destroyPQExpBuffer(q); |
| 16127 |
} |
| 16128 |
|
| 16129 |
/* |
| 16130 |
* Write out default privileges information |
| 16131 |
*/ |
| 16132 |
static void |
| 16133 |
dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo) |
| 16134 |
{ |
| 16135 |
DumpOptions *dopt = fout->dopt; |
| 16136 |
PQExpBuffer q; |
| 16137 |
PQExpBuffer tag; |
| 16138 |
const char *type; |
| 16139 |
|
| 16140 |
/* Do nothing if not dumping schema, or if we're skipping ACLs */ |
| 16141 |
if (!dopt->dumpSchema || dopt->aclsSkip) |
| 16142 |
return; |
| 16143 |
|
| 16144 |
q = createPQExpBuffer(); |
| 16145 |
tag = createPQExpBuffer(); |
| 16146 |
|
| 16147 |
switch (daclinfo->defaclobjtype) |
| 16148 |
{ |
| 16149 |
case DEFACLOBJ_RELATION: |
| 16150 |
type = "TABLES"; |
| 16151 |
break; |
| 16152 |
case DEFACLOBJ_SEQUENCE: |
| 16153 |
type = "SEQUENCES"; |
| 16154 |
break; |
| 16155 |
case DEFACLOBJ_FUNCTION: |
| 16156 |
type = "FUNCTIONS"; |
| 16157 |
break; |
| 16158 |
case DEFACLOBJ_TYPE: |
| 16159 |
type = "TYPES"; |
| 16160 |
break; |
| 16161 |
case DEFACLOBJ_NAMESPACE: |
| 16162 |
type = "SCHEMAS"; |
| 16163 |
break; |
| 16164 |
case DEFACLOBJ_LARGEOBJECT: |
| 16165 |
type = "LARGE OBJECTS"; |
| 16166 |
break; |
| 16167 |
default: |
| 16168 |
/* shouldn't get here */ |
| 16169 |
pg_fatal("unrecognized object type in default privileges: %d", |
| 16170 |
(int) daclinfo->defaclobjtype); |
| 16171 |
type = ""; /* keep compiler quiet */ |
| 16172 |
} |
| 16173 |
|
| 16174 |
appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type); |
| 16175 |
|
| 16176 |
/* build the actual command(s) for this tuple */ |
| 16177 |
if (!buildDefaultACLCommands(type, |
| 16178 |
daclinfo->dobj.namespace != NULL ? |
| 16179 |
daclinfo->dobj.namespace->dobj.name : NULL, |
| 16180 |
daclinfo->dacl.acl, |
| 16181 |
daclinfo->dacl.acldefault, |
| 16182 |
daclinfo->defaclrole, |
| 16183 |
fout->remoteVersion, |
| 16184 |
q)) |
| 16185 |
pg_fatal("could not parse default ACL list (%s)", |
| 16186 |
daclinfo->dacl.acl); |
| 16187 |
|
| 16188 |
if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 16189 |
ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId, |
| 16190 |
ARCHIVE_OPTS(.tag = tag->data, |
| 16191 |
.namespace = daclinfo->dobj.namespace ? |
| 16192 |
daclinfo->dobj.namespace->dobj.name : NULL, |
| 16193 |
.owner = daclinfo->defaclrole, |
| 16194 |
.description = "DEFAULT ACL", |
| 16195 |
.section = SECTION_POST_DATA, |
| 16196 |
.createStmt = q->data)); |
| 16197 |
|
| 16198 |
destroyPQExpBuffer(tag); |
| 16199 |
destroyPQExpBuffer(q); |
| 16200 |
} |
| 16201 |
|
| 16202 |
/*---------- |
| 16203 |
* Write out grant/revoke information |
| 16204 |
* |
| 16205 |
* 'objDumpId' is the dump ID of the underlying object. |
| 16206 |
* 'altDumpId' can be a second dumpId that the ACL entry must also depend on, |
| 16207 |
* or InvalidDumpId if there is no need for a second dependency. |
| 16208 |
* 'type' must be one of |
| 16209 |
* TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE, |
| 16210 |
* FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT. |
| 16211 |
* 'name' is the formatted name of the object. Must be quoted etc. already. |
| 16212 |
* 'subname' is the formatted name of the sub-object, if any. Must be quoted. |
| 16213 |
* (Currently we assume that subname is only provided for table columns.) |
| 16214 |
* 'nspname' is the namespace the object is in (NULL if none). |
| 16215 |
* 'tag' is the tag to use for the ACL TOC entry; typically, this is NULL |
| 16216 |
* to use the default for the object type. |
| 16217 |
* 'owner' is the owner, NULL if there is no owner (for languages). |
| 16218 |
* 'dacl' is the DumpableAcl struct for the object. |
| 16219 |
* |
| 16220 |
* Returns the dump ID assigned to the ACL TocEntry, or InvalidDumpId if |
| 16221 |
* no ACL entry was created. |
| 16222 |
*---------- |
| 16223 |
*/ |
| 16224 |
static DumpId |
| 16225 |
dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, |
| 16226 |
const char *type, const char *name, const char *subname, |
| 16227 |
const char *nspname, const char *tag, const char *owner, |
| 16228 |
const DumpableAcl *dacl) |
| 16229 |
{ |
| 16230 |
DumpId aclDumpId = InvalidDumpId; |
| 16231 |
DumpOptions *dopt = fout->dopt; |
| 16232 |
const char *acls = dacl->acl; |
| 16233 |
const char *acldefault = dacl->acldefault; |
| 16234 |
char privtype = dacl->privtype; |
| 16235 |
const char *initprivs = dacl->initprivs; |
| 16236 |
const char *baseacls; |
| 16237 |
PQExpBuffer sql; |
| 16238 |
|
| 16239 |
/* Do nothing if ACL dump is not enabled */ |
| 16240 |
if (dopt->aclsSkip) |
| 16241 |
return InvalidDumpId; |
| 16242 |
|
| 16243 |
/* --data-only skips ACLs *except* large object ACLs */ |
| 16244 |
if (!dopt->dumpSchema && strcmp(type, "LARGE OBJECT") != 0) |
| 16245 |
return InvalidDumpId; |
| 16246 |
|
| 16247 |
sql = createPQExpBuffer(); |
| 16248 |
|
| 16249 |
/* |
| 16250 |
* In binary upgrade mode, we don't run an extension's script but instead |
| 16251 |
* dump out the objects independently and then recreate them. To preserve |
| 16252 |
* any initial privileges which were set on extension objects, we need to |
| 16253 |
* compute the set of GRANT and REVOKE commands necessary to get from the |
| 16254 |
* default privileges of an object to its initial privileges as recorded |
| 16255 |
* in pg_init_privs. |
| 16256 |
* |
| 16257 |
* At restore time, we apply these commands after having called |
| 16258 |
* binary_upgrade_set_record_init_privs(true). That tells the backend to |
| 16259 |
* copy the results into pg_init_privs. This is how we preserve the |
| 16260 |
* contents of that catalog across binary upgrades. |
| 16261 |
*/ |
| 16262 |
if (dopt->binary_upgrade && privtype == 'e' && |
| 16263 |
initprivs && *initprivs != '\0') |
| 16264 |
{ |
| 16265 |
appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n"); |
| 16266 |
if (!buildACLCommands(name, subname, nspname, type, |
| 16267 |
initprivs, acldefault, owner, |
| 16268 |
"", fout->remoteVersion, sql)) |
| 16269 |
pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)", |
| 16270 |
initprivs, acldefault, name, type); |
| 16271 |
appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n"); |
| 16272 |
} |
| 16273 |
|
| 16274 |
/* |
| 16275 |
* Now figure the GRANT and REVOKE commands needed to get to the object's |
| 16276 |
* actual current ACL, starting from the initprivs if given, else from the |
| 16277 |
* object-type-specific default. Also, while buildACLCommands will assume |
| 16278 |
* that a NULL/empty acls string means it needn't do anything, what that |
| 16279 |
* actually represents is the object-type-specific default; so we need to |
| 16280 |
* substitute the acldefault string to get the right results in that case. |
| 16281 |
*/ |
| 16282 |
if (initprivs && *initprivs != '\0') |
| 16283 |
{ |
| 16284 |
baseacls = initprivs; |
| 16285 |
if (acls == NULL || *acls == '\0') |
| 16286 |
acls = acldefault; |
| 16287 |
} |
| 16288 |
else |
| 16289 |
baseacls = acldefault; |
| 16290 |
|
| 16291 |
if (!buildACLCommands(name, subname, nspname, type, |
| 16292 |
acls, baseacls, owner, |
| 16293 |
"", fout->remoteVersion, sql)) |
| 16294 |
pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)", |
| 16295 |
acls, baseacls, name, type); |
| 16296 |
|
| 16297 |
if (sql->len > 0) |
| 16298 |
{ |
| 16299 |
PQExpBuffer tagbuf = createPQExpBuffer(); |
| 16300 |
DumpId aclDeps[2]; |
| 16301 |
int nDeps = 0; |
| 16302 |
|
| 16303 |
if (tag) |
| 16304 |
appendPQExpBufferStr(tagbuf, tag); |
| 16305 |
else if (subname) |
| 16306 |
appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname); |
| 16307 |
else |
| 16308 |
appendPQExpBuffer(tagbuf, "%s %s", type, name); |
| 16309 |
|
| 16310 |
aclDeps[nDeps++] = objDumpId; |
| 16311 |
if (altDumpId != InvalidDumpId) |
| 16312 |
aclDeps[nDeps++] = altDumpId; |
| 16313 |
|
| 16314 |
aclDumpId = createDumpId(); |
| 16315 |
|
| 16316 |
ArchiveEntry(fout, nilCatalogId, aclDumpId, |
| 16317 |
ARCHIVE_OPTS(.tag = tagbuf->data, |
| 16318 |
.namespace = nspname, |
| 16319 |
.owner = owner, |
| 16320 |
.description = "ACL", |
| 16321 |
.section = SECTION_NONE, |
| 16322 |
.createStmt = sql->data, |
| 16323 |
.deps = aclDeps, |
| 16324 |
.nDeps = nDeps)); |
| 16325 |
|
| 16326 |
destroyPQExpBuffer(tagbuf); |
| 16327 |
} |
| 16328 |
|
| 16329 |
destroyPQExpBuffer(sql); |
| 16330 |
|
| 16331 |
return aclDumpId; |
| 16332 |
} |
| 16333 |
|
| 16334 |
/* |
| 16335 |
* dumpSecLabel |
| 16336 |
* |
| 16337 |
* This routine is used to dump any security labels associated with the |
| 16338 |
* object handed to this routine. The routine takes the object type |
| 16339 |
* and object name (ready to print, except for schema decoration), plus |
| 16340 |
* the namespace and owner of the object (for labeling the ArchiveEntry), |
| 16341 |
* plus catalog ID and subid which are the lookup key for pg_seclabel, |
| 16342 |
* plus the dump ID for the object (for setting a dependency). |
| 16343 |
* If a matching pg_seclabel entry is found, it is dumped. |
| 16344 |
* |
| 16345 |
* Note: although this routine takes a dumpId for dependency purposes, |
| 16346 |
* that purpose is just to mark the dependency in the emitted dump file |
| 16347 |
* for possible future use by pg_restore. We do NOT use it for determining |
| 16348 |
* ordering of the label in the dump file, because this routine is called |
| 16349 |
* after dependency sorting occurs. This routine should be called just after |
| 16350 |
* calling ArchiveEntry() for the specified object. |
| 16351 |
*/ |
| 16352 |
static void |
| 16353 |
dumpSecLabel(Archive *fout, const char *type, const char *name, |
| 16354 |
const char *namespace, const char *owner, |
| 16355 |
CatalogId catalogId, int subid, DumpId dumpId) |
| 16356 |
{ |
| 16357 |
DumpOptions *dopt = fout->dopt; |
| 16358 |
SecLabelItem *labels; |
| 16359 |
int nlabels; |
| 16360 |
int i; |
| 16361 |
PQExpBuffer query; |
| 16362 |
|
| 16363 |
/* do nothing, if --no-security-labels is supplied */ |
| 16364 |
if (dopt->no_security_labels) |
| 16365 |
return; |
| 16366 |
|
| 16367 |
/* |
| 16368 |
* Security labels are schema not data ... except large object labels are |
| 16369 |
* data |
| 16370 |
*/ |
| 16371 |
if (strcmp(type, "LARGE OBJECT") != 0) |
| 16372 |
{ |
| 16373 |
if (!dopt->dumpSchema) |
| 16374 |
return; |
| 16375 |
} |
| 16376 |
else |
| 16377 |
{ |
| 16378 |
/* We do dump large object security labels in binary-upgrade mode */ |
| 16379 |
if (!dopt->dumpData && !dopt->binary_upgrade) |
| 16380 |
return; |
| 16381 |
} |
| 16382 |
|
| 16383 |
/* Search for security labels associated with catalogId, using table */ |
| 16384 |
nlabels = findSecLabels(catalogId.tableoid, catalogId.oid, &labels); |
| 16385 |
|
| 16386 |
query = createPQExpBuffer(); |
| 16387 |
|
| 16388 |
for (i = 0; i < nlabels; i++) |
| 16389 |
{ |
| 16390 |
/* |
| 16391 |
* Ignore label entries for which the subid doesn't match. |
| 16392 |
*/ |
| 16393 |
if (labels[i].objsubid != subid) |
| 16394 |
continue; |
| 16395 |
|
| 16396 |
appendPQExpBuffer(query, |
| 16397 |
"SECURITY LABEL FOR %s ON %s ", |
| 16398 |
fmtId(labels[i].provider), type); |
| 16399 |
if (namespace && *namespace) |
| 16400 |
appendPQExpBuffer(query, "%s.", fmtId(namespace)); |
| 16401 |
appendPQExpBuffer(query, "%s IS ", name); |
| 16402 |
appendStringLiteralAH(query, labels[i].label, fout); |
| 16403 |
appendPQExpBufferStr(query, ";\n"); |
| 16404 |
} |
| 16405 |
|
| 16406 |
if (query->len > 0) |
| 16407 |
{ |
| 16408 |
PQExpBuffer tag = createPQExpBuffer(); |
| 16409 |
|
| 16410 |
appendPQExpBuffer(tag, "%s %s", type, name); |
| 16411 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 16412 |
ARCHIVE_OPTS(.tag = tag->data, |
| 16413 |
.namespace = namespace, |
| 16414 |
.owner = owner, |
| 16415 |
.description = "SECURITY LABEL", |
| 16416 |
.section = SECTION_NONE, |
| 16417 |
.createStmt = query->data, |
| 16418 |
.deps = &dumpId, |
| 16419 |
.nDeps = 1)); |
| 16420 |
destroyPQExpBuffer(tag); |
| 16421 |
} |
| 16422 |
|
| 16423 |
destroyPQExpBuffer(query); |
| 16424 |
} |
| 16425 |
|
| 16426 |
/* |
| 16427 |
* dumpTableSecLabel |
| 16428 |
* |
| 16429 |
* As above, but dump security label for both the specified table (or view) |
| 16430 |
* and its columns. |
| 16431 |
*/ |
| 16432 |
static void |
| 16433 |
dumpTableSecLabel(Archive *fout, const TableInfo *tbinfo, const char *reltypename) |
| 16434 |
{ |
| 16435 |
DumpOptions *dopt = fout->dopt; |
| 16436 |
SecLabelItem *labels; |
| 16437 |
int nlabels; |
| 16438 |
int i; |
| 16439 |
PQExpBuffer query; |
| 16440 |
PQExpBuffer target; |
| 16441 |
|
| 16442 |
/* do nothing, if --no-security-labels is supplied */ |
| 16443 |
if (dopt->no_security_labels) |
| 16444 |
return; |
| 16445 |
|
| 16446 |
/* SecLabel are SCHEMA not data */ |
| 16447 |
if (!dopt->dumpSchema) |
| 16448 |
return; |
| 16449 |
|
| 16450 |
/* Search for comments associated with relation, using table */ |
| 16451 |
nlabels = findSecLabels(tbinfo->dobj.catId.tableoid, |
| 16452 |
tbinfo->dobj.catId.oid, |
| 16453 |
&labels); |
| 16454 |
|
| 16455 |
/* If security labels exist, build SECURITY LABEL statements */ |
| 16456 |
if (nlabels <= 0) |
| 16457 |
return; |
| 16458 |
|
| 16459 |
query = createPQExpBuffer(); |
| 16460 |
target = createPQExpBuffer(); |
| 16461 |
|
| 16462 |
for (i = 0; i < nlabels; i++) |
| 16463 |
{ |
| 16464 |
const char *colname; |
| 16465 |
const char *provider = labels[i].provider; |
| 16466 |
const char *label = labels[i].label; |
| 16467 |
int objsubid = labels[i].objsubid; |
| 16468 |
|
| 16469 |
resetPQExpBuffer(target); |
| 16470 |
if (objsubid == 0) |
| 16471 |
{ |
| 16472 |
appendPQExpBuffer(target, "%s %s", reltypename, |
| 16473 |
fmtQualifiedDumpable(tbinfo)); |
| 16474 |
} |
| 16475 |
else |
| 16476 |
{ |
| 16477 |
colname = getAttrName(objsubid, tbinfo); |
| 16478 |
/* first fmtXXX result must be consumed before calling again */ |
| 16479 |
appendPQExpBuffer(target, "COLUMN %s", |
| 16480 |
fmtQualifiedDumpable(tbinfo)); |
| 16481 |
appendPQExpBuffer(target, ".%s", fmtId(colname)); |
| 16482 |
} |
| 16483 |
appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ", |
| 16484 |
fmtId(provider), target->data); |
| 16485 |
appendStringLiteralAH(query, label, fout); |
| 16486 |
appendPQExpBufferStr(query, ";\n"); |
| 16487 |
} |
| 16488 |
if (query->len > 0) |
| 16489 |
{ |
| 16490 |
resetPQExpBuffer(target); |
| 16491 |
appendPQExpBuffer(target, "%s %s", reltypename, |
| 16492 |
fmtId(tbinfo->dobj.name)); |
| 16493 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 16494 |
ARCHIVE_OPTS(.tag = target->data, |
| 16495 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 16496 |
.owner = tbinfo->rolname, |
| 16497 |
.description = "SECURITY LABEL", |
| 16498 |
.section = SECTION_NONE, |
| 16499 |
.createStmt = query->data, |
| 16500 |
.deps = &(tbinfo->dobj.dumpId), |
| 16501 |
.nDeps = 1)); |
| 16502 |
} |
| 16503 |
destroyPQExpBuffer(query); |
| 16504 |
destroyPQExpBuffer(target); |
| 16505 |
} |
| 16506 |
|
| 16507 |
/* |
| 16508 |
* findSecLabels |
| 16509 |
* |
| 16510 |
* Find the security label(s), if any, associated with the given object. |
| 16511 |
* All the objsubid values associated with the given classoid/objoid are |
| 16512 |
* found with one search. |
| 16513 |
*/ |
| 16514 |
static int |
| 16515 |
findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items) |
| 16516 |
{ |
| 16517 |
SecLabelItem *middle = NULL; |
| 16518 |
SecLabelItem *low; |
| 16519 |
SecLabelItem *high; |
| 16520 |
int nmatch; |
| 16521 |
|
| 16522 |
if (nseclabels <= 0) /* no labels, so no match is possible */ |
| 16523 |
{ |
| 16524 |
*items = NULL; |
| 16525 |
return 0; |
| 16526 |
} |
| 16527 |
|
| 16528 |
/* |
| 16529 |
* Do binary search to find some item matching the object. |
| 16530 |
*/ |
| 16531 |
low = &seclabels[0]; |
| 16532 |
high = &seclabels[nseclabels - 1]; |
| 16533 |
while (low <= high) |
| 16534 |
{ |
| 16535 |
middle = low + (high - low) / 2; |
| 16536 |
|
| 16537 |
if (classoid < middle->classoid) |
| 16538 |
high = middle - 1; |
| 16539 |
else if (classoid > middle->classoid) |
| 16540 |
low = middle + 1; |
| 16541 |
else if (objoid < middle->objoid) |
| 16542 |
high = middle - 1; |
| 16543 |
else if (objoid > middle->objoid) |
| 16544 |
low = middle + 1; |
| 16545 |
else |
| 16546 |
break; /* found a match */ |
| 16547 |
} |
| 16548 |
|
| 16549 |
if (low > high) /* no matches */ |
| 16550 |
{ |
| 16551 |
*items = NULL; |
| 16552 |
return 0; |
| 16553 |
} |
| 16554 |
|
| 16555 |
/* |
| 16556 |
* Now determine how many items match the object. The search loop |
| 16557 |
* invariant still holds: only items between low and high inclusive could |
| 16558 |
* match. |
| 16559 |
*/ |
| 16560 |
nmatch = 1; |
| 16561 |
while (middle > low) |
| 16562 |
{ |
| 16563 |
if (classoid != middle[-1].classoid || |
| 16564 |
objoid != middle[-1].objoid) |
| 16565 |
break; |
| 16566 |
middle--; |
| 16567 |
nmatch++; |
| 16568 |
} |
| 16569 |
|
| 16570 |
*items = middle; |
| 16571 |
|
| 16572 |
middle += nmatch; |
| 16573 |
while (middle <= high) |
| 16574 |
{ |
| 16575 |
if (classoid != middle->classoid || |
| 16576 |
objoid != middle->objoid) |
| 16577 |
break; |
| 16578 |
middle++; |
| 16579 |
nmatch++; |
| 16580 |
} |
| 16581 |
|
| 16582 |
return nmatch; |
| 16583 |
} |
| 16584 |
|
| 16585 |
/* |
| 16586 |
* collectSecLabels |
| 16587 |
* |
| 16588 |
* Construct a table of all security labels available for database objects; |
| 16589 |
* also set the has-seclabel component flag for each relevant object. |
| 16590 |
* |
| 16591 |
* The table is sorted by classoid/objid/objsubid for speed in lookup. |
| 16592 |
*/ |
| 16593 |
static void |
| 16594 |
collectSecLabels(Archive *fout) |
| 16595 |
{ |
| 16596 |
PGresult *res; |
| 16597 |
PQExpBuffer query; |
| 16598 |
int i_label; |
| 16599 |
int i_provider; |
| 16600 |
int i_classoid; |
| 16601 |
int i_objoid; |
| 16602 |
int i_objsubid; |
| 16603 |
int ntups; |
| 16604 |
int i; |
| 16605 |
DumpableObject *dobj; |
| 16606 |
|
| 16607 |
query = createPQExpBuffer(); |
| 16608 |
|
| 16609 |
appendPQExpBufferStr(query, |
| 16610 |
"SELECT label, provider, classoid, objoid, objsubid " |
| 16611 |
"FROM pg_catalog.pg_seclabels " |
| 16612 |
"ORDER BY classoid, objoid, objsubid"); |
| 16613 |
|
| 16614 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 16615 |
|
| 16616 |
/* Construct lookup table containing OIDs in numeric form */ |
| 16617 |
i_label = PQfnumber(res, "label"); |
| 16618 |
i_provider = PQfnumber(res, "provider"); |
| 16619 |
i_classoid = PQfnumber(res, "classoid"); |
| 16620 |
i_objoid = PQfnumber(res, "objoid"); |
| 16621 |
i_objsubid = PQfnumber(res, "objsubid"); |
| 16622 |
|
| 16623 |
ntups = PQntuples(res); |
| 16624 |
|
| 16625 |
seclabels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem)); |
| 16626 |
nseclabels = 0; |
| 16627 |
dobj = NULL; |
| 16628 |
|
| 16629 |
for (i = 0; i < ntups; i++) |
| 16630 |
{ |
| 16631 |
CatalogId objId; |
| 16632 |
int subid; |
| 16633 |
|
| 16634 |
objId.tableoid = atooid(PQgetvalue(res, i, i_classoid)); |
| 16635 |
objId.oid = atooid(PQgetvalue(res, i, i_objoid)); |
| 16636 |
subid = atoi(PQgetvalue(res, i, i_objsubid)); |
| 16637 |
|
| 16638 |
/* We needn't remember labels that don't match any dumpable object */ |
| 16639 |
if (dobj == NULL || |
| 16640 |
dobj->catId.tableoid != objId.tableoid || |
| 16641 |
dobj->catId.oid != objId.oid) |
| 16642 |
dobj = findObjectByCatalogId(objId); |
| 16643 |
if (dobj == NULL) |
| 16644 |
continue; |
| 16645 |
|
| 16646 |
/* |
| 16647 |
* Labels on columns of composite types are linked to the type's |
| 16648 |
* pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag |
| 16649 |
* in the type's own DumpableObject. |
| 16650 |
*/ |
| 16651 |
if (subid != 0 && dobj->objType == DO_TABLE && |
| 16652 |
((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE) |
| 16653 |
{ |
| 16654 |
TypeInfo *cTypeInfo; |
| 16655 |
|
| 16656 |
cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype); |
| 16657 |
if (cTypeInfo) |
| 16658 |
cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL; |
| 16659 |
} |
| 16660 |
else |
| 16661 |
dobj->components |= DUMP_COMPONENT_SECLABEL; |
| 16662 |
|
| 16663 |
seclabels[nseclabels].label = pg_strdup(PQgetvalue(res, i, i_label)); |
| 16664 |
seclabels[nseclabels].provider = pg_strdup(PQgetvalue(res, i, i_provider)); |
| 16665 |
seclabels[nseclabels].classoid = objId.tableoid; |
| 16666 |
seclabels[nseclabels].objoid = objId.oid; |
| 16667 |
seclabels[nseclabels].objsubid = subid; |
| 16668 |
nseclabels++; |
| 16669 |
} |
| 16670 |
|
| 16671 |
PQclear(res); |
| 16672 |
destroyPQExpBuffer(query); |
| 16673 |
} |
| 16674 |
|
| 16675 |
/* |
| 16676 |
* dumpTable |
| 16677 |
* write out to fout the declarations (not data) of a user-defined table |
| 16678 |
*/ |
| 16679 |
static void |
| 16680 |
dumpTable(Archive *fout, const TableInfo *tbinfo) |
| 16681 |
{ |
| 16682 |
DumpOptions *dopt = fout->dopt; |
| 16683 |
DumpId tableAclDumpId = InvalidDumpId; |
| 16684 |
char *namecopy; |
| 16685 |
|
| 16686 |
/* Do nothing if not dumping schema */ |
| 16687 |
if (!dopt->dumpSchema) |
| 16688 |
return; |
| 16689 |
|
| 16690 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 16691 |
{ |
| 16692 |
if (tbinfo->relkind == RELKIND_SEQUENCE) |
| 16693 |
dumpSequence(fout, tbinfo); |
| 16694 |
else |
| 16695 |
dumpTableSchema(fout, tbinfo); |
| 16696 |
} |
| 16697 |
|
| 16698 |
/* Handle the ACL here */ |
| 16699 |
namecopy = pg_strdup(fmtId(tbinfo->dobj.name)); |
| 16700 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL) |
| 16701 |
{ |
| 16702 |
const char *objtype = |
| 16703 |
(tbinfo->relkind == RELKIND_SEQUENCE) ? "SEQUENCE" : "TABLE"; |
| 16704 |
|
| 16705 |
tableAclDumpId = |
| 16706 |
dumpACL(fout, tbinfo->dobj.dumpId, InvalidDumpId, |
| 16707 |
objtype, namecopy, NULL, |
| 16708 |
tbinfo->dobj.namespace->dobj.name, |
| 16709 |
NULL, tbinfo->rolname, &tbinfo->dacl); |
| 16710 |
} |
| 16711 |
|
| 16712 |
/* |
| 16713 |
* Handle column ACLs, if any. Note: we pull these with a separate query |
| 16714 |
* rather than trying to fetch them during getTableAttrs, so that we won't |
| 16715 |
* miss ACLs on system columns. Doing it this way also allows us to dump |
| 16716 |
* ACLs for catalogs that we didn't mark "interesting" back in getTables. |
| 16717 |
*/ |
| 16718 |
if ((tbinfo->dobj.dump & DUMP_COMPONENT_ACL) && tbinfo->hascolumnACLs) |
| 16719 |
{ |
| 16720 |
PQExpBuffer query = createPQExpBuffer(); |
| 16721 |
PGresult *res; |
| 16722 |
int i; |
| 16723 |
|
| 16724 |
if (!fout->is_prepared[PREPQUERY_GETCOLUMNACLS]) |
| 16725 |
{ |
| 16726 |
/* Set up query for column ACLs */ |
| 16727 |
appendPQExpBufferStr(query, |
| 16728 |
"PREPARE getColumnACLs(pg_catalog.oid) AS\n"); |
| 16729 |
|
| 16730 |
if (fout->remoteVersion >= 90600) |
| 16731 |
{ |
| 16732 |
/* |
| 16733 |
* In principle we should call acldefault('c', relowner) to |
| 16734 |
* get the default ACL for a column. However, we don't |
| 16735 |
* currently store the numeric OID of the relowner in |
| 16736 |
* TableInfo. We could convert the owner name using regrole, |
| 16737 |
* but that creates a risk of failure due to concurrent role |
| 16738 |
* renames. Given that the default ACL for columns is empty |
| 16739 |
* and is likely to stay that way, it's not worth extra cycles |
| 16740 |
* and risk to avoid hard-wiring that knowledge here. |
| 16741 |
*/ |
| 16742 |
appendPQExpBufferStr(query, |
| 16743 |
"SELECT at.attname, " |
| 16744 |
"at.attacl, " |
| 16745 |
"'{}' AS acldefault, " |
| 16746 |
"pip.privtype, pip.initprivs " |
| 16747 |
"FROM pg_catalog.pg_attribute at " |
| 16748 |
"LEFT JOIN pg_catalog.pg_init_privs pip ON " |
| 16749 |
"(at.attrelid = pip.objoid " |
| 16750 |
"AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass " |
| 16751 |
"AND at.attnum = pip.objsubid) " |
| 16752 |
"WHERE at.attrelid = $1 AND " |
| 16753 |
"NOT at.attisdropped " |
| 16754 |
"AND (at.attacl IS NOT NULL OR pip.initprivs IS NOT NULL) " |
| 16755 |
"ORDER BY at.attnum"); |
| 16756 |
} |
| 16757 |
else |
| 16758 |
{ |
| 16759 |
appendPQExpBufferStr(query, |
| 16760 |
"SELECT attname, attacl, '{}' AS acldefault, " |
| 16761 |
"NULL AS privtype, NULL AS initprivs " |
| 16762 |
"FROM pg_catalog.pg_attribute " |
| 16763 |
"WHERE attrelid = $1 AND NOT attisdropped " |
| 16764 |
"AND attacl IS NOT NULL " |
| 16765 |
"ORDER BY attnum"); |
| 16766 |
} |
| 16767 |
|
| 16768 |
ExecuteSqlStatement(fout, query->data); |
| 16769 |
|
| 16770 |
fout->is_prepared[PREPQUERY_GETCOLUMNACLS] = true; |
| 16771 |
} |
| 16772 |
|
| 16773 |
printfPQExpBuffer(query, |
| 16774 |
"EXECUTE getColumnACLs('%u')", |
| 16775 |
tbinfo->dobj.catId.oid); |
| 16776 |
|
| 16777 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 16778 |
|
| 16779 |
for (i = 0; i < PQntuples(res); i++) |
| 16780 |
{ |
| 16781 |
char *attname = PQgetvalue(res, i, 0); |
| 16782 |
char *attacl = PQgetvalue(res, i, 1); |
| 16783 |
char *acldefault = PQgetvalue(res, i, 2); |
| 16784 |
char privtype = *(PQgetvalue(res, i, 3)); |
| 16785 |
char *initprivs = PQgetvalue(res, i, 4); |
| 16786 |
DumpableAcl coldacl; |
| 16787 |
char *attnamecopy; |
| 16788 |
|
| 16789 |
coldacl.acl = attacl; |
| 16790 |
coldacl.acldefault = acldefault; |
| 16791 |
coldacl.privtype = privtype; |
| 16792 |
coldacl.initprivs = initprivs; |
| 16793 |
attnamecopy = pg_strdup(fmtId(attname)); |
| 16794 |
|
| 16795 |
/* |
| 16796 |
* Column's GRANT type is always TABLE. Each column ACL depends |
| 16797 |
* on the table-level ACL, since we can restore column ACLs in |
| 16798 |
* parallel but the table-level ACL has to be done first. |
| 16799 |
*/ |
| 16800 |
dumpACL(fout, tbinfo->dobj.dumpId, tableAclDumpId, |
| 16801 |
"TABLE", namecopy, attnamecopy, |
| 16802 |
tbinfo->dobj.namespace->dobj.name, |
| 16803 |
NULL, tbinfo->rolname, &coldacl); |
| 16804 |
free(attnamecopy); |
| 16805 |
} |
| 16806 |
PQclear(res); |
| 16807 |
destroyPQExpBuffer(query); |
| 16808 |
} |
| 16809 |
|
| 16810 |
free(namecopy); |
| 16811 |
} |
| 16812 |
|
| 16813 |
/* |
| 16814 |
* Create the AS clause for a view or materialized view. The semicolon is |
| 16815 |
* stripped because a materialized view must add a WITH NO DATA clause. |
| 16816 |
* |
| 16817 |
* This returns a new buffer which must be freed by the caller. |
| 16818 |
*/ |
| 16819 |
static PQExpBuffer |
| 16820 |
createViewAsClause(Archive *fout, const TableInfo *tbinfo) |
| 16821 |
{ |
| 16822 |
PQExpBuffer query = createPQExpBuffer(); |
| 16823 |
PQExpBuffer result = createPQExpBuffer(); |
| 16824 |
PGresult *res; |
| 16825 |
int len; |
| 16826 |
|
| 16827 |
/* Fetch the view definition */ |
| 16828 |
appendPQExpBuffer(query, |
| 16829 |
"SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef", |
| 16830 |
tbinfo->dobj.catId.oid); |
| 16831 |
|
| 16832 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 16833 |
|
| 16834 |
if (PQntuples(res) != 1) |
| 16835 |
{ |
| 16836 |
if (PQntuples(res) < 1) |
| 16837 |
pg_fatal("query to obtain definition of view \"%s\" returned no data", |
| 16838 |
tbinfo->dobj.name); |
| 16839 |
else |
| 16840 |
pg_fatal("query to obtain definition of view \"%s\" returned more than one definition", |
| 16841 |
tbinfo->dobj.name); |
| 16842 |
} |
| 16843 |
|
| 16844 |
len = PQgetlength(res, 0, 0); |
| 16845 |
|
| 16846 |
if (len == 0) |
| 16847 |
pg_fatal("definition of view \"%s\" appears to be empty (length zero)", |
| 16848 |
tbinfo->dobj.name); |
| 16849 |
|
| 16850 |
/* Strip off the trailing semicolon so that other things may follow. */ |
| 16851 |
Assert(PQgetvalue(res, 0, 0)[len - 1] == ';'); |
| 16852 |
appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1); |
| 16853 |
|
| 16854 |
PQclear(res); |
| 16855 |
destroyPQExpBuffer(query); |
| 16856 |
|
| 16857 |
return result; |
| 16858 |
} |
| 16859 |
|
| 16860 |
/* |
| 16861 |
* Create a dummy AS clause for a view. This is used when the real view |
| 16862 |
* definition has to be postponed because of circular dependencies. |
| 16863 |
* We must duplicate the view's external properties -- column names and types |
| 16864 |
* (including collation) -- so that it works for subsequent references. |
| 16865 |
* |
| 16866 |
* This returns a new buffer which must be freed by the caller. |
| 16867 |
*/ |
| 16868 |
static PQExpBuffer |
| 16869 |
createDummyViewAsClause(Archive *fout, const TableInfo *tbinfo) |
| 16870 |
{ |
| 16871 |
PQExpBuffer result = createPQExpBuffer(); |
| 16872 |
int j; |
| 16873 |
|
| 16874 |
appendPQExpBufferStr(result, "SELECT"); |
| 16875 |
|
| 16876 |
for (j = 0; j < tbinfo->numatts; j++) |
| 16877 |
{ |
| 16878 |
if (j > 0) |
| 16879 |
appendPQExpBufferChar(result, ','); |
| 16880 |
appendPQExpBufferStr(result, "\n "); |
| 16881 |
|
| 16882 |
appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]); |
| 16883 |
|
| 16884 |
/* |
| 16885 |
* Must add collation if not default for the type, because CREATE OR |
| 16886 |
* REPLACE VIEW won't change it |
| 16887 |
*/ |
| 16888 |
if (OidIsValid(tbinfo->attcollation[j])) |
| 16889 |
{ |
| 16890 |
CollInfo *coll; |
| 16891 |
|
| 16892 |
coll = findCollationByOid(tbinfo->attcollation[j]); |
| 16893 |
if (coll) |
| 16894 |
appendPQExpBuffer(result, " COLLATE %s", |
| 16895 |
fmtQualifiedDumpable(coll)); |
| 16896 |
} |
| 16897 |
|
| 16898 |
appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j])); |
| 16899 |
} |
| 16900 |
|
| 16901 |
return result; |
| 16902 |
} |
| 16903 |
|
| 16904 |
/* |
| 16905 |
* dumpTableSchema |
| 16906 |
* write the declaration (not data) of one user-defined table or view |
| 16907 |
*/ |
| 16908 |
static void |
| 16909 |
dumpTableSchema(Archive *fout, const TableInfo *tbinfo) |
| 16910 |
{ |
| 16911 |
DumpOptions *dopt = fout->dopt; |
| 16912 |
PQExpBuffer q = createPQExpBuffer(); |
| 16913 |
PQExpBuffer delq = createPQExpBuffer(); |
| 16914 |
PQExpBuffer extra = createPQExpBuffer(); |
| 16915 |
char *qrelname; |
| 16916 |
char *qualrelname; |
| 16917 |
int numParents; |
| 16918 |
TableInfo **parents; |
| 16919 |
int actual_atts; /* number of attrs in this CREATE statement */ |
| 16920 |
const char *reltypename; |
| 16921 |
char *storage; |
| 16922 |
int j, |
| 16923 |
k; |
| 16924 |
|
| 16925 |
/* We had better have loaded per-column details about this table */ |
| 16926 |
Assert(tbinfo->interesting); |
| 16927 |
|
| 16928 |
qrelname = pg_strdup(fmtId(tbinfo->dobj.name)); |
| 16929 |
qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo)); |
| 16930 |
|
| 16931 |
if (tbinfo->hasoids) |
| 16932 |
pg_log_warning("WITH OIDS is not supported anymore (table \"%s\")", |
| 16933 |
qrelname); |
| 16934 |
|
| 16935 |
if (dopt->binary_upgrade) |
| 16936 |
binary_upgrade_set_type_oids_by_rel(fout, q, tbinfo); |
| 16937 |
|
| 16938 |
/* Is it a table or a view? */ |
| 16939 |
if (tbinfo->relkind == RELKIND_VIEW) |
| 16940 |
{ |
| 16941 |
PQExpBuffer result; |
| 16942 |
|
| 16943 |
/* |
| 16944 |
* Note: keep this code in sync with the is_view case in dumpRule() |
| 16945 |
*/ |
| 16946 |
|
| 16947 |
reltypename = "VIEW"; |
| 16948 |
|
| 16949 |
appendPQExpBuffer(delq, "DROP VIEW %s;\n", qualrelname); |
| 16950 |
|
| 16951 |
if (dopt->binary_upgrade) |
| 16952 |
binary_upgrade_set_pg_class_oids(fout, q, |
| 16953 |
tbinfo->dobj.catId.oid); |
| 16954 |
|
| 16955 |
appendPQExpBuffer(q, "CREATE VIEW %s", qualrelname); |
| 16956 |
|
| 16957 |
if (tbinfo->dummy_view) |
| 16958 |
result = createDummyViewAsClause(fout, tbinfo); |
| 16959 |
else |
| 16960 |
{ |
| 16961 |
if (nonemptyReloptions(tbinfo->reloptions)) |
| 16962 |
{ |
| 16963 |
appendPQExpBufferStr(q, " WITH ("); |
| 16964 |
appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout); |
| 16965 |
appendPQExpBufferChar(q, ')'); |
| 16966 |
} |
| 16967 |
result = createViewAsClause(fout, tbinfo); |
| 16968 |
} |
| 16969 |
appendPQExpBuffer(q, " AS\n%s", result->data); |
| 16970 |
destroyPQExpBuffer(result); |
| 16971 |
|
| 16972 |
if (tbinfo->checkoption != NULL && !tbinfo->dummy_view) |
| 16973 |
appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption); |
| 16974 |
appendPQExpBufferStr(q, ";\n"); |
| 16975 |
} |
| 16976 |
else |
| 16977 |
{ |
| 16978 |
char *partkeydef = NULL; |
| 16979 |
char *ftoptions = NULL; |
| 16980 |
char *srvname = NULL; |
| 16981 |
const char *foreign = ""; |
| 16982 |
|
| 16983 |
/* |
| 16984 |
* Set reltypename, and collect any relkind-specific data that we |
| 16985 |
* didn't fetch during getTables(). |
| 16986 |
*/ |
| 16987 |
switch (tbinfo->relkind) |
| 16988 |
{ |
| 16989 |
case RELKIND_PARTITIONED_TABLE: |
| 16990 |
{ |
| 16991 |
PQExpBuffer query = createPQExpBuffer(); |
| 16992 |
PGresult *res; |
| 16993 |
|
| 16994 |
reltypename = "TABLE"; |
| 16995 |
|
| 16996 |
/* retrieve partition key definition */ |
| 16997 |
appendPQExpBuffer(query, |
| 16998 |
"SELECT pg_get_partkeydef('%u')", |
| 16999 |
tbinfo->dobj.catId.oid); |
| 17000 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 17001 |
partkeydef = pg_strdup(PQgetvalue(res, 0, 0)); |
| 17002 |
PQclear(res); |
| 17003 |
destroyPQExpBuffer(query); |
| 17004 |
break; |
| 17005 |
} |
| 17006 |
case RELKIND_FOREIGN_TABLE: |
| 17007 |
{ |
| 17008 |
PQExpBuffer query = createPQExpBuffer(); |
| 17009 |
PGresult *res; |
| 17010 |
int i_srvname; |
| 17011 |
int i_ftoptions; |
| 17012 |
|
| 17013 |
reltypename = "FOREIGN TABLE"; |
| 17014 |
|
| 17015 |
/* retrieve name of foreign server and generic options */ |
| 17016 |
appendPQExpBuffer(query, |
| 17017 |
"SELECT fs.srvname, " |
| 17018 |
"pg_catalog.array_to_string(ARRAY(" |
| 17019 |
"SELECT pg_catalog.quote_ident(option_name) || " |
| 17020 |
"' ' || pg_catalog.quote_literal(option_value) " |
| 17021 |
"FROM pg_catalog.pg_options_to_table(ftoptions) " |
| 17022 |
"ORDER BY option_name" |
| 17023 |
"), E',\n ') AS ftoptions " |
| 17024 |
"FROM pg_catalog.pg_foreign_table ft " |
| 17025 |
"JOIN pg_catalog.pg_foreign_server fs " |
| 17026 |
"ON (fs.oid = ft.ftserver) " |
| 17027 |
"WHERE ft.ftrelid = '%u'", |
| 17028 |
tbinfo->dobj.catId.oid); |
| 17029 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 17030 |
i_srvname = PQfnumber(res, "srvname"); |
| 17031 |
i_ftoptions = PQfnumber(res, "ftoptions"); |
| 17032 |
srvname = pg_strdup(PQgetvalue(res, 0, i_srvname)); |
| 17033 |
ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions)); |
| 17034 |
PQclear(res); |
| 17035 |
destroyPQExpBuffer(query); |
| 17036 |
|
| 17037 |
foreign = "FOREIGN "; |
| 17038 |
break; |
| 17039 |
} |
| 17040 |
case RELKIND_MATVIEW: |
| 17041 |
reltypename = "MATERIALIZED VIEW"; |
| 17042 |
break; |
| 17043 |
default: |
| 17044 |
reltypename = "TABLE"; |
| 17045 |
break; |
| 17046 |
} |
| 17047 |
|
| 17048 |
numParents = tbinfo->numParents; |
| 17049 |
parents = tbinfo->parents; |
| 17050 |
|
| 17051 |
appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname); |
| 17052 |
|
| 17053 |
if (dopt->binary_upgrade) |
| 17054 |
binary_upgrade_set_pg_class_oids(fout, q, |
| 17055 |
tbinfo->dobj.catId.oid); |
| 17056 |
|
| 17057 |
/* |
| 17058 |
* PostgreSQL 18 has disabled UNLOGGED for partitioned tables, so |
| 17059 |
* ignore it when dumping if it was set in this case. |
| 17060 |
*/ |
| 17061 |
appendPQExpBuffer(q, "CREATE %s%s %s", |
| 17062 |
(tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED && |
| 17063 |
tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ? |
| 17064 |
"UNLOGGED " : "", |
| 17065 |
reltypename, |
| 17066 |
qualrelname); |
| 17067 |
|
| 17068 |
/* |
| 17069 |
* Attach to type, if reloftype; except in case of a binary upgrade, |
| 17070 |
* we dump the table normally and attach it to the type afterward. |
| 17071 |
*/ |
| 17072 |
if (OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade) |
| 17073 |
appendPQExpBuffer(q, " OF %s", |
| 17074 |
getFormattedTypeName(fout, tbinfo->reloftype, |
| 17075 |
zeroIsError)); |
| 17076 |
|
| 17077 |
if (tbinfo->relkind != RELKIND_MATVIEW) |
| 17078 |
{ |
| 17079 |
/* Dump the attributes */ |
| 17080 |
actual_atts = 0; |
| 17081 |
for (j = 0; j < tbinfo->numatts; j++) |
| 17082 |
{ |
| 17083 |
/* |
| 17084 |
* Normally, dump if it's locally defined in this table, and |
| 17085 |
* not dropped. But for binary upgrade, we'll dump all the |
| 17086 |
* columns, and then fix up the dropped and nonlocal cases |
| 17087 |
* below. |
| 17088 |
*/ |
| 17089 |
if (shouldPrintColumn(dopt, tbinfo, j)) |
| 17090 |
{ |
| 17091 |
bool print_default; |
| 17092 |
bool print_notnull; |
| 17093 |
|
| 17094 |
/* |
| 17095 |
* Default value --- suppress if to be printed separately |
| 17096 |
* or not at all. |
| 17097 |
*/ |
| 17098 |
print_default = (tbinfo->attrdefs[j] != NULL && |
| 17099 |
tbinfo->attrdefs[j]->dobj.dump && |
| 17100 |
!tbinfo->attrdefs[j]->separate); |
| 17101 |
|
| 17102 |
/* |
| 17103 |
* Not Null constraint --- print it if it is locally |
| 17104 |
* defined, or if binary upgrade. (In the latter case, we |
| 17105 |
* reset conislocal below.) |
| 17106 |
*/ |
| 17107 |
print_notnull = (tbinfo->notnull_constrs[j] != NULL && |
| 17108 |
(tbinfo->notnull_islocal[j] || |
| 17109 |
dopt->binary_upgrade || |
| 17110 |
tbinfo->ispartition)); |
| 17111 |
|
| 17112 |
/* |
| 17113 |
* Skip column if fully defined by reloftype, except in |
| 17114 |
* binary upgrade |
| 17115 |
*/ |
| 17116 |
if (OidIsValid(tbinfo->reloftype) && |
| 17117 |
!print_default && !print_notnull && |
| 17118 |
!dopt->binary_upgrade) |
| 17119 |
continue; |
| 17120 |
|
| 17121 |
/* Format properly if not first attr */ |
| 17122 |
if (actual_atts == 0) |
| 17123 |
appendPQExpBufferStr(q, " ("); |
| 17124 |
else |
| 17125 |
appendPQExpBufferChar(q, ','); |
| 17126 |
appendPQExpBufferStr(q, "\n "); |
| 17127 |
actual_atts++; |
| 17128 |
|
| 17129 |
/* Attribute name */ |
| 17130 |
appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j])); |
| 17131 |
|
| 17132 |
if (tbinfo->attisdropped[j]) |
| 17133 |
{ |
| 17134 |
/* |
| 17135 |
* ALTER TABLE DROP COLUMN clears |
| 17136 |
* pg_attribute.atttypid, so we will not have gotten a |
| 17137 |
* valid type name; insert INTEGER as a stopgap. We'll |
| 17138 |
* clean things up later. |
| 17139 |
*/ |
| 17140 |
appendPQExpBufferStr(q, " INTEGER /* dummy */"); |
| 17141 |
/* and skip to the next column */ |
| 17142 |
continue; |
| 17143 |
} |
| 17144 |
|
| 17145 |
/* |
| 17146 |
* Attribute type; print it except when creating a typed |
| 17147 |
* table ('OF type_name'), but in binary-upgrade mode, |
| 17148 |
* print it in that case too. |
| 17149 |
*/ |
| 17150 |
if (dopt->binary_upgrade || !OidIsValid(tbinfo->reloftype)) |
| 17151 |
{ |
| 17152 |
appendPQExpBuffer(q, " %s", |
| 17153 |
tbinfo->atttypnames[j]); |
| 17154 |
} |
| 17155 |
|
| 17156 |
if (print_default) |
| 17157 |
{ |
| 17158 |
if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED) |
| 17159 |
appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s) STORED", |
| 17160 |
tbinfo->attrdefs[j]->adef_expr); |
| 17161 |
else if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_VIRTUAL) |
| 17162 |
appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s)", |
| 17163 |
tbinfo->attrdefs[j]->adef_expr); |
| 17164 |
else |
| 17165 |
appendPQExpBuffer(q, " DEFAULT %s", |
| 17166 |
tbinfo->attrdefs[j]->adef_expr); |
| 17167 |
} |
| 17168 |
|
| 17169 |
if (print_notnull) |
| 17170 |
{ |
| 17171 |
if (tbinfo->notnull_constrs[j][0] == '\0') |
| 17172 |
appendPQExpBufferStr(q, " NOT NULL"); |
| 17173 |
else |
| 17174 |
appendPQExpBuffer(q, " CONSTRAINT %s NOT NULL", |
| 17175 |
fmtId(tbinfo->notnull_constrs[j])); |
| 17176 |
|
| 17177 |
if (tbinfo->notnull_noinh[j]) |
| 17178 |
appendPQExpBufferStr(q, " NO INHERIT"); |
| 17179 |
} |
| 17180 |
|
| 17181 |
/* Add collation if not default for the type */ |
| 17182 |
if (OidIsValid(tbinfo->attcollation[j])) |
| 17183 |
{ |
| 17184 |
CollInfo *coll; |
| 17185 |
|
| 17186 |
coll = findCollationByOid(tbinfo->attcollation[j]); |
| 17187 |
if (coll) |
| 17188 |
appendPQExpBuffer(q, " COLLATE %s", |
| 17189 |
fmtQualifiedDumpable(coll)); |
| 17190 |
} |
| 17191 |
} |
| 17192 |
|
| 17193 |
/* |
| 17194 |
* On the other hand, if we choose not to print a column |
| 17195 |
* (likely because it is created by inheritance), but the |
| 17196 |
* column has a locally-defined not-null constraint, we need |
| 17197 |
* to dump the constraint as a standalone object. |
| 17198 |
* |
| 17199 |
* This syntax isn't SQL-conforming, but if you wanted |
| 17200 |
* standard output you wouldn't be creating non-standard |
| 17201 |
* objects to begin with. |
| 17202 |
*/ |
| 17203 |
if (!shouldPrintColumn(dopt, tbinfo, j) && |
| 17204 |
!tbinfo->attisdropped[j] && |
| 17205 |
tbinfo->notnull_constrs[j] != NULL && |
| 17206 |
tbinfo->notnull_islocal[j]) |
| 17207 |
{ |
| 17208 |
/* Format properly if not first attr */ |
| 17209 |
if (actual_atts == 0) |
| 17210 |
appendPQExpBufferStr(q, " ("); |
| 17211 |
else |
| 17212 |
appendPQExpBufferChar(q, ','); |
| 17213 |
appendPQExpBufferStr(q, "\n "); |
| 17214 |
actual_atts++; |
| 17215 |
|
| 17216 |
if (tbinfo->notnull_constrs[j][0] == '\0') |
| 17217 |
appendPQExpBuffer(q, "NOT NULL %s", |
| 17218 |
fmtId(tbinfo->attnames[j])); |
| 17219 |
else |
| 17220 |
appendPQExpBuffer(q, "CONSTRAINT %s NOT NULL %s", |
| 17221 |
tbinfo->notnull_constrs[j], |
| 17222 |
fmtId(tbinfo->attnames[j])); |
| 17223 |
|
| 17224 |
if (tbinfo->notnull_noinh[j]) |
| 17225 |
appendPQExpBufferStr(q, " NO INHERIT"); |
| 17226 |
} |
| 17227 |
} |
| 17228 |
|
| 17229 |
/* |
| 17230 |
* Add non-inherited CHECK constraints, if any. |
| 17231 |
* |
| 17232 |
* For partitions, we need to include check constraints even if |
| 17233 |
* they're not defined locally, because the ALTER TABLE ATTACH |
| 17234 |
* PARTITION that we'll emit later expects the constraint to be |
| 17235 |
* there. (No need to fix conislocal: ATTACH PARTITION does that) |
| 17236 |
*/ |
| 17237 |
for (j = 0; j < tbinfo->ncheck; j++) |
| 17238 |
{ |
| 17239 |
ConstraintInfo *constr = &(tbinfo->checkexprs[j]); |
| 17240 |
|
| 17241 |
if (constr->separate || |
| 17242 |
(!constr->conislocal && !tbinfo->ispartition)) |
| 17243 |
continue; |
| 17244 |
|
| 17245 |
if (actual_atts == 0) |
| 17246 |
appendPQExpBufferStr(q, " (\n "); |
| 17247 |
else |
| 17248 |
appendPQExpBufferStr(q, ",\n "); |
| 17249 |
|
| 17250 |
appendPQExpBuffer(q, "CONSTRAINT %s ", |
| 17251 |
fmtId(constr->dobj.name)); |
| 17252 |
appendPQExpBufferStr(q, constr->condef); |
| 17253 |
|
| 17254 |
actual_atts++; |
| 17255 |
} |
| 17256 |
|
| 17257 |
if (actual_atts) |
| 17258 |
appendPQExpBufferStr(q, "\n)"); |
| 17259 |
else if (!(OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade)) |
| 17260 |
{ |
| 17261 |
/* |
| 17262 |
* No attributes? we must have a parenthesized attribute list, |
| 17263 |
* even though empty, when not using the OF TYPE syntax. |
| 17264 |
*/ |
| 17265 |
appendPQExpBufferStr(q, " (\n)"); |
| 17266 |
} |
| 17267 |
|
| 17268 |
/* |
| 17269 |
* Emit the INHERITS clause (not for partitions), except in |
| 17270 |
* binary-upgrade mode. |
| 17271 |
*/ |
| 17272 |
if (numParents > 0 && !tbinfo->ispartition && |
| 17273 |
!dopt->binary_upgrade) |
| 17274 |
{ |
| 17275 |
appendPQExpBufferStr(q, "\nINHERITS ("); |
| 17276 |
for (k = 0; k < numParents; k++) |
| 17277 |
{ |
| 17278 |
TableInfo *parentRel = parents[k]; |
| 17279 |
|
| 17280 |
if (k > 0) |
| 17281 |
appendPQExpBufferStr(q, ", "); |
| 17282 |
appendPQExpBufferStr(q, fmtQualifiedDumpable(parentRel)); |
| 17283 |
} |
| 17284 |
appendPQExpBufferChar(q, ')'); |
| 17285 |
} |
| 17286 |
|
| 17287 |
if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE) |
| 17288 |
appendPQExpBuffer(q, "\nPARTITION BY %s", partkeydef); |
| 17289 |
|
| 17290 |
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) |
| 17291 |
appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname)); |
| 17292 |
} |
| 17293 |
|
| 17294 |
if (nonemptyReloptions(tbinfo->reloptions) || |
| 17295 |
nonemptyReloptions(tbinfo->toast_reloptions)) |
| 17296 |
{ |
| 17297 |
bool addcomma = false; |
| 17298 |
|
| 17299 |
appendPQExpBufferStr(q, "\nWITH ("); |
| 17300 |
if (nonemptyReloptions(tbinfo->reloptions)) |
| 17301 |
{ |
| 17302 |
addcomma = true; |
| 17303 |
appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout); |
| 17304 |
} |
| 17305 |
if (nonemptyReloptions(tbinfo->toast_reloptions)) |
| 17306 |
{ |
| 17307 |
if (addcomma) |
| 17308 |
appendPQExpBufferStr(q, ", "); |
| 17309 |
appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.", |
| 17310 |
fout); |
| 17311 |
} |
| 17312 |
appendPQExpBufferChar(q, ')'); |
| 17313 |
} |
| 17314 |
|
| 17315 |
/* Dump generic options if any */ |
| 17316 |
if (ftoptions && ftoptions[0]) |
| 17317 |
appendPQExpBuffer(q, "\nOPTIONS (\n %s\n)", ftoptions); |
| 17318 |
|
| 17319 |
/* |
| 17320 |
* For materialized views, create the AS clause just like a view. At |
| 17321 |
* this point, we always mark the view as not populated. |
| 17322 |
*/ |
| 17323 |
if (tbinfo->relkind == RELKIND_MATVIEW) |
| 17324 |
{ |
| 17325 |
PQExpBuffer result; |
| 17326 |
|
| 17327 |
result = createViewAsClause(fout, tbinfo); |
| 17328 |
appendPQExpBuffer(q, " AS\n%s\n WITH NO DATA;\n", |
| 17329 |
result->data); |
| 17330 |
destroyPQExpBuffer(result); |
| 17331 |
} |
| 17332 |
else |
| 17333 |
appendPQExpBufferStr(q, ";\n"); |
| 17334 |
|
| 17335 |
/* Materialized views can depend on extensions */ |
| 17336 |
if (tbinfo->relkind == RELKIND_MATVIEW) |
| 17337 |
append_depends_on_extension(fout, q, &tbinfo->dobj, |
| 17338 |
"pg_catalog.pg_class", |
| 17339 |
"MATERIALIZED VIEW", |
| 17340 |
qualrelname); |
| 17341 |
|
| 17342 |
/* |
| 17343 |
* in binary upgrade mode, update the catalog with any missing values |
| 17344 |
* that might be present. |
| 17345 |
*/ |
| 17346 |
if (dopt->binary_upgrade) |
| 17347 |
{ |
| 17348 |
for (j = 0; j < tbinfo->numatts; j++) |
| 17349 |
{ |
| 17350 |
if (tbinfo->attmissingval[j][0] != '\0') |
| 17351 |
{ |
| 17352 |
appendPQExpBufferStr(q, "\n-- set missing value.\n"); |
| 17353 |
appendPQExpBufferStr(q, |
| 17354 |
"SELECT pg_catalog.binary_upgrade_set_missing_value("); |
| 17355 |
appendStringLiteralAH(q, qualrelname, fout); |
| 17356 |
appendPQExpBufferStr(q, "::pg_catalog.regclass,"); |
| 17357 |
appendStringLiteralAH(q, tbinfo->attnames[j], fout); |
| 17358 |
appendPQExpBufferChar(q, ','); |
| 17359 |
appendStringLiteralAH(q, tbinfo->attmissingval[j], fout); |
| 17360 |
appendPQExpBufferStr(q, ");\n\n"); |
| 17361 |
} |
| 17362 |
} |
| 17363 |
} |
| 17364 |
|
| 17365 |
/* |
| 17366 |
* To create binary-compatible heap files, we have to ensure the same |
| 17367 |
* physical column order, including dropped columns, as in the |
| 17368 |
* original. Therefore, we create dropped columns above and drop them |
| 17369 |
* here, also updating their attlen/attalign values so that the |
| 17370 |
* dropped column can be skipped properly. (We do not bother with |
| 17371 |
* restoring the original attbyval setting.) Also, inheritance |
| 17372 |
* relationships are set up by doing ALTER TABLE INHERIT rather than |
| 17373 |
* using an INHERITS clause --- the latter would possibly mess up the |
| 17374 |
* column order. That also means we have to take care about setting |
| 17375 |
* attislocal correctly, plus fix up any inherited CHECK constraints. |
| 17376 |
* Analogously, we set up typed tables using ALTER TABLE / OF here. |
| 17377 |
* |
| 17378 |
* We process foreign and partitioned tables here, even though they |
| 17379 |
* lack heap storage, because they can participate in inheritance |
| 17380 |
* relationships and we want this stuff to be consistent across the |
| 17381 |
* inheritance tree. We can exclude indexes, toast tables, sequences |
| 17382 |
* and matviews, even though they have storage, because we don't |
| 17383 |
* support altering or dropping columns in them, nor can they be part |
| 17384 |
* of inheritance trees. |
| 17385 |
*/ |
| 17386 |
if (dopt->binary_upgrade && |
| 17387 |
(tbinfo->relkind == RELKIND_RELATION || |
| 17388 |
tbinfo->relkind == RELKIND_FOREIGN_TABLE || |
| 17389 |
tbinfo->relkind == RELKIND_PARTITIONED_TABLE)) |
| 17390 |
{ |
| 17391 |
bool firstitem; |
| 17392 |
bool firstitem_extra; |
| 17393 |
|
| 17394 |
/* |
| 17395 |
* Drop any dropped columns. Merge the pg_attribute manipulations |
| 17396 |
* into a single SQL command, so that we don't cause repeated |
| 17397 |
* relcache flushes on the target table. Otherwise we risk O(N^2) |
| 17398 |
* relcache bloat while dropping N columns. |
| 17399 |
*/ |
| 17400 |
resetPQExpBuffer(extra); |
| 17401 |
firstitem = true; |
| 17402 |
for (j = 0; j < tbinfo->numatts; j++) |
| 17403 |
{ |
| 17404 |
if (tbinfo->attisdropped[j]) |
| 17405 |
{ |
| 17406 |
if (firstitem) |
| 17407 |
{ |
| 17408 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped columns.\n" |
| 17409 |
"UPDATE pg_catalog.pg_attribute\n" |
| 17410 |
"SET attlen = v.dlen, " |
| 17411 |
"attalign = v.dalign, " |
| 17412 |
"attbyval = false\n" |
| 17413 |
"FROM (VALUES "); |
| 17414 |
firstitem = false; |
| 17415 |
} |
| 17416 |
else |
| 17417 |
appendPQExpBufferStr(q, ",\n "); |
| 17418 |
appendPQExpBufferChar(q, '('); |
| 17419 |
appendStringLiteralAH(q, tbinfo->attnames[j], fout); |
| 17420 |
appendPQExpBuffer(q, ", %d, '%c')", |
| 17421 |
tbinfo->attlen[j], |
| 17422 |
tbinfo->attalign[j]); |
| 17423 |
/* The ALTER ... DROP COLUMN commands must come after */ |
| 17424 |
appendPQExpBuffer(extra, "ALTER %sTABLE ONLY %s ", |
| 17425 |
foreign, qualrelname); |
| 17426 |
appendPQExpBuffer(extra, "DROP COLUMN %s;\n", |
| 17427 |
fmtId(tbinfo->attnames[j])); |
| 17428 |
} |
| 17429 |
} |
| 17430 |
if (!firstitem) |
| 17431 |
{ |
| 17432 |
appendPQExpBufferStr(q, ") v(dname, dlen, dalign)\n" |
| 17433 |
"WHERE attrelid = "); |
| 17434 |
appendStringLiteralAH(q, qualrelname, fout); |
| 17435 |
appendPQExpBufferStr(q, "::pg_catalog.regclass\n" |
| 17436 |
" AND attname = v.dname;\n"); |
| 17437 |
/* Now we can issue the actual DROP COLUMN commands */ |
| 17438 |
appendBinaryPQExpBuffer(q, extra->data, extra->len); |
| 17439 |
} |
| 17440 |
|
| 17441 |
/* |
| 17442 |
* Fix up inherited columns. As above, do the pg_attribute |
| 17443 |
* manipulations in a single SQL command. |
| 17444 |
*/ |
| 17445 |
firstitem = true; |
| 17446 |
for (j = 0; j < tbinfo->numatts; j++) |
| 17447 |
{ |
| 17448 |
if (!tbinfo->attisdropped[j] && |
| 17449 |
!tbinfo->attislocal[j]) |
| 17450 |
{ |
| 17451 |
if (firstitem) |
| 17452 |
{ |
| 17453 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited columns.\n"); |
| 17454 |
appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n" |
| 17455 |
"SET attislocal = false\n" |
| 17456 |
"WHERE attrelid = "); |
| 17457 |
appendStringLiteralAH(q, qualrelname, fout); |
| 17458 |
appendPQExpBufferStr(q, "::pg_catalog.regclass\n" |
| 17459 |
" AND attname IN ("); |
| 17460 |
firstitem = false; |
| 17461 |
} |
| 17462 |
else |
| 17463 |
appendPQExpBufferStr(q, ", "); |
| 17464 |
appendStringLiteralAH(q, tbinfo->attnames[j], fout); |
| 17465 |
} |
| 17466 |
} |
| 17467 |
if (!firstitem) |
| 17468 |
appendPQExpBufferStr(q, ");\n"); |
| 17469 |
|
| 17470 |
/* |
| 17471 |
* Fix up not-null constraints that come from inheritance. As |
| 17472 |
* above, do the pg_constraint manipulations in a single SQL |
| 17473 |
* command. (Actually, two in special cases, if we're doing an |
| 17474 |
* upgrade from < 18). |
| 17475 |
*/ |
| 17476 |
firstitem = true; |
| 17477 |
firstitem_extra = true; |
| 17478 |
resetPQExpBuffer(extra); |
| 17479 |
for (j = 0; j < tbinfo->numatts; j++) |
| 17480 |
{ |
| 17481 |
/* |
| 17482 |
* If a not-null constraint comes from inheritance, reset |
| 17483 |
* conislocal. The inhcount is fixed by ALTER TABLE INHERIT, |
| 17484 |
* below. Special hack: in versions < 18, columns with no |
| 17485 |
* local definition need their constraint to be matched by |
| 17486 |
* column number in conkeys instead of by constraint name, |
| 17487 |
* because the latter is not available. (We distinguish the |
| 17488 |
* case because the constraint name is the empty string.) |
| 17489 |
*/ |
| 17490 |
if (tbinfo->notnull_constrs[j] != NULL && |
| 17491 |
!tbinfo->notnull_islocal[j]) |
| 17492 |
{ |
| 17493 |
if (tbinfo->notnull_constrs[j][0] != '\0') |
| 17494 |
{ |
| 17495 |
if (firstitem) |
| 17496 |
{ |
| 17497 |
appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n" |
| 17498 |
"SET conislocal = false\n" |
| 17499 |
"WHERE contype = 'n' AND conrelid = "); |
| 17500 |
appendStringLiteralAH(q, qualrelname, fout); |
| 17501 |
appendPQExpBufferStr(q, "::pg_catalog.regclass AND\n" |
| 17502 |
"conname IN ("); |
| 17503 |
firstitem = false; |
| 17504 |
} |
| 17505 |
else |
| 17506 |
appendPQExpBufferStr(q, ", "); |
| 17507 |
appendStringLiteralAH(q, tbinfo->notnull_constrs[j], fout); |
| 17508 |
} |
| 17509 |
else |
| 17510 |
{ |
| 17511 |
if (firstitem_extra) |
| 17512 |
{ |
| 17513 |
appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n" |
| 17514 |
"SET conislocal = false\n" |
| 17515 |
"WHERE contype = 'n' AND conrelid = "); |
| 17516 |
appendStringLiteralAH(extra, qualrelname, fout); |
| 17517 |
appendPQExpBufferStr(extra, "::pg_catalog.regclass AND\n" |
| 17518 |
"conkey IN ("); |
| 17519 |
firstitem_extra = false; |
| 17520 |
} |
| 17521 |
else |
| 17522 |
appendPQExpBufferStr(extra, ", "); |
| 17523 |
appendPQExpBuffer(extra, "'{%d}'", j + 1); |
| 17524 |
} |
| 17525 |
} |
| 17526 |
} |
| 17527 |
if (!firstitem) |
| 17528 |
appendPQExpBufferStr(q, ");\n"); |
| 17529 |
if (!firstitem_extra) |
| 17530 |
appendPQExpBufferStr(extra, ");\n"); |
| 17531 |
|
| 17532 |
if (extra->len > 0) |
| 17533 |
appendBinaryPQExpBuffer(q, extra->data, extra->len); |
| 17534 |
|
| 17535 |
/* |
| 17536 |
* Add inherited CHECK constraints, if any. |
| 17537 |
* |
| 17538 |
* For partitions, they were already dumped, and conislocal |
| 17539 |
* doesn't need fixing. |
| 17540 |
* |
| 17541 |
* As above, issue only one direct manipulation of pg_constraint. |
| 17542 |
* Although it is tempting to merge the ALTER ADD CONSTRAINT |
| 17543 |
* commands into one as well, refrain for now due to concern about |
| 17544 |
* possible backend memory bloat if there are many such |
| 17545 |
* constraints. |
| 17546 |
*/ |
| 17547 |
resetPQExpBuffer(extra); |
| 17548 |
firstitem = true; |
| 17549 |
for (k = 0; k < tbinfo->ncheck; k++) |
| 17550 |
{ |
| 17551 |
ConstraintInfo *constr = &(tbinfo->checkexprs[k]); |
| 17552 |
|
| 17553 |
if (constr->separate || constr->conislocal || tbinfo->ispartition) |
| 17554 |
continue; |
| 17555 |
|
| 17556 |
if (firstitem) |
| 17557 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraints.\n"); |
| 17558 |
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n", |
| 17559 |
foreign, qualrelname, |
| 17560 |
fmtId(constr->dobj.name), |
| 17561 |
constr->condef); |
| 17562 |
/* Update pg_constraint after all the ALTER TABLEs */ |
| 17563 |
if (firstitem) |
| 17564 |
{ |
| 17565 |
appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n" |
| 17566 |
"SET conislocal = false\n" |
| 17567 |
"WHERE contype = 'c' AND conrelid = "); |
| 17568 |
appendStringLiteralAH(extra, qualrelname, fout); |
| 17569 |
appendPQExpBufferStr(extra, "::pg_catalog.regclass\n"); |
| 17570 |
appendPQExpBufferStr(extra, " AND conname IN ("); |
| 17571 |
firstitem = false; |
| 17572 |
} |
| 17573 |
else |
| 17574 |
appendPQExpBufferStr(extra, ", "); |
| 17575 |
appendStringLiteralAH(extra, constr->dobj.name, fout); |
| 17576 |
} |
| 17577 |
if (!firstitem) |
| 17578 |
{ |
| 17579 |
appendPQExpBufferStr(extra, ");\n"); |
| 17580 |
appendBinaryPQExpBuffer(q, extra->data, extra->len); |
| 17581 |
} |
| 17582 |
|
| 17583 |
if (numParents > 0 && !tbinfo->ispartition) |
| 17584 |
{ |
| 17585 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n"); |
| 17586 |
for (k = 0; k < numParents; k++) |
| 17587 |
{ |
| 17588 |
TableInfo *parentRel = parents[k]; |
| 17589 |
|
| 17590 |
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s INHERIT %s;\n", foreign, |
| 17591 |
qualrelname, |
| 17592 |
fmtQualifiedDumpable(parentRel)); |
| 17593 |
} |
| 17594 |
} |
| 17595 |
|
| 17596 |
if (OidIsValid(tbinfo->reloftype)) |
| 17597 |
{ |
| 17598 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n"); |
| 17599 |
appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n", |
| 17600 |
qualrelname, |
| 17601 |
getFormattedTypeName(fout, tbinfo->reloftype, |
| 17602 |
zeroIsError)); |
| 17603 |
} |
| 17604 |
} |
| 17605 |
|
| 17606 |
/* |
| 17607 |
* In binary_upgrade mode, arrange to restore the old relfrozenxid and |
| 17608 |
* relminmxid of all vacuumable relations. (While vacuum.c processes |
| 17609 |
* TOAST tables semi-independently, here we see them only as children |
| 17610 |
* of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the |
| 17611 |
* child toast table is handled below.) |
| 17612 |
*/ |
| 17613 |
if (dopt->binary_upgrade && |
| 17614 |
(tbinfo->relkind == RELKIND_RELATION || |
| 17615 |
tbinfo->relkind == RELKIND_MATVIEW)) |
| 17616 |
{ |
| 17617 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n"); |
| 17618 |
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n" |
| 17619 |
"SET relfrozenxid = '%u', relminmxid = '%u'\n" |
| 17620 |
"WHERE oid = ", |
| 17621 |
tbinfo->frozenxid, tbinfo->minmxid); |
| 17622 |
appendStringLiteralAH(q, qualrelname, fout); |
| 17623 |
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n"); |
| 17624 |
|
| 17625 |
if (tbinfo->toast_oid) |
| 17626 |
{ |
| 17627 |
/* |
| 17628 |
* The toast table will have the same OID at restore, so we |
| 17629 |
* can safely target it by OID. |
| 17630 |
*/ |
| 17631 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n"); |
| 17632 |
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n" |
| 17633 |
"SET relfrozenxid = '%u', relminmxid = '%u'\n" |
| 17634 |
"WHERE oid = '%u';\n", |
| 17635 |
tbinfo->toast_frozenxid, |
| 17636 |
tbinfo->toast_minmxid, tbinfo->toast_oid); |
| 17637 |
} |
| 17638 |
} |
| 17639 |
|
| 17640 |
/* |
| 17641 |
* In binary_upgrade mode, restore matviews' populated status by |
| 17642 |
* poking pg_class directly. This is pretty ugly, but we can't use |
| 17643 |
* REFRESH MATERIALIZED VIEW since it's possible that some underlying |
| 17644 |
* matview is not populated even though this matview is; in any case, |
| 17645 |
* we want to transfer the matview's heap storage, not run REFRESH. |
| 17646 |
*/ |
| 17647 |
if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW && |
| 17648 |
tbinfo->relispopulated) |
| 17649 |
{ |
| 17650 |
appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n"); |
| 17651 |
appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n" |
| 17652 |
"SET relispopulated = 't'\n" |
| 17653 |
"WHERE oid = "); |
| 17654 |
appendStringLiteralAH(q, qualrelname, fout); |
| 17655 |
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n"); |
| 17656 |
} |
| 17657 |
|
| 17658 |
/* |
| 17659 |
* Dump additional per-column properties that we can't handle in the |
| 17660 |
* main CREATE TABLE command. |
| 17661 |
*/ |
| 17662 |
for (j = 0; j < tbinfo->numatts; j++) |
| 17663 |
{ |
| 17664 |
/* None of this applies to dropped columns */ |
| 17665 |
if (tbinfo->attisdropped[j]) |
| 17666 |
continue; |
| 17667 |
|
| 17668 |
/* |
| 17669 |
* Dump per-column statistics information. We only issue an ALTER |
| 17670 |
* TABLE statement if the attstattarget entry for this column is |
| 17671 |
* not the default value. |
| 17672 |
*/ |
| 17673 |
if (tbinfo->attstattarget[j] >= 0) |
| 17674 |
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n", |
| 17675 |
foreign, qualrelname, |
| 17676 |
fmtId(tbinfo->attnames[j]), |
| 17677 |
tbinfo->attstattarget[j]); |
| 17678 |
|
| 17679 |
/* |
| 17680 |
* Dump per-column storage information. The statement is only |
| 17681 |
* dumped if the storage has been changed from the type's default. |
| 17682 |
*/ |
| 17683 |
if (tbinfo->attstorage[j] != tbinfo->typstorage[j]) |
| 17684 |
{ |
| 17685 |
switch (tbinfo->attstorage[j]) |
| 17686 |
{ |
| 17687 |
case TYPSTORAGE_PLAIN: |
| 17688 |
storage = "PLAIN"; |
| 17689 |
break; |
| 17690 |
case TYPSTORAGE_EXTERNAL: |
| 17691 |
storage = "EXTERNAL"; |
| 17692 |
break; |
| 17693 |
case TYPSTORAGE_EXTENDED: |
| 17694 |
storage = "EXTENDED"; |
| 17695 |
break; |
| 17696 |
case TYPSTORAGE_MAIN: |
| 17697 |
storage = "MAIN"; |
| 17698 |
break; |
| 17699 |
default: |
| 17700 |
storage = NULL; |
| 17701 |
} |
| 17702 |
|
| 17703 |
/* |
| 17704 |
* Only dump the statement if it's a storage type we recognize |
| 17705 |
*/ |
| 17706 |
if (storage != NULL) |
| 17707 |
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n", |
| 17708 |
foreign, qualrelname, |
| 17709 |
fmtId(tbinfo->attnames[j]), |
| 17710 |
storage); |
| 17711 |
} |
| 17712 |
|
| 17713 |
/* |
| 17714 |
* Dump per-column compression, if it's been set. |
| 17715 |
*/ |
| 17716 |
if (!dopt->no_toast_compression) |
| 17717 |
{ |
| 17718 |
const char *cmname; |
| 17719 |
|
| 17720 |
switch (tbinfo->attcompression[j]) |
| 17721 |
{ |
| 17722 |
case 'p': |
| 17723 |
cmname = "pglz"; |
| 17724 |
break; |
| 17725 |
case 'l': |
| 17726 |
cmname = "lz4"; |
| 17727 |
break; |
| 17728 |
default: |
| 17729 |
cmname = NULL; |
| 17730 |
break; |
| 17731 |
} |
| 17732 |
|
| 17733 |
if (cmname != NULL) |
| 17734 |
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET COMPRESSION %s;\n", |
| 17735 |
foreign, qualrelname, |
| 17736 |
fmtId(tbinfo->attnames[j]), |
| 17737 |
cmname); |
| 17738 |
} |
| 17739 |
|
| 17740 |
/* |
| 17741 |
* Dump per-column attributes. |
| 17742 |
*/ |
| 17743 |
if (tbinfo->attoptions[j][0] != '\0') |
| 17744 |
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n", |
| 17745 |
foreign, qualrelname, |
| 17746 |
fmtId(tbinfo->attnames[j]), |
| 17747 |
tbinfo->attoptions[j]); |
| 17748 |
|
| 17749 |
/* |
| 17750 |
* Dump per-column fdw options. |
| 17751 |
*/ |
| 17752 |
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE && |
| 17753 |
tbinfo->attfdwoptions[j][0] != '\0') |
| 17754 |
appendPQExpBuffer(q, |
| 17755 |
"ALTER FOREIGN TABLE ONLY %s ALTER COLUMN %s OPTIONS (\n" |
| 17756 |
" %s\n" |
| 17757 |
");\n", |
| 17758 |
qualrelname, |
| 17759 |
fmtId(tbinfo->attnames[j]), |
| 17760 |
tbinfo->attfdwoptions[j]); |
| 17761 |
} /* end loop over columns */ |
| 17762 |
|
| 17763 |
free(partkeydef); |
| 17764 |
free(ftoptions); |
| 17765 |
free(srvname); |
| 17766 |
} |
| 17767 |
|
| 17768 |
/* |
| 17769 |
* dump properties we only have ALTER TABLE syntax for |
| 17770 |
*/ |
| 17771 |
if ((tbinfo->relkind == RELKIND_RELATION || |
| 17772 |
tbinfo->relkind == RELKIND_PARTITIONED_TABLE || |
| 17773 |
tbinfo->relkind == RELKIND_MATVIEW) && |
| 17774 |
tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT) |
| 17775 |
{ |
| 17776 |
if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX) |
| 17777 |
{ |
| 17778 |
/* nothing to do, will be set when the index is dumped */ |
| 17779 |
} |
| 17780 |
else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING) |
| 17781 |
{ |
| 17782 |
appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n", |
| 17783 |
qualrelname); |
| 17784 |
} |
| 17785 |
else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL) |
| 17786 |
{ |
| 17787 |
appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n", |
| 17788 |
qualrelname); |
| 17789 |
} |
| 17790 |
} |
| 17791 |
|
| 17792 |
if (tbinfo->forcerowsec) |
| 17793 |
appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n", |
| 17794 |
qualrelname); |
| 17795 |
|
| 17796 |
if (dopt->binary_upgrade) |
| 17797 |
binary_upgrade_extension_member(q, &tbinfo->dobj, |
| 17798 |
reltypename, qrelname, |
| 17799 |
tbinfo->dobj.namespace->dobj.name); |
| 17800 |
|
| 17801 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 17802 |
{ |
| 17803 |
char *tablespace = NULL; |
| 17804 |
char *tableam = NULL; |
| 17805 |
|
| 17806 |
/* |
| 17807 |
* _selectTablespace() relies on tablespace-enabled objects in the |
| 17808 |
* default tablespace to have a tablespace of "" (empty string) versus |
| 17809 |
* non-tablespace-enabled objects to have a tablespace of NULL. |
| 17810 |
* getTables() sets tbinfo->reltablespace to "" for the default |
| 17811 |
* tablespace (not NULL). |
| 17812 |
*/ |
| 17813 |
if (RELKIND_HAS_TABLESPACE(tbinfo->relkind)) |
| 17814 |
tablespace = tbinfo->reltablespace; |
| 17815 |
|
| 17816 |
if (RELKIND_HAS_TABLE_AM(tbinfo->relkind) || |
| 17817 |
tbinfo->relkind == RELKIND_PARTITIONED_TABLE) |
| 17818 |
tableam = tbinfo->amname; |
| 17819 |
|
| 17820 |
ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, |
| 17821 |
ARCHIVE_OPTS(.tag = tbinfo->dobj.name, |
| 17822 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 17823 |
.tablespace = tablespace, |
| 17824 |
.tableam = tableam, |
| 17825 |
.relkind = tbinfo->relkind, |
| 17826 |
.owner = tbinfo->rolname, |
| 17827 |
.description = reltypename, |
| 17828 |
.section = tbinfo->postponed_def ? |
| 17829 |
SECTION_POST_DATA : SECTION_PRE_DATA, |
| 17830 |
.createStmt = q->data, |
| 17831 |
.dropStmt = delq->data)); |
| 17832 |
} |
| 17833 |
|
| 17834 |
/* Dump Table Comments */ |
| 17835 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 17836 |
dumpTableComment(fout, tbinfo, reltypename); |
| 17837 |
|
| 17838 |
/* Dump Table Security Labels */ |
| 17839 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 17840 |
dumpTableSecLabel(fout, tbinfo, reltypename); |
| 17841 |
|
| 17842 |
/* |
| 17843 |
* Dump comments for not-null constraints that aren't to be dumped |
| 17844 |
* separately (those are processed by collectComments/dumpComment). |
| 17845 |
*/ |
| 17846 |
if (!fout->dopt->no_comments && dopt->dumpSchema && |
| 17847 |
fout->remoteVersion >= 180000) |
| 17848 |
{ |
| 17849 |
PQExpBuffer comment = NULL; |
| 17850 |
PQExpBuffer tag = NULL; |
| 17851 |
|
| 17852 |
for (j = 0; j < tbinfo->numatts; j++) |
| 17853 |
{ |
| 17854 |
if (tbinfo->notnull_constrs[j] != NULL && |
| 17855 |
tbinfo->notnull_comment[j] != NULL) |
| 17856 |
{ |
| 17857 |
if (comment == NULL) |
| 17858 |
{ |
| 17859 |
comment = createPQExpBuffer(); |
| 17860 |
tag = createPQExpBuffer(); |
| 17861 |
} |
| 17862 |
else |
| 17863 |
{ |
| 17864 |
resetPQExpBuffer(comment); |
| 17865 |
resetPQExpBuffer(tag); |
| 17866 |
} |
| 17867 |
|
| 17868 |
appendPQExpBuffer(comment, "COMMENT ON CONSTRAINT %s ON %s IS ", |
| 17869 |
fmtId(tbinfo->notnull_constrs[j]), qualrelname); |
| 17870 |
appendStringLiteralAH(comment, tbinfo->notnull_comment[j], fout); |
| 17871 |
appendPQExpBufferStr(comment, ";\n"); |
| 17872 |
|
| 17873 |
appendPQExpBuffer(tag, "CONSTRAINT %s ON %s", |
| 17874 |
fmtId(tbinfo->notnull_constrs[j]), qrelname); |
| 17875 |
|
| 17876 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 17877 |
ARCHIVE_OPTS(.tag = tag->data, |
| 17878 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 17879 |
.owner = tbinfo->rolname, |
| 17880 |
.description = "COMMENT", |
| 17881 |
.section = SECTION_NONE, |
| 17882 |
.createStmt = comment->data, |
| 17883 |
.deps = &(tbinfo->dobj.dumpId), |
| 17884 |
.nDeps = 1)); |
| 17885 |
} |
| 17886 |
} |
| 17887 |
|
| 17888 |
destroyPQExpBuffer(comment); |
| 17889 |
destroyPQExpBuffer(tag); |
| 17890 |
} |
| 17891 |
|
| 17892 |
/* Dump comments on inlined table constraints */ |
| 17893 |
for (j = 0; j < tbinfo->ncheck; j++) |
| 17894 |
{ |
| 17895 |
ConstraintInfo *constr = &(tbinfo->checkexprs[j]); |
| 17896 |
|
| 17897 |
if (constr->separate || !constr->conislocal) |
| 17898 |
continue; |
| 17899 |
|
| 17900 |
if (constr->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 17901 |
dumpTableConstraintComment(fout, constr); |
| 17902 |
} |
| 17903 |
|
| 17904 |
destroyPQExpBuffer(q); |
| 17905 |
destroyPQExpBuffer(delq); |
| 17906 |
destroyPQExpBuffer(extra); |
| 17907 |
free(qrelname); |
| 17908 |
free(qualrelname); |
| 17909 |
} |
| 17910 |
|
| 17911 |
/* |
| 17912 |
* dumpTableAttach |
| 17913 |
* write to fout the commands to attach a child partition |
| 17914 |
* |
| 17915 |
* Child partitions are always made by creating them separately |
| 17916 |
* and then using ATTACH PARTITION, rather than using |
| 17917 |
* CREATE TABLE ... PARTITION OF. This is important for preserving |
| 17918 |
* any possible discrepancy in column layout, to allow assigning the |
| 17919 |
* correct tablespace if different, and so that it's possible to restore |
| 17920 |
* a partition without restoring its parent. (You'll get an error from |
| 17921 |
* the ATTACH PARTITION command, but that can be ignored, or skipped |
| 17922 |
* using "pg_restore -L" if you prefer.) The last point motivates |
| 17923 |
* treating ATTACH PARTITION as a completely separate ArchiveEntry |
| 17924 |
* rather than emitting it within the child partition's ArchiveEntry. |
| 17925 |
*/ |
| 17926 |
static void |
| 17927 |
dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo) |
| 17928 |
{ |
| 17929 |
DumpOptions *dopt = fout->dopt; |
| 17930 |
PQExpBuffer q; |
| 17931 |
PGresult *res; |
| 17932 |
char *partbound; |
| 17933 |
|
| 17934 |
/* Do nothing if not dumping schema */ |
| 17935 |
if (!dopt->dumpSchema) |
| 17936 |
return; |
| 17937 |
|
| 17938 |
q = createPQExpBuffer(); |
| 17939 |
|
| 17940 |
if (!fout->is_prepared[PREPQUERY_DUMPTABLEATTACH]) |
| 17941 |
{ |
| 17942 |
/* Set up query for partbound details */ |
| 17943 |
appendPQExpBufferStr(q, |
| 17944 |
"PREPARE dumpTableAttach(pg_catalog.oid) AS\n"); |
| 17945 |
|
| 17946 |
appendPQExpBufferStr(q, |
| 17947 |
"SELECT pg_get_expr(c.relpartbound, c.oid) " |
| 17948 |
"FROM pg_class c " |
| 17949 |
"WHERE c.oid = $1"); |
| 17950 |
|
| 17951 |
ExecuteSqlStatement(fout, q->data); |
| 17952 |
|
| 17953 |
fout->is_prepared[PREPQUERY_DUMPTABLEATTACH] = true; |
| 17954 |
} |
| 17955 |
|
| 17956 |
printfPQExpBuffer(q, |
| 17957 |
"EXECUTE dumpTableAttach('%u')", |
| 17958 |
attachinfo->partitionTbl->dobj.catId.oid); |
| 17959 |
|
| 17960 |
res = ExecuteSqlQueryForSingleRow(fout, q->data); |
| 17961 |
partbound = PQgetvalue(res, 0, 0); |
| 17962 |
|
| 17963 |
/* Perform ALTER TABLE on the parent */ |
| 17964 |
printfPQExpBuffer(q, |
| 17965 |
"ALTER TABLE ONLY %s ", |
| 17966 |
fmtQualifiedDumpable(attachinfo->parentTbl)); |
| 17967 |
appendPQExpBuffer(q, |
| 17968 |
"ATTACH PARTITION %s %s;\n", |
| 17969 |
fmtQualifiedDumpable(attachinfo->partitionTbl), |
| 17970 |
partbound); |
| 17971 |
|
| 17972 |
/* |
| 17973 |
* There is no point in creating a drop query as the drop is done by table |
| 17974 |
* drop. (If you think to change this, see also _printTocEntry().) |
| 17975 |
* Although this object doesn't really have ownership as such, set the |
| 17976 |
* owner field anyway to ensure that the command is run by the correct |
| 17977 |
* role at restore time. |
| 17978 |
*/ |
| 17979 |
ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId, |
| 17980 |
ARCHIVE_OPTS(.tag = attachinfo->dobj.name, |
| 17981 |
.namespace = attachinfo->dobj.namespace->dobj.name, |
| 17982 |
.owner = attachinfo->partitionTbl->rolname, |
| 17983 |
.description = "TABLE ATTACH", |
| 17984 |
.section = SECTION_PRE_DATA, |
| 17985 |
.createStmt = q->data)); |
| 17986 |
|
| 17987 |
PQclear(res); |
| 17988 |
destroyPQExpBuffer(q); |
| 17989 |
} |
| 17990 |
|
| 17991 |
/* |
| 17992 |
* dumpAttrDef --- dump an attribute's default-value declaration |
| 17993 |
*/ |
| 17994 |
static void |
| 17995 |
dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo) |
| 17996 |
{ |
| 17997 |
DumpOptions *dopt = fout->dopt; |
| 17998 |
TableInfo *tbinfo = adinfo->adtable; |
| 17999 |
int adnum = adinfo->adnum; |
| 18000 |
PQExpBuffer q; |
| 18001 |
PQExpBuffer delq; |
| 18002 |
char *qualrelname; |
| 18003 |
char *tag; |
| 18004 |
char *foreign; |
| 18005 |
|
| 18006 |
/* Do nothing if not dumping schema */ |
| 18007 |
if (!dopt->dumpSchema) |
| 18008 |
return; |
| 18009 |
|
| 18010 |
/* Skip if not "separate"; it was dumped in the table's definition */ |
| 18011 |
if (!adinfo->separate) |
| 18012 |
return; |
| 18013 |
|
| 18014 |
q = createPQExpBuffer(); |
| 18015 |
delq = createPQExpBuffer(); |
| 18016 |
|
| 18017 |
qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo)); |
| 18018 |
|
| 18019 |
foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : ""; |
| 18020 |
|
| 18021 |
appendPQExpBuffer(q, |
| 18022 |
"ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n", |
| 18023 |
foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]), |
| 18024 |
adinfo->adef_expr); |
| 18025 |
|
| 18026 |
appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n", |
| 18027 |
foreign, qualrelname, |
| 18028 |
fmtId(tbinfo->attnames[adnum - 1])); |
| 18029 |
|
| 18030 |
tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]); |
| 18031 |
|
| 18032 |
if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18033 |
ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId, |
| 18034 |
ARCHIVE_OPTS(.tag = tag, |
| 18035 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 18036 |
.owner = tbinfo->rolname, |
| 18037 |
.description = "DEFAULT", |
| 18038 |
.section = SECTION_PRE_DATA, |
| 18039 |
.createStmt = q->data, |
| 18040 |
.dropStmt = delq->data)); |
| 18041 |
|
| 18042 |
free(tag); |
| 18043 |
destroyPQExpBuffer(q); |
| 18044 |
destroyPQExpBuffer(delq); |
| 18045 |
free(qualrelname); |
| 18046 |
} |
| 18047 |
|
| 18048 |
/* |
| 18049 |
* getAttrName: extract the correct name for an attribute |
| 18050 |
* |
| 18051 |
* The array tblInfo->attnames[] only provides names of user attributes; |
| 18052 |
* if a system attribute number is supplied, we have to fake it. |
| 18053 |
* We also do a little bit of bounds checking for safety's sake. |
| 18054 |
*/ |
| 18055 |
static const char * |
| 18056 |
getAttrName(int attrnum, const TableInfo *tblInfo) |
| 18057 |
{ |
| 18058 |
if (attrnum > 0 && attrnum <= tblInfo->numatts) |
| 18059 |
return tblInfo->attnames[attrnum - 1]; |
| 18060 |
switch (attrnum) |
| 18061 |
{ |
| 18062 |
case SelfItemPointerAttributeNumber: |
| 18063 |
return "ctid"; |
| 18064 |
case MinTransactionIdAttributeNumber: |
| 18065 |
return "xmin"; |
| 18066 |
case MinCommandIdAttributeNumber: |
| 18067 |
return "cmin"; |
| 18068 |
case MaxTransactionIdAttributeNumber: |
| 18069 |
return "xmax"; |
| 18070 |
case MaxCommandIdAttributeNumber: |
| 18071 |
return "cmax"; |
| 18072 |
case TableOidAttributeNumber: |
| 18073 |
return "tableoid"; |
| 18074 |
} |
| 18075 |
pg_fatal("invalid column number %d for table \"%s\"", |
| 18076 |
attrnum, tblInfo->dobj.name); |
| 18077 |
return NULL; /* keep compiler quiet */ |
| 18078 |
} |
| 18079 |
|
| 18080 |
/* |
| 18081 |
* dumpIndex |
| 18082 |
* write out to fout a user-defined index |
| 18083 |
*/ |
| 18084 |
static void |
| 18085 |
dumpIndex(Archive *fout, const IndxInfo *indxinfo) |
| 18086 |
{ |
| 18087 |
DumpOptions *dopt = fout->dopt; |
| 18088 |
TableInfo *tbinfo = indxinfo->indextable; |
| 18089 |
bool is_constraint = (indxinfo->indexconstraint != 0); |
| 18090 |
PQExpBuffer q; |
| 18091 |
PQExpBuffer delq; |
| 18092 |
char *qindxname; |
| 18093 |
char *qqindxname; |
| 18094 |
|
| 18095 |
/* Do nothing if not dumping schema */ |
| 18096 |
if (!dopt->dumpSchema) |
| 18097 |
return; |
| 18098 |
|
| 18099 |
q = createPQExpBuffer(); |
| 18100 |
delq = createPQExpBuffer(); |
| 18101 |
|
| 18102 |
qindxname = pg_strdup(fmtId(indxinfo->dobj.name)); |
| 18103 |
qqindxname = pg_strdup(fmtQualifiedDumpable(indxinfo)); |
| 18104 |
|
| 18105 |
/* |
| 18106 |
* If there's an associated constraint, don't dump the index per se, but |
| 18107 |
* do dump any comment for it. (This is safe because dependency ordering |
| 18108 |
* will have ensured the constraint is emitted first.) Note that the |
| 18109 |
* emitted comment has to be shown as depending on the constraint, not the |
| 18110 |
* index, in such cases. |
| 18111 |
*/ |
| 18112 |
if (!is_constraint) |
| 18113 |
{ |
| 18114 |
char *indstatcols = indxinfo->indstatcols; |
| 18115 |
char *indstatvals = indxinfo->indstatvals; |
| 18116 |
char **indstatcolsarray = NULL; |
| 18117 |
char **indstatvalsarray = NULL; |
| 18118 |
int nstatcols = 0; |
| 18119 |
int nstatvals = 0; |
| 18120 |
|
| 18121 |
if (dopt->binary_upgrade) |
| 18122 |
binary_upgrade_set_pg_class_oids(fout, q, |
| 18123 |
indxinfo->dobj.catId.oid); |
| 18124 |
|
| 18125 |
/* Plain secondary index */ |
| 18126 |
appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef); |
| 18127 |
|
| 18128 |
/* |
| 18129 |
* Append ALTER TABLE commands as needed to set properties that we |
| 18130 |
* only have ALTER TABLE syntax for. Keep this in sync with the |
| 18131 |
* similar code in dumpConstraint! |
| 18132 |
*/ |
| 18133 |
|
| 18134 |
/* If the index is clustered, we need to record that. */ |
| 18135 |
if (indxinfo->indisclustered) |
| 18136 |
{ |
| 18137 |
appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER", |
| 18138 |
fmtQualifiedDumpable(tbinfo)); |
| 18139 |
/* index name is not qualified in this syntax */ |
| 18140 |
appendPQExpBuffer(q, " ON %s;\n", |
| 18141 |
qindxname); |
| 18142 |
} |
| 18143 |
|
| 18144 |
/* |
| 18145 |
* If the index has any statistics on some of its columns, generate |
| 18146 |
* the associated ALTER INDEX queries. |
| 18147 |
*/ |
| 18148 |
if (strlen(indstatcols) != 0 || strlen(indstatvals) != 0) |
| 18149 |
{ |
| 18150 |
int j; |
| 18151 |
|
| 18152 |
if (!parsePGArray(indstatcols, &indstatcolsarray, &nstatcols)) |
| 18153 |
pg_fatal("could not parse index statistic columns"); |
| 18154 |
if (!parsePGArray(indstatvals, &indstatvalsarray, &nstatvals)) |
| 18155 |
pg_fatal("could not parse index statistic values"); |
| 18156 |
if (nstatcols != nstatvals) |
| 18157 |
pg_fatal("mismatched number of columns and values for index statistics"); |
| 18158 |
|
| 18159 |
for (j = 0; j < nstatcols; j++) |
| 18160 |
{ |
| 18161 |
appendPQExpBuffer(q, "ALTER INDEX %s ", qqindxname); |
| 18162 |
|
| 18163 |
/* |
| 18164 |
* Note that this is a column number, so no quotes should be |
| 18165 |
* used. |
| 18166 |
*/ |
| 18167 |
appendPQExpBuffer(q, "ALTER COLUMN %s ", |
| 18168 |
indstatcolsarray[j]); |
| 18169 |
appendPQExpBuffer(q, "SET STATISTICS %s;\n", |
| 18170 |
indstatvalsarray[j]); |
| 18171 |
} |
| 18172 |
} |
| 18173 |
|
| 18174 |
/* Indexes can depend on extensions */ |
| 18175 |
append_depends_on_extension(fout, q, &indxinfo->dobj, |
| 18176 |
"pg_catalog.pg_class", |
| 18177 |
"INDEX", qqindxname); |
| 18178 |
|
| 18179 |
/* If the index defines identity, we need to record that. */ |
| 18180 |
if (indxinfo->indisreplident) |
| 18181 |
{ |
| 18182 |
appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING", |
| 18183 |
fmtQualifiedDumpable(tbinfo)); |
| 18184 |
/* index name is not qualified in this syntax */ |
| 18185 |
appendPQExpBuffer(q, " INDEX %s;\n", |
| 18186 |
qindxname); |
| 18187 |
} |
| 18188 |
|
| 18189 |
/* |
| 18190 |
* If this index is a member of a partitioned index, the backend will |
| 18191 |
* not allow us to drop it separately, so don't try. It will go away |
| 18192 |
* automatically when we drop either the index's table or the |
| 18193 |
* partitioned index. (If, in a selective restore with --clean, we |
| 18194 |
* drop neither of those, then this index will not be dropped either. |
| 18195 |
* But that's fine, and even if you think it's not, the backend won't |
| 18196 |
* let us do differently.) |
| 18197 |
*/ |
| 18198 |
if (indxinfo->parentidx == 0) |
| 18199 |
appendPQExpBuffer(delq, "DROP INDEX %s;\n", qqindxname); |
| 18200 |
|
| 18201 |
if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18202 |
ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId, |
| 18203 |
ARCHIVE_OPTS(.tag = indxinfo->dobj.name, |
| 18204 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 18205 |
.tablespace = indxinfo->tablespace, |
| 18206 |
.owner = tbinfo->rolname, |
| 18207 |
.description = "INDEX", |
| 18208 |
.section = SECTION_POST_DATA, |
| 18209 |
.createStmt = q->data, |
| 18210 |
.dropStmt = delq->data)); |
| 18211 |
|
| 18212 |
free(indstatcolsarray); |
| 18213 |
free(indstatvalsarray); |
| 18214 |
} |
| 18215 |
|
| 18216 |
/* Dump Index Comments */ |
| 18217 |
if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 18218 |
dumpComment(fout, "INDEX", qindxname, |
| 18219 |
tbinfo->dobj.namespace->dobj.name, |
| 18220 |
tbinfo->rolname, |
| 18221 |
indxinfo->dobj.catId, 0, |
| 18222 |
is_constraint ? indxinfo->indexconstraint : |
| 18223 |
indxinfo->dobj.dumpId); |
| 18224 |
|
| 18225 |
destroyPQExpBuffer(q); |
| 18226 |
destroyPQExpBuffer(delq); |
| 18227 |
free(qindxname); |
| 18228 |
free(qqindxname); |
| 18229 |
} |
| 18230 |
|
| 18231 |
/* |
| 18232 |
* dumpIndexAttach |
| 18233 |
* write out to fout a partitioned-index attachment clause |
| 18234 |
*/ |
| 18235 |
static void |
| 18236 |
dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo) |
| 18237 |
{ |
| 18238 |
/* Do nothing if not dumping schema */ |
| 18239 |
if (!fout->dopt->dumpSchema) |
| 18240 |
return; |
| 18241 |
|
| 18242 |
if (attachinfo->partitionIdx->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18243 |
{ |
| 18244 |
PQExpBuffer q = createPQExpBuffer(); |
| 18245 |
|
| 18246 |
appendPQExpBuffer(q, "ALTER INDEX %s ", |
| 18247 |
fmtQualifiedDumpable(attachinfo->parentIdx)); |
| 18248 |
appendPQExpBuffer(q, "ATTACH PARTITION %s;\n", |
| 18249 |
fmtQualifiedDumpable(attachinfo->partitionIdx)); |
| 18250 |
|
| 18251 |
/* |
| 18252 |
* There is no need for a dropStmt since the drop is done implicitly |
| 18253 |
* when we drop either the index's table or the partitioned index. |
| 18254 |
* Moreover, since there's no ALTER INDEX DETACH PARTITION command, |
| 18255 |
* there's no way to do it anyway. (If you think to change this, |
| 18256 |
* consider also what to do with --if-exists.) |
| 18257 |
* |
| 18258 |
* Although this object doesn't really have ownership as such, set the |
| 18259 |
* owner field anyway to ensure that the command is run by the correct |
| 18260 |
* role at restore time. |
| 18261 |
*/ |
| 18262 |
ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId, |
| 18263 |
ARCHIVE_OPTS(.tag = attachinfo->dobj.name, |
| 18264 |
.namespace = attachinfo->dobj.namespace->dobj.name, |
| 18265 |
.owner = attachinfo->parentIdx->indextable->rolname, |
| 18266 |
.description = "INDEX ATTACH", |
| 18267 |
.section = SECTION_POST_DATA, |
| 18268 |
.createStmt = q->data)); |
| 18269 |
|
| 18270 |
destroyPQExpBuffer(q); |
| 18271 |
} |
| 18272 |
} |
| 18273 |
|
| 18274 |
/* |
| 18275 |
* dumpStatisticsExt |
| 18276 |
* write out to fout an extended statistics object |
| 18277 |
*/ |
| 18278 |
static void |
| 18279 |
dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo) |
| 18280 |
{ |
| 18281 |
DumpOptions *dopt = fout->dopt; |
| 18282 |
PQExpBuffer q; |
| 18283 |
PQExpBuffer delq; |
| 18284 |
PQExpBuffer query; |
| 18285 |
char *qstatsextname; |
| 18286 |
PGresult *res; |
| 18287 |
char *stxdef; |
| 18288 |
|
| 18289 |
/* Do nothing if not dumping schema */ |
| 18290 |
if (!dopt->dumpSchema) |
| 18291 |
return; |
| 18292 |
|
| 18293 |
q = createPQExpBuffer(); |
| 18294 |
delq = createPQExpBuffer(); |
| 18295 |
query = createPQExpBuffer(); |
| 18296 |
|
| 18297 |
qstatsextname = pg_strdup(fmtId(statsextinfo->dobj.name)); |
| 18298 |
|
| 18299 |
appendPQExpBuffer(query, "SELECT " |
| 18300 |
"pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)", |
| 18301 |
statsextinfo->dobj.catId.oid); |
| 18302 |
|
| 18303 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 18304 |
|
| 18305 |
stxdef = PQgetvalue(res, 0, 0); |
| 18306 |
|
| 18307 |
/* Result of pg_get_statisticsobjdef is complete except for semicolon */ |
| 18308 |
appendPQExpBuffer(q, "%s;\n", stxdef); |
| 18309 |
|
| 18310 |
/* |
| 18311 |
* We only issue an ALTER STATISTICS statement if the stxstattarget entry |
| 18312 |
* for this statistics object is not the default value. |
| 18313 |
*/ |
| 18314 |
if (statsextinfo->stattarget >= 0) |
| 18315 |
{ |
| 18316 |
appendPQExpBuffer(q, "ALTER STATISTICS %s ", |
| 18317 |
fmtQualifiedDumpable(statsextinfo)); |
| 18318 |
appendPQExpBuffer(q, "SET STATISTICS %d;\n", |
| 18319 |
statsextinfo->stattarget); |
| 18320 |
} |
| 18321 |
|
| 18322 |
appendPQExpBuffer(delq, "DROP STATISTICS %s;\n", |
| 18323 |
fmtQualifiedDumpable(statsextinfo)); |
| 18324 |
|
| 18325 |
if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18326 |
ArchiveEntry(fout, statsextinfo->dobj.catId, |
| 18327 |
statsextinfo->dobj.dumpId, |
| 18328 |
ARCHIVE_OPTS(.tag = statsextinfo->dobj.name, |
| 18329 |
.namespace = statsextinfo->dobj.namespace->dobj.name, |
| 18330 |
.owner = statsextinfo->rolname, |
| 18331 |
.description = "STATISTICS", |
| 18332 |
.section = SECTION_POST_DATA, |
| 18333 |
.createStmt = q->data, |
| 18334 |
.dropStmt = delq->data)); |
| 18335 |
|
| 18336 |
/* Dump Statistics Comments */ |
| 18337 |
if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 18338 |
dumpComment(fout, "STATISTICS", qstatsextname, |
| 18339 |
statsextinfo->dobj.namespace->dobj.name, |
| 18340 |
statsextinfo->rolname, |
| 18341 |
statsextinfo->dobj.catId, 0, |
| 18342 |
statsextinfo->dobj.dumpId); |
| 18343 |
|
| 18344 |
PQclear(res); |
| 18345 |
destroyPQExpBuffer(q); |
| 18346 |
destroyPQExpBuffer(delq); |
| 18347 |
destroyPQExpBuffer(query); |
| 18348 |
free(qstatsextname); |
| 18349 |
} |
| 18350 |
|
| 18351 |
/* |
| 18352 |
* dumpConstraint |
| 18353 |
* write out to fout a user-defined constraint |
| 18354 |
*/ |
| 18355 |
static void |
| 18356 |
dumpConstraint(Archive *fout, const ConstraintInfo *coninfo) |
| 18357 |
{ |
| 18358 |
DumpOptions *dopt = fout->dopt; |
| 18359 |
TableInfo *tbinfo = coninfo->contable; |
| 18360 |
PQExpBuffer q; |
| 18361 |
PQExpBuffer delq; |
| 18362 |
char *tag = NULL; |
| 18363 |
char *foreign; |
| 18364 |
|
| 18365 |
/* Do nothing if not dumping schema */ |
| 18366 |
if (!dopt->dumpSchema) |
| 18367 |
return; |
| 18368 |
|
| 18369 |
q = createPQExpBuffer(); |
| 18370 |
delq = createPQExpBuffer(); |
| 18371 |
|
| 18372 |
foreign = tbinfo && |
| 18373 |
tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : ""; |
| 18374 |
|
| 18375 |
if (coninfo->contype == 'p' || |
| 18376 |
coninfo->contype == 'u' || |
| 18377 |
coninfo->contype == 'x') |
| 18378 |
{ |
| 18379 |
/* Index-related constraint */ |
| 18380 |
IndxInfo *indxinfo; |
| 18381 |
int k; |
| 18382 |
|
| 18383 |
indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex); |
| 18384 |
|
| 18385 |
if (indxinfo == NULL) |
| 18386 |
pg_fatal("missing index for constraint \"%s\"", |
| 18387 |
coninfo->dobj.name); |
| 18388 |
|
| 18389 |
if (dopt->binary_upgrade) |
| 18390 |
binary_upgrade_set_pg_class_oids(fout, q, |
| 18391 |
indxinfo->dobj.catId.oid); |
| 18392 |
|
| 18393 |
appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign, |
| 18394 |
fmtQualifiedDumpable(tbinfo)); |
| 18395 |
appendPQExpBuffer(q, " ADD CONSTRAINT %s ", |
| 18396 |
fmtId(coninfo->dobj.name)); |
| 18397 |
|
| 18398 |
if (coninfo->condef) |
| 18399 |
{ |
| 18400 |
/* pg_get_constraintdef should have provided everything */ |
| 18401 |
appendPQExpBuffer(q, "%s;\n", coninfo->condef); |
| 18402 |
} |
| 18403 |
else |
| 18404 |
{ |
| 18405 |
appendPQExpBufferStr(q, |
| 18406 |
coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE"); |
| 18407 |
|
| 18408 |
/* |
| 18409 |
* PRIMARY KEY constraints should not be using NULLS NOT DISTINCT |
| 18410 |
* indexes. Being able to create this was fixed, but we need to |
| 18411 |
* make the index distinct in order to be able to restore the |
| 18412 |
* dump. |
| 18413 |
*/ |
| 18414 |
if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p') |
| 18415 |
appendPQExpBufferStr(q, " NULLS NOT DISTINCT"); |
| 18416 |
appendPQExpBufferStr(q, " ("); |
| 18417 |
for (k = 0; k < indxinfo->indnkeyattrs; k++) |
| 18418 |
{ |
| 18419 |
int indkey = indxinfo->indkeys[k]; |
| 18420 |
const char *attname; |
| 18421 |
|
| 18422 |
if (indkey == InvalidAttrNumber) |
| 18423 |
break; |
| 18424 |
attname = getAttrName(indkey, tbinfo); |
| 18425 |
|
| 18426 |
appendPQExpBuffer(q, "%s%s", |
| 18427 |
(k == 0) ? "" : ", ", |
| 18428 |
fmtId(attname)); |
| 18429 |
} |
| 18430 |
if (coninfo->conperiod) |
| 18431 |
appendPQExpBufferStr(q, " WITHOUT OVERLAPS"); |
| 18432 |
|
| 18433 |
if (indxinfo->indnkeyattrs < indxinfo->indnattrs) |
| 18434 |
appendPQExpBufferStr(q, ") INCLUDE ("); |
| 18435 |
|
| 18436 |
for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++) |
| 18437 |
{ |
| 18438 |
int indkey = indxinfo->indkeys[k]; |
| 18439 |
const char *attname; |
| 18440 |
|
| 18441 |
if (indkey == InvalidAttrNumber) |
| 18442 |
break; |
| 18443 |
attname = getAttrName(indkey, tbinfo); |
| 18444 |
|
| 18445 |
appendPQExpBuffer(q, "%s%s", |
| 18446 |
(k == indxinfo->indnkeyattrs) ? "" : ", ", |
| 18447 |
fmtId(attname)); |
| 18448 |
} |
| 18449 |
|
| 18450 |
appendPQExpBufferChar(q, ')'); |
| 18451 |
|
| 18452 |
if (nonemptyReloptions(indxinfo->indreloptions)) |
| 18453 |
{ |
| 18454 |
appendPQExpBufferStr(q, " WITH ("); |
| 18455 |
appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout); |
| 18456 |
appendPQExpBufferChar(q, ')'); |
| 18457 |
} |
| 18458 |
|
| 18459 |
if (coninfo->condeferrable) |
| 18460 |
{ |
| 18461 |
appendPQExpBufferStr(q, " DEFERRABLE"); |
| 18462 |
if (coninfo->condeferred) |
| 18463 |
appendPQExpBufferStr(q, " INITIALLY DEFERRED"); |
| 18464 |
} |
| 18465 |
|
| 18466 |
appendPQExpBufferStr(q, ";\n"); |
| 18467 |
} |
| 18468 |
|
| 18469 |
/* |
| 18470 |
* Append ALTER TABLE commands as needed to set properties that we |
| 18471 |
* only have ALTER TABLE syntax for. Keep this in sync with the |
| 18472 |
* similar code in dumpIndex! |
| 18473 |
*/ |
| 18474 |
|
| 18475 |
/* If the index is clustered, we need to record that. */ |
| 18476 |
if (indxinfo->indisclustered) |
| 18477 |
{ |
| 18478 |
appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER", |
| 18479 |
fmtQualifiedDumpable(tbinfo)); |
| 18480 |
/* index name is not qualified in this syntax */ |
| 18481 |
appendPQExpBuffer(q, " ON %s;\n", |
| 18482 |
fmtId(indxinfo->dobj.name)); |
| 18483 |
} |
| 18484 |
|
| 18485 |
/* If the index defines identity, we need to record that. */ |
| 18486 |
if (indxinfo->indisreplident) |
| 18487 |
{ |
| 18488 |
appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING", |
| 18489 |
fmtQualifiedDumpable(tbinfo)); |
| 18490 |
/* index name is not qualified in this syntax */ |
| 18491 |
appendPQExpBuffer(q, " INDEX %s;\n", |
| 18492 |
fmtId(indxinfo->dobj.name)); |
| 18493 |
} |
| 18494 |
|
| 18495 |
/* Indexes can depend on extensions */ |
| 18496 |
append_depends_on_extension(fout, q, &indxinfo->dobj, |
| 18497 |
"pg_catalog.pg_class", "INDEX", |
| 18498 |
fmtQualifiedDumpable(indxinfo)); |
| 18499 |
|
| 18500 |
appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign, |
| 18501 |
fmtQualifiedDumpable(tbinfo)); |
| 18502 |
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", |
| 18503 |
fmtId(coninfo->dobj.name)); |
| 18504 |
|
| 18505 |
tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name); |
| 18506 |
|
| 18507 |
if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18508 |
ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, |
| 18509 |
ARCHIVE_OPTS(.tag = tag, |
| 18510 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 18511 |
.tablespace = indxinfo->tablespace, |
| 18512 |
.owner = tbinfo->rolname, |
| 18513 |
.description = "CONSTRAINT", |
| 18514 |
.section = SECTION_POST_DATA, |
| 18515 |
.createStmt = q->data, |
| 18516 |
.dropStmt = delq->data)); |
| 18517 |
} |
| 18518 |
else if (coninfo->contype == 'f') |
| 18519 |
{ |
| 18520 |
char *only; |
| 18521 |
|
| 18522 |
/* |
| 18523 |
* Foreign keys on partitioned tables are always declared as |
| 18524 |
* inheriting to partitions; for all other cases, emit them as |
| 18525 |
* applying ONLY directly to the named table, because that's how they |
| 18526 |
* work for regular inherited tables. |
| 18527 |
*/ |
| 18528 |
only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY "; |
| 18529 |
|
| 18530 |
/* |
| 18531 |
* XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the |
| 18532 |
* current table data is not processed |
| 18533 |
*/ |
| 18534 |
appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign, |
| 18535 |
only, fmtQualifiedDumpable(tbinfo)); |
| 18536 |
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", |
| 18537 |
fmtId(coninfo->dobj.name), |
| 18538 |
coninfo->condef); |
| 18539 |
|
| 18540 |
appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign, |
| 18541 |
only, fmtQualifiedDumpable(tbinfo)); |
| 18542 |
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", |
| 18543 |
fmtId(coninfo->dobj.name)); |
| 18544 |
|
| 18545 |
tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name); |
| 18546 |
|
| 18547 |
if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18548 |
ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, |
| 18549 |
ARCHIVE_OPTS(.tag = tag, |
| 18550 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 18551 |
.owner = tbinfo->rolname, |
| 18552 |
.description = "FK CONSTRAINT", |
| 18553 |
.section = SECTION_POST_DATA, |
| 18554 |
.createStmt = q->data, |
| 18555 |
.dropStmt = delq->data)); |
| 18556 |
} |
| 18557 |
else if ((coninfo->contype == 'c' || coninfo->contype == 'n') && tbinfo) |
| 18558 |
{ |
| 18559 |
/* CHECK or invalid not-null constraint on a table */ |
| 18560 |
|
| 18561 |
/* Ignore if not to be dumped separately, or if it was inherited */ |
| 18562 |
if (coninfo->separate && coninfo->conislocal) |
| 18563 |
{ |
| 18564 |
const char *keyword; |
| 18565 |
|
| 18566 |
if (coninfo->contype == 'c') |
| 18567 |
keyword = "CHECK CONSTRAINT"; |
| 18568 |
else |
| 18569 |
keyword = "CONSTRAINT"; |
| 18570 |
|
| 18571 |
/* not ONLY since we want it to propagate to children */ |
| 18572 |
appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign, |
| 18573 |
fmtQualifiedDumpable(tbinfo)); |
| 18574 |
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", |
| 18575 |
fmtId(coninfo->dobj.name), |
| 18576 |
coninfo->condef); |
| 18577 |
|
| 18578 |
appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign, |
| 18579 |
fmtQualifiedDumpable(tbinfo)); |
| 18580 |
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", |
| 18581 |
fmtId(coninfo->dobj.name)); |
| 18582 |
|
| 18583 |
tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name); |
| 18584 |
|
| 18585 |
if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18586 |
ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, |
| 18587 |
ARCHIVE_OPTS(.tag = tag, |
| 18588 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 18589 |
.owner = tbinfo->rolname, |
| 18590 |
.description = keyword, |
| 18591 |
.section = SECTION_POST_DATA, |
| 18592 |
.createStmt = q->data, |
| 18593 |
.dropStmt = delq->data)); |
| 18594 |
} |
| 18595 |
} |
| 18596 |
else if (tbinfo == NULL) |
| 18597 |
{ |
| 18598 |
/* CHECK, NOT NULL constraint on a domain */ |
| 18599 |
TypeInfo *tyinfo = coninfo->condomain; |
| 18600 |
|
| 18601 |
Assert(coninfo->contype == 'c' || coninfo->contype == 'n'); |
| 18602 |
|
| 18603 |
/* Ignore if not to be dumped separately */ |
| 18604 |
if (coninfo->separate) |
| 18605 |
{ |
| 18606 |
const char *keyword; |
| 18607 |
|
| 18608 |
if (coninfo->contype == 'c') |
| 18609 |
keyword = "CHECK CONSTRAINT"; |
| 18610 |
else |
| 18611 |
keyword = "CONSTRAINT"; |
| 18612 |
|
| 18613 |
appendPQExpBuffer(q, "ALTER DOMAIN %s\n", |
| 18614 |
fmtQualifiedDumpable(tyinfo)); |
| 18615 |
appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", |
| 18616 |
fmtId(coninfo->dobj.name), |
| 18617 |
coninfo->condef); |
| 18618 |
|
| 18619 |
appendPQExpBuffer(delq, "ALTER DOMAIN %s ", |
| 18620 |
fmtQualifiedDumpable(tyinfo)); |
| 18621 |
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", |
| 18622 |
fmtId(coninfo->dobj.name)); |
| 18623 |
|
| 18624 |
tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name); |
| 18625 |
|
| 18626 |
if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18627 |
ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, |
| 18628 |
ARCHIVE_OPTS(.tag = tag, |
| 18629 |
.namespace = tyinfo->dobj.namespace->dobj.name, |
| 18630 |
.owner = tyinfo->rolname, |
| 18631 |
.description = keyword, |
| 18632 |
.section = SECTION_POST_DATA, |
| 18633 |
.createStmt = q->data, |
| 18634 |
.dropStmt = delq->data)); |
| 18635 |
|
| 18636 |
if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 18637 |
{ |
| 18638 |
PQExpBuffer conprefix = createPQExpBuffer(); |
| 18639 |
char *qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); |
| 18640 |
|
| 18641 |
appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN", |
| 18642 |
fmtId(coninfo->dobj.name)); |
| 18643 |
|
| 18644 |
dumpComment(fout, conprefix->data, qtypname, |
| 18645 |
tyinfo->dobj.namespace->dobj.name, |
| 18646 |
tyinfo->rolname, |
| 18647 |
coninfo->dobj.catId, 0, coninfo->dobj.dumpId); |
| 18648 |
destroyPQExpBuffer(conprefix); |
| 18649 |
free(qtypname); |
| 18650 |
} |
| 18651 |
} |
| 18652 |
} |
| 18653 |
else |
| 18654 |
{ |
| 18655 |
pg_fatal("unrecognized constraint type: %c", |
| 18656 |
coninfo->contype); |
| 18657 |
} |
| 18658 |
|
| 18659 |
/* Dump Constraint Comments --- only works for table constraints */ |
| 18660 |
if (tbinfo && coninfo->separate && |
| 18661 |
coninfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 18662 |
dumpTableConstraintComment(fout, coninfo); |
| 18663 |
|
| 18664 |
free(tag); |
| 18665 |
destroyPQExpBuffer(q); |
| 18666 |
destroyPQExpBuffer(delq); |
| 18667 |
} |
| 18668 |
|
| 18669 |
/* |
| 18670 |
* dumpTableConstraintComment --- dump a constraint's comment if any |
| 18671 |
* |
| 18672 |
* This is split out because we need the function in two different places |
| 18673 |
* depending on whether the constraint is dumped as part of CREATE TABLE |
| 18674 |
* or as a separate ALTER command. |
| 18675 |
*/ |
| 18676 |
static void |
| 18677 |
dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo) |
| 18678 |
{ |
| 18679 |
TableInfo *tbinfo = coninfo->contable; |
| 18680 |
PQExpBuffer conprefix = createPQExpBuffer(); |
| 18681 |
char *qtabname; |
| 18682 |
|
| 18683 |
qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); |
| 18684 |
|
| 18685 |
appendPQExpBuffer(conprefix, "CONSTRAINT %s ON", |
| 18686 |
fmtId(coninfo->dobj.name)); |
| 18687 |
|
| 18688 |
if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 18689 |
dumpComment(fout, conprefix->data, qtabname, |
| 18690 |
tbinfo->dobj.namespace->dobj.name, |
| 18691 |
tbinfo->rolname, |
| 18692 |
coninfo->dobj.catId, 0, |
| 18693 |
coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId); |
| 18694 |
|
| 18695 |
destroyPQExpBuffer(conprefix); |
| 18696 |
free(qtabname); |
| 18697 |
} |
| 18698 |
|
| 18699 |
static inline SeqType |
| 18700 |
parse_sequence_type(const char *name) |
| 18701 |
{ |
| 18702 |
for (int i = 0; i < lengthof(SeqTypeNames); i++) |
| 18703 |
{ |
| 18704 |
if (strcmp(SeqTypeNames[i], name) == 0) |
| 18705 |
return (SeqType) i; |
| 18706 |
} |
| 18707 |
|
| 18708 |
pg_fatal("unrecognized sequence type: %s", name); |
| 18709 |
return (SeqType) 0; /* keep compiler quiet */ |
| 18710 |
} |
| 18711 |
|
| 18712 |
/* |
| 18713 |
* bsearch() comparator for SequenceItem |
| 18714 |
*/ |
| 18715 |
static int |
| 18716 |
SequenceItemCmp(const void *p1, const void *p2) |
| 18717 |
{ |
| 18718 |
SequenceItem v1 = *((const SequenceItem *) p1); |
| 18719 |
SequenceItem v2 = *((const SequenceItem *) p2); |
| 18720 |
|
| 18721 |
return pg_cmp_u32(v1.oid, v2.oid); |
| 18722 |
} |
| 18723 |
|
| 18724 |
/* |
| 18725 |
* collectSequences |
| 18726 |
* |
| 18727 |
* Construct a table of sequence information. This table is sorted by OID for |
| 18728 |
* speed in lookup. |
| 18729 |
*/ |
| 18730 |
static void |
| 18731 |
collectSequences(Archive *fout) |
| 18732 |
{ |
| 18733 |
PGresult *res; |
| 18734 |
const char *query; |
| 18735 |
|
| 18736 |
/* |
| 18737 |
* Before Postgres 10, sequence metadata is in the sequence itself. With |
| 18738 |
* some extra effort, we might be able to use the sorted table for those |
| 18739 |
* versions, but for now it seems unlikely to be worth it. |
| 18740 |
* |
| 18741 |
* Since version 18, we can gather the sequence data in this query with |
| 18742 |
* pg_get_sequence_data(), but we only do so for non-schema-only dumps. |
| 18743 |
*/ |
| 18744 |
if (fout->remoteVersion < 100000) |
| 18745 |
return; |
| 18746 |
else if (fout->remoteVersion < 180000 || |
| 18747 |
(!fout->dopt->dumpData && !fout->dopt->sequence_data)) |
| 18748 |
query = "SELECT seqrelid, format_type(seqtypid, NULL), " |
| 18749 |
"seqstart, seqincrement, " |
| 18750 |
"seqmax, seqmin, " |
| 18751 |
"seqcache, seqcycle, " |
| 18752 |
"NULL, 'f' " |
| 18753 |
"FROM pg_catalog.pg_sequence " |
| 18754 |
"ORDER BY seqrelid"; |
| 18755 |
else |
| 18756 |
query = "SELECT seqrelid, format_type(seqtypid, NULL), " |
| 18757 |
"seqstart, seqincrement, " |
| 18758 |
"seqmax, seqmin, " |
| 18759 |
"seqcache, seqcycle, " |
| 18760 |
"last_value, is_called " |
| 18761 |
"FROM pg_catalog.pg_sequence, " |
| 18762 |
"pg_get_sequence_data(seqrelid) " |
| 18763 |
"ORDER BY seqrelid;"; |
| 18764 |
|
| 18765 |
res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK); |
| 18766 |
|
| 18767 |
nsequences = PQntuples(res); |
| 18768 |
sequences = (SequenceItem *) pg_malloc(nsequences * sizeof(SequenceItem)); |
| 18769 |
|
| 18770 |
for (int i = 0; i < nsequences; i++) |
| 18771 |
{ |
| 18772 |
sequences[i].oid = atooid(PQgetvalue(res, i, 0)); |
| 18773 |
sequences[i].seqtype = parse_sequence_type(PQgetvalue(res, i, 1)); |
| 18774 |
sequences[i].startv = strtoi64(PQgetvalue(res, i, 2), NULL, 10); |
| 18775 |
sequences[i].incby = strtoi64(PQgetvalue(res, i, 3), NULL, 10); |
| 18776 |
sequences[i].maxv = strtoi64(PQgetvalue(res, i, 4), NULL, 10); |
| 18777 |
sequences[i].minv = strtoi64(PQgetvalue(res, i, 5), NULL, 10); |
| 18778 |
sequences[i].cache = strtoi64(PQgetvalue(res, i, 6), NULL, 10); |
| 18779 |
sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0); |
| 18780 |
sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10); |
| 18781 |
sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0); |
| 18782 |
sequences[i].null_seqtuple = (PQgetisnull(res, i, 8) || PQgetisnull(res, i, 9)); |
| 18783 |
} |
| 18784 |
|
| 18785 |
PQclear(res); |
| 18786 |
} |
| 18787 |
|
| 18788 |
/* |
| 18789 |
* dumpSequence |
| 18790 |
* write the declaration (not data) of one user-defined sequence |
| 18791 |
*/ |
| 18792 |
static void |
| 18793 |
dumpSequence(Archive *fout, const TableInfo *tbinfo) |
| 18794 |
{ |
| 18795 |
DumpOptions *dopt = fout->dopt; |
| 18796 |
SequenceItem *seq; |
| 18797 |
bool is_ascending; |
| 18798 |
int64 default_minv, |
| 18799 |
default_maxv; |
| 18800 |
PQExpBuffer query = createPQExpBuffer(); |
| 18801 |
PQExpBuffer delqry = createPQExpBuffer(); |
| 18802 |
char *qseqname; |
| 18803 |
TableInfo *owning_tab = NULL; |
| 18804 |
|
| 18805 |
qseqname = pg_strdup(fmtId(tbinfo->dobj.name)); |
| 18806 |
|
| 18807 |
/* |
| 18808 |
* For versions >= 10, the sequence information is gathered in a sorted |
| 18809 |
* table before any calls to dumpSequence(). See collectSequences() for |
| 18810 |
* more information. |
| 18811 |
*/ |
| 18812 |
if (fout->remoteVersion >= 100000) |
| 18813 |
{ |
| 18814 |
SequenceItem key = {0}; |
| 18815 |
|
| 18816 |
Assert(sequences); |
| 18817 |
|
| 18818 |
key.oid = tbinfo->dobj.catId.oid; |
| 18819 |
seq = bsearch(&key, sequences, nsequences, |
| 18820 |
sizeof(SequenceItem), SequenceItemCmp); |
| 18821 |
} |
| 18822 |
else |
| 18823 |
{ |
| 18824 |
PGresult *res; |
| 18825 |
|
| 18826 |
/* |
| 18827 |
* Before PostgreSQL 10, sequence metadata is in the sequence itself. |
| 18828 |
* |
| 18829 |
* Note: it might seem that 'bigint' potentially needs to be |
| 18830 |
* schema-qualified, but actually that's a keyword. |
| 18831 |
*/ |
| 18832 |
appendPQExpBuffer(query, |
| 18833 |
"SELECT 'bigint' AS sequence_type, " |
| 18834 |
"start_value, increment_by, max_value, min_value, " |
| 18835 |
"cache_value, is_cycled FROM %s", |
| 18836 |
fmtQualifiedDumpable(tbinfo)); |
| 18837 |
|
| 18838 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 18839 |
|
| 18840 |
if (PQntuples(res) != 1) |
| 18841 |
pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)", |
| 18842 |
"query to get data of sequence \"%s\" returned %d rows (expected 1)", |
| 18843 |
PQntuples(res)), |
| 18844 |
tbinfo->dobj.name, PQntuples(res)); |
| 18845 |
|
| 18846 |
seq = pg_malloc0(sizeof(SequenceItem)); |
| 18847 |
seq->seqtype = parse_sequence_type(PQgetvalue(res, 0, 0)); |
| 18848 |
seq->startv = strtoi64(PQgetvalue(res, 0, 1), NULL, 10); |
| 18849 |
seq->incby = strtoi64(PQgetvalue(res, 0, 2), NULL, 10); |
| 18850 |
seq->maxv = strtoi64(PQgetvalue(res, 0, 3), NULL, 10); |
| 18851 |
seq->minv = strtoi64(PQgetvalue(res, 0, 4), NULL, 10); |
| 18852 |
seq->cache = strtoi64(PQgetvalue(res, 0, 5), NULL, 10); |
| 18853 |
seq->cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0); |
| 18854 |
|
| 18855 |
PQclear(res); |
| 18856 |
} |
| 18857 |
|
| 18858 |
/* Calculate default limits for a sequence of this type */ |
| 18859 |
is_ascending = (seq->incby >= 0); |
| 18860 |
if (seq->seqtype == SEQTYPE_SMALLINT) |
| 18861 |
{ |
| 18862 |
default_minv = is_ascending ? 1 : PG_INT16_MIN; |
| 18863 |
default_maxv = is_ascending ? PG_INT16_MAX : -1; |
| 18864 |
} |
| 18865 |
else if (seq->seqtype == SEQTYPE_INTEGER) |
| 18866 |
{ |
| 18867 |
default_minv = is_ascending ? 1 : PG_INT32_MIN; |
| 18868 |
default_maxv = is_ascending ? PG_INT32_MAX : -1; |
| 18869 |
} |
| 18870 |
else if (seq->seqtype == SEQTYPE_BIGINT) |
| 18871 |
{ |
| 18872 |
default_minv = is_ascending ? 1 : PG_INT64_MIN; |
| 18873 |
default_maxv = is_ascending ? PG_INT64_MAX : -1; |
| 18874 |
} |
| 18875 |
else |
| 18876 |
{ |
| 18877 |
pg_fatal("unrecognized sequence type: %d", seq->seqtype); |
| 18878 |
default_minv = default_maxv = 0; /* keep compiler quiet */ |
| 18879 |
} |
| 18880 |
|
| 18881 |
/* |
| 18882 |
* Identity sequences are not to be dropped separately. |
| 18883 |
*/ |
| 18884 |
if (!tbinfo->is_identity_sequence) |
| 18885 |
{ |
| 18886 |
appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n", |
| 18887 |
fmtQualifiedDumpable(tbinfo)); |
| 18888 |
} |
| 18889 |
|
| 18890 |
resetPQExpBuffer(query); |
| 18891 |
|
| 18892 |
if (dopt->binary_upgrade) |
| 18893 |
{ |
| 18894 |
binary_upgrade_set_pg_class_oids(fout, query, |
| 18895 |
tbinfo->dobj.catId.oid); |
| 18896 |
|
| 18897 |
/* |
| 18898 |
* In older PG versions a sequence will have a pg_type entry, but v14 |
| 18899 |
* and up don't use that, so don't attempt to preserve the type OID. |
| 18900 |
*/ |
| 18901 |
} |
| 18902 |
|
| 18903 |
if (tbinfo->is_identity_sequence) |
| 18904 |
{ |
| 18905 |
owning_tab = findTableByOid(tbinfo->owning_tab); |
| 18906 |
|
| 18907 |
appendPQExpBuffer(query, |
| 18908 |
"ALTER TABLE %s ", |
| 18909 |
fmtQualifiedDumpable(owning_tab)); |
| 18910 |
appendPQExpBuffer(query, |
| 18911 |
"ALTER COLUMN %s ADD GENERATED ", |
| 18912 |
fmtId(owning_tab->attnames[tbinfo->owning_col - 1])); |
| 18913 |
if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS) |
| 18914 |
appendPQExpBufferStr(query, "ALWAYS"); |
| 18915 |
else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT) |
| 18916 |
appendPQExpBufferStr(query, "BY DEFAULT"); |
| 18917 |
appendPQExpBuffer(query, " AS IDENTITY (\n SEQUENCE NAME %s\n", |
| 18918 |
fmtQualifiedDumpable(tbinfo)); |
| 18919 |
|
| 18920 |
/* |
| 18921 |
* Emit persistence option only if it's different from the owning |
| 18922 |
* table's. This avoids using this new syntax unnecessarily. |
| 18923 |
*/ |
| 18924 |
if (tbinfo->relpersistence != owning_tab->relpersistence) |
| 18925 |
appendPQExpBuffer(query, " %s\n", |
| 18926 |
tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ? |
| 18927 |
"UNLOGGED" : "LOGGED"); |
| 18928 |
} |
| 18929 |
else |
| 18930 |
{ |
| 18931 |
appendPQExpBuffer(query, |
| 18932 |
"CREATE %sSEQUENCE %s\n", |
| 18933 |
tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ? |
| 18934 |
"UNLOGGED " : "", |
| 18935 |
fmtQualifiedDumpable(tbinfo)); |
| 18936 |
|
| 18937 |
if (seq->seqtype != SEQTYPE_BIGINT) |
| 18938 |
appendPQExpBuffer(query, " AS %s\n", SeqTypeNames[seq->seqtype]); |
| 18939 |
} |
| 18940 |
|
| 18941 |
appendPQExpBuffer(query, " START WITH " INT64_FORMAT "\n", seq->startv); |
| 18942 |
|
| 18943 |
appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT "\n", seq->incby); |
| 18944 |
|
| 18945 |
if (seq->minv != default_minv) |
| 18946 |
appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT "\n", seq->minv); |
| 18947 |
else |
| 18948 |
appendPQExpBufferStr(query, " NO MINVALUE\n"); |
| 18949 |
|
| 18950 |
if (seq->maxv != default_maxv) |
| 18951 |
appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT "\n", seq->maxv); |
| 18952 |
else |
| 18953 |
appendPQExpBufferStr(query, " NO MAXVALUE\n"); |
| 18954 |
|
| 18955 |
appendPQExpBuffer(query, |
| 18956 |
" CACHE " INT64_FORMAT "%s", |
| 18957 |
seq->cache, (seq->cycled ? "\n CYCLE" : "")); |
| 18958 |
|
| 18959 |
if (tbinfo->is_identity_sequence) |
| 18960 |
appendPQExpBufferStr(query, "\n);\n"); |
| 18961 |
else |
| 18962 |
appendPQExpBufferStr(query, ";\n"); |
| 18963 |
|
| 18964 |
/* binary_upgrade: no need to clear TOAST table oid */ |
| 18965 |
|
| 18966 |
if (dopt->binary_upgrade) |
| 18967 |
binary_upgrade_extension_member(query, &tbinfo->dobj, |
| 18968 |
"SEQUENCE", qseqname, |
| 18969 |
tbinfo->dobj.namespace->dobj.name); |
| 18970 |
|
| 18971 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 18972 |
ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, |
| 18973 |
ARCHIVE_OPTS(.tag = tbinfo->dobj.name, |
| 18974 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 18975 |
.owner = tbinfo->rolname, |
| 18976 |
.description = "SEQUENCE", |
| 18977 |
.section = SECTION_PRE_DATA, |
| 18978 |
.createStmt = query->data, |
| 18979 |
.dropStmt = delqry->data)); |
| 18980 |
|
| 18981 |
/* |
| 18982 |
* If the sequence is owned by a table column, emit the ALTER for it as a |
| 18983 |
* separate TOC entry immediately following the sequence's own entry. It's |
| 18984 |
* OK to do this rather than using full sorting logic, because the |
| 18985 |
* dependency that tells us it's owned will have forced the table to be |
| 18986 |
* created first. We can't just include the ALTER in the TOC entry |
| 18987 |
* because it will fail if we haven't reassigned the sequence owner to |
| 18988 |
* match the table's owner. |
| 18989 |
* |
| 18990 |
* We need not schema-qualify the table reference because both sequence |
| 18991 |
* and table must be in the same schema. |
| 18992 |
*/ |
| 18993 |
if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence) |
| 18994 |
{ |
| 18995 |
owning_tab = findTableByOid(tbinfo->owning_tab); |
| 18996 |
|
| 18997 |
if (owning_tab == NULL) |
| 18998 |
pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found", |
| 18999 |
tbinfo->owning_tab, tbinfo->dobj.catId.oid); |
| 19000 |
|
| 19001 |
if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 19002 |
{ |
| 19003 |
resetPQExpBuffer(query); |
| 19004 |
appendPQExpBuffer(query, "ALTER SEQUENCE %s", |
| 19005 |
fmtQualifiedDumpable(tbinfo)); |
| 19006 |
appendPQExpBuffer(query, " OWNED BY %s", |
| 19007 |
fmtQualifiedDumpable(owning_tab)); |
| 19008 |
appendPQExpBuffer(query, ".%s;\n", |
| 19009 |
fmtId(owning_tab->attnames[tbinfo->owning_col - 1])); |
| 19010 |
|
| 19011 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 19012 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 19013 |
ARCHIVE_OPTS(.tag = tbinfo->dobj.name, |
| 19014 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 19015 |
.owner = tbinfo->rolname, |
| 19016 |
.description = "SEQUENCE OWNED BY", |
| 19017 |
.section = SECTION_PRE_DATA, |
| 19018 |
.createStmt = query->data, |
| 19019 |
.deps = &(tbinfo->dobj.dumpId), |
| 19020 |
.nDeps = 1)); |
| 19021 |
} |
| 19022 |
} |
| 19023 |
|
| 19024 |
/* Dump Sequence Comments and Security Labels */ |
| 19025 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 19026 |
dumpComment(fout, "SEQUENCE", qseqname, |
| 19027 |
tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, |
| 19028 |
tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId); |
| 19029 |
|
| 19030 |
if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 19031 |
dumpSecLabel(fout, "SEQUENCE", qseqname, |
| 19032 |
tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, |
| 19033 |
tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId); |
| 19034 |
|
| 19035 |
if (fout->remoteVersion < 100000) |
| 19036 |
pg_free(seq); |
| 19037 |
destroyPQExpBuffer(query); |
| 19038 |
destroyPQExpBuffer(delqry); |
| 19039 |
free(qseqname); |
| 19040 |
} |
| 19041 |
|
| 19042 |
/* |
| 19043 |
* dumpSequenceData |
| 19044 |
* write the data of one user-defined sequence |
| 19045 |
*/ |
| 19046 |
static void |
| 19047 |
dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo) |
| 19048 |
{ |
| 19049 |
TableInfo *tbinfo = tdinfo->tdtable; |
| 19050 |
int64 last; |
| 19051 |
bool called; |
| 19052 |
PQExpBuffer query; |
| 19053 |
|
| 19054 |
/* needn't bother if not dumping sequence data */ |
| 19055 |
if (!fout->dopt->dumpData && !fout->dopt->sequence_data) |
| 19056 |
return; |
| 19057 |
|
| 19058 |
query = createPQExpBuffer(); |
| 19059 |
|
| 19060 |
/* |
| 19061 |
* For versions >= 18, the sequence information is gathered in the sorted |
| 19062 |
* array before any calls to dumpSequenceData(). See collectSequences() |
| 19063 |
* for more information. |
| 19064 |
* |
| 19065 |
* For older versions, we have to query the sequence relations |
| 19066 |
* individually. |
| 19067 |
*/ |
| 19068 |
if (fout->remoteVersion < 180000) |
| 19069 |
{ |
| 19070 |
PGresult *res; |
| 19071 |
|
| 19072 |
appendPQExpBuffer(query, |
| 19073 |
"SELECT last_value, is_called FROM %s", |
| 19074 |
fmtQualifiedDumpable(tbinfo)); |
| 19075 |
|
| 19076 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 19077 |
|
| 19078 |
if (PQntuples(res) != 1) |
| 19079 |
pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)", |
| 19080 |
"query to get data of sequence \"%s\" returned %d rows (expected 1)", |
| 19081 |
PQntuples(res)), |
| 19082 |
tbinfo->dobj.name, PQntuples(res)); |
| 19083 |
|
| 19084 |
last = strtoi64(PQgetvalue(res, 0, 0), NULL, 10); |
| 19085 |
called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0); |
| 19086 |
|
| 19087 |
PQclear(res); |
| 19088 |
} |
| 19089 |
else |
| 19090 |
{ |
| 19091 |
SequenceItem key = {0}; |
| 19092 |
SequenceItem *entry; |
| 19093 |
|
| 19094 |
Assert(sequences); |
| 19095 |
Assert(tbinfo->dobj.catId.oid); |
| 19096 |
|
| 19097 |
key.oid = tbinfo->dobj.catId.oid; |
| 19098 |
entry = bsearch(&key, sequences, nsequences, |
| 19099 |
sizeof(SequenceItem), SequenceItemCmp); |
| 19100 |
|
| 19101 |
if (entry->null_seqtuple) |
| 19102 |
pg_fatal("failed to get data for sequence \"%s\"; user may lack " |
| 19103 |
"SELECT privilege on the sequence or the sequence may " |
| 19104 |
"have been concurrently dropped", |
| 19105 |
tbinfo->dobj.name); |
| 19106 |
|
| 19107 |
last = entry->last_value; |
| 19108 |
called = entry->is_called; |
| 19109 |
} |
| 19110 |
|
| 19111 |
resetPQExpBuffer(query); |
| 19112 |
appendPQExpBufferStr(query, "SELECT pg_catalog.setval("); |
| 19113 |
appendStringLiteralAH(query, fmtQualifiedDumpable(tbinfo), fout); |
| 19114 |
appendPQExpBuffer(query, ", " INT64_FORMAT ", %s);\n", |
| 19115 |
last, (called ? "true" : "false")); |
| 19116 |
|
| 19117 |
if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA) |
| 19118 |
ArchiveEntry(fout, nilCatalogId, createDumpId(), |
| 19119 |
ARCHIVE_OPTS(.tag = tbinfo->dobj.name, |
| 19120 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 19121 |
.owner = tbinfo->rolname, |
| 19122 |
.description = "SEQUENCE SET", |
| 19123 |
.section = SECTION_DATA, |
| 19124 |
.createStmt = query->data, |
| 19125 |
.deps = &(tbinfo->dobj.dumpId), |
| 19126 |
.nDeps = 1)); |
| 19127 |
|
| 19128 |
destroyPQExpBuffer(query); |
| 19129 |
} |
| 19130 |
|
| 19131 |
/* |
| 19132 |
* dumpTrigger |
| 19133 |
* write the declaration of one user-defined table trigger |
| 19134 |
*/ |
| 19135 |
static void |
| 19136 |
dumpTrigger(Archive *fout, const TriggerInfo *tginfo) |
| 19137 |
{ |
| 19138 |
DumpOptions *dopt = fout->dopt; |
| 19139 |
TableInfo *tbinfo = tginfo->tgtable; |
| 19140 |
PQExpBuffer query; |
| 19141 |
PQExpBuffer delqry; |
| 19142 |
PQExpBuffer trigprefix; |
| 19143 |
PQExpBuffer trigidentity; |
| 19144 |
char *qtabname; |
| 19145 |
char *tag; |
| 19146 |
|
| 19147 |
/* Do nothing if not dumping schema */ |
| 19148 |
if (!dopt->dumpSchema) |
| 19149 |
return; |
| 19150 |
|
| 19151 |
query = createPQExpBuffer(); |
| 19152 |
delqry = createPQExpBuffer(); |
| 19153 |
trigprefix = createPQExpBuffer(); |
| 19154 |
trigidentity = createPQExpBuffer(); |
| 19155 |
|
| 19156 |
qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); |
| 19157 |
|
| 19158 |
appendPQExpBuffer(trigidentity, "%s ", fmtId(tginfo->dobj.name)); |
| 19159 |
appendPQExpBuffer(trigidentity, "ON %s", fmtQualifiedDumpable(tbinfo)); |
| 19160 |
|
| 19161 |
appendPQExpBuffer(query, "%s;\n", tginfo->tgdef); |
| 19162 |
appendPQExpBuffer(delqry, "DROP TRIGGER %s;\n", trigidentity->data); |
| 19163 |
|
| 19164 |
/* Triggers can depend on extensions */ |
| 19165 |
append_depends_on_extension(fout, query, &tginfo->dobj, |
| 19166 |
"pg_catalog.pg_trigger", "TRIGGER", |
| 19167 |
trigidentity->data); |
| 19168 |
|
| 19169 |
if (tginfo->tgispartition) |
| 19170 |
{ |
| 19171 |
Assert(tbinfo->ispartition); |
| 19172 |
|
| 19173 |
/* |
| 19174 |
* Partition triggers only appear here because their 'tgenabled' flag |
| 19175 |
* differs from its parent's. The trigger is created already, so |
| 19176 |
* remove the CREATE and replace it with an ALTER. (Clear out the |
| 19177 |
* DROP query too, so that pg_dump --create does not cause errors.) |
| 19178 |
*/ |
| 19179 |
resetPQExpBuffer(query); |
| 19180 |
resetPQExpBuffer(delqry); |
| 19181 |
appendPQExpBuffer(query, "\nALTER %sTABLE %s ", |
| 19182 |
tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "", |
| 19183 |
fmtQualifiedDumpable(tbinfo)); |
| 19184 |
switch (tginfo->tgenabled) |
| 19185 |
{ |
| 19186 |
case 'f': |
| 19187 |
case 'D': |
| 19188 |
appendPQExpBufferStr(query, "DISABLE"); |
| 19189 |
break; |
| 19190 |
case 't': |
| 19191 |
case 'O': |
| 19192 |
appendPQExpBufferStr(query, "ENABLE"); |
| 19193 |
break; |
| 19194 |
case 'R': |
| 19195 |
appendPQExpBufferStr(query, "ENABLE REPLICA"); |
| 19196 |
break; |
| 19197 |
case 'A': |
| 19198 |
appendPQExpBufferStr(query, "ENABLE ALWAYS"); |
| 19199 |
break; |
| 19200 |
} |
| 19201 |
appendPQExpBuffer(query, " TRIGGER %s;\n", |
| 19202 |
fmtId(tginfo->dobj.name)); |
| 19203 |
} |
| 19204 |
else if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O') |
| 19205 |
{ |
| 19206 |
appendPQExpBuffer(query, "\nALTER %sTABLE %s ", |
| 19207 |
tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "", |
| 19208 |
fmtQualifiedDumpable(tbinfo)); |
| 19209 |
switch (tginfo->tgenabled) |
| 19210 |
{ |
| 19211 |
case 'D': |
| 19212 |
case 'f': |
| 19213 |
appendPQExpBufferStr(query, "DISABLE"); |
| 19214 |
break; |
| 19215 |
case 'A': |
| 19216 |
appendPQExpBufferStr(query, "ENABLE ALWAYS"); |
| 19217 |
break; |
| 19218 |
case 'R': |
| 19219 |
appendPQExpBufferStr(query, "ENABLE REPLICA"); |
| 19220 |
break; |
| 19221 |
default: |
| 19222 |
appendPQExpBufferStr(query, "ENABLE"); |
| 19223 |
break; |
| 19224 |
} |
| 19225 |
appendPQExpBuffer(query, " TRIGGER %s;\n", |
| 19226 |
fmtId(tginfo->dobj.name)); |
| 19227 |
} |
| 19228 |
|
| 19229 |
appendPQExpBuffer(trigprefix, "TRIGGER %s ON", |
| 19230 |
fmtId(tginfo->dobj.name)); |
| 19231 |
|
| 19232 |
tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name); |
| 19233 |
|
| 19234 |
if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 19235 |
ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId, |
| 19236 |
ARCHIVE_OPTS(.tag = tag, |
| 19237 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 19238 |
.owner = tbinfo->rolname, |
| 19239 |
.description = "TRIGGER", |
| 19240 |
.section = SECTION_POST_DATA, |
| 19241 |
.createStmt = query->data, |
| 19242 |
.dropStmt = delqry->data)); |
| 19243 |
|
| 19244 |
if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 19245 |
dumpComment(fout, trigprefix->data, qtabname, |
| 19246 |
tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, |
| 19247 |
tginfo->dobj.catId, 0, tginfo->dobj.dumpId); |
| 19248 |
|
| 19249 |
free(tag); |
| 19250 |
destroyPQExpBuffer(query); |
| 19251 |
destroyPQExpBuffer(delqry); |
| 19252 |
destroyPQExpBuffer(trigprefix); |
| 19253 |
destroyPQExpBuffer(trigidentity); |
| 19254 |
free(qtabname); |
| 19255 |
} |
| 19256 |
|
| 19257 |
/* |
| 19258 |
* dumpEventTrigger |
| 19259 |
* write the declaration of one user-defined event trigger |
| 19260 |
*/ |
| 19261 |
static void |
| 19262 |
dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo) |
| 19263 |
{ |
| 19264 |
DumpOptions *dopt = fout->dopt; |
| 19265 |
PQExpBuffer query; |
| 19266 |
PQExpBuffer delqry; |
| 19267 |
char *qevtname; |
| 19268 |
|
| 19269 |
/* Do nothing if not dumping schema */ |
| 19270 |
if (!dopt->dumpSchema) |
| 19271 |
return; |
| 19272 |
|
| 19273 |
query = createPQExpBuffer(); |
| 19274 |
delqry = createPQExpBuffer(); |
| 19275 |
|
| 19276 |
qevtname = pg_strdup(fmtId(evtinfo->dobj.name)); |
| 19277 |
|
| 19278 |
appendPQExpBufferStr(query, "CREATE EVENT TRIGGER "); |
| 19279 |
appendPQExpBufferStr(query, qevtname); |
| 19280 |
appendPQExpBufferStr(query, " ON "); |
| 19281 |
appendPQExpBufferStr(query, fmtId(evtinfo->evtevent)); |
| 19282 |
|
| 19283 |
if (strcmp("", evtinfo->evttags) != 0) |
| 19284 |
{ |
| 19285 |
appendPQExpBufferStr(query, "\n WHEN TAG IN ("); |
| 19286 |
appendPQExpBufferStr(query, evtinfo->evttags); |
| 19287 |
appendPQExpBufferChar(query, ')'); |
| 19288 |
} |
| 19289 |
|
| 19290 |
appendPQExpBufferStr(query, "\n EXECUTE FUNCTION "); |
| 19291 |
appendPQExpBufferStr(query, evtinfo->evtfname); |
| 19292 |
appendPQExpBufferStr(query, "();\n"); |
| 19293 |
|
| 19294 |
if (evtinfo->evtenabled != 'O') |
| 19295 |
{ |
| 19296 |
appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ", |
| 19297 |
qevtname); |
| 19298 |
switch (evtinfo->evtenabled) |
| 19299 |
{ |
| 19300 |
case 'D': |
| 19301 |
appendPQExpBufferStr(query, "DISABLE"); |
| 19302 |
break; |
| 19303 |
case 'A': |
| 19304 |
appendPQExpBufferStr(query, "ENABLE ALWAYS"); |
| 19305 |
break; |
| 19306 |
case 'R': |
| 19307 |
appendPQExpBufferStr(query, "ENABLE REPLICA"); |
| 19308 |
break; |
| 19309 |
default: |
| 19310 |
appendPQExpBufferStr(query, "ENABLE"); |
| 19311 |
break; |
| 19312 |
} |
| 19313 |
appendPQExpBufferStr(query, ";\n"); |
| 19314 |
} |
| 19315 |
|
| 19316 |
appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n", |
| 19317 |
qevtname); |
| 19318 |
|
| 19319 |
if (dopt->binary_upgrade) |
| 19320 |
binary_upgrade_extension_member(query, &evtinfo->dobj, |
| 19321 |
"EVENT TRIGGER", qevtname, NULL); |
| 19322 |
|
| 19323 |
if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 19324 |
ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId, |
| 19325 |
ARCHIVE_OPTS(.tag = evtinfo->dobj.name, |
| 19326 |
.owner = evtinfo->evtowner, |
| 19327 |
.description = "EVENT TRIGGER", |
| 19328 |
.section = SECTION_POST_DATA, |
| 19329 |
.createStmt = query->data, |
| 19330 |
.dropStmt = delqry->data)); |
| 19331 |
|
| 19332 |
if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 19333 |
dumpComment(fout, "EVENT TRIGGER", qevtname, |
| 19334 |
NULL, evtinfo->evtowner, |
| 19335 |
evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId); |
| 19336 |
|
| 19337 |
if (evtinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) |
| 19338 |
dumpSecLabel(fout, "EVENT TRIGGER", qevtname, |
| 19339 |
NULL, evtinfo->evtowner, |
| 19340 |
evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId); |
| 19341 |
|
| 19342 |
destroyPQExpBuffer(query); |
| 19343 |
destroyPQExpBuffer(delqry); |
| 19344 |
free(qevtname); |
| 19345 |
} |
| 19346 |
|
| 19347 |
/* |
| 19348 |
* dumpRule |
| 19349 |
* Dump a rule |
| 19350 |
*/ |
| 19351 |
static void |
| 19352 |
dumpRule(Archive *fout, const RuleInfo *rinfo) |
| 19353 |
{ |
| 19354 |
DumpOptions *dopt = fout->dopt; |
| 19355 |
TableInfo *tbinfo = rinfo->ruletable; |
| 19356 |
bool is_view; |
| 19357 |
PQExpBuffer query; |
| 19358 |
PQExpBuffer cmd; |
| 19359 |
PQExpBuffer delcmd; |
| 19360 |
PQExpBuffer ruleprefix; |
| 19361 |
char *qtabname; |
| 19362 |
PGresult *res; |
| 19363 |
char *tag; |
| 19364 |
|
| 19365 |
/* Do nothing if not dumping schema */ |
| 19366 |
if (!dopt->dumpSchema) |
| 19367 |
return; |
| 19368 |
|
| 19369 |
/* |
| 19370 |
* If it is an ON SELECT rule that is created implicitly by CREATE VIEW, |
| 19371 |
* we do not want to dump it as a separate object. |
| 19372 |
*/ |
| 19373 |
if (!rinfo->separate) |
| 19374 |
return; |
| 19375 |
|
| 19376 |
/* |
| 19377 |
* If it's an ON SELECT rule, we want to print it as a view definition, |
| 19378 |
* instead of a rule. |
| 19379 |
*/ |
| 19380 |
is_view = (rinfo->ev_type == '1' && rinfo->is_instead); |
| 19381 |
|
| 19382 |
query = createPQExpBuffer(); |
| 19383 |
cmd = createPQExpBuffer(); |
| 19384 |
delcmd = createPQExpBuffer(); |
| 19385 |
ruleprefix = createPQExpBuffer(); |
| 19386 |
|
| 19387 |
qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); |
| 19388 |
|
| 19389 |
if (is_view) |
| 19390 |
{ |
| 19391 |
PQExpBuffer result; |
| 19392 |
|
| 19393 |
/* |
| 19394 |
* We need OR REPLACE here because we'll be replacing a dummy view. |
| 19395 |
* Otherwise this should look largely like the regular view dump code. |
| 19396 |
*/ |
| 19397 |
appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s", |
| 19398 |
fmtQualifiedDumpable(tbinfo)); |
| 19399 |
if (nonemptyReloptions(tbinfo->reloptions)) |
| 19400 |
{ |
| 19401 |
appendPQExpBufferStr(cmd, " WITH ("); |
| 19402 |
appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout); |
| 19403 |
appendPQExpBufferChar(cmd, ')'); |
| 19404 |
} |
| 19405 |
result = createViewAsClause(fout, tbinfo); |
| 19406 |
appendPQExpBuffer(cmd, " AS\n%s", result->data); |
| 19407 |
destroyPQExpBuffer(result); |
| 19408 |
if (tbinfo->checkoption != NULL) |
| 19409 |
appendPQExpBuffer(cmd, "\n WITH %s CHECK OPTION", |
| 19410 |
tbinfo->checkoption); |
| 19411 |
appendPQExpBufferStr(cmd, ";\n"); |
| 19412 |
} |
| 19413 |
else |
| 19414 |
{ |
| 19415 |
/* In the rule case, just print pg_get_ruledef's result verbatim */ |
| 19416 |
appendPQExpBuffer(query, |
| 19417 |
"SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)", |
| 19418 |
rinfo->dobj.catId.oid); |
| 19419 |
|
| 19420 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 19421 |
|
| 19422 |
if (PQntuples(res) != 1) |
| 19423 |
pg_fatal("query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned", |
| 19424 |
rinfo->dobj.name, tbinfo->dobj.name); |
| 19425 |
|
| 19426 |
printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0)); |
| 19427 |
|
| 19428 |
PQclear(res); |
| 19429 |
} |
| 19430 |
|
| 19431 |
/* |
| 19432 |
* Add the command to alter the rules replication firing semantics if it |
| 19433 |
* differs from the default. |
| 19434 |
*/ |
| 19435 |
if (rinfo->ev_enabled != 'O') |
| 19436 |
{ |
| 19437 |
appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtQualifiedDumpable(tbinfo)); |
| 19438 |
switch (rinfo->ev_enabled) |
| 19439 |
{ |
| 19440 |
case 'A': |
| 19441 |
appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n", |
| 19442 |
fmtId(rinfo->dobj.name)); |
| 19443 |
break; |
| 19444 |
case 'R': |
| 19445 |
appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n", |
| 19446 |
fmtId(rinfo->dobj.name)); |
| 19447 |
break; |
| 19448 |
case 'D': |
| 19449 |
appendPQExpBuffer(cmd, "DISABLE RULE %s;\n", |
| 19450 |
fmtId(rinfo->dobj.name)); |
| 19451 |
break; |
| 19452 |
} |
| 19453 |
} |
| 19454 |
|
| 19455 |
if (is_view) |
| 19456 |
{ |
| 19457 |
/* |
| 19458 |
* We can't DROP a view's ON SELECT rule. Instead, use CREATE OR |
| 19459 |
* REPLACE VIEW to replace the rule with something with minimal |
| 19460 |
* dependencies. |
| 19461 |
*/ |
| 19462 |
PQExpBuffer result; |
| 19463 |
|
| 19464 |
appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s", |
| 19465 |
fmtQualifiedDumpable(tbinfo)); |
| 19466 |
result = createDummyViewAsClause(fout, tbinfo); |
| 19467 |
appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data); |
| 19468 |
destroyPQExpBuffer(result); |
| 19469 |
} |
| 19470 |
else |
| 19471 |
{ |
| 19472 |
appendPQExpBuffer(delcmd, "DROP RULE %s ", |
| 19473 |
fmtId(rinfo->dobj.name)); |
| 19474 |
appendPQExpBuffer(delcmd, "ON %s;\n", |
| 19475 |
fmtQualifiedDumpable(tbinfo)); |
| 19476 |
} |
| 19477 |
|
| 19478 |
appendPQExpBuffer(ruleprefix, "RULE %s ON", |
| 19479 |
fmtId(rinfo->dobj.name)); |
| 19480 |
|
| 19481 |
tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name); |
| 19482 |
|
| 19483 |
if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) |
| 19484 |
ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId, |
| 19485 |
ARCHIVE_OPTS(.tag = tag, |
| 19486 |
.namespace = tbinfo->dobj.namespace->dobj.name, |
| 19487 |
.owner = tbinfo->rolname, |
| 19488 |
.description = "RULE", |
| 19489 |
.section = SECTION_POST_DATA, |
| 19490 |
.createStmt = cmd->data, |
| 19491 |
.dropStmt = delcmd->data)); |
| 19492 |
|
| 19493 |
/* Dump rule comments */ |
| 19494 |
if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT) |
| 19495 |
dumpComment(fout, ruleprefix->data, qtabname, |
| 19496 |
tbinfo->dobj.namespace->dobj.name, |
| 19497 |
tbinfo->rolname, |
| 19498 |
rinfo->dobj.catId, 0, rinfo->dobj.dumpId); |
| 19499 |
|
| 19500 |
free(tag); |
| 19501 |
destroyPQExpBuffer(query); |
| 19502 |
destroyPQExpBuffer(cmd); |
| 19503 |
destroyPQExpBuffer(delcmd); |
| 19504 |
destroyPQExpBuffer(ruleprefix); |
| 19505 |
free(qtabname); |
| 19506 |
} |
| 19507 |
|
| 19508 |
/* |
| 19509 |
* getExtensionMembership --- obtain extension membership data |
| 19510 |
* |
| 19511 |
* We need to identify objects that are extension members as soon as they're |
| 19512 |
* loaded, so that we can correctly determine whether they need to be dumped. |
| 19513 |
* Generally speaking, extension member objects will get marked as *not* to |
| 19514 |
* be dumped, as they will be recreated by the single CREATE EXTENSION |
| 19515 |
* command. However, in binary upgrade mode we still need to dump the members |
| 19516 |
* individually. |
| 19517 |
*/ |
| 19518 |
void |
| 19519 |
getExtensionMembership(Archive *fout, ExtensionInfo extinfo[], |
| 19520 |
int numExtensions) |
| 19521 |
{ |
| 19522 |
PQExpBuffer query; |
| 19523 |
PGresult *res; |
| 19524 |
int ntups, |
| 19525 |
i; |
| 19526 |
int i_classid, |
| 19527 |
i_objid, |
| 19528 |
i_refobjid; |
| 19529 |
ExtensionInfo *ext; |
| 19530 |
|
| 19531 |
/* Nothing to do if no extensions */ |
| 19532 |
if (numExtensions == 0) |
| 19533 |
return; |
| 19534 |
|
| 19535 |
query = createPQExpBuffer(); |
| 19536 |
|
| 19537 |
/* refclassid constraint is redundant but may speed the search */ |
| 19538 |
appendPQExpBufferStr(query, "SELECT " |
| 19539 |
"classid, objid, refobjid " |
| 19540 |
"FROM pg_depend " |
| 19541 |
"WHERE refclassid = 'pg_extension'::regclass " |
| 19542 |
"AND deptype = 'e' " |
| 19543 |
"ORDER BY 3"); |
| 19544 |
|
| 19545 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 19546 |
|
| 19547 |
ntups = PQntuples(res); |
| 19548 |
|
| 19549 |
i_classid = PQfnumber(res, "classid"); |
| 19550 |
i_objid = PQfnumber(res, "objid"); |
| 19551 |
i_refobjid = PQfnumber(res, "refobjid"); |
| 19552 |
|
| 19553 |
/* |
| 19554 |
* Since we ordered the SELECT by referenced ID, we can expect that |
| 19555 |
* multiple entries for the same extension will appear together; this |
| 19556 |
* saves on searches. |
| 19557 |
*/ |
| 19558 |
ext = NULL; |
| 19559 |
|
| 19560 |
for (i = 0; i < ntups; i++) |
| 19561 |
{ |
| 19562 |
CatalogId objId; |
| 19563 |
Oid extId; |
| 19564 |
|
| 19565 |
objId.tableoid = atooid(PQgetvalue(res, i, i_classid)); |
| 19566 |
objId.oid = atooid(PQgetvalue(res, i, i_objid)); |
| 19567 |
extId = atooid(PQgetvalue(res, i, i_refobjid)); |
| 19568 |
|
| 19569 |
if (ext == NULL || |
| 19570 |
ext->dobj.catId.oid != extId) |
| 19571 |
ext = findExtensionByOid(extId); |
| 19572 |
|
| 19573 |
if (ext == NULL) |
| 19574 |
{ |
| 19575 |
/* shouldn't happen */ |
| 19576 |
pg_log_warning("could not find referenced extension %u", extId); |
| 19577 |
continue; |
| 19578 |
} |
| 19579 |
|
| 19580 |
recordExtensionMembership(objId, ext); |
| 19581 |
} |
| 19582 |
|
| 19583 |
PQclear(res); |
| 19584 |
|
| 19585 |
destroyPQExpBuffer(query); |
| 19586 |
} |
| 19587 |
|
| 19588 |
/* |
| 19589 |
* processExtensionTables --- deal with extension configuration tables |
| 19590 |
* |
| 19591 |
* There are two parts to this process: |
| 19592 |
* |
| 19593 |
* 1. Identify and create dump records for extension configuration tables. |
| 19594 |
* |
| 19595 |
* Extensions can mark tables as "configuration", which means that the user |
| 19596 |
* is able and expected to modify those tables after the extension has been |
| 19597 |
* loaded. For these tables, we dump out only the data- the structure is |
| 19598 |
* expected to be handled at CREATE EXTENSION time, including any indexes or |
| 19599 |
* foreign keys, which brings us to- |
| 19600 |
* |
| 19601 |
* 2. Record FK dependencies between configuration tables. |
| 19602 |
* |
| 19603 |
* Due to the FKs being created at CREATE EXTENSION time and therefore before |
| 19604 |
* the data is loaded, we have to work out what the best order for reloading |
| 19605 |
* the data is, to avoid FK violations when the tables are restored. This is |
| 19606 |
* not perfect- we can't handle circular dependencies and if any exist they |
| 19607 |
* will cause an invalid dump to be produced (though at least all of the data |
| 19608 |
* is included for a user to manually restore). This is currently documented |
| 19609 |
* but perhaps we can provide a better solution in the future. |
| 19610 |
*/ |
| 19611 |
void |
| 19612 |
processExtensionTables(Archive *fout, ExtensionInfo extinfo[], |
| 19613 |
int numExtensions) |
| 19614 |
{ |
| 19615 |
DumpOptions *dopt = fout->dopt; |
| 19616 |
PQExpBuffer query; |
| 19617 |
PGresult *res; |
| 19618 |
int ntups, |
| 19619 |
i; |
| 19620 |
int i_conrelid, |
| 19621 |
i_confrelid; |
| 19622 |
|
| 19623 |
/* Nothing to do if no extensions */ |
| 19624 |
if (numExtensions == 0) |
| 19625 |
return; |
| 19626 |
|
| 19627 |
/* |
| 19628 |
* Identify extension configuration tables and create TableDataInfo |
| 19629 |
* objects for them, ensuring their data will be dumped even though the |
| 19630 |
* tables themselves won't be. |
| 19631 |
* |
| 19632 |
* Note that we create TableDataInfo objects even in schema-only mode, ie, |
| 19633 |
* user data in a configuration table is treated like schema data. This |
| 19634 |
* seems appropriate since system data in a config table would get |
| 19635 |
* reloaded by CREATE EXTENSION. If the extension is not listed in the |
| 19636 |
* list of extensions to be included, none of its data is dumped. |
| 19637 |
*/ |
| 19638 |
for (i = 0; i < numExtensions; i++) |
| 19639 |
{ |
| 19640 |
ExtensionInfo *curext = &(extinfo[i]); |
| 19641 |
char *extconfig = curext->extconfig; |
| 19642 |
char *extcondition = curext->extcondition; |
| 19643 |
char **extconfigarray = NULL; |
| 19644 |
char **extconditionarray = NULL; |
| 19645 |
int nconfigitems = 0; |
| 19646 |
int nconditionitems = 0; |
| 19647 |
|
| 19648 |
/* |
| 19649 |
* Check if this extension is listed as to include in the dump. If |
| 19650 |
* not, any table data associated with it is discarded. |
| 19651 |
*/ |
| 19652 |
if (extension_include_oids.head != NULL && |
| 19653 |
!simple_oid_list_member(&extension_include_oids, |
| 19654 |
curext->dobj.catId.oid)) |
| 19655 |
continue; |
| 19656 |
|
| 19657 |
/* |
| 19658 |
* Check if this extension is listed as to exclude in the dump. If |
| 19659 |
* yes, any table data associated with it is discarded. |
| 19660 |
*/ |
| 19661 |
if (extension_exclude_oids.head != NULL && |
| 19662 |
simple_oid_list_member(&extension_exclude_oids, |
| 19663 |
curext->dobj.catId.oid)) |
| 19664 |
continue; |
| 19665 |
|
| 19666 |
if (strlen(extconfig) != 0 || strlen(extcondition) != 0) |
| 19667 |
{ |
| 19668 |
int j; |
| 19669 |
|
| 19670 |
if (!parsePGArray(extconfig, &extconfigarray, &nconfigitems)) |
| 19671 |
pg_fatal("could not parse %s array", "extconfig"); |
| 19672 |
if (!parsePGArray(extcondition, &extconditionarray, &nconditionitems)) |
| 19673 |
pg_fatal("could not parse %s array", "extcondition"); |
| 19674 |
if (nconfigitems != nconditionitems) |
| 19675 |
pg_fatal("mismatched number of configurations and conditions for extension"); |
| 19676 |
|
| 19677 |
for (j = 0; j < nconfigitems; j++) |
| 19678 |
{ |
| 19679 |
TableInfo *configtbl; |
| 19680 |
Oid configtbloid = atooid(extconfigarray[j]); |
| 19681 |
bool dumpobj = |
| 19682 |
curext->dobj.dump & DUMP_COMPONENT_DEFINITION; |
| 19683 |
|
| 19684 |
configtbl = findTableByOid(configtbloid); |
| 19685 |
if (configtbl == NULL) |
| 19686 |
continue; |
| 19687 |
|
| 19688 |
/* |
| 19689 |
* Tables of not-to-be-dumped extensions shouldn't be dumped |
| 19690 |
* unless the table or its schema is explicitly included |
| 19691 |
*/ |
| 19692 |
if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION)) |
| 19693 |
{ |
| 19694 |
/* check table explicitly requested */ |
| 19695 |
if (table_include_oids.head != NULL && |
| 19696 |
simple_oid_list_member(&table_include_oids, |
| 19697 |
configtbloid)) |
| 19698 |
dumpobj = true; |
| 19699 |
|
| 19700 |
/* check table's schema explicitly requested */ |
| 19701 |
if (configtbl->dobj.namespace->dobj.dump & |
| 19702 |
DUMP_COMPONENT_DATA) |
| 19703 |
dumpobj = true; |
| 19704 |
} |
| 19705 |
|
| 19706 |
/* check table excluded by an exclusion switch */ |
| 19707 |
if (table_exclude_oids.head != NULL && |
| 19708 |
simple_oid_list_member(&table_exclude_oids, |
| 19709 |
configtbloid)) |
| 19710 |
dumpobj = false; |
| 19711 |
|
| 19712 |
/* check schema excluded by an exclusion switch */ |
| 19713 |
if (simple_oid_list_member(&schema_exclude_oids, |
| 19714 |
configtbl->dobj.namespace->dobj.catId.oid)) |
| 19715 |
dumpobj = false; |
| 19716 |
|
| 19717 |
if (dumpobj) |
| 19718 |
{ |
| 19719 |
makeTableDataInfo(dopt, configtbl); |
| 19720 |
if (configtbl->dataObj != NULL) |
| 19721 |
{ |
| 19722 |
if (strlen(extconditionarray[j]) > 0) |
| 19723 |
configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]); |
| 19724 |
} |
| 19725 |
} |
| 19726 |
} |
| 19727 |
} |
| 19728 |
if (extconfigarray) |
| 19729 |
free(extconfigarray); |
| 19730 |
if (extconditionarray) |
| 19731 |
free(extconditionarray); |
| 19732 |
} |
| 19733 |
|
| 19734 |
/* |
| 19735 |
* Now that all the TableDataInfo objects have been created for all the |
| 19736 |
* extensions, check their FK dependencies and register them to try and |
| 19737 |
* dump the data out in an order that they can be restored in. |
| 19738 |
* |
| 19739 |
* Note that this is not a problem for user tables as their FKs are |
| 19740 |
* recreated after the data has been loaded. |
| 19741 |
*/ |
| 19742 |
|
| 19743 |
query = createPQExpBuffer(); |
| 19744 |
|
| 19745 |
printfPQExpBuffer(query, |
| 19746 |
"SELECT conrelid, confrelid " |
| 19747 |
"FROM pg_constraint " |
| 19748 |
"JOIN pg_depend ON (objid = confrelid) " |
| 19749 |
"WHERE contype = 'f' " |
| 19750 |
"AND refclassid = 'pg_extension'::regclass " |
| 19751 |
"AND classid = 'pg_class'::regclass;"); |
| 19752 |
|
| 19753 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 19754 |
ntups = PQntuples(res); |
| 19755 |
|
| 19756 |
i_conrelid = PQfnumber(res, "conrelid"); |
| 19757 |
i_confrelid = PQfnumber(res, "confrelid"); |
| 19758 |
|
| 19759 |
/* Now get the dependencies and register them */ |
| 19760 |
for (i = 0; i < ntups; i++) |
| 19761 |
{ |
| 19762 |
Oid conrelid, |
| 19763 |
confrelid; |
| 19764 |
TableInfo *reftable, |
| 19765 |
*contable; |
| 19766 |
|
| 19767 |
conrelid = atooid(PQgetvalue(res, i, i_conrelid)); |
| 19768 |
confrelid = atooid(PQgetvalue(res, i, i_confrelid)); |
| 19769 |
contable = findTableByOid(conrelid); |
| 19770 |
reftable = findTableByOid(confrelid); |
| 19771 |
|
| 19772 |
if (reftable == NULL || |
| 19773 |
reftable->dataObj == NULL || |
| 19774 |
contable == NULL || |
| 19775 |
contable->dataObj == NULL) |
| 19776 |
continue; |
| 19777 |
|
| 19778 |
/* |
| 19779 |
* Make referencing TABLE_DATA object depend on the referenced table's |
| 19780 |
* TABLE_DATA object. |
| 19781 |
*/ |
| 19782 |
addObjectDependency(&contable->dataObj->dobj, |
| 19783 |
reftable->dataObj->dobj.dumpId); |
| 19784 |
} |
| 19785 |
PQclear(res); |
| 19786 |
destroyPQExpBuffer(query); |
| 19787 |
} |
| 19788 |
|
| 19789 |
/* |
| 19790 |
* getDependencies --- obtain available dependency data |
| 19791 |
*/ |
| 19792 |
static void |
| 19793 |
getDependencies(Archive *fout) |
| 19794 |
{ |
| 19795 |
PQExpBuffer query; |
| 19796 |
PGresult *res; |
| 19797 |
int ntups, |
| 19798 |
i; |
| 19799 |
int i_classid, |
| 19800 |
i_objid, |
| 19801 |
i_refclassid, |
| 19802 |
i_refobjid, |
| 19803 |
i_deptype; |
| 19804 |
DumpableObject *dobj, |
| 19805 |
*refdobj; |
| 19806 |
|
| 19807 |
pg_log_info("reading dependency data"); |
| 19808 |
|
| 19809 |
query = createPQExpBuffer(); |
| 19810 |
|
| 19811 |
/* |
| 19812 |
* Messy query to collect the dependency data we need. Note that we |
| 19813 |
* ignore the sub-object column, so that dependencies of or on a column |
| 19814 |
* look the same as dependencies of or on a whole table. |
| 19815 |
* |
| 19816 |
* PIN dependencies aren't interesting, and EXTENSION dependencies were |
| 19817 |
* already processed by getExtensionMembership. |
| 19818 |
*/ |
| 19819 |
appendPQExpBufferStr(query, "SELECT " |
| 19820 |
"classid, objid, refclassid, refobjid, deptype " |
| 19821 |
"FROM pg_depend " |
| 19822 |
"WHERE deptype != 'p' AND deptype != 'e'\n"); |
| 19823 |
|
| 19824 |
/* |
| 19825 |
* Since we don't treat pg_amop entries as separate DumpableObjects, we |
| 19826 |
* have to translate their dependencies into dependencies of their parent |
| 19827 |
* opfamily. Ignore internal dependencies though, as those will point to |
| 19828 |
* their parent opclass, which we needn't consider here (and if we did, |
| 19829 |
* it'd just result in circular dependencies). Also, "loose" opfamily |
| 19830 |
* entries will have dependencies on their parent opfamily, which we |
| 19831 |
* should drop since they'd likewise become useless self-dependencies. |
| 19832 |
* (But be sure to keep deps on *other* opfamilies; see amopsortfamily.) |
| 19833 |
*/ |
| 19834 |
appendPQExpBufferStr(query, "UNION ALL\n" |
| 19835 |
"SELECT 'pg_opfamily'::regclass AS classid, amopfamily AS objid, refclassid, refobjid, deptype " |
| 19836 |
"FROM pg_depend d, pg_amop o " |
| 19837 |
"WHERE deptype NOT IN ('p', 'e', 'i') AND " |
| 19838 |
"classid = 'pg_amop'::regclass AND objid = o.oid " |
| 19839 |
"AND NOT (refclassid = 'pg_opfamily'::regclass AND amopfamily = refobjid)\n"); |
| 19840 |
|
| 19841 |
/* Likewise for pg_amproc entries */ |
| 19842 |
appendPQExpBufferStr(query, "UNION ALL\n" |
| 19843 |
"SELECT 'pg_opfamily'::regclass AS classid, amprocfamily AS objid, refclassid, refobjid, deptype " |
| 19844 |
"FROM pg_depend d, pg_amproc p " |
| 19845 |
"WHERE deptype NOT IN ('p', 'e', 'i') AND " |
| 19846 |
"classid = 'pg_amproc'::regclass AND objid = p.oid " |
| 19847 |
"AND NOT (refclassid = 'pg_opfamily'::regclass AND amprocfamily = refobjid)\n"); |
| 19848 |
|
| 19849 |
/* Sort the output for efficiency below */ |
| 19850 |
appendPQExpBufferStr(query, "ORDER BY 1,2"); |
| 19851 |
|
| 19852 |
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 19853 |
|
| 19854 |
ntups = PQntuples(res); |
| 19855 |
|
| 19856 |
i_classid = PQfnumber(res, "classid"); |
| 19857 |
i_objid = PQfnumber(res, "objid"); |
| 19858 |
i_refclassid = PQfnumber(res, "refclassid"); |
| 19859 |
i_refobjid = PQfnumber(res, "refobjid"); |
| 19860 |
i_deptype = PQfnumber(res, "deptype"); |
| 19861 |
|
| 19862 |
/* |
| 19863 |
* Since we ordered the SELECT by referencing ID, we can expect that |
| 19864 |
* multiple entries for the same object will appear together; this saves |
| 19865 |
* on searches. |
| 19866 |
*/ |
| 19867 |
dobj = NULL; |
| 19868 |
|
| 19869 |
for (i = 0; i < ntups; i++) |
| 19870 |
{ |
| 19871 |
CatalogId objId; |
| 19872 |
CatalogId refobjId; |
| 19873 |
char deptype; |
| 19874 |
|
| 19875 |
objId.tableoid = atooid(PQgetvalue(res, i, i_classid)); |
| 19876 |
objId.oid = atooid(PQgetvalue(res, i, i_objid)); |
| 19877 |
refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid)); |
| 19878 |
refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid)); |
| 19879 |
deptype = *(PQgetvalue(res, i, i_deptype)); |
| 19880 |
|
| 19881 |
if (dobj == NULL || |
| 19882 |
dobj->catId.tableoid != objId.tableoid || |
| 19883 |
dobj->catId.oid != objId.oid) |
| 19884 |
dobj = findObjectByCatalogId(objId); |
| 19885 |
|
| 19886 |
/* |
| 19887 |
* Failure to find objects mentioned in pg_depend is not unexpected, |
| 19888 |
* since for example we don't collect info about TOAST tables. |
| 19889 |
*/ |
| 19890 |
if (dobj == NULL) |
| 19891 |
{ |
| 19892 |
#ifdef NOT_USED |
| 19893 |
pg_log_warning("no referencing object %u %u", |
| 19894 |
objId.tableoid, objId.oid); |
| 19895 |
#endif |
| 19896 |
continue; |
| 19897 |
} |
| 19898 |
|
| 19899 |
refdobj = findObjectByCatalogId(refobjId); |
| 19900 |
|
| 19901 |
if (refdobj == NULL) |
| 19902 |
{ |
| 19903 |
#ifdef NOT_USED |
| 19904 |
pg_log_warning("no referenced object %u %u", |
| 19905 |
refobjId.tableoid, refobjId.oid); |
| 19906 |
#endif |
| 19907 |
continue; |
| 19908 |
} |
| 19909 |
|
| 19910 |
/* |
| 19911 |
* For 'x' dependencies, mark the object for later; we still add the |
| 19912 |
* normal dependency, for possible ordering purposes. Currently |
| 19913 |
* pg_dump_sort.c knows to put extensions ahead of all object types |
| 19914 |
* that could possibly depend on them, but this is safer. |
| 19915 |
*/ |
| 19916 |
if (deptype == 'x') |
| 19917 |
dobj->depends_on_ext = true; |
| 19918 |
|
| 19919 |
/* |
| 19920 |
* Ordinarily, table rowtypes have implicit dependencies on their |
| 19921 |
* tables. However, for a composite type the implicit dependency goes |
| 19922 |
* the other way in pg_depend; which is the right thing for DROP but |
| 19923 |
* it doesn't produce the dependency ordering we need. So in that one |
| 19924 |
* case, we reverse the direction of the dependency. |
| 19925 |
*/ |
| 19926 |
if (deptype == 'i' && |
| 19927 |
dobj->objType == DO_TABLE && |
| 19928 |
refdobj->objType == DO_TYPE) |
| 19929 |
addObjectDependency(refdobj, dobj->dumpId); |
| 19930 |
else |
| 19931 |
/* normal case */ |
| 19932 |
addObjectDependency(dobj, refdobj->dumpId); |
| 19933 |
} |
| 19934 |
|
| 19935 |
PQclear(res); |
| 19936 |
|
| 19937 |
destroyPQExpBuffer(query); |
| 19938 |
} |
| 19939 |
|
| 19940 |
|
| 19941 |
/* |
| 19942 |
* createBoundaryObjects - create dummy DumpableObjects to represent |
| 19943 |
* dump section boundaries. |
| 19944 |
*/ |
| 19945 |
static DumpableObject * |
| 19946 |
createBoundaryObjects(void) |
| 19947 |
{ |
| 19948 |
DumpableObject *dobjs; |
| 19949 |
|
| 19950 |
dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject)); |
| 19951 |
|
| 19952 |
dobjs[0].objType = DO_PRE_DATA_BOUNDARY; |
| 19953 |
dobjs[0].catId = nilCatalogId; |
| 19954 |
AssignDumpId(dobjs + 0); |
| 19955 |
dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY"); |
| 19956 |
|
| 19957 |
dobjs[1].objType = DO_POST_DATA_BOUNDARY; |
| 19958 |
dobjs[1].catId = nilCatalogId; |
| 19959 |
AssignDumpId(dobjs + 1); |
| 19960 |
dobjs[1].name = pg_strdup("POST-DATA BOUNDARY"); |
| 19961 |
|
| 19962 |
return dobjs; |
| 19963 |
} |
| 19964 |
|
| 19965 |
/* |
| 19966 |
* addBoundaryDependencies - add dependencies as needed to enforce the dump |
| 19967 |
* section boundaries. |
| 19968 |
*/ |
| 19969 |
static void |
| 19970 |
addBoundaryDependencies(DumpableObject **dobjs, int numObjs, |
| 19971 |
DumpableObject *boundaryObjs) |
| 19972 |
{ |
| 19973 |
DumpableObject *preDataBound = boundaryObjs + 0; |
| 19974 |
DumpableObject *postDataBound = boundaryObjs + 1; |
| 19975 |
int i; |
| 19976 |
|
| 19977 |
for (i = 0; i < numObjs; i++) |
| 19978 |
{ |
| 19979 |
DumpableObject *dobj = dobjs[i]; |
| 19980 |
|
| 19981 |
/* |
| 19982 |
* The classification of object types here must match the SECTION_xxx |
| 19983 |
* values assigned during subsequent ArchiveEntry calls! |
| 19984 |
*/ |
| 19985 |
switch (dobj->objType) |
| 19986 |
{ |
| 19987 |
case DO_NAMESPACE: |
| 19988 |
case DO_EXTENSION: |
| 19989 |
case DO_TYPE: |
| 19990 |
case DO_SHELL_TYPE: |
| 19991 |
case DO_FUNC: |
| 19992 |
case DO_AGG: |
| 19993 |
case DO_OPERATOR: |
| 19994 |
case DO_ACCESS_METHOD: |
| 19995 |
case DO_OPCLASS: |
| 19996 |
case DO_OPFAMILY: |
| 19997 |
case DO_COLLATION: |
| 19998 |
case DO_CONVERSION: |
| 19999 |
case DO_TABLE: |
| 20000 |
case DO_TABLE_ATTACH: |
| 20001 |
case DO_ATTRDEF: |
| 20002 |
case DO_PROCLANG: |
| 20003 |
case DO_CAST: |
| 20004 |
case DO_DUMMY_TYPE: |
| 20005 |
case DO_TSPARSER: |
| 20006 |
case DO_TSDICT: |
| 20007 |
case DO_TSTEMPLATE: |
| 20008 |
case DO_TSCONFIG: |
| 20009 |
case DO_FDW: |
| 20010 |
case DO_FOREIGN_SERVER: |
| 20011 |
case DO_TRANSFORM: |
| 20012 |
/* Pre-data objects: must come before the pre-data boundary */ |
| 20013 |
addObjectDependency(preDataBound, dobj->dumpId); |
| 20014 |
break; |
| 20015 |
case DO_TABLE_DATA: |
| 20016 |
case DO_SEQUENCE_SET: |
| 20017 |
case DO_LARGE_OBJECT: |
| 20018 |
case DO_LARGE_OBJECT_DATA: |
| 20019 |
/* Data objects: must come between the boundaries */ |
| 20020 |
addObjectDependency(dobj, preDataBound->dumpId); |
| 20021 |
addObjectDependency(postDataBound, dobj->dumpId); |
| 20022 |
break; |
| 20023 |
case DO_INDEX: |
| 20024 |
case DO_INDEX_ATTACH: |
| 20025 |
case DO_STATSEXT: |
| 20026 |
case DO_REFRESH_MATVIEW: |
| 20027 |
case DO_TRIGGER: |
| 20028 |
case DO_EVENT_TRIGGER: |
| 20029 |
case DO_DEFAULT_ACL: |
| 20030 |
case DO_POLICY: |
| 20031 |
case DO_PUBLICATION: |
| 20032 |
case DO_PUBLICATION_REL: |
| 20033 |
case DO_PUBLICATION_TABLE_IN_SCHEMA: |
| 20034 |
case DO_SUBSCRIPTION: |
| 20035 |
case DO_SUBSCRIPTION_REL: |
| 20036 |
/* Post-data objects: must come after the post-data boundary */ |
| 20037 |
addObjectDependency(dobj, postDataBound->dumpId); |
| 20038 |
break; |
| 20039 |
case DO_RULE: |
| 20040 |
/* Rules are post-data, but only if dumped separately */ |
| 20041 |
if (((RuleInfo *) dobj)->separate) |
| 20042 |
addObjectDependency(dobj, postDataBound->dumpId); |
| 20043 |
break; |
| 20044 |
case DO_CONSTRAINT: |
| 20045 |
case DO_FK_CONSTRAINT: |
| 20046 |
/* Constraints are post-data, but only if dumped separately */ |
| 20047 |
if (((ConstraintInfo *) dobj)->separate) |
| 20048 |
addObjectDependency(dobj, postDataBound->dumpId); |
| 20049 |
break; |
| 20050 |
case DO_PRE_DATA_BOUNDARY: |
| 20051 |
/* nothing to do */ |
| 20052 |
break; |
| 20053 |
case DO_POST_DATA_BOUNDARY: |
| 20054 |
/* must come after the pre-data boundary */ |
| 20055 |
addObjectDependency(dobj, preDataBound->dumpId); |
| 20056 |
break; |
| 20057 |
case DO_REL_STATS: |
| 20058 |
/* stats section varies by parent object type, DATA or POST */ |
| 20059 |
if (((RelStatsInfo *) dobj)->section == SECTION_DATA) |
| 20060 |
{ |
| 20061 |
addObjectDependency(dobj, preDataBound->dumpId); |
| 20062 |
addObjectDependency(postDataBound, dobj->dumpId); |
| 20063 |
} |
| 20064 |
else |
| 20065 |
addObjectDependency(dobj, postDataBound->dumpId); |
| 20066 |
break; |
| 20067 |
} |
| 20068 |
} |
| 20069 |
} |
| 20070 |
|
| 20071 |
|
| 20072 |
/* |
| 20073 |
* BuildArchiveDependencies - create dependency data for archive TOC entries |
| 20074 |
* |
| 20075 |
* The raw dependency data obtained by getDependencies() is not terribly |
| 20076 |
* useful in an archive dump, because in many cases there are dependency |
| 20077 |
* chains linking through objects that don't appear explicitly in the dump. |
| 20078 |
* For example, a view will depend on its _RETURN rule while the _RETURN rule |
| 20079 |
* will depend on other objects --- but the rule will not appear as a separate |
| 20080 |
* object in the dump. We need to adjust the view's dependencies to include |
| 20081 |
* whatever the rule depends on that is included in the dump. |
| 20082 |
* |
| 20083 |
* Just to make things more complicated, there are also "special" dependencies |
| 20084 |
* such as the dependency of a TABLE DATA item on its TABLE, which we must |
| 20085 |
* not rearrange because pg_restore knows that TABLE DATA only depends on |
| 20086 |
* its table. In these cases we must leave the dependencies strictly as-is |
| 20087 |
* even if they refer to not-to-be-dumped objects. |
| 20088 |
* |
| 20089 |
* To handle this, the convention is that "special" dependencies are created |
| 20090 |
* during ArchiveEntry calls, and an archive TOC item that has any such |
| 20091 |
* entries will not be touched here. Otherwise, we recursively search the |
| 20092 |
* DumpableObject data structures to build the correct dependencies for each |
| 20093 |
* archive TOC item. |
| 20094 |
*/ |
| 20095 |
static void |
| 20096 |
BuildArchiveDependencies(Archive *fout) |
| 20097 |
{ |
| 20098 |
ArchiveHandle *AH = (ArchiveHandle *) fout; |
| 20099 |
TocEntry *te; |
| 20100 |
|
| 20101 |
/* Scan all TOC entries in the archive */ |
| 20102 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 20103 |
{ |
| 20104 |
DumpableObject *dobj; |
| 20105 |
DumpId *dependencies; |
| 20106 |
int nDeps; |
| 20107 |
int allocDeps; |
| 20108 |
|
| 20109 |
/* No need to process entries that will not be dumped */ |
| 20110 |
if (te->reqs == 0) |
| 20111 |
continue; |
| 20112 |
/* Ignore entries that already have "special" dependencies */ |
| 20113 |
if (te->nDeps > 0) |
| 20114 |
continue; |
| 20115 |
/* Otherwise, look up the item's original DumpableObject, if any */ |
| 20116 |
dobj = findObjectByDumpId(te->dumpId); |
| 20117 |
if (dobj == NULL) |
| 20118 |
continue; |
| 20119 |
/* No work if it has no dependencies */ |
| 20120 |
if (dobj->nDeps <= 0) |
| 20121 |
continue; |
| 20122 |
/* Set up work array */ |
| 20123 |
allocDeps = 64; |
| 20124 |
dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId)); |
| 20125 |
nDeps = 0; |
| 20126 |
/* Recursively find all dumpable dependencies */ |
| 20127 |
findDumpableDependencies(AH, dobj, |
| 20128 |
&dependencies, &nDeps, &allocDeps); |
| 20129 |
/* And save 'em ... */ |
| 20130 |
if (nDeps > 0) |
| 20131 |
{ |
| 20132 |
dependencies = (DumpId *) pg_realloc(dependencies, |
| 20133 |
nDeps * sizeof(DumpId)); |
| 20134 |
te->dependencies = dependencies; |
| 20135 |
te->nDeps = nDeps; |
| 20136 |
} |
| 20137 |
else |
| 20138 |
free(dependencies); |
| 20139 |
} |
| 20140 |
} |
| 20141 |
|
| 20142 |
/* Recursive search subroutine for BuildArchiveDependencies */ |
| 20143 |
static void |
| 20144 |
findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, |
| 20145 |
DumpId **dependencies, int *nDeps, int *allocDeps) |
| 20146 |
{ |
| 20147 |
int i; |
| 20148 |
|
| 20149 |
/* |
| 20150 |
* Ignore section boundary objects: if we search through them, we'll |
| 20151 |
* report lots of bogus dependencies. |
| 20152 |
*/ |
| 20153 |
if (dobj->objType == DO_PRE_DATA_BOUNDARY || |
| 20154 |
dobj->objType == DO_POST_DATA_BOUNDARY) |
| 20155 |
return; |
| 20156 |
|
| 20157 |
for (i = 0; i < dobj->nDeps; i++) |
| 20158 |
{ |
| 20159 |
DumpId depid = dobj->dependencies[i]; |
| 20160 |
|
| 20161 |
if (TocIDRequired(AH, depid) != 0) |
| 20162 |
{ |
| 20163 |
/* Object will be dumped, so just reference it as a dependency */ |
| 20164 |
if (*nDeps >= *allocDeps) |
| 20165 |
{ |
| 20166 |
*allocDeps *= 2; |
| 20167 |
*dependencies = (DumpId *) pg_realloc(*dependencies, |
| 20168 |
*allocDeps * sizeof(DumpId)); |
| 20169 |
} |
| 20170 |
(*dependencies)[*nDeps] = depid; |
| 20171 |
(*nDeps)++; |
| 20172 |
} |
| 20173 |
else |
| 20174 |
{ |
| 20175 |
/* |
| 20176 |
* Object will not be dumped, so recursively consider its deps. We |
| 20177 |
* rely on the assumption that sortDumpableObjects already broke |
| 20178 |
* any dependency loops, else we might recurse infinitely. |
| 20179 |
*/ |
| 20180 |
DumpableObject *otherdobj = findObjectByDumpId(depid); |
| 20181 |
|
| 20182 |
if (otherdobj) |
| 20183 |
findDumpableDependencies(AH, otherdobj, |
| 20184 |
dependencies, nDeps, allocDeps); |
| 20185 |
} |
| 20186 |
} |
| 20187 |
} |
| 20188 |
|
| 20189 |
|
| 20190 |
/* |
| 20191 |
* getFormattedTypeName - retrieve a nicely-formatted type name for the |
| 20192 |
* given type OID. |
| 20193 |
* |
| 20194 |
* This does not guarantee to schema-qualify the output, so it should not |
| 20195 |
* be used to create the target object name for CREATE or ALTER commands. |
| 20196 |
* |
| 20197 |
* Note that the result is cached and must not be freed by the caller. |
| 20198 |
*/ |
| 20199 |
static const char * |
| 20200 |
getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts) |
| 20201 |
{ |
| 20202 |
TypeInfo *typeInfo; |
| 20203 |
char *result; |
| 20204 |
PQExpBuffer query; |
| 20205 |
PGresult *res; |
| 20206 |
|
| 20207 |
if (oid == 0) |
| 20208 |
{ |
| 20209 |
if ((opts & zeroAsStar) != 0) |
| 20210 |
return "*"; |
| 20211 |
else if ((opts & zeroAsNone) != 0) |
| 20212 |
return "NONE"; |
| 20213 |
} |
| 20214 |
|
| 20215 |
/* see if we have the result cached in the type's TypeInfo record */ |
| 20216 |
typeInfo = findTypeByOid(oid); |
| 20217 |
if (typeInfo && typeInfo->ftypname) |
| 20218 |
return typeInfo->ftypname; |
| 20219 |
|
| 20220 |
query = createPQExpBuffer(); |
| 20221 |
appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)", |
| 20222 |
oid); |
| 20223 |
|
| 20224 |
res = ExecuteSqlQueryForSingleRow(fout, query->data); |
| 20225 |
|
| 20226 |
/* result of format_type is already quoted */ |
| 20227 |
result = pg_strdup(PQgetvalue(res, 0, 0)); |
| 20228 |
|
| 20229 |
PQclear(res); |
| 20230 |
destroyPQExpBuffer(query); |
| 20231 |
|
| 20232 |
/* |
| 20233 |
* Cache the result for re-use in later requests, if possible. If we |
| 20234 |
* don't have a TypeInfo for the type, the string will be leaked once the |
| 20235 |
* caller is done with it ... but that case really should not happen, so |
| 20236 |
* leaking if it does seems acceptable. |
| 20237 |
*/ |
| 20238 |
if (typeInfo) |
| 20239 |
typeInfo->ftypname = result; |
| 20240 |
|
| 20241 |
return result; |
| 20242 |
} |
| 20243 |
|
| 20244 |
/* |
| 20245 |
* Return a column list clause for the given relation. |
| 20246 |
* |
| 20247 |
* Special case: if there are no undropped columns in the relation, return |
| 20248 |
* "", not an invalid "()" column list. |
| 20249 |
*/ |
| 20250 |
static const char * |
| 20251 |
fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer) |
| 20252 |
{ |
| 20253 |
int numatts = ti->numatts; |
| 20254 |
char **attnames = ti->attnames; |
| 20255 |
bool *attisdropped = ti->attisdropped; |
| 20256 |
char *attgenerated = ti->attgenerated; |
| 20257 |
bool needComma; |
| 20258 |
int i; |
| 20259 |
|
| 20260 |
appendPQExpBufferChar(buffer, '('); |
| 20261 |
needComma = false; |
| 20262 |
for (i = 0; i < numatts; i++) |
| 20263 |
{ |
| 20264 |
if (attisdropped[i]) |
| 20265 |
continue; |
| 20266 |
if (attgenerated[i]) |
| 20267 |
continue; |
| 20268 |
if (needComma) |
| 20269 |
appendPQExpBufferStr(buffer, ", "); |
| 20270 |
appendPQExpBufferStr(buffer, fmtId(attnames[i])); |
| 20271 |
needComma = true; |
| 20272 |
} |
| 20273 |
|
| 20274 |
if (!needComma) |
| 20275 |
return ""; /* no undropped columns */ |
| 20276 |
|
| 20277 |
appendPQExpBufferChar(buffer, ')'); |
| 20278 |
return buffer->data; |
| 20279 |
} |
| 20280 |
|
| 20281 |
/* |
| 20282 |
* Check if a reloptions array is nonempty. |
| 20283 |
*/ |
| 20284 |
static bool |
| 20285 |
nonemptyReloptions(const char *reloptions) |
| 20286 |
{ |
| 20287 |
/* Don't want to print it if it's just "{}" */ |
| 20288 |
return (reloptions != NULL && strlen(reloptions) > 2); |
| 20289 |
} |
| 20290 |
|
| 20291 |
/* |
| 20292 |
* Format a reloptions array and append it to the given buffer. |
| 20293 |
* |
| 20294 |
* "prefix" is prepended to the option names; typically it's "" or "toast.". |
| 20295 |
*/ |
| 20296 |
static void |
| 20297 |
appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, |
| 20298 |
const char *prefix, Archive *fout) |
| 20299 |
{ |
| 20300 |
bool res; |
| 20301 |
|
| 20302 |
res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding, |
| 20303 |
fout->std_strings); |
| 20304 |
if (!res) |
| 20305 |
pg_log_warning("could not parse %s array", "reloptions"); |
| 20306 |
} |
| 20307 |
|
| 20308 |
/* |
| 20309 |
* read_dump_filters - retrieve object identifier patterns from file |
| 20310 |
* |
| 20311 |
* Parse the specified filter file for include and exclude patterns, and add |
| 20312 |
* them to the relevant lists. If the filename is "-" then filters will be |
| 20313 |
* read from STDIN rather than a file. |
| 20314 |
*/ |
| 20315 |
static void |
| 20316 |
read_dump_filters(const char *filename, DumpOptions *dopt) |
| 20317 |
{ |
| 20318 |
FilterStateData fstate; |
| 20319 |
char *objname; |
| 20320 |
FilterCommandType comtype; |
| 20321 |
FilterObjectType objtype; |
| 20322 |
|
| 20323 |
filter_init(&fstate, filename, exit_nicely); |
| 20324 |
|
| 20325 |
while (filter_read_item(&fstate, &objname, &comtype, &objtype)) |
| 20326 |
{ |
| 20327 |
if (comtype == FILTER_COMMAND_TYPE_INCLUDE) |
| 20328 |
{ |
| 20329 |
switch (objtype) |
| 20330 |
{ |
| 20331 |
case FILTER_OBJECT_TYPE_NONE: |
| 20332 |
break; |
| 20333 |
case FILTER_OBJECT_TYPE_DATABASE: |
| 20334 |
case FILTER_OBJECT_TYPE_FUNCTION: |
| 20335 |
case FILTER_OBJECT_TYPE_INDEX: |
| 20336 |
case FILTER_OBJECT_TYPE_TABLE_DATA: |
| 20337 |
case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN: |
| 20338 |
case FILTER_OBJECT_TYPE_TRIGGER: |
| 20339 |
pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"), |
| 20340 |
"include", |
| 20341 |
filter_object_type_name(objtype)); |
| 20342 |
exit_nicely(1); |
| 20343 |
break; /* unreachable */ |
| 20344 |
|
| 20345 |
case FILTER_OBJECT_TYPE_EXTENSION: |
| 20346 |
simple_string_list_append(&extension_include_patterns, objname); |
| 20347 |
break; |
| 20348 |
case FILTER_OBJECT_TYPE_FOREIGN_DATA: |
| 20349 |
simple_string_list_append(&foreign_servers_include_patterns, objname); |
| 20350 |
break; |
| 20351 |
case FILTER_OBJECT_TYPE_SCHEMA: |
| 20352 |
simple_string_list_append(&schema_include_patterns, objname); |
| 20353 |
dopt->include_everything = false; |
| 20354 |
break; |
| 20355 |
case FILTER_OBJECT_TYPE_TABLE: |
| 20356 |
simple_string_list_append(&table_include_patterns, objname); |
| 20357 |
dopt->include_everything = false; |
| 20358 |
break; |
| 20359 |
case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN: |
| 20360 |
simple_string_list_append(&table_include_patterns_and_children, |
| 20361 |
objname); |
| 20362 |
dopt->include_everything = false; |
| 20363 |
break; |
| 20364 |
} |
| 20365 |
} |
| 20366 |
else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE) |
| 20367 |
{ |
| 20368 |
switch (objtype) |
| 20369 |
{ |
| 20370 |
case FILTER_OBJECT_TYPE_NONE: |
| 20371 |
break; |
| 20372 |
case FILTER_OBJECT_TYPE_DATABASE: |
| 20373 |
case FILTER_OBJECT_TYPE_FUNCTION: |
| 20374 |
case FILTER_OBJECT_TYPE_INDEX: |
| 20375 |
case FILTER_OBJECT_TYPE_TRIGGER: |
| 20376 |
case FILTER_OBJECT_TYPE_FOREIGN_DATA: |
| 20377 |
pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"), |
| 20378 |
"exclude", |
| 20379 |
filter_object_type_name(objtype)); |
| 20380 |
exit_nicely(1); |
| 20381 |
break; |
| 20382 |
|
| 20383 |
case FILTER_OBJECT_TYPE_EXTENSION: |
| 20384 |
simple_string_list_append(&extension_exclude_patterns, objname); |
| 20385 |
break; |
| 20386 |
case FILTER_OBJECT_TYPE_TABLE_DATA: |
| 20387 |
simple_string_list_append(&tabledata_exclude_patterns, |
| 20388 |
objname); |
| 20389 |
break; |
| 20390 |
case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN: |
| 20391 |
simple_string_list_append(&tabledata_exclude_patterns_and_children, |
| 20392 |
objname); |
| 20393 |
break; |
| 20394 |
case FILTER_OBJECT_TYPE_SCHEMA: |
| 20395 |
simple_string_list_append(&schema_exclude_patterns, objname); |
| 20396 |
break; |
| 20397 |
case FILTER_OBJECT_TYPE_TABLE: |
| 20398 |
simple_string_list_append(&table_exclude_patterns, objname); |
| 20399 |
break; |
| 20400 |
case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN: |
| 20401 |
simple_string_list_append(&table_exclude_patterns_and_children, |
| 20402 |
objname); |
| 20403 |
break; |
| 20404 |
} |
| 20405 |
} |
| 20406 |
else |
| 20407 |
{ |
| 20408 |
Assert(comtype == FILTER_COMMAND_TYPE_NONE); |
| 20409 |
Assert(objtype == FILTER_OBJECT_TYPE_NONE); |
| 20410 |
} |
| 20411 |
|
| 20412 |
if (objname) |
| 20413 |
free(objname); |
| 20414 |
} |
| 20415 |
|
| 20416 |
filter_free(&fstate); |
| 20417 |
} |