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

18155 {
18156  DumpableObject *preDataBound = boundaryObjs + 0;
18157  DumpableObject *postDataBound = boundaryObjs + 1;
18158  int i;
18159 
18160  for (i = 0; i < numObjs; i++)
18161  {
18162  DumpableObject *dobj = dobjs[i];
18163 
18164  /*
18165  * The classification of object types here must match the SECTION_xxx
18166  * values assigned during subsequent ArchiveEntry calls!
18167  */
18168  switch (dobj->objType)
18169  {
18170  case DO_NAMESPACE:
18171  case DO_EXTENSION:
18172  case DO_TYPE:
18173  case DO_SHELL_TYPE:
18174  case DO_FUNC:
18175  case DO_AGG:
18176  case DO_OPERATOR:
18177  case DO_ACCESS_METHOD:
18178  case DO_OPCLASS:
18179  case DO_OPFAMILY:
18180  case DO_COLLATION:
18181  case DO_CONVERSION:
18182  case DO_TABLE:
18183  case DO_TABLE_ATTACH:
18184  case DO_ATTRDEF:
18185  case DO_PROCLANG:
18186  case DO_CAST:
18187  case DO_DUMMY_TYPE:
18188  case DO_TSPARSER:
18189  case DO_TSDICT:
18190  case DO_TSTEMPLATE:
18191  case DO_TSCONFIG:
18192  case DO_FDW:
18193  case DO_FOREIGN_SERVER:
18194  case DO_TRANSFORM:
18195  case DO_LARGE_OBJECT:
18196  /* Pre-data objects: must come before the pre-data boundary */
18197  addObjectDependency(preDataBound, dobj->dumpId);
18198  break;
18199  case DO_TABLE_DATA:
18200  case DO_SEQUENCE_SET:
18201  case DO_LARGE_OBJECT_DATA:
18202  /* Data objects: must come between the boundaries */
18203  addObjectDependency(dobj, preDataBound->dumpId);
18204  addObjectDependency(postDataBound, dobj->dumpId);
18205  break;
18206  case DO_INDEX:
18207  case DO_INDEX_ATTACH:
18208  case DO_STATSEXT:
18209  case DO_REFRESH_MATVIEW:
18210  case DO_TRIGGER:
18211  case DO_EVENT_TRIGGER:
18212  case DO_DEFAULT_ACL:
18213  case DO_POLICY:
18214  case DO_PUBLICATION:
18215  case DO_PUBLICATION_REL:
18217  case DO_SUBSCRIPTION:
18218  /* Post-data objects: must come after the post-data boundary */
18219  addObjectDependency(dobj, postDataBound->dumpId);
18220  break;
18221  case DO_RULE:
18222  /* Rules are post-data, but only if dumped separately */
18223  if (((RuleInfo *) dobj)->separate)
18224  addObjectDependency(dobj, postDataBound->dumpId);
18225  break;
18226  case DO_CONSTRAINT:
18227  case DO_FK_CONSTRAINT:
18228  /* Constraints are post-data, but only if dumped separately */
18229  if (((ConstraintInfo *) dobj)->separate)
18230  addObjectDependency(dobj, postDataBound->dumpId);
18231  break;
18232  case DO_PRE_DATA_BOUNDARY:
18233  /* nothing to do */
18234  break;
18235  case DO_POST_DATA_BOUNDARY:
18236  /* must come after the pre-data boundary */
18237  addObjectDependency(dobj, preDataBound->dumpId);
18238  break;
18239  }
18240  }
18241 }
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:775
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 7482 of file pg_dump.c.

7483 {
7484  SimplePtrListCell *cell;
7485 
7486  Assert(dobj->objType == DO_FK_CONSTRAINT);
7487 
7488  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7489  {
7490  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7491 
7492  addObjectDependency(dobj, attach->dobj.dumpId);
7493 
7494  if (attach->partitionIdx->partattaches.head != NULL)
7495  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7496  }
7497 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7482
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54
IndxInfo * partitionIdx
Definition: pg_dump.h:414
DumpableObject dobj
Definition: pg_dump.h:412
SimplePtrList partattaches
Definition: pg_dump.h:404

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

4840 {
4841  if (dobj->depends_on_ext)
4842  {
4843  char *nm;
4844  PGresult *res;
4845  PQExpBuffer query;
4846  int ntups;
4847  int i_extname;
4848  int i;
4849 
4850  /* dodge fmtId() non-reentrancy */
4851  nm = pg_strdup(objname);
4852 
4853  query = createPQExpBuffer();
4854  appendPQExpBuffer(query,
4855  "SELECT e.extname "
4856  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4857  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4858  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4859  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4860  catalog,
4861  dobj->catId.oid);
4862  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4863  ntups = PQntuples(res);
4864  i_extname = PQfnumber(res, "extname");
4865  for (i = 0; i < ntups; i++)
4866  {
4867  appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4868  keyword, nm,
4869  fmtId(PQgetvalue(res, i, i_extname)));
4870  }
4871 
4872  PQclear(res);
4873  destroyPQExpBuffer(query);
4874  pg_free(nm);
4875  }
4876 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3314
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3709
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3422
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 18469 of file pg_dump.c.

18471 {
18472  bool res;
18473 
18474  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18475  fout->std_strings);
18476  if (!res)
18477  pg_log_warning("could not parse %s array", "reloptions");
18478 }
#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:227
int encoding
Definition: pg_backup.h:226

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

5128 {
5129  DumpableObject *extobj = NULL;
5130  int i;
5131 
5132  if (!dobj->ext_member)
5133  return;
5134 
5135  /*
5136  * Find the parent extension. We could avoid this search if we wanted to
5137  * add a link field to DumpableObject, but the space costs of that would
5138  * be considerable. We assume that member objects could only have a
5139  * direct dependency on their own extension, not any others.
5140  */
5141  for (i = 0; i < dobj->nDeps; i++)
5142  {
5143  extobj = findObjectByDumpId(dobj->dependencies[i]);
5144  if (extobj && extobj->objType == DO_EXTENSION)
5145  break;
5146  extobj = NULL;
5147  }
5148  if (extobj == NULL)
5149  pg_fatal("could not find parent extension for %s %s",
5150  objtype, objname);
5151 
5152  appendPQExpBufferStr(upgrade_buffer,
5153  "\n-- For binary upgrade, handle extension membership the hard way\n");
5154  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
5155  fmtId(extobj->name),
5156  objtype);
5157  if (objnamespace && *objnamespace)
5158  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
5159  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
5160 }
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:722
#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 5007 of file pg_dump.c.

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

4998 {
4999  Oid pg_type_oid = tbinfo->reltype;
5000 
5001  if (OidIsValid(pg_type_oid))
5002  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5003  pg_type_oid, false, false);
5004 }
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:4910
Oid reltype
Definition: pg_dump.h:307

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

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

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

18269 {
18270  ArchiveHandle *AH = (ArchiveHandle *) fout;
18271  TocEntry *te;
18272 
18273  /* Scan all TOC entries in the archive */
18274  for (te = AH->toc->next; te != AH->toc; te = te->next)
18275  {
18276  DumpableObject *dobj;
18277  DumpId *dependencies;
18278  int nDeps;
18279  int allocDeps;
18280 
18281  /* No need to process entries that will not be dumped */
18282  if (te->reqs == 0)
18283  continue;
18284  /* Ignore entries that already have "special" dependencies */
18285  if (te->nDeps > 0)
18286  continue;
18287  /* Otherwise, look up the item's original DumpableObject, if any */
18288  dobj = findObjectByDumpId(te->dumpId);
18289  if (dobj == NULL)
18290  continue;
18291  /* No work if it has no dependencies */
18292  if (dobj->nDeps <= 0)
18293  continue;
18294  /* Set up work array */
18295  allocDeps = 64;
18296  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18297  nDeps = 0;
18298  /* Recursively find all dumpable dependencies */
18299  findDumpableDependencies(AH, dobj,
18300  &dependencies, &nDeps, &allocDeps);
18301  /* And save 'em ... */
18302  if (nDeps > 0)
18303  {
18304  dependencies = (DumpId *) pg_realloc(dependencies,
18305  nDeps * sizeof(DumpId));
18306  te->dependencies = dependencies;
18307  te->nDeps = nDeps;
18308  }
18309  else
18310  free(dependencies);
18311  }
18312 }
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:267
static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:18316
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 2772 of file pg_dump.c.

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

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

1663 {
1664  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1665 
1666  if (ext == NULL)
1667  return false;
1668 
1669  dobj->ext_member = true;
1670 
1671  /* Record dependency so that getDependencies needn't deal with that */
1672  addObjectDependency(dobj, ext->dobj.dumpId);
1673 
1674  /*
1675  * In 9.6 and above, mark the member object to have any non-initial ACL,
1676  * policies, and security labels dumped.
1677  *
1678  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1679  * extract the information about the object. We don't provide support for
1680  * initial policies and security labels and it seems unlikely for those to
1681  * ever exist, but we may have to revisit this later.
1682  *
1683  * Prior to 9.6, we do not include any extension member components.
1684  *
1685  * In binary upgrades, we still dump all components of the members
1686  * individually, since the idea is to exactly reproduce the database
1687  * contents rather than replace the extension contents with something
1688  * different.
1689  */
1690  if (fout->dopt->binary_upgrade)
1691  dobj->dump = ext->dobj.dump;
1692  else
1693  {
1694  if (fout->remoteVersion < 90600)
1695  dobj->dump = DUMP_COMPONENT_NONE;
1696  else
1697  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1700  }
1701 
1702  return true;
1703 }
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:1008
#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:211
int binary_upgrade
Definition: pg_backup.h:163
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 9981 of file pg_dump.c.

9982 {
9983  PGresult *res;
9984  PQExpBuffer query;
9985  int i_description;
9986  int i_classoid;
9987  int i_objoid;
9988  int i_objsubid;
9989  int ntups;
9990  int i;
9991  DumpableObject *dobj;
9992 
9993  query = createPQExpBuffer();
9994 
9995  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9996  "FROM pg_catalog.pg_description "
9997  "ORDER BY classoid, objoid, objsubid");
9998 
9999  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10000 
10001  /* Construct lookup table containing OIDs in numeric form */
10002 
10003  i_description = PQfnumber(res, "description");
10004  i_classoid = PQfnumber(res, "classoid");
10005  i_objoid = PQfnumber(res, "objoid");
10006  i_objsubid = PQfnumber(res, "objsubid");
10007 
10008  ntups = PQntuples(res);
10009 
10010  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10011  ncomments = 0;
10012  dobj = NULL;
10013 
10014  for (i = 0; i < ntups; i++)
10015  {
10016  CatalogId objId;
10017  int subid;
10018 
10019  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
10020  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
10021  subid = atoi(PQgetvalue(res, i, i_objsubid));
10022 
10023  /* We needn't remember comments that don't match any dumpable object */
10024  if (dobj == NULL ||
10025  dobj->catId.tableoid != objId.tableoid ||
10026  dobj->catId.oid != objId.oid)
10027  dobj = findObjectByCatalogId(objId);
10028  if (dobj == NULL)
10029  continue;
10030 
10031  /*
10032  * Comments on columns of composite types are linked to the type's
10033  * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
10034  * in the type's own DumpableObject.
10035  */
10036  if (subid != 0 && dobj->objType == DO_TABLE &&
10037  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
10038  {
10039  TypeInfo *cTypeInfo;
10040 
10041  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
10042  if (cTypeInfo)
10043  cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
10044  }
10045  else
10046  dobj->components |= DUMP_COMPONENT_COMMENT;
10047 
10048  comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
10050  comments[ncomments].objoid = objId.oid;
10051  comments[ncomments].objsubid = subid;
10052  ncomments++;
10053  }
10054 
10055  PQclear(res);
10056  destroyPQExpBuffer(query);
10057 }
TypeInfo * findTypeByOid(Oid oid)
Definition: common.c:856
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 9526 of file pg_dump.c.

9527 {
9528  PGresult *res;
9529  const char *query;
9530  int i;
9531 
9532  query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
9533 
9534  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
9535 
9537 
9539 
9540  for (i = 0; i < nrolenames; i++)
9541  {
9544  }
9545 
9546  PQclear(res);
9547 }
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 15058 of file pg_dump.c.

15059 {
15060  PGresult *res;
15061  PQExpBuffer query;
15062  int i_label;
15063  int i_provider;
15064  int i_classoid;
15065  int i_objoid;
15066  int i_objsubid;
15067  int ntups;
15068  int i;
15069  DumpableObject *dobj;
15070 
15071  query = createPQExpBuffer();
15072 
15073  appendPQExpBufferStr(query,
15074  "SELECT label, provider, classoid, objoid, objsubid "
15075  "FROM pg_catalog.pg_seclabel "
15076  "ORDER BY classoid, objoid, objsubid");
15077 
15078  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15079 
15080  /* Construct lookup table containing OIDs in numeric form */
15081  i_label = PQfnumber(res, "label");
15082  i_provider = PQfnumber(res, "provider");
15083  i_classoid = PQfnumber(res, "classoid");
15084  i_objoid = PQfnumber(res, "objoid");
15085  i_objsubid = PQfnumber(res, "objsubid");
15086 
15087  ntups = PQntuples(res);
15088 
15089  seclabels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15090  nseclabels = 0;
15091  dobj = NULL;
15092 
15093  for (i = 0; i < ntups; i++)
15094  {
15095  CatalogId objId;
15096  int subid;
15097 
15098  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
15099  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
15100  subid = atoi(PQgetvalue(res, i, i_objsubid));
15101 
15102  /* We needn't remember labels that don't match any dumpable object */
15103  if (dobj == NULL ||
15104  dobj->catId.tableoid != objId.tableoid ||
15105  dobj->catId.oid != objId.oid)
15106  dobj = findObjectByCatalogId(objId);
15107  if (dobj == NULL)
15108  continue;
15109 
15110  /*
15111  * Labels on columns of composite types are linked to the type's
15112  * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
15113  * in the type's own DumpableObject.
15114  */
15115  if (subid != 0 && dobj->objType == DO_TABLE &&
15116  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
15117  {
15118  TypeInfo *cTypeInfo;
15119 
15120  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
15121  if (cTypeInfo)
15122  cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
15123  }
15124  else
15125  dobj->components |= DUMP_COMPONENT_SECLABEL;
15126 
15130  seclabels[nseclabels].objoid = objId.oid;
15131  seclabels[nseclabels].objsubid = subid;
15132  nseclabels++;
15133  }
15134 
15135  PQclear(res);
15136  destroyPQExpBuffer(query);
15137 }
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 12663 of file pg_dump.c.

12664 {
12665  char *name;
12666  char *paren;
12667  bool inquote;
12668 
12669  /* In all cases "-" means a null reference */
12670  if (strcmp(proc, "-") == 0)
12671  return NULL;
12672 
12673  name = pg_strdup(proc);
12674  /* find non-double-quoted left paren */
12675  inquote = false;
12676  for (paren = name; *paren; paren++)
12677  {
12678  if (*paren == '(' && !inquote)
12679  {
12680  *paren = '\0';
12681  break;
12682  }
12683  if (*paren == '"')
12684  inquote = !inquote;
12685  }
12686  return name;
12687 }
const char * name
Definition: encode.c:571

References name, and pg_strdup().

Referenced by dumpOpr().

◆ convertTSFunction()

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

Definition at line 12734 of file pg_dump.c.

12735 {
12736  char *result;
12737  char query[128];
12738  PGresult *res;
12739 
12740  snprintf(query, sizeof(query),
12741  "SELECT '%u'::pg_catalog.regproc", funcOid);
12742  res = ExecuteSqlQueryForSingleRow(fout, query);
12743 
12744  result = pg_strdup(PQgetvalue(res, 0, 0));
12745 
12746  PQclear(res);
12747 
12748  return result;
12749 }
#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 18129 of file pg_dump.c.

18130 {
18131  DumpableObject *dobjs;
18132 
18133  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18134 
18135  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18136  dobjs[0].catId = nilCatalogId;
18137  AssignDumpId(dobjs + 0);
18138  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18139 
18140  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18141  dobjs[1].catId = nilCatalogId;
18142  AssignDumpId(dobjs + 1);
18143  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18144 
18145  return dobjs;
18146 }
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:638
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 15333 of file pg_dump.c.

15334 {
15335  PQExpBuffer result = createPQExpBuffer();
15336  int j;
15337 
15338  appendPQExpBufferStr(result, "SELECT");
15339 
15340  for (j = 0; j < tbinfo->numatts; j++)
15341  {
15342  if (j > 0)
15343  appendPQExpBufferChar(result, ',');
15344  appendPQExpBufferStr(result, "\n ");
15345 
15346  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15347 
15348  /*
15349  * Must add collation if not default for the type, because CREATE OR
15350  * REPLACE VIEW won't change it
15351  */
15352  if (OidIsValid(tbinfo->attcollation[j]))
15353  {
15354  CollInfo *coll;
15355 
15356  coll = findCollationByOid(tbinfo->attcollation[j]);
15357  if (coll)
15358  appendPQExpBuffer(result, " COLLATE %s",
15359  fmtQualifiedDumpable(coll));
15360  }
15361 
15362  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15363  }
15364 
15365  return result;
15366 }
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:911
int j
Definition: isn.c:74
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:165
int numatts
Definition: pg_dump.h:330
Oid * attcollation
Definition: pg_dump.h:343
char ** atttypnames
Definition: pg_dump.h:332
char ** attnames
Definition: pg_dump.h:331

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

15285 {
15286  PQExpBuffer query = createPQExpBuffer();
15287  PQExpBuffer result = createPQExpBuffer();
15288  PGresult *res;
15289  int len;
15290 
15291  /* Fetch the view definition */
15292  appendPQExpBuffer(query,
15293  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15294  tbinfo->dobj.catId.oid);
15295 
15296  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15297 
15298  if (PQntuples(res) != 1)
15299  {
15300  if (PQntuples(res) < 1)
15301  pg_fatal("query to obtain definition of view \"%s\" returned no data",
15302  tbinfo->dobj.name);
15303  else
15304  pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
15305  tbinfo->dobj.name);
15306  }
15307 
15308  len = PQgetlength(res, 0, 0);
15309 
15310  if (len == 0)
15311  pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
15312  tbinfo->dobj.name);
15313 
15314  /* Strip off the trailing semicolon so that other things may follow. */
15315  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15316  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15317 
15318  PQclear(res);
15319  destroyPQExpBuffer(query);
15320 
15321  return result;
15322 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3720
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:283

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

12757 {
12758  DumpOptions *dopt = fout->dopt;
12759  PQExpBuffer q;
12760  PQExpBuffer delq;
12761  char *qamname;
12762 
12763  /* Do nothing in data-only dump */
12764  if (dopt->dataOnly)
12765  return;
12766 
12767  q = createPQExpBuffer();
12768  delq = createPQExpBuffer();
12769 
12770  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12771 
12772  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12773 
12774  switch (aminfo->amtype)
12775  {
12776  case AMTYPE_INDEX:
12777  appendPQExpBufferStr(q, "TYPE INDEX ");
12778  break;
12779  case AMTYPE_TABLE:
12780  appendPQExpBufferStr(q, "TYPE TABLE ");
12781  break;
12782  default:
12783  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12784  aminfo->amtype, qamname);
12785  destroyPQExpBuffer(q);
12786  destroyPQExpBuffer(delq);
12787  free(qamname);
12788  return;
12789  }
12790 
12791  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12792 
12793  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12794  qamname);
12795 
12796  if (dopt->binary_upgrade)
12798  "ACCESS METHOD", qamname, NULL);
12799 
12800  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12801  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12802  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12803  .description = "ACCESS METHOD",
12804  .section = SECTION_PRE_DATA,
12805  .createStmt = q->data,
12806  .dropStmt = delq->data));
12807 
12808  /* Dump Access Method Comments */
12809  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12810  dumpComment(fout, "ACCESS METHOD", qamname,
12811  NULL, "",
12812  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12813 
12814  destroyPQExpBuffer(q);
12815  destroyPQExpBuffer(delq);
12816  free(qamname);
12817 }
@ SECTION_PRE_DATA
Definition: pg_backup.h:57
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:9790
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:5123
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:96
char * amhandler
Definition: pg_dump.h:251
DumpableObject dobj
Definition: pg_dump.h:249
bool dataOnly
Definition: pg_backup.h:167

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

14695 {
14696  DumpId aclDumpId = InvalidDumpId;
14697  DumpOptions *dopt = fout->dopt;
14698  const char *acls = dacl->acl;
14699  const char *acldefault = dacl->acldefault;
14700  char privtype = dacl->privtype;
14701  const char *initprivs = dacl->initprivs;
14702  const char *baseacls;
14703  PQExpBuffer sql;
14704 
14705  /* Do nothing if ACL dump is not enabled */
14706  if (dopt->aclsSkip)
14707  return InvalidDumpId;
14708 
14709  /* --data-only skips ACLs *except* large object ACLs */
14710  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14711  return InvalidDumpId;
14712 
14713  sql = createPQExpBuffer();
14714 
14715  /*
14716  * In binary upgrade mode, we don't run an extension's script but instead
14717  * dump out the objects independently and then recreate them. To preserve
14718  * any initial privileges which were set on extension objects, we need to
14719  * compute the set of GRANT and REVOKE commands necessary to get from the
14720  * default privileges of an object to its initial privileges as recorded
14721  * in pg_init_privs.
14722  *
14723  * At restore time, we apply these commands after having called
14724  * binary_upgrade_set_record_init_privs(true). That tells the backend to
14725  * copy the results into pg_init_privs. This is how we preserve the
14726  * contents of that catalog across binary upgrades.
14727  */
14728  if (dopt->binary_upgrade && privtype == 'e' &&
14729  initprivs && *initprivs != '\0')
14730  {
14731  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14732  if (!buildACLCommands(name, subname, nspname, type,
14733  initprivs, acldefault, owner,
14734  "", fout->remoteVersion, sql))
14735  pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
14736  initprivs, acldefault, name, type);
14737  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14738  }
14739 
14740  /*
14741  * Now figure the GRANT and REVOKE commands needed to get to the object's
14742  * actual current ACL, starting from the initprivs if given, else from the
14743  * object-type-specific default. Also, while buildACLCommands will assume
14744  * that a NULL/empty acls string means it needn't do anything, what that
14745  * actually represents is the object-type-specific default; so we need to
14746  * substitute the acldefault string to get the right results in that case.
14747  */
14748  if (initprivs && *initprivs != '\0')
14749  {
14750  baseacls = initprivs;
14751  if (acls == NULL || *acls == '\0')
14752  acls = acldefault;
14753  }
14754  else
14755  baseacls = acldefault;
14756 
14757  if (!buildACLCommands(name, subname, nspname, type,
14758  acls, baseacls, owner,
14759  "", fout->remoteVersion, sql))
14760  pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
14761  acls, baseacls, name, type);
14762 
14763  if (sql->len > 0)
14764  {
14766  DumpId aclDeps[2];
14767  int nDeps = 0;
14768 
14769  if (subname)
14770  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
14771  else
14772  appendPQExpBuffer(tag, "%s %s", type, name);
14773 
14774  aclDeps[nDeps++] = objDumpId;
14775  if (altDumpId != InvalidDumpId)
14776  aclDeps[nDeps++] = altDumpId;
14777 
14778  aclDumpId = createDumpId();
14779 
14780  ArchiveEntry(fout, nilCatalogId, aclDumpId,
14781  ARCHIVE_OPTS(.tag = tag->data,
14782  .namespace = nspname,
14783  .owner = owner,
14784  .description = "ACL",
14785  .section = SECTION_NONE,
14786  .createStmt = sql->data,
14787  .deps = aclDeps,
14788  .nDeps = nDeps));
14789 
14790  destroyPQExpBuffer(tag);
14791  }
14792 
14793  destroyPQExpBuffer(sql);
14794 
14795  return aclDumpId;
14796 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:780
DumpId createDumpId(void)
Definition: common.c:702
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:56
#define InvalidDumpId
Definition: pg_backup.h:269
NameData subname
bool aclsSkip
Definition: pg_backup.h:169
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

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 generate_unaccent_rules::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 13658 of file pg_dump.c.

13659 {
13660  DumpOptions *dopt = fout->dopt;
13661  PQExpBuffer query;
13662  PQExpBuffer q;
13663  PQExpBuffer delq;
13664  PQExpBuffer details;
13665  char *aggsig; /* identity signature */
13666  char *aggfullsig = NULL; /* full signature */
13667  char *aggsig_tag;
13668  PGresult *res;
13669  int i_agginitval;
13670  int i_aggminitval;
13671  const char *aggtransfn;
13672  const char *aggfinalfn;
13673  const char *aggcombinefn;
13674  const char *aggserialfn;
13675  const char *aggdeserialfn;
13676  const char *aggmtransfn;
13677  const char *aggminvtransfn;
13678  const char *aggmfinalfn;
13679  bool aggfinalextra;
13680  bool aggmfinalextra;
13681  char aggfinalmodify;
13682  char aggmfinalmodify;
13683  const char *aggsortop;
13684  char *aggsortconvop;
13685  char aggkind;
13686  const char *aggtranstype;
13687  const char *aggtransspace;
13688  const char *aggmtranstype;
13689  const char *aggmtransspace;
13690  const char *agginitval;
13691  const char *aggminitval;
13692  const char *proparallel;
13693  char defaultfinalmodify;
13694 
13695  /* Do nothing in data-only dump */
13696  if (dopt->dataOnly)
13697  return;
13698 
13699  query = createPQExpBuffer();
13700  q = createPQExpBuffer();
13701  delq = createPQExpBuffer();
13702  details = createPQExpBuffer();
13703 
13704  if (!fout->is_prepared[PREPQUERY_DUMPAGG])
13705  {
13706  /* Set up query for aggregate-specific details */
13707  appendPQExpBufferStr(query,
13708  "PREPARE dumpAgg(pg_catalog.oid) AS\n");
13709 
13710  appendPQExpBufferStr(query,
13711  "SELECT "
13712  "aggtransfn,\n"
13713  "aggfinalfn,\n"
13714  "aggtranstype::pg_catalog.regtype,\n"
13715  "agginitval,\n"
13716  "aggsortop,\n"
13717  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13718  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
13719 
13720  if (fout->remoteVersion >= 90400)
13721  appendPQExpBufferStr(query,
13722  "aggkind,\n"
13723  "aggmtransfn,\n"
13724  "aggminvtransfn,\n"
13725  "aggmfinalfn,\n"
13726  "aggmtranstype::pg_catalog.regtype,\n"
13727  "aggfinalextra,\n"
13728  "aggmfinalextra,\n"
13729  "aggtransspace,\n"
13730  "aggmtransspace,\n"
13731  "aggminitval,\n");
13732  else
13733  appendPQExpBufferStr(query,
13734  "'n' AS aggkind,\n"
13735  "'-' AS aggmtransfn,\n"
13736  "'-' AS aggminvtransfn,\n"
13737  "'-' AS aggmfinalfn,\n"
13738  "0 AS aggmtranstype,\n"
13739  "false AS aggfinalextra,\n"
13740  "false AS aggmfinalextra,\n"
13741  "0 AS aggtransspace,\n"
13742  "0 AS aggmtransspace,\n"
13743  "NULL AS aggminitval,\n");
13744 
13745  if (fout->remoteVersion >= 90600)
13746  appendPQExpBufferStr(query,
13747  "aggcombinefn,\n"
13748  "aggserialfn,\n"
13749  "aggdeserialfn,\n"
13750  "proparallel,\n");
13751  else
13752  appendPQExpBufferStr(query,
13753  "'-' AS aggcombinefn,\n"
13754  "'-' AS aggserialfn,\n"
13755  "'-' AS aggdeserialfn,\n"
13756  "'u' AS proparallel,\n");
13757 
13758  if (fout->remoteVersion >= 110000)
13759  appendPQExpBufferStr(query,
13760  "aggfinalmodify,\n"
13761  "aggmfinalmodify\n");
13762  else
13763  appendPQExpBufferStr(query,
13764  "'0' AS aggfinalmodify,\n"
13765  "'0' AS aggmfinalmodify\n");
13766 
13767  appendPQExpBufferStr(query,
13768  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13769  "WHERE a.aggfnoid = p.oid "
13770  "AND p.oid = $1");
13771 
13772  ExecuteSqlStatement(fout, query->data);
13773 
13774  fout->is_prepared[PREPQUERY_DUMPAGG] = true;
13775  }
13776 
13777  printfPQExpBuffer(query,
13778  "EXECUTE dumpAgg('%u')",
13779  agginfo->aggfn.dobj.catId.oid);
13780 
13781  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13782 
13783  i_agginitval = PQfnumber(res, "agginitval");
13784  i_aggminitval = PQfnumber(res, "aggminitval");
13785 
13786  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
13787  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
13788  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
13789  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
13790  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
13791  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
13792  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
13793  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
13794  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
13795  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
13796  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
13797  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
13798  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
13799  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
13800  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
13801  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
13802  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
13803  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
13804  agginitval = PQgetvalue(res, 0, i_agginitval);
13805  aggminitval = PQgetvalue(res, 0, i_aggminitval);
13806  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13807 
13808  {
13809  char *funcargs;
13810  char *funciargs;
13811 
13812  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13813  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13814  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13815  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13816  }
13817 
13818  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
13819 
13820  /* identify default modify flag for aggkind (must match DefineAggregate) */
13821  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
13822  /* replace omitted flags for old versions */
13823  if (aggfinalmodify == '0')
13824  aggfinalmodify = defaultfinalmodify;
13825  if (aggmfinalmodify == '0')
13826  aggmfinalmodify = defaultfinalmodify;
13827 
13828  /* regproc and regtype output is already sufficiently quoted */
13829  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
13830  aggtransfn, aggtranstype);
13831 
13832  if (strcmp(aggtransspace, "0") != 0)
13833  {
13834  appendPQExpBuffer(details, ",\n SSPACE = %s",
13835  aggtransspace);
13836  }
13837 
13838  if (!PQgetisnull(res, 0, i_agginitval))
13839  {
13840  appendPQExpBufferStr(details, ",\n INITCOND = ");
13841  appendStringLiteralAH(details, agginitval, fout);
13842  }
13843 
13844  if (strcmp(aggfinalfn, "-") != 0)
13845  {
13846  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
13847  aggfinalfn);
13848  if (aggfinalextra)
13849  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
13850  if (aggfinalmodify != defaultfinalmodify)
13851  {
13852  switch (aggfinalmodify)
13853  {
13854  case AGGMODIFY_READ_ONLY:
13855  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
13856  break;
13857  case AGGMODIFY_SHAREABLE:
13858  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
13859  break;
13860  case AGGMODIFY_READ_WRITE:
13861  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
13862  break;
13863  default:
13864  pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
13865  agginfo->aggfn.dobj.name);
13866  break;
13867  }
13868  }
13869  }
13870 
13871  if (strcmp(aggcombinefn, "-") != 0)
13872  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
13873 
13874  if (strcmp(aggserialfn, "-") != 0)
13875  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
13876 
13877  if (strcmp(aggdeserialfn, "-") != 0)
13878  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
13879 
13880  if (strcmp(aggmtransfn, "-") != 0)
13881  {
13882  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
13883  aggmtransfn,
13884  aggminvtransfn,
13885  aggmtranstype);
13886  }
13887 
13888  if (strcmp(aggmtransspace, "0") != 0)
13889  {
13890  appendPQExpBuffer(details, ",\n MSSPACE = %s",
13891  aggmtransspace);
13892  }
13893 
13894  if (!PQgetisnull(res, 0, i_aggminitval))
13895  {
13896  appendPQExpBufferStr(details, ",\n MINITCOND = ");
13897  appendStringLiteralAH(details, aggminitval, fout);
13898  }
13899 
13900  if (strcmp(aggmfinalfn, "-") != 0)
13901  {
13902  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
13903  aggmfinalfn);
13904  if (aggmfinalextra)
13905  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
13906  if (aggmfinalmodify != defaultfinalmodify)
13907  {
13908  switch (aggmfinalmodify)
13909  {
13910  case AGGMODIFY_READ_ONLY:
13911  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
13912  break;
13913  case AGGMODIFY_SHAREABLE:
13914  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
13915  break;
13916  case AGGMODIFY_READ_WRITE:
13917  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
13918  break;
13919  default:
13920  pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
13921  agginfo->aggfn.dobj.name);
13922  break;
13923  }
13924  }
13925  }
13926 
13927  aggsortconvop = getFormattedOperatorName(aggsortop);
13928  if (aggsortconvop)
13929  {
13930  appendPQExpBuffer(details, ",\n SORTOP = %s",
13931  aggsortconvop);
13932  free(aggsortconvop);
13933  }
13934 
13935  if (aggkind == AGGKIND_HYPOTHETICAL)
13936  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
13937 
13938  if (proparallel[0] != PROPARALLEL_UNSAFE)
13939  {
13940  if (proparallel[0] == PROPARALLEL_SAFE)
13941  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
13942  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13943  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
13944  else if (proparallel[0] != PROPARALLEL_UNSAFE)
13945  pg_fatal("unrecognized proparallel value for function \"%s\"",
13946  agginfo->aggfn.dobj.name);
13947  }
13948 
13949  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
13950  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13951  aggsig);
13952 
13953  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
13954  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13955  aggfullsig ? aggfullsig : aggsig, details->data);
13956 
13957  if (dopt->binary_upgrade)
13958  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
13959  "AGGREGATE", aggsig,
13960  agginfo->aggfn.dobj.namespace->dobj.name);
13961 
13962  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
13963  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
13964  agginfo->aggfn.dobj.dumpId,
13965  ARCHIVE_OPTS(.tag = aggsig_tag,
13966  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
13967  .owner = agginfo->aggfn.rolname,
13968  .description = "AGGREGATE",
13969  .section = SECTION_PRE_DATA,
13970  .createStmt = q->data,
13971  .dropStmt = delq->data));
13972 
13973  /* Dump Aggregate Comments */
13974  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
13975  dumpComment(fout, "AGGREGATE", aggsig,
13976  agginfo->aggfn.dobj.namespace->dobj.name,
13977  agginfo->aggfn.rolname,
13978  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13979 
13980  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
13981  dumpSecLabel(fout, "AGGREGATE", aggsig,
13982  agginfo->aggfn.dobj.namespace->dobj.name,
13983  agginfo->aggfn.rolname,
13984  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13985 
13986  /*
13987  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
13988  * command look like a function's GRANT; in particular this affects the
13989  * syntax for zero-argument aggregates and ordered-set aggregates.
13990  */
13991  free(aggsig);
13992 
13993  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
13994 
13995  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
13996  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
13997  "FUNCTION", aggsig, NULL,
13998  agginfo->aggfn.dobj.namespace->dobj.name,
13999  agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
14000 
14001  free(aggsig);
14002  free(aggfullsig);
14003  free(aggsig_tag);
14004 
14005  PQclear(res);
14006 
14007  destroyPQExpBuffer(query);
14008  destroyPQExpBuffer(q);
14009  destroyPQExpBuffer(delq);
14010  destroyPQExpBuffer(details);
14011 }
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3734
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:328
@ PREPQUERY_DUMPAGG
Definition: pg_backup.h:65
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:278
static char * getFormattedOperatorName(const char *oproid)
Definition: pg_dump.c:12704
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11766
static char * format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:13626
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:14691
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:11743
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:14817
bool * is_prepared
Definition: pg_backup.h:238

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

16240 {
16241  DumpOptions *dopt = fout->dopt;
16242  TableInfo *tbinfo = adinfo->adtable;
16243  int adnum = adinfo->adnum;
16244  PQExpBuffer q;
16245  PQExpBuffer delq;
16246  char *qualrelname;
16247  char *tag;
16248  char *foreign;
16249 
16250  /* Do nothing in data-only dump */
16251  if (dopt->dataOnly)
16252  return;
16253 
16254  /* Skip if not "separate"; it was dumped in the table's definition */
16255  if (!adinfo->separate)
16256  return;
16257 
16258  q = createPQExpBuffer();
16259  delq = createPQExpBuffer();
16260 
16261  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16262 
16263  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16264 
16266  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16267  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16268  adinfo->adef_expr);
16269 
16270  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16271  foreign, qualrelname,
16272  fmtId(tbinfo->attnames[adnum - 1]));
16273 
16274  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16275 
16276  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16277  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16278  ARCHIVE_OPTS(.tag = tag,
16279  .namespace = tbinfo->dobj.namespace->dobj.name,
16280  .owner = tbinfo->rolname,
16281  .description = "DEFAULT",
16282  .section = SECTION_PRE_DATA,
16283  .createStmt = q->data,
16284  .dropStmt = delq->data));
16285 
16286  free(tag);
16287  destroyPQExpBuffer(q);
16288  destroyPQExpBuffer(delq);
16289  free(qualrelname);
16290 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
DumpableObject dobj
Definition: pg_dump.h:373
char * adef_expr
Definition: pg_dump.h:376
TableInfo * adtable
Definition: pg_dump.h:374
bool separate
Definition: pg_dump.h:377
const char * rolname
Definition: pg_dump.h:285

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

10849 {
10850  DumpOptions *dopt = fout->dopt;
10852  PQExpBuffer delq = createPQExpBuffer();
10853  PQExpBuffer query = createPQExpBuffer();
10854  PGresult *res;
10855  char *qtypname;
10856  char *qualtypname;
10857  char *typlen;
10858  char *typinput;
10859  char *typoutput;
10860  char *typreceive;
10861  char *typsend;
10862  char *typmodin;
10863  char *typmodout;
10864  char *typanalyze;
10865  char *typsubscript;
10866  Oid typreceiveoid;
10867  Oid typsendoid;
10868  Oid typmodinoid;
10869  Oid typmodoutoid;
10870  Oid typanalyzeoid;
10871  Oid typsubscriptoid;
10872  char *typcategory;
10873  char *typispreferred;
10874  char *typdelim;
10875  char *typbyval;
10876  char *typalign;
10877  char *typstorage;
10878  char *typcollatable;
10879  char *typdefault;
10880  bool typdefault_is_literal = false;
10881 
10882  if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
10883  {
10884  /* Set up query for type-specific details */
10885  appendPQExpBufferStr(query,
10886  "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
10887  "SELECT typlen, "
10888  "typinput, typoutput, typreceive, typsend, "
10889  "typreceive::pg_catalog.oid AS typreceiveoid, "
10890  "typsend::pg_catalog.oid AS typsendoid, "
10891  "typanalyze, "
10892  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10893  "typdelim, typbyval, typalign, typstorage, "
10894  "typmodin, typmodout, "
10895  "typmodin::pg_catalog.oid AS typmodinoid, "
10896  "typmodout::pg_catalog.oid AS typmodoutoid, "
10897  "typcategory, typispreferred, "
10898  "(typcollation <> 0) AS typcollatable, "
10899  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
10900 
10901  if (fout->remoteVersion >= 140000)
10902  appendPQExpBufferStr(query,
10903  "typsubscript, "
10904  "typsubscript::pg_catalog.oid AS typsubscriptoid ");
10905  else
10906  appendPQExpBufferStr(query,
10907  "'-' AS typsubscript, 0 AS typsubscriptoid ");
10908 
10909  appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
10910  "WHERE oid = $1");
10911 
10912  ExecuteSqlStatement(fout, query->data);
10913 
10914  fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
10915  }
10916 
10917  printfPQExpBuffer(query,
10918  "EXECUTE dumpBaseType('%u')",
10919  tyinfo->dobj.catId.oid);
10920 
10921  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10922 
10923  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10924  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10925  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10926  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10927  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10928  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10929  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10930  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10931  typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
10932  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10933  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10934  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10935  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10936  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10937  typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
10938  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10939  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10940  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10941  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10942  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10943  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10944  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10945  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10946  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10947  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10948  {
10949  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10950  typdefault_is_literal = true; /* it needs quotes */
10951  }
10952  else
10953  typdefault = NULL;
10954 
10955  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10956  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10957 
10958  /*
10959  * The reason we include CASCADE is that the circular dependency between
10960  * the type and its I/O functions makes it impossible to drop the type any
10961  * other way.
10962  */
10963  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
10964 
10965  /*
10966  * We might already have a shell type, but setting pg_type_oid is
10967  * harmless, and in any case we'd better set the array type OID.
10968  */
10969  if (dopt->binary_upgrade)
10971  tyinfo->dobj.catId.oid,
10972  false, false);
10973 
10975  "CREATE TYPE %s (\n"
10976  " INTERNALLENGTH = %s",
10977  qualtypname,
10978  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10979 
10980  /* regproc result is sufficiently quoted already */
10981  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
10982  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
10983  if (OidIsValid(typreceiveoid))
10984  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
10985  if (OidIsValid(typsendoid))
10986  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10987  if (OidIsValid(typmodinoid))
10988  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10989  if (OidIsValid(typmodoutoid))
10990  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10991  if (OidIsValid(typanalyzeoid))
10992  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10993 
10994  if (strcmp(typcollatable, "t") == 0)
10995  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10996 
10997  if (typdefault != NULL)
10998  {
10999  appendPQExpBufferStr(q, ",\n DEFAULT = ");
11000  if (typdefault_is_literal)
11001  appendStringLiteralAH(q, typdefault, fout);
11002  else
11003  appendPQExpBufferStr(q, typdefault);
11004  }
11005 
11006  if (OidIsValid(typsubscriptoid))
11007  appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
11008 
11009  if (OidIsValid(tyinfo->typelem))
11010  appendPQExpBuffer(q, ",\n ELEMENT = %s",
11011  getFormattedTypeName(fout, tyinfo->typelem,
11012  zeroIsError));
11013 
11014  if (strcmp(typcategory, "U") != 0)
11015  {
11016  appendPQExpBufferStr(q, ",\n CATEGORY = ");
11017  appendStringLiteralAH(q, typcategory, fout);
11018  }
11019 
11020  if (strcmp(typispreferred, "t") == 0)
11021  appendPQExpBufferStr(q, ",\n PREFERRED = true");
11022 
11023  if (typdelim && strcmp(typdelim, ",") != 0)
11024  {
11025  appendPQExpBufferStr(q, ",\n DELIMITER = ");
11026  appendStringLiteralAH(q, typdelim, fout);
11027  }
11028 
11029  if (*typalign == TYPALIGN_CHAR)
11030  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
11031  else if (*typalign == TYPALIGN_SHORT)
11032  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
11033  else if (*typalign == TYPALIGN_INT)
11034  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
11035  else if (*typalign == TYPALIGN_DOUBLE)
11036  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
11037 
11038  if (*typstorage == TYPSTORAGE_PLAIN)
11039  appendPQExpBufferStr(q, ",\n STORAGE = plain");
11040  else if (*typstorage == TYPSTORAGE_EXTERNAL)
11041  appendPQExpBufferStr(q, ",\n STORAGE = external");
11042  else if (*typstorage == TYPSTORAGE_EXTENDED)
11043  appendPQExpBufferStr(q, ",\n STORAGE = extended");
11044  else if (*typstorage == TYPSTORAGE_MAIN)
11045  appendPQExpBufferStr(q, ",\n STORAGE = main");
11046 
11047  if (strcmp(typbyval, "t") == 0)
11048  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
11049 
11050  appendPQExpBufferStr(q, "\n);\n");
11051 
11052  if (dopt->binary_upgrade)
11054  "TYPE", qtypname,
11055  tyinfo->dobj.namespace->dobj.name);
11056 
11057  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11058  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11059  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11060  .namespace = tyinfo->dobj.namespace->dobj.name,
11061  .owner = tyinfo->rolname,
11062  .description = "TYPE",
11063  .section = SECTION_PRE_DATA,
11064  .createStmt = q->data,
11065  .dropStmt = delq->data));
11066 
11067  /* Dump Type Comments and Security Labels */
11068  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11069  dumpComment(fout, "TYPE", qtypname,
11070  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11071  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11072 
11073  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11074  dumpSecLabel(fout, "TYPE", qtypname,
11075  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11076  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11077 
11078  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11079  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11080  qtypname, NULL,
11081  tyinfo->dobj.namespace->dobj.name,
11082  tyinfo->rolname, &tyinfo->dacl);
11083 
11084  PQclear(res);
11085  destroyPQExpBuffer(q);
11086  destroyPQExpBuffer(delq);
11087  destroyPQExpBuffer(query);
11088  free(qtypname);
11089  free(qualtypname);
11090 }
@ PREPQUERY_DUMPBASETYPE
Definition: pg_backup.h:66
static const char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18372
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 12210 of file pg_dump.c.

