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_proc_d.h"
#include "catalog/pg_subscription_d.h"
#include "catalog/pg_type_d.h"
#include "common/connect.h"
#include "common/int.h"
#include "common/relpath.h"
#include "compress_io.h"
#include "dumputils.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
#include "filter.h"
#include "getopt_long.h"
#include "libpq/libpq-fs.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "storage/block.h"
Include dependency graph for pg_dump.c:

Go to the source code of this file.

Data Structures

struct  RoleNameItem
 
struct  CommentItem
 
struct  SecLabelItem
 
struct  BinaryUpgradeClassOidItem
 
struct  SequenceItem
 

Macros

#define DUMP_DEFAULT_ROWS_PER_INSERT   1
 
#define MAX_BLOBS_PER_ARCHIVE_ENTRY   1000
 
#define fmtQualifiedDumpable(obj)
 

Typedefs

typedef enum SeqType SeqType
 
typedef enum OidOptions OidOptions
 

Enumerations

enum  SeqType { SEQTYPE_SMALLINT , SEQTYPE_INTEGER , SEQTYPE_BIGINT }
 
enum  OidOptions { zeroIsError = 1 , zeroAsStar = 2 , zeroAsNone = 4 }
 

Functions

 StaticAssertDecl (lengthof(SeqTypeNames)==(SEQTYPE_BIGINT+1), "array length mismatch")
 
static void help (const char *progname)
 
static void setup_connection (Archive *AH, const char *dumpencoding, const char *dumpsnapshot, char *use_role)
 
static ArchiveFormat parseArchiveFormat (const char *format, ArchiveMode *mode)
 
static void expand_schema_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_extension_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_foreign_server_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids)
 
static void expand_table_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names, bool with_child_tables)
 
static void prohibit_crossdb_refs (PGconn *conn, const char *dbname, const char *pattern)
 
static NamespaceInfofindNamespace (Oid nsoid)
 
static void dumpTableData (Archive *fout, const TableDataInfo *tdinfo)
 
static void refreshMatViewData (Archive *fout, const TableDataInfo *tdinfo)
 
static const char * getRoleName (const char *roleoid_str)
 
static void collectRoleNames (Archive *fout)
 
static void getAdditionalACLs (Archive *fout)
 
static void dumpCommentExtended (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId, const char *initdb_comment)
 
static void dumpComment (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
 
static int findComments (Oid classoid, Oid objoid, CommentItem **items)
 
static void collectComments (Archive *fout)
 
static void dumpSecLabel (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
 
static int findSecLabels (Oid classoid, Oid objoid, SecLabelItem **items)
 
static void collectSecLabels (Archive *fout)
 
static void dumpDumpableObject (Archive *fout, DumpableObject *dobj)
 
static void dumpNamespace (Archive *fout, const NamespaceInfo *nspinfo)
 
static void dumpExtension (Archive *fout, const ExtensionInfo *extinfo)
 
static void dumpType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpBaseType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpEnumType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpRangeType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpUndefinedType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpDomain (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpCompositeType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpCompositeTypeColComments (Archive *fout, const TypeInfo *tyinfo, PGresult *res)
 
static void dumpShellType (Archive *fout, const ShellTypeInfo *stinfo)
 
static void dumpProcLang (Archive *fout, const ProcLangInfo *plang)
 
static void dumpFunc (Archive *fout, const FuncInfo *finfo)
 
static void dumpCast (Archive *fout, const CastInfo *cast)
 
static void dumpTransform (Archive *fout, const TransformInfo *transform)
 
static void dumpOpr (Archive *fout, const OprInfo *oprinfo)
 
static void dumpAccessMethod (Archive *fout, const AccessMethodInfo *aminfo)
 
static void dumpOpclass (Archive *fout, const OpclassInfo *opcinfo)
 
static void dumpOpfamily (Archive *fout, const OpfamilyInfo *opfinfo)
 
static void dumpCollation (Archive *fout, const CollInfo *collinfo)
 
static void dumpConversion (Archive *fout, const ConvInfo *convinfo)
 
static void dumpRule (Archive *fout, const RuleInfo *rinfo)
 
static void dumpAgg (Archive *fout, const AggInfo *agginfo)
 
static void dumpTrigger (Archive *fout, const TriggerInfo *tginfo)
 
static void dumpEventTrigger (Archive *fout, const EventTriggerInfo *evtinfo)
 
static void dumpTable (Archive *fout, const TableInfo *tbinfo)
 
static void dumpTableSchema (Archive *fout, const TableInfo *tbinfo)
 
static void dumpTableAttach (Archive *fout, const TableAttachInfo *attachinfo)
 
static void dumpAttrDef (Archive *fout, const AttrDefInfo *adinfo)
 
static void collectSequences (Archive *fout)
 
static void dumpSequence (Archive *fout, const TableInfo *tbinfo)
 
static void dumpSequenceData (Archive *fout, const TableDataInfo *tdinfo)
 
static void dumpIndex (Archive *fout, const IndxInfo *indxinfo)
 
static void dumpIndexAttach (Archive *fout, const IndexAttachInfo *attachinfo)
 
static void dumpStatisticsExt (Archive *fout, const StatsExtInfo *statsextinfo)
 
static void dumpConstraint (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTableConstraintComment (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTSParser (Archive *fout, const TSParserInfo *prsinfo)
 
static void dumpTSDictionary (Archive *fout, const TSDictInfo *dictinfo)
 
static void dumpTSTemplate (Archive *fout, const TSTemplateInfo *tmplinfo)
 
static void dumpTSConfig (Archive *fout, const TSConfigInfo *cfginfo)
 
static void dumpForeignDataWrapper (Archive *fout, const FdwInfo *fdwinfo)
 
static void dumpForeignServer (Archive *fout, const ForeignServerInfo *srvinfo)
 
static void dumpUserMappings (Archive *fout, const char *servername, const char *namespace, const char *owner, CatalogId catalogId, DumpId dumpId)
 
static void dumpDefaultACL (Archive *fout, const DefaultACLInfo *daclinfo)
 
static DumpId dumpACL (Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *tag, const char *owner, const DumpableAcl *dacl)
 
static void getDependencies (Archive *fout)
 
static void BuildArchiveDependencies (Archive *fout)
 
static void findDumpableDependencies (ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
 
static DumpableObjectcreateBoundaryObjects (void)
 
static void addBoundaryDependencies (DumpableObject **dobjs, int numObjs, DumpableObject *boundaryObjs)
 
static void addConstrChildIdxDeps (DumpableObject *dobj, const IndxInfo *refidx)
 
static void getDomainConstraints (Archive *fout, TypeInfo *tyinfo)
 
static void getTableData (DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
 
static void makeTableDataInfo (DumpOptions *dopt, TableInfo *tbinfo)
 
static void buildMatViewRefreshDependencies (Archive *fout)
 
static void getTableDataFKConstraints (void)
 
static void determineNotNullFlags (Archive *fout, PGresult *res, int r, TableInfo *tbinfo, int j, int i_notnull_name, int i_notnull_noinherit, int i_notnull_islocal)
 
static char * format_function_arguments (const FuncInfo *finfo, const char *funcargs, bool is_agg)
 
static char * format_function_signature (Archive *fout, const FuncInfo *finfo, bool honor_quotes)
 
static char * convertRegProcReference (const char *proc)
 
static char * getFormattedOperatorName (const char *oproid)
 
static char * convertTSFunction (Archive *fout, Oid funcOid)
 
static const char * getFormattedTypeName (Archive *fout, Oid oid, OidOptions opts)
 
static void getLOs (Archive *fout)
 
static void dumpLO (Archive *fout, const LoInfo *loinfo)
 
static int dumpLOs (Archive *fout, const void *arg)
 
static void dumpPolicy (Archive *fout, const PolicyInfo *polinfo)
 
static void dumpPublication (Archive *fout, const PublicationInfo *pubinfo)
 
static void dumpPublicationTable (Archive *fout, const PublicationRelInfo *pubrinfo)
 
static void dumpSubscription (Archive *fout, const SubscriptionInfo *subinfo)
 
static void dumpSubscriptionTable (Archive *fout, const SubRelInfo *subrinfo)
 
static void dumpDatabase (Archive *fout)
 
static void dumpDatabaseConfig (Archive *AH, PQExpBuffer outbuf, const char *dbname, Oid dboid)
 
static void dumpEncoding (Archive *AH)
 
static void dumpStdStrings (Archive *AH)
 
static void dumpSearchPath (Archive *AH)
 
static void binary_upgrade_set_type_oids_by_type_oid (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type, bool include_multirange_type)
 
static void binary_upgrade_set_type_oids_by_rel (Archive *fout, PQExpBuffer upgrade_buffer, const TableInfo *tbinfo)
 
static void collectBinaryUpgradeClassOids (Archive *fout)
 
static void binary_upgrade_set_pg_class_oids (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid)
 
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 set_restrict_relation_kind (Archive *AH, const char *value)
 
static void setupDumpWorker (Archive *AH)
 
static TableInfogetRootTableInfo (const TableInfo *tbinfo)
 
static bool forcePartitionRootLoad (const TableInfo *tbinfo)
 
static void read_dump_filters (const char *filename, DumpOptions *dopt)
 
int main (int argc, char **argv)
 
static bool checkExtensionMembership (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableNamespace (NamespaceInfo *nsinfo, Archive *fout)
 
static void selectDumpableTable (TableInfo *tbinfo, Archive *fout)
 
static void selectDumpableType (TypeInfo *tyinfo, Archive *fout)
 
static void selectDumpableDefaultACL (DefaultACLInfo *dinfo, DumpOptions *dopt)
 
static void selectDumpableCast (CastInfo *cast, Archive *fout)
 
static void selectDumpableProcLang (ProcLangInfo *plang, Archive *fout)
 
static void selectDumpableAccessMethod (AccessMethodInfo *method, Archive *fout)
 
static void selectDumpableExtension (ExtensionInfo *extinfo, DumpOptions *dopt)
 
static void selectDumpablePublicationObject (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableStatisticsObject (StatsExtInfo *sobj, Archive *fout)
 
static void selectDumpableObject (DumpableObject *dobj, Archive *fout)
 
static int dumpTableData_copy (Archive *fout, const void *dcontext)
 
static int dumpTableData_insert (Archive *fout, const void *dcontext)
 
void getPolicies (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getPublications (Archive *fout)
 
void getPublicationNamespaces (Archive *fout)
 
void getPublicationTables (Archive *fout, TableInfo tblinfo[], int numTables)
 
static void dumpPublicationNamespace (Archive *fout, const PublicationSchemaInfo *pubsinfo)
 
static bool is_superuser (Archive *fout)
 
void getSubscriptions (Archive *fout)
 
void getSubscriptionTables (Archive *fout)
 
static void append_depends_on_extension (Archive *fout, PQExpBuffer create, const DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
 
static Oid get_next_possible_free_pg_type_oid (Archive *fout, PQExpBuffer upgrade_query)
 
static int BinaryUpgradeClassOidItemCmp (const void *p1, const void *p2)
 
void getNamespaces (Archive *fout)
 
ExtensionInfogetExtensions (Archive *fout, int *numExtensions)
 
void getTypes (Archive *fout)
 
void getOperators (Archive *fout)
 
void getCollations (Archive *fout)
 
void getConversions (Archive *fout)
 
void getAccessMethods (Archive *fout)
 
void getOpclasses (Archive *fout)
 
void getOpfamilies (Archive *fout)
 
void getAggregates (Archive *fout)
 
void getFuncs (Archive *fout)
 
TableInfogetTables (Archive *fout, int *numTables)
 
void getOwnedSeqs (Archive *fout, TableInfo tblinfo[], int numTables)
 
InhInfogetInherits (Archive *fout, int *numInherits)
 
void getPartitioningInfo (Archive *fout)
 
void getIndexes (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getExtendedStatistics (Archive *fout)
 
void getConstraints (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getRules (Archive *fout)
 
void getTriggers (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getEventTriggers (Archive *fout)
 
void getProcLangs (Archive *fout)
 
void getCasts (Archive *fout)
 
static char * get_language_name (Archive *fout, Oid langid)
 
void getTransforms (Archive *fout)
 
void getTableAttrs (Archive *fout, TableInfo *tblinfo, int numTables)
 
bool shouldPrintColumn (const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
 
void getTSParsers (Archive *fout)
 
void getTSDictionaries (Archive *fout)
 
void getTSTemplates (Archive *fout)
 
void getTSConfigurations (Archive *fout)
 
void getForeignDataWrappers (Archive *fout)
 
void getForeignServers (Archive *fout)
 
void getDefaultACLs (Archive *fout)
 
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)
 
static SeqType parse_sequence_type (const char *name)
 
static int SequenceItemCmp (const void *p1, const void *p2)
 
void getExtensionMembership (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 
void processExtensionTables (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 

Variables

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

Macro Definition Documentation

◆ DUMP_DEFAULT_ROWS_PER_INSERT

#define DUMP_DEFAULT_ROWS_PER_INSERT   1

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

◆ MAX_BLOBS_PER_ARCHIVE_ENTRY

#define MAX_BLOBS_PER_ARCHIVE_ENTRY   1000

Definition at line 221 of file pg_dump.c.

Typedef Documentation

◆ OidOptions

typedef enum OidOptions OidOptions

◆ SeqType

typedef enum SeqType SeqType

Enumeration Type Documentation

◆ OidOptions

enum OidOptions
Enumerator
zeroIsError 
zeroAsStar 
zeroAsNone 

Definition at line 137 of file pg_dump.c.

138{
139 zeroIsError = 1,
140 zeroAsStar = 2,
141 zeroAsNone = 4,
142} OidOptions;
OidOptions
Definition: pg_dump.c:138
@ zeroIsError
Definition: pg_dump.c:139
@ zeroAsStar
Definition: pg_dump.c:140
@ zeroAsNone
Definition: pg_dump.c:141

◆ SeqType

enum SeqType
Enumerator
SEQTYPE_SMALLINT 
SEQTYPE_INTEGER 
SEQTYPE_BIGINT 

Definition at line 106 of file pg_dump.c.

107{
111} SeqType;
SeqType
Definition: pg_dump.c:107
@ SEQTYPE_BIGINT
Definition: pg_dump.c:110
@ SEQTYPE_INTEGER
Definition: pg_dump.c:109
@ SEQTYPE_SMALLINT
Definition: pg_dump.c:108

Function Documentation

◆ addBoundaryDependencies()

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

Definition at line 18886 of file pg_dump.c.

18888{
18889 DumpableObject *preDataBound = boundaryObjs + 0;
18890 DumpableObject *postDataBound = boundaryObjs + 1;
18891 int i;
18892
18893 for (i = 0; i < numObjs; i++)
18894 {
18895 DumpableObject *dobj = dobjs[i];
18896
18897 /*
18898 * The classification of object types here must match the SECTION_xxx
18899 * values assigned during subsequent ArchiveEntry calls!
18900 */
18901 switch (dobj->objType)
18902 {
18903 case DO_NAMESPACE:
18904 case DO_EXTENSION:
18905 case DO_TYPE:
18906 case DO_SHELL_TYPE:
18907 case DO_FUNC:
18908 case DO_AGG:
18909 case DO_OPERATOR:
18910 case DO_ACCESS_METHOD:
18911 case DO_OPCLASS:
18912 case DO_OPFAMILY:
18913 case DO_COLLATION:
18914 case DO_CONVERSION:
18915 case DO_TABLE:
18916 case DO_TABLE_ATTACH:
18917 case DO_ATTRDEF:
18918 case DO_PROCLANG:
18919 case DO_CAST:
18920 case DO_DUMMY_TYPE:
18921 case DO_TSPARSER:
18922 case DO_TSDICT:
18923 case DO_TSTEMPLATE:
18924 case DO_TSCONFIG:
18925 case DO_FDW:
18926 case DO_FOREIGN_SERVER:
18927 case DO_TRANSFORM:
18928 /* Pre-data objects: must come before the pre-data boundary */
18929 addObjectDependency(preDataBound, dobj->dumpId);
18930 break;
18931 case DO_TABLE_DATA:
18932 case DO_SEQUENCE_SET:
18933 case DO_LARGE_OBJECT:
18935 /* Data objects: must come between the boundaries */
18936 addObjectDependency(dobj, preDataBound->dumpId);
18937 addObjectDependency(postDataBound, dobj->dumpId);
18938 break;
18939 case DO_INDEX:
18940 case DO_INDEX_ATTACH:
18941 case DO_STATSEXT:
18942 case DO_REFRESH_MATVIEW:
18943 case DO_TRIGGER:
18944 case DO_EVENT_TRIGGER:
18945 case DO_DEFAULT_ACL:
18946 case DO_POLICY:
18947 case DO_PUBLICATION:
18948 case DO_PUBLICATION_REL:
18950 case DO_SUBSCRIPTION:
18952 /* Post-data objects: must come after the post-data boundary */
18953 addObjectDependency(dobj, postDataBound->dumpId);
18954 break;
18955 case DO_RULE:
18956 /* Rules are post-data, but only if dumped separately */
18957 if (((RuleInfo *) dobj)->separate)
18958 addObjectDependency(dobj, postDataBound->dumpId);
18959 break;
18960 case DO_CONSTRAINT:
18961 case DO_FK_CONSTRAINT:
18962 /* Constraints are post-data, but only if dumped separately */
18963 if (((ConstraintInfo *) dobj)->separate)
18964 addObjectDependency(dobj, postDataBound->dumpId);
18965 break;
18967 /* nothing to do */
18968 break;
18970 /* must come after the pre-data boundary */
18971 addObjectDependency(dobj, preDataBound->dumpId);
18972 break;
18973 }
18974 }
18975}
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:795
int i
Definition: isn.c:72
@ DO_EVENT_TRIGGER
Definition: pg_dump.h:79
@ DO_REFRESH_MATVIEW
Definition: pg_dump.h:80
@ DO_POLICY
Definition: pg_dump.h:81
@ DO_CAST
Definition: pg_dump.h:63
@ DO_FOREIGN_SERVER
Definition: pg_dump.h:72
@ DO_PRE_DATA_BOUNDARY
Definition: pg_dump.h:77
@ DO_PROCLANG
Definition: pg_dump.h:62
@ DO_TYPE
Definition: pg_dump.h:42
@ DO_INDEX
Definition: pg_dump.h:55
@ DO_COLLATION
Definition: pg_dump.h:50
@ DO_LARGE_OBJECT
Definition: pg_dump.h:75
@ DO_TSCONFIG
Definition: pg_dump.h:70
@ DO_OPERATOR
Definition: pg_dump.h:46
@ DO_FK_CONSTRAINT
Definition: pg_dump.h:61
@ DO_CONSTRAINT
Definition: pg_dump.h:60
@ DO_SUBSCRIPTION
Definition: pg_dump.h:85
@ DO_DEFAULT_ACL
Definition: pg_dump.h:73
@ DO_FDW
Definition: pg_dump.h:71
@ DO_SUBSCRIPTION_REL
Definition: pg_dump.h:86
@ DO_SEQUENCE_SET
Definition: pg_dump.h:65
@ DO_ATTRDEF
Definition: pg_dump.h:54
@ DO_PUBLICATION_REL
Definition: pg_dump.h:83
@ DO_TABLE_ATTACH
Definition: pg_dump.h:53
@ DO_OPCLASS
Definition: pg_dump.h:48
@ DO_INDEX_ATTACH
Definition: pg_dump.h:56
@ DO_TSTEMPLATE
Definition: pg_dump.h:69
@ DO_STATSEXT
Definition: pg_dump.h:57
@ DO_FUNC
Definition: pg_dump.h:44
@ DO_POST_DATA_BOUNDARY
Definition: pg_dump.h:78
@ DO_LARGE_OBJECT_DATA
Definition: pg_dump.h:76
@ DO_OPFAMILY
Definition: pg_dump.h:49
@ DO_TRANSFORM
Definition: pg_dump.h:74
@ DO_ACCESS_METHOD
Definition: pg_dump.h:47
@ DO_PUBLICATION_TABLE_IN_SCHEMA
Definition: pg_dump.h:84
@ DO_CONVERSION
Definition: pg_dump.h:51
@ DO_TRIGGER
Definition: pg_dump.h:59
@ DO_RULE
Definition: pg_dump.h:58
@ DO_DUMMY_TYPE
Definition: pg_dump.h:66
@ DO_TSDICT
Definition: pg_dump.h:68
@ DO_TSPARSER
Definition: pg_dump.h:67
@ DO_EXTENSION
Definition: pg_dump.h:41
@ DO_TABLE_DATA
Definition: pg_dump.h:64
@ DO_PUBLICATION
Definition: pg_dump.h:82
@ DO_TABLE
Definition: pg_dump.h:52
@ DO_NAMESPACE
Definition: pg_dump.h:40
@ DO_AGG
Definition: pg_dump.h:45
@ DO_SHELL_TYPE
Definition: pg_dump.h:43
DumpId dumpId
Definition: pg_dump.h:145
DumpableObjectType objType
Definition: pg_dump.h:143

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

Referenced by main().

◆ addConstrChildIdxDeps()

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

Definition at line 7979 of file pg_dump.c.

7980{
7981 SimplePtrListCell *cell;
7982
7984
7985 for (cell = refidx->partattaches.head; cell; cell = cell->next)
7986 {
7987 IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7988
7989 addObjectDependency(dobj, attach->dobj.dumpId);
7990
7991 if (attach->partitionIdx->partattaches.head != NULL)
7992 addConstrChildIdxDeps(dobj, attach->partitionIdx);
7993 }
7994}
#define Assert(condition)
Definition: c.h:812
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7979
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54
IndxInfo * partitionIdx
Definition: pg_dump.h:429
DumpableObject dobj
Definition: pg_dump.h:427
SimplePtrList partattaches
Definition: pg_dump.h:419

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

Referenced by addConstrChildIdxDeps(), and 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 5347 of file pg_dump.c.

5353{
5354 if (dobj->depends_on_ext)
5355 {
5356 char *nm;
5357 PGresult *res;
5358 PQExpBuffer query;
5359 int ntups;
5360 int i_extname;
5361 int i;
5362
5363 /* dodge fmtId() non-reentrancy */
5364 nm = pg_strdup(objname);
5365
5366 query = createPQExpBuffer();
5367 appendPQExpBuffer(query,
5368 "SELECT e.extname "
5369 "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
5370 "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
5371 "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
5372 "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
5373 catalog,
5374 dobj->catId.oid);
5375 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5376 ntups = PQntuples(res);
5377 i_extname = PQfnumber(res, "extname");
5378 for (i = 0; i < ntups; i++)
5379 {
5380 appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;",
5381 keyword, nm,
5382 fmtId(PQgetvalue(res, i, i_extname)));
5383 }
5384
5385 PQclear(res);
5386 destroyPQExpBuffer(query);
5387 pg_free(nm);
5388 }
5389}
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:288
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:144
bool depends_on_ext
Definition: pg_dump.h:152

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

19205{
19206 bool res;
19207
19208 res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
19209 fout->std_strings);
19210 if (!res)
19211 pg_log_warning("could not parse %s array", "reloptions");
19212}
#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:235
int encoding
Definition: pg_backup.h:234

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

5663{
5664 DumpableObject *extobj = NULL;
5665 int i;
5666
5667 if (!dobj->ext_member)
5668 return;
5669
5670 /*
5671 * Find the parent extension. We could avoid this search if we wanted to
5672 * add a link field to DumpableObject, but the space costs of that would
5673 * be considerable. We assume that member objects could only have a
5674 * direct dependency on their own extension, not any others.
5675 */
5676 for (i = 0; i < dobj->nDeps; i++)
5677 {
5678 extobj = findObjectByDumpId(dobj->dependencies[i]);
5679 if (extobj && extobj->objType == DO_EXTENSION)
5680 break;
5681 extobj = NULL;
5682 }
5683 if (extobj == NULL)
5684 pg_fatal("could not find parent extension for %s %s",
5685 objtype, objname);
5686
5687 appendPQExpBufferStr(upgrade_buffer,
5688 "\n-- For binary upgrade, handle extension membership the hard way\n");
5689 appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
5690 fmtId(extobj->name),
5691 objtype);
5692 if (objnamespace && *objnamespace)
5693 appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
5694 appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
5695}
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:742
#define pg_fatal(...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * name
Definition: pg_dump.h:146
DumpId * dependencies
Definition: pg_dump.h:153
bool ext_member
Definition: pg_dump.h:151

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 
)
static

Definition at line 5568 of file pg_dump.c.

5570{
5573
5575
5576 /*
5577 * Preserve the OID and relfilenumber of the table, table's index, table's
5578 * toast table and toast table's index if any.
5579 *
5580 * One complexity is that the current table definition might not require
5581 * the creation of a TOAST table, but the old database might have a TOAST
5582 * table that was created earlier, before some wide columns were dropped.
5583 * By setting the TOAST oid we force creation of the TOAST heap and index
5584 * by the new backend, so we can copy the files during binary upgrade
5585 * without worrying about this case.
5586 */
5587 key.oid = pg_class_oid;
5591
5592 appendPQExpBufferStr(upgrade_buffer,
5593 "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
5594
5595 if (entry->relkind != RELKIND_INDEX &&
5596 entry->relkind != RELKIND_PARTITIONED_INDEX)
5597 {
5598 appendPQExpBuffer(upgrade_buffer,
5599 "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
5600 pg_class_oid);
5601
5602 /*
5603 * Not every relation has storage. Also, in a pre-v12 database,
5604 * partitioned tables have a relfilenumber, which should not be
5605 * preserved when upgrading.
5606 */
5607 if (RelFileNumberIsValid(entry->relfilenumber) &&
5608 entry->relkind != RELKIND_PARTITIONED_TABLE)
5609 appendPQExpBuffer(upgrade_buffer,
5610 "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
5611 entry->relfilenumber);
5612
5613 /*
5614 * In a pre-v12 database, partitioned tables might be marked as having
5615 * toast tables, but we should ignore them if so.
5616 */
5617 if (OidIsValid(entry->toast_oid) &&
5618 entry->relkind != RELKIND_PARTITIONED_TABLE)
5619 {
5620 appendPQExpBuffer(upgrade_buffer,
5621 "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
5622 entry->toast_oid);
5623 appendPQExpBuffer(upgrade_buffer,
5624 "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
5625 entry->toast_relfilenumber);
5626
5627 /* every toast table has an index */
5628 appendPQExpBuffer(upgrade_buffer,
5629 "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5630 entry->toast_index_oid);
5631 appendPQExpBuffer(upgrade_buffer,
5632 "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5634 }
5635 }
5636 else
5637 {
5638 /* Preserve the OID and relfilenumber of the index */
5639 appendPQExpBuffer(upgrade_buffer,
5640 "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
5641 pg_class_oid);
5642 appendPQExpBuffer(upgrade_buffer,
5643 "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
5644 entry->relfilenumber);
5645 }
5646
5647 appendPQExpBufferChar(upgrade_buffer, '\n');
5648}
#define OidIsValid(objectId)
Definition: c.h:729
static int nbinaryUpgradeClassOids
Definition: pg_dump.c:204
static BinaryUpgradeClassOidItem * binaryUpgradeClassOids
Definition: pg_dump.c:203
static int BinaryUpgradeClassOidItemCmp(const void *p1, const void *p2)
Definition: pg_dump.c:5518
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
RelFileNumber toast_index_relfilenumber
Definition: pg_dump.c:102
RelFileNumber toast_relfilenumber
Definition: pg_dump.c:100
RelFileNumber relfilenumber
Definition: pg_dump.c:98

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), Assert, BinaryUpgradeClassOidItemCmp(), binaryUpgradeClassOids, sort-test::key, nbinaryUpgradeClassOids, OidIsValid, BinaryUpgradeClassOidItem::relfilenumber, RelFileNumberIsValid, BinaryUpgradeClassOidItem::relkind, BinaryUpgradeClassOidItem::toast_index_oid, BinaryUpgradeClassOidItem::toast_index_relfilenumber, BinaryUpgradeClassOidItem::toast_oid, and BinaryUpgradeClassOidItem::toast_relfilenumber.

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

5506{
5507 Oid pg_type_oid = tbinfo->reltype;
5508
5509 if (OidIsValid(pg_type_oid))
5510 binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
5511 pg_type_oid, false, false);
5512}
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:5423
unsigned int Oid
Definition: postgres_ext.h:31
Oid reltype
Definition: pg_dump.h:318

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

5428{
5429 PQExpBuffer upgrade_query = createPQExpBuffer();
5430 PGresult *res;
5431 Oid pg_type_array_oid;
5432 Oid pg_type_multirange_oid;
5433 Oid pg_type_multirange_array_oid;
5434 TypeInfo *tinfo;
5435
5436 appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
5437 appendPQExpBuffer(upgrade_buffer,
5438 "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5439 pg_type_oid);
5440
5441 tinfo = findTypeByOid(pg_type_oid);
5442 if (tinfo)
5443 pg_type_array_oid = tinfo->typarray;
5444 else
5445 pg_type_array_oid = InvalidOid;
5446
5447 if (!OidIsValid(pg_type_array_oid) && force_array_type)
5448 pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5449
5450 if (OidIsValid(pg_type_array_oid))
5451 {
5452 appendPQExpBufferStr(upgrade_buffer,
5453 "\n-- For binary upgrade, must preserve pg_type array oid\n");
5454 appendPQExpBuffer(upgrade_buffer,
5455 "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5456 pg_type_array_oid);
5457 }
5458
5459 /*
5460 * Pre-set the multirange type oid and its own array type oid.
5461 */
5462 if (include_multirange_type)
5463 {
5464 if (fout->remoteVersion >= 140000)
5465 {
5466 printfPQExpBuffer(upgrade_query,
5467 "SELECT t.oid, t.typarray "
5468 "FROM pg_catalog.pg_type t "
5469 "JOIN pg_catalog.pg_range r "
5470 "ON t.oid = r.rngmultitypid "
5471 "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
5472 pg_type_oid);
5473
5474 res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
5475
5476 pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
5477 pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
5478
5479 PQclear(res);
5480 }
5481 else
5482 {
5483 pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5484 pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
5485 }
5486
5487 appendPQExpBufferStr(upgrade_buffer,
5488 "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
5489 appendPQExpBuffer(upgrade_buffer,
5490 "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5491 pg_type_multirange_oid);
5492 appendPQExpBufferStr(upgrade_buffer,
5493 "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
5494 appendPQExpBuffer(upgrade_buffer,
5495 "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
5496 pg_type_multirange_array_oid);
5497 }
5498
5499 destroyPQExpBuffer(upgrade_query);
5500}
TypeInfo * findTypeByOid(Oid oid)
Definition: common.c:876
static const gbtree_vinfo tinfo
Definition: btree_bit.c:108
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:303
static Oid get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
Definition: pg_dump.c:5392
#define InvalidOid
Definition: postgres_ext.h:36
#define atooid(x)
Definition: postgres_ext.h:42
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
int remoteVersion
Definition: pg_backup.h:224

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

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

◆ BinaryUpgradeClassOidItemCmp()

static int BinaryUpgradeClassOidItemCmp ( const void *  p1,
const void *  p2 
)
static

Definition at line 5518 of file pg_dump.c.

5519{
5522
5523 return pg_cmp_u32(v1.oid, v2.oid);
5524}
static int pg_cmp_u32(uint32 a, uint32 b)
Definition: int.h:652

References BinaryUpgradeClassOidItem::oid, p2, and pg_cmp_u32().

Referenced by binary_upgrade_set_pg_class_oids().

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

Definition at line 19002 of file pg_dump.c.

19003{
19004 ArchiveHandle *AH = (ArchiveHandle *) fout;
19005 TocEntry *te;
19006
19007 /* Scan all TOC entries in the archive */
19008 for (te = AH->toc->next; te != AH->toc; te = te->next)
19009 {
19010 DumpableObject *dobj;
19011 DumpId *dependencies;
19012 int nDeps;
19013 int allocDeps;
19014
19015 /* No need to process entries that will not be dumped */
19016 if (te->reqs == 0)
19017 continue;
19018 /* Ignore entries that already have "special" dependencies */
19019 if (te->nDeps > 0)
19020 continue;
19021 /* Otherwise, look up the item's original DumpableObject, if any */
19022 dobj = findObjectByDumpId(te->dumpId);
19023 if (dobj == NULL)
19024 continue;
19025 /* No work if it has no dependencies */
19026 if (dobj->nDeps <= 0)
19027 continue;
19028 /* Set up work array */
19029 allocDeps = 64;
19030 dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
19031 nDeps = 0;
19032 /* Recursively find all dumpable dependencies */
19033 findDumpableDependencies(AH, dobj,
19034 &dependencies, &nDeps, &allocDeps);
19035 /* And save 'em ... */
19036 if (nDeps > 0)
19037 {
19038 dependencies = (DumpId *) pg_realloc(dependencies,
19039 nDeps * sizeof(DumpId));
19040 te->dependencies = dependencies;
19041 te->nDeps = nDeps;
19042 }
19043 else
19044 free(dependencies);
19045 }
19046}
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define free(a)
Definition: header.h:65
int DumpId
Definition: pg_backup.h:275
static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:19050
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 2935 of file pg_dump.c.

2936{
2937 PQExpBuffer query;
2938 PGresult *res;
2939 int ntups,
2940 i;
2941 int i_classid,
2942 i_objid,
2943 i_refobjid;
2944
2945 /* No Mat Views before 9.3. */
2946 if (fout->remoteVersion < 90300)
2947 return;
2948
2949 query = createPQExpBuffer();
2950
2951 appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2952 "( "
2953 "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2954 "FROM pg_depend d1 "
2955 "JOIN pg_class c1 ON c1.oid = d1.objid "
2956 "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2957 " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2958 "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2959 "AND d2.objid = r1.oid "
2960 "AND d2.refobjid <> d1.objid "
2961 "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2962 "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2963 CppAsString2(RELKIND_VIEW) ") "
2964 "WHERE d1.classid = 'pg_class'::regclass "
2965 "UNION "
2966 "SELECT w.objid, d3.refobjid, c3.relkind "
2967 "FROM w "
2968 "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2969 "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2970 "AND d3.objid = r3.oid "
2971 "AND d3.refobjid <> w.refobjid "
2972 "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2973 "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2974 CppAsString2(RELKIND_VIEW) ") "
2975 ") "
2976 "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2977 "FROM w "
2978 "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2979
2980 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2981
2982 ntups = PQntuples(res);
2983
2984 i_classid = PQfnumber(res, "classid");
2985 i_objid = PQfnumber(res, "objid");
2986 i_refobjid = PQfnumber(res, "refobjid");
2987
2988 for (i = 0; i < ntups; i++)
2989 {
2990 CatalogId objId;
2991 CatalogId refobjId;
2992 DumpableObject *dobj;
2993 DumpableObject *refdobj;
2994 TableInfo *tbinfo;
2995 TableInfo *reftbinfo;
2996
2997 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2998 objId.oid = atooid(PQgetvalue(res, i, i_objid));
2999 refobjId.tableoid = objId.tableoid;
3000 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
3001
3002 dobj = findObjectByCatalogId(objId);
3003 if (dobj == NULL)
3004 continue;
3005
3006 Assert(dobj->objType == DO_TABLE);
3007 tbinfo = (TableInfo *) dobj;
3008 Assert(tbinfo->relkind == RELKIND_MATVIEW);
3009 dobj = (DumpableObject *) tbinfo->dataObj;
3010 if (dobj == NULL)
3011 continue;
3013
3014 refdobj = findObjectByCatalogId(refobjId);
3015 if (refdobj == NULL)
3016 continue;
3017
3018 Assert(refdobj->objType == DO_TABLE);
3019 reftbinfo = (TableInfo *) refdobj;
3020 Assert(reftbinfo->relkind == RELKIND_MATVIEW);
3021 refdobj = (DumpableObject *) reftbinfo->dataObj;
3022 if (refdobj == NULL)
3023 continue;
3024 Assert(refdobj->objType == DO_REFRESH_MATVIEW);
3025
3026 addObjectDependency(dobj, refdobj->dumpId);
3027
3028 if (!reftbinfo->relispopulated)
3029 tbinfo->relispopulated = false;
3030 }
3031
3032 PQclear(res);
3033
3034 destroyPQExpBuffer(query);
3035}
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:755
#define CppAsString2(x)
Definition: c.h:346
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
Oid tableoid
Definition: pg_backup.h:271
bool relispopulated
Definition: pg_dump.h:299
struct _tableDataInfo * dataObj
Definition: pg_dump.h:374
char relkind
Definition: pg_dump.h:297

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

1781{
1783
1784 if (ext == NULL)
1785 return false;
1786
1787 dobj->ext_member = true;
1788
1789 /* Record dependency so that getDependencies needn't deal with that */
1790 addObjectDependency(dobj, ext->dobj.dumpId);
1791
1792 /*
1793 * In 9.6 and above, mark the member object to have any non-initial ACLs
1794 * dumped. (Any initial ACLs will be removed later, using data from
1795 * pg_init_privs, so that we'll dump only the delta from the extension's
1796 * initial setup.)
1797 *
1798 * Prior to 9.6, we do not include any extension member components.
1799 *
1800 * In binary upgrades, we still dump all components of the members
1801 * individually, since the idea is to exactly reproduce the database
1802 * contents rather than replace the extension contents with something
1803 * different.
1804 *
1805 * Note: it might be interesting someday to implement storage and delta
1806 * dumping of extension members' RLS policies and/or security labels.
1807 * However there is a pitfall for RLS policies: trying to dump them
1808 * requires getting a lock on their tables, and the calling user might not
1809 * have privileges for that. We need no lock to examine a table's ACLs,
1810 * so the current feature doesn't have a problem of that sort.
1811 */
1812 if (fout->dopt->binary_upgrade)
1813 dobj->dump = ext->dobj.dump;
1814 else
1815 {
1816 if (fout->remoteVersion < 90600)
1817 dobj->dump = DUMP_COMPONENT_NONE;
1818 else
1819 dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL);
1820 }
1821
1822 return true;
1823}
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:1046
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:109
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:104
DumpOptions * dopt
Definition: pg_backup.h:219
int binary_upgrade
Definition: pg_backup.h:169
DumpComponents dump
Definition: pg_dump.h:147
DumpComponents dump_contains
Definition: pg_dump.h:149
DumpableObject dobj
Definition: pg_dump.h:189

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

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

◆ collectBinaryUpgradeClassOids()

static void collectBinaryUpgradeClassOids ( Archive fout)
static

Definition at line 5534 of file pg_dump.c.

5535{
5536 PGresult *res;
5537 const char *query;
5538
5539 query = "SELECT c.oid, c.relkind, c.relfilenode, c.reltoastrelid, "
5540 "ct.relfilenode, i.indexrelid, cti.relfilenode "
5541 "FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index i "
5542 "ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
5543 "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
5544 "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
5545 "ORDER BY c.oid;";
5546
5547 res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
5548
5552
5553 for (int i = 0; i < nbinaryUpgradeClassOids; i++)
5554 {
5562 }
5563
5564 PQclear(res);
5565}

References atooid, binaryUpgradeClassOids, ExecuteSqlQuery(), i, nbinaryUpgradeClassOids, BinaryUpgradeClassOidItem::oid, pg_malloc(), PGRES_TUPLES_OK, PQclear(), PQgetvalue(), PQntuples(), BinaryUpgradeClassOidItem::relfilenumber, BinaryUpgradeClassOidItem::relkind, res, BinaryUpgradeClassOidItem::toast_index_oid, BinaryUpgradeClassOidItem::toast_index_relfilenumber, BinaryUpgradeClassOidItem::toast_oid, and BinaryUpgradeClassOidItem::toast_relfilenumber.

Referenced by main().

◆ collectComments()

static void collectComments ( Archive fout)
static

Definition at line 10481 of file pg_dump.c.

10482{
10483 PGresult *res;
10484 PQExpBuffer query;
10485 int i_description;
10486 int i_classoid;
10487 int i_objoid;
10488 int i_objsubid;
10489 int ntups;
10490 int i;
10491 DumpableObject *dobj;
10492
10493 query = createPQExpBuffer();
10494
10495 appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
10496 "FROM pg_catalog.pg_description "
10497 "ORDER BY classoid, objoid, objsubid");
10498
10499 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10500
10501 /* Construct lookup table containing OIDs in numeric form */
10502
10503 i_description = PQfnumber(res, "description");
10504 i_classoid = PQfnumber(res, "classoid");
10505 i_objoid = PQfnumber(res, "objoid");
10506 i_objsubid = PQfnumber(res, "objsubid");
10507
10508 ntups = PQntuples(res);
10509
10510 comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10511 ncomments = 0;
10512 dobj = NULL;
10513
10514 for (i = 0; i < ntups; i++)
10515 {
10516 CatalogId objId;
10517 int subid;
10518
10519 objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
10520 objId.oid = atooid(PQgetvalue(res, i, i_objoid));
10521 subid = atoi(PQgetvalue(res, i, i_objsubid));
10522
10523 /* We needn't remember comments that don't match any dumpable object */
10524 if (dobj == NULL ||
10525 dobj->catId.tableoid != objId.tableoid ||
10526 dobj->catId.oid != objId.oid)
10527 dobj = findObjectByCatalogId(objId);
10528 if (dobj == NULL)
10529 continue;
10530
10531 /*
10532 * Comments on columns of composite types are linked to the type's
10533 * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag
10534 * in the type's own DumpableObject.
10535 */
10536 if (subid != 0 && dobj->objType == DO_TABLE &&
10537 ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
10538 {
10539 TypeInfo *cTypeInfo;
10540
10541 cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
10542 if (cTypeInfo)
10544 }
10545 else
10546 dobj->components |= DUMP_COMPONENT_COMMENT;
10547
10548 comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description));
10550 comments[ncomments].objoid = objId.oid;
10551 comments[ncomments].objsubid = subid;
10552 ncomments++;
10553 }
10554
10555 PQclear(res);
10556 destroyPQExpBuffer(query);
10557}
static int ncomments
Definition: pg_dump.c:196
static CommentItem * comments
Definition: pg_dump.c:195
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:107
Oid classoid
Definition: pg_dump.c:80
Oid objoid
Definition: pg_dump.c:81
int objsubid
Definition: pg_dump.c:82
const char * descr
Definition: pg_dump.c:79
DumpComponents components
Definition: pg_dump.h:150
DumpableObject dobj
Definition: pg_dump.h:199

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

10027{
10028 PGresult *res;
10029 const char *query;
10030 int i;
10031
10032 query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1";
10033
10034 res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
10035
10037
10039
10040 for (i = 0; i < nrolenames; i++)
10041 {
10044 }
10045
10046 PQclear(res);
10047}
static RoleNameItem * rolenames
Definition: pg_dump.c:191
static int nrolenames
Definition: pg_dump.c:192
const char * rolename
Definition: pg_dump.c:74
Oid roleoid
Definition: pg_dump.c:73

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

Referenced by main().

◆ collectSecLabels()

static void collectSecLabels ( Archive fout)
static

Definition at line 15625 of file pg_dump.c.

15626{
15627 PGresult *res;
15628 PQExpBuffer query;
15629 int i_label;
15630 int i_provider;
15631 int i_classoid;
15632 int i_objoid;
15633 int i_objsubid;
15634 int ntups;
15635 int i;
15636 DumpableObject *dobj;
15637
15638 query = createPQExpBuffer();
15639
15641 "SELECT label, provider, classoid, objoid, objsubid "
15642 "FROM pg_catalog.pg_seclabel "
15643 "ORDER BY classoid, objoid, objsubid");
15644
15645 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15646
15647 /* Construct lookup table containing OIDs in numeric form */
15648 i_label = PQfnumber(res, "label");
15649 i_provider = PQfnumber(res, "provider");
15650 i_classoid = PQfnumber(res, "classoid");
15651 i_objoid = PQfnumber(res, "objoid");
15652 i_objsubid = PQfnumber(res, "objsubid");
15653
15654 ntups = PQntuples(res);
15655
15656 seclabels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15657 nseclabels = 0;
15658 dobj = NULL;
15659
15660 for (i = 0; i < ntups; i++)
15661 {
15662 CatalogId objId;
15663 int subid;
15664
15665 objId.tableoid = atooid(PQgetvalue(res, i, i_classoid));
15666 objId.oid = atooid(PQgetvalue(res, i, i_objoid));
15667 subid = atoi(PQgetvalue(res, i, i_objsubid));
15668
15669 /* We needn't remember labels that don't match any dumpable object */
15670 if (dobj == NULL ||
15671 dobj->catId.tableoid != objId.tableoid ||
15672 dobj->catId.oid != objId.oid)
15673 dobj = findObjectByCatalogId(objId);
15674 if (dobj == NULL)
15675 continue;
15676
15677 /*
15678 * Labels on columns of composite types are linked to the type's
15679 * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag
15680 * in the type's own DumpableObject.
15681 */
15682 if (subid != 0 && dobj->objType == DO_TABLE &&
15683 ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE)
15684 {
15685 TypeInfo *cTypeInfo;
15686
15687 cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype);
15688 if (cTypeInfo)
15690 }
15691 else
15692 dobj->components |= DUMP_COMPONENT_SECLABEL;
15693
15697 seclabels[nseclabels].objoid = objId.oid;
15698 seclabels[nseclabels].objsubid = subid;
15699 nseclabels++;
15700 }
15701
15702 PQclear(res);
15703 destroyPQExpBuffer(query);
15704}
static int nseclabels
Definition: pg_dump.c:200
static SecLabelItem * seclabels
Definition: pg_dump.c:199
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:108
const char * provider
Definition: pg_dump.c:87
Oid classoid
Definition: pg_dump.c:89
int objsubid
Definition: pg_dump.c:91
const char * label
Definition: pg_dump.c:88
Oid objoid
Definition: pg_dump.c:90

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

Referenced by main().

◆ collectSequences()

static void collectSequences ( Archive fout)
static

Definition at line 17665 of file pg_dump.c.

17666{
17667 PGresult *res;
17668 const char *query;
17669
17670 /*
17671 * Before Postgres 10, sequence metadata is in the sequence itself. With
17672 * some extra effort, we might be able to use the sorted table for those
17673 * versions, but for now it seems unlikely to be worth it.
17674 *
17675 * Since version 18, we can gather the sequence data in this query with
17676 * pg_get_sequence_data(), but we only do so for non-schema-only dumps.
17677 */
17678 if (fout->remoteVersion < 100000)
17679 return;
17680 else if (fout->remoteVersion < 180000 ||
17681 (!fout->dopt->dumpData && !fout->dopt->sequence_data))
17682 query = "SELECT seqrelid, format_type(seqtypid, NULL), "
17683 "seqstart, seqincrement, "
17684 "seqmax, seqmin, "
17685 "seqcache, seqcycle, "
17686 "NULL, 'f' "
17687 "FROM pg_catalog.pg_sequence "
17688 "ORDER BY seqrelid";
17689 else
17690 query = "SELECT seqrelid, format_type(seqtypid, NULL), "
17691 "seqstart, seqincrement, "
17692 "seqmax, seqmin, "
17693 "seqcache, seqcycle, "
17694 "last_value, is_called "
17695 "FROM pg_catalog.pg_sequence, "
17696 "pg_get_sequence_data(seqrelid) "
17697 "ORDER BY seqrelid;";
17698
17699 res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
17700
17703
17704 for (int i = 0; i < nsequences; i++)
17705 {
17706 sequences[i].oid = atooid(PQgetvalue(res, i, 0));
17708 sequences[i].startv = strtoi64(PQgetvalue(res, i, 2), NULL, 10);
17709 sequences[i].incby = strtoi64(PQgetvalue(res, i, 3), NULL, 10);
17710 sequences[i].maxv = strtoi64(PQgetvalue(res, i, 4), NULL, 10);
17711 sequences[i].minv = strtoi64(PQgetvalue(res, i, 5), NULL, 10);
17712 sequences[i].cache = strtoi64(PQgetvalue(res, i, 6), NULL, 10);
17713 sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0);
17714 sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10);
17715 sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0);
17716 }
17717
17718 PQclear(res);
17719}
static int nsequences
Definition: pg_dump.c:208
static SeqType parse_sequence_type(const char *name)
Definition: pg_dump.c:17634
static SequenceItem * sequences
Definition: pg_dump.c:207
int64 minv
Definition: pg_dump.c:128
int64 cache
Definition: pg_dump.c:132
int64 startv
Definition: pg_dump.c:130
int64 maxv
Definition: pg_dump.c:129
bool is_called
Definition: pg_dump.c:134
int64 incby
Definition: pg_dump.c:131
int64 last_value
Definition: pg_dump.c:133
SeqType seqtype
Definition: pg_dump.c:126
bool cycled
Definition: pg_dump.c:127
int sequence_data
Definition: pg_backup.h:205
bool dumpData
Definition: pg_backup.h:210

References atooid, SequenceItem::cache, SequenceItem::cycled, Archive::dopt, _dumpOptions::dumpData, ExecuteSqlQuery(), i, SequenceItem::incby, SequenceItem::is_called, SequenceItem::last_value, SequenceItem::maxv, SequenceItem::minv, nsequences, SequenceItem::oid, parse_sequence_type(), pg_malloc(), PGRES_TUPLES_OK, PQclear(), PQgetvalue(), PQntuples(), Archive::remoteVersion, res, SequenceItem::seqtype, _dumpOptions::sequence_data, sequences, and SequenceItem::startv.

Referenced by main().

◆ convertRegProcReference()

static char * convertRegProcReference ( const char *  proc)
static

Definition at line 13175 of file pg_dump.c.

13176{
13177 char *name;
13178 char *paren;
13179 bool inquote;
13180
13181 /* In all cases "-" means a null reference */
13182 if (strcmp(proc, "-") == 0)
13183 return NULL;
13184
13185 name = pg_strdup(proc);
13186 /* find non-double-quoted left paren */
13187 inquote = false;
13188 for (paren = name; *paren; paren++)
13189 {
13190 if (*paren == '(' && !inquote)
13191 {
13192 *paren = '\0';
13193 break;
13194 }
13195 if (*paren == '"')
13196 inquote = !inquote;
13197 }
13198 return name;
13199}
const char * name

References name, and pg_strdup().

Referenced by dumpOpr().

◆ convertTSFunction()

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

Definition at line 13246 of file pg_dump.c.

13247{
13248 char *result;
13249 char query[128];
13250 PGresult *res;
13251
13252 snprintf(query, sizeof(query),
13253 "SELECT '%u'::pg_catalog.regproc", funcOid);
13254 res = ExecuteSqlQueryForSingleRow(fout, query);
13255
13256 result = pg_strdup(PQgetvalue(res, 0, 0));
13257
13258 PQclear(res);
13259
13260 return result;
13261}
#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 18862 of file pg_dump.c.

18863{
18864 DumpableObject *dobjs;
18865
18866 dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18867
18868 dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18869 dobjs[0].catId = nilCatalogId;
18870 AssignDumpId(dobjs + 0);
18871 dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18872
18873 dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18874 dobjs[1].catId = nilCatalogId;
18875 AssignDumpId(dobjs + 1);
18876 dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18877
18878 return dobjs;
18879}
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:634
static const CatalogId nilCatalogId
Definition: pg_dump.c:184

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

15901{
15902 PQExpBuffer result = createPQExpBuffer();
15903 int j;
15904
15905 appendPQExpBufferStr(result, "SELECT");
15906
15907 for (j = 0; j < tbinfo->numatts; j++)
15908 {
15909 if (j > 0)
15910 appendPQExpBufferChar(result, ',');
15911 appendPQExpBufferStr(result, "\n ");
15912
15913 appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15914
15915 /*
15916 * Must add collation if not default for the type, because CREATE OR
15917 * REPLACE VIEW won't change it
15918 */
15919 if (OidIsValid(tbinfo->attcollation[j]))
15920 {
15921 CollInfo *coll;
15922
15923 coll = findCollationByOid(tbinfo->attcollation[j]);
15924 if (coll)
15925 appendPQExpBuffer(result, " COLLATE %s",
15926 fmtQualifiedDumpable(coll));
15927 }
15928
15929 appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15930 }
15931
15932 return result;
15933}
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:931
int j
Definition: isn.c:73
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:226
int numatts
Definition: pg_dump.h:341
Oid * attcollation
Definition: pg_dump.h:354
char ** atttypnames
Definition: pg_dump.h:343
char ** attnames
Definition: pg_dump.h:342

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

15852{
15854 PQExpBuffer result = createPQExpBuffer();
15855 PGresult *res;
15856 int len;
15857
15858 /* Fetch the view definition */
15859 appendPQExpBuffer(query,
15860 "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15861 tbinfo->dobj.catId.oid);
15862
15863 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15864
15865 if (PQntuples(res) != 1)
15866 {
15867 if (PQntuples(res) < 1)
15868 pg_fatal("query to obtain definition of view \"%s\" returned no data",
15869 tbinfo->dobj.name);
15870 else
15871 pg_fatal("query to obtain definition of view \"%s\" returned more than one definition",
15872 tbinfo->dobj.name);
15873 }
15874
15875 len = PQgetlength(res, 0, 0);
15876
15877 if (len == 0)
15878 pg_fatal("definition of view \"%s\" appears to be empty (length zero)",
15879 tbinfo->dobj.name);
15880
15881 /* Strip off the trailing semicolon so that other things may follow. */
15882 Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15883 appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15884
15885 PQclear(res);
15886 destroyPQExpBuffer(query);
15887
15888 return result;
15889}
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3887
const void size_t len
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:397
DumpableObject dobj
Definition: pg_dump.h:294

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().

◆ determineNotNullFlags()

static void determineNotNullFlags ( Archive fout,
PGresult res,
int  r,
TableInfo tbinfo,
int  j,
int  i_notnull_name,
int  i_notnull_noinherit,
int  i_notnull_islocal 
)
static

Definition at line 9362 of file pg_dump.c.

9366{
9367 DumpOptions *dopt = fout->dopt;
9368
9369 /*
9370 * notnull_noinh is straight from the query result. notnull_islocal also,
9371 * though flagInhAttrs may change that one later in versions < 18.
9372 */
9373 tbinfo->notnull_noinh[j] = PQgetvalue(res, r, i_notnull_noinherit)[0] == 't';
9374 tbinfo->notnull_islocal[j] = PQgetvalue(res, r, i_notnull_islocal)[0] == 't';
9375
9376 /*
9377 * Determine a constraint name to use. If the column is not marked not-
9378 * null, we set NULL which cues ... to do nothing. An empty string says
9379 * to print an unnamed NOT NULL, and anything else is a constraint name to
9380 * use.
9381 */
9382 if (fout->remoteVersion < 180000)
9383 {
9384 /*
9385 * < 18 doesn't have not-null names, so an unnamed constraint is
9386 * sufficient.
9387 */
9388 if (PQgetisnull(res, r, i_notnull_name))
9389 tbinfo->notnull_constrs[j] = NULL;
9390 else
9391 tbinfo->notnull_constrs[j] = "";
9392 }
9393 else
9394 {
9395 if (PQgetisnull(res, r, i_notnull_name))
9396 tbinfo->notnull_constrs[j] = NULL;
9397 else
9398 {
9399 /*
9400 * In binary upgrade of inheritance child tables, must have a
9401 * constraint name that we can UPDATE later.
9402 */
9403 if (dopt->binary_upgrade &&
9404 !tbinfo->ispartition &&
9405 !tbinfo->notnull_islocal)
9406 {
9407 tbinfo->notnull_constrs[j] =
9408 pstrdup(PQgetvalue(res, r, i_notnull_name));
9409 }
9410 else
9411 {
9412 char *default_name;
9413
9414 /* XXX should match ChooseConstraintName better */
9415 default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name,
9416 tbinfo->attnames[j]);
9417 if (strcmp(default_name,
9418 PQgetvalue(res, r, i_notnull_name)) == 0)
9419 tbinfo->notnull_constrs[j] = "";
9420 else
9421 {
9422 tbinfo->notnull_constrs[j] =
9423 pstrdup(PQgetvalue(res, r, i_notnull_name));
9424 }
9425 }
9426 }
9427 }
9428}
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
char * pstrdup(const char *in)
Definition: mcxt.c:1696
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
char ** notnull_constrs
Definition: pg_dump.h:358
bool ispartition
Definition: pg_dump.h:331
bool * notnull_islocal
Definition: pg_dump.h:363
bool * notnull_noinh
Definition: pg_dump.h:362

References _tableInfo::attnames, _dumpOptions::binary_upgrade, _tableInfo::dobj, Archive::dopt, _tableInfo::ispartition, j, _dumpableObject::name, _tableInfo::notnull_constrs, _tableInfo::notnull_islocal, _tableInfo::notnull_noinh, PQgetisnull(), PQgetvalue(), psprintf(), pstrdup(), Archive::remoteVersion, and res.

Referenced by getTableAttrs().

◆ dumpAccessMethod()

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

Definition at line 13268 of file pg_dump.c.

13269{
13270 DumpOptions *dopt = fout->dopt;
13271 PQExpBuffer q;
13272 PQExpBuffer delq;
13273 char *qamname;
13274
13275 /* Do nothing if not dumping schema */
13276 if (!dopt->dumpSchema)
13277 return;
13278
13279 q = createPQExpBuffer();
13280 delq = createPQExpBuffer();
13281
13282 qamname = pg_strdup(fmtId(aminfo->dobj.name));
13283
13284 appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
13285
13286 switch (aminfo->amtype)
13287 {
13288 case AMTYPE_INDEX:
13289 appendPQExpBufferStr(q, "TYPE INDEX ");
13290 break;
13291 case AMTYPE_TABLE:
13292 appendPQExpBufferStr(q, "TYPE TABLE ");
13293 break;
13294 default:
13295 pg_log_warning("invalid type \"%c\" of access method \"%s\"",
13296 aminfo->amtype, qamname);
13298 destroyPQExpBuffer(delq);
13299 free(qamname);
13300 return;
13301 }
13302
13303 appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
13304
13305 appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
13306 qamname);
13307
13308 if (dopt->binary_upgrade)
13310 "ACCESS METHOD", qamname, NULL);
13311
13312 if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13313 ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
13314 ARCHIVE_OPTS(.tag = aminfo->dobj.name,
13315 .description = "ACCESS METHOD",
13316 .section = SECTION_PRE_DATA,
13317 .createStmt = q->data,
13318 .dropStmt = delq->data));
13319
13320 /* Dump Access Method Comments */
13321 if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13322 dumpComment(fout, "ACCESS METHOD", qamname,
13323 NULL, "",
13324 aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
13325
13327 destroyPQExpBuffer(delq);
13328 free(qamname);
13329}
@ SECTION_PRE_DATA
Definition: pg_backup.h:58
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
#define ARCHIVE_OPTS(...)
static void dumpComment(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:10290
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:5658
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:105
char * amhandler
Definition: pg_dump.h:262
DumpableObject dobj
Definition: pg_dump.h:260
bool dumpSchema
Definition: pg_backup.h:209

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

Referenced by dumpDumpableObject().

◆ dumpACL()

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

Definition at line 15256 of file pg_dump.c.

15260{
15261 DumpId aclDumpId = InvalidDumpId;
15262 DumpOptions *dopt = fout->dopt;
15263 const char *acls = dacl->acl;
15264 const char *acldefault = dacl->acldefault;
15265 char privtype = dacl->privtype;
15266 const char *initprivs = dacl->initprivs;
15267 const char *baseacls;
15268 PQExpBuffer sql;
15269
15270 /* Do nothing if ACL dump is not enabled */
15271 if (dopt->aclsSkip)
15272 return InvalidDumpId;
15273
15274 /* --data-only skips ACLs *except* large object ACLs */
15275 if (!dopt->dumpSchema && strcmp(type, "LARGE OBJECT") != 0)
15276 return InvalidDumpId;
15277
15278 sql = createPQExpBuffer();
15279
15280 /*
15281 * In binary upgrade mode, we don't run an extension's script but instead
15282 * dump out the objects independently and then recreate them. To preserve
15283 * any initial privileges which were set on extension objects, we need to
15284 * compute the set of GRANT and REVOKE commands necessary to get from the
15285 * default privileges of an object to its initial privileges as recorded
15286 * in pg_init_privs.
15287 *
15288 * At restore time, we apply these commands after having called
15289 * binary_upgrade_set_record_init_privs(true). That tells the backend to
15290 * copy the results into pg_init_privs. This is how we preserve the
15291 * contents of that catalog across binary upgrades.
15292 */
15293 if (dopt->binary_upgrade && privtype == 'e' &&
15294 initprivs && *initprivs != '\0')
15295 {
15296 appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
15297 if (!buildACLCommands(name, subname, nspname, type,
15298 initprivs, acldefault, owner,
15299 "", fout->remoteVersion, sql))
15300 pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)",
15301 initprivs, acldefault, name, type);
15302 appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
15303 }
15304
15305 /*
15306 * Now figure the GRANT and REVOKE commands needed to get to the object's
15307 * actual current ACL, starting from the initprivs if given, else from the
15308 * object-type-specific default. Also, while buildACLCommands will assume
15309 * that a NULL/empty acls string means it needn't do anything, what that
15310 * actually represents is the object-type-specific default; so we need to
15311 * substitute the acldefault string to get the right results in that case.
15312 */
15313 if (initprivs && *initprivs != '\0')
15314 {
15315 baseacls = initprivs;
15316 if (acls == NULL || *acls == '\0')
15317 acls = acldefault;
15318 }
15319 else
15320 baseacls = acldefault;
15321
15322 if (!buildACLCommands(name, subname, nspname, type,
15323 acls, baseacls, owner,
15324 "", fout->remoteVersion, sql))
15325 pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)",
15326 acls, baseacls, name, type);
15327
15328 if (sql->len > 0)
15329 {
15330 PQExpBuffer tagbuf = createPQExpBuffer();
15331 DumpId aclDeps[2];
15332 int nDeps = 0;
15333
15334 if (tag)
15335 appendPQExpBufferStr(tagbuf, tag);
15336 else if (subname)
15337 appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname);
15338 else
15339 appendPQExpBuffer(tagbuf, "%s %s", type, name);
15340
15341 aclDeps[nDeps++] = objDumpId;
15342 if (altDumpId != InvalidDumpId)
15343 aclDeps[nDeps++] = altDumpId;
15344
15345 aclDumpId = createDumpId();
15346
15347 ArchiveEntry(fout, nilCatalogId, aclDumpId,
15348 ARCHIVE_OPTS(.tag = tagbuf->data,
15349 .namespace = nspname,
15350 .owner = owner,
15351 .description = "ACL",
15352 .section = SECTION_NONE,
15353 .createStmt = sql->data,
15354 .deps = aclDeps,
15355 .nDeps = nDeps));
15356
15357 destroyPQExpBuffer(tagbuf);
15358 }
15359
15360 destroyPQExpBuffer(sql);
15361
15362 return aclDumpId;
15363}
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:787
DumpId createDumpId(void)
Definition: common.c:722
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:64
@ SECTION_NONE
Definition: pg_backup.h:57
#define InvalidDumpId
Definition: pg_backup.h:277
NameData subname
bool aclsSkip
Definition: pg_backup.h:173
char privtype
Definition: pg_dump.h:167
char * acldefault
Definition: pg_dump.h:165
char * acl
Definition: pg_dump.h:164
char * initprivs
Definition: pg_dump.h:168
const char * type

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

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

◆ dumpAgg()

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

Definition at line 14221 of file pg_dump.c.

14222{
14223 DumpOptions *dopt = fout->dopt;
14224 PQExpBuffer query;
14225 PQExpBuffer q;
14226 PQExpBuffer delq;
14227 PQExpBuffer details;
14228 char *aggsig; /* identity signature */
14229 char *aggfullsig = NULL; /* full signature */
14230 char *aggsig_tag;
14231 PGresult *res;
14232 int i_agginitval;
14233 int i_aggminitval;
14234 const char *aggtransfn;
14235 const char *aggfinalfn;
14236 const char *aggcombinefn;
14237 const char *aggserialfn;
14238 const char *aggdeserialfn;
14239 const char *aggmtransfn;
14240 const char *aggminvtransfn;
14241 const char *aggmfinalfn;
14242 bool aggfinalextra;
14243 bool aggmfinalextra;
14244 char aggfinalmodify;
14245 char aggmfinalmodify;
14246 const char *aggsortop;
14247 char *aggsortconvop;
14248 char aggkind;
14249 const char *aggtranstype;
14250 const char *aggtransspace;
14251 const char *aggmtranstype;
14252 const char *aggmtransspace;
14253 const char *agginitval;
14254 const char *aggminitval;
14255 const char *proparallel;
14256 char defaultfinalmodify;
14257
14258 /* Do nothing if not dumping schema */
14259 if (!dopt->dumpSchema)
14260 return;
14261
14262 query = createPQExpBuffer();
14263 q = createPQExpBuffer();
14264 delq = createPQExpBuffer();
14265 details = createPQExpBuffer();
14266
14267 if (!fout->is_prepared[PREPQUERY_DUMPAGG])
14268 {
14269 /* Set up query for aggregate-specific details */
14271 "PREPARE dumpAgg(pg_catalog.oid) AS\n");
14272
14274 "SELECT "
14275 "aggtransfn,\n"
14276 "aggfinalfn,\n"
14277 "aggtranstype::pg_catalog.regtype,\n"
14278 "agginitval,\n"
14279 "aggsortop,\n"
14280 "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
14281 "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
14282
14283 if (fout->remoteVersion >= 90400)
14285 "aggkind,\n"
14286 "aggmtransfn,\n"
14287 "aggminvtransfn,\n"
14288 "aggmfinalfn,\n"
14289 "aggmtranstype::pg_catalog.regtype,\n"
14290 "aggfinalextra,\n"
14291 "aggmfinalextra,\n"
14292 "aggtransspace,\n"
14293 "aggmtransspace,\n"
14294 "aggminitval,\n");
14295 else
14297 "'n' AS aggkind,\n"
14298 "'-' AS aggmtransfn,\n"
14299 "'-' AS aggminvtransfn,\n"
14300 "'-' AS aggmfinalfn,\n"
14301 "0 AS aggmtranstype,\n"
14302 "false AS aggfinalextra,\n"
14303 "false AS aggmfinalextra,\n"
14304 "0 AS aggtransspace,\n"
14305 "0 AS aggmtransspace,\n"
14306 "NULL AS aggminitval,\n");
14307
14308 if (fout->remoteVersion >= 90600)
14310 "aggcombinefn,\n"
14311 "aggserialfn,\n"
14312 "aggdeserialfn,\n"
14313 "proparallel,\n");
14314 else
14316 "'-' AS aggcombinefn,\n"
14317 "'-' AS aggserialfn,\n"
14318 "'-' AS aggdeserialfn,\n"
14319 "'u' AS proparallel,\n");
14320
14321 if (fout->remoteVersion >= 110000)
14323 "aggfinalmodify,\n"
14324 "aggmfinalmodify\n");
14325 else
14327 "'0' AS aggfinalmodify,\n"
14328 "'0' AS aggmfinalmodify\n");
14329
14331 "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14332 "WHERE a.aggfnoid = p.oid "
14333 "AND p.oid = $1");
14334
14335 ExecuteSqlStatement(fout, query->data);
14336
14337 fout->is_prepared[PREPQUERY_DUMPAGG] = true;
14338 }
14339
14340 printfPQExpBuffer(query,
14341 "EXECUTE dumpAgg('%u')",
14342 agginfo->aggfn.dobj.catId.oid);
14343
14344 res = ExecuteSqlQueryForSingleRow(fout, query->data);
14345
14346 i_agginitval = PQfnumber(res, "agginitval");
14347 i_aggminitval = PQfnumber(res, "aggminitval");
14348
14349 aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
14350 aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
14351 aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
14352 aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
14353 aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
14354 aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
14355 aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
14356 aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
14357 aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
14358 aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
14359 aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
14360 aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
14361 aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
14362 aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
14363 aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
14364 aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
14365 aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
14366 aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
14367 agginitval = PQgetvalue(res, 0, i_agginitval);
14368 aggminitval = PQgetvalue(res, 0, i_aggminitval);
14369 proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
14370
14371 {
14372 char *funcargs;
14373 char *funciargs;
14374
14375 funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
14376 funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
14377 aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
14378 aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
14379 }
14380
14381 aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14382
14383 /* identify default modify flag for aggkind (must match DefineAggregate) */
14384 defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14385 /* replace omitted flags for old versions */
14386 if (aggfinalmodify == '0')
14387 aggfinalmodify = defaultfinalmodify;
14388 if (aggmfinalmodify == '0')
14389 aggmfinalmodify = defaultfinalmodify;
14390
14391 /* regproc and regtype output is already sufficiently quoted */
14392 appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14393 aggtransfn, aggtranstype);
14394
14395 if (strcmp(aggtransspace, "0") != 0)
14396 {
14397 appendPQExpBuffer(details, ",\n SSPACE = %s",
14398 aggtransspace);
14399 }
14400
14401 if (!PQgetisnull(res, 0, i_agginitval))
14402 {
14403 appendPQExpBufferStr(details, ",\n INITCOND = ");
14404 appendStringLiteralAH(details, agginitval, fout);
14405 }
14406
14407 if (strcmp(aggfinalfn, "-") != 0)
14408 {
14409 appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14410 aggfinalfn);
14411 if (aggfinalextra)
14412 appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14413 if (aggfinalmodify != defaultfinalmodify)
14414 {
14415 switch (aggfinalmodify)
14416 {
14417 case AGGMODIFY_READ_ONLY:
14418 appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14419 break;
14420 case AGGMODIFY_SHAREABLE:
14421 appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14422 break;
14423 case AGGMODIFY_READ_WRITE:
14424 appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14425 break;
14426 default:
14427 pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14428 agginfo->aggfn.dobj.name);
14429 break;
14430 }
14431 }
14432 }
14433
14434 if (strcmp(aggcombinefn, "-") != 0)
14435 appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14436
14437 if (strcmp(aggserialfn, "-") != 0)
14438 appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14439
14440 if (strcmp(aggdeserialfn, "-") != 0)
14441 appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14442
14443 if (strcmp(aggmtransfn, "-") != 0)
14444 {
14445 appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14446 aggmtransfn,
14447 aggminvtransfn,
14448 aggmtranstype);
14449 }
14450
14451 if (strcmp(aggmtransspace, "0") != 0)
14452 {
14453 appendPQExpBuffer(details, ",\n MSSPACE = %s",
14454 aggmtransspace);
14455 }
14456
14457 if (!PQgetisnull(res, 0, i_aggminitval))
14458 {
14459 appendPQExpBufferStr(details, ",\n MINITCOND = ");
14460 appendStringLiteralAH(details, aggminitval, fout);
14461 }
14462
14463 if (strcmp(aggmfinalfn, "-") != 0)
14464 {
14465 appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14466 aggmfinalfn);
14467 if (aggmfinalextra)
14468 appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14469 if (aggmfinalmodify != defaultfinalmodify)
14470 {
14471 switch (aggmfinalmodify)
14472 {
14473 case AGGMODIFY_READ_ONLY:
14474 appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14475 break;
14476 case AGGMODIFY_SHAREABLE:
14477 appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14478 break;
14479 case AGGMODIFY_READ_WRITE:
14480 appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14481 break;
14482 default:
14483 pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14484 agginfo->aggfn.dobj.name);
14485 break;
14486 }
14487 }
14488 }
14489
14490 aggsortconvop = getFormattedOperatorName(aggsortop);
14491 if (aggsortconvop)
14492 {
14493 appendPQExpBuffer(details, ",\n SORTOP = %s",
14494 aggsortconvop);
14495 free(aggsortconvop);
14496 }
14497
14498 if (aggkind == AGGKIND_HYPOTHETICAL)
14499 appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14500
14501 if (proparallel[0] != PROPARALLEL_UNSAFE)
14502 {
14503 if (proparallel[0] == PROPARALLEL_SAFE)
14504 appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14505 else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14506 appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14507 else if (proparallel[0] != PROPARALLEL_UNSAFE)
14508 pg_fatal("unrecognized proparallel value for function \"%s\"",
14509 agginfo->aggfn.dobj.name);
14510 }
14511
14512 appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14513 fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14514 aggsig);
14515
14516 appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14517 fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14518 aggfullsig ? aggfullsig : aggsig, details->data);
14519
14520 if (dopt->binary_upgrade)
14521 binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
14522 "AGGREGATE", aggsig,
14523 agginfo->aggfn.dobj.namespace->dobj.name);
14524
14525 if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14526 ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14527 agginfo->aggfn.dobj.dumpId,
14528 ARCHIVE_OPTS(.tag = aggsig_tag,
14529 .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14530 .owner = agginfo->aggfn.rolname,
14531 .description = "AGGREGATE",
14532 .section = SECTION_PRE_DATA,
14533 .createStmt = q->data,
14534 .dropStmt = delq->data));
14535
14536 /* Dump Aggregate Comments */
14537 if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14538 dumpComment(fout, "AGGREGATE", aggsig,
14539 agginfo->aggfn.dobj.namespace->dobj.name,
14540 agginfo->aggfn.rolname,
14541 agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14542
14543 if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14544 dumpSecLabel(fout, "AGGREGATE", aggsig,
14545 agginfo->aggfn.dobj.namespace->dobj.name,
14546 agginfo->aggfn.rolname,
14547 agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14548
14549 /*
14550 * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14551 * command look like a function's GRANT; in particular this affects the
14552 * syntax for zero-argument aggregates and ordered-set aggregates.
14553 */
14554 free(aggsig);
14555
14556 aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14557
14558 if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14559 dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
14560 "FUNCTION", aggsig, NULL,
14561 agginfo->aggfn.dobj.namespace->dobj.name,
14562 NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl);
14563
14564 free(aggsig);
14565 free(aggfullsig);
14566 free(aggsig_tag);
14567
14568 PQclear(res);
14569
14570 destroyPQExpBuffer(query);
14572 destroyPQExpBuffer(delq);
14573 destroyPQExpBuffer(details);
14574}
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:337
@ PREPQUERY_DUMPAGG
Definition: pg_backup.h:66
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:276
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *tag, const char *owner, const DumpableAcl *dacl)
Definition: pg_dump.c:15256
static char * getFormattedOperatorName(const char *oproid)
Definition: pg_dump.c:13216
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:12275
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:12252
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:15384
static char * format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:14189
bool * is_prepared
Definition: pg_backup.h:246

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), Archive::dopt, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), _dumpOptions::dumpSchema, 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 16975 of file pg_dump.c.

