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 MAX_BLOBS_PER_ARCHIVE_ENTRY   1000
 
#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 *tag, 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 SimpleStringList extension_exclude_patterns = {NULL, NULL}
 
static SimpleOidList extension_exclude_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 164 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 176 of file pg_dump.c.

◆ MAX_BLOBS_PER_ARCHIVE_ENTRY

#define MAX_BLOBS_PER_ARCHIVE_ENTRY   1000

Definition at line 171 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 18436 of file pg_dump.c.

18438 {
18439  DumpableObject *preDataBound = boundaryObjs + 0;
18440  DumpableObject *postDataBound = boundaryObjs + 1;
18441  int i;
18442 
18443  for (i = 0; i < numObjs; i++)
18444  {
18445  DumpableObject *dobj = dobjs[i];
18446 
18447  /*
18448  * The classification of object types here must match the SECTION_xxx
18449  * values assigned during subsequent ArchiveEntry calls!
18450  */
18451  switch (dobj->objType)
18452  {
18453  case DO_NAMESPACE:
18454  case DO_EXTENSION:
18455  case DO_TYPE:
18456  case DO_SHELL_TYPE:
18457  case DO_FUNC:
18458  case DO_AGG:
18459  case DO_OPERATOR:
18460  case DO_ACCESS_METHOD:
18461  case DO_OPCLASS:
18462  case DO_OPFAMILY:
18463  case DO_COLLATION:
18464  case DO_CONVERSION:
18465  case DO_TABLE:
18466  case DO_TABLE_ATTACH:
18467  case DO_ATTRDEF:
18468  case DO_PROCLANG:
18469  case DO_CAST:
18470  case DO_DUMMY_TYPE:
18471  case DO_TSPARSER:
18472  case DO_TSDICT:
18473  case DO_TSTEMPLATE:
18474  case DO_TSCONFIG:
18475  case DO_FDW:
18476  case DO_FOREIGN_SERVER:
18477  case DO_TRANSFORM:
18478  /* Pre-data objects: must come before the pre-data boundary */
18479  addObjectDependency(preDataBound, dobj->dumpId);
18480  break;
18481  case DO_TABLE_DATA:
18482  case DO_SEQUENCE_SET:
18483  case DO_LARGE_OBJECT:
18484  case DO_LARGE_OBJECT_DATA:
18485  /* Data objects: must come between the boundaries */
18486  addObjectDependency(dobj, preDataBound->dumpId);
18487  addObjectDependency(postDataBound, dobj->dumpId);
18488  break;
18489  case DO_INDEX:
18490  case DO_INDEX_ATTACH:
18491  case DO_STATSEXT:
18492  case DO_REFRESH_MATVIEW:
18493  case DO_TRIGGER:
18494  case DO_EVENT_TRIGGER:
18495  case DO_DEFAULT_ACL:
18496  case DO_POLICY:
18497  case DO_PUBLICATION:
18498  case DO_PUBLICATION_REL:
18500  case DO_SUBSCRIPTION:
18501  case DO_SUBSCRIPTION_REL:
18502  /* Post-data objects: must come after the post-data boundary */
18503  addObjectDependency(dobj, postDataBound->dumpId);
18504  break;
18505  case DO_RULE:
18506  /* Rules are post-data, but only if dumped separately */
18507  if (((RuleInfo *) dobj)->separate)
18508  addObjectDependency(dobj, postDataBound->dumpId);
18509  break;
18510  case DO_CONSTRAINT:
18511  case DO_FK_CONSTRAINT:
18512  /* Constraints are post-data, but only if dumped separately */
18513  if (((ConstraintInfo *) dobj)->separate)
18514  addObjectDependency(dobj, postDataBound->dumpId);
18515  break;
18516  case DO_PRE_DATA_BOUNDARY:
18517  /* nothing to do */
18518  break;
18519  case DO_POST_DATA_BOUNDARY:
18520  /* must come after the pre-data boundary */
18521  addObjectDependency(dobj, preDataBound->dumpId);
18522  break;
18523  }
18524  }
18525 }
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:806
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 7877 of file pg_dump.c.

7878 {
7879  SimplePtrListCell *cell;
7880 
7881  Assert(dobj->objType == DO_FK_CONSTRAINT);
7882 
7883  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7884  {
7885  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7886 
7887  addObjectDependency(dobj, attach->dobj.dumpId);
7888 
7889  if (attach->partitionIdx->partattaches.head != NULL)
7890  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7891  }
7892 }
#define Assert(condition)
Definition: c.h:858
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7877
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54
IndxInfo * partitionIdx
Definition: pg_dump.h:416
DumpableObject dobj
Definition: pg_dump.h:414
SimplePtrList partattaches
Definition: pg_dump.h:406

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 5220 of file pg_dump.c.

5226 {
5227  if (dobj->depends_on_ext)
5228  {
5229  char *nm;
5230  PGresult *res;
5231  PQExpBuffer query;
5232  int ntups;
5233  int i_extname;
5234  int i;
5235 
5236  /* dodge fmtId() non-reentrancy */
5237  nm = pg_strdup(objname);
5238 
5239  query = createPQExpBuffer();
5240  appendPQExpBuffer(query,
5241  "SELECT e.extname "
5242  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
5243  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
5244  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
5245  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
5246  catalog,
5247  dobj->catId.oid);
5248  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5249  ntups = PQntuples(res);
5250  i_extname = PQfnumber(res, "extname");
5251  for (i = 0; i < ntups; i++)
5252  {
5253  appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
5254  keyword, nm,
5255  fmtId(PQgetvalue(res, i, i_extname)));
5256  }
5257 
5258  PQclear(res);
5259  destroyPQExpBuffer(query);
5260  pg_free(nm);
5261  }
5262 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
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:102
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 18753 of file pg_dump.c.

18755 {
18756  bool res;
18757 
18758  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18759  fout->std_strings);
18760  if (!res)
18761  pg_log_warning("could not parse %s array", "reloptions");
18762 }
#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:230
int encoding
Definition: pg_backup.h:229

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 5509 of file pg_dump.c.

5514 {
5515  DumpableObject *extobj = NULL;
5516  int i;
5517 
5518  if (!dobj->ext_member)
5519  return;
5520 
5521  /*
5522  * Find the parent extension. We could avoid this search if we wanted to
5523  * add a link field to DumpableObject, but the space costs of that would
5524  * be considerable. We assume that member objects could only have a
5525  * direct dependency on their own extension, not any others.
5526  */
5527  for (i = 0; i < dobj->nDeps; i++)
5528  {
5529  extobj = findObjectByDumpId(dobj->dependencies[i]);
5530  if (extobj && extobj->objType == DO_EXTENSION)
5531  break;
5532  extobj = NULL;
5533  }
5534  if (extobj == NULL)
5535  pg_fatal("could not find parent extension for %s %s",
5536  objtype, objname);
5537 
5538  appendPQExpBufferStr(upgrade_buffer,
5539  "\n-- For binary upgrade, handle extension membership the hard way\n");
5540  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
5541  fmtId(extobj->name),
5542  objtype);
5543  if (objnamespace && *objnamespace)
5544  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
5545  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
5546 }
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:753
#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 5393 of file pg_dump.c.

5396 {
5397  PQExpBuffer upgrade_query = createPQExpBuffer();
5398  PGresult *upgrade_res;
5399  RelFileNumber relfilenumber;
5400  Oid toast_oid;
5401  RelFileNumber toast_relfilenumber;
5402  char relkind;
5403  Oid toast_index_oid;
5404  RelFileNumber toast_index_relfilenumber;
5405 
5406  /*
5407  * Preserve the OID and relfilenumber of the table, table's index, table's
5408  * toast table and toast table's index if any.
5409  *
5410  * One complexity is that the current table definition might not require
5411  * the creation of a TOAST table, but the old database might have a TOAST
5412  * table that was created earlier, before some wide columns were dropped.
5413  * By setting the TOAST oid we force creation of the TOAST heap and index
5414  * by the new backend, so we can copy the files during binary upgrade
5415  * without worrying about this case.
5416  */
5417  appendPQExpBuffer(upgrade_query,
5418  "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
5419  "FROM pg_catalog.pg_class c LEFT JOIN "
5420  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5421  "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5422  "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5423  "WHERE c.oid = '%u'::pg_catalog.oid;",
5424  pg_class_oid);
5425 
5426  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5427 
5428  relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
5429 
5430  relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5431  PQfnumber(upgrade_res, "relfilenode")));
5432  toast_oid = atooid(PQgetvalue(upgrade_res, 0,
5433  PQfnumber(upgrade_res, "reltoastrelid")));
5434  toast_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5435  PQfnumber(upgrade_res, "toast_relfilenode")));
5436  toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
5437  PQfnumber(upgrade_res, "indexrelid")));
5438  toast_index_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5439  PQfnumber(upgrade_res, "toast_index_relfilenode")));
5440 
5441  appendPQExpBufferStr(upgrade_buffer,
5442  "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
5443 
5444  if (!is_index)
5445  {
5446  appendPQExpBuffer(upgrade_buffer,
5447  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5448  pg_class_oid);
5449 
5450  /*
5451  * Not every relation has storage. Also, in a pre-v12 database,
5452  * partitioned tables have a relfilenumber, which should not be
5453  * preserved when upgrading.
5454  */
5455  if (RelFileNumberIsValid(relfilenumber) && relkind != RELKIND_PARTITIONED_TABLE)
5456  appendPQExpBuffer(upgrade_buffer,
5457  "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
5458  relfilenumber);
5459 
5460  /*
5461  * In a pre-v12 database, partitioned tables might be marked as having
5462  * toast tables, but we should ignore them if so.
5463  */
5464  if (OidIsValid(toast_oid) &&
5465  relkind != RELKIND_PARTITIONED_TABLE)
5466  {
5467  appendPQExpBuffer(upgrade_buffer,
5468  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
5469  toast_oid);
5470  appendPQExpBuffer(upgrade_buffer,
5471  "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
5472  toast_relfilenumber);
5473 
5474  /* every toast table has an index */
5475  appendPQExpBuffer(upgrade_buffer,
5476  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5477  toast_index_oid);
5478  appendPQExpBuffer(upgrade_buffer,
5479  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5480  toast_index_relfilenumber);
5481  }
5482  }
5483  else
5484  {
5485  /* Preserve the OID and relfilenumber of the index */
5486  appendPQExpBuffer(upgrade_buffer,
5487  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5488  pg_class_oid);
5489  appendPQExpBuffer(upgrade_buffer,
5490  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5491  relfilenumber);
5492  }
5493 
5494  PQclear(upgrade_res);
5495 
5496  appendPQExpBufferChar(upgrade_buffer, '\n');
5497 
5498  destroyPQExpBuffer(upgrade_query);
5499 }
#define OidIsValid(objectId)
Definition: c.h:775
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 5381 of file pg_dump.c.

5384 {
5385  Oid pg_type_oid = tbinfo->reltype;
5386 
5387  if (OidIsValid(pg_type_oid))
5388  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5389  pg_type_oid, false, false);
5390 }
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:5296
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 5296 of file pg_dump.c.

