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_cast_d.h"
#include "catalog/pg_class_d.h"
#include "catalog/pg_collation_d.h"
#include "catalog/pg_default_acl_d.h"
#include "catalog/pg_largeobject_d.h"
#include "catalog/pg_largeobject_metadata_d.h"
#include "catalog/pg_proc_d.h"
#include "catalog/pg_trigger_d.h"
#include "catalog/pg_type_d.h"
#include "common/connect.h"
#include "dumputils.h"
#include "fe_utils/string_utils.h"
#include "getopt_long.h"
#include "libpq/libpq-fs.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "storage/block.h"
Include dependency graph for pg_dump.c:

Go to the source code of this file.

Data Structures

struct  CommentItem
 
struct  SecLabelItem
 

Macros

#define DUMP_DEFAULT_ROWS_PER_INSERT   1
 
#define fmtQualifiedDumpable(obj)
 

Typedefs

typedef enum OidOptions OidOptions
 

Enumerations

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

Functions

static void help (const char *progname)
 
static void setup_connection (Archive *AH, const char *dumpencoding, const char *dumpsnapshot, char *use_role)
 
static ArchiveFormat parseArchiveFormat (const char *format, ArchiveMode *mode)
 
static void expand_schema_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_extension_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_foreign_server_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids)
 
static void expand_table_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static NamespaceInfofindNamespace (Oid nsoid)
 
static void dumpTableData (Archive *fout, const TableDataInfo *tdinfo)
 
static void refreshMatViewData (Archive *fout, const TableDataInfo *tdinfo)
 
static void guessConstraintInheritance (TableInfo *tblinfo, int numTables)
 
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 (Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
 
static int collectComments (Archive *fout, CommentItem **items)
 
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 (Archive *fout, Oid classoid, Oid objoid, SecLabelItem **items)
 
static int collectSecLabels (Archive *fout, SecLabelItem **items)
 
static void dumpDumpableObject (Archive *fout, const 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)
 
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 *oprinfo)
 
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 *tbinfo)
 
static void dumpAttrDef (Archive *fout, const AttrDefInfo *adinfo)
 
static void dumpSequence (Archive *fout, const TableInfo *tbinfo)
 
static void dumpSequenceData (Archive *fout, const TableDataInfo *tdinfo)
 
static void dumpIndex (Archive *fout, const IndxInfo *indxinfo)
 
static void dumpIndexAttach (Archive *fout, const IndexAttachInfo *attachinfo)
 
static void dumpStatisticsExt (Archive *fout, const StatsExtInfo *statsextinfo)
 
