Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
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
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
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
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
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
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
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
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
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
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
/*-------------------------------------------------------------------------
 *
 * pl_exec.c        - Executor for the PL/pgSQL
 *            procedural language
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    src/pl/plpgsql/src/pl_exec.c
 *
 *-------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include <ctype.h>
 
#include "access/detoast.h"
#include "access/htup_details.h"
#include "access/tupconvert.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/execExpr.h"
#include "executor/spi.h"
#include "executor/tstoreReceiver.h"
#include "funcapi.h"
#include "mb/stringinfo_mb.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/optimizer.h"
#include "parser/parse_coerce.h"
#include "parser/parse_type.h"
#include "plpgsql.h"
#include "storage/proc.h"
#include "tcop/cmdtag.h"
#include "tcop/pquery.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
 
/*
 * All plpgsql function executions within a single transaction share the same
 * executor EState for evaluating "simple" expressions.  Each function call
 * creates its own "eval_econtext" ExprContext within this estate for
 * per-evaluation workspace.  eval_econtext is freed at normal function exit,
 * and the EState is freed at transaction end (in case of error, we assume
 * that the abort mechanisms clean it all up).  Furthermore, any exception
 * block within a function has to have its own eval_econtext separate from
 * the containing function's, so that we can clean up ExprContext callbacks
 * properly at subtransaction exit.  We maintain a stack that tracks the
 * individual econtexts so that we can clean up correctly at subxact exit.
 *
 * This arrangement is a bit tedious to maintain, but it's worth the trouble
 * so that we don't have to re-prepare simple expressions on each trip through
 * a function.  (We assume the case to optimize is many repetitions of a
 * function within a transaction.)
 *
 * However, there's no value in trying to amortize simple expression setup
 * across multiple executions of a DO block (inline code block), since there
 * can never be any.  If we use the shared EState for a DO block, the expr
 * state trees are effectively leaked till end of transaction, and that can
 * add up if the user keeps on submitting DO blocks.  Therefore, each DO block
 * has its own simple-expression EState, which is cleaned up at exit from
 * plpgsql_inline_handler().  DO blocks still use the simple_econtext_stack,
 * though, so that subxact abort cleanup does the right thing.
 *
 * (However, if a DO block executes COMMIT or ROLLBACK, then exec_stmt_commit
 * or exec_stmt_rollback will unlink it from the DO's simple-expression EState
 * and create a new shared EState that will be used thenceforth.  The original
 * EState will be cleaned up when we get back to plpgsql_inline_handler.  This
 * is a bit ugly, but it isn't worth doing better, since scenarios like this
 * can't result in indefinite accumulation of state trees.)
 */
typedef struct SimpleEcontextStackEntry
{
    ExprContext *stack_econtext;    /* a stacked econtext */
    SubTransactionId xact_subxid;   /* ID for current subxact */
    struct SimpleEcontextStackEntry *next;  /* next stack entry up */
} SimpleEcontextStackEntry;
 
static EState *shared_simple_eval_estate = NULL;
static SimpleEcontextStackEntry *simple_econtext_stack = NULL;
 
/*
 * In addition to the shared simple-eval EState, we have a shared resource
 * owner that holds refcounts on the CachedPlans for any "simple" expressions
 * we have evaluated in the current transaction.  This allows us to avoid
 * continually grabbing and releasing a plan refcount when a simple expression
 * is used over and over.  (DO blocks use their own resowner, in exactly the
 * same way described above for shared_simple_eval_estate.)
 */
static ResourceOwner shared_simple_eval_resowner = NULL;
 
/*
 * Memory management within a plpgsql function generally works with three
 * contexts:
 *
 * 1. Function-call-lifespan data, such as variable values, is kept in the
 * "main" context, a/k/a the "SPI Proc" context established by SPI_connect().
 * This is usually the CurrentMemoryContext while running code in this module
 * (which is not good, because careless coding can easily cause
 * function-lifespan memory leaks, but we live with it for now).
 *
 * 2. Some statement-execution routines need statement-lifespan workspace.
 * A suitable context is created on-demand by get_stmt_mcontext(), and must
 * be reset at the end of the requesting routine.  Error recovery will clean
 * it up automatically.  Nested statements requiring statement-lifespan
 * workspace will result in a stack of such contexts, see push_stmt_mcontext().
 *
 * 3. We use the eval_econtext's per-tuple memory context for expression
 * evaluation, and as a general-purpose workspace for short-lived allocations.
 * Such allocations usually aren't explicitly freed, but are left to be
 * cleaned up by a context reset, typically done by exec_eval_cleanup().
 *
 * These macros are for use in making short-lived allocations:
 */
#define get_eval_mcontext(estate) \
    ((estate)->eval_econtext->ecxt_per_tuple_memory)
#define eval_mcontext_alloc(estate, sz) \
    MemoryContextAlloc(get_eval_mcontext(estate), sz)
#define eval_mcontext_alloc0(estate, sz) \
    MemoryContextAllocZero(get_eval_mcontext(estate), sz)
 
/*
 * We use two session-wide hash tables for caching cast information.
 *
 * cast_expr_hash entries (of type plpgsql_CastExprHashEntry) hold compiled
 * expression trees for casts.  These survive for the life of the session and
 * are shared across all PL/pgSQL functions and DO blocks.  At some point it
 * might be worth invalidating them after pg_cast changes, but for the moment
 * we don't bother.
 *
 * There is a separate hash table shared_cast_hash (with entries of type
 * plpgsql_CastHashEntry) containing evaluation state trees for these
 * expressions, which are managed in the same way as simple expressions
 * (i.e., we assume cast expressions are always simple).
 *
 * As with simple expressions, DO blocks don't use the shared_cast_hash table
 * but must have their own evaluation state trees.  This isn't ideal, but we
 * don't want to deal with multiple simple_eval_estates within a DO block.
 */
typedef struct                  /* lookup key for cast info */
{
    /* NB: we assume this struct contains no padding bytes */
    Oid         srctype;        /* source type for cast */
    Oid         dsttype;        /* destination type for cast */
    int32       srctypmod;      /* source typmod for cast */
    int32       dsttypmod;      /* destination typmod for cast */
} plpgsql_CastHashKey;
 
typedef struct                  /* cast_expr_hash table entry */
{
    plpgsql_CastHashKey key;    /* hash key --- MUST BE FIRST */
    Expr       *cast_expr;      /* cast expression, or NULL if no-op cast */
    CachedExpression *cast_cexpr;   /* cached expression backing the above */
} plpgsql_CastExprHashEntry;
 
typedef struct                  /* cast_hash table entry */
{
    plpgsql_CastHashKey key;    /* hash key --- MUST BE FIRST */
    plpgsql_CastExprHashEntry *cast_centry; /* link to matching expr entry */
    /* ExprState is valid only when cast_lxid matches current LXID */
    ExprState  *cast_exprstate; /* expression's eval tree */
    bool        cast_in_use;    /* true while we're executing eval tree */
    LocalTransactionId cast_lxid;
} plpgsql_CastHashEntry;
 
static HTAB *cast_expr_hash = NULL;
static HTAB *shared_cast_hash = NULL;
 
/*
 * LOOP_RC_PROCESSING encapsulates common logic for looping statements to
 * handle return/exit/continue result codes from the loop body statement(s).
 * It's meant to be used like this:
 *
 *      int rc = PLPGSQL_RC_OK;
 *      for (...)
 *      {
 *          ...
 *          rc = exec_stmts(estate, stmt->body);
 *          LOOP_RC_PROCESSING(stmt->label, break);
 *          ...
 *      }
 *      return rc;
 *
 * If execution of the loop should terminate, LOOP_RC_PROCESSING will execute
 * "exit_action" (typically a "break" or "goto"), after updating "rc" to the
 * value the current statement should return.  If execution should continue,
 * LOOP_RC_PROCESSING will do nothing except reset "rc" to PLPGSQL_RC_OK.
 *
 * estate and rc are implicit arguments to the macro.
 * estate->exitlabel is examined and possibly updated.
 */
#define LOOP_RC_PROCESSING(looplabel, exit_action) \
    if (rc == PLPGSQL_RC_RETURN) \
    { \
        /* RETURN, so propagate RC_RETURN out */ \
        exit_action; \
    } \
    else if (rc == PLPGSQL_RC_EXIT) \
    { \
        if (estate->exitlabel == NULL) \
        { \
            /* unlabeled EXIT terminates this loop */ \
            rc = PLPGSQL_RC_OK; \
            exit_action; \
        } \
        else if ((looplabel) != NULL && \
                 strcmp(looplabel, estate->exitlabel) == 0) \
        { \
            /* labeled EXIT matching this loop, so terminate loop */ \
            estate->exitlabel = NULL; \
            rc = PLPGSQL_RC_OK; \
            exit_action; \
        } \
        else \
        { \
            /* non-matching labeled EXIT, propagate RC_EXIT out */ \
            exit_action; \
        } \
    } \
    else if (rc == PLPGSQL_RC_CONTINUE) \
    { \
        if (estate->exitlabel == NULL) \
        { \
            /* unlabeled CONTINUE matches this loop, so continue in loop */ \
            rc = PLPGSQL_RC_OK; \
        } \
        else if ((looplabel) != NULL && \
                 strcmp(looplabel, estate->exitlabel) == 0) \
        { \
            /* labeled CONTINUE matching this loop, so continue in loop */ \
            estate->exitlabel = NULL; \
            rc = PLPGSQL_RC_OK; \
        } \
        else \
        { \
            /* non-matching labeled CONTINUE, propagate RC_CONTINUE out */ \
            exit_action; \
        } \
    } \
    else \
        Assert(rc == PLPGSQL_RC_OK)
 
/* State struct for count_param_references */
typedef struct count_param_references_context
{
    int         paramid;
    int         count;
    Param      *last_param;
} count_param_references_context;
 
 
/************************************************************
 * Local function forward declarations
 ************************************************************/
static void coerce_function_result_tuple(PLpgSQL_execstate *estate,
                                         TupleDesc tupdesc);
static void plpgsql_exec_error_callback(void *arg);
static void plpgsql_execsql_error_callback(void *arg);
static void copy_plpgsql_datums(PLpgSQL_execstate *estate,
                                PLpgSQL_function *func);
static void plpgsql_fulfill_promise(PLpgSQL_execstate *estate,
                                    PLpgSQL_var *var);
static MemoryContext get_stmt_mcontext(PLpgSQL_execstate *estate);
static void push_stmt_mcontext(PLpgSQL_execstate *estate);
static void pop_stmt_mcontext(PLpgSQL_execstate *estate);
 
static int  exec_toplevel_block(PLpgSQL_execstate *estate,
                                PLpgSQL_stmt_block *block);
static int  exec_stmt_block(PLpgSQL_execstate *estate,
                            PLpgSQL_stmt_block *block);
static int  exec_stmts(PLpgSQL_execstate *estate,
                       List *stmts);
static int  exec_stmt_assign(PLpgSQL_execstate *estate,
                             PLpgSQL_stmt_assign *stmt);
static int  exec_stmt_perform(PLpgSQL_execstate *estate,
                              PLpgSQL_stmt_perform *stmt);
static int  exec_stmt_call(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_call *stmt);
static int  exec_stmt_getdiag(PLpgSQL_execstate *estate,
                              PLpgSQL_stmt_getdiag *stmt);
static int  exec_stmt_if(PLpgSQL_execstate *estate,
                         PLpgSQL_stmt_if *stmt);
static int  exec_stmt_case(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_case *stmt);
static int  exec_stmt_loop(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_loop *stmt);
static int  exec_stmt_while(PLpgSQL_execstate *estate,
                            PLpgSQL_stmt_while *stmt);
static int  exec_stmt_fori(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_fori *stmt);
static int  exec_stmt_fors(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_fors *stmt);
static int  exec_stmt_forc(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_forc *stmt);
static int  exec_stmt_foreach_a(PLpgSQL_execstate *estate,
                                PLpgSQL_stmt_foreach_a *stmt);
static int  exec_stmt_open(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_open *stmt);
static int  exec_stmt_fetch(PLpgSQL_execstate *estate,
                            PLpgSQL_stmt_fetch *stmt);
static int  exec_stmt_close(PLpgSQL_execstate *estate,
                            PLpgSQL_stmt_close *stmt);
static int  exec_stmt_exit(PLpgSQL_execstate *estate,
                           PLpgSQL_stmt_exit *stmt);
static int  exec_stmt_return(PLpgSQL_execstate *estate,
                             PLpgSQL_stmt_return *stmt);
static int  exec_stmt_return_next(PLpgSQL_execstate *estate,
                                  PLpgSQL_stmt_return_next *stmt);
static int  exec_stmt_return_query(PLpgSQL_execstate *estate,
                                   PLpgSQL_stmt_return_query *stmt);
static int  exec_stmt_raise(PLpgSQL_execstate *estate,
                            PLpgSQL_stmt_raise *stmt);
static int  exec_stmt_assert(PLpgSQL_execstate *estate,
                             PLpgSQL_stmt_assert *stmt);
static int  exec_stmt_execsql(PLpgSQL_execstate *estate,
                              PLpgSQL_stmt_execsql *stmt);
static int  exec_stmt_dynexecute(PLpgSQL_execstate *estate,
                                 PLpgSQL_stmt_dynexecute *stmt);
static int  exec_stmt_dynfors(PLpgSQL_execstate *estate,
                              PLpgSQL_stmt_dynfors *stmt);
static int  exec_stmt_commit(PLpgSQL_execstate *estate,
                             PLpgSQL_stmt_commit *stmt);
static int  exec_stmt_rollback(PLpgSQL_execstate *estate,
                               PLpgSQL_stmt_rollback *stmt);
 
static void plpgsql_estate_setup(PLpgSQL_execstate *estate,
                                 PLpgSQL_function *func,
                                 ReturnSetInfo *rsi,
                                 EState *simple_eval_estate,
                                 ResourceOwner simple_eval_resowner);
static void exec_eval_cleanup(PLpgSQL_execstate *estate);
 
static void exec_prepare_plan(PLpgSQL_execstate *estate,
                              PLpgSQL_expr *expr, int cursorOptions);
static void exec_simple_check_plan(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
static bool exec_is_simple_query(PLpgSQL_expr *expr);
static void exec_save_simple_expr(PLpgSQL_expr *expr, CachedPlan *cplan);
static void exec_check_rw_parameter(PLpgSQL_expr *expr, int paramid);
static bool count_param_references(Node *node,
                                   count_param_references_context *context);
static void exec_check_assignable(PLpgSQL_execstate *estate, int dno);
static bool exec_eval_simple_expr(PLpgSQL_execstate *estate,
                                  PLpgSQL_expr *expr,
                                  Datum *result,
                                  bool *isNull,
                                  Oid *rettype,
                                  int32 *rettypmod);
 
static void exec_assign_expr(PLpgSQL_execstate *estate,
                             PLpgSQL_datum *target,
                             PLpgSQL_expr *expr);
static void exec_assign_c_string(PLpgSQL_execstate *estate,
                                 PLpgSQL_datum *target,
                                 const char *str);
static void exec_assign_value(PLpgSQL_execstate *estate,
                              PLpgSQL_datum *target,
                              Datum value, bool isNull,
                              Oid valtype, int32 valtypmod);
static void exec_eval_datum(PLpgSQL_execstate *estate,
                            PLpgSQL_datum *datum,
                            Oid *typeid,
                            int32 *typetypmod,
                            Datum *value,
                            bool *isnull);
static int  exec_eval_integer(PLpgSQL_execstate *estate,
                              PLpgSQL_expr *expr,
                              bool *isNull);
static bool exec_eval_boolean(PLpgSQL_execstate *estate,
                              PLpgSQL_expr *expr,
                              bool *isNull);
static Datum exec_eval_expr(PLpgSQL_execstate *estate,
                            PLpgSQL_expr *expr,
                            bool *isNull,
                            Oid *rettype,
                            int32 *rettypmod);
static int  exec_run_select(PLpgSQL_execstate *estate,
                            PLpgSQL_expr *expr, long maxtuples, Portal *portalP);
static int  exec_for_query(PLpgSQL_execstate *estate, PLpgSQL_stmt_forq *stmt,
                           Portal portal, bool prefetch_ok);
static ParamListInfo setup_param_list(PLpgSQL_execstate *estate,
                                      PLpgSQL_expr *expr);
static ParamExternData *plpgsql_param_fetch(ParamListInfo params,
                                            int paramid, bool speculative,
                                            ParamExternData *prm);
static void plpgsql_param_compile(ParamListInfo params, Param *param,
                                  ExprState *state,
                                  Datum *resv, bool *resnull);
static void plpgsql_param_eval_var_check(ExprState *state, ExprEvalStep *op,
                                         ExprContext *econtext);
static void plpgsql_param_eval_var_transfer(ExprState *state, ExprEvalStep *op,
                                            ExprContext *econtext);
static void plpgsql_param_eval_var(ExprState *state, ExprEvalStep *op,
                                   ExprContext *econtext);
static void plpgsql_param_eval_var_ro(ExprState *state, ExprEvalStep *op,
                                      ExprContext *econtext);
static void plpgsql_param_eval_recfield(ExprState *state, ExprEvalStep *op,
                                        ExprContext *econtext);
static void plpgsql_param_eval_generic(ExprState *state, ExprEvalStep *op,
                                       ExprContext *econtext);
static void plpgsql_param_eval_generic_ro(ExprState *state, ExprEvalStep *op,
                                          ExprContext *econtext);
static void exec_move_row(PLpgSQL_execstate *estate,
                          PLpgSQL_variable *target,
                          HeapTuple tup, TupleDesc tupdesc);
static void revalidate_rectypeid(PLpgSQL_rec *rec);
static ExpandedRecordHeader *make_expanded_record_for_rec(PLpgSQL_execstate *estate,
                                                          PLpgSQL_rec *rec,
                                                          TupleDesc srctupdesc,
                                                          ExpandedRecordHeader *srcerh);
static void exec_move_row_from_fields(PLpgSQL_execstate *estate,
                                      PLpgSQL_variable *target,
                                      ExpandedRecordHeader *newerh,
                                      Datum *values, bool *nulls,
                                      TupleDesc tupdesc);
static bool compatible_tupdescs(TupleDesc src_tupdesc, TupleDesc dst_tupdesc);
static HeapTuple make_tuple_from_row(PLpgSQL_execstate *estate,
                                     PLpgSQL_row *row,
                                     TupleDesc tupdesc);
static void check_record_tupdescs_match(ExpandedRecordHeader *srcerh,
                                        ExpandedRecordHeader *dsterh);
static TupleDesc deconstruct_composite_datum(Datum value,
                                             HeapTupleData *tmptup);
static void exec_move_row_from_datum(PLpgSQL_execstate *estate,
                                     PLpgSQL_variable *target,
                                     Datum value);
static void instantiate_empty_record_variable(PLpgSQL_execstate *estate,
                                              PLpgSQL_rec *rec);
static char *convert_value_to_string(PLpgSQL_execstate *estate,
                                     Datum value, Oid valtype);
static inline Datum exec_cast_value(PLpgSQL_execstate *estate,
                                    Datum value, bool *isnull,
                                    Oid valtype, int32 valtypmod,
                                    Oid reqtype, int32 reqtypmod);
static Datum do_cast_value(PLpgSQL_execstate *estate,
                           Datum value, bool *isnull,
                           Oid valtype, int32 valtypmod,
                           Oid reqtype, int32 reqtypmod);
static plpgsql_CastHashEntry *get_cast_hashentry(PLpgSQL_execstate *estate,
                                                 Oid srctype, int32 srctypmod,
                                                 Oid dsttype, int32 dsttypmod);
static void exec_init_tuple_store(PLpgSQL_execstate *estate);
static void exec_set_found(PLpgSQL_execstate *estate, bool state);
static void plpgsql_create_econtext(PLpgSQL_execstate *estate);
static void plpgsql_destroy_econtext(PLpgSQL_execstate *estate);
static void assign_simple_var(PLpgSQL_execstate *estate, PLpgSQL_var *var,
                              Datum newvalue, bool isnull, bool freeable);
static void assign_text_var(PLpgSQL_execstate *estate, PLpgSQL_var *var,
                            const char *str);
static void assign_record_var(PLpgSQL_execstate *estate, PLpgSQL_rec *rec,
                              ExpandedRecordHeader *erh);
static ParamListInfo exec_eval_using_params(PLpgSQL_execstate *estate,
                                            List *params);
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
                                        PLpgSQL_expr *dynquery, List *params,
                                        const char *portalname, int cursorOptions);
static char *format_expr_params(PLpgSQL_execstate *estate,
                                const PLpgSQL_expr *expr);
static char *format_preparedparamsdata(PLpgSQL_execstate *estate,
                                       ParamListInfo paramLI);
static PLpgSQL_variable *make_callstmt_target(PLpgSQL_execstate *estate,
                                              PLpgSQL_expr *expr);
 
 
/* ----------
 * plpgsql_exec_function    Called by the call handler for
 *              function execution.
 *
 * This is also used to execute inline code blocks (DO blocks).  The only
 * difference that this code is aware of is that for a DO block, we want
 * to use a private simple_eval_estate and a private simple_eval_resowner,
 * which are created and passed in by the caller.  For regular functions,
 * pass NULL, which implies using shared_simple_eval_estate and
 * shared_simple_eval_resowner.  (When using a private simple_eval_estate,
 * we must also use a private cast hashtable, but that's taken care of
 * within plpgsql_estate_setup.)
 * procedure_resowner is a resowner that will survive for the duration
 * of execution of this function/procedure.  It is needed only if we
 * are doing non-atomic execution and there are CALL or DO statements
 * in the function; otherwise it can be NULL.  We use it to hold refcounts
 * on the CALL/DO statements' plans.
 * ----------
 */
Datum
plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo,
                      EState *simple_eval_estate,
                      ResourceOwner simple_eval_resowner,
                      ResourceOwner procedure_resowner,
                      bool atomic)
{
    PLpgSQL_execstate estate;
    ErrorContextCallback plerrcontext;
    int         i;
    int         rc;
 
    /*
     * Setup the execution state
     */
    plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo,
                         simple_eval_estate, simple_eval_resowner);
    estate.procedure_resowner = procedure_resowner;
    estate.atomic = atomic;
 
    /*
     * Setup error traceback support for ereport()
     */
    plerrcontext.callback = plpgsql_exec_error_callback;
    plerrcontext.arg = &estate;
    plerrcontext.previous = error_context_stack;
    error_context_stack = &plerrcontext;
 
    /*
     * Make local execution copies of all the datums
     */
    estate.err_text = gettext_noop("during initialization of execution state");
    copy_plpgsql_datums(&estate, func);
 
    /*
     * Store the actual call argument values into the appropriate variables
     */
    estate.err_text = gettext_noop("while storing call arguments into local variables");
    for (i = 0; i < func->fn_nargs; i++)
    {
        int         n = func->fn_argvarnos[i];
 
        switch (estate.datums[n]->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];
 
                    assign_simple_var(&estate, var,
                                      fcinfo->args[i].value,
                                      fcinfo->args[i].isnull,
                                      false);
 
                    /*
                     * If it's a varlena type, check to see if we received a
                     * R/W expanded-object pointer.  If so, we can commandeer
                     * the object rather than having to copy it.  If passed a
                     * R/O expanded pointer, just keep it as the value of the
                     * variable for the moment.  (We can change it to R/W if
                     * the variable gets modified, but that may very well
                     * never happen.)
                     *
                     * Also, force any flat array value to be stored in
                     * expanded form in our local variable, in hopes of
                     * improving efficiency of uses of the variable.  (This is
                     * a hack, really: why only arrays? Need more thought
                     * about which cases are likely to win.  See also
                     * typisarray-specific heuristic in exec_assign_value.)
                     */
                    if (!var->isnull && var->datatype->typlen == -1)
                    {
                        if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(var->value)))
                        {
                            /* take ownership of R/W object */
                            assign_simple_var(&estate, var,
                                              TransferExpandedObject(var->value,
                                                                     estate.datum_context),
                                              false,
                                              true);
                        }
                        else if (VARATT_IS_EXTERNAL_EXPANDED_RO(DatumGetPointer(var->value)))
                        {
                            /* R/O pointer, keep it as-is until assigned to */
                        }
                        else if (var->datatype->typisarray)
                        {
                            /* flat array, so force to expanded form */
                            assign_simple_var(&estate, var,
                                              expand_array(var->value,
                                                           estate.datum_context,
                                                           NULL),
                                              false,
                                              true);
                        }
                    }
                }
                break;
 
            case PLPGSQL_DTYPE_REC:
                {
                    PLpgSQL_rec *rec = (PLpgSQL_rec *) estate.datums[n];
 
                    if (!fcinfo->args[i].isnull)
                    {
                        /* Assign row value from composite datum */
                        exec_move_row_from_datum(&estate,
                                                 (PLpgSQL_variable *) rec,
                                                 fcinfo->args[i].value);
                    }
                    else
                    {
                        /* If arg is null, set variable to null */
                        exec_move_row(&estate, (PLpgSQL_variable *) rec,
                                      NULL, NULL);
                    }
                    /* clean up after exec_move_row() */
                    exec_eval_cleanup(&estate);
                }
                break;
 
            default:
                /* Anything else should not be an argument variable */
                elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
        }
    }
 
    estate.err_text = gettext_noop("during function entry");
 
    /*
     * Set the magic variable FOUND to false
     */
    exec_set_found(&estate, false);
 
    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
        ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
 
    /*
     * Now call the toplevel block of statements
     */
    estate.err_text = NULL;
    rc = exec_toplevel_block(&estate, func->action);
    if (rc != PLPGSQL_RC_RETURN)
    {
        estate.err_text = NULL;
        ereport(ERROR,
                (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
                 errmsg("control reached end of function without RETURN")));
    }
 
    /*
     * We got a return value - process it
     */
    estate.err_text = gettext_noop("while casting return value to function's return type");
 
    fcinfo->isnull = estate.retisnull;
 
    if (estate.retisset)
    {
        ReturnSetInfo *rsi = estate.rsi;
 
        /* Check caller can handle a set result */
        if (!rsi || !IsA(rsi, ReturnSetInfo))
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("set-valued function called in context that cannot accept a set")));
 
        if (!(rsi->allowedModes & SFRM_Materialize))
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("materialize mode required, but it is not allowed in this context")));
 
        rsi->returnMode = SFRM_Materialize;
 
        /* If we produced any tuples, send back the result */
        if (estate.tuple_store)
        {
            MemoryContext oldcxt;
 
            rsi->setResult = estate.tuple_store;
            oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
            rsi->setDesc = CreateTupleDescCopy(estate.tuple_store_desc);
            MemoryContextSwitchTo(oldcxt);
        }
        estate.retval = (Datum) 0;
        fcinfo->isnull = true;
    }
    else if (!estate.retisnull)
    {
        /*
         * Cast result value to function's declared result type, and copy it
         * out to the upper executor memory context.  We must treat tuple
         * results specially in order to deal with cases like rowtypes
         * involving dropped columns.
         */
        if (estate.retistuple)
        {
            /* Don't need coercion if rowtype is known to match */
            if (func->fn_rettype == estate.rettype &&
                func->fn_rettype != RECORDOID)
            {
                /*
                 * Copy the tuple result into upper executor memory context.
                 * However, if we have a R/W expanded datum, we can just
                 * transfer its ownership out to the upper context.
                 */
                estate.retval = SPI_datumTransfer(estate.retval,
                                                  false,
                                                  -1);
            }
            else
            {
                /*
                 * Need to look up the expected result type.  XXX would be
                 * better to cache the tupdesc instead of repeating
                 * get_call_result_type(), but the only easy place to save it
                 * is in the PLpgSQL_function struct, and that's too
                 * long-lived: composite types could change during the
                 * existence of a PLpgSQL_function.
                 */
                Oid         resultTypeId;
                TupleDesc   tupdesc;
 
                switch (get_call_result_type(fcinfo, &resultTypeId, &tupdesc))
                {
                    case TYPEFUNC_COMPOSITE:
                        /* got the expected result rowtype, now coerce it */
                        coerce_function_result_tuple(&estate, tupdesc);
                        break;
                    case TYPEFUNC_COMPOSITE_DOMAIN:
                        /* got the expected result rowtype, now coerce it */
                        coerce_function_result_tuple(&estate, tupdesc);
                        /* and check domain constraints */
                        /* XXX allowing caching here would be good, too */
                        domain_check(estate.retval, false, resultTypeId,
                                     NULL, NULL);
                        break;
                    case TYPEFUNC_RECORD:
 
                        /*
                         * Failed to determine actual type of RECORD.  We
                         * could raise an error here, but what this means in
                         * practice is that the caller is expecting any old
                         * generic rowtype, so we don't really need to be
                         * restrictive.  Pass back the generated result as-is.
                         */
                        estate.retval = SPI_datumTransfer(estate.retval,
                                                          false,
                                                          -1);
                        break;
                    default:
                        /* shouldn't get here if retistuple is true ... */
                        elog(ERROR, "return type must be a row type");
                        break;
                }
            }
        }
        else
        {
            /* Scalar case: use exec_cast_value */
            estate.retval = exec_cast_value(&estate,
                                            estate.retval,
                                            &fcinfo->isnull,
                                            estate.rettype,
                                            -1,
                                            func->fn_rettype,
                                            -1);
 
            /*
             * If the function's return type isn't by value, copy the value
             * into upper executor memory context.  However, if we have a R/W
             * expanded datum, we can just transfer its ownership out to the
             * upper executor context.
             */
            if (!fcinfo->isnull && !func->fn_retbyval)
                estate.retval = SPI_datumTransfer(estate.retval,
                                                  false,
                                                  func->fn_rettyplen);
        }
    }
    else
    {
        /*
         * We're returning a NULL, which normally requires no conversion work
         * regardless of datatypes.  But, if we are casting it to a domain
         * return type, we'd better check that the domain's constraints pass.
         */
        if (func->fn_retisdomain)
            estate.retval = exec_cast_value(&estate,
                                            estate.retval,
                                            &fcinfo->isnull,
                                            estate.rettype,
                                            -1,
                                            func->fn_rettype,
                                            -1);
    }
 
    estate.err_text = gettext_noop("during function exit");
 
    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
        ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
 
    /* Clean up any leftover temporary memory */
    plpgsql_destroy_econtext(&estate);
    exec_eval_cleanup(&estate);
    /* stmt_mcontext will be destroyed when function's main context is */
 
    /*
     * Pop the error context stack
     */
    error_context_stack = plerrcontext.previous;
 
    /*
     * Return the function's result
     */
    return estate.retval;
}
 
/*
 * Helper for plpgsql_exec_function: coerce composite result to the specified
 * tuple descriptor, and copy it out to upper executor memory.  This is split
 * out mostly for cosmetic reasons --- the logic would be very deeply nested
 * otherwise.
 *
 * estate->retval is updated in-place.
 */
static void
coerce_function_result_tuple(PLpgSQL_execstate *estate, TupleDesc tupdesc)
{
    HeapTuple   rettup;
    TupleDesc   retdesc;
    TupleConversionMap *tupmap;
 
    /* We assume exec_stmt_return verified that result is composite */
    Assert(type_is_rowtype(estate->rettype));
 
    /* We can special-case expanded records for speed */
    if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(estate->retval)))
    {
        ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(estate->retval);
 
        Assert(erh->er_magic == ER_MAGIC);
 
        /* Extract record's TupleDesc */
        retdesc = expanded_record_get_tupdesc(erh);
 
        /* check rowtype compatibility */
        tupmap = convert_tuples_by_position(retdesc,
                                            tupdesc,
                                            gettext_noop("returned record type does not match expected record type"));
 
        /* it might need conversion */
        if (tupmap)
        {
            rettup = expanded_record_get_tuple(erh);
            Assert(rettup);
            rettup = execute_attr_map_tuple(rettup, tupmap);
 
            /*
             * Copy tuple to upper executor memory, as a tuple Datum.  Make
             * sure it is labeled with the caller-supplied tuple type.
             */
            estate->retval = PointerGetDatum(SPI_returntuple(rettup, tupdesc));
            /* no need to free map, we're about to return anyway */
        }
        else if (!(tupdesc->tdtypeid == erh->er_decltypeid ||
                   (tupdesc->tdtypeid == RECORDOID &&
                    !ExpandedRecordIsDomain(erh))))
        {
            /*
             * The expanded record has the right physical tupdesc, but the
             * wrong type ID.  (Typically, the expanded record is RECORDOID
             * but the function is declared to return a named composite type.
             * As in exec_move_row_from_datum, we don't allow returning a
             * composite-domain record from a function declared to return
             * RECORD.)  So we must flatten the record to a tuple datum and
             * overwrite its type fields with the right thing.  spi.c doesn't
             * provide any easy way to deal with this case, so we end up
             * duplicating the guts of datumCopy() :-(
             */
            Size        resultsize;
            HeapTupleHeader tuphdr;
 
            resultsize = EOH_get_flat_size(&erh->hdr);
            tuphdr = (HeapTupleHeader) SPI_palloc(resultsize);
            EOH_flatten_into(&erh->hdr, tuphdr, resultsize);
            heap_datum_update_type(tuphdr, tupdesc);
            estate->retval = PointerGetDatum(tuphdr);
        }
        else
        {
            /*
             * We need only copy result into upper executor memory context.
             * However, if we have a R/W expanded datum, we can just transfer
             * its ownership out to the upper executor context.
             */
            estate->retval = SPI_datumTransfer(estate->retval,
                                               false,
                                               -1);
        }
    }
    else
    {
        /* Convert composite datum to a HeapTuple and TupleDesc */
        HeapTupleData tmptup;
 
        retdesc = deconstruct_composite_datum(estate->retval, &tmptup);
        rettup = &tmptup;
 
        /* check rowtype compatibility */
        tupmap = convert_tuples_by_position(retdesc,
                                            tupdesc,
                                            gettext_noop("returned record type does not match expected record type"));
 
        /* it might need conversion */
        if (tupmap)
            rettup = execute_attr_map_tuple(rettup, tupmap);
 
        /*
         * Copy tuple to upper executor memory, as a tuple Datum.  Make sure
         * it is labeled with the caller-supplied tuple type.
         */
        estate->retval = PointerGetDatum(SPI_returntuple(rettup, tupdesc));
 
        /* no need to free map, we're about to return anyway */
 
        ReleaseTupleDesc(retdesc);
    }
}
 
 
/* ----------
 * plpgsql_exec_trigger     Called by the call handler for
 *              trigger execution.
 * ----------
 */
HeapTuple
plpgsql_exec_trigger(PLpgSQL_function *func,
                     TriggerData *trigdata)
{
    PLpgSQL_execstate estate;
    ErrorContextCallback plerrcontext;
    int         rc;
    TupleDesc   tupdesc;
    PLpgSQL_rec *rec_new,
               *rec_old;
    HeapTuple   rettup;
 
    /*
     * Setup the execution state
     */
    plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
    estate.trigdata = trigdata;
 
    /*
     * Setup error traceback support for ereport()
     */
    plerrcontext.callback = plpgsql_exec_error_callback;
    plerrcontext.arg = &estate;
    plerrcontext.previous = error_context_stack;
    error_context_stack = &plerrcontext;
 
    /*
     * Make local execution copies of all the datums
     */
    estate.err_text = gettext_noop("during initialization of execution state");
    copy_plpgsql_datums(&estate, func);
 
    /*
     * Put the OLD and NEW tuples into record variables
     *
     * We set up expanded records for both variables even though only one may
     * have a value.  This allows record references to succeed in functions
     * that are used for multiple trigger types.  For example, we might have a
     * test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')", which should
     * work regardless of the current trigger type.  If a value is actually
     * fetched from an unsupplied tuple, it will read as NULL.
     */
    tupdesc = RelationGetDescr(trigdata->tg_relation);
 
    rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
    rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
 
    rec_new->erh = make_expanded_record_from_tupdesc(tupdesc,
                                                     estate.datum_context);
    rec_old->erh = make_expanded_record_from_exprecord(rec_new->erh,
                                                       estate.datum_context);
 
    if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
    {
        /*
         * Per-statement triggers don't use OLD/NEW variables
         */
    }
    else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
    {
        expanded_record_set_tuple(rec_new->erh, trigdata->tg_trigtuple,
                                  false, false);
    }
    else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
    {
        expanded_record_set_tuple(rec_new->erh, trigdata->tg_newtuple,
                                  false, false);
        expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
                                  false, false);
 
        /*
         * In BEFORE trigger, stored generated columns are not computed yet,
         * so make them null in the NEW row.  (Only needed in UPDATE branch;
         * in the INSERT case, they are already null, but in UPDATE, the field
         * still contains the old value.)  Alternatively, we could construct a
         * whole new row structure without the generated columns, but this way
         * seems more efficient and potentially less confusing.
         */
        if (tupdesc->constr && tupdesc->constr->has_generated_stored &&
            TRIGGER_FIRED_BEFORE(trigdata->tg_event))
        {
            for (int i = 0; i < tupdesc->natts; i++)
                if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
                    expanded_record_set_field_internal(rec_new->erh,
                                                       i + 1,
                                                       (Datum) 0,
                                                       true,    /* isnull */
                                                       false, false);
        }
    }
    else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
    {
        expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
                                  false, false);
    }
    else
        elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
 
    /* Make transition tables visible to this SPI connection */
    rc = SPI_register_trigger_data(trigdata);
    Assert(rc >= 0);
 
    estate.err_text = gettext_noop("during function entry");
 
    /*
     * Set the magic variable FOUND to false
     */
    exec_set_found(&estate, false);
 
    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
        ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
 
    /*
     * Now call the toplevel block of statements
     */
    estate.err_text = NULL;
    rc = exec_toplevel_block(&estate, func->action);
    if (rc != PLPGSQL_RC_RETURN)
    {
        estate.err_text = NULL;
        ereport(ERROR,
                (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
                 errmsg("control reached end of trigger procedure without RETURN")));
    }
 
    estate.err_text = gettext_noop("during function exit");
 
    if (estate.retisset)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("trigger procedure cannot return a set")));
 
    /*
     * Check that the returned tuple structure has the same attributes, the
     * relation that fired the trigger has. A per-statement trigger always
     * needs to return NULL, so we ignore any return value the function itself
     * produces (XXX: is this a good idea?)
     *
     * XXX This way it is possible, that the trigger returns a tuple where
     * attributes don't have the correct atttypmod's length. It's up to the
     * trigger's programmer to ensure that this doesn't happen. Jan
     */
    if (estate.retisnull || !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
        rettup = NULL;
    else
    {
        TupleDesc   retdesc;
        TupleConversionMap *tupmap;
 
        /* We assume exec_stmt_return verified that result is composite */
        Assert(type_is_rowtype(estate.rettype));
 
        /* We can special-case expanded records for speed */
        if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(estate.retval)))
        {
            ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(estate.retval);
 
            Assert(erh->er_magic == ER_MAGIC);
 
            /* Extract HeapTuple and TupleDesc */
            rettup = expanded_record_get_tuple(erh);
            Assert(rettup);
            retdesc = expanded_record_get_tupdesc(erh);
 
            if (retdesc != RelationGetDescr(trigdata->tg_relation))
            {
                /* check rowtype compatibility */
                tupmap = convert_tuples_by_position(retdesc,
                                                    RelationGetDescr(trigdata->tg_relation),
                                                    gettext_noop("returned row structure does not match the structure of the triggering table"));
                /* it might need conversion */
                if (tupmap)
                    rettup = execute_attr_map_tuple(rettup, tupmap);
                /* no need to free map, we're about to return anyway */
            }
 
            /*
             * Copy tuple to upper executor memory.  But if user just did
             * "return new" or "return old" without changing anything, there's
             * no need to copy; we can return the original tuple (which will
             * save a few cycles in trigger.c as well as here).
             */
            if (rettup != trigdata->tg_newtuple &&
                rettup != trigdata->tg_trigtuple)
                rettup = SPI_copytuple(rettup);
        }
        else
        {
            /* Convert composite datum to a HeapTuple and TupleDesc */
            HeapTupleData tmptup;
 
            retdesc = deconstruct_composite_datum(estate.retval, &tmptup);
            rettup = &tmptup;
 
            /* check rowtype compatibility */
            tupmap = convert_tuples_by_position(retdesc,
                                                RelationGetDescr(trigdata->tg_relation),
                                                gettext_noop("returned row structure does not match the structure of the triggering table"));
            /* it might need conversion */
            if (tupmap)
                rettup = execute_attr_map_tuple(rettup, tupmap);
 
            ReleaseTupleDesc(retdesc);
            /* no need to free map, we're about to return anyway */
 
            /* Copy tuple to upper executor memory */
            rettup = SPI_copytuple(rettup);
        }
    }
 
    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
        ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
 
    /* Clean up any leftover temporary memory */
    plpgsql_destroy_econtext(&estate);
    exec_eval_cleanup(&estate);
    /* stmt_mcontext will be destroyed when function's main context is */
 
    /*
     * Pop the error context stack
     */
    error_context_stack = plerrcontext.previous;
 
    /*
     * Return the trigger's result
     */
    return rettup;
}
 
/* ----------
 * plpgsql_exec_event_trigger       Called by the call handler for
 *              event trigger execution.
 * ----------
 */
void
plpgsql_exec_event_trigger(PLpgSQL_function *func, EventTriggerData *trigdata)
{
    PLpgSQL_execstate estate;
    ErrorContextCallback plerrcontext;
    int         rc;
 
    /*
     * Setup the execution state
     */
    plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
    estate.evtrigdata = trigdata;
 
    /*
     * Setup error traceback support for ereport()
     */
    plerrcontext.callback = plpgsql_exec_error_callback;
    plerrcontext.arg = &estate;
    plerrcontext.previous = error_context_stack;
    error_context_stack = &plerrcontext;
 
    /*
     * Make local execution copies of all the datums
     */
    estate.err_text = gettext_noop("during initialization of execution state");
    copy_plpgsql_datums(&estate, func);
 
    estate.err_text = gettext_noop("during function entry");
 
    /*
     * Set the magic variable FOUND to false
     */
    exec_set_found(&estate, false);
 
    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
        ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
 
    /*
     * Now call the toplevel block of statements
     */
    estate.err_text = NULL;
    rc = exec_toplevel_block(&estate, func->action);
    if (rc != PLPGSQL_RC_RETURN)
    {
        estate.err_text = NULL;
        ereport(ERROR,
                (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
                 errmsg("control reached end of trigger procedure without RETURN")));
    }
 
    estate.err_text = gettext_noop("during function exit");
 
    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
        ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
 
    /* Clean up any leftover temporary memory */
    plpgsql_destroy_econtext(&estate);
    exec_eval_cleanup(&estate);
    /* stmt_mcontext will be destroyed when function's main context is */
 
    /*
     * Pop the error context stack
     */
    error_context_stack = plerrcontext.previous;
}
 
/*
 * error context callback to let us supply a call-stack traceback
 */
static void
plpgsql_exec_error_callback(void *arg)
{
    PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
    int         err_lineno;
 
    /*
     * If err_var is set, report the variable's declaration line number.
     * Otherwise, if err_stmt is set, report the err_stmt's line number.  When
     * err_stmt is not set, we're in function entry/exit, or some such place
     * not attached to a specific line number.
     */
    if (estate->err_var != NULL)
        err_lineno = estate->err_var->lineno;
    else if (estate->err_stmt != NULL)
        err_lineno = estate->err_stmt->lineno;
    else
        err_lineno = 0;
 
    if (estate->err_text != NULL)
    {
        /*
         * We don't expend the cycles to run gettext() on err_text unless we
         * actually need it.  Therefore, places that set up err_text should
         * use gettext_noop() to ensure the strings get recorded in the
         * message dictionary.
         */
        if (err_lineno > 0)
        {
            /*
             * translator: last %s is a phrase such as "during statement block
             * local variable initialization"
             */
            errcontext("PL/pgSQL function %s line %d %s",
                       estate->func->fn_signature,
                       err_lineno,
                       _(estate->err_text));
        }
        else
        {
            /*
             * translator: last %s is a phrase such as "while storing call
             * arguments into local variables"
             */
            errcontext("PL/pgSQL function %s %s",
                       estate->func->fn_signature,
                       _(estate->err_text));
        }
    }
    else if (estate->err_stmt != NULL && err_lineno > 0)
    {
        /* translator: last %s is a plpgsql statement type name */
        errcontext("PL/pgSQL function %s line %d at %s",
                   estate->func->fn_signature,
                   err_lineno,
                   plpgsql_stmt_typename(estate->err_stmt));
    }
    else
        errcontext("PL/pgSQL function %s",
                   estate->func->fn_signature);
}
 
/*
 * error context callback used for "SELECT simple-expr INTO var"
 *
 * This should match the behavior of spi.c's _SPI_error_callback(),
 * so that the construct still reports errors the same as it did
 * before we optimized it with the simple-expression code path.
 */
static void
plpgsql_execsql_error_callback(void *arg)
{
    PLpgSQL_expr *expr = (PLpgSQL_expr *) arg;
    const char *query = expr->query;
    int         syntaxerrposition;
 
    /*
     * If there is a syntax error position, convert to internal syntax error;
     * otherwise treat the query as an item of context stack
     */
    syntaxerrposition = geterrposition();
    if (syntaxerrposition > 0)
    {
        errposition(0);
        internalerrposition(syntaxerrposition);
        internalerrquery(query);
    }
    else
    {
        errcontext("SQL statement \"%s\"", query);
    }
}
 
 
/* ----------
 * Support function for initializing local execution variables
 * ----------
 */
static void
copy_plpgsql_datums(PLpgSQL_execstate *estate,
                    PLpgSQL_function *func)
{
    int         ndatums = estate->ndatums;
    PLpgSQL_datum **indatums;
    PLpgSQL_datum **outdatums;
    char       *workspace;
    char       *ws_next;
    int         i;
 
    /* Allocate local datum-pointer array */
    estate->datums = palloc_array(PLpgSQL_datum *, ndatums);
 
    /*
     * To reduce palloc overhead, we make a single palloc request for all the
     * space needed for locally-instantiated datums.
     */
    workspace = palloc(func->copiable_size);
    ws_next = workspace;
 
    /* Fill datum-pointer array, copying datums into workspace as needed */
    indatums = func->datums;
    outdatums = estate->datums;
    for (i = 0; i < ndatums; i++)
    {
        PLpgSQL_datum *indatum = indatums[i];
        PLpgSQL_datum *outdatum;
 
        /* This must agree with plpgsql_finish_datums on what is copiable */
        switch (indatum->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
            case PLPGSQL_DTYPE_PROMISE:
                outdatum = (PLpgSQL_datum *) ws_next;
                memcpy(outdatum, indatum, sizeof(PLpgSQL_var));
                ws_next += MAXALIGN(sizeof(PLpgSQL_var));
                break;
 
            case PLPGSQL_DTYPE_REC:
                outdatum = (PLpgSQL_datum *) ws_next;
                memcpy(outdatum, indatum, sizeof(PLpgSQL_rec));
                ws_next += MAXALIGN(sizeof(PLpgSQL_rec));
                break;
 
            case PLPGSQL_DTYPE_ROW:
            case PLPGSQL_DTYPE_RECFIELD:
 
                /*
                 * These datum records are read-only at runtime, so no need to
                 * copy them (well, RECFIELD contains cached data, but we'd
                 * just as soon centralize the caching anyway).
                 */
                outdatum = indatum;
                break;
 
            default:
                elog(ERROR, "unrecognized dtype: %d", indatum->dtype);
                outdatum = NULL;    /* keep compiler quiet */
                break;
        }
 
        outdatums[i] = outdatum;
    }
 
    Assert(ws_next == workspace + func->copiable_size);
}
 
/*
 * If the variable has an armed "promise", compute the promised value
 * and assign it to the variable.
 * The assignment automatically disarms the promise.
 */
static void
plpgsql_fulfill_promise(PLpgSQL_execstate *estate,
                        PLpgSQL_var *var)
{
    MemoryContext oldcontext;
 
    if (var->promise == PLPGSQL_PROMISE_NONE)
        return;                 /* nothing to do */
 
    /*
     * This will typically be invoked in a short-lived context such as the
     * mcontext.  We must create variable values in the estate's datum
     * context.  This quick-and-dirty solution risks leaking some additional
     * cruft there, but since any one promise is honored at most once per
     * function call, it's probably not worth being more careful.
     */
    oldcontext = MemoryContextSwitchTo(estate->datum_context);
 
    switch (var->promise)
    {
        case PLPGSQL_PROMISE_TG_NAME:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            assign_simple_var(estate, var,
                              DirectFunctionCall1(namein,
                                                  CStringGetDatum(estate->trigdata->tg_trigger->tgname)),
                              false, true);
            break;
 
        case PLPGSQL_PROMISE_TG_WHEN:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            if (TRIGGER_FIRED_BEFORE(estate->trigdata->tg_event))
                assign_text_var(estate, var, "BEFORE");
            else if (TRIGGER_FIRED_AFTER(estate->trigdata->tg_event))
                assign_text_var(estate, var, "AFTER");
            else if (TRIGGER_FIRED_INSTEAD(estate->trigdata->tg_event))
                assign_text_var(estate, var, "INSTEAD OF");
            else
                elog(ERROR, "unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
            break;
 
        case PLPGSQL_PROMISE_TG_LEVEL:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            if (TRIGGER_FIRED_FOR_ROW(estate->trigdata->tg_event))
                assign_text_var(estate, var, "ROW");
            else if (TRIGGER_FIRED_FOR_STATEMENT(estate->trigdata->tg_event))
                assign_text_var(estate, var, "STATEMENT");
            else
                elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT");
            break;
 
        case PLPGSQL_PROMISE_TG_OP:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            if (TRIGGER_FIRED_BY_INSERT(estate->trigdata->tg_event))
                assign_text_var(estate, var, "INSERT");
            else if (TRIGGER_FIRED_BY_UPDATE(estate->trigdata->tg_event))
                assign_text_var(estate, var, "UPDATE");
            else if (TRIGGER_FIRED_BY_DELETE(estate->trigdata->tg_event))
                assign_text_var(estate, var, "DELETE");
            else if (TRIGGER_FIRED_BY_TRUNCATE(estate->trigdata->tg_event))
                assign_text_var(estate, var, "TRUNCATE");
            else
                elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
            break;
 
        case PLPGSQL_PROMISE_TG_RELID:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            assign_simple_var(estate, var,
                              ObjectIdGetDatum(estate->trigdata->tg_relation->rd_id),
                              false, false);
            break;
 
        case PLPGSQL_PROMISE_TG_TABLE_NAME:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            assign_simple_var(estate, var,
                              DirectFunctionCall1(namein,
                                                  CStringGetDatum(RelationGetRelationName(estate->trigdata->tg_relation))),
                              false, true);
            break;
 
        case PLPGSQL_PROMISE_TG_TABLE_SCHEMA:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            assign_simple_var(estate, var,
                              DirectFunctionCall1(namein,
                                                  CStringGetDatum(get_namespace_name(RelationGetNamespace(estate->trigdata->tg_relation)))),
                              false, true);
            break;
 
        case PLPGSQL_PROMISE_TG_NARGS:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            assign_simple_var(estate, var,
                              Int16GetDatum(estate->trigdata->tg_trigger->tgnargs),
                              false, false);
            break;
 
        case PLPGSQL_PROMISE_TG_ARGV:
            if (estate->trigdata == NULL)
                elog(ERROR, "trigger promise is not in a trigger function");
            if (estate->trigdata->tg_trigger->tgnargs > 0)
            {
                /*
                 * For historical reasons, tg_argv[] subscripts start at zero
                 * not one.  So we can't use construct_array().
                 */
                int         nelems = estate->trigdata->tg_trigger->tgnargs;
                Datum      *elems;
                int         dims[1];
                int         lbs[1];
                int         i;
 
                elems = palloc_array(Datum, nelems);
                for (i = 0; i < nelems; i++)
                    elems[i] = CStringGetTextDatum(estate->trigdata->tg_trigger->tgargs[i]);
                dims[0] = nelems;
                lbs[0] = 0;
 
                assign_simple_var(estate, var,
                                  PointerGetDatum(construct_md_array(elems, NULL,
                                                                     1, dims, lbs,
                                                                     TEXTOID,
                                                                     -1, false, TYPALIGN_INT)),
                                  false, true);
            }
            else
            {
                assign_simple_var(estate, var, (Datum) 0, true, false);
            }
            break;
 
        case PLPGSQL_PROMISE_TG_EVENT:
            if (estate->evtrigdata == NULL)
                elog(ERROR, "event trigger promise is not in an event trigger function");
            assign_text_var(estate, var, estate->evtrigdata->event);
            break;
 
        case PLPGSQL_PROMISE_TG_TAG:
            if (estate->evtrigdata == NULL)
                elog(ERROR, "event trigger promise is not in an event trigger function");
            assign_text_var(estate, var, GetCommandTagName(estate->evtrigdata->tag));
            break;
 
        default:
            elog(ERROR, "unrecognized promise type: %d", var->promise);
    }
 
    MemoryContextSwitchTo(oldcontext);
}
 
/*
 * Create a memory context for statement-lifespan variables, if we don't
 * have one already.  It will be a child of stmt_mcontext_parent, which is
 * either the function's main context or a pushed-down outer stmt_mcontext.
 */
static MemoryContext
get_stmt_mcontext(PLpgSQL_execstate *estate)
{
    if (estate->stmt_mcontext == NULL)
    {
        estate->stmt_mcontext =
            AllocSetContextCreate(estate->stmt_mcontext_parent,
                                  "PLpgSQL per-statement data",
                                  ALLOCSET_DEFAULT_SIZES);
    }
    return estate->stmt_mcontext;
}
 
/*
 * Push down the current stmt_mcontext so that called statements won't use it.
 * This is needed by statements that have statement-lifespan data and need to
 * preserve it across some inner statements.  The caller should eventually do
 * pop_stmt_mcontext().
 */
static void
push_stmt_mcontext(PLpgSQL_execstate *estate)
{
    /* Should have done get_stmt_mcontext() first */
    Assert(estate->stmt_mcontext != NULL);
    /* Assert we've not messed up the stack linkage */
    Assert(MemoryContextGetParent(estate->stmt_mcontext) == estate->stmt_mcontext_parent);
    /* Push it down to become the parent of any nested stmt mcontext */
    estate->stmt_mcontext_parent = estate->stmt_mcontext;
    /* And make it not available for use directly */
    estate->stmt_mcontext = NULL;
}
 
/*
 * Undo push_stmt_mcontext().  We assume this is done just before or after
 * resetting the caller's stmt_mcontext; since that action will also delete
 * any child contexts, there's no need to explicitly delete whatever context
 * might currently be estate->stmt_mcontext.
 */
static void
pop_stmt_mcontext(PLpgSQL_execstate *estate)
{
    /* We need only pop the stack */
    estate->stmt_mcontext = estate->stmt_mcontext_parent;
    estate->stmt_mcontext_parent = MemoryContextGetParent(estate->stmt_mcontext);
}
 
 
/*
 * Subroutine for exec_stmt_block: does any condition in the condition list
 * match the current exception?
 */
static bool
exception_matches_conditions(ErrorData *edata, PLpgSQL_condition *cond)
{
    for (; cond != NULL; cond = cond->next)
    {
        int         sqlerrstate = cond->sqlerrstate;
 
        /*
         * OTHERS matches everything *except* query-canceled and
         * assert-failure.  If you're foolish enough, you can match those
         * explicitly.
         */
        if (sqlerrstate == PLPGSQL_OTHERS)
        {
            if (edata->sqlerrcode != ERRCODE_QUERY_CANCELED &&
                edata->sqlerrcode != ERRCODE_ASSERT_FAILURE)
                return true;
        }
        /* Exact match? */
        else if (edata->sqlerrcode == sqlerrstate)
            return true;
        /* Category match? */
        else if (ERRCODE_IS_CATEGORY(sqlerrstate) &&
                 ERRCODE_TO_CATEGORY(edata->sqlerrcode) == sqlerrstate)
            return true;
    }
    return false;
}
 
 
/* ----------
 * exec_toplevel_block          Execute the toplevel block
 *
 * This is intentionally equivalent to executing exec_stmts() with a
 * list consisting of the one statement.  One tiny difference is that
 * we do not bother to save the entry value of estate->err_stmt;
 * that's assumed to be NULL.
 * ----------
 */
static int
exec_toplevel_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
{
    int         rc;
 
    estate->err_stmt = (PLpgSQL_stmt *) block;
 
    /* Let the plugin know that we are about to execute this statement */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_beg)
        ((*plpgsql_plugin_ptr)->stmt_beg) (estate, (PLpgSQL_stmt *) block);
 
    CHECK_FOR_INTERRUPTS();
 
    rc = exec_stmt_block(estate, block);
 
    /* Let the plugin know that we have finished executing this statement */
    if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_end)
        ((*plpgsql_plugin_ptr)->stmt_end) (estate, (PLpgSQL_stmt *) block);
 
    estate->err_stmt = NULL;
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_block          Execute a block of statements
 * ----------
 */
static int
exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
{
    volatile int rc = -1;
    int         i;
 
    /*
     * First initialize all variables declared in this block
     */
    estate->err_text = gettext_noop("during statement block local variable initialization");
 
    for (i = 0; i < block->n_initvars; i++)
    {
        int         n = block->initvarnos[i];
        PLpgSQL_datum *datum = estate->datums[n];
 
        /*
         * The set of dtypes handled here must match plpgsql_add_initdatums().
         *
         * Note that we currently don't support promise datums within blocks,
         * only at a function's outermost scope, so we needn't handle those
         * here.
         *
         * Since RECFIELD isn't a supported case either, it's okay to cast the
         * PLpgSQL_datum to PLpgSQL_variable.
         */
        estate->err_var = (PLpgSQL_variable *) datum;
 
        switch (datum->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) datum;
 
                    /*
                     * Free any old value, in case re-entering block, and
                     * initialize to NULL
                     */
                    assign_simple_var(estate, var, (Datum) 0, true, false);
 
                    if (var->default_val == NULL)
                    {
                        /*
                         * If needed, give the datatype a chance to reject
                         * NULLs, by assigning a NULL to the variable.  We
                         * claim the value is of type UNKNOWN, not the var's
                         * datatype, else coercion will be skipped.
                         */
                        if (var->datatype->typtype == TYPTYPE_DOMAIN)
                            exec_assign_value(estate,
                                              (PLpgSQL_datum *) var,
                                              (Datum) 0,
                                              true,
                                              UNKNOWNOID,
                                              -1);
 
                        /* parser should have rejected NOT NULL */
                        Assert(!var->notnull);
                    }
                    else
                    {
                        exec_assign_expr(estate, (PLpgSQL_datum *) var,
                                         var->default_val);
                    }
                }
                break;
 
            case PLPGSQL_DTYPE_REC:
                {
                    PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
 
                    /*
                     * Deletion of any existing object will be handled during
                     * the assignments below, and in some cases it's more
                     * efficient for us not to get rid of it beforehand.
                     */
                    if (rec->default_val == NULL)
                    {
                        /*
                         * If needed, give the datatype a chance to reject
                         * NULLs, by assigning a NULL to the variable.
                         */
                        exec_move_row(estate, (PLpgSQL_variable *) rec,
                                      NULL, NULL);
 
                        /* parser should have rejected NOT NULL */
                        Assert(!rec->notnull);
                    }
                    else
                    {
                        exec_assign_expr(estate, (PLpgSQL_datum *) rec,
                                         rec->default_val);
                    }
                }
                break;
 
            default:
                elog(ERROR, "unrecognized dtype: %d", datum->dtype);
        }
    }
 
    estate->err_var = NULL;
 
    if (block->exceptions)
    {
        /*
         * Execute the statements in the block's body inside a sub-transaction
         */
        MemoryContext oldcontext = CurrentMemoryContext;
        ResourceOwner oldowner = CurrentResourceOwner;
        ExprContext *old_eval_econtext = estate->eval_econtext;
        ErrorData  *save_cur_error = estate->cur_error;
        MemoryContext stmt_mcontext;
 
        estate->err_text = gettext_noop("during statement block entry");
 
        /*
         * We will need a stmt_mcontext to hold the error data if an error
         * occurs.  It seems best to force it to exist before entering the
         * subtransaction, so that we reduce the risk of out-of-memory during
         * error recovery, and because this greatly simplifies restoring the
         * stmt_mcontext stack to the correct state after an error.  We can
         * ameliorate the cost of this by allowing the called statements to
         * use this mcontext too; so we don't push it down here.
         */
        stmt_mcontext = get_stmt_mcontext(estate);
 
        BeginInternalSubTransaction(NULL);
        /* Want to run statements inside function's memory context */
        MemoryContextSwitchTo(oldcontext);
 
        PG_TRY();
        {
            /*
             * We need to run the block's statements with a new eval_econtext
             * that belongs to the current subtransaction; if we try to use
             * the outer econtext then ExprContext shutdown callbacks will be
             * called at the wrong times.
             */
            plpgsql_create_econtext(estate);
 
            estate->err_text = NULL;
 
            /* Run the block's statements */
            rc = exec_stmts(estate, block->body);
 
            estate->err_text = gettext_noop("during statement block exit");
 
            /*
             * If the block ended with RETURN, we may need to copy the return
             * value out of the subtransaction eval_context.  We can avoid a
             * physical copy if the value happens to be a R/W expanded object.
             */
            if (rc == PLPGSQL_RC_RETURN &&
                !estate->retisset &&
                !estate->retisnull)
            {
                int16       resTypLen;
                bool        resTypByVal;
 
                get_typlenbyval(estate->rettype, &resTypLen, &resTypByVal);
                estate->retval = datumTransfer(estate->retval,
                                               resTypByVal, resTypLen);
            }
 
            /* Commit the inner transaction, return to outer xact context */
            ReleaseCurrentSubTransaction();
            MemoryContextSwitchTo(oldcontext);
            CurrentResourceOwner = oldowner;
 
            /* Assert that the stmt_mcontext stack is unchanged */
            Assert(stmt_mcontext == estate->stmt_mcontext);
 
            /*
             * Revert to outer eval_econtext.  (The inner one was
             * automatically cleaned up during subxact exit.)
             */
            estate->eval_econtext = old_eval_econtext;
        }
        PG_CATCH();
        {
            ErrorData  *edata;
            ListCell   *e;
 
            estate->err_text = gettext_noop("during exception cleanup");
 
            /* Save error info in our stmt_mcontext */
            MemoryContextSwitchTo(stmt_mcontext);
            edata = CopyErrorData();
            FlushErrorState();
 
            /* Abort the inner transaction */
            RollbackAndReleaseCurrentSubTransaction();
            MemoryContextSwitchTo(oldcontext);
            CurrentResourceOwner = oldowner;
 
            /*
             * Set up the stmt_mcontext stack as though we had restored our
             * previous state and then done push_stmt_mcontext().  The push is
             * needed so that statements in the exception handler won't
             * clobber the error data that's in our stmt_mcontext.
             */
            estate->stmt_mcontext_parent = stmt_mcontext;
            estate->stmt_mcontext = NULL;
 
            /*
             * Now we can delete any nested stmt_mcontexts that might have
             * been created as children of ours.  (Note: we do not immediately
             * release any statement-lifespan data that might have been left
             * behind in stmt_mcontext itself.  We could attempt that by doing
             * a MemoryContextReset on it before collecting the error data
             * above, but it seems too risky to do any significant amount of
             * work before collecting the error.)
             */
            MemoryContextDeleteChildren(stmt_mcontext);
 
            /* Revert to outer eval_econtext */
            estate->eval_econtext = old_eval_econtext;
 
            /*
             * Must clean up the econtext too.  However, any tuple table made
             * in the subxact will have been thrown away by SPI during subxact
             * abort, so we don't need to (and mustn't try to) free the
             * eval_tuptable.
             */
            estate->eval_tuptable = NULL;
            exec_eval_cleanup(estate);
 
            /* Look for a matching exception handler */
            foreach(e, block->exceptions->exc_list)
            {
                PLpgSQL_exception *exception = (PLpgSQL_exception *) lfirst(e);
 
                if (exception_matches_conditions(edata, exception->conditions))
                {
                    /*
                     * Initialize the magic SQLSTATE and SQLERRM variables for
                     * the exception block; this also frees values from any
                     * prior use of the same exception. We needn't do this
                     * until we have found a matching exception.
                     */
                    PLpgSQL_var *state_var;
                    PLpgSQL_var *errm_var;
 
                    state_var = (PLpgSQL_var *)
                        estate->datums[block->exceptions->sqlstate_varno];
                    errm_var = (PLpgSQL_var *)
                        estate->datums[block->exceptions->sqlerrm_varno];
 
                    assign_text_var(estate, state_var,
                                    unpack_sql_state(edata->sqlerrcode));
                    assign_text_var(estate, errm_var, edata->message);
 
                    /*
                     * Also set up cur_error so the error data is accessible
                     * inside the handler.
                     */
                    estate->cur_error = edata;
 
                    estate->err_text = NULL;
 
                    rc = exec_stmts(estate, exception->action);
 
                    break;
                }
            }
 
            /*
             * Restore previous state of cur_error, whether or not we executed
             * a handler.  This is needed in case an error got thrown from
             * some inner block's exception handler.
             */
            estate->cur_error = save_cur_error;
 
            /* If no match found, re-throw the error */
            if (e == NULL)
                ReThrowError(edata);
 
            /* Restore stmt_mcontext stack and release the error data */
            pop_stmt_mcontext(estate);
            MemoryContextReset(stmt_mcontext);
        }
        PG_END_TRY();
 
        Assert(save_cur_error == estate->cur_error);
    }
    else
    {
        /*
         * Just execute the statements in the block's body
         */
        estate->err_text = NULL;
 
        rc = exec_stmts(estate, block->body);
    }
 
    estate->err_text = NULL;
 
    /*
     * Handle the return code.  This is intentionally different from
     * LOOP_RC_PROCESSING(): CONTINUE never matches a block, and EXIT matches
     * a block only if there is a label match.
     */
    switch (rc)
    {
        case PLPGSQL_RC_OK:
        case PLPGSQL_RC_RETURN:
        case PLPGSQL_RC_CONTINUE:
            return rc;
 
        case PLPGSQL_RC_EXIT:
            if (estate->exitlabel == NULL)
                return PLPGSQL_RC_EXIT;
            if (block->label == NULL)
                return PLPGSQL_RC_EXIT;
            if (strcmp(block->label, estate->exitlabel) != 0)
                return PLPGSQL_RC_EXIT;
            estate->exitlabel = NULL;
            return PLPGSQL_RC_OK;
 
        default:
            elog(ERROR, "unrecognized rc: %d", rc);
    }
 
    return PLPGSQL_RC_OK;
}
 
 
/* ----------
 * exec_stmts           Iterate over a list of statements
 *              as long as their return code is OK
 * ----------
 */
static int
exec_stmts(PLpgSQL_execstate *estate, List *stmts)
{
    PLpgSQL_stmt *save_estmt = estate->err_stmt;
    ListCell   *s;
 
    if (stmts == NIL)
    {
        /*
         * Ensure we do a CHECK_FOR_INTERRUPTS() even though there is no
         * statement.  This prevents hangup in a tight loop if, for instance,
         * there is a LOOP construct with an empty body.
         */
        CHECK_FOR_INTERRUPTS();
        return PLPGSQL_RC_OK;
    }
 
    foreach(s, stmts)
    {
        PLpgSQL_stmt *stmt = (PLpgSQL_stmt *) lfirst(s);
        int         rc;
 
        estate->err_stmt = stmt;
 
        /* Let the plugin know that we are about to execute this statement */
        if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_beg)
            ((*plpgsql_plugin_ptr)->stmt_beg) (estate, stmt);
 
        CHECK_FOR_INTERRUPTS();
 
        switch (stmt->cmd_type)
        {
            case PLPGSQL_STMT_BLOCK:
                rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
                break;
 
            case PLPGSQL_STMT_ASSIGN:
                rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign *) stmt);
                break;
 
            case PLPGSQL_STMT_PERFORM:
                rc = exec_stmt_perform(estate, (PLpgSQL_stmt_perform *) stmt);
                break;
 
            case PLPGSQL_STMT_CALL:
                rc = exec_stmt_call(estate, (PLpgSQL_stmt_call *) stmt);
                break;
 
            case PLPGSQL_STMT_GETDIAG:
                rc = exec_stmt_getdiag(estate, (PLpgSQL_stmt_getdiag *) stmt);
                break;
 
            case PLPGSQL_STMT_IF:
                rc = exec_stmt_if(estate, (PLpgSQL_stmt_if *) stmt);
                break;
 
            case PLPGSQL_STMT_CASE:
                rc = exec_stmt_case(estate, (PLpgSQL_stmt_case *) stmt);
                break;
 
            case PLPGSQL_STMT_LOOP:
                rc = exec_stmt_loop(estate, (PLpgSQL_stmt_loop *) stmt);
                break;
 
            case PLPGSQL_STMT_WHILE:
                rc = exec_stmt_while(estate, (PLpgSQL_stmt_while *) stmt);
                break;
 
            case PLPGSQL_STMT_FORI:
                rc = exec_stmt_fori(estate, (PLpgSQL_stmt_fori *) stmt);
                break;
 
            case PLPGSQL_STMT_FORS:
                rc = exec_stmt_fors(estate, (PLpgSQL_stmt_fors *) stmt);
                break;
 
            case PLPGSQL_STMT_FORC:
                rc = exec_stmt_forc(estate, (PLpgSQL_stmt_forc *) stmt);
                break;
 
            case PLPGSQL_STMT_FOREACH_A:
                rc = exec_stmt_foreach_a(estate, (PLpgSQL_stmt_foreach_a *) stmt);
                break;
 
            case PLPGSQL_STMT_EXIT:
                rc = exec_stmt_exit(estate, (PLpgSQL_stmt_exit *) stmt);
                break;
 
            case PLPGSQL_STMT_RETURN:
                rc = exec_stmt_return(estate, (PLpgSQL_stmt_return *) stmt);
                break;
 
            case PLPGSQL_STMT_RETURN_NEXT:
                rc = exec_stmt_return_next(estate, (PLpgSQL_stmt_return_next *) stmt);
                break;
 
            case PLPGSQL_STMT_RETURN_QUERY:
                rc = exec_stmt_return_query(estate, (PLpgSQL_stmt_return_query *) stmt);
                break;
 
            case PLPGSQL_STMT_RAISE:
                rc = exec_stmt_raise(estate, (PLpgSQL_stmt_raise *) stmt);
                break;
 
            case PLPGSQL_STMT_ASSERT:
                rc = exec_stmt_assert(estate, (PLpgSQL_stmt_assert *) stmt);
                break;
 
            case PLPGSQL_STMT_EXECSQL:
                rc = exec_stmt_execsql(estate, (PLpgSQL_stmt_execsql *) stmt);
                break;
 
            case PLPGSQL_STMT_DYNEXECUTE:
                rc = exec_stmt_dynexecute(estate, (PLpgSQL_stmt_dynexecute *) stmt);
                break;
 
            case PLPGSQL_STMT_DYNFORS:
                rc = exec_stmt_dynfors(estate, (PLpgSQL_stmt_dynfors *) stmt);
                break;
 
            case PLPGSQL_STMT_OPEN:
                rc = exec_stmt_open(estate, (PLpgSQL_stmt_open *) stmt);
                break;
 
            case PLPGSQL_STMT_FETCH:
                rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch *) stmt);
                break;
 
            case PLPGSQL_STMT_CLOSE:
                rc = exec_stmt_close(estate, (PLpgSQL_stmt_close *) stmt);
                break;
 
            case PLPGSQL_STMT_COMMIT:
                rc = exec_stmt_commit(estate, (PLpgSQL_stmt_commit *) stmt);
                break;
 
            case PLPGSQL_STMT_ROLLBACK:
                rc = exec_stmt_rollback(estate, (PLpgSQL_stmt_rollback *) stmt);
                break;
 
            default:
                /* point err_stmt to parent, since this one seems corrupt */
                estate->err_stmt = save_estmt;
                elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
                rc = -1;        /* keep compiler quiet */
        }
 
        /* Let the plugin know that we have finished executing this statement */
        if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_end)
            ((*plpgsql_plugin_ptr)->stmt_end) (estate, stmt);
 
        if (rc != PLPGSQL_RC_OK)
        {
            estate->err_stmt = save_estmt;
            return rc;
        }
    }                           /* end of loop over statements */
 
    estate->err_stmt = save_estmt;
    return PLPGSQL_RC_OK;
}
 
 
/* ----------
 * exec_stmt_assign         Evaluate an expression and
 *                  put the result into a variable.
 * ----------
 */
static int
exec_stmt_assign(PLpgSQL_execstate *estate, PLpgSQL_stmt_assign *stmt)
{
    Assert(stmt->varno >= 0);
 
    exec_assign_expr(estate, estate->datums[stmt->varno], stmt->expr);
 
    return PLPGSQL_RC_OK;
}
 
/* ----------
 * exec_stmt_perform        Evaluate query and discard result (but set
 *                          FOUND depending on whether at least one row
 *                          was returned).
 * ----------
 */
static int
exec_stmt_perform(PLpgSQL_execstate *estate, PLpgSQL_stmt_perform *stmt)
{
    PLpgSQL_expr *expr = stmt->expr;
 
    (void) exec_run_select(estate, expr, 0, NULL);
    exec_set_found(estate, (estate->eval_processed != 0));
    exec_eval_cleanup(estate);
 
    return PLPGSQL_RC_OK;
}
 
/*
 * exec_stmt_call
 *
 * NOTE: this is used for both CALL and DO statements.
 */
static int
exec_stmt_call(PLpgSQL_execstate *estate, PLpgSQL_stmt_call *stmt)
{
    PLpgSQL_expr *expr = stmt->expr;
    LocalTransactionId before_lxid;
    LocalTransactionId after_lxid;
    ParamListInfo paramLI;
    SPIExecuteOptions options;
    int         rc;
 
    /*
     * Make a plan if we don't have one already.
     */
    if (expr->plan == NULL)
        exec_prepare_plan(estate, expr, 0);
 
    /*
     * A CALL or DO can never be a simple expression.
     */
    Assert(!expr->expr_simple_expr);
 
    /*
     * Also construct a DTYPE_ROW datum representing the plpgsql variables
     * associated with the procedure's output arguments.  Then we can use
     * exec_move_row() to do the assignments.
     */
    if (stmt->is_call && stmt->target == NULL)
        stmt->target = make_callstmt_target(estate, expr);
 
    paramLI = setup_param_list(estate, expr);
 
    before_lxid = MyProc->vxid.lxid;
 
    /*
     * If we have a procedure-lifespan resowner, use that to hold the refcount
     * for the plan.  This avoids refcount leakage complaints if the called
     * procedure ends the current transaction.
     *
     * Also, tell SPI to allow non-atomic execution.
     */
    memset(&options, 0, sizeof(options));
    options.params = paramLI;
    options.read_only = estate->readonly_func;
    options.allow_nonatomic = true;
    options.owner = estate->procedure_resowner;
 
    rc = SPI_execute_plan_extended(expr->plan, &options);
 
    if (rc < 0)
        elog(ERROR, "SPI_execute_plan_extended failed executing query \"%s\": %s",
             expr->query, SPI_result_code_string(rc));
 
    after_lxid = MyProc->vxid.lxid;
 
    if (before_lxid != after_lxid)
    {
        /*
         * If we are in a new transaction after the call, we need to build new
         * simple-expression infrastructure.
         */
        estate->simple_eval_estate = NULL;
        estate->simple_eval_resowner = NULL;
        plpgsql_create_econtext(estate);
    }
 
    /*
     * Check result rowcount; if there's one row, assign procedure's output
     * values back to the appropriate variables.
     */
    if (SPI_processed == 1)
    {
        SPITupleTable *tuptab = SPI_tuptable;
 
        if (!stmt->is_call)
            elog(ERROR, "DO statement returned a row");
 
        exec_move_row(estate, stmt->target, tuptab->vals[0], tuptab->tupdesc);
    }
    else if (SPI_processed > 1)
        elog(ERROR, "procedure call returned more than one row");
 
    exec_eval_cleanup(estate);
    SPI_freetuptable(SPI_tuptable);
 
    return PLPGSQL_RC_OK;
}
 
/*
 * We construct a DTYPE_ROW datum representing the plpgsql variables
 * associated with the procedure's output arguments.  Then we can use
 * exec_move_row() to do the assignments.
 */
static PLpgSQL_variable *
make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
{
    CachedPlan *cplan;
    PlannedStmt *pstmt;
    CallStmt   *stmt;
    FuncExpr   *funcexpr;
    HeapTuple   func_tuple;
    Oid        *argtypes;
    char      **argnames;
    char       *argmodes;
    int         numargs;
    MemoryContext oldcontext;
    PLpgSQL_row *row;
    int         nfields;
    int         i;
 
    /* Use eval_mcontext for any cruft accumulated here */
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
 
    /*
     * Get the parsed CallStmt, and look up the called procedure.  We use
     * SPI_plan_get_cached_plan to cover the edge case where expr->plan is
     * already stale and needs to be updated.
     */
    cplan = SPI_plan_get_cached_plan(expr->plan);
    if (cplan == NULL || list_length(cplan->stmt_list) != 1)
        elog(ERROR, "query for CALL statement is not a CallStmt");
    pstmt = linitial_node(PlannedStmt, cplan->stmt_list);
    stmt = (CallStmt *) pstmt->utilityStmt;
    if (stmt == NULL || !IsA(stmt, CallStmt))
        elog(ERROR, "query for CALL statement is not a CallStmt");
 
    funcexpr = stmt->funcexpr;
 
    func_tuple = SearchSysCache1(PROCOID,
                                 ObjectIdGetDatum(funcexpr->funcid));
    if (!HeapTupleIsValid(func_tuple))
        elog(ERROR, "cache lookup failed for function %u",
             funcexpr->funcid);
 
    /*
     * Get the argument names and modes, so that we can deliver on-point error
     * messages when something is wrong.
     */
    numargs = get_func_arg_info(func_tuple, &argtypes, &argnames, &argmodes);
 
    ReleaseSysCache(func_tuple);
 
    /*
     * Begin constructing row Datum; keep it in fn_cxt so it's adequately
     * long-lived.
     */
    MemoryContextSwitchTo(estate->func->fn_cxt);
 
    row = palloc0_object(PLpgSQL_row);
    row->dtype = PLPGSQL_DTYPE_ROW;
    row->refname = "(unnamed row)";
    row->lineno = -1;
    row->varnos = palloc_array(int, numargs);
 
    MemoryContextSwitchTo(get_eval_mcontext(estate));
 
    /*
     * Examine procedure's argument list.  Each output arg position should be
     * an unadorned plpgsql variable (Datum), which we can insert into the row
     * Datum.
     */
    nfields = 0;
    for (i = 0; i < numargs; i++)
    {
        if (argmodes &&
            (argmodes[i] == PROARGMODE_INOUT ||
             argmodes[i] == PROARGMODE_OUT))
        {
            Node       *n = list_nth(stmt->outargs, nfields);
 
            if (IsA(n, Param))
            {
                Param      *param = (Param *) n;
                int         dno;
 
                /* paramid is offset by 1 (see make_datum_param()) */
                dno = param->paramid - 1;
                /* must check assignability now, because grammar can't */
                exec_check_assignable(estate, dno);
                row->varnos[nfields++] = dno;
            }
            else
            {
                /* report error using parameter name, if available */
                if (argnames && argnames[i] && argnames[i][0])
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                             errmsg("procedure parameter \"%s\" is an output parameter but corresponding argument is not writable",
                                    argnames[i])));
                else
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                             errmsg("procedure parameter %d is an output parameter but corresponding argument is not writable",
                                    i + 1)));
            }
        }
    }
 
    Assert(nfields == list_length(stmt->outargs));
 
    row->nfields = nfields;
 
    ReleaseCachedPlan(cplan, CurrentResourceOwner);
 
    MemoryContextSwitchTo(oldcontext);
 
    return (PLpgSQL_variable *) row;
}
 
/* ----------
 * exec_stmt_getdiag                    Put internal PG information into
 *                                      specified variables.
 * ----------
 */
static int
exec_stmt_getdiag(PLpgSQL_execstate *estate, PLpgSQL_stmt_getdiag *stmt)
{
    ListCell   *lc;
 
    /*
     * GET STACKED DIAGNOSTICS is only valid inside an exception handler.
     *
     * Note: we trust the grammar to have disallowed the relevant item kinds
     * if not is_stacked, otherwise we'd dump core below.
     */
    if (stmt->is_stacked && estate->cur_error == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
                 errmsg("GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));
 
    foreach(lc, stmt->diag_items)
    {
        PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
        PLpgSQL_datum *var = estate->datums[diag_item->target];
 
        switch (diag_item->kind)
        {
            case PLPGSQL_GETDIAG_ROW_COUNT:
                exec_assign_value(estate, var,
                                  UInt64GetDatum(estate->eval_processed),
                                  false, INT8OID, -1);
                break;
 
            case PLPGSQL_GETDIAG_ROUTINE_OID:
                exec_assign_value(estate, var,
                                  ObjectIdGetDatum(estate->func->fn_oid),
                                  false, OIDOID, -1);
                break;
 
            case PLPGSQL_GETDIAG_ERROR_CONTEXT:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->context);
                break;
 
            case PLPGSQL_GETDIAG_ERROR_DETAIL:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->detail);
                break;
 
            case PLPGSQL_GETDIAG_ERROR_HINT:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->hint);
                break;
 
            case PLPGSQL_GETDIAG_RETURNED_SQLSTATE:
                exec_assign_c_string(estate, var,
                                     unpack_sql_state(estate->cur_error->sqlerrcode));
                break;
 
            case PLPGSQL_GETDIAG_COLUMN_NAME:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->column_name);
                break;
 
            case PLPGSQL_GETDIAG_CONSTRAINT_NAME:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->constraint_name);
                break;
 
            case PLPGSQL_GETDIAG_DATATYPE_NAME:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->datatype_name);
                break;
 
            case PLPGSQL_GETDIAG_MESSAGE_TEXT:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->message);
                break;
 
            case PLPGSQL_GETDIAG_TABLE_NAME:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->table_name);
                break;
 
            case PLPGSQL_GETDIAG_SCHEMA_NAME:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->schema_name);
                break;
 
            case PLPGSQL_GETDIAG_CONTEXT:
                {
                    char       *contextstackstr;
                    MemoryContext oldcontext;
 
                    /* Use eval_mcontext for short-lived string */
                    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
                    contextstackstr = GetErrorContextStack();
                    MemoryContextSwitchTo(oldcontext);
 
                    exec_assign_c_string(estate, var, contextstackstr);
                }
                break;
 
            default:
                elog(ERROR, "unrecognized diagnostic item kind: %d",
                     diag_item->kind);
        }
    }
 
    exec_eval_cleanup(estate);
 
    return PLPGSQL_RC_OK;
}
 
/* ----------
 * exec_stmt_if             Evaluate a bool expression and
 *                  execute the true or false body
 *                  conditionally.
 * ----------
 */
static int
exec_stmt_if(PLpgSQL_execstate *estate, PLpgSQL_stmt_if *stmt)
{
    bool        value;
    bool        isnull;
    ListCell   *lc;
 
    value = exec_eval_boolean(estate, stmt->cond, &isnull);
    exec_eval_cleanup(estate);
    if (!isnull && value)
        return exec_stmts(estate, stmt->then_body);
 
    foreach(lc, stmt->elsif_list)
    {
        PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(lc);
 
        value = exec_eval_boolean(estate, elif->cond, &isnull);
        exec_eval_cleanup(estate);
        if (!isnull && value)
            return exec_stmts(estate, elif->stmts);
    }
 
    return exec_stmts(estate, stmt->else_body);
}
 
 
/*-----------
 * exec_stmt_case
 *-----------
 */
static int
exec_stmt_case(PLpgSQL_execstate *estate, PLpgSQL_stmt_case *stmt)
{
    PLpgSQL_var *t_var = NULL;
    bool        isnull;
    ListCell   *l;
 
    if (stmt->t_expr != NULL)
    {
        /* simple case */
        Datum       t_val;
        Oid         t_typoid;
        int32       t_typmod;
 
        t_val = exec_eval_expr(estate, stmt->t_expr,
                               &isnull, &t_typoid, &t_typmod);
 
        t_var = (PLpgSQL_var *) estate->datums[stmt->t_varno];
 
        /*
         * When expected datatype is different from real, change it. Note that
         * what we're modifying here is an execution copy of the datum, so
         * this doesn't affect the originally stored function parse tree. (In
         * theory, if the expression datatype keeps changing during execution,
         * this could cause a function-lifespan memory leak.  Doesn't seem
         * worth worrying about though.)
         */
        if (t_var->datatype->typoid != t_typoid ||
            t_var->datatype->atttypmod != t_typmod)
            t_var->datatype = plpgsql_build_datatype(t_typoid,
                                                     t_typmod,
                                                     estate->func->fn_input_collation,
                                                     NULL);
 
        /* now we can assign to the variable */
        exec_assign_value(estate,
                          (PLpgSQL_datum *) t_var,
                          t_val,
                          isnull,
                          t_typoid,
                          t_typmod);
 
        exec_eval_cleanup(estate);
    }
 
    /* Now search for a successful WHEN clause */
    foreach(l, stmt->case_when_list)
    {
        PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
        bool        value;
 
        value = exec_eval_boolean(estate, cwt->expr, &isnull);
        exec_eval_cleanup(estate);
        if (!isnull && value)
        {
            /* Found it */
 
            /* We can now discard any value we had for the temp variable */
            if (t_var != NULL)
                assign_simple_var(estate, t_var, (Datum) 0, true, false);
 
            /* Evaluate the statement(s), and we're done */
            return exec_stmts(estate, cwt->stmts);
        }
    }
 
    /* We can now discard any value we had for the temp variable */
    if (t_var != NULL)
        assign_simple_var(estate, t_var, (Datum) 0, true, false);
 
    /* SQL2003 mandates this error if there was no ELSE clause */
    if (!stmt->have_else)
        ereport(ERROR,
                (errcode(ERRCODE_CASE_NOT_FOUND),
                 errmsg("case not found"),
                 errhint("CASE statement is missing ELSE part.")));
 
    /* Evaluate the ELSE statements, and we're done */
    return exec_stmts(estate, stmt->else_stmts);
}
 
 
/* ----------
 * exec_stmt_loop           Loop over statements until
 *                  an exit occurs.
 * ----------
 */
static int
exec_stmt_loop(PLpgSQL_execstate *estate, PLpgSQL_stmt_loop *stmt)
{
    int         rc = PLPGSQL_RC_OK;
 
    for (;;)
    {
        rc = exec_stmts(estate, stmt->body);
 
        LOOP_RC_PROCESSING(stmt->label, break);
    }
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_while          Loop over statements as long
 *                  as an expression evaluates to
 *                  true or an exit occurs.
 * ----------
 */
static int
exec_stmt_while(PLpgSQL_execstate *estate, PLpgSQL_stmt_while *stmt)
{
    int         rc = PLPGSQL_RC_OK;
 
    for (;;)
    {
        bool        value;
        bool        isnull;
 
        value = exec_eval_boolean(estate, stmt->cond, &isnull);
        exec_eval_cleanup(estate);
 
        if (isnull || !value)
            break;
 
        rc = exec_stmts(estate, stmt->body);
 
        LOOP_RC_PROCESSING(stmt->label, break);
    }
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_fori           Iterate an integer variable
 *                  from a lower to an upper value
 *                  incrementing or decrementing by the BY value
 * ----------
 */
static int
exec_stmt_fori(PLpgSQL_execstate *estate, PLpgSQL_stmt_fori *stmt)
{
    PLpgSQL_var *var;
    Datum       value;
    bool        isnull;
    Oid         valtype;
    int32       valtypmod;
    int32       loop_value;
    int32       end_value;
    int32       step_value;
    bool        found = false;
    int         rc = PLPGSQL_RC_OK;
 
    var = (PLpgSQL_var *) (estate->datums[stmt->var->dno]);
 
    /*
     * Get the value of the lower bound
     */
    value = exec_eval_expr(estate, stmt->lower,
                           &isnull, &valtype, &valtypmod);
    value = exec_cast_value(estate, value, &isnull,
                            valtype, valtypmod,
                            var->datatype->typoid,
                            var->datatype->atttypmod);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("lower bound of FOR loop cannot be null")));
    loop_value = DatumGetInt32(value);
    exec_eval_cleanup(estate);
 
    /*
     * Get the value of the upper bound
     */
    value = exec_eval_expr(estate, stmt->upper,
                           &isnull, &valtype, &valtypmod);
    value = exec_cast_value(estate, value, &isnull,
                            valtype, valtypmod,
                            var->datatype->typoid,
                            var->datatype->atttypmod);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("upper bound of FOR loop cannot be null")));
    end_value = DatumGetInt32(value);
    exec_eval_cleanup(estate);
 
    /*
     * Get the step value
     */
    if (stmt->step)
    {
        value = exec_eval_expr(estate, stmt->step,
                               &isnull, &valtype, &valtypmod);
        value = exec_cast_value(estate, value, &isnull,
                                valtype, valtypmod,
                                var->datatype->typoid,
                                var->datatype->atttypmod);
        if (isnull)
            ereport(ERROR,
                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                     errmsg("BY value of FOR loop cannot be null")));
        step_value = DatumGetInt32(value);
        exec_eval_cleanup(estate);
        if (step_value <= 0)
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("BY value of FOR loop must be greater than zero")));
    }
    else
        step_value = 1;
 
    /*
     * Now do the loop
     */
    for (;;)
    {
        /*
         * Check against upper bound
         */
        if (stmt->reverse)
        {
            if (loop_value < end_value)
                break;
        }
        else
        {
            if (loop_value > end_value)
                break;
        }
 
        found = true;           /* looped at least once */
 
        /*
         * Assign current value to loop var
         */
        assign_simple_var(estate, var, Int32GetDatum(loop_value), false, false);
 
        /*
         * Execute the statements
         */
        rc = exec_stmts(estate, stmt->body);
 
        LOOP_RC_PROCESSING(stmt->label, break);
 
        /*
         * Increase/decrease loop value, unless it would overflow, in which
         * case exit the loop.
         */
        if (stmt->reverse)
        {
            if (loop_value < (PG_INT32_MIN + step_value))
                break;
            loop_value -= step_value;
        }
        else
        {
            if (loop_value > (PG_INT32_MAX - step_value))
                break;
            loop_value += step_value;
        }
    }
 
    /*
     * Set the FOUND variable to indicate the result of executing the loop
     * (namely, whether we looped one or more times). This must be set here so
     * that it does not interfere with the value of the FOUND variable inside
     * the loop processing itself.
     */
    exec_set_found(estate, found);
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_fors           Execute a query, assign each
 *                  tuple to a record or row and
 *                  execute a group of statements
 *                  for it.
 * ----------
 */
static int
exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt)
{
    Portal      portal;
    int         rc;
 
    /*
     * Open the implicit cursor for the statement using exec_run_select
     */
    exec_run_select(estate, stmt->query, 0, &portal);
 
    /*
     * Execute the loop
     */
    rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);
 
    /*
     * Close the implicit cursor
     */
    SPI_cursor_close(portal);
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_forc           Execute a loop for each row from a cursor.
 * ----------
 */
static int
exec_stmt_forc(PLpgSQL_execstate *estate, PLpgSQL_stmt_forc *stmt)
{
    PLpgSQL_var *curvar;
    MemoryContext stmt_mcontext = NULL;
    char       *curname = NULL;
    PLpgSQL_expr *query;
    ParamListInfo paramLI;
    Portal      portal;
    int         rc;
 
    /* ----------
     * Get the cursor variable and if it has an assigned name, check
     * that it's not in use currently.
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (!curvar->isnull)
    {
        MemoryContext oldcontext;
 
        /* We only need stmt_mcontext to hold the cursor name string */
        stmt_mcontext = get_stmt_mcontext(estate);
        oldcontext = MemoryContextSwitchTo(stmt_mcontext);
        curname = TextDatumGetCString(curvar->value);
        MemoryContextSwitchTo(oldcontext);
 
        if (SPI_cursor_find(curname) != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_DUPLICATE_CURSOR),
                     errmsg("cursor \"%s\" already in use", curname)));
    }
 
    /* ----------
     * Open the cursor just like an OPEN command
     *
     * Note: parser should already have checked that statement supplies
     * args iff cursor needs them, but we check again to be safe.
     * ----------
     */
    if (stmt->argquery != NULL)
    {
        /* ----------
         * OPEN CURSOR with args.  We fake a SELECT ... INTO ...
         * statement to evaluate the args and put 'em into the
         * internal row.
         * ----------
         */
        PLpgSQL_stmt_execsql set_args;
 
        if (curvar->cursor_explicit_argrow < 0)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("arguments given for cursor without arguments")));
 
        memset(&set_args, 0, sizeof(set_args));
        set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
        set_args.lineno = stmt->lineno;
        set_args.sqlstmt = stmt->argquery;
        set_args.into = true;
        /* XXX historically this has not been STRICT */
        set_args.target = (PLpgSQL_variable *)
            (estate->datums[curvar->cursor_explicit_argrow]);
 
        if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
            elog(ERROR, "open cursor failed during argument processing");
    }
    else
    {
        if (curvar->cursor_explicit_argrow >= 0)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("arguments required for cursor")));
    }
 
    query = curvar->cursor_explicit_expr;
    Assert(query);
 
    if (query->plan == NULL)
        exec_prepare_plan(estate, query, curvar->cursor_options);
 
    /*
     * Set up ParamListInfo for this query
     */
    paramLI = setup_param_list(estate, query);
 
    /*
     * Open the cursor (the paramlist will get copied into the portal)
     */
    portal = SPI_cursor_open_with_paramlist(curname, query->plan,
                                            paramLI,
                                            estate->readonly_func);
    if (portal == NULL)
        elog(ERROR, "could not open cursor: %s",
             SPI_result_code_string(SPI_result));
 
    /*
     * If cursor variable was NULL, store the generated portal name in it,
     * after verifying it's okay to assign to.
     */
    if (curname == NULL)
    {
        exec_check_assignable(estate, stmt->curvar);
        assign_text_var(estate, curvar, portal->name);
    }
 
    /*
     * Clean up before entering exec_for_query
     */
    exec_eval_cleanup(estate);
    if (stmt_mcontext)
        MemoryContextReset(stmt_mcontext);
 
    /*
     * Execute the loop.  We can't prefetch because the cursor is accessible
     * to the user, for instance via UPDATE WHERE CURRENT OF within the loop.
     */
    rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, false);
 
    /* ----------
     * Close portal, and restore cursor variable if it was initially NULL.
     * ----------
     */
    SPI_cursor_close(portal);
 
    if (curname == NULL)
        assign_simple_var(estate, curvar, (Datum) 0, true, false);
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_foreach_a          Loop over elements or slices of an array
 *
 * When looping over elements, the loop variable is the same type that the
 * array stores (eg: integer), when looping through slices, the loop variable
 * is an array of size and dimensions to match the size of the slice.
 * ----------
 */
static int
exec_stmt_foreach_a(PLpgSQL_execstate *estate, PLpgSQL_stmt_foreach_a *stmt)
{
    ArrayType  *arr;
    Oid         arrtype;
    int32       arrtypmod;
    PLpgSQL_datum *loop_var;
    Oid         loop_var_elem_type;
    bool        found = false;
    int         rc = PLPGSQL_RC_OK;
    MemoryContext stmt_mcontext;
    MemoryContext oldcontext;
    ArrayIterator array_iterator;
    Oid         iterator_result_type;
    int32       iterator_result_typmod;
    Datum       value;
    bool        isnull;
 
    /* get the value of the array expression */
    value = exec_eval_expr(estate, stmt->expr, &isnull, &arrtype, &arrtypmod);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("FOREACH expression must not be null")));
 
    /*
     * Do as much as possible of the code below in stmt_mcontext, to avoid any
     * leaks from called subroutines.  We need a private stmt_mcontext since
     * we'll be calling arbitrary statement code.
     */
    stmt_mcontext = get_stmt_mcontext(estate);
    push_stmt_mcontext(estate);
    oldcontext = MemoryContextSwitchTo(stmt_mcontext);
 
    /* check the type of the expression - must be an array */
    if (!OidIsValid(get_element_type(arrtype)))
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("FOREACH expression must yield an array, not type %s",
                        format_type_be(arrtype))));
 
    /*
     * We must copy the array into stmt_mcontext, else it will disappear in
     * exec_eval_cleanup.  This is annoying, but cleanup will certainly happen
     * while running the loop body, so we have little choice.
     */
    arr = DatumGetArrayTypePCopy(value);
 
    /* Clean up any leftover temporary memory */
    exec_eval_cleanup(estate);
 
    /* Slice dimension must be less than or equal to array dimension */
    if (stmt->slice < 0 || stmt->slice > ARR_NDIM(arr))
        ereport(ERROR,
                (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
                 errmsg("slice dimension (%d) is out of the valid range 0..%d",
                        stmt->slice, ARR_NDIM(arr))));
 
    /* Set up the loop variable and see if it is of an array type */
    loop_var = estate->datums[stmt->varno];
    if (loop_var->dtype == PLPGSQL_DTYPE_REC ||
        loop_var->dtype == PLPGSQL_DTYPE_ROW)
    {
        /*
         * Record/row variable is certainly not of array type, and might not
         * be initialized at all yet, so don't try to get its type
         */
        loop_var_elem_type = InvalidOid;
    }
    else
        loop_var_elem_type = get_element_type(plpgsql_exec_get_datum_type(estate,
                                                                          loop_var));
 
    /*
     * Sanity-check the loop variable type.  We don't try very hard here, and
     * should not be too picky since it's possible that exec_assign_value can
     * coerce values of different types.  But it seems worthwhile to complain
     * if the array-ness of the loop variable is not right.
     */
    if (stmt->slice > 0 && loop_var_elem_type == InvalidOid)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("FOREACH ... SLICE loop variable must be of an array type")));
    if (stmt->slice == 0 && loop_var_elem_type != InvalidOid)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("FOREACH loop variable must not be of an array type")));
 
    /* Create an iterator to step through the array */
    array_iterator = array_create_iterator(arr, stmt->slice, NULL);
 
    /* Identify iterator result type */
    if (stmt->slice > 0)
    {
        /* When slicing, nominal type of result is same as array type */
        iterator_result_type = arrtype;
        iterator_result_typmod = arrtypmod;
    }
    else
    {
        /* Without slicing, results are individual array elements */
        iterator_result_type = ARR_ELEMTYPE(arr);
        iterator_result_typmod = arrtypmod;
    }
 
    /* Iterate over the array elements or slices */
    while (array_iterate(array_iterator, &value, &isnull))
    {
        found = true;           /* looped at least once */
 
        /* exec_assign_value and exec_stmts must run in the main context */
        MemoryContextSwitchTo(oldcontext);
 
        /* Assign current element/slice to the loop variable */
        exec_assign_value(estate, loop_var, value, isnull,
                          iterator_result_type, iterator_result_typmod);
 
        /* In slice case, value is temporary; must free it to avoid leakage */
        if (stmt->slice > 0)
            pfree(DatumGetPointer(value));
 
        /*
         * Execute the statements
         */
        rc = exec_stmts(estate, stmt->body);
 
        LOOP_RC_PROCESSING(stmt->label, break);
 
        MemoryContextSwitchTo(stmt_mcontext);
    }
 
    /* Restore memory context state */
    MemoryContextSwitchTo(oldcontext);
    pop_stmt_mcontext(estate);
 
    /* Release temporary memory, including the array value */
    MemoryContextReset(stmt_mcontext);
 
    /*
     * Set the FOUND variable to indicate the result of executing the loop
     * (namely, whether we looped one or more times). This must be set here so
     * that it does not interfere with the value of the FOUND variable inside
     * the loop processing itself.
     */
    exec_set_found(estate, found);
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_exit           Implements EXIT and CONTINUE
 *
 * This begins the process of exiting / restarting a loop.
 * ----------
 */
static int
exec_stmt_exit(PLpgSQL_execstate *estate, PLpgSQL_stmt_exit *stmt)
{
    /*
     * If the exit / continue has a condition, evaluate it
     */
    if (stmt->cond != NULL)
    {
        bool        value;
        bool        isnull;
 
        value = exec_eval_boolean(estate, stmt->cond, &isnull);
        exec_eval_cleanup(estate);
        if (isnull || value == false)
            return PLPGSQL_RC_OK;
    }
 
    estate->exitlabel = stmt->label;
    if (stmt->is_exit)
        return PLPGSQL_RC_EXIT;
    else
        return PLPGSQL_RC_CONTINUE;
}
 
 
/* ----------
 * exec_stmt_return         Evaluate an expression and start
 *                  returning from the function.
 *
 * Note: The result may be in the eval_mcontext.  Therefore, we must not
 * do exec_eval_cleanup while unwinding the control stack.
 * ----------
 */
static int
exec_stmt_return(PLpgSQL_execstate *estate, PLpgSQL_stmt_return *stmt)
{
    /*
     * If processing a set-returning PL/pgSQL function, the final RETURN
     * indicates that the function is finished producing tuples.  The rest of
     * the work will be done at the top level.
     */
    if (estate->retisset)
        return PLPGSQL_RC_RETURN;
 
    /* initialize for null result */
    estate->retval = (Datum) 0;
    estate->retisnull = true;
    estate->rettype = InvalidOid;
 
    /*
     * Special case path when the RETURN expression is a simple variable
     * reference; in particular, this path is always taken in functions with
     * one or more OUT parameters.
     *
     * This special case is especially efficient for returning variables that
     * have R/W expanded values: we can put the R/W pointer directly into
     * estate->retval, leading to transferring the value to the caller's
     * context cheaply.  If we went through exec_eval_expr we'd end up with a
     * R/O pointer.  It's okay to skip MakeExpandedObjectReadOnly here since
     * we know we won't need the variable's value within the function anymore.
     */
    if (stmt->retvarno >= 0)
    {
        PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
 
        switch (retvar->dtype)
        {
            case PLPGSQL_DTYPE_PROMISE:
                /* fulfill promise if needed, then handle like regular var */
                plpgsql_fulfill_promise(estate, (PLpgSQL_var *) retvar);
 
                pg_fallthrough;
 
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) retvar;
 
                    estate->retval = var->value;
                    estate->retisnull = var->isnull;
                    estate->rettype = var->datatype->typoid;
 
                    /*
                     * A PLpgSQL_var could not be of composite type, so
                     * conversion must fail if retistuple.  We throw a custom
                     * error mainly for consistency with historical behavior.
                     * For the same reason, we don't throw error if the result
                     * is NULL.  (Note that plpgsql_exec_trigger assumes that
                     * any non-null result has been verified to be composite.)
                     */
                    if (estate->retistuple && !estate->retisnull)
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("cannot return non-composite value from function returning composite type")));
                }
                break;
 
            case PLPGSQL_DTYPE_ROW:
            case PLPGSQL_DTYPE_REC:
                {
                    /* exec_eval_datum can handle these cases */
                    int32       rettypmod;
 
                    exec_eval_datum(estate,
                                    retvar,
                                    &estate->rettype,
                                    &rettypmod,
                                    &estate->retval,
                                    &estate->retisnull);
                }
                break;
 
            default:
                elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
        }
 
        return PLPGSQL_RC_RETURN;
    }
 
    if (stmt->expr != NULL)
    {
        int32       rettypmod;
 
        estate->retval = exec_eval_expr(estate, stmt->expr,
                                        &(estate->retisnull),
                                        &(estate->rettype),
                                        &rettypmod);
 
        /*
         * As in the DTYPE_VAR case above, throw a custom error if a non-null,
         * non-composite value is returned in a function returning tuple.
         */
        if (estate->retistuple && !estate->retisnull &&
            !type_is_rowtype(estate->rettype))
            ereport(ERROR,
                    (errcode(ERRCODE_DATATYPE_MISMATCH),
                     errmsg("cannot return non-composite value from function returning composite type")));
 
        return PLPGSQL_RC_RETURN;
    }
 
    /*
     * Special hack for function returning VOID: instead of NULL, return a
     * non-null VOID value.  This is of dubious importance but is kept for
     * backwards compatibility.  We don't do it for procedures, though.
     */
    if (estate->fn_rettype == VOIDOID &&
        estate->func->fn_prokind != PROKIND_PROCEDURE)
    {
        estate->retval = (Datum) 0;
        estate->retisnull = false;
        estate->rettype = VOIDOID;
    }
 
    return PLPGSQL_RC_RETURN;
}
 
/* ----------
 * exec_stmt_return_next        Evaluate an expression and add it to the
 *                              list of tuples returned by the current
 *                              SRF.
 * ----------
 */
static int
exec_stmt_return_next(PLpgSQL_execstate *estate,
                      PLpgSQL_stmt_return_next *stmt)
{
    TupleDesc   tupdesc;
    int         natts;
    HeapTuple   tuple;
    MemoryContext oldcontext;
 
    if (!estate->retisset)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("cannot use RETURN NEXT in a non-SETOF function")));
 
    if (estate->tuple_store == NULL)
        exec_init_tuple_store(estate);
 
    /* tuple_store_desc will be filled by exec_init_tuple_store */
    tupdesc = estate->tuple_store_desc;
    natts = tupdesc->natts;
 
    /*
     * Special case path when the RETURN NEXT expression is a simple variable
     * reference; in particular, this path is always taken in functions with
     * one or more OUT parameters.
     *
     * Unlike exec_stmt_return, there's no special win here for R/W expanded
     * values, since they'll have to get flattened to go into the tuplestore.
     * Indeed, we'd better make them R/O to avoid any risk of the casting step
     * changing them in-place.
     */
    if (stmt->retvarno >= 0)
    {
        PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
 
        switch (retvar->dtype)
        {
            case PLPGSQL_DTYPE_PROMISE:
                /* fulfill promise if needed, then handle like regular var */
                plpgsql_fulfill_promise(estate, (PLpgSQL_var *) retvar);
 
                pg_fallthrough;
 
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) retvar;
                    Datum       retval = var->value;
                    bool        isNull = var->isnull;
                    Form_pg_attribute attr;
 
                    if (natts != 1)
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("wrong result type supplied in RETURN NEXT")));
 
                    /* let's be very paranoid about the cast step */
                    retval = MakeExpandedObjectReadOnly(retval,
                                                        isNull,
                                                        var->datatype->typlen);
 
                    /* coerce type if needed */
                    attr = TupleDescAttr(tupdesc, 0);
                    retval = exec_cast_value(estate,
                                             retval,
                                             &isNull,
                                             var->datatype->typoid,
                                             var->datatype->atttypmod,
                                             attr->atttypid,
                                             attr->atttypmod);
 
                    tuplestore_putvalues(estate->tuple_store, tupdesc,
                                         &retval, &isNull);
                }
                break;
 
            case PLPGSQL_DTYPE_REC:
                {
                    PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
                    TupleDesc   rec_tupdesc;
                    TupleConversionMap *tupmap;
 
                    /* If rec is null, try to convert it to a row of nulls */
                    if (rec->erh == NULL)
                        instantiate_empty_record_variable(estate, rec);
                    if (ExpandedRecordIsEmpty(rec->erh))
                        deconstruct_expanded_record(rec->erh);
 
                    /* Use eval_mcontext for tuple conversion work */
                    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
                    rec_tupdesc = expanded_record_get_tupdesc(rec->erh);
                    tupmap = convert_tuples_by_position(rec_tupdesc,
                                                        tupdesc,
                                                        gettext_noop("wrong record type supplied in RETURN NEXT"));
                    tuple = expanded_record_get_tuple(rec->erh);
                    if (tupmap)
                        tuple = execute_attr_map_tuple(tuple, tupmap);
                    else
                    {
                        /*
                         * Expanded records should always have valid contents,
                         * so we don't need a version check here.
                         */
                    }
                    tuplestore_puttuple(estate->tuple_store, tuple);
                    MemoryContextSwitchTo(oldcontext);
                }
                break;
 
            case PLPGSQL_DTYPE_ROW:
                {
                    PLpgSQL_row *row = (PLpgSQL_row *) retvar;
 
                    /* We get here if there are multiple OUT parameters */
 
                    /* Use eval_mcontext for tuple conversion work */
                    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
                    tuple = make_tuple_from_row(estate, row, tupdesc);
                    if (tuple == NULL)  /* should not happen */
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("wrong record type supplied in RETURN NEXT")));
                    tuplestore_puttuple(estate->tuple_store, tuple);
                    MemoryContextSwitchTo(oldcontext);
                }
                break;
 
            default:
                elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
                break;
        }
    }
    else if (stmt->expr)
    {
        Datum       retval;
        bool        isNull;
        Oid         rettype;
        int32       rettypmod;
 
        retval = exec_eval_expr(estate,
                                stmt->expr,
                                &isNull,
                                &rettype,
                                &rettypmod);
 
        if (estate->retistuple)
        {
            /* Expression should be of RECORD or composite type */
            if (!isNull)
            {
                HeapTupleData tmptup;
                TupleDesc   retvaldesc;
                TupleConversionMap *tupmap;
 
                if (!type_is_rowtype(rettype))
                    ereport(ERROR,
                            (errcode(ERRCODE_DATATYPE_MISMATCH),
                             errmsg("cannot return non-composite value from function returning composite type")));
 
                /* Use eval_mcontext for tuple conversion work */
                oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
                retvaldesc = deconstruct_composite_datum(retval, &tmptup);
                tuple = &tmptup;
                tupmap = convert_tuples_by_position(retvaldesc, tupdesc,
                                                    gettext_noop("returned record type does not match expected record type"));
                if (tupmap)
                    tuple = execute_attr_map_tuple(tuple, tupmap);
                else
                {
                    /*
                     * No need to check the version here, due to
                     * deconstruct_composite_datum() already having checked.
                     */
                }
 
                tuplestore_puttuple(estate->tuple_store, tuple);
                ReleaseTupleDesc(retvaldesc);
                MemoryContextSwitchTo(oldcontext);
            }
            else
            {
                /* Composite NULL --- store a row of nulls */
                Datum      *nulldatums;
                bool       *nullflags;
 
                nulldatums = (Datum *)
                    eval_mcontext_alloc0(estate, natts * sizeof(Datum));
                nullflags = (bool *)
                    eval_mcontext_alloc(estate, natts * sizeof(bool));
                memset(nullflags, true, natts * sizeof(bool));
                tuplestore_putvalues(estate->tuple_store, tupdesc,
                                     nulldatums, nullflags);
            }
        }
        else
        {
            Form_pg_attribute attr;
 
            /* Simple scalar result */
            if (natts != 1)
                ereport(ERROR,
                        (errcode(ERRCODE_DATATYPE_MISMATCH),
                         errmsg("wrong result type supplied in RETURN NEXT")));
 
            /* coerce type if needed */
            attr = TupleDescAttr(tupdesc, 0);
            retval = exec_cast_value(estate,
                                     retval,
                                     &isNull,
                                     rettype,
                                     rettypmod,
                                     attr->atttypid,
                                     attr->atttypmod);
 
            tuplestore_putvalues(estate->tuple_store, tupdesc,
                                 &retval, &isNull);
        }
    }
    else
    {
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("RETURN NEXT must have a parameter")));
    }
 
    exec_eval_cleanup(estate);
 
    return PLPGSQL_RC_OK;
}
 
/* ----------
 * exec_stmt_return_query       Evaluate a query and add it to the
 *                              list of tuples returned by the current
 *                              SRF.
 * ----------
 */
static int
exec_stmt_return_query(PLpgSQL_execstate *estate,
                       PLpgSQL_stmt_return_query *stmt)
{
    int64       tcount;
    DestReceiver *treceiver;
    int         rc;
    uint64      processed;
    MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
    MemoryContext oldcontext;
 
    if (!estate->retisset)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("cannot use RETURN QUERY in a non-SETOF function")));
 
    if (estate->tuple_store == NULL)
        exec_init_tuple_store(estate);
    /* There might be some tuples in the tuplestore already */
    tcount = tuplestore_tuple_count(estate->tuple_store);
 
    /*
     * Set up DestReceiver to transfer results directly to tuplestore,
     * converting rowtype if necessary.  DestReceiver lives in mcontext.
     */
    oldcontext = MemoryContextSwitchTo(stmt_mcontext);
    treceiver = CreateDestReceiver(DestTuplestore);
    SetTuplestoreDestReceiverParams(treceiver,
                                    estate->tuple_store,
                                    estate->tuple_store_cxt,
                                    false,
                                    estate->tuple_store_desc,
                                    gettext_noop("structure of query does not match function result type"));
    MemoryContextSwitchTo(oldcontext);
 
    if (stmt->query != NULL)
    {
        /* static query */
        PLpgSQL_expr *expr = stmt->query;
        ParamListInfo paramLI;
        SPIExecuteOptions options;
 
        /*
         * On the first call for this expression generate the plan.
         */
        if (expr->plan == NULL)
            exec_prepare_plan(estate, expr, CURSOR_OPT_PARALLEL_OK);
 
        /*
         * Set up ParamListInfo to pass to executor
         */
        paramLI = setup_param_list(estate, expr);
 
        /*
         * Execute the query
         */
        memset(&options, 0, sizeof(options));
        options.params = paramLI;
        options.read_only = estate->readonly_func;
        options.must_return_tuples = true;
        options.dest = treceiver;
 
        rc = SPI_execute_plan_extended(expr->plan, &options);
        if (rc < 0)
            elog(ERROR, "SPI_execute_plan_extended failed executing query \"%s\": %s",
                 expr->query, SPI_result_code_string(rc));
    }
    else
    {
        /* RETURN QUERY EXECUTE */
        Datum       query;
        bool        isnull;
        Oid         restype;
        int32       restypmod;
        char       *querystr;
        SPIExecuteOptions options;
 
        /*
         * Evaluate the string expression after the EXECUTE keyword. Its
         * result is the querystring we have to execute.
         */
        Assert(stmt->dynquery != NULL);
        query = exec_eval_expr(estate, stmt->dynquery,
                               &isnull, &restype, &restypmod);
        if (isnull)
            ereport(ERROR,
                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                     errmsg("query string argument of EXECUTE is null")));
 
        /* Get the C-String representation */
        querystr = convert_value_to_string(estate, query, restype);
 
        /* copy it into the stmt_mcontext before we clean up */
        querystr = MemoryContextStrdup(stmt_mcontext, querystr);
 
        exec_eval_cleanup(estate);
 
        /* Execute query, passing params if necessary */
        memset(&options, 0, sizeof(options));
        options.params = exec_eval_using_params(estate,
                                                stmt->params);
        options.read_only = estate->readonly_func;
        options.must_return_tuples = true;
        options.dest = treceiver;
 
        rc = SPI_execute_extended(querystr, &options);
        if (rc < 0)
            elog(ERROR, "SPI_execute_extended failed executing query \"%s\": %s",
                 querystr, SPI_result_code_string(rc));
    }
 
    /* Clean up */
    treceiver->rDestroy(treceiver);
    exec_eval_cleanup(estate);
    MemoryContextReset(stmt_mcontext);
 
    /* Count how many tuples we got */
    processed = tuplestore_tuple_count(estate->tuple_store) - tcount;
 
    estate->eval_processed = processed;
    exec_set_found(estate, processed != 0);
 
    return PLPGSQL_RC_OK;
}
 
static void
exec_init_tuple_store(PLpgSQL_execstate *estate)
{
    ReturnSetInfo *rsi = estate->rsi;
    MemoryContext oldcxt;
    ResourceOwner oldowner;
 
    /*
     * Check caller can handle a set result in the way we want
     */
    if (!rsi || !IsA(rsi, ReturnSetInfo))
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("set-valued function called in context that cannot accept a set")));
 
    if (!(rsi->allowedModes & SFRM_Materialize) ||
        rsi->expectedDesc == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("materialize mode required, but it is not allowed in this context")));
 
    /*
     * Switch to the right memory context and resource owner for storing the
     * tuplestore for return set. If we're within a subtransaction opened for
     * an exception-block, for example, we must still create the tuplestore in
     * the resource owner that was active when this function was entered, and
     * not in the subtransaction resource owner.
     */
    oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
    oldowner = CurrentResourceOwner;
    CurrentResourceOwner = estate->tuple_store_owner;
 
    estate->tuple_store =
        tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
                              false, work_mem);
 
    CurrentResourceOwner = oldowner;
    MemoryContextSwitchTo(oldcxt);
 
    estate->tuple_store_desc = rsi->expectedDesc;
}
 
#define SET_RAISE_OPTION_TEXT(opt, name) \
do { \
    if (opt) \
        ereport(ERROR, \
                (errcode(ERRCODE_SYNTAX_ERROR), \
                 errmsg("RAISE option already specified: %s", \
                        name))); \
    opt = MemoryContextStrdup(stmt_mcontext, extval); \
} while (0)
 
/* ----------
 * exec_stmt_raise          Build a message and throw it with elog()
 * ----------
 */
static int
exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
{
    int         err_code = 0;
    char       *condname = NULL;
    char       *err_message = NULL;
    char       *err_detail = NULL;
    char       *err_hint = NULL;
    char       *err_column = NULL;
    char       *err_constraint = NULL;
    char       *err_datatype = NULL;
    char       *err_table = NULL;
    char       *err_schema = NULL;
    MemoryContext stmt_mcontext;
    ListCell   *lc;
 
    /* RAISE with no parameters: re-throw current exception */
    if (stmt->condname == NULL && stmt->message == NULL &&
        stmt->options == NIL)
    {
        if (estate->cur_error != NULL)
            ReThrowError(estate->cur_error);
        /* oops, we're not inside a handler */
        ereport(ERROR,
                (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
                 errmsg("RAISE without parameters cannot be used outside an exception handler")));
    }
 
    /* We'll need to accumulate the various strings in stmt_mcontext */
    stmt_mcontext = get_stmt_mcontext(estate);
 
    if (stmt->condname)
    {
        err_code = plpgsql_recognize_err_condition(stmt->condname, true);
        condname = MemoryContextStrdup(stmt_mcontext, stmt->condname);
    }
 
    if (stmt->message)
    {
        StringInfoData ds;
        ListCell   *current_param;
        char       *cp;
        MemoryContext oldcontext;
 
        /* build string in stmt_mcontext */
        oldcontext = MemoryContextSwitchTo(stmt_mcontext);
        initStringInfo(&ds);
        MemoryContextSwitchTo(oldcontext);
 
        current_param = list_head(stmt->params);
 
        for (cp = stmt->message; *cp; cp++)
        {
            /*
             * Occurrences of a single % are replaced by the next parameter's
             * external representation. Double %'s are converted to one %.
             */
            if (cp[0] == '%')
            {
                Oid         paramtypeid;
                int32       paramtypmod;
                Datum       paramvalue;
                bool        paramisnull;
                char       *extval;
 
                if (cp[1] == '%')
                {
                    appendStringInfoChar(&ds, '%');
                    cp++;
                    continue;
                }
 
                /* should have been checked at compile time */
                if (current_param == NULL)
                    elog(ERROR, "unexpected RAISE parameter list length");
 
                paramvalue = exec_eval_expr(estate,
                                            (PLpgSQL_expr *) lfirst(current_param),
                                            &paramisnull,
                                            &paramtypeid,
                                            &paramtypmod);
 
                if (paramisnull)
                    extval = "<NULL>";
                else
                    extval = convert_value_to_string(estate,
                                                     paramvalue,
                                                     paramtypeid);
                appendStringInfoString(&ds, extval);
                current_param = lnext(stmt->params, current_param);
                exec_eval_cleanup(estate);
            }
            else
                appendStringInfoChar(&ds, cp[0]);
        }
 
        /* should have been checked at compile time */
        if (current_param != NULL)
            elog(ERROR, "unexpected RAISE parameter list length");
 
        err_message = ds.data;
    }
 
    foreach(lc, stmt->options)
    {
        PLpgSQL_raise_option *opt = (PLpgSQL_raise_option *) lfirst(lc);
        Datum       optionvalue;
        bool        optionisnull;
        Oid         optiontypeid;
        int32       optiontypmod;
        char       *extval;
 
        optionvalue = exec_eval_expr(estate, opt->expr,
                                     &optionisnull,
                                     &optiontypeid,
                                     &optiontypmod);
        if (optionisnull)
            ereport(ERROR,
                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                     errmsg("RAISE statement option cannot be null")));
 
        extval = convert_value_to_string(estate, optionvalue, optiontypeid);
 
        switch (opt->opt_type)
        {
            case PLPGSQL_RAISEOPTION_ERRCODE:
                if (err_code)
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                             errmsg("RAISE option already specified: %s",
                                    "ERRCODE")));
                err_code = plpgsql_recognize_err_condition(extval, true);
                condname = MemoryContextStrdup(stmt_mcontext, extval);
                break;
            case PLPGSQL_RAISEOPTION_MESSAGE:
                SET_RAISE_OPTION_TEXT(err_message, "MESSAGE");
                break;
            case PLPGSQL_RAISEOPTION_DETAIL:
                SET_RAISE_OPTION_TEXT(err_detail, "DETAIL");
                break;
            case PLPGSQL_RAISEOPTION_HINT:
                SET_RAISE_OPTION_TEXT(err_hint, "HINT");
                break;
            case PLPGSQL_RAISEOPTION_COLUMN:
                SET_RAISE_OPTION_TEXT(err_column, "COLUMN");
                break;
            case PLPGSQL_RAISEOPTION_CONSTRAINT:
                SET_RAISE_OPTION_TEXT(err_constraint, "CONSTRAINT");
                break;
            case PLPGSQL_RAISEOPTION_DATATYPE:
                SET_RAISE_OPTION_TEXT(err_datatype, "DATATYPE");
                break;
            case PLPGSQL_RAISEOPTION_TABLE:
                SET_RAISE_OPTION_TEXT(err_table, "TABLE");
                break;
            case PLPGSQL_RAISEOPTION_SCHEMA:
                SET_RAISE_OPTION_TEXT(err_schema, "SCHEMA");
                break;
            default:
                elog(ERROR, "unrecognized raise option: %d", opt->opt_type);
        }
 
        exec_eval_cleanup(estate);
    }
 
    /* Default code if nothing specified */
    if (err_code == 0 && stmt->elog_level >= ERROR)
        err_code = ERRCODE_RAISE_EXCEPTION;
 
    /* Default error message if nothing specified */
    if (err_message == NULL)
    {
        if (condname)
        {
            err_message = condname;
            condname = NULL;
        }
        else
            err_message = MemoryContextStrdup(stmt_mcontext,
                                              unpack_sql_state(err_code));
    }
 
    /*
     * Throw the error (may or may not come back)
     */
    ereport(stmt->elog_level,
            (err_code ? errcode(err_code) : 0,
             errmsg_internal("%s", err_message),
             (err_detail != NULL) ? errdetail_internal("%s", err_detail) : 0,
             (err_hint != NULL) ? errhint("%s", err_hint) : 0,
             (err_column != NULL) ?
             err_generic_string(PG_DIAG_COLUMN_NAME, err_column) : 0,
             (err_constraint != NULL) ?
             err_generic_string(PG_DIAG_CONSTRAINT_NAME, err_constraint) : 0,
             (err_datatype != NULL) ?
             err_generic_string(PG_DIAG_DATATYPE_NAME, err_datatype) : 0,
             (err_table != NULL) ?
             err_generic_string(PG_DIAG_TABLE_NAME, err_table) : 0,
             (err_schema != NULL) ?
             err_generic_string(PG_DIAG_SCHEMA_NAME, err_schema) : 0));
 
    /* Clean up transient strings */
    MemoryContextReset(stmt_mcontext);
 
    return PLPGSQL_RC_OK;
}
 
/* ----------
 * exec_stmt_assert         Assert statement
 * ----------
 */
static int
exec_stmt_assert(PLpgSQL_execstate *estate, PLpgSQL_stmt_assert *stmt)
{
    bool        value;
    bool        isnull;
 
    /* do nothing when asserts are not enabled */
    if (!plpgsql_check_asserts)
        return PLPGSQL_RC_OK;
 
    value = exec_eval_boolean(estate, stmt->cond, &isnull);
    exec_eval_cleanup(estate);
 
    if (isnull || !value)
    {
        char       *message = NULL;
 
        if (stmt->message != NULL)
        {
            Datum       val;
            Oid         typeid;
            int32       typmod;
 
            val = exec_eval_expr(estate, stmt->message,
                                 &isnull, &typeid, &typmod);
            if (!isnull)
                message = convert_value_to_string(estate, val, typeid);
            /* we mustn't do exec_eval_cleanup here */
        }
 
        ereport(ERROR,
                (errcode(ERRCODE_ASSERT_FAILURE),
                 message ? errmsg_internal("%s", message) :
                 errmsg("assertion failed")));
    }
 
    return PLPGSQL_RC_OK;
}
 
/* ----------
 * Initialize a mostly empty execution state
 * ----------
 */
static void
plpgsql_estate_setup(PLpgSQL_execstate *estate,
                     PLpgSQL_function *func,
                     ReturnSetInfo *rsi,
                     EState *simple_eval_estate,
                     ResourceOwner simple_eval_resowner)
{
    HASHCTL     ctl;
 
    /* this link will be restored at exit from plpgsql_call_handler */
    func->cur_estate = estate;
 
    estate->func = func;
    estate->trigdata = NULL;
    estate->evtrigdata = NULL;
 
    estate->retval = (Datum) 0;
    estate->retisnull = true;
    estate->rettype = InvalidOid;
 
    estate->fn_rettype = func->fn_rettype;
    estate->retistuple = func->fn_retistuple;
    estate->retisset = func->fn_retset;
 
    estate->readonly_func = func->fn_readonly;
    estate->atomic = true;
 
    estate->exitlabel = NULL;
    estate->cur_error = NULL;
 
    estate->tuple_store = NULL;
    estate->tuple_store_desc = NULL;
    if (rsi)
    {
        estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
        estate->tuple_store_owner = CurrentResourceOwner;
    }
    else
    {
        estate->tuple_store_cxt = NULL;
        estate->tuple_store_owner = NULL;
    }
    estate->rsi = rsi;
 
    estate->found_varno = func->found_varno;
    estate->ndatums = func->ndatums;
    estate->datums = NULL;
    /* the datums array will be filled by copy_plpgsql_datums() */
    estate->datum_context = CurrentMemoryContext;
 
    /* initialize our ParamListInfo with appropriate hook functions */
    estate->paramLI = makeParamList(0);
    estate->paramLI->paramFetch = plpgsql_param_fetch;
    estate->paramLI->paramFetchArg = estate;
    estate->paramLI->paramCompile = plpgsql_param_compile;
    estate->paramLI->paramCompileArg = NULL;    /* not needed */
    estate->paramLI->parserSetup = (ParserSetupHook) plpgsql_parser_setup;
    estate->paramLI->parserSetupArg = NULL; /* filled during use */
    estate->paramLI->numParams = estate->ndatums;
 
    /* Create the session-wide cast-expression hash if we didn't already */
    if (cast_expr_hash == NULL)
    {
        ctl.keysize = sizeof(plpgsql_CastHashKey);
        ctl.entrysize = sizeof(plpgsql_CastExprHashEntry);
        cast_expr_hash = hash_create("PLpgSQL cast expressions",
                                     16,    /* start small and extend */
                                     &ctl,
                                     HASH_ELEM | HASH_BLOBS);
    }
 
    /* set up for use of appropriate simple-expression EState and cast hash */
    if (simple_eval_estate)
    {
        estate->simple_eval_estate = simple_eval_estate;
        /* Private cast hash just lives in function's main context */
        ctl.keysize = sizeof(plpgsql_CastHashKey);
        ctl.entrysize = sizeof(plpgsql_CastHashEntry);
        ctl.hcxt = CurrentMemoryContext;
        estate->cast_hash = hash_create("PLpgSQL private cast cache",
                                        16, /* start small and extend */
                                        &ctl,
                                        HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
    }
    else
    {
        estate->simple_eval_estate = shared_simple_eval_estate;
        /* Create the session-wide cast-info hash table if we didn't already */
        if (shared_cast_hash == NULL)
        {
            ctl.keysize = sizeof(plpgsql_CastHashKey);
            ctl.entrysize = sizeof(plpgsql_CastHashEntry);
            shared_cast_hash = hash_create("PLpgSQL cast cache",
                                           16,  /* start small and extend */
                                           &ctl,
                                           HASH_ELEM | HASH_BLOBS);
        }
        estate->cast_hash = shared_cast_hash;
    }
    /* likewise for the simple-expression resource owner */
    if (simple_eval_resowner)
        estate->simple_eval_resowner = simple_eval_resowner;
    else
        estate->simple_eval_resowner = shared_simple_eval_resowner;
 
    /* if there's a procedure resowner, it'll be filled in later */
    estate->procedure_resowner = NULL;
 
    /*
     * We start with no stmt_mcontext; one will be created only if needed.
     * That context will be a direct child of the function's main execution
     * context.  Additional stmt_mcontexts might be created as children of it.
     */
    estate->stmt_mcontext = NULL;
    estate->stmt_mcontext_parent = CurrentMemoryContext;
 
    estate->eval_tuptable = NULL;
    estate->eval_processed = 0;
    estate->eval_econtext = NULL;
 
    estate->err_stmt = NULL;
    estate->err_var = NULL;
    estate->err_text = NULL;
 
    estate->plugin_info = NULL;
 
    /*
     * Create an EState and ExprContext for evaluation of simple expressions.
     */
    plpgsql_create_econtext(estate);
 
    /*
     * Let the plugin, if any, see this function before we initialize local
     * PL/pgSQL variables.  Note that we also give the plugin a few function
     * pointers, so it can call back into PL/pgSQL for doing things like
     * variable assignments and stack traces.
     */
    if (*plpgsql_plugin_ptr)
    {
        (*plpgsql_plugin_ptr)->error_callback = plpgsql_exec_error_callback;
        (*plpgsql_plugin_ptr)->assign_expr = exec_assign_expr;
        (*plpgsql_plugin_ptr)->assign_value = exec_assign_value;
        (*plpgsql_plugin_ptr)->eval_datum = exec_eval_datum;
        (*plpgsql_plugin_ptr)->cast_value = exec_cast_value;
 
        if ((*plpgsql_plugin_ptr)->func_setup)
            ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
    }
}
 
/* ----------
 * Release temporary memory used by expression/subselect evaluation
 *
 * NB: the result of the evaluation is no longer valid after this is done,
 * unless it is a pass-by-value datatype.
 * ----------
 */
static void
exec_eval_cleanup(PLpgSQL_execstate *estate)
{
    /* Clear result of a full SPI_execute */
    if (estate->eval_tuptable != NULL)
        SPI_freetuptable(estate->eval_tuptable);
    estate->eval_tuptable = NULL;
 
    /*
     * Clear result of exec_eval_simple_expr (but keep the econtext).  This
     * also clears any short-lived allocations done via get_eval_mcontext.
     */
    if (estate->eval_econtext != NULL)
        ResetExprContext(estate->eval_econtext);
}
 
 
/* ----------
 * Generate a prepared plan
 *
 * CAUTION: it is possible for this function to throw an error after it has
 * built a SPIPlan and saved it in expr->plan.  Therefore, be wary of doing
 * additional things contingent on expr->plan being NULL.  That is, given
 * code like
 *
 *  if (query->plan == NULL)
 *  {
 *      // okay to put setup code here
 *      exec_prepare_plan(estate, query, ...);
 *      // NOT okay to put more logic here
 *  }
 *
 * extra steps at the end are unsafe because they will not be executed when
 * re-executing the calling statement, if exec_prepare_plan failed the first
 * time.  This is annoyingly error-prone, but the alternatives are worse.
 * ----------
 */
static void
exec_prepare_plan(PLpgSQL_execstate *estate,
                  PLpgSQL_expr *expr, int cursorOptions)
{
    SPIPlanPtr  plan;
    SPIPrepareOptions options;
 
    /*
     * Generate and save the plan
     */
    memset(&options, 0, sizeof(options));
    options.parserSetup = (ParserSetupHook) plpgsql_parser_setup;
    options.parserSetupArg = expr;
    options.parseMode = expr->parseMode;
    options.cursorOptions = cursorOptions;
    plan = SPI_prepare_extended(expr->query, &options);
    if (plan == NULL)
        elog(ERROR, "SPI_prepare_extended failed for \"%s\": %s",
             expr->query, SPI_result_code_string(SPI_result));
 
    SPI_keepplan(plan);
    expr->plan = plan;
 
    /* Check to see if it's a simple expression */
    exec_simple_check_plan(estate, expr);
}
 
 
/* ----------
 * exec_stmt_execsql            Execute an SQL statement (possibly with INTO).
 *
 * Note: some callers rely on this not touching stmt_mcontext.  If it ever
 * needs to use that, fix those callers to push/pop stmt_mcontext.
 * ----------
 */
static int
exec_stmt_execsql(PLpgSQL_execstate *estate,
                  PLpgSQL_stmt_execsql *stmt)
{
    ParamListInfo paramLI;
    long        tcount;
    int         rc;
    PLpgSQL_expr *expr = stmt->sqlstmt;
    int         too_many_rows_level = 0;
 
    if (plpgsql_extra_errors & PLPGSQL_XCHECK_TOOMANYROWS)
        too_many_rows_level = ERROR;
    else if (plpgsql_extra_warnings & PLPGSQL_XCHECK_TOOMANYROWS)
        too_many_rows_level = WARNING;
 
    /*
     * On the first call for this statement generate the plan, and detect
     * whether the statement is INSERT/UPDATE/DELETE/MERGE
     */
    if (expr->plan == NULL)
        exec_prepare_plan(estate, expr, CURSOR_OPT_PARALLEL_OK);
 
    if (!stmt->mod_stmt_set)
    {
        ListCell   *l;
 
        stmt->mod_stmt = false;
        foreach(l, SPI_plan_get_plan_sources(expr->plan))
        {
            CachedPlanSource *plansource = (CachedPlanSource *) lfirst(l);
 
            /*
             * We could look at the raw_parse_tree, but it seems simpler to
             * check the command tag.  Note we should *not* look at the Query
             * tree(s), since those are the result of rewriting and could be
             * stale, or could have been transmogrified into something else
             * entirely.
             */
            if (plansource->commandTag == CMDTAG_INSERT ||
                plansource->commandTag == CMDTAG_UPDATE ||
                plansource->commandTag == CMDTAG_DELETE ||
                plansource->commandTag == CMDTAG_MERGE)
            {
                stmt->mod_stmt = true;
                break;
            }
        }
        stmt->mod_stmt_set = true;
    }
 
    /*
     * Some users write "SELECT expr INTO var" instead of "var := expr".  If
     * the expression is simple and the INTO target is a single variable, we
     * can bypass SPI and call ExecEvalExpr() directly.  (exec_eval_expr would
     * actually work for non-simple expressions too, but such an expression
     * might return more or less than one row, complicating matters greatly.
     * The potential performance win is small if it's non-simple, and any
     * errors we might issue would likely look different, so avoid using this
     * code path for non-simple cases.)
     */
    if (expr->expr_simple_expr && stmt->into)
    {
        PLpgSQL_datum *target = estate->datums[stmt->target->dno];
 
        if (target->dtype == PLPGSQL_DTYPE_ROW)
        {
            PLpgSQL_row *row = (PLpgSQL_row *) target;
 
            if (row->nfields == 1)
            {
                /* OK, there is a single target variable */
                ErrorContextCallback plerrcontext;
                Datum       value;
                bool        isnull;
                Oid         valtype;
                int32       valtypmod;
                bool        simple;
 
                /*
                 * Setup error traceback support for ereport().  This is so
                 * that error reports for the expression will look similar
                 * whether or not we take this code path.
                 */
                plerrcontext.callback = plpgsql_execsql_error_callback;
                plerrcontext.arg = expr;
                plerrcontext.previous = error_context_stack;
                error_context_stack = &plerrcontext;
 
                /*
                 * Evaluate the already-planned expression.  This fails if the
                 * expression is busy, or if replanning occurs and we find it
                 * is no longer simple.  In such cases, fall through to the
                 * SPI code below.
                 */
                simple = exec_eval_simple_expr(estate, expr,
                                               &value, &isnull,
                                               &valtype, &valtypmod);
 
                /*
                 * Pop the error context stack: the code below would not use
                 * SPI's error handling during the assignment step.
                 */
                error_context_stack = plerrcontext.previous;
 
                if (simple)
                {
                    /* Success; assign the result to the INTO target */
                    exec_assign_value(estate, estate->datums[row->varnos[0]],
                                      value, isnull, valtype, valtypmod);
                    exec_eval_cleanup(estate);
 
                    /*
                     * We must duplicate the other effects of the code below,
                     * as well.  We know that exactly one row was returned, so
                     * it doesn't matter whether the INTO was STRICT or not.
                     */
                    exec_set_found(estate, true);
                    estate->eval_processed = 1;
 
                    return PLPGSQL_RC_OK;
                }
            }
        }
    }
 
    /*
     * Set up ParamListInfo to pass to executor
     */
    paramLI = setup_param_list(estate, expr);
 
    /*
     * If we have INTO, then we only need one row back ... but if we have INTO
     * STRICT or extra check too_many_rows, ask for two rows, so that we can
     * verify the statement returns only one.  INSERT/UPDATE/DELETE/MERGE are
     * always treated strictly. Without INTO, just run the statement to
     * completion (tcount = 0).
     *
     * We could just ask for two rows always when using INTO, but there are
     * some cases where demanding the extra row costs significant time, eg by
     * forcing completion of a sequential scan.  So don't do it unless we need
     * to enforce strictness.
     */
    if (stmt->into)
    {
        if (stmt->strict || stmt->mod_stmt || too_many_rows_level)
            tcount = 2;
        else
            tcount = 1;
    }
    else
        tcount = 0;
 
    /*
     * Execute the plan
     */
    rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
                                         estate->readonly_func, tcount);
 
    /*
     * Check for error, and set FOUND if appropriate (for historical reasons
     * we set FOUND only for certain query types).  Also Assert that we
     * identified the statement type the same as SPI did.
     */
    switch (rc)
    {
        case SPI_OK_SELECT:
            Assert(!stmt->mod_stmt);
            exec_set_found(estate, (SPI_processed != 0));
            break;
 
        case SPI_OK_INSERT:
        case SPI_OK_UPDATE:
        case SPI_OK_DELETE:
        case SPI_OK_MERGE:
        case SPI_OK_INSERT_RETURNING:
        case SPI_OK_UPDATE_RETURNING:
        case SPI_OK_DELETE_RETURNING:
        case SPI_OK_MERGE_RETURNING:
            Assert(stmt->mod_stmt);
            exec_set_found(estate, (SPI_processed != 0));
            break;
 
        case SPI_OK_SELINTO:
        case SPI_OK_UTILITY:
            Assert(!stmt->mod_stmt);
            break;
 
        case SPI_OK_REWRITTEN:
 
            /*
             * The command was rewritten into another kind of command. It's
             * not clear what FOUND would mean in that case (and SPI doesn't
             * return the row count either), so just set it to false.  Note
             * that we can't assert anything about mod_stmt here.
             */
            exec_set_found(estate, false);
            break;
 
            /* Some SPI errors deserve specific error messages */
        case SPI_ERROR_COPY:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("cannot COPY to/from client in PL/pgSQL")));
            break;
 
        case SPI_ERROR_TRANSACTION:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("unsupported transaction command in PL/pgSQL")));
            break;
 
        default:
            elog(ERROR, "SPI_execute_plan_with_paramlist failed executing query \"%s\": %s",
                 expr->query, SPI_result_code_string(rc));
            break;
    }
 
    /* All variants should save result info for GET DIAGNOSTICS */
    estate->eval_processed = SPI_processed;
 
    /* Process INTO if present */
    if (stmt->into)
    {
        SPITupleTable *tuptab = SPI_tuptable;
        uint64      n = SPI_processed;
        PLpgSQL_variable *target;
 
        /* If the statement did not return a tuple table, complain */
        if (tuptab == NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("INTO used with a command that cannot return data")));
 
        /* Fetch target's datum entry */
        target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
 
        /*
         * If SELECT ... INTO specified STRICT, and the query didn't find
         * exactly one row, throw an error.  If STRICT was not specified, then
         * allow the query to find any number of rows.
         */
        if (n == 0)
        {
            if (stmt->strict)
            {
                char       *errdetail;
 
                if (estate->func->print_strict_params)
                    errdetail = format_expr_params(estate, expr);
                else
                    errdetail = NULL;
 
                ereport(ERROR,
                        (errcode(ERRCODE_NO_DATA_FOUND),
                         errmsg("query returned no rows"),
                         errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
            }
            /* set the target to NULL(s) */
            exec_move_row(estate, target, NULL, tuptab->tupdesc);
        }
        else
        {
            if (n > 1 && (stmt->strict || stmt->mod_stmt || too_many_rows_level))
            {
                char       *errdetail;
                int         errlevel;
 
                if (estate->func->print_strict_params)
                    errdetail = format_expr_params(estate, expr);
                else
                    errdetail = NULL;
 
                errlevel = (stmt->strict || stmt->mod_stmt) ? ERROR : too_many_rows_level;
 
                ereport(errlevel,
                        (errcode(ERRCODE_TOO_MANY_ROWS),
                         errmsg("query returned more than one row"),
                         errdetail ? errdetail_internal("parameters: %s", errdetail) : 0,
                         errhint("Make sure the query returns a single row, or use LIMIT 1.")));
            }
            /* Put the first result row into the target */
            exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
        }
 
        /* Clean up */
        exec_eval_cleanup(estate);
        SPI_freetuptable(SPI_tuptable);
    }
    else
    {
        /* If the statement returned a tuple table, complain */
        if (SPI_tuptable != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("query has no destination for result data"),
                     (rc == SPI_OK_SELECT) ? errhint("If you want to discard the results of a SELECT, use PERFORM instead.") : 0));
    }
 
    return PLPGSQL_RC_OK;
}
 
 
/* ----------
 * exec_stmt_dynexecute         Execute a dynamic SQL query
 *                  (possibly with INTO).
 * ----------
 */
static int
exec_stmt_dynexecute(PLpgSQL_execstate *estate,
                     PLpgSQL_stmt_dynexecute *stmt)
{
    Datum       query;
    bool        isnull;
    Oid         restype;
    int32       restypmod;
    char       *querystr;
    int         exec_res;
    ParamListInfo paramLI;
    SPIExecuteOptions options;
    MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
 
    /*
     * First we evaluate the string expression after the EXECUTE keyword. Its
     * result is the querystring we have to execute.
     */
    query = exec_eval_expr(estate, stmt->query, &isnull, &restype, &restypmod);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("query string argument of EXECUTE is null")));
 
    /* Get the C-String representation */
    querystr = convert_value_to_string(estate, query, restype);
 
    /* copy it into the stmt_mcontext before we clean up */
    querystr = MemoryContextStrdup(stmt_mcontext, querystr);
 
    exec_eval_cleanup(estate);
 
    /*
     * Execute the query without preparing a saved plan.
     */
    paramLI = exec_eval_using_params(estate, stmt->params);
 
    memset(&options, 0, sizeof(options));
    options.params = paramLI;
    options.read_only = estate->readonly_func;
 
    exec_res = SPI_execute_extended(querystr, &options);
 
    switch (exec_res)
    {
        case SPI_OK_SELECT:
        case SPI_OK_INSERT:
        case SPI_OK_UPDATE:
        case SPI_OK_DELETE:
        case SPI_OK_MERGE:
        case SPI_OK_INSERT_RETURNING:
        case SPI_OK_UPDATE_RETURNING:
        case SPI_OK_DELETE_RETURNING:
        case SPI_OK_MERGE_RETURNING:
        case SPI_OK_UTILITY:
        case SPI_OK_REWRITTEN:
            break;
 
        case 0:
 
            /*
             * Also allow a zero return, which implies the querystring
             * contained no commands.
             */
            break;
 
        case SPI_OK_SELINTO:
 
            /*
             * We want to disallow SELECT INTO for now, because its behavior
             * is not consistent with SELECT INTO in a normal plpgsql context.
             * (We need to reimplement EXECUTE to parse the string as a
             * plpgsql command, not just feed it to SPI_execute.)  This is not
             * a functional limitation because CREATE TABLE AS is allowed.
             */
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("EXECUTE of SELECT ... INTO is not implemented"),
                     errhint("You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
            break;
 
            /* Some SPI errors deserve specific error messages */
        case SPI_ERROR_COPY:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("cannot COPY to/from client in PL/pgSQL")));
            break;
 
        case SPI_ERROR_TRANSACTION:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("EXECUTE of transaction commands is not implemented")));
            break;
 
        default:
            elog(ERROR, "SPI_execute_extended failed executing query \"%s\": %s",
                 querystr, SPI_result_code_string(exec_res));
            break;
    }
 
    /* Save result info for GET DIAGNOSTICS */
    estate->eval_processed = SPI_processed;
 
    /* Process INTO if present */
    if (stmt->into)
    {
        SPITupleTable *tuptab = SPI_tuptable;
        uint64      n = SPI_processed;
        PLpgSQL_variable *target;
 
        /* If the statement did not return a tuple table, complain */
        if (tuptab == NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("INTO used with a command that cannot return data")));
 
        /* Fetch target's datum entry */
        target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
 
        /*
         * If SELECT ... INTO specified STRICT, and the query didn't find
         * exactly one row, throw an error.  If STRICT was not specified, then
         * allow the query to find any number of rows.
         */
        if (n == 0)
        {
            if (stmt->strict)
            {
                char       *errdetail;
 
                if (estate->func->print_strict_params)
                    errdetail = format_preparedparamsdata(estate, paramLI);
                else
                    errdetail = NULL;
 
                ereport(ERROR,
                        (errcode(ERRCODE_NO_DATA_FOUND),
                         errmsg("query returned no rows"),
                         errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
            }
            /* set the target to NULL(s) */
            exec_move_row(estate, target, NULL, tuptab->tupdesc);
        }
        else
        {
            if (n > 1 && stmt->strict)
            {
                char       *errdetail;
 
                if (estate->func->print_strict_params)
                    errdetail = format_preparedparamsdata(estate, paramLI);
                else
                    errdetail = NULL;
 
                ereport(ERROR,
                        (errcode(ERRCODE_TOO_MANY_ROWS),
                         errmsg("query returned more than one row"),
                         errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
            }
 
            /* Put the first result row into the target */
            exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
        }
        /* clean up after exec_move_row() */
        exec_eval_cleanup(estate);
    }
    else
    {
        /*
         * It might be a good idea to raise an error if the query returned
         * tuples that are being ignored, but historically we have not done
         * that.
         */
    }
 
    /* Release any result from SPI_execute, as well as transient data */
    SPI_freetuptable(SPI_tuptable);
    MemoryContextReset(stmt_mcontext);
 
    return PLPGSQL_RC_OK;
}
 
 
/* ----------
 * exec_stmt_dynfors            Execute a dynamic query, assign each
 *                  tuple to a record or row and
 *                  execute a group of statements
 *                  for it.
 * ----------
 */
static int
exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt)
{
    Portal      portal;
    int         rc;
 
    portal = exec_dynquery_with_params(estate, stmt->query, stmt->params,
                                       NULL, CURSOR_OPT_NO_SCROLL);
 
    /*
     * Execute the loop
     */
    rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);
 
    /*
     * Close the implicit cursor
     */
    SPI_cursor_close(portal);
 
    return rc;
}
 
 
/* ----------
 * exec_stmt_open           Execute an OPEN cursor statement
 * ----------
 */
