~tsarev/percona-server/5.5-processlist_rows_stats-sporadic_fails-fix

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
diff -ruN a/include/heap.h b/include/heap.h
--- a/include/heap.h	2011-04-11 13:44:02.000000000 +0300
+++ b/include/heap.h	2011-06-02 15:28:13.294917001 +0300
@@ -33,7 +33,17 @@
 #include "my_compare.h"
 #include "my_tree.h"
 
-	/* defines used by heap-funktions */
+/* Define index limits to be identical to MyISAM ones for compatibility. */
+
+#if MAX_INDEXES > HA_MAX_POSSIBLE_KEY
+#define HP_MAX_KEY                  HA_MAX_POSSIBLE_KEY /* Max allowed keys */
+#else
+#define HP_MAX_KEY                  MAX_INDEXES         /* Max allowed keys */
+#endif
+
+#define HP_MAX_KEY_LENGTH           1000            /* Max length in bytes */
+
+/* defines used by heap-funktions */
 
 #define HP_MAX_LEVELS	4		/* 128^5 records is enough */
 #define HP_PTRS_IN_NOD	128
@@ -129,22 +139,58 @@
   uint (*get_key_length)(struct st_hp_keydef *keydef, const uchar *key);
 } HP_KEYDEF;
 
-typedef struct st_heap_share
+typedef struct st_heap_columndef		/* column information */
+{
+  int16  type;	  			/* en_fieldtype */
+  uint32 length;		  	/* length of field */
+  uint32 offset;		  	/* Offset to position in row */
+  uint8  null_bit;			/* If column may be 0 */
+  uint16 null_pos;			/* position for null marker */
+  uint8  length_bytes;			/* length of the size, 1 o 2 bytes */
+} HP_COLUMNDEF;
+
+typedef struct st_heap_dataspace   /* control data for data space */
 {
   HP_BLOCK block;
+  /* Total chunks ever allocated in this dataspace */
+  uint chunk_count;
+  uint del_chunk_count;         /* Deleted chunks count */
+  uchar *del_link;              /* Link to last deleted chunk */
+  uint chunk_length;            /* Total length of one chunk */
+  /* Length of payload that will be placed into one chunk */
+  uint chunk_dataspace_length;
+  /* Offset of the status flag relative to the chunk start */
+  uint offset_status;
+  /* Offset of the linking pointer relative to the chunk start */
+  uint offset_link;
+  /* Test whether records have variable size and so "next" pointer */
+  uint is_variable_size;
+  /* Total size allocated within this data space */
+  ulonglong total_data_length;
+} HP_DATASPACE;
+
+typedef struct st_heap_share
+{
   HP_KEYDEF  *keydef;
+  HP_COLUMNDEF *column_defs;
+  /* Describes "block", which contains actual records */
+  HP_DATASPACE recordspace;
   ulong min_records,max_records;	/* Params to open */
-  ulonglong data_length,index_length,max_table_size;
+  ulonglong index_length, max_table_size;
   uint key_stat_version;                /* version to indicate insert/delete */
-  uint records;				/* records */
-  uint blength;				/* records rounded up to 2^n */
-  uint deleted;				/* Deleted records in database */
-  uint reclength;			/* Length of one record */
+  uint records;			/* Actual record (row) count */
+  uint blength;			/* used_chunk_count rounded up to 2^n */
+  /*
+    Length of record's fixed part, which contains keys and always fits into the
+    first chunk.
+  */
+  uint fixed_data_length;
+  uint fixed_column_count;  /* Number of columns stored in fixed_data_length */
   uint changed;
   uint keys,max_key_length;
+  uint column_count;
   uint currently_disabled_keys;    /* saved value from "keys" when disabled */
   uint open_count;
-  uchar *del_link;			/* Link to next block with del. rec */
   char * name;			/* Name of "memory-file" */
   THR_LOCK lock;
   mysql_mutex_t intern_lock;            /* Locking for use with _locking */
@@ -153,6 +199,7 @@
   uint auto_key;
   uint auto_key_type;			/* real type of the auto key segment */
   ulonglong auto_increment;
+  uint blobs;  /* Number of blobs in table */
 } HP_SHARE;
 
 struct st_hp_hash_info;
@@ -162,7 +209,7 @@
   HP_SHARE *s;
   uchar *current_ptr;
   struct st_hp_hash_info *current_hash_ptr;
-  ulong current_record,next_block;
+  ulong current_record;
   int lastinx,errkey;
   int  mode;				/* Mode of file (READONLY..) */
   uint opt_flag,update;
@@ -175,6 +222,9 @@
   my_bool implicit_emptied;
   THR_LOCK_DATA lock;
   LIST open_list;
+  uchar *blob_buffer;  /* Temporary buffer used to return BLOB values */
+  uint blob_size;      /* Current blob_buffer size */
+  uint blob_offset;    /* Current offset in blob_buffer */
 } HP_INFO;
 
 
@@ -196,6 +246,14 @@
     open_count to 1. Is only looked at if not internal_table.
   */
   my_bool pin_share;
+  uint columns;
+  HP_COLUMNDEF *columndef;
+  uint fixed_key_fieldnr;
+  uint fixed_data_size;
+  uint keys_memory_size;
+  uint max_chunk_size;
+  uint is_dynamic;
+  uint blobs;
 } HP_CREATE_INFO;
 
 	/* Prototypes for heap-functions */
@@ -212,9 +270,8 @@
 extern int heap_scan(register HP_INFO *info, uchar *record);
 extern int heap_delete(HP_INFO *info,const uchar *buff);
 extern int heap_info(HP_INFO *info,HEAPINFO *x,int flag);
-extern int heap_create(const char *name,
-                       HP_CREATE_INFO *create_info, HP_SHARE **share,
-                       my_bool *created_new_share);
+extern int heap_create(const char *name, HP_CREATE_INFO *create_info,
+                       HP_SHARE **res, my_bool *created_new_share);
 extern int heap_delete_table(const char *name);
 extern void heap_drop_table(HP_INFO *info);
 extern int heap_extra(HP_INFO *info,enum ha_extra_function function);
diff -ruN a/mysql-test/r/create.result b/mysql-test/r/create.result
--- a/mysql-test/r/create.result	2011-06-02 11:38:04.000000000 +0300
+++ b/mysql-test/r/create.result	2011-06-02 15:56:29.224917005 +0300
@@ -33,10 +33,7 @@
 create table t1 (b char(0) not null, index(b));
 ERROR 42000: The used storage engine can't index column 'b'
 create table t1 (a int not null,b text) engine=heap;
-ERROR 42000: The used table type doesn't support BLOB/TEXT columns
 drop table if exists t1;
-Warnings:
-Note	1051	Unknown table 't1'
 create table t1 (ordid int(8) not null auto_increment, ord  varchar(50) not null, primary key (ord,ordid)) engine=heap;
 ERROR 42000: Incorrect table definition; there can be only one auto column and it must be defined as a key
 create table not_existing_database.test (a int);
diff -ruN a/mysql-test/r/ctype_utf8mb4_heap.result b/mysql-test/r/ctype_utf8mb4_heap.result
--- a/mysql-test/r/ctype_utf8mb4_heap.result	2011-04-11 13:44:02.000000000 +0300
+++ b/mysql-test/r/ctype_utf8mb4_heap.result	2011-06-02 15:31:07.794917003 +0300
@@ -1124,6 +1124,8 @@
 a varchar(255) NOT NULL default '',
 KEY a (a)
 ) ENGINE=heap DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;
+Warnings:
+Warning	1071	Specified key was too long; max key length is 1000 bytes
 insert into t1 values (_utf8mb4 0xe880bd);
 insert into t1 values (_utf8mb4 0x5b);
 select hex(a) from t1;
@@ -1162,6 +1164,8 @@
 Warnings:
 Note	1051	Unknown table 't1'
 CREATE TABLE t1(a VARCHAR(255), KEY(a)) ENGINE=heap DEFAULT CHARSET=utf8mb4;
+Warnings:
+Warning	1071	Specified key was too long; max key length is 1000 bytes
 INSERT INTO t1 VALUES('uuABCDEFGHIGKLMNOPRSTUVWXYZ̈bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb');
 INSERT INTO t1 VALUES('uu');
 check table t1;
diff -ruN a/mysql-test/t/create.test b/mysql-test/t/create.test
--- a/mysql-test/t/create.test	2011-04-11 13:44:01.000000000 +0300
+++ b/mysql-test/t/create.test	2011-06-02 15:32:49.614917004 +0300
@@ -33,7 +33,7 @@
 drop table if exists t1,t2;
 --error 1167
 create table t1 (b char(0) not null, index(b));
---error 1163
+# BLOB/TEXT fields are now supported by HEAP
 create table t1 (a int not null,b text) engine=heap;
 drop table if exists t1;
 
diff -ruN a/storage/heap/CMakeLists.txt b/storage/heap/CMakeLists.txt
--- a/storage/heap/CMakeLists.txt	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/CMakeLists.txt	2011-06-02 15:37:43.744917005 +0300
@@ -20,6 +20,7 @@
 				ha_heap.cc
 				hp_delete.c hp_extra.c hp_hash.c hp_info.c hp_open.c hp_panic.c
 				hp_rename.c hp_rfirst.c hp_rkey.c hp_rlast.c hp_rnext.c hp_rprev.c
+				hp_dspace.c hp_record.c
 				hp_rrnd.c hp_rsame.c hp_scan.c hp_static.c hp_update.c hp_write.c)
 
 MYSQL_ADD_PLUGIN(heap ${HEAP_SOURCES} STORAGE_ENGINE MANDATORY RECOMPILE_FOR_EMBEDDED)