static void dumpConstraint (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTableConstraintComment (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTSParser (Archive *fout, const TSParserInfo *prsinfo)
 
static void dumpTSDictionary (Archive *fout, const TSDictInfo *dictinfo)
 
static void dumpTSTemplate (Archive *fout, const TSTemplateInfo *tmplinfo)
 
static void dumpTSConfig (Archive *fout, const TSConfigInfo *cfginfo)
 
static void dumpForeignDataWrapper (Archive *fout, const FdwInfo *fdwinfo)
 
static void dumpForeignServer (Archive *fout, const ForeignServerInfo *srvinfo)
 
static void dumpUserMappings (Archive *fout, const char *servername, const char *namespace, const char *owner, CatalogId catalogId, DumpId dumpId)
 
static void dumpDefaultACL (Archive *fout, const DefaultACLInfo *daclinfo)
 
static DumpId dumpACL (Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
 
static void getDependencies (Archive *fout)
 
static void BuildArchiveDependencies (Archive *fout)
 
static void findDumpableDependencies (ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
 
static DumpableObjectcreateBoundaryObjects (void)
 
static void addBoundaryDependencies (DumpableObject **dobjs, int numObjs, DumpableObject *boundaryObjs)
 
static void addConstrChildIdxDeps (DumpableObject *dobj, const IndxInfo *refidx)
 
static void getDomainConstraints (Archive *fout, TypeInfo *tyinfo)
 
static void getTableData (DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
 
static void makeTableDataInfo (DumpOptions *dopt, TableInfo *tbinfo)
 
static void buildMatViewRefreshDependencies (Archive *fout)
 
static void getTableDataFKConstraints (void)
 
static char * format_function_arguments (const FuncInfo *finfo, const char *funcargs, bool is_agg)
 
static char * format_function_arguments_old (Archive *fout, const FuncInfo *finfo, int nallargs, char **allargtypes, char **argmodes, char **argnames)
 
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 Oid findLastBuiltinOid_V71 (Archive *fout)
 
static char * getFormattedTypeName (Archive *fout, Oid oid, OidOptions opts)
 
static void getBlobs (Archive *fout)
 
static void dumpBlob (Archive *fout, const BlobInfo *binfo)
 
static int dumpBlobs (Archive *fout, const void *arg)
 
static void dumpPolicy (Archive *fout, const PolicyInfo *polinfo)
 
static void dumpPublication (Archive *fout, const PublicationInfo *pubinfo)
 
static void dumpPublicationTable (Archive *fout, const PublicationRelInfo *pubrinfo)
 
static void dumpSubscription (Archive *fout, const SubscriptionInfo *subinfo)
 
static void dumpDatabase (Archive *AH)
 
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 dumpToastCompression (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_oid (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_rel_oid)
 
static void binary_upgrade_set_pg_class_oids (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
 
static void binary_upgrade_extension_member (PQExpBuffer upgrade_buffer, const DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
 
static const char * getAttrName (int attrnum, const TableInfo *tblInfo)
 
static const char * fmtCopyColumnList (const TableInfo *ti, PQExpBuffer buffer)
 
static bool nonemptyReloptions (const char *reloptions)
 
static void appendIndexCollationVersion (PQExpBuffer buffer, const IndxInfo *indxinfo, int enc, bool coll_unknown, Archive *fout)
 
static void appendReloptionsArrayAH (PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
 
static char * get_synchronized_snapshot (Archive *fout)
 
static void setupDumpWorker (Archive *AHX)
 
static TableInfogetRootTableInfo (const TableInfo *tbinfo)
 
int main (int argc, char **argv)
 
static bool checkExtensionMembership (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableNamespace (NamespaceInfo *nsinfo, Archive *fout)
 
static void selectDumpableTable (TableInfo *tbinfo, Archive *fout)
 
static void selectDumpableType (TypeInfo *tyinfo, Archive *fout)
 
static void selectDumpableDefaultACL (DefaultACLInfo *dinfo, DumpOptions *dopt)
 
static void selectDumpableCast (CastInfo *cast, Archive *fout)
 
static void selectDumpableProcLang (ProcLangInfo *plang, Archive *fout)
 
static void selectDumpableAccessMethod (AccessMethodInfo *method, Archive *fout)
 
static void selectDumpableExtension (ExtensionInfo *extinfo, DumpOptions *dopt)
 
static void selectDumpablePublicationTable (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableObject (DumpableObject *dobj, Archive *fout)
 
static int dumpTableData_copy (Archive *fout, const void *dcontext)
 
static int dumpTableData_insert (Archive *fout, const void *dcontext)
 
void getPolicies (Archive *fout, TableInfo tblinfo[], int numTables)
 
PublicationInfogetPublications (Archive *fout, int *numPublications)
 
void getPublicationTables (Archive *fout, TableInfo tblinfo[], int numTables)
 
static bool is_superuser (Archive *fout)
 
void getSubscriptions (Archive *fout)
 
static void append_depends_on_extension (Archive *fout, PQExpBuffer create, const DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
 
static Oid get_next_possible_free_pg_type_oid (Archive *fout, PQExpBuffer upgrade_query)
 
NamespaceInfogetNamespaces (Archive *fout, int *numNamespaces)
 
ExtensionInfogetExtensions (Archive *fout, int *numExtensions)
 
TypeInfogetTypes (Archive *fout, int *numTypes)
 
OprInfogetOperators (Archive *fout, int *numOprs)
 
CollInfogetCollations (Archive *fout, int *numCollations)
 
ConvInfogetConversions (Archive *fout, int *numConversions)
 
AccessMethodInfogetAccessMethods (Archive *fout, int *numAccessMethods)
 
OpclassInfogetOpclasses (Archive *fout, int *numOpclasses)
 
OpfamilyInfogetOpfamilies (Archive *fout, int *numOpfamilies)
 
AggInfogetAggregates (Archive *fout, int *numAggs)
 
FuncInfogetFuncs (Archive *fout, int *numFuncs)
 
TableInfogetTables (Archive *fout, int *numTables)
 
void getOwnedSeqs (Archive *fout, TableInfo tblinfo[], int numTables)
 
InhInfogetInherits (Archive *fout, int *numInherits)
 
void getIndexes (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getExtendedStatistics (Archive *fout)
 
void getConstraints (Archive *fout, TableInfo tblinfo[], int numTables)
 
RuleInfogetRules (Archive *fout, int *numRules)
 
void getTriggers (Archive *fout, TableInfo tblinfo[], int numTables)
 
EventTriggerInfogetEventTriggers (Archive *fout, int *numEventTriggers)
 
ProcLangInfogetProcLangs (Archive *fout, int *numProcLangs)
 
CastInfogetCasts (Archive *fout, int *numCasts)
 
static char * get_language_name (Archive *fout, Oid langid)
 
TransformInfogetTransforms (Archive *fout, int *numTransforms)
 
void getTableAttrs (Archive *fout, TableInfo *tblinfo, int numTables)
 
bool shouldPrintColumn (const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
 
TSParserInfogetTSParsers (Archive *fout, int *numTSParsers)
 
TSDictInfogetTSDictionaries (Archive *fout, int *numTSDicts)
 
TSTemplateInfogetTSTemplates (Archive *fout, int *numTSTemplates)
 
TSConfigInfogetTSConfigurations (Archive *fout, int *numTSConfigs)
 
FdwInfogetForeignDataWrappers (Archive *fout, int *numForeignDataWrappers)
 
ForeignServerInfogetForeignServers (Archive *fout, int *numForeignServers)
 
DefaultACLInfogetDefaultACLs (Archive *fout, int *numDefaultACLs)
 
static void dumpTableComment (Archive *fout, const TableInfo *tbinfo, const char *reltypename)
 
static char * format_aggregate_signature (const AggInfo *agginfo, Archive *fout, bool honor_quotes)
 
static void dumpTableSecLabel (Archive *fout, const TableInfo *tbinfo, const char *reltypename)
 
static PQExpBuffer createViewAsClause (Archive *fout, const TableInfo *tbinfo)
 
static PQExpBuffer createDummyViewAsClause (Archive *fout, const TableInfo *tbinfo)
 
void getExtensionMembership (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 
void processExtensionTables (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 

Variables

static bool dosync = true
 
static const char * username_subquery
 
static Oid g_last_builtin_oid
 
static int strict_names = 0
 
static SimpleStringList schema_include_patterns = {NULL, NULL}
 
static SimpleOidList schema_include_oids = {NULL, NULL}
 
static SimpleStringList schema_exclude_patterns = {NULL, NULL}
 
static SimpleOidList schema_exclude_oids = {NULL, NULL}
 
static SimpleStringList table_include_patterns = {NULL, NULL}
 
static SimpleOidList table_include_oids = {NULL, NULL}
 
static SimpleStringList table_exclude_patterns = {NULL, NULL}
 
static SimpleOidList table_exclude_oids = {NULL, NULL}
 
static SimpleStringList tabledata_exclude_patterns = {NULL, NULL}
 
static SimpleOidList tabledata_exclude_oids = {NULL, NULL}
 
static SimpleStringList foreign_servers_include_patterns = {NULL, NULL}
 
static SimpleOidList foreign_servers_include_oids = {NULL, NULL}
 
static SimpleStringList extension_include_patterns = {NULL, NULL}
 
static SimpleOidList extension_include_oids = {NULL, NULL}
 
static const CatalogId nilCatalogId = {0, 0}
 
static bool have_extra_float_digits = false
 
static int extra_float_digits
 

Macro Definition Documentation

◆ DUMP_DEFAULT_ROWS_PER_INSERT

#define DUMP_DEFAULT_ROWS_PER_INSERT   1

Definition at line 139 of file pg_dump.c.

Referenced by main().

◆ fmtQualifiedDumpable

Typedef Documentation

◆ OidOptions

typedef enum OidOptions OidOptions

Enumeration Type Documentation

◆ OidOptions

enum OidOptions
Enumerator
zeroIsError 
zeroAsStar 
zeroAsNone 

Definition at line 84 of file pg_dump.c.

85 {
86  zeroIsError = 1,
87  zeroAsStar = 2,
88  zeroAsNone = 4
89 } OidOptions;
OidOptions
Definition: pg_dump.c:84

Function Documentation

◆ addBoundaryDependencies()

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

Definition at line 18607 of file pg_dump.c.

References addObjectDependency(), DO_ACCESS_METHOD, DO_AGG, DO_ATTRDEF, DO_BLOB, DO_BLOB_DATA, 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_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_REFRESH_MATVIEW, DO_RULE, DO_SEQUENCE_SET, DO_SHELL_TYPE, DO_STATSEXT, DO_SUBSCRIPTION, DO_TABLE, DO_TABLE_ATTACH, DO_TABLE_DATA, DO_TRANSFORM, DO_TRIGGER, DO_TSCONFIG, DO_TSDICT, DO_TSPARSER, DO_TSTEMPLATE, DO_TYPE, _dumpableObject::dumpId, i, and _dumpableObject::objType.

Referenced by main().

18609 {
18610  DumpableObject *preDataBound = boundaryObjs + 0;
18611  DumpableObject *postDataBound = boundaryObjs + 1;
18612  int i;
18613 
18614  for (i = 0; i < numObjs; i++)
18615  {
18616  DumpableObject *dobj = dobjs[i];
18617 
18618  /*
18619  * The classification of object types here must match the SECTION_xxx
18620  * values assigned during subsequent ArchiveEntry calls!
18621  */
18622  switch (dobj->objType)
18623  {
18624  case DO_NAMESPACE:
18625  case DO_EXTENSION:
18626  case DO_TYPE:
18627  case DO_SHELL_TYPE:
18628  case DO_FUNC:
18629  case DO_AGG:
18630  case DO_OPERATOR:
18631  case DO_ACCESS_METHOD:
18632  case DO_OPCLASS:
18633  case DO_OPFAMILY:
18634  case DO_COLLATION:
18635  case DO_CONVERSION:
18636  case DO_TABLE:
18637  case DO_TABLE_ATTACH:
18638  case DO_ATTRDEF:
18639  case DO_PROCLANG:
18640  case DO_CAST:
18641  case DO_DUMMY_TYPE:
18642  case DO_TSPARSER:
18643  case DO_TSDICT:
18644  case DO_TSTEMPLATE:
18645  case DO_TSCONFIG:
18646  case DO_FDW:
18647  case DO_FOREIGN_SERVER:
18648  case DO_TRANSFORM:
18649  case DO_BLOB:
18650  /* Pre-data objects: must come before the pre-data boundary */
18651  addObjectDependency(preDataBound, dobj->dumpId);
18652  break;
18653  case DO_TABLE_DATA:
18654  case DO_SEQUENCE_SET:
18655  case DO_BLOB_DATA:
18656  /* Data objects: must come between the boundaries */
18657  addObjectDependency(dobj, preDataBound->dumpId);
18658  addObjectDependency(postDataBound, dobj->dumpId);
18659  break;
18660  case DO_INDEX:
18661  case DO_INDEX_ATTACH:
18662  case DO_STATSEXT:
18663  case DO_REFRESH_MATVIEW:
18664  case DO_TRIGGER:
18665  case DO_EVENT_TRIGGER:
18666  case DO_DEFAULT_ACL:
18667  case DO_POLICY:
18668  case DO_PUBLICATION:
18669  case DO_PUBLICATION_REL:
18670  case DO_SUBSCRIPTION:
18671  /* Post-data objects: must come after the post-data boundary */
18672  addObjectDependency(dobj, postDataBound->dumpId);
18673  break;
18674  case DO_RULE:
18675  /* Rules are post-data, but only if dumped separately */
18676  if (((RuleInfo *) dobj)->separate)
18677  addObjectDependency(dobj, postDataBound->dumpId);
18678  break;
18679  case DO_CONSTRAINT:
18680  case DO_FK_CONSTRAINT:
18681  /* Constraints are post-data, but only if dumped separately */
18682  if (((ConstraintInfo *) dobj)->separate)
18683  addObjectDependency(dobj, postDataBound->dumpId);
18684  break;
18685  case DO_PRE_DATA_BOUNDARY:
18686  /* nothing to do */
18687  break;
18688  case DO_POST_DATA_BOUNDARY:
18689  /* must come after the pre-data boundary */
18690  addObjectDependency(dobj, preDataBound->dumpId);
18691  break;
18692  }
18693  }
18694 }
DumpId dumpId
Definition: pg_dump.h:130
Definition: pg_dump.h:45
Definition: pg_dump.h:71
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:844
int i
DumpableObjectType objType
Definition: pg_dump.h:128

◆ addConstrChildIdxDeps()

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

Definition at line 7840 of file pg_dump.c.

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

Referenced by getConstraints().

7841 {
7842  SimplePtrListCell *cell;
7843 
7844  Assert(dobj->objType == DO_FK_CONSTRAINT);
7845 
7846  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7847  {
7848  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7849 
7850  addObjectDependency(dobj, attach->dobj.dumpId);
7851 
7852  if (attach->partitionIdx->partattaches.head != NULL)
7853  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7854  }
7855 }
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7840
DumpId dumpId
Definition: pg_dump.h:130
DumpableObject dobj
Definition: pg_dump.h:392
IndxInfo * partitionIdx
Definition: pg_dump.h:394
SimplePtrList partattaches
Definition: pg_dump.h:384
struct SimplePtrListCell * next
Definition: simple_list.h:48
#define Assert(condition)
Definition: c.h:804
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:844
SimplePtrListCell * head
Definition: simple_list.h:54
DumpableObjectType objType
Definition: pg_dump.h:128

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

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(), and PQntuples().

Referenced by dumpConstraint(), dumpFunc(), dumpIndex(), dumpTableSchema(), and dumpTrigger().

4573 {
4574  if (dobj->depends_on_ext)
4575  {
4576  char *nm;
4577  PGresult *res;
4578  PQExpBuffer query;
4579  int ntups;
4580  int i_extname;
4581  int i;
4582 
4583  /* dodge fmtId() non-reentrancy */
4584  nm = pg_strdup(objname);
4585 
4586  query = createPQExpBuffer();
4587  appendPQExpBuffer(query,
4588  "SELECT e.extname "
4589  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4590  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4591  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4592  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4593  catalog,
4594  dobj->catId.oid);
4595  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4596  ntups = PQntuples(res);
4597  i_extname = PQfnumber(res, "extname");
4598  for (i = 0; i < ntups; i++)
4599  {
4600  appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4601  keyword, nm,
4602  fmtId(PQgetvalue(res, i, i_extname)));
4603  }
4604 
4605  PQclear(res);
4606  destroyPQExpBuffer(query);
4607  pg_free(nm);
4608  }
4609 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
bool depends_on_ext
Definition: pg_dump.h:136
void PQclear(PGresult *res)
Definition: fe-exec.c:680
void pg_free(void *ptr)
Definition: fe_memutils.c:105
CatalogId catId
Definition: pg_dump.h:129
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ appendIndexCollationVersion()

static void appendIndexCollationVersion ( PQExpBuffer  buffer,
const IndxInfo indxinfo,
int  enc,
bool  coll_unknown,
Archive fout 
)
static

Definition at line 18906 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, _dumpableObject::catId, _indxInfo::dobj, fatal, free, i, _indxInfo::inddependcollnames, _indxInfo::inddependcollversions, CatalogId::oid, and parsePGArray().

Referenced by dumpIndex().

18908 {
18909  char *inddependcollnames = indxinfo->inddependcollnames;
18910  char *inddependcollversions = indxinfo->inddependcollversions;
18911  char **inddependcollnamesarray;
18912  char **inddependcollversionsarray;
18913  int ninddependcollnames;
18914  int ninddependcollversions;
18915 
18916  /*
18917  * By default, the new cluster's index will have pg_depends rows with
18918  * current collation versions, meaning that we assume the index isn't
18919  * corrupted if importing from a release that didn't record versions.
18920  * However, if --index-collation-versions-unknown was passed in, then we
18921  * assume such indexes might be corrupted, and clobber versions with
18922  * 'unknown' to trigger version warnings.
18923  */
18924  if (coll_unknown)
18925  {
18926  appendPQExpBuffer(buffer,
18927  "\n-- For binary upgrade, clobber new index's collation versions\n");
18928  appendPQExpBuffer(buffer,
18929  "UPDATE pg_catalog.pg_depend SET refobjversion = 'unknown' WHERE objid = '%u'::pg_catalog.oid AND refclassid = 'pg_catalog.pg_collation'::regclass AND refobjversion IS NOT NULL;\n",
18930  indxinfo->dobj.catId.oid);
18931  }
18932 
18933  /* Restore the versions that were recorded by the old cluster (if any). */
18934  if (strlen(inddependcollnames) == 0 && strlen(inddependcollversions) == 0)
18935  {
18936  ninddependcollnames = ninddependcollversions = 0;
18937  inddependcollnamesarray = inddependcollversionsarray = NULL;
18938  }
18939  else
18940  {
18941  if (!parsePGArray(inddependcollnames,
18942  &inddependcollnamesarray,
18943  &ninddependcollnames))
18944  fatal("could not parse index collation name array");
18945  if (!parsePGArray(inddependcollversions,
18946  &inddependcollversionsarray,
18947  &ninddependcollversions))
18948  fatal("could not parse index collation version array");
18949  }
18950 
18951  if (ninddependcollnames != ninddependcollversions)
18952  fatal("mismatched number of collation names and versions for index");
18953 
18954  if (ninddependcollnames > 0)
18955  appendPQExpBufferStr(buffer,
18956  "\n-- For binary upgrade, restore old index's collation versions\n");
18957  for (int i = 0; i < ninddependcollnames; i++)
18958  {
18959  /*
18960  * Import refobjversion from the old cluster, being careful to resolve
18961  * the collation OID by name in the new cluster.
18962  */
18963  appendPQExpBuffer(buffer,
18964  "UPDATE pg_catalog.pg_depend SET refobjversion = %s WHERE objid = '%u'::pg_catalog.oid AND refclassid = 'pg_catalog.pg_collation'::regclass AND refobjversion IS NOT NULL AND refobjid = ",
18965  inddependcollversionsarray[i],
18966  indxinfo->dobj.catId.oid);
18967  appendStringLiteralAH(buffer, inddependcollnamesarray[i], fout);
18968  appendPQExpBuffer(buffer, "::regcollation;\n");
18969  }
18970 
18971  if (inddependcollnamesarray)
18972  free(inddependcollnamesarray);
18973  if (inddependcollversionsarray)
18974  free(inddependcollversionsarray);
18975 }
bool parsePGArray(const char *atext, char ***itemarray, int *nitems)
Definition: string_utils.c:657
char * inddependcollnames
Definition: pg_dump.h:379
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:306
#define free(a)
Definition: header.h:65
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:129
DumpableObject dobj
Definition: pg_dump.h:368
int i
char * inddependcollversions
Definition: pg_dump.h:380

◆ appendReloptionsArrayAH()

static void appendReloptionsArrayAH ( PQExpBuffer  buffer,
const char *  reloptions,
const char *  prefix,
Archive fout 
)
static

Definition at line 18983 of file pg_dump.c.

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

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

18985 {
18986  bool res;
18987 
18988  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18989  fout->std_strings);
18990  if (!res)
18991  pg_log_warning("could not parse reloptions array");
18992 }
int encoding
Definition: pg_backup.h:207
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:741
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool std_strings
Definition: pg_backup.h:208

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

References appendPQExpBuffer(), appendPQExpBufferStr(), _dumpableObject::dependencies, DO_EXTENSION, _dumpableObject::ext_member, fatal, findObjectByDumpId(), fmtId(), i, _dumpableObject::name, _dumpableObject::nDeps, and _dumpableObject::objType.

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

4844 {
4845  DumpableObject *extobj = NULL;
4846  int i;
4847 
4848  if (!dobj->ext_member)
4849  return;
4850 
4851  /*
4852  * Find the parent extension. We could avoid this search if we wanted to
4853  * add a link field to DumpableObject, but the space costs of that would
4854  * be considerable. We assume that member objects could only have a
4855  * direct dependency on their own extension, not any others.
4856  */
4857  for (i = 0; i < dobj->nDeps; i++)
4858  {
4859  extobj = findObjectByDumpId(dobj->dependencies[i]);
4860  if (extobj && extobj->objType == DO_EXTENSION)
4861  break;
4862  extobj = NULL;
4863  }
4864  if (extobj == NULL)
4865  fatal("could not find parent extension for %s %s",
4866  objtype, objname);
4867 
4868  appendPQExpBufferStr(upgrade_buffer,
4869  "\n-- For binary upgrade, handle extension membership the hard way\n");
4870  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4871  fmtId(extobj->name),
4872  objtype);
4873  if (objnamespace && *objnamespace)
4874  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4875  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4876 }
char * name
Definition: pg_dump.h:131
DumpId * dependencies
Definition: pg_dump.h:137
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
bool ext_member
Definition: pg_dump.h:135
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define fatal(...)
int i
DumpableObjectType objType
Definition: pg_dump.h:128

◆ binary_upgrade_set_pg_class_oids()

static void binary_upgrade_set_pg_class_oids ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_class_oid,
bool  is_index 
)
static

Definition at line 4756 of file pg_dump.c.

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

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

4759 {
4760  appendPQExpBufferStr(upgrade_buffer,
4761  "\n-- For binary upgrade, must preserve pg_class oids\n");
4762 
4763  if (!is_index)
4764  {
4765  PQExpBuffer upgrade_query = createPQExpBuffer();
4766  PGresult *upgrade_res;
4767  Oid pg_class_reltoastrelid;
4768  char pg_class_relkind;
4769  Oid pg_index_indexrelid;
4770 
4771  appendPQExpBuffer(upgrade_buffer,
4772  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4773  pg_class_oid);
4774 
4775  /*
4776  * Preserve the OIDs of the table's toast table and index, if any.
4777  * Indexes cannot have toast tables, so we need not make this probe in
4778  * the index code path.
4779  *
4780  * One complexity is that the current table definition might not
4781  * require the creation of a TOAST table, but the old database might
4782  * have a TOAST table that was created earlier, before some wide
4783  * columns were dropped. By setting the TOAST oid we force creation
4784  * of the TOAST heap and index by the new backend, so we can copy the
4785  * files during binary upgrade without worrying about this case.
4786  */
4787  appendPQExpBuffer(upgrade_query,
4788  "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
4789  "FROM pg_catalog.pg_class c LEFT JOIN "
4790  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4791  "WHERE c.oid = '%u'::pg_catalog.oid;",
4792  pg_class_oid);
4793 
4794  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4795 
4796  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
4797  PQfnumber(upgrade_res, "reltoastrelid")));
4798  pg_class_relkind = *PQgetvalue(upgrade_res, 0,
4799  PQfnumber(upgrade_res, "relkind"));
4800  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
4801  PQfnumber(upgrade_res, "indexrelid")));
4802 
4803  /*
4804  * In a pre-v12 database, partitioned tables might be marked as having
4805  * toast tables, but we should ignore them if so.
4806  */
4807  if (OidIsValid(pg_class_reltoastrelid) &&
4808  pg_class_relkind != RELKIND_PARTITIONED_TABLE)
4809  {
4810  appendPQExpBuffer(upgrade_buffer,
4811  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4812  pg_class_reltoastrelid);
4813 
4814  /* every toast table has an index */
4815  appendPQExpBuffer(upgrade_buffer,
4816  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4817  pg_index_indexrelid);
4818  }
4819 
4820  PQclear(upgrade_res);
4821  destroyPQExpBuffer(upgrade_query);
4822  }
4823  else
4824  appendPQExpBuffer(upgrade_buffer,
4825  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4826  pg_class_oid);
4827 
4828  appendPQExpBufferChar(upgrade_buffer, '\n');
4829 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void PQclear(PGresult *res)
Definition: fe-exec.c:680

◆ binary_upgrade_set_type_oids_by_rel_oid()

static void binary_upgrade_set_type_oids_by_rel_oid ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_rel_oid 
)
static

Definition at line 4729 of file pg_dump.c.

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

Referenced by dumpTableSchema().

4732 {
4733  PQExpBuffer upgrade_query = createPQExpBuffer();
4734  PGresult *upgrade_res;
4735  Oid pg_type_oid;
4736 
4737  appendPQExpBuffer(upgrade_query,
4738  "SELECT c.reltype AS crel "
4739  "FROM pg_catalog.pg_class c "
4740  "WHERE c.oid = '%u'::pg_catalog.oid;",
4741  pg_rel_oid);
4742 
4743  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4744 
4745  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4746 
4747  if (OidIsValid(pg_type_oid))
4748  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4749  pg_type_oid, false, false);
4750 
4751  PQclear(upgrade_res);
4752  destroyPQExpBuffer(upgrade_query);
4753 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
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:4643
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
void PQclear(PGresult *res)
Definition: fe-exec.c:680

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

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

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

4648 {
4649  PQExpBuffer upgrade_query = createPQExpBuffer();
4650  PGresult *res;
4651  Oid pg_type_array_oid;
4652  Oid pg_type_multirange_oid;
4653  Oid pg_type_multirange_array_oid;
4654 
4655  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4656  appendPQExpBuffer(upgrade_buffer,
4657  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4658  pg_type_oid);
4659 
4660  /* we only support old >= 8.3 for binary upgrades */
4661  appendPQExpBuffer(upgrade_query,
4662  "SELECT typarray "
4663  "FROM pg_catalog.pg_type "
4664  "WHERE oid = '%u'::pg_catalog.oid;",
4665  pg_type_oid);
4666 
4667  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4668 
4669  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4670 
4671  PQclear(res);
4672 
4673  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4674  pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4675 
4676  if (OidIsValid(pg_type_array_oid))
4677  {
4678  appendPQExpBufferStr(upgrade_buffer,
4679  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4680  appendPQExpBuffer(upgrade_buffer,
4681  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4682  pg_type_array_oid);
4683  }
4684 
4685  /*
4686  * Pre-set the multirange type oid and its own array type oid.
4687  */
4688  if (include_multirange_type)
4689  {
4690  if (fout->remoteVersion >= 140000)
4691  {
4692  appendPQExpBuffer(upgrade_query,
4693  "SELECT t.oid, t.typarray "
4694  "FROM pg_catalog.pg_type t "
4695  "JOIN pg_catalog.pg_range r "
4696  "ON t.oid = r.rngmultitypid "
4697  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
4698  pg_type_oid);
4699 
4700  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4701 
4702  pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
4703  pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4704 
4705  PQclear(res);
4706  }
4707  else
4708  {
4709  pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4710  pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4711  }
4712 
4713  appendPQExpBufferStr(upgrade_buffer,
4714  "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
4715  appendPQExpBuffer(upgrade_buffer,
4716  "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4717  pg_type_multirange_oid);
4718  appendPQExpBufferStr(upgrade_buffer,
4719  "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
4720  appendPQExpBuffer(upgrade_buffer,
4721  "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4722  pg_type_multirange_array_oid);
4723  }
4724 
4725  destroyPQExpBuffer(upgrade_query);
4726 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
static Oid get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
Definition: pg_dump.c:4612
void PQclear(PGresult *res)
Definition: fe-exec.c:680
int remoteVersion
Definition: pg_backup.h:197

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

Definition at line 18721 of file pg_dump.c.

References _tocEntry::dependencies, _tocEntry::dumpId, findDumpableDependencies(), findObjectByDumpId(), free, _dumpableObject::nDeps, _tocEntry::nDeps, _tocEntry::next, pg_malloc(), pg_realloc(), _tocEntry::reqs, and _archiveHandle::toc.

Referenced by main().

18722 {
18723  ArchiveHandle *AH = (ArchiveHandle *) fout;
18724  TocEntry *te;
18725 
18726  /* Scan all TOC entries in the archive */
18727  for (te = AH->toc->next; te != AH->toc; te = te->next)
18728  {
18729  DumpableObject *dobj;
18730  DumpId *dependencies;
18731  int nDeps;
18732  int allocDeps;
18733 
18734  /* No need to process entries that will not be dumped */
18735  if (te->reqs == 0)
18736  continue;
18737  /* Ignore entries that already have "special" dependencies */
18738  if (te->nDeps > 0)
18739  continue;
18740  /* Otherwise, look up the item's original DumpableObject, if any */
18741  dobj = findObjectByDumpId(te->dumpId);
18742  if (dobj == NULL)
18743  continue;
18744  /* No work if it has no dependencies */
18745  if (dobj->nDeps <= 0)
18746  continue;
18747  /* Set up work array */
18748  allocDeps = 64;
18749  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18750  nDeps = 0;
18751  /* Recursively find all dumpable dependencies */
18752  findDumpableDependencies(AH, dobj,
18753  &dependencies, &nDeps, &allocDeps);
18754  /* And save 'em ... */
18755  if (nDeps > 0)
18756  {
18757  dependencies = (DumpId *) pg_realloc(dependencies,
18758  nDeps * sizeof(DumpId));
18759  te->dependencies = dependencies;
18760  te->nDeps = nDeps;
18761  }
18762  else
18763  free(dependencies);
18764  }
18765 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:246
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:669
struct _tocEntry * toc
DumpId * dependencies
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define free(a)
Definition: header.h:65
static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:18769

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

Definition at line 2567 of file pg_dump.c.

References addObjectDependency(), appendPQExpBufferStr(), Assert, atooid, CppAsString2, createPQExpBuffer(), PQExpBufferData::data, _tableInfo::dataObj, destroyPQExpBuffer(), DO_REFRESH_MATVIEW, DO_TABLE, _dumpableObject::dumpId, ExecuteSqlQuery(), findObjectByCatalogId(), i, _dumpableObject::objType, CatalogId::oid, PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), _tableInfo::relispopulated, _tableInfo::relkind, Archive::remoteVersion, and CatalogId::tableoid.

Referenced by main().

2568 {
2569  PQExpBuffer query;
2570  PGresult *res;
2571  int ntups,
2572  i;
2573  int i_classid,
2574  i_objid,
2575  i_refobjid;
2576 
2577  /* No Mat Views before 9.3. */
2578  if (fout->remoteVersion < 90300)
2579  return;
2580 
2581  query = createPQExpBuffer();
2582 
2583  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2584  "( "
2585  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2586  "FROM pg_depend d1 "
2587  "JOIN pg_class c1 ON c1.oid = d1.objid "
2588  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2589  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2590  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2591  "AND d2.objid = r1.oid "
2592  "AND d2.refobjid <> d1.objid "
2593  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2594  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2595  CppAsString2(RELKIND_VIEW) ") "
2596  "WHERE d1.classid = 'pg_class'::regclass "
2597  "UNION "
2598  "SELECT w.objid, d3.refobjid, c3.relkind "
2599  "FROM w "
2600  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2601  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2602  "AND d3.objid = r3.oid "
2603  "AND d3.refobjid <> w.refobjid "
2604  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2605  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2606  CppAsString2(RELKIND_VIEW) ") "
2607  ") "
2608  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2609  "FROM w "
2610  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2611 
2612  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2613 
2614  ntups = PQntuples(res);
2615 
2616  i_classid = PQfnumber(res, "classid");
2617  i_objid = PQfnumber(res, "objid");
2618  i_refobjid = PQfnumber(res, "refobjid");
2619 
2620  for (i = 0; i < ntups; i++)
2621  {
2622  CatalogId objId;
2623  CatalogId refobjId;
2624  DumpableObject *dobj;
2625  DumpableObject *refdobj;
2626  TableInfo *tbinfo;
2627  TableInfo *reftbinfo;
2628 
2629  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2630  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2631  refobjId.tableoid = objId.tableoid;
2632  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2633 
2634  dobj = findObjectByCatalogId(objId);
2635  if (dobj == NULL)
2636  continue;
2637 
2638  Assert(dobj->objType == DO_TABLE);
2639  tbinfo = (TableInfo *) dobj;
2640  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2641  dobj = (DumpableObject *) tbinfo->dataObj;
2642  if (dobj == NULL)
2643  continue;
2644  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2645 
2646  refdobj = findObjectByCatalogId(refobjId);
2647  if (refdobj == NULL)
2648  continue;
2649 
2650  Assert(refdobj->objType == DO_TABLE);
2651  reftbinfo = (TableInfo *) refdobj;
2652  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2653  refdobj = (DumpableObject *) reftbinfo->dataObj;
2654  if (refdobj == NULL)
2655  continue;
2656  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2657 
2658  addObjectDependency(dobj, refdobj->dumpId);
2659 
2660  if (!reftbinfo->relispopulated)
2661  tbinfo->relispopulated = false;
2662  }
2663 
2664  PQclear(res);
2665 
2666  destroyPQExpBuffer(query);
2667 }
char relkind
Definition: pg_dump.h:267
Oid tableoid
Definition: pg_backup.h:242
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpId dumpId
Definition: pg_dump.h:130
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:687
#define atooid(x)
Definition: postgres_ext.h:42
#define CppAsString2(x)
Definition: c.h:289
struct _tableDataInfo * dataObj
Definition: pg_dump.h:338
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define Assert(condition)
Definition: c.h:804
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:844
int i
bool relispopulated
Definition: pg_dump.h:269
DumpableObjectType objType
Definition: pg_dump.h:128
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294
int remoteVersion
Definition: pg_backup.h:197

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

Definition at line 1563 of file pg_dump.c.

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

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

1564 {
1565  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1566 
1567  if (ext == NULL)
1568  return false;
1569 
1570  dobj->ext_member = true;
1571 
1572  /* Record dependency so that getDependencies needn't deal with that */
1573  addObjectDependency(dobj, ext->dobj.dumpId);
1574 
1575  /*
1576  * In 9.6 and above, mark the member object to have any non-initial ACL,
1577  * policies, and security labels dumped.
1578  *
1579  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1580  * extract the information about the object. We don't provide support for
1581  * initial policies and security labels and it seems unlikely for those to
1582  * ever exist, but we may have to revisit this later.
1583  *
1584  * Prior to 9.6, we do not include any extension member components.
1585  *
1586  * In binary upgrades, we still dump all components of the members
1587  * individually, since the idea is to exactly reproduce the database
1588  * contents rather than replace the extension contents with something
1589  * different.
1590  */
1591  if (fout->dopt->binary_upgrade)
1592  dobj->dump = ext->dobj.dump;
1593  else
1594  {
1595  if (fout->remoteVersion < 90600)
1596  dobj->dump = DUMP_COMPONENT_NONE;
1597  else
1598  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1601  }
1602 
1603  return true;
1604 }
DumpableObject dobj
Definition: pg_dump.h:154
DumpComponents dump
Definition: pg_dump.h:132
DumpId dumpId
Definition: pg_dump.h:130
bool ext_member
Definition: pg_dump.h:135
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:89
int binary_upgrade
Definition: pg_backup.h:143
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:1007
DumpOptions * dopt
Definition: pg_backup.h:192
DumpComponents dump_contains
Definition: pg_dump.h:134
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:844
CatalogId catId
Definition: pg_dump.h:129
#define DUMP_COMPONENT_POLICY
Definition: pg_dump.h:95
int remoteVersion
Definition: pg_backup.h:197

◆ collectComments()

static int collectComments ( Archive fout,
CommentItem **  items 
)
static

Definition at line 10265 of file pg_dump.c.

References appendPQExpBufferStr(), atooid, CommentItem::classoid, createPQExpBuffer(), PQExpBufferData::data, CommentItem::descr, destroyPQExpBuffer(), ExecuteSqlQuery(), i, CommentItem::objoid, CommentItem::objsubid, pg_malloc(), PGRES_TUPLES_OK, PQfnumber(), PQgetvalue(), and PQntuples().

Referenced by findComments().

10266 {
10267  PGresult *res;
10268  PQExpBuffer query;
10269  int i_description;
10270  int i_classoid;
10271  int i_objoid;
10272  int i_objsubid;
10273  int ntups;
10274  int i;
10275  CommentItem *comments;
10276 
10277  query = createPQExpBuffer();
10278 
10279  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
10280  "FROM pg_catalog.pg_description "
10281  "ORDER BY classoid, objoid, objsubid");
10282 
10283  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10284 
10285  /* Construct lookup table containing OIDs in numeric form */
10286 
10287  i_description = PQfnumber(res, "description");
10288  i_classoid = PQfnumber(res, "classoid");
10289  i_objoid = PQfnumber(res, "objoid");
10290  i_objsubid = PQfnumber(res, "objsubid");
10291 
10292  ntups = PQntuples(res);
10293 
10294  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10295 
10296  for (i = 0; i < ntups; i++)
10297  {
10298  comments[i].descr = PQgetvalue(res, i, i_description);
10299  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
10300  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
10301  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
10302  }
10303 
10304  /* Do NOT free the PGresult since we are keeping pointers into it */
10305  destroyPQExpBuffer(query);
10306 
10307  *items = comments;
10308  return ntups;
10309 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:72
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:69
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
Oid objoid
Definition: pg_dump.c:71
Oid classoid
Definition: pg_dump.c:70
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ collectSecLabels()

static int collectSecLabels ( Archive fout,
SecLabelItem **  items 
)
static

Definition at line 15570 of file pg_dump.c.

References appendPQExpBufferStr(), atooid, SecLabelItem::classoid, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), ExecuteSqlQuery(), i, SecLabelItem::label, SecLabelItem::objoid, SecLabelItem::objsubid, pg_malloc(), PGRES_TUPLES_OK, PQfnumber(), PQgetvalue(), PQntuples(), and SecLabelItem::provider.

Referenced by findSecLabels().

15571 {
15572  PGresult *res;
15573  PQExpBuffer query;
15574  int i_label;
15575  int i_provider;
15576  int i_classoid;
15577  int i_objoid;
15578  int i_objsubid;
15579  int ntups;
15580  int i;
15581  SecLabelItem *labels;
15582 
15583  query = createPQExpBuffer();
15584 
15585  appendPQExpBufferStr(query,
15586  "SELECT label, provider, classoid, objoid, objsubid "
15587  "FROM pg_catalog.pg_seclabel "
15588  "ORDER BY classoid, objoid, objsubid");
15589 
15590  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15591 
15592  /* Construct lookup table containing OIDs in numeric form */
15593  i_label = PQfnumber(res, "label");
15594  i_provider = PQfnumber(res, "provider");
15595  i_classoid = PQfnumber(res, "classoid");
15596  i_objoid = PQfnumber(res, "objoid");
15597  i_objsubid = PQfnumber(res, "objsubid");
15598 
15599  ntups = PQntuples(res);
15600 
15601  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15602 
15603  for (i = 0; i < ntups; i++)
15604  {
15605  labels[i].label = PQgetvalue(res, i, i_label);
15606  labels[i].provider = PQgetvalue(res, i, i_provider);
15607  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15608  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15609  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15610  }
15611 
15612  /* Do NOT free the PGresult since we are keeping pointers into it */
15613  destroyPQExpBuffer(query);
15614 
15615  *items = labels;
15616  return ntups;
15617 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
Oid objoid
Definition: pg_dump.c:80
const char * provider
Definition: pg_dump.c:77
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
int objsubid
Definition: pg_dump.c:81
int i
Oid classoid
Definition: pg_dump.c:79
const char * label
Definition: pg_dump.c:78
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ convertRegProcReference()

static char * convertRegProcReference ( const char *  proc)
static

Definition at line 13109 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

13110 {
13111  char *name;
13112  char *paren;
13113  bool inquote;
13114 
13115  /* In all cases "-" means a null reference */
13116  if (strcmp(proc, "-") == 0)
13117  return NULL;
13118 
13119  name = pg_strdup(proc);
13120  /* find non-double-quoted left paren */
13121  inquote = false;
13122  for (paren = name; *paren; paren++)
13123  {
13124  if (*paren == '(' && !inquote)
13125  {
13126  *paren = '\0';
13127  break;
13128  }
13129  if (*paren == '"')
13130  inquote = !inquote;
13131  }
13132  return name;
13133 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * name
Definition: encode.c:515

◆ convertTSFunction()

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

Definition at line 13180 of file pg_dump.c.

References ExecuteSqlQueryForSingleRow(), pg_strdup(), PQclear(), PQgetvalue(), and snprintf.

Referenced by dumpTSParser(), and dumpTSTemplate().

13181 {
13182  char *result;
13183  char query[128];
13184  PGresult *res;
13185 
13186  snprintf(query, sizeof(query),
13187  "SELECT '%u'::pg_catalog.regproc", funcOid);
13188  res = ExecuteSqlQueryForSingleRow(fout, query);
13189 
13190  result = pg_strdup(PQgetvalue(res, 0, 0));
13191 
13192  PQclear(res);
13193 
13194  return result;
13195 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define snprintf
Definition: port.h:216

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

Definition at line 18583 of file pg_dump.c.

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

18584 {
18585  DumpableObject *dobjs;
18586 
18587  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18588 
18589  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18590  dobjs[0].catId = nilCatalogId;
18591  AssignDumpId(dobjs + 0);
18592  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18593 
18594  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18595  dobjs[1].catId = nilCatalogId;
18596  AssignDumpId(dobjs + 1);
18597  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18598 
18599  return dobjs;
18600 }
char * name
Definition: pg_dump.h:131
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:603
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
CatalogId catId
Definition: pg_dump.h:129
static const CatalogId nilCatalogId
Definition: pg_dump.c:129
DumpableObjectType objType
Definition: pg_dump.h:128

◆ createDummyViewAsClause()

static PQExpBuffer createDummyViewAsClause ( Archive fout,
const TableInfo tbinfo 
)
static

Definition at line 15813 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), _tableInfo::attcollation, _tableInfo::attnames, _tableInfo::atttypnames, createPQExpBuffer(), findCollationByOid(), fmtId(), fmtQualifiedDumpable, _tableInfo::numatts, and OidIsValid.

Referenced by dumpRule(), and dumpTableSchema().

15814 {
15815  PQExpBuffer result = createPQExpBuffer();
15816  int j;
15817 
15818  appendPQExpBufferStr(result, "SELECT");
15819 
15820  for (j = 0; j < tbinfo->numatts; j++)
15821  {
15822  if (j > 0)
15823  appendPQExpBufferChar(result, ',');
15824  appendPQExpBufferStr(result, "\n ");
15825 
15826  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15827 
15828  /*
15829  * Must add collation if not default for the type, because CREATE OR
15830  * REPLACE VIEW won't change it
15831  */
15832  if (OidIsValid(tbinfo->attcollation[j]))
15833  {
15834  CollInfo *coll;
15835 
15836  coll = findCollationByOid(tbinfo->attcollation[j]);
15837  if (coll)
15838  appendPQExpBuffer(result, " COLLATE %s",
15839  fmtQualifiedDumpable(coll));
15840  }
15841 
15842  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15843  }
15844 
15845  return result;
15846 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char ** atttypnames
Definition: pg_dump.h:306
char ** attnames
Definition: pg_dump.h:305
#define OidIsValid(objectId)
Definition: c.h:710
Oid * attcollation
Definition: pg_dump.h:318
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
int numatts
Definition: pg_dump.h:304
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:144
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:935

◆ createViewAsClause()

static PQExpBuffer createViewAsClause ( Archive fout,
const TableInfo tbinfo 
)
static

Definition at line 15764 of file pg_dump.c.

References appendBinaryPQExpBuffer(), appendPQExpBuffer(), Assert, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _tableInfo::dobj, ExecuteSqlQuery(), fatal, _dumpableObject::name, CatalogId::oid, PGRES_TUPLES_OK, PQclear(), PQgetlength(), PQgetvalue(), and PQntuples().

Referenced by dumpRule(), and dumpTableSchema().

15765 {
15766  PQExpBuffer query = createPQExpBuffer();
15767  PQExpBuffer result = createPQExpBuffer();
15768  PGresult *res;
15769  int len;
15770 
15771  /* Fetch the view definition */
15772  appendPQExpBuffer(query,
15773  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15774  tbinfo->dobj.catId.oid);
15775 
15776  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15777 
15778  if (PQntuples(res) != 1)
15779  {
15780  if (PQntuples(res) < 1)
15781  fatal("query to obtain definition of view \"%s\" returned no data",
15782  tbinfo->dobj.name);
15783  else
15784  fatal("query to obtain definition of view \"%s\" returned more than one definition",
15785  tbinfo->dobj.name);
15786  }
15787 
15788  len = PQgetlength(res, 0, 0);
15789 
15790  if (len == 0)
15791  fatal("definition of view \"%s\" appears to be empty (length zero)",
15792  tbinfo->dobj.name);
15793 
15794  /* Strip off the trailing semicolon so that other things may follow. */
15795  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15796  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15797 
15798  PQclear(res);
15799  destroyPQExpBuffer(query);
15800 
15801  return result;
15802 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3572
char * name
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
DumpableObject dobj
Definition: pg_dump.h:261
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define Assert(condition)
Definition: c.h:804
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:129
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:399
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ dumpAccessMethod()

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

Definition at line 13202 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

13203 {
13204  DumpOptions *dopt = fout->dopt;
13205  PQExpBuffer q;
13206  PQExpBuffer delq;
13207  char *qamname;
13208 
13209  /* Skip if not to be dumped */
13210  if (!aminfo->dobj.dump || dopt->dataOnly)
13211  return;
13212 
13213  q = createPQExpBuffer();
13214  delq = createPQExpBuffer();
13215 
13216  qamname = pg_strdup(fmtId(aminfo->dobj.name));
13217 
13218  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
13219 
13220  switch (aminfo->amtype)
13221  {
13222  case AMTYPE_INDEX:
13223  appendPQExpBufferStr(q, "TYPE INDEX ");
13224  break;
13225  case AMTYPE_TABLE:
13226  appendPQExpBufferStr(q, "TYPE TABLE ");
13227  break;
13228  default:
13229  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
13230  aminfo->amtype, qamname);
13231  destroyPQExpBuffer(q);
13232  destroyPQExpBuffer(delq);
13233  free(qamname);
13234  return;
13235  }
13236 
13237  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
13238 
13239  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
13240  qamname);
13241 
13242  if (dopt->binary_upgrade)
13243  binary_upgrade_extension_member(q, &aminfo->dobj,
13244  "ACCESS METHOD", qamname, NULL);
13245 
13246  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13247  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
13248  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
13249  .description = "ACCESS METHOD",
13250  .section = SECTION_PRE_DATA,
13251  .createStmt = q->data,
13252  .dropStmt = delq->data));
13253 
13254  /* Dump Access Method Comments */
13255  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13256  dumpComment(fout, "ACCESS METHOD", qamname,
13257  NULL, "",
13258  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
13259 
13260  destroyPQExpBuffer(q);
13261  destroyPQExpBuffer(delq);
13262  free(qamname);
13263 }
#define ARCHIVE_OPTS(...)
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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:4839
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
#define pg_log_warning(...)
Definition: pgfnames.c:24
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:10002

◆ dumpACL()

static DumpId dumpACL ( Archive fout,
DumpId  objDumpId,
DumpId  altDumpId,
const char *  type,
const char *  name,
const char *  subname,
const char *  nspname,
const char *  owner,
const char *  acls,
const char *  racls,
const char *  initacls,
const char *  initracls 
)
static

Definition at line 15224 of file pg_dump.c.

References _dumpOptions::aclsSkip, appendPQExpBuffer(), appendPQExpBufferStr(), ARCHIVE_OPTS, ArchiveEntry(), buildACLCommands(), createDumpId(), createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), Archive::dopt, fatal, InvalidDumpId, PQExpBufferData::len, Archive::remoteVersion, and SECTION_NONE.

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

15229 {
15230  DumpId aclDumpId = InvalidDumpId;
15231  DumpOptions *dopt = fout->dopt;
15232  PQExpBuffer sql;
15233 
15234  /* Do nothing if ACL dump is not enabled */
15235  if (dopt->aclsSkip)
15236  return InvalidDumpId;
15237 
15238  /* --data-only skips ACLs *except* BLOB ACLs */
15239  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
15240  return InvalidDumpId;
15241 
15242  sql = createPQExpBuffer();
15243 
15244  /*
15245  * Check to see if this object has had any initial ACLs included for it.
15246  * If so, we are in binary upgrade mode and these are the ACLs to turn
15247  * into GRANT and REVOKE statements to set and record the initial
15248  * privileges for an extension object. Let the backend know that these
15249  * are to be recorded by calling binary_upgrade_set_record_init_privs()
15250  * before and after.
15251  */
15252  if (strlen(initacls) != 0 || strlen(initracls) != 0)
15253  {
15254  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
15255  if (!buildACLCommands(name, subname, nspname, type,
15256  initacls, initracls, owner,
15257  "", fout->remoteVersion, sql))
15258  fatal("could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)",
15259  initacls, initracls, name, type);
15260  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
15261  }
15262 
15263  if (!buildACLCommands(name, subname, nspname, type,
15264  acls, racls, owner,
15265  "", fout->remoteVersion, sql))
15266  fatal("could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)",
15267  acls, racls, name, type);
15268 
15269  if (sql->len > 0)
15270  {
15272  DumpId aclDeps[2];
15273  int nDeps = 0;
15274 
15275  if (subname)
15276  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
15277  else
15278  appendPQExpBuffer(tag, "%s %s", type, name);
15279 
15280  aclDeps[nDeps++] = objDumpId;
15281  if (altDumpId != InvalidDumpId)
15282  aclDeps[nDeps++] = altDumpId;
15283 
15284  aclDumpId = createDumpId();
15285 
15286  ArchiveEntry(fout, nilCatalogId, aclDumpId,
15287  ARCHIVE_OPTS(.tag = tag->data,
15288  .namespace = nspname,
15289  .owner = owner,
15290  .description = "ACL",
15291  .section = SECTION_NONE,
15292  .createStmt = sql->data,
15293  .deps = aclDeps,
15294  .nDeps = nDeps));
15295 
15296  destroyPQExpBuffer(tag);
15297  }
15298 
15299  destroyPQExpBuffer(sql);
15300 
15301  return aclDumpId;
15302 }
#define ARCHIVE_OPTS(...)
int DumpId
Definition: pg_backup.h:246
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
NameData subname
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
bool aclsSkip
Definition: pg_backup.h:149
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define InvalidDumpId
Definition: pg_backup.h:248
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:58
#define fatal(...)
const char * name
Definition: encode.c:515
static const CatalogId nilCatalogId
Definition: pg_dump.c:129
DumpId createDumpId(void)
Definition: common.c:649
int remoteVersion
Definition: pg_backup.h:197

◆ dumpAgg()

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

Definition at line 14170 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

14171 {
14172  DumpOptions *dopt = fout->dopt;
14173  PQExpBuffer query;
14174  PQExpBuffer q;
14175  PQExpBuffer delq;
14176  PQExpBuffer details;
14177  char *aggsig; /* identity signature */
14178  char *aggfullsig = NULL; /* full signature */
14179  char *aggsig_tag;
14180  PGresult *res;
14181  int i_agginitval;
14182  int i_aggminitval;
14183  const char *aggtransfn;
14184  const char *aggfinalfn;
14185  const char *aggcombinefn;
14186  const char *aggserialfn;
14187  const char *aggdeserialfn;
14188  const char *aggmtransfn;
14189  const char *aggminvtransfn;
14190  const char *aggmfinalfn;
14191  bool aggfinalextra;
14192  bool aggmfinalextra;
14193  char aggfinalmodify;
14194  char aggmfinalmodify;
14195  const char *aggsortop;
14196  char *aggsortconvop;
14197  char aggkind;
14198  const char *aggtranstype;
14199  const char *aggtransspace;
14200  const char *aggmtranstype;
14201  const char *aggmtransspace;
14202  const char *agginitval;
14203  const char *aggminitval;
14204  const char *proparallel;
14205  char defaultfinalmodify;
14206 
14207  /* Skip if not to be dumped */
14208  if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
14209  return;
14210 
14211  query = createPQExpBuffer();
14212  q = createPQExpBuffer();
14213  delq = createPQExpBuffer();
14214  details = createPQExpBuffer();
14215 
14216  /* Get aggregate-specific details */
14217  appendPQExpBufferStr(query,
14218  "SELECT\n"
14219  "aggtransfn,\n"
14220  "aggfinalfn,\n"
14221  "aggtranstype::pg_catalog.regtype,\n"
14222  "agginitval,\n");
14223 
14224  if (fout->remoteVersion >= 80100)
14225  appendPQExpBufferStr(query,
14226  "aggsortop,\n");
14227  else
14228  appendPQExpBufferStr(query,
14229  "0 AS aggsortop,\n");
14230 
14231  if (fout->remoteVersion >= 80400)
14232  appendPQExpBufferStr(query,
14233  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
14234  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
14235 
14236  if (fout->remoteVersion >= 90400)
14237  appendPQExpBufferStr(query,
14238  "aggkind,\n"
14239  "aggmtransfn,\n"
14240  "aggminvtransfn,\n"
14241  "aggmfinalfn,\n"
14242  "aggmtranstype::pg_catalog.regtype,\n"
14243  "aggfinalextra,\n"
14244  "aggmfinalextra,\n"
14245  "aggtransspace,\n"
14246  "aggmtransspace,\n"
14247  "aggminitval,\n");
14248  else
14249  appendPQExpBufferStr(query,
14250  "'n' AS aggkind,\n"
14251  "'-' AS aggmtransfn,\n"
14252  "'-' AS aggminvtransfn,\n"
14253  "'-' AS aggmfinalfn,\n"
14254  "0 AS aggmtranstype,\n"
14255  "false AS aggfinalextra,\n"
14256  "false AS aggmfinalextra,\n"
14257  "0 AS aggtransspace,\n"
14258  "0 AS aggmtransspace,\n"
14259  "NULL AS aggminitval,\n");
14260 
14261  if (fout->remoteVersion >= 90600)
14262  appendPQExpBufferStr(query,
14263  "aggcombinefn,\n"
14264  "aggserialfn,\n"
14265  "aggdeserialfn,\n"
14266  "proparallel,\n");
14267  else
14268  appendPQExpBufferStr(query,
14269  "'-' AS aggcombinefn,\n"
14270  "'-' AS aggserialfn,\n"
14271  "'-' AS aggdeserialfn,\n"
14272  "'u' AS proparallel,\n");
14273 
14274  if (fout->remoteVersion >= 110000)
14275  appendPQExpBufferStr(query,
14276  "aggfinalmodify,\n"
14277  "aggmfinalmodify\n");
14278  else
14279  appendPQExpBufferStr(query,
14280  "'0' AS aggfinalmodify,\n"
14281  "'0' AS aggmfinalmodify\n");
14282 
14283  appendPQExpBuffer(query,
14284  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14285  "WHERE a.aggfnoid = p.oid "
14286  "AND p.oid = '%u'::pg_catalog.oid",
14287  agginfo->aggfn.dobj.catId.oid);
14288 
14289  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14290 
14291  i_agginitval = PQfnumber(res, "agginitval");
14292  i_aggminitval = PQfnumber(res, "aggminitval");
14293 
14294  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
14295  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
14296  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
14297  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
14298  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
14299  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
14300  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
14301  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
14302  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
14303  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
14304  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
14305  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
14306  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
14307  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
14308  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
14309  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
14310  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
14311  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
14312  agginitval = PQgetvalue(res, 0, i_agginitval);
14313  aggminitval = PQgetvalue(res, 0, i_aggminitval);
14314  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
14315 
14316  if (fout->remoteVersion >= 80400)
14317  {
14318  /* 8.4 or later; we rely on server-side code for most of the work */
14319  char *funcargs;
14320  char *funciargs;
14321 
14322  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
14323  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
14324  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
14325  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
14326  }
14327  else
14328  /* pre-8.4, do it ourselves */
14329  aggsig = format_aggregate_signature(agginfo, fout, true);
14330 
14331  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14332 
14333  /* identify default modify flag for aggkind (must match DefineAggregate) */
14334  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14335  /* replace omitted flags for old versions */
14336  if (aggfinalmodify == '0')
14337  aggfinalmodify = defaultfinalmodify;
14338  if (aggmfinalmodify == '0')
14339  aggmfinalmodify = defaultfinalmodify;
14340 
14341  /* regproc and regtype output is already sufficiently quoted */
14342  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14343  aggtransfn, aggtranstype);
14344 
14345  if (strcmp(aggtransspace, "0") != 0)
14346  {
14347  appendPQExpBuffer(details, ",\n SSPACE = %s",
14348  aggtransspace);
14349  }
14350 
14351  if (!PQgetisnull(res, 0, i_agginitval))
14352  {
14353  appendPQExpBufferStr(details, ",\n INITCOND = ");
14354  appendStringLiteralAH(details, agginitval, fout);
14355  }
14356 
14357  if (strcmp(aggfinalfn, "-") != 0)
14358  {
14359  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14360  aggfinalfn);
14361  if (aggfinalextra)
14362  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14363  if (aggfinalmodify != defaultfinalmodify)
14364  {
14365  switch (aggfinalmodify)
14366  {
14367  case AGGMODIFY_READ_ONLY:
14368  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14369  break;
14370  case AGGMODIFY_SHAREABLE:
14371  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14372  break;
14373  case AGGMODIFY_READ_WRITE:
14374  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14375  break;
14376  default:
14377  fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14378  agginfo->aggfn.dobj.name);
14379  break;
14380  }
14381  }
14382  }
14383 
14384  if (strcmp(aggcombinefn, "-") != 0)
14385  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14386 
14387  if (strcmp(aggserialfn, "-") != 0)
14388  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14389 
14390  if (strcmp(aggdeserialfn, "-") != 0)
14391  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14392 
14393  if (strcmp(aggmtransfn, "-") != 0)
14394  {
14395  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14396  aggmtransfn,
14397  aggminvtransfn,
14398  aggmtranstype);
14399  }
14400 
14401  if (strcmp(aggmtransspace, "0") != 0)
14402  {
14403  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14404  aggmtransspace);
14405  }
14406 
14407  if (!PQgetisnull(res, 0, i_aggminitval))
14408  {
14409  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14410  appendStringLiteralAH(details, aggminitval, fout);
14411  }
14412 
14413  if (strcmp(aggmfinalfn, "-") != 0)
14414  {
14415  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14416  aggmfinalfn);
14417  if (aggmfinalextra)
14418  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14419  if (aggmfinalmodify != defaultfinalmodify)
14420  {
14421  switch (aggmfinalmodify)
14422  {
14423  case AGGMODIFY_READ_ONLY:
14424  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14425  break;
14426  case AGGMODIFY_SHAREABLE:
14427  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14428  break;
14429  case AGGMODIFY_READ_WRITE:
14430  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14431  break;
14432  default:
14433  fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14434  agginfo->aggfn.dobj.name);
14435  break;
14436  }
14437  }
14438  }
14439 
14440  aggsortconvop = getFormattedOperatorName(aggsortop);
14441  if (aggsortconvop)
14442  {
14443  appendPQExpBuffer(details, ",\n SORTOP = %s",
14444  aggsortconvop);
14445  free(aggsortconvop);
14446  }
14447 
14448  if (aggkind == AGGKIND_HYPOTHETICAL)
14449  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14450 
14451  if (proparallel[0] != PROPARALLEL_UNSAFE)
14452  {
14453  if (proparallel[0] == PROPARALLEL_SAFE)
14454  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14455  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14456  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14457  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14458  fatal("unrecognized proparallel value for function \"%s\"",
14459  agginfo->aggfn.dobj.name);
14460  }
14461 
14462  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14463  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14464  aggsig);
14465 
14466  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14467  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14468  aggfullsig ? aggfullsig : aggsig, details->data);
14469 
14470  if (dopt->binary_upgrade)
14471  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
14472  "AGGREGATE", aggsig,
14473  agginfo->aggfn.dobj.namespace->dobj.name);
14474 
14475  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14476  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14477  agginfo->aggfn.dobj.dumpId,
14478  ARCHIVE_OPTS(.tag = aggsig_tag,
14479  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14480  .owner = agginfo->aggfn.rolname,
14481  .description = "AGGREGATE",
14482  .section = SECTION_PRE_DATA,
14483  .createStmt = q->data,
14484  .dropStmt = delq->data));
14485 
14486  /* Dump Aggregate Comments */
14487  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14488  dumpComment(fout, "AGGREGATE", aggsig,
14489  agginfo->aggfn.dobj.namespace->dobj.name,
14490  agginfo->aggfn.rolname,
14491  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14492 
14493  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14494  dumpSecLabel(fout, "AGGREGATE", aggsig,
14495  agginfo->aggfn.dobj.namespace->dobj.name,
14496  agginfo->aggfn.rolname,
14497  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14498 
14499  /*
14500  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14501  * command look like a function's GRANT; in particular this affects the
14502  * syntax for zero-argument aggregates and ordered-set aggregates.
14503  */
14504  free(aggsig);
14505 
14506  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14507 
14508  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14509  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
14510  "FUNCTION", aggsig, NULL,
14511  agginfo->aggfn.dobj.namespace->dobj.name,
14512  agginfo->aggfn.rolname, agginfo->aggfn.proacl,
14513  agginfo->aggfn.rproacl,
14514  agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
14515 
14516  free(aggsig);
14517  if (aggfullsig)
14518  free(aggfullsig);
14519  free(aggsig_tag);
14520 
14521  PQclear(res);
14522 
14523  destroyPQExpBuffer(query);
14524  destroyPQExpBuffer(q);
14525  destroyPQExpBuffer(delq);
14526  destroyPQExpBuffer(details);
14527 }
#define ARCHIVE_OPTS(...)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
static char * format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:14132
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:11990
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:15323
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:12085
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
static char * getFormattedOperatorName(const char *oproid)
Definition: pg_dump.c:13150
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
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:4839
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
#define InvalidDumpId
Definition: pg_backup.h:248
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:306
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:15224
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
#define fatal(...)
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3586
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:10002
int remoteVersion
Definition: pg_backup.h:197

◆ dumpAttrDef()

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

Definition at line 16662 of file pg_dump.c.

References _attrDefInfo::adef_expr, _attrDefInfo::adnum, _attrDefInfo::adtable, appendPQExpBuffer(), ARCHIVE_OPTS, ArchiveEntry(), _tableInfo::attnames, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _tableInfo::dobj, _attrDefInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_DEFINITION, _dumpableObject::dumpId, fmtId(), fmtQualifiedDumpable, free, _dumpableObject::name, pg_strdup(), psprintf(), _tableInfo::relkind, _tableInfo::rolname, SECTION_PRE_DATA, and _attrDefInfo::separate.

Referenced by dumpDumpableObject().

16663 {
16664  DumpOptions *dopt = fout->dopt;
16665  TableInfo *tbinfo = adinfo->adtable;
16666  int adnum = adinfo->adnum;
16667  PQExpBuffer q;
16668  PQExpBuffer delq;
16669  char *qualrelname;
16670  char *tag;
16671  char *foreign;
16672 
16673  /* Skip if table definition not to be dumped */
16674  if (!tbinfo->dobj.dump || dopt->dataOnly)
16675  return;
16676 
16677  /* Skip if not "separate"; it was dumped in the table's definition */
16678  if (!adinfo->separate)
16679  return;
16680 
16681  q = createPQExpBuffer();
16682  delq = createPQExpBuffer();
16683 
16684  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16685 
16686  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16687 
16689  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16690  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16691  adinfo->adef_expr);
16692 
16693  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16694  foreign, qualrelname,
16695  fmtId(tbinfo->attnames[adnum - 1]));
16696 
16697  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16698 
16699  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16700  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16701  ARCHIVE_OPTS(.tag = tag,
16702  .namespace = tbinfo->dobj.namespace->dobj.name,
16703  .owner = tbinfo->rolname,
16704  .description = "DEFAULT",
16705  .section = SECTION_PRE_DATA,
16706  .createStmt = q->data,
16707  .dropStmt = delq->data));
16708 
16709  free(tag);
16710  destroyPQExpBuffer(q);
16711  destroyPQExpBuffer(delq);
16712  free(qualrelname);
16713 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
char relkind
Definition: pg_dump.h:267
DumpComponents dump
Definition: pg_dump.h:132
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
char * rolname
Definition: pg_dump.h:262
DumpId dumpId
Definition: pg_dump.h:130
char ** attnames
Definition: pg_dump.h:305
DumpableObject dobj
Definition: pg_dump.h:261
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
bool separate
Definition: pg_dump.h:356
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
TableInfo * adtable
Definition: pg_dump.h:353
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
DumpableObject dobj
Definition: pg_dump.h:352
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:144
CatalogId catId
Definition: pg_dump.h:129
char * adef_expr
Definition: pg_dump.h:355

◆ dumpBaseType()

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

Definition at line 11045 of file pg_dump.c.

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(), 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(), fmtId(), fmtQualifiedDumpable, free, getFormattedTypeName(), _typeInfo::initrtypacl, _typeInfo::inittypacl, InvalidDumpId, _dumpableObject::name, CatalogId::oid, OidIsValid, pg_strdup(), PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), Archive::remoteVersion, _typeInfo::rolname, _typeInfo::rtypacl, SECTION_PRE_DATA, _typeInfo::typacl, typalign, _typeInfo::typelem, and zeroIsError.

Referenced by dumpType().

11046 {
11047  DumpOptions *dopt = fout->dopt;
11049  PQExpBuffer delq = createPQExpBuffer();
11050  PQExpBuffer query = createPQExpBuffer();
11051  PGresult *res;
11052  char *qtypname;
11053  char *qualtypname;
11054  char *typlen;
11055  char *typinput;
11056  char *typoutput;
11057  char *typreceive;
11058  char *typsend;
11059  char *typmodin;
11060  char *typmodout;
11061  char *typanalyze;
11062  char *typsubscript;
11063  Oid typreceiveoid;
11064  Oid typsendoid;
11065  Oid typmodinoid;
11066  Oid typmodoutoid;
11067  Oid typanalyzeoid;
11068  Oid typsubscriptoid;
11069  char *typcategory;
11070  char *typispreferred;
11071  char *typdelim;
11072  char *typbyval;
11073  char *typalign;
11074  char *typstorage;
11075  char *typcollatable;
11076  char *typdefault;
11077  bool typdefault_is_literal = false;
11078 
11079  /* Fetch type-specific details */
11080  appendPQExpBufferStr(query, "SELECT typlen, "
11081  "typinput, typoutput, typreceive, typsend, "
11082  "typreceive::pg_catalog.oid AS typreceiveoid, "
11083  "typsend::pg_catalog.oid AS typsendoid, "
11084  "typanalyze, "
11085  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
11086  "typdelim, typbyval, typalign, typstorage, ");
11087 
11088  if (fout->remoteVersion >= 80300)
11089  appendPQExpBufferStr(query,
11090  "typmodin, typmodout, "
11091  "typmodin::pg_catalog.oid AS typmodinoid, "
11092  "typmodout::pg_catalog.oid AS typmodoutoid, ");
11093  else
11094  appendPQExpBufferStr(query,
11095  "'-' AS typmodin, '-' AS typmodout, "
11096  "0 AS typmodinoid, 0 AS typmodoutoid, ");
11097 
11098  if (fout->remoteVersion >= 80400)
11099  appendPQExpBufferStr(query,
11100  "typcategory, typispreferred, ");
11101  else
11102  appendPQExpBufferStr(query,
11103  "'U' AS typcategory, false AS typispreferred, ");
11104 
11105  if (fout->remoteVersion >= 90100)
11106  appendPQExpBufferStr(query, "(typcollation <> 0) AS typcollatable, ");
11107  else
11108  appendPQExpBufferStr(query, "false AS typcollatable, ");
11109 
11110  if (fout->remoteVersion >= 140000)
11111  appendPQExpBufferStr(query,
11112  "typsubscript, "
11113  "typsubscript::pg_catalog.oid AS typsubscriptoid, ");
11114  else
11115  appendPQExpBufferStr(query,
11116  "'-' AS typsubscript, 0 AS typsubscriptoid, ");
11117 
11118  /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
11119  if (fout->remoteVersion >= 80400)
11120  appendPQExpBufferStr(query,
11121  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault ");
11122  else
11123  appendPQExpBufferStr(query,
11124  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault ");
11125 
11126  appendPQExpBuffer(query, "FROM pg_catalog.pg_type "
11127  "WHERE oid = '%u'::pg_catalog.oid",
11128  tyinfo->dobj.catId.oid);
11129 
11130  res = ExecuteSqlQueryForSingleRow(fout, query->data);
11131 
11132  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
11133  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
11134  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
11135  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
11136  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
11137  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
11138  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
11139  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
11140  typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
11141  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
11142  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
11143  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
11144  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
11145  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
11146  typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
11147  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
11148  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
11149  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
11150  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
11151  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
11152  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
11153  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
11154  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
11155  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
11156  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
11157  {
11158  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
11159  typdefault_is_literal = true; /* it needs quotes */
11160  }
11161  else
11162  typdefault = NULL;
11163 
11164  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11165  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11166 
11167  /*
11168  * The reason we include CASCADE is that the circular dependency between
11169  * the type and its I/O functions makes it impossible to drop the type any
11170  * other way.
11171  */
11172  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
11173 
11174  /*
11175  * We might already have a shell type, but setting pg_type_oid is
11176  * harmless, and in any case we'd better set the array type OID.
11177  */
11178  if (dopt->binary_upgrade)
11180  tyinfo->dobj.catId.oid,
11181  false, false);
11182 
11184  "CREATE TYPE %s (\n"
11185  " INTERNALLENGTH = %s",
11186  qualtypname,
11187  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
11188 
11189  /* regproc result is sufficiently quoted already */
11190  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
11191  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
11192  if (OidIsValid(typreceiveoid))
11193  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
11194  if (OidIsValid(typsendoid))
11195  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
11196  if (OidIsValid(typmodinoid))
11197  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
11198  if (OidIsValid(typmodoutoid))
11199  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
11200  if (OidIsValid(typanalyzeoid))
11201  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
11202 
11203  if (strcmp(typcollatable, "t") == 0)
11204  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
11205 
11206  if (typdefault != NULL)
11207  {
11208  appendPQExpBufferStr(q, ",\n DEFAULT = ");
11209  if (typdefault_is_literal)
11210  appendStringLiteralAH(q, typdefault, fout);
11211  else
11212  appendPQExpBufferStr(q, typdefault);
11213  }
11214 
11215  if (OidIsValid(typsubscriptoid))
11216  appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
11217 
11218  if (OidIsValid(tyinfo->typelem))
11219  {
11220  char *elemType;
11221 
11222  elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroIsError);
11223  appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
11224  free(elemType);
11225  }
11226 
11227  if (strcmp(typcategory, "U") != 0)
11228  {
11229  appendPQExpBufferStr(q, ",\n CATEGORY = ");
11230  appendStringLiteralAH(q, typcategory, fout);
11231  }
11232 
11233  if (strcmp(typispreferred, "t") == 0)
11234  appendPQExpBufferStr(q, ",\n PREFERRED = true");
11235 
11236  if (typdelim && strcmp(typdelim, ",") != 0)
11237  {
11238  appendPQExpBufferStr(q, ",\n DELIMITER = ");
11239  appendStringLiteralAH(q, typdelim, fout);
11240  }
11241 
11242  if (*typalign == TYPALIGN_CHAR)
11243  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
11244  else if (*typalign == TYPALIGN_SHORT)
11245  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
11246  else if (*typalign == TYPALIGN_INT)
11247  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
11248  else if (*typalign == TYPALIGN_DOUBLE)
11249  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
11250 
11251  if (*typstorage == TYPSTORAGE_PLAIN)
11252  appendPQExpBufferStr(q, ",\n STORAGE = plain");
11253  else if (*typstorage == TYPSTORAGE_EXTERNAL)
11254  appendPQExpBufferStr(q, ",\n STORAGE = external");
11255  else if (*typstorage == TYPSTORAGE_EXTENDED)
11256  appendPQExpBufferStr(q, ",\n STORAGE = extended");
11257  else if (*typstorage == TYPSTORAGE_MAIN)
11258  appendPQExpBufferStr(q, ",\n STORAGE = main");
11259 
11260  if (strcmp(typbyval, "t") == 0)
11261  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
11262 
11263  appendPQExpBufferStr(q, "\n);\n");
11264 
11265  if (dopt->binary_upgrade)
11267  "TYPE", qtypname,
11268  tyinfo->dobj.namespace->dobj.name);
11269 
11270  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11271  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11272  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11273  .namespace = tyinfo->dobj.namespace->dobj.name,
11274  .owner = tyinfo->rolname,
11275  .description = "TYPE",
11276  .section = SECTION_PRE_DATA,
11277  .createStmt = q->data,
11278  .dropStmt = delq->data));
11279 
11280  /* Dump Type Comments and Security Labels */
11281  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11282  dumpComment(fout, "TYPE", qtypname,
11283  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11284  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11285 
11286  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11287  dumpSecLabel(fout, "TYPE", qtypname,
11288  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11289  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11290 
11291  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11292  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11293  qtypname, NULL,
11294  tyinfo->dobj.namespace->dobj.name,
11295  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11296  tyinfo->inittypacl, tyinfo->initrtypacl);
11297 
11298  PQclear(res);
11299  destroyPQExpBuffer(q);
11300  destroyPQExpBuffer(delq);
11301  destroyPQExpBuffer(query);
11302  free(qtypname);
11303  free(qualtypname);
11304 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18825
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
DumpComponents dump
Definition: pg_dump.h:132
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
char * inittypacl
Definition: pg_dump.h:173
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrtypacl
Definition: pg_dump.h:174
char * typacl
Definition: pg_dump.h:171
DumpId dumpId
Definition: pg_dump.h:130
unsigned int Oid
Definition: postgres_ext.h:31
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:15323
#define OidIsValid(objectId)
Definition: c.h:710
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:4643
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
char typalign
Definition: pg_type.h:176
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
Oid typelem
Definition: pg_dump.h:175
#define atooid(x)
Definition: postgres_ext.h:42
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:4839
DumpableObject dobj
Definition: pg_dump.h:164
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
#define InvalidDumpId
Definition: pg_backup.h:248
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:306
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:144
char * rtypacl
Definition: pg_dump.h:172
char * rolname
Definition: pg_dump.h:170
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:15224
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
CatalogId catId
Definition: pg_dump.h:129
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3586
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:10002
int remoteVersion
Definition: pg_backup.h:197

◆ dumpBlob()

static void dumpBlob ( Archive fout,
const BlobInfo binfo 
)
static

Definition at line 3605 of file pg_dump.c.

References appendPQExpBuffer(), ARCHIVE_OPTS, ArchiveEntry(), _blobInfo::blobacl, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _blobInfo::dobj, _dumpableObject::dump, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), _dumpableObject::dumpId, dumpSecLabel(), _blobInfo::initblobacl, _blobInfo::initrblobacl, InvalidDumpId, _dumpableObject::name, _blobInfo::rblobacl, _blobInfo::rolname, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

3606 {
3607  PQExpBuffer cquery = createPQExpBuffer();
3608  PQExpBuffer dquery = createPQExpBuffer();
3609 
3610  appendPQExpBuffer(cquery,
3611  "SELECT pg_catalog.lo_create('%s');\n",
3612  binfo->dobj.name);
3613 
3614  appendPQExpBuffer(dquery,
3615  "SELECT pg_catalog.lo_unlink('%s');\n",
3616  binfo->dobj.name);
3617 
3618  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3619  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3620  ARCHIVE_OPTS(.tag = binfo->dobj.name,
3621  .owner = binfo->rolname,
3622  .description = "BLOB",
3623  .section = SECTION_PRE_DATA,
3624  .createStmt = cquery->data,
3625  .dropStmt = dquery->data));
3626 
3627  /* Dump comment if any */
3628  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3629  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3630  NULL, binfo->rolname,
3631  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3632 
3633  /* Dump security label if any */
3634  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3635  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3636  NULL, binfo->rolname,
3637  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3638 
3639  /* Dump ACL if any */
3640  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3641  dumpACL(fout, binfo->dobj.dumpId, InvalidDumpId, "LARGE OBJECT",
3642  binfo->dobj.name, NULL,
3643  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3644  binfo->initblobacl, binfo->initrblobacl);
3645 
3646  destroyPQExpBuffer(cquery);
3647  destroyPQExpBuffer(dquery);
3648 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
DumpComponents dump
Definition: pg_dump.h:132
DumpId dumpId
Definition: pg_dump.h:130
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:15323
char * initblobacl
Definition: pg_dump.h:583
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject dobj
Definition: pg_dump.h:579
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
char * rolname
Definition: pg_dump.h:580
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define InvalidDumpId
Definition: pg_backup.h:248
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:15224
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
char * blobacl
Definition: pg_dump.h:581
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
CatalogId catId
Definition: pg_dump.h:129
char * rblobacl
Definition: pg_dump.h:582
char * initrblobacl
Definition: pg_dump.h:584
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:10002

◆ dumpBlobs()

static int dumpBlobs ( Archive fout,
const void *  arg 
)
static

Definition at line 3655 of file pg_dump.c.

References atooid, buf, conn, EndBlob(), ExecuteSqlQuery(), ExecuteSqlStatement(), fatal, GetConnection(), i, INV_READ, lo_close(), lo_open(), lo_read(), LOBBUFSIZE, pg_log_info, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQgetvalue(), PQntuples(), Archive::remoteVersion, StartBlob(), and WriteData().

Referenced by dumpDumpableObject().

3656 {
3657  const char *blobQry;
3658  const char *blobFetchQry;
3659  PGconn *conn = GetConnection(fout);
3660  PGresult *res;
3661  char buf[LOBBUFSIZE];
3662  int ntups;
3663  int i;
3664  int cnt;
3665 
3666  pg_log_info("saving large objects");
3667 
3668  /*
3669  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3670  * the already-in-memory dumpable objects instead...
3671  */
3672  if (fout->remoteVersion >= 90000)
3673  blobQry =
3674  "DECLARE bloboid CURSOR FOR "
3675  "SELECT oid FROM pg_largeobject_metadata ORDER BY 1";
3676  else
3677  blobQry =
3678  "DECLARE bloboid CURSOR FOR "
3679  "SELECT DISTINCT loid FROM pg_largeobject ORDER BY 1";
3680 
3681  ExecuteSqlStatement(fout, blobQry);
3682 
3683  /* Command to fetch from cursor */
3684  blobFetchQry = "FETCH 1000 IN bloboid";
3685 
3686  do
3687  {
3688  /* Do a fetch */
3689  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3690 
3691  /* Process the tuples, if any */
3692  ntups = PQntuples(res);
3693  for (i = 0; i < ntups; i++)
3694  {
3695  Oid blobOid;
3696  int loFd;
3697 
3698  blobOid = atooid(PQgetvalue(res, i, 0));
3699  /* Open the BLOB */
3700  loFd = lo_open(conn, blobOid, INV_READ);
3701  if (loFd == -1)
3702  fatal("could not open large object %u: %s",
3703  blobOid, PQerrorMessage(conn));
3704 
3705  StartBlob(fout, blobOid);
3706 
3707  /* Now read it in chunks, sending data to archive */
3708  do
3709  {
3710  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3711  if (cnt < 0)
3712  fatal("error reading large object %u: %s",
3713  blobOid, PQerrorMessage(conn));
3714 
3715  WriteData(fout, buf, cnt);
3716  } while (cnt > 0);
3717 
3718  lo_close(conn, loFd);
3719 
3720  EndBlob(fout, blobOid);
3721  }
3722 
3723  PQclear(res);
3724  } while (ntups > 0);
3725 
3726  return 1;
3727 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6727
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
#define INV_READ
Definition: libpq-fs.h:22
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:96
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:125
int StartBlob(Archive *AH, Oid oid)
PGconn * conn
Definition: streamutil.c:54
static char * buf
Definition: pg_test_fsync.c:68
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:142
void WriteData(Archive *AH, const void *data, size_t dLen)
#define atooid(x)
Definition: postgres_ext.h:42
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
void PQclear(PGresult *res)
Definition: fe-exec.c:680
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:282
#define fatal(...)
int EndBlob(Archive *AH, Oid oid)
int i
#define LOBBUFSIZE
#define pg_log_info(...)
Definition: logging.h:88
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294
int remoteVersion
Definition: pg_backup.h:197

◆ dumpCast()

static void dumpCast ( Archive fout,
const CastInfo cast 
)
static

Definition at line 12646 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _castInfo::castcontext, _castInfo::castfunc, _castInfo::castmethod, _castInfo::castsource, _castInfo::casttarget, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _funcInfo::dobj, _castInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, fatal, findFuncByOid(), fmtId(), format_function_signature(), free, getFormattedTypeName(), _dumpableObject::name, OidIsValid, pg_log_warning, SECTION_PRE_DATA, and zeroAsNone.

Referenced by dumpDumpableObject().

12647 {
12648  DumpOptions *dopt = fout->dopt;
12649  PQExpBuffer defqry;
12650  PQExpBuffer delqry;
12651  PQExpBuffer labelq;
12652  PQExpBuffer castargs;
12653  FuncInfo *funcInfo = NULL;
12654  char *sourceType;
12655  char *targetType;
12656 
12657  /* Skip if not to be dumped */
12658  if (!cast->dobj.dump || dopt->dataOnly)
12659  return;
12660 
12661  /* Cannot dump if we don't have the cast function's info */
12662  if (OidIsValid(cast->castfunc))
12663  {
12664  funcInfo = findFuncByOid(cast->castfunc);
12665  if (funcInfo == NULL)
12666  fatal("could not find function definition for function with OID %u",
12667  cast->castfunc);
12668  }
12669 
12670  defqry = createPQExpBuffer();
12671  delqry = createPQExpBuffer();
12672  labelq = createPQExpBuffer();
12673  castargs = createPQExpBuffer();
12674 
12675  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12676  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12677  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12678  sourceType, targetType);
12679 
12680  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12681  sourceType, targetType);
12682 
12683  switch (cast->castmethod)
12684  {
12685  case COERCION_METHOD_BINARY:
12686  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12687  break;
12688  case COERCION_METHOD_INOUT:
12689  appendPQExpBufferStr(defqry, "WITH INOUT");
12690  break;
12691  case COERCION_METHOD_FUNCTION:
12692  if (funcInfo)
12693  {
12694  char *fsig = format_function_signature(fout, funcInfo, true);
12695 
12696  /*
12697  * Always qualify the function name (format_function_signature
12698  * won't qualify it).
12699  */
12700  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12701  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12702  free(fsig);
12703  }
12704  else
12705  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12706  break;
12707  default:
12708  pg_log_warning("bogus value in pg_cast.castmethod field");
12709  }
12710 
12711  if (cast->castcontext == 'a')
12712  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12713  else if (cast->castcontext == 'i')
12714  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12715  appendPQExpBufferStr(defqry, ";\n");
12716 
12717  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12718  sourceType, targetType);
12719 
12720  appendPQExpBuffer(castargs, "(%s AS %s)",
12721  sourceType, targetType);
12722 
12723  if (dopt->binary_upgrade)
12724  binary_upgrade_extension_member(defqry, &cast->dobj,
12725  "CAST", castargs->data, NULL);
12726 
12727  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12728  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12729  ARCHIVE_OPTS(.tag = labelq->data,
12730  .description = "CAST",
12731  .section = SECTION_PRE_DATA,
12732  .createStmt = defqry->data,
12733  .dropStmt = delqry->data));
12734 
12735  /* Dump Cast Comments */
12736  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12737  dumpComment(fout, "CAST", castargs->data,
12738  NULL, "",
12739  cast->dobj.catId, 0, cast->dobj.dumpId);
12740 
12741  free(sourceType);
12742  free(targetType);
12743 
12744  destroyPQExpBuffer(defqry);
12745  destroyPQExpBuffer(delqry);
12746  destroyPQExpBuffer(labelq);
12747  destroyPQExpBuffer(castargs);
12748 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18825
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
Oid castsource
Definition: pg_dump.h:484
DumpComponents dump
Definition: pg_dump.h:132
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpableObject dobj
Definition: pg_dump.h:198
DumpId dumpId
Definition: pg_dump.h:130
Oid castfunc
Definition: pg_dump.h:486
#define OidIsValid(objectId)
Definition: c.h:710
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:12085
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpableObject dobj
Definition: pg_dump.h:483
Oid casttarget
Definition: pg_dump.h:485
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
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:4839
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:913
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:129
char castmethod
Definition: pg_dump.h:488
#define pg_log_warning(...)
Definition: pgfnames.c:24
char castcontext
Definition: pg_dump.h:487
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:10002