5301 {
5302  PQExpBuffer upgrade_query = createPQExpBuffer();
5303  PGresult *res;
5304  Oid pg_type_array_oid;
5305  Oid pg_type_multirange_oid;
5306  Oid pg_type_multirange_array_oid;
5307 
5308  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
5309  appendPQExpBuffer(upgrade_buffer,
5310  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5311  pg_type_oid);
5312 
5313  appendPQExpBuffer(upgrade_query,
5314  "SELECT typarray "
5315  "FROM pg_catalog.pg_type "
5316  "WHERE oid = '%u'::pg_catalog.oid;",
5317  pg_type_oid);
5318 
5319  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5320 
5321  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5322 
5323  PQclear(res);
5324 
5325  if (!OidIsValid(pg_type_array_oid) && force_array_type)
5326  pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5327 
5328  if (OidIsValid(pg_type_array_oid))
5329  {
5330  appendPQExpBufferStr(upgrade_buffer,
5331  "\n-- For binary upgrade, must preserve pg_type array oid\n");
5332  appendPQExpBuffer(upgrade_buffer,
5333  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5334  pg_type_array_oid);
5335  }
5336 
5337  /*
5338  * Pre-set the multirange type oid and its own array type oid.
5339  */
5340  if (include_multirange_type)
5341  {
5342  if (fout->remoteVersion >= 140000)
5343  {
5344  printfPQExpBuffer(upgrade_query,
5345  "SELECT t.oid, t.typarray "
5346  "FROM pg_catalog.pg_type t "
5347  "JOIN pg_catalog.pg_range r "
5348  "ON t.oid = r.rngmultitypid "
5349  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
5350  pg_type_oid);
5351 
5352  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5353 
5354  pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
5355  pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5356 
5357  PQclear(res);
5358  }
5359  else
5360  {
5361  pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5362  pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5363  }
5364 
5365  appendPQExpBufferStr(upgrade_buffer,
5366  "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
5367  appendPQExpBuffer(upgrade_buffer,
5368  "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5369  pg_type_multirange_oid);
5370  appendPQExpBufferStr(upgrade_buffer,
5371  "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
5372  appendPQExpBuffer(upgrade_buffer,
5373  "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5374  pg_type_multirange_array_oid);
5375  }
5376 
5377  destroyPQExpBuffer(upgrade_query);
5378 }
static Oid get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
Definition: pg_dump.c:5265
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
int remoteVersion
Definition: pg_backup.h:219

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 18552 of file pg_dump.c.

18553 {
18554  ArchiveHandle *AH = (ArchiveHandle *) fout;
18555  TocEntry *te;
18556 
18557  /* Scan all TOC entries in the archive */
18558  for (te = AH->toc->next; te != AH->toc; te = te->next)
18559  {
18560  DumpableObject *dobj;
18561  DumpId *dependencies;
18562  int nDeps;
18563  int allocDeps;
18564 
18565  /* No need to process entries that will not be dumped */
18566  if (te->reqs == 0)
18567  continue;
18568  /* Ignore entries that already have "special" dependencies */
18569  if (te->nDeps > 0)
18570  continue;
18571  /* Otherwise, look up the item's original DumpableObject, if any */
18572  dobj = findObjectByDumpId(te->dumpId);
18573  if (dobj == NULL)
18574  continue;
18575  /* No work if it has no dependencies */
18576  if (dobj->nDeps <= 0)
18577  continue;
18578  /* Set up work array */
18579  allocDeps = 64;
18580  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18581  nDeps = 0;
18582  /* Recursively find all dumpable dependencies */
18583  findDumpableDependencies(AH, dobj,
18584  &dependencies, &nDeps, &allocDeps);
18585  /* And save 'em ... */
18586  if (nDeps > 0)
18587  {
18588  dependencies = (DumpId *) pg_realloc(dependencies,
18589  nDeps * sizeof(DumpId));
18590  te->dependencies = dependencies;
18591  te->nDeps = nDeps;
18592  }
18593  else
18594  free(dependencies);
18595  }
18596 }
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:270
static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:18600
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 2841 of file pg_dump.c.

2842 {
2843  PQExpBuffer query;
2844  PGresult *res;
2845  int ntups,
2846  i;
2847  int i_classid,
2848  i_objid,
2849  i_refobjid;
2850 
2851  /* No Mat Views before 9.3. */
2852  if (fout->remoteVersion < 90300)
2853  return;
2854 
2855  query = createPQExpBuffer();
2856 
2857  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2858  "( "
2859  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2860  "FROM pg_depend d1 "
2861  "JOIN pg_class c1 ON c1.oid = d1.objid "
2862  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2863  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2864  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2865  "AND d2.objid = r1.oid "
2866  "AND d2.refobjid <> d1.objid "
2867  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2868  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2869  CppAsString2(RELKIND_VIEW) ") "
2870  "WHERE d1.classid = 'pg_class'::regclass "
2871  "UNION "
2872  "SELECT w.objid, d3.refobjid, c3.relkind "
2873  "FROM w "
2874  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2875  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2876  "AND d3.objid = r3.oid "
2877  "AND d3.refobjid <> w.refobjid "
2878  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2879  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2880  CppAsString2(RELKIND_VIEW) ") "
2881  ") "
2882  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2883  "FROM w "
2884  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2885 
2886  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2887 
2888  ntups = PQntuples(res);
2889 
2890  i_classid = PQfnumber(res, "classid");
2891  i_objid = PQfnumber(res, "objid");
2892  i_refobjid = PQfnumber(res, "refobjid");
2893 
2894  for (i = 0; i < ntups; i++)
2895  {
2896  CatalogId objId;
2897  CatalogId refobjId;
2898  DumpableObject *dobj;
2899  DumpableObject *refdobj;
2900  TableInfo *tbinfo;
2901  TableInfo *reftbinfo;
2902 
2903  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2904  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2905  refobjId.tableoid = objId.tableoid;
2906  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2907 
2908  dobj = findObjectByCatalogId(objId);
2909  if (dobj == NULL)
2910  continue;
2911 
2912  Assert(dobj->objType == DO_TABLE);
2913  tbinfo = (TableInfo *) dobj;
2914  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2915  dobj = (DumpableObject *) tbinfo->dataObj;
2916  if (dobj == NULL)
2917  continue;
2918  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2919 
2920  refdobj = findObjectByCatalogId(refobjId);
2921  if (refdobj == NULL)
2922  continue;
2923 
2924  Assert(refdobj->objType == DO_TABLE);
2925  reftbinfo = (TableInfo *) refdobj;
2926  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2927  refdobj = (DumpableObject *) reftbinfo->dataObj;
2928  if (refdobj == NULL)
2929  continue;
2930  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2931 
2932  addObjectDependency(dobj, refdobj->dumpId);
2933 
2934  if (!reftbinfo->relispopulated)
2935  tbinfo->relispopulated = false;
2936  }
2937 
2938  PQclear(res);
2939 
2940  destroyPQExpBuffer(query);
2941 }
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:766
#define CppAsString2(x)
Definition: c.h:327
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid tableoid
Definition: pg_backup.h:266
bool relispopulated
Definition: pg_dump.h:290
struct _tableDataInfo * dataObj
Definition: pg_dump.h:361
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 1703 of file pg_dump.c.

1704 {
1705  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1706 
1707  if (ext == NULL)
1708  return false;
1709 
1710  dobj->ext_member = true;
1711 
1712  /* Record dependency so that getDependencies needn't deal with that */
1713  addObjectDependency(dobj, ext->dobj.dumpId);
1714 
1715  /*
1716  * In 9.6 and above, mark the member object to have any non-initial ACLs
1717  * dumped. (Any initial ACLs will be removed later, using data from
1718  * pg_init_privs, so that we'll dump only the delta from the extension's
1719  * initial setup.)
1720  *
1721  * Prior to 9.6, we do not include any extension member components.
1722  *
1723  * In binary upgrades, we still dump all components of the members
1724  * individually, since the idea is to exactly reproduce the database
1725  * contents rather than replace the extension contents with something
1726  * different.
1727  *
1728  * Note: it might be interesting someday to implement storage and delta
1729  * dumping of extension members' RLS policies and/or security labels.
1730  * However there is a pitfall for RLS policies: trying to dump them
1731  * requires getting a lock on their tables, and the calling user might not
1732  * have privileges for that. We need no lock to examine a table's ACLs,
1733  * so the current feature doesn't have a problem of that sort.
1734  */
1735  if (fout->dopt->binary_upgrade)
1736  dobj->dump = ext->dobj.dump;
1737  else
1738  {
1739  if (fout->remoteVersion < 90600)
1740  dobj->dump = DUMP_COMPONENT_NONE;
1741  else
1742  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL);
1743  }
1744 
1745  return true;
1746 }
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:1057
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:101
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:96
DumpOptions * dopt
Definition: pg_backup.h:214
int binary_upgrade
Definition: pg_backup.h:166
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 10305 of file pg_dump.c.

10306 {
10307  PGresult *res;
10308  PQExpBuffer query;
10309  int i_description;
10310  int i_classoid;
10311  int i_objoid;
10312  int i_objsubid;
10313  int ntups;
10314  int i;
10315  DumpableObject *dobj;
10316 
10317  query = createPQExpBuffer();
10318 
10319  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
10320  "FROM pg_catalog.pg_description "
10321  "ORDER BY classoid, objoid, objsubid");
10322 
10323  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10324 
10325  /* Construct lookup table containing OIDs in numeric form */
10326 
10327  i_description = PQfnumber(res, "description");
10328  i_classoid = PQfnumber(res, "classoid");
10329  i_objoid = PQfnumber(res, "objoid");
10330  i_objsubid = PQfnumber(res, "objsubid");
10331 
10332  ntups = PQntuples(res);
10333 
10334  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10335  ncomments = 0;
10336  dobj = NULL;
10337 
10338  for (i = 0; i < ntups; i++)
10339  {
10340  CatalogId objId;
10341  int subid;
10342 
10343  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
10344  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
10345  subid = atoi(PQgetvalue(res, i, i_objsubid));
10346 
10347  /* We needn't remember comments that don't match any dumpable object */
10348  if (dobj == NULL ||
10349  dobj->catId.tableoid != objId.tableoid ||
10350  dobj->catId.oid != objId.oid)
10351  dobj = findObjectByCatalogId(objId);
10352  if (dobj == NULL)
10353  continue;
10354 
10355  /*
10356  * Comments on columns of composite types are linked to the type's
10357  * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
10358  * in the type's own DumpableObject.
10359  */
10360  if (subid != 0 && dobj->objType == DO_TABLE &&
10361  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
10362  {
10363  TypeInfo *cTypeInfo;
10364 
10365  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
10366  if (cTypeInfo)
10367  cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
10368  }
10369  else
10370  dobj->components |= DUMP_COMPONENT_COMMENT;
10371 
10372  comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
10374  comments[ncomments].objoid = objId.oid;
10375  comments[ncomments].objsubid = subid;
10376  ncomments++;
10377  }
10378 
10379  PQclear(res);
10380  destroyPQExpBuffer(query);
10381 }
TypeInfo * findTypeByOid(Oid oid)
Definition: common.c:887
static int ncomments
Definition: pg_dump.c:154
static CommentItem * comments
Definition: pg_dump.c:153
#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 9850 of file pg_dump.c.

9851 {
9852  PGresult *res;
9853  const char *query;
9854  int i;
9855 
9856  query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
9857 
9858  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
9859 
9861 
9863 
9864  for (i = 0; i < nrolenames; i++)
9865  {
9868  }
9869 
9870  PQclear(res);
9871 }
static RoleNameItem * rolenames
Definition: pg_dump.c:149
static int nrolenames
Definition: pg_dump.c:150
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 15447 of file pg_dump.c.

15448 {
15449  PGresult *res;
15450  PQExpBuffer query;
15451  int i_label;
15452  int i_provider;
15453  int i_classoid;
15454  int i_objoid;
15455  int i_objsubid;
15456  int ntups;
15457  int i;
15458  DumpableObject *dobj;
15459 
15460  query = createPQExpBuffer();
15461 
15462  appendPQExpBufferStr(query,
15463  "SELECT label, provider, classoid, objoid, objsubid "
15464  "FROM pg_catalog.pg_seclabel "
15465  "ORDER BY classoid, objoid, objsubid");
15466 
15467  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15468 
15469  /* Construct lookup table containing OIDs in numeric form */
15470  i_label = PQfnumber(res, "label");
15471  i_provider = PQfnumber(res, "provider");
15472  i_classoid = PQfnumber(res, "classoid");
15473  i_objoid = PQfnumber(res, "objoid");
15474  i_objsubid = PQfnumber(res, "objsubid");
15475 
15476  ntups = PQntuples(res);
15477 
15478  seclabels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15479  nseclabels = 0;
15480  dobj = NULL;
15481 
15482  for (i = 0; i < ntups; i++)
15483  {
15484  CatalogId objId;
15485  int subid;
15486 
15487  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
15488  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
15489  subid = atoi(PQgetvalue(res, i, i_objsubid));
15490 
15491  /* We needn't remember labels that don't match any dumpable object */
15492  if (dobj == NULL ||
15493  dobj->catId.tableoid != objId.tableoid ||
15494  dobj->catId.oid != objId.oid)
15495  dobj = findObjectByCatalogId(objId);
15496  if (dobj == NULL)
15497  continue;
15498 
15499  /*
15500  * Labels on columns of composite types are linked to the type's
15501  * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
15502  * in the type's own DumpableObject.
15503  */
15504  if (subid != 0 && dobj->objType == DO_TABLE &&
15505  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
15506  {
15507  TypeInfo *cTypeInfo;
15508 
15509  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
15510  if (cTypeInfo)
15511  cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
15512  }
15513  else
15514  dobj->components |= DUMP_COMPONENT_SECLABEL;
15515 
15519  seclabels[nseclabels].objoid = objId.oid;
15520  seclabels[nseclabels].objsubid = subid;
15521  nseclabels++;
15522  }
15523 
15524  PQclear(res);
15525  destroyPQExpBuffer(query);
15526 }
static int nseclabels
Definition: pg_dump.c:158
static SecLabelItem * seclabels
Definition: pg_dump.c:157
#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 12997 of file pg_dump.c.

12998 {
12999  char *name;
13000  char *paren;
13001  bool inquote;
13002 
13003  /* In all cases "-" means a null reference */
13004  if (strcmp(proc, "-") == 0)
13005  return NULL;
13006 
13007  name = pg_strdup(proc);
13008  /* find non-double-quoted left paren */
13009  inquote = false;
13010  for (paren = name; *paren; paren++)
13011  {
13012  if (*paren == '(' && !inquote)
13013  {
13014  *paren = '\0';
13015  break;
13016  }
13017  if (*paren == '"')
13018  inquote = !inquote;
13019  }
13020  return name;
13021 }
const char * name

References name, and pg_strdup().

Referenced by dumpOpr().

◆ convertTSFunction()

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

Definition at line 13068 of file pg_dump.c.

13069 {
13070  char *result;
13071  char query[128];
13072  PGresult *res;
13073 
13074  snprintf(query, sizeof(query),
13075  "SELECT '%u'::pg_catalog.regproc", funcOid);
13076  res = ExecuteSqlQueryForSingleRow(fout, query);
13077 
13078  result = pg_strdup(PQgetvalue(res, 0, 0));
13079 
13080  PQclear(res);
13081 
13082  return result;
13083 }
#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 18412 of file pg_dump.c.

18413 {
18414  DumpableObject *dobjs;
18415 
18416  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18417 
18418  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18419  dobjs[0].catId = nilCatalogId;
18420  AssignDumpId(dobjs + 0);
18421  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18422 
18423  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18424  dobjs[1].catId = nilCatalogId;
18425  AssignDumpId(dobjs + 1);
18426  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18427 
18428  return dobjs;
18429 }
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:645
static const CatalogId nilCatalogId
Definition: pg_dump.c:142

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 15722 of file pg_dump.c.

15723 {
15724  PQExpBuffer result = createPQExpBuffer();
15725  int j;
15726 
15727  appendPQExpBufferStr(result, "SELECT");
15728 
15729  for (j = 0; j < tbinfo->numatts; j++)
15730  {
15731  if (j > 0)
15732  appendPQExpBufferChar(result, ',');
15733  appendPQExpBufferStr(result, "\n ");
15734 
15735  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15736 
15737  /*
15738  * Must add collation if not default for the type, because CREATE OR
15739  * REPLACE VIEW won't change it
15740  */
15741  if (OidIsValid(tbinfo->attcollation[j]))
15742  {
15743  CollInfo *coll;
15744 
15745  coll = findCollationByOid(tbinfo->attcollation[j]);
15746  if (coll)
15747  appendPQExpBuffer(result, " COLLATE %s",
15748  fmtQualifiedDumpable(coll));
15749  }
15750 
15751  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15752  }
15753 
15754  return result;
15755 }
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:942
int j
Definition: isn.c:74
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:176
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 15673 of file pg_dump.c.

15674 {
15675  PQExpBuffer query = createPQExpBuffer();
15676  PQExpBuffer result = createPQExpBuffer();
15677  PGresult *res;
15678  int len;
15679 
15680  /* Fetch the view definition */
15681  appendPQExpBuffer(query,
15682  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15683  tbinfo->dobj.catId.oid);
15684 
15685  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15686 
15687  if (PQntuples(res) != 1)
15688  {
15689  if (PQntuples(res) < 1)
15690  pg_fatal("query to obtain definition of view \"%s\" returned no data",
15691  tbinfo->dobj.name);
15692  else
15693  pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
15694  tbinfo->dobj.name);
15695  }
15696 
15697  len = PQgetlength(res, 0, 0);
15698 
15699  if (len == 0)
15700  pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
15701  tbinfo->dobj.name);
15702 
15703  /* Strip off the trailing semicolon so that other things may follow. */
15704  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15705  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15706 
15707  PQclear(res);
15708  destroyPQExpBuffer(query);
15709 
15710  return result;
15711 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3887
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 13090 of file pg_dump.c.

13091 {
13092  DumpOptions *dopt = fout->dopt;
13093  PQExpBuffer q;
13094  PQExpBuffer delq;
13095  char *qamname;
13096 
13097  /* Do nothing in data-only dump */
13098  if (dopt->dataOnly)
13099  return;
13100 
13101  q = createPQExpBuffer();
13102  delq = createPQExpBuffer();
13103 
13104  qamname = pg_strdup(fmtId(aminfo->dobj.name));
13105 
13106  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
13107 
13108  switch (aminfo->amtype)
13109  {
13110  case AMTYPE_INDEX:
13111  appendPQExpBufferStr(q, "TYPE INDEX ");
13112  break;
13113  case AMTYPE_TABLE:
13114  appendPQExpBufferStr(q, "TYPE TABLE ");
13115  break;
13116  default:
13117  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
13118  aminfo->amtype, qamname);
13119  destroyPQExpBuffer(q);
13120  destroyPQExpBuffer(delq);
13121  free(qamname);
13122  return;
13123  }
13124 
13125  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
13126 
13127  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
13128  qamname);
13129 
13130  if (dopt->binary_upgrade)
13132  "ACCESS METHOD", qamname, NULL);
13133 
13134  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13135  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
13136  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
13137  .description = "ACCESS METHOD",
13138  .section = SECTION_PRE_DATA,
13139  .createStmt = q->data,
13140  .dropStmt = delq->data));
13141 
13142  /* Dump Access Method Comments */
13143  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13144  dumpComment(fout, "ACCESS METHOD", qamname,
13145  NULL, "",
13146  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
13147 
13148  destroyPQExpBuffer(q);
13149  destroyPQExpBuffer(delq);
13150  free(qamname);
13151 }
@ 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:10114
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:5509
#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:170

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 *  tag,
const char *  owner,
const DumpableAcl dacl 
)
static

