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

18371 {
18372  DumpableObject *preDataBound = boundaryObjs + 0;
18373  DumpableObject *postDataBound = boundaryObjs + 1;
18374  int i;
18375 
18376  for (i = 0; i < numObjs; i++)
18377  {
18378  DumpableObject *dobj = dobjs[i];
18379 
18380  /*
18381  * The classification of object types here must match the SECTION_xxx
18382  * values assigned during subsequent ArchiveEntry calls!
18383  */
18384  switch (dobj->objType)
18385  {
18386  case DO_NAMESPACE:
18387  case DO_EXTENSION:
18388  case DO_TYPE:
18389  case DO_SHELL_TYPE:
18390  case DO_FUNC:
18391  case DO_AGG:
18392  case DO_OPERATOR:
18393  case DO_ACCESS_METHOD:
18394  case DO_OPCLASS:
18395  case DO_OPFAMILY:
18396  case DO_COLLATION:
18397  case DO_CONVERSION:
18398  case DO_TABLE:
18399  case DO_TABLE_ATTACH:
18400  case DO_ATTRDEF:
18401  case DO_PROCLANG:
18402  case DO_CAST:
18403  case DO_DUMMY_TYPE:
18404  case DO_TSPARSER:
18405  case DO_TSDICT:
18406  case DO_TSTEMPLATE:
18407  case DO_TSCONFIG:
18408  case DO_FDW:
18409  case DO_FOREIGN_SERVER:
18410  case DO_TRANSFORM:
18411  case DO_BLOB:
18412  /* Pre-data objects: must come before the pre-data boundary */
18413  addObjectDependency(preDataBound, dobj->dumpId);
18414  break;
18415  case DO_TABLE_DATA:
18416  case DO_SEQUENCE_SET:
18417  case DO_BLOB_DATA:
18418  /* Data objects: must come between the boundaries */
18419  addObjectDependency(dobj, preDataBound->dumpId);
18420  addObjectDependency(postDataBound, dobj->dumpId);
18421  break;
18422  case DO_INDEX:
18423  case DO_INDEX_ATTACH:
18424  case DO_STATSEXT:
18425  case DO_REFRESH_MATVIEW:
18426  case DO_TRIGGER:
18427  case DO_EVENT_TRIGGER:
18428  case DO_DEFAULT_ACL:
18429  case DO_POLICY:
18430  case DO_PUBLICATION:
18431  case DO_PUBLICATION_REL:
18432  case DO_SUBSCRIPTION:
18433  /* Post-data objects: must come after the post-data boundary */
18434  addObjectDependency(dobj, postDataBound->dumpId);
18435  break;
18436  case DO_RULE:
18437  /* Rules are post-data, but only if dumped separately */
18438  if (((RuleInfo *) dobj)->separate)
18439  addObjectDependency(dobj, postDataBound->dumpId);
18440  break;
18441  case DO_CONSTRAINT:
18442  case DO_FK_CONSTRAINT:
18443  /* Constraints are post-data, but only if dumped separately */
18444  if (((ConstraintInfo *) dobj)->separate)
18445  addObjectDependency(dobj, postDataBound->dumpId);
18446  break;
18447  case DO_PRE_DATA_BOUNDARY:
18448  /* nothing to do */
18449  break;
18450  case DO_POST_DATA_BOUNDARY:
18451  /* must come after the pre-data boundary */
18452  addObjectDependency(dobj, preDataBound->dumpId);
18453  break;
18454  }
18455  }
18456 }
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:715
int i
DumpableObjectType objType
Definition: pg_dump.h:128

◆ addConstrChildIdxDeps()

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

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

7432 {
7433  SimplePtrListCell *cell;
7434 
7435  Assert(dobj->objType == DO_FK_CONSTRAINT);
7436 
7437  for (cell = refidx->partattaches.head; cell; cell = cell->next)
7438  {
7439  IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
7440 
7441  addObjectDependency(dobj, attach->dobj.dumpId);
7442 
7443  if (attach->partitionIdx->partattaches.head != NULL)
7444  addConstrChildIdxDeps(dobj, attach->partitionIdx);
7445  }
7446 }
static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx)
Definition: pg_dump.c:7431
DumpId dumpId
Definition: pg_dump.h:130
DumpableObject dobj
Definition: pg_dump.h:394
IndxInfo * partitionIdx
Definition: pg_dump.h:396
SimplePtrList partattaches
Definition: pg_dump.h:386
struct SimplePtrListCell * next
Definition: simple_list.h:48
#define Assert(condition)
Definition: c.h:804
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:715
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 4503 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().

4509 {
4510  if (dobj->depends_on_ext)
4511  {
4512  char *nm;
4513  PGresult *res;
4514  PQExpBuffer query;
4515  int ntups;
4516  int i_extname;
4517  int i;
4518 
4519  /* dodge fmtId() non-reentrancy */
4520  nm = pg_strdup(objname);
4521 
4522  query = createPQExpBuffer();
4523  appendPQExpBuffer(query,
4524  "SELECT e.extname "
4525  "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4526  "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4527  "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4528  "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4529  catalog,
4530  dobj->catId.oid);
4531  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4532  ntups = PQntuples(res);
4533  i_extname = PQfnumber(res, "extname");
4534  for (i = 0; i < ntups; i++)
4535  {
4536  appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4537  keyword, nm,
4538  fmtId(PQgetvalue(res, i, i_extname)));
4539  }
4540 
4541  PQclear(res);
4542  destroyPQExpBuffer(query);
4543  pg_free(nm);
4544  }
4545 }
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 18684 of file pg_dump.c.

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

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

18686 {
18687  bool res;
18688 
18689  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18690  fout->std_strings);
18691  if (!res)
18692  pg_log_warning("could not parse reloptions array");
18693 }
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 4775 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().

4780 {
4781  DumpableObject *extobj = NULL;
4782  int i;
4783 
4784  if (!dobj->ext_member)
4785  return;
4786 
4787  /*
4788  * Find the parent extension. We could avoid this search if we wanted to
4789  * add a link field to DumpableObject, but the space costs of that would
4790  * be considerable. We assume that member objects could only have a
4791  * direct dependency on their own extension, not any others.
4792  */
4793  for (i = 0; i < dobj->nDeps; i++)
4794  {
4795  extobj = findObjectByDumpId(dobj->dependencies[i]);
4796  if (extobj && extobj->objType == DO_EXTENSION)
4797  break;
4798  extobj = NULL;
4799  }
4800  if (extobj == NULL)
4801  fatal("could not find parent extension for %s %s",
4802  objtype, objname);
4803 
4804  appendPQExpBufferStr(upgrade_buffer,
4805  "\n-- For binary upgrade, handle extension membership the hard way\n");
4806  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4807  fmtId(extobj->name),
4808  objtype);
4809  if (objnamespace && *objnamespace)
4810  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4811  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4812 }
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:661
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 4692 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().

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

4668 {
4669  PQExpBuffer upgrade_query = createPQExpBuffer();
4670  PGresult *upgrade_res;
4671  Oid pg_type_oid;
4672 
4673  appendPQExpBuffer(upgrade_query,
4674  "SELECT c.reltype AS crel "
4675  "FROM pg_catalog.pg_class c "
4676  "WHERE c.oid = '%u'::pg_catalog.oid;",
4677  pg_rel_oid);
4678 
4679  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4680 
4681  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4682 
4683  if (OidIsValid(pg_type_oid))
4684  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4685  pg_type_oid, false, false);
4686 
4687  PQclear(upgrade_res);
4688  destroyPQExpBuffer(upgrade_query);
4689 }
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:4579
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 4579 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().

4584 {
4585  PQExpBuffer upgrade_query = createPQExpBuffer();
4586  PGresult *res;
4587  Oid pg_type_array_oid;
4588  Oid pg_type_multirange_oid;
4589  Oid pg_type_multirange_array_oid;
4590 
4591  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4592  appendPQExpBuffer(upgrade_buffer,
4593  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4594  pg_type_oid);
4595 
4596  /* we only support old >= 8.3 for binary upgrades */
4597  appendPQExpBuffer(upgrade_query,
4598  "SELECT typarray "
4599  "FROM pg_catalog.pg_type "
4600  "WHERE oid = '%u'::pg_catalog.oid;",
4601  pg_type_oid);
4602 
4603  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4604 
4605  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4606 
4607  PQclear(res);
4608 
4609  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4610  pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4611 
4612  if (OidIsValid(pg_type_array_oid))
4613  {
4614  appendPQExpBufferStr(upgrade_buffer,
4615  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4616  appendPQExpBuffer(upgrade_buffer,
4617  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4618  pg_type_array_oid);
4619  }
4620 
4621  /*
4622  * Pre-set the multirange type oid and its own array type oid.
4623  */
4624  if (include_multirange_type)
4625  {
4626  if (fout->remoteVersion >= 140000)
4627  {
4628  appendPQExpBuffer(upgrade_query,
4629  "SELECT t.oid, t.typarray "
4630  "FROM pg_catalog.pg_type t "
4631  "JOIN pg_catalog.pg_range r "
4632  "ON t.oid = r.rngmultitypid "
4633  "WHERE r.rngtypid = '%u'::pg_catalog.oid;",
4634  pg_type_oid);
4635 
4636  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4637 
4638  pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
4639  pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4640 
4641  PQclear(res);
4642  }
4643  else
4644  {
4645  pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4646  pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query);
4647  }
4648 
4649  appendPQExpBufferStr(upgrade_buffer,
4650  "\n-- For binary upgrade, must preserve multirange pg_type oid\n");
4651  appendPQExpBuffer(upgrade_buffer,
4652  "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4653  pg_type_multirange_oid);
4654  appendPQExpBufferStr(upgrade_buffer,
4655  "\n-- For binary upgrade, must preserve multirange pg_type array oid\n");
4656  appendPQExpBuffer(upgrade_buffer,
4657  "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4658  pg_type_multirange_array_oid);
4659  }
4660 
4661  destroyPQExpBuffer(upgrade_query);
4662 }
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:4548
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 18483 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().

18484 {
18485  ArchiveHandle *AH = (ArchiveHandle *) fout;
18486  TocEntry *te;
18487 
18488  /* Scan all TOC entries in the archive */
18489  for (te = AH->toc->next; te != AH->toc; te = te->next)
18490  {
18491  DumpableObject *dobj;
18492  DumpId *dependencies;
18493  int nDeps;
18494  int allocDeps;
18495 
18496  /* No need to process entries that will not be dumped */
18497  if (te->reqs == 0)
18498  continue;
18499  /* Ignore entries that already have "special" dependencies */
18500  if (te->nDeps > 0)
18501  continue;
18502  /* Otherwise, look up the item's original DumpableObject, if any */
18503  dobj = findObjectByDumpId(te->dumpId);
18504  if (dobj == NULL)
18505  continue;
18506  /* No work if it has no dependencies */
18507  if (dobj->nDeps <= 0)
18508  continue;
18509  /* Set up work array */
18510  allocDeps = 64;
18511  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18512  nDeps = 0;
18513  /* Recursively find all dumpable dependencies */
18514  findDumpableDependencies(AH, dobj,
18515  &dependencies, &nDeps, &allocDeps);
18516  /* And save 'em ... */
18517  if (nDeps > 0)
18518  {
18519  dependencies = (DumpId *) pg_realloc(dependencies,
18520  nDeps * sizeof(DumpId));
18521  te->dependencies = dependencies;
18522  te->nDeps = nDeps;
18523  }
18524  else
18525  free(dependencies);
18526  }
18527 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:244
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:661
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:18531

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

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

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

10002 {
10003  PGresult *res;
10004  PQExpBuffer query;
10005  int i_description;
10006  int i_classoid;
10007  int i_objoid;
10008  int i_objsubid;
10009  int ntups;
10010  int i;
10011  CommentItem *comments;
10012 
10013  query = createPQExpBuffer();
10014 
10015  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
10016  "FROM pg_catalog.pg_description "
10017  "ORDER BY classoid, objoid, objsubid");
10018 
10019  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10020 
10021  /* Construct lookup table containing OIDs in numeric form */
10022 
10023  i_description = PQfnumber(res, "description");
10024  i_classoid = PQfnumber(res, "classoid");
10025  i_objoid = PQfnumber(res, "objoid");
10026  i_objsubid = PQfnumber(res, "objsubid");
10027 
10028  ntups = PQntuples(res);
10029 
10030  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
10031 
10032  for (i = 0; i < ntups; i++)
10033  {
10034  comments[i].descr = PQgetvalue(res, i, i_description);
10035  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
10036  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
10037  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
10038  }
10039 
10040  /* Do NOT free the PGresult since we are keeping pointers into it */
10041  destroyPQExpBuffer(query);
10042 
10043  *items = comments;
10044  return ntups;
10045 }
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 15333 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().

15334 {
15335  PGresult *res;
15336  PQExpBuffer query;
15337  int i_label;
15338  int i_provider;
15339  int i_classoid;
15340  int i_objoid;
15341  int i_objsubid;
15342  int ntups;
15343  int i;
15344  SecLabelItem *labels;
15345 
15346  query = createPQExpBuffer();
15347 
15348  appendPQExpBufferStr(query,
15349  "SELECT label, provider, classoid, objoid, objsubid "
15350  "FROM pg_catalog.pg_seclabel "
15351  "ORDER BY classoid, objoid, objsubid");
15352 
15353  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15354 
15355  /* Construct lookup table containing OIDs in numeric form */
15356  i_label = PQfnumber(res, "label");
15357  i_provider = PQfnumber(res, "provider");
15358  i_classoid = PQfnumber(res, "classoid");
15359  i_objoid = PQfnumber(res, "objoid");
15360  i_objsubid = PQfnumber(res, "objsubid");
15361 
15362  ntups = PQntuples(res);
15363 
15364  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15365 
15366  for (i = 0; i < ntups; i++)
15367  {
15368  labels[i].label = PQgetvalue(res, i, i_label);
15369  labels[i].provider = PQgetvalue(res, i, i_provider);
15370  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15371  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15372  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15373  }
15374 
15375  /* Do NOT free the PGresult since we are keeping pointers into it */
15376  destroyPQExpBuffer(query);
15377 
15378  *items = labels;
15379  return ntups;
15380 }
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 12856 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

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

◆ convertTSFunction()

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

Definition at line 12927 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

12928 {
12929  char *result;
12930  char query[128];
12931  PGresult *res;
12932 
12933  snprintf(query, sizeof(query),
12934  "SELECT '%u'::pg_catalog.regproc", funcOid);
12935  res = ExecuteSqlQueryForSingleRow(fout, query);
12936 
12937  result = pg_strdup(PQgetvalue(res, 0, 0));
12938 
12939  PQclear(res);
12940 
12941  return result;
12942 }
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:217

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

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

18346 {
18347  DumpableObject *dobjs;
18348 
18349  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18350 
18351  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18352  dobjs[0].catId = nilCatalogId;
18353  AssignDumpId(dobjs + 0);
18354  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18355 
18356  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18357  dobjs[1].catId = nilCatalogId;
18358  AssignDumpId(dobjs + 1);
18359  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18360 
18361  return dobjs;
18362 }
char * name
Definition: pg_dump.h:131
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:577
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 15576 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().

15577 {
15578  PQExpBuffer result = createPQExpBuffer();
15579  int j;
15580 
15581  appendPQExpBufferStr(result, "SELECT");
15582 
15583  for (j = 0; j < tbinfo->numatts; j++)
15584  {
15585  if (j > 0)
15586  appendPQExpBufferChar(result, ',');
15587  appendPQExpBufferStr(result, "\n ");
15588 
15589  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15590 
15591  /*
15592  * Must add collation if not default for the type, because CREATE OR
15593  * REPLACE VIEW won't change it
15594  */
15595  if (OidIsValid(tbinfo->attcollation[j]))
15596  {
15597  CollInfo *coll;
15598 
15599  coll = findCollationByOid(tbinfo->attcollation[j]);
15600  if (coll)
15601  appendPQExpBuffer(result, " COLLATE %s",
15602  fmtQualifiedDumpable(coll));
15603  }
15604 
15605  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15606  }
15607 
15608  return result;
15609 }
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:310
char ** attnames
Definition: pg_dump.h:309
#define OidIsValid(objectId)
Definition: c.h:710
Oid * attcollation
Definition: pg_dump.h:322
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
int numatts
Definition: pg_dump.h:308
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:853

◆ createViewAsClause()

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

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

15528 {
15529  PQExpBuffer query = createPQExpBuffer();
15530  PQExpBuffer result = createPQExpBuffer();
15531  PGresult *res;
15532  int len;
15533 
15534  /* Fetch the view definition */
15535  appendPQExpBuffer(query,
15536  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15537  tbinfo->dobj.catId.oid);
15538 
15539  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15540 
15541  if (PQntuples(res) != 1)
15542  {
15543  if (PQntuples(res) < 1)
15544  fatal("query to obtain definition of view \"%s\" returned no data",
15545  tbinfo->dobj.name);
15546  else
15547  fatal("query to obtain definition of view \"%s\" returned more than one definition",
15548  tbinfo->dobj.name);
15549  }
15550 
15551  len = PQgetlength(res, 0, 0);
15552 
15553  if (len == 0)
15554  fatal("definition of view \"%s\" appears to be empty (length zero)",
15555  tbinfo->dobj.name);
15556 
15557  /* Strip off the trailing semicolon so that other things may follow. */
15558  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15559  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15560 
15561  PQclear(res);
15562  destroyPQExpBuffer(query);
15563 
15564  return result;
15565 }
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:265
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 12949 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().

12950 {
12951  DumpOptions *dopt = fout->dopt;
12952  PQExpBuffer q;
12953  PQExpBuffer delq;
12954  char *qamname;
12955 
12956  /* Skip if not to be dumped */
12957  if (!aminfo->dobj.dump || dopt->dataOnly)
12958  return;
12959 
12960  q = createPQExpBuffer();
12961  delq = createPQExpBuffer();
12962 
12963  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12964 
12965  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12966 
12967  switch (aminfo->amtype)
12968  {
12969  case AMTYPE_INDEX:
12970  appendPQExpBufferStr(q, "TYPE INDEX ");
12971  break;
12972  case AMTYPE_TABLE:
12973  appendPQExpBufferStr(q, "TYPE TABLE ");
12974  break;
12975  default:
12976  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12977  aminfo->amtype, qamname);
12978  destroyPQExpBuffer(q);
12979  destroyPQExpBuffer(delq);
12980  free(qamname);
12981  return;
12982  }
12983 
12984  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12985 
12986  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12987  qamname);
12988 
12989  if (dopt->binary_upgrade)
12990  binary_upgrade_extension_member(q, &aminfo->dobj,
12991  "ACCESS METHOD", qamname, NULL);
12992 
12993  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12994  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12995  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12996  .description = "ACCESS METHOD",
12997  .section = SECTION_PRE_DATA,
12998  .createStmt = q->data,
12999  .dropStmt = delq->data));
13000 
13001  /* Dump Access Method Comments */
13002  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13003  dumpComment(fout, "ACCESS METHOD", qamname,
13004  NULL, "",
13005  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
13006 
13007  destroyPQExpBuffer(q);
13008  destroyPQExpBuffer(delq);
13009  free(qamname);
13010 }
#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:4775
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:9802

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

14992 {
14993  DumpId aclDumpId = InvalidDumpId;
14994  DumpOptions *dopt = fout->dopt;
14995  PQExpBuffer sql;
14996 
14997  /* Do nothing if ACL dump is not enabled */
14998  if (dopt->aclsSkip)
14999  return InvalidDumpId;
15000 
15001  /* --data-only skips ACLs *except* BLOB ACLs */
15002  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
15003  return InvalidDumpId;
15004 
15005  sql = createPQExpBuffer();
15006 
15007  /*
15008  * Check to see if this object has had any initial ACLs included for it.
15009  * If so, we are in binary upgrade mode and these are the ACLs to turn
15010  * into GRANT and REVOKE statements to set and record the initial
15011  * privileges for an extension object. Let the backend know that these
15012  * are to be recorded by calling binary_upgrade_set_record_init_privs()
15013  * before and after.
15014  */
15015  if (strlen(initacls) != 0 || strlen(initracls) != 0)
15016  {
15017  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
15018  if (!buildACLCommands(name, subname, nspname, type,
15019  initacls, initracls, owner,
15020  "", fout->remoteVersion, sql))
15021  fatal("could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)",
15022  initacls, initracls, name, type);
15023  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
15024  }
15025 
15026  if (!buildACLCommands(name, subname, nspname, type,
15027  acls, racls, owner,
15028  "", fout->remoteVersion, sql))
15029  fatal("could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)",
15030  acls, racls, name, type);
15031 
15032  if (sql->len > 0)
15033  {
15035  DumpId aclDeps[2];
15036  int nDeps = 0;
15037 
15038  if (subname)
15039  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
15040  else
15041  appendPQExpBuffer(tag, "%s %s", type, name);
15042 
15043  aclDeps[nDeps++] = objDumpId;
15044  if (altDumpId != InvalidDumpId)
15045  aclDeps[nDeps++] = altDumpId;
15046 
15047  aclDumpId = createDumpId();
15048 
15049  ArchiveEntry(fout, nilCatalogId, aclDumpId,
15050  ARCHIVE_OPTS(.tag = tag->data,
15051  .namespace = nspname,
15052  .owner = owner,
15053  .description = "ACL",
15054  .section = SECTION_NONE,
15055  .createStmt = sql->data,
15056  .deps = aclDeps,
15057  .nDeps = nDeps));
15058 
15059  destroyPQExpBuffer(tag);
15060  }
15061 
15062  destroyPQExpBuffer(sql);
15063 
15064  return aclDumpId;
15065 }
#define ARCHIVE_OPTS(...)
int DumpId
Definition: pg_backup.h:244
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:246
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:58
#define fatal(...)
const char * name
Definition: encode.c:561
static const CatalogId nilCatalogId
Definition: pg_dump.c:131
DumpId createDumpId(void)
Definition: common.c:641
int remoteVersion
Definition: pg_backup.h:196

◆ dumpAgg()

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

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