16976{
16977 DumpOptions *dopt = fout->dopt;
16978 TableInfo *tbinfo = adinfo->adtable;
16979 int adnum = adinfo->adnum;
16980 PQExpBuffer q;
16981 PQExpBuffer delq;
16982 char *qualrelname;
16983 char *tag;
16984 char *foreign;
16985
16986 /* Do nothing if not dumping schema */
16987 if (!dopt->dumpSchema)
16988 return;
16989
16990 /* Skip if not "separate"; it was dumped in the table's definition */
16991 if (!adinfo->separate)
16992 return;
16993
16994 q = createPQExpBuffer();
16995 delq = createPQExpBuffer();
16996
16997 qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16998
16999 foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
17000
17002 "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
17003 foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
17004 adinfo->adef_expr);
17005
17006 appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
17007 foreign, qualrelname,
17008 fmtId(tbinfo->attnames[adnum - 1]));
17009
17010 tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
17011
17012 if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17013 ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
17014 ARCHIVE_OPTS(.tag = tag,
17015 .namespace = tbinfo->dobj.namespace->dobj.name,
17016 .owner = tbinfo->rolname,
17017 .description = "DEFAULT",
17018 .section = SECTION_PRE_DATA,
17019 .createStmt = q->data,
17020 .dropStmt = delq->data));
17021
17022 free(tag);
17024 destroyPQExpBuffer(delq);
17025 free(qualrelname);
17026}
DumpableObject dobj
Definition: pg_dump.h:388
char * adef_expr
Definition: pg_dump.h:391
TableInfo * adtable
Definition: pg_dump.h:389
bool separate
Definition: pg_dump.h:392
const char * rolname
Definition: pg_dump.h:296

