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

Go to the source code of this file.

Data Structures

struct  CommentItem
 
struct  SecLabelItem
 

Macros

#define DUMP_DEFAULT_ROWS_PER_INSERT   1
 
#define fmtQualifiedDumpable(obj)
 

Typedefs

typedef enum OidOptions OidOptions
 

Enumerations

enum  OidOptions { 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 141 of file pg_dump.c.

Referenced by main().

◆ fmtQualifiedDumpable

Typedef Documentation

◆ OidOptions

typedef enum OidOptions OidOptions

Enumeration Type Documentation

◆ OidOptions

enum OidOptions
Enumerator
zeroAsOpaque 
zeroAsAny 
zeroAsStar 
zeroAsNone 

Definition at line 83 of file pg_dump.c.

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

Function Documentation

◆ addBoundaryDependencies()

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

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

18127 {
18128  DumpableObject *preDataBound = boundaryObjs + 0;
18129  DumpableObject *postDataBound = boundaryObjs + 1;
18130  int i;
18131 
18132  for (i = 0; i < numObjs; i++)
18133  {
18134  DumpableObject *dobj = dobjs[i];
18135 
18136  /*
18137  * The classification of object types here must match the SECTION_xxx
18138  * values assigned during subsequent ArchiveEntry calls!
18139  */
18140  switch (dobj->objType)
18141  {
18142  case DO_NAMESPACE:
18143  case DO_EXTENSION:
18144  case DO_TYPE:
18145  case DO_SHELL_TYPE:
18146  case DO_FUNC:
18147  case DO_AGG:
18148  case DO_OPERATOR:
18149  case DO_ACCESS_METHOD:
18150  case DO_OPCLASS:
18151  case DO_OPFAMILY:
18152  case DO_COLLATION:
18153  case DO_CONVERSION:
18154  case DO_TABLE:
18155  case DO_ATTRDEF:
18156  case DO_PROCLANG:
18157  case DO_CAST:
18158  case DO_DUMMY_TYPE:
18159  case DO_TSPARSER:
18160  case DO_TSDICT:
18161  case DO_TSTEMPLATE:
18162  case DO_TSCONFIG:
18163  case DO_FDW:
18164  case DO_FOREIGN_SERVER:
18165  case DO_TRANSFORM:
18166  case DO_BLOB:
18167  /* Pre-data objects: must come before the pre-data boundary */
18168  addObjectDependency(preDataBound, dobj->dumpId);
18169  break;
18170  case DO_TABLE_DATA:
18171  case DO_SEQUENCE_SET:
18172  case DO_BLOB_DATA:
18173  /* Data objects: must come between the boundaries */
18174  addObjectDependency(dobj, preDataBound->dumpId);
18175  addObjectDependency(postDataBound, dobj->dumpId);
18176  break;
18177  case DO_INDEX:
18178  case DO_INDEX_ATTACH:
18179  case DO_STATSEXT:
18180  case DO_REFRESH_MATVIEW:
18181  case DO_TRIGGER:
18182  case DO_EVENT_TRIGGER:
18183  case DO_DEFAULT_ACL:
18184  case DO_POLICY:
18185  case DO_PUBLICATION:
18186  case DO_PUBLICATION_REL:
18187  case DO_SUBSCRIPTION:
18188  /* Post-data objects: must come after the post-data boundary */
18189  addObjectDependency(dobj, postDataBound->dumpId);
18190  break;
18191  case DO_RULE:
18192  /* Rules are post-data, but only if dumped separately */
18193  if (((RuleInfo *) dobj)->separate)
18194  addObjectDependency(dobj, postDataBound->dumpId);
18195  break;
18196  case DO_CONSTRAINT:
18197  case DO_FK_CONSTRAINT:
18198  /* Constraints are post-data, but only if dumped separately */
18199  if (((ConstraintInfo *) dobj)->separate)
18200  addObjectDependency(dobj, postDataBound->dumpId);
18201  break;
18202  case DO_PRE_DATA_BOUNDARY:
18203  /* nothing to do */
18204  break;
18205  case DO_POST_DATA_BOUNDARY:
18206  /* must come after the pre-data boundary */
18207  addObjectDependency(dobj, preDataBound->dumpId);
18208  break;
18209  }
18210  }
18211 }
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:781
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 18428 of file pg_dump.c.

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

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

18430 {
18431  bool res;
18432 
18433  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18434  fout->std_strings);
18435  if (!res)
18436  pg_log_warning("could not parse reloptions array");
18437 }
int encoding
Definition: pg_backup.h:197
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:741
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool std_strings
Definition: pg_backup.h:198

◆ binary_upgrade_extension_member()

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

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

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

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

◆ binary_upgrade_set_type_oids_by_rel_oid()

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

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

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

◆ binary_upgrade_set_type_oids_by_type_oid()

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

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

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

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

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

18239 {
18240  ArchiveHandle *AH = (ArchiveHandle *) fout;
18241  TocEntry *te;
18242 
18243  /* Scan all TOC entries in the archive */
18244  for (te = AH->toc->next; te != AH->toc; te = te->next)
18245  {
18246  DumpableObject *dobj;
18247  DumpId *dependencies;
18248  int nDeps;
18249  int allocDeps;
18250 
18251  /* No need to process entries that will not be dumped */
18252  if (te->reqs == 0)
18253  continue;
18254  /* Ignore entries that already have "special" dependencies */
18255  if (te->nDeps > 0)
18256  continue;
18257  /* Otherwise, look up the item's original DumpableObject, if any */
18258  dobj = findObjectByDumpId(te->dumpId);
18259  if (dobj == NULL)
18260  continue;
18261  /* No work if it has no dependencies */
18262  if (dobj->nDeps <= 0)
18263  continue;
18264  /* Set up work array */
18265  allocDeps = 64;
18266  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
18267  nDeps = 0;
18268  /* Recursively find all dumpable dependencies */
18269  findDumpableDependencies(AH, dobj,
18270  &dependencies, &nDeps, &allocDeps);
18271  /* And save 'em ... */
18272  if (nDeps > 0)
18273  {
18274  dependencies = (DumpId *) pg_realloc(dependencies,
18275  nDeps * sizeof(DumpId));
18276  te->dependencies = dependencies;
18277  te->nDeps = nDeps;
18278  }
18279  else
18280  free(dependencies);
18281  }
18282 }
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:609
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:18286
#define free(a)
Definition: header.h:65

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

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

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

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

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

1414 {
1415  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1416 
1417  if (ext == NULL)
1418  return false;
1419 
1420  dobj->ext_member = true;
1421 
1422  /* Record dependency so that getDependencies needn't deal with that */
1423  addObjectDependency(dobj, ext->dobj.dumpId);
1424 
1425  /*
1426  * In 9.6 and above, mark the member object to have any non-initial ACL,
1427  * policies, and security labels dumped.
1428  *
1429  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1430  * extract the information about the object. We don't provide support for
1431  * initial policies and security labels and it seems unlikely for those to
1432  * ever exist, but we may have to revisit this later.
1433  *
1434  * Prior to 9.6, we do not include any extension member components.
1435  *
1436  * In binary upgrades, we still dump all components of the members
1437  * individually, since the idea is to exactly reproduce the database
1438  * contents rather than replace the extension contents with something
1439  * different.
1440  */
1441  if (fout->dopt->binary_upgrade)
1442  dobj->dump = ext->dobj.dump;
1443  else
1444  {
1445  if (fout->remoteVersion < 90600)
1446  dobj->dump = DUMP_COMPONENT_NONE;
1447  else
1448  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1451  }
1452 
1453  return true;
1454 }
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:933
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:781
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 9766 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().

9767 {
9768  PGresult *res;
9769  PQExpBuffer query;
9770  int i_description;
9771  int i_classoid;
9772  int i_objoid;
9773  int i_objsubid;
9774  int ntups;
9775  int i;
9776  CommentItem *comments;
9777 
9778  query = createPQExpBuffer();
9779 
9780  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9781  "FROM pg_catalog.pg_description "
9782  "ORDER BY classoid, objoid, objsubid");
9783 
9784  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9785 
9786  /* Construct lookup table containing OIDs in numeric form */
9787 
9788  i_description = PQfnumber(res, "description");
9789  i_classoid = PQfnumber(res, "classoid");
9790  i_objoid = PQfnumber(res, "objoid");
9791  i_objsubid = PQfnumber(res, "objsubid");
9792 
9793  ntups = PQntuples(res);
9794 
9795  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9796 
9797  for (i = 0; i < ntups; i++)
9798  {
9799  comments[i].descr = PQgetvalue(res, i, i_description);
9800  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9801  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9802  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9803  }
9804 
9805  /* Do NOT free the PGresult since we are keeping pointers into it */
9806  destroyPQExpBuffer(query);
9807 
9808  *items = comments;
9809  return ntups;
9810 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:71
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
Oid objoid
Definition: pg_dump.c:70
Oid classoid
Definition: pg_dump.c:69
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401

◆ collectSecLabels()

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

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

