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 "getopt_long.h"
#include "libpq/libpq-fs.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "storage/block.h"
Include dependency graph for pg_dump.c:

Go to the source code of this file.

Data Structures

struct  RoleNameItem
 
struct  CommentItem
 
struct  SecLabelItem
 

Macros

#define DUMP_DEFAULT_ROWS_PER_INSERT   1
 
#define fmtQualifiedDumpable(obj)
 

Typedefs

typedef enum OidOptions OidOptions
 

Enumerations

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

Functions

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

Variables

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

Macro Definition Documentation

◆ DUMP_DEFAULT_ROWS_PER_INSERT

#define DUMP_DEFAULT_ROWS_PER_INSERT   1

Definition at line 160 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 165 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 94 of file pg_dump.c.

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

Function Documentation

◆ addBoundaryDependencies()

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

Definition at line 18429 of file pg_dump.c.

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

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

7493 {
7494  SimplePtrListCell *cell;
7495 
7496  Assert(dobj->objType == DO_FK_CONSTRAINT);
7497 
7498  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7499  {
7500  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7501 
7502  addObjectDependency(dobj, attach->dobj.dumpId);
7503 
7504  if (attach->partitionIdx->partattaches.head != NULL)
7505  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7506  }
7507 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7492
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54
IndxInfo * partitionIdx
Definition: pg_dump.h:420
DumpableObject dobj
Definition: pg_dump.h:418
SimplePtrList partattaches
Definition: pg_dump.h:410

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

4848 {
4849  if (dobj->depends_on_ext)
4850  {
4851  char *nm;
4852  PGresult *res;
4853  PQExpBuffer query;
4854  int ntups;
4855  int i_extname;
4856  int i;
4857 
4858  /* dodge fmtId() non-reentrancy */
4859  nm = pg_strdup(objname);
4860 
4861  query = createPQExpBuffer();
4862  appendPQExpBuffer(query,
4863  "SELECT e.extname "
4864  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4865  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4866  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4867  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4868  catalog,
4869  dobj->catId.oid);
4870  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4871  ntups = PQntuples(res);
4872  i_extname = PQfnumber(res, "extname");
4873  for (i = 0; i < ntups; i++)
4874  {
4875  appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
4876  keyword, nm,
4877  fmtId(PQgetvalue(res, i, i_extname)));
4878  }
4879 
4880  PQclear(res);
4881  destroyPQExpBuffer(query);
4882  pg_free(nm);
4883  }
4884 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3395
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3790
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3503
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:290
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
CatalogId catId
Definition: pg_dump.h:135
bool depends_on_ext
Definition: pg_dump.h:143

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

18747 {
18748  bool res;
18749 
18750  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18751  fout->std_strings);
18752  if (!res)
18753  pg_log_warning("could not parse %s array", "reloptions");
18754 }
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:804
bool std_strings
Definition: pg_backup.h:228
int encoding
Definition: pg_backup.h:227

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

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

◆ binary_upgrade_extension_member()

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

Definition at line 5131 of file pg_dump.c.

5136 {
5137  DumpableObject *extobj = NULL;
5138  int i;
5139 
5140  if (!dobj->ext_member)
5141  return;
5142 
5143  /*
5144  * Find the parent extension. We could avoid this search if we wanted to
5145  * add a link field to DumpableObject, but the space costs of that would
5146  * be considerable. We assume that member objects could only have a
5147  * direct dependency on their own extension, not any others.
5148  */
5149  for (i = 0; i < dobj->nDeps; i++)
5150  {
5151  extobj = findObjectByDumpId(dobj->dependencies[i]);
5152  if (extobj && extobj->objType == DO_EXTENSION)
5153  break;
5154  extobj = NULL;
5155  }
5156  if (extobj == NULL)
5157  pg_fatal("could not find parent extension for %s %s",
5158  objtype, objname);
5159 
5160  appendPQExpBufferStr(upgrade_buffer,
5161  "\n-- For binary upgrade, handle extension membership the hard way\n");
5162  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
5163  fmtId(extobj->name),
5164  objtype);
5165  if (objnamespace && *objnamespace)
5166  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
5167  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
5168 }
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:726
#define pg_fatal(...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * name
Definition: pg_dump.h:137
DumpId * dependencies
Definition: pg_dump.h:144
bool ext_member
Definition: pg_dump.h:142

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

5018 {
5019  PQExpBuffer upgrade_query = createPQExpBuffer();
5020  PGresult *upgrade_res;
5021  RelFileNumber relfilenumber;
5022  Oid toast_oid;
5023  RelFileNumber toast_relfilenumber;
5024  char relkind;
5025  Oid toast_index_oid;
5026  RelFileNumber toast_index_relfilenumber;
5027 
5028  /*
5029  * Preserve the OID and relfilenumber of the table, table's index, table's
5030  * toast table and toast table's index if any.
5031  *
5032  * One complexity is that the current table definition might not require
5033  * the creation of a TOAST table, but the old database might have a TOAST
5034  * table that was created earlier, before some wide columns were dropped.
5035  * By setting the TOAST oid we force creation of the TOAST heap and index
5036  * by the new backend, so we can copy the files during binary upgrade
5037  * without worrying about this case.
5038  */
5039  appendPQExpBuffer(upgrade_query,
5040  "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
5041  "FROM pg_catalog.pg_class c LEFT JOIN "
5042  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5043  "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5044  "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5045  "WHERE c.oid = '%u'::pg_catalog.oid;",
5046  pg_class_oid);
5047 
5048  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5049 
5050  relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
5051 
5052  relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5053  PQfnumber(upgrade_res, "relfilenode")));
5054  toast_oid = atooid(PQgetvalue(upgrade_res, 0,
5055  PQfnumber(upgrade_res, "reltoastrelid")));
5056  toast_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5057  PQfnumber(upgrade_res, "toast_relfilenode")));
5058  toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
5059  PQfnumber(upgrade_res, "indexrelid")));
5060  toast_index_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
5061  PQfnumber(upgrade_res, "toast_index_relfilenode")));
5062 
5063  appendPQExpBufferStr(upgrade_buffer,
5064  "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
5065 
5066  if (!is_index)
5067  {
5068  appendPQExpBuffer(upgrade_buffer,
5069  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5070  pg_class_oid);
5071 
5072  /*
5073  * Not every relation has storage. Also, in a pre-v12 database,
5074  * partitioned tables have a relfilenumber, which should not be
5075  * preserved when upgrading.
5076  */
5077  if (RelFileNumberIsValid(relfilenumber) && relkind != RELKIND_PARTITIONED_TABLE)
5078  appendPQExpBuffer(upgrade_buffer,
5079  "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
5080  relfilenumber);
5081 
5082  /*
5083  * In a pre-v12 database, partitioned tables might be marked as having
5084  * toast tables, but we should ignore them if so.
5085  */
5086  if (OidIsValid(toast_oid) &&
5087  relkind != RELKIND_PARTITIONED_TABLE)
5088  {
5089  appendPQExpBuffer(upgrade_buffer,
5090  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
5091  toast_oid);
5092  appendPQExpBuffer(upgrade_buffer,
5093  "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
5094  toast_relfilenumber);
5095 
5096  /* every toast table has an index */
5097  appendPQExpBuffer(upgrade_buffer,
5098  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5099  toast_index_oid);
5100  appendPQExpBuffer(upgrade_buffer,
5101  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5102  toast_index_relfilenumber);
5103  }
5104 
5105  PQclear(upgrade_res);
5106  }
5107  else
5108  {
5109  /* Preserve the OID and relfilenumber of the index */
5110  appendPQExpBuffer(upgrade_buffer,
5111  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5112  pg_class_oid);
5113  appendPQExpBuffer(upgrade_buffer,
5114  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5115  relfilenumber);
5116  }
5117 
5118  appendPQExpBufferChar(upgrade_buffer, '\n');
5119 
5120  destroyPQExpBuffer(upgrade_query);
5121 }
#define OidIsValid(objectId)
Definition: c.h:764
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:305
unsigned int Oid
Definition: postgres_ext.h:31
#define atooid(x)
Definition: postgres_ext.h:42
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
Oid RelFileNumber
Definition: relpath.h:25
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27

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

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

◆ binary_upgrade_set_type_oids_by_rel()

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

Definition at line 5003 of file pg_dump.c.

5006 {
5007  Oid pg_type_oid = tbinfo->reltype;
5008 
5009  if (OidIsValid(pg_type_oid))
5010  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5011  pg_type_oid, false, false);
5012 }
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:4918
Oid reltype
Definition: pg_dump.h:308

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