References _attrDefInfo::adef_expr, _attrDefInfo::adnum, _attrDefInfo::adtable, appendPQExpBuffer(), ARCHIVE_OPTS, ArchiveEntry(), _tableInfo::attnames, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _tableInfo::dobj, _attrDefInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_DEFINITION, _dumpableObject::dumpId, _dumpOptions::dumpSchema, 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 11357 of file pg_dump.c.

11358{
11359 DumpOptions *dopt = fout->dopt;
11363 PGresult *res;
11364 char *qtypname;
11365 char *qualtypname;
11366 char *typlen;
11367 char *typinput;
11368 char *typoutput;
11369 char *typreceive;
11370 char *typsend;
11371 char *typmodin;
11372 char *typmodout;
11373 char *typanalyze;
11374 char *typsubscript;
11375 Oid typreceiveoid;
11376 Oid typsendoid;
11377 Oid typmodinoid;
11378 Oid typmodoutoid;
11379 Oid typanalyzeoid;
11380 Oid typsubscriptoid;
11381 char *typcategory;
11382 char *typispreferred;
11383 char *typdelim;
11384 char *typbyval;
11385 char *typalign;
11386 char *typstorage;
11387 char *typcollatable;
11388 char *typdefault;
11389 bool typdefault_is_literal = false;
11390
11392 {
11393 /* Set up query for type-specific details */
11395 "PREPARE dumpBaseType(pg_catalog.oid) AS\n"
11396 "SELECT typlen, "
11397 "typinput, typoutput, typreceive, typsend, "
11398 "typreceive::pg_catalog.oid AS typreceiveoid, "
11399 "typsend::pg_catalog.oid AS typsendoid, "
11400 "typanalyze, "
11401 "typanalyze::pg_catalog.oid AS typanalyzeoid, "
11402 "typdelim, typbyval, typalign, typstorage, "
11403 "typmodin, typmodout, "
11404 "typmodin::pg_catalog.oid AS typmodinoid, "
11405 "typmodout::pg_catalog.oid AS typmodoutoid, "
11406 "typcategory, typispreferred, "
11407 "(typcollation <> 0) AS typcollatable, "
11408 "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, ");
11409
11410 if (fout->remoteVersion >= 140000)
11412 "typsubscript, "
11413 "typsubscript::pg_catalog.oid AS typsubscriptoid ");
11414 else
11416 "'-' AS typsubscript, 0 AS typsubscriptoid ");
11417
11418 appendPQExpBufferStr(query, "FROM pg_catalog.pg_type "
11419 "WHERE oid = $1");
11420
11421 ExecuteSqlStatement(fout, query->data);
11422
11423 fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true;
11424 }
11425
11426 printfPQExpBuffer(query,
11427 "EXECUTE dumpBaseType('%u')",
11428 tyinfo->dobj.catId.oid);
11429
11430 res = ExecuteSqlQueryForSingleRow(fout, query->data);
11431
11432 typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
11433 typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
11434 typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
11435 typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
11436 typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
11437 typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
11438 typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
11439 typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
11440 typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
11441 typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
11442 typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
11443 typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
11444 typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
11445 typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
11446 typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
11447 typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
11448 typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
11449 typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
11450 typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
11451 typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
11452 typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
11453 typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
11454 if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
11455 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
11456 else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
11457 {
11458 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
11459 typdefault_is_literal = true; /* it needs quotes */
11460 }
11461 else
11462 typdefault = NULL;
11463
11464 qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11465 qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11466
11467 /*
11468 * The reason we include CASCADE is that the circular dependency between
11469 * the type and its I/O functions makes it impossible to drop the type any
11470 * other way.
11471 */
11472 appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
11473
11474 /*
11475 * We might already have a shell type, but setting pg_type_oid is
11476 * harmless, and in any case we'd better set the array type OID.
11477 */
11478 if (dopt->binary_upgrade)
11480 tyinfo->dobj.catId.oid,
11481 false, false);
11482
11484 "CREATE TYPE %s (\n"
11485 " INTERNALLENGTH = %s",
11486 qualtypname,
11487 (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
11488
11489 /* regproc result is sufficiently quoted already */
11490 appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
11491 appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
11492 if (OidIsValid(typreceiveoid))
11493 appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
11494 if (OidIsValid(typsendoid))
11495 appendPQExpBuffer(q, ",\n SEND = %s", typsend);
11496 if (OidIsValid(typmodinoid))
11497 appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
11498 if (OidIsValid(typmodoutoid))
11499 appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
11500 if (OidIsValid(typanalyzeoid))
11501 appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
11502
11503 if (strcmp(typcollatable, "t") == 0)
11504 appendPQExpBufferStr(q, ",\n COLLATABLE = true");
11505
11506 if (typdefault != NULL)
11507 {
11508 appendPQExpBufferStr(q, ",\n DEFAULT = ");
11509 if (typdefault_is_literal)
11510 appendStringLiteralAH(q, typdefault, fout);
11511 else
11512 appendPQExpBufferStr(q, typdefault);
11513 }
11514
11515 if (OidIsValid(typsubscriptoid))
11516 appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
11517
11518 if (OidIsValid(tyinfo->typelem))
11519 appendPQExpBuffer(q, ",\n ELEMENT = %s",
11520 getFormattedTypeName(fout, tyinfo->typelem,
11521 zeroIsError));
11522
11523 if (strcmp(typcategory, "U") != 0)
11524 {
11525 appendPQExpBufferStr(q, ",\n CATEGORY = ");
11526 appendStringLiteralAH(q, typcategory, fout);
11527 }
11528
11529 if (strcmp(typispreferred, "t") == 0)
11530 appendPQExpBufferStr(q, ",\n PREFERRED = true");
11531
11532 if (typdelim && strcmp(typdelim, ",") != 0)
11533 {
11534 appendPQExpBufferStr(q, ",\n DELIMITER = ");
11535 appendStringLiteralAH(q, typdelim, fout);
11536 }
11537
11538 if (*typalign == TYPALIGN_CHAR)
11539 appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
11540 else if (*typalign == TYPALIGN_SHORT)
11541 appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
11542 else if (*typalign == TYPALIGN_INT)
11543 appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
11544 else if (*typalign == TYPALIGN_DOUBLE)
11545 appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
11546
11547 if (*typstorage == TYPSTORAGE_PLAIN)
11548 appendPQExpBufferStr(q, ",\n STORAGE = plain");
11549 else if (*typstorage == TYPSTORAGE_EXTERNAL)
11550 appendPQExpBufferStr(q, ",\n STORAGE = external");
11551 else if (*typstorage == TYPSTORAGE_EXTENDED)
11552 appendPQExpBufferStr(q, ",\n STORAGE = extended");
11553 else if (*typstorage == TYPSTORAGE_MAIN)
11554 appendPQExpBufferStr(q, ",\n STORAGE = main");
11555
11556 if (strcmp(typbyval, "t") == 0)
11557 appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
11558
11559 appendPQExpBufferStr(q, "\n);\n");
11560
11561 if (dopt->binary_upgrade)
11563 "TYPE", qtypname,
11564 tyinfo->dobj.namespace->dobj.name);
11565
11566 if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11567 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11568 ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11569 .namespace = tyinfo->dobj.namespace->dobj.name,
11570 .owner = tyinfo->rolname,
11571 .description = "TYPE",
11572 .section = SECTION_PRE_DATA,
11573 .createStmt = q->data,
11574 .dropStmt = delq->data));
11575
11576 /* Dump Type Comments and Security Labels */
11577 if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11578 dumpComment(fout, "TYPE", qtypname,
11579 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11580 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11581
11582 if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11583 dumpSecLabel(fout, "TYPE", qtypname,
11584 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11585 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11586
11587 if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11588 dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11589 qtypname, NULL,
11590 tyinfo->dobj.namespace->dobj.name,
11591 NULL, tyinfo->rolname, &tyinfo->dacl);
11592
11593 PQclear(res);
11595 destroyPQExpBuffer(delq);
11596 destroyPQExpBuffer(query);
11597 free(qtypname);
11598 free(qualtypname);
11599}
@ PREPQUERY_DUMPBASETYPE
Definition: pg_backup.h:67
static const char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:19106
char typalign
Definition: pg_type.h:176
DumpableAcl dacl
Definition: pg_dump.h:200
Oid typelem
Definition: pg_dump.h:209
const char * rolname
Definition: pg_dump.h:208

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

