PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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_am.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 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)
 
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)
 
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 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)
 
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)
 
PartInfogetPartitions (Archive *fout, int *numPartitions)
 
void getIndexes (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 getTablePartitionKeyInfo (Archive *fout, TableInfo *tblinfo, int numTables)
 
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 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

Enumeration Type Documentation

enum OidOptions
Enumerator
zeroAsOpaque 
zeroAsAny 
zeroAsStar 
zeroAsNone 

Definition at line 81 of file pg_dump.c.

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

Function Documentation

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

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

17085 {
17086  DumpableObject *preDataBound = boundaryObjs + 0;
17087  DumpableObject *postDataBound = boundaryObjs + 1;
17088  int i;
17089 
17090  for (i = 0; i < numObjs; i++)
17091  {
17092  DumpableObject *dobj = dobjs[i];
17093 
17094  /*
17095  * The classification of object types here must match the SECTION_xxx
17096  * values assigned during subsequent ArchiveEntry calls!
17097  */
17098  switch (dobj->objType)
17099  {
17100  case DO_NAMESPACE:
17101  case DO_EXTENSION:
17102  case DO_TYPE:
17103  case DO_SHELL_TYPE:
17104  case DO_FUNC:
17105  case DO_AGG:
17106  case DO_OPERATOR:
17107  case DO_ACCESS_METHOD:
17108  case DO_OPCLASS:
17109  case DO_OPFAMILY:
17110  case DO_COLLATION:
17111  case DO_CONVERSION:
17112  case DO_TABLE:
17113  case DO_ATTRDEF:
17114  case DO_PROCLANG:
17115  case DO_CAST:
17116  case DO_DUMMY_TYPE:
17117  case DO_TSPARSER:
17118  case DO_TSDICT:
17119  case DO_TSTEMPLATE:
17120  case DO_TSCONFIG:
17121  case DO_FDW:
17122  case DO_FOREIGN_SERVER:
17123  case DO_TRANSFORM:
17124  case DO_BLOB:
17125  /* Pre-data objects: must come before the pre-data boundary */
17126  addObjectDependency(preDataBound, dobj->dumpId);
17127  break;
17128  case DO_TABLE_DATA:
17129  case DO_SEQUENCE_SET:
17130  case DO_BLOB_DATA:
17131  /* Data objects: must come between the boundaries */
17132  addObjectDependency(dobj, preDataBound->dumpId);
17133  addObjectDependency(postDataBound, dobj->dumpId);
17134  break;
17135  case DO_INDEX:
17136  case DO_REFRESH_MATVIEW:
17137  case DO_TRIGGER:
17138  case DO_EVENT_TRIGGER:
17139  case DO_DEFAULT_ACL:
17140  case DO_POLICY:
17141  case DO_PUBLICATION:
17142  case DO_PUBLICATION_REL:
17143  case DO_SUBSCRIPTION:
17144  /* Post-data objects: must come after the post-data boundary */
17145  addObjectDependency(dobj, postDataBound->dumpId);
17146  break;
17147  case DO_RULE:
17148  /* Rules are post-data, but only if dumped separately */
17149  if (((RuleInfo *) dobj)->separate)
17150  addObjectDependency(dobj, postDataBound->dumpId);
17151  break;
17152  case DO_CONSTRAINT:
17153  case DO_FK_CONSTRAINT:
17154  /* Constraints are post-data, but only if dumped separately */
17155  if (((ConstraintInfo *) dobj)->separate)
17156  addObjectDependency(dobj, postDataBound->dumpId);
17157  break;
17158  case DO_PRE_DATA_BOUNDARY:
17159  /* nothing to do */
17160  break;
17161  case DO_POST_DATA_BOUNDARY:
17162  /* must come after the pre-data boundary */
17163  addObjectDependency(dobj, preDataBound->dumpId);
17164  break;
17165  }
17166  }
17167 }
DumpId dumpId
Definition: pg_dump.h:131
Definition: pg_dump.h:49
Definition: pg_dump.h:72
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:745
int i
DumpableObjectType objType
Definition: pg_dump.h:129
static void appendReloptionsArrayAH ( PQExpBuffer  buffer,
const char *  reloptions,
const char *  prefix,
Archive fout 
)
static

Definition at line 17413 of file pg_dump.c.

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

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

17415 {
17416  bool res;
17417 
17418  res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
17419  fout->std_strings);
17420  if (!res)
17421  write_msg(NULL, "WARNING: could not parse reloptions array\n");
17422 }
int encoding
Definition: pg_backup.h:194
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:731
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
bool std_strings
Definition: pg_backup.h:195
static void binary_upgrade_extension_member ( PQExpBuffer  upgrade_buffer,
DumpableObject dobj,
const char *  objlabel 
)
static

Definition at line 3897 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), _dumpableObject::dependencies, DO_EXTENSION, exit_horribly(), _dumpableObject::ext_member, findObjectByDumpId(), fmtId(), i, _dumpableObject::name, _dumpableObject::nDeps, NULL, 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().

3900 {
3901  DumpableObject *extobj = NULL;
3902  int i;
3903 
3904  if (!dobj->ext_member)
3905  return;
3906 
3907  /*
3908  * Find the parent extension. We could avoid this search if we wanted to
3909  * add a link field to DumpableObject, but the space costs of that would
3910  * be considerable. We assume that member objects could only have a
3911  * direct dependency on their own extension, not any others.
3912  */
3913  for (i = 0; i < dobj->nDeps; i++)
3914  {
3915  extobj = findObjectByDumpId(dobj->dependencies[i]);
3916  if (extobj && extobj->objType == DO_EXTENSION)
3917  break;
3918  extobj = NULL;
3919  }
3920  if (extobj == NULL)
3921  exit_horribly(NULL, "could not find parent extension for %s\n", objlabel);
3922 
3923  appendPQExpBufferStr(upgrade_buffer,
3924  "\n-- For binary upgrade, handle extension membership the hard way\n");
3925  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s;\n",
3926  fmtId(extobj->name),
3927  objlabel);
3928 }
char * name
Definition: pg_dump.h:132
DumpId * dependencies
Definition: pg_dump.h:137
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:136
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:573
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
int i
DumpableObjectType objType
Definition: pg_dump.h:129
static void binary_upgrade_set_pg_class_oids ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_class_oid,
bool  is_index 
)
static

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

3833 {
3834  PQExpBuffer upgrade_query = createPQExpBuffer();
3835  PGresult *upgrade_res;
3836  Oid pg_class_reltoastrelid;
3837  Oid pg_index_indexrelid;
3838 
3839  appendPQExpBuffer(upgrade_query,
3840  "SELECT c.reltoastrelid, i.indexrelid "
3841  "FROM pg_catalog.pg_class c LEFT JOIN "
3842  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
3843  "WHERE c.oid = '%u'::pg_catalog.oid;",
3844  pg_class_oid);
3845 
3846  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
3847 
3848  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
3849  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
3850 
3851  appendPQExpBufferStr(upgrade_buffer,
3852  "\n-- For binary upgrade, must preserve pg_class oids\n");
3853 
3854  if (!is_index)
3855  {
3856  appendPQExpBuffer(upgrade_buffer,
3857  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
3858  pg_class_oid);
3859  /* only tables have toast tables, not indexes */
3860  if (OidIsValid(pg_class_reltoastrelid))
3861  {
3862  /*
3863  * One complexity is that the table definition might not require
3864  * the creation of a TOAST table, and the TOAST table might have
3865  * been created long after table creation, when the table was
3866  * loaded with wide data. By setting the TOAST oid we force
3867  * creation of the TOAST heap and TOAST index by the backend so we
3868  * can cleanly copy the files during binary upgrade.
3869  */
3870 
3871  appendPQExpBuffer(upgrade_buffer,
3872  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
3873  pg_class_reltoastrelid);
3874 
3875  /* every toast table has an index */
3876  appendPQExpBuffer(upgrade_buffer,
3877  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
3878  pg_index_indexrelid);
3879  }
3880  }
3881  else
3882  appendPQExpBuffer(upgrade_buffer,
3883  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
3884  pg_class_oid);
3885 
3886  appendPQExpBufferChar(upgrade_buffer, '\n');
3887 
3888  PQclear(upgrade_res);
3889  destroyPQExpBuffer(upgrade_query);
3890 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:534
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define atooid(x)
Definition: lo.c:17
static bool binary_upgrade_set_type_oids_by_rel_oid ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_rel_oid 
)
static

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

3787 {
3788  PQExpBuffer upgrade_query = createPQExpBuffer();
3789  PGresult *upgrade_res;
3790  Oid pg_type_oid;
3791  bool toast_set = false;
3792 
3793  /* we only support old >= 8.3 for binary upgrades */
3794  appendPQExpBuffer(upgrade_query,
3795  "SELECT c.reltype AS crel, t.reltype AS trel "
3796  "FROM pg_catalog.pg_class c "
3797  "LEFT JOIN pg_catalog.pg_class t ON "
3798  " (c.reltoastrelid = t.oid) "
3799  "WHERE c.oid = '%u'::pg_catalog.oid;",
3800  pg_rel_oid);
3801 
3802  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
3803 
3804  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
3805 
3806  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
3807  pg_type_oid);
3808 
3809  if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
3810  {
3811  /* Toast tables do not have pg_type array rows */
3812  Oid pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
3813  PQfnumber(upgrade_res, "trel")));
3814 
3815  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
3816  appendPQExpBuffer(upgrade_buffer,
3817  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
3818  pg_type_toast_oid);
3819 
3820  toast_set = true;
3821  }
3822 
3823  PQclear(upgrade_res);
3824  destroyPQExpBuffer(upgrade_query);
3825 
3826  return toast_set;
3827 }
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid)
Definition: pg_dump.c:3746
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
unsigned int Oid
Definition: postgres_ext.h:31
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define atooid(x)
Definition: lo.c:17
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
static void binary_upgrade_set_type_oids_by_type_oid ( Archive fout,
PQExpBuffer  upgrade_buffer,
Oid  pg_type_oid 
)
static

Definition at line 3746 of file pg_dump.c.

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

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

3749 {
3750  PQExpBuffer upgrade_query = createPQExpBuffer();
3751  PGresult *upgrade_res;
3752  Oid pg_type_array_oid;
3753 
3754  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
3755  appendPQExpBuffer(upgrade_buffer,
3756  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
3757  pg_type_oid);
3758 
3759  /* we only support old >= 8.3 for binary upgrades */
3760  appendPQExpBuffer(upgrade_query,
3761  "SELECT typarray "
3762  "FROM pg_catalog.pg_type "
3763  "WHERE pg_type.oid = '%u'::pg_catalog.oid;",
3764  pg_type_oid);
3765 
3766  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
3767 
3768  pg_type_array_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "typarray")));
3769 
3770  if (OidIsValid(pg_type_array_oid))
3771  {
3772  appendPQExpBufferStr(upgrade_buffer,
3773  "\n-- For binary upgrade, must preserve pg_type array oid\n");
3774  appendPQExpBuffer(upgrade_buffer,
3775  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
3776  pg_type_array_oid);
3777  }
3778 
3779  PQclear(upgrade_res);
3780  destroyPQExpBuffer(upgrade_query);
3781 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:534
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define atooid(x)
Definition: lo.c:17
static void BuildArchiveDependencies ( Archive fout)
static

Definition at line 17194 of file pg_dump.c.

References _tocEntry::dependencies, _tocEntry::dumpId, findDumpableDependencies(), findObjectByDumpId(), free, _dumpableObject::nDeps, _tocEntry::nDeps, _tocEntry::next, NULL, pg_malloc(), pg_realloc(), _tocEntry::reqs, and _archiveHandle::toc.

Referenced by main().

17195 {
17196  ArchiveHandle *AH = (ArchiveHandle *) fout;
17197  TocEntry *te;
17198 
17199  /* Scan all TOC entries in the archive */
17200  for (te = AH->toc->next; te != AH->toc; te = te->next)
17201  {
17202  DumpableObject *dobj;
17203  DumpId *dependencies;
17204  int nDeps;
17205  int allocDeps;
17206 
17207  /* No need to process entries that will not be dumped */
17208  if (te->reqs == 0)
17209  continue;
17210  /* Ignore entries that already have "special" dependencies */
17211  if (te->nDeps > 0)
17212  continue;
17213  /* Otherwise, look up the item's original DumpableObject, if any */
17214  dobj = findObjectByDumpId(te->dumpId);
17215  if (dobj == NULL)
17216  continue;
17217  /* No work if it has no dependencies */
17218  if (dobj->nDeps <= 0)
17219  continue;
17220  /* Set up work array */
17221  allocDeps = 64;
17222  dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
17223  nDeps = 0;
17224  /* Recursively find all dumpable dependencies */
17225  findDumpableDependencies(AH, dobj,
17226  &dependencies, &nDeps, &allocDeps);
17227  /* And save 'em ... */
17228  if (nDeps > 0)
17229  {
17230  dependencies = (DumpId *) pg_realloc(dependencies,
17231  nDeps * sizeof(DumpId));
17232  te->dependencies = dependencies;
17233  te->nDeps = nDeps;
17234  }
17235  else
17236  free(dependencies);
17237  }
17238 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:228
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:573
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:17242
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
static void buildMatViewRefreshDependencies ( Archive fout)
static

Definition at line 2178 of file pg_dump.c.

References addObjectDependency(), appendPQExpBufferStr(), Assert, atooid, createPQExpBuffer(), PQExpBufferData::data, _tableInfo::dataObj, destroyPQExpBuffer(), DO_REFRESH_MATVIEW, DO_TABLE, _dumpableObject::dumpId, ExecuteSqlQuery(), findObjectByCatalogId(), i, NULL, _dumpableObject::objType, CatalogId::oid, PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), _tableInfo::relispopulated, _tableInfo::relkind, RELKIND_MATVIEW, Archive::remoteVersion, selectSourceSchema(), and CatalogId::tableoid.

Referenced by main().

2179 {
2180  PQExpBuffer query;
2181  PGresult *res;
2182  int ntups,
2183  i;
2184  int i_classid,
2185  i_objid,
2186  i_refobjid;
2187 
2188  /* No Mat Views before 9.3. */
2189  if (fout->remoteVersion < 90300)
2190  return;
2191 
2192  /* Make sure we are in proper schema */
2193  selectSourceSchema(fout, "pg_catalog");
2194 
2195  query = createPQExpBuffer();
2196 
2197  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2198  "( "
2199  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2200  "FROM pg_depend d1 "
2201  "JOIN pg_class c1 ON c1.oid = d1.objid "
2202  "AND c1.relkind = 'm' "
2203  "JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2204  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2205  "AND d2.objid = r1.oid "
2206  "AND d2.refobjid <> d1.objid "
2207  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2208  "AND c2.relkind IN ('m','v') "
2209  "WHERE d1.classid = 'pg_class'::regclass "
2210  "UNION "
2211  "SELECT w.objid, d3.refobjid, c3.relkind "
2212  "FROM w "
2213  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2214  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2215  "AND d3.objid = r3.oid "
2216  "AND d3.refobjid <> w.refobjid "
2217  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2218  "AND c3.relkind IN ('m','v') "
2219  ") "
2220  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2221  "FROM w "
2222  "WHERE refrelkind = 'm'");
2223 
2224  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2225 
2226  ntups = PQntuples(res);
2227 
2228  i_classid = PQfnumber(res, "classid");
2229  i_objid = PQfnumber(res, "objid");
2230  i_refobjid = PQfnumber(res, "refobjid");
2231 
2232  for (i = 0; i < ntups; i++)
2233  {
2234  CatalogId objId;
2235  CatalogId refobjId;
2236  DumpableObject *dobj;
2237  DumpableObject *refdobj;
2238  TableInfo *tbinfo;
2239  TableInfo *reftbinfo;
2240 
2241  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2242  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2243  refobjId.tableoid = objId.tableoid;
2244  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2245 
2246  dobj = findObjectByCatalogId(objId);
2247  if (dobj == NULL)
2248  continue;
2249 
2250  Assert(dobj->objType == DO_TABLE);
2251  tbinfo = (TableInfo *) dobj;
2252  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2253  dobj = (DumpableObject *) tbinfo->dataObj;
2254  if (dobj == NULL)
2255  continue;
2256  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2257 
2258  refdobj = findObjectByCatalogId(refobjId);
2259  if (refdobj == NULL)
2260  continue;
2261 
2262  Assert(refdobj->objType == DO_TABLE);
2263  reftbinfo = (TableInfo *) refdobj;
2264  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2265  refdobj = (DumpableObject *) reftbinfo->dataObj;
2266  if (refdobj == NULL)
2267  continue;
2268  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2269 
2270  addObjectDependency(dobj, refdobj->dumpId);
2271 
2272  if (!reftbinfo->relispopulated)
2273  tbinfo->relispopulated = false;
2274  }
2275 
2276  PQclear(res);
2277 
2278  destroyPQExpBuffer(query);
2279 }
char relkind
Definition: pg_dump.h:266
Oid tableoid
Definition: pg_backup.h:224
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
#define RELKIND_MATVIEW
Definition: pg_class.h:167
DumpId dumpId
Definition: pg_dump.h:131
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
DumpableObject * findObjectByCatalogId(CatalogId catalogId)
Definition: common.c:591
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
struct _tableDataInfo * dataObj
Definition: pg_dump.h:325
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:745
#define atooid(x)
Definition: lo.c:17
int i
bool relispopulated
Definition: pg_dump.h:268
DumpableObjectType objType
Definition: pg_dump.h:129
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:409
int remoteVersion
Definition: pg_backup.h:183
static bool checkExtensionMembership ( DumpableObject dobj,
Archive fout 
)
static

Definition at line 1305 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(), NULL, and Archive::remoteVersion.

Referenced by selectDumpableAccessMethod(), selectDumpableCast(), selectDumpableNamespace(), selectDumpableObject(), selectDumpableProcLang(), selectDumpableTable(), and selectDumpableType().

1306 {
1307  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1308 
1309  if (ext == NULL)
1310  return false;
1311 
1312  dobj->ext_member = true;
1313 
1314  /* Record dependency so that getDependencies needn't deal with that */
1315  addObjectDependency(dobj, ext->dobj.dumpId);
1316 
1317  /*
1318  * In 9.6 and above, mark the member object to have any non-initial ACL,
1319  * policies, and security labels dumped.
1320  *
1321  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1322  * extract the information about the object. We don't provide support for
1323  * initial policies and security labels and it seems unlikely for those to
1324  * ever exist, but we may have to revisit this later.
1325  *
1326  * Prior to 9.6, we do not include any extension member components.
1327  *
1328  * In binary upgrades, we still dump all components of the members
1329  * individually, since the idea is to exactly reproduce the database
1330  * contents rather than replace the extension contents with something
1331  * different.
1332  */
1333  if (fout->dopt->binary_upgrade)
1334  dobj->dump = ext->dobj.dump;
1335  else
1336  {
1337  if (fout->remoteVersion < 90600)
1338  dobj->dump = DUMP_COMPONENT_NONE;
1339  else
1340  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1343  }
1344 
1345  return true;
1346 }
DumpableObject dobj
Definition: pg_dump.h:154
DumpComponents dump
Definition: pg_dump.h:133
DumpId dumpId
Definition: pg_dump.h:131
bool ext_member
Definition: pg_dump.h:136
#define DUMP_COMPONENT_NONE
Definition: pg_dump.h:90
int binary_upgrade
Definition: pg_backup.h:133
ExtensionInfo * findOwningExtension(CatalogId catalogId)
Definition: common.c:885
DumpOptions * dopt
Definition: pg_backup.h:178
DumpComponents dump_contains
Definition: pg_dump.h:135
#define NULL
Definition: c.h:226
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:94
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:95
void addObjectDependency(DumpableObject *dobj, DumpId refId)
Definition: common.c:745
CatalogId catId
Definition: pg_dump.h:130
#define DUMP_COMPONENT_POLICY
Definition: pg_dump.h:96
int remoteVersion
Definition: pg_backup.h:183
static int collectComments ( Archive fout,
CommentItem **  items 
)
static

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

9054 {
9055  PGresult *res;
9056  PQExpBuffer query;
9057  int i_description;
9058  int i_classoid;
9059  int i_objoid;
9060  int i_objsubid;
9061  int ntups;
9062  int i;
9063  CommentItem *comments;
9064 
9065  /*
9066  * Note we do NOT change source schema here; preserve the caller's
9067  * setting, instead.
9068  */
9069 
9070  query = createPQExpBuffer();
9071 
9072  appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9073  "FROM pg_catalog.pg_description "
9074  "ORDER BY classoid, objoid, objsubid");
9075 
9076  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9077 
9078  /* Construct lookup table containing OIDs in numeric form */
9079 
9080  i_description = PQfnumber(res, "description");
9081  i_classoid = PQfnumber(res, "classoid");
9082  i_objoid = PQfnumber(res, "objoid");
9083  i_objsubid = PQfnumber(res, "objsubid");
9084 
9085  ntups = PQntuples(res);
9086 
9087  comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9088 
9089  for (i = 0; i < ntups; i++)
9090  {
9091  comments[i].descr = PQgetvalue(res, i, i_description);
9092  comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9093  comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9094  comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9095  }
9096 
9097  /* Do NOT free the PGresult since we are keeping pointers into it */
9098  destroyPQExpBuffer(query);
9099 
9100  *items = comments;
9101  return ntups;
9102 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:69
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
const char * descr
Definition: pg_dump.c:66
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
Oid objoid
Definition: pg_dump.c:68
Oid classoid
Definition: pg_dump.c:67
#define atooid(x)
Definition: lo.c:17
int i
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:409
static int collectSecLabels ( Archive fout,
SecLabelItem **  items 
)
static

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

14438 {
14439  PGresult *res;
14440  PQExpBuffer query;
14441  int i_label;
14442  int i_provider;
14443  int i_classoid;
14444  int i_objoid;
14445  int i_objsubid;
14446  int ntups;
14447  int i;
14448  SecLabelItem *labels;
14449 
14450  query = createPQExpBuffer();
14451 
14452  appendPQExpBufferStr(query,
14453  "SELECT label, provider, classoid, objoid, objsubid "
14454  "FROM pg_catalog.pg_seclabel "
14455  "ORDER BY classoid, objoid, objsubid");
14456 
14457  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14458 
14459  /* Construct lookup table containing OIDs in numeric form */
14460  i_label = PQfnumber(res, "label");
14461  i_provider = PQfnumber(res, "provider");
14462  i_classoid = PQfnumber(res, "classoid");
14463  i_objoid = PQfnumber(res, "objoid");
14464  i_objsubid = PQfnumber(res, "objsubid");
14465 
14466  ntups = PQntuples(res);
14467 
14468  labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
14469 
14470  for (i = 0; i < ntups; i++)
14471  {
14472  labels[i].label = PQgetvalue(res, i, i_label);
14473  labels[i].provider = PQgetvalue(res, i, i_provider);
14474  labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
14475  labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
14476  labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
14477  }
14478 
14479  /* Do NOT free the PGresult since we are keeping pointers into it */
14480  destroyPQExpBuffer(query);
14481 
14482  *items = labels;
14483  return ntups;
14484 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
Oid objoid
Definition: pg_dump.c:77
const char * provider
Definition: pg_dump.c:74
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
int objsubid
Definition: pg_dump.c:78
#define atooid(x)
Definition: lo.c:17
int i
Oid classoid
Definition: pg_dump.c:76
const char * label
Definition: pg_dump.c:75
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:409
static char * convertOperatorReference ( Archive fout,
const char *  opr 
)
static

Definition at line 11968 of file pg_dump.c.

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

Referenced by dumpAgg(), and dumpOpr().

11969 {
11970  char *name;
11971  char *oname;
11972  char *ptr;
11973  bool inquote;
11974  bool sawdot;
11975 
11976  /* In all cases "0" means a null reference */
11977  if (strcmp(opr, "0") == 0)
11978  return NULL;
11979 
11980  name = pg_strdup(opr);
11981  /* find non-double-quoted left paren, and check for non-quoted dot */
11982  inquote = false;
11983  sawdot = false;
11984  for (ptr = name; *ptr; ptr++)
11985  {
11986  if (*ptr == '"')
11987  inquote = !inquote;
11988  else if (*ptr == '.' && !inquote)
11989  sawdot = true;
11990  else if (*ptr == '(' && !inquote)
11991  {
11992  *ptr = '\0';
11993  break;
11994  }
11995  }
11996  /* If not schema-qualified, don't need to add OPERATOR() */
11997  if (!sawdot)
11998  return name;
11999  oname = psprintf("OPERATOR(%s)", name);
12000  free(name);
12001  return oname;
12002 }
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:60
#define NULL
Definition: c.h:226
const char * name
Definition: encode.c:521
static char * convertRegProcReference ( Archive fout,
const char *  proc 
)
static

Definition at line 11932 of file pg_dump.c.

References name, NULL, and pg_strdup().

Referenced by dumpOpr().

11933 {
11934  char *name;
11935  char *paren;
11936  bool inquote;
11937 
11938  /* In all cases "-" means a null reference */
11939  if (strcmp(proc, "-") == 0)
11940  return NULL;
11941 
11942  name = pg_strdup(proc);
11943  /* find non-double-quoted left paren */
11944  inquote = false;
11945  for (paren = name; *paren; paren++)
11946  {
11947  if (*paren == '(' && !inquote)
11948  {
11949  *paren = '\0';
11950  break;
11951  }
11952  if (*paren == '"')
11953  inquote = !inquote;
11954  }
11955  return name;
11956 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
const char * name
Definition: encode.c:521
static char * convertTSFunction ( Archive fout,
Oid  funcOid 
)
static

Definition at line 12013 of file pg_dump.c.

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

Referenced by dumpTSParser(), and dumpTSTemplate().

12014 {
12015  char *result;
12016  char query[128];
12017  PGresult *res;
12018 
12019  snprintf(query, sizeof(query),
12020  "SELECT '%u'::pg_catalog.regproc", funcOid);
12021  res = ExecuteSqlQueryForSingleRow(fout, query);
12022 
12023  result = pg_strdup(PQgetvalue(res, 0, 0));
12024 
12025  PQclear(res);
12026 
12027  return result;
12028 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
void PQclear(PGresult *res)
Definition: fe-exec.c:650
static DumpableObject * createBoundaryObjects ( void  )
static

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

17060 {
17061  DumpableObject *dobjs;
17062 
17063  dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
17064 
17065  dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
17066  dobjs[0].catId = nilCatalogId;
17067  AssignDumpId(dobjs + 0);
17068  dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
17069 
17070  dobjs[1].objType = DO_POST_DATA_BOUNDARY;
17071  dobjs[1].catId = nilCatalogId;
17072  AssignDumpId(dobjs + 1);
17073  dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
17074 
17075  return dobjs;
17076 }
char * name
Definition: pg_dump.h:132
void AssignDumpId(DumpableObject *dobj)
Definition: common.c:509
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:130
static const CatalogId nilCatalogId
Definition: pg_dump.c:130
DumpableObjectType objType
Definition: pg_dump.h:129
static PQExpBuffer createDummyViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

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

14676 {
14677  PQExpBuffer result = createPQExpBuffer();
14678  int j;
14679 
14680  appendPQExpBufferStr(result, "SELECT");
14681 
14682  for (j = 0; j < tbinfo->numatts; j++)
14683  {
14684  if (j > 0)
14685  appendPQExpBufferChar(result, ',');
14686  appendPQExpBufferStr(result, "\n ");
14687 
14688  appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
14689 
14690  /*
14691  * Must add collation if not default for the type, because CREATE OR
14692  * REPLACE VIEW won't change it
14693  */
14694  if (OidIsValid(tbinfo->attcollation[j]))
14695  {
14696  CollInfo *coll;
14697 
14698  coll = findCollationByOid(tbinfo->attcollation[j]);
14699  if (coll)
14700  {
14701  /* always schema-qualify, don't try to be smart */
14702  appendPQExpBuffer(result, " COLLATE %s.",
14703  fmtId(coll->dobj.namespace->dobj.name));
14704  appendPQExpBufferStr(result, fmtId(coll->dobj.name));
14705  }
14706  }
14707 
14708  appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
14709  }
14710 
14711  return result;
14712 }
char * name
Definition: pg_dump.h:132
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:302
char ** attnames
Definition: pg_dump.h:301
#define OidIsValid(objectId)
Definition: c.h:534
DumpableObject dobj
Definition: pg_dump.h:245
Oid * attcollation
Definition: pg_dump.h:312
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
int numatts
Definition: pg_dump.h:300
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:836
static PQExpBuffer createViewAsClause ( Archive fout,
TableInfo tbinfo 
)
static

Definition at line 14626 of file pg_dump.c.

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

Referenced by dumpRule(), and dumpTableSchema().

14627 {
14628  PQExpBuffer query = createPQExpBuffer();
14629  PQExpBuffer result = createPQExpBuffer();
14630  PGresult *res;
14631  int len;
14632 
14633  /* Fetch the view definition */
14634  appendPQExpBuffer(query,
14635  "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
14636  tbinfo->dobj.catId.oid);
14637 
14638  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14639 
14640  if (PQntuples(res) != 1)
14641  {
14642  if (PQntuples(res) < 1)
14643  exit_horribly(NULL, "query to obtain definition of view \"%s\" returned no data\n",
14644  tbinfo->dobj.name);
14645  else
14646  exit_horribly(NULL, "query to obtain definition of view \"%s\" returned more than one definition\n",
14647  tbinfo->dobj.name);
14648  }
14649 
14650  len = PQgetlength(res, 0, 0);
14651 
14652  if (len == 0)
14653  exit_horribly(NULL, "definition of view \"%s\" appears to be empty (length zero)\n",
14654  tbinfo->dobj.name);
14655 
14656  /* Strip off the trailing semicolon so that other things may follow. */
14657  Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
14658  appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
14659 
14660  PQclear(res);
14661  destroyPQExpBuffer(query);
14662 
14663  return result;
14664 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3078
char * name
Definition: pg_dump.h:132
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
DumpableObject dobj
Definition: pg_dump.h:260
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:650
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:130
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:409
static void dumpAccessMethod ( Archive fout,
AccessMethodInfo oprinfo 
)
static

Definition at line 12035 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, NULL, pg_free(), pg_strdup(), SECTION_PRE_DATA, and write_msg().

Referenced by dumpDumpableObject().

12036 {
12037  DumpOptions *dopt = fout->dopt;
12038  PQExpBuffer q;
12039  PQExpBuffer delq;
12040  PQExpBuffer labelq;
12041  char *qamname;
12042 
12043  /* Skip if not to be dumped */
12044  if (!aminfo->dobj.dump || dopt->dataOnly)
12045  return;
12046 
12047  q = createPQExpBuffer();
12048  delq = createPQExpBuffer();
12049  labelq = createPQExpBuffer();
12050 
12051  qamname = pg_strdup(fmtId(aminfo->dobj.name));
12052 
12053  appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12054 
12055  switch (aminfo->amtype)
12056  {
12057  case AMTYPE_INDEX:
12058  appendPQExpBuffer(q, "TYPE INDEX ");
12059  break;
12060  default:
12061  write_msg(NULL, "WARNING: invalid type \"%c\" of access method \"%s\"\n",
12062  aminfo->amtype, qamname);
12063  pg_free(qamname);
12064  destroyPQExpBuffer(q);
12065  destroyPQExpBuffer(delq);
12066  destroyPQExpBuffer(labelq);
12067  return;
12068  }
12069 
12070  appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12071 
12072  appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12073  qamname);
12074 
12075  appendPQExpBuffer(labelq, "ACCESS METHOD %s",
12076  qamname);
12077 
12078  if (dopt->binary_upgrade)
12079  binary_upgrade_extension_member(q, &aminfo->dobj, labelq->data);
12080 
12081  if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12082  ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12083  aminfo->dobj.name,
12084  NULL,
12085  NULL,
12086  "",
12087  false, "ACCESS METHOD", SECTION_PRE_DATA,
12088  q->data, delq->data, NULL,
12089  NULL, 0,
12090  NULL, NULL);
12091 
12092  /* Dump Access Method Comments */
12093  if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12094  dumpComment(fout, labelq->data,
12095  NULL, "",
12096  aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12097 
12098  pg_free(qamname);
12099 
12100  destroyPQExpBuffer(q);
12101  destroyPQExpBuffer(delq);
12102  destroyPQExpBuffer(labelq);
12103 }
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:133
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
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:93
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:3897
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
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)
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 14128 of file pg_dump.c.

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

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

14133 {
14134  DumpOptions *dopt = fout->dopt;
14135  PQExpBuffer sql;
14136 
14137  /* Do nothing if ACL dump is not enabled */
14138  if (dopt->aclsSkip)
14139  return;
14140 
14141  /* --data-only skips ACLs *except* BLOB ACLs */
14142  if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14143  return;
14144 
14145  sql = createPQExpBuffer();
14146 
14147  /*
14148  * Check to see if this object has had any initial ACLs included for it.
14149  * If so, we are in binary upgrade mode and these are the ACLs to turn
14150  * into GRANT and REVOKE statements to set and record the initial
14151  * privileges for an extension object. Let the backend know that these
14152  * are to be recorded by calling binary_upgrade_set_record_init_privs()
14153  * before and after.
14154  */
14155  if (strlen(initacls) != 0 || strlen(initracls) != 0)
14156  {
14157  appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14158  if (!buildACLCommands(name, subname, type, initacls, initracls, owner,
14159  "", fout->remoteVersion, sql))
14161  "could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14162  initacls, initracls, name, type);
14163  appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14164  }
14165 
14166  if (!buildACLCommands(name, subname, type, acls, racls, owner,
14167  "", fout->remoteVersion, sql))
14169  "could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14170  acls, racls, name, type);
14171 
14172  if (sql->len > 0)
14174  tag, nspname,
14175  NULL,
14176  owner ? owner : "",
14177  false, "ACL", SECTION_NONE,
14178  sql->data, "", NULL,
14179  &(objDumpId), 1,
14180  NULL, NULL);
14181 
14182  destroyPQExpBuffer(sql);
14183 }
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
bool aclsSkip
Definition: pg_backup.h:139
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define NULL
Definition: c.h:226
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:130
DumpId createDumpId(void)
Definition: common.c:553
int remoteVersion
Definition: pg_backup.h:183
static void dumpAgg ( Archive fout,
AggInfo agginfo 
)
static

Definition at line 12985 of file pg_dump.c.

References _aggInfo::aggfn, 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, NULL, 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().

12986 {
12987  DumpOptions *dopt = fout->dopt;
12988  PQExpBuffer query;
12989  PQExpBuffer q;
12990  PQExpBuffer delq;
12991  PQExpBuffer labelq;
12992  PQExpBuffer details;
12993  char *aggsig; /* identity signature */
12994  char *aggfullsig = NULL; /* full signature */
12995  char *aggsig_tag;
12996  PGresult *res;
12997  int i_aggtransfn;
12998  int i_aggfinalfn;
12999  int i_aggcombinefn;
13000  int i_aggserialfn;
13001  int i_aggdeserialfn;
13002  int i_aggmtransfn;
13003  int i_aggminvtransfn;
13004  int i_aggmfinalfn;
13005  int i_aggfinalextra;
13006  int i_aggmfinalextra;
13007  int i_aggsortop;
13008  int i_hypothetical;
13009  int i_aggtranstype;
13010  int i_aggtransspace;
13011  int i_aggmtranstype;
13012  int i_aggmtransspace;
13013  int i_agginitval;
13014  int i_aggminitval;
13015  int i_convertok;
13016  int i_proparallel;
13017  const char *aggtransfn;
13018  const char *aggfinalfn;
13019  const char *aggcombinefn;
13020  const char *aggserialfn;
13021  const char *aggdeserialfn;
13022  const char *aggmtransfn;
13023  const char *aggminvtransfn;
13024  const char *aggmfinalfn;
13025  bool aggfinalextra;
13026  bool aggmfinalextra;
13027  const char *aggsortop;
13028  char *aggsortconvop;
13029  bool hypothetical;
13030  const char *aggtranstype;
13031  const char *aggtransspace;
13032  const char *aggmtranstype;
13033  const char *aggmtransspace;
13034  const char *agginitval;
13035  const char *aggminitval;
13036  bool convertok;
13037  const char *proparallel;
13038 
13039  /* Skip if not to be dumped */
13040  if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13041  return;
13042 
13043  query = createPQExpBuffer();
13044  q = createPQExpBuffer();
13045  delq = createPQExpBuffer();
13046  labelq = createPQExpBuffer();
13047  details = createPQExpBuffer();
13048 
13049  /* Make sure we are in proper schema */
13050  selectSourceSchema(fout, agginfo->aggfn.dobj.namespace->dobj.name);
13051 
13052  /* Get aggregate-specific details */
13053  if (fout->remoteVersion >= 90600)
13054  {
13055  appendPQExpBuffer(query, "SELECT aggtransfn, "
13056  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13057  "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13058  "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13059  "aggfinalextra, aggmfinalextra, "
13060  "aggsortop::pg_catalog.regoperator, "
13061  "(aggkind = 'h') AS hypothetical, "
13062  "aggtransspace, agginitval, "
13063  "aggmtransspace, aggminitval, "
13064  "true AS convertok, "
13065  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13066  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13067  "p.proparallel "
13068  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13069  "WHERE a.aggfnoid = p.oid "
13070  "AND p.oid = '%u'::pg_catalog.oid",
13071  agginfo->aggfn.dobj.catId.oid);
13072  }
13073  else if (fout->remoteVersion >= 90400)
13074  {
13075  appendPQExpBuffer(query, "SELECT aggtransfn, "
13076  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13077  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13078  "'-' AS aggdeserialfn, aggmtransfn, aggminvtransfn, "
13079  "aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13080  "aggfinalextra, aggmfinalextra, "
13081  "aggsortop::pg_catalog.regoperator, "
13082  "(aggkind = 'h') AS hypothetical, "
13083  "aggtransspace, agginitval, "
13084  "aggmtransspace, aggminitval, "
13085  "true AS convertok, "
13086  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13087  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13088  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13089  "WHERE a.aggfnoid = p.oid "
13090  "AND p.oid = '%u'::pg_catalog.oid",
13091  agginfo->aggfn.dobj.catId.oid);
13092  }
13093  else if (fout->remoteVersion >= 80400)
13094  {
13095  appendPQExpBuffer(query, "SELECT aggtransfn, "
13096  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13097  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13098  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13099  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13100  "0 AS aggmtranstype, false AS aggfinalextra, "
13101  "false AS aggmfinalextra, "
13102  "aggsortop::pg_catalog.regoperator, "
13103  "false AS hypothetical, "
13104  "0 AS aggtransspace, agginitval, "
13105  "0 AS aggmtransspace, NULL AS aggminitval, "
13106  "true AS convertok, "
13107  "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13108  "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13109  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13110  "WHERE a.aggfnoid = p.oid "
13111  "AND p.oid = '%u'::pg_catalog.oid",
13112  agginfo->aggfn.dobj.catId.oid);
13113  }
13114  else if (fout->remoteVersion >= 80100)
13115  {
13116  appendPQExpBuffer(query, "SELECT aggtransfn, "
13117  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13118  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13119  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13120  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13121  "0 AS aggmtranstype, false AS aggfinalextra, "
13122  "false AS aggmfinalextra, "
13123  "aggsortop::pg_catalog.regoperator, "
13124  "false AS hypothetical, "
13125  "0 AS aggtransspace, agginitval, "
13126  "0 AS aggmtransspace, NULL AS aggminitval, "
13127  "true AS convertok "
13128  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13129  "WHERE a.aggfnoid = p.oid "
13130  "AND p.oid = '%u'::pg_catalog.oid",
13131  agginfo->aggfn.dobj.catId.oid);
13132  }
13133  else
13134  {
13135  appendPQExpBuffer(query, "SELECT aggtransfn, "
13136  "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13137  "'-' AS aggcombinefn, '-' AS aggserialfn, "
13138  "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13139  "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13140  "0 AS aggmtranstype, false AS aggfinalextra, "
13141  "false AS aggmfinalextra, 0 AS aggsortop, "
13142  "false AS hypothetical, "
13143  "0 AS aggtransspace, agginitval, "
13144  "0 AS aggmtransspace, NULL AS aggminitval, "
13145  "true AS convertok "
13146  "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13147  "WHERE a.aggfnoid = p.oid "
13148  "AND p.oid = '%u'::pg_catalog.oid",
13149  agginfo->aggfn.dobj.catId.oid);
13150  }
13151 
13152  res = ExecuteSqlQueryForSingleRow(fout, query->data);
13153 
13154  i_aggtransfn = PQfnumber(res, "aggtransfn");
13155  i_aggfinalfn = PQfnumber(res, "aggfinalfn");
13156  i_aggcombinefn = PQfnumber(res, "aggcombinefn");
13157  i_aggserialfn = PQfnumber(res, "aggserialfn");
13158  i_aggdeserialfn = PQfnumber(res, "aggdeserialfn");
13159  i_aggmtransfn = PQfnumber(res, "aggmtransfn");
13160  i_aggminvtransfn = PQfnumber(res, "aggminvtransfn");
13161  i_aggmfinalfn = PQfnumber(res, "aggmfinalfn");
13162  i_aggfinalextra = PQfnumber(res, "aggfinalextra");
13163  i_aggmfinalextra = PQfnumber(res, "aggmfinalextra");
13164  i_aggsortop = PQfnumber(res, "aggsortop");
13165  i_hypothetical = PQfnumber(res, "hypothetical");
13166  i_aggtranstype = PQfnumber(res, "aggtranstype");
13167  i_aggtransspace = PQfnumber(res, "aggtransspace");
13168  i_aggmtranstype = PQfnumber(res, "aggmtranstype");
13169  i_aggmtransspace = PQfnumber(res, "aggmtransspace");
13170  i_agginitval = PQfnumber(res, "agginitval");
13171  i_aggminitval = PQfnumber(res, "aggminitval");
13172  i_convertok = PQfnumber(res, "convertok");
13173  i_proparallel = PQfnumber(res, "proparallel");
13174 
13175  aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
13176  aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
13177  aggcombinefn = PQgetvalue(res, 0, i_aggcombinefn);
13178  aggserialfn = PQgetvalue(res, 0, i_aggserialfn);
13179  aggdeserialfn = PQgetvalue(res, 0, i_aggdeserialfn);
13180  aggmtransfn = PQgetvalue(res, 0, i_aggmtransfn);
13181  aggminvtransfn = PQgetvalue(res, 0, i_aggminvtransfn);
13182  aggmfinalfn = PQgetvalue(res, 0, i_aggmfinalfn);
13183  aggfinalextra = (PQgetvalue(res, 0, i_aggfinalextra)[0] == 't');
13184  aggmfinalextra = (PQgetvalue(res, 0, i_aggmfinalextra)[0] == 't');
13185  aggsortop = PQgetvalue(res, 0, i_aggsortop);
13186  hypothetical = (PQgetvalue(res, 0, i_hypothetical)[0] == 't');
13187  aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
13188  aggtransspace = PQgetvalue(res, 0, i_aggtransspace);
13189  aggmtranstype = PQgetvalue(res, 0, i_aggmtranstype);
13190  aggmtransspace = PQgetvalue(res, 0, i_aggmtransspace);
13191  agginitval = PQgetvalue(res, 0, i_agginitval);
13192  aggminitval = PQgetvalue(res, 0, i_aggminitval);
13193  convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
13194 
13195  if (fout->remoteVersion >= 80400)
13196  {
13197  /* 8.4 or later; we rely on server-side code for most of the work */
13198  char *funcargs;
13199  char *funciargs;
13200 
13201  funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13202  funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13203  aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13204  aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13205  }
13206  else
13207  /* pre-8.4, do it ourselves */
13208  aggsig = format_aggregate_signature(agginfo, fout, true);
13209 
13210  aggsig_tag = format_aggregate_signature(agginfo, fout, false);
13211 
13212  if (i_proparallel != -1)
13213  proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13214  else
13215  proparallel = NULL;
13216 
13217  if (!convertok)
13218  {
13219  write_msg(NULL, "WARNING: aggregate function %s could not be dumped correctly for this database version; ignored\n",
13220  aggsig);
13221 
13222  if (aggfullsig)
13223  free(aggfullsig);
13224 
13225  free(aggsig);
13226 
13227  return;
13228  }
13229 
13230  /* regproc and regtype output is already sufficiently quoted */
13231  appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
13232  aggtransfn, aggtranstype);
13233 
13234  if (strcmp(aggtransspace, "0") != 0)
13235  {
13236  appendPQExpBuffer(details, ",\n SSPACE = %s",
13237  aggtransspace);
13238  }
13239 
13240  if (!PQgetisnull(res, 0, i_agginitval))
13241  {
13242  appendPQExpBufferStr(details, ",\n INITCOND = ");
13243  appendStringLiteralAH(details, agginitval, fout);
13244  }
13245 
13246  if (strcmp(aggfinalfn, "-") != 0)
13247  {
13248  appendPQExpBuffer(details, ",\n FINALFUNC = %s",
13249  aggfinalfn);
13250  if (aggfinalextra)
13251  appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA");
13252  }
13253 
13254  if (strcmp(aggcombinefn, "-") != 0)
13255  appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn);
13256 
13257  if (strcmp(aggserialfn, "-") != 0)
13258  appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn);
13259 
13260  if (strcmp(aggdeserialfn, "-") != 0)
13261  appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn);
13262 
13263  if (strcmp(aggmtransfn, "-") != 0)
13264  {
13265  appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s",
13266  aggmtransfn,
13267  aggminvtransfn,
13268  aggmtranstype);
13269  }
13270 
13271  if (strcmp(aggmtransspace, "0") != 0)
13272  {
13273  appendPQExpBuffer(details, ",\n MSSPACE = %s",
13274  aggmtransspace);
13275  }
13276 
13277  if (!PQgetisnull(res, 0, i_aggminitval))
13278  {
13279  appendPQExpBufferStr(details, ",\n MINITCOND = ");
13280  appendStringLiteralAH(details, aggminitval, fout);
13281  }
13282 
13283  if (strcmp(aggmfinalfn, "-") != 0)
13284  {
13285  appendPQExpBuffer(details, ",\n MFINALFUNC = %s",
13286  aggmfinalfn);
13287  if (aggmfinalextra)
13288  appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA");
13289  }
13290 
13291  aggsortconvop = convertOperatorReference(fout, aggsortop);
13292  if (aggsortconvop)
13293  {
13294  appendPQExpBuffer(details, ",\n SORTOP = %s",
13295  aggsortconvop);
13296  free(aggsortconvop);
13297  }
13298 
13299  if (hypothetical)
13300  appendPQExpBufferStr(details, ",\n HYPOTHETICAL");
13301 
13302  if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
13303  {
13304  if (proparallel[0] == PROPARALLEL_SAFE)
13305  appendPQExpBufferStr(details, ",\n PARALLEL = safe");
13306  else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13307  appendPQExpBufferStr(details, ",\n PARALLEL = restricted");
13308  else if (proparallel[0] != PROPARALLEL_UNSAFE)
13309  exit_horribly(NULL, "unrecognized proparallel value for function \"%s\"\n",
13310  agginfo->aggfn.dobj.name);
13311  }
13312 
13313  /*
13314  * DROP must be fully qualified in case same name appears in pg_catalog
13315  */
13316  appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
13317  fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13318  aggsig);
13319 
13320  appendPQExpBuffer(q, "CREATE AGGREGATE %s (\n%s\n);\n",
13321  aggfullsig ? aggfullsig : aggsig, details->data);
13322 
13323  appendPQExpBuffer(labelq, "AGGREGATE %s", aggsig);
13324 
13325  if (dopt->binary_upgrade)
13326  binary_upgrade_extension_member(q, &agginfo->aggfn.dobj, labelq->data);
13327 
13328  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
13329  ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
13330  agginfo->aggfn.dobj.dumpId,
13331  aggsig_tag,
13332  agginfo->aggfn.dobj.namespace->dobj.name,
13333  NULL,
13334  agginfo->aggfn.rolname,
13335  false, "AGGREGATE", SECTION_PRE_DATA,
13336  q->data, delq->data, NULL,
13337  NULL, 0,
13338  NULL, NULL);
13339 
13340  /* Dump Aggregate Comments */
13341  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
13342  dumpComment(fout, labelq->data,
13343  agginfo->aggfn.dobj.namespace->dobj.name,
13344  agginfo->aggfn.rolname,
13345  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13346 
13347  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
13348  dumpSecLabel(fout, labelq->data,
13349  agginfo->aggfn.dobj.namespace->dobj.name,
13350  agginfo->aggfn.rolname,
13351  agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13352 
13353  /*
13354  * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
13355  * command look like a function's GRANT; in particular this affects the
13356  * syntax for zero-argument aggregates and ordered-set aggregates.
13357  */
13358  free(aggsig);
13359  free(aggsig_tag);
13360 
13361  aggsig = format_function_signature(fout, &agginfo->aggfn, true);
13362  aggsig_tag = format_function_signature(fout, &agginfo->aggfn, false);
13363 
13364  if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
13365  dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
13366  "FUNCTION",
13367  aggsig, NULL, aggsig_tag,
13368  agginfo->aggfn.dobj.namespace->dobj.name,
13369  agginfo->aggfn.rolname, agginfo->aggfn.proacl,
13370  agginfo->aggfn.rproacl,
13371  agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
13372 
13373  free(aggsig);
13374  if (aggfullsig)
13375  free(aggfullsig);
13376  free(aggsig_tag);
13377 
13378  PQclear(res);
13379 
13380  destroyPQExpBuffer(query);
13381  destroyPQExpBuffer(q);
13382  destroyPQExpBuffer(delq);
13383  destroyPQExpBuffer(labelq);
13384  destroyPQExpBuffer(details);
13385 }
char * name
Definition: pg_dump.h:132
DumpComponents dump
Definition: pg_dump.h:133
static char * convertOperatorReference(Archive *fout, const char *opr)
Definition: pg_dump.c:11968
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:206
DumpableObject dobj
Definition: pg_dump.h:197
#define PROPARALLEL_RESTRICTED
Definition: pg_proc.h:5382
DumpId dumpId
Definition: pg_dump.h:131
static char * format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
Definition: pg_dump.c:10839
FuncInfo aggfn
Definition: pg_dump.h:212
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:133
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
static void dumpSecLabel(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:14204
#define PROPARALLEL_SAFE
Definition: pg_proc.h:5381
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
#define PROPARALLEL_UNSAFE
Definition: pg_proc.h:5383
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:3897
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:297
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define free(a)
Definition: header.h:60
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
char * initproacl
Definition: pg_dump.h:205
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:14128
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:10934
char * rolname
Definition: pg_dump.h:198
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:94
char * proacl
Definition: pg_dump.h:203
char * rproacl
Definition: pg_dump.h:204
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:95
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:130
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:3092
static char * format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
Definition: pg_dump.c:12947
int remoteVersion
Definition: pg_backup.h:183
static void dumpAttrDef ( Archive fout,
AttrDefInfo adinfo 
)
static

Definition at line 15417 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, NULL, psprintf(), _tableInfo::rolname, SECTION_PRE_DATA, and _attrDefInfo::separate.

Referenced by dumpDumpableObject().

15418 {
15419  DumpOptions *dopt = fout->dopt;
15420  TableInfo *tbinfo = adinfo->adtable;
15421  int adnum = adinfo->adnum;
15422  PQExpBuffer q;
15423  PQExpBuffer delq;
15424  char *tag;
15425 
15426  /* Skip if table definition not to be dumped */
15427  if (!tbinfo->dobj.dump || dopt->dataOnly)
15428  return;
15429 
15430  /* Skip if not "separate"; it was dumped in the table's definition */
15431  if (!adinfo->separate)
15432  return;
15433 
15434  q = createPQExpBuffer();
15435  delq = createPQExpBuffer();
15436 
15437  appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15438  fmtId(tbinfo->dobj.name));
15439  appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
15440  fmtId(tbinfo->attnames[adnum - 1]),
15441  adinfo->adef_expr);
15442 
15443  /*
15444  * DROP must be fully qualified in case same name appears in pg_catalog
15445  */
15446  appendPQExpBuffer(delq, "ALTER TABLE %s.",
15447  fmtId(tbinfo->dobj.namespace->dobj.name));
15448  appendPQExpBuffer(delq, "%s ",
15449  fmtId(tbinfo->dobj.name));
15450  appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
15451  fmtId(tbinfo->attnames[adnum - 1]));
15452 
15453  tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
15454 
15455  if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15456  ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
15457  tag,
15458  tbinfo->dobj.namespace->dobj.name,
15459  NULL,
15460  tbinfo->rolname,
15461  false, "DEFAULT", SECTION_PRE_DATA,
15462  q->data, delq->data, NULL,
15463  NULL, 0,
15464  NULL, NULL);
15465 
15466  free(tag);
15467  destroyPQExpBuffer(q);
15468  destroyPQExpBuffer(delq);
15469 }
char * name
Definition: pg_dump.h:132
DumpComponents dump
Definition: pg_dump.h:133
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:261
DumpId dumpId
Definition: pg_dump.h:131
char ** attnames
Definition: pg_dump.h:301
DumpableObject dobj
Definition: pg_dump.h:260
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
bool separate
Definition: pg_dump.h:338
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
TableInfo * adtable
Definition: pg_dump.h:335
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
DumpableObject dobj
Definition: pg_dump.h:334
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
CatalogId catId
Definition: pg_dump.h:130
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:337
static void dumpBaseType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 9828 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, NULL, 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().

9829 {
9830  DumpOptions *dopt = fout->dopt;
9832  PQExpBuffer delq = createPQExpBuffer();
9833  PQExpBuffer labelq = createPQExpBuffer();
9834  PQExpBuffer query = createPQExpBuffer();
9835  PGresult *res;
9836  char *qtypname;
9837  char *typlen;
9838  char *typinput;
9839  char *typoutput;
9840  char *typreceive;
9841  char *typsend;
9842  char *typmodin;
9843  char *typmodout;
9844  char *typanalyze;
9845  Oid typreceiveoid;
9846  Oid typsendoid;
9847  Oid typmodinoid;
9848  Oid typmodoutoid;
9849  Oid typanalyzeoid;
9850  char *typcategory;
9851  char *typispreferred;
9852  char *typdelim;
9853  char *typbyval;
9854  char *typalign;
9855  char *typstorage;
9856  char *typcollatable;
9857  char *typdefault;
9858  bool typdefault_is_literal = false;
9859 
9860  /* Set proper schema search path so regproc references list correctly */
9861  selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
9862 
9863  /* Fetch type-specific details */
9864  if (fout->remoteVersion >= 90100)
9865  {
9866  appendPQExpBuffer(query, "SELECT typlen, "
9867  "typinput, typoutput, typreceive, typsend, "
9868  "typmodin, typmodout, typanalyze, "
9869  "typreceive::pg_catalog.oid AS typreceiveoid, "
9870  "typsend::pg_catalog.oid AS typsendoid, "
9871  "typmodin::pg_catalog.oid AS typmodinoid, "
9872  "typmodout::pg_catalog.oid AS typmodoutoid, "
9873  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
9874  "typcategory, typispreferred, "
9875  "typdelim, typbyval, typalign, typstorage, "
9876  "(typcollation <> 0) AS typcollatable, "
9877  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
9878  "FROM pg_catalog.pg_type "
9879  "WHERE oid = '%u'::pg_catalog.oid",
9880  tyinfo->dobj.catId.oid);
9881  }
9882  else if (fout->remoteVersion >= 80400)
9883  {
9884  appendPQExpBuffer(query, "SELECT typlen, "
9885  "typinput, typoutput, typreceive, typsend, "
9886  "typmodin, typmodout, typanalyze, "
9887  "typreceive::pg_catalog.oid AS typreceiveoid, "
9888  "typsend::pg_catalog.oid AS typsendoid, "
9889  "typmodin::pg_catalog.oid AS typmodinoid, "
9890  "typmodout::pg_catalog.oid AS typmodoutoid, "
9891  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
9892  "typcategory, typispreferred, "
9893  "typdelim, typbyval, typalign, typstorage, "
9894  "false AS typcollatable, "
9895  "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
9896  "FROM pg_catalog.pg_type "
9897  "WHERE oid = '%u'::pg_catalog.oid",
9898  tyinfo->dobj.catId.oid);
9899  }
9900  else if (fout->remoteVersion >= 80300)
9901  {
9902  /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
9903  appendPQExpBuffer(query, "SELECT typlen, "
9904  "typinput, typoutput, typreceive, typsend, "
9905  "typmodin, typmodout, typanalyze, "
9906  "typreceive::pg_catalog.oid AS typreceiveoid, "
9907  "typsend::pg_catalog.oid AS typsendoid, "
9908  "typmodin::pg_catalog.oid AS typmodinoid, "
9909  "typmodout::pg_catalog.oid AS typmodoutoid, "
9910  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
9911  "'U' AS typcategory, false AS typispreferred, "
9912  "typdelim, typbyval, typalign, typstorage, "
9913  "false AS typcollatable, "
9914  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
9915  "FROM pg_catalog.pg_type "
9916  "WHERE oid = '%u'::pg_catalog.oid",
9917  tyinfo->dobj.catId.oid);
9918  }
9919  else
9920  {
9921  appendPQExpBuffer(query, "SELECT typlen, "
9922  "typinput, typoutput, typreceive, typsend, "
9923  "'-' AS typmodin, '-' AS typmodout, "
9924  "typanalyze, "
9925  "typreceive::pg_catalog.oid AS typreceiveoid, "
9926  "typsend::pg_catalog.oid AS typsendoid, "
9927  "0 AS typmodinoid, 0 AS typmodoutoid, "
9928  "typanalyze::pg_catalog.oid AS typanalyzeoid, "
9929  "'U' AS typcategory, false AS typispreferred, "
9930  "typdelim, typbyval, typalign, typstorage, "
9931  "false AS typcollatable, "
9932  "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
9933  "FROM pg_catalog.pg_type "
9934  "WHERE oid = '%u'::pg_catalog.oid",
9935  tyinfo->dobj.catId.oid);
9936  }
9937 
9938  res = ExecuteSqlQueryForSingleRow(fout, query->data);
9939 
9940  typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
9941  typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
9942  typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
9943  typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
9944  typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
9945  typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
9946  typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
9947  typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
9948  typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
9949  typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
9950  typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
9951  typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
9952  typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
9953  typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
9954  typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
9955  typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
9956  typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
9957  typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
9958  typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
9959  typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
9960  if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
9961  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
9962  else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
9963  {
9964  typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
9965  typdefault_is_literal = true; /* it needs quotes */
9966  }
9967  else
9968  typdefault = NULL;
9969 
9970  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
9971 
9972  /*
9973  * DROP must be fully qualified in case same name appears in pg_catalog.
9974  * The reason we include CASCADE is that the circular dependency between
9975  * the type and its I/O functions makes it impossible to drop the type any
9976  * other way.
9977  */
9978  appendPQExpBuffer(delq, "DROP TYPE %s.",
9979  fmtId(tyinfo->dobj.namespace->dobj.name));
9980  appendPQExpBuffer(delq, "%s CASCADE;\n",
9981  qtypname);
9982 
9983  /* We might already have a shell type, but setting pg_type_oid is harmless */
9984  if (dopt->binary_upgrade)
9986  tyinfo->dobj.catId.oid);
9987 
9989  "CREATE TYPE %s (\n"
9990  " INTERNALLENGTH = %s",
9991  qtypname,
9992  (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
9993 
9994  /* regproc result is sufficiently quoted already */
9995  appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
9996  appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
9997  if (OidIsValid(typreceiveoid))
9998  appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
9999  if (OidIsValid(typsendoid))
10000  appendPQExpBuffer(q, ",\n SEND = %s", typsend);
10001  if (OidIsValid(typmodinoid))
10002  appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
10003  if (OidIsValid(typmodoutoid))
10004  appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
10005  if (OidIsValid(typanalyzeoid))
10006  appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
10007 
10008  if (strcmp(typcollatable, "t") == 0)
10009  appendPQExpBufferStr(q, ",\n COLLATABLE = true");
10010 
10011  if (typdefault != NULL)
10012  {
10013  appendPQExpBufferStr(q, ",\n DEFAULT = ");
10014  if (typdefault_is_literal)
10015  appendStringLiteralAH(q, typdefault, fout);
10016  else
10017  appendPQExpBufferStr(q, typdefault);
10018  }
10019 
10020  if (OidIsValid(tyinfo->typelem))
10021  {
10022  char *elemType;
10023 
10024  /* reselect schema in case changed by function dump */
10025  selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10026  elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroAsOpaque);
10027  appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
10028  free(elemType);
10029  }
10030 
10031  if (strcmp(typcategory, "U") != 0)
10032  {
10033  appendPQExpBufferStr(q, ",\n CATEGORY = ");
10034  appendStringLiteralAH(q, typcategory, fout);
10035  }
10036 
10037  if (strcmp(typispreferred, "t") == 0)
10038  appendPQExpBufferStr(q, ",\n PREFERRED = true");
10039 
10040  if (typdelim && strcmp(typdelim, ",") != 0)
10041  {
10042  appendPQExpBufferStr(q, ",\n DELIMITER = ");
10043  appendStringLiteralAH(q, typdelim, fout);
10044  }
10045 
10046  if (strcmp(typalign, "c") == 0)
10047  appendPQExpBufferStr(q, ",\n ALIGNMENT = char");
10048  else if (strcmp(typalign, "s") == 0)
10049  appendPQExpBufferStr(q, ",\n ALIGNMENT = int2");
10050  else if (strcmp(typalign, "i") == 0)
10051  appendPQExpBufferStr(q, ",\n ALIGNMENT = int4");
10052  else if (strcmp(typalign, "d") == 0)
10053  appendPQExpBufferStr(q, ",\n ALIGNMENT = double");
10054 
10055  if (strcmp(typstorage, "p") == 0)
10056  appendPQExpBufferStr(q, ",\n STORAGE = plain");
10057  else if (strcmp(typstorage, "e") == 0)
10058  appendPQExpBufferStr(q, ",\n STORAGE = external");
10059  else if (strcmp(typstorage, "x") == 0)
10060  appendPQExpBufferStr(q, ",\n STORAGE = extended");
10061  else if (strcmp(typstorage, "m") == 0)
10062  appendPQExpBufferStr(q, ",\n STORAGE = main");
10063 
10064  if (strcmp(typbyval, "t") == 0)
10065  appendPQExpBufferStr(q, ",\n PASSEDBYVALUE");
10066 
10067  appendPQExpBufferStr(q, "\n);\n");
10068 
10069  appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10070 
10071  if (dopt->binary_upgrade)
10072  binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10073 
10074  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10075  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10076  tyinfo->dobj.name,
10077  tyinfo->dobj.namespace->dobj.name,
10078  NULL,
10079  tyinfo->rolname, false,
10080  "TYPE", SECTION_PRE_DATA,
10081  q->data, delq->data, NULL,
10082  NULL, 0,
10083  NULL, NULL);
10084 
10085  /* Dump Type Comments and Security Labels */
10086  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10087  dumpComment(fout, labelq->data,
10088  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10089  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10090 
10091  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10092  dumpSecLabel(fout, labelq->data,
10093  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10094  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10095 
10096  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10097  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10098  qtypname, NULL, tyinfo->dobj.name,
10099  tyinfo->dobj.namespace->dobj.name,
10100  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10101  tyinfo->inittypacl, tyinfo->initrtypacl);
10102 
10103  PQclear(res);
10104  destroyPQExpBuffer(q);
10105  destroyPQExpBuffer(delq);
10106  destroyPQExpBuffer(labelq);
10107  destroyPQExpBuffer(query);
10108 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:17330
char * name
Definition: pg_dump.h:132
DumpComponents dump
Definition: pg_dump.h:133
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid)
Definition: pg_dump.c:3746
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
char * inittypacl
Definition: pg_dump.h:173
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:174
char * typacl
Definition: pg_dump.h:171
DumpId dumpId
Definition: pg_dump.h:131
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:133
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
DumpOptions * dopt
Definition: pg_backup.h:178
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:14204
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
Oid typelem
Definition: pg_dump.h:175
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
DumpableObject dobj
Definition: pg_dump.h:164
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:3897
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:297
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
char * rtypacl
Definition: pg_dump.h:172
char * rolname
Definition: pg_dump.h:170
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:14128
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:94
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:95
CatalogId catId
Definition: pg_dump.h:130
#define atooid(x)
Definition: lo.c:17
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:3092
int remoteVersion
Definition: pg_backup.h:183
static void dumpBlob ( Archive fout,
BlobInfo binfo 
)
static

Definition at line 2928 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, NULL, _blobInfo::rblobacl, resetPQExpBuffer(), _blobInfo::rolname, and SECTION_PRE_DATA.

Referenced by dumpDumpableObject().

2929 {
2930  PQExpBuffer cquery = createPQExpBuffer();
2931  PQExpBuffer dquery = createPQExpBuffer();
2932 
2933  appendPQExpBuffer(cquery,
2934  "SELECT pg_catalog.lo_create('%s');\n",
2935  binfo->dobj.name);
2936 
2937  appendPQExpBuffer(dquery,
2938  "SELECT pg_catalog.lo_unlink('%s');\n",
2939  binfo->dobj.name);
2940 
2941  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
2942  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
2943  binfo->dobj.name,
2944  NULL, NULL,
2945  binfo->rolname, false,
2946  "BLOB", SECTION_PRE_DATA,
2947  cquery->data, dquery->data, NULL,
2948  NULL, 0,
2949  NULL, NULL);
2950 
2951  /* set up tag for comment and/or ACL */
2952  resetPQExpBuffer(cquery);
2953  appendPQExpBuffer(cquery, "LARGE OBJECT %s", binfo->dobj.name);
2954 
2955  /* Dump comment if any */
2956  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
2957  dumpComment(fout, cquery->data,
2958  NULL, binfo->rolname,
2959  binfo->dobj.catId, 0, binfo->dobj.dumpId);
2960 
2961  /* Dump security label if any */
2962  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
2963  dumpSecLabel(fout, cquery->data,
2964  NULL, binfo->rolname,
2965  binfo->dobj.catId, 0, binfo->dobj.dumpId);
2966 
2967  /* Dump ACL if any */
2968  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
2969  dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
2970  binfo->dobj.name, NULL, cquery->data,
2971  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
2972  binfo->initblobacl, binfo->initrblobacl);
2973 
2974  destroyPQExpBuffer(cquery);
2975  destroyPQExpBuffer(dquery);
2976 }
char * name
Definition: pg_dump.h:132
DumpComponents dump
Definition: pg_dump.h:133
DumpId dumpId
Definition: pg_dump.h:131
char * initblobacl
Definition: pg_dump.h:553
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
DumpableObject dobj
Definition: pg_dump.h:549
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
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:14204
char * rolname
Definition: pg_dump.h:550
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define NULL
Definition: c.h:226
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:14128
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:94
char * blobacl
Definition: pg_dump.h:551
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:95
CatalogId catId
Definition: pg_dump.h:130
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:552
char * initrblobacl
Definition: pg_dump.h:554
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
static int dumpBlobs ( Archive fout,
void *  arg 
)
static

Definition at line 2983 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, NULL, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQgetvalue(), PQntuples(), Archive::remoteVersion, selectSourceSchema(), StartBlob(), write_msg(), and WriteData().

Referenced by dumpDumpableObject().

2984 {
2985  const char *blobQry;
2986  const char *blobFetchQry;
2987  PGconn *conn = GetConnection(fout);
2988  PGresult *res;
2989  char buf[LOBBUFSIZE];
2990  int ntups;
2991  int i;
2992  int cnt;
2993 
2994  if (g_verbose)
2995  write_msg(NULL, "saving large objects\n");
2996 
2997  /* Make sure we are in proper schema */
2998  selectSourceSchema(fout, "pg_catalog");
2999 
3000  /*
3001  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3002  * the already-in-memory dumpable objects instead...
3003  */
3004  if (fout->remoteVersion >= 90000)
3005  blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_largeobject_metadata";
3006  else
3007  blobQry = "DECLARE bloboid CURSOR FOR SELECT DISTINCT loid FROM pg_largeobject";
3008 
3009  ExecuteSqlStatement(fout, blobQry);
3010 
3011  /* Command to fetch from cursor */
3012  blobFetchQry = "FETCH 1000 IN bloboid";
3013 
3014  do
3015  {
3016  /* Do a fetch */
3017  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3018 
3019  /* Process the tuples, if any */
3020  ntups = PQntuples(res);
3021  for (i = 0; i < ntups; i++)
3022  {
3023  Oid blobOid;
3024  int loFd;
3025 
3026  blobOid = atooid(PQgetvalue(res, i, 0));
3027  /* Open the BLOB */
3028  loFd = lo_open(conn, blobOid, INV_READ);
3029  if (loFd == -1)
3030  exit_horribly(NULL, "could not open large object %u: %s",
3031  blobOid, PQerrorMessage(conn));
3032 
3033  StartBlob(fout, blobOid);
3034 
3035  /* Now read it in chunks, sending data to archive */
3036  do
3037  {
3038  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3039  if (cnt < 0)
3040  exit_horribly(NULL, "error reading large object %u: %s",
3041  blobOid, PQerrorMessage(conn));
3042 
3043  WriteData(fout, buf, cnt);
3044  } while (cnt > 0);
3045 
3046  lo_close(conn, loFd);
3047 
3048  EndBlob(fout, blobOid);
3049  }
3050 
3051  PQclear(res);
3052  } while (ntups > 0);
3053 
3054  return 1;
3055 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
#define INV_READ
Definition: libpq-fs.h:22
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:100
int StartBlob(Archive *AH, Oid oid)
PGconn * conn
Definition: streamutil.c:42
static char * buf
Definition: pg_test_fsync.c:65
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:155
void WriteData(Archive *AH, const void *data, size_t dLen)
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:58
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:97
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:397
void exit_horribly(const char *modulename, const char *fmt,...)
#define atooid(x)
Definition: lo.c:17
int EndBlob(Archive *AH, Oid oid)
int i
#define LOBBUFSIZE
bool g_verbose
Definition: pg_dump.c:90
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:409
int remoteVersion
Definition: pg_backup.h:183
static void dumpCast ( Archive fout,
CastInfo cast 
)
static

Definition at line 11470 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, NULL, OidIsValid, SECTION_PRE_DATA, selectSourceSchema(), write_msg(), and zeroAsNone.

Referenced by dumpDumpableObject().

11471 {
11472  DumpOptions *dopt = fout->dopt;
11473  PQExpBuffer defqry;
11474  PQExpBuffer delqry;
11475  PQExpBuffer labelq;
11476  FuncInfo *funcInfo = NULL;
11477  char *sourceType;
11478  char *targetType;
11479 
11480  /* Skip if not to be dumped */
11481  if (!cast->dobj.dump || dopt->dataOnly)
11482  return;
11483 
11484  /* Cannot dump if we don't have the cast function's info */
11485  if (OidIsValid(cast->castfunc))
11486  {
11487  funcInfo = findFuncByOid(cast->castfunc);
11488  if (funcInfo == NULL)
11489  exit_horribly(NULL, "could not find function definition for function with OID %u\n",
11490  cast->castfunc);
11491  }
11492 
11493  /*
11494  * Make sure we are in proper schema (needed for getFormattedTypeName).
11495  * Casts don't have a schema of their own, so use pg_catalog.
11496  */
11497  selectSourceSchema(fout, "pg_catalog");
11498 
11499  defqry = createPQExpBuffer();
11500  delqry = createPQExpBuffer();
11501  labelq = createPQExpBuffer();
11502 
11503  sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
11504  targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
11505  appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
11506  sourceType, targetType);
11507 
11508  appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
11509  sourceType, targetType);
11510 
11511  switch (cast->castmethod)
11512  {
11514  appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
11515  break;
11516  case COERCION_METHOD_INOUT:
11517  appendPQExpBufferStr(defqry, "WITH INOUT");
11518  break;
11520  if (funcInfo)
11521  {
11522  char *fsig = format_function_signature(fout, funcInfo, true);
11523 
11524  /*
11525  * Always qualify the function name, in case it is not in
11526  * pg_catalog schema (format_function_signature won't qualify
11527  * it).
11528  */
11529  appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
11530  fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
11531  free(fsig);
11532  }
11533  else
11534  write_msg(NULL, "WARNING: bogus value in pg_cast.castfunc or pg_cast.castmethod field\n");
11535  break;
11536  default:
11537  write_msg(NULL, "WARNING: bogus value in pg_cast.castmethod field\n");
11538  }
11539 
11540  if (cast->castcontext == 'a')
11541  appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
11542  else if (cast->castcontext == 'i')
11543  appendPQExpBufferStr(defqry, " AS IMPLICIT");
11544  appendPQExpBufferStr(defqry, ";\n");
11545 
11546  appendPQExpBuffer(labelq, "CAST (%s AS %s)",
11547  sourceType, targetType);
11548 
11549  if (dopt->binary_upgrade)
11550  binary_upgrade_extension_member(defqry, &cast->dobj, labelq->data);
11551 
11552  if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
11553  ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
11554  labelq->data,
11555  "pg_catalog", NULL, "",
11556  false, "CAST", SECTION_PRE_DATA,
11557  defqry->data, delqry->data, NULL,
11558  NULL, 0,
11559  NULL, NULL);
11560 
11561  /* Dump Cast Comments */
11562  if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
11563  dumpComment(fout, labelq->data,
11564  NULL, "",
11565  cast->dobj.catId, 0, cast->dobj.dumpId);
11566 
11567  free(sourceType);
11568  free(targetType);
11569 
11570  destroyPQExpBuffer(defqry);
11571  destroyPQExpBuffer(delqry);
11572  destroyPQExpBuffer(labelq);
11573 }
static char * getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
Definition: pg_dump.c:17330
char * name
Definition: pg_dump.h:132
Oid castsource
Definition: pg_dump.h:445
DumpComponents dump
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
DumpableObject dobj
Definition: pg_dump.h:197
DumpId dumpId
Definition: pg_dump.h:131
Oid castfunc
Definition: pg_dump.h:447
#define OidIsValid(objectId)
Definition: c.h:534
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:133
DumpableObject dobj
Definition: pg_dump.h:444
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
Oid casttarget
Definition: pg_dump.h:446
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
FuncInfo * findFuncByOid(Oid oid)
Definition: common.c:814
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:3897
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define free(a)
Definition: header.h:60
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
static char * format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
Definition: pg_dump.c:10934
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:130
char castmethod
Definition: pg_dump.h:449
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:448
static void dumpCollation ( Archive fout,
CollInfo collinfo 
)
static

Definition at line 12752 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(), fmtId(), _dumpableObject::name, NULL, CatalogId::oid, PQclear(), PQfnumber(), PQgetvalue(), _collInfo::rolname, SECTION_PRE_DATA, and selectSourceSchema().

Referenced by dumpDumpableObject().

12753 {
12754  DumpOptions *dopt = fout->dopt;
12755  PQExpBuffer query;
12756  PQExpBuffer q;
12757  PQExpBuffer delq;
12758  PQExpBuffer labelq;
12759  PGresult *res;
12760  int i_collcollate;
12761  int i_collctype;
12762  const char *collcollate;
12763  const char *collctype;
12764 
12765  /* Skip if not to be dumped */
12766  if (!collinfo->dobj.dump || dopt->dataOnly)
12767  return;
12768 
12769  query = createPQExpBuffer();
12770  q = createPQExpBuffer();
12771  delq = createPQExpBuffer();
12772  labelq = createPQExpBuffer();
12773 
12774  /* Make sure we are in proper schema */
12775  selectSourceSchema(fout, collinfo->dobj.namespace->dobj.name);
12776 
12777  /* Get collation-specific details */
12778  appendPQExpBuffer(query, "SELECT "
12779  "collcollate, "
12780  "collctype "
12781  "FROM pg_catalog.pg_collation c "
12782  "WHERE c.oid = '%u'::pg_catalog.oid",
12783  collinfo->dobj.catId.oid);
12784 
12785  res = ExecuteSqlQueryForSingleRow(fout, query->data);
12786 
12787  i_collcollate = PQfnumber(res, "collcollate");
12788  i_collctype = PQfnumber(res, "collctype");
12789 
12790  collcollate = PQgetvalue(res, 0, i_collcollate);
12791  collctype = PQgetvalue(res, 0, i_collctype);
12792 
12793  /*
12794  * DROP must be fully qualified in case same name appears in pg_catalog
12795  */
12796  appendPQExpBuffer(delq, "DROP COLLATION %s",
12797  fmtId(collinfo->dobj.namespace->dobj.name));
12798  appendPQExpBuffer(delq, ".%s;\n",
12799  fmtId(collinfo->dobj.name));
12800 
12801  appendPQExpBuffer(q, "CREATE COLLATION %s (lc_collate = ",
12802  fmtId(collinfo->dobj.name));
12803  appendStringLiteralAH(q, collcollate, fout);
12804  appendPQExpBufferStr(q, ", lc_ctype = ");
12805  appendStringLiteralAH(q, collctype, fout);
12806  appendPQExpBufferStr(q, ");\n");
12807 
12808  appendPQExpBuffer(labelq, "COLLATION %s", fmtId(collinfo->dobj.name));
12809 
12810  if (dopt->binary_upgrade)
12811  binary_upgrade_extension_member(q, &collinfo->dobj, labelq->data);
12812 
12813  if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12814  ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
12815  collinfo->dobj.name,
12816  collinfo->dobj.namespace->dobj.name,
12817  NULL,
12818  collinfo->rolname,
12819  false, "COLLATION", SECTION_PRE_DATA,
12820  q->data, delq->data, NULL,
12821  NULL, 0,
12822  NULL, NULL);
12823 
12824  /* Dump Collation Comments */
12825  if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12826  dumpComment(fout, labelq->data,
12827  collinfo->dobj.namespace->dobj.name, collinfo->rolname,
12828  collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
12829 
12830  PQclear(res);
12831 
12832  destroyPQExpBuffer(query);
12833  destroyPQExpBuffer(q);
12834  destroyPQExpBuffer(delq);
12835  destroyPQExpBuffer(labelq);
12836 }
char * name
Definition: pg_dump.h:132
DumpComponents dump
Definition: pg_dump.h:133
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:131
DumpableObject dobj
Definition: pg_dump.h:245
char * rolname
Definition: pg_dump.h:246
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:133
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:3897
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:297
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define NULL
Definition: c.h:226
CatalogId catId
Definition: pg_dump.h:130
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 void dumpComment ( Archive fout,
const char *  target,
const char *  namespace,
const char *  owner,
CatalogId  catalogId,
int  subid,
DumpId  dumpId 
)
static

Definition at line 8815 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ArchiveEntry(), createDumpId(), createPQExpBuffer(), PQExpBufferData::data, _dumpOptions::dataOnly, CommentItem::descr, destroyPQExpBuffer(), Archive::dopt, findComments(), NULL, 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(), dumpRangeType(), dumpRule(), dumpSequence(), dumpTableConstraintComment(), dumpTransform(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), and dumpUndefinedType().

8818 {
8819  DumpOptions *dopt = fout->dopt;
8820  CommentItem *comments;
8821  int ncomments;
8822 
8823  /* Comments are schema not data ... except blob comments are data */
8824  if (strncmp(target, "LARGE OBJECT ", 13) != 0)
8825  {
8826  if (dopt->dataOnly)
8827  return;
8828  }
8829  else
8830  {
8831  if (dopt->schemaOnly)
8832  return;
8833  }
8834 
8835  /* Search for comments associated with catalogId, using table */
8836  ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
8837  &comments);
8838 
8839  /* Is there one matching the subid? */
8840  while (ncomments > 0)
8841  {
8842  if (comments->objsubid == subid)
8843  break;
8844  comments++;
8845  ncomments--;
8846  }
8847 
8848  /* If a comment exists, build COMMENT ON statement */
8849  if (ncomments > 0)
8850  {
8851  PQExpBuffer query = createPQExpBuffer();
8852 
8853  appendPQExpBuffer(query, "COMMENT ON %s IS ", target);
8854  appendStringLiteralAH(query, comments->descr, fout);
8855  appendPQExpBufferStr(query, ";\n");
8856 
8857  /*
8858  * We mark comments as SECTION_NONE because they really belong in the
8859  * same section as their parent, whether that is pre-data or
8860  * post-data.
8861  */
8863  target, namespace, NULL, owner,
8864  false, "COMMENT", SECTION_NONE,
8865  query->data, "", NULL,
8866  &(dumpId), 1,
8867  NULL, NULL);
8868 
8869  destroyPQExpBuffer(query);
8870  }
8871 }
Oid tableoid
Definition: pg_backup.h:224
bool schemaOnly
Definition: pg_backup.h:136
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int objsubid
Definition: pg_dump.c:69
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:8969
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
const char * descr
Definition: pg_dump.c:66
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:297
#define NULL
Definition: c.h:226
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:130
DumpId createDumpId(void)
Definition: common.c:553
static void dumpCompositeType ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10303 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, NULL, 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().

10304 {
10305  DumpOptions *dopt = fout->dopt;
10307  PQExpBuffer dropped = createPQExpBuffer();
10308  PQExpBuffer delq = createPQExpBuffer();
10309  PQExpBuffer labelq = createPQExpBuffer();
10310  PQExpBuffer query = createPQExpBuffer();
10311  PGresult *res;
10312  char *qtypname;
10313  int ntups;
10314  int i_attname;
10315  int i_atttypdefn;
10316  int i_attlen;
10317  int i_attalign;
10318  int i_attisdropped;
10319  int i_attcollation;
10320  int i;
10321  int actual_atts;
10322 
10323  /* Set proper schema search path so type references list correctly */
10324  selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10325 
10326  /* Fetch type specific details */
10327  if (fout->remoteVersion >= 90100)
10328  {
10329  /*
10330  * attcollation is new in 9.1. Since we only want to dump COLLATE
10331  * clauses for attributes whose collation is different from their
10332  * type's default, we use a CASE here to suppress uninteresting
10333  * attcollations cheaply. atttypid will be 0 for dropped columns;
10334  * collation does not matter for those.
10335  */
10336  appendPQExpBuffer(query, "SELECT a.attname, "
10337  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10338  "a.attlen, a.attalign, a.attisdropped, "
10339  "CASE WHEN a.attcollation <> at.typcollation "
10340  "THEN a.attcollation ELSE 0 END AS attcollation "
10341  "FROM pg_catalog.pg_type ct "
10342  "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
10343  "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
10344  "WHERE ct.oid = '%u'::pg_catalog.oid "
10345  "ORDER BY a.attnum ",
10346  tyinfo->dobj.catId.oid);
10347  }
10348  else
10349  {
10350  /*
10351  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
10352  * should always be false.
10353  */
10354  appendPQExpBuffer(query, "SELECT a.attname, "
10355  "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10356  "a.attlen, a.attalign, a.attisdropped, "
10357  "0 AS attcollation "
10358  "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
10359  "WHERE ct.oid = '%u'::pg_catalog.oid "
10360  "AND a.attrelid = ct.typrelid "
10361  "ORDER BY a.attnum ",
10362  tyinfo->dobj.catId.oid);
10363  }
10364 
10365  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10366 
10367  ntups = PQntuples(res);
10368 
10369  i_attname = PQfnumber(res, "attname");
10370  i_atttypdefn = PQfnumber(res, "atttypdefn");
10371  i_attlen = PQfnumber(res, "attlen");
10372  i_attalign = PQfnumber(res, "attalign");
10373  i_attisdropped = PQfnumber(res, "attisdropped");
10374  i_attcollation = PQfnumber(res, "attcollation");
10375 
10376  if (dopt->binary_upgrade)
10377  {
10379  tyinfo->dobj.catId.oid);
10380  binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
10381  }
10382 
10383  qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10384 
10385  appendPQExpBuffer(q, "CREATE TYPE %s AS (",
10386  qtypname);
10387 
10388  actual_atts = 0;
10389  for (i = 0; i < ntups; i++)
10390  {
10391  char *attname;
10392  char *atttypdefn;
10393  char *attlen;
10394  char *attalign;
10395  bool attisdropped;
10396  Oid attcollation;
10397 
10398  attname = PQgetvalue(res, i, i_attname);
10399  atttypdefn = PQgetvalue(res, i, i_atttypdefn);
10400  attlen = PQgetvalue(res, i, i_attlen);
10401  attalign = PQgetvalue(res, i, i_attalign);
10402  attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
10403  attcollation = atooid(PQgetvalue(res, i, i_attcollation));
10404 
10405  if (attisdropped && !dopt->binary_upgrade)
10406  continue;
10407 
10408  /* Format properly if not first attr */
10409  if (actual_atts++ > 0)
10410  appendPQExpBufferChar(q, ',');
10411  appendPQExpBufferStr(q, "\n\t");
10412 
10413  if (!attisdropped)
10414  {
10415  appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
10416 
10417  /* Add collation if not default for the column type */
10418  if (OidIsValid(attcollation))
10419  {
10420  CollInfo *coll;
10421 
10422  coll = findCollationByOid(attcollation);
10423  if (coll)
10424  {
10425  /* always schema-qualify, don't try to be smart */
10426  appendPQExpBuffer(q, " COLLATE %s.",
10427  fmtId(coll->dobj.namespace->dobj.name));
10428  appendPQExpBufferStr(q, fmtId(coll->dobj.name));
10429  }
10430  }
10431  }
10432  else
10433  {
10434  /*
10435  * This is a dropped attribute and we're in binary_upgrade mode.
10436  * Insert a placeholder for it in the CREATE TYPE command, and set
10437  * length and alignment with direct UPDATE to the catalogs
10438  * afterwards. See similar code in dumpTableSchema().
10439  */
10440  appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
10441 
10442  /* stash separately for insertion after the CREATE TYPE */
10443  appendPQExpBufferStr(dropped,
10444  "\n-- For binary upgrade, recreate dropped column.\n");
10445  appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
10446  "SET attlen = %s, "
10447  "attalign = '%s', attbyval = false\n"
10448  "WHERE attname = ", attlen, attalign);
10449  appendStringLiteralAH(dropped, attname, fout);
10450  appendPQExpBufferStr(dropped, "\n AND attrelid = ");
10451  appendStringLiteralAH(dropped, qtypname, fout);
10452  appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
10453 
10454  appendPQExpBuffer(dropped, "ALTER TYPE %s ",
10455  qtypname);
10456  appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
10457  fmtId(attname));
10458  }
10459  }
10460  appendPQExpBufferStr(q, "\n);\n");
10461  appendPQExpBufferStr(q, dropped->data);
10462 
10463  /*
10464  * DROP must be fully qualified in case same name appears in pg_catalog
10465  */
10466  appendPQExpBuffer(delq, "DROP TYPE %s.",
10467  fmtId(tyinfo->dobj.namespace->dobj.name));
10468  appendPQExpBuffer(delq, "%s;\n",
10469  qtypname);
10470 
10471  appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10472 
10473  if (dopt->binary_upgrade)
10474  binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10475 
10476  if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10477  ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10478  tyinfo->dobj.name,
10479  tyinfo->dobj.namespace->dobj.name,
10480  NULL,
10481  tyinfo->rolname, false,
10482  "TYPE", SECTION_PRE_DATA,
10483  q->data, delq->data, NULL,
10484  NULL, 0,
10485  NULL, NULL);
10486 
10487 
10488  /* Dump Type Comments and Security Labels */
10489  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10490  dumpComment(fout, labelq->data,
10491  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10492  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10493 
10494  if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10495  dumpSecLabel(fout, labelq->data,
10496  tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10497  tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10498 
10499  if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10500  dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10501  qtypname, NULL, tyinfo->dobj.name,
10502  tyinfo->dobj.namespace->dobj.name,
10503  tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10504  tyinfo->inittypacl, tyinfo->initrtypacl);
10505 
10506  PQclear(res);
10507  destroyPQExpBuffer(q);
10508  destroyPQExpBuffer(dropped);
10509  destroyPQExpBuffer(delq);
10510  destroyPQExpBuffer(labelq);
10511  destroyPQExpBuffer(query);
10512 
10513  /* Dump any per-column comments */
10514  if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10515  dumpCompositeTypeColComments(fout, tyinfo);
10516 }
char * name
Definition: pg_dump.h:132
DumpComponents dump
Definition: pg_dump.h:133
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_type_oid)
Definition: pg_dump.c:3746
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
char * inittypacl
Definition: pg_dump.h:173
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
Oid typrelid
Definition: pg_dump.h:176
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char * initrtypacl
Definition: pg_dump.h:174
static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
Definition: pg_dump.c:10524
char * typacl
Definition: pg_dump.h:171
DumpId dumpId
Definition: pg_dump.h:131
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
#define OidIsValid(objectId)
Definition: c.h:534
DumpableObject dobj
Definition: pg_dump.h:245
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:133
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
DumpOptions * dopt
Definition: pg_backup.h:178
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:14204
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:3830
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
DumpableObject dobj
Definition: pg_dump.h:164
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:3897
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:297
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define NULL
Definition: c.h:226
char * rtypacl
Definition: pg_dump.h:172
char * rolname
Definition: pg_dump.h:170
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:14128
CollInfo * findCollationByOid(Oid oid)
Definition: common.c:836
#define DUMP_COMPONENT_SECLABEL
Definition: pg_dump.h:94
#define DUMP_COMPONENT_ACL
Definition: pg_dump.h:95
CatalogId catId
Definition: pg_dump.h:130
#define atooid(x)
Definition: lo.c:17
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:409
int remoteVersion
Definition: pg_backup.h:183
static void dumpCompositeTypeColComments ( Archive fout,
TypeInfo tyinfo 
)
static

Definition at line 10524 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, NULL, CommentItem::objsubid, PGRES_TUPLES_OK, PQclear(), PQfnumber(), PQgetvalue(), PQntuples(), resetPQExpBuffer(), _typeInfo::rolname, SECTION_NONE, and _typeInfo::typrelid.

Referenced by dumpCompositeType().

10525 {
10526  CommentItem *comments;
10527  int ncomments;
10528  PGresult *res;
10529  PQExpBuffer query;
10530  PQExpBuffer target;
10531  Oid pgClassOid;
10532  int i;
10533  int ntups;
10534  int i_attname;
10535  int i_attnum;
10536 
10537  query = createPQExpBuffer();
10538 
10539  appendPQExpBuffer(query,
10540  "SELECT c.tableoid, a.attname, a.attnum "
10541  "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
10542  "WHERE c.oid = '%u' AND c.oid = a.attrelid "
10543  " AND NOT a.attisdropped "
10544  "ORDER BY a.attnum ",
10545  tyinfo->typrelid);
10546 
10547  /* Fetch column attnames */
10548  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10549 
10550  ntups = PQntuples(res);
10551  if (ntups < 1)
10552  {
10553  PQclear(res);
10554  destroyPQExpBuffer(query);
10555  return;
10556  }
10557 
10558  pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
10559 
10560  /* Search for comments associated with type's pg_class OID */
10561  ncomments = findComments(fout,
10562  pgClassOid,
10563  tyinfo->typrelid,
10564  &comments);
10565 
10566  /* If no comments exist, we're done */
10567  if (ncomments <= 0)
10568  {
10569  PQclear(res);
10570  destroyPQExpBuffer(query);
10571  return;
10572  }
10573 
10574  /* Build COMMENT ON statements */
10575  target = createPQExpBuffer();
10576 
10577  i_attnum = PQfnumber(res, "attnum");
10578  i_attname = PQfnumber(res, "attname");
10579  while (ncomments > 0)
10580  {
10581  const char *attname;
10582 
10583  attname = NULL;
10584  for (i = 0; i < ntups; i++)
10585  {
10586  if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
10587  {
10588  attname = PQgetvalue(res, i, i_attname);
10589  break;
10590  }
10591  }
10592  if (attname) /* just in case we don't find it */
10593  {
10594  const char *descr = comments->descr;
10595 
10596  resetPQExpBuffer(target);
10597  appendPQExpBuffer(target, "COLUMN %s.",
10598  fmtId(tyinfo->dobj.name));
10599  appendPQExpBufferStr(target, fmtId(attname));
10600 
10601  resetPQExpBuffer(query);
10602  appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
10603  appendStringLiteralAH(query, descr, fout);
10604  appendPQExpBufferStr(query, ";\n");
10605 
10607  target->data,
10608  tyinfo->dobj.namespace->dobj.name,
10609  NULL, tyinfo->rolname,
10610  false, "COMMENT", SECTION_NONE,
10611  query->data, "", NULL,
10612  &(tyinfo->dobj.dumpId), 1,
10613  NULL, NULL);
10614  }
10615 
10616  comments++;
10617  ncomments--;
10618  }
10619 
10620  PQclear(res);
10621  destroyPQExpBuffer(query);
10622  destroyPQExpBuffer(target);
10623 }
char * name
Definition: pg_dump.h:132
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
Oid typrelid
Definition: pg_dump.h:176
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int objsubid
Definition: pg_dump.c:69
DumpId dumpId
Definition: pg_dump.h:131
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items)
Definition: pg_dump.c:8969
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
const char * descr
Definition: pg_dump.c:66
DumpableObject dobj
Definition: pg_dump.h:164
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:297
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:226
char * rolname
Definition: pg_dump.h:170
#define atooid(x)
Definition: lo.c:17
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:130
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:409
DumpId createDumpId(void)
Definition: common.c:553
static void dumpConstraint ( Archive fout,
ConstraintInfo coninfo 
)
static

Definition at line 15603 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(), NULL, CatalogId::oid, psprintf(), _typeInfo::rolname, _tableInfo::rolname, SECTION_POST_DATA, _constraintInfo::separate, and _indxInfo::tablespace.

Referenced by dumpDumpableObject().

15604 {
15605  DumpOptions *dopt = fout->dopt;
15606  TableInfo *tbinfo = coninfo->contable;
15607  PQExpBuffer q;
15608  PQExpBuffer delq;
15609  char *tag = NULL;
15610 
15611  /* Skip if not to be dumped */
15612  if (!coninfo->dobj.dump || dopt->dataOnly)
15613  return;
15614 
15615  q = createPQExpBuffer();
15616  delq = createPQExpBuffer();
15617 
15618  if (coninfo->contype == 'p' ||
15619  coninfo->contype == 'u' ||
15620  coninfo->contype == 'x')
15621  {
15622  /* Index-related constraint */
15623  IndxInfo *indxinfo;
15624  int k;
15625 
15626  indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
15627 
15628  if (indxinfo == NULL)
15629  exit_horribly(NULL, "missing index for constraint \"%s\"\n",
15630  coninfo->dobj.name);
15631 
15632  if (dopt->binary_upgrade)
15634  indxinfo->dobj.catId.oid, true);
15635 
15636  appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
15637  fmtId(tbinfo->dobj.name));
15638  appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
15639  fmtId(coninfo->dobj.name));
15640 
15641  if (coninfo->condef)
15642  {
15643  /* pg_get_constraintdef should have provided everything */
15644  appendPQExpBuffer(q, "%s;\n", coninfo->condef);
15645  }
15646  else
15647  {
15648  appendPQExpBuffer(q, "%s (",
15649  coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
15650  for (k = 0; k < indxinfo->indnkeys; k++)
15651  {
15652  int indkey = (int) indxinfo->indkeys[k];
15653  const char *attname;
15654 
15655  if (indkey == InvalidAttrNumber)
15656  break;
15657  attname = getAttrName(indkey, tbinfo);
15658 
15659  appendPQExpBuffer(q, "%s%s",
15660  (k == 0) ? "" : ", ",
15661  fmtId(attname));
15662  }
15663 
15664  appendPQExpBufferChar(q, ')');
15665 
15666  if (nonemptyReloptions(indxinfo->indreloptions))
15667  {
15668  appendPQExpBufferStr(q, " WITH (");
15669  appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
15670  appendPQExpBufferChar(q, ')');
15671  }
15672 
15673  if (coninfo->condeferrable)
15674  {
15675  appendPQExpBufferStr(q, " DEFERRABLE");
15676  if (coninfo->condeferred)
15677  appendPQExpBufferStr(q, " INITIALLY DEFERRED");
15678  }
15679 
15680  appendPQExpBufferStr(q, ";\n");
15681  }
15682 
15683  /* If the index is clustered, we need to record that. */
15684  if (indxinfo->indisclustered)
15685  {
15686  appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
15687  fmtId(tbinfo->dobj.name));
15688  appendPQExpBuffer(q, " ON %s;\n",
15689  fmtId(indxinfo->dobj.name));
15690  }
15691 
15692  /*
15693  * DROP must be fully qualified in case same name appears in
15694  * pg_catalog
15695  */
15696  appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
15697  fmtId(tbinfo->dobj.namespace->dobj.name));
15698  appendPQExpBuffer(delq, "%s ",
15699  fmtId(tbinfo->dobj.name));
15700  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
15701  fmtId(coninfo->dobj.name));
15702 
15703  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
15704 
15705  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15706  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
15707  tag,
15708  tbinfo->dobj.namespace->dobj.name,
15709  indxinfo->tablespace,
15710  tbinfo->rolname, false,
15711  "CONSTRAINT", SECTION_POST_DATA,
15712  q->data, delq->data, NULL,
15713  NULL, 0,
15714  NULL, NULL);
15715  }
15716  else if (coninfo->contype == 'f')
15717  {
15718  /*
15719  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
15720  * current table data is not processed
15721  */
15722  appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
15723  fmtId(tbinfo->dobj.name));
15724  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
15725  fmtId(coninfo->dobj.name),
15726  coninfo->condef);
15727 
15728  /*
15729  * DROP must be fully qualified in case same name appears in
15730  * pg_catalog
15731  */
15732  appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
15733  fmtId(tbinfo->dobj.namespace->dobj.name));
15734  appendPQExpBuffer(delq, "%s ",
15735  fmtId(tbinfo->dobj.name));
15736  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
15737  fmtId(coninfo->dobj.name));
15738 
15739  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
15740 
15741  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15742  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
15743  tag,
15744  tbinfo->dobj.namespace->dobj.name,
15745  NULL,
15746  tbinfo->rolname, false,
15747  "FK CONSTRAINT", SECTION_POST_DATA,
15748  q->data, delq->data, NULL,
15749  NULL, 0,
15750  NULL, NULL);
15751  }
15752  else if (coninfo->contype == 'c' && tbinfo)
15753  {
15754  /* CHECK constraint on a table */
15755 
15756  /* Ignore if not to be dumped separately, or if it was inherited */
15757  if (coninfo->separate && coninfo->conislocal)
15758  {
15759  /* not ONLY since we want it to propagate to children */
15760  appendPQExpBuffer(q, "ALTER TABLE %s\n",
15761  fmtId(tbinfo->dobj.name));
15762  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
15763  fmtId(coninfo->dobj.name),
15764  coninfo->condef);
15765 
15766  /*
15767  * DROP must be fully qualified in case same name appears in
15768  * pg_catalog
15769  */
15770  appendPQExpBuffer(delq, "ALTER TABLE %s.",
15771  fmtId(tbinfo->dobj.namespace->dobj.name));
15772  appendPQExpBuffer(delq, "%s ",
15773  fmtId(tbinfo->dobj.name));
15774  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
15775  fmtId(coninfo->dobj.name));
15776 
15777  tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
15778 
15779  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15780  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
15781  tag,
15782  tbinfo->dobj.namespace->dobj.name,
15783  NULL,
15784  tbinfo->rolname, false,
15785  "CHECK CONSTRAINT", SECTION_POST_DATA,
15786  q->data, delq->data, NULL,
15787  NULL, 0,
15788  NULL, NULL);
15789  }
15790  }
15791  else if (coninfo->contype == 'c' && tbinfo == NULL)
15792  {
15793  /* CHECK constraint on a domain */
15794  TypeInfo *tyinfo = coninfo->condomain;
15795 
15796  /* Ignore if not to be dumped separately */
15797  if (coninfo->separate)
15798  {
15799  appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
15800  fmtId(tyinfo->dobj.name));
15801  appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
15802  fmtId(coninfo->dobj.name),
15803  coninfo->condef);
15804 
15805  /*
15806  * DROP must be fully qualified in case same name appears in
15807  * pg_catalog
15808  */
15809  appendPQExpBuffer(delq, "ALTER DOMAIN %s.",
15810  fmtId(tyinfo->dobj.namespace->dobj.name));
15811  appendPQExpBuffer(delq, "%s ",
15812  fmtId(tyinfo->dobj.name));
15813  appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
15814  fmtId(coninfo->dobj.name));
15815 
15816  tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
15817 
15818  if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15819  ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
15820  tag,
15821  tyinfo->dobj.namespace->dobj.name,
15822  NULL,
15823  tyinfo->rolname, false,
15824  "CHECK CONSTRAINT", SECTION_POST_DATA,
15825  q->data, delq->data, NULL,
15826  NULL, 0,
15827  NULL, NULL);
15828  }
15829  }
15830  else
15831  {
15832  exit_horribly(NULL, "unrecognized constraint type: %c\n",
15833  coninfo->contype);
15834  }
15835 
15836  /* Dump Constraint Comments --- only works for table constraints */
15837  if (tbinfo && coninfo->separate &&
15838  coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15839  dumpTableConstraintComment(fout, coninfo);
15840 
15841  free(tag);
15842  destroyPQExpBuffer(q);
15843  destroyPQExpBuffer(delq);
15844 }
char * tablespace
Definition: pg_dump.h:354
char * name
Definition: pg_dump.h:132
DumpComponents dump
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 * 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:17413
char * rolname
Definition: pg_dump.h:261
DumpId dumpId
Definition: pg_dump.h:131
DumpId conindex
Definition: pg_dump.h:421
DumpableObject dobj
Definition: pg_dump.h:260
char * condef
Definition: pg_dump.h:419
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:133
DumpableObject dobj
Definition: pg_dump.h:415
DumpOptions * dopt
Definition: pg_backup.h:178
static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
Definition: pg_dump.c:15854
DumpableObject * findObjectByDumpId(DumpId dumpId)
Definition: common.c:573
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
Oid * indkeys
Definition: pg_dump.h:357
static void binary_upgrade_set_pg_class_oids(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_class_oid, bool is_index)
Definition: pg_dump.c:3830
static const char * getAttrName(int attrnum, TableInfo *tblInfo)
Definition: pg_dump.c:15479
bool condeferrable
Definition: pg_dump.h:422
static bool nonemptyReloptions(const char *reloptions)
Definition: pg_dump.c:17401
int indnkeys
Definition: pg_dump.h:356
char * indreloptions
Definition: pg_dump.h:355
DumpableObject dobj
Definition: pg_dump.h:164
TypeInfo * condomain
Definition: pg_dump.h:417
bool conislocal
Definition: pg_dump.h:424
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
TableInfo * contable
Definition: pg_dump.h:416
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define free(a)
Definition: header.h:60
bool condeferred
Definition: pg_dump.h:423
#define NULL
Definition: c.h:226
char * rolname
Definition: pg_dump.h:170
#define InvalidAttrNumber
Definition: attnum.h:23
void exit_horribly(const char *modulename, const char *fmt,...)
CatalogId catId
Definition: pg_dump.h:130
DumpableObject dobj
Definition: pg_dump.h:351
bool indisclustered
Definition: pg_dump.h:358
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 void dumpConversion ( Archive fout,
ConvInfo convinfo 
)
static

