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_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 "dumputils.h"
#include "fe_utils/connect.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_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 (Archive *fout, Oid nsoid)
 
static void dumpTableData (Archive *fout, TableDataInfo *tdinfo)
 
static void refreshMatViewData (Archive *fout, 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, DumpableObject *dobj)
 
static void dumpNamespace (Archive *fout, NamespaceInfo *nspinfo)
 
static void dumpExtension (Archive *fout, ExtensionInfo *extinfo)
 
static void dumpType (Archive *fout, TypeInfo *tyinfo)
 
static void dumpBaseType (Archive *fout, TypeInfo *tyinfo)
 
static void dumpEnumType (Archive *fout, TypeInfo *tyinfo)
 
static void dumpRangeType (Archive *fout, TypeInfo *tyinfo)
 
static void dumpUndefinedType (Archive *fout, TypeInfo *tyinfo)
 
static void dumpDomain (Archive *fout, TypeInfo *tyinfo)
 
static void dumpCompositeType (Archive *fout, TypeInfo *tyinfo)
 
static void dumpCompositeTypeColComments (Archive *fout, TypeInfo *tyinfo)
 
static void dumpShellType (Archive *fout, ShellTypeInfo *stinfo)
 
static void dumpProcLang (Archive *fout, ProcLangInfo *plang)
 
static void dumpFunc (Archive *fout, FuncInfo *finfo)
 
static void dumpCast (Archive *fout, CastInfo *cast)
 
static void dumpTransform (Archive *fout, TransformInfo *transform)
 
static void dumpOpr (Archive *fout, OprInfo *oprinfo)
 
static void dumpAccessMethod (Archive *fout, AccessMethodInfo *oprinfo)
 
static void dumpOpclass (Archive *fout, OpclassInfo *opcinfo)
 
static void dumpOpfamily (Archive *fout, OpfamilyInfo *opfinfo)
 
static void dumpCollation (Archive *fout, CollInfo *collinfo)
 
static void dumpConversion (Archive *fout, ConvInfo *convinfo)
 
static void dumpRule (Archive *fout, RuleInfo *rinfo)
 
static void dumpAgg (Archive *fout, AggInfo *agginfo)
 
static void dumpTrigger (Archive *fout, TriggerInfo *tginfo)
 
static void dumpEventTrigger (Archive *fout, EventTriggerInfo *evtinfo)
 
static void dumpTable (Archive *fout, TableInfo *tbinfo)
 
static void dumpTableSchema (Archive *fout, TableInfo *tbinfo)
 
static void dumpAttrDef (Archive *fout, AttrDefInfo *adinfo)
 
static void dumpSequence (Archive *fout, TableInfo *tbinfo)
 
static void dumpSequenceData (Archive *fout, TableDataInfo *tdinfo)
 
static void dumpIndex (Archive *fout, IndxInfo *indxinfo)
 
static void dumpIndexAttach (Archive *fout, IndexAttachInfo *attachinfo)
 
static void dumpStatisticsExt (Archive *fout, StatsExtInfo *statsextinfo)
 
static void dumpConstraint (Archive *fout, ConstraintInfo *coninfo)
 
static void dumpTableConstraintComment (Archive *fout, ConstraintInfo *coninfo)
 
static void dumpTSParser (Archive *fout, TSParserInfo *prsinfo)
 
static void dumpTSDictionary (Archive *fout, TSDictInfo *dictinfo)
 
static void dumpTSTemplate (Archive *fout, TSTemplateInfo *tmplinfo)
 
static void dumpTSConfig (Archive *fout, TSConfigInfo *cfginfo)
 
static void dumpForeignDataWrapper (Archive *fout, FdwInfo *fdwinfo)
 
static void dumpForeignServer (Archive *fout, 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, DefaultACLInfo *daclinfo)
 
