1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* common.c
|
2011-11-30 02:41:06 +01:00
|
|
|
* Catalog routines used by pg_dump; long ago these were shared
|
2011-12-17 01:09:38 +01:00
|
|
|
* by another dump tool, but not anymore.
|
2002-05-11 00:36:27 +02:00
|
|
|
*
|
2012-01-02 00:01:58 +01:00
|
|
|
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/bin/pg_dump/common.c
|
2000-09-15 06:35:16 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2011-11-30 02:41:06 +01:00
|
|
|
#include "pg_backup_archiver.h"
|
2003-12-08 17:39:05 +01:00
|
|
|
|
2008-03-27 04:57:34 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
#include "catalog/pg_class.h"
|
|
|
|
#include "dumpmem.h"
|
2011-11-30 02:41:06 +01:00
|
|
|
#include "dumputils.h"
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Variables for mapping DumpId to DumpableObject
|
|
|
|
*/
|
|
|
|
static DumpableObject **dumpIdMap = NULL;
|
|
|
|
static int allocedDumpIds = 0;
|
|
|
|
static DumpId lastDumpId = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Variables for mapping CatalogId to DumpableObject
|
|
|
|
*/
|
|
|
|
static bool catalogIdMapValid = false;
|
|
|
|
static DumpableObject **catalogIdMap = NULL;
|
|
|
|
static int numCatalogIds = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These variables are static to avoid the notational cruft of having to pass
|
|
|
|
* them into findTableByOid() and friends. For each of these arrays, we
|
|
|
|
* build a sorted-by-OID index array immediately after it's built, and then
|
|
|
|
* we use binary search in findTableByOid() and friends. (qsort'ing the base
|
|
|
|
* arrays themselves would be simpler, but it doesn't work because pg_dump.c
|
|
|
|
* may have already established pointers between items.)
|
|
|
|
*/
|
|
|
|
static TableInfo *tblinfo;
|
|
|
|
static TypeInfo *typinfo;
|
|
|
|
static FuncInfo *funinfo;
|
|
|
|
static OprInfo *oprinfo;
|
|
|
|
static int numTables;
|
|
|
|
static int numTypes;
|
|
|
|
static int numFuncs;
|
|
|
|
static int numOperators;
|
|
|
|
static int numCollations;
|
|
|
|
static DumpableObject **tblinfoindex;
|
|
|
|
static DumpableObject **typinfoindex;
|
|
|
|
static DumpableObject **funinfoindex;
|
|
|
|
static DumpableObject **oprinfoindex;
|
|
|
|
static DumpableObject **collinfoindex;
|
|
|
|
|
|
|
|
|
|
|
|
static void flagInhTables(TableInfo *tbinfo, int numTables,
|
|
|
|
InhInfo *inhinfo, int numInherits);
|
|
|
|
static void flagInhAttrs(TableInfo *tblinfo, int numTables);
|
|
|
|
static DumpableObject **buildIndexArray(void *objArray, int numObjs,
|
|
|
|
Size objSize);
|
|
|
|
static int DOCatalogIdCompare(const void *p1, const void *p2);
|
|
|
|
static void findParentsByOid(TableInfo *self,
|
|
|
|
InhInfo *inhinfo, int numInherits);
|
|
|
|
static int strInArray(const char *pattern, char **arr, int arr_size);
|
|
|
|
|
|
|
|
|
2006-10-10 01:36:59 +02:00
|
|
|
/*
|
2011-11-27 04:34:36 +01:00
|
|
|
* getSchemaData
|
|
|
|
* Collect information about all potentially dumpable objects
|
2006-10-10 01:36:59 +02:00
|
|
|
*/
|
2011-11-27 04:34:36 +01:00
|
|
|
TableInfo *
|
2012-02-06 20:07:55 +01:00
|
|
|
getSchemaData(Archive *fout, int *numTablesPtr)
|
2011-11-27 04:34:36 +01:00
|
|
|
{
|
|
|
|
ExtensionInfo *extinfo;
|
|
|
|
InhInfo *inhinfo;
|
|
|
|
CollInfo *collinfo;
|
|
|
|
int numNamespaces;
|
|
|
|
int numExtensions;
|
|
|
|
int numAggregates;
|
|
|
|
int numInherits;
|
|
|
|
int numRules;
|
|
|
|
int numProcLangs;
|
|
|
|
int numCasts;
|
|
|
|
int numOpclasses;
|
|
|
|
int numOpfamilies;
|
|
|
|
int numConversions;
|
|
|
|
int numTSParsers;
|
|
|
|
int numTSTemplates;
|
|
|
|
int numTSDicts;
|
|
|
|
int numTSConfigs;
|
|
|
|
int numForeignDataWrappers;
|
|
|
|
int numForeignServers;
|
|
|
|
int numDefaultACLs;
|
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading schemas\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getNamespaces(fout, &numNamespaces);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getTables should be done as soon as possible, so as to minimize the
|
|
|
|
* window between starting our transaction and acquiring per-table locks.
|
|
|
|
* However, we have to do getNamespaces first because the tables get
|
|
|
|
* linked to their containing namespaces during getTables.
|
|
|
|
*/
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined tables\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
tblinfo = getTables(fout, &numTables);
|
2011-11-27 04:34:36 +01:00
|
|
|
tblinfoindex = buildIndexArray(tblinfo, numTables, sizeof(TableInfo));
|
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading extensions\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
extinfo = getExtensions(fout, &numExtensions);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined functions\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
funinfo = getFuncs(fout, &numFuncs);
|
2011-11-27 04:34:36 +01:00
|
|
|
funinfoindex = buildIndexArray(funinfo, numFuncs, sizeof(FuncInfo));
|
|
|
|
|
|
|
|
/* this must be after getTables and getFuncs */
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined types\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
typinfo = getTypes(fout, &numTypes);
|
2011-11-27 04:34:36 +01:00
|
|
|
typinfoindex = buildIndexArray(typinfo, numTypes, sizeof(TypeInfo));
|
|
|
|
|
|
|
|
/* this must be after getFuncs, too */
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading procedural languages\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getProcLangs(fout, &numProcLangs);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined aggregate functions\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getAggregates(fout, &numAggregates);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined operators\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
oprinfo = getOperators(fout, &numOperators);
|
2011-11-27 04:34:36 +01:00
|
|
|
oprinfoindex = buildIndexArray(oprinfo, numOperators, sizeof(OprInfo));
|
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined operator classes\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getOpclasses(fout, &numOpclasses);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined operator families\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getOpfamilies(fout, &numOpfamilies);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined text search parsers\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getTSParsers(fout, &numTSParsers);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined text search templates\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getTSTemplates(fout, &numTSTemplates);
|
2006-10-10 01:36:59 +02:00
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined text search dictionaries\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getTSDictionaries(fout, &numTSDicts);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined text search configurations\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getTSConfigurations(fout, &numTSConfigs);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined foreign-data wrappers\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getForeignDataWrappers(fout, &numForeignDataWrappers);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined foreign servers\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getForeignServers(fout, &numForeignServers);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading default privileges\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getDefaultACLs(fout, &numDefaultACLs);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined collations\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
collinfo = getCollations(fout, &numCollations);
|
2011-11-27 04:34:36 +01:00
|
|
|
collinfoindex = buildIndexArray(collinfo, numCollations, sizeof(CollInfo));
|
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined conversions\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getConversions(fout, &numConversions);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading type casts\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getCasts(fout, &numCasts);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading table inheritance information\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
inhinfo = getInherits(fout, &numInherits);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading rewrite rules\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getRules(fout, &numRules);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify extension member objects and mark them as not to be dumped.
|
|
|
|
* This must happen after reading all objects that can be direct members
|
|
|
|
* of extensions, but before we begin to process table subsidiary objects.
|
|
|
|
*/
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "finding extension members\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getExtensionMembership(fout, extinfo, numExtensions);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
/* Link tables to parents, mark parents of target tables interesting */
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "finding inheritance relationships\n");
|
|
|
|
flagInhTables(tblinfo, numTables, inhinfo, numInherits);
|
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading column info for interesting tables\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getTableAttrs(fout, tblinfo, numTables);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "flagging inherited columns in subtables\n");
|
|
|
|
flagInhAttrs(tblinfo, numTables);
|
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading indexes\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getIndexes(fout, tblinfo, numTables);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading constraints\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getConstraints(fout, tblinfo, numTables);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading triggers\n");
|
2012-02-06 20:07:55 +01:00
|
|
|
getTriggers(fout, tblinfo, numTables);
|
2011-11-27 04:34:36 +01:00
|
|
|
|
|
|
|
*numTablesPtr = numTables;
|
|
|
|
return tblinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flagInhTables -
|
|
|
|
* Fill in parent link fields of every target table, and mark
|
|
|
|
* parents of target tables as interesting
|
|
|
|
*
|
|
|
|
* Note that only direct ancestors of targets are marked interesting.
|
|
|
|
* This is sufficient; we don't much care whether they inherited their
|
|
|
|
* attributes or not.
|
|
|
|
*
|
|
|
|
* modifies tblinfo
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flagInhTables(TableInfo *tblinfo, int numTables,
|
|
|
|
InhInfo *inhinfo, int numInherits)
|
2006-10-10 01:36:59 +02:00
|
|
|
{
|
2011-11-27 04:34:36 +01:00
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int numParents;
|
|
|
|
TableInfo **parents;
|
|
|
|
|
|
|
|
for (i = 0; i < numTables; i++)
|
|
|
|
{
|
|
|
|
/* Sequences and views never have parents */
|
|
|
|
if (tblinfo[i].relkind == RELKIND_SEQUENCE ||
|
|
|
|
tblinfo[i].relkind == RELKIND_VIEW)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't bother computing anything for non-target tables, either */
|
|
|
|
if (!tblinfo[i].dobj.dump)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Find all the immediate parent tables */
|
|
|
|
findParentsByOid(&tblinfo[i], inhinfo, numInherits);
|
2006-10-10 01:36:59 +02:00
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
/* Mark the parents as interesting for getTableAttrs */
|
|
|
|
numParents = tblinfo[i].numParents;
|
|
|
|
parents = tblinfo[i].parents;
|
|
|
|
for (j = 0; j < numParents; j++)
|
|
|
|
parents[j]->interesting = true;
|
|
|
|
}
|
2006-10-10 01:36:59 +02:00
|
|
|
}
|
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
/* flagInhAttrs -
|
|
|
|
* for each dumpable table in tblinfo, flag its inherited attributes
|
2012-02-10 19:28:05 +01:00
|
|
|
*
|
|
|
|
* What we need to do here is detect child columns that inherit NOT NULL
|
|
|
|
* bits from their parents (so that we needn't specify that again for the
|
|
|
|
* child) and child columns that have DEFAULT NULL when their parents had
|
|
|
|
* some non-null default. In the latter case, we make up a dummy AttrDefInfo
|
|
|
|
* object so that we'll correctly emit the necessary DEFAULT NULL clause;
|
|
|
|
* otherwise the backend will apply an inherited default to the column.
|
2011-11-27 04:34:36 +01:00
|
|
|
*
|
|
|
|
* modifies tblinfo
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flagInhAttrs(TableInfo *tblinfo, int numTables)
|
2006-10-10 01:36:59 +02:00
|
|
|
{
|
2011-11-27 04:34:36 +01:00
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
k;
|
|
|
|
|
|
|
|
for (i = 0; i < numTables; i++)
|
|
|
|
{
|
|
|
|
TableInfo *tbinfo = &(tblinfo[i]);
|
|
|
|
int numParents;
|
|
|
|
TableInfo **parents;
|
|
|
|
|
|
|
|
/* Sequences and views never have parents */
|
|
|
|
if (tbinfo->relkind == RELKIND_SEQUENCE ||
|
|
|
|
tbinfo->relkind == RELKIND_VIEW)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't bother computing anything for non-target tables, either */
|
|
|
|
if (!tbinfo->dobj.dump)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
numParents = tbinfo->numParents;
|
|
|
|
parents = tbinfo->parents;
|
|
|
|
|
|
|
|
if (numParents == 0)
|
|
|
|
continue; /* nothing to see here, move along */
|
|
|
|
|
2012-02-10 19:28:05 +01:00
|
|
|
/* For each column, search for matching column names in parent(s) */
|
2011-11-27 04:34:36 +01:00
|
|
|
for (j = 0; j < tbinfo->numatts; j++)
|
|
|
|
{
|
|
|
|
bool foundNotNull; /* Attr was NOT NULL in a parent */
|
2012-02-10 19:28:05 +01:00
|
|
|
bool foundDefault; /* Found a default in a parent */
|
2011-11-27 04:34:36 +01:00
|
|
|
|
2012-02-10 19:28:05 +01:00
|
|
|
/* no point in examining dropped columns */
|
|
|
|
if (tbinfo->attisdropped[j])
|
|
|
|
continue;
|
2011-11-27 04:34:36 +01:00
|
|
|
|
2012-02-10 19:28:05 +01:00
|
|
|
foundNotNull = false;
|
|
|
|
foundDefault = false;
|
2011-11-27 04:34:36 +01:00
|
|
|
for (k = 0; k < numParents; k++)
|
|
|
|
{
|
2012-02-10 19:28:05 +01:00
|
|
|
TableInfo *parent = parents[k];
|
2011-11-27 04:34:36 +01:00
|
|
|
int inhAttrInd;
|
|
|
|
|
|
|
|
inhAttrInd = strInArray(tbinfo->attnames[j],
|
|
|
|
parent->attnames,
|
|
|
|
parent->numatts);
|
2012-02-10 19:28:05 +01:00
|
|
|
if (inhAttrInd >= 0)
|
2011-11-27 04:34:36 +01:00
|
|
|
{
|
|
|
|
foundNotNull |= parent->notnull[inhAttrInd];
|
2012-02-10 19:28:05 +01:00
|
|
|
foundDefault |= (parent->attrdefs[inhAttrInd] != NULL);
|
2011-11-27 04:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 19:28:05 +01:00
|
|
|
/* Remember if we found inherited NOT NULL */
|
|
|
|
tbinfo->inhNotNull[j] = foundNotNull;
|
|
|
|
|
|
|
|
/* Manufacture a DEFAULT NULL clause if necessary */
|
|
|
|
if (foundDefault && tbinfo->attrdefs[j] == NULL)
|
2011-11-27 04:34:36 +01:00
|
|
|
{
|
2012-02-10 19:28:05 +01:00
|
|
|
AttrDefInfo *attrDef;
|
|
|
|
|
|
|
|
attrDef = (AttrDefInfo *) pg_malloc(sizeof(AttrDefInfo));
|
|
|
|
attrDef->dobj.objType = DO_ATTRDEF;
|
|
|
|
attrDef->dobj.catId.tableoid = 0;
|
|
|
|
attrDef->dobj.catId.oid = 0;
|
|
|
|
AssignDumpId(&attrDef->dobj);
|
|
|
|
attrDef->dobj.name = pg_strdup(tbinfo->dobj.name);
|
|
|
|
attrDef->dobj.namespace = tbinfo->dobj.namespace;
|
|
|
|
attrDef->dobj.dump = tbinfo->dobj.dump;
|
|
|
|
|
|
|
|
attrDef->adtable = tbinfo;
|
|
|
|
attrDef->adnum = j + 1;
|
|
|
|
attrDef->adef_expr = pg_strdup("NULL");
|
|
|
|
|
|
|
|
/* Will column be dumped explicitly? */
|
|
|
|
if (shouldPrintColumn(tbinfo, j))
|
2011-11-27 04:34:36 +01:00
|
|
|
{
|
2012-02-10 19:28:05 +01:00
|
|
|
attrDef->separate = false;
|
|
|
|
/* No dependency needed: NULL cannot have dependencies */
|
2011-11-27 04:34:36 +01:00
|
|
|
}
|
2012-02-10 19:28:05 +01:00
|
|
|
else
|
2011-11-27 04:34:36 +01:00
|
|
|
{
|
2012-02-10 19:28:05 +01:00
|
|
|
/* column will be suppressed, print default separately */
|
|
|
|
attrDef->separate = true;
|
|
|
|
/* ensure it comes out after the table */
|
|
|
|
addObjectDependency(&attrDef->dobj,
|
|
|
|
tbinfo->dobj.dumpId);
|
2011-11-27 04:34:36 +01:00
|
|
|
}
|
|
|
|
|
2012-02-10 19:28:05 +01:00
|
|
|
tbinfo->attrdefs[j] = attrDef;
|
2011-11-27 04:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AssignDumpId
|
|
|
|
* Given a newly-created dumpable object, assign a dump ID,
|
|
|
|
* and enter the object into the lookup table.
|
|
|
|
*
|
|
|
|
* The caller is expected to have filled in objType and catId,
|
|
|
|
* but not any of the other standard fields of a DumpableObject.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AssignDumpId(DumpableObject *dobj)
|
|
|
|
{
|
|
|
|
dobj->dumpId = ++lastDumpId;
|
|
|
|
dobj->name = NULL; /* must be set later */
|
|
|
|
dobj->namespace = NULL; /* may be set later */
|
|
|
|
dobj->dump = true; /* default assumption */
|
|
|
|
dobj->ext_member = false; /* default assumption */
|
|
|
|
dobj->dependencies = NULL;
|
|
|
|
dobj->nDeps = 0;
|
|
|
|
dobj->allocDeps = 0;
|
|
|
|
|
|
|
|
while (dobj->dumpId >= allocedDumpIds)
|
|
|
|
{
|
|
|
|
int newAlloc;
|
|
|
|
|
|
|
|
if (allocedDumpIds <= 0)
|
|
|
|
{
|
|
|
|
newAlloc = 256;
|
|
|
|
dumpIdMap = (DumpableObject **)
|
|
|
|
pg_malloc(newAlloc * sizeof(DumpableObject *));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newAlloc = allocedDumpIds * 2;
|
|
|
|
dumpIdMap = (DumpableObject **)
|
|
|
|
pg_realloc(dumpIdMap, newAlloc * sizeof(DumpableObject *));
|
|
|
|
}
|
|
|
|
memset(dumpIdMap + allocedDumpIds, 0,
|
|
|
|
(newAlloc - allocedDumpIds) * sizeof(DumpableObject *));
|
|
|
|
allocedDumpIds = newAlloc;
|
|
|
|
}
|
|
|
|
dumpIdMap[dobj->dumpId] = dobj;
|
|
|
|
|
|
|
|
/* mark catalogIdMap invalid, but don't rebuild it yet */
|
|
|
|
catalogIdMapValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assign a DumpId that's not tied to a DumpableObject.
|
|
|
|
*
|
|
|
|
* This is used when creating a "fixed" ArchiveEntry that doesn't need to
|
|
|
|
* participate in the sorting logic.
|
|
|
|
*/
|
|
|
|
DumpId
|
|
|
|
createDumpId(void)
|
|
|
|
{
|
|
|
|
return ++lastDumpId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the largest DumpId so far assigned
|
|
|
|
*/
|
|
|
|
DumpId
|
|
|
|
getMaxDumpId(void)
|
|
|
|
{
|
|
|
|
return lastDumpId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a DumpableObject by dump ID
|
|
|
|
*
|
|
|
|
* Returns NULL for invalid ID
|
|
|
|
*/
|
|
|
|
DumpableObject *
|
|
|
|
findObjectByDumpId(DumpId dumpId)
|
|
|
|
{
|
|
|
|
if (dumpId <= 0 || dumpId >= allocedDumpIds)
|
|
|
|
return NULL; /* out of range? */
|
|
|
|
return dumpIdMap[dumpId];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a DumpableObject by catalog ID
|
|
|
|
*
|
|
|
|
* Returns NULL for unknown ID
|
|
|
|
*
|
|
|
|
* We use binary search in a sorted list that is built on first call.
|
|
|
|
* If AssignDumpId() and findObjectByCatalogId() calls were freely intermixed,
|
|
|
|
* the code would work, but possibly be very slow. In the current usage
|
|
|
|
* pattern that does not happen, indeed we build the list at most twice.
|
|
|
|
*/
|
|
|
|
DumpableObject *
|
|
|
|
findObjectByCatalogId(CatalogId catalogId)
|
|
|
|
{
|
|
|
|
DumpableObject **low;
|
|
|
|
DumpableObject **high;
|
|
|
|
|
|
|
|
if (!catalogIdMapValid)
|
|
|
|
{
|
|
|
|
if (catalogIdMap)
|
|
|
|
free(catalogIdMap);
|
|
|
|
getDumpableObjects(&catalogIdMap, &numCatalogIds);
|
|
|
|
if (numCatalogIds > 1)
|
|
|
|
qsort((void *) catalogIdMap, numCatalogIds,
|
|
|
|
sizeof(DumpableObject *), DOCatalogIdCompare);
|
|
|
|
catalogIdMapValid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We could use bsearch() here, but the notational cruft of calling
|
|
|
|
* bsearch is nearly as bad as doing it ourselves; and the generalized
|
|
|
|
* bsearch function is noticeably slower as well.
|
|
|
|
*/
|
|
|
|
if (numCatalogIds <= 0)
|
|
|
|
return NULL;
|
|
|
|
low = catalogIdMap;
|
|
|
|
high = catalogIdMap + (numCatalogIds - 1);
|
|
|
|
while (low <= high)
|
|
|
|
{
|
|
|
|
DumpableObject **middle;
|
|
|
|
int difference;
|
|
|
|
|
|
|
|
middle = low + (high - low) / 2;
|
|
|
|
/* comparison must match DOCatalogIdCompare, below */
|
|
|
|
difference = oidcmp((*middle)->catId.oid, catalogId.oid);
|
|
|
|
if (difference == 0)
|
|
|
|
difference = oidcmp((*middle)->catId.tableoid, catalogId.tableoid);
|
|
|
|
if (difference == 0)
|
|
|
|
return *middle;
|
|
|
|
else if (difference < 0)
|
|
|
|
low = middle + 1;
|
|
|
|
else
|
|
|
|
high = middle - 1;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a DumpableObject by OID, in a pre-sorted array of one type of object
|
|
|
|
*
|
|
|
|
* Returns NULL for unknown OID
|
|
|
|
*/
|
|
|
|
static DumpableObject *
|
|
|
|
findObjectByOid(Oid oid, DumpableObject **indexArray, int numObjs)
|
|
|
|
{
|
|
|
|
DumpableObject **low;
|
|
|
|
DumpableObject **high;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the same as findObjectByCatalogId except we assume we need not
|
|
|
|
* look at table OID because the objects are all the same type.
|
|
|
|
*
|
|
|
|
* We could use bsearch() here, but the notational cruft of calling
|
|
|
|
* bsearch is nearly as bad as doing it ourselves; and the generalized
|
|
|
|
* bsearch function is noticeably slower as well.
|
|
|
|
*/
|
|
|
|
if (numObjs <= 0)
|
|
|
|
return NULL;
|
|
|
|
low = indexArray;
|
|
|
|
high = indexArray + (numObjs - 1);
|
|
|
|
while (low <= high)
|
|
|
|
{
|
|
|
|
DumpableObject **middle;
|
|
|
|
int difference;
|
|
|
|
|
|
|
|
middle = low + (high - low) / 2;
|
|
|
|
difference = oidcmp((*middle)->catId.oid, oid);
|
|
|
|
if (difference == 0)
|
|
|
|
return *middle;
|
|
|
|
else if (difference < 0)
|
|
|
|
low = middle + 1;
|
|
|
|
else
|
|
|
|
high = middle - 1;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build an index array of DumpableObject pointers, sorted by OID
|
|
|
|
*/
|
|
|
|
static DumpableObject **
|
|
|
|
buildIndexArray(void *objArray, int numObjs, Size objSize)
|
|
|
|
{
|
|
|
|
DumpableObject **ptrs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ptrs = (DumpableObject **) pg_malloc(numObjs * sizeof(DumpableObject *));
|
|
|
|
for (i = 0; i < numObjs; i++)
|
|
|
|
ptrs[i] = (DumpableObject *) ((char *) objArray + i * objSize);
|
|
|
|
|
|
|
|
/* We can use DOCatalogIdCompare to sort since its first key is OID */
|
|
|
|
if (numObjs > 1)
|
|
|
|
qsort((void *) ptrs, numObjs, sizeof(DumpableObject *),
|
|
|
|
DOCatalogIdCompare);
|
|
|
|
|
|
|
|
return ptrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* qsort comparator for pointers to DumpableObjects
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
DOCatalogIdCompare(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
const DumpableObject *obj1 = *(DumpableObject * const *) p1;
|
|
|
|
const DumpableObject *obj2 = *(DumpableObject * const *) p2;
|
|
|
|
int cmpval;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare OID first since it's usually unique, whereas there will only be
|
|
|
|
* a few distinct values of tableoid.
|
|
|
|
*/
|
|
|
|
cmpval = oidcmp(obj1->catId.oid, obj2->catId.oid);
|
|
|
|
if (cmpval == 0)
|
|
|
|
cmpval = oidcmp(obj1->catId.tableoid, obj2->catId.tableoid);
|
|
|
|
return cmpval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build an array of pointers to all known dumpable objects
|
|
|
|
*
|
|
|
|
* This simply creates a modifiable copy of the internal map.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
getDumpableObjects(DumpableObject ***objs, int *numObjs)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
|
|
|
|
*objs = (DumpableObject **)
|
|
|
|
pg_malloc(allocedDumpIds * sizeof(DumpableObject *));
|
|
|
|
j = 0;
|
|
|
|
for (i = 1; i < allocedDumpIds; i++)
|
|
|
|
{
|
|
|
|
if (dumpIdMap[i])
|
|
|
|
(*objs)[j++] = dumpIdMap[i];
|
|
|
|
}
|
|
|
|
*numObjs = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a dependency link to a DumpableObject
|
|
|
|
*
|
|
|
|
* Note: duplicate dependencies are currently not eliminated
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
addObjectDependency(DumpableObject *dobj, DumpId refId)
|
|
|
|
{
|
|
|
|
if (dobj->nDeps >= dobj->allocDeps)
|
|
|
|
{
|
|
|
|
if (dobj->allocDeps <= 0)
|
|
|
|
{
|
|
|
|
dobj->allocDeps = 16;
|
|
|
|
dobj->dependencies = (DumpId *)
|
|
|
|
pg_malloc(dobj->allocDeps * sizeof(DumpId));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dobj->allocDeps *= 2;
|
|
|
|
dobj->dependencies = (DumpId *)
|
|
|
|
pg_realloc(dobj->dependencies,
|
|
|
|
dobj->allocDeps * sizeof(DumpId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dobj->dependencies[dobj->nDeps++] = refId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a dependency link from a DumpableObject
|
|
|
|
*
|
|
|
|
* If there are multiple links, all are removed
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
removeObjectDependency(DumpableObject *dobj, DumpId refId)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < dobj->nDeps; i++)
|
|
|
|
{
|
|
|
|
if (dobj->dependencies[i] != refId)
|
|
|
|
dobj->dependencies[j++] = dobj->dependencies[i];
|
|
|
|
}
|
|
|
|
dobj->nDeps = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* findTableByOid
|
|
|
|
* finds the entry (in tblinfo) of the table with the given oid
|
|
|
|
* returns NULL if not found
|
|
|
|
*/
|
|
|
|
TableInfo *
|
|
|
|
findTableByOid(Oid oid)
|
|
|
|
{
|
|
|
|
return (TableInfo *) findObjectByOid(oid, tblinfoindex, numTables);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* findTypeByOid
|
|
|
|
* finds the entry (in typinfo) of the type with the given oid
|
|
|
|
* returns NULL if not found
|
|
|
|
*/
|
|
|
|
TypeInfo *
|
|
|
|
findTypeByOid(Oid oid)
|
|
|
|
{
|
|
|
|
return (TypeInfo *) findObjectByOid(oid, typinfoindex, numTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* findFuncByOid
|
|
|
|
* finds the entry (in funinfo) of the function with the given oid
|
|
|
|
* returns NULL if not found
|
|
|
|
*/
|
|
|
|
FuncInfo *
|
|
|
|
findFuncByOid(Oid oid)
|
|
|
|
{
|
|
|
|
return (FuncInfo *) findObjectByOid(oid, funinfoindex, numFuncs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* findOprByOid
|
|
|
|
* finds the entry (in oprinfo) of the operator with the given oid
|
|
|
|
* returns NULL if not found
|
|
|
|
*/
|
|
|
|
OprInfo *
|
|
|
|
findOprByOid(Oid oid)
|
|
|
|
{
|
|
|
|
return (OprInfo *) findObjectByOid(oid, oprinfoindex, numOperators);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* findCollationByOid
|
|
|
|
* finds the entry (in collinfo) of the collation with the given oid
|
|
|
|
* returns NULL if not found
|
|
|
|
*/
|
|
|
|
CollInfo *
|
|
|
|
findCollationByOid(Oid oid)
|
|
|
|
{
|
|
|
|
return (CollInfo *) findObjectByOid(oid, collinfoindex, numCollations);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* findParentsByOid
|
|
|
|
* find a table's parents in tblinfo[]
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
findParentsByOid(TableInfo *self,
|
|
|
|
InhInfo *inhinfo, int numInherits)
|
|
|
|
{
|
|
|
|
Oid oid = self->dobj.catId.oid;
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int numParents;
|
|
|
|
|
|
|
|
numParents = 0;
|
|
|
|
for (i = 0; i < numInherits; i++)
|
|
|
|
{
|
|
|
|
if (inhinfo[i].inhrelid == oid)
|
|
|
|
numParents++;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->numParents = numParents;
|
|
|
|
|
|
|
|
if (numParents > 0)
|
|
|
|
{
|
|
|
|
self->parents = (TableInfo **)
|
|
|
|
pg_malloc(sizeof(TableInfo *) * numParents);
|
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < numInherits; i++)
|
|
|
|
{
|
|
|
|
if (inhinfo[i].inhrelid == oid)
|
|
|
|
{
|
|
|
|
TableInfo *parent;
|
|
|
|
|
|
|
|
parent = findTableByOid(inhinfo[i].inhparent);
|
|
|
|
if (parent == NULL)
|
|
|
|
{
|
|
|
|
write_msg(NULL, "failed sanity check, parent OID %u of table \"%s\" (OID %u) not found\n",
|
|
|
|
inhinfo[i].inhparent,
|
|
|
|
self->dobj.name,
|
|
|
|
oid);
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
self->parents[j++] = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
self->parents = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parseOidArray
|
|
|
|
* parse a string of numbers delimited by spaces into a character array
|
|
|
|
*
|
|
|
|
* Note: actually this is used for both Oids and potentially-signed
|
|
|
|
* attribute numbers. This should cause no trouble, but we could split
|
|
|
|
* the function into two functions with different argument types if it does.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
parseOidArray(const char *str, Oid *array, int arraysize)
|
|
|
|
{
|
|
|
|
int j,
|
|
|
|
argNum;
|
|
|
|
char temp[100];
|
|
|
|
char s;
|
|
|
|
|
|
|
|
argNum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
s = *str++;
|
|
|
|
if (s == ' ' || s == '\0')
|
|
|
|
{
|
|
|
|
if (j > 0)
|
|
|
|
{
|
|
|
|
if (argNum >= arraysize)
|
|
|
|
{
|
|
|
|
write_msg(NULL, "could not parse numeric array \"%s\": too many numbers\n", str);
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
temp[j] = '\0';
|
|
|
|
array[argNum++] = atooid(temp);
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
if (s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(isdigit((unsigned char) s) || s == '-') ||
|
|
|
|
j >= sizeof(temp) - 1)
|
|
|
|
{
|
|
|
|
write_msg(NULL, "could not parse numeric array \"%s\": invalid character in number\n", str);
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
temp[j++] = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (argNum < arraysize)
|
|
|
|
array[argNum++] = InvalidOid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* strInArray:
|
|
|
|
* takes in a string and a string array and the number of elements in the
|
|
|
|
* string array.
|
|
|
|
* returns the index if the string is somewhere in the array, -1 otherwise
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
strInArray(const char *pattern, char **arr, int arr_size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < arr_size; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(pattern, arr[i]) == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Support for simple list operations
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
simple_oid_list_append(SimpleOidList *list, Oid val)
|
|
|
|
{
|
|
|
|
SimpleOidListCell *cell;
|
|
|
|
|
|
|
|
cell = (SimpleOidListCell *) pg_malloc(sizeof(SimpleOidListCell));
|
|
|
|
cell->next = NULL;
|
|
|
|
cell->val = val;
|
|
|
|
|
|
|
|
if (list->tail)
|
|
|
|
list->tail->next = cell;
|
|
|
|
else
|
|
|
|
list->head = cell;
|
|
|
|
list->tail = cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
simple_string_list_append(SimpleStringList *list, const char *val)
|
|
|
|
{
|
|
|
|
SimpleStringListCell *cell;
|
|
|
|
|
|
|
|
/* this calculation correctly accounts for the null trailing byte */
|
|
|
|
cell = (SimpleStringListCell *)
|
|
|
|
pg_malloc(sizeof(SimpleStringListCell) + strlen(val));
|
|
|
|
cell->next = NULL;
|
|
|
|
strcpy(cell->val, val);
|
2006-10-10 01:36:59 +02:00
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
if (list->tail)
|
|
|
|
list->tail->next = cell;
|
|
|
|
else
|
|
|
|
list->head = cell;
|
|
|
|
list->tail = cell;
|
2006-10-10 01:36:59 +02:00
|
|
|
}
|
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
bool
|
|
|
|
simple_oid_list_member(SimpleOidList *list, Oid val)
|
2006-10-10 01:36:59 +02:00
|
|
|
{
|
2011-11-27 04:34:36 +01:00
|
|
|
SimpleOidListCell *cell;
|
2006-10-10 01:36:59 +02:00
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
for (cell = list->head; cell; cell = cell->next)
|
|
|
|
{
|
|
|
|
if (cell->val == val)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2006-10-10 01:36:59 +02:00
|
|
|
}
|
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
bool
|
|
|
|
simple_string_list_member(SimpleStringList *list, const char *val)
|
2006-10-10 01:36:59 +02:00
|
|
|
{
|
2011-11-27 04:34:36 +01:00
|
|
|
SimpleStringListCell *cell;
|
2006-10-10 01:36:59 +02:00
|
|
|
|
2011-11-27 04:34:36 +01:00
|
|
|
for (cell = list->head; cell; cell = cell->next)
|
|
|
|
{
|
|
|
|
if (strcmp(cell->val, val) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2006-10-10 01:36:59 +02:00
|
|
|
}
|