rangefuncs.out 56.8 KB
Newer Older
1
SELECT name, setting FROM pg_settings WHERE name LIKE 'enable%';
2 3 4
         name         | setting 
----------------------+---------
 enable_bitmapscan    | on
R
Richard Guo 已提交
5
 enable_groupagg      | on
6 7 8 9 10
 enable_hashagg       | on
 enable_hashjoin      | on
 enable_indexonlyscan | on
 enable_indexscan     | on
 enable_material      | on
R
Richard Guo 已提交
11 12
 enable_mergejoin     | off
 enable_nestloop      | off
13 14 15
 enable_seqscan       | on
 enable_sort          | on
 enable_tidscan       | on
R
Richard Guo 已提交
16
(12 rows)
17

18 19 20 21
CREATE TABLE foo2(fooid int, f2 int);
INSERT INTO foo2 VALUES(1, 11);
INSERT INTO foo2 VALUES(2, 22);
INSERT INTO foo2 VALUES(1, 111);
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
CREATE FUNCTION foot(int) returns setof foo2 as 'SELECT * FROM foo2 WHERE fooid = $1 ORDER BY f2;' LANGUAGE SQL;
-- function with ORDINALITY
select * from foot(1) with ordinality as z(a,b,ord);
 a |  b  | ord 
---+-----+-----
 1 |  11 |   1
 1 | 111 |   2
(2 rows)

select * from foot(1) with ordinality as z(a,b,ord) where b > 100;   -- ordinal 2, not 1
 a |  b  | ord 
---+-----+-----
 1 | 111 |   2
(1 row)

-- ordinality vs. column names and types
select a,b,ord from foot(1) with ordinality as z(a,b,ord);
 a |  b  | ord 
---+-----+-----
 1 |  11 |   1
 1 | 111 |   2
(2 rows)

select a,ord from unnest(array['a','b']) with ordinality as z(a,ord);
 a | ord 
---+-----
 a |   1
 b |   2
(2 rows)

select * from unnest(array['a','b']) with ordinality as z(a,ord);
 a | ord 
---+-----
 a |   1
 b |   2
(2 rows)

select a,ord from unnest(array[1.0::float8]) with ordinality as z(a,ord);
 a | ord 
---+-----
 1 |   1
(1 row)

select * from unnest(array[1.0::float8]) with ordinality as z(a,ord);
 a | ord 
---+-----
 1 |   1
(1 row)

71 72 73 74 75 76 77 78 79
select row_to_json(s.*) from generate_series(11,14) with ordinality s;
       row_to_json       
-------------------------
 {"s":11,"ordinality":1}
 {"s":12,"ordinality":2}
 {"s":13,"ordinality":3}
 {"s":14,"ordinality":4}
(4 rows)

80 81 82 83 84 85 86 87 88
-- ordinality vs. views
create temporary view vw_ord as select * from (values (1)) v(n) join foot(1) with ordinality as z(a,b,ord) on (n=ord);
select * from vw_ord;
 n | a | b  | ord 
---+---+----+-----
 1 | 1 | 11 |   1
(1 row)

select definition from pg_views where viewname='vw_ord';
89 90 91 92 93 94 95 96
                             definition                              
---------------------------------------------------------------------
  SELECT v.n,                                                       +
     z.a,                                                           +
     z.b,                                                           +
     z.ord                                                          +
    FROM (( VALUES (1)) v(n)                                        +
      JOIN foot(1) WITH ORDINALITY z(a, b, ord) ON ((v.n = z.ord)));
97 98 99
(1 row)

drop view vw_ord;
100
-- multiple functions
N
Noah Misch 已提交
101
select * from rows from(foot(1),foot(2)) with ordinality as z(a,b,c,d,ord);
102 103 104 105 106 107
 a |  b  | c | d  | ord 
---+-----+---+----+-----
 1 |  11 | 2 | 22 |   1
 1 | 111 |   |    |   2
(2 rows)

N
Noah Misch 已提交
108
create temporary view vw_ord as select * from (values (1)) v(n) join rows from(foot(1),foot(2)) with ordinality as z(a,b,c,d,ord) on (n=ord);
109 110 111 112 113 114 115
select * from vw_ord;
 n | a | b  | c | d  | ord 
---+---+----+---+----+-----
 1 | 1 | 11 | 2 | 22 |   1
(1 row)

select definition from pg_views where viewname='vw_ord';
116 117 118 119 120 121 122 123 124 125
                                          definition                                           
-----------------------------------------------------------------------------------------------
  SELECT v.n,                                                                                 +
     z.a,                                                                                     +
     z.b,                                                                                     +
     z.c,                                                                                     +
     z.d,                                                                                     +
     z.ord                                                                                    +
    FROM (( VALUES (1)) v(n)                                                                  +
      JOIN ROWS FROM(foot(1), foot(2)) WITH ORDINALITY z(a, b, c, d, ord) ON ((v.n = z.ord)));
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
(1 row)

drop view vw_ord;
-- expansions of unnest()
select * from unnest(array[10,20],array['foo','bar'],array[1.0]);
 unnest | unnest | unnest 
--------+--------+--------
     10 | foo    |    1.0
     20 | bar    |       
(2 rows)

select * from unnest(array[10,20],array['foo','bar'],array[1.0]) with ordinality as z(a,b,c,ord);
 a  |  b  |  c  | ord 
----+-----+-----+-----
 10 | foo | 1.0 |   1
 20 | bar |     |   2
(2 rows)

N
Noah Misch 已提交
144
select * from rows from(unnest(array[10,20],array['foo','bar'],array[1.0])) with ordinality as z(a,b,c,ord);
145 146 147 148 149 150
 a  |  b  |  c  | ord 
----+-----+-----+-----
 10 | foo | 1.0 |   1
 20 | bar |     |   2
(2 rows)

N
Noah Misch 已提交
151
select * from rows from(unnest(array[10,20],array['foo','bar']), generate_series(101,102)) with ordinality as z(a,b,c,ord);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
 a  |  b  |  c  | ord 
----+-----+-----+-----
 10 | foo | 101 |   1
 20 | bar | 102 |   2
(2 rows)

create temporary view vw_ord as select * from unnest(array[10,20],array['foo','bar'],array[1.0]) as z(a,b,c);
select * from vw_ord;
 a  |  b  |  c  
----+-----+-----
 10 | foo | 1.0
 20 | bar |    
(2 rows)

select definition from pg_views where viewname='vw_ord';
                                       definition                                       
----------------------------------------------------------------------------------------
  SELECT z.a,                                                                          +
     z.b,                                                                              +
     z.c                                                                               +
    FROM UNNEST(ARRAY[10, 20], ARRAY['foo'::text, 'bar'::text], ARRAY[1.0]) z(a, b, c);
(1 row)

drop view vw_ord;
N
Noah Misch 已提交
176
create temporary view vw_ord as select * from rows from(unnest(array[10,20],array['foo','bar'],array[1.0])) as z(a,b,c);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
select * from vw_ord;
 a  |  b  |  c  
----+-----+-----
 10 | foo | 1.0
 20 | bar |    
(2 rows)

select definition from pg_views where viewname='vw_ord';
                                       definition                                       
----------------------------------------------------------------------------------------
  SELECT z.a,                                                                          +
     z.b,                                                                              +
     z.c                                                                               +
    FROM UNNEST(ARRAY[10, 20], ARRAY['foo'::text, 'bar'::text], ARRAY[1.0]) z(a, b, c);
(1 row)

drop view vw_ord;
N
Noah Misch 已提交
194
create temporary view vw_ord as select * from rows from(unnest(array[10,20],array['foo','bar']), generate_series(1,2)) as z(a,b,c);
195 196 197 198 199 200 201 202
select * from vw_ord;
 a  |  b  | c 
----+-----+---
 10 | foo | 1
 20 | bar | 2
(2 rows)

select definition from pg_views where viewname='vw_ord';
N
Noah Misch 已提交
203 204 205 206 207 208
                                                      definition                                                      
----------------------------------------------------------------------------------------------------------------------
  SELECT z.a,                                                                                                        +
     z.b,                                                                                                            +
     z.c                                                                                                             +
    FROM ROWS FROM(unnest(ARRAY[10, 20]), unnest(ARRAY['foo'::text, 'bar'::text]), generate_series(1, 2)) z(a, b, c);
209 210 211 212
(1 row)

drop view vw_ord;
-- ordinality and multiple functions vs. rewind and reverse scan
213 214
-- Backward scan not supported in GPDB, which makes this a lot less
-- interesting than in PostgreSQL.
215
begin;
N
Noah Misch 已提交
216
declare foo scroll cursor for select * from rows from(generate_series(1,5),generate_series(1,2)) with ordinality as g(i,j,o);
217
fetch all from foo;
218 219 220 221 222 223 224
 i | j | o 
---+---+---
 1 | 1 | 1
 2 | 2 | 2
 3 |   | 3
 4 |   | 4
 5 |   | 5
225 226
(5 rows)

227
--fetch backward all from foo;
228
fetch all from foo;
229 230
 i | j | o 
---+---+---
231
(0 rows)
232 233

fetch next from foo;
234 235
 i | j | o 
---+---+---
236 237 238
(0 rows)

fetch next from foo;
239 240
 i | j | o 
---+---+---
241 242
(0 rows)

243 244
--fetch prior from foo;
--fetch absolute 1 from foo;
245 246 247
fetch next from foo;
 i | j | o 