12211 {
12212  DumpOptions *dopt = fout->dopt;
12213  PQExpBuffer defqry;
12214  PQExpBuffer delqry;
12215  PQExpBuffer labelq;
12216  PQExpBuffer castargs;
12217  FuncInfo *funcInfo = NULL;
12218  const char *sourceType;
12219  const char *targetType;
12220 
12221  /* Do nothing in data-only dump */
12222  if (dopt->dataOnly)
12223  return;
12224 
12225  /* Cannot dump if we don't have the cast function's info */
12226  if (OidIsValid(cast->castfunc))
12227  {
12228  funcInfo = findFuncByOid(cast->castfunc);
12229  if (funcInfo == NULL)
12230  pg_fatal("could not find function definition for function with OID %u",
12231  cast->castfunc);
12232  }
12233 
12234  defqry = createPQExpBuffer();
12235  delqry = createPQExpBuffer();
12236  labelq = createPQExpBuffer();
12237  castargs = createPQExpBuffer();
12238 
12239  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12240  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12241  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12242  sourceType, targetType);
12243 
12244  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12245  sourceType, targetType);
12246 
12247  switch (cast->castmethod)
12248  {
12249  case COERCION_METHOD_BINARY:
12250  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12251  break;
12252  case COERCION_METHOD_INOUT:
12253  appendPQExpBufferStr(defqry, "WITH INOUT");
12254  break;
12255  case COERCION_METHOD_FUNCTION:
12256  if (funcInfo)
12257  {
12258  char *fsig = format_function_signature(fout, funcInfo, true);
12259 
12260  /*
12261  * Always qualify the function name (format_function_signature
12262  * won't qualify it).
12263  */
12264  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12265  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12266  free(fsig);
12267  }
12268  else
12269  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12270  break;
12271  default:
12272  pg_log_warning("bogus value in pg_cast.castmethod field");
12273  }
12274 
12275  if (cast->castcontext == 'a')
12276  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12277  else if (cast->castcontext == 'i')
12278  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12279  appendPQExpBufferStr(defqry, ";\n");
12280 
12281  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12282  sourceType, targetType);
12283 
12284  appendPQExpBuffer(castargs, "(%s AS %s)",
12285  sourceType, targetType);
12286 
12287  if (dopt->binary_upgrade)
12288  binary_upgrade_extension_member(defqry, &cast->dobj,
12289  "CAST", castargs->data, NULL);
12290 
12291  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12292  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12293  ARCHIVE_OPTS(.tag = labelq->data,
12294  .description = "CAST",
12295  .section = SECTION_PRE_DATA,
12296  .createStmt = defqry->data,
12297  .dropStmt = delqry->data));
12298 
12299  /* Dump Cast Comments */
12300  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12301  dumpComment(fout, "CAST", castargs->data,
12302  NULL, "",
12303  cast->dobj.catId, 0, cast->dobj.dumpId);
12304 
12305  destroyPQExpBuffer(defqry);
12306  destroyPQExpBuffer(delqry);
12307  destroyPQExpBuffer(labelq);
12308  destroyPQExpBuffer(castargs);
12309 }
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:875
char castmethod
Definition: pg_dump.h:506
Oid casttarget
Definition: pg_dump.h:503
char castcontext
Definition: pg_dump.h:505
DumpableObject dobj
Definition: pg_dump.h:501
Oid castsource
Definition: pg_dump.h:502
Oid castfunc
Definition: pg_dump.h:504
DumpableObject dobj
Definition: pg_dump.h: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 13324 of file pg_dump.c.

13325 {
13326  DumpOptions *dopt = fout->dopt;
13327  PQExpBuffer query;
13328  PQExpBuffer q;
13329  PQExpBuffer delq;
13330  char *qcollname;
13331  PGresult *res;
13332  int i_collprovider;
13333  int i_collisdeterministic;
13334  int i_collcollate;
13335  int i_collctype;
13336  int i_colliculocale;
13337  int i_collicurules;
13338  const char *collprovider;
13339  const char *collcollate;
13340  const char *collctype;
13341  const char *colliculocale;
13342  const char *collicurules;
13343 
13344  /* Do nothing in data-only dump */
13345  if (dopt->dataOnly)
13346  return;
13347 
13348  query = createPQExpBuffer();
13349  q = createPQExpBuffer();
13350  delq = createPQExpBuffer();
13351 
13352  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13353 
13354  /* Get collation-specific details */
13355  appendPQExpBufferStr(query, "SELECT ");
13356 
13357  if (fout->remoteVersion >= 100000)
13358  appendPQExpBufferStr(query,
13359  "collprovider, "
13360  "collversion, ");
13361  else
13362  appendPQExpBufferStr(query,
13363  "'c' AS collprovider, "
13364  "NULL AS collversion, ");
13365 
13366  if (fout->remoteVersion >= 120000)
13367  appendPQExpBufferStr(query,
13368  "collisdeterministic, ");
13369  else
13370  appendPQExpBufferStr(query,
13371  "true AS collisdeterministic, ");
13372 
13373  if (fout->remoteVersion >= 150000)
13374  appendPQExpBufferStr(query,
13375  "colliculocale, ");
13376  else
13377  appendPQExpBufferStr(query,
13378  "NULL AS colliculocale, ");
13379 
13380  if (fout->remoteVersion >= 160000)
13381  appendPQExpBufferStr(query,
13382  "collicurules, ");
13383  else
13384  appendPQExpBufferStr(query,
13385  "NULL AS collicurules, ");
13386 
13387  appendPQExpBuffer(query,
13388  "collcollate, "
13389  "collctype "
13390  "FROM pg_catalog.pg_collation c "
13391  "WHERE c.oid = '%u'::pg_catalog.oid",
13392  collinfo->dobj.catId.oid);
13393 
13394  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13395 
13396  i_collprovider = PQfnumber(res, "collprovider");
13397  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13398  i_collcollate = PQfnumber(res, "collcollate");
13399  i_collctype = PQfnumber(res, "collctype");
13400  i_colliculocale = PQfnumber(res, "colliculocale");
13401  i_collicurules = PQfnumber(res, "collicurules");
13402 
13403  collprovider = PQgetvalue(res, 0, i_collprovider);
13404 
13405  if (!PQgetisnull(res, 0, i_collcollate))
13406  collcollate = PQgetvalue(res, 0, i_collcollate);
13407  else
13408  collcollate = NULL;
13409 
13410  if (!PQgetisnull(res, 0, i_collctype))
13411  collctype = PQgetvalue(res, 0, i_collctype);
13412  else
13413  collctype = NULL;
13414 
13415  if (!PQgetisnull(res, 0, i_colliculocale))
13416  colliculocale = PQgetvalue(res, 0, i_colliculocale);
13417  else
13418  colliculocale = NULL;
13419 
13420  if (!PQgetisnull(res, 0, i_collicurules))
13421  collicurules = PQgetvalue(res, 0, i_collicurules);
13422  else
13423  collicurules = NULL;
13424 
13425  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13426  fmtQualifiedDumpable(collinfo));
13427 
13428  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13429  fmtQualifiedDumpable(collinfo));
13430 
13431  appendPQExpBufferStr(q, "provider = ");
13432  if (collprovider[0] == 'c')
13433  appendPQExpBufferStr(q, "libc");
13434  else if (collprovider[0] == 'i')
13435  appendPQExpBufferStr(q, "icu");
13436  else if (collprovider[0] == 'd')
13437  /* to allow dumping pg_catalog; not accepted on input */
13438  appendPQExpBufferStr(q, "default");
13439  else
13440  pg_fatal("unrecognized collation provider: %s",
13441  collprovider);
13442 
13443  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13444  appendPQExpBufferStr(q, ", deterministic = false");
13445 
13446  if (colliculocale != NULL)
13447  {
13448  appendPQExpBufferStr(q, ", locale = ");
13449  appendStringLiteralAH(q, colliculocale, fout);
13450  }
13451  else
13452  {
13453  Assert(collcollate != NULL);
13454  Assert(collctype != NULL);
13455 
13456  if (strcmp(collcollate, collctype) == 0)
13457  {
13458  appendPQExpBufferStr(q, ", locale = ");
13459  appendStringLiteralAH(q, collcollate, fout);
13460  }
13461  else
13462  {
13463  appendPQExpBufferStr(q, ", lc_collate = ");
13464  appendStringLiteralAH(q, collcollate, fout);
13465  appendPQExpBufferStr(q, ", lc_ctype = ");
13466  appendStringLiteralAH(q, collctype, fout);
13467  }
13468  }
13469 
13470  if (collicurules)
13471  {
13472  appendPQExpBufferStr(q, ", rules = ");
13473  appendStringLiteralAH(q, collicurules, fout);
13474  }
13475 
13476  /*
13477  * For binary upgrade, carry over the collation version. For normal
13478  * dump/restore, omit the version, so that it is computed upon restore.
13479  */
13480  if (dopt->binary_upgrade)
13481  {
13482  int i_collversion;
13483 
13484  i_collversion = PQfnumber(res, "collversion");
13485  if (!PQgetisnull(res, 0, i_collversion))
13486  {
13487  appendPQExpBufferStr(q, ", version = ");
13489  PQgetvalue(res, 0, i_collversion),
13490  fout);
13491  }
13492  }
13493 
13494  appendPQExpBufferStr(q, ");\n");
13495 
13496  if (dopt->binary_upgrade)
13497  binary_upgrade_extension_member(q, &collinfo->dobj,
13498  "COLLATION", qcollname,
13499  collinfo->dobj.namespace->dobj.name);
13500 
13501  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13502  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13503  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13504  .namespace = collinfo->dobj.namespace->dobj.name,
13505  .owner = collinfo->rolname,
13506  .description = "COLLATION",
13507  .section = SECTION_PRE_DATA,
13508  .createStmt = q->data,
13509  .dropStmt = delq->data));
13510 
13511  /* Dump Collation Comments */
13512  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13513  dumpComment(fout, "COLLATION", qcollname,
13514  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13515  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13516 
13517  PQclear(res);
13518 
13519  destroyPQExpBuffer(query);
13520  destroyPQExpBuffer(q);
13521  destroyPQExpBuffer(delq);
13522  free(qcollname);
13523 }
const char * rolname
Definition: pg_dump.h:269
DumpableObject dobj
Definition: pg_dump.h:268

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), Assert(), _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_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 9790 of file pg_dump.c.