13932 {
13933  DumpOptions *dopt = fout->dopt;
13934  PQExpBuffer query;
13935  PQExpBuffer q;
13936  PQExpBuffer delq;
13937  PQExpBuffer details;
13938  char *aggsig; /* identity signature */
13939  char *aggfullsig = NULL; /* full signature */
13940  char *aggsig_tag;
13941  PGresult *res;
13942  int i_agginitval;
13943  int i_aggminitval;
13944  const char *aggtransfn;
13945  const char *aggfinalfn;
13946  const char *aggcombinefn;
13947  const char *aggserialfn;
13948  const char *aggdeserialfn;
13949  const char *aggmtransfn;
13950  const char *aggminvtransfn;
13951  const char *aggmfinalfn;
13952  bool aggfinalextra;
13953  bool aggmfinalextra;
13954  char aggfinalmodify;
13955  char aggmfinalmodify;
13956  const char *aggsortop;
13957  char *aggsortconvop;
13958  char aggkind;
13959  const char *aggtranstype;
13960  const char *aggtransspace;
13961  const char *aggmtranstype;
13962  const char *aggmtransspace;
13963  const char *agginitval;
13964  const char *aggminitval;
13965  const char *proparallel;
13966  char defaultfinalmodify;
13967 
13968  /* Skip if not to be dumped */
13969  if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13970  return;
13971 
13972  query = createPQExpBuffer();
13973  q = createPQExpBuffer();
13974  delq = createPQExpBuffer();
13975  details = createPQExpBuffer();
13976 
13977  /* Get aggregate-specific details */
13978  appendPQExpBufferStr(query,
13979  "SELECT\n"
13980  "aggtransfn,\n"
13981  "aggfinalfn,\n"
13982  "aggtranstype::pg_catalog.regtype,\n"
13983  "agginitval,\n");
13984 
13985  if (fout->remoteVersion >= 80100)
13986  appendPQExpBufferStr(query,
13987  "aggsortop,\n");
13988  else
13989  appendPQExpBufferStr(query,
13990  "0 AS aggsortop,\n");
13991 
13992  if (fout->remoteVersion >= 80400)
13993  appendPQExpBufferStr(query,
13994  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n"
13995  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n");
13996 
13997  if (fout->remoteVersion >= 90400)
13998  appendPQExpBufferStr(query,
13999  "aggkind,\n"
14000  "aggmtransfn,\n"
14001  "aggminvtransfn,\n"
14002  "aggmfinalfn,\n"
14003  "aggmtranstype::pg_catalog.regtype,\n"
14004  "aggfinalextra,\n"
14005  "aggmfinalextra,\n"
14006  "aggtransspace,\n"
14007  "aggmtransspace,\n"
14008  "aggminitval,\n");
14009  else
14010  appendPQExpBufferStr(query,
14011  "'n' AS aggkind,\n"
14012  "'-' AS aggmtransfn,\n"
14013  "'-' AS aggminvtransfn,\n"
14014  "'-' AS aggmfinalfn,\n"
14015  "0 AS aggmtranstype,\n"
14016  "false AS aggfinalextra,\n"
14017  "false AS aggmfinalextra,\n"
14018  "0 AS aggtransspace,\n"
14019  "0 AS aggmtransspace,\n"
14020  "NULL AS aggminitval,\n");
14021 
14022  if (fout->remoteVersion >= 90600)
14023  appendPQExpBufferStr(query,
14024  "aggcombinefn,\n"
14025  "aggserialfn,\n"
14026  "aggdeserialfn,\n"
14027  "proparallel,\n");
14028  else
14029  appendPQExpBufferStr(query,
14030  "'-' AS aggcombinefn,\n"
14031  "'-' AS aggserialfn,\n"
14032  "'-' AS aggdeserialfn,\n"
14033  "'u' AS proparallel,\n");
14034 
14035  if (fout->remoteVersion >= 110000)
14036  appendPQExpBufferStr(query,
14037  "aggfinalmodify,\n"
14038  "aggmfinalmodify\n");
14039  else
14040  appendPQExpBufferStr(query,
14041  "'0' AS aggfinalmodify,\n"
14042  "'0' AS aggmfinalmodify\n");
14043 
14044  appendPQExpBuffer(query,
14045  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
14046  "WHERE a.aggfnoid = p.oid "
14047  "AND p.oid = '%u'::pg_catalog.oid",
14048  agginfo->aggfn.dobj.catId.oid);
14049 
14050  res = ExecuteSqlQueryForSingleRow(fout, query->data);
14051 
14052  i_agginitval = PQfnumber(res, "agginitval");
14053  i_aggminitval = PQfnumber(res, "aggminitval");
14054 
14055  aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn"));
14056  aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn"));
14057  aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn"));
14058  aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn"));
14059  aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn"));
14060  aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn"));
14061  aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn"));
14062  aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn"));
14063  aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't');
14064  aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't');
14065  aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0];
14066  aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0];
14067  aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop"));
14068  aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0];
14069  aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype"));
14070  aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace"));
14071  aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype"));
14072  aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace"));
14073  agginitval = PQgetvalue(res, 0, i_agginitval);
14074  aggminitval = PQgetvalue(res, 0, i_aggminitval);
14075  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
14076 
14077  if (fout->remoteVersion >= 80400)
14078  {
14079  /* 8.4 or later; we rely on server-side code for most of the work */
14080  char *funcargs;
14081  char *funciargs;
14082 
14083  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
14084  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
14085  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
14086  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
14087  }
14088  else
14089  /* pre-8.4, do it ourselves */
14090  aggsig = format_aggregate_signature(agginfo, fout, true);
14091 
14092  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
14093 
14094  /* identify default modify flag for aggkind (must match DefineAggregate) */
14095  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
14096  /* replace omitted flags for old versions */
14097  if (aggfinalmodify == '0')
14098  aggfinalmodify = defaultfinalmodify;
14099  if (aggmfinalmodify == '0')
14100  aggmfinalmodify = defaultfinalmodify;
14101 
14102  /* regproc and regtype output is already sufficiently quoted */
14103  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
14104  aggtransfn, aggtranstype);
14105 
14106  if (strcmp(aggtransspace, "0") != 0)
14107  {
14108  appendPQExpBuffer(details, ",\n SSPACE = %s",
14109  aggtransspace);
14110  }
14111 
14112  if (!PQgetisnull(res, 0, i_agginitval))
14113  {
14114  appendPQExpBufferStr(details, ",\n INITCOND = ");
14115  appendStringLiteralAH(details, agginitval, fout);
14116  }
14117 
14118  if (strcmp(aggfinalfn, "-") != 0)
14119  {
14120  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14121  aggfinalfn);
14122  if (aggfinalextra)
14123  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14124  if (aggfinalmodify != defaultfinalmodify)
14125  {
14126  switch (aggfinalmodify)
14127  {
14128  case AGGMODIFY_READ_ONLY:
14129  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14130  break;
14131  case AGGMODIFY_SHAREABLE:
14132  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14133  break;
14134  case AGGMODIFY_READ_WRITE:
14135  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14136  break;
14137  default:
14138  fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14139  agginfo->aggfn.dobj.name);
14140  break;
14141  }
14142  }
14143  }
14144 
14145  if (strcmp(aggcombinefn, "-") != 0)
14146  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14147 
14148  if (strcmp(aggserialfn, "-") != 0)
14149  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14150 
14151  if (strcmp(aggdeserialfn, "-") != 0)
14152  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14153 
14154  if (strcmp(aggmtransfn, "-") != 0)
14155  {
14156  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14157  aggmtransfn,
14158  aggminvtransfn,
14159  aggmtranstype);
14160  }
14161 
14162  if (strcmp(aggmtransspace, "0") != 0)
14163  {
14164  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14165  aggmtransspace);
14166  }
14167 
14168  if (!PQgetisnull(res, 0, i_aggminitval))
14169  {
14170  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14171  appendStringLiteralAH(details, aggminitval, fout);
14172  }
14173 
14174  if (strcmp(aggmfinalfn, "-") != 0)
14175  {
14176  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14177  aggmfinalfn);
14178  if (aggmfinalextra)
14179  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14180  if (aggmfinalmodify != defaultfinalmodify)
14181  {
14182  switch (aggmfinalmodify)
14183  {
14184  case AGGMODIFY_READ_ONLY:
14185  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14186  break;
14187  case AGGMODIFY_SHAREABLE:
14188  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14189  break;
14190  case AGGMODIFY_READ_WRITE:
14191  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14192  break;
14193  default:
14194  fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14195  agginfo->aggfn.dobj.name);
14196  break;
14197  }
14198  }
14199  }
14200 
14201  aggsortconvop = getFormattedOperatorName(aggsortop);
14202  if (aggsortconvop)
14203  {
14204  appendPQExpBuffer(details, ",\n SORTOP = %s",
14205  aggsortconvop);
14206  free(aggsortconvop);
14207  }
14208 
14209  if (aggkind == AGGKIND_HYPOTHETICAL)
14210  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14211 
14212  if (proparallel[0] != PROPARALLEL_UNSAFE)
14213  {
14214  if (proparallel[0] == PROPARALLEL_SAFE)
14215  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14216  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14217  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14218  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14219  fatal("unrecognized proparallel value for function \"%s\"",
14220  agginfo->aggfn.dobj.name);
14221  }
14222 
14223  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14224  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14225  aggsig);
14226 
14227  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14228  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14229  aggfullsig ? aggfullsig : aggsig, details->data);
14230 
14231  if (dopt->binary_upgrade)
14232  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
14233  "AGGREGATE", aggsig,
14234  agginfo->aggfn.dobj.namespace->dobj.name);
14235 
14236  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14237  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14238  agginfo->aggfn.dobj.dumpId,
14239  ARCHIVE_OPTS(.tag = aggsig_tag,
14240  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14241  .owner = agginfo->aggfn.rolname,
14242  .description = "AGGREGATE",
14243  .section = SECTION_PRE_DATA,
14244  .createStmt = q->data,
14245  .dropStmt = delq->data));
14246 
14247  /* Dump Aggregate Comments */
14248  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14249  dumpComment(fout, "AGGREGATE", aggsig,
14250  agginfo->aggfn.dobj.namespace->dobj.name,
14251  agginfo->aggfn.rolname,
14252  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14253 
14254  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14255  dumpSecLabel(fout, "AGGREGATE", aggsig,
14256  agginfo->aggfn.dobj.namespace->dobj.name,
14257  agginfo->aggfn.rolname,
14258  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14259 
14260  /*
14261  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14262  * command look like a function's GRANT; in particular this affects the
14263  * syntax for zero-argument aggregates and ordered-set aggregates.
14264  */
14265  free(aggsig);
14266 
14267  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14268 
14269  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14270  dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId,
14271  "FUNCTION", aggsig, NULL,
14272  agginfo->aggfn.dobj.namespace->dobj.name,
14273  agginfo->aggfn.rolname, agginfo->aggfn.proacl,
14274  agginfo->aggfn.rproacl,
14275  agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
14276 
14277  free(aggsig);
14278  if (aggfullsig)
14279  free(aggfullsig);
14280  free(aggsig_tag);
14281 
14282  PQclear(res);
14283 
14284  destroyPQExpBuffer(query);
14285  destroyPQExpBuffer(q);
14286  destroyPQExpBuffer(delq);
14287  destroyPQExpBuffer(details);
14288 }
#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:13899
static char * format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg)
Definition: pg_dump.c:11748
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:15086
static char * format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11842
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:12897
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:4775
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:246
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:304
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:14987
#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:9802
int remoteVersion
Definition: pg_backup.h:196

