PostgreSQL Source Code  git master
pg_dump.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <ctype.h>
#include "getopt_long.h"
#include "access/attnum.h"
#include "access/sysattr.h"
#include "access/transam.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_am.h"
#include "catalog/pg_attribute.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_class.h"
#include "catalog/pg_default_acl.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_largeobject_metadata.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "libpq/libpq-fs.h"
#include "dumputils.h"
#include "parallel.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
#include "pg_dump.h"
#include "fe_utils/string_utils.h"
Include dependency graph for pg_dump.c:

Go to the source code of this file.

Data Structures

struct  CommentItem
 
struct  SecLabelItem
 

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 *target, 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 *target, 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 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 *tag, 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, bool oids, char relkind)
 
static void makeTableDataInfo (DumpOptions *dopt, TableInfo *tbinfo, bool oids)
 
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 * convertOperatorReference (Archive *fout, const char *opr)
 
static char * convertTSFunction (Archive *fout, Oid funcOid)
 
static Oid findLastBuiltinOid_V71 (Archive *fout, const char *)
 
static void selectSourceSchema (Archive *fout, const char *schemaName)
 
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 dumpEncoding (Archive *AH)
 
static void dumpStdStrings (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 *objlabel)
 
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, TableInfo tblinfo[], int numTables)
 
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

bool g_verbose
 
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}
 

Typedef Documentation

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

17755 {
17756  DumpableObject *preDataBound = boundaryObjs + 0;
17757  DumpableObject *postDataBound = boundaryObjs + 1;
17758  int i;
17759 
17760  for (i = 0; i < numObjs; i++)
17761  {
17762  DumpableObject *dobj = dobjs[i];
17763 
17764  /*
17765  * The classification of object types here must match the SECTION_xxx
17766  * values assigned during subsequent ArchiveEntry calls!
17767  */
17768  switch (dobj->objType)
17769  {
17770  case DO_NAMESPACE:
17771  case DO_EXTENSION:
17772  case DO_TYPE:
17773  case DO_SHELL_TYPE:
17774  case DO_FUNC:
17775  case DO_AGG:
17776  case DO_OPERATOR:
17777  case DO_ACCESS_METHOD:
17778  case DO_OPCLASS:
17779  case DO_OPFAMILY:
17780  case DO_COLLATION:
17781  case DO_CONVERSION:
17782  case DO_TABLE:
17783  case DO_ATTRDEF:
17784  case DO_PROCLANG:
17785  case DO_CAST:
17786  case DO_DUMMY_TYPE:
17787  case DO_TSPARSER:
17788  case DO_TSDICT:
17789  case DO_TSTEMPLATE:
17790  case DO_TSCONFIG:
17791  case DO_FDW:
17792  case DO_FOREIGN_SERVER:
17793  case DO_TRANSFORM:
17794  case DO_BLOB:
17795  /* Pre-data objects: must come before the pre-data boundary */
17796  addObjectDependency(preDataBound, dobj->dumpId);
17797  break;
17798  case DO_TABLE_DATA:
17799  case DO_SEQUENCE_SET:
17800  case DO_BLOB_DATA:
17801  /* Data objects: must come between the boundaries */
17802  addObjectDependency(dobj, preDataBound->dumpId);
17803  addObjectDependency(postDataBound, dobj->dumpId);
17804  break;
17805  case DO_INDEX:
17806  case DO_STATSEXT:
17807  case DO_REFRESH_MATVIEW:
17808  case DO_TRIGGER:
17809  case DO_EVENT_TRIGGER:
17810  case DO_DEFAULT_ACL:
17811  case DO_POLICY:
17812  case DO_PUBLICATION:
17813  case DO_PUBLICATION_REL:
17814  case DO_SUBSCRIPTION:
17815  /* Post-data objects: must come after the post-data boundary */
17816  addObjectDependency(dobj, postDataBound->dumpId);
17817  break;
17818  case DO_RULE:
17819  /* Rules are post-data, but only if dumped separately */
17820  if (((RuleInfo *) dobj)->separate)
17821  addObjectDependency(dobj, postDataBound->dumpId);
17822  break;
17823  case DO_CONSTRAINT:
17824  case DO_FK_CONSTRAINT:
17825  /* Constraints are post-data, but only if dumped separately */
17826  if (((ConstraintInfo *) dobj)->separate)
17827  addObjectDependency(dobj, postDataBound->dumpId);
17828  break;
17829  case DO_PRE_DATA_BOUNDARY:
17830  /* nothing to do */
17831  break;
17832  case DO_POST_DATA_BOUNDARY:
17833  /* must come after the pre-data boundary */
17834  addObjectDependency(dobj, preDataBound->dumpId);
17835  break;
17836  }
17837  }
17838 }
DumpId dumpId
Definition: pg_dump.h:132
Definition: pg_dump.h:49
Definition: pg_dump.h:73
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:712
int i
DumpableObjectType objType
Definition: pg_dump.h:130

◆ appendReloptionsArrayAH()

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

Definition at line 18084 of file pg_dump.c.

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

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

18086 {
18087  bool res;
18088 
18089  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18090  fout->std_strings);
18091  if (!res)
18092  write_msg(NULL, "WARNING: could not parse reloptions array\n");
18093 }
int encoding
Definition: pg_backup.h:196
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:747
void write_msg(const char *modulename, const char *fmt,...)
bool std_strings
Definition: pg_backup.h:197

◆ binary_upgrade_extension_member()

static void binary_upgrade_extension_member ( PQExpBuffer  upgrade_buffer,
DumpableObject dobj,
const char *  objlabel 
)
static

Definition at line 4143 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), _dumpableObject::dependencies, DO_EXTENSION, exit_horribly(), _dumpableObject::ext_member, findObjectByDumpId(), fmtId(), i, _dumpableObject::name, _dumpableObject::nDeps, and _dumpableObject::objType.

Referenced by dumpAccessMethod(), dumpAgg(), dumpBaseType(), dumpCast(), dumpCollation(), dumpCompositeType(), dumpConversion(), dumpDomain(), dumpEnumType(), dumpForeignDataWrapper(), dumpForeignServer(), dumpFunc(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpProcLang(), dumpRangeType(), dumpSequence(), dumpTableSchema(), dumpTransform(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), and dumpUndefinedType().

4146 {
4147  DumpableObject *extobj = NULL;
4148  int i;
4149 
4150  if (!dobj->ext_member)
4151  return;
4152 
4153  /*
4154  * Find the parent extension. We could avoid this search if we wanted to
4155  * add a link field to DumpableObject, but the space costs of that would
4156  * be considerable. We assume that member objects could only have a
4157  * direct dependency on their own extension, not any others.
4158  */
4159  for (i = 0; i < dobj->nDeps; i++)
4160  {
4161  extobj = findObjectByDumpId(dobj->dependencies[i]);
4162  if (extobj && extobj->objType == DO_EXTENSION)
4163  break;
4164  extobj = NULL;
4165  }
4166  if (extobj == NULL)
4167  exit_horribly(NULL, "could not find parent extension for %s\n", objlabel);
4168 
4169  appendPQExpBufferStr(upgrade_buffer,
4170  "\n-- For binary upgrade, handle extension membership the hard way\n");
4171  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s;\n",
4172  fmtId(extobj->name),
4173  objlabel);
4174 }
char * name
Definition: pg_dump.h:133
DumpId * dependencies
Definition: pg_dump.h:138
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
bool ext_member
Definition: pg_dump.h:137
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:540
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
void exit_horribly(const char *modulename, const char *fmt,...)
int i
DumpableObjectType objType
Definition: pg_dump.h:130

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

4079 {
4080  PQExpBuffer upgrade_query = createPQExpBuffer();
4081  PGresult *upgrade_res;
4082  Oid pg_class_reltoastrelid;
4083  Oid pg_index_indexrelid;
4084 
4085  appendPQExpBuffer(upgrade_query,
4086  "SELECT c.reltoastrelid, i.indexrelid "
4087  "FROM pg_catalog.pg_class c LEFT JOIN "
4088  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4089  "WHERE c.oid = '%u'::pg_catalog.oid;",
4090  pg_class_oid);
4091 
4092  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4093 
4094  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
4095  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
4096 
4097  appendPQExpBufferStr(upgrade_buffer,
4098  "\n-- For binary upgrade, must preserve pg_class oids\n");
4099 
4100  if (!is_index)
4101  {
4102  appendPQExpBuffer(upgrade_buffer,
4103  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4104  pg_class_oid);
4105  /* only tables have toast tables, not indexes */
4106  if (OidIsValid(pg_class_reltoastrelid))
4107  {
4108  /*
4109  * One complexity is that the table definition might not require
4110  * the creation of a TOAST table, and the TOAST table might have
4111  * been created long after table creation, when the table was
4112  * loaded with wide data. By setting the TOAST oid we force
4113  * creation of the TOAST heap and TOAST index by the backend so we
4114  * can cleanly copy the files during binary upgrade.
4115  */
4116 
4117  appendPQExpBuffer(upgrade_buffer,
4118  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4119  pg_class_reltoastrelid);
4120 
4121  /* every toast table has an index */
4122  appendPQExpBuffer(upgrade_buffer,
4123  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4124  pg_index_indexrelid);
4125  }
4126  }
4127  else
4128  appendPQExpBuffer(upgrade_buffer,
4129  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4130  pg_class_oid);
4131 
4132  appendPQExpBufferChar(upgrade_buffer, '\n');
4133 
4134  PQclear(upgrade_res);
4135  destroyPQExpBuffer(upgrade_query);
4136 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
void PQclear(PGresult *res)
Definition: fe-exec.c:671

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

4033 {
4034  PQExpBuffer upgrade_query = createPQExpBuffer();
4035  PGresult *upgrade_res;
4036  Oid pg_type_oid;
4037  bool toast_set = false;
4038 
4039  /* we only support old >= 8.3 for binary upgrades */
4040  appendPQExpBuffer(upgrade_query,
4041  "SELECT c.reltype AS crel, t.reltype AS trel "
4042  "FROM pg_catalog.pg_class c "
4043  "LEFT JOIN pg_catalog.pg_class t ON "
4044  " (c.reltoastrelid = t.oid) "
4045  "WHERE c.oid = '%u'::pg_catalog.oid;",
4046  pg_rel_oid);
4047 
4048  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4049 
4050  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4051 
4052  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4053  pg_type_oid, false);
4054 
4055  if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
4056  {
4057  /* Toast tables do not have pg_type array rows */
4058  Oid pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
4059  PQfnumber(upgrade_res, "trel")));
4060 
4061  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
4062  appendPQExpBuffer(upgrade_buffer,
4063  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4064  pg_type_toast_oid);
4065 
4066  toast_set = true;
4067  }
4068 
4069  PQclear(upgrade_res);
4070  destroyPQExpBuffer(upgrade_query);
4071 
4072  return toast_set;
4073 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
unsigned int Oid
Definition: postgres_ext.h:31
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
#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:3961
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143

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

