-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplpgsql_control.sql
More file actions
1135 lines (1032 loc) · 30 KB
/
Copy pathplpgsql_control.sql
File metadata and controls
1135 lines (1032 loc) · 30 KB
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Tests for PL/pgSQL control structures
--
-- integer FOR loop
do LANGUAGE uplpgsql $$
begin
-- basic case
for i in 1..3 loop
raise notice '1..3: i = %', i;
end loop;
-- with BY, end matches exactly
for i in 1..10 by 3 loop
raise notice '1..10 by 3: i = %', i;
end loop;
-- with BY, end does not match
for i in 1..11 by 3 loop
raise notice '1..11 by 3: i = %', i;
end loop;
-- zero iterations
for i in 1..0 by 3 loop
raise notice '1..0 by 3: i = %', i;
end loop;
-- REVERSE
for i in reverse 10..0 by 3 loop
raise notice 'reverse 10..0 by 3: i = %', i;
end loop;
-- potential overflow
for i in 2147483620..2147483647 by 10 loop
raise notice '2147483620..2147483647 by 10: i = %', i;
end loop;
-- potential overflow, reverse direction
for i in reverse -2147483620..-2147483647 by 10 loop
raise notice 'reverse -2147483620..-2147483647 by 10: i = %', i;
end loop;
end$$;
-- BY can't be zero or negative
do LANGUAGE uplpgsql $$
begin
for i in 1..3 by 0 loop
raise notice '1..3 by 0: i = %', i;
end loop;
end$$;
do LANGUAGE uplpgsql $$
begin
for i in 1..3 by -1 loop
raise notice '1..3 by -1: i = %', i;
end loop;
end$$;
do LANGUAGE uplpgsql $$
begin
for i in reverse 1..3 by -1 loop
raise notice 'reverse 1..3 by -1: i = %', i;
end loop;
end$$;
-- CONTINUE statement
create table conttesttbl(idx serial, v integer);
insert into conttesttbl(v) values(10);
insert into conttesttbl(v) values(20);
insert into conttesttbl(v) values(30);
insert into conttesttbl(v) values(40);
create function continue_test1() returns void as $$
declare _i integer = 0; _r record;
begin
raise notice '---1---';
loop
_i := _i + 1;
raise notice '%', _i;
continue when _i < 10;
exit;
end loop;
raise notice '---2---';
<<lbl>>
loop
_i := _i - 1;
loop
raise notice '%', _i;
continue lbl when _i > 0;
exit lbl;
end loop;
end loop;
raise notice '---3---';
<<the_loop>>
while _i < 10 loop
_i := _i + 1;
continue the_loop when _i % 2 = 0;
raise notice '%', _i;
end loop;
raise notice '---4---';
for _i in 1..10 loop
begin
-- applies to outer loop, not the nested begin block
continue when _i < 5;
raise notice '%', _i;
end;
end loop;
raise notice '---5---';
for _r in select * from conttesttbl loop
continue when _r.v <= 20;
raise notice '%', _r.v;
end loop;
raise notice '---6---';
for _r in execute 'select * from conttesttbl' loop
continue when _r.v <= 20;
raise notice '%', _r.v;
end loop;
raise notice '---7---';
<<looplabel>>
for _i in 1..3 loop
continue looplabel when _i = 2;
raise notice '%', _i;
end loop;
raise notice '---8---';
_i := 1;
while _i <= 3 loop
raise notice '%', _i;
_i := _i + 1;
continue when _i = 3;
end loop;
raise notice '---9---';
for _r in select * from conttesttbl order by v limit 1 loop
raise notice '%', _r.v;
continue;
end loop;
raise notice '---10---';
for _r in execute 'select * from conttesttbl order by v limit 1' loop
raise notice '%', _r.v;
continue;
end loop;
raise notice '---11---';
<<outerlooplabel>>
for _i in 1..2 loop
raise notice 'outer %', _i;
<<innerlooplabel>>
for _j in 1..3 loop
continue outerlooplabel when _j = 2;
raise notice 'inner %', _j;
end loop;
end loop;
end; $$ LANGUAGE uplpgsql;
select continue_test1();
-- should fail: CONTINUE is only legal inside a loop
create function continue_error1() returns void as $$
begin
begin
continue;
end;
end;
$$ LANGUAGE uplpgsql;
-- should fail: unlabeled EXIT is only legal inside a loop
create function exit_error1() returns void as $$
begin
begin
exit;
end;
end;
$$ LANGUAGE uplpgsql;
-- should fail: no such label
create function continue_error2() returns void as $$
begin
begin
loop
continue no_such_label;
end loop;
end;
end;
$$ LANGUAGE uplpgsql;
-- should fail: no such label
create function exit_error2() returns void as $$
begin
begin
loop
exit no_such_label;
end loop;
end;
end;
$$ LANGUAGE uplpgsql;
-- should fail: CONTINUE can't reference the label of a named block
create function continue_error3() returns void as $$
begin
<<begin_block1>>
begin
loop
continue begin_block1;
end loop;
end;
end;
$$ LANGUAGE uplpgsql;
-- On the other hand, EXIT *can* reference the label of a named block
create function exit_block1() returns void as $$
begin
<<begin_block1>>
begin
loop
exit begin_block1;
raise exception 'should not get here';
end loop;
end;
end;
$$ LANGUAGE uplpgsql;
select exit_block1();
--
-- EXIT with a block label.
--
-- A labeled BEGIN...END is an EXIT target just as a loop is, so codegen has to
-- push it onto the same stack -- but only as an EXIT target: an unlabeled
-- EXIT/CONTINUE must look straight through it to the innermost real loop, and
-- CONTINUE may never name a block. When the block carries an exception
-- handler the jump also has to run the subtransaction release on its way out
-- rather than branch past it.
--
create table exitblk_t (v int);
-- control resumes after the block, skipping the rest of its body
create function exit_block_resumes() returns int language uplpgsql as $$
declare r int := 0;
begin
<<b>> begin r := 1; exit b; r := 99; end;
r := r + 10;
return r;
end; $$;
select exit_block_resumes();
-- EXIT unwinds out of nested loops to the block label
create function exit_block_nested_loop() returns int language uplpgsql as $$
declare r int := 0;
begin
<<b>> begin
for i in 1..10 loop
for j in 1..10 loop r := r + 1; exit b; end loop;
end loop;
end;
return r;
end; $$;
select exit_block_nested_loop();
-- conditional EXIT on a block label
create function exit_block_when() returns int language uplpgsql as $$
declare r int := 0;
begin
<<b>> begin
for i in 1..10 loop r := r + 1; exit b when i = 4; end loop;
end;
return r;
end; $$;
select exit_block_when();
-- an unlabeled EXIT inside a labeled block still targets the enclosing loop
create function exit_unlabeled_in_block() returns int language uplpgsql as $$
declare r int := 0;
begin
for i in 1..5 loop
<<b>> begin r := r + 1; exit; end;
end loop;
return r;
end; $$;
select exit_unlabeled_in_block();
-- inner vs outer block labels resolve independently
create function exit_outer_from_inner() returns int language uplpgsql as $$
declare r int := 0;
begin
<<o>> begin <<i>> begin r := 1; exit o; end; r := 99; end;
return r;
end; $$;
select exit_outer_from_inner();
create function exit_inner_only() returns int language uplpgsql as $$
declare r int := 0;
begin
<<o>> begin <<i>> begin r := 1; exit i; r := 50; end; r := r + 5; end;
return r;
end; $$;
select exit_inner_only();
-- CONTINUE may not name a block label
create function continue_block_label() returns int language uplpgsql as $$
begin
<<b>> begin loop continue b; end loop; end;
return 1;
end; $$;
-- EXIT must name a label that actually encloses the statement
create function exit_no_such_label() returns int language uplpgsql as $$
begin
<<b>> begin exit zzz; end;
return 1;
end; $$;
-- EXIT out of a block that has an exception handler must still release the
-- subtransaction: the inserted row has to survive
create function exit_block_exc_subxact() returns int language uplpgsql as $$
begin
<<b>> begin
insert into exitblk_t values (5);
exit b;
exception when others then null;
end;
return (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_block_exc_subxact();
-- EXIT from inside a handler body leaves the block, running handler cleanup on
-- the way: the failed insert rolls back, the handler's insert commits
create function exit_handler_subxact() returns int language uplpgsql as $$
begin
<<b>> begin
insert into exitblk_t values (1);
raise exception 'boom';
exception when others then
insert into exitblk_t values (2);
exit b;
end;
return (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_handler_subxact();
-- statements after EXIT inside a handler are skipped
create function exit_from_handler_body() returns int language uplpgsql as $$
declare r int := 0;
begin
<<b>> begin
raise exception 'boom';
exception when others then
r := 7; exit b; r := 99;
end;
return r + 1;
end; $$;
select exit_from_handler_body();
-- exception handling still works in a later block after an EXIT-ed one
create function exc_still_works_after_exit() returns int language uplpgsql as $$
declare r int := 0;
begin
<<b>> begin exit b; exception when others then r := 9; end;
begin raise exception 'x'; exception when others then r := 3; end;
return r;
end; $$;
select exc_still_works_after_exit();
--
-- EXIT/CONTINUE whose target lies *outside* an exception block.
--
-- These jumps cross the block's frame rather than leave through its own exit
-- paths, so codegen has to release each crossed frame on the way out --
-- commit the subtransaction from a try body, run handler cleanup from a
-- handler. Branching past that release left the subtransaction open and
-- PG_exception_stack pointing at a dead frame: the next BEGIN warned "there
-- is already a transaction in progress" and ROLLBACK took the backend down.
--
-- an unlabeled EXIT in a try body targets the enclosing loop; the insert
-- must survive the crossed frame's subtransaction commit
create function exit_loop_from_try() returns int language uplpgsql as $$
declare r int := 0;
begin
for i in 1..5 loop
begin
insert into exitblk_t values (i);
r := r + 1;
exit;
exception when others then r := 99;
end;
end loop;
return r + (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_loop_from_try();
-- conditional EXIT crossing a frame unwinds only when the branch is taken
create function exit_when_cross_exc() returns int language uplpgsql as $$
declare r int := 0;
begin
for i in 1..5 loop
begin
insert into exitblk_t values (i);
exit when i = 3;
r := r + 1;
exception when others then r := 99;
end;
end loop;
return r * 10 + (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_when_cross_exc();
-- EXIT of an outer block label from inside a nested exception block: the
-- inner frame's subtransaction commits, the rest of the outer body is skipped
create function exit_outer_cross_exc() returns int language uplpgsql as $$
declare r int := 0;
begin
<<b>> begin
begin
insert into exitblk_t values (1);
exit b;
exception when others then r := 99;
end;
r := 50;
end;
return r + (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_outer_cross_exc();
-- CONTINUE from inside a handler to the next iteration of the outer FOR,
-- several times over; a trapped error afterwards proves the exception
-- machinery is still intact
create function continue_from_handler(n int) returns text language uplpgsql as $$
declare r int := 0; caught int := 0;
begin
for i in 1..n loop
begin
if i % 2 = 1 then
raise exception 'odd %', i;
end if;
r := r + i;
exception when others then
caught := caught + 1;
continue;
end;
r := r + 100;
end loop;
begin
perform 1 / 0;
exception when division_by_zero then r := r + 1000;
end;
return r::text || '/' || caught::text;
end; $$;
select continue_from_handler(6);
-- EXIT crossing TWO nested exception frames releases both, innermost first:
-- both inserts commit
create function exit_cross_two_frames() returns int language uplpgsql as $$
declare r int := 0;
begin
<<o>> begin
begin
insert into exitblk_t values (1);
begin
insert into exitblk_t values (2);
exit o;
exception when others then r := 90;
end;
r := 91;
exception when others then r := 92;
end;
r := 93;
end;
return r + (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_cross_two_frames();
-- EXIT from a handler that also crosses an enclosing try body: handler
-- cleanup for the inner frame, subtransaction commit for the outer one
create function exit_handler_cross_try() returns int language uplpgsql as $$
declare r int := 0;
begin
<<o>> begin
begin
insert into exitblk_t values (1);
begin
raise exception 'boom';
exception when others then
insert into exitblk_t values (2);
exit o;
end;
exception when others then r := 80;
end;
end;
return r + (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_handler_cross_try();
-- EXIT of an outer block label from inside a FOR-over-rows loop must close
-- the loop's portal on the way out; a pinned portal left behind makes the
-- surrounding transaction unable to commit ("cannot commit while a portal is
-- pinned"), so an unfixed build errors on this very statement
create function exit_portal_cross() returns int language uplpgsql as $$
declare r record; n int := 0;
begin
<<outer>> begin
for r in select g from generate_series(1, 5) g loop
n := n + r.g;
exit outer;
end loop;
n := -99;
end;
return n;
end; $$;
select exit_portal_cross();
-- CONTINUE of an outer loop from inside an inner FOR-over-rows closes the
-- inner portal each time it fires
create function continue_portal_cross() returns int language uplpgsql as $$
declare r record; n int := 0;
begin
<<top>> for i in 1..3 loop
for r in select g from generate_series(1, 5) g loop
n := n + 1;
continue top;
end loop;
n := n + 1000;
end loop;
return n;
end; $$;
select continue_portal_cross();
-- EXIT crossing both a FOR-over-rows portal and an enclosing exception frame:
-- the portal closes and the subtransaction commits, innermost first
create function exit_portal_and_frame() returns int language uplpgsql as $$
declare r record; n int := 0;
begin
<<outer>> begin
begin
for r in select g from generate_series(1, 5) g loop
insert into exitblk_t values (r.g);
exit outer;
end loop;
exception when others then n := 99;
end;
n := 50;
end;
return n + (select count(*)::int from exitblk_t);
end; $$;
truncate exitblk_t;
select exit_portal_and_frame();
-- EXIT crossing two nested FOR-over-rows loops closes both portals
create function exit_two_portals() returns int language uplpgsql as $$
declare a record; b record; n int := 0;
begin
<<outer>> begin
for a in select g from generate_series(1, 3) g loop
for b in select g from generate_series(1, 3) g loop
n := n + 1;
exit outer;
end loop;
n := n + 1000;
end loop;
n := n + 100000;
end;
return n;
end; $$;
select exit_two_portals();
-- a bound cursor FOR loop (FORC) holds a portal too; EXIT crossing it must
-- close the cursor
create function exit_forc_cross() returns int language uplpgsql as $$
declare
cur cursor for select g from generate_series(1, 5) g;
r record;
n int := 0;
begin
<<outer>> begin
for r in cur loop
n := n + r.g;
exit outer;
end loop;
n := -99;
end;
return n;
end; $$;
select exit_forc_cross();
-- FOR ... IN EXECUTE (dynamic query, DYNFORS) holds a portal; EXIT crossing
-- it closes it
create function exit_dynfors_cross() returns int language uplpgsql as $$
declare r record; n int := 0;
begin
<<outer>> begin
for r in execute 'select g from generate_series(1, 5) g' loop
n := n + r.g;
exit outer;
end loop;
n := -99;
end;
return n;
end; $$;
select exit_dynfors_cross();
-- a FOR-over-rows loop that exits normally still closes its portal (baseline)
create function fors_normal_exit() returns int language uplpgsql as $$
declare r record; n int := 0;
begin
for r in select g from generate_series(1, 4) g loop
n := n + r.g;
end loop;
return n;
end; $$;
select fors_normal_exit();
-- the session's transaction state is sane after all of the above: an
-- explicit transaction block opens and closes cleanly (a leaked
-- subtransaction would warn on BEGIN and abort on ROLLBACK), and no portal
-- was left pinned
begin;
select 1 as sanity;
select count(*)::int as open_portals from pg_cursors;
rollback;
-- and a trapped error still traps
create function exc_still_works_after_crossing() returns int language uplpgsql as $$
begin
begin
perform 1 / 0;
exception when division_by_zero then return 42;
end;
return 0;
end; $$;
select exc_still_works_after_crossing();
drop function exit_block_resumes();
drop function exit_block_nested_loop();
drop function exit_block_when();
drop function exit_unlabeled_in_block();
drop function exit_outer_from_inner();
drop function exit_inner_only();
drop function exit_block_exc_subxact();
drop function exit_handler_subxact();
drop function exit_from_handler_body();
drop function exc_still_works_after_exit();
drop function exit_loop_from_try();
drop function exit_when_cross_exc();
drop function exit_outer_cross_exc();
drop function continue_from_handler(int);
drop function exit_cross_two_frames();
drop function exit_handler_cross_try();
drop function exit_portal_cross();
drop function continue_portal_cross();
drop function exit_portal_and_frame();
drop function exit_two_portals();
drop function exit_forc_cross();
drop function exit_dynfors_cross();
drop function fors_normal_exit();
drop function exc_still_works_after_crossing();
drop table exitblk_t;
-- verbose end block and end loop
create function end_label1() returns void as $$
<<blbl>>
begin
<<flbl1>>
for i in 1 .. 10 loop
raise notice 'i = %', i;
exit flbl1;
end loop flbl1;
<<flbl2>>
for j in 1 .. 10 loop
raise notice 'j = %', j;
exit flbl2;
end loop;
end blbl;
$$ LANGUAGE uplpgsql;
select end_label1();
-- should fail: undefined end label
create function end_label2() returns void as $$
begin
for _i in 1 .. 10 loop
exit;
end loop flbl1;
end;
$$ LANGUAGE uplpgsql;
-- should fail: end label does not match start label
create function end_label3() returns void as $$
<<outer_label>>
begin
<<inner_label>>
for _i in 1 .. 10 loop
exit;
end loop outer_label;
end;
$$ LANGUAGE uplpgsql;
-- should fail: end label on a block without a start label
create function end_label4() returns void as $$
<<outer_label>>
begin
for _i in 1 .. 10 loop
exit;
end loop outer_label;
end;
$$ LANGUAGE uplpgsql;
-- unlabeled exit matches no blocks
do LANGUAGE uplpgsql $$
begin
for i in 1..10 loop
<<innerblock>>
begin
begin -- unlabeled block
exit;
raise notice 'should not get here';
end;
raise notice 'should not get here, either';
end;
raise notice 'nor here';
end loop;
raise notice 'should get here';
end$$;
-- check exit out of an unlabeled block to a labeled one
do LANGUAGE uplpgsql $$
<<outerblock>>
begin
<<innerblock>>
begin
<<moreinnerblock>>
begin
begin -- unlabeled block
exit innerblock;
raise notice 'should not get here';
end;
raise notice 'should not get here, either';
end;
raise notice 'nor here';
end;
raise notice 'should get here';
end$$;
-- check exit out of outermost block
do LANGUAGE uplpgsql $$
<<outerblock>>
begin
<<innerblock>>
begin
exit outerblock;
raise notice 'should not get here';
end;
raise notice 'should not get here, either';
end$$;
-- unlabeled exit does match a while loop
do LANGUAGE uplpgsql $$
begin
<<outermostwhile>>
while 1 > 0 loop
<<outerwhile>>
while 1 > 0 loop
<<innerwhile>>
while 1 > 0 loop
exit;
raise notice 'should not get here';
end loop;
raise notice 'should get here';
exit outermostwhile;
raise notice 'should not get here, either';
end loop;
raise notice 'nor here';
end loop;
raise notice 'should get here, too';
end$$;
-- check exit out of an unlabeled while to a labeled one
do LANGUAGE uplpgsql $$
begin
<<outerwhile>>
while 1 > 0 loop
while 1 > 0 loop
exit outerwhile;
raise notice 'should not get here';
end loop;
raise notice 'should not get here, either';
end loop;
raise notice 'should get here';
end$$;
-- continue to an outer while
do LANGUAGE uplpgsql $$
declare i int := 0;
begin
<<outermostwhile>>
while i < 2 loop
raise notice 'outermostwhile, i = %', i;
i := i + 1;
<<outerwhile>>
while 1 > 0 loop
<<innerwhile>>
while 1 > 0 loop
continue outermostwhile;
raise notice 'should not get here';
end loop;
raise notice 'should not get here, either';
end loop;
raise notice 'nor here';
end loop;
raise notice 'out of outermostwhile, i = %', i;
end$$;
-- return out of a while
create function return_from_while() returns int LANGUAGE uplpgsql as $$
declare i int := 0;
begin
while i < 10 loop
if i > 2 then
return i;
end if;
i := i + 1;
end loop;
return null;
end$$;
select return_from_while();
-- using list of scalars in fori and fore stmts
create function for_vect() returns void as $proc$
<<lbl>>declare a integer; b varchar; c varchar; r record;
begin
-- fori
for i in 1 .. 3 loop
raise notice '%', i;
end loop;
-- fore with record var
for r in select gs as aa, 'BB' as bb, 'CC' as cc from generate_series(1,4) gs loop
raise notice '% % %', r.aa, r.bb, r.cc;
end loop;
-- fore with single scalar
for a in select gs from generate_series(1,4) gs loop
raise notice '%', a;
end loop;
-- fore with multiple scalars
for a,b,c in select gs, 'BB','CC' from generate_series(1,4) gs loop
raise notice '% % %', a, b, c;
end loop;
-- using qualified names in fors, fore is enabled, disabled only for fori
for lbl.a, lbl.b, lbl.c in execute $$select gs, 'bb','cc' from generate_series(1,4) gs$$ loop
raise notice '% % %', a, b, c;
end loop;
end;
$proc$ LANGUAGE uplpgsql;
select for_vect();
-- CASE statement
create or replace function case_test(bigint) returns text as $$
declare a int = 10;
b int = 1;
begin
case $1
when 1 then
return 'one';
when 2 then
return 'two';
when 3,4,3+5 then
return 'three, four or eight';
when a then
return 'ten';
when a+b, a+b+1 then
return 'eleven, twelve';
end case;
end;
$$ LANGUAGE uplpgsql immutable;
select case_test(1);
select case_test(2);
select case_test(3);
select case_test(4);
select case_test(5); -- fails
select case_test(8);
select case_test(10);
select case_test(11);
select case_test(12);
select case_test(13); -- fails
create or replace function catch() returns void as $$
begin
raise notice '%', case_test(6);
exception
when case_not_found then
raise notice 'caught case_not_found % %', SQLSTATE, SQLERRM;
end
$$ LANGUAGE uplpgsql;
select catch();
-- test the searched variant too, as well as ELSE
create or replace function case_test(bigint) returns text as $$
declare a int = 10;
begin
case
when $1 = 1 then
return 'one';
when $1 = a + 2 then
return 'twelve';
else
return 'other';
end case;
end;
$$ LANGUAGE uplpgsql immutable;
select case_test(1);
select case_test(2);
select case_test(12);
select case_test(13);
-- test line comment between WHEN and THEN
create or replace function case_comment(int) returns text as $$
begin
case $1
when 1 -- comment before THEN
then return 'one';
else
return 'other';
end case;
end;
$$ LANGUAGE uplpgsql immutable;
select case_comment(1);
--
-- FOR ... IN <cursor> (cursor FOR loop).
--
-- The loop opens the cursor through uplpgsql_rt_open_forc_cursor(), which
-- wraps the Portal in the cursor context that the fetch and close helpers
-- expect. Calling exec_open_forc_cursor() directly yields a bare Portal
-- and the fetch reads it as a context.
--
create or replace function forc_basic() returns int language uplpgsql as $$
declare t int := 0; rec record;
c cursor for select g as v from generate_series(1,3) g;
begin
for rec in c loop t := t + rec.v; end loop;
return t;
end; $$;
select forc_basic();
-- parameterised cursor
create or replace function forc_args() returns int language uplpgsql as $$
declare t int := 0; rec record;
c cursor(n int) for select g as v from generate_series(1,n) g;
begin
for rec in c(4) loop t := t + rec.v; end loop;
return t;
end; $$;
select forc_args();
-- RETURN out of the loop body: closes the cursor on the return path,
-- which is a separate close site from normal loop exit
create or replace function forc_return() returns int language uplpgsql as $$
declare rec record;
c cursor for select g as v from generate_series(5,9) g;
begin
for rec in c loop return rec.v; end loop;
return -1;
end; $$;
select forc_return();
-- empty result, and FOUND afterwards
create or replace function forc_empty() returns int language uplpgsql as $$
declare t int := 0; rec record;
c cursor for select g as v from generate_series(1,0) g;
begin
for rec in c loop t := t + rec.v; end loop;
if found then return -1; end if;
return t;
end; $$;
select forc_empty();
drop function forc_basic();
drop function forc_args();
drop function forc_return();
drop function forc_empty();
--
-- Simple CASE over types other than integer.
--