diff -ruN a/storage/heap/_check.c b/storage/heap/_check.c
--- a/storage/heap/_check.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/_check.c	2011-06-02 15:37:43.744917005 +0300
@@ -43,7 +43,7 @@
 {
   int error;
   uint key;
-  ulong records=0, deleted=0, pos, next_block;
+  ulong records= 0, deleted= 0, chunk_count= 0, pos, next_block;
   HP_SHARE *share=info->s;
   HP_INFO save_info= *info;			/* Needed because scan_init */
   DBUG_ENTER("heap_check_heap");
@@ -64,31 +64,55 @@
   {
     if (pos < next_block)
     {
-      info->current_ptr+= share->block.recbuffer;
+      info->current_ptr+= share->recordspace.block.recbuffer;
     }
     else
     {
-      next_block+= share->block.records_in_block;
-      if (next_block >= share->records+share->deleted)
+      next_block+= share->recordspace.block.records_in_block;
+      if (next_block >= share->recordspace.chunk_count)
       {
-	next_block= share->records+share->deleted;
-	if (pos >= next_block)
-	  break;				/* End of file */
+        next_block= share->recordspace.chunk_count;
+        if (pos >= next_block)
+          break;				/* End of file */
       }
     }
     hp_find_record(info,pos);
 
-    if (!info->current_ptr[share->reclength])
+    switch (get_chunk_status(&share->recordspace, info->current_ptr)) {
+    case CHUNK_STATUS_DELETED:
       deleted++;
-    else
+      chunk_count++;
+      break;
+    case CHUNK_STATUS_ACTIVE:
       records++;
+        chunk_count++;
+        break;
+    case CHUNK_STATUS_LINKED:
+      chunk_count++;
+      break;
+    default:
+      DBUG_PRINT("error",
+                 ("Unknown record status: Record: 0x%lx  Status %lu",
+                  (long) info->current_ptr,
+                  (ulong) get_chunk_status(&share->recordspace,
+                                           info->current_ptr)));
+        error|= 1;
+        break;
+    }
   }
 
-  if (records != share->records || deleted != share->deleted)
-  {
-    DBUG_PRINT("error",("Found rows: %lu (%lu)  deleted %lu (%lu)",
-			records, (ulong) share->records,
-                        deleted, (ulong) share->deleted));
+  /* TODO: verify linked chunks (no orphans, no cycles, no bad links) */
+
+  if (records != share->records ||
+      chunk_count != share->recordspace.chunk_count ||
+      deleted != share->recordspace.del_chunk_count)
+  {
+    DBUG_PRINT("error",
+               ("Found rows: %lu (%lu) total chunks %lu (%lu) deleted chunks "
+                "%lu (%lu)",
+                records, (ulong) share->records,
+                chunk_count, (ulong) share->recordspace.chunk_count,
+                deleted, (ulong) share->recordspace.del_chunk_count));
     error= 1;
   }
   *info= save_info;
@@ -177,7 +201,7 @@
     do
     {
       memcpy(&recpos, key + (*keydef->get_key_length)(keydef,key), sizeof(uchar*));
-      key_length= hp_rb_make_key(keydef, info->recbuf, recpos, 0);
+      key_length= hp_rb_make_key(keydef, info->recbuf, recpos, 0, TRUE);
       if (ha_key_cmp(keydef->seg, (uchar*) info->recbuf, (uchar*) key,
 		     key_length, SEARCH_FIND | SEARCH_SAME, not_used))
       {
diff -ruN a/storage/heap/_rectest.c b/storage/heap/_rectest.c
--- a/storage/heap/_rectest.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/_rectest.c	2011-06-02 15:38:02.774917002 +0300
@@ -22,7 +22,9 @@
 {
   DBUG_ENTER("hp_rectest");
 
-  if (memcmp(info->current_ptr,old,(size_t) info->s->reclength))
+  if (hp_process_record_data_to_chunkset(info->s, old,
+                                         info->current_ptr,
+                                         1))
   {
     DBUG_RETURN((my_errno=HA_ERR_RECORD_CHANGED)); /* Record have changed */
   }
diff -ruN a/storage/heap/ha_heap.cc b/storage/heap/ha_heap.cc
--- a/storage/heap/ha_heap.cc	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/ha_heap.cc	2011-06-02 15:37:43.744917005 +0300
@@ -113,6 +113,7 @@
 
     rc= heap_create(name, &create_info, &internal_share, &created_new_share);
     my_free(create_info.keydef);
+    my_free(create_info.columndef);
     if (rc)
       goto end;
 
@@ -194,6 +195,12 @@
   {
     if (table->key_info[i].algorithm == HA_KEY_ALG_BTREE)
       btree_keys.set_bit(i);
+    /*
+      Reset per-key block size specification so they are not shown
+      in SHOW CREATE TABLE.
+    */
+    table->key_info[i].block_size= 0;
+    table->key_info[i].flags&= ~HA_USES_BLOCK_SIZE;
   }
 }
 
@@ -427,6 +434,13 @@
   return 0;
 }
 
+enum row_type ha_heap::get_row_type() const
+{
+  if (file->s->recordspace.is_variable_size)
+    return ROW_TYPE_DYNAMIC;
+
+  return ROW_TYPE_FIXED;
+}
 
 int ha_heap::extra(enum ha_extra_function operation)
 {
@@ -644,23 +658,70 @@
 heap_prepare_hp_create_info(TABLE *table_arg, bool internal_table,
                             HP_CREATE_INFO *hp_create_info)
 {
-  uint key, parts, mem_per_row= 0, keys= table_arg->s->keys;
+  uint key, parts, mem_per_row_keys= 0, keys= table_arg->s->keys;
   uint auto_key= 0, auto_key_type= 0;
-  ha_rows max_rows;
+  uint fixed_key_fieldnr = 0, fixed_data_size = 0, next_field_pos = 0;
+  uint column_idx, column_count= table_arg->s->fields;
+  HP_COLUMNDEF *columndef;
   HP_KEYDEF *keydef;
   HA_KEYSEG *seg;
   TABLE_SHARE *share= table_arg->s;
   bool found_real_auto_increment= 0;
+  uint blobs= 0;
 
   bzero(hp_create_info, sizeof(*hp_create_info));
 
+  if (!(columndef= (HP_COLUMNDEF*) my_malloc(column_count *
+                                             sizeof(HP_COLUMNDEF),
+                                             MYF(MY_WME))))
+    return my_errno;
+
+  for (column_idx= 0; column_idx < column_count; column_idx++)
+  {
+    Field* field= *(table_arg->field + column_idx);
+    HP_COLUMNDEF* column= columndef + column_idx;
+    column->type= (uint16) field->type();
+    column->length= field->pack_length();
+    column->offset= field->offset(table_arg->record[0]);
+
+    if (field->null_bit)
+    {
+      column->null_bit= field->null_bit;
+      column->null_pos= (uint) (field->null_ptr -
+                                (uchar*) table_arg->record[0]);
+    }
+    else
+    {
+      column->null_bit= 0;
+      column->null_pos= 0;
+    }
+
+    if (field->type() == MYSQL_TYPE_VARCHAR)
+    {
+      column->length_bytes= (uint8) (((Field_varstring *) field)->length_bytes);
+    }
+    else if (field->type() == MYSQL_TYPE_BLOB)
+    {
+      blobs++;
+      column->length_bytes= (uint8)
+        (((Field_blob *) field)->pack_length_no_ptr());
+    }
+    else
+    {
+      column->length_bytes= 0;
+    }
+  }
+
   for (key= parts= 0; key < keys; key++)
     parts+= table_arg->key_info[key].key_parts;
 
   if (!(keydef= (HP_KEYDEF*) my_malloc(keys * sizeof(HP_KEYDEF) +
 				       parts * sizeof(HA_KEYSEG),
 				       MYF(MY_WME))))
+  {
+    my_free((uchar *) columndef);
     return my_errno;
+  }
   seg= reinterpret_cast<HA_KEYSEG*>(keydef + keys);
   for (key= 0; key < keys; key++)
   {
@@ -676,11 +737,11 @@
     case HA_KEY_ALG_UNDEF:
     case HA_KEY_ALG_HASH:
       keydef[key].algorithm= HA_KEY_ALG_HASH;
-      mem_per_row+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
+      mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
       break;
     case HA_KEY_ALG_BTREE:
       keydef[key].algorithm= HA_KEY_ALG_BTREE;
-      mem_per_row+=sizeof(TREE_ELEMENT)+pos->key_length+sizeof(char*);
+      mem_per_row_keys+=sizeof(TREE_ELEMENT)+pos->key_length+sizeof(char*);
       break;
     default:
       DBUG_ASSERT(0); // cannot happen
@@ -705,6 +766,16 @@
       seg->length=  (uint) key_part->length;
       seg->flag=    key_part->key_part_flag;
 
+      next_field_pos= seg->start;
+      if (field->type() == MYSQL_TYPE_VARCHAR)
+      {
+        Field *orig_field= *(table_arg->field + key_part->field->field_index);
+        next_field_pos+= orig_field->pack_length();
+      }
+      else
+      {
+        next_field_pos+= seg->length;
+      }
       if (field->flags & (ENUM_FLAG | SET_FLAG))
         seg->charset= &my_charset_bin;
       else
@@ -730,9 +801,75 @@
         auto_key= key+ 1;
 	auto_key_type= field->key_type();
       }
+
+      switch (seg->type) {
+      case HA_KEYTYPE_SHORT_INT:
+      case HA_KEYTYPE_LONG_INT:
+      case HA_KEYTYPE_FLOAT:
+      case HA_KEYTYPE_DOUBLE:
+      case HA_KEYTYPE_USHORT_INT:
+      case HA_KEYTYPE_ULONG_INT:
+      case HA_KEYTYPE_LONGLONG:
+      case HA_KEYTYPE_ULONGLONG:
+      case HA_KEYTYPE_INT24:
+      case HA_KEYTYPE_UINT24:
+      case HA_KEYTYPE_INT8:
+        seg->flag|= HA_SWAP_KEY;
+        break;
+      case HA_KEYTYPE_VARBINARY1:
+        /* Case-insensitiveness is handled in coll->hash_sort */
+        seg->type= HA_KEYTYPE_VARTEXT1;
+        /* fall through */
+      case HA_KEYTYPE_VARTEXT1:
+        keydef[key].flag|= HA_VAR_LENGTH_KEY;
+        /* Save number of bytes used to store length */
+        if (seg->flag & HA_BLOB_PART)
+          seg->bit_start= field->pack_length() - share->blob_ptr_size;
+        else
+          seg->bit_start= 1;
+        break;
+      case HA_KEYTYPE_VARBINARY2:
+        /* Case-insensitiveness is handled in coll->hash_sort */
+        /* fall_through */
+      case HA_KEYTYPE_VARTEXT2:
+        keydef[key].flag|= HA_VAR_LENGTH_KEY;
+        /* Save number of bytes used to store length */
+        if (seg->flag & HA_BLOB_PART)
+          seg->bit_start= field->pack_length() - share->blob_ptr_size;
+        else
+          seg->bit_start= 2;
+        /*
+          Make future comparison simpler by only having to check for
+          one type
+        */
+        seg->type= HA_KEYTYPE_VARTEXT1;
+        break;
+      default:
+        break;
+      }
+
+      if (next_field_pos > fixed_data_size)
+      {
+        fixed_data_size= next_field_pos;
+      }
+
+
+      if (field->field_index >= fixed_key_fieldnr)
+      {
+        /*
+          Do not use seg->fieldnr as it's not reliable in case of temp tables
+        */
+        fixed_key_fieldnr= field->field_index + 1;
+      }
     }
   }
-  mem_per_row+= MY_ALIGN(share->reclength + 1, sizeof(char*));
+
+  if (fixed_data_size < share->null_bytes)
+  {
+    /* Make sure to include null fields regardless of the presense of keys */
+    fixed_data_size = share->null_bytes;
+  }
+
   if (table_arg->found_next_number_field)
   {
     keydef[share->next_number_index].flag|= HA_AUTO_KEY;
@@ -743,16 +880,19 @@
   hp_create_info->max_table_size=current_thd->variables.max_heap_table_size;
   hp_create_info->with_auto_increment= found_real_auto_increment;
   hp_create_info->internal_table= internal_table;
-
-  max_rows= (ha_rows) (hp_create_info->max_table_size / mem_per_row);
-  if (share->max_rows && share->max_rows < max_rows)
-    max_rows= share->max_rows;
-
-  hp_create_info->max_records= (ulong) max_rows;
+  hp_create_info->max_chunk_size= share->key_block_size;
+  hp_create_info->is_dynamic= (share->row_type == ROW_TYPE_DYNAMIC);
+  hp_create_info->columns= column_count;
+  hp_create_info->columndef= columndef;
+  hp_create_info->fixed_key_fieldnr= fixed_key_fieldnr;
+  hp_create_info->fixed_data_size= fixed_data_size;
+  hp_create_info->max_records= (ulong) share->max_rows;
   hp_create_info->min_records= (ulong) share->min_rows;
   hp_create_info->keys= share->keys;
   hp_create_info->reclength= share->reclength;
+  hp_create_info->keys_memory_size= mem_per_row_keys;
   hp_create_info->keydef= keydef;
+  hp_create_info->blobs= blobs;
   return 0;
 }
 
@@ -772,6 +912,7 @@
 				  create_info->auto_increment_value - 1 : 0);
   error= heap_create(name, &hp_create_info, &internal_share, &created);
   my_free(hp_create_info.keydef);
+  my_free(hp_create_info.columndef);
   DBUG_ASSERT(file == 0);
   return (error);
 }
@@ -782,6 +923,13 @@
   table->file->info(HA_STATUS_AUTO);
   if (!(create_info->used_fields & HA_CREATE_USED_AUTO))
     create_info->auto_increment_value= stats.auto_increment_value;
+  if (!(create_info->used_fields & HA_CREATE_USED_KEY_BLOCK_SIZE))
+  {
+    if (file->s->recordspace.is_variable_size)
+      create_info->key_block_size= file->s->recordspace.chunk_length;
+    else
+      create_info->key_block_size= 0;
+  }
 }
 
 void ha_heap::get_auto_increment(ulonglong offset, ulonglong increment,
diff -ruN a/storage/heap/ha_heap.h b/storage/heap/ha_heap.h
--- a/storage/heap/ha_heap.h	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/ha_heap.h	2011-06-02 15:37:43.744917005 +0300
@@ -46,12 +46,11 @@
     return ((table_share->key_info[inx].algorithm == HA_KEY_ALG_BTREE) ?
             "BTREE" : "HASH");
   }
-  /* Rows also use a fixed-size format */
-  enum row_type get_row_type() const { return ROW_TYPE_FIXED; }
+  enum row_type get_row_type() const;
   const char **bas_ext() const;
   ulonglong table_flags() const
   {
-    return (HA_FAST_KEY_READ | HA_NO_BLOBS | HA_NULL_IN_KEY |
+    return (HA_FAST_KEY_READ | HA_NULL_IN_KEY |
             HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE |
             HA_REC_NOT_IN_SEQ | HA_CAN_INSERT_DELAYED | HA_NO_TRANSACTIONS |
             HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT);
@@ -63,8 +62,9 @@
             HA_ONLY_WHOLE_INDEX | HA_KEY_SCAN_NOT_ROR);
   }
   const key_map *keys_to_use_for_scanning() { return &btree_keys; }
-  uint max_supported_keys()          const { return MAX_KEY; }
-  uint max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
+  uint max_supported_keys()          const { return HP_MAX_KEY; }
+  uint max_supported_key_length() const { return HP_MAX_KEY_LENGTH; }
+  uint max_supported_key_part_length() const { return HP_MAX_KEY_LENGTH; }
   double scan_time()
   { return (double) (stats.records+stats.deleted) / 20.0+10; }
   double read_time(uint index, uint ranges, ha_rows rows)
diff -ruN a/storage/heap/heapdef.h b/storage/heap/heapdef.h
--- a/storage/heap/heapdef.h	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/heapdef.h	2011-06-02 15:37:43.754916999 +0300
@@ -32,6 +32,13 @@
 #define HP_MIN_RECORDS_IN_BLOCK 16
 #define HP_MAX_RECORDS_IN_BLOCK 8192
 
+/* this chunk has been deleted and can be reused */
+#define CHUNK_STATUS_DELETED 0
+/* this chunk represents the first part of a live record */
+#define CHUNK_STATUS_ACTIVE  1
+/* this chunk is a continuation from another chunk (part of chunkset) */
+#define CHUNK_STATUS_LINKED  2
+
 	/* Some extern variables */
 
 extern LIST *heap_open_list,*heap_share_list;
@@ -42,7 +49,14 @@
 #define hp_find_hash(A,B) ((HASH_INFO*) hp_find_block((A),(B)))
 
 	/* Find pos for record and update it in info->current_ptr */
-#define hp_find_record(info,pos) (info)->current_ptr= hp_find_block(&(info)->s->block,pos)
+#define hp_find_record(info,pos) \
+  (info)->current_ptr= hp_find_block(&(info)->s->recordspace.block,pos)
+
+#define get_chunk_status(info,ptr) (ptr[(info)->offset_status])
+
+#define get_chunk_count(info,rec_length) \
+  ((rec_length + (info)->chunk_dataspace_length - 1) / \
+   (info)->chunk_dataspace_length)
 
 typedef struct st_hp_hash_info
 {
@@ -90,7 +104,7 @@
 		      const uchar *key);
 extern void hp_make_key(HP_KEYDEF *keydef,uchar *key,const uchar *rec);
 extern uint hp_rb_make_key(HP_KEYDEF *keydef, uchar *key,
-			   const uchar *rec, uchar *recpos);
+			   const uchar *rec, uchar *recpos, my_bool packed);
 extern uint hp_rb_key_length(HP_KEYDEF *keydef, const uchar *key);
 extern uint hp_rb_null_key_length(HP_KEYDEF *keydef, const uchar *key);
 extern uint hp_rb_var_key_length(HP_KEYDEF *keydef, const uchar *key);
@@ -100,6 +114,23 @@
 extern void hp_clear_keys(HP_SHARE *info);
 extern uint hp_rb_pack_key(HP_KEYDEF *keydef, uchar *key, const uchar *old,
                            key_part_map keypart_map);
+extern uint hp_calc_blob_length(uint length, const uchar *pos);
+
+/* Chunkset management (alloc/free/encode/decode) functions */
+extern uchar *hp_allocate_chunkset(HP_DATASPACE *info, uint chunk_count);
+extern int hp_reallocate_chunkset(HP_DATASPACE *info, uint chunk_count,
+                                  uchar *pos);
+extern void hp_free_chunks(HP_DATASPACE *info, uchar *pos);
+extern void hp_clear_dataspace(HP_DATASPACE *info);
+
+extern uint hp_get_encoded_data_length(HP_SHARE *info, const uchar *record,
+                                       uint *chunk_count);
+extern void hp_copy_record_data_to_chunkset(HP_SHARE *info, const uchar *record,
+                                            uchar *pos);
+extern int hp_extract_record(HP_INFO *info, uchar *record, const uchar *pos);
+extern uint hp_process_record_data_to_chunkset(HP_SHARE *info,
+                                               const uchar *record, uchar *pos,
+                                               uint is_compare);
 
 extern mysql_mutex_t THR_LOCK_heap;
 
diff -ruN a/storage/heap/hp_clear.c b/storage/heap/hp_clear.c
--- a/storage/heap/hp_clear.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_clear.c	2011-06-02 15:37:43.754916999 +0300
@@ -30,16 +30,11 @@
 {
   DBUG_ENTER("hp_clear");
 
-  if (info->block.levels)
-    (void) hp_free_level(&info->block,info->block.levels,info->block.root,
-			(uchar*) 0);
-  info->block.levels=0;
+  hp_clear_dataspace(&info->recordspace);
   hp_clear_keys(info);
-  info->records= info->deleted= 0;
-  info->data_length= 0;
+  info->records= 0;
   info->blength=1;
   info->changed=0;
-  info->del_link=0;
   DBUG_VOID_RETURN;
 }
 
@@ -157,7 +152,7 @@
   int error= 0;
   HP_SHARE *share= info->s;
 
-  if (share->data_length || share->index_length)
+  if (share->recordspace.total_data_length || share->index_length)
     error= HA_ERR_CRASHED;
   else
     if (share->currently_disabled_keys)
diff -ruN a/storage/heap/hp_close.c b/storage/heap/hp_close.c
--- a/storage/heap/hp_close.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_close.c	2011-06-02 15:37:43.754916999 +0300
@@ -46,6 +46,10 @@
     heap_open_list=list_delete(heap_open_list,&info->open_list);
   if (!--info->s->open_count && info->s->delete_on_close)
     hp_free(info->s);				/* Table was deleted */
+  if (info->blob_buffer)
+  {
+    my_free(info->blob_buffer);
+  }
   my_free(info);
   DBUG_RETURN(error);
 }
diff -ruN a/storage/heap/hp_create.c b/storage/heap/hp_create.c
--- a/storage/heap/hp_create.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_create.c	2011-06-02 15:37:43.754916999 +0300
@@ -14,11 +14,21 @@
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
 #include "heapdef.h"
+#include <mysql_com.h>
+#include <mysqld_error.h>
 
 static int keys_compare(heap_rb_param *param, uchar *key1, uchar *key2);