static int
exec_stmt_open(PLpgSQL_execstate *estate, PLpgSQL_stmt_open *stmt)
{
    PLpgSQL_var *curvar;
    MemoryContext stmt_mcontext = NULL;
    char       *curname = NULL;
    PLpgSQL_expr *query;
    Portal      portal;
    ParamListInfo paramLI;
 
    /* ----------
     * Get the cursor variable and if it has an assigned name, check
     * that it's not in use currently.
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (!curvar->isnull)
    {
        MemoryContext oldcontext;
 
        /* We only need stmt_mcontext to hold the cursor name string */
        stmt_mcontext = get_stmt_mcontext(estate);
        oldcontext = MemoryContextSwitchTo(stmt_mcontext);
        curname = TextDatumGetCString(curvar->value);
        MemoryContextSwitchTo(oldcontext);
 
        if (SPI_cursor_find(curname) != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_DUPLICATE_CURSOR),
                     errmsg("cursor \"%s\" already in use", curname)));
    }
 
    /* ----------
     * Process the OPEN according to its type.
     * ----------
     */
    if (stmt->query != NULL)
    {
        /* ----------
         * This is an OPEN refcursor FOR SELECT ...
         *
         * We just make sure the query is planned. The real work is
         * done downstairs.
         * ----------
         */
        query = stmt->query;
        if (query->plan == NULL)
            exec_prepare_plan(estate, query, stmt->cursor_options);
    }
    else if (stmt->dynquery != NULL)
    {
        /* ----------
         * This is an OPEN refcursor FOR EXECUTE ...
         * ----------
         */
        portal = exec_dynquery_with_params(estate,
                                           stmt->dynquery,
                                           stmt->params,
                                           curname,
                                           stmt->cursor_options);
 
        /*
         * If cursor variable was NULL, store the generated portal name in it,
         * after verifying it's okay to assign to.
         *
         * Note: exec_dynquery_with_params already reset the stmt_mcontext, so
         * curname is a dangling pointer here; but testing it for nullness is
         * OK.
         */
        if (curname == NULL)
        {
            exec_check_assignable(estate, stmt->curvar);
            assign_text_var(estate, curvar, portal->name);
        }
 
        return PLPGSQL_RC_OK;
    }
    else
    {
        /* ----------
         * This is an OPEN cursor
         *
         * Note: parser should already have checked that statement supplies
         * args iff cursor needs them, but we check again to be safe.
         * ----------
         */
        if (stmt->argquery != NULL)
        {
            /* ----------
             * OPEN CURSOR with args.  We fake a SELECT ... INTO ...
             * statement to evaluate the args and put 'em into the
             * internal row.
             * ----------
             */
            PLpgSQL_stmt_execsql set_args;
 
            if (curvar->cursor_explicit_argrow < 0)
                ereport(ERROR,
                        (errcode(ERRCODE_SYNTAX_ERROR),
                         errmsg("arguments given for cursor without arguments")));
 
            memset(&set_args, 0, sizeof(set_args));
            set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
            set_args.lineno = stmt->lineno;
            set_args.sqlstmt = stmt->argquery;
            set_args.into = true;
            /* XXX historically this has not been STRICT */
            set_args.target = (PLpgSQL_variable *)
                (estate->datums[curvar->cursor_explicit_argrow]);
 
            if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
                elog(ERROR, "open cursor failed during argument processing");
        }
        else
        {
            if (curvar->cursor_explicit_argrow >= 0)
                ereport(ERROR,
                        (errcode(ERRCODE_SYNTAX_ERROR),
                         errmsg("arguments required for cursor")));
        }
 
        query = curvar->cursor_explicit_expr;
        if (query->plan == NULL)
            exec_prepare_plan(estate, query, curvar->cursor_options);
    }
 
    /*
     * Set up ParamListInfo for this query
     */
    paramLI = setup_param_list(estate, query);
 
    /*
     * Open the cursor (the paramlist will get copied into the portal)
     */
    portal = SPI_cursor_open_with_paramlist(curname, query->plan,
                                            paramLI,
                                            estate->readonly_func);
    if (portal == NULL)
        elog(ERROR, "could not open cursor: %s",
             SPI_result_code_string(SPI_result));
 
    /*
     * If cursor variable was NULL, store the generated portal name in it,
     * after verifying it's okay to assign to.
     */
    if (curname == NULL)
    {
        exec_check_assignable(estate, stmt->curvar);
        assign_text_var(estate, curvar, portal->name);
    }
 
    /* If we had any transient data, clean it up */
    exec_eval_cleanup(estate);
    if (stmt_mcontext)
        MemoryContextReset(stmt_mcontext);
 
    return PLPGSQL_RC_OK;
}
 
 
/* ----------
 * exec_stmt_fetch          Fetch from a cursor into a target, or just
 *                          move the current position of the cursor
 * ----------
 */
static int
exec_stmt_fetch(PLpgSQL_execstate *estate, PLpgSQL_stmt_fetch *stmt)
{
    PLpgSQL_var *curvar;
    long        how_many = stmt->how_many;
    SPITupleTable *tuptab;
    Portal      portal;
    char       *curname;
    uint64      n;
    MemoryContext oldcontext;
 
    /* ----------
     * Get the portal of the cursor by name
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (curvar->isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("cursor variable \"%s\" is null", curvar->refname)));
 
    /* Use eval_mcontext for short-lived string */
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
    curname = TextDatumGetCString(curvar->value);
    MemoryContextSwitchTo(oldcontext);
 
    portal = SPI_cursor_find(curname);
    if (portal == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_UNDEFINED_CURSOR),
                 errmsg("cursor \"%s\" does not exist", curname)));
 
    /* Calculate position for FETCH_RELATIVE or FETCH_ABSOLUTE */
    if (stmt->expr)
    {
        bool        isnull;
 
        /* XXX should be doing this in LONG not INT width */
        how_many = exec_eval_integer(estate, stmt->expr, &isnull);
 
        if (isnull)
            ereport(ERROR,
                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                     errmsg("relative or absolute cursor position is null")));
 
        exec_eval_cleanup(estate);
    }
 
    if (!stmt->is_move)
    {
        PLpgSQL_variable *target;
 
        /* ----------
         * Fetch 1 tuple from the cursor
         * ----------
         */
        SPI_scroll_cursor_fetch(portal, stmt->direction, how_many);
        tuptab = SPI_tuptable;
        n = SPI_processed;
 
        /* ----------
         * Set the target appropriately.
         * ----------
         */
        target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
        if (n == 0)
            exec_move_row(estate, target, NULL, tuptab->tupdesc);
        else
            exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
 
        exec_eval_cleanup(estate);
        SPI_freetuptable(tuptab);
    }
    else
    {
        /* Move the cursor */
        SPI_scroll_cursor_move(portal, stmt->direction, how_many);
        n = SPI_processed;
    }
 
    /* Set the ROW_COUNT and the global FOUND variable appropriately. */
    estate->eval_processed = n;
    exec_set_found(estate, n != 0);
 
    return PLPGSQL_RC_OK;
}
 
/* ----------
 * exec_stmt_close          Close a cursor
 * ----------
 */
static int
exec_stmt_close(PLpgSQL_execstate *estate, PLpgSQL_stmt_close *stmt)
{
    PLpgSQL_var *curvar;
    Portal      portal;
    char       *curname;
    MemoryContext oldcontext;
 
    /* ----------
     * Get the portal of the cursor by name
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (curvar->isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("cursor variable \"%s\" is null", curvar->refname)));
 
    /* Use eval_mcontext for short-lived string */
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
    curname = TextDatumGetCString(curvar->value);
    MemoryContextSwitchTo(oldcontext);
 
    portal = SPI_cursor_find(curname);
    if (portal == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_UNDEFINED_CURSOR),
                 errmsg("cursor \"%s\" does not exist", curname)));
 
    /* ----------
     * And close it.
     * ----------
     */
    SPI_cursor_close(portal);
 
    return PLPGSQL_RC_OK;
}
 
/*
 * exec_stmt_commit
 *
 * Commit the transaction.
 */
static int
exec_stmt_commit(PLpgSQL_execstate *estate, PLpgSQL_stmt_commit *stmt)
{
    if (stmt->chain)
        SPI_commit_and_chain();
    else
        SPI_commit();
 
    /*
     * We need to build new simple-expression infrastructure, since the old
     * data structures are gone.
     */
    estate->simple_eval_estate = NULL;
    estate->simple_eval_resowner = NULL;
    plpgsql_create_econtext(estate);
 
    return PLPGSQL_RC_OK;
}
 
/*
 * exec_stmt_rollback
 *
 * Abort the transaction.
 */
static int
exec_stmt_rollback(PLpgSQL_execstate *estate, PLpgSQL_stmt_rollback *stmt)
{
    if (stmt->chain)
        SPI_rollback_and_chain();
    else
        SPI_rollback();
 
    /*
     * We need to build new simple-expression infrastructure, since the old
     * data structures are gone.
     */
    estate->simple_eval_estate = NULL;
    estate->simple_eval_resowner = NULL;
    plpgsql_create_econtext(estate);
 
    return PLPGSQL_RC_OK;
}
 
/* ----------
 * exec_assign_expr         Put an expression's result into a variable.
 * ----------
 */
static void
exec_assign_expr(PLpgSQL_execstate *estate, PLpgSQL_datum *target,
                 PLpgSQL_expr *expr)
{
    Datum       value;
    bool        isnull;
    Oid         valtype;
    int32       valtypmod;
 
    /*
     * If first time through, create a plan for this expression.
     */
    if (expr->plan == NULL)
        exec_prepare_plan(estate, expr, 0);
 
    value = exec_eval_expr(estate, expr, &isnull, &valtype, &valtypmod);
    exec_assign_value(estate, target, value, isnull, valtype, valtypmod);
    exec_eval_cleanup(estate);
}
 
 
/* ----------
 * exec_assign_c_string     Put a C string into a text variable.
 *
 * We take a NULL pointer as signifying empty string, not SQL null.
 *
 * As with the underlying exec_assign_value, caller is expected to do
 * exec_eval_cleanup later.
 * ----------
 */
static void
exec_assign_c_string(PLpgSQL_execstate *estate, PLpgSQL_datum *target,
                     const char *str)
{
    text       *value;
    MemoryContext oldcontext;
 
    /* Use eval_mcontext for short-lived text value */
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
    if (str != NULL)
        value = cstring_to_text(str);
    else
        value = cstring_to_text("");
    MemoryContextSwitchTo(oldcontext);
 
    exec_assign_value(estate, target, PointerGetDatum(value), false,
                      TEXTOID, -1);
}
 
 
/* ----------
 * exec_assign_value            Put a value into a target datum
 *
 * Note: in some code paths, this will leak memory in the eval_mcontext;
 * we assume that will be cleaned up later by exec_eval_cleanup.  We cannot
 * call exec_eval_cleanup here for fear of destroying the input Datum value.
 * ----------
 */
static void
exec_assign_value(PLpgSQL_execstate *estate,
                  PLpgSQL_datum *target,
                  Datum value, bool isNull,
                  Oid valtype, int32 valtypmod)
{
    switch (target->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
        case PLPGSQL_DTYPE_PROMISE:
            {
                /*
                 * Target is a variable
                 */
                PLpgSQL_var *var = (PLpgSQL_var *) target;
                Datum       newvalue;
 
                newvalue = exec_cast_value(estate,
                                           value,
                                           &isNull,
                                           valtype,
                                           valtypmod,
                                           var->datatype->typoid,
                                           var->datatype->atttypmod);
 
                if (isNull && var->notnull)
                    ereport(ERROR,
                            (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                             errmsg("null value cannot be assigned to variable \"%s\" declared NOT NULL",
                                    var->refname)));
 
                /*
                 * If type is by-reference, copy the new value (which is
                 * probably in the eval_mcontext) into the procedure's main
                 * memory context.  But if it's a read/write reference to an
                 * expanded object, no physical copy needs to happen; at most
                 * we need to reparent the object's memory context.
                 *
                 * If it's an array, we force the value to be stored in R/W
                 * expanded form.  This wins if the function later does, say,
                 * a lot of array subscripting operations on the variable, and
                 * otherwise might lose.  We might need to use a different
                 * heuristic, but it's too soon to tell.  Also, are there
                 * cases where it'd be useful to force non-array values into
                 * expanded form?
                 */
                if (!var->datatype->typbyval && !isNull)
                {
                    if (var->datatype->typisarray &&
                        !VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(newvalue)))
                    {
                        /* array and not already R/W, so apply expand_array */
                        newvalue = expand_array(newvalue,
                                                estate->datum_context,
                                                NULL);
                    }
                    else
                    {
                        /* else transfer value if R/W, else just datumCopy */
                        newvalue = datumTransfer(newvalue,
                                                 false,
                                                 var->datatype->typlen);
                    }
                }
 
                /*
                 * Now free the old value, if any, and assign the new one. But
                 * skip the assignment if old and new values are the same.
                 * Note that for expanded objects, this test is necessary and
                 * cannot reliably be made any earlier; we have to be looking
                 * at the object's standard R/W pointer to be sure pointer
                 * equality is meaningful.
                 *
                 * Also, if it's a promise variable, we should disarm the
                 * promise in any case --- otherwise, assigning null to an
                 * armed promise variable would fail to disarm the promise.
                 */
                if (var->value != newvalue || var->isnull || isNull)
                    assign_simple_var(estate, var, newvalue, isNull,
                                      (!var->datatype->typbyval && !isNull));
                else
                    var->promise = PLPGSQL_PROMISE_NONE;
                break;
            }
 
        case PLPGSQL_DTYPE_ROW:
            {
                /*
                 * Target is a row variable
                 */
                PLpgSQL_row *row = (PLpgSQL_row *) target;
 
                if (isNull)
                {
                    /* If source is null, just assign nulls to the row */
                    exec_move_row(estate, (PLpgSQL_variable *) row,
                                  NULL, NULL);
                }
                else
                {
                    /* Source must be of RECORD or composite type */
                    if (!type_is_rowtype(valtype))
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("cannot assign non-composite value to a row variable")));
                    exec_move_row_from_datum(estate, (PLpgSQL_variable *) row,
                                             value);
                }
                break;
            }
 
        case PLPGSQL_DTYPE_REC:
            {
                /*
                 * Target is a record variable
                 */
                PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
 
                if (isNull)
                {
                    if (rec->notnull)
                        ereport(ERROR,
                                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                 errmsg("null value cannot be assigned to variable \"%s\" declared NOT NULL",
                                        rec->refname)));
 
                    /* Set variable to a simple NULL */
                    exec_move_row(estate, (PLpgSQL_variable *) rec,
                                  NULL, NULL);
                }
                else
                {
                    /* Source must be of RECORD or composite type */
                    if (!type_is_rowtype(valtype))
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("cannot assign non-composite value to a record variable")));
                    exec_move_row_from_datum(estate, (PLpgSQL_variable *) rec,
                                             value);
                }
                break;
            }
 
        case PLPGSQL_DTYPE_RECFIELD:
            {
                /*
                 * Target is a field of a record
                 */
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
                PLpgSQL_rec *rec;
                ExpandedRecordHeader *erh;
 
                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
                erh = rec->erh;
 
                /*
                 * If record variable is NULL, instantiate it if it has a
                 * named composite type, else complain.  (This won't change
                 * the logical state of the record, but if we successfully
                 * assign below, the unassigned fields will all become NULLs.)
                 */
                if (erh == NULL)
                {
                    instantiate_empty_record_variable(estate, rec);
                    erh = rec->erh;
                }
 
                /*
                 * Look up the field's properties if we have not already, or
                 * if the tuple descriptor ID changed since last time.
                 */
                if (unlikely(recfield->rectupledescid != erh->er_tupdesc_id))
                {
                    if (!expanded_record_lookup_field(erh,
                                                      recfield->fieldname,
                                                      &recfield->finfo))
                        ereport(ERROR,
                                (errcode(ERRCODE_UNDEFINED_COLUMN),
                                 errmsg("record \"%s\" has no field \"%s\"",
                                        rec->refname, recfield->fieldname)));
                    recfield->rectupledescid = erh->er_tupdesc_id;
                }
 
                /* Cast the new value to the right type, if needed. */
                value = exec_cast_value(estate,
                                        value,
                                        &isNull,
                                        valtype,
                                        valtypmod,
                                        recfield->finfo.ftypeid,
                                        recfield->finfo.ftypmod);
 
                /*
                 * Edge case: the cast step can run user-defined code, which
                 * could call the current plpgsql function recursively, and
                 * potentially change the recfield datum underneath us.  If we
                 * see the tupdesc ID change, repeat the lookup, and allow it
                 * so long as the field datatype didn't change.
                 */
                if (unlikely(recfield->rectupledescid != erh->er_tupdesc_id))
                {
                    ExpandedRecordFieldInfo save_finfo = recfield->finfo;
 
                    if (!expanded_record_lookup_field(erh,
                                                      recfield->fieldname,
                                                      &recfield->finfo))
                        ereport(ERROR,
                                (errcode(ERRCODE_UNDEFINED_COLUMN),
                                 errmsg("record \"%s\" has no field \"%s\"",
                                        rec->refname, recfield->fieldname)));
                    recfield->rectupledescid = erh->er_tupdesc_id;
                    if (save_finfo.ftypeid != recfield->finfo.ftypeid ||
                        save_finfo.ftypmod != recfield->finfo.ftypmod)
                        ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                 errmsg("record \"%s\" field \"%s\" changed type during assignment",
                                        rec->refname, recfield->fieldname)));
                }
 
                /* We don't support assignments to system columns. */
                if (recfield->finfo.fnumber <= 0)
                    ereport(ERROR,
                            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                             errmsg("cannot assign to system column \"%s\"",
                                    recfield->fieldname)));
 
                /* All OK, assign it. */
                expanded_record_set_field(erh, recfield->finfo.fnumber,
                                          value, isNull, !estate->atomic);
                break;
            }
 
        default:
            elog(ERROR, "unrecognized dtype: %d", target->dtype);
    }
}
 
/*
 * exec_eval_datum              Get current value of a PLpgSQL_datum
 *
 * The type oid, typmod, value in Datum format, and null flag are returned.
 *
 * At present this doesn't handle PLpgSQL_expr datums; that's not needed
 * because we never pass references to such datums to SPI.
 *
 * NOTE: the returned Datum points right at the stored value in the case of
 * pass-by-reference datatypes.  Generally callers should take care not to
 * modify the stored value.  Some callers intentionally manipulate variables
 * referenced by R/W expanded pointers, though; it is those callers'
 * responsibility that the results are semantically OK.
 *
 * In some cases we have to palloc a return value, and in such cases we put
 * it into the estate's eval_mcontext.
 */
static void
exec_eval_datum(PLpgSQL_execstate *estate,
                PLpgSQL_datum *datum,
                Oid *typeid,
                int32 *typetypmod,
                Datum *value,
                bool *isnull)
{
    MemoryContext oldcontext;
 
    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_PROMISE:
            /* fulfill promise if needed, then handle like regular var */
            plpgsql_fulfill_promise(estate, (PLpgSQL_var *) datum);
 
            pg_fallthrough;
 
        case PLPGSQL_DTYPE_VAR:
            {
                PLpgSQL_var *var = (PLpgSQL_var *) datum;
 
                *typeid = var->datatype->typoid;
                *typetypmod = var->datatype->atttypmod;
                *value = var->value;
                *isnull = var->isnull;
                break;
            }
 
        case PLPGSQL_DTYPE_ROW:
            {
                PLpgSQL_row *row = (PLpgSQL_row *) datum;
                HeapTuple   tup;
 
                /* We get here if there are multiple OUT parameters */
                if (!row->rowtupdesc)   /* should not happen */
                    elog(ERROR, "row variable has no tupdesc");
                /* Make sure we have a valid type/typmod setting */
                BlessTupleDesc(row->rowtupdesc);
                oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
                tup = make_tuple_from_row(estate, row, row->rowtupdesc);
                if (tup == NULL)    /* should not happen */
                    elog(ERROR, "row not compatible with its own tupdesc");
                *typeid = row->rowtupdesc->tdtypeid;
                *typetypmod = row->rowtupdesc->tdtypmod;
                *value = HeapTupleGetDatum(tup);
                *isnull = false;
                MemoryContextSwitchTo(oldcontext);
                break;
            }
 
        case PLPGSQL_DTYPE_REC:
            {
                PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
 
                if (rec->erh == NULL)
                {
                    /* Treat uninstantiated record as a simple NULL */
                    *value = (Datum) 0;
                    *isnull = true;
                    /* Report variable's declared type */
                    *typeid = rec->rectypeid;
                    *typetypmod = -1;
                }
                else
                {
                    if (ExpandedRecordIsEmpty(rec->erh))
                    {
                        /* Empty record is also a NULL */
                        *value = (Datum) 0;
                        *isnull = true;
                    }
                    else
                    {
                        *value = ExpandedRecordGetDatum(rec->erh);
                        *isnull = false;
                    }
                    if (rec->rectypeid != RECORDOID)
                    {
                        /* Report variable's declared type, if not RECORD */
                        *typeid = rec->rectypeid;
                        *typetypmod = -1;
                    }
                    else
                    {
                        /* Report record's actual type if declared RECORD */
                        *typeid = rec->erh->er_typeid;
                        *typetypmod = rec->erh->er_typmod;
                    }
                }
                break;
            }
 
        case PLPGSQL_DTYPE_RECFIELD:
            {
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
                PLpgSQL_rec *rec;
                ExpandedRecordHeader *erh;
 
                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
                erh = rec->erh;
 
                /*
                 * If record variable is NULL, instantiate it if it has a
                 * named composite type, else complain.  (This won't change
                 * the logical state of the record: it's still NULL.)
                 */
                if (erh == NULL)
                {
                    instantiate_empty_record_variable(estate, rec);
                    erh = rec->erh;
                }
 
                /*
                 * Look up the field's properties if we have not already, or
                 * if the tuple descriptor ID changed since last time.
                 */
                if (unlikely(recfield->rectupledescid != erh->er_tupdesc_id))
                {
                    if (!expanded_record_lookup_field(erh,
                                                      recfield->fieldname,
                                                      &recfield->finfo))
                        ereport(ERROR,
                                (errcode(ERRCODE_UNDEFINED_COLUMN),
                                 errmsg("record \"%s\" has no field \"%s\"",
                                        rec->refname, recfield->fieldname)));
                    recfield->rectupledescid = erh->er_tupdesc_id;
                }
 
                /* Report type data. */
                *typeid = recfield->finfo.ftypeid;
                *typetypmod = recfield->finfo.ftypmod;
 
                /* And fetch the field value. */
                *value = expanded_record_get_field(erh,
                                                   recfield->finfo.fnumber,
                                                   isnull);
                break;
            }
 
        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
    }
}
 
/*
 * plpgsql_exec_get_datum_type              Get datatype of a PLpgSQL_datum
 *
 * This is the same logic as in exec_eval_datum, but we skip acquiring
 * the actual value of the variable.  Also, needn't support DTYPE_ROW.
 */
Oid
plpgsql_exec_get_datum_type(PLpgSQL_execstate *estate,
                            PLpgSQL_datum *datum)
{
    Oid         typeid;
 
    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
        case PLPGSQL_DTYPE_PROMISE:
            {
                PLpgSQL_var *var = (PLpgSQL_var *) datum;
 
                typeid = var->datatype->typoid;
                break;
            }
 
        case PLPGSQL_DTYPE_REC:
            {
                PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
 
                if (rec->erh == NULL || rec->rectypeid != RECORDOID)
                {
                    /* Report variable's declared type */
                    typeid = rec->rectypeid;
                }
                else
                {
                    /* Report record's actual type if declared RECORD */
                    typeid = rec->erh->er_typeid;
                }
                break;
            }
 
        case PLPGSQL_DTYPE_RECFIELD:
            {
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
                PLpgSQL_rec *rec;
 
                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
 
                /*
                 * If record variable is NULL, instantiate it if it has a
                 * named composite type, else complain.  (This won't change
                 * the logical state of the record: it's still NULL.)
                 */
                if (rec->erh == NULL)
                    instantiate_empty_record_variable(estate, rec);
 
                /*
                 * Look up the field's properties if we have not already, or
                 * if the tuple descriptor ID changed since last time.
                 */
                if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
                {
                    if (!expanded_record_lookup_field(rec->erh,
                                                      recfield->fieldname,
                                                      &recfield->finfo))
                        ereport(ERROR,
                                (errcode(ERRCODE_UNDEFINED_COLUMN),
                                 errmsg("record \"%s\" has no field \"%s\"",
                                        rec->refname, recfield->fieldname)));
                    recfield->rectupledescid = rec->erh->er_tupdesc_id;
                }
 
                typeid = recfield->finfo.ftypeid;
                break;
            }
 
        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
            typeid = InvalidOid;    /* keep compiler quiet */
            break;
    }
 
    return typeid;
}
 
/*
 * plpgsql_exec_get_datum_type_info         Get datatype etc of a PLpgSQL_datum
 *
 * An extended version of plpgsql_exec_get_datum_type, which also retrieves the
 * typmod and collation of the datum.  Note however that we don't report the
 * possibly-mutable typmod of RECORD values, but say -1 always.
 */
void
plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate,
                                 PLpgSQL_datum *datum,
                                 Oid *typeId, int32 *typMod, Oid *collation)
{
    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
        case PLPGSQL_DTYPE_PROMISE:
            {
                PLpgSQL_var *var = (PLpgSQL_var *) datum;
 
                *typeId = var->datatype->typoid;
                *typMod = var->datatype->atttypmod;
                *collation = var->datatype->collation;
                break;
            }
 
        case PLPGSQL_DTYPE_REC:
            {
                PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
 
                if (rec->erh == NULL || rec->rectypeid != RECORDOID)
                {
                    /* Report variable's declared type */
                    *typeId = rec->rectypeid;
                    *typMod = -1;
                }
                else
                {
                    /* Report record's actual type if declared RECORD */
                    *typeId = rec->erh->er_typeid;
                    /* do NOT return the mutable typmod of a RECORD variable */
                    *typMod = -1;
                }
                /* composite types are never collatable */
                *collation = InvalidOid;
                break;
            }
 
        case PLPGSQL_DTYPE_RECFIELD:
            {
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
                PLpgSQL_rec *rec;
 
                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
 
                /*
                 * If record variable is NULL, instantiate it if it has a
                 * named composite type, else complain.  (This won't change
                 * the logical state of the record: it's still NULL.)
                 */
                if (rec->erh == NULL)
                    instantiate_empty_record_variable(estate, rec);
 
                /*
                 * Look up the field's properties if we have not already, or
                 * if the tuple descriptor ID changed since last time.
                 */
                if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
                {
                    if (!expanded_record_lookup_field(rec->erh,
                                                      recfield->fieldname,
                                                      &recfield->finfo))
                        ereport(ERROR,
                                (errcode(ERRCODE_UNDEFINED_COLUMN),
                                 errmsg("record \"%s\" has no field \"%s\"",
                                        rec->refname, recfield->fieldname)));
                    recfield->rectupledescid = rec->erh->er_tupdesc_id;
                }
 
                *typeId = recfield->finfo.ftypeid;
                *typMod = recfield->finfo.ftypmod;
                *collation = recfield->finfo.fcollation;
                break;
            }
 
        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
            *typeId = InvalidOid;   /* keep compiler quiet */
            *typMod = -1;
            *collation = InvalidOid;
            break;
    }
}
 
/* ----------
 * exec_eval_integer        Evaluate an expression, coerce result to int4
 *
 * Note we do not do exec_eval_cleanup here; the caller must do it at
 * some later point.  (We do this because the caller may be holding the
 * results of other, pass-by-reference, expression evaluations, such as
 * an array value to be subscripted.)
 * ----------
 */
static int
exec_eval_integer(PLpgSQL_execstate *estate,
                  PLpgSQL_expr *expr,
                  bool *isNull)
{
    Datum       exprdatum;
    Oid         exprtypeid;
    int32       exprtypmod;
 
    exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid, &exprtypmod);
    exprdatum = exec_cast_value(estate, exprdatum, isNull,
                                exprtypeid, exprtypmod,
                                INT4OID, -1);
    return DatumGetInt32(exprdatum);
}
 
/* ----------
 * exec_eval_boolean        Evaluate an expression, coerce result to bool
 *
 * Note we do not do exec_eval_cleanup here; the caller must do it at
 * some later point.
 * ----------
 */
static bool
exec_eval_boolean(PLpgSQL_execstate *estate,
                  PLpgSQL_expr *expr,
                  bool *isNull)
{
    Datum       exprdatum;
    Oid         exprtypeid;
    int32       exprtypmod;
 
    exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid, &exprtypmod);
    exprdatum = exec_cast_value(estate, exprdatum, isNull,
                                exprtypeid, exprtypmod,
                                BOOLOID, -1);
    return DatumGetBool(exprdatum);
}
 
/* ----------
 * exec_eval_expr           Evaluate an expression and return
 *                  the result Datum, along with data type/typmod.
 *
 * NOTE: caller must do exec_eval_cleanup when done with the Datum.
 * ----------
 */
static Datum
exec_eval_expr(PLpgSQL_execstate *estate,
               PLpgSQL_expr *expr,
               bool *isNull,
               Oid *rettype,
               int32 *rettypmod)
{
    Datum       result = 0;
    int         rc;
    Form_pg_attribute attr;
 
    /*
     * If first time through, create a plan for this expression.
     */
    if (expr->plan == NULL)
        exec_prepare_plan(estate, expr, CURSOR_OPT_PARALLEL_OK);
 
    /*
     * If this is a simple expression, bypass SPI and use the executor
     * directly
     */
    if (exec_eval_simple_expr(estate, expr,
                              &result, isNull, rettype, rettypmod))
        return result;
 
    /*
     * Else do it the hard way via exec_run_select
     */
    rc = exec_run_select(estate, expr, 0, NULL);
    if (rc != SPI_OK_SELECT)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("query did not return data"),
                 errcontext("query: %s", expr->query)));
 
    /*
     * Check that the expression returns exactly one column...
     */
    if (estate->eval_tuptable->tupdesc->natts != 1)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg_plural("query returned %d column",
                               "query returned %d columns",
                               estate->eval_tuptable->tupdesc->natts,
                               estate->eval_tuptable->tupdesc->natts),
                 errcontext("query: %s", expr->query)));
 
    /*
     * ... and get the column's datatype.
     */
    attr = TupleDescAttr(estate->eval_tuptable->tupdesc, 0);
    *rettype = attr->atttypid;
    *rettypmod = attr->atttypmod;
 
    /*
     * If there are no rows selected, the result is a NULL of that type.
     */
    if (estate->eval_processed == 0)
    {
        *isNull = true;
        return (Datum) 0;
    }
 
    /*
     * Check that the expression returned no more than one row.
     */
    if (estate->eval_processed != 1)
        ereport(ERROR,
                (errcode(ERRCODE_CARDINALITY_VIOLATION),
                 errmsg("query returned more than one row"),
                 errcontext("query: %s", expr->query)));
 
    /*
     * Return the single result Datum.
     */
    return SPI_getbinval(estate->eval_tuptable->vals[0],
                         estate->eval_tuptable->tupdesc, 1, isNull);
}
 
 
/* ----------
 * exec_run_select          Execute a select query
 *
 * Note: passing maxtuples different from 0 ("return all tuples") is
 * deprecated because it will prevent parallel execution of the query.
 * However, we retain the parameter in case we need it someday.
 * ----------
 */
static int
exec_run_select(PLpgSQL_execstate *estate,
                PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
{
    ParamListInfo paramLI;
    int         rc;
 
    /*
     * On the first call for this expression generate the plan.
     *
     * If we don't need to return a portal, then we're just going to execute
     * the query immediately, which means it's OK to use a parallel plan, even
     * if the number of rows being fetched is limited.  If we do need to
     * return a portal (i.e., this is for a FOR loop), the user's code might
     * invoke additional operations inside the FOR loop, making parallel query
     * unsafe.  In any case, we don't expect any cursor operations to be done,
     * so specify NO_SCROLL for efficiency and semantic safety.
     */
    if (expr->plan == NULL)
    {
        int         cursorOptions = CURSOR_OPT_NO_SCROLL;
 
        if (portalP == NULL)
            cursorOptions |= CURSOR_OPT_PARALLEL_OK;
        exec_prepare_plan(estate, expr, cursorOptions);
    }
 
    /*
     * Set up ParamListInfo to pass to executor
     */
    paramLI = setup_param_list(estate, expr);
 
    /*
     * If a portal was requested, put the query and paramlist into the portal
     */
    if (portalP != NULL)
    {
        *portalP = SPI_cursor_open_with_paramlist(NULL, expr->plan,
                                                  paramLI,
                                                  estate->readonly_func);
        if (*portalP == NULL)
            elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
                 expr->query, SPI_result_code_string(SPI_result));
        exec_eval_cleanup(estate);
        return SPI_OK_CURSOR;
    }
 
    /*
     * Execute the query
     */
    rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
                                         estate->readonly_func, maxtuples);
    if (rc != SPI_OK_SELECT)
    {
        /*
         * SELECT INTO deserves a special error message, because "query is not
         * a SELECT" is not very helpful in that case.
         */
        if (rc == SPI_OK_SELINTO)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("query is SELECT INTO, but it should be plain SELECT"),
                     errcontext("query: %s", expr->query)));
        else
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("query is not a SELECT"),
                     errcontext("query: %s", expr->query)));
    }
 
    /* Save query results for eventual cleanup */
    Assert(estate->eval_tuptable == NULL);
    estate->eval_tuptable = SPI_tuptable;
    estate->eval_processed = SPI_processed;
 
    return rc;
}
 
 
/*
 * exec_for_query --- execute body of FOR loop for each row from a portal
 *
 * Used by exec_stmt_fors, exec_stmt_forc and exec_stmt_dynfors
 */
static int
exec_for_query(PLpgSQL_execstate *estate, PLpgSQL_stmt_forq *stmt,
               Portal portal, bool prefetch_ok)
{
    PLpgSQL_variable *var;
    SPITupleTable *tuptab;
    bool        found = false;
    int         rc = PLPGSQL_RC_OK;
    uint64      previous_id = INVALID_TUPLEDESC_IDENTIFIER;
    bool        tupdescs_match = true;
    uint64      n;
 
    /* Fetch loop variable's datum entry */
    var = (PLpgSQL_variable *) estate->datums[stmt->var->dno];
 
    /*
     * Make sure the portal doesn't get closed by the user statements we
     * execute.
     */
    PinPortal(portal);
 
    /*
     * In a non-atomic context, we dare not prefetch, even if it would
     * otherwise be safe.  Aside from any semantic hazards that that might
     * create, if we prefetch toasted data and then the user commits the
     * transaction, the toast references could turn into dangling pointers.
     * (Rows we haven't yet fetched from the cursor are safe, because the
     * PersistHoldablePortal mechanism handles this scenario.)
     */
    if (!estate->atomic)
        prefetch_ok = false;
 
    /*
     * Fetch the initial tuple(s).  If prefetching is allowed then we grab a
     * few more rows to avoid multiple trips through executor startup
     * overhead.
     */
    SPI_cursor_fetch(portal, true, prefetch_ok ? 10 : 1);
    tuptab = SPI_tuptable;
    n = SPI_processed;
 
    /*
     * If the query didn't return any rows, set the target to NULL and fall
     * through with found = false.
     */
    if (n == 0)
    {
        exec_move_row(estate, var, NULL, tuptab->tupdesc);
        exec_eval_cleanup(estate);
    }
    else
        found = true;           /* processed at least one tuple */
 
    /*
     * Now do the loop
     */
    while (n > 0)
    {
        uint64      i;
 
        for (i = 0; i < n; i++)
        {
            /*
             * Assign the tuple to the target.  Here, because we know that all
             * loop iterations should be assigning the same tupdesc, we can
             * optimize away repeated creations of expanded records with
             * identical tupdescs.  Testing for changes of er_tupdesc_id is
             * reliable even if the loop body contains assignments that
             * replace the target's value entirely, because it's assigned from
             * a process-global counter.  The case where the tupdescs don't
             * match could possibly be handled more efficiently than this
             * coding does, but it's not clear extra effort is worthwhile.
             */
            if (var->dtype == PLPGSQL_DTYPE_REC)
            {
                PLpgSQL_rec *rec = (PLpgSQL_rec *) var;
 
                if (rec->erh &&
                    rec->erh->er_tupdesc_id == previous_id &&
                    tupdescs_match)
                {
                    /* Only need to assign a new tuple value */
                    expanded_record_set_tuple(rec->erh, tuptab->vals[i],
                                              true, !estate->atomic);
                }
                else
                {
                    /*
                     * First time through, or var's tupdesc changed in loop,
                     * or we have to do it the hard way because type coercion
                     * is needed.
                     */
                    exec_move_row(estate, var,
                                  tuptab->vals[i], tuptab->tupdesc);
 
                    /*
                     * Check to see if physical assignment is OK next time.
                     * Once the tupdesc comparison has failed once, we don't
                     * bother rechecking in subsequent loop iterations.
                     */
                    if (tupdescs_match)
                    {
                        tupdescs_match =
                            (rec->rectypeid == RECORDOID ||
                             rec->rectypeid == tuptab->tupdesc->tdtypeid ||
                             compatible_tupdescs(tuptab->tupdesc,
                                                 expanded_record_get_tupdesc(rec->erh)));
                    }
                    previous_id = rec->erh->er_tupdesc_id;
                }
            }
            else
                exec_move_row(estate, var, tuptab->vals[i], tuptab->tupdesc);
 
            exec_eval_cleanup(estate);
 
            /*
             * Execute the statements
             */
            rc = exec_stmts(estate, stmt->body);
 
            LOOP_RC_PROCESSING(stmt->label, goto loop_exit);
        }
 
        SPI_freetuptable(tuptab);
 
        /*
         * Fetch more tuples.  If prefetching is allowed, grab 50 at a time.
         */
        SPI_cursor_fetch(portal, true, prefetch_ok ? 50 : 1);
        tuptab = SPI_tuptable;
        n = SPI_processed;
    }
 
loop_exit:
 
    /*
     * Release last group of tuples (if any)
     */
    SPI_freetuptable(tuptab);
 
    UnpinPortal(portal);
 
    /*
     * Set the FOUND variable to indicate the result of executing the loop
     * (namely, whether we looped one or more times). This must be set last so
     * that it does not interfere with the value of the FOUND variable inside
     * the loop processing itself.
     */
    exec_set_found(estate, found);
 
    return rc;
}
 
 
/* ----------
 * exec_eval_simple_expr -      Evaluate a simple expression returning
 *                              a Datum by directly calling ExecEvalExpr().
 *
 * If successful, store results into *result, *isNull, *rettype, *rettypmod
 * and return true.  If the expression cannot be handled by simple evaluation,
 * return false.
 *
 * Because we only store one execution tree for a simple expression, we
 * can't handle recursion cases.  So, if we see the tree is already busy
 * with an evaluation in the current xact, we just return false and let the
 * caller run the expression the hard way.  (Other alternatives such as
 * creating a new tree for a recursive call either introduce memory leaks,
 * or add enough bookkeeping to be doubtful wins anyway.)  Another case that
 * is covered by the expr_simple_in_use test is where a previous execution
 * of the tree was aborted by an error: the tree may contain bogus state
 * so we dare not re-use it.
 *
 * It is possible that we'd need to replan a simple expression; for example,
 * someone might redefine a SQL function that had been inlined into the simple
 * expression.  That cannot cause a simple expression to become non-simple (or
 * vice versa), but we do have to handle replacing the expression tree.
 *
 * Note: if pass-by-reference, the result is in the eval_mcontext.
 * It will be freed when exec_eval_cleanup is done.
 * ----------
 */
static bool
exec_eval_simple_expr(PLpgSQL_execstate *estate,
                      PLpgSQL_expr *expr,
                      Datum *result,
                      bool *isNull,
                      Oid *rettype,
                      int32 *rettypmod)
{
    ExprContext *econtext = estate->eval_econtext;
    LocalTransactionId curlxid = MyProc->vxid.lxid;
    ParamListInfo paramLI;
    void       *save_setup_arg;
    bool        need_snapshot;
    MemoryContext oldcontext;
 
    /*
     * Forget it if expression wasn't simple before.
     */
    if (expr->expr_simple_expr == NULL)
        return false;
 
    /*
     * If expression is in use in current xact, don't touch it.
     */
    if (unlikely(expr->expr_simple_in_use) &&
        expr->expr_simple_lxid == curlxid)
        return false;
 
    /*
     * Ensure that there's a portal-level snapshot, in case this simple
     * expression is the first thing evaluated after a COMMIT or ROLLBACK.
     * We'd have to do this anyway before executing the expression, so we
     * might as well do it now to ensure that any possible replanning doesn't
     * need to take a new snapshot.
     */
    EnsurePortalSnapshotExists();
 
    /*
     * Check to see if the cached plan has been invalidated.  If not, and this
     * is the first use in the current transaction, save a plan refcount in
     * the simple-expression resowner.
     */
    if (likely(CachedPlanIsSimplyValid(expr->expr_simple_plansource,
                                       expr->expr_simple_plan,
                                       (expr->expr_simple_plan_lxid != curlxid ?
                                        estate->simple_eval_resowner : NULL))))
    {
        /*
         * It's still good, so just remember that we have a refcount on the
         * plan in the current transaction.  (If we already had one, this
         * assignment is a no-op.)
         */
        expr->expr_simple_plan_lxid = curlxid;
    }
    else
    {
        /* Need to replan */
        CachedPlan *cplan;
 
        /*
         * If we have a valid refcount on some previous version of the plan,
         * release it, so we don't leak plans intra-transaction.
         */
        if (expr->expr_simple_plan_lxid == curlxid)
            ReleaseCachedPlan(expr->expr_simple_plan,
                              estate->simple_eval_resowner);
 
        /*
         * Reset to "not simple" to leave sane state (with no dangling
         * pointers) in case we fail while replanning.  We'll need to
         * re-determine simplicity and R/W optimizability anyway, since those
         * could change with the new plan.  expr_simple_plansource can be left
         * alone however, as that cannot move.
         */
        expr->expr_simple_expr = NULL;
        expr->expr_rwopt = PLPGSQL_RWOPT_UNKNOWN;
        expr->expr_rw_param = NULL;
        expr->expr_simple_plan = NULL;
        expr->expr_simple_plan_lxid = InvalidLocalTransactionId;
 
        /* Do the replanning work in the eval_mcontext */
        oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
        cplan = SPI_plan_get_cached_plan(expr->plan);
        MemoryContextSwitchTo(oldcontext);
 
        /*
         * We can't get a failure here, because the number of
         * CachedPlanSources in the SPI plan can't change from what
         * exec_simple_check_plan saw; it's a property of the raw parsetree
         * generated from the query text.
         */
        Assert(cplan != NULL);
 
        /*
         * Recheck exec_is_simple_query, which could now report false in
         * edge-case scenarios such as a non-SRF having been replaced with a
         * SRF.  Also recheck CachedPlanAllowsSimpleValidityCheck, just to be
         * sure.  If either test fails, cope by declaring the plan to be
         * non-simple.  On success, we'll acquire a refcount on the new plan,
         * stored in simple_eval_resowner.
         */
        if (exec_is_simple_query(expr) &&
            CachedPlanAllowsSimpleValidityCheck(expr->expr_simple_plansource,
                                                cplan,
                                                estate->simple_eval_resowner))
        {
            /* Remember that we have the refcount */
            expr->expr_simple_plan = cplan;
            expr->expr_simple_plan_lxid = curlxid;
        }
        else
        {
            /* Release SPI_plan_get_cached_plan's refcount */
            ReleaseCachedPlan(cplan, CurrentResourceOwner);
            return false;
        }
 
        /*
         * SPI_plan_get_cached_plan acquired a plan refcount stored in the
         * active resowner.  We don't need that anymore, so release it.
         */
        ReleaseCachedPlan(cplan, CurrentResourceOwner);
 
        /* Extract desired scalar expression from cached plan */
        exec_save_simple_expr(expr, cplan);
    }
 
    /*
     * Pass back previously-determined result type.
     */
    *rettype = expr->expr_simple_type;
    *rettypmod = expr->expr_simple_typmod;
 
    /*
     * Set up ParamListInfo to pass to executor.  For safety, save and restore
     * estate->paramLI->parserSetupArg around our use of the param list.
     */
    paramLI = estate->paramLI;
    save_setup_arg = paramLI->parserSetupArg;
 
    /*
     * We can skip using setup_param_list() in favor of just doing this
     * unconditionally, because there's no need for the optimization of
     * possibly setting ecxt_param_list_info to NULL; we've already forced use
     * of a generic plan.
     */
    paramLI->parserSetupArg = expr;
    econtext->ecxt_param_list_info = paramLI;
 
    /*
     * Prepare the expression for execution, if it's not been done already in
     * the current transaction.  (This will be forced to happen if we called
     * exec_save_simple_expr above.)
     */
    if (unlikely(expr->expr_simple_lxid != curlxid))
    {
        oldcontext = MemoryContextSwitchTo(estate->simple_eval_estate->es_query_cxt);
        expr->expr_simple_state =
            ExecInitExprWithParams(expr->expr_simple_expr,
                                   econtext->ecxt_param_list_info);
        expr->expr_simple_in_use = false;
        expr->expr_simple_lxid = curlxid;
        MemoryContextSwitchTo(oldcontext);
    }
 
    /*
     * We have to do some of the things SPI_execute_plan would do, in
     * particular push a new snapshot so that stable functions within the
     * expression can see updates made so far by our own function.  However,
     * we can skip doing that (and just invoke the expression with the same
     * snapshot passed to our function) in some cases, which is useful because
     * it's quite expensive relative to the cost of a simple expression.  We
     * can skip it if the expression contains no stable or volatile functions;
     * immutable functions shouldn't need to see our updates.  Also, if this
     * is a read-only function, we haven't made any updates so again it's okay
     * to skip.
     */
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
    need_snapshot = (expr->expr_simple_mutable && !estate->readonly_func);
    if (need_snapshot)
    {
        CommandCounterIncrement();
        PushActiveSnapshot(GetTransactionSnapshot());
    }
 
    /*
     * Mark expression as busy for the duration of the ExecEvalExpr call.
     */
    expr->expr_simple_in_use = true;
 
    /*
     * Finally we can call the executor to evaluate the expression
     */
    *result = ExecEvalExpr(expr->expr_simple_state,
                           econtext,
                           isNull);
 
    /* Assorted cleanup */
    expr->expr_simple_in_use = false;
 
    econtext->ecxt_param_list_info = NULL;
 
    paramLI->parserSetupArg = save_setup_arg;
 
    if (need_snapshot)
        PopActiveSnapshot();
 
    MemoryContextSwitchTo(oldcontext);
 
    /*
     * That's it.
     */
    return true;
}
 
 
/*
 * Create a ParamListInfo to pass to SPI
 *
 * We use a single ParamListInfo struct for all SPI calls made to evaluate
 * PLpgSQL_exprs in this estate.  It contains no per-param data, just hook
 * functions, so it's effectively read-only for SPI.
 *
 * An exception from pure read-only-ness is that the parserSetupArg points
 * to the specific PLpgSQL_expr being evaluated.  This is not an issue for
 * statement-level callers, but lower-level callers must save and restore
 * estate->paramLI->parserSetupArg just in case there's an active evaluation
 * at an outer call level.  (A plausible alternative design would be to
 * create a ParamListInfo struct for each PLpgSQL_expr, but for the moment
 * that seems like a waste of memory.)
 */
static ParamListInfo
setup_param_list(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
{
    ParamListInfo paramLI;
 
    /*
     * We must have created the SPIPlan already (hence, query text has been
     * parsed/analyzed at least once); else we cannot rely on expr->paramnos.
     */
    Assert(expr->plan != NULL);
 
    /*
     * We only need a ParamListInfo if the expression has parameters.
     */
    if (!bms_is_empty(expr->paramnos))
    {
        /* Use the common ParamListInfo */
        paramLI = estate->paramLI;
 
        /*
         * Set up link to active expr where the hook functions can find it.
         * Callers must save and restore parserSetupArg if there is any chance
         * that they are interrupting an active use of parameters.
         */
        paramLI->parserSetupArg = expr;
    }
    else
    {
        /*
         * Expression requires no parameters.  Be sure we represent this case
         * as a NULL ParamListInfo, so that plancache.c knows there is no
         * point in a custom plan.
         */
        paramLI = NULL;
    }
    return paramLI;
}
 
/*
 * plpgsql_param_fetch      paramFetch callback for dynamic parameter fetch
 *
 * We always use the caller's workspace to construct the returned struct.
 *
 * Note: this is no longer used during query execution.  It is used during
 * planning (with speculative == true) and when the ParamListInfo we supply
 * to the executor is copied into a cursor portal or transferred to a
 * parallel child process.
 */
static ParamExternData *
plpgsql_param_fetch(ParamListInfo params,
                    int paramid, bool speculative,
                    ParamExternData *prm)
{
    int         dno;
    PLpgSQL_execstate *estate;
    PLpgSQL_expr *expr;
    PLpgSQL_datum *datum;
    bool        ok = true;
    int32       prmtypmod;
 
    /* paramid's are 1-based, but dnos are 0-based */
    dno = paramid - 1;
    Assert(dno >= 0 && dno < params->numParams);
 
    /* fetch back the hook data */
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    expr = (PLpgSQL_expr *) params->parserSetupArg;
    Assert(params->numParams == estate->ndatums);
 
    /* now we can access the target datum */
    datum = estate->datums[dno];
 
    /*
     * Since copyParamList() or SerializeParamList() will try to materialize
     * every single parameter slot, it's important to return a dummy param
     * when asked for a datum that's not supposed to be used by this SQL
     * expression.  Otherwise we risk failures in exec_eval_datum(), or
     * copying a lot more data than necessary.
     */
    if (!bms_is_member(dno, expr->paramnos))
        ok = false;
 
    /*
     * If the access is speculative, we prefer to return no data rather than
     * to fail in exec_eval_datum().  Check the likely failure cases.
     */
    else if (speculative)
    {
        switch (datum->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
            case PLPGSQL_DTYPE_PROMISE:
                /* always safe */
                break;
 
            case PLPGSQL_DTYPE_ROW:
                /* should be safe in all interesting cases */
                break;
 
            case PLPGSQL_DTYPE_REC:
                /* always safe (might return NULL, that's fine) */
                break;
 
            case PLPGSQL_DTYPE_RECFIELD:
                {
                    PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
                    PLpgSQL_rec *rec;
 
                    rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
 
                    /*
                     * If record variable is NULL, don't risk anything.
                     */
                    if (rec->erh == NULL)
                        ok = false;
 
                    /*
                     * Look up the field's properties if we have not already,
                     * or if the tuple descriptor ID changed since last time.
                     */
                    else if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
                    {
                        if (expanded_record_lookup_field(rec->erh,
                                                         recfield->fieldname,
                                                         &recfield->finfo))
                            recfield->rectupledescid = rec->erh->er_tupdesc_id;
                        else
                            ok = false;
                    }
                    break;
                }
 
            default:
                ok = false;
                break;
        }
    }
 
    /* Return "no such parameter" if not ok */
    if (!ok)
    {
        prm->value = (Datum) 0;
        prm->isnull = true;
        prm->pflags = 0;
        prm->ptype = InvalidOid;
        return prm;
    }
 
    /* OK, evaluate the value and store into the return struct */
    exec_eval_datum(estate, datum,
                    &prm->ptype, &prmtypmod,
                    &prm->value, &prm->isnull);
    /* We can always mark params as "const" for executor's purposes */
    prm->pflags = PARAM_FLAG_CONST;
 
    /*
     * If it's a read/write expanded datum, convert reference to read-only.
     * (There's little point in trying to optimize read/write parameters,
     * given the cases in which this function is used.)
     */
    if (datum->dtype == PLPGSQL_DTYPE_VAR)
        prm->value = MakeExpandedObjectReadOnly(prm->value,
                                                prm->isnull,
                                                ((PLpgSQL_var *) datum)->datatype->typlen);
    else if (datum->dtype == PLPGSQL_DTYPE_REC)
        prm->value = MakeExpandedObjectReadOnly(prm->value,
                                                prm->isnull,
                                                -1);
 
    return prm;
}
 
/*
 * plpgsql_param_compile        paramCompile callback for plpgsql parameters
 */
static void
plpgsql_param_compile(ParamListInfo params, Param *param,
                      ExprState *state,
                      Datum *resv, bool *resnull)
{
    PLpgSQL_execstate *estate;
    PLpgSQL_expr *expr;
    int         dno;
    PLpgSQL_datum *datum;
    ExprEvalStep scratch;
 
    /* fetch back the hook data */
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    expr = (PLpgSQL_expr *) params->parserSetupArg;
 
    /* paramid's are 1-based, but dnos are 0-based */
    dno = param->paramid - 1;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    datum = estate->datums[dno];
 
    scratch.opcode = EEOP_PARAM_CALLBACK;
    scratch.resvalue = resv;
    scratch.resnull = resnull;
 
    /*
     * Select appropriate eval function.
     *
     * First, if this Param references the same varlena-type DTYPE_VAR datum
     * that is the target of the assignment containing this simple expression,
     * then it's possible we will be able to optimize handling of R/W expanded
     * datums.  We don't want to do the work needed to determine that unless
     * we actually see a R/W expanded datum at runtime, so install a checking
     * function that will figure that out when needed.
     *
     * Otherwise, it seems worth special-casing DTYPE_VAR and DTYPE_RECFIELD
     * for performance.  Also, we can determine in advance whether
     * MakeExpandedObjectReadOnly() will be required.  Currently, only
     * VAR/PROMISE and REC datums could contain read/write expanded objects.
     */
    if (datum->dtype == PLPGSQL_DTYPE_VAR)
    {
        bool        isvarlena = (((PLpgSQL_var *) datum)->datatype->typlen == -1);
 
        if (isvarlena && dno == expr->target_param && expr->expr_simple_expr)
            scratch.d.cparam.paramfunc = plpgsql_param_eval_var_check;
        else if (isvarlena)
            scratch.d.cparam.paramfunc = plpgsql_param_eval_var_ro;
        else
            scratch.d.cparam.paramfunc = plpgsql_param_eval_var;
    }
    else if (datum->dtype == PLPGSQL_DTYPE_RECFIELD)
        scratch.d.cparam.paramfunc = plpgsql_param_eval_recfield;
    else if (datum->dtype == PLPGSQL_DTYPE_PROMISE)
    {
        if (((PLpgSQL_var *) datum)->datatype->typlen == -1)
            scratch.d.cparam.paramfunc = plpgsql_param_eval_generic_ro;
        else
            scratch.d.cparam.paramfunc = plpgsql_param_eval_generic;
    }
    else if (datum->dtype == PLPGSQL_DTYPE_REC)
        scratch.d.cparam.paramfunc = plpgsql_param_eval_generic_ro;
    else
        scratch.d.cparam.paramfunc = plpgsql_param_eval_generic;
 
    /*
     * Note: it's tempting to use paramarg to store the estate pointer and
     * thereby save an indirection or two in the eval functions.  But that
     * doesn't work because the compiled expression might be used with
     * different estates for the same PL/pgSQL function.  Instead, store
     * pointers to the PLpgSQL_expr as well as this specific Param, to support
     * plpgsql_param_eval_var_check().
     */
    scratch.d.cparam.paramarg = expr;
    scratch.d.cparam.paramarg2 = param;
    scratch.d.cparam.paramid = param->paramid;
    scratch.d.cparam.paramtype = param->paramtype;
    ExprEvalPushStep(state, &scratch);
}
 
/*
 * plpgsql_param_eval_var_check     evaluation of EEOP_PARAM_CALLBACK step
 *
 * This is specialized to the case of DTYPE_VAR variables for which
 * we may need to determine the applicability of a read/write optimization,
 * but we've not done that yet.  The work to determine applicability will
 * be done at most once (per construction of the PL/pgSQL function's cache
 * entry) when we first see that the target variable's old value is a R/W
 * expanded object.  If we never do see that, nothing is lost: the amount
 * of work done by this function in that case is just about the same as
 * what would be done by plpgsql_param_eval_var_ro, which is what we'd
 * have used otherwise.
 */
static void
plpgsql_param_eval_var_check(ExprState *state, ExprEvalStep *op,
                             ExprContext *econtext)
{
    ParamListInfo params;
    PLpgSQL_execstate *estate;
    int         dno = op->d.cparam.paramid - 1;
    PLpgSQL_var *var;
 
    /* fetch back the hook data */
    params = econtext->ecxt_param_list_info;
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    var = (PLpgSQL_var *) estate->datums[dno];
    Assert(var->dtype == PLPGSQL_DTYPE_VAR);
 
    /*
     * If the variable's current value is a R/W expanded object, it's time to
     * decide whether/how to optimize the assignment.
     */
    if (!var->isnull &&
        VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(var->value)))
    {
        PLpgSQL_expr *expr = (PLpgSQL_expr *) op->d.cparam.paramarg;
        Param      *param = (Param *) op->d.cparam.paramarg2;
 
        /*
         * We might have already figured this out while evaluating some other
         * Param referencing the same variable, so check expr_rwopt first.
         */
        if (expr->expr_rwopt == PLPGSQL_RWOPT_UNKNOWN)
            exec_check_rw_parameter(expr, op->d.cparam.paramid);
 
        /*
         * Update the callback pointer to match what we decided to do, so that
         * this function will not be called again.  Then pass off this
         * execution to the newly-selected function.
         */
        switch (expr->expr_rwopt)
        {
            case PLPGSQL_RWOPT_UNKNOWN:
                Assert(false);
                break;
            case PLPGSQL_RWOPT_NOPE:
                /* Force the value to read-only in all future executions */
                op->d.cparam.paramfunc = plpgsql_param_eval_var_ro;
                plpgsql_param_eval_var_ro(state, op, econtext);
                break;
            case PLPGSQL_RWOPT_TRANSFER:
                /* There can be only one matching Param in this case */
                Assert(param == expr->expr_rw_param);
                /* When the value is read/write, transfer to exec context */
                op->d.cparam.paramfunc = plpgsql_param_eval_var_transfer;
                plpgsql_param_eval_var_transfer(state, op, econtext);
                break;
            case PLPGSQL_RWOPT_INPLACE:
                if (param == expr->expr_rw_param)
                {
                    /* When the value is read/write, deliver it as-is */
                    op->d.cparam.paramfunc = plpgsql_param_eval_var;
                    plpgsql_param_eval_var(state, op, econtext);
                }
                else
                {
                    /* Not the optimizable reference, so force to read-only */
                    op->d.cparam.paramfunc = plpgsql_param_eval_var_ro;
                    plpgsql_param_eval_var_ro(state, op, econtext);
                }
                break;
        }
        return;
    }
 
    /*
     * Otherwise, continue to postpone that decision, and execute an inlined
     * version of exec_eval_datum().  Although this value could potentially
     * need MakeExpandedObjectReadOnly, we know it doesn't right now.
     */
    *op->resvalue = var->value;
    *op->resnull = var->isnull;
 
    /* safety check -- an assertion should be sufficient */
    Assert(var->datatype->typoid == op->d.cparam.paramtype);
}
 
/*
 * plpgsql_param_eval_var_transfer      evaluation of EEOP_PARAM_CALLBACK step
 *
 * This is specialized to the case of DTYPE_VAR variables for which
 * we have determined that a read/write expanded value can be handed off
 * into execution of the expression (and then possibly returned to our
 * function's ownership afterwards).  We have to test though, because the
 * variable might not contain a read/write expanded value during this
 * execution.
 */
static void
plpgsql_param_eval_var_transfer(ExprState *state, ExprEvalStep *op,
                                ExprContext *econtext)
{
    ParamListInfo params;
    PLpgSQL_execstate *estate;
    int         dno = op->d.cparam.paramid - 1;
    PLpgSQL_var *var;
 
    /* fetch back the hook data */
    params = econtext->ecxt_param_list_info;
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    var = (PLpgSQL_var *) estate->datums[dno];
    Assert(var->dtype == PLPGSQL_DTYPE_VAR);
 
    /*
     * If the variable's current value is a R/W expanded object, transfer its
     * ownership into the expression execution context, then drop our own
     * reference to the value by setting the variable to NULL.  That'll be
     * overwritten (perhaps with this same object) when control comes back
     * from the expression.
     */
    if (!var->isnull &&
        VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(var->value)))
    {
        *op->resvalue = TransferExpandedObject(var->value,
                                               get_eval_mcontext(estate));
        *op->resnull = false;
 
        var->value = (Datum) 0;
        var->isnull = true;
        var->freeval = false;
    }
    else
    {
        /*
         * Otherwise we can pass the variable's value directly; we now know
         * that MakeExpandedObjectReadOnly isn't needed.
         */
        *op->resvalue = var->value;
        *op->resnull = var->isnull;
    }
 
    /* safety check -- an assertion should be sufficient */
    Assert(var->datatype->typoid == op->d.cparam.paramtype);
}
 
/*
 * plpgsql_param_eval_var       evaluation of EEOP_PARAM_CALLBACK step
 *
 * This is specialized to the case of DTYPE_VAR variables for which
 * we do not need to invoke MakeExpandedObjectReadOnly.
 */
static void
plpgsql_param_eval_var(ExprState *state, ExprEvalStep *op,
                       ExprContext *econtext)
{
    ParamListInfo params;
    PLpgSQL_execstate *estate;
    int         dno = op->d.cparam.paramid - 1;
    PLpgSQL_var *var;
 
    /* fetch back the hook data */
    params = econtext->ecxt_param_list_info;
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    var = (PLpgSQL_var *) estate->datums[dno];
    Assert(var->dtype == PLPGSQL_DTYPE_VAR);
 
    /* inlined version of exec_eval_datum() */
    *op->resvalue = var->value;
    *op->resnull = var->isnull;
 
    /* safety check -- an assertion should be sufficient */
    Assert(var->datatype->typoid == op->d.cparam.paramtype);
}
 
/*
 * plpgsql_param_eval_var_ro        evaluation of EEOP_PARAM_CALLBACK step
 *
 * This is specialized to the case of DTYPE_VAR variables for which
 * we need to invoke MakeExpandedObjectReadOnly.
 */
static void
plpgsql_param_eval_var_ro(ExprState *state, ExprEvalStep *op,
                          ExprContext *econtext)
{
    ParamListInfo params;
    PLpgSQL_execstate *estate;
    int         dno = op->d.cparam.paramid - 1;
    PLpgSQL_var *var;
 
    /* fetch back the hook data */
    params = econtext->ecxt_param_list_info;
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    var = (PLpgSQL_var *) estate->datums[dno];
    Assert(var->dtype == PLPGSQL_DTYPE_VAR);
 
    /*
     * Inlined version of exec_eval_datum() ... and while we're at it, force
     * expanded datums to read-only.
     */
    *op->resvalue = MakeExpandedObjectReadOnly(var->value,
                                               var->isnull,
                                               -1);
    *op->resnull = var->isnull;
 
    /* safety check -- an assertion should be sufficient */
    Assert(var->datatype->typoid == op->d.cparam.paramtype);
}
 
/*
 * plpgsql_param_eval_recfield      evaluation of EEOP_PARAM_CALLBACK step
 *
 * This is specialized to the case of DTYPE_RECFIELD variables, for which
 * we never need to invoke MakeExpandedObjectReadOnly.
 */
static void
plpgsql_param_eval_recfield(ExprState *state, ExprEvalStep *op,
                            ExprContext *econtext)
{
    ParamListInfo params;
    PLpgSQL_execstate *estate;
    int         dno = op->d.cparam.paramid - 1;
    PLpgSQL_recfield *recfield;
    PLpgSQL_rec *rec;
    ExpandedRecordHeader *erh;
 
    /* fetch back the hook data */
    params = econtext->ecxt_param_list_info;
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    recfield = (PLpgSQL_recfield *) estate->datums[dno];
    Assert(recfield->dtype == PLPGSQL_DTYPE_RECFIELD);
 
    /* inline the relevant part of exec_eval_datum */
    rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
    erh = rec->erh;
 
    /*
     * If record variable is NULL, instantiate it if it has a named composite
     * type, else complain.  (This won't change the logical state of the
     * record: it's still NULL.)
     */
    if (erh == NULL)
    {
        instantiate_empty_record_variable(estate, rec);
        erh = rec->erh;
    }
 
    /*
     * Look up the field's properties if we have not already, or if the tuple
     * descriptor ID changed since last time.
     */
    if (unlikely(recfield->rectupledescid != erh->er_tupdesc_id))
    {
        if (!expanded_record_lookup_field(erh,
                                          recfield->fieldname,
                                          &recfield->finfo))
            ereport(ERROR,
                    (errcode(ERRCODE_UNDEFINED_COLUMN),
                     errmsg("record \"%s\" has no field \"%s\"",
                            rec->refname, recfield->fieldname)));
        recfield->rectupledescid = erh->er_tupdesc_id;
    }
 
    /* OK to fetch the field value. */
    *op->resvalue = expanded_record_get_field(erh,
                                              recfield->finfo.fnumber,
                                              op->resnull);
 
    /* safety check -- needed for, eg, record fields */
    if (unlikely(recfield->finfo.ftypeid != op->d.cparam.paramtype))
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
                        op->d.cparam.paramid,
                        format_type_be(recfield->finfo.ftypeid),
                        format_type_be(op->d.cparam.paramtype))));
}
 
/*
 * plpgsql_param_eval_generic       evaluation of EEOP_PARAM_CALLBACK step
 *
 * This handles all variable types, but assumes we do not need to invoke
 * MakeExpandedObjectReadOnly.
 */
static void
plpgsql_param_eval_generic(ExprState *state, ExprEvalStep *op,
                           ExprContext *econtext)
{
    ParamListInfo params;
    PLpgSQL_execstate *estate;
    int         dno = op->d.cparam.paramid - 1;
    PLpgSQL_datum *datum;
    Oid         datumtype;
    int32       datumtypmod;
 
    /* fetch back the hook data */
    params = econtext->ecxt_param_list_info;
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    datum = estate->datums[dno];
 
    /* fetch datum's value */
    exec_eval_datum(estate, datum,
                    &datumtype, &datumtypmod,
                    op->resvalue, op->resnull);
 
    /* safety check -- needed for, eg, record fields */
    if (unlikely(datumtype != op->d.cparam.paramtype))
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
                        op->d.cparam.paramid,
                        format_type_be(datumtype),
                        format_type_be(op->d.cparam.paramtype))));
}
 
/*
 * plpgsql_param_eval_generic_ro    evaluation of EEOP_PARAM_CALLBACK step
 *
 * This handles all variable types, but assumes we need to invoke
 * MakeExpandedObjectReadOnly (hence, variable must be of a varlena type).
 */
static void
plpgsql_param_eval_generic_ro(ExprState *state, ExprEvalStep *op,
                              ExprContext *econtext)
{
    ParamListInfo params;
    PLpgSQL_execstate *estate;
    int         dno = op->d.cparam.paramid - 1;
    PLpgSQL_datum *datum;
    Oid         datumtype;
    int32       datumtypmod;
 
    /* fetch back the hook data */
    params = econtext->ecxt_param_list_info;
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    Assert(dno >= 0 && dno < estate->ndatums);
 
    /* now we can access the target datum */
    datum = estate->datums[dno];
 
    /* fetch datum's value */
    exec_eval_datum(estate, datum,
                    &datumtype, &datumtypmod,
                    op->resvalue, op->resnull);
 
    /* safety check -- needed for, eg, record fields */
    if (unlikely(datumtype != op->d.cparam.paramtype))
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
                        op->d.cparam.paramid,
                        format_type_be(datumtype),
                        format_type_be(op->d.cparam.paramtype))));
 
    /* force the value to read-only */
    *op->resvalue = MakeExpandedObjectReadOnly(*op->resvalue,
                                               *op->resnull,
                                               -1);
}
 
 
/*
 * exec_move_row            Move one tuple's values into a record or row
 *
 * tup and tupdesc may both be NULL if we're just assigning an indeterminate
 * composite NULL to the target.  Alternatively, can have tup be NULL and
 * tupdesc not NULL, in which case we assign a row of NULLs to the target.
 *
 * Since this uses the mcontext for workspace, caller should eventually call
 * exec_eval_cleanup to prevent long-term memory leaks.
 */
static void
exec_move_row(PLpgSQL_execstate *estate,
              PLpgSQL_variable *target,
              HeapTuple tup, TupleDesc tupdesc)
{
    ExpandedRecordHeader *newerh = NULL;
 
    /*
     * If target is RECORD, we may be able to avoid field-by-field processing.
     */
    if (target->dtype == PLPGSQL_DTYPE_REC)
    {
        PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
 
        /*
         * If we have no source tupdesc, just set the record variable to NULL.
         * (If we have a source tupdesc but not a tuple, we'll set the
         * variable to a row of nulls, instead.  This is odd perhaps, but
         * backwards compatible.)
         */
        if (tupdesc == NULL)
        {
            if (rec->datatype &&
                rec->datatype->typtype == TYPTYPE_DOMAIN)
            {
                /*
                 * If it's a composite domain, NULL might not be a legal
                 * value, so we instead need to make an empty expanded record
                 * and ensure that domain type checking gets done.  If there
                 * is already an expanded record, piggyback on its lookups.
                 */
                newerh = make_expanded_record_for_rec(estate, rec,
                                                      NULL, rec->erh);
                expanded_record_set_tuple(newerh, NULL, false, false);
                assign_record_var(estate, rec, newerh);
            }
            else
            {
                /* Just clear it to NULL */
                if (rec->erh)
                    DeleteExpandedObject(ExpandedRecordGetDatum(rec->erh));
                rec->erh = NULL;
            }
            return;
        }
 
        /*
         * Build a new expanded record with appropriate tupdesc.
         */
        newerh = make_expanded_record_for_rec(estate, rec, tupdesc, NULL);
 
        /*
         * If the rowtypes match, or if we have no tuple anyway, we can
         * complete the assignment without field-by-field processing.
         *
         * The tests here are ordered more or less in order of cheapness.  We
         * can easily detect it will work if the target is declared RECORD or
         * has the same typeid as the source.  But when assigning from a query
         * result, it's common to have a source tupdesc that's labeled RECORD
         * but is actually physically compatible with a named-composite-type
         * target, so it's worth spending extra cycles to check for that.
         */
        if (rec->rectypeid == RECORDOID ||
            rec->rectypeid == tupdesc->tdtypeid ||
            !HeapTupleIsValid(tup) ||
            compatible_tupdescs(tupdesc, expanded_record_get_tupdesc(newerh)))
        {
            if (!HeapTupleIsValid(tup))
            {
                /* No data, so force the record into all-nulls state */
                deconstruct_expanded_record(newerh);
            }
            else
            {
                /* No coercion is needed, so just assign the row value */
                expanded_record_set_tuple(newerh, tup, true, !estate->atomic);
            }
 
            /* Complete the assignment */
            assign_record_var(estate, rec, newerh);
 
            return;
        }
    }
 
    /*
     * Otherwise, deconstruct the tuple and do field-by-field assignment,
     * using exec_move_row_from_fields.
     */
    if (tupdesc && HeapTupleIsValid(tup))
    {
        int         td_natts = tupdesc->natts;
        Datum      *values;
        bool       *nulls;
        Datum       values_local[64];
        bool        nulls_local[64];
 
        /*
         * Need workspace arrays.  If td_natts is small enough, use local
         * arrays to save doing a palloc.  Even if it's not small, we can
         * allocate both the Datum and isnull arrays in one palloc chunk.
         */
        if (td_natts <= lengthof(values_local))
        {
            values = values_local;
            nulls = nulls_local;
        }
        else
        {
            char       *chunk;
 
            chunk = eval_mcontext_alloc(estate,
                                        td_natts * (sizeof(Datum) + sizeof(bool)));
            values = (Datum *) chunk;
            nulls = (bool *) (chunk + td_natts * sizeof(Datum));
        }
 
        heap_deform_tuple(tup, tupdesc, values, nulls);
 
        exec_move_row_from_fields(estate, target, newerh,
                                  values, nulls, tupdesc);
    }
    else
    {
        /*
         * Assign all-nulls.
         */
        exec_move_row_from_fields(estate, target, newerh,
                                  NULL, NULL, NULL);
    }
}
 
/*
 * Verify that a PLpgSQL_rec's rectypeid is up-to-date.
 */
static void
revalidate_rectypeid(PLpgSQL_rec *rec)
{
    PLpgSQL_type *typ = rec->datatype;
    TypeCacheEntry *typentry;
 
    if (rec->rectypeid == RECORDOID)
        return;                 /* it's RECORD, so nothing to do */
    Assert(typ != NULL);
    if (typ->tcache &&
        typ->tcache->tupDesc_identifier == typ->tupdesc_id)
    {
        /*
         * Although *typ is known up-to-date, it's possible that rectypeid
         * isn't, because *rec is cloned during each function startup from a
         * copy that we don't have a good way to update.  Hence, forcibly fix
         * rectypeid before returning.
         */
        rec->rectypeid = typ->typoid;
        return;
    }
 
    /*
     * typcache entry has suffered invalidation, so re-look-up the type name
     * if possible, and then recheck the type OID.  If we don't have a
     * TypeName, then we just have to soldier on with the OID we've got.
     */
    if (typ->origtypname != NULL)
    {
        /* this bit should match parse_datatype() in pl_gram.y */
        typenameTypeIdAndMod(NULL, typ->origtypname,
                             &typ->typoid,
                             &typ->atttypmod);
    }
 
    /* this bit should match build_datatype() in pl_comp.c */
    typentry = lookup_type_cache(typ->typoid,
                                 TYPECACHE_TUPDESC |
                                 TYPECACHE_DOMAIN_BASE_INFO);
    if (typentry->typtype == TYPTYPE_DOMAIN)
        typentry = lookup_type_cache(typentry->domainBaseType,
                                     TYPECACHE_TUPDESC);
    if (typentry->tupDesc == NULL)
    {
        /*
         * If we get here, user tried to replace a composite type with a
         * non-composite one.  We're not gonna support that.
         */
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("type %s is not composite",
                        format_type_be(typ->typoid))));
    }
 
    /*
     * Update tcache and tupdesc_id.  Since we don't support changing to a
     * non-composite type, none of the rest of *typ needs to change.
     */
    typ->tcache = typentry;
    typ->tupdesc_id = typentry->tupDesc_identifier;
 
    /*
     * Update *rec, too.  (We'll deal with subsidiary RECFIELDs as needed.)
     */
    rec->rectypeid = typ->typoid;
}
 
/*
 * Build an expanded record object suitable for assignment to "rec".
 *
 * Caller must supply either a source tuple descriptor or a source expanded
 * record (not both).  If the record variable has declared type RECORD,
 * it'll adopt the source's rowtype.  Even if it doesn't, we may be able to
 * piggyback on a source expanded record to save a typcache lookup.
 *
 * Caller must fill the object with data, then do assign_record_var().
 *
 * The new record is initially put into the mcontext, so it will be cleaned up
 * if we fail before reaching assign_record_var().
 */
static ExpandedRecordHeader *
make_expanded_record_for_rec(PLpgSQL_execstate *estate,
                             PLpgSQL_rec *rec,
                             TupleDesc srctupdesc,
                             ExpandedRecordHeader *srcerh)
{
    ExpandedRecordHeader *newerh;
    MemoryContext mcontext = get_eval_mcontext(estate);
 
    if (rec->rectypeid != RECORDOID)
    {
        /*
         * Make sure rec->rectypeid is up-to-date before using it.
         */
        revalidate_rectypeid(rec);
 
        /*
         * New record must be of desired type, but maybe srcerh has already
         * done all the same lookups.
         */
        if (srcerh && rec->rectypeid == srcerh->er_decltypeid)
            newerh = make_expanded_record_from_exprecord(srcerh,
                                                         mcontext);
        else
            newerh = make_expanded_record_from_typeid(rec->rectypeid, -1,
                                                      mcontext);
    }
    else
    {
        /*
         * We'll adopt the input tupdesc.  We can still use
         * make_expanded_record_from_exprecord, if srcerh isn't a composite
         * domain.  (If it is, we effectively adopt its base type.)
         */
        if (srcerh && !ExpandedRecordIsDomain(srcerh))
            newerh = make_expanded_record_from_exprecord(srcerh,
                                                         mcontext);
        else
        {
            if (!srctupdesc)
                srctupdesc = expanded_record_get_tupdesc(srcerh);
            newerh = make_expanded_record_from_tupdesc(srctupdesc,
                                                       mcontext);
        }
    }
 
    return newerh;
}
 
/*
 * exec_move_row_from_fields    Move arrays of field values into a record or row
 *
 * When assigning to a record, the caller must have already created a suitable
 * new expanded record object, newerh.  Pass NULL when assigning to a row.
 *
 * tupdesc describes the input row, which might have different column
 * types and/or different dropped-column positions than the target.
 * values/nulls/tupdesc can all be NULL if we just want to assign nulls to
 * all fields of the record or row.
 *
 * Since this uses the mcontext for workspace, caller should eventually call
 * exec_eval_cleanup to prevent long-term memory leaks.
 */
static void
exec_move_row_from_fields(PLpgSQL_execstate *estate,
                          PLpgSQL_variable *target,
                          ExpandedRecordHeader *newerh,
                          Datum *values, bool *nulls,
                          TupleDesc tupdesc)
{
    int         td_natts = tupdesc ? tupdesc->natts : 0;
    int         fnum;
    int         anum;
    int         strict_multiassignment_level = 0;
 
    /*
     * The extra check strict strict_multi_assignment can be active, only when
     * input tupdesc is specified.
     */
    if (tupdesc != NULL)
    {
        if (plpgsql_extra_errors & PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT)
            strict_multiassignment_level = ERROR;
        else if (plpgsql_extra_warnings & PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT)
            strict_multiassignment_level = WARNING;
    }
 
    /* Handle RECORD-target case */
    if (target->dtype == PLPGSQL_DTYPE_REC)
    {
        PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
        TupleDesc   var_tupdesc;
        Datum       newvalues_local[64];
        bool        newnulls_local[64];
 
        Assert(newerh != NULL); /* caller must have built new object */
 
        var_tupdesc = expanded_record_get_tupdesc(newerh);
 
        /*
         * Coerce field values if needed.  This might involve dealing with
         * different sets of dropped columns and/or coercing individual column
         * types.  That's sort of a pain, but historically plpgsql has allowed
         * it, so we preserve the behavior.  However, it's worth a quick check
         * to see if the tupdescs are identical.  (Since expandedrecord.c
         * prefers to use refcounted tupdescs from the typcache, expanded
         * records with the same rowtype will have pointer-equal tupdescs.)
         */
        if (var_tupdesc != tupdesc)
        {
            int         vtd_natts = var_tupdesc->natts;
            Datum      *newvalues;
            bool       *newnulls;
 
            /*
             * Need workspace arrays.  If vtd_natts is small enough, use local
             * arrays to save doing a palloc.  Even if it's not small, we can
             * allocate both the Datum and isnull arrays in one palloc chunk.
             */
            if (vtd_natts <= lengthof(newvalues_local))
            {
                newvalues = newvalues_local;
                newnulls = newnulls_local;
            }
            else
            {
                char       *chunk;
 
                chunk = eval_mcontext_alloc(estate,
                                            vtd_natts * (sizeof(Datum) + sizeof(bool)));
                newvalues = (Datum *) chunk;
                newnulls = (bool *) (chunk + vtd_natts * sizeof(Datum));
            }
 
            /* Walk over destination columns */
            anum = 0;
            for (fnum = 0; fnum < vtd_natts; fnum++)
            {
                Form_pg_attribute attr = TupleDescAttr(var_tupdesc, fnum);
                Datum       value;
                bool        isnull;
                Oid         valtype;
                int32       valtypmod;
 
                if (attr->attisdropped)
                {
                    /* expanded_record_set_fields should ignore this column */
                    continue;   /* skip dropped column in record */
                }
 
                while (anum < td_natts &&
                       TupleDescAttr(tupdesc, anum)->attisdropped)
                    anum++;     /* skip dropped column in tuple */
 
                if (anum < td_natts)
                {
                    value = values[anum];
                    isnull = nulls[anum];
                    valtype = TupleDescAttr(tupdesc, anum)->atttypid;
                    valtypmod = TupleDescAttr(tupdesc, anum)->atttypmod;
                    anum++;
                }
                else
                {
                    /* no source for destination column */
                    value = (Datum) 0;
                    isnull = true;
                    valtype = UNKNOWNOID;
                    valtypmod = -1;
 
                    /* When source value is missing */
                    if (strict_multiassignment_level)
                        ereport(strict_multiassignment_level,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("number of source and target fields in assignment does not match"),
                        /* translator: %s represents a name of an extra check */
                                 errdetail("%s check of %s is active.",
                                           "strict_multi_assignment",
                                           strict_multiassignment_level == ERROR ? "extra_errors" :
                                           "extra_warnings"),
                                 errhint("Make sure the query returns the exact list of columns.")));
                }
 
                /* Cast the new value to the right type, if needed. */
                newvalues[fnum] = exec_cast_value(estate,
                                                  value,
                                                  &isnull,
                                                  valtype,
                                                  valtypmod,
                                                  attr->atttypid,
                                                  attr->atttypmod);
                newnulls[fnum] = isnull;
            }
 
            /*
             * When strict_multiassignment extra check is active, then ensure
             * there are no unassigned source attributes.
             */
            if (strict_multiassignment_level && anum < td_natts)
            {
                /* skip dropped columns in the source descriptor */
                while (anum < td_natts &&
                       TupleDescAttr(tupdesc, anum)->attisdropped)
                    anum++;
 
                if (anum < td_natts)
                    ereport(strict_multiassignment_level,
                            (errcode(ERRCODE_DATATYPE_MISMATCH),
                             errmsg("number of source and target fields in assignment does not match"),
                    /* translator: %s represents a name of an extra check */
                             errdetail("%s check of %s is active.",
                                       "strict_multi_assignment",
                                       strict_multiassignment_level == ERROR ? "extra_errors" :
                                       "extra_warnings"),
                             errhint("Make sure the query returns the exact list of columns.")));
            }
 
            values = newvalues;
            nulls = newnulls;
        }
 
        /* Insert the coerced field values into the new expanded record */
        expanded_record_set_fields(newerh, values, nulls, !estate->atomic);
 
        /* Complete the assignment */
        assign_record_var(estate, rec, newerh);
 
        return;
    }
 
    /* newerh should not have been passed in non-RECORD cases */
    Assert(newerh == NULL);
 
    /*
     * For a row, we assign the individual field values to the variables the
     * row points to.
     *
     * NOTE: both this code and the record code above silently ignore extra
     * columns in the source and assume NULL for missing columns.  This is
     * pretty dubious but it's the historical behavior.
     *
     * If we have no input data at all, we'll assign NULL to all columns of
     * the row variable.
     */
    if (target->dtype == PLPGSQL_DTYPE_ROW)
    {
        PLpgSQL_row *row = (PLpgSQL_row *) target;
 
        anum = 0;
        for (fnum = 0; fnum < row->nfields; fnum++)
        {
            PLpgSQL_var *var;
            Datum       value;
            bool        isnull;
            Oid         valtype;
            int32       valtypmod;
 
            var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]);
 
            while (anum < td_natts &&
                   TupleDescAttr(tupdesc, anum)->attisdropped)
                anum++;         /* skip dropped column in tuple */
 
            if (anum < td_natts)
            {
                value = values[anum];
                isnull = nulls[anum];
                valtype = TupleDescAttr(tupdesc, anum)->atttypid;
                valtypmod = TupleDescAttr(tupdesc, anum)->atttypmod;
                anum++;
            }
            else
            {
                /* no source for destination column */
                value = (Datum) 0;
                isnull = true;
                valtype = UNKNOWNOID;
                valtypmod = -1;
 
                if (strict_multiassignment_level)
                    ereport(strict_multiassignment_level,
                            (errcode(ERRCODE_DATATYPE_MISMATCH),
                             errmsg("number of source and target fields in assignment does not match"),
                    /* translator: %s represents a name of an extra check */
                             errdetail("%s check of %s is active.",
                                       "strict_multi_assignment",
                                       strict_multiassignment_level == ERROR ? "extra_errors" :
                                       "extra_warnings"),
                             errhint("Make sure the query returns the exact list of columns.")));
            }
 
            exec_assign_value(estate, (PLpgSQL_datum *) var,
                              value, isnull, valtype, valtypmod);
        }
 
        /*
         * When strict_multiassignment extra check is active, ensure there are
         * no unassigned source attributes.
         */
        if (strict_multiassignment_level && anum < td_natts)
        {
            while (anum < td_natts &&
                   TupleDescAttr(tupdesc, anum)->attisdropped)
                anum++;         /* skip dropped column in tuple */
 
            if (anum < td_natts)
                ereport(strict_multiassignment_level,
                        (errcode(ERRCODE_DATATYPE_MISMATCH),
                         errmsg("number of source and target fields in assignment does not match"),
                /* translator: %s represents a name of an extra check */
                         errdetail("%s check of %s is active.",
                                   "strict_multi_assignment",
                                   strict_multiassignment_level == ERROR ? "extra_errors" :
                                   "extra_warnings"),
                         errhint("Make sure the query returns the exact list of columns.")));
        }
 
        return;
    }
 
    elog(ERROR, "unsupported target type: %d", target->dtype);
}
 
/*
 * compatible_tupdescs: detect whether two tupdescs are physically compatible
 *
 * TRUE indicates that a tuple satisfying src_tupdesc can be used directly as
 * a value for a composite variable using dst_tupdesc.
 */
static bool
compatible_tupdescs(TupleDesc src_tupdesc, TupleDesc dst_tupdesc)
{
    int         i;
 
    /* Possibly we could allow src_tupdesc to have extra columns? */
    if (dst_tupdesc->natts != src_tupdesc->natts)
        return false;
 
    for (i = 0; i < dst_tupdesc->natts; i++)
    {
        Form_pg_attribute dattr = TupleDescAttr(dst_tupdesc, i);
        Form_pg_attribute sattr = TupleDescAttr(src_tupdesc, i);
 
        if (dattr->attisdropped != sattr->attisdropped)
            return false;
        if (!dattr->attisdropped)
        {
            /* Normal columns must match by type and typmod */
            if (dattr->atttypid != sattr->atttypid ||
                (dattr->atttypmod >= 0 &&
                 dattr->atttypmod != sattr->atttypmod))
                return false;
        }
        else
        {
            /* Dropped columns are OK as long as length/alignment match */
            if (dattr->attlen != sattr->attlen ||
                dattr->attalign != sattr->attalign)
                return false;
        }
    }
    return true;
}
 
/* ----------
 * make_tuple_from_row      Make a tuple from the values of a row object
 *
 * A NULL return indicates rowtype mismatch; caller must raise suitable error
 *
 * The result tuple is freshly palloc'd in caller's context.  Some junk
 * may be left behind in eval_mcontext, too.
 * ----------
 */
static HeapTuple
make_tuple_from_row(PLpgSQL_execstate *estate,
                    PLpgSQL_row *row,
                    TupleDesc tupdesc)
{
    int         natts = tupdesc->natts;
    HeapTuple   tuple;
    Datum      *dvalues;
    bool       *nulls;
    int         i;
 
    if (natts != row->nfields)
        return NULL;
 
    dvalues = (Datum *) eval_mcontext_alloc0(estate, natts * sizeof(Datum));
    nulls = (bool *) eval_mcontext_alloc(estate, natts * sizeof(bool));
 
    for (i = 0; i < natts; i++)
    {
        Oid         fieldtypeid;
        int32       fieldtypmod;
 
        if (TupleDescAttr(tupdesc, i)->attisdropped)
        {
            nulls[i] = true;    /* leave the column as null */
            continue;
        }
 
        exec_eval_datum(estate, estate->datums[row->varnos[i]],
                        &fieldtypeid, &fieldtypmod,
                        &dvalues[i], &nulls[i]);
        if (fieldtypeid != TupleDescAttr(tupdesc, i)->atttypid)
            return NULL;
        /* XXX should we insist on typmod match, too? */
    }
 
    tuple = heap_form_tuple(tupdesc, dvalues, nulls);
 
    return tuple;
}
 
/*
 * deconstruct_composite_datum      extract tuple+tupdesc from composite Datum
 *
 * The caller must supply a HeapTupleData variable, in which we set up a
 * tuple header pointing to the composite datum's body.  To make the tuple
 * value outlive that variable, caller would need to apply heap_copytuple...
 * but current callers only need a short-lived tuple value anyway.
 *
 * Returns a pointer to the TupleDesc of the datum's rowtype.
 * Caller is responsible for calling ReleaseTupleDesc when done with it.
 *
 * Note: it's caller's responsibility to be sure value is of composite type.
 * Also, best to call this in a short-lived context, as it might leak memory.
 */
static TupleDesc
deconstruct_composite_datum(Datum value, HeapTupleData *tmptup)
{
    HeapTupleHeader td;
    TupleDesc   tupdesc;
    Oid         tupType;
    int32       tupTypmod;
 
    /* Get tuple body (note this could involve detoasting) */
    td = DatumGetHeapTupleHeader(value);
 
    /* Build a temporary HeapTuple control structure */
    tmptup->t_len = HeapTupleHeaderGetDatumLength(td);
    ItemPointerSetInvalid(&(tmptup->t_self));
    tmptup->t_tableOid = InvalidOid;
    tmptup->t_data = td;
 
    /* Extract rowtype info and find a tupdesc */
    tupType = HeapTupleHeaderGetTypeId(td);
    tupTypmod = HeapTupleHeaderGetTypMod(td);
    tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
 
    /*
     * The tuple might have a different version than the tupledesc. We could
     * check this in all callers instead, but duplicating the check doesn't
     * make a whole lot of sense.
     *
     * XXX: It'd be much better if we had a core API that wrapped all this,
     * there are many copies of this around our tree and extensions.
     */
    if (HeapTupleHeaderIsVersionedDatum(td))
        heap_datum_check(td, tupdesc);
 
    return tupdesc;
}
 
/*
 * Verify that the tuple held by srcerh may be copied directly into dsterh.
 *
 * It's possible that the tupledescs for the source and target have been built
 * at different times. If so, check if they're incompatible - in that case we
 * have no choice but to error out.
 *
 * XXX: It'd be nicer to do this in the ERH layer, but that'd require a few
 * more changes and would make it harder to emit a somewhat understandable
 * error message.
 */
static void
check_record_tupdescs_match(ExpandedRecordHeader *srcerh, ExpandedRecordHeader *dsterh)
{
    Assert(srcerh->er_typeid == dsterh->er_typeid);
 
    /* very common fastpath */
    if (srcerh->er_tupdesc_id == dsterh->er_tupdesc_id)
        return;
 
    /*
     * Note that we intentionally don't use equalTupleDescs() here, since that
     * flags even harmless differences between the descriptors.
     */
    if (!compatible_tupdescs(expanded_record_get_tupdesc(srcerh),
                             expanded_record_get_tupdesc(dsterh)))
        ereport(ERROR,
                errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                errmsg("assigned value is incompatible with the variable's type"),
                errdetail("A variable can retain the type definition established when it was first used."));
}
 
/*
 * exec_move_row_from_datum     Move a composite Datum into a record or row
 *
 * This is equivalent to deconstruct_composite_datum() followed by
 * exec_move_row(), but we can optimize things if the Datum is an
 * expanded-record reference.
 *
 * Note: it's caller's responsibility to be sure value is of composite type.
 */
static void
exec_move_row_from_datum(PLpgSQL_execstate *estate,
                         PLpgSQL_variable *target,
                         Datum value)
{
    /* Check to see if source is an expanded record */
    if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(value)))
    {
        ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(value);
        ExpandedRecordHeader *newerh = NULL;
 
        Assert(erh->er_magic == ER_MAGIC);
 
        /* These cases apply if the target is record not row... */
        if (target->dtype == PLPGSQL_DTYPE_REC)
        {
            PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
 
            /*
             * If it's the same record already stored in the variable, do
             * nothing.  This would happen only in silly cases like "r := r",
             * but we need some check to avoid possibly freeing the variable's
             * live value below.  Note that this applies even if what we have
             * is a R/O pointer.
             */
            if (erh == rec->erh)
                return;
 
            /*
             * Make sure rec->rectypeid is up-to-date before using it.
             */
            revalidate_rectypeid(rec);
 
            /*
             * If we have a R/W pointer, we're allowed to just commandeer
             * ownership of the expanded record.  If it's of the right type to
             * put into the record variable, do that.  (Note we don't accept
             * an expanded record of a composite-domain type as a RECORD
             * value.  We'll treat it as the base composite type instead;
             * compare logic in make_expanded_record_for_rec.)
             */
            if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(value)) &&
                (rec->rectypeid == erh->er_decltypeid ||
                 (rec->rectypeid == RECORDOID &&
                  !ExpandedRecordIsDomain(erh))))
            {
                assign_record_var(estate, rec, erh);
                return;
            }
 
            /*
             * If we already have an expanded record object in the target
             * variable, and the source record contains a valid tuple
             * representation with the right rowtype, then we can skip making
             * a new expanded record and just assign the tuple with
             * expanded_record_set_tuple.  (We can't do the equivalent if we
             * have to do field-by-field assignment, since that wouldn't be
             * atomic if there's an error.)  We consider that there's a
             * rowtype match only if it's the same named composite type or
             * same registered rowtype; checking for matches of anonymous
             * rowtypes would be more expensive than this is worth.
             */
            if (rec->erh &&
                (erh->flags & ER_FLAG_FVALUE_VALID) &&
                erh->er_typeid == rec->erh->er_typeid &&
                (erh->er_typeid != RECORDOID ||
                 (erh->er_typmod == rec->erh->er_typmod &&
                  erh->er_typmod >= 0)))
            {
                /*
                 * Check if the tuple descriptors are compatible. It may seem
                 * tempting to just not enter the surrounding "if" if the
                 * er_tupdesc_id doesn't match, but the fieldwise-assignment
                 * path differs behaviorally, due to not copying the ctid
                 * along. As the er_tupdesc_id can increment due to
                 * invalidation processing, that would lead to unpredictable
                 * behavior.
                 */
                check_record_tupdescs_match(erh, rec->erh);
                expanded_record_set_tuple(rec->erh, erh->fvalue,
                                          true, !estate->atomic);
                return;
            }
 
            /*
             * Otherwise we're gonna need a new expanded record object.  Make
             * it here in hopes of piggybacking on the source object's
             * previous typcache lookup.
             */
            newerh = make_expanded_record_for_rec(estate, rec, NULL, erh);
 
            /*
             * If the expanded record contains a valid tuple representation,
             * and we don't need rowtype conversion, then just copying the
             * tuple is probably faster than field-by-field processing.  (This
             * isn't duplicative of the previous check, since here we will
             * catch the case where the record variable was previously empty.)
             */
            if ((erh->flags & ER_FLAG_FVALUE_VALID) &&
                (rec->rectypeid == RECORDOID ||
                 rec->rectypeid == erh->er_typeid))
            {
                /* check comment above previous call */
                check_record_tupdescs_match(erh, newerh);
                expanded_record_set_tuple(newerh, erh->fvalue,
                                          true, !estate->atomic);
                assign_record_var(estate, rec, newerh);
                return;
            }
 
            /*
             * Need to special-case empty source record, else code below would
             * leak newerh.
             */
            if (ExpandedRecordIsEmpty(erh))
            {
                /* Set newerh to a row of NULLs */
                deconstruct_expanded_record(newerh);
                assign_record_var(estate, rec, newerh);
                return;
            }
        }                       /* end of record-target-only cases */
 
        /*
         * If the source expanded record is empty, we should treat that like a
         * NULL tuple value.  (We're unlikely to see such a case, but we must
         * check this; deconstruct_expanded_record would cause a change of
         * logical state, which is not OK.)
         */
        if (ExpandedRecordIsEmpty(erh))
        {
            exec_move_row(estate, target, NULL,
                          expanded_record_get_tupdesc(erh));
            return;
        }
 
        /*
         * Otherwise, ensure that the source record is deconstructed, and
         * assign from its field values.
         */
        deconstruct_expanded_record(erh);
        exec_move_row_from_fields(estate, target, newerh,
                                  erh->dvalues, erh->dnulls,
                                  expanded_record_get_tupdesc(erh));
    }
    else
    {
        /*
         * Nope, we've got a plain composite Datum.  Deconstruct it; but we
         * don't use deconstruct_composite_datum(), because we may be able to
         * skip calling lookup_rowtype_tupdesc().
         */
        HeapTupleHeader td;
        HeapTupleData tmptup;
        Oid         tupType;
        int32       tupTypmod;
        TupleDesc   tupdesc;
        MemoryContext oldcontext;
 
        /* Ensure that any detoasted data winds up in the eval_mcontext */
        oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
        /* Get tuple body (note this could involve detoasting) */
        td = DatumGetHeapTupleHeader(value);
        MemoryContextSwitchTo(oldcontext);
 
        /* Build a temporary HeapTuple control structure */
        tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
        ItemPointerSetInvalid(&(tmptup.t_self));
        tmptup.t_tableOid = InvalidOid;
        tmptup.t_data = td;
 
        /* Extract rowtype info */
        tupType = HeapTupleHeaderGetTypeId(td);
        tupTypmod = HeapTupleHeaderGetTypMod(td);
 
        /* Now, if the target is record not row, maybe we can optimize ... */
        if (target->dtype == PLPGSQL_DTYPE_REC)
        {
            PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
 
            /*
             * If we already have an expanded record object in the target
             * variable, and the source datum has a matching rowtype, then we
             * can skip making a new expanded record and just assign the tuple
             * with expanded_record_set_tuple.  We consider that there's a
             * rowtype match only if it's the same named composite type or
             * same registered rowtype.  (Checking to reject an anonymous
             * rowtype here should be redundant, but let's be safe.)
             */
            if (rec->erh &&
                tupType == rec->erh->er_typeid &&
                (tupType != RECORDOID ||
                 (tupTypmod == rec->erh->er_typmod &&
                  tupTypmod >= 0)))
            {
                expanded_record_set_tuple(rec->erh, &tmptup,
                                          true, !estate->atomic);
                return;
            }
 
            /*
             * If the source datum has a rowtype compatible with the target
             * variable, just build a new expanded record and assign the tuple
             * into it.  Using make_expanded_record_from_typeid() here saves
             * one typcache lookup compared to the code below.
             */
            if (rec->rectypeid == RECORDOID || rec->rectypeid == tupType)
            {
                ExpandedRecordHeader *newerh;
                MemoryContext mcontext = get_eval_mcontext(estate);
 
                newerh = make_expanded_record_from_typeid(tupType, tupTypmod,
                                                          mcontext);
                expanded_record_set_tuple(newerh, &tmptup,
                                          true, !estate->atomic);
                assign_record_var(estate, rec, newerh);
                return;
            }
 
            /*
             * Otherwise, we're going to need conversion, so fall through to
             * do it the hard way.
             */
        }
 
        /*
         * ROW target, or unoptimizable RECORD target, so we have to expend a
         * lookup to obtain the source datum's tupdesc.
         */
        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
 
        /* Do the move */
        exec_move_row(estate, target, &tmptup, tupdesc);
 
        /* Release tupdesc usage count */
        ReleaseTupleDesc(tupdesc);
    }
}
 
/*
 * If we have not created an expanded record to hold the record variable's
 * value, do so.  The expanded record will be "empty", so this does not
 * change the logical state of the record variable: it's still NULL.
 * However, now we'll have a tupdesc with which we can e.g. look up fields.
 */