◆ dumpCollation()

static void dumpCollation ( Archive fout,
const CollInfo collinfo 
)
static

Definition at line 13905 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

13906 {
13907  DumpOptions *dopt = fout->dopt;
13908  PQExpBuffer query;
13909  PQExpBuffer q;
13910  PQExpBuffer delq;
13911  char *qcollname;
13912  PGresult *res;
13913  int i_collprovider;
13914  int i_collisdeterministic;
13915  int i_collcollate;
13916  int i_collctype;
13917  const char *collprovider;
13918  const char *collcollate;
13919  const char *collctype;
13920 
13921  /* Skip if not to be dumped */
13922  if (!collinfo->dobj.dump || dopt->dataOnly)
13923  return;
13924 
13925  query = createPQExpBuffer();
13926  q = createPQExpBuffer();
13927  delq = createPQExpBuffer();
13928 
13929  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13930 
13931  /* Get collation-specific details */
13932  appendPQExpBufferStr(query, "SELECT ");
13933 
13934  if (fout->remoteVersion >= 100000)
13935  appendPQExpBufferStr(query,
13936  "collprovider, ");
13937  else
13938  appendPQExpBufferStr(query,
13939  "'c' AS collprovider, ");
13940 
13941  if (fout->remoteVersion >= 120000)
13942  appendPQExpBufferStr(query,
13943  "collisdeterministic, ");
13944  else
13945  appendPQExpBufferStr(query,
13946  "true AS collisdeterministic, ");
13947 
13948  appendPQExpBuffer(query,
13949  "collcollate, "
13950  "collctype "
13951  "FROM pg_catalog.pg_collation c "
13952  "WHERE c.oid = '%u'::pg_catalog.oid",
13953  collinfo->dobj.catId.oid);
13954 
13955  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13956 
13957  i_collprovider = PQfnumber(res, "collprovider");
13958  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13959  i_collcollate = PQfnumber(res, "collcollate");
13960  i_collctype = PQfnumber(res, "collctype");
13961 
13962  collprovider = PQgetvalue(res, 0, i_collprovider);
13963  collcollate = PQgetvalue(res, 0, i_collcollate);
13964  collctype = PQgetvalue(res, 0, i_collctype);
13965 
13966  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13967  fmtQualifiedDumpable(collinfo));
13968 
13969  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13970  fmtQualifiedDumpable(collinfo));
13971 
13972  appendPQExpBufferStr(q, "provider = ");
13973  if (collprovider[0] == 'c')
13974  appendPQExpBufferStr(q, "libc");
13975  else if (collprovider[0] == 'i')
13976  appendPQExpBufferStr(q, "icu");
13977  else if (collprovider[0] == 'd')
13978  /* to allow dumping pg_catalog; not accepted on input */
13979  appendPQExpBufferStr(q, "default");
13980  else
13981  fatal("unrecognized collation provider: %s",
13982  collprovider);
13983 
13984  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13985  appendPQExpBufferStr(q, ", deterministic = false");
13986 
13987  if (strcmp(collcollate, collctype) == 0)
13988  {
13989  appendPQExpBufferStr(q, ", locale = ");
13990  appendStringLiteralAH(q, collcollate, fout);
13991  }
13992  else
13993  {
13994  appendPQExpBufferStr(q, ", lc_collate = ");
13995  appendStringLiteralAH(q, collcollate, fout);
13996  appendPQExpBufferStr(q, ", lc_ctype = ");
13997  appendStringLiteralAH(q, collctype, fout);
13998  }
13999 
14000  appendPQExpBufferStr(q, ");\n");
14001 
14002  if (dopt->binary_upgrade)
14003  binary_upgrade_extension_member(q, &collinfo->dobj,
14004  "COLLATION", qcollname,
14005  collinfo->dobj.namespace->dobj.name);
14006 
14007  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14008  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
14009  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
14010  .namespace = collinfo->dobj.namespace->dobj.name,
14011  .owner = collinfo->rolname,
14012  .description = "COLLATION",
14013  .section = SECTION_PRE_DATA,
14014  .createStmt = q->data,
14015  .dropStmt = delq->data));
14016 
14017  /* Dump Collation Comments */
14018  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14019  dumpComment(fout, "COLLATION", qcollname,
14020  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
14021  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
14022 
14023  PQclear(res);
14024 
14025  destroyPQExpBuffer(query);
14026  destroyPQExpBuffer(q);
14027  destroyPQExpBuffer(delq);
14028  free(qcollname);
14029 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
DumpComponents dump
Definition: pg_dump.h:132
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpId dumpId
Definition: pg_dump.h:130
DumpableObject dobj
Definition: pg_dump.h:246
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
char * rolname
Definition: pg_dump.h:247
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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:4839
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:306
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:144
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:129
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:10002
int remoteVersion
Definition: pg_backup.h:197

◆ dumpComment()

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

Definition at line 10002 of file pg_dump.c.

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

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

10005 {
10006  DumpOptions *dopt = fout->dopt;
10007  CommentItem *comments;
10008  int ncomments;
10009 
10010  /* do nothing, if --no-comments is supplied */
10011  if (dopt->no_comments)
10012  return;
10013 
10014  /* Comments are schema not data ... except blob comments are data */
10015  if (strcmp(type, "LARGE OBJECT") != 0)
10016  {
10017  if (dopt->dataOnly)
10018  return;
10019  }
10020  else
10021  {
10022  /* We do dump blob comments in binary-upgrade mode */
10023  if (dopt->schemaOnly && !dopt->binary_upgrade)
10024  return;
10025  }
10026 
10027  /* Search for comments associated with catalogId, using table */
10028  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
10029  &comments);
10030 
10031  /* Is there one matching the subid? */
10032  while (ncomments > 0)
10033  {
10034  if (comments->objsubid == subid)
10035  break;
10036  comments++;
10037  ncomments--;
10038  }
10039 
10040  /* If a comment exists, build COMMENT ON statement */
10041  if (ncomments > 0)
10042  {
10043  PQExpBuffer query = createPQExpBuffer();
10045 
10046  appendPQExpBuffer(query, "COMMENT ON %s ", type);
10047  if (namespace && *namespace)
10048  appendPQExpBuffer(query, "%s.", fmtId(namespace));
10049  appendPQExpBuffer(query, "%s IS ", name);
10050  appendStringLiteralAH(query, comments->descr, fout);
10051  appendPQExpBufferStr(query, ";\n");
10052 
10053  appendPQExpBuffer(tag, "%s %s", type, name);
10054 
10055  /*
10056  * We mark comments as SECTION_NONE because they really belong in the
10057  * same section as their parent, whether that is pre-data or
10058  * post-data.
10059  */
10061  ARCHIVE_OPTS(.tag = tag->data,
10062  .namespace = namespace,
10063  .owner = owner,
10064  .description = "COMMENT",
10065  .section = SECTION_NONE,
10066  .createStmt = query->data,
10067  .deps = &dumpId,
10068  .nDeps = 1));
10069 
10070  destroyPQExpBuffer(query);
10071  destroyPQExpBuffer(tag);
10072  }
10073 }
#define ARCHIVE_OPTS(...)
Oid tableoid
Definition: pg_backup.h:242
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
bool schemaOnly
Definition: pg_backup.h:146
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:72
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:10181
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
const char * descr
Definition: pg_dump.c:69
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:306
int no_comments
Definition: pg_backup.h:157
const char * name
Definition: encode.c:515
static const CatalogId nilCatalogId
Definition: pg_dump.c:129
DumpId createDumpId(void)
Definition: common.c:649

◆ dumpCompositeType()

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

Definition at line 11486 of file pg_dump.c.

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(), 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(), findCollationByOid(), fmtId(), fmtQualifiedDumpable, free, i, _typeInfo::initrtypacl, _typeInfo::inittypacl, InvalidDumpId, _dumpableObject::name, CatalogId::oid, OidIsValid, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), Archive::remoteVersion, _typeInfo::rolname, _typeInfo::rtypacl, SECTION_PRE_DATA, _typeInfo::typacl, and _typeInfo::typrelid.

Referenced by dumpType().

11487 {
11488  DumpOptions *dopt = fout->dopt;
11490  PQExpBuffer dropped = createPQExpBuffer();
11491  PQExpBuffer delq = createPQExpBuffer();
11492  PQExpBuffer query = createPQExpBuffer();
11493  PGresult *res;
11494  char *qtypname;
11495  char *qualtypname;
11496  int ntups;
11497  int i_attname;
11498  int i_atttypdefn;
11499  int i_attlen;
11500  int i_attalign;
11501  int i_attisdropped;
11502  int i_attcollation;
11503  int i;
11504  int actual_atts;
11505 
11506  /* Fetch type specific details */
11507  if (fout->remoteVersion >= 90100)
11508  {
11509  /*
11510  * attcollation is new in 9.1. Since we only want to dump COLLATE
11511  * clauses for attributes whose collation is different from their
11512  * type's default, we use a CASE here to suppress uninteresting
11513  * attcollations cheaply. atttypid will be 0 for dropped columns;
11514  * collation does not matter for those.
11515  */
11516  appendPQExpBuffer(query, "SELECT a.attname, "
11517  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11518  "a.attlen, a.attalign, a.attisdropped, "
11519  "CASE WHEN a.attcollation <> at.typcollation "
11520  "THEN a.attcollation ELSE 0 END AS attcollation "
11521  "FROM pg_catalog.pg_type ct "
11522  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11523  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11524  "WHERE ct.oid = '%u'::pg_catalog.oid "
11525  "ORDER BY a.attnum ",
11526  tyinfo->dobj.catId.oid);
11527  }
11528  else
11529  {
11530  /*
11531  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
11532  * should always be false.
11533  */
11534  appendPQExpBuffer(query, "SELECT a.attname, "
11535  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11536  "a.attlen, a.attalign, a.attisdropped, "
11537  "0 AS attcollation "
11538  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
11539  "WHERE ct.oid = '%u'::pg_catalog.oid "
11540  "AND a.attrelid = ct.typrelid "
11541  "ORDER BY a.attnum ",
11542  tyinfo->dobj.catId.oid);
11543  }
11544 
11545  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11546 
11547  ntups = PQntuples(res);
11548 
11549  i_attname = PQfnumber(res, "attname");
11550  i_atttypdefn = PQfnumber(res, "atttypdefn");
11551  i_attlen = PQfnumber(res, "attlen");
11552  i_attalign = PQfnumber(res, "attalign");
11553  i_attisdropped = PQfnumber(res, "attisdropped");
11554  i_attcollation = PQfnumber(res, "attcollation");
11555 
11556  if (dopt->binary_upgrade)
11557  {
11559  tyinfo->dobj.catId.oid,
11560  false, false);
11561  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11562  }
11563 
11564  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11565  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11566 
11567  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11568  qualtypname);
11569 
11570  actual_atts = 0;
11571  for (i = 0; i < ntups; i++)
11572  {
11573  char *attname;
11574  char *atttypdefn;
11575  char *attlen;
11576  char *attalign;
11577  bool attisdropped;
11578  Oid attcollation;
11579 
11580  attname = PQgetvalue(res, i, i_attname);
11581  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11582  attlen = PQgetvalue(res, i, i_attlen);
11583  attalign = PQgetvalue(res, i, i_attalign);
11584  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11585  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11586 
11587  if (attisdropped && !dopt->binary_upgrade)
11588  continue;
11589 
11590  /* Format properly if not first attr */
11591  if (actual_atts++ > 0)
11592  appendPQExpBufferChar(q, ',');
11593  appendPQExpBufferStr(q, "\n\t");
11594 
11595  if (!attisdropped)
11596  {
11597  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11598 
11599  /* Add collation if not default for the column type */
11600  if (OidIsValid(attcollation))
11601  {
11602  CollInfo *coll;
11603 
11604  coll = findCollationByOid(attcollation);
11605  if (coll)
11606  appendPQExpBuffer(q, " COLLATE %s",
11607  fmtQualifiedDumpable(coll));
11608  }
11609  }
11610  else
11611  {
11612  /*
11613  * This is a dropped attribute and we're in binary_upgrade mode.
11614  * Insert a placeholder for it in the CREATE TYPE command, and set
11615  * length and alignment with direct UPDATE to the catalogs
11616  * afterwards. See similar code in dumpTableSchema().
11617  */
11618  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11619 
11620  /* stash separately for insertion after the CREATE TYPE */
11621  appendPQExpBufferStr(dropped,
11622  "\n-- For binary upgrade, recreate dropped column.\n");
11623  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11624  "SET attlen = %s, "
11625  "attalign = '%s', attbyval = false\n"
11626  "WHERE attname = ", attlen, attalign);
11627  appendStringLiteralAH(dropped, attname, fout);
11628  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11629  appendStringLiteralAH(dropped, qualtypname, fout);
11630  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11631 
11632  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11633  qualtypname);
11634  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11635  fmtId(attname));
11636  }
11637  }
11638  appendPQExpBufferStr(q, "\n);\n");
11639  appendPQExpBufferStr(q, dropped->data);
11640 
11641  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11642 
11643  if (dopt->binary_upgrade)
11645  "TYPE", qtypname,
11646  tyinfo->dobj.namespace->dobj.name);
11647 
11648  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11649  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11650  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11651  .namespace = tyinfo->dobj.namespace->dobj.name,
11652  .owner = tyinfo->rolname,
11653  .description = "TYPE",
11654  .section = SECTION_PRE_DATA,
11655  .createStmt = q->data,
11656  .dropStmt = delq->data));
11657 
11658 
11659  /* Dump Type Comments and Security Labels */
11660  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11661  dumpComment(fout, "TYPE", qtypname,
11662  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11663  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11664 
11665  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11666  dumpSecLabel(fout, "TYPE", qtypname,
11667  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11668  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11669 
11670  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11671  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11672  qtypname, NULL,
11673  tyinfo->dobj.namespace->dobj.name,
11674  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11675  tyinfo->inittypacl, tyinfo->initrtypacl);
11676 
11677  PQclear(res);
11678  destroyPQExpBuffer(q);
11679  destroyPQExpBuffer(dropped);
11680  destroyPQExpBuffer(delq);
11681  destroyPQExpBuffer(query);
11682  free(qtypname);
11683  free(qualtypname);
11684 
11685  /* Dump any per-column comments */
11686  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11687  dumpCompositeTypeColComments(fout, tyinfo);
11688 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
DumpComponents dump
Definition: pg_dump.h:132
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
char * inittypacl
Definition: pg_dump.h:173
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
Oid typrelid
Definition: pg_dump.h:176
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrtypacl
Definition: pg_dump.h:174
char * typacl
Definition: pg_dump.h:171
DumpId dumpId
Definition: pg_dump.h:130
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
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:15323
#define OidIsValid(objectId)
Definition: c.h:710
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:4643
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:41
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
char attalign
Definition: pg_attribute.h:126
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4756
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define atooid(x)
Definition: postgres_ext.h:42
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:4839
int16 attlen
Definition: pg_attribute.h:68
DumpableObject dobj
Definition: pg_dump.h:164
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
#define InvalidDumpId
Definition: pg_backup.h:248
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:306
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:144
char * rtypacl
Definition: pg_dump.h:172
char * rolname
Definition: pg_dump.h:170
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:935
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:15224
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
CatalogId catId
Definition: pg_dump.h:129
int i
static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo)
Definition: pg_dump.c:11696
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:10002
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294
int remoteVersion
Definition: pg_backup.h:197