4923 {
4924  PQExpBuffer upgrade_query = createPQExpBuffer();
4925  PGresult *res;
4926  Oid pg_type_array_oid;
4927  Oid pg_type_multirange_oid;
4928  Oid pg_type_multirange_array_oid;
4929 
4930  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4931  appendPQExpBuffer(upgrade_buffer,
4932  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4933  pg_type_oid);
4934 
4935  appendPQExpBuffer(upgrade_query,
4936  "SELECT typarray "
4937  "FROM pg_catalog.pg_type "
4938  "WHERE oid = '%u'::pg_catalog.oid;",
4939  pg_type_oid);
4940 
4941  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4942 
4943  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4944 
4945  PQclear(res);
4946 
4947  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4948  pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4949 
4950  if (OidIsValid(pg_type_array_oid))
4951  {
4952  appendPQExpBufferStr(upgrade_buffer,
4953  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4954  appendPQExpBuffer(upgrade_buffer,
4955  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4956  pg_type_array_oid);
4957  }
4958 
4959  /*
4960  * Pre-set the multirange type oid and its own array type oid.
4961  */
4962  if (include_multirange_type)
4963  {
4964  if (fout->remoteVersion >= 140000)
4965  {
4966  printfPQExpBuffer(upgrade_query,
4967  "SELECT t.oid, t.typarray "
4968  "FROM pg_catalog.pg_type t "
4969  "JOIN pg_catalog.pg_range r "
4970  "ON t.oid = r.rngmultitypid "
4971  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
4972  pg_type_oid);
4973 
4974  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4975 
4976  pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
4977  pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4978 
4979  PQclear(res);
4980  }
4981  else
4982  {
4983  pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4984  pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4985  }
4986 
4987  appendPQExpBufferStr(upgrade_buffer,
4988  "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
4989  appendPQExpBuffer(upgrade_buffer,
4990  "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4991  pg_type_multirange_oid);
4992  appendPQExpBufferStr(upgrade_buffer,
4993  "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
4994  appendPQExpBuffer(upgrade_buffer,
4995  "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4996  pg_type_multirange_array_oid);
4997  }
4998 
4999  destroyPQExpBuffer(upgrade_query);
5000 }
static Oid get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
Definition: pg_dump.c:4887
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
int remoteVersion
Definition: pg_backup.h:217

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

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

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

Definition at line 18544 of file pg_dump.c.

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

2781 {
2782  PQExpBuffer query;
2783  PGresult *res;
2784  int ntups,
2785  i;
2786  int i_classid,
2787  i_objid,
2788  i_refobjid;
2789 
2790  /* No Mat Views before 9.3. */
2791  if (fout->remoteVersion < 90300)
2792  return;
2793 
2794  query = createPQExpBuffer();
2795 
2796  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2797  "( "
2798  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2799  "FROM pg_depend d1 "
2800  "JOIN pg_class c1 ON c1.oid = d1.objid "
2801  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2802  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2803  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2804  "AND d2.objid = r1.oid "
2805  "AND d2.refobjid <> d1.objid "
2806  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2807  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2808  CppAsString2(RELKIND_VIEW) ") "
2809  "WHERE d1.classid = 'pg_class'::regclass "
2810  "UNION "
2811  "SELECT w.objid, d3.refobjid, c3.relkind "
2812  "FROM w "
2813  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2814  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2815  "AND d3.objid = r3.oid "
2816  "AND d3.refobjid <> w.refobjid "
2817  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2818  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2819  CppAsString2(RELKIND_VIEW) ") "
2820  ") "
2821  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2822  "FROM w "
2823  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2824 
2825  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2826 
2827  ntups = PQntuples(res);
2828 
2829  i_classid = PQfnumber(res, "classid");
2830  i_objid = PQfnumber(res, "objid");
2831  i_refobjid = PQfnumber(res, "refobjid");
2832 
2833  for (i = 0; i < ntups; i++)
2834  {
2835  CatalogId objId;
2836  CatalogId refobjId;
2837  DumpableObject *dobj;
2838  DumpableObject *refdobj;
2839  TableInfo *tbinfo;
2840  TableInfo *reftbinfo;
2841 
2842  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2843  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2844  refobjId.tableoid = objId.tableoid;
2845  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2846 
2847  dobj = findObjectByCatalogId(objId);
2848  if (dobj == NULL)
2849  continue;
2850 
2851  Assert(dobj->objType == DO_TABLE);
2852  tbinfo = (TableInfo *) dobj;
2853  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2854  dobj = (DumpableObject *) tbinfo->dataObj;
2855  if (dobj == NULL)
2856  continue;
2857  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2858 
2859  refdobj = findObjectByCatalogId(refobjId);
2860  if (refdobj == NULL)
2861  continue;
2862 
2863  Assert(refdobj->objType == DO_TABLE);
2864  reftbinfo = (TableInfo *) refdobj;
2865  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2866  refdobj = (DumpableObject *) reftbinfo->dataObj;
2867  if (refdobj == NULL)
2868  continue;
2869  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2870 
2871  addObjectDependency(dobj, refdobj->dumpId);
2872 
2873  if (!reftbinfo->relispopulated)
2874  tbinfo->relispopulated = false;
2875  }
2876 
2877  PQclear(res);
2878 
2879  destroyPQExpBuffer(query);
2880 }
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:739
#define CppAsString2(x)
Definition: c.h:316
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid tableoid
Definition: pg_backup.h:264
bool relispopulated
Definition: pg_dump.h:289
struct _tableDataInfo * dataObj
Definition: pg_dump.h:365
char relkind
Definition: pg_dump.h:287

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

1671 {
1672  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1673 
1674  if (ext == NULL)
1675  return false;
1676 
1677  dobj->ext_member = true;
1678 
1679  /* Record dependency so that getDependencies needn't deal with that */
1680  addObjectDependency(dobj, ext->dobj.dumpId);
1681 
1682  /*
1683  * In 9.6 and above, mark the member object to have any non-initial ACL,
1684  * policies, and security labels dumped.
1685  *
1686  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1687  * extract the information about the object. We don't provide support for
1688  * initial policies and security labels and it seems unlikely for those to
1689  * ever exist, but we may have to revisit this later.
1690  *
1691  * Prior to 9.6, we do not include any extension member components.
1692  *
1693  * In binary upgrades, we still dump all components of the members
1694  * individually, since the idea is to exactly reproduce the database
1695  * contents rather than replace the extension contents with something
1696  * different.
1697  */
1698  if (fout->dopt->binary_upgrade)
1699  dobj->dump = ext->dobj.dump;
1700  else
1701  {
1702  if (fout->remoteVersion < 90600)
1703  dobj->dump = DUMP_COMPONENT_NONE;
1704  else
1705  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1708  }
1709 
1710  return true;
1711 }
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:1012
#define DUMP_COMPONENT_POLICY
Definition: pg_dump.h:101
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:99
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:100
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:95
DumpOptions * dopt
Definition: pg_backup.h:212
int binary_upgrade
Definition: pg_backup.h:164
DumpComponents dump
Definition: pg_dump.h:138
DumpComponents dump_contains
Definition: pg_dump.h:140
DumpableObject dobj
Definition: pg_dump.h:180

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

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

◆ collectComments()

static void collectComments ( Archive fout)
static

Definition at line 10171 of file pg_dump.c.

10172 {
10173  PGresult *res;
10174  PQExpBuffer query;
10175  int i_description;
10176  int i_classoid;
10177  int i_objoid;
10178  int i_objsubid;
10179  int ntups;
10180  int i;
10181  DumpableObject *dobj;
10182 
10183  query = createPQExpBuffer();
10184 
10185  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
10186  "FROM pg_catalog.pg_description "
10187  "ORDER BY classoid, objoid, objsubid");
10188 
10189  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10190 
10191  /* Construct lookup table containing OIDs in numeric form */
10192 
10193  i_description = PQfnumber(res, "description");
10194  i_classoid = PQfnumber(res, "classoid");
10195  i_objoid = PQfnumber(res, "objoid");
10196  i_objsubid = PQfnumber(res, "objsubid");
10197 
10198  ntups = PQntuples(res);
10199 
10200  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10201  ncomments = 0;
10202  dobj = NULL;
10203 
10204  for (i = 0; i < ntups; i++)
10205  {
10206  CatalogId objId;
10207  int subid;
10208 
10209  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
10210  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
10211  subid = atoi(PQgetvalue(res, i, i_objsubid));
10212 
10213  /* We needn't remember comments that don't match any dumpable object */
10214  if (dobj == NULL ||
10215  dobj->catId.tableoid != objId.tableoid ||
10216  dobj->catId.oid != objId.oid)
10217  dobj = findObjectByCatalogId(objId);
10218  if (dobj == NULL)
10219  continue;
10220 
10221  /*
10222  * Comments on columns of composite types are linked to the type's
10223  * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
10224  * in the type's own DumpableObject.
10225  */
10226  if (subid != 0 && dobj->objType == DO_TABLE &&
10227  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
10228  {
10229  TypeInfo *cTypeInfo;
10230 
10231  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
10232  if (cTypeInfo)
10233  cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
10234  }
10235  else
10236  dobj->components |= DUMP_COMPONENT_COMMENT;
10237 
10238  comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
10240  comments[ncomments].objoid = objId.oid;
10241  comments[ncomments].objsubid = subid;
10242  ncomments++;
10243  }
10244 
10245  PQclear(res);
10246  destroyPQExpBuffer(query);
10247 }
TypeInfo * findTypeByOid(Oid oid)
Definition: common.c:860
static int ncomments
Definition: pg_dump.c:150
static CommentItem * comments
Definition: pg_dump.c:149
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:98
Oid classoid
Definition: pg_dump.c:80
Oid objoid
Definition: pg_dump.c:81
int objsubid
Definition: pg_dump.c:82
const char * descr
Definition: pg_dump.c:79
DumpComponents components
Definition: pg_dump.h:141
DumpableObject dobj
Definition: pg_dump.h:190

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

9717 {
9718  PGresult *res;
9719  const char *query;
9720  int i;
9721 
9722  query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
9723 
9724  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
9725 
9727 
9729 
9730  for (i = 0; i < nrolenames; i++)
9731  {
9734  }
9735 
9736  PQclear(res);
9737 }
static RoleNameItem * rolenames
Definition: pg_dump.c:145
static int nrolenames
Definition: pg_dump.c:146
const char * rolename
Definition: pg_dump.c:74
Oid roleoid
Definition: pg_dump.c:73

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

15287 {
15288  PGresult *res;
15289  PQExpBuffer query;
15290  int i_label;
15291  int i_provider;
15292  int i_classoid;
15293  int i_objoid;
15294  int i_objsubid;
15295  int ntups;
15296  int i;
15297  DumpableObject *dobj;
15298 
15299  query = createPQExpBuffer();
15300 
15301  appendPQExpBufferStr(query,
15302  "SELECT label, provider, classoid, objoid, objsubid "
15303  "FROM pg_catalog.pg_seclabel "
15304  "ORDER BY classoid, objoid, objsubid");
15305 
15306  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15307 
15308  /* Construct lookup table containing OIDs in numeric form */
15309  i_label = PQfnumber(res, "label");
15310  i_provider = PQfnumber(res, "provider");
15311  i_classoid = PQfnumber(res, "classoid");
15312  i_objoid = PQfnumber(res, "objoid");
15313  i_objsubid = PQfnumber(res, "objsubid");
15314 
15315  ntups = PQntuples(res);
15316 
15317  seclabels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15318  nseclabels = 0;
15319  dobj = NULL;
15320 
15321  for (i = 0; i < ntups; i++)
15322  {
15323  CatalogId objId;
15324  int subid;
15325 
15326  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
15327  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
15328  subid = atoi(PQgetvalue(res, i, i_objsubid));
15329 
15330  /* We needn't remember labels that don't match any dumpable object */
15331  if (dobj == NULL ||
15332  dobj->catId.tableoid != objId.tableoid ||
15333  dobj->catId.oid != objId.oid)
15334  dobj = findObjectByCatalogId(objId);
15335  if (dobj == NULL)
15336  continue;
15337 
15338  /*
15339  * Labels on columns of composite types are linked to the type's
15340  * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
15341  * in the type's own DumpableObject.
15342  */
15343  if (subid != 0 && dobj->objType == DO_TABLE &&
15344  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
15345  {
15346  TypeInfo *cTypeInfo;
15347 
15348  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
15349  if (cTypeInfo)
15350  cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
15351  }
15352  else
15353  dobj->components |= DUMP_COMPONENT_SECLABEL;
15354 
15358  seclabels[nseclabels].objoid = objId.oid;
15359  seclabels[nseclabels].objsubid = subid;
15360  nseclabels++;
15361  }
15362 
15363  PQclear(res);
15364  destroyPQExpBuffer(query);
15365 }
static int nseclabels
Definition: pg_dump.c:154
static SecLabelItem * seclabels
Definition: pg_dump.c:153
const char * provider
Definition: pg_dump.c:87
Oid classoid
Definition: pg_dump.c:89
int objsubid
Definition: pg_dump.c:91
const char * label
Definition: pg_dump.c:88
Oid objoid
Definition: pg_dump.c:90

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

12855 {
12856  char *name;
12857  char *paren;
12858  bool inquote;
12859 
12860  /* In all cases "-" means a null reference */
12861  if (strcmp(proc, "-") == 0)
12862  return NULL;
12863 
12864  name = pg_strdup(proc);
12865  /* find non-double-quoted left paren */
12866  inquote = false;
12867  for (paren = name; *paren; paren++)
12868  {
12869  if (*paren == '(' && !inquote)
12870  {
12871  *paren = '\0';
12872  break;
12873  }
12874  if (*paren == '"')
12875  inquote = !inquote;
12876  }
12877  return name;
12878 }
const char * name

References name, and pg_strdup().

Referenced by dumpOpr().

◆ convertTSFunction()

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

Definition at line 12925 of file pg_dump.c.

12926 {
12927  char *result;
12928  char query[128];
12929  PGresult *res;
12930 
12931  snprintf(query, sizeof(query),
12932  "SELECT '%u'::pg_catalog.regproc", funcOid);
12933  res = ExecuteSqlQueryForSingleRow(fout, query);
12934 
12935  result = pg_strdup(PQgetvalue(res, 0, 0));
12936 
12937  PQclear(res);
12938 
12939  return result;
12940 }
#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 18405 of file pg_dump.c.

18406 {
18407  DumpableObject *dobjs;
18408 
18409  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18410 
18411  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18412  dobjs[0].catId = nilCatalogId;
18413  AssignDumpId(dobjs + 0);
18414  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18415 
18416  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18417  dobjs[1].catId = nilCatalogId;
18418  AssignDumpId(dobjs + 1);
18419  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18420 
18421  return dobjs;
18422 }
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:642
static const CatalogId nilCatalogId
Definition: pg_dump.c:138

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

15562 {
15563  PQExpBuffer result = createPQExpBuffer();
15564  int j;
15565 
15566  appendPQExpBufferStr(result, "SELECT");
15567 
15568  for (j = 0; j < tbinfo->numatts; j++)
15569  {
15570  if (j > 0)
15571  appendPQExpBufferChar(result, ',');
15572  appendPQExpBufferStr(result, "\n ");
15573 
15574  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15575 
15576  /*
15577  * Must add collation if not default for the type, because CREATE OR
15578  * REPLACE VIEW won't change it
15579  */
15580  if (OidIsValid(tbinfo->attcollation[j]))
15581  {
15582  CollInfo *coll;
15583 
15584  coll = findCollationByOid(tbinfo->attcollation[j]);
15585  if (coll)
15586  appendPQExpBuffer(result, " COLLATE %s",
15587  fmtQualifiedDumpable(coll));
15588  }
15589 
15590  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15591  }
15592 
15593  return result;
15594 }
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:915
int j
Definition: isn.c:74
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:165
int numatts
Definition: pg_dump.h:331
Oid * attcollation
Definition: pg_dump.h:344
char ** atttypnames
Definition: pg_dump.h:333
char ** attnames
Definition: pg_dump.h:332

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

15513 {
15514  PQExpBuffer query = createPQExpBuffer();
15515  PQExpBuffer result = createPQExpBuffer();
15516  PGresult *res;
15517  int len;
15518 
15519  /* Fetch the view definition */
15520  appendPQExpBuffer(query,
15521  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15522  tbinfo->dobj.catId.oid);
15523 
15524  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15525 
15526  if (PQntuples(res) != 1)
15527  {
15528  if (PQntuples(res) < 1)
15529  pg_fatal("query to obtain definition of view \"%s\" returned no data",
15530  tbinfo->dobj.name);
15531  else
15532  pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
15533  tbinfo->dobj.name);
15534  }
15535 
15536  len = PQgetlength(res, 0, 0);
15537 
15538  if (len == 0)
15539  pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
15540  tbinfo->dobj.name);
15541 
15542  /* Strip off the trailing semicolon so that other things may follow. */
15543  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15544  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15545 
15546  PQclear(res);
15547  destroyPQExpBuffer(query);
15548 
15549  return result;
15550 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3801
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:284

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

12948 {
12949  DumpOptions *dopt = fout->dopt;
12950  PQExpBuffer q;
12951  PQExpBuffer delq;
12952  char *qamname;
12953 
12954  /* Do nothing in data-only dump */
12955  if (dopt->dataOnly)
12956  return;
12957 
12958  q = createPQExpBuffer();
12959  delq = createPQExpBuffer();
12960 
12961  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12962 
12963  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12964 
12965  switch (aminfo->amtype)
12966  {
12967  case AMTYPE_INDEX:
12968  appendPQExpBufferStr(q, "TYPE INDEX ");
12969  break;
12970  case AMTYPE_TABLE:
12971  appendPQExpBufferStr(q, "TYPE TABLE ");
12972  break;
12973  default:
12974  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12975  aminfo->amtype, qamname);
12976  destroyPQExpBuffer(q);
12977  destroyPQExpBuffer(delq);
12978  free(qamname);
12979  return;
12980  }
12981 
12982  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12983 
12984  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12985  qamname);
12986 
12987  if (dopt->binary_upgrade)
12989  "ACCESS METHOD", qamname, NULL);
12990 
12991  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12992  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12993  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12994  .description = "ACCESS METHOD",
12995  .section = SECTION_PRE_DATA,
12996  .createStmt = q->data,
12997  .dropStmt = delq->data));
12998 
12999  /* Dump Access Method Comments */
13000  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13001  dumpComment(fout, "ACCESS METHOD", qamname,
13002  NULL, "",
13003  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
13004 
13005  destroyPQExpBuffer(q);
13006  destroyPQExpBuffer(delq);
13007  free(qamname);
13008 }
@ 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:9980
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:5131
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:96
char * amhandler
Definition: pg_dump.h:252
DumpableObject dobj
Definition: pg_dump.h:250
bool dataOnly
Definition: pg_backup.h:168

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

Referenced by dumpDumpableObject().

◆ dumpACL()

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

Definition at line 14919 of file pg_dump.c.

14923 {
14924  DumpId aclDumpId = InvalidDumpId;
14925  DumpOptions *dopt = fout->dopt;
14926  const char *acls = dacl->acl;
14927  const char *acldefault = dacl->acldefault;
14928  char privtype = dacl->privtype;
14929  const char *initprivs = dacl->initprivs;
14930  const char *baseacls;
14931  PQExpBuffer sql;
14932 
14933  /* Do nothing if ACL dump is not enabled */
14934  if (dopt->aclsSkip)
14935  return InvalidDumpId;
14936 
14937  /* --data-only skips ACLs *except* large object ACLs */
14938  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14939  return InvalidDumpId;
14940 
14941  sql = createPQExpBuffer();
14942 
14943  /*
14944  * In binary upgrade mode, we don't run an extension's script but instead
14945  * dump out the objects independently and then recreate them. To preserve
14946  * any initial privileges which were set on extension objects, we need to
14947  * compute the set of GRANT and REVOKE commands necessary to get from the
14948  * default privileges of an object to its initial privileges as recorded
14949  * in pg_init_privs.
14950  *
14951  * At restore time, we apply these commands after having called
14952  * binary_upgrade_set_record_init_privs(true). That tells the backend to
14953  * copy the results into pg_init_privs. This is how we preserve the
14954  * contents of that catalog across binary upgrades.
14955  */
14956  if (dopt->binary_upgrade && privtype == 'e' &&
14957  initprivs && *initprivs != '\0')
14958  {
14959  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14960  if (!buildACLCommands(name, subname, nspname, type,
14961  initprivs, acldefault, owner,
14962  "", fout->remoteVersion, sql))
14963  pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
14964  initprivs, acldefault, name, type);
14965  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14966  }
14967 
14968  /*
14969  * Now figure the GRANT and REVOKE commands needed to get to the object's
14970  * actual current ACL, starting from the initprivs if given, else from the
14971  * object-type-specific default. Also, while buildACLCommands will assume
14972  * that a NULL/empty acls string means it needn't do anything, what that
14973  * actually represents is the object-type-specific default; so we need to
14974  * substitute the acldefault string to get the right results in that case.
14975  */
14976  if (initprivs && *initprivs != '\0')
14977  {
14978  baseacls = initprivs;
14979  if (acls == NULL || *acls == '\0')
14980  acls = acldefault;
14981  }
14982  else
14983  baseacls = acldefault;
14984 
14985  if (!buildACLCommands(name, subname, nspname, type,
14986  acls, baseacls, owner,
14987  "", fout->remoteVersion, sql))
14988  pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
14989  acls, baseacls, name, type);
14990 
14991  if (sql->len > 0)
14992  {
14994  DumpId aclDeps[2];
14995  int nDeps = 0;
14996 
14997  if (subname)
14998  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
14999  else
15000  appendPQExpBuffer(tag, "%s %s", type, name);
15001 
15002  aclDeps[nDeps++] = objDumpId;
15003  if (altDumpId != InvalidDumpId)
15004  aclDeps[nDeps++] = altDumpId;
15005 
15006  aclDumpId = createDumpId();
15007 
15008  ArchiveEntry(fout, nilCatalogId, aclDumpId,
15009  ARCHIVE_OPTS(.tag = tag->data,
15010  .namespace = nspname,
15011  .owner = owner,
15012  .description = "ACL",
15013  .section = SECTION_NONE,
15014  .createStmt = sql->data,
15015  .deps = aclDeps,
15016  .nDeps = nDeps));
15017 
15018  destroyPQExpBuffer(tag);
15019  }
15020 
15021  destroyPQExpBuffer(sql);
15022 
15023  return aclDumpId;
15024 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:777
DumpId createDumpId(void)
Definition: common.c:706
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:64
@ SECTION_NONE
Definition: pg_backup.h:57
#define InvalidDumpId
Definition: pg_backup.h:270
NameData subname
bool aclsSkip
Definition: pg_backup.h:170
char privtype
Definition: pg_dump.h:158
char * acldefault
Definition: pg_dump.h:156
char * acl
Definition: pg_dump.h:155
char * initprivs
Definition: pg_dump.h:159
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 13886 of file pg_dump.c.

