aboutsummaryrefslogtreecommitdiff
path: root/doc/TODO.detail/limit
blob: 83714b49ec23b6ca85a447fdb4d754e8cfa2aeb2 (plain)
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
From owner-pgsql-hackers@hub.org Tue Oct 13 15:05:53 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id PAA09435
	for <maillist@candle.pha.pa.us>; Tue, 13 Oct 1998 15:05:50 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id OAA11700;
	Tue, 13 Oct 1998 14:43:31 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Tue, 13 Oct 1998 14:41:03 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id OAA11395
	for pgsql-hackers-outgoing; Tue, 13 Oct 1998 14:41:00 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from terry1.acun.com (terry@terry1.acun.com [206.27.86.12])
	by hub.org (8.8.8/8.8.8) with ESMTP id OAA11372
	for <hackers@postgreSQL.org>; Tue, 13 Oct 1998 14:40:54 -0400 (EDT)
	(envelope-from terry@terrym.com)
Received: from localhost (terry@localhost)
	by terry1.acun.com (8.8.5/8.8.5) with SMTP id OAA09491
	for <hackers@postgreSQL.org>; Tue, 13 Oct 1998 14:53:22 -0400
Date: Tue, 13 Oct 1998 14:53:22 -0400 (EDT)
From: Terry Mackintosh <terry@terrym.com>
X-Sender: terry@terry1.acun.com
Reply-To: Terry Mackintosh <terry@terrym.com>
To: PostgreSQL-development <hackers@postgreSQL.org>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <Pine.GSO.3.96.SK.981013211058.17758A-100000@ra>
Message-ID: <Pine.LNX.3.95.981013141634.9255C-100000@terry1.acun.com>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

Hi, my 2 cents...

I agree completely, LIMIT would be VERY usefull in web based apps, which
is all I run.  It does not matter to me if it is not part of a formal
standard.  The idea is so common that it is a defacto standard.

I would not expect it for this release, but could it get put on the TODO
list for next time?  I am even willing to work at an apprentise level on
this with a more expeireanced person that knows this stuff.

A note on implimentation:
I *used to* :) work with VFP on NT's :(
And the way VFP did LIMIT, it would only return the number of rows asked
for, BUT it still did the WHOLE search!
So on a larger table, which we had (property tax database for the county),
if some one put in too vague a query, it would try to collect ALL of the
rows as the initial result set, then give you the first x rows of that.

This did save on pushing mass amounts of data out to the browser, but it
would have been even better if it could have simply aborted the select
after having found x rows.

Also, it did not have the concept of an offset, so one could not select
100 rows, starting 200 rows in, which would be REALLY usefull for "paging"
through data.  I do not know if mySQL or any other has such a concept
either, but it would be nice.

So a properly implemented "LIMIT" could:
1.  Save pushing mass amounts of data across the web, that no one wants
any way.
2.  Stop vague queries from bogging down the server.
(On very larg tables this could be critical!)
3.  Enable "Paging" of data. (easyer then now (app. level))
4.  Would be a very nice feather in PostgreSQL's cap that could make it
even more attractive to those looking at all sorts of databases out there.

Have a great day.

On Tue, 13 Oct 1998, Oleg Bartunov wrote:

> Hi,
> 
> I took a look at mysql and was very impressed with   possibility
> to limit number of rows returned from select. This is very useful
> feature for Web applications when user need to browse results of
> selection page by page. In my application I have to do full
> select every time user press button [Next] and show requested page
> using perl. This works more or less ok for several thousands rows but
> totally unusable for large selections. But now I'm about to work
> with big database and I don't know how I'll stay with postgres :-)
> It'll just doesn't work if customer will wait several minutes just browse
> next page. Mysql lacks some useful features postgres has 
> (subselects, transaction ..) but for most Web applications I need
> just select :-) I dont' know how LIMIT is implemented in Mysql and
> I know it's not in SQL92 standart, but this makes Mysql very popular.
> 
> Is it difficult to implement this feature in postgres ?
> 
> 	Regards,
> 
> 		Oleg
> 
> 
> _____________________________________________________________
> Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
> Sternberg Astronomical Institute, Moscow University (Russia)
> Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
> phone: +007(095)939-16-83, +007(095)939-23-83
> 
> 

Terry Mackintosh <terry@terrym.com>          http://www.terrym.com
sysadmin/owner  Please! No MIME encoded or HTML mail, unless needed.

Proudly powered by R H Linux 4.2, Apache 1.3, PHP 3, PostgreSQL 6.3
-------------------------------------------------------------------
Success Is A Choice ... book by Rick Patino, get it, read it!




From owner-pgsql-hackers@hub.org Tue Oct 13 18:12:41 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id SAA12156
	for <maillist@candle.pha.pa.us>; Tue, 13 Oct 1998 18:12:39 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id RAA04181;
	Tue, 13 Oct 1998 17:56:17 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Tue, 13 Oct 1998 17:54:49 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id RAA03869
	for pgsql-hackers-outgoing; Tue, 13 Oct 1998 17:54:47 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from remapcorp.com (root@remapcorp.com [206.196.37.193])
	by hub.org (8.8.8/8.8.8) with ESMTP id RAA03838
	for <hackers@postgreSQL.org>; Tue, 13 Oct 1998 17:54:36 -0400 (EDT)
	(envelope-from jeff@remapcorp.com)
Received: from go-to-jail (gotojail.remapcorp.com [206.196.37.197])
	by remapcorp.com (8.8.7/8.8.7) with SMTP id QAA25337;
	Tue, 13 Oct 1998 16:55:35 -0500 (CDT)
	(envelope-from jeff@remapcorp.com)
Message-ID: <006701bdf6f4$60ed75f0$c525c4ce@go-to-jail.remapcorp.com>
From: "Jeff Hoffmann" <jeff@remapcorp.com>
To: "Marc G. Fournier" <scrappy@hub.org>, "Eric Lee Green" <eric@linux-hw.com>
Cc: "PostgreSQL-development" <hackers@postgreSQL.org>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
Date: Tue, 13 Oct 1998 16:56:48 -0500
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3115.0
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

>On Tue, 13 Oct 1998, Eric Lee Green wrote:
>
>> On Tue, 13 Oct 1998, Jeff Hoffmann wrote:
>> > >I agree completely, LIMIT would be VERY usefull in web based apps,
which
>> > >is all I run.  It does not matter to me if it is not part of a formal
>> > >standard.  The idea is so common that it is a defacto standard.
>> >
>> > i'm not familiar with mysql and using "LIMIT" but wouldn't this same
effect
>> > be achieved by declaring a cursor and fetching however many records in
the
>> > cursor?  it's a very noticeable improvement when you only want the
first 20
>> > out of 500 in a 200k record database, at least.
>>
>> The problem with declaring a cursor vs. the "LIMIT" clause is that the
>> "LIMIT" clause, if used properly by the database engine (along with the
>> database engine using indexes in "ORDER BY" clauses) allows the database
>> engine to short-circuit the tail end of the query. That is, if you have
25
>> names and the last one ends with BEAVIS, the database engine doesn't have
>> to go through the BUTTHEADS and KENNYs and etc.
>>
>> Theoretically a cursor is superior to the "LIMIT" clause because you're
>> eventually going to want the B's and K's and etc. anyhow -- but only in a
>> stateful enviornment. In the stateless web environment, a cursor is
>> useless because the connection can close at any time even when you're
>> using "persistent" connections (and of course when the connection closes
>> the cursor closes).
>
>Ookay, I'm sorry, butyou lost me here.  I haven't gotten into using
>CURSORs/FETCHs yet, since I haven't need it...but can you give an example
>of what you would want to do using a LIMIT?  I may be missing something,
>but wha is the different between using LIMIT to get X records, and
>definiing a cursor to FETCH X records?
>
>Practical example of *at least* the LIMIT side would be good, so that we
>can at least see a physical example of what LIMIT can do that
>CURSORs/FETCH can't...
>


fetch with cursors should work properly (i.e., you can short circuit it by
just ending your transaction)  my understanding on how this works is exactly
how you explained LIMIT to work.  here's some empirical proof from one of my
sample databases:

the sample table i'm using has 156k records (names of people)
i'm using a PP180 with 128MB RAM and some old slow SCSI drives.

public_mn=> select count(*) from public_ramsey;
 count
------
156566
(1 row)

i did the following query:
public_mn=> select * from public_ramsey where ownerlname ~ 'SMITH';

which returned 711 matches and took about 12 seconds.

i did the same thing with a cursor:

public_mn=> begin;
BEGIN
public_mn=> declare test cursor for select * from public_ramsey where
ownerlname  ~ 'SMITH';
SELECT

the select was instantaneous.

public_mn=> fetch 20 in test;

returns 20 records almost instantaneously.  each additional 20 took less
than a second, as well.

if this isn't what you're talking about, i don't understand what you're
saying.

jeff



From eric@ireland.linux-hw.com Tue Oct 13 18:52:42 1998
Received: from ireland.linux-hw.com (IDENT:eric@ireland.linux-hw.com [199.72.95.215])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id SAA12388
	for <maillist@candle.pha.pa.us>; Tue, 13 Oct 1998 18:52:40 -0400 (EDT)
Received: from localhost (eric@localhost)
	by ireland.linux-hw.com (8.8.7/8.8.7) with SMTP id SAA31316;
	Tue, 13 Oct 1998 18:55:22 -0400
Date: Tue, 13 Oct 1998 18:55:22 -0400 (EDT)
From: Eric Lee Green <eric@linux-hw.com>
To: Bruce Momjian <maillist@candle.pha.pa.us>
cc: jeff@remapcorp.com, hackers@postgreSQL.org
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <199810132116.RAA11249@candle.pha.pa.us>
Message-ID: <Pine.LNX.3.96.981013184022.31202B-100000@ireland.linux-hw.com>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Status: RO