9794 {
9795  dumpCommentExtended(fout, type, name, namespace, owner,
9796  catalogId, subid, dumpId, NULL);
9797 }
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:9690

References dumpCommentExtended(), name, and generate_unaccent_rules::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 9690 of file pg_dump.c.

9695 {
9696  DumpOptions *dopt = fout->dopt;
9698  int ncomments;
9699 
9700  /* do nothing, if --no-comments is supplied */
9701  if (dopt->no_comments)
9702  return;
9703 
9704  /* Comments are schema not data ... except LO comments are data */
9705  if (strcmp(type, "LARGE OBJECT") != 0)
9706  {
9707  if (dopt->dataOnly)
9708  return;
9709  }
9710  else
9711  {
9712  /* We do dump LO comments in binary-upgrade mode */
9713  if (dopt->schemaOnly && !dopt->binary_upgrade)
9714  return;
9715  }
9716 
9717  /* Search for comments associated with catalogId, using table */
9718  ncomments = findComments(catalogId.tableoid, catalogId.oid,
9719  &comments);
9720 
9721  /* Is there one matching the subid? */
9722  while (ncomments > 0)
9723  {
9724  if (comments->objsubid == subid)
9725  break;
9726  comments++;
9727  ncomments--;
9728  }
9729 
9730  if (initdb_comment != NULL)
9731  {
9732  static CommentItem empty_comment = {.descr = ""};
9733 
9734  /*
9735  * initdb creates this object with a comment. Skip dumping the
9736  * initdb-provided comment, which would complicate matters for
9737  * non-superuser use of pg_dump. When the DBA has removed initdb's
9738  * comment, replicate that.
9739  */
9740  if (ncomments == 0)
9741  {
9742  comments = &empty_comment;
9743  ncomments = 1;
9744  }
9745  else if (strcmp(comments->descr, initdb_comment) == 0)
9746  ncomments = 0;
9747  }
9748 
9749  /* If a comment exists, build COMMENT ON statement */
9750  if (ncomments > 0)
9751  {
9752  PQExpBuffer query = createPQExpBuffer();
9754 
9755  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9756  if (namespace && *namespace)
9757  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9758  appendPQExpBuffer(query, "%s IS ", name);
9759  appendStringLiteralAH(query, comments->descr, fout);
9760  appendPQExpBufferStr(query, ";\n");
9761 
9762  appendPQExpBuffer(tag, "%s %s", type, name);
9763 
9764  /*
9765  * We mark comments as SECTION_NONE because they really belong in the
9766  * same section as their parent, whether that is pre-data or
9767  * post-data.
9768  */
9770  ARCHIVE_OPTS(.tag = tag->data,
9771  .namespace = namespace,
9772  .owner = owner,
9773  .description = "COMMENT",
9774  .section = SECTION_NONE,
9775  .createStmt = query->data,
9776  .deps = &dumpId,
9777  .nDeps = 1));
9778 
9779  destroyPQExpBuffer(query);
9780  destroyPQExpBuffer(tag);
9781  }
9782 }
static int findComments(Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9904
int no_comments
Definition: pg_backup.h:177
bool schemaOnly
Definition: pg_backup.h:166

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 generate_unaccent_rules::type.

Referenced by dumpComment(), and dumpNamespace().

◆ dumpCompositeType()

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

Definition at line 11270 of file pg_dump.c.

11271 {
11272  DumpOptions *dopt = fout->dopt;
11274  PQExpBuffer dropped = createPQExpBuffer();
11275  PQExpBuffer delq = createPQExpBuffer();
11276  PQExpBuffer query = createPQExpBuffer();
11277  PGresult *res;
11278  char *qtypname;
11279  char *qualtypname;
11280  int ntups;
11281  int i_attname;
11282  int i_atttypdefn;
11283  int i_attlen;
11284  int i_attalign;
11285  int i_attisdropped;
11286  int i_attcollation;
11287  int i;
11288  int actual_atts;
11289 
11291  {
11292  /*
11293  * Set up query for type-specific details.
11294  *
11295  * Since we only want to dump COLLATE clauses for attributes whose
11296  * collation is different from their type's default, we use a CASE
11297  * here to suppress uninteresting attcollations cheaply. atttypid
11298  * will be 0 for dropped columns; collation does not matter for those.
11299  */
11300  appendPQExpBufferStr(query,
11301  "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
11302  "SELECT a.attname, a.attnum, "
11303  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11304  "a.attlen, a.attalign, a.attisdropped, "
11305  "CASE WHEN a.attcollation <> at.typcollation "
11306  "THEN a.attcollation ELSE 0 END AS attcollation "
11307  "FROM pg_catalog.pg_type ct "
11308  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11309  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11310  "WHERE ct.oid = $1 "
11311  "ORDER BY a.attnum");
11312 
11313  ExecuteSqlStatement(fout, query->data);
11314 
11316  }
11317 
11318  printfPQExpBuffer(query,
11319  "EXECUTE dumpCompositeType('%u')",
11320  tyinfo->dobj.catId.oid);
11321 
11322  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11323 
11324  ntups = PQntuples(res);
11325 
11326  i_attname = PQfnumber(res, "attname");
11327  i_atttypdefn = PQfnumber(res, "atttypdefn");
11328  i_attlen = PQfnumber(res, "attlen");
11329  i_attalign = PQfnumber(res, "attalign");
11330  i_attisdropped = PQfnumber(res, "attisdropped");
11331  i_attcollation = PQfnumber(res, "attcollation");
11332 
11333  if (dopt->binary_upgrade)
11334  {
11336  tyinfo->dobj.catId.oid,
11337  false, false);
11338  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11339  }
11340 
11341  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11342  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11343 
11344  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11345  qualtypname);
11346 
11347  actual_atts = 0;
11348  for (i = 0; i < ntups; i++)
11349  {
11350  char *attname;
11351  char *atttypdefn;
11352  char *attlen;
11353  char *attalign;
11354  bool attisdropped;
11355  Oid attcollation;
11356 
11357  attname = PQgetvalue(res, i, i_attname);
11358  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11359  attlen = PQgetvalue(res, i, i_attlen);
11360  attalign = PQgetvalue(res, i, i_attalign);
11361  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11362  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11363 
11364  if (attisdropped && !dopt->binary_upgrade)
11365  continue;
11366 
11367  /* Format properly if not first attr */
11368  if (actual_atts++ > 0)
11369  appendPQExpBufferChar(q, ',');
11370  appendPQExpBufferStr(q, "\n\t");
11371 
11372  if (!attisdropped)
11373  {
11374  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11375 
11376  /* Add collation if not default for the column type */
11377  if (OidIsValid(attcollation))
11378  {
11379  CollInfo *coll;
11380 
11381  coll = findCollationByOid(attcollation);
11382  if (coll)
11383  appendPQExpBuffer(q, " COLLATE %s",
11384  fmtQualifiedDumpable(coll));
11385  }
11386  }
11387  else
11388  {
11389  /*
11390  * This is a dropped attribute and we're in binary_upgrade mode.
11391  * Insert a placeholder for it in the CREATE TYPE command, and set
11392  * length and alignment with direct UPDATE to the catalogs
11393  * afterwards. See similar code in dumpTableSchema().
11394  */
11395  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11396 
11397  /* stash separately for insertion after the CREATE TYPE */
11398  appendPQExpBufferStr(dropped,
11399  "\n-- For binary upgrade, recreate dropped column.\n");
11400  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11401  "SET attlen = %s, "
11402  "attalign = '%s', attbyval = false\n"
11403  "WHERE attname = ", attlen, attalign);
11404  appendStringLiteralAH(dropped, attname, fout);
11405  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11406  appendStringLiteralAH(dropped, qualtypname, fout);
11407  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11408 
11409  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11410  qualtypname);
11411  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11412  fmtId(attname));
11413  }
11414  }
11415  appendPQExpBufferStr(q, "\n);\n");
11416  appendPQExpBufferStr(q, dropped->data);
11417 
11418  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11419 
11420  if (dopt->binary_upgrade)
11422  "TYPE", qtypname,
11423  tyinfo->dobj.namespace->dobj.name);
11424 
11425  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11426  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11427  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11428  .namespace = tyinfo->dobj.namespace->dobj.name,
11429  .owner = tyinfo->rolname,
11430  .description = "TYPE",
11431  .section = SECTION_PRE_DATA,
11432  .createStmt = q->data,
11433  .dropStmt = delq->data));
11434 
11435 
11436  /* Dump Type Comments and Security Labels */
11437  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11438  dumpComment(fout, "TYPE", qtypname,
11439  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11440  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11441 
11442  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11443  dumpSecLabel(fout, "TYPE", qtypname,
11444  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11445  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11446 
11447  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11448  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11449  qtypname, NULL,
11450  tyinfo->dobj.namespace->dobj.name,
11451  tyinfo->rolname, &tyinfo->dacl);
11452 
11453  /* Dump any per-column comments */
11454  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11455  dumpCompositeTypeColComments(fout, tyinfo, res);
11456 
11457  PQclear(res);
11458  destroyPQExpBuffer(q);
11459  destroyPQExpBuffer(dropped);
11460  destroyPQExpBuffer(delq);
11461  destroyPQExpBuffer(query);
11462  free(qtypname);
11463  free(qualtypname);
11464 }
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:67
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:5007
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, PGresult *res)
Definition: pg_dump.c:11476
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 11476 of file pg_dump.c.