◆ dumpAttrDef()

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

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

16424 {
16425  DumpOptions *dopt = fout->dopt;
16426  TableInfo *tbinfo = adinfo->adtable;
16427  int adnum = adinfo->adnum;
16428  PQExpBuffer q;
16429  PQExpBuffer delq;
16430  char *qualrelname;
16431  char *tag;
16432  char *foreign;
16433 
16434  /* Skip if table definition not to be dumped */
16435  if (!tbinfo->dobj.dump || dopt->dataOnly)
16436  return;
16437 
16438  /* Skip if not "separate"; it was dumped in the table's definition */
16439  if (!adinfo->separate)
16440  return;
16441 
16442  q = createPQExpBuffer();
16443  delq = createPQExpBuffer();
16444 
16445  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16446 
16447  foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "";
16448 
16450  "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n",
16451  foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]),
16452  adinfo->adef_expr);
16453 
16454  appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n",
16455  foreign, qualrelname,
16456  fmtId(tbinfo->attnames[adnum - 1]));
16457 
16458  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16459 
16460  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16461  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16462  ARCHIVE_OPTS(.tag = tag,
16463  .namespace = tbinfo->dobj.namespace->dobj.name,
16464  .owner = tbinfo->rolname,
16465  .description = "DEFAULT",
16466  .section = SECTION_PRE_DATA,
16467  .createStmt = q->data,
16468  .dropStmt = delq->data));
16469 
16470  free(tag);
16471  destroyPQExpBuffer(q);
16472  destroyPQExpBuffer(delq);
16473  free(qualrelname);
16474 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:131
char relkind
Definition: pg_dump.h:271
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:266
DumpId dumpId
Definition: pg_dump.h:130
char ** attnames
Definition: pg_dump.h:309
DumpableObject dobj
Definition: pg_dump.h:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
bool separate
Definition: pg_dump.h:360
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:357
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
DumpableObject dobj
Definition: pg_dump.h:356
#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:359

◆ dumpBaseType()

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

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

10808 {
10809  DumpOptions *dopt = fout->dopt;
10811  PQExpBuffer delq = createPQExpBuffer();
10812  PQExpBuffer query = createPQExpBuffer();
10813  PGresult *res;
10814  char *qtypname;
10815  char *qualtypname;
10816  char *typlen;
10817  char *typinput;
10818  char *typoutput;
10819  char *typreceive;
10820  char *typsend;
10821  char *typmodin;
10822  char *typmodout;
10823  char *typanalyze;
10824  char *typsubscript;
10825  Oid typreceiveoid;
10826  Oid typsendoid;
10827  Oid typmodinoid;
10828  Oid typmodoutoid;
10829  Oid typanalyzeoid;
10830  Oid typsubscriptoid;
10831  char *typcategory;
10832  char *typispreferred;
10833  char *typdelim;
10834  char *typbyval;
10835  char *typalign;
10836  char *typstorage;
10837  char *typcollatable;
10838  char *typdefault;
10839  bool typdefault_is_literal = false;
10840 
10841  /* Fetch type-specific details */
10842  appendPQExpBufferStr(query, "SELECT typlen, "
10843  "typinput, typoutput, typreceive, typsend, "
10844  "typreceive::pg_catalog.oid AS typreceiveoid, "
10845  "typsend::pg_catalog.oid AS typsendoid, "
10846  "typanalyze, "
10847  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10848  "typdelim, typbyval, typalign, typstorage, ");
10849 
10850  if (fout->remoteVersion >= 80300)
10851  appendPQExpBufferStr(query,
10852  "typmodin, typmodout, "
10853  "typmodin::pg_catalog.oid AS typmodinoid, "
10854  "typmodout::pg_catalog.oid AS typmodoutoid, ");
10855  else
10856  appendPQExpBufferStr(query,
10857  "'-' AS typmodin, '-' AS typmodout, "
10858  "0 AS typmodinoid, 0 AS typmodoutoid, ");
10859 
10860  if (fout->remoteVersion >= 80400)
10861  appendPQExpBufferStr(query,
10862  "typcategory, typispreferred, ");
10863  else
10864  appendPQExpBufferStr(query,
10865  "'U' AS typcategory, false AS typispreferred, ");
10866 
10867  if (fout->remoteVersion >= 90100)
10868  appendPQExpBufferStr(query, "(typcollation <> 0) AS typcollatable, ");
10869  else
10870  appendPQExpBufferStr(query, "false AS typcollatable, ");
10871 
10872  if (fout->remoteVersion >= 140000)
10873  appendPQExpBufferStr(query,
10874  "typsubscript, "
10875  "typsubscript::pg_catalog.oid AS typsubscriptoid, ");
10876  else
10877  appendPQExpBufferStr(query,
10878  "'-' AS typsubscript, 0 AS typsubscriptoid, ");
10879 
10880  /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
10881  if (fout->remoteVersion >= 80400)
10882  appendPQExpBufferStr(query,
10883  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault ");
10884  else
10885  appendPQExpBufferStr(query,
10886  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault ");
10887 
10888  appendPQExpBuffer(query, "FROM pg_catalog.pg_type "
10889  "WHERE oid = '%u'::pg_catalog.oid",
10890  tyinfo->dobj.catId.oid);
10891 
10892  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10893 
10894  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10895  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10896  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10897  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10898  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10899  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10900  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10901  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10902  typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript"));
10903  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10904  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10905  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10906  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10907  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10908  typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid")));
10909  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10910  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10911  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10912  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10913  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10914  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10915  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10916  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10917  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10918  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10919  {
10920  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10921  typdefault_is_literal = true; /* it needs quotes */
10922  }
10923  else
10924  typdefault = NULL;
10925 
10926  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10927  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10928 
10929  /*
10930  * The reason we include CASCADE is that the circular dependency between
10931  * the type and its I/O functions makes it impossible to drop the type any
10932  * other way.
10933  */
10934  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
10935 
10936  /*
10937  * We might already have a shell type, but setting pg_type_oid is
10938  * harmless, and in any case we'd better set the array type OID.
10939  */
10940  if (dopt->binary_upgrade)
10942  tyinfo->dobj.catId.oid,
10943  false, false);
10944 
10946  "CREATE TYPE %s (\n"
10947  " INTERNALLENGTH = %s",
10948  qualtypname,
10949  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10950 
10951  /* regproc result is sufficiently quoted already */
10952  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
10953  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
10954  if (OidIsValid(typreceiveoid))
10955  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
10956  if (OidIsValid(typsendoid))
10957  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10958  if (OidIsValid(typmodinoid))
10959  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10960  if (OidIsValid(typmodoutoid))
10961  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10962  if (OidIsValid(typanalyzeoid))
10963  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10964 
10965  if (strcmp(typcollatable, "t") == 0)
10966  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10967 
10968  if (typdefault != NULL)
10969  {
10970  appendPQExpBufferStr(q, ",\n DEFAULT = ");
10971  if (typdefault_is_literal)
10972  appendStringLiteralAH(q, typdefault, fout);
10973  else
10974  appendPQExpBufferStr(q, typdefault);
10975  }
10976 
10977  if (OidIsValid(typsubscriptoid))
10978  appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript);
10979 
10980  if (OidIsValid(tyinfo->typelem))
10981  appendPQExpBuffer(q, ",\n ELEMENT = %s",
10982  getFormattedTypeName(fout, tyinfo->typelem,
10983  zeroIsError));
10984 
10985  if (strcmp(typcategory, "U") != 0)
10986  {
10987  appendPQExpBufferStr(q, ",\n CATEGORY = ");
10988  appendStringLiteralAH(q, typcategory, fout);
10989  }
10990 
10991  if (strcmp(typispreferred, "t") == 0)
10992  appendPQExpBufferStr(q, ",\n PREFERRED = true");
10993 
10994  if (typdelim && strcmp(typdelim, ",") != 0)
10995  {
10996  appendPQExpBufferStr(q, ",\n DELIMITER = ");
10997  appendStringLiteralAH(q, typdelim, fout);
10998  }
10999 
11000  if (*typalign == TYPALIGN_CHAR)
11001  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
11002  else if (*typalign == TYPALIGN_SHORT)
11003  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
11004  else if (*typalign == TYPALIGN_INT)
11005  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
11006  else if (*typalign == TYPALIGN_DOUBLE)
11007  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
11008 
11009  if (*typstorage == TYPSTORAGE_PLAIN)
11010  appendPQExpBufferStr(q, ",\n STORAGE = plain");
11011  else if (*typstorage == TYPSTORAGE_EXTERNAL)
11012  appendPQExpBufferStr(q, ",\n STORAGE = external");
11013  else if (*typstorage == TYPSTORAGE_EXTENDED)
11014  appendPQExpBufferStr(q, ",\n STORAGE = extended");
11015  else if (*typstorage == TYPSTORAGE_MAIN)
11016  appendPQExpBufferStr(q, ",\n STORAGE = main");
11017 
11018  if (strcmp(typbyval, "t") == 0)
11019  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
11020 
11021  appendPQExpBufferStr(q, "\n);\n");
11022 
11023  if (dopt->binary_upgrade)
11025  "TYPE", qtypname,
11026  tyinfo->dobj.namespace->dobj.name);
11027 
11028  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11029  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11030  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11031  .namespace = tyinfo->dobj.namespace->dobj.name,
11032  .owner = tyinfo->rolname,
11033  .description = "TYPE",
11034  .section = SECTION_PRE_DATA,
11035  .createStmt = q->data,
11036  .dropStmt = delq->data));
11037 
11038  /* Dump Type Comments and Security Labels */
11039  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11040  dumpComment(fout, "TYPE", qtypname,
11041  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11042  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11043 
11044  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11045  dumpSecLabel(fout, "TYPE", qtypname,
11046  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11047  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11048 
11049  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11050  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11051  qtypname, NULL,
11052  tyinfo->dobj.namespace->dobj.name,
11053  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11054  tyinfo->inittypacl, tyinfo->initrtypacl);
11055 
11056  PQclear(res);
11057  destroyPQExpBuffer(q);
11058  destroyPQExpBuffer(delq);
11059  destroyPQExpBuffer(query);
11060  free(qtypname);
11061  free(qualtypname);
11062 }
#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:177
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:178
char * typacl
Definition: pg_dump.h:175
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:15086
#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:4579
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:179
#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:4775
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:246
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:92
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:304
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:176
static const char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18587
char * rolname
Definition: pg_dump.h:174
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:14987
#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:9802
int remoteVersion
Definition: pg_backup.h:196