On Tue, 13 Oct 1998, Bruce Momjian wrote:
> > Theoretically a cursor is superior to the "LIMIT" clause because you're
> > eventually going to want the B's and K's and etc. anyhow -- but only in a
> > stateful enviornment. In the stateless web environment, a cursor is
> > useless because the connection can close at any time even when you're
> > using "persistent" connections (and of course when the connection closes
> What we could do is _if_ there is only one table(no joins), and an index
> exists that matches the ORDER BY, we could use the index to
> short-circuit the query.

This is exactly what MySQL does in this situation, except that it can use
the ORDER BY to do the short circuiting even if there is a join involved
if all of the elements of the ORDER BY belong to one table. Obviously if
I'm doing an "ORDER BY table1.foo table2.bar" that isn't going to work!
But "select table1.fsname,table1.lname,table2.receivables where 
table2.receivables > 0 and table1.custnum=table2.custnum order by
(table1.lname,table1.fsname) limit 50" can be short-circuited by fiddling
with the join order -- table1.fsname table1.lname have to be the first two
things in the join order. 

Whether this is feasible in PostgreSQL I have no earthly idea. This would
seem to conflict with the join optimizer.

> happier?  If there is an ORDER BY and no index, or a join, I can't
> figure out how we would short-circuit the query.

If there is an ORDER BY and no index you can't short-circuit the query.
MySQL doesn't either. Under certain circumstances (such as above) you can
short-circuit a join, but it's unclear whether it'd be easy to add such
a capability to PostgreSQL given the current structure of the query
optimizer. (And I certainly am not in a position to tackle it, at the
moment MySQL is sufficing for my project despite the fact that it is 
quite limited compared to PostgreSQL, I need to get my project finished
first).  

--
Eric Lee Green         eric@linux-hw.com     http://www.linux-hw.com/~eric
"To call Microsoft an innovator is like calling the Pope Jewish ..." 
            -- James Love (Consumer Project on Technology)


From owner-pgsql-hackers@hub.org Wed Oct 14 09:01:01 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id JAA24574
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 09:01:00 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id HAA17762 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 07:47:57 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id HAA09214;
	Wed, 14 Oct 1998 07:04:59 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 07:00:44 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id HAA09116
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 07:00:40 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id HAA09102
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 07:00:27 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id NAA05037; Wed, 14 Oct 1998 13:02:40 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma004737; Wed, 14 Oct 98 13:02:09 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id MAA20155;
	Wed, 14 Oct 1998 12:59:23 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id NAA20772;
	Wed, 14 Oct 1998 13:01:35 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<eric@linux-hw.com>> 
	id m0zTMGL-000B5AC; Wed, 14 Oct 98 10:26 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for eric@linux-hw.com 
	id m0zTOnx-000EBRC; Wed, 14 Oct 98 13:09 MET DST
Message-Id: <m0zTOnx-000EBRC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: eric@linux-hw.com (Eric Lee Green)
Date: Wed, 14 Oct 1998 13:09:21 +0200 (MET DST)
Cc: jeff@remapcorp.com, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <Pine.LNX.3.96.981013161955.30555A-100000@ireland.linux-hw.com> from "Eric Lee Green" at Oct 13, 98 04:24:20 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

Eric Lee Green wrote:
>
> On Tue, 13 Oct 1998, Jeff Hoffmann wrote:
> > >I agree completely, LIMIT would be VERY usefull in web based apps, which
> > >is all I run.  It does not matter to me if it is not part of a formal
> > >standard.  The idea is so common that it is a defacto standard.
> >
> > i'm not familiar with mysql and using "LIMIT" but wouldn't this same effect
> > be achieved by declaring a cursor and fetching however many records in the
> > cursor?  it's a very noticeable improvement when you only want the first 20
> > out of 500 in a 200k record database, at least.
>
> The problem with declaring a cursor vs. the "LIMIT" clause is that the
> "LIMIT" clause, if used properly by the database engine (along with the
> database engine using indexes in "ORDER BY" clauses) allows the database
> engine to short-circuit the tail end of the query. That is, if you have 25
> names and the last one ends with BEAVIS, the database engine doesn't have
> to go through the BUTTHEADS and KENNYs and etc.
>
> Theoretically a cursor is superior to the "LIMIT" clause because you're
> eventually going to want the B's and K's and etc. anyhow -- but only in a
> stateful enviornment. In the stateless web environment, a cursor is
> useless because the connection can close at any time even when you're
> using "persistent" connections (and of course when the connection closes
> the cursor closes).

    I'm  missing something. Well it's right that in the stateless
    web environment a cursor has to be declared  and  closed  for
    any  single  CGI  call.  But even if you have a LIMIT clause,
    your CGI must know with which key to start.

    So your query must look like

        SELECT ... WHERE key > 'last processed key' ORDER BY key;

    And your key must be unique (or at least contain no duplicate
    entries)  or you might miss some rows between the pages (have
    100 Brown's in the table and last processed key was  a  Brown
    while using LIMIT).

    In  postgres you could actually do the following (but read on
    below - it's not optimized correct)

        BEGIN;
        DECLARE c CURSOR FOR SELECT ... WHERE key > 'last' ORDER BY key;
        FETCH 20 IN c;
        (process the 20 rows in CGI)
        CLOSE c;
        COMMIT;

    Having LIMIT looks more elegant and has less overhead in CGI-
    backend   communication.   But  the  cursor  version  is  SQL
    standard and portable.

>
> I wanted very badly to use PostgreSQL for a web project I'm working on,
> but it just wouldn't do the job :-(.

    I've done some tests and what I found out might be a  bug  in
    PostgreSQL's  query  optimizer.  Having a table with 25k rows
    where key is a text field with a unique  index.  Now  I  used
    EXPLAIN for some queries

        SELECT * FROM tab;

        results in a seqscan - expected.

        SELECT * FROM tab ORDER BY key;

        results in a sort->seqscan - I would have
        expected an indexscan!

        SELECT * FROM tab WHERE key > 'G';

        results in an indexscan - expected.

        SELECT * FROM tab WHERE key > 'G' ORDER BY key;

        results in a sort->indexscan - hmmm.

    These  results  stay  the same even if I blow up the table by
    duplicating all rows (now with a non-unique  index)  to  100k
    rows and have them presorted in the table.

    Needless to say that everything is vacuum'd for statistics.

    The  last  one  is  the  query  we  would  need  in  the  web
    environment used over a cursor as in the example  above.  But
    due  to  the  sort,  the backend selects until the end of the
    table, sorts them and then returns only  the  first  20  rows
    (out of sorts result).

    This  is very painful if the qualification (key > ...) points
    to the beginning of the key list.

    Looking at planner.c I can see, that if there is a sortClause
    in  the  parsetree,  the planner creates a sort node and does
    absolutely not check if there is an index that could be  used
    to  do  it.  In  the  examples  above, the sort is absolutely
    needless because the  index  scan  will  already  return  the
    tuples in the right order :-).

    Somewhere  deep  in my brain I found a statement that sorting
    sorted  data  isn't  only  unnecessary  (except   the   order
    changes),  it  is  slow too compared against sorting randomly
    ordered data.

    Can we fix this before 6.4 release, will it be a past 6.4  or
    am I doing something wrong here? I think it isn't a fix (it's
    a planner enhancement) so it should  really  be  a  past  6.4
    item.

    For  now, the only possibility is to omit the ORDER BY in the
    query and hope the planner will always generate an index scan
    (because  of  the  qualification  'key  >  ...').  Doing so I
    selected multiple times 20 rows (with the last key qual  like
    a  CGI  would do) in separate transactions.  Using cursor and
    fetch speeds up the access by a factor of 1000!   But  it  is
    unsafe  and thus NOT RECOMMENDED! It's only a test if cursors
    can do the LIMIT job - and they could if the planner would do
    a better job.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From owner-pgsql-hackers@hub.org Wed Oct 14 11:02:04 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id LAA25519
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 11:02:02 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id JAA24583 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 09:46:21 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id IAA17022;
	Wed, 14 Oct 1998 08:59:20 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 08:54:40 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id IAA16687
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 08:54:34 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from ra.sai.msu.su (ra.sai.msu.su [158.250.29.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id IAA16656
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 08:54:00 -0400 (EDT)
	(envelope-from oleg@sai.msu.su)
Received: from ra (ra [158.250.29.2])
	by ra.sai.msu.su (8.9.1/8.9.1) with SMTP id PAA11714;
	Wed, 14 Oct 1998 15:53:53 +0300 (MSK)
Date: Wed, 14 Oct 1998 16:53:53 +0400 (MSD)
From: Oleg Bartunov <oleg@sai.msu.su>
X-Sender: megera@ra
Reply-To: Oleg Bartunov <oleg@sai.msu.su>
To: hackers@postgreSQL.org
cc: t-ishii@sra.co.jp
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <m0zTOnx-000EBRC@orion.SAPserv.Hamburg.dsh.de>
Message-ID: <Pine.GSO.3.96.SK.981014163020.10948B-100000@ra>
Organization: Sternberg Astronomical Institute (Moscow University)
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

On Wed, 14 Oct 1998, Jan Wieck wrote:

> Date: Wed, 14 Oct 1998 13:09:21 +0200 (MET DST)
> From: Jan Wieck <jwieck@debis.com>
> To: Eric Lee Green <eric@linux-hw.com>
> Cc: jeff@remapcorp.com, hackers@postgreSQL.org
> Subject: Re: [HACKERS] What about LIMIT in SELECT ?
> 
> Eric Lee Green wrote:
> >
> > On Tue, 13 Oct 1998, Jeff Hoffmann wrote:
> > > >I agree completely, LIMIT would be VERY usefull in web based apps, which
> > > >is all I run.  It does not matter to me if it is not part of a formal
> > > >standard.  The idea is so common that it is a defacto standard.
> > >
> > > i'm not familiar with mysql and using "LIMIT" but wouldn't this same effect
> > > be achieved by declaring a cursor and fetching however many records in the
> > > cursor?  it's a very noticeable improvement when you only want the first 20
> > > out of 500 in a 200k record database, at least.
> >
> > The problem with declaring a cursor vs. the "LIMIT" clause is that the
> > "LIMIT" clause, if used properly by the database engine (along with the
> > database engine using indexes in "ORDER BY" clauses) allows the database
> > engine to short-circuit the tail end of the query. That is, if you have 25
> > names and the last one ends with BEAVIS, the database engine doesn't have
> > to go through the BUTTHEADS and KENNYs and etc.
> >
> > Theoretically a cursor is superior to the "LIMIT" clause because you're
> > eventually going to want the B's and K's and etc. anyhow -- but only in a
> > stateful enviornment. In the stateless web environment, a cursor is
> > useless because the connection can close at any time even when you're
> > using "persistent" connections (and of course when the connection closes
> > the cursor closes).
> 
>     I'm  missing something. Well it's right that in the stateless
>     web environment a cursor has to be declared  and  closed  for
>     any  single  CGI  call.  But even if you have a LIMIT clause,
>     your CGI must know with which key to start.
> 
      This is not a problem for CGI-script to know which key to start.
      Without LIMIT every CGI call backend will do *FULL* selection
      and cursor helps just in fetching a definite number of rows,
      in principle I can do this with CGI-script. Also, cursor
      returns data back in  ASCII  format (man l declare) and this requires
      additional job for backend to convert data from intrinsic (binary)
      format. Right implementation of LIMIT offset,number_of_rows could be
      a great win and make postgres superior free database engine for
      Web applications. Many colleagues of mine used mysql instead of
      postgres just because of lacking LIMIT. Tatsuo posted a patch
      for set query_limit to 'num', I just tested it and seems it
      works fine. Now, we need only possibility to specify offset,
      say 
         set query_limit to 'offset,num'
      ( Tatsuo, How difficult to do this ?)
      and LIMIT problem will ne gone.

      I'm wonder how many useful patches could be hidden from people  :-),
      
	Regards,

		Oleg

PS.

	Tatsuo, do you have patch for 6.3.2 ?
        I can't wait for 6.4 :-)
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83





From owner-pgsql-hackers@hub.org Wed Oct 14 11:02:00 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id LAA25510
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 11:01:59 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id KAA28854 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 10:40:56 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id KAA21542;
	Wed, 14 Oct 1998 10:03:45 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 09:59:10 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id JAA21121
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 09:59:08 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from golem.jpl.nasa.gov (root@hectic-2.jpl.nasa.gov [128.149.68.204])
	by hub.org (8.8.8/8.8.8) with ESMTP id JAA21106
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 09:59:02 -0400 (EDT)
	(envelope-from lockhart@alumni.caltech.edu)
Received: from alumni.caltech.edu (localhost [127.0.0.1])
	by golem.jpl.nasa.gov (8.8.5/8.8.5) with ESMTP id NAA19587;
	Wed, 14 Oct 1998 13:59:56 GMT
Message-ID: <3624AE5C.752E4E7F@alumni.caltech.edu>
Date: Wed, 14 Oct 1998 13:59:56 +0000
From: "Thomas G. Lockhart" <lockhart@alumni.caltech.edu>
Organization: Caltech/JPL
X-Mailer: Mozilla 4.07 [en] (X11; I; Linux 2.0.30 i686)
MIME-Version: 1.0
To: Jan Wieck <jwieck@debis.com>
CC: Eric Lee Green <eric@linux-hw.com>, jeff@remapcorp.com,
        hackers@postgreSQL.org
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
References: <m0zTOnx-000EBRC@orion.SAPserv.Hamburg.dsh.de>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

>     I've done some tests and what I found out might be a  bug  in
>     PostgreSQL's  query  optimizer.
>         SELECT * FROM tab ORDER BY key;
>         results in a sort->seqscan - I would have
>         expected an indexscan!

Given that a table _could_ be completely unsorted on disk, it is
probably reasonable to suck the data in for a possible in-memory sort
rather than skipping around the disk to pick up individual tuples via
the index. Don't know if vacuum has a statistic on "orderness"...

>         SELECT * FROM tab WHERE key > 'G' ORDER BY key;
>         results in a sort->indexscan - hmmm.
>     The  last  one  is  the  query  we  would  need  in  the  web
>     environment used over a cursor as in the example  above.  But
>     due  to  the  sort,  the backend selects until the end of the
>     table, sorts them and then returns only  the  first  20  rows
>     (out of sorts result).

So you are saying that for this last case the sort was unnecessary? Does
the backend traverse the index in the correct order to guarantee that
the tuples are coming out already sorted? Does a hash index give the
same plan (I would expect a sort->seqscan for a hash index)?

                      - Tom


From owner-pgsql-hackers@hub.org Wed Oct 14 11:01:52 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id LAA25504
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 11:01:51 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id KAA00198 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 10:57:15 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id KAA22877;
	Wed, 14 Oct 1998 10:19:47 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 10:15:44 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id KAA22675
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 10:15:41 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id KAA22657
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 10:15:32 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id QAA20563; Wed, 14 Oct 1998 16:18:02 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma020404; Wed, 14 Oct 98 16:17:25 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id QAA05077;
	Wed, 14 Oct 1998 16:14:48 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id QAA22248;
	Wed, 14 Oct 1998 16:17:06 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<hackers@postgreSQL.org>> 
	id m0zTPJb-000B5AC; Wed, 14 Oct 98 13:42 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for hackers@postgreSQL.org 
	id m0zTRrE-000EBRC; Wed, 14 Oct 98 16:24 MET DST
Message-Id: <m0zTRrE-000EBRC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: oleg@sai.msu.su
Date: Wed, 14 Oct 1998 16:24:56 +0200 (MET DST)
Cc: hackers@postgreSQL.org, t-ishii@sra.co.jp
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <Pine.GSO.3.96.SK.981014163020.10948B-100000@ra> from "Oleg Bartunov" at Oct 14, 98 04:53:53 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

Oleg Bartunov wrote:
>       This is not a problem for CGI-script to know which key to start.

    Never  meant that would be a problem. A FORM variable will of
    course do this.

>       Without LIMIT every CGI call backend will do *FULL* selection
>       and cursor helps just in fetching a definite number of rows,
>       in principle I can do this with CGI-script. Also, cursor
>       returns data back in  ASCII  format (man l declare) and this requires
>       additional job for backend to convert data from intrinsic (binary)
>       format. Right implementation of LIMIT offset,number_of_rows could be
>       a great win and make postgres superior free database engine for
>       Web applications. Many colleagues of mine used mysql instead of

    That's the point I was missing. The offset!

>       postgres just because of lacking LIMIT. Tatsuo posted a patch
>       for set query_limit to 'num', I just tested it and seems it
>       works fine. Now, we need only possibility to specify offset,
>       say
>          set query_limit to 'offset,num'
>       ( Tatsuo, How difficult to do this ?)
>       and LIMIT problem will ne gone.

    Think you haven't read my posting completely. Even  with  the
    executor  limit,  the  complete scan into the sort is done by
    the backend.  You need to specify ORDER BY to  get  the  same
    list  again  (without  the  offset  doesn't  make sense). But
    currently, ORDER BY forces a sort node into the query plan.

    What the executor limit  tells  is  how  many  rows  will  be
    returned  from  the sorted data. Not what goes into the sort.
    Filling the sort and sorting the data consumes the most  time
    of the queries execution.

    I  haven't  looked  at  Tatsuo's  patch  very well. But if it
    limits the amount of data going into the sort (on ORDER  BY),
    it  will  break it! The requested ordering could be different
    from what the choosen index might return. The used  index  is
    choosen by the planner upon the qualifications given, not the
    ordering wanted.

    So if you select WHERE b = 1 ORDER BY a, then it will use  an
    index on attribute b to match the qualification. The complete
    result of that index scan goes into the sort to  get  ordered
    by  a. If now the executor limit stops sort filling after the
    limit is exceeded, only the same tuples will go into the sort
    every  time.  But  they have nothing to do with the requested
    order by a.

    What LIMIT first needs is  a  planner  enhancement.  In  file
    backend/optimizer/plan/planner.c  line 284 it must be checked
    if the actual plan is an indexscan, if the indexed attributes
    are  all  the same as those in the given sort clause and that
    the requested sort order (operator) is that  what  the  index
    will  return.   If  that  all matches, it can ignore the sort
    clause and return the index scan itself.

    Second enhancement must be the handling of  the  offset.   In
    the  executor,  the  index scan must skip offset index tuples
    before returning the first. But  NOT  if  the  plan  isn't  a
    1-table-index-scan.  In that case the result tuples (from the
    topmost unique/join/whatever node) have to be skipped.

    With these enhancements,  the  index  tuples  to  be  skipped
    (offset)  will still be scanned, but not the data tuples they
    point to. Index scanning might be somewhat faster.

    This all will only speedup simple 1-table-queries,  no  joins
    or  if  the requested order isn't that what the index exactly
    returns.

    Anyway, I'll take a look if I can change the planner to  omit
    the  sort  if  the tests described above are true. I think it
    would be good anyway.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From owner-pgsql-hackers@hub.org Wed Oct 14 11:01:36 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id LAA25489
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 11:01:34 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id KAA24286;
	Wed, 14 Oct 1998 10:30:14 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 10:26:34 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id KAA23732
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 10:26:27 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id KAA23717
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 10:26:13 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id QAA25644; Wed, 14 Oct 1998 16:28:01 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma025301; Wed, 14 Oct 98 16:27:43 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id QAA05943;
	Wed, 14 Oct 1998 16:24:42 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id QAA22339;
	Wed, 14 Oct 1998 16:26:57 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<lockhart@alumni.caltech.edu>> 
	id m0zTPT8-000B5AC; Wed, 14 Oct 98 13:51 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for lockhart@alumni.caltech.edu 
	id m0zTS0m-000EBRC; Wed, 14 Oct 98 16:34 MET DST
Message-Id: <m0zTS0m-000EBRC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: lockhart@alumni.caltech.edu (Thomas G. Lockhart)
Date: Wed, 14 Oct 1998 16:34:47 +0200 (MET DST)
Cc: jwieck@debis.com, eric@linux-hw.com, jeff@remapcorp.com,
        hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <3624AE5C.752E4E7F@alumni.caltech.edu> from "Thomas G. Lockhart" at Oct 14, 98 01:59:56 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

>
> >         SELECT * FROM tab WHERE key > 'G' ORDER BY key;
> >         results in a sort->indexscan - hmmm.
> >     The  last  one  is  the  query  we  would  need  in  the  web
> >     environment used over a cursor as in the example  above.  But
> >     due  to  the  sort,  the backend selects until the end of the
> >     table, sorts them and then returns only  the  first  20  rows
> >     (out of sorts result).
>
> So you are saying that for this last case the sort was unnecessary? Does
> the backend traverse the index in the correct order to guarantee that
> the tuples are coming out already sorted? Does a hash index give the
> same plan (I would expect a sort->seqscan for a hash index)?

    Good  point!  As  far as I can see, the planner chooses index
    usage only depending on the WHERE clause.  A  hash  index  is
    only  usable  when  the  given  qualification  uses  = on the
    indexed attribute(s).

    If the sortClause exactly matches the indexed  attributes  of
    the  ONE used btree index and all operators request ascending
    order I think the index  scan  already  returns  the  correct
    order. Who know's definitely?

    Addition  to  my  last  posting: ... and if the index scan is
    using a btree index ...


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From owner-pgsql-hackers@hub.org Wed Oct 14 13:55:58 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA29300
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 13:55:56 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id NAA14245 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 13:49:19 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id NAA13110;
	Wed, 14 Oct 1998 13:25:55 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 13:22:14 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id NAA12694
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 13:22:13 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from candle.pha.pa.us (maillist@s5-03.ppp.op.net [209.152.195.67])
	by hub.org (8.8.8/8.8.8) with ESMTP id NAA12677
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 13:22:05 -0400 (EDT)
	(envelope-from maillist@candle.pha.pa.us)
Received: (from maillist@localhost)
	by candle.pha.pa.us (8.9.0/8.9.0) id NAA28746;
	Wed, 14 Oct 1998 13:21:15 -0400 (EDT)
From: Bruce Momjian <maillist@candle.pha.pa.us>
Message-Id: <199810141721.NAA28746@candle.pha.pa.us>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <3624AE5C.752E4E7F@alumni.caltech.edu> from "Thomas G. Lockhart" at "Oct 14, 1998  1:59:56 pm"
To: lockhart@alumni.caltech.edu (Thomas G. Lockhart)
Date: Wed, 14 Oct 1998 13:21:15 -0400 (EDT)
Cc: jwieck@debis.com, eric@linux-hw.com, jeff@remapcorp.com,
        hackers@postgreSQL.org
X-Mailer: ELM [version 2.4ME+ PL47 (25)]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

> >     I've done some tests and what I found out might be a  bug  in
> >     PostgreSQL's  query  optimizer.
> >         SELECT * FROM tab ORDER BY key;
> >         results in a sort->seqscan - I would have
> >         expected an indexscan!
> 
> Given that a table _could_ be completely unsorted on disk, it is
> probably reasonable to suck the data in for a possible in-memory sort
> rather than skipping around the disk to pick up individual tuples via
> the index. Don't know if vacuum has a statistic on "orderness"...

Thomas is correct on this.  Vadim has run some tests, and with our
optimized psort() code, the in-memory sort is often faster than using
the index to get the tuple, because you are jumping all over the drive. 
I don't remember, but obviously there is a break-even point where
getting X rows using the index on a table of Y rows is faster , but
getting X+1 rows on a table of Y rows is faster getting all the rows
sequentailly, and doing the sort.

You would have to pick only certain queries(no joins, index matches
ORDER BY), take the number of rows requested, and the number of rows
selected, and figure out if it is faster to use the index, or a
sequential scan and do the ORDER BY yourself.


Add to this the OFFSET capability.  I am not sure how you are going to
get into the index and start at the n-th entry, unless perhaps you just
sequential scan the index.

In fact, many queries just get column already indexed, and we could just
pull the data right out of the index.

I have added this to the TODO list:

	* Pull requested data directly from indexes, bypassing heap data        

I think this has to be post-6.4 work, but I think we need to work in
this direction.  I am holding off any cnfify fixes for post-6.4, but a
6.4.1 performance release certainly is possible.


But, you are correct that certain cases where in index is already being
used on a query, you could just skip the sort IF you used the index to
get the rows from the base table.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026


From owner-pgsql-hackers@hub.org Wed Oct 14 13:55:59 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA29303
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 13:55:58 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id NAA13463 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 13:39:05 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id NAA11655;
	Wed, 14 Oct 1998 13:13:32 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 13:09:41 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id NAA11013
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 13:09:39 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from terry1.acun.com (terry@terry1.acun.com [206.27.86.12])
	by hub.org (8.8.8/8.8.8) with ESMTP id NAA10997
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 13:09:30 -0400 (EDT)
	(envelope-from terry@terrym.com)
Received: from localhost (terry@localhost)
	by terry1.acun.com (8.8.5/8.8.5) with SMTP id NAA14478;
	Wed, 14 Oct 1998 13:21:51 -0400
Date: Wed, 14 Oct 1998 13:21:51 -0400 (EDT)
From: Terry Mackintosh <terry@terrym.com>
X-Sender: terry@terry1.acun.com
To: Jeff Hoffmann <jeff@remapcorp.com>
cc: PostgreSQL-development <hackers@postgreSQL.org>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <005101bdf6de$f9345150$c525c4ce@go-to-jail.remapcorp.com>
Message-ID: <Pine.LNX.3.95.981014130857.14397B-100000@terry1.acun.com>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

On Tue, 13 Oct 1998, Jeff Hoffmann wrote:

> >Hi, my 2 cents...
> >
> >I agree completely, LIMIT would be VERY usefull in web based apps, which
> >is all I run.  It does not matter to me if it is not part of a formal
> >standard.  The idea is so common that it is a defacto standard.
> 
> i'm not familiar with mysql and using "LIMIT" but wouldn't this same effect
> be achieved by declaring a cursor and fetching however many records in the
> cursor?  it's a very noticeable improvement when you only want the first 20
> out of 500 in a 200k record database, at least.

Yes, while this is an improvement, it still has to do the entire query,
would be nice if the query could be terminated after a designated number
of rows where found, thus freeing system resources that are other wise
consumed.  
I have seen web users run ridculous querys, like search for the
letter 'a', and it happens to be a substring search, now the box go'es ape
shit for 5 or 10 min.s while it basically gets the whole db as the search
result.  All this befor you can do a 'FETCH', as I understand FETCH, I
will need to read up on it.

Note that I do not have any databases that larg on my box, I was thinking
back to my VFP/NT experiances.

Have a great day
Terry Mackintosh <terry@terrym.com>          http://www.terrym.com
sysadmin/owner  Please! No MIME encoded or HTML mail, unless needed.

Proudly powered by R H Linux 4.2, Apache 1.3, PHP 3, PostgreSQL 6.3
-------------------------------------------------------------------
Success Is A Choice ... book by Rick Patino, get it, read it!



From owner-pgsql-hackers@hub.org Wed Oct 14 13:59:05 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA29345
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 13:58:59 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id NAA14021;
	Wed, 14 Oct 1998 13:32:51 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 13:29:09 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id NAA13364
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 13:29:07 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from terry1.acun.com (terry@terry1.acun.com [206.27.86.12])
	by hub.org (8.8.8/8.8.8) with ESMTP id NAA13328
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 13:28:56 -0400 (EDT)
	(envelope-from terry@terrym.com)
Received: from localhost (terry@localhost)
	by terry1.acun.com (8.8.5/8.8.5) with SMTP id NAA14606
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 13:41:25 -0400
Date: Wed, 14 Oct 1998 13:41:24 -0400 (EDT)
From: Terry Mackintosh <terry@terrym.com>
X-Sender: terry@terry1.acun.com
To: PostgreSQL-development <hackers@postgreSQL.org>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <199810132116.RAA11249@candle.pha.pa.us>
Message-ID: <Pine.LNX.3.95.981014133641.14397D-100000@terry1.acun.com>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

On Tue, 13 Oct 1998, Bruce Momjian wrote:

> What we could do is _if_ there is only one table(no joins), and an index
> exists that matches the ORDER BY, we could use the index to
> short-circuit the query.
> 
> I have added this item to the TODO list:
> 
> * Allow LIMIT ability on single-table queries that have no ORDER BY or
>         a matching index
>        
> This looks do-able, and a real win.  Would this make web applications
> happier?  If there is an ORDER BY and no index, or a join, I can't
> figure out how we would short-circuit the query.
> 
Yes, this would do for most of my apps.
It may just be my lack of sophistication, but I find that most web apps
are very simple in nature/table layout, and thus queries are often on only
a single table.

Thanks
Terry Mackintosh <terry@terrym.com>          http://www.terrym.com
sysadmin/owner  Please! No MIME encoded or HTML mail, unless needed.

Proudly powered by R H Linux 4.2, Apache 1.3, PHP 3, PostgreSQL 6.3
-------------------------------------------------------------------
Success Is A Choice ... book by Rick Patino, get it, read it!



From wieck@sapserv.debis.de Wed Oct 14 13:55:53 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA29290
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 13:55:51 -0400 (EDT)
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id NAA14370 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 13:51:19 -0400 (EDT)
Received: by dsh.de; id TAA03418; Wed, 14 Oct 1998 19:50:18 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma003369; Wed, 14 Oct 98 19:49:51 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id TAA16746;
	Wed, 14 Oct 1998 19:47:14 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id TAA23570;
	Wed, 14 Oct 1998 19:49:32 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<lockhart@alumni.caltech.edu>> 
	id m0zTSdF-000B5AC; Wed, 14 Oct 98 17:14 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for lockhart@alumni.caltech.edu 
	id m0zTVAt-000EBRC; Wed, 14 Oct 98 19:57 MET DST
Message-Id: <m0zTVAt-000EBRC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Wed, 14 Oct 1998 19:57:27 +0200 (MET DST)
Cc: lockhart@alumni.caltech.edu, jwieck@debis.com, eric@linux-hw.com,
        jeff@remapcorp.com, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810141721.NAA28746@candle.pha.pa.us> from "Bruce Momjian" at Oct 14, 98 01:21:15 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Status: RO

> But, you are correct that certain cases where in index is already being
> used on a query, you could just skip the sort IF you used the index to
> get the rows from the base table.

    Especially in the case where

        SELECT ... WHERE key > 'val' ORDER BY key;

    creates  a Sort->IndexScan plan. The index scan already jumps
    around on the disc to collect the sorts input  and  the  sort
    finally returns exactly the same output (if the used index is
    only on key).

    And this is the case for  large  tables.  The  planner  first
    decides  to  use  an  index  scan due to the WHERE clause and
    later it notices the ORDER BY clause and creates a sort  over
    the scan.

    I'm  actually  hacking  around on it to see what happens if I
    suppress the sort node in some cases.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #



From owner-pgsql-hackers@hub.org Wed Oct 14 16:31:07 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id QAA01119
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 16:31:05 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id PAA22534 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 15:29:50 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id PAA26335;
	Wed, 14 Oct 1998 15:05:26 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 15:02:13 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id PAA26013
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 15:02:11 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from candle.pha.pa.us (root@s5-03.ppp.op.net [209.152.195.67])
	by hub.org (8.8.8/8.8.8) with ESMTP id PAA25996
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 15:01:58 -0400 (EDT)
	(envelope-from maillist@candle.pha.pa.us)
Received: (from maillist@localhost)
	by candle.pha.pa.us (8.9.0/8.9.0) id OAA29639;
	Wed, 14 Oct 1998 14:27:05 -0400 (EDT)
From: Bruce Momjian <maillist@candle.pha.pa.us>
Message-Id: <199810141827.OAA29639@candle.pha.pa.us>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <199810141721.NAA28746@candle.pha.pa.us> from Bruce Momjian at "Oct 14, 1998  1:21:15 pm"
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Wed, 14 Oct 1998 14:27:05 -0400 (EDT)
Cc: lockhart@alumni.caltech.edu, jwieck@debis.com, eric@linux-hw.com,
        jeff@remapcorp.com, hackers@postgreSQL.org
X-Mailer: ELM [version 2.4ME+ PL47 (25)]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

> Thomas is correct on this.  Vadim has run some tests, and with our
> optimized psort() code, the in-memory sort is often faster than using
> the index to get the tuple, because you are jumping all over the drive. 
> I don't remember, but obviously there is a break-even point where
> getting X rows using the index on a table of Y rows is faster , but
> getting X+1 rows on a table of Y rows is faster getting all the rows
> sequentailly, and doing the sort.
> 
> You would have to pick only certain queries(no joins, index matches
> ORDER BY), take the number of rows requested, and the number of rows
> selected, and figure out if it is faster to use the index, or a
> sequential scan and do the ORDER BY yourself.
> 
> Add to this the OFFSET capability.  I am not sure how you are going to
> get into the index and start at the n-th entry, unless perhaps you just
> sequential scan the index.
> 
> In fact, many queries just get column already indexed, and we could just
> pull the data right out of the index.
> 
> I have added this to the TODO list:
> 
> 	* Pull requested data directly from indexes, bypassing heap data        
> 
> I think this has to be post-6.4 work, but I think we need to work in
> this direction.  I am holding off any cnfify fixes for post-6.4, but a
> 6.4.1 performance release certainly is possible.
> 
> 
> But, you are correct that certain cases where in index is already being
> used on a query, you could just skip the sort IF you used the index to
> get the rows from the base table.

I have had more time to think about this.  Basically, for pre-sorted
data, our psort code is very fast, because it does not need to sort
anything.  It just moves the rows in and out of the sort memory.  Yes,
it could be removed in some cases, and probably should be, but it is not
going to produce great speedups.

The more general case I will describe below.

First, let's look at a normal query:

	SELECT *
	FROM tab
	ORDER BY col1

This is not going to use an index, and probably should not because it is
faster for large tables to sort them in memory, rather than moving all
over the disk.  For small tables, if the entire table fits in the buffer
cache, it may be faster to use the index, but on a small table the sort
doesn't take very long either, and the buffer cache effectiveness is
affected by other backends using it, so it may be better not to count on
it for a speedup.

However, if you only want the first 10 rows, that is a different story. 
We pull all the rows into the backend, sort them, then return 10 rows. 
The query, if we could do it, should be written as:

	SELECT *
	FROM tab
	WHERE col1 < some_unknown_value
	ORDER BY col1

In this case, the optimizer looks at the column statistics, and properly
uses an index to pull only a small subset of the table.  This is the
type of behavior people want for queries returning only a few values.

But, unfortunately, we don't know that mystery value.

Now, everyone agrees we need an index matching the ORDER BY to make this
query quick, but we don't know that mystery value, so currently we
execute the whole query, and do a fetch.

What I am now thinking is that maybe we need a way to walk around that
index.  Someone months ago asked how to do that, and we told him he
couldn't, because this not a C-ISAM/dbm type database.  However, if we
could somehow pass into the query the index location we want to start
at, and how many rows we need, that would solve our problem, and perhaps
even allow joined queries to work, assuming the table in the ORDER BY is
in an outer join loop.

	SELECT *
	FROM tab
	WHERE col1 < some_unknown_value
	ORDER BY col1
	USING INDEX tab_idx(452) COUNT 100

where 452 is an 452th index entry, and COUNT is the number of index rows
you want to process.  The query may return more or less than 100 rows if
there is a join and it joins to zero or more than one row in the joined
table, but this seems like perhaps a good way to go at it.  We need to
do it this way because if a single index row returns 4 result rows, and
only two of the four rows fit in the number of rows returnd as set by the
user, it is hard to re-start the query at the proper point, because you
would have to process the index rows a second time, and return just part
of the result, and that is hard.

If the index changes, or rows are added, the results are going to be
unreliable, but that is probably going to be true of any state-less
implementation we can devise.

I think this may be fairly easy to implement.  We could sequential scan
the index to get to the 452th row.  That is going to be quick.  We can
pass the 452 into the btree index code, so only a certain range of index
tuples are returned, and the system believes it has processed the entire
query, while we know it hasn't.  Doesn't really work with hash, so we
will not allow it for those indexes. 

To make it really easy, we could implement it as a 'SET' command, so we
don't actually have it as part of the query, and have to pass it around
through all the modules.  You would do the proper 'SET' before running
the query.  Optimizer would look at 'SET' value to force index use.

	SET INDEX TO tab_idx START 452 COUNT 100

or

	SET INDEX TO tab_idx FROM 452 COUNT 451

There would have to be some way to signal that the end of the index had
been reached, because returning zero rows is not enough of a guarantee
in a joined SELECT.

Comments?

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026


From owner-pgsql-hackers@hub.org Wed Oct 14 17:31:23 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id RAA01591
	for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 17:31:21 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id RAA02744 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 17:26:53 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id RAA05601;
	Wed, 14 Oct 1998 17:03:21 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 16:59:54 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id QAA04964
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 16:59:52 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id QAA04943
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 16:59:28 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id WAA28383; Wed, 14 Oct 1998 22:57:42 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma028354; Wed, 14 Oct 98 22:57:28 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id WAA20547;
	Wed, 14 Oct 1998 22:54:51 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id WAA24383;
	Wed, 14 Oct 1998 22:57:09 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<lockhart@alumni.caltech.edu>> 
	id m0zTVYr-000B5AC; Wed, 14 Oct 98 20:22 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for lockhart@alumni.caltech.edu 
	id m0zTY6V-000EBRC; Wed, 14 Oct 98 23:05 MET DST
Message-Id: <m0zTY6V-000EBRC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Wed, 14 Oct 1998 23:05:07 +0200 (MET DST)
Cc: maillist@candle.pha.pa.us, lockhart@alumni.caltech.edu, jwieck@debis.com,
        eric@linux-hw.com, jeff@remapcorp.com, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810141827.OAA29639@candle.pha.pa.us> from "Bruce Momjian" at Oct 14, 98 02:27:05 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

> I have had more time to think about this.  Basically, for pre-sorted
> data, our psort code is very fast, because it does not need to sort
> anything.  It just moves the rows in and out of the sort memory.  Yes,
> it could be removed in some cases, and probably should be, but it is not
> going to produce great speedups.

    And I got the time to hack around about this.

    I  hacked  in  a little check into the planner, that compares
    the sortClause against the key field list of  an  index  scan
    and  just  suppresses  the sort node if it exactly matchs and
    all sort operators are "<".

    I tested with a 10k row table where key is a text field.  The
    base query is a

        SELECT ... WHERE key > 'val' ORDER BY key;

    The  used 'val' is always a key that is close to the first of
    all keys in the table ('' on the first  query  and  the  last
    selected value on subsequent ones).

    Scenario  1  (S1)  uses exactly the above query but processes
    only the first 20 rows  from  the  result  buffer.  Thus  the
    frontend receives nearly the whole table.

    Scenario  2  (S2)  uses a cursor and FETCH 20. But closes the
    cursor and creates a new one for  the  next  selection  (only
    with another 'val') as it would occur in a web application.

    If  there  is  no index on key, the backend will allways do a
    Sort->SeqScan and due  to  the  'val'  close  to  the  lowest
    existing  key  nearly all tuples get scanned and put into the
    sort. S1 here runs about 10 seconds and S2 about  6  seconds.
    The  speedup  in  S2  results  from  the  reduced overhead of
    sending not wanted tuples into the frontend.

    Now with a btree index  on  key  and  an  unpatched  backend.
    Produced  plan  is  always  a  Sort->IndexScan.   S1 needs 16
    seconds and S2 needs 12 seconds. Again nearly all data is put
    into  the  sort but this time over the index scan and that is
    slower.

    Last with the btree index on key  and  the  patched  backend.
    This  time the plan is a plain IndexScan because the ORDER BY
    clause exactly matches the sort order of the  choosen  index.
    S1  needs  13  seconds  and  S2 less than 0.2!  This dramatic
    speedup comes from the fact, that this time the index scan is
    the  toplevel  executor  node and the executor run is stopped
    after 20 tuples have been selected.

    Analysis of the above timings:

    If there is an ORDER BY clause, using an index  scan  is  the
    clever  way  if  the  indexqual  dramatically reduces the the
    amount of data selected and sorted.   I  think  this  is  the
    normal case (who really selects nearly all rows from a 5M row
    table?). So choosing the index path  is  correct.  This  will
    hurt if someone really selects most of the rows and the index
    scan jumps over the disc.  But here the programmer should use
    an  unqualified  query  to  perform  a  seqscan  and  do  the
    qualification in the frontend application.

    The speedup for the cursor/fetch scenario  is  so  impressive
    that  I'll  create  a  post 6.4 patch. I don't want it in 6.4
    because there is absolutely no query in the whole  regression
    test,  where  it  suppresses  the  sort  node.   So  we  have
    absolutely no check that it doesn't break anything.

    For a web application, that can use a unique  key  to  select
    the next amount of rows, it will be a big win.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From owner-pgsql-hackers@hub.org Thu Oct 15 00:01:10 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id AAA06040
	for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 00:01:04 -0400 (EDT)
Received: from hub.org (root@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id XAA29020 for <maillist@candle.pha.pa.us>; Wed, 14 Oct 1998 23:57:58 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id WAA02215;
	Wed, 14 Oct 1998 22:39:07 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 14 Oct 1998 22:35:19 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id WAA02061
	for pgsql-hackers-outgoing; Wed, 14 Oct 1998 22:35:16 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from sraigw.sra.co.jp (sraigw.sra.co.jp [202.32.10.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id WAA01851
	for <hackers@postgreSQL.org>; Wed, 14 Oct 1998 22:35:01 -0400 (EDT)
	(envelope-from t-ishii@srapc451.sra.co.jp)
Received: from srapc451.sra.co.jp (srapc451 [133.137.44.37])
	by sraigw.sra.co.jp (8.8.7/3.6Wbeta7-sraigw) with ESMTP id LAA17765;
	Thu, 15 Oct 1998 11:34:39 +0900 (JST)
Received: from srapc451.sra.co.jp (localhost [127.0.0.1]) by srapc451.sra.co.jp (8.8.8/3.5Wpl7) with ESMTP id LAA08260; Thu, 15 Oct 1998 11:34:54 +0900 (JST)
Message-Id: <199810150234.LAA08260@srapc451.sra.co.jp>
To: jwieck@debis.com (Jan Wieck)
cc: oleg@sai.msu.su, hackers@postgreSQL.org, t-ishii@sra.co.jp
Subject: Re: [HACKERS] What about LIMIT in SELECT ? 
From: Tatsuo Ishii <t-ishii@sra.co.jp>
Reply-To: t-ishii@sra.co.jp
In-reply-to: Your message of Wed, 14 Oct 1998 16:24:56 +0200.
             <m0zTRrE-000EBRC@orion.SAPserv.Hamburg.dsh.de> 
Date: Thu, 15 Oct 1998 11:34:54 +0900
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

>>       postgres just because of lacking LIMIT. Tatsuo posted a patch
>>       for set query_limit to 'num', I just tested it and seems it
>>       works fine. Now, we need only possibility to specify offset,
>>       say
>>          set query_limit to 'offset,num'
>>       ( Tatsuo, How difficult to do this ?)
>>       and LIMIT problem will ne gone.
>
>    Think you haven't read my posting completely. Even  with  the
>    executor  limit,  the  complete scan into the sort is done by
>    the backend.  You need to specify ORDER BY to  get  the  same
>    list  again  (without  the  offset  doesn't  make sense). But
>    currently, ORDER BY forces a sort node into the query plan.

I think we have understanded your point. set query_limit is just a
easy alternative of using cursor and fetch.

>    I  haven't  looked  at  Tatsuo's  patch  very well. But if it
>    limits the amount of data going into the sort (on ORDER  BY),
>    it  will  break it! The requested ordering could be different
>    from what the choosen index might return. The used  index  is
>    choosen by the planner upon the qualifications given, not the
>    ordering wanted.

I think it limits the final result. When query_limit is set,
the arg "numberTuples" of ExecutePlan() is set to it instead of 0
(this means no limit).

Talking about "offset," it shouldn't be very difficult. I guess all we
have to do is adding a new arg "offset" to ExecutePlan() then making
obvious modifications. (and of course we have to modify set
query_limit syntax but it's trivial)

However, before going ahead, I would like to ask other hackers about
this direction. This might be convenient for some users, but still the 
essential performance issue would remain. In another word, this is a
short-term solution not a intrinsic one, IMHO.
--
Tatsuo Ishii
t-ishii@sra.co.jp


From owner-pgsql-hackers@hub.org Thu Oct 15 10:01:17 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id KAA13960
	for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 10:01:15 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id JAA20266 for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 09:12:21 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id IAA26142;
	Thu, 15 Oct 1998 08:19:49 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Thu, 15 Oct 1998 08:13:48 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id IAA25747
	for pgsql-hackers-outgoing; Thu, 15 Oct 1998 08:13:46 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id IAA25733
	for <hackers@postgreSQL.org>; Thu, 15 Oct 1998 08:13:40 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id OAA18677; Thu, 15 Oct 1998 14:16:12 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma018279; Thu, 15 Oct 98 14:15:39 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id OAA01227;
	Thu, 15 Oct 1998 14:13:09 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id OAA28938;
	Thu, 15 Oct 1998 14:15:27 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<jwieck@debis.com>> 
	id m0zTjtm-000B5AC; Thu, 15 Oct 98 11:40 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for jwieck@debis.com 
	id m0zTmRT-000EBRC; Thu, 15 Oct 98 14:23 MET DST
Message-Id: <m0zTmRT-000EBRC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: t-ishii@sra.co.jp
Date: Thu, 15 Oct 1998 14:23:43 +0200 (MET DST)
Cc: jwieck@debis.com, oleg@sai.msu.su, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810150234.LAA08260@srapc451.sra.co.jp> from "Tatsuo Ishii" at Oct 15, 98 11:34:54 am
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

Tatsuo Ishii wrote:

> I think we have understanded your point. set query_limit is just a
> easy alternative of using cursor and fetch.
>
> >    I  haven't  looked  at  Tatsuo's  patch  very well. But if it
> >    limits the amount of data going into the sort (on ORDER  BY),
> >    it  will  break it! The requested ordering could be different
> >    from what the choosen index might return. The used  index  is
> >    choosen by the planner upon the qualifications given, not the
> >    ordering wanted.
>
> I think it limits the final result. When query_limit is set,
> the arg "numberTuples" of ExecutePlan() is set to it instead of 0
> (this means no limit).
>
> Talking about "offset," it shouldn't be very difficult. I guess all we
> have to do is adding a new arg "offset" to ExecutePlan() then making
> obvious modifications. (and of course we have to modify set
> query_limit syntax but it's trivial)

    The offset could become

        FETCH n IN cursor [OFFSET n];

    and

        SELECT ... [LIMIT offset,count];

    The  FETCH command already calls ExecutorRun() with the given
    count (the tuple limit). Telling it the offset too is  really
    simple.   And  ExecutorRun()  could  check  if  the  toplevel
    executor node is an index scan. Skipping  tuples  during  the
    index  scan  requires,  that  all  qualifications  are in the
    indexqual, thus any tuple returned by it will become a  final
    result  row  (as it would be in the simple 1-table-queries we
    discussed).  If  that  isn't  the  case,  the  executor  must
    fallback to skip the final result tuples and that is after an
    eventually processed sort/merge of the complete  result  set.
    That would only reduce communication to the client and memory
    required there to buffer the result  set  (not  a  bad  thing
    either).

    ProcessQueryDesc()  in tcop/pquery.c also calls ExecutorRun()
    but with a constant 0 tuple count. Having offset and count in
    the  parsetree  would  make it without any state variables or
    SET command. And it's the only clean way to restrict LIMIT to
    SELECT  queries.  Any  thrown  in LIMIT to ExecutorRun() from
    another place could badly hurt the rewrite  system.  Remember
    that   non-instead   actions   on   insert/update/delete  are
    processed before the  original  query!  And  what  about  SQL
    functions that get processed during the evaluation of another
    query (view using an SQL function for count(*))?

    A little better would it be to make the LIMIT values able  to
    be  parameter  nodes. C or PL functions use the prepared plan
    feature  of  the  SPI  manager   for   performance   reasons.
    Especially  the  offset  value  might  there  need  to  be  a
    parameter that the executor has to pick  out  first.   If  we
    change  the  count  argument of ExecutorRun to a List *limit,
    this one could be NIL (to mean  the  old  0  count  0  offset
    behaviour)  or a list of two elements that both can be either
    a Const or a Param of type int4.  Easy for  the  executor  to
    evaluate.

    The   only   places   where   ExecutorRun()   is  called  are
    tcop/pquery.c  (queries  from  frontend),  commands/command.c
    (FETCH  command),  executor/functions.c  (SQL  functions) and
    executor/spi.c (SPI manager). So it is  easy  to  change  the
    call interface too.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From owner-pgsql-hackers@hub.org Thu Oct 15 14:32:34 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id OAA19803
	for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 14:32:31 -0400 (EDT)
Received: from hub.org (root@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id NAA10847 for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 13:38:16 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id MAA22772;
	Thu, 15 Oct 1998 12:07:20 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Thu, 15 Oct 1998 12:02:33 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id MAA22026
	for pgsql-hackers-outgoing; Thu, 15 Oct 1998 12:02:31 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from ra.sai.msu.su (ra.sai.msu.su [158.250.29.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id MAA22007
	for <hackers@postgreSQL.org>; Thu, 15 Oct 1998 12:02:16 -0400 (EDT)
	(envelope-from oleg@sai.msu.su)
Received: from ra (ra [158.250.29.2])
	by ra.sai.msu.su (8.9.1/8.9.1) with SMTP id TAA21024;
	Thu, 15 Oct 1998 19:01:23 +0300 (MSK)
Date: Thu, 15 Oct 1998 20:01:23 +0400 (MSD)
From: Oleg Bartunov <oleg@sai.msu.su>
X-Sender: megera@ra
To: Jan Wieck <jwieck@debis.com>
cc: t-ishii@sra.co.jp, hackers@postgreSQL.org
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <m0zTmRT-000EBRC@orion.SAPserv.Hamburg.dsh.de>
Message-ID: <Pine.GSO.3.96.SK.981015193853.19322D-100000@ra>
Organization: Sternberg Astronomical Institute (Moscow University)
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

This is a little bit off-topic,
I did some timings with latest cvs on my real database 
( all output redirected to /dev/null ), table contains 8798 records,
31 columns, order key have indices.

1.select count(*) from work_flats;
0.02user 0.00system 0:00.18elapsed 10%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (131major+21minor)pagefaults 0swaps

2.select * from work_flats order by rooms, metro_id;
2.35user 0.25system 0:10.11elapsed 25%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (131major+2799minor)pagefaults 0swaps

3.set query_limit to '150';
SET VARIABLE
select * from work_flats order by rooms, metro_id;
0.06user 0.00system 0:02.75elapsed 2%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (131major+67minor)pagefaults 0swaps

4.begin;
declare tt cursor  for
select * from work_flats order by rooms, metro_id;
fetch 150 in tt;
end;
0.05user 0.01system 0:02.76elapsed 2%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (131major+67minor)pagefaults 0swaps

As you can see timings for query_limit and cursor are very similar,
I didn't expected this. So, in principle, enhanced version of fetch
(with offset) would cover all we need from LIMIT, but query_limit would be
still useful, for example to restrict loadness of server.
Will all enhancements you discussed go to the 6.4 ?
I'm really interested in testing this stuff because I begin new project
and everything we discussed here are badly needed.


	Regards,

	 Oleg



On Thu, 15 Oct 1998, Jan Wieck wrote:

> Date: Thu, 15 Oct 1998 14:23:43 +0200 (MET DST)
> From: Jan Wieck <jwieck@debis.com>
> To: t-ishii@sra.co.jp
> Cc: jwieck@debis.com, oleg@sai.msu.su, hackers@postgreSQL.org
> Subject: Re: [HACKERS] What about LIMIT in SELECT ?
> 
> Tatsuo Ishii wrote:
> 
> > I think we have understanded your point. set query_limit is just a
> > easy alternative of using cursor and fetch.
> >
> > >    I  haven't  looked  at  Tatsuo's  patch  very well. But if it
> > >    limits the amount of data going into the sort (on ORDER  BY),
> > >    it  will  break it! The requested ordering could be different
> > >    from what the choosen index might return. The used  index  is
> > >    choosen by the planner upon the qualifications given, not the
> > >    ordering wanted.
> >
> > I think it limits the final result. When query_limit is set,
> > the arg "numberTuples" of ExecutePlan() is set to it instead of 0
> > (this means no limit).
> >
> > Talking about "offset," it shouldn't be very difficult. I guess all we
> > have to do is adding a new arg "offset" to ExecutePlan() then making
> > obvious modifications. (and of course we have to modify set
> > query_limit syntax but it's trivial)
> 
>     The offset could become
> 
>         FETCH n IN cursor [OFFSET n];
> 
>     and
> 
>         SELECT ... [LIMIT offset,count];
> 
>     The  FETCH command already calls ExecutorRun() with the given
>     count (the tuple limit). Telling it the offset too is  really
>     simple.   And  ExecutorRun()  could  check  if  the  toplevel
>     executor node is an index scan. Skipping  tuples  during  the
>     index  scan  requires,  that  all  qualifications  are in the
>     indexqual, thus any tuple returned by it will become a  final
>     result  row  (as it would be in the simple 1-table-queries we
>     discussed).  If  that  isn't  the  case,  the  executor  must
>     fallback to skip the final result tuples and that is after an
>     eventually processed sort/merge of the complete  result  set.
>     That would only reduce communication to the client and memory
>     required there to buffer the result  set  (not  a  bad  thing
>     either).
> 
>     ProcessQueryDesc()  in tcop/pquery.c also calls ExecutorRun()
>     but with a constant 0 tuple count. Having offset and count in
>     the  parsetree  would  make it without any state variables or
>     SET command. And it's the only clean way to restrict LIMIT to
>     SELECT  queries.  Any  thrown  in LIMIT to ExecutorRun() from
>     another place could badly hurt the rewrite  system.  Remember
>     that   non-instead   actions   on   insert/update/delete  are
>     processed before the  original  query!  And  what  about  SQL
>     functions that get processed during the evaluation of another
>     query (view using an SQL function for count(*))?
> 
>     A little better would it be to make the LIMIT values able  to
>     be  parameter  nodes. C or PL functions use the prepared plan
>     feature  of  the  SPI  manager   for   performance   reasons.
>     Especially  the  offset  value  might  there  need  to  be  a
>     parameter that the executor has to pick  out  first.   If  we
>     change  the  count  argument of ExecutorRun to a List *limit,
>     this one could be NIL (to mean  the  old  0  count  0  offset
>     behaviour)  or a list of two elements that both can be either
>     a Const or a Param of type int4.  Easy for  the  executor  to
>     evaluate.
> 
>     The   only   places   where   ExecutorRun()   is  called  are
>     tcop/pquery.c  (queries  from  frontend),  commands/command.c
>     (FETCH  command),  executor/functions.c  (SQL  functions) and
>     executor/spi.c (SPI manager). So it is  easy  to  change  the
>     call interface too.
> 
> 
> Jan
> 
> --
> 
> #======================================================================#
> # It's easier to get forgiveness for being wrong than for being right. #
> # Let's break this rule - forgive me.                                  #
> #======================================== jwieck@debis.com (Jan Wieck) #
> 
> 

_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83



From owner-pgsql-hackers@hub.org Thu Oct 15 13:22:48 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA18540
	for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 13:22:46 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id MAA01819;
	Thu, 15 Oct 1998 12:56:25 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Thu, 15 Oct 1998 12:51:43 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id MAA01305
	for pgsql-hackers-outgoing; Thu, 15 Oct 1998 12:51:40 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id MAA01283
	for <pgsql-hackers@postgresql.org>; Thu, 15 Oct 1998 12:51:28 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id SAA21874; Thu, 15 Oct 1998 18:54:00 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma021705; Thu, 15 Oct 98 18:53:31 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id SAA25226;
	Thu, 15 Oct 1998 18:50:57 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id SAA30639;
	Thu, 15 Oct 1998 18:53:14 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<jwieck@debis.com>> 
	id m0zToEf-000B5AC; Thu, 15 Oct 98 16:18 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for jwieck@debis.com 
	id m0zTqmM-000EBRC; Thu, 15 Oct 98 19:01 MET DST
Message-Id: <m0zTqmM-000EBRC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: hannu@trust.ee (Hannu Krosing)
Date: Thu, 15 Oct 1998 19:01:33 +0200 (MET DST)
Cc: jwieck@debis.com, pgsql-hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <36261DF7.D20368A0@trust.ee> from "Hannu Krosing" at Oct 15, 98 07:08:23 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

Hannu Krosing wrote:

> Jan Wieck wrote:
> >    The speedup for the cursor/fetch scenario  is  so  impressive
> >    that  I'll  create  a  post 6.4 patch. I don't want it in 6.4
> >    because there is absolutely no query in the whole  regression
> >    test,  where  it  suppresses  the  sort  node.
>
> Good, then it works as expected ;)
>
> More seriously, it is not within powers of current regression test
> framework to test speed improvements (only the case where
> performance-wise bad implementation will actually crash the backend,
> as in the cnfify problem, but AFAIK we dont test for those now)
>
> >   So  we  have absolutely no check that it doesn't break anything.
>
> If it did pass the regression, then IMHO it did not break anything.

    Thats  the  point.  The  check  if  the sort node is required
    returns TRUE for  ALL  queries  of  the  regression.  So  the
    behaviour when it returns FALSE is absolutely not tested.

>
> I would vote for putting it in (maybe with a
> 'set fix_optimiser_stupidity on' safeguard to enable it). I see no
> reason to postpone it to 6.4.1 and force almost everybody to first
> patch their copy and then upgrade very soon.
>
> I would even go far enough to call it a bugfix, as it does not really
> introduce any new functionality only fixes some existing functionality
> so that much bigger databases can be actually used.

    I can't call it a bugfix because it is only a performance win
    in some situations. And I feel the risk is too  high  to  put
    untested  code  into  the  backend  at BETA2 time. The max we
    should do is to take this one  and  the  LIMIT  thing  (maybe
    implemented  as  I  suggested  lately),  and  put  out a Web-
    Performance-Release at the same time we release 6.4.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From Inoue@tpf.co.jp Thu Oct 15 20:31:01 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id UAA26050
	for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 20:31:00 -0400 (EDT)
Received: from sd.tpf.co.jp (sd.tpf.co.jp [210.161.239.34]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id UAA12888 for <maillist@candle.pha.pa.us>; Thu, 15 Oct 1998 20:10:03 -0400 (EDT)
Received: from cadzone ([126.0.1.40])
          by sd.tpf.co.jp (2.0 Build 2131 (Berkeley 8.8.4)/8.8.4) with SMTP
   id JAA02574; Fri, 16 Oct 1998 09:00:34 +0900
From: "Hiroshi Inoue" <Inoue@tpf.co.jp>
To: "Jan Wieck" <jwieck@debis.com>,
        "Bruce Momjian" <maillist@candle.pha.pa.us>
Subject: RE: [HACKERS] What about LIMIT in SELECT ?
Date: Fri, 16 Oct 1998 09:12:55 +0900
Message-ID: <000201bdf899$b953bf00$2801007e@cadzone.tpf.co.jp>
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0
In-Reply-To: <199810150552.BAA07576@candle.pha.pa.us>
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
Status: ROr

> -----Original Message-----
> From: owner-pgsql-hackers@postgreSQL.org
> [mailto:owner-pgsql-hackers@postgreSQL.org]On Behalf Of Bruce Momjian
> Sent: Thursday, October 15, 1998 2:52 PM
> To: jwieck@debis.com
> Cc: lockhart@alumni.caltech.edu; jwieck@debis.com; eric@linux-hw.com;
> jeff@remapcorp.com; hackers@postgreSQL.org
> Subject: Re: [HACKERS] What about LIMIT in SELECT ?
>
>
> > > I have had more time to think about this.  Basically, for pre-sorted
> > > data, our psort code is very fast, because it does not need to sort
> > > anything.  It just moves the rows in and out of the sort memory.  Yes,
> > > it could be removed in some cases, and probably should be,
> but it is not
> > > going to produce great speedups.
> >
> >     And I got the time to hack around about this.
> >
> >     I  hacked  in  a little check into the planner, that compares
> >     the sortClause against the key field list of  an  index  scan
> >     and  just  suppresses  the sort node if it exactly matchs and
> >     all sort operators are "<".
> >
> >     I tested with a 10k row table where key is a text field.  The
> >     base query is a
> >
> >         SELECT ... WHERE key > 'val' ORDER BY key;
> >
> >     The  used 'val' is always a key that is close to the first of
> >     all keys in the table ('' on the first  query  and  the  last
> >     selected value on subsequent ones).
>
> This is good stuff.  I want to think about it for a day.  Sounds very
> promising.
>

Did you see my contribution about this subject ?
I have already implemented above cases and used on trial for three
months or more.
It is good to be formally supported by PostgreSQL community.

And please remember that there are descending order cases.
(Moreover there are compound cases such as
 SELECT * from ... order by key1 desc,key2 asc;
 I didn't implement such cases.)

Thanks.

Hiroshi Inoue
Inoue@tpf.co.jp


From owner-pgsql-hackers@hub.org Fri Oct 16 04:01:07 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id EAA02029
	for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 04:01:04 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id DAA05509 for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 03:43:53 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id CAA11278;
	Fri, 16 Oct 1998 02:00:01 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Fri, 16 Oct 1998 01:57:25 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id BAA11129
	for pgsql-hackers-outgoing; Fri, 16 Oct 1998 01:57:21 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from candle.pha.pa.us (root@s5-03.ppp.op.net [209.152.195.67])
	by hub.org (8.8.8/8.8.8) with ESMTP id BAA11116
	for <hackers@postgreSQL.org>; Fri, 16 Oct 1998 01:57:00 -0400 (EDT)
	(envelope-from maillist@candle.pha.pa.us)
Received: (from maillist@localhost)
	by candle.pha.pa.us (8.9.0/8.9.0) id BAA29942;
	Fri, 16 Oct 1998 01:34:33 -0400 (EDT)
From: Bruce Momjian <maillist@candle.pha.pa.us>
Message-Id: <199810160534.BAA29942@candle.pha.pa.us>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <m0zTY6V-000EBRC@orion.SAPserv.Hamburg.dsh.de> from Jan Wieck at "Oct 14, 1998 11: 5: 7 pm"
To: jwieck@debis.com
Date: Fri, 16 Oct 1998 01:34:33 -0400 (EDT)
Cc: lockhart@alumni.caltech.edu, jwieck@debis.com, eric@linux-hw.com,
        jeff@remapcorp.com, hackers@postgreSQL.org
X-Mailer: ELM [version 2.4ME+ PL47 (25)]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

OK, I have had my day of thinking, and will address this specific
posting first, because it is the most fundamental concerning the future
direction of the optimization.

> 
>     And I got the time to hack around about this.
> 
>     I  hacked  in  a little check into the planner, that compares
>     the sortClause against the key field list of  an  index  scan
>     and  just  suppresses  the sort node if it exactly matchs and
>     all sort operators are "<".
> 
>     I tested with a 10k row table where key is a text field.  The
>     base query is a
> 
>         SELECT ... WHERE key > 'val' ORDER BY key;
> 
>     The  used 'val' is always a key that is close to the first of
>     all keys in the table ('' on the first  query  and  the  last
>     selected value on subsequent ones).
> 
>     Scenario  1  (S1)  uses exactly the above query but processes
>     only the first 20 rows  from  the  result  buffer.  Thus  the
>     frontend receives nearly the whole table.

OK.

> 
>     Scenario  2  (S2)  uses a cursor and FETCH 20. But closes the
>     cursor and creates a new one for  the  next  selection  (only
>     with another 'val') as it would occur in a web application.
> 
>     If  there  is  no index on key, the backend will allways do a
>     Sort->SeqScan and due  to  the  'val'  close  to  the  lowest
>     existing  key  nearly all tuples get scanned and put into the
>     sort. S1 here runs about 10 seconds and S2 about  6  seconds.
>     The  speedup  in  S2  results  from  the  reduced overhead of
>     sending not wanted tuples into the frontend.

Makes sense.  All rows are processed, but not sent to client.

> 
>     Now with a btree index  on  key  and  an  unpatched  backend.
>     Produced  plan  is  always  a  Sort->IndexScan.   S1 needs 16
>     seconds and S2 needs 12 seconds. Again nearly all data is put
>     into  the  sort but this time over the index scan and that is
>     slower.

VACUUM ANALYZE could affect this.  Because it had no stats, it thought
index use would be faster, but in fact because 'val' was near the lowest
value, it as selecting 90% of the table, and would have been better with
a sequential scan.  pg_statistics's low/hi values for a column could
have told that to the optimizer.

I know the good part of the posting is coming.

>     Last with the btree index on key  and  the  patched  backend.
>     This  time the plan is a plain IndexScan because the ORDER BY
>     clause exactly matches the sort order of the  chosen  index.
>     S1  needs  13  seconds  and  S2 less than 0.2!  This dramatic
>     speedup comes from the fact, that this time the index scan is
>     the  toplevel  executor  node and the executor run is stopped
>     after 20 tuples have been selected.

OK, seems like in the S1 case, the use of the psort/ORDER BY code on top
of the index was taking and extra 3 seconds, which is 23%.  That is a
lot more than I thought for the psort code, and shows we could gain a
lot by removing unneeded sorts from queries that are already using
matching indexes.

Just for clarity, added to TODO.  I think everyone is clear on this one,
and its magnitude is a surprise to me:

  * Prevent psort() usage when query already using index matching ORDER BY


>     Analysis of the above timings:
> 
>     If there is an ORDER BY clause, using an index  scan  is  the
>     clever  way  if  the  indexqual  dramatically reduces the the
>     amount of data selected and sorted.   I  think  this  is  the
>     normal case (who really selects nearly all rows from a 5M row
>     table?). So choosing the index path  is  correct.  This  will
>     hurt if someone really selects most of the rows and the index
>     scan jumps over the disc.  But here the programmer should use
>     an  unqualified  query  to  perform  a  seqscan  and  do  the
>     qualification in the frontend application.

Fortunately, the optimizer already does the index selection for us, and
guesses pretty well if the index or sequential scan is better.  Once we
implement the above removal of psort(), we will have to change the
timings because now you have to compare index scan against sequential
scan AND psort(), because in the index scan situation, you don't need
the psort(), assuming the ORDER BY matches the index exactly.

>     The speedup for the cursor/fetch scenario  is  so  impressive
>     that  I'll  create  a  post 6.4 patch. I don't want it in 6.4
>     because there is absolutely no query in the whole  regression
>     test,  where  it  suppresses  the  sort  node.   So  we  have
>     absolutely no check that it doesn't break anything.
> 
>     For a web application, that can use a unique  key  to  select
>     the next amount of rows, it will be a big win.

OK, I think the reason the regression test did not show your code being
used is important.

First, most of the tables are small in the regression test, so sequential
scans are faster.  Second, most queries using indexes are either joins,
which do the entire table, or equality tests, like col = 3, where there
is no matching ORDER BY because all the col values are 3.  Again, your
code can't help with these.

The only regression-type code that would use it would be a 'col > 3'
qualification with a col ORDER BY, and there aren't many of those.

However, if we think of the actual application you are addressing, it is
a major win.  If we are going after only one row of the index, it is
fast.  If we are going after the entire table, it is faster to
sequential scan and psort().  You big win is with the partial queries,
where you end up doing a full sequential scan or index scan, then and
ORDER BY, while you really only need a few rows from the query, and if
you deal directly with the index, you can prevent many rows from being
processed.  It is the ability to skip processing those extra rows that
makes it a big win, not so much the removal of the ORDER BY, though that
helps too.

Your solution really is tailored for this 'partial' query application,
and I think it is a big need for certain applications that can't use
cursors, like web apps.  Most other apps have long-time connections to
the database, and are better off with cursors.

I did profiling to improve startup time, because the database
requirements of web apps are different from normal db apps, and we have
to adjust to that.

So, to reiterate, full queries are not benefited as much from the new
code, because sequential scan/psort is faster, or because the index only
retrieves a small number of rows because the qualification of values is
very specific.

Those open-ended, give me the rows from 100 to 199 really need your
modifications.

OK, we have QUERY_LIMIT, and that allows us to throw any query at the
system, and it will return that many of the first rows for the ORDER BY.
No fancy stuff required.  If we can get a matching index, we may be able
to remove the requirement of scanning all the row (with Jan's patch),
and that is a big win.  If not, we at least prevent the rows from being
returned to the client.

However, there is the OFFSET issue.  This is really a case where the
user wants to _restart_ the query where they left off.  That is a
different problem.  All of a sudden, we need to evaluate more of the
query, and return a segment from the middle of the result set.

I think we need to decide how to handle such a restart.  Do we
re-evaluate the entire query, skipping all the rows up to OFFSET, and
return the number of rows they requested after OFFSET.  I would think we
don't want to do that, do we.  It would be much easier to code.  If it
is a single table, skipping forward has to be done anyway, because we
can't just _jump_ to the 100th entry in the index, unless we pass some
_tid_ to the user, and expect them to pass that back to start the query.
I don't think we went to do that.  It is ugly, and the row may have
moved since we started.  So, for a single table, adding a QUERY_OFFSET
would do exactly what we need, with Jan's patches.

For a joined query, I think you will have to do the entire _join_ before
returning anything. 

You can't just process all the joins up to the OFFSET location, and you
can't just jump to the 100th index location, because you don't know that
the 100th index location produced the 100th result just returned to the
user.  You have to process the whole query, and because of the join and
not knowing which data row from each table is going to make which entry
in the final result.  If you are really craft, and the ORDER BY table is
in the outer part of the join loop, you could start processing the table
that is part of the outer loop in _index_ order, because you know that
the rows processed in index order are going to produce the output in
result order.  You then could process and throw away the results up to
offset, and generate the needed rows and stop.

The other way of doing it is to specify a query limit based on specific
index entries, so you say I want the query returned by the first 20
index entries matching the ORDER BY, or entries 100-199, and the query
is limited to using only those entries in the index.  In that case,
though, in joins, you could return more or less rows in the result
depending on the other tables, and that may be unacceptable.  However,
for this case, the advantage is that you don't need to process the rows
from 1 to 99 because you have been told the user only wants rows from
certain index slots.  If the user requests rows 50000-50100, this would
be much faster because you don't have to process the 50000 rows before
returning any data.  However, I question how often people grab stuff
from the center of large data sets.  Seems the QUERY_OFFSET idea may be
easier for users.

I will be commenting on the rest of the optimization postings tomorrow.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026


From Inoue@tpf.co.jp Fri Oct 16 03:31:02 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id DAA01767
	for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 03:31:00 -0400 (EDT)
Received: from sd.tpf.co.jp (sd.tpf.co.jp [210.161.239.34]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id DAA04551 for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 03:13:40 -0400 (EDT)
Received: from cadzone ([126.0.1.40])
          by sd.tpf.co.jp (2.0 Build 2131 (Berkeley 8.8.4)/8.8.4) with SMTP
   id QAA02680; Fri, 16 Oct 1998 16:04:09 +0900
From: "Hiroshi Inoue" <Inoue@tpf.co.jp>
To: "Bruce Momjian" <maillist@candle.pha.pa.us>
Cc: <jwieck@debis.com>
Subject: RE: [HACKERS] What about LIMIT in SELECT ?
Date: Fri, 16 Oct 1998 16:16:29 +0900
Message-ID: <000001bdf8d4$e4cdf520$2801007e@cadzone.tpf.co.jp>
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
In-Reply-To: <199810160621.CAA01030@candle.pha.pa.us>
Status: RO

Where's my contibution to hackers@potsgreSQL.org ?
I will resend it.

> -----Original Message-----
> From: Bruce Momjian [mailto:maillist@candle.pha.pa.us]
> Sent: Friday, October 16, 1998 3:22 PM
> To: Hiroshi Inoue
> Cc: jwieck@debis.com
> Subject: Re: [HACKERS] What about LIMIT in SELECT ?
>
>
> [Charset iso-8859-1 unsupported, filtering to ASCII...]
> > > >     The  used 'val' is always a key that is close to the first of
> > > >     all keys in the table ('' on the first  query  and  the  last
> > > >     selected value on subsequent ones).
> > >
> > > This is good stuff.  I want to think about it for a day.  Sounds very
> > > promising.
> > >
> >
> > Did you see my contribution about this subject ?
>
> I am sorry.  I have not seen it, and I am confused how I could have
> missed it.
>
> > I have already implemented above cases and used on trial for three
> > months or more.
> > It is good to be formally supported by PostgreSQL community.
> >
> > And please remember that there are descending order cases.
> > (Moreover there are compound cases such as
> >  SELECT * from ... order by key1 desc,key2 asc;
> >  I didn't implement such cases.)
>
> Where is the discussion of this?  I am confused.  You have been using
> code for three months that does this?
>

Hi all.
I didn't follow all the posts about this thread.
So this post may be out of center.

I think current PostgreSQL lacks the concern to the response to get first
rows quickly.
For example,queries with ORDER BY clause necessarily include sort steps
and process all target rows to get first rows only.
So I modified my code for ORDER BY cases and use on trial.
I don't understand PostgreSQL sources,so my code is not complete.

I modified my code for the following 2 cases.

1.In many cases the following query uses index scan.
      SELECT * from ... where key > ...; (where (key) is an index)
   If so,we can omit sort steps from the access plan for the following
  query.
      SELECT * from ... where key > ... order by key;

  Currently cursors without sort steps may be sensitive diffrent from
  cursors with sort steps.  But no one mind it.

2.In many cases the following query uses index scan same as case 1.
        SELECT * from ... where key < ...;(where (key) is an index)
   If so and if we scan the index backward,we can omit sort steps from
   the access plan for the following query.
        SELECT * from ... where key < ... order by key desc;

  To achive this(backward scan),I used hidden(provided for the future ?)code
  that is never executed and is not necessarily correct.

In the following cases I didn't modify my code to use index scan,
because I couldn't formulate how to tell PostgreSQL optimizer whether
the response to get first rows is needed or the throughput to process
sufficiently many target rows is needed.

3.The access plan made by current PostgreSQL optimizer for a query with
   ORDER BY clause doesn't include index scan.

I thought the use of Tatsuo's QUERY_LIMIT to decide that the responce
is needed. It is sufficient but not necessary ?
In Oracle the hints FIRST_ROWS,ALL_ROWS are used.

Thanks.

Hiroshi Inoue
Inoue@tpf.co.jp


From wieck@sapserv.debis.de Fri Oct 16 05:01:03 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id FAA02500
	for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 05:01:02 -0400 (EDT)
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id EAA06270 for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 04:13:59 -0400 (EDT)
Received: by dsh.de; id KAA11635; Fri, 16 Oct 1998 10:12:45 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma011343; Fri, 16 Oct 98 10:12:15 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id KAA21793;
	Fri, 16 Oct 1998 10:09:49 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id KAA01799;
	Fri, 16 Oct 1998 10:12:11 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zU2aB-000B5AC; Fri, 16 Oct 98 07:37 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zU57w-000EBQC; Fri, 16 Oct 98 10:20 MET DST
Message-Id: <m0zU57w-000EBQC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: Inoue@tpf.co.jp (Hiroshi Inoue)
Date: Fri, 16 Oct 1998 10:20:47 +0200 (MET DST)
Cc: maillist@candle.pha.pa.us, jwieck@debis.com
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <000001bdf8d4$e4cdf520$2801007e@cadzone.tpf.co.jp> from "Hiroshi Inoue" at Oct 16, 98 04:16:29 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Status: RO

Hiroshi Inoue wrote:

> In the following cases I didn't modify my code to use index scan,
> because I couldn't formulate how to tell PostgreSQL optimizer whether
> the response to get first rows is needed or the throughput to process
> sufficiently many target rows is needed.
>
> 3.The access plan made by current PostgreSQL optimizer for a query with
>    ORDER BY clause doesn't include index scan.
>
> I thought the use of Tatsuo's QUERY_LIMIT to decide that the responce
> is needed. It is sufficient but not necessary ?
> In Oracle the hints FIRST_ROWS,ALL_ROWS are used.

    I  still  think  that  the  QUERY LIMIT should be part of the
    parse tree and not thrown in  by  a  magic  SET  command.  If
    rewriting  or  function  calls turn the one query sent to the
    backend into multiple queries processed internal, how  should
    this QUERY LIMIT variable know to which of all the queries it
    has to be applied?  It can really break functions and rewrite
    rules  if  this  variable  is used on all queries while it is
    set.

    For your case 3 I think, if there is a  QUERY  LIMIT  in  the
    parse  tree, the (future) optimizer definitely knows that not
    all rows will get processed even if there is no qualification
    given.   So  if  there is an index, that matches the ORDER BY
    clause and it is no a join and the (future) executor  handles
    OFFSET  in  single table index scans fast, it could choose an
    index scan for this query too.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #



From owner-pgsql-hackers@hub.org Fri Oct 16 12:02:27 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id MAA13063
	for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 12:02:23 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id MAA18435 for <maillist@candle.pha.pa.us>; Fri, 16 Oct 1998 12:01:46 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id LAA24469;
	Fri, 16 Oct 1998 11:28:54 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Fri, 16 Oct 1998 11:25:54 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id LAA24370
	for pgsql-hackers-outgoing; Fri, 16 Oct 1998 11:25:52 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id LAA24356
	for <pgsql-hackers@postgreSQL.org>; Fri, 16 Oct 1998 11:25:34 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id RAA06506; Fri, 16 Oct 1998 17:28:04 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma006149; Fri, 16 Oct 98 17:27:12 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id RAA00811
	for <pgsql-hackers@postgreSQL.org>; Fri, 16 Oct 1998 17:24:37 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id RAA04532
	for <pgsql-hackers@postgreSQL.org>; Fri, 16 Oct 1998 17:26:54 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<pgsql-hackers@postgreSQL.org>> 
	id m0zU9N0-000B5AC; Fri, 16 Oct 98 14:52 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for pgsql-hackers@postgreSQL.org 
	id m0zUBum-000EBQC; Fri, 16 Oct 98 17:35 MET DST
Message-Id: <m0zUBum-000EBQC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: [HACKERS] SELECT ... LIMIT (trial implementation)
To: pgsql-hackers@postgreSQL.org (PostgreSQL HACKERS)
Date: Fri, 16 Oct 1998 17:35:39 +0200 (MET DST)
Reply-To: jwieck@debis.com (Jan Wieck)
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

Here we go,

    this is up to now only for discussion, do not apply to CVS!

    Those involved into the LIMIT discussion please comment.

    Here is what I had in mind for the SELECT ... LIMIT.  It adds

        SELECT ... [LIMIT count [, offset]]

    to the parser and arranges that these values are passed  down
    to the executor.

    It is a clean implementation of LIMIT (regression tested) and
    the open items on it are to enable parameters and  handle  it
    in  SQL  functions and SPI stuff (currently ignored in both).
    Optimizing the executor would require  the  other  sort  node
    stuff  discussion  first to come to a conclusion.  For now it
    skips final result rows - but that's already one step forward
    since  it  reduces  the  rows sent to the frontend to exactly
    that what LIMIT requested.

    I've seen the queryLimit  by  SET  variable  stuff  and  that
    really  can  break rewrite rules, triggers or functions. This
    is because the query limit will be  inherited  by  any  query
    (inserts, updates, deletes too) done by them. Have a rule for
    constraint deletes of referencing tuples

        CREATE RULE del_table1 AS ON DELETE TO table1 DO
            DELETE FROM table2 WHERE ref = OLD.key;

    If the user now sets the query limit to 1 via SET and deletes
    a row from table1, only the first found record in table2 will
    be constraint deleted, not all of them.

    This is a feature where  users  can  get  around  rules  that
    ensure data integrity.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #


begin 644 opt_limit.diff.gz
M'XL(`$]=)S8"`^4\:W?B1K*?R:_H82=98&1;$F^<>`^+F1DV&!S`N<DGCBP:
M6SM"(I*PQSOQ?[]5_9!:(`$S<1Y[+V<&I.[JZJ[NZGIUM1?.<DE.[("$@7WJ
M!\[=V:UE?Z#>XLSV5RO+6X3RX=1&F#W57U4JE6/0%-X&#AG;$3$:Q#`Z]6JG
MVB)&N]WZZN3DY$`?J;9FI][HU&J\;27]88.IMC6H9J^(&E_K!)Y.OB+D;XYG
MNYL%)<5-Y+CAV<K^&)W>%S-JUGX066YV7?@4VI9]3['VC5(;1H'CW?$V\.^L
MPKI5/U!**H7"-4/>_TCM3>0'[ZFUOJ(K/W@B8;19+C.I,G1#,_1&0I>AFU#0
MDI05'"\J%`I+:D6;@)YCR0\;&CQ=TM"&/G^1SZR&]];SO8A^C(C-?\^!E@*4
MA8#'=5:VO_&B<TY)(9N4`M`26IX3/1&8#?M#F#URHZH91DL9N5'7C&HM'OGS
MGD[>L$YZ`9!%B85##2/B^3#;R\!?D>B>DCOG@7J$#9<\6.Z&\D8YN,[P9T57
M(8U*WT@R-:)K)'3^0_UE29:5RVQ"Y.MI]+2FA<)W9#9G<Y2N9.-B$-^1P6A6
M&P\N,P!<ZD&]Z`?6JU;.`&(D`%CI$E9R51;+L`/FA-[&=0'N;7<X[6<`W#X!
M)ASOY*:?V1YF8$]S!U@\3`$@3#X?W-&(K0;?-\GJB'?/6E$.F8/@+)MWF@W-
M:)F<=P`N=^/`?*68NHS0`OP=C1+($A]0F7'V*U*0N"8;KQ1O$HW$+:<1<)YL
MI!&QOS3.;V6Y/YPE*2THL.9WWY&1[]$R;$68JZOQCWTD#<>R7@:4)EU`4[87
MVK"+V\DN_NL06!KA+JN41S?#8?(6;YG?1OHB5P%1,5SV<&4YWI8&RJC/5D$9
M@,?KH$.-&QU#[]1;^4JH4=,:[43DP6M3EXL<PHP[-GGPG07I>XMKU_)*^$4J
M:_C62)\M":E0!*1LHD63V6;MTIEUZ]*IZT>DPM>6,@SI5K#`"4:V"%P=0(D5
MA'0&"Z*1WFHQ`YE%_#4-`+\/D&\0$D03\9=+$!"LPU`@P&)OL[JE@2@F4]OR
M+IV`VMB8+.23@&<$EBIK4(K1<N/995(JJ]3P"0`2)A04)WV@I6WZ0OC62!8>
M+5M<-$#5R'E'5;LC;#[WP[`@%V\/#;:6NK,275M1-MGVFN`,QAL,7_A6`O2?
M<,K$>A0*\8(D>KR``XG73@@/8"LCX:N_-KE<@(#XX)RE%'SY)&2K##"4FJJA
MU`01VVS'(G:;M("&&Y=9.F#],(OS$L49RC16B.Q7*)0J6.!X;$3(@4QG,J.+
M$P026D_*N#G"BJ1QL_,YWH)J`TEME:1V33/K"4E\PD\N:#A?![Y-0;<O9.=J
MI0LJ'3<3&"D>6`?.8NPLV`#?9`^06U\@X;F-)76\(-A?LK?AX&HP([9K;4)A
M>N4@8J87JHN8"<@KT!F@7\I8\XD)'V&`5FP.P684QL9^KJW`6D'=6JUC:-E3
M^.A$]CTIH7DXL^Z2;LH,O^B@`'8-E59<AQ<5['@-2ZP<-%W<^ES`)""PPN62
M:')RD5AM90EZ"_;J!VDSQ3VRX<L>J>O?E?J3R7BBD>(::VA$`V*%!#IVI/P%
M4S<BS@H8=D5![2_($XV*>=TLZ-("3L[L8>/1CVN0SH""6<]HJY*O%]O]%362
M,7N\OV?9$2ZA&-ZW1.=SF^XM10+H"*3BEA*/WL'V>:""@N?8J#R:\_BV^BV,
MQS'LX;O$X-YA.Z5J+]<)_^$@TTDAH?*<TD<B103'L=??B^%X7W\8O['NTNRF
MNEUI;N-CV\ML'&0_KX$PE*LEU!*SHE'MY,C=MF;J]=C[$!^B&%6B(+:IU%+%
MK'J5E':'P_GLYGK8GZ:`W_K!HQ4L4A95"D!5/^>\0JP':@2S86IF(]$(+S)4
MH:G5(NXNO]#`L^;<U%N::50376>"^C:;K1>@[,TN9>0%*<NDQH3!FTV%FE93
M,]O5/X&:?X)K\YO)J<+H:Z827-.K6E5OORPY>@IF/.KS_?+2:],`JZJA6E7-
M>DMK-FH*,8=]*%[\6QTI7OP%WE0F8<V:J35K"M.U#%UKF<HJ?>(HN>5+N"Y,
M;\V4",Y1T*BA81U`A#^21]3-J#<BZCX1[CY3'@#".02CFH0?G+5L)GJ.&/D$
M1'Q`P8\(45W</@G%SB?N-.X)5M,%W-!3>.]OW`6)8"DBGRQ\`O+?@1FC%.:?
MN.!)NJ%L!KC7?A@Z8.Z3TF*#`5J`7="/)(3I+DNP$_(ORQ,O><2>;5D_?/'(
MA51+0M6?G*C50G=BC-7Q-CPP&T<\,V?VB-@(KC^R1I@7'%$`#D1'%,@O"(_D
MM:YV=*.CF_GQ$5/70;4VI&K%R1`J&<V=87<*ULMX.OOAIC^<]\975]W198F&
MY3+Y!^G_U._-)_T9"`;2$6\W(QGN"B@@\5+.*@U/+GY9:`1_I6L:NZ4ZTTEB
M093`!)?68I1BX_QYH\P.QC$3-F?X1W!1N';R^(=5'>`<!I/!,^T#/+/;#KBE
MU3';^=S2!%'=3(EJ5M",)=JM[[NP-4,921IXD3^A+I/%L%1+RPWYUK/OK8!%
M$4"*^B.P@57IQX]0`K%I<XY"I`49RWIA0V:-NV76M5955R2QV=!:R7$4ZXUQ
MPO1Z,&<&[7Q\?3/Z?C3^'\8K7%*\(?*3?402?RK\YXCCDBA]7J)@V(O_3'D[
M_A!%?E)G*0E6DCI4V0&.SU8DL'+`D@WL`H$2>/NT);L%FP?10AR\1+$GEMV$
M'\#P)O$9238H.XJ1E(KCF#RDZ#$?A90?T&R!RH`\DQ\L(H\B@D?DF2)>!Q1]
M/[8O1'1><"!C>SSK7()?Q_D1A-;-#%AR.NO.IMD,7@,&KQN)AZ2P\_C[>>]F
M,AU/$D;./XX0`H_)R;?HUD7I4X8Y8K0W04#1`U:#7!FAKW,Y#?$>Q:GH75W.
MI_UAOS<CWWQ#XKY/+N3I!711YKNT`?92HY'LTA>CZMA#E3^,W'PU@5(#S^#7
M3ZCDMRV-G=IL9;$#MJLO:GJVOMC?%/2,V:G6\U6&46^V-*/>4CQ)6:0LK$<?
ML9^3BXT'T]9CH228Y(BNUG/7$0%@(8,+N%H@I]9/)12F&A&--1[1&/-@V?EA
MR)Y8:KY=!6\)D%B;'UH8?Q/EKTM2N6]9$JC/796<EK`H>J>N[['ZZFW-;"C)
M&;P@<>ZM]1IZF;(\BH&W]$&0!1J/K9U<W%OA=',[=+P/(1A8Q2C8T"+85D6F
MW8O\:"D'`\"I*USDL'.@`Y>*P^PP`E_+7(3*JA,19<I"N,,<^Q$RYCB(KQ=+
MB\\R_?CJ@76PR.<=I78?\RA@G\L]>4V!?8Q.K;IG3[/C/363!`N:B<<>^=`-
M[%\W7,-CB4O9;\`<N(ONR^=,"Z(7FF8&K@9=W[;<>98PP+()#+F$#%>6QR5'
M=Z6R"7<AU:[4VMVNWGQ!5YR!<GKJB8!R1D>)*$I:'2&-6"@D.+/`.7_Z#]WB
MJ.W*;(;:ALK@)R.;G_:VK()+T3&;>]BII6M&2SU6PX*VKKI[,I0^I2ZUHVFT
MBCHBA@/Z]E6IE)2#'D^B7&50VBRC`O->1-!:!%V2J$L46%ZX](-5@J2T%B9#
M+F(9B^=8MAEH:T#J>!3`3!R2-0ZAZ"D'%,^<)`KR-Y>XWB8(_>!8XM08U$&F
MNPNLU>E3%D?PFKWLQD$R>,W<RVL9S>H=L[HW(Q&,Y+KB!.*KL9N1:%O`+_[=
M&?PBUV2G'N+)QW:-9:,=>/;1LJ/MC$2)<WTW1P\J3DT45O[5S7`V^.?/LWX*
MW^H6X1_15<8&1R1:%`JPQ-&<'\6!V0LOMT_,@-)XL&W.#IQ"^<9KD$K^R%"@
MJ12C8"\<S%]'<RL(K*?Y+;#>(D1S*MPJ8@CHQW4@VEA1%``@<.,\LH([&HGR
MK0*3GVW`5"A]P1#CT"NY"_S-.A[5#/CHC@9O07EU@[N0S^77S#?]%N7E10'U
MVIS+T:0*HQ/4\BX*L/?FT(62O&$DMNC_RRGDK;%-R&3"G,F8+Y[8[%,\3+%K
M;F5/Z(8BY@4Z!]QTZ(E&]OW\WG^<KRSOB:O[U%#4D<YE!"7!@M-P41BOHRG]
M90C/20U,$G7CNKY+5WDGCV:CJ9G-:K*_F(<_O];(V_ZL]QY^AN/N#'[0J82O
M_N#="!XFXROX1ON`-7HWZ8X`Z-UD?`,MWW=_'(S>P>_X9@*8&,0`6@U&HSX6
MP,.T/YH.9H,?^_QE,L/?67_R8W?(GL;P/>5+]J\QMOV^_[-&AMW1NYON.V@T
M['<O61_#_EMH.QQ\CX7C7E<,Z*K+!G\U&-W,&#57X]'L/3Z,NE?R0'34G0W&
M(^P1GFXF[*'WO@N$COH_`=;1&/_C`Q#*VMY<]2>#'F\]?JN1\0C_#V%HXVO$
M!;\3_'_9QQ_HFM'/#Q<-F.<DCOA?/<]X:O+7G>X<?]#`1*9&RB.L@TNH*X*Q
M0(15H8@W\GA/`RI>!)0J:,B]]0#^51J`V?420!&WW)@A&5*HH-AM!=5J07M\
M97V@S#M+*J0%4RAXS(WX98,>Q&LSYSBUH>.IO:Z>#\,;%)EFZDC50^<7Z8F=
MDM>MK9X4A^5U6ZU#.I,J0Y?&'[-@7QL&RX49#,NR^%-\%.OMN"?1?4G7`(D1
MFZ$)5.Q:`)"Q!?0L'YBE>+@C'I'/ZR%5^RQ)??T:C5<14?/D#/#JO.G'_(ZF
MKL8CF@;HAJ8:H?^5=*<]>:Q<^$18/\5OB^?D6=1?]C,`+A2`LTK_ZGKV<^4L
M"\M91>315,X(GR?A]VVS8Z?`ST5WU0_YN_9WDJF59$\N#SJ4Y"\L."Q1K0Q?
M9OE<]/LKR>O@$"869=["!5XIHUJXHJ(Y@)^GR=SMK5,8L`BW=)OX2O/CF/3&
M8V4QFWDB-2KC'H92FW,)0X'8NH'QVM@&R+A[H=3N7+Q(M=R^=9&JW+YRD<77
MPO'BD\PRO$9^>J9R,KW`&1.Y5]SBVYOK)?>-N,ATV!?[0)\>_6`19D<`DMJ]
M/ED"]ME^64[3.K3KF'M"D@9L>,-,3C`^%9E]740]?-G_Z5F3A:!OG"ADY>]!
M_\VF295'@V*LWZ'XA!?#L**BU/`)=$B]T&&I:2E30`&(J+7@E3-0]$D%K"*P
M5C$Q%I0JGQ>/GS5QQXJ3)<782Y!U</QO]M/]&\G*7#VSI1E5-<;/"FH*W>`@
M+$!S%F.K26)WZ3(J<A,J+G(^(%5H3TERV(XI"NLJ@0-B/%8*M(SB8I\1"";C
M95)D,]*850:%^?LHH(^P$E3^7M*EXVW'TW)@LO=4#G#&SFID[ZQC$$#K5J>^
MYQRF:IA:U5`L'%9@*AEP]]1"J\T/:8D^@"0"+\]-L@5%FHQ(P1$:RO'(@T,?
MB<.%6+A98[2-+G82==BQ5]JZ$*F_Y-=?B5K;V\D+WA*D:K9QNE<YG+`8AVQQ
MQ(2/^/3TE*4_+47^$I[!!QN7DM4FY)FJ()\7!'-%$`D+_8BV_'PVY"E?)9E0
M.T<0&!"V^!IW+I\U__;?)Q<P=2SBN"]X%MG^^FS-:-]BKU1--E.E0#)8J9G-
M2OG-:AW=[%0;>X)GNE:+\X24R-;V+6$9VA(G(1B](A6>"3&6YZ*8:,QR\N("
M,!5@LEZEKB1=\X/5^(Y+YFT7R9S9]WP_^]X-BPLR2G?C@G\RI8>O[KSL5&2G
MBX%3:!B_Q\4N]>3LU9%K+^\J95Y*PEN<KN7Q8G@0"=J"@M_AKM874'#\=:PC
M2<P4_55=JU;CO8MIEL2RHPT3@\'&B]-#3T\/7`T^+L-B<C.2^7SB.GEH/5"0
MSDL_9-9OWUN(&UY"MMZL%]!<E+&SV"2#E^Q<F]*RDB_$/5Y)JW(C^O>G55F]
MG0R2/XSZS)5O5K5JLYF$+YD*FR/])7['E3%3%)_Y,'>#!0<PYK7#PFFI^\QG
M7/01VQ$OU8?2,&4Y[):KI_"[^E8(\/1=8GPXO6>Z<4]]6O?N`=S5P/4M8^[8
MQF9'KW6,/:>EK:K64F_6`@]$/KK:(<\MC%.!T1Q2+F=+*44_@IWO\8NE3!I)
M+I]&5A`==:.4Z2\5S\Y5Z!>XC'N>C#6^H(R(8?L<.\@8`=[*9P-E6%BT(H!9
MB<(25]K@&[@TT$B<J@KVFZ:T8OGQ924M[X>;_N3G.3-%>:8:6Y1$I_S?6I1C
M[([_BJ7*%PX\(X8)%O:X)1MVJ[-%PRY<AF1H94N&`VVKG6J]4]V3RM_0M49=
M^5,'\-I44RCP#(Q9"DJ4^ASU$WL/B1504%_>!W""-MZ"!EQ5!O3!\3>A>J6G
MPATVX=\A+S"T:F(#HOT;W@]-7S")?)XAD]62YS,4\EN*K!BV7X1F93$+<.>P
M&G6-!Z/F.C4F]M8**7JR/(T0&V$)D;XMP>*\OWK2J+:UAOHWC!HU`PJ2-+DX
MBSU),N$4*']QI62SQ(LRCM'F">%\B#Q7_A;<T>#IG*<&6X2_LHNFC+*R0/./
J5".V8K`1>&?LC3PZT;V_B8@X[<#CAC]SA9Y)<AHC_9+_!=<!%F+O2@``
`
end


From oleg@sai.msu.su Sun Oct 18 14:01:49 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id OAA01739
	for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 14:01:48 -0400 (EDT)
Received: from ra.sai.msu.su (ra.sai.msu.su [158.250.29.2]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id NAA23532 for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 13:51:14 -0400 (EDT)
Received: from ra (ra [158.250.29.2])
	by ra.sai.msu.su (8.9.1/8.9.1) with SMTP id UAA17832;
	Sun, 18 Oct 1998 20:45:25 +0300 (MSK)
Date: Sun, 18 Oct 1998 21:45:24 +0400 (MSD)
From: Oleg Bartunov <oleg@sai.msu.su>
X-Sender: megera@ra
To: Tom Lane <tgl@sss.pgh.pa.us>
cc: Bruce Momjian <maillist@candle.pha.pa.us>, pgsql-hackers@postgreSQL.org,
        jwieck@debis.com
Subject: Re: [HACKERS] SELECT ... LIMIT (trial implementation) 
In-Reply-To: <2292.908726689@sss.pgh.pa.us>
Message-ID: <Pine.GSO.3.96.SK.981018213213.17519C-100000@ra>
Organization: Sternberg Astronomical Institute (Moscow University)
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Status: RO

On Sun, 18 Oct 1998, Tom Lane wrote:

> Date: Sun, 18 Oct 1998 12:04:49 -0400
> From: Tom Lane <tgl@sss.pgh.pa.us>
> To: Bruce Momjian <maillist@candle.pha.pa.us>
> Cc: pgsql-hackers@postgreSQL.org
> Subject: Re: [HACKERS] SELECT ... LIMIT (trial implementation) 
> 
> Bruce Momjian <maillist@candle.pha.pa.us> writes:
> > What if someone wants the rows from 500 to the end.  Should we allow
> > the syntax to be:
> >          SELECT ... [LIMIT count] [OFFSET offset]
> > LIMIT and OFFSET are independent.
> 
> I like that syntax the best, but remember we are not inventing in
> a green field here.  Isn't this a feature that already exists in
> other DBMs?  We should probably copy their syntax, unless it's
> truly spectacularly awful...
> 
> 			regards, tom lane
> 

Mysql uses LIMIT [offset,] rows 
>From documentation:

      LIMIT takes one or two numeric arguments. A single argument
      represents the maximum number of rows to return in a result. If two
      arguments are given the first argument is the offset to the first row to
      return, while the second is the maximum number of rows to return in the
      result. 

What would be nice if somehow total number of rows could be returned.
This is often needed for altavista-like application.
Of course, I can do
select count(*) from sometable ... LIMIT offset, rows
and then
select ... from sometable ... LIMIT offset, rows
but this seems not elegant solution.

	Regards,

		Oleg
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83


From owner-pgsql-hackers@hub.org Sun Oct 18 14:31:12 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id OAA02288
	for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 14:31:10 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id OAA24844 for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 14:15:35 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id OAA26655;
	Sun, 18 Oct 1998 14:00:03 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Sun, 18 Oct 1998 13:58:57 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id NAA26381
	for pgsql-hackers-outgoing; Sun, 18 Oct 1998 13:58:55 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from ra.sai.msu.su (ra.sai.msu.su [158.250.29.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id NAA26367
	for <pgsql-hackers@postgreSQL.org>; Sun, 18 Oct 1998 13:58:49 -0400 (EDT)
	(envelope-from oleg@sai.msu.su)
Received: from ra (ra [158.250.29.2])
	by ra.sai.msu.su (8.9.1/8.9.1) with SMTP id UAA18077;
	Sun, 18 Oct 1998 20:58:41 +0300 (MSK)
Date: Sun, 18 Oct 1998 21:58:41 +0400 (MSD)
From: Oleg Bartunov <oleg@sai.msu.su>
X-Sender: megera@ra
To: Jan Wieck <jwieck@debis.com>
cc: PostgreSQL HACKERS <pgsql-hackers@postgreSQL.org>
Subject: Re: [HACKERS] SELECT ... LIMIT (trial implementation)
In-Reply-To: <m0zUBum-000EBQC@orion.SAPserv.Hamburg.dsh.de>
Message-ID: <Pine.GSO.3.96.SK.981018215259.17519D-100000@ra>
Organization: Sternberg Astronomical Institute (Moscow University)
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

Jan,

I tested your patch on my Linux box and it works ok, except
aggregates functions doesn't work properly, for example
count(*) always produces 0

kdo=> select count(*)  from work_flats limit 10,1000;
count
-----
(0 rows)

while

kdo=> select rooms from work_flats limit 10,1000;
rooms
-----
    3
    3
    3
    3
    3
    3
    3
    3
    3
    3
(10 rows)


	Regards,

		Oleg
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83



From wieck@sapserv.debis.de Sun Oct 18 15:17:53 1998
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id PAA03203
	for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 15:17:49 -0400 (EDT)
Received: by dsh.de; id VAA01180; Sun, 18 Oct 1998 21:19:50 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma001117; Sun, 18 Oct 98 21:19:33 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id VAA25465;
	Sun, 18 Oct 1998 21:17:29 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id VAA14993;
	Sun, 18 Oct 1998 21:19:58 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zUvyS-000B5AC; Sun, 18 Oct 98 18:46 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zUyWO-000EBPC; Sun, 18 Oct 98 21:29 MET DST
Message-Id: <m0zUyWO-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] SELECT ... LIMIT (trial implementation)
To: oleg@sai.msu.su (Oleg Bartunov)
Date: Sun, 18 Oct 1998 21:29:43 +0200 (MET DST)
Cc: tgl@sss.pgh.pa.us, maillist@candle.pha.pa.us, pgsql-hackers@postgreSQL.org,
        jwieck@debis.com
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <Pine.GSO.3.96.SK.981018213213.17519C-100000@ra> from "Oleg Bartunov" at Oct 18, 98 09:45:24 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Status: RO

Oleg Bartunov wrote:

> On Sun, 18 Oct 1998, Tom Lane wrote:
>
> > Bruce Momjian <maillist@candle.pha.pa.us> writes:
> > > What if someone wants the rows from 500 to the end.  Should we allow
> > > the syntax to be:
> > >          SELECT ... [LIMIT count] [OFFSET offset]
> > > LIMIT and OFFSET are independent.
> >
> > I like that syntax the best, but remember we are not inventing in
> > a green field here.  Isn't this a feature that already exists in
> > other DBMs?  We should probably copy their syntax, unless it's
> > truly spectacularly awful...
> >
> >            regards, tom lane
> >
>
> Mysql uses LIMIT [offset,] rows
> >From documentation:
>
>       LIMIT takes one or two numeric arguments. A single argument
>       represents the maximum number of rows to return in a result. If two
>       arguments are given the first argument is the offset to the first row to
>       return, while the second is the maximum number of rows to return in the
>       result.

    Simple change, just flip them in gram.y.

    And for the 500 to end:

        SELECT ... LIMIT 500, 0 (after flipped)

    The  0  has  the  same meaning as ALL. And that could also be
    added to the parser easily so one can say

        SELECT ... LIMIT 500, ALL

    too.

>
> What would be nice if somehow total number of rows could be returned.
> This is often needed for altavista-like application.
> Of course, I can do
> select count(*) from sometable ... LIMIT offset, rows
> and then
> select ... from sometable ... LIMIT offset, rows
> but this seems not elegant solution.

    Absolutely makes no sense  for  me.  As  said  in  the  other
    posting,  aggregates  do  the counting scan in a deeper level
    and thus cannot get limited. So if you invoke  an  aggregate,
    the whole scan is always done.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #



From owner-pgsql-hackers@hub.org Sun Oct 18 19:08:47 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id TAA00573
	for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 19:08:46 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id QAA01305 for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 16:14:30 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id PAA06110;
	Sun, 18 Oct 1998 15:55:20 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Sun, 18 Oct 1998 15:54:07 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id PAA05771
	for pgsql-hackers-outgoing; Sun, 18 Oct 1998 15:54:05 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id PAA05753
	for <hackers@postgreSQL.org>; Sun, 18 Oct 1998 15:53:52 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id VAA09240; Sun, 18 Oct 1998 21:56:10 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma008902; Sun, 18 Oct 98 21:55:19 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id VAA28158;
	Sun, 18 Oct 1998 21:53:16 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id VAA15349;
	Sun, 18 Oct 1998 21:55:45 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<hackers@postgreSQL.org>> 
	id m0zUwX6-000B5AC; Sun, 18 Oct 98 19:22 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for hackers@postgreSQL.org 
	id m0zUz52-000EBPC; Sun, 18 Oct 98 22:05 MET DST
Message-Id: <m0zUz52-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] SELECT ... LIMIT (trial implementation)
To: terry@terrym.com (Terry Mackintosh)
Date: Sun, 18 Oct 1998 22:05:31 +0200 (MET DST)
Cc: hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <Pine.LNX.3.95.981018155322.29282B-100000@terry1.acun.com> from "Terry Mackintosh" at Oct 18, 98 03:58:57 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

>
> On Sun, 18 Oct 1998, Tom Lane wrote:
>
> > Bruce Momjian <maillist@candle.pha.pa.us> writes:
> > > What if someone wants the rows from 500 to the end.  Should we allow
> > > the syntax to be:
> > >          SELECT ... [LIMIT count] [OFFSET offset]
> > > LIMIT and OFFSET are independent.
> >
> > I like that syntax the best, but remember we are not inventing in
> > a green field here.  Isn't this a feature that already exists in
> > other DBMs?  We should probably copy their syntax, unless it's
> > truly spectacularly awful...
> >
> >            regards, tom lane
>
> None that I have used (VFP, M$ SQL Server) that had 'LIMIT', had 'OFFSET'.
> So it would seem that the very idea of OFFSET is to break with what others
> are doing.
>
> I too like the above syntax.
> Why mimic, when you can do better?  Go for it!
>

    We have a powerful parser. So we can provide

        ... [ LIMIT { rows | ALL } ] [ OFFSET skip ]

        or

        ... [ LIMIT [ skip , ] { rows | ALL } ]

    at the same time.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From owner-pgsql-hackers@hub.org Sun Oct 18 19:08:39 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id TAA00557
	for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 19:08:37 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id QAA03555 for <maillist@candle.pha.pa.us>; Sun, 18 Oct 1998 16:56:03 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id QAA10374;
	Sun, 18 Oct 1998 16:36:26 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Sun, 18 Oct 1998 16:35:16 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id QAA10298
	for pgsql-hackers-outgoing; Sun, 18 Oct 1998 16:35:15 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id QAA09974
	for <hackers@postgreSQL.org>; Sun, 18 Oct 1998 16:32:21 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id WAA18249; Sun, 18 Oct 1998 22:34:46 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma018115; Sun, 18 Oct 98 22:34:11 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id WAA29950;
	Sun, 18 Oct 1998 22:32:01 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id WAA15581;
	Sun, 18 Oct 1998 22:34:28 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<jwieck@debis.com>> 
	id m0zUx8Z-000B5AC; Sun, 18 Oct 98 20:01 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for jwieck@debis.com 
	id m0zUzgV-000EBPC; Sun, 18 Oct 98 22:44 MET DST
Message-Id: <m0zUzgV-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] SELECT ... LIMIT (trial implementation)
To: jwieck@debis.com
Date: Sun, 18 Oct 1998 22:44:15 +0200 (MET DST)
Cc: terry@terrym.com, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <m0zUz52-000EBPC@orion.SAPserv.Hamburg.dsh.de> from "Jan Wieck" at Oct 18, 98 10:05:31 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

>     We have a powerful parser. So we can provide
> [...]

    This version now accepts all of the following

        ... [ LIMIT rows ] [ OFFSET skip ]
        ... [ OFFSET skip ] [ LIMIT rows ]
        ... [ LIMIT [ skip , ] rows ]

    rows  can be a positive integer constant greater that 0, a $n
    parameter (in SPI_prepare()) or  the  keyword  ALL.  0  isn't
    accepted  as  constant to force ALL in that case making clear
    that this is wanted. In the  parameter  version  the  integer
    value 0 still is used to mean ALL.

    skip can be a positive integer constant greater or equal to 0
    or a $n parameter for SPI_prepare.

    If any of these syntaxes is used  in  SPI_prepare()'d  plans,
    the  given tcount argument for SPI_execp() is ignored and the
    plan or parameter values are used.

    Anyone happy now?


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #

begin 644 opt_limit.diff.gz
M'XL(")%0*C8"`V]P=%]L:6UI="YD:69F`.4\:W?;-K*?U5^!:-.NY-"V*-EZ
MN?$>5:83;67)U2-MSSWWZ-`29/-&(E62BN--_=_OS``@08F4E4>;[JY/&Y'`
M8(`9#&8&@P%GSGS.#J<^"_SID><[M\<W]O0M=V?'4V^YM-U9H!Z.I@BSH_J;
M@X.#?=#D+GV']:<A,ZO,-)NGE6:ESLQ&H_[-X>'A$WTDVI:;I]7FR8EH>Y#\
MH\%4&@94TRNBQM=3!D^'WS#V-\>=+M8SSO+KT%D$Q\OI^_#H+I]2L_+\T%ZD
MUP4/P=2>WG&L?:'5!J'ON+>B#?QW?$#=ZG]0R@YRN6M";KWGTW7H^:^YO;KB
M2\]_8$&XGL]3J3)+IF&6JC%=9JD,!75%6<YQPUPN-^=VN/;Y&9;\M.;^PP4/
MIM#G;^J9:D1O;<\-^?N03<7O&="2@[(`\"R<Y=1;N^&9H"273DH.:`ELUPD?
M&'!C^C9('[E9,4RSKHW</#7,RDDT\L<=G;R@3MH^D,69C4,-0N9ZP.VY[RU9
M>,?9K?..NXR&R][9BS47C3)P'>//DB\#'A:^4V0:K&2PP/D7]^8%558L$D/4
MZU'XL.*YW$LVFA"/DI4T+H)XR3J]T4F_<Y$"L.`NU,M^8+Y.BBE`1`*`%2Y@
M)I=%.0U;8$[@KA<+@+ML=8=6"L#-`V#"\0[&5FI[X,".Y@Z(>)``0)AL.;CE
M(<V&6#?Q[,AWUUYR`9F!X#A==FI5PZR7A>P`7.;"`7XEA+J(T!+\%0]CR((8
M4)$D^QG+*5R#M5N(%HG!HI;#$"1/-3*87%^&D+>B6A_.G!5F'$3SY4O6\UQ>
MA*4(O+KJO[&0-!S+:NYS'G<!36DM-&`5-^)5_-<AL-##5790[(V[W?@M6C*?
M1_HLTP!Q.5QZN+(==\,"I=2GFZ`4P/UMT!.-RZ5FZ;1I-K*-4/7$J#9BE0>O
MM9*:Y``X[DS9.\^9,<N=72]LMX#_L(,5_&LPBZ:$'7`$Y,1HV62T7BWXR+Y9
M\.'""]F!F%M.&)*M8()CC#0)PAQ`B>T'?`038K#V<C8"G<6\%?<!OP>0+Q`2
M5!/SYG-0$-1A(!%@L;M>WG!?%K/AU'8O')]/L3&;J2<)3P06#E9@%,/YVIT6
M6:&H4R,8`"0,.!A._HX7-ND+X%^#I>$QTM5%%4R-XCN:VBUE\[%_A`6E>'-H
ML+3TE17;V@-MD6W."7(P6F#X(I82H/^`+)/SD<M%$Q+;\1P.))H[J3Q`K,Q8
MKO[:Y`H%`NI#2)96\.E,2#<9X"C5=$>I!BJVUHA4["9I/@_6"_)TP/LAC_,"
MU1GJ-"I$\<OE"@=8X+@T(I1`LIGD=`F"0$.7XC+ACE"1<FZV_O;WH!I`4D,G
MJ7%BE.LQ28+AA^<\F*Q\;\K!ML]4YWKE`DPZ+B9P4ESP#IQ9WYG1`%^D#U!X
M7Z#AA8^E;+PDV)O36[=SU1FQZ<)>!]+URD!$KA>:BT@(V#.P&6!?BECS@92/
M=$`/I@*".)J[MGU["86KK<*N$X0==^[E5O36$54T!8YR6G+!O1-.[U@!_<:1
M?1OW7Z2.9<\Y<'BX<N^:HB@WC2:W0.5@`J/69Q(F!H%^BP79Y/`\=N>*"O0&
M'-FWT;A4CT2)ZG$5HZ/RM!XEL0"CSSP63A;`D(D#'(E[(9Y'332.XQ]?>+<%
M:S#H#PR6)R`><I_-/9]!MXXR!.!SA[">F3*,I,-Y/B(,X0L.R1R37?V/\[]'
M;QUWAI-\W1JTKB:=WIM6%SQCYKQXH0;P00U$&V34\J5JV1M?L>^^TS$[5+M2
MO*8:9Q:1%7,:7QZW&;'=AQS=E^;,1I_";T_O)($63"MBON$T7V)G$V/=D#FM
M@UC@TF5NQN<VZ#LE;HD!K%W^?@4V'+0'[;%P1\.^G3$[2)"<-UC*4A)#>U0=
M(=URE-^SDB1X'W)=?@M*]ITB]C':>NRMGX3R_1SU)##LT$[QMBQ63IMEGZJ;
MY/;S2=6D;(RNF;0QQ$9(ZB5Z_62U%"'3M%*BMS]3*8G!_$5UDN3SUU!)>_'E
MHS62P+I;(25E[<_21]1K4AWIP9ND-A)#W*F,-BE-TT7@4JE%*YU;VHNC\YKA
MO36,<NDTBF'(/Z9MS61!M#/32[7-V;.XM-7M3D;CZZXU3`!?>OZ][<\2^[($
M@.[$GHD*.1_H5Y8;9:/<B/W*+S)4Z>_K12+H]H4&GL;S<JENE,V*%@4NE8V*
M%BW]=,I>;%/&OB!EJ=2485K*-8T:LV94RI6O0,T/]O3M9Y-3J1GED[)&3J5B
M5"J-+TM.*0'3[UEBO7SIN:G"WJRJ[\UJ];I1:YQHQ#P=B1'%GQN.$<6?$)-)
M):QV4C9J)YK0U4]*1OU4FZ4/`J78/S-API-+,Z&",QPX].!@'D"%W[-[]-V6
M0$S(%P_2?G$11D8>PM:<!6^=E6HF>PZ)?`8JWN>_K6'&P%S</$C'3S#N*.H)
M9G,!N*&GX,Y;+V8LA*D(/3;SF$-6<\8Y\)\M^#N^"%0SP+WR@L"Y67!6F*WQ
MF`=@9_P]"X#=105VR/YIN_(EB]CC#>]83!X[5V9).A^'AWJUM+)X4N.X:W&\
M$YV;I')VCP@KSC^*1I`58M4`GHBQ:I"?$&3-:MUHELQFJ9P=934K)<.LG&J!
M$2K84O,Y$9D6;!/>*<BK.%5282T1!R^J<`C-$,6V<(4>GI-_T$]$+=COO[--
MB/;6QF'#S]"W(^*49/A3ET4<(-_M`?IP<!DLN0O"+)P/&CL.RK7]6RDOVBH4
MQW].ECDL@0M252X(MI"N"WILW=80G/W^</33V.I.VOVKJU;OHL"#8I']@UF_
M6.W)P!J!`F5-^3;NJ<,%GP,2-Q$:Y,'A^6\S@^&O"@1&0<`2\5<*KA8&%MY'
MJ0*CC!3,UQME^M$'S6C&\/=8;<'*R5IG5/7$"B.8E+75>&)M;;=K-,NGS4HE
M>U75P*35$B:-"N)5=>-Y"Y"U0,7M.V[H#?B";!9,U=Q>!$)%3>]LGV*V()U>
M#W8KNI40$NM+Y29WUB&Y')-I=,2*S*=PH-A>J-:Z%Q[92^F'I]%4+Y\:]4I)
MLV;EFE&OGFJ:0DK)\+HSH<4ZZ5^/>S_V^C^3'`EM^V+7*?4K&7X@G2#-#]DF
MX4<]?2PM-V;"*<A4*!^D-4W%A#W07TOMD(2F`<-&9A2QAX"=>>^X[SO@WV"Y
MLE^"^RS:62J,&3T)4Z:F)G7DT;8)M*"(O^PW_)Z7"-S@^*.Q'[%Q@,D`"V]J
M+U@[2@F(J/#8:@U3(0:&H@>F?2]25%Z`+H5Z;H!>KK:8>ADE";#X+YDMD`2-
M4@84J)XWD`*ZX&Z$=2N%(`6>]MT27N82A%J\9KN!B`.(!G%60`H@918H^E1V
M02I"-)9[(!2Y!AN`"=F*CI^W](/:DN/:D89=KA5Q#DV.X\KG*-.DG^29M%SM
MI'XPPV<^XW.Q]L%XC$>P_(>CUFB8KDQ.0)F<FO&.7E,=_1\G[?%@V!_$2B/[
M$%X:'K)7E^@>A,FS]0EBG*Y]GU-`23O:23GP.5-LB/0ALJ)]=3$96EVK/<)@
M5=3WX;DZLX<NBD(CUL&_KU=CC?C%J-K4#'UY`OB5B,TVUJA%,.]L]8`.V:9?
MO%6;;K*WP+:M]DDIW6KO;@K6OMP$;S?;'3ZMU0WSM*[%/521-JTNO\=^#L_7
M+K"M+?3K2Q;RY8H"M0EKA^L.5-CJH8"^JL%D8X-I$UD\>QJR+:=:+%8I61(D
M\JF>FAAO'6;/2URY:UIBJ(^=E8R6,"FEYFDI>U+*IPVC7-42$D5![$W9JQ7T
M,J3<03PN`#7F&^)`X/#\S@Z&ZYNNX[X-P,W-A_Z:Y\'#S9./E1?I%!D8`$Z?
MX;R`G0`=.%4"9DL0Q%QF(M1W03(FFH9P2SAV(Q0^SE/XVI&V^"@'7,P>;/5F
MV;*CU>X2'@WL8Z4GJRF(C]D\V>&,BY06/7L2"VIQ?"GTH!M8OXM@!8\%L6?Y
M#KR%V_"N>$8V$&,F26$01I`<J$F:,L"R`0RY@`(7[8GW[DH7$^%:Z5WIM=M=
MO?B$KH0`9?34ECY$2D>Q*HI;[:&-R)[YQ[9K+Q[^Q3<D:K,R7:`VH5+DR4R7
MIYTM*\TR;.]J.\0)C+Q9UU-)L*!1TC?=ZOQOR!=\&@[#9=B4,0:PM\\*A;@<
M/++8N!?!:%,6(>9ZRM"'#$[$,<+0M]U@[OG+&$EA)1V&3,3JC$E@V12@C0'I
MX]$`4W$HT7@*15L[YWP4)-%V)HNX]MH/,**T'W%ZQ/1)H;N%K=G10YI$B)J=
MXB9`4F2MO%/69+/AVA7-ZJQ<;I9+S4HU6]#`13[5MMOX:FYGX4]MD!?O]AA^
M46K2T^TQ?K998T_1#SQ^;T_#S2Q\A7-U.\'M592.+WW\JW%WU/GAUY&5P+>\
M0?A[#%A@@SV2"W,YF&+8B9#*!*<77FX>R($R1&A8G'T'ZDW4()7BD5"@JQ2A
MH!<!YJW"B>W[]L/D!D1O%J`[%6P4B6#F^Y4OV]AAZ`,@2.,DM/U;'LKRC8*R
M.(D#5FA]P1"C@P)VZWOK532J$<C1+?<OP7BU_-M`\/);VKA^C_KR/(=V;2+T
M:%R%,2)NN^<Y6'L3Z$)+6#1C7_2_DH6B-;8)2"=,2,=\,F/3SYPQK;RVD3%8
M,C4U+]$YL)>'GG@XO9O<>?>3I>T^"'.?&(H^THD(+<@B<4PQ43<O8LS(FO-<
M?Q4.^6^8"A/7`./X(JJS%GR9=79>KM:,<JT2KSD*#TRN#79IC=JOX:?;;XW@
M![>9\(_5>=6#AT'_"OY%GX$:O1JT>@#T:M`?0\O7K3>=WBOX[8\'@(D@.M"J
MT^M96``/0ZLW[(PZ;RSQ,ACA[\@:O&EUZ:D/_P[%-/ZSCVU_M'XU6+?5>S5N
MO8)&7:MU07UTK4MHV^W\B(7]=DL.Z*I%@[_J],8CHN:JWQN]QH=>ZTH=Z?=:
MHTZ_ASW"TWA`#^W7+2"T9_T"6'M]_!\?@%!J.[ZR!IVV:-V_-%B_A_]W86C]
M:\0%OP/\_\+"'^B:Z*<]2<T$/M?BA?GOS&>,&_YUV9TJYHV&4=%31#J]]L"Z
MLGK$D`OK%_QY#;V-AL2I$="-K.E%K`>>M/M7UYTN]M7M`$0/68!@R`@Q''CZ
MD<AO_0(<'EO$$O74?R//QGO6ST@I#*`ULBY^B)_'0T3>Z_<LXL1KXCP\="Y_
MI5\Q&IRL?N=BB%RP!JT1\N"Z-1S^#(R`IT&_;5T0>ZFS@=7N7`.^@853@;]#
M:X0_,`4]P#'HPV`&8W5N/[1^&EN]-@#"6#HX1P".O!^^1L#AJ(4RA)$[R;WA
MZ`+E!GY@`@P,5P)K8!R$;=Q3G!KW1AU`]J;5'H^O\+?;`:`WUN"'_M"BAR%P
M4&:25&"NM&/P?_>YNKPDEO^GSEE&3`;TW:E^Q@5OIT:Y6M*<DQR3GKWF8K#[
M.^YS^2*A=&//[NQWCGN;!*"]M0+07!ZQH6`IGD!.VSOE])T#[HF7]EM.$9*X
M0NTB<CF7MO*_K7$7_[R<<1Q<+9615LT_P#<H*I<31^<N!J"0GB@P\+R^T9,6
M-'C>T.N0SKC*+)WI297/39/.M#IQSF>4=(F--T($X5VA9``2,]H*QE#1]AZ`
MS`V@QSBI47%[9T?B=#&KAT3MHR+U^7/M?,)5'!#56>S'C,!:28\)UDSPSQ)I
M0[^SUK`=)3!\8-1/_OO\&7N4]1=6"L"Y!G!\8%U=CWX].$[#<GP@,R\/CIG@
MDXR];(IC,R=.XE+\/?9WX^]LVS6D8S[1U4)$_@KJMX=+]GFY"/^<%,]DQ[^S
M1`^ZDRFT4X:ON;N3DR+U]'0G3V&BTZ0-7-GC^N1NMKGR&<0C+I&P$&$[/F`D
M#C).)IL#^%ER_K='WLQUZ,!-Q73$$A`G]DF-1&7%C2SOYR;[GIGI"<:B,SR^
M$<?5RW5`B;>M;I=Y/K,Q[<K!#%P\Q^6PB1)YQYC^DM?[<65>>\H=;*TVXP*V
M!K%Q^_JYN0F0<N]:J]VZ=)UHN7GC.E&Y>=TZ3;?(`)283V#1QTS('\BETE^7
M291'GV23N*^19!.5Z612X$$DX\?I_IOU=),N(2)43)O<+>[N&&9RX>GK_#-6
MWG_ABOC+3[;P">3'09Z.];[E#_>>/PO23QCBVITQWQCLH^.^6M-D[)?.&78<
M,H`S8Y;C_(@/>8K?Y>4&Z=%0A>!+.V&0CW=,<97+_7P4*X#B0U$,PPKS*EH0
M0P?<%68BGP@K:``AMV?Y:$,65X3<!P'-QX$'K<H3Q?U'0WZW1)"E7+0O0=:3
MXW^QF^[/)"MU]LIUPZSH.014<*+1O8!>85>0CR(P"ON"S\.\",=$1<Y;I`IC
M,XH<,O5Y&:F)X8`8-Z]VQ%&Q1P3B]C@NFA)I%.')(N*T:IA5,Y$(`045C0C4
M6GD5SE&XW?42)F^:C\([JL*;0UG_4I$07804/EH$Y<QPWG$O'14M</SM\0"V
MRS$<$MI'(K.5@,_O08RX^KW@<\?=/&S,@$E7"!G`*6JAFJX6]D$`K>O-TQU)
M*A6S;%3,Q&66<O(RRQVW<3OM!;S`WW$WG/@RMS1*%<H=JVQZZ6T[+GOG\'OF
MB`3J8+W"HT@^V\JYIYR@W=G<GY3)G>A5#2?07-1C$=B#$1\='=%-AKF\BH!Y
MF?YZP2/?%P\X9@S3F1$)G8O)MB)U+1"W-PKJ;MP$06!`V.);%#_!->_F_P[/
M@75T'+OK9#&<>JOC%=&^(5Z)FG2A2H"DB%(M792RFYTT2^7=)XLEXR1*9=>.
M_38_&Z;._62:"![ML0.1\]]726-X9Y"NUT0%8,Z!6<\2WRBY%EEGT4<O4C]_
MH80S_<-?'_TA#CHT)4JW#TV_,J5/?\OCR[(B_4;#J5$VS3_B2R]Z6M&S/>=>
M?;PD]2LE^%FGA>V*8GB0=RTE!7_`QUL^@8+]O\^R)XFIJK]2,BJ5:.UBKC:S
MI^&:U*"_=J.;7D='3WPK;+_DT\&XIZZ<R._+!39%$N9>0)>F+7<F/_DB=>MX
M-8/FLHP2U>++>&SK.RI&6F:J_+"7HE7[1-H?3ZLV>UO?Z?K3J$^=^5K%J-1J
M\0$7F;`)TE\0'[V*LON+4?ZIB-KB&<66"">U[J/@N.PC\B.^5!]:PXT$YLUR
M/45QV]Y*!9[\N!@^'-V1;=Q1G[2].P"W+?#IAC.W;^-RLW32-'>DDM4K1EW_
MU!;(0.CA=C@0]]KB.VWR6P3R:VU*2_'WL$EQQ9>F2!MI-_'\<*]/3)']TO%L
M?1OM"WR=ZRP>:_3%,D0,RV??048(\#-]-%#"0C$;'[@2!@5AM&%CL^"^P:+;
M5."_&5HKNDY4U&XL_#2V!K].R!452?PT*;%-^<^:E'W\CG^+J<I6#B)=F!0+
M/6[HANWJ=-6P#9>B&>KIFN&)MI5FY;19V7$KMUHRJMJ=7'RMZ?FEF`Q$GH)V
M?'B&]HG>`V;[',R7^Q8V06MWQGUA*GW^SO'6@7X[_T!>M1/[.[HIB&CUK$]$
M^S?\%%#RKGCHB?3AM)8BV3.7W5*F#--ZD9:5`BZPG<-JM#4NC%K8U(C8&SO@
MN),5=RRP$98PM;=E6)SU&=1JI6%4]8\:5T],*(CO$$07+>,,7$&!]@G6PI2R
M4HLXQJGXH*X8HKC.>0/;4?_A3-R:LIEXI<^Y$&5%B>8?B48T8[`01&?TQNZ=
>\,Y;ATP>0^,Y\-><H4<6'Y.K?<G_`Z+L!%(`6P``
`
end


From owner-pgsql-hackers@hub.org Mon Oct 19 07:31:10 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id HAA05591
	for <maillist@candle.pha.pa.us>; Mon, 19 Oct 1998 07:31:09 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id HAA13574 for <maillist@candle.pha.pa.us>; Mon, 19 Oct 1998 07:12:57 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id GAA13957;
	Mon, 19 Oct 1998 06:25:09 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Mon, 19 Oct 1998 06:22:35 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id GAA13581
	for pgsql-hackers-outgoing; Mon, 19 Oct 1998 06:22:33 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id GAA13566
	for <pgsql-hackers@postgreSQL.org>; Mon, 19 Oct 1998 06:22:27 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id MAA13918; Mon, 19 Oct 1998 12:21:16 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma013635; Mon, 19 Oct 98 12:20:55 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id MAA11037;
	Mon, 19 Oct 1998 12:18:27 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id MAA29382;
	Mon, 19 Oct 1998 12:20:49 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zVA2V-000B5AC; Mon, 19 Oct 98 09:47 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zVCaT-000EBPC; Mon, 19 Oct 98 12:30 MET DST
Message-Id: <m0zVCaT-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: Inoue@tpf.co.jp (Hiroshi Inoue)
Date: Mon, 19 Oct 1998 12:30:52 +0200 (MET DST)
Cc: jwieck@debis.com, pgsql-hackers@postgreSQL.org, maillist@candle.pha.pa.us
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <002801bdfb46$39ad8ec0$2801007e@cadzone.tpf.co.jp> from "Hiroshi Inoue" at Oct 19, 98 06:52:46 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

Hiroshi Inoue wrote:

> When using cursors,in most cases the response to get first(next) rows
> is necessary for me,not the throughput.
> How can we tell PostgreSQL optimzer that the response is necessary ?

    With my LIMIT patch, the offset and the row count are part of
    the querytree. And if a LIMIT is given, the limitCount elemet
    of the querytree (a Node *) isn't NULL what it is by default.

    When a LIMIT is given, the optimizer could assume that  first
    rows  is  wanted (even if the limit is ALL maybe - but I have
    to think about this some more). And this assumption might let
    it  decide  to use an index to resolve an ORDER BY even if no
    qualification was given.

    Telling the optimizer that first  rows  wanted  in  a  cursor
    operation would read

        DECLARE CURSOR c FOR SELECT * FROM mytab ORDER BY a LIMIT ALL;


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From owner-pgsql-hackers@hub.org Tue Oct 20 06:01:49 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id GAA02483
	for <maillist@candle.pha.pa.us>; Tue, 20 Oct 1998 06:01:48 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id FAA07799 for <maillist@candle.pha.pa.us>; Tue, 20 Oct 1998 05:51:19 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id FAA00108;
	Tue, 20 Oct 1998 05:17:58 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Tue, 20 Oct 1998 05:16:37 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id FAA29953
	for pgsql-hackers-outgoing; Tue, 20 Oct 1998 05:16:35 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id FAA29939
	for <hackers@postgreSQL.org>; Tue, 20 Oct 1998 05:16:27 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id LAA04585; Tue, 20 Oct 1998 11:15:05 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma004337; Tue, 20 Oct 98 11:14:46 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id LAA14628;
	Tue, 20 Oct 1998 11:12:27 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id LAA03564;
	Tue, 20 Oct 1998 11:14:52 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zVVUa-000B5AC; Tue, 20 Oct 98 08:42 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zVY2c-000EBPC; Tue, 20 Oct 98 11:25 MET DST
Message-Id: <m0zVY2c-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: Inoue@tpf.co.jp (Hiroshi Inoue)
Date: Tue, 20 Oct 1998 11:25:22 +0200 (MET DST)
Cc: maillist@candle.pha.pa.us, jwieck@debis.com, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp> from "Hiroshi Inoue" at Oct 20, 98 05:24:09 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

Hiroshi Inoue wrote:

> >   * Prevent psort() usage when query already using index matching ORDER BY
> >
> >
>
> I can't find the reference to descending order cases except my posting.
> If  we use an index scan to remove sorts in those cases,backward positioning
> and scanning are necessary.

    I  think  it's  only thought as a reminder that the optimizer
    needs some optimization.

    That topic, and the LIMIT stuff too I think, is past 6.4 work
    and  may  go into a 6.4.1 performance release. So when we are
    after 6.4, we have enough time to work out a  real  solution,
    instead of just throwing in a patch as a quick shot.

    What  we  two did where steps in the same direction. Your one
    covers more situations, but after all if multiple people have
    the  same  idea  there  is a good chance that it is the right
    thing to do.

>
> Let t be a table with 2 indices, index1(key1,key2), index2(key1,key3).
> i.e. key1 is common to index1 and index2.
>
> And for the query
>   select * from t where key1>....;
>
> If   PosgreSQL optimizer choose [ index scan on index1 ] we can't remove
> sorts from the following query.
>    select * from t where key1>... order by key1,key3;
>
> Similarly if  [ index scan on index2 ] are chosen  we can't remove sorts
> from the following query.
>    select * from t where key1>... order by key1,key2;
>
> But in both cases (clever) optimizer can choose another index for scan.

    Right. As I remember, your solution does basically  the  same
    as  my  one. It does not change the optimizers decision about
    the index or if an index at all is used.  So  I  assume  they
    hook  into  the same position where depending on the order by
    clause the sort node is added. And that is at the very end of
    the optimizer.

    What  you  describe above requires changes in upper levels of
    optimization.  Doing that is far away from my knowledge about
    the  optimizer.   And  some of your earlier statements let me
    think you aren't familiar enough with  it  too.  We  need  at
    least help from others to do it well.

    I  don't want to dive that deep into the optimizer. There was
    a far too long time where the rule system was broken and  got
    out  of  sync with the parser/optimizer capabilities. I fixed
    many things in it for 6.4. My first priority now is,  not  to
    let such a situation come up again.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From wieck@sapserv.debis.de Tue Oct 20 13:00:04 1998
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA08269
	for <maillist@candle.pha.pa.us>; Tue, 20 Oct 1998 13:00:01 -0400 (EDT)
Received: by dsh.de; id TAA14203; Tue, 20 Oct 1998 19:02:15 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma014037; Tue, 20 Oct 98 19:01:39 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id SAA24445;
	Tue, 20 Oct 1998 18:59:16 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id TAA06159;
	Tue, 20 Oct 1998 19:01:40 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zVcmS-000B5AC; Tue, 20 Oct 98 16:29 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zVfKV-000EBPC; Tue, 20 Oct 98 19:12 MET DST
Message-Id: <m0zVfKV-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Tue, 20 Oct 1998 19:12:19 +0200 (MET DST)
Cc: jwieck@debis.com, Inoue@tpf.co.jp, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810201645.MAA07946@candle.pha.pa.us> from "Bruce Momjian" at Oct 20, 98 12:45:49 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Status: ROr

>
> I agree.  Another good thing is that the LIMIT thing will not require a
> dump/reload, so it is a good candidate for a minor release.

    That's wrong, sorry.

    The  limit  thing as I implemented it adds 2 new variables to
    the Query structure. Rewrite rules are stored  as  querytrees
    and in the existing pg_rewrite entries that would be missing.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #



From owner-pgsql-hackers@hub.org Tue Oct 20 13:24:47 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA08484
	for <maillist@candle.pha.pa.us>; Tue, 20 Oct 1998 13:24:45 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id NAA01878;
	Tue, 20 Oct 1998 13:00:06 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Tue, 20 Oct 1998 12:59:59 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id MAA01579
	for pgsql-hackers-outgoing; Tue, 20 Oct 1998 12:59:58 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id MAA01557
	for <hackers@postgreSQL.org>; Tue, 20 Oct 1998 12:59:52 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id TAA14203; Tue, 20 Oct 1998 19:02:15 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma014037; Tue, 20 Oct 98 19:01:39 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id SAA24445;
	Tue, 20 Oct 1998 18:59:16 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id TAA06159;
	Tue, 20 Oct 1998 19:01:40 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zVcmS-000B5AC; Tue, 20 Oct 98 16:29 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zVfKV-000EBPC; Tue, 20 Oct 98 19:12 MET DST
Message-Id: <m0zVfKV-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Tue, 20 Oct 1998 19:12:19 +0200 (MET DST)
Cc: jwieck@debis.com, Inoue@tpf.co.jp, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810201645.MAA07946@candle.pha.pa.us> from "Bruce Momjian" at Oct 20, 98 12:45:49 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

>
> I agree.  Another good thing is that the LIMIT thing will not require a
> dump/reload, so it is a good candidate for a minor release.

    That's wrong, sorry.

    The  limit  thing as I implemented it adds 2 new variables to
    the Query structure. Rewrite rules are stored  as  querytrees
    and in the existing pg_rewrite entries that would be missing.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #




From wieck@sapserv.debis.de Tue Oct 20 13:10:22 1998
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA08339
	for <maillist@candle.pha.pa.us>; Tue, 20 Oct 1998 13:10:18 -0400 (EDT)
Received: by dsh.de; id TAA17171; Tue, 20 Oct 1998 19:12:30 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma017064; Tue, 20 Oct 98 19:12:00 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id TAA24806;
	Tue, 20 Oct 1998 19:09:37 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id TAA06212;
	Tue, 20 Oct 1998 19:12:01 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zVcwS-000B5AC; Tue, 20 Oct 98 16:39 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zVfUW-000EBPC; Tue, 20 Oct 98 19:22 MET DST
Message-Id: <m0zVfUW-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Tue, 20 Oct 1998 19:22:40 +0200 (MET DST)
Cc: jwieck@debis.com, Inoue@tpf.co.jp, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810201702.NAA08286@candle.pha.pa.us> from "Bruce Momjian" at Oct 20, 98 01:02:58 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Status: RO

> 
> > >
> > > I agree.  Another good thing is that the LIMIT thing will not require a
> > > dump/reload, so it is a good candidate for a minor release.
> > 
> >     That's wrong, sorry.
> > 
> >     The  limit  thing as I implemented it adds 2 new variables to
> >     the Query structure. Rewrite rules are stored  as  querytrees
> >     and in the existing pg_rewrite entries that would be missing.
> 
> Oh, sorry.  I forgot.  That could be tough.

    But it wouldn't hurt to add them now to have them in
    place. The required out-, read- and copyfuncs are in
    my patch too. This  would prevent  dump/load when we
    later add the real LIMIT functionality. And  it does
    not change anything now.


Jan

-- 

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #


From owner-pgsql-hackers@hub.org Tue Oct 20 14:57:36 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id OAA11449
	for <maillist@candle.pha.pa.us>; Tue, 20 Oct 1998 14:57:34 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id NAA03547;
	Tue, 20 Oct 1998 13:10:38 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Tue, 20 Oct 1998 13:10:23 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id NAA03488
	for pgsql-hackers-outgoing; Tue, 20 Oct 1998 13:10:21 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.8.8/8.8.8) with ESMTP id NAA03455
	for <hackers@postgreSQL.org>; Tue, 20 Oct 1998 13:10:10 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id TAA17171; Tue, 20 Oct 1998 19:12:30 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma017064; Tue, 20 Oct 98 19:12:00 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id TAA24806;
	Tue, 20 Oct 1998 19:09:37 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id TAA06212;
	Tue, 20 Oct 1998 19:12:01 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zVcwS-000B5AC; Tue, 20 Oct 98 16:39 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zVfUW-000EBPC; Tue, 20 Oct 98 19:22 MET DST
Message-Id: <m0zVfUW-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Tue, 20 Oct 1998 19:22:40 +0200 (MET DST)
Cc: jwieck@debis.com, Inoue@tpf.co.jp, hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810201702.NAA08286@candle.pha.pa.us> from "Bruce Momjian" at Oct 20, 98 01:02:58 pm
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

> 
> > >
> > > I agree.  Another good thing is that the LIMIT thing will not require a
> > > dump/reload, so it is a good candidate for a minor release.
> > 
> >     That's wrong, sorry.
> > 
> >     The  limit  thing as I implemented it adds 2 new variables to
> >     the Query structure. Rewrite rules are stored  as  querytrees
> >     and in the existing pg_rewrite entries that would be missing.
> 
> Oh, sorry.  I forgot.  That could be tough.

    But it wouldn't hurt to add them now to have them in
    place. The required out-, read- and copyfuncs are in
    my patch too. This  would prevent  dump/load when we
    later add the real LIMIT functionality. And  it does
    not change anything now.


Jan

-- 

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #



From owner-pgsql-hackers@hub.org Wed Oct 21 02:35:54 1998
Received: from hub.org (majordom@hub.org [209.47.148.200])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id CAA29494
	for <maillist@candle.pha.pa.us>; Wed, 21 Oct 1998 02:35:53 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.8.8/8.8.8) with SMTP id CAA13326;
	Wed, 21 Oct 1998 02:10:42 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Wed, 21 Oct 1998 02:09:35 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.8.8/8.8.8) id CAA12900
	for pgsql-hackers-outgoing; Wed, 21 Oct 1998 02:09:33 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from candle.pha.pa.us (maillist@s5-03.ppp.op.net [209.152.195.67])
	by hub.org (8.8.8/8.8.8) with ESMTP id CAA12871
	for <hackers@postgreSQL.org>; Wed, 21 Oct 1998 02:09:26 -0400 (EDT)
	(envelope-from maillist@candle.pha.pa.us)
