Commit 44c52881 authored by Stephen Frost's avatar Stephen Frost

Change the way pg_dump retrieves partitioning info

This gets rid of the code that issued separate queries to retrieve the
partitioning parent-child relationship, parent partition key, and child
partition bound information.  With this patch, the information is
retrieved instead using the queries issued from getTables() and
getInherits(), which is both more efficient than the previous approach
and doesn't require any new code.

Since the partitioning parent-child relationship is now retrieved with
the same old code that handles inheritance, partition attributes receive
a proper flagInhAttrs() treatment (that it didn't receive before), which
is needed so that the inherited NOT NULL constraints are not emitted if
we already emitted it for the parent.

Also, fix a bug in pg_dump's --binary-upgrade code, which caused pg_dump
to emit invalid command to attach a partition to its parent.

Author: Amit Langote, with some additional changes by me.
parent 5469e44f
......@@ -68,8 +68,6 @@ static int numextmembers;
static void flagInhTables(TableInfo *tbinfo, int numTables,
InhInfo *inhinfo, int numInherits);
static void flagPartitions(TableInfo *tblinfo, int numTables,
PartInfo *partinfo, int numPartitions);
static void flagInhAttrs(DumpOptions *dopt, TableInfo *tblinfo, int numTables);
static DumpableObject **buildIndexArray(void *objArray, int numObjs,
Size objSize);
......@@ -77,8 +75,6 @@ static int DOCatalogIdCompare(const void *p1, const void *p2);
static int ExtensionMemberIdCompare(const void *p1, const void *p2);
static void findParentsByOid(TableInfo *self,
InhInfo *inhinfo, int numInherits);
static void findPartitionParentByOid(TableInfo *self, PartInfo *partinfo,
int numPartitions);
static int strInArray(const char *pattern, char **arr, int arr_size);
......@@ -97,10 +93,8 @@ getSchemaData(Archive *fout, int *numTablesPtr)
NamespaceInfo *nspinfo;
ExtensionInfo *extinfo;
InhInfo *inhinfo;
PartInfo *partinfo;
int numAggregates;
int numInherits;
int numPartitions;
int numRules;
int numProcLangs;
int numCasts;
......@@ -237,10 +231,6 @@ getSchemaData(Archive *fout, int *numTablesPtr)
write_msg(NULL, "reading table inheritance information\n");
inhinfo = getInherits(fout, &numInherits);
if (g_verbose)
write_msg(NULL, "reading partition information\n");
partinfo = getPartitions(fout, &numPartitions);
if (g_verbose)
write_msg(NULL, "reading event triggers\n");
getEventTriggers(fout, &numEventTriggers);
......@@ -255,11 +245,6 @@ getSchemaData(Archive *fout, int *numTablesPtr)
write_msg(NULL, "finding inheritance relationships\n");
flagInhTables(tblinfo, numTables, inhinfo, numInherits);
/* Link tables to partition parents, mark parents as interesting */
if (g_verbose)
write_msg(NULL, "finding partition relationships\n");
flagPartitions(tblinfo, numTables, partinfo, numPartitions);
if (g_verbose)
write_msg(NULL, "reading column info for interesting tables\n");
getTableAttrs(fout, tblinfo, numTables);
......@@ -292,10 +277,6 @@ getSchemaData(Archive *fout, int *numTablesPtr)
write_msg(NULL, "reading policies\n");
getPolicies(fout, tblinfo, numTables);
if (g_verbose)
write_msg(NULL, "reading partition key information for interesting tables\n");
getTablePartitionKeyInfo(fout, tblinfo, numTables);
if (g_verbose)
write_msg(NULL, "reading publications\n");
getPublications(fout);
......@@ -354,43 +335,6 @@ flagInhTables(TableInfo *tblinfo, int numTables,
}
}
/* flagPartitions -
* Fill in parent link fields of every target table that is partition,
* and mark parents of partitions as interesting
*
* modifies tblinfo
*/
static void
flagPartitions(TableInfo *tblinfo, int numTables,
PartInfo *partinfo, int numPartitions)
{
int i;
for (i = 0; i < numTables; i++)
{
/* Some kinds are never partitions */
if (tblinfo[i].relkind == RELKIND_SEQUENCE ||
tblinfo[i].relkind == RELKIND_VIEW ||
tblinfo[i].relkind == RELKIND_MATVIEW)
continue;
/* Don't bother computing anything for non-target tables, either */
if (!tblinfo[i].dobj.dump)
continue;
/* Find the parent TableInfo and save */
findPartitionParentByOid(&tblinfo[i], partinfo, numPartitions);
/* Mark the parent as interesting for getTableAttrs */
if (tblinfo[i].partitionOf)
{
tblinfo[i].partitionOf->interesting = true;
addObjectDependency(&tblinfo[i].dobj,
tblinfo[i].partitionOf->dobj.dumpId);
}
}
}
/* flagInhAttrs -
* for each dumpable table in tblinfo, flag its inherited attributes
*
......@@ -991,40 +935,6 @@ findParentsByOid(TableInfo *self,
self->parents = NULL;
}
/*
* findPartitionParentByOid
* find a partition's parent in tblinfo[]
*/
static void
findPartitionParentByOid(TableInfo *self, PartInfo *partinfo,
int numPartitions)
{
Oid oid = self->dobj.catId.oid;
int i;
for (i = 0; i < numPartitions; i++)
{
if (partinfo[i].partrelid == oid)
{
TableInfo *parent;
parent = findTableByOid(partinfo[i].partparent);
if (parent == NULL)
{
write_msg(NULL, "failed sanity check, parent OID %u of table \"%s\" (OID %u) not found\n",
partinfo[i].partparent,
self->dobj.name,
oid);
exit_nicely(1);
}
self->partitionOf = parent;
/* While we're at it, also save the partdef */
self->partitiondef = partinfo[i].partdef;
}
}
}
/*
* parseOidArray
* parse a string of numbers delimited by spaces into a character array
......
This diff is collapsed.
......@@ -294,6 +294,7 @@ typedef struct _tableInfo
bool interesting; /* true if need to collect more data */
bool dummy_view; /* view's real definition must be postponed */
bool postponed_def; /* matview must be postponed into post-data */
bool ispartition; /* is table a partition? */
/*
* These fields are computed only if we decide the table is interesting
......@@ -319,6 +320,7 @@ typedef struct _tableInfo
struct _attrDefInfo **attrdefs; /* DEFAULT expressions */
struct _constraintInfo *checkexprs; /* CHECK constraints */
char *partkeydef; /* partition key definition */
char *partbound; /* partition bound definition */
bool needs_override; /* has GENERATED ALWAYS AS IDENTITY */
/*
......@@ -329,8 +331,6 @@ typedef struct _tableInfo
struct _tableDataInfo *dataObj; /* TableDataInfo, if dumping its data */
int numTriggers; /* number of triggers for table */
struct _triggerInfo *triggers; /* array of TriggerInfo structs */
struct _tableInfo *partitionOf; /* TableInfo for the partition parent */
char *partitiondef; /* partition key definition */
} TableInfo;
typedef struct _attrDefInfo
......@@ -476,15 +476,6 @@ typedef struct _inhInfo
Oid inhparent; /* OID of its parent */
} InhInfo;
/* PartInfo isn't a DumpableObject, just temporary state */
typedef struct _partInfo
{
Oid partrelid; /* OID of a partition */
Oid partparent; /* OID of its parent */
char *partdef; /* partition bound definition */
} PartInfo;
typedef struct _prsInfo
{
DumpableObject dobj;
......@@ -691,7 +682,6 @@ extern ConvInfo *getConversions(Archive *fout, int *numConversions);
extern TableInfo *getTables(Archive *fout, int *numTables);
extern void getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables);
extern InhInfo *getInherits(Archive *fout, int *numInherits);
extern PartInfo *getPartitions(Archive *fout, int *numPartitions);
extern void getIndexes(Archive *fout, TableInfo tblinfo[], int numTables);
extern void getExtendedStatistics(Archive *fout, TableInfo tblinfo[], int numTables);
extern void getConstraints(Archive *fout, TableInfo tblinfo[], int numTables);
......@@ -717,7 +707,6 @@ extern void processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
int numExtensions);
extern EventTriggerInfo *getEventTriggers(Archive *fout, int *numEventTriggers);
extern void getPolicies(Archive *fout, TableInfo tblinfo[], int numTables);
extern void getTablePartitionKeyInfo(Archive *fout, TableInfo *tblinfo, int numTables);
extern void getPublications(Archive *fout);
extern void getPublicationTables(Archive *fout, TableInfo tblinfo[],
int numTables);
......
......@@ -1049,6 +1049,40 @@ my %tests = (
section_post_data => 1,
section_data => 1, }, },
'ALTER TABLE ONLY dump_test.measurement ATTACH PARTITION measurement_y2006m2' => {
all_runs => 1,
regexp => qr/^
\QALTER TABLE ONLY dump_test.measurement ATTACH PARTITION measurement_y2006m2 \E
\QFOR VALUES FROM ('2006-02-01') TO ('2006-03-01');\E\n
/xm,
like => {
binary_upgrade => 1, },
unlike => {
clean => 1,
clean_if_exists => 1,
createdb => 1,
defaults => 1,
exclude_dump_test_schema => 1,
exclude_test_table => 1,
exclude_test_table_data => 1,
no_blobs => 1,
no_privs => 1,
no_owner => 1,
pg_dumpall_dbprivs => 1,
role => 1,
schema_only => 1,
section_pre_data => 1,
with_oids => 1,
only_dump_test_schema => 1,
only_dump_test_table => 1,
pg_dumpall_globals => 1,
pg_dumpall_globals_clean => 1,
section_post_data => 1,
test_schema_plus_blobs => 1,
column_inserts => 1,
data_only => 1,
section_data => 1, }, },
'ALTER TABLE test_table CLUSTER ON test_table_pkey' => {
all_runs => 1,
catch_all => 'ALTER TABLE ... commands',
......@@ -4764,8 +4798,6 @@ qr/CREATE TRANSFORM FOR integer LANGUAGE sql \(FROM SQL WITH FUNCTION pg_catalog
\Q--\E\n\n
\QCREATE TABLE measurement_y2006m2 PARTITION OF dump_test.measurement\E\n
\QFOR VALUES FROM ('2006-02-01') TO ('2006-03-01');\E\n
\QALTER TABLE ONLY measurement_y2006m2 ALTER COLUMN city_id SET NOT NULL;\E\n
\QALTER TABLE ONLY measurement_y2006m2 ALTER COLUMN logdate SET NOT NULL;\E\n
/xm,
like => {
clean => 1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment