1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
|
--
-- tsrf - targetlist set returning function tests
--
-- simple srf
SELECT generate_series(1, 3);
generate_series
-----------------
1
2
3
(3 rows)
-- parallel iteration
SELECT generate_series(1, 3), generate_series(3,5);
generate_series | generate_series
-----------------+-----------------
1 | 3
2 | 4
3 | 5
(3 rows)
-- parallel iteration, different number of rows
SELECT generate_series(1, 2), generate_series(1,4);
generate_series | generate_series
-----------------+-----------------
1 | 1
2 | 2
| 3
| 4
(4 rows)
-- srf, with SRF argument
SELECT generate_series(1, generate_series(1, 3));
generate_series
-----------------
1
1
2
1
2
3
(6 rows)
-- but we've traditionally rejected the same in FROM
SELECT * FROM generate_series(1, generate_series(1, 3));
ERROR: set-returning functions must appear at top level of FROM
LINE 1: SELECT * FROM generate_series(1, generate_series(1, 3));
^
-- srf, with two SRF arguments
SELECT generate_series(generate_series(1,3), generate_series(2, 4));
generate_series
-----------------
1
2
2
3
3
4
(6 rows)
-- check proper nesting of SRFs in different expressions
explain (verbose, costs off)
SELECT generate_series(1, generate_series(1, 3)), generate_series(2, 4);
QUERY PLAN
--------------------------------------------------------------------------------
ProjectSet
Output: generate_series(1, (generate_series(1, 3))), (generate_series(2, 4))
-> ProjectSet
Output: generate_series(1, 3), generate_series(2, 4)
-> Result
(5 rows)
SELECT generate_series(1, generate_series(1, 3)), generate_series(2, 4);
generate_series | generate_series
-----------------+-----------------
1 | 2
1 | 3
2 | 3
1 | 4
2 | 4
3 | 4
(6 rows)
CREATE TABLE few(id int, dataa text, datab text);
INSERT INTO few VALUES(1, 'a', 'foo'),(2, 'a', 'bar'),(3, 'b', 'bar');
-- SRF with a provably-dummy relation
explain (verbose, costs off)
SELECT unnest(ARRAY[1, 2]) FROM few WHERE false;
QUERY PLAN
--------------------------------------
ProjectSet
Output: unnest('{1,2}'::integer[])
-> Result
One-Time Filter: false
(4 rows)
SELECT unnest(ARRAY[1, 2]) FROM few WHERE false;
unnest
--------
(0 rows)
-- SRF shouldn't prevent upper query from recognizing lower as dummy
explain (verbose, costs off)
SELECT * FROM few f1,
(SELECT unnest(ARRAY[1,2]) FROM few f2 WHERE false OFFSET 0) ss;
QUERY PLAN
------------------------------------------------
Result
Output: f1.id, f1.dataa, f1.datab, ss.unnest
One-Time Filter: false
(3 rows)
SELECT * FROM few f1,
(SELECT unnest(ARRAY[1,2]) FROM few f2 WHERE false OFFSET 0) ss;
id | dataa | datab | unnest
----+-------+-------+--------
(0 rows)
-- SRF output order of sorting is maintained, if SRF is not referenced
SELECT few.id, generate_series(1,3) g FROM few ORDER BY id DESC;
id | g
----+---
3 | 1
3 | 2
3 | 3
2 | 1
2 | 2
2 | 3
1 | 1
1 | 2
1 | 3
(9 rows)
-- but SRFs can be referenced in sort
SELECT few.id, generate_series(1,3) g FROM few ORDER BY id, g DESC;
id | g
----+---
1 | 3
1 | 2
1 | 1
2 | 3
2 | 2
2 | 1
3 | 3
3 | 2
3 | 1
(9 rows)
SELECT few.id, generate_series(1,3) g FROM few ORDER BY id, generate_series(1,3) DESC;
id | g
----+---
1 | 3
1 | 2
1 | 1
2 | 3
2 | 2
2 | 1
3 | 3
3 | 2
3 | 1
(9 rows)
-- it's weird to have ORDER BYs that increase the number of results
SELECT few.id FROM few ORDER BY id, generate_series(1,3) DESC;
id
----
1
1
1
2
2
2
3
3
3
(9 rows)
-- SRFs are computed after aggregation
SET enable_hashagg TO 0; -- stable output order
SELECT few.dataa, count(*), min(id), max(id), unnest('{1,1,3}'::int[]) FROM few WHERE few.id = 1 GROUP BY few.dataa;
dataa | count | min | max | unnest
-------+-------+-----+-----+--------
a | 1 | 1 | 1 | 1
a | 1 | 1 | 1 | 1
a | 1 | 1 | 1 | 3
(3 rows)
-- unless referenced in GROUP BY clause
SELECT few.dataa, count(*), min(id), max(id), unnest('{1,1,3}'::int[]) FROM few WHERE few.id = 1 GROUP BY few.dataa, unnest('{1,1,3}'::int[]);
dataa | count | min | max | unnest
-------+-------+-----+-----+--------
a | 2 | 1 | 1 | 1
a | 1 | 1 | 1 | 3
(2 rows)
SELECT few.dataa, count(*), min(id), max(id), unnest('{1,1,3}'::int[]) FROM few WHERE few.id = 1 GROUP BY few.dataa, 5;
dataa | count | min | max | unnest
-------+-------+-----+-----+--------
a | 2 | 1 | 1 | 1
a | 1 | 1 | 1 | 3
(2 rows)
RESET enable_hashagg;
-- check HAVING works when GROUP BY does [not] reference SRF output
SELECT dataa, generate_series(1,1), count(*) FROM few GROUP BY 1 HAVING count(*) > 1;
dataa | generate_series | count
-------+-----------------+-------
a | 1 | 2
(1 row)
SELECT dataa, generate_series(1,1), count(*) FROM few GROUP BY 1, 2 HAVING count(*) > 1;
dataa | generate_series | count
-------+-----------------+-------
a | 1 | 2
(1 row)
-- it's weird to have GROUP BYs that increase the number of results
SELECT few.dataa, count(*) FROM few WHERE dataa = 'a' GROUP BY few.dataa ORDER BY 2;
dataa | count
-------+-------
a | 2
(1 row)
SELECT few.dataa, count(*) FROM few WHERE dataa = 'a' GROUP BY few.dataa, unnest('{1,1,3}'::int[]) ORDER BY 2;
dataa | count
-------+-------
a | 2
a | 4
(2 rows)
-- SRFs are not allowed if they'd need to be conditionally executed
SELECT q1, case when q1 > 0 then generate_series(1,3) else 0 end FROM int8_tbl;
ERROR: set-returning functions are not allowed in CASE
LINE 1: SELECT q1, case when q1 > 0 then generate_series(1,3) else 0...
^
HINT: You might be able to move the set-returning function into a LATERAL FROM item.
SELECT q1, coalesce(generate_series(1,3), 0) FROM int8_tbl;
ERROR: set-returning functions are not allowed in COALESCE
LINE 1: SELECT q1, coalesce(generate_series(1,3), 0) FROM int8_tbl;
^
HINT: You might be able to move the set-returning function into a LATERAL FROM item.
-- SRFs are not allowed in aggregate arguments
SELECT min(generate_series(1, 3)) FROM few;
ERROR: aggregate function calls cannot contain set-returning function calls
LINE 1: SELECT min(generate_series(1, 3)) FROM few;
^
HINT: You might be able to move the set-returning function into a LATERAL FROM item.
-- ... unless they're within a sub-select
SELECT sum((3 = ANY(SELECT generate_series(1,4)))::int);
sum
-----
1
(1 row)
SELECT sum((3 = ANY(SELECT lag(x) over(order by x)
FROM generate_series(1,4) x))::int);
sum
-----
1
(1 row)
-- SRFs are not allowed in window function arguments, either
SELECT min(generate_series(1, 3)) OVER() FROM few;
ERROR: window function calls cannot contain set-returning function calls
LINE 1: SELECT min(generate_series(1, 3)) OVER() FROM few;
^
HINT: You might be able to move the set-returning function into a LATERAL FROM item.
-- SRFs are normally computed after window functions
SELECT id,lag(id) OVER(), count(*) OVER(), generate_series(1,3) FROM few;
id | lag | count | generate_series
----+-----+-------+-----------------
1 | | 3 | 1
1 | | 3 | 2
1 | | 3 | 3
2 | 1 | 3 | 1
2 | 1 | 3 | 2
2 | 1 | 3 | 3
3 | 2 | 3 | 1
3 | 2 | 3 | 2
3 | 2 | 3 | 3
(9 rows)
-- unless referencing SRFs
SELECT SUM(count(*)) OVER(PARTITION BY generate_series(1,3) ORDER BY generate_series(1,3)), generate_series(1,3) g FROM few GROUP BY g;
sum | g
-----+---
3 | 1
3 | 2
3 | 3
(3 rows)
-- sorting + grouping
SELECT few.dataa, count(*), min(id), max(id), generate_series(1,3) FROM few GROUP BY few.dataa ORDER BY 5, 1;
dataa | count | min | max | generate_series
-------+-------+-----+-----+-----------------
a | 2 | 1 | 2 | 1
b | 1 | 3 | 3 | 1
a | 2 | 1 | 2 | 2
b | 1 | 3 | 3 | 2
a | 2 | 1 | 2 | 3
b | 1 | 3 | 3 | 3
(6 rows)
-- grouping sets are a bit special, they produce NULLs in columns not actually NULL
set enable_hashagg = false;
SELECT dataa, datab b, generate_series(1,2) g, count(*) FROM few GROUP BY CUBE(dataa, datab);
dataa | b | g | count
-------+-----+---+-------
a | bar | 1 | 1
a | bar | 2 | 1
a | foo | 1 | 1
a | foo | 2 | 1
a | | 1 | 2
a | | 2 | 2
b | bar | 1 | 1
b | bar | 2 | 1
b | | 1 | 1
b | | 2 | 1
| | 1 | 3
| | 2 | 3
| bar | 1 | 2
| bar | 2 | 2
| foo | 1 | 1
| foo | 2 | 1
(16 rows)
SELECT dataa, datab b, generate_series(1,2) g, count(*) FROM few GROUP BY CUBE(dataa, datab) ORDER BY dataa;
dataa | b | g | count
-------+-----+---+-------
a | bar | 1 | 1
a | bar | 2 | 1
a | foo | 1 | 1
a | foo | 2 | 1
a | | 1 | 2
a | | 2 | 2
b | bar | 1 | 1
b | bar | 2 | 1
b | | 1 | 1
b | | 2 | 1
| | 1 | 3
| | 2 | 3
| bar | 1 | 2
| bar | 2 | 2
| foo | 1 | 1
| foo | 2 | 1
(16 rows)
SELECT dataa, datab b, generate_series(1,2) g, count(*) FROM few GROUP BY CUBE(dataa, datab) ORDER BY g;
dataa | b | g | count
-------+-----+---+-------
a | bar | 1 | 1
a | foo | 1 | 1
a | | 1 | 2
b | bar | 1 | 1
b | | 1 | 1
| | 1 | 3
| bar | 1 | 2
| foo | 1 | 1
| foo | 2 | 1
a | bar | 2 | 1
b | | 2 | 1
a | foo | 2 | 1
| bar | 2 | 2
a | | 2 | 2
| | 2 | 3
b | bar | 2 | 1
(16 rows)
SELECT dataa, datab b, generate_series(1,2) g, count(*) FROM few GROUP BY CUBE(dataa, datab, g);
dataa | b | g | count
-------+-----+---+-------
a | bar | 1 | 1
a | bar | 2 | 1
a | bar | | 2
a | foo | 1 | 1
a | foo | 2 | 1
a | foo | | 2
a | | | 4
b | bar | 1 | 1
b | bar | 2 | 1
b | bar | | 2
b | | | 2
| | | 6
| bar | 1 | 2
| bar | 2 | 2
| bar | | 4
| foo | 1 | 1
| foo | 2 | 1
| foo | | 2
a | | 1 | 2
b | | 1 | 1
| | 1 | 3
a | | 2 | 2
b | | 2 | 1
| | 2 | 3
(24 rows)
SELECT dataa, datab b, generate_series(1,2) g, count(*) FROM few GROUP BY CUBE(dataa, datab, g) ORDER BY dataa;
dataa | b | g | count
-------+-----+---+-------
a | foo | | 2
a | | | 4
a | | 2 | 2
a | bar | 1 | 1
a | bar | 2 | 1
a | bar | | 2
a | foo | 1 | 1
a | foo | 2 | 1
a | | 1 | 2
b | bar | 1 | 1
b | | | 2
b | | 1 | 1
b | bar | 2 | 1
b | bar | | 2
b | | 2 | 1
| | 2 | 3
| | | 6
| bar | 1 | 2
| bar | 2 | 2
| bar | | 4
| foo | 1 | 1
| foo | 2 | 1
| foo | | 2
| | 1 | 3
(24 rows)
SELECT dataa, datab b, generate_series(1,2) g, count(*) FROM few GROUP BY CUBE(dataa, datab, g) ORDER BY g;
dataa | b | g | count
-------+-----+---+-------
a | bar | 1 | 1
a | foo | 1 | 1
b | bar | 1 | 1
| bar | 1 | 2
| foo | 1 | 1
a | | 1 | 2
b | | 1 | 1
| | 1 | 3
a | | 2 | 2
b | | 2 | 1
| bar | 2 | 2
| | 2 | 3
| foo | 2 | 1
a | bar | 2 | 1
a | foo | 2 | 1
b | bar | 2 | 1
a | | | 4
b | bar | | 2
b | | | 2
| | | 6
a | foo | | 2
a | bar | | 2
| bar | | 4
| foo | | 2
(24 rows)
reset enable_hashagg;
-- case with degenerate ORDER BY
explain (verbose, costs off)
select 'foo' as f, generate_series(1,2) as g from few order by 1;
QUERY PLAN
----------------------------------------------
ProjectSet
Output: 'foo'::text, generate_series(1, 2)
-> Seq Scan on public.few
Output: id, dataa, datab
(4 rows)
select 'foo' as f, generate_series(1,2) as g from few order by 1;
f | g
-----+---
foo | 1
foo | 2
foo | 1
foo | 2
foo | 1
foo | 2
(6 rows)
-- data modification
CREATE TABLE fewmore AS SELECT generate_series(1,3) AS data;
INSERT INTO fewmore VALUES(generate_series(4,5));
SELECT * FROM fewmore;
data
------
1
2
3
4
5
(5 rows)
-- SRFs are not allowed in UPDATE (they once were, but it was nonsense)
UPDATE fewmore SET data = generate_series(4,9);
ERROR: set-returning functions are not allowed in UPDATE
LINE 1: UPDATE fewmore SET data = generate_series(4,9);
^
-- SRFs are not allowed in RETURNING
INSERT INTO fewmore VALUES(1) RETURNING generate_series(1,3);
ERROR: set-returning functions are not allowed in RETURNING
LINE 1: INSERT INTO fewmore VALUES(1) RETURNING generate_series(1,3)...
^
-- nor standalone VALUES (but surely this is a bug?)
VALUES(1, generate_series(1,2));
ERROR: set-returning functions are not allowed in VALUES
LINE 1: VALUES(1, generate_series(1,2));
^
-- We allow tSRFs that are not at top level
SELECT int4mul(generate_series(1,2), 10);
int4mul
---------
10
20
(2 rows)
SELECT generate_series(1,3) IS DISTINCT FROM 2;
?column?
----------
t
f
t
(3 rows)
-- but SRFs in function RTEs must be at top level (annoying restriction)
SELECT * FROM int4mul(generate_series(1,2), 10);
ERROR: set-returning functions must appear at top level of FROM
LINE 1: SELECT * FROM int4mul(generate_series(1,2), 10);
^
-- DISTINCT ON is evaluated before tSRF evaluation if SRF is not
-- referenced either in ORDER BY or in the DISTINCT ON list. The ORDER
-- BY reference can be implicitly generated, if there's no other ORDER BY.
-- implicit reference (via implicit ORDER) to all columns
SELECT DISTINCT ON (a) a, b, generate_series(1,3) g
FROM (VALUES (3, 2), (3,1), (1,1), (1,4), (5,3), (5,1)) AS t(a, b);
a | b | g
---+---+---
1 | 1 | 1
3 | 2 | 1
5 | 3 | 1
(3 rows)
-- unreferenced in DISTINCT ON or ORDER BY
SELECT DISTINCT ON (a) a, b, generate_series(1,3) g
FROM (VALUES (3, 2), (3,1), (1,1), (1,4), (5,3), (5,1)) AS t(a, b)
ORDER BY a, b DESC;
a | b | g
---+---+---
1 | 4 | 1
1 | 4 | 2
1 | 4 | 3
3 | 2 | 1
3 | 2 | 2
3 | 2 | 3
5 | 3 | 1
5 | 3 | 2
5 | 3 | 3
(9 rows)
-- referenced in ORDER BY
SELECT DISTINCT ON (a) a, b, generate_series(1,3) g
FROM (VALUES (3, 2), (3,1), (1,1), (1,4), (5,3), (5,1)) AS t(a, b)
ORDER BY a, b DESC, g DESC;
a | b | g
---+---+---
1 | 4 | 3
3 | 2 | 3
5 | 3 | 3
(3 rows)
-- referenced in ORDER BY and DISTINCT ON
SELECT DISTINCT ON (a, b, g) a, b, generate_series(1,3) g
FROM (VALUES (3, 2), (3,1), (1,1), (1,4), (5,3), (5,1)) AS t(a, b)
ORDER BY a, b DESC, g DESC;
a | b | g
---+---+---
1 | 4 | 3
1 | 4 | 2
1 | 4 | 1
1 | 1 | 3
1 | 1 | 2
1 | 1 | 1
3 | 2 | 3
3 | 2 | 2
3 | 2 | 1
3 | 1 | 3
3 | 1 | 2
3 | 1 | 1
5 | 3 | 3
5 | 3 | 2
5 | 3 | 1
5 | 1 | 3
5 | 1 | 2
5 | 1 | 1
(18 rows)
-- only SRF mentioned in DISTINCT ON
SELECT DISTINCT ON (g) a, b, generate_series(1,3) g
FROM (VALUES (3, 2), (3,1), (1,1), (1,4), (5,3), (5,1)) AS t(a, b);
a | b | g
---+---+---
3 | 2 | 1
5 | 1 | 2
3 | 1 | 3
(3 rows)
-- LIMIT / OFFSET is evaluated after SRF evaluation
SELECT a, generate_series(1,2) FROM (VALUES(1),(2),(3)) r(a) LIMIT 2 OFFSET 2;
a | generate_series
---+-----------------
2 | 1
2 | 2
(2 rows)
-- SRFs are not allowed in LIMIT.
SELECT 1 LIMIT generate_series(1,3);
ERROR: set-returning functions are not allowed in LIMIT
LINE 1: SELECT 1 LIMIT generate_series(1,3);
^
-- tSRF in correlated subquery, referencing table outside
SELECT (SELECT generate_series(1,3) LIMIT 1 OFFSET few.id) FROM few;
generate_series
-----------------
2
3
(3 rows)
-- tSRF in correlated subquery, referencing SRF outside
SELECT (SELECT generate_series(1,3) LIMIT 1 OFFSET g.i) FROM generate_series(0,3) g(i);
generate_series
-----------------
1
2
3
(4 rows)
-- Operators can return sets too
CREATE OPERATOR |@| (PROCEDURE = unnest, RIGHTARG = ANYARRAY);
SELECT |@|ARRAY[1,2,3];
?column?
----------
1
2
3
(3 rows)
-- Some fun cases involving duplicate SRF calls
explain (verbose, costs off)
select generate_series(1,3) as x, generate_series(1,3) + 1 as xp1;
QUERY PLAN
------------------------------------------------------------------
Result
Output: (generate_series(1, 3)), ((generate_series(1, 3)) + 1)
-> ProjectSet
Output: generate_series(1, 3)
-> Result
(5 rows)
select generate_series(1,3) as x, generate_series(1,3) + 1 as xp1;
x | xp1
---+-----
1 | 2
2 | 3
3 | 4
(3 rows)
explain (verbose, costs off)
select generate_series(1,3)+1 order by generate_series(1,3);
QUERY PLAN
------------------------------------------------------------------------
Sort
Output: (((generate_series(1, 3)) + 1)), (generate_series(1, 3))
Sort Key: (generate_series(1, 3))
-> Result
Output: ((generate_series(1, 3)) + 1), (generate_series(1, 3))
-> ProjectSet
Output: generate_series(1, 3)
-> Result
(8 rows)
select generate_series(1,3)+1 order by generate_series(1,3);
?column?
----------
2
3
4
(3 rows)
-- Check that SRFs of same nesting level run in lockstep
explain (verbose, costs off)
select generate_series(1,3) as x, generate_series(3,6) + 1 as y;
QUERY PLAN
------------------------------------------------------------------
Result
Output: (generate_series(1, 3)), ((generate_series(3, 6)) + 1)
-> ProjectSet
Output: generate_series(1, 3), generate_series(3, 6)
-> Result
(5 rows)
select generate_series(1,3) as x, generate_series(3,6) + 1 as y;
x | y
---+---
1 | 4
2 | 5
3 | 6
| 7
(4 rows)
-- Clean up
DROP TABLE few;
DROP TABLE fewmore;
|