Received: (from maillist@localhost)
	by candle.pha.pa.us (8.9.0/8.9.0) id CAA27774;
	Wed, 21 Oct 1998 02:09:27 -0400 (EDT)
From: Bruce Momjian <maillist@candle.pha.pa.us>
Message-Id: <199810210609.CAA27774@candle.pha.pa.us>
Subject: Re: [HACKERS] What about LIMIT in SELECT ?
In-Reply-To: <m0zVfUW-000EBPC@orion.SAPserv.Hamburg.dsh.de> from Jan Wieck at "Oct 20, 1998  7:22:40 pm"
To: jwieck@debis.com
Date: Wed, 21 Oct 1998 02:09:26 -0400 (EDT)
Cc: jwieck@debis.com, Inoue@tpf.co.jp, hackers@postgreSQL.org
X-Mailer: ELM [version 2.4ME+ PL47 (25)]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

> > 
> > > >
> > > > I agree.  Another good thing is that the LIMIT thing will not require a
> > > > dump/reload, so it is a good candidate for a minor release.
> > > 
> > >     That's wrong, sorry.
> > > 
> > >     The  limit  thing as I implemented it adds 2 new variables to
> > >     the Query structure. Rewrite rules are stored  as  querytrees
> > >     and in the existing pg_rewrite entries that would be missing.
> > 
> > Oh, sorry.  I forgot.  That could be tough.
> 
>     But it wouldn't hurt to add them now to have them in
>     place. The required out-, read- and copyfuncs are in
>     my patch too. This  would prevent  dump/load when we
>     later add the real LIMIT functionality. And  it does
>     not change anything now.
> 