12723{
12724 DumpOptions *dopt = fout->dopt;
12725 PQExpBuffer defqry;
12726 PQExpBuffer delqry;
12727 PQExpBuffer labelq;
12728 PQExpBuffer castargs;
12729 FuncInfo *funcInfo = NULL;
12730 const char *sourceType;
12731 const char *targetType;
12732
12733 /* Do nothing if not dumping schema */
12734 if (!dopt->dumpSchema)
12735 return;
12736
12737 /* Cannot dump if we don't have the cast function's info */
12738 if (OidIsValid(cast->castfunc))
12739 {
12740 funcInfo = findFuncByOid(cast->castfunc);
12741 if (funcInfo == NULL)
12742 pg_fatal("could not find function definition for function with OID %u",
12743 cast->castfunc);
12744 }
12745
12746 defqry = createPQExpBuffer();
12747 delqry = createPQExpBuffer();
12748 labelq = createPQExpBuffer();
12749 castargs = createPQExpBuffer();
12750
12751 sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12752 targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12753 appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12754 sourceType, targetType);
12755
12756 appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12757 sourceType, targetType);
12758
12759 switch (cast->castmethod)
12760 {
12761 case COERCION_METHOD_BINARY:
12762 appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12763 break;
12764 case COERCION_METHOD_INOUT:
12765 appendPQExpBufferStr(defqry, "WITH INOUT");
12766 break;
12767 case COERCION_METHOD_FUNCTION:
12768 if (funcInfo)
12769 {
12770 char *fsig = format_function_signature(fout, funcInfo, true);
12771
12772 /*
12773 * Always qualify the function name (format_function_signature
12774 * won't qualify it).
12775 */
12776 appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12777 fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12778 free(fsig);
12779 }
12780 else
12781 pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12782 break;
12783 default:
12784 pg_log_warning("bogus value in pg_cast.castmethod field");
12785 }
12786
12787 if (cast->castcontext == 'a')
12788 appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12789 else if (cast->castcontext == 'i')
12790 appendPQExpBufferStr(defqry, " AS IMPLICIT");
12791 appendPQExpBufferStr(defqry, ";\n");
12792
12793 appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12794 sourceType, targetType);
12795
12796 appendPQExpBuffer(castargs, "(%s AS %s)",
12797 sourceType, targetType);
12798
12799 if (dopt->binary_upgrade)
12801 "CAST", castargs->data, NULL);
12802
12804 ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12805 ARCHIVE_OPTS(.tag = labelq->data,
12806 .description = "CAST",
12807 .section = SECTION_PRE_DATA,
12808 .createStmt = defqry->data,
12809 .dropStmt = delqry->data));
12810
12811 /* Dump Cast Comments */
12812 if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12813 dumpComment(fout, "CAST", castargs->data,
12814 NULL, "",
12815 cast->dobj.catId, 0, cast->dobj.dumpId);
12816
12817 destroyPQExpBuffer(defqry);
12818 destroyPQExpBuffer(delqry);
12819 destroyPQExpBuffer(labelq);
12820 destroyPQExpBuffer(castargs);
12821}
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:895
char castmethod
Definition: pg_dump.h:513
Oid casttarget
Definition: pg_dump.h:510
char castcontext
Definition: pg_dump.h:512
DumpableObject dobj
Definition: pg_dump.h:508
Oid castsource
Definition: pg_dump.h:509
Oid castfunc
Definition: pg_dump.h:511
DumpableObject dobj
Definition: pg_dump.h:233

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, destroyPQExpBuffer(), _funcInfo::dobj, _castInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, _dumpOptions::dumpSchema, 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 13836 of file pg_dump.c.