Definition at line 15078 of file pg_dump.c.

15082 {
15083  DumpId aclDumpId = InvalidDumpId;
15084  DumpOptions *dopt = fout->dopt;
15085  const char *acls = dacl->acl;
15086  const char *acldefault = dacl->acldefault;
15087  char privtype = dacl->privtype;
15088  const char *initprivs = dacl->initprivs;
15089  const char *baseacls;
15090  PQExpBuffer sql;
15091 
15092  /* Do nothing if ACL dump is not enabled */
15093  if (dopt->aclsSkip)
15094  return InvalidDumpId;
15095 
15096  /* --data-only skips ACLs *except* large object ACLs */
15097  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
15098  return InvalidDumpId;
15099 
15100  sql = createPQExpBuffer();
15101 
15102  /*
15103  * In binary upgrade mode, we don't run an extension's script but instead
15104  * dump out the objects independently and then recreate them. To preserve
15105  * any initial privileges which were set on extension objects, we need to
15106  * compute the set of GRANT and REVOKE commands necessary to get from the
15107  * default privileges of an object to its initial privileges as recorded
15108  * in pg_init_privs.
15109  *
15110  * At restore time, we apply these commands after having called
15111  * binary_upgrade_set_record_init_privs(true). That tells the backend to
15112  * copy the results into pg_init_privs. This is how we preserve the
15113  * contents of that catalog across binary upgrades.
15114  */
15115  if (dopt->binary_upgrade && privtype == 'e' &&
15116  initprivs && *initprivs != '\0')
15117  {
15118  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
15119  if (!buildACLCommands(name, subname, nspname, type,
15120  initprivs, acldefault, owner,
15121  "", fout->remoteVersion, sql))
15122  pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
15123  initprivs, acldefault, name, type);
15124  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
15125  }
15126 
15127  /*
15128  * Now figure the GRANT and REVOKE commands needed to get to the object's
15129  * actual current ACL, starting from the initprivs if given, else from the
15130  * object-type-specific default. Also, while buildACLCommands will assume
15131  * that a NULL/empty acls string means it needn't do anything, what that
15132  * actually represents is the object-type-specific default; so we need to
15133  * substitute the acldefault string to get the right results in that case.
15134  */
15135  if (initprivs && *initprivs != '\0')
15136  {
15137  baseacls = initprivs;
15138  if (acls == NULL || *acls == '\0')
15139  acls = acldefault;
15140  }
15141  else
15142  baseacls = acldefault;
15143 
15144  if (!buildACLCommands(name, subname, nspname, type,
15145  acls, baseacls, owner,
15146  "", fout->remoteVersion, sql))
15147  pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
15148  acls, baseacls, name, type);
15149 
15150  if (sql->len > 0)
15151  {
15152  PQExpBuffer tagbuf = createPQExpBuffer();
15153  DumpId aclDeps[2];
15154  int nDeps = 0;
15155 
15156  if (tag)
15157  appendPQExpBufferStr(tagbuf, tag);
15158  else if (subname)
15159  appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname);
15160  else
15161  appendPQExpBuffer(tagbuf, "%s %s", type, name);
15162 
15163  aclDeps[nDeps++] = objDumpId;
15164  if (altDumpId != InvalidDumpId)
15165  aclDeps[nDeps++] = altDumpId;
15166 
15167  aclDumpId = createDumpId();
15168 
15169  ArchiveEntry(fout, nilCatalogId, aclDumpId,
15170  ARCHIVE_OPTS(.tag = tagbuf->data,
15171  .namespace = nspname,
15172  .owner = owner,
15173  .description = "ACL",
15174  .section = SECTION_NONE,
15175  .createStmt = sql->data,
15176  .deps = aclDeps,
15177  .nDeps = nDeps));
15178 
15179  destroyPQExpBuffer(tagbuf);
15180  }
15181 
15182  destroyPQExpBuffer(sql);
15183 
15184  return aclDumpId;
15185 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:786
DumpId createDumpId(void)
Definition: common.c:733
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:272
NameData subname
bool aclsSkip
Definition: pg_backup.h:172
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 14043 of file pg_dump.c.