11478 {
11480  int ncomments;
11481  PQExpBuffer query;
11482  PQExpBuffer target;
11483  int i;
11484  int ntups;
11485  int i_attname;
11486  int i_attnum;
11487  int i_attisdropped;
11488 
11489  /* do nothing, if --no-comments is supplied */
11490  if (fout->dopt->no_comments)
11491  return;
11492 
11493  /* Search for comments associated with type's pg_class OID */
11494  ncomments = findComments(RelationRelationId, tyinfo->typrelid,
11495  &comments);
11496 
11497  /* If no comments exist, we're done */
11498  if (ncomments <= 0)
11499  return;
11500 
11501  /* Build COMMENT ON statements */
11502  query = createPQExpBuffer();
11503  target = createPQExpBuffer();
11504 
11505  ntups = PQntuples(res);
11506  i_attnum = PQfnumber(res, "attnum");
11507  i_attname = PQfnumber(res, "attname");
11508  i_attisdropped = PQfnumber(res, "attisdropped");
11509  while (ncomments > 0)
11510  {
11511  const char *attname;
11512 
11513  attname = NULL;
11514  for (i = 0; i < ntups; i++)
11515  {
11516  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
11517  PQgetvalue(res, i, i_attisdropped)[0] != 't')
11518  {
11519  attname = PQgetvalue(res, i, i_attname);
11520  break;
11521  }
11522  }
11523  if (attname) /* just in case we don't find it */
11524  {
11525  const char *descr = comments->descr;
11526 
11527  resetPQExpBuffer(target);
11528  appendPQExpBuffer(target, "COLUMN %s.",
11529  fmtId(tyinfo->dobj.name));
11531 
11532  resetPQExpBuffer(query);
11533  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11534  fmtQualifiedDumpable(tyinfo));
11535  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11536  appendStringLiteralAH(query, descr, fout);
11537  appendPQExpBufferStr(query, ";\n");
11538 
11540  ARCHIVE_OPTS(.tag = target->data,
11541  .namespace = tyinfo->dobj.namespace->dobj.name,
11542  .owner = tyinfo->rolname,
11543  .description = "COMMENT",
11544  .section = SECTION_NONE,
11545  .createStmt = query->data,
11546  .deps = &(tyinfo->dobj.dumpId),
11547  .nDeps = 1));
11548  }
11549 
11550  comments++;
11551  ncomments--;
11552  }
11553 
11554  destroyPQExpBuffer(query);
11555  destroyPQExpBuffer(target);
11556 }
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 16587 of file pg_dump.c.

16588 {
16589  DumpOptions *dopt = fout->dopt;
16590  TableInfo *tbinfo = coninfo->contable;
16591  PQExpBuffer q;
16592  PQExpBuffer delq;
16593  char *tag = NULL;
16594  char *foreign;
16595 
16596  /* Do nothing in data-only dump */
16597  if (dopt->dataOnly)
16598  return;
16599 
16600  q = createPQExpBuffer();
16601  delq = createPQExpBuffer();
16602 
16603  foreign = tbinfo &&
16604  tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16605 
16606  if (coninfo->contype == 'p' ||
16607  coninfo->contype == 'u' ||
16608  coninfo->contype == 'x')
16609  {
16610  /* Index-related constraint */
16611  IndxInfo *indxinfo;
16612  int k;
16613 
16614  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16615 
16616  if (indxinfo == NULL)
16617  pg_fatal("missing index for constraint \"%s\"",
16618  coninfo->dobj.name);
16619 
16620  if (dopt->binary_upgrade)
16622  indxinfo->dobj.catId.oid, true);
16623 
16624  appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
16625  fmtQualifiedDumpable(tbinfo));
16626  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
16627  fmtId(coninfo->dobj.name));
16628 
16629  if (coninfo->condef)
16630  {
16631  /* pg_get_constraintdef should have provided everything */
16632  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16633  }
16634  else
16635  {
16637  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16638 
16639  /*
16640  * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
16641  * indexes. Being able to create this was fixed, but we need to
16642  * make the index distinct in order to be able to restore the
16643  * dump.
16644  */
16645  if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
16646  appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
16647  appendPQExpBufferStr(q, " (");
16648  for (k = 0; k < indxinfo->indnkeyattrs; k++)
16649  {
16650  int indkey = (int) indxinfo->indkeys[k];
16651  const char *attname;
16652 
16653  if (indkey == InvalidAttrNumber)
16654  break;
16655  attname = getAttrName(indkey, tbinfo);
16656 
16657  appendPQExpBuffer(q, "%s%s",
16658  (k == 0) ? "" : ", ",
16659  fmtId(attname));
16660  }
16661 
16662  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
16663  appendPQExpBufferStr(q, ") INCLUDE (");
16664 
16665  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
16666  {
16667  int indkey = (int) indxinfo->indkeys[k];
16668  const char *attname;
16669 
16670  if (indkey == InvalidAttrNumber)
16671  break;
16672  attname = getAttrName(indkey, tbinfo);
16673 
16674  appendPQExpBuffer(q, "%s%s",
16675  (k == indxinfo->indnkeyattrs) ? "" : ", ",
16676  fmtId(attname));
16677  }
16678 
16679  appendPQExpBufferChar(q, ')');
16680 
16681  if (nonemptyReloptions(indxinfo->indreloptions))
16682  {
16683  appendPQExpBufferStr(q, " WITH (");
16684  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16685  appendPQExpBufferChar(q, ')');
16686  }
16687 
16688  if (coninfo->condeferrable)
16689  {
16690  appendPQExpBufferStr(q, " DEFERRABLE");
16691  if (coninfo->condeferred)
16692  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16693  }
16694 
16695  appendPQExpBufferStr(q, ";\n");
16696  }
16697 
16698  /*
16699  * Append ALTER TABLE commands as needed to set properties that we
16700  * only have ALTER TABLE syntax for. Keep this in sync with the
16701  * similar code in dumpIndex!
16702  */
16703 
16704  /* If the index is clustered, we need to record that. */
16705  if (indxinfo->indisclustered)
16706  {
16707  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16708  fmtQualifiedDumpable(tbinfo));
16709  /* index name is not qualified in this syntax */
16710  appendPQExpBuffer(q, " ON %s;\n",
16711  fmtId(indxinfo->dobj.name));
16712  }
16713 
16714  /* If the index defines identity, we need to record that. */
16715  if (indxinfo->indisreplident)
16716  {
16717  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
16718  fmtQualifiedDumpable(tbinfo));
16719  /* index name is not qualified in this syntax */
16720  appendPQExpBuffer(q, " INDEX %s;\n",
16721  fmtId(indxinfo->dobj.name));
16722  }
16723 
16724  /* Indexes can depend on extensions */
16725  append_depends_on_extension(fout, q, &indxinfo->dobj,
16726  "pg_catalog.pg_class", "INDEX",
16727  fmtQualifiedDumpable(indxinfo));
16728 
16729  appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
16730  fmtQualifiedDumpable(tbinfo));
16731  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16732  fmtId(coninfo->dobj.name));
16733 
16734  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16735 
16736  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16737  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16738  ARCHIVE_OPTS(.tag = tag,
16739  .namespace = tbinfo->dobj.namespace->dobj.name,
16740  .tablespace = indxinfo->tablespace,
16741  .owner = tbinfo->rolname,
16742  .description = "CONSTRAINT",
16743  .section = SECTION_POST_DATA,
16744  .createStmt = q->data,
16745  .dropStmt = delq->data));
16746  }
16747  else if (coninfo->contype == 'f')
16748  {
16749  char *only;
16750 
16751  /*
16752  * Foreign keys on partitioned tables are always declared as
16753  * inheriting to partitions; for all other cases, emit them as
16754  * applying ONLY directly to the named table, because that's how they
16755  * work for regular inherited tables.
16756  */
16757  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
16758 
16759  /*
16760  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
16761  * current table data is not processed
16762  */
16763  appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
16764  only, fmtQualifiedDumpable(tbinfo));
16765  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16766  fmtId(coninfo->dobj.name),
16767  coninfo->condef);
16768 
16769  appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
16770  only, fmtQualifiedDumpable(tbinfo));
16771  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16772  fmtId(coninfo->dobj.name));
16773 
16774  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16775 
16776  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16777  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16778  ARCHIVE_OPTS(.tag = tag,
16779  .namespace = tbinfo->dobj.namespace->dobj.name,
16780  .owner = tbinfo->rolname,
16781  .description = "FK CONSTRAINT",
16782  .section = SECTION_POST_DATA,
16783  .createStmt = q->data,
16784  .dropStmt = delq->data));
16785  }
16786  else if (coninfo->contype == 'c' && tbinfo)
16787  {
16788  /* CHECK constraint on a table */
16789 
16790  /* Ignore if not to be dumped separately, or if it was inherited */
16791  if (coninfo->separate && coninfo->conislocal)
16792  {
16793  /* not ONLY since we want it to propagate to children */
16794  appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
16795  fmtQualifiedDumpable(tbinfo));
16796  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16797  fmtId(coninfo->dobj.name),
16798  coninfo->condef);
16799 
16800  appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
16801  fmtQualifiedDumpable(tbinfo));
16802  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16803  fmtId(coninfo->dobj.name));
16804 
16805  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16806 
16807  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16808  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16809  ARCHIVE_OPTS(.tag = tag,
16810  .namespace = tbinfo->dobj.namespace->dobj.name,
16811  .owner = tbinfo->rolname,
16812  .description = "CHECK CONSTRAINT",
16813  .section = SECTION_POST_DATA,
16814  .createStmt = q->data,
16815  .dropStmt = delq->data));
16816  }
16817  }
16818  else if (coninfo->contype == 'c' && tbinfo == NULL)
16819  {
16820  /* CHECK constraint on a domain */
16821  TypeInfo *tyinfo = coninfo->condomain;
16822 
16823  /* Ignore if not to be dumped separately */
16824  if (coninfo->separate)
16825  {
16826  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
16827  fmtQualifiedDumpable(tyinfo));
16828  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16829  fmtId(coninfo->dobj.name),
16830  coninfo->condef);
16831 
16832  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
16833  fmtQualifiedDumpable(tyinfo));
16834  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16835  fmtId(coninfo->dobj.name));
16836 
16837  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
16838 
16839  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16840  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16841  ARCHIVE_OPTS(.tag = tag,
16842  .namespace = tyinfo->dobj.namespace->dobj.name,
16843  .owner = tyinfo->rolname,
16844  .description = "CHECK CONSTRAINT",
16845  .section = SECTION_POST_DATA,
16846  .createStmt = q->data,
16847  .dropStmt = delq->data));
16848  }
16849  }
16850  else
16851  {
16852  pg_fatal("unrecognized constraint type: %c",
16853  coninfo->contype);
16854  }
16855 
16856  /* Dump Constraint Comments --- only works for table constraints */
16857  if (tbinfo && coninfo->separate &&
16858  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16859  dumpTableConstraintComment(fout, coninfo);
16860 
16861  free(tag);
16862  destroyPQExpBuffer(q);
16863  destroyPQExpBuffer(delq);
16864 }
#define InvalidAttrNumber
Definition: attnum.h:23
@ SECTION_POST_DATA
Definition: pg_backup.h:59
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:4834
static const char * getAttrName(int attrnum, const TableInfo *tblInfo)
Definition: pg_dump.c:16300
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
Definition: pg_dump.c:18469
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
Definition: pg_dump.c:16874
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18457
TypeInfo * condomain
Definition: pg_dump.h:477
TableInfo * contable
Definition: pg_dump.h:476
bool condeferred
Definition: pg_dump.h:483
bool conislocal
Definition: pg_dump.h:484
DumpableObject dobj
Definition: pg_dump.h:475
DumpId conindex
Definition: pg_dump.h:481
bool condeferrable
Definition: pg_dump.h:482
char * condef
Definition: pg_dump.h:479
bool indisreplident
Definition: pg_dump.h:401
int indnkeyattrs
Definition: pg_dump.h:396
int indnattrs
Definition: pg_dump.h:397
Oid * indkeys
Definition: pg_dump.h:398
char * indreloptions
Definition: pg_dump.h:393
bool indisclustered
Definition: pg_dump.h:400
char * tablespace
Definition: pg_dump.h:392
bool indnullsnotdistinct
Definition: pg_dump.h:402
DumpableObject dobj
Definition: pg_dump.h:389

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

Referenced by dumpDumpableObject().

◆ dumpConversion()

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

Definition at line 13530 of file pg_dump.c.

13531 {
13532  DumpOptions *dopt = fout->dopt;
13533  PQExpBuffer query;
13534  PQExpBuffer q;
13535  PQExpBuffer delq;
13536  char *qconvname;
13537  PGresult *res;
13538  int i_conforencoding;
13539  int i_contoencoding;
13540  int i_conproc;
13541  int i_condefault;
13542  const char *conforencoding;
13543  const char *contoencoding;
13544  const char *conproc;
13545  bool condefault;
13546 
13547  /* Do nothing in data-only dump */
13548  if (dopt->dataOnly)
13549  return;
13550 
13551  query = createPQExpBuffer();
13552  q = createPQExpBuffer();
13553  delq = createPQExpBuffer();
13554 
13555  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13556 
13557  /* Get conversion-specific details */
13558  appendPQExpBuffer(query, "SELECT "
13559  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13560  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13561  "conproc, condefault "
13562  "FROM pg_catalog.pg_conversion c "
13563  "WHERE c.oid = '%u'::pg_catalog.oid",
13564  convinfo->dobj.catId.oid);
13565 
13566  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13567 
13568  i_conforencoding = PQfnumber(res, "conforencoding");
13569  i_contoencoding = PQfnumber(res, "contoencoding");
13570  i_conproc = PQfnumber(res, "conproc");
13571  i_condefault = PQfnumber(res, "condefault");
13572 
13573  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13574  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13575  conproc = PQgetvalue(res, 0, i_conproc);
13576  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13577 
13578  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13579  fmtQualifiedDumpable(convinfo));
13580 
13581  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13582  (condefault) ? "DEFAULT " : "",
13583  fmtQualifiedDumpable(convinfo));
13584  appendStringLiteralAH(q, conforencoding, fout);
13585  appendPQExpBufferStr(q, " TO ");
13586  appendStringLiteralAH(q, contoencoding, fout);
13587  /* regproc output is already sufficiently quoted */
13588  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13589 
13590  if (dopt->binary_upgrade)
13591  binary_upgrade_extension_member(q, &convinfo->dobj,
13592  "CONVERSION", qconvname,
13593  convinfo->dobj.namespace->dobj.name);
13594 
13595  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13596  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13597  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13598  .namespace = convinfo->dobj.namespace->dobj.name,
13599  .owner = convinfo->rolname,
13600  .description = "CONVERSION",
13601  .section = SECTION_PRE_DATA,
13602  .createStmt = q->data,
13603  .dropStmt = delq->data));
13604 
13605  /* Dump Conversion Comments */
13606  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13607  dumpComment(fout, "CONVERSION", qconvname,
13608  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13609  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13610 
13611  PQclear(res);
13612 
13613  destroyPQExpBuffer(query);
13614  destroyPQExpBuffer(q);
13615  destroyPQExpBuffer(delq);
13616  free(qconvname);
13617 }
DumpableObject dobj
Definition: pg_dump.h:274
const char * rolname
Definition: pg_dump.h:275

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

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

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

3382 {
3383  PGconn *conn = GetConnection(AH);
3385  PGresult *res;
3386 
3387  /* First collect database-specific options */
3388  printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3389  "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3390  dboid);
3391 
3392  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3393 
3394  for (int i = 0; i < PQntuples(res); i++)
3396  "DATABASE", dbname, NULL, NULL,
3397  outbuf);
3398 
3399  PQclear(res);
3400 
3401  /* Now look for role-and-database-specific options */
3402  printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3403  "FROM pg_db_role_setting s, pg_roles r "
3404  "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3405  dboid);
3406 
3407  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3408 
3409  for (int i = 0; i < PQntuples(res); i++)
3411  "ROLE", PQgetvalue(res, i, 0),
3412  "DATABASE", dbname,
3413  outbuf);
3414 
3415  PQclear(res);
3416 
3418 }
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
Definition: dumputils.c:823
static char * buf
Definition: pg_test_fsync.c: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 14604 of file pg_dump.c.

14605 {
14606  DumpOptions *dopt = fout->dopt;
14607  PQExpBuffer q;
14608  PQExpBuffer tag;
14609  const char *type;
14610 
14611  /* Do nothing in data-only dump, or if we're skipping ACLs */
14612  if (dopt->dataOnly || dopt->aclsSkip)
14613  return;
14614 
14615  q = createPQExpBuffer();
14616  tag = createPQExpBuffer();
14617 
14618  switch (daclinfo->defaclobjtype)
14619  {
14620  case DEFACLOBJ_RELATION:
14621  type = "TABLES";
14622  break;
14623  case DEFACLOBJ_SEQUENCE:
14624  type = "SEQUENCES";
14625  break;
14626  case DEFACLOBJ_FUNCTION:
14627  type = "FUNCTIONS";
14628  break;
14629  case DEFACLOBJ_TYPE:
14630  type = "TYPES";
14631  break;
14632  case DEFACLOBJ_NAMESPACE:
14633  type = "SCHEMAS";
14634  break;
14635  default:
14636  /* shouldn't get here */
14637  pg_fatal("unrecognized object type in default privileges: %d",
14638  (int) daclinfo->defaclobjtype);
14639  type = ""; /* keep compiler quiet */
14640  }
14641 
14642  appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
14643 
14644  /* build the actual command(s) for this tuple */
14646  daclinfo->dobj.namespace != NULL ?
14647  daclinfo->dobj.namespace->dobj.name : NULL,
14648  daclinfo->dacl.acl,
14649  daclinfo->dacl.acldefault,
14650  daclinfo->defaclrole,
14651  fout->remoteVersion,
14652  q))
14653  pg_fatal("could not parse default ACL list (%s)",
14654  daclinfo->dacl.acl);
14655 
14656  if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
14657  ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
14658  ARCHIVE_OPTS(.tag = tag->data,
14659  .namespace = daclinfo->dobj.namespace ?
14660  daclinfo->dobj.namespace->dobj.name : NULL,
14661  .owner = daclinfo->defaclrole,
14662  .description = "DEFAULT ACL",
14663  .section = SECTION_POST_DATA,
14664  .createStmt = q->data));
14665 
14666  destroyPQExpBuffer(tag);
14667  destroyPQExpBuffer(q);
14668 }
bool buildDefaultACLCommands(const char *type, const char *nspname, const char *acls, const char *acldefault, const char *owner, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:326
DumpableObject dobj
Definition: pg_dump.h:580
DumpableAcl dacl
Definition: pg_dump.h:581
const char * defaclrole
Definition: pg_dump.h:582
char defaclobjtype
Definition: pg_dump.h:583

References _dumpableAcl::acl, _dumpableAcl::acldefault, _dumpOptions::aclsSkip, appendPQExpBuffer(), ARCHIVE_OPTS, ArchiveEntry(), buildDefaultACLCommands(), _dumpableObject::catId, createPQExpBuffer(), _defaultACLInfo::dacl, PQExpBufferData::data, _dumpOptions::dataOnly, _defaultACLInfo::defaclobjtype, _defaultACLInfo::defaclrole, destroyPQExpBuffer(), _defaultACLInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_ACL, _dumpableObject::dumpId, _dumpableObject::name, pg_fatal, Archive::remoteVersion, SECTION_POST_DATA, and generate_unaccent_rules::type.

Referenced by dumpDumpableObject().

◆ dumpDomain()

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

Definition at line 11097 of file pg_dump.c.

11098 {
11099  DumpOptions *dopt = fout->dopt;
11101  PQExpBuffer delq = createPQExpBuffer();
11102  PQExpBuffer query = createPQExpBuffer();
11103  PGresult *res;
11104  int i;
11105  char *qtypname;
11106  char *qualtypname;
11107  char *typnotnull;
11108  char *typdefn;
11109  char *typdefault;
11110  Oid typcollation;
11111  bool typdefault_is_literal = false;
11112 
11113  if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN])
11114  {
11115  /* Set up query for domain-specific details */
11116  appendPQExpBufferStr(query,
11117  "PREPARE dumpDomain(pg_catalog.oid) AS\n");
11118 
11119  appendPQExpBufferStr(query, "SELECT t.typnotnull, "
11120  "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
11121  "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
11122  "t.typdefault, "
11123  "CASE WHEN t.typcollation <> u.typcollation "
11124  "THEN t.typcollation ELSE 0 END AS typcollation "
11125  "FROM pg_catalog.pg_type t "
11126  "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
11127  "WHERE t.