◆ dumpCompositeTypeColComments()

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

Definition at line 11696 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), atooid, attname, createDumpId(), createPQExpBuffer(), PQExpBufferData::data, CommentItem::descr, destroyPQExpBuffer(), _typeInfo::dobj, Archive::dopt, _dumpableObject::dumpId, ExecuteSqlQuery(), findComments(), fmtId(), fmtQualifiedDumpable, i, _dumpableObject::name, _dumpOptions::no_comments, CommentItem::objsubid, PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), resetPQExpBuffer(), _typeInfo::rolname, SECTION_NONE, and _typeInfo::typrelid.

Referenced by dumpCompositeType().

11697 {
11698  CommentItem *comments;
11699  int ncomments;
11700  PGresult *res;
11701  PQExpBuffer query;
11702  PQExpBuffer target;
11703  Oid pgClassOid;
11704  int i;
11705  int ntups;
11706  int i_attname;
11707  int i_attnum;
11708 
11709  /* do nothing, if --no-comments is supplied */
11710  if (fout->dopt->no_comments)
11711  return;
11712 
11713  query = createPQExpBuffer();
11714 
11715  appendPQExpBuffer(query,
11716  "SELECT c.tableoid, a.attname, a.attnum "
11717  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
11718  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
11719  " AND NOT a.attisdropped "
11720  "ORDER BY a.attnum ",
11721  tyinfo->typrelid);
11722 
11723  /* Fetch column attnames */
11724  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11725 
11726  ntups = PQntuples(res);
11727  if (ntups < 1)
11728  {
11729  PQclear(res);
11730  destroyPQExpBuffer(query);
11731  return;
11732  }
11733 
11734  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
11735 
11736  /* Search for comments associated with type's pg_class OID */
11737  ncomments = findComments(fout,
11738  pgClassOid,
11739  tyinfo->typrelid,
11740  &comments);
11741 
11742  /* If no comments exist, we're done */
11743  if (ncomments <= 0)
11744  {
11745  PQclear(res);
11746  destroyPQExpBuffer(query);
11747  return;
11748  }
11749 
11750  /* Build COMMENT ON statements */
11751  target = createPQExpBuffer();
11752 
11753  i_attnum = PQfnumber(res, "attnum");
11754  i_attname = PQfnumber(res, "attname");
11755  while (ncomments > 0)
11756  {
11757  const char *attname;
11758 
11759  attname = NULL;
11760  for (i = 0; i < ntups; i++)
11761  {
11762  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
11763  {
11764  attname = PQgetvalue(res, i, i_attname);
11765  break;
11766  }
11767  }
11768  if (attname) /* just in case we don't find it */
11769  {
11770  const char *descr = comments->descr;
11771 
11772  resetPQExpBuffer(target);
11773  appendPQExpBuffer(target, "COLUMN %s.",
11774  fmtId(tyinfo->dobj.name));
11775  appendPQExpBufferStr(target, fmtId(attname));
11776 
11777  resetPQExpBuffer(query);
11778  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11779  fmtQualifiedDumpable(tyinfo));
11780  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11781  appendStringLiteralAH(query, descr, fout);
11782  appendPQExpBufferStr(query, ";\n");
11783 
11785  ARCHIVE_OPTS(.tag = target->data,
11786  .namespace = tyinfo->dobj.namespace->dobj.name,
11787  .owner = tyinfo->rolname,
11788  .description = "COMMENT",
11789  .section = SECTION_NONE,
11790  .createStmt = query->data,
11791  .deps = &(tyinfo->dobj.dumpId),
11792  .nDeps = 1));
11793  }
11794 
11795  comments++;
11796  ncomments--;
11797  }
11798 
11799  PQclear(res);
11800  destroyPQExpBuffer(query);
11801  destroyPQExpBuffer(target);
11802 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3561
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
Oid typrelid
Definition: pg_dump.h:176
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:72
DumpId dumpId
Definition: pg_dump.h:130
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3167
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:10181
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:41
DumpOptions * dopt
Definition: pg_backup.h:192
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:69
DumpableObject dobj
Definition: pg_dump.h:164
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3275
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:306
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:144
char * rolname
Definition: pg_dump.h:170
int no_comments
Definition: pg_backup.h:157
int i
static const CatalogId nilCatalogId
Definition: pg_dump.c:129
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294
DumpId createDumpId(void)
Definition: common.c:649

◆ dumpConstraint()

static void dumpConstraint ( Archive fout,
const ConstraintInfo coninfo 
)
static

Definition at line 17030 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

17031 {
17032  DumpOptions *dopt = fout->dopt;
17033  TableInfo *tbinfo = coninfo->contable;
17034  PQExpBuffer q;
17035  PQExpBuffer delq;
17036  char *tag = NULL;
17037  char *foreign;
17038 
17039  /* Skip if not to be dumped */
17040  if (!coninfo->dobj.dump || dopt->dataOnly)
17041  return;
17042 
17043  q = createPQExpBuffer();
17044  delq = createPQExpBuffer();
17045 
17046  foreign = tbinfo &&
17047  tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
17048 
17049  if (coninfo->contype == 'p' ||
17050  coninfo->contype == 'u' ||
17051  coninfo->contype == 'x')
17052  {
17053  /* Index-related constraint */
17054  IndxInfo *indxinfo;
17055  int k;
17056 
17057  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
17058 
17059  if (indxinfo == NULL)
17060  fatal("missing index for constraint \"%s\"",
17061  coninfo->dobj.name);
17062 
17063  if (dopt->binary_upgrade)
17065  indxinfo->dobj.catId.oid, true);
17066 
17067  appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
17068  fmtQualifiedDumpable(tbinfo));
17069  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
17070  fmtId(coninfo->dobj.name));
17071 
17072  if (coninfo->condef)
17073  {
17074  /* pg_get_constraintdef should have provided everything */
17075  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
17076  }
17077  else
17078  {
17079  appendPQExpBuffer(q, "%s (",
17080  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
17081  for (k = 0; k < indxinfo->indnkeyattrs; k++)
17082  {
17083  int indkey = (int) indxinfo->indkeys[k];
17084  const char *attname;
17085 
17086  if (indkey == InvalidAttrNumber)
17087  break;
17088  attname = getAttrName(indkey, tbinfo);
17089 
17090  appendPQExpBuffer(q, "%s%s",
17091  (k == 0) ? "" : ", ",
17092  fmtId(attname));
17093  }
17094 
17095  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
17096  appendPQExpBufferStr(q, ") INCLUDE (");
17097 
17098  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
17099  {
17100  int indkey = (int) indxinfo->indkeys[k];
17101  const char *attname;
17102 
17103  if (indkey == InvalidAttrNumber)
17104  break;
17105  attname = getAttrName(indkey, tbinfo);
17106 
17107  appendPQExpBuffer(q, "%s%s",
17108  (k == indxinfo->indnkeyattrs) ? "" : ", ",
17109  fmtId(attname));
17110  }
17111 
17112  appendPQExpBufferChar(q, ')');
17113 
17114  if (nonemptyReloptions(indxinfo->indreloptions))
17115  {
17116  appendPQExpBufferStr(q, " WITH (");
17117  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
17118  appendPQExpBufferChar(q, ')');
17119  }
17120 
17121  if (coninfo->condeferrable)
17122  {
17123  appendPQExpBufferStr(q, " DEFERRABLE");
17124  if (coninfo->condeferred)
17125  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
17126  }
17127 
17128  appendPQExpBufferStr(q, ";\n");
17129  }
17130 
17131  /*
17132  * Append ALTER TABLE commands as needed to set properties that we
17133  * only have ALTER TABLE syntax for. Keep this in sync with the
17134  * similar code in dumpIndex!
17135  */
17136 
17137  /* If the index is clustered, we need to record that. */
17138  if (indxinfo->indisclustered)
17139  {
17140  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
17141  fmtQualifiedDumpable(tbinfo));
17142  /* index name is not qualified in this syntax */
17143  appendPQExpBuffer(q, " ON %s;\n",
17144  fmtId(indxinfo->dobj.name));
17145  }
17146 
17147  /* If the index defines identity, we need to record that. */
17148  if (indxinfo->indisreplident)
17149  {
17150  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
17151  fmtQualifiedDumpable(tbinfo));
17152  /* index name is not qualified in this syntax */
17153  appendPQExpBuffer(q, " INDEX %s;\n",
17154  fmtId(indxinfo->dobj.name));
17155  }
17156 
17157  /* Indexes can depend on extensions */
17158  append_depends_on_extension(fout, q, &indxinfo->dobj,
17159  "pg_catalog.pg_class", "INDEX",
17160  fmtQualifiedDumpable(indxinfo));
17161 
17162  appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
17163  fmtQualifiedDumpable(tbinfo));
17164  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17165  fmtId(coninfo->dobj.name));
17166 
17167  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17168 
17169  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17170  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17171  ARCHIVE_OPTS(.tag = tag,
17172  .namespace = tbinfo->dobj.namespace->dobj.name,
17173  .tablespace = indxinfo->tablespace,
17174  .owner = tbinfo->rolname,
17175  .description = "CONSTRAINT",
17176  .section = SECTION_POST_DATA,
17177  .createStmt = q->data,
17178  .dropStmt = delq->data));
17179  }
17180  else if (coninfo->contype == 'f')
17181  {
17182  char *only;
17183 
17184  /*
17185  * Foreign keys on partitioned tables are always declared as
17186  * inheriting to partitions; for all other cases, emit them as
17187  * applying ONLY directly to the named table, because that's how they
17188  * work for regular inherited tables.
17189  */
17190  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
17191 
17192  /*
17193  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
17194  * current table data is not processed
17195  */
17196  appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
17197  only, fmtQualifiedDumpable(tbinfo));
17198  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17199  fmtId(coninfo->dobj.name),
17200  coninfo->condef);
17201 
17202  appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
17203  only, fmtQualifiedDumpable(tbinfo));
17204  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17205  fmtId(coninfo->dobj.name));
17206 
17207  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17208 
17209  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17210  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17211  ARCHIVE_OPTS(.tag = tag,
17212  .namespace = tbinfo->dobj.namespace->dobj.name,
17213  .owner = tbinfo->rolname,
17214  .description = "FK CONSTRAINT",
17215  .section = SECTION_POST_DATA,
17216  .createStmt = q->data,
17217  .dropStmt = delq->data));
17218  }
17219  else if (coninfo->contype == 'c' && tbinfo)
17220  {
17221  /* CHECK constraint on a table */
17222 
17223  /* Ignore if not to be dumped separately, or if it was inherited */
17224  if (coninfo->separate && coninfo->conislocal)
17225  {
17226  /* not ONLY since we want it to propagate to children */
17227  appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
17228  fmtQualifiedDumpable(tbinfo));
17229  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17230  fmtId(coninfo->dobj.name),
17231  coninfo->condef);
17232 
17233  appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
17234  fmtQualifiedDumpable(tbinfo));
17235  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17236  fmtId(coninfo->dobj.name));
17237 
17238  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
17239 
17240  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17241  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17242  ARCHIVE_OPTS(.tag = tag,
17243  .namespace = tbinfo->dobj.namespace->dobj.name,
17244  .owner = tbinfo->rolname,
17245  .description = "CHECK CONSTRAINT",
17246  .section = SECTION_POST_DATA,
17247  .createStmt = q->data,
17248  .dropStmt = delq->data));
17249  }
17250  }
17251  else if (coninfo->contype == 'c' && tbinfo == NULL)
17252  {
17253  /* CHECK constraint on a domain */
17254  TypeInfo *tyinfo = coninfo->condomain;
17255 
17256  /* Ignore if not to be dumped separately */
17257  if (coninfo->separate)
17258  {
17259  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
17260  fmtQualifiedDumpable(tyinfo));
17261  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17262  fmtId(coninfo->dobj.name),
17263  coninfo->condef);
17264 
17265  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
17266  fmtQualifiedDumpable(tyinfo));
17267  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17268  fmtId(coninfo->dobj.name));
17269 
17270  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
17271 
17272  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17273  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17274  ARCHIVE_OPTS(.tag = tag,
17275  .namespace = tyinfo->dobj.namespace->dobj.name,
17276  .owner = tyinfo->rolname,
17277  .description = "CHECK CONSTRAINT",
17278  .section = SECTION_POST_DATA,
17279  .createStmt = q->data,
17280  .dropStmt = delq->data));
17281  }
17282  }
17283  else
17284  {
17285  fatal("unrecognized constraint type: %c",
17286  coninfo->contype);
17287  }
17288 
17289  /* Dump Constraint Comments --- only works for table constraints */
17290  if (tbinfo && coninfo->separate &&
17291  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17292  dumpTableConstraintComment(fout, coninfo);
17293 
17294  free(tag);
17295  destroyPQExpBuffer(q);
17296  destroyPQExpBuffer(delq);
17297 }
char * tablespace
Definition: pg_dump.h:371
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
char relkind
Definition: pg_dump.h:267
DumpComponents dump
Definition: pg_dump.h:132
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
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:4567
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
Definition: pg_dump.c:18983
char * rolname
Definition: pg_dump.h:262
DumpId dumpId
Definition: pg_dump.h:130
DumpId conindex
Definition: pg_dump.h:460
DumpableObject dobj
Definition: pg_dump.h:261
char * condef
Definition: pg_dump.h:458
bool indisreplident
Definition: pg_dump.h:382
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:41
int binary_upgrade
Definition: pg_backup.h:143
DumpableObject dobj
Definition: pg_dump.h:454
DumpOptions * dopt
Definition: pg_backup.h:192
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:669
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
Oid * indkeys
Definition: pg_dump.h:377
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4756
bool condeferrable
Definition: pg_dump.h:461
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18895
char * indreloptions
Definition: pg_dump.h:372
DumpableObject dobj
Definition: pg_dump.h:164
TypeInfo * condomain
Definition: pg_dump.h:456
bool conislocal
Definition: pg_dump.h:463
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
TableInfo * contable
Definition: pg_dump.h:455
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
Definition: pg_dump.c:17307
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
bool condeferred
Definition: pg_dump.h:462
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:144
char * rolname
Definition: pg_dump.h:170
#define fatal(...)
#define InvalidAttrNumber
Definition: attnum.h:23
int indnkeyattrs
Definition: pg_dump.h:375
int indnattrs
Definition: pg_dump.h:376
CatalogId catId
Definition: pg_dump.h:129
DumpableObject dobj
Definition: pg_dump.h:368
bool indisclustered
Definition: pg_dump.h:381
static const char * getAttrName(int attrnum, const TableInfo *tblInfo)
Definition: pg_dump.c:16723