---+---+---
248
(0 rows)
249 250 251 252

fetch next from foo;
 i | j | o 
---+---+---
253
(0 rows)
254 255 256 257

fetch next from foo;
 i | j | o 
---+---+---
258
(0 rows)
259

260 261 262
--fetch prior from foo;
--fetch prior from foo;
--fetch prior from foo;
263
commit;
264
-- function with implicit LATERAL
265
select * from foo2, foot(foo2.fooid) z where foo2.f2 = z.f2;
266 267 268
ERROR:  query plan with multiple segworker groups is not supported
HINT:  likely caused by a function that reads or modifies data in a distributed table
CONTEXT:  SQL function "foot" statement 1
269 270
-- function with implicit LATERAL and explicit ORDINALITY
select * from foo2, foot(foo2.fooid) with ordinality as z(fooid,f2,ord) where foo2.f2 = z.f2;
271 272 273
ERROR:  query plan with multiple segworker groups is not supported
HINT:  likely caused by a function that reads or modifies data in a distributed table
CONTEXT:  SQL function "foot" statement 1
274 275
-- function in subselect
select * from foo2 where f2 in (select f2 from foot(foo2.fooid) z where z.fooid = foo2.fooid) ORDER BY 1,2;
276
ERROR:  function cannot execute on a QE slice because it accesses relation "public.foo2"
277
CONTEXT:  SQL function "foot" during startup
278 279
-- function in subselect
select * from foo2 where f2 in (select f2 from foot(1) z where z.fooid = foo2.fooid) ORDER BY 1,2;
280
ERROR:  function cannot execute on a QE slice because it accesses relation "public.foo2"
281
CONTEXT:  SQL function "foot" during startup
282 283
-- function in subselect
select * from foo2 where f2 in (select f2 from foot(foo2.fooid) z where z.fooid = 1) ORDER BY 1,2;
284
ERROR:  function cannot execute on a QE slice because it accesses relation "public.foo2"
285
CONTEXT:  SQL function "foot" during startup
286 287 288 289 290 291 292 293 294 295 296 297 298
-- nested functions
select foot.fooid, foot.f2 from foot(sin(pi()/2)::int) ORDER BY 1,2;
 fooid | f2  
-------+-----
     1 |  11
     1 | 111
(2 rows)

CREATE TABLE foo (fooid int, foosubid int, fooname text, primary key(fooid,foosubid));
INSERT INTO foo VALUES(1,1,'Joe');
INSERT INTO foo VALUES(1,2,'Ed');
INSERT INTO foo VALUES(2,1,'Mary');
-- sql, proretset = f, prorettype = b
299 300
CREATE FUNCTION getfoo1(int) RETURNS int AS 'SELECT $1;' LANGUAGE SQL;
SELECT * FROM getfoo1(1) AS t1;
301 302 303
 t1 
----
  1
304 305
(1 row)

306
SELECT * FROM getfoo1(1) WITH ORDINALITY AS t1(v,o);
307 308 309 310 311
 v | o 
---+---
 1 | 1
(1 row)

312
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo1(1);
313
SELECT * FROM vw_getfoo;
314 315 316
 getfoo1 
---------
       1
317 318
(1 row)

319
DROP VIEW vw_getfoo;
320
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo1(1) WITH ORDINALITY as t1(v,o);
321 322 323 324 325 326
SELECT * FROM vw_getfoo;
 v | o 
---+---
 1 | 1
(1 row)

327
DROP VIEW vw_getfoo;
328 329 330
-- sql, proretset = t, prorettype = b
CREATE FUNCTION getfoo2(int) RETURNS setof int AS 'SELECT fooid FROM foo WHERE fooid = $1;' LANGUAGE SQL;
SELECT * FROM getfoo2(1) AS t1;
331 332 333 334
 t1 
----
  1
  1
335 336
(2 rows)

337
SELECT * FROM getfoo2(1) WITH ORDINALITY AS t1(v,o);
338 339 340 341 342 343
 v | o 
---+---
 1 | 1
 1 | 2
(2 rows)

344
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo2(1);
345
SELECT * FROM vw_getfoo;
346 347 348 349
 getfoo2 
---------
       1
       1
350 351
(2 rows)

352
DROP VIEW vw_getfoo;
353
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo2(1) WITH ORDINALITY AS t1(v,o);
354 355 356 357 358 359 360
SELECT * FROM vw_getfoo;
 v | o 
---+---
 1 | 1
 1 | 2
(2 rows)

361
DROP VIEW vw_getfoo;
362
-- sql, proretset = t, prorettype = b
363
CREATE FUNCTION getfoo3(int) RETURNS setof text AS 'SELECT fooname FROM foo WHERE fooid = $1 ORDER BY fooname DESC /* ORDER BY to force the Joe row to be returned */ ;' LANGUAGE SQL;
364
SELECT * FROM getfoo3(1) AS t1;
365 366
 t1  
-----
367 368 369 370
 Joe
 Ed
(2 rows)

371
SELECT * FROM getfoo3(1) WITH ORDINALITY AS t1(v,o);
372 373 374 375 376 377
  v  | o 
-----+---
 Joe | 1
 Ed  | 2
(2 rows)

378
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo3(1);
379
SELECT * FROM vw_getfoo;
380 381
 getfoo3 
---------
382 383 384 385
 Joe
 Ed
(2 rows)

386
DROP VIEW vw_getfoo;
387
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo3(1) WITH ORDINALITY AS t1(v,o);
388 389 390 391 392 393 394
SELECT * FROM vw_getfoo;
  v  | o 
-----+---
 Joe | 1
 Ed  | 2
(2 rows)

395
DROP VIEW vw_getfoo;
396
-- sql, proretset = f, prorettype = c
397
CREATE FUNCTION getfoo4(int) RETURNS foo AS 'SELECT * FROM foo WHERE fooid = $1 ORDER BY fooname DESC /* ORDER BY to force the Joe row to be returned */ ;' LANGUAGE SQL;
398
SELECT * FROM getfoo4(1) AS t1;
399 400 401 402 403
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
(1 row)

404
SELECT * FROM getfoo4(1) WITH ORDINALITY AS t1(a,b,c,o);
405 406 407 408 409
 a | b |  c  | o 
---+---+-----+---
 1 | 1 | Joe | 1
(1 row)

410
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo4(1);
411 412 413 414 415 416
SELECT * FROM vw_getfoo;
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
(1 row)

417
DROP VIEW vw_getfoo;
418
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo4(1) WITH ORDINALITY AS t1(a,b,c,o);
419 420 421 422 423 424
SELECT * FROM vw_getfoo;
 a | b |  c  | o 
---+---+-----+---
 1 | 1 | Joe | 1
(1 row)

425
DROP VIEW vw_getfoo;
426
-- sql, proretset = t, prorettype = c
427
CREATE FUNCTION getfoo5(int) RETURNS setof foo AS 'SELECT * FROM foo WHERE fooid = $1 ORDER BY fooname DESC /* ORDER BY to force the Joe row to be returned */ ;' LANGUAGE SQL;
428
SELECT * FROM getfoo5(1) AS t1;
429 430 431 432 433 434
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
     1 |        2 | Ed
(2 rows)

435
SELECT * FROM getfoo5(1) WITH ORDINALITY AS t1(a,b,c,o);
436 437 438 439 440 441
 a | b |  c  | o 
---+---+-----+---
 1 | 1 | Joe | 1
 1 | 2 | Ed  | 2
(2 rows)

442
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo5(1);
443 444 445 446 447 448 449
SELECT * FROM vw_getfoo;
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
     1 |        2 | Ed
(2 rows)

450
DROP VIEW vw_getfoo;
451
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo5(1) WITH ORDINALITY AS t1(a,b,c,o);
452 453 454 455 456 457 458
SELECT * FROM vw_getfoo;
 a | b |  c  | o 
---+---+-----+---
 1 | 1 | Joe | 1
 1 | 2 | Ed  | 2
(2 rows)

459
DROP VIEW vw_getfoo;
460
-- sql, proretset = f, prorettype = record
461
CREATE FUNCTION getfoo6(int) RETURNS RECORD AS 'SELECT * FROM foo WHERE fooid = $1 ORDER BY fooname DESC /* ORDER BY to force the Joe row to be returned */ ;' LANGUAGE SQL;
462
SELECT * FROM getfoo6(1) AS t1(fooid int, foosubid int, fooname text);
463 464 465 466 467
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
(1 row)

N
Noah Misch 已提交
468
SELECT * FROM ROWS FROM( getfoo6(1) AS (fooid int, foosubid int, fooname text) ) WITH ORDINALITY;
469 470 471 472 473 474
 fooid | foosubid | fooname | ordinality 
-------+----------+---------+------------
     1 |        1 | Joe     |          1
(1 row)

CREATE VIEW vw_getfoo AS SELECT * FROM getfoo6(1) AS
475 476 477 478 479 480 481 482
(fooid int, foosubid int, fooname text);
SELECT * FROM vw_getfoo;
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
(1 row)

DROP VIEW vw_getfoo;
483
CREATE VIEW vw_getfoo AS
N
Noah Misch 已提交
484
  SELECT * FROM ROWS FROM( getfoo6(1) AS (fooid int, foosubid int, fooname text) )
485 486 487 488 489 490 491 492 493
                WITH ORDINALITY;
