PostgreSQL Source Code  git master
pg_dump.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include "access/attnum.h"
#include "access/sysattr.h"
#include "access/transam.h"
#include "catalog/pg_aggregate_d.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_attribute_d.h"
#include "catalog/pg_authid_d.h"
#include "catalog/pg_cast_d.h"
#include "catalog/pg_class_d.h"
#include "catalog/pg_default_acl_d.h"
#include "catalog/pg_largeobject_d.h"
#include "catalog/pg_largeobject_metadata_d.h"
#include "catalog/pg_proc_d.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_trigger_d.h"
#include "catalog/pg_type_d.h"
#include "common/connect.h"
#include "dumputils.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
#include "getopt_long.h"
#include "libpq/libpq-fs.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "storage/block.h"
Include dependency graph for pg_dump.c:

Go to the source code of this file.

Data Structures

struct  CommentItem
 
struct  SecLabelItem
 

Macros

#define DUMP_DEFAULT_ROWS_PER_INSERT   1
 
#define fmtQualifiedDumpable(obj)
 

Typedefs

typedef enum OidOptions OidOptions
 

Enumerations

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

Functions

static void help (const char *progname)
 
static void setup_connection (Archive *AH, const char *dumpencoding, const char *dumpsnapshot, char *use_role)
 
static ArchiveFormat parseArchiveFormat (const char *format, ArchiveMode *mode)
 
static void expand_schema_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_extension_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static void expand_foreign_server_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids)
 
static void expand_table_name_patterns (Archive *fout, SimpleStringList *patterns, SimpleOidList *oids, bool strict_names)
 
static NamespaceInfofindNamespace (Oid nsoid)
 
static void dumpTableData (Archive *fout, const TableDataInfo *tdinfo)
 
static void refreshMatViewData (Archive *fout, const TableDataInfo *tdinfo)
 
static void guessConstraintInheritance (TableInfo *tblinfo, int numTables)
 
static void dumpCommentExtended (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId, const char *initdb_comment)
 