Jan, we found that I am having to require an initdb for the INET/CIDR
type, so if you want stuff to change the views/rules for the limit
addition post 6.4, please send them in and I will apply them.

You clearly have the syntax down, so I think you should go ahead.


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026


From wieck@sapserv.debis.de Thu Oct 22 10:20:58 1998
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id KAA20566
	for <maillist@candle.pha.pa.us>; Thu, 22 Oct 1998 10:20:54 -0400 (EDT)
Received: by dsh.de; id QAA09067; Thu, 22 Oct 1998 16:23:14 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma008719; Thu, 22 Oct 98 16:22:40 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id QAA01558;
	Thu, 22 Oct 1998 16:19:55 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id QAA18978;
	Thu, 22 Oct 1998 16:22:20 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zWJG2-000B5AC; Thu, 22 Oct 98 13:50 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zWLoE-000EBPC; Thu, 22 Oct 98 16:33 MET DST
Message-Id: <m0zWLoE-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] psql's help (the LIMIT stuff)
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Thu, 22 Oct 1998 16:33:50 +0200 (MET DST)
Cc: jwieck@debis.com, jose@sferacarta.com, pgsql-hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810221351.JAA19663@candle.pha.pa.us> from "Bruce Momjian" at Oct 22, 98 09:51:19 am
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Status: ROr

