spg-engine 7.37.23

Execution engine for SPG: glues spg-sql parsing to spg-storage. Foreign keys, joins, vectors, cold tier.
Documentation
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
//! DML execution — the row-mutating statements (INSERT / UPDATE /
//! MERGE / DELETE) lifted out of `lib.rs` (v7.32 engine
//! modularisation). These `impl Engine` methods are dispatched from
//! `Engine::execute` and reach deep into the storage / catalog / WAL
//! internals, which `lib.rs` exposes pub(crate) for them.

use alloc::string::String;
use alloc::vec::Vec;

use spg_sql::ast::{Expr, InsertStatement, SelectItem};

/// v7.37.19 (19.13) — view auto-updatable redirect.
///
/// PG calls a view "simple-query" auto-updatable when its body has
/// the shape `SELECT col1, col2, ... FROM base_table` with no
/// joins / WHERE / GROUP BY / HAVING / DISTINCT / aggregates /
/// unions / ORDER BY / LIMIT / OFFSET. SPG mirrors PG's rule.
///
/// When `view_name` matches such a view in the active catalog,
/// returns `Some(base_table_name)` and the INSERT/UPDATE/DELETE
/// dispatcher rewrites stmt.table to the base. Column lists are
/// preserved as-is — the parsed view body has plain Column
/// references with the same names as the base table's columns
/// (column rename via the view's `columns:` field is rejected by
/// the auto-updatable check), so no per-column mapping is needed.
///
/// Returns None when:
///   - view_name is not a view (a real table → caller's existing path)
///   - the view's body is not a simple-query shape
///   - the view's `columns:` field is non-empty (column rename)
/// v7.38 (read01 P6.46) — resolve an auto-updatable simple view to its base
/// table plus the view's own WHERE predicate (if any). UPDATE / DELETE on the
/// view rewrite to the base table AND-ing the view's WHERE onto the caller's;
/// INSERT ignores the WHERE (no WITH CHECK OPTION support yet). Returns `None`
/// when the view is not a simple single-table projection.
/// v7.39 (round 136) — the resolved target of a write through an auto-updatable
/// view, following nested views down to the real base table.
/// v7.39 (round 154/155) — one computed (expression) column of a view,
/// as seen from the OUTERMOST redirected view.
#[derive(Clone)]
struct ComputedViewCol {
    /// The column's name at the outermost view.
    name: String,
    /// Defining expression, over base-table columns.
    def: spg_sql::ast::Expr,
    /// The view level that defines the expression and its column name
    /// THERE — PG attributes a write-target error to that level even
    /// when an outer view re-exports the column under another name.
    origin_view: String,
    origin_col: String,
}

struct ViewRedirect {
    /// The real base table (bottom of the view chain).
    base: String,
    /// Composed WHERE (every level's predicate AND-ed), in base columns. Filters
    /// which base rows an UPDATE / DELETE through the view may touch.
    where_at_base: Option<spg_sql::ast::Expr>,
    /// Outermost-view-col → base-table-col map (composed across the chain).
    /// Empty when no level renames columns (the common path).
    col_map: Vec<(String, String)>,
    /// v7.39 (round 154) — computed (expression) view columns. PG keeps
    /// such a view auto-updatable; a write TARGETING one of these errors
    /// ("View columns that are not columns of their base relation are
    /// not updatable"), reads of them rewrite to the expression. Round
    /// 155 — a plain projection over a computed view composes: the
    /// re-exported column carries the DEFINING level's view and column
    /// name (PG attributes the error there, through any outer rename).
    computed: Vec<ComputedViewCol>,
    /// v7.39 (round 154) — the view's full output column order,
    /// `(view_col, Some(base_col) = simple | None = computed)`. Filled
    /// only when `computed` is non-empty (positional writes can no
    /// longer be derived from `col_map` alone then).
    view_cols: Vec<(String, Option<String>)>,
    /// Per-level `(view_name, qual_at_base, check_option)`, outermost first, for
    /// WITH CHECK OPTION enforcement (only WHERE-bearing levels appear).
    check_chain: Vec<(String, spg_sql::ast::Expr, u8)>,
}

/// Compose two view column maps: `a` maps outer→mid, `b` maps mid→base; the
/// result maps outer→base. An empty map is the identity.
fn compose_view_maps(a: &[(String, String)], b: &[(String, String)]) -> Vec<(String, String)> {
    if a.is_empty() {
        return b.to_vec();
    }
    if b.is_empty() {
        return a.to_vec();
    }
    let bm: alloc::collections::BTreeMap<&String, &String> =
        b.iter().map(|(k, v)| (k, v)).collect();
    a.iter()
        .map(|(outer, mid)| {
            let base = bm
                .get(mid)
                .map(|s| (*s).clone())
                .unwrap_or_else(|| mid.clone());
            (outer.clone(), base)
        })
        .collect()
}

/// v7.39 (round 267) — why a view is not auto-updatable. PG reports a
/// specific DETAIL per reason, and when several apply it names exactly
/// one: measured precedence on PG 18.4 is set-op > DISTINCT > GROUP BY
/// > WITH > LIMIT/OFFSET > not-a-single-table, which is the order the
/// checks below run in.
///
/// `Unsupported` is the honest odd one out: the shape may well be
/// auto-updatable in PG, but SPG's redirect cannot express it yet. It
/// must not borrow a PG DETAIL it does not mean.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ViewNotUpdatable {
    SetOp,
    Distinct,
    GroupBy,
    With,
    LimitOffset,
    NotSingleTable,
    Unsupported,
}

impl ViewNotUpdatable {
    /// PG's DETAIL line, verbatim from PG 18.4.
    fn detail(self) -> &'static str {
        match self {
            Self::SetOp => {
                "Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable."
            }
            Self::Distinct => "Views containing DISTINCT are not automatically updatable.",
            Self::GroupBy => "Views containing GROUP BY are not automatically updatable.",
            Self::With => "Views containing WITH are not automatically updatable.",
            Self::LimitOffset => {
                "Views containing LIMIT or OFFSET are not automatically updatable."
            }
            Self::NotSingleTable => {
                "Views that do not select from a single table or view are not automatically \
                 updatable."
            }
            Self::Unsupported => "This view shape is not auto-updatable in SPG.",
        }
    }
}

/// The verb-dependent halves of PG's non-updatable-view error.
struct WriteVerb {
    /// "insert into" / "update" / "delete from", as it appears after
    /// "cannot " in the primary message.
    cannot: &'static str,
    /// "inserting into" / "updating" / "deleting from", for the HINT.
    gerund: &'static str,
    /// "INSERT" / "UPDATE" / "DELETE", for the trigger and rule names.
    keyword: &'static str,
}

const INSERT_VERB: WriteVerb = WriteVerb {
    cannot: "insert into",
    gerund: "inserting into",
    keyword: "INSERT",
};
const UPDATE_VERB: WriteVerb = WriteVerb {
    cannot: "update",
    gerund: "updating",
    keyword: "UPDATE",
};
const DELETE_VERB: WriteVerb = WriteVerb {
    cannot: "delete from",
    gerund: "deleting from",
    keyword: "DELETE",
};

/// Build PG's full "cannot <verb> view" error for a non-updatable view.
/// MERGE names itself in the HINT and offers only the trigger, not the
/// rule — measured on PG 18.4.
fn view_not_updatable_error(
    view_name: &str,
    verb: &WriteVerb,
    reason: ViewNotUpdatable,
    via_merge: bool,
) -> EngineError {
    let hint = if via_merge {
        alloc::format!(
            "To enable {} the view using MERGE, provide an INSTEAD OF {} trigger.",
            verb.gerund,
            verb.keyword,
        )
    } else {
        alloc::format!(
            "To enable {} the view, provide an INSTEAD OF {} trigger or an unconditional ON {} \
             DO INSTEAD rule.",
            verb.gerund,
            verb.keyword,
            verb.keyword,
        )
    };
    EngineError::Unsupported(alloc::format!(
        "cannot {} view \"{}\" DETAIL: {} HINT: {}",
        verb.cannot,
        view_name,
        reason.detail(),
        hint,
    ))
}

/// v7.39 (round 267) — the single auto-updatability judgement. The write
/// paths and `information_schema` both go through it, so the catalog
/// cannot advertise an updatability the engine does not honour (before
/// this round it advertised NO for every view while happily writing
/// through the simple ones).
pub(crate) fn view_is_auto_updatable(catalog: &spg_storage::Catalog, view_name: &str) -> bool {
    view_redirect_checked(catalog, view_name).is_ok()
}

/// v7.39 (round 268) — the view's columns that are plain base columns,
/// i.e. legal write targets. Computed (expression) columns are excluded:
/// PG keeps such a view updatable but reports is_updatable = NO for that
/// one column, and a write targeting it errors. Reading it off the same
/// redirect the write path uses keeps the catalog's per-column answer
/// and the engine's behaviour in step.
pub(crate) fn view_simple_column_names(
    catalog: &spg_storage::Catalog,
    view_name: &str,
) -> Vec<String> {
    let Ok(vr) = view_redirect_checked(catalog, view_name) else {
        return Vec::new();
    };
    if !vr.view_cols.is_empty() {
        // Filled only when the view has computed columns; the pairing
        // says which is which.
        return vr
            .view_cols
            .iter()
            .filter(|(_, base)| base.is_some())
            .map(|(n, _)| n.clone())
            .collect();
    }
    if !vr.col_map.is_empty() {
        return vr.col_map.iter().map(|(v, _)| v.clone()).collect();
    }
    // The identity shape keeps col_map empty as a fast path: every
    // column passes through from the base table under its own name.
    catalog
        .get(&vr.base)
        .map(|t| t.schema().columns.iter().map(|c| c.name.clone()).collect())
        .unwrap_or_default()
}

fn view_redirect_to_simple_base(
    catalog: &spg_storage::Catalog,
    view_name: &str,
) -> Option<ViewRedirect> {
    view_redirect_checked(catalog, view_name).ok()
}

fn view_redirect_checked(
    catalog: &spg_storage::Catalog,
    view_name: &str,
) -> Result<ViewRedirect, ViewNotUpdatable> {
    let view = catalog
        .views_all()
        .get(view_name)
        .ok_or(ViewNotUpdatable::Unsupported)?;
    let this_check = view.check_option;
    // v7.39 (round 133) — a column-rename list maps the view's column names back
    // to the primary's columns.
    let rename_cols = view.columns.clone();
    let stmt =
        spg_sql::parser::parse_statement(&view.body).map_err(|_| ViewNotUpdatable::Unsupported)?;
    let select = match stmt {
        spg_sql::ast::Statement::Select(s) => s,
        _ => return Err(ViewNotUpdatable::Unsupported),
    };
    // Simple-query shape. A WHERE is allowed, and so is ORDER BY — PG
    // auto-updates `SELECT a FROM t ORDER BY a` (measured on 18.4), and
    // ordering is meaningless to a write anyway. SPG rejected it until
    // round 267, which made an INSERT PG accepts fail here.
    //
    // The order of these checks is PG's reporting precedence, measured
    // by building views that break two rules at once.
    if !select.unions.is_empty() {
        return Err(ViewNotUpdatable::SetOp);
    }
    if select.distinct {
        return Err(ViewNotUpdatable::Distinct);
    }
    if select.group_by.is_some() || select.group_by_all || select.having.is_some() {
        return Err(ViewNotUpdatable::GroupBy);
    }
    if !select.ctes.is_empty() {
        return Err(ViewNotUpdatable::With);
    }
    if select.limit.is_some() || select.offset.is_some() {
        return Err(ViewNotUpdatable::LimitOffset);
    }
    let from = select
        .from
        .as_ref()
        .ok_or(ViewNotUpdatable::NotSingleTable)?;
    if !from.joins.is_empty() {
        return Err(ViewNotUpdatable::NotSingleTable);
    }
    if from.primary.unnest_expr.is_some() || from.primary.as_of_segment.is_some() {
        return Err(ViewNotUpdatable::NotSingleTable);
    }
    let primary_name = from.primary.name.clone();
    let is_leaf = catalog.get(&primary_name).is_some();
    // Build the view's output columns in order: each is either a simple
    // primary column (writable) or — leaf level only, v7.39 round 154 —
    // a computed expression (readable, not a write target; PG keeps the
    // view auto-updatable). The view-side name is the rename-list entry
    // when present, else the item alias, else the column's own name.
    // `*` projects the primary's columns in declaration order.
    let mut out_cols: Vec<(String, Option<String>, Option<spg_sql::ast::Expr>)> = Vec::new(); // (view-side name, base col, computed expr)
    for item in &select.items {
        match item {
            spg_sql::ast::SelectItem::Wildcard | spg_sql::ast::SelectItem::QualifiedWildcard(_) => {
                // Leaf: resolve the primary's output columns (a base
                // table's schema). Over a nested view the wildcard is the
                // identity pass-through (no per-column info needed) —
                // renamed-wildcard-over-nested-view bails at the rename
                // length check below, as before.
                if is_leaf {
                    let base = catalog
                        .get(&primary_name)
                        .ok_or(ViewNotUpdatable::Unsupported)?;
                    for c in &base.schema().columns {
                        out_cols.push((c.name.clone(), Some(c.name.clone()), None));
                    }
                }
            }
            spg_sql::ast::SelectItem::Expr { expr, alias } => match expr {
                spg_sql::ast::Expr::Column(c) => {
                    let name = alias.clone().unwrap_or_else(|| c.name.clone());
                    out_cols.push((name, Some(c.name.clone()), None));
                }
                other => {
                    // v7.39 (round 154) — a computed column keeps the view
                    // auto-updatable ONLY for plain expressions at the leaf
                    // level: an aggregate (`SELECT max(v) …`) makes the
                    // whole view non-updatable in PG, and window / SRF /
                    // subquery shapes stay conservatively rejected too.
                    if !is_leaf
                        || crate::aggregate::contains_aggregate(other)
                        || expr_mentions_subquery_or_window(other)
                    {
                        return Err(ViewNotUpdatable::Unsupported);
                    }
                    let name = alias.clone().unwrap_or_else(|| String::from("?column?"));
                    out_cols.push((name, None, Some(other.clone())));
                }
            },
        }
    }
    // A rename list overrides every view-side name positionally.
    if !rename_cols.is_empty() {
        if rename_cols.len() != out_cols.len() {
            return Err(ViewNotUpdatable::Unsupported);
        }
        for (slot, name) in out_cols.iter_mut().zip(rename_cols) {
            slot.0 = name;
        }
    }
    let computed: Vec<ComputedViewCol> = out_cols
        .iter()
        .filter_map(|(n, _, e)| {
            e.clone().map(|def| ComputedViewCol {
                name: n.clone(),
                def,
                origin_view: String::from(view_name),
                origin_col: n.clone(),
            })
        })
        .collect();
    // The simple view→base pairs. `col_map` stays EMPTY on the pure
    // identity shape (no rename, no alias, no computed column) so the
    // established fast path — no rewriting at all — is byte-identical.
    let all_simple: Vec<(String, String)> = out_cols
        .iter()
        .filter_map(|(n, b, _)| b.clone().map(|b| (n.clone(), b)))
        .collect();
    let identity = computed.is_empty() && all_simple.iter().all(|(v, b)| v.eq_ignore_ascii_case(b));
    let this_map: Vec<(String, String)> = if identity { Vec::new() } else { all_simple };
    let view_cols: Vec<(String, Option<String>)> = if computed.is_empty() {
        Vec::new()
    } else {
        out_cols
            .iter()
            .map(|(n, b, _)| (n.clone(), b.clone()))
            .collect()
    };
    let this_where = select.where_;

    // Leaf: the primary is a real base table.
    if catalog.get(&primary_name).is_some() {
        let mut chain = Vec::new();
        if let Some(w) = &this_where {
            chain.push((
                alloc::string::String::from(view_name),
                w.clone(),
                this_check,
            ));
        }
        return Ok(ViewRedirect {
            base: primary_name,
            where_at_base: this_where,
            col_map: this_map,
            computed,
            view_cols,
            check_chain: chain,
        });
    }
    // Nested: the primary is itself an auto-updatable view — recurse and compose.
    if catalog.has_view(&primary_name) {
        let inner = view_redirect_checked(catalog, &primary_name)?;
        let inner_map: alloc::collections::BTreeMap<String, String> =
            inner.col_map.iter().cloned().collect();
        let inner_cmap: alloc::collections::BTreeMap<String, spg_sql::ast::Expr> = inner
            .computed
            .iter()
            .map(|c| (c.name.clone(), c.def.clone()))
            .collect();
        // This view's WHERE references the inner view's columns; translate them
        // to base columns through the inner map (round 155 — a computed
        // reference substitutes its defining expression).
        let this_where_at_base = this_where.map(|mut w| {
            if !inner_map.is_empty() || !inner_cmap.is_empty() {
                rewrite_view_refs_to_base(&mut w, &inner_map, &inner_cmap, None);
            }
            w
        });
        let mut chain = Vec::new();
        if let Some(w) = &this_where_at_base {
            chain.push((
                alloc::string::String::from(view_name),
                w.clone(),
                this_check,
            ));
        }
        chain.extend(inner.check_chain);
        if inner.computed.is_empty() {
            return Ok(ViewRedirect {
                base: inner.base,
                where_at_base: and_optional_predicates(this_where_at_base, inner.where_at_base),
                col_map: compose_view_maps(&this_map, &inner.col_map),
                computed: Vec::new(),
                view_cols: Vec::new(),
                check_chain: chain,
            });
        }
        // v7.39 (round 155) — compose over a computed inner view: each of
        // this level's (all-simple — expressions bailed above for a
        // non-leaf) columns resolves through the inner's columns. A
        // re-exported computed column keeps the inner's origin for error
        // attribution. A wildcard pass-through (empty out_cols) adopts
        // the inner's columns unchanged.
        let inner_by_name: alloc::collections::BTreeMap<&str, &Option<String>> = inner
            .view_cols
            .iter()
            .map(|(n, b)| (n.as_str(), b))
            .collect();
        let mut composed: Vec<(String, Option<String>, Option<&ComputedViewCol>)> = Vec::new();
        if out_cols.is_empty() {
            for (n, b) in &inner.view_cols {
                let c = inner.computed.iter().find(|c| c.name == *n);
                composed.push((n.clone(), b.clone(), c));
            }
        } else {
            for (name, inner_ref, _) in &out_cols {
                let inner_ref = inner_ref.as_ref().ok_or(ViewNotUpdatable::Unsupported)?; // all-simple here
                if let Some(c) = inner.computed.iter().find(|c| c.name == *inner_ref) {
                    composed.push((name.clone(), None, Some(c)));
                } else {
                    let base = inner_by_name
                        .get(inner_ref.as_str())
                        .ok_or(ViewNotUpdatable::Unsupported)?;
                    // A simple inner column — its base name is right there.
                    composed.push((name.clone(), (*base).clone(), None));
                }
            }
        }
        let computed: Vec<ComputedViewCol> = composed
            .iter()
            .filter_map(|(n, _, c)| {
                c.map(|c| ComputedViewCol {
                    name: n.clone(),
                    def: c.def.clone(),
                    origin_view: c.origin_view.clone(),
                    origin_col: c.origin_col.clone(),
                })
            })
            .collect();
        let col_map: Vec<(String, String)> = composed
            .iter()
            .filter_map(|(n, b, _)| b.clone().map(|b| (n.clone(), b)))
            .collect();
        let view_cols: Vec<(String, Option<String>)> = composed
            .iter()
            .map(|(n, b, _)| (n.clone(), b.clone()))
            .collect();
        return Ok(ViewRedirect {
            base: inner.base,
            where_at_base: and_optional_predicates(this_where_at_base, inner.where_at_base),
            col_map,
            computed,
            view_cols,
            check_chain: chain,
        });
    }
    Err(ViewNotUpdatable::Unsupported)
}

/// v7.39 (round 154) — shapes that make a view non-auto-updatable even
/// as a "computed column": subqueries and window functions (aggregates
/// are checked separately via `contains_aggregate`).
fn expr_mentions_subquery_or_window(e: &spg_sql::ast::Expr) -> bool {
    if crate::subquery::expr_has_subquery(e) {
        return true;
    }
    let mut found = false;
    let mut probe = e.clone();
    crate::expr_analysis::rewrite_nodes_mut(&mut probe, &mut |n| {
        if matches!(n, spg_sql::ast::Expr::WindowFunction { .. }) {
            found = true;
        }
        false
    });
    found
}

/// v7.39 (round 154) — PG's error for a write that targets a computed
/// (expression) view column; the view stays auto-updatable otherwise.
/// `verb` is "insert into" / "update" / "merge into" (PG varies it by
/// statement kind).
fn view_computed_col_write_err(verb: &str, col: &str, view: &str) -> EngineError {
    EngineError::Unsupported(alloc::format!(
        "cannot {verb} column \"{col}\" of view \"{view}\" \
         DETAIL: View columns that are not columns of their base relation are not updatable."
    ))
}

/// v7.39 (round 154) — the round-133 ref rewriter extended for computed
/// view columns: a simple column renames to its base column, a computed
/// column substitutes its defining expression. `target_alias` selects
/// the context: `Some(alias)` (MERGE) rewrites only bare / alias-
/// qualified refs — source-alias refs must survive — and stamps the
/// alias onto every rewritten ref (the combined-row context resolves
/// target columns through it); `None` (single-table UPDATE / DELETE /
/// INSERT context) rewrites every ref and leaves them bare.
fn rewrite_view_refs_to_base(
    expr: &mut spg_sql::ast::Expr,
    map: &alloc::collections::BTreeMap<String, String>,
    computed: &alloc::collections::BTreeMap<String, spg_sql::ast::Expr>,
    target_alias: Option<&str>,
) {
    use spg_sql::ast::Expr;
    crate::expr_analysis::rewrite_nodes_mut(expr, &mut |n| {
        if let Expr::Column(c) = n {
            let eligible = match target_alias {
                None => true,
                Some(a) => {
                    c.qualifier.is_none()
                        || c.qualifier
                            .as_deref()
                            .is_some_and(|q| q.eq_ignore_ascii_case(a))
                }
            };
            if eligible {
                if let Some(base) = map.get(&c.name) {
                    c.name = base.clone();
                    c.qualifier = target_alias.map(Into::into);
                } else if let Some(def) = computed.get(&c.name) {
                    let mut sub = def.clone();
                    if let Some(q) = target_alias {
                        crate::expr_analysis::rewrite_nodes_mut(&mut sub, &mut |m| {
                            if let Expr::Column(mc) = m {
                                if mc.qualifier.is_none() {
                                    mc.qualifier = Some(q.into());
                                }
                                return true;
                            }
                            false
                        });
                    }
                    *n = sub;
                }
            }
            return true;
        }
        false
    });
}

/// v7.39 (round 133) — rewrite an auto-updatable view's column references in a
/// write predicate / assignment to the base table's columns (the view rename
/// map is view-col → base-col). Bare and view-qualified refs both remap; the
/// qualifier is dropped since the base is a single table.
fn rewrite_view_col_refs(
    expr: &mut spg_sql::ast::Expr,
    map: &alloc::collections::BTreeMap<String, String>,
) {
    use spg_sql::ast::Expr;
    match expr {
        Expr::Column(c) => {
            if let Some(base) = map.get(&c.name) {
                c.name = base.clone();
                c.qualifier = None;
            }
        }
        Expr::Binary { lhs, rhs, .. } => {
            rewrite_view_col_refs(lhs, map);
            rewrite_view_col_refs(rhs, map);
        }
        Expr::Unary { expr, .. } | Expr::Cast { expr, .. } | Expr::IsNull { expr, .. } => {
            rewrite_view_col_refs(expr, map)
        }
        Expr::FunctionCall { args, .. } => {
            for a in args {
                rewrite_view_col_refs(a, map);
            }
        }
        Expr::Case {
            operand,
            branches,
            else_branch,
        } => {
            if let Some(o) = operand {
                rewrite_view_col_refs(o, map);
            }
            for (c, v) in branches {
                rewrite_view_col_refs(c, map);
                rewrite_view_col_refs(v, map);
            }
            if let Some(x) = else_branch {
                rewrite_view_col_refs(x, map);
            }
        }
        _ => {}
    }
}

/// v7.39 (round 134) — rewrite a RETURNING projection on a column-renamed view:
/// each item's column refs map from view columns to base columns, but the
/// OUTPUT name stays the view column (PG labels `RETURNING a` "a", not the base
/// "id"). A bare `*` expands to `base AS view` for every mapped column.
fn rewrite_view_returning_items(
    items: &[spg_sql::ast::SelectItem],
    col_map: &[(String, String)],
    computed: &[ComputedViewCol],
    view_cols: &[(String, Option<String>)],
) -> Vec<spg_sql::ast::SelectItem> {
    use spg_sql::ast::{Expr, SelectItem};
    let map: alloc::collections::BTreeMap<String, String> = col_map.iter().cloned().collect();
    let cmap: alloc::collections::BTreeMap<String, Expr> = computed
        .iter()
        .map(|c| (c.name.clone(), c.def.clone()))
        .collect();
    let mut out: Vec<SelectItem> = Vec::new();
    for it in items {
        match it {
            // `RETURNING *` / `v.*` → the view's columns (base value, view
            // name). v7.39 (round 154) — a computed column projects its
            // defining expression (evaluated on the post-write row, as PG);
            // `view_cols` carries the declaration order when one exists.
            SelectItem::Wildcard | SelectItem::QualifiedWildcard(_) => {
                if view_cols.is_empty() {
                    for (view_col, base_col) in col_map {
                        out.push(SelectItem::Expr {
                            expr: Expr::Column(spg_sql::ast::ColumnName {
                                qualifier: None,
                                name: base_col.clone(),
                            }),
                            alias: Some(view_col.clone()),
                        });
                    }
                } else {
                    for (view_col, base_col) in view_cols {
                        let expr = match base_col {
                            Some(b) => Expr::Column(spg_sql::ast::ColumnName {
                                qualifier: None,
                                name: b.clone(),
                            }),
                            None => cmap
                                .get(view_col)
                                .cloned()
                                .expect("view_cols and computed come from the same projection"),
                        };
                        out.push(SelectItem::Expr {
                            expr,
                            alias: Some(view_col.clone()),
                        });
                    }
                }
            }
            SelectItem::Expr { expr, alias } => {
                // Preserve the view column name as the output name before the
                // ref rewrite renames it to the base column.
                let out_alias = if alias.is_some() {
                    alias.clone()
                } else if let Expr::Column(c) = expr
                    && (map.contains_key(&c.name) || cmap.contains_key(&c.name))
                {
                    Some(c.name.clone())
                } else {
                    alias.clone()
                };
                let mut e = expr.clone();
                rewrite_view_refs_to_base(&mut e, &map, &cmap, None);
                out.push(SelectItem::Expr {
                    expr: e,
                    alias: out_alias,
                });
            }
        }
    }
    out
}

/// v7.39 (round 132/136) — a pending `WITH CHECK OPTION` to enforce on the base
/// rows a write through an auto-updatable view produces. `chain` is the per-view
/// `(name, qual_at_base, own_check_option)` list (outermost first) down the
/// nested-view stack; `written_opt` is the outermost (written) view's option.
#[derive(Clone)]
struct ViewCheck {
    written_opt: u8,
    chain: Vec<(String, spg_sql::ast::Expr, u8)>,
}

/// v7.38 (read01 P6.46) — AND two optional predicates (the view's WHERE and the
/// caller's WHERE) into one.
fn and_optional_predicates(
    a: Option<spg_sql::ast::Expr>,
    b: Option<spg_sql::ast::Expr>,
) -> Option<spg_sql::ast::Expr> {
    match (a, b) {
        (Some(x), Some(y)) => Some(spg_sql::ast::Expr::Binary {
            lhs: alloc::boxed::Box::new(x),
            op: spg_sql::ast::BinOp::And,
            rhs: alloc::boxed::Box::new(y),
        }),
        (Some(x), None) | (None, Some(x)) => Some(x),
        (None, None) => None,
    }
}
use spg_storage::{ColumnSchema, DataType, Row, StorageError, Value};

use crate::eval::{EvalContext, EvalError};
use crate::{
    CancelToken, Engine, EngineError, QueryResult, any_column_changed, apply_fk_child_step,
    apply_on_conflict_assignments, build_projection, canonicalize_set_value, check_unsigned_range,
    coerce_value, enforce_check_constraints, enforce_enum_label, enforce_fk_inserts,
    enforce_not_null, enforce_unique_index_inserts, enforce_unique_updates,
    enforce_uniqueness_inserts, eval, eval_runtime_default_free, expr_has_subquery,
    literal_expr_to_value, literal_expr_to_value_in, lookup_row_position_by_keys,
    on_conflict_keys_exist, plan_fk_parent_deletions, plan_fk_parent_updates,
    resolve_column_default_free, triggers, try_index_seek_positions, try_pk_predicate,
    value_to_literal_expr_permissive,
};

/// Pre-borrow snapshots gathered by `prepare_insert_snapshots` for the
/// INSERT row loop, taken before the mutable catalog borrow opens.
struct InsertSnapshots {
    clock: Option<crate::ClockFn>,
    before_insert_triggers: Vec<(spg_storage::FunctionDef, String, String)>,
    after_insert_triggers: Vec<(spg_storage::FunctionDef, String, String)>,
    trigger_session_cfg: Option<String>,
    enum_label_lookup: alloc::collections::BTreeMap<usize, Vec<String>>,
    set_variant_lookup: alloc::collections::BTreeMap<usize, Vec<String>>,
    seq_floors: alloc::collections::BTreeMap<usize, i64>,
}

