1 CREATE TABLE test_tablesample (id int, name text) WITH (fillfactor=10);
2 -- use fillfactor so we don't have to load too much data to get multiple pages
3 INSERT INTO test_tablesample
4 SELECT i, repeat(i::text, 200) FROM generate_series(0, 9) s(i);
5 SELECT t.id FROM test_tablesample AS t TABLESAMPLE SYSTEM (50) REPEATABLE (0);
16 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (100.0/11) REPEATABLE (0);
21 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (50) REPEATABLE (0);
32 SELECT id FROM test_tablesample TABLESAMPLE BERNOULLI (50) REPEATABLE (0);
42 SELECT id FROM test_tablesample TABLESAMPLE BERNOULLI (5.5) REPEATABLE (0);
48 -- 100% should give repeatable count results (ie, all rows) in any case
49 SELECT count(*) FROM test_tablesample TABLESAMPLE SYSTEM (100);
55 SELECT count(*) FROM test_tablesample TABLESAMPLE SYSTEM (100) REPEATABLE (1+2);
61 SELECT count(*) FROM test_tablesample TABLESAMPLE SYSTEM (100) REPEATABLE (0.4);
67 CREATE VIEW test_tablesample_v1 AS
68 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (10*2) REPEATABLE (2);
69 CREATE VIEW test_tablesample_v2 AS
70 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (99);
71 \d+ test_tablesample_v1
72 View "public.test_tablesample_v1"
73 Column | Type | Collation | Nullable | Default | Storage | Description
74 --------+---------+-----------+----------+---------+---------+-------------
75 id | integer | | | | plain |
77 SELECT test_tablesample.id
78 FROM test_tablesample TABLESAMPLE system ((10 * 2)) REPEATABLE (2);
80 \d+ test_tablesample_v2
81 View "public.test_tablesample_v2"
82 Column | Type | Collation | Nullable | Default | Storage | Description
83 --------+---------+-----------+----------+---------+---------+-------------
84 id | integer | | | | plain |
86 SELECT test_tablesample.id
87 FROM test_tablesample TABLESAMPLE system (99);
89 -- check a sampled query doesn't affect cursor in progress
91 DECLARE tablesample_cur CURSOR FOR
92 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (50) REPEATABLE (0);
93 FETCH FIRST FROM tablesample_cur;
99 FETCH NEXT FROM tablesample_cur;
105 FETCH NEXT FROM tablesample_cur;
111 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (50) REPEATABLE (0);
122 FETCH NEXT FROM tablesample_cur;
128 FETCH NEXT FROM tablesample_cur;
134 FETCH NEXT FROM tablesample_cur;
140 FETCH FIRST FROM tablesample_cur;
146 FETCH NEXT FROM tablesample_cur;
152 FETCH NEXT FROM tablesample_cur;
158 FETCH NEXT FROM tablesample_cur;
164 FETCH NEXT FROM tablesample_cur;
170 FETCH NEXT FROM tablesample_cur;
176 CLOSE tablesample_cur;
179 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (50) REPEATABLE (2);
181 --------------------------------------------------------------------
182 Sample Scan on test_tablesample
183 Sampling: system ('50'::real) REPEATABLE ('2'::double precision)
187 SELECT * FROM test_tablesample_v1;
189 --------------------------------------------------------------------
190 Sample Scan on test_tablesample
191 Sampling: system ('20'::real) REPEATABLE ('2'::double precision)
194 -- check inheritance behavior
196 select count(*) from person tablesample bernoulli (100);
198 -------------------------------------------------
201 -> Sample Scan on person person_1
202 Sampling: bernoulli ('100'::real)
203 -> Sample Scan on emp person_2
204 Sampling: bernoulli ('100'::real)
205 -> Sample Scan on student person_3
206 Sampling: bernoulli ('100'::real)
207 -> Sample Scan on stud_emp person_4
208 Sampling: bernoulli ('100'::real)
211 select count(*) from person tablesample bernoulli (100);
217 select count(*) from person;
223 -- check that collations get assigned within the tablesample arguments
224 SELECT count(*) FROM test_tablesample TABLESAMPLE bernoulli (('1'::text < '0'::text)::int);
230 -- check behavior during rescans, as well as correct handling of min/max pct
232 (values (0),(100)) v(pct),
233 lateral (select count(*) from tenk1 tablesample bernoulli (pct)) ss;
241 (values (0),(100)) v(pct),
242 lateral (select count(*) from tenk1 tablesample system (pct)) ss;
250 select pct, count(unique1) from
251 (values (0),(100)) v(pct),
252 lateral (select * from tenk1 tablesample bernoulli (pct)) ss
255 --------------------------------------------------------
257 Group Key: "*VALUES*".column1
259 -> Values Scan on "*VALUES*"
260 -> Sample Scan on tenk1
261 Sampling: bernoulli ("*VALUES*".column1)
264 select pct, count(unique1) from
265 (values (0),(100)) v(pct),
266 lateral (select * from tenk1 tablesample bernoulli (pct)) ss
273 select pct, count(unique1) from
274 (values (0),(100)) v(pct),
275 lateral (select * from tenk1 tablesample system (pct)) ss
283 SELECT id FROM test_tablesample TABLESAMPLE FOOBAR (1);
284 ERROR: tablesample method foobar does not exist
285 LINE 1: SELECT id FROM test_tablesample TABLESAMPLE FOOBAR (1);
287 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (NULL);
288 ERROR: TABLESAMPLE parameter cannot be null
289 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (50) REPEATABLE (NULL);
290 ERROR: TABLESAMPLE REPEATABLE parameter cannot be null
291 SELECT id FROM test_tablesample TABLESAMPLE BERNOULLI (-1);
292 ERROR: sample percentage must be between 0 and 100
293 SELECT id FROM test_tablesample TABLESAMPLE BERNOULLI (200);
294 ERROR: sample percentage must be between 0 and 100
295 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (-1);
296 ERROR: sample percentage must be between 0 and 100
297 SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (200);
298 ERROR: sample percentage must be between 0 and 100
299 SELECT id FROM test_tablesample_v1 TABLESAMPLE BERNOULLI (1);
300 ERROR: TABLESAMPLE clause can only be applied to tables and materialized views
301 LINE 1: SELECT id FROM test_tablesample_v1 TABLESAMPLE BERNOULLI (1)...
303 INSERT INTO test_tablesample_v1 VALUES(1);
304 ERROR: cannot insert into view "test_tablesample_v1"
305 DETAIL: Views containing TABLESAMPLE are not automatically updatable.
306 HINT: To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
307 WITH query_select AS (SELECT * FROM test_tablesample)
308 SELECT * FROM query_select TABLESAMPLE BERNOULLI (5.5) REPEATABLE (1);
309 ERROR: TABLESAMPLE clause can only be applied to tables and materialized views
310 LINE 2: SELECT * FROM query_select TABLESAMPLE BERNOULLI (5.5) REPEA...
312 SELECT q.* FROM (SELECT * FROM test_tablesample) as q TABLESAMPLE BERNOULLI (5);
313 ERROR: syntax error at or near "TABLESAMPLE"
314 LINE 1: ...CT q.* FROM (SELECT * FROM test_tablesample) as q TABLESAMPL...
316 -- check partitioned tables support tablesample
317 create table parted_sample (a int) partition by list (a);
318 create table parted_sample_1 partition of parted_sample for values in (1);
319 create table parted_sample_2 partition of parted_sample for values in (2);
321 select * from parted_sample tablesample bernoulli (100);
323 -------------------------------------------
325 -> Sample Scan on parted_sample_1
326 Sampling: bernoulli ('100'::real)
327 -> Sample Scan on parted_sample_2
328 Sampling: bernoulli ('100'::real)
331 drop table parted_sample, parted_sample_1, parted_sample_2;