1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* common.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* common routines between pg_dump and pg4_dump
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-05-11 00:36:27 +02:00
|
|
|
* Since pg4_dump is long-dead code, there is no longer any useful distinction
|
|
|
|
* between this file and pg_dump.c.
|
|
|
|
*
|
2004-08-29 06:13:13 +02:00
|
|
|
* Portions Copyright (c) 1996-2004, 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
|
2004-08-29 07:07:03 +02:00
|
|
|
* $PostgreSQL: pgsql/src/bin/pg_dump/common.c,v 1.84 2004/08/29 05:06:53 momjian Exp $
|
2000-09-15 06:35:16 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2001-06-27 23:21:37 +02:00
|
|
|
#include "postgres_fe.h"
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "pg_dump.h"
|
2001-06-27 23:21:37 +02:00
|
|
|
#include "pg_backup_archiver.h"
|
2003-12-08 17:39:05 +01:00
|
|
|
|
2002-04-24 04:44:19 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
#include "catalog/pg_class.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-10-30 17:48:03 +01:00
|
|
|
#include <ctype.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#include "libpq-fe.h"
|
1997-02-13 09:32:20 +01:00
|
|
|
#ifndef HAVE_STRDUP
|
1996-11-26 08:39:11 +01:00
|
|
|
#include "strdup.h"
|
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Variables for mapping DumpId to DumpableObject
|
|
|
|
*/
|
|
|
|
static DumpableObject **dumpIdMap = NULL;
|
|
|
|
static int allocedDumpIds = 0;
|
|
|
|
static DumpId lastDumpId = 0;
|
|
|
|
|
2003-12-07 04:14:01 +01:00
|
|
|
/*
|
|
|
|
* Variables for mapping CatalogId to DumpableObject
|
|
|
|
*/
|
|
|
|
static bool catalogIdMapValid = false;
|
|
|
|
static DumpableObject **catalogIdMap = NULL;
|
|
|
|
static int numCatalogIds = 0;
|
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
/*
|
|
|
|
* These variables are static to avoid the notational cruft of having to pass
|
|
|
|
* them into findTableByOid() and friends.
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
static TableInfo *tblinfo;
|
|
|
|
static TypeInfo *typinfo;
|
|
|
|
static FuncInfo *funinfo;
|
|
|
|
static OprInfo *oprinfo;
|
|
|
|
static int numTables;
|
|
|
|
static int numTypes;
|
|
|
|
static int numFuncs;
|
|
|
|
static int numOperators;
|
2003-12-06 04:00:16 +01:00
|
|
|
|
|
|
|
|
2002-05-11 00:36:27 +02:00
|
|
|
static void flagInhTables(TableInfo *tbinfo, int numTables,
|
2002-09-04 22:31:48 +02:00
|
|
|
InhInfo *inhinfo, int numInherits);
|
1998-09-01 06:40:42 +02:00
|
|
|
static void flagInhAttrs(TableInfo *tbinfo, int numTables,
|
1997-09-08 23:56:23 +02:00
|
|
|
InhInfo *inhinfo, int numInherits);
|
2003-12-07 04:14:01 +01:00
|
|
|
static int DOCatalogIdCompare(const void *p1, const void *p2);
|
|
|
|
static void findParentsByOid(TableInfo *self,
|
2004-08-29 07:07:03 +02:00
|
|
|
InhInfo *inhinfo, int numInherits);
|
1997-09-08 04:41:22 +02:00
|
|
|
static int strInArray(const char *pattern, char **arr, int arr_size);
|
1997-08-19 23:40:56 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
2003-12-06 04:00:16 +01:00
|
|
|
* getSchemaData
|
|
|
|
* Collect information about all potentially dumpable objects
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
TableInfo *
|
2003-12-06 04:00:16 +01:00
|
|
|
getSchemaData(int *numTablesPtr,
|
|
|
|
const bool schemaOnly,
|
|
|
|
const bool dataOnly)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-05-11 00:36:27 +02:00
|
|
|
NamespaceInfo *nsinfo;
|
|
|
|
AggInfo *agginfo;
|
|
|
|
InhInfo *inhinfo;
|
2003-12-06 04:00:16 +01:00
|
|
|
RuleInfo *ruleinfo;
|
|
|
|
ProcLangInfo *proclanginfo;
|
|
|
|
CastInfo *castinfo;
|
2002-07-30 23:56:04 +02:00
|
|
|
OpclassInfo *opcinfo;
|
2003-12-06 04:00:16 +01:00
|
|
|
ConvInfo *convinfo;
|
|
|
|
int numNamespaces;
|
|
|
|
int numAggregates;
|
|
|
|
int numInherits;
|
|
|
|
int numRules;
|
|
|
|
int numProcLangs;
|
|
|
|
int numCasts;
|
|
|
|
int numOpclasses;
|
|
|
|
int numConversions;
|
2002-05-11 00:36:27 +02:00
|
|
|
|
|
|
|
if (g_verbose)
|
2003-07-23 10:47:41 +02:00
|
|
|
write_msg(NULL, "reading schemas\n");
|
2002-05-11 00:36:27 +02:00
|
|
|
nsinfo = getNamespaces(&numNamespaces);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined functions\n");
|
2004-05-20 19:13:52 +02:00
|
|
|
funinfo = getFuncs(&numFuncs);
|
2003-12-06 04:00:16 +01:00
|
|
|
|
|
|
|
/* this must be after getFuncs */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (g_verbose)
|
2001-07-03 22:21:50 +02:00
|
|
|
write_msg(NULL, "reading user-defined types\n");
|
2004-05-20 19:13:52 +02:00
|
|
|
typinfo = getTypes(&numTypes);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
/* this must be after getFuncs, too */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (g_verbose)
|
2003-12-06 04:00:16 +01:00
|
|
|
write_msg(NULL, "reading procedural languages\n");
|
|
|
|
proclanginfo = getProcLangs(&numProcLangs);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (g_verbose)
|
2001-07-03 22:21:50 +02:00
|
|
|
write_msg(NULL, "reading user-defined aggregate functions\n");
|
1997-09-07 07:04:48 +02:00
|
|
|
agginfo = getAggregates(&numAggregates);
|
|
|
|
|
|
|
|
if (g_verbose)
|
2001-07-03 22:21:50 +02:00
|
|
|
write_msg(NULL, "reading user-defined operators\n");
|
1997-09-07 07:04:48 +02:00
|
|
|
oprinfo = getOperators(&numOperators);
|
|
|
|
|
2002-07-30 23:56:04 +02:00
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined operator classes\n");
|
|
|
|
opcinfo = getOpclasses(&numOpclasses);
|
|
|
|
|
2003-11-21 23:32:49 +01:00
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading user-defined conversions\n");
|
|
|
|
convinfo = getConversions(&numConversions);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (g_verbose)
|
2001-07-03 22:21:50 +02:00
|
|
|
write_msg(NULL, "reading user-defined tables\n");
|
2002-05-11 00:36:27 +02:00
|
|
|
tblinfo = getTables(&numTables);
|
2001-01-12 16:41:29 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (g_verbose)
|
2001-07-03 22:21:50 +02:00
|
|
|
write_msg(NULL, "reading table inheritance information\n");
|
1997-09-07 07:04:48 +02:00
|
|
|
inhinfo = getInherits(&numInherits);
|
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading rewrite rules\n");
|
|
|
|
ruleinfo = getRules(&numRules);
|
|
|
|
|
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading type casts\n");
|
|
|
|
castinfo = getCasts(&numCasts);
|
|
|
|
|
2002-05-11 00:36:27 +02:00
|
|
|
/* Link tables to parents, mark parents of target tables interesting */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (g_verbose)
|
2002-05-11 00:36:27 +02:00
|
|
|
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");
|
1997-09-07 07:04:48 +02:00
|
|
|
getTableAttrs(tblinfo, numTables);
|
|
|
|
|
|
|
|
if (g_verbose)
|
2001-07-03 22:21:50 +02:00
|
|
|
write_msg(NULL, "flagging inherited columns in subtables\n");
|
1997-09-07 07:04:48 +02:00
|
|
|
flagInhAttrs(tblinfo, numTables, inhinfo, numInherits);
|
|
|
|
|
2000-07-04 16:25:28 +02:00
|
|
|
if (g_verbose)
|
2003-12-06 04:00:16 +01:00
|
|
|
write_msg(NULL, "reading indexes\n");
|
|
|
|
getIndexes(tblinfo, numTables);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading constraints\n");
|
|
|
|
getConstraints(tblinfo, numTables);
|
2002-07-19 01:11:32 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
if (g_verbose)
|
|
|
|
write_msg(NULL, "reading triggers\n");
|
|
|
|
getTriggers(tblinfo, numTables);
|
2003-11-21 23:32:49 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
*numTablesPtr = numTables;
|
2001-01-12 16:41:29 +01:00
|
|
|
return tblinfo;
|
1996-08-24 22:49:41 +02:00
|
|
|
}
|
|
|
|
|
2002-05-11 00:36:27 +02:00
|
|
|
/* flagInhTables -
|
2003-12-06 04:00:16 +01:00
|
|
|
* Fill in parent link fields of every target table, and mark
|
2002-05-11 00:36:27 +02:00
|
|
|
* parents of target tables as interesting
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2002-05-11 00:36:27 +02:00
|
|
|
* 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.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* modifies tblinfo
|
2002-05-11 00:36:27 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flagInhTables(TableInfo *tblinfo, int numTables,
|
|
|
|
InhInfo *inhinfo, int numInherits)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int numParents;
|
2003-12-06 04:00:16 +01:00
|
|
|
TableInfo **parents;
|
2002-05-11 00:36:27 +02:00
|
|
|
|
|
|
|
for (i = 0; i < numTables; i++)
|
|
|
|
{
|
2002-08-29 02:17:06 +02:00
|
|
|
/* Sequences and views never have parents */
|
2002-05-11 00:36:27 +02:00
|
|
|
if (tblinfo[i].relkind == RELKIND_SEQUENCE ||
|
2002-08-29 02:17:06 +02:00
|
|
|
tblinfo[i].relkind == RELKIND_VIEW)
|
2002-05-11 00:36:27 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't bother computing anything for non-target tables, either */
|
|
|
|
if (!tblinfo[i].dump)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Find all the immediate parent tables */
|
2003-12-06 04:00:16 +01:00
|
|
|
findParentsByOid(&tblinfo[i], inhinfo, numInherits);
|
2002-05-11 00:36:27 +02:00
|
|
|
|
|
|
|
/* Mark the parents as interesting for getTableAttrs */
|
2003-12-06 04:00:16 +01:00
|
|
|
numParents = tblinfo[i].numParents;
|
|
|
|
parents = tblinfo[i].parents;
|
2002-05-11 00:36:27 +02:00
|
|
|
for (j = 0; j < numParents; j++)
|
2003-12-06 04:00:16 +01:00
|
|
|
parents[j]->interesting = true;
|
2002-05-11 00:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flagInhAttrs -
|
|
|
|
* for each dumpable table in tblinfo, flag its inherited attributes
|
|
|
|
* so when we dump the table out, we don't dump out the inherited attributes
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-05-11 00:36:27 +02:00
|
|
|
* modifies tblinfo
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1997-09-08 23:56:23 +02:00
|
|
|
flagInhAttrs(TableInfo *tblinfo, int numTables,
|
|
|
|
InhInfo *inhinfo, int numInherits)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
k;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-11 00:36:27 +02:00
|
|
|
for (i = 0; i < numTables; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
TableInfo *tbinfo = &(tblinfo[i]);
|
|
|
|
int numParents;
|
|
|
|
TableInfo **parents;
|
|
|
|
TableInfo *parent;
|
|
|
|
|
2002-08-29 02:17:06 +02:00
|
|
|
/* Sequences and views never have parents */
|
2003-12-06 04:00:16 +01:00
|
|
|
if (tbinfo->relkind == RELKIND_SEQUENCE ||
|
|
|
|
tbinfo->relkind == RELKIND_VIEW)
|
2002-05-11 00:36:27 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't bother computing anything for non-target tables, either */
|
2003-12-06 04:00:16 +01:00
|
|
|
if (!tbinfo->dump)
|
2001-04-03 10:52:59 +02:00
|
|
|
continue;
|
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
numParents = tbinfo->numParents;
|
|
|
|
parents = tbinfo->parents;
|
2002-05-11 00:36:27 +02:00
|
|
|
|
|
|
|
if (numParents == 0)
|
|
|
|
continue; /* nothing to see here, move along */
|
2001-04-03 10:52:59 +02:00
|
|
|
|
2002-10-09 18:20:25 +02:00
|
|
|
/*----------------------------------------------------------------
|
2001-10-25 07:50:21 +02:00
|
|
|
* For each attr, check the parent info: if no parent has an attr
|
|
|
|
* with the same name, then it's not inherited. If there *is* an
|
|
|
|
* attr with the same name, then only dump it if:
|
2001-04-03 10:52:59 +02:00
|
|
|
*
|
2002-10-09 18:20:25 +02:00
|
|
|
* - it is NOT NULL and zero parents are NOT NULL
|
2003-08-04 02:43:34 +02:00
|
|
|
* OR
|
2002-10-09 18:20:25 +02:00
|
|
|
* - it has a default value AND the default value does not match
|
2003-08-04 02:43:34 +02:00
|
|
|
* all parent default values, or no parents specify a default.
|
2001-04-03 10:52:59 +02:00
|
|
|
*
|
|
|
|
* See discussion on -hackers around 2-Apr-2001.
|
2002-10-09 18:20:25 +02:00
|
|
|
*----------------------------------------------------------------
|
2001-04-03 10:52:59 +02:00
|
|
|
*/
|
2003-12-06 04:00:16 +01:00
|
|
|
for (j = 0; j < tbinfo->numatts; j++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
bool foundAttr; /* Attr was found in a parent */
|
|
|
|
bool foundNotNull; /* Attr was NOT NULL in a parent */
|
|
|
|
bool defaultsMatch; /* All non-empty defaults match */
|
|
|
|
bool defaultsFound; /* Found a default in a parent */
|
|
|
|
AttrDefInfo *attrDef;
|
|
|
|
|
2001-04-03 10:52:59 +02:00
|
|
|
foundAttr = false;
|
|
|
|
foundNotNull = false;
|
|
|
|
defaultsMatch = true;
|
|
|
|
defaultsFound = false;
|
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
attrDef = tbinfo->attrdefs[j];
|
2001-04-03 10:52:59 +02:00
|
|
|
|
2002-05-11 00:36:27 +02:00
|
|
|
for (k = 0; k < numParents; k++)
|
1998-09-20 05:18:43 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
int inhAttrInd;
|
|
|
|
|
|
|
|
parent = parents[k];
|
|
|
|
inhAttrInd = strInArray(tbinfo->attnames[j],
|
|
|
|
parent->attnames,
|
|
|
|
parent->numatts);
|
2001-04-03 10:52:59 +02:00
|
|
|
|
|
|
|
if (inhAttrInd != -1)
|
|
|
|
{
|
|
|
|
foundAttr = true;
|
2003-12-06 04:00:16 +01:00
|
|
|
foundNotNull |= parent->notnull[inhAttrInd];
|
2002-05-11 00:36:27 +02:00
|
|
|
if (attrDef != NULL) /* If we have a default,
|
2001-10-25 07:50:21 +02:00
|
|
|
* check parent */
|
2001-04-03 10:52:59 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
AttrDefInfo *inhDef;
|
2001-04-03 10:52:59 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
inhDef = parent->attrdefs[inhAttrInd];
|
2001-04-03 10:52:59 +02:00
|
|
|
if (inhDef != NULL)
|
|
|
|
{
|
|
|
|
defaultsFound = true;
|
2003-12-06 04:00:16 +01:00
|
|
|
defaultsMatch &= (strcmp(attrDef->adef_expr,
|
2004-08-29 07:07:03 +02:00
|
|
|
inhDef->adef_expr) == 0);
|
2002-05-11 00:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-04-03 10:52:59 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* Based on the scan of the parents, decide if we can rely on
|
|
|
|
* the inherited attr
|
2001-04-03 10:52:59 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
if (foundAttr) /* Attr was inherited */
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-04-03 10:52:59 +02:00
|
|
|
/* Set inherited flag by default */
|
2003-12-06 04:00:16 +01:00
|
|
|
tbinfo->inhAttrs[j] = true;
|
|
|
|
tbinfo->inhAttrDef[j] = true;
|
|
|
|
tbinfo->inhNotNull[j] = true;
|
2001-04-03 10:52:59 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* Clear it if attr had a default, but parents did not, or
|
|
|
|
* mismatch
|
|
|
|
*/
|
|
|
|
if ((attrDef != NULL) && (!defaultsFound || !defaultsMatch))
|
2001-04-03 10:52:59 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
tbinfo->inhAttrs[j] = false;
|
|
|
|
tbinfo->inhAttrDef[j] = false;
|
2001-04-03 10:52:59 +02:00
|
|
|
}
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* Clear it if NOT NULL and none of the parents were NOT
|
|
|
|
* NULL
|
|
|
|
*/
|
2003-12-06 04:00:16 +01:00
|
|
|
if (tbinfo->notnull[j] && !foundNotNull)
|
2001-04-03 10:52:59 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
tbinfo->inhAttrs[j] = false;
|
|
|
|
tbinfo->inhNotNull[j] = false;
|
2001-04-03 10:52:59 +02:00
|
|
|
}
|
2002-10-09 18:20:25 +02:00
|
|
|
|
|
|
|
/* Clear it if attr has local definition */
|
2003-12-06 04:00:16 +01:00
|
|
|
if (tbinfo->attislocal[j])
|
|
|
|
tbinfo->inhAttrs[j] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for inherited CHECK constraints. We assume a constraint
|
|
|
|
* is inherited if its expression matches the parent and the name
|
|
|
|
* is the same, *or* both names start with '$'.
|
|
|
|
*/
|
|
|
|
for (j = 0; j < tbinfo->ncheck; j++)
|
|
|
|
{
|
|
|
|
ConstraintInfo *constr;
|
|
|
|
|
|
|
|
constr = &(tbinfo->checkexprs[j]);
|
|
|
|
|
|
|
|
for (k = 0; k < numParents; k++)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int l;
|
2003-12-06 04:00:16 +01:00
|
|
|
|
|
|
|
parent = parents[k];
|
|
|
|
for (l = 0; l < parent->ncheck; l++)
|
|
|
|
{
|
|
|
|
ConstraintInfo *pconstr;
|
|
|
|
|
|
|
|
pconstr = &(parent->checkexprs[l]);
|
|
|
|
if (strcmp(pconstr->condef, constr->condef) != 0)
|
|
|
|
continue;
|
2004-03-03 22:28:55 +01:00
|
|
|
if (strcmp(pconstr->dobj.name, constr->dobj.name) == 0 ||
|
|
|
|
(pconstr->dobj.name[0] == '$' &&
|
|
|
|
constr->dobj.name[0] == '$'))
|
2003-12-06 04:00:16 +01:00
|
|
|
{
|
|
|
|
constr->coninherited = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (constr->coninherited)
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-06 04:00:16 +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 catalogId,
|
|
|
|
* but not any of the other standard fields of a DumpableObject.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AssignDumpId(DumpableObject *dobj)
|
|
|
|
{
|
|
|
|
dobj->dumpId = ++lastDumpId;
|
2004-03-03 22:28:55 +01:00
|
|
|
dobj->name = NULL; /* must be set later */
|
|
|
|
dobj->namespace = NULL; /* may be set later */
|
2003-12-06 04:00:16 +01:00
|
|
|
dobj->dependencies = NULL;
|
|
|
|
dobj->nDeps = 0;
|
|
|
|
dobj->allocDeps = 0;
|
|
|
|
|
|
|
|
while (dobj->dumpId >= allocedDumpIds)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int newAlloc;
|
2003-12-06 04:00:16 +01:00
|
|
|
|
|
|
|
if (allocedDumpIds <= 0)
|
|
|
|
{
|
|
|
|
newAlloc = 256;
|
|
|
|
dumpIdMap = (DumpableObject **)
|
|
|
|
malloc(newAlloc * sizeof(DumpableObject *));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newAlloc = allocedDumpIds * 2;
|
|
|
|
dumpIdMap = (DumpableObject **)
|
|
|
|
realloc(dumpIdMap, newAlloc * sizeof(DumpableObject *));
|
|
|
|
}
|
|
|
|
if (dumpIdMap == NULL)
|
|
|
|
exit_horribly(NULL, NULL, "out of memory\n");
|
|
|
|
memset(dumpIdMap + allocedDumpIds, 0,
|
|
|
|
(newAlloc - allocedDumpIds) * sizeof(DumpableObject *));
|
|
|
|
allocedDumpIds = newAlloc;
|
|
|
|
}
|
|
|
|
dumpIdMap[dobj->dumpId] = dobj;
|
2003-12-07 04:14:01 +01:00
|
|
|
|
|
|
|
/* mark catalogIdMap invalid, but don't rebuild it yet */
|
|
|
|
catalogIdMapValid = false;
|
2003-12-06 04:00:16 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
2003-12-06 04:00:16 +01:00
|
|
|
* 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
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-12-07 04:14:01 +01:00
|
|
|
* We use binary search in a sorted list that is built on first call.
|
|
|
|
* If AssignDumpId() and findObjectByCatalogId() calls were intermixed,
|
2004-08-29 07:07:03 +02:00
|
|
|
* the code would work, but possibly be very slow. In the current usage
|
2003-12-07 04:14:01 +01:00
|
|
|
* pattern that does not happen, indeed we only need to build the list once.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2003-12-06 04:00:16 +01:00
|
|
|
DumpableObject *
|
|
|
|
findObjectByCatalogId(CatalogId catalogId)
|
|
|
|
{
|
2003-12-07 04:14:01 +01:00
|
|
|
DumpableObject **low;
|
|
|
|
DumpableObject **high;
|
2003-12-06 04:00:16 +01:00
|
|
|
|
2003-12-07 04:14:01 +01:00
|
|
|
if (!catalogIdMapValid)
|
2003-12-06 04:00:16 +01:00
|
|
|
{
|
2003-12-07 04:14:01 +01:00
|
|
|
if (catalogIdMap)
|
|
|
|
free(catalogIdMap);
|
|
|
|
getDumpableObjects(&catalogIdMap, &numCatalogIds);
|
|
|
|
if (numCatalogIds > 1)
|
|
|
|
qsort((void *) catalogIdMap, numCatalogIds,
|
|
|
|
sizeof(DumpableObject *), DOCatalogIdCompare);
|
|
|
|
catalogIdMapValid = true;
|
|
|
|
}
|
2003-12-06 04:00:16 +01:00
|
|
|
|
2003-12-07 04:14:01 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
2003-12-06 04:00:16 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-12-07 04:14:01 +01:00
|
|
|
static int
|
|
|
|
DOCatalogIdCompare(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
DumpableObject *obj1 = *(DumpableObject **) p1;
|
|
|
|
DumpableObject *obj2 = *(DumpableObject **) 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;
|
|
|
|
}
|
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
/*
|
|
|
|
* 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 **)
|
|
|
|
malloc(allocedDumpIds * sizeof(DumpableObject *));
|
|
|
|
if (*objs == NULL)
|
|
|
|
exit_horribly(NULL, NULL, "out of memory\n");
|
|
|
|
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 *)
|
|
|
|
malloc(dobj->allocDeps * sizeof(DumpId));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dobj->allocDeps *= 2;
|
|
|
|
dobj->dependencies = (DumpId *)
|
|
|
|
realloc(dobj->dependencies,
|
|
|
|
dobj->allocDeps * sizeof(DumpId));
|
|
|
|
}
|
|
|
|
if (dobj->dependencies == NULL)
|
|
|
|
exit_horribly(NULL, NULL, "out of memory\n");
|
|
|
|
}
|
|
|
|
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)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
2003-12-06 04:00:16 +01:00
|
|
|
int j = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
for (i = 0; i < dobj->nDeps; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
if (dobj->dependencies[i] != refId)
|
|
|
|
dobj->dependencies[j++] = dobj->dependencies[i];
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-12-06 04:00:16 +01:00
|
|
|
dobj->nDeps = j;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-05-11 00:36:27 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2003-12-06 04:00:16 +01:00
|
|
|
* findTableByOid
|
|
|
|
* finds the entry (in tblinfo) of the table with the given oid
|
|
|
|
* returns NULL if not found
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTE: should hash this, but just do linear search for now
|
|
|
|
*/
|
2003-12-06 04:00:16 +01:00
|
|
|
TableInfo *
|
|
|
|
findTableByOid(Oid oid)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
for (i = 0; i < numTables; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
if (tblinfo[i].dobj.catId.oid == oid)
|
|
|
|
return &tblinfo[i];
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-12-06 04:00:16 +01:00
|
|
|
return NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2002-07-19 01:11:32 +02:00
|
|
|
/*
|
2003-12-06 04:00:16 +01:00
|
|
|
* findTypeByOid
|
2004-05-20 19:13:52 +02:00
|
|
|
* finds the entry (in typinfo) of the type with the given oid
|
2003-12-06 04:00:16 +01:00
|
|
|
* returns NULL if not found
|
|
|
|
*
|
|
|
|
* NOTE: should hash this, but just do linear search for now
|
2002-07-19 01:11:32 +02:00
|
|
|
*/
|
2003-12-06 04:00:16 +01:00
|
|
|
TypeInfo *
|
|
|
|
findTypeByOid(Oid oid)
|
2002-07-19 01:11:32 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < numTypes; i++)
|
|
|
|
{
|
2004-05-20 19:13:52 +02:00
|
|
|
if (typinfo[i].dobj.catId.oid == oid)
|
|
|
|
return &typinfo[i];
|
2002-07-19 01:11:32 +02:00
|
|
|
}
|
2003-12-06 04:00:16 +01:00
|
|
|
return NULL;
|
2002-07-19 01:11:32 +02:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2003-12-06 04:00:16 +01:00
|
|
|
* findFuncByOid
|
2004-05-20 19:13:52 +02:00
|
|
|
* finds the entry (in funinfo) of the function with the given oid
|
2003-12-06 04:00:16 +01:00
|
|
|
* returns NULL if not found
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTE: should hash this, but just do linear search for now
|
|
|
|
*/
|
2003-12-06 04:00:16 +01:00
|
|
|
FuncInfo *
|
|
|
|
findFuncByOid(Oid oid)
|
2002-05-11 00:36:27 +02:00
|
|
|
{
|
|
|
|
int i;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
for (i = 0; i < numFuncs; i++)
|
2002-05-11 00:36:27 +02:00
|
|
|
{
|
2004-05-20 19:13:52 +02:00
|
|
|
if (funinfo[i].dobj.catId.oid == oid)
|
|
|
|
return &funinfo[i];
|
2002-05-11 00:36:27 +02:00
|
|
|
}
|
2003-12-06 04:00:16 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-05-11 00:36:27 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
/*
|
|
|
|
* findOprByOid
|
|
|
|
* finds the entry (in oprinfo) of the operator with the given oid
|
|
|
|
* returns NULL if not found
|
|
|
|
*
|
|
|
|
* NOTE: should hash this, but just do linear search for now
|
|
|
|
*/
|
|
|
|
OprInfo *
|
|
|
|
findOprByOid(Oid oid)
|
|
|
|
{
|
|
|
|
int i;
|
2002-05-11 00:36:27 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
for (i = 0; i < numOperators; i++)
|
|
|
|
{
|
|
|
|
if (oprinfo[i].dobj.catId.oid == oid)
|
|
|
|
return &oprinfo[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
2002-05-11 00:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* findParentsByOid
|
2003-12-06 04:00:16 +01:00
|
|
|
* find a table's parents in tblinfo[]
|
2002-05-11 00:36:27 +02:00
|
|
|
*/
|
|
|
|
static void
|
2003-12-06 04:00:16 +01:00
|
|
|
findParentsByOid(TableInfo *self,
|
|
|
|
InhInfo *inhinfo, int numInherits)
|
2002-05-11 00:36:27 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
Oid oid = self->dobj.catId.oid;
|
2002-05-11 00:36:27 +02:00
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int numParents;
|
|
|
|
|
|
|
|
numParents = 0;
|
|
|
|
for (i = 0; i < numInherits; i++)
|
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
if (inhinfo[i].inhrelid == oid)
|
2002-05-11 00:36:27 +02:00
|
|
|
numParents++;
|
|
|
|
}
|
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
self->numParents = numParents;
|
2002-05-11 00:36:27 +02:00
|
|
|
|
|
|
|
if (numParents > 0)
|
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
self->parents = (TableInfo **) malloc(sizeof(TableInfo *) * numParents);
|
2002-05-11 00:36:27 +02:00
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < numInherits; i++)
|
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
if (inhinfo[i].inhrelid == oid)
|
2002-05-11 00:36:27 +02:00
|
|
|
{
|
2003-12-06 04:00:16 +01:00
|
|
|
TableInfo *parent;
|
2002-05-11 00:36:27 +02:00
|
|
|
|
2003-12-06 04:00:16 +01:00
|
|
|
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,
|
2004-03-03 22:28:55 +01:00
|
|
|
self->dobj.name,
|
2003-12-06 04:00:16 +01:00
|
|
|
oid);
|
2002-05-11 00:36:27 +02:00
|
|
|
exit_nicely();
|
|
|
|
}
|
2003-12-06 04:00:16 +01:00
|
|
|
self->parents[j++] = parent;
|
2002-05-11 00:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2003-12-06 04:00:16 +01:00
|
|
|
self->parents = NULL;
|
2002-05-11 00:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-12-06 04:00:16 +01:00
|
|
|
* parseOidArray
|
2002-05-11 00:36:27 +02:00
|
|
|
* parse a string of numbers delimited by spaces into a character array
|
2003-12-06 04:00:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2002-05-11 00:36:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2003-12-06 04:00:16 +01:00
|
|
|
parseOidArray(const char *str, Oid *array, int arraysize)
|
2002-05-11 00:36:27 +02:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2003-07-23 10:47:41 +02:00
|
|
|
write_msg(NULL, "could not parse numeric array: too many numbers\n");
|
2002-05-11 00:36:27 +02:00
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
temp[j] = '\0';
|
2003-12-06 04:00:16 +01:00
|
|
|
array[argNum++] = atooid(temp);
|
2002-05-11 00:36:27 +02:00
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
if (s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(isdigit((unsigned char) s) || s == '-') ||
|
|
|
|
j >= sizeof(temp) - 1)
|
|
|
|
{
|
2003-07-23 10:47:41 +02:00
|
|
|
write_msg(NULL, "could not parse numeric array: invalid character in number\n");
|
2002-05-11 00:36:27 +02:00
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
temp[j++] = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (argNum < arraysize)
|
2003-12-06 04:00:16 +01:00
|
|
|
array[argNum++] = InvalidOid;
|
2002-05-11 00:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-05-11 00:36:27 +02:00
|
|
|
for (i = 0; i < arr_size; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2002-05-11 00:36:27 +02:00
|
|
|
if (strcmp(pattern, arr[i]) == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|