3965 {
3966  PQExpBuffer upgrade_query = createPQExpBuffer();
3967  PGresult *res;
3968  Oid pg_type_array_oid;
3969 
3970  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
3971  appendPQExpBuffer(upgrade_buffer,
3972  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
3973  pg_type_oid);
3974 
3975  /* we only support old >= 8.3 for binary upgrades */
3976  appendPQExpBuffer(upgrade_query,
3977  "SELECT typarray "
3978  "FROM pg_catalog.pg_type "
3979  "WHERE oid = '%u'::pg_catalog.oid;",
3980  pg_type_oid);
3981 
3982  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
3983 
3984  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
3985 
3986  PQclear(res);
3987 
3988  if (!OidIsValid(pg_type_array_oid) && force_array_type)
3989  {
3990  /*
3991  * If the old version didn't assign an array type, but the new version
3992  * does, we must select an unused type OID to assign. This currently
3993  * only happens for domains, when upgrading pre-v11 to v11 and up.
3994  *
3995  * Note: local state here is kind of ugly, but we must have some,
3996  * since we mustn't choose the same unused OID more than once.
3997  */
3998  static Oid next_possible_free_oid = FirstNormalObjectId;
3999  bool is_dup;
4000 
4001  do
4002  {
4003  ++next_possible_free_oid;
4004  printfPQExpBuffer(upgrade_query,
4005  "SELECT EXISTS(SELECT 1 "
4006  "FROM pg_catalog.pg_type "
4007  "WHERE oid = '%u'::pg_catalog.oid);",
4008  next_possible_free_oid);
4009  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4010  is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
4011  PQclear(res);
4012  } while (is_dup);
4013 
4014  pg_type_array_oid = next_possible_free_oid;
4015  }
4016 
4017  if (OidIsValid(pg_type_array_oid))
4018  {
4019  appendPQExpBufferStr(upgrade_buffer,
4020  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4021  appendPQExpBuffer(upgrade_buffer,
4022  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4023  pg_type_array_oid);
4024  }
4025 
4026  destroyPQExpBuffer(upgrade_query);
4027 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
#define FirstNormalObjectId
Definition: transam.h:94
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
void PQclear(PGresult *res)
Definition: fe-exec.c:671

◆ BuildArchiveDependencies()

static void BuildArchiveDependencies ( Archive fout)
static

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

17866 {
17867  ArchiveHandle *AH = (ArchiveHandle *) fout;
17868  TocEntry *te;
17869 
17870  /* Scan all TOC entries in the archive */
17871  for (te = AH->toc->next; te != AH->toc; te = te->next)
17872  {
17873  DumpableObject *dobj;
17874  DumpId *dependencies;
17875  int nDeps;
17876  int allocDeps;
17877 
17878  /* No need to process entries that will not be dumped */
17879  if (te->reqs == 0)
17880  continue;
17881  /* Ignore entries that already have "special" dependencies */
17882  if (te->nDeps > 0)
17883  continue;
17884  /* Otherwise, look up the item's original DumpableObject, if any */
17885  dobj = findObjectByDumpId(te->dumpId);
17886  if (dobj == NULL)
17887  continue;
17888  /* No work if it has no dependencies */
17889  if (dobj->nDeps <= 0)
17890  continue;
17891  /* Set up work array */
17892  allocDeps = 64;
17893  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
17894  nDeps = 0;
17895  /* Recursively find all dumpable dependencies */
17896  findDumpableDependencies(AH, dobj,
17897  &dependencies, &nDeps, &allocDeps);
17898  /* And save 'em ... */
17899  if (nDeps > 0)
17900  {
17901  dependencies = (DumpId *) pg_realloc(dependencies,
17902  nDeps * sizeof(DumpId));
17903  te->dependencies = dependencies;
17904  te->nDeps = nDeps;
17905  }
17906  else
17907  free(dependencies);
17908  }
17909 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:230
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:540
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:17913
#define free(a)
Definition: header.h:65

◆ buildMatViewRefreshDependencies()

static void buildMatViewRefreshDependencies ( Archive fout)
static

Definition at line 2291 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, RELKIND_MATVIEW, RELKIND_VIEW, Archive::remoteVersion, selectSourceSchema(), and CatalogId::tableoid.

Referenced by main().

2292 {
2293  PQExpBuffer query;
2294  PGresult *res;
2295  int ntups,
2296  i;
2297  int i_classid,
2298  i_objid,
2299  i_refobjid;
2300 
2301  /* No Mat Views before 9.3. */
2302  if (fout->remoteVersion < 90300)
2303  return;
2304 
2305  /* Make sure we are in proper schema */
2306  selectSourceSchema(fout, "pg_catalog");
2307 
2308  query = createPQExpBuffer();
2309 
2310  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2311  "( "
2312  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2313  "FROM pg_depend d1 "
2314  "JOIN pg_class c1 ON c1.oid = d1.objid "
2315  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2316  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2317  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2318  "AND d2.objid = r1.oid "
2319  "AND d2.refobjid <> d1.objid "
2320  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2321  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2323  "WHERE d1.classid = 'pg_class'::regclass "
2324  "UNION "
2325  "SELECT w.objid, d3.refobjid, c3.relkind "
2326  "FROM w "
2327  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2328  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2329  "AND d3.objid = r3.oid "
2330  "AND d3.refobjid <> w.refobjid "
2331  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2332  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2334  ") "
2335  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2336  "FROM w "
2337  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2338 
2339  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2340 
2341  ntups = PQntuples(res);
2342 
2343  i_classid = PQfnumber(res, "classid");
2344  i_objid = PQfnumber(res, "objid");
2345  i_refobjid = PQfnumber(res, "refobjid");
2346 
2347  for (i = 0; i < ntups; i++)
2348  {
2349  CatalogId objId;
2350  CatalogId refobjId;
2351  DumpableObject *dobj;
2352  DumpableObject *refdobj;
2353  TableInfo *tbinfo;
2354  TableInfo *reftbinfo;
2355 
2356  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2357  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2358  refobjId.tableoid = objId.tableoid;
2359  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2360 
2361  dobj = findObjectByCatalogId(objId);
2362  if (dobj == NULL)
2363  continue;
2364 
2365  Assert(dobj->objType == DO_TABLE);
2366  tbinfo = (TableInfo *) dobj;
2367  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2368  dobj = (DumpableObject *) tbinfo->dataObj;
2369  if (dobj == NULL)
2370  continue;
2371  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2372 
2373  refdobj = findObjectByCatalogId(refobjId);
2374  if (refdobj == NULL)
2375  continue;
2376 
2377  Assert(refdobj->objType == DO_TABLE);
2378  reftbinfo = (TableInfo *) refdobj;
2379  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2380  refdobj = (DumpableObject *) reftbinfo->dataObj;
2381  if (refdobj == NULL)
2382  continue;
2383  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2384 
2385  addObjectDependency(dobj, refdobj->dumpId);
2386 
2387  if (!reftbinfo->relispopulated)
2388  tbinfo->relispopulated = false;
2389  }
2390 
2391  PQclear(res);
2392 
2393  destroyPQExpBuffer(query);
2394 }
char relkind
Definition: pg_dump.h:267
Oid tableoid
Definition: pg_backup.h:226
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define RELKIND_MATVIEW
Definition: pg_class.h:165
DumpId dumpId
Definition: pg_dump.h:132
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:558
#define atooid(x)
Definition: postgres_ext.h:42
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
#define CppAsString2(x)
Definition: c.h:216
struct _tableDataInfo * dataObj
Definition: pg_dump.h:331
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define Assert(condition)
Definition: c.h:680
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:712
#define RELKIND_VIEW
Definition: pg_class.h:164
int i
bool relispopulated
Definition: pg_dump.h:269
DumpableObjectType objType
Definition: pg_dump.h:130
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:407
int remoteVersion
Definition: pg_backup.h:186

◆ checkExtensionMembership()

static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

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

1330 {
1331  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1332 
1333  if (ext == NULL)
1334  return false;
1335 
1336  dobj->ext_member = true;
1337 
1338  /* Record dependency so that getDependencies needn't deal with that */
1339  addObjectDependency(dobj, ext->dobj.dumpId);
1340 
1341  /*
1342  * In 9.6 and above, mark the member object to have any non-initial ACL,
1343  * policies, and security labels dumped.
1344  *
1345  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1346  * extract the information about the object. We don't provide support for
1347  * initial policies and security labels and it seems unlikely for those to
1348  * ever exist, but we may have to revisit this later.
1349  *
1350  * Prior to 9.6, we do not include any extension member components.
1351  *
1352  * In binary upgrades, we still dump all components of the members
1353  * individually, since the idea is to exactly reproduce the database
1354  * contents rather than replace the extension contents with something
1355  * different.
1356  */
1357  if (fout->dopt->binary_upgrade)
1358  dobj->dump = ext->dobj.dump;
1359  else
1360  {
1361  if (fout->remoteVersion < 90600)
1362  dobj->dump = DUMP_COMPONENT_NONE;
1363  else
1364  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1367  }
1368 
1369  return true;
1370 }
DumpableObject dobj
Definition: pg_dump.h:155
DumpComponents dump
Definition: pg_dump.h:134
DumpId dumpId
Definition: pg_dump.h:132
bool ext_member
Definition: pg_dump.h:137
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:91
int binary_upgrade
Definition: pg_backup.h:135
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:852
DumpOptions * dopt
Definition: pg_backup.h:181
DumpComponents dump_contains
Definition: pg_dump.h:136
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:95
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:96
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:712
CatalogId catId
Definition: pg_dump.h:131
#define DUMP_COMPONENT_POLICY
Definition: pg_dump.h:97
int remoteVersion
Definition: pg_backup.h:186

◆ collectComments()

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

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

9409 {
9410  PGresult *res;
9411  PQExpBuffer query;
9412  int i_description;
9413  int i_classoid;
9414  int i_objoid;
9415  int i_objsubid;
9416  int ntups;
9417  int i;
9418  CommentItem *comments;
9419 
9420  /*
9421  * Note we do NOT change source schema here; preserve the caller's
9422  * setting, instead.
9423  */
9424 
9425  query = createPQExpBuffer();
9426 
9427  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9428  "FROM pg_catalog.pg_description "
9429  "ORDER BY classoid, objoid, objsubid");
9430 
9431  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9432 
9433  /* Construct lookup table containing OIDs in numeric form */
9434 
9435  i_description = PQfnumber(res, "description");
9436  i_classoid = PQfnumber(res, "classoid");
9437  i_objoid = PQfnumber(res, "objoid");
9438  i_objsubid = PQfnumber(res, "objsubid");
9439 
9440  ntups = PQntuples(res);
9441 
9442  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9443 
9444  for (i = 0; i < ntups; i++)
9445  {
9446  comments[i].descr = PQgetvalue(res, i, i_description);
9447  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9448  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9449  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9450  }
9451 
9452  /* Do NOT free the PGresult since we are keeping pointers into it */
9453  destroyPQExpBuffer(query);
9454 
9455  *items = comments;
9456  return ntups;
9457 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int objsubid
Definition: pg_dump.c:71
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
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:407

◆ collectSecLabels()

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

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

14986 {
14987  PGresult *res;
14988  PQExpBuffer query;
14989  int i_label;
14990  int i_provider;
14991  int i_classoid;
14992  int i_objoid;
14993  int i_objsubid;
14994  int ntups;
14995  int i;
14996  SecLabelItem *labels;
14997 
14998  query = createPQExpBuffer();
14999 
15000  appendPQExpBufferStr(query,
15001  "SELECT label, provider, classoid, objoid, objsubid "
15002  "FROM pg_catalog.pg_seclabel "
15003  "ORDER BY classoid, objoid, objsubid");
15004 
15005  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15006 
15007  /* Construct lookup table containing OIDs in numeric form */
15008  i_label = PQfnumber(res, "label");
15009  i_provider = PQfnumber(res, "provider");
15010  i_classoid = PQfnumber(res, "classoid");
15011  i_objoid = PQfnumber(res, "objoid");
15012  i_objsubid = PQfnumber(res, "objsubid");
15013 
15014  ntups = PQntuples(res);
15015 
15016  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15017 
15018  for (i = 0; i < ntups; i++)
15019  {
15020  labels[i].label = PQgetvalue(res, i, i_label);
15021  labels[i].provider = PQgetvalue(res, i, i_provider);
15022  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15023  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15024  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15025  }
15026 
15027  /* Do NOT free the PGresult since we are keeping pointers into it */
15028  destroyPQExpBuffer(query);
15029 
15030  *items = labels;
15031  return ntups;
15032 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
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:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
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:407

◆ convertOperatorReference()

static char * convertOperatorReference ( Archive fout,
const char *  opr 
)
static

Definition at line 12350 of file pg_dump.c.

References free, name, pg_strdup(), and psprintf().

Referenced by dumpAgg(), and dumpOpr().

12351 {
12352  char *name;
12353  char *oname;
12354  char *ptr;
12355  bool inquote;
12356  bool sawdot;
12357 
12358  /* In all cases "0" means a null reference */
12359  if (strcmp(opr, "0") == 0)
12360  return NULL;
12361 
12362  name = pg_strdup(opr);
12363  /* find non-double-quoted left paren, and check for non-quoted dot */
12364  inquote = false;
12365  sawdot = false;
12366  for (ptr = name; *ptr; ptr++)
12367  {
12368  if (*ptr == '"')
12369  inquote = !inquote;
12370  else if (*ptr == '.' && !inquote)
12371  sawdot = true;
12372  else if (*ptr == '(' && !inquote)
12373  {
12374  *ptr = '\0';
12375  break;
12376  }
12377  }
12378  /* If not schema-qualified, don't need to add OPERATOR() */
12379  if (!sawdot)
12380  return name;
12381  oname = psprintf("OPERATOR(%s)", name);
12382  free(name);
12383  return oname;
12384 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
const char * name
Definition: encode.c:521

◆ convertRegProcReference()

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

Definition at line 12314 of file pg_dump.c.

References name, and pg_strdup().

Referenced by dumpOpr().

12315 {
12316  char *name;
12317  char *paren;
12318  bool inquote;
12319 
12320  /* In all cases "-" means a null reference */
12321  if (strcmp(proc, "-") == 0)
12322  return NULL;
12323 
12324  name = pg_strdup(proc);
12325  /* find non-double-quoted left paren */
12326  inquote = false;
12327  for (paren = name; *paren; paren++)
12328  {
12329  if (*paren == '(' && !inquote)
12330  {
12331  *paren = '\0';
12332  break;
12333  }
12334  if (*paren == '"')
12335  inquote = !inquote;
12336  }
12337  return name;
12338 }
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 12395 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

12396 {
12397  char *result;
12398  char query[128];
12399  PGresult *res;
12400 
12401  snprintf(query, sizeof(query),
12402  "SELECT '%u'::pg_catalog.regproc", funcOid);
12403  res = ExecuteSqlQueryForSingleRow(fout, query);
12404 
12405  result = pg_strdup(PQgetvalue(res, 0, 0));
12406 
12407  PQclear(res);
12408 
12409  return result;
12410 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void PQclear(PGresult *res)
Definition: fe-exec.c:671

◆ createBoundaryObjects()

static DumpableObject * createBoundaryObjects ( void  )
static

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

17730 {
17731  DumpableObject *dobjs;
17732 
17733  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
17734 
17735  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
17736  dobjs[0].catId = nilCatalogId;
17737  AssignDumpId(dobjs + 0);
17738  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
17739 
17740  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
17741  dobjs[1].catId = nilCatalogId;
17742  AssignDumpId(dobjs + 1);
17743  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
17744 
17745  return dobjs;
17746 }
char * name
Definition: pg_dump.h:133
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:476
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:131
static const CatalogId nilCatalogId
Definition: pg_dump.c:133
DumpableObjectType objType
Definition: pg_dump.h:130

◆ createDummyViewAsClause()

static PQExpBuffer createDummyViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

Definition at line 15223 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), _tableInfo::attcollation, _tableInfo::attnames, _tableInfo::atttypnames, createPQExpBuffer(), _collInfo::dobj, findCollationByOid(), fmtId(), _dumpableObject::name, _tableInfo::numatts, and OidIsValid.

Referenced by dumpRule(), and dumpTableSchema().

15224 {
15225  PQExpBuffer result = createPQExpBuffer();
15226  int j;
15227 
15228  appendPQExpBufferStr(result, "SELECT");
15229 
15230  for (j = 0; j < tbinfo->numatts; j++)
15231  {
15232  if (j > 0)
15233  appendPQExpBufferChar(result, ',');
15234  appendPQExpBufferStr(result, "\n ");
15235 
15236  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15237 
15238  /*
15239  * Must add collation if not default for the type, because CREATE OR
15240  * REPLACE VIEW won't change it
15241  */
15242  if (OidIsValid(tbinfo->attcollation[j]))
15243  {
15244  CollInfo *coll;
15245 
15246  coll = findCollationByOid(tbinfo->attcollation[j]);
15247  if (coll)
15248  {
15249  /* always schema-qualify, don't try to be smart */
15250  appendPQExpBuffer(result, " COLLATE %s.",
15251  fmtId(coll->dobj.namespace->dobj.name));
15252  appendPQExpBufferStr(result, fmtId(coll->dobj.name));
15253  }
15254  }
15255 
15256  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15257  }
15258 
15259  return result;
15260 }
char * name
Definition: pg_dump.h:133
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char ** atttypnames
Definition: pg_dump.h:305
char ** attnames
Definition: pg_dump.h:304
#define OidIsValid(objectId)
Definition: c.h:586
DumpableObject dobj
Definition: pg_dump.h:246
Oid * attcollation
Definition: pg_dump.h:316
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
int numatts
Definition: pg_dump.h:303
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:803

◆ createViewAsClause()

static PQExpBuffer createViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

Definition at line 15174 of file pg_dump.c.

References appendBinaryPQExpBuffer(), appendPQExpBuffer(), Assert, _dumpableObject::catId, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), _tableInfo::dobj, ExecuteSqlQuery(), exit_horribly(), _dumpableObject::name, CatalogId::oid, PGRES_TUPLES_OK, PQclear(), PQgetlength(), PQgetvalue(), and PQntuples().

Referenced by dumpRule(), and dumpTableSchema().

15175 {
15176  PQExpBuffer query = createPQExpBuffer();
15177  PQExpBuffer result = createPQExpBuffer();
15178  PGresult *res;
15179  int len;
15180 
15181  /* Fetch the view definition */
15182  appendPQExpBuffer(query,
15183  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15184  tbinfo->dobj.catId.oid);
15185 
15186  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15187 
15188  if (PQntuples(res) != 1)
15189  {
15190  if (PQntuples(res) < 1)
15191  exit_horribly(NULL, "query to obtain definition of view \"%s\" returned no data\n",
15192  tbinfo->dobj.name);
15193  else
15194  exit_horribly(NULL, "query to obtain definition of view \"%s\" returned more than one definition\n",
15195  tbinfo->dobj.name);
15196  }
15197 
15198  len = PQgetlength(res, 0, 0);
15199 
15200  if (len == 0)
15201  exit_horribly(NULL, "definition of view \"%s\" appears to be empty (length zero)\n",
15202  tbinfo->dobj.name);
15203 
15204  /* Strip off the trailing semicolon so that other things may follow. */
15205  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15206  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15207 
15208  PQclear(res);
15209  destroyPQExpBuffer(query);
15210 
15211  return result;
15212 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3129
char * name
Definition: pg_dump.h:133
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
DumpableObject dobj
Definition: pg_dump.h:261
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define Assert(condition)
Definition: c.h:680
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:131
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:415
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:407

◆ dumpAccessMethod()

static void dumpAccessMethod ( Archive fout,
AccessMethodInfo oprinfo 
)
static

Definition at line 12417 of file pg_dump.c.

References _accessMethodInfo::amhandler, _accessMethodInfo::amtype, AMTYPE_INDEX, appendPQExpBuffer(), 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(), _dumpableObject::name, pg_free(), pg_strdup(), SECTION_PRE_DATA, and write_msg().

Referenced by dumpDumpableObject().

12418 {
12419  DumpOptions *dopt = fout->dopt;
12420  PQExpBuffer q;
12421  PQExpBuffer delq;
12422  PQExpBuffer labelq;
12423  char *qamname;
12424 
12425  /* Skip if not to be dumped */
12426  if (!aminfo->dobj.dump || dopt->dataOnly)
12427  return;
12428 
12429  q = createPQExpBuffer();
12430  delq = createPQExpBuffer();
12431  labelq = createPQExpBuffer();
12432 
12433  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12434 
12435  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12436 
12437  switch (aminfo->amtype)
12438  {
12439  case AMTYPE_INDEX:
12440  appendPQExpBuffer(q, "TYPE INDEX ");
12441  break;
12442  default:
12443  write_msg(NULL, "WARNING: invalid type \"%c\" of access method \"%s\"\n",
12444  aminfo->amtype, qamname);
12445  pg_free(qamname);
12446  destroyPQExpBuffer(q);
12447  destroyPQExpBuffer(delq);
12448  destroyPQExpBuffer(labelq);
12449  return;
12450  }
12451 
12452  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12453 
12454  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12455  qamname);
12456 
12457  appendPQExpBuffer(labelq, "ACCESS METHOD %s",
12458  qamname);
12459 
12460  if (dopt->binary_upgrade)
12461  binary_upgrade_extension_member(q, &aminfo->dobj, labelq->data);
12462 
12463  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12464  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12465  aminfo->dobj.name,
12466  NULL,
12467  NULL,
12468  "",
12469  false, "ACCESS METHOD", SECTION_PRE_DATA,
12470  q->data, delq->data, NULL,
12471  NULL, 0,
12472  NULL, NULL);
12473 
12474  /* Dump Access Method Comments */
12475  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12476  dumpComment(fout, labelq->data,
12477  NULL, "",
12478  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12479 
12480  pg_free(qamname);
12481 
12482  destroyPQExpBuffer(q);
12483  destroyPQExpBuffer(delq);
12484  destroyPQExpBuffer(labelq);
12485 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define AMTYPE_INDEX
Definition: pg_am.h:61
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:4143
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
void write_msg(const char *modulename, const char *fmt,...)
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)

◆ dumpACL()

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

Definition at line 14675 of file pg_dump.c.

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

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

14680 {
14681  DumpOptions *dopt = fout->dopt;
14682  PQExpBuffer sql;
14683 
14684  /* Do nothing if ACL dump is not enabled */
14685  if (dopt->aclsSkip)
14686  return;
14687 
14688  /* --data-only skips ACLs *except* BLOB ACLs */
14689  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14690  return;
14691 
14692  sql = createPQExpBuffer();
14693 
14694  /*
14695  * Check to see if this object has had any initial ACLs included for it.
14696  * If so, we are in binary upgrade mode and these are the ACLs to turn
14697  * into GRANT and REVOKE statements to set and record the initial
14698  * privileges for an extension object. Let the backend know that these
14699  * are to be recorded by calling binary_upgrade_set_record_init_privs()
14700  * before and after.
14701  */
14702  if (strlen(initacls) != 0 || strlen(initracls) != 0)
14703  {
14704  appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14705  if (!buildACLCommands(name, subname, type, initacls, initracls, owner,
14706  "", fout->remoteVersion, sql))
14707  exit_horribly(NULL,
14708  "could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14709  initacls, initracls, name, type);
14710  appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14711  }
14712 
14713  if (!buildACLCommands(name, subname, type, acls, racls, owner,
14714  "", fout->remoteVersion, sql))
14715  exit_horribly(NULL,
14716  "could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14717  acls, racls, name, type);
14718 
14719  if (sql->len > 0)
14721  tag, nspname,
14722  NULL,
14723  owner ? owner : "",
14724  false, "ACL", SECTION_NONE,
14725  sql->data, "", NULL,
14726  &(objDumpId), 1,
14727  NULL, NULL);
14728 
14729  destroyPQExpBuffer(sql);
14730 }
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
bool aclsSkip
Definition: pg_backup.h:141
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
bool buildACLCommands(const char *name, const char *subname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:55
const char * name
Definition: encode.c:521
void exit_horribly(const char *modulename, const char *fmt,...)
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
static const CatalogId nilCatalogId
Definition: pg_dump.c:133
DumpId createDumpId(void)
Definition: common.c:520
int remoteVersion
Definition: pg_backup.h:186

◆ dumpAgg()

static void dumpAgg ( Archive fout,
AggInfo agginfo 
)
static

Definition at line 13437 of file pg_dump.c.

References _aggInfo::aggfn, AGGKIND_HYPOTHETICAL, AGGKIND_NORMAL, AGGMODIFY_READ_ONLY, AGGMODIFY_READ_WRITE, AGGMODIFY_SHARABLE, appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _dumpableObject::catId, convertOperatorReference(), 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(), exit_horribly(), fmtId(), format_aggregate_signature(), format_function_arguments(), format_function_signature(), free, _funcInfo::initproacl, _funcInfo::initrproacl, _dumpableObject::name, CatalogId::oid, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), _funcInfo::proacl, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE, PROPARALLEL_UNSAFE, Archive::remoteVersion, _funcInfo::rolname, _funcInfo::rproacl, SECTION_PRE_DATA, selectSourceSchema(), and write_msg().

Referenced by dumpDumpableObject().

13438 {
13439  DumpOptions *dopt = fout->dopt;
13440  PQExpBuffer query;
13441  PQExpBuffer q;
13442  PQExpBuffer delq;
13443  PQExpBuffer labelq;
13444  PQExpBuffer details;
13445  char *aggsig; /* identity signature */
13446  char *aggfullsig = NULL; /* full signature */
13447  char *aggsig_tag;
13448  PGresult *res;
13449  int i_aggtransfn;
13450  int i_aggfinalfn;
13451  int i_aggcombinefn;
13452  int i_aggserialfn;
13453  int i_aggdeserialfn;
13454  int i_aggmtransfn;
13455  int i_aggminvtransfn;
13456  int i_aggmfinalfn;
13457  int i_aggfinalextra;
13458  int i_aggmfinalextra;
13459  int i_aggfinalmodify;
13460  int i_aggmfinalmodify;
13461  int i_aggsortop;
13462  int i_aggkind;
13463  int i_aggtranstype;
13464  int i_aggtransspace;
13465  int i_aggmtranstype;
13466  int i_aggmtransspace;
13467  int i_agginitval;
13468  int i_aggminitval;
13469  int i_convertok;
13470  int i_proparallel;
13471  const char *aggtransfn;
13472  const char *aggfinalfn;
13473  const char *aggcombinefn;
13474  const char *aggserialfn;
13475  const char *aggdeserialfn;
13476  const char *aggmtransfn;
13477  const char *aggminvtransfn;
13478  const char *aggmfinalfn;
13479  bool aggfinalextra;
13480  bool aggmfinalextra;
13481  char aggfinalmodify;
13482  char aggmfinalmodify;
13483  const char *aggsortop;
13484  char *aggsortconvop;
13485  char aggkind;
13486  const char *aggtranstype;
13487  const char *aggtransspace;
13488  const char *aggmtranstype;
13489  const char *aggmtransspace;
13490  const char *agginitval;
13491  const char *aggminitval;
13492  bool convertok;
13493  const char *proparallel;
13494  char defaultfinalmodify;
13495 
13496  /* Skip if not to be dumped */
13497  if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13498  return;
13499 
13500  query = createPQExpBuffer();
13501  q = createPQExpBuffer();
13502  delq = createPQExpBuffer();
13503  labelq = createPQExpBuffer();
13504  details = createPQExpBuffer();
13505 
13506  /* Make sure we are in proper schema */
13507  selectSourceSchema(fout, agginfo->aggfn.dobj.namespace->dobj.name);
13508 
13509  /* Get aggregate-specific details */
13510  if (fout->remoteVersion >= 110000)
13511  {
13512  appendPQExpBuffer(query, "SELECT aggtransfn, "
13513  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13514  "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13515  "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13516  "aggfinalextra, aggmfinalextra, "
13517  "aggfinalmodify, aggmfinalmodify, "
13518  "aggsortop::pg_catalog.regoperator, "
13519  "aggkind, "
13520  "aggtransspace, agginitval, "
13521  "aggmtransspace, aggminitval, "
13522  "true AS convertok, "
13523  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13524  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13525  "p.proparallel "
13526  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13527  "WHERE a.aggfnoid = p.oid "
13528  "AND p.oid = '%u'::pg_catalog.oid",
13529  agginfo->aggfn.dobj.catId.oid);
13530  }
13531  else if (fout->remoteVersion >= 90600)
13532  {
13533  appendPQExpBuffer(query, "SELECT aggtransfn, "
13534  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13535  "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13536  "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13537  "aggfinalextra, aggmfinalextra, "
13538  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13539  "aggsortop::pg_catalog.regoperator, "
13540  "aggkind, "
13541  "aggtransspace, agginitval, "
13542  "aggmtransspace, aggminitval, "
13543  "true AS convertok, "
13544  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13545  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13546  "p.proparallel "
13547  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13548  "WHERE a.aggfnoid = p.oid "
13549  "AND p.oid = '%u'::pg_catalog.oid",
13550  agginfo->aggfn.dobj.catId.oid);
13551  }
13552  else if (fout->remoteVersion >= 90400)
13553  {
13554  appendPQExpBuffer(query, "SELECT aggtransfn, "
13555  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13556  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13557  "'-' AS aggdeserialfn, aggmtransfn, aggminvtransfn, "
13558  "aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13559  "aggfinalextra, aggmfinalextra, "
13560  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13561  "aggsortop::pg_catalog.regoperator, "
13562  "aggkind, "
13563  "aggtransspace, agginitval, "
13564  "aggmtransspace, aggminitval, "
13565  "true AS convertok, "
13566  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13567  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13568  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13569  "WHERE a.aggfnoid = p.oid "
13570  "AND p.oid = '%u'::pg_catalog.oid",
13571  agginfo->aggfn.dobj.catId.oid);
13572  }
13573  else if (fout->remoteVersion >= 80400)
13574  {
13575  appendPQExpBuffer(query, "SELECT aggtransfn, "
13576  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13577  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13578  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13579  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13580  "0 AS aggmtranstype, false AS aggfinalextra, "
13581  "false AS aggmfinalextra, "
13582  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13583  "aggsortop::pg_catalog.regoperator, "
13584  "'n' AS aggkind, "
13585  "0 AS aggtransspace, agginitval, "
13586  "0 AS aggmtransspace, NULL AS aggminitval, "
13587  "true AS convertok, "
13588  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13589  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13590  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13591  "WHERE a.aggfnoid = p.oid "
13592  "AND p.oid = '%u'::pg_catalog.oid",
13593  agginfo->aggfn.dobj.catId.oid);
13594  }
13595  else if (fout->remoteVersion >= 80100)
13596  {
13597  appendPQExpBuffer(query, "SELECT aggtransfn, "
13598  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13599  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13600  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13601  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13602  "0 AS aggmtranstype, false AS aggfinalextra, "
13603  "false AS aggmfinalextra, "
13604  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13605  "aggsortop::pg_catalog.regoperator, "
13606  "'n' AS aggkind, "
13607  "0 AS aggtransspace, agginitval, "
13608  "0 AS aggmtransspace, NULL AS aggminitval, "
13609  "true AS convertok "
13610  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13611  "WHERE a.aggfnoid = p.oid "
13612  "AND p.oid = '%u'::pg_catalog.oid",
13613  agginfo->aggfn.dobj.catId.oid);
13614  }
13615  else
13616  {
13617  appendPQExpBuffer(query, "SELECT aggtransfn, "
13618  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13619  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13620  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13621  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13622  "0 AS aggmtranstype, false AS aggfinalextra, "
13623  "false AS aggmfinalextra, "
13624  "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13625  "0 AS aggsortop, "
13626  "'n' AS aggkind, "
13627  "0 AS aggtransspace, agginitval, "
13628  "0 AS aggmtransspace, NULL AS aggminitval, "
13629  "true AS convertok "
13630  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13631  "WHERE a.aggfnoid = p.oid "
13632  "AND p.oid = '%u'::pg_catalog.oid",
13633  agginfo->aggfn.dobj.catId.oid);
13634  }
13635 
13636  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13637 
13638  i_aggtransfn = PQfnumber(res, "aggtransfn");
13639  i_aggfinalfn = PQfnumber(res, "aggfinalfn");
13640  i_aggcombinefn = PQfnumber(res, "aggcombinefn");
13641  i_aggserialfn = PQfnumber(res, "aggserialfn");
13642  i_aggdeserialfn = PQfnumber(res, "aggdeserialfn");
13643  i_aggmtransfn = PQfnumber(res, "aggmtransfn");
13644  i_aggminvtransfn = PQfnumber(res, "aggminvtransfn");
13645  i_aggmfinalfn = PQfnumber(res, "aggmfinalfn");
13646  i_aggfinalextra = PQfnumber(res, "aggfinalextra");
13647  i_aggmfinalextra = PQfnumber(res, "aggmfinalextra");
13648  i_aggfinalmodify = PQfnumber(res, "aggfinalmodify");
13649  i_aggmfinalmodify = PQfnumber(res, "aggmfinalmodify");
13650  i_aggsortop = PQfnumber(res, "aggsortop");
13651  i_aggkind = PQfnumber(res, "aggkind");
13652  i_aggtranstype = PQfnumber(res, "aggtranstype");
13653  i_aggtransspace = PQfnumber(res, "aggtransspace");
13654  i_aggmtranstype = PQfnumber(res, "aggmtranstype");
13655  i_aggmtransspace = PQfnumber(res, "aggmtransspace");
13656  i_agginitval = PQfnumber(res, "agginitval");
13657  i_aggminitval = PQfnumber(res, "aggminitval");
13658  i_convertok = PQfnumber(res, "convertok");
13659  i_proparallel = PQfnumber(res, "proparallel");
13660 
13661  aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
13662  aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
13663  aggcombinefn = PQgetvalue(res, 0, i_aggcombinefn);
13664  aggserialfn = PQgetvalue(res, 0, i_aggserialfn);
13665  aggdeserialfn = PQgetvalue(res, 0, i_aggdeserialfn);
13666  aggmtransfn = PQgetvalue(res, 0, i_aggmtransfn);
13667  aggminvtransfn = PQgetvalue(res, 0, i_aggminvtransfn);
13668  aggmfinalfn = PQgetvalue(res, 0, i_aggmfinalfn);
13669  aggfinalextra = (PQgetvalue(res, 0, i_aggfinalextra)[0] == 't');
13670  aggmfinalextra = (PQgetvalue(res, 0, i_aggmfinalextra)[0] == 't');
13671  aggfinalmodify = PQgetvalue(res, 0, i_aggfinalmodify)[0];
13672  aggmfinalmodify = PQgetvalue(res, 0, i_aggmfinalmodify)[0];
13673  aggsortop = PQgetvalue(res, 0, i_aggsortop);
13674  aggkind = PQgetvalue(res, 0, i_aggkind)[0];
13675  aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
13676  aggtransspace = PQgetvalue(res, 0, i_aggtransspace);
13677  aggmtranstype = PQgetvalue(res, 0, i_aggmtranstype);
13678  aggmtransspace = PQgetvalue(res, 0, i_aggmtransspace);
13679  agginitval = PQgetvalue(res, 0, i_agginitval);
13680  aggminitval = PQgetvalue(res, 0, i_aggminitval);
13681  convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
13682 
13683  if (fout->remoteVersion >= 80400)
13684  {
13685  /* 8.4 or later; we rely on server-side code for most of the work */
13686  char *funcargs;
13687  char *funciargs;
13688 
13689  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13690  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13691  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13692  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13693  }
13694  else
13695  /* pre-8.4, do it ourselves */
13696  aggsig = format_aggregate_signature(agginfo, fout, true);
13697 
13698  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
13699 
13700  if (i_proparallel != -1)
13701  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13702  else
13703  proparallel = NULL;
13704 
13705  if (!convertok)
13706  {
13707  write_msg(NULL, "WARNING: aggregate function %s could not be dumped correctly for this database version; ignored\n",
13708  aggsig);
13709 
13710  if (aggfullsig)
13711  free(aggfullsig);
13712 
13713  free(aggsig);
13714 
13715  return;
13716  }
13717 
13718  /* identify default modify flag for aggkind (must match DefineAggregate) */
13719  defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
13720  /* replace omitted flags for old versions */
13721  if (aggfinalmodify == '0')
13722  aggfinalmodify = defaultfinalmodify;
13723  if (aggmfinalmodify == '0')
13724  aggmfinalmodify = defaultfinalmodify;
13725 
13726  /* regproc and regtype output is already sufficiently quoted */
13727  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
13728  aggtransfn, aggtranstype);
13729 
13730  if (strcmp(aggtransspace, "0") != 0)
13731  {
13732  appendPQExpBuffer(details, ",\n SSPACE = %s",
13733  aggtransspace);
13734  }
13735 
13736  if (!PQgetisnull(res, 0, i_agginitval))
13737  {
13738  appendPQExpBufferStr(details, ",\n INITCOND = ");
13739  appendStringLiteralAH(details, agginitval, fout);
13740  }
13741 
13742  if (strcmp(aggfinalfn, "-") != 0)
13743  {
13744  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
13745  aggfinalfn);
13746  if (aggfinalextra)
13747  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
13748  if (aggfinalmodify != defaultfinalmodify)
13749  {
13750  switch (aggfinalmodify)
13751  {
13752  case AGGMODIFY_READ_ONLY:
13753  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY");
13754  break;
13755  case AGGMODIFY_SHARABLE:
13756  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHARABLE");
13757  break;
13758  case AGGMODIFY_READ_WRITE:
13759  appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE");
13760  break;
13761  default:
13762  exit_horribly(NULL, "unrecognized aggfinalmodify value for aggregate \"%s\"\n",
13763  agginfo->aggfn.dobj.name);
13764  break;
13765  }
13766  }
13767  }
13768 
13769  if (strcmp(aggcombinefn, "-") != 0)
13770  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
13771 
13772  if (strcmp(aggserialfn, "-") != 0)
13773  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
13774 
13775  if (strcmp(aggdeserialfn, "-") != 0)
13776  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
13777 
13778  if (strcmp(aggmtransfn, "-") != 0)
13779  {
13780  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
13781  aggmtransfn,
13782  aggminvtransfn,
13783  aggmtranstype);
13784  }
13785 
13786  if (strcmp(aggmtransspace, "0") != 0)
13787  {
13788  appendPQExpBuffer(details, ",\n MSSPACE = %s",
13789  aggmtransspace);
13790  }
13791 
13792  if (!PQgetisnull(res, 0, i_aggminitval))
13793  {
13794  appendPQExpBufferStr(details, ",\n MINITCOND = ");
13795  appendStringLiteralAH(details, aggminitval, fout);
13796  }
13797 
13798  if (strcmp(aggmfinalfn, "-") != 0)
13799  {
13800  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
13801  aggmfinalfn);
13802  if (aggmfinalextra)
13803  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
13804  if (aggmfinalmodify != defaultfinalmodify)
13805  {
13806  switch (aggmfinalmodify)
13807  {
13808  case AGGMODIFY_READ_ONLY:
13809  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY");
13810  break;
13811  case AGGMODIFY_SHARABLE:
13812  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHARABLE");
13813  break;
13814  case AGGMODIFY_READ_WRITE:
13815  appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE");
13816  break;
13817  default:
13818  exit_horribly(NULL, "unrecognized aggmfinalmodify value for aggregate \"%s\"\n",
13819  agginfo->aggfn.dobj.name);
13820  break;
13821  }
13822  }
13823  }
13824 
13825  aggsortconvop = convertOperatorReference(fout, aggsortop);
13826  if (aggsortconvop)
13827  {
13828  appendPQExpBuffer(details, ",\n SORTOP = %s",
13829  aggsortconvop);
13830  free(aggsortconvop);
13831  }
13832 
13833  if (aggkind == AGGKIND_HYPOTHETICAL)
13834  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
13835 
13836  if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
13837  {
13838  if (proparallel[0] == PROPARALLEL_SAFE)
13839  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
13840  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13841  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
13842  else if (proparallel[0] != PROPARALLEL_UNSAFE)
13843  exit_horribly(NULL, "unrecognized proparallel value for function \"%s\"\n",
13844  agginfo->aggfn.dobj.name);
13845  }
13846 
13847  /*
13848  * DROP must be fully qualified in case same name appears in pg_catalog
13849  */
13850  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
13851  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13852  aggsig);
13853 
13854  appendPQExpBuffer(q, "CREATE AGGREGATE %s (\n%s\n);\n",
13855  aggfullsig ? aggfullsig : aggsig, details->data);
13856 
13857  appendPQExpBuffer(labelq, "AGGREGATE %s", aggsig);
13858 
13859  if (dopt->binary_upgrade)
13860  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj, labelq->data);
13861 
13862  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
13863  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
13864  agginfo->aggfn.dobj.dumpId,
13865  aggsig_tag,
13866  agginfo->aggfn.dobj.namespace->dobj.name,
13867  NULL,
13868  agginfo->aggfn.rolname,
13869  false, "AGGREGATE", SECTION_PRE_DATA,
13870  q->data, delq->data, NULL,
13871  NULL, 0,
13872  NULL, NULL);
13873 
13874  /* Dump Aggregate Comments */
13875  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
13876  dumpComment(fout, labelq->data,
13877  agginfo->aggfn.dobj.namespace->dobj.name,
13878  agginfo->aggfn.rolname,
13879  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13880 
13881  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
13882  dumpSecLabel(fout, labelq->data,
13883  agginfo->aggfn.dobj.namespace->dobj.name,
13884  agginfo->aggfn.rolname,
13885  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13886 
13887  /*
13888  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
13889  * command look like a function's GRANT; in particular this affects the
13890  * syntax for zero-argument aggregates and ordered-set aggregates.
13891  */
13892  free(aggsig);
13893  free(aggsig_tag);
13894 
13895  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
13896  aggsig_tag = format_function_signature(fout, &agginfo->aggfn, false);
13897 
13898  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
13899  dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
13900  "FUNCTION",
13901  aggsig, NULL, aggsig_tag,
13902  agginfo->aggfn.dobj.namespace->dobj.name,
13903  agginfo->aggfn.rolname, agginfo->aggfn.proacl,
13904  agginfo->aggfn.rproacl,
13905  agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
13906 
13907  free(aggsig);
13908  if (aggfullsig)
13909  free(aggfullsig);
13910  free(aggsig_tag);
13911 
13912  PQclear(res);
13913 
13914  destroyPQExpBuffer(query);
13915  destroyPQExpBuffer(q);
13916  destroyPQExpBuffer(delq);
13917  destroyPQExpBuffer(labelq);
13918  destroyPQExpBuffer(details);
13919 }
char * name
Definition: pg_dump.h:133
#define AGGMODIFY_READ_ONLY
Definition: pg_aggregate.h:143
#define AGGMODIFY_READ_WRITE
Definition: pg_aggregate.h:145
DumpComponents dump
Definition: pg_dump.h:134
static char * convertOperatorReference(Archive *fout, const char *opr)
Definition: pg_dump.c:12350
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char * initrproacl
Definition: pg_dump.h:207
DumpableObject dobj
Definition: pg_dump.h:198
#define PROPARALLEL_RESTRICTED
Definition: pg_proc.h:5547
DumpId dumpId
Definition: pg_dump.h:132
static char * format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
Definition: pg_dump.c:11207
FuncInfo aggfn
Definition: pg_dump.h:213
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
static void dumpSecLabel(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14751
#define AGGKIND_NORMAL
Definition: pg_aggregate.h:128
#define AGGMODIFY_SHARABLE
Definition: pg_aggregate.h:144
#define PROPARALLEL_SAFE
Definition: pg_proc.h:5546
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
#define PROPARALLEL_UNSAFE
Definition: pg_proc.h:5548
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:4143
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:299
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
#define free(a)
Definition: header.h:65
void write_msg(const char *modulename, const char *fmt,...)
char * initproacl
Definition: pg_dump.h:206
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *tag, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14675
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11302
char * rolname
Definition: pg_dump.h:199
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:95
char * proacl
Definition: pg_dump.h:204
char * rproacl
Definition: pg_dump.h:205
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:96
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:131
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
#define AGGKIND_HYPOTHETICAL
Definition: pg_aggregate.h:130
static char * format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:13399
int remoteVersion
Definition: pg_backup.h:186

◆ dumpAttrDef()

static void dumpAttrDef ( Archive fout,
AttrDefInfo adinfo 
)
static

Definition at line 15994 of file pg_dump.c.

References _attrDefInfo::adef_expr, _attrDefInfo::adnum, _attrDefInfo::adtable, appendPQExpBuffer(), 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(), free, _dumpableObject::name, psprintf(), _tableInfo::rolname, SECTION_PRE_DATA, and _attrDefInfo::separate.

Referenced by dumpDumpableObject().

15995 {
15996  DumpOptions *dopt = fout->dopt;
15997  TableInfo *tbinfo = adinfo->adtable;
15998  int adnum = adinfo->adnum;
15999  PQExpBuffer q;
16000  PQExpBuffer delq;
16001  char *tag;
16002 
16003  /* Skip if table definition not to be dumped */
16004  if (!tbinfo->dobj.dump || dopt->dataOnly)
16005  return;
16006 
16007  /* Skip if not "separate"; it was dumped in the table's definition */
16008  if (!adinfo->separate)
16009  return;
16010 
16011  q = createPQExpBuffer();
16012  delq = createPQExpBuffer();
16013 
16014  appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16015  fmtId(tbinfo->dobj.name));
16016  appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
16017  fmtId(tbinfo->attnames[adnum - 1]),
16018  adinfo->adef_expr);
16019 
16020  /*
16021  * DROP must be fully qualified in case same name appears in pg_catalog
16022  */
16023  appendPQExpBuffer(delq, "ALTER TABLE %s.",
16024  fmtId(tbinfo->dobj.namespace->dobj.name));
16025  appendPQExpBuffer(delq, "%s ",
16026  fmtId(tbinfo->dobj.name));
16027  appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
16028  fmtId(tbinfo->attnames[adnum - 1]));
16029 
16030  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16031 
16032  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16033  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16034  tag,
16035  tbinfo->dobj.namespace->dobj.name,
16036  NULL,
16037  tbinfo->rolname,
16038  false, "DEFAULT", SECTION_PRE_DATA,
16039  q->data, delq->data, NULL,
16040  NULL, 0,
16041  NULL, NULL);
16042 
16043  free(tag);
16044  destroyPQExpBuffer(q);
16045  destroyPQExpBuffer(delq);
16046 }
char * name
Definition: pg_dump.h:133
DumpComponents dump
Definition: pg_dump.h:134
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
char * rolname
Definition: pg_dump.h:262
DumpId dumpId
Definition: pg_dump.h:132
char ** attnames
Definition: pg_dump.h:304
DumpableObject dobj
Definition: pg_dump.h:261
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
bool separate
Definition: pg_dump.h:342
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
TableInfo * adtable
Definition: pg_dump.h:339
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
DumpableObject dobj
Definition: pg_dump.h:338
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
#define free(a)
Definition: header.h:65
CatalogId catId
Definition: pg_dump.h:131
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
char * adef_expr
Definition: pg_dump.h:341

◆ dumpBaseType()

static void dumpBaseType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10189 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, 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(), 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, selectSourceSchema(), _typeInfo::typacl, _typeInfo::typelem, and zeroAsOpaque.

Referenced by dumpType().

10190 {
10191  DumpOptions *dopt = fout->dopt;
10193  PQExpBuffer delq = createPQExpBuffer();
10194  PQExpBuffer labelq = createPQExpBuffer();
10195  PQExpBuffer query = createPQExpBuffer();
10196  PGresult *res;
10197  char *qtypname;
10198  char *typlen;
10199  char *typinput;
10200  char *typoutput;
10201  char *typreceive;
10202  char *typsend;
10203  char *typmodin;
10204  char *typmodout;
10205  char *typanalyze;
10206  Oid typreceiveoid;
10207  Oid typsendoid;
10208  Oid typmodinoid;
10209  Oid typmodoutoid;
10210  Oid typanalyzeoid;
10211  char *typcategory;
10212  char *typispreferred;
10213  char *typdelim;
10214  char *typbyval;
10215  char *typalign;
10216  char *typstorage;
10217  char *typcollatable;
10218  char *typdefault;
10219  bool typdefault_is_literal = false;
10220 
10221  /* Set proper schema search path so regproc references list correctly */
10222  selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10223 
10224  /* Fetch type-specific details */
10225  if (fout->remoteVersion >= 90100)
10226  {
10227  appendPQExpBuffer(query, "SELECT typlen, "
10228  "typinput, typoutput, typreceive, typsend, "
10229  "typmodin, typmodout, typanalyze, "
10230  "typreceive::pg_catalog.oid AS typreceiveoid, "
10231  "typsend::pg_catalog.oid AS typsendoid, "
10232  "typmodin::pg_catalog.oid AS typmodinoid, "
10233  "typmodout::pg_catalog.oid AS typmodoutoid, "
10234  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10235  "typcategory, typispreferred, "
10236  "typdelim, typbyval, typalign, typstorage, "
10237  "(typcollation <> 0) AS typcollatable, "
10238  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10239  "FROM pg_catalog.pg_type "
10240  "WHERE oid = '%u'::pg_catalog.oid",
10241  tyinfo->dobj.catId.oid);
10242  }
10243  else if (fout->remoteVersion >= 80400)
10244  {
10245  appendPQExpBuffer(query, "SELECT typlen, "
10246  "typinput, typoutput, typreceive, typsend, "
10247  "typmodin, typmodout, typanalyze, "
10248  "typreceive::pg_catalog.oid AS typreceiveoid, "
10249  "typsend::pg_catalog.oid AS typsendoid, "
10250  "typmodin::pg_catalog.oid AS typmodinoid, "
10251  "typmodout::pg_catalog.oid AS typmodoutoid, "
10252  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10253  "typcategory, typispreferred, "
10254  "typdelim, typbyval, typalign, typstorage, "
10255  "false AS typcollatable, "
10256  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10257  "FROM pg_catalog.pg_type "
10258  "WHERE oid = '%u'::pg_catalog.oid",
10259  tyinfo->dobj.catId.oid);
10260  }
10261  else if (fout->remoteVersion >= 80300)
10262  {
10263  /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
10264  appendPQExpBuffer(query, "SELECT typlen, "
10265  "typinput, typoutput, typreceive, typsend, "
10266  "typmodin, typmodout, typanalyze, "
10267  "typreceive::pg_catalog.oid AS typreceiveoid, "
10268  "typsend::pg_catalog.oid AS typsendoid, "
10269  "typmodin::pg_catalog.oid AS typmodinoid, "
10270  "typmodout::pg_catalog.oid AS typmodoutoid, "
10271  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10272  "'U' AS typcategory, false AS typispreferred, "
10273  "typdelim, typbyval, typalign, typstorage, "
10274  "false AS typcollatable, "
10275  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10276  "FROM pg_catalog.pg_type "
10277  "WHERE oid = '%u'::pg_catalog.oid",
10278  tyinfo->dobj.catId.oid);
10279  }
10280  else
10281  {
10282  appendPQExpBuffer(query, "SELECT typlen, "
10283  "typinput, typoutput, typreceive, typsend, "
10284  "'-' AS typmodin, '-' AS typmodout, "
10285  "typanalyze, "
10286  "typreceive::pg_catalog.oid AS typreceiveoid, "
10287  "typsend::pg_catalog.oid AS typsendoid, "
10288  "0 AS typmodinoid, 0 AS typmodoutoid, "
10289  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10290  "'U' AS typcategory, false AS typispreferred, "
10291  "typdelim, typbyval, typalign, typstorage, "
10292  "false AS typcollatable, "
10293  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10294  "FROM pg_catalog.pg_type "
10295  "WHERE oid = '%u'::pg_catalog.oid",
10296  tyinfo->dobj.catId.oid);
10297  }
10298 
10299  res = ExecuteSqlQueryForSingleRow(fout, query->data);
10300 
10301  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10302  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10303  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10304  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10305  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10306  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10307  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10308  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10309  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10310  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10311  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10312  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10313  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10314  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10315  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10316  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10317  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10318  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10319  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10320  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10321  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10322  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10323  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10324  {
10325  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10326  typdefault_is_literal = true; /* it needs quotes */
10327  }
10328  else
10329  typdefault = NULL;
10330 
10331  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10332 
10333  /*
10334  * DROP must be fully qualified in case same name appears in pg_catalog.
10335  * The reason we include CASCADE is that the circular dependency between
10336  * the type and its I/O functions makes it impossible to drop the type any
10337  * other way.
10338  */
10339  appendPQExpBuffer(delq, "DROP TYPE %s.",
10340  fmtId(tyinfo->dobj.namespace->dobj.name));
10341  appendPQExpBuffer(delq, "%s CASCADE;\n",
10342  qtypname);
10343 
10344  /*
10345  * We might already have a shell type, but setting pg_type_oid is
10346  * harmless, and in any case we'd better set the array type OID.
10347  */
10348  if (dopt->binary_upgrade)
10350  tyinfo->dobj.catId.oid,
10351  false);
10352 
10354  "CREATE TYPE %s (\n"
10355  " INTERNALLENGTH = %s",
10356  qtypname,
10357  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10358 
10359  /* regproc result is sufficiently quoted already */
10360  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
10361  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
10362  if (OidIsValid(typreceiveoid))
10363  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
10364  if (OidIsValid(typsendoid))
10365  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10366  if (OidIsValid(typmodinoid))
10367  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10368  if (OidIsValid(typmodoutoid))
10369  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10370  if (OidIsValid(typanalyzeoid))
10371  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10372 
10373  if (strcmp(typcollatable, "t") == 0)
10374  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10375 
10376  if (typdefault != NULL)
10377  {
10378  appendPQExpBufferStr(q, ",\n DEFAULT = ");
10379  if (typdefault_is_literal)
10380  appendStringLiteralAH(q, typdefault, fout);
10381  else
10382  appendPQExpBufferStr(q, typdefault);
10383  }
10384 
10385  if (OidIsValid(tyinfo->typelem))
10386  {
10387  char *elemType;
10388 
10389  /* reselect schema in case changed by function dump */
10390  selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10391  elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroAsOpaque);
10392  appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
10393  free(elemType);
10394  }
10395 
10396  if (strcmp(typcategory, "U") != 0)
10397  {
10398  appendPQExpBufferStr(q, ",\n CATEGORY = ");
10399  appendStringLiteralAH(q, typcategory, fout);
10400  }
10401 
10402  if (strcmp(typispreferred, "t") == 0)
10403  appendPQExpBufferStr(q, ",\n PREFERRED = true");
10404 
10405  if (typdelim && strcmp(typdelim, ",") != 0)
10406  {
10407  appendPQExpBufferStr(q, ",\n DELIMITER = ");
10408  appendStringLiteralAH(q, typdelim, fout);
10409  }
10410 
10411  if (strcmp(typalign, "c") == 0)
10412  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
10413  else if (strcmp(typalign, "s") == 0)
10414  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
10415  else if (strcmp(typalign, "i") == 0)
10416  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
10417  else if (strcmp(typalign, "d") == 0)
10418  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
10419 
10420  if (strcmp(typstorage, "p") == 0)
10421  appendPQExpBufferStr(q, ",\n STORAGE = plain");
10422  else if (strcmp(typstorage, "e") == 0)
10423  appendPQExpBufferStr(q, ",\n STORAGE = external");
10424  else if (strcmp(typstorage, "x") == 0)
10425  appendPQExpBufferStr(q, ",\n STORAGE = extended");
10426  else if (strcmp(typstorage, "m") == 0)
10427  appendPQExpBufferStr(q, ",\n STORAGE = main");
10428 
10429  if (strcmp(typbyval, "t") == 0)
10430  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
10431 
10432  appendPQExpBufferStr(q, "\n);\n");
10433 
10434  appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10435 
10436  if (dopt->binary_upgrade)
10437  binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10438 
10439  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10440  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10441  tyinfo->dobj.name,
10442  tyinfo->dobj.namespace->dobj.name,
10443  NULL,
10444  tyinfo->rolname, false,
10445  "TYPE", SECTION_PRE_DATA,
10446  q->data, delq->data, NULL,
10447  NULL, 0,
10448  NULL, NULL);
10449 
10450  /* Dump Type Comments and Security Labels */
10451  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10452  dumpComment(fout, labelq->data,
10453  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10454  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10455 
10456  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10457  dumpSecLabel(fout, labelq->data,
10458  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10459  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10460 
10461  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10462  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10463  qtypname, NULL, tyinfo->dobj.name,
10464  tyinfo->dobj.namespace->dobj.name,
10465  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10466  tyinfo->inittypacl, tyinfo->initrtypacl);
10467 
10468  PQclear(res);
10469  destroyPQExpBuffer(q);
10470  destroyPQExpBuffer(delq);
10471  destroyPQExpBuffer(labelq);
10472  destroyPQExpBuffer(query);
10473 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18001
char * name
Definition: pg_dump.h:133
DumpComponents dump
Definition: pg_dump.h:134
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
char * inittypacl
Definition: pg_dump.h:174
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char * initrtypacl
Definition: pg_dump.h:175
char * typacl
Definition: pg_dump.h:172
DumpId dumpId
Definition: pg_dump.h:132
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static void dumpSecLabel(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14751
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
Oid typelem
Definition: pg_dump.h:176
#define atooid(x)
Definition: postgres_ext.h:42
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
DumpableObject dobj
Definition: pg_dump.h:165
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:3961
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:4143
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:299
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
#define free(a)
Definition: header.h:65
char * rtypacl
Definition: pg_dump.h:173
char * rolname
Definition: pg_dump.h:171
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *tag, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14675
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:95
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:96
CatalogId catId
Definition: pg_dump.h:131
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
int remoteVersion
Definition: pg_backup.h:186

◆ dumpBlob()

static void dumpBlob ( Archive fout,
BlobInfo binfo 
)
static

Definition at line 3057 of file pg_dump.c.

References appendPQExpBuffer(), 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, resetPQExpBuffer(), _blobInfo::rolname, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

3058 {
3059  PQExpBuffer cquery = createPQExpBuffer();
3060  PQExpBuffer dquery = createPQExpBuffer();
3061 
3062  appendPQExpBuffer(cquery,
3063  "SELECT pg_catalog.lo_create('%s');\n",
3064  binfo->dobj.name);
3065 
3066  appendPQExpBuffer(dquery,
3067  "SELECT pg_catalog.lo_unlink('%s');\n",
3068  binfo->dobj.name);
3069 
3070  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3071  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3072  binfo->dobj.name,
3073  NULL, NULL,
3074  binfo->rolname, false,
3075  "BLOB", SECTION_PRE_DATA,
3076  cquery->data, dquery->data, NULL,
3077  NULL, 0,
3078  NULL, NULL);
3079 
3080  /* set up tag for comment and/or ACL */
3081  resetPQExpBuffer(cquery);
3082  appendPQExpBuffer(cquery, "LARGE OBJECT %s", binfo->dobj.name);
3083 
3084  /* Dump comment if any */
3085  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3086  dumpComment(fout, cquery->data,
3087  NULL, binfo->rolname,
3088  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3089 
3090  /* Dump security label if any */
3091  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3092  dumpSecLabel(fout, cquery->data,
3093  NULL, binfo->rolname,
3094  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3095 
3096  /* Dump ACL if any */
3097  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3098  dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
3099  binfo->dobj.name, NULL, cquery->data,
3100  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3101  binfo->initblobacl, binfo->initrblobacl);
3102 
3103  destroyPQExpBuffer(cquery);
3104  destroyPQExpBuffer(dquery);
3105 }
char * name
Definition: pg_dump.h:133
DumpComponents dump
Definition: pg_dump.h:134
DumpId dumpId
Definition: pg_dump.h:132
char * initblobacl
Definition: pg_dump.h:555
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
DumpableObject dobj
Definition: pg_dump.h:551
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static void dumpSecLabel(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14751
char * rolname
Definition: pg_dump.h:552
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *tag, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14675
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:95
char * blobacl
Definition: pg_dump.h:553
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:96
CatalogId catId
Definition: pg_dump.h:131
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
char * rblobacl
Definition: pg_dump.h:554
char * initrblobacl
Definition: pg_dump.h:556
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145

◆ dumpBlobs()

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

Definition at line 3112 of file pg_dump.c.

References atooid, buf, conn, EndBlob(), ExecuteSqlQuery(), ExecuteSqlStatement(), exit_horribly(), g_verbose, GetConnection(), i, INV_READ, lo_close(), lo_open(), lo_read(), LOBBUFSIZE, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQgetvalue(), PQntuples(), Archive::remoteVersion, selectSourceSchema(), StartBlob(), write_msg(), and WriteData().

Referenced by dumpDumpableObject().

3113 {
3114  const char *blobQry;
3115  const char *blobFetchQry;
3116  PGconn *conn = GetConnection(fout);
3117  PGresult *res;
3118  char buf[LOBBUFSIZE];
3119  int ntups;
3120  int i;
3121  int cnt;
3122 
3123  if (g_verbose)
3124  write_msg(NULL, "saving large objects\n");
3125 
3126  /* Make sure we are in proper schema */
3127  selectSourceSchema(fout, "pg_catalog");
3128 
3129  /*
3130  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3131  * the already-in-memory dumpable objects instead...
3132  */
3133  if (fout->remoteVersion >= 90000)
3134  blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_largeobject_metadata";
3135  else
3136  blobQry = "DECLARE bloboid CURSOR FOR SELECT DISTINCT loid FROM pg_largeobject";
3137 
3138  ExecuteSqlStatement(fout, blobQry);
3139 
3140  /* Command to fetch from cursor */
3141  blobFetchQry = "FETCH 1000 IN bloboid";
3142 
3143  do
3144  {
3145  /* Do a fetch */
3146  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3147 
3148  /* Process the tuples, if any */
3149  ntups = PQntuples(res);
3150  for (i = 0; i < ntups; i++)
3151  {
3152  Oid blobOid;
3153  int loFd;
3154 
3155  blobOid = atooid(PQgetvalue(res, i, 0));
3156  /* Open the BLOB */
3157  loFd = lo_open(conn, blobOid, INV_READ);
3158  if (loFd == -1)
3159  exit_horribly(NULL, "could not open large object %u: %s",
3160  blobOid, PQerrorMessage(conn));
3161 
3162  StartBlob(fout, blobOid);
3163 
3164  /* Now read it in chunks, sending data to archive */
3165  do
3166  {
3167  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3168  if (cnt < 0)
3169  exit_horribly(NULL, "error reading large object %u: %s",
3170  blobOid, PQerrorMessage(conn));
3171 
3172  WriteData(fout, buf, cnt);
3173  } while (cnt > 0);
3174 
3175  lo_close(conn, loFd);
3176 
3177  EndBlob(fout, blobOid);
3178  }
3179 
3180  PQclear(res);
3181  } while (ntups > 0);
3182 
3183  return 1;
3184 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
#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:46
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
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:107
void write_msg(const char *modulename, const char *fmt,...)
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:395
void exit_horribly(const char *modulename, const char *fmt,...)
int EndBlob(Archive *AH, Oid oid)
int i
#define LOBBUFSIZE
bool g_verbose
Definition: pg_dump.c:92
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:407
int remoteVersion
Definition: pg_backup.h:186

◆ dumpCast()

static void dumpCast ( Archive fout,
CastInfo cast 
)
static

Definition at line 11852 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), ArchiveEntry(), _dumpOptions::binary_upgrade, binary_upgrade_extension_member(), _castInfo::castcontext, _castInfo::castfunc, _castInfo::castmethod, _castInfo::castsource, _castInfo::casttarget, _dumpableObject::catId, COERCION_METHOD_BINARY, COERCION_METHOD_FUNCTION, COERCION_METHOD_INOUT, createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, destroyPQExpBuffer(), _funcInfo::dobj, _castInfo::dobj, Archive::dopt, _dumpableObject::dump, DUMP_COMPONENT_COMMENT, DUMP_COMPONENT_DEFINITION, dumpComment(), _dumpableObject::dumpId, exit_horribly(), findFuncByOid(), fmtId(), format_function_signature(), free, getFormattedTypeName(), _dumpableObject::name, OidIsValid, SECTION_PRE_DATA, selectSourceSchema(), write_msg(), and zeroAsNone.

Referenced by dumpDumpableObject().

11853 {
11854  DumpOptions *dopt = fout->dopt;
11855  PQExpBuffer defqry;
11856  PQExpBuffer delqry;
11857  PQExpBuffer labelq;
11858  FuncInfo *funcInfo = NULL;
11859  char *sourceType;
11860  char *targetType;
11861 
11862  /* Skip if not to be dumped */
11863  if (!cast->dobj.dump || dopt->dataOnly)
11864  return;
11865 
11866  /* Cannot dump if we don't have the cast function's info */
11867  if (OidIsValid(cast->castfunc))
11868  {
11869  funcInfo = findFuncByOid(cast->castfunc);
11870  if (funcInfo == NULL)
11871  exit_horribly(NULL, "could not find function definition for function with OID %u\n",
11872  cast->castfunc);
11873  }
11874 
11875  /*
11876  * Make sure we are in proper schema (needed for getFormattedTypeName).
11877  * Casts don't have a schema of their own, so use pg_catalog.
11878  */
11879  selectSourceSchema(fout, "pg_catalog");
11880 
11881  defqry = createPQExpBuffer();
11882  delqry = createPQExpBuffer();
11883  labelq = createPQExpBuffer();
11884 
11885  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
11886  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
11887  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
11888  sourceType, targetType);
11889 
11890  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
11891  sourceType, targetType);
11892 
11893  switch (cast->castmethod)
11894  {
11896  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
11897  break;
11898  case COERCION_METHOD_INOUT:
11899  appendPQExpBufferStr(defqry, "WITH INOUT");
11900  break;
11902  if (funcInfo)
11903  {
11904  char *fsig = format_function_signature(fout, funcInfo, true);
11905 
11906  /*
11907  * Always qualify the function name, in case it is not in
11908  * pg_catalog schema (format_function_signature won't qualify
11909  * it).
11910  */
11911  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
11912  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
11913  free(fsig);
11914  }
11915  else
11916  write_msg(NULL, "WARNING: bogus value in pg_cast.castfunc or pg_cast.castmethod field\n");
11917  break;
11918  default:
11919  write_msg(NULL, "WARNING: bogus value in pg_cast.castmethod field\n");
11920  }
11921 
11922  if (cast->castcontext == 'a')
11923  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
11924  else if (cast->castcontext == 'i')
11925  appendPQExpBufferStr(defqry, " AS IMPLICIT");
11926  appendPQExpBufferStr(defqry, ";\n");
11927 
11928  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
11929  sourceType, targetType);
11930 
11931  if (dopt->binary_upgrade)
11932  binary_upgrade_extension_member(defqry, &cast->dobj, labelq->data);
11933 
11934  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
11935  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
11936  labelq->data,
11937  "pg_catalog", NULL, "",
11938  false, "CAST", SECTION_PRE_DATA,
11939  defqry->data, delqry->data, NULL,
11940  NULL, 0,
11941  NULL, NULL);
11942 
11943  /* Dump Cast Comments */
11944  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
11945  dumpComment(fout, labelq->data,
11946  "pg_catalog", "",
11947  cast->dobj.catId, 0, cast->dobj.dumpId);
11948 
11949  free(sourceType);
11950  free(targetType);
11951 
11952  destroyPQExpBuffer(defqry);
11953  destroyPQExpBuffer(delqry);
11954  destroyPQExpBuffer(labelq);
11955 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:18001
char * name
Definition: pg_dump.h:133
Oid castsource
Definition: pg_dump.h:456
DumpComponents dump
Definition: pg_dump.h:134
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
DumpableObject dobj
Definition: pg_dump.h:198
DumpId dumpId
Definition: pg_dump.h:132
Oid castfunc
Definition: pg_dump.h:458
#define OidIsValid(objectId)
Definition: c.h:586
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
DumpableObject dobj
Definition: pg_dump.h:455
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
Oid casttarget
Definition: pg_dump.h:457
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:781
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:4143
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
#define free(a)
Definition: header.h:65
void write_msg(const char *modulename, const char *fmt,...)
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:11302
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:131
char castmethod
Definition: pg_dump.h:460
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
char castcontext
Definition: pg_dump.h:459

◆ dumpCollation()

static void dumpCollation ( Archive fout,
CollInfo collinfo 
)
static

Definition at line 13146 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, 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(), exit_horribly(), fmtId(), _dumpableObject::name, CatalogId::oid, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), Archive::remoteVersion, _collInfo::rolname, SECTION_PRE_DATA, and selectSourceSchema().

Referenced by dumpDumpableObject().

13147 {
13148  DumpOptions *dopt = fout->dopt;
13149  PQExpBuffer query;
13150  PQExpBuffer q;
13151  PQExpBuffer delq;
13152  PQExpBuffer labelq;
13153  PGresult *res;
13154  int i_collprovider;
13155  int i_collcollate;
13156  int i_collctype;
13157  const char *collprovider;
13158  const char *collcollate;
13159  const char *collctype;
13160 
13161  /* Skip if not to be dumped */
13162  if (!collinfo->dobj.dump || dopt->dataOnly)
13163  return;
13164 
13165  query = createPQExpBuffer();
13166  q = createPQExpBuffer();
13167  delq = createPQExpBuffer();
13168  labelq = createPQExpBuffer();
13169 
13170  /* Make sure we are in proper schema */
13171  selectSourceSchema(fout, collinfo->dobj.namespace->dobj.name);
13172 
13173  /* Get collation-specific details */
13174  if (fout->remoteVersion >= 100000)
13175  appendPQExpBuffer(query, "SELECT "
13176  "collprovider, "
13177  "collcollate, "
13178  "collctype, "
13179  "collversion "
13180  "FROM pg_catalog.pg_collation c "
13181  "WHERE c.oid = '%u'::pg_catalog.oid",
13182  collinfo->dobj.catId.oid);
13183  else
13184  appendPQExpBuffer(query, "SELECT "
13185  "'c' AS collprovider, "
13186  "collcollate, "
13187  "collctype, "
13188  "NULL AS collversion "
13189  "FROM pg_catalog.pg_collation c "
13190  "WHERE c.oid = '%u'::pg_catalog.oid",
13191  collinfo->dobj.catId.oid);
13192 
13193  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13194 
13195  i_collprovider = PQfnumber(res, "collprovider");
13196  i_collcollate = PQfnumber(res, "collcollate");
13197  i_collctype = PQfnumber(res, "collctype");
13198 
13199  collprovider = PQgetvalue(res, 0, i_collprovider);
13200  collcollate = PQgetvalue(res, 0, i_collcollate);
13201  collctype = PQgetvalue(res, 0, i_collctype);
13202 
13203  /*
13204  * DROP must be fully qualified in case same name appears in pg_catalog
13205  */
13206  appendPQExpBuffer(delq, "DROP COLLATION %s",
13207  fmtId(collinfo->dobj.namespace->dobj.name));
13208  appendPQExpBuffer(delq, ".%s;\n",
13209  fmtId(collinfo->dobj.name));
13210 
13211  appendPQExpBuffer(q, "CREATE COLLATION %s (",
13212  fmtId(collinfo->dobj.name));
13213 
13214  appendPQExpBufferStr(q, "provider = ");
13215  if (collprovider[0] == 'c')
13216  appendPQExpBufferStr(q, "libc");
13217  else if (collprovider[0] == 'i')
13218  appendPQExpBufferStr(q, "icu");
13219  else if (collprovider[0] == 'd')
13220  /* to allow dumping pg_catalog; not accepted on input */
13221  appendPQExpBufferStr(q, "default");
13222  else
13223  exit_horribly(NULL,
13224  "unrecognized collation provider: %s\n",
13225  collprovider);
13226 
13227  if (strcmp(collcollate, collctype) == 0)
13228  {
13229  appendPQExpBufferStr(q, ", locale = ");
13230  appendStringLiteralAH(q, collcollate, fout);
13231  }
13232  else
13233  {
13234  appendPQExpBufferStr(q, ", lc_collate = ");
13235  appendStringLiteralAH(q, collcollate, fout);
13236  appendPQExpBufferStr(q, ", lc_ctype = ");
13237  appendStringLiteralAH(q, collctype, fout);
13238  }
13239 
13240  /*
13241  * For binary upgrade, carry over the collation version. For normal
13242  * dump/restore, omit the version, so that it is computed upon restore.
13243  */
13244  if (dopt->binary_upgrade)
13245  {
13246  int i_collversion;
13247 
13248  i_collversion = PQfnumber(res, "collversion");
13249  if (!PQgetisnull(res, 0, i_collversion))
13250  {
13251  appendPQExpBufferStr(q, ", version = ");
13253  PQgetvalue(res, 0, i_collversion),
13254  fout);
13255  }
13256  }
13257 
13258  appendPQExpBufferStr(q, ");\n");
13259 
13260  appendPQExpBuffer(labelq, "COLLATION %s", fmtId(collinfo->dobj.name));
13261 
13262  if (dopt->binary_upgrade)
13263  binary_upgrade_extension_member(q, &collinfo->dobj, labelq->data);
13264 
13265  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13266  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13267  collinfo->dobj.name,
13268  collinfo->dobj.namespace->dobj.name,
13269  NULL,
13270  collinfo->rolname,
13271  false, "COLLATION", SECTION_PRE_DATA,
13272  q->data, delq->data, NULL,
13273  NULL, 0,
13274  NULL, NULL);
13275 
13276  /* Dump Collation Comments */
13277  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13278  dumpComment(fout, labelq->data,
13279  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13280  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13281 
13282  PQclear(res);
13283 
13284  destroyPQExpBuffer(query);
13285  destroyPQExpBuffer(q);
13286  destroyPQExpBuffer(delq);
13287  destroyPQExpBuffer(labelq);
13288 }
char * name
Definition: pg_dump.h:133
DumpComponents dump
Definition: pg_dump.h:134
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
DumpId dumpId
Definition: pg_dump.h:132
DumpableObject dobj
Definition: pg_dump.h:246
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
char * rolname
Definition: pg_dump.h:247
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:4143
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:299
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:131
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
int remoteVersion
Definition: pg_backup.h:186

◆ dumpComment()

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

Definition at line 9169 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ArchiveEntry(), _dumpOptions::binary_upgrade, createDumpId(), createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, CommentItem::descr, destroyPQExpBuffer(), Archive::dopt, findComments(), 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().

9172 {
9173  DumpOptions *dopt = fout->dopt;
9174  CommentItem *comments;
9175  int ncomments;
9176 
9177  /* Comments are schema not data ... except blob comments are data */
9178  if (strncmp(target, "LARGE OBJECT ", 13) != 0)
9179  {
9180  if (dopt->dataOnly)
9181  return;
9182  }
9183  else
9184  {
9185  /* We do dump blob comments in binary-upgrade mode */
9186  if (dopt->schemaOnly && !dopt->binary_upgrade)
9187  return;
9188  }
9189 
9190  /* Search for comments associated with catalogId, using table */
9191  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9192  &comments);
9193 
9194  /* Is there one matching the subid? */
9195  while (ncomments > 0)
9196  {
9197  if (comments->objsubid == subid)
9198  break;
9199  comments++;
9200  ncomments--;
9201  }
9202 
9203  /* If a comment exists, build COMMENT ON statement */
9204  if (ncomments > 0)
9205  {
9206  PQExpBuffer query = createPQExpBuffer();
9207 
9208  appendPQExpBuffer(query, "COMMENT ON %s IS ", target);
9209  appendStringLiteralAH(query, comments->descr, fout);
9210  appendPQExpBufferStr(query, ";\n");
9211 
9212  /*
9213  * We mark comments as SECTION_NONE because they really belong in the
9214  * same section as their parent, whether that is pre-data or
9215  * post-data.
9216  */
9218  target, namespace, NULL, owner,
9219  false, "COMMENT", SECTION_NONE,
9220  query->data, "", NULL,
9221  &(dumpId), 1,
9222  NULL, NULL);
9223 
9224  destroyPQExpBuffer(query);
9225  }
9226 }
Oid tableoid
Definition: pg_backup.h:226
bool schemaOnly
Definition: pg_backup.h:138
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int objsubid
Definition: pg_dump.c:71
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9324
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
const char * descr
Definition: pg_dump.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:299
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
static const CatalogId nilCatalogId
Definition: pg_dump.c:133
DumpId createDumpId(void)
Definition: common.c:520

◆ dumpCompositeType()

static void dumpCompositeType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10669 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralAH, ArchiveEntry(), atooid, _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, _collInfo::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(), 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, selectSourceSchema(), _typeInfo::typacl, and _typeInfo::typrelid.

Referenced by dumpType().

10670 {
10671  DumpOptions *dopt = fout->dopt;
10673  PQExpBuffer dropped = createPQExpBuffer();
10674  PQExpBuffer delq = createPQExpBuffer();
10675  PQExpBuffer labelq = createPQExpBuffer();
10676  PQExpBuffer query = createPQExpBuffer();
10677  PGresult *res;
10678  char *qtypname;
10679  int ntups;
10680  int i_attname;
10681  int i_atttypdefn;
10682  int i_attlen;
10683  int i_attalign;
10684  int i_attisdropped;
10685  int i_attcollation;
10686  int i;
10687  int actual_atts;
10688 
10689  /* Set proper schema search path so type references list correctly */
10690  selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10691 
10692  /* Fetch type specific details */
10693  if (fout->remoteVersion >= 90100)
10694  {
10695  /*
10696  * attcollation is new in 9.1. Since we only want to dump COLLATE
10697  * clauses for attributes whose collation is different from their
10698  * type's default, we use a CASE here to suppress uninteresting
10699  * attcollations cheaply. atttypid will be 0 for dropped columns;
10700  * collation does not matter for those.
10701  */
10702  appendPQExpBuffer(query, "SELECT a.attname, "
10703  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10704  "a.attlen, a.attalign, a.attisdropped, "
10705  "CASE WHEN a.attcollation <> at.typcollation "
10706  "THEN a.attcollation ELSE 0 END AS attcollation "
10707  "FROM pg_catalog.pg_type ct "
10708  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
10709  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
10710  "WHERE ct.oid = '%u'::pg_catalog.oid "
10711  "ORDER BY a.attnum ",
10712  tyinfo->dobj.catId.oid);
10713  }
10714  else
10715  {
10716  /*
10717  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
10718  * should always be false.
10719  */
10720  appendPQExpBuffer(query, "SELECT a.attname, "
10721  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10722  "a.attlen, a.attalign, a.attisdropped, "
10723  "0 AS attcollation "
10724  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
10725  "WHERE ct.oid = '%u'::pg_catalog.oid "
10726  "AND a.attrelid = ct.typrelid "
10727  "ORDER BY a.attnum ",
10728  tyinfo->dobj.catId.oid);
10729  }
10730 
10731  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10732 
10733  ntups = PQntuples(res);
10734 
10735  i_attname = PQfnumber(res, "attname");
10736  i_atttypdefn = PQfnumber(res, "atttypdefn");
10737  i_attlen = PQfnumber(res, "attlen");
10738  i_attalign = PQfnumber(res, "attalign");
10739  i_attisdropped = PQfnumber(res, "attisdropped");
10740  i_attcollation = PQfnumber(res, "attcollation");
10741 
10742  if (dopt->binary_upgrade)
10743  {
10745  tyinfo->dobj.catId.oid,
10746  false);
10747  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
10748  }
10749 
10750  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10751 
10752  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
10753  qtypname);
10754 
10755  actual_atts = 0;
10756  for (i = 0; i < ntups; i++)
10757  {
10758  char *attname;
10759  char *atttypdefn;
10760  char *attlen;
10761  char *attalign;
10762  bool attisdropped;
10763  Oid attcollation;
10764 
10765  attname = PQgetvalue(res, i, i_attname);
10766  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
10767  attlen = PQgetvalue(res, i, i_attlen);
10768  attalign = PQgetvalue(res, i, i_attalign);
10769  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
10770  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
10771 
10772  if (attisdropped && !dopt->binary_upgrade)
10773  continue;
10774 
10775  /* Format properly if not first attr */
10776  if (actual_atts++ > 0)
10777  appendPQExpBufferChar(q, ',');
10778  appendPQExpBufferStr(q, "\n\t");
10779 
10780  if (!attisdropped)
10781  {
10782  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
10783 
10784  /* Add collation if not default for the column type */
10785  if (OidIsValid(attcollation))
10786  {
10787  CollInfo *coll;
10788 
10789  coll = findCollationByOid(attcollation);
10790  if (coll)
10791  {
10792  /* always schema-qualify, don't try to be smart */
10793  appendPQExpBuffer(q, " COLLATE %s.",
10794  fmtId(coll->dobj.namespace->dobj.name));
10795  appendPQExpBufferStr(q, fmtId(coll->dobj.name));
10796  }
10797  }
10798  }
10799  else
10800  {
10801  /*
10802  * This is a dropped attribute and we're in binary_upgrade mode.
10803  * Insert a placeholder for it in the CREATE TYPE command, and set
10804  * length and alignment with direct UPDATE to the catalogs
10805  * afterwards. See similar code in dumpTableSchema().
10806  */
10807  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
10808 
10809  /* stash separately for insertion after the CREATE TYPE */
10810  appendPQExpBufferStr(dropped,
10811  "\n-- For binary upgrade, recreate dropped column.\n");
10812  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
10813  "SET attlen = %s, "
10814  "attalign = '%s', attbyval = false\n"
10815  "WHERE attname = ", attlen, attalign);
10816  appendStringLiteralAH(dropped, attname, fout);
10817  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
10818  appendStringLiteralAH(dropped, qtypname, fout);
10819  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
10820 
10821  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
10822  qtypname);
10823  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
10824  fmtId(attname));
10825  }
10826  }
10827  appendPQExpBufferStr(q, "\n);\n");
10828  appendPQExpBufferStr(q, dropped->data);
10829 
10830  /*
10831  * DROP must be fully qualified in case same name appears in pg_catalog
10832  */
10833  appendPQExpBuffer(delq, "DROP TYPE %s.",
10834  fmtId(tyinfo->dobj.namespace->dobj.name));
10835  appendPQExpBuffer(delq, "%s;\n",
10836  qtypname);
10837 
10838  appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10839 
10840  if (dopt->binary_upgrade)
10841  binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10842 
10843  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10844  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10845  tyinfo->dobj.name,
10846  tyinfo->dobj.namespace->dobj.name,
10847  NULL,
10848  tyinfo->rolname, false,
10849  "TYPE", SECTION_PRE_DATA,
10850  q->data, delq->data, NULL,
10851  NULL, 0,
10852  NULL, NULL);
10853 
10854 
10855  /* Dump Type Comments and Security Labels */
10856  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10857  dumpComment(fout, labelq->data,
10858  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10859  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10860 
10861  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10862  dumpSecLabel(fout, labelq->data,
10863  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10864  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10865 
10866  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10867  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10868  qtypname, NULL, tyinfo->dobj.name,
10869  tyinfo->dobj.namespace->dobj.name,
10870  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10871  tyinfo->inittypacl, tyinfo->initrtypacl);
10872 
10873  PQclear(res);
10874  destroyPQExpBuffer(q);
10875  destroyPQExpBuffer(dropped);
10876  destroyPQExpBuffer(delq);
10877  destroyPQExpBuffer(labelq);
10878  destroyPQExpBuffer(query);
10879 
10880  /* Dump any per-column comments */
10881  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10882  dumpCompositeTypeColComments(fout, tyinfo);
10883 }
char * name
Definition: pg_dump.h:133
DumpComponents dump
Definition: pg_dump.h:134
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
char * inittypacl
Definition: pg_dump.h:174
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
Oid typrelid
Definition: pg_dump.h:177
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char * initrtypacl
Definition: pg_dump.h:175
static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
Definition: pg_dump.c:10891
char * typacl
Definition: pg_dump.h:172
DumpId dumpId
Definition: pg_dump.h:132
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
#define OidIsValid(objectId)
Definition: c.h:586
DumpableObject dobj
Definition: pg_dump.h:246
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static void dumpSecLabel(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14751
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4076
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define atooid(x)
Definition: postgres_ext.h:42
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
DumpableObject dobj
Definition: pg_dump.h:165
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:3961
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:4143
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:299
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
char * rtypacl
Definition: pg_dump.h:173
char * rolname
Definition: pg_dump.h:171
static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *subname, const char *tag, const char *nspname, const char *owner, const char *acls, const char *racls, const char *initacls, const char *initracls)
Definition: pg_dump.c:14675
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:803
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:95
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:96
CatalogId catId
Definition: pg_dump.h:131
int i
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:407
int remoteVersion
Definition: pg_backup.h:186

◆ dumpCompositeTypeColComments()

static void dumpCompositeTypeColComments ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10891 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ArchiveEntry(), atooid, createDumpId(), createPQExpBuffer(), PQExpBufferData::data, CommentItem::descr, destroyPQExpBuffer(), _typeInfo::dobj, _dumpableObject::dumpId, ExecuteSqlQuery(), findComments(), fmtId(), i, _dumpableObject::name, CommentItem::objsubid, PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), resetPQExpBuffer(), _typeInfo::rolname, SECTION_NONE, and _typeInfo::typrelid.

Referenced by dumpCompositeType().

10892 {
10893  CommentItem *comments;
10894  int ncomments;
10895  PGresult *res;
10896  PQExpBuffer query;
10897  PQExpBuffer target;
10898  Oid pgClassOid;
10899  int i;
10900  int ntups;
10901  int i_attname;
10902  int i_attnum;
10903 
10904  query = createPQExpBuffer();
10905 
10906  appendPQExpBuffer(query,
10907  "SELECT c.tableoid, a.attname, a.attnum "
10908  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
10909  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
10910  " AND NOT a.attisdropped "
10911  "ORDER BY a.attnum ",
10912  tyinfo->typrelid);
10913 
10914  /* Fetch column attnames */
10915  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10916 
10917  ntups = PQntuples(res);
10918  if (ntups < 1)
10919  {
10920  PQclear(res);
10921  destroyPQExpBuffer(query);
10922  return;
10923  }
10924 
10925  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
10926 
10927  /* Search for comments associated with type's pg_class OID */
10928  ncomments = findComments(fout,
10929  pgClassOid,
10930  tyinfo->typrelid,
10931  &comments);
10932 
10933  /* If no comments exist, we're done */
10934  if (ncomments <= 0)
10935  {
10936  PQclear(res);
10937  destroyPQExpBuffer(query);
10938  return;
10939  }
10940 
10941  /* Build COMMENT ON statements */
10942  target = createPQExpBuffer();
10943 
10944  i_attnum = PQfnumber(res, "attnum");
10945  i_attname = PQfnumber(res, "attname");
10946  while (ncomments > 0)
10947  {
10948  const char *attname;
10949 
10950  attname = NULL;
10951  for (i = 0; i < ntups; i++)
10952  {
10953  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
10954  {
10955  attname = PQgetvalue(res, i, i_attname);
10956  break;
10957  }
10958  }
10959  if (attname) /* just in case we don't find it */
10960  {
10961  const char *descr = comments->descr;
10962 
10963  resetPQExpBuffer(target);
10964  appendPQExpBuffer(target, "COLUMN %s.",
10965  fmtId(tyinfo->dobj.name));
10966  appendPQExpBufferStr(target, fmtId(attname));
10967 
10968  resetPQExpBuffer(query);
10969  appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
10970  appendStringLiteralAH(query, descr, fout);
10971  appendPQExpBufferStr(query, ";\n");
10972 
10974  target->data,
10975  tyinfo->dobj.namespace->dobj.name,
10976  NULL, tyinfo->rolname,
10977  false, "COMMENT", SECTION_NONE,
10978  query->data, "", NULL,
10979  &(tyinfo->dobj.dumpId), 1,
10980  NULL, NULL);
10981  }
10982 
10983  comments++;
10984  ncomments--;
10985  }
10986 
10987  PQclear(res);
10988  destroyPQExpBuffer(query);
10989  destroyPQExpBuffer(target);
10990 }
char * name
Definition: pg_dump.h:133
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
Oid typrelid
Definition: pg_dump.h:177
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int objsubid
Definition: pg_dump.c:71
DumpId dumpId
Definition: pg_dump.h:132
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:9324
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
#define atooid(x)
Definition: postgres_ext.h:42
const char * descr
Definition: pg_dump.c:68
DumpableObject dobj
Definition: pg_dump.h:165
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:299
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * rolname
Definition: pg_dump.h:171
int i
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
static const CatalogId nilCatalogId
Definition: pg_dump.c:133
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:407
DumpId createDumpId(void)
Definition: common.c:520

◆ dumpConstraint()

static void dumpConstraint ( Archive fout,
ConstraintInfo coninfo 
)
static

Definition at line 16235 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendReloptionsArrayAH(), ArchiveEntry(), _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(), exit_horribly(), findObjectByDumpId(), fmtId(), free, getAttrName(), _indxInfo::indisclustered, _indxInfo::indkeys, _indxInfo::indnkeys, _indxInfo::indreloptions, InvalidAttrNumber, _dumpableObject::name, nonemptyReloptions(), CatalogId::oid, psprintf(), _typeInfo::rolname, _tableInfo::rolname, SECTION_POST_DATA, _constraintInfo::separate, and _indxInfo::tablespace.

Referenced by dumpDumpableObject().

16236 {
16237  DumpOptions *dopt = fout->dopt;
16238  TableInfo *tbinfo = coninfo->contable;
16239  PQExpBuffer q;
16240  PQExpBuffer delq;
16241  char *tag = NULL;
16242 
16243  /* Skip if not to be dumped */
16244  if (!coninfo->dobj.dump || dopt->dataOnly)
16245  return;
16246 
16247  q = createPQExpBuffer();
16248  delq = createPQExpBuffer();
16249 
16250  if (coninfo->contype == 'p' ||
16251  coninfo->contype == 'u' ||
16252  coninfo->contype == 'x')
16253  {
16254  /* Index-related constraint */
16255  IndxInfo *indxinfo;
16256  int k;
16257 
16258  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16259 
16260  if (indxinfo == NULL)
16261  exit_horribly(NULL, "missing index for constraint \"%s\"\n",
16262  coninfo->dobj.name);
16263 
16264  if (dopt->binary_upgrade)
16266  indxinfo->dobj.catId.oid, true);
16267 
16268  appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
16269  fmtId(tbinfo->dobj.name));
16270  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
16271  fmtId(coninfo->dobj.name));
16272 
16273  if (coninfo->condef)
16274  {
16275  /* pg_get_constraintdef should have provided everything */
16276  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16277  }
16278  else
16279  {
16280  appendPQExpBuffer(q, "%s (",
16281  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16282  for (k = 0; k < indxinfo->indnkeys; k++)
16283  {
16284  int indkey = (int) indxinfo->indkeys[k];
16285  const char *attname;
16286 
16287  if (indkey == InvalidAttrNumber)
16288  break;
16289  attname = getAttrName(indkey, tbinfo);
16290 
16291  appendPQExpBuffer(q, "%s%s",
16292  (k == 0) ? "" : ", ",
16293  fmtId(attname));
16294  }
16295 
16296  appendPQExpBufferChar(q, ')');
16297 
16298  if (nonemptyReloptions(indxinfo->indreloptions))
16299  {
16300  appendPQExpBufferStr(q, " WITH (");
16301  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16302  appendPQExpBufferChar(q, ')');
16303  }
16304 
16305  if (coninfo->condeferrable)
16306  {
16307  appendPQExpBufferStr(q, " DEFERRABLE");
16308  if (coninfo->condeferred)
16309  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16310  }
16311 
16312  appendPQExpBufferStr(q, ";\n");
16313  }
16314 
16315  /* If the index is clustered, we need to record that. */
16316  if (indxinfo->indisclustered)
16317  {
16318  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16319  fmtId(tbinfo->dobj.name));
16320  appendPQExpBuffer(q, " ON %s;\n",
16321  fmtId(indxinfo->dobj.name));
16322  }
16323 
16324  /*
16325  * DROP must be fully qualified in case same name appears in
16326  * pg_catalog
16327  */
16328  appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
16329  fmtId(tbinfo->dobj.namespace->dobj.name));
16330  appendPQExpBuffer(delq, "%s ",
16331  fmtId(tbinfo->dobj.name));
16332  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16333  fmtId(coninfo->dobj.name));
16334 
16335  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16336 
16337  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16338  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16339  tag,
16340  tbinfo->dobj.namespace->dobj.name,
16341  indxinfo->tablespace,
16342  tbinfo->rolname, false,
16343  "CONSTRAINT", SECTION_POST_DATA,
16344  q->data, delq->data, NULL,
16345  NULL, 0,
16346  NULL, NULL);
16347  }
16348  else if (coninfo->contype == 'f')
16349  {
16350  /*
16351  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
16352  * current table data is not processed
16353  */
16354  appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
16355  fmtId(tbinfo->dobj.name));
16356  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16357  fmtId(coninfo->dobj.name),
16358  coninfo->condef);
16359 
16360  /*
16361  * DROP must be fully qualified in case same name appears in
16362  * pg_catalog
16363  */
16364  appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
16365  fmtId(tbinfo->dobj.namespace->dobj.name));
16366  appendPQExpBuffer(delq, "%s ",
16367  fmtId(tbinfo->dobj.name));
16368  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16369  fmtId(coninfo->dobj.name));
16370 
16371  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16372 
16373  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16374  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16375  tag,
16376  tbinfo->dobj.namespace->dobj.name,
16377  NULL,
16378  tbinfo->rolname, false,
16379  "FK CONSTRAINT", SECTION_POST_DATA,
16380  q->data, delq->data, NULL,
16381  NULL, 0,
16382  NULL, NULL);
16383  }
16384  else if (coninfo->contype == 'c' && tbinfo)
16385  {
16386  /* CHECK constraint on a table */
16387 
16388  /* Ignore if not to be dumped separately, or if it was inherited */
16389  if (coninfo->separate && coninfo->conislocal)
16390  {
16391  /* not ONLY since we want it to propagate to children */
16392  appendPQExpBuffer(q, "ALTER TABLE %s\n",
16393  fmtId(tbinfo->dobj.name));
16394  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16395  fmtId(coninfo->dobj.name),
16396  coninfo->condef);
16397 
16398  /*
16399  * DROP must be fully qualified in case same name appears in
16400  * pg_catalog
16401  */
16402  appendPQExpBuffer(delq, "ALTER TABLE %s.",
16403  fmtId(tbinfo->dobj.namespace->dobj.name));
16404  appendPQExpBuffer(delq, "%s ",
16405  fmtId(tbinfo->dobj.name));
16406  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16407  fmtId(coninfo->dobj.name));
16408 
16409  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16410 
16411  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16412  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16413  tag,
16414  tbinfo->dobj.namespace->dobj.name,
16415  NULL,
16416  tbinfo->rolname, false,
16417  "CHECK CONSTRAINT", SECTION_POST_DATA,
16418  q->data, delq->data, NULL,
16419  NULL, 0,
16420  NULL, NULL);
16421  }
16422  }
16423  else if (coninfo->contype == 'c' && tbinfo == NULL)
16424  {
16425  /* CHECK constraint on a domain */
16426  TypeInfo *tyinfo = coninfo->condomain;
16427 
16428  /* Ignore if not to be dumped separately */
16429  if (coninfo->separate)
16430  {
16431  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
16432  fmtId(tyinfo->dobj.name));
16433  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
16434  fmtId(coninfo->dobj.name),
16435  coninfo->condef);
16436 
16437  /*
16438  * DROP must be fully qualified in case same name appears in
16439  * pg_catalog
16440  */
16441  appendPQExpBuffer(delq, "ALTER DOMAIN %s.",
16442  fmtId(tyinfo->dobj.namespace->dobj.name));
16443  appendPQExpBuffer(delq, "%s ",
16444  fmtId(tyinfo->dobj.name));
16445  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16446  fmtId(coninfo->dobj.name));
16447 
16448  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
16449 
16450  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16451  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16452  tag,
16453  tyinfo->dobj.namespace->dobj.name,
16454  NULL,
16455  tyinfo->rolname, false,
16456  "CHECK CONSTRAINT", SECTION_POST_DATA,
16457  q->data, delq->data, NULL,
16458  NULL, 0,
16459  NULL, NULL);
16460  }
16461  }
16462  else
16463  {
16464  exit_horribly(NULL, "unrecognized constraint type: %c\n",
16465  coninfo->contype);
16466  }
16467 
16468  /* Dump Constraint Comments --- only works for table constraints */
16469  if (tbinfo && coninfo->separate &&
16470  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16471  dumpTableConstraintComment(fout, coninfo);
16472 
16473  free(tag);
16474  destroyPQExpBuffer(q);
16475  destroyPQExpBuffer(delq);
16476 }
char * tablespace
Definition: pg_dump.h:358
char * name
Definition: pg_dump.h:133
DumpComponents dump
Definition: pg_dump.h:134
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
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:18084
char * rolname
Definition: pg_dump.h:262
DumpId dumpId
Definition: pg_dump.h:132
DumpId conindex
Definition: pg_dump.h:432
DumpableObject dobj
Definition: pg_dump.h:261
char * condef
Definition: pg_dump.h:430
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
DumpableObject dobj
Definition: pg_dump.h:426
DumpOptions * dopt
Definition: pg_backup.h:181
static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
Definition: pg_dump.c:16486
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:540
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
Oid * indkeys
Definition: pg_dump.h:361
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:4076
static const char * getAttrName(int attrnum, TableInfo *tblInfo)
Definition: pg_dump.c:16056
bool condeferrable
Definition: pg_dump.h:433
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:18072
int indnkeys
Definition: pg_dump.h:360
char * indreloptions
Definition: pg_dump.h:359
DumpableObject dobj
Definition: pg_dump.h:165
TypeInfo * condomain
Definition: pg_dump.h:428
bool conislocal
Definition: pg_dump.h:435
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
TableInfo * contable
Definition: pg_dump.h:427
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
#define free(a)
Definition: header.h:65
bool condeferred
Definition: pg_dump.h:434
char * rolname
Definition: pg_dump.h:171
#define InvalidAttrNumber
Definition: attnum.h:23
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:131
DumpableObject dobj
Definition: pg_dump.h:355
bool indisclustered
Definition: pg_dump.h:362
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)

◆ dumpConversion()

static void dumpConversion ( Archive fout,
ConvInfo convinfo 
)
static

Definition at line 13295 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, 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(), _dumpableObject::name, CatalogId::oid, PQclear(), PQfnumber(), PQgetvalue(), _convInfo::rolname, SECTION_PRE_DATA, and selectSourceSchema().

Referenced by dumpDumpableObject().

13296 {
13297  DumpOptions *dopt = fout->dopt;
13298  PQExpBuffer query;
13299  PQExpBuffer q;
13300  PQExpBuffer delq;
13301  PQExpBuffer labelq;
13302  PGresult *res;
13303  int i_conforencoding;
13304  int i_contoencoding;
13305  int i_conproc;
13306  int i_condefault;
13307  const char *conforencoding;
13308  const char *contoencoding;
13309  const char *conproc;
13310  bool condefault;
13311 
13312  /* Skip if not to be dumped */
13313  if (!convinfo->dobj.dump || dopt->dataOnly)
13314  return;
13315 
13316  query = createPQExpBuffer();
13317  q = createPQExpBuffer();
13318  delq = createPQExpBuffer();
13319  labelq = createPQExpBuffer();
13320 
13321  /* Make sure we are in proper schema */
13322  selectSourceSchema(fout, convinfo->dobj.namespace->dobj.name);
13323 
13324  /* Get conversion-specific details */
13325  appendPQExpBuffer(query, "SELECT "
13326  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13327  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13328  "conproc, condefault "
13329  "FROM pg_catalog.pg_conversion c "
13330  "WHERE c.oid = '%u'::pg_catalog.oid",
13331  convinfo->dobj.catId.oid);
13332 
13333  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13334 
13335  i_conforencoding = PQfnumber(res, "conforencoding");
13336  i_contoencoding = PQfnumber(res, "contoencoding");
13337  i_conproc = PQfnumber(res, "conproc");
13338  i_condefault = PQfnumber(res, "condefault");
13339 
13340  conforencoding = PQgetvalue(res, 0, i_conforencoding);
13341  contoencoding = PQgetvalue(res, 0, i_contoencoding);
13342  conproc = PQgetvalue(res, 0, i_conproc);
13343  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13344 
13345  /*
13346  * DROP must be fully qualified in case same name appears in pg_catalog
13347  */
13348  appendPQExpBuffer(delq, "DROP CONVERSION %s",
13349  fmtId(convinfo->dobj.namespace->dobj.name));
13350  appendPQExpBuffer(delq, ".%s;\n",
13351  fmtId(convinfo->dobj.name));
13352 
13353  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13354  (condefault) ? "DEFAULT " : "",
13355  fmtId(convinfo->dobj.name));
13356  appendStringLiteralAH(q, conforencoding, fout);
13357  appendPQExpBufferStr(q, " TO ");
13358  appendStringLiteralAH(q, contoencoding, fout);
13359  /* regproc output is already sufficiently quoted */
13360  appendPQExpBuffer(q, " FROM %s;\n", conproc);
13361 
13362  appendPQExpBuffer(labelq, "CONVERSION %s", fmtId(convinfo->dobj.name));
13363 
13364  if (dopt->binary_upgrade)
13365  binary_upgrade_extension_member(q, &convinfo->dobj, labelq->data);
13366 
13367  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13368  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13369  convinfo->dobj.name,
13370  convinfo->dobj.namespace->dobj.name,
13371  NULL,
13372  convinfo->rolname,
13373  false, "CONVERSION", SECTION_PRE_DATA,
13374  q->data, delq->data, NULL,
13375  NULL, 0,
13376  NULL, NULL);
13377 
13378  /* Dump Conversion Comments */
13379  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13380  dumpComment(fout, labelq->data,
13381  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13382  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13383 
13384  PQclear(res);
13385 
13386  destroyPQExpBuffer(query);
13387  destroyPQExpBuffer(q);
13388  destroyPQExpBuffer(delq);
13389  destroyPQExpBuffer(labelq);
13390 }
char * name
Definition: pg_dump.h:133
DumpComponents dump
Definition: pg_dump.h:134
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char * rolname
Definition: pg_dump.h:253
DumpId dumpId
Definition: pg_dump.h:132
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:422
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:135
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:9169
DumpOptions * dopt
Definition: pg_backup.h:181
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:139
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17975
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:94
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:4143
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:299
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:92
DumpableObject dobj
Definition: pg_dump.h:252
CatalogId catId
Definition: pg_dump.h:131
void ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)

◆ dumpDatabase()