14044 {
14045  DumpOptions *dopt = fout->dopt;
14046  PQExpBuffer query;
14047  PQExpBuffer q;
14048  PQExpBuffer delq;
14049  PQExpBuffer details;
14050  char *aggsig; /* identity signature */
14051  char *aggfullsig = NULL; /* full signature */
14052  char *aggsig_tag;
14053  PGresult *res;
14054  int i_agginitval;
14055  int i_aggminitval;
14056  const char *aggtransfn;
14057  const char *aggfinalfn;
14058  const char *aggcombinefn;
14059  const char *aggserialfn;
14060  const char *aggdeserialfn;
14061  const char *aggmtransfn;
14062  const char *aggminvtransfn;
14063  const char *aggmfinalfn;
14064  bool aggfinalextra;
14065  bool aggmfinalextra;
14066  char aggfinalmodify;
14067  char aggmfinalmodify;
14068  const char *aggsortop;
14069  char *aggsortconvop;
14070  char aggkind;
14071  const char *aggtranstype;
14072  const char *aggtransspace;
14073  const char *aggmtranstype;
14074  const char *aggmtransspace;
14075  const char *agginitval;
14076  const char *aggminitval;
14077  const char *proparallel;
14078  char defaultfinalmodify;
14079 
14080  /* Do nothing in data-only dump */
14081  if (dopt->dataOnly)
14082  return;
14083 
14084  query = createPQExpBuffer();
14085  q = createPQExpBuffer();
14086  delq = createPQExpBuffer();
14087  details = createPQExpBuffer();
14088 
14089  if (!fout->is_prepared[PREPQUERY_DUMPAGG])
14090  {
14091  /* Set up query for aggregate-specific details */
14092  appendPQExpBufferStr(query,
14093  "PREPARE dumpAgg(pg_catalog.oid) AS\n");
14094 
14095  appendPQExpBufferStr(query,
14096  "SELECT "
14097  "aggtransfn,\n"
14098  "aggfinalfn,\n"
14099  "aggtranstype::pg_catalog.regtype,\n"
14100  "agginitval,\n"
14101  "aggsortop,\n"
14102  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
14103  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
14104 
14105  if (fout->remoteVersion >= 90400)
14106  appendPQExpBufferStr(query,
14107  "aggkind,\n"
14108  "aggmtransfn,\n"
14109  "aggminvtransfn,\n"
14110  "aggmfinalfn,\n"
14111  "aggmtranstype::pg_catalog.regtype,\n"
14112  "aggfinalextra,\n"
14113  "aggmfinalextra,\n"
14114  "aggtransspace,\n"
14115  "aggmtransspace,\n"
14116  "aggminitval,\n");
14117  else
14118  appendPQExpBufferStr(query,
14119  "'n' AS aggkind,\n"
14120  "'-' AS aggmtransfn,\n"
14121  "'-' AS aggminvtransfn,\n"
14122  "'-' AS aggmfinalfn,\n"
14123  "0 AS aggmtranstype,\n"
14124  "false AS aggfinalextra,\n"
14125  "false AS aggmfinalextra,\n"
14126  "0 AS aggtransspace,\n"
14127  "0 AS aggmtransspace,\n"
14128  "NULL AS aggminitval,\n");
14129 
14130  if (fout->remoteVersion >= 90600)
14131  appendPQExpBufferStr(query,
14132  "aggcombinefn,\n"
14133  "aggserialfn,\n"
14134  "aggdeserialfn,\n"
14135  "proparallel,\n");
14136  else
14137  appendPQExpBufferStr(query,
14138  "'-' AS aggcombinefn,\n"
14139  "'-' AS aggserialfn,\n"
14140  "'-' AS aggdeserialfn,\n"
14141  "'u' AS proparallel,\n");
14142 
14143  if (fout->remoteVersion >= 110000)
14144  appendPQExpBufferStr(query,
14145  "aggfinalmodify,\n"
14146  "aggmfinalmodify\n");
14147  else
14148  appendPQExpBufferStr(query,
14149  "'0' AS aggfinalmodify,\n"
14150  "'0' AS aggmfinalmodify\n");
14151 
14152  appendPQExpBufferStr(query,
14153  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14154  "WHERE a.aggfnoid = p.oid "
14155  "AND p.oid = $1");
14156 
14157  ExecuteSqlStatement(fout, query->data);
14158 
14159  fout->is_prepared[PREPQUERY_DUMPAGG] = true;
14160  }
14161 
14162  printfPQExpBuffer(query,
14163  "EXECUTE dumpAgg('%u')",
14164  agginfo->aggfn.dobj.catId.oid);
14165 
14166  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14167 
14168  i_agginitval = PQfnumber(res, "agginitval");
14169  i_aggminitval = PQfnumber(res, "aggminitval");
14170 
14171  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
14172  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
14173  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
14174  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
14175  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
14176  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
14177  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
14178  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
14179  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
14180  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
14181  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
14182  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
14183  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
14184  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
14185  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
14186  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
14187  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
14188  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
14189  agginitval = PQgetvalue(res, 0, i_agginitval);
14190  aggminitval = PQgetvalue(res, 0, i_aggminitval);
14191  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
14192 
14193  {
14194  char *funcargs;
14195  char *funciargs;
14196 
14197  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
14198  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
14199  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
14200  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
14201  }
14202 
14203  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14204 
14205  /* identify default modify flag for aggkind (must match DefineAggregate) */
14206  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14207  /* replace omitted flags for old versions */
14208  if (aggfinalmodify == '0')
14209  aggfinalmodify = defaultfinalmodify;
14210  if (aggmfinalmodify == '0')
14211  aggmfinalmodify = defaultfinalmodify;
14212 
14213  /* regproc and regtype output is already sufficiently quoted */
14214  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14215  aggtransfn, aggtranstype);
14216 
14217  if (strcmp(aggtransspace, "0") != 0)
14218  {
14219  appendPQExpBuffer(details, ",\n SSPACE = %s",
14220  aggtransspace);
14221  }
14222 
14223  if (!PQgetisnull(res, 0, i_agginitval))
14224  {
14225  appendPQExpBufferStr(details, ",\n INITCOND = ");
14226  appendStringLiteralAH(details, agginitval, fout);
14227  }
14228 
14229  if (strcmp(aggfinalfn, "-") != 0)
14230  {
14231  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14232  aggfinalfn);
14233  if (aggfinalextra)
14234  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14235  if (aggfinalmodify != defaultfinalmodify)
14236  {
14237  switch (aggfinalmodify)
14238  {
14239  case AGGMODIFY_READ_ONLY:
14240  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14241  break;
14242  case AGGMODIFY_SHAREABLE:
14243  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14244  break;
14245  case AGGMODIFY_READ_WRITE:
14246  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14247  break;
14248  default:
14249  pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14250  agginfo->aggfn.dobj.name);
14251  break;
14252  }
14253  }
14254  }
14255 
14256  if (strcmp(aggcombinefn, "-") != 0)
14257  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14258 
14259  if (strcmp(aggserialfn, "-") != 0)
14260  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14261 
14262  if (strcmp(aggdeserialfn, "-") != 0)
14263  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14264 
14265  if (strcmp(aggmtransfn, "-") != 0)
14266  {
14267  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14268  aggmtransfn,
14269  aggminvtransfn,
14270  aggmtranstype);
14271  }
14272 
14273  if (strcmp(aggmtransspace, "0") != 0)
14274  {
14275  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14276  aggmtransspace);
14277  }
14278 
14279  if (!PQgetisnull(res, 0, i_aggminitval))
14280  {
14281  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14282  appendStringLiteralAH(details, aggminitval, fout);
14283  }
14284 
14285  if (strcmp(aggmfinalfn, "-") != 0)
14286  {
14287  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14288  aggmfinalfn);
14289  if (aggmfinalextra)
14290  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14291  if (aggmfinalmodify != defaultfinalmodify)
14292  {
14293  switch (aggmfinalmodify)
14294  {
14295  case AGGMODIFY_READ_ONLY:
14296  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14297  break;
14298  case AGGMODIFY_SHAREABLE:
14299  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14300  break;
14301  case AGGMODIFY_READ_WRITE:
14302  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14303  break;
14304  default:
14305  pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14306  agginfo->aggfn.dobj.name);
14307  break;
14308  }
14309  }
14310  }
14311 
14312  aggsortconvop = getFormattedOperatorName(aggsortop);
14313  if (aggsortconvop)
14314  {
14315  appendPQExpBuffer(details, ",\n SORTOP = %s",
14316  aggsortconvop);
14317  free(aggsortconvop);
14318  }
14319 
14320  if (aggkind == AGGKIND_HYPOTHETICAL)
14321  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14322 
14323  if (proparallel[0] != PROPARALLEL_UNSAFE)
14324  {
14325  if (proparallel[0] == PROPARALLEL_SAFE)
14326  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14327  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14328  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14329  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14330  pg_fatal("unrecognized proparallel value for function \"%s\"",
14331  agginfo->aggfn.dobj.name);
14332  }
14333 
14334  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14335  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14336  aggsig);
14337 
14338  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14339  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14340  aggfullsig ? aggfullsig : aggsig, details->data);
14341 
14342  if (dopt->binary_upgrade)
14343  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
14344  "AGGREGATE", aggsig,
14345  agginfo->aggfn.dobj.namespace->dobj.name);
14346 
14347  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14348  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14349  agginfo->aggfn.dobj.dumpId,
14350  ARCHIVE_OPTS(.tag = aggsig_tag,
14351  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14352  .owner = agginfo->aggfn.rolname,
14353  .description = "AGGREGATE",
14354  .section = SECTION_PRE_DATA,
14355  .createStmt = q->data,
14356  .dropStmt = delq->data));
14357 
14358  /* Dump Aggregate Comments */
14359  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14360  dumpComment(fout, "AGGREGATE", aggsig,
14361  agginfo->aggfn.dobj.namespace->dobj.name,
14362  agginfo->aggfn.rolname,
14363  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14364 
14365  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14366  dumpSecLabel(fout, "AGGREGATE", aggsig,
14367  agginfo->aggfn.dobj.namespace->dobj.name,
14368  agginfo->aggfn.rolname,
14369  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14370 
14371  /*
14372  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14373  * command look like a function's GRANT; in particular this affects the
14374  * syntax for zero-argument aggregates and ordered-set aggregates.
14375  */
14376  free(aggsig);
14377 
14378  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14379 
14380  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14381  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
14382  "FUNCTION", aggsig, NULL,
14383  agginfo->aggfn.dobj.namespace->dobj.name,
14384  NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
14385 
14386  free(aggsig);
14387  free(aggfullsig);
14388  free(aggsig_tag);
14389 
14390  PQclear(res);
14391 
14392  destroyPQExpBuffer(query);
14393  destroyPQExpBuffer(q);
14394  destroyPQExpBuffer(delq);
14395  destroyPQExpBuffer(details);
14396 }
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:332
@ PREPQUERY_DUMPAGG
Definition: pg_backup.h:66
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:278
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *tag, const char *owner, const DumpableAcl *dacl)
Definition: pg_dump.c:15078
static char * getFormattedOperatorName(const char *oproid)
Definition: pg_dump.c:13038
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:12099
static char * format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:14011
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:12076
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:15206
bool * is_prepared
Definition: pg_backup.h:241

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 16630 of file pg_dump.c.

16631 {
16632  DumpOptions *dopt = fout->dopt;
16633  TableInfo *tbinfo = adinfo->adtable;
16634  int adnum = adinfo->adnum;
16635  PQExpBuffer q;
16636  PQExpBuffer delq;
16637  char *qualrelname;
16638  char *tag;
16639  char *foreign;
16640 
16641  /* Do nothing in data-only dump */
16642  if (dopt->dataOnly)
16643  return;
16644 
16645  /* Skip if not "separate"; it was dumped in the table's definition */
16646  if (!adinfo->separate)
16647  return;
16648 
16649  q = createPQExpBuffer();
16650  delq = createPQExpBuffer();
16651 
16652  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16653 
16654  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16655 
16657  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16658  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16659  adinfo->adef_expr);
16660 
16661  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16662  foreign, qualrelname,
16663  fmtId(tbinfo->attnames[adnum - 1]));
16664 
16665  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16666 
16667  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16668  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16669  ARCHIVE_OPTS(.tag = tag,
16670  .namespace = tbinfo->dobj.namespace->dobj.name,
16671  .owner = tbinfo->rolname,
16672  .description = "DEFAULT",
16673  .section = SECTION_PRE_DATA,
16674  .createStmt = q->data,
16675  .dropStmt = delq->data));
16676 
16677  free(tag);
16678  destroyPQExpBuffer(q);
16679  destroyPQExpBuffer(delq);
16680  free(qualrelname);
16681 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
DumpableObject dobj
Definition: pg_dump.h:375
char * adef_expr
Definition: pg_dump.h:378
TableInfo * adtable
Definition: pg_dump.h:376
bool separate
Definition: pg_dump.h:379
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 11181 of file pg_dump.c.