13887 {
13888  DumpOptions *dopt = fout->dopt;
13889  PQExpBuffer query;
13890  PQExpBuffer q;
13891  PQExpBuffer delq;
13892  PQExpBuffer details;
13893  char *aggsig; /* identity signature */
13894  char *aggfullsig = NULL; /* full signature */
13895  char *aggsig_tag;
13896  PGresult *res;
13897  int i_agginitval;
13898  int i_aggminitval;
13899  const char *aggtransfn;
13900  const char *aggfinalfn;
13901  const char *aggcombinefn;
13902  const char *aggserialfn;
13903  const char *aggdeserialfn;
13904  const char *aggmtransfn;
13905  const char *aggminvtransfn;
13906  const char *aggmfinalfn;
13907  bool aggfinalextra;
13908  bool aggmfinalextra;
13909  char aggfinalmodify;
13910  char aggmfinalmodify;
13911  const char *aggsortop;
13912  char *aggsortconvop;
13913  char aggkind;
13914  const char *aggtranstype;
13915  const char *aggtransspace;
13916  const char *aggmtranstype;
13917  const char *aggmtransspace;
13918  const char *agginitval;
13919  const char *aggminitval;
13920  const char *proparallel;
13921  char defaultfinalmodify;
13922 
13923  /* Do nothing in data-only dump */
13924  if (dopt->dataOnly)
13925  return;
13926 
13927  query = createPQExpBuffer();
13928  q = createPQExpBuffer();
13929  delq = createPQExpBuffer();
13930  details = createPQExpBuffer();
13931 
13932  if (!fout->is_prepared[PREPQUERY_DUMPAGG])
13933  {
13934  /* Set up query for aggregate-specific details */
13935  appendPQExpBufferStr(query,
13936  "PREPARE dumpAgg(pg_catalog.oid) AS\n");
13937 
13938  appendPQExpBufferStr(query,
13939  "SELECT "
13940  "aggtransfn,\n"
13941  "aggfinalfn,\n"
13942  "aggtranstype::pg_catalog.regtype,\n"
13943  "agginitval,\n"
13944  "aggsortop,\n"
13945  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13946  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
13947 
13948  if (fout->remoteVersion >= 90400)
13949  appendPQExpBufferStr(query,
13950  "aggkind,\n"
13951  "aggmtransfn,\n"
13952  "aggminvtransfn,\n"
13953  "aggmfinalfn,\n"
13954  "aggmtranstype::pg_catalog.regtype,\n"
13955  "aggfinalextra,\n"
13956  "aggmfinalextra,\n"
13957  "aggtransspace,\n"
13958  "aggmtransspace,\n"
13959  "aggminitval,\n");
13960  else
13961  appendPQExpBufferStr(query,
13962  "'n' AS aggkind,\n"
13963  "'-' AS aggmtransfn,\n"
13964  "'-' AS aggminvtransfn,\n"
13965  "'-' AS aggmfinalfn,\n"
13966  "0 AS aggmtranstype,\n"
13967  "false AS aggfinalextra,\n"
13968  "false AS aggmfinalextra,\n"
13969  "0 AS aggtransspace,\n"
13970  "0 AS aggmtransspace,\n"
13971  "NULL AS aggminitval,\n");
13972 
13973  if (fout->remoteVersion >= 90600)
13974  appendPQExpBufferStr(query,
13975  "aggcombinefn,\n"
13976  "aggserialfn,\n"
13977  "aggdeserialfn,\n"
13978  "proparallel,\n");
13979  else
13980  appendPQExpBufferStr(query,
13981  "'-' AS aggcombinefn,\n"
13982  "'-' AS aggserialfn,\n"
13983  "'-' AS aggdeserialfn,\n"
13984  "'u' AS proparallel,\n");
13985 
13986  if (fout->remoteVersion >= 110000)
13987  appendPQExpBufferStr(query,
13988  "aggfinalmodify,\n"
13989  "aggmfinalmodify\n");
13990  else
13991  appendPQExpBufferStr(query,
13992  "'0' AS aggfinalmodify,\n"
13993  "'0' AS aggmfinalmodify\n");
13994 
13995  appendPQExpBufferStr(query,
13996  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13997  "WHERE a.aggfnoid = p.oid "
13998  "AND p.oid = $1");
13999 
14000  ExecuteSqlStatement(fout, query->data);
14001 
14002  fout->is_prepared[PREPQUERY_DUMPAGG] = true;
14003  }
14004 
14005  printfPQExpBuffer(query,
14006  "EXECUTE dumpAgg('%u')",
14007  agginfo->aggfn.dobj.catId.oid);
14008 
14009  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14010 
14011  i_agginitval = PQfnumber(res, "agginitval");
14012  i_aggminitval = PQfnumber(res, "aggminitval");
14013 
14014  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
14015  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
14016  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
14017  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
14018  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
14019  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
14020  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
14021  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
14022  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
14023  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
14024  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
14025  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
14026  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
14027  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
14028  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
14029  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
14030  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
14031  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
14032  agginitval = PQgetvalue(res, 0, i_agginitval);
14033  aggminitval = PQgetvalue(res, 0, i_aggminitval);
14034  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
14035 
14036  {
14037  char *funcargs;
14038  char *funciargs;
14039 
14040  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
14041  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
14042  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
14043  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
14044  }
14045 
14046  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14047 
14048  /* identify default modify flag for aggkind (must match DefineAggregate) */
14049  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14050  /* replace omitted flags for old versions */
14051  if (aggfinalmodify == '0')
14052  aggfinalmodify = defaultfinalmodify;
14053  if (aggmfinalmodify == '0')
14054  aggmfinalmodify = defaultfinalmodify;
14055 
14056  /* regproc and regtype output is already sufficiently quoted */
14057  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14058  aggtransfn, aggtranstype);
14059 
14060  if (strcmp(aggtransspace, "0") != 0)
14061  {
14062  appendPQExpBuffer(details, ",\n SSPACE = %s",
14063  aggtransspace);
14064  }
14065 
14066  if (!PQgetisnull(res, 0, i_agginitval))
14067  {
14068  appendPQExpBufferStr(details, ",\n INITCOND = ");
14069  appendStringLiteralAH(details, agginitval, fout);
14070  }
14071 
14072  if (strcmp(aggfinalfn, "-") != 0)
14073  {
14074  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14075  aggfinalfn);
14076  if (aggfinalextra)
14077  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14078  if (aggfinalmodify != defaultfinalmodify)
14079  {
14080  switch (aggfinalmodify)
14081  {
14082  case AGGMODIFY_READ_ONLY:
14083  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14084  break;
14085  case AGGMODIFY_SHAREABLE:
14086  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14087  break;
14088  case AGGMODIFY_READ_WRITE:
14089  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14090  break;
14091  default:
14092  pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14093  agginfo->aggfn.dobj.name);
14094  break;
14095  }
14096  }
14097  }
14098 
14099  if (strcmp(aggcombinefn, "-") != 0)
14100  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14101 
14102  if (strcmp(aggserialfn, "-") != 0)
14103  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14104 
14105  if (strcmp(aggdeserialfn, "-") != 0)
14106  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14107 
14108  if (strcmp(aggmtransfn, "-") != 0)
14109  {
14110  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14111  aggmtransfn,
14112  aggminvtransfn,
14113  aggmtranstype);
14114  }
14115 
14116  if (strcmp(aggmtransspace, "0") != 0)
14117  {
14118  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14119  aggmtransspace);
14120  }
14121 
14122  if (!PQgetisnull(res, 0, i_aggminitval))
14123  {
14124  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14125  appendStringLiteralAH(details, aggminitval, fout);
14126  }
14127 
14128  if (strcmp(aggmfinalfn, "-") != 0)
14129  {
14130  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14131  aggmfinalfn);
14132  if (aggmfinalextra)
14133  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14134  if (aggmfinalmodify != defaultfinalmodify)
14135  {
14136  switch (aggmfinalmodify)
14137  {
14138  case AGGMODIFY_READ_ONLY:
14139  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14140  break;
14141  case AGGMODIFY_SHAREABLE:
14142  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14143  break;
14144  case AGGMODIFY_READ_WRITE:
14145  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14146  break;
14147  default:
14148  pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14149  agginfo->aggfn.dobj.name);
14150  break;
14151  }
14152  }
14153  }
14154 
14155  aggsortconvop = getFormattedOperatorName(aggsortop);
14156  if (aggsortconvop)
14157  {
14158  appendPQExpBuffer(details, ",\n SORTOP = %s",
14159  aggsortconvop);
14160  free(aggsortconvop);
14161  }
14162 
14163  if (aggkind == AGGKIND_HYPOTHETICAL)
14164  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14165 
14166  if (proparallel[0] != PROPARALLEL_UNSAFE)
14167  {
14168  if (proparallel[0] == PROPARALLEL_SAFE)
14169  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14170  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14171  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14172  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14173  pg_fatal("unrecognized proparallel value for function \"%s\"",
14174  agginfo->aggfn.dobj.name);
14175  }
14176 
14177  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14178  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14179  aggsig);
14180 
14181  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14182  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14183  aggfullsig ? aggfullsig : aggsig, details->data);
14184 
14185  if (dopt->binary_upgrade)
14186  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
14187  "AGGREGATE", aggsig,
14188  agginfo->aggfn.dobj.namespace->dobj.name);
14189 
14190  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14191  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14192  agginfo->aggfn.dobj.dumpId,
14193  ARCHIVE_OPTS(.tag = aggsig_tag,
14194  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14195  .owner = agginfo->aggfn.rolname,
14196  .description = "AGGREGATE",
14197  .section = SECTION_PRE_DATA,
14198  .createStmt = q->data,
14199  .dropStmt = delq->data));
14200 
14201  /* Dump Aggregate Comments */
14202  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14203  dumpComment(fout, "AGGREGATE", aggsig,
14204  agginfo->aggfn.dobj.namespace->dobj.name,
14205  agginfo->aggfn.rolname,
14206  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14207 
14208  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14209  dumpSecLabel(fout, "AGGREGATE", aggsig,
14210  agginfo->aggfn.dobj.namespace->dobj.name,
14211  agginfo->aggfn.rolname,
14212  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14213 
14214  /*
14215  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14216  * command look like a function's GRANT; in particular this affects the
14217  * syntax for zero-argument aggregates and ordered-set aggregates.
14218  */
14219  free(aggsig);
14220 
14221  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14222 
14223  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14224  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
14225  "FUNCTION", aggsig, NULL,
14226  agginfo->aggfn.dobj.namespace->dobj.name,
14227  agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
14228 
14229  free(aggsig);
14230  free(aggfullsig);
14231  free(aggsig_tag);
14232 
14233  PQclear(res);
14234 
14235  destroyPQExpBuffer(query);
14236  destroyPQExpBuffer(q);
14237  destroyPQExpBuffer(delq);
14238  destroyPQExpBuffer(details);
14239 }
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3815
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:330
@ PREPQUERY_DUMPAGG
Definition: pg_backup.h:66
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:278
static char * getFormattedOperatorName(const char *oproid)
Definition: pg_dump.c:12895
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11956
static char * format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:13854
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const DumpableAcl *dacl)
Definition: pg_dump.c:14919
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:11933
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:15045
bool * is_prepared
Definition: pg_backup.h:239

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

