PostgreSQL Source Code  git master
pg_dump.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include "getopt_long.h"
#include "access/attnum.h"
#include "access/sysattr.h"
#include "access/transam.h"
#include "catalog/pg_aggregate_d.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_attribute_d.h"
#include "catalog/pg_cast_d.h"
#include "catalog/pg_class_d.h"
#include "catalog/pg_default_acl_d.h"
#include "catalog/pg_largeobject_d.h"
#include "catalog/pg_largeobject_metadata_d.h"
#include "catalog/pg_proc_d.h"
#include "catalog/pg_trigger_d.h"
#include "catalog/pg_type_d.h"
#include "libpq/libpq-fs.h"
#include "storage/block.h"
#include "dumputils.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "fe_utils/connect.h"
#include "fe_utils/string_utils.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 { zeroAsOpaque = 1, zeroAsAny = 2, zeroAsStar = 4, zeroAsNone = 8 }
 

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

Variables

static bool dosync = true
 
static const char * username_subquery
 
static Oid g_last_builtin_oid
 
static int strict_names = 0
 
static SimpleStringList schema_include_patterns = {NULL, NULL}
 
static SimpleOidList schema_include_oids = {NULL, NULL}
 
static SimpleStringList schema_exclude_patterns = {NULL, NULL}
 
static SimpleOidList schema_exclude_oids = {NULL, NULL}
 
static SimpleStringList table_include_patterns = {NULL, NULL}
 
static SimpleOidList table_include_oids = {NULL, NULL}
 
static SimpleStringList table_exclude_patterns = {NULL, NULL}
 
static SimpleOidList table_exclude_oids = {NULL, NULL}
 
static SimpleStringList tabledata_exclude_patterns = {NULL, NULL}
 
static SimpleOidList tabledata_exclude_oids = {NULL, NULL}
 
char g_opaque_type [10]
 
char g_comment_start [10]
 
char g_comment_end [10]
 
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 144 of file pg_dump.c.

Referenced by main().

◆ fmtQualifiedDumpable

Typedef Documentation

◆ OidOptions

Enumeration Type Documentation

◆ OidOptions

enum OidOptions
Enumerator
zeroAsOpaque 
zeroAsAny 
zeroAsStar 
zeroAsNone 

Definition at line 86 of file pg_dump.c.

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

Function Documentation

◆ addBoundaryDependencies()

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

Definition at line 18070 of file pg_dump.c.

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

Referenced by main().

18072 {
18073  DumpableObject *preDataBound = boundaryObjs + 0;
18074  DumpableObject *postDataBound = boundaryObjs + 1;
18075  int i;
18076 
18077  for (i = 0; i < numObjs; i++)
18078  {
18079  DumpableObject *dobj = dobjs[i];
18080 
18081  /*
18082  * The classification of object types here must match the SECTION_xxx
18083  * values assigned during subsequent ArchiveEntry calls!
18084  */
18085  switch (dobj->objType)
18086  {
18087  case DO_NAMESPACE:
18088  case DO_EXTENSION:
18089  case DO_TYPE:
18090  case DO_SHELL_TYPE:
18091  case DO_FUNC:
18092  case DO_AGG:
18093  case DO_OPERATOR:
18094  case DO_ACCESS_METHOD:
18095  case DO_OPCLASS:
18096  case DO_OPFAMILY:
18097  case DO_COLLATION:
18098  case DO_CONVERSION:
18099  case DO_TABLE:
18100  case DO_ATTRDEF:
18101  case DO_PROCLANG:
18102  case DO_CAST:
18103  case DO_DUMMY_TYPE:
18104  case DO_TSPARSER:
18105  case DO_TSDICT:
18106  case DO_TSTEMPLATE:
18107  case DO_TSCONFIG:
18108  case DO_FDW:
18109  case DO_FOREIGN_SERVER:
18110  case DO_TRANSFORM:
18111  case DO_BLOB:
18112  /* Pre-data objects: must come before the pre-data boundary */
18113  addObjectDependency(preDataBound, dobj->dumpId);
18114  break;
18115  case DO_TABLE_DATA:
18116  case DO_SEQUENCE_SET:
18117  case DO_BLOB_DATA:
18118  /* Data objects: must come between the boundaries */
18119  addObjectDependency(dobj, preDataBound->dumpId);
18120  addObjectDependency(postDataBound, dobj->dumpId);
18121  break;
18122  case DO_INDEX:
18123  case DO_INDEX_ATTACH:
18124  case DO_STATSEXT:
18125  case DO_REFRESH_MATVIEW:
18126  case DO_TRIGGER:
18127  case DO_EVENT_TRIGGER:
18128  case DO_DEFAULT_ACL:
18129  case DO_POLICY:
18130  case DO_PUBLICATION:
18131  case DO_PUBLICATION_REL:
18132  case DO_SUBSCRIPTION:
18133  /* Post-data objects: must come after the post-data boundary */
18134  addObjectDependency(dobj, postDataBound->dumpId);
18135  break;
18136  case DO_RULE:
18137  /* Rules are post-data, but only if dumped separately */
18138  if (((RuleInfo *) dobj)->separate)
18139  addObjectDependency(dobj, postDataBound->dumpId);
18140  break;
18141  case DO_CONSTRAINT:
18142  case DO_FK_CONSTRAINT:
18143  /* Constraints are post-data, but only if dumped separately */
18144  if (((ConstraintInfo *) dobj)->separate)
18145  addObjectDependency(dobj, postDataBound->dumpId);
18146  break;
18147  case DO_PRE_DATA_BOUNDARY:
18148  /* nothing to do */
18149  break;
18150  case DO_POST_DATA_BOUNDARY:
18151  /* must come after the pre-data boundary */
18152  addObjectDependency(dobj, preDataBound->dumpId);
18153  break;
18154  }
18155  }
18156 }
DumpId dumpId
Definition: pg_dump.h:129
Definition: pg_dump.h:45
Definition: pg_dump.h:70
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:780
int i
DumpableObjectType objType
Definition: pg_dump.h:127

◆ appendReloptionsArrayAH()

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

Definition at line 18373 of file pg_dump.c.

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

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

18375 {
18376  bool res;
18377 
18378  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18379  fout->std_strings);
18380  if (!res)
18381  pg_log_warning("could not parse reloptions array");
18382 }
int encoding
Definition: pg_backup.h:197
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:743
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool std_strings
Definition: pg_backup.h:198

◆ binary_upgrade_extension_member()

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

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

4478 {
4479  DumpableObject *extobj = NULL;
4480  int i;
4481 
4482  if (!dobj->ext_member)
4483  return;
4484 
4485  /*
4486  * Find the parent extension. We could avoid this search if we wanted to
4487  * add a link field to DumpableObject, but the space costs of that would
4488  * be considerable. We assume that member objects could only have a
4489  * direct dependency on their own extension, not any others.
4490  */
4491  for (i = 0; i < dobj->nDeps; i++)
4492  {
4493  extobj = findObjectByDumpId(dobj->dependencies[i]);
4494  if (extobj && extobj->objType == DO_EXTENSION)
4495  break;
4496  extobj = NULL;
4497  }
4498  if (extobj == NULL)
4499  fatal("could not find parent extension for %s %s",
4500  objtype, objname);
4501 
4502  appendPQExpBufferStr(upgrade_buffer,
4503  "\n-- For binary upgrade, handle extension membership the hard way\n");
4504  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4505  fmtId(extobj->name),
4506  objtype);
4507  if (objnamespace && *objnamespace)
4508  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4509  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4510 }
char * name
Definition: pg_dump.h:130
DumpId * dependencies
Definition: pg_dump.h:135
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
bool ext_member
Definition: pg_dump.h:134
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:608
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define fatal(...)
int i
DumpableObjectType objType
Definition: pg_dump.h:127

◆ binary_upgrade_set_pg_class_oids()

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

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

4406 {
4407  PQExpBuffer upgrade_query = createPQExpBuffer();
4408  PGresult *upgrade_res;
4409  Oid pg_class_reltoastrelid;
4410  Oid pg_index_indexrelid;
4411 
4412  appendPQExpBuffer(upgrade_query,
4413  "SELECT c.reltoastrelid, i.indexrelid "
4414  "FROM pg_catalog.pg_class c LEFT JOIN "
4415  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4416  "WHERE c.oid = '%u'::pg_catalog.oid;",
4417  pg_class_oid);
4418 
4419  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4420 
4421  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
4422  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
4423 
4424  appendPQExpBufferStr(upgrade_buffer,
4425  "\n-- For binary upgrade, must preserve pg_class oids\n");
4426 
4427  if (!is_index)
4428  {
4429  appendPQExpBuffer(upgrade_buffer,
4430  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4431  pg_class_oid);
4432  /* only tables have toast tables, not indexes */
4433  if (OidIsValid(pg_class_reltoastrelid))
4434  {
4435  /*
4436  * One complexity is that the table definition might not require
4437  * the creation of a TOAST table, and the TOAST table might have
4438  * been created long after table creation, when the table was
4439  * loaded with wide data. By setting the TOAST oid we force
4440  * creation of the TOAST heap and TOAST index by the backend so we
4441  * can cleanly copy the files during binary upgrade.
4442  */
4443 
4444  appendPQExpBuffer(upgrade_buffer,
4445  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4446  pg_class_reltoastrelid);
4447 
4448  /* every toast table has an index */
4449  appendPQExpBuffer(upgrade_buffer,
4450  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4451  pg_index_indexrelid);
4452  }
4453  }
4454  else
4455  appendPQExpBuffer(upgrade_buffer,
4456  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4457  pg_class_oid);
4458 
4459  appendPQExpBufferChar(upgrade_buffer, '\n');
4460 
4461  PQclear(upgrade_res);
4462  destroyPQExpBuffer(upgrade_query);
4463 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
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:638
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
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:2878
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void PQclear(PGresult *res)
Definition: fe-exec.c:695

◆ binary_upgrade_set_type_oids_by_rel_oid()

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

Definition at line 4351 of file pg_dump.c.

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

Referenced by dumpSequence(), and dumpTableSchema().

4354 {
4355  PQExpBuffer upgrade_query = createPQExpBuffer();
4356  PGresult *upgrade_res;
4357  Oid pg_type_oid;
4358  bool toast_set = false;
4359 
4360  /*
4361  * We only support old >= 8.3 for binary upgrades.
4362  *
4363  * We purposefully ignore toast OIDs for partitioned tables; the reason is
4364  * that versions 10 and 11 have them, but 12 does not, so emitting them
4365  * causes the upgrade to fail.
4366  */
4367  appendPQExpBuffer(upgrade_query,
4368  "SELECT c.reltype AS crel, t.reltype AS trel "
4369  "FROM pg_catalog.pg_class c "
4370  "LEFT JOIN pg_catalog.pg_class t ON "
4371  " (c.reltoastrelid = t.oid AND c.relkind <> '%c') "
4372  "WHERE c.oid = '%u'::pg_catalog.oid;",
4373  RELKIND_PARTITIONED_TABLE, pg_rel_oid);
4374 
4375  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4376 
4377  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4378 
4379  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4380  pg_type_oid, false);
4381 
4382  if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
4383  {
4384  /* Toast tables do not have pg_type array rows */
4385  Oid pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
4386  PQfnumber(upgrade_res, "trel")));
4387 
4388  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
4389  appendPQExpBuffer(upgrade_buffer,
4390  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4391  pg_type_toast_oid);
4392 
4393  toast_set = true;
4394  }
4395 
4396  PQclear(upgrade_res);
4397  destroyPQExpBuffer(upgrade_query);
4398 
4399  return toast_set;
4400 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned int Oid
Definition: postgres_ext.h:31
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
#define atooid(x)
Definition: postgres_ext.h:42
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type)
Definition: pg_dump.c:4282
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
void PQclear(PGresult *res)
Definition: fe-exec.c:695
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189

◆ binary_upgrade_set_type_oids_by_type_oid()

static void binary_upgrade_set_type_oids_by_type_oid ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_type_oid,
bool  force_array_type 
)
static

Definition at line 4282 of file pg_dump.c.

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

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

4286 {
4287  PQExpBuffer upgrade_query = createPQExpBuffer();
4288  PGresult *res;
4289  Oid pg_type_array_oid;
4290 
4291  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4292  appendPQExpBuffer(upgrade_buffer,
4293  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4294  pg_type_oid);
4295 
4296  /* we only support old >= 8.3 for binary upgrades */
4297  appendPQExpBuffer(upgrade_query,
4298  "SELECT typarray "
4299  "FROM pg_catalog.pg_type "
4300  "WHERE oid = '%u'::pg_catalog.oid;",
4301  pg_type_oid);
4302 
4303  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4304 
4305  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4306 
4307  PQclear(res);
4308 
4309  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4310  {
4311  /*
4312  * If the old version didn't assign an array type, but the new version
4313  * does, we must select an unused type OID to assign. This currently
4314  * only happens for domains, when upgrading pre-v11 to v11 and up.
4315  *
4316  * Note: local state here is kind of ugly, but we must have some,
4317  * since we mustn't choose the same unused OID more than once.
4318  */
4319  static Oid next_possible_free_oid = FirstNormalObjectId;
4320  bool is_dup;
4321 
4322  do
4323  {
4324  ++next_possible_free_oid;
4325  printfPQExpBuffer(upgrade_query,
4326  "SELECT EXISTS(SELECT 1 "
4327  "FROM pg_catalog.pg_type "
4328  "WHERE oid = '%u'::pg_catalog.oid);",
4329  next_possible_free_oid);
4330  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4331  is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
4332  PQclear(res);
4333  } while (is_dup);
4334 
4335  pg_type_array_oid = next_possible_free_oid;
4336  }
4337 
4338  if (OidIsValid(pg_type_array_oid))
4339  {
4340  appendPQExpBufferStr(upgrade_buffer,
4341  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4342  appendPQExpBuffer(upgrade_buffer,
4343  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4344  pg_type_array_oid);
4345  }
4346 
4347  destroyPQExpBuffer(upgrade_query);
4348 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
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:638
#define FirstNormalObjectId
Definition: transam.h:141
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
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:2878
void PQclear(PGresult *res)
Definition: fe-exec.c:695

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

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

18184 {
18185  ArchiveHandle *AH = (ArchiveHandle *) fout;
18186  TocEntry *te;
18187 
18188  /* Scan all TOC entries in the archive */
18189  for (te = AH->toc->next; te != AH->toc; te = te->next)
18190  {
18191  DumpableObject *dobj;
18192  DumpId *dependencies;
18193  int nDeps;
18194  int allocDeps;
18195 
18196  /* No need to process entries that will not be dumped */
18197  if (te->reqs == 0)
18198  continue;
18199  /* Ignore entries that already have "special" dependencies */
18200  if (te->nDeps > 0)
18201  continue;
18202  /* Otherwise, look up the item's original DumpableObject, if any */
18203  dobj = findObjectByDumpId(te->dumpId);
18204  if (dobj == NULL)
18205  continue;
18206  /* No work if it has no dependencies */
18207  if (dobj->nDeps <= 0)
18208  continue;
18209  /* Set up work array */
18210  allocDeps = 64;
18211  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18212  nDeps = 0;
18213  /* Recursively find all dumpable dependencies */
18214  findDumpableDependencies(AH, dobj,
18215  &dependencies, &nDeps, &allocDeps);
18216  /* And save 'em ... */
18217  if (nDeps > 0)
18218  {
18219  dependencies = (DumpId *) pg_realloc(dependencies,
18220  nDeps * sizeof(DumpId));
18221  te->dependencies = dependencies;
18222  te->nDeps = nDeps;
18223  }
18224  else
18225  free(dependencies);
18226  }
18227 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:234
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:608
struct _tocEntry * toc
DumpId * dependencies
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
static void findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj, DumpId **dependencies, int *nDeps, int *allocDeps)
Definition: pg_dump.c:18231
#define free(a)
Definition: header.h:65

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

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

2397 {
2398  PQExpBuffer query;
2399  PGresult *res;
2400  int ntups,
2401  i;
2402  int i_classid,
2403  i_objid,
2404  i_refobjid;
2405 
2406  /* No Mat Views before 9.3. */
2407  if (fout->remoteVersion < 90300)
2408  return;
2409 
2410  query = createPQExpBuffer();
2411 
2412  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2413  "( "
2414  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2415  "FROM pg_depend d1 "
2416  "JOIN pg_class c1 ON c1.oid = d1.objid "
2417  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2418  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2419  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2420  "AND d2.objid = r1.oid "
2421  "AND d2.refobjid <> d1.objid "
2422  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2423  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2424  CppAsString2(RELKIND_VIEW) ") "
2425  "WHERE d1.classid = 'pg_class'::regclass "
2426  "UNION "
2427  "SELECT w.objid, d3.refobjid, c3.relkind "
2428  "FROM w "
2429  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2430  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2431  "AND d3.objid = r3.oid "
2432  "AND d3.refobjid <> w.refobjid "
2433  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2434  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2435  CppAsString2(RELKIND_VIEW) ") "
2436  ") "
2437  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2438  "FROM w "
2439  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2440 
2441  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2442 
2443  ntups = PQntuples(res);
2444 
2445  i_classid = PQfnumber(res, "classid");
2446  i_objid = PQfnumber(res, "objid");
2447  i_refobjid = PQfnumber(res, "refobjid");
2448 
2449  for (i = 0; i < ntups; i++)
2450  {
2451  CatalogId objId;
2452  CatalogId refobjId;
2453  DumpableObject *dobj;
2454  DumpableObject *refdobj;
2455  TableInfo *tbinfo;
2456  TableInfo *reftbinfo;
2457 
2458  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2459  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2460  refobjId.tableoid = objId.tableoid;
2461  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2462 
2463  dobj = findObjectByCatalogId(objId);
2464  if (dobj == NULL)
2465  continue;
2466 
2467  Assert(dobj->objType == DO_TABLE);
2468  tbinfo = (TableInfo *) dobj;
2469  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2470  dobj = (DumpableObject *) tbinfo->dataObj;
2471  if (dobj == NULL)
2472  continue;
2473  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2474 
2475  refdobj = findObjectByCatalogId(refobjId);
2476  if (refdobj == NULL)
2477  continue;
2478 
2479  Assert(refdobj->objType == DO_TABLE);
2480  reftbinfo = (TableInfo *) refdobj;
2481  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2482  refdobj = (DumpableObject *) reftbinfo->dataObj;
2483  if (refdobj == NULL)
2484  continue;
2485  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2486 
2487  addObjectDependency(dobj, refdobj->dumpId);
2488 
2489  if (!reftbinfo->relispopulated)
2490  tbinfo->relispopulated = false;
2491  }
2492 
2493  PQclear(res);
2494 
2495  destroyPQExpBuffer(query);
2496 }
char relkind
Definition: pg_dump.h:264
Oid tableoid
Definition: pg_backup.h:230
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpId dumpId
Definition: pg_dump.h:129
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:626
#define atooid(x)
Definition: postgres_ext.h:42
#define CppAsString2(x)
Definition: c.h:224
struct _tableDataInfo * dataObj
Definition: pg_dump.h:333
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define Assert(condition)
Definition: c.h:732
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:780
int i
bool relispopulated
Definition: pg_dump.h:266
DumpableObjectType objType
Definition: pg_dump.h:127
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:403
int remoteVersion
Definition: pg_backup.h:187

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

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

1417 {
1418  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1419 
1420  if (ext == NULL)
1421  return false;
1422 
1423  dobj->ext_member = true;
1424 
1425  /* Record dependency so that getDependencies needn't deal with that */
1426  addObjectDependency(dobj, ext->dobj.dumpId);
1427 
1428  /*
1429  * In 9.6 and above, mark the member object to have any non-initial ACL,
1430  * policies, and security labels dumped.
1431  *
1432  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1433  * extract the information about the object. We don't provide support for
1434  * initial policies and security labels and it seems unlikely for those to
1435  * ever exist, but we may have to revisit this later.
1436  *
1437  * Prior to 9.6, we do not include any extension member components.
1438  *
1439  * In binary upgrades, we still dump all components of the members
1440  * individually, since the idea is to exactly reproduce the database
1441  * contents rather than replace the extension contents with something
1442  * different.
1443  */
1444  if (fout->dopt->binary_upgrade)
1445  dobj->dump = ext->dobj.dump;
1446  else
1447  {
1448  if (fout->remoteVersion < 90600)
1449  dobj->dump = DUMP_COMPONENT_NONE;
1450  else
1451  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1454  }
1455 
1456  return true;
1457 }
DumpableObject dobj
Definition: pg_dump.h:152
DumpComponents dump
Definition: pg_dump.h:131
DumpId dumpId
Definition: pg_dump.h:129
bool ext_member
Definition: pg_dump.h:134
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:88
int binary_upgrade
Definition: pg_backup.h:135
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:932
DumpOptions * dopt
Definition: pg_backup.h:182
DumpComponents dump_contains
Definition: pg_dump.h:133
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:780
CatalogId catId
Definition: pg_dump.h:128
#define DUMP_COMPONENT_POLICY
Definition: pg_dump.h:94
int remoteVersion
Definition: pg_backup.h:187

◆ collectComments()

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

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

9723 {
9724  PGresult *res;
9725  PQExpBuffer query;
9726  int i_description;
9727  int i_classoid;
9728  int i_objoid;
9729  int i_objsubid;
9730  int ntups;
9731  int i;
9732  CommentItem *comments;
9733 
9734  query = createPQExpBuffer();
9735 
9736  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9737  "FROM pg_catalog.pg_description "
9738  "ORDER BY classoid, objoid, objsubid");
9739 
9740  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9741 
9742  /* Construct lookup table containing OIDs in numeric form */
9743 
9744  i_description = PQfnumber(res, "description");
9745  i_classoid = PQfnumber(res, "classoid");
9746  i_objoid = PQfnumber(res, "objoid");
9747  i_objsubid = PQfnumber(res, "objsubid");
9748 
9749  ntups = PQntuples(res);
9750 
9751  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9752 
9753  for (i = 0; i < ntups; i++)
9754  {
9755  comments[i].descr = PQgetvalue(res, i, i_description);
9756  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9757  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9758  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9759  }
9760 
9761  /* Do NOT free the PGresult since we are keeping pointers into it */
9762  destroyPQExpBuffer(query);
9763 
9764  *items = comments;
9765  return ntups;
9766 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
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:2770
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:2878
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:403

◆ collectSecLabels()

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

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

15198 {
15199  PGresult *res;
15200  PQExpBuffer query;
15201  int i_label;
15202  int i_provider;
15203  int i_classoid;
15204  int i_objoid;
15205  int i_objsubid;
15206  int ntups;
15207  int i;
15208  SecLabelItem *labels;
15209 
15210  query = createPQExpBuffer();
15211 
15212  appendPQExpBufferStr(query,
15213  "SELECT label, provider, classoid, objoid, objsubid "
15214  "FROM pg_catalog.pg_seclabel "
15215  "ORDER BY classoid, objoid, objsubid");
15216 
15217  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15218 
15219  /* Construct lookup table containing OIDs in numeric form */
15220  i_label = PQfnumber(res, "label");
15221  i_provider = PQfnumber(res, "provider");
15222  i_classoid = PQfnumber(res, "classoid");
15223  i_objoid = PQfnumber(res, "objoid");
15224  i_objsubid = PQfnumber(res, "objsubid");
15225 
15226  ntups = PQntuples(res);
15227 
15228  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15229 
15230  for (i = 0; i < ntups; i++)
15231  {
15232  labels[i].label = PQgetvalue(res, i, i_label);
15233  labels[i].provider = PQgetvalue(res, i, i_provider);
15234  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15235  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15236  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15237  }
15238 
15239  /* Do NOT free the PGresult since we are keeping pointers into it */
15240  destroyPQExpBuffer(query);
15241 
15242  *items = labels;
15243  return ntups;
15244 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
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:2770
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:2878
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:403

◆ convertRegProcReference()

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

Definition at line 12619 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

12620 {
12621  char *name;
12622  char *paren;
12623  bool inquote;
12624 
12625  /* In all cases "-" means a null reference */
12626  if (strcmp(proc, "-") == 0)
12627  return NULL;
12628 
12629  name = pg_strdup(proc);
12630  /* find non-double-quoted left paren */
12631  inquote = false;
12632  for (paren = name; *paren; paren++)
12633  {
12634  if (*paren == '(' && !inquote)
12635  {
12636  *paren = '\0';
12637  break;
12638  }
12639  if (*paren == '"')
12640  inquote = !inquote;
12641  }
12642  return name;
12643 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * name
Definition: encode.c:521

◆ convertTSFunction()

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

Definition at line 12690 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

12691 {
12692  char *result;
12693  char query[128];
12694  PGresult *res;
12695 
12696  snprintf(query, sizeof(query),
12697  "SELECT '%u'::pg_catalog.regproc", funcOid);
12698  res = ExecuteSqlQueryForSingleRow(fout, query);
12699 
12700  result = pg_strdup(PQgetvalue(res, 0, 0));
12701 
12702  PQclear(res);
12703 
12704  return result;
12705 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define snprintf
Definition: port.h:192

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

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

18047 {
18048  DumpableObject *dobjs;
18049 
18050  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18051 
18052  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18053  dobjs[0].catId = nilCatalogId;
18054  AssignDumpId(dobjs + 0);
18055  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18056 
18057  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18058  dobjs[1].catId = nilCatalogId;
18059  AssignDumpId(dobjs + 1);
18060  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18061 
18062  return dobjs;
18063 }
char * name
Definition: pg_dump.h:130
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:544
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
CatalogId catId
Definition: pg_dump.h:128
static const CatalogId nilCatalogId
Definition: pg_dump.c:134
DumpableObjectType objType
Definition: pg_dump.h:127

◆ createDummyViewAsClause()

static PQExpBuffer createDummyViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

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

15436 {
15437  PQExpBuffer result = createPQExpBuffer();
15438  int j;
15439 
15440  appendPQExpBufferStr(result, "SELECT");
15441 
15442  for (j = 0; j < tbinfo->numatts; j++)
15443  {
15444  if (j > 0)
15445  appendPQExpBufferChar(result, ',');
15446  appendPQExpBufferStr(result, "\n ");
15447 
15448  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15449 
15450  /*
15451  * Must add collation if not default for the type, because CREATE OR
15452  * REPLACE VIEW won't change it
15453  */
15454  if (OidIsValid(tbinfo->attcollation[j]))
15455  {
15456  CollInfo *coll;
15457 
15458  coll = findCollationByOid(tbinfo->attcollation[j]);
15459  if (coll)
15460  appendPQExpBuffer(result, " COLLATE %s",
15461  fmtQualifiedDumpable(coll));
15462  }
15463 
15464  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15465  }
15466 
15467  return result;
15468 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char ** atttypnames
Definition: pg_dump.h:302
char ** attnames
Definition: pg_dump.h:301
#define OidIsValid(objectId)
Definition: c.h:638
Oid * attcollation
Definition: pg_dump.h:314
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
int numatts
Definition: pg_dump.h:300
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:871

◆ createViewAsClause()

static PQExpBuffer createViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

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

15387 {
15388  PQExpBuffer query = createPQExpBuffer();
15389  PQExpBuffer result = createPQExpBuffer();
15390  PGresult *res;
15391  int len;
15392 
15393  /* Fetch the view definition */
15394  appendPQExpBuffer(query,
15395  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15396  tbinfo->dobj.catId.oid);
15397 
15398  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15399 
15400  if (PQntuples(res) != 1)
15401  {
15402  if (PQntuples(res) < 1)
15403  fatal("query to obtain definition of view \"%s\" returned no data",
15404  tbinfo->dobj.name);
15405  else
15406  fatal("query to obtain definition of view \"%s\" returned more than one definition",
15407  tbinfo->dobj.name);
15408  }
15409 
15410  len = PQgetlength(res, 0, 0);
15411 
15412  if (len == 0)
15413  fatal("definition of view \"%s\" appears to be empty (length zero)",
15414  tbinfo->dobj.name);
15415 
15416  /* Strip off the trailing semicolon so that other things may follow. */
15417  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15418  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15419 
15420  PQclear(res);
15421  destroyPQExpBuffer(query);
15422 
15423  return result;
15424 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3175
char * name
Definition: pg_dump.h:130
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
DumpableObject dobj
Definition: pg_dump.h:258
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:695
#define Assert(condition)
Definition: c.h:732
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:128
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:399
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:403

◆ dumpAccessMethod()

static void dumpAccessMethod ( Archive fout,
AccessMethodInfo oprinfo 
)
static

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

12713 {
12714  DumpOptions *dopt = fout->dopt;
12715  PQExpBuffer q;
12716  PQExpBuffer delq;
12717  char *qamname;
12718 
12719  /* Skip if not to be dumped */
12720  if (!aminfo->dobj.dump || dopt->dataOnly)
12721  return;
12722 
12723  q = createPQExpBuffer();
12724  delq = createPQExpBuffer();
12725 
12726  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12727 
12728  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12729 
12730  switch (aminfo->amtype)
12731  {
12732  case AMTYPE_INDEX:
12733  appendPQExpBufferStr(q, "TYPE INDEX ");
12734  break;
12735  case AMTYPE_TABLE:
12736  appendPQExpBufferStr(q, "TYPE TABLE ");
12737  break;
12738  default:
12739  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12740  aminfo->amtype, qamname);
12741  destroyPQExpBuffer(q);
12742  destroyPQExpBuffer(delq);
12743  free(qamname);
12744  return;
12745  }
12746 
12747  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12748 
12749  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12750  qamname);
12751 
12752  if (dopt->binary_upgrade)
12753  binary_upgrade_extension_member(q, &aminfo->dobj,
12754  "ACCESS METHOD", qamname, NULL);
12755 
12756  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12757  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12758  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12759  .description = "ACCESS METHOD",
12760  .section = SECTION_PRE_DATA,
12761  .createStmt = q->data,
12762  .dropStmt = delq->data));
12763 
12764  /* Dump Access Method Comments */
12765  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12766  dumpComment(fout, "ACCESS METHOD", qamname,
12767  NULL, "",
12768  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12769 
12770  destroyPQExpBuffer(q);
12771  destroyPQExpBuffer(delq);
12772  free(qamname);
12773 }
#define ARCHIVE_OPTS(...)
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4473
#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:9459

◆ dumpACL()

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

Definition at line 14863 of file pg_dump.c.

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

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

14868 {
14869  DumpOptions *dopt = fout->dopt;
14870  PQExpBuffer sql;
14871 
14872  /* Do nothing if ACL dump is not enabled */
14873  if (dopt->aclsSkip)
14874  return;
14875 
14876  /* --data-only skips ACLs *except* BLOB ACLs */
14877  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14878  return;
14879 
14880  sql = createPQExpBuffer();
14881 
14882  /*
14883  * Check to see if this object has had any initial ACLs included for it.
14884  * If so, we are in binary upgrade mode and these are the ACLs to turn
14885  * into GRANT and REVOKE statements to set and record the initial
14886  * privileges for an extension object. Let the backend know that these
14887  * are to be recorded by calling binary_upgrade_set_record_init_privs()
14888  * before and after.
14889  */
14890  if (strlen(initacls) != 0 || strlen(initracls) != 0)
14891  {
14892  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14893  if (!buildACLCommands(name, subname, nspname, type,
14894  initacls, initracls, owner,
14895  "", fout->remoteVersion, sql))
14896  fatal("could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)",
14897  initacls, initracls, name, type);
14898  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14899  }
14900 
14901  if (!buildACLCommands(name, subname, nspname, type,
14902  acls, racls, owner,
14903  "", fout->remoteVersion, sql))
14904  fatal("could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)",
14905  acls, racls, name, type);
14906 
14907  if (sql->len > 0)
14908  {
14910 
14911  if (subname)
14912  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
14913  else
14914  appendPQExpBuffer(tag, "%s %s", type, name);
14915 
14917  ARCHIVE_OPTS(.tag = tag->data,
14918  .namespace = nspname,
14919  .owner = owner,
14920  .description = "ACL",
14921  .section = SECTION_NONE,
14922  .createStmt = sql->data,
14923  .deps = &objDumpId,
14924  .nDeps = 1));
14925  destroyPQExpBuffer(tag);
14926  }
14927 
14928  destroyPQExpBuffer(sql);
14929 }
#define ARCHIVE_OPTS(...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
NameData subname
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
bool aclsSkip
Definition: pg_backup.h:141
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:58
#define fatal(...)
const char * name
Definition: encode.c:521
static const CatalogId nilCatalogId
Definition: pg_dump.c:134
DumpId createDumpId(void)
Definition: common.c:588
int remoteVersion
Definition: pg_backup.h:187

◆ dumpAgg()

static void dumpAgg ( Archive fout,
AggInfo agginfo 
)
static

Definition at line 13700 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

13701 {
13702  DumpOptions *dopt = fout->dopt;
13703  PQExpBuffer query;
13704  PQExpBuffer q;
13705  PQExpBuffer delq;
13706  PQExpBuffer details;
13707  char *aggsig; /* identity signature */
13708  char *aggfullsig = NULL; /* full signature */
13709  char *aggsig_tag;
13710  PGresult *res;
13711  int i_aggtransfn;
13712  int i_aggfinalfn;
13713  int i_aggcombinefn;
13714  int i_aggserialfn;
13715  int i_aggdeserialfn;
13716  int i_aggmtransfn;
13717  int i_aggminvtransfn;
13718  int i_aggmfinalfn;
13719  int i_aggfinalextra;
13720  int i_aggmfinalextra;
13721  int i_aggfinalmodify;
13722  int i_aggmfinalmodify;
13723  int i_aggsortop;
13724  int i_aggkind;
13725  int i_aggtranstype;
13726  int i_aggtransspace;
13727  int i_aggmtranstype;
13728  int i_aggmtransspace;
13729  int i_agginitval;
13730  int i_aggminitval;
13731  int i_convertok;
13732  int i_proparallel;
13733  const char *aggtransfn;
13734  const char *aggfinalfn;
13735  const char *aggcombinefn;
13736  const char *aggserialfn;
13737  const char *aggdeserialfn;
13738  const char *aggmtransfn;
13739  const char *aggminvtransfn;
13740  const char *aggmfinalfn;
13741  bool aggfinalextra;
13742  bool aggmfinalextra;
13743  char aggfinalmodify;
13744  char aggmfinalmodify;
13745  const char *aggsortop;
13746  char *aggsortconvop;
13747  char aggkind;
13748  const char *aggtranstype;
13749  const char *aggtransspace;
13750  const char *aggmtranstype;
13751  const char *aggmtransspace;
13752  const char *agginitval;
13753  const char *aggminitval;
13754  bool convertok;
13755  const char *proparallel;
13756  char defaultfinalmodify;
13757 
13758  /* Skip if not to be dumped */
13759  if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13760  return;
13761 
13762  query = createPQExpBuffer();
13763  q = createPQExpBuffer();
13764  delq = createPQExpBuffer();
13765  details = createPQExpBuffer();
13766 
13767  /* Get aggregate-specific details */
13768  if (fout->remoteVersion >= 110000)
13769  {
13770  appendPQExpBuffer(query, "SELECT aggtransfn, "
13771  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13772  "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13773  "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13774  "aggfinalextra, aggmfinalextra, "
13775  "aggfinalmodify, aggmfinalmodify, "
13776  "aggsortop, "
13777  "aggkind, "
13778  "aggtransspace, agginitval, "
13779  "aggmtransspace, aggminitval, "
13780  "true AS convertok, "
13781  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13782  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13783  "p.proparallel "
13784  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13785  "WHERE a.aggfnoid = p.oid "
13786  "AND p.oid = '%u'::pg_catalog.oid",
13787  agginfo->aggfn.dobj.catId.oid);
13788  }
13789  else if (fout->remoteVersion >= 90600)
13790  {
13791  appendPQExpBuffer(query, "SELECT aggtransfn, "
13792  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13793  "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13794  "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13795  "aggfinalextra, aggmfinalextra, "
13796  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13797  "aggsortop, "
13798  "aggkind, "
13799  "aggtransspace, agginitval, "
13800  "aggmtransspace, aggminitval, "
13801  "true AS convertok, "
13802  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13803  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13804  "p.proparallel "
13805  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13806  "WHERE a.aggfnoid = p.oid "
13807  "AND p.oid = '%u'::pg_catalog.oid",
13808  agginfo->aggfn.dobj.catId.oid);
13809  }
13810  else if (fout->remoteVersion >= 90400)
13811  {
13812  appendPQExpBuffer(query, "SELECT aggtransfn, "
13813  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13814  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13815  "'-' AS aggdeserialfn, aggmtransfn, aggminvtransfn, "
13816  "aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13817  "aggfinalextra, aggmfinalextra, "
13818  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13819  "aggsortop, "
13820  "aggkind, "
13821  "aggtransspace, agginitval, "
13822  "aggmtransspace, aggminitval, "
13823  "true AS convertok, "
13824  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13825  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13826  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13827  "WHERE a.aggfnoid = p.oid "
13828  "AND p.oid = '%u'::pg_catalog.oid",
13829  agginfo->aggfn.dobj.catId.oid);
13830  }
13831  else if (fout->remoteVersion >= 80400)
13832  {
13833  appendPQExpBuffer(query, "SELECT aggtransfn, "
13834  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13835  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13836  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13837  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13838  "0 AS aggmtranstype, false AS aggfinalextra, "
13839  "false AS aggmfinalextra, "
13840  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13841  "aggsortop, "
13842  "'n' AS aggkind, "
13843  "0 AS aggtransspace, agginitval, "
13844  "0 AS aggmtransspace, NULL AS aggminitval, "
13845  "true AS convertok, "
13846  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13847  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13848  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13849  "WHERE a.aggfnoid = p.oid "
13850  "AND p.oid = '%u'::pg_catalog.oid",
13851  agginfo->aggfn.dobj.catId.oid);
13852  }
13853  else if (fout->remoteVersion >= 80100)
13854  {
13855  appendPQExpBuffer(query, "SELECT aggtransfn, "
13856  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13857  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13858  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13859  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13860  "0 AS aggmtranstype, false AS aggfinalextra, "
13861  "false AS aggmfinalextra, "
13862  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13863  "aggsortop, "
13864  "'n' AS aggkind, "
13865  "0 AS aggtransspace, agginitval, "
13866  "0 AS aggmtransspace, NULL AS aggminitval, "
13867  "true AS convertok "
13868  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13869  "WHERE a.aggfnoid = p.oid "
13870  "AND p.oid = '%u'::pg_catalog.oid",
13871  agginfo->aggfn.dobj.catId.oid);
13872  }
13873  else
13874  {
13875  appendPQExpBuffer(query, "SELECT aggtransfn, "
13876  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13877  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13878  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13879  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13880  "0 AS aggmtranstype, false AS aggfinalextra, "
13881  "false AS aggmfinalextra, "
13882  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13883  "0 AS aggsortop, "
13884  "'n' AS aggkind, "
13885  "0 AS aggtransspace, agginitval, "
13886  "0 AS aggmtransspace, NULL AS aggminitval, "
13887  "true AS convertok "
13888  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13889  "WHERE a.aggfnoid = p.oid "
13890  "AND p.oid = '%u'::pg_catalog.oid",
13891  agginfo->aggfn.dobj.catId.oid);
13892  }
13893 
13894  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13895 
13896  i_aggtransfn = PQfnumber(res, "aggtransfn");
13897  i_aggfinalfn = PQfnumber(res, "aggfinalfn");
13898  i_aggcombinefn = PQfnumber(res, "aggcombinefn");
13899  i_aggserialfn = PQfnumber(res, "aggserialfn");
13900  i_aggdeserialfn = PQfnumber(res, "aggdeserialfn");
13901  i_aggmtransfn = PQfnumber(res, "aggmtransfn");
13902  i_aggminvtransfn = PQfnumber(res, "aggminvtransfn");
13903  i_aggmfinalfn = PQfnumber(res, "aggmfinalfn");
13904  i_aggfinalextra = PQfnumber(res, "aggfinalextra");
13905  i_aggmfinalextra = PQfnumber(res, "aggmfinalextra");
13906  i_aggfinalmodify = PQfnumber(res, "aggfinalmodify");
13907  i_aggmfinalmodify = PQfnumber(res, "aggmfinalmodify");
13908  i_aggsortop = PQfnumber(res, "aggsortop");
13909  i_aggkind = PQfnumber(res, "aggkind");
13910  i_aggtranstype = PQfnumber(res, "aggtranstype");
13911  i_aggtransspace = PQfnumber(res, "aggtransspace");
13912  i_aggmtranstype = PQfnumber(res, "aggmtranstype");
13913  i_aggmtransspace = PQfnumber(res, "aggmtransspace");
13914  i_agginitval = PQfnumber(res, "agginitval");
13915  i_aggminitval = PQfnumber(res, "aggminitval");
13916  i_convertok = PQfnumber(res, "convertok");
13917  i_proparallel = PQfnumber(res, "proparallel");
13918 
13919  aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
13920  aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
13921  aggcombinefn = PQgetvalue(res, 0, i_aggcombinefn);
13922  aggserialfn = PQgetvalue(res, 0, i_aggserialfn);
13923  aggdeserialfn = PQgetvalue(res, 0, i_aggdeserialfn);
13924  aggmtransfn = PQgetvalue(res, 0, i_aggmtransfn);
13925  aggminvtransfn = PQgetvalue(res, 0, i_aggminvtransfn);
13926  aggmfinalfn = PQgetvalue(res, 0, i_aggmfinalfn);
13927  aggfinalextra = (PQgetvalue(res, 0, i_aggfinalextra)[0] == 't');
13928  aggmfinalextra = (PQgetvalue(res, 0, i_aggmfinalextra)[0] == 't');
13929  aggfinalmodify = PQgetvalue(res, 0, i_aggfinalmodify)[0];
13930  aggmfinalmodify = PQgetvalue(res, 0, i_aggmfinalmodify)[0];
13931  aggsortop = PQgetvalue(res, 0, i_aggsortop);
13932  aggkind = PQgetvalue(res, 0, i_aggkind)[0];
13933  aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
13934  aggtransspace = PQgetvalue(res, 0, i_aggtransspace);
13935  aggmtranstype = PQgetvalue(res, 0, i_aggmtranstype);
13936  aggmtransspace = PQgetvalue(res, 0, i_aggmtransspace);
13937  agginitval = PQgetvalue(res, 0, i_agginitval);
13938  aggminitval = PQgetvalue(res, 0, i_aggminitval);
13939  convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
13940 
13941  if (fout->remoteVersion >= 80400)
13942  {
13943  /* 8.4 or later; we rely on server-side code for most of the work */
13944  char *funcargs;
13945  char *funciargs;
13946 
13947  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13948  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13949  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13950  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13951  }
13952  else
13953  /* pre-8.4, do it ourselves */
13954  aggsig = format_aggregate_signature(agginfo, fout, true);
13955 
13956  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
13957 
13958  if (i_proparallel != -1)
13959  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13960  else
13961  proparallel = NULL;
13962 
13963  if (!convertok)
13964  {
13965  pg_log_warning("aggregate function %s could not be dumped correctly for this database version; ignored",
13966  aggsig);
13967 
13968  if (aggfullsig)
13969  free(aggfullsig);
13970 
13971  free(aggsig);
13972 
13973  return;
13974  }
13975 
13976  /* identify default modify flag for aggkind (must match DefineAggregate) */
13977  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
13978  /* replace omitted flags for old versions */
13979  if (aggfinalmodify == '0')
13980  aggfinalmodify = defaultfinalmodify;
13981  if (aggmfinalmodify == '0')
13982  aggmfinalmodify = defaultfinalmodify;
13983 
13984  /* regproc and regtype output is already sufficiently quoted */
13985  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
13986  aggtransfn, aggtranstype);
13987 
13988  if (strcmp(aggtransspace, "0") != 0)
13989  {
13990  appendPQExpBuffer(details, ",\n SSPACE = %s",
13991  aggtransspace);
13992  }
13993 
13994  if (!PQgetisnull(res, 0, i_agginitval))
13995  {
13996  appendPQExpBufferStr(details, ",\n INITCOND = ");
13997  appendStringLiteralAH(details, agginitval, fout);
13998  }
13999 
14000  if (strcmp(aggfinalfn, "-") != 0)
14001  {
14002  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
14003  aggfinalfn);
14004  if (aggfinalextra)
14005  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
14006  if (aggfinalmodify != defaultfinalmodify)
14007  {
14008  switch (aggfinalmodify)
14009  {
14010  case AGGMODIFY_READ_ONLY:
14011  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
14012  break;
14013  case AGGMODIFY_SHAREABLE:
14014  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE");
14015  break;
14016  case AGGMODIFY_READ_WRITE:
14017  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
14018  break;
14019  default:
14020  fatal("unrecognized aggfinalmodify value for aggregate \"%s\"",
14021  agginfo->aggfn.dobj.name);
14022  break;
14023  }
14024  }
14025  }
14026 
14027  if (strcmp(aggcombinefn, "-") != 0)
14028  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
14029 
14030  if (strcmp(aggserialfn, "-") != 0)
14031  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
14032 
14033  if (strcmp(aggdeserialfn, "-") != 0)
14034  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
14035 
14036  if (strcmp(aggmtransfn, "-") != 0)
14037  {
14038  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
14039  aggmtransfn,
14040  aggminvtransfn,
14041  aggmtranstype);
14042  }
14043 
14044  if (strcmp(aggmtransspace, "0") != 0)
14045  {
14046  appendPQExpBuffer(details, ",\n MSSPACE = %s",
14047  aggmtransspace);
14048  }
14049 
14050  if (!PQgetisnull(res, 0, i_aggminitval))
14051  {
14052  appendPQExpBufferStr(details, ",\n MINITCOND = ");
14053  appendStringLiteralAH(details, aggminitval, fout);
14054  }
14055 
14056  if (strcmp(aggmfinalfn, "-") != 0)
14057  {
14058  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
14059  aggmfinalfn);
14060  if (aggmfinalextra)
14061  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
14062  if (aggmfinalmodify != defaultfinalmodify)
14063  {
14064  switch (aggmfinalmodify)
14065  {
14066  case AGGMODIFY_READ_ONLY:
14067  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
14068  break;
14069  case AGGMODIFY_SHAREABLE:
14070  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE");
14071  break;
14072  case AGGMODIFY_READ_WRITE:
14073  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
14074  break;
14075  default:
14076  fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"",
14077  agginfo->aggfn.dobj.name);
14078  break;
14079  }
14080  }
14081  }
14082 
14083  aggsortconvop = getFormattedOperatorName(fout, aggsortop);
14084  if (aggsortconvop)
14085  {
14086  appendPQExpBuffer(details, ",\n SORTOP = %s",
14087  aggsortconvop);
14088  free(aggsortconvop);
14089  }
14090 
14091  if (aggkind == AGGKIND_HYPOTHETICAL)
14092  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
14093 
14094  if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
14095  {
14096  if (proparallel[0] == PROPARALLEL_SAFE)
14097  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
14098  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
14099  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
14100  else if (proparallel[0] != PROPARALLEL_UNSAFE)
14101  fatal("unrecognized proparallel value for function \"%s\"",
14102  agginfo->aggfn.dobj.name);
14103  }
14104 
14105  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
14106  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14107  aggsig);
14108 
14109  appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
14110  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
14111  aggfullsig ? aggfullsig : aggsig, details->data);
14112 
14113  if (dopt->binary_upgrade)
14115  "AGGREGATE", aggsig,
14116  agginfo->aggfn.dobj.namespace->dobj.name);
14117 
14118  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
14119  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
14120  agginfo->aggfn.dobj.dumpId,
14121  ARCHIVE_OPTS(.tag = aggsig_tag,
14122  .namespace = agginfo->aggfn.dobj.namespace->dobj.name,
14123  .owner = agginfo->aggfn.rolname,
14124  .description = "AGGREGATE",
14125  .section = SECTION_PRE_DATA,
14126  .createStmt = q->data,
14127  .dropStmt = delq->data));
14128 
14129  /* Dump Aggregate Comments */
14130  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
14131  dumpComment(fout, "AGGREGATE", aggsig,
14132  agginfo->aggfn.dobj.namespace->dobj.name,
14133  agginfo->aggfn.rolname,
14134  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14135 
14136  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
14137  dumpSecLabel(fout, "AGGREGATE", aggsig,
14138  agginfo->aggfn.dobj.namespace->dobj.name,
14139  agginfo->aggfn.rolname,
14140  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
14141 
14142  /*
14143  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
14144  * command look like a function's GRANT; in particular this affects the
14145  * syntax for zero-argument aggregates and ordered-set aggregates.
14146  */
14147  free(aggsig);
14148 
14149  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
14150 
14151  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
14152  dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
14153  "FUNCTION", aggsig, NULL,
14154  agginfo->aggfn.dobj.namespace->dobj.name,
14155  agginfo->aggfn.rolname, agginfo->aggfn.proacl,
14156  agginfo->aggfn.rproacl,
14157  agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
14158 
14159  free(aggsig);
14160  if (aggfullsig)
14161  free(aggfullsig);
14162  free(aggsig_tag);
14163 
14164  PQclear(res);
14165 
14166  destroyPQExpBuffer(query);
14167  destroyPQExpBuffer(q);
14168  destroyPQExpBuffer(delq);
14169  destroyPQExpBuffer(details);
14170 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrproacl
Definition: pg_dump.h:204
DumpableObject dobj
Definition: pg_dump.h:195
DumpId dumpId
Definition: pg_dump.h:129
static void dumpSecLabel(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14950
static char * format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
Definition: pg_dump.c:11446
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14863
FuncInfo aggfn
Definition: pg_dump.h:210
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
char * initproacl
Definition: pg_dump.h:203
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11541
char * rolname
Definition: pg_dump.h:196
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
#define fatal(...)
char * proacl
Definition: pg_dump.h:201
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4473
char * rproacl
Definition: pg_dump.h:202
static char * getFormattedOperatorName(Archive *fout, const char *oproid)
Definition: pg_dump.c:12660
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
#define pg_log_warning(...)
Definition: pgfnames.c:24
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189
static char * format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:13662
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:9459
int remoteVersion
Definition: pg_backup.h:187

◆ dumpAttrDef()

static void dumpAttrDef ( Archive fout,
AttrDefInfo adinfo 
)
static

Definition at line 16223 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::rolname, SECTION_PRE_DATA, and _attrDefInfo::separate.

Referenced by dumpDumpableObject().

16224 {
16225  DumpOptions *dopt = fout->dopt;
16226  TableInfo *tbinfo = adinfo->adtable;
16227  int adnum = adinfo->adnum;
16228  PQExpBuffer q;
16229  PQExpBuffer delq;
16230  char *qualrelname;
16231  char *tag;
16232 
16233  /* Skip if table definition not to be dumped */
16234  if (!tbinfo->dobj.dump || dopt->dataOnly)
16235  return;
16236 
16237  /* Skip if not "separate"; it was dumped in the table's definition */
16238  if (!adinfo->separate)
16239  return;
16240 
16241  q = createPQExpBuffer();
16242  delq = createPQExpBuffer();
16243 
16244  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16245 
16246  appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16247  qualrelname);
16248  appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
16249  fmtId(tbinfo->attnames[adnum - 1]),
16250  adinfo->adef_expr);
16251 
16252  appendPQExpBuffer(delq, "ALTER TABLE %s ",
16253  qualrelname);
16254  appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
16255  fmtId(tbinfo->attnames[adnum - 1]));
16256 
16257  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16258 
16259  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16260  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16261  ARCHIVE_OPTS(.tag = tag,
16262  .namespace = tbinfo->dobj.namespace->dobj.name,
16263  .owner = tbinfo->rolname,
16264  .description = "DEFAULT",
16265  .section = SECTION_PRE_DATA,
16266  .createStmt = q->data,
16267  .dropStmt = delq->data));
16268 
16269  free(tag);
16270  destroyPQExpBuffer(q);
16271  destroyPQExpBuffer(delq);
16272  free(qualrelname);
16273 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
char * rolname
Definition: pg_dump.h:259
DumpId dumpId
Definition: pg_dump.h:129
char ** attnames
Definition: pg_dump.h:301
DumpableObject dobj
Definition: pg_dump.h:258
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
bool separate
Definition: pg_dump.h:344
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
TableInfo * adtable
Definition: pg_dump.h:341
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
DumpableObject dobj
Definition: pg_dump.h:340
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
CatalogId catId
Definition: pg_dump.h:128
char * adef_expr
Definition: pg_dump.h:343

