3 -- Check the LIMIT/OFFSET feature of SELECT
5 SELECT ''::text AS two, unique1, unique2, stringu1
6 FROM onek WHERE unique1 > 50
7 ORDER BY unique1 LIMIT 2;
8 two | unique1 | unique2 | stringu1
9 -----+---------+---------+----------
14 SELECT ''::text AS five, unique1, unique2, stringu1
15 FROM onek WHERE unique1 > 60
16 ORDER BY unique1 LIMIT 5;
17 five | unique1 | unique2 | stringu1
18 ------+---------+---------+----------
26 SELECT ''::text AS two, unique1, unique2, stringu1
27 FROM onek WHERE unique1 > 60 AND unique1 < 63
28 ORDER BY unique1 LIMIT 5;
29 two | unique1 | unique2 | stringu1
30 -----+---------+---------+----------
35 SELECT ''::text AS three, unique1, unique2, stringu1
36 FROM onek WHERE unique1 > 100
37 ORDER BY unique1 LIMIT 3 OFFSET 20;
38 three | unique1 | unique2 | stringu1
39 -------+---------+---------+----------
45 SELECT ''::text AS zero, unique1, unique2, stringu1
46 FROM onek WHERE unique1 < 50
47 ORDER BY unique1 DESC LIMIT 8 OFFSET 99;
48 zero | unique1 | unique2 | stringu1
49 ------+---------+---------+----------
52 SELECT ''::text AS eleven, unique1, unique2, stringu1
53 FROM onek WHERE unique1 < 50
54 ORDER BY unique1 DESC LIMIT 20 OFFSET 39;
55 eleven | unique1 | unique2 | stringu1
56 --------+---------+---------+----------
70 SELECT ''::text AS ten, unique1, unique2, stringu1
72 ORDER BY unique1 OFFSET 990;
73 ten | unique1 | unique2 | stringu1
74 -----+---------+---------+----------
87 SELECT ''::text AS five, unique1, unique2, stringu1
89 ORDER BY unique1 OFFSET 990 LIMIT 5;
90 five | unique1 | unique2 | stringu1
91 ------+---------+---------+----------
99 SELECT ''::text AS five, unique1, unique2, stringu1
101 ORDER BY unique1 LIMIT 5 OFFSET 900;
102 five | unique1 | unique2 | stringu1
103 ------+---------+---------+----------
111 -- Test null limit and offset. The planner would discard a simple null
112 -- constant, so to ensure executor is exercised, do this:
113 select * from int8_tbl limit (case when random() < 0.5 then null::bigint end);
115 ------------------+-------------------
117 123 | 4567890123456789
118 4567890123456789 | 123
119 4567890123456789 | 4567890123456789
120 4567890123456789 | -4567890123456789
123 select * from int8_tbl offset (case when random() < 0.5 then null::bigint end);
125 ------------------+-------------------
127 123 | 4567890123456789
128 4567890123456789 | 123
129 4567890123456789 | 4567890123456789
130 4567890123456789 | -4567890123456789
133 -- Test assorted cases involving backwards fetch from a LIMIT plan node
135 declare c1 cursor for select * from int8_tbl limit 10;
138 ------------------+-------------------
140 123 | 4567890123456789
141 4567890123456789 | 123
142 4567890123456789 | 4567890123456789
143 4567890123456789 | -4567890123456789
151 fetch backward 1 in c1;
153 ------------------+-------------------
154 4567890123456789 | -4567890123456789
157 fetch backward all in c1;
159 ------------------+------------------
160 4567890123456789 | 4567890123456789
161 4567890123456789 | 123
162 123 | 4567890123456789
166 fetch backward 1 in c1;
173 ------------------+-------------------
175 123 | 4567890123456789
176 4567890123456789 | 123
177 4567890123456789 | 4567890123456789
178 4567890123456789 | -4567890123456789
181 declare c2 cursor for select * from int8_tbl limit 3;
184 ------------------+------------------
186 123 | 4567890123456789
187 4567890123456789 | 123
195 fetch backward 1 in c2;
197 ------------------+-----
198 4567890123456789 | 123
201 fetch backward all in c2;
203 -----+------------------
204 123 | 4567890123456789
208 fetch backward 1 in c2;
215 ------------------+------------------
217 123 | 4567890123456789
218 4567890123456789 | 123
221 declare c3 cursor for select * from int8_tbl offset 3;
224 ------------------+-------------------
225 4567890123456789 | 4567890123456789
226 4567890123456789 | -4567890123456789
234 fetch backward 1 in c3;
236 ------------------+-------------------
237 4567890123456789 | -4567890123456789
240 fetch backward all in c3;
242 ------------------+------------------
243 4567890123456789 | 4567890123456789
246 fetch backward 1 in c3;
253 ------------------+-------------------
254 4567890123456789 | 4567890123456789
255 4567890123456789 | -4567890123456789
258 declare c4 cursor for select * from int8_tbl offset 10;
269 fetch backward 1 in c4;
274 fetch backward all in c4;
279 fetch backward 1 in c4;
289 declare c5 cursor for select * from int8_tbl order by q1 fetch first 2 rows with ties;
292 -----+------------------
294 123 | 4567890123456789
302 fetch backward 1 in c5;
304 -----+------------------
305 123 | 4567890123456789
308 fetch backward 1 in c5;
316 -----+------------------
317 123 | 4567890123456789
320 fetch backward all in c5;
322 -----+------------------
323 123 | 4567890123456789
329 -----+------------------
331 123 | 4567890123456789
334 fetch backward all in c5;
336 -----+------------------
337 123 | 4567890123456789
342 -- Stress test for variable LIMIT in conjunction with bounded-heap sorting
345 FROM (VALUES (1)) AS x,
346 (SELECT n FROM generate_series(1,10) AS n
347 ORDER BY n LIMIT 1 OFFSET s-1) AS y) AS z
348 FROM generate_series(1,10) AS s;
364 -- Test behavior of volatile and set-returning functions in conjunction
365 -- with ORDER BY and LIMIT.
367 create temp sequence testseq;
368 explain (verbose, costs off)
369 select unique1, unique2, nextval('testseq')
370 from tenk1 order by unique2 limit 10;
372 ----------------------------------------------------------------
374 Output: unique1, unique2, (nextval('testseq'::regclass))
375 -> Index Scan using tenk1_unique2 on public.tenk1
376 Output: unique1, unique2, nextval('testseq'::regclass)
379 select unique1, unique2, nextval('testseq')
380 from tenk1 order by unique2 limit 10;
381 unique1 | unique2 | nextval
382 ---------+---------+---------
395 select currval('testseq');
401 explain (verbose, costs off)
402 select unique1, unique2, nextval('testseq')
403 from tenk1 order by tenthous limit 10;
405 --------------------------------------------------------------------------
407 Output: unique1, unique2, (nextval('testseq'::regclass)), tenthous
409 Output: unique1, unique2, nextval('testseq'::regclass), tenthous
411 Output: unique1, unique2, tenthous
412 Sort Key: tenk1.tenthous
413 -> Seq Scan on public.tenk1
414 Output: unique1, unique2, tenthous
417 select unique1, unique2, nextval('testseq')
418 from tenk1 order by tenthous limit 10;
419 unique1 | unique2 | nextval
420 ---------+---------+---------
433 select currval('testseq');
439 explain (verbose, costs off)
440 select unique1, unique2, generate_series(1,10)
441 from tenk1 order by unique2 limit 7;
443 -------------------------------------------------------------------------------------------------------------------------------------------------------------
445 Output: unique1, unique2, (generate_series(1, 10))
447 Output: unique1, unique2, generate_series(1, 10)
448 -> Index Scan using tenk1_unique2 on public.tenk1
449 Output: unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even, stringu1, stringu2, string4
452 select unique1, unique2, generate_series(1,10)
453 from tenk1 order by unique2 limit 7;
454 unique1 | unique2 | generate_series
455 ---------+---------+-----------------
465 explain (verbose, costs off)
466 select unique1, unique2, generate_series(1,10)
467 from tenk1 order by tenthous limit 7;
469 --------------------------------------------------------------------
471 Output: unique1, unique2, (generate_series(1, 10)), tenthous
473 Output: unique1, unique2, generate_series(1, 10), tenthous
475 Output: unique1, unique2, tenthous
476 Sort Key: tenk1.tenthous
477 -> Seq Scan on public.tenk1
478 Output: unique1, unique2, tenthous
481 select unique1, unique2, generate_series(1,10)
482 from tenk1 order by tenthous limit 7;
483 unique1 | unique2 | generate_series
484 ---------+---------+-----------------
494 -- use of random() is to keep planner from folding the expressions together
495 explain (verbose, costs off)
496 select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2;
498 ------------------------------------------------------------------------------------------------------
500 Output: generate_series(0, 2), generate_series(((random() * '0.1'::double precision))::integer, 2)
504 select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2;
512 explain (verbose, costs off)
513 select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2
516 ------------------------------------------------------------------------------------------------------------
518 Output: (generate_series(0, 2)), (generate_series(((random() * '0.1'::double precision))::integer, 2))
519 Sort Key: (generate_series(((random() * '0.1'::double precision))::integer, 2)) DESC
521 Output: generate_series(0, 2), generate_series(((random() * '0.1'::double precision))::integer, 2)
525 select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2
534 -- test for failure to set all aggregates' aggtranstype
535 explain (verbose, costs off)
536 select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
537 from tenk1 group by thousand order by thousand limit 3;
539 -------------------------------------------------------------------------------------------------------------------
541 Output: (sum(tenthous)), (((sum(tenthous))::double precision + (random() * '0'::double precision))), thousand
543 Output: sum(tenthous), ((sum(tenthous))::double precision + (random() * '0'::double precision)), thousand
544 Group Key: tenk1.thousand
545 -> Index Only Scan using tenk1_thous_tenthous on public.tenk1
546 Output: thousand, tenthous
549 select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
550 from tenk1 group by thousand order by thousand limit 3;
560 -- Check the WITH TIES clause
563 FROM onek WHERE thousand < 5
564 ORDER BY thousand FETCH FIRST 2 ROW WITH TIES;
580 FROM onek WHERE thousand < 5
581 ORDER BY thousand FETCH FIRST ROWS WITH TIES;
597 FROM onek WHERE thousand < 5
598 ORDER BY thousand FETCH FIRST 1 ROW WITH TIES;
614 FROM onek WHERE thousand < 5
615 ORDER BY thousand FETCH FIRST 2 ROW ONLY;
623 SELECT ''::text AS two, unique1, unique2, stringu1
624 FROM onek WHERE unique1 > 50
625 FETCH FIRST 2 ROW WITH TIES;
626 ERROR: WITH TIES cannot be specified without ORDER BY clause
628 CREATE VIEW limit_thousand_v_1 AS SELECT thousand FROM onek WHERE thousand < 995
629 ORDER BY thousand FETCH FIRST 5 ROWS WITH TIES OFFSET 10;
630 \d+ limit_thousand_v_1
631 View "public.limit_thousand_v_1"
632 Column | Type | Collation | Nullable | Default | Storage | Description
633 ----------+---------+-----------+----------+---------+---------+-------------
634 thousand | integer | | | | plain |
638 WHERE onek.thousand < 995
639 ORDER BY onek.thousand
641 FETCH FIRST 5 ROWS WITH TIES;
643 CREATE VIEW limit_thousand_v_2 AS SELECT thousand FROM onek WHERE thousand < 995
644 ORDER BY thousand OFFSET 10 FETCH FIRST 5 ROWS ONLY;
645 \d+ limit_thousand_v_2
646 View "public.limit_thousand_v_2"
647 Column | Type | Collation | Nullable | Default | Storage | Description
648 ----------+---------+-----------+----------+---------+---------+-------------
649 thousand | integer | | | | plain |
653 WHERE onek.thousand < 995
654 ORDER BY onek.thousand
658 CREATE VIEW limit_thousand_v_3 AS SELECT thousand FROM onek WHERE thousand < 995
659 ORDER BY thousand FETCH FIRST NULL ROWS WITH TIES; -- fails
660 ERROR: row count cannot be null in FETCH FIRST ... WITH TIES clause
661 CREATE VIEW limit_thousand_v_3 AS SELECT thousand FROM onek WHERE thousand < 995
662 ORDER BY thousand FETCH FIRST (NULL+1) ROWS WITH TIES;
663 \d+ limit_thousand_v_3
664 View "public.limit_thousand_v_3"
665 Column | Type | Collation | Nullable | Default | Storage | Description
666 ----------+---------+-----------+----------+---------+---------+-------------
667 thousand | integer | | | | plain |
671 WHERE onek.thousand < 995
672 ORDER BY onek.thousand
673 FETCH FIRST (NULL::integer + 1) ROWS WITH TIES;
675 CREATE VIEW limit_thousand_v_4 AS SELECT thousand FROM onek WHERE thousand < 995
676 ORDER BY thousand FETCH FIRST NULL ROWS ONLY;
677 \d+ limit_thousand_v_4
678 View "public.limit_thousand_v_4"
679 Column | Type | Collation | Nullable | Default | Storage | Description
680 ----------+---------+-----------+----------+---------+---------+-------------
681 thousand | integer | | | | plain |
685 WHERE onek.thousand < 995
686 ORDER BY onek.thousand