Enable CREATE TABLE PARTITION OF syntax for distributed tables

pull/1467/head
Onder Kalaci 2017-06-23 14:17:55 +03:00
parent c594a056ae
commit 1f8c343d15
6 changed files with 159 additions and 4 deletions

View File

@ -78,9 +78,6 @@ static char LookupDistributionMethod(Oid distributionMethodOid);
static Oid SupportFunctionForColumn(Var *partitionColumn, Oid accessMethodId, static Oid SupportFunctionForColumn(Var *partitionColumn, Oid accessMethodId,
int16 supportFunctionNumber); int16 supportFunctionNumber);
static bool LocalTableEmpty(Oid tableId); static bool LocalTableEmpty(Oid tableId);
static void CreateHashDistributedTable(Oid relationId, char *distributionColumnName,
char *colocateWithTableName,
int shardCount, int replicationFactor);
static Oid ColumnType(Oid relationId, char *columnName); static Oid ColumnType(Oid relationId, char *columnName);
static void CopyLocalDataIntoShards(Oid destinationDistributedRelationId, List * static void CopyLocalDataIntoShards(Oid destinationDistributedRelationId, List *
sourceLocalRelationList); sourceLocalRelationList);
@ -618,7 +615,7 @@ CreateTruncateTrigger(Oid relationId)
/* /*
* CreateHashDistributedTable creates a hash distributed table. * CreateHashDistributedTable creates a hash distributed table.
*/ */
static void void
CreateHashDistributedTable(Oid relationId, char *distributionColumnName, CreateHashDistributedTable(Oid relationId, char *distributionColumnName,
char *colocateWithTableName, int shardCount, char *colocateWithTableName, int shardCount,
int replicationFactor) int replicationFactor)

View File

@ -512,6 +512,41 @@ multi_ProcessUtility(PlannedStmt *pstmt,
} }
} }
#if (PG_VERSION_NUM >= 100000)
if (IsA(parsetree, CreateStmt))
{
CreateStmt *createStatement = (CreateStmt *) parsetree;
/* if a partition is being created */
if (createStatement->inhRelations != NIL && createStatement->partbound != NULL)
{
RangeVar *parentRelation = linitial(createStatement->inhRelations);
char *parentSchemaName = parentRelation->schemaname ?
parentRelation->schemaname : "public";
Oid parentId = get_relname_relid(parentRelation->relname, get_namespace_oid(
parentSchemaName, false));
char *schemaName = createStatement->relation->schemaname ?
createStatement->relation->schemaname : "public";
Oid relationId = get_relname_relid(createStatement->relation->relname,
get_namespace_oid(schemaName, false));
/* if the table is being attached to a distribtued table, it should be distributed as well */
if (IsDistributedTable(parentId))
{
Var *parentPartitionKey = DistPartitionKey(parentId);
char *parentPartitionKeyStr =
get_relid_attribute_name(parentId,
parentPartitionKey->varattno);
CreateHashDistributedTable(relationId, parentPartitionKeyStr,
get_rel_name(parentId), 0, 0);
}
}
}
#endif
/* TODO: fold VACUUM's processing into the above block */ /* TODO: fold VACUUM's processing into the above block */
if (IsA(parsetree, VacuumStmt)) if (IsA(parsetree, VacuumStmt))
{ {

View File

@ -49,5 +49,9 @@ extern void deparse_shard_query(Query *query, Oid distrelid, int64 shardid,
extern char * generate_relation_name(Oid relid, List *namespaces); extern char * generate_relation_name(Oid relid, List *namespaces);
extern char * generate_qualified_relation_name(Oid relid); extern char * generate_qualified_relation_name(Oid relid);
/* TODO: THIS SHOULD NOT BE HERE */
extern void CreateHashDistributedTable(Oid relationId, char *distributionColumnName,
char *colocateWithTableName,
int shardCount, int replicationFactor);
#endif /* CITUS_RULEUTILS_H */ #endif /* CITUS_RULEUTILS_H */

View File

@ -81,6 +81,55 @@ ORDER BY
localhost | 57638 | 6 localhost | 57638 | 6
(2 rows) (2 rows)
-- now create a partition and see that it also becomes a distributed table
CREATE TABLE partitioning_test_2011 PARTITION OF partitioning_test FOR VALUES FROM ('2011-01-01') TO ('2012-01-01');
SELECT
*
FROM
pg_dist_partition
WHERE
logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011')
ORDER BY 1;
logicalrelid | partmethod | partkey | colocationid | repmodel
------------------------+------------+------------------------------------------------------------------------------------------------------------------------+--------------+----------
partitioning_test | h | {VAR :varno 1 :varattno 1 :vartype 23 :vartypmod -1 :varcollid 0 :varlevelsup 0 :varnoold 1 :varoattno 1 :location -1} | 2 | c
partitioning_test_2009 | h | {VAR :varno 1 :varattno 1 :vartype 23 :vartypmod -1 :varcollid 0 :varlevelsup 0 :varnoold 1 :varoattno 1 :location -1} | 2 | c
partitioning_test_2010 | h | {VAR :varno 1 :varattno 1 :vartype 23 :vartypmod -1 :varcollid 0 :varlevelsup 0 :varnoold 1 :varoattno 1 :location -1} | 2 | c
partitioning_test_2011 | h | {VAR :varno 1 :varattno 1 :vartype 23 :vartypmod -1 :varcollid 0 :varlevelsup 0 :varnoold 1 :varoattno 1 :location -1} | 2 | c
(4 rows)
SELECT
logicalrelid, count(*)
FROM pg_dist_shard
WHERE logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011')
GROUP BY
logicalrelid
ORDER BY
1,2;
logicalrelid | count
------------------------+-------
partitioning_test | 4
partitioning_test_2009 | 4
partitioning_test_2010 | 4
partitioning_test_2011 | 4
(4 rows)
SELECT
nodename, nodeport, count(*)
FROM
pg_dist_shard_placement
WHERE
shardid IN (SELECT shardid FROM pg_dist_shard WHERE logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011') )
GROUP BY
nodename, nodeport
ORDER BY
1,2,3;
nodename | nodeport | count
-----------+----------+-------
localhost | 57637 | 8
localhost | 57638 | 8
(2 rows)
-- dropping the parent should CASCADE to the children as well -- dropping the parent should CASCADE to the children as well
DROP TABLE partitioning_test; DROP TABLE partitioning_test;
\d+ partitioning_test* \d+ partitioning_test*

View File

@ -80,6 +80,45 @@ GROUP BY
ORDER BY ORDER BY
1,2,3; 1,2,3;
ERROR: relation "partitioning_test" does not exist ERROR: relation "partitioning_test" does not exist
LINE 6: ...shardid FROM pg_dist_shard WHERE logicalrelid IN ('partition...
^
-- now create a partition and see that it also becomes a distributed table
CREATE TABLE partitioning_test_2011 PARTITION OF partitioning_test FOR VALUES FROM ('2011-01-01') TO ('2012-01-01');
ERROR: syntax error at or near "PARTITION"
LINE 1: CREATE TABLE partitioning_test_2011 PARTITION OF partitionin...
^
SELECT
*
FROM
pg_dist_partition
WHERE
logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011')
ORDER BY 1;
ERROR: relation "partitioning_test" does not exist
LINE 6: logicalrelid IN ('partitioning_test', 'partitioning_test_20...
^
SELECT
logicalrelid, count(*)
FROM pg_dist_shard
WHERE logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011')
GROUP BY
logicalrelid
ORDER BY
1,2;
ERROR: relation "partitioning_test" does not exist
LINE 4: WHERE logicalrelid IN ('partitioning_test', 'partitioning_t...
^
SELECT
nodename, nodeport, count(*)
FROM
pg_dist_shard_placement
WHERE
shardid IN (SELECT shardid FROM pg_dist_shard WHERE logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011') )
GROUP BY
nodename, nodeport
ORDER BY
1,2,3;
ERROR: relation "partitioning_test" does not exist
LINE 6: ...shardid FROM pg_dist_shard WHERE logicalrelid IN ('partition... LINE 6: ...shardid FROM pg_dist_shard WHERE logicalrelid IN ('partition...
^ ^
-- dropping the parent should CASCADE to the children as well -- dropping the parent should CASCADE to the children as well

View File

@ -54,6 +54,37 @@ GROUP BY
ORDER BY ORDER BY
1,2,3; 1,2,3;
-- now create a partition and see that it also becomes a distributed table
CREATE TABLE partitioning_test_2011 PARTITION OF partitioning_test FOR VALUES FROM ('2011-01-01') TO ('2012-01-01');
SELECT
*
FROM
pg_dist_partition
WHERE
logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011')
ORDER BY 1;
SELECT
logicalrelid, count(*)
FROM pg_dist_shard
WHERE logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011')
GROUP BY
logicalrelid
ORDER BY
1,2;
SELECT
nodename, nodeport, count(*)
FROM
pg_dist_shard_placement
WHERE
shardid IN (SELECT shardid FROM pg_dist_shard WHERE logicalrelid IN ('partitioning_test', 'partitioning_test_2009', 'partitioning_test_2010', 'partitioning_test_2011') )
GROUP BY
nodename, nodeport
ORDER BY
1,2,3;
-- dropping the parent should CASCADE to the children as well -- dropping the parent should CASCADE to the children as well
DROP TABLE partitioning_test; DROP TABLE partitioning_test;