> >
> >     I hope the QUERY_LIMIT too.
>
> I still have that cnfify() possible fix to review for KQSO.  Are you
> still thinking limit for 6.4 final, or a minor release after that?

    I  posted the part that is the minimum applied to 6.4 to make
    adding LIMIT later  non-initdb  earlier.  Anyway,  here  it's
    again.

    My  LIMIT  implementation  that  does  it like the SET in the
    toplevel executor (but via parsetree  values)  is  ready  for
    production.  I  only  held  it back because it's feature, not
    bugfix.

    Do you want it in 6.4 final?


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #


diff -cr src.orig/backend/nodes/copyfuncs.c src/backend/nodes/copyfuncs.c
*** src.orig/backend/nodes/copyfuncs.c	Fri Oct 16 11:53:40 1998
--- src/backend/nodes/copyfuncs.c	Fri Oct 16 13:32:35 1998
***************
*** 1578,1583 ****
--- 1578,1586 ----
  		newnode->unionClause = temp_list;
  	}
  
+ 	Node_Copy(from, newnode, limitOffset);
+ 	Node_Copy(from, newnode, limitCount);
+ 
  	return newnode;
  }
  
diff -cr src.orig/backend/nodes/outfuncs.c src/backend/nodes/outfuncs.c
*** src.orig/backend/nodes/outfuncs.c	Fri Oct 16 11:53:40 1998
--- src/backend/nodes/outfuncs.c	Fri Oct 16 13:30:50 1998
***************
*** 259,264 ****
--- 259,268 ----
  	appendStringInfo(str, (node->hasSubLinks ? "true" : "false"));
  	appendStringInfo(str, " :unionClause ");
  	_outNode(str, node->unionClause);