-static void init_block(HP_BLOCK *block,uint reclength,ulong min_records,
+static void init_block(HP_BLOCK *block,uint chunk_length, ulong min_records,
 		       ulong max_records);
 
+#define FIXED_REC_OVERHEAD (sizeof(uchar))
+#define VARIABLE_REC_OVERHEAD (sizeof(uchar **) + sizeof(uchar))
+
+/* Minimum size that a chunk can take, 12 bytes on 32bit, 24 bytes on 64bit */
+#define VARIABLE_MIN_CHUNK_SIZE                                         \
+  ((sizeof(uchar **) + VARIABLE_REC_OVERHEAD + sizeof(uchar **) - 1) &  \
+   ~(sizeof(uchar **) - 1))
+
 /* Create a heap table */
 
 int heap_create(const char *name, HP_CREATE_INFO *create_info,
@@ -32,6 +42,7 @@
   uint keys= create_info->keys;
   ulong min_records= create_info->min_records;
   ulong max_records= create_info->max_records;
+  ulong max_rows_for_stated_memory;
   DBUG_ENTER("heap_create");
 
   if (!create_info->internal_table)
@@ -48,15 +59,147 @@
 
   if (!share)
   {
+    uint chunk_dataspace_length, chunk_length, is_variable_size;
+    uint fixed_data_length, fixed_column_count;
     HP_KEYDEF *keyinfo;
     DBUG_PRINT("info",("Initializing new table"));
-    
+
+    if (create_info->max_chunk_size)
+    {
+      uint configured_chunk_size= create_info->max_chunk_size;
+
+      /* User requested variable-size records, let's see if they're possible */
+
+      if (configured_chunk_size < create_info->fixed_data_size)
+      {
+        /*
+          The resulting chunk_size cannot be smaller than fixed data part
+          at the start of the first chunk which allows faster copying
+          with a single memcpy().
+        */
+        my_error(ER_CANT_USE_OPTION_HERE, MYF(0), "key_block_size");
+        goto err;
+      }
+
+      if (reclength > configured_chunk_size + VARIABLE_REC_OVERHEAD ||
+	  create_info->blobs > 0)
+      {
+        /*
+          Allow variable size only if we're saving some space, i.e.
+          if a fixed-size record would take more space than variable-size
+          one plus the variable-size overhead.
+          There has to be at least one field after indexed fields.
+          Note that NULL bits are already included in key_part_size.
+        */
+        is_variable_size= 1;
+        chunk_dataspace_length= configured_chunk_size;
+      }
+      else
+      {
+        /* max_chunk_size is near the full reclength, let's use fixed size */
+        is_variable_size= 0;
+        chunk_dataspace_length= reclength;
+      }
+    }
+    else if ((create_info->is_dynamic && reclength >
+              256 + VARIABLE_REC_OVERHEAD)
+             || create_info->blobs > 0)
+    {
+      /*
+        User asked for dynamic records - use 256 as the chunk size, if that
+        will may save some memory. Otherwise revert to fixed size format.
+      */
+      if ((create_info->fixed_data_size + VARIABLE_REC_OVERHEAD) > 256)
+        chunk_dataspace_length= create_info->fixed_data_size;
+      else
+        chunk_dataspace_length= 256 - VARIABLE_REC_OVERHEAD;
+
+      is_variable_size= 1;
+    }
+    else
+    {
+      /*
+        If max_chunk_size is not specified, put the whole record in one chunk
+      */
+      is_variable_size= 0;
+      chunk_dataspace_length= reclength;
+    }
+
+    if (is_variable_size)
+    {
+      /* Check whether we have any variable size records past key data */
+      uint has_variable_fields= 0;
+
+      fixed_data_length= create_info->fixed_data_size;
+      fixed_column_count= create_info->fixed_key_fieldnr;
+
+      for (i= create_info->fixed_key_fieldnr; i < create_info->columns; i++)
+      {
+        HP_COLUMNDEF *column= create_info->columndef + i;
+	if ((column->type == MYSQL_TYPE_VARCHAR &&
+	     (column->length - column->length_bytes) >= 32) ||
+	    column->type == MYSQL_TYPE_BLOB)
+        {
+            /*
+              The field has to be either blob or >= 5.0.3 true VARCHAR
+              and have substantial length.
+              TODO: do we want to calculate minimum length?
+            */
+            has_variable_fields= 1;
+            break;
+        }
+
+        if (has_variable_fields)
+        {
+          break;
+        }
+
+        if ((column->offset + column->length) <= chunk_dataspace_length)
+        {
+          /* Still no variable-size columns, add one fixed-length */
+          fixed_column_count= i + 1;
+          fixed_data_length= column->offset + column->length;
+        }
+      }
+
+      if (!has_variable_fields && create_info->blobs == 0)
+      {
+        /*
+          There is no need to use variable-size records without variable-size
+          columns.
+          Reset sizes if it's not variable size anymore.
+        */
+        is_variable_size= 0;
+        chunk_dataspace_length= reclength;
+        fixed_data_length= reclength;
+        fixed_column_count= create_info->columns;
+      }
+    }
+    else
+    {
+      fixed_data_length= reclength;
+      fixed_column_count= create_info->columns;
+    }
+
     /*
-      We have to store sometimes uchar* del_link in records,
-      so the record length should be at least sizeof(uchar*)
+      We store uchar* del_link inside the data area of deleted records,
+      so the data length should be at least sizeof(uchar*)
     */
-    set_if_bigger(reclength, sizeof (uchar*));
-    
+    set_if_bigger(chunk_dataspace_length, sizeof (uchar **));
+
+    if (is_variable_size)
+    {
+      chunk_length= chunk_dataspace_length + VARIABLE_REC_OVERHEAD;
+    }
+    else
+    {
+      chunk_length= chunk_dataspace_length + FIXED_REC_OVERHEAD;
+    }
+
+    /* Align chunk length to the next pointer */
+    chunk_length= (uint) (chunk_length + sizeof(uchar **) - 1) &
+      ~(sizeof(uchar **) - 1);
+
     for (i= key_segs= max_length= 0, keyinfo= keydef; i < keys; i++, keyinfo++)
     {
       bzero((char*) &keyinfo->block,sizeof(keyinfo->block));
@@ -73,42 +216,11 @@
 	    keyinfo->rb_tree.size_of_element++;
 	}
 	switch (keyinfo->seg[j].type) {
-	case HA_KEYTYPE_SHORT_INT:
-	case HA_KEYTYPE_LONG_INT:
-	case HA_KEYTYPE_FLOAT:
-	case HA_KEYTYPE_DOUBLE:
-	case HA_KEYTYPE_USHORT_INT:
-	case HA_KEYTYPE_ULONG_INT:
-	case HA_KEYTYPE_LONGLONG:
-	case HA_KEYTYPE_ULONGLONG:
-	case HA_KEYTYPE_INT24:
-	case HA_KEYTYPE_UINT24:
-	case HA_KEYTYPE_INT8:
-	  keyinfo->seg[j].flag|= HA_SWAP_KEY;
-          break;
         case HA_KEYTYPE_VARBINARY1:
-          /* Case-insensitiveness is handled in coll->hash_sort */
-          keyinfo->seg[j].type= HA_KEYTYPE_VARTEXT1;
-          /* fall_through */
         case HA_KEYTYPE_VARTEXT1:
-          keyinfo->flag|= HA_VAR_LENGTH_KEY;
-          length+= 2;
-          /* Save number of bytes used to store length */
-          keyinfo->seg[j].bit_start= 1;
-          break;
         case HA_KEYTYPE_VARBINARY2:
-          /* Case-insensitiveness is handled in coll->hash_sort */
-          /* fall_through */
         case HA_KEYTYPE_VARTEXT2:
-          keyinfo->flag|= HA_VAR_LENGTH_KEY;
           length+= 2;
-          /* Save number of bytes used to store length */
-          keyinfo->seg[j].bit_start= 2;
-          /*
-            Make future comparison simpler by only having to check for
-            one type
-          */
-          keyinfo->seg[j].type= HA_KEYTYPE_VARTEXT1;
           break;
 	default:
 	  break;
@@ -133,13 +245,34 @@
     }
     if (!(share= (HP_SHARE*) my_malloc((uint) sizeof(HP_SHARE)+
 				       keys*sizeof(HP_KEYDEF)+
+                                       (create_info->columns *
+                                        sizeof(HP_COLUMNDEF)) +
 				       key_segs*sizeof(HA_KEYSEG),
 				       MYF(MY_ZEROFILL))))
       goto err;
-    share->keydef= (HP_KEYDEF*) (share + 1);
+
+    /*
+      Max_records is used for estimating block sizes and for enforcement.
+      Calculate the very maximum number of rows (if everything was one chunk)
+      and then take either that value or configured max_records (pick smallest
+      one).
+    */
+    max_rows_for_stated_memory= (ha_rows) (create_info->max_table_size /
+                                           (create_info->keys_memory_size +
+                                            chunk_length));
+    max_records = ((max_records && max_records < max_rows_for_stated_memory) ?
+                   max_records : max_rows_for_stated_memory);
+
+    share->column_defs= (HP_COLUMNDEF*) (share + 1);
+    memcpy(share->column_defs, create_info->columndef,
+           (size_t) (sizeof(create_info->columndef[0]) *
+                     create_info->columns));
+
+    share->keydef= (HP_KEYDEF*) (share->column_defs + create_info->columns);
     share->key_stat_version= 1;
     keyseg= (HA_KEYSEG*) (share->keydef + keys);
-    init_block(&share->block, reclength + 1, min_records, max_records);
+    init_block(&share->recordspace.block, chunk_length, min_records,
+               max_records);
 	/* Fix keys */
     memcpy(share->keydef, keydef, (size_t) (sizeof(keydef[0]) * keys));
     for (i= 0, keyinfo= share->keydef; i < keys; i++, keyinfo++)
@@ -177,15 +310,35 @@
     share->min_records= min_records;
     share->max_records= max_records;
     share->max_table_size= create_info->max_table_size;
-    share->data_length= share->index_length= 0;
-    share->reclength= reclength;
+    share->index_length= 0;
     share->blength= 1;
     share->keys= keys;
     share->max_key_length= max_length;
+    share->column_count= create_info->columns;
     share->changed= 0;
     share->auto_key= create_info->auto_key;
     share->auto_key_type= create_info->auto_key_type;
     share->auto_increment= create_info->auto_increment;
+
+    share->fixed_data_length= fixed_data_length;
+    share->fixed_column_count= fixed_column_count;
+    share->blobs= create_info->blobs;
+
+    share->recordspace.chunk_length= chunk_length;
+    share->recordspace.chunk_dataspace_length= chunk_dataspace_length;
+    share->recordspace.is_variable_size= is_variable_size;
+    share->recordspace.total_data_length= 0;
+
+    if (is_variable_size) {
+      share->recordspace.offset_link= chunk_dataspace_length;
+      share->recordspace.offset_status= share->recordspace.offset_link +
+        sizeof(uchar **);
+    } else {
+      /* Make it likely to fail if anyone uses this offset */
+      share->recordspace.offset_link= 1 << 22;
+      share->recordspace.offset_status= chunk_dataspace_length;
+    }
+
     /* Must be allocated separately for rename to work */
     if (!(share->name= my_strdup(name,MYF(0))))
     {
@@ -227,7 +380,7 @@
 		    param->search_flag, not_used);
 }
 
-static void init_block(HP_BLOCK *block, uint reclength, ulong min_records,
+static void init_block(HP_BLOCK *block, uint chunk_length, ulong min_records,
 		       ulong max_records)
 {
   uint i,recbuffer,records_in_block;
@@ -235,7 +388,12 @@
   max_records= max(min_records,max_records);
   if (!max_records)
     max_records= 1000;			/* As good as quess as anything */
-  recbuffer= (uint) (reclength + sizeof(uchar**) - 1) & ~(sizeof(uchar**) - 1);
+  /*
+    We want to start each chunk at 8 bytes boundary, round recbuffer to the
+    next 8.
+  */
+  recbuffer= (uint) (chunk_length + sizeof(uchar**) - 1) &
+    ~(sizeof(uchar**) - 1);
   records_in_block= max_records / 10;
   if (records_in_block < 10 && max_records)
     records_in_block= 10;
diff -ruN a/storage/heap/hp_delete.c b/storage/heap/hp_delete.c
--- a/storage/heap/hp_delete.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_delete.c	2011-06-02 15:37:43.754916999 +0300
@@ -22,6 +22,8 @@
   uchar *pos;
   HP_SHARE *share=info->s;
   HP_KEYDEF *keydef, *end, *p_lastinx;
+  uint rec_length, chunk_count;
+
   DBUG_ENTER("heap_delete");
   DBUG_PRINT("enter",("info: 0x%lx  record: 0x%lx", (long) info, (long) record));
 
@@ -31,6 +33,8 @@
     DBUG_RETURN(my_errno);			/* Record changed */
   share->changed=1;
 
+  rec_length = hp_get_encoded_data_length(share, record, &chunk_count);
+
   if ( --(share->records) < share->blength >> 1) share->blength>>=1;
   pos=info->current_ptr;
 
@@ -43,10 +47,7 @@
   }
 
   info->update=HA_STATE_DELETED;
-  *((uchar**) pos)=share->del_link;
-  share->del_link=pos;
-  pos[share->reclength]=0;		/* Record deleted */
-  share->deleted++;
+  hp_free_chunks(&share->recordspace, pos);
   info->current_hash_ptr=0;
 #if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
   DBUG_EXECUTE("check_heap",heap_check_heap(info, 0););
@@ -75,7 +76,8 @@
     info->last_pos= NULL; /* For heap_rnext/heap_rprev */
 
   custom_arg.keyseg= keyinfo->seg;
-  custom_arg.key_length= hp_rb_make_key(keyinfo, info->recbuf, record, recpos);
+  custom_arg.key_length= hp_rb_make_key(keyinfo, info->recbuf, record, recpos,
+                                        FALSE);
   custom_arg.search_flag= SEARCH_SAME;
   old_allocated= keyinfo->rb_tree.allocated;
   res= tree_delete(&keyinfo->rb_tree, info->recbuf, custom_arg.key_length,
@@ -112,6 +114,7 @@
   blength=share->blength;
   if (share->records+1 == blength)
     blength+= blength;
+
   lastpos=hp_find_hash(&keyinfo->block,share->records);
   last_ptr=0;
 
diff -ruN /dev/null b/storage/heap/hp_dspace.c
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ b/storage/heap/hp_dspace.c	2011-06-02 15:40:14.104917001 +0300
@@ -0,0 +1,440 @@
+/* Copyright (C) 2000-2002 MySQL AB
+   Copyright (C) 2008 eBay, Inc
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 of the License.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+/*
+  Implements various base dataspace-related functions - allocate, free, clear
+*/
+
+#include "heapdef.h"
+
+
+/*
+  MySQL Heap tables keep data in arrays of fixed-size chunks.
+  These chunks are organized into two groups of HP_BLOCK structures:
+    - group1 contains indexes, with one HP_BLOCK per key
+      (part of HP_KEYDEF)
+    - group2 contains record data, with single HP_BLOCK
+      for all records, referenced by HP_SHARE.recordspace.block
+
+  While columns used in index are usually small, other columns
+  in the table may need to accomodate larger data. Typically,
+  larger data is placed into VARCHAR or BLOB columns. With actual
+  sizes varying, Heap Engine has to support variable-sized records
+  in memory. Heap Engine implements the concept of dataspace
+  (HP_DATASPACE), which incorporates HP_BLOCK for the record data,
+  and adds more information for managing variable-sized records.
+
+  Variable-size records are stored in multiple "chunks",
+  which means that a single record of data (database "row") can
+  consist of multiple chunks organized into one "set". HP_BLOCK
+  contains chunks. In variable-size format, one record
+  is represented as one or many chunks, depending on the actual
+  data, while in fixed-size mode, one record is always represented
+  as one chunk. The index structures would always point to the first
+  chunk in the chunkset.
+
+  At the time of table creation, Heap Engine attempts to find out if
+  variable-size records are desired. A user can request
+  variable-size records by providing either row_type=dynamic or
+  key_block_size=NNN table create option. Heap Engine will check
+  whether key_block_size provides enough space in the first chunk
+  to keep all null bits and columns that are used in indexes.
+  If key_block_size is too small, table creation will be aborted
+  with an error. Heap Engine will revert to fixed-size allocation
+  mode if key_block_size provides no memory benefits (if the
+  fixed-size record would always be shorter then the first chunk
+  in the chunkset with the specified key_block_size).
+
+  In order to improve index search performance, Heap Engine needs
+  to keep all null flags and all columns used as keys inside
+  the first chunk of a chunkset. In particular, this means that
+  all columns used as keys should be defined first in the table
+  creation SQL. The length of data used by null bits and key columns
+  is stored as fixed_data_length inside HP_SHARE. fixed_data_length
+  will extend past last key column if more fixed-length fields can
+  fit into the first chunk.
+
+  Variable-size records are necessary only in the presence of
+  variable-size columns. Heap Engine will be looking for BLOB
+  columns or VARCHAR columns, which declare length of 32 or more. If
+  no such columns are found, table will be switched to fixed-size
+  format. You should always try to put such columns at the end of
+  the table definition.
+
+  Whenever data is being inserted or updated in the table
+  Heap Engine will calculate how many chunks are necessary.
+  For insert operations, Heap Engine allocates new chunkset in
+  the recordspace. For update operations it will modify length of
+  the existing chunkset, unlinking unnecessary chunks at the end,
+  or allocating and adding more if larger length is necessary.
+
+  When writing data to chunks or copying data back to record,
+  fixed-size columns are copied in their full format. VARCHARs and
+  BLOBs are copied based on their actual length. Any NULL values
+  after fixed_data_length are skipped.
+
+  The allocation and contents of the actual chunks varies between
+  fixed and variable-size modes. Total chunk length is always
+  aligned to the next sizeof(uchar*). Here is the format of
+  fixed-size chunk:
+      uchar[] - sizeof=chunk_dataspace_length, but at least
+                sizeof(uchar*) bytes. Keeps actual data or pointer
+                to the next deleted chunk.
+                chunk_dataspace_length equals to full record length
+      uchar   - status field (1 means "in use", 0 means "deleted")
+
+  Variable-size chunk uses different format:
+      uchar[] - sizeof=chunk_dataspace_length, but at least
+                sizeof(uchar*) bytes. Keeps actual data or pointer
+                to the next deleted chunk.
+                chunk_dataspace_length is set according to table
+                setup (key_block_size)
+      uchar*  - pointer to the next chunk in this chunkset,
+                or NULL for the last chunk
+      uchar   - status field (1 means "first", 0 means "deleted",
+                2 means "linked")
+
+  When allocating a new chunkset of N chunks, Heap Engine will try
+  to allocate chunks one-by-one, linking them as they become
+  allocated. Allocation of a single chunk will attempt to reuse
+  a deleted (freed) chunk. If no free chunks are available,
+  it will attempt to allocate a new area inside HP_BLOCK.
+  Freeing chunks will place them at the front of free list
+  referenced by del_link in HP_DATASPACE. The newly freed chunk
+  will contain reference to the previously freed chunk in its first
+  sizeof(uchar*) of the payload space.
+
+  Here is open issues:
+    -  It is not very nice to require people to keep key columns
+       at the beginning of the table creation SQL. There are three
+       proposed resolutions:
+       a. Leave it as is. It's a reasonable limitation
+       b. Add new HA_KEEP_KEY_COLUMNS_TO_FRONT flag to handler.h and
+          make table.cpp align columns when it creates the table
+       c. Make HeapEngine reorder columns in the chunk data, so that
+          key columns go first. Add parallel HA_KEYSEG structures
+          to distinguish positions in record vs. positions in
+          the first chunk. Copy all data field-by-field rather than
+          using single memcpy unless DBA kept key columns to
+          the beginning.
+    -  heap_check_heap needs verify linked chunks, looking for
+       issues such as orphans, cycles, and bad links. However,
+       Heap Engine today does not do similar things even for
+       free list.
+    -  In a more sophisticated implementation, some space can
+       be saved even with all fixed-size columns if many of them
+       have NULL value, as long as these columns are not used
+       in indexes
+    -  In variable-size format status should be moved to lower
+       bits of the "next" pointer. Pointer is always aligned
+       to sizeof(byte*), which is at least 4, leaving 2 lower
+       bits free. This will save 8 bytes per chunk
+       on 64-bit platform.
+    -  As we do not want to modify FRM format or to add new SQL
+       keywords, KEY_BLOCK_SIZE option of "CREATE TABLE" is reused
+       to specify block size for Heap Engine tables.
+    -  since all key columns must fit in the first chunk, having keys
+       on BLOB columns is currently impossible. This limitation is
+       relatively easiy to remove in future.
+*/
+
+static uchar *hp_allocate_one_chunk(HP_DATASPACE *info);
+
+
+/**
+  Clear a dataspace
+
+  Frees memory and zeros-out any relevant counters in the dataspace
+
+  @param  info  the dataspace to clear
+*/
+
+void hp_clear_dataspace(HP_DATASPACE *info)
+{
+  if (info->block.levels)
+  {
+    hp_free_level(&info->block,info->block.levels,info->block.root,
+                  (uchar *) 0);
+  }
+  info->block.levels= 0;
+  info->del_chunk_count= info->chunk_count= 0;
+  info->del_link= 0;
+  info->total_data_length= 0;
+}
+
+
+/**
+  Allocate or reallocate a chunkset in the dataspace
+
+  Attempts to allocate a new chunkset or change the size of an existing chunkset
+
+  @param  info            the hosting dataspace
+  @param  chunk_count     the number of chunks that we expect as the result
+  @param  existing_set    non-null value asks function to resize existing
+                          chunkset, return value would point to this set
+
+  @return  Pointer to the first chunk in the new or updated chunkset, or NULL
+           if unsuccessful
+*/
+
+static uchar *hp_allocate_variable_chunkset(HP_DATASPACE *info,
+                                           uint chunk_count,
+                                           uchar *existing_set)
+{
+  int alloc_count= chunk_count, i;
+  uchar *first_chunk= 0, *curr_chunk= 0, *prev_chunk= 0;
+  uchar  *last_existing_chunk= 0;
+
+  DBUG_ASSERT(alloc_count);
+
+  if (existing_set)
+  {
+    first_chunk= existing_set;
+
+    curr_chunk= existing_set;
+    while (curr_chunk && alloc_count)
+    {
+      prev_chunk= curr_chunk;
+      curr_chunk= *((uchar **) (curr_chunk + info->offset_link));
+      alloc_count--;
+    }
+
+    if (!alloc_count)
+    {
+      if (curr_chunk)
+      {
+        /*
+          We came through all chunks and there is more left, let's truncate the
+          list.
+        */
+        *((uchar **) (prev_chunk + info->offset_link))= NULL;
+        hp_free_chunks(info, curr_chunk);
+      }
+
+      return first_chunk;
+    }
+
+    last_existing_chunk= prev_chunk;
+  }
+
+  /*
+    We can reach this point only if we're allocating new chunkset or more chunks
+    in existing set.
+  */
+
+  for (i= 0; i < alloc_count; i++)
+  {
+    curr_chunk= hp_allocate_one_chunk(info);
+    if (!curr_chunk)
+    {
+      /* no space in the current block */
+
+      if (last_existing_chunk)
+      {
+        /* Truncate whatever was added at the end of the existing chunkset */
+        prev_chunk= last_existing_chunk;
+        curr_chunk= *((uchar **)(prev_chunk + info->offset_link));
+        *((uchar **)(prev_chunk + info->offset_link))= NULL;
+        hp_free_chunks(info, curr_chunk);
+      }
+      else if (first_chunk)
+      {
+        /* free any chunks previously allocated */
+        hp_free_chunks(info, first_chunk);
+      }
+
+      return NULL;
+    }
+
+    /* mark as if this chunk is last in the chunkset */
+    *((uchar **) (curr_chunk + info->offset_link))= 0;
+
+    if (prev_chunk)
+    {
+      /* tie them into a linked list */
+      *((uchar **) (prev_chunk + info->offset_link))= curr_chunk;
+      /* Record linked from active */
+      curr_chunk[info->offset_status]= CHUNK_STATUS_LINKED;
+    }
+    else
+    {
+      /* Record active */
+      curr_chunk[info->offset_status]= CHUNK_STATUS_ACTIVE;
+    }
+
+    if (!first_chunk)
+    {
+      first_chunk= curr_chunk;
+    }
+
+    prev_chunk= curr_chunk;
+}
+
+  return first_chunk;
+}
+
+
+/**
+  Allocate a new chunkset in the dataspace
+
+  Attempts to allocate a new chunkset
+
+  @param  info            the hosting dataspace
+  @param  chunk_count     the number of chunks that we expect as the result
+
+  @return  Pointer to the first chunk in the new or updated chunkset, or NULL if
+           unsuccessful
+*/
+
+uchar *hp_allocate_chunkset(HP_DATASPACE *info, uint chunk_count)
+{
+  uchar *result;
+
+  DBUG_ENTER("hp_allocate_chunks");
+
+  if (info->is_variable_size)
+  {
+    result = hp_allocate_variable_chunkset(info, chunk_count, NULL);
+  }
+  else
+  {
+    result= hp_allocate_one_chunk(info);
+    if (result)
+    {
+      result[info->offset_status]= CHUNK_STATUS_ACTIVE;
+    }
+
+    DBUG_RETURN(result);
+  }
+
+  DBUG_RETURN(result);
+}
+
+
+/**
+  Reallocate an existing chunkset in the dataspace
+
+  Attempts to change the size of an existing chunkset
+
+  @param  info            the hosting dataspace
+  @param  chunk_count     the number of chunks that we expect as the result
+  @param  pos             pointer to the existing chunkset
+
+  @return  Error code or zero if successful
+*/
+
+int hp_reallocate_chunkset(HP_DATASPACE *info, uint chunk_count, uchar *pos)
+{
+  DBUG_ENTER("hp_reallocate_chunks");
+
+  if (!info->is_variable_size)
+  {
+    /* Update should never change chunk_count in fixed-size mode */
+    my_errno= HA_ERR_WRONG_COMMAND;
+    return my_errno;
+  }
+
+  /* Reallocate never moves the first chunk */
+  if (!hp_allocate_variable_chunkset(info, chunk_count, pos))
+    DBUG_RETURN(my_errno);
+
+  DBUG_RETURN(0);
+}
+
+
+/**
+  Allocate a single chunk in the dataspace
+
+  Attempts to allocate a new chunk or reuse one from deleted list
+
+  @param  info            the hosting dataspace
+
+  @return  Pointer to the chunk, or NULL if unsuccessful
+*/
+
+static uchar *hp_allocate_one_chunk(HP_DATASPACE *info)
+{
+  uchar *curr_chunk;
+  size_t length;
+  ulong block_pos;
+
+  if (info->del_link)
+  {
+    curr_chunk= info->del_link;
+    info->del_link= *((uchar **) curr_chunk);
+    info->del_chunk_count--;
+
+    DBUG_PRINT("hp_allocate_one_chunk",
+               ("Used old position: 0x%lx",(long) curr_chunk));
+    return curr_chunk;
+  }
+
+  block_pos= (info->chunk_count % info->block.records_in_block);
+  if (!block_pos)
+  {
+    if (hp_get_new_block(&info->block, &length))
+    {
+      /* no space in the current block */
+      return NULL;
+    }
+
+    info->total_data_length+= length;
+  }
+
+  info->chunk_count++;
+  curr_chunk= ((uchar *) info->block.level_info[0].last_blocks +
+               block_pos * info->block.recbuffer);
+
+  DBUG_PRINT("hp_allocate_one_chunk",
+             ("Used new position: 0x%lx", (long) curr_chunk));
+
+  return curr_chunk;
+}
+
+
+/**
+  Free a list of chunks
+
+  Reclaims all chunks linked by the pointer,
+  which could be the whole chunkset or a part of an existing chunkset
+
+  @param  info            the hosting dataspace
+  @param  pos             pointer to the head of the chunkset
+*/
+
+void hp_free_chunks(HP_DATASPACE *info, uchar *pos)
+{
+  uchar *curr_chunk= pos;
+
+  while (curr_chunk)
+  {
+    info->del_chunk_count++;
+    *((uchar **) curr_chunk)= info->del_link;
+    info->del_link= curr_chunk;
+
+    curr_chunk[info->offset_status]= CHUNK_STATUS_DELETED;
+
+    DBUG_PRINT("hp_free_chunks",("Freed position: 0x%lx", (long) curr_chunk));
+
+    if (!info->is_variable_size)
+    {
+      break;
+    }
+
+    /* Delete next chunk in this chunkset */
+    curr_chunk= *((uchar **)(curr_chunk + info->offset_link));
+  }
+}
diff -ruN a/storage/heap/hp_extra.c b/storage/heap/hp_extra.c
--- a/storage/heap/hp_extra.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_extra.c	2011-06-02 15:37:57.814917006 +0300
@@ -56,7 +56,6 @@
   info->current_record= (ulong) ~0L;
   info->current_hash_ptr=0;
   info->update=0;
-  info->next_block=0;
   return 0;
 }
 
diff -ruN a/storage/heap/hp_hash.c b/storage/heap/hp_hash.c
--- a/storage/heap/hp_hash.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_hash.c	2011-06-02 15:37:57.824916999 +0300
@@ -336,16 +336,26 @@
     {
       CHARSET_INFO *cs= seg->charset;
       uint pack_length= seg->bit_start;
-      uint length= (pack_length == 1 ? (uint) *(uchar*) pos : uint2korr(pos));
+      uint length= hp_calc_blob_length(pack_length, pos);
+
+      if (seg->flag & HA_BLOB_PART)
+      {
+        memcpy(&pos, pos + pack_length, sizeof(char *));
+      }
+      else
+      {
+        pos+= pack_length;
+      }
+
       if (cs->mbmaxlen > 1)
       {
         uint char_length;
-        char_length= my_charpos(cs, pos + pack_length,
-                                pos + pack_length + length,
+        char_length= my_charpos(cs, pos,
+                                pos + length,
                                 seg->length/cs->mbmaxlen);
         set_if_smaller(length, char_length);
       }
-      cs->coll->hash_sort(cs, pos+pack_length, length, &nr, &nr2);
+      cs->coll->hash_sort(cs, pos, length, &nr, &nr2);
     }
     else
     {
@@ -545,18 +555,18 @@
       uint char_length1, char_length2;
       uint pack_length= seg->bit_start;
       CHARSET_INFO *cs= seg->charset;
-      if (pack_length == 1)
-      {
-        char_length1= (uint) *(uchar*) pos1++;
-        char_length2= (uint) *(uchar*) pos2++;
-      }
-      else
+
+      char_length1= hp_calc_blob_length(pack_length, pos1);
+      char_length2= hp_calc_blob_length(pack_length, pos2);
+      pos1+= pack_length;
+      pos2+= pack_length;
+
+      if (seg->flag & HA_BLOB_PART)
       {
-        char_length1= uint2korr(pos1);
-        char_length2= uint2korr(pos2);
-        pos1+= 2;
-        pos2+= 2;
+        memcpy(&pos1, pos1, sizeof(char *));
+        memcpy(&pos2, pos2, sizeof(char *));
       }
+
       if (cs->mbmaxlen > 1)
       {
         uint safe_length1= char_length1;
@@ -668,6 +678,34 @@
 }
 
 
+/**
+   Returns a BLOB length stored in the specified number of bytes at the
+   specified location.
+
+   @param length       the number of bytes used to store length
+   @param pos          pointer to length bytes
+
+   @return  Length of BLOB data.
+*/
+
+uint hp_calc_blob_length(uint bytes, const uchar *pos)
+{
+  switch (bytes) {
+  case 1:
+    return (uint) *pos;
+  case 2:
+    return uint2korr(pos);
+  case 3:
+    return uint3korr(pos);
+  case 4:
+    return uint4korr(pos);
+  default:
+    break;
+  }
+
+  return 0; /* Impossible */
+}
+
 	/* Copy a key from a record to a keybuffer */
 
 void hp_make_key(HP_KEYDEF *keydef, uchar *key, const uchar *rec)
@@ -678,18 +716,37 @@
   {
     CHARSET_INFO *cs= seg->charset;
     uint char_length= seg->length;
-    uchar *pos= (uchar*) rec + seg->start;
+    const uchar *pos= rec + seg->start;
     if (seg->null_bit)
       *key++= test(rec[seg->null_pos] & seg->null_bit);
-    if (cs->mbmaxlen > 1)
+
+    if (seg->flag & HA_BLOB_PART)
     {
-      char_length= my_charpos(cs, pos, pos + seg->length,
-                              char_length / cs->mbmaxlen);
-      set_if_smaller(char_length, seg->length); /* QQ: ok to remove? */
-    }
-    if (seg->type == HA_KEYTYPE_VARTEXT1)
-      char_length+= seg->bit_start;             /* Copy also length */
-    memcpy(key,rec+seg->start,(size_t) char_length);
+      uint tmp_length= hp_calc_blob_length(seg->bit_start, pos);
+      uint length= min(seg->length, tmp_length);
+
+      memcpy(&pos, rec + seg->bit_start, sizeof(char *));
+      if (cs->mbmaxlen > 1)
+      {
+        char_length= my_charpos(cs, pos, pos + seg->length,
+                                char_length / cs->mbmaxlen);
+        set_if_smaller(char_length, length); /* QQ: ok to remove? */
+      }
+      store_key_length_inc(key, char_length);
+    }
+    else
+    {
+      if (cs->mbmaxlen > 1)
+      {
+        char_length= my_charpos(cs, pos, pos + seg->length,
+                                char_length / cs->mbmaxlen);
+        set_if_smaller(char_length, seg->length); /* QQ: ok to remove? */
+      }
+      if (seg->type == HA_KEYTYPE_VARTEXT1)
+        char_length+= seg->bit_start;             /* Copy also length */
+    }
+
+    memcpy(key, pos, (size_t) char_length);
     key+= char_length;
   }
 }
@@ -702,8 +759,8 @@
   } while(0)
 
 
-uint hp_rb_make_key(HP_KEYDEF *keydef, uchar *key, 
-		    const uchar *rec, uchar *recpos)
+uint hp_rb_make_key(HP_KEYDEF *keydef, uchar *key,
+                    const uchar *rec, uchar *recpos, my_bool packed)
 {
   uchar *start_key= key;
   HA_KEYSEG *seg, *endseg;
@@ -772,6 +829,29 @@
       key+= char_length;
       continue;
     }
+    else if (seg->flag & HA_BLOB_PART)
+    {
+      uchar *pos=      (uchar*) rec + seg->start;
+      uint tmp_length= hp_calc_blob_length(seg->bit_start, pos);
+      uint length= min(seg->length, tmp_length);
+      CHARSET_INFO *cs= seg->charset;
+      char_length= seg->length / cs->mbmaxlen;
+
+      /* check_one_rb_key() calls hp_rb_make_key() for already packed records */
+      if (!packed)
+      {
+        memcpy(&pos, pos + seg->bit_start, sizeof(char *));
+      }
+      else
+      {
+        pos+= seg->bit_start;
+      }
+      FIX_LENGTH(cs, pos, length, char_length);
+      store_key_length_inc(key, char_length);
+      memcpy(key, pos, (size_t) char_length);
+      key+= char_length;
+      continue;
+    }
 
     char_length= seg->length;
     if (seg->charset->mbmaxlen > 1)
diff -ruN a/storage/heap/hp_info.c b/storage/heap/hp_info.c
--- a/storage/heap/hp_info.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_info.c	2011-06-02 15:37:57.824916999 +0300
@@ -47,9 +47,22 @@
 {
   DBUG_ENTER("heap_info");
   x->records         = info->s->records;
-  x->deleted         = info->s->deleted;
-  x->reclength       = info->s->reclength;
-  x->data_length     = info->s->data_length;
+  x->deleted         = info->s->recordspace.del_chunk_count;
+
+  if (info->s->recordspace.is_variable_size)
+  {
+    if (info->s->records)
+      x->reclength   = (uint) (info->s->recordspace.total_data_length /
+                               (ulonglong) info->s->records);
+    else
+      x->reclength   = info->s->recordspace.chunk_length;
+  }
+  else
+  {
+    x->reclength     = info->s->recordspace.chunk_dataspace_length;
+  }
+
+  x->data_length     = info->s->recordspace.total_data_length;
   x->index_length    = info->s->index_length;
   x->max_records     = info->s->max_records;
   x->errkey          = info->errkey;
diff -ruN a/storage/heap/hp_open.c b/storage/heap/hp_open.c
--- a/storage/heap/hp_open.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_open.c	2011-06-02 15:37:57.824916999 +0300
@@ -47,9 +47,9 @@
 #ifndef DBUG_OFF
   info->opt_flag= READ_CHECK_USED;		/* Check when changing */
 #endif
-  DBUG_PRINT("exit",("heap: 0x%lx  reclength: %d  records_in_block: %d",
-		     (long) info, share->reclength,
-                     share->block.records_in_block));
+  DBUG_PRINT("exit",("heap: 0x%lx  chunk_length: %d  records_in_block: %d",
+                     (long) info, share->recordspace.chunk_length,
+                     share->recordspace.block.records_in_block));
   DBUG_RETURN(info);
 }
 
diff -ruN /dev/null b/storage/heap/hp_record.c
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ b/storage/heap/hp_record.c	2011-06-02 15:40:21.634916999 +0300
@@ -0,0 +1,498 @@
+/* Copyright (C) 2000-2002 MySQL AB
+   Copyright (C) 2008 eBay, Inc
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 of the License.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+/*
+  Implements various base record-related functions, such as encode and decode
+  into chunks.
+*/
+
+#include "heapdef.h"
+#include <mysql_com.h>
+
+/**
+  Calculate size of the record for the purpose of storing in chunks
+
+  Walk through the fields of the record and calculates the exact space
+  needed in chunks as well the the total chunk count
+
+  @param       info         the hosting table
+  @param       record       the record in standard unpacked format
+  @param[out]  chunk_count  the number of chunks needed for this record
+
+  @return The size of the required storage in bytes
+*/
+
+uint hp_get_encoded_data_length(HP_SHARE *info, const uchar *record,
+                                uint *chunk_count)
+{
+  uint i, dst_offset;
+
+  dst_offset= info->fixed_data_length;
+
+  if (!info->recordspace.is_variable_size)
+  {
+    /* Nothing more to copy */
+    *chunk_count= 1;
+    return dst_offset;
+  }
+
+  for (i= info->fixed_column_count; i < info->column_count; i++)
+  {
+    uint src_offset, length;
+
+    HP_COLUMNDEF *column= info->column_defs + i;
+
+    if (column->null_bit)
+    {
+      if (record[column->null_pos] & column->null_bit)
+      {
+        /* Skip all NULL values */
+        continue;
+      }
+    }
+
+    src_offset= column->offset;
+    if (column->type == MYSQL_TYPE_VARCHAR)
+    {
+      uint pack_length;
+
+      /* >= 5.0.3 true VARCHAR */
+
+      pack_length= column->length_bytes;
+      length= pack_length + (pack_length == 1 ?
+                             (uint) *(uchar *) (record + src_offset) :
+                             uint2korr(record + src_offset));
+    }
+    else if (column->type == MYSQL_TYPE_BLOB)
+    {
+      uint pack_length= column->length_bytes;
+
+      length= pack_length + hp_calc_blob_length(pack_length,
+                                                record + src_offset);
+    }
+    else
+    {
+      length= column->length;
+    }
+
+    dst_offset+= length;
+  }
+
+  *chunk_count= get_chunk_count(&info->recordspace, dst_offset);
+
+  return dst_offset;
+}
+
+
+#if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
+static void dump_chunk(HP_SHARE *info, const uchar *curr_chunk)
+{
+  uint i;
+  fprintf(stdout, "Chunk dump at 0x%lx: ", (long) curr_chunk);
+  for (i= 0; i < info->recordspace.chunk_dataspace_length; i++)
+  {
+    uint b= *((uchar *)(curr_chunk + i));
+    if (b < 0x10)
+    {
+      fprintf(stdout, "0");
+    }
+    fprintf(stdout, "%lx ", (long) b);
+  }
+  fprintf(stdout, ". Next = 0x%lx, Status = %d\n",
+          (long) (*((uchar **) (curr_chunk + info->recordspace.offset_link))),
+          (uint) (*((uchar *) (curr_chunk + info->recordspace.offset_status))));
+}
+#endif
+
+/**
+  Stores data from packed field into the preallocated chunkset,
+  or performs data comparison
+
+  @param  info         the hosting table
+  @param  data         the field data in packed format
+  @param  length       the field data length
+  @param  pos_ptr      the target chunkset
+  @param  off_ptr      the pointer to the offset within the current chunkset
+  @param  is_compare   flag indicating whether we should compare data or store
+                       it
+
+  @return  Status of comparison
+    @retval  non-zero  if comparison found data differences
+    @retval  zero      otherwise
+*/
+
+static inline uint
+hp_process_field_data_to_chunkset(HP_SHARE *info, const uchar *data,
+                                  uint length, uchar **pos_ptr, uint *off_ptr,
+                                  uint is_compare)
+{
+  uint to_copy;
+  uchar *curr_chunk= *pos_ptr;
+  uint dst_offset= *off_ptr;
+  uint rc= 1;
+
+  while (length > 0)
+  {
+
+    to_copy= info->recordspace.chunk_dataspace_length - dst_offset;
+    if (to_copy == 0)
+    {
+      /* Jump to the next chunk */
+#if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
+      dump_chunk(info, curr_chunk);
+#endif
+      curr_chunk= *((uchar **) (curr_chunk + info->recordspace.offset_link));
+      dst_offset= 0;
+      continue;
+    }
+
+    to_copy= min(length, to_copy);
+
+    if (is_compare)
+    {
+      if (memcmp(curr_chunk + dst_offset, data, (size_t) to_copy))
+      {
+        goto end;
+      }
+    }
+    else
+    {
+      memcpy(curr_chunk + dst_offset, data, (size_t) to_copy);
+    }
+
+    data+= to_copy;
+    dst_offset+= to_copy;
+    length-= to_copy;
+  }
+
+  rc= 0;
+
+end:
+  *pos_ptr= curr_chunk;
+  *off_ptr= dst_offset;
+
+  return rc;
+}
+
+/**
+  Encodes or compares record
+
+  Copies data from original unpacked record into the preallocated chunkset,
+  or performs data comparison
+
+  @param  info         the hosting table
+  @param  record       the record in standard unpacked format
+  @param  pos          the target chunkset
+  @param  is_compare   flag indicating whether we should compare data or store
+                       it
+
+  @return  Status of comparison
+    @retval  non-zero  if comparison fond data differences
+    @retval  zero      otherwise
+*/
+
+uint hp_process_record_data_to_chunkset(HP_SHARE *info, const uchar *record,
+                                        uchar *pos, uint is_compare)
+{
+  uint i, dst_offset;
+  uchar *curr_chunk= pos;
+
+  if (is_compare)
+  {
+    if (memcmp(curr_chunk, record, (size_t) info->fixed_data_length))
+    {
+      return 1;
+    }
+  }
+  else
+  {
+    memcpy(curr_chunk, record, (size_t) info->fixed_data_length);
+  }
+
+  if (!info->recordspace.is_variable_size)
+  {
+    /* Nothing more to copy */
+    return 0;
+  }
+
+  dst_offset= info->fixed_data_length;
+
+  for (i= info->fixed_column_count; i < info->column_count; i++)
+  {
+    uint length;
+    const uchar *data;
+
+    HP_COLUMNDEF *column= info->column_defs + i;
+
+    if (column->null_bit)
+    {
+      if (record[column->null_pos] & column->null_bit)
+      {
+        /* Skip all NULL values */
+        continue;
+      }
+    }
+
+    data= record + column->offset;
+    if (column->type == MYSQL_TYPE_VARCHAR)
+    {
+      uint pack_length;
+
+      /* >= 5.0.3 true VARCHAR */
+
+      /* Make sure to copy length indicator and actuals string bytes */
+      pack_length= column->length_bytes;
+      length= pack_length + (pack_length == 1 ? (uint) *data : uint2korr(data));
+    }
+    else if (column->type == MYSQL_TYPE_BLOB)
+    {
+      uint pack_length;
+
+      pack_length= column->length_bytes;
+      /* Just want to store the length, so not interested in the return code */
+      (void) hp_process_field_data_to_chunkset(info, data, pack_length,
+                                               &curr_chunk, &dst_offset, 0);
+      length= hp_calc_blob_length(pack_length, data);
+      memcpy(&data, data + pack_length, sizeof(char *));
+    }
+    else
+    {
+      length= column->length;
+    }
+
+    if (hp_process_field_data_to_chunkset(info, data, length, &curr_chunk,
+                                          &dst_offset, is_compare))
+    {
+      return 1;
+    }
+  }
+
+#if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
+  dump_chunk(info, curr_chunk);
+#endif
+
+  return 0;
+}
+
+
+/**
+  Stores record in the heap table chunks
+
+  Copies data from original unpacked record into the preallocated chunkset
+
+  @param  info         the hosting table
+  @param  record       the record in standard unpacked format
+  @param  pos          the target chunkset
+*/
+
+void hp_copy_record_data_to_chunkset(HP_SHARE *info, const uchar *record,
+                                     uchar *pos)
+{
+  DBUG_ENTER("hp_copy_record_data_to_chunks");
+
+  hp_process_record_data_to_chunkset(info, record, pos, 0);
+
+  DBUG_VOID_RETURN;
+}
+
+
+/*
+  Macro to switch curr_chunk to the next chunk in the chunkset and reset
+  src_offset.
+*/
+#if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
+#define SWITCH_TO_NEXT_CHUNK_FOR_READ(share, curr_chunk, src_offset)     \
+  {                                                                     \
+    curr_chunk= *((uchar**) (curr_chunk + share->recordspace.offset_link)); \
+    src_offset= 0;                                                      \
+    dump_chunk(share, curr_chunk);                                       \
+  }
+#else
+#define SWITCH_TO_NEXT_CHUNK_FOR_READ(share, curr_chunk, src_offset)     \
+  {                                                                     \
+    curr_chunk= *((uchar**) (curr_chunk + share->recordspace.offset_link)); \
+    src_offset= 0;                                                      \
+  }
+#endif
+
+/**
+  Copies record data from storage to unpacked record format
+
+  Copies data from chunkset into its original unpacked record
+
+  @param       info         the hosting table
+  @param[out]  record       the target record in standard unpacked format
+  @param       pos          the source chunkset
+
+  @return                   Status of conversion
+    @retval   0             success
+    @retval   1             out of memory
+*/
+
+int hp_extract_record(HP_INFO *info, uchar *record, const uchar *pos)
+{
+  uint i, src_offset;
+  const uchar *curr_chunk= pos;
+  HP_SHARE *share= info->s;
+  uint *rec_offsets;
+  uint *buf_offsets;
+  uint nblobs= 0;
+  uint init_offset= share->blobs * sizeof(uint) * 2;
+
+  DBUG_ENTER("hp_extract_record");
+
+#if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
+  if (share->recordspace.is_variable_size)
+  {
+    dump_chunk(share, curr_chunk);
+  }
+#endif
+
+  memcpy(record, curr_chunk, (size_t) share->fixed_data_length);
+
+  if (!share->recordspace.is_variable_size)
+  {
+    /* Nothing more to copy */
+    DBUG_RETURN(0);
+  }
+
+  /* Reserve space for rec_offsets and buf_offsets.*/
+  info->blob_offset= init_offset;
+  src_offset= share->fixed_data_length;
+
+  for (i= share->fixed_column_count; i < share->column_count; i++)
+  {
+    uint length, is_null= 0;
+    uchar *to;
+
+    HP_COLUMNDEF *column= share->column_defs + i;
+
+    if (column->null_bit)
+    {
+      if (record[column->null_pos] & column->null_bit)
+      {
+        is_null= 1;
+      }
+    }
+
+    if (is_null)
+    {
+      /* TODO: is memset really needed? */
+      memset(record + column->offset, 0, column->length);
+      continue;
+    }
+
+    to= record + column->offset;
+    if (column->type == MYSQL_TYPE_VARCHAR || column->type == MYSQL_TYPE_BLOB)
+    {
+      uint pack_length, i;
+      uchar *tmp= to;
+
+      pack_length= column->length_bytes;
+
+      for (i= 0; i < pack_length; i++)
+      {
+        if (src_offset == share->recordspace.chunk_dataspace_length)
+        {
+          SWITCH_TO_NEXT_CHUNK_FOR_READ(share, curr_chunk, src_offset);
+        }
+        *to++= curr_chunk[src_offset++];
+      }
+      /*
+        We copy byte-by-byte and then use hp_calc_blob_length to combine bytes
+        in the right order.
+      */
+      length= hp_calc_blob_length(pack_length, tmp);
+
+      if (column->type == MYSQL_TYPE_BLOB && length == 0)
+      {
+        /*
+          Store a zero pointer for zero-length BLOBs because the server
+          relies on that (see Field_blob::val_*().
+        */
+        *(uchar **) to= 0;
+      }
+      else if (column->type == MYSQL_TYPE_BLOB && length > 0)
+      {
+        uint newsize= info->blob_offset + length;
+
+        DBUG_ASSERT(share->blobs > 0);
+        /*
+          Make sure we have enough space in blob_buffer and store the pointer
+          to this blob in record.
+        */
+        if (info->blob_size < newsize)
+        {
+          uchar *ptr;
+          ptr= my_realloc(info->blob_buffer, newsize, MYF(MY_ALLOW_ZERO_PTR));
+          if (ptr == NULL)
+          {
+            DBUG_RETURN(1);
+          }
+
+          if (info->blob_buffer == NULL)
+          {
+            memset(ptr, 0, init_offset);
+          }
+          info->blob_buffer= ptr;
+          info->blob_size= newsize;
+        }
+
+        rec_offsets= (uint *) info->blob_buffer;
+        buf_offsets= rec_offsets + share->blobs;
+
+        rec_offsets[nblobs]= (uint) (to - record);
+        buf_offsets[nblobs]= info->blob_offset;
+        nblobs++;
+
+        /* Change 'to' so blob data is copied into blob_buffer */
+        to= info->blob_buffer + info->blob_offset;
+        info->blob_offset= newsize;
+      }
+    }
+    else
+    {
+      length= column->length;
+    }
+
+    while (length > 0)
+    {
+      uint to_copy;
+
+      to_copy= share->recordspace.chunk_dataspace_length - src_offset;
+      if (to_copy == 0)
+      {
+        SWITCH_TO_NEXT_CHUNK_FOR_READ(share, curr_chunk, src_offset);
+        to_copy= share->recordspace.chunk_dataspace_length;
+      }
+
+      to_copy= min(length, to_copy);
+
+      memcpy(to, curr_chunk + src_offset, (size_t) to_copy);
+      src_offset+= to_copy;
+      to+= to_copy;
+      length-= to_copy;
+    }
+  }
+
+  /* Store pointers to blob data in record */
+  for (i= 0; i < nblobs; i++)
+  {
+    *(uchar **) (record + rec_offsets[i]) = info->blob_buffer + buf_offsets[i];
+  }
+
+  DBUG_RETURN(0);
+}
diff -ruN a/storage/heap/hp_rfirst.c b/storage/heap/hp_rfirst.c
--- a/storage/heap/hp_rfirst.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_rfirst.c	2011-06-02 15:38:02.764917001 +0300
@@ -34,7 +34,10 @@
       memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos), 
 	     sizeof(uchar*));
       info->current_ptr = pos;