static void dumpComment (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
 
static int findComments (Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
 
static int collectComments (Archive *fout, CommentItem **items)
 
static void dumpSecLabel (Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
 
static int findSecLabels (Archive *fout, Oid classoid, Oid objoid, SecLabelItem **items)
 
static int collectSecLabels (Archive *fout, SecLabelItem **items)
 
static void dumpDumpableObject (Archive *fout, const DumpableObject *dobj)
 
static void dumpNamespace (Archive *fout, const NamespaceInfo *nspinfo)
 
static void dumpExtension (Archive *fout, const ExtensionInfo *extinfo)
 
static void dumpType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpBaseType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpEnumType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpRangeType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpUndefinedType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpDomain (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpCompositeType (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpCompositeTypeColComments (Archive *fout, const TypeInfo *tyinfo)
 
static void dumpShellType (Archive *fout, const ShellTypeInfo *stinfo)
 
static void dumpProcLang (Archive *fout, const ProcLangInfo *plang)
 
static void dumpFunc (Archive *fout, const FuncInfo *finfo)
 
static void dumpCast (Archive *fout, const CastInfo *cast)
 
static void dumpTransform (Archive *fout, const TransformInfo *transform)
 
static void dumpOpr (Archive *fout, const OprInfo *oprinfo)
 
static void dumpAccessMethod (Archive *fout, const AccessMethodInfo *oprinfo)
 
static void dumpOpclass (Archive *fout, const OpclassInfo *opcinfo)
 
static void dumpOpfamily (Archive *fout, const OpfamilyInfo *opfinfo)
 
static void dumpCollation (Archive *fout, const CollInfo *collinfo)
 
static void dumpConversion (Archive *fout, const ConvInfo *convinfo)
 
static void dumpRule (Archive *fout, const RuleInfo *rinfo)
 
static void dumpAgg (Archive *fout, const AggInfo *agginfo)
 
static void dumpTrigger (Archive *fout, const TriggerInfo *tginfo)
 
static void dumpEventTrigger (Archive *fout, const EventTriggerInfo *evtinfo)
 
static void dumpTable (Archive *fout, const TableInfo *tbinfo)
 
static void dumpTableSchema (Archive *fout, const TableInfo *tbinfo)
 
static void dumpTableAttach (Archive *fout, const TableAttachInfo *tbinfo)
 
static void dumpAttrDef (Archive *fout, const AttrDefInfo *adinfo)
 
static void dumpSequence (Archive *fout, const TableInfo *tbinfo)
 
static void dumpSequenceData (Archive *fout, const TableDataInfo *tdinfo)
 
static void dumpIndex (Archive *fout, const IndxInfo *indxinfo)
 
static void dumpIndexAttach (Archive *fout, const IndexAttachInfo *attachinfo)
 
static void dumpStatisticsExt (Archive *fout, const StatsExtInfo *statsextinfo)
 
static void dumpConstraint (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTableConstraintComment (Archive *fout, const ConstraintInfo *coninfo)
 
static void dumpTSParser (Archive *fout, const TSParserInfo *prsinfo)
 
static void dumpTSDictionary (Archive *fout, const TSDictInfo *dictinfo)
 
static void dumpTSTemplate (Archive *fout, const TSTemplateInfo *tmplinfo)
 
static void dumpTSConfig (Archive *fout, const TSConfigInfo *cfginfo)
 
static void dumpForeignDataWrapper (Archive *fout, const FdwInfo *fdwinfo)
 
static void dumpForeignServer (Archive *fout, const ForeignServerInfo *srvinfo)
 
static void dumpUserMappings (Archive *fout, const char *servername, const char *namespace, const char *owner, CatalogId catalogId, DumpId dumpId)
 
static void dumpDefaultACL (Archive *fout, const DefaultACLInfo *daclinfo)
 
static DumpId dumpACL (Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
 
static void getDependencies (Archive *fout)
 
static void BuildArchiveDependencies (Archive *fout)
 
static void findDumpableDependencies (ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
 
static DumpableObjectcreateBoundaryObjects (void)
 
static void addBoundaryDependencies (DumpableObject **dobjs, int numObjs, DumpableObject *boundaryObjs)
 
static void addConstrChildIdxDeps (DumpableObject *dobj, const IndxInfo *refidx)
 
static void getDomainConstraints (Archive *fout, TypeInfo *tyinfo)
 
static void getTableData (DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind)
 
static void makeTableDataInfo (DumpOptions *dopt, TableInfo *tbinfo)
 
static void buildMatViewRefreshDependencies (Archive *fout)
 
static void getTableDataFKConstraints (void)
 
static char * format_function_arguments (const FuncInfo *finfo, const char *funcargs, bool is_agg)
 
static char * format_function_arguments_old (Archive *fout, const FuncInfo *finfo, int nallargs, char **allargtypes, char **argmodes, char **argnames)
 
static char * format_function_signature (Archive *fout, const FuncInfo *finfo, bool honor_quotes)
 
static char * convertRegProcReference (const char *proc)
 
static char * getFormattedOperatorName (const char *oproid)
 
static char * convertTSFunction (Archive *fout, Oid funcOid)
 
static Oid findLastBuiltinOid_V71 (Archive *fout)
 
static char * getFormattedTypeName (Archive *fout, Oid oid, OidOptions opts)
 
static void getBlobs (Archive *fout)
 
static void dumpBlob (Archive *fout, const BlobInfo *binfo)
 
static int dumpBlobs (Archive *fout, const void *arg)
 
static void dumpPolicy (Archive *fout, const PolicyInfo *polinfo)
 
static void dumpPublication (Archive *fout, const PublicationInfo *pubinfo)
 
static void dumpPublicationTable (Archive *fout, const PublicationRelInfo *pubrinfo)
 
static void dumpSubscription (Archive *fout, const SubscriptionInfo *subinfo)
 
static void dumpDatabase (Archive *AH)
 
static void dumpDatabaseConfig (Archive *AH, PQExpBuffer outbuf, const char *dbname, Oid dboid)
 
static void dumpEncoding (Archive *AH)
 
static void dumpStdStrings (Archive *AH)
 
static void dumpSearchPath (Archive *AH)
 
static void binary_upgrade_set_type_oids_by_type_oid (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type, bool include_multirange_type)
 
static void binary_upgrade_set_type_oids_by_rel_oid (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_rel_oid)
 
static void binary_upgrade_set_pg_class_oids (Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
 
static void binary_upgrade_extension_member (PQExpBuffer upgrade_buffer, const DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
 
static const char * getAttrName (int attrnum, const TableInfo *tblInfo)
 
static const char * fmtCopyColumnList (const TableInfo *ti, PQExpBuffer buffer)
 
static bool nonemptyReloptions (const char *reloptions)
 
static void appendReloptionsArrayAH (PQExpBuffer buffer, const char *reloptions, const char *prefix, Archive *fout)
 
static char * get_synchronized_snapshot (Archive *fout)
 
static void setupDumpWorker (Archive *AHX)
 
static TableInfogetRootTableInfo (const TableInfo *tbinfo)
 
int main (int argc, char **argv)
 
static bool checkExtensionMembership (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableNamespace (NamespaceInfo *nsinfo, Archive *fout)
 
static void selectDumpableTable (TableInfo *tbinfo, Archive *fout)
 
static void selectDumpableType (TypeInfo *tyinfo, Archive *fout)
 
static void selectDumpableDefaultACL (DefaultACLInfo *dinfo, DumpOptions *dopt)
 
static void selectDumpableCast (CastInfo *cast, Archive *fout)
 
static void selectDumpableProcLang (ProcLangInfo *plang, Archive *fout)
 
static void selectDumpableAccessMethod (AccessMethodInfo *method, Archive *fout)
 
static void selectDumpableExtension (ExtensionInfo *extinfo, DumpOptions *dopt)
 
static void selectDumpablePublicationTable (DumpableObject *dobj, Archive *fout)
 
static void selectDumpableObject (DumpableObject *dobj, Archive *fout)
 
static int dumpTableData_copy (Archive *fout, const void *dcontext)
 
static int dumpTableData_insert (Archive *fout, const void *dcontext)
 
void getPolicies (Archive *fout, TableInfo tblinfo[], int numTables)
 
PublicationInfogetPublications (Archive *fout, int *numPublications)
 
void getPublicationTables (Archive *fout, TableInfo tblinfo[], int numTables)
 
static bool is_superuser (Archive *fout)
 
void getSubscriptions (Archive *fout)
 
static void append_depends_on_extension (Archive *fout, PQExpBuffer create, const DumpableObject *dobj, const char *catalog, const char *keyword, const char *objname)
 
static Oid get_next_possible_free_pg_type_oid (Archive *fout, PQExpBuffer upgrade_query)
 
NamespaceInfogetNamespaces (Archive *fout, int *numNamespaces)
 
ExtensionInfogetExtensions (Archive *fout, int *numExtensions)
 
TypeInfogetTypes (Archive *fout, int *numTypes)
 
OprInfogetOperators (Archive *fout, int *numOprs)
 
CollInfogetCollations (Archive *fout, int *numCollations)
 
ConvInfogetConversions (Archive *fout, int *numConversions)
 
AccessMethodInfogetAccessMethods (Archive *fout, int *numAccessMethods)
 
OpclassInfogetOpclasses (Archive *fout, int *numOpclasses)
 
OpfamilyInfogetOpfamilies (Archive *fout, int *numOpfamilies)
 
AggInfogetAggregates (Archive *fout, int *numAggs)
 
FuncInfogetFuncs (Archive *fout, int *numFuncs)
 
TableInfogetTables (Archive *fout, int *numTables)
 
void getOwnedSeqs (Archive *fout, TableInfo tblinfo[], int numTables)
 
InhInfogetInherits (Archive *fout, int *numInherits)
 
void getIndexes (Archive *fout, TableInfo tblinfo[], int numTables)
 
void getExtendedStatistics (Archive *fout)
 
void getConstraints (Archive *fout, TableInfo tblinfo[], int numTables)
 
RuleInfogetRules (Archive *fout, int *numRules)
 
void getTriggers (Archive *fout, TableInfo tblinfo[], int numTables)
 
EventTriggerInfogetEventTriggers (Archive *fout, int *numEventTriggers)
 
ProcLangInfogetProcLangs (Archive *fout, int *numProcLangs)
 
CastInfogetCasts (Archive *fout, int *numCasts)
 
static char * get_language_name (Archive *fout, Oid langid)
 
TransformInfogetTransforms (Archive *fout, int *numTransforms)
 
void getTableAttrs (Archive *fout, TableInfo *tblinfo, int numTables)
 
bool shouldPrintColumn (const DumpOptions *dopt, const TableInfo *tbinfo, int colno)
 
TSParserInfogetTSParsers (Archive *fout, int *numTSParsers)
 
TSDictInfogetTSDictionaries (Archive *fout, int *numTSDicts)
 
TSTemplateInfogetTSTemplates (Archive *fout, int *numTSTemplates)
 
TSConfigInfogetTSConfigurations (Archive *fout, int *numTSConfigs)
 
FdwInfogetForeignDataWrappers (Archive *fout, int *numForeignDataWrappers)
 
ForeignServerInfogetForeignServers (Archive *fout, int *numForeignServers)
 
DefaultACLInfogetDefaultACLs (Archive *fout, int *numDefaultACLs)
 
static void dumpTableComment (Archive *fout, const TableInfo *tbinfo, const char *reltypename)
 
static char * format_aggregate_signature (const AggInfo *agginfo, Archive *fout, bool honor_quotes)
 
static void dumpTableSecLabel (Archive *fout, const TableInfo *tbinfo, const char *reltypename)
 
static PQExpBuffer createViewAsClause (Archive *fout, const TableInfo *tbinfo)
 
static PQExpBuffer createDummyViewAsClause (Archive *fout, const TableInfo *tbinfo)
 
void getExtensionMembership (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 
void processExtensionTables (Archive *fout, ExtensionInfo extinfo[], int numExtensions)
 

Variables

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

Macro Definition Documentation

◆ DUMP_DEFAULT_ROWS_PER_INSERT

#define DUMP_DEFAULT_ROWS_PER_INSERT   1

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

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

Function Documentation

◆ addBoundaryDependencies()

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

Definition at line 18633 of file pg_dump.c.

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

Referenced by main().

18635 {
18636  DumpableObject *preDataBound = boundaryObjs + 0;
18637  DumpableObject *postDataBound = boundaryObjs + 1;
18638  int i;
18639 
18640  for (i = 0; i < numObjs; i++)
18641  {
18642  DumpableObject *dobj = dobjs[i];
18643 
18644  /*
18645  * The classification of object types here must match the SECTION_xxx
18646  * values assigned during subsequent ArchiveEntry calls!
18647  */
18648  switch (dobj->objType)
18649  {
18650  case DO_NAMESPACE:
18651  case DO_EXTENSION:
18652  case DO_TYPE:
18653  case DO_SHELL_TYPE:
18654  case DO_FUNC:
18655  case DO_AGG:
18656  case DO_OPERATOR:
18657  case DO_ACCESS_METHOD:
18658  case DO_OPCLASS:
18659  case DO_OPFAMILY:
18660  case DO_COLLATION:
18661  case DO_CONVERSION:
18662  case DO_TABLE:
18663  case DO_TABLE_ATTACH:
18664  case DO_ATTRDEF:
18665  case DO_PROCLANG:
18666  case DO_CAST:
18667  case DO_DUMMY_TYPE:
18668  case DO_TSPARSER:
18669  case DO_TSDICT:
18670  case DO_TSTEMPLATE:
18671  case DO_TSCONFIG:
18672  case DO_FDW:
18673  case DO_FOREIGN_SERVER:
18674  case DO_TRANSFORM:
18675  case DO_BLOB:
18676  /* Pre-data objects: must come before the pre-data boundary */
18677  addObjectDependency(preDataBound, dobj->dumpId);
18678  break;
18679  case DO_TABLE_DATA:
18680  case DO_SEQUENCE_SET:
18681  case DO_BLOB_DATA:
18682  /* Data objects: must come between the boundaries */
18683  addObjectDependency(dobj, preDataBound->dumpId);
18684  addObjectDependency(postDataBound, dobj->dumpId);
18685  break;
18686  case DO_INDEX:
18687  case DO_INDEX_ATTACH:
18688  case DO_STATSEXT:
18689  case DO_REFRESH_MATVIEW:
18690  case DO_TRIGGER:
18691  case DO_EVENT_TRIGGER:
18692  case DO_DEFAULT_ACL:
18693  case DO_POLICY:
18694  case DO_PUBLICATION:
18695  case DO_PUBLICATION_REL:
18696  case DO_SUBSCRIPTION:
18697  /* Post-data objects: must come after the post-data boundary */
18698  addObjectDependency(dobj, postDataBound->dumpId);
18699  break;
18700  case DO_RULE:
18701  /* Rules are post-data, but only if dumped separately */
18702  if (((RuleInfo *) dobj)->separate)
18703  addObjectDependency(dobj, postDataBound->dumpId);
18704  break;
18705  case DO_CONSTRAINT:
18706  case DO_FK_CONSTRAINT:
18707  /* Constraints are post-data, but only if dumped separately */
18708  if (((ConstraintInfo *) dobj)->separate)
18709  addObjectDependency(dobj, postDataBound->dumpId);
18710  break;
18711  case DO_PRE_DATA_BOUNDARY:
18712  /* nothing to do */
18713  break;
18714  case DO_POST_DATA_BOUNDARY:
18715  /* must come after the pre-data boundary */
18716  addObjectDependency(dobj, preDataBound->dumpId);
18717  break;
18718  }
18719  }
18720 }
DumpId dumpId
Definition: pg_dump.h:130
Definition: pg_dump.h:45
Definition: pg_dump.h:71
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:846
int i
DumpableObjectType objType
Definition: pg_dump.h:128

◆ addConstrChildIdxDeps()

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

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

7731 {
7732  SimplePtrListCell *cell;
7733 
7734  Assert(dobj->objType == DO_FK_CONSTRAINT);
7735 
7736  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7737  {
7738  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7739 
7740  addObjectDependency(dobj, attach->dobj.dumpId);
7741 
7742  if (attach->partitionIdx->partattaches.head != NULL)
7743  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7744  }
7745 }
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7730
DumpId dumpId
Definition: pg_dump.h:130
DumpableObject dobj
Definition: pg_dump.h:392
IndxInfo * partitionIdx
Definition: pg_dump.h:394
SimplePtrList partattaches
Definition: pg_dump.h:384
struct SimplePtrListCell * next
Definition: simple_list.h:48
#define Assert(condition)
Definition: c.h:804
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:846
SimplePtrListCell * head
Definition: simple_list.h:54
DumpableObjectType objType
Definition: pg_dump.h:128

◆ append_depends_on_extension()

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

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

4516 {
4517  if (dobj->depends_on_ext)
4518  {
4519  char *nm;
4520  PGresult *res;
4521  PQExpBuffer query;
4522  int ntups;
4523  int i_extname;
4524  int i;
4525 
4526  /* dodge fmtId() non-reentrancy */
4527  nm = pg_strdup(objname);
4528 
4529  query = createPQExpBuffer();
4530  appendPQExpBuffer(query,
4531  "SELECT e.extname "
4532  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4533  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4534  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4535  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4536  catalog,
4537  dobj->catId.oid);
4538  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4539  ntups = PQntuples(res);
4540  i_extname = PQfnumber(res, "extname");
4541  for (i = 0; i < ntups; i++)
4542  {
4543  appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4544  keyword, nm,
4545  fmtId(PQgetvalue(res, i, i_extname)));
4546  }
4547 
4548  PQclear(res);
4549  destroyPQExpBuffer(query);
4550  pg_free(nm);
4551  }
4552 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
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:3356
bool depends_on_ext
Definition: pg_dump.h:136
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:129
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ appendReloptionsArrayAH()

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

Definition at line 18933 of file pg_dump.c.

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

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

18935 {
18936  bool res;
18937 
18938  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18939  fout->std_strings);
18940  if (!res)
18941  pg_log_warning("could not parse reloptions array");
18942 }
int encoding
Definition: pg_backup.h:206
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:207

◆ binary_upgrade_extension_member()

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

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

4787 {
4788  DumpableObject *extobj = NULL;
4789  int i;
4790 
4791  if (!dobj->ext_member)
4792  return;
4793 
4794  /*
4795  * Find the parent extension. We could avoid this search if we wanted to
4796  * add a link field to DumpableObject, but the space costs of that would
4797  * be considerable. We assume that member objects could only have a
4798  * direct dependency on their own extension, not any others.
4799  */
4800  for (i = 0; i < dobj->nDeps; i++)
4801  {
4802  extobj = findObjectByDumpId(dobj->dependencies[i]);
4803  if (extobj && extobj->objType == DO_EXTENSION)
4804  break;
4805  extobj = NULL;
4806  }
4807  if (extobj == NULL)
4808  fatal("could not find parent extension for %s %s",
4809  objtype, objname);
4810 
4811  appendPQExpBufferStr(upgrade_buffer,
4812  "\n-- For binary upgrade, handle extension membership the hard way\n");
4813  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4814  fmtId(extobj->name),
4815  objtype);
4816  if (objnamespace && *objnamespace)
4817  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4818  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4819 }
char * name
Definition: pg_dump.h:131
DumpId * dependencies
Definition: pg_dump.h:137
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
bool ext_member
Definition: pg_dump.h:135
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:671
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define fatal(...)
int i
DumpableObjectType objType
Definition: pg_dump.h:128

◆ binary_upgrade_set_pg_class_oids()

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

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

4702 {
4703  appendPQExpBufferStr(upgrade_buffer,
4704  "\n-- For binary upgrade, must preserve pg_class oids\n");
4705 
4706  if (!is_index)
4707  {
4708  PQExpBuffer upgrade_query = createPQExpBuffer();
4709  PGresult *upgrade_res;
4710  Oid pg_class_reltoastrelid;
4711  char pg_class_relkind;
4712  Oid pg_index_indexrelid;
4713 
4714  appendPQExpBuffer(upgrade_buffer,
4715  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4716  pg_class_oid);
4717 
4718  /*
4719  * Preserve the OIDs of the table's toast table and index, if any.
4720  * Indexes cannot have toast tables, so we need not make this probe in
4721  * the index code path.
4722  *
4723  * One complexity is that the current table definition might not
4724  * require the creation of a TOAST table, but the old database might
4725  * have a TOAST table that was created earlier, before some wide
4726  * columns were dropped. By setting the TOAST oid we force creation
4727  * of the TOAST heap and index by the new backend, so we can copy the
4728  * files during binary upgrade without worrying about this case.
4729  */
4730  appendPQExpBuffer(upgrade_query,
4731  "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
4732  "FROM pg_catalog.pg_class c LEFT JOIN "
4733  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4734  "WHERE c.oid = '%u'::pg_catalog.oid;",
4735  pg_class_oid);
4736 
4737  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4738 
4739  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
4740  PQfnumber(upgrade_res, "reltoastrelid")));
4741  pg_class_relkind = *PQgetvalue(upgrade_res, 0,
4742  PQfnumber(upgrade_res, "relkind"));
4743  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
4744  PQfnumber(upgrade_res, "indexrelid")));
4745 
4746  /*
4747  * In a pre-v12 database, partitioned tables might be marked as having
4748  * toast tables, but we should ignore them if so.
4749  */
4750  if (OidIsValid(pg_class_reltoastrelid) &&
4751  pg_class_relkind != RELKIND_PARTITIONED_TABLE)
4752  {
4753  appendPQExpBuffer(upgrade_buffer,
4754  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4755  pg_class_reltoastrelid);
4756 
4757  /* every toast table has an index */
4758  appendPQExpBuffer(upgrade_buffer,
4759  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4760  pg_index_indexrelid);
4761  }
4762 
4763  PQclear(upgrade_res);
4764  destroyPQExpBuffer(upgrade_query);
4765  }
4766  else
4767  appendPQExpBuffer(upgrade_buffer,
4768  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4769  pg_class_oid);
4770 
4771  appendPQExpBufferChar(upgrade_buffer, '\n');
4772 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3356
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 4672 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().

4675 {
4676  PQExpBuffer upgrade_query = createPQExpBuffer();
4677  PGresult *upgrade_res;
4678  Oid pg_type_oid;
4679 
4680  appendPQExpBuffer(upgrade_query,
4681  "SELECT c.reltype AS crel "
4682  "FROM pg_catalog.pg_class c "
4683  "WHERE c.oid = '%u'::pg_catalog.oid;",
4684  pg_rel_oid);
4685 
4686  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4687 
4688  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4689 
4690  if (OidIsValid(pg_type_oid))
4691  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4692  pg_type_oid, false, false);
4693 
4694  PQclear(upgrade_res);
4695  destroyPQExpBuffer(upgrade_query);
4696 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type, bool include_multirange_type)
Definition: pg_dump.c:4586
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3356
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,
bool  include_multirange_type 
)
static

Definition at line 4586 of file pg_dump.c.

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

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

4591 {
4592  PQExpBuffer upgrade_query = createPQExpBuffer();
4593  PGresult *res;
4594  Oid pg_type_array_oid;
4595  Oid pg_type_multirange_oid;
4596  Oid pg_type_multirange_array_oid;
4597 
4598  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4599  appendPQExpBuffer(upgrade_buffer,
4600  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4601  pg_type_oid);
4602 
4603  /* we only support old >= 8.3 for binary upgrades */
4604  appendPQExpBuffer(upgrade_query,
4605  "SELECT typarray "
4606  "FROM pg_catalog.pg_type "
4607  "WHERE oid = '%u'::pg_catalog.oid;",
4608  pg_type_oid);
4609 
4610  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4611 
4612  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4613 
4614  PQclear(res);
4615 
4616  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4617  pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4618 
4619  if (OidIsValid(pg_type_array_oid))
4620  {
4621  appendPQExpBufferStr(upgrade_buffer,
4622  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4623  appendPQExpBuffer(upgrade_buffer,
4624  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4625  pg_type_array_oid);
4626  }
4627 
4628  /*
4629  * Pre-set the multirange type oid and its own array type oid.
4630  */
4631  if (include_multirange_type)
4632  {
4633  if (fout->remoteVersion >= 140000)
4634  {
4635  appendPQExpBuffer(upgrade_query,
4636  "SELECT t.oid, t.typarray "
4637  "FROM pg_catalog.pg_type t "
4638  "JOIN pg_catalog.pg_range r "
4639  "ON t.oid = r.rngmultitypid "
4640  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
4641  pg_type_oid);
4642 
4643  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4644 
4645  pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
4646  pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4647 
4648  PQclear(res);
4649  }
4650  else
4651  {
4652  pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4653  pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4654  }
4655 
4656  appendPQExpBufferStr(upgrade_buffer,
4657  "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
4658  appendPQExpBuffer(upgrade_buffer,
4659  "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4660  pg_type_multirange_oid);
4661  appendPQExpBufferStr(upgrade_buffer,
4662  "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
4663  appendPQExpBuffer(upgrade_buffer,
4664  "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4665  pg_type_multirange_array_oid);
4666  }
4667 
4668  destroyPQExpBuffer(upgrade_query);
4669 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3356
static Oid get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query)
Definition: pg_dump.c:4555
void PQclear(PGresult *res)
Definition: fe-exec.c:694
int remoteVersion
Definition: pg_backup.h:196

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

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

18748 {
18749  ArchiveHandle *AH = (ArchiveHandle *) fout;
18750  TocEntry *te;
18751 
18752  /* Scan all TOC entries in the archive */
18753  for (te = AH->toc->next; te != AH->toc; te = te->next)
18754  {
18755  DumpableObject *dobj;
18756  DumpId *dependencies;
18757  int nDeps;
18758  int allocDeps;
18759 
18760  /* No need to process entries that will not be dumped */
18761  if (te->reqs == 0)
18762  continue;
18763  /* Ignore entries that already have "special" dependencies */
18764  if (te->nDeps > 0)
18765  continue;
18766  /* Otherwise, look up the item's original DumpableObject, if any */
18767  dobj = findObjectByDumpId(te->dumpId);
18768  if (dobj == NULL)
18769  continue;
18770  /* No work if it has no dependencies */
18771  if (dobj->nDeps <= 0)
18772  continue;
18773  /* Set up work array */
18774  allocDeps = 64;
18775  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18776  nDeps = 0;
18777  /* Recursively find all dumpable dependencies */
18778  findDumpableDependencies(AH, dobj,
18779  &dependencies, &nDeps, &allocDeps);
18780  /* And save 'em ... */
18781  if (nDeps > 0)
18782  {
18783  dependencies = (DumpId *) pg_realloc(dependencies,
18784  nDeps * sizeof(DumpId));
18785  te->dependencies = dependencies;
18786  te->nDeps = nDeps;
18787  }
18788  else
18789  free(dependencies);
18790  }
18791 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:243
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:671
struct _tocEntry * toc
DumpId * dependencies
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define free(a)
Definition: header.h:65
static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:18795

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

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

2548 {
2549  PQExpBuffer query;
2550  PGresult *res;
2551  int ntups,
2552  i;
2553  int i_classid,
2554  i_objid,
2555  i_refobjid;
2556 
2557  /* No Mat Views before 9.3. */
2558  if (fout->remoteVersion < 90300)
2559  return;
2560 
2561  query = createPQExpBuffer();
2562 
2563  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2564  "( "
2565  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2566  "FROM pg_depend d1 "
2567  "JOIN pg_class c1 ON c1.oid = d1.objid "
2568  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2569  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2570  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2571  "AND d2.objid = r1.oid "
2572  "AND d2.refobjid <> d1.objid "
2573  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2574  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2575  CppAsString2(RELKIND_VIEW) ") "
2576  "WHERE d1.classid = 'pg_class'::regclass "
2577  "UNION "
2578  "SELECT w.objid, d3.refobjid, c3.relkind "
2579  "FROM w "
2580  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2581  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2582  "AND d3.objid = r3.oid "
2583  "AND d3.refobjid <> w.refobjid "
2584  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2585  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2586  CppAsString2(RELKIND_VIEW) ") "
2587  ") "
2588  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2589  "FROM w "
2590  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2591 
2592  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2593 
2594  ntups = PQntuples(res);
2595 
2596  i_classid = PQfnumber(res, "classid");
2597  i_objid = PQfnumber(res, "objid");
2598  i_refobjid = PQfnumber(res, "refobjid");
2599 
2600  for (i = 0; i < ntups; i++)
2601  {
2602  CatalogId objId;
2603  CatalogId refobjId;
2604  DumpableObject *dobj;
2605  DumpableObject *refdobj;
2606  TableInfo *tbinfo;
2607  TableInfo *reftbinfo;
2608 
2609  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2610  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2611  refobjId.tableoid = objId.tableoid;
2612  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2613 
2614  dobj = findObjectByCatalogId(objId);
2615  if (dobj == NULL)
2616  continue;
2617 
2618  Assert(dobj->objType == DO_TABLE);
2619  tbinfo = (TableInfo *) dobj;
2620  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2621  dobj = (DumpableObject *) tbinfo->dataObj;
2622  if (dobj == NULL)
2623  continue;
2624  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2625 
2626  refdobj = findObjectByCatalogId(refobjId);
2627  if (refdobj == NULL)
2628  continue;
2629 
2630  Assert(refdobj->objType == DO_TABLE);
2631  reftbinfo = (TableInfo *) refdobj;
2632  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2633  refdobj = (DumpableObject *) reftbinfo->dataObj;
2634  if (refdobj == NULL)
2635  continue;
2636  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2637 
2638  addObjectDependency(dobj, refdobj->dumpId);
2639 
2640  if (!reftbinfo->relispopulated)
2641  tbinfo->relispopulated = false;
2642  }
2643 
2644  PQclear(res);
2645 
2646  destroyPQExpBuffer(query);
2647 }
char relkind
Definition: pg_dump.h:269
Oid tableoid
Definition: pg_backup.h:239
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpId dumpId
Definition: pg_dump.h:130
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:689
#define atooid(x)
Definition: postgres_ext.h:42
#define CppAsString2(x)
Definition: c.h:289
struct _tableDataInfo * dataObj
Definition: pg_dump.h:340
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3356
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:804
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:846
int i
bool relispopulated
Definition: pg_dump.h:271
DumpableObjectType objType
Definition: pg_dump.h:128
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294
int remoteVersion
Definition: pg_backup.h:196

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

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

1535 {
1536  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1537 
1538  if (ext == NULL)
1539  return false;
1540 
1541  dobj->ext_member = true;
1542 
1543  /* Record dependency so that getDependencies needn't deal with that */
1544  addObjectDependency(dobj, ext->dobj.dumpId);
1545 
1546  /*
1547  * In 9.6 and above, mark the member object to have any non-initial ACL,
1548  * policies, and security labels dumped.
1549  *
1550  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1551  * extract the information about the object. We don't provide support for
1552  * initial policies and security labels and it seems unlikely for those to
1553  * ever exist, but we may have to revisit this later.
1554  *
1555  * Prior to 9.6, we do not include any extension member components.
1556  *
1557  * In binary upgrades, we still dump all components of the members
1558  * individually, since the idea is to exactly reproduce the database
1559  * contents rather than replace the extension contents with something
1560  * different.
1561  */
1562  if (fout->dopt->binary_upgrade)
1563  dobj->dump = ext->dobj.dump;
1564  else
1565  {
1566  if (fout->remoteVersion < 90600)
1567  dobj->dump = DUMP_COMPONENT_NONE;
1568  else
1569  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1572  }
1573 
1574  return true;
1575 }
DumpableObject dobj
Definition: pg_dump.h:156
DumpComponents dump
Definition: pg_dump.h:132
DumpId dumpId
Definition: pg_dump.h:130
bool ext_member
Definition: pg_dump.h:135
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:89
int binary_upgrade
Definition: pg_backup.h:143
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:1009
DumpOptions * dopt
Definition: pg_backup.h:191
DumpComponents dump_contains
Definition: pg_dump.h:134
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:846
CatalogId catId
Definition: pg_dump.h:129
#define DUMP_COMPONENT_POLICY
Definition: pg_dump.h:95
int remoteVersion
Definition: pg_backup.h:196

◆ collectComments()

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

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

10242 {
10243  PGresult *res;
10244  PQExpBuffer query;
10245  int i_description;
10246  int i_classoid;
10247  int i_objoid;
10248  int i_objsubid;
10249  int ntups;
10250  int i;
10251  CommentItem *comments;
10252 
10253  query = createPQExpBuffer();
10254 
10255  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
10256  "FROM pg_catalog.pg_description "
10257  "ORDER BY classoid, objoid, objsubid");
10258 
10259  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10260 
10261  /* Construct lookup table containing OIDs in numeric form */
10262 
10263  i_description = PQfnumber(res, "description");
10264  i_classoid = PQfnumber(res, "classoid");
10265  i_objoid = PQfnumber(res, "objoid");
10266  i_objsubid = PQfnumber(res, "objsubid");
10267 
10268  ntups = PQntuples(res);
10269 
10270  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10271 
10272  for (i = 0; i < ntups; i++)
10273  {
10274  comments[i].descr = PQgetvalue(res, i, i_description);
10275  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
10276  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
10277  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
10278  }
10279 
10280  /* Do NOT free the PGresult since we are keeping pointers into it */
10281  destroyPQExpBuffer(query);
10282 
10283  *items = comments;
10284  return ntups;
10285 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
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:74
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:71
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3356
Oid objoid
Definition: pg_dump.c:73
Oid classoid
Definition: pg_dump.c:72
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ collectSecLabels()

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

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

15586 {
15587  PGresult *res;
15588  PQExpBuffer query;
15589  int i_label;
15590  int i_provider;
15591  int i_classoid;
15592  int i_objoid;
15593  int i_objsubid;
15594  int ntups;
15595  int i;
15596  SecLabelItem *labels;
15597 
15598  query = createPQExpBuffer();
15599 
15600  appendPQExpBufferStr(query,
15601  "SELECT label, provider, classoid, objoid, objsubid "
15602  "FROM pg_catalog.pg_seclabel "
15603  "ORDER BY classoid, objoid, objsubid");
15604 
15605  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15606 
15607  /* Construct lookup table containing OIDs in numeric form */
15608  i_label = PQfnumber(res, "label");
15609  i_provider = PQfnumber(res, "provider");
15610  i_classoid = PQfnumber(res, "classoid");
15611  i_objoid = PQfnumber(res, "objoid");
15612  i_objsubid = PQfnumber(res, "objsubid");
15613 
15614  ntups = PQntuples(res);
15615 
15616  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15617 
15618  for (i = 0; i < ntups; i++)
15619  {
15620  labels[i].label = PQgetvalue(res, i, i_label);
15621  labels[i].provider = PQgetvalue(res, i, i_provider);
15622  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15623  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15624  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15625  }
15626 
15627  /* Do NOT free the PGresult since we are keeping pointers into it */
15628  destroyPQExpBuffer(query);
15629 
15630  *items = labels;
15631  return ntups;
15632 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
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:3248
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
Oid objoid
Definition: pg_dump.c:82
const char * provider
Definition: pg_dump.c:79
#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:3356
int objsubid
Definition: pg_dump.c:83
int i
Oid classoid
Definition: pg_dump.c:81
const char * label
Definition: pg_dump.c:80
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ convertRegProcReference()

static char * convertRegProcReference ( const char *  proc)
static

Definition at line 13104 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

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

◆ convertTSFunction()

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

Definition at line 13175 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

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

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

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

18610 {
18611  DumpableObject *dobjs;
18612 
18613  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18614 
18615  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18616  dobjs[0].catId = nilCatalogId;
18617  AssignDumpId(dobjs + 0);
18618  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18619 
18620  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18621  dobjs[1].catId = nilCatalogId;
18622  AssignDumpId(dobjs + 1);
18623  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18624 
18625  return dobjs;
18626 }
char * name
Definition: pg_dump.h:131
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:605
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
CatalogId catId
Definition: pg_dump.h:129
static const CatalogId nilCatalogId
Definition: pg_dump.c:131
DumpableObjectType objType
Definition: pg_dump.h:128

◆ createDummyViewAsClause()

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

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

15829 {
15830  PQExpBuffer result = createPQExpBuffer();
15831  int j;
15832 
15833  appendPQExpBufferStr(result, "SELECT");
15834 
15835  for (j = 0; j < tbinfo->numatts; j++)
15836  {
15837  if (j > 0)
15838  appendPQExpBufferChar(result, ',');
15839  appendPQExpBufferStr(result, "\n ");
15840 
15841  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15842 
15843  /*
15844  * Must add collation if not default for the type, because CREATE OR
15845  * REPLACE VIEW won't change it
15846  */
15847  if (OidIsValid(tbinfo->attcollation[j]))
15848  {
15849  CollInfo *coll;
15850 
15851  coll = findCollationByOid(tbinfo->attcollation[j]);
15852  if (coll)
15853  appendPQExpBuffer(result, " COLLATE %s",
15854  fmtQualifiedDumpable(coll));
15855  }
15856 
15857  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15858  }
15859 
15860  return result;
15861 }
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:308
char ** attnames
Definition: pg_dump.h:307
#define OidIsValid(objectId)
Definition: c.h:710
Oid * attcollation
Definition: pg_dump.h:320
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
int numatts
Definition: pg_dump.h:306
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:937

◆ createViewAsClause()

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

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

15780 {
15781  PQExpBuffer query = createPQExpBuffer();
15782  PQExpBuffer result = createPQExpBuffer();
15783  PGresult *res;
15784  int len;
15785 
15786  /* Fetch the view definition */
15787  appendPQExpBuffer(query,
15788  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15789  tbinfo->dobj.catId.oid);
15790 
15791  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15792 
15793  if (PQntuples(res) != 1)
15794  {
15795  if (PQntuples(res) < 1)
15796  fatal("query to obtain definition of view \"%s\" returned no data",
15797  tbinfo->dobj.name);
15798  else
15799  fatal("query to obtain definition of view \"%s\" returned more than one definition",
15800  tbinfo->dobj.name);
15801  }
15802 
15803  len = PQgetlength(res, 0, 0);
15804 
15805  if (len == 0)
15806  fatal("definition of view \"%s\" appears to be empty (length zero)",
15807  tbinfo->dobj.name);
15808 
15809  /* Strip off the trailing semicolon so that other things may follow. */
15810  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15811  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15812 
15813  PQclear(res);
15814  destroyPQExpBuffer(query);
15815 
15816  return result;
15817 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3653
char * name
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
DumpableObject dobj
Definition: pg_dump.h:263
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:804
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:129
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:399
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ dumpAccessMethod()

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

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

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

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

15244 {
15245  DumpId aclDumpId = InvalidDumpId;
15246  DumpOptions *dopt = fout->dopt;
15247  PQExpBuffer sql;
15248 
15249  /* Do nothing if ACL dump is not enabled */
15250  if (dopt->aclsSkip)
15251  return InvalidDumpId;
15252 
15253  /* --data-only skips ACLs *except* BLOB ACLs */
15254  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
15255  return InvalidDumpId;
15256 
15257  sql = createPQExpBuffer();
15258 
15259  /*
15260  * Check to see if this object has had any initial ACLs included for it.
15261  * If so, we are in binary upgrade mode and these are the ACLs to turn
15262  * into GRANT and REVOKE statements to set and record the initial
15263  * privileges for an extension object. Let the backend know that these
15264  * are to be recorded by calling binary_upgrade_set_record_init_privs()
15265  * before and after.
15266  */
15267  if (strlen(initacls) != 0 || strlen(initracls) != 0)
15268  {
15269  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
15270  if (!buildACLCommands(name, subname, nspname, type,
15271  initacls, initracls, owner,
15272  "", fout->remoteVersion, sql))
15273  fatal("could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)",
15274  initacls, initracls, name, type);
15275  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
15276  }
15277 
15278  if (!buildACLCommands(name, subname, nspname, type,
15279  acls, racls, owner,
15280  "", fout->remoteVersion, sql))
15281  fatal("could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)",
15282  acls, racls, name, type);
15283 
15284  if (sql->len > 0)
15285  {
15287  DumpId aclDeps[2];
15288  int nDeps = 0;
15289 
15290  if (subname)
15291  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
15292  else
15293  appendPQExpBuffer(tag, "%s %s", type, name);
15294 
15295  aclDeps[nDeps++] = objDumpId;
15296  if (altDumpId != InvalidDumpId)
15297  aclDeps[nDeps++] = altDumpId;
15298 
15299  aclDumpId = createDumpId();
15300 
15301  ArchiveEntry(fout, nilCatalogId, aclDumpId,
15302  ARCHIVE_OPTS(.tag = tag->data,
15303  .namespace = nspname,
15304  .owner = owner,
15305  .description = "ACL",
15306  .section = SECTION_NONE,
15307  .createStmt = sql->data,
15308  .deps = aclDeps,
15309  .nDeps = nDeps));
15310 
15311  destroyPQExpBuffer(tag);
15312  }
15313 
15314  destroyPQExpBuffer(sql);
15315 
15316  return aclDumpId;
15317 }
#define ARCHIVE_OPTS(...)
int DumpId
Definition: pg_backup.h:243
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:191
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:245
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:58
#define fatal(...)
const char * name
Definition: encode.c:515
static const CatalogId nilCatalogId
Definition: pg_dump.c:131
DumpId createDumpId(void)
Definition: common.c:651
int remoteVersion
Definition: pg_backup.h:196

◆ dumpAgg()

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

Definition at line 14185 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

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

◆ dumpAttrDef()

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

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

16676 {
16677  DumpOptions *dopt = fout->dopt;
16678  TableInfo *tbinfo = adinfo->adtable;
16679  int adnum = adinfo->adnum;
16680  PQExpBuffer q;
16681  PQExpBuffer delq;
16682  char *qualrelname;
16683  char *tag;
16684  char *foreign;
16685 
16686  /* Skip if table definition not to be dumped */
16687  if (!tbinfo->dobj.dump || dopt->dataOnly)
16688  return;
16689 
16690  /* Skip if not "separate"; it was dumped in the table's definition */
16691  if (!adinfo->separate)
16692  return;
16693 
16694  q = createPQExpBuffer();
16695  delq = createPQExpBuffer();
16696 
16697  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16698 
16699  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16700 
16702  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16703  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16704  adinfo->adef_expr);
16705 
16706  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16707  foreign, qualrelname,
16708  fmtId(tbinfo->attnames[adnum - 1]));
16709 
16710  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16711 
16712  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16713  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16714  ARCHIVE_OPTS(.tag = tag,
16715  .namespace = tbinfo->dobj.namespace->dobj.name,
16716  .owner = tbinfo->rolname,
16717  .description = "DEFAULT",
16718  .section = SECTION_PRE_DATA,
16719  .createStmt = q->data,
16720  .dropStmt = delq->data));
16721 
16722  free(tag);
16723  destroyPQExpBuffer(q);
16724  destroyPQExpBuffer(delq);
16725  free(qualrelname);
16726 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
char relkind
Definition: pg_dump.h:269
DumpComponents dump
Definition: pg_dump.h:132
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
char * rolname
Definition: pg_dump.h:264
DumpId dumpId
Definition: pg_dump.h:130
char ** attnames
Definition: pg_dump.h:307
DumpableObject dobj
Definition: pg_dump.h:263
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
bool separate
Definition: pg_dump.h:358
DumpOptions * dopt
Definition: pg_backup.h:191
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:355
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
DumpableObject dobj
Definition: pg_dump.h:354
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
CatalogId catId
Definition: pg_dump.h:129
char * adef_expr
Definition: pg_dump.h:357

◆ dumpBaseType()

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

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

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

◆ dumpBlob()

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

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

3534 {
3535  PQExpBuffer cquery = createPQExpBuffer();
3536  PQExpBuffer dquery = createPQExpBuffer();
3537 
3538  appendPQExpBuffer(cquery,
3539  "SELECT pg_catalog.lo_create('%s');\n",
3540  binfo->dobj.name);
3541 
3542  appendPQExpBuffer(dquery,
3543  "SELECT pg_catalog.lo_unlink('%s');\n",
3544  binfo->dobj.name);
3545 
3546  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3547  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3548  ARCHIVE_OPTS(.tag = binfo->dobj.name,
3549  .owner = binfo->rolname,
3550  .description = "BLOB",
3551  .section = SECTION_PRE_DATA,
3552  .createStmt = cquery->data,
3553  .dropStmt = dquery->data));
3554 
3555  /* Dump comment if any */
3556  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3557  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3558  NULL, binfo->rolname,
3559  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3560 
3561  /* Dump security label if any */
3562  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3563  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3564  NULL, binfo->rolname,
3565  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3566 
3567  /* Dump ACL if any */
3568  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3569  dumpACL(fout, binfo->dobj.dumpId, InvalidDumpId, "LARGE OBJECT",
3570  binfo->dobj.name, NULL,
3571  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3572  binfo->initblobacl, binfo->initrblobacl);
3573 
3574  destroyPQExpBuffer(cquery);
3575  destroyPQExpBuffer(dquery);
3576 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
DumpComponents dump
Definition: pg_dump.h:132
DumpId dumpId
Definition: pg_dump.h:130
static void dumpSecLabel(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:15338
char * initblobacl
Definition: pg_dump.h:584
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject dobj
Definition: pg_dump.h:580
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:581
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define InvalidDumpId
Definition: pg_backup.h:245
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:90
static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:15239
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
char * blobacl
Definition: pg_dump.h:582
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
CatalogId catId
Definition: pg_dump.h:129
char * rblobacl
Definition: pg_dump.h:583
char * initrblobacl
Definition: pg_dump.h:585
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:10042

◆ dumpBlobs()

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

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

3584 {
3585  const char *blobQry;
3586  const char *blobFetchQry;
3587  PGconn *conn = GetConnection(fout);
3588  PGresult *res;
3589  char buf[LOBBUFSIZE];
3590  int ntups;
3591  int i;
3592  int cnt;
3593 
3594  pg_log_info("saving large objects");
3595 
3596  /*
3597  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3598  * the already-in-memory dumpable objects instead...
3599  */
3600  if (fout->remoteVersion >= 90000)
3601  blobQry =
3602  "DECLARE bloboid CURSOR FOR "
3603  "SELECT oid FROM pg_largeobject_metadata ORDER BY 1";
3604  else
3605  blobQry =
3606  "DECLARE bloboid CURSOR FOR "
3607  "SELECT DISTINCT loid FROM pg_largeobject ORDER BY 1";
3608 
3609  ExecuteSqlStatement(fout, blobQry);
3610 
3611  /* Command to fetch from cursor */
3612  blobFetchQry = "FETCH 1000 IN bloboid";
3613 
3614  do
3615  {
3616  /* Do a fetch */
3617  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3618 
3619  /* Process the tuples, if any */
3620  ntups = PQntuples(res);
3621  for (i = 0; i < ntups; i++)
3622  {
3623  Oid blobOid;
3624  int loFd;
3625 
3626  blobOid = atooid(PQgetvalue(res, i, 0));
3627  /* Open the BLOB */
3628  loFd = lo_open(conn, blobOid, INV_READ);
3629  if (loFd == -1)
3630  fatal("could not open large object %u: %s",
3631  blobOid, PQerrorMessage(conn));
3632 
3633  StartBlob(fout, blobOid);
3634 
3635  /* Now read it in chunks, sending data to archive */
3636  do
3637  {
3638  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3639  if (cnt < 0)
3640  fatal("error reading large object %u: %s",
3641  blobOid, PQerrorMessage(conn));
3642 
3643  WriteData(fout, buf, cnt);
3644  } while (cnt > 0);
3645 
3646  lo_close(conn, loFd);
3647 
3648  EndBlob(fout, blobOid);
3649  }
3650 
3651  PQclear(res);
3652  } while (ntups > 0);
3653 
3654  return 1;
3655 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6737
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
#define INV_READ
Definition: libpq-fs.h:22
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:96
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:125
int StartBlob(Archive *AH, Oid oid)
PGconn * conn
Definition: streamutil.c:54
static char * buf
Definition: pg_test_fsync.c:68
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:142
void WriteData(Archive *AH, const void *data, size_t dLen)
#define atooid(x)
Definition: postgres_ext.h:42
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
void PQclear(PGresult *res)
Definition: fe-exec.c:694
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:282
#define fatal(...)
int EndBlob(Archive *AH, Oid oid)
int i
#define LOBBUFSIZE
#define pg_log_info(...)
Definition: logging.h:88
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294
int remoteVersion
Definition: pg_backup.h:196

◆ dumpCast()

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

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

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

◆ dumpCollation()

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

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

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

◆ dumpComment()

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

Definition at line 10042 of file pg_dump.c.

References dumpCommentExtended().

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

10046 {
10047  dumpCommentExtended(fout, type, name, namespace, owner,
10048  catalogId, subid, dumpId, NULL);
10049 }
static void dumpCommentExtended(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId, const char *initdb_comment)
Definition: pg_dump.c:9942
const char * name
Definition: encode.c:515

◆ dumpCommentExtended()

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

Definition at line 9942 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 dumpComment(), and dumpNamespace().

9947 {
9948  DumpOptions *dopt = fout->dopt;
9949  CommentItem *comments;
9950  int ncomments;
9951 
9952  /* do nothing, if --no-comments is supplied */
9953  if (dopt->no_comments)
9954  return;
9955 
9956  /* Comments are schema not data ... except blob comments are data */
9957  if (strcmp(type, "LARGE OBJECT") != 0)
9958  {
9959  if (dopt->dataOnly)
9960  return;
9961  }
9962  else
9963  {
9964  /* We do dump blob comments in binary-upgrade mode */
9965  if (dopt->schemaOnly && !dopt->binary_upgrade)
9966  return;
9967  }
9968 
9969  /* Search for comments associated with catalogId, using table */
9970  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9971  &comments);
9972 
9973  /* Is there one matching the subid? */
9974  while (ncomments > 0)
9975  {
9976  if (comments->objsubid == subid)
9977  break;
9978  comments++;
9979  ncomments--;
9980  }
9981 
9982  if (initdb_comment != NULL)
9983  {
9984  static CommentItem empty_comment = {.descr = ""};
9985 
9986  /*
9987  * initdb creates this object with a comment. Skip dumping the
9988  * initdb-provided comment, which would complicate matters for
9989  * non-superuser use of pg_dump. When the DBA has removed initdb's
9990  * comment, replicate that.
9991  */
9992  if (ncomments == 0)
9993  {
9994  comments = &empty_comment;
9995  ncomments = 1;
9996  }
9997  else if (strcmp(comments->descr, initdb_comment) == 0)
9998  ncomments = 0;
9999  }
10000 
10001  /* If a comment exists, build COMMENT ON statement */
10002  if (ncomments > 0)
10003  {
10004  PQExpBuffer query = createPQExpBuffer();
10006 
10007  appendPQExpBuffer(query, "COMMENT ON %s ", type);
10008  if (namespace && *namespace)
10009  appendPQExpBuffer(query, "%s.", fmtId(namespace));
10010  appendPQExpBuffer(query, "%s IS ", name);
10011  appendStringLiteralAH(query, comments->descr, fout);
10012  appendPQExpBufferStr(query, ";\n");
10013 
10014  appendPQExpBuffer(tag, "%s %s", type, name);
10015 
10016  /*
10017  * We mark comments as SECTION_NONE because they really belong in the
10018  * same section as their parent, whether that is pre-data or
10019  * post-data.
10020  */
10022  ARCHIVE_OPTS(.tag = tag->data,
10023  .namespace = namespace,
10024  .owner = owner,
10025  .description = "COMMENT",
10026  .section = SECTION_NONE,
10027  .createStmt = query->data,
10028  .deps = &dumpId,
10029  .nDeps = 1));
10030 
10031  destroyPQExpBuffer(query);
10032  destroyPQExpBuffer(tag);
10033  }
10034 }
#define ARCHIVE_OPTS(...)
Oid tableoid
Definition: pg_backup.h:239
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:74
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:10157
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:143
DumpOptions * dopt
Definition: pg_backup.h:191
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:71
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:303
int no_comments
Definition: pg_backup.h:157
const char * name
Definition: encode.c:515
static const CatalogId nilCatalogId
Definition: pg_dump.c:131
DumpId createDumpId(void)
Definition: common.c:651

◆ dumpCompositeType()

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

Definition at line 11481 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralAH, ARCHIVE_OPTS, ArchiveEntry(), atooid, attalign, attlen, attname, _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), binary_upgrade_set_pg_class_oids(), binary_upgrade_set_type_oids_by_type_oid(), _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _typeInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_ACL, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, DUMP_COMPONENT_SECLABEL, dumpACL(), dumpComment(), dumpCompositeTypeColComments(), _dumpableObject::dumpId, dumpSecLabel(), ExecuteSqlQuery(), findCollationByOid(), fmtId(), fmtQualifiedDumpable, free, i, _typeInfo::initrtypacl, _typeInfo::inittypacl, InvalidDumpId, _dumpableObject::name, CatalogId::oid, OidIsValid, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), Archive::remoteVersion, _typeInfo::rolname, _typeInfo::rtypacl, SECTION_PRE_DATA, _typeInfo::typacl, and _typeInfo::typrelid.

Referenced by dumpType().

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

◆ dumpCompositeTypeColComments()

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

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

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

◆ dumpConstraint()

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

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

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

◆ dumpConversion()

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

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

14052 {
14053  DumpOptions *dopt = fout->dopt;
14054  PQExpBuffer query;
14055  PQExpBuffer q;
14056  PQExpBuffer delq;
14057  char *qconvname;
14058  PGresult *res;
14059  int i_conforencoding;
14060  int i_contoencoding;
14061  int i_conproc;
14062  int i_condefault;
14063  const char *conforencoding;
14064  const char *contoencoding;
14065  const char *conproc;
14066  bool condefault;
14067 
14068  /* Skip if not to be dumped */
14069  if (!convinfo->dobj.dump || dopt->dataOnly)
14070  return;
14071 
14072  query = createPQExpBuffer();
14073  q = createPQExpBuffer();
14074  delq = createPQExpBuffer();
14075 
14076  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
14077 
14078  /* Get conversion-specific details */
14079  appendPQExpBuffer(query, "SELECT "
14080  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
14081  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
14082  "conproc, condefault "
14083  "FROM pg_catalog.pg_conversion c "
14084  "WHERE c.oid = '%u'::pg_catalog.oid",
14085  convinfo->dobj.catId.oid);
14086 
14087  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14088 
14089  i_conforencoding = PQfnumber(res, "conforencoding");
14090  i_contoencoding = PQfnumber(res, "contoencoding");
14091  i_conproc = PQfnumber(res, "conproc");
14092  i_condefault = PQfnumber(res, "condefault");
14093 
14094  conforencoding = PQgetvalue(res, 0, i_conforencoding);
14095  contoencoding = PQgetvalue(res, 0, i_contoencoding);
14096  conproc = PQgetvalue(res, 0, i_conproc);
14097  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
14098 
14099  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
14100  fmtQualifiedDumpable(convinfo));
14101 
14102  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
14103  (condefault) ? "DEFAULT " : "",
14104  fmtQualifiedDumpable(convinfo));
14105  appendStringLiteralAH(q, conforencoding, fout);
14106  appendPQExpBufferStr(q, " TO ");
14107  appendStringLiteralAH(q, contoencoding, fout);
14108  /* regproc output is already sufficiently quoted */
14109  appendPQExpBuffer(q, " FROM %s;\n", conproc);
14110 
14111  if (dopt->binary_upgrade)
14112  binary_upgrade_extension_member(q, &convinfo->dobj,
14113  "CONVERSION", qconvname,
14114  convinfo->dobj.namespace->dobj.name);
14115 
14116  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14117  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
14118  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
14119  .namespace = convinfo->dobj.namespace->dobj.name,
14120  .owner = convinfo->rolname,
14121  .description = "CONVERSION",
14122  .section = SECTION_PRE_DATA,
14123  .createStmt = q->data,
14124  .dropStmt = delq->data));
14125 
14126  /* Dump Conversion Comments */
14127  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14128  dumpComment(fout, "CONVERSION", qconvname,
14129  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
14130  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
14131 
14132  PQclear(res);
14133 
14134  destroyPQExpBuffer(query);
14135  destroyPQExpBuffer(q);
14136  destroyPQExpBuffer(delq);
14137  free(qconvname);
14138 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
DumpComponents dump
Definition: pg_dump.h:132
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
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:255
DumpId dumpId
Definition: pg_dump.h:130
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309