static void dumpACL (Archive *fout, CatalogId objCatId, DumpId objDumpId, 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, DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
 
static DumpableObjectcreateBoundaryObjects (void)
 
static void addBoundaryDependencies (DumpableObject **dobjs, int numObjs, DumpableObject *boundaryObjs)
 
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 (FuncInfo *finfo, char *funcargs, bool is_agg)
 
static char * format_function_arguments_old (Archive *fout, FuncInfo *finfo, int nallargs, char **allargtypes, char **argmodes, char **argnames)
 
static char * format_function_signature (Archive *fout, FuncInfo *finfo, bool honor_quotes)
 
static char * convertRegProcReference (Archive *fout, const char *proc)
 
static char * getFormattedOperatorName (Archive *fout, 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, BlobInfo *binfo)
 
static int dumpBlobs (Archive *fout, void *arg)
 
static void dumpPolicy (Archive *fout, PolicyInfo *polinfo)
 
static void dumpPublication (Archive *fout, PublicationInfo *pubinfo)
 
static void dumpPublicationTable (Archive *fout, PublicationRelInfo *pubrinfo)
 
static void dumpSubscription (Archive *fout, 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 binary_upgrade_set_type_oids_by_type_oid (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type)
 
static bool 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, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
 
static const char * getAttrName (int attrnum, TableInfo *tblInfo)
 
static const char * fmtCopyColumnList (const TableInfo *ti, PQExpBuffer buffer)
 
static bool nonemptyReloptions (const char *reloptions)
 
static void appendReloptionsArrayAH (PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
 
static char * get_synchronized_snapshot (Archive *fout)
 
static void setupDumpWorker (Archive *AHX)
 
static TableInfogetRootTableInfo (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, void *dcontext)
 
static int dumpTableData_insert (Archive *fout, void *dcontext)
 
void getPolicies (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getPublications (Archive *fout)
 
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, DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
 
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 (DumpOptions *dopt, 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, TableInfo *tbinfo, const char *reltypename)
 
static char * format_aggregate_signature (AggInfo *agginfo, Archive *fout, bool honor_quotes)
 
static void dumpTableSecLabel (Archive *fout, TableInfo *tbinfo, const char *reltypename)
 
static PQExpBuffer createViewAsClause (Archive *fout, TableInfo *tbinfo)
 
static PQExpBuffer createDummyViewAsClause (Archive *fout, 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 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 135 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 83 of file pg_dump.c.

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

Function Documentation

◆ addBoundaryDependencies()

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

Definition at line 18341 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_DATA, DO_TRANSFORM, DO_TRIGGER, DO_TSCONFIG, DO_TSDICT, DO_TSPARSER, DO_TSTEMPLATE, DO_TYPE, _dumpableObject::dumpId, i, and _dumpableObject::objType.

Referenced by main().

18343 {
18344  DumpableObject *preDataBound = boundaryObjs + 0;
18345  DumpableObject *postDataBound = boundaryObjs + 1;
18346  int i;
18347 
18348  for (i = 0; i < numObjs; i++)
18349  {
18350  DumpableObject *dobj = dobjs[i];
18351 
18352  /*
18353  * The classification of object types here must match the SECTION_xxx
18354  * values assigned during subsequent ArchiveEntry calls!
18355  */
18356  switch (dobj->objType)
18357  {
18358  case DO_NAMESPACE:
18359  case DO_EXTENSION:
18360  case DO_TYPE:
18361  case DO_SHELL_TYPE:
18362  case DO_FUNC:
18363  case DO_AGG:
18364  case DO_OPERATOR:
18365  case DO_ACCESS_METHOD:
18366  case DO_OPCLASS:
18367  case DO_OPFAMILY:
18368  case DO_COLLATION:
18369  case DO_CONVERSION:
18370  case DO_TABLE:
18371  case DO_ATTRDEF:
18372  case DO_PROCLANG:
18373  case DO_CAST:
18374  case DO_DUMMY_TYPE:
18375  case DO_TSPARSER:
18376  case DO_TSDICT:
18377  case DO_TSTEMPLATE:
18378  case DO_TSCONFIG:
18379  case DO_FDW:
18380  case DO_FOREIGN_SERVER:
18381  case DO_TRANSFORM:
18382  case DO_BLOB:
18383  /* Pre-data objects: must come before the pre-data boundary */
18384  addObjectDependency(preDataBound, dobj->dumpId);
18385  break;
18386  case DO_TABLE_DATA:
18387  case DO_SEQUENCE_SET:
18388  case DO_BLOB_DATA:
18389  /* Data objects: must come between the boundaries */
18390  addObjectDependency(dobj, preDataBound->dumpId);
18391  addObjectDependency(postDataBound, dobj->dumpId);
18392  break;
18393  case DO_INDEX:
18394  case DO_INDEX_ATTACH:
18395  case DO_STATSEXT:
18396  case DO_REFRESH_MATVIEW:
18397  case DO_TRIGGER:
18398  case DO_EVENT_TRIGGER:
18399  case DO_DEFAULT_ACL:
18400  case DO_POLICY:
18401  case DO_PUBLICATION:
18402  case DO_PUBLICATION_REL:
18403  case DO_SUBSCRIPTION:
18404  /* Post-data objects: must come after the post-data boundary */
18405  addObjectDependency(dobj, postDataBound->dumpId);
18406  break;
18407  case DO_RULE:
18408  /* Rules are post-data, but only if dumped separately */
18409  if (((RuleInfo *) dobj)->separate)
18410  addObjectDependency(dobj, postDataBound->dumpId);
18411  break;
18412  case DO_CONSTRAINT:
18413  case DO_FK_CONSTRAINT:
18414  /* Constraints are post-data, but only if dumped separately */
18415  if (((ConstraintInfo *) dobj)->separate)
18416  addObjectDependency(dobj, postDataBound->dumpId);
18417  break;
18418  case DO_PRE_DATA_BOUNDARY:
18419  /* nothing to do */
18420  break;
18421  case DO_POST_DATA_BOUNDARY:
18422  /* must come after the pre-data boundary */
18423  addObjectDependency(dobj, preDataBound->dumpId);
18424  break;
18425  }
18426  }
18427 }
DumpId dumpId
Definition: pg_dump.h:129
Definition: pg_dump.h:45
Definition: pg_dump.h:70
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:782
int i
DumpableObjectType objType
Definition: pg_dump.h:127

◆ append_depends_on_extension()

static void append_depends_on_extension ( Archive fout,
PQExpBuffer  create,
DumpableObject dobj,
const char *  catalog,
const char *  keyword,
const char *  objname 
)
static

Definition at line 4382 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().

4388 {
4389  if (dobj->depends_on_ext)
4390  {
4391  char *nm;
4392  PGresult *res;
4393  PQExpBuffer query;
4394  int ntups;
4395  int i_extname;
4396  int i;
4397 
4398  /* dodge fmtId() non-reentrancy */
4399  nm = pg_strdup(objname);
4400 
4401  query = createPQExpBuffer();
4402  appendPQExpBuffer(query,
4403  "SELECT e.extname "
4404  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4405  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4406  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4407  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4408  catalog,
4409  dobj->catId.oid);
4410  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4411  ntups = PQntuples(res);
4412  i_extname = PQfnumber(res, "extname");
4413  for (i = 0; i < ntups; i++)
4414  {
4415  appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4416  keyword, nm,
4417  fmtId(PQgetvalue(res, i, i_extname)));
4418  }
4419 
4420  PQclear(res);
4421  destroyPQExpBuffer(query);
4422  pg_free(nm);
4423  }
4424 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
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:2877
bool depends_on_ext
Definition: pg_dump.h:135
void PQclear(PGresult *res)
Definition: fe-exec.c:694
void pg_free(void *ptr)
Definition: fe_memutils.c:105
CatalogId catId
Definition: pg_dump.h:128
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401

◆ appendReloptionsArrayAH()

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

Definition at line 18640 of file pg_dump.c.

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

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

18642 {
18643  bool res;
18644 
18645  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18646  fout->std_strings);
18647  if (!res)
18648  pg_log_warning("could not parse reloptions array");
18649 }
int encoding
Definition: pg_backup.h:197
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:198

◆ binary_upgrade_extension_member()

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

Definition at line 4619 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().

4624 {
4625  DumpableObject *extobj = NULL;
4626  int i;
4627 
4628  if (!dobj->ext_member)
4629  return;
4630 
4631  /*
4632  * Find the parent extension. We could avoid this search if we wanted to
4633  * add a link field to DumpableObject, but the space costs of that would
4634  * be considerable. We assume that member objects could only have a
4635  * direct dependency on their own extension, not any others.
4636  */
4637  for (i = 0; i < dobj->nDeps; i++)
4638  {
4639  extobj = findObjectByDumpId(dobj->dependencies[i]);
4640  if (extobj && extobj->objType == DO_EXTENSION)
4641  break;
4642  extobj = NULL;
4643  }
4644  if (extobj == NULL)
4645  fatal("could not find parent extension for %s %s",
4646  objtype, objname);
4647 
4648  appendPQExpBufferStr(upgrade_buffer,
4649  "\n-- For binary upgrade, handle extension membership the hard way\n");
4650  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4651  fmtId(extobj->name),
4652  objtype);
4653  if (objnamespace && *objnamespace)
4654  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4655  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4656 }
char * name
Definition: pg_dump.h:130
DumpId * dependencies
Definition: pg_dump.h:136
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:134
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:610
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define fatal(...)
int i
DumpableObjectType objType
Definition: pg_dump.h:127

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

4552 {
4553  PQExpBuffer upgrade_query = createPQExpBuffer();
4554  PGresult *upgrade_res;
4555  Oid pg_class_reltoastrelid;
4556  Oid pg_index_indexrelid;
4557 
4558  appendPQExpBuffer(upgrade_query,
4559  "SELECT c.reltoastrelid, i.indexrelid "
4560  "FROM pg_catalog.pg_class c LEFT JOIN "
4561  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4562  "WHERE c.oid = '%u'::pg_catalog.oid;",
4563  pg_class_oid);
4564 
4565  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4566 
4567  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
4568  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
4569 
4570  appendPQExpBufferStr(upgrade_buffer,
4571  "\n-- For binary upgrade, must preserve pg_class oids\n");
4572 
4573  if (!is_index)
4574  {
4575  appendPQExpBuffer(upgrade_buffer,
4576  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4577  pg_class_oid);
4578  /* only tables have toast tables, not indexes */
4579  if (OidIsValid(pg_class_reltoastrelid))
4580  {
4581  /*
4582  * One complexity is that the table definition might not require
4583  * the creation of a TOAST table, and the TOAST table might have
4584  * been created long after table creation, when the table was
4585  * loaded with wide data. By setting the TOAST oid we force
4586  * creation of the TOAST heap and TOAST index by the backend so we
4587  * can cleanly copy the files during binary upgrade.
4588  */
4589 
4590  appendPQExpBuffer(upgrade_buffer,
4591  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4592  pg_class_reltoastrelid);
4593 
4594  /* every toast table has an index */
4595  appendPQExpBuffer(upgrade_buffer,
4596  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4597  pg_index_indexrelid);
4598  }
4599  }
4600  else
4601  appendPQExpBuffer(upgrade_buffer,
4602  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4603  pg_class_oid);
4604 
4605  appendPQExpBufferChar(upgrade_buffer, '\n');
4606 
4607  PQclear(upgrade_res);
4608  destroyPQExpBuffer(upgrade_query);
4609 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
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:644
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
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:2877
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void PQclear(PGresult *res)
Definition: fe-exec.c:694

◆ binary_upgrade_set_type_oids_by_rel_oid()

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

Definition at line 4497 of file pg_dump.c.

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

Referenced by dumpSequence(), and dumpTableSchema().

4500 {
4501  PQExpBuffer upgrade_query = createPQExpBuffer();
4502  PGresult *upgrade_res;
4503  Oid pg_type_oid;
4504  bool toast_set = false;
4505 
4506  /*
4507  * We only support old >= 8.3 for binary upgrades.
4508  *
4509  * We purposefully ignore toast OIDs for partitioned tables; the reason is
4510  * that versions 10 and 11 have them, but 12 does not, so emitting them
4511  * causes the upgrade to fail.
4512  */
4513  appendPQExpBuffer(upgrade_query,
4514  "SELECT c.reltype AS crel, t.reltype AS trel "
4515  "FROM pg_catalog.pg_class c "
4516  "LEFT JOIN pg_catalog.pg_class t ON "
4517  " (c.reltoastrelid = t.oid AND c.relkind <> '%c') "
4518  "WHERE c.oid = '%u'::pg_catalog.oid;",
4519  RELKIND_PARTITIONED_TABLE, pg_rel_oid);
4520 
4521  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4522 
4523  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4524 
4525  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4526  pg_type_oid, false);
4527 
4528  if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
4529  {
4530  /* Toast tables do not have pg_type array rows */
4531  Oid pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
4532  PQfnumber(upgrade_res, "trel")));
4533 
4534  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
4535  appendPQExpBuffer(upgrade_buffer,
4536  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4537  pg_type_toast_oid);
4538 
4539  toast_set = true;
4540  }
4541 
4542  PQclear(upgrade_res);
4543  destroyPQExpBuffer(upgrade_query);
4544 
4545  return toast_set;
4546 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned int Oid
Definition: postgres_ext.h:31
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
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
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type)
Definition: pg_dump.c:4428
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188

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

Definition at line 4428 of file pg_dump.c.

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

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

4432 {
4433  PQExpBuffer upgrade_query = createPQExpBuffer();
4434  PGresult *res;
4435  Oid pg_type_array_oid;
4436 
4437  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4438  appendPQExpBuffer(upgrade_buffer,
4439  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4440  pg_type_oid);
4441 
4442  /* we only support old >= 8.3 for binary upgrades */
4443  appendPQExpBuffer(upgrade_query,
4444  "SELECT typarray "
4445  "FROM pg_catalog.pg_type "
4446  "WHERE oid = '%u'::pg_catalog.oid;",
4447  pg_type_oid);
4448 
4449  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4450 
4451  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4452 
4453  PQclear(res);
4454 
4455  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4456  {
4457  /*
4458  * If the old version didn't assign an array type, but the new version
4459  * does, we must select an unused type OID to assign. This currently
4460  * only happens for domains, when upgrading pre-v11 to v11 and up.
4461  *
4462  * Note: local state here is kind of ugly, but we must have some,
4463  * since we mustn't choose the same unused OID more than once.
4464  */
4465  static Oid next_possible_free_oid = FirstNormalObjectId;
4466  bool is_dup;
4467 
4468  do
4469  {
4470  ++next_possible_free_oid;
4471  printfPQExpBuffer(upgrade_query,
4472  "SELECT EXISTS(SELECT 1 "
4473  "FROM pg_catalog.pg_type "
4474  "WHERE oid = '%u'::pg_catalog.oid);",
4475  next_possible_free_oid);
4476  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4477  is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
4478  PQclear(res);
4479  } while (is_dup);
4480 
4481  pg_type_array_oid = next_possible_free_oid;
4482  }
4483 
4484  if (OidIsValid(pg_type_array_oid))
4485  {
4486  appendPQExpBufferStr(upgrade_buffer,
4487  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4488  appendPQExpBuffer(upgrade_buffer,
4489  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4490  pg_type_array_oid);
4491  }
4492 
4493  destroyPQExpBuffer(upgrade_query);
4494 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
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:644
#define FirstNormalObjectId
Definition: transam.h:155
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
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:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

Definition at line 18454 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().

18455 {
18456  ArchiveHandle *AH = (ArchiveHandle *) fout;
18457  TocEntry *te;
18458 
18459  /* Scan all TOC entries in the archive */
18460  for (te = AH->toc->next; te != AH->toc; te = te->next)
18461  {
18462  DumpableObject *dobj;
18463  DumpId *dependencies;
18464  int nDeps;
18465  int allocDeps;
18466 
18467  /* No need to process entries that will not be dumped */
18468  if (te->reqs == 0)
18469  continue;
18470  /* Ignore entries that already have "special" dependencies */
18471  if (te->nDeps > 0)
18472  continue;
18473  /* Otherwise, look up the item's original DumpableObject, if any */
18474  dobj = findObjectByDumpId(te->dumpId);
18475  if (dobj == NULL)
18476  continue;
18477  /* No work if it has no dependencies */
18478  if (dobj->nDeps <= 0)
18479  continue;
18480  /* Set up work array */
18481  allocDeps = 64;
18482  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18483  nDeps = 0;
18484  /* Recursively find all dumpable dependencies */
18485  findDumpableDependencies(AH, dobj,
18486  &dependencies, &nDeps, &allocDeps);
18487  /* And save 'em ... */
18488  if (nDeps > 0)
18489  {
18490  dependencies = (DumpId *) pg_realloc(dependencies,
18491  nDeps * sizeof(DumpId));
18492  te->dependencies = dependencies;
18493  te->nDeps = nDeps;
18494  }
18495  else
18496  free(dependencies);
18497  }
18498 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:234
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:610
struct _tocEntry * toc
DumpId * dependencies
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
static void findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:18502
#define free(a)
Definition: header.h:65

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

Definition at line 2458 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().

2459 {
2460  PQExpBuffer query;
2461  PGresult *res;
2462  int ntups,
2463  i;
2464  int i_classid,
2465  i_objid,
2466  i_refobjid;
2467 
2468  /* No Mat Views before 9.3. */
2469  if (fout->remoteVersion < 90300)
2470  return;
2471 
2472  query = createPQExpBuffer();
2473 
2474  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2475  "( "
2476  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2477  "FROM pg_depend d1 "
2478  "JOIN pg_class c1 ON c1.oid = d1.objid "
2479  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2480  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2481  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2482  "AND d2.objid = r1.oid "
2483  "AND d2.refobjid <> d1.objid "
2484  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2485  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2486  CppAsString2(RELKIND_VIEW) ") "
2487  "WHERE d1.classid = 'pg_class'::regclass "
2488  "UNION "
2489  "SELECT w.objid, d3.refobjid, c3.relkind "
2490  "FROM w "
2491  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2492  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2493  "AND d3.objid = r3.oid "
2494  "AND d3.refobjid <> w.refobjid "
2495  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2496  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2497  CppAsString2(RELKIND_VIEW) ") "
2498  ") "
2499  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2500  "FROM w "
2501  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2502 
2503  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2504 
2505  ntups = PQntuples(res);
2506 
2507  i_classid = PQfnumber(res, "classid");
2508  i_objid = PQfnumber(res, "objid");
2509  i_refobjid = PQfnumber(res, "refobjid");
2510 
2511  for (i = 0; i < ntups; i++)
2512  {
2513  CatalogId objId;
2514  CatalogId refobjId;
2515  DumpableObject *dobj;
2516  DumpableObject *refdobj;
2517  TableInfo *tbinfo;
2518  TableInfo *reftbinfo;
2519 
2520  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2521  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2522  refobjId.tableoid = objId.tableoid;
2523  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2524 
2525  dobj = findObjectByCatalogId(objId);
2526  if (dobj == NULL)
2527  continue;
2528 
2529  Assert(dobj->objType == DO_TABLE);
2530  tbinfo = (TableInfo *) dobj;
2531  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2532  dobj = (DumpableObject *) tbinfo->dataObj;
2533  if (dobj == NULL)
2534  continue;
2535  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2536 
2537  refdobj = findObjectByCatalogId(refobjId);
2538  if (refdobj == NULL)
2539  continue;
2540 
2541  Assert(refdobj->objType == DO_TABLE);
2542  reftbinfo = (TableInfo *) refdobj;
2543  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2544  refdobj = (DumpableObject *) reftbinfo->dataObj;
2545  if (refdobj == NULL)
2546  continue;
2547  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2548 
2549  addObjectDependency(dobj, refdobj->dumpId);
2550 
2551  if (!reftbinfo->relispopulated)
2552  tbinfo->relispopulated = false;
2553  }
2554 
2555  PQclear(res);
2556 
2557  destroyPQExpBuffer(query);
2558 }
char relkind
Definition: pg_dump.h:265
Oid tableoid
Definition: pg_backup.h:230
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpId dumpId
Definition: pg_dump.h:129
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:628
#define atooid(x)
Definition: postgres_ext.h:42
#define CppAsString2(x)
Definition: c.h:222
struct _tableDataInfo * dataObj
Definition: pg_dump.h:335
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:738
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:782
int i
bool relispopulated
Definition: pg_dump.h:267
DumpableObjectType objType
Definition: pg_dump.h:127
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401
int remoteVersion
Definition: pg_backup.h:187

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

Definition at line 1470 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().

1471 {
1472  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1473 
1474  if (ext == NULL)
1475  return false;
1476 
1477  dobj->ext_member = true;
1478 
1479  /* Record dependency so that getDependencies needn't deal with that */
1480  addObjectDependency(dobj, ext->dobj.dumpId);
1481 
1482  /*
1483  * In 9.6 and above, mark the member object to have any non-initial ACL,
1484  * policies, and security labels dumped.
1485  *
1486  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1487  * extract the information about the object. We don't provide support for
1488  * initial policies and security labels and it seems unlikely for those to
1489  * ever exist, but we may have to revisit this later.
1490  *
1491  * Prior to 9.6, we do not include any extension member components.
1492  *
1493  * In binary upgrades, we still dump all components of the members
1494  * individually, since the idea is to exactly reproduce the database
1495  * contents rather than replace the extension contents with something
1496  * different.
1497  */
1498  if (fout->dopt->binary_upgrade)
1499  dobj->dump = ext->dobj.dump;
1500  else
1501  {
1502  if (fout->remoteVersion < 90600)
1503  dobj->dump = DUMP_COMPONENT_NONE;
1504  else
1505  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1508  }
1509 
1510  return true;
1511 }
DumpableObject dobj
Definition: pg_dump.h:153
DumpComponents dump
Definition: pg_dump.h:131
DumpId dumpId
Definition: pg_dump.h:129
bool ext_member
Definition: pg_dump.h:134
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:88
int binary_upgrade
Definition: pg_backup.h:135
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:934
DumpOptions * dopt
Definition: pg_backup.h:182
DumpComponents dump_contains
Definition: pg_dump.h:133
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:782
CatalogId catId
Definition: pg_dump.h:128
#define DUMP_COMPONENT_POLICY
Definition: pg_dump.h:94
int remoteVersion
Definition: pg_backup.h:187

◆ collectComments()

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

Definition at line 9947 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().

9948 {
9949  PGresult *res;
9950  PQExpBuffer query;
9951  int i_description;
9952  int i_classoid;
9953  int i_objoid;
9954  int i_objsubid;
9955  int ntups;
9956  int i;
9957  CommentItem *comments;
9958 
9959  query = createPQExpBuffer();
9960 
9961  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9962  "FROM pg_catalog.pg_description "
9963  "ORDER BY classoid, objoid, objsubid");
9964 
9965  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9966 
9967  /* Construct lookup table containing OIDs in numeric form */
9968 
9969  i_description = PQfnumber(res, "description");
9970  i_classoid = PQfnumber(res, "classoid");
9971  i_objoid = PQfnumber(res, "objoid");
9972  i_objsubid = PQfnumber(res, "objsubid");
9973 
9974  ntups = PQntuples(res);
9975 
9976  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9977 
9978  for (i = 0; i < ntups; i++)
9979  {
9980  comments[i].descr = PQgetvalue(res, i, i_description);
9981  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9982  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9983  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9984  }
9985 
9986  /* Do NOT free the PGresult since we are keeping pointers into it */
9987  destroyPQExpBuffer(query);
9988 
9989  *items = comments;
9990  return ntups;
9991 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
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:71
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
Oid objoid
Definition: pg_dump.c:70
Oid classoid
Definition: pg_dump.c:69
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401

◆ collectSecLabels()

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

Definition at line 15428 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().

15429 {
15430  PGresult *res;
15431  PQExpBuffer query;
15432  int i_label;
15433  int i_provider;
15434  int i_classoid;
15435  int i_objoid;
15436  int i_objsubid;
15437  int ntups;
15438  int i;
15439  SecLabelItem *labels;
15440 
15441  query = createPQExpBuffer();
15442 
15443  appendPQExpBufferStr(query,
15444  "SELECT label, provider, classoid, objoid, objsubid "
15445  "FROM pg_catalog.pg_seclabel "
15446  "ORDER BY classoid, objoid, objsubid");
15447 
15448  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15449 
15450  /* Construct lookup table containing OIDs in numeric form */
15451  i_label = PQfnumber(res, "label");
15452  i_provider = PQfnumber(res, "provider");
15453  i_classoid = PQfnumber(res, "classoid");
15454  i_objoid = PQfnumber(res, "objoid");
15455  i_objsubid = PQfnumber(res, "objsubid");
15456 
15457  ntups = PQntuples(res);
15458 
15459  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15460 
15461  for (i = 0; i < ntups; i++)
15462  {
15463  labels[i].label = PQgetvalue(res, i, i_label);
15464  labels[i].provider = PQgetvalue(res, i, i_provider);
15465  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15466  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15467  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15468  }
15469 
15470  /* Do NOT free the PGresult since we are keeping pointers into it */
15471  destroyPQExpBuffer(query);
15472 
15473  *items = labels;
15474  return ntups;
15475 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
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:2769
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
Oid objoid
Definition: pg_dump.c:79
const char * provider
Definition: pg_dump.c:76
#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:2877
int objsubid
Definition: pg_dump.c:80
int i
Oid classoid
Definition: pg_dump.c:78
const char * label
Definition: pg_dump.c:77
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401

◆ convertRegProcReference()

static char * convertRegProcReference ( Archive fout,
const char *  proc 
)
static

Definition at line 12850 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

12851 {
12852  char *name;
12853  char *paren;
12854  bool inquote;
12855 
12856  /* In all cases "-" means a null reference */
12857  if (strcmp(proc, "-") == 0)
12858  return NULL;
12859 
12860  name = pg_strdup(proc);
12861  /* find non-double-quoted left paren */
12862  inquote = false;
12863  for (paren = name; *paren; paren++)
12864  {
12865  if (*paren == '(' && !inquote)
12866  {
12867  *paren = '\0';
12868  break;
12869  }
12870  if (*paren == '"')
12871  inquote = !inquote;
12872  }
12873  return name;
12874 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * name
Definition: encode.c:555

◆ convertTSFunction()

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

Definition at line 12921 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

12922 {
12923  char *result;
12924  char query[128];
12925  PGresult *res;
12926 
12927  snprintf(query, sizeof(query),
12928  "SELECT '%u'::pg_catalog.regproc", funcOid);
12929  res = ExecuteSqlQueryForSingleRow(fout, query);
12930 
12931  result = pg_strdup(PQgetvalue(res, 0, 0));
12932 
12933  PQclear(res);
12934 
12935  return result;
12936 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define snprintf
Definition: port.h:193

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

Definition at line 18317 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().

18318 {
18319  DumpableObject *dobjs;
18320 
18321  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18322 
18323  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18324  dobjs[0].catId = nilCatalogId;
18325  AssignDumpId(dobjs + 0);
18326  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18327 
18328  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18329  dobjs[1].catId = nilCatalogId;
18330  AssignDumpId(dobjs + 1);
18331  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18332 
18333  return dobjs;
18334 }
char * name
Definition: pg_dump.h:130
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:545
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:128
static const CatalogId nilCatalogId
Definition: pg_dump.c:125
DumpableObjectType objType
Definition: pg_dump.h:127

◆ createDummyViewAsClause()

static PQExpBuffer createDummyViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

Definition at line 15664 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().

15665 {
15666  PQExpBuffer result = createPQExpBuffer();
15667  int j;
15668 
15669  appendPQExpBufferStr(result, "SELECT");
15670 
15671  for (j = 0; j < tbinfo->numatts; j++)
15672  {
15673  if (j > 0)
15674  appendPQExpBufferChar(result, ',');
15675  appendPQExpBufferStr(result, "\n ");
15676 
15677  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15678 
15679  /*
15680  * Must add collation if not default for the type, because CREATE OR
15681  * REPLACE VIEW won't change it
15682  */
15683  if (OidIsValid(tbinfo->attcollation[j]))
15684  {
15685  CollInfo *coll;
15686 
15687  coll = findCollationByOid(tbinfo->attcollation[j]);
15688  if (coll)
15689  appendPQExpBuffer(result, " COLLATE %s",
15690  fmtQualifiedDumpable(coll));
15691  }
15692 
15693  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15694  }
15695 
15696  return result;
15697 }
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:304
char ** attnames
Definition: pg_dump.h:303
#define OidIsValid(objectId)
Definition: c.h:644
Oid * attcollation
Definition: pg_dump.h:316
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
int numatts
Definition: pg_dump.h:302
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:140
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:873

◆ createViewAsClause()

static PQExpBuffer createViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

Definition at line 15615 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().

15616 {
15617  PQExpBuffer query = createPQExpBuffer();
15618  PQExpBuffer result = createPQExpBuffer();
15619  PGresult *res;
15620  int len;
15621 
15622  /* Fetch the view definition */
15623  appendPQExpBuffer(query,
15624  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15625  tbinfo->dobj.catId.oid);
15626 
15627  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15628 
15629  if (PQntuples(res) != 1)
15630  {
15631  if (PQntuples(res) < 1)
15632  fatal("query to obtain definition of view \"%s\" returned no data",
15633  tbinfo->dobj.name);
15634  else
15635  fatal("query to obtain definition of view \"%s\" returned more than one definition",
15636  tbinfo->dobj.name);
15637  }
15638 
15639  len = PQgetlength(res, 0, 0);
15640 
15641  if (len == 0)
15642  fatal("definition of view \"%s\" appears to be empty (length zero)",
15643  tbinfo->dobj.name);
15644 
15645  /* Strip off the trailing semicolon so that other things may follow. */
15646  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15647  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15648 
15649  PQclear(res);
15650  destroyPQExpBuffer(query);
15651 
15652  return result;
15653 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3174
char * name
Definition: pg_dump.h:130
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
DumpableObject dobj
Definition: pg_dump.h:259
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:694
#define Assert(condition)
Definition: c.h:738
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:128
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:401

◆ dumpAccessMethod()

static void dumpAccessMethod ( Archive fout,
AccessMethodInfo oprinfo 
)
static

Definition at line 12943 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().

12944 {
12945  DumpOptions *dopt = fout->dopt;
12946  PQExpBuffer q;
12947  PQExpBuffer delq;
12948  char *qamname;
12949 
12950  /* Skip if not to be dumped */
12951  if (!aminfo->dobj.dump || dopt->dataOnly)
12952  return;
12953 
12954  q = createPQExpBuffer();
12955  delq = createPQExpBuffer();
12956 
12957  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12958 
12959  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12960 
12961  switch (aminfo->amtype)
12962  {
12963  case AMTYPE_INDEX:
12964  appendPQExpBufferStr(q, "TYPE INDEX ");
12965  break;
12966  case AMTYPE_TABLE:
12967  appendPQExpBufferStr(q, "TYPE TABLE ");
12968  break;
12969  default:
12970  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12971  aminfo->amtype, qamname);
12972  destroyPQExpBuffer(q);
12973  destroyPQExpBuffer(delq);
12974  free(qamname);
12975  return;
12976  }
12977 
12978  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12979 
12980  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12981  qamname);
12982 
12983  if (dopt->binary_upgrade)
12984  binary_upgrade_extension_member(q, &aminfo->dobj,
12985  "ACCESS METHOD", qamname, NULL);
12986 
12987  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12988  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12989  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12990  .description = "ACCESS METHOD",
12991  .section = SECTION_PRE_DATA,
12992  .createStmt = q->data,
12993  .dropStmt = delq->data));
12994 
12995  /* Dump Access Method Comments */
12996  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12997  dumpComment(fout, "ACCESS METHOD", qamname,
12998  NULL, "",
12999  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
13000 
13001  destroyPQExpBuffer(q);
13002  destroyPQExpBuffer(delq);
13003  free(qamname);
13004 }
#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:135
DumpOptions * dopt
Definition: pg_backup.h:182
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:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4619
#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:9684

◆ dumpACL()

static void dumpACL ( Archive fout,
CatalogId  objCatId,
DumpId  objDumpId,
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 15094 of file pg_dump.c.

References _dumpOptions::aclsSkip, appendPQExpBuffer(), appendPQExpBufferStr(), ARCHIVE_OPTS, ArchiveEntry(), buildACLCommands(), createDumpId(), createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), Archive::dopt, fatal, 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().

15099 {
15100  DumpOptions *dopt = fout->dopt;
15101  PQExpBuffer sql;
15102 
15103  /* Do nothing if ACL dump is not enabled */
15104  if (dopt->aclsSkip)
15105  return;
15106 
15107  /* --data-only skips ACLs *except* BLOB ACLs */
15108  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
15109  return;
15110 
15111  sql = createPQExpBuffer();
15112 
15113  /*
15114  * Check to see if this object has had any initial ACLs included for it.
15115  * If so, we are in binary upgrade mode and these are the ACLs to turn
15116  * into GRANT and REVOKE statements to set and record the initial
15117  * privileges for an extension object. Let the backend know that these
15118  * are to be recorded by calling binary_upgrade_set_record_init_privs()
15119  * before and after.
15120  */
15121  if (strlen(initacls) != 0 || strlen(initracls) != 0)
15122  {
15123  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
15124  if (!buildACLCommands(name, subname, nspname, type,
15125  initacls, initracls, owner,
15126  "", fout->remoteVersion, sql))
15127  fatal("could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)",
15128  initacls, initracls, name, type);
15129  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
15130  }
15131 
15132  if (!buildACLCommands(name, subname, nspname, type,
15133  acls, racls, owner,
15134  "", fout->remoteVersion, sql))
15135  fatal("could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)",
15136  acls, racls, name, type);
15137 
15138  if (sql->len > 0)
15139  {
15141 
15142  if (subname)
15143  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
15144  else
15145  appendPQExpBuffer(tag, "%s %s", type, name);
15146 
15148  ARCHIVE_OPTS(.tag = tag->data,
15149  .namespace = nspname,
15150  .owner = owner,
15151  .description = "ACL",
15152  .section = SECTION_NONE,
15153  .createStmt = sql->data,
15154  .deps = &objDumpId,
15155  .nDeps = 1));
15156  destroyPQExpBuffer(tag);
15157  }
15158 
15159  destroyPQExpBuffer(sql);
15160 }
#define ARCHIVE_OPTS(...)
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:182
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:139
bool aclsSkip
Definition: pg_backup.h:141
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
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:555
static const CatalogId nilCatalogId
Definition: pg_dump.c:125
DumpId createDumpId(void)
Definition: common.c:590
int remoteVersion
Definition: pg_backup.h:187

◆ dumpAgg()

static void dumpAgg ( Archive fout,
AggInfo agginfo 
)
static

Definition at line 13931 of file pg_dump.c.

References _aggInfo::aggfn, appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _funcInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), _dumpableObject::dumpId, dumpSecLabel(), ExecuteSqlQueryForSingleRow(), fatal, fmtId(), format_aggregate_signature(), format_function_arguments(), format_function_signature(), free, getFormattedOperatorName(), _funcInfo::initproacl, _funcInfo::initrproacl, _dumpableObject::name, CatalogId::oid, pg_log_warning, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), _funcInfo::proacl, Archive::remoteVersion, _funcInfo::rolname, _funcInfo::rproacl, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

13932 {
13933  DumpOptions *dopt = fout->dopt;
13934  PQExpBuffer query;
13935  PQExpBuffer q;
13936  PQExpBuffer delq;
13937  PQExpBuffer details;
13938  char *aggsig; /* identity signature */
13939  char *aggfullsig = NULL; /* full signature */
13940  char *aggsig_tag;
13941  PGresult *res;
13942  int i_aggtransfn;
13943  int i_aggfinalfn;
13944  int i_aggcombinefn;
13945  int i_aggserialfn;
13946  int i_aggdeserialfn;
13947  int i_aggmtransfn;
13948  int i_aggminvtransfn;
13949  int i_aggmfinalfn;
13950  int i_aggfinalextra;
13951  int i_aggmfinalextra;
13952  int i_aggfinalmodify;
13953  int i_aggmfinalmodify;
13954  int i_aggsortop;
13955  int i_aggkind;
13956  int i_aggtranstype;
13957  int i_aggtransspace;
13958  int i_aggmtranstype;
13959  int i_aggmtransspace;
13960  int i_agginitval;
13961  int i_aggminitval;
13962  int i_convertok;
13963  int i_proparallel;
13964  const char *aggtransfn;
13965  const char *aggfinalfn;
13966  const char *aggcombinefn;
13967  const char *aggserialfn;
13968  const char *aggdeserialfn;
13969  const char *aggmtransfn;
13970  const char *aggminvtransfn;
13971  const char *aggmfinalfn;
13972  bool aggfinalextra;
13973  bool aggmfinalextra;
13974  char aggfinalmodify;
13975  char aggmfinalmodify;
13976  const char *aggsortop;
13977  char *aggsortconvop;
13978  char aggkind;
13979  const char *aggtranstype;
13980  const char *aggtransspace;
13981  const char *aggmtranstype;
13982  const char *aggmtransspace;
13983  const char *agginitval;
13984  const char *aggminitval;
13985  bool convertok;
13986  const char *proparallel;
13987  char defaultfinalmodify;
13988 
13989  /* Skip if not to be dumped */
13990  if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13991  return;
13992 
13993  query = createPQExpBuffer();
13994  q = createPQExpBuffer();
13995  delq = createPQExpBuffer();
13996  details = createPQExpBuffer();
13997 
13998  /* Get aggregate-specific details */
13999  if (fout->remoteVersion >= 110000)
14000  {
14001  appendPQExpBuffer(query, "SELECT aggtransfn, "
14002  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
14003  "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
14004  "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
14005  "aggfinalextra, aggmfinalextra, "
14006  "aggfinalmodify, aggmfinalmodify, "
14007  "aggsortop, "
14008  "aggkind, "
14009  "aggtransspace, agginitval, "
14010  "aggmtransspace, aggminitval, "
14011  "true AS convertok, "
14012  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
14013  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
14014  "p.proparallel "
14015  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14016  "WHERE a.aggfnoid = p.oid "
14017  "AND p.oid = '%u'::pg_catalog.oid",
14018  agginfo->aggfn.dobj.catId.oid);
14019  }
14020  else if (fout->remoteVersion >= 90600)
14021  {
14022  appendPQExpBuffer(query, "SELECT aggtransfn, "
14023  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
14024  "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
14025  "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
14026  "aggfinalextra, aggmfinalextra, "
14027  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
14028  "aggsortop, "
14029  "aggkind, "
14030  "aggtransspace, agginitval, "
14031  "aggmtransspace, aggminitval, "
14032  "true AS convertok, "
14033  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
14034  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
14035  "p.proparallel "
14036  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14037  "WHERE a.aggfnoid = p.oid "
14038  "AND p.oid = '%u'::pg_catalog.oid",
14039  agginfo->aggfn.dobj.catId.oid);
14040  }
14041  else if (fout->remoteVersion >= 90400)
14042  {
14043  appendPQExpBuffer(query, "SELECT aggtransfn, "
14044  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
14045  "'-' AS aggcombinefn, '-' AS aggserialfn, "
14046  "'-' AS aggdeserialfn, aggmtransfn, aggminvtransfn, "
14047  "aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
14048  "aggfinalextra, aggmfinalextra, "
14049  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
14050  "aggsortop, "
14051  "aggkind, "
14052  "aggtransspace, agginitval, "
14053  "aggmtransspace, aggminitval, "
14054  "true AS convertok, "
14055  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
14056  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
14057  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14058  "WHERE a.aggfnoid = p.oid "
14059  "AND p.oid = '%u'::pg_catalog.oid",
14060  agginfo->aggfn.dobj.catId.oid);
14061  }
14062  else if (fout->remoteVersion >= 80400)
14063  {
14064  appendPQExpBuffer(query, "SELECT aggtransfn, "
14065  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
14066  "'-' AS aggcombinefn, '-' AS aggserialfn, "
14067  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
14068  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
14069  "0 AS aggmtranstype, false AS aggfinalextra, "
14070  "false AS aggmfinalextra, "
14071  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
14072  "aggsortop, "
14073  "'n' AS aggkind, "
14074  "0 AS aggtransspace, agginitval, "
14075  "0 AS aggmtransspace, NULL AS aggminitval, "
14076  "true AS convertok, "
14077  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
14078  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
14079  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14080  "WHERE a.aggfnoid = p.oid "
14081  "AND p.oid = '%u'::pg_catalog.oid",
14082  agginfo->aggfn.dobj.catId.oid);
14083  }
14084  else if (fout->remoteVersion >= 80100)
14085  {
14086  appendPQExpBuffer(query, "SELECT aggtransfn, "
14087  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
14088  "'-' AS aggcombinefn, '-' AS aggserialfn, "
14089  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
14090  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
14091  "0 AS aggmtranstype, false AS aggfinalextra, "
14092  "false AS aggmfinalextra, "
14093  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
14094  "aggsortop, "
14095  "'n' AS aggkind, "
14096  "0 AS aggtransspace, agginitval, "
14097  "0 AS aggmtransspace, NULL AS aggminitval, "
14098  "true AS convertok "
14099  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14100  "WHERE a.aggfnoid = p.oid "
14101  "AND p.oid = '%u'::pg_catalog.oid",
14102  agginfo->aggfn.dobj.catId.oid);
14103  }
14104  else
14105  {
14106  appendPQExpBuffer(query, "SELECT aggtransfn, "
14107  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
14108  "'-' AS aggcombinefn, '-' AS aggserialfn, "
14109  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
14110  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
14111  "0 AS aggmtranstype, false AS aggfinalextra, "
14112  "false AS aggmfinalextra, "
14113  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
14114  "0 AS aggsortop, "
14115  "'n' AS aggkind, "
14116  "0 AS aggtransspace, agginitval, "
14117  "0 AS aggmtransspace, NULL AS aggminitval, "
14118  "true AS convertok "
14119  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14120  "WHERE a.aggfnoid = p.oid "
14121  "AND p.oid = '%u'::pg_catalog.oid",
14122  agginfo->aggfn.dobj.catId.oid);
14123  }
14124 
14125  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14126 
14127  i_aggtransfn = PQfnumber(res, "aggtransfn");
14128  i_aggfinalfn = PQfnumber(res, "aggfinalfn");
14129  i_aggcombinefn = PQfnumber(res, "aggcombinefn");
14130  i_aggserialfn = PQfnumber(res, "aggserialfn");
14131  i_aggdeserialfn = PQfnumber(res, "aggdeserialfn");
14132  i_aggmtransfn = PQfnumber(res, "aggmtransfn");
14133  i_aggminvtransfn = PQfnumber(res, "aggminvtransfn");
14134  i_aggmfinalfn = PQfnumber(res, "aggmfinalfn");
14135  i_aggfinalextra = PQfnumber(res, "aggfinalextra");
14136  i_aggmfinalextra = PQfnumber(res, "aggmfinalextra");
14137  i_aggfinalmodify = PQfnumber(res, "aggfinalmodify");
14138  i_aggmfinalmodify = PQfnumber(res, "aggmfinalmodify");
14139  i_aggsortop = PQfnumber(res, "aggsortop");
14140  i_aggkind = PQfnumber(res, "aggkind");
14141  i_aggtranstype = PQfnumber(res, "aggtranstype");
14142  i_aggtransspace = PQfnumber(res, "aggtransspace");
14143  i_aggmtranstype = PQfnumber(res, "aggmtranstype");
14144  i_aggmtransspace = PQfnumber(res, "aggmtransspace");
14145  i_agginitval = PQfnumber(res, "agginitval");
14146  i_aggminitval = PQfnumber(res, "aggminitval");
14147  i_convertok = PQfnumber(res, "convertok");
14148  i_proparallel = PQfnumber(res, "proparallel");
14149 
14150  aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
14151  aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
14152  aggcombinefn = PQgetvalue(res, 0, i_aggcombinefn);
14153  aggserialfn = PQgetvalue(res, 0, i_aggserialfn);
14154  aggdeserialfn = PQgetvalue(res, 0, i_aggdeserialfn);
14155  aggmtransfn = PQgetvalue(res, 0, i_aggmtransfn);
14156  aggminvtransfn = PQgetvalue(res, 0, i_aggminvtransfn);
14157  aggmfinalfn = PQgetvalue(res, 0, i_aggmfinalfn);
14158  aggfinalextra = (PQgetvalue(res, 0, i_aggfinalextra)[0] == 't');
14159  aggmfinalextra = (PQgetvalue(res, 0, i_aggmfinalextra)[0] == 't');
14160  aggfinalmodify = PQgetvalue(res, 0, i_aggfinalmodify)[0];
14161  aggmfinalmodify = PQgetvalue(res, 0, i_aggmfinalmodify)[0];
14162  aggsortop = PQgetvalue(res, 0, i_aggsortop);
14163  aggkind = PQgetvalue(res, 0, i_aggkind)[0];
14164  aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
14165  aggtransspace = PQgetvalue(res, 0, i_aggtransspace);
14166  aggmtranstype = PQgetvalue(res, 0, i_aggmtranstype);
14167  aggmtransspace = PQgetvalue(res, 0, i_aggmtransspace);
14168  agginitval = PQgetvalue(res, 0, i_agginitval);
14169  aggminitval = PQgetvalue(res, 0, i_aggminitval);
14170  convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
14171 
14172  if (fout->remoteVersion >= 80400)
14173  {
14174  /* 8.4 or later; we rely on server-side code for most of the work */
14175  char *funcargs;
14176  char *funciargs;
14177 
14178  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
14179  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
14180  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
14181  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
14182  }
14183  else
14184  /* pre-8.4, do it ourselves */
14185  aggsig = format_aggregate_signature(agginfo, fout, true);
14186 
14187  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14188 
14189  if (i_proparallel != -1)
14190  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
14191  else
14192  proparallel = NULL;
14193 
14194  if (!convertok)
14195  {
14196  pg_log_warning("aggregate function %s could not be dumped correctly for this database version; ignored",
14197  aggsig);
14198 
14199  if (aggfullsig)
14200  free(aggfullsig);
14201 
14202  free(aggsig);
14203 
14204  return;
14205  }
14206 
14207  /* identify default modify flag for aggkind (must match DefineAggregate) */
14208  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14209  /* replace omitted flags for old versions */
14210  if (aggfinalmodify == '0')
14211  aggfinalmodify = defaultfinalmodify;
14212  if (aggmfinalmodify == '0')
14213  aggmfinalmodify = defaultfinalmodify;
14214 
14215  /* regproc and regtype output is already sufficiently quoted */
14216  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14217  aggtransfn, aggtranstype);
14218 
14219  if (strcmp(aggtransspace, "0") != 0)
14220  {
14221  appendPQExpBuffer(details, ",\n SSPACE = %s",
14222  aggtransspace);
14223  }
14224 
14225  if (!PQgetisnull(res, 0, i_agginitval))
14226  {
14227  appendPQExpBufferStr(details, ",\n INITCOND = ");
14228  appendStringLiteralAH(details, agginitval, fout);
14229  }
14230 
14231  if (strcmp(aggfinalfn, "-") != 0)
14232  {
14233  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14234  aggfinalfn);
14235  if (aggfinalextra)
14236  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14237  if (aggfinalmodify != defaultfinalmodify)
14238  {
14239  switch (aggfinalmodify)
14240  {
14241  case AGGMODIFY_READ_ONLY:
14242  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14243  break;
14244  case AGGMODIFY_SHAREABLE:
14245  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14246  break;
14247  case AGGMODIFY_READ_WRITE:
14248  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14249  break;
14250  default:
14251  fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14252  agginfo->aggfn.dobj.name);
14253  break;
14254  }
14255  }
14256  }
14257 
14258  if (strcmp(aggcombinefn, "-") != 0)
14259  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14260 
14261  if (strcmp(aggserialfn, "-") != 0)
14262  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14263 
14264  if (strcmp(aggdeserialfn, "-") != 0)
14265  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14266 
14267  if (strcmp(aggmtransfn, "-") != 0)
14268  {
14269  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14270  aggmtransfn,
14271  aggminvtransfn,
14272  aggmtranstype);
14273  }
14274 
14275  if (strcmp(aggmtransspace, "0") != 0)
14276  {
14277  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14278  aggmtransspace);
14279  }
14280 
14281  if (!PQgetisnull(res, 0, i_aggminitval))
14282  {
14283  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14284  appendStringLiteralAH(details, aggminitval, fout);
14285  }
14286 
14287  if (strcmp(aggmfinalfn, "-") != 0)
14288  {
14289  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14290  aggmfinalfn);
14291  if (aggmfinalextra)
14292  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14293  if (aggmfinalmodify != defaultfinalmodify)
14294  {
14295  switch (aggmfinalmodify)
14296  {
14297  case AGGMODIFY_READ_ONLY:
14298  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14299  break;
14300  case AGGMODIFY_SHAREABLE:
14301  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14302  break;
14303  case AGGMODIFY_READ_WRITE:
14304  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14305  break;
14306  default:
14307  fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14308  agginfo->aggfn.dobj.name);
14309  break;
14310  }
14311  }
14312  }
14313 
14314  aggsortconvop = getFormattedOperatorName(fout, aggsortop);
14315  if (aggsortconvop)
14316  {
14317  appendPQExpBuffer(details, ",\n SORTOP = %s",
14318  aggsortconvop);
14319  free(aggsortconvop);
14320  }
14321 
14322  if (aggkind == AGGKIND_HYPOTHETICAL)
14323  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14324 
14325  if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
14326  {
14327  if (proparallel[0] == PROPARALLEL_SAFE)
14328  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14329  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14330  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14331  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14332  fatal("unrecognized proparallel value for function \"%s\"",
14333  agginfo->aggfn.dobj.name);
14334  }
14335 
14336  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14337  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14338  aggsig);
14339 
14340  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14341  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14342  aggfullsig ? aggfullsig : aggsig, details->data);
14343 
14344  if (dopt->binary_upgrade)
14346  "AGGREGATE", aggsig,
14347  agginfo->aggfn.dobj.namespace->dobj.name);
14348 
14349  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14350  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14351  agginfo->aggfn.dobj.dumpId,
14352  ARCHIVE_OPTS(.tag = aggsig_tag,
14353  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14354  .owner = agginfo->aggfn.rolname,
14355  .description = "AGGREGATE",
14356  .section = SECTION_PRE_DATA,
14357  .createStmt = q->data,
14358  .dropStmt = delq->data));
14359 
14360  /* Dump Aggregate Comments */
14361  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14362  dumpComment(fout, "AGGREGATE", aggsig,
14363  agginfo->aggfn.dobj.namespace->dobj.name,
14364  agginfo->aggfn.rolname,
14365  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14366 
14367  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14368  dumpSecLabel(fout, "AGGREGATE", aggsig,
14369  agginfo->aggfn.dobj.namespace->dobj.name,
14370  agginfo->aggfn.rolname,
14371  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14372 
14373  /*
14374  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14375  * command look like a function's GRANT; in particular this affects the
14376  * syntax for zero-argument aggregates and ordered-set aggregates.
14377  */
14378  free(aggsig);
14379 
14380  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14381 
14382  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14383  dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
14384  "FUNCTION", aggsig, NULL,
14385  agginfo->aggfn.dobj.namespace->dobj.name,
14386  agginfo->aggfn.rolname, agginfo->aggfn.proacl,
14387  agginfo->aggfn.rproacl,
14388  agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
14389 
14390  free(aggsig);
14391  if (aggfullsig)
14392  free(aggfullsig);
14393  free(aggsig_tag);
14394 
14395  PQclear(res);
14396 
14397  destroyPQExpBuffer(query);
14398  destroyPQExpBuffer(q);
14399  destroyPQExpBuffer(delq);
14400  destroyPQExpBuffer(details);
14401 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrproacl
Definition: pg_dump.h:205
DumpableObject dobj
Definition: pg_dump.h:196
DumpId dumpId
Definition: pg_dump.h:129
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:15181
static char * format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
Definition: pg_dump.c:11671
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, 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:15094
FuncInfo aggfn
Definition: pg_dump.h:211
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
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:139
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
char * initproacl
Definition: pg_dump.h:204
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11766
char * rolname
Definition: pg_dump.h:197
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
#define fatal(...)
char * proacl
Definition: pg_dump.h:202
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4619
char * rproacl
Definition: pg_dump.h:203
static char * getFormattedOperatorName(Archive *fout, const char *oproid)
Definition: pg_dump.c:12891
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
#define pg_log_warning(...)
Definition: pgfnames.c:24
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
static char * format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:13893
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:9684
int remoteVersion
Definition: pg_backup.h:187

◆ dumpAttrDef()

static void dumpAttrDef ( Archive fout,
AttrDefInfo adinfo 
)
static

Definition at line 16447 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().

16448 {
16449  DumpOptions *dopt = fout->dopt;
16450  TableInfo *tbinfo = adinfo->adtable;
16451  int adnum = adinfo->adnum;
16452  PQExpBuffer q;
16453  PQExpBuffer delq;
16454  char *qualrelname;
16455  char *tag;
16456  char *foreign;
16457 
16458  /* Skip if table definition not to be dumped */
16459  if (!tbinfo->dobj.dump || dopt->dataOnly)
16460  return;
16461 
16462  /* Skip if not "separate"; it was dumped in the table's definition */
16463  if (!adinfo->separate)
16464  return;
16465 
16466  q = createPQExpBuffer();
16467  delq = createPQExpBuffer();
16468 
16469  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16470 
16471  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16472 
16474  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16475  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16476  adinfo->adef_expr);
16477 
16478  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16479  foreign, qualrelname,
16480  fmtId(tbinfo->attnames[adnum - 1]));
16481 
16482  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16483 
16484  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16485  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16486  ARCHIVE_OPTS(.tag = tag,
16487  .namespace = tbinfo->dobj.namespace->dobj.name,
16488  .owner = tbinfo->rolname,
16489  .description = "DEFAULT",
16490  .section = SECTION_PRE_DATA,
16491  .createStmt = q->data,
16492  .dropStmt = delq->data));
16493 
16494  free(tag);
16495  destroyPQExpBuffer(q);
16496  destroyPQExpBuffer(delq);
16497  free(qualrelname);
16498 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
char relkind
Definition: pg_dump.h:265
DumpComponents dump
Definition: pg_dump.h:131
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:260
DumpId dumpId
Definition: pg_dump.h:129
char ** attnames
Definition: pg_dump.h:303
DumpableObject dobj
Definition: pg_dump.h:259
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
bool separate
Definition: pg_dump.h:346
DumpOptions * dopt
Definition: pg_backup.h:182
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:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
TableInfo * adtable
Definition: pg_dump.h:343
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
DumpableObject dobj
Definition: pg_dump.h:342
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:140
CatalogId catId
Definition: pg_dump.h:128
char * adef_expr
Definition: pg_dump.h:345

