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 "common/connect.h"
#include "dumputils.h"
#include "fe_utils/string_utils.h"
#include "getopt_long.h"
#include "libpq/libpq-fs.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "storage/block.h"
Include dependency graph for pg_dump.c:

Go to the source code of this file.

Data Structures

struct  CommentItem
 
struct  SecLabelItem
 

Macros

#define DUMP_DEFAULT_ROWS_PER_INSERT   1
 
#define fmtQualifiedDumpable(obj)
 

Typedefs

typedef enum OidOptions OidOptions
 

Enumerations

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

Functions

static void help (const char *progname)
 
static void setup_connection (Archive *AH, const char *dumpencoding, const char *dumpsnapshot, char *use_role)
 
static ArchiveFormat parseArchiveFormat (const char *format, ArchiveMode *mode)
 
static void expand_schema_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_foreign_server_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids)
 
static void expand_table_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static NamespaceInfofindNamespace (Oid nsoid)
 
static void dumpTableData (Archive *fout, 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 DumpId dumpACL (Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
 
static void getDependencies (Archive *fout)
 
static void BuildArchiveDependencies (Archive *fout)
 
static void findDumpableDependencies (ArchiveHandle *AH, DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
 
static DumpableObjectcreateBoundaryObjects (void)
 
static void addBoundaryDependencies (DumpableObject **dobjs, int numObjs, DumpableObject *boundaryObjs)
 
static void addConstrChildIdxDeps (DumpableObject *dobj, IndxInfo *refidx)
 
static void getDomainConstraints (Archive *fout, TypeInfo *tyinfo)
 
static void getTableData (DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
 
static void makeTableDataInfo (DumpOptions *dopt, TableInfo *tbinfo)
 
static void buildMatViewRefreshDependencies (Archive *fout)
 
static void getTableDataFKConstraints (void)
 
static char * format_function_arguments (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 (const char *proc)
 
static char * getFormattedOperatorName (const char *oproid)
 
static char * convertTSFunction (Archive *fout, Oid funcOid)
 
static Oid findLastBuiltinOid_V71 (Archive *fout)
 
static char * getFormattedTypeName (Archive *fout, Oid oid, OidOptions opts)
 
static void getBlobs (Archive *fout)
 
static void dumpBlob (Archive *fout, 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 void binary_upgrade_set_type_oids_by_rel_oid (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_rel_oid)
 
static void binary_upgrade_set_pg_class_oids (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
 
static void binary_upgrade_extension_member (PQExpBuffer upgrade_buffer, 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 18168 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().

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

◆ addConstrChildIdxDeps()

static void addConstrChildIdxDeps ( DumpableObject dobj,
IndxInfo refidx 
)
static

Definition at line 7568 of file pg_dump.c.

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

Referenced by getConstraints().

7569 {
7570  SimplePtrListCell *cell;
7571 
7572  Assert(dobj->objType == DO_FK_CONSTRAINT);
7573 
7574  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7575  {
7576  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7577 
7578  addObjectDependency(dobj, attach->dobj.dumpId);
7579 
7580  if (attach->partitionIdx->partattaches.head != NULL)
7581  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7582  }
7583 }
DumpId dumpId
Definition: pg_dump.h:129
DumpableObject dobj
Definition: pg_dump.h:380
IndxInfo * partitionIdx
Definition: pg_dump.h:382
SimplePtrList partattaches
Definition: pg_dump.h:372
struct SimplePtrListCell * next
Definition: simple_list.h:48
#define Assert(condition)
Definition: c.h:746
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:785
SimplePtrListCell * head
Definition: simple_list.h:54
static void addConstrChildIdxDeps(DumpableObject *dobj, IndxInfo *refidx)
Definition: pg_dump.c:7568
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 4417 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().

4423 {
4424  if (dobj->depends_on_ext)
4425  {
4426  char *nm;
4427  PGresult *res;
4428  PQExpBuffer query;
4429  int ntups;
4430  int i_extname;
4431  int i;
4432 
4433  /* dodge fmtId() non-reentrancy */
4434  nm = pg_strdup(objname);
4435 
4436  query = createPQExpBuffer();
4437  appendPQExpBuffer(query,
4438  "SELECT e.extname "
4439  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4440  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4441  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4442  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4443  catalog,
4444  dobj->catId.oid);
4445  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4446  ntups = PQntuples(res);
4447  i_extname = PQfnumber(res, "extname");
4448  for (i = 0; i < ntups; i++)
4449  {
4450  appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4451  keyword, nm,
4452  fmtId(PQgetvalue(res, i, i_extname)));
4453  }
4454 
4455  PQclear(res);
4456  destroyPQExpBuffer(query);
4457  pg_free(nm);
4458  }
4459 }
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:296

◆ appendReloptionsArrayAH()

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

Definition at line 18467 of file pg_dump.c.

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

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

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

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

4647 {
4648  DumpableObject *extobj = NULL;
4649  int i;
4650 
4651  if (!dobj->ext_member)
4652  return;
4653 
4654  /*
4655  * Find the parent extension. We could avoid this search if we wanted to
4656  * add a link field to DumpableObject, but the space costs of that would
4657  * be considerable. We assume that member objects could only have a
4658  * direct dependency on their own extension, not any others.
4659  */
4660  for (i = 0; i < dobj->nDeps; i++)
4661  {
4662  extobj = findObjectByDumpId(dobj->dependencies[i]);
4663  if (extobj && extobj->objType == DO_EXTENSION)
4664  break;
4665  extobj = NULL;
4666  }
4667  if (extobj == NULL)
4668  fatal("could not find parent extension for %s %s",
4669  objtype, objname);
4670 
4671  appendPQExpBufferStr(upgrade_buffer,
4672  "\n-- For binary upgrade, handle extension membership the hard way\n");
4673  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4674  fmtId(extobj->name),
4675  objtype);
4676  if (objnamespace && *objnamespace)
4677  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4678  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4679 }
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 4559 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().

4562 {
4563  appendPQExpBufferStr(upgrade_buffer,
4564  "\n-- For binary upgrade, must preserve pg_class oids\n");
4565 
4566  if (!is_index)
4567  {
4568  PQExpBuffer upgrade_query = createPQExpBuffer();
4569  PGresult *upgrade_res;
4570  Oid pg_class_reltoastrelid;
4571  char pg_class_relkind;
4572  Oid pg_index_indexrelid;
4573 
4574  appendPQExpBuffer(upgrade_buffer,
4575  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4576  pg_class_oid);
4577 
4578  /*
4579  * Preserve the OIDs of the table's toast table and index, if any.
4580  * Indexes cannot have toast tables, so we need not make this probe in
4581  * the index code path.
4582  *
4583  * One complexity is that the current table definition might not
4584  * require the creation of a TOAST table, but the old database might
4585  * have a TOAST table that was created earlier, before some wide
4586  * columns were dropped. By setting the TOAST oid we force creation
4587  * of the TOAST heap and index by the new backend, so we can copy the
4588  * files during binary upgrade without worrying about this case.
4589  */
4590  appendPQExpBuffer(upgrade_query,
4591  "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
4592  "FROM pg_catalog.pg_class c LEFT JOIN "
4593  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4594  "WHERE c.oid = '%u'::pg_catalog.oid;",
4595  pg_class_oid);
4596 
4597  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4598 
4599  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
4600  PQfnumber(upgrade_res, "reltoastrelid")));
4601  pg_class_relkind = *PQgetvalue(upgrade_res, 0,
4602  PQfnumber(upgrade_res, "relkind"));
4603  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
4604  PQfnumber(upgrade_res, "indexrelid")));
4605 
4606  /*
4607  * In a pre-v12 database, partitioned tables might be marked as having
4608  * toast tables, but we should ignore them if so.
4609  */
4610  if (OidIsValid(pg_class_reltoastrelid) &&
4611  pg_class_relkind != RELKIND_PARTITIONED_TABLE)
4612  {
4613  appendPQExpBuffer(upgrade_buffer,
4614  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4615  pg_class_reltoastrelid);
4616 
4617  /* every toast table has an index */
4618  appendPQExpBuffer(upgrade_buffer,
4619  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4620  pg_index_indexrelid);
4621  }
4622 
4623  PQclear(upgrade_res);
4624  destroyPQExpBuffer(upgrade_query);
4625  }
4626  else
4627  appendPQExpBuffer(upgrade_buffer,
4628  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4629  pg_class_oid);
4630 
4631  appendPQExpBufferChar(upgrade_buffer, '\n');
4632 }
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:652
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:311
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 void binary_upgrade_set_type_oids_by_rel_oid ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_rel_oid 
)
static

Definition at line 4532 of file pg_dump.c.

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

Referenced by dumpTableSchema().

4535 {
4536  PQExpBuffer upgrade_query = createPQExpBuffer();
4537  PGresult *upgrade_res;
4538  Oid pg_type_oid;
4539 
4540  appendPQExpBuffer(upgrade_query,
4541  "SELECT c.reltype AS crel "
4542  "FROM pg_catalog.pg_class c "
4543  "WHERE c.oid = '%u'::pg_catalog.oid;",
4544  pg_rel_oid);
4545 
4546  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4547 
4548  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4549 
4550  if (OidIsValid(pg_type_oid))
4551  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4552  pg_type_oid, false);
4553 
4554  PQclear(upgrade_res);
4555  destroyPQExpBuffer(upgrade_query);
4556 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:311
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:4463
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

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

4467 {
4468  PQExpBuffer upgrade_query = createPQExpBuffer();
4469  PGresult *res;
4470  Oid pg_type_array_oid;
4471 
4472  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4473  appendPQExpBuffer(upgrade_buffer,
4474  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4475  pg_type_oid);
4476 
4477  /* we only support old >= 8.3 for binary upgrades */
4478  appendPQExpBuffer(upgrade_query,
4479  "SELECT typarray "
4480  "FROM pg_catalog.pg_type "
4481  "WHERE oid = '%u'::pg_catalog.oid;",
4482  pg_type_oid);
4483 
4484  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4485 
4486  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4487 
4488  PQclear(res);
4489 
4490  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4491  {
4492  /*
4493  * If the old version didn't assign an array type, but the new version
4494  * does, we must select an unused type OID to assign. This currently
4495  * only happens for domains, when upgrading pre-v11 to v11 and up.
4496  *
4497  * Note: local state here is kind of ugly, but we must have some,
4498  * since we mustn't choose the same unused OID more than once.
4499  */
4500  static Oid next_possible_free_oid = FirstNormalObjectId;
4501  bool is_dup;
4502 
4503  do
4504  {
4505  ++next_possible_free_oid;
4506  printfPQExpBuffer(upgrade_query,
4507  "SELECT EXISTS(SELECT 1 "
4508  "FROM pg_catalog.pg_type "
4509  "WHERE oid = '%u'::pg_catalog.oid);",
4510  next_possible_free_oid);
4511  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4512  is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
4513  PQclear(res);
4514  } while (is_dup);
4515 
4516  pg_type_array_oid = next_possible_free_oid;
4517  }
4518 
4519  if (OidIsValid(pg_type_array_oid))
4520  {
4521  appendPQExpBufferStr(upgrade_buffer,
4522  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4523  appendPQExpBuffer(upgrade_buffer,
4524  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4525  pg_type_array_oid);
4526  }
4527 
4528  destroyPQExpBuffer(upgrade_query);
4529 }
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:652
#define FirstNormalObjectId
Definition: transam.h:190
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:311
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 18281 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().

18282 {
18283  ArchiveHandle *AH = (ArchiveHandle *) fout;
18284  TocEntry *te;
18285 
18286  /* Scan all TOC entries in the archive */
18287  for (te = AH->toc->next; te != AH->toc; te = te->next)
18288  {
18289  DumpableObject *dobj;
18290  DumpId *dependencies;
18291  int nDeps;
18292  int allocDeps;
18293 
18294  /* No need to process entries that will not be dumped */
18295  if (te->reqs == 0)
18296  continue;
18297  /* Ignore entries that already have "special" dependencies */
18298  if (te->nDeps > 0)
18299  continue;
18300  /* Otherwise, look up the item's original DumpableObject, if any */
18301  dobj = findObjectByDumpId(te->dumpId);
18302  if (dobj == NULL)
18303  continue;
18304  /* No work if it has no dependencies */
18305  if (dobj->nDeps <= 0)
18306  continue;
18307  /* Set up work array */
18308  allocDeps = 64;
18309  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18310  nDeps = 0;
18311  /* Recursively find all dumpable dependencies */
18312  findDumpableDependencies(AH, dobj,
18313  &dependencies, &nDeps, &allocDeps);
18314  /* And save 'em ... */
18315  if (nDeps > 0)
18316  {
18317  dependencies = (DumpId *) pg_realloc(dependencies,
18318  nDeps * sizeof(DumpId));
18319  te->dependencies = dependencies;
18320  te->nDeps = nDeps;
18321  }
18322  else
18323  free(dependencies);
18324  }
18325 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:242
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:18329
#define free(a)
Definition: header.h:65

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

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

2469 {
2470  PQExpBuffer query;
2471  PGresult *res;
2472  int ntups,
2473  i;
2474  int i_classid,
2475  i_objid,
2476  i_refobjid;
2477 
2478  /* No Mat Views before 9.3. */
2479  if (fout->remoteVersion < 90300)
2480  return;
2481 
2482  query = createPQExpBuffer();
2483 
2484  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2485  "( "
2486  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2487  "FROM pg_depend d1 "
2488  "JOIN pg_class c1 ON c1.oid = d1.objid "
2489  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2490  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2491  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2492  "AND d2.objid = r1.oid "
2493  "AND d2.refobjid <> d1.objid "
2494  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2495  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2496  CppAsString2(RELKIND_VIEW) ") "
2497  "WHERE d1.classid = 'pg_class'::regclass "
2498  "UNION "
2499  "SELECT w.objid, d3.refobjid, c3.relkind "
2500  "FROM w "
2501  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2502  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2503  "AND d3.objid = r3.oid "
2504  "AND d3.refobjid <> w.refobjid "
2505  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2506  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2507  CppAsString2(RELKIND_VIEW) ") "
2508  ") "
2509  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2510  "FROM w "
2511  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2512 
2513  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2514 
2515  ntups = PQntuples(res);
2516 
2517  i_classid = PQfnumber(res, "classid");
2518  i_objid = PQfnumber(res, "objid");
2519  i_refobjid = PQfnumber(res, "refobjid");
2520 
2521  for (i = 0; i < ntups; i++)
2522  {
2523  CatalogId objId;
2524  CatalogId refobjId;
2525  DumpableObject *dobj;
2526  DumpableObject *refdobj;
2527  TableInfo *tbinfo;
2528  TableInfo *reftbinfo;
2529 
2530  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2531  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2532  refobjId.tableoid = objId.tableoid;
2533  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2534 
2535  dobj = findObjectByCatalogId(objId);
2536  if (dobj == NULL)
2537  continue;
2538 
2539  Assert(dobj->objType == DO_TABLE);
2540  tbinfo = (TableInfo *) dobj;
2541  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2542  dobj = (DumpableObject *) tbinfo->dataObj;
2543  if (dobj == NULL)
2544  continue;
2545  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2546 
2547  refdobj = findObjectByCatalogId(refobjId);
2548  if (refdobj == NULL)
2549  continue;
2550 
2551  Assert(refdobj->objType == DO_TABLE);
2552  reftbinfo = (TableInfo *) refdobj;
2553  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2554  refdobj = (DumpableObject *) reftbinfo->dataObj;
2555  if (refdobj == NULL)
2556  continue;
2557  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2558 
2559  addObjectDependency(dobj, refdobj->dumpId);
2560 
2561  if (!reftbinfo->relispopulated)
2562  tbinfo->relispopulated = false;
2563  }
2564 
2565  PQclear(res);
2566 
2567  destroyPQExpBuffer(query);
2568 }
char relkind
Definition: pg_dump.h:265
Oid tableoid
Definition: pg_backup.h:238
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:223
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:746
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:785
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:296
int remoteVersion
Definition: pg_backup.h:195

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

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

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

◆ collectComments()

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

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

9942 {
9943  PGresult *res;
9944  PQExpBuffer query;
9945  int i_description;
9946  int i_classoid;
9947  int i_objoid;
9948  int i_objsubid;
9949  int ntups;
9950  int i;
9951  CommentItem *comments;
9952 
9953  query = createPQExpBuffer();
9954 
9955  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9956  "FROM pg_catalog.pg_description "
9957  "ORDER BY classoid, objoid, objsubid");
9958 
9959  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9960 
9961  /* Construct lookup table containing OIDs in numeric form */
9962 
9963  i_description = PQfnumber(res, "description");
9964  i_classoid = PQfnumber(res, "classoid");
9965  i_objoid = PQfnumber(res, "objoid");
9966  i_objsubid = PQfnumber(res, "objsubid");
9967 
9968  ntups = PQntuples(res);
9969 
9970  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9971 
9972  for (i = 0; i < ntups; i++)
9973  {
9974  comments[i].descr = PQgetvalue(res, i, i_description);
9975  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9976  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9977  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9978  }
9979 
9980  /* Do NOT free the PGresult since we are keeping pointers into it */
9981  destroyPQExpBuffer(query);
9982 
9983  *items = comments;
9984  return ntups;
9985 }
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:296

◆ collectSecLabels()

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

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

15244 {
15245  PGresult *res;
15246  PQExpBuffer query;
15247  int i_label;
15248  int i_provider;
15249  int i_classoid;
15250  int i_objoid;
15251  int i_objsubid;
15252  int ntups;
15253  int i;
15254  SecLabelItem *labels;
15255 
15256  query = createPQExpBuffer();
15257 
15258  appendPQExpBufferStr(query,
15259  "SELECT label, provider, classoid, objoid, objsubid "
15260  "FROM pg_catalog.pg_seclabel "
15261  "ORDER BY classoid, objoid, objsubid");
15262 
15263  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15264 
15265  /* Construct lookup table containing OIDs in numeric form */
15266  i_label = PQfnumber(res, "label");
15267  i_provider = PQfnumber(res, "provider");
15268  i_classoid = PQfnumber(res, "classoid");
15269  i_objoid = PQfnumber(res, "objoid");
15270  i_objsubid = PQfnumber(res, "objsubid");
15271 
15272  ntups = PQntuples(res);
15273 
15274  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15275 
15276  for (i = 0; i < ntups; i++)
15277  {
15278  labels[i].label = PQgetvalue(res, i, i_label);
15279  labels[i].provider = PQgetvalue(res, i, i_provider);
15280  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15281  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15282  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15283  }
15284 
15285  /* Do NOT free the PGresult since we are keeping pointers into it */
15286  destroyPQExpBuffer(query);
15287 
15288  *items = labels;
15289  return ntups;
15290 }
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:296

◆ convertRegProcReference()

static char * convertRegProcReference ( const char *  proc)
static

Definition at line 12762 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

12763 {
12764  char *name;
12765  char *paren;
12766  bool inquote;
12767 
12768  /* In all cases "-" means a null reference */
12769  if (strcmp(proc, "-") == 0)
12770  return NULL;
12771 
12772  name = pg_strdup(proc);
12773  /* find non-double-quoted left paren */
12774  inquote = false;
12775  for (paren = name; *paren; paren++)
12776  {
12777  if (*paren == '(' && !inquote)
12778  {
12779  *paren = '\0';
12780  break;
12781  }
12782  if (*paren == '"')
12783  inquote = !inquote;
12784  }
12785  return name;
12786 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * name
Definition: encode.c:561

◆ convertTSFunction()

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

Definition at line 12833 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

12834 {
12835  char *result;
12836  char query[128];
12837  PGresult *res;
12838 
12839  snprintf(query, sizeof(query),
12840  "SELECT '%u'::pg_catalog.regproc", funcOid);
12841  res = ExecuteSqlQueryForSingleRow(fout, query);
12842 
12843  result = pg_strdup(PQgetvalue(res, 0, 0));
12844 
12845  PQclear(res);
12846 
12847  return result;
12848 }
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:311
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:215

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

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

18145 {
18146  DumpableObject *dobjs;
18147 
18148  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18149 
18150  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18151  dobjs[0].catId = nilCatalogId;
18152  AssignDumpId(dobjs + 0);
18153  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18154 
18155  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18156  dobjs[1].catId = nilCatalogId;
18157  AssignDumpId(dobjs + 1);
18158  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18159 
18160  return dobjs;
18161 }
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 15486 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().

15487 {
15488  PQExpBuffer result = createPQExpBuffer();
15489  int j;
15490 
15491  appendPQExpBufferStr(result, "SELECT");
15492 
15493  for (j = 0; j < tbinfo->numatts; j++)
15494  {
15495  if (j > 0)
15496  appendPQExpBufferChar(result, ',');
15497  appendPQExpBufferStr(result, "\n ");
15498 
15499  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15500 
15501  /*
15502  * Must add collation if not default for the type, because CREATE OR
15503  * REPLACE VIEW won't change it
15504  */
15505  if (OidIsValid(tbinfo->attcollation[j]))
15506  {
15507  CollInfo *coll;
15508 
15509  coll = findCollationByOid(tbinfo->attcollation[j]);
15510  if (coll)
15511  appendPQExpBuffer(result, " COLLATE %s",
15512  fmtQualifiedDumpable(coll));
15513  }
15514 
15515  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15516  }
15517 
15518  return result;
15519 }
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:652
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:876

◆ createViewAsClause()

static PQExpBuffer createViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

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

15438 {
15439  PQExpBuffer query = createPQExpBuffer();
15440  PQExpBuffer result = createPQExpBuffer();
15441  PGresult *res;
15442  int len;
15443 
15444  /* Fetch the view definition */
15445  appendPQExpBuffer(query,
15446  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15447  tbinfo->dobj.catId.oid);
15448 
15449  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15450 
15451  if (PQntuples(res) != 1)
15452  {
15453  if (PQntuples(res) < 1)
15454  fatal("query to obtain definition of view \"%s\" returned no data",
15455  tbinfo->dobj.name);
15456  else
15457  fatal("query to obtain definition of view \"%s\" returned more than one definition",
15458  tbinfo->dobj.name);
15459  }
15460 
15461  len = PQgetlength(res, 0, 0);
15462 
15463  if (len == 0)
15464  fatal("definition of view \"%s\" appears to be empty (length zero)",
15465  tbinfo->dobj.name);
15466 
15467  /* Strip off the trailing semicolon so that other things may follow. */
15468  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15469  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15470 
15471  PQclear(res);
15472  destroyPQExpBuffer(query);
15473 
15474  return result;
15475 }
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:746
#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:296

◆ dumpAccessMethod()

static void dumpAccessMethod ( Archive fout,
AccessMethodInfo oprinfo 
)
static

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

12856 {
12857  DumpOptions *dopt = fout->dopt;
12858  PQExpBuffer q;
12859  PQExpBuffer delq;
12860  char *qamname;
12861 
12862  /* Skip if not to be dumped */
12863  if (!aminfo->dobj.dump || dopt->dataOnly)
12864  return;
12865 
12866  q = createPQExpBuffer();
12867  delq = createPQExpBuffer();
12868 
12869  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12870 
12871  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12872 
12873  switch (aminfo->amtype)
12874  {
12875  case AMTYPE_INDEX:
12876  appendPQExpBufferStr(q, "TYPE INDEX ");
12877  break;
12878  case AMTYPE_TABLE:
12879  appendPQExpBufferStr(q, "TYPE TABLE ");
12880  break;
12881  default:
12882  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12883  aminfo->amtype, qamname);
12884  destroyPQExpBuffer(q);
12885  destroyPQExpBuffer(delq);
12886  free(qamname);
12887  return;
12888  }
12889 
12890  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12891 
12892  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12893  qamname);
12894 
12895  if (dopt->binary_upgrade)
12896  binary_upgrade_extension_member(q, &aminfo->dobj,
12897  "ACCESS METHOD", qamname, NULL);
12898 
12899  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12900  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12901  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12902  .description = "ACCESS METHOD",
12903  .section = SECTION_PRE_DATA,
12904  .createStmt = q->data,
12905  .dropStmt = delq->data));
12906 
12907  /* Dump Access Method Comments */
12908  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12909  dumpComment(fout, "ACCESS METHOD", qamname,
12910  NULL, "",
12911  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12912 
12913  destroyPQExpBuffer(q);
12914  destroyPQExpBuffer(delq);
12915  free(qamname);
12916 }
#define ARCHIVE_OPTS(...)
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:190
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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:4642
#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:9678