-      memcpy(record, pos, (size_t)share->reclength);
+      if (hp_extract_record(info, record, pos))
+      {
+        DBUG_RETURN(my_errno);
+      }
       /*
         If we're performing index_first on a table that was taken from
         table cache, info->lastkey_len is initialized to previous query.
diff -ruN a/storage/heap/hp_rkey.c b/storage/heap/hp_rkey.c
--- a/storage/heap/hp_rkey.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_rkey.c	2011-06-02 15:38:02.764917001 +0300
@@ -66,7 +66,10 @@
     if (!(keyinfo->flag & HA_NOSAME))
       memcpy(info->lastkey, key, (size_t) keyinfo->length);
   }
-  memcpy(record, pos, (size_t) share->reclength);
+  if (hp_extract_record(info, record, pos))
+  {
+    DBUG_RETURN(my_errno);
+  }
   info->update= HA_STATE_AKTIV;
   DBUG_RETURN(0);
 }
diff -ruN a/storage/heap/hp_rlast.c b/storage/heap/hp_rlast.c
--- a/storage/heap/hp_rlast.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_rlast.c	2011-06-02 15:38:02.764917001 +0300
@@ -35,7 +35,10 @@
       memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos), 
 	     sizeof(uchar*));
       info->current_ptr = pos;
-      memcpy(record, pos, (size_t)share->reclength);
+      if (hp_extract_record(info, record, pos))
+      {
+        DBUG_RETURN(my_errno);
+      }
       info->update = HA_STATE_AKTIV;
     }
     else
diff -ruN a/storage/heap/hp_rnext.c b/storage/heap/hp_rnext.c
--- a/storage/heap/hp_rnext.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_rnext.c	2011-06-02 15:38:02.764917001 +0300
@@ -109,7 +109,10 @@
       my_errno=HA_ERR_END_OF_FILE;
     DBUG_RETURN(my_errno);
   }
-  memcpy(record,pos,(size_t) share->reclength);
+  if (hp_extract_record(info, record, pos))
+  {
+    DBUG_RETURN(my_errno);
+  }
   info->update=HA_STATE_AKTIV | HA_STATE_NEXT_FOUND;
   DBUG_RETURN(0);
 }
diff -ruN a/storage/heap/hp_rprev.c b/storage/heap/hp_rprev.c
--- a/storage/heap/hp_rprev.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_rprev.c	2011-06-02 15:38:02.764917001 +0300
@@ -77,7 +77,10 @@
       my_errno=HA_ERR_END_OF_FILE;
     DBUG_RETURN(my_errno);
   }
-  memcpy(record,pos,(size_t) share->reclength);
+  if (hp_extract_record(info, record, pos))
+  {
+    DBUG_RETURN(my_errno);
+  }
   info->update=HA_STATE_AKTIV | HA_STATE_PREV_FOUND;
   DBUG_RETURN(0);
 }
diff -ruN a/storage/heap/hp_rrnd.c b/storage/heap/hp_rrnd.c
--- a/storage/heap/hp_rrnd.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_rrnd.c	2011-06-02 15:38:02.764917001 +0300
@@ -36,13 +36,18 @@
     info->update= 0;
     DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
   }
-  if (!info->current_ptr[share->reclength])
+  if (get_chunk_status(&share->recordspace, info->current_ptr) !=
+      CHUNK_STATUS_ACTIVE)
   {
+    /* treat deleted and linked chunks as deleted */
     info->update= HA_STATE_PREV_FOUND | HA_STATE_NEXT_FOUND;
     DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
   }
   info->update=HA_STATE_PREV_FOUND | HA_STATE_NEXT_FOUND | HA_STATE_AKTIV;