◆ dumpBaseType()

static void dumpBaseType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10710 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, _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().

10711 {
10712  DumpOptions *dopt = fout->dopt;
10714  PQExpBuffer delq = createPQExpBuffer();
10715  PQExpBuffer query = createPQExpBuffer();
10716  PGresult *res;
10717  char *qtypname;
10718  char *qualtypname;
10719  char *typlen;
10720  char *typinput;
10721  char *typoutput;
10722  char *typreceive;
10723  char *typsend;
10724  char *typmodin;
10725  char *typmodout;
10726  char *typanalyze;
10727  Oid typreceiveoid;
10728  Oid typsendoid;
10729  Oid typmodinoid;
10730  Oid typmodoutoid;
10731  Oid typanalyzeoid;
10732  char *typcategory;
10733  char *typispreferred;
10734  char *typdelim;
10735  char *typbyval;
10736  char *typalign;
10737  char *typstorage;
10738  char *typcollatable;
10739  char *typdefault;
10740  bool typdefault_is_literal = false;
10741 
10742  /* Fetch type-specific details */
10743  if (fout->remoteVersion >= 90100)
10744  {
10745  appendPQExpBuffer(query, "SELECT typlen, "
10746  "typinput, typoutput, typreceive, typsend, "
10747  "typmodin, typmodout, typanalyze, "
10748  "typreceive::pg_catalog.oid AS typreceiveoid, "
10749  "typsend::pg_catalog.oid AS typsendoid, "
10750  "typmodin::pg_catalog.oid AS typmodinoid, "
10751  "typmodout::pg_catalog.oid AS typmodoutoid, "
10752  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10753  "typcategory, typispreferred, "
10754  "typdelim, typbyval, typalign, typstorage, "
10755  "(typcollation <> 0) AS typcollatable, "
10756  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10757  "FROM pg_catalog.pg_type "
10758  "WHERE oid = '%u'::pg_catalog.oid",
10759  tyinfo->dobj.catId.oid);
10760  }
10761  else if (fout->remoteVersion >= 80400)
10762  {
10763  appendPQExpBuffer(query, "SELECT typlen, "
10764  "typinput, typoutput, typreceive, typsend, "
10765  "typmodin, typmodout, typanalyze, "
10766  "typreceive::pg_catalog.oid AS typreceiveoid, "
10767  "typsend::pg_catalog.oid AS typsendoid, "
10768  "typmodin::pg_catalog.oid AS typmodinoid, "
10769  "typmodout::pg_catalog.oid AS typmodoutoid, "
10770  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10771  "typcategory, typispreferred, "
10772  "typdelim, typbyval, typalign, typstorage, "
10773  "false AS typcollatable, "
10774  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10775  "FROM pg_catalog.pg_type "
10776  "WHERE oid = '%u'::pg_catalog.oid",
10777  tyinfo->dobj.catId.oid);
10778  }
10779  else if (fout->remoteVersion >= 80300)
10780  {
10781  /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
10782  appendPQExpBuffer(query, "SELECT typlen, "
10783  "typinput, typoutput, typreceive, typsend, "
10784  "typmodin, typmodout, typanalyze, "
10785  "typreceive::pg_catalog.oid AS typreceiveoid, "
10786  "typsend::pg_catalog.oid AS typsendoid, "
10787  "typmodin::pg_catalog.oid AS typmodinoid, "
10788  "typmodout::pg_catalog.oid AS typmodoutoid, "
10789  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10790  "'U' AS typcategory, false AS typispreferred, "
10791  "typdelim, typbyval, typalign, typstorage, "
10792  "false AS typcollatable, "
10793  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10794  "FROM pg_catalog.pg_type "
10795  "WHERE oid = '%u'::pg_catalog.oid",
10796  tyinfo->dobj.catId.oid);
10797  }
10798  else
10799  {
10800  appendPQExpBuffer(query, "SELECT typlen, "
10801  "typinput, typoutput, typreceive, typsend, "
10802  "'-' AS typmodin, '-' AS typmodout, "
10803  "typanalyze, "
10804  "typreceive::pg_catalog.oid AS typreceiveoid, "
10805  "typsend::pg_catalog.oid AS typsendoid, "
10806  "0 AS typmodinoid, 0 AS typmodoutoid, "
10807  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10808  "'U' AS typcategory, false AS typispreferred, "
10809  "typdelim, typbyval, typalign, typstorage, "
10810  "false AS typcollatable, "
10811  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10812  "FROM pg_catalog.pg_type "
10813  "WHERE oid = '%u'::pg_catalog.oid",
10814  tyinfo->dobj.catId.oid);
10815  }
10816 
10817  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10818 
10819  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10820  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10821  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10822  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10823  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10824  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10825  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10826  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10827  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10828  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10829  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10830  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10831  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10832  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10833  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10834  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10835  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10836  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10837  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10838  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10839  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10840  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10841  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10842  {
10843  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10844  typdefault_is_literal = true; /* it needs quotes */
10845  }
10846  else
10847  typdefault = NULL;
10848 
10849  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10850  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10851 
10852  /*
10853  * The reason we include CASCADE is that the circular dependency between
10854  * the type and its I/O functions makes it impossible to drop the type any
10855  * other way.
10856  */
10857  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
10858 
10859  /*
10860  * We might already have a shell type, but setting pg_type_oid is
10861  * harmless, and in any case we'd better set the array type OID.
10862  */
10863  if (dopt->binary_upgrade)
10865  tyinfo->dobj.catId.oid,
10866  false);
10867 
10869  "CREATE TYPE %s (\n"
10870  " INTERNALLENGTH = %s",
10871  qualtypname,
10872  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10873 
10874  /* regproc result is sufficiently quoted already */
10875  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
10876  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
10877  if (OidIsValid(typreceiveoid))
10878  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
10879  if (OidIsValid(typsendoid))
10880  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10881  if (OidIsValid(typmodinoid))
10882  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10883  if (OidIsValid(typmodoutoid))
10884  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10885  if (OidIsValid(typanalyzeoid))
10886  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10887 
10888  if (strcmp(typcollatable, "t") == 0)
10889  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10890 
10891  if (typdefault != NULL)
10892  {
10893  appendPQExpBufferStr(q, ",\n DEFAULT = ");
10894  if (typdefault_is_literal)
10895  appendStringLiteralAH(q, typdefault, fout);
10896  else
10897  appendPQExpBufferStr(q, typdefault);
10898  }
10899 
10900  if (OidIsValid(tyinfo->typelem))
10901  {
10902  char *elemType;
10903 
10904  elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroIsError);
10905  appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
10906  free(elemType);
10907  }
10908 
10909  if (strcmp(typcategory, "U") != 0)
10910  {
10911  appendPQExpBufferStr(q, ",\n CATEGORY = ");
10912  appendStringLiteralAH(q, typcategory, fout);
10913  }
10914 
10915  if (strcmp(typispreferred, "t") == 0)
10916  appendPQExpBufferStr(q, ",\n PREFERRED = true");
10917 
10918  if (typdelim && strcmp(typdelim, ",") != 0)
10919  {
10920  appendPQExpBufferStr(q, ",\n DELIMITER = ");
10921  appendStringLiteralAH(q, typdelim, fout);
10922  }
10923 
10924  if (*typalign == TYPALIGN_CHAR)
10925  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
10926  else if (*typalign == TYPALIGN_SHORT)
10927  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
10928  else if (*typalign == TYPALIGN_INT)
10929  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
10930  else if (*typalign == TYPALIGN_DOUBLE)
10931  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
10932 
10933  if (*typstorage == TYPSTORAGE_PLAIN)
10934  appendPQExpBufferStr(q, ",\n STORAGE = plain");
10935  else if (*typstorage == TYPSTORAGE_EXTERNAL)
10936  appendPQExpBufferStr(q, ",\n STORAGE = external");
10937  else if (*typstorage == TYPSTORAGE_EXTENDED)
10938  appendPQExpBufferStr(q, ",\n STORAGE = extended");
10939  else if (*typstorage == TYPSTORAGE_MAIN)
10940  appendPQExpBufferStr(q, ",\n STORAGE = main");
10941 
10942  if (strcmp(typbyval, "t") == 0)
10943  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
10944 
10945  appendPQExpBufferStr(q, "\n);\n");
10946 
10947  if (dopt->binary_upgrade)
10949  "TYPE", qtypname,
10950  tyinfo->dobj.namespace->dobj.name);
10951 
10952  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10953  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10954  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
10955  .namespace = tyinfo->dobj.namespace->dobj.name,
10956  .owner = tyinfo->rolname,
10957  .description = "TYPE",
10958  .section = SECTION_PRE_DATA,
10959  .createStmt = q->data,
10960  .dropStmt = delq->data));
10961 
10962  /* Dump Type Comments and Security Labels */
10963  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10964  dumpComment(fout, "TYPE", qtypname,
10965  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10966  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10967 
10968  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10969  dumpSecLabel(fout, "TYPE", qtypname,
10970  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10971  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10972 
10973  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10974  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10975  qtypname, NULL,
10976  tyinfo->dobj.namespace->dobj.name,
10977  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10978  tyinfo->inittypacl, tyinfo->initrtypacl);
10979 
10980  PQclear(res);
10981  destroyPQExpBuffer(q);
10982  destroyPQExpBuffer(delq);
10983  destroyPQExpBuffer(query);
10984  free(qtypname);
10985  free(qualtypname);
10986 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18558
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
char * inittypacl
Definition: pg_dump.h:172
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:173
char * typacl
Definition: pg_dump.h:170
DumpId dumpId
Definition: pg_dump.h:129
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:15181
#define OidIsValid(objectId)
Definition: c.h:644
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, 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:15094
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
char typalign
Definition: pg_type.h:170
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
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:174
#define atooid(x)
Definition: postgres_ext.h:42
DumpableObject dobj
Definition: pg_dump.h:163
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type)
Definition: pg_dump.c:4428
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:140
char * rtypacl
Definition: pg_dump.h:171
char * rolname
Definition: pg_dump.h:169
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4619
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
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:9684
int remoteVersion
Definition: pg_backup.h:187