◆ dumpBaseType()

static void dumpBaseType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10485 of file pg_dump.c.

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

Referenced by dumpType().

10486 {
10487  DumpOptions *dopt = fout->dopt;
10489  PQExpBuffer delq = createPQExpBuffer();
10490  PQExpBuffer query = createPQExpBuffer();
10491  PGresult *res;
10492  char *qtypname;
10493  char *qualtypname;
10494  char *typlen;
10495  char *typinput;
10496  char *typoutput;
10497  char *typreceive;
10498  char *typsend;
10499  char *typmodin;
10500  char *typmodout;
10501  char *typanalyze;
10502  Oid typreceiveoid;
10503  Oid typsendoid;
10504  Oid typmodinoid;
10505  Oid typmodoutoid;
10506  Oid typanalyzeoid;
10507  char *typcategory;
10508  char *typispreferred;
10509  char *typdelim;
10510  char *typbyval;
10511  char *typalign;
10512  char *typstorage;
10513  char *typcollatable;
10514  char *typdefault;
10515  bool typdefault_is_literal = false;
10516 
10517  /* Fetch type-specific details */
10518  if (fout->remoteVersion >= 90100)
10519  {
10520  appendPQExpBuffer(query, "SELECT typlen, "
10521  "typinput, typoutput, typreceive, typsend, "
10522  "typmodin, typmodout, typanalyze, "
10523  "typreceive::pg_catalog.oid AS typreceiveoid, "
10524  "typsend::pg_catalog.oid AS typsendoid, "
10525  "typmodin::pg_catalog.oid AS typmodinoid, "
10526  "typmodout::pg_catalog.oid AS typmodoutoid, "
10527  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10528  "typcategory, typispreferred, "
10529  "typdelim, typbyval, typalign, typstorage, "
10530  "(typcollation <> 0) AS typcollatable, "
10531  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10532  "FROM pg_catalog.pg_type "
10533  "WHERE oid = '%u'::pg_catalog.oid",
10534  tyinfo->dobj.catId.oid);
10535  }
10536  else if (fout->remoteVersion >= 80400)
10537  {
10538  appendPQExpBuffer(query, "SELECT typlen, "
10539  "typinput, typoutput, typreceive, typsend, "
10540  "typmodin, typmodout, typanalyze, "
10541  "typreceive::pg_catalog.oid AS typreceiveoid, "
10542  "typsend::pg_catalog.oid AS typsendoid, "
10543  "typmodin::pg_catalog.oid AS typmodinoid, "
10544  "typmodout::pg_catalog.oid AS typmodoutoid, "
10545  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10546  "typcategory, typispreferred, "
10547  "typdelim, typbyval, typalign, typstorage, "
10548  "false AS typcollatable, "
10549  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10550  "FROM pg_catalog.pg_type "
10551  "WHERE oid = '%u'::pg_catalog.oid",
10552  tyinfo->dobj.catId.oid);
10553  }
10554  else if (fout->remoteVersion >= 80300)
10555  {
10556  /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
10557  appendPQExpBuffer(query, "SELECT typlen, "
10558  "typinput, typoutput, typreceive, typsend, "
10559  "typmodin, typmodout, typanalyze, "
10560  "typreceive::pg_catalog.oid AS typreceiveoid, "
10561  "typsend::pg_catalog.oid AS typsendoid, "
10562  "typmodin::pg_catalog.oid AS typmodinoid, "
10563  "typmodout::pg_catalog.oid AS typmodoutoid, "
10564  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10565  "'U' AS typcategory, false AS typispreferred, "
10566  "typdelim, typbyval, typalign, typstorage, "
10567  "false AS typcollatable, "
10568  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10569  "FROM pg_catalog.pg_type "
10570  "WHERE oid = '%u'::pg_catalog.oid",
10571  tyinfo->dobj.catId.oid);
10572  }
10573  else
10574  {
10575  appendPQExpBuffer(query, "SELECT typlen, "
10576  "typinput, typoutput, typreceive, typsend, "
10577  "'-' AS typmodin, '-' AS typmodout, "
10578  "typanalyze, "
10579  "typreceive::pg_catalog.oid AS typreceiveoid, "
10580  "typsend::pg_catalog.oid AS typsendoid, "
10581  "0 AS typmodinoid, 0 AS typmodoutoid, "
10582  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10583  "'U' AS typcategory, false AS typispreferred, "
10584  "typdelim, typbyval, typalign, typstorage, "
10585  "false AS typcollatable, "
10586  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10587  "FROM pg_catalog.pg_type "
10588  "WHERE oid = '%u'::pg_catalog.oid",
10589  tyinfo->dobj.catId.oid);
10590  }
10591 
10592  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10593 
10594  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10595  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10596  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10597  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10598  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10599  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10600  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10601  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10602  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10603  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10604  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10605  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10606  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10607  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10608  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10609  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10610  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10611  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10612  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10613  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10614  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10615  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10616  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10617  {
10618  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10619  typdefault_is_literal = true; /* it needs quotes */
10620  }
10621  else
10622  typdefault = NULL;
10623 
10624  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10625  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10626 
10627  /*
10628  * The reason we include CASCADE is that the circular dependency between
10629  * the type and its I/O functions makes it impossible to drop the type any
10630  * other way.
10631  */
10632  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
10633 
10634  /*
10635  * We might already have a shell type, but setting pg_type_oid is
10636  * harmless, and in any case we'd better set the array type OID.
10637  */
10638  if (dopt->binary_upgrade)
10640  tyinfo->dobj.catId.oid,
10641  false);
10642 
10644  "CREATE TYPE %s (\n"
10645  " INTERNALLENGTH = %s",
10646  qualtypname,
10647  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10648 
10649  /* regproc result is sufficiently quoted already */
10650  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
10651  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
10652  if (OidIsValid(typreceiveoid))
10653  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
10654  if (OidIsValid(typsendoid))
10655  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10656  if (OidIsValid(typmodinoid))
10657  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10658  if (OidIsValid(typmodoutoid))
10659  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10660  if (OidIsValid(typanalyzeoid))
10661  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10662 
10663  if (strcmp(typcollatable, "t") == 0)
10664  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10665 
10666  if (typdefault != NULL)
10667  {
10668  appendPQExpBufferStr(q, ",\n DEFAULT = ");
10669  if (typdefault_is_literal)
10670  appendStringLiteralAH(q, typdefault, fout);
10671  else
10672  appendPQExpBufferStr(q, typdefault);
10673  }
10674 
10675  if (OidIsValid(tyinfo->typelem))
10676  {
10677  char *elemType;
10678 
10679  elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroAsOpaque);
10680  appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
10681  free(elemType);
10682  }
10683 
10684  if (strcmp(typcategory, "U") != 0)
10685  {
10686  appendPQExpBufferStr(q, ",\n CATEGORY = ");
10687  appendStringLiteralAH(q, typcategory, fout);
10688  }
10689 
10690  if (strcmp(typispreferred, "t") == 0)
10691  appendPQExpBufferStr(q, ",\n PREFERRED = true");
10692 
10693  if (typdelim && strcmp(typdelim, ",") != 0)
10694  {
10695  appendPQExpBufferStr(q, ",\n DELIMITER = ");
10696  appendStringLiteralAH(q, typdelim, fout);
10697  }
10698 
10699  if (strcmp(typalign, "c") == 0)
10700  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
10701  else if (strcmp(typalign, "s") == 0)
10702  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
10703  else if (strcmp(typalign, "i") == 0)
10704  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
10705  else if (strcmp(typalign, "d") == 0)
10706  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
10707 
10708  if (strcmp(typstorage, "p") == 0)
10709  appendPQExpBufferStr(q, ",\n STORAGE = plain");
10710  else if (strcmp(typstorage, "e") == 0)
10711  appendPQExpBufferStr(q, ",\n STORAGE = external");
10712  else if (strcmp(typstorage, "x") == 0)
10713  appendPQExpBufferStr(q, ",\n STORAGE = extended");
10714  else if (strcmp(typstorage, "m") == 0)
10715  appendPQExpBufferStr(q, ",\n STORAGE = main");
10716 
10717  if (strcmp(typbyval, "t") == 0)
10718  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
10719 
10720  appendPQExpBufferStr(q, "\n);\n");
10721 
10722  if (dopt->binary_upgrade)
10724  "TYPE", qtypname,
10725  tyinfo->dobj.namespace->dobj.name);
10726 
10727  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10728  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10729  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
10730  .namespace = tyinfo->dobj.namespace->dobj.name,
10731  .owner = tyinfo->rolname,
10732  .description = "TYPE",
10733  .section = SECTION_PRE_DATA,
10734  .createStmt = q->data,
10735  .dropStmt = delq->data));
10736 
10737  /* Dump Type Comments and Security Labels */
10738  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10739  dumpComment(fout, "TYPE", qtypname,
10740  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10741  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10742 
10743  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10744  dumpSecLabel(fout, "TYPE", qtypname,
10745  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10746  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10747 
10748  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10749  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10750  qtypname, NULL,
10751  tyinfo->dobj.namespace->dobj.name,
10752  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10753  tyinfo->inittypacl, tyinfo->initrtypacl);
10754 
10755  PQclear(res);
10756  destroyPQExpBuffer(q);
10757  destroyPQExpBuffer(delq);
10758  destroyPQExpBuffer(query);
10759  free(qtypname);
10760  free(qualtypname);
10761 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18287
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
char * inittypacl
Definition: pg_dump.h:171
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrtypacl
Definition: pg_dump.h:172
char * typacl
Definition: pg_dump.h:169
DumpId dumpId
Definition: pg_dump.h:129
unsigned int Oid
Definition: postgres_ext.h:31
static void dumpSecLabel(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14950
#define OidIsValid(objectId)
Definition: c.h:638
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14863
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
char typalign
Definition: pg_type.h:170
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
Oid typelem
Definition: pg_dump.h:173
#define atooid(x)
Definition: postgres_ext.h:42
DumpableObject dobj
Definition: pg_dump.h:162
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type)
Definition: pg_dump.c:4282
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
char * rtypacl
Definition: pg_dump.h:170
char * rolname
Definition: pg_dump.h:168
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4473
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189
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:9459
int remoteVersion
Definition: pg_backup.h:187

◆ dumpBlob()

static void dumpBlob ( Archive fout,
BlobInfo binfo 
)
static

Definition at line 3382 of file pg_dump.c.

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

Referenced by dumpDumpableObject().

3383 {
3384  PQExpBuffer cquery = createPQExpBuffer();
3385  PQExpBuffer dquery = createPQExpBuffer();
3386 
3387  appendPQExpBuffer(cquery,
3388  "SELECT pg_catalog.lo_create('%s');\n",
3389  binfo->dobj.name);
3390 
3391  appendPQExpBuffer(dquery,
3392  "SELECT pg_catalog.lo_unlink('%s');\n",
3393  binfo->dobj.name);
3394 
3395  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3396  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3397  ARCHIVE_OPTS(.tag = binfo->dobj.name,
3398  .owner = binfo->rolname,
3399  .description = "BLOB",
3400  .section = SECTION_PRE_DATA,
3401  .createStmt = cquery->data,
3402  .dropStmt = dquery->data));
3403 
3404  /* Dump comment if any */
3405  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3406  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3407  NULL, binfo->rolname,
3408  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3409 
3410  /* Dump security label if any */
3411  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3412  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3413  NULL, binfo->rolname,
3414  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3415 
3416  /* Dump ACL if any */
3417  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3418  dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
3419  binfo->dobj.name, NULL,
3420  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3421  binfo->initblobacl, binfo->initrblobacl);
3422 
3423  destroyPQExpBuffer(cquery);
3424  destroyPQExpBuffer(dquery);
3425 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
DumpId dumpId
Definition: pg_dump.h:129
static void dumpSecLabel(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14950
char * initblobacl
Definition: pg_dump.h:566
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14863
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject dobj
Definition: pg_dump.h:562
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:563
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
char * blobacl
Definition: pg_dump.h:564
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
char * rblobacl
Definition: pg_dump.h:565
char * initrblobacl
Definition: pg_dump.h:567
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:9459

◆ dumpBlobs()

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

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

3433 {
3434  const char *blobQry;
3435  const char *blobFetchQry;
3436  PGconn *conn = GetConnection(fout);
3437  PGresult *res;
3438  char buf[LOBBUFSIZE];
3439  int ntups;
3440  int i;
3441  int cnt;
3442 
3443  pg_log_info("saving large objects");
3444 
3445  /*
3446  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3447  * the already-in-memory dumpable objects instead...
3448  */
3449  if (fout->remoteVersion >= 90000)
3450  blobQry =
3451  "DECLARE bloboid CURSOR FOR "
3452  "SELECT oid FROM pg_largeobject_metadata ORDER BY 1";
3453  else
3454  blobQry =
3455  "DECLARE bloboid CURSOR FOR "
3456  "SELECT DISTINCT loid FROM pg_largeobject ORDER BY 1";
3457 
3458  ExecuteSqlStatement(fout, blobQry);
3459 
3460  /* Command to fetch from cursor */
3461  blobFetchQry = "FETCH 1000 IN bloboid";
3462 
3463  do
3464  {
3465  /* Do a fetch */
3466  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3467 
3468  /* Process the tuples, if any */
3469  ntups = PQntuples(res);
3470  for (i = 0; i < ntups; i++)
3471  {
3472  Oid blobOid;
3473  int loFd;
3474 
3475  blobOid = atooid(PQgetvalue(res, i, 0));
3476  /* Open the BLOB */
3477  loFd = lo_open(conn, blobOid, INV_READ);
3478  if (loFd == -1)
3479  fatal("could not open large object %u: %s",
3480  blobOid, PQerrorMessage(conn));
3481 
3482  StartBlob(fout, blobOid);
3483 
3484  /* Now read it in chunks, sending data to archive */
3485  do
3486  {
3487  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3488  if (cnt < 0)
3489  fatal("error reading large object %u: %s",
3490  blobOid, PQerrorMessage(conn));
3491 
3492  WriteData(fout, buf, cnt);
3493  } while (cnt > 0);
3494 
3495  lo_close(conn, loFd);
3496 
3497  EndBlob(fout, blobOid);
3498  }
3499 
3500  PQclear(res);
3501  } while (ntups > 0);
3502 
3503  return 1;
3504 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6578
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
#define INV_READ
Definition: libpq-fs.h:22
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:99
int StartBlob(Archive *AH, Oid oid)
PGconn * conn
Definition: streamutil.c:56
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:695
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:107
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:391
#define fatal(...)
int EndBlob(Archive *AH, Oid oid)
int i
#define LOBBUFSIZE
#define pg_log_info(...)
Definition: logging.h:87
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:403
int remoteVersion
Definition: pg_backup.h:187

◆ dumpCast()

static void dumpCast ( Archive fout,
CastInfo cast 
)
static

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

12163 {
12164  DumpOptions *dopt = fout->dopt;
12165  PQExpBuffer defqry;
12166  PQExpBuffer delqry;
12167  PQExpBuffer labelq;
12168  PQExpBuffer castargs;
12169  FuncInfo *funcInfo = NULL;
12170  char *sourceType;
12171  char *targetType;
12172 
12173  /* Skip if not to be dumped */
12174  if (!cast->dobj.dump || dopt->dataOnly)
12175  return;
12176 
12177  /* Cannot dump if we don't have the cast function's info */
12178  if (OidIsValid(cast->castfunc))
12179  {
12180  funcInfo = findFuncByOid(cast->castfunc);
12181  if (funcInfo == NULL)
12182  fatal("could not find function definition for function with OID %u",
12183  cast->castfunc);
12184  }
12185 
12186  defqry = createPQExpBuffer();
12187  delqry = createPQExpBuffer();
12188  labelq = createPQExpBuffer();
12189  castargs = createPQExpBuffer();
12190 
12191  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12192  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12193  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12194  sourceType, targetType);
12195 
12196  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12197  sourceType, targetType);
12198 
12199  switch (cast->castmethod)
12200  {
12201  case COERCION_METHOD_BINARY:
12202  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12203  break;
12204  case COERCION_METHOD_INOUT:
12205  appendPQExpBufferStr(defqry, "WITH INOUT");
12206  break;
12207  case COERCION_METHOD_FUNCTION:
12208  if (funcInfo)
12209  {
12210  char *fsig = format_function_signature(fout, funcInfo, true);
12211 
12212  /*
12213  * Always qualify the function name (format_function_signature
12214  * won't qualify it).
12215  */
12216  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12217  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12218  free(fsig);
12219  }
12220  else
12221  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12222  break;
12223  default:
12224  pg_log_warning("bogus value in pg_cast.castmethod field");
12225  }
12226 
12227  if (cast->castcontext == 'a')
12228  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12229  else if (cast->castcontext == 'i')
12230  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12231  appendPQExpBufferStr(defqry, ";\n");
12232 
12233  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12234  sourceType, targetType);
12235 
12236  appendPQExpBuffer(castargs, "(%s AS %s)",
12237  sourceType, targetType);
12238 
12239  if (dopt->binary_upgrade)
12240  binary_upgrade_extension_member(defqry, &cast->dobj,
12241  "CAST", castargs->data, NULL);
12242 
12243  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12244  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12245  ARCHIVE_OPTS(.tag = labelq->data,
12246  .description = "CAST",
12247  .section = SECTION_PRE_DATA,
12248  .createStmt = defqry->data,
12249  .dropStmt = delqry->data));
12250 
12251  /* Dump Cast Comments */
12252  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12253  dumpComment(fout, "CAST", castargs->data,
12254  NULL, "",
12255  cast->dobj.catId, 0, cast->dobj.dumpId);
12256 
12257  free(sourceType);
12258  free(targetType);
12259 
12260  destroyPQExpBuffer(defqry);
12261  destroyPQExpBuffer(delqry);
12262  destroyPQExpBuffer(labelq);
12263  destroyPQExpBuffer(castargs);
12264 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18287
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
Oid castsource
Definition: pg_dump.h:467
DumpComponents dump
Definition: pg_dump.h:131
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpableObject dobj
Definition: pg_dump.h:195
DumpId dumpId
Definition: pg_dump.h:129
Oid castfunc
Definition: pg_dump.h:469
#define OidIsValid(objectId)
Definition: c.h:638
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpableObject dobj
Definition: pg_dump.h:466
Oid casttarget
Definition: pg_dump.h:468
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:849
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11541
#define fatal(...)
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4473
CatalogId catId
Definition: pg_dump.h:128
char castmethod
Definition: pg_dump.h:471
#define pg_log_warning(...)
Definition: pgfnames.c:24
char castcontext
Definition: pg_dump.h:470
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:9459

◆ dumpCollation()

static void dumpCollation ( Archive fout,
CollInfo collinfo 
)
static

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

13416 {
13417  DumpOptions *dopt = fout->dopt;
13418  PQExpBuffer query;
13419  PQExpBuffer q;
13420  PQExpBuffer delq;
13421  char *qcollname;
13422  PGresult *res;
13423  int i_collprovider;
13424  int i_collisdeterministic;
13425  int i_collcollate;
13426  int i_collctype;
13427  const char *collprovider;
13428  const char *collcollate;
13429  const char *collctype;
13430 
13431  /* Skip if not to be dumped */
13432  if (!collinfo->dobj.dump || dopt->dataOnly)
13433  return;
13434 
13435  query = createPQExpBuffer();
13436  q = createPQExpBuffer();
13437  delq = createPQExpBuffer();
13438 
13439  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13440 
13441  /* Get collation-specific details */
13442  appendPQExpBufferStr(query, "SELECT ");
13443 
13444  if (fout->remoteVersion >= 100000)
13445  appendPQExpBufferStr(query,
13446  "collprovider, "
13447  "collversion, ");
13448  else
13449  appendPQExpBufferStr(query,
13450  "'c' AS collprovider, "
13451  "NULL AS collversion, ");
13452 
13453  if (fout->remoteVersion >= 120000)
13454  appendPQExpBufferStr(query,
13455  "collisdeterministic, ");
13456  else
13457  appendPQExpBufferStr(query,
13458  "true AS collisdeterministic, ");
13459 
13460  appendPQExpBuffer(query,
13461  "collcollate, "
13462  "collctype "
13463  "FROM pg_catalog.pg_collation c "
13464  "WHERE c.oid = '%u'::pg_catalog.oid",
13465  collinfo->dobj.catId.oid);
13466 
13467  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13468 
13469  i_collprovider = PQfnumber(res, "collprovider");
13470  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13471  i_collcollate = PQfnumber(res, "collcollate");
13472  i_collctype = PQfnumber(res, "collctype");
13473 
13474  collprovider = PQgetvalue(res, 0, i_collprovider);
13475  collcollate = PQgetvalue(res, 0, i_collcollate);
13476  collctype = PQgetvalue(res, 0, i_collctype);
13477 
13478  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13479  fmtQualifiedDumpable(collinfo));
13480 
13481  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13482  fmtQualifiedDumpable(collinfo));
13483 
13484  appendPQExpBufferStr(q, "provider = ");
13485  if (collprovider[0] == 'c')
13486  appendPQExpBufferStr(q, "libc");
13487  else if (collprovider[0] == 'i')
13488  appendPQExpBufferStr(q, "icu");
13489  else if (collprovider[0] == 'd')
13490  /* to allow dumping pg_catalog; not accepted on input */
13491  appendPQExpBufferStr(q, "default");
13492  else
13493  fatal("unrecognized collation provider: %s",
13494  collprovider);
13495 
13496  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13497  appendPQExpBufferStr(q, ", deterministic = false");
13498 
13499  if (strcmp(collcollate, collctype) == 0)
13500  {
13501  appendPQExpBufferStr(q, ", locale = ");
13502  appendStringLiteralAH(q, collcollate, fout);
13503  }
13504  else
13505  {
13506  appendPQExpBufferStr(q, ", lc_collate = ");
13507  appendStringLiteralAH(q, collcollate, fout);
13508  appendPQExpBufferStr(q, ", lc_ctype = ");
13509  appendStringLiteralAH(q, collctype, fout);
13510  }
13511 
13512  /*
13513  * For binary upgrade, carry over the collation version. For normal
13514  * dump/restore, omit the version, so that it is computed upon restore.
13515  */
13516  if (dopt->binary_upgrade)
13517  {
13518  int i_collversion;
13519 
13520  i_collversion = PQfnumber(res, "collversion");
13521  if (!PQgetisnull(res, 0, i_collversion))
13522  {
13523  appendPQExpBufferStr(q, ", version = ");
13525  PQgetvalue(res, 0, i_collversion),
13526  fout);
13527  }
13528  }
13529 
13530  appendPQExpBufferStr(q, ");\n");
13531 
13532  if (dopt->binary_upgrade)
13533  binary_upgrade_extension_member(q, &collinfo->dobj,
13534  "COLLATION", qcollname,
13535  collinfo->dobj.namespace->dobj.name);
13536 
13537  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13538  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13539  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13540  .namespace = collinfo->dobj.namespace->dobj.name,
13541  .owner = collinfo->rolname,
13542  .description = "COLLATION",
13543  .section = SECTION_PRE_DATA,
13544  .createStmt = q->data,
13545  .dropStmt = delq->data));
13546 
13547  /* Dump Collation Comments */
13548  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13549  dumpComment(fout, "COLLATION", qcollname,
13550  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13551  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13552 
13553  PQclear(res);
13554 
13555  destroyPQExpBuffer(query);
13556  destroyPQExpBuffer(q);
13557  destroyPQExpBuffer(delq);
13558  free(qcollname);
13559 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpId dumpId
Definition: pg_dump.h:129
DumpableObject dobj
Definition: pg_dump.h:243
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
char * rolname
Definition: pg_dump.h:244
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
#define fatal(...)
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4473
CatalogId catId
Definition: pg_dump.h:128
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189
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:9459
int remoteVersion
Definition: pg_backup.h:187

◆ dumpComment()

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

Definition at line 9459 of file pg_dump.c.

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

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

9462 {
9463  DumpOptions *dopt = fout->dopt;
9464  CommentItem *comments;
9465  int ncomments;
9466 
9467  /* do nothing, if --no-comments is supplied */
9468  if (dopt->no_comments)
9469  return;
9470 
9471  /* Comments are schema not data ... except blob comments are data */
9472  if (strcmp(type, "LARGE OBJECT") != 0)
9473  {
9474  if (dopt->dataOnly)
9475  return;
9476  }
9477  else
9478  {
9479  /* We do dump blob comments in binary-upgrade mode */
9480  if (dopt->schemaOnly && !dopt->binary_upgrade)
9481  return;
9482  }
9483 
9484  /* Search for comments associated with catalogId, using table */
9485  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9486  &comments);
9487 
9488  /* Is there one matching the subid? */
9489  while (ncomments > 0)
9490  {
9491  if (comments->objsubid == subid)
9492  break;
9493  comments++;
9494  ncomments--;
9495  }
9496 
9497  /* If a comment exists, build COMMENT ON statement */
9498  if (ncomments > 0)
9499  {
9500  PQExpBuffer query = createPQExpBuffer();
9502 
9503  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9504  if (namespace && *namespace)
9505  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9506  appendPQExpBuffer(query, "%s IS ", name);
9507  appendStringLiteralAH(query, comments->descr, fout);
9508  appendPQExpBufferStr(query, ";\n");
9509 
9510  appendPQExpBuffer(tag, "%s %s", type, name);
9511 
9512  /*
9513  * We mark comments as SECTION_NONE because they really belong in the
9514  * same section as their parent, whether that is pre-data or
9515  * post-data.
9516  */
9518  ARCHIVE_OPTS(.tag = tag->data,
9519  .namespace = namespace,
9520  .owner = owner,
9521  .description = "COMMENT",
9522  .section = SECTION_NONE,
9523  .createStmt = query->data,
9524  .deps = &dumpId,
9525  .nDeps = 1));
9526 
9527  destroyPQExpBuffer(query);
9528  destroyPQExpBuffer(tag);
9529  }
9530 }
#define ARCHIVE_OPTS(...)
Oid tableoid
Definition: pg_backup.h:230
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
bool schemaOnly
Definition: pg_backup.h:138
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:9638
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
const char * descr
Definition: pg_dump.c:71
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
int no_comments
Definition: pg_backup.h:149
const char * name
Definition: encode.c:521
static const CatalogId nilCatalogId
Definition: pg_dump.c:134
DumpId createDumpId(void)
Definition: common.c:588

◆ dumpCompositeType()

static void dumpCompositeType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10942 of file pg_dump.c.

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

Referenced by dumpType().

10943 {
10944  DumpOptions *dopt = fout->dopt;
10946  PQExpBuffer dropped = createPQExpBuffer();
10947  PQExpBuffer delq = createPQExpBuffer();
10948  PQExpBuffer query = createPQExpBuffer();
10949  PGresult *res;
10950  char *qtypname;
10951  char *qualtypname;
10952  int ntups;
10953  int i_attname;
10954  int i_atttypdefn;
10955  int i_attlen;
10956  int i_attalign;
10957  int i_attisdropped;
10958  int i_attcollation;
10959  int i;
10960  int actual_atts;
10961 
10962  /* Fetch type specific details */
10963  if (fout->remoteVersion >= 90100)
10964  {
10965  /*
10966  * attcollation is new in 9.1. Since we only want to dump COLLATE
10967  * clauses for attributes whose collation is different from their
10968  * type's default, we use a CASE here to suppress uninteresting
10969  * attcollations cheaply. atttypid will be 0 for dropped columns;
10970  * collation does not matter for those.
10971  */
10972  appendPQExpBuffer(query, "SELECT a.attname, "
10973  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10974  "a.attlen, a.attalign, a.attisdropped, "
10975  "CASE WHEN a.attcollation <> at.typcollation "
10976  "THEN a.attcollation ELSE 0 END AS attcollation "
10977  "FROM pg_catalog.pg_type ct "
10978  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
10979  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
10980  "WHERE ct.oid = '%u'::pg_catalog.oid "
10981  "ORDER BY a.attnum ",
10982  tyinfo->dobj.catId.oid);
10983  }
10984  else
10985  {
10986  /*
10987  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
10988  * should always be false.
10989  */
10990  appendPQExpBuffer(query, "SELECT a.attname, "
10991  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10992  "a.attlen, a.attalign, a.attisdropped, "
10993  "0 AS attcollation "
10994  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
10995  "WHERE ct.oid = '%u'::pg_catalog.oid "
10996  "AND a.attrelid = ct.typrelid "
10997  "ORDER BY a.attnum ",
10998  tyinfo->dobj.catId.oid);
10999  }
11000 
11001  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11002 
11003  ntups = PQntuples(res);
11004 
11005  i_attname = PQfnumber(res, "attname");
11006  i_atttypdefn = PQfnumber(res, "atttypdefn");
11007  i_attlen = PQfnumber(res, "attlen");
11008  i_attalign = PQfnumber(res, "attalign");
11009  i_attisdropped = PQfnumber(res, "attisdropped");
11010  i_attcollation = PQfnumber(res, "attcollation");
11011 
11012  if (dopt->binary_upgrade)
11013  {
11015  tyinfo->dobj.catId.oid,
11016  false);
11017  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11018  }
11019 
11020  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11021  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11022 
11023  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11024  qualtypname);
11025 
11026  actual_atts = 0;
11027  for (i = 0; i < ntups; i++)
11028  {
11029  char *attname;
11030  char *atttypdefn;
11031  char *attlen;
11032  char *attalign;
11033  bool attisdropped;
11034  Oid attcollation;
11035 
11036  attname = PQgetvalue(res, i, i_attname);
11037  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11038  attlen = PQgetvalue(res, i, i_attlen);
11039  attalign = PQgetvalue(res, i, i_attalign);
11040  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11041  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11042 
11043  if (attisdropped && !dopt->binary_upgrade)
11044  continue;
11045 
11046  /* Format properly if not first attr */
11047  if (actual_atts++ > 0)
11048  appendPQExpBufferChar(q, ',');
11049  appendPQExpBufferStr(q, "\n\t");
11050 
11051  if (!attisdropped)
11052  {
11053  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11054 
11055  /* Add collation if not default for the column type */
11056  if (OidIsValid(attcollation))
11057  {
11058  CollInfo *coll;
11059 
11060  coll = findCollationByOid(attcollation);
11061  if (coll)
11062  appendPQExpBuffer(q, " COLLATE %s",
11063  fmtQualifiedDumpable(coll));
11064  }
11065  }
11066  else
11067  {
11068  /*
11069  * This is a dropped attribute and we're in binary_upgrade mode.
11070  * Insert a placeholder for it in the CREATE TYPE command, and set
11071  * length and alignment with direct UPDATE to the catalogs
11072  * afterwards. See similar code in dumpTableSchema().
11073  */
11074  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11075 
11076  /* stash separately for insertion after the CREATE TYPE */
11077  appendPQExpBufferStr(dropped,
11078  "\n-- For binary upgrade, recreate dropped column.\n");
11079  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11080  "SET attlen = %s, "
11081  "attalign = '%s', attbyval = false\n"
11082  "WHERE attname = ", attlen, attalign);
11083  appendStringLiteralAH(dropped, attname, fout);
11084  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11085  appendStringLiteralAH(dropped, qualtypname, fout);
11086  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11087 
11088  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11089  qualtypname);
11090  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11091  fmtId(attname));
11092  }
11093  }
11094  appendPQExpBufferStr(q, "\n);\n");
11095  appendPQExpBufferStr(q, dropped->data);
11096 
11097  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11098 
11099  if (dopt->binary_upgrade)
11101  "TYPE", qtypname,
11102  tyinfo->dobj.namespace->dobj.name);
11103 
11104  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11105  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11106  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11107  .namespace = tyinfo->dobj.namespace->dobj.name,
11108  .owner = tyinfo->rolname,
11109  .description = "TYPE",
11110  .section = SECTION_PRE_DATA,
11111  .createStmt = q->data,
11112  .dropStmt = delq->data));
11113 
11114 
11115  /* Dump Type Comments and Security Labels */
11116  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11117  dumpComment(fout, "TYPE", qtypname,
11118  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11119  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11120 
11121  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11122  dumpSecLabel(fout, "TYPE", qtypname,
11123  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11124  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11125 
11126  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11127  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
11128  qtypname, NULL,
11129  tyinfo->dobj.namespace->dobj.name,
11130  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11131  tyinfo->inittypacl, tyinfo->initrtypacl);
11132 
11133  PQclear(res);
11134  destroyPQExpBuffer(q);
11135  destroyPQExpBuffer(dropped);
11136  destroyPQExpBuffer(delq);
11137  destroyPQExpBuffer(query);
11138  free(qtypname);
11139  free(qualtypname);
11140 
11141  /* Dump any per-column comments */
11142  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11143  dumpCompositeTypeColComments(fout, tyinfo);
11144 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
char * inittypacl
Definition: pg_dump.h:171
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
Oid typrelid
Definition: pg_dump.h:174
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrtypacl
Definition: pg_dump.h:172
static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
Definition: pg_dump.c:11152
char * typacl
Definition: pg_dump.h:169
DumpId dumpId
Definition: pg_dump.h:129
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
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:14950
#define OidIsValid(objectId)
Definition: c.h:638
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14863
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
Oid attcollation
Definition: pg_attribute.h:164
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
char attalign
Definition: pg_attribute.h:129
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4403
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define atooid(x)
Definition: postgres_ext.h:42
int16 attlen
Definition: pg_attribute.h:64
DumpableObject dobj
Definition: pg_dump.h:162
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid, bool force_array_type)
Definition: pg_dump.c:4282
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
char * rtypacl
Definition: pg_dump.h:170
char * rolname
Definition: pg_dump.h:168
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:871
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:92
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4473
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
int i
static void dumpComment(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9459
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:403
int remoteVersion
Definition: pg_backup.h:187

◆ dumpCompositeTypeColComments()

static void dumpCompositeTypeColComments ( Archive fout,
TypeInfo tyinfo 
)
static

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

11153 {
11154  CommentItem *comments;
11155  int ncomments;
11156  PGresult *res;
11157  PQExpBuffer query;
11158  PQExpBuffer target;
11159  Oid pgClassOid;
11160  int i;
11161  int ntups;
11162  int i_attname;
11163  int i_attnum;
11164 
11165  /* do nothing, if --no-comments is supplied */
11166  if (fout->dopt->no_comments)
11167  return;
11168 
11169  query = createPQExpBuffer();
11170 
11171  appendPQExpBuffer(query,
11172  "SELECT c.tableoid, a.attname, a.attnum "
11173  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
11174  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
11175  " AND NOT a.attisdropped "
11176  "ORDER BY a.attnum ",
11177  tyinfo->typrelid);
11178 
11179  /* Fetch column attnames */
11180  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11181 
11182  ntups = PQntuples(res);
11183  if (ntups < 1)
11184  {
11185  PQclear(res);
11186  destroyPQExpBuffer(query);
11187  return;
11188  }
11189 
11190  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
11191 
11192  /* Search for comments associated with type's pg_class OID */
11193  ncomments = findComments(fout,
11194  pgClassOid,
11195  tyinfo->typrelid,
11196  &comments);
11197 
11198  /* If no comments exist, we're done */
11199  if (ncomments <= 0)
11200  {
11201  PQclear(res);
11202  destroyPQExpBuffer(query);
11203  return;
11204  }
11205 
11206  /* Build COMMENT ON statements */
11207  target = createPQExpBuffer();
11208 
11209  i_attnum = PQfnumber(res, "attnum");
11210  i_attname = PQfnumber(res, "attname");
11211  while (ncomments > 0)
11212  {
11213  const char *attname;
11214 
11215  attname = NULL;
11216  for (i = 0; i < ntups; i++)
11217  {
11218  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
11219  {
11220  attname = PQgetvalue(res, i, i_attname);
11221  break;
11222  }
11223  }
11224  if (attname) /* just in case we don't find it */
11225  {
11226  const char *descr = comments->descr;
11227 
11228  resetPQExpBuffer(target);
11229  appendPQExpBuffer(target, "COLUMN %s.",
11230  fmtId(tyinfo->dobj.name));
11231  appendPQExpBufferStr(target, fmtId(attname));
11232 
11233  resetPQExpBuffer(query);
11234  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11235  fmtQualifiedDumpable(tyinfo));
11236  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11237  appendStringLiteralAH(query, descr, fout);
11238  appendPQExpBufferStr(query, ";\n");
11239 
11241  ARCHIVE_OPTS(.tag = target->data,
11242  .namespace = tyinfo->dobj.namespace->dobj.name,
11243  .owner = tyinfo->rolname,
11244  .description = "COMMENT",
11245  .section = SECTION_NONE,
11246  .createStmt = query->data,
11247  .deps = &(tyinfo->dobj.dumpId),
11248  .nDeps = 1));
11249  }
11250 
11251  comments++;
11252  ncomments--;
11253  }
11254 
11255  PQclear(res);
11256  destroyPQExpBuffer(query);
11257  destroyPQExpBuffer(target);
11258 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
Oid typrelid
Definition: pg_dump.h:174
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:74
DumpId dumpId
Definition: pg_dump.h:129
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9638
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:71
DumpableObject dobj
Definition: pg_dump.h:162
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
char * rolname
Definition: pg_dump.h:168
int no_comments
Definition: pg_backup.h:149
int i
static const CatalogId nilCatalogId
Definition: pg_dump.c:134
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:403
DumpId createDumpId(void)
Definition: common.c:588