Referenced by dumpDumpableObject().

◆ dumpAttrDef()

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

Definition at line 16507 of file pg_dump.c.

16508 {
16509  DumpOptions *dopt = fout->dopt;
16510  TableInfo *tbinfo = adinfo->adtable;
16511  int adnum = adinfo->adnum;
16512  PQExpBuffer q;
16513  PQExpBuffer delq;
16514  char *qualrelname;
16515  char *tag;
16516  char *foreign;
16517 
16518  /* Do nothing in data-only dump */
16519  if (dopt->dataOnly)
16520  return;
16521 
16522  /* Skip if not "separate"; it was dumped in the table's definition */
16523  if (!adinfo->separate)
16524  return;
16525 
16526  q = createPQExpBuffer();
16527  delq = createPQExpBuffer();
16528 
16529  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16530 
16531  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16532 
16534  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16535  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16536  adinfo->adef_expr);
16537 
16538  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16539  foreign, qualrelname,
16540  fmtId(tbinfo->attnames[adnum - 1]));
16541 
16542  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16543 
16544  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16545  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16546  ARCHIVE_OPTS(.tag = tag,
16547  .namespace = tbinfo->dobj.namespace->dobj.name,
16548  .owner = tbinfo->rolname,
16549  .description = "DEFAULT",
16550  .section = SECTION_PRE_DATA,
16551  .createStmt = q->data,
16552  .dropStmt = delq->data));
16553 
16554  free(tag);
16555  destroyPQExpBuffer(q);
16556  destroyPQExpBuffer(delq);
16557  free(qualrelname);
16558 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
DumpableObject dobj
Definition: pg_dump.h:379
char * adef_expr
Definition: pg_dump.h:382
TableInfo * adtable
Definition: pg_dump.h:380
bool separate
Definition: pg_dump.h:383
const char * rolname
Definition: pg_dump.h:286

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