15242 {
15243  PGresult *res;
15244  PQExpBuffer query;
15245  int i_label;
15246  int i_provider;
15247  int i_classoid;
15248  int i_objoid;
15249  int i_objsubid;
15250  int ntups;
15251  int i;
15252  SecLabelItem *labels;
15253 
15254  query = createPQExpBuffer();
15255 
15256  appendPQExpBufferStr(query,
15257  "SELECT label, provider, classoid, objoid, objsubid "
15258  "FROM pg_catalog.pg_seclabel "
15259  "ORDER BY classoid, objoid, objsubid");
15260 
15261  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15262 
15263  /* Construct lookup table containing OIDs in numeric form */
15264  i_label = PQfnumber(res, "label");
15265  i_provider = PQfnumber(res, "provider");
15266  i_classoid = PQfnumber(res, "classoid");
15267  i_objoid = PQfnumber(res, "objoid");
15268  i_objsubid = PQfnumber(res, "objsubid");
15269 
15270  ntups = PQntuples(res);
15271 
15272  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15273 
15274  for (i = 0; i < ntups; i++)
15275  {
15276  labels[i].label = PQgetvalue(res, i, i_label);
15277  labels[i].provider = PQgetvalue(res, i, i_provider);
15278  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15279  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15280  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15281  }
15282 
15283  /* Do NOT free the PGresult since we are keeping pointers into it */
15284  destroyPQExpBuffer(query);
15285 
15286  *items = labels;
15287  return ntups;
15288 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
Oid objoid
Definition: pg_dump.c:79
const char * provider
Definition: pg_dump.c:76
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
int objsubid
Definition: pg_dump.c:80
int i
Oid classoid
Definition: pg_dump.c:78
const char * label
Definition: pg_dump.c:77
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401

◆ convertRegProcReference()

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

Definition at line 12663 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

12664 {
12665  char *name;
12666  char *paren;
12667  bool inquote;
12668 
12669  /* In all cases "-" means a null reference */
12670  if (strcmp(proc, "-") == 0)
12671  return NULL;
12672 
12673  name = pg_strdup(proc);
12674  /* find non-double-quoted left paren */
12675  inquote = false;
12676  for (paren = name; *paren; paren++)
12677  {
12678  if (*paren == '(' && !inquote)
12679  {
12680  *paren = '\0';
12681  break;
12682  }
12683  if (*paren == '"')
12684  inquote = !inquote;
12685  }
12686  return name;
12687 }
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 12734 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

12735 {
12736  char *result;
12737  char query[128];
12738  PGresult *res;
12739 
12740  snprintf(query, sizeof(query),
12741  "SELECT '%u'::pg_catalog.regproc", funcOid);
12742  res = ExecuteSqlQueryForSingleRow(fout, query);
12743 
12744  result = pg_strdup(PQgetvalue(res, 0, 0));
12745 
12746  PQclear(res);
12747 
12748  return result;
12749 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define snprintf
Definition: port.h:192

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

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

18102 {
18103  DumpableObject *dobjs;
18104 
18105  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
18106 
18107  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
18108  dobjs[0].catId = nilCatalogId;
18109  AssignDumpId(dobjs + 0);
18110  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
18111 
18112  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
18113  dobjs[1].catId = nilCatalogId;
18114  AssignDumpId(dobjs + 1);
18115  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
18116 
18117  return dobjs;
18118 }
char * name
Definition: pg_dump.h:130
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:545
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
CatalogId catId
Definition: pg_dump.h:128
static const CatalogId nilCatalogId
Definition: pg_dump.c:131
DumpableObjectType objType
Definition: pg_dump.h:127

◆ createDummyViewAsClause()

static PQExpBuffer createDummyViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

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

15478 {
15479  PQExpBuffer result = createPQExpBuffer();
15480  int j;
15481 
15482  appendPQExpBufferStr(result, "SELECT");
15483 
15484  for (j = 0; j < tbinfo->numatts; j++)
15485  {
15486  if (j > 0)
15487  appendPQExpBufferChar(result, ',');
15488  appendPQExpBufferStr(result, "\n ");
15489 
15490  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15491 
15492  /*
15493  * Must add collation if not default for the type, because CREATE OR
15494  * REPLACE VIEW won't change it
15495  */
15496  if (OidIsValid(tbinfo->attcollation[j]))
15497  {
15498  CollInfo *coll;
15499 
15500  coll = findCollationByOid(tbinfo->attcollation[j]);
15501  if (coll)
15502  appendPQExpBuffer(result, " COLLATE %s",
15503  fmtQualifiedDumpable(coll));
15504  }
15505 
15506  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15507  }
15508 
15509  return result;
15510 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char ** atttypnames
Definition: pg_dump.h:302
char ** attnames
Definition: pg_dump.h:301
#define OidIsValid(objectId)
Definition: c.h:645
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:146
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:872

◆ createViewAsClause()

static PQExpBuffer createViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

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

15429 {
15430  PQExpBuffer query = createPQExpBuffer();
15431  PQExpBuffer result = createPQExpBuffer();
15432  PGresult *res;
15433  int len;
15434 
15435  /* Fetch the view definition */
15436  appendPQExpBuffer(query,
15437  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15438  tbinfo->dobj.catId.oid);
15439 
15440  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15441 
15442  if (PQntuples(res) != 1)
15443  {
15444  if (PQntuples(res) < 1)
15445  fatal("query to obtain definition of view \"%s\" returned no data",
15446  tbinfo->dobj.name);
15447  else
15448  fatal("query to obtain definition of view \"%s\" returned more than one definition",
15449  tbinfo->dobj.name);
15450  }
15451 
15452  len = PQgetlength(res, 0, 0);
15453 
15454  if (len == 0)
15455  fatal("definition of view \"%s\" appears to be empty (length zero)",
15456  tbinfo->dobj.name);
15457 
15458  /* Strip off the trailing semicolon so that other things may follow. */
15459  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15460  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15461 
15462  PQclear(res);
15463  destroyPQExpBuffer(query);
15464 
15465  return result;
15466 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3174
char * name
Definition: pg_dump.h:130
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
DumpableObject dobj
Definition: pg_dump.h: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:694
#define Assert(condition)
Definition: c.h:739
#define fatal(...)
CatalogId catId
Definition: pg_dump.h:128
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:399
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401

◆ dumpAccessMethod()

static void dumpAccessMethod ( Archive fout,
AccessMethodInfo oprinfo 
)
static

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

12757 {
12758  DumpOptions *dopt = fout->dopt;
12759  PQExpBuffer q;
12760  PQExpBuffer delq;
12761  char *qamname;
12762 
12763  /* Skip if not to be dumped */
12764  if (!aminfo->dobj.dump || dopt->dataOnly)
12765  return;
12766 
12767  q = createPQExpBuffer();
12768  delq = createPQExpBuffer();
12769 
12770  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12771 
12772  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12773 
12774  switch (aminfo->amtype)
12775  {
12776  case AMTYPE_INDEX:
12777  appendPQExpBufferStr(q, "TYPE INDEX ");
12778  break;
12779  case AMTYPE_TABLE:
12780  appendPQExpBufferStr(q, "TYPE TABLE ");
12781  break;
12782  default:
12783  pg_log_warning("invalid type \"%c\" of access method \"%s\"",
12784  aminfo->amtype, qamname);
12785  destroyPQExpBuffer(q);
12786  destroyPQExpBuffer(delq);
12787  free(qamname);
12788  return;
12789  }
12790 
12791  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12792 
12793  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12794  qamname);
12795 
12796  if (dopt->binary_upgrade)
12797  binary_upgrade_extension_member(q, &aminfo->dobj,
12798  "ACCESS METHOD", qamname, NULL);
12799 
12800  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12801  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12802  ARCHIVE_OPTS(.tag = aminfo->dobj.name,
12803  .description = "ACCESS METHOD",
12804  .section = SECTION_PRE_DATA,
12805  .createStmt = q->data,
12806  .dropStmt = delq->data));
12807 
12808  /* Dump Access Method Comments */
12809  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12810  dumpComment(fout, "ACCESS METHOD", qamname,
12811  NULL, "",
12812  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12813 
12814  destroyPQExpBuffer(q);
12815  destroyPQExpBuffer(delq);
12816  free(qamname);
12817 }
#define ARCHIVE_OPTS(...)
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objtype, const char *objname, const char *objnamespace)
Definition: pg_dump.c:4470
#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:9503

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

14912 {
14913  DumpOptions *dopt = fout->dopt;
14914  PQExpBuffer sql;
14915 
14916  /* Do nothing if ACL dump is not enabled */
14917  if (dopt->aclsSkip)
14918  return;
14919 
14920  /* --data-only skips ACLs *except* BLOB ACLs */
14921  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14922  return;
14923 
14924  sql = createPQExpBuffer();
14925 
14926  /*
14927  * Check to see if this object has had any initial ACLs included for it.
14928  * If so, we are in binary upgrade mode and these are the ACLs to turn
14929  * into GRANT and REVOKE statements to set and record the initial
14930  * privileges for an extension object. Let the backend know that these
14931  * are to be recorded by calling binary_upgrade_set_record_init_privs()
14932  * before and after.
14933  */
14934  if (strlen(initacls) != 0 || strlen(initracls) != 0)
14935  {
14936  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14937  if (!buildACLCommands(name, subname, nspname, type,
14938  initacls, initracls, owner,
14939  "", fout->remoteVersion, sql))
14940  fatal("could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)",
14941  initacls, initracls, name, type);
14942  appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14943  }
14944 
14945  if (!buildACLCommands(name, subname, nspname, type,
14946  acls, racls, owner,
14947  "", fout->remoteVersion, sql))
14948  fatal("could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)",
14949  acls, racls, name, type);
14950 
14951  if (sql->len > 0)
14952  {
14954 
14955  if (subname)
14956  appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
14957  else
14958  appendPQExpBuffer(tag, "%s %s", type, name);
14959 
14961  ARCHIVE_OPTS(.tag = tag->data,
14962  .namespace = nspname,
14963  .owner = owner,
14964  .description = "ACL",
14965  .section = SECTION_NONE,
14966  .createStmt = sql->data,
14967  .deps = &objDumpId,
14968  .nDeps = 1));
14969  destroyPQExpBuffer(tag);
14970  }
14971 
14972  destroyPQExpBuffer(sql);
14973 }
#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:131
DumpId createDumpId(void)
Definition: common.c:589
int remoteVersion
Definition: pg_backup.h:187

◆ dumpAgg()

static void dumpAgg ( Archive fout,
AggInfo agginfo 
)
static

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

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

◆ dumpAttrDef()

static void dumpAttrDef ( Archive fout,
AttrDefInfo adinfo 
)
static

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

16266 {
16267  DumpOptions *dopt = fout->dopt;
16268  TableInfo *tbinfo = adinfo->adtable;
16269  int adnum = adinfo->adnum;
16270  PQExpBuffer q;
16271  PQExpBuffer delq;
16272  char *qualrelname;
16273  char *tag;
16274 
16275  /* Skip if table definition not to be dumped */
16276  if (!tbinfo->dobj.dump || dopt->dataOnly)
16277  return;
16278 
16279  /* Skip if not "separate"; it was dumped in the table's definition */
16280  if (!adinfo->separate)
16281  return;
16282 
16283  q = createPQExpBuffer();
16284  delq = createPQExpBuffer();
16285 
16286  qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16287 
16288  appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16289  qualrelname);
16290  appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
16291  fmtId(tbinfo->attnames[adnum - 1]),
16292  adinfo->adef_expr);
16293 
16294  appendPQExpBuffer(delq, "ALTER TABLE %s ",
16295  qualrelname);
16296  appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
16297  fmtId(tbinfo->attnames[adnum - 1]));
16298 
16299  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16300 
16301  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16302  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16303  ARCHIVE_OPTS(.tag = tag,
16304  .namespace = tbinfo->dobj.namespace->dobj.name,
16305  .owner = tbinfo->rolname,
16306  .description = "DEFAULT",
16307  .section = SECTION_PRE_DATA,
16308  .createStmt = q->data,
16309  .dropStmt = delq->data));
16310 
16311  free(tag);
16312  destroyPQExpBuffer(q);
16313  destroyPQExpBuffer(delq);
16314  free(qualrelname);
16315 }
#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:64
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:146
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 10529 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().

10530 {
10531  DumpOptions *dopt = fout->dopt;
10533  PQExpBuffer delq = createPQExpBuffer();
10534  PQExpBuffer query = createPQExpBuffer();
10535  PGresult *res;
10536  char *qtypname;
10537  char *qualtypname;
10538  char *typlen;
10539  char *typinput;
10540  char *typoutput;
10541  char *typreceive;
10542  char *typsend;
10543  char *typmodin;
10544  char *typmodout;
10545  char *typanalyze;
10546  Oid typreceiveoid;
10547  Oid typsendoid;
10548  Oid typmodinoid;
10549  Oid typmodoutoid;
10550  Oid typanalyzeoid;
10551  char *typcategory;
10552  char *typispreferred;
10553  char *typdelim;
10554  char *typbyval;
10555  char *typalign;
10556  char *typstorage;
10557  char *typcollatable;
10558  char *typdefault;
10559  bool typdefault_is_literal = false;
10560 
10561  /* Fetch type-specific details */
10562  if (fout->remoteVersion >= 90100)
10563  {
10564  appendPQExpBuffer(query, "SELECT typlen, "
10565  "typinput, typoutput, typreceive, typsend, "
10566  "typmodin, typmodout, typanalyze, "
10567  "typreceive::pg_catalog.oid AS typreceiveoid, "
10568  "typsend::pg_catalog.oid AS typsendoid, "
10569  "typmodin::pg_catalog.oid AS typmodinoid, "
10570  "typmodout::pg_catalog.oid AS typmodoutoid, "
10571  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10572  "typcategory, typispreferred, "
10573  "typdelim, typbyval, typalign, typstorage, "
10574  "(typcollation <> 0) AS typcollatable, "
10575  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10576  "FROM pg_catalog.pg_type "
10577  "WHERE oid = '%u'::pg_catalog.oid",
10578  tyinfo->dobj.catId.oid);
10579  }
10580  else if (fout->remoteVersion >= 80400)
10581  {
10582  appendPQExpBuffer(query, "SELECT typlen, "
10583  "typinput, typoutput, typreceive, typsend, "
10584  "typmodin, typmodout, typanalyze, "
10585  "typreceive::pg_catalog.oid AS typreceiveoid, "
10586  "typsend::pg_catalog.oid AS typsendoid, "
10587  "typmodin::pg_catalog.oid AS typmodinoid, "
10588  "typmodout::pg_catalog.oid AS typmodoutoid, "
10589  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10590  "typcategory, typispreferred, "
10591  "typdelim, typbyval, typalign, typstorage, "
10592  "false AS typcollatable, "
10593  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10594  "FROM pg_catalog.pg_type "
10595  "WHERE oid = '%u'::pg_catalog.oid",
10596  tyinfo->dobj.catId.oid);
10597  }
10598  else if (fout->remoteVersion >= 80300)
10599  {
10600  /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
10601  appendPQExpBuffer(query, "SELECT typlen, "
10602  "typinput, typoutput, typreceive, typsend, "
10603  "typmodin, typmodout, typanalyze, "
10604  "typreceive::pg_catalog.oid AS typreceiveoid, "
10605  "typsend::pg_catalog.oid AS typsendoid, "
10606  "typmodin::pg_catalog.oid AS typmodinoid, "
10607  "typmodout::pg_catalog.oid AS typmodoutoid, "
10608  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10609  "'U' AS typcategory, false AS typispreferred, "
10610  "typdelim, typbyval, typalign, typstorage, "
10611  "false AS typcollatable, "
10612  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10613  "FROM pg_catalog.pg_type "
10614  "WHERE oid = '%u'::pg_catalog.oid",
10615  tyinfo->dobj.catId.oid);
10616  }
10617  else
10618  {
10619  appendPQExpBuffer(query, "SELECT typlen, "
10620  "typinput, typoutput, typreceive, typsend, "
10621  "'-' AS typmodin, '-' AS typmodout, "
10622  "typanalyze, "
10623  "typreceive::pg_catalog.oid AS typreceiveoid, "
10624  "typsend::pg_catalog.oid AS typsendoid, "
10625  "0 AS typmodinoid, 0 AS typmodoutoid, "
10626  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10627  "'U' AS typcategory, false AS typispreferred, "
10628  "typdelim, typbyval, typalign, typstorage, "
10629  "false AS typcollatable, "
10630  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10631  "FROM pg_catalog.pg_type "
10632  "WHERE oid = '%u'::pg_catalog.oid",
10633  tyinfo->dobj.catId.oid);
10634  }
10635 
10636  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10637 
10638  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10639  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10640  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10641  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10642  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10643  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10644  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10645  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10646  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10647  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10648  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10649  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10650  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10651  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10652  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10653  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10654  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10655  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10656  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10657  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10658  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10659  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10660  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10661  {
10662  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10663  typdefault_is_literal = true; /* it needs quotes */
10664  }
10665  else
10666  typdefault = NULL;
10667 
10668  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10669  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10670 
10671  /*
10672  * The reason we include CASCADE is that the circular dependency between
10673  * the type and its I/O functions makes it impossible to drop the type any
10674  * other way.
10675  */
10676  appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
10677 
10678  /*
10679  * We might already have a shell type, but setting pg_type_oid is
10680  * harmless, and in any case we'd better set the array type OID.
10681  */
10682  if (dopt->binary_upgrade)
10684  tyinfo->dobj.catId.oid,
10685  false);
10686 
10688  "CREATE TYPE %s (\n"
10689  " INTERNALLENGTH = %s",
10690  qualtypname,
10691  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10692 
10693  /* regproc result is sufficiently quoted already */
10694  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
10695  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
10696  if (OidIsValid(typreceiveoid))
10697  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
10698  if (OidIsValid(typsendoid))
10699  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10700  if (OidIsValid(typmodinoid))
10701  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10702  if (OidIsValid(typmodoutoid))
10703  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10704  if (OidIsValid(typanalyzeoid))
10705  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10706 
10707  if (strcmp(typcollatable, "t") == 0)
10708  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10709 
10710  if (typdefault != NULL)
10711  {
10712  appendPQExpBufferStr(q, ",\n DEFAULT = ");
10713  if (typdefault_is_literal)
10714  appendStringLiteralAH(q, typdefault, fout);
10715  else
10716  appendPQExpBufferStr(q, typdefault);
10717  }
10718 
10719  if (OidIsValid(tyinfo->typelem))
10720  {
10721  char *elemType;
10722 
10723  elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroAsOpaque);
10724  appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
10725  free(elemType);
10726  }
10727 
10728  if (strcmp(typcategory, "U") != 0)
10729  {
10730  appendPQExpBufferStr(q, ",\n CATEGORY = ");
10731  appendStringLiteralAH(q, typcategory, fout);
10732  }
10733 
10734  if (strcmp(typispreferred, "t") == 0)
10735  appendPQExpBufferStr(q, ",\n PREFERRED = true");
10736 
10737  if (typdelim && strcmp(typdelim, ",") != 0)
10738  {
10739  appendPQExpBufferStr(q, ",\n DELIMITER = ");
10740  appendStringLiteralAH(q, typdelim, fout);
10741  }
10742 
10743  if (strcmp(typalign, "c") == 0)
10744  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
10745  else if (strcmp(typalign, "s") == 0)
10746  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
10747  else if (strcmp(typalign, "i") == 0)
10748  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
10749  else if (strcmp(typalign, "d") == 0)
10750  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
10751 
10752  if (strcmp(typstorage, "p") == 0)
10753  appendPQExpBufferStr(q, ",\n STORAGE = plain");
10754  else if (strcmp(typstorage, "e") == 0)
10755  appendPQExpBufferStr(q, ",\n STORAGE = external");
10756  else if (strcmp(typstorage, "x") == 0)
10757  appendPQExpBufferStr(q, ",\n STORAGE = extended");
10758  else if (strcmp(typstorage, "m") == 0)
10759  appendPQExpBufferStr(q, ",\n STORAGE = main");
10760 
10761  if (strcmp(typbyval, "t") == 0)
10762  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
10763 
10764  appendPQExpBufferStr(q, "\n);\n");
10765 
10766  if (dopt->binary_upgrade)
10768  "TYPE", qtypname,
10769  tyinfo->dobj.namespace->dobj.name);
10770 
10771  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10772  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10773  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
10774  .namespace = tyinfo->dobj.namespace->dobj.name,
10775  .owner = tyinfo->rolname,
10776  .description = "TYPE",
10777  .section = SECTION_PRE_DATA,
10778  .createStmt = q->data,
10779  .dropStmt = delq->data));
10780 
10781  /* Dump Type Comments and Security Labels */
10782  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10783  dumpComment(fout, "TYPE", qtypname,
10784  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10785  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10786 
10787  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10788  dumpSecLabel(fout, "TYPE", qtypname,
10789  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10790  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10791 
10792  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10793  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10794  qtypname, NULL,
10795  tyinfo->dobj.namespace->dobj.name,
10796  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10797  tyinfo->inittypacl, tyinfo->initrtypacl);
10798 
10799  PQclear(res);
10800  destroyPQExpBuffer(q);
10801  destroyPQExpBuffer(delq);
10802  destroyPQExpBuffer(query);
10803  free(qtypname);
10804  free(qualtypname);
10805 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18342
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
char * inittypacl
Definition: pg_dump.h:171
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * initrtypacl
Definition: pg_dump.h: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:14994
#define OidIsValid(objectId)
Definition: c.h:645
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:14907
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
char typalign
Definition: pg_type.h:169
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:4279
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
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:4470
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
static void dumpComment(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9503
int remoteVersion
Definition: pg_backup.h:187

◆ dumpBlob()

static void dumpBlob ( Archive fout,
BlobInfo binfo 
)
static

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

3380 {
3381  PQExpBuffer cquery = createPQExpBuffer();
3382  PQExpBuffer dquery = createPQExpBuffer();
3383 
3384  appendPQExpBuffer(cquery,
3385  "SELECT pg_catalog.lo_create('%s');\n",
3386  binfo->dobj.name);
3387 
3388  appendPQExpBuffer(dquery,
3389  "SELECT pg_catalog.lo_unlink('%s');\n",
3390  binfo->dobj.name);
3391 
3392  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3393  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3394  ARCHIVE_OPTS(.tag = binfo->dobj.name,
3395  .owner = binfo->rolname,
3396  .description = "BLOB",
3397  .section = SECTION_PRE_DATA,
3398  .createStmt = cquery->data,
3399  .dropStmt = dquery->data));
3400 
3401  /* Dump comment if any */
3402  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3403  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3404  NULL, binfo->rolname,
3405  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3406 
3407  /* Dump security label if any */
3408  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3409  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3410  NULL, binfo->rolname,
3411  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3412 
3413  /* Dump ACL if any */
3414  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3415  dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
3416  binfo->dobj.name, NULL,
3417  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3418  binfo->initblobacl, binfo->initrblobacl);
3419 
3420  destroyPQExpBuffer(cquery);
3421  destroyPQExpBuffer(dquery);
3422 }
#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:14994
char * initblobacl
Definition: pg_dump.h:569
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:14907
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
DumpableObject dobj
Definition: pg_dump.h:565
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:566
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:567
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:93
CatalogId catId
Definition: pg_dump.h:128
char * rblobacl
Definition: pg_dump.h:568
char * initrblobacl
Definition: pg_dump.h:570
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:9503

◆ dumpBlobs()

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

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

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

◆ dumpCast()

static void dumpCast ( Archive fout,
CastInfo cast 
)
static

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

12207 {
12208  DumpOptions *dopt = fout->dopt;
12209  PQExpBuffer defqry;
12210  PQExpBuffer delqry;
12211  PQExpBuffer labelq;
12212  PQExpBuffer castargs;
12213  FuncInfo *funcInfo = NULL;
12214  char *sourceType;
12215  char *targetType;
12216 
12217  /* Skip if not to be dumped */
12218  if (!cast->dobj.dump || dopt->dataOnly)
12219  return;
12220 
12221  /* Cannot dump if we don't have the cast function's info */
12222  if (OidIsValid(cast->castfunc))
12223  {
12224  funcInfo = findFuncByOid(cast->castfunc);
12225  if (funcInfo == NULL)
12226  fatal("could not find function definition for function with OID %u",
12227  cast->castfunc);
12228  }
12229 
12230  defqry = createPQExpBuffer();
12231  delqry = createPQExpBuffer();
12232  labelq = createPQExpBuffer();
12233  castargs = createPQExpBuffer();
12234 
12235  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12236  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12237  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12238  sourceType, targetType);
12239 
12240  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12241  sourceType, targetType);
12242 
12243  switch (cast->castmethod)
12244  {
12245  case COERCION_METHOD_BINARY:
12246  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12247  break;
12248  case COERCION_METHOD_INOUT:
12249  appendPQExpBufferStr(defqry, "WITH INOUT");
12250  break;
12251  case COERCION_METHOD_FUNCTION:
12252  if (funcInfo)
12253  {
12254  char *fsig = format_function_signature(fout, funcInfo, true);
12255 
12256  /*
12257  * Always qualify the function name (format_function_signature
12258  * won't qualify it).
12259  */
12260  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12261  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12262  free(fsig);
12263  }
12264  else
12265  pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field");
12266  break;
12267  default:
12268  pg_log_warning("bogus value in pg_cast.castmethod field");
12269  }
12270 
12271  if (cast->castcontext == 'a')
12272  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12273  else if (cast->castcontext == 'i')
12274  appendPQExpBufferStr(defqry, " AS IMPLICIT");
12275  appendPQExpBufferStr(defqry, ";\n");
12276 
12277  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12278  sourceType, targetType);
12279 
12280  appendPQExpBuffer(castargs, "(%s AS %s)",
12281  sourceType, targetType);
12282 
12283  if (dopt->binary_upgrade)
12284  binary_upgrade_extension_member(defqry, &cast->dobj,
12285  "CAST", castargs->data, NULL);
12286 
12287  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12288  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12289  ARCHIVE_OPTS(.tag = labelq->data,
12290  .description = "CAST",
12291  .section = SECTION_PRE_DATA,
12292  .createStmt = defqry->data,
12293  .dropStmt = delqry->data));
12294 
12295  /* Dump Cast Comments */
12296  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12297  dumpComment(fout, "CAST", castargs->data,
12298  NULL, "",
12299  cast->dobj.catId, 0, cast->dobj.dumpId);
12300 
12301  free(sourceType);
12302  free(targetType);
12303 
12304  destroyPQExpBuffer(defqry);
12305  destroyPQExpBuffer(delqry);
12306  destroyPQExpBuffer(labelq);
12307  destroyPQExpBuffer(castargs);
12308 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18342
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
Oid castsource
Definition: pg_dump.h:470
DumpComponents dump
Definition: pg_dump.h:131
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpableObject dobj
Definition: pg_dump.h:195
DumpId dumpId
Definition: pg_dump.h:129
Oid castfunc
Definition: pg_dump.h:472
#define OidIsValid(objectId)
Definition: c.h:645
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpableObject dobj
Definition: pg_dump.h:469
Oid casttarget
Definition: pg_dump.h:471
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:850
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:11585
#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:4470
CatalogId catId
Definition: pg_dump.h:128
char castmethod
Definition: pg_dump.h:474
#define pg_log_warning(...)
Definition: pgfnames.c:24
char castcontext
Definition: pg_dump.h:473
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:9503

◆ dumpCollation()

static void dumpCollation ( Archive fout,
CollInfo collinfo 
)
static

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

13460 {
13461  DumpOptions *dopt = fout->dopt;
13462  PQExpBuffer query;
13463  PQExpBuffer q;
13464  PQExpBuffer delq;
13465  char *qcollname;
13466  PGresult *res;
13467  int i_collprovider;
13468  int i_collisdeterministic;
13469  int i_collcollate;
13470  int i_collctype;
13471  const char *collprovider;
13472  const char *collcollate;
13473  const char *collctype;
13474 
13475  /* Skip if not to be dumped */
13476  if (!collinfo->dobj.dump || dopt->dataOnly)
13477  return;
13478 
13479  query = createPQExpBuffer();
13480  q = createPQExpBuffer();
13481  delq = createPQExpBuffer();
13482 
13483  qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13484 
13485  /* Get collation-specific details */
13486  appendPQExpBufferStr(query, "SELECT ");
13487 
13488  if (fout->remoteVersion >= 100000)
13489  appendPQExpBufferStr(query,
13490  "collprovider, "
13491  "collversion, ");
13492  else
13493  appendPQExpBufferStr(query,
13494  "'c' AS collprovider, "
13495  "NULL AS collversion, ");
13496 
13497  if (fout->remoteVersion >= 120000)
13498  appendPQExpBufferStr(query,
13499  "collisdeterministic, ");
13500  else
13501  appendPQExpBufferStr(query,
13502  "true AS collisdeterministic, ");
13503 
13504  appendPQExpBuffer(query,
13505  "collcollate, "
13506  "collctype "
13507  "FROM pg_catalog.pg_collation c "
13508  "WHERE c.oid = '%u'::pg_catalog.oid",
13509  collinfo->dobj.catId.oid);
13510 
13511  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13512 
13513  i_collprovider = PQfnumber(res, "collprovider");
13514  i_collisdeterministic = PQfnumber(res, "collisdeterministic");
13515  i_collcollate = PQfnumber(res, "collcollate");
13516  i_collctype = PQfnumber(res, "collctype");
13517 
13518  collprovider = PQgetvalue(res, 0, i_collprovider);
13519  collcollate = PQgetvalue(res, 0, i_collcollate);
13520  collctype = PQgetvalue(res, 0, i_collctype);
13521 
13522  appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13523  fmtQualifiedDumpable(collinfo));
13524 
13525  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13526  fmtQualifiedDumpable(collinfo));
13527 
13528  appendPQExpBufferStr(q, "provider = ");
13529  if (collprovider[0] == 'c')
13530  appendPQExpBufferStr(q, "libc");
13531  else if (collprovider[0] == 'i')
13532  appendPQExpBufferStr(q, "icu");
13533  else if (collprovider[0] == 'd')
13534  /* to allow dumping pg_catalog; not accepted on input */
13535  appendPQExpBufferStr(q, "default");
13536  else
13537  fatal("unrecognized collation provider: %s",
13538  collprovider);
13539 
13540  if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0)
13541  appendPQExpBufferStr(q, ", deterministic = false");
13542 
13543  if (strcmp(collcollate, collctype) == 0)
13544  {
13545  appendPQExpBufferStr(q, ", locale = ");
13546  appendStringLiteralAH(q, collcollate, fout);
13547  }
13548  else
13549  {
13550  appendPQExpBufferStr(q, ", lc_collate = ");
13551  appendStringLiteralAH(q, collcollate, fout);
13552  appendPQExpBufferStr(q, ", lc_ctype = ");
13553  appendStringLiteralAH(q, collctype, fout);
13554  }
13555 
13556  /*
13557  * For binary upgrade, carry over the collation version. For normal
13558  * dump/restore, omit the version, so that it is computed upon restore.
13559  */
13560  if (dopt->binary_upgrade)
13561  {
13562  int i_collversion;
13563 
13564  i_collversion = PQfnumber(res, "collversion");
13565  if (!PQgetisnull(res, 0, i_collversion))
13566  {
13567  appendPQExpBufferStr(q, ", version = ");
13569  PQgetvalue(res, 0, i_collversion),
13570  fout);
13571  }
13572  }
13573 
13574  appendPQExpBufferStr(q, ");\n");
13575 
13576  if (dopt->binary_upgrade)
13577  binary_upgrade_extension_member(q, &collinfo->dobj,
13578  "COLLATION", qcollname,
13579  collinfo->dobj.namespace->dobj.name);
13580 
13581  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13582  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13583  ARCHIVE_OPTS(.tag = collinfo->dobj.name,
13584  .namespace = collinfo->dobj.namespace->dobj.name,
13585  .owner = collinfo->rolname,
13586  .description = "COLLATION",
13587  .section = SECTION_PRE_DATA,
13588  .createStmt = q->data,
13589  .dropStmt = delq->data));
13590 
13591  /* Dump Collation Comments */
13592  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13593  dumpComment(fout, "COLLATION", qcollname,
13594  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13595  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13596 
13597  PQclear(res);
13598 
13599  destroyPQExpBuffer(query);
13600  destroyPQExpBuffer(q);
13601  destroyPQExpBuffer(delq);
13602  free(qcollname);
13603 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
DumpId dumpId
Definition: pg_dump.h:129
DumpableObject dobj
Definition: pg_dump.h:243
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
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:2877
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
#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:4470
CatalogId catId
Definition: pg_dump.h:128
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
static void dumpComment(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9503
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 9503 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().

9506 {
9507  DumpOptions *dopt = fout->dopt;
9508  CommentItem *comments;
9509  int ncomments;
9510 
9511  /* do nothing, if --no-comments is supplied */
9512  if (dopt->no_comments)
9513  return;
9514 
9515  /* Comments are schema not data ... except blob comments are data */
9516  if (strcmp(type, "LARGE OBJECT") != 0)
9517  {
9518  if (dopt->dataOnly)
9519  return;
9520  }
9521  else
9522  {
9523  /* We do dump blob comments in binary-upgrade mode */
9524  if (dopt->schemaOnly && !dopt->binary_upgrade)
9525  return;
9526  }
9527 
9528  /* Search for comments associated with catalogId, using table */
9529  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9530  &comments);
9531 
9532  /* Is there one matching the subid? */
9533  while (ncomments > 0)
9534  {
9535  if (comments->objsubid == subid)
9536  break;
9537  comments++;
9538  ncomments--;
9539  }
9540 
9541  /* If a comment exists, build COMMENT ON statement */
9542  if (ncomments > 0)
9543  {
9544  PQExpBuffer query = createPQExpBuffer();
9546 
9547  appendPQExpBuffer(query, "COMMENT ON %s ", type);
9548  if (namespace && *namespace)
9549  appendPQExpBuffer(query, "%s.", fmtId(namespace));
9550  appendPQExpBuffer(query, "%s IS ", name);
9551  appendStringLiteralAH(query, comments->descr, fout);
9552  appendPQExpBufferStr(query, ";\n");
9553 
9554  appendPQExpBuffer(tag, "%s %s", type, name);
9555 
9556  /*
9557  * We mark comments as SECTION_NONE because they really belong in the
9558  * same section as their parent, whether that is pre-data or
9559  * post-data.
9560  */
9562  ARCHIVE_OPTS(.tag = tag->data,
9563  .namespace = namespace,
9564  .owner = owner,
9565  .description = "COMMENT",
9566  .section = SECTION_NONE,
9567  .createStmt = query->data,
9568  .deps = &dumpId,
9569  .nDeps = 1));
9570 
9571  destroyPQExpBuffer(query);
9572  destroyPQExpBuffer(tag);
9573  }
9574 }
#define ARCHIVE_OPTS(...)
Oid tableoid
Definition: pg_backup.h:230
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
bool schemaOnly
Definition: pg_backup.h:138
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:71
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9682
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
const char * descr
Definition: pg_dump.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
int no_comments
Definition: pg_backup.h:149
const char * name
Definition: encode.c:521
static const CatalogId nilCatalogId
Definition: pg_dump.c:131
DumpId createDumpId(void)
Definition: common.c:589