◆ dumpBlob()

static void dumpBlob ( Archive fout,
BlobInfo binfo 
)
static

Definition at line 3444 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, _dumpableObject::name, _blobInfo::rblobacl, _blobInfo::rolname, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

3445 {
3446  PQExpBuffer cquery = createPQExpBuffer();
3447  PQExpBuffer dquery = createPQExpBuffer();
3448 
3449  appendPQExpBuffer(cquery,
3450  "SELECT pg_catalog.lo_create('%s');\n",
3451  binfo->dobj.name);
3452 
3453  appendPQExpBuffer(dquery,
3454  "SELECT pg_catalog.lo_unlink('%s');\n",
3455  binfo->dobj.name);
3456 
3457  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3458  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3459  ARCHIVE_OPTS(.tag = binfo->dobj.name,
3460  .owner = binfo->rolname,
3461  .description = "BLOB",
3462  .section = SECTION_PRE_DATA,
3463  .createStmt = cquery->data,
3464  .dropStmt = dquery->data));
3465 
3466  /* Dump comment if any */
3467  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3468  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3469  NULL, binfo->rolname,
3470  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3471 
3472  /* Dump security label if any */
3473  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3474  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3475  NULL, binfo->rolname,
3476  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3477 
3478  /* Dump ACL if any */
3479  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3480  dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
3481  binfo->dobj.name, NULL,
3482  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3483  binfo->initblobacl, binfo->initrblobacl);
3484 
3485  destroyPQExpBuffer(cquery);
3486  destroyPQExpBuffer(dquery);
3487 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
DumpId dumpId
Definition: pg_dump.h:129
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:15181
char * initblobacl
Definition: pg_dump.h:571
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, 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:15094
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject dobj
Definition: pg_dump.h:567
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:568
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
char * blobacl
Definition: pg_dump.h:569
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
char * rblobacl
Definition: pg_dump.h:570
char * initrblobacl
Definition: pg_dump.h:572
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:9684

◆ dumpBlobs()

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

Definition at line 3494 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().

3495 {
3496  const char *blobQry;
3497  const char *blobFetchQry;
3498  PGconn *conn = GetConnection(fout);
3499  PGresult *res;
3500  char buf[LOBBUFSIZE];
3501  int ntups;
3502  int i;
3503  int cnt;
3504 
3505  pg_log_info("saving large objects");
3506 
3507  /*
3508  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3509  * the already-in-memory dumpable objects instead...
3510  */
3511  if (fout->remoteVersion >= 90000)
3512  blobQry =
3513  "DECLARE bloboid CURSOR FOR "
3514  "SELECT oid FROM pg_largeobject_metadata ORDER BY 1";
3515  else
3516  blobQry =
3517  "DECLARE bloboid CURSOR FOR "
3518  "SELECT DISTINCT loid FROM pg_largeobject ORDER BY 1";
3519 
3520  ExecuteSqlStatement(fout, blobQry);
3521 
3522  /* Command to fetch from cursor */
3523  blobFetchQry = "FETCH 1000 IN bloboid";
3524 
3525  do
3526  {
3527  /* Do a fetch */
3528  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3529 
3530  /* Process the tuples, if any */
3531  ntups = PQntuples(res);
3532  for (i = 0; i < ntups; i++)
3533  {
3534  Oid blobOid;
3535  int loFd;
3536 
3537  blobOid = atooid(PQgetvalue(res, i, 0));
3538  /* Open the BLOB */
3539  loFd = lo_open(conn, blobOid, INV_READ);
3540  if (loFd == -1)
3541  fatal("could not open large object %u: %s",
3542  blobOid, PQerrorMessage(conn));
3543 
3544  StartBlob(fout, blobOid);
3545 
3546  /* Now read it in chunks, sending data to archive */
3547  do
3548  {
3549  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3550  if (cnt < 0)
3551  fatal("error reading large object %u: %s",
3552  blobOid, PQerrorMessage(conn));
3553 
3554  WriteData(fout, buf, cnt);
3555  } while (cnt > 0);
3556 
3557  lo_close(conn, loFd);
3558 
3559  EndBlob(fout, blobOid);
3560  }
3561 
3562  PQclear(res);
3563  } while (ntups > 0);
3564 
3565  return 1;
3566 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6687
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define INV_READ
Definition: libpq-fs.h:22
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:99
int StartBlob(Archive *AH, Oid oid)
PGconn * conn
Definition: streamutil.c:54
static char * buf
Definition: pg_test_fsync.c:67
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:694
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:107
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:389
#define fatal(...)
int EndBlob(Archive *AH, Oid oid)
int i
#define LOBBUFSIZE
#define pg_log_info(...)
Definition: logging.h:87
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401
int remoteVersion
Definition: pg_backup.h:187

◆ dumpCast()

static void dumpCast ( Archive fout,
CastInfo cast 
)
static

Definition at line 12393 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().

12394 {
12395  DumpOptions *dopt = fout->dopt;
12396  PQExpBuffer defqry;
12397  PQExpBuffer delqry;
12398  PQExpBuffer labelq;
12399  PQExpBuffer castargs;
12400  FuncInfo *funcInfo = NULL;
12401  char *sourceType;
12402  char *targetType;
12403 
12404  /* Skip if not to be dumped */
12405  if (!cast->dobj.dump || dopt->dataOnly)
12406  return;
12407 
12408  /* Cannot dump if we don't have the cast function's info */
12409  if (OidIsValid(cast->castfunc))
12410  {
12411  funcInfo = findFuncByOid(cast->castfunc);
12412  if (funcInfo == NULL)
12413  fatal("could not find function definition for function with OID %u",
12414  cast->castfunc);
12415  }
12416 
12417  defqry = createPQExpBuffer();
12418  delqry = createPQExpBuffer();
12419  labelq = createPQExpBuffer();
12420  castargs = createPQExpBuffer();
12421 
12422  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12423  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12424  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12425  sourceType, targetType);
12426 
12427  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12428  sourceType, targetType);
12429 
12430  switch (cast->castmethod)
12431  {
12432  case COERCION_METHOD_BINARY:
12433  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12434  break;
12435  case COERCION_METHOD_INOUT:
12436  appendPQExpBufferStr(defqry, "WITH INOUT");
12437  break;
12438  case COERCION_METHOD_FUNCTION:
12439  if (funcInfo)
12440  {
12441  char *fsig = format_function_signature(fout, funcInfo, true);
12442 
12443  /*
12444  * Always qualify the function name (format_function_signature
12445  * won't qualify it).
12446  */
12447  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12448  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12449  free(fsig);
12450  }
12451  else
12452  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12453  break;
12454  default:
12455  pg_log_warning("bogus value in pg_cast.castmethod field");
12456  }
12457 
12458  if (cast->castcontext == 'a')
12459  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12460  else if (cast->castcontext == 'i')
12461  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12462  appendPQExpBufferStr(defqry, ";\n");
12463 
12464  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12465  sourceType, targetType);
12466 
12467  appendPQExpBuffer(castargs, "(%s AS %s)",
12468  sourceType, targetType);
12469 
12470  if (dopt->binary_upgrade)
12471  binary_upgrade_extension_member(defqry, &cast->dobj,
12472  "CAST", castargs->data, NULL);
12473 
12474  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12475  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12476  ARCHIVE_OPTS(.tag = labelq->data,
12477  .description = "CAST",
12478  .section = SECTION_PRE_DATA,
12479  .createStmt = defqry->data,
12480  .dropStmt = delqry->data));
12481 
12482  /* Dump Cast Comments */
12483  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12484  dumpComment(fout, "CAST", castargs->data,
12485  NULL, "",
12486  cast->dobj.catId, 0, cast->dobj.dumpId);
12487 
12488  free(sourceType);
12489  free(targetType);
12490 
12491  destroyPQExpBuffer(defqry);
12492  destroyPQExpBuffer(delqry);
12493  destroyPQExpBuffer(labelq);
12494  destroyPQExpBuffer(castargs);
12495 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18558
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
Oid castsource
Definition: pg_dump.h:472
DumpComponents dump
Definition: pg_dump.h:131
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:196
DumpId dumpId
Definition: pg_dump.h:129
Oid castfunc
Definition: pg_dump.h:474
#define OidIsValid(objectId)
Definition: c.h:644
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpableObject dobj
Definition: pg_dump.h:471
Oid casttarget
Definition: pg_dump.h:473
DumpOptions * dopt
Definition: pg_backup.h:182
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:139
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:851
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11766
#define fatal(...)
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4619
CatalogId catId
Definition: pg_dump.h:128
char castmethod
Definition: pg_dump.h:476
#define pg_log_warning(...)
Definition: pgfnames.c:24
char castcontext
Definition: pg_dump.h:475
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:9684