13837{
13838 DumpOptions *dopt = fout->dopt;
13839 PQExpBuffer query;
13840 PQExpBuffer q;
13841 PQExpBuffer delq;
13842 char *qcollname;
13843 PGresult *res;
13844 int i_collprovider;
13845 int i_collisdeterministic;
13846 int i_collcollate;
13847 int i_collctype;
13848 int i_colllocale;
13849 int i_collicurules;
13850 const char *collprovider;
13851 const char *collcollate;
13852 const char *collctype;
13853 const char *colllocale;
13854 const char *collicurules;
13855
13856 /* Do nothing if not dumping schema */
13857 if (!dopt->dumpSchema)
13858 return;
13859
13860 query = createPQExpBuffer();
13861 q = createPQExpBuffer();
13862 delq = createPQExpBuffer();
13863
13864 qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13865
13866 /* Get collation-specific details */
13867 appendPQExpBufferStr(query, "SELECT ");
13868
13869 if (fout->remoteVersion >= 100000)
13871 "collprovider, "
13872 "collversion, ");
13873 else
13875 "'c' AS collprovider, "
13876 "NULL AS collversion, ");
13877
13878 if (fout->remoteVersion >= 120000)
13880 "collisdeterministic, ");
13881 else
13883 "true AS collisdeterministic, ");
13884
13885 if (fout->remoteVersion >= 170000)
13887 "colllocale, ");
13888 else if (fout->remoteVersion >= 150000)
13890 "colliculocale AS colllocale, ");
13891 else
13893 "NULL AS colllocale, ");
13894
13895 if (fout->remoteVersion >= 160000)
13897 "collicurules, ");
13898 else
13900 "NULL AS collicurules, ");
13901
13902 appendPQExpBuffer(query,
13903 "collcollate, "
13904 "collctype "
13905 "FROM pg_catalog.pg_collation c "
13906 "WHERE c.oid = '%u'::pg_catalog.oid",
13907 collinfo->dobj.catId.oid);
13908
13909 res = ExecuteSqlQueryForSingleRow(fout, query->data);
13910
13911 i_collprovider = PQfnumber(res, "collprovider");
13912 i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13913 i_collcollate = PQfnumber(res, "collcollate");
13914 i_collctype = PQfnumber(res, "collctype");
13915 i_colllocale = PQfnumber(res, "colllocale");
13916 i_collicurules = PQfnumber(res, "collicurules");
13917
13918 collprovider = PQgetvalue(res, 0, i_collprovider);
13919
13920 if (!PQgetisnull(res, 0, i_collcollate))
13921 collcollate = PQgetvalue(res, 0, i_collcollate);
13922 else
13923 collcollate = NULL;
13924
13925 if (!PQgetisnull(res, 0, i_collctype))
13926 collctype = PQgetvalue(res, 0, i_collctype);
13927 else
13928 collctype = NULL;
13929
13930 /*
13931 * Before version 15, collcollate and collctype were of type NAME and
13932 * non-nullable. Treat empty strings as NULL for consistency.
13933 */
13934 if (fout->remoteVersion < 150000)
13935 {
13936 if (collcollate[0] == '\0')
13937 collcollate = NULL;
13938 if (collctype[0] == '\0')
13939 collctype = NULL;
13940 }
13941
13942 if (!PQgetisnull(res, 0, i_colllocale))
13943 colllocale = PQgetvalue(res, 0, i_colllocale);
13944 else
13945 colllocale = NULL;
13946
13947 if (!PQgetisnull(res, 0, i_collicurules))
13948 collicurules = PQgetvalue(res, 0, i_collicurules);
13949 else
13950 collicurules = NULL;
13951
13952 appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13953 fmtQualifiedDumpable(collinfo));
13954
13955 appendPQExpBuffer(q, "CREATE COLLATION %s (",
13956 fmtQualifiedDumpable(collinfo));
13957
13958 appendPQExpBufferStr(q, "provider = ");
13959 if (collprovider[0] == 'b')
13960 appendPQExpBufferStr(q, "builtin");
13961 else if (collprovider[0] == 'c')
13962 appendPQExpBufferStr(q, "libc");
13963 else if (collprovider[0] == 'i')
13964 appendPQExpBufferStr(q, "icu");
13965 else if (collprovider[0] == 'd')
13966 /* to allow dumping pg_catalog; not accepted on input */
13967 appendPQExpBufferStr(q, "default");
13968 else
13969 pg_fatal("unrecognized collation provider: %s",
13970 collprovider);
13971
13972 if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13973 appendPQExpBufferStr(q, ", deterministic = false");
13974
13975 if (collprovider[0] == 'd')
13976 {
13977 if (collcollate || collctype || colllocale || collicurules)
13978 pg_log_warning("invalid collation \"%s\"", qcollname);
13979
13980 /* no locale -- the default collation cannot be reloaded anyway */
13981 }
13982 else if (collprovider[0] == 'b')
13983 {
13984 if (collcollate || collctype || !colllocale || collicurules)
13985 pg_log_warning("invalid collation \"%s\"", qcollname);
13986
13987 appendPQExpBufferStr(q, ", locale = ");
13988 appendStringLiteralAH(q, colllocale ? colllocale : "",
13989 fout);
13990 }
13991 else if (collprovider[0] == 'i')
13992 {
13993 if (fout->remoteVersion >= 150000)
13994 {
13995 if (collcollate || collctype || !colllocale)
13996 pg_log_warning("invalid collation \"%s\"", qcollname);
13997
13998 appendPQExpBufferStr(q, ", locale = ");
13999 appendStringLiteralAH(q, colllocale ? colllocale : "",
14000 fout);
14001 }
14002 else
14003 {
14004 if (!collcollate || !collctype || colllocale ||
14005 strcmp(collcollate, collctype) != 0)
14006 pg_log_warning("invalid collation \"%s\"", qcollname);
14007
14008 appendPQExpBufferStr(q, ", locale = ");
14009 appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
14010 }
14011
14012 if (collicurules)
14013 {
14014 appendPQExpBufferStr(q, ", rules = ");
14015 appendStringLiteralAH(q, collicurules ? collicurules : "", fout);
14016 }
14017 }
14018 else if (collprovider[0] == 'c')
14019 {
14020 if (colllocale || collicurules || !collcollate || !collctype)
14021 pg_log_warning("invalid collation \"%s\"", qcollname);
14022
14023 if (collcollate && collctype && strcmp(collcollate, collctype) == 0)
14024 {
14025 appendPQExpBufferStr(q, ", locale = ");
14026 appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
14027 }
14028 else
14029 {
14030 appendPQExpBufferStr(q, ", lc_collate = ");
14031 appendStringLiteralAH(q, collcollate ? collcollate : "", fout);
14032 appendPQExpBufferStr(q, ", lc_ctype = ");
14033 appendStringLiteralAH(q, collctype ? collctype : "", fout);
14034 }
14035 }
14036 else
14037 pg_fatal("unrecognized collation provider: %s", collprovider);
14038
14039 /*
14040 * For binary upgrade, carry over the collation version. For normal
14041 * dump/restore, omit the version, so that it is computed upon restore.
14042 */
14043 if (dopt->binary_upgrade)
14044 {
14045 int i_collversion;
14046
14047 i_collversion = PQfnumber(res, "collversion");
14048 if (!PQgetisnull(res, 0, i_collversion))
14049 {
14050 appendPQExpBufferStr(q, ", version = ");
14052 PQgetvalue(res, 0, i_collversion),
14053 fout);
14054 }
14055 }
14056
14057 appendPQExpBufferStr(q, ");\n");
14058
14059 if (dopt->binary_upgrade)
14061 "COLLATION", qcollname,
14062 collinfo->dobj.namespace->dobj.name);
14063
14064 if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14065 ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
14066 ARCHIVE_OPTS(.tag = collinfo->dobj.name,
14067 .namespace = collinfo->dobj.namespace->dobj.name,
14068 .owner = collinfo->rolname,
14069 .description = "COLLATION",
14070 .section = SECTION_PRE_DATA,
14071 .createStmt = q->data,
14072 .dropStmt = delq->data));
14073
14074 /* Dump Collation Comments */
14075 if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14076 dumpComment(fout, "COLLATION", qcollname,
14077 collinfo->dobj.namespace->dobj.name, collinfo->rolname,
14078 collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
14079
14080 PQclear(res);
14081
14082 destroyPQExpBuffer(query);
14084 destroyPQExpBuffer(delq);
14085 free(qcollname);
14086}
const char * rolname
Definition: pg_dump.h:280
DumpableObject dobj
Definition: pg_dump.h:279

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

Referenced by dumpDumpableObject().

◆ dumpComment()

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

Definition at line 10290 of file pg_dump.c.

10294{
10295 dumpCommentExtended(fout, type, name, namespace, owner,
10296 catalogId, subid, dumpId, NULL);
10297}
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:10190

References dumpCommentExtended(), name, and type.

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

◆ dumpCommentExtended()

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

Definition at line 10190 of file pg_dump.c.

10195{
10196 DumpOptions *dopt = fout->dopt;
10198 int ncomments;
10199
10200 /* do nothing, if --no-comments is supplied */
10201 if (dopt->no_comments)
10202 return;
10203
10204 /* Comments are schema not data ... except LO comments are data */
10205 if (strcmp(type, "LARGE OBJECT") != 0)
10206 {
10207 if (!dopt->dumpSchema)
10208 return;
10209 }
10210 else
10211 {
10212 /* We do dump LO comments in binary-upgrade mode */
10213 if (!dopt->dumpData && !dopt->binary_upgrade)
10214 return;
10215 }
10216
10217 /* Search for comments associated with catalogId, using table */
10218 ncomments = findComments(catalogId.tableoid, catalogId.oid,
10219 &comments);
10220
10221 /* Is there one matching the subid? */
10222 while (ncomments > 0)
10223 {
10224 if (comments->objsubid == subid)
10225 break;
10226 comments++;
10227 ncomments--;
10228 }
10229
10230 if (initdb_comment != NULL)
10231 {
10232 static CommentItem empty_comment = {.descr = ""};
10233
10234 /*
10235 * initdb creates this object with a comment. Skip dumping the
10236 * initdb-provided comment, which would complicate matters for
10237 * non-superuser use of pg_dump. When the DBA has removed initdb's
10238 * comment, replicate that.
10239 */
10240 if (ncomments == 0)
10241 {
10242 comments = &empty_comment;
10243 ncomments = 1;
10244 }
10245 else if (strcmp(comments->descr, initdb_comment) == 0)
10246 ncomments = 0;
10247 }
10248
10249 /* If a comment exists, build COMMENT ON statement */
10250 if (ncomments > 0)
10251 {
10254
10255 appendPQExpBuffer(query, "COMMENT ON %s ", type);
10256 if (namespace && *namespace)
10257 appendPQExpBuffer(query, "%s.", fmtId(namespace));
10258 appendPQExpBuffer(query, "%s IS ", name);
10259 appendStringLiteralAH(query, comments->descr, fout);
10260 appendPQExpBufferStr(query, ";\n");
10261
10262 appendPQExpBuffer(tag, "%s %s", type, name);
10263
10264 /*
10265 * We mark comments as SECTION_NONE because they really belong in the
10266 * same section as their parent, whether that is pre-data or
10267 * post-data.
10268 */
10270 ARCHIVE_OPTS(.tag = tag->data,
10271 .namespace = namespace,
10272 .owner = owner,
10273 .description = "COMMENT",
10274 .section = SECTION_NONE,
10275 .createStmt = query->data,
10276 .deps = &dumpId,
10277 .nDeps = 1));
10278
10279 destroyPQExpBuffer(query);
10280 destroyPQExpBuffer(tag);
10281 }
10282}
static int findComments(Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:10404
int no_comments
Definition: pg_backup.h:181

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

Referenced by dumpComment(), and dumpNamespace().

◆ dumpCompositeType()

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

Definition at line 11779 of file pg_dump.c.

11780{
11781 DumpOptions *dopt = fout->dopt;
11783 PQExpBuffer dropped = createPQExpBuffer();
11786 PGresult *res;
11787 char *qtypname;
11788 char *qualtypname;
11789 int ntups;
11790 int i_attname;
11791 int i_atttypdefn;
11792 int i_attlen;
11793 int i_attalign;
11794 int i_attisdropped;
11795 int i_attcollation;
11796 int i;
11797 int actual_atts;
11798
11800 {
11801 /*
11802 * Set up query for type-specific details.
11803 *
11804 * Since we only want to dump COLLATE clauses for attributes whose
11805 * collation is different from their type's default, we use a CASE
11806 * here to suppress uninteresting attcollations cheaply. atttypid
11807 * will be 0 for dropped columns; collation does not matter for those.
11808 */
11810 "PREPARE dumpCompositeType(pg_catalog.oid) AS\n"
11811 "SELECT a.attname, a.attnum, "
11812 "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11813 "a.attlen, a.attalign, a.attisdropped, "
11814 "CASE WHEN a.attcollation <> at.typcollation "
11815 "THEN a.attcollation ELSE 0 END AS attcollation "
11816 "FROM pg_catalog.pg_type ct "
11817 "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11818 "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11819 "WHERE ct.oid = $1 "
11820 "ORDER BY a.attnum");
11821
11822 ExecuteSqlStatement(fout, query->data);
11823
11825 }
11826
11827 printfPQExpBuffer(query,
11828 "EXECUTE dumpCompositeType('%u')",
11829 tyinfo->dobj.catId.oid);
11830
11831 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11832
11833 ntups = PQntuples(res);
11834
11835 i_attname = PQfnumber(res, "attname");
11836 i_atttypdefn = PQfnumber(res, "atttypdefn");
11837 i_attlen = PQfnumber(res, "attlen");
11838 i_attalign = PQfnumber(res, "attalign");
11839 i_attisdropped = PQfnumber(res, "attisdropped");
11840 i_attcollation = PQfnumber(res, "attcollation");
11841
11842 if (dopt->binary_upgrade)
11843 {
11845 tyinfo->dobj.catId.oid,
11846 false, false);
11848 }
11849
11850 qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11851 qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11852
11853 appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11854 qualtypname);
11855
11856 actual_atts = 0;
11857 for (i = 0; i < ntups; i++)
11858 {
11859 char *attname;
11860 char *atttypdefn;
11861 char *attlen;
11862 char *attalign;
11863 bool attisdropped;
11864 Oid attcollation;
11865
11866 attname = PQgetvalue(res, i, i_attname);
11867 atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11868 attlen = PQgetvalue(res, i, i_attlen);
11869 attalign = PQgetvalue(res, i, i_attalign);
11870 attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11871 attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11872
11873 if (attisdropped && !dopt->binary_upgrade)
11874 continue;
11875
11876 /* Format properly if not first attr */
11877 if (actual_atts++ > 0)
11878 appendPQExpBufferChar(q, ',');
11879 appendPQExpBufferStr(q, "\n\t");
11880
11881 if (!attisdropped)
11882 {
11883 appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11884
11885 /* Add collation if not default for the column type */
11886 if (OidIsValid(attcollation))
11887 {
11888 CollInfo *coll;
11889
11890 coll = findCollationByOid(attcollation);
11891 if (coll)
11892 appendPQExpBuffer(q, " COLLATE %s",
11893 fmtQualifiedDumpable(coll));
11894 }
11895 }
11896 else
11897 {
11898 /*
11899 * This is a dropped attribute and we're in binary_upgrade mode.
11900 * Insert a placeholder for it in the CREATE TYPE command, and set
11901 * length and alignment with direct UPDATE to the catalogs
11902 * afterwards. See similar code in dumpTableSchema().
11903 */
11904 appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11905
11906 /* stash separately for insertion after the CREATE TYPE */
11907 appendPQExpBufferStr(dropped,
11908 "\n-- For binary upgrade, recreate dropped column.\n");
11909 appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11910 "SET attlen = %s, "
11911 "attalign = '%s', attbyval = false\n"
11912 "WHERE attname = ", attlen, attalign);
11913 appendStringLiteralAH(dropped, attname, fout);
11914 appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11915 appendStringLiteralAH(dropped, qualtypname, fout);
11916 appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11917
11918 appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11919 qualtypname);
11920 appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11921 fmtId(attname));
11922 }
11923 }
11924 appendPQExpBufferStr(q, "\n);\n");
11925 appendPQExpBufferStr(q, dropped->data);
11926
11927 appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11928
11929 if (dopt->binary_upgrade)
11931 "TYPE", qtypname,
11932 tyinfo->dobj.namespace->dobj.name);
11933
11934 if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11935 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11936 ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11937 .namespace = tyinfo->dobj.namespace->dobj.name,
11938 .owner = tyinfo->rolname,
11939 .description = "TYPE",
11940 .section = SECTION_PRE_DATA,
11941 .createStmt = q->data,
11942 .dropStmt = delq->data));
11943
11944
11945 /* Dump Type Comments and Security Labels */
11946 if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11947 dumpComment(fout, "TYPE", qtypname,
11948 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11949 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11950
11951 if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11952 dumpSecLabel(fout, "TYPE", qtypname,
11953 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11954 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11955
11956 if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11957 dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11958 qtypname, NULL,
11959 tyinfo->dobj.namespace->dobj.name,
11960 NULL, tyinfo->rolname, &tyinfo->dacl);
11961
11962 /* Dump any per-column comments */
11963 if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11964 dumpCompositeTypeColComments(fout, tyinfo, res);
11965
11966 PQclear(res);
11968 destroyPQExpBuffer(dropped);
11969 destroyPQExpBuffer(delq);
11970 destroyPQExpBuffer(query);
11971 free(qtypname);
11972 free(qualtypname);
11973}
NameData attname
Definition: pg_attribute.h:41
char attalign
Definition: pg_attribute.h:100
int16 attlen
Definition: pg_attribute.h:59
@ PREPQUERY_DUMPCOMPOSITETYPE
Definition: pg_backup.h:68
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid)
Definition: pg_dump.c:5568
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, PGresult *res)
Definition: pg_dump.c:11985
Oid typrelid
Definition: pg_dump.h:210

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