SELECT * FROM vw_getfoo;
 fooid | foosubid | fooname | ordinality 
-------+----------+---------+------------
     1 |        1 | Joe     |          1
(1 row)

DROP VIEW vw_getfoo;
-- sql, proretset = t, prorettype = record
494
CREATE FUNCTION getfoo7(int) RETURNS setof record AS 'SELECT * FROM foo WHERE fooid = $1 ORDER BY fooname DESC /* ORDER BY to force the Joe row to be returned */ ;' LANGUAGE SQL;
495
SELECT * FROM getfoo7(1) AS t1(fooid int, foosubid int, fooname text);
496 497 498 499 500 501
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
     1 |        2 | Ed
(2 rows)

N
Noah Misch 已提交
502
SELECT * FROM ROWS FROM( getfoo7(1) AS (fooid int, foosubid int, fooname text) ) WITH ORDINALITY;
503 504 505 506 507 508 509
 fooid | foosubid | fooname | ordinality 
-------+----------+---------+------------
     1 |        1 | Joe     |          1
     1 |        2 | Ed      |          2
(2 rows)

CREATE VIEW vw_getfoo AS SELECT * FROM getfoo7(1) AS
510 511 512 513 514 515 516 517
(fooid int, foosubid int, fooname text);
SELECT * FROM vw_getfoo;
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
     1 |        2 | Ed
(2 rows)

518
DROP VIEW vw_getfoo;
519
CREATE VIEW vw_getfoo AS
N
Noah Misch 已提交
520
  SELECT * FROM ROWS FROM( getfoo7(1) AS (fooid int, foosubid int, fooname text) )
521 522 523 524 525 526 527 528 529 530 531 532
                WITH ORDINALITY;
SELECT * FROM vw_getfoo;
 fooid | foosubid | fooname | ordinality 
-------+----------+---------+------------
     1 |        1 | Joe     |          1
     1 |        2 | Ed      |          2
(2 rows)

DROP VIEW vw_getfoo;
-- plpgsql, proretset = f, prorettype = b
CREATE FUNCTION getfoo8(int) RETURNS int AS 'DECLARE fooint int; BEGIN SELECT fooid into fooint FROM foo WHERE fooid = $1; RETURN fooint; END;' LANGUAGE plpgsql;
SELECT * FROM getfoo8(1) AS t1;
533 534 535
 t1 
----
  1
536 537
(1 row)

538
SELECT * FROM getfoo8(1) WITH ORDINALITY AS t1(v,o);
539 540 541 542 543
 v | o 
---+---
 1 | 1
(1 row)

544
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo8(1);
545
SELECT * FROM vw_getfoo;
546 547 548
 getfoo8 
---------
       1
549 550
(1 row)

551
DROP VIEW vw_getfoo;
552
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo8(1) WITH ORDINALITY AS t1(v,o);
553 554 555 556
SELECT * FROM vw_getfoo;
 v | o 
---+---
 1 | 1
557 558
(1 row)

559
DROP VIEW vw_getfoo;
560
-- plpgsql, proretset = f, prorettype = c
561
CREATE FUNCTION getfoo9(int) RETURNS foo AS 'DECLARE footup foo%ROWTYPE; BEGIN SELECT * into footup FROM foo WHERE fooid = $1 ORDER BY fooname DESC /* ORDER BY to force the Joe row to be returned */ ; RETURN footup; END;' LANGUAGE plpgsql;
562
SELECT * FROM getfoo9(1) AS t1;
563 564 565 566 567
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
(1 row)

568
SELECT * FROM getfoo9(1) WITH ORDINALITY AS t1(a,b,c,o);
569 570 571 572 573
 a | b |  c  | o 
---+---+-----+---
 1 | 1 | Joe | 1
(1 row)

574
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo9(1);
575 576 577 578 579 580
SELECT * FROM vw_getfoo;
 fooid | foosubid | fooname 
-------+----------+---------
     1 |        1 | Joe
(1 row)

581
DROP VIEW vw_getfoo;
582
CREATE VIEW vw_getfoo AS SELECT * FROM getfoo9(1) WITH ORDINALITY AS t1(a,b,c,o);
583 584 585 586 587 588
SELECT * FROM vw_getfoo;
 a | b |  c  | o 
---+---+-----+---
 1 | 1 | Joe | 1
(1 row)

589
DROP VIEW vw_getfoo;
590
-- mix 'n match kinds, to exercise expandRTE and related logic
N
Noah Misch 已提交
591
select * from rows from(getfoo1(1),getfoo2(1),getfoo3(1),getfoo4(1),getfoo5(1),
592 593 594 595 596 597 598 599 600 601
                    getfoo6(1) AS (fooid int, foosubid int, fooname text),
                    getfoo7(1) AS (fooid int, foosubid int, fooname text),
                    getfoo8(1),getfoo9(1))
              with ordinality as t1(a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q,r,s,t,u);
 a | b |  c  | d | e |  f  | g | h |  i  | j | k |  l  | m | o |  p  | q | r | s |  t  | u 
---+---+-----+---+---+-----+---+---+-----+---+---+-----+---+---+-----+---+---+---+-----+---
 1 | 1 | Joe | 1 | 1 | Joe | 1 | 1 | Joe | 1 | 1 | Joe | 1 | 1 | Joe | 1 | 1 | 1 | Joe | 1
   | 1 | Ed  |   |   |     | 1 | 2 | Ed  |   |   |     | 1 | 2 | Ed  |   |   |   |     | 2
(2 rows)

N
Noah Misch 已提交
602
select * from rows from(getfoo9(1),getfoo8(1),
603 604 605 606 607 608 609 610 611 612 613
                    getfoo7(1) AS (fooid int, foosubid int, fooname text),
                    getfoo6(1) AS (fooid int, foosubid int, fooname text),
                    getfoo5(1),getfoo4(1),getfoo3(1),getfoo2(1),getfoo1(1))
              with ordinality as t1(a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q,r,s,t,u);
 a | b |  c  | d | e | f |  g  | h | i |  j  | k | l |  m  | o | p |  q  |  r  | s | t | u 
---+---+-----+---+---+---+-----+---+---+-----+---+---+-----+---+---+-----+-----+---+---+---
 1 | 1 | Joe | 1 | 1 | 1 | Joe | 1 | 1 | Joe | 1 | 1 | Joe | 1 | 1 | Joe | Joe | 1 | 1 | 1
   |   |     |   | 1 | 2 | Ed  |   |   |     | 1 | 2 | Ed  |   |   |     | Ed  | 1 |   | 2
(2 rows)

create temporary view vw_foo as
N
Noah Misch 已提交
614
  select * from rows from(getfoo9(1),
615 616 617 618 619 620 621 622 623 624 625
                      getfoo7(1) AS (fooid int, foosubid int, fooname text),
                      getfoo1(1))
                with ordinality as t1(a,b,c,d,e,f,g,n);
select * from vw_foo;
 a | b |  c  | d | e |  f  | g | n 
---+---+-----+---+---+-----+---+---
 1 | 1 | Joe | 1 | 1 | Joe | 1 | 1
   |   |     | 1 | 2 | Ed  |   | 2
(2 rows)

select pg_get_viewdef('vw_foo');
N
Noah Misch 已提交
626 627 628 629 630 631 632 633 634 635 636
                                                                    pg_get_viewdef                                                                    
------------------------------------------------------------------------------------------------------------------------------------------------------
  SELECT t1.a,                                                                                                                                       +
     t1.b,                                                                                                                                           +
     t1.c,                                                                                                                                           +
     t1.d,                                                                                                                                           +
     t1.e,                                                                                                                                           +
     t1.f,                                                                                                                                           +
     t1.g,                                                                                                                                           +
     t1.n                                                                                                                                            +
    FROM ROWS FROM(getfoo9(1), getfoo7(1) AS (fooid integer, foosubid integer, fooname text), getfoo1(1)) WITH ORDINALITY t1(a, b, c, d, e, f, g, n);
637 638 639 640 641 642 643 644 645 646 647 648
(1 row)

drop view vw_foo;
DROP FUNCTION getfoo1(int);
DROP FUNCTION getfoo2(int);
DROP FUNCTION getfoo3(int);
DROP FUNCTION getfoo4(int);
DROP FUNCTION getfoo5(int);
DROP FUNCTION getfoo6(int);
DROP FUNCTION getfoo7(int);
DROP FUNCTION getfoo8(int);
DROP FUNCTION getfoo9(int);
649
DROP FUNCTION foot(int);
650
DROP TABLE foo2;
651 652
DROP TABLE foo;
-- Rescan tests --
653 654
CREATE TEMPORARY SEQUENCE foo_rescan_seq1;
CREATE TEMPORARY SEQUENCE foo_rescan_seq2;
655
CREATE TYPE foo_rescan_t AS (i integer, s bigint);
656
CREATE FUNCTION foo_sql(int,int) RETURNS setof foo_rescan_t AS 'SELECT i, nextval(''foo_rescan_seq1'') FROM generate_series($1,$2) i;' LANGUAGE SQL;
657
-- plpgsql functions use materialize mode
658
CREATE FUNCTION foo_mat(int,int) RETURNS setof foo_rescan_t AS 'begin for i in $1..$2 loop return next (i, nextval(''foo_rescan_seq2'')); end loop; end;' LANGUAGE plpgsql;
659 660 661
--invokes ExecReScanFunctionScan - all these cases should materialize the function only once
-- LEFT JOIN on a condition that the planner can't prove to be true is used to ensure the function
-- is on the inner path of a nestloop join
662 663 664 665
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN foo_sql(11,13) ON (r+i)<100;
 r | i  | s 
---+----+---
 1 | 11 | 1
 1 | 12 | 2
 1 | 13 | 3
 2 | 11 | 1
 2 | 12 | 2
 2 | 13 | 3
 3 | 11 | 1
 3 | 12 | 2
 3 | 13 | 3
(9 rows)

682 683 684 685
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN foo_sql(11,13) WITH ORDINALITY AS f(i,s,o) ON (r+i)<100;
 r | i  | s | o 
---+----+---+---
 1 | 11 | 1 | 1
 1 | 12 | 2 | 2
 1 | 13 | 3 | 3
 2 | 11 | 1 | 1
 2 | 12 | 2 | 2
 2 | 13 | 3 | 3
 3 | 11 | 1 | 1
 3 | 12 | 2 | 2
 3 | 13 | 3 | 3
(9 rows)

702 703 704 705
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN foo_mat(11,13) ON (r+i)<100;
 r | i  | s 
---+----+---
 1 | 11 | 1
 1 | 12 | 2
 1 | 13 | 3
 2 | 11 | 1
 2 | 12 | 2
 2 | 13 | 3
 3 | 11 | 1
 3 | 12 | 2
 3 | 13 | 3
(9 rows)

722 723 724 725
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN foo_mat(11,13) WITH ORDINALITY AS f(i,s,o) ON (r+i)<100;
 r | i  | s | o 
---+----+---+---
 1 | 11 | 1 | 1
 1 | 12 | 2 | 2
 1 | 13 | 3 | 3
 2 | 11 | 1 | 1
 2 | 12 | 2 | 2
 2 | 13 | 3 | 3
 3 | 11 | 1 | 1
 3 | 12 | 2 | 2
 3 | 13 | 3 | 3
(9 rows)

742 743 744 745 746 747
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
(1 row)

N
Noah Misch 已提交
748
SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN ROWS FROM( foo_sql(11,13), foo_mat(11,13) ) WITH ORDINALITY AS f(i1,s1,i2,s2,o) ON (r+i1+i2)<100;
749 750 751 752 753 754 755 756 757 758 759 760 761
 r | i1 | s1 | i2 | s2 | o 
---+----+----+----+----+---
 1 | 11 |  1 | 11 |  1 | 1
 1 | 12 |  2 | 12 |  2 | 2
 1 | 13 |  3 | 13 |  3 | 3
 2 | 11 |  1 | 11 |  1 | 1
 2 | 12 |  2 | 12 |  2 | 2
 2 | 13 |  3 | 13 |  3 | 3
 3 | 11 |  1 | 11 |  1 | 1
 3 | 12 |  2 | 12 |  2 | 2
 3 | 13 |  3 | 13 |  3 | 3
(9 rows)

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN generate_series(11,13) f(i) ON (r+i)<100;
 r | i  
---+----
 1 | 11
 1 | 12
 1 | 13
 2 | 11
 2 | 12
 2 | 13
 3 | 11
 3 | 12
 3 | 13
(9 rows)

SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN generate_series(11,13) WITH ORDINALITY AS f(i,o) ON (r+i)<100;
 r | i  | o 
---+----+---
 1 | 11 | 1
 1 | 12 | 2
 1 | 13 | 3
 2 | 11 | 1
 2 | 12 | 2
 2 | 13 | 3
 3 | 11 | 1
 3 | 12 | 2
 3 | 13 | 3
(9 rows)

SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN unnest(array[10,20,30]) f(i) ON (r+i)<100;
 r | i  
---+----
 1 | 10
 1 | 20
 1 | 30
 2 | 10
 2 | 20
 2 | 30
 3 | 10
 3 | 20
 3 | 30
(9 rows)

SELECT * FROM (VALUES (1),(2),(3)) v(r) LEFT JOIN unnest(array[10,20,30]) WITH ORDINALITY AS f(i,o) ON (r+i)<100;
 r | i  | o 
---+----+---
 1 | 10 | 1
 1 | 20 | 2
 1 | 30 | 3
 2 | 10 | 1
 2 | 20 | 2
 2 | 30 | 3
 3 | 10 | 1
 3 | 20 | 2
 3 | 30 | 3
(9 rows)

--invokes ExecReScanFunctionScan with chgParam != NULL (using implied LATERAL)
819 820 821 822
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
823 824 825 826 827 828 829 830 831 832 833 834 835
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_sql(10+r,13);
 r | i  | s 
---+----+---
 1 | 11 | 1
 1 | 12 | 2
 1 | 13 | 3
 2 | 12 | 4
 2 | 13 | 5
 3 | 13 | 6
(6 rows)

836 837 838 839
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
840 841 842 843 844 845 846 847 848 849 850 851 852
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_sql(10+r,13) WITH ORDINALITY AS f(i,s,o);
 r | i  | s | o 
---+----+---+---
 1 | 11 | 1 | 1
 1 | 12 | 2 | 2
 1 | 13 | 3 | 3
 2 | 12 | 4 | 1
 2 | 13 | 5 | 2
 3 | 13 | 6 | 1
(6 rows)

853 854 855 856
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
857 858 859 860 861 862 863 864 865 866 867 868 869
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_sql(11,10+r);
 r | i  | s 
---+----+---
 1 | 11 | 1
 2 | 11 | 2
 2 | 12 | 3
 3 | 11 | 4
 3 | 12 | 5
 3 | 13 | 6
(6 rows)

870 871 872 873
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
874 875 876 877 878 879 880 881 882 883 884 885 886
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_sql(11,10+r) WITH ORDINALITY AS f(i,s,o);
 r | i  | s | o 
---+----+---+---
 1 | 11 | 1 | 1
 2 | 11 | 2 | 1
 2 | 12 | 3 | 2
 3 | 11 | 4 | 1
 3 | 12 | 5 | 2
 3 | 13 | 6 | 3
(6 rows)

887 888 889 890
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
(1 row)

SELECT * FROM (VALUES (11,12),(13,15),(16,20)) v(r1,r2), foo_sql(r1,r2);
 r1 | r2 | i  | s  
----+----+----+----
 11 | 12 | 11 |  1
 11 | 12 | 12 |  2
 13 | 15 | 13 |  3
 13 | 15 | 14 |  4
 13 | 15 | 15 |  5
 16 | 20 | 16 |  6
 16 | 20 | 17 |  7
 16 | 20 | 18 |  8
 16 | 20 | 19 |  9
 16 | 20 | 20 | 10
906 907
(10 rows)

908 909 910 911
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
(1 row)

SELECT * FROM (VALUES (11,12),(13,15),(16,20)) v(r1,r2), foo_sql(r1,r2) WITH ORDINALITY AS f(i,s,o);
 r1 | r2 | i  | s  | o 
----+----+----+----+---
 11 | 12 | 11 |  1 | 1
 11 | 12 | 12 |  2 | 2
 13 | 15 | 13 |  3 | 1
 13 | 15 | 14 |  4 | 2
 13 | 15 | 15 |  5 | 3
 16 | 20 | 16 |  6 | 1
 16 | 20 | 17 |  7 | 2
 16 | 20 | 18 |  8 | 3
 16 | 20 | 19 |  9 | 4
 16 | 20 | 20 | 10 | 5
927 928
(10 rows)

929 930 931 932
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
933 934 935 936 937 938 939 940 941 942 943
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_mat(10+r,13);
 r | i  | s 
---+----+---
 1 | 11 | 1
 1 | 12 | 2
 1 | 13 | 3
 2 | 12 | 4
 2 | 13 | 5
 3 | 13 | 6
944 945
(6 rows)

946 947 948 949
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
950 951 952 953 954 955 956 957 958 959 960 961 962
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_mat(10+r,13) WITH ORDINALITY AS f(i,s,o);
 r | i  | s | o 
---+----+---+---
 1 | 11 | 1 | 1
 1 | 12 | 2 | 2
 1 | 13 | 3 | 3
 2 | 12 | 4 | 1
 2 | 13 | 5 | 2
 3 | 13 | 6 | 1
(6 rows)

963 964 965 966
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
967
(1 row)
968

969 970 971 972 973 974 975 976 977
SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_mat(11,10+r);
 r | i  | s 
---+----+---
 1 | 11 | 1
 2 | 11 | 2
 2 | 12 | 3
 3 | 11 | 4
 3 | 12 | 5
 3 | 13 | 6
978 979
(6 rows)

980 981 982 983
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
984 985 986 987 988 989 990 991 992 993 994 995 996
(1 row)

SELECT * FROM (VALUES (1),(2),(3)) v(r), foo_mat(11,10+r) WITH ORDINALITY AS f(i,s,o);
 r | i  | s | o 
---+----+---+---
 1 | 11 | 1 | 1
 2 | 11 | 2 | 1
 2 | 12 | 3 | 2
 3 | 11 | 4 | 1
 3 | 12 | 5 | 2
 3 | 13 | 6 | 3
(6 rows)

997 998 999 1000
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
(1 row)

SELECT * FROM (VALUES (11,12),(13,15),(16,20)) v(r1,r2), foo_mat(r1,r2);
 r1 | r2 | i  | s  