◆ dumpCollation()

static void dumpCollation ( Archive fout,
CollInfo collinfo 
)
static

Definition at line 13646 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(), PQgetisnull(), PQgetvalue(), Archive::remoteVersion, _collInfo::rolname, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

13647 {
13648  DumpOptions *dopt = fout->dopt;
13649  PQExpBuffer query;
13650  PQExpBuffer q;
13651  PQExpBuffer delq;
13652  char *qcollname;
13653  PGresult *res;
13654  int i_collprovider;
13655  int i_collisdeterministic;
13656  int i_collcollate;
13657  int i_collctype;
13658  const char *collprovider;
13659  const char *collcollate;
13660  const char *collctype;
13661 
13662  /* Skip if not to be dumped */
13663  if (!collinfo->dobj.dump || dopt->dataOnly)
13664  return;
13665 
13666  query = createPQExpBuffer();
13667  q = createPQExpBuffer();
13668  delq = createPQExpBuffer();
13669 
13670  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13671 
13672  /* Get collation-specific details */
13673  appendPQExpBufferStr(query, "SELECT ");
13674 
13675  if (fout->remoteVersion >= 100000)
13676  appendPQExpBufferStr(query,
13677  "collprovider, "
13678  "collversion, ");
13679  else
13680  appendPQExpBufferStr(query,
13681  "'c' AS collprovider, "
13682  "NULL AS collversion, ");
13683 
13684  if (fout->remoteVersion >= 120000)
13685  appendPQExpBufferStr(query,
13686  "collisdeterministic, ");
13687  else
13688  appendPQExpBufferStr(query,
13689  "true AS collisdeterministic, ");
13690 
13691  appendPQExpBuffer(query,
13692  "collcollate, "
13693  "collctype "
13694  "FROM pg_catalog.pg_collation c "
13695  "WHERE c.oid = '%u'::pg_catalog.oid",
13696  collinfo->dobj.catId.oid);
13697 
13698  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13699 
13700  i_collprovider = PQfnumber(res, "collprovider");
13701  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13702  i_collcollate = PQfnumber(res, "collcollate");
13703  i_collctype = PQfnumber(res, "collctype");
13704 
13705  collprovider = PQgetvalue(res, 0, i_collprovider);
13706  collcollate = PQgetvalue(res, 0, i_collcollate);
13707  collctype = PQgetvalue(res, 0, i_collctype);
13708 
13709  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13710  fmtQualifiedDumpable(collinfo));
13711 
13712  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13713  fmtQualifiedDumpable(collinfo));
13714 
13715  appendPQExpBufferStr(q, "provider = ");
13716  if (collprovider[0] == 'c')
13717  appendPQExpBufferStr(q, "libc");
13718  else if (collprovider[0] == 'i')
13719  appendPQExpBufferStr(q, "icu");
13720  else if (collprovider[0] == 'd')
13721  /* to allow dumping pg_catalog; not accepted on input */
13722  appendPQExpBufferStr(q, "default");
13723  else
13724  fatal("unrecognized collation provider: %s",
13725  collprovider);
13726 
13727  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13728  appendPQExpBufferStr(q, ", deterministic = false");
13729 
13730  if (strcmp(collcollate, collctype) == 0)
13731  {
13732  appendPQExpBufferStr(q, ", locale = ");
13733  appendStringLiteralAH(q, collcollate, fout);
13734  }
13735  else
13736  {
13737  appendPQExpBufferStr(q, ", lc_collate = ");
13738  appendStringLiteralAH(q, collcollate, fout);
13739  appendPQExpBufferStr(q, ", lc_ctype = ");
13740  appendStringLiteralAH(q, collctype, fout);
13741  }
13742 
13743  /*
13744  * For binary upgrade, carry over the collation version. For normal
13745  * dump/restore, omit the version, so that it is computed upon restore.
13746  */
13747  if (dopt->binary_upgrade)
13748  {
13749  int i_collversion;
13750 
13751  i_collversion = PQfnumber(res, "collversion");
13752  if (!PQgetisnull(res, 0, i_collversion))
13753  {
13754  appendPQExpBufferStr(q, ", version = ");
13756  PQgetvalue(res, 0, i_collversion),
13757  fout);
13758  }
13759  }
13760 
13761  appendPQExpBufferStr(q, ");\n");
13762 
13763  if (dopt->binary_upgrade)
13764  binary_upgrade_extension_member(q, &collinfo->dobj,
13765  "COLLATION", qcollname,
13766  collinfo->dobj.namespace->dobj.name);
13767 
13768  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13769  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13770  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13771  .namespace = collinfo->dobj.namespace->dobj.name,
13772  .owner = collinfo->rolname,
13773  .description = "COLLATION",
13774  .section = SECTION_PRE_DATA,
13775  .createStmt = q->data,
13776  .dropStmt = delq->data));
13777 
13778  /* Dump Collation Comments */
13779  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13780  dumpComment(fout, "COLLATION", qcollname,
13781  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13782  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13783 
13784  PQclear(res);
13785 
13786  destroyPQExpBuffer(query);
13787  destroyPQExpBuffer(q);
13788  destroyPQExpBuffer(delq);
13789  free(qcollname);
13790 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
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:129
DumpableObject dobj
Definition: pg_dump.h:244
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
char * rolname
Definition: pg_dump.h:245
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
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:139
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:2877
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:140
#define fatal(...)
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4619
CatalogId catId
Definition: pg_dump.h:128
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
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:9684
int remoteVersion
Definition: pg_backup.h:187

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

9687 {
9688  DumpOptions *dopt = fout->dopt;
9689  CommentItem *comments;
9690  int ncomments;
9691 
9692  /* do nothing, if --no-comments is supplied */
9693  if (dopt->no_comments)
9694  return;
9695 
9696  /* Comments are schema not data ... except blob comments are data */
9697  if (strcmp(type, "LARGE OBJECT") != 0)
9698  {
9699  if (dopt->dataOnly)
9700  return;
9701  }
9702  else
9703  {
9704  /* We do dump blob comments in binary-upgrade mode */
9705  if (dopt->schemaOnly && !dopt->binary_upgrade)
9706  return;
9707  }
9708 
9709  /* Search for comments associated with catalogId, using table */
9710  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9711  &comments);
9712 
9713  /* Is there one matching the subid? */
9714  while (ncomments > 0)
9715  {
9716  if (comments->objsubid == subid)
9717  break;
9718  comments++;
9719  ncomments--;
9720  }
9721 
9722  /* If a comment exists, build COMMENT ON statement */
9723  if (ncomments > 0)
9724  {
9725  PQExpBuffer query = createPQExpBuffer();
9727 
9728  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9729  if (namespace && *namespace)
9730  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9731  appendPQExpBuffer(query, "%s IS ", name);
9732  appendStringLiteralAH(query, comments->descr, fout);
9733  appendPQExpBufferStr(query, ";\n");
9734 
9735  appendPQExpBuffer(tag, "%s %s", type, name);
9736 
9737  /*
9738  * We mark comments as SECTION_NONE because they really belong in the
9739  * same section as their parent, whether that is pre-data or
9740  * post-data.
9741  */
9743  ARCHIVE_OPTS(.tag = tag->data,
9744  .namespace = namespace,
9745  .owner = owner,
9746  .description = "COMMENT",
9747  .section = SECTION_NONE,
9748  .createStmt = query->data,
9749  .deps = &dumpId,
9750  .nDeps = 1));
9751 
9752  destroyPQExpBuffer(query);
9753  destroyPQExpBuffer(tag);
9754  }
9755 }
#define ARCHIVE_OPTS(...)
Oid tableoid
Definition: pg_backup.h:230
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
bool schemaOnly
Definition: pg_backup.h:138
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:71
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9863
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
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:139
const char * descr
Definition: pg_dump.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
int no_comments
Definition: pg_backup.h:149
const char * name
Definition: encode.c:555
static const CatalogId nilCatalogId
Definition: pg_dump.c:125
DumpId createDumpId(void)
Definition: common.c:590

◆ dumpCompositeType()

static void dumpCompositeType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 11167 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), atooid, attalign, attcollation, 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, _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().

11168 {
11169  DumpOptions *dopt = fout->dopt;
11171  PQExpBuffer dropped = createPQExpBuffer();
11172  PQExpBuffer delq = createPQExpBuffer();
11173  PQExpBuffer query = createPQExpBuffer();
11174  PGresult *res;
11175  char *qtypname;
11176  char *qualtypname;
11177  int ntups;
11178  int i_attname;
11179  int i_atttypdefn;
11180  int i_attlen;
11181  int i_attalign;
11182  int i_attisdropped;
11183  int i_attcollation;
11184  int i;
11185  int actual_atts;
11186 
11187  /* Fetch type specific details */
11188  if (fout->remoteVersion >= 90100)
11189  {
11190  /*
11191  * attcollation is new in 9.1. Since we only want to dump COLLATE
11192  * clauses for attributes whose collation is different from their
11193  * type's default, we use a CASE here to suppress uninteresting
11194  * attcollations cheaply. atttypid will be 0 for dropped columns;
11195  * collation does not matter for those.
11196  */
11197  appendPQExpBuffer(query, "SELECT a.attname, "
11198  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11199  "a.attlen, a.attalign, a.attisdropped, "
11200  "CASE WHEN a.attcollation <> at.typcollation "
11201  "THEN a.attcollation ELSE 0 END AS attcollation "
11202  "FROM pg_catalog.pg_type ct "
11203  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11204  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11205  "WHERE ct.oid = '%u'::pg_catalog.oid "
11206  "ORDER BY a.attnum ",
11207  tyinfo->dobj.catId.oid);
11208  }
11209  else
11210  {
11211  /*
11212  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
11213  * should always be false.
11214  */
11215  appendPQExpBuffer(query, "SELECT a.attname, "
11216  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11217  "a.attlen, a.attalign, a.attisdropped, "
11218  "0 AS attcollation "
11219  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
11220  "WHERE ct.oid = '%u'::pg_catalog.oid "
11221  "AND a.attrelid = ct.typrelid "
11222  "ORDER BY a.attnum ",
11223  tyinfo->dobj.catId.oid);
11224  }
11225 
11226  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11227 
11228  ntups = PQntuples(res);
11229 
11230  i_attname = PQfnumber(res, "attname");
11231  i_atttypdefn = PQfnumber(res, "atttypdefn");
11232  i_attlen = PQfnumber(res, "attlen");
11233  i_attalign = PQfnumber(res, "attalign");
11234  i_attisdropped = PQfnumber(res, "attisdropped");
11235  i_attcollation = PQfnumber(res, "attcollation");
11236 
11237  if (dopt->binary_upgrade)
11238  {
11240  tyinfo->dobj.catId.oid,
11241  false);
11242  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11243  }
11244 
11245  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11246  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11247 
11248  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11249  qualtypname);
11250 
11251  actual_atts = 0;
11252  for (i = 0; i < ntups; i++)
11253  {
11254  char *attname;
11255  char *atttypdefn;
11256  char *attlen;
11257  char *attalign;
11258  bool attisdropped;
11259  Oid attcollation;
11260 
11261  attname = PQgetvalue(res, i, i_attname);
11262  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11263  attlen = PQgetvalue(res, i, i_attlen);
11264  attalign = PQgetvalue(res, i, i_attalign);
11265  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11266  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11267 
11268  if (attisdropped && !dopt->binary_upgrade)
11269  continue;
11270 
11271  /* Format properly if not first attr */
11272  if (actual_atts++ > 0)
11273  appendPQExpBufferChar(q, ',');
11274  appendPQExpBufferStr(q, "\n\t");
11275 
11276  if (!attisdropped)
11277  {
11278  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11279 
11280  /* Add collation if not default for the column type */
11281  if (OidIsValid(attcollation))
11282  {
11283  CollInfo *coll;
11284 
11285  coll = findCollationByOid(attcollation);
11286  if (coll)
11287  appendPQExpBuffer(q, " COLLATE %s",
11288  fmtQualifiedDumpable(coll));
11289  }
11290  }
11291  else
11292  {
11293  /*
11294  * This is a dropped attribute and we're in binary_upgrade mode.
11295  * Insert a placeholder for it in the CREATE TYPE command, and set
11296  * length and alignment with direct UPDATE to the catalogs
11297  * afterwards. See similar code in dumpTableSchema().
11298  */
11299  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11300 
11301  /* stash separately for insertion after the CREATE TYPE */
11302  appendPQExpBufferStr(dropped,
11303  "\n-- For binary upgrade, recreate dropped column.\n");
11304  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11305  "SET attlen = %s, "
11306  "attalign = '%s', attbyval = false\n"
11307  "WHERE attname = ", attlen, attalign);
11308  appendStringLiteralAH(dropped, attname, fout);
11309  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11310  appendStringLiteralAH(dropped, qualtypname, fout);
11311  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11312 
11313  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11314  qualtypname);
11315  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11316  fmtId(attname));
11317  }
11318  }
11319  appendPQExpBufferStr(q, "\n);\n");
11320  appendPQExpBufferStr(q, dropped->data);
11321 
11322  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11323 
11324  if (dopt->binary_upgrade)
11326  "TYPE", qtypname,
11327  tyinfo->dobj.namespace->dobj.name);
11328 
11329  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11330  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11331  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11332  .namespace = tyinfo->dobj.namespace->dobj.name,
11333  .owner = tyinfo->rolname,
11334  .description = "TYPE",
11335  .section = SECTION_PRE_DATA,
11336  .createStmt = q->data,
11337  .dropStmt = delq->data));
11338 
11339 
11340  /* Dump Type Comments and Security Labels */
11341  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11342  dumpComment(fout, "TYPE", qtypname,
11343  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11344  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11345 
11346  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11347  dumpSecLabel(fout, "TYPE", qtypname,
11348  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11349  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11350 
11351  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11352  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
11353  qtypname, NULL,
11354  tyinfo->dobj.namespace->dobj.name,
11355  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11356  tyinfo->inittypacl, tyinfo->initrtypacl);
11357 
11358  PQclear(res);
11359  destroyPQExpBuffer(q);
11360  destroyPQExpBuffer(dropped);
11361  destroyPQExpBuffer(delq);
11362  destroyPQExpBuffer(query);
11363  free(qtypname);
11364  free(qualtypname);
11365 
11366  /* Dump any per-column comments */
11367  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11368  dumpCompositeTypeColComments(fout, tyinfo);
11369 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
char * inittypacl
Definition: pg_dump.h:172
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
Oid typrelid
Definition: pg_dump.h:175
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrtypacl
Definition: pg_dump.h:173
static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
Definition: pg_dump.c:11377
char * typacl
Definition: pg_dump.h:170
DumpId dumpId
Definition: pg_dump.h:129
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
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:15181
#define OidIsValid(objectId)
Definition: c.h:644
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, 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:15094
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
Oid attcollation
Definition: pg_attribute.h:157
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
char attalign
Definition: pg_attribute.h:122
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4549
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define atooid(x)
Definition: postgres_ext.h:42
int16 attlen
Definition: pg_attribute.h:64
DumpableObject dobj
Definition: pg_dump.h:163
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type)
Definition: pg_dump.c:4428
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:140
char * rtypacl
Definition: pg_dump.h:171
char * rolname
Definition: pg_dump.h:169
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:873
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4619
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
int i
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:9684
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401
int remoteVersion
Definition: pg_backup.h:187

◆ dumpCompositeTypeColComments()

static void dumpCompositeTypeColComments ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 11377 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().

11378 {
11379  CommentItem *comments;
11380  int ncomments;
11381  PGresult *res;
11382  PQExpBuffer query;
11383  PQExpBuffer target;
11384  Oid pgClassOid;
11385  int i;
11386  int ntups;
11387  int i_attname;
11388  int i_attnum;
11389 
11390  /* do nothing, if --no-comments is supplied */
11391  if (fout->dopt->no_comments)
11392  return;
11393 
11394  query = createPQExpBuffer();
11395 
11396  appendPQExpBuffer(query,
11397  "SELECT c.tableoid, a.attname, a.attnum "
11398  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
11399  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
11400  " AND NOT a.attisdropped "
11401  "ORDER BY a.attnum ",
11402  tyinfo->typrelid);
11403 
11404  /* Fetch column attnames */
11405  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11406 
11407  ntups = PQntuples(res);
11408  if (ntups < 1)
11409  {
11410  PQclear(res);
11411  destroyPQExpBuffer(query);
11412  return;
11413  }
11414 
11415  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
11416 
11417  /* Search for comments associated with type's pg_class OID */
11418  ncomments = findComments(fout,
11419  pgClassOid,
11420  tyinfo->typrelid,
11421  &comments);
11422 
11423  /* If no comments exist, we're done */
11424  if (ncomments <= 0)
11425  {
11426  PQclear(res);
11427  destroyPQExpBuffer(query);
11428  return;
11429  }
11430 
11431  /* Build COMMENT ON statements */
11432  target = createPQExpBuffer();
11433 
11434  i_attnum = PQfnumber(res, "attnum");
11435  i_attname = PQfnumber(res, "attname");
11436  while (ncomments > 0)
11437  {
11438  const char *attname;
11439 
11440  attname = NULL;
11441  for (i = 0; i < ntups; i++)
11442  {
11443  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
11444  {
11445  attname = PQgetvalue(res, i, i_attname);
11446  break;
11447  }
11448  }
11449  if (attname) /* just in case we don't find it */
11450  {
11451  const char *descr = comments->descr;
11452 
11453  resetPQExpBuffer(target);
11454  appendPQExpBuffer(target, "COLUMN %s.",
11455  fmtId(tyinfo->dobj.name));
11456  appendPQExpBufferStr(target, fmtId(attname));
11457 
11458  resetPQExpBuffer(query);
11459  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11460  fmtQualifiedDumpable(tyinfo));
11461  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11462  appendStringLiteralAH(query, descr, fout);
11463  appendPQExpBufferStr(query, ";\n");
11464 
11466  ARCHIVE_OPTS(.tag = target->data,
11467  .namespace = tyinfo->dobj.namespace->dobj.name,
11468  .owner = tyinfo->rolname,
11469  .description = "COMMENT",
11470  .section = SECTION_NONE,
11471  .createStmt = query->data,
11472  .deps = &(tyinfo->dobj.dumpId),
11473  .nDeps = 1));
11474  }
11475 
11476  comments++;
11477  ncomments--;
11478  }
11479 
11480  PQclear(res);
11481  destroyPQExpBuffer(query);
11482  destroyPQExpBuffer(target);
11483 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
Oid typrelid
Definition: pg_dump.h:175
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:71
DumpId dumpId
Definition: pg_dump.h:129
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9863
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
DumpOptions * dopt
Definition: pg_backup.h:182
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:68
DumpableObject dobj
Definition: pg_dump.h:163
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:140
char * rolname
Definition: pg_dump.h:169
int no_comments
Definition: pg_backup.h:149
int i
static const CatalogId nilCatalogId
Definition: pg_dump.c:125
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401
DumpId createDumpId(void)
Definition: common.c:590