◆ dumpCompositeType()

static void dumpCompositeType ( Archive fout,
TypeInfo tyinfo 
)
static

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

10987 {
10988  DumpOptions *dopt = fout->dopt;
10990  PQExpBuffer dropped = createPQExpBuffer();
10991  PQExpBuffer delq = createPQExpBuffer();
10992  PQExpBuffer query = createPQExpBuffer();
10993  PGresult *res;
10994  char *qtypname;
10995  char *qualtypname;
10996  int ntups;
10997  int i_attname;
10998  int i_atttypdefn;
10999  int i_attlen;
11000  int i_attalign;
11001  int i_attisdropped;
11002  int i_attcollation;
11003  int i;
11004  int actual_atts;
11005 
11006  /* Fetch type specific details */
11007  if (fout->remoteVersion >= 90100)
11008  {
11009  /*
11010  * attcollation is new in 9.1. Since we only want to dump COLLATE
11011  * clauses for attributes whose collation is different from their
11012  * type's default, we use a CASE here to suppress uninteresting
11013  * attcollations cheaply. atttypid will be 0 for dropped columns;
11014  * collation does not matter for those.
11015  */
11016  appendPQExpBuffer(query, "SELECT a.attname, "
11017  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11018  "a.attlen, a.attalign, a.attisdropped, "
11019  "CASE WHEN a.attcollation <> at.typcollation "
11020  "THEN a.attcollation ELSE 0 END AS attcollation "
11021  "FROM pg_catalog.pg_type ct "
11022  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
11023  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
11024  "WHERE ct.oid = '%u'::pg_catalog.oid "
11025  "ORDER BY a.attnum ",
11026  tyinfo->dobj.catId.oid);
11027  }
11028  else
11029  {
11030  /*
11031  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
11032  * should always be false.
11033  */
11034  appendPQExpBuffer(query, "SELECT a.attname, "
11035  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
11036  "a.attlen, a.attalign, a.attisdropped, "
11037  "0 AS attcollation "
11038  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
11039  "WHERE ct.oid = '%u'::pg_catalog.oid "
11040  "AND a.attrelid = ct.typrelid "
11041  "ORDER BY a.attnum ",
11042  tyinfo->dobj.catId.oid);
11043  }
11044 
11045  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11046 
11047  ntups = PQntuples(res);
11048 
11049  i_attname = PQfnumber(res, "attname");
11050  i_atttypdefn = PQfnumber(res, "atttypdefn");
11051  i_attlen = PQfnumber(res, "attlen");
11052  i_attalign = PQfnumber(res, "attalign");
11053  i_attisdropped = PQfnumber(res, "attisdropped");
11054  i_attcollation = PQfnumber(res, "attcollation");
11055 
11056  if (dopt->binary_upgrade)
11057  {
11059  tyinfo->dobj.catId.oid,
11060  false);
11061  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
11062  }
11063 
11064  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
11065  qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
11066 
11067  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
11068  qualtypname);
11069 
11070  actual_atts = 0;
11071  for (i = 0; i < ntups; i++)
11072  {
11073  char *attname;
11074  char *atttypdefn;
11075  char *attlen;
11076  char *attalign;
11077  bool attisdropped;
11078  Oid attcollation;
11079 
11080  attname = PQgetvalue(res, i, i_attname);
11081  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
11082  attlen = PQgetvalue(res, i, i_attlen);
11083  attalign = PQgetvalue(res, i, i_attalign);
11084  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
11085  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
11086 
11087  if (attisdropped && !dopt->binary_upgrade)
11088  continue;
11089 
11090  /* Format properly if not first attr */
11091  if (actual_atts++ > 0)
11092  appendPQExpBufferChar(q, ',');
11093  appendPQExpBufferStr(q, "\n\t");
11094 
11095  if (!attisdropped)
11096  {
11097  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
11098 
11099  /* Add collation if not default for the column type */
11100  if (OidIsValid(attcollation))
11101  {
11102  CollInfo *coll;
11103 
11104  coll = findCollationByOid(attcollation);
11105  if (coll)
11106  appendPQExpBuffer(q, " COLLATE %s",
11107  fmtQualifiedDumpable(coll));
11108  }
11109  }
11110  else
11111  {
11112  /*
11113  * This is a dropped attribute and we're in binary_upgrade mode.
11114  * Insert a placeholder for it in the CREATE TYPE command, and set
11115  * length and alignment with direct UPDATE to the catalogs
11116  * afterwards. See similar code in dumpTableSchema().
11117  */
11118  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
11119 
11120  /* stash separately for insertion after the CREATE TYPE */
11121  appendPQExpBufferStr(dropped,
11122  "\n-- For binary upgrade, recreate dropped column.\n");
11123  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
11124  "SET attlen = %s, "
11125  "attalign = '%s', attbyval = false\n"
11126  "WHERE attname = ", attlen, attalign);
11127  appendStringLiteralAH(dropped, attname, fout);
11128  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
11129  appendStringLiteralAH(dropped, qualtypname, fout);
11130  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
11131 
11132  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
11133  qualtypname);
11134  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
11135  fmtId(attname));
11136  }
11137  }
11138  appendPQExpBufferStr(q, "\n);\n");
11139  appendPQExpBufferStr(q, dropped->data);
11140 
11141  appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
11142 
11143  if (dopt->binary_upgrade)
11145  "TYPE", qtypname,
11146  tyinfo->dobj.namespace->dobj.name);
11147 
11148  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11149  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
11150  ARCHIVE_OPTS(.tag = tyinfo->dobj.name,
11151  .namespace = tyinfo->dobj.namespace->dobj.name,
11152  .owner = tyinfo->rolname,
11153  .description = "TYPE",
11154  .section = SECTION_PRE_DATA,
11155  .createStmt = q->data,
11156  .dropStmt = delq->data));
11157 
11158 
11159  /* Dump Type Comments and Security Labels */
11160  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11161  dumpComment(fout, "TYPE", qtypname,
11162  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11163  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11164 
11165  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11166  dumpSecLabel(fout, "TYPE", qtypname,
11167  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11168  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11169 
11170  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11171  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
11172  qtypname, NULL,
11173  tyinfo->dobj.namespace->dobj.name,
11174  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11175  tyinfo->inittypacl, tyinfo->initrtypacl);
11176 
11177  PQclear(res);
11178  destroyPQExpBuffer(q);
11179  destroyPQExpBuffer(dropped);
11180  destroyPQExpBuffer(delq);
11181  destroyPQExpBuffer(query);
11182  free(qtypname);
11183  free(qualtypname);
11184 
11185  /* Dump any per-column comments */
11186  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11187  dumpCompositeTypeColComments(fout, tyinfo);
11188 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
char * inittypacl
Definition: pg_dump.h:171
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
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:11196
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:2769
static void dumpSecLabel(Archive *fout, const char *type, const char *name, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14994
#define OidIsValid(objectId)
Definition: c.h:645
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:14907
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:4400
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:4279
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
char * rtypacl
Definition: pg_dump.h:170
char * rolname
Definition: pg_dump.h:168
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:872
#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:4470
#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:9503
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401
int remoteVersion
Definition: pg_backup.h:187

◆ dumpCompositeTypeColComments()

static void dumpCompositeTypeColComments ( Archive fout,
TypeInfo tyinfo 
)
static

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

11197 {
11198  CommentItem *comments;
11199  int ncomments;
11200  PGresult *res;
11201  PQExpBuffer query;
11202  PQExpBuffer target;
11203  Oid pgClassOid;
11204  int i;
11205  int ntups;
11206  int i_attname;
11207  int i_attnum;
11208 
11209  /* do nothing, if --no-comments is supplied */
11210  if (fout->dopt->no_comments)
11211  return;
11212 
11213  query = createPQExpBuffer();
11214 
11215  appendPQExpBuffer(query,
11216  "SELECT c.tableoid, a.attname, a.attnum "
11217  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
11218  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
11219  " AND NOT a.attisdropped "
11220  "ORDER BY a.attnum ",
11221  tyinfo->typrelid);
11222 
11223  /* Fetch column attnames */
11224  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11225 
11226  ntups = PQntuples(res);
11227  if (ntups < 1)
11228  {
11229  PQclear(res);
11230  destroyPQExpBuffer(query);
11231  return;
11232  }
11233 
11234  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
11235 
11236  /* Search for comments associated with type's pg_class OID */
11237  ncomments = findComments(fout,
11238  pgClassOid,
11239  tyinfo->typrelid,
11240  &comments);
11241 
11242  /* If no comments exist, we're done */
11243  if (ncomments <= 0)
11244  {
11245  PQclear(res);
11246  destroyPQExpBuffer(query);
11247  return;
11248  }
11249 
11250  /* Build COMMENT ON statements */
11251  target = createPQExpBuffer();
11252 
11253  i_attnum = PQfnumber(res, "attnum");
11254  i_attname = PQfnumber(res, "attname");
11255  while (ncomments > 0)
11256  {
11257  const char *attname;
11258 
11259  attname = NULL;
11260  for (i = 0; i < ntups; i++)
11261  {
11262  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
11263  {
11264  attname = PQgetvalue(res, i, i_attname);
11265  break;
11266  }
11267  }
11268  if (attname) /* just in case we don't find it */
11269  {
11270  const char *descr = comments->descr;
11271 
11272  resetPQExpBuffer(target);
11273  appendPQExpBuffer(target, "COLUMN %s.",
11274  fmtId(tyinfo->dobj.name));
11275  appendPQExpBufferStr(target, fmtId(attname));
11276 
11277  resetPQExpBuffer(query);
11278  appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11279  fmtQualifiedDumpable(tyinfo));
11280  appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11281  appendStringLiteralAH(query, descr, fout);
11282  appendPQExpBufferStr(query, ";\n");
11283 
11285  ARCHIVE_OPTS(.tag = target->data,
11286  .namespace = tyinfo->dobj.namespace->dobj.name,
11287  .owner = tyinfo->rolname,
11288  .description = "COMMENT",
11289  .section = SECTION_NONE,
11290  .createStmt = query->data,
11291  .deps = &(tyinfo->dobj.dumpId),
11292  .nDeps = 1));
11293  }
11294 
11295  comments++;
11296  ncomments--;
11297  }
11298 
11299  PQclear(res);
11300  destroyPQExpBuffer(query);
11301  destroyPQExpBuffer(target);
11302 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
Oid typrelid
Definition: pg_dump.h:174
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int objsubid
Definition: pg_dump.c:71
DumpId dumpId
Definition: pg_dump.h:129
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9682
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
NameData attname
Definition: pg_attribute.h:40
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:68
DumpableObject dobj
Definition: pg_dump.h:162
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
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:131
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401
DumpId createDumpId(void)
Definition: common.c:589

◆ dumpConstraint()

static void dumpConstraint ( Archive fout,
ConstraintInfo coninfo 
)
static

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

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

13611 {
13612  DumpOptions *dopt = fout->dopt;
13613  PQExpBuffer query;
13614  PQExpBuffer q;
13615  PQExpBuffer delq;
13616  char *qconvname;
13617  PGresult *res;
13618  int i_conforencoding;
13619  int i_contoencoding;
13620  int i_conproc;
13621  int i_condefault;
13622  const char *conforencoding;
13623  const char *contoencoding;
13624  const char *conproc;
13625  bool condefault;
13626 
13627  /* Skip if not to be dumped */
13628  if (!convinfo->dobj.dump || dopt->dataOnly)
13629  return;
13630 
13631  query = createPQExpBuffer();
13632  q = createPQExpBuffer();
13633  delq = createPQExpBuffer();
13634 
13635  qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13636 
13637  /* Get conversion-specific details */
13638  appendPQExpBuffer(query, "SELECT "
13639  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13640  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13641  "conproc, condefault "
13642  "FROM pg_catalog.pg_conversion c "
13643  "WHERE c.oid = '%u'::pg_catalog.oid",
13644  convinfo->dobj.catId.oid);
13645 
13646  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13647 
13648  i_conforencoding = PQfnumber(res, "conforencoding");
13649  i_contoencoding = PQfnumber(res, "contoencoding");
13650  i_conproc = PQfnumber(res, "conproc");
13651  i_condefault = PQfnumber(res, "condefault");
13652 
13653  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13654  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13655  conproc = PQgetvalue(res, 0, i_conproc);
13656  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13657 
13658  appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13659  fmtQualifiedDumpable(convinfo));
13660 
13661  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13662  (condefault) ? "DEFAULT " : "",
13663  fmtQualifiedDumpable(convinfo));
13664  appendStringLiteralAH(q, conforencoding, fout);
13665  appendPQExpBufferStr(q, " TO ");
13666  appendStringLiteralAH(q, contoencoding, fout);
13667  /* regproc output is already sufficiently quoted */
13668  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13669 
13670  if (dopt->binary_upgrade)
13671  binary_upgrade_extension_member(q, &convinfo->dobj,
13672  "CONVERSION", qconvname,
13673  convinfo->dobj.namespace->dobj.name);
13674 
13675  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13676  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13677  ARCHIVE_OPTS(.tag = convinfo->dobj.name,
13678  .namespace = convinfo->dobj.namespace->dobj.name,
13679  .owner = convinfo->rolname,
13680  .description = "CONVERSION",
13681  .section = SECTION_PRE_DATA,
13682  .createStmt = q->data,
13683  .dropStmt = delq->data));
13684 
13685  /* Dump Conversion Comments */
13686  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13687  dumpComment(fout, "CONVERSION", qconvname,
13688  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13689  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13690 
13691  PQclear(res);
13692 
13693  destroyPQExpBuffer(query);
13694  destroyPQExpBuffer(q);
13695  destroyPQExpBuffer(delq);
13696  free(qconvname);
13697 }
#define ARCHIVE_OPTS(...)
char * name
Definition: pg_dump.h:130
DumpComponents dump
Definition: pg_dump.h:131
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * rolname
Definition: pg_dump.h:250
DumpId dumpId
Definition: pg_dump.h:129
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:182
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
bool dataOnly
Definition: pg_backup.h:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:91
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:291
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:89
#define free(a)
Definition: header.h:65
#define fmtQualifiedDumpable(obj)
Definition: pg_dump.c:146
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:4470
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:9503

◆ dumpDatabase()