Commit 4b9a98e1 authored by Robert Haas's avatar Robert Haas

Clean up code, comments, and formatting for table partitioning.

Amit Langote, plus pgindent-ing by me.  Inspired in part by review
comments from Tomas Vondra.
parent acddbe22
...@@ -1887,6 +1887,10 @@ heap_drop_with_catalog(Oid relid) ...@@ -1887,6 +1887,10 @@ heap_drop_with_catalog(Oid relid)
if (parent) if (parent)
{ {
/*
* Invalidate the parent's relcache so that the partition is no longer
* included in its partition descriptor.
*/
CacheInvalidateRelcache(parent); CacheInvalidateRelcache(parent);
heap_close(parent, NoLock); /* keep the lock */ heap_close(parent, NoLock); /* keep the lock */
} }
......
...@@ -1492,7 +1492,7 @@ generate_partition_qual(Relation rel, bool recurse) ...@@ -1492,7 +1492,7 @@ generate_partition_qual(Relation rel, bool recurse)
* Construct values[] and isnull[] arrays for the partition key * Construct values[] and isnull[] arrays for the partition key
* of a tuple. * of a tuple.
* *
* pkinfo partition key execution info * pd Partition dispatch object of the partitioned table
* slot Heap tuple from which to extract partition key * slot Heap tuple from which to extract partition key
* estate executor state for evaluating any partition key * estate executor state for evaluating any partition key
* expressions (must be non-NULL) * expressions (must be non-NULL)
...@@ -1565,7 +1565,7 @@ FormPartitionKeyDatum(PartitionDispatch pd, ...@@ -1565,7 +1565,7 @@ FormPartitionKeyDatum(PartitionDispatch pd,
* the latter case. * the latter case.
*/ */
int int
get_partition_for_tuple(PartitionDispatch * pd, get_partition_for_tuple(PartitionDispatch *pd,
TupleTableSlot *slot, TupleTableSlot *slot,
EState *estate, EState *estate,
Oid *failed_at) Oid *failed_at)
......
...@@ -161,11 +161,11 @@ typedef struct CopyStateData ...@@ -161,11 +161,11 @@ typedef struct CopyStateData
ExprState **defexprs; /* array of default att expressions */ ExprState **defexprs; /* array of default att expressions */
bool volatile_defexprs; /* is any of defexprs volatile? */ bool volatile_defexprs; /* is any of defexprs volatile? */
List *range_table; List *range_table;
PartitionDispatch *partition_dispatch_info; PartitionDispatch *partition_dispatch_info;
int num_dispatch; int num_dispatch;
int num_partitions; int num_partitions;
ResultRelInfo *partitions; ResultRelInfo *partitions;
TupleConversionMap **partition_tupconv_maps; TupleConversionMap **partition_tupconv_maps;
/* /*
* These variables are used to reduce overhead in textual COPY FROM. * These variables are used to reduce overhead in textual COPY FROM.
...@@ -1403,31 +1403,28 @@ BeginCopy(ParseState *pstate, ...@@ -1403,31 +1403,28 @@ BeginCopy(ParseState *pstate,
errmsg("table \"%s\" does not have OIDs", errmsg("table \"%s\" does not have OIDs",
RelationGetRelationName(cstate->rel)))); RelationGetRelationName(cstate->rel))));
/* /* Initialize state for CopyFrom tuple routing. */
* Initialize state for CopyFrom tuple routing. Watch out for
* any foreign partitions.
*/
if (is_from && rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) if (is_from && rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
{ {
PartitionDispatch *pd; List *leaf_parts;
List *leaf_parts; ListCell *cell;
ListCell *cell; int i,
int i, num_parted;
num_parted, ResultRelInfo *leaf_part_rri;
num_leaf_parts;
ResultRelInfo *leaf_part_rri;
/* Get the tuple-routing information and lock partitions */ /* Get the tuple-routing information and lock partitions */
pd = RelationGetPartitionDispatchInfo(rel, RowExclusiveLock, cstate->partition_dispatch_info =
&num_parted, &leaf_parts); RelationGetPartitionDispatchInfo(rel, RowExclusiveLock,
num_leaf_parts = list_length(leaf_parts); &num_parted,
cstate->partition_dispatch_info = pd; &leaf_parts);
cstate->num_dispatch = num_parted; cstate->num_dispatch = num_parted;
cstate->num_partitions = num_leaf_parts; cstate->num_partitions = list_length(leaf_parts);
cstate->partitions = (ResultRelInfo *) palloc(num_leaf_parts * cstate->partitions = (ResultRelInfo *)
sizeof(ResultRelInfo)); palloc(cstate->num_partitions *
sizeof(ResultRelInfo));
cstate->partition_tupconv_maps = (TupleConversionMap **) cstate->partition_tupconv_maps = (TupleConversionMap **)
palloc0(num_leaf_parts * sizeof(TupleConversionMap *)); palloc0(cstate->num_partitions *
sizeof(TupleConversionMap *));
leaf_part_rri = cstate->partitions; leaf_part_rri = cstate->partitions;
i = 0; i = 0;
...@@ -1438,8 +1435,8 @@ BeginCopy(ParseState *pstate, ...@@ -1438,8 +1435,8 @@ BeginCopy(ParseState *pstate,
/* /*
* We locked all the partitions above including the leaf * We locked all the partitions above including the leaf
* partitions. Note that each of the relations in * partitions. Note that each of the relations in
* cstate->partitions will be closed by CopyFrom() after * cstate->partitions will be closed by CopyFrom() after it's
* it's finished with its processing. * finished with its processing.
*/ */
partrel = heap_open(lfirst_oid(cell), NoLock); partrel = heap_open(lfirst_oid(cell), NoLock);
...@@ -1451,8 +1448,9 @@ BeginCopy(ParseState *pstate, ...@@ -1451,8 +1448,9 @@ BeginCopy(ParseState *pstate,
InitResultRelInfo(leaf_part_rri, InitResultRelInfo(leaf_part_rri,
partrel, partrel,
1, /* dummy */ 1, /* dummy */
false, /* no partition constraint check */ false, /* no partition constraint
* check */
0); 0);
/* Open partition indices */ /* Open partition indices */
...@@ -1460,9 +1458,9 @@ BeginCopy(ParseState *pstate, ...@@ -1460,9 +1458,9 @@ BeginCopy(ParseState *pstate,
if (!equalTupleDescs(tupDesc, RelationGetDescr(partrel))) if (!equalTupleDescs(tupDesc, RelationGetDescr(partrel)))
cstate->partition_tupconv_maps[i] = cstate->partition_tupconv_maps[i] =
convert_tuples_by_name(tupDesc, convert_tuples_by_name(tupDesc,
RelationGetDescr(partrel), RelationGetDescr(partrel),
gettext_noop("could not convert row type")); gettext_noop("could not convert row type"));
leaf_part_rri++; leaf_part_rri++;
i++; i++;
} }
...@@ -2486,8 +2484,8 @@ CopyFrom(CopyState cstate) ...@@ -2486,8 +2484,8 @@ CopyFrom(CopyState cstate)
* BEFORE/INSTEAD OF triggers, or we need to evaluate volatile default * BEFORE/INSTEAD OF triggers, or we need to evaluate volatile default
* expressions. Such triggers or expressions might query the table we're * expressions. Such triggers or expressions might query the table we're
* inserting to, and act differently if the tuples that have already been * inserting to, and act differently if the tuples that have already been
* processed and prepared for insertion are not there. We also can't * processed and prepared for insertion are not there. We also can't do
* do it if the table is partitioned. * it if the table is partitioned.
*/ */
if ((resultRelInfo->ri_TrigDesc != NULL && if ((resultRelInfo->ri_TrigDesc != NULL &&
(resultRelInfo->ri_TrigDesc->trig_insert_before_row || (resultRelInfo->ri_TrigDesc->trig_insert_before_row ||
...@@ -2572,7 +2570,7 @@ CopyFrom(CopyState cstate) ...@@ -2572,7 +2570,7 @@ CopyFrom(CopyState cstate)
/* Determine the partition to heap_insert the tuple into */ /* Determine the partition to heap_insert the tuple into */
if (cstate->partition_dispatch_info) if (cstate->partition_dispatch_info)
{ {
int leaf_part_index; int leaf_part_index;
TupleConversionMap *map; TupleConversionMap *map;
/* /*
...@@ -2584,7 +2582,7 @@ CopyFrom(CopyState cstate) ...@@ -2584,7 +2582,7 @@ CopyFrom(CopyState cstate)
* partition, respectively. * partition, respectively.
*/ */
leaf_part_index = ExecFindPartition(resultRelInfo, leaf_part_index = ExecFindPartition(resultRelInfo,
cstate->partition_dispatch_info, cstate->partition_dispatch_info,
slot, slot,
estate); estate);
Assert(leaf_part_index >= 0 && Assert(leaf_part_index >= 0 &&
...@@ -2601,7 +2599,7 @@ CopyFrom(CopyState cstate) ...@@ -2601,7 +2599,7 @@ CopyFrom(CopyState cstate)
if (resultRelInfo->ri_FdwRoutine) if (resultRelInfo->ri_FdwRoutine)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot route inserted tuples to a foreign table"))); errmsg("cannot route inserted tuples to a foreign table")));
/* /*
* For ExecInsertIndexTuples() to work on the partition's indexes * For ExecInsertIndexTuples() to work on the partition's indexes
...@@ -2752,7 +2750,7 @@ CopyFrom(CopyState cstate) ...@@ -2752,7 +2750,7 @@ CopyFrom(CopyState cstate)
/* Close all the partitioned tables, leaf partitions, and their indices */ /* Close all the partitioned tables, leaf partitions, and their indices */
if (cstate->partition_dispatch_info) if (cstate->partition_dispatch_info)
{ {
int i; int i;
/* /*
* Remember cstate->partition_dispatch_info[0] corresponds to the root * Remember cstate->partition_dispatch_info[0] corresponds to the root
......
This diff is collapsed.
...@@ -1718,26 +1718,26 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) ...@@ -1718,26 +1718,26 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
if (operation == CMD_INSERT && if (operation == CMD_INSERT &&
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
{ {
PartitionDispatch *pd;
int i, int i,
j, j,
num_parted, num_parted;
num_leaf_parts;
List *leaf_parts; List *leaf_parts;
ListCell *cell; ListCell *cell;
ResultRelInfo *leaf_part_rri; ResultRelInfo *leaf_part_rri;
/* Form the partition node tree and lock partitions */ /* Get the tuple-routing information and lock partitions */
pd = RelationGetPartitionDispatchInfo(rel, RowExclusiveLock, mtstate->mt_partition_dispatch_info =
&num_parted, &leaf_parts); RelationGetPartitionDispatchInfo(rel, RowExclusiveLock,
mtstate->mt_partition_dispatch_info = pd; &num_parted,
&leaf_parts);
mtstate->mt_num_dispatch = num_parted; mtstate->mt_num_dispatch = num_parted;
num_leaf_parts = list_length(leaf_parts); mtstate->mt_num_partitions = list_length(leaf_parts);
mtstate->mt_num_partitions = num_leaf_parts;
mtstate->mt_partitions = (ResultRelInfo *) mtstate->mt_partitions = (ResultRelInfo *)
palloc0(num_leaf_parts * sizeof(ResultRelInfo)); palloc0(mtstate->mt_num_partitions *
sizeof(ResultRelInfo));
mtstate->mt_partition_tupconv_maps = (TupleConversionMap **) mtstate->mt_partition_tupconv_maps = (TupleConversionMap **)
palloc0(num_leaf_parts * sizeof(TupleConversionMap *)); palloc0(mtstate->mt_num_partitions *
sizeof(TupleConversionMap *));
leaf_part_rri = mtstate->mt_partitions; leaf_part_rri = mtstate->mt_partitions;
i = j = 0; i = j = 0;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/include/catalog/pg_partitioned_table.h $ * src/include/catalog/pg_partitioned_table.h
* *
* NOTES * NOTES
* the genbki.sh script reads this file and generates .bki * the genbki.sh script reads this file and generates .bki
...@@ -29,9 +29,9 @@ ...@@ -29,9 +29,9 @@
CATALOG(pg_partitioned_table,3350) BKI_WITHOUT_OIDS CATALOG(pg_partitioned_table,3350) BKI_WITHOUT_OIDS
{ {
Oid partrelid; /* partitioned table oid */ Oid partrelid; /* partitioned table oid */
char partstrat; /* partitioning strategy */ char partstrat; /* partitioning strategy */
int16 partnatts; /* number of partition key columns */ int16 partnatts; /* number of partition key columns */
/* /*
* variable-length fields start here, but we allow direct access to * variable-length fields start here, but we allow direct access to
...@@ -39,29 +39,27 @@ CATALOG(pg_partitioned_table,3350) BKI_WITHOUT_OIDS ...@@ -39,29 +39,27 @@ CATALOG(pg_partitioned_table,3350) BKI_WITHOUT_OIDS
* field of a heap tuple can be reliably accessed using its C struct * field of a heap tuple can be reliably accessed using its C struct
* offset, as previous fields are all non-nullable fixed-length fields. * offset, as previous fields are all non-nullable fixed-length fields.
*/ */
int2vector partattrs; /* each member of the array is the int2vector partattrs; /* each member of the array is the attribute
* attribute number of a partition key * number of a partition key column, or 0 if
* column, or 0 if the column is actually * the column is actually an expression */
* an expression */
#ifdef CATALOG_VARLEN #ifdef CATALOG_VARLEN
oidvector partclass; /* operator class to compare keys */ oidvector partclass; /* operator class to compare keys */
oidvector partcollation; /* user-specified collation for keys */ oidvector partcollation; /* user-specified collation for keys */
pg_node_tree partexprs; /* list of expressions in the partitioning pg_node_tree partexprs; /* list of expressions in the partition key;
* key; one item for each zero entry in * one item for each zero entry in partattrs[] */
* partattrs[] */
#endif #endif
} FormData_pg_partitioned_table; } FormData_pg_partitioned_table;
/* ---------------- /* ----------------
* Form_pg_partitioned_table corresponds to a pointer to a tuple with * Form_pg_partitioned_table corresponds to a pointer to a tuple with
* the format of pg_partitioned_table relation. * the format of pg_partitioned_table relation.
* ---------------- * ----------------
*/ */
typedef FormData_pg_partitioned_table *Form_pg_partitioned_table; typedef FormData_pg_partitioned_table *Form_pg_partitioned_table;
/* ---------------- /* ----------------
* compiler constants for pg_partitioned_table * compiler constants for pg_partitioned_table
* ---------------- * ----------------
*/ */
#define Natts_pg_partitioned_table 7 #define Natts_pg_partitioned_table 7
...@@ -70,7 +68,7 @@ typedef FormData_pg_partitioned_table *Form_pg_partitioned_table; ...@@ -70,7 +68,7 @@ typedef FormData_pg_partitioned_table *Form_pg_partitioned_table;
#define Anum_pg_partitioned_table_partnatts 3 #define Anum_pg_partitioned_table_partnatts 3
#define Anum_pg_partitioned_table_partattrs 4 #define Anum_pg_partitioned_table_partattrs 4
#define Anum_pg_partitioned_table_partclass 5 #define Anum_pg_partitioned_table_partclass 5
#define Anum_pg_partitioned_table_partcollation 6 #define Anum_pg_partitioned_table_partcollation 6
#define Anum_pg_partitioned_table_partexprs 7 #define Anum_pg_partitioned_table_partexprs 7
#endif /* PG_PARTITIONED_TABLE_H */ #endif /* PG_PARTITIONED_TABLE_H */
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