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 "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)
 
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 *binfo)
 
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)
 
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 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 SimpleOidList table_include_oids = {NULL, NULL}
 
static SimpleStringList table_exclude_patterns = {NULL, NULL}
 
static SimpleOidList table_exclude_oids = {NULL, NULL}
 
static SimpleStringList tabledata_exclude_patterns = {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 155 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 160 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 93 of file pg_dump.c.

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

Function Documentation

◆ addBoundaryDependencies()

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

Definition at line 17948 of file pg_dump.c.

17950 {
17951  DumpableObject *preDataBound = boundaryObjs + 0;
17952  DumpableObject *postDataBound = boundaryObjs + 1;
17953  int i;
17954 
17955  for (i = 0; i < numObjs; i++)
17956  {
17957  DumpableObject *dobj = dobjs[i];
17958 
17959  /*
17960  * The classification of object types here must match the SECTION_xxx
17961  * values assigned during subsequent ArchiveEntry calls!
17962  */
17963  switch (dobj->objType)
17964  {
17965  case DO_NAMESPACE:
17966  case DO_EXTENSION:
17967  case DO_TYPE:
17968  case DO_SHELL_TYPE:
17969  case DO_FUNC:
17970  case DO_AGG:
17971  case DO_OPERATOR:
17972  case DO_ACCESS_METHOD:
17973  case DO_OPCLASS:
17974  case DO_OPFAMILY:
17975  case DO_COLLATION:
17976  case DO_CONVERSION:
17977  case DO_TABLE:
17978  case DO_TABLE_ATTACH:
17979  case DO_ATTRDEF:
17980  case DO_PROCLANG:
17981  case DO_CAST:
17982  case DO_DUMMY_TYPE:
17983  case DO_TSPARSER:
17984  case DO_TSDICT:
17985  case DO_TSTEMPLATE:
17986  case DO_TSCONFIG:
17987  case DO_FDW:
17988  case DO_FOREIGN_SERVER:
17989  case DO_TRANSFORM:
17990  case DO_LARGE_OBJECT:
17991  /* Pre-data objects: must come before the pre-data boundary */
17992  addObjectDependency(preDataBound, dobj->dumpId);
17993  break;
17994  case DO_TABLE_DATA:
17995  case DO_SEQUENCE_SET:
17996  case DO_LARGE_OBJECT_DATA:
17997  /* Data objects: must come between the boundaries */
17998  addObjectDependency(dobj, preDataBound->dumpId);
17999  addObjectDependency(postDataBound, dobj->dumpId);
18000  break;
18001  case DO_INDEX:
18002  case DO_INDEX_ATTACH:
18003  case DO_STATSEXT:
18004  case DO_REFRESH_MATVIEW:
18005  case DO_TRIGGER:
18006  case DO_EVENT_TRIGGER:
18007  case DO_DEFAULT_ACL:
18008  case DO_POLICY:
18009  case DO_PUBLICATION:
18010  case DO_PUBLICATION_REL:
18012  case DO_SUBSCRIPTION:
18013  /* Post-data objects: must come after the post-data boundary */
18014  addObjectDependency(dobj, postDataBound->dumpId);
18015  break;
18016  case DO_RULE:
18017  /* Rules are post-data, but only if dumped separately */
18018  if (((RuleInfo *) dobj)->separate)
18019  addObjectDependency(dobj, postDataBound->dumpId);
18020  break;
18021  case DO_CONSTRAINT:
18022  case DO_FK_CONSTRAINT:
18023  /* Constraints are post-data, but only if dumped separately */
18024  if (((ConstraintInfo *) dobj)->separate)
18025  addObjectDependency(dobj, postDataBound->dumpId);
18026  break;
18027  case DO_PRE_DATA_BOUNDARY:
18028  /* nothing to do */
18029  break;
18030  case DO_POST_DATA_BOUNDARY:
18031  /* must come after the pre-data boundary */
18032  addObjectDependency(dobj, preDataBound->dumpId);
18033  break;
18034  }
18035  }
18036 }
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:732
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 7302 of file pg_dump.c.

7303 {
7304  SimplePtrListCell *cell;
7305 
7306  Assert(dobj->objType == DO_FK_CONSTRAINT);
7307 
7308  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7309  {
7310  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7311 
7312  addObjectDependency(dobj, attach->dobj.dumpId);
7313 
7314  if (attach->partitionIdx->partattaches.head != NULL)
7315  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7316  }
7317 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7302
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54
IndxInfo * partitionIdx
Definition: pg_dump.h:413
DumpableObject dobj
Definition: pg_dump.h:411
SimplePtrList partattaches
Definition: pg_dump.h:403

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

4730 {
4731  if (dobj->depends_on_ext)
4732  {
4733  char *nm;
4734  PGresult *res;
4735  PQExpBuffer query;
4736  int ntups;
4737  int i_extname;
4738  int i;
4739 
4740  /* dodge fmtId() non-reentrancy */
4741  nm = pg_strdup(objname);
4742 
4743  query = createPQExpBuffer();
4744  appendPQExpBuffer(query,
4745  "SELECT e.extname "
4746  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4747  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4748  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4749  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4750  catalog,
4751  dobj->catId.oid);
4752  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4753  ntups = PQntuples(res);
4754  i_extname = PQfnumber(res, "extname");
4755  for (i = 0; i < ntups; i++)
4756  {
4757  appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4758  keyword, nm,
4759  fmtId(PQgetvalue(res, i, i_extname)));
4760  }
4761 
4762  PQclear(res);
4763  destroyPQExpBuffer(query);
4764  pg_free(nm);
4765  }
4766 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3705
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3418
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 18264 of file pg_dump.c.

18266 {
18267  bool res;
18268 
18269  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18270  fout->std_strings);
18271  if (!res)
18272  pg_log_warning("could not parse %s array", "reloptions");
18273 }
#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 5013 of file pg_dump.c.

5018 {
5019  DumpableObject *extobj = NULL;
5020  int i;
5021 
5022  if (!dobj->ext_member)
5023  return;
5024 
5025  /*
5026  * Find the parent extension. We could avoid this search if we wanted to
5027  * add a link field to DumpableObject, but the space costs of that would
5028  * be considerable. We assume that member objects could only have a
5029  * direct dependency on their own extension, not any others.
5030  */
5031  for (i = 0; i < dobj->nDeps; i++)
5032  {
5033  extobj = findObjectByDumpId(dobj->dependencies[i]);
5034  if (extobj && extobj->objType == DO_EXTENSION)
5035  break;
5036  extobj = NULL;
5037  }
5038  if (extobj == NULL)
5039  pg_fatal("could not find parent extension for %s %s",
5040  objtype, objname);
5041 
5042  appendPQExpBufferStr(upgrade_buffer,
5043  "\n-- For binary upgrade, handle extension membership the hard way\n");
5044  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
5045  fmtId(extobj->name),
5046  objtype);
5047  if (objnamespace && *objnamespace)
5048  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
5049  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
5050 }
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:679
#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 4897 of file pg_dump.c.

4900 {
4901  PQExpBuffer upgrade_query = createPQExpBuffer();
4902  PGresult *upgrade_res;
4903  RelFileNumber relfilenumber;
4904  Oid toast_oid;
4905  RelFileNumber toast_relfilenumber;
4906  char relkind;
4907  Oid toast_index_oid;
4908  RelFileNumber toast_index_relfilenumber;
4909 
4910  /*
4911  * Preserve the OID and relfilenumber of the table, table's index, table's
4912  * toast table and toast table's index if any.
4913  *
4914  * One complexity is that the current table definition might not require
4915  * the creation of a TOAST table, but the old database might have a TOAST
4916  * table that was created earlier, before some wide columns were dropped.
4917  * By setting the TOAST oid we force creation of the TOAST heap and index
4918  * by the new backend, so we can copy the files during binary upgrade
4919  * without worrying about this case.
4920  */
4921  appendPQExpBuffer(upgrade_query,
4922  "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
4923  "FROM pg_catalog.pg_class c LEFT JOIN "
4924  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4925  "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
4926  "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
4927  "WHERE c.oid = '%u'::pg_catalog.oid;",
4928  pg_class_oid);
4929 
4930  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4931 
4932  relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
4933 
4934  relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
4935  PQfnumber(upgrade_res, "relfilenode")));
4936  toast_oid = atooid(PQgetvalue(upgrade_res, 0,
4937  PQfnumber(upgrade_res, "reltoastrelid")));
4938  toast_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
4939  PQfnumber(upgrade_res, "toast_relfilenode")));
4940  toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
4941  PQfnumber(upgrade_res, "indexrelid")));
4942  toast_index_relfilenumber = atooid(PQgetvalue(upgrade_res, 0,
4943  PQfnumber(upgrade_res, "toast_index_relfilenode")));
4944 
4945  appendPQExpBufferStr(upgrade_buffer,
4946  "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
4947 
4948  if (!is_index)
4949  {
4950  appendPQExpBuffer(upgrade_buffer,
4951  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4952  pg_class_oid);
4953 
4954  /*
4955  * Not every relation has storage. Also, in a pre-v12 database,
4956  * partitioned tables have a relfilenumber, which should not be
4957  * preserved when upgrading.
4958  */
4959  if (RelFileNumberIsValid(relfilenumber) && relkind != RELKIND_PARTITIONED_TABLE)
4960  appendPQExpBuffer(upgrade_buffer,
4961  "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
4962  relfilenumber);
4963 
4964  /*
4965  * In a pre-v12 database, partitioned tables might be marked as having
4966  * toast tables, but we should ignore them if so.
4967  */
4968  if (OidIsValid(toast_oid) &&
4969  relkind != RELKIND_PARTITIONED_TABLE)
4970  {
4971  appendPQExpBuffer(upgrade_buffer,
4972  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4973  toast_oid);
4974  appendPQExpBuffer(upgrade_buffer,
4975  "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
4976  toast_relfilenumber);
4977 
4978  /* every toast table has an index */
4979  appendPQExpBuffer(upgrade_buffer,
4980  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4981  toast_index_oid);
4982  appendPQExpBuffer(upgrade_buffer,
4983  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
4984  toast_index_relfilenumber);
4985  }
4986 
4987  PQclear(upgrade_res);
4988  }
4989  else
4990  {
4991  /* Preserve the OID and relfilenumber of the index */
4992  appendPQExpBuffer(upgrade_buffer,
4993  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4994  pg_class_oid);
4995  appendPQExpBuffer(upgrade_buffer,
4996  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
4997  relfilenumber);
4998  }
4999 
5000  appendPQExpBufferChar(upgrade_buffer, '\n');
5001 
5002  destroyPQExpBuffer(upgrade_query);
5003 }
#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 4885 of file pg_dump.c.

4888 {
4889  Oid pg_type_oid = tbinfo->reltype;
4890 
4891  if (OidIsValid(pg_type_oid))
4892  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4893  pg_type_oid, false, false);
4894 }
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:4800
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 4800 of file pg_dump.c.

4805 {
4806  PQExpBuffer upgrade_query = createPQExpBuffer();
4807  PGresult *res;
4808  Oid pg_type_array_oid;
4809  Oid pg_type_multirange_oid;
4810  Oid pg_type_multirange_array_oid;
4811 
4812  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4813  appendPQExpBuffer(upgrade_buffer,
4814  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4815  pg_type_oid);
4816 
4817  appendPQExpBuffer(upgrade_query,
4818  "SELECT typarray "
4819  "FROM pg_catalog.pg_type "
4820  "WHERE oid = '%u'::pg_catalog.oid;",
4821  pg_type_oid);
4822 
4823  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4824 
4825  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4826 
4827  PQclear(res);
4828 
4829  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4830  pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4831 
4832  if (OidIsValid(pg_type_array_oid))
4833  {
4834  appendPQExpBufferStr(upgrade_buffer,
4835  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4836  appendPQExpBuffer(upgrade_buffer,
4837  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4838  pg_type_array_oid);
4839  }
4840 
4841  /*
4842  * Pre-set the multirange type oid and its own array type oid.
4843  */
4844  if (include_multirange_type)
4845  {
4846  if (fout->remoteVersion >= 140000)
4847  {
4848  printfPQExpBuffer(upgrade_query,
4849  "SELECT t.oid, t.typarray "
4850  "FROM pg_catalog.pg_type t "
4851  "JOIN pg_catalog.pg_range r "
4852  "ON t.oid = r.rngmultitypid "
4853  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
4854  pg_type_oid);
4855 
4856  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4857 
4858  pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
4859  pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4860 
4861  PQclear(res);
4862  }
4863  else
4864  {
4865  pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4866  pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4867  }
4868 
4869  appendPQExpBufferStr(upgrade_buffer,
4870  "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
4871  appendPQExpBuffer(upgrade_buffer,
4872  "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4873  pg_type_multirange_oid);
4874  appendPQExpBufferStr(upgrade_buffer,
4875  "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
4876  appendPQExpBuffer(upgrade_buffer,
4877  "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4878  pg_type_multirange_array_oid);
4879  }
4880 
4881  destroyPQExpBuffer(upgrade_query);
4882 }
static Oid get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
Definition: pg_dump.c:4769
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 18063 of file pg_dump.c.

18064 {
18065  ArchiveHandle *AH = (ArchiveHandle *) fout;
18066  TocEntry *te;
18067 
18068  /* Scan all TOC entries in the archive */
18069  for (te = AH->toc->next; te != AH->toc; te = te->next)
18070  {
18071  DumpableObject *dobj;
18072  DumpId *dependencies;
18073  int nDeps;
18074  int allocDeps;
18075 
18076  /* No need to process entries that will not be dumped */
18077  if (te->reqs == 0)
18078  continue;
18079  /* Ignore entries that already have "special" dependencies */
18080  if (te->nDeps > 0)
18081  continue;
18082  /* Otherwise, look up the item's original DumpableObject, if any */
18083  dobj = findObjectByDumpId(te->dumpId);
18084  if (dobj == NULL)
18085  continue;
18086  /* No work if it has no dependencies */
18087  if (dobj->nDeps <= 0)
18088  continue;
18089  /* Set up work array */
18090  allocDeps = 64;
18091  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18092  nDeps = 0;
18093  /* Recursively find all dumpable dependencies */
18094  findDumpableDependencies(AH, dobj,
18095  &dependencies, &nDeps, &allocDeps);
18096  /* And save 'em ... */
18097  if (nDeps > 0)
18098  {
18099  dependencies = (DumpId *) pg_realloc(dependencies,
18100  nDeps * sizeof(DumpId));
18101  te->dependencies = dependencies;
18102  te->nDeps = nDeps;
18103  }
18104  else
18105  free(dependencies);
18106  }
18107 }
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:18111
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 2673 of file pg_dump.c.

2674 {
2675  PQExpBuffer query;
2676  PGresult *res;
2677  int ntups,
2678  i;
2679  int i_classid,
2680  i_objid,
2681  i_refobjid;
2682 
2683  /* No Mat Views before 9.3. */
2684  if (fout->remoteVersion < 90300)
2685  return;
2686 
2687  query = createPQExpBuffer();
2688 
2689  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2690  "( "
2691  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2692  "FROM pg_depend d1 "
2693  "JOIN pg_class c1 ON c1.oid = d1.objid "
2694  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2695  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2696  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2697  "AND d2.objid = r1.oid "
2698  "AND d2.refobjid <> d1.objid "
2699  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2700  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2701  CppAsString2(RELKIND_VIEW) ") "
2702  "WHERE d1.classid = 'pg_class'::regclass "
2703  "UNION "
2704  "SELECT w.objid, d3.refobjid, c3.relkind "
2705  "FROM w "
2706  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2707  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2708  "AND d3.objid = r3.oid "
2709  "AND d3.refobjid <> w.refobjid "
2710  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2711  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2712  CppAsString2(RELKIND_VIEW) ") "
2713  ") "
2714  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2715  "FROM w "
2716  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2717 
2718  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2719 
2720  ntups = PQntuples(res);
2721 
2722  i_classid = PQfnumber(res, "classid");
2723  i_objid = PQfnumber(res, "objid");
2724  i_refobjid = PQfnumber(res, "refobjid");
2725 
2726  for (i = 0; i < ntups; i++)
2727  {
2728  CatalogId objId;
2729  CatalogId refobjId;
2730  DumpableObject *dobj;
2731  DumpableObject *refdobj;
2732  TableInfo *tbinfo;
2733  TableInfo *reftbinfo;
2734 
2735  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2736  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2737  refobjId.tableoid = objId.tableoid;
2738  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2739 
2740  dobj = findObjectByCatalogId(objId);
2741  if (dobj == NULL)
2742  continue;
2743 
2744  Assert(dobj->objType == DO_TABLE);
2745  tbinfo = (TableInfo *) dobj;
2746  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2747  dobj = (DumpableObject *) tbinfo->dataObj;
2748  if (dobj == NULL)
2749  continue;
2750  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2751 
2752  refdobj = findObjectByCatalogId(refobjId);
2753  if (refdobj == NULL)
2754  continue;
2755 
2756  Assert(refdobj->objType == DO_TABLE);
2757  reftbinfo = (TableInfo *) refdobj;
2758  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2759  refdobj = (DumpableObject *) reftbinfo->dataObj;
2760  if (refdobj == NULL)
2761  continue;
2762  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2763 
2764  addObjectDependency(dobj, refdobj->dumpId);
2765 
2766  if (!reftbinfo->relispopulated)
2767  tbinfo->relispopulated = false;
2768  }
2769 
2770  PQclear(res);
2771 
2772  destroyPQExpBuffer(query);
2773 }
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:692
#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:358
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 1601 of file pg_dump.c.

1602 {
1603  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1604 
1605  if (ext == NULL)
1606  return false;
1607 
1608  dobj->ext_member = true;
1609 
1610  /* Record dependency so that getDependencies needn't deal with that */
1611  addObjectDependency(dobj, ext->dobj.dumpId);
1612 
1613  /*
1614  * In 9.6 and above, mark the member object to have any non-initial ACL,
1615  * policies, and security labels dumped.
1616  *
1617  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1618  * extract the information about the object. We don't provide support for
1619  * initial policies and security labels and it seems unlikely for those to
1620  * ever exist, but we may have to revisit this later.
1621  *
1622  * Prior to 9.6, we do not include any extension member components.
1623  *
1624  * In binary upgrades, we still dump all components of the members
1625  * individually, since the idea is to exactly reproduce the database
1626  * contents rather than replace the extension contents with something
1627  * different.
1628  */
1629  if (fout->dopt->binary_upgrade)
1630  dobj->dump = ext->dobj.dump;
1631  else
1632  {
1633  if (fout->remoteVersion < 90600)
1634  dobj->dump = DUMP_COMPONENT_NONE;
1635  else
1636  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1639  }
1640 
1641  return true;
1642 }
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:965
#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 9806 of file pg_dump.c.

9807 {
9808  PGresult *res;
9809  PQExpBuffer query;
9810  int i_description;
9811  int i_classoid;
9812  int i_objoid;
9813  int i_objsubid;
9814  int ntups;
9815  int i;
9816  DumpableObject *dobj;
9817 
9818  query = createPQExpBuffer();
9819 
9820  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9821  "FROM pg_catalog.pg_description "
9822  "ORDER BY classoid, objoid, objsubid");
9823 
9824  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9825 
9826  /* Construct lookup table containing OIDs in numeric form */
9827 
9828  i_description = PQfnumber(res, "description");
9829  i_classoid = PQfnumber(res, "classoid");
9830  i_objoid = PQfnumber(res, "objoid");
9831  i_objsubid = PQfnumber(res, "objsubid");
9832 
9833  ntups = PQntuples(res);
9834 
9835  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9836  ncomments = 0;
9837  dobj = NULL;
9838 
9839  for (i = 0; i < ntups; i++)
9840  {
9841  CatalogId objId;
9842  int subid;
9843 
9844  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
9845  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
9846  subid = atoi(PQgetvalue(res, i, i_objsubid));
9847 
9848  /* We needn't remember comments that don't match any dumpable object */
9849  if (dobj == NULL ||
9850  dobj->catId.tableoid != objId.tableoid ||
9851  dobj->catId.oid != objId.oid)
9852  dobj = findObjectByCatalogId(objId);
9853  if (dobj == NULL)
9854  continue;
9855 
9856  /*
9857  * Comments on columns of composite types are linked to the type's
9858  * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
9859  * in the type's own DumpableObject.
9860  */
9861  if (subid != 0 && dobj->objType == DO_TABLE &&
9862  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
9863  {
9864  TypeInfo *cTypeInfo;
9865 
9866  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
9867  if (cTypeInfo)
9868  cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT;
9869  }
9870  else
9871  dobj->components |= DUMP_COMPONENT_COMMENT;
9872 
9873  comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
9875  comments[ncomments].objoid = objId.oid;
9876  comments[ncomments].objsubid = subid;
9877  ncomments++;
9878  }
9879 
9880  PQclear(res);
9881  destroyPQExpBuffer(query);
9882 }
TypeInfo * findTypeByOid(Oid oid)
Definition: common.c:813
static int ncomments
Definition: pg_dump.c:145
static CommentItem * comments
Definition: pg_dump.c:144
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:98
Oid classoid
Definition: pg_dump.c:79
Oid objoid
Definition: pg_dump.c:80
int objsubid
Definition: pg_dump.c:81
const char * descr
Definition: pg_dump.c:78
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 9351 of file pg_dump.c.

9352 {
9353  PGresult *res;
9354  const char *query;
9355  int i;
9356 
9357  query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
9358 
9359  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
9360 
9362 
9364 
9365  for (i = 0; i < nrolenames; i++)
9366  {
9369  }
9370 
9371  PQclear(res);
9372 }
static RoleNameItem * rolenames
Definition: pg_dump.c:140
static int nrolenames
Definition: pg_dump.c:141
const char * rolename
Definition: pg_dump.c:73
Oid roleoid
Definition: pg_dump.c:72

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

14860 {
14861  PGresult *res;
14862  PQExpBuffer query;
14863  int i_label;
14864  int i_provider;
14865  int i_classoid;
14866  int i_objoid;
14867  int i_objsubid;
14868  int ntups;
14869  int i;
14870  DumpableObject *dobj;
14871 
14872  query = createPQExpBuffer();
14873 
14874  appendPQExpBufferStr(query,
14875  "SELECT label, provider, classoid, objoid, objsubid "
14876  "FROM pg_catalog.pg_seclabel "
14877  "ORDER BY classoid, objoid, objsubid");
14878 
14879  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14880 
14881  /* Construct lookup table containing OIDs in numeric form */
14882  i_label = PQfnumber(res, "label");
14883  i_provider = PQfnumber(res, "provider");
14884  i_classoid = PQfnumber(res, "classoid");
14885  i_objoid = PQfnumber(res, "objoid");
14886  i_objsubid = PQfnumber(res, "objsubid");
14887 
14888  ntups = PQntuples(res);
14889 
14890  seclabels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
14891  nseclabels = 0;
14892  dobj = NULL;
14893 
14894  for (i = 0; i < ntups; i++)
14895  {
14896  CatalogId objId;
14897  int subid;
14898 
14899  objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
14900  objId.oid = atooid(PQgetvalue(res, i, i_objoid));
14901  subid = atoi(PQgetvalue(res, i, i_objsubid));
14902 
14903  /* We needn't remember labels that don't match any dumpable object */
14904  if (dobj == NULL ||
14905  dobj->catId.tableoid != objId.tableoid ||
14906  dobj->catId.oid != objId.oid)
14907  dobj = findObjectByCatalogId(objId);
14908  if (dobj == NULL)
14909  continue;
14910 
14911  /*
14912  * Labels on columns of composite types are linked to the type's
14913  * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
14914  * in the type's own DumpableObject.
14915  */
14916  if (subid != 0 && dobj->objType == DO_TABLE &&
14917  ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
14918  {
14919  TypeInfo *cTypeInfo;
14920 
14921  cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
14922  if (cTypeInfo)
14923  cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL;
14924  }
14925  else
14926  dobj->components |= DUMP_COMPONENT_SECLABEL;
14927 
14931  seclabels[nseclabels].objoid = objId.oid;
14932  seclabels[nseclabels].objsubid = subid;
14933  nseclabels++;
14934  }
14935 
14936  PQclear(res);
14937  destroyPQExpBuffer(query);
14938 }
static int nseclabels
Definition: pg_dump.c:149
static SecLabelItem * seclabels
Definition: pg_dump.c:148
const char * provider
Definition: pg_dump.c:86
Oid classoid
Definition: pg_dump.c:88
int objsubid
Definition: pg_dump.c:90
const char * label
Definition: pg_dump.c:87
Oid objoid
Definition: pg_dump.c:89

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

12489 {
12490  char *name;
12491  char *paren;
12492  bool inquote;
12493 
12494  /* In all cases "-" means a null reference */
12495  if (strcmp(proc, "-") == 0)
12496  return NULL;
12497 
12498  name = pg_strdup(proc);
12499  /* find non-double-quoted left paren */
12500  inquote = false;
12501  for (paren = name; *paren; paren++)
12502  {
12503  if (*paren == '(' && !inquote)
12504  {
12505  *paren = '\0';
12506  break;
12507  }
12508  if (*paren == '"')
12509  inquote = !inquote;
12510  }
12511  return name;
12512 }
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 12559 of file pg_dump.c.

12560 {
12561  char *result;
12562  char query[128];
12563  PGresult *res;
12564 
12565  snprintf(query, sizeof(query),
12566  "SELECT '%u'::pg_catalog.regproc", funcOid);
12567  res = ExecuteSqlQueryForSingleRow(fout, query);
12568 
12569  result = pg_strdup(PQgetvalue(res, 0, 0));
12570 
12571  PQclear(res);
12572 
12573  return result;
12574 }
#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 17924 of file pg_dump.c.

17925 {
17926  DumpableObject *dobjs;
17927 
17928  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
17929 
17930  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
17931  dobjs[0].catId = nilCatalogId;
17932  AssignDumpId(dobjs + 0);
17933  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
17934 
17935  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
17936  dobjs[1].catId = nilCatalogId;
17937  AssignDumpId(dobjs + 1);
17938  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
17939 
17940  return dobjs;
17941 }
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:595
static const CatalogId nilCatalogId
Definition: pg_dump.c:133

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

15135 {
15136  PQExpBuffer result = createPQExpBuffer();
15137  int j;
15138 
15139  appendPQExpBufferStr(result, "SELECT");
15140 
15141  for (j = 0; j < tbinfo->numatts; j++)
15142  {
15143  if (j > 0)
15144  appendPQExpBufferChar(result, ',');
15145  appendPQExpBufferStr(result, "\n ");
15146 
15147  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15148 
15149  /*
15150  * Must add collation if not default for the type, because CREATE OR
15151  * REPLACE VIEW won't change it
15152  */
15153  if (OidIsValid(tbinfo->attcollation[j]))
15154  {
15155  CollInfo *coll;
15156 
15157  coll = findCollationByOid(tbinfo->attcollation[j]);
15158  if (coll)
15159  appendPQExpBuffer(result, " COLLATE %s",
15160  fmtQualifiedDumpable(coll));
15161  }
15162 
15163  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15164  }
15165 
15166  return result;
15167 }
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:868
int j
Definition: isn.c:74
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:160
int numatts
Definition: pg_dump.h:326
Oid * attcollation
Definition: pg_dump.h:340
char ** atttypnames
Definition: pg_dump.h:328
char ** attnames
Definition: pg_dump.h:327

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

15086 {
15087  PQExpBuffer query = createPQExpBuffer();
15088  PQExpBuffer result = createPQExpBuffer();
15089  PGresult *res;
15090  int len;
15091 
15092  /* Fetch the view definition */
15093  appendPQExpBuffer(query,
15094  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15095  tbinfo->dobj.catId.oid);
15096 
15097  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15098 
15099  if (PQntuples(res) != 1)
15100  {
15101  if (PQntuples(res) < 1)
15102  pg_fatal("query to obtain definition of view \"%s\" returned no data",
15103  tbinfo->dobj.name);
15104  else
15105  pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
15106  tbinfo->dobj.name);
15107  }
15108 
15109  len = PQgetlength(res, 0, 0);
15110 
15111  if (len == 0)
15112  pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
15113  tbinfo->dobj.name);
15114 
15115  /* Strip off the trailing semicolon so that other things may follow. */
15116  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15117  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15118 
15119  PQclear(res);
15120  destroyPQExpBuffer(query);
15121 
15122  return result;
15123 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3716
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 12581 of file pg_dump.c.

12582 {
12583  DumpOptions *dopt = fout->dopt;
12584  PQExpBuffer q;
12585  PQExpBuffer delq;
12586  char *qamname;
12587 
12588  /* Do nothing in data-only dump */
12589  if (dopt->dataOnly)
12590  return;
12591 
12592  q = createPQExpBuffer();
12593  delq = createPQExpBuffer();
12594 
12595  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12596 
12597  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12598 
12599  switch (aminfo->amtype)
12600  {
12601  case AMTYPE_INDEX:
12602  appendPQExpBufferStr(q, "TYPE INDEX ");
12603  break;
12604  case AMTYPE_TABLE:
12605  appendPQExpBufferStr(q, "TYPE TABLE ");
12606  break;
12607  default:
12608  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12609  aminfo->amtype, qamname);
12610  destroyPQExpBuffer(q);
12611  destroyPQExpBuffer(delq);
12612  free(qamname);
12613  return;
12614  }
12615 
12616  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12617 
12618  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12619  qamname);
12620 
12621  if (dopt->binary_upgrade)
12623  "ACCESS METHOD", qamname, NULL);
12624 
12625  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12626  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12627  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12628  .description = "ACCESS METHOD",
12629  .section = SECTION_PRE_DATA,
12630  .createStmt = q->data,
12631  .dropStmt = delq->data));
12632 
12633  /* Dump Access Method Comments */
12634  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12635  dumpComment(fout, "ACCESS METHOD", qamname,
12636  NULL, "",
12637  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12638 
12639  destroyPQExpBuffer(q);
12640  destroyPQExpBuffer(delq);
12641  free(qamname);
12642 }
@ 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:9615
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:5013
#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 14495 of file pg_dump.c.

14499 {
14500  DumpId aclDumpId = InvalidDumpId;
14501  DumpOptions *dopt = fout->dopt;
14502  const char *acls = dacl->acl;
14503  const char *acldefault = dacl->acldefault;
14504  char privtype = dacl->privtype;
14505  const char *initprivs = dacl->initprivs;
14506  const char *baseacls;
14507  PQExpBuffer sql;
14508 
14509  /* Do nothing if ACL dump is not enabled */
14510  if (dopt->aclsSkip)
14511  return InvalidDumpId;
14512 
14513  /* --data-only skips ACLs *except* large object ACLs */
14514  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14515  return InvalidDumpId;
14516 
14517  sql = createPQExpBuffer();
14518 
14519  /*
14520  * In binary upgrade mode, we don't run an extension's script but instead
14521  * dump out the objects independently and then recreate them. To preserve
14522  * any initial privileges which were set on extension objects, we need to
14523  * compute the set of GRANT and REVOKE commands necessary to get from the
14524  * default privileges of an object to its initial privileges as recorded
14525  * in pg_init_privs.
14526  *
14527  * At restore time, we apply these commands after having called
14528  * binary_upgrade_set_record_init_privs(true). That tells the backend to
14529  * copy the results into pg_init_privs. This is how we preserve the
14530  * contents of that catalog across binary upgrades.
14531  */
14532  if (dopt->binary_upgrade && privtype == 'e' &&
14533  initprivs && *initprivs != '\0')
14534  {
14535  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14536  if (!buildACLCommands(name, subname, nspname, type,
14537  initprivs, acldefault, owner,
14538  "", fout->remoteVersion, sql))
14539  pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
14540  initprivs, acldefault, name, type);
14541  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14542  }
14543 
14544  /*
14545  * Now figure the GRANT and REVOKE commands needed to get to the object's
14546  * actual current ACL, starting from the initprivs if given, else from the
14547  * object-type-specific default. Also, while buildACLCommands will assume
14548  * that a NULL/empty acls string means it needn't do anything, what that
14549  * actually represents is the object-type-specific default; so we need to
14550  * substitute the acldefault string to get the right results in that case.
14551  */
14552  if (initprivs && *initprivs != '\0')
14553  {
14554  baseacls = initprivs;
14555  if (acls == NULL || *acls == '\0')
14556  acls = acldefault;
14557  }
14558  else
14559  baseacls = acldefault;
14560 
14561  if (!buildACLCommands(name, subname, nspname, type,
14562  acls, baseacls, owner,
14563  "", fout->remoteVersion, sql))
14564  pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
14565  acls, baseacls, name, type);
14566 
14567  if (sql->len > 0)
14568  {
14570  DumpId aclDeps[2];
14571  int nDeps = 0;
14572 
14573  if (subname)
14574  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
14575  else
14576  appendPQExpBuffer(tag, "%s %s", type, name);
14577 
14578  aclDeps[nDeps++] = objDumpId;
14579  if (altDumpId != InvalidDumpId)
14580  aclDeps[nDeps++] = altDumpId;
14581 
14582  aclDumpId = createDumpId();
14583 
14584  ArchiveEntry(fout, nilCatalogId, aclDumpId,
14585  ARCHIVE_OPTS(.tag = tag->data,
14586  .namespace = nspname,
14587  .owner = owner,
14588  .description = "ACL",
14589  .section = SECTION_NONE,
14590  .createStmt = sql->data,
14591  .deps = aclDeps,
14592  .nDeps = nDeps));
14593 
14594  destroyPQExpBuffer(tag);
14595  }
14596 
14597  destroyPQExpBuffer(sql);
14598 
14599  return aclDumpId;
14600 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:780
DumpId createDumpId(void)
Definition: common.c:659
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 13462 of file pg_dump.c.

13463 {
13464  DumpOptions *dopt = fout->dopt;
13465  PQExpBuffer query;
13466  PQExpBuffer q;
13467  PQExpBuffer delq;
13468  PQExpBuffer details;
13469  char *aggsig; /* identity signature */
13470  char *aggfullsig = NULL; /* full signature */
13471  char *aggsig_tag;
13472  PGresult *res;
13473  int i_agginitval;
13474  int i_aggminitval;
13475  const char *aggtransfn;
13476  const char *aggfinalfn;
13477  const char *aggcombinefn;
13478  const char *aggserialfn;
13479  const char *aggdeserialfn;
13480  const char *aggmtransfn;
13481  const char *aggminvtransfn;
13482  const char *aggmfinalfn;
13483  bool aggfinalextra;
13484  bool aggmfinalextra;
13485  char aggfinalmodify;
13486  char aggmfinalmodify;
13487  const char *aggsortop;
13488  char *aggsortconvop;
13489  char aggkind;
13490  const char *aggtranstype;
13491  const char *aggtransspace;
13492  const char *aggmtranstype;
13493  const char *aggmtransspace;
13494  const char *agginitval;
13495  const char *aggminitval;
13496  const char *proparallel;
13497  char defaultfinalmodify;
13498 
13499  /* Do nothing in data-only dump */
13500  if (dopt->dataOnly)
13501  return;
13502 
13503  query = createPQExpBuffer();
13504  q = createPQExpBuffer();
13505  delq = createPQExpBuffer();
13506  details = createPQExpBuffer();
13507 
13508  if (!fout->is_prepared[PREPQUERY_DUMPAGG])
13509  {
13510  /* Set up query for aggregate-specific details */
13511  appendPQExpBufferStr(query,
13512  "PREPARE dumpAgg(pg_catalog.oid) AS\n");
13513 
13514  appendPQExpBufferStr(query,
13515  "SELECT "
13516  "aggtransfn,\n"
13517  "aggfinalfn,\n"
13518  "aggtranstype::pg_catalog.regtype,\n"
13519  "agginitval,\n"
13520  "aggsortop,\n"
13521  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13522  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
13523 
13524  if (fout->remoteVersion >= 90400)
13525  appendPQExpBufferStr(query,
13526  "aggkind,\n"
13527  "aggmtransfn,\n"
13528  "aggminvtransfn,\n"
13529  "aggmfinalfn,\n"
13530  "aggmtranstype::pg_catalog.regtype,\n"
13531  "aggfinalextra,\n"
13532  "aggmfinalextra,\n"
13533  "aggtransspace,\n"
13534  "aggmtransspace,\n"
13535  "aggminitval,\n");
13536  else
13537  appendPQExpBufferStr(query,
13538  "'n' AS aggkind,\n"
13539  "'-' AS aggmtransfn,\n"
13540  "'-' AS aggminvtransfn,\n"
13541  "'-' AS aggmfinalfn,\n"
13542  "0 AS aggmtranstype,\n"
13543  "false AS aggfinalextra,\n"
13544  "false AS aggmfinalextra,\n"
13545  "0 AS aggtransspace,\n"
13546  "0 AS aggmtransspace,\n"
13547  "NULL AS aggminitval,\n");
13548 
13549  if (fout->remoteVersion >= 90600)
13550  appendPQExpBufferStr(query,
13551  "aggcombinefn,\n"
13552  "aggserialfn,\n"
13553  "aggdeserialfn,\n"
13554  "proparallel,\n");
13555  else
13556  appendPQExpBufferStr(query,
13557  "'-' AS aggcombinefn,\n"
13558  "'-' AS aggserialfn,\n"
13559  "'-' AS aggdeserialfn,\n"
13560  "'u' AS proparallel,\n");
13561 
13562  if (fout->remoteVersion >= 110000)
13563  appendPQExpBufferStr(query,
13564  "aggfinalmodify,\n"
13565  "aggmfinalmodify\n");
13566  else
13567  appendPQExpBufferStr(query,
13568  "'0' AS aggfinalmodify,\n"
13569  "'0' AS aggmfinalmodify\n");
13570 
13571  appendPQExpBufferStr(query,
13572  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13573  "WHERE a.aggfnoid = p.oid "
13574  "AND p.oid = $1");
13575 
13576  ExecuteSqlStatement(fout, query->data);
13577 
13578  fout->is_prepared[PREPQUERY_DUMPAGG] = true;
13579  }
13580 
13581  printfPQExpBuffer(query,
13582  "EXECUTE dumpAgg('%u')",
13583  agginfo->aggfn.dobj.catId.oid);
13584 
13585  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13586 
13587  i_agginitval = PQfnumber(res, "agginitval");
13588  i_aggminitval = PQfnumber(res, "aggminitval");
13589 
13590  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
13591  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
13592  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
13593  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
13594  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
13595  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
13596  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
13597  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
13598  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
13599  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
13600  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
13601  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
13602  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
13603  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
13604  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
13605  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
13606  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
13607  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
13608  agginitval = PQgetvalue(res, 0, i_agginitval);
13609  aggminitval = PQgetvalue(res, 0, i_aggminitval);
13610  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13611 
13612  {
13613  char *funcargs;
13614  char *funciargs;
13615 
13616  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13617  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13618  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13619  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13620  }
13621 
13622  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
13623 
13624  /* identify default modify flag for aggkind (must match DefineAggregate) */
13625  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
13626  /* replace omitted flags for old versions */
13627  if (aggfinalmodify == '0')
13628  aggfinalmodify = defaultfinalmodify;
13629  if (aggmfinalmodify == '0')
13630  aggmfinalmodify = defaultfinalmodify;
13631 
13632  /* regproc and regtype output is already sufficiently quoted */
13633  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
13634  aggtransfn, aggtranstype);
13635 
13636  if (strcmp(aggtransspace, "0") != 0)
13637  {
13638  appendPQExpBuffer(details, ",\n SSPACE = %s",
13639  aggtransspace);
13640  }
13641 
13642  if (!PQgetisnull(res, 0, i_agginitval))
13643  {
13644  appendPQExpBufferStr(details, ",\n INITCOND = ");
13645  appendStringLiteralAH(details, agginitval, fout);
13646  }
13647 
13648  if (strcmp(aggfinalfn, "-") != 0)
13649  {
13650  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
13651  aggfinalfn);
13652  if (aggfinalextra)
13653  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
13654  if (aggfinalmodify != defaultfinalmodify)
13655  {
13656  switch (aggfinalmodify)
13657  {
13658  case AGGMODIFY_READ_ONLY:
13659  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
13660  break;
13661  case AGGMODIFY_SHAREABLE:
13662  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
13663  break;
13664  case AGGMODIFY_READ_WRITE:
13665  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
13666  break;
13667  default:
13668  pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
13669  agginfo->aggfn.dobj.name);
13670  break;
13671  }
13672  }
13673  }
13674 
13675  if (strcmp(aggcombinefn, "-") != 0)
13676  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
13677 
13678  if (strcmp(aggserialfn, "-") != 0)
13679  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
13680 
13681  if (strcmp(aggdeserialfn, "-") != 0)
13682  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
13683 
13684  if (strcmp(aggmtransfn, "-") != 0)
13685  {
13686  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
13687  aggmtransfn,
13688  aggminvtransfn,
13689  aggmtranstype);
13690  }
13691 
13692  if (strcmp(aggmtransspace, "0") != 0)
13693  {
13694  appendPQExpBuffer(details, ",\n MSSPACE = %s",
13695  aggmtransspace);
13696  }
13697 
13698  if (!PQgetisnull(res, 0, i_aggminitval))
13699  {
13700  appendPQExpBufferStr(details, ",\n MINITCOND = ");
13701  appendStringLiteralAH(details, aggminitval, fout);
13702  }
13703 
13704  if (strcmp(aggmfinalfn, "-") != 0)
13705  {
13706  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
13707  aggmfinalfn);
13708  if (aggmfinalextra)
13709  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
13710  if (aggmfinalmodify != defaultfinalmodify)
13711  {
13712  switch (aggmfinalmodify)
13713  {
13714  case AGGMODIFY_READ_ONLY:
13715  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
13716  break;
13717  case AGGMODIFY_SHAREABLE:
13718  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
13719  break;
13720  case AGGMODIFY_READ_WRITE:
13721  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
13722  break;
13723  default:
13724  pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
13725  agginfo->aggfn.dobj.name);
13726  break;
13727  }
13728  }
13729  }
13730 
13731  aggsortconvop = getFormattedOperatorName(aggsortop);
13732  if (aggsortconvop)
13733  {
13734  appendPQExpBuffer(details, ",\n SORTOP = %s",
13735  aggsortconvop);
13736  free(aggsortconvop);
13737  }
13738 
13739  if (aggkind == AGGKIND_HYPOTHETICAL)
13740  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
13741 
13742  if (proparallel[0] != PROPARALLEL_UNSAFE)
13743  {
13744  if (proparallel[0] == PROPARALLEL_SAFE)
13745  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
13746  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13747  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
13748  else if (proparallel[0] != PROPARALLEL_UNSAFE)
13749  pg_fatal("unrecognized proparallel value for function \"%s\"",
13750  agginfo->aggfn.dobj.name);
13751  }
13752 
13753  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
13754  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13755  aggsig);
13756 
13757  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
13758  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13759  aggfullsig ? aggfullsig : aggsig, details->data);
13760 
13761  if (dopt->binary_upgrade)
13762  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
13763  "AGGREGATE", aggsig,
13764  agginfo->aggfn.dobj.namespace->dobj.name);
13765 
13766  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
13767  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
13768  agginfo->aggfn.dobj.dumpId,
13769  ARCHIVE_OPTS(.tag = aggsig_tag,
13770  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
13771  .owner = agginfo->aggfn.rolname,
13772  .description = "AGGREGATE",
13773  .section = SECTION_PRE_DATA,
13774  .createStmt = q->data,
13775  .dropStmt = delq->data));
13776 
13777  /* Dump Aggregate Comments */
13778  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
13779  dumpComment(fout, "AGGREGATE", aggsig,
13780  agginfo->aggfn.dobj.namespace->dobj.name,
13781  agginfo->aggfn.rolname,
13782  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13783 
13784  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
13785  dumpSecLabel(fout, "AGGREGATE", aggsig,
13786  agginfo->aggfn.dobj.namespace->dobj.name,
13787  agginfo->aggfn.rolname,
13788  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13789 
13790  /*
13791  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
13792  * command look like a function's GRANT; in particular this affects the
13793  * syntax for zero-argument aggregates and ordered-set aggregates.
13794  */
13795  free(aggsig);
13796 
13797  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
13798 
13799  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
13800  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
13801  "FUNCTION", aggsig, NULL,
13802  agginfo->aggfn.dobj.namespace->dobj.name,
13803  agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
13804 
13805  free(aggsig);
13806  free(aggfullsig);
13807  free(aggsig_tag);
13808 
13809  PQclear(res);
13810 
13811  destroyPQExpBuffer(query);
13812  destroyPQExpBuffer(q);
13813  destroyPQExpBuffer(delq);
13814  destroyPQExpBuffer(details);
13815 }
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3730
#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:12529
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11591
static char * format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:13430
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:14495
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:11568
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:14621
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 16041 of file pg_dump.c.

16042 {
16043  DumpOptions *dopt = fout->dopt;
16044  TableInfo *tbinfo = adinfo->adtable;
16045  int adnum = adinfo->adnum;
16046  PQExpBuffer q;
16047  PQExpBuffer delq;
16048  char *qualrelname;
16049  char *tag;
16050  char *foreign;
16051 
16052  /* Do nothing in data-only dump */
16053  if (dopt->dataOnly)
16054  return;
16055 
16056  /* Skip if not "separate"; it was dumped in the table's definition */
16057  if (!adinfo->separate)
16058  return;
16059 
16060  q = createPQExpBuffer();
16061  delq = createPQExpBuffer();
16062 
16063  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16064 
16065  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16066 
16068  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16069  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16070  adinfo->adef_expr);
16071 
16072  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16073  foreign, qualrelname,
16074  fmtId(tbinfo->attnames[adnum - 1]));
16075 
16076  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16077 
16078  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16079  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16080  ARCHIVE_OPTS(.tag = tag,
16081  .namespace = tbinfo->dobj.namespace->dobj.name,
16082  .owner = tbinfo->rolname,
16083  .description = "DEFAULT",
16084  .section = SECTION_PRE_DATA,
16085  .createStmt = q->data,
16086  .dropStmt = delq->data));
16087 
16088  free(tag);
16089  destroyPQExpBuffer(q);
16090  destroyPQExpBuffer(delq);
16091  free(qualrelname);
16092 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
DumpableObject dobj
Definition: pg_dump.h:372
char * adef_expr
Definition: pg_dump.h:375
TableInfo * adtable
Definition: pg_dump.h:373
bool separate
Definition: pg_dump.h:376
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 10673 of file pg_dump.c.

10674 {
10675  DumpOptions *dopt = fout->dopt;
10677  PQExpBuffer delq = createPQExpBuffer();
10678  PQExpBuffer query = createPQExpBuffer();
10679  PGresult *res;
10680  char *qtypname;
10681  char *qualtypname;
10682  char *typlen;
10683  char *typinput;
10684  char *typoutput;
10685  char *typreceive;
10686  char *typsend;
10687  char *typmodin;
10688  char *typmodout;
10689  char *typanalyze;
10690  char *typsubscript;
10691  Oid typreceiveoid;
10692  Oid typsendoid;
10693  Oid typmodinoid;
10694  Oid typmodoutoid;
10695  Oid typanalyzeoid;
10696  Oid typsubscriptoid;
10697  char *typcategory;
10698  char *typispreferred;
10699  char *typdelim;
10700  char *typbyval;
10701  char *typalign;
10702  char *typstorage;
10703  char *typcollatable;
10704  char *typdefault;
10705  bool typdefault_is_literal = false;
10706 
10707  if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE])
10708  {
10709  /* Set up query for type-specific details */
10710  appendPQExpBufferStr(query,
10711  "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
10712  "SELECT typlen, "
10713  "typinput, typoutput, typreceive, typsend, "
10714  "typreceive::pg_catalog.oid AS typreceiveoid, "
10715  "typsend::pg_catalog.oid AS typsendoid, "
10716  "typanalyze, "
10717  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10718  "typdelim, typbyval, typalign, typstorage, "
10719  "typmodin, typmodout, "
10720  "typmodin::pg_catalog.oid AS typmodinoid, "
10721  "typmodout::pg_catalog.oid AS typmodoutoid, "
10722  "typcategory, typispreferred, "
10723  "(typcollation <> 0) AS typcollatable, "
10724  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
10725 
10726  if (fout->remoteVersion >= 140000)
10727  appendPQExpBufferStr(query,
10728  "typsubscript, "
10729  "typsubscript::pg_catalog.oid AS typsubscriptoid ");
10730  else
10731  appendPQExpBufferStr(query,
10732  "'-' AS typsubscript, 0 AS typsubscriptoid ");
10733 
10734  appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
10735  "WHERE oid = $1");
10736 
10737  ExecuteSqlStatement(fout, query->data);
10738 
10739  fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
10740  }
10741 
10742  printfPQExpBuffer(query,
10743  "EXECUTE dumpBaseType('%u')",
10744  tyinfo->dobj.catId.oid);
10745 
10746  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10747 
10748  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10749  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10750  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10751  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10752  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10753  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10754  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10755  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10756  typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
10757  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10758  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10759  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10760  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10761  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10762  typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
10763  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10764  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10765  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10766  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10767  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10768  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10769  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10770  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10771  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10772  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10773  {
10774  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10775  typdefault_is_literal = true; /* it needs quotes */
10776  }
10777  else
10778  typdefault = NULL;
10779 
10780  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10781  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10782 
10783  /*
10784  * The reason we include CASCADE is that the circular dependency between
10785  * the type and its I/O functions makes it impossible to drop the type any
10786  * other way.
10787  */
10788  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
10789 
10790  /*
10791  * We might already have a shell type, but setting pg_type_oid is
10792  * harmless, and in any case we'd better set the array type OID.
10793  */
10794  if (dopt->binary_upgrade)
10796  tyinfo->dobj.catId.oid,
10797  false, false);
10798 
10800  "CREATE TYPE %s (\n"
10801  " INTERNALLENGTH = %s",
10802  qualtypname,
10803  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10804 
10805  /* regproc result is sufficiently quoted already */
10806  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
10807  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
10808  if (OidIsValid(typreceiveoid))
10809  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
10810  if (OidIsValid(typsendoid))
10811  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10812  if (OidIsValid(typmodinoid))
10813  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10814  if (OidIsValid(typmodoutoid))
10815  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10816  if (OidIsValid(typanalyzeoid))
10817  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10818 
10819  if (strcmp(typcollatable, "t") == 0)
10820  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10821 
10822  if (typdefault != NULL)
10823  {
10824  appendPQExpBufferStr(q, ",\n DEFAULT = ");
10825  if (typdefault_is_literal)
10826  appendStringLiteralAH(q, typdefault, fout);
10827  else
10828  appendPQExpBufferStr(q, typdefault);
10829  }
10830 
10831  if (OidIsValid(typsubscriptoid))
10832  appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
10833 
10834  if (OidIsValid(tyinfo->typelem))
10835  appendPQExpBuffer(q, ",\n ELEMENT = %s",
10836  getFormattedTypeName(fout, tyinfo->typelem,
10837  zeroIsError));
10838 
10839  if (strcmp(typcategory, "U") != 0)
10840  {
10841  appendPQExpBufferStr(q, ",\n CATEGORY = ");
10842  appendStringLiteralAH(q, typcategory, fout);
10843  }
10844 
10845  if (strcmp(typispreferred, "t") == 0)
10846  appendPQExpBufferStr(q, ",\n PREFERRED = true");
10847 
10848  if (typdelim && strcmp(typdelim, ",") != 0)
10849  {
10850  appendPQExpBufferStr(q, ",\n DELIMITER = ");
10851  appendStringLiteralAH(q, typdelim, fout);
10852  }
10853 
10854  if (*typalign == TYPALIGN_CHAR)
10855  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
10856  else if (*typalign == TYPALIGN_SHORT)
10857  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
10858  else if (*typalign == TYPALIGN_INT)
10859  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
10860  else if (*typalign == TYPALIGN_DOUBLE)
10861  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
10862 
10863  if (*typstorage == TYPSTORAGE_PLAIN)
10864  appendPQExpBufferStr(q, ",\n STORAGE = plain");
10865  else if (*typstorage == TYPSTORAGE_EXTERNAL)
10866  appendPQExpBufferStr(q, ",\n STORAGE = external");
10867  else if (*typstorage == TYPSTORAGE_EXTENDED)
10868  appendPQExpBufferStr(q, ",\n STORAGE = extended");
10869  else if (*typstorage == TYPSTORAGE_MAIN)
10870  appendPQExpBufferStr(q, ",\n STORAGE = main");
10871 
10872  if (strcmp(typbyval, "t") == 0)
10873  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
10874 
10875  appendPQExpBufferStr(q, "\n);\n");
10876 
10877  if (dopt->binary_upgrade)
10879  "TYPE", qtypname,
10880  tyinfo->dobj.namespace->dobj.name);
10881 
10882  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10883  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10884  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
10885  .namespace = tyinfo->dobj.namespace->dobj.name,
10886  .owner = tyinfo->rolname,
10887  .description = "TYPE",
10888  .section = SECTION_PRE_DATA,
10889  .createStmt = q->data,
10890  .dropStmt = delq->data));
10891 
10892  /* Dump Type Comments and Security Labels */
10893  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10894  dumpComment(fout, "TYPE", qtypname,
10895  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10896  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10897 
10898  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10899  dumpSecLabel(fout, "TYPE", qtypname,
10900  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10901  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10902 
10903  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10904  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
10905  qtypname, NULL,
10906  tyinfo->dobj.namespace->dobj.name,
10907  tyinfo->rolname, &tyinfo->dacl);
10908 
10909  PQclear(res);
10910  destroyPQExpBuffer(q);
10911  destroyPQExpBuffer(delq);
10912  destroyPQExpBuffer(query);
10913  free(qtypname);
10914  free(qualtypname);
10915 }
@ PREPQUERY_DUMPBASETYPE
Definition: pg_backup.h:66
static const char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18167
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 12035 of file pg_dump.c.

12036 {
12037  DumpOptions *dopt = fout->dopt;
12038  PQExpBuffer defqry;
12039  PQExpBuffer delqry;
12040  PQExpBuffer labelq;
12041  PQExpBuffer castargs;
12042  FuncInfo *funcInfo = NULL;
12043  const char *sourceType;
12044  const char *targetType;
12045 
12046  /* Do nothing in data-only dump */
12047  if (dopt->dataOnly)
12048  return;
12049 
12050  /* Cannot dump if we don't have the cast function's info */
12051  if (OidIsValid(cast->castfunc))
12052  {
12053  funcInfo = findFuncByOid(cast->castfunc);
12054  if (funcInfo == NULL)
12055  pg_fatal("could not find function definition for function with OID %u",
12056  cast->castfunc);
12057  }
12058 
12059  defqry = createPQExpBuffer();
12060  delqry = createPQExpBuffer();
12061  labelq = createPQExpBuffer();
12062  castargs = createPQExpBuffer();
12063 
12064  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12065  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12066  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12067  sourceType, targetType);
12068 
12069  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12070  sourceType, targetType);
12071 
12072  switch (cast->castmethod)
12073  {
12074  case COERCION_METHOD_BINARY:
12075  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12076  break;
12077  case COERCION_METHOD_INOUT:
12078  appendPQExpBufferStr(defqry, "WITH INOUT");
12079  break;
12080  case COERCION_METHOD_FUNCTION:
12081  if (funcInfo)
12082  {
12083  char *fsig = format_function_signature(fout, funcInfo, true);
12084 
12085  /*
12086  * Always qualify the function name (format_function_signature
12087  * won't qualify it).
12088  */
12089  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12090  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12091  free(fsig);
12092  }
12093  else
12094  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12095  break;
12096  default:
12097  pg_log_warning("bogus value in pg_cast.castmethod field");
12098  }
12099 
12100  if (cast->castcontext == 'a')
12101  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12102  else if (cast->castcontext == 'i')
12103  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12104  appendPQExpBufferStr(defqry, ";\n");
12105 
12106  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12107  sourceType, targetType);
12108 
12109  appendPQExpBuffer(castargs, "(%s AS %s)",
12110  sourceType, targetType);
12111 
12112  if (dopt->binary_upgrade)
12113  binary_upgrade_extension_member(defqry, &cast->dobj,
12114  "CAST", castargs->data, NULL);
12115 
12116  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12117  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12118  ARCHIVE_OPTS(.tag = labelq->data,
12119  .description = "CAST",
12120  .section = SECTION_PRE_DATA,
12121  .createStmt = defqry->data,
12122  .dropStmt = delqry->data));
12123 
12124  /* Dump Cast Comments */
12125  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12126  dumpComment(fout, "CAST", castargs->data,
12127  NULL, "",
12128  cast->dobj.catId, 0, cast->dobj.dumpId);
12129 
12130  destroyPQExpBuffer(defqry);
12131  destroyPQExpBuffer(delqry);
12132  destroyPQExpBuffer(labelq);
12133  destroyPQExpBuffer(castargs);
12134 }
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:832
char castmethod
Definition: pg_dump.h:505
Oid casttarget
Definition: pg_dump.h:502
char castcontext
Definition: pg_dump.h:504
DumpableObject dobj
Definition: pg_dump.h:500
Oid castsource
Definition: pg_dump.h:501
Oid castfunc
Definition: pg_dump.h:503
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 13149 of file pg_dump.c.

13150 {
13151  DumpOptions *dopt = fout->dopt;
13152  PQExpBuffer query;
13153  PQExpBuffer q;
13154  PQExpBuffer delq;
13155  char *qcollname;
13156  PGresult *res;
13157  int i_collprovider;
13158  int i_collisdeterministic;
13159  int i_collcollate;
13160  int i_collctype;
13161  int i_colliculocale;
13162  const char *collprovider;
13163  const char *collcollate;
13164  const char *collctype;
13165  const char *colliculocale;
13166 
13167  /* Do nothing in data-only dump */
13168  if (dopt->dataOnly)
13169  return;
13170 
13171  query = createPQExpBuffer();
13172  q = createPQExpBuffer();
13173  delq = createPQExpBuffer();
13174 
13175  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13176 
13177  /* Get collation-specific details */
13178  appendPQExpBufferStr(query, "SELECT ");
13179 
13180  if (fout->remoteVersion >= 100000)
13181  appendPQExpBufferStr(query,
13182  "collprovider, "
13183  "collversion, ");
13184  else
13185  appendPQExpBufferStr(query,
13186  "'c' AS collprovider, "
13187  "NULL AS collversion, ");
13188 
13189  if (fout->remoteVersion >= 120000)
13190  appendPQExpBufferStr(query,
13191  "collisdeterministic, ");
13192  else
13193  appendPQExpBufferStr(query,
13194  "true AS collisdeterministic, ");
13195 
13196  if (fout->remoteVersion >= 150000)
13197  appendPQExpBufferStr(query,
13198  "colliculocale, ");
13199  else
13200  appendPQExpBufferStr(query,
13201  "NULL AS colliculocale, ");
13202 
13203  appendPQExpBuffer(query,
13204  "collcollate, "
13205  "collctype "
13206  "FROM pg_catalog.pg_collation c "
13207  "WHERE c.oid = '%u'::pg_catalog.oid",
13208  collinfo->dobj.catId.oid);
13209 
13210  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13211 
13212  i_collprovider = PQfnumber(res, "collprovider");
13213  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13214  i_collcollate = PQfnumber(res, "collcollate");
13215  i_collctype = PQfnumber(res, "collctype");
13216  i_colliculocale = PQfnumber(res, "colliculocale");
13217 
13218  collprovider = PQgetvalue(res, 0, i_collprovider);
13219 
13220  if (!PQgetisnull(res, 0, i_collcollate))
13221  collcollate = PQgetvalue(res, 0, i_collcollate);
13222  else
13223  collcollate = NULL;
13224 
13225  if (!PQgetisnull(res, 0, i_collctype))
13226  collctype = PQgetvalue(res, 0, i_collctype);
13227  else
13228  collctype = NULL;
13229 
13230  if (!PQgetisnull(res, 0, i_colliculocale))
13231  colliculocale = PQgetvalue(res, 0, i_colliculocale);
13232  else
13233  colliculocale = NULL;
13234 
13235  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13236  fmtQualifiedDumpable(collinfo));
13237 
13238  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13239  fmtQualifiedDumpable(collinfo));
13240 
13241  appendPQExpBufferStr(q, "provider = ");
13242  if (collprovider[0] == 'c')
13243  appendPQExpBufferStr(q, "libc");
13244  else if (collprovider[0] == 'i')
13245  appendPQExpBufferStr(q, "icu");
13246  else if (collprovider[0] == 'd')
13247  /* to allow dumping pg_catalog; not accepted on input */
13248  appendPQExpBufferStr(q, "default");
13249  else
13250  pg_fatal("unrecognized collation provider: %s",
13251  collprovider);
13252 
13253  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13254  appendPQExpBufferStr(q, ", deterministic = false");
13255 
13256  if (colliculocale != NULL)
13257  {
13258  appendPQExpBufferStr(q, ", locale = ");
13259  appendStringLiteralAH(q, colliculocale, fout);
13260  }
13261  else
13262  {
13263  Assert(collcollate != NULL);
13264  Assert(collctype != NULL);
13265 
13266  if (strcmp(collcollate, collctype) == 0)
13267  {
13268  appendPQExpBufferStr(q, ", locale = ");
13269  appendStringLiteralAH(q, collcollate, fout);
13270  }
13271  else
13272  {
13273  appendPQExpBufferStr(q, ", lc_collate = ");
13274  appendStringLiteralAH(q, collcollate, fout);
13275  appendPQExpBufferStr(q, ", lc_ctype = ");
13276  appendStringLiteralAH(q, collctype, fout);
13277  }
13278  }
13279 
13280  /*
13281  * For binary upgrade, carry over the collation version. For normal
13282  * dump/restore, omit the version, so that it is computed upon restore.
13283  */
13284  if (dopt->binary_upgrade)
13285  {
13286  int i_collversion;
13287 
13288  i_collversion = PQfnumber(res, "collversion");
13289  if (!PQgetisnull(res, 0, i_collversion))
13290  {
13291  appendPQExpBufferStr(q, ", version = ");
13293  PQgetvalue(res, 0, i_collversion),
13294  fout);
13295  }
13296  }
13297 
13298  appendPQExpBufferStr(q, ");\n");
13299 
13300  if (dopt->binary_upgrade)
13301  binary_upgrade_extension_member(q, &collinfo->dobj,
13302  "COLLATION", qcollname,
13303  collinfo->dobj.namespace->dobj.name);
13304 
13305  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13306  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13307  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13308  .namespace = collinfo->dobj.namespace->dobj.name,
13309  .owner = collinfo->rolname,
13310  .description = "COLLATION",
13311  .section = SECTION_PRE_DATA,
13312  .createStmt = q->data,
13313  .dropStmt = delq->data));
13314 
13315  /* Dump Collation Comments */
13316  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13317  dumpComment(fout, "COLLATION", qcollname,
13318  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13319  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13320 
13321  PQclear(res);
13322 
13323  destroyPQExpBuffer(query);
13324  destroyPQExpBuffer(q);
13325  destroyPQExpBuffer(delq);
13326  free(qcollname);
13327 }
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 9615 of file pg_dump.c.

9619 {
9620  dumpCommentExtended(fout, type, name, namespace, owner,
9621  catalogId, subid, dumpId, NULL);
9622 }
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:9515

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

9520 {
9521  DumpOptions *dopt = fout->dopt;
9523  int ncomments;
9524 
9525  /* do nothing, if --no-comments is supplied */
9526  if (dopt->no_comments)
9527  return;
9528 
9529  /* Comments are schema not data ... except LO comments are data */
9530  if (strcmp(type, "LARGE OBJECT") != 0)
9531  {
9532  if (dopt->dataOnly)
9533  return;
9534  }
9535  else
9536  {
9537  /* We do dump LO comments in binary-upgrade mode */
9538  if (dopt->schemaOnly && !dopt->binary_upgrade)
9539  return;
9540  }
9541 
9542  /* Search for comments associated with catalogId, using table */
9543  ncomments = findComments(catalogId.tableoid, catalogId.oid,
9544  &comments);
9545 
9546  /* Is there one matching the subid? */
9547  while (ncomments > 0)
9548  {
9549  if (comments->objsubid == subid)
9550  break;
9551  comments++;
9552  ncomments--;
9553  }
9554 
9555  if (initdb_comment != NULL)
9556  {
9557  static CommentItem empty_comment = {.descr = ""};
9558 
9559  /*
9560  * initdb creates this object with a comment. Skip dumping the
9561  * initdb-provided comment, which would complicate matters for
9562  * non-superuser use of pg_dump. When the DBA has removed initdb's
9563  * comment, replicate that.
9564  */
9565  if (ncomments == 0)
9566  {
9567  comments = &empty_comment;
9568  ncomments = 1;
9569  }
9570  else if (strcmp(comments->descr, initdb_comment) == 0)
9571  ncomments = 0;
9572  }
9573 
9574  /* If a comment exists, build COMMENT ON statement */
9575  if (ncomments > 0)
9576  {
9577  PQExpBuffer query = createPQExpBuffer();
9579 
9580  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9581  if (namespace && *namespace)
9582  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9583  appendPQExpBuffer(query, "%s IS ", name);
9584  appendStringLiteralAH(query, comments->descr, fout);
9585  appendPQExpBufferStr(query, ";\n");
9586 
9587  appendPQExpBuffer(tag, "%s %s", type, name);
9588 
9589  /*
9590  * We mark comments as SECTION_NONE because they really belong in the
9591  * same section as their parent, whether that is pre-data or
9592  * post-data.
9593  */
9595  ARCHIVE_OPTS(.tag = tag->data,
9596  .namespace = namespace,
9597  .owner = owner,
9598  .description = "COMMENT",
9599  .section = SECTION_NONE,
9600  .createStmt = query->data,
9601  .deps = &dumpId,
9602  .nDeps = 1));
9603 
9604  destroyPQExpBuffer(query);
9605  destroyPQExpBuffer(tag);
9606  }
9607 }
static int findComments(Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9729
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 11095 of file pg_dump.c.

11096 {
11097  DumpOptions *dopt = fout->dopt;
11099  PQExpBuffer dropped = createPQExpBuffer();
11100  PQExpBuffer delq = createPQExpBuffer();
11101  PQExpBuffer query = createPQExpBuffer();
11102  PGresult *res;
11103  char *qtypname;
11104  char *qualtypname;
11105  int ntups;
11106  int i_attname;
11107  int i_atttypdefn;
11108  int i_attlen;
11109  int i_attalign;
11110  int i_attisdropped;
11111  int i_attcollation;
11112  int i;
11113  int actual_atts;
11114 
11116  {
11117  /*
11118  * Set up query for type-specific details.
11119  *
11120  * Since we only want to dump COLLATE clauses for attributes whose
11121  * collation is different from their type's default, we use a CASE
11122  * here to suppress uninteresting attcollations cheaply. atttypid
11123  * will be 0 for dropped columns; collation does not matter for those.
11124  */
11125  appendPQExpBufferStr(query,
11126  "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
11127  "SELECT a.attname, a.attnum, "
11128  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11129  "a.attlen, a.attalign, a.attisdropped, "
11130  "CASE WHEN a.attcollation <> at.typcollation "
11131  "THEN a.attcollation ELSE 0 END AS attcollation "
11132  "FROM pg_catalog.pg_type ct "
11133  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11134  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11135  "WHERE ct.oid = $1 "
11136  "ORDER BY a.attnum");
11137 
11138  ExecuteSqlStatement(fout, query->data);
11139 
11141  }
11142 
11143  printfPQExpBuffer(query,
11144  "EXECUTE dumpCompositeType('%u')",
11145  tyinfo->dobj.catId.oid);
11146 
11147  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11148 
11149  ntups = PQntuples(res);
11150 
11151  i_attname = PQfnumber(res, "attname");
11152  i_atttypdefn = PQfnumber(res, "atttypdefn");
11153  i_attlen = PQfnumber(res, "attlen");
11154  i_attalign = PQfnumber(res, "attalign");
11155  i_attisdropped = PQfnumber(res, "attisdropped");
11156  i_attcollation = PQfnumber(res, "attcollation");
11157 
11158  if (dopt->binary_upgrade)
11159  {
11161  tyinfo->dobj.catId.oid,
11162  false, false);
11163  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11164  }
11165 
11166  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11167  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11168 
11169  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11170  qualtypname);
11171 
11172  actual_atts = 0;
11173  for (i = 0; i < ntups; i++)
11174  {
11175  char *attname;
11176  char *atttypdefn;
11177  char *attlen;
11178  char *attalign;
11179  bool attisdropped;
11180  Oid attcollation;
11181 
11182  attname = PQgetvalue(res, i, i_attname);
11183  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11184  attlen = PQgetvalue(res, i, i_attlen);
11185  attalign = PQgetvalue(res, i, i_attalign);
11186  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11187  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11188 
11189  if (attisdropped && !dopt->binary_upgrade)
11190  continue;
11191 
11192  /* Format properly if not first attr */
11193  if (actual_atts++ > 0)
11194  appendPQExpBufferChar(q, ',');
11195  appendPQExpBufferStr(q, "\n\t");
11196 
11197  if (!attisdropped)
11198  {
11199  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11200 
11201  /* Add collation if not default for the column type */
11202  if (OidIsValid(attcollation))
11203  {
11204  CollInfo *coll;
11205 
11206  coll = findCollationByOid(attcollation);
11207  if (coll)
11208  appendPQExpBuffer(q, " COLLATE %s",
11209  fmtQualifiedDumpable(coll));
11210  }
11211  }
11212  else
11213  {
11214  /*
11215  * This is a dropped attribute and we're in binary_upgrade mode.
11216  * Insert a placeholder for it in the CREATE TYPE command, and set
11217  * length and alignment with direct UPDATE to the catalogs
11218  * afterwards. See similar code in dumpTableSchema().
11219  */
11220  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11221 
11222  /* stash separately for insertion after the CREATE TYPE */
11223  appendPQExpBufferStr(dropped,
11224  "\n-- For binary upgrade, recreate dropped column.\n");
11225  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11226  "SET attlen = %s, "
11227  "attalign = '%s', attbyval = false\n"
11228  "WHERE attname = ", attlen, attalign);
11229  appendStringLiteralAH(dropped, attname, fout);
11230  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11231  appendStringLiteralAH(dropped, qualtypname, fout);
11232  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11233 
11234  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11235  qualtypname);
11236  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11237  fmtId(attname));
11238  }
11239  }
11240  appendPQExpBufferStr(q, "\n);\n");
11241  appendPQExpBufferStr(q, dropped->data);
11242 
11243  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11244 
11245  if (dopt->binary_upgrade)
11247  "TYPE", qtypname,
11248  tyinfo->dobj.namespace->dobj.name);
11249 
11250  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11251  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11252  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11253  .namespace = tyinfo->dobj.namespace->dobj.name,
11254  .owner = tyinfo->rolname,
11255  .description = "TYPE",
11256  .section = SECTION_PRE_DATA,
11257  .createStmt = q->data,
11258  .dropStmt = delq->data));
11259 
11260 
11261  /* Dump Type Comments and Security Labels */
11262  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11263  dumpComment(fout, "TYPE", qtypname,
11264  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11265  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11266 
11267  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11268  dumpSecLabel(fout, "TYPE", qtypname,
11269  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11270  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11271 
11272  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11273  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11274  qtypname, NULL,
11275  tyinfo->dobj.namespace->dobj.name,
11276  tyinfo->rolname, &tyinfo->dacl);
11277 
11278  /* Dump any per-column comments */
11279  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11280  dumpCompositeTypeColComments(fout, tyinfo, res);
11281 
11282  PQclear(res);
11283  destroyPQExpBuffer(q);
11284  destroyPQExpBuffer(dropped);
11285  destroyPQExpBuffer(delq);
11286  destroyPQExpBuffer(query);
11287  free(qtypname);
11288  free(qualtypname);
11289 }
NameData attname
Definition: pg_attribute.h:41
char attalign
Definition: pg_attribute.h:118
int16 attlen
Definition: pg_attribute.h:68
@ 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:4897
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, PGresult *res)
Definition: pg_dump.c:11301
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 11301 of file pg_dump.c.

11303 {
11305  int ncomments;
11306  PQExpBuffer query;
11307  PQExpBuffer target;
11308  int i;
11309  int ntups;
11310  int i_attname;
11311  int i_attnum;
11312  int i_attisdropped;
11313 
11314  /* do nothing, if --no-comments is supplied */
11315  if (fout->dopt->no_comments)
11316  return;
11317 
11318  /* Search for comments associated with type's pg_class OID */
11319  ncomments = findComments(RelationRelationId, tyinfo->typrelid,
11320  &comments);
11321 
11322  /* If no comments exist, we're done */
11323  if (ncomments <= 0)
11324  return;
11325 
11326  /* Build COMMENT ON statements */
11327  query = createPQExpBuffer();
11328  target = createPQExpBuffer();
11329 
11330  ntups = PQntuples(res);
11331  i_attnum = PQfnumber(res, "attnum");
11332  i_attname = PQfnumber(res, "attname");
11333  i_attisdropped = PQfnumber(res, "attisdropped");
11334  while (ncomments > 0)
11335  {
11336  const char *attname;
11337 
11338  attname = NULL;
11339  for (i = 0; i < ntups; i++)
11340  {
11341  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
11342  PQgetvalue(res, i, i_attisdropped)[0] != 't')
11343  {
11344  attname = PQgetvalue(res, i, i_attname);
11345  break;
11346  }
11347  }
11348  if (attname) /* just in case we don't find it */
11349  {
11350  const char *descr = comments->descr;
11351 
11352  resetPQExpBuffer(target);
11353  appendPQExpBuffer(target, "COLUMN %s.",
11354  fmtId(tyinfo->dobj.name));
11356 
11357  resetPQExpBuffer(query);
11358  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11359  fmtQualifiedDumpable(tyinfo));
11360  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11361  appendStringLiteralAH(query, descr, fout);
11362  appendPQExpBufferStr(query, ";\n");
11363 
11365  ARCHIVE_OPTS(.tag = target->data,
11366  .namespace = tyinfo->dobj.namespace->dobj.name,
11367  .owner = tyinfo->rolname,
11368  .description = "COMMENT",
11369  .section = SECTION_NONE,
11370  .createStmt = query->data,
11371  .deps = &(tyinfo->dobj.dumpId),
11372  .nDeps = 1));
11373  }
11374 
11375  comments++;
11376  ncomments--;
11377  }
11378 
11379  destroyPQExpBuffer(query);
11380  destroyPQExpBuffer(target);
11381 }
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 16389 of file pg_dump.c.

16390 {
16391  DumpOptions *dopt = fout->dopt;
16392  TableInfo *tbinfo = coninfo->contable;
16393  PQExpBuffer q;
16394  PQExpBuffer delq;
16395  char *tag = NULL;
16396  char *foreign;
16397 
16398  /* Do nothing in data-only dump */
16399  if (dopt->dataOnly)
16400  return;
16401 
16402  q = createPQExpBuffer();
16403  delq = createPQExpBuffer();
16404 
16405  foreign = tbinfo &&
16406  tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16407 
16408  if (coninfo->contype == 'p' ||
16409  coninfo->contype == 'u' ||
16410  coninfo->contype == 'x')
16411  {
16412  /* Index-related constraint */
16413  IndxInfo *indxinfo;
16414  int k;
16415 
16416  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16417 
16418  if (indxinfo == NULL)
16419  pg_fatal("missing index for constraint \"%s\"",
16420  coninfo->dobj.name);
16421 
16422  if (dopt->binary_upgrade)
16424  indxinfo->dobj.catId.oid, true);
16425 
16426  appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
16427  fmtQualifiedDumpable(tbinfo));
16428  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
16429  fmtId(coninfo->dobj.name));
16430 
16431  if (coninfo->condef)
16432  {
16433  /* pg_get_constraintdef should have provided everything */
16434  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16435  }
16436  else
16437  {
16439  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16440  if (indxinfo->indnullsnotdistinct)
16441  appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
16442  appendPQExpBufferStr(q, " (");
16443  for (k = 0; k < indxinfo->indnkeyattrs; k++)
16444  {
16445  int indkey = (int) indxinfo->indkeys[k];
16446  const char *attname;
16447 
16448  if (indkey == InvalidAttrNumber)
16449  break;
16450  attname = getAttrName(indkey, tbinfo);
16451 
16452  appendPQExpBuffer(q, "%s%s",
16453  (k == 0) ? "" : ", ",
16454  fmtId(attname));
16455  }
16456 
16457  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
16458  appendPQExpBufferStr(q, ") INCLUDE (");
16459 
16460  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
16461  {
16462  int indkey = (int) indxinfo->indkeys[k];
16463  const char *attname;
16464 
16465  if (indkey == InvalidAttrNumber)
16466  break;
16467  attname = getAttrName(indkey, tbinfo);
16468 
16469  appendPQExpBuffer(q, "%s%s",
16470  (k == indxinfo->indnkeyattrs) ? "" : ", ",
16471  fmtId(attname));
16472  }
16473 
16474  appendPQExpBufferChar(q, ')');
16475 
16476  if (nonemptyReloptions(indxinfo->indreloptions))
16477  {
16478  appendPQExpBufferStr(q, " WITH (");
16479  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16480  appendPQExpBufferChar(q, ')');
16481  }
16482 
16483  if (coninfo->condeferrable)
16484  {
16485  appendPQExpBufferStr(q, " DEFERRABLE");
16486  if (coninfo->condeferred)
16487  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16488  }
16489 
16490  appendPQExpBufferStr(q, ";\n");
16491  }
16492 
16493  /*
16494  * Append ALTER TABLE commands as needed to set properties that we
16495  * only have ALTER TABLE syntax for. Keep this in sync with the
16496  * similar code in dumpIndex!
16497  */
16498 
16499  /* If the index is clustered, we need to record that. */
16500  if (indxinfo->indisclustered)
16501  {
16502  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16503  fmtQualifiedDumpable(tbinfo));
16504  /* index name is not qualified in this syntax */
16505  appendPQExpBuffer(q, " ON %s;\n",
16506  fmtId(indxinfo->dobj.name));
16507  }
16508 
16509  /* If the index defines identity, we need to record that. */
16510  if (indxinfo->indisreplident)
16511  {
16512  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
16513  fmtQualifiedDumpable(tbinfo));
16514  /* index name is not qualified in this syntax */
16515  appendPQExpBuffer(q, " INDEX %s;\n",
16516  fmtId(indxinfo->dobj.name));
16517  }
16518 
16519  /* Indexes can depend on extensions */
16520  append_depends_on_extension(fout, q, &indxinfo->dobj,
16521  "pg_catalog.pg_class", "INDEX",
16522  fmtQualifiedDumpable(indxinfo));
16523 
16524  appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
16525  fmtQualifiedDumpable(tbinfo));
16526  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16527  fmtId(coninfo->dobj.name));
16528 
16529  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16530 
16531  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16532  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16533  ARCHIVE_OPTS(.tag = tag,
16534  .namespace = tbinfo->dobj.namespace->dobj.name,
16535  .tablespace = indxinfo->tablespace,
16536  .owner = tbinfo->rolname,
16537  .description = "CONSTRAINT",
16538  .section = SECTION_POST_DATA,
16539  .createStmt = q->data,
16540  .dropStmt = delq->data));
16541  }
16542  else if (coninfo->contype == 'f')
16543  {
16544  char *only;
16545 
16546  /*
16547  * Foreign keys on partitioned tables are always declared as
16548  * inheriting to partitions; for all other cases, emit them as
16549  * applying ONLY directly to the named table, because that's how they
16550  * work for regular inherited tables.
16551  */
16552  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
16553 
16554  /*
16555  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
16556  * current table data is not processed
16557  */
16558  appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
16559  only, fmtQualifiedDumpable(tbinfo));
16560  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16561  fmtId(coninfo->dobj.name),
16562  coninfo->condef);
16563 
16564  appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
16565  only, fmtQualifiedDumpable(tbinfo));
16566  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16567  fmtId(coninfo->dobj.name));
16568 
16569  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16570 
16571  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16572  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16573  ARCHIVE_OPTS(.tag = tag,
16574  .namespace = tbinfo->dobj.namespace->dobj.name,
16575  .owner = tbinfo->rolname,
16576  .description = "FK CONSTRAINT",
16577  .section = SECTION_POST_DATA,
16578  .createStmt = q->data,
16579  .dropStmt = delq->data));
16580  }
16581  else if (coninfo->contype == 'c' && tbinfo)
16582  {
16583  /* CHECK constraint on a table */
16584 
16585  /* Ignore if not to be dumped separately, or if it was inherited */
16586  if (coninfo->separate && coninfo->conislocal)
16587  {
16588  /* not ONLY since we want it to propagate to children */
16589  appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
16590  fmtQualifiedDumpable(tbinfo));
16591  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16592  fmtId(coninfo->dobj.name),
16593  coninfo->condef);
16594 
16595  appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
16596  fmtQualifiedDumpable(tbinfo));
16597  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16598  fmtId(coninfo->dobj.name));
16599 
16600  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16601 
16602  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16603  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16604  ARCHIVE_OPTS(.tag = tag,
16605  .namespace = tbinfo->dobj.namespace->dobj.name,
16606  .owner = tbinfo->rolname,
16607  .description = "CHECK CONSTRAINT",
16608  .section = SECTION_POST_DATA,
16609  .createStmt = q->data,
16610  .dropStmt = delq->data));
16611  }
16612  }
16613  else if (coninfo->contype == 'c' && tbinfo == NULL)
16614  {
16615  /* CHECK constraint on a domain */
16616  TypeInfo *tyinfo = coninfo->condomain;
16617 
16618  /* Ignore if not to be dumped separately */
16619  if (coninfo->separate)
16620  {
16621  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
16622  fmtQualifiedDumpable(tyinfo));
16623  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16624  fmtId(coninfo->dobj.name),
16625  coninfo->condef);
16626 
16627  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
16628  fmtQualifiedDumpable(tyinfo));
16629  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16630  fmtId(coninfo->dobj.name));
16631 
16632  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
16633 
16634  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16635  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16636  ARCHIVE_OPTS(.tag = tag,
16637  .namespace = tyinfo->dobj.namespace->dobj.name,
16638  .owner = tyinfo->rolname,
16639  .description = "CHECK CONSTRAINT",
16640  .section = SECTION_POST_DATA,
16641  .createStmt = q->data,
16642  .dropStmt = delq->data));
16643  }
16644  }
16645  else
16646  {
16647  pg_fatal("unrecognized constraint type: %c",
16648  coninfo->contype);
16649  }
16650 
16651  /* Dump Constraint Comments --- only works for table constraints */
16652  if (tbinfo && coninfo->separate &&
16653  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16654  dumpTableConstraintComment(fout, coninfo);
16655 
16656  free(tag);
16657  destroyPQExpBuffer(q);
16658  destroyPQExpBuffer(delq);
16659 }
#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:4724
static const char * getAttrName(int attrnum, const TableInfo *tblInfo)
Definition: pg_dump.c:16102
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
Definition: pg_dump.c:18264
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
Definition: pg_dump.c:16669
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18252
TypeInfo * condomain
Definition: pg_dump.h:476
TableInfo * contable
Definition: pg_dump.h:475
bool condeferred
Definition: pg_dump.h:482
bool conislocal
Definition: pg_dump.h:483
DumpableObject dobj
Definition: pg_dump.h:474
DumpId conindex
Definition: pg_dump.h:480
bool condeferrable
Definition: pg_dump.h:481
char * condef
Definition: pg_dump.h:478
bool indisreplident
Definition: pg_dump.h:400
int indnkeyattrs
Definition: pg_dump.h:395
int indnattrs
Definition: pg_dump.h:396
Oid * indkeys
Definition: pg_dump.h:397
char * indreloptions
Definition: pg_dump.h:392
bool indisclustered
Definition: pg_dump.h:399
char * tablespace
Definition: pg_dump.h:391
bool indnullsnotdistinct
Definition: pg_dump.h:401
DumpableObject dobj
Definition: pg_dump.h:388

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

13335 {
13336  DumpOptions *dopt = fout->dopt;
13337  PQExpBuffer query;
13338  PQExpBuffer q;
13339  PQExpBuffer delq;
13340  char *qconvname;
13341  PGresult *res;
13342  int i_conforencoding;
13343  int i_contoencoding;
13344  int i_conproc;
13345  int i_condefault;
13346  const char *conforencoding;
13347  const char *contoencoding;
13348  const char *conproc;
13349  bool condefault;
13350 
13351  /* Do nothing in data-only dump */
13352  if (dopt->dataOnly)
13353  return;
13354 
13355  query = createPQExpBuffer();
13356  q = createPQExpBuffer();
13357  delq = createPQExpBuffer();
13358 
13359  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13360 
13361  /* Get conversion-specific details */
13362  appendPQExpBuffer(query, "SELECT "
13363  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13364  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13365  "conproc, condefault "
13366  "FROM pg_catalog.pg_conversion c "
13367  "WHERE c.oid = '%u'::pg_catalog.oid",
13368  convinfo->dobj.catId.oid);
13369 
13370  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13371 
13372  i_conforencoding = PQfnumber(res, "conforencoding");
13373  i_contoencoding = PQfnumber(res, "contoencoding");
13374  i_conproc = PQfnumber(res, "conproc");
13375  i_condefault = PQfnumber(res, "condefault");
13376 
13377  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13378  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13379  conproc = PQgetvalue(res, 0, i_conproc);
13380  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13381 
13382  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13383  fmtQualifiedDumpable(convinfo));
13384 
13385  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13386  (condefault) ? "DEFAULT " : "",
13387  fmtQualifiedDumpable(convinfo));
13388  appendStringLiteralAH(q, conforencoding, fout);
13389  appendPQExpBufferStr(q, " TO ");
13390  appendStringLiteralAH(q, contoencoding, fout);
13391  /* regproc output is already sufficiently quoted */
13392  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13393 
13394  if (dopt->binary_upgrade)
13395  binary_upgrade_extension_member(q, &convinfo->dobj,
13396  "CONVERSION", qconvname,
13397  convinfo->dobj.namespace->dobj.name);
13398 
13399  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13400  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13401  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13402  .namespace = convinfo->dobj.namespace->dobj.name,
13403  .owner = convinfo->rolname,
13404  .description = "CONVERSION",
13405  .section = SECTION_PRE_DATA,
13406  .createStmt = q->data,
13407  .dropStmt = delq->data));
13408 
13409  /* Dump Conversion Comments */
13410  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13411  dumpComment(fout, "CONVERSION", qconvname,
13412  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13413  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13414 
13415  PQclear(res);
13416 
13417  destroyPQExpBuffer(query);
13418  destroyPQExpBuffer(q);
13419  destroyPQExpBuffer(delq);
13420  free(qconvname);
13421 }
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 2829 of file pg_dump.c.

2830 {
2831  DumpOptions *dopt = fout->dopt;
2832  PQExpBuffer dbQry = createPQExpBuffer();
2833  PQExpBuffer delQry = createPQExpBuffer();
2834  PQExpBuffer creaQry = createPQExpBuffer();
2835  PQExpBuffer labelq = createPQExpBuffer();
2836  PGconn *conn = GetConnection(fout);
2837  PGresult *res;
2838  int i_tableoid,
2839  i_oid,
2840  i_datname,
2841  i_datdba,
2842  i_encoding,
2843  i_datlocprovider,
2844  i_collate,
2845  i_ctype,
2846  i_daticulocale,
2847  i_frozenxid,
2848  i_minmxid,
2849  i_datacl,
2850  i_acldefault,
2851  i_datistemplate,
2852  i_datconnlimit,
2853  i_datcollversion,
2854  i_tablespace;
2855  CatalogId dbCatId;
2856  DumpId dbDumpId;
2857  DumpableAcl dbdacl;
2858  const char *datname,
2859  *dba,
2860  *encoding,
2861  *datlocprovider,
2862  *collate,
2863  *ctype,
2864  *iculocale,
2865  *datistemplate,
2866  *datconnlimit,
2867  *tablespace;
2868  uint32 frozenxid,
2869  minmxid;
2870  char *qdatname;
2871 
2872  pg_log_info("saving database definition");
2873 
2874  /*
2875  * Fetch the database-level properties for this database.
2876  */
2877  appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, "
2878  "datdba, "
2879  "pg_encoding_to_char(encoding) AS encoding, "
2880  "datcollate, datctype, datfrozenxid, "
2881  "datacl, acldefault('d', datdba) AS acldefault, "
2882  "datistemplate, datconnlimit, ");
2883  if (fout->remoteVersion >= 90300)
2884  appendPQExpBufferStr(dbQry, "datminmxid, ");
2885  else
2886  appendPQExpBufferStr(dbQry, "0 AS datminmxid, ");
2887  if (fout->remoteVersion >= 150000)
2888  appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale, datcollversion, ");
2889  else
2890  appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS daticulocale, NULL AS datcollversion, ");
2891  appendPQExpBufferStr(dbQry,
2892  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2893  "shobj_description(oid, 'pg_database') AS description "
2894  "FROM pg_database "
2895  "WHERE datname = current_database()");
2896 
2897  res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
2898 
2899  i_tableoid = PQfnumber(res, "tableoid");
2900  i_oid = PQfnumber(res, "oid");
2901  i_datname = PQfnumber(res, "datname");
2902  i_datdba = PQfnumber(res, "datdba");
2903  i_encoding = PQfnumber(res, "encoding");
2904  i_datlocprovider = PQfnumber(res, "datlocprovider");
2905  i_collate = PQfnumber(res, "datcollate");
2906  i_ctype = PQfnumber(res, "datctype");
2907  i_daticulocale = PQfnumber(res, "daticulocale");
2908  i_frozenxid = PQfnumber(res, "datfrozenxid");
2909  i_minmxid = PQfnumber(res, "datminmxid");
2910  i_datacl = PQfnumber(res, "datacl");
2911  i_acldefault = PQfnumber(res, "acldefault");
2912  i_datistemplate = PQfnumber(res, "datistemplate");
2913  i_datconnlimit = PQfnumber(res, "datconnlimit");
2914  i_datcollversion = PQfnumber(res, "datcollversion");
2915  i_tablespace = PQfnumber(res, "tablespace");
2916 
2917  dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
2918  dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2919  datname = PQgetvalue(res, 0, i_datname);
2920  dba = getRoleName(PQgetvalue(res, 0, i_datdba));
2921  encoding = PQgetvalue(res, 0, i_encoding);
2922  datlocprovider = PQgetvalue(res, 0, i_datlocprovider);
2923  collate = PQgetvalue(res, 0, i_collate);
2924  ctype = PQgetvalue(res, 0, i_ctype);
2925  if (!PQgetisnull(res, 0, i_daticulocale))
2926  iculocale = PQgetvalue(res, 0, i_daticulocale);
2927  else
2928  iculocale = NULL;
2929  frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
2930  minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
2931  dbdacl.acl = PQgetvalue(res, 0, i_datacl);
2932  dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault);
2933  datistemplate = PQgetvalue(res, 0, i_datistemplate);
2934  datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
2935  tablespace = PQgetvalue(res, 0, i_tablespace);
2936 
2937  qdatname = pg_strdup(fmtId(datname));
2938 
2939  /*
2940  * Prepare the CREATE DATABASE command. We must specify OID (if we want
2941  * to preserve that), as well as the encoding, locale, and tablespace
2942  * since those can't be altered later. Other DB properties are left to
2943  * the DATABASE PROPERTIES entry, so that they can be applied after
2944  * reconnecting to the target DB.
2945  */
2946  if (dopt->binary_upgrade)
2947  {
2948  appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
2949  qdatname, dbCatId.oid);
2950  }
2951  else
2952  {
2953  appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
2954  qdatname);
2955  }
2956  if (strlen(encoding) > 0)
2957  {
2958  appendPQExpBufferStr(creaQry, " ENCODING = ");
2959  appendStringLiteralAH(creaQry, encoding, fout);
2960  }
2961 
2962  appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = ");
2963  if (datlocprovider[0] == 'c')
2964  appendPQExpBufferStr(creaQry, "libc");
2965  else if (datlocprovider[0] == 'i')
2966  appendPQExpBufferStr(creaQry, "icu");
2967  else
2968  pg_fatal("unrecognized locale provider: %s",
2969  datlocprovider);
2970 
2971  if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
2972  {
2973  appendPQExpBufferStr(creaQry, " LOCALE = ");
2974  appendStringLiteralAH(creaQry, collate, fout);
2975  }
2976  else
2977  {
2978  if (strlen(collate) > 0)
2979  {
2980  appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
2981  appendStringLiteralAH(creaQry, collate, fout);
2982  }
2983  if (strlen(ctype) > 0)
2984  {
2985  appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
2986  appendStringLiteralAH(creaQry, ctype, fout);
2987  }
2988  }
2989  if (iculocale)
2990  {
2991  appendPQExpBufferStr(creaQry, " ICU_LOCALE = ");
2992  appendStringLiteralAH(creaQry, iculocale, fout);
2993  }
2994 
2995  /*
2996  * For binary upgrade, carry over the collation version. For normal
2997  * dump/restore, omit the version, so that it is computed upon restore.
2998  */
2999  if (dopt->binary_upgrade)
3000  {
3001  if (!PQgetisnull(res, 0, i_datcollversion))
3002  {
3003  appendPQExpBufferStr(creaQry, " COLLATION_VERSION = ");
3004  appendStringLiteralAH(creaQry,
3005  PQgetvalue(res, 0, i_datcollversion),
3006  fout);
3007  }
3008  }
3009 
3010  /*
3011  * Note: looking at dopt->outputNoTablespaces here is completely the wrong
3012  * thing; the decision whether to specify a tablespace should be left till
3013  * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
3014  * label the DATABASE entry with the tablespace and let the normal
3015  * tablespace selection logic work ... but CREATE DATABASE doesn't pay
3016  * attention to default_tablespace, so that won't work.
3017  */
3018  if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
3019  !dopt->outputNoTablespaces)
3020  appendPQExpBuffer(creaQry, " TABLESPACE = %s",
3021  fmtId(tablespace));
3022  appendPQExpBufferStr(creaQry, ";\n");
3023 
3024  appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
3025  qdatname);
3026 
3027  dbDumpId = createDumpId();
3028 
3029  ArchiveEntry(fout,
3030  dbCatId, /* catalog ID */
3031  dbDumpId, /* dump ID */
3032  ARCHIVE_OPTS(.tag = datname,
3033  .owner = dba,
3034  .description = "DATABASE",
3035  .section = SECTION_PRE_DATA,
3036  .createStmt = creaQry->data,
3037  .dropStmt = delQry->data));
3038 
3039  /* Compute correct tag for archive entry */
3040  appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
3041 
3042  /* Dump DB comment if any */
3043  {
3044  /*
3045  * 8.2 and up keep comments on shared objects in a shared table, so we
3046  * cannot use the dumpComment() code used for other database objects.
3047  * Be careful that the ArchiveEntry parameters match that function.
3048  */
3049  char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
3050 
3051  if (comment && *comment && !dopt->no_comments)
3052  {
3053  resetPQExpBuffer(dbQry);
3054 
3055  /*
3056  * Generates warning when loaded into a differently-named
3057  * database.
3058  */
3059  appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
3060  appendStringLiteralAH(dbQry, comment, fout);
3061  appendPQExpBufferStr(dbQry, ";\n");
3062 
3064  ARCHIVE_OPTS(.tag = labelq->data,
3065  .owner = dba,
3066  .description = "COMMENT",
3067  .section = SECTION_NONE,
3068  .createStmt = dbQry->data,
3069  .deps = &dbDumpId,
3070  .nDeps = 1));
3071  }
3072  }
3073 
3074  /* Dump DB security label, if enabled */
3075  if (!dopt->no_security_labels)
3076  {
3077  PGresult *shres;
3078  PQExpBuffer seclabelQry;
3079 
3080  seclabelQry = createPQExpBuffer();
3081 
3082  buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
3083  shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
3084  resetPQExpBuffer(seclabelQry);
3085  emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
3086  if (seclabelQry->len > 0)
3088  ARCHIVE_OPTS(.tag = labelq->data,
3089  .owner = dba,
3090  .description = "SECURITY LABEL",
3091  .section = SECTION_NONE,
3092  .createStmt = seclabelQry->data,
3093  .deps = &dbDumpId,
3094  .nDeps = 1));
3095  destroyPQExpBuffer(seclabelQry);
3096  PQclear(shres);
3097  }
3098 
3099  /*
3100  * Dump ACL if any. Note that we do not support initial privileges
3101  * (pg_init_privs) on databases.
3102  */
3103  dbdacl.privtype = 0;
3104  dbdacl.initprivs = NULL;
3105 
3106  dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE",
3107  qdatname, NULL, NULL,
3108  dba, &dbdacl);
3109 
3110  /*
3111  * Now construct a DATABASE PROPERTIES archive entry to restore any
3112  * non-default database-level properties. (The reason this must be
3113  * separate is that we cannot put any additional commands into the TOC
3114  * entry that has CREATE DATABASE. pg_restore would execute such a group
3115  * in an implicit transaction block, and the backend won't allow CREATE
3116  * DATABASE in that context.)
3117  */
3118  resetPQExpBuffer(creaQry);
3119  resetPQExpBuffer(delQry);
3120 
3121  if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
3122  appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
3123  qdatname, datconnlimit);
3124 
3125  if (strcmp(datistemplate, "t") == 0)
3126  {
3127  appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
3128  qdatname);
3129 
3130  /*
3131  * The backend won't accept DROP DATABASE on a template database. We
3132  * can deal with that by removing the template marking before the DROP
3133  * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
3134  * since no such command is currently supported, fake it with a direct
3135  * UPDATE on pg_database.
3136  */
3137  appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
3138  "SET datistemplate = false WHERE datname = ");
3139  appendStringLiteralAH(delQry, datname, fout);
3140  appendPQExpBufferStr(delQry, ";\n");
3141  }
3142 
3143  /* Add database-specific SET options */
3144  dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
3145 
3146  /*
3147  * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
3148  * entry, too, for lack of a better place.
3149  */
3150  if (dopt->binary_upgrade)
3151  {
3152  appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
3153  appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
3154  "SET datfrozenxid = '%u', datminmxid = '%u'\n"
3155  "WHERE datname = ",
3156  frozenxid, minmxid);
3157  appendStringLiteralAH(creaQry, datname, fout);
3158  appendPQExpBufferStr(creaQry, ";\n");
3159  }
3160 
3161  if (creaQry->len > 0)
3163  ARCHIVE_OPTS(.tag = datname,
3164  .owner = dba,
3165  .description = "DATABASE PROPERTIES",
3166  .section = SECTION_PRE_DATA,
3167  .createStmt = creaQry->data,
3168  .dropStmt = delQry->data,
3169  .deps = &dbDumpId));
3170 
3171  /*
3172  * pg_largeobject comes from the old system intact, so set its
3173  * relfrozenxids, relminmxids and relfilenode.
3174  */
3175  if (dopt->binary_upgrade)
3176  {
3177  PGresult *lo_res;
3178  PQExpBuffer loFrozenQry = createPQExpBuffer();
3179  PQExpBuffer loOutQry = createPQExpBuffer();
3180  PQExpBuffer loHorizonQry = createPQExpBuffer();
3181  int ii_relfrozenxid,
3182  ii_relfilenode,
3183  ii_oid,
3184  ii_relminmxid;
3185 
3186  /*
3187  * pg_largeobject
3188  */
3189  if (fout->remoteVersion >= 90300)
3190  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n"
3191  "FROM pg_catalog.pg_class\n"
3192  "WHERE oid IN (%u, %u);\n",
3193  LargeObjectRelationId, LargeObjectLOidPNIndexId);
3194  else
3195  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid, relfilenode, oid\n"
3196  "FROM pg_catalog.pg_class\n"
3197  "WHERE oid IN (%u, %u);\n",
3198  LargeObjectRelationId, LargeObjectLOidPNIndexId);
3199 
3200  lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
3201 
3202  ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
3203  ii_relminmxid = PQfnumber(lo_res, "relminmxid");
3204  ii_relfilenode = PQfnumber(lo_res, "relfilenode");
3205  ii_oid = PQfnumber(lo_res, "oid");
3206 
3207  appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
3208  appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n");
3209  for (int i = 0; i < PQntuples(lo_res); ++i)
3210  {
3211  Oid oid;
3212  RelFileNumber relfilenumber;
3213 
3214  appendPQExpBuffer(loHorizonQry, "UPDATE pg_catalog.pg_class\n"
3215  "SET relfrozenxid = '%u', relminmxid = '%u'\n"
3216  "WHERE oid = %u;\n",
3217  atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)),
3218  atooid(PQgetvalue(lo_res, i, ii_relminmxid)),
3219  atooid(PQgetvalue(lo_res, i, ii_oid)));
3220 
3221  oid = atooid(PQgetvalue(lo_res, i, ii_oid));
3222  relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode));
3223 
3224  if (oid == LargeObjectRelationId)
3225  appendPQExpBuffer(loOutQry,
3226  "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
3227  relfilenumber);
3228  else if (oid == LargeObjectLOidPNIndexId)
3229  appendPQExpBuffer(loOutQry,
3230  "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
3231  relfilenumber);
3232  }
3233 
3234  appendPQExpBufferStr(loOutQry,
3235  "TRUNCATE pg_catalog.pg_largeobject;\n");
3236  appendPQExpBufferStr(loOutQry, loHorizonQry->data);
3237 
3239  ARCHIVE_OPTS(.tag = "pg_largeobject",
3240  .description = "pg_largeobject",
3241  .section = SECTION_PRE_DATA,
3242  .createStmt = loOutQry->data));
3243 
3244  PQclear(lo_res);
3245 
3246  destroyPQExpBuffer(loFrozenQry);
3247  destroyPQExpBuffer(loHorizonQry);
3248  destroyPQExpBuffer(loOutQry);
3249  }
3250 
3251  PQclear(res);
3252 
3253  free(qdatname);
3254  destroyPQExpBuffer(dbQry);
3255  destroyPQExpBuffer(delQry);
3256  destroyPQExpBuffer(creaQry);
3257  destroyPQExpBuffer(labelq);
3258 }
unsigned int uint32
Definition: c.h:490
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:135
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 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:9315
static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf, const char *dbname, Oid dboid)
Definition: pg_dump.c:3265
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(), 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 3265 of file pg_dump.c.

3267 {
3268  PGconn *conn = GetConnection(AH);
3270  PGresult *res;
3271 
3272  /* First collect database-specific options */
3273  printfPQExpBuffer(buf, "SELECT unnest(setconfig)");
3274  if (AH->remoteVersion >= 160000)
3275  appendPQExpBufferStr(buf, ", unnest(setuser)");
3276  appendPQExpBuffer(buf, " FROM pg_db_role_setting "
3277  "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3278  dboid);
3279 
3280  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3281 
3282  for (int i = 0; i < PQntuples(res); i++)
3283  {
3284  char *userset = NULL;
3285 
3286  if (AH->remoteVersion >= 160000)
3287  userset = PQgetvalue(res, i, 1);
3288  makeAlterConfigCommand(conn, PQgetvalue(res, i, 0), userset,
3289  "DATABASE", dbname, NULL, NULL,
3290  outbuf);
3291  }
3292 
3293  PQclear(res);
3294 
3295  /* Now look for role-and-database-specific options */
3296  printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig)");
3297  if (AH->remoteVersion >= 160000)
3298  appendPQExpBufferStr(buf, ", unnest(setuser)");
3299  appendPQExpBuffer(buf, " FROM pg_db_role_setting s, pg_roles r "
3300  "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3301  dboid);
3302 
3303  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3304 
3305  for (int i = 0; i < PQntuples(res); i++)
3306  {
3307  char *userset = NULL;
3308 
3309  if (AH->remoteVersion >= 160000)
3310  userset = PQgetvalue(res, i, 2);
3311  makeAlterConfigCommand(conn, PQgetvalue(res, i, 1), userset,
3312  "ROLE", PQgetvalue(res, i, 0),
3313  "DATABASE", dbname,
3314  outbuf);
3315  }
3316 
3317  PQclear(res);
3318 
3320 }
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *userset, 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 appendPQExpBuffer(), appendPQExpBufferStr(), buf, conn, createPQExpBuffer(), dbname, destroyPQExpBuffer(), ExecuteSqlQuery(), GetConnection(), i, makeAlterConfigCommand(), PGRES_TUPLES_OK, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), Archive::remoteVersion, and res.

Referenced by dumpDatabase().

◆ dumpDefaultACL()

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

Definition at line 14408 of file pg_dump.c.

14409 {
14410  DumpOptions *dopt = fout->dopt;
14411  PQExpBuffer q;
14412  PQExpBuffer tag;
14413  const char *type;
14414 
14415  /* Do nothing in data-only dump, or if we're skipping ACLs */
14416  if (dopt->dataOnly || dopt->aclsSkip)
14417  return;
14418 
14419  q = createPQExpBuffer();
14420  tag = createPQExpBuffer();
14421 
14422  switch (daclinfo->defaclobjtype)
14423  {
14424  case DEFACLOBJ_RELATION:
14425  type = "TABLES";
14426  break;
14427  case DEFACLOBJ_SEQUENCE:
14428  type = "SEQUENCES";
14429  break;
14430  case DEFACLOBJ_FUNCTION:
14431  type = "FUNCTIONS";
14432  break;
14433  case DEFACLOBJ_TYPE:
14434  type = "TYPES";
14435  break;
14436  case DEFACLOBJ_NAMESPACE:
14437  type = "SCHEMAS";
14438  break;
14439  default:
14440  /* shouldn't get here */
14441  pg_fatal("unrecognized object type in default privileges: %d",
14442  (int) daclinfo->defaclobjtype);
14443  type = ""; /* keep compiler quiet */
14444  }
14445 
14446  appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
14447 
14448  /* build the actual command(s) for this tuple */
14450  daclinfo->dobj.namespace != NULL ?
14451  daclinfo->dobj.namespace->dobj.name : NULL,
14452  daclinfo->dacl.acl,
14453  daclinfo->dacl.acldefault,
14454  daclinfo->defaclrole,
14455  fout->remoteVersion,
14456  q))
14457  pg_fatal("could not parse default ACL list (%s)",
14458  daclinfo->dacl.acl);
14459 
14460  if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
14461  ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
14462  ARCHIVE_OPTS(.tag = tag->data,
14463  .namespace = daclinfo->dobj.namespace ?
14464  daclinfo->dobj.namespace->dobj.name : NULL,
14465  .owner = daclinfo->defaclrole,
14466  .description = "DEFAULT ACL",
14467  .section = SECTION_POST_DATA,
14468  .createStmt = q->data));
14469 
14470  destroyPQExpBuffer(tag);
14471  destroyPQExpBuffer(q);
14472 }
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:579
DumpableAcl dacl
Definition: pg_dump.h:580
const char * defaclrole
Definition: pg_dump.h:581
char defaclobjtype
Definition: pg_dump.h:582

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

10923 {
10924  DumpOptions *dopt = fout->dopt;
10926  PQExpBuffer delq = createPQExpBuffer();
10927  PQExpBuffer query = createPQExpBuffer();
10928  PGresult *res;
10929  int i;
10930  char *qtypname;
10931  char *qualtypname;
10932  char *typnotnull;
10933  char *typdefn;
10934  char *typdefault;
10935  Oid typcollation;
10936  bool typdefault_is_literal = false;
10937 
10938  if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN])
10939  {
10940  /* Set up query for domain-specific details */
10941  appendPQExpBufferStr(query,
10942  "PREPARE dumpDomain(pg_catalog.oid) AS\n");
10943 
10944  appendPQExpBufferStr(query, "SELECT t.typnotnull, "
10945  "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
10946  "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
10947  "t.typdefault, "
10948  "CASE WHEN t.typcollation <> u.typcollation "
10949  "THEN t.typcollation ELSE 0 END AS typcollation "
10950  "FROM pg_catalog.pg_type t "
10951  "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
10952  "WHERE t.oid = $1");
10953 
10954  ExecuteSqlStatement(fout, query->data);
10955 
10956  fout->is_prepared[PREPQUERY_DUMPDOMAIN] = true;
10957  }
10958 
10959  printfPQExpBuffer(query,
10960  "EXECUTE dumpDomain('%u')",
10961  tyinfo->dobj.catId.oid);
10962 
10963  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10964 
10965  typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
10966  typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
10967  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10968  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10969  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10970  {
10971  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10972  typdefault_is_literal = true; /* it needs quotes */
10973  }
10974  else
10975  typdefault = NULL;
10976  typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
10977 
10978  if (dopt->binary_upgrade)
10980  tyinfo->dobj.