Definition at line 12843 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, NULL, CatalogId::oid, PQclear(), PQfnumber(), PQgetvalue(), _convInfo::rolname, SECTION_PRE_DATA, and selectSourceSchema().

Referenced by dumpDumpableObject().

12844 {
12845  DumpOptions *dopt = fout->dopt;
12846  PQExpBuffer query;
12847  PQExpBuffer q;
12848  PQExpBuffer delq;
12849  PQExpBuffer labelq;
12850  PGresult *res;
12851  int i_conforencoding;
12852  int i_contoencoding;
12853  int i_conproc;
12854  int i_condefault;
12855  const char *conforencoding;
12856  const char *contoencoding;
12857  const char *conproc;
12858  bool condefault;
12859 
12860  /* Skip if not to be dumped */
12861  if (!convinfo->dobj.dump || dopt->dataOnly)
12862  return;
12863 
12864  query = createPQExpBuffer();
12865  q = createPQExpBuffer();
12866  delq = createPQExpBuffer();
12867  labelq = createPQExpBuffer();
12868 
12869  /* Make sure we are in proper schema */
12870  selectSourceSchema(fout, convinfo->dobj.namespace->dobj.name);
12871 
12872  /* Get conversion-specific details */
12873  appendPQExpBuffer(query, "SELECT "
12874  "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
12875  "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
12876  "conproc, condefault "
12877  "FROM pg_catalog.pg_conversion c "
12878  "WHERE c.oid = '%u'::pg_catalog.oid",
12879  convinfo->dobj.catId.oid);
12880 
12881  res = ExecuteSqlQueryForSingleRow(fout, query->data);
12882 
12883  i_conforencoding = PQfnumber(res, "conforencoding");
12884  i_contoencoding = PQfnumber(res, "contoencoding");
12885  i_conproc = PQfnumber(res, "conproc");
12886  i_condefault = PQfnumber(res, "condefault");
12887 
12888  conforencoding = PQgetvalue(res, 0, i_conforencoding);
12889  contoencoding = PQgetvalue(res, 0, i_contoencoding);
12890  conproc = PQgetvalue(res, 0, i_conproc);
12891  condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
12892 
12893  /*
12894  * DROP must be fully qualified in case same name appears in pg_catalog
12895  */
12896  appendPQExpBuffer(delq, "DROP CONVERSION %s",
12897  fmtId(convinfo->dobj.namespace->dobj.name));
12898  appendPQExpBuffer(delq, ".%s;\n",
12899  fmtId(convinfo->dobj.name));
12900 
12901  appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
12902  (condefault) ? "DEFAULT " : "",
12903  fmtId(convinfo->dobj.name));
12904  appendStringLiteralAH(q, conforencoding, fout);
12905  appendPQExpBufferStr(q, " TO ");
12906  appendStringLiteralAH(q, contoencoding, fout);
12907  /* regproc output is already sufficiently quoted */
12908  appendPQExpBuffer(q, " FROM %s;\n", conproc);
12909 
12910  appendPQExpBuffer(labelq, "CONVERSION %s", fmtId(convinfo->dobj.name));
12911 
12912  if (dopt->binary_upgrade)
12913  binary_upgrade_extension_member(q, &convinfo->dobj, labelq->data);
12914 
12915  if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12916  ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
12917  convinfo->dobj.name,
12918  convinfo->dobj.namespace->dobj.name,
12919  NULL,
12920  convinfo->rolname,
12921  false, "CONVERSION", SECTION_PRE_DATA,
12922  q->data, delq->data, NULL,
12923  NULL, 0,
12924  NULL, NULL);
12925 
12926  /* Dump Conversion Comments */
12927  if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12928  dumpComment(fout, labelq->data,
12929  convinfo->dobj.namespace->dobj.name, convinfo->rolname,
12930  convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
12931 
12932  PQclear(res);
12933 
12934  destroyPQExpBuffer(query);
12935  destroyPQExpBuffer(q);
12936  destroyPQExpBuffer(delq);
12937  destroyPQExpBuffer(labelq);
12938 }
char * name
Definition: pg_dump.h:132
DumpComponents dump
Definition: pg_dump.h:133
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:252
DumpId dumpId
Definition: pg_dump.h:131
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
int binary_upgrade
Definition: pg_backup.h:133
static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId)
Definition: pg_dump.c:8815
DumpOptions * dopt
Definition: pg_backup.h:178
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool dataOnly
Definition: pg_backup.h:137
static void selectSourceSchema(Archive *fout, const char *schemaName)
Definition: pg_dump.c:17304
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
#define DUMP_COMPONENT_COMMENT
Definition: pg_dump.h:93
static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, DumpableObject *dobj, const char *objlabel)
Definition: pg_dump.c:3897
#define appendStringLiteralAH(buf, str, AH)
Definition: pg_backup.h:297
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define DUMP_COMPONENT_DEFINITION
Definition: pg_dump.h:91
#define NULL
Definition: c.h:226
DumpableObject dobj
Definition: pg_dump.h:251
CatalogId catId
Definition: pg_dump.h:130
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 void dumpDatabase ( Archive AH)
static

Definition at line 2410 of file pg_dump.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralAH, ArchiveEntry(), atooid, _dumpOptions::binary_upgrade, buildShSecLabelQuery(), conn, createDumpId(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), Archive::dopt, dumpComment(), emitShSecLabels(), encoding, ExecuteSqlQuery(), ExecuteSqlQueryForSingleRow(), fmtId(), g_verbose, GetConnection(), LargeObjectMetadataRelationId, LargeObjectRelationId, _dumpOptions::no_security_labels, NULL, CatalogId::oid, _dumpOptions::outputNoTablespaces, PGRES_TUPLES_OK, PQclear(), PQdb(), PQfnumber(), PQgetvalue(), Archive::remoteVersion, resetPQExpBuffer(), SECTION_NONE, SECTION_PRE_DATA, selectSourceSchema(), CatalogId::tableoid, tablespace, username_subquery, and write_msg().

Referenced by main().

2411 {
2412  DumpOptions *dopt = fout->dopt;
2413  PQExpBuffer dbQry = createPQExpBuffer();
2414  PQExpBuffer delQry = createPQExpBuffer();
2415  PQExpBuffer creaQry = createPQExpBuffer();
2416  PGconn *conn = GetConnection(fout);
2417  PGresult *res;
2418  int i_tableoid,
2419  i_oid,
2420  i_dba,
2421  i_encoding,
2422  i_collate,
2423  i_ctype,
2424  i_frozenxid,
2425  i_minmxid,
2426  i_tablespace;
2427  CatalogId dbCatId;
2428  DumpId dbDumpId;
2429  const char *datname,
2430  *dba,
2431  *encoding,
2432  *collate,
2433  *ctype,
2434  *tablespace;
2435  uint32 frozenxid,
2436  minmxid;
2437 
2438  datname = PQdb(conn);
2439 
2440  if (g_verbose)
2441  write_msg(NULL, "saving database definition\n");
2442 
2443  /* Make sure we are in proper schema */
2444  selectSourceSchema(fout, "pg_catalog");
2445 
2446  /* Get the database owner and parameters from pg_database */
2447  if (fout->remoteVersion >= 90300)
2448  {
2449  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2450  "(%s datdba) AS dba, "
2451  "pg_encoding_to_char(encoding) AS encoding, "
2452  "datcollate, datctype, datfrozenxid, datminmxid, "
2453  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2454  "shobj_description(oid, 'pg_database') AS description "
2455 
2456  "FROM pg_database "
2457  "WHERE datname = ",
2459  appendStringLiteralAH(dbQry, datname, fout);
2460  }
2461  else if (fout->remoteVersion >= 80400)
2462  {
2463  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2464  "(%s datdba) AS dba, "
2465  "pg_encoding_to_char(encoding) AS encoding, "
2466  "datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
2467  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2468  "shobj_description(oid, 'pg_database') AS description "
2469 
2470  "FROM pg_database "
2471  "WHERE datname = ",
2473  appendStringLiteralAH(dbQry, datname, fout);
2474  }
2475  else if (fout->remoteVersion >= 80200)
2476  {
2477  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2478  "(%s datdba) AS dba, "
2479  "pg_encoding_to_char(encoding) AS encoding, "
2480  "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2481  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2482  "shobj_description(oid, 'pg_database') AS description "
2483 
2484  "FROM pg_database "
2485  "WHERE datname = ",
2487  appendStringLiteralAH(dbQry, datname, fout);
2488  }
2489  else
2490  {
2491  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2492  "(%s datdba) AS dba, "
2493  "pg_encoding_to_char(encoding) AS encoding, "
2494  "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2495  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
2496  "FROM pg_database "
2497  "WHERE datname = ",
2499  appendStringLiteralAH(dbQry, datname, fout);
2500  }
2501 
2502  res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
2503 
2504  i_tableoid = PQfnumber(res, "tableoid");
2505  i_oid = PQfnumber(res, "oid");
2506  i_dba = PQfnumber(res, "dba");
2507  i_encoding = PQfnumber(res, "encoding");
2508  i_collate = PQfnumber(res, "datcollate");
2509  i_ctype = PQfnumber(res, "datctype");
2510  i_frozenxid = PQfnumber(res, "datfrozenxid");
2511  i_minmxid = PQfnumber(res, "datminmxid");
2512  i_tablespace = PQfnumber(res, "tablespace");
2513 
2514  dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
2515  dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2516  dba = PQgetvalue(res, 0, i_dba);
2517  encoding = PQgetvalue(res, 0, i_encoding);
2518  collate = PQgetvalue(res, 0, i_collate);
2519  ctype = PQgetvalue(res, 0, i_ctype);
2520  frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
2521  minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
2522  tablespace = PQgetvalue(res, 0, i_tablespace);
2523 
2524  appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
2525  fmtId(datname));
2526  if (strlen(encoding) > 0)
2527  {
2528  appendPQExpBufferStr(creaQry, " ENCODING = ");
2529  appendStringLiteralAH(creaQry, encoding, fout);
2530  }
2531  if (strlen(collate) > 0)
2532  {
2533  appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
2534  appendStringLiteralAH(creaQry, collate, fout);
2535  }
2536  if (strlen(ctype) > 0)
2537  {
2538  appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
2539  appendStringLiteralAH(creaQry, ctype, fout);
2540  }
2541  if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
2542  !dopt->outputNoTablespaces)
2543  appendPQExpBuffer(creaQry, " TABLESPACE = %s",
2544  fmtId(tablespace));
2545  appendPQExpBufferStr(creaQry, ";\n");
2546 
2547  if (dopt->binary_upgrade)
2548  {
2549  appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
2550  appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
2551  "SET datfrozenxid = '%u', datminmxid = '%u'\n"
2552  "WHERE datname = ",
2553  frozenxid, minmxid);
2554  appendStringLiteralAH(creaQry, datname, fout);
2555  appendPQExpBufferStr(creaQry, ";\n");
2556 
2557  }
2558 
2559  appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
2560  fmtId(datname));
2561 
2562  dbDumpId = createDumpId();
2563 
2564  ArchiveEntry(fout,
2565  dbCatId, /* catalo