11182 {
11183  DumpOptions *dopt = fout->dopt;
11185  PQExpBuffer delq = createPQExpBuffer();
11186  PQExpBuffer query = createPQExpBuffer();
11187  PGresult *res;
11188  char *qtypname;
11189  char *qualtypname;
11190  char *typlen;
11191  char *typinput;
11192  char *typoutput;
11193  char *typreceive;
11194  char *typsend;
11195  char *typmodin;
11196  char *typmodout;
11197  char *typanalyze;
11198  char *typsubscript;
11199  Oid typreceiveoid;
11200  Oid typsendoid;
11201  Oid typmodinoid;
11202  Oid typmodoutoid;
11203  Oid typanalyzeoid;
11204  Oid typsubscriptoid;
11205  char *typcategory;
11206  char *typispreferred;
11207  char *typdelim;
11208  char *typbyval;
11209  char *typalign;
11210  char *typstorage;
11211  char *typcollatable;
11212  char *typdefault;
11213  bool typdefault_is_literal = false;
11214 
11215  if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
11216  {
11217  /* Set up query for type-specific details */
11218  appendPQExpBufferStr(query,
11219  "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
11220  "SELECT typlen, "
11221  "typinput, typoutput, typreceive, typsend, "
11222  "typreceive::pg_catalog.oid AS typreceiveoid, "
11223  "typsend::pg_catalog.oid AS typsendoid, "
11224  "typanalyze, "
11225  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
11226  "typdelim, typbyval, typalign, typstorage, "
11227  "typmodin, typmodout, "
11228  "typmodin::pg_catalog.oid AS typmodinoid, "
11229  "typmodout::pg_catalog.oid AS typmodoutoid, "
11230  "typcategory, typispreferred, "
11231  "(typcollation <> 0) AS typcollatable, "
11232  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
11233 
11234  if (fout->remoteVersion >= 140000)
11235  appendPQExpBufferStr(query,
11236  "typsubscript, "
11237  "typsubscript::pg_catalog.oid AS typsubscriptoid ");
11238  else
11239  appendPQExpBufferStr(query,
11240  "'-' AS typsubscript, 0 AS typsubscriptoid ");
11241 
11242  appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
11243  "WHERE oid = $1");
11244 
11245  ExecuteSqlStatement(fout, query->data);
11246 
11247  fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
11248  }
11249 
11250  printfPQExpBuffer(query,
11251  "EXECUTE dumpBaseType('%u')",
11252  tyinfo->dobj.catId.oid);
11253 
11254  res = ExecuteSqlQueryForSingleRow(fout, query->data);
11255 
11256  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
11257  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
11258  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
11259  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
11260  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
11261  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
11262  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
11263  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
11264  typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
11265  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
11266  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
11267  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
11268  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
11269  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
11270  typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
11271  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
11272  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
11273  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
11274  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
11275  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
11276  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
11277  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
11278  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
11279  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
11280  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
11281  {
11282  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
11283  typdefault_is_literal = true; /* it needs quotes */
11284  }
11285  else
11286  typdefault = NULL;
11287 
11288  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11289  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11290 
11291  /*
11292  * The reason we include CASCADE is that the circular dependency between
11293  * the type and its I/O functions makes it impossible to drop the type any
11294  * other way.
11295  */
11296  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
11297 
11298  /*
11299  * We might already have a shell type, but setting pg_type_oid is
11300  * harmless, and in any case we'd better set the array type OID.
11301  */
11302  if (dopt->binary_upgrade)
11304  tyinfo->dobj.catId.oid,
11305  false, false);
11306 
11308  "CREATE TYPE %s (\n"
11309  " INTERNALLENGTH = %s",
11310  qualtypname,
11311  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
11312 
11313  /* regproc result is sufficiently quoted already */
11314  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
11315  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
11316  if (OidIsValid(typreceiveoid))
11317  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
11318  if (OidIsValid(typsendoid))
11319  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
11320  if (OidIsValid(typmodinoid))
11321  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
11322  if (OidIsValid(typmodoutoid))
11323  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
11324  if (OidIsValid(typanalyzeoid))
11325  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
11326 
11327  if (strcmp(typcollatable, "t") == 0)
11328  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
11329 
11330  if (typdefault != NULL)
11331  {
11332  appendPQExpBufferStr(q, ",\n DEFAULT = ");
11333  if (typdefault_is_literal)
11334  appendStringLiteralAH(q, typdefault, fout);
11335  else
11336  appendPQExpBufferStr(q, typdefault);
11337  }
11338 
11339  if (OidIsValid(typsubscriptoid))
11340  appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
11341 
11342  if (OidIsValid(tyinfo->typelem))
11343  appendPQExpBuffer(q, ",\n ELEMENT = %s",
11344  getFormattedTypeName(fout, tyinfo->typelem,
11345  zeroIsError));
11346 
11347  if (strcmp(typcategory, "U") != 0)
11348  {
11349  appendPQExpBufferStr(q, ",\n CATEGORY = ");
11350  appendStringLiteralAH(q, typcategory, fout);
11351  }
11352 
11353  if (strcmp(typispreferred, "t") == 0)
11354  appendPQExpBufferStr(q, ",\n PREFERRED = true");
11355 
11356  if (typdelim && strcmp(typdelim, ",") != 0)
11357  {
11358  appendPQExpBufferStr(q, ",\n DELIMITER = ");
11359  appendStringLiteralAH(q, typdelim, fout);
11360  }
11361 
11362  if (*typalign == TYPALIGN_CHAR)
11363  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
11364  else if (*typalign == TYPALIGN_SHORT)
11365  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
11366  else if (*typalign == TYPALIGN_INT)
11367  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
11368  else if (*typalign == TYPALIGN_DOUBLE)
11369  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
11370 
11371  if (*typstorage == TYPSTORAGE_PLAIN)
11372  appendPQExpBufferStr(q, ",\n STORAGE = plain");
11373  else if (*typstorage == TYPSTORAGE_EXTERNAL)
11374  appendPQExpBufferStr(q, ",\n STORAGE = external");
11375  else if (*typstorage == TYPSTORAGE_EXTENDED)
11376  appendPQExpBufferStr(q, ",\n STORAGE = extended");
11377  else if (*typstorage == TYPSTORAGE_MAIN)
11378  appendPQExpBufferStr(q, ",\n STORAGE = main");
11379 
11380  if (strcmp(typbyval, "t") == 0)
11381  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
11382 
11383  appendPQExpBufferStr(q, "\n);\n");
11384 
11385  if (dopt->binary_upgrade)
11387  "TYPE", qtypname,
11388  tyinfo->dobj.namespace->dobj.name);
11389 
11390  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11391  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11392  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11393  .namespace = tyinfo->dobj.namespace->dobj.name,
11394  .owner = tyinfo->rolname,
11395  .description = "TYPE",
11396  .section = SECTION_PRE_DATA,
11397  .createStmt = q->data,
11398  .dropStmt = delq->data));
11399 
11400  /* Dump Type Comments and Security Labels */
11401  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11402  dumpComment(fout, "TYPE", qtypname,
11403  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11404  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11405 
11406  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11407  dumpSecLabel(fout, "TYPE", qtypname,
11408  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11409  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11410 
11411  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11412  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11413  qtypname, NULL,
11414  tyinfo->dobj.namespace->dobj.name,
11415  NULL, tyinfo->rolname, &tyinfo->dacl);
11416 
11417  PQclear(res);
11418  destroyPQExpBuffer(q);
11419  destroyPQExpBuffer(delq);
11420  destroyPQExpBuffer(query);
11421  free(qtypname);
11422  free(qualtypname);
11423 }
@ PREPQUERY_DUMPBASETYPE
Definition: pg_backup.h:67
static const char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18656
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 12544 of file pg_dump.c.

12545 {
12546  DumpOptions *dopt = fout->dopt;
12547  PQExpBuffer defqry;
12548  PQExpBuffer delqry;
12549  PQExpBuffer labelq;
12550  PQExpBuffer castargs;
12551  FuncInfo *funcInfo = NULL;
12552  const char *sourceType;
12553  const char *targetType;
12554 
12555  /* Do nothing in data-only dump */
12556  if (dopt->dataOnly)
12557  return;
12558 
12559  /* Cannot dump if we don't have the cast function's info */
12560  if (OidIsValid(cast->castfunc))
12561  {
12562  funcInfo = findFuncByOid(cast->castfunc);
12563  if (funcInfo == NULL)
12564  pg_fatal("could not find function definition for function with OID %u",
12565  cast->castfunc);
12566  }
12567 
12568  defqry = createPQExpBuffer();
12569  delqry = createPQExpBuffer();
12570  labelq = createPQExpBuffer();
12571  castargs = createPQExpBuffer();
12572 
12573  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12574  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12575  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12576  sourceType, targetType);
12577 
12578  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12579  sourceType, targetType);
12580 
12581  switch (cast->castmethod)
12582  {
12583  case COERCION_METHOD_BINARY:
12584  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12585  break;
12586  case COERCION_METHOD_INOUT:
12587  appendPQExpBufferStr(defqry, "WITH INOUT");
12588  break;
12589  case COERCION_METHOD_FUNCTION:
12590  if (funcInfo)
12591  {
12592  char *fsig = format_function_signature(fout, funcInfo, true);
12593 
12594  /*
12595  * Always qualify the function name (format_function_signature
12596  * won't qualify it).
12597  */
12598  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12599  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12600  free(fsig);
12601  }
12602  else
12603  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12604  break;
12605  default:
12606  pg_log_warning("bogus value in pg_cast.castmethod field");
12607  }
12608 
12609  if (cast->castcontext == 'a')
12610  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12611  else if (cast->castcontext == 'i')
12612  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12613  appendPQExpBufferStr(defqry, ";\n");
12614 
12615  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12616  sourceType, targetType);
12617 
12618  appendPQExpBuffer(castargs, "(%s AS %s)",
12619  sourceType, targetType);
12620 
12621  if (dopt->binary_upgrade)
12622  binary_upgrade_extension_member(defqry, &cast->dobj,
12623  "CAST", castargs->data, NULL);
12624 
12625  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12626  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12627  ARCHIVE_OPTS(.tag = labelq->data,
12628  .description = "CAST",
12629  .section = SECTION_PRE_DATA,
12630  .createStmt = defqry->data,
12631  .dropStmt = delqry->data));
12632 
12633  /* Dump Cast Comments */
12634  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12635  dumpComment(fout, "CAST", castargs->data,
12636  NULL, "",
12637  cast->dobj.catId, 0, cast->dobj.dumpId);
12638 
12639  destroyPQExpBuffer(defqry);
12640  destroyPQExpBuffer(delqry);
12641  destroyPQExpBuffer(labelq);
12642  destroyPQExpBuffer(castargs);
12643 }
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:906
char castmethod
Definition: pg_dump.h:499
Oid casttarget
Definition: pg_dump.h:496
char castcontext
Definition: pg_dump.h:498
DumpableObject dobj
Definition: pg_dump.h:494
Oid castsource
Definition: pg_dump.h:495
Oid castfunc
Definition: pg_dump.h:497
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 13658 of file pg_dump.c.

13659 {
13660  DumpOptions *dopt = fout->dopt;
13661  PQExpBuffer query;
13662  PQExpBuffer q;
13663  PQExpBuffer delq;
13664  char *qcollname;
13665  PGresult *res;
13666  int i_collprovider;
13667  int i_collisdeterministic;
13668  int i_collcollate;
13669  int i_collctype;
13670  int i_colllocale;
13671  int i_collicurules;
13672  const char *collprovider;
13673  const char *collcollate;
13674  const char *collctype;
13675  const char *colllocale;
13676  const char *collicurules;
13677 
13678  /* Do nothing in data-only dump */
13679  if (dopt->dataOnly)
13680  return;
13681 
13682  query = createPQExpBuffer();
13683  q = createPQExpBuffer();
13684  delq = createPQExpBuffer();
13685 
13686  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13687 
13688  /* Get collation-specific details */
13689  appendPQExpBufferStr(query, "SELECT ");
13690 
13691  if (fout->remoteVersion >= 100000)
13692  appendPQExpBufferStr(query,
13693  "collprovider, "
13694  "collversion, ");
13695  else
13696  appendPQExpBufferStr(query,
13697  "'c' AS collprovider, "
13698  "NULL AS collversion, ");
13699 
13700  if (fout->remoteVersion >= 120000)
13701  appendPQExpBufferStr(query,
13702  "collisdeterministic, ");
13703  else
13704  appendPQExpBufferStr(query,
13705  "true AS collisdeterministic, ");
13706 
13707  if (fout->remoteVersion >= 170000)
13708  appendPQExpBufferStr(query,
13709  "colllocale, ");
13710  else if (fout->remoteVersion >= 150000)
13711  appendPQExpBufferStr(query,
13712  "colliculocale AS colllocale, ");
13713  else
13714  appendPQExpBufferStr(query,
13715  "NULL AS colllocale, ");
13716 
13717  if (fout->remoteVersion >= 160000)
13718  appendPQExpBufferStr(query,
13719  "collicurules, ");
13720  else
13721  appendPQExpBufferStr(query,
13722  "NULL AS collicurules, ");
13723 
13724  appendPQExpBuffer(query,
13725  "collcollate, "
13726  "collctype "
13727  "FROM pg_catalog.pg_collation c "
13728  "WHERE c.oid = '%u'::pg_catalog.oid",
13729  collinfo->dobj.catId.oid);
13730 
13731  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13732 
13733  i_collprovider = PQfnumber(res, "collprovider");
13734  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13735  i_collcollate = PQfnumber(res, "collcollate");
13736  i_collctype = PQfnumber(res, "collctype");
13737  i_colllocale = PQfnumber(res, "colllocale");
13738  i_collicurules = PQfnumber(res, "collicurules");
13739 
13740  collprovider = PQgetvalue(res, 0, i_collprovider);
13741 
13742  if (!PQgetisnull(res, 0, i_collcollate))
13743  collcollate = PQgetvalue(res, 0, i_collcollate);
13744  else
13745  collcollate = NULL;
13746 
13747  if (!PQgetisnull(res, 0, i_collctype))
13748  collctype = PQgetvalue(res, 0, i_collctype);
13749  else
13750  collctype = NULL;
13751 
13752  /*
13753  * Before version 15, collcollate and collctype were of type NAME and
13754  * non-nullable. Treat empty strings as NULL for consistency.
13755  */
13756  if (fout->remoteVersion < 150000)
13757  {
13758  if (collcollate[0] == '\0')
13759  collcollate = NULL;
13760  if (collctype[0] == '\0')
13761  collctype = NULL;
13762  }
13763 
13764  if (!PQgetisnull(res, 0, i_colllocale))
13765  colllocale = PQgetvalue(res, 0, i_colllocale);
13766  else
13767  colllocale = NULL;
13768 
13769  if (!PQgetisnull(res, 0, i_collicurules))
13770  collicurules = PQgetvalue(res, 0, i_collicurules);
13771  else
13772  collicurules = NULL;
13773 
13774  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13775  fmtQualifiedDumpable(collinfo));
13776 
13777  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13778  fmtQualifiedDumpable(collinfo));
13779 
13780  appendPQExpBufferStr(q, "provider = ");
13781  if (collprovider[0] == 'b')
13782  appendPQExpBufferStr(q, "builtin");
13783  else if (collprovider[0] == 'c')
13784  appendPQExpBufferStr(q, "libc");
13785  else if (collprovider[0] == 'i')
13786  appendPQExpBufferStr(q, "icu");
13787  else if (collprovider[0] == 'd')
13788  /* to allow dumping pg_catalog; not accepted on input */
13789  appendPQExpBufferStr(q, "default");
13790  else
13791  pg_fatal("unrecognized collation provider: %s",
13792  collprovider);
13793 
13794  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13795  appendPQExpBufferStr(q, ", deterministic = false");
13796 
13797  if (collprovider[0] == 'd')
13798  {
13799  if (collcollate || collctype || colllocale || collicurules)
13800  pg_log_warning("invalid collation \"%s\"", qcollname);
13801 
13802  /* no locale -- the default collation cannot be reloaded anyway */
13803  }
13804  else if (collprovider[0] == 'b')
13805  {
13806  if (collcollate || collctype || !colllocale || collicurules)
13807  pg_log_warning("invalid collation \"%s\"", qcollname);
13808 
13809  appendPQExpBufferStr(q, ", locale = ");
13810  appendStringLiteralAH(q, colllocale ? colllocale : "",
13811  fout);
13812  }
13813  else if (collprovider[0] == 'i')
13814  {
13815  if (fout->remoteVersion >= 150000)
13816  {
13817  if (collcollate || collctype || !colllocale)
13818  pg_log_warning("invalid collation \"%s\"", qcollname);
13819 
13820  appendPQExpBufferStr(q, ", locale = ");
13821  appendStringLiteralAH(q, colllocale ? colllocale : "",
13822  fout);
13823  }
13824  else
13825  {
13826  if (!collcollate || !collctype || colllocale ||
13827  strcmp(collcollate, collctype) != 0)
13828  pg_log_warning("invalid collation \"%s\"", qcollname);
13829 
13830  appendPQExpBufferStr(q, ", locale = ");
13831  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13832  }
13833 
13834  if (collicurules)
13835  {
13836  appendPQExpBufferStr(q, ", rules = ");
13837  appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
13838  }
13839  }
13840  else if (collprovider[0] == 'c')
13841  {
13842  if (colllocale || collicurules || !collcollate || !collctype)
13843  pg_log_warning("invalid collation \"%s\"", qcollname);
13844 
13845  if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
13846  {
13847  appendPQExpBufferStr(q, ", locale = ");
13848  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13849  }
13850  else
13851  {
13852  appendPQExpBufferStr(q, ", lc_collate = ");
13853  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13854  appendPQExpBufferStr(q, ", lc_ctype = ");
13855  appendStringLiteralAH(q, collctype ? collctype : "", fout);
13856  }
13857  }
13858  else
13859  pg_fatal("unrecognized collation provider: %s", collprovider);
13860 
13861  /*
13862  * For binary upgrade, carry over the collation version. For normal
13863  * dump/restore, omit the version, so that it is computed upon restore.
13864  */
13865  if (dopt->binary_upgrade)
13866  {
13867  int i_collversion;
13868 
13869  i_collversion = PQfnumber(res, "collversion");
13870  if (!PQgetisnull(res, 0, i_collversion))
13871  {
13872  appendPQExpBufferStr(q, ", version = ");
13874  PQgetvalue(res, 0, i_collversion),
13875  fout);
13876  }
13877  }
13878 
13879  appendPQExpBufferStr(q, ");\n");
13880 
13881  if (dopt->binary_upgrade)
13882  binary_upgrade_extension_member(q, &collinfo->dobj,
13883  "COLLATION", qcollname,
13884  collinfo->dobj.namespace->dobj.name);
13885 
13886  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13887  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13888  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13889  .namespace = collinfo->dobj.namespace->dobj.name,
13890  .owner = collinfo->rolname,
13891  .description = "COLLATION",
13892  .section = SECTION_PRE_DATA,
13893  .createStmt = q->data,
13894  .dropStmt = delq->data));
13895 
13896  /* Dump Collation Comments */
13897  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13898  dumpComment(fout, "COLLATION", qcollname,
13899  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13900  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13901 
13902  PQclear(res);
13903 
13904  destroyPQExpBuffer(query);
13905  destroyPQExpBuffer(q);
13906  destroyPQExpBuffer(delq);
13907  free(qcollname);
13908 }
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 10114 of file pg_dump.c.

10118 {
10119  dumpCommentExtended(fout, type, name, namespace, owner,
10120  catalogId, subid, dumpId, NULL);
10121 }
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:10014

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 10014 of file pg_dump.c.

10019 {
10020  DumpOptions *dopt = fout->dopt;
10022  int ncomments;
10023 
10024  /* do nothing, if --no-comments is supplied */
10025  if (dopt->no_comments)
10026  return;
10027 
10028  /* Comments are schema not data ... except LO comments are data */
10029  if (strcmp(type, "LARGE OBJECT") != 0)
10030  {
10031  if (dopt->dataOnly)
10032  return;
10033  }
10034  else
10035  {
10036  /* We do dump LO comments in binary-upgrade mode */
10037  if (dopt->schemaOnly && !dopt->binary_upgrade)
10038  return;
10039  }
10040 
10041  /* Search for comments associated with catalogId, using table */
10042  ncomments = findComments(catalogId.tableoid, catalogId.oid,
10043  &comments);
10044 
10045  /* Is there one matching the subid? */
10046  while (ncomments > 0)
10047  {
10048  if (comments->objsubid == subid)
10049  break;
10050  comments++;
10051  ncomments--;
10052  }
10053 
10054  if (initdb_comment != NULL)
10055  {
10056  static CommentItem empty_comment = {.descr = ""};
10057 
10058  /*
10059  * initdb creates this object with a comment. Skip dumping the
10060  * initdb-provided comment, which would complicate matters for
10061  * non-superuser use of pg_dump. When the DBA has removed initdb's
10062  * comment, replicate that.
10063  */
10064  if (ncomments == 0)
10065  {
10066  comments = &empty_comment;
10067  ncomments = 1;
10068  }
10069  else if (strcmp(comments->descr, initdb_comment) == 0)
10070  ncomments = 0;
10071  }
10072 
10073  /* If a comment exists, build COMMENT ON statement */
10074  if (ncomments > 0)
10075  {
10076  PQExpBuffer query = createPQExpBuffer();
10078 
10079  appendPQExpBuffer(query, "COMMENT ON %s ", type);
10080  if (namespace && *namespace)
10081  appendPQExpBuffer(query, "%s.", fmtId(namespace));
10082  appendPQExpBuffer(query, "%s IS ", name);
10083  appendStringLiteralAH(query, comments->descr, fout);
10084  appendPQExpBufferStr(query, ";\n");
10085 
10086  appendPQExpBuffer(tag, "%s %s", type, name);
10087 
10088  /*
10089  * We mark comments as SECTION_NONE because they really belong in the
10090  * same section as their parent, whether that is pre-data or
10091  * post-data.
10092  */
10094  ARCHIVE_OPTS(.tag = tag->data,
10095  .namespace = namespace,
10096  .owner = owner,
10097  .description = "COMMENT",
10098  .section = SECTION_NONE,
10099  .createStmt = query->data,
10100  .deps = &dumpId,
10101  .nDeps = 1));
10102 
10103  destroyPQExpBuffer(query);
10104  destroyPQExpBuffer(tag);
10105  }
10106 }
static int findComments(Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:10228
int no_comments
Definition: pg_backup.h:180
bool schemaOnly
Definition: pg_backup.h:169

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 11603 of file pg_dump.c.

11604 {
11605  DumpOptions *dopt = fout->dopt;
11607  PQExpBuffer dropped = createPQExpBuffer();
11608  PQExpBuffer delq = createPQExpBuffer();
11609  PQExpBuffer query = createPQExpBuffer();
11610  PGresult *res;
11611  char *qtypname;
11612  char *qualtypname;
11613  int ntups;
11614  int i_attname;
11615  int i_atttypdefn;
11616  int i_attlen;
11617  int i_attalign;
11618  int i_attisdropped;
11619  int i_attcollation;
11620  int i;
11621  int actual_atts;
11622 
11624  {
11625  /*
11626  * Set up query for type-specific details.
11627  *
11628  * Since we only want to dump COLLATE clauses for attributes whose
11629  * collation is different from their type's default, we use a CASE
11630  * here to suppress uninteresting attcollations cheaply. atttypid
11631  * will be 0 for dropped columns; collation does not matter for those.
11632  */
11633  appendPQExpBufferStr(query,
11634  "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
11635  "SELECT a.attname, a.attnum, "
11636  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11637  "a.attlen, a.attalign, a.attisdropped, "
11638  "CASE WHEN a.attcollation <> at.typcollation "
11639  "THEN a.attcollation ELSE 0 END AS attcollation "
11640  "FROM pg_catalog.pg_type ct "
11641  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11642  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11643  "WHERE ct.oid = $1 "
11644  "ORDER BY a.attnum");
11645 
11646  ExecuteSqlStatement(fout, query->data);
11647 
11649  }
11650 
11651  printfPQExpBuffer(query,
11652  "EXECUTE dumpCompositeType('%u')",
11653  tyinfo->dobj.catId.oid);
11654 
11655  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11656 
11657  ntups = PQntuples(res);
11658 
11659  i_attname = PQfnumber(res, "attname");
11660  i_atttypdefn = PQfnumber(res, "atttypdefn");
11661  i_attlen = PQfnumber(res, "attlen");
11662  i_attalign = PQfnumber(res, "attalign");
11663  i_attisdropped = PQfnumber(res, "attisdropped");
11664  i_attcollation = PQfnumber(res, "attcollation");
11665 
11666  if (dopt->binary_upgrade)
11667  {
11669  tyinfo->dobj.catId.oid,
11670  false, false);
11671  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11672  }
11673 
11674  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11675  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11676 
11677  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11678  qualtypname);
11679 
11680  actual_atts = 0;
11681  for (i = 0; i < ntups; i++)
11682  {
11683  char *attname;
11684  char *atttypdefn;
11685  char *attlen;
11686  char *attalign;
11687  bool attisdropped;
11688  Oid attcollation;
11689 
11690  attname = PQgetvalue(res, i, i_attname);
11691  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11692  attlen = PQgetvalue(res, i, i_attlen);
11693  attalign = PQgetvalue(res, i, i_attalign);
11694  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11695  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11696 
11697  if (attisdropped && !dopt->binary_upgrade)
11698  continue;
11699 
11700  /* Format properly if not first attr */
11701  if (actual_atts++ > 0)
11702  appendPQExpBufferChar(q, ',');
11703  appendPQExpBufferStr(q, "\n\t");
11704 
11705  if (!attisdropped)
11706  {
11707  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11708 
11709  /* Add collation if not default for the column type */
11710  if (OidIsValid(attcollation))
11711  {
11712  CollInfo *coll;
11713 
11714  coll = findCollationByOid(attcollation);
11715  if (coll)
11716  appendPQExpBuffer(q, " COLLATE %s",
11717  fmtQualifiedDumpable(coll));
11718  }
11719  }
11720  else
11721  {
11722  /*
11723  * This is a dropped attribute and we're in binary_upgrade mode.
11724  * Insert a placeholder for it in the CREATE TYPE command, and set
11725  * length and alignment with direct UPDATE to the catalogs
11726  * afterwards. See similar code in dumpTableSchema().
11727  */
11728  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11729 
11730  /* stash separately for insertion after the CREATE TYPE */
11731  appendPQExpBufferStr(dropped,
11732  "\n-- For binary upgrade, recreate dropped column.\n");
11733  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11734  "SET attlen = %s, "
11735  "attalign = '%s', attbyval = false\n"
11736  "WHERE attname = ", attlen, attalign);
11737  appendStringLiteralAH(dropped, attname, fout);
11738  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11739  appendStringLiteralAH(dropped, qualtypname, fout);
11740  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11741 
11742  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11743  qualtypname);
11744  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11745  fmtId(attname));
11746  }
11747  }
11748  appendPQExpBufferStr(q, "\n);\n");
11749  appendPQExpBufferStr(q, dropped->data);
11750 
11751  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11752 
11753  if (dopt->binary_upgrade)
11755  "TYPE", qtypname,
11756  tyinfo->dobj.namespace->dobj.name);
11757 
11758  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11759  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11760  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11761  .namespace = tyinfo->dobj.namespace->dobj.name,
11762  .owner = tyinfo->rolname,
11763  .description = "TYPE",
11764  .section = SECTION_PRE_DATA,
11765  .createStmt = q->data,
11766  .dropStmt = delq->data));
11767 
11768 
11769  /* Dump Type Comments and Security Labels */
11770  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11771  dumpComment(fout, "TYPE", qtypname,
11772  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11773  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11774 
11775  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11776  dumpSecLabel(fout, "TYPE", qtypname,
11777  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11778  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11779 
11780  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11781  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11782  qtypname, NULL,
11783  tyinfo->dobj.namespace->dobj.name,
11784  NULL, tyinfo->rolname, &tyinfo->dacl);
11785 
11786  /* Dump any per-column comments */
11787  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11788  dumpCompositeTypeColComments(fout, tyinfo, res);
11789 
11790  PQclear(res);
11791  destroyPQExpBuffer(q);
11792  destroyPQExpBuffer(dropped);
11793  destroyPQExpBuffer(delq);
11794  destroyPQExpBuffer(query);
11795  free(qtypname);
11796  free(qualtypname);
11797 }
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:5393
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, PGresult *res)
Definition: pg_dump.c:11809
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 11809 of file pg_dump.c.

11811 {
11813  int ncomments;
11814  PQExpBuffer query;
11815  PQExpBuffer target;
11816  int i;
11817  int ntups;
11818  int i_attname;
11819  int i_attnum;
11820  int i_attisdropped;
11821 
11822  /* do nothing, if --no-comments is supplied */
11823  if (fout->dopt->no_comments)
11824  return;
11825 
11826  /* Search for comments associated with type's pg_class OID */
11827  ncomments = findComments(RelationRelationId, tyinfo->typrelid,
11828  &comments);
11829 
11830  /* If no comments exist, we're done */
11831  if (ncomments <= 0)
11832  return;
11833 
11834  /* Build COMMENT ON statements */
11835  query = createPQExpBuffer();
11836  target = createPQExpBuffer();
11837 
11838  ntups = PQntuples(res);
11839  i_attnum = PQfnumber(res, "attnum");
11840  i_attname = PQfnumber(res, "attname");
11841  i_attisdropped = PQfnumber(res, "attisdropped");
11842  while (ncomments > 0)
11843  {
11844  const char *attname;
11845 
11846  attname = NULL;
11847  for (i = 0; i < ntups; i++)
11848  {
11849  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
11850  PQgetvalue(res, i, i_attisdropped)[0] != 't')
11851  {
11852  attname = PQgetvalue(res, i, i_attname);
11853  break;
11854  }
11855  }
11856  if (attname) /* just in case we don't find it */
11857  {
11858  const char *descr = comments->descr;
11859 
11860  resetPQExpBuffer(target);
11861  appendPQExpBuffer(target, "COLUMN %s.",
11862  fmtId(tyinfo->dobj.name));
11864 
11865  resetPQExpBuffer(query);
11866  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11867  fmtQualifiedDumpable(tyinfo));
11868  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11869  appendStringLiteralAH(query, descr, fout);
11870  appendPQExpBufferStr(query, ";\n");
11871 
11873  ARCHIVE_OPTS(.tag = target->data,
11874  .namespace = tyinfo->dobj.namespace->dobj.name,
11875  .owner = tyinfo->rolname,
11876  .description = "COMMENT",
11877  .section = SECTION_NONE,
11878  .createStmt = query->data,
11879  .deps = &(tyinfo->dobj.dumpId),
11880  .nDeps = 1));
11881  }
11882 
11883  comments++;
11884  ncomments--;
11885  }
11886 
11887  destroyPQExpBuffer(query);
11888  destroyPQExpBuffer(target);
11889 }
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 16977 of file pg_dump.c.

