PostgreSQL Source Code  git master
pg_dump.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include "access/attnum.h"
#include "access/sysattr.h"
#include "access/transam.h"
#include "catalog/pg_aggregate_d.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_attribute_d.h"
#include "catalog/pg_authid_d.h"
#include "catalog/pg_cast_d.h"
#include "catalog/pg_class_d.h"
#include "catalog/pg_default_acl_d.h"
#include "catalog/pg_largeobject_d.h"
#include "catalog/pg_largeobject_metadata_d.h"
#include "catalog/pg_proc_d.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_trigger_d.h"
#include "catalog/pg_type_d.h"
#include "common/connect.h"
#include "common/relpath.h"
#include "compress_io.h"
#include "dumputils.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
#include "filter.h"
#include "getopt_long.h"
#include "libpq/libpq-fs.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "storage/block.h"
Include dependency graph for pg_dump.c:

Go to the source code of this file.

Data Structures

struct  RoleNameItem
 
struct  CommentItem
 
struct  SecLabelItem
 

Macros

#define DUMP_DEFAULT_ROWS_PER_INSERT   1
 
#define fmtQualifiedDumpable(obj)
 

Typedefs

typedef enum OidOptions OidOptions
 

Enumerations

enum  OidOptions { zeroIsError = 1 , zeroAsStar = 2 , zeroAsNone = 4 }
 

Functions

static void help (const char *progname)
 
static void setup_connection (Archive *AH, const char *dumpencoding, const char *dumpsnapshot, char *use_role)
 
static ArchiveFormat parseArchiveFormat (const char *format, ArchiveMode *mode)
 
static void expand_schema_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_extension_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_foreign_server_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids)
 
static void expand_table_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names, bool with_child_tables)
 
static void prohibit_crossdb_refs (PGconn *conn, const char *dbname, const char *pattern)
 
static NamespaceInfofindNamespace (Oid nsoid)
 
static void dumpTableData (Archive *fout, const TableDataInfo *tdinfo)
 
static void refreshMatViewData (Archive *fout, const TableDataInfo *tdinfo)
 
static const char * getRoleName (const char *roleoid_str)
 
static void collectRoleNames (Archive *fout)
 
static void getAdditionalACLs (Archive *fout)
 
static void dumpCommentExtended (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId, const char *initdb_comment)
 
static void dumpComment (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
 
static int findComments (Oid classoid, Oid objoid, CommentItem **items)
 
static void collectComments (Archive *fout)
 
static void dumpSecLabel (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
 
static int findSecLabels (Oid classoid, Oid objoid, SecLabelItem **items)
 
static void collectSecLabels (Archive *fout)
 
static void dumpDumpableObject (Archive *fout, DumpableObject *dobj)
 
static void dumpNamespace (Archive *fout, const NamespaceInfo *nspinfo)
 
static void dumpExtension (Archive *fout, const ExtensionInfo *extinfo)
 
static void dumpType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpBaseType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpEnumType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpRangeType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpUndefinedType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpDomain (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpCompositeType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpCompositeTypeColComments (Archive *fout, const TypeInfo *tyinfo, PGresult *res)
 
static void dumpShellType (Archive *fout, const ShellTypeInfo *stinfo)
 
static void dumpProcLang (Archive *fout, const ProcLangInfo *plang)
 
static void dumpFunc (Archive *fout, const FuncInfo *finfo)
 
static void dumpCast (Archive *fout, const CastInfo *cast)
 
static void dumpTransform (Archive *fout, const TransformInfo *transform)
 
static void dumpOpr (Archive *fout, const OprInfo *oprinfo)
 
static void dumpAccessMethod (Archive *fout, const AccessMethodInfo *aminfo)
 
static void dumpOpclass (Archive *fout, const OpclassInfo *opcinfo)
 
static void dumpOpfamily (Archive *fout, const OpfamilyInfo *opfinfo)
 
static void dumpCollation (Archive *fout, const CollInfo *collinfo)
 
static void dumpConversion (Archive *fout, const ConvInfo *convinfo)
 
static void dumpRule (Archive *fout, const RuleInfo *rinfo)
 
static void dumpAgg (Archive *fout, const AggInfo *agginfo)
 
static void dumpTrigger (Archive *fout, const TriggerInfo *tginfo)
 
static void dumpEventTrigger (Archive *fout, const EventTriggerInfo *evtinfo)
 
static void dumpTable (Archive *fout, const TableInfo *tbinfo)
 
static void dumpTableSchema (Archive *fout, const TableInfo *tbinfo)
 
static void dumpTableAttach (Archive *fout, const TableAttachInfo *attachinfo)
 
static void dumpAttrDef (Archive *fout, const AttrDefInfo *adinfo)
 
static void dumpSequence (Archive *fout, const TableInfo *tbinfo)
 
static void dumpSequenceData (Archive *fout, const TableDataInfo *tdinfo)
 
static void dumpIndex (Archive *fout, const IndxInfo *indxinfo)
 
static void dumpIndexAttach (Archive *fout, const IndexAttachInfo *attachinfo)
 
static void dumpStatisticsExt (Archive *fout, const StatsExtInfo *statsextinfo)
 
static void dumpConstraint (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTableConstraintComment (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTSParser (Archive *fout, const TSParserInfo *prsinfo)
 
static void dumpTSDictionary (Archive *fout, const TSDictInfo *dictinfo)
 
static void dumpTSTemplate (Archive *fout, const TSTemplateInfo *tmplinfo)
 
static void dumpTSConfig (Archive *fout, const TSConfigInfo *cfginfo)
 
static void dumpForeignDataWrapper (Archive *fout, const FdwInfo *fdwinfo)
 
static void dumpForeignServer (Archive *fout, const ForeignServerInfo *srvinfo)
 
static void dumpUserMappings (Archive *fout, const char *servername, const char *namespace, const char *owner, CatalogId catalogId, DumpId dumpId)
 
static void dumpDefaultACL (Archive *fout, const DefaultACLInfo *daclinfo)
 
static DumpId dumpACL (Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const DumpableAcl *dacl)
 
static void getDependencies (Archive *fout)
 
static void BuildArchiveDependencies (Archive *fout)
 
static void findDumpableDependencies (ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
 
static DumpableObjectcreateBoundaryObjects (void)
 
static void addBoundaryDependencies (DumpableObject **dobjs, int numObjs, DumpableObject *boundaryObjs)
 
static void addConstrChildIdxDeps (DumpableObject *dobj, const IndxInfo *refidx)
 
static void getDomainConstraints (Archive *fout, TypeInfo *tyinfo)
 
static void getTableData (DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
 
static void makeTableDataInfo (DumpOptions *dopt, TableInfo *tbinfo)
 
static void buildMatViewRefreshDependencies (Archive *fout)
 
static void getTableDataFKConstraints (void)
 
static char * format_function_arguments (const FuncInfo *finfo, const char *funcargs, bool is_agg)
 
static char * format_function_signature (Archive *fout, const FuncInfo *finfo, bool honor_quotes)
 
static char * convertRegProcReference (const char *proc)
 
static char * getFormattedOperatorName (const char *oproid)
 
static char * convertTSFunction (Archive *fout, Oid funcOid)
 
static const char * getFormattedTypeName (Archive *fout, Oid oid, OidOptions opts)
 
static void getLOs (Archive *fout)
 
static void dumpLO (Archive *fout, const LoInfo *loinfo)
 
static int dumpLOs (Archive *fout, const void *arg)
 
static void dumpPolicy (Archive *fout, const PolicyInfo *polinfo)
 
static void dumpPublication (Archive *fout, const PublicationInfo *pubinfo)
 
static void dumpPublicationTable (Archive *fout, const PublicationRelInfo *pubrinfo)
 
static void dumpSubscription (Archive *fout, const SubscriptionInfo *subinfo)
 
static void dumpSubscriptionTable (Archive *fout, const SubRelInfo *subrinfo)
 
static void dumpDatabase (Archive *fout)
 
static void dumpDatabaseConfig (Archive *AH, PQExpBuffer outbuf, const char *dbname, Oid dboid)
 
static void dumpEncoding (Archive *AH)
 
static void dumpStdStrings (Archive *AH)
 
static void dumpSearchPath (Archive *AH)
 
static void binary_upgrade_set_type_oids_by_type_oid (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type, bool include_multirange_type)
 
static void binary_upgrade_set_type_oids_by_rel (Archive *fout, PQExpBuffer upgrade_buffer, const TableInfo *tbinfo)
 
static void binary_upgrade_set_pg_class_oids (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
 
static void binary_upgrade_extension_member (PQExpBuffer upgrade_buffer, const DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
 
static const char * getAttrName (int attrnum, const TableInfo *tblInfo)
 
static const char * fmtCopyColumnList (const TableInfo *ti, PQExpBuffer buffer)
 
static bool nonemptyReloptions (const char *reloptions)
 
static void appendReloptionsArrayAH (PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
 
static char * get_synchronized_snapshot (Archive *fout)
 
static void setupDumpWorker (Archive *AH)
 
static TableInfogetRootTableInfo (const TableInfo *tbinfo)
 
static bool forcePartitionRootLoad (const TableInfo *tbinfo)
 
static void read_dump_filters (const char *filename, DumpOptions *dopt)
 
int main (int argc, char **argv)
 
static bool checkExtensionMembership (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableNamespace (NamespaceInfo *nsinfo, Archive *fout)
 
static void selectDumpableTable (TableInfo *tbinfo, Archive *fout)
 
static void selectDumpableType (TypeInfo *tyinfo, Archive *fout)
 
static void selectDumpableDefaultACL (DefaultACLInfo *dinfo, DumpOptions *dopt)
 
static void selectDumpableCast (CastInfo *cast, Archive *fout)
 
static void selectDumpableProcLang (ProcLangInfo *plang, Archive *fout)
 
static void selectDumpableAccessMethod (AccessMethodInfo *method, Archive *fout)
 
static void selectDumpableExtension (ExtensionInfo *extinfo, DumpOptions *dopt)
 
static void selectDumpablePublicationObject (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableStatisticsObject (StatsExtInfo *sobj, Archive *fout)
 
static void selectDumpableObject (DumpableObject *dobj, Archive *fout)
 
static int dumpTableData_copy (Archive *fout, const void *dcontext)
 
static int dumpTableData_insert (Archive *fout, const void *dcontext)
 
void getPolicies (Archive *fout, TableInfo tblinfo[], int numTables)
 
PublicationInfogetPublications (Archive *fout, int *numPublications)
 
void getPublicationNamespaces (Archive *fout)
 
void getPublicationTables (Archive *fout, TableInfo tblinfo[], int numTables)
 
static void dumpPublicationNamespace (Archive *fout, const PublicationSchemaInfo *pubsinfo)
 
static bool is_superuser (Archive *fout)
 
void getSubscriptions (Archive *fout)
 
void getSubscriptionTables (Archive *fout)
 
static void append_depends_on_extension (Archive *fout, PQExpBuffer create, const DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
 
static Oid get_next_possible_free_pg_type_oid (Archive *fout, PQExpBuffer upgrade_query)
 
NamespaceInfogetNamespaces (Archive *fout, int *numNamespaces)
 
ExtensionInfogetExtensions (Archive *fout, int *numExtensions)
 
TypeInfogetTypes (Archive *fout, int *numTypes)
 
OprInfogetOperators (Archive *fout, int *numOprs)
 
CollInfogetCollations (Archive *fout, int *numCollations)
 
ConvInfogetConversions (Archive *fout, int *numConversions)
 
AccessMethodInfogetAccessMethods (Archive *fout, int *numAccessMethods)
 
OpclassInfogetOpclasses (Archive *fout, int *numOpclasses)
 
OpfamilyInfogetOpfamilies (Archive *fout, int *numOpfamilies)
 
AggInfogetAggregates (Archive *fout, int *numAggs)
 
FuncInfogetFuncs (Archive *fout, int *numFuncs)
 
TableInfogetTables (Archive *fout, int *numTables)
 
void getOwnedSeqs (Archive *fout, TableInfo tblinfo[], int numTables)
 
InhInfogetInherits (Archive *fout, int *numInherits)
 
void getPartitioningInfo (Archive *fout)
 
void getIndexes (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getExtendedStatistics (Archive *fout)
 
void getConstraints (Archive *fout, TableInfo tblinfo[], int numTables)
 
RuleInfogetRules (Archive *fout, int *numRules)
 
void getTriggers (Archive *fout, TableInfo tblinfo[], int numTables)
 
EventTriggerInfogetEventTriggers (Archive *fout, int *numEventTriggers)
 
ProcLangInfogetProcLangs (Archive *fout, int *numProcLangs)
 
CastInfogetCasts (Archive *fout, int *numCasts)
 
static char * get_language_name (Archive *fout, Oid langid)
 
TransformInfogetTransforms (Archive *fout, int *numTransforms)
 
void getTableAttrs (Archive *fout, TableInfo *tblinfo, int numTables)
 
bool shouldPrintColumn (const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
 
TSParserInfogetTSParsers (Archive *fout, int *numTSParsers)
 
TSDictInfogetTSDictionaries (Archive *fout, int *numTSDicts)
 
TSTemplateInfogetTSTemplates (Archive *fout, int *numTSTemplates)
 
TSConfigInfogetTSConfigurations (Archive *fout, int *numTSConfigs)
 
FdwInfogetForeignDataWrappers (Archive *fout, int *numForeignDataWrappers)
 
ForeignServerInfogetForeignServers (Archive *fout, int *numForeignServers)
 
DefaultACLInfogetDefaultACLs (Archive *fout, int *numDefaultACLs)
 
static void dumpTableComment (Archive *fout, const TableInfo *tbinfo, const char *reltypename)
 
static char * format_aggregate_signature (const AggInfo *agginfo, Archive *fout, bool honor_quotes)
 
static void dumpTableSecLabel (Archive *fout, const TableInfo *tbinfo, const char *reltypename)
 
static PQExpBuffer createViewAsClause (Archive *fout, const TableInfo *tbinfo)
 
static PQExpBuffer createDummyViewAsClause (Archive *fout, const TableInfo *tbinfo)
 
void getExtensionMembership (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 
void processExtensionTables (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 

Variables

static bool dosync = true
 
static Oid g_last_builtin_oid
 
static int strict_names = 0
 
static pg_compress_algorithm compression_algorithm = PG_COMPRESSION_NONE
 
static SimpleStringList schema_include_patterns = {NULL, NULL}
 
static SimpleOidList schema_include_oids = {NULL, NULL}
 
static SimpleStringList schema_exclude_patterns = {NULL, NULL}
 
static SimpleOidList schema_exclude_oids = {NULL, NULL}
 
static SimpleStringList table_include_patterns = {NULL, NULL}
 
static SimpleStringList table_include_patterns_and_children = {NULL, NULL}
 
static SimpleOidList table_include_oids = {NULL, NULL}
 
static SimpleStringList table_exclude_patterns = {NULL, NULL}
 
static SimpleStringList table_exclude_patterns_and_children = {NULL, NULL}
 
static SimpleOidList table_exclude_oids = {NULL, NULL}
 
static SimpleStringList tabledata_exclude_patterns = {NULL, NULL}
 
static SimpleStringList tabledata_exclude_patterns_and_children = {NULL, NULL}
 
static SimpleOidList tabledata_exclude_oids = {NULL, NULL}
 
static SimpleStringList foreign_servers_include_patterns = {NULL, NULL}
 
static SimpleOidList foreign_servers_include_oids = {NULL, NULL}
 
static SimpleStringList extension_include_patterns = {NULL, NULL}
 
static SimpleOidList extension_include_oids = {NULL, NULL}
 
static const CatalogId nilCatalogId = {0, 0}
 
static bool have_extra_float_digits = false
 
static int extra_float_digits
 
static RoleNameItemrolenames = NULL
 
static int nrolenames = 0
 
static CommentItemcomments = NULL
 
static int ncomments = 0
 
static SecLabelItemseclabels = NULL
 
static int nseclabels = 0
 

Macro Definition Documentation

◆ DUMP_DEFAULT_ROWS_PER_INSERT

#define DUMP_DEFAULT_ROWS_PER_INSERT   1

Definition at line 161 of file pg_dump.c.

◆ fmtQualifiedDumpable

#define fmtQualifiedDumpable (   obj)
Value:
fmtQualifiedId((obj)->dobj.namespace->dobj.name, \
(obj)->dobj.name)
const char * fmtQualifiedId(const char *schema, const char *id)
Definition: string_utils.c:145

Definition at line 166 of file pg_dump.c.

Typedef Documentation

◆ OidOptions

typedef enum OidOptions OidOptions

Enumeration Type Documentation

◆ OidOptions

enum OidOptions
Enumerator
zeroIsError 
zeroAsStar 
zeroAsNone 

Definition at line 95 of file pg_dump.c.

96 {
97  zeroIsError = 1,
98  zeroAsStar = 2,
99  zeroAsNone = 4,
100 } OidOptions;
OidOptions
Definition: pg_dump.c:96
@ zeroIsError
Definition: pg_dump.c:97
@ zeroAsStar
Definition: pg_dump.c:98
@ zeroAsNone
Definition: pg_dump.c:99

Function Documentation

◆ addBoundaryDependencies()

static void addBoundaryDependencies ( DumpableObject **  dobjs,
int  numObjs,
DumpableObject boundaryObjs 
)
static

Definition at line 18547 of file pg_dump.c.

18549 {
18550  DumpableObject *preDataBound = boundaryObjs + 0;
18551  DumpableObject *postDataBound = boundaryObjs + 1;
18552  int i;
18553 
18554  for (i = 0; i < numObjs; i++)
18555  {
18556  DumpableObject *dobj = dobjs[i];
18557 
18558  /*
18559  * The classification of object types here must match the SECTION_xxx
18560  * values assigned during subsequent ArchiveEntry calls!
18561  */
18562  switch (dobj->objType)
18563  {
18564  case DO_NAMESPACE:
18565  case DO_EXTENSION:
18566  case DO_TYPE:
18567  case DO_SHELL_TYPE:
18568  case DO_FUNC:
18569  case DO_AGG:
18570  case DO_OPERATOR:
18571  case DO_ACCESS_METHOD:
18572  case DO_OPCLASS:
18573  case DO_OPFAMILY:
18574  case DO_COLLATION:
18575  case DO_CONVERSION:
18576  case DO_TABLE:
18577  case DO_TABLE_ATTACH:
18578  case DO_ATTRDEF:
18579  case DO_PROCLANG:
18580  case DO_CAST:
18581  case DO_DUMMY_TYPE:
18582  case DO_TSPARSER:
18583  case DO_TSDICT:
18584  case DO_TSTEMPLATE:
18585  case DO_TSCONFIG:
18586  case DO_FDW:
18587  case DO_FOREIGN_SERVER:
18588  case DO_TRANSFORM:
18589  case DO_LARGE_OBJECT:
18590  /* Pre-data objects: must come before the pre-data boundary */
18591  addObjectDependency(preDataBound, dobj->dumpId);
18592  break;
18593  case DO_TABLE_DATA:
18594  case DO_SEQUENCE_SET:
18595  case DO_LARGE_OBJECT_DATA:
18596  /* Data objects: must come between the boundaries */
18597  addObjectDependency(dobj, preDataBound->dumpId);
18598  addObjectDependency(postDataBound, dobj->dumpId);
18599  break;
18600  case DO_INDEX:
18601  case DO_INDEX_ATTACH:
18602  case DO_STATSEXT:
18603  case DO_REFRESH_MATVIEW:
18604  case DO_TRIGGER:
18605  case DO_EVENT_TRIGGER:
18606  case DO_DEFAULT_ACL:
18607  case DO_POLICY:
18608  case DO_PUBLICATION:
18609  case DO_PUBLICATION_REL:
18611  case DO_SUBSCRIPTION:
18612  case DO_SUBSCRIPTION_REL:
18613  /* Post-data objects: must come after the post-data boundary */
18614  addObjectDependency(dobj, postDataBound->dumpId);
18615  break;
18616  case DO_RULE:
18617  /* Rules are post-data, but only if dumped separately */
18618  if (((RuleInfo *) dobj)->separate)
18619  addObjectDependency(dobj, postDataBound->dumpId);
18620  break;
18621  case DO_CONSTRAINT:
18622  case DO_FK_CONSTRAINT:
18623  /* Constraints are post-data, but only if dumped separately */
18624  if (((ConstraintInfo *) dobj)->separate)
18625  addObjectDependency(dobj, postDataBound->dumpId);
18626  break;
18627  case DO_PRE_DATA_BOUNDARY:
18628  /* nothing to do */
18629  break;
18630  case DO_POST_DATA_BOUNDARY:
18631  /* must come after the pre-data boundary */
18632  addObjectDependency(dobj, preDataBound->dumpId);
18633  break;
18634  }
18635  }
18636 }
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:783
int i
Definition: isn.c:73
@ DO_EVENT_TRIGGER
Definition: pg_dump.h:79
@ DO_REFRESH_MATVIEW
Definition: pg_dump.h:80
@ DO_POLICY
Definition: pg_dump.h:81
@ DO_CAST
Definition: pg_dump.h:63
@ DO_FOREIGN_SERVER
Definition: pg_dump.h:72
@ DO_PRE_DATA_BOUNDARY
Definition: pg_dump.h:77
@ DO_PROCLANG
Definition: pg_dump.h:62
@ DO_TYPE
Definition: pg_dump.h:42
@ DO_INDEX
Definition: pg_dump.h:55
@ DO_COLLATION
Definition: pg_dump.h:50
@ DO_LARGE_OBJECT
Definition: pg_dump.h:75
@ DO_TSCONFIG
Definition: pg_dump.h:70
@ DO_OPERATOR
Definition: pg_dump.h:46
@ DO_FK_CONSTRAINT
Definition: pg_dump.h:61
@ DO_CONSTRAINT
Definition: pg_dump.h:60
@ DO_SUBSCRIPTION
Definition: pg_dump.h:85
@ DO_DEFAULT_ACL
Definition: pg_dump.h:73
@ DO_FDW
Definition: pg_dump.h:71
@ DO_SUBSCRIPTION_REL
Definition: pg_dump.h:86
@ DO_SEQUENCE_SET
Definition: pg_dump.h:65
@ DO_ATTRDEF
Definition: pg_dump.h:54
@ DO_PUBLICATION_REL
Definition: pg_dump.h:83
@ DO_TABLE_ATTACH
Definition: pg_dump.h:53
@ DO_OPCLASS
Definition: pg_dump.h:48
@ DO_INDEX_ATTACH
Definition: pg_dump.h:56
@ DO_TSTEMPLATE
Definition: pg_dump.h:69
@ DO_STATSEXT
Definition: pg_dump.h:57
@ DO_FUNC
Definition: pg_dump.h:44
@ DO_POST_DATA_BOUNDARY
Definition: pg_dump.h:78
@ DO_LARGE_OBJECT_DATA
Definition: pg_dump.h:76
@ DO_OPFAMILY
Definition: pg_dump.h:49
@ DO_TRANSFORM
Definition: pg_dump.h:74
@ DO_ACCESS_METHOD
Definition: pg_dump.h:47
@ DO_PUBLICATION_TABLE_IN_SCHEMA
Definition: pg_dump.h:84
@ DO_CONVERSION
Definition: pg_dump.h:51
@ DO_TRIGGER
Definition: pg_dump.h:59
@ DO_RULE
Definition: pg_dump.h:58
@ DO_DUMMY_TYPE
Definition: pg_dump.h:66
@ DO_TSDICT
Definition: pg_dump.h:68
@ DO_TSPARSER
Definition: pg_dump.h:67
@ DO_EXTENSION
Definition: pg_dump.h:41
@ DO_TABLE_DATA
Definition: pg_dump.h:64
@ DO_PUBLICATION
Definition: pg_dump.h:82
@ DO_TABLE
Definition: pg_dump.h:52
@ DO_NAMESPACE
Definition: pg_dump.h:40
@ DO_AGG
Definition: pg_dump.h:45
@ DO_SHELL_TYPE
Definition: pg_dump.h:43
DumpId dumpId
Definition: pg_dump.h:137
DumpableObjectType objType
Definition: pg_dump.h:135

References addObjectDependency(), DO_ACCESS_METHOD, DO_AGG, DO_ATTRDEF, DO_CAST, DO_COLLATION, DO_CONSTRAINT, DO_CONVERSION, DO_DEFAULT_ACL, DO_DUMMY_TYPE, DO_EVENT_TRIGGER, DO_EXTENSION, DO_FDW, DO_FK_CONSTRAINT, DO_FOREIGN_SERVER, DO_FUNC, DO_INDEX, DO_INDEX_ATTACH, DO_LARGE_OBJECT, DO_LARGE_OBJECT_DATA, DO_NAMESPACE, DO_OPCLASS, DO_OPERATOR, DO_OPFAMILY, DO_POLICY, DO_POST_DATA_BOUNDARY, DO_PRE_DATA_BOUNDARY, DO_PROCLANG, DO_PUBLICATION, DO_PUBLICATION_REL, DO_PUBLICATION_TABLE_IN_SCHEMA, DO_REFRESH_MATVIEW, DO_RULE, DO_SEQUENCE_SET, DO_SHELL_TYPE, DO_STATSEXT, DO_SUBSCRIPTION, DO_SUBSCRIPTION_REL, DO_TABLE, DO_TABLE_ATTACH, DO_TABLE_DATA, DO_TRANSFORM, DO_TRIGGER, DO_TSCONFIG, DO_TSDICT, DO_TSPARSER, DO_TSTEMPLATE, DO_TYPE, _dumpableObject::dumpId, i, and _dumpableObject::objType.

Referenced by main().

◆ addConstrChildIdxDeps()

static void addConstrChildIdxDeps ( DumpableObject dobj,
const IndxInfo refidx 
)
static

Definition at line 7792 of file pg_dump.c.

7793 {
7794  SimplePtrListCell *cell;
7795 
7796  Assert(dobj->objType == DO_FK_CONSTRAINT);
7797 
7798  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7799  {
7800  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7801 
7802  addObjectDependency(dobj, attach->dobj.dumpId);
7803 
7804  if (attach->partitionIdx->partattaches.head != NULL)
7805  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7806  }
7807 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7792
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54
IndxInfo * partitionIdx
Definition: pg_dump.h:421
DumpableObject dobj
Definition: pg_dump.h:419
SimplePtrList partattaches
Definition: pg_dump.h:411

References addObjectDependency(), Assert(), DO_FK_CONSTRAINT, _indexAttachInfo::dobj, _dumpableObject::dumpId, SimplePtrList::head, SimplePtrListCell::next, _dumpableObject::objType, _indxInfo::partattaches, _indexAttachInfo::partitionIdx, and SimplePtrListCell::ptr.

Referenced by getConstraints().

◆ append_depends_on_extension()

static void append_depends_on_extension ( Archive fout,
PQExpBuffer  create,
const DumpableObject dobj,
const char *  catalog,
const char *  keyword,
const char *  objname 
)
static

Definition at line 5128 of file pg_dump.c.

5134 {
5135  if (dobj->depends_on_ext)
5136  {
5137  char *nm;
5138  PGresult *res;
5139  PQExpBuffer query;
5140  int ntups;
5141  int i_extname;
5142  int i;
5143 
5144  /* dodge fmtId() non-reentrancy */
5145  nm = pg_strdup(objname);
5146 
5147  query = createPQExpBuffer();
5148  appendPQExpBuffer(query,
5149  "SELECT e.extname "
5150  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
5151  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
5152  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
5153  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
5154  catalog,
5155  dobj->catId.oid);
5156  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5157  ntups = PQntuples(res);
5158  i_extname = PQfnumber(res, "extname");
5159  for (i = 0; i < ntups; i++)
5160  {
5161  appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
5162  keyword, nm,
5163  fmtId(PQgetvalue(res, i, i_extname)));
5164  }
5165 
5166  PQclear(res);
5167  destroyPQExpBuffer(query);
5168  pg_free(nm);
5169  }
5170 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3441
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3836
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3549
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:290
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
CatalogId catId
Definition: pg_dump.h:136
bool depends_on_ext
Definition: pg_dump.h:144

References appendPQExpBuffer(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpableObject::depends_on_ext, destroyPQExpBuffer(), ExecuteSqlQuery(), fmtId(), i, CatalogId::oid, pg_free(), pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), and res.

Referenced by dumpConstraint(), dumpFunc(), dumpIndex(), dumpTableSchema(), and dumpTrigger().

◆ appendReloptionsArrayAH()

static void appendReloptionsArrayAH ( PQExpBuffer  buffer,
const char *  reloptions,
const char *  prefix,
Archive fout 
)
static

Definition at line 18864 of file pg_dump.c.

18866 {
18867  bool res;
18868 
18869  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18870  fout->std_strings);
18871  if (!res)
18872  pg_log_warning("could not parse %s array", "reloptions");
18873 }
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:804
bool std_strings
Definition: pg_backup.h:228
int encoding
Definition: pg_backup.h:227

References appendReloptionsArray(), Archive::encoding, pg_log_warning, res, and Archive::std_strings.

Referenced by dumpConstraint(), dumpRule(), and dumpTableSchema().

◆ binary_upgrade_extension_member()

static void binary_upgrade_extension_member ( PQExpBuffer  upgrade_buffer,
const DumpableObject dobj,
const char *  objtype,
const char *  objname,
const char *  objnamespace 
)
static

Definition at line 5417 of file pg_dump.c.

5422 {
5423  DumpableObject *extobj = NULL;
5424  int i;
5425 
5426  if (!dobj->ext_member)
5427  return;
5428 
5429  /*
5430  * Find the parent extension. We could avoid this search if we wanted to
5431  * add a link field to DumpableObject, but the space costs of that would
5432  * be considerable. We assume that member objects could only have a
5433  * direct dependency on their own extension, not any others.
5434  */
5435  for (i = 0; i < dobj->nDeps; i++)
5436  {
5437  extobj = findObjectByDumpId(dobj->dependencies[i]);
5438  if (extobj && extobj->objType == DO_EXTENSION)
5439  break;
5440  extobj = NULL;
5441  }
5442  if (extobj == NULL)
5443  pg_fatal("could not find parent extension for %s %s",
5444  objtype, objname);
5445 
5446  appendPQExpBufferStr(upgrade_buffer,
5447  "\n-- For binary upgrade, handle extension membership the hard way\n");
5448  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
5449  fmtId(extobj->name),
5450  objtype);
5451  if (objnamespace && *objnamespace)
5452  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
5453  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
5454 }
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:730
#define pg_fatal(...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * name
Definition: pg_dump.h:138
DumpId * dependencies
Definition: pg_dump.h:145
bool ext_member
Definition: pg_dump.h:143

References appendPQExpBuffer(), appendPQExpBufferStr(), _dumpableObject::dependencies, DO_EXTENSION, _dumpableObject::ext_member, findObjectByDumpId(), fmtId(), i, _dumpableObject::name, _dumpableObject::nDeps, _dumpableObject::objType, and pg_fatal.

Referenced by dumpAccessMethod(), dumpAgg(), dumpBaseType(), dumpCast(), dumpCollation(), dumpCompositeType(), dumpConversion(), dumpDomain(), dumpEnumType(), dumpEventTrigger(), dumpForeignDataWrapper(), dumpForeignServer(), dumpFunc(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpProcLang(), dumpRangeType(), dumpSequence(), dumpTableSchema(), dumpTransform(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), and dumpUndefinedType().

◆ binary_upgrade_set_pg_class_oids()

static void binary_upgrade_set_pg_class_oids ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_class_oid,
bool  is_index 
)
static

Definition at line 5301 of file pg_dump.c.

5304 {
5305  PQExpBuffer upgrade_query = createPQExpBuffer();
5306  PGresult *upgrade_res;
5307  RelFileNumber relfilenumber;
5308  Oid toast_oid;
5309  RelFileNumber toast_relfilenumber;
5310  char relkind;
5311  Oid toast_index_oid;
5312  RelFileNumber toast_index_relfilenumber;
5313 
5314  /*
5315  * Preserve the OID and relfilenumber of the table, table's index, table's
5316  * toast table and toast table's index if any.
5317  *
5318  * One complexity is that the current table definition might not require
5319  * the creation of a TOAST table, but the old database might have a TOAST
5320  * table that was created earlier, before some wide columns were dropped.
5321  * By setting the TOAST oid we force creation of the TOAST heap and index
5322  * by the new backend, so we can copy the files during binary upgrade
5323  * without worrying about this case.
5324  */
5325  appendPQExpBuffer(upgrade_query,
5326  "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
5327  "FROM pg_catalog.pg_class c LEFT JOIN "
5328  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5329  "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5330  "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5331  "WHERE c.oid = '%u'::pg_catalog.oid;",
5332  pg_class_oid);
5333 
5334  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5335 
5336  relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
5337 
5338  relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5339  PQfnumber(upgrade_res, "relfilenode")));
5340  toast_oid = atooid(PQgetvalue(upgrade_res, 0,
5341  PQfnumber(upgrade_res, "reltoastrelid")));
5342  toast_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5343  PQfnumber(upgrade_res, "toast_relfilenode")));
5344  toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
5345  PQfnumber(upgrade_res, "indexrelid")));
5346  toast_index_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5347  PQfnumber(upgrade_res, "toast_index_relfilenode")));
5348 
5349  appendPQExpBufferStr(upgrade_buffer,
5350  "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
5351 
5352  if (!is_index)
5353  {
5354  appendPQExpBuffer(upgrade_buffer,
5355  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5356  pg_class_oid);
5357 
5358  /*
5359  * Not every relation has storage. Also, in a pre-v12 database,
5360  * partitioned tables have a relfilenumber, which should not be
5361  * preserved when upgrading.
5362  */
5363  if (RelFileNumberIsValid(relfilenumber) && relkind != RELKIND_PARTITIONED_TABLE)
5364  appendPQExpBuffer(upgrade_buffer,
5365  "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
5366  relfilenumber);
5367 
5368  /*
5369  * In a pre-v12 database, partitioned tables might be marked as having
5370  * toast tables, but we should ignore them if so.
5371  */
5372  if (OidIsValid(toast_oid) &&
5373  relkind != RELKIND_PARTITIONED_TABLE)
5374  {
5375  appendPQExpBuffer(upgrade_buffer,
5376  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
5377  toast_oid);
5378  appendPQExpBuffer(upgrade_buffer,
5379  "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
5380  toast_relfilenumber);
5381 
5382  /* every toast table has an index */
5383  appendPQExpBuffer(upgrade_buffer,
5384  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5385  toast_index_oid);
5386  appendPQExpBuffer(upgrade_buffer,
5387  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5388  toast_index_relfilenumber);
5389  }
5390 
5391  PQclear(upgrade_res);
5392  }
5393  else
5394  {
5395  /* Preserve the OID and relfilenumber of the index */
5396  appendPQExpBuffer(upgrade_buffer,
5397  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5398  pg_class_oid);
5399  appendPQExpBuffer(upgrade_buffer,
5400  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5401  relfilenumber);
5402  }
5403 
5404  appendPQExpBufferChar(upgrade_buffer, '\n');
5405 
5406  destroyPQExpBuffer(upgrade_query);
5407 }
#define OidIsValid(objectId)
Definition: c.h:764
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:305
unsigned int Oid
Definition: postgres_ext.h:31
#define atooid(x)
Definition: postgres_ext.h:42
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
Oid RelFileNumber
Definition: relpath.h:25
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), atooid, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), ExecuteSqlQueryForSingleRow(), OidIsValid, PQclear(), PQfnumber(), PQgetvalue(), and RelFileNumberIsValid.

Referenced by dumpCompositeType(), dumpConstraint(), dumpIndex(), dumpSequence(), and dumpTableSchema().

◆ binary_upgrade_set_type_oids_by_rel()

static void binary_upgrade_set_type_oids_by_rel ( Archive fout,
PQExpBuffer  upgrade_buffer,
const TableInfo tbinfo 
)
static

Definition at line 5289 of file pg_dump.c.

5292 {
5293  Oid pg_type_oid = tbinfo->reltype;
5294 
5295  if (OidIsValid(pg_type_oid))
5296  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5297  pg_type_oid, false, false);
5298 }
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type, bool include_multirange_type)
Definition: pg_dump.c:5204
Oid reltype
Definition: pg_dump.h:309

References binary_upgrade_set_type_oids_by_type_oid(), OidIsValid, and _tableInfo::reltype.

Referenced by dumpTableSchema().

◆ binary_upgrade_set_type_oids_by_type_oid()

static void binary_upgrade_set_type_oids_by_type_oid ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_type_oid,
bool  force_array_type,
bool  include_multirange_type 
)
static

Definition at line 5204 of file pg_dump.c.

5209 {
5210  PQExpBuffer upgrade_query = createPQExpBuffer();
5211  PGresult *res;
5212  Oid pg_type_array_oid;
5213  Oid pg_type_multirange_oid;
5214  Oid pg_type_multirange_array_oid;
5215 
5216  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
5217  appendPQExpBuffer(upgrade_buffer,
5218  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5219  pg_type_oid);
5220 
5221  appendPQExpBuffer(upgrade_query,
5222  "SELECT typarray "
5223  "FROM pg_catalog.pg_type "
5224  "WHERE oid = '%u'::pg_catalog.oid;",
5225  pg_type_oid);
5226 
5227  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5228 
5229  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5230 
5231  PQclear(res);
5232 
5233  if (!OidIsValid(pg_type_array_oid) && force_array_type)
5234  pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5235 
5236  if (OidIsValid(pg_type_array_oid))
5237  {
5238  appendPQExpBufferStr(upgrade_buffer,
5239  "\n-- For binary upgrade, must preserve pg_type array oid\n");
5240  appendPQExpBuffer(upgrade_buffer,
5241  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5242  pg_type_array_oid);
5243  }
5244 
5245  /*
5246  * Pre-set the multirange type oid and its own array type oid.
5247  */
5248  if (include_multirange_type)
5249  {
5250  if (fout->remoteVersion >= 140000)
5251  {
5252  printfPQExpBuffer(upgrade_query,
5253  "SELECT t.oid, t.typarray "
5254  "FROM pg_catalog.pg_type t "
5255  "JOIN pg_catalog.pg_range r "
5256  "ON t.oid = r.rngmultitypid "
5257  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
5258  pg_type_oid);
5259 
5260  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5261 
5262  pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
5263  pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5264 
5265  PQclear(res);
5266  }
5267  else
5268  {
5269  pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5270  pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5271  }
5272 
5273  appendPQExpBufferStr(upgrade_buffer,
5274  "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
5275  appendPQExpBuffer(upgrade_buffer,
5276  "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5277  pg_type_multirange_oid);
5278  appendPQExpBufferStr(upgrade_buffer,
5279  "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
5280  appendPQExpBuffer(upgrade_buffer,
5281  "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5282  pg_type_multirange_array_oid);
5283  }
5284 
5285  destroyPQExpBuffer(upgrade_query);
5286 }
static Oid get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
Definition: pg_dump.c:5173
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
int remoteVersion
Definition: pg_backup.h:217

References appendPQExpBuffer(), appendPQExpBufferStr(), atooid, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), ExecuteSqlQueryForSingleRow(), get_next_possible_free_pg_type_oid(), OidIsValid, PQclear(), PQfnumber(), PQgetvalue(), printfPQExpBuffer(), Archive::remoteVersion, and res.

Referenced by binary_upgrade_set_type_oids_by_rel(), dumpBaseType(), dumpCompositeType(), dumpDomain(), dumpEnumType(), dumpRangeType(), dumpShellType(), and dumpUndefinedType().

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

Definition at line 18663 of file pg_dump.c.

18664 {
18665  ArchiveHandle *AH = (ArchiveHandle *) fout;
18666  TocEntry *te;
18667 
18668  /* Scan all TOC entries in the archive */
18669  for (te = AH->toc->next; te != AH->toc; te = te->next)
18670  {
18671  DumpableObject *dobj;
18672  DumpId *dependencies;
18673  int nDeps;
18674  int allocDeps;
18675 
18676  /* No need to process entries that will not be dumped */
18677  if (te->reqs == 0)
18678  continue;
18679  /* Ignore entries that already have "special" dependencies */
18680  if (te->nDeps > 0)
18681  continue;
18682  /* Otherwise, look up the item's original DumpableObject, if any */
18683  dobj = findObjectByDumpId(te->dumpId);
18684  if (dobj == NULL)
18685  continue;
18686  /* No work if it has no dependencies */
18687  if (dobj->nDeps <= 0)
18688  continue;
18689  /* Set up work array */
18690  allocDeps = 64;
18691  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18692  nDeps = 0;
18693  /* Recursively find all dumpable dependencies */
18694  findDumpableDependencies(AH, dobj,
18695  &dependencies, &nDeps, &allocDeps);
18696  /* And save 'em ... */
18697  if (nDeps > 0)
18698  {
18699  dependencies = (DumpId *) pg_realloc(dependencies,
18700  nDeps * sizeof(DumpId));
18701  te->dependencies = dependencies;
18702  te->nDeps = nDeps;
18703  }
18704  else
18705  free(dependencies);
18706  }
18707 }
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define free(a)
Definition: header.h:65
int DumpId
Definition: pg_backup.h:268
static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:18711
struct _tocEntry * toc
struct _tocEntry * next
DumpId * dependencies

References _tocEntry::dependencies, _tocEntry::dumpId, findDumpableDependencies(), findObjectByDumpId(), free, _tocEntry::nDeps, _dumpableObject::nDeps, _tocEntry::next, pg_malloc(), pg_realloc(), _tocEntry::reqs, and _archiveHandle::toc.

Referenced by main().

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

Definition at line 2814 of file pg_dump.c.

2815 {
2816  PQExpBuffer query;
2817  PGresult *res;
2818  int ntups,
2819  i;
2820  int i_classid,
2821  i_objid,
2822  i_refobjid;
2823 
2824  /* No Mat Views before 9.3. */
2825  if (fout->remoteVersion < 90300)
2826  return;
2827 
2828  query = createPQExpBuffer();
2829 
2830  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2831  "( "
2832  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2833  "FROM pg_depend d1 "
2834  "JOIN pg_class c1 ON c1.oid = d1.objid "
2835  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2836  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2837  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2838  "AND d2.objid = r1.oid "
2839  "AND d2.refobjid <> d1.objid "
2840  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2841  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2842  CppAsString2(RELKIND_VIEW) ") "
2843  "WHERE d1.classid = 'pg_class'::regclass "
2844  "UNION "
2845  "SELECT w.objid, d3.refobjid, c3.relkind "
2846  "FROM w "
2847  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2848  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2849  "AND d3.objid = r3.oid "
2850  "AND d3.refobjid <> w.refobjid "
2851  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2852  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2853  CppAsString2(RELKIND_VIEW) ") "
2854  ") "
2855  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2856  "FROM w "
2857  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2858 
2859  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2860 
2861  ntups = PQntuples(res);
2862 
2863  i_classid = PQfnumber(res, "classid");
2864  i_objid = PQfnumber(res, "objid");
2865  i_refobjid = PQfnumber(res, "refobjid");
2866 
2867  for (i = 0; i < ntups; i++)
2868  {
2869  CatalogId objId;
2870  CatalogId refobjId;
2871  DumpableObject *dobj;
2872  DumpableObject *refdobj;
2873  TableInfo *tbinfo;
2874  TableInfo *reftbinfo;
2875 
2876  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2877  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2878  refobjId.tableoid = objId.tableoid;
2879  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2880 
2881  dobj = findObjectByCatalogId(objId);
2882  if (dobj == NULL)
2883  continue;
2884 
2885  Assert(dobj->objType == DO_TABLE);
2886  tbinfo = (TableInfo *) dobj;
2887  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2888  dobj = (DumpableObject *) tbinfo->dataObj;
2889  if (dobj == NULL)
2890  continue;
2891  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2892 
2893  refdobj = findObjectByCatalogId(refobjId);
2894  if (refdobj == NULL)
2895  continue;
2896 
2897  Assert(refdobj->objType == DO_TABLE);
2898  reftbinfo = (TableInfo *) refdobj;
2899  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2900  refdobj = (DumpableObject *) reftbinfo->dataObj;
2901  if (refdobj == NULL)
2902  continue;
2903  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2904 
2905  addObjectDependency(dobj, refdobj->dumpId);
2906 
2907  if (!reftbinfo->relispopulated)
2908  tbinfo->relispopulated = false;
2909  }
2910 
2911  PQclear(res);
2912 
2913  destroyPQExpBuffer(query);
2914 }
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:743
#define CppAsString2(x)
Definition: c.h:316
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid tableoid
Definition: pg_backup.h:264
bool relispopulated
Definition: pg_dump.h:290
struct _tableDataInfo * dataObj
Definition: pg_dump.h:366
char relkind
Definition: pg_dump.h:288

References addObjectDependency(), appendPQExpBufferStr(), Assert(), atooid, CppAsString2, createPQExpBuffer(), PQExpBufferData::data, _tableInfo::dataObj, destroyPQExpBuffer(), DO_REFRESH_MATVIEW, DO_TABLE, _dumpableObject::dumpId, ExecuteSqlQuery(), findObjectByCatalogId(), i, if(), _dumpableObject::objType, CatalogId::oid, PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), _tableInfo::relispopulated, _tableInfo::relkind, Archive::remoteVersion, res, and CatalogId::tableoid.

Referenced by main().

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

Definition at line 1682 of file pg_dump.c.

1683 {
1684  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1685 
1686  if (ext == NULL)
1687  return false;
1688 
1689  dobj->ext_member = true;
1690 
1691  /* Record dependency so that getDependencies needn't deal with that */
1692  addObjectDependency(dobj, ext->dobj.dumpId);
1693 
1694  /*
1695  * In 9.6 and above, mark the member object to have any non-initial ACLs
1696  * dumped. (Any initial ACLs will be removed later, using data from
1697  * pg_init_privs, so that we'll dump only the delta from the extension's
1698  * initial setup.)
1699  *
1700  * Prior to 9.6, we do not include any extension member components.
1701  *
1702  * In binary upgrades, we still dump all components of the members
1703  * individually, since the idea is to exactly reproduce the database
1704  * contents rather than replace the extension contents with something
1705  * different.
1706  *
1707  * Note: it might be interesting someday to implement storage and delta
1708  * dumping of extension members' RLS policies and/or security labels.
1709  * However there is a pitfall for RLS policies: trying to dump them
1710  * requires getting a lock on their tables, and the calling user might not
1711  * have privileges for that. We need no lock to examine a table's ACLs,
1712  * so the current feature doesn't have a problem of that sort.
1713  */
1714  if (fout->dopt->binary_upgrade)
1715  dobj->dump = ext->dobj.dump;
1716  else
1717  {
1718  if (fout->remoteVersion < 90600)
1719  dobj->dump = DUMP_COMPONENT_NONE;
1720  else
1721  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL);
1722  }
1723 
1724  return true;
1725 }
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:1034
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:101
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:96
DumpOptions * dopt
Definition: pg_backup.h:212
int binary_upgrade
Definition: pg_backup.h:164
DumpComponents dump
Definition: pg_dump.h:139
DumpComponents dump_contains
Definition: pg_dump.h:141
DumpableObject dobj
Definition: pg_dump.h:181

References addObjectDependency(), _dumpOptions::binary_upgrade, _dumpableObject::catId, _extensionInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_ACL, DUMP_COMPONENT_NONE, _dumpableObject::dump_contains, _dumpableObject::dumpId, _dumpableObject::ext_member, findOwningExtension(), and Archive::remoteVersion.

Referenced by selectDumpableAccessMethod(), selectDumpableCast(), selectDumpableNamespace(), selectDumpableObject(), selectDumpableProcLang(), selectDumpablePublicationObject(), selectDumpableStatisticsObject(), selectDumpableTable(), and selectDumpableType().

◆ collectComments()

static void collectComments ( Archive fout)
static

Definition at line 10400 of file pg_dump.c.

10401 {
10402  PGresult *res;
10403  PQExpBuffer query;
10404  int i_description;
10405  int i_classoid;
10406  int i_objoid;
10407  int i_objsubid;
10408  int ntups;
10409  int i;
10410  DumpableObject *dobj;
10411 
10412  query = createPQExpBuffer();
10413 
10414  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
10415  "FROM pg_catalog.pg_description "
10416  "ORDER BY classoid, objoid, objsubid");
10417 
10418  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10419 
10420  /* Construct lookup table containing OIDs in numeric form */
10421 
10422  i_description = PQfnumber(res, "description");
10423  i_classoid = PQfnumber(res, "classoid");
10424  i_objoid = PQfnumber(res, "objoid");
10425  i_objsubid = PQfnumber(res, "objsubid");
10426 
10427  ntups = PQntuples(res);
10428 
10429  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10430  ncomments = 0;
10431  dobj = NULL;
10432 
10433  for (i = 0; i < ntups; i++)
10434  {
10435  CatalogId objId;
10436  int subid;
10437 
10438  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
10439  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
10440  subid = atoi(PQgetvalue(res, i, i_objsubid));
10441 
10442  /* We needn't remember comments that don't match any dumpable object */
10443  if (dobj == NULL ||
10444  dobj->catId.tableoid != objId.tableoid ||
10445  dobj->catId.oid != objId.oid)
10446  dobj = findObjectByCatalogId(objId);
10447  if (dobj == NULL)
10448  continue;
10449 
10450  /*
10451  * Comments on columns of composite types are linked to the type's
10452  * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
10453  * in the type's own DumpableObject.
10454  */
10455  if (subid != 0 && dobj->objType == DO_TABLE &&
10456  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
10457  {
10458  TypeInfo *cTypeInfo;
10459 
10460  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
10461  if (cTypeInfo)
10462  cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
10463  }
10464  else
10465  dobj->components |= DUMP_COMPONENT_COMMENT;
10466 
10467  comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
10469  comments[ncomments].objoid = objId.oid;
10470  comments[ncomments].objsubid = subid;
10471  ncomments++;
10472  }
10473 
10474  PQclear(res);
10475  destroyPQExpBuffer(query);
10476 }
TypeInfo * findTypeByOid(Oid oid)
Definition: common.c:864
static int ncomments
Definition: pg_dump.c:151
static CommentItem * comments
Definition: pg_dump.c:150
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:99
Oid classoid
Definition: pg_dump.c:81
Oid objoid
Definition: pg_dump.c:82
int objsubid
Definition: pg_dump.c:83
const char * descr
Definition: pg_dump.c:80
DumpComponents components
Definition: pg_dump.h:142
DumpableObject dobj
Definition: pg_dump.h:191

References appendPQExpBufferStr(), atooid, _dumpableObject::catId, CommentItem::classoid, comments, _dumpableObject::components, createPQExpBuffer(), PQExpBufferData::data, CommentItem::descr, destroyPQExpBuffer(), DO_TABLE, _typeInfo::dobj, DUMP_COMPONENT_COMMENT, ExecuteSqlQuery(), findObjectByCatalogId(), findTypeByOid(), i, ncomments, CommentItem::objoid, CommentItem::objsubid, _dumpableObject::objType, CatalogId::oid, pg_malloc(), pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), res, and CatalogId::tableoid.

Referenced by main().

◆ collectRoleNames()

static void collectRoleNames ( Archive fout)
static

Definition at line 9945 of file pg_dump.c.

9946 {
9947  PGresult *res;
9948  const char *query;
9949  int i;
9950 
9951  query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
9952 
9953  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
9954 
9956 
9958 
9959  for (i = 0; i < nrolenames; i++)
9960  {
9963  }
9964 
9965  PQclear(res);
9966 }
static RoleNameItem * rolenames
Definition: pg_dump.c:146
static int nrolenames
Definition: pg_dump.c:147
const char * rolename
Definition: pg_dump.c:75
Oid roleoid
Definition: pg_dump.c:74

References atooid, ExecuteSqlQuery(), i, nrolenames, pg_malloc(), pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQgetvalue(), PQntuples(), res, RoleNameItem::rolename, rolenames, and RoleNameItem::roleoid.

Referenced by main().

◆ collectSecLabels()

static void collectSecLabels ( Archive fout)
static

Definition at line 15518 of file pg_dump.c.

15519 {
15520  PGresult *res;
15521  PQExpBuffer query;
15522  int i_label;
15523  int i_provider;
15524  int i_classoid;
15525  int i_objoid;
15526  int i_objsubid;
15527  int ntups;
15528  int i;
15529  DumpableObject *dobj;
15530 
15531  query = createPQExpBuffer();
15532 
15533  appendPQExpBufferStr(query,
15534  "SELECT label, provider, classoid, objoid, objsubid "
15535  "FROM pg_catalog.pg_seclabel "
15536  "ORDER BY classoid, objoid, objsubid");
15537 
15538  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15539 
15540  /* Construct lookup table containing OIDs in numeric form */
15541  i_label = PQfnumber(res, "label");
15542  i_provider = PQfnumber(res, "provider");
15543  i_classoid = PQfnumber(res, "classoid");
15544  i_objoid = PQfnumber(res, "objoid");
15545  i_objsubid = PQfnumber(res, "objsubid");
15546 
15547  ntups = PQntuples(res);
15548 
15549  seclabels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15550  nseclabels = 0;
15551  dobj = NULL;
15552 
15553  for (i = 0; i < ntups; i++)
15554  {
15555  CatalogId objId;
15556  int subid;
15557 
15558  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
15559  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
15560  subid = atoi(PQgetvalue(res, i, i_objsubid));
15561 
15562  /* We needn't remember labels that don't match any dumpable object */
15563  if (dobj == NULL ||
15564  dobj->catId.tableoid != objId.tableoid ||
15565  dobj->catId.oid != objId.oid)
15566  dobj = findObjectByCatalogId(objId);
15567  if (dobj == NULL)
15568  continue;
15569 
15570  /*
15571  * Labels on columns of composite types are linked to the type's
15572  * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
15573  * in the type's own DumpableObject.
15574  */
15575  if (subid != 0 && dobj->objType == DO_TABLE &&
15576  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
15577  {
15578  TypeInfo *cTypeInfo;
15579 
15580  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
15581  if (cTypeInfo)
15582  cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
15583  }
15584  else
15585  dobj->components |= DUMP_COMPONENT_SECLABEL;
15586 
15590  seclabels[nseclabels].objoid = objId.oid;
15591  seclabels[nseclabels].objsubid = subid;
15592  nseclabels++;
15593  }
15594 
15595  PQclear(res);
15596  destroyPQExpBuffer(query);
15597 }
static int nseclabels
Definition: pg_dump.c:155
static SecLabelItem * seclabels
Definition: pg_dump.c:154
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:100
const char * provider
Definition: pg_dump.c:88
Oid classoid
Definition: pg_dump.c:90
int objsubid
Definition: pg_dump.c:92
const char * label
Definition: pg_dump.c:89
Oid objoid
Definition: pg_dump.c:91

References appendPQExpBufferStr(), atooid, _dumpableObject::catId, SecLabelItem::classoid, _dumpableObject::components, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), DO_TABLE, _typeInfo::dobj, DUMP_COMPONENT_SECLABEL, ExecuteSqlQuery(), findObjectByCatalogId(), findTypeByOid(), i, SecLabelItem::label, nseclabels, SecLabelItem::objoid, SecLabelItem::objsubid, _dumpableObject::objType, CatalogId::oid, pg_malloc(), pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), SecLabelItem::provider, res, seclabels, and CatalogId::tableoid.

Referenced by main().

◆ convertRegProcReference()

static char * convertRegProcReference ( const char *  proc)
static

Definition at line 13086 of file pg_dump.c.

13087 {
13088  char *name;
13089  char *paren;
13090  bool inquote;
13091 
13092  /* In all cases "-" means a null reference */
13093  if (strcmp(proc, "-") == 0)
13094  return NULL;
13095 
13096  name = pg_strdup(proc);
13097  /* find non-double-quoted left paren */
13098  inquote = false;
13099  for (paren = name; *paren; paren++)
13100  {
13101  if (*paren == '(' && !inquote)
13102  {
13103  *paren = '\0';
13104  break;
13105  }
13106  if (*paren == '"')
13107  inquote = !inquote;
13108  }
13109  return name;
13110 }
const char * name

References name, and pg_strdup().

Referenced by dumpOpr().

◆ convertTSFunction()

static char * convertTSFunction ( Archive fout,
Oid  funcOid 
)
static

Definition at line 13157 of file pg_dump.c.

13158 {
13159  char *result;
13160  char query[128];
13161  PGresult *res;
13162 
13163  snprintf(query, sizeof(query),
13164  "SELECT '%u'::pg_catalog.regproc", funcOid);
13165  res = ExecuteSqlQueryForSingleRow(fout, query);
13166 
13167  result = pg_strdup(PQgetvalue(res, 0, 0));
13168 
13169  PQclear(res);
13170 
13171  return result;
13172 }
#define snprintf
Definition: port.h:238

References ExecuteSqlQueryForSingleRow(), pg_strdup(), PQclear(), PQgetvalue(), res, and snprintf.

Referenced by dumpTSParser(), and dumpTSTemplate().

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

Definition at line 18523 of file pg_dump.c.

18524 {
18525  DumpableObject *dobjs;
18526 
18527  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18528 
18529  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18530  dobjs[0].catId = nilCatalogId;
18531  AssignDumpId(dobjs + 0);
18532  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18533 
18534  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18535  dobjs[1].catId = nilCatalogId;
18536  AssignDumpId(dobjs + 1);
18537  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18538 
18539  return dobjs;
18540 }
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:646
static const CatalogId nilCatalogId
Definition: pg_dump.c:139

References AssignDumpId(), _dumpableObject::catId, DO_POST_DATA_BOUNDARY, DO_PRE_DATA_BOUNDARY, _dumpableObject::name, nilCatalogId, _dumpableObject::objType, pg_malloc(), and pg_strdup().

Referenced by main().

◆ createDummyViewAsClause()

static PQExpBuffer createDummyViewAsClause ( Archive fout,
const TableInfo tbinfo 
)
static

Definition at line 15793 of file pg_dump.c.

15794 {
15795  PQExpBuffer result = createPQExpBuffer();
15796  int j;
15797 
15798  appendPQExpBufferStr(result, "SELECT");
15799 
15800  for (j = 0; j < tbinfo->numatts; j++)
15801  {
15802  if (j > 0)
15803  appendPQExpBufferChar(result, ',');
15804  appendPQExpBufferStr(result, "\n ");
15805 
15806  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15807 
15808  /*
15809  * Must add collation if not default for the type, because CREATE OR
15810  * REPLACE VIEW won't change it
15811  */
15812  if (OidIsValid(tbinfo->attcollation[j]))
15813  {
15814  CollInfo *coll;
15815 
15816  coll = findCollationByOid(tbinfo->attcollation[j]);
15817  if (coll)
15818  appendPQExpBuffer(result, " COLLATE %s",
15819  fmtQualifiedDumpable(coll));
15820  }
15821 
15822  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15823  }
15824 
15825  return result;
15826 }
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:919
int j
Definition: isn.c:74
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:166
int numatts
Definition: pg_dump.h:332
Oid * attcollation
Definition: pg_dump.h:345
char ** atttypnames
Definition: pg_dump.h:334
char ** attnames
Definition: pg_dump.h:333

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), _tableInfo::attcollation, _tableInfo::attnames, _tableInfo::atttypnames, createPQExpBuffer(), findCollationByOid(), fmtId(), fmtQualifiedDumpable, j, _tableInfo::numatts, and OidIsValid.

Referenced by dumpRule(), and dumpTableSchema().

◆ createViewAsClause()

static PQExpBuffer createViewAsClause ( Archive fout,
const TableInfo tbinfo 
)
static

Definition at line 15744 of file pg_dump.c.

15745 {
15746  PQExpBuffer query = createPQExpBuffer();
15747  PQExpBuffer result = createPQExpBuffer();
15748  PGresult *res;
15749  int len;
15750 
15751  /* Fetch the view definition */
15752  appendPQExpBuffer(query,
15753  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15754  tbinfo->dobj.catId.oid);
15755 
15756  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15757 
15758  if (PQntuples(res) != 1)
15759  {
15760  if (PQntuples(res) < 1)
15761  pg_fatal("query to obtain definition of view \"%s\" returned no data",
15762  tbinfo->dobj.name);
15763  else
15764  pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
15765  tbinfo->dobj.name);
15766  }
15767 
15768  len = PQgetlength(res, 0, 0);
15769 
15770  if (len == 0)
15771  pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
15772  tbinfo->dobj.name);
15773 
15774  /* Strip off the trailing semicolon so that other things may follow. */
15775  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15776  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15777 
15778  PQclear(res);
15779  destroyPQExpBuffer(query);
15780 
15781  return result;
15782 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3847
const void size_t len
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:397
DumpableObject dobj
Definition: pg_dump.h:285

References appendBinaryPQExpBuffer(), appendPQExpBuffer(), Assert(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _tableInfo::dobj, ExecuteSqlQuery(), len, _dumpableObject::name, CatalogId::oid, pg_fatal, PGRES_TUPLES_OK, PQclear(), PQgetlength(), PQgetvalue(), PQntuples(), and res.

Referenced by dumpRule(), and dumpTableSchema().

◆ dumpAccessMethod()

static void dumpAccessMethod ( Archive fout,
const AccessMethodInfo aminfo 
)
static

Definition at line 13179 of file pg_dump.c.

13180 {
13181  DumpOptions *dopt = fout->dopt;
13182  PQExpBuffer q;
13183  PQExpBuffer delq;
13184  char *qamname;
13185 
13186  /* Do nothing in data-only dump */
13187  if (dopt->dataOnly)
13188  return;
13189 
13190  q = createPQExpBuffer();
13191  delq = createPQExpBuffer();
13192 
13193  qamname = pg_strdup(fmtId(aminfo->dobj.name));
13194 
13195  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
13196 
13197  switch (aminfo->amtype)
13198  {
13199  case AMTYPE_INDEX:
13200  appendPQExpBufferStr(q, "TYPE INDEX ");
13201  break;
13202  case AMTYPE_TABLE:
13203  appendPQExpBufferStr(q, "TYPE TABLE ");
13204  break;
13205  default:
13206  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
13207  aminfo->amtype, qamname);
13208  destroyPQExpBuffer(q);
13209  destroyPQExpBuffer(delq);
13210  free(qamname);
13211  return;
13212  }
13213 
13214  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
13215 
13216  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
13217  qamname);
13218 
13219  if (dopt->binary_upgrade)
13221  "ACCESS METHOD", qamname, NULL);
13222 
13223  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13224  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
13225  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
13226  .description = "ACCESS METHOD",
13227  .section = SECTION_PRE_DATA,
13228  .createStmt = q->data,
13229  .dropStmt = delq->data));
13230 
13231  /* Dump Access Method Comments */
13232  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13233  dumpComment(fout, "ACCESS METHOD", qamname,
13234  NULL, "",
13235  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
13236 
13237  destroyPQExpBuffer(q);
13238  destroyPQExpBuffer(delq);
13239  free(qamname);
13240 }
@ SECTION_PRE_DATA
Definition: pg_backup.h:58
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
#define ARCHIVE_OPTS(...)
static void dumpComment(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:10209
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, const DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:5417
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:97
char * amhandler
Definition: pg_dump.h:253
DumpableObject dobj
Definition: pg_dump.h:251
bool dataOnly
Definition: pg_backup.h:168

References _accessMethodInfo::amhandler, _accessMethodInfo::amtype, appendPQExpBuffer(), appendPQExpBufferStr(), ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _accessMethodInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, fmtId(), free, _dumpableObject::name, pg_log_warning, pg_strdup(), and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

◆ dumpACL()

static DumpId dumpACL ( Archive fout,
DumpId  objDumpId,
DumpId  altDumpId,
const char *  type,
const char *  name,
const char *  subname,
const char *  nspname,
const char *  owner,
const DumpableAcl dacl 
)
static

Definition at line 15151 of file pg_dump.c.

15155 {
15156  DumpId aclDumpId = InvalidDumpId;
15157  DumpOptions *dopt = fout->dopt;
15158  const char *acls = dacl->acl;
15159  const char *acldefault = dacl->acldefault;
15160  char privtype = dacl->privtype;
15161  const char *initprivs = dacl->initprivs;
15162  const char *baseacls;
15163  PQExpBuffer sql;
15164 
15165  /* Do nothing if ACL dump is not enabled */
15166  if (dopt->aclsSkip)
15167  return InvalidDumpId;
15168 
15169  /* --data-only skips ACLs *except* large object ACLs */
15170  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
15171  return InvalidDumpId;
15172 
15173  sql = createPQExpBuffer();
15174 
15175  /*
15176  * In binary upgrade mode, we don't run an extension's script but instead
15177  * dump out the objects independently and then recreate them. To preserve
15178  * any initial privileges which were set on extension objects, we need to
15179  * compute the set of GRANT and REVOKE commands necessary to get from the
15180  * default privileges of an object to its initial privileges as recorded
15181  * in pg_init_privs.
15182  *
15183  * At restore time, we apply these commands after having called
15184  * binary_upgrade_set_record_init_privs(true). That tells the backend to
15185  * copy the results into pg_init_privs. This is how we preserve the
15186  * contents of that catalog across binary upgrades.
15187  */
15188  if (dopt->binary_upgrade && privtype == 'e' &&
15189  initprivs && *initprivs != '\0')
15190  {
15191  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
15192  if (!buildACLCommands(name, subname, nspname, type,
15193  initprivs, acldefault, owner,
15194  "", fout->remoteVersion, sql))
15195  pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
15196  initprivs, acldefault, name, type);
15197  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
15198  }
15199 
15200  /*
15201  * Now figure the GRANT and REVOKE commands needed to get to the object's
15202  * actual current ACL, starting from the initprivs if given, else from the
15203  * object-type-specific default. Also, while buildACLCommands will assume
15204  * that a NULL/empty acls string means it needn't do anything, what that
15205  * actually represents is the object-type-specific default; so we need to
15206  * substitute the acldefault string to get the right results in that case.
15207  */
15208  if (initprivs && *initprivs != '\0')
15209  {
15210  baseacls = initprivs;
15211  if (acls == NULL || *acls == '\0')
15212  acls = acldefault;
15213  }
15214  else
15215  baseacls = acldefault;
15216 
15217  if (!buildACLCommands(name, subname, nspname, type,
15218  acls, baseacls, owner,
15219  "", fout->remoteVersion, sql))
15220  pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
15221  acls, baseacls, name, type);
15222 
15223  if (sql->len > 0)
15224  {
15226  DumpId aclDeps[2];
15227  int nDeps = 0;
15228 
15229  if (subname)
15230  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
15231  else
15232  appendPQExpBuffer(tag, "%s %s", type, name);
15233 
15234  aclDeps[nDeps++] = objDumpId;
15235  if (altDumpId != InvalidDumpId)
15236  aclDeps[nDeps++] = altDumpId;
15237 
15238  aclDumpId = createDumpId();
15239 
15240  ArchiveEntry(fout, nilCatalogId, aclDumpId,
15241  ARCHIVE_OPTS(.tag = tag->data,
15242  .namespace = nspname,
15243  .owner = owner,
15244  .description = "ACL",
15245  .section = SECTION_NONE,
15246  .createStmt = sql->data,
15247  .deps = aclDeps,
15248  .nDeps = nDeps));
15249 
15250  destroyPQExpBuffer(tag);
15251  }
15252 
15253  destroyPQExpBuffer(sql);
15254 
15255  return aclDumpId;
15256 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:777
DumpId createDumpId(void)
Definition: common.c:710
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:64
@ SECTION_NONE
Definition: pg_backup.h:57
#define InvalidDumpId
Definition: pg_backup.h:270
NameData subname
bool aclsSkip
Definition: pg_backup.h:170
char privtype
Definition: pg_dump.h:159
char * acldefault
Definition: pg_dump.h:157
char * acl
Definition: pg_dump.h:156
char * initprivs
Definition: pg_dump.h:160
const char * type

References _dumpableAcl::acl, acldefault(), _dumpableAcl::acldefault, _dumpOptions::aclsSkip, appendPQExpBuffer(), appendPQExpBufferStr(), ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, buildACLCommands(), createDumpId(), createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), Archive::dopt, _dumpableAcl::initprivs, InvalidDumpId, PQExpBufferData::len, name, nilCatalogId, pg_fatal, _dumpableAcl::privtype, Archive::remoteVersion, SECTION_NONE, subname, and type.

Referenced by dumpAgg(), dumpBaseType(), dumpCompositeType(), dumpDatabase(), dumpDomain(), dumpEnumType(), dumpForeignDataWrapper(), dumpForeignServer(), dumpFunc(), dumpLO(), dumpNamespace(), dumpProcLang(), dumpRangeType(), dumpTable(), and dumpUndefinedType().

◆ dumpAgg()

static void dumpAgg ( Archive fout,
const AggInfo agginfo 
)
static

Definition at line 14118 of file pg_dump.c.

14119 {
14120  DumpOptions *dopt = fout->dopt;
14121  PQExpBuffer query;
14122  PQExpBuffer q;
14123  PQExpBuffer delq;
14124  PQExpBuffer details;
14125  char *aggsig; /* identity signature */
14126  char *aggfullsig = NULL; /* full signature */
14127  char *aggsig_tag;
14128  PGresult *res;
14129  int i_agginitval;
14130  int i_aggminitval;
14131  const char *aggtransfn;
14132  const char *aggfinalfn;
14133  const char *aggcombinefn;
14134  const char *aggserialfn;
14135  const char *aggdeserialfn;
14136  const char *aggmtransfn;
14137  const char *aggminvtransfn;
14138  const char *aggmfinalfn;
14139  bool aggfinalextra;
14140  bool aggmfinalextra;
14141  char aggfinalmodify;
14142  char aggmfinalmodify;
14143  const char *aggsortop;
14144  char *aggsortconvop;
14145  char aggkind;
14146  const char *aggtranstype;
14147  const char *aggtransspace;
14148  const char *aggmtranstype;
14149  const char *aggmtransspace;
14150  const char *agginitval;
14151  const char *aggminitval;
14152  const char *proparallel;
14153  char defaultfinalmodify;
14154 
14155  /* Do nothing in data-only dump */
14156  if (dopt->dataOnly)
14157  return;
14158 
14159  query = createPQExpBuffer();
14160  q = createPQExpBuffer();
14161  delq = createPQExpBuffer();
14162  details = createPQExpBuffer();
14163 
14164  if (!fout->is_prepared[PREPQUERY_DUMPAGG])
14165  {
14166  /* Set up query for aggregate-specific details */
14167  appendPQExpBufferStr(query,
14168  "PREPARE dumpAgg(pg_catalog.oid) AS\n");
14169 
14170  appendPQExpBufferStr(query,
14171  "SELECT "
14172  "aggtransfn,\n"
14173  "aggfinalfn,\n"
14174  "aggtranstype::pg_catalog.regtype,\n"
14175  "agginitval,\n"
14176  "aggsortop,\n"
14177  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
14178  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
14179 
14180  if (fout->remoteVersion >= 90400)
14181  appendPQExpBufferStr(query,
14182  "aggkind,\n"
14183  "aggmtransfn,\n"
14184  "aggminvtransfn,\n"
14185  "aggmfinalfn,\n"
14186  "aggmtranstype::pg_catalog.regtype,\n"
14187  "aggfinalextra,\n"
14188  "aggmfinalextra,\n"
14189  "aggtransspace,\n"
14190  "aggmtransspace,\n"
14191  "aggminitval,\n");
14192  else
14193  appendPQExpBufferStr(query,
14194  "'n' AS aggkind,\n"
14195  "'-' AS aggmtransfn,\n"
14196  "'-' AS aggminvtransfn,\n"
14197  "'-' AS aggmfinalfn,\n"
14198  "0 AS aggmtranstype,\n"
14199  "false AS aggfinalextra,\n"
14200  "false AS aggmfinalextra,\n"
14201  "0 AS aggtransspace,\n"
14202  "0 AS aggmtransspace,\n"
14203  "NULL AS aggminitval,\n");
14204 
14205  if (fout->remoteVersion >= 90600)
14206  appendPQExpBufferStr(query,
14207  "aggcombinefn,\n"
14208  "aggserialfn,\n"
14209  "aggdeserialfn,\n"
14210  "proparallel,\n");
14211  else
14212  appendPQExpBufferStr(query,
14213  "'-' AS aggcombinefn,\n"
14214  "'-' AS aggserialfn,\n"
14215  "'-' AS aggdeserialfn,\n"
14216  "'u' AS proparallel,\n");
14217 
14218  if (fout->remoteVersion >= 110000)
14219  appendPQExpBufferStr(query,
14220  "aggfinalmodify,\n"
14221  "aggmfinalmodify\n");
14222  else
14223  appendPQExpBufferStr(query,
14224  "'0' AS aggfinalmodify,\n"
14225  "'0' AS aggmfinalmodify\n");
14226 
14227  appendPQExpBufferStr(query,
14228  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14229  "WHERE a.aggfnoid = p.oid "
14230  "AND p.oid = $1");
14231 
14232  ExecuteSqlStatement(fout, query->data);
14233 
14234  fout->is_prepared[PREPQUERY_DUMPAGG] = true;
14235  }
14236 
14237  printfPQExpBuffer(query,
14238  "EXECUTE dumpAgg('%u')",
14239  agginfo->aggfn.dobj.catId.oid);
14240 
14241  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14242 
14243  i_agginitval = PQfnumber(res, "agginitval");
14244  i_aggminitval = PQfnumber(res, "aggminitval");
14245 
14246  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
14247  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
14248  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
14249  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
14250  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
14251  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
14252  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
14253  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
14254  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
14255  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
14256  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
14257  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
14258  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
14259  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
14260  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
14261  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
14262  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
14263  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
14264  agginitval = PQgetvalue(res, 0, i_agginitval);
14265  aggminitval = PQgetvalue(res, 0, i_aggminitval);
14266  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
14267 
14268  {
14269  char *funcargs;
14270  char *funciargs;
14271 
14272  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
14273  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
14274  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
14275  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
14276  }
14277 
14278  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14279 
14280  /* identify default modify flag for aggkind (must match DefineAggregate) */
14281  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14282  /* replace omitted flags for old versions */
14283  if (aggfinalmodify == '0')
14284  aggfinalmodify = defaultfinalmodify;
14285  if (aggmfinalmodify == '0')
14286  aggmfinalmodify = defaultfinalmodify;
14287 
14288  /* regproc and regtype output is already sufficiently quoted */
14289  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14290  aggtransfn, aggtranstype);
14291 
14292  if (strcmp(aggtransspace, "0") != 0)
14293  {
14294  appendPQExpBuffer(details, ",\n SSPACE = %s",
14295  aggtransspace);
14296  }
14297 
14298  if (!PQgetisnull(res, 0, i_agginitval))
14299  {
14300  appendPQExpBufferStr(details, ",\n INITCOND = ");
14301  appendStringLiteralAH(details, agginitval, fout);
14302  }
14303 
14304  if (strcmp(aggfinalfn, "-") != 0)
14305  {
14306  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14307  aggfinalfn);
14308  if (aggfinalextra)
14309  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14310  if (aggfinalmodify != defaultfinalmodify)
14311  {
14312  switch (aggfinalmodify)
14313  {
14314  case AGGMODIFY_READ_ONLY:
14315  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14316  break;
14317  case AGGMODIFY_SHAREABLE:
14318  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14319  break;
14320  case AGGMODIFY_READ_WRITE:
14321  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14322  break;
14323  default:
14324  pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14325  agginfo->aggfn.dobj.name);
14326  break;
14327  }
14328  }
14329  }
14330 
14331  if (strcmp(aggcombinefn, "-") != 0)
14332  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14333 
14334  if (strcmp(aggserialfn, "-") != 0)
14335  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14336 
14337  if (strcmp(aggdeserialfn, "-") != 0)
14338  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14339 
14340  if (strcmp(aggmtransfn, "-") != 0)
14341  {
14342  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14343  aggmtransfn,
14344  aggminvtransfn,
14345  aggmtranstype);
14346  }
14347 
14348  if (strcmp(aggmtransspace, "0") != 0)
14349  {
14350  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14351  aggmtransspace);
14352  }
14353 
14354  if (!PQgetisnull(res, 0, i_aggminitval))
14355  {
14356  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14357  appendStringLiteralAH(details, aggminitval, fout);
14358  }
14359 
14360  if (strcmp(aggmfinalfn, "-") != 0)
14361  {
14362  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14363  aggmfinalfn);
14364  if (aggmfinalextra)
14365  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14366  if (aggmfinalmodify != defaultfinalmodify)
14367  {
14368  switch (aggmfinalmodify)
14369  {
14370  case AGGMODIFY_READ_ONLY:
14371  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14372  break;
14373  case AGGMODIFY_SHAREABLE:
14374  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14375  break;
14376  case AGGMODIFY_READ_WRITE:
14377  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14378  break;
14379  default:
14380  pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14381  agginfo->aggfn.dobj.name);
14382  break;
14383  }
14384  }
14385  }
14386 
14387  aggsortconvop = getFormattedOperatorName(aggsortop);
14388  if (aggsortconvop)
14389  {
14390  appendPQExpBuffer(details, ",\n SORTOP = %s",
14391  aggsortconvop);
14392  free(aggsortconvop);
14393  }
14394 
14395  if (aggkind == AGGKIND_HYPOTHETICAL)
14396  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14397 
14398  if (proparallel[0] != PROPARALLEL_UNSAFE)
14399  {
14400  if (proparallel[0] == PROPARALLEL_SAFE)
14401  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14402  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14403  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14404  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14405  pg_fatal("unrecognized proparallel value for function \"%s\"",
14406  agginfo->aggfn.dobj.name);
14407  }
14408 
14409  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14410  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14411  aggsig);
14412 
14413  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14414  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14415  aggfullsig ? aggfullsig : aggsig, details->data);
14416 
14417  if (dopt->binary_upgrade)
14418  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
14419  "AGGREGATE", aggsig,
14420  agginfo->aggfn.dobj.namespace->dobj.name);
14421 
14422  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14423  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14424  agginfo->aggfn.dobj.dumpId,
14425  ARCHIVE_OPTS(.tag = aggsig_tag,
14426  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14427  .owner = agginfo->aggfn.rolname,
14428  .description = "AGGREGATE",
14429  .section = SECTION_PRE_DATA,
14430  .createStmt = q->data,
14431  .dropStmt = delq->data));
14432 
14433  /* Dump Aggregate Comments */
14434  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14435  dumpComment(fout, "AGGREGATE", aggsig,
14436  agginfo->aggfn.dobj.namespace->dobj.name,
14437  agginfo->aggfn.rolname,
14438  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14439 
14440  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14441  dumpSecLabel(fout, "AGGREGATE", aggsig,
14442  agginfo->aggfn.dobj.namespace->dobj.name,
14443  agginfo->aggfn.rolname,
14444  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14445 
14446  /*
14447  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14448  * command look like a function's GRANT; in particular this affects the
14449  * syntax for zero-argument aggregates and ordered-set aggregates.
14450  */
14451  free(aggsig);
14452 
14453  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14454 
14455  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14456  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
14457  "FUNCTION", aggsig, NULL,
14458  agginfo->aggfn.dobj.namespace->dobj.name,
14459  agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
14460 
14461  free(aggsig);
14462  free(aggfullsig);
14463  free(aggsig_tag);
14464 
14465  PQclear(res);
14466 
14467  destroyPQExpBuffer(query);
14468  destroyPQExpBuffer(q);
14469  destroyPQExpBuffer(delq);
14470  destroyPQExpBuffer(details);
14471 }
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3861
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:330
@ PREPQUERY_DUMPAGG
Definition: pg_backup.h:66
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:278
static char * getFormattedOperatorName(const char *oproid)
Definition: pg_dump.c:13127
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:12188
static char * format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:14086
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const DumpableAcl *dacl)
Definition: pg_dump.c:15151
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:12165
static void dumpSecLabel(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:15277
bool * is_prepared
Definition: pg_backup.h:239

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), Archive::dopt, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), dumpSecLabel(), ExecuteSqlQueryForSingleRow(), ExecuteSqlStatement(), fmtId(), format_aggregate_signature(), format_function_arguments(), format_function_signature(), free, getFormattedOperatorName(), InvalidDumpId, Archive::is_prepared, pg_fatal, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), PREPQUERY_DUMPAGG, printfPQExpBuffer(), Archive::remoteVersion, res, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

◆ dumpAttrDef()

static void dumpAttrDef ( Archive fout,
const AttrDefInfo adinfo 
)
static

Definition at line 16739 of file pg_dump.c.

16740 {
16741  DumpOptions *dopt = fout->dopt;
16742  TableInfo *tbinfo = adinfo->adtable;
16743  int adnum = adinfo->adnum;
16744  PQExpBuffer q;
16745  PQExpBuffer delq;
16746  char *qualrelname;
16747  char *tag;
16748  char *foreign;
16749 
16750  /* Do nothing in data-only dump */
16751  if (dopt->dataOnly)
16752  return;
16753 
16754  /* Skip if not "separate"; it was dumped in the table's definition */
16755  if (!adinfo->separate)
16756  return;
16757 
16758  q = createPQExpBuffer();
16759  delq = createPQExpBuffer();
16760 
16761  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16762 
16763  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16764 
16766  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16767  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16768  adinfo->adef_expr);
16769 
16770  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16771  foreign, qualrelname,
16772  fmtId(tbinfo->attnames[adnum - 1]));
16773 
16774  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16775 
16776  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16777  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16778  ARCHIVE_OPTS(.tag = tag,
16779  .namespace = tbinfo->dobj.namespace->dobj.name,
16780  .owner = tbinfo->rolname,
16781  .description = "DEFAULT",
16782  .section = SECTION_PRE_DATA,
16783  .createStmt = q->data,
16784  .dropStmt = delq->data));
16785 
16786  free(tag);
16787  destroyPQExpBuffer(q);
16788  destroyPQExpBuffer(delq);
16789  free(qualrelname);
16790 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
DumpableObject dobj
Definition: pg_dump.h:380
char * adef_expr
Definition: pg_dump.h:383
TableInfo * adtable
Definition: pg_dump.h:381
bool separate
Definition: pg_dump.h:384
const char * rolname
Definition: pg_dump.h:287