static void
instantiate_empty_record_variable(PLpgSQL_execstate *estate, PLpgSQL_rec *rec)
{
    Assert(rec->erh == NULL);   /* else caller error */
 
    /* If declared type is RECORD, we can't instantiate */
    if (rec->rectypeid == RECORDOID)
        ereport(ERROR,
                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                 errmsg("record \"%s\" is not assigned yet", rec->refname),
                 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
 
    /* Make sure rec->rectypeid is up-to-date before using it */
    revalidate_rectypeid(rec);
 
    /* OK, do it */
    rec->erh = make_expanded_record_from_typeid(rec->rectypeid, -1,
                                                estate->datum_context);
}
 
/* ----------
 * convert_value_to_string          Convert a non-null Datum to C string
 *
 * Note: the result is in the estate's eval_mcontext, and will be cleared
 * by the next exec_eval_cleanup() call.  The invoked output function might
 * leave additional cruft there as well, so just pfree'ing the result string
 * would not be enough to avoid memory leaks if we did not do it like this.
 * In most usages the Datum being passed in is also in that context (if
 * pass-by-reference) and so an exec_eval_cleanup() call is needed anyway.
 *
 * Note: not caching the conversion function lookup is bad for performance.
 * However, this function isn't currently used in any places where an extra
 * catalog lookup or two seems like a big deal.
 * ----------
 */
static char *
convert_value_to_string(PLpgSQL_execstate *estate, Datum value, Oid valtype)
{
    char       *result;
    MemoryContext oldcontext;
    Oid         typoutput;
    bool        typIsVarlena;
 
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
    getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
    result = OidOutputFunctionCall(typoutput, value);
    MemoryContextSwitchTo(oldcontext);
 
    return result;
}
 
/* ----------
 * exec_cast_value          Cast a value if required
 *
 * Note that *isnull is an input and also an output parameter.  While it's
 * unlikely that a cast operation would produce null from non-null or vice
 * versa, that could happen in principle.
 *
 * Note: the estate's eval_mcontext is used for temporary storage, and may
 * also contain the result Datum if we have to do a conversion to a pass-
 * by-reference data type.  Be sure to do an exec_eval_cleanup() call when
 * done with the result.
 * ----------
 */
static inline Datum
exec_cast_value(PLpgSQL_execstate *estate,
                Datum value, bool *isnull,
                Oid valtype, int32 valtypmod,
                Oid reqtype, int32 reqtypmod)
{
    /*
     * If the type of the given value isn't what's requested, convert it.
     */
    if (valtype != reqtype ||
        (valtypmod != reqtypmod && reqtypmod != -1))
    {
        /* We keep the slow path out-of-line. */
        value = do_cast_value(estate, value, isnull, valtype, valtypmod,
                              reqtype, reqtypmod);
    }
 
    return value;
}
 
/* ----------
 * do_cast_value            Slow path for exec_cast_value.
 * ----------
 */
static Datum
do_cast_value(PLpgSQL_execstate *estate,
              Datum value, bool *isnull,
              Oid valtype, int32 valtypmod,
              Oid reqtype, int32 reqtypmod)
{
    plpgsql_CastHashEntry *cast_entry;
 
    cast_entry = get_cast_hashentry(estate,
                                    valtype, valtypmod,
                                    reqtype, reqtypmod);
    if (cast_entry)
    {
        ExprContext *econtext = estate->eval_econtext;
        MemoryContext oldcontext;
 
        oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
 
        econtext->caseValue_datum = value;
        econtext->caseValue_isNull = *isnull;
 
        cast_entry->cast_in_use = true;
 
        value = ExecEvalExpr(cast_entry->cast_exprstate, econtext,
                             isnull);
 
        cast_entry->cast_in_use = false;
 
        MemoryContextSwitchTo(oldcontext);
    }
 
    return value;
}
 
/* ----------
 * get_cast_hashentry           Look up how to perform a type cast
 *
 * Returns a plpgsql_CastHashEntry if an expression has to be evaluated,
 * or NULL if the cast is a mere no-op relabeling.  If there's work to be
 * done, the cast_exprstate field contains an expression evaluation tree
 * based on a CaseTestExpr input, and the cast_in_use field should be set
 * true while executing it.
 * ----------
 */
static plpgsql_CastHashEntry *
get_cast_hashentry(PLpgSQL_execstate *estate,
                   Oid srctype, int32 srctypmod,
                   Oid dsttype, int32 dsttypmod)
{
    plpgsql_CastHashKey cast_key;
    plpgsql_CastHashEntry *cast_entry;
    plpgsql_CastExprHashEntry *expr_entry;
    bool        found;
    LocalTransactionId curlxid;
    MemoryContext oldcontext;
 
    /* Look for existing entry */
    cast_key.srctype = srctype;
    cast_key.dsttype = dsttype;
    cast_key.srctypmod = srctypmod;
    cast_key.dsttypmod = dsttypmod;
    cast_entry = (plpgsql_CastHashEntry *) hash_search(estate->cast_hash,
                                                       &cast_key,
                                                       HASH_ENTER, &found);
    if (!found)                 /* initialize if new entry */
    {
        /* We need a second lookup to see if a cast_expr_hash entry exists */
        expr_entry = (plpgsql_CastExprHashEntry *) hash_search(cast_expr_hash,
                                                               &cast_key,
                                                               HASH_ENTER,
                                                               &found);
        if (!found)             /* initialize if new expr entry */
            expr_entry->cast_cexpr = NULL;
 
        cast_entry->cast_centry = expr_entry;
        cast_entry->cast_exprstate = NULL;
        cast_entry->cast_in_use = false;
        cast_entry->cast_lxid = InvalidLocalTransactionId;
    }
    else
    {
        /* Use always-valid link to avoid a second hash lookup */
        expr_entry = cast_entry->cast_centry;
    }
 
    if (expr_entry->cast_cexpr == NULL ||
        !expr_entry->cast_cexpr->is_valid)
    {
        /*
         * We've not looked up this coercion before, or we have but the cached
         * expression has been invalidated.
         */
        Node       *cast_expr;
        CachedExpression *cast_cexpr;
        CaseTestExpr *placeholder;
 
        /*
         * Drop old cached expression if there is one.
         */
        if (expr_entry->cast_cexpr)
        {
            FreeCachedExpression(expr_entry->cast_cexpr);
            expr_entry->cast_cexpr = NULL;
        }
 
        /*
         * Since we could easily fail (no such coercion), construct a
         * temporary coercion expression tree in the short-lived
         * eval_mcontext, then if successful save it as a CachedExpression.
         */
        oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
 
        /*
         * We use a CaseTestExpr as the base of the coercion tree, since it's
         * very cheap to insert the source value for that.
         */
        placeholder = makeNode(CaseTestExpr);
        placeholder->typeId = srctype;
        placeholder->typeMod = srctypmod;
        placeholder->collation = get_typcollation(srctype);
 
        /*
         * Apply coercion.  We use the special coercion context
         * COERCION_PLPGSQL to match plpgsql's historical behavior, namely
         * that any cast not available at ASSIGNMENT level will be implemented
         * as an I/O coercion.  (It's somewhat dubious that we prefer I/O
         * coercion over cast pathways that exist at EXPLICIT level.  Changing
         * that would cause assorted minor behavioral differences though, and
         * a user who wants the explicit-cast behavior can always write an
         * explicit cast.)
         *
         * If source type is UNKNOWN, coerce_to_target_type will fail (it only
         * expects to see that for Const input nodes), so don't call it; we'll
         * apply CoerceViaIO instead.  Likewise, it doesn't currently work for
         * coercing RECORD to some other type, so skip for that too.
         */
        if (srctype == UNKNOWNOID || srctype == RECORDOID)
            cast_expr = NULL;
        else
            cast_expr = coerce_to_target_type(NULL,
                                              (Node *) placeholder, srctype,
                                              dsttype, dsttypmod,
                                              COERCION_PLPGSQL,
                                              COERCE_IMPLICIT_CAST,
                                              -1);
 
        /*
         * If there's no cast path according to the parser, fall back to using
         * an I/O coercion; this is semantically dubious but matches plpgsql's
         * historical behavior.  We would need something of the sort for
         * UNKNOWN literals in any case.  The only case we reject is casting
         * to/from INTERNAL.  (Other than that case, this is probably now only
         * reachable in the case where srctype is UNKNOWN/RECORD.)
         */
        if (cast_expr == NULL)
        {
            CoerceViaIO *iocoerce = makeNode(CoerceViaIO);
 
            if (srctype == INTERNALOID || dsttype == INTERNALOID)
                ereport(ERROR,
                        (errcode(ERRCODE_CANNOT_COERCE),
                         errmsg("cannot cast type %s to %s",
                                format_type_be(srctype),
                                format_type_be(dsttype))));
 
            iocoerce->arg = (Expr *) placeholder;
            iocoerce->resulttype = dsttype;
            iocoerce->resultcollid = InvalidOid;
            iocoerce->coerceformat = COERCE_IMPLICIT_CAST;
            iocoerce->location = -1;
            cast_expr = (Node *) iocoerce;
            if (dsttypmod != -1)
                cast_expr = coerce_to_target_type(NULL,
                                                  cast_expr, dsttype,
                                                  dsttype, dsttypmod,
                                                  COERCION_ASSIGNMENT,
                                                  COERCE_IMPLICIT_CAST,
                                                  -1);
        }
 
        /* Note: we don't bother labeling the expression tree with collation */
 
        /* Plan the expression and build a CachedExpression */
        cast_cexpr = GetCachedExpression(cast_expr);
        cast_expr = cast_cexpr->expr;
 
        /* Detect whether we have a no-op (RelabelType) coercion */
        if (IsA(cast_expr, RelabelType) &&
            ((RelabelType *) cast_expr)->arg == (Expr *) placeholder)
            cast_expr = NULL;
 
        /* Now we can fill in the expression hashtable entry. */
        expr_entry->cast_cexpr = cast_cexpr;
        expr_entry->cast_expr = (Expr *) cast_expr;
 
        /* Be sure to reset the exprstate hashtable entry, too. */
        cast_entry->cast_exprstate = NULL;
        cast_entry->cast_in_use = false;
        cast_entry->cast_lxid = InvalidLocalTransactionId;
 
        MemoryContextSwitchTo(oldcontext);
    }
 
    /* Done if we have determined that this is a no-op cast. */
    if (expr_entry->cast_expr == NULL)
        return NULL;
 
    /*
     * Prepare the expression for execution, if it's not been done already in
     * the current transaction; also, if it's marked busy in the current
     * transaction, abandon that expression tree and build a new one, so as to
     * avoid potential problems with recursive cast expressions and failed
     * executions.  (We will leak some memory intra-transaction if that
     * happens a lot, but we don't expect it to.)  It's okay to update the
     * hash table with the new tree because all plpgsql functions within a
     * given transaction share the same simple_eval_estate.  (Well, regular
     * functions do; DO blocks have private simple_eval_estates, and private
     * cast hash tables to go with them.)
     */
    curlxid = MyProc->vxid.lxid;
    if (cast_entry->cast_lxid != curlxid || cast_entry->cast_in_use)
    {
        oldcontext = MemoryContextSwitchTo(estate->simple_eval_estate->es_query_cxt);
        cast_entry->cast_exprstate = ExecInitExpr(expr_entry->cast_expr, NULL);
        cast_entry->cast_in_use = false;
        cast_entry->cast_lxid = curlxid;
        MemoryContextSwitchTo(oldcontext);
    }
 
    return cast_entry;
}
 
 
/* ----------
 * exec_simple_check_plan -     Check if a plan is simple enough to
 *                              be evaluated by ExecEvalExpr() instead
 *                              of SPI.
 *
 * Note: the refcount manipulations in this function assume that expr->plan
 * is a "saved" SPI plan.  That's a bit annoying from the caller's standpoint,
 * but it's otherwise difficult to avoid leaking the plan on failure.
 * ----------
 */
static void
exec_simple_check_plan(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
{
    List       *plansources;
    CachedPlanSource *plansource;
    CachedPlan *cplan;
    MemoryContext oldcontext;
 
    /*
     * Initialize to "not simple", and reset R/W optimizability.
     */
    expr->expr_simple_expr = NULL;
    expr->expr_rwopt = PLPGSQL_RWOPT_UNKNOWN;
    expr->expr_rw_param = NULL;
 
    /*
     * Check the analyzed-and-rewritten form of the query to see if we will be
     * able to treat it as a simple expression.  Since this function is only
     * called immediately after creating the CachedPlanSource, we need not
     * worry about the query being stale.
     */
    if (!exec_is_simple_query(expr))
        return;
 
    /* exec_is_simple_query verified that there's just one CachedPlanSource */
    plansources = SPI_plan_get_plan_sources(expr->plan);
    plansource = (CachedPlanSource *) linitial(plansources);
 
    /*
     * Get the generic plan for the query.  If replanning is needed, do that
     * work in the eval_mcontext.  (Note that replanning could throw an error,
     * in which case the expr is left marked "not simple", which is fine.)
     */
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
    cplan = SPI_plan_get_cached_plan(expr->plan);
    MemoryContextSwitchTo(oldcontext);
 
    /* Can't fail, because we checked for a single CachedPlanSource above */
    Assert(cplan != NULL);
 
    /*
     * Verify that plancache.c thinks the plan is simple enough to use
     * CachedPlanIsSimplyValid.  Given the restrictions above, it's unlikely
     * that this could fail, but if it does, just treat plan as not simple. On
     * success, save a refcount on the plan in the simple-expression resowner.
     */
    if (CachedPlanAllowsSimpleValidityCheck(plansource, cplan,
                                            estate->simple_eval_resowner))
    {
        /* Remember that we have the refcount */
        expr->expr_simple_plansource = plansource;
        expr->expr_simple_plan = cplan;
        expr->expr_simple_plan_lxid = MyProc->vxid.lxid;
 
        /* Share the remaining work with the replan code path */
        exec_save_simple_expr(expr, cplan);
    }
 
    /*
     * Release the plan refcount obtained by SPI_plan_get_cached_plan.  (This
     * refcount is held by the wrong resowner, so we can't just repurpose it.)
     */
    ReleaseCachedPlan(cplan, CurrentResourceOwner);
}
 
/*
 * exec_is_simple_query - precheck a query tree to see if it might be simple
 *
 * Check the analyzed-and-rewritten form of a query to see if we will be
 * able to treat it as a simple expression.  It is caller's responsibility
 * that the CachedPlanSource be up-to-date.
 */
static bool
exec_is_simple_query(PLpgSQL_expr *expr)
{
    List       *plansources;
    CachedPlanSource *plansource;
    Query      *query;
 
    /*
     * We can only test queries that resulted in exactly one CachedPlanSource.
     */
    plansources = SPI_plan_get_plan_sources(expr->plan);
    if (list_length(plansources) != 1)
        return false;
    plansource = (CachedPlanSource *) linitial(plansources);
 
    /*
     * 1. There must be one single querytree.
     */
    if (list_length(plansource->query_list) != 1)
        return false;
    query = (Query *) linitial(plansource->query_list);
 
    /*
     * 2. It must be a plain SELECT query without any input tables.
     */
    if (!IsA(query, Query))
        return false;
    if (query->commandType != CMD_SELECT)
        return false;
    if (query->rtable != NIL)
        return false;
 
    /*
     * 3. Can't have any subplans, aggregates, qual clauses either.  (These
     * tests should generally match what inline_function() checks before
     * inlining a SQL function; otherwise, inlining could change our
     * conclusion about whether an expression is simple, which we don't want.)
     */
    if (query->hasAggs ||
        query->hasWindowFuncs ||
        query->hasTargetSRFs ||
        query->hasSubLinks ||
        query->cteList ||
        query->jointree->fromlist ||
        query->jointree->quals ||
        query->groupClause ||
        query->groupingSets ||
        query->havingQual ||
        query->windowClause ||
        query->distinctClause ||
        query->sortClause ||
        query->limitOffset ||
        query->limitCount ||
        query->setOperations)
        return false;
 
    /*
     * 4. The query must have a single attribute as result.
     */
    if (list_length(query->targetList) != 1)
        return false;
 
    /*
     * OK, we can treat it as a simple plan.
     */
    return true;
}
 
/*
 * exec_save_simple_expr --- extract simple expression from CachedPlan
 */
static void
exec_save_simple_expr(PLpgSQL_expr *expr, CachedPlan *cplan)
{
    PlannedStmt *stmt;
    Plan       *plan;
    Expr       *tle_expr;
 
    /*
     * Given the checks that exec_simple_check_plan did, none of the Asserts
     * here should ever fail.
     */
 
    /* Extract the single PlannedStmt */
    Assert(list_length(cplan->stmt_list) == 1);
    stmt = linitial_node(PlannedStmt, cplan->stmt_list);
    Assert(stmt->commandType == CMD_SELECT);
 
    /*
     * Ordinarily, the plan node should be a simple Result.  However, if
     * debug_parallel_query is on, the planner might've stuck a Gather node
     * atop that; and/or if this plan is for a scrollable cursor, the planner
     * might've stuck a Material node atop it.  The simplest way to deal with
     * this is to look through the Gather and/or Material nodes.  The upper
     * node's tlist would normally contain a Var referencing the child node's
     * output ... but setrefs.c might also have copied a Const as-is.
     */
    plan = stmt->planTree;
    for (;;)
    {
        /* Extract the single tlist expression */
        Assert(list_length(plan->targetlist) == 1);
        tle_expr = linitial_node(TargetEntry, plan->targetlist)->expr;
 
        if (IsA(plan, Result))
        {
            Assert(plan->lefttree == NULL &&
                   plan->righttree == NULL &&
                   plan->initPlan == NULL &&
                   plan->qual == NULL &&
                   ((Result *) plan)->resconstantqual == NULL);
            break;
        }
        else if (IsA(plan, Gather) || IsA(plan, Material))
        {
            Assert(plan->lefttree != NULL &&
                   plan->righttree == NULL &&
                   plan->initPlan == NULL &&
                   plan->qual == NULL);
            /* If setrefs.c copied up a Const, no need to look further */
            if (IsA(tle_expr, Const))
                break;
            /* Otherwise, it better be an outer Var */
            Assert(IsA(tle_expr, Var));
            Assert(((Var *) tle_expr)->varno == OUTER_VAR);
            /* Descend to the child node */
            plan = plan->lefttree;
        }
        else
            elog(ERROR, "unexpected plan node type: %d",
                 (int) nodeTag(plan));
    }
 
    /*
     * Save the simple expression, and initialize state to "not valid in
     * current transaction".
     */
    expr->expr_simple_expr = tle_expr;
    expr->expr_simple_state = NULL;
    expr->expr_simple_in_use = false;
    expr->expr_simple_lxid = InvalidLocalTransactionId;
    /* Also stash away the expression result type */
    expr->expr_simple_type = exprType((Node *) tle_expr);
    expr->expr_simple_typmod = exprTypmod((Node *) tle_expr);
    /* We also want to remember if it is immutable or not */
    expr->expr_simple_mutable = contain_mutable_functions((Node *) tle_expr);
}
 
/*
 * exec_check_rw_parameter --- can we pass expanded object as read/write param?
 *
 * There are two separate cases in which we can optimize an update to a
 * variable that has a read/write expanded value by letting the called
 * expression operate directly on the expanded value.  In both cases we
 * are considering assignments like "var := array_append(var, foo)" where
 * the assignment target is also an input to the RHS expression.
 *
 * Case 1 (RWOPT_TRANSFER rule): if the variable is "local" in the sense that
 * its declaration is not outside any BEGIN...EXCEPTION block surrounding the
 * assignment, then we do not need to worry about preserving its value if the
 * RHS expression throws an error.  If in addition the variable is referenced
 * exactly once in the RHS expression, then we can optimize by converting the
 * read/write expanded value into a transient value within the expression
 * evaluation context, and then setting the variable's recorded value to NULL
 * to prevent double-free attempts.  This works regardless of any other
 * details of the RHS expression.  If the expression eventually returns that
 * same expanded object (possibly modified) then the variable will re-acquire
 * ownership; while if it returns something else or throws an error, the
 * expanded object will be discarded as part of cleanup of the evaluation
 * context.
 *
 * Case 2 (RWOPT_INPLACE rule): if we have a non-local assignment or if
 * it looks like "var := array_append(var, var[1])" with multiple references
 * to the target variable, then we can't use case 1.  Nonetheless, if the
 * top-level function is trusted not to corrupt its argument in case of an
 * error, then when the var has an expanded object as value, it is safe to
 * pass the value as a read/write pointer to the top-level function and let
 * the function modify the value in-place.  (Any other references have to be
 * passed as read-only pointers as usual.)  Only the top-level function has to
 * be trusted, since if anything further down fails, the object hasn't been
 * modified yet.
 *
 * This function checks to see if the assignment is optimizable according
 * to either rule, and updates expr->expr_rwopt accordingly.  In addition,
 * it sets expr->expr_rw_param to the address of the Param within the
 * expression that can be passed as read/write (there can be only one);
 * or to NULL when there is no safe Param.
 *
 * Note that this mechanism intentionally allows just one Param to emit a
 * read/write pointer; in case 2, the expression could contain other Params
 * referencing the target variable, but those must be treated as read-only.
 *
 * Also note that we only apply this optimization within simple expressions.
 * There's no point in it for non-simple expressions, because the
 * exec_run_select code path will flatten any expanded result anyway.
 */
static void
exec_check_rw_parameter(PLpgSQL_expr *expr, int paramid)
{
    Expr       *sexpr = expr->expr_simple_expr;
    Oid         funcid;
    List       *fargs;
    Oid         prosupport;
 
    /* Assume unsafe */
    expr->expr_rwopt = PLPGSQL_RWOPT_NOPE;
    expr->expr_rw_param = NULL;
 
    /* Shouldn't be here for non-simple expression */
    Assert(sexpr != NULL);
 
    /* Param should match the expression's assignment target, too */
    Assert(paramid == expr->target_param + 1);
 
    /*
     * If the assignment is to a "local" variable (one whose value won't
     * matter anymore if expression evaluation fails), and this Param is the
     * only reference to that variable in the expression, then we can
     * unconditionally optimize using the "transfer" method.
     */
    if (expr->target_is_local)
    {
        count_param_references_context context;
 
        /* See how many references there are, and find one of them */
        context.paramid = paramid;
        context.count = 0;
        context.last_param = NULL;
        (void) count_param_references((Node *) sexpr, &context);
 
        /* If we're here, the expr must contain some reference to the var */
        Assert(context.count > 0);
 
        /* If exactly one reference, success! */
        if (context.count == 1)
        {
            expr->expr_rwopt = PLPGSQL_RWOPT_TRANSFER;
            expr->expr_rw_param = context.last_param;
            return;
        }
    }
 
    /*
     * Otherwise, see if we can trust the expression's top-level function to
     * apply the "inplace" method.
     *
     * Top level of expression must be a simple FuncExpr, OpExpr, or
     * SubscriptingRef, else we can't identify which function is relevant. But
     * it's okay to look through any RelabelType above that, since that can't
     * fail.
     */
    if (IsA(sexpr, RelabelType))
        sexpr = ((RelabelType *) sexpr)->arg;
    if (IsA(sexpr, FuncExpr))
    {
        FuncExpr   *fexpr = (FuncExpr *) sexpr;
 
        funcid = fexpr->funcid;
        fargs = fexpr->args;
    }
    else if (IsA(sexpr, OpExpr))
    {
        OpExpr     *opexpr = (OpExpr *) sexpr;
 
        funcid = opexpr->opfuncid;
        fargs = opexpr->args;
    }
    else if (IsA(sexpr, SubscriptingRef))
    {
        SubscriptingRef *sbsref = (SubscriptingRef *) sexpr;
 
        funcid = get_typsubscript(sbsref->refcontainertype, NULL);
 
        /*
         * We assume that only the refexpr and refassgnexpr (if any) are
         * relevant to the support function's decision.  If that turns out to
         * be a bad idea, we could incorporate the subscript expressions into
         * the fargs list somehow.
         */
        fargs = list_make2(sbsref->refexpr, sbsref->refassgnexpr);
    }
    else
        return;
 
    /*
     * The top-level function must be one that can handle in-place update
     * safely.  We allow functions to declare their ability to do that via a
     * support function request.
     */
    prosupport = get_func_support(funcid);
    if (OidIsValid(prosupport))
    {
        SupportRequestModifyInPlace req;
        Param      *param;
 
        req.type = T_SupportRequestModifyInPlace;
        req.funcid = funcid;
        req.args = fargs;
        req.paramid = paramid;
 
        param = (Param *)
            DatumGetPointer(OidFunctionCall1(prosupport,
                                             PointerGetDatum(&req)));
 
        if (param == NULL)
            return;             /* support function fails */
 
        /* Verify support function followed the API */
        Assert(IsA(param, Param));
        Assert(param->paramkind == PARAM_EXTERN);
        Assert(param->paramid == paramid);
 
        /* Found the Param we want to pass as read/write */
        expr->expr_rwopt = PLPGSQL_RWOPT_INPLACE;
        expr->expr_rw_param = param;
        return;
    }
}
 
/*
 * Count Params referencing the specified paramid, and return one of them
 * if there are any.
 *
 * We actually only need to distinguish 0, 1, and N references; so we can
 * abort the tree traversal as soon as we've found two.
 */
static bool
count_param_references(Node *node, count_param_references_context *context)
{
    if (node == NULL)
        return false;
    else if (IsA(node, Param))
    {
        Param      *param = (Param *) node;
 
        if (param->paramkind == PARAM_EXTERN &&
            param->paramid == context->paramid)
        {
            context->last_param = param;
            if (++(context->count) > 1)
                return true;    /* abort tree traversal */
        }
        return false;
    }
    else
        return expression_tree_walker(node, count_param_references, context);
}
 
/*
 * exec_check_assignable --- is it OK to assign to the indicated datum?
 *
 * This should match pl_gram.y's check_assignable().
 */
static void
exec_check_assignable(PLpgSQL_execstate *estate, int dno)
{
    PLpgSQL_datum *datum;
 
    Assert(dno >= 0 && dno < estate->ndatums);
    datum = estate->datums[dno];
    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
        case PLPGSQL_DTYPE_PROMISE:
        case PLPGSQL_DTYPE_REC:
            if (((PLpgSQL_variable *) datum)->isconst)
                ereport(ERROR,
                        (errcode(ERRCODE_ERROR_IN_ASSIGNMENT),
                         errmsg("variable \"%s\" is declared CONSTANT",
                                ((PLpgSQL_variable *) datum)->refname)));
            break;
        case PLPGSQL_DTYPE_ROW:
            /* always assignable; member vars were checked at compile time */
            break;
        case PLPGSQL_DTYPE_RECFIELD:
            /* assignable if parent record is */
            exec_check_assignable(estate,
                                  ((PLpgSQL_recfield *) datum)->recparentno);
            break;
        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
            break;
    }
}
 
/* ----------
 * exec_set_found           Set the global found variable to true/false
 * ----------
 */
static void
exec_set_found(PLpgSQL_execstate *estate, bool state)
{
    PLpgSQL_var *var;
 
    var = (PLpgSQL_var *) (estate->datums[estate->found_varno]);
 
    /*
     * Use pg_assume() to avoid a spurious warning with some compilers, by
     * telling the compiler that the VARATT_IS_EXTERNAL_NON_EXPANDED() branch
     * in assign_simple_var() will never be reached when called from here, due
     * to "found" being a boolean (i.e. a byvalue type), not a varlena.
     */
    pg_assume(var->datatype->typlen != -1);
 
    assign_simple_var(estate, var, BoolGetDatum(state), false, false);
}
 
/*
 * plpgsql_create_econtext --- create an eval_econtext for the current function
 *
 * We may need to create a new shared_simple_eval_estate too, if there's not
 * one already for the current transaction.  The EState will be cleaned up at
 * transaction end.  Ditto for shared_simple_eval_resowner.
 */
static void
plpgsql_create_econtext(PLpgSQL_execstate *estate)
{
    SimpleEcontextStackEntry *entry;
 
    /*
     * Create an EState for evaluation of simple expressions, if there's not
     * one already in the current transaction.  The EState is made a child of
     * TopTransactionContext so it will have the right lifespan.
     *
     * Note that this path is never taken when beginning a DO block; the
     * required EState was already made by plpgsql_inline_handler.  However,
     * if the DO block executes COMMIT or ROLLBACK, then we'll come here and
     * make a shared EState to use for the rest of the DO block.  That's OK;
     * see the comments for shared_simple_eval_estate.  (Note also that a DO
     * block will continue to use its private cast hash table for the rest of
     * the block.  That's okay for now, but it might cause problems someday.)
     */
    if (estate->simple_eval_estate == NULL)
    {
        MemoryContext oldcontext;
 
        if (shared_simple_eval_estate == NULL)
        {
            oldcontext = MemoryContextSwitchTo(TopTransactionContext);
            shared_simple_eval_estate = CreateExecutorState();
            MemoryContextSwitchTo(oldcontext);
        }
        estate->simple_eval_estate = shared_simple_eval_estate;
    }
 
    /*
     * Likewise for the simple-expression resource owner.
     */
    if (estate->simple_eval_resowner == NULL)
    {
        if (shared_simple_eval_resowner == NULL)
            shared_simple_eval_resowner =
                ResourceOwnerCreate(TopTransactionResourceOwner,
                                    "PL/pgSQL simple expressions");
        estate->simple_eval_resowner = shared_simple_eval_resowner;
    }
 
    /*
     * Create a child econtext for the current function.
     */
    estate->eval_econtext = CreateExprContext(estate->simple_eval_estate);
 
    /*
     * Make a stack entry so we can clean up the econtext at subxact end.
     * Stack entries are kept in TopTransactionContext for simplicity.
     */
    entry = (SimpleEcontextStackEntry *)
        MemoryContextAlloc(TopTransactionContext,
                           sizeof(SimpleEcontextStackEntry));
 
    entry->stack_econtext = estate->eval_econtext;
    entry->xact_subxid = GetCurrentSubTransactionId();
 
    entry->next = simple_econtext_stack;
    simple_econtext_stack = entry;
}
 
/*
 * plpgsql_destroy_econtext --- destroy function's econtext
 *
 * We check that it matches the top stack entry, and destroy the stack
 * entry along with the context.
 */
static void
plpgsql_destroy_econtext(PLpgSQL_execstate *estate)
{
    SimpleEcontextStackEntry *next;
 
    /*
     * [PG_CUSTOM: DBMS_JAVA] BEGIN
     * Java Connection.commit()/rollback() выполняет вложенный SPI commit/rollback.
     * Transaction callback уже мог очистить simple_econtext_stack, но приватный
     * eval_econtext DO-блока ещё живёт в portal и должен быть освобождён здесь.
     */
    if (simple_econtext_stack == NULL)
    {
        if (estate->eval_econtext != NULL)
            FreeExprContext(estate->eval_econtext, true);
        estate->eval_econtext = NULL;
        return;
    }
    /* [PG_CUSTOM: DBMS_JAVA] END */
 
    Assert(simple_econtext_stack->stack_econtext == estate->eval_econtext);
 
    next = simple_econtext_stack->next;
    pfree(simple_econtext_stack);
    simple_econtext_stack = next;
 
    FreeExprContext(estate->eval_econtext, true);
    estate->eval_econtext = NULL;
}
 
/*
 * plpgsql_xact_cb --- post-transaction-commit-or-abort cleanup
 *
 * If a simple-expression EState was created in the current transaction,
 * it has to be cleaned up.  The same for the simple-expression resowner.
 */
void
plpgsql_xact_cb(XactEvent event, void *arg)
{
    /*
     * If we are doing a clean transaction shutdown, free the EState and tell
     * the resowner to release whatever plancache references it has, so that
     * all remaining resources will be released correctly.  (We don't need to
     * actually delete the resowner here; deletion of the
     * TopTransactionResourceOwner will take care of that.)
     *
     * In an abort, we expect the regular abort recovery procedures to release
     * everything of interest, so just clear our pointers.
     */
    if (event == XACT_EVENT_COMMIT ||
        event == XACT_EVENT_PARALLEL_COMMIT ||
        event == XACT_EVENT_PREPARE)
    {
        simple_econtext_stack = NULL;
 
        if (shared_simple_eval_estate)
            FreeExecutorState(shared_simple_eval_estate);
        shared_simple_eval_estate = NULL;
        if (shared_simple_eval_resowner)
            ReleaseAllPlanCacheRefsInOwner(shared_simple_eval_resowner);
        shared_simple_eval_resowner = NULL;
    }
    else if (event == XACT_EVENT_ABORT ||
             event == XACT_EVENT_PARALLEL_ABORT)
    {
        simple_econtext_stack = NULL;
        shared_simple_eval_estate = NULL;
        shared_simple_eval_resowner = NULL;
    }
}
 
/*
 * plpgsql_subxact_cb --- post-subtransaction-commit-or-abort cleanup
 *
 * Make sure any simple-expression econtexts created in the current
 * subtransaction get cleaned up.  We have to do this explicitly because
 * no other code knows which econtexts belong to which level of subxact.
 */
void
plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
                   SubTransactionId parentSubid, void *arg)
{
    if (event == SUBXACT_EVENT_COMMIT_SUB || event == SUBXACT_EVENT_ABORT_SUB)
    {
        while (simple_econtext_stack != NULL &&
               simple_econtext_stack->xact_subxid == mySubid)
        {
            SimpleEcontextStackEntry *next;
 
            FreeExprContext(simple_econtext_stack->stack_econtext,
                            (event == SUBXACT_EVENT_COMMIT_SUB));
            next = simple_econtext_stack->next;
            pfree(simple_econtext_stack);
            simple_econtext_stack = next;
        }
    }
}
 
/*
 * assign_simple_var --- assign a new value to any VAR datum.
 *
 * This should be the only mechanism for assignment to simple variables,
 * lest we do the release of the old value incorrectly (not to mention
 * the detoasting business).
 */
static void
assign_simple_var(PLpgSQL_execstate *estate, PLpgSQL_var *var,
                  Datum newvalue, bool isnull, bool freeable)
{
    Assert(var->dtype == PLPGSQL_DTYPE_VAR ||
           var->dtype == PLPGSQL_DTYPE_PROMISE);
 
    /*
     * In non-atomic contexts, we do not want to store TOAST pointers in
     * variables, because such pointers might become stale after a commit.
     * Forcibly detoast in such cases.  We don't want to detoast (flatten)
     * expanded objects, however; those should be OK across a transaction
     * boundary since they're just memory-resident objects.  (Elsewhere in
     * this module, operations on expanded records likewise need to request
     * detoasting of record fields when !estate->atomic.  Expanded arrays are
     * not a problem since all array entries are always detoasted.)
     */
    if (!estate->atomic && !isnull && var->datatype->typlen == -1 &&
        VARATT_IS_EXTERNAL_NON_EXPANDED(DatumGetPointer(newvalue)))
    {
        MemoryContext oldcxt;
        Datum       detoasted;
 
        /*
         * Do the detoasting in the eval_mcontext to avoid long-term leakage
         * of whatever memory toast fetching might leak.  Then we have to copy
         * the detoasted datum to the function's main context, which is a
         * pain, but there's little choice.
         */
        oldcxt = MemoryContextSwitchTo(get_eval_mcontext(estate));
        detoasted = PointerGetDatum(detoast_external_attr((varlena *) DatumGetPointer(newvalue)));
        MemoryContextSwitchTo(oldcxt);
        /* Now's a good time to not leak the input value if it's freeable */
        if (freeable)
            pfree(DatumGetPointer(newvalue));
        /* Once we copy the value, it's definitely freeable */
        newvalue = datumCopy(detoasted, false, -1);
        freeable = true;
        /* Can't clean up eval_mcontext here, but it'll happen before long */
    }
 
    /* Free the old value if needed */
    if (var->freeval)
    {
        if (DatumIsReadWriteExpandedObject(var->value,
                                           var->isnull,
                                           var->datatype->typlen))
            DeleteExpandedObject(var->value);
        else
            pfree(DatumGetPointer(var->value));
    }
    /* Assign new value to datum */
    var->value = newvalue;
    var->isnull = isnull;
    var->freeval = freeable;
 
    /*
     * If it's a promise variable, then either we just assigned the promised
     * value, or the user explicitly assigned an overriding value.  Either
     * way, cancel the promise.
     */
    var->promise = PLPGSQL_PROMISE_NONE;
}
 
/*
 * free old value of a text variable and assign new value from C string
 */
static void
assign_text_var(PLpgSQL_execstate *estate, PLpgSQL_var *var, const char *str)
{
    assign_simple_var(estate, var, CStringGetTextDatum(str), false, true);
}
 
/*
 * assign_record_var --- assign a new value to any REC datum.
 */
static void
assign_record_var(PLpgSQL_execstate *estate, PLpgSQL_rec *rec,
                  ExpandedRecordHeader *erh)
{
    Assert(rec->dtype == PLPGSQL_DTYPE_REC);
 
    /* Transfer new record object into datum_context */
    TransferExpandedRecord(erh, estate->datum_context);
 
    /* Free the old value ... */
    if (rec->erh)
        DeleteExpandedObject(ExpandedRecordGetDatum(rec->erh));
 
    /* ... and install the new */
    rec->erh = erh;
}
 
/*
 * exec_eval_using_params --- evaluate params of USING clause
 *
 * The result data structure is created in the stmt_mcontext, and should
 * be freed by resetting that context.
 */
static ParamListInfo
exec_eval_using_params(PLpgSQL_execstate *estate, List *params)
{
    ParamListInfo paramLI;
    int         nargs;
    MemoryContext stmt_mcontext;
    MemoryContext oldcontext;
    int         i;
    ListCell   *lc;
 
    /* Fast path for no parameters: we can just return NULL */
    if (params == NIL)
        return NULL;
 
    nargs = list_length(params);
    stmt_mcontext = get_stmt_mcontext(estate);
    oldcontext = MemoryContextSwitchTo(stmt_mcontext);
    paramLI = makeParamList(nargs);
    MemoryContextSwitchTo(oldcontext);
 
    i = 0;
    foreach(lc, params)
    {
        PLpgSQL_expr *param = (PLpgSQL_expr *) lfirst(lc);
        ParamExternData *prm = &paramLI->params[i];
        int32       ppdtypmod;
 
        /*
         * Always mark params as const, since we only use the result with
         * one-shot plans.
         */
        prm->pflags = PARAM_FLAG_CONST;
 
        prm->value = exec_eval_expr(estate, param,
                                    &prm->isnull,
                                    &prm->ptype,
                                    &ppdtypmod);
 
        oldcontext = MemoryContextSwitchTo(stmt_mcontext);
 
        if (prm->ptype == UNKNOWNOID)
        {
            /*
             * Treat 'unknown' parameters as text, since that's what most
             * people would expect.  The SPI functions can coerce unknown
             * constants in a more intelligent way, but not unknown Params.
             * This code also takes care of copying into the right context.
             * Note we assume 'unknown' has the representation of C-string.
             */
            prm->ptype = TEXTOID;
            if (!prm->isnull)
                prm->value = CStringGetTextDatum(DatumGetCString(prm->value));
        }
        /* pass-by-ref non null values must be copied into stmt_mcontext */
        else if (!prm->isnull)
        {
            int16       typLen;
            bool        typByVal;
 
            get_typlenbyval(prm->ptype, &typLen, &typByVal);
            if (!typByVal)
                prm->value = datumCopy(prm->value, typByVal, typLen);
        }
 
        MemoryContextSwitchTo(oldcontext);
 
        exec_eval_cleanup(estate);
 
        i++;
    }
 
    return paramLI;
}
 
/*
 * Open portal for dynamic query
 *
 * Caution: this resets the stmt_mcontext at exit.  We might eventually need
 * to move that responsibility to the callers, but currently no caller needs
 * to have statement-lifetime temp data that survives past this, so it's
 * simpler to do it here.
 */
static Portal
exec_dynquery_with_params(PLpgSQL_execstate *estate,
                          PLpgSQL_expr *dynquery,
                          List *params,
                          const char *portalname,
                          int cursorOptions)
{
    Portal      portal;
    Datum       query;
    bool        isnull;
    Oid         restype;
    int32       restypmod;
    char       *querystr;
    SPIParseOpenOptions options;
    MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
 
    /*
     * Evaluate the string expression after the EXECUTE keyword. Its result is
     * the querystring we have to execute.
     */
    query = exec_eval_expr(estate, dynquery, &isnull, &restype, &restypmod);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("query string argument of EXECUTE is null")));
 
    /* Get the C-String representation */
    querystr = convert_value_to_string(estate, query, restype);
 
    /* copy it into the stmt_mcontext before we clean up */
    querystr = MemoryContextStrdup(stmt_mcontext, querystr);
 
    exec_eval_cleanup(estate);
 
    /*
     * Open an implicit cursor for the query.  We use SPI_cursor_parse_open
     * even when there are no params, because this avoids making and freeing
     * one copy of the plan.
     */
    memset(&options, 0, sizeof(options));
    options.params = exec_eval_using_params(estate, params);
    options.cursorOptions = cursorOptions;
    options.read_only = estate->readonly_func;
 
    portal = SPI_cursor_parse_open(portalname, querystr, &options);
 
    if (portal == NULL)
        elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
             querystr, SPI_result_code_string(SPI_result));
 
    /* Release transient data */
    MemoryContextReset(stmt_mcontext);
 
    return portal;
}
 
/*
 * Return a formatted string with information about an expression's parameters,
 * or NULL if the expression does not take any parameters.
 * The result is in the eval_mcontext.
 */
static char *
format_expr_params(PLpgSQL_execstate *estate,
                   const PLpgSQL_expr *expr)
{
    int         paramno;
    int         dno;
    StringInfoData paramstr;
    MemoryContext oldcontext;
 
    if (!expr->paramnos)
        return NULL;
 
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
 
    initStringInfo(&paramstr);
    paramno = 0;
    dno = -1;
    while ((dno = bms_next_member(expr->paramnos, dno)) >= 0)
    {
        Datum       paramdatum;
        Oid         paramtypeid;
        bool        paramisnull;
        int32       paramtypmod;
        PLpgSQL_var *curvar;
 
        curvar = (PLpgSQL_var *) estate->datums[dno];
 
        exec_eval_datum(estate, (PLpgSQL_datum *) curvar,
                        &paramtypeid, &paramtypmod,
                        &paramdatum, &paramisnull);
 
        appendStringInfo(&paramstr, "%s%s = ",
                         paramno > 0 ? ", " : "",
                         curvar->refname);
 
        if (paramisnull)
            appendStringInfoString(&paramstr, "NULL");
        else
            appendStringInfoStringQuoted(&paramstr,
                                         convert_value_to_string(estate,
                                                                 paramdatum,
                                                                 paramtypeid),
                                         -1);
 
        paramno++;
    }
 
    MemoryContextSwitchTo(oldcontext);
 
    return paramstr.data;
}
 
/*
 * Return a formatted string with information about the parameter values,
 * or NULL if there are no parameters.
 * The result is in the eval_mcontext.
 */
static char *
format_preparedparamsdata(PLpgSQL_execstate *estate,
                          ParamListInfo paramLI)
{
    int         paramno;
    StringInfoData paramstr;
    MemoryContext oldcontext;
 
    if (!paramLI)
        return NULL;
 
    oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
 
    initStringInfo(&paramstr);
    for (paramno = 0; paramno < paramLI->numParams; paramno++)
    {
        ParamExternData *prm = &paramLI->params[paramno];
 
        /*
         * Note: for now, this is only used on ParamListInfos produced by
         * exec_eval_using_params(), so we don't worry about invoking the
         * paramFetch hook or skipping unused parameters.
         */
        appendStringInfo(&paramstr, "%s$%d = ",
                         paramno > 0 ? ", " : "",
                         paramno + 1);
 
        if (prm->isnull)
            appendStringInfoString(&paramstr, "NULL");
        else
            appendStringInfoStringQuoted(&paramstr,
                                         convert_value_to_string(estate,
                                                                 prm->value,
                                                                 prm->ptype),
                                         -1);
    }
 
    MemoryContextSwitchTo(oldcontext);
 
    return paramstr.data;
}