11039 {
11040  DumpOptions *dopt = fout->dopt;
11042  PQExpBuffer delq = createPQExpBuffer();
11043  PQExpBuffer query = createPQExpBuffer();
11044  PGresult *res;
11045  char *qtypname;
11046  char *qualtypname;
11047  char *typlen;
11048  char *typinput;
11049  char *typoutput;
11050  char *typreceive;
11051  char *typsend;
11052  char *typmodin;
11053  char *typmodout;
11054  char *typanalyze;
11055  char *typsubscript;
11056  Oid typreceiveoid;
11057  Oid typsendoid;
11058  Oid typmodinoid;
11059  Oid typmodoutoid;
11060  Oid typanalyzeoid;
11061  Oid typsubscriptoid;
11062  char *typcategory;
11063  char *typispreferred;
11064  char *typdelim;
11065  char *typbyval;
11066  char *typalign;
11067  char *typstorage;
11068  char *typcollatable;
11069  char *typdefault;
11070  bool typdefault_is_literal = false;
11071 
11072  if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
11073  {
11074  /* Set up query for type-specific details */
11075  appendPQExpBufferStr(query,
11076  "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
11077  "SELECT typlen, "
11078  "typinput, typoutput, typreceive, typsend, "
11079  "typreceive::pg_catalog.oid AS typreceiveoid, "
11080  "typsend::pg_catalog.oid AS typsendoid, "
11081  "typanalyze, "
11082  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
11083  "typdelim, typbyval, typalign, typstorage, "
11084  "typmodin, typmodout, "
11085  "typmodin::pg_catalog.oid AS typmodinoid, "
11086  "typmodout::pg_catalog.oid AS typmodoutoid, "
11087  "typcategory, typispreferred, "
11088  "(typcollation <> 0) AS typcollatable, "
11089  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
11090 
11091  if (fout->remoteVersion >= 140000)
11092  appendPQExpBufferStr(query,
11093  "typsubscript, "
11094  "typsubscript::pg_catalog.oid AS typsubscriptoid ");
11095  else
11096  appendPQExpBufferStr(query,
11097  "'-' AS typsubscript, 0 AS typsubscriptoid ");
11098 
11099  appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
11100  "WHERE oid = $1");
11101 
11102  ExecuteSqlStatement(fout, query->data);
11103 
11104  fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
11105  }
11106 
11107  printfPQExpBuffer(query,
11108  "EXECUTE dumpBaseType('%u')",
11109  tyinfo->dobj.catId.oid);
11110 
11111  res = ExecuteSqlQueryForSingleRow(fout, query->data);
11112 
11113  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
11114  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
11115  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
11116  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
11117  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
11118  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
11119  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
11120  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
11121  typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
11122  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
11123  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
11124  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
11125  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
11126  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
11127  typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
11128  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
11129  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
11130  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
11131  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
11132  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
11133  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
11134  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
11135  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
11136  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
11137  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
11138  {
11139  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
11140  typdefault_is_literal = true; /* it needs quotes */
11141  }
11142  else
11143  typdefault = NULL;
11144 
11145  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11146  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11147 
11148  /*
11149  * The reason we include CASCADE is that the circular dependency between
11150  * the type and its I/O functions makes it impossible to drop the type any
11151  * other way.
11152  */
11153  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
11154 
11155  /*
11156  * We might already have a shell type, but setting pg_type_oid is
11157  * harmless, and in any case we'd better set the array type OID.
11158  */
11159  if (dopt->binary_upgrade)
11161  tyinfo->dobj.catId.oid,
11162  false, false);
11163 
11165  "CREATE TYPE %s (\n"
11166  " INTERNALLENGTH = %s",
11167  qualtypname,
11168  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
11169 
11170  /* regproc result is sufficiently quoted already */
11171  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
11172  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
11173  if (OidIsValid(typreceiveoid))
11174  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
11175  if (OidIsValid(typsendoid))
11176  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
11177  if (OidIsValid(typmodinoid))
11178  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
11179  if (OidIsValid(typmodoutoid))
11180  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
11181  if (OidIsValid(typanalyzeoid))
11182  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
11183 
11184  if (strcmp(typcollatable, "t") == 0)
11185  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
11186 
11187  if (typdefault != NULL)
11188  {
11189  appendPQExpBufferStr(q, ",\n DEFAULT = ");
11190  if (typdefault_is_literal)
11191  appendStringLiteralAH(q, typdefault, fout);
11192  else
11193  appendPQExpBufferStr(q, typdefault);
11194  }
11195 
11196  if (OidIsValid(typsubscriptoid))
11197  appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
11198 
11199  if (OidIsValid(tyinfo->typelem))
11200  appendPQExpBuffer(q, ",\n ELEMENT = %s",
11201  getFormattedTypeName(fout, tyinfo->typelem,
11202  zeroIsError));
11203 
11204  if (strcmp(typcategory, "U") != 0)
11205  {
11206  appendPQExpBufferStr(q, ",\n CATEGORY = ");
11207  appendStringLiteralAH(q, typcategory, fout);
11208  }
11209 
11210  if (strcmp(typispreferred, "t") == 0)
11211  appendPQExpBufferStr(q, ",\n PREFERRED = true");
11212 
11213  if (typdelim && strcmp(typdelim, ",") != 0)
11214  {
11215  appendPQExpBufferStr(q, ",\n DELIMITER = ");
11216  appendStringLiteralAH(q, typdelim, fout);
11217  }
11218 
11219  if (*typalign == TYPALIGN_CHAR)
11220  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
11221  else if (*typalign == TYPALIGN_SHORT)
11222  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
11223  else if (*typalign == TYPALIGN_INT)
11224  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
11225  else if (*typalign == TYPALIGN_DOUBLE)
11226  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
11227 
11228  if (*typstorage == TYPSTORAGE_PLAIN)
11229  appendPQExpBufferStr(q, ",\n STORAGE = plain");
11230  else if (*typstorage == TYPSTORAGE_EXTERNAL)
11231  appendPQExpBufferStr(q, ",\n STORAGE = external");
11232  else if (*typstorage == TYPSTORAGE_EXTENDED)
11233  appendPQExpBufferStr(q, ",\n STORAGE = extended");
11234  else if (*typstorage == TYPSTORAGE_MAIN)
11235  appendPQExpBufferStr(q, ",\n STORAGE = main");
11236 
11237  if (strcmp(typbyval, "t") == 0)
11238  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
11239 
11240  appendPQExpBufferStr(q, "\n);\n");
11241 
11242  if (dopt->binary_upgrade)
11244  "TYPE", qtypname,
11245  tyinfo->dobj.namespace->dobj.name);
11246 
11247  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11248  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11249  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11250  .namespace = tyinfo->dobj.namespace->dobj.name,
11251  .owner = tyinfo->rolname,
11252  .description = "TYPE",
11253  .section = SECTION_PRE_DATA,
11254  .createStmt = q->data,
11255  .dropStmt = delq->data));
11256 
11257  /* Dump Type Comments and Security Labels */
11258  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11259  dumpComment(fout, "TYPE", qtypname,
11260  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11261  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11262 
11263  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11264  dumpSecLabel(fout, "TYPE", qtypname,
11265  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11266  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11267 
11268  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11269  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11270  qtypname, NULL,
11271  tyinfo->dobj.namespace->dobj.name,
11272  tyinfo->rolname, &tyinfo->dacl);
11273 
11274  PQclear(res);
11275  destroyPQExpBuffer(q);
11276  destroyPQExpBuffer(delq);
11277  destroyPQExpBuffer(query);
11278  free(qtypname);
11279  free(qualtypname);
11280 }
@ PREPQUERY_DUMPBASETYPE
Definition: pg_backup.h:67
static const char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18648
char typalign
Definition: pg_type.h:176
DumpableAcl dacl
Definition: pg_dump.h:191
Oid typelem
Definition: pg_dump.h:200
const char * rolname
Definition: pg_dump.h:199

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

12402 {
12403  DumpOptions *dopt = fout->dopt;
12404  PQExpBuffer defqry;
12405  PQExpBuffer delqry;
12406  PQExpBuffer labelq;
12407  PQExpBuffer castargs;
12408  FuncInfo *funcInfo = NULL;
12409  const char *sourceType;
12410  const char *targetType;
12411 
12412  /* Do nothing in data-only dump */
12413  if (dopt->dataOnly)
12414  return;
12415 
12416  /* Cannot dump if we don't have the cast function's info */
12417  if (OidIsValid(cast->castfunc))
12418  {
12419  funcInfo = findFuncByOid(cast->castfunc);
12420  if (funcInfo == NULL)
12421  pg_fatal("could not find function definition for function with OID %u",
12422  cast->castfunc);
12423  }
12424 
12425  defqry = createPQExpBuffer();
12426  delqry = createPQExpBuffer();
12427  labelq = createPQExpBuffer();
12428  castargs = createPQExpBuffer();
12429 
12430  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12431  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12432  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12433  sourceType, targetType);
12434 
12435  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12436  sourceType, targetType);
12437 
12438  switch (cast->castmethod)
12439  {
12440  case COERCION_METHOD_BINARY:
12441  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12442  break;
12443  case COERCION_METHOD_INOUT:
12444  appendPQExpBufferStr(defqry, "WITH INOUT");
12445  break;
12446  case COERCION_METHOD_FUNCTION:
12447  if (funcInfo)
12448  {
12449  char *fsig = format_function_signature(fout, funcInfo, true);
12450 
12451  /*
12452  * Always qualify the function name (format_function_signature
12453  * won't qualify it).
12454  */
12455  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12456  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12457  free(fsig);
12458  }
12459  else
12460  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12461  break;
12462  default:
12463  pg_log_warning("bogus value in pg_cast.castmethod field");
12464  }
12465 
12466  if (cast->castcontext == 'a')
12467  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12468  else if (cast->castcontext == 'i')
12469  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12470  appendPQExpBufferStr(defqry, ";\n");
12471 
12472  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12473  sourceType, targetType);
12474 
12475  appendPQExpBuffer(castargs, "(%s AS %s)",
12476  sourceType, targetType);
12477 
12478  if (dopt->binary_upgrade)
12479  binary_upgrade_extension_member(defqry, &cast->dobj,
12480  "CAST", castargs->data, NULL);
12481 
12482  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12483  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12484  ARCHIVE_OPTS(.tag = labelq->data,
12485  .description = "CAST",
12486  .section = SECTION_PRE_DATA,
12487  .createStmt = defqry->data,
12488  .dropStmt = delqry->data));
12489 
12490  /* Dump Cast Comments */
12491  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12492  dumpComment(fout, "CAST", castargs->data,
12493  NULL, "",
12494  cast->dobj.catId, 0, cast->dobj.dumpId);
12495 
12496  destroyPQExpBuffer(defqry);
12497  destroyPQExpBuffer(delqry);
12498  destroyPQExpBuffer(labelq);
12499  destroyPQExpBuffer(castargs);
12500 }
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:879
char castmethod
Definition: pg_dump.h:512
Oid casttarget
Definition: pg_dump.h:509
char castcontext
Definition: pg_dump.h:511
DumpableObject dobj
Definition: pg_dump.h:507
Oid castsource
Definition: pg_dump.h:508
Oid castfunc
Definition: pg_dump.h:510
DumpableObject dobj
Definition: pg_dump.h:223

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