impl Engine {
    /// v4.4 `UPDATE <table> SET col = expr [, ...] [WHERE cond]`.
    /// Filter pass uses the same WHERE eval as `exec_select`. Per
    /// matched row, evaluate each RHS expression against the *old*
    /// row, then call `Table::update_row` which rebuilds indices.
    /// Indexed columns are correctly reflected because rebuild
    /// happens after the cell rewrite.
    /// v7.39 (round 132/136) — enforce a view's `WITH CHECK OPTION` over the base
    /// rows a write produced. Each row must satisfy the qual of every checked
    /// view in the chain (only a definite TRUE passes — NULL / FALSE fail,
    /// mirroring row visibility). PG's cascade rule: the written view's qual is
    /// always checked; an underlying view's qual is checked iff the written view
    /// is CASCADED or that underlying view itself has a check option (and a
    /// CASCADED underlying re-arms the cascade for its own underlyings). The
    /// error names the specific failing view (SQLSTATE 44000 + failing-row
    /// DETAIL). Constant `2` = CASCADED, `1` = LOCAL, `0` = none.
    fn enforce_view_check(
        &self,
        check: &ViewCheck,
        rows: &[Vec<Value<'static>>],
        schema_cols: &[ColumnSchema],
        base_table: &str,
    ) -> Result<(), EngineError> {
        let ctx = self.ev_ctx(schema_cols, Some(base_table));
        let cancel = CancelToken::none();
        for vals in rows {
            let row = Row::new(vals.clone());
            let mut cascade = check.written_opt == 2;
            for (view_name, qual, opt) in &check.chain {
                if cascade || *opt != 0 {
                    let v = self.eval_expr_with_correlated(qual, &row, &ctx, cancel, None)?;
                    if !matches!(v, Value::Bool(true)) {
                        let failing = crate::constraints::format_failing_row(vals);
                        return Err(EngineError::Unsupported(alloc::format!(
                            "new row violates check option for view \"{view_name}\" \
                             DETAIL: Failing row contains ({failing})."
                        )));
                    }
                }
                if *opt == 2 {
                    cascade = true;
                }
            }
        }
        Ok(())
    }

    pub(crate) fn exec_update_cancel(
        &mut self,
        stmt: &spg_sql::ast::UpdateStatement,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let table = stmt.table.clone();
        self.bump_table_change(&table);
        self.exec_update_cancel_inner(stmt, None, cancel)
            .map_err(|e| enrich_not_null(e, &table))
    }

    /// v7.39 (round 533) — resolve the UNQUALIFIED assignment leaves that
    /// `UPDATE … FROM` could not classify at parse time.
    ///
    /// A name that is only the SOURCE's becomes the same correlated
    /// scalar subquery the parser builds for a qualified one. A name
    /// that is in BOTH is refused with PG's wording. A name that is only
    /// the target's, or neither, is left exactly as it was — so a
    /// statement with nothing to resolve is returned untouched and pays
    /// no clone.
    fn resolve_update_from_leaves(
        &self,
        stmt: &spg_sql::ast::UpdateStatement,
        target_cols: &[ColumnSchema],
    ) -> Result<Option<spg_sql::ast::UpdateStatement>, EngineError> {
        let Some(src) = stmt.from_sources.as_deref() else {
            return Ok(None);
        };
        // The source relations' columns, by the names they are known by.
        let mut source_cols: Vec<alloc::string::String> = Vec::new();
        for t in core::iter::once(&src.from.primary).chain(src.from.joins.iter().map(|j| &j.table))
        {
            if let Some(tbl) = self.active_catalog().get(&t.name) {
                for c in &tbl.schema().columns {
                    source_cols.push(c.name.clone());
                }
            }
        }
        if source_cols.is_empty() {
            return Ok(None);
        }
        let in_target = |n: &str| target_cols.iter().any(|c| c.name.eq_ignore_ascii_case(n));
        let in_source = |n: &str| source_cols.iter().any(|c| c.eq_ignore_ascii_case(n));
        let mut out = stmt.clone();
        let mut changed = false;
        for (_, expr) in &mut out.assignments {
            resolve_unqualified_source_leaf(
                expr,
                &in_target,
                &in_source,
                &src.from,
                src.sub_where.as_ref(),
                &mut changed,
            )?;
        }
        Ok(changed.then_some(out))
    }

    /// v7.39 (round 621) — a partition-parent UPDATE that touches the key
    /// column, done as PG does it: the row moves to the partition its new key
    /// belongs to. See the call site for the shape and the failure contract.
    fn exec_partition_key_update(
        &mut self,
        stmt: &spg_sql::ast::UpdateStatement,
        _key_cols: &[String],
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        use spg_sql::ast::{DeleteStatement, InsertStatement, Overriding, SelectItem};
        let parent = stmt.table.clone();
        let children = crate::partition::children_of_parent(self.active_catalog(), &parent);
        // Phase 1 — capture EVERY child's victims before reinserting any.
        // Interleaving the two was the first cut, and it double-moved rows: a
        // child processed later saw the rows an earlier child had just routed
        // into it, deleted them again, and RETURNING reported the row twice.
        let mut captured: Vec<(String, Vec<ColumnSchema>, Vec<Row<'static>>)> = Vec::new();
        let mut total: usize = 0;
        for child in children {
            let del = DeleteStatement {
                ctes: Vec::new(),
                table: child.clone(),
                only: false,
                alias: None,
                where_: stmt.where_.clone(),
                order_limit: None,
                returning: Some(alloc::vec![SelectItem::Wildcard]),
            };
            let (pre_cols, pre_rows) = match self.exec_delete_cancel(&del, cancel)? {
                QueryResult::Rows { columns, rows } => (columns, rows),
                _ => continue,
            };
            if pre_rows.is_empty() {
                continue;
            }
            total += pre_rows.len();
            captured.push((child, pre_cols, pre_rows));
        }
        // Phase 2 — post-images from the PRE-image (PG's rule: `SET v = v +
        // 100` reads the old v), then one reinsert through the parent's
        // routing. On any failure every child's pre-images go back first.
        let mut moved_rows: Vec<Row<'static>> = Vec::new();
        let mut columns_seen: Option<Vec<ColumnSchema>> = None;
        let mut all_pre: Vec<(String, Vec<String>, Vec<Vec<Expr>>)> = Vec::new();
        let mut all_post: Vec<Vec<Expr>> = Vec::new();
        let mut post_col_names: Vec<String> = Vec::new();
        let mut result = Ok(());
        'children: for (child, pre_cols, pre_rows) in &captured {
            let ctx = self.ev_ctx(pre_cols, Some(child));
            let mut corr_memo = crate::memoize::MemoizeCache::default();
            let mut pre_tuples: Vec<Vec<Expr>> = Vec::with_capacity(pre_rows.len());
            for row in pre_rows {
                let mut pre_t = Vec::with_capacity(pre_cols.len());
                let mut post_t = Vec::with_capacity(pre_cols.len());
                for (i, col) in pre_cols.iter().enumerate() {
                    let old = row.values.get(i).cloned().unwrap_or(Value::Null);
                    match crate::substitute::value_to_literal_expr_permissive(old.clone()) {
                        Ok(e) => pre_t.push(e),
                        Err(e) => {
                            result = Err(e);
                            pre_tuples.push(pre_t);
                            all_pre.push((
                                child.clone(),
                                pre_cols.iter().map(|c| c.name.clone()).collect(),
                                pre_tuples,
                            ));
                            break 'children;
                        }
                    }
                    let assigned = stmt
                        .assignments
                        .iter()
                        .find(|(c, _)| c.eq_ignore_ascii_case(&col.name));
                    let new_v = match assigned {
                        Some((_, expr)) => match self.eval_expr_with_correlated(
                            expr,
                            row,
                            &ctx,
                            cancel,
                            Some(&mut corr_memo),
                        ) {
                            Ok(v) => v,
                            Err(e) => {
                                result = Err(e);
                                pre_tuples.push(pre_t);
                                all_pre.push((
                                    child.clone(),
                                    pre_cols.iter().map(|c| c.name.clone()).collect(),
                                    pre_tuples,
                                ));
                                break 'children;
                            }
                        },
                        None => old,
                    };
                    match crate::substitute::value_to_literal_expr_permissive(new_v) {
                        Ok(e) => post_t.push(e),
                        Err(e) => {
                            result = Err(e);
                            pre_tuples.push(pre_t);
                            all_pre.push((
                                child.clone(),
                                pre_cols.iter().map(|c| c.name.clone()).collect(),
                                pre_tuples,
                            ));
                            break 'children;
                        }
                    }
                }
                if stmt.returning.is_some() {
                    let vals: Vec<Value<'static>> = post_t
                        .iter()
                        .map(|e| literal_expr_to_value(e.clone()).unwrap_or(Value::Null))
                        .collect();
                    moved_rows.push(Row::new(vals));
                }
                pre_tuples.push(pre_t);
                all_post.push(post_t);
            }
            if post_col_names.is_empty() {
                post_col_names = pre_cols.iter().map(|c| c.name.clone()).collect();
            }
            all_pre.push((
                child.clone(),
                pre_cols.iter().map(|c| c.name.clone()).collect(),
                pre_tuples,
            ));
            if columns_seen.is_none() {
                columns_seen = Some(pre_cols.clone());
            }
        }
        let outcome = match result {
            Ok(()) if !all_post.is_empty() => self
                .exec_insert(InsertStatement {
                    ctes: Vec::new(),
                    table: parent.clone(),
                    alias: None,
                    columns: Some(post_col_names),
                    rows: all_post,
                    select_source: None,
                    on_conflict: None,
                    returning: None,
                    overriding: Overriding::None,
                    mysql_ignore: false,
                })
                .map(|_| ()),
            other => other,
        };
        if let Err(e) = outcome {
            for (child, names, tuples) in all_pre {
                if tuples.is_empty() {
                    continue;
                }
                let _ = self.exec_insert(InsertStatement {
                    ctes: Vec::new(),
                    table: child,
                    alias: None,
                    columns: Some(names),
                    rows: tuples,
                    select_source: None,
                    on_conflict: None,
                    returning: None,
                    overriding: Overriding::None,
                    mysql_ignore: false,
                });
            }
            return Err(e);
        }
        if let (Some(ret), Some(cols)) = (&stmt.returning, columns_seen) {
            let ctx = self.ev_ctx(&cols, Some(&parent));
            let mut out_rows = Vec::with_capacity(moved_rows.len());
            let mut out_cols: Vec<ColumnSchema> = Vec::new();
            for row in &moved_rows {
                let mut vals = Vec::new();
                let mut names: Vec<ColumnSchema> = Vec::new();
                for item in ret {
                    match item {
                        SelectItem::Wildcard | SelectItem::QualifiedWildcard(_) => {
                            for (i, c) in cols.iter().enumerate() {
                                vals.push(row.values.get(i).cloned().unwrap_or(Value::Null));
                                names.push(c.clone());
                            }
                        }
                        SelectItem::Expr { expr, alias } => {
                            let v = eval::eval_expr(expr, row, &ctx).map_err(EngineError::Eval)?;
                            let name = match alias {
                                Some(a) => a.clone(),
                                None => crate::select::default_output_name(expr, false),
                            };
                            names.push(ColumnSchema::new(
                                name,
                                v.data_type().unwrap_or(spg_storage::DataType::Text),
                                true,
                            ));
                            vals.push(v);
                        }
                    }
                }
                if out_cols.is_empty() {
                    out_cols = names;
                }
                out_rows.push(Row::new(vals));
            }
            return Ok(QueryResult::Rows {
                columns: out_cols,
                rows: out_rows,
            });
        }
        Ok(QueryResult::CommandOk {
            affected: total,
            modified_catalog: false,
        })
    }

    fn exec_update_cancel_inner(
        &mut self,
        stmt: &spg_sql::ast::UpdateStatement,
        view_check: Option<ViewCheck>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        // v7.37.43-T4.4 — writable CTE outer body (UPDATE).
        if !stmt.ctes.is_empty() {
            return self.exec_update_with_ctes(stmt.clone(), cancel);
        }
        // v7.39 (RLS) Phase 2 — UPDATE USING visibility: AND the policy's USING
        // predicate into WHERE so a policy-subject session only updates rows it
        // can see (a hidden row is silently skipped). None for superuser / non-
        // RLS tables (no clone).
        let rls_upd;
        let stmt = match self.rls_write_using_predicate(&stmt.table, spg_storage::PolicyCmd::Update)
        {
            Some(pred) => {
                let mut s = stmt.clone();
                s.where_ = and_optional_predicates(s.where_.take(), Some(pred));
                rls_upd = s;
                &rls_upd
            }
            None => stmt,
        };
        // v7.39 (round 139/141) — DO INSTEAD NOTHING rules narrow the UPDATE.
        // Unconditional → AND a constant FALSE. Conditional (WHERE over NEW/OLD)
        // → AND `COALESCE(NOT(cond), TRUE)` with `NEW.col` rewritten to the SET
        // expression, so the predicate — evaluated against the pre-image row —
        // reproduces the rule's post-image test. RETURNING on a fully suppressed
        // statement is rejected as PG does.
        // v7.39 (round 333, V59) — the statement as the caller wrote it: a
        // conditional instead-command rule's action runs against THESE rows.
        let unnarrowed_upd = stmt;
        let rule_blocked_upd;
        let upd_block = if self.rule_blocks_statement(&stmt.table, "UPDATE") {
            if stmt.returning.is_some() {
                return Err(crate::rules::rule_returning_error("UPDATE", &stmt.table));
            }
            Some(spg_sql::ast::Expr::Literal(spg_sql::ast::Literal::Bool(
                false,
            )))
        } else {
            self.conditional_block_predicate(&stmt.table, "UPDATE", &stmt.assignments)?
        };
        let stmt = if let Some(pred) = upd_block {
            let mut s = stmt.clone();
            s.where_ = and_optional_predicates(s.where_.take(), Some(pred));
            rule_blocked_upd = s;
            &rule_blocked_upd
        } else {
            stmt
        };
        // v7.39 (round 140/142) — rule-command forms. DO INSTEAD <command>
        // replaces the UPDATE (original op never runs, tag `UPDATE 0`); DO ALSO
        // runs the real update first, then the commands. Both bind OLD + derived
        // NEW per matching row. The guard bounds a rule → update cycle.
        if !self.rule_rewrite_active {
            let instead_upd = self.instead_command_rules(&stmt.table, "UPDATE");
            // v7.39 (round 333, V59) — an UNCONDITIONAL instead-command rule
            // replaces the statement; a CONDITIONAL one only claims the rows
            // its WHERE holds for, and the rest still run the original (the
            // block predicate above has already removed them from it).
            let (uncond, cond): (Vec<_>, Vec<_>) = instead_upd
                .into_iter()
                .partition(|r| r.when_condition.is_empty());
            if !uncond.is_empty() {
                let mut all = uncond;
                all.extend(cond);
                return self.exec_update_instead_command(unnarrowed_upd, all, cancel);
            }
            if !cond.is_empty() {
                // The actions must see the rows the CALLER asked for, not the
                // narrowed set — those are exactly the rows they replace.
                self.run_update_instead_actions(unnarrowed_upd, &cond, cancel)?;
            }
            let also_upd = self.also_rules(&stmt.table, "UPDATE");
            if !also_upd.is_empty() {
                return self.exec_update_with_also(stmt, also_upd, view_check, cancel);
            }
        }
        // v7.39 (round 137, Phase 2) — INSTEAD OF UPDATE trigger on the target
        // view fires per matching view row instead of the auto-updatable
        // redirect. Takes precedence.
        let iof_upd = self.snapshot_row_triggers(&stmt.table, "UPDATE", "INSTEAD OF");
        if !iof_upd.is_empty() {
            return self.exec_update_view_instead_of(stmt, iof_upd, cancel);
        }
        // v7.37.19 (19.13) — auto-updatable view redirect. v7.38 (P6.46) — a
        // view WHERE is AND-ed onto the caller's so only rows visible through
        // the view are updated.
        // v7.39 (round 267) — the view exists but is not auto-updatable.
        // Without this the write fell through to the base-table lookup and
        // reported `relation "<view>" does not exist`, which is not merely
        // the wrong wording — it denies the existence of an object the
        // catalog plainly has.
        if let Err(reason) = view_redirect_checked(self.active_catalog(), &stmt.table) {
            if self.active_catalog().has_view(&stmt.table) {
                return Err(view_not_updatable_error(
                    &stmt.table,
                    &UPDATE_VERB,
                    reason,
                    false,
                ));
            }
        }
        if let Some(vr) = view_redirect_to_simple_base(self.active_catalog(), &stmt.table) {
            let ViewRedirect {
                base,
                where_at_base,
                col_map,
                computed,
                view_cols,
                check_chain,
            } = vr;
            // v7.39 (round 154) — a computed view column is never a write
            // target (PG: the view stays updatable, the column doesn't).
            for (target, _) in &stmt.assignments {
                if let Some(cc) = computed.iter().find(|c| c.name == *target) {
                    return Err(view_computed_col_write_err(
                        "update",
                        &cc.origin_col,
                        &cc.origin_view,
                    ));
                }
            }
            // v7.39 (round 132/136) — WITH CHECK OPTION: the updated row must
            // still satisfy the view chain's quals. The written (outermost)
            // view's own option drives the cascade.
            let written_opt = self
                .active_catalog()
                .views_all()
                .get(&stmt.table)
                .map_or(0, |v| v.check_option);
            // v7.39 (round 152) — a lower view carrying its OWN check
            // option enforces even when the written view has none (PG,
            // r152 probe P6; enforce_view_check already walks the chain
            // that way — the gate here just failed to arm it).
            let check = if written_opt != 0 || check_chain.iter().any(|(_, _, o)| *o != 0) {
                Some(ViewCheck {
                    written_opt,
                    chain: check_chain,
                })
            } else {
                None
            };
            let mut rewritten = stmt.clone();
            // v7.39 (round 133) — a column-renamed view: rewrite SET targets and
            // WHERE / assignment expressions from view columns to base columns
            // before AND-ing the (base-column) view WHERE. Round 154 — a
            // computed column READ substitutes its defining expression.
            if !col_map.is_empty() || !computed.is_empty() {
                let map: alloc::collections::BTreeMap<String, String> =
                    col_map.iter().cloned().collect();
                let cmap: alloc::collections::BTreeMap<String, Expr> = computed
                    .iter()
                    .map(|c| (c.name.clone(), c.def.clone()))
                    .collect();
                for (target, e) in &mut rewritten.assignments {
                    if let Some(b) = map.get(target) {
                        *target = b.clone();
                    }
                    rewrite_view_refs_to_base(e, &map, &cmap, None);
                }
                if let Some(w) = &mut rewritten.where_ {
                    rewrite_view_refs_to_base(w, &map, &cmap, None);
                }
                // v7.39 (round 134) — rewrite RETURNING view cols → base cols.
                if let Some(ret) = &rewritten.returning {
                    rewritten.returning = Some(rewrite_view_returning_items(
                        ret, &col_map, &computed, &view_cols,
                    ));
                }
            }
            rewritten.table = base;
            rewritten.where_ = and_optional_predicates(where_at_base, rewritten.where_);
            return self.exec_update_cancel_inner(&rewritten, check, cancel);
        }
        // v7.37 D.47 (partial) — UPDATE on a partition parent fans out to every
        // child (the parent holds no rows of its own, so a parent-targeted UPDATE
        // would otherwise affect nothing). An UPDATE whose SET list touches the
        // partition-key column may move a row across a partition boundary, which
        // needs row movement (delete-from-source + reinsert-through-routing) — a
        // focused follow-up. Until then, reject a key-touching parent UPDATE
        // honestly rather than fan it out in place and leave a row in the wrong
        // partition (silent-wrong). A non-key UPDATE is always safe to fan out.
        // v7.39 (round 646) — an INHERITANCE parent fans out to its
        // children AND to itself; see the DELETE twin for why `only` is
        // what makes the parent expressible without recursing. There is
        // no partition key here, so the row-movement branch below cannot
        // apply: nothing moves between an inheritance parent and a child.
        // The extra test lives INSIDE the branch, not beside it. Round
        // 646 first wrote `let inherit_fanout = …` at the top of this
        // function — one statement, evaluated on every UPDATE and DELETE
        // — and `SELECT count(*) FROM d WHERE g BETWEEN 10 AND 20` went
        // 5.9 ms to 97 ms, reproduced outside the panel. Nothing about
        // that query comes near here. Rounds 641, 643 and 644 met the
        // same wall from their own directions; what a function's BODY
        // holds is a codegen decision, and the cold side of a branch is
        // not the body.
        if !stmt.only
            && (crate::partition::is_partition_parent(self.active_catalog(), &stmt.table)
                || crate::partition::has_inheritance_children(self.active_catalog(), &stmt.table))
        {
            let inherit_fanout =
                !crate::partition::is_partition_parent(self.active_catalog(), &stmt.table);
            let key_cols: Vec<String> = {
                let parent = self.active_catalog().get(&stmt.table).ok_or_else(|| {
                    EngineError::Storage(StorageError::TableNotFound {
                        name: stmt.table.clone(),
                    })
                })?;
                match &parent.schema().partition_role {
                    Some(spg_storage::PartitionRole::Parent {
                        key_column_positions,
                        ..
                    }) => key_column_positions
                        .iter()
                        .filter_map(|&p| parent.schema().columns.get(p).map(|c| c.name.clone()))
                        .collect(),
                    _ => Vec::new(),
                }
            };
            let touches_key = stmt
                .assignments
                .iter()
                .any(|(col, _)| key_cols.iter().any(|k| k.eq_ignore_ascii_case(col)));
            if touches_key {
                // v7.39 (round 621) — row movement. A key-touching parent
                // UPDATE used to be refused (the honest stand-in: fanning it
                // out in place leaves a row in the wrong partition, which is
                // silent-wrong). PG moves the row, so an ordinary
                // `UPDATE pm SET v = 120` — measured: the row lands in the
                // partition its new key belongs to, and a key no partition
                // takes raises `no partition of relation … found for row`.
                //
                // Implemented as capture + reinsert, through machinery that
                // already exists: each child's victims are taken with
                // `DELETE … RETURNING *` (one statement, so the pre-image and
                // the removal cannot disagree), the SET list is evaluated
                // against the PRE-image (PG's rule — `SET v = v + 100` reads
                // the old v), and the post-images go back through the
                // parent's own INSERT routing, which places each row or
                // raises PG's error. If that insert fails, the pre-images are
                // put back into the child they came from before the error
                // propagates, so a failed statement does not eat rows.
                return self.exec_partition_key_update(stmt, &key_cols, cancel);
            }
            let mut children =
                crate::partition::children_of_parent(self.active_catalog(), &stmt.table);
            if inherit_fanout {
                children.push(stmt.table.clone());
            }
            let mut total_affected = 0usize;
            let mut ret_columns: Option<Vec<ColumnSchema>> = None;
            let mut ret_rows: Vec<Row<'static>> = Vec::new();
            for child in children {
                let mut child_stmt = stmt.clone();
                // The parent's own term carries ONLY, which is what
                // stops the recursive call re-entering this fan-out.
                child_stmt.only = child == stmt.table;
                child_stmt.table = child;
                match self.exec_update_cancel(&child_stmt, cancel)? {
                    QueryResult::CommandOk { affected, .. } => total_affected += affected,
                    QueryResult::Rows { columns, rows } => {
                        total_affected += rows.len();
                        ret_columns = Some(columns);
                        ret_rows.extend(rows);
                    }
                }
            }
            return Ok(match ret_columns {
                Some(columns) => QueryResult::Rows {
                    columns,
                    rows: ret_rows,
                },
                None => QueryResult::CommandOk {
                    affected: total_affected,
                    modified_catalog: true,
                },
            });
        }
        // v7.12.5 — snapshot BEFORE/AFTER UPDATE row triggers + the
        // session FTS config before the table mut-borrow opens (the
        // INSERT path uses the same pattern). Empty vecs are the
        // common "no triggers on this table" fast path.
        // v7.13.0 — UPDATE triggers carry an optional `UPDATE OF
        // cols` filter. The filter is paired with each function so
        // the per-row fire loop can skip when no listed column
        // actually differs between OLD and NEW.
        let before_update_triggers = self.snapshot_update_row_triggers(&stmt.table, "BEFORE");
        let after_update_triggers = self.snapshot_update_row_triggers(&stmt.table, "AFTER");
        let trigger_session_cfg: Option<String> = self
            .session_params
            .get("default_text_search_config")
            .cloned();
        // v5.2.3: if the WHERE is a PK equality and matches a cold-
        // tier row, promote it back to the hot tier *before* the
        // hot-row walk. The promote pushes the row to the end of
        // `table.rows`, where the upcoming SET-evaluation loop will
        // pick it up and apply the assignments. Lookups for the key
        // never observe a gap because `promote_cold_row` inserts the
        // hot row before retiring the cold locator.
        if let Some(w) = &stmt.where_ {
            let schema_cols = self
                .active_catalog()
                .get(&stmt.table)
                .ok_or_else(|| {
                    EngineError::Storage(StorageError::TableNotFound {
                        name: stmt.table.clone(),
                    })
                })?
                .schema()
                .columns
                .clone();
            if let Some((col_pos, key)) = try_pk_predicate(w, &schema_cols, stmt.table.as_str())
                && let Some(idx_name) = self
                    .active_catalog()
                    .get(&stmt.table)
                    .and_then(|t| t.index_on(col_pos).map(|i| i.name.clone()))
            {
                // Promote may be a no-op (key is hot-only or absent);
                // we don't care about the return value here — the
                // subsequent hot walk will either match or not.
                let _ = self
                    .active_catalog_mut()
                    .promote_cold_row(&stmt.table, &idx_name, &key);
            } else {
                // v7.36 (cold-tier coverage) — UPDATE with a non-PK
                // WHERE on a cold-bearing table previously missed
                // cold-tier matching rows because the candidate walk
                // only iterates `(0..table.row_count())` (hot). Walk
                // each cold row, eval WHERE against it, and promote
                // every match to the hot tier so the regular SET
                // loop below picks it up. Costs one segment-page
                // read + decode per cold row scanned — bounded by
                // the table's cold-row count.
                let pre_promote_keys: Vec<spg_storage::IndexKey> = {
                    let mut keys = Vec::new();
                    // v7.39 (round 456) — `has_cold_rows_fast()` first.
                    //
                    // A profile of a range-predicated DELETE put 57.8% of
                    // self-time in `count_cold_locators`, which walks every
                    // (key, locator) pair of every BTree index — O(table) —
                    // to answer a question that is only ever asked as
                    // "> 0". That is the whole of this shape's cost scaling
                    // with table size: a one-row range DELETE costs 0.024 ms
                    // at 10k rows and 1.220 ms at 200k, while the same
                    // delete by equality (which never reaches here) stays at
                    // 0.006 ms.
                    //
                    // `has_cold_rows_fast` is the O(1) predicate v7.36 added
                    // for exactly this, and its own doc comment says the
                    // O(N) walk is "unsuitable per join stage". It is
                    // conservative — true when the cached count is stale —
                    // so it short-circuits rather than replaces: the exact
                    // walk still runs whenever cold rows might exist, and
                    // the answer is unchanged.
                    if let Some(t) = self.active_catalog().get(&stmt.table)
                        && t.has_cold_rows_fast()
                        && t.count_cold_locators() > 0
                    {
                        // v7.39 (round 524) — the cold-row scan evaluates the
                        // same predicate as the hot path and must read the
                        // same session.
                        let cold_sess = self.dml_session();
                        let ctx = eval::EvalContext::new(
                            &schema_cols,
                            Some(stmt.alias.as_deref().unwrap_or(stmt.table.as_str())),
                        )
                        .with_session(&cold_sess);
                        for (key, row) in
                            crate::constraints::iter_cold_rows_with_pk_key(self.active_catalog(), t)
                        {
                            let cond = eval::eval_expr(w, &row, &ctx).map_err(EngineError::Eval)?;
                            if crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
                                keys.push(key);
                            }
                        }
                    }
                    keys
                };
                if !pre_promote_keys.is_empty()
                    && let Some(idx_name) = self
                        .active_catalog()
                        .get(&stmt.table)
                        .and_then(crate::constraints::pk_btree_index_name)
                {
                    for key in pre_promote_keys {
                        let _ = self.active_catalog_mut().promote_cold_row(
                            &stmt.table,
                            &idx_name,
                            &key,
                        );
                    }
                }
            }
        }

        // v7.12.1 — cache session FTS config before the table
        // mut-borrow (same reason as exec_delete).
        let ts_cfg: Option<String> = self
            .session_param("default_text_search_config")
            .map(String::from);
        // v7.17.0 Phase 2.1 — snapshot the clock pointer before
        // we hold the catalog mutably so ON UPDATE runtime
        // overrides see the engine wall clock.
        let clock_for_on_update = self.clock;
        // v7.31 (mailrs round-28) — the candidate-gathering phase
        // below is READ-ONLY (it builds `planned`; the mutation
        // happens after `let _ = table`). Borrow the catalog
        // SHARED here so a correlated scalar subquery in SET / WHERE
        // can re-enter the engine read path (`eval_expr_with_correlated`
        // also takes `&self`) with the target row as its outer
        // context. The apply phase re-acquires `active_catalog_mut()`.
        let table = self.active_catalog().get(&stmt.table).ok_or_else(|| {
            EngineError::Storage(StorageError::TableNotFound {
                name: stmt.table.clone(),
            })
        })?;
        let schema_cols: Vec<ColumnSchema> = table.schema().columns.clone();
        // v7.39 (round 533) — finish the `UPDATE … FROM` lowering the
        // parser could only start. It classifies a QUALIFIED leaf and
        // leaves everything else in the target's scope, because deciding
        // an unqualified name needs both column lists:
        //
        //   UPDATE a SET v = v + d FROM b WHERE a.id = b.id
        //   PG18  110, 20      SPG  column "d" does not exist
        //
        // And where the name is in BOTH, PG refuses it while SPG quietly
        // took the target's:
        //
        //   UPDATE a SET v = shared FROM b …
        //   PG18  ERROR: column reference "shared" is ambiguous
        //   SPG   wrote the target's own value
        //
        // Before `targets` below, which borrows the assignments.
        let resolved = self.resolve_update_from_leaves(stmt, &schema_cols)?;
        let stmt = resolved.as_ref().unwrap_or(stmt);
        let table = self.active_catalog().get(&stmt.table).ok_or_else(|| {
            EngineError::Storage(StorageError::TableNotFound {
                name: stmt.table.clone(),
            })
        })?;
        // Resolve each SET target to a column position once, validate
        // up front so a typo'd column doesn't leave a partial mutation
        // behind.
        let mut targets: Vec<(usize, &Expr)> = Vec::with_capacity(stmt.assignments.len());
        for (col, expr) in &stmt.assignments {
            let pos = schema_cols
                .iter()
                .position(|c| c.name == *col)
                .ok_or_else(|| {
                    EngineError::Eval(EvalError::ColumnNotFound { name: col.clone() })
                })?;
            targets.push((pos, expr));
        }
        // v7.17.0 Phase 2.1 — for every column with an
        // `ON UPDATE CURRENT_TIMESTAMP` binding that the caller
        // did NOT explicitly set, schedule an automatic override.
        // Reuses `eval_runtime_default_free` so the same
        // canonical runtime-expression whitelist (now /
        // current_timestamp / current_date / …) governs both
        // DEFAULT and ON UPDATE.
        let mut on_update_overrides: Vec<(usize, String)> = Vec::new();
        for (i, col) in schema_cols.iter().enumerate() {
            if targets.iter().any(|(p, _)| *p == i) {
                continue;
            }
            if let Some(src) = &col.on_update_runtime {
                on_update_overrides.push((i, src.clone()));
            }
        }
        // v7.39 (read01 round 54) — carry the catalog. Without it an
        // `m = 'happy'::mood` (or ::regclass / composite / domain) in an
        // UPDATE / DELETE WHERE failed outright with "unsupported cast target
        // `::mood`" — the same SELECT worked. Catalog::clone is a structural
        // Arc bump, so this is cheap and sidesteps the &mut self borrow.
        let cat_for_ctx = self.active_catalog().clone();
        // v7.39 (round 512) — `UPDATE … WHERE ctid …`, the same hook the
        // DELETE path takes: the value is the candidate's own position, and
        // the column joins the schema only when the predicate names it.
        let wants_ctid = stmt
            .where_
            .as_ref()
            .is_some_and(crate::select::expr_references_ctid);
        let mut schema_cols = schema_cols;
        if wants_ctid {
            schema_cols.push(ColumnSchema::new(
                alloc::string::String::from(crate::select::CTID_COLUMN),
                spg_storage::DataType::Text,
                false,
            ));
        }
        let schema_cols = schema_cols;
        // v7.39 (round 533) — finish the `UPDATE … FROM` lowering the
        // parser could only start. It classifies a QUALIFIED leaf and
        // leaves everything else in the target's scope, because deciding
        // an unqualified name needs both column lists:
        //
        //   UPDATE a SET v = v + d FROM b WHERE a.id = b.id
        //   PG18  110, 20      SPG  column "d" does not exist
        //
        // And where the name is in BOTH, PG refuses it while SPG quietly
        // took the target's:
        //
        //   UPDATE a SET v = shared FROM b …
        //   PG18  ERROR: column reference "shared" is ambiguous
        //   SPG   wrote the target's own value
        // v7.39 (round 524) — with the SESSION. This context was bare, so
        // every expression in an UPDATE's SET and WHERE was evaluated by
        // an engine that knew nothing about the connection:
        //
        //   SELECT current_user                        -> unmei
        //   UPDATE w SET a = current_user              -> writes `admin`
        //   UPDATE w SET b = current_setting('app…')   -> writes empty
        //   UPDATE w SET … WHERE current_setting(…)=x  -> matches nothing
        //
        // The last one is the worst: an update gated on a request-context
        // GUC — the shape RLS policies and multi-tenant code are written
        // in — silently updated zero rows. Nothing errored in any of them.
        //
        // Cloned rather than borrowed because the row loop below needs
        // `self` mutably; a session bag is small and this is once per
        // statement.
        let sess = self.dml_session();
        let ctx = EvalContext::new(
            &schema_cols,
            Some(stmt.alias.as_deref().unwrap_or(stmt.table.as_str())),
        )
        .with_default_text_search_config(ts_cfg.as_deref())
        .with_catalog(&cat_for_ctx)
        .with_session(&sess);
        // Walk candidate rows, evaluate WHERE then SET
        // expressions. We gather (position, new_values) tuples
        // first and apply them afterwards so the WHERE/RHS
        // evaluation reads the original row state — matches PG
        // semantics (UPDATE doesn't see its own writes).
        //
        // v7.20 P4 — index seek: a single-column equality WHERE
        // on an indexed column narrows the walk from
        // O(table.rows()) to O(matches). The full WHERE still
        // re-evaluates per candidate (the seek may be an
        // over-approximation under AND-composites), so semantics
        // are unchanged. profile: the bench's `UPDATE … WHERE
        // id = $1` on a 5 000-row table was a ~1.3 ms full scan
        // per statement; with the seek it's ~2 µs.
        // v7.39 (round 490) — taken BEFORE the seek: the seek now drops
        // versions this snapshot cannot see, and the loop below skips on
        // exactly the same test, so the two must agree.
        let scan_snapshot = self.current_snapshot();
        let seek_positions: Option<Vec<usize>> = stmt.where_.as_ref().and_then(|w| {
            try_index_seek_positions(w, &schema_cols, table, stmt.table.as_str(), &scan_snapshot)
        });
        let mut planned: Vec<(usize, Vec<Value<'static>>)> = Vec::new();
        let candidate_positions: Vec<usize> = match &seek_positions {
            Some(list) => list.clone(),
            None => {
                // v7.39 (round 455) — a mutation that finds no usable index
                // walks the table, and PG counts those tuples: its
                // `pg_stat_user_tables.seq_tup_read` covers DML, not just
                // SELECT. SPG only reported from `scan_visible`, which the
                // mutation paths do not use, so an UPDATE or DELETE that
                // scanned every row reported reading none — measured in
                // round 454 with a deliberately unindexed predicate over
                // 50k rows, which came back 0.
                //
                // Silently-wrong monitoring for any write workload, and the
                // instrument the round-452 investigation needs to tell a
                // range mutation's seek from a scan.
                table.note_seq_scan();
                (0..table.row_count()).collect()
            }
        };
        // v7.37.16 (gate-on inventory) — MVCC visibility gate for the
        // UPDATE target scan. Without it, a tombstoned old version
        // matched the WHERE again and entered `planned` alongside its
        // live successor — the whole gate-on UPDATE e2e fail cluster
        // (double-apply → spurious UNIQUE violations, double triggers,
        // doubled affected counts / redo). A no-op under the default
        // gate-off: every hot row is visible.
        for (loop_n, &i) in candidate_positions.iter().enumerate() {
            // v4.5: cooperative cancel checkpoint every 256 rows so
            // a runaway UPDATE without WHERE doesn't drag past the
            // server's query-timeout watchdog.
            if loop_n.is_multiple_of(256) {
                cancel.check()?;
            }
            if !table.is_row_visible(i, &scan_snapshot) {
                continue;
            }
            let Some(row) = table.rows().get(i) else {
                continue;
            };
            // The extended row is for the PREDICATE only. The SET
            // expressions and the new-row construction below build a row of
            // the table's own arity, and handing them a row with an extra
            // column made an `UPDATE … WHERE ctid = …` die on "row arity
            // mismatch: expected 1 columns, got 2".
            let with_ctid;
            let where_row = if wants_ctid {
                let mut vals = row.values.clone();
                vals.push(Value::Tid(0, i as u32 + 1));
                with_ctid = spg_storage::Row::new(vals);
                &with_ctid
            } else {
                row
            };
            if let Some(w) = &stmt.where_ {
                // v7.31 (round-28) — correlated subqueries in the
                // UPDATE WHERE bind to the candidate row, like the
                // SELECT path; plain predicates keep the cheap
                // interpreter.
                let cond = if expr_has_subquery(w) {
                    self.eval_expr_with_correlated(w, where_row, &ctx, cancel, None)?
                } else {
                    eval::eval_expr(w, where_row, &ctx)?
                };
                if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
                    continue;
                }
            }
            let mut new_vals = row.values.clone();
            for (pos, expr) in &targets {
                // `SET col = DEFAULT` — the parser's marker call;
                // resolve the column's declared default here.
                if matches!(expr, Expr::FunctionCall { name, args }
                    if name == "__column_default" && args.is_empty())
                {
                    let v =
                        resolve_column_default_free(&schema_cols[*pos], self.clock, Some(&sess))?;
                    new_vals[*pos] = v;
                    continue;
                }
                // v7.31 (round-28) — correlated scalar subquery in
                // SET (`SET c = (SELECT … WHERE x = target.col)`)
                // binds the target row as its outer context. The
                // non-correlated case was materialised up front by
                // resolve_expr_subqueries at the UPDATE entry.
                let v = if expr_has_subquery(expr) {
                    self.eval_expr_with_correlated(expr, row, &ctx, cancel, None)?
                } else {
                    eval::eval_expr(expr, row, &ctx)?
                };
                let v = crate::conversions::normalize_composite_for_column(
                    v,
                    &schema_cols[*pos],
                    Some(&cat_for_ctx),
                )?;
                // v7.39 (round 524) — an UPDATE assigns under the same
                // session rules an INSERT does: an ambiguous date is read
                // in the session's order, and a naive value bound for a
                // timestamptz column is a wall-clock reading in the
                // session zone. Round 523 gave INSERT both and left this
                // path measuring the same way it always had.
                let sc = crate::eval::SessionCoercion::from_ctx(&ctx);
                let col = &schema_cols[*pos];
                let v = crate::eval::session_read_temporal_text(v, col.ty, sc.as_ref());
                let coerced = coerce_value(v, col.ty, &col.name, *pos)?;
                let coerced = localize_assignment_to_tstz(
                    coerced,
                    col,
                    expr_names_an_instant(
                        expr,
                        sc.as_ref().map_or(crate::eval::DateOrder::Mdy, |c| c.order),
                    ),
                    sc.as_ref(),
                );
                let coerced = crate::conversions::truncate_to_column_fsp(coerced, col);
                check_unsigned_range(&coerced, col, *pos)?;
                new_vals[*pos] = coerced;
            }
            // v7.17.0 Phase 2.1 — apply ON UPDATE overrides for
            // any column the SET clause didn't touch.
            for (pos, src) in &on_update_overrides {
                let v = eval_runtime_default_free(
                    src,
                    schema_cols[*pos].ty,
                    clock_for_on_update,
                    Some(&sess),
                )?;
                new_vals[*pos] = v;
            }
            planned.push((i, new_vals));
        }
        // v7.39 (round 413) — MySQL `UPDATE … ORDER BY … LIMIT n`. Sort the
        // matched rows by the ORDER BY keys (evaluated on the pre-image row),
        // truncate to the LIMIT, then restore position order so the FK /
        // trigger / apply passes below see the ascending-row invariant.
        // UPDATE's ctx above does not thread the engine (a long-standing site,
        // not touched here), so read the dialect straight off the session.
        if self.backslash_escapes
            && let Some(ol) = stmt.order_limit.as_deref()
        {
            if !ol.order_by.is_empty() {
                let mut tagged: Vec<(Vec<Value<'static>>, usize, Vec<Value<'static>>)> = planned
                    .into_iter()
                    .map(|(pos, new_vals)| {
                        let row = table
                            .rows()
                            .get(pos)
                            .expect("planned position was visible under scan_snapshot");
                        let keys: Vec<Value<'static>> = ol
                            .order_by
                            .iter()
                            .map(|o| eval::eval_expr(&o.expr, row, &ctx))
                            .collect::<Result<_, _>>()?;
                        Ok::<_, EngineError>((keys, pos, new_vals))
                    })
                    .collect::<Result<_, _>>()?;
                tagged.sort_by(|a, b| {
                    for (k, o) in ol.order_by.iter().enumerate() {
                        let cmp = crate::order_by_value_cmp_in(
                            o.desc,
                            o.nulls_first,
                            &a.0[k],
                            &b.0[k],
                            true,
                        );
                        if cmp != core::cmp::Ordering::Equal {
                            return cmp;
                        }
                    }
                    core::cmp::Ordering::Equal
                });
                planned = tagged.into_iter().map(|(_, i, v)| (i, v)).collect();
            }
            if let Some(n) = ol.limit {
                planned.truncate(n as usize);
            }
        }
        // planned must stay position-sorted: downstream passes
        // (FK pairing, trigger walks, the apply loop) iterate it
        // assuming ascending row order, which the full-scan path
        // guaranteed implicitly.
        planned.sort_by_key(|(i, _)| *i);
        // v7.37.7(sentori Epic 3 P1)— recompute stored generated
        // columns against each post-UPDATE candidate row, BEFORE
        // FK / CHECK / trigger passes so guards reason about the
        // computed value the same way they would for a literal cell.
        {
            let mut staged: Vec<Vec<Value<'static>>> = planned
                .iter()
                .map(|(_pos, new_vals)| new_vals.clone())
                .collect();
            apply_generated_stored_columns(&schema_cols, &mut staged)?;
            for ((_pos, new_vals), recomputed) in planned.iter_mut().zip(staged) {
                *new_vals = recomputed;
            }
        }
        // v7.6.6 — capture pre-update row values for the FK
        // enforcement passes below. `planned` carries new values
        // only; pair them with the old row.
        let plan_with_old: Vec<(usize, Vec<Value<'static>>, Vec<Value<'static>>)> = planned
            .iter()
            .map(|(pos, new_vals)| (*pos, table.rows()[*pos].values.clone(), new_vals.clone()))
            .collect();
        let self_fks = table.schema().foreign_keys.clone();
        // v7.12.5 — `affected` is computed post-BEFORE-trigger
        // below (triggers may RETURN NULL to skip individual
        // rows). The pre-trigger len shape is no longer accurate.
        // Release mutable borrow on `table` for the FK passes.
        let _ = table;
        // v7.6.6 — Stage 2a: outbound FK check. For every row whose
        // local FK columns changed, the new value must exist in the
        // parent.
        if !self_fks.is_empty() {
            let new_rows: Vec<Vec<Value<'static>>> = planned
                .iter()
                .map(|(_pos, new_vals)| new_vals.clone())
                .collect();
            // v7.39 (round 288) — a DEFERRED constraint is not checked
            // here; COMMIT (or SET CONSTRAINTS IMMEDIATE) runs it.
            let now = self.immediate_fks(&self_fks);
            if !now.is_empty() {
                enforce_fk_inserts(self.active_catalog(), &stmt.table, &now, &new_rows)?;
            }
        }
        // v7.13.0 — CHECK constraint enforcement on UPDATE
        // (mailrs round-5 G3). Predicates evaluated against the
        // candidate post-UPDATE row; false rejects the UPDATE.
        {
            let new_rows: Vec<Vec<Value<'static>>> = planned
                .iter()
                .map(|(_pos, new_vals)| new_vals.clone())
                .collect();
            // v7.39 (read01 round 117) — NOT NULL on the post-update rows,
            // pre-write with PG's `DETAIL: Failing row contains (...)`. Before
            // CHECK, matching PG's ordering.
            enforce_not_null(self.active_catalog(), &stmt.table, &new_rows)?;
            enforce_check_constraints(self.active_catalog(), &stmt.table, &new_rows, Some(&sess))?;
            // v7.39 (RLS) Phase 2 — UPDATE WITH CHECK on the post-update rows.
            let cols = self
                .active_catalog()
                .get(&stmt.table)
                .map(|t| t.schema().columns.clone())
                .unwrap_or_default();
            self.rls_check_new_rows(
                &stmt.table,
                spg_storage::PolicyCmd::Update,
                &cols,
                &new_rows,
            )?;
            // v7.39 (round 132) — WITH CHECK OPTION on the post-update rows.
            if let Some(check) = &view_check {
                self.enforce_view_check(check, &new_rows, &cols, &stmt.table)?;
            }
        }
        // v7.38 (read01 U1) — UNIQUE / PRIMARY KEY + unique-index
        // enforcement on UPDATE. The pre-image of each updated row is
        // excluded from the existing-key set (see enforce_unique_updates),
        // and only keys whose columns actually changed are scanned.
        {
            let mut changed_cols: hashbrown::HashSet<usize> = hashbrown::HashSet::new();
            for (_pos, old_vals, new_vals) in &plan_with_old {
                for (i, (o, n)) in old_vals.iter().zip(new_vals.iter()).enumerate() {
                    if o != n {
                        changed_cols.insert(i);
                    }
                }
            }
            enforce_unique_updates(
                self.active_catalog(),
                &stmt.table,
                &planned,
                &changed_cols,
                self.backslash_escapes,
            )?;
            // v7.39 (round 210) — EXCLUDE constraints on the post-update rows;
            // each updated row's pre-image is excluded from the scan.
            let exclusions = self
                .active_catalog()
                .get(&stmt.table)
                .map(|t| t.schema().exclusion_constraints.clone())
                .unwrap_or_default();
            crate::constraints::enforce_exclusion_updates(
                self.active_catalog(),
                &stmt.table,
                &exclusions,
                &planned,
            )?;
        }
        // v7.6.6 — Stage 2b: inbound FK check. For every row that
        // changed value in a column that *some other table* uses as
        // a FK parent column, react per `on_update` action.
        let child_plan =
            plan_fk_parent_updates(self.active_catalog(), &stmt.table, &plan_with_old)?;
        // Stage 3a — apply each child-side action.
        for step in &child_plan {
            apply_fk_child_step(self.active_catalog_mut(), step)?;
        }
        // v7.37.15 (Phase C.3, step 4b) — read the in-place kill switch
        // and the writer version BEFORE the table mut borrow. When on,
        // UPDATE tombstones the old row version (xmax = v) and appends
        // the new version (xmin = v) instead of an in-place replace; the
        // now-uniformly-gated readers hide the old version and show the
        // new one, vacuum reclaims the old later. Default OFF → legacy
        // in-place update_row, byte-for-byte unchanged.
        let inplace = self.mvcc_inplace();
        let v = self.writer_version_for_current_stmt();
        // v7.39 (round 426) — read the dialect BEFORE the table mut-borrow
        // opens; the affected-count rule below needs it.
        let mysql_changed_count = self.backslash_escapes;
        // Stage 3b — apply the original UPDATE.
        let table = self
            .active_catalog_mut()
            .get_mut(&stmt.table)
            .ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.table.clone(),
                })
            })?;
        // v7.12.5 — fire BEFORE/AFTER UPDATE row-level triggers
        // around the apply loop. BEFORE sees NEW=candidate +
        // OLD=current; may rewrite NEW or RETURN NULL to skip.
        // AFTER sees NEW=post-write + OLD=pre-write (both read-
        // only).
        //
        // Filter `planned` through the BEFORE pass first so the
        // RETURNING snapshot reflects what actually got written
        // (triggers may rewrite cells, including a cancellation).
        let mut applied_after_before: Vec<(usize, Row, Row)> = Vec::with_capacity(planned.len());
        // v7.12.7 — embedded SQL queue.
        let mut deferred_embedded: Vec<triggers::DeferredEmbeddedStmt> = Vec::new();
        let mut raised_notices: Vec<(crate::NoticeSeverity, alloc::string::String)> = Vec::new();
        for (pos, new_vals) in &planned {
            let old_row = table.rows()[*pos].clone();
            let mut new_row = Row::new(new_vals.clone());
            let mut skip = false;
            for (fd, filter, when, tgname) in &before_update_triggers {
                // v7.13.0 — `UPDATE OF cols` filter (mailrs round-5
                // G7). Skip this trigger when the filter is set and
                // no listed column actually differs between OLD and
                // NEW for this row.
                if !filter.is_empty()
                    && !any_column_changed(filter, &schema_cols, &old_row, &new_row)
                {
                    continue;
                }
                // v7.39 (round 138) — WHEN filter over OLD / NEW.
                if !triggers::trigger_when_holds(
                    when,
                    Some(&new_row),
                    Some(&old_row),
                    &schema_cols,
                )? {
                    continue;
                }
                let raise_sink = triggers::NoticeSink::default();
                let fired = triggers::fire_row_trigger(
                    fd,
                    Some(new_row.clone()),
                    Some(&old_row),
                    &stmt.table,
                    &schema_cols,
                    &[],
                    trigger_session_cfg.as_deref(),
                    false,
                    &triggers::TgMeta {
                        op: "UPDATE",
                        name: tgname,
                        level: "ROW",
                    },
                    Some(&raise_sink),
                );
                raised_notices.extend(raise_sink.into_inner());
                let (outcome, deferred) = fired.map_err(|e| {
                    EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}")))
                })?;
                deferred_embedded.extend(deferred);
                match outcome {
                    triggers::TriggerOutcome::Row(r) => new_row = r,
                    triggers::TriggerOutcome::Skip => {
                        skip = true;
                        break;
                    }
                }
            }
            if !skip {
                // v7.39 (read01 round 82) — recompute stored generated columns
                // over the BEFORE trigger's OUTPUT. The pre-loop pass above ran
                // before the triggers, so a `w GENERATED AS (v*2)` kept the value
                // for the pre-trigger `v` when a BEFORE UPDATE trigger changed
                // `NEW.v`. PG's order is BEFORE trigger → generated → write.
                if !before_update_triggers.is_empty() {
                    let mut one = [core::mem::take(&mut new_row.values)];
                    apply_generated_stored_columns(&schema_cols, &mut one)?;
                    new_row.values = core::mem::take(&mut one[0]);
                }
                applied_after_before.push((*pos, new_row, old_row));
            }
        }
        // v7.9.4 — snapshot post-update values for RETURNING (post-
        // BEFORE-trigger because triggers can rewrite cells).
        let updated_for_returning: Vec<Vec<Value<'static>>> = if stmt.returning.is_some() {
            applied_after_before
                .iter()
                .map(|(_pos, new_row, _old)| new_row.values.clone())
                .collect()
        } else {
            Vec::new()
        };
        // v7.39 (read01 round 126) — pre-update snapshot for RETURNING OLD.*.
        let old_for_returning: Vec<Vec<Value<'static>>> = if stmt.returning.is_some() {
            applied_after_before
                .iter()
                .map(|(_pos, _new, old_row)| old_row.values.clone())
                .collect()
        } else {
            Vec::new()
        };
        // v7.39 (round 426) — MySQL reports rows CHANGED, not rows matched:
        // `UPDATE t SET v = v` over three rows answers `Rows matched: 3
        // Changed: 0`, and ROW_COUNT() reads the 0. PG's UPDATE tag counts
        // every matched row (each gets a new row version), so this is
        // dialect-gated. The comparison is against the pre-image the apply
        // loop already carries — no extra read.
        let affected = if mysql_changed_count {
            applied_after_before
                .iter()
                .filter(|(_pos, new_row, old_row)| new_row.values != old_row.values)
                .count()
        } else {
            applied_after_before.len()
        };
        // Apply, then fire AFTER triggers per row. AFTER runs read-
        // only against the freshly-written row; v7.12.4-shape
        // assignment errors with a clear message.
        // v7.37.17 (Phase E4 fix) — collect (old, new) RowId pairs for
        // the in-place UPDATEs so the RC rebase keeps each UPDATE's
        // tombstone+insert atomic under write-write conflicts.
        let mut update_rid_pairs: Vec<(
            spg_storage::row_header::RowId,
            spg_storage::row_header::RowId,
        )> = Vec::new();
        for (pos, new_row, old_row) in applied_after_before {
            if inplace {
                // MVCC: tombstone the old version (xmax = v) + append the
                // new version (xmin = v). Appending does not shift earlier
                // physical positions, so later `pos` values stay valid.
                let old_rid = table.rowids().get(pos).copied();
                let _ = table.mark_row_deleted(pos, v);
                table
                    .insert_with_xmin(Row::new(new_row.values.clone()), v)
                    .map_err(EngineError::Storage)?;
                if let (Some(o), Some(n)) = (
                    old_rid,
                    table
                        .rowids()
                        .get(table.rowids().len().wrapping_sub(1))
                        .copied(),
                ) {
                    update_rid_pairs.push((o, n));
                }
            } else {
                table.update_row(pos, new_row.values.clone())?;
            }
            for (fd, filter, when, tgname) in &after_update_triggers {
                if !filter.is_empty()
                    && !any_column_changed(filter, &schema_cols, &old_row, &new_row)
                {
                    continue;
                }
                // v7.39 (round 138) — WHEN filter over OLD / NEW.
                if !triggers::trigger_when_holds(
                    when,
                    Some(&new_row),
                    Some(&old_row),
                    &schema_cols,
                )? {
                    continue;
                }
                let raise_sink = triggers::NoticeSink::default();
                let fired = triggers::fire_row_trigger(
                    fd,
                    Some(new_row.clone()),
                    Some(&old_row),
                    &stmt.table,
                    &schema_cols,
                    &[],
                    trigger_session_cfg.as_deref(),
                    true,
                    &triggers::TgMeta {
                        op: "UPDATE",
                        name: tgname,
                        level: "ROW",
                    },
                    Some(&raise_sink),
                );
                raised_notices.extend(raise_sink.into_inner());
                let (_outcome, deferred) = fired.map_err(|e| {
                    EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}")))
                })?;
                deferred_embedded.extend(deferred);
            }
        }
        let _ = table;
        // v7.37.17 (Phase E4 fix) — persist the UPDATE pairs on the
        // open tx (no-op in autocommit).
        self.record_update_pairs(&stmt.table, update_rid_pairs);
        // v7.12.7 — drain trigger-emitted embedded SQL for this UPDATE.
        self.queue_raised(raised_notices);
        self.execute_deferred_trigger_stmts(deferred_embedded, cancel)?;
        // v6.2.1 — auto-analyze modified-row tracking for UPDATE.
        // This connection's slot, not the engine-wide predicate: with a
        // shared engine the global one is true while ANY connection has a
        // transaction open, and an autocommit write then went uncounted.
        // The table never crosses the analyze threshold, its statistics
        // stay stale, and the planner keeps choosing from them — measured
        // in round 796, 200 autocommit inserts counted as none.
        if self.catalog_change_is_committed() && affected > 0 {
            self.statistics
                .record_modifications(&stmt.table, affected as u64);
        }
        // v7.37.16 — autovacuum trigger: an in-place UPDATE tombstones
        // the pre-update versions; reclaim when the dead-row meter
        // crosses the threshold (no-op gate-off / in-tx / below it).
        if affected > 0 {
            self.maybe_autovacuum(&stmt.table);
        }
        // v7.9.4 — RETURNING projection. v7.39 (round 126) — UPDATE exposes both
        // OLD (pre-update) and NEW (post-update = the default row).
        if let Some(items) = &stmt.returning {
            let new_for_returning = updated_for_returning.clone();
            return self.build_returning_rows_old_new(
                &stmt.table,
                stmt.alias.as_deref(),
                items,
                updated_for_returning,
                Some(old_for_returning),
                Some(new_for_returning),
            );
        }
        Ok(QueryResult::CommandOk {
            affected,
            modified_catalog: self.catalog_change_is_committed(),
        })
    }

    /// v4.4 `DELETE FROM <table> [WHERE cond]`. Collects matching
    /// positions then delegates to `Table::delete_rows` (single index
    /// rebuild for the batch).
    /// v7.17.0 Phase 3.P0-42 — SQL:2003 / PG 15+ `MERGE` execution.
    ///
    /// Semantics:
    ///   * Resolve `target` and `source` tables (catalog reads).
    ///   * Build a combined `(target_alias.col, source_alias.col)`
    ///     schema so the ON / WHEN AND / SET / VALUES expressions
    ///     resolve through the standard qualifier-aware resolver.
    ///   * Pass 1: walk every source row × every target hot row,
    ///     evaluate ON, then pick the first WHEN clause that fits
    ///     (`Matched` if any target row matched, `NotMatched`
    ///     otherwise; AND-condition must hold). Collect the action
    ///     plan as `(deletes, updates, inserts)` so the apply pass
    ///     reads the original target row state.
    ///   * Pass 2: apply the plan against the target's mutable row
    ///     vector. Deletes execute by index in descending order so
    ///     earlier indices remain stable; updates next; inserts
    ///     last (matching PG's "INSERT branch sees the post-delete
    ///     state" behaviour for the common upsert shape).
    ///
    /// v7.17 simplifications (documented limitations):
    ///   * No triggers / WAL plumbing (MVP); MERGE rows don't fire
    ///     INSERT / UPDATE / DELETE row triggers in v7.17.
    ///   * No cardinality check (PG-canonical: "MERGE command
    ///     cannot affect row a second time" — SPG silently applies
    ///     the last action for a target row covered twice).
    ///   * Source must be a catalog-resolvable table (no subquery
    ///     source); RETURNING / BY SOURCE / BY TARGET unsupported.
    pub(crate) fn exec_merge_cancel(
        &mut self,
        stmt: &spg_sql::ast::MergeStatement,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        // v7.39 (round 149) — writable CTE outer body (MERGE): PG 15
        // allows `WITH <ctes> MERGE INTO …`; each CTE materialises
        // first and its alias resolves as a source relation.
        if !stmt.ctes.is_empty() {
            return self.exec_merge_with_ctes(stmt.clone(), cancel);
        }
        // v7.39 (round 148, PG17) — MERGE INTO an auto-updatable view rewrites
        // to the base table: column names map view → base (qualified by the
        // view's alias), the view's WHERE narrows which target rows the merge
        // can see, and a positional INSERT follows the VIEW's column order.
        // v7.39 (round 267) — as above; MERGE reports the verb of its
        // FIRST WHEN clause (measured on PG 18.4) and names MERGE in the
        // HINT instead of offering a rewrite rule.
        if let Err(reason) = view_redirect_checked(self.active_catalog(), &stmt.target) {
            if self.active_catalog().has_view(&stmt.target) {
                let verb = stmt
                    .clauses
                    .iter()
                    .find_map(|c| match c.action {
                        spg_sql::ast::MergeAction::Insert { .. } => Some(&INSERT_VERB),
                        spg_sql::ast::MergeAction::Update { .. } => Some(&UPDATE_VERB),
                        spg_sql::ast::MergeAction::Delete => Some(&DELETE_VERB),
                        _ => None,
                    })
                    .unwrap_or(&INSERT_VERB);
                return Err(view_not_updatable_error(&stmt.target, verb, reason, true));
            }
        }
        if let Some(vr) = view_redirect_to_simple_base(self.active_catalog(), &stmt.target) {
            let mut s = stmt.clone();
            let alias = s.target_alias.clone().unwrap_or_else(|| s.target.clone());
            // Keep resolving `<view>.col` references: the view name stays the
            // target's alias while the storage target becomes the base table.
            s.target_alias = Some(alias.clone());
            s.target = vr.base.clone();
            if !vr.col_map.is_empty() || !vr.computed.is_empty() {
                let map: alloc::collections::BTreeMap<String, String> =
                    vr.col_map.iter().cloned().collect();
                let cmap: alloc::collections::BTreeMap<String, Expr> = vr
                    .computed
                    .iter()
                    .map(|c| (c.name.clone(), c.def.clone()))
                    .collect();
                let rw = |e: &mut Expr| {
                    rewrite_view_refs_to_base(e, &map, &cmap, Some(&alias));
                };
                rw(&mut s.on);
                for cl in &mut s.clauses {
                    if let Some(cond) = &mut cl.condition {
                        rw(cond);
                    }
                    match &mut cl.action {
                        spg_sql::ast::MergeAction::Update { assignments } => {
                            for (col, e) in assignments {
                                // v7.39 (round 154) — a computed view column
                                // is never a write target.
                                if let Some(cc) = vr.computed.iter().find(|cc| cc.name == *col) {
                                    return Err(view_computed_col_write_err(
                                        "merge into",
                                        &cc.origin_col,
                                        &cc.origin_view,
                                    ));
                                }
                                if let Some(b) = map.get(col) {
                                    *col = b.clone();
                                }
                                rw(e);
                            }
                        }
                        spg_sql::ast::MergeAction::Insert { columns, values } => {
                            if columns.is_empty() {
                                // Positional tuples follow the VIEW's column
                                // order; make that explicit as base columns.
                                // Round 154 — with computed columns present
                                // the order comes from `view_cols`; a tuple
                                // value landing on a computed slot errors.
                                if vr.computed.is_empty() {
                                    *columns = vr.col_map.iter().map(|(_, b)| b.clone()).collect();
                                } else {
                                    let n = values.len();
                                    let mut cols: Vec<String> = Vec::with_capacity(n);
                                    for (view_col, base_col) in vr.view_cols.iter().take(n) {
                                        match base_col {
                                            Some(b) => cols.push(b.clone()),
                                            None => {
                                                let cc = vr
                                                    .computed
                                                    .iter()
                                                    .find(|cc| cc.name == *view_col)
                                                    .expect("computed slot has an entry");
                                                return Err(view_computed_col_write_err(
                                                    "merge into",
                                                    &cc.origin_col,
                                                    &cc.origin_view,
                                                ));
                                            }
                                        }
                                    }
                                    *columns = cols;
                                }
                            } else {
                                for c in columns.iter_mut() {
                                    if let Some(cc) = vr.computed.iter().find(|cc| cc.name == *c) {
                                        return Err(view_computed_col_write_err(
                                            "merge into",
                                            &cc.origin_col,
                                            &cc.origin_view,
                                        ));
                                    }
                                    if let Some(b) = map.get(c) {
                                        *c = b.clone();
                                    }
                                }
                            }
                            for e in values {
                                rw(e);
                            }
                        }
                        _ => {}
                    }
                }
                // v7.39 (round 152) — RETURNING through a column-renamed
                // view: rewrite view-column references to base columns
                // while keeping the VIEW name as the output name (PG).
                // Qualifier-aware: bare refs and refs qualified by the
                // view alias / OLD / NEW remap; source-alias refs don't.
                if let Some(items) = &mut s.returning {
                    // Round 154 — an OLD./NEW.-qualified reference to a
                    // COMPUTED view column would need the expression
                    // evaluated on the pre-/post-image specifically; the
                    // substitution below is post-image only. Honest error
                    // instead of a silently-wrong value.
                    for it in items.iter() {
                        if let spg_sql::ast::SelectItem::Expr { expr, .. } = it {
                            let mut bad = false;
                            crate::expr_analysis::rewrite_nodes_mut(&mut expr.clone(), &mut |n| {
                                if let Expr::Column(c) = n {
                                    if c.qualifier.as_deref().is_some_and(|q| {
                                        q.eq_ignore_ascii_case("old")
                                            || q.eq_ignore_ascii_case("new")
                                    }) && vr.computed.iter().any(|cc| cc.name == c.name)
                                    {
                                        bad = true;
                                    }
                                    return true;
                                }
                                false
                            });
                            if bad {
                                return Err(EngineError::Unsupported(alloc::format!(
                                    "OLD/NEW references to computed view column of view \"{}\" in MERGE RETURNING are not supported",
                                    stmt.target
                                )));
                            }
                        }
                    }
                    let is_target_q = |q: &Option<String>| match q.as_deref() {
                        None => true,
                        Some(x) => {
                            x.eq_ignore_ascii_case(&alias)
                                || x.eq_ignore_ascii_case("old")
                                || x.eq_ignore_ascii_case("new")
                        }
                    };
                    let mut out: alloc::vec::Vec<spg_sql::ast::SelectItem> =
                        alloc::vec::Vec::with_capacity(items.len());
                    let push_view_cols = |out: &mut alloc::vec::Vec<spg_sql::ast::SelectItem>| {
                        // Round 154 — `view_cols` carries the order when
                        // computed columns exist; a computed column
                        // projects its (target-qualified) expression.
                        if vr.view_cols.is_empty() {
                            for (view_col, base_col) in &vr.col_map {
                                out.push(spg_sql::ast::SelectItem::Expr {
                                    expr: Expr::Column(spg_sql::ast::ColumnName {
                                        qualifier: Some(alias.clone()),
                                        name: base_col.clone(),
                                    }),
                                    alias: Some(view_col.clone()),
                                });
                            }
                        } else {
                            for (view_col, base_col) in &vr.view_cols {
                                let expr = match base_col {
                                    Some(b) => Expr::Column(spg_sql::ast::ColumnName {
                                        qualifier: Some(alias.clone()),
                                        name: b.clone(),
                                    }),
                                    None => {
                                        let mut e = Expr::Column(spg_sql::ast::ColumnName {
                                            qualifier: None,
                                            name: view_col.clone(),
                                        });
                                        rewrite_view_refs_to_base(
                                            &mut e,
                                            &map,
                                            &cmap,
                                            Some(&alias),
                                        );
                                        e
                                    }
                                };
                                out.push(spg_sql::ast::SelectItem::Expr {
                                    expr,
                                    alias: Some(view_col.clone()),
                                });
                            }
                        }
                    };
                    for it in items.iter() {
                        match it {
                            spg_sql::ast::SelectItem::QualifiedWildcard(q)
                                if q.eq_ignore_ascii_case(&alias) =>
                            {
                                // `v.*` → the view's columns: base value,
                                // view-name output.
                                push_view_cols(&mut out);
                            }
                            // Bare `*` keeps PG's MERGE range-table order —
                            // source columns first, then the view's columns
                            // under their VIEW names.
                            spg_sql::ast::SelectItem::Wildcard => {
                                let src =
                                    s.source_alias.clone().unwrap_or_else(|| s.source.clone());
                                out.push(spg_sql::ast::SelectItem::QualifiedWildcard(src));
                                push_view_cols(&mut out);
                            }
                            spg_sql::ast::SelectItem::Expr { expr, alias: a } => {
                                let out_alias = if a.is_some() {
                                    a.clone()
                                } else if let Expr::Column(c) = expr
                                    && is_target_q(&c.qualifier)
                                    && (map.contains_key(&c.name) || cmap.contains_key(&c.name))
                                {
                                    Some(c.name.clone())
                                } else {
                                    a.clone()
                                };
                                let mut e = expr.clone();
                                crate::expr_analysis::rewrite_nodes_mut(&mut e, &mut |n| {
                                    if let Expr::Column(c) = n {
                                        if is_target_q(&c.qualifier) {
                                            if let Some(b) = map.get(&c.name) {
                                                c.name = b.clone();
                                            } else if cmap.contains_key(&c.name) {
                                                // Substitute the computed
                                                // column's expression,
                                                // target-qualified.
                                                let mut sub =
                                                    Expr::Column(spg_sql::ast::ColumnName {
                                                        qualifier: None,
                                                        name: c.name.clone(),
                                                    });
                                                rewrite_view_refs_to_base(
                                                    &mut sub,
                                                    &map,
                                                    &cmap,
                                                    Some(&alias),
                                                );
                                                *n = sub;
                                            }
                                        }
                                        return true;
                                    }
                                    false
                                });
                                out.push(spg_sql::ast::SelectItem::Expr {
                                    expr: e,
                                    alias: out_alias,
                                });
                            }
                            other => out.push(other.clone()),
                        }
                    }
                    *items = out;
                }
            }
            // v7.39 (round 152) — WITH CHECK OPTION rides the merge: the
            // outermost (written) view's own option drives the cascade,
            // and any lower view carrying its OWN option enforces
            // regardless of it (PG, probe P6).
            let written_opt = self
                .active_catalog()
                .views_all()
                .get(&stmt.target)
                .map_or(0, |v| v.check_option);
            let check = if written_opt != 0 || vr.check_chain.iter().any(|(_, _, o)| *o != 0) {
                Some(ViewCheck {
                    written_opt,
                    chain: vr.check_chain,
                })
            } else {
                None
            };
            return self.exec_merge_filtered(&s, vr.where_at_base, check, cancel);
        }
        self.exec_merge_filtered(stmt, None, None, cancel)
    }

    /// The MERGE executor proper. `target_filter` (the view's WHERE, over bare
    /// base columns) narrows which target rows the merge can see — rows outside
    /// it are neither matched nor eligible for a BY SOURCE clause, as in PG.
    fn exec_merge_filtered(
        &mut self,
        stmt: &spg_sql::ast::MergeStatement,
        target_filter: Option<Expr>,
        view_check: Option<ViewCheck>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        // v7.39 (round 525) — MERGE's clauses read the session too.
        let merge_sess = self.dml_session();
        let target_alias = stmt
            .target_alias
            .clone()
            .unwrap_or_else(|| stmt.target.clone());
        let source_alias = stmt
            .source_alias
            .clone()
            .unwrap_or_else(|| stmt.source.clone());
        // v7.36 (cold-tier coverage) — MERGE mutates target rows in
        // place by hot row index. Cold-tier target rows can't be
        // mutated by this path (cold-row mutation is a v7.37 item),
        // so MATCHED clauses would silently skip them — losing the
        // INSERT-or-update semantic. PG and MariaDB never half-apply
        // a MERGE; surface the gap explicitly.
        // v7.39 (round 456) — O(1) predicate first; see the DELETE path.
        if let Some(t) = self.active_catalog().get(&stmt.target)
            && t.has_cold_rows_fast()
            && t.count_cold_locators() > 0
        {
            return Err(EngineError::Unsupported(alloc::format!(
                "MERGE INTO {:?}: cold-tier rows exist on target; \
                 cold-tier mutation by MERGE is a v7.37 candidate. \
                 Run COMPACT and retry.",
                stmt.target
            )));
        }
        let (target_cols, target_rows_snapshot) = {
            let t = self.active_catalog().get(&stmt.target).ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.target.clone(),
                })
            })?;
            // v7.37.15 Phase B — MERGE target snapshot consults
            // visibility so an in-flight delete on the target table
            // is not merged against. Phase B's `current_snapshot()`
            // returns unbounded (every row visible) — behaviour
            // matches pre-v7.37.15 exactly.
            // v7.39 (round 146) — keep each visible row's TRUE storage
            // position. The apply phase feeds update_row / delete_rows
            // storage positions; using the snapshot ordinal instead
            // mutated the wrong rows once dead versions (any prior MVCC
            // update/delete) preceded a target row.
            let snap = self.current_snapshot();
            let cols = t.schema().columns.clone();
            // v7.39 (round 148) — a view-redirected merge only sees the target
            // rows satisfying the view's WHERE (bare base-column predicate).
            let filter_ctx = EvalContext::new(&cols, None).with_session(&merge_sess);
            let mut rows: Vec<(usize, Row<'static>)> = Vec::new();
            for (pos, r) in t.scan_visible(&snap) {
                if let Some(f) = &target_filter
                    && !matches!(eval::eval_expr(f, r, &filter_ctx), Ok(Value::Bool(true)))
                {
                    continue;
                }
                rows.push((pos, r.clone()));
            }
            (cols, rows)
        };
        let (source_cols, source_rows) = if let Some(sub) = &stmt.source_select {
            // v7.37 D.44 — `USING (SELECT …) alias` subquery source: materialise
            // the SELECT and use its result columns/rows as the merge input.
            let QueryResult::Rows { mut columns, rows } = self.exec_select_cancel(sub, cancel)?
            else {
                return Err(EngineError::Unsupported(alloc::format!(
                    "MERGE USING subquery did not return rows"
                )));
            };
            // v7.39 (round 768, F31-D5) — `s(id, v)` renames the source
            // columns positionally (PG's rule; the VALUES lowering names
            // them column1..columnN otherwise).
            for (i, new_name) in stmt.source_column_aliases.iter().enumerate() {
                if let Some(c) = columns.get_mut(i) {
                    c.name = new_name.clone();
                }
            }
            (columns, rows)
        } else {
            let s = self.active_catalog().get(&stmt.source).ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.source.clone(),
                })
            })?;
            // v7.36 — also fold cold-tier source rows into the merge
            // input. Source rows are read-only inputs (we never
            // mutate the source) so this is the same shape as
            // `materialise_table_ref`'s v7.35.1 cold-aware lift.
            // v7.37.15 Phase B — visibility-gated source scan.
            let snap = self.current_snapshot();
            let mut rows: Vec<Row<'static>> =
                s.scan_visible(&snap).map(|(_, r)| r.clone()).collect();
            rows.extend(crate::constraints::iter_cold_rows_of_parent(
                self.active_catalog(),
                s,
            ));
            (s.schema().columns.clone(), rows)
        };
        // Composite schema: target_alias.col ... source_alias.col ...
        let mut combined_schema: Vec<ColumnSchema> = Vec::new();
        for col in &target_cols {
            combined_schema.push(ColumnSchema::new(
                alloc::format!("{target_alias}.{}", col.name),
                col.ty,
                col.nullable,
            ));
        }
        for col in &source_cols {
            combined_schema.push(ColumnSchema::new(
                alloc::format!("{source_alias}.{}", col.name),
                col.ty,
                col.nullable,
            ));
        }
        // v7.39 (round 525) — MERGE's clauses read the session too: a
        // `WHEN MATCHED THEN UPDATE SET v = current_setting(…)` failed.
        let combined_ctx = EvalContext::new(&combined_schema, None).with_session(&merge_sess);
        // Source-only context for WHEN NOT MATCHED actions (no
        // matched target row exists — the source-side qualified
        // columns must still resolve).
        let mut source_only_schema: Vec<ColumnSchema> = Vec::new();
        for col in &target_cols {
            source_only_schema.push(ColumnSchema::new(
                alloc::format!("{target_alias}.{}", col.name),
                col.ty,
                col.nullable,
            ));
        }
        for col in &source_cols {
            source_only_schema.push(ColumnSchema::new(
                alloc::format!("{source_alias}.{}", col.name),
                col.ty,
                col.nullable,
            ));
        }
        let source_only_ctx = EvalContext::new(&source_only_schema, None).with_session(&merge_sess);
        let target_arity = target_cols.len();
        let source_arity = source_cols.len();

        // Resolve INSERT column positions once (validate names).
        // For each clause that's an INSERT, map column names → target positions.
        let mut delete_indices: Vec<usize> = Vec::new();
        let mut updates: Vec<(usize, Vec<Value<'static>>)> = Vec::new();
        let mut inserts: Vec<Vec<Value<'static>>> = Vec::new();
        let mut affected: usize = 0;
        // v7.39 (round 130) — RETURNING records, collected only when the
        // statement has a RETURNING clause.
        let want_returning = stmt.returning.is_some();
        let mut ret_records: Vec<MergeRetRecord> = Vec::new();
        // v7.39 (round 146, PG17) — target rows matched by ANY source row,
        // for the WHEN NOT MATCHED BY SOURCE pass below.
        let has_by_source = stmt
            .clauses
            .iter()
            .any(|c| matches!(c.matched, spg_sql::ast::MergeMatched::NotMatchedBySource));
        let mut ever_matched: alloc::collections::BTreeSet<usize> =
            alloc::collections::BTreeSet::new();

        for (src_idx, src_row) in source_rows.iter().enumerate() {
            if src_idx.is_multiple_of(256) {
                cancel.check()?;
            }
            // Find every matched target index (per the ON predicate).
            let mut matched_targets: Vec<usize> = Vec::new();
            for (t_idx, (_, t_row)) in target_rows_snapshot.iter().enumerate() {
                let mut combined_vals = t_row.values.clone();
                combined_vals.extend(src_row.values.iter().cloned());
                let combined_row = Row::new(combined_vals);
                let cond = eval::eval_expr(&stmt.on, &combined_row, &combined_ctx)?;
                if crate::eval::predicate_is_true(&cond, "JOIN/ON", combined_ctx.mysql_dialect)? {
                    matched_targets.push(t_idx);
                }
            }
            let is_matched = !matched_targets.is_empty();
            ever_matched.extend(matched_targets.iter().copied());
            // Pick the first WHEN clause whose kind agrees with
            // `is_matched` and whose AND condition (if any) holds.
            // AND condition for MATCHED: evaluated against the
            // first matched target row × source. For NOT MATCHED:
            // evaluated with target side NULL-padded.
            let fired_clause = stmt.clauses.iter().find(|c| {
                let kind_ok = match c.matched {
                    spg_sql::ast::MergeMatched::Matched => is_matched,
                    spg_sql::ast::MergeMatched::NotMatched => !is_matched,
                    // Fires from the target-side pass below, never per source row.
                    spg_sql::ast::MergeMatched::NotMatchedBySource => false,
                };
                if !kind_ok {
                    return false;
                }
                let Some(cond_expr) = &c.condition else {
                    return true;
                };
                let row = if is_matched {
                    let t = &target_rows_snapshot[matched_targets[0]].1;
                    let mut vals = t.values.clone();
                    vals.extend(src_row.values.iter().cloned());
                    Row::new(vals)
                } else {
                    let mut vals: Vec<Value<'static>> =
                        (0..target_arity).map(|_| Value::Null).collect();
                    vals.extend(src_row.values.iter().cloned());
                    Row::new(vals)
                };
                let ctx_ref = if is_matched {
                    &combined_ctx
                } else {
                    &source_only_ctx
                };
                matches!(
                    eval::eval_expr(cond_expr, &row, ctx_ref),
                    Ok(Value::Bool(true))
                )
            });
            let Some(clause) = fired_clause else { continue };
            match &clause.action {
                spg_sql::ast::MergeAction::DoNothing => {}
                spg_sql::ast::MergeAction::Delete => {
                    for &t_idx in &matched_targets {
                        let pos = target_rows_snapshot[t_idx].0;
                        if !delete_indices.contains(&pos) {
                            delete_indices.push(pos);
                            affected += 1;
                            if want_returning {
                                let t_row = &target_rows_snapshot[t_idx].1;
                                ret_records.push(MergeRetRecord {
                                    action: "DELETE",
                                    target_final: t_row.values.clone(),
                                    old: Some(t_row.values.clone()),
                                    new: None,
                                    source: src_row.values.clone(),
                                });
                            }
                        }
                    }
                }
                spg_sql::ast::MergeAction::Update { assignments } => {
                    // Pre-resolve SET targets to target column positions.
                    let mut planned_sets: Vec<(usize, &Expr)> =
                        Vec::with_capacity(assignments.len());
                    for (col, expr) in assignments {
                        let pos =
                            target_cols
                                .iter()
                                .position(|c| c.name == *col)
                                .ok_or_else(|| {
                                    EngineError::Eval(EvalError::ColumnNotFound {
                                        name: col.clone(),
                                    })
                                })?;
                        planned_sets.push((pos, expr));
                    }
                    for &t_idx in &matched_targets {
                        let (t_pos, t_row) = &target_rows_snapshot[t_idx];
                        let mut new_values = t_row.values.clone();
                        let mut combined_vals = t_row.values.clone();
                        combined_vals.extend(src_row.values.iter().cloned());
                        let combined_row = Row::new(combined_vals);
                        for (pos, expr) in &planned_sets {
                            let raw = eval::eval_expr(expr, &combined_row, &combined_ctx)?;
                            let coerced = coerce_value(
                                raw,
                                target_cols[*pos].ty,
                                &target_cols[*pos].name,
                                *pos,
                            )?;
                            new_values[*pos] = coerced;
                        }
                        if want_returning {
                            ret_records.push(MergeRetRecord {
                                action: "UPDATE",
                                target_final: new_values.clone(),
                                old: Some(t_row.values.clone()),
                                new: Some(new_values.clone()),
                                source: src_row.values.clone(),
                            });
                        }
                        updates.push((*t_pos, new_values));
                        affected += 1;
                    }
                }
                spg_sql::ast::MergeAction::Insert { columns, values } => {
                    // For INSERT NOT MATCHED, target side is NULL-padded.
                    let mut vals: Vec<Value<'static>> =
                        (0..target_arity).map(|_| Value::Null).collect();
                    vals.extend(src_row.values.iter().cloned());
                    let synth_row = Row::new(vals);
                    let mut new_row_values: Vec<Value<'static>> =
                        (0..target_arity).map(|_| Value::Null).collect();
                    // v7.39 (read01 round 123) — an omitted column list (`INSERT
                    // VALUES (…)`) maps the values positionally to every column
                    // in declaration order, like a plain INSERT.
                    if columns.is_empty() {
                        if values.len() > target_arity {
                            return Err(EngineError::Unsupported(alloc::format!(
                                "MERGE INSERT has more expressions ({}) than target columns ({target_arity})",
                                values.len()
                            )));
                        }
                        for (pos, expr) in values.iter().enumerate() {
                            let raw = eval::eval_expr(expr, &synth_row, &source_only_ctx)?;
                            let coerced = coerce_value(
                                raw,
                                target_cols[pos].ty,
                                &target_cols[pos].name,
                                pos,
                            )?;
                            new_row_values[pos] = coerced;
                        }
                    } else {
                        for (col, expr) in columns.iter().zip(values.iter()) {
                            let pos = target_cols.iter().position(|c| c.name == *col).ok_or_else(
                                || {
                                    EngineError::Eval(EvalError::ColumnNotFound {
                                        name: col.clone(),
                                    })
                                },
                            )?;
                            let raw = eval::eval_expr(expr, &synth_row, &source_only_ctx)?;
                            let coerced = coerce_value(
                                raw,
                                target_cols[pos].ty,
                                &target_cols[pos].name,
                                pos,
                            )?;
                            new_row_values[pos] = coerced;
                        }
                    }
                    if want_returning {
                        ret_records.push(MergeRetRecord {
                            action: "INSERT",
                            target_final: new_row_values.clone(),
                            old: None,
                            new: Some(new_row_values.clone()),
                            source: src_row.values.clone(),
                        });
                    }
                    inserts.push(new_row_values);
                    affected += 1;
                }
            }
        }
        // v7.39 (round 146, PG17) — WHEN NOT MATCHED BY SOURCE: a second pass
        // over the TARGET rows no source row matched. The source side does not
        // exist for these rows, so a source-alias column reference anywhere in
        // the clause is PG's "invalid reference" error, the eval row NULL-pads
        // the source columns, and the RETURNING record carries a NULL source.
        if has_by_source {
            for c in &stmt.clauses {
                if !matches!(c.matched, spg_sql::ast::MergeMatched::NotMatchedBySource) {
                    continue;
                }
                let mut quals: Vec<&str> = Vec::new();
                let mut all_q = true;
                if let Some(cond) = &c.condition {
                    crate::expr_analysis::collect_column_qualifiers(cond, &mut quals, &mut all_q);
                }
                if let spg_sql::ast::MergeAction::Update { assignments } = &c.action {
                    for (_, e) in assignments {
                        crate::expr_analysis::collect_column_qualifiers(e, &mut quals, &mut all_q);
                    }
                }
                if quals.iter().any(|q| q.eq_ignore_ascii_case(&source_alias)) {
                    return Err(EngineError::Unsupported(alloc::format!(
                        "invalid reference to FROM-clause entry for table \"{source_alias}\" \
                         DETAIL: There is an entry for table \"{source_alias}\", but it cannot \
                         be referenced from this part of the query."
                    )));
                }
            }
            for (t_idx, (t_pos, t_row)) in target_rows_snapshot.iter().enumerate() {
                if ever_matched.contains(&t_idx) {
                    continue;
                }
                // Eval row: target values + NULL-padded source side.
                let mut vals = t_row.values.clone();
                vals.extend((0..source_arity).map(|_| Value::Null));
                let eval_row = Row::new(vals);
                let fired = stmt.clauses.iter().find(|c| {
                    if !matches!(c.matched, spg_sql::ast::MergeMatched::NotMatchedBySource) {
                        return false;
                    }
                    let Some(cond) = &c.condition else {
                        return true;
                    };
                    matches!(
                        eval::eval_expr(cond, &eval_row, &combined_ctx),
                        Ok(Value::Bool(true))
                    )
                });
                let Some(clause) = fired else { continue };
                let null_source: Vec<Value<'static>> =
                    (0..source_arity).map(|_| Value::Null).collect();
                match &clause.action {
                    spg_sql::ast::MergeAction::DoNothing => {}
                    spg_sql::ast::MergeAction::Delete => {
                        if !delete_indices.contains(t_pos) {
                            delete_indices.push(*t_pos);
                            affected += 1;
                            if want_returning {
                                ret_records.push(MergeRetRecord {
                                    action: "DELETE",
                                    target_final: t_row.values.clone(),
                                    old: Some(t_row.values.clone()),
                                    new: None,
                                    source: null_source,
                                });
                            }
                        }
                    }
                    spg_sql::ast::MergeAction::Update { assignments } => {
                        let mut new_values = t_row.values.clone();
                        for (col, expr) in assignments {
                            let pos = target_cols.iter().position(|c| c.name == *col).ok_or_else(
                                || {
                                    EngineError::Eval(EvalError::ColumnNotFound {
                                        name: col.clone(),
                                    })
                                },
                            )?;
                            let raw = eval::eval_expr(expr, &eval_row, &combined_ctx)?;
                            let coerced = coerce_value(
                                raw,
                                target_cols[pos].ty,
                                &target_cols[pos].name,
                                pos,
                            )?;
                            new_values[pos] = coerced;
                        }
                        if want_returning {
                            ret_records.push(MergeRetRecord {
                                action: "UPDATE",
                                target_final: new_values.clone(),
                                old: Some(t_row.values.clone()),
                                new: Some(new_values.clone()),
                                source: null_source,
                            });
                        }
                        updates.push((*t_pos, new_values));
                        affected += 1;
                    }
                    // The parser has no INSERT production under BY SOURCE.
                    spg_sql::ast::MergeAction::Insert { .. } => unreachable!(),
                }
            }
        }
        let _ = source_arity; // captured for symmetry; cancellation cost negligible.

        // v7.39 (round 152) — WITH CHECK OPTION through a MERGE: every
        // row an UPDATE or INSERT action produces must still satisfy
        // the view chain's quals (DELETE is exempt, as in PG). Checked
        // before anything applies so a violation leaves the table
        // untouched — PG never half-applies a MERGE.
        if let Some(check) = &view_check {
            let mut pending: alloc::vec::Vec<alloc::vec::Vec<Value<'static>>> =
                alloc::vec::Vec::with_capacity(updates.len() + inserts.len());
            for (_, new_vals) in &updates {
                pending.push(new_vals.clone());
            }
            for vals in &inserts {
                pending.push(vals.clone());
            }
            self.enforce_view_check(check, &pending, &target_cols, &stmt.target)?;
        }
        // v7.37.15 Phase C — fetch the writer version BEFORE
        // taking the table mut borrow (so we don't double-mut
        // self). Shared across MERGE INSERT / UPDATE / DELETE so
        // the whole statement commits atomically.
        let xmin = self.writer_version_for_current_stmt();
        // v7.37.15 (Phase C.3, step 4b) — in-place kill switch, read
        // before the table mut borrow (mirrors DELETE/UPDATE). When on,
        // MERGE UPDATE tombstones the matched old version + appends the
        // new version (both stamped with the shared `xmin`) instead of
        // an in-place replace. Default OFF → legacy update_row.
        let inplace = self.mvcc_inplace();
        // Apply the plan to the target table.
        let table = self
            .active_catalog_mut()
            .get_mut(&stmt.target)
            .ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.target.clone(),
                })
            })?;
        // Apply updates first (in-place), then deletes (one batch),
        // then inserts. The storage API uses `update_row(pos,
        // new_values)`, `delete_rows(&[positions])`, and `insert(row)`.
        for (idx, new_vals) in &updates {
            if inplace {
                // MVCC: tombstone old version (xmax = xmin) + append new
                // version (xmin). Appending keeps earlier positions valid,
                // so the remaining `idx` values in this loop stay correct.
                let _ = table.mark_row_deleted(*idx, xmin);
                table
                    .insert_with_xmin(Row::new(new_vals.clone()), xmin)
                    .map_err(EngineError::Storage)?;
            } else {
                table
                    .update_row(*idx, new_vals.clone())
                    .map_err(EngineError::Storage)?;
            }
        }
        if !delete_indices.is_empty() {
            table.delete_rows(&delete_indices);
        }
        // v7.37.15 Phase C — MERGE inserts share the pre-fetched
        // writer version (xmin captured above).
        for vals in inserts {
            table
                .insert_with_xmin(Row::new(vals), xmin)
                .map_err(EngineError::Storage)?;
        }
        let _ = table; // drop the mut borrow before building RETURNING.
        if let Some(items) = &stmt.returning {
            return self.build_merge_returning(
                &target_alias,
                &source_alias,
                &target_cols,
                &source_cols,
                ret_records,
                items,
            );
        }
        Ok(QueryResult::CommandOk {
            affected,
            modified_catalog: affected > 0,
        })
    }

    /// v7.39 (round 130) — project a MERGE's `RETURNING` list over the actions
    /// it took. Each `MergeRetRecord` is flattened into a synthetic row whose
    /// columns cover the target alias (`t.col` = post-image), the source alias
    /// (`s.col`), the bare target names, `OLD.*`/`NEW.*` blocks, and a
    /// `__merge_action` cell; RETURNING expressions are rewritten onto those
    /// names (`merge_action()` → `__merge_action`, `OLD.c`/`NEW.c` → the block
    /// cells) and evaluated. Matches PG18 byte-identical.
    fn build_merge_returning(
        &self,
        target_alias: &str,
        source_alias: &str,
        target_cols: &[ColumnSchema],
        source_cols: &[ColumnSchema],
        records: Vec<MergeRetRecord>,
        items: &[SelectItem],
    ) -> Result<QueryResult, EngineError> {
        let t_arity = target_cols.len();
        // Synthetic schema: [t.col…] [s.col…] [bare target col…]
        //                   [__ret_old_col…] [__ret_new_col…] [__merge_action]
        let mut syn: Vec<ColumnSchema> = Vec::new();
        for c in target_cols {
            syn.push(ColumnSchema::new(
                alloc::format!("{target_alias}.{}", c.name),
                c.ty,
                c.nullable,
            ));
        }
        for c in source_cols {
            syn.push(ColumnSchema::new(
                alloc::format!("{source_alias}.{}", c.name),
                c.ty,
                c.nullable,
            ));
        }
        for c in target_cols {
            syn.push(ColumnSchema::new(c.name.clone(), c.ty, true));
        }
        for c in target_cols {
            syn.push(ColumnSchema::new(
                alloc::format!("__ret_old_{}", c.name),
                c.ty,
                true,
            ));
        }
        for c in target_cols {
            syn.push(ColumnSchema::new(
                alloc::format!("__ret_new_{}", c.name),
                c.ty,
                true,
            ));
        }
        syn.push(ColumnSchema::new(
            alloc::string::String::from("__merge_action"),
            DataType::Text,
            false,
        ));

        // Expand + rewrite the projection into flat (output_name, ty, expr)
        // triples against the synthetic schema.
        let expanded = expand_merge_returning_items(items, target_alias, source_alias, target_cols);
        let projection = build_projection(&expanded, &syn, "", self.backslash_escapes)?;
        let columns: Vec<ColumnSchema> = projection
            .iter()
            .map(|p| ColumnSchema::new(p.output_name.clone(), p.ty, p.nullable))
            .collect();

        let ctx = self.ev_ctx(&syn, None);
        let cancel = CancelToken::none();
        let mut out_rows: Vec<Row<'static>> = Vec::with_capacity(records.len());
        for rec in &records {
            let null_block = || alloc::vec![Value::Null; t_arity];
            let mut vals: Vec<Value<'static>> = Vec::with_capacity(syn.len());
            vals.extend(rec.target_final.iter().cloned()); // t.col
            vals.extend(rec.source.iter().cloned()); // s.col
            vals.extend(rec.target_final.iter().cloned()); // bare target col
            vals.extend(rec.old.clone().unwrap_or_else(null_block)); // __ret_old_
            vals.extend(rec.new.clone().unwrap_or_else(null_block)); // __ret_new_
            vals.push(Value::Text(rec.action.into())); // __merge_action
            let syn_row = Row::new(vals);
            let mut proj: Vec<Value<'static>> = Vec::with_capacity(projection.len());
            for p in &projection {
                proj.push(self.eval_expr_with_correlated(&p.expr, &syn_row, &ctx, cancel, None)?);
            }
            out_rows.push(Row::new(proj));
        }
        Ok(QueryResult::Rows {
            columns,
            rows: out_rows,
        })
    }

    pub(crate) fn exec_delete_cancel(
        &mut self,
        stmt: &spg_sql::ast::DeleteStatement,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        // v7.37.43-T4.4 — writable CTE outer body (DELETE).
        if !stmt.ctes.is_empty() {
            return self.exec_delete_with_ctes(stmt.clone(), cancel);
        }
        self.bump_table_change(&stmt.table);
        // v7.39 (RLS) Phase 2 — DELETE USING visibility: only delete rows the
        // policy-subject session can see (a hidden row is silently skipped).
        let rls_del;
        let stmt = match self.rls_write_using_predicate(&stmt.table, spg_storage::PolicyCmd::Delete)
        {
            Some(pred) => {
                let mut s = stmt.clone();
                s.where_ = and_optional_predicates(s.where_.take(), Some(pred));
                rls_del = s;
                &rls_del
            }
            None => stmt,
        };
        // v7.39 (round 139/141) — DO INSTEAD NOTHING rules narrow the DELETE.
        // Unconditional → AND a constant FALSE (deletes nothing). Conditional
        // (WHERE over OLD) → AND `COALESCE(NOT(cond), TRUE)` so only rows the
        // rule does not block are deleted. Either way the normal path runs so the
        // `DELETE n` tag / RETURNING stay byte-identical. A RETURNING on a fully
        // (unconditionally) suppressed statement is rejected as PG does.
        // v7.39 (round 333, V59) — as the caller wrote it; the conditional
        // instead-command action runs against THESE rows.
        let unnarrowed_del = stmt;
        let rule_blocked_del;
        let del_block = if self.rule_blocks_statement(&stmt.table, "DELETE") {
            if stmt.returning.is_some() {
                return Err(crate::rules::rule_returning_error("DELETE", &stmt.table));
            }
            Some(spg_sql::ast::Expr::Literal(spg_sql::ast::Literal::Bool(
                false,
            )))
        } else {
            self.conditional_block_predicate(&stmt.table, "DELETE", &[])?
        };
        let stmt = if let Some(pred) = del_block {
            let mut s = stmt.clone();
            s.where_ = and_optional_predicates(s.where_.take(), Some(pred));
            rule_blocked_del = s;
            &rule_blocked_del
        } else {
            stmt
        };
        // v7.39 (round 140/142) — rule-command forms. DO INSTEAD <command>
        // replaces the DELETE (original op never runs, tag `DELETE 0`); DO ALSO
        // runs the real delete first, then the commands. Both bind OLD per
        // matching row. The guard bounds a rule → delete cycle.
        if !self.rule_rewrite_active {
            let instead_del = self.instead_command_rules(&stmt.table, "DELETE");
            // v7.39 (round 333, V59) — see the UPDATE path: unconditional
            // replaces the statement, conditional only claims its own rows.
            let (uncond, cond): (Vec<_>, Vec<_>) = instead_del
                .into_iter()
                .partition(|r| r.when_condition.is_empty());
            if !uncond.is_empty() {
                let mut all = uncond;
                all.extend(cond);
                return self.exec_delete_instead_command(unnarrowed_del, all, cancel);
            }
            if !cond.is_empty() {
                let (columns, old_rows) =
                    self.scan_relation_rows(&unnarrowed_del.table, &unnarrowed_del.where_, cancel)?;
                let rows: Vec<(Option<Row<'static>>, Option<Row<'static>>)> =
                    old_rows.into_iter().map(|r| (None, Some(r))).collect();
                self.run_also_rules(&cond, &columns, &rows, cancel)?;
            }
            let also_del = self.also_rules(&stmt.table, "DELETE");
            if !also_del.is_empty() {
                return self.exec_delete_with_also(stmt, also_del, cancel);
            }
        }
        // v7.39 (round 137, Phase 2) — INSTEAD OF DELETE trigger on the target
        // view fires per matching view row instead of the auto-updatable
        // redirect.
        let iof_del = self.snapshot_row_triggers(&stmt.table, "DELETE", "INSTEAD OF");
        if !iof_del.is_empty() {
            return self.exec_delete_view_instead_of(stmt, iof_del, cancel);
        }
        // v7.37.19 (19.13) — auto-updatable view redirect. v7.38 (P6.46) — a
        // view WHERE is AND-ed onto the caller's so only rows visible through
        // the view are deleted.
        // v7.39 (round 267) — the view exists but is not auto-updatable.
        // Without this the write fell through to the base-table lookup and
        // reported `relation "<view>" does not exist`, which is not merely
        // the wrong wording — it denies the existence of an object the
        // catalog plainly has.
        if let Err(reason) = view_redirect_checked(self.active_catalog(), &stmt.table) {
            if self.active_catalog().has_view(&stmt.table) {
                return Err(view_not_updatable_error(
                    &stmt.table,
                    &DELETE_VERB,
                    reason,
                    false,
                ));
            }
        }
        if let Some(vr) = view_redirect_to_simple_base(self.active_catalog(), &stmt.table) {
            // DELETE removes rows; there is no post-image to check, so
            // WITH CHECK OPTION does not apply. The composed WHERE (all nested
            // levels AND-ed) still restricts which base rows are visible.
            let mut rewritten = stmt.clone();
            // v7.39 (round 133) — column-renamed view: rewrite the WHERE's view
            // columns to base columns before AND-ing the view WHERE. Round 154
            // — a computed column READ substitutes its defining expression.
            if !vr.col_map.is_empty() || !vr.computed.is_empty() {
                let map: alloc::collections::BTreeMap<String, String> =
                    vr.col_map.iter().cloned().collect();
                let cmap: alloc::collections::BTreeMap<String, Expr> = vr
                    .computed
                    .iter()
                    .map(|c| (c.name.clone(), c.def.clone()))
                    .collect();
                if let Some(w) = &mut rewritten.where_ {
                    rewrite_view_refs_to_base(w, &map, &cmap, None);
                }
                // v7.39 (round 134) — rewrite RETURNING view cols → base cols.
                if let Some(ret) = &rewritten.returning {
                    rewritten.returning = Some(rewrite_view_returning_items(
                        ret,
                        &vr.col_map,
                        &vr.computed,
                        &vr.view_cols,
                    ));
                }
            }
            rewritten.table = vr.base;
            rewritten.where_ = and_optional_predicates(vr.where_at_base, rewritten.where_);
            return self.exec_delete_cancel(&rewritten, cancel);
        }
        // v7.37 D.46 — DELETE on a partition parent fans out to every child;
        // the parent table holds no rows of its own, so a parent-targeted DELETE
        // would otherwise silently affect nothing. Each child DELETE re-evaluates
        // the same WHERE against that child's rows (and recurses for sub-partitions,
        // since a child is not itself a parent unless further partitioned).
        // v7.39 (round 646) — an INHERITANCE parent fans out the same way,
        // plus itself. Round 645 refused this rather than answer half of
        // it; what makes the parent expressible as a term is `only`,
        // which arrived with this round — without it, running the
        // statement on the parent re-enters this fan-out forever.
        //
        // `ONLY` on the incoming statement means the caller asked for
        // the parent alone, so neither parent descends.
        // The extra test lives INSIDE the branch, not beside it. Round
        // 646 first wrote `let inherit_fanout = …` at the top of this
        // function — one statement, evaluated on every UPDATE and DELETE
        // — and `SELECT count(*) FROM d WHERE g BETWEEN 10 AND 20` went
        // 5.9 ms to 97 ms, reproduced outside the panel. Nothing about
        // that query comes near here. Rounds 641, 643 and 644 met the
        // same wall from their own directions; what a function's BODY
        // holds is a codegen decision, and the cold side of a branch is
        // not the body.
        if !stmt.only
            && (crate::partition::is_partition_parent(self.active_catalog(), &stmt.table)
                || crate::partition::has_inheritance_children(self.active_catalog(), &stmt.table))
        {
            let inherit_fanout =
                !crate::partition::is_partition_parent(self.active_catalog(), &stmt.table);
            let mut children =
                crate::partition::children_of_parent(self.active_catalog(), &stmt.table);
            if inherit_fanout {
                // The parent's own rows are a term too — as ONLY, so the
                // recursive call stops here.
                children.push(stmt.table.clone());
            }
            let mut total_affected = 0usize;
            let mut ret_columns: Option<Vec<ColumnSchema>> = None;
            let mut ret_rows: Vec<Row<'static>> = Vec::new();
            for child in children {
                let mut child_stmt = stmt.clone();
                child_stmt.only = child == stmt.table;
                child_stmt.table = child;
                match self.exec_delete_cancel(&child_stmt, cancel)? {
                    QueryResult::CommandOk { affected, .. } => total_affected += affected,
                    QueryResult::Rows { columns, rows } => {
                        total_affected += rows.len();
                        ret_columns = Some(columns);
                        ret_rows.extend(rows);
                    }
                }
            }
            return Ok(match ret_columns {
                // DELETE … RETURNING: merge every child's returned rows.
                Some(columns) => QueryResult::Rows {
                    columns,
                    rows: ret_rows,
                },
                None => QueryResult::CommandOk {
                    affected: total_affected,
                    modified_catalog: true,
                },
            });
        }
        // v7.12.5 — snapshot BEFORE/AFTER DELETE row triggers + the
        // session FTS config before the mut borrow (same shape as
        // INSERT / UPDATE).
        let before_delete_triggers = self.snapshot_row_triggers(&stmt.table, "DELETE", "BEFORE");
        let after_delete_triggers = self.snapshot_row_triggers(&stmt.table, "DELETE", "AFTER");
        let trigger_session_cfg: Option<String> = self
            .session_params
            .get("default_text_search_config")
            .cloned();
        // v5.2.3: PK-targeted DELETE → first retire any cold-tier
        // locator for the key. The cold row body stays in the
        // segment (becoming shadowed garbage that a future
        // compaction pass reclaims) but the index no longer
        // resolves it. The shadow count contributes to the
        // affected total; the subsequent hot walk handles any hot
        // rows for the same key.
        let mut cold_shadow_count: usize = 0;
        if let Some(w) = &stmt.where_ {
            let schema_cols = self
                .active_catalog()
                .get(&stmt.table)
                .ok_or_else(|| {
                    EngineError::Storage(StorageError::TableNotFound {
                        name: stmt.table.clone(),
                    })
                })?
                .schema()
                .columns
                .clone();
            if let Some((col_pos, key)) = try_pk_predicate(w, &schema_cols, stmt.table.as_str())
                && let Some(idx_name) = self
                    .active_catalog()
                    .get(&stmt.table)
                    .and_then(|t| t.index_on(col_pos).map(|i| i.name.clone()))
            {
                cold_shadow_count = self
                    .active_catalog_mut()
                    .shadow_cold_row(&stmt.table, &idx_name, &key)
                    .unwrap_or(0);
            } else {
                // v7.36 (cold-tier coverage) — DELETE with a non-PK
                // WHERE on a cold-bearing table previously missed
                // cold-tier matching rows. Walk each cold row, eval
                // WHERE against it, and shadow the matching PK keys
                // (retiring the cold-tier index entries; the row
                // body becomes shadowed garbage that compaction
                // reclaims).
                let pre_shadow_keys: Vec<spg_storage::IndexKey> = {
                    let mut keys = Vec::new();
                    // v7.39 (round 456) — `has_cold_rows_fast()` first.
                    //
                    // A profile of a range-predicated DELETE put 57.8% of
                    // self-time in `count_cold_locators`, which walks every
                    // (key, locator) pair of every BTree index — O(table) —
                    // to answer a question that is only ever asked as
                    // "> 0". That is the whole of this shape's cost scaling
                    // with table size: a one-row range DELETE costs 0.024 ms
                    // at 10k rows and 1.220 ms at 200k, while the same
                    // delete by equality (which never reaches here) stays at
                    // 0.006 ms.
                    //
                    // `has_cold_rows_fast` is the O(1) predicate v7.36 added
                    // for exactly this, and its own doc comment says the
                    // O(N) walk is "unsuitable per join stage". It is
                    // conservative — true when the cached count is stale —
                    // so it short-circuits rather than replaces: the exact
                    // walk still runs whenever cold rows might exist, and
                    // the answer is unchanged.
                    if let Some(t) = self.active_catalog().get(&stmt.table)
                        && t.has_cold_rows_fast()
                        && t.count_cold_locators() > 0
                    {
                        // v7.39 (round 524) — the cold-row scan evaluates the
                        // same predicate as the hot path and must read the
                        // same session.
                        let cold_sess = self.dml_session();
                        let ctx = eval::EvalContext::new(
                            &schema_cols,
                            Some(stmt.alias.as_deref().unwrap_or(stmt.table.as_str())),
                        )
                        .with_session(&cold_sess);
                        for (key, row) in
                            crate::constraints::iter_cold_rows_with_pk_key(self.active_catalog(), t)
                        {
                            let cond = eval::eval_expr(w, &row, &ctx).map_err(EngineError::Eval)?;
                            if crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
                                keys.push(key);
                            }
                        }
                    }
                    keys
                };
                if !pre_shadow_keys.is_empty()
                    && let Some(idx_name) = self
                        .active_catalog()
                        .get(&stmt.table)
                        .and_then(crate::constraints::pk_btree_index_name)
                {
                    for key in pre_shadow_keys {
                        cold_shadow_count += self
                            .active_catalog_mut()
                            .shadow_cold_row(&stmt.table, &idx_name, &key)
                            .unwrap_or(0);
                    }
                }
            }
        }

        // v7.37.17 (Phase E2) — a cold-tier shadow is a PHYSICAL index
        // edit inside the tx's shadow catalog, not a versioned row
        // write; the RC rebase cannot re-express it. Poison the rebase
        // so this tx keeps its frozen view (never loses the shadow).
        if cold_shadow_count > 0 && self.in_transaction() {
            self.poison_tx_rebase();
        }
        // v7.12.1 — cache the session FTS config as an owned
        // String before the mutable table borrow below; the
        // ctx-builder then references it via `as_deref` so the
        // immutable read of `session_params` doesn't conflict
        // with the mut borrow chain.
        let ts_cfg: Option<String> = self
            .session_param("default_text_search_config")
            .map(String::from);
        // A WHERE containing subqueries (DELETE … USING lowers to
        // EXISTS) cannot evaluate inside the &mut walk below — the
        // correlated resolver re-enters the engine read path.
        // Resolve the matching hot positions up front, immutably.
        let subquery_hits: Option<Vec<usize>> = if stmt
            .where_
            .as_ref()
            .is_some_and(|w| crate::subquery::expr_has_subquery(w))
        {
            let w = stmt.where_.as_ref().expect("guarded above");
            let table = self.active_catalog().get(&stmt.table).ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.table.clone(),
                })
            })?;
            let schema_cols: Vec<ColumnSchema> = table.schema().columns.clone();
            // v7.39 (read01 round 54) — carry the catalog (see above).
            let cat_for_ctx = self.active_catalog().clone();
            // v7.39 (round 524) — with the session, like UPDATE's.
            let sess = self.dml_session();
            let ctx = EvalContext::new(
                &schema_cols,
                Some(stmt.alias.as_deref().unwrap_or(stmt.table.as_str())),
            )
            .with_default_text_search_config(ts_cfg.as_deref())
            .with_catalog(&cat_for_ctx)
            .with_session(&sess);
            // v7.37.16 (gate-on inventory) — visibility gate, same as
            // the main walk below: a tombstoned version must not be
            // re-targeted. No-op under gate-off.
            let snap = self.current_snapshot();
            let mut hits: Vec<usize> = Vec::new();
            for i in 0..table.row_count() {
                if i.is_multiple_of(256) {
                    cancel.check()?;
                }
                if !table.is_row_visible(i, &snap) {
                    continue;
                }
                let Some(row) = table.rows().get(i) else {
                    continue;
                };
                let cond = self.eval_expr_with_correlated(w, row, &ctx, cancel, None)?;
                if crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
                    hits.push(i);
                }
            }
            Some(hits)
        } else {
            None
        };
        // v7.37.16 — snapshot for the DELETE target walk, taken BEFORE
        // the &mut table borrow below (current_snapshot needs &self).
        let scan_snapshot = self.current_snapshot();
        // v7.37.16 — old-row snapshot need, decided before the &mut
        // borrow too (reads the catalog): FK children, row triggers, or
        // RETURNING are the only consumers of `to_delete_rows`.
        let need_old_rows = !before_delete_triggers.is_empty()
            || !after_delete_triggers.is_empty()
            || stmt.returning.is_some()
            || crate::constraints::any_fk_child_references(self.active_catalog(), &stmt.table);
        // v7.39 (read01 round 54) — carry the catalog. Without it an
        // `m = 'happy'::mood` (or ::regclass / composite / domain) in an
        // UPDATE / DELETE WHERE failed outright with "unsupported cast target
        // `::mood`" — the same SELECT worked. Catalog::clone is a structural
        // Arc bump, so it is cheap; taken BEFORE the &mut borrow below.
        let cat_for_ctx = self.active_catalog().clone();
        // v7.39 (round 432) — MySQL `DELETE … ORDER BY … [LIMIT n]`. Read the
        // session dialect BEFORE the &mut catalog borrow below, which makes
        // `self` unreachable for the rest of the scan. (DELETE's ctx, like
        // UPDATE's, does not thread the engine — a long-standing site.)
        let mysql_order_limit = if self.backslash_escapes {
            stmt.order_limit.as_deref()
        } else {
            None
        };
        // v7.39 (round 524) — with the session: `DELETE … WHERE
        // current_setting('app.tenant') = …` matched nothing.
        let sess = self.dml_session();
        let table = self
            .active_catalog_mut()
            .get_mut(&stmt.table)
            .ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.table.clone(),
                })
            })?;
        // v7.39 (round 512) — `DELETE … WHERE ctid …`, the half of the dedup
        // idiom round 511 left out. The value comes from the candidate's own
        // position, which this loop already has; the column joins the schema
        // only when the predicate names it.
        let wants_ctid = stmt
            .where_
            .as_ref()
            .is_some_and(crate::select::expr_references_ctid);
        let mut schema_cols: Vec<ColumnSchema> = table.schema().columns.clone();
        if wants_ctid {
            schema_cols.push(ColumnSchema::new(
                alloc::string::String::from(crate::select::CTID_COLUMN),
                spg_storage::DataType::Text,
                false,
            ));
        }
        let schema_cols = schema_cols;
        let ctx = EvalContext::new(
            &schema_cols,
            Some(stmt.alias.as_deref().unwrap_or(stmt.table.as_str())),
        )
        .with_default_text_search_config(ts_cfg.as_deref())
        .with_catalog(&cat_for_ctx)
        .with_session(&sess);
        let mut positions: Vec<usize> = Vec::new();
        // v7.6.3 — collect every to-delete row's full Value tuple
        // alongside its position, so the FK enforcement pass can
        // run after the mut borrow drops.
        // v7.37.16 — only when `need_old_rows` (see above): a plain
        // DELETE on an unreferenced, trigger-free table skips ~1 ms of
        // per-row clones on a 10k-row delete; plan_fk_parent_deletions
        // early-returns on the empty rows slice.
        let mut to_delete_rows: Vec<Vec<Value<'static>>> = Vec::new();
        // v7.20 P4 — index seek (same shape as exec_update_cancel):
        // an equality WHERE on an indexed column narrows the walk
        // to the matching hot positions; the full WHERE still
        // re-evaluates per candidate. Downstream passes assume
        // ascending position order, so the seek result is sorted.
        let seek_positions: Option<Vec<usize>> = stmt.where_.as_ref().and_then(|w| {
            try_index_seek_positions(w, &schema_cols, table, stmt.table.as_str(), &scan_snapshot)
        });
        let candidate_positions: Vec<usize> = match seek_positions {
            Some(mut list) => {
                list.sort_unstable();
                list
            }
            None => {
                // v7.39 (round 455) — a mutation that finds no usable index
                // walks the table, and PG counts those tuples: its
                // `pg_stat_user_tables.seq_tup_read` covers DML, not just
                // SELECT. SPG only reported from `scan_visible`, which the
                // mutation paths do not use, so an UPDATE or DELETE that
                // scanned every row reported reading none — measured in
                // round 454 with a deliberately unindexed predicate over
                // 50k rows, which came back 0.
                //
                // Silently-wrong monitoring for any write workload, and the
                // instrument the round-452 investigation needs to tell a
                // range mutation's seek from a scan.
                table.note_seq_scan();
                (0..table.row_count()).collect()
            }
        };
        for (loop_n, &i) in candidate_positions.iter().enumerate() {
            if loop_n.is_multiple_of(256) {
                cancel.check()?;
            }
            // v7.37.16 (gate-on inventory) — a DELETE must not re-target
            // a tombstoned version (double-delete / doubled affected
            // counts under gate-on). No-op under gate-off.
            if !table.is_row_visible(i, &scan_snapshot) {
                continue;
            }
            let Some(row) = table.rows().get(i) else {
                continue;
            };
            // The extended row is for the PREDICATE only — `to_delete_rows`
            // below feeds RETURNING and the trigger walks, which expect the
            // table's own arity.
            let with_ctid;
            let where_row = if wants_ctid {
                let mut vals = row.values.clone();
                vals.push(Value::Tid(0, i as u32 + 1));
                with_ctid = spg_storage::Row::new(vals);
                &with_ctid
            } else {
                row
            };
            let keep = if let Some(hits) = &subquery_hits {
                hits.binary_search(&i).is_err()
            } else if let Some(w) = &stmt.where_ {
                let cond = eval::eval_expr(w, where_row, &ctx)?;
                !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)?
            } else {
                false
            };
            if !keep {
                positions.push(i);
                if need_old_rows {
                    to_delete_rows.push(row.values.clone());
                }
            }
        }
        // v7.39 (round 432) — MySQL `DELETE … ORDER BY … LIMIT n`: order the
        // MATCHED rows by the ORDER BY keys (evaluated on the pre-delete row),
        // keep the first `limit`, then restore position order — every pass
        // below (FK pairing, trigger walks, the apply loop) reads `positions`
        // assuming ascending row order. `to_delete_rows` is parallel to
        // `positions` when it is populated at all, so it moves in lockstep.
        if let Some(ol) = mysql_order_limit {
            if !ol.order_by.is_empty() {
                let mut keyed: Vec<(Vec<Value<'static>>, usize)> = positions
                    .iter()
                    .map(|&pos| {
                        let row = table
                            .rows()
                            .get(pos)
                            .expect("matched position was visible under scan_snapshot");
                        let keys: Vec<Value<'static>> = ol
                            .order_by
                            .iter()
                            .map(|o| eval::eval_expr(&o.expr, row, &ctx))
                            .collect::<Result<_, _>>()?;
                        Ok::<_, EngineError>((keys, pos))
                    })
                    .collect::<Result<_, _>>()?;
                keyed.sort_by(|a, b| {
                    for (k, o) in ol.order_by.iter().enumerate() {
                        let cmp = crate::order_by_value_cmp_in(
                            o.desc,
                            o.nulls_first,
                            &a.0[k],
                            &b.0[k],
                            true,
                        );
                        if cmp != core::cmp::Ordering::Equal {
                            return cmp;
                        }
                    }
                    core::cmp::Ordering::Equal
                });
                positions = keyed.into_iter().map(|(_, pos)| pos).collect();
            }
            if let Some(n) = ol.limit {
                positions.truncate(n as usize);
            }
            positions.sort_unstable();
            if need_old_rows {
                let kept: Vec<Vec<Value<'static>>> = positions
                    .iter()
                    .map(|&pos| {
                        table
                            .rows()
                            .get(pos)
                            .expect("kept position was matched above")
                            .values
                            .clone()
                    })
                    .collect();
                to_delete_rows = kept;
            }
        }
        // v7.6.3 / v7.6.4 — Stage 2: FK enforcement on the immutable
        // catalog. Release the mut borrow and run reverse-scan
        // against every child table whose FK targets this table.
        // RESTRICT / NoAction raise an error; CASCADE returns a
        // cascade plan that stage 3 applies after the primary delete.
        // SET NULL / SET DEFAULT remain Unsupported until v7.6.5.
        let _ = table;
        // v7.12.5 — BEFORE DELETE row-level triggers. Each fires
        // with NEW=None / OLD=pre-delete row; RETURN OLD (or NEW)
        // = proceed, RETURN NULL = skip the row entirely. The
        // filter must run BEFORE the FK cascade plan so cascaded
        // child rows track the trigger's skip-decision on the
        // parent.
        // v7.12.7 — embedded SQL queue.
        let mut deferred_embedded: Vec<triggers::DeferredEmbeddedStmt> = Vec::new();
        let mut raised_notices: Vec<(crate::NoticeSeverity, alloc::string::String)> = Vec::new();
        if !before_delete_triggers.is_empty() {
            let mut filtered_positions: Vec<usize> = Vec::with_capacity(positions.len());
            let mut filtered_old_rows: Vec<Vec<Value<'static>>> =
                Vec::with_capacity(to_delete_rows.len());
            for (pos, old_vals) in positions.iter().zip(to_delete_rows.iter()) {
                let old_row = Row::new(old_vals.clone());
                let mut cancel_this = false;
                for (fd, when, tgname) in &before_delete_triggers {
                    if !triggers::trigger_when_holds(when, None, Some(&old_row), &schema_cols)? {
                        continue;
                    }
                    let raise_sink = triggers::NoticeSink::default();
                    let fired = triggers::fire_row_trigger(
                        fd,
                        None,
                        Some(&old_row),
                        &stmt.table,
                        &schema_cols,
                        &[],
                        trigger_session_cfg.as_deref(),
                        false,
                        &triggers::TgMeta {
                            op: "DELETE",
                            name: tgname,
                            level: "ROW",
                        },
                        Some(&raise_sink),
                    );
                    raised_notices.extend(raise_sink.into_inner());
                    let (outcome, deferred) = fired.map_err(|e| {
                        EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}")))
                    })?;
                    deferred_embedded.extend(deferred);
                    if matches!(outcome, triggers::TriggerOutcome::Skip) {
                        cancel_this = true;
                        break;
                    }
                }
                if !cancel_this {
                    filtered_positions.push(*pos);
                    filtered_old_rows.push(old_vals.clone());
                }
            }
            positions = filtered_positions;
            to_delete_rows = filtered_old_rows;
        }
        let cascade_plan = plan_fk_parent_deletions(
            self.active_catalog(),
            &stmt.table,
            &positions,
            &to_delete_rows,
        )?;
        // Stage 3a — apply each FK child step (SET NULL / SET
        // DEFAULT / CASCADE delete) before deleting the parent.
        // The plan is already ordered: nulls/defaults first, then
        // cascade deletes (so a row mutated and later deleted
        // surfaces as deleted — though v7.6.5 doesn't produce
        // that overlap today).
        for step in &cascade_plan {
            apply_fk_child_step(self.active_catalog_mut(), step)?;
        }
        // Stage 3b — actually delete the original target rows.
        // v7.37.15 Phase C — stamp xmax on each row's header with
        // the enclosing tx's version BEFORE the physical removal.
        // Fetch the version BEFORE the table mut borrow so we
        // don't double-mut self. Shared across the whole DELETE
        // statement so all tombstones commit atomically.
        let xmax = self.writer_version_for_current_stmt();
        let _ = xmax; // pre-fetched; consumed below
        // v7.37.15 (Phase C.3, step 4a) — read the in-place kill switch
        // before the table mut borrow. When on, DELETE tombstones the
        // rows (xmax stamped, kept physically present) instead of
        // physically removing them: the now-uniformly-gated readers
        // skip them and vacuum reclaims later, giving concurrent
        // snapshots a consistent view. Default OFF → legacy physical
        // delete, byte-for-byte unchanged.
        //
        // KNOWN follow-ups before this gate can flip in production
        // (see plan appendix B): (6) WAL redo capture for the tombstone
        // — `mark_row_deleted` does not record a RowChange, so gate-on
        // is in-memory-only until Epic W wires tombstone redo; (1)
        // unique/FK constraint checks must adopt MVCC/dirty-snapshot
        // semantics so a re-insert of a tombstoned key succeeds; (8)
        // cold-tier tombstoning via the cold overlay (step 6).
        let inplace = self.mvcc_inplace();
        let table = self
            .active_catalog_mut()
            .get_mut(&stmt.table)
            .ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.table.clone(),
                })
            })?;
        let affected = if inplace {
            // v7.37.15 (Epic W durable-tombstone slice) — stamp xmax
            // ONLY on the in-place path; the gate-off path below removes
            // the rows via `delete_rows` (which records
            // `RowChange::Delete`), so tombstoning there too would
            // double-log the deletion.
            // v7.37.16 — BATCH tombstone: one pass, ONE multi-rowid
            // `RowChange::Tombstone` redo record instead of one per row
            // (~800 ns/row on a 10k-row DELETE). The return value counts
            // rows NEWLY tombstoned — positions come from the
            // visibility-gated WHERE scan above, so every entry is a
            // live, visible row and the count equals the distinct
            // position count the per-row loop used to compute.
            table.mark_rows_deleted(&positions, xmax) + cold_shadow_count
        } else {
            table.delete_rows(&positions) + cold_shadow_count
        };
        let _ = table;
        // v7.12.5 — AFTER DELETE row-level triggers fire post-write
        // with NEW=None / OLD=pre-delete row (each from the
        // already-snapshotted to_delete_rows). Return value is
        // ignored (matches PG AFTER semantics).
        if !after_delete_triggers.is_empty() {
            for old_vals in &to_delete_rows {
                let old_row = Row::new(old_vals.clone());
                for (fd, when, tgname) in &after_delete_triggers {
                    if !triggers::trigger_when_holds(when, None, Some(&old_row), &schema_cols)? {
                        continue;
                    }
                    let raise_sink = triggers::NoticeSink::default();
                    let fired = triggers::fire_row_trigger(
                        fd,
                        None,
                        Some(&old_row),
                        &stmt.table,
                        &schema_cols,
                        &[],
                        trigger_session_cfg.as_deref(),
                        true,
                        &triggers::TgMeta {
                            op: "DELETE",
                            name: tgname,
                            level: "ROW",
                        },
                        Some(&raise_sink),
                    );
                    raised_notices.extend(raise_sink.into_inner());
                    let (_outcome, deferred) = fired.map_err(|e| {
                        EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}")))
                    })?;
                    deferred_embedded.extend(deferred);
                }
            }
        }
        // v7.12.7 — drain trigger-emitted embedded SQL for this DELETE.
        self.queue_raised(raised_notices);
        self.execute_deferred_trigger_stmts(deferred_embedded, cancel)?;
        // v6.2.1 — auto-analyze modified-row tracking for DELETE.
        // This connection's slot, not the engine-wide predicate: with a
        // shared engine the global one is true while ANY connection has a
        // transaction open, and an autocommit write then went uncounted.
        // The table never crosses the analyze threshold, its statistics
        // stay stale, and the planner keeps choosing from them — measured
        // in round 796, 200 autocommit inserts counted as none.
        if self.catalog_change_is_committed() && affected > 0 {
            self.statistics
                .record_modifications(&stmt.table, affected as u64);
        }
        // v7.37.16 — autovacuum trigger (no-op gate-off / in-tx /
        // below the dead-row threshold).
        if affected > 0 {
            self.maybe_autovacuum(&stmt.table);
        }
        // v7.9.4 — RETURNING projection over the soon-to-be-gone
        // rows. `to_delete_rows` was snapshotted in stage 1 before
        // mutation, so the projection sees the pre-delete state
        // (matches PG semantics: DELETE RETURNING returns the row
        // as it was just before removal).
        if let Some(items) = &stmt.returning {
            // v7.39 (round 126) — DELETE: OLD = the deleted row (= default),
            // NEW = NULL (the row no longer exists).
            let old_for_returning = to_delete_rows.clone();
            return self.build_returning_rows_old_new(
                &stmt.table,
                stmt.alias.as_deref(),
                items,
                to_delete_rows,
                Some(old_for_returning),
                None,
            );
        }
        Ok(QueryResult::CommandOk {
            affected,
            modified_catalog: self.catalog_change_is_committed(),
        })
    }

    /// Snapshot the per-INSERT catalog state the row loop depends on,
    /// taken while the catalog is still immutably borrowable (before the
    /// `get_mut` window): the clock fn, BEFORE/AFTER row triggers + their
    /// session config, the column ENUM / SET variant lookups, and the
    /// AUTO_INCREMENT sequence floors.
    fn prepare_insert_snapshots(&self, table_name: &str) -> Result<InsertSnapshots, EngineError> {
        // v7.9.21 — snapshot the clock fn pointer before the mut
        // borrow on the catalog opens; runtime DEFAULT eval needs
        // it inside the row hot loop.
        let clock = self.clock;
        // v7.12.4 — snapshot row-level triggers + their referenced
        // functions before the mut borrow on the catalog opens.
        // Cloned out so the row hot loop can fire them without
        // re-borrowing the catalog (which would conflict with
        // table.insert's mutable borrow).
        let before_insert_triggers = self.snapshot_row_triggers(table_name, "INSERT", "BEFORE");
        let after_insert_triggers = self.snapshot_row_triggers(table_name, "INSERT", "AFTER");
        let trigger_session_cfg: Option<alloc::string::String> = self
            .session_params
            .get("default_text_search_config")
            .cloned();
        // v7.17.0 Phase 1.4 — snapshot the enum label lookup BEFORE
        // opening the mutable borrow on the table below. We need
        // catalog-level read access (enum_types lives at the
        // catalog level, not the table) and the upcoming mutable
        // borrow shadows it.
        let pre_borrow_column_meta: Vec<ColumnSchema> = {
            let preview_table = self.active_catalog().get(table_name).ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: String::from(table_name),
                })
            })?;
            preview_table.schema().columns.clone()
        };
        let enum_label_lookup: alloc::collections::BTreeMap<usize, Vec<String>> =
            pre_borrow_column_meta
                .iter()
                .enumerate()
                .filter_map(|(i, col)| {
                    // v7.17.0 Phase 3.P0-36 — MySQL inline ENUM
                    // variant lists take priority over the PG
                    // catalog enum_types lookup (they're
                    // column-local and authoritative when set).
                    if let Some(inline) = &col.inline_enum_variants {
                        return Some((i, inline.clone()));
                    }
                    col.user_enum_type.as_ref().and_then(|ename| {
                        self.active_catalog()
                            .enum_types()
                            .get(ename)
                            .map(|e| (i, e.labels.clone()))
                    })
                })
                .collect();
        // v7.17.0 Phase 3.P0-37 — MySQL inline SET variant lists.
        // Distinct from enum_label_lookup: SET validates that
        // every comma-separated token is in the variant list, and
        // canonicalises the cell to definition-order de-duped text.
        let set_variant_lookup: alloc::collections::BTreeMap<usize, Vec<String>> =
            pre_borrow_column_meta
                .iter()
                .enumerate()
                .filter_map(|(i, col)| col.inline_set_variants.as_ref().map(|vs| (i, vs.clone())))
                .collect();
        // v7.29 (round-23a) - when the column's implicit sequence
        // exists (born on first nextval/setval address), a setval
        // above the table MAX moves the next auto-assigned id:
        // assign from max(table_max + 1, last_value + 1). Tables
        // whose sequence was never addressed keep the bare max+1
        // path (identical pre-7.29 behaviour, no lookup cost
        // beyond one map probe per auto column per statement).
        let mut seq_floors: alloc::collections::BTreeMap<usize, i64> =
            alloc::collections::BTreeMap::new();
        for (i, col) in pre_borrow_column_meta.iter().enumerate() {
            if col.auto_increment
                && let Some(sd) = self.active_catalog().sequence(&alloc::format!(
                    "{}_{}_seq",
                    table_name,
                    col.name
                ))
            {
                // is_called=false (fresh RESTART / setval(_, false))
                // means the NEXT value is last_value itself.
                let floor = if sd.is_called {
                    sd.last_value + 1
                } else {
                    sd.last_value
                };
                seq_floors.insert(i, floor);
            }
        }
        Ok(InsertSnapshots {
            clock,
            before_insert_triggers,
            after_insert_triggers,
            trigger_session_cfg,
            enum_label_lookup,
            set_variant_lookup,
            seq_floors,
        })
    }

    /// v7.39 (round 137) — INSERT through a view carrying an INSTEAD OF INSERT
    /// trigger: build a NEW row over the view's columns for each VALUES tuple and
    /// fire the trigger(s). The function body does the real write; no row is
    /// written to the view itself.
    fn exec_insert_view_instead_of(
        &mut self,
        stmt: &spg_sql::ast::InsertStatement,
        triggers_list: Vec<(
            spg_storage::FunctionDef,
            alloc::string::String,
            alloc::string::String,
        )>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let view_def = self
            .active_catalog()
            .views_all()
            .get(&stmt.table)
            .cloned()
            .ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.table.clone(),
                })
            })?;
        let body = match spg_sql::parser::parse_statement(&view_def.body) {
            Ok(spg_sql::ast::Statement::Select(b)) => b,
            _ => {
                return Err(EngineError::Unsupported(
                    "INSTEAD OF: view body is not a SELECT".into(),
                ));
            }
        };
        let cols = self.view_output_columns(&body, &view_def.columns)?;
        let col_schemas: Vec<ColumnSchema> = cols
            .iter()
            .map(|(n, t)| ColumnSchema::new(n.clone(), *t, true))
            .collect();
        let trigger_cfg: Option<String> = self
            .session_params
            .get("default_text_search_config")
            .cloned();
        let eval_ctx = self.ev_ctx(&[], None);
        let empty = Row::new(Vec::new());
        let mut deferred_all: Vec<triggers::DeferredEmbeddedStmt> = Vec::new();
        let mut raised_notices: Vec<(crate::NoticeSeverity, alloc::string::String)> = Vec::new();
        let mut returned: Vec<Row<'static>> = Vec::new();
        let mut affected = 0usize;
        for tuple in &stmt.rows {
            let mut new_vals: Vec<Value<'static>> = alloc::vec![Value::Null; col_schemas.len()];
            match &stmt.columns {
                Some(names) => {
                    for (name, expr) in names.iter().zip(tuple) {
                        let pos = col_schemas
                            .iter()
                            .position(|c| &c.name == name)
                            .ok_or_else(|| {
                                EngineError::Eval(EvalError::ColumnNotFound { name: name.clone() })
                            })?;
                        new_vals[pos] =
                            self.eval_expr_with_correlated(expr, &empty, &eval_ctx, cancel, None)?;
                    }
                }
                None => {
                    for (i, expr) in tuple.iter().enumerate() {
                        if let Some(slot) = new_vals.get_mut(i) {
                            *slot = self
                                .eval_expr_with_correlated(expr, &empty, &eval_ctx, cancel, None)?;
                        }
                    }
                }
            }
            // The row RETURNING projects over is the one the trigger returns
            // (chained through multiple INSTEAD OF triggers); RETURN NULL skips.
            let mut current = Row::new(new_vals);
            let mut skipped = false;
            for (fd, _when, tgname) in &triggers_list {
                let raise_sink = triggers::NoticeSink::default();
                let fired = triggers::fire_row_trigger(
                    fd,
                    Some(current.clone()),
                    None,
                    &stmt.table,
                    &col_schemas,
                    &[],
                    trigger_cfg.as_deref(),
                    false,
                    &triggers::TgMeta {
                        op: "INSERT",
                        name: tgname,
                        level: "ROW",
                    },
                    Some(&raise_sink),
                );
                raised_notices.extend(raise_sink.into_inner());
                let (outcome, deferred) = fired.map_err(|e| {
                    EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}")))
                })?;
                deferred_all.extend(deferred);
                match outcome {
                    triggers::TriggerOutcome::Row(r) => current = r,
                    triggers::TriggerOutcome::Skip => {
                        skipped = true;
                        break;
                    }
                }
            }
            if !skipped {
                returned.push(current);
                affected += 1;
            }
        }
        self.queue_raised(raised_notices);
        self.execute_deferred_trigger_stmts(deferred_all, cancel)?;
        if let Some(items) = &stmt.returning {
            return self.project_instead_of_returning(items, &stmt.table, &col_schemas, &returned);
        }
        Ok(QueryResult::CommandOk {
            affected,
            modified_catalog: true,
        })
    }

    /// v7.39 (round 137, Phase 3) — project a RETURNING list over the rows an
    /// INSTEAD OF trigger returned (NEW for INSERT/UPDATE, OLD for DELETE). The
    /// items resolve against the view's column schema.
    fn project_instead_of_returning(
        &self,
        items: &[spg_sql::ast::SelectItem],
        view_name: &str,
        col_schemas: &[ColumnSchema],
        returned: &[Row<'static>],
    ) -> Result<QueryResult, EngineError> {
        let columns = self.derive_output_columns(items, col_schemas, view_name);
        let mut out: Vec<Row<'static>> = Vec::with_capacity(returned.len());
        for row in returned {
            out.push(self.project_row_simple(row, items, col_schemas, view_name)?);
        }
        Ok(QueryResult::Rows { columns, rows: out })
    }

    /// v7.39 (round 137/140) — materialise the OLD rows an UPDATE / DELETE
    /// targets: `SELECT * FROM <relation> [WHERE <pred>]`. Used both by the
    /// INSTEAD OF view path and by the DO ALSO rule wrapper (base tables).
    /// Returns the relation's output columns + the matching rows.
    fn scan_relation_rows(
        &self,
        view_name: &str,
        where_: &Option<spg_sql::ast::Expr>,
        cancel: CancelToken<'_>,
    ) -> Result<(Vec<ColumnSchema>, Vec<Row<'static>>), EngineError> {
        let sql = match where_ {
            Some(w) => alloc::format!("SELECT * FROM {view_name} WHERE {w}"),
            None => alloc::format!("SELECT * FROM {view_name}"),
        };
        let scan = match spg_sql::parser::parse_statement(&sql) {
            Ok(spg_sql::ast::Statement::Select(s)) => s,
            _ => {
                return Err(EngineError::Unsupported(
                    "INSTEAD OF: could not build the view scan".into(),
                ));
            }
        };
        match self.exec_select_cancel(&scan, cancel)? {
            QueryResult::Rows { columns, rows } => Ok((columns, rows)),
            _ => Err(EngineError::Unsupported(
                "INSTEAD OF: view scan did not return rows".into(),
            )),
        }
    }

    /// v7.39 (round 142) — a DELETE replaced by DO INSTEAD <command> rules: the
    /// matching rows are scanned only to bind OLD; the delete itself never runs.
    /// PG reports `DELETE 0` and rejects an outer RETURNING. The commands are
    /// full statements (they may themselves hit rules; the trigger-cascade
    /// recursion guard bounds a cycle).
    fn exec_delete_instead_command(
        &mut self,
        stmt: &spg_sql::ast::DeleteStatement,
        rules: Vec<spg_storage::RuleDef>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        if stmt.returning.is_some() {
            return Err(crate::rules::rule_returning_error("DELETE", &stmt.table));
        }
        let (columns, old_rows) = self.scan_relation_rows(&stmt.table, &stmt.where_, cancel)?;
        let rows: Vec<(Option<Row<'static>>, Option<Row<'static>>)> =
            old_rows.into_iter().map(|r| (None, Some(r))).collect();
        self.run_also_rules(&rules, &columns, &rows, cancel)?;
        Ok(QueryResult::CommandOk {
            affected: 0,
            modified_catalog: self.catalog_change_is_committed(),
        })
    }

    /// v7.39 (round 142) — an UPDATE replaced by DO INSTEAD <command> rules:
    /// scan the matching rows for OLD, derive NEW from the SET assignments, and
    /// run the commands per row. The update itself never runs; PG reports
    /// `UPDATE 0` and rejects an outer RETURNING.
    fn exec_update_instead_command(
        &mut self,
        stmt: &spg_sql::ast::UpdateStatement,
        rules: Vec<spg_storage::RuleDef>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        if stmt.returning.is_some() {
            return Err(crate::rules::rule_returning_error("UPDATE", &stmt.table));
        }
        let (columns, old_rows) = self.scan_relation_rows(&stmt.table, &stmt.where_, cancel)?;
        let mut pairs: Vec<(Option<Row<'static>>, Option<Row<'static>>)> =
            Vec::with_capacity(old_rows.len());
        {
            let ctx = self.ev_ctx(&columns, Some(&stmt.table));
            for old in &old_rows {
                let mut new_vals = old.values.clone();
                for (col, expr) in &stmt.assignments {
                    let pos = columns.iter().position(|c| &c.name == col).ok_or_else(|| {
                        EngineError::Eval(EvalError::ColumnNotFound { name: col.clone() })
                    })?;
                    new_vals[pos] =
                        self.eval_expr_with_correlated(expr, old, &ctx, cancel, None)?;
                }
                pairs.push((Some(Row::new(new_vals)), Some(old.clone())));
            }
        }
        self.run_also_rules(&rules, &columns, &pairs, cancel)?;
        Ok(QueryResult::CommandOk {
            affected: 0,
            modified_catalog: self.catalog_change_is_committed(),
        })
    }

    /// v7.39 (round 333, V59) — run a conditional `DO INSTEAD <command>`
    /// rule's action for the rows it claims, WITHOUT suppressing the
    /// original statement. `build_also_rule_stmts` applies each rule's own
    /// WHERE per row, so a row the condition misses contributes nothing
    /// here and is left to the (already narrowed) original.
    fn run_update_instead_actions(
        &mut self,
        stmt: &spg_sql::ast::UpdateStatement,
        rules: &[spg_storage::RuleDef],
        cancel: CancelToken<'_>,
    ) -> Result<(), EngineError> {
        let (columns, old_rows) = self.scan_relation_rows(&stmt.table, &stmt.where_, cancel)?;
        let mut pairs: Vec<(Option<Row<'static>>, Option<Row<'static>>)> =
            Vec::with_capacity(old_rows.len());
        {
            let ctx = self.ev_ctx(&columns, Some(&stmt.table));
            for old in &old_rows {
                let mut new_vals = old.values.clone();
                for (col, expr) in &stmt.assignments {
                    let pos = columns.iter().position(|c| &c.name == col).ok_or_else(|| {
                        EngineError::Eval(EvalError::ColumnNotFound { name: col.clone() })
                    })?;
                    new_vals[pos] =
                        self.eval_expr_with_correlated(expr, old, &ctx, cancel, None)?;
                }
                pairs.push((Some(Row::new(new_vals)), Some(old.clone())));
            }
        }
        self.run_also_rules(rules, &columns, &pairs, cancel)
    }

    /// v7.39 (round 140) — a DELETE carrying DO ALSO rules: capture the OLD rows
    /// first, run the real delete with rule rewrite suppressed, then fire each
    /// rule's command once per deleted row (OLD bound, no NEW).
    fn exec_delete_with_also(
        &mut self,
        stmt: &spg_sql::ast::DeleteStatement,
        also: Vec<spg_storage::RuleDef>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let (columns, old_rows) = self.scan_relation_rows(&stmt.table, &stmt.where_, cancel)?;
        self.rule_rewrite_active = true;
        let res = self.exec_delete_cancel(stmt, cancel);
        self.rule_rewrite_active = false;
        let res = res?;
        let rows: Vec<(Option<Row<'static>>, Option<Row<'static>>)> =
            old_rows.into_iter().map(|r| (None, Some(r))).collect();
        self.run_also_rules(&also, &columns, &rows, cancel)?;
        Ok(res)
    }

    /// v7.39 (round 140) — an UPDATE carrying DO ALSO rules: capture OLD, derive
    /// NEW per row by applying the SET assignments (mirroring the INSTEAD OF view
    /// path), run the real update with rule rewrite suppressed, then fire each
    /// rule's command once per updated row (OLD + NEW bound).
    fn exec_update_with_also(
        &mut self,
        stmt: &spg_sql::ast::UpdateStatement,
        also: Vec<spg_storage::RuleDef>,
        view_check: Option<ViewCheck>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let (columns, old_rows) = self.scan_relation_rows(&stmt.table, &stmt.where_, cancel)?;
        let mut pairs: Vec<(Option<Row<'static>>, Option<Row<'static>>)> =
            Vec::with_capacity(old_rows.len());
        {
            let ctx = self.ev_ctx(&columns, Some(&stmt.table));
            for old in &old_rows {
                let mut new_vals = old.values.clone();
                for (col, expr) in &stmt.assignments {
                    let pos = columns.iter().position(|c| &c.name == col).ok_or_else(|| {
                        EngineError::Eval(EvalError::ColumnNotFound { name: col.clone() })
                    })?;
                    new_vals[pos] =
                        self.eval_expr_with_correlated(expr, old, &ctx, cancel, None)?;
                }
                pairs.push((Some(Row::new(new_vals)), Some(old.clone())));
            }
        }
        self.rule_rewrite_active = true;
        let res = self.exec_update_cancel_inner(stmt, view_check, cancel);
        self.rule_rewrite_active = false;
        let res = res?;
        self.run_also_rules(&also, &columns, &pairs, cancel)?;
        Ok(res)
    }

    /// v7.39 (round 137, Phase 2) — UPDATE through a view with an INSTEAD OF
    /// UPDATE trigger: scan the view for matching OLD rows, derive NEW per row by
    /// applying the SET assignments, and fire the trigger(s). The function body
    /// does the real write.
    fn exec_update_view_instead_of(
        &mut self,
        stmt: &spg_sql::ast::UpdateStatement,
        triggers_list: Vec<(
            spg_storage::FunctionDef,
            alloc::string::String,
            alloc::string::String,
        )>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let (columns, old_rows) = self.scan_relation_rows(&stmt.table, &stmt.where_, cancel)?;
        let trigger_cfg: Option<String> = self
            .session_params
            .get("default_text_search_config")
            .cloned();
        let mut deferred_all: Vec<triggers::DeferredEmbeddedStmt> = Vec::new();
        let mut raised_notices: Vec<(crate::NoticeSeverity, alloc::string::String)> = Vec::new();
        let mut returned: Vec<Row<'static>> = Vec::new();
        let mut affected = 0usize;
        {
            let ctx = self.ev_ctx(&columns, Some(&stmt.table));
            for old in &old_rows {
                // NEW = OLD with the SET assignments applied (assignments and
                // WHERE reference the view's columns).
                let mut new_vals = old.values.clone();
                for (col, expr) in &stmt.assignments {
                    let pos = columns.iter().position(|c| &c.name == col).ok_or_else(|| {
                        EngineError::Eval(EvalError::ColumnNotFound { name: col.clone() })
                    })?;
                    new_vals[pos] =
                        self.eval_expr_with_correlated(expr, old, &ctx, cancel, None)?;
                }
                let mut current = Row::new(new_vals);
                let mut skipped = false;
                for (fd, _when, tgname) in &triggers_list {
                    let raise_sink = triggers::NoticeSink::default();
                    let fired = triggers::fire_row_trigger(
                        fd,
                        Some(current.clone()),
                        Some(old),
                        &stmt.table,
                        &columns,
                        &[],
                        trigger_cfg.as_deref(),
                        false,
                        &triggers::TgMeta {
                            op: "UPDATE",
                            name: tgname,
                            level: "ROW",
                        },
                        Some(&raise_sink),
                    );
                    raised_notices.extend(raise_sink.into_inner());
                    let (outcome, deferred) = fired.map_err(|e| {
                        EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}")))
                    })?;
                    deferred_all.extend(deferred);
                    match outcome {
                        triggers::TriggerOutcome::Row(r) => current = r,
                        triggers::TriggerOutcome::Skip => {
                            skipped = true;
                            break;
                        }
                    }
                }
                if !skipped {
                    returned.push(current);
                    affected += 1;
                }
            }
        }
        self.queue_raised(raised_notices);
        self.execute_deferred_trigger_stmts(deferred_all, cancel)?;
        if let Some(items) = &stmt.returning {
            return self.project_instead_of_returning(items, &stmt.table, &columns, &returned);
        }
        Ok(QueryResult::CommandOk {
            affected,
            modified_catalog: true,
        })
    }

    /// v7.39 (round 137, Phase 2) — DELETE through a view with an INSTEAD OF
    /// DELETE trigger: scan the view for matching OLD rows and fire the
    /// trigger(s) per row (OLD only).
    fn exec_delete_view_instead_of(
        &mut self,
        stmt: &spg_sql::ast::DeleteStatement,
        triggers_list: Vec<(
            spg_storage::FunctionDef,
            alloc::string::String,
            alloc::string::String,
        )>,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let (columns, old_rows) = self.scan_relation_rows(&stmt.table, &stmt.where_, cancel)?;
        let trigger_cfg: Option<String> = self
            .session_params
            .get("default_text_search_config")
            .cloned();
        let mut deferred_all: Vec<triggers::DeferredEmbeddedStmt> = Vec::new();
        let mut raised_notices: Vec<(crate::NoticeSeverity, alloc::string::String)> = Vec::new();
        let mut returned: Vec<Row<'static>> = Vec::new();
        let mut affected = 0usize;
        for old in &old_rows {
            // RETURNING on DELETE projects the OLD row the trigger returns.
            let mut current = old.clone();
            let mut skipped = false;
            for (fd, _when, tgname) in &triggers_list {
                let raise_sink = triggers::NoticeSink::default();
                let fired = triggers::fire_row_trigger(
                    fd,
                    None,
                    Some(old),
                    &stmt.table,
                    &columns,
                    &[],
                    trigger_cfg.as_deref(),
                    false,
                    &triggers::TgMeta {
                        op: "DELETE",
                        name: tgname,
                        level: "ROW",
                    },
                    Some(&raise_sink),
                );
                raised_notices.extend(raise_sink.into_inner());
                let (outcome, deferred) = fired.map_err(|e| {
                    EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}")))
                })?;
                deferred_all.extend(deferred);
                match outcome {
                    triggers::TriggerOutcome::Row(r) => current = r,
                    triggers::TriggerOutcome::Skip => {
                        skipped = true;
                        break;
                    }
                }
            }
            if !skipped {
                returned.push(current);
                affected += 1;
            }
        }
        self.queue_raised(raised_notices);
        self.execute_deferred_trigger_stmts(deferred_all, cancel)?;
        if let Some(items) = &stmt.returning {
            return self.project_instead_of_returning(items, &stmt.table, &columns, &returned);
        }
        Ok(QueryResult::CommandOk {
            affected,
            modified_catalog: true,
        })
    }

    pub(crate) fn exec_insert(
        &mut self,
        stmt: InsertStatement,
    ) -> Result<QueryResult, EngineError> {
        // v7.39 (SQLSTATE fidelity) — qualify a NOT NULL rejection
        // with the relation, PG's full 23502 form (the storage layer
        // that raises it has no table name).
        let table = stmt.table.clone();
        self.bump_table_change(&table);
        self.exec_insert_inner(stmt)
            .map_err(|e| enrich_not_null(e, &table))
    }

    fn exec_insert_inner(&mut self, mut stmt: InsertStatement) -> Result<QueryResult, EngineError> {
        // v7.37.43-T4.4 — writable CTE outer body: materialise every
        // leading WITH clause first (running any modifying CTE
        // bodies against the active catalog so their writes land
        // in the same transaction as the outer INSERT), then
        // execute the INSERT against an enriched catalog where
        // the CTE alias resolves to the materialised RETURNING
        // rows. Sentori 0065's
        //   WITH new_scopes AS (INSERT … RETURNING id, name)
        //   INSERT INTO org_identity_scopes …
        //     SELECT … FROM orgs o JOIN new_scopes …
        // is the exact shape we exercise here.
        if !stmt.ctes.is_empty() {
            return self.exec_insert_with_ctes(stmt);
        }
        // v7.17.0 Phase 1.1 — pre-resolve any nextval / currval /
        // setval calls against the catalog before the row loop. We
        // walk each tuple expression and replace matching
        // FunctionCall nodes with their concrete Literal. This
        // keeps `literal_expr_to_value` free of `&mut self` and
        // lets multi-row INSERT VALUES (… nextval('seq') …)
        // mint a separate sequence value per row.
        for tuple in &mut stmt.rows {
            for cell in tuple.iter_mut() {
                self.resolve_sequence_calls_in_expr(cell)?;
            }
        }
        // v7.39 (round 139) — unconditional DO INSTEAD NOTHING rule blocks the
        // INSERT. PG rejects a RETURNING on a suppressed statement; otherwise
        // drop every source row so the normal path inserts nothing (the
        // `INSERT 0 0` tag stays byte-identical).
        if self.rule_blocks_statement(&stmt.table, "INSERT") {
            if stmt.returning.is_some() {
                return Err(crate::rules::rule_returning_error("INSERT", &stmt.table));
            }
            stmt.rows.clear();
            stmt.select_source = None;
        }
        // v7.39 (round 137) — INSTEAD OF INSERT trigger on the target view: fire
        // the trigger per row instead of the auto-updatable redirect. The
        // function body does the real write. Takes precedence over redirect.
        let iof_triggers = self.snapshot_row_triggers(&stmt.table, "INSERT", "INSTEAD OF");
        if !iof_triggers.is_empty() {
            return self.exec_insert_view_instead_of(&stmt, iof_triggers, CancelToken::none());
        }
        // v7.37.19 (19.13) — auto-updatable view redirect.
        // INSERT INTO simple_view (cols) VALUES (...) rewrites to
        // INSERT INTO base_table (cols) VALUES (...) when the view
        // is a simple-query shape (SELECT col1, col2, ... FROM base
        // with no joins / WHERE / GROUP BY / aggregates / etc).
        // v7.38 (P6.46) — INSERT into a WHERE-view goes straight to the base;
        // the view's WHERE only filters reads (no WITH CHECK OPTION yet).
        // v7.39 (round 132) — WITH CHECK OPTION: an inserted row must satisfy
        // the view's WHERE. Captured here (view name = the pre-redirect table),
        // enforced below once the full base rows are assembled.
        let mut view_check: Option<ViewCheck> = None;
        // v7.39 (round 267) — the view exists but is not auto-updatable.
        // Without this the write fell through to the base-table lookup and
        // reported `relation "<view>" does not exist`, which is not merely
        // the wrong wording — it denies the existence of an object the
        // catalog plainly has.
        if let Err(reason) = view_redirect_checked(self.active_catalog(), &stmt.table) {
            if self.active_catalog().has_view(&stmt.table) {
                return Err(view_not_updatable_error(
                    &stmt.table,
                    &INSERT_VERB,
                    reason,
                    false,
                ));
            }
        }
        if let Some(vr) = view_redirect_to_simple_base(self.active_catalog(), &stmt.table) {
            let ViewRedirect {
                base,
                where_at_base: _,
                col_map,
                computed,
                view_cols,
                check_chain,
            } = vr;
            // v7.39 (round 154) — a computed view column is never a write
            // target (PG: "cannot insert into column … of view …").
            if let Some(cols) = &stmt.columns {
                for c in cols {
                    if let Some(cc) = computed.iter().find(|cc| cc.name == *c) {
                        return Err(view_computed_col_write_err(
                            "insert into",
                            &cc.origin_col,
                            &cc.origin_view,
                        ));
                    }
                }
            }
            let written_opt = self
                .active_catalog()
                .views_all()
                .get(&stmt.table)
                .map_or(0, |v| v.check_option);
            // v7.39 (round 152) — arm the check whenever any level of the
            // chain carries its own option too (PG, r152 probe P6).
            if written_opt != 0 || check_chain.iter().any(|(_, _, o)| *o != 0) {
                view_check = Some(ViewCheck {
                    written_opt,
                    chain: check_chain,
                });
            }
            // v7.39 (round 133) — column-renamed view: translate the target
            // column list to base columns. An explicit `(a, b)` list maps each
            // name; a positional insert becomes an explicit base-column list in
            // view-column order (so a subset / reordered view lands correctly).
            // Round 154 — with computed columns present the order comes from
            // `view_cols`: the first N view columns cover N positional values
            // (PG takes a short tuple, probe P12) and hitting a computed slot
            // errors.
            if !col_map.is_empty() || !computed.is_empty() {
                let map: alloc::collections::BTreeMap<String, String> =
                    col_map.iter().cloned().collect();
                match &mut stmt.columns {
                    Some(cols) => {
                        for c in cols.iter_mut() {
                            if let Some(b) = map.get(c) {
                                *c = b.clone();
                            }
                        }
                    }
                    None if computed.is_empty() => {
                        stmt.columns = Some(col_map.iter().map(|(_, b)| b.clone()).collect());
                    }
                    None => {
                        let n = stmt.rows.iter().map(Vec::len).max().unwrap_or(0);
                        let mut cols: Vec<String> = Vec::with_capacity(n);
                        for (view_col, base_col) in view_cols.iter().take(n) {
                            match base_col {
                                Some(b) => cols.push(b.clone()),
                                None => {
                                    let cc = computed
                                        .iter()
                                        .find(|cc| cc.name == *view_col)
                                        .expect("computed slot has an entry");
                                    return Err(view_computed_col_write_err(
                                        "insert into",
                                        &cc.origin_col,
                                        &cc.origin_view,
                                    ));
                                }
                            }
                        }
                        stmt.columns = Some(cols);
                    }
                }
                // v7.39 (round 134) — rewrite RETURNING view cols → base cols.
                if let Some(ret) = &stmt.returning {
                    stmt.returning = Some(rewrite_view_returning_items(
                        ret, &col_map, &computed, &view_cols,
                    ));
                }
            }
            stmt.table = base;
        }
        // v7.37.6-B(sentori Epic 2 P0)— route INSERTs that target
        // a partition parent down to the matching child(`Range`
        // half-open hit, fall back to `Default`). Sequence-resolution
        // and INSERT…SELECT desugar above run first so the per-tuple
        // routing sees fully literal expressions.
        if crate::partition::is_partition_parent(self.active_catalog(), &stmt.table)
            && stmt.select_source.is_none()
        {
            return self.exec_insert_route_partition_parent(stmt);
        }
        // v7.13.0 — `INSERT INTO t [(cols)] SELECT …` (mailrs
        // round-5 G4). Execute the inner SELECT first, then route
        // back through the regular VALUES code path with the
        // materialised rows.
        if let Some(select) = stmt.select_source.clone() {
            let select_result = self.exec_select_cancel(&select, CancelToken::none())?;
            let rows = match select_result {
                QueryResult::Rows { rows, .. } => rows,
                other => {
                    return Err(EngineError::Unsupported(alloc::format!(
                        "INSERT … SELECT: inner statement produced {other:?} instead of a row set"
                    )));
                }
            };
            let mut materialised: Vec<Vec<Expr>> = Vec::with_capacity(rows.len());
            for row in rows {
                let mut tuple: Vec<Expr> = Vec::with_capacity(row.values.len());
                for v in row.values {
                    tuple.push(value_to_literal_expr_permissive(v)?);
                }
                materialised.push(tuple);
            }
            let recurse = InsertStatement {
                ctes: Vec::new(),
                table: stmt.table,
                alias: stmt.alias,
                columns: stmt.columns,
                rows: materialised,
                select_source: None,
                on_conflict: stmt.on_conflict,
                returning: stmt.returning,
                overriding: stmt.overriding,
                // The materialised rows are still the IGNORE statement's.
                mysql_ignore: stmt.mysql_ignore,
            };
            return self.exec_insert(recurse);
        }
        // Snapshot everything the row loop needs from the catalog
        // before the mutable borrow below shadows it (clock, triggers,
        // enum/set variant lookups, sequence floors).
        let InsertSnapshots {
            clock,
            before_insert_triggers,
            after_insert_triggers,
            trigger_session_cfg,
            enum_label_lookup,
            set_variant_lookup,
            seq_floors,
        } = self.prepare_insert_snapshots(&stmt.table)?;
        // v7.39 (read01 round 55) — the catalog for user-named casts in VALUES.
        // Taken BEFORE the &mut borrow below (Catalog::clone is an Arc bump).
        let cat_for_insert = self.active_catalog().clone();
        let insert_mysql = self.backslash_escapes;
        // v7.39 (round 470) — read before the &mut borrow below, same
        // reason `cat_for_insert` is.
        let insert_non_strict = insert_mysql && !self.mysql_strict;
        // v7.39 (round 523) — read BEFORE the table borrow: the zone lives
        // in the session params, and `table` borrows self mutably.
        let session_zone = self.session_coercion();
        let insert_sess_bag = self.dml_session();
        let table = self
            .active_catalog_mut()
            .get_mut(&stmt.table)
            .ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.table.clone(),
                })
            })?;
        // v3.1.5: clone the columns vector only (not the whole
        // TableSchema — saves one String alloc for the table name).
        // We need an owned snapshot because we'll call `table.insert`
        // (mutable borrow on `table`) inside the row loop while
        // reading schema fields.
        let column_meta: Vec<ColumnSchema> = table.schema().columns.clone();
        let schema_cols_len = column_meta.len();
        let tuple_pos = build_tuple_pos(&stmt.columns, &column_meta, &stmt.table)?;
        let expected_tuple_len = stmt.columns.as_ref().map_or(schema_cols_len, Vec::len);
        // v7.6.2 — snapshot this table's FK list before the
        // mutable-borrow window so we can run parent lookups
        // against the immutable catalog after parsing. Empty vec is
        // the no-FK fast path; clone cost is O(fks * arity) which
        // is < 100 ns for typical schemas.
        let fks = table.schema().foreign_keys.clone();
        // Stage 1 — parse + AUTO_INC + coerce all rows under the
        // (immutable) table borrow.
        let overriding = stmt.overriding;
        let mut first_auto: Option<i64> = None;
        let mut all_values = parse_insert_rows(
            table,
            Some(&cat_for_insert),
            stmt.rows,
            &column_meta,
            &tuple_pos,
            expected_tuple_len,
            clock,
            &seq_floors,
            &enum_label_lookup,
            &set_variant_lookup,
            overriding,
            &mut first_auto,
            insert_mysql,
            // v7.39 (round 470) — `INSERT IGNORE` bends values, and so does
            // a non-strict `sql_mode`. Same conversion, different trigger.
            insert_mysql && stmt.mysql_ignore,
            insert_non_strict,
            session_zone.as_ref(),
            Some(&insert_sess_bag),
        )?;
        // Stage 2 — FK enforcement on the immutable catalog.
        // Non-lexical lifetimes release the mutable borrow on
        // `table` here since stage 1 was the last use. The
        // parent-table lookup runs before any row is committed.
        let uniqueness = table.schema().uniqueness_constraints.clone();
        // v7.39 (round 210) — EXCLUDE constraints run alongside uniqueness on
        // the post-ON-CONFLICT row set.
        let exclusions = table.schema().exclusion_constraints.clone();
        let _ = table;
        // v7.39 (round 712) — a constraint that is DEFERRED right now sits
        // out the statement-time check; the COMMIT sweep (or a SET
        // CONSTRAINTS … IMMEDIATE) runs it instead. This is the enforcement
        // half of F08: a transient duplicate healed before COMMIT is a
        // legal PG sequence that used to fail here on the first INSERT.
        // (Placed after `table`'s borrow ends — the filter reads the
        // catalog again for the synthesised constraint name.)
        let uniqueness: Vec<_> = if let Some(tx) = self.current_tx
            && let Some(st) = self.tx_catalogs.get(&tx)
        {
            let tname = stmt.table.clone();
            uniqueness
                .into_iter()
                .filter(|uc| {
                    if !uc.deferrable {
                        return true;
                    }
                    let conname = st
                        .catalog
                        .get(tname.as_str())
                        .map(|t| crate::system_catalog::pg_unique_conname(t, uc, &tname))
                        .unwrap_or_default();
                    !crate::constraints::uc_deferred_in(st, uc, &conname)
                })
                .collect()
        } else {
            uniqueness
        };
        // v7.39 (round 347, M2) — MySQL's LAST_INSERT_ID() reports the
        // FIRST value this statement generated, and is left ALONE by a
        // statement that generated none (measured on MariaDB 11: an
        // explicit id, an UPDATE and an insert into a table with no
        // AUTO_INCREMENT column all leave it unchanged). Set once the
        // borrow on `table` is released.
        if let Some(v) = first_auto {
            self.last_insert_id
                .store(v, core::sync::atomic::Ordering::Relaxed);
        }
        // v7.37.7(sentori Epic 3 P1)— stored generated-column
        // evaluation runs AFTER ordinary INSERT values are coerced
        // (so the expression sees the materialised row)but BEFORE
        // FK / CHECK / UNIQUE so those guards reason about the
        // computed value the way they would for any literal column.
        apply_generated_stored_columns(&column_meta, &mut all_values)?;
        // v7.39 (round 141) — conditional DO INSTEAD NOTHING: drop the proposed
        // rows a rule's WHERE (over NEW = the post-default row) blocks, before any
        // constraint check, matching PG's query-rewrite ordering.
        let cond_ins = self.conditional_instead_nothing_rules(&stmt.table, "INSERT");
        // v7.39 (round 333, V59) — a conditional INSTEAD rule that carries a
        // COMMAND runs it for the rows it claims, and those rows are then
        // dropped from the original INSERT by the same filter that serves
        // the NOTHING form. Measured on PG 18.4: with `WHERE new.id < 0 DO
        // INSTEAD INSERT INTO q59 …`, inserting (1, -1, 2, -2) answers
        // `INSERT 0 2`, leaves 1 and 2 in the table and -1 / -2 in q59.
        {
            let cond_cmd: Vec<spg_storage::RuleDef> = cond_ins
                .iter()
                .filter(|r| !r.commands.is_empty())
                .cloned()
                .collect();
            if !cond_cmd.is_empty() {
                if stmt.returning.is_some() {
                    return Err(crate::rules::rule_returning_error("INSERT", &stmt.table));
                }
                // `build_also_rule_stmts` applies each rule's own WHERE per
                // row, so handing it every proposed row fires only the
                // claimed ones.
                let pairs: Vec<(Option<Row<'static>>, Option<Row<'static>>)> = all_values
                    .iter()
                    .map(|v| (Some(Row::new(v.clone())), None))
                    .collect();
                self.run_also_rules(&cond_cmd, &column_meta, &pairs, CancelToken::none())?;
            }
        }
        if !cond_ins.is_empty() {
            let mut kept: Vec<Vec<Value<'static>>> = Vec::with_capacity(all_values.len());
            for row in all_values {
                let new_row = Row::new(row.clone());
                let mut blocked = false;
                for r in &cond_ins {
                    if triggers::trigger_when_holds(
                        &r.when_condition,
                        Some(&new_row),
                        None,
                        &column_meta,
                    )? {
                        blocked = true;
                        break;
                    }
                }
                if !blocked {
                    kept.push(row);
                }
            }
            all_values = kept;
        }
        // v7.39 (round 142) — DO INSTEAD <command>: the original INSERT is
        // suppressed entirely (no constraint checks against this table — nothing
        // lands in it); the command runs once per proposed row with NEW = the
        // post-default tuple. PG reports the SOURCE row count in the tag
        // (`INSERT 0 n`) and rejects an outer RETURNING.
        let instead_ins: Vec<spg_storage::RuleDef> = self
            .instead_command_rules(&stmt.table, "INSERT")
            .into_iter()
            // v7.39 (round 333, V59) — only the UNCONDITIONAL ones replace
            // the statement; the conditional ones were handled above.
            .filter(|r| r.when_condition.is_empty())
            .collect();
        if !instead_ins.is_empty() {
            if stmt.returning.is_some() {
                return Err(crate::rules::rule_returning_error("INSERT", &stmt.table));
            }
            let n = all_values.len();
            let rows: Vec<(Option<Row<'static>>, Option<Row<'static>>)> = all_values
                .into_iter()
                .map(|v| (Some(Row::new(v)), None))
                .collect();
            self.run_also_rules(&instead_ins, &column_meta, &rows, CancelToken::none())?;
            return Ok(QueryResult::CommandOk {
                affected: n,
                modified_catalog: self.catalog_change_is_committed(),
            });
        }
        // v7.39 (read01 round 117) — NOT NULL, checked pre-write over the fully
        // assembled rows so a violation aborts the whole statement (no partial
        // rows) and carries PG's `DETAIL: Failing row contains (...)`. Runs
        // before FK / CHECK, matching PG's not-null-first ordering.
        enforce_not_null(self.active_catalog(), &stmt.table, &all_values)?;
        if !fks.is_empty() {
            // v7.39 (round 288) — same split on the INSERT path.
            let now = self.immediate_fks(&fks);
            if !now.is_empty() {
                enforce_fk_inserts(self.active_catalog(), &stmt.table, &now, &all_values)?;
            }
        }
        // v7.13.0 — CHECK constraint enforcement (mailrs round-5 G3).
        // v7.39 (round 525) — with the session: a CHECK may name one.
        let check_sess = self.dml_session();
        enforce_check_constraints(
            self.active_catalog(),
            &stmt.table,
            &all_values,
            Some(&check_sess),
        )?;
        // v7.39 (RLS) Phase 2 — INSERT WITH CHECK: a policy-subject session's
        // new rows must satisfy the combined WITH CHECK predicate.
        self.rls_check_new_rows(
            &stmt.table,
            spg_storage::PolicyCmd::Insert,
            &column_meta,
            &all_values,
        )?;
        // v7.39 (round 132) — WITH CHECK OPTION on the assembled base rows.
        if let Some(check) = &view_check {
            self.enforce_view_check(check, &all_values, &column_meta, &stmt.table)?;
        }
        // NOTE (mailrs embed round-12): UNIQUE / PRIMARY KEY and
        // UNIQUE INDEX enforcement moved BELOW the ON CONFLICT
        // resolution pass. Running them first made every
        // `ON CONFLICT … DO UPDATE` upsert fail with a uniqueness
        // violation before the conflict handler could route the row
        // to an UPDATE — PG resolves the conflict action first and
        // only errors on rows no arbiter matched.
        // v7.9.8 / v7.9.9 — ON CONFLICT handling.
        //   - `DO NOTHING` filters `all_values` to non-conflicting
        //     rows + drops within-batch duplicates.
        //   - `DO UPDATE SET …` ALSO filters, but for each
        //     conflicting row it queues an UPDATE on the existing
        //     row using the incoming row's values as `EXCLUDED.*`.
        let (pending_updates, skipped_count) = match &stmt.on_conflict {
            Some(clause) => {
                let (kept, pending, skipped) = self.resolve_insert_on_conflict(
                    &stmt.table,
                    stmt.alias.as_deref(),
                    clause,
                    all_values,
                )?;
                all_values = kept;
                (pending, skipped)
            }
            None => (Vec::new(), 0usize),
        };
        // v7.9.19 — composite UNIQUE / PRIMARY KEY enforcement.
        // v7.9.29 — CREATE UNIQUE INDEX [WHERE pred] enforcement.
        // Both run on the post-ON-CONFLICT row set: conflicting rows
        // already left `all_values` (DO NOTHING drop / DO UPDATE
        // reroute), so what remains must be genuinely unique.
        let mysql = self.backslash_escapes;
        // v7.39 (round 427) — which MySQL upsert spelling produced the
        // DO UPDATE clause. Both lower onto the same AST; REPLACE is the one
        // whose assignment list is EMPTY (round 419's "take the incoming
        // row"). They charge an unchanged row differently, so the kind has
        // to reach `insert_parsed_rows`.
        let mysql_upsert_kind = if mysql {
            match stmt.on_conflict.as_ref().map(|c| &c.action) {
                Some(spg_sql::ast::OnConflictAction::Update { assignments, .. }) => {
                    Some(if assignments.is_empty() {
                        MysqlUpsertCount::Replace
                    } else {
                        MysqlUpsertCount::OnDuplicate
                    })
                }
                _ => None,
            }
        } else {
            None
        };
        enforce_uniqueness_inserts(
            self.active_catalog(),
            &stmt.table,
            &uniqueness,
            &all_values,
            mysql,
        )?;
        enforce_unique_index_inserts(self.active_catalog(), &stmt.table, &all_values, mysql)?;
        crate::constraints::enforce_exclusion_inserts(
            self.active_catalog(),
            &stmt.table,
            &exclusions,
            &all_values,
        )?;
        // v7.39 (round 140) — DO ALSO INSERT rules: capture the post-image rows
        // (RETURNING is forced on so defaults / sequences are reflected) and run
        // each rule's command per inserted row after the write completes.
        let also_ins = self.also_rules(&stmt.table, "INSERT");
        // v7.37.15 Phase C — pre-fetch the writer version BEFORE
        // the table mut borrow; otherwise the call below would
        // need &mut self while `table` already holds it. Shared
        // across every row this statement inserts (atomic commit
        // at the tx boundary).
        let xmin_for_stmt = self.writer_version_for_current_stmt();
        // v7.37.15 (Phase C.3, step 4b) — in-place kill switch for the
        // ON CONFLICT DO UPDATE tombstone+insert path, read before the
        // table mut borrow and threaded into insert_parsed_rows.
        let inplace_for_stmt = self.mvcc_inplace();
        // v7.39 (round 493) — the floor below which a deleted version is
        // invisible to everyone, read before the table mut borrow (it needs
        // `&self`) and published so the insert path can drop this key's dead
        // index entries while it already holds the posting list.
        //
        // Only under in-place MVCC: with the gate off a delete removes the
        // row physically and no dead versions exist to prune.
        let prune_horizon = if inplace_for_stmt {
            self.vacuum_oldest_active()
        } else {
            0
        };
        let table = self
            .active_catalog_mut()
            .get_mut(&stmt.table)
            .ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: stmt.table.clone(),
                })
            })?;
        table.set_prune_horizon(prune_horizon);
        // Stage 3 — insert the surviving rows + fire row triggers under
        // a fresh mutable borrow, then apply queued ON CONFLICT updates.
        let mut raised_notices: Vec<(crate::NoticeSeverity, alloc::string::String)> = Vec::new();
        let (returning_rows, deferred_embedded, affected, oc_pairs, oc_old_images) =
            insert_parsed_rows(
                table,
                xmin_for_stmt,
                // v7.39 (round 427) — MySQL's upsert row accounting. REPLACE
                // and ON DUPLICATE KEY UPDATE both lower onto the same
                // DO UPDATE clause; the parser distinguishes them by the
                // assignment list, which REPLACE leaves EMPTY ("take the
                // incoming row", round 419). They count an unchanged row
                // differently, so the distinction has to survive to here.
                mysql_upsert_kind,
                inplace_for_stmt,
                all_values,
                pending_updates,
                &before_insert_triggers,
                &after_insert_triggers,
                &column_meta,
                &stmt.table,
                trigger_session_cfg.as_deref(),
                stmt.returning.is_some() || !also_ins.is_empty(),
                &mut raised_notices,
            )?;
        let _ = skipped_count;
        // v7.12.7 — drop the table mut borrow and drain any
        // trigger-emitted embedded SQL queued during this INSERT.
        // The borrow has to release first because each deferred
        // stmt may UPDATE / INSERT / DELETE the same (or another)
        // table — including, in principle, this one.
        let _ = table;
        // v7.37.17 (E4 r3) — persist ON CONFLICT update pairs on the tx
        // (after the table borrow drops).
        self.record_update_pairs(&stmt.table, oc_pairs);
        self.queue_raised(raised_notices);
        self.execute_deferred_trigger_stmts(deferred_embedded, CancelToken::none())?;
        // v7.39 (round 140) — fire DO ALSO INSERT rules per inserted row. NEW is
        // the post-image (defaults / sequences applied); there is no OLD.
        if !also_ins.is_empty() {
            let rows: Vec<(Option<Row<'static>>, Option<Row<'static>>)> = returning_rows
                .iter()
                .map(|v| (Some(Row::new(v.clone())), None))
                .collect();
            self.run_also_rules(&also_ins, &column_meta, &rows, CancelToken::none())?;
        }
        // v7.9.4/v7.9.9 — RETURNING streams the rows that ended
        // up in the table after this statement (insert or
        // post-update on conflict).
        if let Some(items) = &stmt.returning {
            // v7.39 (round 126/129) — INSERT: NEW = the inserted-or-updated row
            // (= default). OLD is all-NULL for a plain insert and the pre-update
            // conflicting row for an ON CONFLICT DO UPDATE (r129 threads the
            // per-row OLD image out of insert_parsed_rows).
            let new_for_returning = returning_rows.clone();
            return self.build_returning_rows_old_new(
                &stmt.table,
                stmt.alias.as_deref(),
                items,
                returning_rows,
                Some(oc_old_images),
                Some(new_for_returning),
            );
        }
        // v6.2.1 — auto-analyze: track per-table modified-row
        // counter so the background sweep can decide when to
        // re-ANALYZE. Cheap path on the autocommit-wrap hot loop
        // — one BTreeMap entry update per INSERT batch.
        // This connection's slot, not the engine-wide predicate: with a
        // shared engine the global one is true while ANY connection has a
        // transaction open, and an autocommit write then went uncounted.
        // The table never crosses the analyze threshold, its statistics
        // stay stale, and the planner keeps choosing from them — measured
        // in round 796, 200 autocommit inserts counted as none.
        if self.catalog_change_is_committed() && affected > 0 {
            self.statistics
                .record_modifications(&stmt.table, affected as u64);
        }
        Ok(QueryResult::CommandOk {
            affected,
            modified_catalog: self.catalog_change_is_committed(),
        })
    }

    /// (ON CONFLICT) Resolve a `DO NOTHING` / `DO UPDATE` clause against
    /// the post-parse row set: drop or reroute conflicting rows, queue
    /// updates for the existing rows they collide with, and return the
    /// rows that survive to be inserted plus those queued updates.
    #[allow(clippy::type_complexity)]
    fn resolve_insert_on_conflict(
        &self,
        table_name: &str,
        alias: Option<&str>,
        clause: &spg_sql::ast::OnConflictClause,
        all_values: Vec<Vec<Value<'static>>>,
    ) -> Result<
        (
            Vec<Vec<Value<'static>>>,
            // (target_pos, new_row, old_row) — the pre-update row is the OLD
            // image for `RETURNING OLD.*` on the DO UPDATE path (v7.39 r129).
            Vec<(usize, Vec<Value<'static>>, Vec<Value<'static>>)>,
            usize,
        ),
        EngineError,
    > {
        let mut pending_updates: Vec<(usize, Vec<Value<'static>>, Vec<Value<'static>>)> =
            Vec::new();
        let mut skipped_count = 0usize;
        // v7.37.17 (17.6 siblings) — `ON CONFLICT ON CONSTRAINT
        // <name>` resolves the name to the constraint's columns via
        // the same synthetic naming convention pg_constraint
        // synthesises ({t}_pkey for the primary key, {t}_uniq{i}
        // for the i-th non-PK unique).
        let named_columns: Vec<String> = if let Some(cname) = &clause.constraint_name {
            let table = self.active_catalog().get(table_name).ok_or_else(|| {
                spg_storage::StorageError::TableNotFound {
                    name: alloc::string::String::from(table_name),
                }
            })?;
            let schema = table.schema();
            let mut found: Option<Vec<String>> = None;
            for uc in &schema.uniqueness_constraints {
                let synth = crate::system_catalog::pg_unique_conname(table, uc, table_name);
                if synth.eq_ignore_ascii_case(cname) {
                    found = Some(
                        uc.columns
                            .iter()
                            .filter_map(|&pos| schema.columns.get(pos).map(|c| c.name.clone()))
                            .collect(),
                    );
                    break;
                }
            }
            found.ok_or_else(|| {
                EngineError::Unsupported(alloc::format!(
                    "ON CONFLICT ON CONSTRAINT: no unique or primary key \
                     constraint named {cname:?} on table {table_name:?}"
                ))
            })?
        } else {
            Vec::new()
        };
        let target_cols: &[String] = if named_columns.is_empty() {
            clause.target_columns.as_slice()
        } else {
            named_columns.as_slice()
        };
        // v7.39 (round 240) — DO UPDATE has one row to find, so PG requires
        // an explicit conflict target for it (42601).
        if target_cols.is_empty()
            && !clause.mysql_lowered
            && matches!(clause.action, spg_sql::ast::OnConflictAction::Update { .. })
        {
            return Err(EngineError::Unsupported(
                "ON CONFLICT DO UPDATE requires inference specification or constraint name".into(),
            ));
        }
        // The arbiter column sets this clause watches: exactly one for an
        // explicit target (validated against the table's unique
        // constraints), every unique constraint and index for the bare
        // form — which used to pick ONE, so a DO NOTHING let a conflict on
        // any other constraint escalate to a duplicate-key error.
        let arbiters = crate::constraints::on_conflict_arbiters(
            self.active_catalog(),
            table_name,
            target_cols,
            clause.constraint_name.is_some(),
        )?;
        let mut kept: Vec<Vec<Value<'static>>> = Vec::with_capacity(all_values.len());
        // Per-arbiter batch-local keys (a bare clause tracks several sets).
        let mut seen_keys: Vec<Vec<Vec<Value<'static>>>> = alloc::vec![Vec::new(); arbiters.len()];
        for values in all_values {
            // SQL spec: NULL in any conflict column means "no conflict
            // possible" (NULL ≠ NULL for uniqueness) — UNLESS the
            // constraint says NULLS NOT DISTINCT (v7.29; mailrs
            // migrate-013 replays its seed row ('super', NULL) under
            // exactly that declaration).
            let mut collides_with_table = false;
            let mut collides_with_batch = false;
            // Which arbiter hit — the DO UPDATE row lookup keys off it (a
            // MySQL-lowered bare clause can have several).
            let mut hit_arbiter = 0usize;
            for (ai, (cols, nnd)) in arbiters.iter().enumerate() {
                let kt: Vec<&Value> = cols.iter().map(|&c| &values[c]).collect();
                let has_null = !nnd && kt.iter().any(|v| matches!(v, Value::Null));
                if has_null {
                    continue;
                }
                if on_conflict_keys_exist(self.active_catalog(), table_name, cols, &kt) {
                    if !collides_with_table && !collides_with_batch {
                        hit_arbiter = ai;
                    }
                    collides_with_table = true;
                }
                let kt_owned: Vec<Value<'static>> = kt.iter().map(|v| (*v).clone()).collect();
                if seen_keys[ai].iter().any(|k| k == &kt_owned) {
                    if !collides_with_table && !collides_with_batch {
                        hit_arbiter = ai;
                    }
                    collides_with_batch = true;
                }
            }
            let conflict_cols = &arbiters
                .get(hit_arbiter)
                .map(|(c, _)| c.clone())
                .unwrap_or_default();
            let key_tuple: Vec<&Value> = conflict_cols.iter().map(|&c| &values[c]).collect();
            let key_tuple_owned: Vec<Value<'static>> =
                key_tuple.iter().map(|v| (*v).clone()).collect();
            let collides = collides_with_table || collides_with_batch;
            match (&clause.action, collides) {
                (_, false) => {
                    for (ai, (cols, nnd)) in arbiters.iter().enumerate() {
                        let kt: Vec<Value<'static>> =
                            cols.iter().map(|&c| values[c].clone()).collect();
                        if *nnd || !kt.iter().any(|v| matches!(v, Value::Null)) {
                            seen_keys[ai].push(kt);
                        }
                    }
                    kept.push(values);
                }
                (spg_sql::ast::OnConflictAction::Nothing, true) => {
                    skipped_count += 1;
                }
                (
                    spg_sql::ast::OnConflictAction::Update {
                        assignments,
                        where_,
                    },
                    true,
                ) => {
                    // v7.38 (read01 sweep) — PG refuses to touch a row twice in
                    // one command: if this conflict key already appeared in the
                    // batch (either inserted by an earlier row or updated by an
                    // earlier DO UPDATE), it is a cardinality violation
                    // ("ON CONFLICT DO UPDATE command cannot affect row a second
                    // time").
                    if collides_with_batch {
                        // v7.39 (round 240) — PG's own wording (21000); the
                        // shared CardinalityViolation display is the scalar
                        // subquery's message and reads as an internal leak
                        // here.
                        return Err(EngineError::Unsupported(
                            "ON CONFLICT DO UPDATE command cannot affect row a second time".into(),
                        ));
                    }
                    // Claim this key so a later duplicate in the same batch is
                    // caught above, on the arbiter that produced the hit.
                    seen_keys[hit_arbiter].push(key_tuple_owned);
                    let target_pos = lookup_row_position_by_keys(
                        self.active_catalog(),
                        table_name,
                        conflict_cols,
                        &key_tuple,
                    )
                    .ok_or_else(|| {
                        EngineError::Unsupported(
                            "ON CONFLICT DO UPDATE: conflict detected but row \
                                 position could not be resolved (cold-tier row?)"
                                .into(),
                        )
                    })?;
                    // Snapshot the pre-update row: PG's `RETURNING OLD.*` on a
                    // DO UPDATE returns the conflicting row as it was BEFORE the
                    // update applied.
                    let old_row_vals: Vec<Value<'static>> = self
                        .active_catalog()
                        .get(table_name)
                        .and_then(|t| t.rows().get(target_pos).map(|r| r.values.clone()))
                        .unwrap_or_default();
                    // v7.39 (round 525) — with the session, like every
                    // other write path.
                    let oc_sess = self.dml_session();
                    let updated = apply_on_conflict_assignments(
                        self.active_catalog(),
                        table_name,
                        alias,
                        target_pos,
                        &values,
                        assignments,
                        where_.as_ref(),
                        Some(&oc_sess),
                    )?;
                    if let Some(new_row) = updated {
                        pending_updates.push((target_pos, new_row, old_row_vals));
                    } else {
                        skipped_count += 1;
                    }
                }
            }
        }
        Ok((kept, pending_updates, skipped_count))
    }

    /// v7.37.6-B(sentori Epic 2 P0)— route an INSERT whose target
    /// is a `PartitionRole::Parent` table down to the matching
    /// children. Each tuple's partition-key value picks one child
    /// (first hit on a `Range` child, else the `Default` child if
    /// any). Tuples land in per-child buckets that are re-issued
    /// through `exec_insert` against the child's name; the parent
    /// table itself never receives rows. Returns the summed
    /// `Modified { affected }` count across all children.
    ///
    /// v7.37.6-B contract caveats:
    ///   * ON CONFLICT / RETURNING on a partition-parent insert are
    ///     not supported(parser still allows them, but the engine
    ///     surfaces `Unsupported` here). PG ≤ 11 had the same
    ///     restriction; sentori doesn't rely on either against the
    ///     `events_partitioned` / `spans` tables.
    ///   * Column lists are honoured: the partition-key column must
    ///     be present in the INSERT column list(or omitted via the
    ///     "full schema order" form), otherwise routing has nothing
    ///     to read.
    pub(crate) fn exec_insert_route_partition_parent(
        &mut self,
        stmt: InsertStatement,
    ) -> Result<QueryResult, EngineError> {
        use spg_storage::PartitionRole;
        if stmt.on_conflict.is_some() {
            return Err(EngineError::Unsupported(alloc::format!(
                "INSERT INTO partition parent {:?} … ON CONFLICT: not supported \
                 at v7.37.6-B(route through the child explicitly)",
                stmt.table
            )));
        }
        if stmt.returning.is_some() {
            return Err(EngineError::Unsupported(alloc::format!(
                "INSERT INTO partition parent {:?} … RETURNING: not supported \
                 at v7.37.6-B(route through the child explicitly)",
                stmt.table
            )));
        }
        // Pull what we need from the parent + child catalog state
        // before any mutating call(per-child exec_insert below
        // takes &mut self).
        let parent_name = stmt.table.clone();
        let (parent_columns, key_position, parent_kind): (
            Vec<spg_storage::ColumnSchema>,
            usize,
            spg_storage::PartitionKind,
        ) = {
            let parent = self.active_catalog().get(&parent_name).ok_or_else(|| {
                EngineError::Storage(StorageError::TableNotFound {
                    name: parent_name.clone(),
                })
            })?;
            let cols = parent.schema().columns.clone();
            let (key_position, kind) = match &parent.schema().partition_role {
                Some(PartitionRole::Parent {
                    key_column_positions,
                    kind,
                    ..
                }) => (
                    *key_column_positions.first().ok_or_else(|| {
                        EngineError::Unsupported(alloc::format!(
                            "partition parent {parent_name:?} has empty key column list"
                        ))
                    })?,
                    *kind,
                ),
                _ => {
                    return Err(EngineError::Unsupported(alloc::format!(
                        "INSERT routing: {parent_name:?} is not a partition parent"
                    )));
                }
            };
            (cols, key_position, kind)
        };
        // Map the user's INSERT column list back to a parent-schema
        // position. Missing list means "every column in schema
        // order" — common shape; sentori's
        // INSERT INTO events_partitioned (project_id, received_at,
        //   payload) VALUES (…) takes the explicit branch.
        let tuple_key_index: usize = match &stmt.columns {
            None => key_position,
            Some(col_list) => {
                let key_col_name = parent_columns[key_position].name.as_str();
                col_list
                    .iter()
                    .position(|c| c.as_str().eq_ignore_ascii_case(key_col_name))
                    .ok_or_else(|| {
                        EngineError::Unsupported(alloc::format!(
                            "INSERT INTO {parent_name:?}: partition key column \
                             {key_col_name:?} is not in the INSERT column list, \
                             so the engine cannot route the tuple to a child"
                        ))
                    })?
            }
        };
        // Snapshot every child(name, role)so the per-row routing
        // loop only touches an immutable view of the catalog.
        let children = crate::partition::children_of_parent(self.active_catalog(), &parent_name);
        let mut range_children: Vec<(
            String,
            spg_storage::PartitionBound,
            spg_storage::PartitionBound,
        )> = Vec::new();
        // v7.37.16 (16.1) — LIST children: (name, accepted values).
        let mut list_children: Vec<(String, Vec<spg_storage::PartitionBound>)> = Vec::new();
        // v7.37.16 (16.2) — HASH children: (name, modulus, remainder).
        let mut hash_children: Vec<(String, u32, u32)> = Vec::new();
        let mut default_child: Option<String> = None;
        for child_name in &children {
            let Some(child) = self.active_catalog().get(child_name) else {
                continue;
            };
            match &child.schema().partition_role {
                Some(PartitionRole::Range { lower, upper, .. }) => {
                    range_children.push((child_name.clone(), lower.clone(), upper.clone()));
                }
                Some(PartitionRole::List { values, .. }) => {
                    list_children.push((child_name.clone(), values.clone()));
                }
                Some(PartitionRole::Hash {
                    modulus, remainder, ..
                }) => {
                    hash_children.push((child_name.clone(), *modulus, *remainder));
                }
                Some(PartitionRole::Default { .. }) => {
                    default_child = Some(child_name.clone());
                }
                _ => {}
            }
        }
        // Bucket tuples by destination child; preserve original row
        // order within each bucket so error messages reference the
        // tuple the user actually wrote.
        let mut buckets: alloc::collections::BTreeMap<String, Vec<Vec<Expr>>> =
            alloc::collections::BTreeMap::new();
        for tuple in stmt.rows {
            if tuple.len() <= tuple_key_index {
                return Err(EngineError::Unsupported(alloc::format!(
                    "INSERT INTO {parent_name:?}: tuple has {} expressions, \
                     partition key index is {tuple_key_index} — column list / \
                     value list shape mismatch",
                    tuple.len()
                )));
            }
            let key_expr = tuple[tuple_key_index].clone();
            let key_value = literal_expr_to_value(key_expr)?;
            if matches!(key_value, Value::Null) {
                return Err(EngineError::Unsupported(alloc::format!(
                    "INSERT INTO {parent_name:?}: partition key value is \
                     NULL, but the partition key is NOT NULL"
                )));
            }
            let target = match parent_kind {
                spg_storage::PartitionKind::Range => {
                    // v7.37 D.45 — route by the key's actual type (int-family /
                    // DATE / TIMESTAMPTZ / TEXT), not a forced TIMESTAMPTZ coercion.
                    let key_bound =
                        crate::partition::value_to_bound(&key_value).ok_or_else(|| {
                            EngineError::Unsupported(alloc::format!(
                                "INSERT INTO {parent_name:?}: partition key value \
                             {key_value:?} is not a supported RANGE key type"
                            ))
                        })?;
                    range_children
                        .iter()
                        .find(|(_, lo, hi)| crate::partition::value_in_range(&key_bound, lo, hi))
                        .map(|(name, _, _)| name.clone())
                        .or_else(|| default_child.clone())
                        .ok_or_else(|| {
                            EngineError::Unsupported(alloc::format!(
                                // v7.39 (round 621) — PG's exact wording, which the row-movement
                                // differential reads.
                                "no partition of relation {parent_name:?} found for row"
                            ))
                        })?
                }
                // v7.37.16 (16.1) — LIST routing: pick the first
                // child whose values contains the key.
                spg_storage::PartitionKind::List => list_children
                    .iter()
                    .find(|(_, values)| values.iter().any(|b| b.equals_value(&key_value)))
                    .map(|(name, _)| name.clone())
                    .or_else(|| default_child.clone())
                    .ok_or_else(|| {
                        EngineError::Unsupported(alloc::format!(
                            // v7.39 (round 621) — PG's exact wording, which the row-movement
                            // differential reads.
                            "no partition of relation {parent_name:?} found for row"
                        ))
                    })?,
                // v7.37.16 (16.2) — HASH routing: hash(key) mod
                // modulus picks the child. All HASH siblings under
                // a parent share a single modulus (DDL gate).
                spg_storage::PartitionKind::Hash => {
                    let h = crate::partition::pg_compatible_hash(&key_value);
                    hash_children
                        .iter()
                        .find(|(_, m, r)| h.rem_euclid(u64::from(*m)) == u64::from(*r))
                        .map(|(name, _, _)| name.clone())
                        .or_else(|| default_child.clone())
                        .ok_or_else(|| {
                            EngineError::Unsupported(alloc::format!(
                                // v7.39 (round 621) — PG's exact wording, which the row-movement
                                // differential reads.
                                "no partition of relation {parent_name:?} found for row"
                            ))
                        })?
                }
            };
            buckets.entry(target).or_default().push(tuple);
        }
        let mut total_affected: usize = 0;
        for (child_name, rows) in buckets {
            let child_stmt = InsertStatement {
                ctes: Vec::new(),
                table: child_name,
                alias: stmt.alias.clone(),
                columns: stmt.columns.clone(),
                rows,
                select_source: None,
                on_conflict: None,
                returning: None,
                overriding: stmt.overriding,
                // Routing a row to its partition keeps the statement's IGNORE.
                mysql_ignore: stmt.mysql_ignore,
            };
            let result = self.exec_insert(child_stmt)?;
            if let QueryResult::CommandOk { affected, .. } = result {
                total_affected += affected;
            }
        }
        Ok(QueryResult::CommandOk {
            affected: total_affected,
            modified_catalog: self.catalog_change_is_committed(),
        })
    }

    /// v7.37.43-T4.4 — execute an INSERT carrying leading `WITH cte
    /// AS (…)` clauses (PG writable CTE outer). Materialises every
    /// CTE through `materialise_ctes` (running any modifying CTE
    /// bodies against the active engine so their mutations land in
    /// the current transaction), then runs the outer INSERT
    /// against an enriched catalog where each CTE alias resolves
    /// to its materialised rows. We add CTE temp tables to the
    /// active catalog for the duration of the outer execution and
    /// drop them when done — keeping the writes from the outer
    /// INSERT on the original tables intact.
    pub(crate) fn exec_insert_with_ctes(
        &mut self,
        mut stmt: InsertStatement,
    ) -> Result<QueryResult, EngineError> {
        let cte_defs = core::mem::take(&mut stmt.ctes);
        self.guard_dml_target_not_cte(&stmt.table, &cte_defs)?;
        let mut outer_reads = alloc::collections::BTreeSet::new();
        collect_insert_reads(&stmt, &mut outer_reads);
        Self::guard_no_returning_cte_refs(&cte_defs, &outer_reads)?;
        let mut cte_defs = cte_defs;
        // v7.39 (round 157) — CTE-shadows-table on the write path: rename
        // the shadow temps and rewrite this statement's read references,
        // then re-verify nothing still reads the old name.
        let shadow_renames = self.resolve_cte_table_shadows(&mut cte_defs)?;
        if !shadow_renames.is_empty() {
            for (old, new) in &shadow_renames {
                rename_rel_in_insert(&mut stmt, old, new);
            }
            let mut reads = alloc::collections::BTreeSet::new();
            collect_insert_reads(&stmt, &mut reads);
            shadow_rename_leak_check(&shadow_renames, &reads)?;
        }
        self.run_with_cte_temps(&cte_defs, |engine| engine.exec_insert(stmt))
    }

    /// v7.39 (round 150) — PG rejects REFERENCING a data-modifying CTE
    /// that has no RETURNING clause at parse analysis ("WITH query
    /// \"d\" does not have a RETURNING clause", 0A000,
    /// parse_relation.c addRangeTableEntryForCTE): the alias has no
    /// result shape to scan. An unreferenced no-RETURNING body stays
    /// legal and still runs. Checked BEFORE any CTE materialises —
    /// PG errors before execution, so no side effect may land.
    fn guard_no_returning_cte_refs(
        ctes: &[spg_sql::ast::Cte],
        outer_reads: &alloc::collections::BTreeSet<String>,
    ) -> Result<(), EngineError> {
        for (i, cte) in ctes.iter().enumerate() {
            let no_returning = match &cte.body {
                spg_sql::ast::CteBody::Select(_) => continue,
                spg_sql::ast::CteBody::Insert(b) => b.returning.is_none(),
                spg_sql::ast::CteBody::Update(b) => b.returning.is_none(),
                spg_sql::ast::CteBody::Delete(b) => b.returning.is_none(),
                spg_sql::ast::CteBody::Merge(b) => b.returning.is_none(),
            };
            if !no_returning {
                continue;
            }
            let named = |t: &String| t.eq_ignore_ascii_case(&cte.name);
            let referenced = outer_reads.iter().any(named)
                || ctes.iter().enumerate().any(|(j, other)| {
                    if j == i {
                        return false;
                    }
                    let mut reads = alloc::collections::BTreeSet::new();
                    collect_cte_body_reads(&other.body, &mut reads);
                    reads.iter().any(named)
                });
            if referenced {
                return Err(EngineError::Unsupported(alloc::format!(
                    "WITH query \"{}\" does not have a RETURNING clause",
                    cte.name
                )));
            }
        }
        Ok(())
    }

    /// v7.39 (round 157) — write-path half of the CTE-shadows-table
    /// support (the read path is round 156): rename each shadowing CTE's
    /// temp to a fresh `__cte_shadow_*` name and rewrite the LATER
    /// sibling bodies' read references (the shadowing CTE's own
    /// non-recursive body keeps seeing the real table; a RECURSIVE
    /// self-reference renames — it is the CTE). Returns the rename
    /// pairs; the CALLER must rewrite the outer statement's reads and
    /// re-verify with `shadow_rename_leak_check`. Later bodies are
    /// verified here: a surviving old-name read means the rewriter
    /// missed a spot — fail honestly rather than read the table.
    fn resolve_cte_table_shadows(
        &self,
        ctes: &mut [spg_sql::ast::Cte],
    ) -> Result<alloc::vec::Vec<(String, String)>, EngineError> {
        let mut renames: alloc::vec::Vec<(usize, String, String)> = alloc::vec::Vec::new();
        for i in 0..ctes.len() {
            let old = ctes[i].name.clone();
            if self.active_catalog().get(&old).is_none() {
                continue;
            }
            let mut new = alloc::format!("__cte_shadow_{old}");
            let mut k = 0usize;
            while self.active_catalog().get(&new).is_some()
                || ctes.iter().any(|c| c.name.eq_ignore_ascii_case(&new))
            {
                k += 1;
                new = alloc::format!("__cte_shadow_{old}_{k}");
            }
            ctes[i].name = new.clone();
            if ctes[i].recursive {
                rename_rel_in_cte_body(&mut ctes[i].body, &old, &new);
            }
            for c in ctes.iter_mut().skip(i + 1) {
                if c.name.eq_ignore_ascii_case(&old) {
                    break;
                }
                rename_rel_in_cte_body(&mut c.body, &old, &new);
            }
            renames.push((i, old, new));
        }
        // Leak check over the later sibling bodies (the shadowing CTE's
        // own body and EARLIER bodies legitimately read the real table).
        for (i, old, _) in &renames {
            for c in ctes.iter().skip(i + 1) {
                let mut reads = alloc::collections::BTreeSet::new();
                collect_cte_body_reads(&c.body, &mut reads);
                if reads.iter().any(|t| t.eq_ignore_ascii_case(old)) {
                    return Err(cte_shadow_err(old));
                }
            }
        }
        Ok(renames.into_iter().map(|(_, o, n)| (o, n)).collect())
    }

    /// v7.39 (round 149) — PG resolves a DML target only against real
    /// relations, never a CTE of the same statement (`WITH c AS (…)
    /// DELETE FROM c` errors "relation \"c\" does not exist"). SPG's
    /// temp-table CTE machinery would otherwise resolve the target to
    /// the just-installed temp — the write lands there and vanishes
    /// when the statement ends. Must run BEFORE the temps install
    /// (the catalog probe distinguishes a real same-named table).
    fn guard_dml_target_not_cte(
        &self,
        target: &str,
        ctes: &[spg_sql::ast::Cte],
    ) -> Result<(), EngineError> {
        if ctes.iter().any(|c| c.name.eq_ignore_ascii_case(target))
            && self.active_catalog().get(target).is_none()
        {
            return Err(EngineError::Storage(
                spg_storage::StorageError::TableNotFound {
                    name: target.into(),
                },
            ));
        }
        Ok(())
    }

    /// v7.37.43-T4.4 — UPDATE counterpart of `exec_insert_with_ctes`.
    pub(crate) fn exec_update_with_ctes(
        &mut self,
        mut stmt: spg_sql::ast::UpdateStatement,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let cte_defs = core::mem::take(&mut stmt.ctes);
        self.guard_dml_target_not_cte(&stmt.table, &cte_defs)?;
        let mut outer_reads = alloc::collections::BTreeSet::new();
        collect_update_reads(&stmt, &mut outer_reads);
        Self::guard_no_returning_cte_refs(&cte_defs, &outer_reads)?;
        let mut cte_defs = cte_defs;
        let shadow_renames = self.resolve_cte_table_shadows(&mut cte_defs)?;
        if !shadow_renames.is_empty() {
            for (old, new) in &shadow_renames {
                rename_rel_in_update(&mut stmt, old, new);
            }
            let mut reads = alloc::collections::BTreeSet::new();
            collect_update_reads(&stmt, &mut reads);
            shadow_rename_leak_check(&shadow_renames, &reads)?;
        }
        self.run_with_cte_temps(&cte_defs, |engine| {
            // v7.39 (round 157) — uncorrelated-subquery materialisation
            // for SET / WHERE runs HERE, after the CTE temps installed
            // (the dispatch-level pass skips statements with a WITH).
            for (_, e) in &mut stmt.assignments {
                engine.resolve_expr_subqueries(e, cancel)?;
            }
            if let Some(w) = &mut stmt.where_ {
                engine.resolve_expr_subqueries(w, cancel)?;
            }
            engine.exec_update_cancel(&stmt, cancel)
        })
    }

    /// SELECT with a data-modifying CTE body (`WITH d AS (DELETE …
    /// RETURNING …) SELECT … FROM d`) — the writes must land
    /// transactionally, so the outer SELECT routes through the
    /// same &mut temp-table machinery the DML outers use.
    pub(crate) fn exec_select_with_modifying_ctes(
        &mut self,
        mut stmt: spg_sql::ast::SelectStatement,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let cte_defs = core::mem::take(&mut stmt.ctes);
        let mut outer_reads = alloc::collections::BTreeSet::new();
        crate::acl::collect_read_tables(&stmt, &mut outer_reads);
        Self::guard_no_returning_cte_refs(&cte_defs, &outer_reads)?;
        let mut cte_defs = cte_defs;
        let shadow_renames = self.resolve_cte_table_shadows(&mut cte_defs)?;
        if !shadow_renames.is_empty() {
            for (old, new) in &shadow_renames {
                rename_rel_in_select(&mut stmt, old, new);
            }
            let mut reads = alloc::collections::BTreeSet::new();
            crate::acl::collect_read_tables(&stmt, &mut reads);
            shadow_rename_leak_check(&shadow_renames, &reads)?;
        }
        self.run_with_cte_temps(&cte_defs, |engine| engine.exec_select_cancel(&stmt, cancel))
    }

    /// v7.39 (round 149) — MERGE counterpart of `exec_insert_with_ctes`.
    pub(crate) fn exec_merge_with_ctes(
        &mut self,
        mut stmt: spg_sql::ast::MergeStatement,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let cte_defs = core::mem::take(&mut stmt.ctes);
        self.guard_dml_target_not_cte(&stmt.target, &cte_defs)?;
        let mut outer_reads = alloc::collections::BTreeSet::new();
        collect_merge_reads(&stmt, &mut outer_reads);
        Self::guard_no_returning_cte_refs(&cte_defs, &outer_reads)?;
        let mut cte_defs = cte_defs;
        let shadow_renames = self.resolve_cte_table_shadows(&mut cte_defs)?;
        if !shadow_renames.is_empty() {
            for (old, new) in &shadow_renames {
                rename_rel_in_merge(&mut stmt, old, new);
            }
            let mut reads = alloc::collections::BTreeSet::new();
            collect_merge_reads(&stmt, &mut reads);
            shadow_rename_leak_check(&shadow_renames, &reads)?;
        }
        self.run_with_cte_temps(&cte_defs, |engine| engine.exec_merge_cancel(&stmt, cancel))
    }

    /// v7.37.43-T4.4 — DELETE counterpart of `exec_insert_with_ctes`.
    pub(crate) fn exec_delete_with_ctes(
        &mut self,
        mut stmt: spg_sql::ast::DeleteStatement,
        cancel: CancelToken<'_>,
    ) -> Result<QueryResult, EngineError> {
        let cte_defs = core::mem::take(&mut stmt.ctes);
        self.guard_dml_target_not_cte(&stmt.table, &cte_defs)?;
        let mut outer_reads = alloc::collections::BTreeSet::new();
        collect_delete_reads(&stmt, &mut outer_reads);
        Self::guard_no_returning_cte_refs(&cte_defs, &outer_reads)?;
        let mut cte_defs = cte_defs;
        let shadow_renames = self.resolve_cte_table_shadows(&mut cte_defs)?;
        if !shadow_renames.is_empty() {
            for (old, new) in &shadow_renames {
                rename_rel_in_delete(&mut stmt, old, new);
            }
            let mut reads = alloc::collections::BTreeSet::new();
            collect_delete_reads(&stmt, &mut reads);
            shadow_rename_leak_check(&shadow_renames, &reads)?;
        }
        self.run_with_cte_temps(&cte_defs, |engine| {
            // v7.39 (round 157) — see exec_update_with_ctes: the
            // uncorrelated-subquery pass runs after the temps install.
            if let Some(w) = &mut stmt.where_ {
                engine.resolve_expr_subqueries(w, cancel)?;
            }
            engine.exec_delete_cancel(&stmt, cancel)
        })
    }

    /// v7.37.43-T4.4 — install each CTE alias as a temp table on
    /// the active catalog (running modifying CTE bodies through
    /// `self` so writes land transactionally), execute the
    /// caller-supplied closure, then drop every CTE temp table
    /// regardless of success or failure (RAII-style cleanup
    /// keeps the catalog in a consistent shape after the outer
    /// statement returns).
    fn run_with_cte_temps<F>(
        &mut self,
        ctes: &[spg_sql::ast::Cte],
        body: F,
    ) -> Result<QueryResult, EngineError>
    where
        F: FnOnce(&mut Engine) -> Result<QueryResult, EngineError>,
    {
        // v7.39 (round 149) — a modifying CTE body's target must be a
        // real relation too, never a sibling CTE (PG: relation does
        // not exist). Checked before any temp installs so the catalog
        // probe still distinguishes a real same-named table.
        for cte in ctes {
            let body_target = match &cte.body {
                spg_sql::ast::CteBody::Select(_) => None,
                spg_sql::ast::CteBody::Insert(i) => Some(i.table.as_str()),
                spg_sql::ast::CteBody::Update(u) => Some(u.table.as_str()),
                spg_sql::ast::CteBody::Delete(d) => Some(d.table.as_str()),
                spg_sql::ast::CteBody::Merge(m) => Some(m.target.as_str()),
            };
            if let Some(t) = body_target {
                self.guard_dml_target_not_cte(t, ctes)?;
            }
        }
        // Phase 1 — execute / materialise each CTE in declaration
        // order, mutating self (real-table writes go through) and
        // capturing the rows that will populate the CTE alias.
        let mut installed: alloc::vec::Vec<String> = alloc::vec::Vec::with_capacity(ctes.len());
        for cte in ctes {
            if self.active_catalog().get(&cte.name).is_some() {
                self.drop_cte_temps(&installed);
                return Err(EngineError::Unsupported(alloc::format!(
                    "CTE name {:?} shadows an existing table; rename the CTE",
                    cte.name
                )));
            }
            let result = self.materialise_one_cte_to_temp(cte);
            let (columns, rows) = match result {
                Ok(out) => out,
                Err(e) => {
                    self.drop_cte_temps(&installed);
                    return Err(e);
                }
            };
            let inferred = crate::select::infer_column_types(&columns, &rows);
            let mut columns = inferred;
            if !cte.column_overrides.is_empty() {
                if cte.column_overrides.len() != columns.len() {
                    self.drop_cte_temps(&installed);
                    return Err(EngineError::Unsupported(alloc::format!(
                        "CTE {:?} column list has {} names but body returns {} columns",
                        cte.name,
                        cte.column_overrides.len(),
                        columns.len()
                    )));
                }
                for (col, name) in columns.iter_mut().zip(cte.column_overrides.iter()) {
                    col.name.clone_from(name);
                }
            }
            let schema = spg_storage::TableSchema::new(cte.name.clone(), columns);
            if let Err(e) = self.active_catalog_mut().create_table(schema) {
                self.drop_cte_temps(&installed);
                return Err(EngineError::Storage(e));
            }
            installed.push(cte.name.clone());
            let table = self
                .active_catalog_mut()
                .get_mut(&cte.name)
                .expect("just-created CTE temp must exist");
            for row in rows {
                if let Err(e) = table.insert(row) {
                    let installed_clone = installed.clone();
                    self.drop_cte_temps(&installed_clone);
                    return Err(EngineError::Storage(e));
                }
            }
        }
        // Phase 2 — execute the outer statement against the
        // enriched catalog.
        let outcome = body(self);
        // Phase 3 — drop CTE temp tables regardless of outcome.
        self.drop_cte_temps(&installed);
        outcome
    }

    fn drop_cte_temps(&mut self, names: &[String]) {
        for name in names {
            let _ = self.active_catalog_mut().drop_table(name);
        }
    }

    /// v7.37.43-T4.4 — materialise one CTE body, running any
    /// modifying statement against the active catalog and
    /// capturing the RETURNING projection.
    fn materialise_one_cte_to_temp(
        &mut self,
        cte: &spg_sql::ast::Cte,
    ) -> Result<
        (
            alloc::vec::Vec<spg_storage::ColumnSchema>,
            alloc::vec::Vec<Row<'static>>,
        ),
        EngineError,
    > {
        use spg_sql::ast::CteBody;
        let cancel = CancelToken::none();
        match &cte.body {
            CteBody::Select(s) if cte.recursive => {
                // Reuse the SELECT-side recursive helper by wrapping
                // through a synthetic Cte (CteBody::Select).
                let snapshot = self.active_catalog().clone();
                let synthetic = spg_sql::ast::Cte {
                    name: cte.name.clone(),
                    body: CteBody::Select(s.clone()),
                    recursive: true,
                    column_overrides: cte.column_overrides.clone(),
                    search: None,
                    cycle: None,
                };
                let (columns, rows) =
                    self.materialise_recursive_cte(&synthetic, &snapshot, cancel)?;
                Ok((columns, rows))
            }
            CteBody::Select(s) => {
                let result = self.exec_select_cancel(s, cancel)?;
                match result {
                    QueryResult::Rows { columns, rows } => Ok((columns, rows)),
                    other => Err(EngineError::Unsupported(alloc::format!(
                        "CTE {:?} SELECT body produced {other:?}",
                        cte.name
                    ))),
                }
            }
            CteBody::Insert(body) => {
                // round 151 — a WITH-headed body keeps its own ctes; the
                // body statement routes through its writable-CTE entry.
                let body = (**body).clone();
                let result = self.exec_insert(body)?;
                self.cte_returning_or_empty(&cte.name, result)
            }
            CteBody::Update(body) => {
                // round 151 — a WITH-headed body keeps its own ctes; the
                // body statement routes through its writable-CTE entry.
                let body = (**body).clone();
                let result = self.exec_update_cancel(&body, cancel)?;
                self.cte_returning_or_empty(&cte.name, result)
            }
            CteBody::Delete(body) => {
                // round 151 — a WITH-headed body keeps its own ctes; the
                // body statement routes through its writable-CTE entry.
                let body = (**body).clone();
                let result = self.exec_delete_cancel(&body, cancel)?;
                self.cte_returning_or_empty(&cte.name, result)
            }
            // v7.39 (round 149) — PG 17 allows MERGE as a CTE body;
            // without RETURNING the alias materialises empty, as with
            // the other data-modifying bodies.
            CteBody::Merge(body) => {
                // round 151 — a WITH-headed body keeps its own ctes; the
                // body statement routes through its writable-CTE entry.
                let body = (**body).clone();
                let result = self.exec_merge_cancel(&body, cancel)?;
                self.cte_returning_or_empty(&cte.name, result)
            }
        }
    }

    fn cte_returning_or_empty(
        &self,
        cte_name: &str,
        result: QueryResult,
    ) -> Result<
        (
            alloc::vec::Vec<spg_storage::ColumnSchema>,
            alloc::vec::Vec<Row<'static>>,
        ),
        EngineError,
    > {
        match result {
            QueryResult::Rows { columns, rows } => Ok((columns, rows)),
            QueryResult::CommandOk { .. } => {
                let placeholder = spg_storage::ColumnSchema::new(
                    alloc::format!("{cte_name}_returning_absent"),
                    spg_storage::DataType::Text,
                    true,
                );
                Ok((alloc::vec![placeholder], alloc::vec::Vec::new()))
            }
        }
    }
}