----+----+----+----
 11 | 12 | 11 |  1
 11 | 12 | 12 |  2
 13 | 15 | 13 |  3
 13 | 15 | 14 |  4
 13 | 15 | 15 |  5
 16 | 20 | 16 |  6
 16 | 20 | 17 |  7
 16 | 20 | 18 |  8
 16 | 20 | 19 |  9
 16 | 20 | 20 | 10
(10 rows)

1018 1019 1020 1021
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
(1 row)

SELECT * FROM (VALUES (11,12),(13,15),(16,20)) v(r1,r2), foo_mat(r1,r2) WITH ORDINALITY AS f(i,s,o);
 r1 | r2 | i  | s  | o 
----+----+----+----+---
 11 | 12 | 11 |  1 | 1
 11 | 12 | 12 |  2 | 2
 13 | 15 | 13 |  3 | 1
 13 | 15 | 14 |  4 | 2
 13 | 15 | 15 |  5 | 3
 16 | 20 | 16 |  6 | 1
 16 | 20 | 17 |  7 | 2
 16 | 20 | 18 |  8 | 3
 16 | 20 | 19 |  9 | 4
 16 | 20 | 20 | 10 | 5
(10 rows)

1039 1040 1041 1042 1043 1044 1045
-- selective rescan of multiple functions:
SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
(1 row)

N
Noah Misch 已提交
1046
SELECT * FROM (VALUES (1),(2),(3)) v(r), ROWS FROM( foo_sql(11,11), foo_mat(10+r,13) );
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
 r | i  | s | i  | s 
---+----+---+----+---
 1 | 11 | 1 | 11 | 1
 1 |    |   | 12 | 2
 1 |    |   | 13 | 3
 2 | 11 | 1 | 12 | 4
 2 |    |   | 13 | 5
 3 | 11 | 1 | 13 | 6
(6 rows)

SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
(1 row)

N
Noah Misch 已提交
1063
SELECT * FROM (VALUES (1),(2),(3)) v(r), ROWS FROM( foo_sql(10+r,13), foo_mat(11,11) );
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
 r | i  | s | i  | s 
---+----+---+----+---
 1 | 11 | 1 | 11 | 1
 1 | 12 | 2 |    |  
 1 | 13 | 3 |    |  
 2 | 12 | 4 | 11 | 1
 2 | 13 | 5 |    |  
 3 | 13 | 6 | 11 | 1
(6 rows)

SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
(1 row)

N
Noah Misch 已提交
1080
SELECT * FROM (VALUES (1),(2),(3)) v(r), ROWS FROM( foo_sql(10+r,13), foo_mat(10+r,13) );
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
 r | i  | s | i  | s 
---+----+---+----+---
 1 | 11 | 1 | 11 | 1
 1 | 12 | 2 | 12 | 2
 1 | 13 | 3 | 13 | 3
 2 | 12 | 4 | 12 | 4
 2 | 13 | 5 | 13 | 5
 3 | 13 | 6 | 13 | 6
(6 rows)

SELECT setval('foo_rescan_seq1',1,false),setval('foo_rescan_seq2',1,false);
 setval | setval 
--------+--------
      1 |      1
(1 row)

N
Noah Misch 已提交
1097
SELECT * FROM generate_series(1,2) r1, generate_series(r1,3) r2, ROWS FROM( foo_sql(10+r1,13), foo_mat(10+r2,13) );
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
 r1 | r2 | i  | s  | i  | s 
----+----+----+----+----+---
  1 |  1 | 11 |  1 | 11 | 1
  1 |  1 | 12 |  2 | 12 | 2
  1 |  1 | 13 |  3 | 13 | 3
  1 |  2 | 11 |  4 | 12 | 4
  1 |  2 | 12 |  5 | 13 | 5
  1 |  2 | 13 |  6 |    |  
  1 |  3 | 11 |  7 | 13 | 6
  1 |  3 | 12 |  8 |    |  
  1 |  3 | 13 |  9 |    |  
  2 |  2 | 12 | 10 | 12 | 7
  2 |  2 | 13 | 11 | 13 | 8
  2 |  3 | 12 | 12 | 13 | 9
  2 |  3 | 13 | 13 |    |  
(13 rows)

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
SELECT * FROM (VALUES (1),(2),(3)) v(r), generate_series(10+r,20-r) f(i);
 r | i  
---+----
 1 | 11
 1 | 12
 1 | 13
 1 | 14
 1 | 15
 1 | 16
 1 | 17
 1 | 18
 1 | 19
 2 | 12
 2 | 13
 2 | 14
 2 | 15
 2 | 16
 2 | 17
 2 | 18
 3 | 13
 3 | 14
 3 | 15
 3 | 16
 3 | 17
(21 rows)

SELECT * FROM (VALUES (1),(2),(3)) v(r), generate_series(10+r,20-r) WITH ORDINALITY AS f(i,o);
 r | i  | o 
---+----+---
 1 | 11 | 1
 1 | 12 | 2
 1 | 13 | 3
 1 | 14 | 4
 1 | 15 | 5
 1 | 16 | 6
 1 | 17 | 7
 1 | 18 | 8
 1 | 19 | 9
 2 | 12 | 1
 2 | 13 | 2
 2 | 14 | 3
 2 | 15 | 4
 2 | 16 | 5
 2 | 17 | 6
 2 | 18 | 7
 3 | 13 | 1
 3 | 14 | 2
 3 | 15 | 3
 3 | 16 | 4
 3 | 17 | 5
(21 rows)

SELECT * FROM (VALUES (1),(2),(3)) v(r), unnest(array[r*10,r*20,r*30]) f(i);
 r | i  
---+----
 1 | 10
 1 | 20
 1 | 30
 2 | 20
 2 | 40
 2 | 60
 3 | 30
 3 | 60
 3 | 90
(9 rows)

SELECT * FROM (VALUES (1),(2),(3)) v(r), unnest(array[r*10,r*20,r*30]) WITH ORDINALITY AS f(i,o);
 r | i  | o 
---+----+---
 1 | 10 | 1
 1 | 20 | 2
 1 | 30 | 3
 2 | 20 | 1
 2 | 40 | 2
 2 | 60 | 3
 3 | 30 | 1
 3 | 60 | 2
 3 | 90 | 3
(9 rows)

-- deep nesting
SELECT * FROM (VALUES (1),(2),(3)) v1(r1),
              LATERAL (SELECT r1, * FROM (VALUES (10),(20),(30)) v2(r2)
                                         LEFT JOIN generate_series(21,23) f(i) ON ((r2+i)<100) OFFSET 0) s1;
 r1 | r1 | r2 | i  
----+----+----+----
  1 |  1 | 10 | 21
  1 |  1 | 10 | 22
  1 |  1 | 10 | 23
  1 |  1 | 20 | 21
  1 |  1 | 20 | 22
  1 |  1 | 20 | 23
  1 |  1 | 30 | 21
  1 |  1 | 30 | 22
  1 |  1 | 30 | 23
  2 |  2 | 10 | 21
  2 |  2 | 10 | 22
  2 |  2 | 10 | 23
  2 |  2 | 20 | 21
  2 |  2 | 20 | 22
  2 |  2 | 20 | 23
  2 |  2 | 30 | 21
  2 |  2 | 30 | 22
  2 |  2 | 30 | 23
  3 |  3 | 10 | 21
  3 |  3 | 10 | 22
  3 |  3 | 10 | 23
  3 |  3 | 20 | 21
  3 |  3 | 20 | 22
  3 |  3 | 20 | 23
  3 |  3 | 30 | 21
  3 |  3 | 30 | 22
  3 |  3 | 30 | 23
(27 rows)

SELECT * FROM (VALUES (1),(2),(3)) v1(r1),
              LATERAL (SELECT r1, * FROM (VALUES (10),(20),(30)) v2(r2)
                                         LEFT JOIN generate_series(20+r1,23) f(i) ON ((r2+i)<100) OFFSET 0) s1;
 r1 | r1 | r2 | i  
----+----+----+----
  1 |  1 | 10 | 21
  1 |  1 | 10 | 22
  1 |  1 | 10 | 23
  1 |  1 | 20 | 21
  1 |  1 | 20 | 22
  1 |  1 | 20 | 23
  1 |  1 | 30 | 21
  1 |  1 | 30 | 22
  1 |  1 | 30 | 23
  2 |  2 | 10 | 22
  2 |  2 | 10 | 23
  2 |  2 | 20 | 22
  2 |  2 | 20 | 23
  2 |  2 | 30 | 22
  2 |  2 | 30 | 23
  3 |  3 | 10 | 23
  3 |  3 | 20 | 23
  3 |  3 | 30 | 23
(18 rows)

SELECT * FROM (VALUES (1),(2),(3)) v1(r1),
              LATERAL (SELECT r1, * FROM (VALUES (10),(20),(30)) v2(r2)
                                         LEFT JOIN generate_series(r2,r2+3) f(i) ON ((r2+i)<100) OFFSET 0) s1;
 r1 | r1 | r2 | i  