-  memcpy(record,info->current_ptr,(size_t) share->reclength);
+  if (hp_extract_record(info, record, info->current_ptr))
+  {
+    DBUG_RETURN(my_errno);
+  }
   DBUG_PRINT("exit", ("found record at 0x%lx", (long) info->current_ptr));
   info->current_hash_ptr=0;			/* Can't use rnext */
   DBUG_RETURN(0);
@@ -70,17 +75,17 @@
   {
     pos= ++info->current_record;
     if (pos % share->block.records_in_block &&	/* Quick next record */
-	pos < share->records+share->deleted &&
-	(info->update & HA_STATE_PREV_FOUND))
+        pos < share->used_chunk_count + share->deleted_chunk_count &&
+        (info->update & HA_STATE_PREV_FOUND))
     {
-      info->current_ptr+=share->block.recbuffer;
+      info->current_ptr+= share->block.recbufferlen;
       goto end;
     }
   }
   else
     info->current_record=pos;
 
-  if (pos >= share->records+share->deleted)
+  if (pos >= share->used_chunk_count + share->deleted_chunk_count)
   {
     info->update= 0;
     DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
@@ -90,13 +95,17 @@
   hp_find_record(info, pos);
 
 end:
-  if (!info->current_ptr[share->reclength])
+  if (GET_CHUNK_STATUS(info, info->current_ptr) != CHUNK_STATUS_ACTIVE)
   {
+    /* treat deleted and linked chunks as deleted */
     info->update= HA_STATE_PREV_FOUND | HA_STATE_NEXT_FOUND;
     DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
   }
   info->update=HA_STATE_PREV_FOUND | HA_STATE_NEXT_FOUND | HA_STATE_AKTIV;
-  memcpy(record,info->current_ptr,(size_t) share->reclength);
+  if (hp_extract_record(info, record, info->current_ptr))
+  {
+    DBUG_RETURN(my_errno);
+  }
   DBUG_PRINT("exit",("found record at 0x%lx",info->current_ptr));
   info->current_hash_ptr=0;			/* Can't use rnext */
   DBUG_RETURN(0);
diff -ruN a/storage/heap/hp_rsame.c b/storage/heap/hp_rsame.c
--- a/storage/heap/hp_rsame.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_rsame.c	2011-06-02 15:38:02.764917001 +0300
@@ -31,7 +31,8 @@
   DBUG_ENTER("heap_rsame");
 
   test_active(info);
-  if (info->current_ptr[share->reclength])
+  if (get_chunk_status(&share->recordspace, info->current_ptr) ==
+      CHUNK_STATUS_ACTIVE)
   {
     if (inx < -1 || inx >= (int) share->keys)
     {
@@ -47,9 +48,15 @@
 	DBUG_RETURN(my_errno);
       }
     }
-    memcpy(record,info->current_ptr,(size_t) share->reclength);
+    if (hp_extract_record(info, record, info->current_ptr))
+    {
+      DBUG_RETURN(my_errno);
+    }
     DBUG_RETURN(0);
   }
+
+  /* treat deleted and linked chunks as deleted */
+
   info->update=0;
 
   DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
diff -ruN a/storage/heap/hp_scan.c b/storage/heap/hp_scan.c
--- a/storage/heap/hp_scan.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_scan.c	2011-06-02 15:38:02.774917002 +0300
@@ -30,7 +30,6 @@
   info->lastinx= -1;
   info->current_record= (ulong) ~0L;		/* No current record */
   info->update=0;
-  info->next_block=0;
   DBUG_RETURN(0);
 }
 
@@ -41,32 +40,26 @@
   DBUG_ENTER("heap_scan");
 
   pos= ++info->current_record;
-  if (pos < info->next_block)
+  if (pos >= share->recordspace.chunk_count)
   {
-    info->current_ptr+=share->block.recbuffer;
+    info->update= 0;
+    DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
   }
-  else
-  {
-    info->next_block+=share->block.records_in_block;
-    if (info->next_block >= share->records+share->deleted)
-    {
-      info->next_block= share->records+share->deleted;
-      if (pos >= info->next_block)
-      {
-	info->update= 0;
-	DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
-      }
-    }
-    hp_find_record(info, pos);
-  }
-  if (!info->current_ptr[share->reclength])
+
+  hp_find_record(info, pos);
+
+  if (get_chunk_status(&share->recordspace, info->current_ptr) !=
+      CHUNK_STATUS_ACTIVE)
   {
-    DBUG_PRINT("warning",("Found deleted record"));
+    DBUG_PRINT("warning",("Found deleted record or secondary chunk"));
     info->update= HA_STATE_PREV_FOUND | HA_STATE_NEXT_FOUND;
     DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
   }
   info->update= HA_STATE_PREV_FOUND | HA_STATE_NEXT_FOUND | HA_STATE_AKTIV;
-  memcpy(record,info->current_ptr,(size_t) share->reclength);
+  if (hp_extract_record(info, record, info->current_ptr))
+  {
+    DBUG_RETURN(my_errno);
+  }
   info->current_hash_ptr=0;			/* Can't use read_next */
   DBUG_RETURN(0);
 } /* heap_scan */
diff -ruN a/storage/heap/hp_test1.c b/storage/heap/hp_test1.c
--- a/storage/heap/hp_test1.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_test1.c	2011-06-02 15:38:02.774917002 +0300
@@ -22,6 +22,7 @@
 #include <my_global.h>
 #include <my_sys.h>
 #include <m_string.h>
+#include <mysql_com.h>
 #include "heap.h"
 
 static int get_options(int argc, char *argv[]);
@@ -35,6 +36,7 @@
   uchar record[128],key[32];
   const char *filename;
   HP_KEYDEF keyinfo[10];