+ 	appendStringInfo(str, " :limitOffset ");
+ 	_outNode(str, node->limitOffset);
+ 	appendStringInfo(str, " :limitCount ");
+ 	_outNode(str, node->limitCount);
  }
  
  static void
diff -cr src.orig/backend/nodes/readfuncs.c src/backend/nodes/readfuncs.c
*** src.orig/backend/nodes/readfuncs.c	Fri Oct 16 11:53:40 1998
--- src/backend/nodes/readfuncs.c	Fri Oct 16 13:31:43 1998
***************
*** 163,168 ****
--- 163,174 ----
  	token = lsptok(NULL, &length);		/* skip :unionClause */
  	local_node->unionClause = nodeRead(true);
  
+ 	token = lsptok(NULL, &length);		/* skip :limitOffset */
+ 	local_node->limitOffset = nodeRead(true);
+ 
+ 	token = lsptok(NULL, &length);		/* skip :limitCount */
+ 	local_node->limitCount = nodeRead(true);
+ 
  	return local_node;
  }
  
diff -cr src.orig/include/nodes/parsenodes.h src/include/nodes/parsenodes.h
*** src.orig/include/nodes/parsenodes.h	Fri Oct 16 11:53:58 1998
--- src/include/nodes/parsenodes.h	Fri Oct 16 13:35:32 1998
***************
*** 60,65 ****
--- 60,67 ----
  
  	List	   *unionClause;	/* unions are linked under the previous
  								 * query */
+ 	Node	   *limitOffset;	/* # of result tuples to skip */
+ 	Node	   *limitCount;		/* # of result tuples to return */
  
  	/* internal to planner */
  	List	   *base_rel_list;	/* base relation list */
***************
*** 639,644 ****
--- 641,648 ----
  	char	   *portalname;		/* the portal (cursor) to create */
  	bool		binary;			/* a binary (internal) portal? */
  	bool		unionall;		/* union without unique sort */
+ 	Node	   *limitOffset;	/* # of result tuples to skip */
+ 	Node	   *limitCount;		/* # of result tuples to return */
  } SelectStmt;
  
  

From owner-pgsql-hackers@hub.org Thu Oct 22 11:33:41 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id LAA01724
	for <maillist@candle.pha.pa.us>; Thu, 22 Oct 1998 11:33:31 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id LAA12702 for <maillist@candle.pha.pa.us>; Thu, 22 Oct 1998 11:25:02 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.9.1/8.8.8) with SMTP id KAA11023;
	Thu, 22 Oct 1998 10:22:13 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Thu, 22 Oct 1998 10:21:07 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.9.1/8.8.8) id KAA10873
	for pgsql-hackers-outgoing; Thu, 22 Oct 1998 10:21:05 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.9.1/8.8.8) with ESMTP id KAA10847
	for <pgsql-hackers@postgreSQL.org>; Thu, 22 Oct 1998 10:21:00 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id QAA09067; Thu, 22 Oct 1998 16:23:14 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma008719; Thu, 22 Oct 98 16:22:40 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id QAA01558;
	Thu, 22 Oct 1998 16:19:55 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id QAA18978;
	Thu, 22 Oct 1998 16:22:20 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zWJG2-000B5AC; Thu, 22 Oct 98 13:50 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zWLoE-000EBPC; Thu, 22 Oct 98 16:33 MET DST
Message-Id: <m0zWLoE-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] psql's help (the LIMIT stuff)
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Thu, 22 Oct 1998 16:33:50 +0200 (MET DST)
Cc: jwieck@debis.com, jose@sferacarta.com, pgsql-hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810221351.JAA19663@candle.pha.pa.us> from "Bruce Momjian" at Oct 22, 98 09:51:19 am
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: RO

> >
> >     I hope the QUERY_LIMIT too.
>
> I still have that cnfify() possible fix to review for KQSO.  Are you
> still thinking limit for 6.4 final, or a minor release after that?

    I  posted the part that is the minimum applied to 6.4 to make
    adding LIMIT later  non-initdb  earlier.  Anyway,  here  it's
    again.

    My  LIMIT  implementation  that  does  it like the SET in the
    toplevel executor (but via parsetree  values)  is  ready  for
    production.  I  only  held  it back because it's feature, not
    bugfix.

    Do you want it in 6.4 final?


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #


diff -cr src.orig/backend/nodes/copyfuncs.c src/backend/nodes/copyfuncs.c
*** src.orig/backend/nodes/copyfuncs.c	Fri Oct 16 11:53:40 1998
--- src/backend/nodes/copyfuncs.c	Fri Oct 16 13:32:35 1998
***************
*** 1578,1583 ****
--- 1578,1586 ----
  		newnode->unionClause = temp_list;
  	}
  
+ 	Node_Copy(from, newnode, limitOffset);
+ 	Node_Copy(from, newnode, limitCount);
+ 
  	return newnode;
  }
  
diff -cr src.orig/backend/nodes/outfuncs.c src/backend/nodes/outfuncs.c
*** src.orig/backend/nodes/outfuncs.c	Fri Oct 16 11:53:40 1998
--- src/backend/nodes/outfuncs.c	Fri Oct 16 13:30:50 1998
***************
*** 259,264 ****
--- 259,268 ----
  	appendStringInfo(str, (node->hasSubLinks ? "true" : "false"));
  	appendStringInfo(str, " :unionClause ");
  	_outNode(str, node->unionClause);
+ 	appendStringInfo(str, " :limitOffset ");
+ 	_outNode(str, node->limitOffset);
+ 	appendStringInfo(str, " :limitCount ");
+ 	_outNode(str, node->limitCount);
  }
  
  static void
diff -cr src.orig/backend/nodes/readfuncs.c src/backend/nodes/readfuncs.c
*** src.orig/backend/nodes/readfuncs.c	Fri Oct 16 11:53:40 1998
--- src/backend/nodes/readfuncs.c	Fri Oct 16 13:31:43 1998
***************
*** 163,168 ****
--- 163,174 ----
  	token = lsptok(NULL, &length);		/* skip :unionClause */
  	local_node->unionClause = nodeRead(true);
  
+ 	token = lsptok(NULL, &length);		/* skip :limitOffset */
+ 	local_node->limitOffset = nodeRead(true);
+ 
+ 	token = lsptok(NULL, &length);		/* skip :limitCount */
+ 	local_node->limitCount = nodeRead(true);
+ 
  	return local_node;
  }
  
diff -cr src.orig/include/nodes/parsenodes.h src/include/nodes/parsenodes.h
*** src.orig/include/nodes/parsenodes.h	Fri Oct 16 11:53:58 1998
--- src/include/nodes/parsenodes.h	Fri Oct 16 13:35:32 1998
***************
*** 60,65 ****
--- 60,67 ----
  
  	List	   *unionClause;	/* unions are linked under the previous
  								 * query */
+ 	Node	   *limitOffset;	/* # of result tuples to skip */
+ 	Node	   *limitCount;		/* # of result tuples to return */
  
  	/* internal to planner */
  	List	   *base_rel_list;	/* base relation list */
***************
*** 639,644 ****
--- 641,648 ----
  	char	   *portalname;		/* the portal (cursor) to create */
  	bool		binary;			/* a binary (internal) portal? */
  	bool		unionall;		/* union without unique sort */
+ 	Node	   *limitOffset;	/* # of result tuples to skip */
+ 	Node	   *limitCount;		/* # of result tuples to return */
  } SelectStmt;
  
  


From wieck@sapserv.debis.de Thu Oct 22 11:01:05 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id LAA21185
	for <maillist@candle.pha.pa.us>; Thu, 22 Oct 1998 11:01:00 -0400 (EDT)
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id KAA09646 for <maillist@candle.pha.pa.us>; Thu, 22 Oct 1998 10:44:36 -0400 (EDT)
Received: by dsh.de; id QAA19394; Thu, 22 Oct 1998 16:43:42 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma017268; Thu, 22 Oct 98 16:39:44 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id QAA02988;
	Thu, 22 Oct 1998 16:36:46 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id QAA19155;
	Thu, 22 Oct 1998 16:39:10 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zWJWL-000B5DC; Thu, 22 Oct 98 14:07 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zWM4W-000EBPC; Thu, 22 Oct 98 16:50 MET DST
Message-Id: <m0zWM4W-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] psql's help (the LIMIT stuff)
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Thu, 22 Oct 1998 16:50:40 +0200 (MET DST)
Cc: jwieck@debis.com, jose@sferacarta.com, pgsql-hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810221424.KAA20601@candle.pha.pa.us> from "Bruce Momjian" at Oct 22, 98 10:24:08 am
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Status: RO

>
> > > >
> > > >     I hope the QUERY_LIMIT too.
> > >
> > > I still have that cnfify() possible fix to review for KQSO.  Are you
> > > still thinking limit for 6.4 final, or a minor release after that?
> >
> >     I  posted the part that is the minimum applied to 6.4 to make
> >     adding LIMIT later  non-initdb  earlier.  Anyway,  here  it's
> >     again.
>
> Already applied.  I assume it is the same as the one I applied.

    Seen,  thanks.  Your 'Applied' just arrived after I packed it
    again. It's the same.

> We are close to final, and can easily put it in 6.4.1, which I am sure
> we will need, and if we split CVS trees, you'll have lots of minor
> versions to pick from.  :-)
>
> Seems like it would be a nice minor release item, but the problem is
> that minor releases aren't tested as much as major ones.  How confident
> are you in the code?  What do others thing?

    I regression tested it,  and  did  additional  tests  in  the
    SPI/PL  area.  It  works.  It only touches the parser and the
    executor. Rules, planner/optimizer just bypass the values  in
    the  parsetree.  The  parser  and  the  executor are parts of
    Postgres I feel very familiar with (not so in the optimizer).
    I  trust  in  the  code  and  would  use  it  in a production
    environment.

    It's below.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #


diff -cr src.orig/backend/commands/command.c src/backend/commands/command.c
*** src.orig/backend/commands/command.c	Fri Oct 16 11:53:38 1998
--- src/backend/commands/command.c	Fri Oct 16 12:56:44 1998
***************
*** 39,44 ****
--- 39,45 ----
  #include "utils/mcxt.h"
  #include "utils/portal.h"
  #include "utils/syscache.h"
+ #include "string.h"
  
  /* ----------------
   *		PortalExecutorHeapMemory stuff
***************
*** 101,106 ****
--- 102,108 ----
  	int			feature;
  	QueryDesc  *queryDesc;
  	MemoryContext context;
+ 	Const		limcount;
  
  	/* ----------------
  	 *	sanity checks
***************
*** 113,118 ****
--- 115,134 ----
  	}
  
  	/* ----------------
+ 	 *	Create a const node from the given count value
+ 	 * ----------------
+ 	 */
+ 	memset(&limcount, 0, sizeof(limcount));
+ 	limcount.type		= T_Const;
+ 	limcount.consttype	= INT4OID;
+ 	limcount.constlen	= sizeof(int4);
+ 	limcount.constvalue	= (Datum)count;
+ 	limcount.constisnull	= FALSE;
+ 	limcount.constbyval	= TRUE;
+ 	limcount.constisset	= FALSE;
+ 	limcount.constiscast	= FALSE;
+ 
+ 	/* ----------------
  	 *	get the portal from the portal name
  	 * ----------------
  	 */
***************
*** 176,182 ****
  	PortalExecutorHeapMemory = (MemoryContext)
  		PortalGetHeapMemory(portal);
  
! 	ExecutorRun(queryDesc, PortalGetState(portal), feature, count);
  
  	if (dest == None)			/* MOVE */
  		pfree(queryDesc);
--- 192,198 ----
  	PortalExecutorHeapMemory = (MemoryContext)
  		PortalGetHeapMemory(portal);
  
! 	ExecutorRun(queryDesc, PortalGetState(portal), feature, (Node *)NULL, (Node *)&limcount);
  
  	if (dest == None)			/* MOVE */
  		pfree(queryDesc);
diff -cr src.orig/backend/executor/execMain.c src/backend/executor/execMain.c
*** src.orig/backend/executor/execMain.c	Fri Oct 16 11:53:38 1998
--- src/backend/executor/execMain.c	Fri Oct 16 20:05:19 1998
***************
*** 64,69 ****
--- 64,70 ----
  static void EndPlan(Plan *plan, EState *estate);
  static TupleTableSlot *ExecutePlan(EState *estate, Plan *plan,
  			Query *parseTree, CmdType operation,
+ 			int offsetTuples,
  			int numberTuples, ScanDirection direction,
  			void (*printfunc) ());
  static void ExecRetrieve(TupleTableSlot *slot, void (*printfunc) (),
***************
*** 163,169 ****
   * ----------------------------------------------------------------
   */
  TupleTableSlot *
! ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, int count)
  {
  	CmdType		operation;
  	Query	   *parseTree;
--- 164,170 ----
   * ----------------------------------------------------------------
   */
  TupleTableSlot *
! ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, Node *limoffset, Node *limcount)
  {
  	CmdType		operation;
  	Query	   *parseTree;
***************
*** 171,176 ****
--- 172,179 ----
  	TupleTableSlot *result;
  	CommandDest dest;
  	void		(*destination) ();
+ 	int		offset = 0;
+ 	int		count = 0;
  
  	/******************
  	 *	sanity checks
***************
*** 191,196 ****
--- 194,289 ----
  	estate->es_processed = 0;
  	estate->es_lastoid = InvalidOid;
  
+ 	/******************
+ 	 *	if given get the offset of the LIMIT clause
+ 	 ******************
+ 	 */
+ 	if (limoffset != NULL)
+ 	{
+ 		Const		*coffset;
+ 		Param		*poffset;
+ 		ParamListInfo	paramLI;
+ 		int		i;
+ 
+ 		switch (nodeTag(limoffset))
+ 		{
+ 			case T_Const:
+ 				coffset = (Const *)limoffset;
+ 				offset = (int)(coffset->constvalue);
+ 				break;
+ 
+ 			case T_Param:
+ 				poffset = (Param *)limoffset;
+ 				paramLI = estate->es_param_list_info;
+ 
+ 				if (paramLI == NULL)
+ 					elog(ERROR, "parameter for limit offset not in executor state");
+ 				for (i = 0; paramLI[i].kind != PARAM_INVALID; i++)
+ 				{
+ 					if (paramLI[i].kind == PARAM_NUM && paramLI[i].id == poffset->paramid)
+ 						break;
+ 				}
+ 				if (paramLI[i].kind == PARAM_INVALID)
+ 					elog(ERROR, "parameter for limit offset not in executor state");
+ 				if (paramLI[i].isnull)
+ 					elog(ERROR, "limit offset cannot be NULL value");
+ 				offset = (int)(paramLI[i].value);
+ 
+ 				break;
+ 
+ 			default:
+ 				elog(ERROR, "unexpected node type %d as limit offset", nodeTag(limoffset));
+ 		}
+ 
+ 		if (offset < 0)
+ 			elog(ERROR, "limit offset cannot be negative");
+ 	}
+ 
+ 	/******************
+ 	 *	if given get the count of the LIMIT clause
+ 	 ******************
+ 	 */
+ 	if (limcount != NULL)
+ 	{
+ 		Const		*ccount;
+ 		Param		*pcount;
+ 		ParamListInfo	paramLI;
+ 		int		i;
+ 
+ 		switch (nodeTag(limcount))
+ 		{
+ 			case T_Const:
+ 				ccount = (Const *)limcount;
+ 				count = (int)(ccount->constvalue);
+ 				break;
+ 
+ 			case T_Param:
+ 				pcount = (Param *)limcount;
+ 				paramLI = estate->es_param_list_info;
+ 
+ 				if (paramLI == NULL)
+ 					elog(ERROR, "parameter for limit count not in executor state");
+ 				for (i = 0; paramLI[i].kind != PARAM_INVALID; i++)
+ 				{
+ 					if (paramLI[i].kind == PARAM_NUM && paramLI[i].id == pcount->paramid)
+ 						break;
+ 				}
+ 				if (paramLI[i].kind == PARAM_INVALID)
+ 					elog(ERROR, "parameter for limit count not in executor state");
+ 				if (paramLI[i].isnull)
+ 					elog(ERROR, "limit count cannot be NULL value");
+ 				count = (int)(paramLI[i].value);
+ 
+ 				break;
+ 
+ 			default:
+ 				elog(ERROR, "unexpected node type %d as limit count", nodeTag(limcount));
+ 		}
+ 
+ 		if (count < 0)
+ 			elog(ERROR, "limit count cannot be negative");
+ 	}
+ 
  	switch (feature)
  	{
  
***************
*** 199,205 ****
  								 plan,
  								 parseTree,
  								 operation,
! 								 ALL_TUPLES,
  								 ForwardScanDirection,
  								 destination);
  			break;
--- 292,299 ----
  								 plan,
  								 parseTree,
  								 operation,
! 								 offset,
! 								 count,
  								 ForwardScanDirection,
  								 destination);
  			break;
***************
*** 208,213 ****
--- 302,308 ----
  								 plan,
  								 parseTree,
  								 operation,
+ 								 offset,
  								 count,
  								 ForwardScanDirection,
  								 destination);
***************
*** 222,227 ****
--- 317,323 ----
  								 plan,
  								 parseTree,
  								 operation,
+ 								 offset,
  								 count,
  								 BackwardScanDirection,
  								 destination);
***************
*** 237,242 ****
--- 333,339 ----
  								 plan,
  								 parseTree,
  								 operation,
+ 								 0,
  								 ONE_TUPLE,
  								 ForwardScanDirection,
  								 destination);
***************
*** 691,696 ****
--- 788,794 ----
  			Plan *plan,
  			Query *parseTree,
  			CmdType operation,
+ 			int offsetTuples,
  			int numberTuples,
  			ScanDirection direction,
  			void (*printfunc) ())
***************
*** 742,747 ****
--- 840,859 ----
  		{
  			result = NULL;
  			break;
+ 		}
+ 
+ 		/******************
+ 		 *	For now we completely execute the plan and skip
+ 		 *	result tuples if requested by LIMIT offset.
+ 		 *	Finally we should try to do it in deeper levels
+ 		 *	if possible (during index scan)
+ 		 *	- Jan
+ 		 ******************
+ 		 */
+ 		if (offsetTuples > 0)
+ 		{
+ 			--offsetTuples;
+ 			continue;
  		}
  
  		/******************
diff -cr src.orig/backend/executor/functions.c src/backend/executor/functions.c
*** src.orig/backend/executor/functions.c	Fri Oct 16 11:53:38 1998
--- src/backend/executor/functions.c	Fri Oct 16 19:01:02 1998
***************
*** 130,135 ****
--- 130,138 ----
  									 None);
  		estate = CreateExecutorState();
  
+ 		if (queryTree->limitOffset != NULL || queryTree->limitCount != NULL)
+ 			elog(ERROR, "LIMIT clause from SQL functions not yet implemented");
+ 
  		if (nargs > 0)
  		{
  			int			i;
***************
*** 200,206 ****
  
  	feature = (LAST_POSTQUEL_COMMAND(es)) ? EXEC_RETONE : EXEC_RUN;
  
! 	return ExecutorRun(es->qd, es->estate, feature, 0);
  }
  
  static void
--- 203,209 ----
  
  	feature = (LAST_POSTQUEL_COMMAND(es)) ? EXEC_RETONE : EXEC_RUN;
  
! 	return ExecutorRun(es->qd, es->estate, feature, (Node *)NULL, (Node *)NULL);
  }
  
  static void
diff -cr src.orig/backend/executor/spi.c src/backend/executor/spi.c
*** src.orig/backend/executor/spi.c	Fri Oct 16 11:53:39 1998
--- src/backend/executor/spi.c	Fri Oct 16 19:25:33 1998
***************
*** 791,796 ****
--- 791,798 ----
  	bool		isRetrieveIntoRelation = false;
  	char	   *intoName = NULL;
  	int			res;
+ 	Const		tcount_const;
+ 	Node		*count = NULL;
  
  	switch (operation)
  	{
***************
*** 825,830 ****
--- 827,865 ----
  			return SPI_ERROR_OPUNKNOWN;
  	}
  
+ 	/* ----------------
+ 	 *	Get the query LIMIT tuple count
+ 	 * ----------------
+ 	 */
+ 	if (parseTree->limitCount != NULL)
+ 	{
+ 		/* ----------------
+ 		 *      A limit clause in the parsetree overrides the
+ 		 *	tcount parameter
+ 		 * ----------------
+ 		 */
+ 		count = parseTree->limitCount;
+ 	}
+ 	else
+ 	{
+ 		/* ----------------
+ 		 *      No LIMIT clause in parsetree. Use a local Const node
+ 		 *	to put tcount into it
+ 		 * ----------------
+ 		 */
+ 		memset(&tcount_const, 0, sizeof(tcount_const));
+ 		tcount_const.type           = T_Const;
+ 		tcount_const.consttype      = INT4OID;
+ 		tcount_const.constlen       = sizeof(int4);
+ 		tcount_const.constvalue     = (Datum)tcount;
+ 		tcount_const.constisnull    = FALSE;
+ 		tcount_const.constbyval     = TRUE;
+ 		tcount_const.constisset     = FALSE;
+ 		tcount_const.constiscast    = FALSE;
+ 
+ 		count = (Node *)&tcount_const;
+ 	}
+ 
  	if (state == NULL)			/* plan preparation */
  		return res;
  #ifdef SPI_EXECUTOR_STATS
***************
*** 845,851 ****
  		return SPI_OK_CURSOR;
  	}
  
! 	ExecutorRun(queryDesc, state, EXEC_FOR, tcount);
  
  	_SPI_current->processed = state->es_processed;
  	if (operation == CMD_SELECT && queryDesc->dest == SPI)
--- 880,886 ----
  		return SPI_OK_CURSOR;
  	}
  
! 	ExecutorRun(queryDesc, state, EXEC_FOR, parseTree->limitOffset, count);
  
  	_SPI_current->processed = state->es_processed;
  	if (operation == CMD_SELECT && queryDesc->dest == SPI)
diff -cr src.orig/backend/parser/analyze.c src/backend/parser/analyze.c
*** src.orig/backend/parser/analyze.c	Fri Oct 16 11:53:41 1998
--- src/backend/parser/analyze.c	Fri Oct 16 13:29:27 1998
***************
*** 180,186 ****
--- 180,190 ----
  
  		case T_SelectStmt:
  			if (!((SelectStmt *) parseTree)->portalname)
+ 			{
  				result = transformSelectStmt(pstate, (SelectStmt *) parseTree);
+ 				result->limitOffset = ((SelectStmt *)parseTree)->limitOffset;
+ 				result->limitCount = ((SelectStmt *)parseTree)->limitCount;
+ 			}
  			else
  				result = transformCursorStmt(pstate, (SelectStmt *) parseTree);
  			break;
diff -cr src.orig/backend/parser/gram.y src/backend/parser/gram.y
*** src.orig/backend/parser/gram.y	Fri Oct 16 11:53:42 1998
--- src/backend/parser/gram.y	Sun Oct 18 22:20:36 1998
***************
*** 45,50 ****
--- 45,51 ----
  #include "catalog/catname.h"
  #include "utils/elog.h"
  #include "access/xact.h"
+ #include "catalog/pg_type.h"
  
  #ifdef MULTIBYTE
  #include "mb/pg_wchar.h"
***************
*** 163,169 ****
  		sort_clause, sortby_list, index_params, index_list, name_list,
  		from_clause, from_list, opt_array_bounds, nest_array_bounds,
  		expr_list, attrs, res_target_list, res_target_list2,
! 		def_list, opt_indirection, group_clause, TriggerFuncArgs
  
  %type <node>	func_return
  %type <boolean>	set_opt
--- 164,171 ----
  		sort_clause, sortby_list, index_params, index_list, name_list,
  		from_clause, from_list, opt_array_bounds, nest_array_bounds,
  		expr_list, attrs, res_target_list, res_target_list2,
! 		def_list, opt_indirection, group_clause, TriggerFuncArgs,
! 		opt_select_limit
  
  %type <node>	func_return
  %type <boolean>	set_opt
***************
*** 192,197 ****
--- 194,201 ----
  
  %type <ival>	fetch_how_many
  
+ %type <node>	select_limit_value select_offset_value
+ 
  %type <list>	OptSeqList
  %type <defelt>	OptSeqElem
  
***************
*** 267,273 ****
  		FALSE_P, FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
  		GRANT, GROUP, HAVING, HOUR_P,
  		IN, INNER_P, INSENSITIVE, INSERT, INTERVAL, INTO, IS,
! 		JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LOCAL,
  		MATCH, MINUTE_P, MONTH_P, NAMES,
  		NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULL_P, NUMERIC,
  		OF, ON, ONLY, OPTION, OR, ORDER, OUTER_P,
--- 271,277 ----
  		FALSE_P, FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
  		GRANT, GROUP, HAVING, HOUR_P,
  		IN, INNER_P, INSENSITIVE, INSERT, INTERVAL, INTO, IS,
! 		JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LIMIT, LOCAL,
  		MATCH, MINUTE_P, MONTH_P, NAMES,
  		NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULL_P, NUMERIC,
  		OF, ON, ONLY, OPTION, OR, ORDER, OUTER_P,
***************
*** 299,305 ****
  		INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
  		LANCOMPILER, LISTEN, LOAD, LOCATION, LOCK_P, MAXVALUE, MINVALUE, MOVE,
  		NEW, NOCREATEDB, NOCREATEUSER, NONE, NOTHING, NOTIFY, NOTNULL,
! 		OIDS, OPERATOR, PASSWORD, PROCEDURAL,
  		RECIPE, RENAME, RESET, RETURNS, ROW, RULE,
  		SEQUENCE, SERIAL, SETOF, SHOW, START, STATEMENT, STDIN, STDOUT, TRUSTED, 
  		UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
--- 303,309 ----
  		INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
  		LANCOMPILER, LISTEN, LOAD, LOCATION, LOCK_P, MAXVALUE, MINVALUE, MOVE,
  		NEW, NOCREATEDB, NOCREATEUSER, NONE, NOTHING, NOTIFY, NOTNULL,
! 		OFFSET, OIDS, OPERATOR, PASSWORD, PROCEDURAL,
  		RECIPE, RENAME, RESET, RETURNS, ROW, RULE,
  		SEQUENCE, SERIAL, SETOF, SHOW, START, STATEMENT, STDIN, STDOUT, TRUSTED, 
  		UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
***************
*** 2591,2596 ****
--- 2595,2601 ----
  			 result from_clause where_clause
  			 group_clause having_clause
  			 union_clause sort_clause
+ 			 opt_select_limit
  				{
  					SelectStmt *n = makeNode(SelectStmt);
  					n->unique = $2;
***************
*** 2602,2607 ****
--- 2607,2622 ----
  					n->havingClause = $8;
  					n->unionClause = $9;
  					n->sortClause = $10;
+ 					if ($11 != NIL)
+ 					{
+ 						n->limitOffset = nth(0, $11);
+ 						n->limitCount = nth(1, $11);
+ 					}
+ 					else
+ 					{
+ 						n->limitOffset = NULL;
+ 						n->limitCount = NULL;
+ 					}
  					$$ = (Node *)n;
  				}
  		;
***************
*** 2699,2704 ****
--- 2714,2794 ----
  		| ASC									{ $$ = "<"; }
  		| DESC									{ $$ = ">"; }
  		| /*EMPTY*/								{ $$ = "<"; /*default*/ }
+ 		;
+ 
+ opt_select_limit:	LIMIT select_offset_value ',' select_limit_value
+ 	{ $$ = lappend(lappend(NIL, $2), $4); }
+ 		| LIMIT select_limit_value OFFSET select_offset_value
+ 	{ $$ = lappend(lappend(NIL, $4), $2); }
+ 		| LIMIT select_limit_value
+ 	{ $$ = lappend(lappend(NIL, NULL), $2); }
+ 		| OFFSET select_offset_value LIMIT select_limit_value
+ 	{ $$ = lappend(lappend(NIL, $2), $4); }
+ 		| OFFSET select_offset_value
+ 	{ $$ = lappend(lappend(NIL, $2), NULL); }
+ 		| /* EMPTY */
+ 	{ $$ = NIL; }
+ 		;
+ 
+ select_limit_value:	Iconst
+ 			{
+ 				Const	*n = makeNode(Const);
+ 
+ 				if ($1 < 1)
+ 					elog(ERROR, "selection limit must be ALL or a positive integer value > 0");
+ 
+ 				n->consttype	= INT4OID;
+ 				n->constlen	= sizeof(int4);
+ 				n->constvalue	= (Datum)$1;
+ 				n->constisnull	= FALSE;
+ 				n->constbyval	= TRUE;
+ 				n->constisset	= FALSE;
+ 				n->constiscast	= FALSE;
+ 				$$ = (Node *)n;
+ 			}
+ 		| ALL
+ 			{
+ 				Const	*n = makeNode(Const);
+ 				n->consttype	= INT4OID;
+ 				n->constlen	= sizeof(int4);
+ 				n->constvalue	= (Datum)0;
+ 				n->constisnull	= FALSE;
+ 				n->constbyval	= TRUE;
+ 				n->constisset	= FALSE;
+ 				n->constiscast	= FALSE;
+ 				$$ = (Node *)n;
+ 			}
+ 		| PARAM
+ 			{
+ 				Param	*n = makeNode(Param);
+ 				n->paramkind = PARAM_NUM;
+ 				n->paramid = $1;
+ 				n->paramtype = INT4OID;
+ 				$$ = (Node *)n;
+ 			}
+ 		;
+ 
+ select_offset_value:	Iconst
+ 			{
+ 				Const	*n = makeNode(Const);
+ 
+ 				n->consttype	= INT4OID;
+ 				n->constlen	= sizeof(int4);
+ 				n->constvalue	= (Datum)$1;
+ 				n->constisnull	= FALSE;
+ 				n->constbyval	= TRUE;
+ 				n->constisset	= FALSE;
+ 				n->constiscast	= FALSE;
+ 				$$ = (Node *)n;
+ 			}
+ 		| PARAM
+ 			{
+ 				Param	*n = makeNode(Param);
+ 				n->paramkind = PARAM_NUM;
+ 				n->paramid = $1;
+ 				n->paramtype = INT4OID;
+ 				$$ = (Node *)n;
+ 			}
  		;
  
  /*
diff -cr src.orig/backend/parser/keywords.c src/backend/parser/keywords.c
*** src.orig/backend/parser/keywords.c	Fri Oct 16 11:53:42 1998
--- src/backend/parser/keywords.c	Sun Oct 18 22:13:29 1998
***************
*** 128,133 ****
--- 128,134 ----
  	{"leading", LEADING},
  	{"left", LEFT},
  	{"like", LIKE},
+ 	{"limit", LIMIT},
  	{"listen", LISTEN},
  	{"load", LOAD},
  	{"local", LOCAL},
***************
*** 156,161 ****
--- 157,163 ----
  	{"null", NULL_P},
  	{"numeric", NUMERIC},
  	{"of", OF},
+ 	{"offset", OFFSET},
  	{"oids", OIDS},
  	{"old", CURRENT},
  	{"on", ON},
diff -cr src.orig/backend/rewrite/rewriteDefine.c src/backend/rewrite/rewriteDefine.c
*** src.orig/backend/rewrite/rewriteDefine.c	Fri Oct 16 11:53:46 1998
--- src/backend/rewrite/rewriteDefine.c	Fri Oct 16 13:48:55 1998
***************
*** 312,317 ****
--- 312,323 ----
  		heap_close(event_relation);
  
  		/*
+ 		 * LIMIT in view is not supported
+ 		 */
+ 		if (query->limitOffset != NULL || query->limitCount != NULL)
+ 			elog(ERROR, "LIMIT clause not supported in views");
+ 
+ 		/*
  		 * ... and finally the rule must be named _RETviewname.
  		 */
  		sprintf(expected_name, "_RET%s", event_obj->relname);
diff -cr src.orig/backend/tcop/pquery.c src/backend/tcop/pquery.c
*** src.orig/backend/tcop/pquery.c	Fri Oct 16 11:53:47 1998
--- src/backend/tcop/pquery.c	Fri Oct 16 14:02:36 1998
***************
*** 40,46 ****
  #include "commands/command.h"
  
  static char *CreateOperationTag(int operationType);
! static void ProcessQueryDesc(QueryDesc *queryDesc);
  
  
  /* ----------------------------------------------------------------
--- 40,46 ----
  #include "commands/command.h"
  
  static char *CreateOperationTag(int operationType);
! static void ProcessQueryDesc(QueryDesc *queryDesc, Node *limoffset, Node *limcount);
  
  
  /* ----------------------------------------------------------------
***************
*** 205,211 ****
   * ----------------------------------------------------------------
   */
  static void
! ProcessQueryDesc(QueryDesc *queryDesc)
  {
  	Query	   *parseTree;
  	Plan	   *plan;
--- 205,211 ----
   * ----------------------------------------------------------------
   */
  static void
! ProcessQueryDesc(QueryDesc *queryDesc, Node *limoffset, Node *limcount)
  {
  	Query	   *parseTree;
  	Plan	   *plan;
***************
*** 330,336 ****
  	 *	 actually run the plan..
  	 * ----------------
  	 */
! 	ExecutorRun(queryDesc, state, EXEC_RUN, 0);
  
  	/* save infos for EndCommand */
  	UpdateCommandInfo(operation, state->es_lastoid, state->es_processed);
--- 330,336 ----
  	 *	 actually run the plan..
  	 * ----------------
  	 */
! 	ExecutorRun(queryDesc, state, EXEC_RUN, limoffset, limcount);
  
  	/* save infos for EndCommand */
  	UpdateCommandInfo(operation, state->es_lastoid, state->es_processed);
***************
*** 373,377 ****
  		print_plan(plan, parsetree);
  	}
  	else
! 		ProcessQueryDesc(queryDesc);
  }
--- 373,377 ----
  		print_plan(plan, parsetree);
  	}
  	else
! 		ProcessQueryDesc(queryDesc, parsetree->limitOffset, parsetree->limitCount);
  }
diff -cr src.orig/include/executor/executor.h src/include/executor/executor.h
*** src.orig/include/executor/executor.h	Fri Oct 16 11:53:56 1998
--- src/include/executor/executor.h	Fri Oct 16 12:04:17 1998
***************
*** 83,89 ****
   * prototypes from functions in execMain.c
   */
  extern TupleDesc ExecutorStart(QueryDesc *queryDesc, EState *estate);
! extern TupleTableSlot *ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, int count);
  extern void ExecutorEnd(QueryDesc *queryDesc, EState *estate);
  extern HeapTuple ExecConstraints(char *caller, Relation rel, HeapTuple tuple);
  #ifdef QUERY_LIMIT
--- 83,89 ----
   * prototypes from functions in execMain.c
   */
  extern TupleDesc ExecutorStart(QueryDesc *queryDesc, EState *estate);
! extern TupleTableSlot *ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, Node *limoffset, Node *limcount);
  extern void ExecutorEnd(QueryDesc *queryDesc, EState *estate);
  extern HeapTuple ExecConstraints(char *caller, Relation rel, HeapTuple tuple);
  #ifdef QUERY_LIMIT

From owner-pgsql-hackers@hub.org Thu Oct 22 13:12:34 1998
Received: from renoir.op.net (root@renoir.op.net [209.152.193.4])
	by candle.pha.pa.us (8.9.0/8.9.0) with ESMTP id NAA01350
	for <maillist@candle.pha.pa.us>; Thu, 22 Oct 1998 13:12:29 -0400 (EDT)
Received: from hub.org (majordom@hub.org [209.47.148.200]) by renoir.op.net (o1/$ Revision: 1.18 $) with ESMTP id MAA17808 for <maillist@candle.pha.pa.us>; Thu, 22 Oct 1998 12:35:22 -0400 (EDT)
Received: from localhost (majordom@localhost)
	by hub.org (8.9.1/8.8.8) with SMTP id KAA14887;
	Thu, 22 Oct 1998 10:49:09 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@hub.org)
Received: by hub.org (TLB v0.10a (1.23 tibbs 1997/01/09 00:29:32)); Thu, 22 Oct 1998 10:44:59 +0000 (EDT)
Received: (from majordom@localhost)
	by hub.org (8.9.1/8.8.8) id KAA14445
	for pgsql-hackers-outgoing; Thu, 22 Oct 1998 10:44:57 -0400 (EDT)
	(envelope-from owner-pgsql-hackers@postgreSQL.org)
X-Authentication-Warning: hub.org: majordom set sender to owner-pgsql-hackers@postgreSQL.org using -f
Received: from dsh.de (firewall-user@neptun.sns-felb.debis.de [53.122.101.2])
	by hub.org (8.9.1/8.8.8) with ESMTP id KAA14431
	for <pgsql-hackers@postgreSQL.org>; Thu, 22 Oct 1998 10:44:47 -0400 (EDT)
	(envelope-from wieck@sapserv.debis.de)
Received: by dsh.de; id QAA19394; Thu, 22 Oct 1998 16:43:42 +0200 (MET DST)
Received: from dshmail.dsh.de(53.47.15.3) by neptun.dsh.de via smap (3.2)
	id xma017268; Thu, 22 Oct 98 16:39:44 +0200
Received: from mail1.hh1.dsh.de (mail1.hh1.dsh.de [53.47.9.5])
	by dshmail.dsh.de (8.8.7/8.8.7) with ESMTP id QAA02988;
	Thu, 22 Oct 1998 16:36:46 +0200 (MET DST)
Received: from mars.SAPserv.Hamburg.dsh.de (root@mail5.hh1.dsh.de [53.2.168.17])
	by mail1.hh1.dsh.de (8.8.7/8.8.7) with SMTP id QAA19155;
	Thu, 22 Oct 1998 16:39:10 +0200
Received: from orion.SAPserv.Hamburg.dsh.de 
	by mars.SAPserv.Hamburg.dsh.de with smtp 
	for <<maillist@candle.pha.pa.us>> 
	id m0zWJWL-000B5DC; Thu, 22 Oct 98 14:07 MET DST
Received: by orion.SAPserv.Hamburg.dsh.de 
	for maillist@candle.pha.pa.us 
	id m0zWM4W-000EBPC; Thu, 22 Oct 98 16:50 MET DST
Message-Id: <m0zWM4W-000EBPC@orion.SAPserv.Hamburg.dsh.de>
From: jwieck@debis.com (Jan Wieck)
Subject: Re: [HACKERS] psql's help (the LIMIT stuff)
To: maillist@candle.pha.pa.us (Bruce Momjian)
Date: Thu, 22 Oct 1998 16:50:40 +0200 (MET DST)
Cc: jwieck@debis.com, jose@sferacarta.com, pgsql-hackers@postgreSQL.org
Reply-To: jwieck@debis.com (Jan Wieck)
In-Reply-To: <199810221424.KAA20601@candle.pha.pa.us> from "Bruce Momjian" at Oct 22, 98 10:24:08 am
X-Mailer: ELM [version 2.4 PL25]
Content-Type: text
Sender: owner-pgsql-hackers@postgreSQL.org
Precedence: bulk
Status: ROr

>
> > > >
> > > >     I hope the QUERY_LIMIT too.
> > >
> > > I still have that cnfify() possible fix to review for KQSO.  Are you
> > > still thinking limit for 6.4 final, or a minor release after that?
> >
> >     I  posted the part that is the minimum applied to 6.4 to make
> >     adding LIMIT later  non-initdb  earlier.  Anyway,  here  it's
> >     again.
>
> Already applied.  I assume it is the same as the one I applied.

    Seen,  thanks.  Your 'Applied' just arrived after I packed it
    again. It's the same.

> We are close to final, and can easily put it in 6.4.1, which I am sure
> we will need, and if we split CVS trees, you'll have lots of minor
> versions to pick from.  :-)
>
> Seems like it would be a nice minor release item, but the problem is
> that minor releases aren't tested as much as major ones.  How confident
> are you in the code?  What do others thing?

    I regression tested it,  and  did  additional  tests  in  the
    SPI/PL  area.  It  works.  It only touches the parser and the
    executor. Rules, planner/optimizer just bypass the values  in
    the  parsetree.  The  parser  and  the  executor are parts of
    Postgres I feel very familiar with (not so in the optimizer).
    I  trust  in  the  code  and  would  use  it  in a production
    environment.

    It's below.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #


diff -cr src.orig/backend/commands/command.c src/backend/commands/command.c
*** src.orig/backend/commands/command.c	Fri Oct 16 11:53:38 1998
--- src/backend/commands/command.c	Fri Oct 16 12:56:44 1998
***************
*** 39,44 ****
--- 39,45 ----
  #include "utils/mcxt.h"
  #include "utils/portal.h"
  #include "utils/syscache.h"
+ #include "string.h"
  
  /* ----------------
   *		PortalExecutorHeapMemory stuff
***************
*** 101,106 ****
--- 102,108 ----
  	int			feature;
  	QueryDesc  *queryDesc;
  	MemoryContext context;
+ 	Const		limcount;
  
  	/* ----------------
  	 *	sanity checks
***************
*** 113,118 ****
--- 115,134 ----
  	}
  
  	/* ----------------
+ 	 *	Create a const node from the given count value
+ 	 * ----------------
+ 	 */
+ 	memset(&limcount, 0, sizeof(limcount));
+ 	limcount.type		= T_Const;
+ 	limcount.consttype	= INT4OID;
+ 	limcount.constlen	= sizeof(int4);
+ 	limcount.constvalue	= (Datum)count;
+ 	limcount.constisnull	= FALSE;
+ 	limcount.constbyval	= TRUE;
+ 	limcount.constisset	= FALSE;
+ 	limcount.constiscast	= FALSE;
+ 
+ 	/* ----------------
  	 *	get the portal from the portal name
  	 * ----------------
  	 */
***************
*** 176,182 ****
  	PortalExecutorHeapMemory = (MemoryContext)
  		PortalGetHeapMemory(portal);
  
! 	ExecutorRun(queryDesc, PortalGetState(portal), feature, count);
  
  	if (dest == None)			/* MOVE */
  		pfree(queryDesc);
--- 192,198 ----
  	PortalExecutorHeapMemory = (MemoryContext)
  		PortalGetHeapMemory(portal);
  
! 	ExecutorRun(queryDesc, PortalGetState(portal), feature, (Node *)NULL, (Node *)&limcount);
  
  	if (dest == None)			/* MOVE */
  		pfree(queryDesc);
diff -cr src.orig/backend/executor/execMain.c src/backend/executor/execMain.c
*** src.orig/backend/executor/execMain.c	Fri Oct 16 11:53:38 1998
--- src/backend/executor/execMain.c	Fri Oct 16 20:05:19 1998
***************
*** 64,69 ****
--- 64,70 ----
  static void EndPlan(Plan *plan, EState *estate);
  static TupleTableSlot *ExecutePlan(EState *estate, Plan *plan,
  			Query *parseTree, CmdType operation,
+ 			int offsetTuples,
  			int numberTuples, ScanDirection direction,
  			void (*printfunc) ());
  static void ExecRetrieve(TupleTableSlot *slot, void (*printfunc) (),
***************
*** 163,169 ****
   * ----------------------------------------------------------------
   */
  TupleTableSlot *
! ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, int count)
  {
  	CmdType		operation;
  	Query	   *parseTree;
--- 164,170 ----
   * ----------------------------------------------------------------
   */
  TupleTableSlot *
! ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, Node *limoffset, Node *limcount)
  {
  	CmdType		operation;
  	Query	   *parseTree;
***************
*** 171,176 ****
--- 172,179 ----
  	TupleTableSlot *result;
  	CommandDest dest;
  	void		(*destination) ();
+ 	int		offset = 0;
+ 	int		count = 0;
  
  	/******************
  	 *	sanity checks
***************
*** 191,196 ****
--- 194,289 ----
  	estate->es_processed = 0;
  	estate->es_lastoid = InvalidOid;
  
+ 	/******************
+ 	 *	if given get the offset of the LIMIT clause
+ 	 ******************
+ 	 */
+ 	if (limoffset != NULL)
+ 	{
+ 		Const		*coffset;
+ 		Param		*poffset;
+ 		ParamListInfo	paramLI;
+ 		int		i;
+ 
+ 		switch (nodeTag(limoffset))
+ 		{
+ 			case T_Const:
+ 				coffset = (Const *)limoffset;
+ 				offset = (int)(coffset->constvalue);
+ 				break;
+ 
+ 			case T_Param:
+ 				poffset = (Param *)limoffset;
+ 				paramLI = estate->es_param_list_info;
+ 
+ 				if (paramLI == NULL)
+ 					elog(ERROR, "parameter for limit offset not in executor state");
+ 				for (i = 0; paramLI[i].kind != PARAM_INVALID; i++)
+ 				{
+ 					if (paramLI[i].kind == PARAM_NUM && paramLI[i].id == poffset->paramid)
+ 						break;
+ 				}
+ 				if (paramLI[i].kind == PARAM_INVALID)
+ 					elog(ERROR, "parameter for limit offset not in executor state");
+ 				if (paramLI[i].isnull)
+ 					elog(ERROR, "limit offset cannot be NULL value");
+ 				offset = (int)(paramLI[i].value);
+ 
+ 				break;
+ 
+ 			default:
+ 				elog(ERROR, "unexpected node type %d as limit offset", nodeTag(limoffset));
+ 		}
+ 
+ 		if (offset < 0)
+ 			elog(ERROR, "limit offset cannot be negative");
+ 	}
+ 
+ 	/******************
+ 	 *	if given get the count of the LIMIT clause
+ 	 ******************
+ 	 */
+ 	if (limcount != NULL)
+ 	{
+ 		Const		*ccount;
+ 		Param		*pcount;
+ 		ParamListInfo	paramLI;
+ 		int		i;
+ 
+ 		switch (nodeTag(limcount))
+ 		{
+ 			case T_Const:
+ 				ccount = (Const *)limcount;
+ 				count = (int)(ccount->constvalue);
+ 				break;
+ 
+ 			case T_Param:
+ 				pcount = (Param *)limcount;
+ 				paramLI = estate->es_param_list_info;
+ 
+ 				if (paramLI == NULL)
+ 					elog(ERROR, "parameter for limit count not in executor state");
+ 				for (i = 0; paramLI[i].kind != PARAM_INVALID; i++)
+ 				{
+ 					if (paramLI[i].kind == PARAM_NUM && paramLI[i].id == pcount->paramid)
+ 						break;
+ 				}
+ 				if (paramLI[i].kind == PARAM_INVALID)
+ 					elog(ERROR, "parameter for limit count not in executor state");
+ 				if (paramLI[i].isnull)
+ 					elog(ERROR, "limit count cannot be NULL value");
+ 				count = (int)(paramLI[i].value);
+ 
+ 				break;
+ 
+ 			default:
+ 				elog(ERROR, "unexpected node type %d as limit count", nodeTag(limcount));
+ 		}
+ 
+ 		if (count < 0)
+ 			elog(ERROR, "limit count cannot be negative");
+ 	}
+ 
  	switch (feature)
  	{
  
***************
*** 199,205 ****
  								 plan,
  								 parseTree,
  								 operation,
! 								 ALL_TUPLES,
  								 ForwardScanDirection,
  								 destination);
  			break;
--- 292,299 ----
  								 plan,
  								 parseTree,
  								 operation,
! 								 offset,
! 								 count,
  								 ForwardScanDirection,
  								 destination);
  			break;
***************
*** 208,213 ****
--- 302,308 ----
  								 plan,
  								 parseTree,
  								 operation,
+ 								 offset,
  								 count,
  								 ForwardScanDirection,
  								 destination);
***************
*** 222,227 ****
--- 317,323 ----
  								 plan,
  								 parseTree,
  								 operation,
+ 								 offset,
  								 count,
  								 BackwardScanDirection,
  								 destination);
***************
*** 237,242 ****
--- 333,339 ----
  								 plan,
  								 parseTree,
  								 operation,
+ 								 0,
  								 ONE_TUPLE,
  								 ForwardScanDirection,
  								 destination);
***************
*** 691,696 ****
--- 788,794 ----
  			Plan *plan,
  			Query *parseTree,
  			CmdType operation,
+ 			int offsetTuples,
  			int numberTuples,
  			ScanDirection direction,
  			void (*printfunc) ())
***************
*** 742,747 ****
--- 840,859 ----
  		{
  			result = NULL;
  			break;
+ 		}
+ 
+ 		/******************
+ 		 *	For now we completely execute the plan and skip
+ 		 *	result tuples if requested by LIMIT offset.
+ 		 *	Finally we should try to do it in deeper levels
+ 		 *	if possible (during index scan)
+ 		 *	- Jan
+ 		 ******************
+ 		 */
+ 		if (offsetTuples > 0)
+ 		{
+ 			--offsetTuples;
+ 			continue;
  		}
  
  		/******************
diff -cr src.orig/backend/executor/functions.c src/backend/executor/functions.c
*** src.orig/backend/executor/functions.c	Fri Oct 16 11:53:38 1998
--- src/backend/executor/functions.c	Fri Oct 16 19:01:02 1998
***************
*** 130,135 ****
--- 130,138 ----
  									 None);
  		estate = CreateExecutorState();
  
+ 		if (queryTree->limitOffset != NULL || queryTree->limitCount != NULL)
+ 			elog(ERROR, "LIMIT clause from SQL functions not yet implemented");
+ 
  		if (nargs > 0)
  		{
  			int			i;
***************
*** 200,206 ****
  
  	feature = (LAST_POSTQUEL_COMMAND(es)) ? EXEC_RETONE : EXEC_RUN;
  
! 	return ExecutorRun(es->qd, es->estate, feature, 0);
  }
  
  static void
--- 203,209 ----
  
  	feature = (LAST_POSTQUEL_COMMAND(es)) ? EXEC_RETONE : EXEC_RUN;
  
! 	return ExecutorRun(es->qd, es->estate, feature, (Node *)NULL, (Node *)NULL);
  }
  
  static void
diff -cr src.orig/backend/executor/spi.c src/backend/executor/spi.c
*** src.orig/backend/executor/spi.c	Fri Oct 16 11:53:39 1998
--- src/backend/executor/spi.c	Fri Oct 16 19:25:33 1998
***************
*** 791,796 ****
--- 791,798 ----
  	bool		isRetrieveIntoRelation = false;
  	char	   *intoName = NULL;
  	int			res;
+ 	Const		tcount_const;
+ 	Node		*count = NULL;
  
  	switch (operation)
  	{
***************
*** 825,830 ****
--- 827,865 ----
  			return SPI_ERROR_OPUNKNOWN;
  	}
  
+ 	/* ----------------
+ 	 *	Get the query LIMIT tuple count
+ 	 * ----------------
+ 	 */
+ 	if (parseTree->limitCount != NULL)
+ 	{
+ 		/* ----------------
+ 		 *      A limit clause in the parsetree overrides the
+ 		 *	tcount parameter
+ 		 * ----------------
+ 		 */
+ 		count = parseTree->limitCount;
+ 	}
+ 	else
+ 	{
+ 		/* ----------------
+ 		 *      No LIMIT clause in parsetree. Use a local Const node
+ 		 *	to put tcount into it
+ 		 * ----------------
+ 		 */
+ 		memset(&tcount_const, 0, sizeof(tcount_const));
+ 		tcount_const.type           = T_Const;
+ 		tcount_const.consttype      = INT4OID;
+ 		tcount_const.constlen       = sizeof(int4);
+ 		tcount_const.constvalue     = (Datum)tcount;
+ 		tcount_const.constisnull    = FALSE;
+ 		tcount_const.constbyval     = TRUE;
+ 		tcount_const.constisset     = FALSE;
+ 		tcount_const.constiscast    = FALSE;
+ 
+ 		count = (Node *)&tcount_const;
+ 	}
+ 
  	if (state == NULL)			/* plan preparation */
  		return res;
  #ifdef SPI_EXECUTOR_STATS
***************
*** 845,851 ****
  		return SPI_OK_CURSOR;
  	}
  
! 	ExecutorRun(queryDesc, state, EXEC_FOR, tcount);
  
  	_SPI_current->processed = state->es_processed;
  	if (operation == CMD_SELECT && queryDesc->dest == SPI)
--- 880,886 ----
  		return SPI_OK_CURSOR;
  	}
  
! 	ExecutorRun(queryDesc, state, EXEC_FOR, parseTree->limitOffset, count);
  
  	_SPI_current->processed = state->es_processed;
  	if (operation == CMD_SELECT && queryDesc->dest == SPI)
diff -cr src.orig/backend/parser/analyze.c src/backend/parser/analyze.c
*** src.orig/backend/parser/analyze.c	Fri Oct 16 11:53:41 1998
--- src/backend/parser/analyze.c	Fri Oct 16 13:29:27 1998
***************
*** 180,186 ****
--- 180,190 ----
  
  		case T_SelectStmt:
  			if (!((SelectStmt *) parseTree)->portalname)
+ 			{
  				result = transformSelectStmt(pstate, (SelectStmt *) parseTree);
+ 				result->limitOffset = ((SelectStmt *)parseTree)->limitOffset;
+ 				result->limitCount = ((SelectStmt *)parseTree)->limitCount;
+ 			}
  			else
  				result = transformCursorStmt(pstate, (SelectStmt *) parseTree);
  			break;
diff -cr src.orig/backend/parser/gram.y src/backend/parser/gram.y
*** src.orig/backend/parser/gram.y	Fri Oct 16 11:53:42 1998
--- src/backend/parser/gram.y	Sun Oct 18 22:20:36 1998
***************
*** 45,50 ****
--- 45,51 ----
  #include "catalog/catname.h"
  #include "utils/elog.h"
  #include "access/xact.h"
+ #include "catalog/pg_type.h"
  
  #ifdef MULTIBYTE
  #include "mb/pg_wchar.h"
***************
*** 163,169 ****
  		sort_clause, sortby_list, index_params, index_list, name_list,
  		from_clause, from_list, opt_array_bounds, nest_array_bounds,
  		expr_list, attrs, res_target_list, res_target_list2,
! 		def_list, opt_indirection, group_clause, TriggerFuncArgs
  
  %type <node>	func_return
  %type <boolean>	set_opt
--- 164,171 ----
  		sort_clause, sortby_list, index_params, index_list, name_list,
  		from_clause, from_list, opt_array_bounds, nest_array_bounds,
  		expr_list, attrs, res_target_list, res_target_list2,
! 		def_list, opt_indirection, group_clause, TriggerFuncArgs,
! 		opt_select_limit
  
  %type <node>	func_return
  %type <boolean>	set_opt
***************
*** 192,197 ****
--- 194,201 ----
  
  %type <ival>	fetch_how_many
  
+ %type <node>	select_limit_value select_offset_value
+ 
  %type <list>	OptSeqList
  %type <defelt>	OptSeqElem
  
***************
*** 267,273 ****
  		FALSE_P, FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
  		GRANT, GROUP, HAVING, HOUR_P,
  		IN, INNER_P, INSENSITIVE, INSERT, INTERVAL, INTO, IS,
! 		JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LOCAL,
  		MATCH, MINUTE_P, MONTH_P, NAMES,
  		NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULL_P, NUMERIC,
  		OF, ON, ONLY, OPTION, OR, ORDER, OUTER_P,
--- 271,277 ----
  		FALSE_P, FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
  		GRANT, GROUP, HAVING, HOUR_P,
  		IN, INNER_P, INSENSITIVE, INSERT, INTERVAL, INTO, IS,
! 		JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LIMIT, LOCAL,
  		MATCH, MINUTE_P, MONTH_P, NAMES,
  		NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULL_P, NUMERIC,
  		OF, ON, ONLY, OPTION, OR, ORDER, OUTER_P,
***************
*** 299,305 ****
  		INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
  		LANCOMPILER, LISTEN, LOAD, LOCATION, LOCK_P, MAXVALUE, MINVALUE, MOVE,
  		NEW, NOCREATEDB, NOCREATEUSER, NONE, NOTHING, NOTIFY, NOTNULL,
! 		OIDS, OPERATOR, PASSWORD, PROCEDURAL,
  		RECIPE, RENAME, RESET, RETURNS, ROW, RULE,
  		SEQUENCE, SERIAL, SETOF, SHOW, START, STATEMENT, STDIN, STDOUT, TRUSTED, 
  		UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
--- 303,309 ----
  		INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
  		LANCOMPILER, LISTEN, LOAD, LOCATION, LOCK_P, MAXVALUE, MINVALUE, MOVE,
  		NEW, NOCREATEDB, NOCREATEUSER, NONE, NOTHING, NOTIFY, NOTNULL,
! 		OFFSET, OIDS, OPERATOR, PASSWORD, PROCEDURAL,
  		RECIPE, RENAME, RESET, RETURNS, ROW, RULE,
  		SEQUENCE, SERIAL, SETOF, SHOW, START, STATEMENT, STDIN, STDOUT, TRUSTED, 
  		UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
***************
*** 2591,2596 ****
--- 2595,2601 ----
  			 result from_clause where_clause
  			 group_clause having_clause
  			 union_clause sort_clause
+ 			 opt_select_limit
  				{
  					SelectStmt *n = makeNode(SelectStmt);
  					n->unique = $2;
***************
*** 2602,2607 ****
--- 2607,2622 ----
  					n->havingClause = $8;
  					n->unionClause = $9;
  					n->sortClause = $10;
+ 					if ($11 != NIL)
+ 					{
+ 						n->limitOffset = nth(0, $11);
+ 						n->limitCount = nth(1, $11);
+ 					}
+ 					else
+ 					{
+ 						n->limitOffset = NULL;
+ 						n->limitCount = NULL;
+ 					}
  					$$ = (Node *)n;
  				}
  		;
***************
*** 2699,2704 ****
--- 2714,2794 ----
  		| ASC									{ $$ = "<"; }
  		| DESC									{ $$ = ">"; }
  		| /*EMPTY*/								{ $$ = "<"; /*default*/ }
+ 		;
+ 
+ opt_select_limit:	LIMIT select_offset_value ',' select_limit_value
+ 	{ $$ = lappend(lappend(NIL, $2), $4); }
+ 		| LIMIT select_limit_value OFFSET select_offset_value
+ 	{ $$ = lappend(lappend(NIL, $4), $2); }
+ 		| LIMIT select_limit_value
+ 	{ $$ = lappend(lappend(NIL, NULL), $2); }
+ 		| OFFSET select_offset_value LIMIT select_limit_value
+ 	{ $$ = lappend(lappend(NIL, $2), $4); }
+ 		| OFFSET select_offset_value
+ 	{ $$ = lappend(lappend(NIL, $2), NULL); }
+ 		| /* EMPTY */
+ 	{ $$ = NIL; }
+ 		;
+ 
+ select_limit_value:	Iconst
+ 			{
+ 				Const	*n = makeNode(Const);
+ 
+ 				if ($1 < 1)
+ 					elog(ERROR, "selection limit must be ALL or a positive integer value > 0");
+ 
+ 				n->consttype	= INT4OID;
+ 				n->constlen	= sizeof(int4);
+ 				n->constvalue	= (Datum)$1;
+ 				n->constisnull	= FALSE;
+ 				n->constbyval	= TRUE;
+ 				n->constisset	= FALSE;
+ 				n->constiscast	= FALSE;
+ 				$$ = (Node *)n;
+ 			}
+ 		| ALL
+ 			{
+ 				Const	*n = makeNode(Const);
+ 				n->consttype	= INT4OID;
+ 				n->constlen	= sizeof(int4);
+ 				n->constvalue	= (Datum)0;
+ 				n->constisnull	= FALSE;
+ 				n->constbyval	= TRUE;
+ 				n->constisset	= FALSE;
+ 				n->constiscast	= FALSE;
+ 				$$ = (Node *)n;
+ 			}
+ 		| PARAM
+ 			{
+ 				Param	*n = makeNode(Param);
+ 				n->paramkind = PARAM_NUM;
+ 				n->paramid = $1;
+ 				n->paramtype = INT4OID;
+ 				$$ = (Node *)n;
+ 			}
+ 		;
+ 
+ select_offset_value:	Iconst
+ 			{
+ 				Const	*n = makeNode(Const);
+ 
+ 				n->consttype	= INT4OID;
+ 				n->constlen	= sizeof(int4);
+ 				n->constvalue	= (Datum)$1;
+ 				n->constisnull	= FALSE;
+ 				n->constbyval	= TRUE;
+ 				n->constisset	= FALSE;
+ 				n->constiscast	= FALSE;
+ 				$$ = (Node *)n;
+ 			}
+ 		| PARAM
+ 			{
+ 				Param	*n = makeNode(Param);
+ 				n->paramkind = PARAM_NUM;
+ 				n->paramid = $1;
+ 				n->paramtype = INT4OID;
+ 				$$ = (Node *)n;
+ 			}
  		;
  
  /*
diff -cr src.orig/backend/parser/keywords.c src/backend/parser/keywords.c
*** src.orig/backend/parser/keywords.c	Fri Oct 16 11:53:42 1998
--- src/backend/parser/keywords.c	Sun Oct 18 22:13:29 1998
***************
*** 128,133 ****
--- 128,134 ----
  	{"leading", LEADING},
  	{"left", LEFT},
  	{"like", LIKE},
+ 	{"limit", LIMIT},
  	{"listen", LISTEN},
  	{"load", LOAD},
  	{"local", LOCAL},
***************
*** 156,161 ****
--- 157,163 ----
  	{"null", NULL_P},
  	{"numeric", NUMERIC},
  	{"of", OF},
+ 	{"offset", OFFSET},
  	{"oids", OIDS},
  	{"old", CURRENT},
  	{"on", ON},
diff -cr src.orig/backend/rewrite/rewriteDefine.c src/backend/rewrite/rewriteDefine.c
*** src.orig/backend/rewrite/rewriteDefine.c	Fri Oct 16 11:53:46 1998
--- src/backend/rewrite/rewriteDefine.c	Fri Oct 16 13:48:55 1998
***************
*** 312,317 ****
--- 312,323 ----
  		heap_close(event_relation);
  
  		/*
+ 		 * LIMIT in view is not supported
+ 		 */
+ 		if (query->limitOffset != NULL || query->limitCount != NULL)
+ 			elog(ERROR, "LIMIT clause not supported in views");
+ 
+ 		/*
  		 * ... and finally the rule must be named _RETviewname.
  		 */
  		sprintf(expected_name, "_RET%s", event_obj->relname);
diff -cr src.orig/backend/tcop/pquery.c src/backend/tcop/pquery.c
*** src.orig/backend/tcop/pquery.c	Fri Oct 16 11:53:47 1998
--- src/backend/tcop/pquery.c	Fri Oct 16 14:02:36 1998
***************
*** 40,46 ****
  #include "commands/command.h"
  
  static char *CreateOperationTag(int operationType);
! static void ProcessQueryDesc(QueryDesc *queryDesc);
  
  
  /* ----------------------------------------------------------------
--- 40,46 ----
  #include "commands/command.h"
  
  static char *CreateOperationTag(int operationType);
! static void ProcessQueryDesc(QueryDesc *queryDesc, Node *limoffset, Node *limcount);
  
  
  /* ----------------------------------------------------------------
***************
*** 205,211 ****
   * ----------------------------------------------------------------
   */
  static void
! ProcessQueryDesc(QueryDesc *queryDesc)
  {
  	Query	   *parseTree;
  	Plan	   *plan;
--- 205,211 ----
   * ----------------------------------------------------------------
   */
  static void
! ProcessQueryDesc(QueryDesc *queryDesc, Node *limoffset, Node *limcount)
  {
  	Query	   *parseTree;
  	Plan	   *plan;
***************
*** 330,336 ****
  	 *	 actually run the plan..
  	 * ----------------
  	 */
! 	ExecutorRun(queryDesc, state, EXEC_RUN, 0);
  
  	/* save infos for EndCommand */
  	UpdateCommandInfo(operation, state->es_lastoid, state->es_processed);
--- 330,336 ----
  	 *	 actually run the plan..
  	 * ----------------
  	 */
! 	ExecutorRun(queryDesc, state, EXEC_RUN, limoffset, limcount);
  
  	/* save infos for EndCommand */
  	UpdateCommandInfo(operation, state->es_lastoid, state->es_processed);
***************
*** 373,377 ****
  		print_plan(plan, parsetree);
  	}
  	else
! 		ProcessQueryDesc(queryDesc);
  }
--- 373,377 ----
  		print_plan(plan, parsetree);
  	}
  	else
! 		ProcessQueryDesc(queryDesc, parsetree->limitOffset, parsetree->limitCount);
  }
diff -cr src.orig/include/executor/executor.h src/include/executor/executor.h
*** src.orig/include/executor/executor.h	Fri Oct 16 11:53:56 1998
--- src/include/executor/executor.h	Fri Oct 16 12:04:17 1998
***************
*** 83,89 ****
   * prototypes from functions in execMain.c
   */
  extern TupleDesc ExecutorStart(QueryDesc *queryDesc, EState *estate);
! extern TupleTableSlot *ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, int count);
  extern void ExecutorEnd(QueryDesc *queryDesc, EState *estate);
  extern HeapTuple ExecConstraints(char *caller, Relation rel, HeapTuple tuple);
  #ifdef QUERY_LIMIT
--- 83,89 ----
   * prototypes from functions in execMain.c
   */
  extern TupleDesc ExecutorStart(QueryDesc *queryDesc, EState *estate);
! extern TupleTableSlot *ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, Node *limoffset, Node *limcount);
  extern void ExecutorEnd(QueryDesc *queryDesc, EState *estate);
  extern HeapTuple ExecConstraints(char *caller, Relation rel, HeapTuple tuple);
  #ifdef QUERY_LIMIT