◆ dumpConstraint()

static void dumpConstraint ( Archive fout,
ConstraintInfo coninfo 
)
static

Definition at line 16538 of file pg_dump.c.

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

16539 {
16540  DumpOptions *dopt = fout->dopt;
16541  TableInfo *tbinfo = coninfo->contable;
16542  PQExpBuffer q;
16543  PQExpBuffer delq;
16544  char *tag = NULL;
16545 
16546  /* Skip if not to be dumped */
16547  if (!coninfo->dobj.dump || dopt->dataOnly)
16548  return;
16549 
16550  q = createPQExpBuffer();
16551  delq = createPQExpBuffer();
16552 
16553  if (coninfo->contype == 'p' ||
16554  coninfo->contype == 'u' ||
16555  coninfo->contype == 'x')
16556  {
16557  /* Index-related constraint */
16558  IndxInfo *indxinfo;
16559  int k;
16560 
16561  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16562 
16563  if (indxinfo == NULL)
16564  fatal("missing index for constraint \"%s\"",
16565  coninfo->dobj.name);
16566 
16567  if (dopt->binary_upgrade)
16569  indxinfo->dobj.catId.oid, true);
16570 
16571  appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
16572  fmtQualifiedDumpable(tbinfo));
16573  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
16574  fmtId(coninfo->dobj.name));
16575 
16576  if (coninfo->condef)
16577  {
16578  /* pg_get_constraintdef should have provided everything */
16579  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16580  }
16581  else
16582  {
16583  appendPQExpBuffer(q, "%s (",
16584  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16585  for (k = 0; k < indxinfo->indnkeyattrs; k++)
16586  {
16587  int indkey = (int) indxinfo->indkeys[k];
16588  const char *attname;
16589 
16590  if (indkey == InvalidAttrNumber)
16591  break;
16592  attname = getAttrName(indkey, tbinfo);
16593 
16594  appendPQExpBuffer(q, "%s%s",
16595  (k == 0) ? "" : ", ",
16596  fmtId(attname));
16597  }
16598 
16599  if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
16600  appendPQExpBufferStr(q, ") INCLUDE (");
16601 
16602  for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
16603  {
16604  int indkey = (int) indxinfo->indkeys[k];
16605  const char *attname;
16606 
16607  if (indkey == InvalidAttrNumber)
16608  break;
16609  attname = getAttrName(indkey, tbinfo);
16610 
16611  appendPQExpBuffer(q, "%s%s",
16612  (k == indxinfo->indnkeyattrs) ? "" : ", ",
16613  fmtId(attname));
16614  }
16615 
16616  appendPQExpBufferChar(q, ')');
16617 
16618  if (nonemptyReloptions(indxinfo->indreloptions))
16619  {
16620  appendPQExpBufferStr(q, " WITH (");
16621  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16622  appendPQExpBufferChar(q, ')');
16623  }
16624 
16625  if (coninfo->condeferrable)
16626  {
16627  appendPQExpBufferStr(q, " DEFERRABLE");
16628  if (coninfo->condeferred)
16629  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16630  }
16631 
16632  appendPQExpBufferStr(q, ";\n");
16633  }
16634 
16635  /*
16636  * Append ALTER TABLE commands as needed to set properties that we
16637  * only have ALTER TABLE syntax for. Keep this in sync with the
16638  * similar code in dumpIndex!
16639  */
16640 
16641  /* If the index is clustered, we need to record that. */
16642  if (indxinfo->indisclustered)
16643  {
16644  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16645  fmtQualifiedDumpable(tbinfo));
16646  /* index name is not qualified in this syntax */
16647  appendPQExpBuffer(q, " ON %s;\n",
16648  fmtId(indxinfo->dobj.name));
16649  }
16650 
16651  /* If the index defines identity, we need to record that. */
16652  if (indxinfo->indisreplident)
16653  {
16654  appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
16655  fmtQualifiedDumpable(tbinfo));
16656  /* index name is not qualified in this syntax */
16657  appendPQExpBuffer(q, " INDEX %s;\n",
16658  fmtId(indxinfo->dobj.name));
16659  }
16660 
16661  appendPQExpBuffer(delq, "ALTER TABLE ONLY %s ",
16662  fmtQualifiedDumpable(tbinfo));
16663  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16664  fmtId(coninfo->dobj.name));
16665 
16666  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16667 
16668  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16669  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16670  ARCHIVE_OPTS(.tag = tag,
16671  .namespace = tbinfo->dobj.namespace->dobj.name,
16672  .tablespace = indxinfo->tablespace,
16673  .owner = tbinfo->rolname,
16674  .description = "CONSTRAINT",
16675  .section = SECTION_POST_DATA,
16676  .createStmt = q->data,
16677  .dropStmt = delq->data));
16678  }
16679  else if (coninfo->contype == 'f')
16680  {
16681  char *only;
16682 
16683  /*
16684  * Foreign keys on partitioned tables are always declared as
16685  * inheriting to partitions; for all other cases, emit them as
16686  * applying ONLY directly to the named table, because that's how they
16687  * work for regular inherited tables.
16688  */
16689  only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
16690 
16691  /*
16692  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
16693  * current table data is not processed
16694  */
16695  appendPQExpBuffer(q, "ALTER TABLE %s%s\n",
16696  only, fmtQualifiedDumpable(tbinfo));
16697  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16698  fmtId(coninfo->dobj.name),
16699  coninfo->condef);
16700 
16701  appendPQExpBuffer(delq, "ALTER TABLE %s%s ",
16702  only, fmtQualifiedDumpable(tbinfo));
16703  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16704  fmtId(coninfo->dobj.name));
16705 
16706  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16707 
16708  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16709  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16710  ARCHIVE_OPTS(.tag = tag,
16711  .namespace = tbinfo->dobj.namespace->dobj.name,
16712  .owner = tbinfo->rolname,
16713  .description = "FK CONSTRAINT",
16714  .section = SECTION_POST_DATA,
16715  .createStmt = q->data,
16716  .dropStmt = delq->data));
16717  }
16718  else if (coninfo->contype == 'c' && tbinfo)
16719  {
16720  /* CHECK constraint on a table */
16721 
16722  /* Ignore if not to be dumped separately, or if it was inherited */
16723  if (coninfo->separate && coninfo->conislocal)
16724  {
16725  /* not ONLY since we want it to propagate to children */
16726  appendPQExpBuffer(q, "ALTER TABLE %s\n",
16727  fmtQualifiedDumpable(tbinfo));
16728  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16729  fmtId(coninfo->dobj.name),
16730  coninfo->condef);
16731 
16732  appendPQExpBuffer(delq, "ALTER TABLE %s ",
16733  fmtQualifiedDumpable(tbinfo));
16734  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16735  fmtId(coninfo->dobj.name));
16736 
16737  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16738 
16739  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16740  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16741  ARCHIVE_OPTS(.tag = tag,
16742  .namespace = tbinfo->dobj.namespace->dobj.name,
16743  .owner = tbinfo->rolname,
16744  .description = "CHECK CONSTRAINT",
16745  .section = SECTION_POST_DATA,
16746  .createStmt = q->data,
16747  .dropStmt = delq->data));
16748  }
16749  }
16750  else if (coninfo->contype == 'c' && tbinfo == NULL)
16751  {
16752  /* CHECK constraint on a domain */
16753  TypeInfo *tyinfo = coninfo->condomain;
16754 
16755  /* Ignore if not to be dumped separately */
16756  if (coninfo->separate)
16757  {
16758  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
16759  fmtQualifiedDumpable(tyinfo));
16760  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16761  fmtId(coninfo->dobj.name),
16762  coninfo->condef);
16763 
16764  appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
16765  fmtQualifiedDumpable(tyinfo));
16766  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16767  fmtId(coninfo->dobj.name));
16768 
16769  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
16770 
16771  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16772  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16773  ARCHIVE_OPTS(.tag = tag,
16774  .namespace = tyinfo->dobj.namespace->dobj.name,
16775  .owner = tyinfo->rolname,
16776  .description = "CHECK CONSTRAINT",
16777  .section = SECTION_POST_DATA,
16778  .createStmt = q->data,
16779  .dropStmt = delq->data));
16780  }
16781  }
16782  else
16783  {
16784  fatal("unrecognized constraint type: %c",
16785  coninfo->contype);
16786  }
16787 
16788  /* Dump Constraint Comments --- only works for table constraints */
16789  if (tbinfo && coninfo->separate &&
16790  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16791  dumpTableConstraintComment(fout, coninfo);
16792 
16793  free(tag);
16794  destroyPQExpBuffer(q);
16795  destroyPQExpBuffer(delq);
16796 }
char * tablespace
Definition: pg_dump.h:359
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
char relkind
Definition: pg_dump.h:264
DumpComponents dump
Definition: pg_dump.h:131
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
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:18373
char * rolname
Definition: pg_dump.h:259
DumpId dumpId
Definition: pg_dump.h:129
DumpId conindex
Definition: pg_dump.h:443
DumpableObject dobj
Definition: pg_dump.h:258
char * condef
Definition: pg_dump.h:441
bool indisreplident
Definition: pg_dump.h:368
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
int binary_upgrade
Definition: pg_backup.h:135
DumpableObject dobj
Definition: pg_dump.h:437
DumpOptions * dopt
Definition: pg_backup.h:182
static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
Definition: pg_dump.c:16806
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:608
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
Oid * indkeys
Definition: pg_dump.h:365
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4403
static const char * getAttrName(int attrnum, TableInfo *tblInfo)
Definition: pg_dump.c:16283
bool condeferrable
Definition: pg_dump.h:444
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18361
char * indreloptions
Definition: pg_dump.h:360
DumpableObject dobj
Definition: pg_dump.h:162
TypeInfo * condomain
Definition: pg_dump.h:439
bool conislocal
Definition: pg_dump.h:446
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
TableInfo * contable
Definition: pg_dump.h:438
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
bool condeferred
Definition: pg_dump.h:445
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
char * rolname
Definition: pg_dump.h:168
#define fatal(...)
#define InvalidAttrNumber
Definition: attnum.h:23
int indnkeyattrs
Definition: pg_dump.h:363
int indnattrs
Definition: pg_dump.h:364
CatalogId catId
Definition: pg_dump.h:128
DumpableObject dobj
Definition: pg_dump.h:356
bool indisclustered
Definition: pg_dump.h:367

◆ dumpConversion()

static void dumpConversion ( Archive fout,
ConvInfo convinfo 
)
static

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

13567 {
13568  DumpOptions *dopt = fout->dopt;
13569  PQExpBuffer query;
13570  PQExpBuffer q;
13571  PQExpBuffer delq;
13572  char *qconvname;
13573  PGresult *res;
13574  int i_conforencoding;
13575  int i_contoencoding;
13576  int i_conproc;
13577  int i_condefault;
13578  const char *conforencoding;
13579  const char *contoencoding;
13580  const char *conproc;
13581  bool condefault;
13582 
13583  /* Skip if not to be dumped */
13584  if (!convinfo->dobj.dump || dopt->dataOnly)
13585  return;
13586 
13587  query = createPQExpBuffer();
13588  q = createPQExpBuffer();
13589  delq = createPQExpBuffer();
13590 
13591  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13592 
13593  /* Get conversion-specific details */
13594  appendPQExpBuffer(query, "SELECT "
13595  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13596  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13597  "conproc, condefault "
13598  "FROM pg_catalog.pg_conversion c "
13599  "WHERE c.oid = '%u'::pg_catalog.oid",
13600  convinfo->dobj.catId.oid);
13601 
13602  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13603 
13604  i_conforencoding = PQfnumber(res, "conforencoding");
13605  i_contoencoding = PQfnumber(res, "contoencoding");
13606  i_conproc = PQfnumber(res, "conproc");
13607  i_condefault = PQfnumber(res, "condefault");
13608 
13609  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13610  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13611  conproc = PQgetvalue(res, 0, i_conproc);
13612  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13613 
13614  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13615  fmtQualifiedDumpable(convinfo));
13616 
13617  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13618  (condefault) ? "DEFAULT " : "",
13619  fmtQualifiedDumpable(convinfo));
13620  appendStringLiteralAH(q, conforencoding, fout);
13621  appendPQExpBufferStr(q, " TO ");
13622  appendStringLiteralAH(q, contoencoding, fout);
13623  /* regproc output is already sufficiently quoted */
13624  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13625 
13626  if (dopt->binary_upgrade)
13627  binary_upgrade_extension_member(q, &convinfo->dobj,
13628  "CONVERSION", qconvname,
13629  convinfo->dobj.namespace->dobj.name);
13630 
13631  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13632  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13633  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13634  .namespace = convinfo->dobj.namespace->dobj.name,
13635  .owner = convinfo->rolname,
13636  .description = "CONVERSION",
13637  .section = SECTION_PRE_DATA,
13638  .createStmt = q->data,
13639  .dropStmt = delq->data));
13640 
13641  /* Dump Conversion Comments */
13642  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13643  dumpComment(fout, "CONVERSION", qconvname,
13644  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13645  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13646 
13647  PQclear(res);
13648 
13649  destroyPQExpBuffer(query);
13650  destroyPQExpBuffer(q);
13651  destroyPQExpBuffer(delq);
13652  free(qconvname);
13653 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * rolname
Definition: pg_dump.h:250
DumpId dumpId
Definition: pg_dump.h:129
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:418
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:149
DumpableObject dobj
Definition: pg_dump.h:249
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4473
CatalogId catId
Definition: pg_dump.h:128
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:9459

◆ dumpDatabase()