11987{
11989 int ncomments;
11990 PQExpBuffer query;
11991 PQExpBuffer target;
11992 int i;
11993 int ntups;
11994 int i_attname;
11995 int i_attnum;
11996 int i_attisdropped;
11997
11998 /* do nothing, if --no-comments is supplied */
11999 if (fout->dopt->no_comments)
12000 return;
12001
12002 /* Search for comments associated with type's pg_class OID */
12003 ncomments = findComments(RelationRelationId, tyinfo->typrelid,
12004 &comments);
12005
12006 /* If no comments exist, we're done */
12007 if (ncomments <= 0)
12008 return;
12009
12010 /* Build COMMENT ON statements */
12011 query = createPQExpBuffer();
12012 target = createPQExpBuffer();
12013
12014 ntups = PQntuples(res);
12015 i_attnum = PQfnumber(res, "attnum");
12016 i_attname = PQfnumber(res, "attname");
12017 i_attisdropped = PQfnumber(res, "attisdropped");
12018 while (ncomments > 0)
12019 {
12020 const char *attname;
12021
12022 attname = NULL;
12023 for (i = 0; i < ntups; i++)
12024 {
12025 if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid &&
12026 PQgetvalue(res, i, i_attisdropped)[0] != 't')
12027 {
12028 attname = PQgetvalue(res, i, i_attname);
12029 break;
12030 }
12031 }
12032 if (attname) /* just in case we don't find it */
12033 {
12034 const char *descr = comments->descr;
12035
12036 resetPQExpBuffer(target);
12037 appendPQExpBuffer(target, "COLUMN %s.",
12038 fmtId(tyinfo->dobj.name));
12040
12041 resetPQExpBuffer(query);
12042 appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
12043 fmtQualifiedDumpable(tyinfo));
12044 appendPQExpBuffer(query, "%s IS ", fmtId(attname));
12045 appendStringLiteralAH(query, descr, fout);
12046 appendPQExpBufferStr(query, ";\n");
12047
12049 ARCHIVE_OPTS(.tag = target->data,
12050 .namespace = tyinfo->dobj.namespace->dobj.name,
12051 .owner = tyinfo->rolname,
12052 .description = "COMMENT",
12053 .section = SECTION_NONE,
12054 .createStmt = query->data,
12055 .deps = &(tyinfo->dobj.dumpId),
12056 .nDeps = 1));
12057 }
12058
12059 comments++;
12060 ncomments--;
12061 }
12062
12063 destroyPQExpBuffer(query);
12064 destroyPQExpBuffer(target);
12065}
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 17322 of file pg_dump.c.

17323{
17324 DumpOptions *dopt = fout->dopt;
17325 TableInfo *tbinfo = coninfo->contable;
17326 PQExpBuffer q;
17327 PQExpBuffer delq;
17328 char *tag = NULL;
17329 char *foreign;
17330
17331 /* Do nothing if not dumping schema */
17332 if (!dopt->dumpSchema)
17333 return;
17334
17335 q = createPQExpBuffer();
17336 delq = createPQExpBuffer();
17337
17338 foreign = tbinfo &&
17339 tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
17340
17341 if (coninfo->contype == 'p' ||
17342 coninfo->contype == 'u' ||
17343 coninfo->contype == 'x')
17344 {
17345 /* Index-related constraint */
17346 IndxInfo *indxinfo;
17347 int k;
17348
17349 indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
17350
17351 if (indxinfo == NULL)
17352 pg_fatal("missing index for constraint \"%s\"",
17353 coninfo->dobj.name);
17354
17355 if (dopt->binary_upgrade)
17357 indxinfo->dobj.catId.oid);
17358
17359 appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
17360 fmtQualifiedDumpable(tbinfo));
17361 appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
17362 fmtId(coninfo->dobj.name));
17363
17364 if (coninfo->condef)
17365 {
17366 /* pg_get_constraintdef should have provided everything */
17367 appendPQExpBuffer(q, "%s;\n", coninfo->condef);
17368 }
17369 else
17370 {
17372 coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
17373
17374 /*
17375 * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT
17376 * indexes. Being able to create this was fixed, but we need to
17377 * make the index distinct in order to be able to restore the
17378 * dump.
17379 */
17380 if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p')
17381 appendPQExpBufferStr(q, " NULLS NOT DISTINCT");
17382 appendPQExpBufferStr(q, " (");
17383 for (k = 0; k < indxinfo->indnkeyattrs; k++)
17384 {
17385 int indkey = (int) indxinfo->indkeys[k];
17386 const char *attname;
17387
17388 if (indkey == InvalidAttrNumber)
17389 break;
17390 attname = getAttrName(indkey, tbinfo);
17391
17392 appendPQExpBuffer(q, "%s%s",
17393 (k == 0) ? "" : ", ",
17394 fmtId(attname));
17395 }
17396 if (coninfo->conperiod)
17397 appendPQExpBufferStr(q, " WITHOUT OVERLAPS");
17398
17399 if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
17400 appendPQExpBufferStr(q, ") INCLUDE (");
17401
17402 for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
17403 {
17404 int indkey = (int) indxinfo->indkeys[k];
17405 const char *attname;
17406
17407 if (indkey == InvalidAttrNumber)
17408 break;
17409 attname = getAttrName(indkey, tbinfo);
17410
17411 appendPQExpBuffer(q, "%s%s",
17412 (k == indxinfo->indnkeyattrs) ? "" : ", ",
17413 fmtId(attname));
17414 }
17415
17416 appendPQExpBufferChar(q, ')');
17417
17418 if (nonemptyReloptions(indxinfo->indreloptions))
17419 {
17420 appendPQExpBufferStr(q, " WITH (");
17421 appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
17422 appendPQExpBufferChar(q, ')');
17423 }
17424
17425 if (coninfo->condeferrable)
17426 {
17427 appendPQExpBufferStr(q, " DEFERRABLE");
17428 if (coninfo->condeferred)
17429 appendPQExpBufferStr(q, " INITIALLY DEFERRED");
17430 }
17431
17432 appendPQExpBufferStr(q, ";\n");
17433 }
17434
17435 /*
17436 * Append ALTER TABLE commands as needed to set properties that we
17437 * only have ALTER TABLE syntax for. Keep this in sync with the
17438 * similar code in dumpIndex!
17439 */
17440
17441 /* If the index is clustered, we need to record that. */
17442 if (indxinfo->indisclustered)
17443 {
17444 appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
17445 fmtQualifiedDumpable(tbinfo));
17446 /* index name is not qualified in this syntax */
17447 appendPQExpBuffer(q, " ON %s;\n",
17448 fmtId(indxinfo->dobj.name));
17449 }
17450
17451 /* If the index defines identity, we need to record that. */
17452 if (indxinfo->indisreplident)
17453 {
17454 appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
17455 fmtQualifiedDumpable(tbinfo));
17456 /* index name is not qualified in this syntax */
17457 appendPQExpBuffer(q, " INDEX %s;\n",
17458 fmtId(indxinfo->dobj.name));
17459 }
17460
17461 /* Indexes can depend on extensions */
17462 append_depends_on_extension(fout, q, &indxinfo->dobj,
17463 "pg_catalog.pg_class", "INDEX",
17464 fmtQualifiedDumpable(indxinfo));
17465
17466 appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
17467 fmtQualifiedDumpable(tbinfo));
17468 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17469 fmtId(coninfo->dobj.name));
17470
17471 tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17472
17473 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17474 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17475 ARCHIVE_OPTS(.tag = tag,
17476 .namespace = tbinfo->dobj.namespace->dobj.name,
17477 .tablespace = indxinfo->tablespace,
17478 .owner = tbinfo->rolname,
17479 .description = "CONSTRAINT",
17480 .section = SECTION_POST_DATA,
17481 .createStmt = q->data,
17482 .dropStmt = delq->data));
17483 }
17484 else if (coninfo->contype == 'f')
17485 {
17486 char *only;
17487
17488 /*
17489 * Foreign keys on partitioned tables are always declared as
17490 * inheriting to partitions; for all other cases, emit them as
17491 * applying ONLY directly to the named table, because that's how they
17492 * work for regular inherited tables.
17493 */
17494 only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
17495
17496 /*
17497 * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
17498 * current table data is not processed
17499 */
17500 appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
17501 only, fmtQualifiedDumpable(tbinfo));
17502 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17503 fmtId(coninfo->dobj.name),
17504 coninfo->condef);
17505
17506 appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
17507 only, fmtQualifiedDumpable(tbinfo));
17508 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17509 fmtId(coninfo->dobj.name));
17510
17511 tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17512
17513 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17514 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17515 ARCHIVE_OPTS(.tag = tag,
17516 .namespace = tbinfo->dobj.namespace->dobj.name,
17517 .owner = tbinfo->rolname,
17518 .description = "FK CONSTRAINT",
17519 .section = SECTION_POST_DATA,
17520 .createStmt = q->data,
17521 .dropStmt = delq->data));
17522 }
17523 else if (coninfo->contype == 'c' && tbinfo)
17524 {
17525 /* CHECK constraint on a table */
17526
17527 /* Ignore if not to be dumped separately, or if it was inherited */
17528 if (coninfo->separate && coninfo->conislocal)
17529 {
17530 /* not ONLY since we want it to propagate to children */
17531 appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
17532 fmtQualifiedDumpable(tbinfo));
17533 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17534 fmtId(coninfo->dobj.name),
17535 coninfo->condef);
17536
17537 appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
17538 fmtQualifiedDumpable(tbinfo));
17539 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17540 fmtId(coninfo->dobj.name));
17541
17542 tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17543
17544 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17545 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17546 ARCHIVE_OPTS(.tag = tag,
17547 .namespace = tbinfo->dobj.namespace->dobj.name,
17548 .owner = tbinfo->rolname,
17549 .description = "CHECK CONSTRAINT",
17550 .section = SECTION_POST_DATA,
17551 .createStmt = q->data,
17552 .dropStmt = delq->data));
17553 }
17554 }
17555 else if (coninfo->contype == 'c' && tbinfo == NULL)
17556 {
17557 /* CHECK constraint on a domain */
17558 TypeInfo *tyinfo = coninfo->condomain;
17559
17560 /* Ignore if not to be dumped separately */
17561 if (coninfo->separate)
17562 {
17563 appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
17564 fmtQualifiedDumpable(tyinfo));
17565 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17566 fmtId(coninfo->dobj.name),
17567 coninfo->condef);
17568
17569 appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
17570 fmtQualifiedDumpable(tyinfo));
17571 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17572 fmtId(coninfo->dobj.name));
17573
17574 tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
17575
17576 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17577 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17578 ARCHIVE_OPTS(.tag = tag,
17579 .namespace = tyinfo->dobj.namespace->dobj.name,
17580 .owner = tyinfo->rolname,
17581 .description = "CHECK CONSTRAINT",
17582 .section = SECTION_POST_DATA,
17583 .createStmt = q->data,
17584 .dropStmt = delq->data));
17585 }
17586 }
17587 else
17588 {
17589 pg_fatal("unrecognized constraint type: %c",
17590 coninfo->contype);
17591 }
17592
17593 /* Dump Constraint Comments --- only works for table constraints */
17594 if (tbinfo && coninfo->separate &&
17595 coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17596 dumpTableConstraintComment(fout, coninfo);
17597
17598 free(tag);
17600 destroyPQExpBuffer(delq);
17601}
#define InvalidAttrNumber
Definition: attnum.h:23
@ SECTION_POST_DATA
Definition: pg_backup.h:60
static void append_depends_on_extension(Archive *fout, PQExpBuffer create, const DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
Definition: pg_dump.c:5347
static const char * getAttrName(int attrnum, const TableInfo *tblInfo)
Definition: pg_dump.c:17036
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
Definition: pg_dump.c:19203
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
Definition: pg_dump.c:17611
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:19191
TypeInfo * condomain
Definition: pg_dump.h:483
TableInfo * contable
Definition: pg_dump.h:482
bool condeferred
Definition: pg_dump.h:489
bool conperiod
Definition: pg_dump.h:490
bool conislocal
Definition: pg_dump.h:491
DumpableObject dobj
Definition: pg_dump.h:481
DumpId conindex
Definition: pg_dump.h:487
bool condeferrable
Definition: pg_dump.h:488
char * condef
Definition: pg_dump.h:485
bool indisreplident
Definition: pg_dump.h:416
int indnkeyattrs
Definition: pg_dump.h:411
int indnattrs
Definition: pg_dump.h:412
Oid * indkeys
Definition: pg_dump.h:413
char * indreloptions
Definition: pg_dump.h:408
bool indisclustered
Definition: pg_dump.h:415
char * tablespace
Definition: pg_dump.h:407
bool indnullsnotdistinct
Definition: pg_dump.h:417
DumpableObject dobj
Definition: pg_dump.h:404

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::conperiod, _constraintInfo::contable, _constraintInfo::contype, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _typeInfo::dobj, _tableInfo::dobj, _indxInfo::dobj, _constraintInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, _dumpableObject::dumpId, _dumpOptions::dumpSchema, 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 14093 of file pg_dump.c.

14094{
14095 DumpOptions *dopt = fout->dopt;
14096 PQExpBuffer query;
14097 PQExpBuffer q;
14098 PQExpBuffer delq;
14099 char *qconvname;
14100 PGresult *res;
14101 int i_conforencoding;
14102 int i_contoencoding;
14103 int i_conproc;
14104 int i_condefault;
14105 const char *conforencoding;
14106 const char *contoencoding;
14107 const char *conproc;
14108 bool condefault;
14109
14110 /* Do nothing if not dumping schema */
14111 if (!dopt->dumpSchema)
14112 return;
14113
14114 query = createPQExpBuffer();
14115 q = createPQExpBuffer();
14116 delq = createPQExpBuffer();
14117
14118 qconvname = pg_strdup(fmtId(convinfo->dobj.name));
14119
14120 /* Get conversion-specific details */
14121 appendPQExpBuffer(query, "SELECT "
14122 "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
14123 "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
14124 "conproc, condefault "
14125 "FROM pg_catalog.pg_conversion c "
14126 "WHERE c.oid = '%u'::pg_catalog.oid",
14127 convinfo->dobj.catId.oid);
14128
14129 res = ExecuteSqlQueryForSingleRow(fout, query->data);
14130
14131 i_conforencoding = PQfnumber(res, "conforencoding");
14132 i_contoencoding = PQfnumber(res, "contoencoding");
14133 i_conproc = PQfnumber(res, "conproc");
14134 i_condefault = PQfnumber(res, "condefault");
14135
14136 conforencoding = PQgetvalue(res, 0, i_conforencoding);
14137 contoencoding = PQgetvalue(res, 0, i_contoencoding);
14138 conproc = PQgetvalue(res, 0, i_conproc);
14139 condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
14140
14141 appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
14142 fmtQualifiedDumpable(convinfo));
14143
14144 appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
14145 (condefault) ? "DEFAULT " : "",
14146 fmtQualifiedDumpable(convinfo));
14147 appendStringLiteralAH(q, conforencoding, fout);
14148 appendPQExpBufferStr(q, " TO ");
14149 appendStringLiteralAH(q, contoencoding, fout);
14150 /* regproc output is already sufficiently quoted */
14151 appendPQExpBuffer(q, " FROM %s;\n", conproc);
14152
14153 if (dopt->binary_upgrade)
14155 "CONVERSION", qconvname,
14156 convinfo->dobj.namespace->dobj.name);
14157
14158 if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14159 ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
14160 ARCHIVE_OPTS(.tag = convinfo->dobj.name,
14161 .namespace = convinfo->dobj.namespace->dobj.name,
14162 .owner = convinfo->rolname,
14163 .description = "CONVERSION",
14164 .section = SECTION_PRE_DATA,
14165 .createStmt = q->data,
14166 .dropStmt = delq->data));
14167
14168 /* Dump Conversion Comments */
14169 if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14170 dumpComment(fout, "CONVERSION", qconvname,
14171 convinfo->dobj.namespace->dobj.name, convinfo->rolname,
14172 convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
14173
14174 PQclear(res);
14175
14176 destroyPQExpBuffer(query);
14178 destroyPQExpBuffer(delq);
14179 free(qconvname);
14180}
DumpableObject dobj
Definition: pg_dump.h:285
const char * rolname
Definition: pg_dump.h:286

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _convInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, _dumpOptions::dumpSchema, 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 3091 of file pg_dump.c.