◆ dumpACL()

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

Definition at line 14897 of file pg_dump.c.

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

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

14902 {
14903  DumpId aclDumpId = InvalidDumpId;
14904  DumpOptions *dopt = fout->dopt;
14905  PQExpBuffer sql;
14906 
14907  /* Do nothing if ACL dump is not enabled */
14908  if (dopt->aclsSkip)
14909  return InvalidDumpId;
14910 
14911  /* --data-only skips ACLs *except* BLOB ACLs */
14912  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14913  return InvalidDumpId;
14914 
14915  sql = createPQExpBuffer();
14916 
14917  /*
14918  * Check to see if this object has had any initial ACLs included for it.
14919  * If so, we are in binary upgrade mode and these are the ACLs to turn
14920  * into GRANT and REVOKE statements to set and record the initial
14921  * privileges for an extension object. Let the backend know that these
14922  * are to be recorded by calling binary_upgrade_set_record_init_privs()
14923  * before and after.
14924  */
14925  if (strlen(initacls) != 0 || strlen(initracls) != 0)
14926  {
14927  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14928  if (!buildACLCommands(name, subname, nspname, type,
14929  initacls, initracls, owner,
14930  "", fout->remoteVersion, sql))
14931  fatal("could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)",
14932  initacls, initracls, name, type);
14933  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14934  }
14935 
14936  if (!buildACLCommands(name, subname, nspname, type,
14937  acls, racls, owner,
14938  "", fout->remoteVersion, sql))
14939  fatal("could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)",
14940  acls, racls, name, type);
14941 
14942  if (sql->len > 0)
14943  {
14945  DumpId aclDeps[2];
14946  int nDeps = 0;
14947 
14948  if (subname)
14949  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
14950  else
14951  appendPQExpBuffer(tag, "%s %s", type, name);
14952 
14953  aclDeps[nDeps++] = objDumpId;
14954  if (altDumpId != InvalidDumpId)
14955  aclDeps[nDeps++] = altDumpId;
14956 
14957  aclDumpId = createDumpId();
14958 
14959  ArchiveEntry(fout, nilCatalogId, aclDumpId,
14960  ARCHIVE_OPTS(.tag = tag->data,
14961  .namespace = nspname,
14962  .owner = owner,
14963  .description = "ACL",
14964  .section = SECTION_NONE,
14965  .createStmt = sql->data,
14966  .deps = aclDeps,
14967  .nDeps = nDeps));
14968 
14969  destroyPQExpBuffer(tag);
14970  }
14971 
14972  destroyPQExpBuffer(sql);
14973 
14974  return aclDumpId;
14975 }
#define ARCHIVE_OPTS(...)
int DumpId
Definition: pg_backup.h:242
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:190
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
bool aclsSkip
Definition: pg_backup.h:149
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define InvalidDumpId
Definition: pg_backup.h:244
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:561
static const CatalogId nilCatalogId
Definition: pg_dump.c:125
DumpId createDumpId(void)
Definition: common.c:590
int remoteVersion
Definition: pg_backup.h:195

◆ dumpAgg()

static void dumpAgg ( Archive fout,
AggInfo agginfo 
)
static

Definition at line 13843 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, InvalidDumpId, _dumpableObject::name, CatalogId::oid, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), _funcInfo::proacl, Archive::remoteVersion, _funcInfo::rolname, _funcInfo::rproacl, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

13844 {
13845  DumpOptions *dopt = fout->dopt;
13846  PQExpBuffer query;
13847  PQExpBuffer q;
13848  PQExpBuffer delq;
13849  PQExpBuffer details;
13850  char *aggsig; /* identity signature */
13851  char *aggfullsig = NULL; /* full signature */
13852  char *aggsig_tag;
13853  PGresult *res;
13854  int i_agginitval;
13855  int i_aggminitval;
13856  const char *aggtransfn;
13857  const char *aggfinalfn;
13858  const char *aggcombinefn;
13859  const char *aggserialfn;
13860  const char *aggdeserialfn;
13861  const char *aggmtransfn;
13862  const char *aggminvtransfn;
13863  const char *aggmfinalfn;
13864  bool aggfinalextra;
13865  bool aggmfinalextra;
13866  char aggfinalmodify;
13867  char aggmfinalmodify;
13868  const char *aggsortop;
13869  char *aggsortconvop;
13870  char aggkind;
13871  const char *aggtranstype;
13872  const char *aggtransspace;
13873  const char *aggmtranstype;
13874  const char *aggmtransspace;
13875  const char *agginitval;
13876  const char *aggminitval;
13877  const char *proparallel;
13878  char defaultfinalmodify;
13879 
13880  /* Skip if not to be dumped */
13881  if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13882  return;
13883 
13884  query = createPQExpBuffer();
13885  q = createPQExpBuffer();
13886  delq = createPQExpBuffer();
13887  details = createPQExpBuffer();
13888 
13889  /* Get aggregate-specific details */
13890  appendPQExpBufferStr(query,
13891  "SELECT\n"
13892  "aggtransfn,\n"
13893  "aggfinalfn,\n"
13894  "aggtranstype::pg_catalog.regtype,\n"
13895  "agginitval,\n");
13896 
13897  if (fout->remoteVersion >= 80100)
13898  appendPQExpBufferStr(query,
13899  "aggsortop,\n");
13900  else
13901  appendPQExpBufferStr(query,
13902  "0 AS aggsortop,\n");
13903 
13904  if (fout->remoteVersion >= 80400)
13905  appendPQExpBufferStr(query,
13906  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13907  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
13908 
13909  if (fout->remoteVersion >= 90400)
13910  appendPQExpBufferStr(query,
13911  "aggkind,\n"
13912  "aggmtransfn,\n"
13913  "aggminvtransfn,\n"
13914  "aggmfinalfn,\n"
13915  "aggmtranstype::pg_catalog.regtype,\n"
13916  "aggfinalextra,\n"
13917  "aggmfinalextra,\n"
13918  "aggtransspace,\n"
13919  "aggmtransspace,\n"
13920  "aggminitval,\n");
13921  else
13922  appendPQExpBufferStr(query,
13923  "'n' AS aggkind,\n"
13924  "'-' AS aggmtransfn,\n"
13925  "'-' AS aggminvtransfn,\n"
13926  "'-' AS aggmfinalfn,\n"
13927  "0 AS aggmtranstype,\n"
13928  "false AS aggfinalextra,\n"
13929  "false AS aggmfinalextra,\n"
13930  "0 AS aggtransspace,\n"
13931  "0 AS aggmtransspace,\n"
13932  "NULL AS aggminitval,\n");
13933 
13934  if (fout->remoteVersion >= 90600)
13935  appendPQExpBufferStr(query,
13936  "aggcombinefn,\n"
13937  "aggserialfn,\n"
13938  "aggdeserialfn,\n"
13939  "proparallel,\n");
13940  else
13941  appendPQExpBufferStr(query,
13942  "'-' AS aggcombinefn,\n"
13943  "'-' AS aggserialfn,\n"
13944  "'-' AS aggdeserialfn,\n"
13945  "'u' AS proparallel,\n");
13946 
13947  if (fout->remoteVersion >= 110000)
13948  appendPQExpBufferStr(query,
13949  "aggfinalmodify,\n"
13950  "aggmfinalmodify\n");
13951  else
13952  appendPQExpBufferStr(query,
13953  "'0' AS aggfinalmodify,\n"
13954  "'0' AS aggmfinalmodify\n");
13955 
13956  appendPQExpBuffer(query,
13957  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13958  "WHERE a.aggfnoid = p.oid "
13959  "AND p.oid = '%u'::pg_catalog.oid",
13960  agginfo->aggfn.dobj.catId.oid);
13961 
13962  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13963 
13964  i_agginitval = PQfnumber(res, "agginitval");
13965  i_aggminitval = PQfnumber(res, "aggminitval");
13966 
13967  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
13968  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
13969  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
13970  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
13971  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
13972  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
13973  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
13974  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
13975  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
13976  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
13977  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
13978  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
13979  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
13980  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
13981  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
13982  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
13983  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
13984  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
13985  agginitval = PQgetvalue(res, 0, i_agginitval);
13986  aggminitval = PQgetvalue(res, 0, i_aggminitval);
13987  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13988 
13989  if (fout->remoteVersion >= 80400)
13990  {
13991  /* 8.4 or later; we rely on server-side code for most of the work */
13992  char *funcargs;
13993  char *funciargs;
13994 
13995  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13996  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13997  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13998  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13999  }
14000  else
14001  /* pre-8.4, do it ourselves */
14002  aggsig = format_aggregate_signature(agginfo, fout, true);
14003 
14004  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14005 
14006  /* identify default modify flag for aggkind (must match DefineAggregate) */
14007  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14008  /* replace omitted flags for old versions */
14009  if (aggfinalmodify == '0')
14010  aggfinalmodify = defaultfinalmodify;
14011  if (aggmfinalmodify == '0')
14012  aggmfinalmodify = defaultfinalmodify;
14013 
14014  /* regproc and regtype output is already sufficiently quoted */
14015  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14016  aggtransfn, aggtranstype);
14017 
14018  if (strcmp(aggtransspace, "0") != 0)
14019  {
14020  appendPQExpBuffer(details, ",\n SSPACE = %s",
14021  aggtransspace);
14022  }
14023 
14024  if (!PQgetisnull(res, 0, i_agginitval))
14025  {
14026  appendPQExpBufferStr(details, ",\n INITCOND = ");
14027  appendStringLiteralAH(details, agginitval, fout);
14028  }
14029 
14030  if (strcmp(aggfinalfn, "-") != 0)
14031  {
14032  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14033  aggfinalfn);
14034  if (aggfinalextra)
14035  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14036  if (aggfinalmodify != defaultfinalmodify)
14037  {
14038  switch (aggfinalmodify)
14039  {
14040  case AGGMODIFY_READ_ONLY:
14041  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14042  break;
14043  case AGGMODIFY_SHAREABLE:
14044  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14045  break;
14046  case AGGMODIFY_READ_WRITE:
14047  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14048  break;
14049  default:
14050  fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14051  agginfo->aggfn.dobj.name);
14052  break;
14053  }
14054  }
14055  }
14056 
14057  if (strcmp(aggcombinefn, "-") != 0)
14058  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14059 
14060  if (strcmp(aggserialfn, "-") != 0)
14061  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14062 
14063  if (strcmp(aggdeserialfn, "-") != 0)
14064  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14065 
14066  if (strcmp(aggmtransfn, "-") != 0)
14067  {
14068  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14069  aggmtransfn,
14070  aggminvtransfn,
14071  aggmtranstype);
14072  }
14073 
14074  if (strcmp(aggmtransspace, "0") != 0)
14075  {
14076  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14077  aggmtransspace);
14078  }
14079 
14080  if (!PQgetisnull(res, 0, i_aggminitval))
14081  {
14082  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14083  appendStringLiteralAH(details, aggminitval, fout);
14084  }
14085 
14086  if (strcmp(aggmfinalfn, "-") != 0)
14087  {
14088  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14089  aggmfinalfn);
14090  if (aggmfinalextra)
14091  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14092  if (aggmfinalmodify != defaultfinalmodify)
14093  {
14094  switch (aggmfinalmodify)
14095  {
14096  case AGGMODIFY_READ_ONLY:
14097  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14098  break;
14099  case AGGMODIFY_SHAREABLE:
14100  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14101  break;
14102  case AGGMODIFY_READ_WRITE:
14103  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14104  break;
14105  default:
14106  fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14107  agginfo->aggfn.dobj.name);
14108  break;
14109  }
14110  }
14111  }
14112 
14113  aggsortconvop = getFormattedOperatorName(aggsortop);
14114  if (aggsortconvop)
14115  {
14116  appendPQExpBuffer(details, ",\n SORTOP = %s",
14117  aggsortconvop);
14118  free(aggsortconvop);
14119  }
14120 
14121  if (aggkind == AGGKIND_HYPOTHETICAL)
14122  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14123 
14124  if (proparallel[0] != PROPARALLEL_UNSAFE)
14125  {
14126  if (proparallel[0] == PROPARALLEL_SAFE)
14127  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14128  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14129  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14130  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14131  fatal("unrecognized proparallel value for function \"%s\"",
14132  agginfo->aggfn.dobj.name);
14133  }
14134 
14135  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14136  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14137  aggsig);
14138 
14139  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14140  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14141  aggfullsig ? aggfullsig : aggsig, details->data);
14142 
14143  if (dopt->binary_upgrade)
14145  "AGGREGATE", aggsig,
14146  agginfo->aggfn.dobj.namespace->dobj.name);
14147 
14148  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14149  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14150  agginfo->aggfn.dobj.dumpId,
14151  ARCHIVE_OPTS(.tag = aggsig_tag,
14152  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14153  .owner = agginfo->aggfn.rolname,
14154  .description = "AGGREGATE",
14155  .section = SECTION_PRE_DATA,
14156  .createStmt = q->data,
14157  .dropStmt = delq->data));
14158 
14159  /* Dump Aggregate Comments */
14160  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14161  dumpComment(fout, "AGGREGATE", aggsig,
14162  agginfo->aggfn.dobj.namespace->dobj.name,
14163  agginfo->aggfn.rolname,
14164  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14165 
14166  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14167  dumpSecLabel(fout, "AGGREGATE", aggsig,
14168  agginfo->aggfn.dobj.namespace->dobj.name,
14169  agginfo->aggfn.rolname,
14170  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14171 
14172  /*
14173  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14174  * command look like a function's GRANT; in particular this affects the
14175  * syntax for zero-argument aggregates and ordered-set aggregates.
14176  */
14177  free(aggsig);
14178 
14179  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14180 
14181  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14182  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
14183  "FUNCTION", aggsig, NULL,
14184  agginfo->aggfn.dobj.namespace->dobj.name,
14185  agginfo->aggfn.rolname, agginfo->aggfn.proacl,
14186  agginfo->aggfn.rproacl,
14187  agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
14188 
14189  free(aggsig);
14190  if (aggfullsig)
14191  free(aggfullsig);
14192  free(aggsig_tag);
14193 
14194  PQclear(res);
14195 
14196  destroyPQExpBuffer(query);
14197  destroyPQExpBuffer(q);
14198  destroyPQExpBuffer(delq);
14199  destroyPQExpBuffer(details);
14200 }
#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:14996
static char * format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
Definition: pg_dump.c:11665
FuncInfo aggfn
Definition: pg_dump.h:211
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:311
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
static char * getFormattedOperatorName(const char *oproid)
Definition: pg_dump.c:12803
DumpOptions * dopt
Definition: pg_backup.h:190
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define InvalidDumpId
Definition: pg_backup.h:244
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:302
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:11760
char * rolname
Definition: pg_dump.h:197
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14897
#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:4642
char * rproacl
Definition: pg_dump.h:203
#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 char * format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:13805
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:9678
int remoteVersion
Definition: pg_backup.h:195

◆ dumpAttrDef()

static void dumpAttrDef ( Archive fout,
AttrDefInfo adinfo 
)
static

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

16272 {
16273  DumpOptions *dopt = fout->dopt;
16274  TableInfo *tbinfo = adinfo->adtable;
16275  int adnum = adinfo->adnum;
16276  PQExpBuffer q;
16277  PQExpBuffer delq;
16278  char *qualrelname;
16279  char *tag;
16280  char *foreign;
16281 
16282  /* Skip if table definition not to be dumped */
16283  if (!tbinfo->dobj.dump || dopt->dataOnly)
16284  return;
16285 
16286  /* Skip if not "separate"; it was dumped in the table's definition */
16287  if (!adinfo->separate)
16288  return;
16289 
16290  q = createPQExpBuffer();
16291  delq = createPQExpBuffer();
16292 
16293  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16294 
16295  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16296 
16298  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16299  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16300  adinfo->adef_expr);
16301 
16302  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16303  foreign, qualrelname,
16304  fmtId(tbinfo->attnames[adnum - 1]));
16305 
16306  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16307 
16308  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16309  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16310  ARCHIVE_OPTS(.tag = tag,
16311  .namespace = tbinfo->dobj.namespace->dobj.name,
16312  .owner = tbinfo->rolname,
16313  .description = "DEFAULT",
16314  .section = SECTION_PRE_DATA,
16315  .createStmt = q->data,
16316  .dropStmt = delq->data));
16317 
16318  free(tag);
16319  destroyPQExpBuffer(q);
16320  destroyPQExpBuffer(delq);
16321  free(qualrelname);
16322 }
#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:190
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
TableInfo * adtable
Definition: pg_dump.h: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 10704 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), atooid, _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), binary_upgrade_set_type_oids_by_type_oid(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _typeInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), _dumpableObject::dumpId, dumpSecLabel(), ExecuteSqlQueryForSingleRow(), fmtId(), fmtQualifiedDumpable, free, getFormattedTypeName(), _typeInfo::initrtypacl, _typeInfo::inittypacl, InvalidDumpId, _dumpableObject::name, CatalogId::oid, OidIsValid, pg_strdup(), PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), Archive::remoteVersion, _typeInfo::rolname, _typeInfo::rtypacl, SECTION_PRE_DATA, _typeInfo::typacl, typalign, _typeInfo::typelem, and zeroIsError.

Referenced by dumpType().

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

◆ dumpBlob()

static void dumpBlob ( Archive fout,
BlobInfo binfo 
)
static

Definition at line 3454 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

3455 {
3456  PQExpBuffer cquery = createPQExpBuffer();
3457  PQExpBuffer dquery = createPQExpBuffer();
3458 
3459  appendPQExpBuffer(cquery,
3460  "SELECT pg_catalog.lo_create('%s');\n",
3461  binfo->dobj.name);
3462 
3463  appendPQExpBuffer(dquery,
3464  "SELECT pg_catalog.lo_unlink('%s');\n",
3465  binfo->dobj.name);
3466 
3467  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3468  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3469  ARCHIVE_OPTS(.tag = binfo->dobj.name,
3470  .owner = binfo->rolname,
3471  .description = "BLOB",
3472  .section = SECTION_PRE_DATA,
3473  .createStmt = cquery->data,
3474  .dropStmt = dquery->data));
3475 
3476  /* Dump comment if any */
3477  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3478  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3479  NULL, binfo->rolname,
3480  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3481 
3482  /* Dump security label if any */
3483  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3484  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3485  NULL, binfo->rolname,
3486  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3487 
3488  /* Dump ACL if any */
3489  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3490  dumpACL(fout, binfo->dobj.dumpId, InvalidDumpId, "LARGE OBJECT",
3491  binfo->dobj.name, NULL,
3492  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3493  binfo->initblobacl, binfo->initrblobacl);
3494 
3495  destroyPQExpBuffer(cquery);
3496  destroyPQExpBuffer(dquery);
3497 }
#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:14996
char * initblobacl
Definition: pg_dump.h:571
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 InvalidDumpId
Definition: pg_backup.h:244
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14897
#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:9678

