]> granicus.if.org Git - postgresql/commitdiff
Make tablesample work with partitioned tables.
authorRobert Haas <rhaas@postgresql.org>
Fri, 24 Feb 2017 06:51:46 +0000 (12:21 +0530)
committerRobert Haas <rhaas@postgresql.org>
Fri, 24 Feb 2017 06:53:28 +0000 (12:23 +0530)
This was an oversight in the original partitioning commit.

Amit Langote, reviewed by David Fetter

Discussion: http://postgr.es/m/59af6590-8ace-04c4-c36c-ea35d435c60e@lab.ntt.co.jp

src/backend/parser/parse_clause.c
src/test/regress/expected/tablesample.out
src/test/regress/sql/tablesample.sql

index fecc1d65984c99ebb5dd46884c421bf813089329..b5eae56006d2dec14319dae120014f0c4c22f499 100644 (file)
@@ -907,7 +907,8 @@ transformFromClauseItem(ParseState *pstate, Node *n,
                rte = rt_fetch(rtr->rtindex, pstate->p_rtable);
                /* We only support this on plain relations and matviews */
                if (rte->relkind != RELKIND_RELATION &&
-                       rte->relkind != RELKIND_MATVIEW)
+                       rte->relkind != RELKIND_MATVIEW &&
+                       rte->relkind != RELKIND_PARTITIONED_TABLE)
                        ereport(ERROR,
                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                         errmsg("TABLESAMPLE clause can only be applied to tables and materialized views"),
index 7e91b958aea22b4e9960ba624a08dac3ee309d82..b18e420e9b5f2f80898645aa47c974d56cc35b4e 100644 (file)
@@ -313,3 +313,21 @@ SELECT q.* FROM (SELECT * FROM test_tablesample) as q TABLESAMPLE BERNOULLI (5);
 ERROR:  syntax error at or near "TABLESAMPLE"
 LINE 1: ...CT q.* FROM (SELECT * FROM test_tablesample) as q TABLESAMPL...
                                                              ^
+-- check partitioned tables support tablesample
+create table parted_sample (a int) partition by list (a);
+create table parted_sample_1 partition of parted_sample for values in (1);
+create table parted_sample_2 partition of parted_sample for values in (2);
+explain (costs off)
+  select * from parted_sample tablesample bernoulli (100);
+                QUERY PLAN                 
+-------------------------------------------
+ Append
+   ->  Sample Scan on parted_sample
+         Sampling: bernoulli ('100'::real)
+   ->  Sample Scan on parted_sample_1
+         Sampling: bernoulli ('100'::real)
+   ->  Sample Scan on parted_sample_2
+         Sampling: bernoulli ('100'::real)
+(7 rows)
+
+drop table parted_sample, parted_sample_1, parted_sample_2;
index eec97934966966229800a47563153669b5ea353a..c39fe4b7509c37a11a92a3150f67f44029fc1fb6 100644 (file)
@@ -100,3 +100,11 @@ WITH query_select AS (SELECT * FROM test_tablesample)
 SELECT * FROM query_select TABLESAMPLE BERNOULLI (5.5) REPEATABLE (1);
 
 SELECT q.* FROM (SELECT * FROM test_tablesample) as q TABLESAMPLE BERNOULLI (5);
+
+-- check partitioned tables support tablesample
+create table parted_sample (a int) partition by list (a);
+create table parted_sample_1 partition of parted_sample for values in (1);
+create table parted_sample_2 partition of parted_sample for values in (2);
+explain (costs off)
+  select * from parted_sample tablesample bernoulli (100);
+drop table parted_sample, parted_sample_1, parted_sample_2;