13516 {
13517  DumpOptions *dopt = fout->dopt;
13518  PQExpBuffer query;
13519  PQExpBuffer q;
13520  PQExpBuffer delq;
13521  char *qcollname;
13522  PGresult *res;
13523  int i_collprovider;
13524  int i_collisdeterministic;
13525  int i_collcollate;
13526  int i_collctype;
13527  int i_colliculocale;
13528  int i_collicurules;
13529  const char *collprovider;
13530  const char *collcollate;
13531  const char *collctype;
13532  const char *colliculocale;
13533  const char *collicurules;
13534 
13535  /* Do nothing in data-only dump */
13536  if (dopt->dataOnly)
13537  return;
13538 
13539  query = createPQExpBuffer();
13540  q = createPQExpBuffer();
13541  delq = createPQExpBuffer();
13542 
13543  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13544 
13545  /* Get collation-specific details */
13546  appendPQExpBufferStr(query, "SELECT ");
13547 
13548  if (fout->remoteVersion >= 100000)
13549  appendPQExpBufferStr(query,
13550  "collprovider, "
13551  "collversion, ");
13552  else
13553  appendPQExpBufferStr(query,
13554  "'c' AS collprovider, "
13555  "NULL AS collversion, ");
13556 
13557  if (fout->remoteVersion >= 120000)
13558  appendPQExpBufferStr(query,
13559  "collisdeterministic, ");
13560  else
13561  appendPQExpBufferStr(query,
13562  "true AS collisdeterministic, ");
13563 
13564  if (fout->remoteVersion >= 150000)
13565  appendPQExpBufferStr(query,
13566  "colliculocale, ");
13567  else
13568  appendPQExpBufferStr(query,
13569  "NULL AS colliculocale, ");
13570 
13571  if (fout->remoteVersion >= 160000)
13572  appendPQExpBufferStr(query,
13573  "collicurules, ");
13574  else
13575  appendPQExpBufferStr(query,
13576  "NULL AS collicurules, ");
13577 
13578  appendPQExpBuffer(query,
13579  "collcollate, "
13580  "collctype "
13581  "FROM pg_catalog.pg_collation c "
13582  "WHERE c.oid = '%u'::pg_catalog.oid",
13583  collinfo->dobj.catId.oid);
13584 
13585  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13586 
13587  i_collprovider = PQfnumber(res, "collprovider");
13588  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13589  i_collcollate = PQfnumber(res, "collcollate");
13590  i_collctype = PQfnumber(res, "collctype");
13591  i_colliculocale = PQfnumber(res, "colliculocale");
13592  i_collicurules = PQfnumber(res, "collicurules");
13593 
13594  collprovider = PQgetvalue(res, 0, i_collprovider);
13595 
13596  if (!PQgetisnull(res, 0, i_collcollate))
13597  collcollate = PQgetvalue(res, 0, i_collcollate);
13598  else
13599  collcollate = NULL;
13600 
13601  if (!PQgetisnull(res, 0, i_collctype))
13602  collctype = PQgetvalue(res, 0, i_collctype);
13603  else
13604  collctype = NULL;
13605 
13606  /*
13607  * Before version 15, collcollate and collctype were of type NAME and
13608  * non-nullable. Treat empty strings as NULL for consistency.
13609  */
13610  if (fout->remoteVersion < 150000)
13611  {
13612  if (collcollate[0] == '\0')
13613  collcollate = NULL;
13614  if (collctype[0] == '\0')
13615  collctype = NULL;
13616  }
13617 
13618  if (!PQgetisnull(res, 0, i_colliculocale))
13619  colliculocale = PQgetvalue(res, 0, i_colliculocale);
13620  else
13621  colliculocale = NULL;
13622 
13623  if (!PQgetisnull(res, 0, i_collicurules))
13624  collicurules = PQgetvalue(res, 0, i_collicurules);
13625  else
13626  collicurules = NULL;
13627 
13628  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13629  fmtQualifiedDumpable(collinfo));
13630 
13631  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13632  fmtQualifiedDumpable(collinfo));
13633 
13634  appendPQExpBufferStr(q, "provider = ");
13635  if (collprovider[0] == 'c')
13636  appendPQExpBufferStr(q, "libc");
13637  else if (collprovider[0] == 'i')
13638  appendPQExpBufferStr(q, "icu");
13639  else if (collprovider[0] == 'd')
13640  /* to allow dumping pg_catalog; not accepted on input */
13641  appendPQExpBufferStr(q, "default");
13642  else
13643  pg_fatal("unrecognized collation provider: %s",
13644  collprovider);
13645 
13646  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13647  appendPQExpBufferStr(q, ", deterministic = false");
13648 
13649  if (collprovider[0] == 'd')
13650  {
13651  if (collcollate || collctype || colliculocale || collicurules)
13652  pg_log_warning("invalid collation \"%s\"", qcollname);
13653 
13654  /* no locale -- the default collation cannot be reloaded anyway */
13655  }
13656  else if (collprovider[0] == 'i')
13657  {
13658  if (fout->remoteVersion >= 150000)
13659  {
13660  if (collcollate || collctype || !colliculocale)
13661  pg_log_warning("invalid collation \"%s\"", qcollname);
13662 
13663  appendPQExpBufferStr(q, ", locale = ");
13664  appendStringLiteralAH(q, colliculocale ? colliculocale : "",
13665  fout);
13666  }
13667  else
13668  {
13669  if (!collcollate || !collctype || colliculocale ||
13670  strcmp(collcollate, collctype) != 0)
13671  pg_log_warning("invalid collation \"%s\"", qcollname);
13672 
13673  appendPQExpBufferStr(q, ", locale = ");
13674  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13675  }
13676 
13677  if (collicurules)
13678  {
13679  appendPQExpBufferStr(q, ", rules = ");
13680  appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
13681  }
13682  }
13683  else if (collprovider[0] == 'c')
13684  {
13685  if (colliculocale || collicurules || !collcollate || !collctype)
13686  pg_log_warning("invalid collation \"%s\"", qcollname);
13687 
13688  if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
13689  {
13690  appendPQExpBufferStr(q, ", locale = ");
13691  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13692  }
13693  else
13694  {
13695  appendPQExpBufferStr(q, ", lc_collate = ");
13696  appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
13697  appendPQExpBufferStr(q, ", lc_ctype = ");
13698  appendStringLiteralAH(q, collctype ? collctype : "", fout);
13699  }
13700  }
13701  else
13702  pg_fatal("unrecognized collation provider: %s", collprovider);
13703 
13704  /*
13705  * For binary upgrade, carry over the collation version. For normal
13706  * dump/restore, omit the version, so that it is computed upon restore.
13707  */
13708  if (dopt->binary_upgrade)
13709  {
13710  int i_collversion;
13711 
13712  i_collversion = PQfnumber(res, "collversion");
13713  if (!PQgetisnull(res, 0, i_collversion))
13714  {
13715  appendPQExpBufferStr(q, ", version = ");
13717  PQgetvalue(res, 0, i_collversion),
13718  fout);
13719  }
13720  }
13721 
13722  appendPQExpBufferStr(q, ");\n");
13723 
13724  if (dopt->binary_upgrade)
13725  binary_upgrade_extension_member(q, &collinfo->dobj,
13726  "COLLATION", qcollname,
13727  collinfo->dobj.namespace->dobj.name);
13728 
13729  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13730  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13731  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13732  .namespace = collinfo->dobj.namespace->dobj.name,
13733  .owner = collinfo->rolname,
13734  .description = "COLLATION",
13735  .section = SECTION_PRE_DATA,
13736  .createStmt = q->data,
13737  .dropStmt = delq->data));
13738 
13739  /* Dump Collation Comments */
13740  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13741  dumpComment(fout, "COLLATION", qcollname,
13742  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13743  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13744 
13745  PQclear(res);
13746 
13747  destroyPQExpBuffer(query);
13748  destroyPQExpBuffer(q);
13749  destroyPQExpBuffer(delq);
13750  free(qcollname);
13751 }
const char * rolname
Definition: pg_dump.h:270
DumpableObject dobj
Definition: pg_dump.h:269

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

9984 {
9985  dumpCommentExtended(fout, type, name, namespace, owner,
9986  catalogId, subid, dumpId, NULL);
9987 }
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:9880

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

9885 {
9886  DumpOptions *dopt = fout->dopt;
9888  int ncomments;
9889 
9890  /* do nothing, if --no-comments is supplied */
9891  if (dopt->no_comments)
9892  return;
9893 
9894  /* Comments are schema not data ... except LO comments are data */
9895  if (strcmp(type, "LARGE OBJECT") != 0)
9896  {
9897  if (dopt->dataOnly)
9898  return;
9899  }
9900  else
9901  {
9902  /* We do dump LO comments in binary-upgrade mode */
9903  if (dopt->schemaOnly && !dopt->binary_upgrade)
9904  return;
9905  }
9906 
9907  /* Search for comments associated with catalogId, using table */
9908  ncomments = findComments(catalogId.tableoid, catalogId.oid,
9909  &comments);
9910 
9911  /* Is there one matching the subid? */
9912  while (ncomments > 0)
9913  {
9914  if (comments->objsubid == subid)
9915  break;
9916  comments++;
9917  ncomments--;
9918  }
9919 
9920  if (initdb_comment != NULL)
9921  {
9922  static CommentItem empty_comment = {.descr = ""};
9923 
9924  /*
9925  * initdb creates this object with a comment. Skip dumping the
9926  * initdb-provided comment, which would complicate matters for
9927  * non-superuser use of pg_dump. When the DBA has removed initdb's
9928  * comment, replicate that.
9929  */
9930  if (ncomments == 0)
9931  {
9932  comments = &empty_comment;
9933  ncomments = 1;
9934  }
9935  else if (strcmp(comments->descr, initdb_comment) == 0)
9936  ncomments = 0;
9937  }
9938 
9939  /* If a comment exists, build COMMENT ON statement */
9940  if (ncomments > 0)
9941  {
9942  PQExpBuffer query = createPQExpBuffer();
9944 
9945  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9946  if (namespace && *namespace)
9947  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9948  appendPQExpBuffer(query, "%s IS ", name);
9949  appendStringLiteralAH(query, comments->descr, fout);
9950  appendPQExpBufferStr(query, ";\n");
9951 
9952  appendPQExpBuffer(tag, "%s %s", type, name);
9953 
9954  /*
9955  * We mark comments as SECTION_NONE because they really belong in the
9956  * same section as their parent, whether that is pre-data or
9957  * post-data.
9958  */
9960  ARCHIVE_OPTS(.tag = tag->data,
9961  .namespace = namespace,
9962  .owner = owner,
9963  .description = "COMMENT",
9964  .section = SECTION_NONE,
9965  .createStmt = query->data,
9966  .deps = &dumpId,
9967  .nDeps = 1));
9968 
9969  destroyPQExpBuffer(query);
9970  destroyPQExpBuffer(tag);
9971  }
9972 }
static int findComments(Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:10094
int no_comments
Definition: pg_backup.h:178
bool schemaOnly
Definition: pg_backup.h:167

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

Referenced by dumpComment(), and dumpNamespace().

◆ dumpCompositeType()

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

Definition at line 11460 of file pg_dump.c.

11461 {
11462  DumpOptions *dopt = fout->dopt;
11464  PQExpBuffer dropped = createPQExpBuffer();
11465  PQExpBuffer delq = createPQExpBuffer();
11466  PQExpBuffer query = createPQExpBuffer();
11467  PGresult *res;
11468  char *qtypname;
11469  char *qualtypname;
11470  int ntups;
11471  int i_attname;
11472  int i_atttypdefn;
11473  int i_attlen;
11474  int i_attalign;
11475  int i_attisdropped;
11476  int i_attcollation;
11477  int i;
11478  int actual_atts;
11479 
11481  {
11482  /*
11483  * Set up query for type-specific details.
11484  *
11485  * Since we only want to dump COLLATE clauses for attributes whose
11486  * collation is different from their type's default, we use a CASE
11487  * here to suppress uninteresting attcollations cheaply. atttypid
11488  * will be 0 for dropped columns; collation does not matter for those.
11489  */
11490  appendPQExpBufferStr(query,
11491  "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
11492  "SELECT a.attname, a.attnum, "
11493  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11494  "a.attlen, a.attalign, a.attisdropped, "
11495  "CASE WHEN a.attcollation <> at.typcollation "
11496  "THEN a.attcollation ELSE 0 END AS attcollation "
11497  "FROM pg_catalog.pg_type ct "
11498  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11499  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11500  "WHERE ct.oid = $1 "
11501  "ORDER BY a.attnum");
11502 
11503  ExecuteSqlStatement(fout, query->data);
11504 
11506  }
11507 
11508  printfPQExpBuffer(query,
11509  "EXECUTE dumpCompositeType('%u')",
11510  tyinfo->dobj.catId.oid);
11511 
11512  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11513 
11514  ntups = PQntuples(res);
11515 
11516  i_attname = PQfnumber(res, "attname");
11517  i_atttypdefn = PQfnumber(res, "atttypdefn");
11518  i_attlen = PQfnumber(res, "attlen");
11519  i_attalign = PQfnumber(res, "attalign");
11520  i_attisdropped = PQfnumber(res, "attisdropped");
11521  i_attcollation = PQfnumber(res, "attcollation");
11522 
11523  if (dopt->binary_upgrade)
11524  {
11526  tyinfo->dobj.catId.oid,
11527  false, false);
11528  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11529  }
11530 
11531  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11532  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11533 
11534  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11535  qualtypname);
11536 
11537  actual_atts = 0;
11538  for (i = 0; i < ntups; i++)
11539  {
11540  char *attname;
11541  char *atttypdefn;
11542  char *attlen;
11543  char *attalign;
11544  bool attisdropped;
11545  Oid attcollation;
11546 
11547  attname = PQgetvalue(res, i, i_attname);
11548  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11549  attlen = PQgetvalue(res, i, i_attlen);
11550  attalign = PQgetvalue(res, i, i_attalign);
11551  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11552  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11553 
11554  if (attisdropped && !dopt->binary_upgrade)
11555  continue;
11556 
11557  /* Format properly if not first attr */
11558  if (actual_atts++ > 0)
11559  appendPQExpBufferChar(q, ',');
11560  appendPQExpBufferStr(q, "\n\t");
11561 
11562  if (!attisdropped)
11563  {
11564  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11565 
11566  /* Add collation if not default for the column type */
11567  if (OidIsValid(attcollation))
11568  {
11569  CollInfo *coll;
11570 
11571  coll = findCollationByOid(attcollation);
11572  if (coll)
11573  appendPQExpBuffer(q, " COLLATE %s",
11574  fmtQualifiedDumpable(coll));
11575  }
11576  }
11577  else
11578  {
11579  /*
11580  * This is a dropped attribute and we're in binary_upgrade mode.
11581  * Insert a placeholder for it in the CREATE TYPE command, and set
11582  * length and alignment with direct UPDATE to the catalogs
11583  * afterwards. See similar code in dumpTableSchema().
11584  */
11585  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11586 
11587  /* stash separately for insertion after the CREATE TYPE */
11588  appendPQExpBufferStr(dropped,
11589  "\n-- For binary upgrade, recreate dropped column.\n");
11590  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11591  "SET attlen = %s, "
11592  "attalign = '%s', attbyval = false\n"
11593  "WHERE attname = ", attlen, attalign);
11594  appendStringLiteralAH(dropped, attname, fout);
11595  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11596  appendStringLiteralAH(dropped, qualtypname, fout);
11597  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11598 
11599  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11600  qualtypname);
11601  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11602  fmtId(attname));
11603  }
11604  }
11605  appendPQExpBufferStr(q, "\n);\n");
11606  appendPQExpBufferStr(q, dropped->data);
11607 
11608  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11609 
11610  if (dopt->binary_upgrade)
11612  "TYPE", qtypname,
11613  tyinfo->dobj.namespace->dobj.name);
11614 
11615  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11616  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11617  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11618  .namespace = tyinfo->dobj.namespace->dobj.name,
11619  .owner = tyinfo->rolname,
11620  .description = "TYPE",
11621  .section = SECTION_PRE_DATA,
11622  .createStmt = q->data,
11623  .dropStmt = delq->data));
11624 
11625 
11626  /* Dump Type Comments and Security Labels */
11627  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11628  dumpComment(fout, "TYPE", qtypname,
11629  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11630  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11631 
11632  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11633  dumpSecLabel(fout, "TYPE", qtypname,
11634  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11635  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11636 
11637  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11638  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11639  qtypname, NULL,
11640  tyinfo->dobj.namespace->dobj.name,
11641  tyinfo->rolname, &tyinfo->dacl);
11642 
11643  /* Dump any per-column comments */
11644  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11645  dumpCompositeTypeColComments(fout, tyinfo, res);
11646 
11647  PQclear(res);
11648  destroyPQExpBuffer(q);
11649  destroyPQExpBuffer(dropped);
11650  destroyPQExpBuffer(delq);
11651  destroyPQExpBuffer(query);
11652  free(qtypname);
11653  free(qualtypname);
11654 }
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:5015
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, PGresult *res)
Definition: pg_dump.c:11666
Oid typrelid
Definition: pg_dump.h:201

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