◆ dumpBlobs()

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

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

3505 {
3506  const char *blobQry;
3507  const char *blobFetchQry;
3508  PGconn *conn = GetConnection(fout);
3509  PGresult *res;
3510  char buf[LOBBUFSIZE];
3511  int ntups;
3512  int i;
3513  int cnt;
3514 
3515  pg_log_info("saving large objects");
3516 
3517  /*
3518  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3519  * the already-in-memory dumpable objects instead...
3520  */
3521  if (fout->remoteVersion >= 90000)
3522  blobQry =
3523  "DECLARE bloboid CURSOR FOR "
3524  "SELECT oid FROM pg_largeobject_metadata ORDER BY 1";
3525  else
3526  blobQry =
3527  "DECLARE bloboid CURSOR FOR "
3528  "SELECT DISTINCT loid FROM pg_largeobject ORDER BY 1";
3529 
3530  ExecuteSqlStatement(fout, blobQry);
3531 
3532  /* Command to fetch from cursor */
3533  blobFetchQry = "FETCH 1000 IN bloboid";
3534 
3535  do
3536  {
3537  /* Do a fetch */
3538  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3539 
3540  /* Process the tuples, if any */
3541  ntups = PQntuples(res);
3542  for (i = 0; i < ntups; i++)
3543  {
3544  Oid blobOid;
3545  int loFd;
3546 
3547  blobOid = atooid(PQgetvalue(res, i, 0));
3548  /* Open the BLOB */
3549  loFd = lo_open(conn, blobOid, INV_READ);
3550  if (loFd == -1)
3551  fatal("could not open large object %u: %s",
3552  blobOid, PQerrorMessage(conn));
3553 
3554  StartBlob(fout, blobOid);
3555 
3556  /* Now read it in chunks, sending data to archive */
3557  do
3558  {
3559  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3560  if (cnt < 0)
3561  fatal("error reading large object %u: %s",
3562  blobOid, PQerrorMessage(conn));
3563 
3564  WriteData(fout, buf, cnt);
3565  } while (cnt > 0);
3566 
3567  lo_close(conn, loFd);
3568 
3569  EndBlob(fout, blobOid);
3570  }
3571 
3572  PQclear(res);
3573  } while (ntups > 0);
3574 
3575  return 1;
3576 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
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:68
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:142
void WriteData(Archive *AH, const void *data, size_t dLen)
#define atooid(x)
Definition: postgres_ext.h:42
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
void PQclear(PGresult *res)
Definition: fe-exec.c:694
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:109
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:284
#define fatal(...)
int EndBlob(Archive *AH, Oid oid)
int i
#define LOBBUFSIZE
#define pg_log_info(...)
Definition: logging.h:88
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:296
int remoteVersion
Definition: pg_backup.h:195

◆ dumpCast()

static void dumpCast ( Archive fout,
CastInfo cast 
)
static

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

12300 {
12301  DumpOptions *dopt = fout->dopt;
12302  PQExpBuffer defqry;
12303  PQExpBuffer delqry;
12304  PQExpBuffer labelq;
12305  PQExpBuffer castargs;
12306  FuncInfo *funcInfo = NULL;
12307  char *sourceType;
12308  char *targetType;
12309 
12310  /* Skip if not to be dumped */
12311  if (!cast->dobj.dump || dopt->dataOnly)
12312  return;
12313 
12314  /* Cannot dump if we don't have the cast function's info */
12315  if (OidIsValid(cast->castfunc))
12316  {
12317  funcInfo = findFuncByOid(cast->castfunc);
12318  if (funcInfo == NULL)
12319  fatal("could not find function definition for function with OID %u",
12320  cast->castfunc);
12321  }
12322 
12323  defqry = createPQExpBuffer();
12324  delqry = createPQExpBuffer();
12325  labelq = createPQExpBuffer();
12326  castargs = createPQExpBuffer();
12327 
12328  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12329  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12330  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12331  sourceType, targetType);
12332 
12333  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12334  sourceType, targetType);
12335 
12336  switch (cast->castmethod)
12337  {
12338  case COERCION_METHOD_BINARY:
12339  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12340  break;
12341  case COERCION_METHOD_INOUT:
12342  appendPQExpBufferStr(defqry, "WITH INOUT");
12343  break;
12344  case COERCION_METHOD_FUNCTION:
12345  if (funcInfo)
12346  {
12347  char *fsig = format_function_signature(fout, funcInfo, true);
12348 
12349  /*
12350  * Always qualify the function name (format_function_signature
12351  * won't qualify it).
12352  */
12353  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12354  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12355  free(fsig);
12356  }
12357  else
12358  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12359  break;
12360  default:
12361  pg_log_warning("bogus value in pg_cast.castmethod field");
12362  }
12363 
12364  if (cast->castcontext == 'a')
12365  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12366  else if (cast->castcontext == 'i')
12367  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12368  appendPQExpBufferStr(defqry, ";\n");
12369 
12370  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12371  sourceType, targetType);
12372 
12373  appendPQExpBuffer(castargs, "(%s AS %s)",
12374  sourceType, targetType);
12375 
12376  if (dopt->binary_upgrade)
12377  binary_upgrade_extension_member(defqry, &cast->dobj,
12378  "CAST", castargs->data, NULL);
12379 
12380  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12381  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12382  ARCHIVE_OPTS(.tag = labelq->data,
12383  .description = "CAST",
12384  .section = SECTION_PRE_DATA,
12385  .createStmt = defqry->data,
12386  .dropStmt = delqry->data));
12387 
12388  /* Dump Cast Comments */
12389  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12390  dumpComment(fout, "CAST", castargs->data,
12391  NULL, "",
12392  cast->dobj.catId, 0, cast->dobj.dumpId);
12393 
12394  free(sourceType);
12395  free(targetType);
12396 
12397  destroyPQExpBuffer(defqry);
12398  destroyPQExpBuffer(delqry);
12399  destroyPQExpBuffer(labelq);
12400  destroyPQExpBuffer(castargs);
12401 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18385
#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:652
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpableObject dobj
Definition: pg_dump.h:471
Oid casttarget
Definition: pg_dump.h:473
DumpOptions * dopt
Definition: pg_backup.h:190
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:854
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:11760
#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:4642
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:9678