+  HP_COLUMNDEF columndef[2];
   HA_KEYSEG keyseg[4];
   HP_CREATE_INFO hp_create_info;
   HP_SHARE *tmp_share;
@@ -51,6 +53,10 @@
   hp_create_info.reclength= 30;
   hp_create_info.max_records= (ulong) flag*100000L;
   hp_create_info.min_records= 10UL;
+  hp_create_info.columns= 2;
+  hp_create_info.columndef= columndef;
+  hp_create_info.fixed_key_fieldnr= 30;
+  hp_create_info.fixed_data_size= sizeof(char*) * 2;
 
   keyinfo[0].keysegs=1;
   keyinfo[0].seg=keyseg;
@@ -62,11 +68,20 @@
   keyinfo[0].seg[0].null_bit= 0;
   keyinfo[0].flag = HA_NOSAME;
 
+  memset(columndef, 0, 2 * sizeof(HP_COLUMNDEF));
+  columndef[0].type= MYSQL_TYPE_STRING;
+  columndef[0].offset= 1;
+  columndef[0].length= 6;
+  columndef[1].type= MYSQL_TYPE_STRING;
+  columndef[1].offset= 7;
+  columndef[1].length= 23;
+
   deleted=0;
   bzero((uchar*) flags,sizeof(flags));
 
   printf("- Creating heap-file\n");
-  if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
+  if (heap_create(filename, &hp_create_info,
+                  &tmp_share, &unused) ||
       !(file= heap_open(filename, 2)))
     goto err;
   printf("- Writing records:s\n");
diff -ruN a/storage/heap/hp_test2.c b/storage/heap/hp_test2.c
--- a/storage/heap/hp_test2.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_test2.c	2011-06-02 15:38:02.774917002 +0300
@@ -18,6 +18,7 @@
 
 #include "heapdef.h"		/* Because of hp_find_block */
 #include <signal.h>
+#include <mysql_com.h>
 
 #define MAX_RECORDS 100000
 #define MAX_KEYS 4
@@ -44,6 +45,7 @@
   register uint i,j;
   uint ant,n1,n2,n3;
   uint write_count,update,opt_delete,check2,dupp_keys,found_key;
+  uint mem_per_keys;
   int error;
   ulong pos;
   unsigned long key_check;
@@ -53,6 +55,7 @@
   HP_SHARE *tmp_share;
   HP_KEYDEF keyinfo[MAX_KEYS];
   HA_KEYSEG keyseg[MAX_KEYS*5];
+  HP_COLUMNDEF columndef[4];
   HEAP_PTR UNINIT_VAR(position);
   HP_CREATE_INFO hp_create_info;
   CHARSET_INFO *cs= &my_charset_latin1;
@@ -65,12 +68,16 @@
   get_options(argc,argv);
 
   bzero(&hp_create_info, sizeof(hp_create_info));
-  hp_create_info.max_table_size= 1024L*1024L;
+  hp_create_info.max_table_size= 1024L*1024L*1024L;
   hp_create_info.keys= keys;
   hp_create_info.keydef= keyinfo;
   hp_create_info.reclength= reclength;
   hp_create_info.max_records= (ulong) flag*100000L;
   hp_create_info.min_records= (ulong) recant/2;
+  hp_create_info.columns= 4;
+  hp_create_info.columndef= columndef;
+  hp_create_info.fixed_key_fieldnr= 4;
+  hp_create_info.fixed_data_size= 39;
 
   write_count=update=opt_delete=0;
   key_check=0;
@@ -118,11 +125,30 @@
   keyinfo[3].seg[0].null_pos=38;
   keyinfo[3].seg[0].charset=cs;
 
+  memset(columndef, 0, 4 * sizeof(HP_COLUMNDEF));
+  columndef[0].type= MYSQL_TYPE_STRING;
+  columndef[0].offset= 0;
+  columndef[0].length= 6;
+  columndef[1].type= MYSQL_TYPE_STRING;
+  columndef[1].offset= 7;
+  columndef[1].length= 6;
+  columndef[2].type= MYSQL_TYPE_STRING;
+  columndef[2].offset= 12;
+  columndef[2].length= 8;
+  columndef[3].type= MYSQL_TYPE_TINY;
+  columndef[3].offset= 37;
+  columndef[3].length= 1;
+  columndef[3].null_bit= 1;
+  columndef[3].null_pos= 38;
+
+  mem_per_keys= (sizeof(char*) * 2) * 4;
+
   bzero((char*) key1,sizeof(key1));
   bzero((char*) key3,sizeof(key3));
 
   printf("- Creating heap-file\n");
-  if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
+  if (heap_create(filename, &hp_create_info,
+                  &tmp_share, &unused) ||
       !(file= heap_open(filename, 2)))
     goto err;
   signal(SIGINT,endprog);
diff -ruN a/storage/heap/hp_write.c b/storage/heap/hp_write.c
--- a/storage/heap/hp_write.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_write.c	2011-06-02 15:38:02.774917002 +0300
@@ -25,7 +25,6 @@
 #define HIGHFIND 4
 #define HIGHUSED 8
 
-static uchar *next_free_record_pos(HP_SHARE *info);
 static HASH_INFO *hp_find_free_hash(HP_SHARE *info, HP_BLOCK *block,
 				     ulong records);
 
@@ -34,6 +33,8 @@
   HP_KEYDEF *keydef, *end;
   uchar *pos;
   HP_SHARE *share=info->s;
+  uint rec_length, chunk_count;
+
   DBUG_ENTER("heap_write");
 #ifndef DBUG_OFF
   if (info->mode & O_RDONLY)
@@ -41,7 +42,18 @@
     DBUG_RETURN(my_errno=EACCES);
   }
 #endif
-  if (!(pos=next_free_record_pos(share)))
+
+  if ((share->records >= share->max_records && share->max_records) ||
+      (share->recordspace.total_data_length + share->index_length >=
+       share->max_table_size))
+  {
+    my_errno= HA_ERR_RECORD_FILE_FULL;
+    DBUG_RETURN(my_errno);
+  }
+
+  rec_length= hp_get_encoded_data_length(share, record, &chunk_count);
+
+  if (!(pos= hp_allocate_chunkset(&share->recordspace, chunk_count)))
     DBUG_RETURN(my_errno);
   share->changed=1;
 
@@ -52,8 +64,8 @@
       goto err;
   }
 
-  memcpy(pos,record,(size_t) share->reclength);
-  pos[share->reclength]=1;		/* Mark record as not deleted */
+  hp_copy_record_data_to_chunkset(share, record, pos);
+
   if (++share->records == share->blength)
     share->blength+= share->blength;
   info->current_ptr=pos;
@@ -87,10 +99,7 @@
     keydef--;
   } 
 
-  share->deleted++;
-  *((uchar**) pos)=share->del_link;
-  share->del_link=pos;
-  pos[share->reclength]=0;			/* Record deleted */
+  hp_free_chunks(&share->recordspace, pos);
 
   DBUG_RETURN(my_errno);
 } /* heap_write */
@@ -106,7 +115,8 @@
   uint old_allocated;
 
   custom_arg.keyseg= keyinfo->seg;
-  custom_arg.key_length= hp_rb_make_key(keyinfo, info->recbuf, record, recpos);
+  custom_arg.key_length= hp_rb_make_key(keyinfo, info->recbuf, record, recpos,
+                                        FALSE);
   if (keyinfo->flag & HA_NOSAME)
   {
     custom_arg.search_flag= SEARCH_FIND | SEARCH_UPDATE;
@@ -128,42 +138,6 @@
   return 0;
 }
 
-	/* Find where to place new record */
-
-static uchar *next_free_record_pos(HP_SHARE *info)
-{
-  int block_pos;
-  uchar *pos;
-  size_t length;
-  DBUG_ENTER("next_free_record_pos");
-
-  if (info->del_link)
-  {
-    pos=info->del_link;
-    info->del_link= *((uchar**) pos);
-    info->deleted--;
-    DBUG_PRINT("exit",("Used old position: 0x%lx",(long) pos));
-    DBUG_RETURN(pos);
-  }
-  if (!(block_pos=(info->records % info->block.records_in_block)))
-  {
-    if ((info->records > info->max_records && info->max_records) ||
-        (info->data_length + info->index_length >= info->max_table_size))
-    {
-      my_errno=HA_ERR_RECORD_FILE_FULL;
-      DBUG_RETURN(NULL);
-    }
-    if (hp_get_new_block(&info->block,&length))
-      DBUG_RETURN(NULL);
-    info->data_length+=length;
-  }
-  DBUG_PRINT("exit",("Used new position: 0x%lx",
-		     (long) ((uchar*) info->block.level_info[0].last_blocks+
-                             block_pos * info->block.recbuffer)));
-  DBUG_RETURN((uchar*) info->block.level_info[0].last_blocks+
-	      block_pos*info->block.recbuffer);
-}
-
 
 /*
   Write a hash-key to the hash-index
diff -ruN a/storage/heap/hp_update.c b/storage/heap/hp_update.c
--- a/storage/heap/hp_update.c	2011-04-11 13:44:03.000000000 +0300
+++ b/storage/heap/hp_update.c	2011-05-26 09:07:47.605561000 +0300
@@ -17,43 +17,66 @@
 
 #include "heapdef.h"
 
-int heap_update(HP_INFO *info, const uchar *old, const uchar *heap_new)
+int heap_update(HP_INFO *info, const uchar *old_record, const uchar *new_record)
 {
   HP_KEYDEF *keydef, *end, *p_lastinx;
   uchar *pos;
   my_bool auto_key_changed= 0;
   HP_SHARE *share= info->s;
+  uint old_length, new_length;
+  uint old_chunk_count, new_chunk_count;
+
   DBUG_ENTER("heap_update");
 
   test_active(info);
   pos=info->current_ptr;
 
-  if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,old))
+  if (info->opt_flag & READ_CHECK_USED && hp_rectest(info, old_record))
     DBUG_RETURN(my_errno);				/* Record changed */
+
+  old_length = hp_get_encoded_data_length(share, old_record, &old_chunk_count);
+  new_length = hp_get_encoded_data_length(share, new_record, &new_chunk_count);
+
+  if (new_chunk_count > old_chunk_count)
+  {
+    /* extend the old chunkset size as necessary, but do not shrink yet */
+    if (hp_reallocate_chunkset(&share->recordspace, new_chunk_count, pos))
+    {
+      DBUG_RETURN(my_errno); /* Out of memory or table space */
+    }
+  }
+
   if (--(share->records) < share->blength >> 1) share->blength>>= 1;
   share->changed=1;
 
   p_lastinx= share->keydef + info->lastinx;
   for (keydef= share->keydef, end= keydef + share->keys; keydef < end; keydef++)
   {
-    if (hp_rec_key_cmp(keydef, old, heap_new, 0))
+    if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
     {
-      if ((*keydef->delete_key)(info, keydef, old, pos, keydef == p_lastinx) ||
-          (*keydef->write_key)(info, keydef, heap_new, pos))
+      if ((*keydef->delete_key)(info, keydef, old_record, pos,
+                                keydef == p_lastinx) ||
+          (*keydef->write_key)(info, keydef, new_record, pos))
         goto err;
       if (share->auto_key == (uint) (keydef - share->keydef + 1))
         auto_key_changed= 1;
     }
   }
 
-  memcpy(pos,heap_new,(size_t) share->reclength);
+  hp_copy_record_data_to_chunkset(share, new_record, pos);
   if (++(share->records) == share->blength) share->blength+= share->blength;
 
+  if (new_chunk_count < old_chunk_count)
+  {
+    /* Shrink the chunkset to its new size */
+    hp_reallocate_chunkset(&share->recordspace, new_chunk_count, pos);
+  }
+
 #if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
   DBUG_EXECUTE("check_heap",heap_check_heap(info, 0););
 #endif
   if (auto_key_changed)
-    heap_update_auto_increment(info, heap_new);
+    heap_update_auto_increment(info, new_record);
   DBUG_RETURN(0);
 
  err:
@@ -63,7 +86,7 @@
     if (keydef->algorithm == HA_KEY_ALG_BTREE)
     {
       /* we don't need to delete non-inserted key from rb-tree */
-      if ((*keydef->write_key)(info, keydef, old, pos))
+      if ((*keydef->write_key)(info, keydef, old_record, pos))
       {
         if (++(share->records) == share->blength)
 	  share->blength+= share->blength;
@@ -73,10 +96,10 @@
     }
     while (keydef >= share->keydef)
     {
-      if (hp_rec_key_cmp(keydef, old, heap_new, 0))
+      if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
       {
-	if ((*keydef->delete_key)(info, keydef, heap_new, pos, 0) ||
-	    (*keydef->write_key)(info, keydef, old, pos))
+        if ((*keydef->delete_key)(info, keydef, new_record, pos, 0) ||
+            (*keydef->write_key)(info, keydef, old_record, pos))
 	  break;
       }
       keydef--;
@@ -84,5 +107,12 @@
   }
   if (++(share->records) == share->blength)
     share->blength+= share->blength;
+
+  if (new_chunk_count > old_chunk_count)
+  {
+    /* Shrink the chunkset to its original size */
+    hp_reallocate_chunkset(&share->recordspace, old_chunk_count, pos);
+  }
+
   DBUG_RETURN(my_errno);
 } /* heap_update */