3092{
3093 DumpOptions *dopt = fout->dopt;
3095 PQExpBuffer delQry = createPQExpBuffer();
3096 PQExpBuffer creaQry = createPQExpBuffer();
3097 PQExpBuffer labelq = createPQExpBuffer();
3098 PGconn *conn = GetConnection(fout);
3099 PGresult *res;
3100 int i_tableoid,
3101 i_oid,
3102 i_datname,
3103 i_datdba,
3104 i_encoding,
3105 i_datlocprovider,
3106 i_collate,
3107 i_ctype,
3108 i_datlocale,
3109 i_daticurules,
3110 i_frozenxid,
3111 i_minmxid,
3112 i_datacl,
3113 i_acldefault,
3114 i_datistemplate,
3115 i_datconnlimit,
3116 i_datcollversion,
3117 i_tablespace;
3118 CatalogId dbCatId;
3119 DumpId dbDumpId;
3120 DumpableAcl dbdacl;
3121 const char *datname,
3122 *dba,
3123 *encoding,
3125 *collate,
3126 *ctype,
3127 *locale,
3128 *icurules,
3130 *datconnlimit,
3131 *tablespace;
3132 uint32 frozenxid,
3133 minmxid;
3134 char *qdatname;
3135
3136 pg_log_info("saving database definition");
3137
3138 /*
3139 * Fetch the database-level properties for this database.
3140 */
3141 appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, "
3142 "datdba, "
3143 "pg_encoding_to_char(encoding) AS encoding, "
3144 "datcollate, datctype, datfrozenxid, "
3145 "datacl, acldefault('d', datdba) AS acldefault, "
3146 "datistemplate, datconnlimit, ");
3147 if (fout->remoteVersion >= 90300)
3148 appendPQExpBufferStr(dbQry, "datminmxid, ");
3149 else
3150 appendPQExpBufferStr(dbQry, "0 AS datminmxid, ");
3151 if (fout->remoteVersion >= 170000)
3152 appendPQExpBufferStr(dbQry, "datlocprovider, datlocale, datcollversion, ");
3153 else if (fout->remoteVersion >= 150000)
3154 appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale AS datlocale, datcollversion, ");
3155 else
3156 appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS datlocale, NULL AS datcollversion, ");
3157 if (fout->remoteVersion >= 160000)
3158 appendPQExpBufferStr(dbQry, "daticurules, ");
3159 else
3160 appendPQExpBufferStr(dbQry, "NULL AS daticurules, ");
3162 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
3163 "shobj_description(oid, 'pg_database') AS description "
3164 "FROM pg_database "
3165 "WHERE datname = current_database()");
3166
3167 res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
3168
3169 i_tableoid = PQfnumber(res, "tableoid");
3170 i_oid = PQfnumber(res, "oid");
3171 i_datname = PQfnumber(res, "datname");
3172 i_datdba = PQfnumber(res, "datdba");
3173 i_encoding = PQfnumber(res, "encoding");
3174 i_datlocprovider = PQfnumber(res, "datlocprovider");
3175 i_collate = PQfnumber(res, "datcollate");
3176 i_ctype = PQfnumber(res, "datctype");
3177 i_datlocale = PQfnumber(res, "datlocale");
3178 i_daticurules = PQfnumber(res, "daticurules");
3179 i_frozenxid = PQfnumber(res, "datfrozenxid");
3180 i_minmxid = PQfnumber(res, "datminmxid");
3181 i_datacl = PQfnumber(res, "datacl");
3182 i_acldefault = PQfnumber(res, "acldefault");
3183 i_datistemplate = PQfnumber(res, "datistemplate");
3184 i_datconnlimit = PQfnumber(res, "datconnlimit");
3185 i_datcollversion = PQfnumber(res, "datcollversion");
3186 i_tablespace = PQfnumber(res, "tablespace");
3187
3188 dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
3189 dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
3190 datname = PQgetvalue(res, 0, i_datname);
3191 dba = getRoleName(PQgetvalue(res, 0, i_datdba));
3192 encoding = PQgetvalue(res, 0, i_encoding);
3193 datlocprovider = PQgetvalue(res, 0, i_datlocprovider);
3194 collate = PQgetvalue(res, 0, i_collate);
3195 ctype = PQgetvalue(res, 0, i_ctype);
3196 if (!PQgetisnull(res, 0, i_datlocale))
3197 locale = PQgetvalue(res, 0, i_datlocale);
3198 else
3199 locale = NULL;
3200 if (!PQgetisnull(res, 0, i_daticurules))
3201 icurules = PQgetvalue(res, 0, i_daticurules);
3202 else
3203 icurules = NULL;
3204 frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
3205 minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
3206 dbdacl.acl = PQgetvalue(res, 0, i_datacl);
3207 dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault);
3208 datistemplate = PQgetvalue(res, 0, i_datistemplate);
3209 datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
3210 tablespace = PQgetvalue(res, 0, i_tablespace);
3211
3212 qdatname = pg_strdup(fmtId(datname));
3213
3214 /*
3215 * Prepare the CREATE DATABASE command. We must specify OID (if we want
3216 * to preserve that), as well as the encoding, locale, and tablespace
3217 * since those can't be altered later. Other DB properties are left to
3218 * the DATABASE PROPERTIES entry, so that they can be applied after
3219 * reconnecting to the target DB.
3220 *
3221 * For binary upgrade, we use the FILE_COPY strategy because testing has
3222 * shown it to be faster. When the server is in binary upgrade mode, it
3223 * will also skip the checkpoints this strategy ordinarily performs.
3224 */
3225 if (dopt->binary_upgrade)
3226 {
3227 appendPQExpBuffer(creaQry,
3228 "CREATE DATABASE %s WITH TEMPLATE = template0 "
3229 "OID = %u STRATEGY = FILE_COPY",
3230 qdatname, dbCatId.oid);
3231 }
3232 else
3233 {
3234 appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
3235 qdatname);
3236 }
3237 if (strlen(encoding) > 0)
3238 {
3239 appendPQExpBufferStr(creaQry, " ENCODING = ");
3240 appendStringLiteralAH(creaQry, encoding, fout);
3241 }
3242
3243 appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = ");
3244 if (datlocprovider[0] == 'b')
3245 appendPQExpBufferStr(creaQry, "builtin");
3246 else if (datlocprovider[0] == 'c')
3247 appendPQExpBufferStr(creaQry, "libc");
3248 else if (datlocprovider[0] == 'i')
3249 appendPQExpBufferStr(creaQry, "icu");
3250 else
3251 pg_fatal("unrecognized locale provider: %s",
3253
3254 if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
3255 {
3256 appendPQExpBufferStr(creaQry, " LOCALE = ");
3257 appendStringLiteralAH(creaQry, collate, fout);
3258 }
3259 else
3260 {
3261 if (strlen(collate) > 0)
3262 {
3263 appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
3264 appendStringLiteralAH(creaQry, collate, fout);
3265 }
3266 if (strlen(ctype) > 0)
3267 {
3268 appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
3269 appendStringLiteralAH(creaQry, ctype, fout);
3270 }
3271 }
3272 if (locale)
3273 {
3274 if (datlocprovider[0] == 'b')
3275 appendPQExpBufferStr(creaQry, " BUILTIN_LOCALE = ");
3276 else
3277 appendPQExpBufferStr(creaQry, " ICU_LOCALE = ");
3278
3279 appendStringLiteralAH(creaQry, locale, fout);
3280 }
3281
3282 if (icurules)
3283 {
3284 appendPQExpBufferStr(creaQry, " ICU_RULES = ");
3285 appendStringLiteralAH(creaQry, icurules, fout);
3286 }
3287
3288 /*
3289 * For binary upgrade, carry over the collation version. For normal
3290 * dump/restore, omit the version, so that it is computed upon restore.
3291 */
3292 if (dopt->binary_upgrade)
3293 {
3294 if (!PQgetisnull(res, 0, i_datcollversion))
3295 {
3296 appendPQExpBufferStr(creaQry, " COLLATION_VERSION = ");
3297 appendStringLiteralAH(creaQry,
3298 PQgetvalue(res, 0, i_datcollversion),
3299 fout);
3300 }
3301 }
3302
3303 /*
3304 * Note: looking at dopt->outputNoTablespaces here is completely the wrong
3305 * thing; the decision whether to specify a tablespace should be left till
3306 * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
3307 * label the DATABASE entry with the tablespace and let the normal
3308 * tablespace selection logic work ... but CREATE DATABASE doesn't pay
3309 * attention to default_tablespace, so that won't work.
3310 */
3311 if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
3312 !dopt->outputNoTablespaces)
3313 appendPQExpBuffer(creaQry, " TABLESPACE = %s",
3314 fmtId(tablespace));
3315 appendPQExpBufferStr(creaQry, ";\n");
3316
3317 appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
3318 qdatname);
3319
3320 dbDumpId = createDumpId();
3321
3322 ArchiveEntry(fout,
3323 dbCatId, /* catalog ID */
3324 dbDumpId, /* dump ID */
3325 ARCHIVE_OPTS(.tag = datname,
3326 .owner = dba,
3327 .description = "DATABASE",
3328 .section = SECTION_PRE_DATA,
3329 .createStmt = creaQry->data,
3330 .dropStmt = delQry->data));
3331
3332 /* Compute correct tag for archive entry */
3333 appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
3334
3335 /* Dump DB comment if any */
3336 {
3337 /*
3338 * 8.2 and up keep comments on shared objects in a shared table, so we
3339 * cannot use the dumpComment() code used for other database objects.
3340 * Be careful that the ArchiveEntry parameters match that function.
3341 */
3342 char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
3343
3344 if (comment && *comment && !dopt->no_comments)
3345 {
3346 resetPQExpBuffer(dbQry);
3347
3348 /*
3349 * Generates warning when loaded into a differently-named
3350 * database.
3351 */
3352 appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
3353 appendStringLiteralAH(dbQry, comment, fout);
3354 appendPQExpBufferStr(dbQry, ";\n");
3355
3357 ARCHIVE_OPTS(.tag = labelq->data,
3358 .owner = dba,
3359 .description = "COMMENT",
3360 .section = SECTION_NONE,
3361 .createStmt = dbQry->data,
3362 .deps = &dbDumpId,
3363 .nDeps = 1));
3364 }
3365 }
3366
3367 /* Dump DB security label, if enabled */
3368 if (!dopt->no_security_labels)
3369 {
3370 PGresult *shres;
3371 PQExpBuffer seclabelQry;
3372
3373 seclabelQry = createPQExpBuffer();
3374
3375 buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry);
3376 shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
3377 resetPQExpBuffer(seclabelQry);
3378 emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
3379 if (seclabelQry->len > 0)
3381 ARCHIVE_OPTS(.tag = labelq->data,
3382 .owner = dba,
3383 .description = "SECURITY LABEL",
3384 .section = SECTION_NONE,
3385 .createStmt = seclabelQry->data,
3386 .deps = &dbDumpId,
3387 .nDeps = 1));
3388 destroyPQExpBuffer(seclabelQry);
3389 PQclear(shres);
3390 }
3391
3392 /*
3393 * Dump ACL if any. Note that we do not support initial privileges
3394 * (pg_init_privs) on databases.
3395 */
3396 dbdacl.privtype = 0;
3397 dbdacl.initprivs = NULL;
3398
3399 dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE",
3400 qdatname, NULL, NULL,
3401 NULL, dba, &dbdacl);
3402
3403 /*
3404 * Now construct a DATABASE PROPERTIES archive entry to restore any
3405 * non-default database-level properties. (The reason this must be
3406 * separate is that we cannot put any additional commands into the TOC
3407 * entry that has CREATE DATABASE. pg_restore would execute such a group
3408 * in an implicit transaction block, and the backend won't allow CREATE
3409 * DATABASE in that context.)
3410 */
3411 resetPQExpBuffer(creaQry);
3412 resetPQExpBuffer(delQry);
3413
3414 if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
3415 appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
3416 qdatname, datconnlimit);
3417
3418 if (strcmp(datistemplate, "t") == 0)
3419 {
3420 appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
3421 qdatname);
3422
3423 /*
3424 * The backend won't accept DROP DATABASE on a template database. We
3425 * can deal with that by removing the template marking before the DROP
3426 * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
3427 * since no such command is currently supported, fake it with a direct
3428 * UPDATE on pg_database.
3429 */
3430 appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
3431 "SET datistemplate = false WHERE datname = ");
3432 appendStringLiteralAH(delQry, datname, fout);
3433 appendPQExpBufferStr(delQry, ";\n");
3434 }
3435
3436 /*
3437 * We do not restore pg_database.dathasloginevt because it is set
3438 * automatically on login event trigger creation.
3439 */
3440
3441 /* Add database-specific SET options */
3442 dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
3443
3444 /*
3445 * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
3446 * entry, too, for lack of a better place.
3447 */
3448 if (dopt->binary_upgrade)
3449 {
3450 appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
3451 appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
3452 "SET datfrozenxid = '%u', datminmxid = '%u'\n"
3453 "WHERE datname = ",
3454 frozenxid, minmxid);
3455 appendStringLiteralAH(creaQry, datname, fout);
3456 appendPQExpBufferStr(creaQry, ";\n");
3457 }
3458
3459 if (creaQry->len > 0)
3461 ARCHIVE_OPTS(.tag = datname,
3462 .owner = dba,
3463 .description = "DATABASE PROPERTIES",
3464 .section = SECTION_PRE_DATA,
3465 .createStmt = creaQry->data,
3466 .dropStmt = delQry->data,
3467 .deps = &dbDumpId));
3468
3469 /*
3470 * pg_largeobject comes from the old system intact, so set its
3471 * relfrozenxids, relminmxids and relfilenode.
3472 */
3473 if (dopt->binary_upgrade)
3474 {
3475 PGresult *lo_res;
3476 PQExpBuffer loFrozenQry = createPQExpBuffer();
3477 PQExpBuffer loOutQry = createPQExpBuffer();
3478 PQExpBuffer loHorizonQry = createPQExpBuffer();
3479 int ii_relfrozenxid,
3480 ii_relfilenode,
3481 ii_oid,
3482 ii_relminmxid;
3483
3484 /*
3485 * pg_largeobject
3486 */
3487 if (fout->remoteVersion >= 90300)
3488 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n"
3489 "FROM pg_catalog.pg_class\n"
3490 "WHERE oid IN (%u, %u);\n",
3491 LargeObjectRelationId, LargeObjectLOidPNIndexId);
3492 else
3493 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid, relfilenode, oid\n"
3494 "FROM pg_catalog.pg_class\n"
3495 "WHERE oid IN (%u, %u);\n",
3496 LargeObjectRelationId, LargeObjectLOidPNIndexId);
3497
3498 lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK);
3499
3500 ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
3501 ii_relminmxid = PQfnumber(lo_res, "relminmxid");
3502 ii_relfilenode = PQfnumber(lo_res, "relfilenode");
3503 ii_oid = PQfnumber(lo_res, "oid");
3504
3505 appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
3506 appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n");
3507 for (int i = 0; i < PQntuples(lo_res); ++i)
3508 {
3509 Oid oid;
3510 RelFileNumber relfilenumber;
3511
3512 appendPQExpBuffer(loHorizonQry, "UPDATE pg_catalog.pg_class\n"
3513 "SET relfrozenxid = '%u', relminmxid = '%u'\n"
3514 "WHERE oid = %u;\n",
3515 atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)),
3516 atooid(PQgetvalue(lo_res, i, ii_relminmxid)),
3517 atooid(PQgetvalue(lo_res, i, ii_oid)));
3518
3519 oid = atooid(PQgetvalue(lo_res, i, ii_oid));
3520 relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode));
3521
3522 if (oid == LargeObjectRelationId)
3523 appendPQExpBuffer(loOutQry,
3524 "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
3525 relfilenumber);
3526 else if (oid == LargeObjectLOidPNIndexId)
3527 appendPQExpBuffer(loOutQry,
3528 "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
3529 relfilenumber);
3530 }
3531
3532 appendPQExpBufferStr(loOutQry,
3533 "TRUNCATE pg_catalog.pg_largeobject;\n");
3534 appendPQExpBufferStr(loOutQry, loHorizonQry->data);
3535
3537 ARCHIVE_OPTS(.tag = "pg_largeobject",
3538 .description = "pg_largeobject",
3539 .section = SECTION_PRE_DATA,
3540 .createStmt = loOutQry->data));
3541
3542 PQclear(lo_res);
3543
3544 destroyPQExpBuffer(loFrozenQry);
3545 destroyPQExpBuffer(loHorizonQry);
3546 destroyPQExpBuffer(loOutQry);
3547 }
3548
3549 PQclear(res);
3550
3551 free(qdatname);
3552 destroyPQExpBuffer(dbQry);
3553 destroyPQExpBuffer(delQry);
3554 destroyPQExpBuffer(creaQry);
3555 destroyPQExpBuffer(labelq);
3556}
uint32_t uint32
Definition: c.h:485
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:202
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
Definition: dumputils.c:637
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
Definition: dumputils.c:655
#define comment
Definition: indent_codes.h:49
static char * locale
Definition: initdb.c:140
#define pg_log_info(...)
Definition: logging.h:124
char datlocprovider
Definition: pg_database.h:44
NameData datname
Definition: pg_database.h:35
int32 encoding
Definition: pg_database.h:41
bool datistemplate
Definition: pg_database.h:47
int32 datconnlimit
Definition: pg_database.h:59
static const char * getRoleName(const char *roleoid_str)
Definition: pg_dump.c:9990
static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf, const char *dbname, Oid dboid)
Definition: pg_dump.c:3563
static char * tablespace
Definition: pgbench.c:216
Oid RelFileNumber
Definition: relpath.h:25
PGconn * conn
Definition: streamutil.c:53
int no_security_labels
Definition: pg_backup.h:182
int outputNoTablespaces
Definition: pg_backup.h:190
const char * description

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

Referenced by main().

◆ dumpDatabaseConfig()

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

Definition at line 3563 of file pg_dump.c.

3565{
3566 PGconn *conn = GetConnection(AH);
3568 PGresult *res;
3569
3570 /* First collect database-specific options */
3571 printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
3572 "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3573 dboid);
3574
3575 res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3576
3577 for (int i = 0; i < PQntuples(res); i++)
3579 "DATABASE", dbname, NULL, NULL,
3580 outbuf);
3581
3582 PQclear(res);
3583
3584 /* Now look for role-and-database-specific options */
3585 printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3586 "FROM pg_db_role_setting s, pg_roles r "
3587 "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3588 dboid);
3589
3590 res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3591
3592 for (int i = 0; i < PQntuples(res); i++)