impl Engine {
    /// v7.9.4 — INSERT / UPDATE / DELETE RETURNING projector.
    /// Given the table name, the user-supplied projection items,
    /// and the mutated rows (post-insert / post-update values, or
    /// pre-delete snapshot), build a `QueryResult::Rows` whose
    /// schema describes the projected columns. Mailrs migration
    /// blocker #1.
    fn build_returning_rows(
        &self,
        table_name: &str,
        items: &[SelectItem],
        mutated_rows: Vec<Vec<Value<'static>>>,
    ) -> Result<QueryResult, EngineError> {
        self.build_returning_rows_old_new(table_name, None, items, mutated_rows, None, None)
    }

    /// v7.39 (read01 round 126) — RETURNING with optional `OLD.col` / `NEW.col`
    /// references (PG18). `default_rows` are the bare-column source (NEW for
    /// INSERT/UPDATE, OLD for DELETE); `old_rows` / `new_rows` back the `OLD.`
    /// / `NEW.` qualifiers (`None` = the row didn't exist on that side, so those
    /// columns read NULL — OLD for INSERT, NEW for DELETE). When no OLD/NEW
    /// reference is present this is the plain single-row projection.
    fn build_returning_rows_old_new(
        &self,
        table_name: &str,
        // v7.39 (round 241) — the target alias when the statement gave one:
        // the catalog lookup needs the real name, the expression qualifier
        // (and `alias.*` wildcards) the alias.
        alias: Option<&str>,
        items: &[SelectItem],
        default_rows: Vec<Vec<Value<'static>>>,
        old_rows: Option<Vec<Vec<Value<'static>>>>,
        new_rows: Option<Vec<Vec<Value<'static>>>>,
    ) -> Result<QueryResult, EngineError> {
        let table = self.active_catalog().get(table_name).ok_or_else(|| {
            EngineError::Storage(StorageError::TableNotFound {
                name: table_name.into(),
            })
        })?;
        let schema_cols = table.schema().columns.clone();
        // Output columns come from the ORIGINAL items: `derive_output_columns`
        // resolves `OLD.v` / `NEW.v` by name (ignoring the qualifier) to v's
        // name + type, matching PG.
        let qualifier = alias.unwrap_or(table_name);
        let columns = self.derive_output_columns(items, &schema_cols, qualifier);

        // Rewrite OLD./NEW. qualifiers to synthetic bare columns for value
        // resolution. If none appear, take the plain fast path.
        let mut items_rw = items.to_vec();
        let mut uses_old_new = false;
        for it in &mut items_rw {
            match it {
                SelectItem::Expr { expr, .. } => uses_old_new |= rewrite_returning_old_new(expr),
                // v7.39 (round 128) — `OLD.*` / `NEW.*` also need the synthetic
                // path (a plain `t.*` / `*` stays on the fast path).
                SelectItem::QualifiedWildcard(q) => {
                    let ql = q.to_ascii_lowercase();
                    uses_old_new |= ql == "old" || ql == "new";
                }
                SelectItem::Wildcard => {}
            }
        }
        if !uses_old_new {
            let mut out_rows: Vec<Row<'static>> = Vec::with_capacity(default_rows.len());
            for values in default_rows {
                let row = Row::new(values);
                let projected = self.project_row_simple(&row, items, &schema_cols, qualifier)?;
                out_rows.push(projected);
            }
            return Ok(QueryResult::Rows {
                columns,
                rows: out_rows,
            });
        }

        // Synthetic schema: [table cols | __ret_old_<c> | __ret_new_<c>].
        let arity = schema_cols.len();
        let mut syn_schema = schema_cols.clone();
        for c in &schema_cols {
            let mut oc = c.clone();
            oc.name = alloc::format!("__ret_old_{}", c.name);
            syn_schema.push(oc);
        }
        for c in &schema_cols {
            let mut nc = c.clone();
            nc.name = alloc::format!("__ret_new_{}", c.name);
            syn_schema.push(nc);
        }
        let n = default_rows.len();
        let null_block = || alloc::vec![alloc::vec![Value::Null; arity]; n];
        let old_rows = old_rows.unwrap_or_else(null_block);
        let new_rows = new_rows.unwrap_or_else(null_block);
        let ctx = self.ev_ctx(&syn_schema, Some(qualifier));
        let cancel = CancelToken::none();
        let mut out_rows: Vec<Row<'static>> = Vec::with_capacity(n);
        for i in 0..n {
            let mut syn_vals = default_rows[i].clone();
            syn_vals.extend(old_rows[i].iter().cloned());
            syn_vals.extend(new_rows[i].iter().cloned());
            let syn_row = Row::new(syn_vals);
            let mut vals: Vec<Value<'static>> = Vec::with_capacity(items_rw.len());
            for it in &items_rw {
                match it {
                    // A bare wildcard expands to the default (table) columns
                    // only, never the synthetic OLD/NEW blocks.
                    SelectItem::Wildcard => vals.extend(default_rows[i].iter().cloned()),
                    // `OLD.*` / `NEW.*` expand the pre-image / post-image block;
                    // a table-qualified `t.*` expands the default columns.
                    SelectItem::QualifiedWildcard(q) => match q.to_ascii_lowercase().as_str() {
                        "old" => vals.extend(old_rows[i].iter().cloned()),
                        "new" => vals.extend(new_rows[i].iter().cloned()),
                        _ => vals.extend(default_rows[i].iter().cloned()),
                    },
                    SelectItem::Expr { expr, .. } => {
                        vals.push(
                            self.eval_expr_with_correlated(expr, &syn_row, &ctx, cancel, None)?,
                        );
                    }
                }
            }
            out_rows.push(Row::new(vals));
        }
        Ok(QueryResult::Rows {
            columns,
            rows: out_rows,
        })
    }
}

/// v7.39 (read01 round 126) — rewrite `OLD.col` / `NEW.col` references in a
/// RETURNING expression to synthetic bare columns (`__ret_old_col` /
/// `__ret_new_col`) so they resolve against the OLD/NEW blocks of the synthetic
/// RETURNING row. Returns whether any OLD/NEW reference was found. Covers the
/// expression shapes a RETURNING item realistically uses.
fn rewrite_returning_old_new(expr: &mut Expr) -> bool {
    match expr {
        Expr::Column(c) => {
            if let Some(q) = &c.qualifier {
                let ql = q.to_ascii_lowercase();
                if ql == "old" || ql == "new" {
                    c.name = alloc::format!("__ret_{ql}_{}", c.name);
                    c.qualifier = None;
                    return true;
                }
            }
            false
        }
        Expr::Binary { lhs, rhs, .. } => {
            let a = rewrite_returning_old_new(lhs);
            rewrite_returning_old_new(rhs) || a
        }
        Expr::Unary { expr, .. } | Expr::Cast { expr, .. } | Expr::IsNull { expr, .. } => {
            rewrite_returning_old_new(expr)
        }
        Expr::FunctionCall { args, .. } => {
            let mut found = false;
            for a in args {
                found |= rewrite_returning_old_new(a);
            }
            found
        }
        Expr::Case {
            operand,
            branches,
            else_branch,
        } => {
            let mut found = false;
            if let Some(o) = operand {
                found |= rewrite_returning_old_new(o);
            }
            for (c, v) in branches {
                found |= rewrite_returning_old_new(c);
                found |= rewrite_returning_old_new(v);
            }
            if let Some(x) = else_branch {
                found |= rewrite_returning_old_new(x);
            }
            found
        }
        _ => false,
    }
}