11668 {
11670  int ncomments;
11671  PQExpBuffer query;
11672  PQExpBuffer target;
11673  int i;
11674  int ntups;
11675  int i_attname;
11676  int i_attnum;
11677  int i_attisdropped;
11678 
11679  /* do nothing, if --no-comments is supplied */
11680  if (fout->dopt->no_comments)
11681  return;
11682 
11683  /* Search for comments associated with type's pg_class OID */
11684  ncomments = findComments(RelationRelationId, tyinfo->typrelid,
11685  &comments);
11686 
11687  /* If no comments exist, we're done */
11688  if (ncomments <= 0)
11689  return;
11690 
11691  /* Build COMMENT ON statements */
11692  query = createPQExpBuffer();
11693  target = createPQExpBuffer();
11694 
11695  ntups = PQntuples(res);
11696  i_attnum = PQfnumber(res, "attnum");
11697  i_attname = PQfnumber(res, "attname");
11698  i_attisdropped = PQfnumber(res, "attisdropped");
11699  while (ncomments > 0)
11700  {
11701  const char *attname;
11702 
11703  attname = NULL;
11704  for (i = 0; i < ntups; i++)
11705  {
11706  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
11707  PQgetvalue(res, i, i_attisdropped)[0] != 't')
11708  {
11709  attname = PQgetvalue(res, i, i_attname);
11710  break;
11711  }
11712  }
11713  if (attname) /* just in case we don't find it */
11714  {
11715  const char *descr = comments->descr;
11716 
11717  resetPQExpBuffer(target);
11718  appendPQExpBuffer(target, "COLUMN %s.",
11719  fmtId(tyinfo->dobj.name));
11721 
11722  resetPQExpBuffer(query);
11723  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11724  fmtQualifiedDumpable(tyinfo));
11725  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11726  appendStringLiteralAH(query, descr, fout);
11727  appendPQExpBufferStr(query, ";\n");
11728 
11730  ARCHIVE_OPTS(.tag = target->data,
11731  .namespace = tyinfo->dobj.namespace->dobj.name,
11732  .owner = tyinfo->rolname,
11733  .description = "COMMENT",
11734  .section = SECTION_NONE,
11735  .createStmt = query->data,
11736  .deps = &(tyinfo->dobj.dumpId),
11737  .nDeps = 1));
11738  }
11739 
11740  comments++;
11741  ncomments--;
11742  }
11743 
11744  destroyPQExpBuffer(query);
11745  destroyPQExpBuffer(target);
11746 }
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 16855 of file pg_dump.c.

16856 {
16857  DumpOptions *dopt = fout->dopt;
16858  TableInfo *tbinfo = coninfo->contable;
16859  PQExpBuffer q;
16860  PQExpBuffer delq;
16861  char *tag = NULL;
16862  char *foreign;
16863 
16864  /* Do nothing in data-only dump */
16865  if (dopt->dataOnly)
16866  return;
16867 
16868  q = createPQExpBuffer();
16869  delq = createPQExpBuffer();
16870 
16871  foreign = tbinfo &&
16872  tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16873 
16874  if (coninfo->contype == 'p' ||
16875  coninfo->contype == 'u' ||
16876  coninfo->contype == 'x')
16877  {
16878  /* Index-related constraint */
16879  IndxInfo *indxinfo;
16880  int k;
16881 
16882  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16883 
16884  if (indxinfo == NULL)
16885  pg_fatal("missing index for constraint \"%s\"",
16886  coninfo->dobj.name);
16887 
16888  if (dopt->binary_upgrade)
16890  indxinfo->dobj.catId.oid, true);
16891 
16892  appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
16893  fmtQualifiedDumpable(tbinfo));
16894  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
16895  fmtId(coninfo->dobj.name));
16896 
16897  if (coninfo->condef)
16898  {
16899  /* pg_get_constraintdef should have provided everything */
16900  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16901  }
16902  else
16903  {
16905  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16906 
16907  /*
16908  * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
16909  * indexes. Being able to create this was fixed, but we need to
16910  * make the index distinct in order to be able to restore the
16911  * dump.
16912  */
16913  if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
16914  appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
16915  appendPQExpBufferStr(q, " (");
16916  for (k = 0; k < indxinfo->indnkeyattrs; k++)
16917  {
16918  int indkey = (int) indxinfo->indkeys[k];
16919  const char *attname;
16920 
16921  if (indkey == InvalidAttrNumber)
16922  break;
16923  attname = getAttrName(indkey, tbinfo);
16924 
16925  appendPQExpBuffer(q, "%s%s",
16926  (k == 0) ? "" : ", ",
16927  fmtId(attname));
16928  }
16929 
16930  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
16931  appendPQExpBufferStr(q, ") INCLUDE (");
16932 
16933  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
16934  {
16935  int indkey = (int) indxinfo->indkeys[k];
16936  const char *attname;
16937 
16938  if (indkey == InvalidAttrNumber)
16939  break;
16940  attname = getAttrName(indkey, tbinfo);
16941 
16942  appendPQExpBuffer(q, "%s%s",
16943  (k == indxinfo->indnkeyattrs) ? "" : ", ",
16944  fmtId(attname));
16945  }
16946 
16947  appendPQExpBufferChar(q, ')');
16948 
16949  if (nonemptyReloptions(indxinfo->indreloptions))
16950  {
16951  appendPQExpBufferStr(q, " WITH (");
16952  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16953  appendPQExpBufferChar(q, ')');
16954  }
16955 
16956  if (coninfo->condeferrable)
16957  {
16958  appendPQExpBufferStr(q, " DEFERRABLE");
16959  if (coninfo->condeferred)
16960  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16961  }
16962 
16963  appendPQExpBufferStr(q, ";\n");
16964  }
16965 
16966  /*
16967  * Append ALTER TABLE commands as needed to set properties that we
16968  * only have ALTER TABLE syntax for. Keep this in sync with the
16969  * similar code in dumpIndex!
16970  */
16971 
16972  /* Drop any not-null constraints that were added to support the PK */
16973  if (coninfo->contype == 'p')
16974  for (int i = 0; i < tbinfo->numatts; i++)
16975  if (tbinfo->notnull_throwaway[i])
16976  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s DROP CONSTRAINT %s;",
16977  fmtQualifiedDumpable(tbinfo),
16978  tbinfo->notnull_constrs[i]);
16979 
16980  /* If the index is clustered, we need to record that. */
16981  if (indxinfo->indisclustered)
16982  {
16983  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16984  fmtQualifiedDumpable(tbinfo));
16985  /* index name is not qualified in this syntax */
16986  appendPQExpBuffer(q, " ON %s;\n",
16987  fmtId(indxinfo->dobj.name));
16988  }
16989 
16990  /* If the index defines identity, we need to record that. */
16991  if (indxinfo->indisreplident)
16992  {
16993  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
16994  fmtQualifiedDumpable(tbinfo));
16995  /* index name is not qualified in this syntax */
16996  appendPQExpBuffer(q, " INDEX %s;\n",
16997  fmtId(indxinfo->dobj.name));
16998  }
16999 
17000  /* Indexes can depend on extensions */
17001  append_depends_on_extension(fout, q, &indxinfo->dobj,
17002  "pg_catalog.pg_class", "INDEX",
17003  fmtQualifiedDumpable(indxinfo));
17004 
17005  appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
17006  fmtQualifiedDumpable(tbinfo));
17007  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17008  fmtId(coninfo->dobj.name));
17009 
17010  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17011 
17012  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17013  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17014  ARCHIVE_OPTS(.tag = tag,
17015  .namespace = tbinfo->dobj.namespace->dobj.name,
17016  .tablespace = indxinfo->tablespace,
17017  .owner = tbinfo->rolname,
17018  .description = "CONSTRAINT",
17019  .section = SECTION_POST_DATA,
17020  .createStmt = q->data,
17021  .dropStmt = delq->data));
17022  }
17023  else if (coninfo->contype == 'f')
17024  {
17025  char *only;
17026 
17027  /*
17028  * Foreign keys on partitioned tables are always declared as
17029  * inheriting to partitions; for all other cases, emit them as
17030  * applying ONLY directly to the named table, because that's how they
17031  * work for regular inherited tables.
17032  */
17033  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
17034 
17035  /*
17036  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
17037  * current table data is not processed
17038  */
17039  appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
17040  only, fmtQualifiedDumpable(tbinfo));
17041  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17042  fmtId(coninfo->dobj.name),
17043  coninfo->condef);
17044 
17045  appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
17046  only, fmtQualifiedDumpable(tbinfo));
17047  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17048  fmtId(coninfo->dobj.name));
17049 
17050  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17051 
17052  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17053  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17054  ARCHIVE_OPTS(.tag = tag,
17055  .namespace = tbinfo->dobj.namespace->dobj.name,
17056  .owner = tbinfo->rolname,
17057  .description = "FK CONSTRAINT",
17058  .section = SECTION_POST_DATA,
17059  .createStmt = q->data,
17060  .dropStmt = delq->data));
17061  }
17062  else if (coninfo->contype == 'c' && tbinfo)
17063  {
17064  /* CHECK constraint on a table */
17065 
17066  /* Ignore if not to be dumped separately, or if it was inherited */
17067  if (coninfo->separate && coninfo->conislocal)
17068  {
17069  /* not ONLY since we want it to propagate to children */
17070  appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
17071  fmtQualifiedDumpable(tbinfo));
17072  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17073  fmtId(coninfo->dobj.name),
17074  coninfo->condef);
17075 
17076  appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
17077  fmtQualifiedDumpable(tbinfo));
17078  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17079  fmtId(coninfo->dobj.name));
17080 
17081  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17082 
17083  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17084  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17085  ARCHIVE_OPTS(.tag = tag,
17086  .namespace = tbinfo->dobj.namespace->dobj.name,
17087  .owner = tbinfo->rolname,
17088  .description = "CHECK CONSTRAINT",
17089  .section = SECTION_POST_DATA,
17090  .createStmt = q->data,
17091  .dropStmt = delq->data));
17092  }
17093  }
17094  else if (coninfo->contype == 'c' && tbinfo == NULL)
17095  {
17096  /* CHECK constraint on a domain */
17097  TypeInfo *tyinfo = coninfo->condomain;
17098 
17099  /* Ignore if not to be dumped separately */
17100  if (coninfo->separate)
17101  {
17102  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
17103  fmtQualifiedDumpable(tyinfo));
17104  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17105  fmtId(coninfo->dobj.name),
17106  coninfo->condef);
17107 
17108  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
17109  fmtQualifiedDumpable(tyinfo));
17110  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17111  fmtId(coninfo->dobj.name));
17112 
17113  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
17114 
17115  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17116  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17117  ARCHIVE_OPTS(.tag = tag,
17118  .namespace = tyinfo->dobj.namespace->dobj.name,
17119  .owner = tyinfo->rolname,
17120  .description = "CHECK CONSTRAINT",
17121  .section = SECTION_POST_DATA,
17122  .createStmt = q->data,
17123  .dropStmt = delq->data));
17124  }
17125  }
17126  else
17127  {
17128  pg_fatal("unrecognized constraint type: %c",
17129  coninfo->contype);
17130  }
17131 
17132  /* Dump Constraint Comments --- only works for table constraints */
17133  if (tbinfo && coninfo->separate &&
17134  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17135  dumpTableConstraintComment(fout, coninfo);
17136 
17137  free(tag);
17138  destroyPQExpBuffer(q);
17139  destroyPQExpBuffer(delq);
17140 }
#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:4842
static const char * getAttrName(int attrnum, const TableInfo *tblInfo)
Definition: pg_dump.c:16568
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
Definition: pg_dump.c:18745
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
Definition: pg_dump.c:17150
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18733
TypeInfo * condomain
Definition: pg_dump.h:483
TableInfo * contable
Definition: pg_dump.h:482
bool condeferred
Definition: pg_dump.h:489
bool conislocal
Definition: pg_dump.h:490
DumpableObject dobj
Definition: pg_dump.h:481
DumpId conindex
Definition: pg_dump.h:487
bool condeferrable
Definition: pg_dump.h:488
char * condef
Definition: pg_dump.h:485
bool indisreplident
Definition: pg_dump.h:407
int indnkeyattrs
Definition: pg_dump.h:402
int indnattrs
Definition: pg_dump.h:403
Oid * indkeys
Definition: pg_dump.h:404
char * indreloptions
Definition: pg_dump.h:399
bool indisclustered
Definition: pg_dump.h:406
char * tablespace
Definition: pg_dump.h:398
bool indnullsnotdistinct
Definition: pg_dump.h:408
DumpableObject dobj
Definition: pg_dump.h:395
char ** notnull_constrs
Definition: pg_dump.h:348
bool * notnull_throwaway
Definition: pg_dump.h:353

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(), i, if(), _indxInfo::indisclustered, _indxInfo::indisreplident, _indxInfo::indkeys, _indxInfo::indnattrs, _indxInfo::indnkeyattrs, _indxInfo::indnullsnotdistinct, _indxInfo::indreloptions, InvalidAttrNumber, _dumpableObject::name, nonemptyReloptions(), _tableInfo::notnull_constrs, _tableInfo::notnull_throwaway, _tableInfo::numatts, CatalogId::oid, pg_fatal, psprintf(), _tableInfo::relkind, _typeInfo::rolname, _tableInfo::rolname, SECTION_POST_DATA, _constraintInfo::separate, and _indxInfo::tablespace.