16978 {
16979  DumpOptions *dopt = fout->dopt;
16980  TableInfo *tbinfo = coninfo->contable;
16981  PQExpBuffer q;
16982  PQExpBuffer delq;
16983  char *tag = NULL;
16984  char *foreign;
16985 
16986  /* Do nothing in data-only dump */
16987  if (dopt->dataOnly)
16988  return;
16989 
16990  q = createPQExpBuffer();
16991  delq = createPQExpBuffer();
16992 
16993  foreign = tbinfo &&
16994  tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16995 
16996  if (coninfo->contype == 'p' ||
16997  coninfo->contype == 'u' ||
16998  coninfo->contype == 'x')
16999  {
17000  /* Index-related constraint */
17001  IndxInfo *indxinfo;
17002  int k;
17003 
17004  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
17005 
17006  if (indxinfo == NULL)
17007  pg_fatal("missing index for constraint \"%s\"",
17008  coninfo->dobj.name);
17009 
17010  if (dopt->binary_upgrade)
17012  indxinfo->dobj.catId.oid, true);
17013 
17014  appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
17015  fmtQualifiedDumpable(tbinfo));
17016  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
17017  fmtId(coninfo->dobj.name));
17018 
17019  if (coninfo->condef)
17020  {
17021  /* pg_get_constraintdef should have provided everything */
17022  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
17023  }
17024  else
17025  {
17027  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
17028 
17029  /*
17030  * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
17031  * indexes. Being able to create this was fixed, but we need to
17032  * make the index distinct in order to be able to restore the
17033  * dump.
17034  */
17035  if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
17036  appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
17037  appendPQExpBufferStr(q, " (");
17038  for (k = 0; k < indxinfo->indnkeyattrs; k++)
17039  {
17040  int indkey = (int) indxinfo->indkeys[k];
17041  const char *attname;
17042 
17043  if (indkey == InvalidAttrNumber)
17044  break;
17045  attname = getAttrName(indkey, tbinfo);
17046 
17047  appendPQExpBuffer(q, "%s%s",
17048  (k == 0) ? "" : ", ",
17049  fmtId(attname));
17050  }
17051 
17052  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
17053  appendPQExpBufferStr(q, ") INCLUDE (");
17054 
17055  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
17056  {
17057  int indkey = (int) indxinfo->indkeys[k];
17058  const char *attname;
17059 
17060  if (indkey == InvalidAttrNumber)
17061  break;
17062  attname = getAttrName(indkey, tbinfo);
17063 
17064  appendPQExpBuffer(q, "%s%s",
17065  (k == indxinfo->indnkeyattrs) ? "" : ", ",
17066  fmtId(attname));
17067  }
17068 
17069  appendPQExpBufferChar(q, ')');
17070 
17071  if (nonemptyReloptions(indxinfo->indreloptions))
17072  {
17073  appendPQExpBufferStr(q, " WITH (");
17074  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
17075  appendPQExpBufferChar(q, ')');
17076  }
17077 
17078  if (coninfo->condeferrable)
17079  {
17080  appendPQExpBufferStr(q, " DEFERRABLE");
17081  if (coninfo->condeferred)
17082  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
17083  }
17084 
17085  appendPQExpBufferStr(q, ";\n");
17086  }
17087 
17088  /*
17089  * Append ALTER TABLE commands as needed to set properties that we
17090  * only have ALTER TABLE syntax for. Keep this in sync with the
17091  * similar code in dumpIndex!
17092  */
17093 
17094  /* If the index is clustered, we need to record that. */
17095  if (indxinfo->indisclustered)
17096  {
17097  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
17098  fmtQualifiedDumpable(tbinfo));
17099  /* index name is not qualified in this syntax */
17100  appendPQExpBuffer(q, " ON %s;\n",
17101  fmtId(indxinfo->dobj.name));
17102  }
17103 
17104  /* If the index defines identity, we need to record that. */
17105  if (indxinfo->indisreplident)
17106  {
17107  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
17108  fmtQualifiedDumpable(tbinfo));
17109  /* index name is not qualified in this syntax */
17110  appendPQExpBuffer(q, " INDEX %s;\n",
17111  fmtId(indxinfo->dobj.name));
17112  }
17113 
17114  /* Indexes can depend on extensions */
17115  append_depends_on_extension(fout, q, &indxinfo->dobj,
17116  "pg_catalog.pg_class", "INDEX",
17117  fmtQualifiedDumpable(indxinfo));
17118 
17119  appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
17120  fmtQualifiedDumpable(tbinfo));
17121  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17122  fmtId(coninfo->dobj.name));
17123 
17124  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17125 
17126  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17127  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17128  ARCHIVE_OPTS(.tag = tag,
17129  .namespace = tbinfo->dobj.namespace->dobj.name,
17130  .tablespace = indxinfo->tablespace,
17131  .owner = tbinfo->rolname,
17132  .description = "CONSTRAINT",
17133  .section = SECTION_POST_DATA,
17134  .createStmt = q->data,
17135  .dropStmt = delq->data));
17136  }
17137  else if (coninfo->contype == 'f')
17138  {
17139  char *only;
17140 
17141  /*
17142  * Foreign keys on partitioned tables are always declared as
17143  * inheriting to partitions; for all other cases, emit them as
17144  * applying ONLY directly to the named table, because that's how they
17145  * work for regular inherited tables.
17146  */
17147  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
17148 
17149  /*
17150  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
17151  * current table data is not processed
17152  */
17153  appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
17154  only, fmtQualifiedDumpable(tbinfo));
17155  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17156  fmtId(coninfo->dobj.name),
17157  coninfo->condef);
17158 
17159  appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
17160  only, fmtQualifiedDumpable(tbinfo));
17161  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17162  fmtId(coninfo->dobj.name));
17163 
17164  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17165 
17166  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17167  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17168  ARCHIVE_OPTS(.tag = tag,
17169  .namespace = tbinfo->dobj.namespace->dobj.name,
17170  .owner = tbinfo->rolname,
17171  .description = "FK CONSTRAINT",
17172  .section = SECTION_POST_DATA,
17173  .createStmt = q->data,
17174  .dropStmt = delq->data));
17175  }
17176  else if (coninfo->contype == 'c' && tbinfo)
17177  {
17178  /* CHECK constraint on a table */
17179 
17180  /* Ignore if not to be dumped separately, or if it was inherited */
17181  if (coninfo->separate && coninfo->conislocal)
17182  {
17183  /* not ONLY since we want it to propagate to children */
17184  appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
17185  fmtQualifiedDumpable(tbinfo));
17186  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17187  fmtId(coninfo->dobj.name),
17188  coninfo->condef);
17189 
17190  appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
17191  fmtQualifiedDumpable(tbinfo));
17192  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17193  fmtId(coninfo->dobj.name));
17194 
17195  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17196 
17197  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17198  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17199  ARCHIVE_OPTS(.tag = tag,
17200  .namespace = tbinfo->dobj.namespace->dobj.name,
17201  .owner = tbinfo->rolname,
17202  .description = "CHECK CONSTRAINT",
17203  .section = SECTION_POST_DATA,
17204  .createStmt = q->data,
17205  .dropStmt = delq->data));
17206  }
17207  }
17208  else if (coninfo->contype == 'c' && tbinfo == NULL)
17209  {
17210  /* CHECK constraint on a domain */
17211  TypeInfo *tyinfo = coninfo->condomain;
17212 
17213  /* Ignore if not to be dumped separately */
17214  if (coninfo->separate)
17215  {
17216  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
17217  fmtQualifiedDumpable(tyinfo));
17218  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17219  fmtId(coninfo->dobj.name),
17220  coninfo->condef);
17221 
17222  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
17223  fmtQualifiedDumpable(tyinfo));
17224  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17225  fmtId(coninfo->dobj.name));
17226 
17227  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
17228 
17229  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17230  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17231  ARCHIVE_OPTS(.tag = tag,
17232  .namespace = tyinfo->dobj.namespace->dobj.name,
17233  .owner = tyinfo->rolname,
17234  .description = "CHECK CONSTRAINT",
17235  .section = SECTION_POST_DATA,
17236  .createStmt = q->data,
17237  .dropStmt = delq->data));
17238  }
17239  }
17240  else
17241  {
17242  pg_fatal("unrecognized constraint type: %c",
17243  coninfo->contype);
17244  }
17245 
17246  /* Dump Constraint Comments --- only works for table constraints */
17247  if (tbinfo && coninfo->separate &&
17248  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17249  dumpTableConstraintComment(fout, coninfo);
17250 
17251  free(tag);
17252  destroyPQExpBuffer(q);
17253  destroyPQExpBuffer(delq);
17254 }
#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:5220
static const char * getAttrName(int attrnum, const TableInfo *tblInfo)
Definition: pg_dump.c:16691
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
Definition: pg_dump.c:18753
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
Definition: pg_dump.c:17264
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18741
TypeInfo * condomain
Definition: pg_dump.h:470
TableInfo * contable
Definition: pg_dump.h:469
bool condeferred
Definition: pg_dump.h:476
bool conislocal
Definition: pg_dump.h:477
DumpableObject dobj
Definition: pg_dump.h:468
DumpId conindex
Definition: pg_dump.h:474
bool condeferrable
Definition: pg_dump.h:475
char * condef
Definition: pg_dump.h:472
bool indisreplident
Definition: pg_dump.h:403
int indnkeyattrs
Definition: pg_dump.h:398
int indnattrs
Definition: pg_dump.h:399
Oid * indkeys
Definition: pg_dump.h:400
char * indreloptions
Definition: pg_dump.h:395
bool indisclustered
Definition: pg_dump.h:402
char * tablespace
Definition: pg_dump.h:394
bool indnullsnotdistinct
Definition: pg_dump.h:404
DumpableObject dobj
Definition: pg_dump.h:391

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, 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(), if(), _indxInfo::indisclustered, _indxInfo::indisreplident, _indxInfo::indkeys, _indxInfo::indnattrs, _indxInfo::indnkeyattrs, _indxInfo::indnullsnotdistinct, _indxInfo::indreloptions, InvalidAttrNumber, _dumpableObject::name, nonemptyReloptions(), 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 13915 of file pg_dump.c.

13916 {
13917  DumpOptions *dopt = fout->dopt;
13918  PQExpBuffer query;
13919  PQExpBuffer q;
13920  PQExpBuffer delq;
13921  char *qconvname;
13922  PGresult *res;
13923  int i_conforencoding;
13924  int i_contoencoding;
13925  int i_conproc;
13926  int i_condefault;
13927  const char *conforencoding;
13928  const char *contoencoding;
13929  const char *conproc;
13930  bool condefault;
13931 
13932  /* Do nothing in data-only dump */
13933  if (dopt->dataOnly)
13934  return;
13935 
13936  query = createPQExpBuffer();
13937  q = createPQExpBuffer();
13938  delq = createPQExpBuffer();
13939 
13940  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13941 
13942  /* Get conversion-specific details */
13943  appendPQExpBuffer(query, "SELECT "
13944  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13945  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13946  "conproc, condefault "
13947  "FROM pg_catalog.pg_conversion c "
13948  "WHERE c.oid = '%u'::pg_catalog.oid",
13949  convinfo->dobj.catId.oid);
13950 
13951  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13952 
13953  i_conforencoding = PQfnumber(res, "conforencoding");
13954  i_contoencoding = PQfnumber(res, "contoencoding");
13955  i_conproc = PQfnumber(res, "conproc");
13956  i_condefault = PQfnumber(res, "condefault");
13957 
13958  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13959  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13960  conproc = PQgetvalue(res, 0, i_conproc);
13961  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13962 
13963  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13964  fmtQualifiedDumpable(convinfo));
13965 
13966  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13967  (condefault) ? "DEFAULT " : "",
13968  fmtQualifiedDumpable(convinfo));
13969  appendStringLiteralAH(q, conforencoding, fout);
13970  appendPQExpBufferStr(q, " TO ");
13971  appendStringLiteralAH(q, contoencoding, fout);
13972  /* regproc output is already sufficiently quoted */
13973  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13974 
13975  if (dopt->binary_upgrade)
13976  binary_upgrade_extension_member(q, &convinfo->dobj,
13977  "CONVERSION", qconvname,
13978  convinfo->dobj.namespace->dobj.name);
13979 
13980  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13981  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13982  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13983  .namespace = convinfo->dobj.namespace->dobj.name,
13984  .owner = convinfo->rolname,
13985  .description = "CONVERSION",
13986  .section = SECTION_PRE_DATA,
13987  .createStmt = q->data,
13988  .dropStmt = delq->data));
13989 
13990  /* Dump Conversion Comments */
13991  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13992  dumpComment(fout, "CONVERSION", qconvname,
13993  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13994  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13995 
13996  PQclear(res);
13997 
13998  destroyPQExpBuffer(query);
13999  destroyPQExpBuffer(q);
14000  destroyPQExpBuffer(delq);
14001  free(qconvname);
14002 }
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 2997 of file pg_dump.c.

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

3465 {
3466  PGconn *conn = GetConnection(AH);
3468  PGresult *res;
3469 
3470  /* First collect database-specific options */
3471  printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3472  "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3473  dboid);
3474 
3475  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3476 
3477  for (int i = 0; i < PQntuples(res); i++)
3479  "DATABASE", dbname, NULL, NULL,
3480  outbuf);
3481 
3482  PQclear(res);
3483 
3484  /* Now look for role-and-database-specific options */
3485  printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3486  "FROM pg_db_role_setting s, pg_roles r "
3487  "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3488  dboid);
3489 
3490  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3491 
3492  for (int i = 0; i < PQntuples(res); i++)
3494  "ROLE", PQgetvalue(res, i, 0),
3495  "DATABASE", dbname,
3496  outbuf);
3497 
3498  PQclear(res);
3499 
3501 }
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
Definition: dumputils.c:823
static char * buf
Definition: pg_test_fsync.c:73
char * dbname
Definition: streamutil.c:52

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 14989 of file pg_dump.c.

14990 {
14991  DumpOptions *dopt = fout->dopt;
14992  PQExpBuffer q;
14993  PQExpBuffer tag;
14994  const char *type;
14995 
14996  /* Do nothing in data-only dump, or if we're skipping ACLs */
14997  if (dopt->dataOnly || dopt->aclsSkip)
14998  return;
14999 
15000  q = createPQExpBuffer();
15001  tag = createPQExpBuffer();
15002 
15003  switch (daclinfo->defaclobjtype)
15004  {
15005  case DEFACLOBJ_RELATION:
15006  type = "TABLES";
15007  break;
15008  case DEFACLOBJ_SEQUENCE:
15009  type = "SEQUENCES";
15010  break;
15011  case DEFACLOBJ_FUNCTION:
15012  type = "FUNCTIONS";
15013  break;
15014  case DEFACLOBJ_TYPE:
15015  type = "TYPES";
15016  break;
15017  case DEFACLOBJ_NAMESPACE:
15018  type = "SCHEMAS";
15019  break;
15020  default: