diff --git a/create_shards.c b/create_shards.c index e32915d..d314ce0 100644 --- a/create_shards.c +++ b/create_shards.c @@ -121,6 +121,8 @@ master_create_worker_shards(PG_FUNCTION_ARGS) int32 replicationFactor = PG_GETARG_INT32(2); Oid distributedTableId = ResolveRelationId(tableNameText); + char relationKind = get_rel_relkind(distributedTableId); + char shardStorageType = '\0'; int32 shardIndex = 0; List *workerNodeList = NIL; List *ddlCommandList = NIL; @@ -184,6 +186,16 @@ master_create_worker_shards(PG_FUNCTION_ARGS) placementAttemptCount++; } + /* set shard storage type according to relation type */ + if (relationKind == RELKIND_FOREIGN_TABLE) + { + shardStorageType = SHARD_STORAGE_FOREIGN; + } + else + { + shardStorageType = SHARD_STORAGE_TABLE; + } + for (shardIndex = 0; shardIndex < shardCount; shardIndex++) { uint64 shardId = NextSequenceId(SHARD_ID_SEQUENCE_NAME); @@ -248,7 +260,7 @@ master_create_worker_shards(PG_FUNCTION_ARGS) /* insert the shard metadata row along with its min/max values */ minHashTokenText = IntegerToText(shardMinHashToken); maxHashTokenText = IntegerToText(shardMaxHashToken); - InsertShardRow(distributedTableId, shardId, SHARD_STORAGE_TABLE, + InsertShardRow(distributedTableId, shardId, shardStorageType, minHashTokenText, maxHashTokenText); } diff --git a/expected/create_shards.out.tmpl b/expected/create_shards.out.tmpl index c01e6aa..c39d826 100644 --- a/expected/create_shards.out.tmpl +++ b/expected/create_shards.out.tmpl @@ -155,3 +155,73 @@ SELECT COUNT(*) FROM pg_class WHERE relname LIKE 'throwaway%' AND relkind = 'r'; 0 (1 row) +\set VERBOSITY terse +-- test foreign table creation +CREATE FOREIGN TABLE foreign_table_to_distribute +( + name text, + id bigint +) +SERVER fake_fdw_server; +SELECT master_create_distributed_table('foreign_table_to_distribute', 'id'); + master_create_distributed_table +--------------------------------- + +(1 row) + +SELECT master_create_worker_shards('foreign_table_to_distribute', 16, 1); +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" +WARNING: Connection failed to adeadhost:5432 +WARNING: could not create shard on "adeadhost:5432" + master_create_worker_shards +----------------------------- + +(1 row) + +\set VERBOSITY default +SELECT storage, min_value, max_value FROM pgs_distribution_metadata.shard + WHERE relation_id = 'foreign_table_to_distribute'::regclass + ORDER BY (min_value COLLATE "C") ASC; + storage | min_value | max_value +---------+-------------+------------- + f | -1073741828 | -805306374 + f | -1342177283 | -1073741829 + f | -1610612738 | -1342177284 + f | -1879048193 | -1610612739 + f | -2147483648 | -1879048194 + f | -268435463 | -9 + f | -536870918 | -268435464 + f | -8 | 268435446 + f | -805306373 | -536870919 + f | 1073741812 | 1342177266 + f | 1342177267 | 1610612721 + f | 1610612722 | 1879048176 + f | 1879048177 | 2147483647 + f | 268435447 | 536870901 + f | 536870902 | 805306356 + f | 805306357 | 1073741811 +(16 rows) + +-- cleanup foreign table, related shards and shard placements +DELETE FROM pgs_distribution_metadata.shard_placement + WHERE shard_id IN (SELECT shard_id FROM pgs_distribution_metadata.shard + WHERE relation_id = 'foreign_table_to_distribute'::regclass); + +DELETE FROM pgs_distribution_metadata.shard + WHERE relation_id = 'foreign_table_to_distribute'::regclass; + +DELETE FROM pgs_distribution_metadata.partition + WHERE relation_id = 'foreign_table_to_distribute'::regclass; diff --git a/expected/modifications.out.tmpl b/expected/modifications.out.tmpl index 500e998..4f08766 100644 --- a/expected/modifications.out.tmpl +++ b/expected/modifications.out.tmpl @@ -63,12 +63,12 @@ ERROR: cannot plan INSERT using row with NULL value in partition column INSERT INTO limit_orders VALUES (18811, 'BUD', 14962, '2014-04-05 08:32:16', 'sell', -5.00); WARNING: Bad result from localhost:$PGPORT -DETAIL: Remote message: new row for relation "limit_orders_10018" violates check constraint "limit_orders_limit_price_check" +DETAIL: Remote message: new row for relation "limit_orders_10034" violates check constraint "limit_orders_limit_price_check" ERROR: could not modify any active placements -- insert violating primary key constraint INSERT INTO limit_orders VALUES (32743, 'LUV', 5994, '2001-04-16 03:37:28', 'buy', 0.58); WARNING: Bad result from localhost:$PGPORT -DETAIL: Remote message: duplicate key value violates unique constraint "limit_orders_pkey_10019" +DETAIL: Remote message: duplicate key value violates unique constraint "limit_orders_pkey_10035" ERROR: could not modify any active placements -- queries with non-constant partition values are unsupported INSERT INTO limit_orders VALUES (random() * 100, 'ORCL', 152, '2011-08-25 11:50:45', diff --git a/sql/create_shards.sql.tmpl b/sql/create_shards.sql.tmpl index 09798eb..8de9dd8 100644 --- a/sql/create_shards.sql.tmpl +++ b/sql/create_shards.sql.tmpl @@ -79,3 +79,32 @@ SELECT sort_names('sumedh', 'jason', 'ozgun'); SELECT create_table_then_fail('localhost', $PGPORT); SELECT COUNT(*) FROM pg_class WHERE relname LIKE 'throwaway%' AND relkind = 'r'; + +\set VERBOSITY terse + +-- test foreign table creation +CREATE FOREIGN TABLE foreign_table_to_distribute +( + name text, + id bigint +) +SERVER fake_fdw_server; + +SELECT master_create_distributed_table('foreign_table_to_distribute', 'id'); +SELECT master_create_worker_shards('foreign_table_to_distribute', 16, 1); + +\set VERBOSITY default +SELECT storage, min_value, max_value FROM pgs_distribution_metadata.shard + WHERE relation_id = 'foreign_table_to_distribute'::regclass + ORDER BY (min_value COLLATE "C") ASC; + +-- cleanup foreign table, related shards and shard placements +DELETE FROM pgs_distribution_metadata.shard_placement + WHERE shard_id IN (SELECT shard_id FROM pgs_distribution_metadata.shard + WHERE relation_id = 'foreign_table_to_distribute'::regclass); + +DELETE FROM pgs_distribution_metadata.shard + WHERE relation_id = 'foreign_table_to_distribute'::regclass; + +DELETE FROM pgs_distribution_metadata.partition + WHERE relation_id = 'foreign_table_to_distribute'::regclass;