Referenced by dumpDumpableObject().

◆ dumpConversion()

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

Definition at line 13758 of file pg_dump.c.

13759 {
13760  DumpOptions *dopt = fout->dopt;
13761  PQExpBuffer query;
13762  PQExpBuffer q;
13763  PQExpBuffer delq;
13764  char *qconvname;
13765  PGresult *res;
13766  int i_conforencoding;
13767  int i_contoencoding;
13768  int i_conproc;
13769  int i_condefault;
13770  const char *conforencoding;
13771  const char *contoencoding;
13772  const char *conproc;
13773  bool condefault;
13774 
13775  /* Do nothing in data-only dump */
13776  if (dopt->dataOnly)
13777  return;
13778 
13779  query = createPQExpBuffer();
13780  q = createPQExpBuffer();
13781  delq = createPQExpBuffer();
13782 
13783  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13784 
13785  /* Get conversion-specific details */
13786  appendPQExpBuffer(query, "SELECT "
13787  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13788  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13789  "conproc, condefault "
13790  "FROM pg_catalog.pg_conversion c "
13791  "WHERE c.oid = '%u'::pg_catalog.oid",
13792  convinfo->dobj.catId.oid);
13793 
13794  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13795 
13796  i_conforencoding = PQfnumber(res, "conforencoding");
13797  i_contoencoding = PQfnumber(res, "contoencoding");
13798  i_conproc = PQfnumber(res, "conproc");
13799  i_condefault = PQfnumber(res, "condefault");
13800 
13801  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13802  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13803  conproc = PQgetvalue(res, 0, i_conproc);
13804  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13805 
13806  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13807  fmtQualifiedDumpable(convinfo));
13808 
13809  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13810  (condefault) ? "DEFAULT " : "",
13811  fmtQualifiedDumpable(convinfo));
13812  appendStringLiteralAH(q, conforencoding, fout);
13813  appendPQExpBufferStr(q, " TO ");
13814  appendStringLiteralAH(q, contoencoding, fout);
13815  /* regproc output is already sufficiently quoted */
13816  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13817 
13818  if (dopt->binary_upgrade)
13819  binary_upgrade_extension_member(q, &convinfo->dobj,
13820  "CONVERSION", qconvname,
13821  convinfo->dobj.namespace->dobj.name);
13822 
13823  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13824  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13825  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13826  .namespace = convinfo->dobj.namespace->dobj.name,
13827  .owner = convinfo->rolname,
13828  .description = "CONVERSION",
13829  .section = SECTION_PRE_DATA,
13830  .createStmt = q->data,
13831  .dropStmt = delq->data));
13832 
13833  /* Dump Conversion Comments */
13834  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13835  dumpComment(fout, "CONVERSION", qconvname,
13836  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13837  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13838 
13839  PQclear(res);
13840 
13841  destroyPQExpBuffer(query);
13842  destroyPQExpBuffer(q);
13843  destroyPQExpBuffer(delq);
13844  free(qconvname);
13845 }
DumpableObject dobj
Definition: pg_dump.h:275
const char * rolname
Definition: pg_dump.h:276

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

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

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

Referenced by main().

◆ dumpDatabaseConfig()

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

Definition at line 3388 of file pg_dump.c.

3390 {
3391  PGconn *conn = GetConnection(AH);
3393  PGresult *res;
3394 
3395  /* First collect database-specific options */
3396  printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3397  "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3398  dboid);
3399 
3400  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3401 
3402  for (int i = 0; i < PQntuples(res); i++)
3404  "DATABASE", dbname, NULL, NULL,
3405  outbuf);
3406 
3407  PQclear(res);
3408 
3409  /* Now look for role-and-database-specific options */
3410  printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3411  "FROM pg_db_role_setting s, pg_roles r "
3412  "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3413  dboid);
3414 
3415  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3416 
3417  for (int i = 0; i < PQntuples(res); i++)
3419  "ROLE", PQgetvalue(res, i, 0),
3420  "DATABASE", dbname,
3421  outbuf);
3422 
3423  PQclear(res);
3424 
3426 }
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
Definition: dumputils.c:822
static char * buf
Definition: pg_test_fsync.c:67
char * dbname
Definition: streamutil.c:51

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

Referenced by dumpDatabase().

◆ dumpDefaultACL()

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

Definition at line 14832 of file pg_dump.c.

14833 {
14834  DumpOptions *dopt = fout->dopt;
14835  PQExpBuffer q;
14836  PQExpBuffer tag;
14837  const char *type;
14838 
14839  /* Do nothing in data-only dump, or if we're skipping ACLs */
14840  if (dopt->dataOnly || dopt->aclsSkip)
14841  return;
14842 
14843  q = createPQExpBuffer();
14844  tag = createPQExpBuffer();
14845 
14846  switch (daclinfo->defaclobjtype)
14847  {
14848  case DEFACLOBJ_RELATION:
14849  type = "TABLES";
14850  break;
14851  case DEFACLOBJ_SEQUENCE:
14852  type = "SEQUENCES";
14853  break;
14854  case DEFACLOBJ_FUNCTION:
14855  type = "FUNCTIONS";
14856  break;
14857  case DEFACLOBJ_TYPE:
14858  type = "TYPES";
14859  break;
14860  case DEFACLOBJ_NAMESPACE:
14861  type = "SCHEMAS";
14862  break;
14863  default:
14864  /* shouldn't get here */
14865  pg_fatal("unrecognized object type in default privileges: %d",
14866  (int) daclinfo->defaclobjtype);
14867  type = ""; /* keep compiler quiet */
14868  }
14869 
14870  appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
14871 
14872  /* build the actual command(s) for this tuple */
14874  daclinfo->dobj.namespace != NULL ?
14875  daclinfo->dobj.namespace->dobj.name : NULL,
14876  daclinfo->dacl.acl,
14877  daclinfo->dacl.acldefault,
14878  daclinfo->defaclrole,
14879  fout->remoteVersion,
14880  q))
14881  pg_fatal("could not parse default ACL list (%s)",
14882  daclinfo->dacl.acl);
14883 
14884  if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
14885  ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
14886  ARCHIVE_OPTS(.tag = tag->data,
14887  .namespace = daclinfo->dobj.namespace ?
14888  daclinfo->dobj.namespace->dobj.name : NULL,
14889  .owner = daclinfo->defaclrole,
14890  .description = "DEFAULT ACL",
14891  .section = SECTION_POST_DATA,
14892  .createStmt = q->data));
14893 
14894  destroyPQExpBuffer(tag);
14895  destroyPQExpBuffer(q);
14896 }
bool buildDefaultACLCommands(const char *type, const char *nspname, const char *acls, const char *acldefault, const char *owner, int remoteVersion, PQExpBuffer sql)
Definition: du