◆ dumpCollation()

static void dumpCollation ( Archive fout,
CollInfo collinfo 
)
static

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

13559 {
13560  DumpOptions *dopt = fout->dopt;
13561  PQExpBuffer query;
13562  PQExpBuffer q;
13563  PQExpBuffer delq;
13564  char *qcollname;
13565  PGresult *res;
13566  int i_collprovider;
13567  int i_collisdeterministic;
13568  int i_collcollate;
13569  int i_collctype;
13570  const char *collprovider;
13571  const char *collcollate;
13572  const char *collctype;
13573 
13574  /* Skip if not to be dumped */
13575  if (!collinfo->dobj.dump || dopt->dataOnly)
13576  return;
13577 
13578  query = createPQExpBuffer();
13579  q = createPQExpBuffer();
13580  delq = createPQExpBuffer();
13581 
13582  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13583 
13584  /* Get collation-specific details */
13585  appendPQExpBufferStr(query, "SELECT ");
13586 
13587  if (fout->remoteVersion >= 100000)
13588  appendPQExpBufferStr(query,
13589  "collprovider, "
13590  "collversion, ");
13591  else
13592  appendPQExpBufferStr(query,
13593  "'c' AS collprovider, "
13594  "NULL AS collversion, ");
13595 
13596  if (fout->remoteVersion >= 120000)
13597  appendPQExpBufferStr(query,
13598  "collisdeterministic, ");
13599  else
13600  appendPQExpBufferStr(query,
13601  "true AS collisdeterministic, ");
13602 
13603  appendPQExpBuffer(query,
13604  "collcollate, "
13605  "collctype "
13606  "FROM pg_catalog.pg_collation c "
13607  "WHERE c.oid = '%u'::pg_catalog.oid",
13608  collinfo->dobj.catId.oid);
13609 
13610  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13611 
13612  i_collprovider = PQfnumber(res, "collprovider");
13613  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13614  i_collcollate = PQfnumber(res, "collcollate");
13615  i_collctype = PQfnumber(res, "collctype");
13616 
13617  collprovider = PQgetvalue(res, 0, i_collprovider);
13618  collcollate = PQgetvalue(res, 0, i_collcollate);
13619  collctype = PQgetvalue(res, 0, i_collctype);
13620 
13621  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13622  fmtQualifiedDumpable(collinfo));
13623 
13624  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13625  fmtQualifiedDumpable(collinfo));
13626 
13627  appendPQExpBufferStr(q, "provider = ");
13628  if (collprovider[0] == 'c')
13629  appendPQExpBufferStr(q, "libc");
13630  else if (collprovider[0] == 'i')
13631  appendPQExpBufferStr(q, "icu");
13632  else if (collprovider[0] == 'd')
13633  /* to allow dumping pg_catalog; not accepted on input */
13634  appendPQExpBufferStr(q, "default");
13635  else
13636  fatal("unrecognized collation provider: %s",
13637  collprovider);
13638 
13639  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13640  appendPQExpBufferStr(q, ", deterministic = false");
13641 
13642  if (strcmp(collcollate, collctype) == 0)
13643  {
13644  appendPQExpBufferStr(q, ", locale = ");
13645  appendStringLiteralAH(q, collcollate, fout);
13646  }
13647  else
13648  {
13649  appendPQExpBufferStr(q, ", lc_collate = ");
13650  appendStringLiteralAH(q, collcollate, fout);
13651  appendPQExpBufferStr(q, ", lc_ctype = ");
13652  appendStringLiteralAH(q, collctype, fout);
13653  }
13654 
13655  /*
13656  * For binary upgrade, carry over the collation version. For normal
13657  * dump/restore, omit the version, so that it is computed upon restore.
13658  */
13659  if (dopt->binary_upgrade)
13660  {
13661  int i_collversion;
13662 
13663  i_collversion = PQfnumber(res, "collversion");
13664  if (!PQgetisnull(res, 0, i_collversion))
13665  {
13666  appendPQExpBufferStr(q, ", version = ");
13668  PQgetvalue(res, 0, i_collversion),
13669  fout);
13670  }
13671  }
13672 
13673  appendPQExpBufferStr(q, ");\n");
13674 
13675  if (dopt->binary_upgrade)
13676  binary_upgrade_extension_member(q, &collinfo->dobj,
13677  "COLLATION", qcollname,
13678  collinfo->dobj.namespace->dobj.name);
13679 
13680  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13681  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13682  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13683  .namespace = collinfo->dobj.namespace->dobj.name,
13684  .owner = collinfo->rolname,
13685  .description = "COLLATION",
13686  .section = SECTION_PRE_DATA,
13687  .createStmt = q->data,
13688  .dropStmt = delq->data));
13689 
13690  /* Dump Collation Comments */
13691  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13692  dumpComment(fout, "COLLATION", qcollname,
13693  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13694  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13695 
13696  PQclear(res);
13697 
13698  destroyPQExpBuffer(query);
13699  destroyPQExpBuffer(q);
13700  destroyPQExpBuffer(delq);
13701  free(qcollname);
13702 }
#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:311
char * rolname
Definition: pg_dump.h:245
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:190
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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:302
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:4642
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:9678
int remoteVersion
Definition: pg_backup.h:195

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

9681 {
9682  DumpOptions *dopt = fout->dopt;
9683  CommentItem *comments;
9684  int ncomments;
9685 
9686  /* do nothing, if --no-comments is supplied */
9687  if (dopt->no_comments)
9688  return;
9689 
9690  /* Comments are schema not data ... except blob comments are data */
9691  if (strcmp(type, "LARGE OBJECT") != 0)
9692  {
9693  if (dopt->dataOnly)
9694  return;
9695  }
9696  else
9697  {
9698  /* We do dump blob comments in binary-upgrade mode */
9699  if (dopt->schemaOnly && !dopt->binary_upgrade)
9700  return;
9701  }
9702 
9703  /* Search for comments associated with catalogId, using table */
9704  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9705  &comments);
9706 
9707  /* Is there one matching the subid? */
9708  while (ncomments > 0)
9709  {
9710  if (comments->objsubid == subid)
9711  break;
9712  comments++;
9713  ncomments--;
9714  }
9715 
9716  /* If a comment exists, build COMMENT ON statement */
9717  if (ncomments > 0)
9718  {
9719  PQExpBuffer query = createPQExpBuffer();
9721 
9722  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9723  if (namespace && *namespace)
9724  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9725  appendPQExpBuffer(query, "%s IS ", name);
9726  appendStringLiteralAH(query, comments->descr, fout);
9727  appendPQExpBufferStr(query, ";\n");
9728 
9729  appendPQExpBuffer(tag, "%s %s", type, name);
9730 
9731  /*
9732  * We mark comments as SECTION_NONE because they really belong in the
9733  * same section as their parent, whether that is pre-data or
9734  * post-data.
9735  */
9737  ARCHIVE_OPTS(.tag = tag->data,
9738  .namespace = namespace,
9739  .owner = owner,
9740  .description = "COMMENT",
9741  .section = SECTION_NONE,
9742  .createStmt = query->data,
9743  .deps = &dumpId,
9744  .nDeps = 1));
9745 
9746  destroyPQExpBuffer(query);
9747  destroyPQExpBuffer(tag);
9748  }
9749 }
#define ARCHIVE_OPTS(...)
Oid tableoid
Definition: pg_backup.h:238
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
bool schemaOnly
Definition: pg_backup.h:146
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:71
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9857
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:190
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:147
const char * descr
Definition: pg_dump.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:302
int no_comments
Definition: pg_backup.h:157
const char * name
Definition: encode.c:561
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 11161 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, InvalidDumpId, _dumpableObject::name, CatalogId::oid, OidIsValid, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), Archive::remoteVersion, _typeInfo::rolname, _typeInfo::rtypacl, SECTION_PRE_DATA, _typeInfo::typacl, and _typeInfo::typrelid.

Referenced by dumpType().

11162 {
11163  DumpOptions *dopt = fout->dopt;
11165  PQExpBuffer dropped = createPQExpBuffer();
11166  PQExpBuffer delq = createPQExpBuffer();
11167  PQExpBuffer query = createPQExpBuffer();
11168  PGresult *res;
11169  char *qtypname;
11170  char *qualtypname;
11171  int ntups;
11172  int i_attname;
11173  int i_atttypdefn;
11174  int i_attlen;
11175  int i_attalign;
11176  int i_attisdropped;
11177  int i_attcollation;
11178  int i;
11179  int actual_atts;
11180 
11181  /* Fetch type specific details */
11182  if (fout->remoteVersion >= 90100)
11183  {
11184  /*
11185  * attcollation is new in 9.1. Since we only want to dump COLLATE
11186  * clauses for attributes whose collation is different from their
11187  * type's default, we use a CASE here to suppress uninteresting
11188  * attcollations cheaply. atttypid will be 0 for dropped columns;
11189  * collation does not matter for those.
11190  */
11191  appendPQExpBuffer(query, "SELECT a.attname, "
11192  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11193  "a.attlen, a.attalign, a.attisdropped, "
11194  "CASE WHEN a.attcollation <> at.typcollation "
11195  "THEN a.attcollation ELSE 0 END AS attcollation "
11196  "FROM pg_catalog.pg_type ct "
11197  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11198  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11199  "WHERE ct.oid = '%u'::pg_catalog.oid "
11200  "ORDER BY a.attnum ",
11201  tyinfo->dobj.catId.oid);
11202  }
11203  else
11204  {
11205  /*
11206  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
11207  * should always be false.
11208  */
11209  appendPQExpBuffer(query, "SELECT a.attname, "
11210  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11211  "a.attlen, a.attalign, a.attisdropped, "
11212  "0 AS attcollation "
11213  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
11214  "WHERE ct.oid = '%u'::pg_catalog.oid "
11215  "AND a.attrelid = ct.typrelid "
11216  "ORDER BY a.attnum ",
11217  tyinfo->dobj.catId.oid);
11218  }
11219 
11220  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11221 
11222  ntups = PQntuples(res);
11223 
11224  i_attname = PQfnumber(res, "attname");
11225  i_atttypdefn = PQfnumber(res, "atttypdefn");
11226  i_attlen = PQfnumber(res, "attlen");
11227  i_attalign = PQfnumber(res, "attalign");
11228  i_attisdropped = PQfnumber(res, "attisdropped");
11229  i_attcollation = PQfnumber(res, "attcollation");
11230 
11231  if (dopt->binary_upgrade)
11232  {
11234  tyinfo->dobj.catId.oid,
11235  false);
11236  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11237  }
11238 
11239  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11240  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11241 
11242  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11243  qualtypname);
11244 
11245  actual_atts = 0;
11246  for (i = 0; i < ntups; i++)
11247  {
11248  char *attname;
11249  char *atttypdefn;
11250  char *attlen;
11251  char *attalign;
11252  bool attisdropped;
11253  Oid attcollation;
11254 
11255  attname = PQgetvalue(res, i, i_attname);
11256  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11257  attlen = PQgetvalue(res, i, i_attlen);
11258  attalign = PQgetvalue(res, i, i_attalign);
11259  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11260  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11261 
11262  if (attisdropped && !dopt->binary_upgrade)
11263  continue;
11264 
11265  /* Format properly if not first attr */
11266  if (actual_atts++ > 0)
11267  appendPQExpBufferChar(q, ',');
11268  appendPQExpBufferStr(q, "\n\t");
11269 
11270  if (!attisdropped)
11271  {
11272  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11273 
11274  /* Add collation if not default for the column type */
11275  if (OidIsValid(attcollation))
11276  {
11277  CollInfo *coll;
11278 
11279  coll = findCollationByOid(attcollation);
11280  if (coll)
11281  appendPQExpBuffer(q, " COLLATE %s",
11282  fmtQualifiedDumpable(coll));
11283  }
11284  }
11285  else
11286  {
11287  /*
11288  * This is a dropped attribute and we're in binary_upgrade mode.
11289  * Insert a placeholder for it in the CREATE TYPE command, and set
11290  * length and alignment with direct UPDATE to the catalogs
11291  * afterwards. See similar code in dumpTableSchema().
11292  */
11293  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11294 
11295  /* stash separately for insertion after the CREATE TYPE */
11296  appendPQExpBufferStr(dropped,
11297  "\n-- For binary upgrade, recreate dropped column.\n");
11298  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11299  "SET attlen = %s, "
11300  "attalign = '%s', attbyval = false\n"
11301  "WHERE attname = ", attlen, attalign);
11302  appendStringLiteralAH(dropped, attname, fout);
11303  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11304  appendStringLiteralAH(dropped, qualtypname, fout);
11305  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11306 
11307  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11308  qualtypname);
11309  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11310  fmtId(attname));
11311  }
11312  }
11313  appendPQExpBufferStr(q, "\n);\n");
11314  appendPQExpBufferStr(q, dropped->data);
11315 
11316  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11317 
11318  if (dopt->binary_upgrade)
11320  "TYPE", qtypname,
11321  tyinfo->dobj.namespace->dobj.name);
11322 
11323  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11324  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11325  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11326  .namespace = tyinfo->dobj.namespace->dobj.name,
11327  .owner = tyinfo->rolname,
11328  .description = "TYPE",
11329  .section = SECTION_PRE_DATA,
11330  .createStmt = q->data,
11331  .dropStmt = delq->data));
11332 
11333 
11334  /* Dump Type Comments and Security Labels */
11335  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11336  dumpComment(fout, "TYPE", qtypname,
11337  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11338  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11339 
11340  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11341  dumpSecLabel(fout, "TYPE", qtypname,
11342  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11343  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11344 
11345  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11346  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11347  qtypname, NULL,
11348  tyinfo->dobj.namespace->dobj.name,
11349  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11350  tyinfo->inittypacl, tyinfo->initrtypacl);
11351 
11352  PQclear(res);
11353  destroyPQExpBuffer(q);
11354  destroyPQExpBuffer(dropped);
11355  destroyPQExpBuffer(delq);
11356  destroyPQExpBuffer(query);
11357  free(qtypname);
11358  free(qualtypname);
11359 
11360  /* Dump any per-column comments */
11361  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11362  dumpCompositeTypeColComments(fout, tyinfo);
11363 }
#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:11371
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:14996
#define OidIsValid(objectId)
Definition: c.h:652
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:190
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:4559
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:4463
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define InvalidDumpId
Definition: pg_backup.h:244
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:302
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:876
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14897
#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:4642
#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:9678
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:296
int remoteVersion
Definition: pg_backup.h:195

