diff --git a/src/backend/distributed/planner/shard_pruning.c b/src/backend/distributed/planner/shard_pruning.c index 719cb2f1b..b05f1674e 100644 --- a/src/backend/distributed/planner/shard_pruning.c +++ b/src/backend/distributed/planner/shard_pruning.c @@ -216,12 +216,19 @@ List * PruneShards(Oid relationId, Index rangeTableId, List *whereClauseList) { DistTableCacheEntry *cacheEntry = DistributedTableCacheEntry(relationId); + int shardCount = cacheEntry->shardIntervalArrayLength; char partitionMethod = cacheEntry->partitionMethod; ClauseWalkerContext context = { 0 }; ListCell *pruneCell; List *prunedList = NIL; bool foundRestriction = false; + /* there are no shards to return */ + if (shardCount == 0) + { + return NIL; + } + /* always return empty result if WHERE clause is of the form: false (AND ..) */ if (ContainsFalseClause(whereClauseList)) { diff --git a/src/test/regress/expected/multi_create_table.out b/src/test/regress/expected/multi_create_table.out index 5ff32cdd2..4d714b46a 100644 --- a/src/test/regress/expected/multi_create_table.out +++ b/src/test/regress/expected/multi_create_table.out @@ -387,6 +387,46 @@ SELECT * FROM data_load_test ORDER BY col1; (2 rows) DROP TABLE data_load_test; +-- test queries on distributed tables with no shards +CREATE TABLE no_shard_test (col1 int, col2 text); +SELECT create_distributed_table('no_shard_test', 'col1', 'append'); + create_distributed_table +-------------------------- + +(1 row) + +SELECT * FROM no_shard_test WHERE col1 > 1; + col1 | col2 +------+------ +(0 rows) + +DROP TABLE no_shard_test; +CREATE TABLE no_shard_test (col1 int, col2 text); +SELECT create_distributed_table('no_shard_test', 'col1', 'range'); + create_distributed_table +-------------------------- + +(1 row) + +SELECT * FROM no_shard_test WHERE col1 > 1; + col1 | col2 +------+------ +(0 rows) + +DROP TABLE no_shard_test; +CREATE TABLE no_shard_test (col1 int, col2 text); +SELECT master_create_distributed_table('no_shard_test', 'col1', 'hash'); + master_create_distributed_table +--------------------------------- + +(1 row) + +SELECT * FROM no_shard_test WHERE col1 > 1; + col1 | col2 +------+------ +(0 rows) + +DROP TABLE no_shard_test; -- ensure writes in the same transaction as create_distributed_table are visible BEGIN; CREATE TABLE data_load_test (col1 int, col2 text, col3 serial); diff --git a/src/test/regress/sql/multi_create_table.sql b/src/test/regress/sql/multi_create_table.sql index c5abd5d62..e2e04da28 100644 --- a/src/test/regress/sql/multi_create_table.sql +++ b/src/test/regress/sql/multi_create_table.sql @@ -206,6 +206,22 @@ SELECT create_distributed_table('data_load_test', 'col1'); SELECT * FROM data_load_test ORDER BY col1; DROP TABLE data_load_test; +-- test queries on distributed tables with no shards +CREATE TABLE no_shard_test (col1 int, col2 text); +SELECT create_distributed_table('no_shard_test', 'col1', 'append'); +SELECT * FROM no_shard_test WHERE col1 > 1; +DROP TABLE no_shard_test; + +CREATE TABLE no_shard_test (col1 int, col2 text); +SELECT create_distributed_table('no_shard_test', 'col1', 'range'); +SELECT * FROM no_shard_test WHERE col1 > 1; +DROP TABLE no_shard_test; + +CREATE TABLE no_shard_test (col1 int, col2 text); +SELECT master_create_distributed_table('no_shard_test', 'col1', 'hash'); +SELECT * FROM no_shard_test WHERE col1 > 1; +DROP TABLE no_shard_test; + -- ensure writes in the same transaction as create_distributed_table are visible BEGIN; CREATE TABLE data_load_test (col1 int, col2 text, col3 serial);