References _attrDefInfo::adef_expr, _attrDefInfo::adnum, _attrDefInfo::adtable, appendPQExpBuffer(), ARCHIVE_OPTS, ArchiveEntry(), _tableInfo::attnames, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _tableInfo::dobj, _attrDefInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_DEFINITION, _dumpableObject::dumpId, fmtId(), fmtQualifiedDumpable, free, _dumpableObject::name, pg_strdup(), psprintf(), _tableInfo::relkind, _tableInfo::rolname, SECTION_PRE_DATA, and _attrDefInfo::separate.

Referenced by dumpDumpableObject().

◆ dumpBaseType()

static void dumpBaseType ( Archive fout,
const TypeInfo tyinfo 
)
static

Definition at line 11270 of file pg_dump.c.

11271 {
11272  DumpOptions *dopt = fout->dopt;
11274  PQExpBuffer delq = createPQExpBuffer();
11275  PQExpBuffer query = createPQExpBuffer();
11276  PGresult *res;
11277  char *qtypname;
11278  char *qualtypname;
11279  char *typlen;
11280  char *typinput;
11281  char *typoutput;
11282  char *typreceive;
11283  char *typsend;
11284  char *typmodin;
11285  char *typmodout;
11286  char *typanalyze;
11287  char *typsubscript;
11288  Oid typreceiveoid;
11289  Oid typsendoid;
11290  Oid typmodinoid;
11291  Oid typmodoutoid;
11292  Oid typanalyzeoid;
11293  Oid typsubscriptoid;
11294  char *typcategory;
11295  char *typispreferred;
11296  char *typdelim;
11297  char *typbyval;
11298  char *typalign;
11299  char *typstorage;
11300  char *typcollatable;
11301  char *typdefault;
11302  bool typdefault_is_literal = false;
11303 
11304  if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
11305  {
11306  /* Set up query for type-specific details */
11307  appendPQExpBufferStr(query,
11308  "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
11309  "SELECT typlen, "
11310  "typinput, typoutput, typreceive, typsend, "
11311  "typreceive::pg_catalog.oid AS typreceiveoid, "
11312  "typsend::pg_catalog.oid AS typsendoid, "
11313  "typanalyze, "
11314  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
11315  "typdelim, typbyval, typalign, typstorage, "
11316  "typmodin, typmodout, "
11317  "typmodin::pg_catalog.oid AS typmodinoid, "
11318  "typmodout::pg_catalog.oid AS typmodoutoid, "
11319  "typcategory, typispreferred, "
11320  "(typcollation <> 0) AS typcollatable, "
11321  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
11322 
11323  if (fout->remoteVersion >= 140000)
11324  appendPQExpBufferStr(query,
11325  "typsubscript, "
11326  "typsubscript::pg_catalog.oid AS typsubscriptoid ");
11327  else
11328  appendPQExpBufferStr(query,
11329  "'-' AS typsubscript, 0 AS typsubscriptoid ");
11330 
11331  appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
11332  "WHERE oid = $1");
11333 
11334  ExecuteSqlStatement(fout, query->data);
11335 
11336  fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
11337  }
11338 
11339  printfPQExpBuffer(query,
11340  "EXECUTE dumpBaseType('%u')",
11341  tyinfo->dobj.catId.oid);
11342 
11343  res = ExecuteSqlQueryForSingleRow(fout, query->data);
11344 
11345  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
11346  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
11347  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
11348  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
11349  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
11350  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
11351  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
11352  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
11353  typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
11354  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
11355  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
11356  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
11357  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
11358  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
11359  typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
11360  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
11361  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
11362  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
11363  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
11364  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
11365  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
11366  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
11367  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
11368  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
11369  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
11370  {
11371  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
11372  typdefault_is_literal = true; /* it needs quotes */
11373  }
11374  else
11375  typdefault = NULL;
11376 
11377  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11378  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11379 
11380  /*
11381  * The reason we include CASCADE is that the circular dependency between
11382  * the type and its I/O functions makes it impossible to drop the type any
11383  * other way.
11384  */
11385  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
11386 
11387  /*
11388  * We might already have a shell type, but setting pg_type_oid is
11389  * harmless, and in any case we'd better set the array type OID.
11390  */
11391  if (dopt->binary_upgrade)
11393  tyinfo->dobj.catId.oid,
11394  false, false);
11395 
11397  "CREATE TYPE %s (\n"
11398  " INTERNALLENGTH = %s",
11399  qualtypname,
11400  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
11401 
11402  /* regproc result is sufficiently quoted already */
11403  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
11404  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
11405  if (OidIsValid(typreceiveoid))
11406  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
11407  if (OidIsValid(typsendoid))
11408  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
11409  if (OidIsValid(typmodinoid))
11410  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
11411  if (OidIsValid(typmodoutoid))
11412  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
11413  if (OidIsValid(typanalyzeoid))
11414  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
11415 
11416  if (strcmp(typcollatable, "t") == 0)
11417  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
11418 
11419  if (typdefault != NULL)
11420  {
11421  appendPQExpBufferStr(q, ",\n DEFAULT = ");
11422  if (typdefault_is_literal)
11423  appendStringLiteralAH(q, typdefault, fout);
11424  else
11425  appendPQExpBufferStr(q, typdefault);
11426  }
11427 
11428  if (OidIsValid(typsubscriptoid))
11429  appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
11430 
11431  if (OidIsValid(tyinfo->typelem))
11432  appendPQExpBuffer(q, ",\n ELEMENT = %s",
11433  getFormattedTypeName(fout, tyinfo->typelem,
11434  zeroIsError));
11435 
11436  if (strcmp(typcategory, "U") != 0)
11437  {
11438  appendPQExpBufferStr(q, ",\n CATEGORY = ");
11439  appendStringLiteralAH(q, typcategory, fout);
11440  }
11441 
11442  if (strcmp(typispreferred, "t") == 0)
11443  appendPQExpBufferStr(q, ",\n PREFERRED = true");
11444 
11445  if (typdelim && strcmp(typdelim, ",") != 0)
11446  {
11447  appendPQExpBufferStr(q, ",\n DELIMITER = ");
11448  appendStringLiteralAH(q, typdelim, fout);
11449  }
11450 
11451  if (*typalign == TYPALIGN_CHAR)
11452  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
11453  else if (*typalign == TYPALIGN_SHORT)
11454  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
11455  else if (*typalign == TYPALIGN_INT)
11456  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
11457  else if (*typalign == TYPALIGN_DOUBLE)
11458  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
11459 
11460  if (*typstorage == TYPSTORAGE_PLAIN)
11461  appendPQExpBufferStr(q, ",\n STORAGE = plain");
11462  else if (*typstorage == TYPSTORAGE_EXTERNAL)
11463  appendPQExpBufferStr(q, ",\n STORAGE = external");
11464  else if (*typstorage == TYPSTORAGE_EXTENDED)
11465  appendPQExpBufferStr(q, ",\n STORAGE = extended");
11466  else if (*typstorage == TYPSTORAGE_MAIN)
11467  appendPQExpBufferStr(q, ",\n STORAGE = main");
11468 
11469  if (strcmp(typbyval, "t") == 0)
11470  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
11471 
11472  appendPQExpBufferStr(q, "\n);\n");
11473 
11474  if (dopt->binary_upgrade)
11476  "TYPE", qtypname,
11477  tyinfo->dobj.namespace->dobj.name);
11478 
11479  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11480  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11481  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11482  .namespace = tyinfo->dobj.namespace->dobj.name,
11483  .owner = tyinfo->rolname,
11484  .description = "TYPE",
11485  .section = SECTION_PRE_DATA,
11486  .createStmt = q->data,
11487  .dropStmt = delq->data));
11488 
11489  /* Dump Type Comments and Security Labels */
11490  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11491  dumpComment(fout, "TYPE", qtypname,
11492  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11493  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11494 
11495  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11496  dumpSecLabel(fout, "TYPE", qtypname,
11497  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11498  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11499 
11500  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11501  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11502  qtypname, NULL,
11503  tyinfo->dobj.namespace->dobj.name,
11504  tyinfo->rolname, &tyinfo->dacl);
11505 
11506  PQclear(res);
11507  destroyPQExpBuffer(q);
11508  destroyPQExpBuffer(delq);
11509  destroyPQExpBuffer(query);
11510  free(qtypname);
11511  free(qualtypname);
11512 }
@ PREPQUERY_DUMPBASETYPE
Definition: pg_backup.h:67
static const char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18767
char typalign
Definition: pg_type.h:176
DumpableAcl dacl
Definition: pg_dump.h:192
Oid typelem
Definition: pg_dump.h:201
const char * rolname
Definition: pg_dump.h:200

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), atooid, _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), binary_upgrade_set_type_oids_by_type_oid(), _dumpableObject::catId, createPQExpBuffer(), _typeInfo::dacl, PQExpBufferData::data, destroyPQExpBuffer(), _typeInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), _dumpableObject::dumpId, dumpSecLabel(), ExecuteSqlQueryForSingleRow(), ExecuteSqlStatement(), fmtId(), fmtQualifiedDumpable, free, getFormattedTypeName(), InvalidDumpId, Archive::is_prepared, _dumpableObject::name, CatalogId::oid, OidIsValid, pg_strdup(), PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), PREPQUERY_DUMPBASETYPE, printfPQExpBuffer(), Archive::remoteVersion, res, _typeInfo::rolname, SECTION_PRE_DATA, typalign, _typeInfo::typelem, and zeroIsError.

Referenced by dumpType().

◆ dumpCast()

static void dumpCast ( Archive fout,
const CastInfo cast 
)
static

Definition at line 12633 of file pg_dump.c.

12634 {
12635  DumpOptions *dopt = fout->dopt;
12636  PQExpBuffer defqry;
12637  PQExpBuffer delqry;
12638  PQExpBuffer labelq;
12639  PQExpBuffer castargs;
12640  FuncInfo *funcInfo = NULL;
12641  const char *sourceType;
12642  const char *targetType;
12643 
12644  /* Do nothing in data-only dump */
12645  if (dopt->dataOnly)
12646  return;
12647 
12648  /* Cannot dump if we don't have the cast function's info */
12649  if (OidIsValid(cast->castfunc))
12650  {
12651  funcInfo = findFuncByOid(cast->castfunc);
12652  if (funcInfo == NULL)
12653  pg_fatal("could not find function definition for function with OID %u",
12654  cast->castfunc);
12655  }
12656 
12657  defqry = createPQExpBuffer();
12658  delqry = createPQExpBuffer();
12659  labelq = createPQExpBuffer();
12660  castargs = createPQExpBuffer();
12661 
12662  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12663  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12664  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12665  sourceType, targetType);
12666 
12667  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12668  sourceType, targetType);
12669 
12670  switch (cast->castmethod)
12671  {
12672  case COERCION_METHOD_BINARY:
12673  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12674  break;
12675  case COERCION_METHOD_INOUT:
12676  appendPQExpBufferStr(defqry, "WITH INOUT");
12677  break;
12678  case COERCION_METHOD_FUNCTION:
12679  if (funcInfo)
12680  {
12681  char *fsig = format_function_signature(fout, funcInfo, true);
12682 
12683  /*
12684  * Always qualify the function name (format_function_signature
12685  * won't qualify it).
12686  */
12687  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12688  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12689  free(fsig);
12690  }
12691  else
12692  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12693  break;
12694  default:
12695  pg_log_warning("bogus value in pg_cast.castmethod field");
12696  }
12697 
12698  if (cast->castcontext == 'a')
12699  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12700  else if (cast->castcontext == 'i')
12701  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12702  appendPQExpBufferStr(defqry, ";\n");
12703 
12704  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12705  sourceType, targetType);
12706 
12707  appendPQExpBuffer(castargs, "(%s AS %s)",
12708  sourceType, targetType);
12709 
12710  if (dopt->binary_upgrade)
12711  binary_upgrade_extension_member(defqry, &cast->dobj,
12712  "CAST", castargs->data, NULL);
12713 
12714  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12715  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12716  ARCHIVE_OPTS(.tag = labelq->data,
12717  .description = "CAST",
12718  .section = SECTION_PRE_DATA,
12719  .createStmt = defqry->data,
12720  .dropStmt = delqry->data));
12721 
12722  /* Dump Cast Comments */
12723  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12724  dumpComment(fout, "CAST", castargs->data,
12725  NULL, "",
12726  cast->dobj.catId, 0, cast->dobj.dumpId);
12727 
12728  destroyPQExpBuffer(defqry);
12729  destroyPQExpBuffer(delqry);
12730  destroyPQExpBuffer(labelq);
12731  destroyPQExpBuffer(castargs);
12732 }
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:883
char castmethod
Definition: pg_dump.h:506
Oid casttarget
Definition: pg_dump.h:503
char castcontext
Definition: pg_dump.h:505
DumpableObject dobj
Definition: pg_dump.h:501
Oid castsource
Definition: pg_dump.h:502
Oid castfunc
Definition: pg_dump.h:504
DumpableObject dobj
Definition: pg_dump.h:224

References appendPQExpBuffer(), appendPQExpBufferStr(), ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _castInfo::castcontext, _castInfo::castfunc, _castInfo::castmethod, _castInfo::castsource, _castInfo::casttarget, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _funcInfo::dobj, _castInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, findFuncByOid(), fmtId(), format_function_signature(), free, getFormattedTypeName(), _dumpableObject::name, OidIsValid, pg_fatal, pg_log_warning, SECTION_PRE_DATA, and zeroAsNone.

Referenced by dumpDumpableObject().

◆ dumpCollation()

static void dumpCollation ( Archive fout,
const CollInfo collinfo 
)
static

Definition at line 13747 of file pg_dump.c.

13748 {
13749  DumpOptions *dopt = fout->dopt;
13750  PQExpBuffer query;
13751  PQExpBuffer q;
13752  PQExpBuffer delq;
13753  char *qcollname;
13754  PGresult *res;
13755  int i_collprovider;
13756  int i_collisdeterministic;
13757  int i_collcollate;
13758  int i_collctype;
13759  int i_colliculocale;
13760  int i_collicurules;
13761  const char *collprovider;
13762  const char *collcollate;
13763  const char *collctype;
13764  const char *colliculocale;
13765  const char *collicurules;
13766 
13767  /* Do nothing in data-only dump */
13768  if (dopt->dataOnly)
13769  return;
13770 
13771  query = createPQExpBuffer();
13772  q = createPQExpBuffer();
13773  delq = createPQExpBuffer();
13774 
13775  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13776 
13777  /* Get collation-specific details */
13778  appendPQExpBufferStr(query, "SELECT ");
13779 
13780  if (fout->remoteVersion >= 100000)
13781  appendPQExpBufferStr(query,
13782  "collprovider, "
13783  "collversion, ");
13784  else
13785  appendPQExpBufferStr(query,
13786  "'c' AS collprovider, "
13787  "NULL AS collversion, ");
13788 
13789  if (fout->remoteVersion >= 120000)
13790  appendPQExpBufferStr(query,
13791  "collisdeterministic, ");
13792  else
13793  appendPQExpBufferStr(query,
13794  "true AS collisdeterministic, ");
13795 
13796  if (fout->remoteVersion >= 150000)
13797  appendPQExpBufferStr(query,
13798  "colliculocale, ");
13799  else
13800  appendPQExpBufferStr(query,
13801  "NULL AS colliculocale, ");
13802 
13803  if (fout->remoteVersion >= 160000)
13804  appendPQExpBufferStr(query,
13805  "collicurules, ");
13806  else
13807  appendPQExpBufferStr(query,
13808  "NULL AS collicurules, ");
13809 
13810  appendPQExpBuffer(query,
13811  "collcollate, "
13812  "collctype "
13813  "FROM pg_catalog.pg_collation c "
13814  "WHERE c.oid = '%u'::pg_catalog.oid",
13815  collinfo->dobj.catId.oid);
13816 
13817  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13818 
13819  i_collprovider = PQfnumber(res, "collprovider");
13820  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13821  i_collcollate = PQfnumber(res, "collcollate");
13822  i_collctype = PQfnumber(res, "collctype");
13823  i_colliculocale = PQfnumber(res, "colliculocale");
13824  i_collicurules = PQfnumber(res, "collicurules");
13825 
13826  collprovider = PQgetvalue(res, 0, i_collprovider);
13827 
13828  if (!PQgetisnull(res, 0, i_collcollate))
13829  collcollate = PQgetvalue(res, 0, i_collcollate);
13830  else
13831  collcollate = NULL;
13832 
13833  if (!PQgetisnull(res, 0, i_collctype))
13834  collctype = PQgetvalue(res, 0, i_collctype);
13835  else
13836  collctype = NULL;
13837 
13838  /*
13839  * Before version 15, collcollate and collctype were of type NAME and
13840  * non-nullable. Treat empty strings as NULL for consistency.
13841  */
13842  if (fout->remoteVersion < 150000)
13843  {
13844  if (collcollate[0] == '\0')
13845  collcollate = NULL;
13846  if (collctype[0] == '\0')
13847  collctype = NULL;
13848  }
13849 
13850  if (!PQgetisnull(res, 0, i_colliculocale))
13851  colliculocale = PQgetvalue(res, 0, i_colliculocale);
13852  else
13853  colliculocale = NULL;
13854 
13855  if (!PQgetisnull(res, 0, i_collicurules))
13856  collicurules = PQgetvalue(res, 0, i_collicurules);
13857  else
13858  collicurules = NULL;
13859 
13860  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13861  fmtQualifiedDumpable(collinfo));
13862 
13863  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13864  fmtQualifiedDumpable(collinfo));
13865 
13866  appendPQExpBufferStr(q, "provider = ");
13867  if (collprovider[0] == 'c')
13868  appendPQExpBufferStr(q, "libc");
13869  else if (collprovider[0] == 'i')
13870  appendPQExpBufferStr(q, "icu");
13871  else if (collprovider[0] == 'd')
13872  /* to allow dumping pg_catalog; not accepted on input */
13873  appendPQExpBufferStr(q, "default");
13874  else
13875  pg_fatal("unrecognized collation provider: %s",
13876  collprovider);
13877 
13878  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13879  appendPQExpBufferStr(q, ", deterministic = false");
13880 
13881  if (collprovider[0] == 'd')
13882  {
13883  if (collcollate || collctype || colliculocale || collicurules)
13884  pg_log_warning("invalid collation \"%s\"", qcollname);
13885 
13886  /* no locale -- the default collation cannot be reloaded anyway */
13887  }
13888  else if (collprovider[0] == 'i')
13889  {
13890  if (fout->remoteVersion >= 150000)
13891  {
13892  if (collcollate || collctype || !colliculocale)
13893  pg_log_warning("invalid collation \"%s\"", qcollname);
13894 
13895  appendPQExpBufferStr(q, ", locale = ");
13896  appendStringLiteralAH(q, colliculocale ? colliculocale : "",
13897  fout);
13898  }
13899  else
13900  {
13901  if (!collcollate || !collctype || colliculocale ||
13902  strcmp(collcollate, collctype) != 0)
13903  pg_log_warning("invalid collation \"%s\"", qcollname);
13904 
13905  appendPQExpBufferStr(q, ", locale = ");
13906  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13907  }
13908 
13909  if (collicurules)
13910  {
13911  appendPQExpBufferStr(q, ", rules = ");
13912  appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
13913  }
13914  }
13915  else if (collprovider[0] == 'c')
13916  {
13917  if (colliculocale || collicurules || !collcollate || !collctype)
13918  pg_log_warning("invalid collation \"%s\"", qcollname);
13919 
13920  if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
13921  {
13922  appendPQExpBufferStr(q, ", locale = ");
13923  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13924  }
13925  else
13926  {
13927  appendPQExpBufferStr(q, ", lc_collate = ");
13928  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13929  appendPQExpBufferStr(q, ", lc_ctype = ");
13930  appendStringLiteralAH(q, collctype ? collctype : "", fout);
13931  }
13932  }
13933  else
13934  pg_fatal("unrecognized collation provider: %s", collprovider);
13935 
13936  /*
13937  * For binary upgrade, carry over the collation version. For normal
13938  * dump/restore, omit the version, so that it is computed upon restore.
13939  */
13940  if (dopt->binary_upgrade)
13941  {
13942  int i_collversion;
13943 
13944  i_collversion = PQfnumber(res, "collversion");
13945  if (!PQgetisnull(res, 0, i_collversion))
13946  {
13947  appendPQExpBufferStr(q, ", version = ");
13949  PQgetvalue(res, 0, i_collversion),
13950  fout);
13951  }
13952  }
13953 
13954  appendPQExpBufferStr(q, ");\n");
13955 
13956  if (dopt->binary_upgrade)
13957  binary_upgrade_extension_member(q, &collinfo->dobj,
13958  "COLLATION", qcollname,
13959  collinfo->dobj.namespace->dobj.name);
13960 
13961  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13962  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13963  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13964  .namespace = collinfo->dobj.namespace->dobj.name,
13965  .owner = collinfo->rolname,
13966  .description = "COLLATION",
13967  .section = SECTION_PRE_DATA,
13968  .createStmt = q->data,
13969  .dropStmt = delq->data));
13970 
13971  /* Dump Collation Comments */
13972  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13973  dumpComment(fout, "COLLATION", qcollname,
13974  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13975  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13976 
13977  PQclear(res);
13978 
13979  destroyPQExpBuffer(query);
13980  destroyPQExpBuffer(q);
13981  destroyPQExpBuffer(delq);
13982  free(qcollname);
13983 }
const char * rolname
Definition: pg_dump.h:271
DumpableObject dobj
Definition: pg_dump.h:270

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _collInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, ExecuteSqlQueryForSingleRow(), fmtId(), fmtQualifiedDumpable, free, _dumpableObject::name, CatalogId::oid, pg_fatal, pg_log_warning, pg_strdup(), PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), Archive::remoteVersion, res, _collInfo::rolname, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

◆ dumpComment()

static void dumpComment ( Archive fout,
const char *  type,
const char *  name,
const char *  namespace,
const char *  owner,
CatalogId  catalogId,
int  subid,
DumpId  dumpId 
)
inlinestatic

Definition at line 10209 of file pg_dump.c.

10213 {
10214  dumpCommentExtended(fout, type, name, namespace, owner,
10215  catalogId, subid, dumpId, NULL);
10216 }
static void dumpCommentExtended(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId, const char *initdb_comment)
Definition: pg_dump.c:10109

References dumpCommentExtended(), name, and type.

Referenced by dumpAccessMethod(), dumpAgg(), dumpBaseType(), dumpCast(), dumpCollation(), dumpCompositeType(), dumpConversion(), dumpDomain(), dumpEnumType(), dumpEventTrigger(), dumpExtension(), dumpForeignDataWrapper(), dumpForeignServer(), dumpFunc(), dumpIndex(), dumpLO(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpPolicy(), dumpProcLang(), dumpPublication(), dumpRangeType(), dumpRule(), dumpSequence(), dumpStatisticsExt(), dumpSubscription(), dumpTableConstraintComment(), dumpTransform(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), and dumpUndefinedType().

◆ dumpCommentExtended()

static void dumpCommentExtended ( Archive fout,
const char *  type,
const char *  name,
const char *  namespace,
const char *  owner,
CatalogId  catalogId,
int  subid,
DumpId  dumpId,
const char *  initdb_comment 
)
static

Definition at line 10109 of file pg_dump.c.

10114 {
10115  DumpOptions *dopt = fout->dopt;
10117  int ncomments;
10118 
10119  /* do nothing, if --no-comments is supplied */
10120  if (dopt->no_comments)
10121  return;
10122 
10123  /* Comments are schema not data ... except LO comments are data */
10124  if (strcmp(type, "LARGE OBJECT") != 0)
10125  {
10126  if (dopt->dataOnly)
10127  return;
10128  }
10129  else
10130  {
10131  /* We do dump LO comments in binary-upgrade mode */
10132  if (dopt->schemaOnly && !dopt->binary_upgrade)
10133  return;
10134  }
10135 
10136  /* Search for comments associated with catalogId, using table */
10137  ncomments = findComments(catalogId.tableoid, catalogId.oid,
10138  &comments);
10139 
10140  /* Is there one matching the subid? */
10141  while (ncomments > 0)
10142  {
10143  if (comments->objsubid == subid)
10144  break;
10145  comments++;
10146  ncomments--;
10147  }
10148 
10149  if (initdb_comment != NULL)
10150  {
10151  static CommentItem empty_comment = {.descr = ""};
10152 
10153  /*
10154  * initdb creates this object with a comment. Skip dumping the
10155  * initdb-provided comment, which would complicate matters for
10156  * non-superuser use of pg_dump. When the DBA has removed initdb's
10157  * comment, replicate that.
10158  */
10159  if (ncomments == 0)
10160  {
10161  comments = &empty_comment;
10162  ncomments = 1;
10163  }
10164  else if (strcmp(comments->descr, initdb_comment) == 0)
10165  ncomments = 0;
10166  }
10167 
10168  /* If a comment exists, build COMMENT ON statement */
10169  if (ncomments > 0)
10170  {
10171  PQExpBuffer query = createPQExpBuffer();
10173 
10174  appendPQExpBuffer(query, "COMMENT ON %s ", type);
10175  if (namespace && *namespace)
10176  appendPQExpBuffer(query, "%s.", fmtId(namespace));
10177  appendPQExpBuffer(query, "%s IS ", name);
10178  appendStringLiteralAH(query, comments->descr, fout);
10179  appendPQExpBufferStr(query, ";\n");
10180 
10181  appendPQExpBuffer(tag, "%s %s", type, name);
10182 
10183  /*
10184  * We mark comments as SECTION_NONE because they really belong in the
10185  * same section as their parent, whether that is pre-data or
10186  * post-data.
10187  */
10189  ARCHIVE_OPTS(.tag = tag->data,
10190  .namespace = namespace,
10191  .owner = owner,
10192  .description = "COMMENT",
10193  .section = SECTION_NONE,
10194  .createStmt = query->data,
10195  .deps = &dumpId,
10196  .nDeps = 1));
10197 
10198  destroyPQExpBuffer(query);
10199  destroyPQExpBuffer(tag);
10200  }
10201 }
static int findComments(Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:10323
int no_comments
Definition: pg_backup.h:178
bool schemaOnly
Definition: pg_backup.h:167

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, comments, createDumpId(), createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, CommentItem::descr, destroyPQExpBuffer(), Archive::dopt, findComments(), fmtId(), name, ncomments, nilCatalogId, _dumpOptions::no_comments, CommentItem::objsubid, CatalogId::oid, _dumpOptions::schemaOnly, SECTION_NONE, CatalogId::tableoid, and type.

Referenced by dumpComment(), and dumpNamespace().

◆ dumpCompositeType()

static void dumpCompositeType ( Archive fout,
const TypeInfo tyinfo 
)
static

Definition at line 11692 of file pg_dump.c.

11693 {
11694  DumpOptions *dopt = fout->dopt;
11696  PQExpBuffer dropped = createPQExpBuffer();
11697  PQExpBuffer delq = createPQExpBuffer();
11698  PQExpBuffer query = createPQExpBuffer();
11699  PGresult *res;
11700  char *qtypname;
11701  char *qualtypname;
11702  int ntups;
11703  int i_attname;
11704  int i_atttypdefn;
11705  int i_attlen;
11706  int i_attalign;
11707  int i_attisdropped;
11708  int i_attcollation;
11709  int i;
11710  int actual_atts;
11711 
11713  {
11714  /*
11715  * Set up query for type-specific details.
11716  *
11717  * Since we only want to dump COLLATE clauses for attributes whose
11718  * collation is different from their type's default, we use a CASE
11719  * here to suppress uninteresting attcollations cheaply. atttypid
11720  * will be 0 for dropped columns; collation does not matter for those.
11721  */
11722  appendPQExpBufferStr(query,
11723  "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
11724  "SELECT a.attname, a.attnum, "
11725  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11726  "a.attlen, a.attalign, a.attisdropped, "
11727  "CASE WHEN a.attcollation <> at.typcollation "
11728  "THEN a.attcollation ELSE 0 END AS attcollation "
11729  "FROM pg_catalog.pg_type ct "
11730  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11731  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11732  "WHERE ct.oid = $1 "
11733  "ORDER BY a.attnum");
11734 
11735  ExecuteSqlStatement(fout, query->data);
11736 
11738  }
11739 
11740  printfPQExpBuffer(query,
11741  "EXECUTE dumpCompositeType('%u')",
11742  tyinfo->dobj.catId.oid);
11743 
11744  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11745 
11746  ntups = PQntuples(res);
11747 
11748  i_attname = PQfnumber(res, "attname");
11749  i_atttypdefn = PQfnumber(res, "atttypdefn");
11750  i_attlen = PQfnumber(res, "attlen");
11751  i_attalign = PQfnumber(res, "attalign");
11752  i_attisdropped = PQfnumber(res, "attisdropped");
11753  i_attcollation = PQfnumber(res, "attcollation");
11754 
11755  if (dopt->binary_upgrade)
11756  {
11758  tyinfo->dobj.catId.oid,
11759  false, false);
11760  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11761  }
11762 
11763  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11764  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11765 
11766  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11767  qualtypname);
11768 
11769  actual_atts = 0;
11770  for (i = 0; i < ntups; i++)
11771  {
11772  char *attname;
11773  char *atttypdefn;
11774  char *attlen;
11775  char *attalign;
11776  bool attisdropped;
11777  Oid attcollation;
11778 
11779  attname = PQgetvalue(res, i, i_attname);
11780  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11781  attlen = PQgetvalue(res, i, i_attlen);
11782  attalign = PQgetvalue(res, i, i_attalign);
11783  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11784  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11785 
11786  if (attisdropped && !dopt->binary_upgrade)
11787  continue;
11788 
11789  /* Format properly if not first attr */
11790  if (actual_atts++ > 0)
11791  appendPQExpBufferChar(q, ',');
11792  appendPQExpBufferStr(q, "\n\t");
11793 
11794  if (!attisdropped)
11795  {
11796  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11797 
11798  /* Add collation if not default for the column type */
11799  if (OidIsValid(attcollation))
11800  {
11801  CollInfo *coll;
11802 
11803  coll = findCollationByOid(attcollation);
11804  if (coll)
11805  appendPQExpBuffer(q, " COLLATE %s",
11806  fmtQualifiedDumpable(coll));
11807  }
11808  }
11809  else
11810  {
11811  /*
11812  * This is a dropped attribute and we're in binary_upgrade mode.
11813  * Insert a placeholder for it in the CREATE TYPE command, and set
11814  * length and alignment with direct UPDATE to the catalogs
11815  * afterwards. See similar code in dumpTableSchema().
11816  */
11817  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11818 
11819  /* stash separately for insertion after the CREATE TYPE */
11820  appendPQExpBufferStr(dropped,
11821  "\n-- For binary upgrade, recreate dropped column.\n");
11822  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11823  "SET attlen = %s, "
11824  "attalign = '%s', attbyval = false\n"
11825  "WHERE attname = ", attlen, attalign);
11826  appendStringLiteralAH(dropped, attname, fout);
11827  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11828  appendStringLiteralAH(dropped, qualtypname, fout);
11829  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11830 
11831  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11832  qualtypname);
11833  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11834  fmtId(attname));
11835  }
11836  }
11837  appendPQExpBufferStr(q, "\n);\n");
11838  appendPQExpBufferStr(q, dropped->data);
11839 
11840  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11841 
11842  if (dopt->binary_upgrade)
11844  "TYPE", qtypname,
11845  tyinfo->dobj.namespace->dobj.name);
11846 
11847  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11848  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11849  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11850  .namespace = tyinfo->dobj.namespace->dobj.name,
11851  .owner = tyinfo->rolname,
11852  .description = "TYPE",
11853  .section = SECTION_PRE_DATA,
11854  .createStmt = q->data,
11855  .dropStmt = delq->data));
11856 
11857 
11858  /* Dump Type Comments and Security Labels */
11859  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11860  dumpComment(fout, "TYPE", qtypname,
11861  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11862  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11863 
11864  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11865  dumpSecLabel(fout, "TYPE", qtypname,
11866  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11867  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11868 
11869  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11870  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11871  qtypname, NULL,
11872  tyinfo->dobj.namespace->dobj.name,
11873  tyinfo->rolname, &tyinfo->dacl);
11874 
11875  /* Dump any per-column comments */
11876  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11877  dumpCompositeTypeColComments(fout, tyinfo, res);
11878 
11879  PQclear(res);
11880  destroyPQExpBuffer(q);
11881  destroyPQExpBuffer(dropped);
11882  destroyPQExpBuffer(delq);
11883  destroyPQExpBuffer(query);
11884  free(qtypname);
11885  free(qualtypname);
11886 }
NameData attname
Definition: pg_attribute.h:41
char attalign
Definition: pg_attribute.h:109
int16 attlen
Definition: pg_attribute.h:59
@ PREPQUERY_DUMPCOMPOSITETYPE
Definition: pg_backup.h:68
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:5301
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, PGresult *res)
Definition: pg_dump.c:11898
Oid typrelid
Definition: pg_dump.h:202

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), atooid, attalign, attlen, attname, _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), binary_upgrade_set_pg_class_oids(), binary_upgrade_set_type_oids_by_type_oid(), _dumpableObject::catId, createPQExpBuffer(), _typeInfo::dacl, PQExpBufferData::data, destroyPQExpBuffer(), _typeInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), dumpCompositeTypeColComments(), _dumpableObject::dumpId, dumpSecLabel(), ExecuteSqlQuery(), ExecuteSqlStatement(), findCollationByOid(), fmtId(), fmtQualifiedDumpable, free, i, InvalidDumpId, Archive::is_prepared, _dumpableObject::name, CatalogId::oid, OidIsValid, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), PREPQUERY_DUMPCOMPOSITETYPE, printfPQExpBuffer(), res, _typeInfo::rolname, SECTION_PRE_DATA, and _typeInfo::typrelid.

Referenced by dumpType().

◆ dumpCompositeTypeColComments()

static void dumpCompositeTypeColComments ( Archive fout,
const TypeInfo tyinfo,
PGresult res 
)
static

Definition at line 11898 of file pg_dump.c.

11900 {
11902  int ncomments;
11903  PQExpBuffer query;
11904  PQExpBuffer target;
11905  int i;
11906  int ntups;
11907  int i_attname;
11908  int i_attnum;
11909  int i_attisdropped;
11910 
11911  /* do nothing, if --no-comments is supplied */
11912  if (fout->dopt->no_comments)
11913  return;
11914 
11915  /* Search for comments associated with type's pg_class OID */
11916  ncomments = findComments(RelationRelationId, tyinfo->typrelid,
11917  &comments);
11918 
11919  /* If no comments exist, we're done */
11920  if (ncomments <= 0)
11921  return;
11922 
11923  /* Build COMMENT ON statements */
11924  query = createPQExpBuffer();
11925  target = createPQExpBuffer();
11926 
11927  ntups = PQntuples(res);
11928  i_attnum = PQfnumber(res, "attnum");
11929  i_attname = PQfnumber(res, "attname");
11930  i_attisdropped = PQfnumber(res, "attisdropped");
11931  while (ncomments > 0)
11932  {
11933  const char *attname;
11934 
11935  attname = NULL;
11936  for (i = 0; i < ntups; i++)
11937  {
11938  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
11939  PQgetvalue(res, i, i_attisdropped)[0] != 't')
11940  {
11941  attname = PQgetvalue(res, i, i_attname);
11942  break;
11943  }
11944  }
11945  if (attname) /* just in case we don't find it */
11946  {
11947  const char *descr = comments->descr;
11948 
11949  resetPQExpBuffer(target);
11950  appendPQExpBuffer(target, "COLUMN %s.",
11951  fmtId(tyinfo->dobj.name));
11953 
11954  resetPQExpBuffer(query);
11955  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11956  fmtQualifiedDumpable(tyinfo));
11957  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11958  appendStringLiteralAH(query, descr, fout);
11959  appendPQExpBufferStr(query, ";\n");
11960 
11962  ARCHIVE_OPTS(.tag = target->data,
11963  .namespace = tyinfo->dobj.namespace->dobj.name,
11964  .owner = tyinfo->rolname,
11965  .description = "COMMENT",
11966  .section = SECTION_NONE,
11967  .createStmt = query->data,
11968  .deps = &(tyinfo->dobj.dumpId),
11969  .nDeps = 1));
11970  }
11971 
11972  comments++;
11973  ncomments--;
11974  }
11975 
11976  destroyPQExpBuffer(query);
11977  destroyPQExpBuffer(target);
11978 }
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), attname, comments, createDumpId(), createPQExpBuffer(), PQExpBufferData::data, CommentItem::descr, destroyPQExpBuffer(), _typeInfo::dobj, Archive::dopt, _dumpableObject::dumpId, findComments(), fmtId(), fmtQualifiedDumpable, i, _dumpableObject::name, ncomments, nilCatalogId, _dumpOptions::no_comments, CommentItem::objsubid, PQfnumber(), PQgetvalue(), PQntuples(), res, resetPQExpBuffer(), _typeInfo::rolname, SECTION_NONE, and _typeInfo::typrelid.

Referenced by dumpCompositeType().

◆ dumpConstraint()

static void dumpConstraint ( Archive fout,
const ConstraintInfo coninfo 
)
static

Definition at line 17087 of file pg_dump.c.

17088 {
17089  DumpOptions *dopt = fout->dopt;
17090  TableInfo *tbinfo = coninfo->contable;
17091  PQExpBuffer q;
17092  PQExpBuffer delq;
17093  char *tag = NULL;
17094  char *foreign;
17095 
17096  /* Do nothing in data-only dump */
17097  if (dopt->dataOnly)
17098  return;
17099 
17100  q = createPQExpBuffer();
17101  delq = createPQExpBuffer();
17102 
17103  foreign = tbinfo &&
17104  tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
17105 
17106  if (coninfo->contype == 'p' ||
17107  coninfo->contype == 'u' ||
17108  coninfo->contype == 'x')
17109  {
17110  /* Index-related constraint */
17111  IndxInfo *indxinfo;
17112  int k;
17113 
17114  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
17115 
17116  if (indxinfo == NULL)
17117  pg_fatal("missing index for constraint \"%s\"",
17118  coninfo->dobj.name);
17119 
17120  if (dopt->binary_upgrade)
17122  indxinfo->dobj.catId.oid, true);
17123 
17124  appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
17125  fmtQualifiedDumpable(tbinfo));
17126  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
17127  fmtId(coninfo->dobj.name));
17128 
17129  if (coninfo->condef)
17130  {
17131  /* pg_get_constraintdef should have provided everything */
17132  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
17133  }
17134  else
17135  {
17137  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
17138 
17139  /*
17140  * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
17141  * indexes. Being able to create this was fixed, but we need to
17142  * make the index distinct in order to be able to restore the
17143  * dump.
17144  */
17145  if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
17146  appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
17147  appendPQExpBufferStr(q, " (");
17148  for (k = 0; k < indxinfo->indnkeyattrs; k++)
17149  {
17150  int indkey = (int) indxinfo->indkeys[k];
17151  const char *attname;
17152 
17153  if (indkey == InvalidAttrNumber)
17154  break;
17155  attname = getAttrName(indkey, tbinfo);
17156 
17157  appendPQExpBuffer(q, "%s%s",
17158  (k == 0) ? "" : ", ",
17159  fmtId(attname));
17160  }
17161  if (coninfo->conwithoutoverlaps)
17162  appendPQExpBufferStr(q, " WITHOUT OVERLAPS");
17163 
17164  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
17165  appendPQExpBufferStr(q, ") INCLUDE (");
17166 
17167  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
17168  {
17169  int indkey = (int) indxinfo->indkeys[k];
17170  const char *attname;
17171 
17172  if (indkey == InvalidAttrNumber)
17173  break;
17174  attname = getAttrName(indkey, tbinfo);
17175 
17176  appendPQExpBuffer(q, "%s%s",
17177  (k == indxinfo->indnkeyattrs) ? "" : ", ",
17178  fmtId(attname));
17179  }
17180 
17181  appendPQExpBufferChar(q, ')');
17182 
17183  if (nonemptyReloptions(indxinfo->indreloptions))
17184  {
17185  appendPQExpBufferStr(q, " WITH (");
17186  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
17187  appendPQExpBufferChar(q, ')');
17188  }
17189 
17190  if (coninfo->condeferrable)
17191  {
17192  appendPQExpBufferStr(q, " DEFERRABLE");
17193  if (coninfo->condeferred)
17194  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
17195  }
17196 
17197  appendPQExpBufferStr(q, ";\n");
17198  }
17199 
17200  /*
17201  * Append ALTER TABLE commands as needed to set properties that we
17202  * only have ALTER TABLE syntax for. Keep this in sync with the
17203  * similar code in dumpIndex!
17204  */
17205 
17206  /* Drop any not-null constraints that were added to support the PK */
17207  if (coninfo->contype == 'p')
17208  for (int i = 0; i < tbinfo->numatts; i++)
17209  if (tbinfo->notnull_throwaway[i])
17210  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s DROP CONSTRAINT %s;",
17211  fmtQualifiedDumpable(tbinfo),
17212  tbinfo->notnull_constrs[i]);
17213 
17214  /* If the index is clustered, we need to record that. */
17215  if (indxinfo->indisclustered)
17216  {
17217  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
17218  fmtQualifiedDumpable(tbinfo));
17219  /* index name is not qualified in this syntax */
17220  appendPQExpBuffer(q, " ON %s;\n",
17221  fmtId(indxinfo->dobj.name));
17222  }
17223 
17224  /* If the index defines identity, we need to record that. */
17225  if (indxinfo->indisreplident)
17226  {
17227  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
17228  fmtQualifiedDumpable(tbinfo));
17229  /* index name is not qualified in this syntax */
17230  appendPQExpBuffer(q, " INDEX %s;\n",
17231  fmtId(indxinfo->dobj.name));
17232  }
17233 
17234  /* Indexes can depend on extensions */
17235  append_depends_on_extension(fout, q, &indxinfo->dobj,
17236  "pg_catalog.pg_class", "INDEX",
17237  fmtQualifiedDumpable(indxinfo));
17238 
17239  appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
17240  fmtQualifiedDumpable(tbinfo));
17241  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17242  fmtId(coninfo->dobj.name));
17243 
17244  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17245 
17246  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17247  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17248  ARCHIVE_OPTS(.tag = tag,
17249  .namespace = tbinfo->dobj.namespace->dobj.name,
17250  .tablespace = indxinfo->tablespace,
17251  .owner = tbinfo->rolname,
17252  .description = "CONSTRAINT",
17253  .section = SECTION_POST_DATA,
17254  .createStmt = q->data,
17255  .dropStmt = delq->data));
17256  }
17257  else if (coninfo->contype == 'f')
17258  {
17259  char *only;
17260 
17261  /*
17262  * Foreign keys on partitioned tables are always declared as
17263  * inheriting to partitions; for all other cases, emit them as
17264  * applying ONLY directly to the named table, because that's how they
17265  * work for regular inherited tables.
17266  */
17267  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
17268 
17269  /*
17270  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
17271  * current table data is not processed
17272  */
17273  appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
17274  only, fmtQualifiedDumpable(tbinfo));
17275  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17276  fmtId(coninfo->dobj.name),
17277  coninfo->condef);
17278 
17279  appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
17280  only, fmtQualifiedDumpable(tbinfo));
17281  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17282  fmtId(coninfo->dobj.name));
17283 
17284  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17285 
17286  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17287  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17288  ARCHIVE_OPTS(.tag = tag,
17289  .namespace = tbinfo->dobj.namespace->dobj.name,
17290  .owner = tbinfo->rolname,
17291  .description = "FK CONSTRAINT",
17292  .section = SECTION_POST_DATA,
17293  .createStmt = q->data,
17294  .dropStmt = delq->data));
17295  }
17296  else if (coninfo->contype == 'c' && tbinfo)
17297  {
17298  /* CHECK constraint on a table */
17299 
17300  /* Ignore if not to be dumped separately, or if it was inherited */
17301  if (coninfo->separate && coninfo->conislocal)
17302  {
17303  /* not ONLY since we want it to propagate to children */
17304  appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
17305  fmtQualifiedDumpable(tbinfo));
17306  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17307  fmtId(coninfo->dobj.name),
17308  coninfo->condef);
17309 
17310  appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
17311  fmtQualifiedDumpable(tbinfo));
17312  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17313  fmtId(coninfo->dobj.name));
17314 
17315  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17316 
17317  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17318  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17319  ARCHIVE_OPTS(.tag = tag,
17320  .namespace = tbinfo->dobj.namespace->dobj.name,
17321  .owner = tbinfo->rolname,
17322  .description = "CHECK CONSTRAINT",
17323  .section = SECTION_POST_DATA,
17324  .createStmt = q->data,
17325  .dropStmt = delq->data));
17326  }
17327  }
17328  else if (coninfo->contype == 'c' && tbinfo == NULL)
17329  {
17330  /* CHECK constraint on a domain */
17331  TypeInfo *tyinfo = coninfo->condomain;
17332 
17333  /* Ignore if not to be dumped separately */
17334  if (coninfo->separate)
17335  {
17336  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
17337  fmtQualifiedDumpable(tyinfo));
17338  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17339  fmtId(coninfo->dobj.name),
17340  coninfo->condef);
17341 
17342  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
17343  fmtQualifiedDumpable(tyinfo));
17344  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17345  fmtId(coninfo->dobj.name));
17346 
17347  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
17348 
17349  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17350  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17351  ARCHIVE_OPTS(.tag = tag,
17352  .namespace = tyinfo->dobj.namespace->dobj.name,
17353  .owner = tyinfo->rolname,
17354  .description = "CHECK CONSTRAINT",
17355  .section = SECTION_POST_DATA,
17356  .createStmt = q->data,
17357  .dropStmt = delq->data));
17358  }
17359  }
17360  else
17361  {
17362  pg_fatal("unrecognized constraint type: %c",
17363  coninfo->contype);
17364  }
17365 
17366  /* Dump Constraint Comments --- only works for table constraints */
17367  if (tbinfo && coninfo->separate &&
17368  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17369  dumpTableConstraintComment(fout, coninfo);
17370 
17371  free(tag);
17372  destroyPQExpBuffer(q);
17373  destroyPQExpBuffer(delq);
17374 }
#define InvalidAttrNumber
Definition: attnum.h:23
@ SECTION_POST_DATA
Definition: pg_backup.h:60
static void append_depends_on_extension(Archive *fout, PQExpBuffer create, const DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
Definition: pg_dump.c:5128
static const char * getAttrName(int attrnum, const TableInfo *tblInfo)
Definition: pg_dump.c:16800
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
Definition: pg_dump.c:18864
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
Definition: pg_dump.c:17384
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18852
TypeInfo * condomain
Definition: pg_dump.h:475
TableInfo * contable
Definition: pg_dump.h:474
bool condeferred
Definition: pg_dump.h:481
bool conislocal
Definition: pg_dump.h:484
DumpableObject dobj
Definition: pg_dump.h:473
DumpId conindex
Definition: pg_dump.h:479
bool condeferrable
Definition: pg_dump.h:480
char * condef
Definition: pg_dump.h:477
bool conwithoutoverlaps
Definition: pg_dump.h:482
bool indisreplident
Definition: pg_dump.h:408
int indnkeyattrs
Definition: pg_dump.h:403
int indnattrs
Definition: pg_dump.h:404
Oid * indkeys
Definition: pg_dump.h:405
char * indreloptions
Definition: pg_dump.h:400
bool indisclustered
Definition: pg_dump.h:407
char * tablespace
Definition: pg_dump.h:399
bool indnullsnotdistinct
Definition: pg_dump.h:409
DumpableObject dobj
Definition: pg_dump.h:396
char ** notnull_constrs
Definition: pg_dump.h:349
bool * notnull_throwaway
Definition: pg_dump.h:354

References append_depends_on_extension(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendReloptionsArrayAH(), ARCHIVE_OPTS, ArchiveEntry(), attname, _dumpOptions::binary_upgrade, binary_upgrade_set_pg_class_oids(), _dumpableObject::catId, _constraintInfo::condef, _constraintInfo::condeferrable, _constraintInfo::condeferred, _constraintInfo::condomain, _constraintInfo::conindex, _constraintInfo::conislocal, _constraintInfo::contable, _constraintInfo::contype, _constraintInfo::conwithoutoverlaps, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _typeInfo::dobj, _tableInfo::dobj, _indxInfo::dobj, _constraintInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, _dumpableObject::dumpId, dumpTableConstraintComment(), findObjectByDumpId(), fmtId(), fmtQualifiedDumpable, free, getAttrName(), i, if(), _indxInfo::indisclustered, _indxInfo::indisreplident, _indxInfo::indkeys, _indxInfo::indnattrs, _indxInfo::indnkeyattrs, _indxInfo::indnullsnotdistinct, _indxInfo::indreloptions, InvalidAttrNumber, _dumpableObject::name, nonemptyReloptions(), _tableInfo::notnull_constrs, _tableInfo::notnull_throwaway, _tableInfo::numatts, CatalogId::oid, pg_fatal, psprintf(), _tableInfo::relkind, _typeInfo::rolname, _tableInfo::rolname, SECTION_POST_DATA, _constraintInfo::separate, and _indxInfo::tablespace.

Referenced by dumpDumpableObject().

◆ dumpConversion()

static void dumpConversion ( Archive fout,
const ConvInfo convinfo 
)
static

Definition at line 13990 of file pg_dump.c.

13991 {
13992  DumpOptions *dopt = fout->dopt;
13993  PQExpBuffer query;
13994  PQExpBuffer q;
13995  PQExpBuffer delq;
13996  char *qconvname;
13997  PGresult *res;
13998  int i_conforencoding;
13999  int i_contoencoding;
14000  int i_conproc;
14001  int i_condefault;
14002  const char *conforencoding;
14003  const char *contoencoding;
14004  const char *conproc;
14005  bool condefault;
14006 
14007  /* Do nothing in data-only dump */
14008  if (dopt->dataOnly)
14009  return;
14010 
14011  query = createPQExpBuffer();
14012  q = createPQExpBuffer();
14013  delq = createPQExpBuffer();
14014 
14015  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
14016 
14017  /* Get conversion-specific details */
14018  appendPQExpBuffer(query, "SELECT "
14019  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
14020  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
14021  "conproc, condefault "
14022  "FROM pg_catalog.pg_conversion c "
14023  "WHERE c.oid = '%u'::pg_catalog.oid",
14024  convinfo->dobj.catId.oid);
14025 
14026  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14027 
14028  i_conforencoding = PQfnumber(res, "conforencoding");
14029  i_contoencoding = PQfnumber(res, "contoencoding");
14030  i_conproc = PQfnumber(res, "conproc");
14031  i_condefault = PQfnumber(res, "condefault");
14032 
14033  conforencoding = PQgetvalue(res, 0, i_conforencoding);
14034  contoencoding = PQgetvalue(res, 0, i_contoencoding);
14035  conproc = PQgetvalue(res, 0, i_conproc);
14036  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
14037 
14038  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
14039  fmtQualifiedDumpable(convinfo));
14040 
14041  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
14042  (condefault) ? "DEFAULT " : "",
14043  fmtQualifiedDumpable(convinfo));
14044  appendStringLiteralAH(q, conforencoding, fout);
14045  appendPQExpBufferStr(q, " TO ");
14046  appendStringLiteralAH(q, contoencoding, fout);
14047  /* regproc output is already sufficiently quoted */
14048  appendPQExpBuffer(q, " FROM %s;\n", conproc);
14049 
14050  if (dopt->binary_upgrade)
14051  binary_upgrade_extension_member(q, &convinfo->dobj,
14052  "CONVERSION", qconvname,
14053  convinfo->dobj.namespace->dobj.name);
14054 
14055  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14056  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
14057  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
14058  .namespace = convinfo->dobj.namespace->dobj.name,
14059  .owner = convinfo->rolname,
14060  .description = "CONVERSION",
14061  .section = SECTION_PRE_DATA,
14062  .createStmt = q->data,
14063  .dropStmt = delq->data));
14064 
14065  /* Dump Conversion Comments */
14066  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14067  dumpComment(fout, "CONVERSION", qconvname,
14068  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
14069  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
14070 
14071  PQclear(res);
14072 
14073  destroyPQExpBuffer(query);
14074  destroyPQExpBuffer(q);
14075  destroyPQExpBuffer(delq);
14076  free(qconvname);
14077 }
DumpableObject dobj
Definition: pg_dump.h:276
const char * rolname
Definition: pg_dump.h:277

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _convInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, ExecuteSqlQueryForSingleRow(), fmtId(), fmtQualifiedDumpable, free, _dumpableObject::name, CatalogId::oid, pg_strdup(), PQclear(), PQfnumber(), PQgetvalue(), res, _convInfo::rolname, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

◆ dumpDatabase()

static void dumpDatabase ( Archive fout)
static

Definition at line 2970 of file pg_dump.c.