/// v7.39 (round 130) — is `e` a bare `merge_action()` call?
fn is_merge_action_call(e: &Expr) -> bool {
    matches!(e, Expr::FunctionCall { name, args }
        if name.eq_ignore_ascii_case("merge_action") && args.is_empty())
}

/// v7.39 (round 130) — replace `merge_action()` calls anywhere in `expr` with a
/// reference to the synthetic `__merge_action` column (the per-row action
/// string). Mirrors `rewrite_returning_old_new`'s tree walk.
fn rewrite_merge_action(expr: &mut Expr) {
    match expr {
        Expr::FunctionCall { name, args }
            if name.eq_ignore_ascii_case("merge_action") && args.is_empty() =>
        {
            *expr = Expr::Column(spg_sql::ast::ColumnName {
                qualifier: None,
                name: String::from("__merge_action"),
            });
        }
        Expr::Binary { lhs, rhs, .. } => {
            rewrite_merge_action(lhs);
            rewrite_merge_action(rhs);
        }
        Expr::Unary { expr, .. } | Expr::Cast { expr, .. } | Expr::IsNull { expr, .. } => {
            rewrite_merge_action(expr)
        }
        Expr::FunctionCall { args, .. } => {
            for a in args {
                rewrite_merge_action(a);
            }
        }
        Expr::Case {
            operand,
            branches,
            else_branch,
        } => {
            if let Some(o) = operand {
                rewrite_merge_action(o);
            }
            for (c, v) in branches {
                rewrite_merge_action(c);
                rewrite_merge_action(v);
            }
            if let Some(x) = else_branch {
                rewrite_merge_action(x);
            }
        }
        _ => {}
    }
}

/// v7.39 (round 130) — expand a MERGE RETURNING list against the synthetic
/// schema `build_merge_returning` builds. `*` → bare target columns; `OLD.*` /
/// `NEW.*` → the pre/post-image blocks (aliased back to the bare column name);
/// `t.*` / `s.*` are left for `build_projection`'s prefix expansion. Scalar
/// expressions have `merge_action()` and `OLD.`/`NEW.` qualifiers rewritten onto
/// the synthetic column names.
fn expand_merge_returning_items(
    items: &[SelectItem],
    target_alias: &str,
    source_alias: &str,
    target_cols: &[ColumnSchema],
) -> Vec<SelectItem> {
    let mut out: Vec<SelectItem> = Vec::new();
    for it in items {
        match it {
            // PG expands a bare `*` in MERGE RETURNING to the source columns
            // first, then the target columns (the MERGE range-table order).
            SelectItem::Wildcard => {
                out.push(SelectItem::QualifiedWildcard(source_alias.into()));
                out.push(SelectItem::QualifiedWildcard(target_alias.into()));
            }
            SelectItem::QualifiedWildcard(q) => {
                let ql = q.to_ascii_lowercase();
                if ql == "old" || ql == "new" {
                    let prefix = if ql == "old" {
                        "__ret_old_"
                    } else {
                        "__ret_new_"
                    };
                    for c in target_cols {
                        out.push(SelectItem::Expr {
                            expr: Expr::Column(spg_sql::ast::ColumnName {
                                qualifier: None,
                                name: alloc::format!("{prefix}{}", c.name),
                            }),
                            alias: Some(c.name.clone()),
                        });
                    }
                } else {
                    out.push(SelectItem::QualifiedWildcard(q.clone()));
                }
            }
            SelectItem::Expr { expr, alias } => {
                let mut e = expr.clone();
                // PG labels an unaliased column by its bare name (dropping the
                // qualifier) and a bare `merge_action()` "merge_action". Capture
                // that from the ORIGINAL expr, before the OLD./NEW. rewrite
                // mangles the name into `__ret_old_*`.
                let out_alias = if alias.is_some() {
                    alias.clone()
                } else if is_merge_action_call(&e) {
                    Some(String::from("merge_action"))
                } else if let Expr::Column(c) = &e {
                    Some(c.name.clone())
                } else {
                    None
                };
                rewrite_merge_action(&mut e);
                rewrite_returning_old_new(&mut e);
                out.push(SelectItem::Expr {
                    expr: e,
                    alias: out_alias,
                });
            }
        }
    }
    out
}

/// Build the INSERT column permutation `tuple_pos[c] = Some(j)` (schema
/// column `c` is filled from the `j`-th tuple slot; `None` = fill with
/// NULL / DEFAULT). `None` overall means the 1-1 fast path. Validates
/// the column list once for reuse across every row.
/// v7.38 (read01) — the parser's `DEFAULT`-in-VALUES marker: a `__column_default()`
/// call with no args. `INSERT … VALUES (…, DEFAULT, …)` uses it for a slot, and
/// the INSERT executor resolves it against the target column's declared default.
fn is_column_default_marker(e: &Expr) -> bool {
    matches!(e, Expr::FunctionCall { name, args } if name == "__column_default" && args.is_empty())
}

/// v7.39 (round 433) — does this AUTO_INCREMENT column need a generated
/// value for the supplied cell?
///
/// PG's answer is "only when the cell is NULL". MySQL's — measured on
/// MariaDB 11 — also counts an explicit **zero**: `INSERT INTO t(id) VALUES
/// (0)` stores the next generated id, exactly as `VALUES (NULL)` does, and
/// LAST_INSERT_ID() reports the generated value. Legacy code and several
/// ORMs write 0 for "please assign one", so treating it literally stored a
/// row with id 0 AND left every later id one short of MySQL's.
///
/// The zero is recognised through the same widening every integer literal
/// gets, so `'0'` (MariaDB generates for it too) and a zero of any integer
/// width all qualify. A float / decimal zero does not: MySQL only applies
/// this to integer AUTO_INCREMENT columns, which is the only shape SPG
/// allows the attribute on.
///
/// MySQL's `NO_AUTO_VALUE_ON_ZERO` sql_mode turns this off and stores the 0
/// literally. SPG tracks only the escaping bit of sql_mode today, so that
/// mode is not honoured — the default (this behaviour) is what a session
/// gets. Noted rather than guessed at.
fn auto_increment_needs_value(col: &ColumnSchema, raw: &Value<'_>, mysql: bool) -> bool {
    if !col.auto_increment {
        return false;
    }
    if raw.is_null() {
        return true;
    }
    mysql
        && match raw {
            Value::SmallInt(n) => *n == 0,
            Value::Int(n) => *n == 0,
            Value::BigInt(n) => *n == 0,
            Value::Text(s) => s.trim().parse::<i64>() == Ok(0),
            _ => false,
        }
}

/// v7.39 (round 433) — the value an AUTO_INCREMENT cell was explicitly
/// given, as an integer, or None when it is not an integer at all. Same
/// widening as [`auto_increment_needs_value`] reads.
fn explicit_auto_value(raw: &Value<'_>) -> Option<i64> {
    match raw {
        Value::SmallInt(n) => Some(i64::from(*n)),
        Value::Int(n) => Some(i64::from(*n)),
        Value::BigInt(n) => Some(*n),
        Value::Text(s) => s.trim().parse::<i64>().ok(),
        _ => None,
    }
}

/// v7.39 (round 433) — the statement-local AUTO_INCREMENT cursor's starting
/// point for `col`: the table's next value, floored by any sequence restart.
/// Extracted so the generate path and the explicit-value bump path below can
/// never seed from different places.
fn auto_cursor_seed(
    table: &spg_storage::Table,
    i: usize,
    col: &ColumnSchema,
    seq_floors: &alloc::collections::BTreeMap<usize, i64>,
) -> Result<i64, EngineError> {
    let base = table.next_auto_value(i).ok_or_else(|| {
        EngineError::Unsupported(alloc::format!(
            "AUTO_INCREMENT applies to integer columns only (column `{}`)",
            col.name
        ))
    })?;
    Ok(base.max(seq_floors.get(&i).copied().unwrap_or(i64::MIN)))
}

/// The `DEFAULT`-slot marker expression (`__column_default()`), used to
/// force a column to its declared default / sequence value.
fn column_default_marker() -> Expr {
    Expr::FunctionCall {
        name: String::from("__column_default"),
        args: Vec::new(),
    }
}

fn build_tuple_pos(
    columns: &Option<Vec<String>>,
    column_meta: &[ColumnSchema],
    table_name: &str,
) -> Result<Option<Vec<Option<usize>>>, EngineError> {
    let schema_cols_len = column_meta.len();
    // Build a permutation `tuple_pos[c] = Some(j)` meaning schema
    // column `c` is filled from the `j`-th tuple slot; `None` means
    // "fill with NULL". Validated once and reused for every row.
    let tuple_pos: Option<Vec<Option<usize>>> = match columns {
        None => None, // 1-1 mapping, fast path
        Some(cols) => {
            let mut map = alloc::vec![None; schema_cols_len];
            for (j, name) in cols.iter().enumerate() {
                let idx = column_meta
                    .iter()
                    .position(|c| c.name == *name)
                    .ok_or_else(|| {
                        // v7.39 (read01 round 88) — PG names the relation in an
                        // INSERT target-column miss: `column "x" of relation "t"
                        // does not exist` (42703). The bare ColumnNotFound
                        // ("column \"x\" does not exist") dropped the relation.
                        EngineError::Unsupported(alloc::format!(
                            "column \"{}\" of relation \"{}\" does not exist",
                            name,
                            table_name
                        ))
                    })?;
                if map[idx].is_some() {
                    // v7.39 (read01 round 89) — a column named twice in the
                    // INSERT target list is PG's 42701, not an arity error:
                    // `column "a" specified more than once`.
                    return Err(EngineError::Unsupported(alloc::format!(
                        "column \"{name}\" specified more than once"
                    )));
                }
                map[idx] = Some(j);
            }
            // v7.39 (read01 round 117) — the omitted-column NOT NULL pre-check
            // used to fire here (before row values existed), so it could not
            // carry PG's `DETAIL: Failing row contains (...)`. It is now
            // subsumed by `enforce_not_null` over the fully assembled rows: an
            // omitted no-default not-null column lands as NULL and is caught
            // there (pre-write, with the row for the DETAIL), together with an
            // explicitly-supplied NULL — one path, one message shape.
            Some(map)
        }
    };
    Ok(tuple_pos)
}

/// v7.37.7(sentori Epic 3 P1)— for every column with a stored
/// `generated_stored_expr`, parse the cached Display-form source,
/// evaluate it against each candidate row, coerce to the column
/// type, and overwrite whatever the caller passed in that slot.
/// Mirrors PG's "GENERATED ALWAYS AS … STORED" semantics — the
/// user has no say in the cell's value, the expression always wins.
///
/// Called from the INSERT and UPDATE row-assembly paths after the
/// regular column-value coercion runs, so the expression sees
/// fully-typed sibling cells.
pub(crate) fn apply_generated_stored_columns(
    column_meta: &[ColumnSchema],
    rows: &mut [Vec<Value<'static>>],
) -> Result<(), EngineError> {
    use spg_engine_no_alias::ParsedExpr;
    let mut parsed: Vec<Option<ParsedExpr>> = Vec::with_capacity(column_meta.len());
    for col in column_meta {
        if let Some(src) = &col.generated_stored_expr {
            let expr = spg_sql::parser::parse_expression(src).map_err(EngineError::Parse)?;
            parsed.push(Some(ParsedExpr {
                expr,
                ty: col.ty,
                col_name: col.name.clone(),
            }));
        } else {
            parsed.push(None);
        }
    }
    if parsed.iter().all(Option::is_none) {
        return Ok(());
    }
    // Empty params slice + no sequence resolver — a stored generated
    // expression is row-local and can't reference $N or sequences.
    let no_params: [spg_storage::Value; 0] = [];
    for row_values in rows.iter_mut() {
        let row = spg_storage::Row::new(row_values.clone());
        for (idx, slot) in parsed.iter().enumerate() {
            let Some(pe) = slot else {
                continue;
            };
            let ctx = crate::eval::EvalContext {
                columns: column_meta,
                table_alias: None,
                params: &no_params,
                default_text_search_config: None,
                sequence_resolver: None,
                catalog: None,
                mysql_dialect: false,
                session_gucs: None,
                users: None,
                fn_depth: 0,
                engine: None,
                sample_rng: None,
                recursion_base: core::cell::Cell::new(0),
                render_style: crate::eval::RenderStyle::default(),
                tz_offset_fn: None,
                tz_localize_fn: None,
                tz_abbrev_fn: None,
                // This DEFAULT-expression eval path has no engine handle;
                // gen_random_bytes / uuidv7 aren't expected in a column
                // DEFAULT, so the deterministic fallbacks are acceptable here.
                salt_fn: None,
                backend_pid_fn: None,
                wal_lsn_fn: None,
                backend_signal_fn: None,
                clock: None,
                xact: None,
                assigned_xid: core::cell::Cell::new(None),
            };
            let value = crate::eval::eval_expr(&pe.expr, &row, &ctx).map_err(EngineError::Eval)?;
            let coerced = crate::coerce_value(value, pe.ty, &pe.col_name, idx)?;
            row_values[idx] = coerced;
        }
    }
    Ok(())
}

/// Module alias to keep the parser-form on hand while we walk many
/// rows; the `mod` boundary is only here to dodge an "Expr too
/// large for the borrow checker" complaint about reusing a parsed
/// expression across the per-row loop above without cloning each
/// iteration(Expr is `Clone` and we already pay one clone per
/// `apply` call to materialise the row view; the per-row inner
/// loop borrows the parsed Expr).
mod spg_engine_no_alias {
    use spg_sql::ast::Expr;
    use spg_storage::DataType;
    pub(crate) struct ParsedExpr {
        pub expr: Expr,
        pub ty: DataType,
        pub col_name: alloc::string::String,
    }
}

/// Stage 1 — parse every INSERT tuple into a coerced row of `Value`s:
/// apply the column permutation, mint AUTO_INCREMENT ids (statement-
/// scoped cursors), run DEFAULT / ENUM / SET / unsigned-range checks.
/// Reads the table immutably (`next_auto_value`); no row is written yet.
#[allow(clippy::too_many_arguments)]
/// v7.39 (round 523) — does this VALUES expression already name an
/// INSTANT, rather than a wall-clock reading that still needs a zone?
///
/// Neither the value nor the expression's SHAPE is enough on its own.
/// SPG keeps timestamptz in the same `Value::Timestamp` as a naive one,
/// so by the time there is a value the two are indistinguishable; and a
/// bare string names an instant exactly when it carries an offset
/// (`'2020-01-01 00:00:00+05'` does, `'2020-01-01 00:00:00'` does not).
/// A first cut keyed on the shape alone shifted the offset-bearing form
/// a second time.
///
/// `TIMESTAMP '…'` is a wall-clock reading whatever it carries — PG
/// drops the offset for that type — so only the string's own offset,
/// read where the type does not already answer, decides.
fn expr_names_an_instant(e: &Expr, order: crate::eval::DateOrder) -> bool {
    let text_carries_offset = |s: &str| {
        crate::eval::parse_timestamp_literal_tz_ordered_pub(s, order)
            .is_some_and(|(_, had_tz)| had_tz)
    };
    match e {
        Expr::Literal(spg_sql::ast::Literal::String(s)) => text_carries_offset(s),
        Expr::Cast { expr, target } => match target {
            // A naive type, whatever the literal says.
            spg_sql::ast::CastTarget::Timestamp | spg_sql::ast::CastTarget::Date => false,
            spg_sql::ast::CastTarget::Timestamptz => match &**expr {
                Expr::Literal(spg_sql::ast::Literal::String(s)) => text_carries_offset(s),
                _ => true,
            },
            _ => false,
        },
        _ => false,
    }
}

/// v7.39 (round 523) — PG's assignment cast into a timestamptz column.
///
/// A no-op unless the session is on a non-UTC zone, the column is
/// timestamptz, and the source was a wall-clock reading.
fn localize_assignment_to_tstz(
    v: Value<'static>,
    col: &ColumnSchema,
    src_names_an_instant: bool,
    zone: Option<&crate::eval::SessionCoercion>,
) -> Value<'static> {
    if src_names_an_instant || col.ty != DataType::Timestamptz {
        return v;
    }
    let (Some(z), Value::Timestamp(wall)) = (zone, &v) else {
        return v;
    };
    z.wall_to_utc(*wall).map_or(v, Value::Timestamp)
}

fn parse_insert_rows(
    table: &spg_storage::Table,
    // v7.39 (read01 round 55) — the catalog, so a user-named cast in an
    // INSERT's VALUES (`ROW(1,2)::pt`, `5::posint`) resolves. Without it the
    // whole INSERT failed with "unsupported cast target `::pt`".
    catalog: Option<&spg_storage::Catalog>,
    mut rows: Vec<Vec<Expr>>,
    column_meta: &[ColumnSchema],
    tuple_pos: &Option<Vec<Option<usize>>>,
    expected_tuple_len: usize,
    clock: Option<crate::ClockFn>,
    seq_floors: &alloc::collections::BTreeMap<usize, i64>,
    enum_label_lookup: &alloc::collections::BTreeMap<usize, Vec<String>>,
    set_variant_lookup: &alloc::collections::BTreeMap<usize, Vec<String>>,
    overriding: spg_sql::ast::Overriding,
    // v7.39 (round 347, M2) — the FIRST auto-generated value of this
    // statement, which is exactly what MySQL's LAST_INSERT_ID() reports
    // (measured: a three-row insert reports the first id, not the last).
    // `None` when the statement generated none, in which case MariaDB
    // leaves the session's previous value alone.
    first_auto: &mut Option<i64>,
    // v7.39 (round 367, M20) — the session dialect, so a `0x…` / `X'…'`
    // binary-string literal coerces into its target column the MySQL way
    // (big-endian number into a numeric column, bytes-as-string into a
    // text column) instead of failing the byte→column type check.
    mysql: bool,
    // v7.39 (round 434) — the statement was spelled `INSERT IGNORE`, so a
    // value the ordinary path would reject is bent to fit instead (see
    // `mysql_ignore_fit`). MySQL-only; a PG session never sets it.
    ignore: bool,
    // v7.39 (round 470) — the session's `sql_mode` has no STRICT_ flag, so
    // a value that would raise is bent to fit. NOT the same trigger as
    // `ignore`, and measured on MariaDB 11 to differ in one place: a
    // non-strict session still raises 1048 on an EXPLICIT NULL into a NOT
    // NULL column, while `INSERT IGNORE` stores 0 for it. Only an OMITTED
    // column gets the implicit default.
    non_strict: bool,
    // v7.39 (round 523) — the session zone, when it is not UTC. PG reads a
    // naive value assigned to a timestamptz column as a wall-clock reading
    // in the session zone; this path evaluates VALUES through a
    // context-free literal walker, so the zone has to arrive as an
    // argument like the other session facts above. Without it, `SET
    // TimeZone = 'Asia/Tokyo'` followed by an INSERT of a naive timestamp
    // stored an instant nine hours from the one PG stores.
    session_zone: Option<&crate::eval::SessionCoercion>,
    // v7.39 (round 525) — and the session itself, for a column DEFAULT
    // that names one.
    insert_sess: Option<&crate::eval::DmlSession>,
) -> Result<Vec<Vec<Value<'static>>>, EngineError> {
    use spg_sql::ast::Overriding;
    // v7.39 (round 523) — the session's date order, which the
    // offset-detection above parses with.
    let date_order = session_zone.map_or(crate::eval::DateOrder::Mdy, |z| z.order);
    let schema_cols_len = column_meta.len();
    let mut all_values: Vec<Vec<Value<'static>>> = Vec::with_capacity(rows.len());
    // v7.24 (round-16 collateral) — statement-scoped serial
    // cursors. next_auto_value() is a max+1 scan over COMMITTED
    // rows; multi-row `INSERT … VALUES (…),(…)` computed it per
    // tuple BEFORE any insertion, so every row drew the SAME id
    // (then sailed through, compounding with the inline-PK
    // enforcement gap). First use per column seeds from the
    // table; subsequent rows increment.
    let mut auto_cursors: alloc::collections::BTreeMap<usize, i64> =
        alloc::collections::BTreeMap::new();
    // v7.38 (read01 P6.41) — PG rejects an EXPLICIT value for a generated
    // column ("cannot insert a non-DEFAULT value into column …"), but a
    // `DEFAULT` marker for it is allowed (the column recomputes). So a
    // generated column is refused only when SOME row supplies a non-DEFAULT
    // value at its slot.
    // v7.38 (read01 sweep) — with no column list a short row omits its trailing
    // positions, so a slot is "reached" only when a row is long enough.
    for (i, col) in column_meta.iter().enumerate() {
        if col.generated_stored_expr.is_none() {
            continue;
        }
        let slot: Option<usize> = match tuple_pos {
            Some(map) => map.get(i).copied().flatten(),
            None => Some(i),
        };
        let Some(j) = slot else { continue };
        let has_explicit_value = rows
            .iter()
            .any(|row| row.get(j).is_some_and(|e| !is_column_default_marker(e)));
        if has_explicit_value {
            return Err(EngineError::Unsupported(alloc::format!(
                "cannot insert a non-DEFAULT value into column {:?} — it is a generated column",
                col.name
            )));
        }
    }
    // v7.38 (read01) — OVERRIDING USER VALUE: ignore any explicit value on an
    // identity column and generate from the sequence. We flatten a supplied
    // value to the DEFAULT marker at the affected slots so the existing
    // auto-increment path fills them. Done before the ALWAYS-reject check so a
    // USER override on an ALWAYS column generates rather than erroring.
    if overriding == Overriding::User {
        for (i, col) in column_meta.iter().enumerate() {
            if !col.auto_increment {
                continue;
            }
            let slot: Option<usize> = match tuple_pos {
                Some(map) => map.get(i).copied().flatten(),
                None => Some(i),
            };
            let Some(j) = slot else { continue };
            for row in rows.iter_mut() {
                if let Some(e) = row.get_mut(j) {
                    if !is_column_default_marker(e) {
                        *e = column_default_marker();
                    }
                }
            }
        }
    }
    // v7.38 (read01) — GENERATED ALWAYS AS IDENTITY. PG rejects an
    // explicit non-DEFAULT value for such a column unless the statement
    // carries OVERRIDING SYSTEM VALUE. A DEFAULT marker is always allowed
    // (the sequence fills it). OVERRIDING USER VALUE already flattened any
    // explicit value above, so it too passes here and generates. BY DEFAULT
    // identity columns (auto_increment && !identity_always) accept an
    // explicit value unmodified — no check here.
    if overriding != Overriding::System {
        for (i, col) in column_meta.iter().enumerate() {
            if !col.identity_always {
                continue;
            }
            let slot: Option<usize> = match tuple_pos {
                Some(map) => map.get(i).copied().flatten(),
                None => Some(i),
            };
            let Some(j) = slot else { continue };
            let has_explicit_value = rows
                .iter()
                .any(|row| row.get(j).is_some_and(|e| !is_column_default_marker(e)));
            if has_explicit_value {
                // v7.39 (read01 round 45) — PG's three-part message
                // (428C9 at the wire): the DETAIL/HINT tail splits into
                // its own PG_DIAG fields on the wire.
                let name = &col.name;
                return Err(EngineError::Unsupported(alloc::format!(
                    "cannot insert a non-DEFAULT value into column {name:?} \
                     DETAIL: Column {name:?} is an identity column defined as GENERATED ALWAYS.\n\
                     HINT:  Use OVERRIDING SYSTEM VALUE to override."
                )));
            }
        }
    }
    for tuple in rows {
        // v7.38 (read01 sweep) — with no explicit column list, PG lets a row
        // supply FEWER values than the table has columns; the trailing columns
        // take their DEFAULT (or NULL). A row is still rejected for supplying
        // MORE values than columns, and an explicit column list must match its
        // value count exactly.
        let too_many = tuple.len() > expected_tuple_len;
        let list_mismatch = tuple_pos.is_some() && tuple.len() != expected_tuple_len;
        if too_many || list_mismatch {
            // v7.39 (read01 round 88) — PG's 42601 wording, distinguishing the
            // two directions. Before, both came out as SPG's generic
            // "row arity mismatch: expected N columns, got M", which no client
            // matches. (More values → "more expressions than target columns";
            // a column list with fewer values → "more target columns than
            // expressions".)
            let msg = if tuple.len() > expected_tuple_len {
                "INSERT has more expressions than target columns"
            } else {
                "INSERT has more target columns than expressions"
            };
            return Err(EngineError::Unsupported(msg.into()));
        }
        // Fast path: no column-list permutation → tuple slot j
        // maps to schema column j. We can zip schema with tuple
        // and skip the `raw_tuple` staging allocation entirely.
        let values: Vec<Value<'static>> = if let Some(map) = &tuple_pos {
            // Permuted path: still need raw_tuple to index by `map[i]`. A
            // `DEFAULT` marker maps to None here and is resolved per-column
            // below (it has no column-free value).
            // v7.39 (round 523) — captured before the expressions are
            // consumed: only the EXPRESSION can say whether a value already
            // names an instant (see the fast path's note).
            let slot_is_tstz: Vec<bool> = tuple
                .iter()
                .map(|e| expr_names_an_instant(e, date_order))
                .collect();
            let raw_tuple: Vec<Option<Value<'static>>> = tuple
                .into_iter()
                .map(|e| {
                    if is_column_default_marker(&e) {
                        Ok(None)
                    } else {
                        literal_expr_to_value_in(e, catalog).map(Some)
                    }
                })
                .collect::<Result<_, _>>()?;
            let mut out = Vec::with_capacity(schema_cols_len);
            for (i, col) in column_meta.iter().enumerate() {
                // v7.39 (round 470) — a column the statement did not name,
                // or named as `DEFAULT`, is "omitted" for MySQL's purposes.
                let omitted = match map[i] {
                    Some(j) => raw_tuple[j].is_none(),
                    None => true,
                };
                let mut raw = match map[i] {
                    Some(j) => match &raw_tuple[j] {
                        Some(v) => v.clone(),
                        None => resolve_column_default_free(col, clock, insert_sess)?,
                    },
                    None => resolve_column_default_free(col, clock, insert_sess)?,
                };
                if auto_increment_needs_value(col, &raw, mysql) {
                    let next = match auto_cursors.get(&i) {
                        Some(n) => *n,
                        None => auto_cursor_seed(table, i, col, seq_floors)?,
                    };
                    auto_cursors.insert(i, next + 1);
                    if first_auto.is_none() {
                        *first_auto = Some(next);
                    }
                    raw = Value::BigInt(next);
                } else if mysql
                    && col.auto_increment
                    && let Some(n) = explicit_auto_value(&raw)
                {
                    // v7.39 (round 433) — an explicit value inside a
                    // multi-row INSERT raises the counter for the LATER rows
                    // of the same statement, measured on MariaDB 11:
                    // `(NULL,·),(7,·),(NULL,·)` yields 1, 7, 8. SPG derives
                    // the next value from the table's current max, which has
                    // not moved yet mid-statement, so the third row used to
                    // land on 2 — a value the statement had already handed
                    // out on a wider table, and a silent drift from MySQL's
                    // ids either way. A LOWER explicit value never pulls the
                    // counter back (`(50,·),(3,·),(NULL,·)` yields 51), so
                    // the seed still comes from the table.
                    let cursor = match auto_cursors.get(&i) {
                        Some(c) => *c,
                        None => auto_cursor_seed(table, i, col, seq_floors)?,
                    };
                    auto_cursors.insert(i, cursor.max(n.saturating_add(1)));
                }
                // v7.39 (round 263) — a COMPOSITE column relabels + coerces
                // its value through the declared type first; ROW()'s
                // placeholder names would otherwise be stored and the
                // read side, which looks fields up by name, would rebuild
                // an all-NULL record.
                let raw = crate::conversions::normalize_composite_for_column(
                    raw.into_owned(),
                    col,
                    catalog,
                )?;
                let raw = crate::conversions::mysql_bytes_for_column(raw, col.ty, mysql);
                // v7.39 (round 434) — `INSERT IGNORE` bends a value that
                // would otherwise raise, so a MySQL bulk load never stops
                // mid-file. Values the ordinary path accepts are untouched.
                // v7.39 (round 470) — a non-strict `sql_mode` bends the same
                // values `INSERT IGNORE` bends. Same conversion, different
                // trigger: one is per-statement, the other per-session.
                // v7.39 (round 470) — a strict MySQL session refuses an
                // OMITTED NOT NULL column with its own error, not the
                // not-null one: MariaDB says `Field 'n' doesn't have a
                // default value` (1364), which is what a migration tool
                // branches on, where an explicit NULL is 1048. The generic
                // not-null check downstream cannot tell the two apart —
                // only here is it known that the column was never named.
                if mysql && !non_strict && !ignore && omitted && raw.is_null() && !col.nullable {
                    return Err(EngineError::Unsupported(alloc::format!(
                        "Field '{}' doesn't have a default value",
                        col.name
                    )));
                }
                // v7.39 (round 470) — non-strict bends a SUPPLIED value that
                // would not fit, and fills an OMITTED column; it does not
                // bend an explicit NULL, which MariaDB still rejects with
                // 1048 whatever the mode.
                let raw = if ignore || (non_strict && (omitted || !raw.is_null())) {
                    crate::conversions::mysql_ignore_fit(raw, col)
                } else {
                    raw
                };
                let raw = crate::eval::session_read_temporal_text(raw, col.ty, session_zone);
                let coerced = coerce_value(raw, col.ty, &col.name, i)?;
                let coerced = localize_assignment_to_tstz(
                    coerced,
                    col,
                    map[i].is_some_and(|j| slot_is_tstz[j]),
                    session_zone,
                );
                enforce_enum_label(enum_label_lookup, i, &col.name, &coerced)?;
                let coerced = canonicalize_set_value(set_variant_lookup, i, &col.name, coerced)?;
                let coerced = crate::conversions::truncate_to_column_fsp(coerced, col);
                check_unsigned_range(&coerced, col, i)?;
                out.push(coerced);
            }
            out
        } else {
            // 1-1 mapping fast path: single Vec alloc, no raw_tuple.
            // v7.38 (read01 sweep) — a short row (fewer values than columns, no
            // column list) fills its trailing columns from their DEFAULT / NULL.
            let mut out = Vec::with_capacity(schema_cols_len);
            let tuple_len = tuple.len();
            let mut tuple_iter = tuple.into_iter();
            for (i, col) in column_meta.iter().enumerate() {
                // v7.39 (round 523) — whether the VALUES expression already
                // names an instant, which only the EXPRESSION can say: SPG
                // keeps timestamptz in the same `Value::Timestamp` as a
                // naive one, so by the time there is a value the two are
                // indistinguishable.
                let mut src_is_tstz = false;
                let mut raw = if i < tuple_len {
                    let e = tuple_iter.next().expect("i < tuple_len has a value");
                    if is_column_default_marker(&e) {
                        resolve_column_default_free(col, clock, insert_sess)?
                    } else {
                        src_is_tstz = expr_names_an_instant(&e, date_order);
                        literal_expr_to_value_in(e, catalog)?
                    }
                } else {
                    resolve_column_default_free(col, clock, insert_sess)?
                };
                if auto_increment_needs_value(col, &raw, mysql) {
                    let next = match auto_cursors.get(&i) {
                        Some(n) => *n,
                        None => auto_cursor_seed(table, i, col, seq_floors)?,
                    };
                    auto_cursors.insert(i, next + 1);
                    if first_auto.is_none() {
                        *first_auto = Some(next);
                    }
                    raw = Value::BigInt(next);
                } else if mysql
                    && col.auto_increment
                    && let Some(n) = explicit_auto_value(&raw)
                {
                    // v7.39 (round 433) — an explicit value inside a
                    // multi-row INSERT raises the counter for the LATER rows
                    // of the same statement, measured on MariaDB 11:
                    // `(NULL,·),(7,·),(NULL,·)` yields 1, 7, 8. SPG derives
                    // the next value from the table's current max, which has
                    // not moved yet mid-statement, so the third row used to
                    // land on 2 — a value the statement had already handed
                    // out on a wider table, and a silent drift from MySQL's
                    // ids either way. A LOWER explicit value never pulls the
                    // counter back (`(50,·),(3,·),(NULL,·)` yields 51), so
                    // the seed still comes from the table.
                    let cursor = match auto_cursors.get(&i) {
                        Some(c) => *c,
                        None => auto_cursor_seed(table, i, col, seq_floors)?,
                    };
                    auto_cursors.insert(i, cursor.max(n.saturating_add(1)));
                }
                let raw = crate::conversions::normalize_composite_for_column(raw, col, catalog)?;
                let raw = crate::conversions::mysql_bytes_for_column(raw, col.ty, mysql);
                // v7.39 (round 434) — `INSERT IGNORE` bends a value that
                // would otherwise raise, so a MySQL bulk load never stops
                // mid-file. Values the ordinary path accepts are untouched.
                // v7.39 (round 470) — a non-strict `sql_mode` bends the same
                // values `INSERT IGNORE` bends. Same conversion, different
                // trigger: one is per-statement, the other per-session.
                // v7.39 (round 470) — the positional path names every column,
                // so nothing here is omitted and an explicit NULL stays an
                // explicit NULL.
                let raw = if ignore || (non_strict && !raw.is_null()) {
                    crate::conversions::mysql_ignore_fit(raw, col)
                } else {
                    raw
                };
                let raw = crate::eval::session_read_temporal_text(raw, col.ty, session_zone);
                let raw = crate::eval::session_read_temporal_text(raw, col.ty, session_zone);
                let coerced = coerce_value(raw, col.ty, &col.name, i)?;
                let coerced = localize_assignment_to_tstz(coerced, col, src_is_tstz, session_zone);
                enforce_enum_label(enum_label_lookup, i, &col.name, &coerced)?;
                let coerced = canonicalize_set_value(set_variant_lookup, i, &col.name, coerced)?;
                let coerced = crate::conversions::truncate_to_column_fsp(coerced, col);
                check_unsigned_range(&coerced, col, i)?;
                out.push(coerced);
            }
            out
        };
        all_values.push(values);
    }
    Ok(all_values)
}

/// Stage 3 — insert the surviving rows under a mutable table borrow,
/// firing BEFORE / AFTER row triggers (which may rewrite or skip a row
/// and emit deferred embedded SQL), then apply the queued ON CONFLICT
/// DO UPDATE rewrites. Returns the RETURNING projection rows, the
/// deferred trigger statements, and the affected-row count.
// v7.37.15 Phase C — the `xmin` parameter below carries the writer
// version every row in this batch stamps on its `xmin`. The caller
// pre-fetches it via `Engine::writer_version_for_current_stmt` so
// all rows in the same statement share xmin (atomic commit at the
// tx boundary).
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
/// v7.39 (round 130) — one per action a MERGE actually took, captured so a
/// trailing `RETURNING` can project `merge_action()` / `OLD.*` / `NEW.*` / the
/// target+source aliases after the plan applies.
struct MergeRetRecord {
    action: &'static str,
    /// The row as `t.*` sees it: NEW for UPDATE/INSERT, the deleted row for DELETE.
    target_final: Vec<Value<'static>>,
    /// Pre-image (UPDATE/DELETE); `None` (→ NULL block) for INSERT.
    old: Option<Vec<Value<'static>>>,
    /// Post-image (UPDATE/INSERT); `None` (→ NULL block) for DELETE.
    new: Option<Vec<Value<'static>>>,
    /// The source row that drove this action (for `s.*` references).
    source: Vec<Value<'static>>,
}

/// v7.39 (round 427) — how MySQL counts a row an upsert resolved by
/// UPDATING an existing one. Measured on MariaDB 11, per row:
///
/// | shape                                    | ON DUPLICATE | REPLACE |
/// |------------------------------------------|--------------|---------|
/// | no conflict (plain insert)               | 1            | 1       |
/// | conflict, row CHANGED                    | 2            | 2       |
/// | conflict, row identical to what was there| 0            | 1       |
///
/// (`ON DUPLICATE` counts the update as delete+insert only when it really
/// changed something; `REPLACE` still charges for the insert either way.)
/// `None` is the PG dialect, where every affected row counts 1.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum MysqlUpsertCount {
    OnDuplicate,
    Replace,
}

fn insert_parsed_rows(
    table: &mut spg_storage::Table,
    xmin: u64,
    // v7.39 (round 427) — MySQL's per-row upsert accounting; None keeps
    // PG's "one per affected row".
    mysql_upsert: Option<MysqlUpsertCount>,
    // v7.37.15 (Phase C.3, step 4b) — in-place kill switch, threaded
    // from the caller (fetched before the table mut borrow). When on,
    // the ON CONFLICT DO UPDATE pass below tombstones + inserts instead
    // of in-place update_row. Default OFF → legacy update_row.
    inplace: bool,
    all_values: Vec<Vec<Value<'static>>>,
    pending_updates: Vec<(usize, Vec<Value<'static>>, Vec<Value<'static>>)>,
    before_insert_triggers: &[(
        spg_storage::FunctionDef,
        alloc::string::String,
        alloc::string::String,
    )],
    after_insert_triggers: &[(
        spg_storage::FunctionDef,
        alloc::string::String,
        alloc::string::String,
    )],
    column_meta: &[ColumnSchema],
    table_name: &str,
    trigger_session_cfg: Option<&str>,
    returning_enabled: bool,
    // v7.39 (round 757, F31-B3) — RAISE messages surface here; the
    // caller queues them into the session (this is a free fn, no self).
    raised_notices: &mut Vec<(crate::NoticeSeverity, alloc::string::String)>,
) -> Result<
    (
        Vec<Vec<Value<'static>>>,
        Vec<triggers::DeferredEmbeddedStmt>,
        usize,
        // v7.37.17 (E4 r3)(old, new) RowId pairs from the
        // ON CONFLICT DO UPDATE tombstone+insert branch, for the
        // RC rebase's pair-atomic conflict handling.
        Vec<(
            spg_storage::row_header::RowId,
            spg_storage::row_header::RowId,
        )>,
        // v7.39 (r129) — per-returning-row OLD image, aligned 1:1 with the
        // returning rows: a NULL block for a plain insert, the pre-update row
        // for an ON CONFLICT DO UPDATE. Empty when RETURNING is off.
        Vec<Vec<Value<'static>>>,
    ),
    EngineError,
> {
    let arity = column_meta.len();
    let mut affected = 0usize;
    let mut oc_pairs: Vec<(
        spg_storage::row_header::RowId,
        spg_storage::row_header::RowId,
    )> = Vec::new();
    // v7.9.4 — keep RETURNING projection rows separate per
    // INSERT and per UPDATE branch so DO UPDATE pushes the new
    // post-update state, not the incoming-only values.
    let mut returning_rows: Vec<Vec<Value<'static>>> = Vec::new();
    // v7.39 (r129) — OLD image aligned with returning_rows (see return type).
    let mut old_images: Vec<Vec<Value<'static>>> = Vec::new();
    // v7.12.7 — collect embedded SQL emitted by any trigger
    // fire across the row loop; engine drains the queue after
    // the table mut borrow drops.
    let mut deferred_embedded: Vec<triggers::DeferredEmbeddedStmt> = Vec::new();
    'rowloop: for values in all_values {
        let mut row = Row::new(values);
        // v7.12.4 — BEFORE INSERT row-level triggers. Each
        // trigger may rewrite NEW cells (e.g. populate
        // `search_vector := to_tsvector(...)`) and may return
        // NULL to skip the row entirely.
        for (fd, when, tgname) in before_insert_triggers {
            // v7.39 (round 138) — WHEN filter over the incoming NEW row.
            if !triggers::trigger_when_holds(when, Some(&row), None, column_meta)? {
                continue;
            }
            let raise_sink = triggers::NoticeSink::default();
            let fired = triggers::fire_row_trigger(
                fd,
                Some(row.clone()),
                None,
                table_name,
                column_meta,
                &[],
                trigger_session_cfg,
                false,
                &triggers::TgMeta {
                    op: "INSERT",
                    name: tgname,
                    level: "ROW",
                },
                Some(&raise_sink),
            );
            raised_notices.extend(raise_sink.into_inner());
            let (outcome, deferred) = fired
                .map_err(|e| EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}"))))?;
            deferred_embedded.extend(deferred);
            match outcome {
                triggers::TriggerOutcome::Row(r) => row = r,
                triggers::TriggerOutcome::Skip => continue 'rowloop,
            }
        }
        // v7.39 (read01 round 82) — a stored generated column is computed AFTER
        // the BEFORE trigger runs, not before: PG's order is BEFORE trigger →
        // generated columns → write. The pre-loop `apply_generated_stored_columns`
        // saw only the incoming NEW, so `w GENERATED AS (v*2)` kept the value for
        // the ORIGINAL v when a BEFORE trigger changed `NEW.v`. Recompute here
        // over the trigger's output. (No-op when there are no generated columns.)
        if !before_insert_triggers.is_empty() {
            let mut one = [core::mem::take(&mut row.values)];
            apply_generated_stored_columns(column_meta, &mut one)?;
            row.values = core::mem::take(&mut one[0]);
        }
        if returning_enabled {
            returning_rows.push(row.values.clone());
            // A plain insert has no prior row — OLD is all-NULL.
            old_images.push(alloc::vec![Value::Null; arity]);
        }
        // v7.12.4 — clone for the AFTER trigger view; insert
        // moves the row into the table.
        let inserted = row.clone();
        // v7.37.15 Phase C — every row in this insert statement
        // shares the caller-supplied xmin so concurrent readers
        // see all the rows commit together (autocommit) or stay
        // hidden until the enclosing tx COMMIT (explicit tx).
        table.insert_with_xmin(row, xmin)?;
        affected += 1;
        // v7.12.4 — AFTER INSERT row-level triggers fire post-
        // write. Return value is ignored (PG semantics); we
        // surface any error from the body up to the caller.
        for (fd, when, tgname) in after_insert_triggers {
            // v7.39 (round 138) — WHEN filter over the inserted NEW row.
            if !triggers::trigger_when_holds(when, Some(&inserted), None, column_meta)? {
                continue;
            }
            let raise_sink = triggers::NoticeSink::default();
            let fired = triggers::fire_row_trigger(
                fd,
                Some(inserted.clone()),
                None,
                table_name,
                column_meta,
                &[],
                trigger_session_cfg,
                true,
                &triggers::TgMeta {
                    op: "INSERT",
                    name: tgname,
                    level: "ROW",
                },
                Some(&raise_sink),
            );
            raised_notices.extend(raise_sink.into_inner());
            let (_outcome, deferred) = fired
                .map_err(|e| EngineError::Storage(StorageError::Corrupt(alloc::format!("{e}"))))?;
            deferred_embedded.extend(deferred);
        }
    }
    // v7.9.9 — apply ON CONFLICT DO UPDATE rewrites collected
    // in the conflict-resolution pass. update_row handles
    // index maintenance + body re-encoding.
    for (pos, new_row, old_row) in pending_updates {
        // v7.39 (round 427) — MySQL charges an upsert-resolved-by-update as
        // delete+insert (2) only when the row really changed; an ON
        // DUPLICATE that rewrote identical values counts 0, while a REPLACE
        // still charges 1 for its insert. Compute before `old_row` moves.
        let row_changed = new_row != old_row;
        if returning_enabled {
            returning_rows.push(new_row.clone());
            // DO UPDATE: OLD is the pre-update conflicting row.
            old_images.push(old_row);
        }
        if inplace {
            // MVCC: tombstone the conflicting old version (xmax = xmin)
            // + append the DO UPDATE result as a new version (xmin).
            let old_rid = table.rowids().get(pos).copied();
            let _ = table.mark_row_deleted(pos, xmin);
            table
                .insert_with_xmin(Row::new(new_row), xmin)
                .map_err(EngineError::Storage)?;
            if let (Some(o), Some(n)) = (
                old_rid,
                table
                    .rowids()
                    .get(table.rowids().len().wrapping_sub(1))
                    .copied(),
            ) {
                oc_pairs.push((o, n));
            }
        } else {
            table.update_row(pos, new_row)?;
        }
        affected += match (mysql_upsert, row_changed) {
            (Some(_), true) => 2,
            (Some(MysqlUpsertCount::OnDuplicate), false) => 0,
            (Some(MysqlUpsertCount::Replace), false) => 1,
            (None, _) => 1,
        };
    }
    Ok((
        returning_rows,
        deferred_embedded,
        affected,
        oc_pairs,
        old_images,
    ))
}

/// v7.39 (SQLSTATE fidelity) — PG's full 23502 message needs the
/// relation name the storage error can't carry; DML entry points
/// wrap their results through this.
fn enrich_not_null(e: EngineError, table: &str) -> EngineError {
    match e {
        EngineError::Storage(spg_storage::StorageError::NullInNotNull { column }) => {
            EngineError::Unsupported(alloc::format!(
                "null value in column \"{column}\" of relation \"{table}\" \
                 violates not-null constraint"
            ))
        }
        other => other,
    }
}

/// v7.39 (round 150) — relation reads of a CTE body, for the
/// no-RETURNING-reference guard. Modifying bodies reuse the deep
/// SELECT walker via a synthetic SELECT carrying their expression
/// slots (the acl privilege pass uses the same trick), so subquery
/// references are seen too.
pub(crate) fn collect_cte_body_reads(
    body: &spg_sql::ast::CteBody,
    into: &mut alloc::collections::BTreeSet<String>,
) {
    match body {
        spg_sql::ast::CteBody::Select(s) => crate::acl::collect_read_tables(s, into),
        spg_sql::ast::CteBody::Insert(b) => collect_insert_reads(b, into),
        spg_sql::ast::CteBody::Update(b) => collect_update_reads(b, into),
        spg_sql::ast::CteBody::Delete(b) => collect_delete_reads(b, into),
        spg_sql::ast::CteBody::Merge(b) => collect_merge_reads(b, into),
    }
}

fn push_items(sub: &mut spg_sql::ast::SelectStatement, items: &[SelectItem]) {
    for item in items {
        sub.items.push(item.clone());
    }
}

fn push_expr(sub: &mut spg_sql::ast::SelectStatement, e: &spg_sql::ast::Expr) {
    sub.items.push(SelectItem::Expr {
        expr: e.clone(),
        alias: None,
    });
}

pub(crate) fn collect_insert_reads(
    i: &spg_sql::ast::InsertStatement,
    into: &mut alloc::collections::BTreeSet<String>,
) {
    let mut sub = spg_sql::ast::SelectStatement::default();
    for row in &i.rows {
        for e in row {
            push_expr(&mut sub, e);
        }
    }
    if let Some(oc) = &i.on_conflict
        && let spg_sql::ast::OnConflictAction::Update {
            assignments,
            where_,
        } = &oc.action
    {
        for (_, e) in assignments {
            push_expr(&mut sub, e);
        }
        if let Some(w) = where_ {
            push_expr(&mut sub, w);
        }
    }
    if let Some(r) = &i.returning {
        push_items(&mut sub, r);
    }
    crate::acl::collect_read_tables(&sub, into);
    if let Some(src) = &i.select_source {
        crate::acl::collect_read_tables(src, into);
    }
}

pub(crate) fn collect_update_reads(
    u: &spg_sql::ast::UpdateStatement,
    into: &mut alloc::collections::BTreeSet<String>,
) {
    let mut sub = spg_sql::ast::SelectStatement::default();
    for (_, e) in &u.assignments {
        push_expr(&mut sub, e);
    }
    sub.where_ = u.where_.clone();
    if let Some(r) = &u.returning {
        push_items(&mut sub, r);
    }
    crate::acl::collect_read_tables(&sub, into);
}

pub(crate) fn collect_delete_reads(
    d: &spg_sql::ast::DeleteStatement,
    into: &mut alloc::collections::BTreeSet<String>,
) {
    let mut sub = spg_sql::ast::SelectStatement::default();
    sub.where_ = d.where_.clone();
    if let Some(r) = &d.returning {
        push_items(&mut sub, r);
    }
    crate::acl::collect_read_tables(&sub, into);
}

pub(crate) fn collect_merge_reads(
    m: &spg_sql::ast::MergeStatement,
    into: &mut alloc::collections::BTreeSet<String>,
) {
    if let Some(src) = &m.source_select {
        crate::acl::collect_read_tables(src, into);
    } else {
        into.insert(m.source.clone());
    }
    let mut sub = spg_sql::ast::SelectStatement::default();
    push_expr(&mut sub, &m.on);
    for clause in &m.clauses {
        if let Some(c) = &clause.condition {
            push_expr(&mut sub, c);
        }
        match &clause.action {
            spg_sql::ast::MergeAction::Insert { values, .. } => {
                for v in values {
                    push_expr(&mut sub, v);
                }
            }
            spg_sql::ast::MergeAction::Update { assignments } => {
                for (_, e) in assignments {
                    push_expr(&mut sub, e);
                }
            }
            spg_sql::ast::MergeAction::Delete | spg_sql::ast::MergeAction::DoNothing => {}
        }
    }
    if let Some(r) = &m.returning {
        push_items(&mut sub, r);
    }
    crate::acl::collect_read_tables(&sub, into);
}

// ---------------------------------------------------------------------------
// v7.39 (round 157) — write-path CTE-shadows-table support. The write-path
// CTE machinery installs temps on the LIVE catalog, so a CTE that shadows a
// real table gets a renamed temp (`__cte_shadow_<name>`) and every READ
// reference to the name is rewritten to it, honouring PG's WITH scoping:
//   * the shadowing CTE's own (non-recursive) body still sees the real
//     table (probe P2/P8); a RECURSIVE self-reference is the CTE (P6);
//   * later sibling bodies and the outer statement see the CTE (P1-P5);
//   * DML TARGETS are never renamed — they resolve to real relations only
//     (probe P6/P7: `INSERT INTO t … FROM t` writes the table, reads the CTE);
//   * a nested WITH that REDEFINES the name owns it for its whole subtree.
// After rewriting, the caller re-collects the statement's read set: any
// surviving reference to the old name means the rewriter missed a spot —
// fail with the historic honest error instead of silently reading the table.

/// The historic honest error for an unsupported shadow shape.
fn cte_shadow_err(name: &str) -> EngineError {
    EngineError::Unsupported(alloc::format!(
        "CTE name {name:?} shadows an existing table; rename the CTE"
    ))
}

/// v7.39 (round 157) — post-rewrite verification: any surviving read of a
/// renamed (shadowed) name means the rewriter missed a reference — fail
/// honestly instead of silently reading the real table.
fn shadow_rename_leak_check(
    renames: &[(String, String)],
    reads: &alloc::collections::BTreeSet<String>,
) -> Result<(), EngineError> {
    for (old, _) in renames {
        if reads.iter().any(|t| t.eq_ignore_ascii_case(old)) {
            return Err(cte_shadow_err(old));
        }
    }
    Ok(())
}

fn rename_rel_in_table_ref(t: &mut spg_sql::ast::TableRef, old: &str, new: &str) {
    if let Some(sub) = &mut t.lateral_subquery {
        rename_rel_in_select(sub, old, new);
        return;
    }
    let synthetic = t.unnest_expr.is_some()
        || t.generate_series_args.is_some()
        || t.jsonb_each_text_arg.is_some()
        || t.table_fn_call.is_some();
    if synthetic {
        // The synthetic sources aren't tables, but their argument
        // expressions may carry subqueries that read the name.
        if let Some(e) = &mut t.unnest_expr {
            rename_rel_in_expr(e, old, new);
        }
        if let Some(args) = &mut t.generate_series_args {
            for a in args {
                rename_rel_in_expr(a, old, new);
            }
        }
        if let Some((_, e)) = &mut t.jsonb_each_text_arg {
            rename_rel_in_expr(e, old, new);
        }
        if let Some(b) = &mut t.table_fn_call {
            for a in &mut b.1 {
                rename_rel_in_expr(a, old, new);
            }
        }
        return;
    }
    if t.name.eq_ignore_ascii_case(old) {
        // Keep the user-visible alias: an unaliased table ref is referred
        // to by its name in column qualifiers, which must keep resolving.
        if t.alias.is_none() {
            t.alias = Some(t.name.clone());
        }
        t.name = new.into();
    }
}

fn rename_rel_in_expr(e: &mut spg_sql::ast::Expr, old: &str, new: &str) {
    use spg_sql::ast::Expr;
    crate::expr_analysis::rewrite_nodes_mut(e, &mut |n| match n {
        Expr::ScalarSubquery(s) => {
            rename_rel_in_select(s, old, new);
            true
        }
        Expr::Exists { subquery, .. } => {
            rename_rel_in_select(subquery, old, new);
            true
        }
        Expr::InSubquery { expr, subquery, .. } => {
            rename_rel_in_expr(expr, old, new);
            rename_rel_in_select(subquery, old, new);
            true
        }
        Expr::RowInSubquery { row, subquery, .. } | Expr::RowCmpSubquery { row, subquery, .. } => {
            for x in row {
                rename_rel_in_expr(x, old, new);
            }
            rename_rel_in_select(subquery, old, new);
            true
        }
        _ => false,
    });
}

pub(crate) fn rename_rel_in_select(s: &mut spg_sql::ast::SelectStatement, old: &str, new: &str) {
    // A nested WITH redefining the name owns it for this whole subtree.
    if s.ctes.iter().any(|c| c.name.eq_ignore_ascii_case(old)) {
        return;
    }
    for cte in &mut s.ctes {
        rename_rel_in_cte_body(&mut cte.body, old, new);
    }
    if let Some(from) = &mut s.from {
        rename_rel_in_table_ref(&mut from.primary, old, new);
        for j in &mut from.joins {
            rename_rel_in_table_ref(&mut j.table, old, new);
            if let Some(on) = &mut j.on {
                rename_rel_in_expr(on, old, new);
            }
        }
    }
    for item in &mut s.items {
        if let SelectItem::Expr { expr, .. } = item {
            rename_rel_in_expr(expr, old, new);
        }
    }
    if let Some(w) = &mut s.where_ {
        rename_rel_in_expr(w, old, new);
    }
    if let Some(h) = &mut s.having {
        rename_rel_in_expr(h, old, new);
    }
    if let Some(gs) = &mut s.group_by {
        for g in gs {
            rename_rel_in_expr(g, old, new);
        }
    }
    for o in &mut s.order_by {
        rename_rel_in_expr(&mut o.expr, old, new);
    }
    for (_, peer) in &mut s.unions {
        rename_rel_in_select(peer, old, new);
    }
}

fn rename_rel_in_returning(items: &mut Option<Vec<SelectItem>>, old: &str, new: &str) {
    if let Some(items) = items {
        for it in items.iter_mut() {
            if let SelectItem::Expr { expr, .. } = it {
                rename_rel_in_expr(expr, old, new);
            }
        }
    }
}

pub(crate) fn rename_rel_in_insert(i: &mut InsertStatement, old: &str, new: &str) {
    // `i.table` is the DML target — never renamed.
    for row in &mut i.rows {
        for e in row {
            rename_rel_in_expr(e, old, new);
        }
    }
    if let Some(src) = &mut i.select_source {
        rename_rel_in_select(src, old, new);
    }
    if let Some(oc) = &mut i.on_conflict
        && let spg_sql::ast::OnConflictAction::Update {
            assignments,
            where_,
        } = &mut oc.action
    {
        for (_, e) in assignments {
            rename_rel_in_expr(e, old, new);
        }
        if let Some(w) = where_ {
            rename_rel_in_expr(w, old, new);
        }
    }
    rename_rel_in_returning(&mut i.returning, old, new);
}

pub(crate) fn rename_rel_in_update(u: &mut spg_sql::ast::UpdateStatement, old: &str, new: &str) {
    for (_, e) in &mut u.assignments {
        rename_rel_in_expr(e, old, new);
    }
    if let Some(w) = &mut u.where_ {
        rename_rel_in_expr(w, old, new);
    }
    rename_rel_in_returning(&mut u.returning, old, new);
}

pub(crate) fn rename_rel_in_delete(d: &mut spg_sql::ast::DeleteStatement, old: &str, new: &str) {
    if let Some(w) = &mut d.where_ {
        rename_rel_in_expr(w, old, new);
    }
    rename_rel_in_returning(&mut d.returning, old, new);
}

pub(crate) fn rename_rel_in_merge(m: &mut spg_sql::ast::MergeStatement, old: &str, new: &str) {
    // `m.target` is the DML target — never renamed. The SOURCE is a read.
    if let Some(src) = &mut m.source_select {
        rename_rel_in_select(src, old, new);
    } else if m.source.eq_ignore_ascii_case(old) {
        if m.source_alias.is_none() {
            m.source_alias = Some(m.source.clone());
        }
        m.source = new.into();
    }
    rename_rel_in_expr(&mut m.on, old, new);
    for cl in &mut m.clauses {
        if let Some(c) = &mut cl.condition {
            rename_rel_in_expr(c, old, new);
        }
        match &mut cl.action {
            spg_sql::ast::MergeAction::Insert { values, .. } => {
                for v in values {
                    rename_rel_in_expr(v, old, new);
                }
            }
            spg_sql::ast::MergeAction::Update { assignments } => {
                for (_, e) in assignments {
                    rename_rel_in_expr(e, old, new);
                }
            }
            spg_sql::ast::MergeAction::Delete | spg_sql::ast::MergeAction::DoNothing => {}
        }
    }
    rename_rel_in_returning(&mut m.returning, old, new);
}

pub(crate) fn rename_rel_in_cte_body(body: &mut spg_sql::ast::CteBody, old: &str, new: &str) {
    match body {
        spg_sql::ast::CteBody::Select(s) => rename_rel_in_select(s, old, new),
        spg_sql::ast::CteBody::Insert(b) => rename_rel_in_insert(b, old, new),
        spg_sql::ast::CteBody::Update(b) => rename_rel_in_update(b, old, new),
        spg_sql::ast::CteBody::Delete(b) => rename_rel_in_delete(b, old, new),
        spg_sql::ast::CteBody::Merge(b) => rename_rel_in_merge(b, old, new),
    }
}

/// v7.39 (round 533) — rewrite an unqualified leaf that names a source
/// column into the correlated subquery the parser builds for a
/// qualified one.
fn resolve_unqualified_source_leaf(
    e: &mut Expr,
    in_target: &dyn Fn(&str) -> bool,
    in_source: &dyn Fn(&str) -> bool,
    from: &spg_sql::ast::FromClause,
    sub_where: Option<&Expr>,
    changed: &mut bool,
) -> Result<(), EngineError> {
    match e {
        Expr::Column(c) if c.qualifier.is_none() => {
            let target = in_target(&c.name);
            let source = in_source(&c.name);
            if target && source {
                return Err(EngineError::Unsupported(alloc::format!(
                    "column reference \"{}\" is ambiguous",
                    c.name
                )));
            }
            if source {
                *e = Expr::ScalarSubquery(alloc::boxed::Box::new(spg_sql::ast::SelectStatement {
                    locking: None,
                    ctes: Vec::new(),
                    distinct: false,
                    distinct_on: Vec::new(),
                    items: alloc::vec![spg_sql::ast::SelectItem::Expr {
                        expr: e.clone(),
                        alias: None,
                    }],
                    from: Some(from.clone()),
                    where_: sub_where.cloned(),
                    group_by: None,
                    group_by_all: false,
                    having: None,
                    unions: Vec::new(),
                    order_by: Vec::new(),
                    limit: None,
                    offset: None,
                    limit_with_ties: false,
                    window_check_exprs: Vec::new(),
                }));
                *changed = true;
            }
            Ok(())
        }
        Expr::Binary { lhs, rhs, .. } => {
            resolve_unqualified_source_leaf(lhs, in_target, in_source, from, sub_where, changed)?;
            resolve_unqualified_source_leaf(rhs, in_target, in_source, from, sub_where, changed)
        }
        Expr::Unary { expr, .. } | Expr::Cast { expr, .. } => {
            resolve_unqualified_source_leaf(expr, in_target, in_source, from, sub_where, changed)
        }
        Expr::FunctionCall { args, .. } => {
            for a in args {
                resolve_unqualified_source_leaf(a, in_target, in_source, from, sub_where, changed)?;
            }
            Ok(())
        }
        // Anything else keeps the scope it had; a subquery brings its
        // own FROM and is not this walk's business.
        _ => Ok(()),
    }
}