◆ dumpConversion()

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

Definition at line 14036 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

14037 {
14038  DumpOptions *dopt = fout->dopt;
14039  PQExpBuffer query;
14040  PQExpBuffer q;
14041  PQExpBuffer delq;
14042  char *qconvname;
14043  PGresult *res;
14044  int i_conforencoding;
14045  int i_contoencoding;
14046  int i_conproc;
14047  int i_condefault;
14048  const char *conforencoding;
14049  const char *contoencoding;
14050  const char *conproc;
14051  bool condefault;
14052 
14053  /* Skip if not to be dumped */
14054  if (!convinfo->dobj.dump || dopt->dataOnly)
14055  return;
14056 
14057  query = createPQExpBuffer();
14058  q = createPQExpBuffer();
14059  delq = createPQExpBuffer();
14060 
14061  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
14062 
14063  /* Get conversion-specific details */
14064  appendPQExpBuffer(query, "SELECT "
14065  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
14066  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
14067  "conproc, condefault "
14068  "FROM pg_catalog.pg_conversion c "
14069  "WHERE c.oid = '%u'::pg_catalog.oid",
14070  convinfo->dobj.catId.oid);
14071 
14072  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14073 
14074  i_conforencoding = PQfnumber(res, "conforencoding");
14075  i_contoencoding = PQfnumber(res, "contoencoding");
14076  i_conproc = PQfnumber(res, "conproc");
14077  i_condefault = PQfnumber(res, "condefault");
14078 
14079  conforencoding = PQgetvalue(res, 0, i_conforencoding);
14080  contoencoding = PQgetvalue(res, 0, i_contoencoding);
14081  conproc = PQgetvalue(res, 0, i_conproc);
14082  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
14083 
14084  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
14085  fmtQualifiedDumpable(convinfo));
14086 
14087  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
14088  (condefault) ? "DEFAULT " : "",
14089  fmtQualifiedDumpable(convinfo));
14090  appendStringLiteralAH(q, conforencoding, fout);
14091  appendPQExpBufferStr(q, " TO ");
14092  appendStringLiteralAH(q, contoencoding, fout);
14093  /* regproc output is already sufficiently quoted */
14094  appendPQExpBuffer(q, " FROM %s;\n", conproc);
14095 
14096  if (dopt->binary_upgrade)
14097  binary_upgrade_extension_member(q, &convinfo->dobj,
14098  "CONVERSION", qconvname,
14099  convinfo->dobj.namespace->dobj.name);
14100 
14101  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14102  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
14103  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
14104  .namespace = convinfo->dobj.namespace->dobj.name,
14105  .owner = convinfo->rolname,
14106  .description = "CONVERSION",
14107  .section = SECTION_PRE_DATA,