◆ dumpCompositeTypeColComments()

static void dumpCompositeTypeColComments ( Archive fout,
TypeInfo tyinfo 
)
static

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

11372 {
11373  CommentItem *comments;
11374  int ncomments;
11375  PGresult *res;
11376  PQExpBuffer query;
11377  PQExpBuffer target;
11378  Oid pgClassOid;
11379  int i;
11380  int ntups;
11381  int i_attname;
11382  int i_attnum;
11383 
11384  /* do nothing, if --no-comments is supplied */
11385  if (fout->dopt->no_comments)
11386  return;
11387 
11388  query = createPQExpBuffer();
11389 
11390  appendPQExpBuffer(query,
11391  "SELECT c.tableoid, a.attname, a.attnum "
11392  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
11393  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
11394  " AND NOT a.attisdropped "
11395  "ORDER BY a.attnum ",
11396  tyinfo->typrelid);
11397 
11398  /* Fetch column attnames */
11399  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11400 
11401  ntups = PQntuples(res);
11402  if (ntups < 1)
11403  {
11404  PQclear(res);
11405  destroyPQExpBuffer(query);
11406  return;
11407  }
11408 
11409  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
11410 
11411  /* Search for comments associated with type's pg_class OID */
11412  ncomments = findComments(fout,
11413  pgClassOid,
11414  tyinfo->typrelid,
11415  &comments);
11416 
11417  /* If no comments exist, we're done */
11418  if (ncomments <= 0)
11419  {
11420  PQclear(res);
11421  destroyPQExpBuffer(query);
11422  return;
11423  }
11424 
11425  /* Build COMMENT ON statements */
11426  target = createPQExpBuffer();
11427 
11428  i_attnum = PQfnumber(res, "attnum");
11429  i_attname = PQfnumber(res, "attname");
11430  while (ncomments > 0)
11431  {
11432  const char *attname;
11433 
11434  attname = NULL;
11435  for (i = 0; i < ntups; i++)
11436  {
11437  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
11438  {
11439  attname = PQgetvalue(res, i, i_attname);
11440  break;
11441  }
11442  }
11443  if (attname) /* just in case we don't find it */
11444  {
11445  const char *descr = comments->descr;
11446 
11447  resetPQExpBuffer(target);
11448  appendPQExpBuffer(target, "COLUMN %s.",
11449  fmtId(tyinfo->dobj.name));
11450  appendPQExpBufferStr(target, fmtId(attname));
11451 
11452  resetPQExpBuffer(query);
11453  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11454  fmtQualifiedDumpable(tyinfo));
11455  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11456  appendStringLiteralAH(query, descr, fout);
11457  appendPQExpBufferStr(query, ";\n");
11458 
11460  ARCHIVE_OPTS(.tag = target->data,
11461  .namespace = tyinfo->dobj.namespace->dobj.name,
11462  .owner = tyinfo->rolname,
11463  .description = "COMMENT",
11464  .section = SECTION_NONE,
11465  .createStmt = query->data,
11466  .deps = &(tyinfo->dobj.dumpId),
11467  .nDeps = 1));
11468  }
11469 
11470  comments++;
11471  ncomments--;
11472  }
11473 
11474  PQclear(res);
11475  destroyPQExpBuffer(query);
11476  destroyPQExpBuffer(target);
11477 }
#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:9857
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
DumpOptions * dopt
Definition: pg_backup.h:190
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:302
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:157
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:296
DumpId createDumpId(void)
Definition: common.c:590

◆ dumpConstraint()

static void dumpConstraint ( Archive fout,
ConstraintInfo coninfo 
)
static

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

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

13710 {
13711  DumpOptions *dopt = fout->dopt;
13712  PQExpBuffer query;
13713  PQExpBuffer q;
13714  PQExpBuffer delq;
13715  char *qconvname;
13716  PGresult *res;
13717  int i_conforencoding;
13718  int i_contoencoding;
13719  int i_conproc;
13720  int i_condefault;
13721  const char *conforencoding;
13722  const char *contoencoding;
13723  const char *conproc;
13724  bool condefault;
13725 
13726  /* Skip if not to be dumped */
13727  if (!convinfo->dobj.dump || dopt->dataOnly)
13728  return;
13729 
13730  query = createPQExpBuffer();
13731  q = createPQExpBuffer();
13732  delq = createPQExpBuffer();
13733 
13734  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13735 
13736  /* Get conversion-specific details */
13737  appendPQExpBuffer(query, "SELECT "
13738  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13739  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13740  "conproc, condefault "
13741  "FROM pg_catalog.pg_conversion c "
13742  "WHERE c.oid = '%u'::pg_catalog.oid",
13743  convinfo->dobj.catId.oid);
13744 
13745  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13746 
13747  i_conforencoding = PQfnumber(res, "conforencoding");
13748  i_contoencoding = PQfnumber(res, "contoencoding");
13749  i_conproc = PQfnumber(res, "conproc");
13750  i_condefault = PQfnumber(res, "condefault");
13751 
13752  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13753  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13754  conproc = PQgetvalue(res, 0, i_conproc);
13755  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13756 
13757  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13758  fmtQualifiedDumpable(convinfo));
13759 
13760  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13761  (condefault) ? "DEFAULT " : "",
13762  fmtQualifiedDumpable(convinfo));
13763  appendStringLiteralAH(q, conforencoding, fout);
13764  appendPQExpBufferStr(q, " TO ");
13765  appendStringLiteralAH(q, contoencoding, fout);
13766  /* regproc output is already sufficiently quoted */
13767  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13768 
13769  if (dopt->binary_upgrade)
13770  binary_upgrade_extension_member(q, &convinfo->dobj,
13771  "CONVERSION", qconvname,
13772  convinfo->dobj.namespace->dobj.name);
13773 
13774  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13775  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13776  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13777  .namespace = convinfo->dobj.namespace->dobj.name,
13778  .owner = convinfo->rolname,
13779  .description = "CONVERSION",
13780  .section = SECTION_PRE_DATA,
13781  .createStmt = q->data,
13782  .dropStmt = delq->data));
13783 
13784  /* Dump Conversion Comments */
13785  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13786  dumpComment(fout, "CONVERSION", qconvname,
13787  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13788  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13789 
13790  PQclear(res);
13791 
13792  destroyPQExpBuffer(query);
13793  destroyPQExpBuffer(q);
13794  destroyPQExpBuffer(delq);
13795  free(qconvname);
13796 }
#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 * rolname
Definition: pg_dump.h:251
DumpId dumpId
Definition: pg_dump.h:129
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:311
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143