2971 {
2972  DumpOptions *dopt = fout->dopt;
2973  PQExpBuffer dbQry = createPQExpBuffer();
2974  PQExpBuffer delQry = createPQExpBuffer();
2975  PQExpBuffer creaQry = createPQExpBuffer();
2976  PQExpBuffer labelq = createPQExpBuffer();
2977  PGconn *conn = GetConnection(fout);
2978  PGresult *res;
2979  int i_tableoid,
2980  i_oid,
2981  i_datname,
2982  i_datdba,
2983  i_encoding,
2984  i_datlocprovider,
2985  i_collate,
2986  i_ctype,
2987  i_daticulocale,
2988  i_daticurules,
2989  i_frozenxid,
2990  i_minmxid,
2991  i_datacl,
2992  i_acldefault,
2993  i_datistemplate,
2994  i_datconnlimit,
2995  i_datcollversion,
2996  i_tablespace;
2997  CatalogId dbCatId;
2998  DumpId dbDumpId;
2999  DumpableAcl dbdacl;
3000  const char *datname,
3001  *dba,
3002  *encoding,
3003  *datlocprovider,
3004  *collate,
3005  *ctype,
3006  *iculocale,
3007  *icurules,
3008  *datistemplate,
3009  *datconnlimit,
3010  *tablespace;
3011  uint32 frozenxid,
3012  minmxid;
3013  char *qdatname;
3014 
3015  pg_log_info("saving database definition");
3016 
3017  /*
3018  * Fetch the database-level properties for this database.
3019  */
3020  appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, "
3021  "datdba, "
3022  "pg_encoding_to_char(encoding) AS encoding, "
3023  "datcollate, datctype, datfrozenxid, "
3024  "datacl, acldefault('d', datdba) AS acldefault, "
3025  "datistemplate, datconnlimit, ");
3026  if (fout->remoteVersion >= 90300)
3027  appendPQExpBufferStr(dbQry, "datminmxid, ");
3028  else
3029  appendPQExpBufferStr(dbQry, "0 AS datminmxid, ");
3030  if (fout->remoteVersion >= 150000)
3031  appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale, datcollversion, ");
3032  else
3033  appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS daticulocale, NULL AS datcollversion, ");
3034  if (fout->remoteVersion >= 160000)
3035  appendPQExpBufferStr(dbQry, "daticurules, ");
3036  else
3037  appendPQExpBufferStr(dbQry, "NULL AS daticurules, ");
3038  appendPQExpBufferStr(dbQry,
3039  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
3040  "shobj_description(oid, 'pg_database') AS description "
3041  "FROM pg_database "
3042  "WHERE datname = current_database()");
3043 
3044  res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
3045 
3046  i_tableoid = PQfnumber(res, "tableoid");
3047  i_oid = PQfnumber(res, "oid");
3048  i_datname = PQfnumber(res, "datname");
3049  i_datdba = PQfnumber(res, "datdba");
3050  i_encoding = PQfnumber(res, "encoding");
3051  i_datlocprovider = PQfnumber(res, "datlocprovider");
3052  i_collate = PQfnumber(res, "datcollate");
3053  i_ctype = PQfnumber(res, "datctype");
3054  i_daticulocale = PQfnumber(res, "daticulocale");
3055  i_daticurules = PQfnumber(res, "daticurules");
3056  i_frozenxid = PQfnumber(res, "datfrozenxid");
3057  i_minmxid = PQfnumber(res, "datminmxid");
3058  i_datacl = PQfnumber(res, "datacl");
3059  i_acldefault = PQfnumber(res, "acldefault");
3060  i_datistemplate = PQfnumber(res, "datistemplate");
3061  i_datconnlimit = PQfnumber(res, "datconnlimit");
3062  i_datcollversion = PQfnumber(res, "datcollversion");
3063  i_tablespace = PQfnumber(res, "tablespace");
3064 
3065  dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
3066  dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
3067  datname = PQgetvalue(res, 0, i_datname);
3068  dba = getRoleName(PQgetvalue(res, 0, i_datdba));
3069  encoding = PQgetvalue(res, 0, i_encoding);
3070  datlocprovider = PQgetvalue(res, 0, i_datlocprovider);
3071  collate = PQgetvalue(res, 0, i_collate);
3072  ctype = PQgetvalue(res, 0, i_ctype);
3073  if (!PQgetisnull(res, 0, i_daticulocale))
3074  iculocale = PQgetvalue(res, 0, i_daticulocale);
3075  else
3076  iculocale = NULL;
3077  if (!PQgetisnull(res, 0, i_daticurules))
3078  icurules = PQgetvalue(res, 0, i_daticurules);
3079  else
3080  icurules = NULL;
3081  frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
3082  minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
3083  dbdacl.acl = PQgetvalue(res, 0, i_datacl);
3084  dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault);
3085  datistemplate = PQgetvalue(res, 0, i_datistemplate);
3086  datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
3087  tablespace = PQgetvalue(res, 0, i_tablespace);
3088 
3089  qdatname = pg_strdup(fmtId(datname));
3090 
3091  /*
3092  * Prepare the CREATE DATABASE command. We must specify OID (if we want
3093  * to preserve that), as well as the encoding, locale, and tablespace
3094  * since those can't be altered later. Other DB properties are left to
3095  * the DATABASE PROPERTIES entry, so that they can be applied after
3096  * reconnecting to the target DB.
3097  */
3098  if (dopt->binary_upgrade)
3099  {
3100  appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
3101  qdatname, dbCatId.oid);
3102  }
3103  else
3104  {
3105  appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
3106  qdatname);
3107  }
3108  if (strlen(encoding) > 0)
3109  {
3110  appendPQExpBufferStr(creaQry, " ENCODING = ");
3111  appendStringLiteralAH(creaQry, encoding, fout);
3112  }
3113 
3114  appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = ");
3115  if (datlocprovider[0] == 'c')
3116  appendPQExpBufferStr(creaQry, "libc");
3117  else if (datlocprovider[0] == 'i')
3118  appendPQExpBufferStr(creaQry, "icu");
3119  else
3120  pg_fatal("unrecognized locale provider: %s",
3121  datlocprovider);
3122 
3123  if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
3124  {
3125  appendPQExpBufferStr(creaQry, " LOCALE = ");
3126  appendStringLiteralAH(creaQry, collate, fout);
3127  }
3128  else
3129  {
3130  if (strlen(collate) > 0)
3131  {
3132  appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
3133  appendStringLiteralAH(creaQry, collate, fout);
3134  }
3135  if (strlen(ctype) > 0)
3136  {
3137  appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
3138  appendStringLiteralAH(creaQry, ctype, fout);
3139  }
3140  }
3141  if (iculocale)
3142  {
3143  appendPQExpBufferStr(creaQry, " ICU_LOCALE = ");
3144  appendStringLiteralAH(creaQry, iculocale, fout);
3145  }
3146  if (icurules)
3147  {
3148  appendPQExpBufferStr(creaQry, " ICU_RULES = ");
3149  appendStringLiteralAH(creaQry, icurules, fout);
3150  }
3151 
3152  /*
3153  * For binary upgrade, carry over the collation version. For normal
3154  * dump/restore, omit the version, so that it is computed upon restore.
3155  */
3156  if (dopt->binary_upgrade)
3157  {
3158  if (!PQgetisnull(res, 0, i_datcollversion))
3159  {
3160  appendPQExpBufferStr(creaQry, " COLLATION_VERSION = ");
3161  appendStringLiteralAH(creaQry,
3162  PQgetvalue(res, 0, i_datcollversion),
3163  fout);
3164  }
3165  }
3166 
3167  /*
3168  * Note: looking at dopt->outputNoTablespaces here is completely the wrong
3169  * thing; the decision whether to specify a tablespace should be left till
3170  * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
3171  * label the DATABASE entry with the tablespace and let the normal
3172  * tablespace selection logic work ... but CREATE DATABASE doesn't pay
3173  * attention to default_tablespace, so that won't work.
3174  */
3175  if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
3176  !dopt->outputNoTablespaces)
3177  appendPQExpBuffer(creaQry, " TABLESPACE = %s",
3178  fmtId(tablespace));
3179  appendPQExpBufferStr(creaQry, ";\n");
3180 
3181  appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
3182  qdatname);
3183 
3184  dbDumpId = createDumpId();
3185 
3186  ArchiveEntry(fout,
3187  dbCatId, /* catalog ID */
3188  dbDumpId, /* dump ID */
3189  ARCHIVE_OPTS(.tag = datname,
3190  .owner = dba,
3191  .description = "DATABASE",
3192  .section = SECTION_PRE_DATA,
3193  .createStmt = creaQry->data,
3194  .dropStmt = delQry->data));
3195 
3196  /* Compute correct tag for archive entry */
3197  appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
3198 
3199  /* Dump DB comment if any */
3200  {
3201  /*
3202  * 8.2 and up keep comments on shared objects in a shared table, so we
3203  * cannot use the dumpComment() code used for other database objects.
3204  * Be careful that the ArchiveEntry parameters match that function.
3205  */
3206  char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
3207 
3208  if (comment && *comment && !dopt->no_comments)
3209  {
3210  resetPQExpBuffer(dbQry);
3211 
3212  /*
3213  * Generates warning when loaded into a differently-named
3214  * database.
3215  */
3216  appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
3217  appendStringLiteralAH(dbQry, comment, fout);
3218  appendPQExpBufferStr(dbQry, ";\n");
3219 
3221  ARCHIVE_OPTS(.tag = labelq->data,
3222  .owner = dba,
3223  .description = "COMMENT",
3224  .section = SECTION_NONE,
3225  .createStmt = dbQry->data,
3226  .deps = &dbDumpId,
3227  .nDeps = 1));
3228  }
3229  }
3230 
3231  /* Dump DB security label, if enabled */
3232  if (!dopt->no_security_labels)
3233  {
3234  PGresult *shres;
3235  PQExpBuffer seclabelQry;
3236 
3237  seclabelQry = createPQExpBuffer();
3238 
3239  buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
3240  shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
3241  resetPQExpBuffer(seclabelQry);
3242  emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
3243  if (seclabelQry->len > 0)
3245  ARCHIVE_OPTS(.tag = labelq->data,
3246  .owner = dba,
3247  .description = "SECURITY LABEL",
3248  .section = SECTION_NONE,
3249  .createStmt = seclabelQry->data,
3250  .deps = &dbDumpId,
3251  .nDeps = 1));
3252  destroyPQExpBuffer(seclabelQry);
3253  PQclear(shres);
3254  }
3255 
3256  /*
3257  * Dump ACL if any. Note that we do not support initial privileges
3258  * (pg_init_privs) on databases.
3259  */
3260  dbdacl.privtype = 0;
3261  dbdacl.initprivs = NULL;
3262 
3263  dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE",
3264  qdatname, NULL, NULL,
3265  dba, &dbdacl);
3266 
3267  /*
3268  * Now construct a DATABASE PROPERTIES archive entry to restore any
3269  * non-default database-level properties. (The reason this must be
3270  * separate is that we cannot put any additional commands into the TOC
3271  * entry that has CREATE DATABASE. pg_restore would execute such a group
3272  * in an implicit transaction block, and the backend won't allow CREATE
3273  * DATABASE in that context.)
3274  */
3275  resetPQExpBuffer(creaQry);
3276  resetPQExpBuffer(delQry);
3277 
3278  if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
3279  appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
3280  qdatname, datconnlimit);
3281 
3282  if (strcmp(datistemplate, "t") == 0)
3283  {
3284  appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
3285  qdatname);
3286 
3287  /*
3288  * The backend won't accept DROP DATABASE on a template database. We
3289  * can deal with that by removing the template marking before the DROP
3290  * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
3291  * since no such command is currently supported, fake it with a direct
3292  * UPDATE on pg_database.
3293  */
3294  appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
3295  "SET datistemplate = false WHERE datname = ");
3296  appendStringLiteralAH(delQry, datname, fout);
3297  appendPQExpBufferStr(delQry, ";\n");
3298  }
3299 
3300  /*
3301  * We do not restore pg_database.dathasloginevt because it is set
3302  * automatically on login event trigger creation.
3303  */
3304 
3305  /* Add database-specific SET options */
3306  dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
3307 
3308  /*
3309  * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
3310  * entry, too, for lack of a better place.
3311  */
3312  if (dopt->binary_upgrade)
3313  {
3314  appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
3315  appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
3316  "SET datfrozenxid = '%u', datminmxid = '%u'\n"
3317  "WHERE datname = ",
3318  frozenxid, minmxid);
3319  appendStringLiteralAH(creaQry, datname, fout);
3320  appendPQExpBufferStr(creaQry, ";\n");
3321  }
3322 
3323  if (creaQry->len > 0)
3325  ARCHIVE_OPTS(.tag = datname,
3326  .owner = dba,
3327  .description = "DATABASE PROPERTIES",
3328  .section = SECTION_PRE_DATA,
3329  .createStmt = creaQry->data,
3330  .dropStmt = delQry->data,
3331  .deps = &dbDumpId));
3332 
3333  /*
3334  * pg_largeobject comes from the old system intact, so set its
3335  * relfrozenxids, relminmxids and relfilenode.
3336  */
3337  if (dopt->binary_upgrade)
3338  {
3339  PGresult *lo_res;
3340  PQExpBuffer loFrozenQry = createPQExpBuffer();
3341  PQExpBuffer loOutQry = createPQExpBuffer();
3342  PQExpBuffer loHorizonQry = createPQExpBuffer();
3343  int ii_relfrozenxid,
3344  ii_relfilenode,
3345  ii_oid,
3346  ii_relminmxid;
3347 
3348  /*
3349  * pg_largeobject
3350  */
3351  if (fout->remoteVersion >= 90300)
3352  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n"
3353  "FROM pg_catalog.pg_class\n"
3354  "WHERE oid IN (%u, %u);\n",
3355  LargeObjectRelationId, LargeObjectLOidPNIndexId);
3356  else
3357  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid, relfilenode, oid\n"
3358  "FROM pg_catalog.pg_class\n"
3359  "WHERE oid IN (%u, %u);\n",
3360  LargeObjectRelationId, LargeObjectLOidPNIndexId);
3361 
3362  lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
3363 
3364  ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
3365  ii_relminmxid = PQfnumber(lo_res, "relminmxid");
3366  ii_relfilenode = PQfnumber(lo_res, "relfilenode");
3367  ii_oid = PQfnumber(lo_res, "oid");
3368 
3369  appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
3370  appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n");
3371  for (int i = 0; i < PQntuples(lo_res); ++i)
3372  {
3373  Oid oid;
3374  RelFileNumber relfilenumber;
3375 
3376  appendPQExpBuffer(loHorizonQry, "UPDATE pg_catalog.pg_class\n"
3377  "SET relfrozenxid = '%u', relminmxid = '%u'\n"
3378  "WHERE oid = %u;\n",
3379  atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)),
3380  atooid(PQgetvalue(lo_res, i, ii_relminmxid)),
3381  atooid(PQgetvalue(lo_res, i, ii_oid)));
3382 
3383  oid = atooid(PQgetvalue(lo_res, i, ii_oid));
3384  relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode));
3385 
3386  if (oid == LargeObjectRelationId)
3387  appendPQExpBuffer(loOutQry,
3388  "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
3389  relfilenumber);
3390  else if (oid == LargeObjectLOidPNIndexId)
3391  appendPQExpBuffer(loOutQry,
3392  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
3393  relfilenumber);
3394  }
3395 
3396  appendPQExpBufferStr(loOutQry,
3397  "TRUNCATE pg_catalog.pg_largeobject;\n");
3398  appendPQExpBufferStr(loOutQry, loHorizonQry->data);
3399 
3401  ARCHIVE_OPTS(.tag = "pg_largeobject",
3402  .description = "pg_largeobject",
3403  .section = SECTION_PRE_DATA,
3404  .createStmt = loOutQry->data));
3405 
3406  PQclear(lo_res);
3407 
3408  destroyPQExpBuffer(loFrozenQry);
3409  destroyPQExpBuffer(loHorizonQry);
3410  destroyPQExpBuffer(loOutQry);
3411  }
3412 
3413  PQclear(res);
3414 
3415  free(qdatname);
3416  destroyPQExpBuffer(dbQry);
3417  destroyPQExpBuffer(delQry);
3418  destroyPQExpBuffer(creaQry);
3419  destroyPQExpBuffer(labelq);
3420 }
unsigned int uint32
Definition: c.h:495
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:177
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
Definition: dumputils.c:636
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
Definition: dumputils.c:654
#define comment
Definition: indent_codes.h:49
#define pg_log_info(...)
Definition: logging.h:124
char datlocprovider
Definition: pg_database.h:44
NameData datname
Definition: pg_database.h:35
int32 encoding
Definition: pg_database.h:41
bool datistemplate
Definition: pg_database.h:47
int32 datconnlimit
Definition: pg_database.h:59
static const char * getRoleName(const char *roleoid_str)
Definition: pg_dump.c:9909
static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf, const char *dbname, Oid dboid)
Definition: pg_dump.c:3427
char * tablespace
Definition: pgbench.c:216
PGconn * conn
Definition: streamutil.c:54
int no_security_labels
Definition: pg_backup.h:179
int outputNoTablespaces
Definition: pg_backup.h:187
const char * description

References _dumpableAcl::acl, _dumpableAcl::acldefault, appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), atooid, _dumpOptions::binary_upgrade, buildShSecLabelQuery(), comment, conn, createDumpId(), createPQExpBuffer(), PQExpBufferData::data, datconnlimit, datistemplate, datlocprovider, datname, description, destroyPQExpBuffer(), Archive::dopt, dumpACL(), dumpDatabaseConfig(), emitShSecLabels(), encoding, ExecuteSqlQuery(), ExecuteSqlQueryForSingleRow(), fmtId(), free, GetConnection(), getRoleName(), i, _dumpableAcl::initprivs, InvalidDumpId, PQExpBufferData::len, nilCatalogId, _dumpOptions::no_comments, _dumpOptions::no_security_labels, CatalogId::oid, _dumpOptions::outputNoTablespaces, pg_fatal, pg_log_info, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), PQntuples(), _dumpableAcl::privtype, Archive::remoteVersion, res, resetPQExpBuffer(), SECTION_NONE, SECTION_PRE_DATA, CatalogId::tableoid, and tablespace.

Referenced by main().

◆ dumpDatabaseConfig()

static void dumpDatabaseConfig ( Archive AH,
PQExpBuffer  outbuf,
const char *  dbname,
Oid  dboid 
)
static

Definition at line 3427 of file pg_dump.c.

3429 {
3430  PGconn *conn = GetConnection(AH);
3432  PGresult *res;
3433 
3434  /* First collect database-specific options */
3435  printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3436  "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3437  dboid);
3438 
3439  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3440 
3441  for (int i = 0; i < PQntuples(res); i++)
3443  "DATABASE", dbname, NULL, NULL,
3444  outbuf);
3445 
3446  PQclear(res);
3447 
3448  /* Now look for role-and-database-specific options */
3449  printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3450  "FROM pg_db_role_setting s, pg_roles r "
3451  "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3452  dboid);
3453 
3454  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3455 
3456  for (int i = 0; i < PQntuples(res); i++)
3458  "ROLE", PQgetvalue(res, i, 0),
3459  "DATABASE", dbname,
3460  outbuf);
3461 
3462  PQclear(res);
3463 
3465 }
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
Definition: dumputils.c:822
static char * buf
Definition: pg_test_fsync.c:73
char * dbname
Definition: streamutil.c:51

References buf, conn, createPQExpBuffer(), dbname, destroyPQExpBuffer(), ExecuteSqlQuery(), GetConnection(), i, makeAlterConfigCommand(), PGRES_TUPLES_OK, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), and res.

Referenced by dumpDatabase().

◆ dumpDefaultACL()

static void dumpDefaultACL ( Archive fout,
const DefaultACLInfo daclinfo 
)
static

Definition at line 15064 of file pg_dump.c.

15065 {
15066  DumpOptions *dopt = fout->dopt;
15067  PQExpBuffer q;
15068  PQExpBuffer tag;
15069  const char *type;
15070 
15071  /* Do nothing in data-only dump, or if we're skipping ACLs */
15072  if (dopt->dataOnly || dopt->aclsSkip)
15073  return;
15074 
15075  q = createPQExpBuffer();
15076  tag = createPQExpBuffer();
15077 
15078  switch (daclinfo->defaclobjtype)
15079  {
15080  case DEFACLOBJ_RELATION:
15081  type = "TABLES";
15082  break;
15083  case DEFACLOBJ_SEQUENCE:
15084  type = "SEQUENCES";
15085  break;
15086  case DEFACLOBJ_FUNCTION:
15087  type = "FUNCTIONS";
15088  break;
15089  case DEFACLOBJ_TYPE:
15090  type = "TYPES";
15091  break;
15092  case DEFACLOBJ_NAMESPACE:
15093  type = "SCHEMAS";
15094  break;
15095  default:
15096  /* shouldn't get here */
15097  pg_fatal("unrecognized object type in default privileges: %d",
15098  (int) daclinfo->defaclobjtype);
15099  type = ""; /* keep compiler quiet */
15100  }
15101 
15102  appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
15103 
15104  /* build the actual command(s) for this tuple */
15106  daclinfo->dobj.namespace != NULL ?
15107  daclinfo->dobj.namespace->dobj.name : NULL,
15108  daclinfo->dacl.acl,
15109  daclinfo->dacl.acldefault,
15110  daclinfo->defaclrole,
15111  fout->remoteVersion,