◆ dumpConstraint()

static void dumpConstraint ( Archive fout,
ConstraintInfo coninfo 
)
static

Definition at line 16782 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().

16783 {
16784  DumpOptions *dopt = fout->dopt;
16785  TableInfo *tbinfo = coninfo->contable;
16786  PQExpBuffer q;
16787  PQExpBuffer delq;
16788  char *tag = NULL;
16789  char *foreign;
16790 
16791  /* Skip if not to be dumped */
16792  if (!coninfo->dobj.dump || dopt->dataOnly)
16793  return;
16794 
16795  q = createPQExpBuffer();
16796  delq = createPQExpBuffer();
16797 
16798  foreign = tbinfo &&
16799  tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16800 
16801  if (coninfo->contype == 'p' ||
16802  coninfo->contype == 'u' ||
16803  coninfo->contype == 'x')
16804  {
16805  /* Index-related constraint */
16806  IndxInfo *indxinfo;
16807  int k;
16808 
16809  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16810 
16811  if (indxinfo == NULL)
16812  fatal("missing index for constraint \"%s\"",
16813  coninfo->dobj.name);
16814 
16815  if (dopt->binary_upgrade)
16817  indxinfo->dobj.catId.oid, true);
16818 
16819  appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign,
16820  fmtQualifiedDumpable(tbinfo));
16821  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
16822  fmtId(coninfo->dobj.name));
16823 
16824  if (coninfo->condef)
16825  {
16826  /* pg_get_constraintdef should have provided everything */
16827  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16828  }
16829  else
16830  {
16831  appendPQExpBuffer(q, "%s (",
16832  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16833  for (k = 0; k < indxinfo->indnkeyattrs; k++)
16834  {
16835  int indkey = (int) indxinfo->indkeys[k];
16836  const char *attname;
16837 
16838  if (indkey == InvalidAttrNumber)
16839  break;
16840  attname = getAttrName(indkey, tbinfo);
16841 
16842  appendPQExpBuffer(q, "%s%s",
16843  (k == 0) ? "" : ", ",
16844  fmtId(attname));
16845  }
16846 
16847  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
16848  appendPQExpBufferStr(q, ") INCLUDE (");
16849 
16850  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
16851  {
16852  int indkey = (int) indxinfo->indkeys[k];
16853  const char *attname;
16854 
16855  if (indkey == InvalidAttrNumber)
16856  break;
16857  attname = getAttrName(indkey, tbinfo);
16858 
16859  appendPQExpBuffer(q, "%s%s",
16860  (k == indxinfo->indnkeyattrs) ? "" : ", ",
16861  fmtId(attname));
16862  }
16863 
16864  appendPQExpBufferChar(q, ')');
16865 
16866  if (nonemptyReloptions(indxinfo->indreloptions))
16867  {
16868  appendPQExpBufferStr(q, " WITH (");
16869  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16870  appendPQExpBufferChar(q, ')');
16871  }
16872 
16873  if (coninfo->condeferrable)
16874  {
16875  appendPQExpBufferStr(q, " DEFERRABLE");
16876  if (coninfo->condeferred)
16877  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16878  }
16879 
16880  appendPQExpBufferStr(q, ";\n");
16881  }
16882 
16883  /*
16884  * Append ALTER TABLE commands as needed to set properties that we
16885  * only have ALTER TABLE syntax for. Keep this in sync with the
16886  * similar code in dumpIndex!
16887  */
16888 
16889  /* If the index is clustered, we need to record that. */
16890  if (indxinfo->indisclustered)
16891  {
16892  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16893  fmtQualifiedDumpable(tbinfo));
16894  /* index name is not qualified in this syntax */
16895  appendPQExpBuffer(q, " ON %s;\n",
16896  fmtId(indxinfo->dobj.name));
16897  }
16898 
16899  /* If the index defines identity, we need to record that. */
16900  if (indxinfo->indisreplident)
16901  {
16902  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
16903  fmtQualifiedDumpable(tbinfo));
16904  /* index name is not qualified in this syntax */
16905  appendPQExpBuffer(q, " INDEX %s;\n",
16906  fmtId(indxinfo->dobj.name));
16907  }
16908 
16909  /* Indexes can depend on extensions */
16910  append_depends_on_extension(fout, q, &indxinfo->dobj,
16911  "pg_catalog.pg_class", "INDEX",
16912  fmtQualifiedDumpable(indxinfo));
16913 
16914  appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign,
16915  fmtQualifiedDumpable(tbinfo));
16916  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16917  fmtId(coninfo->dobj.name));
16918 
16919  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16920 
16921  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16922  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16923  ARCHIVE_OPTS(.tag = tag,
16924  .namespace = tbinfo->dobj.namespace->dobj.name,
16925  .tablespace = indxinfo->tablespace,
16926  .owner = tbinfo->rolname,
16927  .description = "CONSTRAINT",
16928  .section = SECTION_POST_DATA,
16929  .createStmt = q->data,
16930  .dropStmt = delq->data));
16931  }
16932  else if (coninfo->contype == 'f')
16933  {
16934  char *only;
16935 
16936  /*
16937  * Foreign keys on partitioned tables are always declared as
16938  * inheriting to partitions; for all other cases, emit them as
16939  * applying ONLY directly to the named table, because that's how they
16940  * work for regular inherited tables.
16941  */
16942  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
16943 
16944  /*
16945  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
16946  * current table data is not processed
16947  */
16948  appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign,
16949  only, fmtQualifiedDumpable(tbinfo));
16950  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16951  fmtId(coninfo->dobj.name),
16952  coninfo->condef);
16953 
16954  appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign,
16955  only, fmtQualifiedDumpable(tbinfo));
16956  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16957  fmtId(coninfo->dobj.name));
16958 
16959  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16960 
16961  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16962  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16963  ARCHIVE_OPTS(.tag = tag,
16964  .namespace = tbinfo->dobj.namespace->dobj.name,
16965  .owner = tbinfo->rolname,
16966  .description = "FK CONSTRAINT",
16967  .section = SECTION_POST_DATA,
16968  .createStmt = q->data,
16969  .dropStmt = delq->data));
16970  }
16971  else if (coninfo->contype == 'c' && tbinfo)
16972  {
16973  /* CHECK constraint on a table */
16974 
16975  /* Ignore if not to be dumped separately, or if it was inherited */
16976  if (coninfo->separate && coninfo->conislocal)
16977  {
16978  /* not ONLY since we want it to propagate to children */
16979  appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign,
16980  fmtQualifiedDumpable(tbinfo));
16981  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16982  fmtId(coninfo->dobj.name),
16983  coninfo->condef);
16984 
16985  appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign,
16986  fmtQualifiedDumpable(tbinfo));
16987  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16988  fmtId(coninfo->dobj.name));
16989 
16990  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16991 
16992  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16993  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16994  ARCHIVE_OPTS(.tag = tag,
16995  .namespace = tbinfo->dobj.namespace->dobj.name,
16996  .owner = tbinfo->rolname,
16997  .description = "CHECK CONSTRAINT",
16998  .section = SECTION_POST_DATA,
16999  .createStmt = q->data,
17000  .dropStmt = delq->data));
17001  }
17002  }
17003  else if (coninfo->contype == 'c' && tbinfo == NULL)
17004  {
17005  /* CHECK constraint on a domain */
17006  TypeInfo *tyinfo = coninfo->condomain;
17007 
17008  /* Ignore if not to be dumped separately */
17009  if (coninfo->separate)
17010  {
17011  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
17012  fmtQualifiedDumpable(tyinfo));
17013  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
17014  fmtId(coninfo->dobj.name),
17015  coninfo->condef);
17016 
17017  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
17018  fmtQualifiedDumpable(tyinfo));
17019  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
17020  fmtId(coninfo->dobj.name));
17021 
17022  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
17023 
17024  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17025  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
17026  ARCHIVE_OPTS(.tag = tag,
17027  .namespace = tyinfo->dobj.namespace->dobj.name,
17028  .owner = tyinfo->rolname,
17029  .description = "CHECK CONSTRAINT",
17030  .section = SECTION_POST_DATA,
17031  .createStmt = q->data,
17032  .dropStmt = delq->data));
17033  }
17034  }
17035  else
17036  {
17037  fatal("unrecognized constraint type: %c",
17038  coninfo->contype);
17039  }
17040 
17041  /* Dump Constraint Comments --- only works for table constraints */
17042  if (tbinfo && coninfo->separate &&
17043  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17044  dumpTableConstraintComment(fout, coninfo);
17045 
17046  free(tag);
17047  destroyPQExpBuffer(q);
17048  destroyPQExpBuffer(delq);
17049 }
char * tablespace
Definition: pg_dump.h:361
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
char relkind
Definition: pg_dump.h:265
DumpComponents dump
Definition: pg_dump.h:131
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
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:18640
char * rolname
Definition: pg_dump.h:260
DumpId dumpId
Definition: pg_dump.h:129
DumpId conindex
Definition: pg_dump.h:448
DumpableObject dobj
Definition: pg_dump.h:259
char * condef
Definition: pg_dump.h:446
static void append_depends_on_extension(Archive *fout, PQExpBuffer create, DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
Definition: pg_dump.c:4382
bool indisreplident
Definition: pg_dump.h:370
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
int binary_upgrade
Definition: pg_backup.h:135
DumpableObject dobj
Definition: pg_dump.h:442
DumpOptions * dopt
Definition: pg_backup.h:182
static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
Definition: pg_dump.c:17059
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:610
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:139
Oid * indkeys
Definition: pg_dump.h:367
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4549
static const char * getAttrName(int attrnum, TableInfo *tblInfo)
Definition: pg_dump.c:16508
bool condeferrable
Definition: pg_dump.h:449
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18628
char * indreloptions
Definition: pg_dump.h:362
DumpableObject dobj
Definition: pg_dump.h:163
TypeInfo * condomain
Definition: pg_dump.h:444
bool conislocal
Definition: pg_dump.h:451
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
TableInfo * contable
Definition: pg_dump.h:443
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
bool condeferred
Definition: pg_dump.h:450
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:140
char * rolname
Definition: pg_dump.h:169
#define fatal(...)
#define InvalidAttrNumber
Definition: attnum.h:23
int indnkeyattrs
Definition: pg_dump.h:365
int indnattrs
Definition: pg_dump.h:366
CatalogId catId
Definition: pg_dump.h:128
DumpableObject dobj
Definition: pg_dump.h:358
bool indisclustered
Definition: pg_dump.h:369

◆ dumpConversion()

static void dumpConversion ( Archive fout,
ConvInfo convinfo 
)
static

Definition at line 13797 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().

13798 {
13799  DumpOptions *dopt = fout->dopt;
13800  PQExpBuffer query;
13801  PQExpBuffer q;
13802  PQExpBuffer delq;
13803  char *qconvname;
13804  PGresult *res;
13805  int i_conforencoding;
13806  int i_contoencoding;
13807  int i_conproc;
13808  int i_condefault;
13809  const char *conforencoding;
13810  const char *contoencoding;
13811  const char *conproc;
13812  bool condefault;
13813 
13814  /* Skip if not to be dumped */
13815  if (!convinfo->dobj.dump || dopt->dataOnly)
13816  return;
13817 
13818  query = createPQExpBuffer();
13819  q = createPQExpBuffer();
13820  delq = createPQExpBuffer();
13821 
13822  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13823 
13824  /* Get conversion-specific details */
13825  appendPQExpBuffer(query, "SELECT "
13826  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13827  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13828  "conproc, condefault "
13829  "FROM pg_catalog.pg_conversion c "
13830  "WHERE c.oid = '%u'::pg_catalog.oid",
13831  convinfo->dobj.catId.oid);
13832 
13833  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13834 
13835  i_conforencoding = PQfnumber(res, "conforencoding");
13836  i_contoencoding = PQfnumber(res, "contoencoding");
13837  i_conproc = PQfnumber(res, "conproc");
13838  i_condefault = PQfnumber(res, "condefault");
13839 
13840  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13841  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13842  conproc = PQgetvalue(res, 0, i_conproc);
13843  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13844 
13845  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13846  fmtQualifiedDumpable(convinfo));
13847 
13848  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13849  (condefault) ? "DEFAULT " : "",
13850  fmtQualifiedDumpable(convinfo));
13851  appendStringLiteralAH(q, conforencoding, fout);
13852  appendPQExpBufferStr(q, " TO ");
13853  appendStringLiteralAH(q, contoencoding, fout);
13854  /* regproc output is already sufficiently quoted */
13855  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13856 
13857  if (dopt->binary_upgrade)
13858  binary_upgrade_extension_member(q, &convinfo->dobj,
13859  "CONVERSION", qconvname,
13860  convinfo->dobj.namespace->dobj.name);
13861 
13862  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13863  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13864  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13865  .namespace = convinfo->dobj.namespace->dobj.name,