----+----+----+----
  1 |  1 | 10 | 10
  1 |  1 | 10 | 11
  1 |  1 | 10 | 12
  1 |  1 | 10 | 13
  1 |  1 | 20 | 20
  1 |  1 | 20 | 21
  1 |  1 | 20 | 22
  1 |  1 | 20 | 23
  1 |  1 | 30 | 30
  1 |  1 | 30 | 31
  1 |  1 | 30 | 32
  1 |  1 | 30 | 33
  2 |  2 | 10 | 10
  2 |  2 | 10 | 11
  2 |  2 | 10 | 12
  2 |  2 | 10 | 13
  2 |  2 | 20 | 20
  2 |  2 | 20 | 21
  2 |  2 | 20 | 22
  2 |  2 | 20 | 23
  2 |  2 | 30 | 30
  2 |  2 | 30 | 31
  2 |  2 | 30 | 32
  2 |  2 | 30 | 33
  3 |  3 | 10 | 10
  3 |  3 | 10 | 11
  3 |  3 | 10 | 12
  3 |  3 | 10 | 13
  3 |  3 | 20 | 20
  3 |  3 | 20 | 21
  3 |  3 | 20 | 22
  3 |  3 | 20 | 23
  3 |  3 | 30 | 30
  3 |  3 | 30 | 31
  3 |  3 | 30 | 32
  3 |  3 | 30 | 33
(36 rows)

SELECT * FROM (VALUES (1),(2),(3)) v1(r1),
              LATERAL (SELECT r1, * FROM (VALUES (10),(20),(30)) v2(r2)
                                         LEFT JOIN generate_series(r1,2+r2/5) f(i) ON ((r2+i)<100) OFFSET 0) s1;
 r1 | r1 | r2 | i 
----+----+----+---
  1 |  1 | 10 | 1
  1 |  1 | 10 | 2
  1 |  1 | 10 | 3
  1 |  1 | 10 | 4
  1 |  1 | 20 | 1
  1 |  1 | 20 | 2
  1 |  1 | 20 | 3
  1 |  1 | 20 | 4
  1 |  1 | 20 | 5
  1 |  1 | 20 | 6
  1 |  1 | 30 | 1
  1 |  1 | 30 | 2
  1 |  1 | 30 | 3
  1 |  1 | 30 | 4
  1 |  1 | 30 | 5
  1 |  1 | 30 | 6
  1 |  1 | 30 | 7
  1 |  1 | 30 | 8
  2 |  2 | 10 | 2
  2 |  2 | 10 | 3
  2 |  2 | 10 | 4
  2 |  2 | 20 | 2
  2 |  2 | 20 | 3
  2 |  2 | 20 | 4
  2 |  2 | 20 | 5
  2 |  2 | 20 | 6
  2 |  2 | 30 | 2
  2 |  2 | 30 | 3
  2 |  2 | 30 | 4
  2 |  2 | 30 | 5
  2 |  2 | 30 | 6
  2 |  2 | 30 | 7
  2 |  2 | 30 | 8
  3 |  3 | 10 | 3
  3 |  3 | 10 | 4
  3 |  3 | 20 | 3
  3 |  3 | 20 | 4
  3 |  3 | 20 | 5
  3 |  3 | 20 | 6
  3 |  3 | 30 | 3
  3 |  3 | 30 | 4
  3 |  3 | 30 | 5
  3 |  3 | 30 | 6
  3 |  3 | 30 | 7
  3 |  3 | 30 | 8
(45 rows)

DROP FUNCTION foo_sql(int,int);
DROP FUNCTION foo_mat(int,int);
1352 1353
DROP SEQUENCE foo_rescan_seq1;
DROP SEQUENCE foo_rescan_seq2;
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
--
-- Test cases involving OUT parameters
--
CREATE FUNCTION foo(in f1 int, out f2 int)
AS 'select $1+1' LANGUAGE sql;
SELECT foo(42);
 foo 
-----
  43
(1 row)

SELECT * FROM foo(42);
1366 1367 1368
 f2 
----
 43
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
(1 row)

SELECT * FROM foo(42) AS p(x);
 x  
----
 43
(1 row)

-- explicit spec of return type is OK
CREATE OR REPLACE FUNCTION foo(in f1 int, out f2 int) RETURNS int
AS 'select $1+1' LANGUAGE sql;
-- error, wrong result type
CREATE OR REPLACE FUNCTION foo(in f1 int, out f2 int) RETURNS float
AS 'select $1+1' LANGUAGE sql;
ERROR:  function result type must be integer because of OUT parameters
-- with multiple OUT params you must get a RECORD result
CREATE OR REPLACE FUNCTION foo(in f1 int, out f2 int, out f3 text) RETURNS int
AS 'select $1+1' LANGUAGE sql;
ERROR:  function result type must be record because of OUT parameters
CREATE OR REPLACE FUNCTION foo(in f1 int, out f2 int, out f3 text)
RETURNS record
AS 'select $1+1' LANGUAGE sql;
ERROR:  cannot change return type of existing function
1392
HINT:  Use DROP FUNCTION foo(integer) first.
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
CREATE OR REPLACE FUNCTION foor(in f1 int, out f2 int, out text)
AS $$select $1-1, $1::text || 'z'$$ LANGUAGE sql;
SELECT f1, foor(f1) FROM int4_tbl;
     f1      |            foor            
-------------+----------------------------
           0 | (-1,0z)
      123456 | (123455,123456z)
     -123456 | (-123457,-123456z)
  2147483647 | (2147483646,2147483647z)
 -2147483647 | (-2147483648,-2147483647z)
(5 rows)

SELECT * FROM foor(42);
 f2 | column2 
----+---------
 41 | 42z
(1 row)

SELECT * FROM foor(42) AS p(a,b);
 a  |  b  
----+-----
 41 | 42z
(1 row)

CREATE OR REPLACE FUNCTION foob(in f1 int, inout f2 int, out text)
AS $$select $2-1, $1::text || 'z'$$ LANGUAGE sql;
SELECT f1, foob(f1, f1/2) FROM int4_tbl;
     f1      |            foob            
-------------+----------------------------
           0 | (-1,0z)
      123456 | (61727,123456z)
     -123456 | (-61729,-123456z)
  2147483647 | (1073741822,2147483647z)
 -2147483647 | (-1073741824,-2147483647z)
(5 rows)

SELECT * FROM foob(42, 99);
 f2 | column2 
----+---------
 98 | 42z
(1 row)

SELECT * FROM foob(42, 99) AS p(a,b);
 a  |  b  
----+-----
 98 | 42z
(1 row)

-- Can reference function with or without OUT params for DROP, etc
DROP FUNCTION foo(int);
DROP FUNCTION foor(in f2 int, out f1 int, out text);
DROP FUNCTION foob(in f1 int, inout f2 int);
--
-- For my next trick, polymorphic OUT parameters
--
CREATE FUNCTION dup (f1 anyelement, f2 out anyelement, f3 out anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
SELECT dup(22);
      dup       
----------------
 (22,"{22,22}")
(1 row)

SELECT dup('xyz');	-- fails
1457
ERROR:  could not determine polymorphic type because input has type "unknown"
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
SELECT dup('xyz'::text);
        dup        
-------------------
 (xyz,"{xyz,xyz}")
(1 row)

SELECT * FROM dup('xyz'::text);
 f2  |    f3     
-----+-----------
 xyz | {xyz,xyz}
(1 row)

1470 1471 1472 1473
-- fails, as we are attempting to rename first argument
CREATE OR REPLACE FUNCTION dup (inout f2 anyelement, out f3 anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
ERROR:  cannot change name of input parameter "f1"
1474
HINT:  Use DROP FUNCTION dup(anyelement) first.
1475 1476
DROP FUNCTION dup(anyelement);
-- equivalent behavior, though different name exposed for input arg
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
CREATE OR REPLACE FUNCTION dup (inout f2 anyelement, out f3 anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
SELECT dup(22);
      dup       
----------------
 (22,"{22,22}")
(1 row)

DROP FUNCTION dup(anyelement);
-- fails, no way to deduce outputs
CREATE FUNCTION bad (f1 int, out f2 anyelement, out f3 anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
ERROR:  cannot determine result data type
1490
DETAIL:  A function returning a polymorphic type must have at least one polymorphic argument.
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
--
-- table functions
--
CREATE OR REPLACE FUNCTION foo()
RETURNS TABLE(a int)
AS $$ SELECT a FROM generate_series(1,5) a(a) $$ LANGUAGE sql;
SELECT * FROM foo();
 a 
---
 1
 2
 3
 4
 5
(5 rows)

DROP FUNCTION foo();
CREATE OR REPLACE FUNCTION foo(int)
RETURNS TABLE(a int, b int)
AS $$ SELECT a, b
         FROM generate_series(1,$1) a(a),
              generate_series(1,$1) b(b) $$ LANGUAGE sql;
SELECT * FROM foo(3);
 a | b 
---+---
 1 | 1
 1 | 2
 1 | 3
 2 | 1
 2 | 2
 2 | 3
 3 | 1
 3 | 2
 3 | 3
(9 rows)

DROP FUNCTION foo(int);
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
-- case that causes change of typmod knowledge during inlining
CREATE OR REPLACE FUNCTION foo()
RETURNS TABLE(a varchar(5))
AS $$ SELECT 'hello'::varchar(5) $$ LANGUAGE sql STABLE;
SELECT * FROM foo() GROUP BY 1;
   a   
-------
 hello
(1 row)

DROP FUNCTION foo();
1539 1540 1541 1542
--
-- some tests on SQL functions with RETURNING
--
create temp table tt(f1 serial, data text);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
-- GPDB: The tests below which throw NOTICEs, throw them in indeterminate
-- order, if the rows are hashed to different segments. Force the rows
-- that have problem to be hashed to the same segment, using a custom hash
-- function.
CREATE OPERATOR FAMILY dummy_int_hash_ops USING hash;
CREATE FUNCTION dummy_hashfunc(int) RETURNS int AS $$
begin
  return CASE WHEN $1 BETWEEN 7 AND 15 THEN 0 ELSE $1 END;
end; $$ LANGUAGE plpgsql STRICT IMMUTABLE;
CREATE OPERATOR CLASS dummy_int_hash_ops FOR TYPE int4
  USING hash FAMILY dummy_int_hash_ops AS
  OPERATOR 1 =,
  FUNCTION 1 dummy_hashfunc(int);
ALTER TABLE tt SET DISTRIBUTED BY (f1 dummy_int_hash_ops);
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
create function insert_tt(text) returns int as
$$ insert into tt(data) values($1) returning f1 $$
language sql;
select insert_tt('foo');
 insert_tt 
-----------
         1
(1 row)

select insert_tt('bar');
 insert_tt 
-----------
         2
(1 row)

select * from tt;
 f1 | data 
----+------
  1 | foo
  2 | bar
(2 rows)

-- insert will execute to completion even if function needs just 1 row
create or replace function insert_tt(text) returns int as
1581
$$ insert into tt(data) values($1),($1||$1) returning (f1>0)::int $$
1582 1583 1584 1585
language sql;
select insert_tt('fool');
 insert_tt 
-----------
1586
         1
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
(1 row)

select * from tt;
 f1 |   data   
----+----------
  1 | foo
  2 | bar
  3 | fool
  4 | foolfool
(4 rows)

-- setof does what's expected
create or replace function insert_tt2(text,text) returns setof int as
$$ insert into tt(data) values($1),($2) returning f1 $$
language sql;
select insert_tt2('foolish','barrish');
 insert_tt2 
------------
          5
          6
(2 rows)

select * from insert_tt2('baz','quux');
 insert_tt2 
------------
          7
          8
(2 rows)

select * from tt;
 f1 |   data   
----+----------
  1 | foo
  2 | bar
  3 | fool
  4 | foolfool
  5 | foolish
  6 | barrish
  7 | baz
  8 | quux
(8 rows)

-- limit doesn't prevent execution to completion
select insert_tt2('foolish','barrish') limit 1;
 insert_tt2 
------------
          9
(1 row)

select * from tt;
 f1 |   data   
----+----------
  1 | foo
  2 | bar
  3 | fool
  4 | foolfool
  5 | foolish
  6 | barrish
  7 | baz
  8 | quux
  9 | foolish
 10 | barrish
(10 rows)

-- triggers will fire, too
create function noticetrigger() returns trigger as $$
begin
  raise notice 'noticetrigger % %', new.f1, new.data;
  return null;
end $$ language plpgsql;
create trigger tnoticetrigger after insert on tt for each row
execute procedure noticetrigger();
select insert_tt2('foolme','barme') limit 1;
1660 1661 1662 1663
NOTICE:  noticetrigger 11 foolme
CONTEXT:  SQL function "insert_tt2" statement 1
NOTICE:  noticetrigger 12 barme
CONTEXT:  SQL function "insert_tt2" statement 1
1664 1665
 insert_tt2 
------------
1666
         11
1667 1668 1669
(1 row)

select * from tt;
1670 1671 1672
 f1 |   data   
----+----------
  1 | foo
1673
  2 | bar
1674 1675
  3 | fool
  4 | foolfool
1676 1677
  5 | foolish
  6 | barrish
1678 1679
  7 | baz
  8 | quux
1680 1681
  9 | foolish
 10 | barrish
1682 1683 1684
 11 | foolme
 12 | barme
(12 rows)
1685 1686 1687 1688 1689 1690

-- and rules work
create temp table tt_log(f1 int, data text);
create rule insert_tt_rule as on insert to tt do also
  insert into tt_log values(new.*);
select insert_tt2('foollog','barlog') limit 1;
1691 1692 1693 1694
NOTICE:  noticetrigger 13 foollog
CONTEXT:  SQL function "insert_tt2" statement 1
NOTICE:  noticetrigger 14 barlog
CONTEXT:  SQL function "insert_tt2" statement 1
1695 1696
 insert_tt2 
------------
1697
         13
1698 1699 1700
(1 row)

select * from tt;
1701 1702 1703
 f1 |   data   
----+----------
  1 | foo
1704
  2 | bar
1705 1706
  3 | fool
  4 | foolfool
1707 1708
  5 | foolish
  6 | barrish
1709 1710
  7 | baz
  8 | quux
1711 1712
  9 | foolish
 10 | barrish
1713 1714 1715 1716 1717
 11 | foolme
 12 | barme
 13 | foollog
 14 | barlog
(14 rows)
1718 1719 1720 1721 1722 1723

-- note that nextval() gets executed a second time in the rule expansion,
-- which is expected.
select * from tt_log;
 f1 |  data   
----+---------
1724 1725
 15 | foollog
 16 | barlog
1726 1727
(2 rows)

1728 1729 1730 1731 1732 1733
-- test case for a whole-row-variable bug
create function foo1(n integer, out a text, out b text)
  returns setof record
  language sql
  as $$ select 'foo ' || i, 'bar ' || i from generate_series(1,$1) i $$;
set work_mem='64kB';
1734
WARNING:  "work_mem": setting is deprecated, and may be removed in a future release.
1735 1736 1737 1738 1739 1740 1741
select t.a, t, t.a from foo1(10000) t limit 1;
   a   |         t         |   a   
-------+-------------------+-------
 foo 1 | ("foo 1","bar 1") | foo 1
(1 row)

reset work_mem;
1742
WARNING:  "work_mem": setting is deprecated, and may be removed in a future release.
1743 1744 1745 1746 1747 1748 1749
select t.a, t, t.a from foo1(10000) t limit 1;
   a   |         t         |   a   
-------+-------------------+-------
 foo 1 | ("foo 1","bar 1") | foo 1
(1 row)

drop function foo1(n integer);
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
-- test use of SQL functions returning record
-- this is supported in some cases where the query doesn't specify
-- the actual record type ...
create function array_to_set(anyarray) returns setof record as $$
  select i AS "index", $1[i] AS "value" from generate_subscripts($1, 1) i
$$ language sql strict immutable;
select array_to_set(array['one', 'two']);
 array_to_set 
--------------
 (1,one)
 (2,two)
(2 rows)

select * from array_to_set(array['one', 'two']) as t(f1 int,f2 text);
 f1 | f2  
----+-----
  1 | one
  2 | two
(2 rows)

select * from array_to_set(array['one', 'two']); -- fail
ERROR:  a column definition list is required for functions returning "record"
LINE 1: select * from array_to_set(array['one', 'two']);
                      ^
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
create temp table foo(f1 int8, f2 int8);
create function testfoo() returns record as $$
  insert into foo values (1,2) returning *;
$$ language sql;
select testfoo();
 testfoo 
---------
 (1,2)
(1 row)

select * from testfoo() as t(f1 int8,f2 int8);
 f1 | f2 
----+----
  1 |  2
(1 row)

select * from testfoo(); -- fail
ERROR:  a column definition list is required for functions returning "record"
LINE 1: select * from testfoo();
                      ^
drop function testfoo();
create function testfoo() returns setof record as $$
  insert into foo values (1,2), (3,4) returning *;
$$ language sql;
select testfoo();
 testfoo 
---------
 (1,2)
 (3,4)
(2 rows)

select * from testfoo() as t(f1 int8,f2 int8);
 f1 | f2 
----+----
  1 |  2
  3 |  4
(2 rows)

select * from testfoo(); -- fail
ERROR:  a column definition list is required for functions returning "record"
LINE 1: select * from testfoo();
                      ^
drop function testfoo();
1817
--
1818
-- Check some cases involving added/dropped columns in a rowtype result
1819
--
1820 1821 1822
create temp table users (userid text, seq int, email text, todrop bool, moredrop int, enabled bool);
insert into users values ('id',1,'email',true,11,true);
insert into users values ('id2',2,'email2',true,12,true);
1823 1824 1825 1826 1827
alter table users drop column todrop;
create or replace function get_first_user() returns users as
$$ SELECT * FROM users ORDER BY userid LIMIT 1; $$
language sql stable;
SELECT get_first_user();
1828 1829 1830
  get_first_user   
-------------------
 (id,1,email,11,t)
1831 1832 1833
(1 row)

SELECT * FROM get_first_user();
1834 1835 1836
 userid | seq | email | moredrop | enabled 
--------+-----+-------+----------+---------
 id     |   1 | email |       11 | t
1837 1838 1839 1840 1841 1842
(1 row)

create or replace function get_users() returns setof users as
$$ SELECT * FROM users ORDER BY userid; $$
language sql stable;
SELECT get_users();
1843 1844 1845 1846
      get_users      
---------------------
 (id,1,email,11,t)
 (id2,2,email2,12,t)
1847 1848 1849
(2 rows)

SELECT * FROM get_users();
1850 1851 1852 1853
 userid | seq | email  | moredrop | enabled 
--------+-----+--------+----------+---------
 id     |   1 | email  |       11 | t
 id2    |   2 | email2 |       12 | t
1854 1855
(2 rows)

1856
SELECT * FROM get_users() WITH ORDINALITY;   -- make sure ordinality copes
1857 1858 1859 1860 1861 1862 1863
 userid | seq | email  | moredrop | enabled | ordinality 
--------+-----+--------+----------+---------+------------
 id     |   1 | email  |       11 | t       |          1
 id2    |   2 | email2 |       12 | t       |          2
(2 rows)

-- multiple functions vs. dropped columns
N
Noah Misch 已提交
1864
SELECT * FROM ROWS FROM(generate_series(10,11), get_users()) WITH ORDINALITY;
1865 1866 1867 1868 1869 1870
 generate_series | userid | seq | email  | moredrop | enabled | ordinality 
-----------------+--------+-----+--------+----------+---------+------------
              10 | id     |   1 | email  |       11 | t       |          1
              11 | id2    |   2 | email2 |       12 | t       |          2
(2 rows)

N
Noah Misch 已提交
1871
SELECT * FROM ROWS FROM(get_users(), generate_series(10,11)) WITH ORDINALITY;
1872 1873 1874 1875
 userid | seq | email  | moredrop | enabled | generate_series | ordinality 
--------+-----+--------+----------+---------+-----------------+------------
 id     |   1 | email  |       11 | t       |              10 |          1
 id2    |   2 | email2 |       12 | t       |              11 |          2
1876 1877
(2 rows)

1878 1879
-- check that we can cope with post-parsing changes in rowtypes
create temp view usersview as
N
Noah Misch 已提交
1880
SELECT * FROM ROWS FROM(get_users(), generate_series(10,11)) WITH ORDINALITY;
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
select * from usersview;
 userid | seq | email  | moredrop | enabled | generate_series | ordinality 
--------+-----+--------+----------+---------+-----------------+------------
 id     |   1 | email  |       11 | t       |              10 |          1
 id2    |   2 | email2 |       12 | t       |              11 |          2
(2 rows)

alter table users drop column moredrop;
select * from usersview;
 userid | seq | email  | moredrop | enabled | generate_series | ordinality 
--------+-----+--------+----------+---------+-----------------+------------
 id     |   1 | email  |          | t       |              10 |          1
 id2    |   2 | email2 |          | t       |              11 |          2
(2 rows)

alter table users add column junk text;
select * from usersview;
 userid | seq | email  | moredrop | enabled | generate_series | ordinality 
--------+-----+--------+----------+---------+-----------------+------------
 id     |   1 | email  |          | t       |              10 |          1
 id2    |   2 | email2 |          | t       |              11 |          2
1902 1903
(2 rows)

1904 1905 1906 1907 1908
alter table users alter column seq type numeric;
select * from usersview;  -- expect clean failure
ERROR:  attribute 2 has wrong type
DETAIL:  Table has type numeric, but query expects integer.
drop view usersview;
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
drop function get_first_user();
drop function get_users();
drop table users;
-- this won't get inlined because of type coercion, but it shouldn't fail
create or replace function foobar() returns setof text as
$$ select 'foo'::varchar union all select 'bar'::varchar ; $$
language sql stable;
select foobar();
 foobar 
--------
 foo
 bar
(2 rows)

select * from foobar();
 foobar 
--------
 foo
 bar
(2 rows)

drop function foobar();
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
-- check handling of a SQL function with multiple OUT params (bug #5777)
create or replace function foobar(out integer, out numeric) as
$$ select (1, 2.1) $$ language sql;
select * from foobar();
 column1 | column2 
---------+---------
       1 |     2.1
(1 row)

create or replace function foobar(out integer, out numeric) as
$$ select (1, 2) $$ language sql;
select * from foobar();  -- fail
ERROR:  function return row and query-specified return row do not match
DETAIL:  Returned type integer at ordinal position 2, but query expects numeric.
create or replace function foobar(out integer, out numeric) as
$$ select (1, 2.1, 3) $$ language sql;
select * from foobar();  -- fail
ERROR:  function return row and query-specified return row do not match
DETAIL:  Returned row contains 3 attributes, but query expects 2.
drop function foobar();
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
-- check behavior when a function's input sometimes returns a set (bug #8228)
SELECT *,
  lower(CASE WHEN id = 2 THEN (regexp_matches(str, '^0*([1-9]\d+)$'))[1]
        ELSE str
        END)
FROM
  (VALUES (1,''), (2,'0000000049404'), (3,'FROM 10000000876')) v(id, str);
 id |       str        |      lower       
----+------------------+------------------
  1 |                  | 
  2 | 0000000049404    | 49404
  3 | FROM 10000000876 | from 10000000876
(3 rows)

1965 1966 1967 1968 1969 1970
-- check whole-row-Var handling in nested lateral functions (bug #11703)
create function extractq2(t int8_tbl) returns int8 as $$
  select t.q2
$$ language sql immutable;
explain (verbose, costs off)
select x from int8_tbl, extractq2(int8_tbl) f(x);
1971 1972 1973
                   QUERY PLAN                   
------------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)
1974
   Output: f.x
1975
   ->  Nested Loop
1976
         Output: f.x
1977 1978 1979 1980 1981
         ->  Seq Scan on public.int8_tbl
               Output: int8_tbl.q1, int8_tbl.q2
         ->  Function Scan on f
               Output: f.x
               Function Call: int8_tbl.q2
1982
 Optimizer: Postgres query optimizer
1983
(10 rows)
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995

select x from int8_tbl, extractq2(int8_tbl) f(x);
         x         
-------------------
               456
  4567890123456789
               123
  4567890123456789
 -4567890123456789
(5 rows)

create function extractq2_2(t int8_tbl) returns table(ret1 int8) as $$
1996
  select extractq2(t) offset 0
1997 1998 1999
$$ language sql immutable;
explain (verbose, costs off)
select x from int8_tbl, extractq2_2(int8_tbl) f(x);
2000 2001
                   QUERY PLAN                   
------------------------------------------------
2002 2003
 Nested Loop
   Output: ((int8_tbl.*).q2)
2004
   ->  Gather Motion 3:1  (slice1; segments: 3)
2005
         Output: int8_tbl.*
2006 2007 2008 2009 2010 2011
         ->  Seq Scan on public.int8_tbl
               Output: int8_tbl.*
   ->  Materialize
         Output: ((int8_tbl.*).q2)
         ->  Result
               Output: (int8_tbl.*).q2
2012
 Optimizer: Postgres query optimizer
2013
(11 rows)
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024

select x from int8_tbl, extractq2_2(int8_tbl) f(x);
         x         
-------------------
               456
  4567890123456789
               123
  4567890123456789
 -4567890123456789
(5 rows)

2025 2026 2027 2028 2029 2030
-- without the "offset 0", this function gets optimized quite differently
create function extractq2_2_opt(t int8_tbl) returns table(ret1 int8) as $$
  select extractq2(t)
$$ language sql immutable;
explain (verbose, costs off)
select x from int8_tbl, extractq2_2_opt(int8_tbl) f(x);
2031 2032 2033
                QUERY PLAN                
------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)
2034
   Output: int8_tbl.q2
2035 2036 2037 2038
   ->  Seq Scan on public.int8_tbl
         Output: int8_tbl.q2
 Optimizer: Postgres query optimizer
(5 rows)
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049

select x from int8_tbl, extractq2_2_opt(int8_tbl) f(x);
         x         
-------------------
               456
  4567890123456789
               123
  4567890123456789
 -4567890123456789
(5 rows)

2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
-- gpdb: test append node in subquery_motionHazard_walker(). Without that
-- change the select query below will panic.
create function extractq2_append(t int8_tbl) returns table(ret1 int8) as $$
  select extractq2(t) union all select extractq2(t)
$$ language sql immutable;
explain (verbose, costs off)
select x from int8_tbl, extractq2_append(int8_tbl) f(x);
                   QUERY PLAN                   
------------------------------------------------
 Nested Loop
   Output: ((int8_tbl.*).q2)
   ->  Gather Motion 3:1  (slice1; segments: 3)
         Output: int8_tbl.*
         ->  Seq Scan on public.int8_tbl
               Output: int8_tbl.*
   ->  Materialize
         Output: ((int8_tbl.*).q2)
         ->  Append
               ->  Result
                     Output: (int8_tbl.*).q2
               ->  Result
                     Output: (int8_tbl.*).q2
2072
 Optimizer: Postgres query optimizer
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
(14 rows)

select x from int8_tbl, extractq2_append(int8_tbl) f(x);
         x         
-------------------
               123
               123
  4567890123456789
  4567890123456789
 -4567890123456789
 -4567890123456789
               456
               456
  4567890123456789
  4567890123456789
(10 rows)

2090 2091 2092 2093
create function extractq2_wapper(t int8_tbl) returns table(ret1 int8) as $$      
  select (select extractq2(t))                                                  
$$ language sql immutable;                                                      
explain (verbose, costs off) select x from int8_tbl, extractq2_wapper(int8_tbl) f(x);
2094 2095 2096 2097 2098 2099
                QUERY PLAN                
------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)
   Output: ((SubPlan 1))
   ->  Seq Scan on public.int8_tbl
         Output: (SubPlan 1)
2100
         SubPlan 1
2101 2102
           ->  Result
                 Output: (int8_tbl.*).q2
2103
 Optimizer: Postgres query optimizer
2104
(8 rows)
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115

select x from int8_tbl, extractq2_wapper(int8_tbl) f(x);
         x         
-------------------
               123
  4567890123456789
 -4567890123456789
               456
  4567890123456789
(5 rows)