◆ dumpBlob()

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

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

3535 {
3536  PQExpBuffer cquery = createPQExpBuffer();
3537  PQExpBuffer dquery = createPQExpBuffer();
3538 
3539  appendPQExpBuffer(cquery,
3540  "SELECT pg_catalog.lo_create('%s');\n",
3541  binfo->dobj.name);
3542 
3543  appendPQExpBuffer(dquery,
3544  "SELECT pg_catalog.lo_unlink('%s');\n",
3545  binfo->dobj.name);
3546 
3547  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3548  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3549  ARCHIVE_OPTS(.tag = binfo->dobj.name,
3550  .owner = binfo->rolname,
3551  .description = "BLOB",
3552  .section = SECTION_PRE_DATA,
3553  .createStmt = cquery->data,
3554  .dropStmt = dquery->data));
3555 
3556  /* Dump comment if any */
3557  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3558  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3559  NULL, binfo->rolname,
3560  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3561 
3562  /* Dump security label if any */
3563  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3564  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3565  NULL, binfo->rolname,
3566  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3567 
3568  /* Dump ACL if any */
3569  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3570  dumpACL(fout, binfo->dobj.dumpId, InvalidDumpId, "LARGE OBJECT",
3571  binfo->dobj.name, NULL,
3572  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3573  binfo->initblobacl, binfo->initrblobacl);
3574 
3575  destroyPQExpBuffer(cquery);
3576  destroyPQExpBuffer(dquery);
3577 }
#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:15086
char * initblobacl
Definition: pg_dump.h:586
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject dobj
Definition: pg_dump.h:582
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:583
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define InvalidDumpId
Definition: pg_backup.h:246
#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:14987
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:93
char * blobacl
Definition: pg_dump.h:584
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:94
CatalogId catId
Definition: pg_dump.h:129
char * rblobacl
Definition: pg_dump.h:585
char * initrblobacl
Definition: pg_dump.h:587
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:9802

◆ dumpBlobs()

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

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

3585 {
3586  const char *blobQry;
3587  const char *blobFetchQry;
3588  PGconn *conn = GetConnection(fout);
3589  PGresult *res;
3590  char buf[LOBBUFSIZE];
3591  int ntups;
3592  int i;
3593  int cnt;
3594 
3595  pg_log_info("saving large objects");
3596 
3597  /*
3598  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3599  * the already-in-memory dumpable objects instead...
3600  */
3601  if (fout->remoteVersion >= 90000)
3602  blobQry =
3603  "DECLARE bloboid CURSOR FOR "
3604  "SELECT oid FROM pg_largeobject_metadata ORDER BY 1";
3605  else
3606  blobQry =
3607  "DECLARE bloboid CURSOR FOR "
3608  "SELECT DISTINCT loid FROM pg_largeobject ORDER BY 1";
3609 
3610  ExecuteSqlStatement(fout, blobQry);
3611 
3612  /* Command to fetch from cursor */
3613  blobFetchQry = "FETCH 1000 IN bloboid";
3614 
3615  do
3616  {
3617  /* Do a fetch */
3618  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3619 
3620  /* Process the tuples, if any */
3621  ntups = PQntuples(res);
3622  for (i = 0; i < ntups; i++)
3623  {
3624  Oid blobOid;
3625  int loFd;
3626 
3627  blobOid = atooid(PQgetvalue(res, i, 0));
3628  /* Open the BLOB */
3629  loFd = lo_open(conn, blobOid, INV_READ);
3630  if (loFd == -1)
3631  fatal("could not open large object %u: %s",
3632  blobOid, PQerrorMessage(conn));
3633 
3634  StartBlob(fout, blobOid);
3635 
3636  /* Now read it in chunks, sending data to archive */
3637  do
3638  {
3639  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3640  if (cnt < 0)
3641  fatal("error reading large object %u: %s",
3642  blobOid, PQerrorMessage(conn));
3643 
3644  WriteData(fout, buf, cnt);
3645  } while (cnt > 0);
3646 
3647  lo_close(conn, loFd);
3648 
3649  EndBlob(fout, blobOid);
3650  }
3651 
3652  PQclear(res);
3653  } while (ntups > 0);
3654 
3655  return 1;
3656 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
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:127
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 12397 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().

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

◆ dumpCollation()

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

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

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

9806 {
9807  dumpCommentExtended(fout, type, name, namespace, owner,
9808  catalogId, subid, dumpId, NULL);
9809 }
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:9702
const char * name
Definition: encode.c:561

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

9707 {
9708  DumpOptions *dopt = fout->dopt;
9709  CommentItem *comments;
9710  int ncomments;
9711 
9712  /* do nothing, if --no-comments is supplied */
9713  if (dopt->no_comments)
9714  return;
9715 
9716  /* Comments are schema not data ... except blob comments are data */
9717  if (strcmp(type, "LARGE OBJECT") != 0)
9718  {
9719  if (dopt->dataOnly)
9720  return;
9721  }
9722  else
9723  {
9724  /* We do dump blob comments in binary-upgrade mode */
9725  if (dopt->schemaOnly && !dopt->binary_upgrade)
9726  return;
9727  }
9728 
9729  /* Search for comments associated with catalogId, using table */
9730  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9731  &comments);
9732 
9733  /* Is there one matching the subid? */
9734  while (ncomments > 0)
9735  {
9736  if (comments->objsubid == subid)
9737  break;
9738  comments++;
9739  ncomments--;
9740  }
9741 
9742  if (initdb_comment != NULL)
9743  {
9744  static CommentItem empty_comment = {.descr = ""};
9745 
9746  /*
9747  * initdb creates this object with a comment. Skip dumping the
9748  * initdb-provided comment, which would complicate matters for
9749  * non-superuser use of pg_dump. When the DBA has removed initdb's
9750  * comment, replicate that.
9751  */
9752  if (ncomments == 0)
9753  {
9754  comments = &empty_comment;
9755  ncomments = 1;
9756  }
9757  else if (strcmp(comments->descr, initdb_comment) == 0)
9758  ncomments = 0;
9759  }
9760 
9761  /* If a comment exists, build COMMENT ON statement */
9762  if (ncomments > 0)
9763  {
9764  PQExpBuffer query = createPQExpBuffer();
9766 
9767  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9768  if (namespace && *namespace)
9769  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9770  appendPQExpBuffer(query, "%s IS ", name);
9771  appendStringLiteralAH(query, comments->descr, fout);
9772  appendPQExpBufferStr(query, ";\n");
9773 
9774  appendPQExpBuffer(tag, "%s %s", type, name);
9775 
9776  /*
9777  * We mark comments as SECTION_NONE because they really belong in the
9778  * same section as their parent, whether that is pre-data or
9779  * post-data.
9780  */
9782  ARCHIVE_OPTS(.tag = tag->data,
9783  .namespace = namespace,
9784  .owner = owner,
9785  .description = "COMMENT",
9786  .section = SECTION_NONE,
9787  .createStmt = query->data,
9788  .deps = &dumpId,
9789  .nDeps = 1));
9790 
9791  destroyPQExpBuffer(query);
9792  destroyPQExpBuffer(tag);
9793  }
9794 }
#define ARCHIVE_OPTS(...)
Oid tableoid
Definition: pg_backup.h:240
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:9917
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:304
int no_comments
Definition: pg_backup.h:157
const char * name
Definition: encode.c:561
static const CatalogId nilCatalogId
Definition: pg_dump.c:131
DumpId createDumpId(void)
Definition: common.c:641

◆ dumpCompositeType()

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

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

11245 {
11246  DumpOptions *dopt = fout->dopt;
11248  PQExpBuffer dropped = createPQExpBuffer();
11249  PQExpBuffer delq = createPQExpBuffer();
11250  PQExpBuffer query = createPQExpBuffer();
11251  PGresult *res;
11252  char *qtypname;
11253  char *qualtypname;
11254  int ntups;
11255  int i_attname;
11256  int i_atttypdefn;
11257  int i_attlen;
11258  int i_attalign;
11259  int i_attisdropped;
11260  int i_attcollation;
11261  int i;
11262  int actual_atts;
11263 
11264  /* Fetch type specific details */
11265  if (fout->remoteVersion >= 90100)
11266  {
11267  /*
11268  * attcollation is new in 9.1. Since we only want to dump COLLATE
11269  * clauses for attributes whose collation is different from their
11270  * type's default, we use a CASE here to suppress uninteresting
11271  * attcollations cheaply. atttypid will be 0 for dropped columns;
11272  * collation does not matter for those.
11273  */
11274  appendPQExpBuffer(query, "SELECT a.attname, "
11275  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11276  "a.attlen, a.attalign, a.attisdropped, "
11277  "CASE WHEN a.attcollation <> at.typcollation "
11278  "THEN a.attcollation ELSE 0 END AS attcollation "
11279  "FROM pg_catalog.pg_type ct "
11280  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11281  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11282  "WHERE ct.oid = '%u'::pg_catalog.oid "
11283  "ORDER BY a.attnum ",
11284  tyinfo->dobj.catId.oid);
11285  }
11286  else
11287  {
11288  /*
11289  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
11290  * should always be false.
11291  */
11292  appendPQExpBuffer(query, "SELECT a.attname, "
11293  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11294  "a.attlen, a.attalign, a.attisdropped, "
11295  "0 AS attcollation "
11296  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
11297  "WHERE ct.oid = '%u'::pg_catalog.oid "
11298  "AND a.attrelid = ct.typrelid "
11299  "ORDER BY a.attnum ",
11300  tyinfo->dobj.catId.oid);
11301  }
11302 
11303  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11304 
11305  ntups = PQntuples(res);
11306 
11307  i_attname = PQfnumber(res, "attname");
11308  i_atttypdefn = PQfnumber(res, "atttypdefn");
11309  i_attlen = PQfnumber(res, "attlen");
11310  i_attalign = PQfnumber(res, "attalign");
11311  i_attisdropped = PQfnumber(res, "attisdropped");
11312  i_attcollation = PQfnumber(res, "attcollation");
11313 
11314  if (dopt->binary_upgrade)
11315  {
11317  tyinfo->dobj.catId.oid,
11318  false, false);
11319  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11320  }
11321 
11322  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11323  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11324 
11325  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11326  qualtypname);
11327 
11328  actual_atts = 0;
11329  for (i = 0; i < ntups; i++)
11330  {
11331  char *attname;
11332  char *atttypdefn;
11333  char *attlen;
11334  char *attalign;
11335  bool attisdropped;
11336  Oid attcollation;
11337 
11338  attname = PQgetvalue(res, i, i_attname);
11339  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11340  attlen = PQgetvalue(res, i, i_attlen);
11341  attalign = PQgetvalue(res, i, i_attalign);
11342  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11343  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11344 
11345  if (attisdropped && !dopt->binary_upgrade)
11346  continue;
11347 
11348  /* Format properly if not first attr */
11349  if (actual_atts++ > 0)
11350  appendPQExpBufferChar(q, ',');
11351  appendPQExpBufferStr(q, "\n\t");
11352 
11353  if (!attisdropped)
11354  {
11355  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11356 
11357  /* Add collation if not default for the column type */
11358  if (OidIsValid(attcollation))
11359  {
11360  CollInfo *coll;
11361 
11362  coll = findCollationByOid(attcollation);
11363  if (coll)
11364  appendPQExpBuffer(q, " COLLATE %s",
11365  fmtQualifiedDumpable(coll));
11366  }
11367  }
11368  else
11369  {
11370  /*
11371  * This is a dropped attribute and we're in binary_upgrade mode.
11372  * Insert a placeholder for it in the CREATE TYPE command, and set
11373  * length and alignment with direct UPDATE to the catalogs
11374  * afterwards. See similar code in dumpTableSchema().
11375  */
11376  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11377 
11378  /* stash separately for insertion after the CREATE TYPE */
11379  appendPQExpBufferStr(dropped,
11380  "\n-- For binary upgrade, recreate dropped column.\n");
11381  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11382  "SET attlen = %s, "
11383  "attalign = '%s', attbyval = false\n"
11384  "WHERE attname = ", attlen, attalign);
11385  appendStringLiteralAH(dropped, attname, fout);
11386  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11387  appendStringLiteralAH(dropped, qualtypname, fout);
11388  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11389 
11390  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11391  qualtypname);
11392  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11393  fmtId(attname));
11394  }
11395  }
11396  appendPQExpBufferStr(q, "\n);\n");
11397  appendPQExpBufferStr(q, dropped->data);
11398 
11399  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11400 
11401  if (dopt->binary_upgrade)
11403  "TYPE", qtypname,
11404  tyinfo->dobj.namespace->dobj.name);
11405 
11406  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11407  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11408  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11409  .namespace = tyinfo->dobj.namespace->dobj.name,
11410  .owner = tyinfo->rolname,
11411  .description = "TYPE",
11412  .section = SECTION_PRE_DATA,
11413  .createStmt = q->data,
11414  .dropStmt = delq->data));
11415 
11416 
11417  /* Dump Type Comments and Security Labels */
11418  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11419  dumpComment(fout, "TYPE", qtypname,
11420  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11421  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11422 
11423  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11424  dumpSecLabel(fout, "TYPE", qtypname,
11425  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11426  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11427 
11428  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11429  dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE",
11430  qtypname, NULL,
11431  tyinfo->dobj.namespace->dobj.name,
11432  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11433  tyinfo->inittypacl, tyinfo->initrtypacl);
11434 
11435  PQclear(res);
11436  destroyPQExpBuffer(q);
11437  destroyPQExpBuffer(dropped);
11438  destroyPQExpBuffer(delq);
11439  destroyPQExpBuffer(query);
11440  free(qtypname);
11441  free(qualtypname);
11442 
11443  /* Dump any per-column comments */
11444  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11445  dumpCompositeTypeColComments(fout, tyinfo);
11446 }
#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:177
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
Oid typrelid
Definition: pg_dump.h:180
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrtypacl
Definition: pg_dump.h:178
char * typacl
Definition: pg_dump.h:175
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:15086
#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:4579
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:4692
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:4775
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:246
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:304
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:176
char * rolname
Definition: pg_dump.h:174
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:853
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:14987
#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:11454
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:9802
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 11454 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().

11455 {
11456  CommentItem *comments;
11457  int ncomments;
11458  PGresult *res;
11459  PQExpBuffer query;
11460  PQExpBuffer target;
11461  Oid pgClassOid;
11462  int i;
11463  int ntups;
11464  int i_attname;
11465  int i_attnum;
11466 
11467  /* do nothing, if --no-comments is supplied */
11468  if (fout->dopt->no_comments)
11469  return;
11470 
11471  query = createPQExpBuffer();
11472 
11473  appendPQExpBuffer(query,
11474  "SELECT c.tableoid, a.attname, a.attnum "
11475  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
11476  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
11477  " AND NOT a.attisdropped "
11478  "ORDER BY a.attnum ",
11479  tyinfo->typrelid);
11480 
11481  /* Fetch column attnames */
11482  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11483 
11484  ntups = PQntuples(res);
11485  if (ntups < 1)
11486  {
11487  PQclear(res);
11488  destroyPQExpBuffer(query);
11489  return;
11490  }
11491 
11492  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
11493 
11494  /* Search for comments associated with type's pg_class OID */
11495  ncomments = findComments(fout,
11496  pgClassOid,
11497  tyinfo->typrelid,
11498  &comments);
11499 
11500  /* If no comments exist, we're done */
11501  if (ncomments <= 0)
11502  {
11503  PQclear(res);
11504  destroyPQExpBuffer(query);
11505  return;
11506  }
11507 
11508  /* Build COMMENT ON statements */
11509  target = createPQExpBuffer();
11510 
11511  i_attnum = PQfnumber(res, "attnum");
11512  i_attname = PQfnumber(res, "attname");
11513  while (ncomments > 0)
11514  {
11515  const char *attname;
11516 
11517  attname = NULL;
11518  for (i = 0; i < ntups; i++)
11519  {
11520  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
11521  {
11522  attname = PQgetvalue(res, i, i_attname);
11523  break;
11524  }
11525  }
11526  if (attname) /* just in case we don't find it */
11527  {
11528  const char *descr = comments->descr;
11529 
11530  resetPQExpBuffer(target);
11531  appendPQExpBuffer(target, "COLUMN %s.",
11532  fmtId(tyinfo->dobj.name));
11533  appendPQExpBufferStr(target, fmtId(attname));
11534 
11535  resetPQExpBuffer(query);
11536  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11537  fmtQualifiedDumpable(tyinfo));
11538  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11539  appendStringLiteralAH(query, descr, fout);
11540  appendPQExpBufferStr(query, ";\n");
11541 
11543  ARCHIVE_OPTS(.tag = target->data,
11544  .namespace = tyinfo->dobj.namespace->dobj.name,
11545  .owner = tyinfo->rolname,
11546  .description = "COMMENT",
11547  .section = SECTION_NONE,
11548  .createStmt = query->data,
11549  .deps = &(tyinfo->dobj.dumpId),
11550  .nDeps = 1));
11551  }
11552 
11553  comments++;
11554  ncomments--;
11555  }
11556 
11557  PQclear(res);
11558  destroyPQExpBuffer(query);
11559  destroyPQExpBuffer(target);
11560 }
#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:180
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:9917
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:304
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
char * rolname
Definition: pg_dump.h:174
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:641

◆ dumpConstraint()

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

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

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

◆ dumpConversion()

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

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

13804 {
13805  DumpOptions *dopt = fout->dopt;
13806  PQExpBuffer query;
13807  PQExpBuffer q;
13808  PQExpBuffer delq;
13809  char *qconvname;
13810  PGresult *res;
13811  int i_conforencoding;
13812  int i_contoencoding;
13813  int i_conproc;
13814  int i_condefault;
13815  const char *conforencoding;
13816  const char *contoencoding;
13817  const char *conproc;
13818  bool condefault;
13819 
13820  /* Skip if not to be dumped */
13821  if (!convinfo->dobj.dump || dopt->dataOnly)
13822  return;
13823 
13824  query = createPQExpBuffer();
13825  q = createPQExpBuffer();
13826  delq = createPQExpBuffer();
13827 
13828  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13829 
13830  /* Get conversion-specific details */
13831  appendPQExpBuffer(query, "SELECT "
13832  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13833  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13834  "conproc, condefault "
13835  "FROM pg_catalog.pg_conversion c "
13836  "WHERE c.oid = '%u'::pg_catalog.oid",
13837  convinfo->dobj.catId.oid);
13838 
13839  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13840 
13841  i_conforencoding = PQfnumber(res, "conforencoding");
13842  i_contoencoding = PQfnumber(res, "contoencoding");
13843  i_conproc = PQfnumber(res, "conproc");
13844  i_condefault = PQfnumber(res, "condefault");
13845 
13846  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13847  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13848  conproc = PQgetvalue(res, 0, i_conproc);
13849  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13850 
13851  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13852  fmtQualifiedDumpable(convinfo));
13853 
13854  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13855  (condefault) ? "DEFAULT " : "",
13856  fmtQualifiedDumpable(convinfo));
13857  appendStringLiteralAH(q, conforencoding, fout);
13858  appendPQExpBufferStr(q, " TO ");
13859  appendStringLiteralAH(q, contoencoding, fout);
13860  /* regproc output is already sufficiently quoted */
13861  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13862 
13863  if (dopt->binary_upgrade)
13864  binary_upgrade_extension_member(q, &convinfo->dobj,
13865  "CONVERSION", qconvname,
13866  convinfo->dobj.namespace->dobj.name);
13867 
13868  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13869  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13870  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13871  .namespace = convinfo->dobj.namespace->dobj.name,
13872  .owner = convinfo->rolname,
13873  .description = "CONVERSION",
13874  .section = SECTION_PRE_DATA,
13875  .createStmt = q->data,
13876  .dropStmt = delq->data));
13877 
13878  /* Dump Conversion Comments */
13879  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13880  dumpComment(fout, "CONVERSION", qconvname,
13881  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13882  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13883 
13884  PQclear(res);
13885 
13886  destroyPQExpBuffer(query);
13887  destroyPQExpBuffer(q);
13888  destroyPQExpBuffer(delq);
13889  free(qconvname);
13890 }
#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:257
DumpId dumpId
Definition: pg_dump.h:130
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
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: