forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit3f8c8e3
committed
Fix failure to detoast fields in composite elements of structured types.
If we have an array of records stored on disk, the individual record fieldscannot contain out-of-line TOAST pointers: the tuptoaster.c mechanisms areonly prepared to deal with TOAST pointers appearing in top-level fields ofa stored row. The same applies for ranges over composite types, nestedcomposites, etc. However, the existing code only took care of expandingsub-field TOAST pointers for the case of nested composites, not for otherstructured types containing composites. For example, given a command suchasUPDATE tab SET arraycol = ARRAY[(ROW(x,42)::mycompositetype] ...where x is a direct reference to a field of an on-disk tuple, if that fieldis long enough to be toasted out-of-line then the TOAST pointer would beinserted as-is into the array column. If the source record for x is laterdeleted, the array field value would become a dangling pointer, leadingto errors along the line of "missing chunk number 0 for toast value ..."when the value is referenced. A reproducible test case for this wasprovided by Jan Pecek, but it seems likely that some of the "missing chunknumber" reports we've heard in the past were caused by similar issues.Code-wise, the problem is that PG_DETOAST_DATUM() is not adequate toproduce a self-contained Datum value if the Datum is of composite type.Seen in this light, the problem is not just confined to arrays and ranges,but could also affect some other places where detoasting is done in thatway, for example form_index_tuple().I tried teaching the array code to apply toast_flatten_tuple_attribute()along with PG_DETOAST_DATUM() when the array element type is composite,but this was messy and imposed extra cache lookup costs whether or not anyTOAST pointers were present, indeed sometimes when the array element typeisn't even composite (since sometimes it takes a typcache lookup to findthat out). The idea of extending that approach to all the places thatcurrently use PG_DETOAST_DATUM() wasn't attractive at all.This patch instead solves the problem by decreeing that composite Datumvalues must not contain any out-of-line TOAST pointers in the first place;that is, we expand out-of-line fields at the point of constructing acomposite Datum, not at the point where we're about to insert it into alarger tuple. This rule is applied only to true composite Datums, notto tuples that are being passed around the system as tuples, so it's notas invasive as it might sound at first. With this approach, the amountof code that has to be touched for a full solution is greatly reduced,and added cache lookup costs are avoided except when there actually isa TOAST pointer that needs to be inlined.The main drawback of this approach is that we might sometimes dereferencea TOAST pointer that will never actually be used by the query, imposing arather large cost that wasn't there before. On the other side of the coin,if the field value is used multiple times then we'll come out ahead byavoiding repeat detoastings. Experimentation suggests that common SQLcoding patterns are unaffected either way, though. Applications that arevery negatively affected could be advised to modify their code to not fetchcolumns they won't be using.In future, we might consider reverting this solution in favor of detoastingonly at the point where data is about to be stored to disk, using somemethod that can drill down into multiple levels of nested structured types.That will require defining new APIs for structured types, though, so itdoesn't seem feasible as a back-patchable fix.Note that this patch changes HeapTupleGetDatum() from a macro to a functioncall; this means that any third-party code using that macro will not getprotection against creating TOAST-pointer-containing Datums until it'srecompiled. The same applies to any uses of PG_RETURN_HEAPTUPLEHEADER().It seems likely that this is not a big problem in practice: most of thetuple-returning functions in core and contrib produce outputs that couldnot possibly be toasted anyway, and the same probably holds for third-partyextensions.This bug has existed since TOAST was invented, so back-patch to allsupported branches.1 parent65fb5ff commit3f8c8e3
File tree
16 files changed
+245
-151
lines changed- src
- backend
- access
- common
- heap
- executor
- utils/adt
- include
- access
- pl/plpgsql/src
- test/regress
- expected
- sql
16 files changed
+245
-151
lines changedLines changed: 43 additions & 37 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
617 | 617 |
| |
618 | 618 |
| |
619 | 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 | + | |
620 | 655 |
| |
621 | 656 |
| |
622 | 657 |
| |
| |||
635 | 670 |
| |
636 | 671 |
| |
637 | 672 |
| |
638 |
| - | |
639 | 673 |
| |
640 | 674 |
| |
641 | 675 |
| |
| |||
646 | 680 |
| |
647 | 681 |
| |
648 | 682 |
| |
649 |
| - | |
650 |
| - | |
651 |
| - | |
652 |
| - | |
653 |
| - | |
654 |
| - | |
655 |
| - | |
656 |
| - | |
657 |
| - | |
| 683 | + | |
658 | 684 |
| |
659 | 685 |
| |
660 | 686 |
| |
661 | 687 |
| |
662 |
| - | |
663 |
| - | |
664 |
| - | |
665 |
| - | |
666 |
| - | |
667 | 688 |
| |
668 |
| - | |
669 |
| - | |
670 |
| - | |
| 689 | + | |
| 690 | + | |
671 | 691 |
| |
672 | 692 |
| |
673 | 693 |
| |
| |||
697 | 717 |
| |
698 | 718 |
| |
699 | 719 |
| |
700 |
| - | |
| 720 | + | |
| 721 | + | |
701 | 722 |
| |
702 | 723 |
| |
703 | 724 |
| |
| |||
1389 | 1410 |
| |
1390 | 1411 |
| |
1391 | 1412 |
| |
1392 |
| - | |
1393 | 1413 |
| |
1394 | 1414 |
| |
1395 | 1415 |
| |
| |||
1400 | 1420 |
| |
1401 | 1421 |
| |
1402 | 1422 |
| |
1403 |
| - | |
1404 |
| - | |
1405 |
| - | |
1406 |
| - | |
1407 |
| - | |
1408 |
| - | |
1409 |
| - | |
1410 |
| - | |
1411 |
| - | |
| 1423 | + | |
1412 | 1424 |
| |
1413 | 1425 |
| |
1414 | 1426 |
| |
1415 | 1427 |
| |
1416 |
| - | |
1417 |
| - | |
1418 |
| - | |
1419 |
| - | |
1420 |
| - | |
1421 | 1428 |
| |
1422 |
| - | |
1423 |
| - | |
1424 |
| - | |
| 1429 | + | |
| 1430 | + | |
1425 | 1431 |
| |
1426 | 1432 |
| |
1427 | 1433 |
| |
|
Lines changed: 5 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
158 | 158 |
| |
159 | 159 |
| |
160 | 160 |
| |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
161 | 166 |
| |
162 | 167 |
| |
163 | 168 |
| |
|
Lines changed: 44 additions & 48 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
991 | 991 |
| |
992 | 992 |
| |
993 | 993 |
| |
| 994 | + | |
| 995 | + | |
| 996 | + | |
994 | 997 |
| |
995 | 998 |
| |
996 | 999 |
| |
| |||
1068 | 1071 |
| |
1069 | 1072 |
| |
1070 | 1073 |
| |
1071 |
| - | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
1072 | 1088 |
| |
1073 |
| - | |
1074 |
| - | |
1075 |
| - | |
1076 |
| - | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
1077 | 1096 |
| |
1078 |
| - | |
1079 |
| - | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
1080 | 1100 |
| |
1081 | 1101 |
| |
1082 | 1102 |
| |
1083 |
| - | |
1084 |
| - | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
1085 | 1106 |
| |
1086 |
| - | |
1087 |
| - | |
1088 | 1107 |
| |
1089 | 1108 |
| |
1090 | 1109 |
| |
1091 | 1110 |
| |
1092 | 1111 |
| |
1093 |
| - | |
1094 |
| - | |
| 1112 | + | |
| 1113 | + | |
1095 | 1114 |
| |
1096 |
| - | |
1097 | 1115 |
| |
1098 | 1116 |
| |
1099 | 1117 |
| |
1100 | 1118 |
| |
1101 | 1119 |
| |
1102 |
| - | |
1103 |
| - | |
1104 |
| - | |
1105 |
| - | |
1106 |
| - | |
1107 |
| - | |
1108 |
| - | |
1109 |
| - | |
1110 |
| - | |
1111 |
| - | |
1112 |
| - | |
1113 |
| - | |
1114 |
| - | |
1115 |
| - | |
1116 |
| - | |
1117 |
| - | |
1118 | 1120 |
| |
1119 |
| - | |
| 1121 | + | |
1120 | 1122 |
| |
1121 | 1123 |
| |
1122 |
| - | |
| 1124 | + | |
1123 | 1125 |
| |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
1124 | 1129 |
| |
1125 | 1130 |
| |
1126 | 1131 |
| |
| |||
1144 | 1149 |
| |
1145 | 1150 |
| |
1146 | 1151 |
| |
1147 |
| - | |
1148 | 1152 |
| |
1149 | 1153 |
| |
1150 | 1154 |
| |
1151 | 1155 |
| |
1152 |
| - | |
1153 |
| - | |
1154 |
| - | |
1155 |
| - | |
1156 |
| - | |
1157 |
| - | |
1158 |
| - | |
1159 |
| - | |
1160 |
| - | |
1161 | 1156 |
| |
1162 | 1157 |
| |
1163 | 1158 |
| |
| |||
1166 | 1161 |
| |
1167 | 1162 |
| |
1168 | 1163 |
| |
1169 |
| - | |
| 1164 | + | |
1170 | 1165 |
| |
1171 | 1166 |
| |
1172 | 1167 |
| |
| |||
1178 | 1173 |
| |
1179 | 1174 |
| |
1180 | 1175 |
| |
1181 |
| - | |
| 1176 | + | |
1182 | 1177 |
| |
1183 | 1178 |
| |
1184 |
| - | |
1185 |
| - | |
| 1179 | + | |
| 1180 | + | |
1186 | 1181 |
| |
1187 |
| - | |
| 1182 | + | |
1188 | 1183 |
| |
| 1184 | + | |
| 1185 | + | |
1189 | 1186 |
| |
1190 | 1187 |
| |
1191 | 1188 |
| |
| |||
1202 | 1199 |
| |
1203 | 1200 |
| |
1204 | 1201 |
| |
1205 |
| - | |
1206 | 1202 |
| |
1207 | 1203 |
| |
1208 | 1204 |
| |
|
Lines changed: 9 additions & 23 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
896 | 896 |
| |
897 | 897 |
| |
898 | 898 |
| |
899 |
| - | |
900 |
| - | |
901 | 899 |
| |
902 | 900 |
| |
903 | 901 |
| |
| |||
927 | 925 |
| |
928 | 926 |
| |
929 | 927 |
| |
930 |
| - | |
931 |
| - | |
932 |
| - | |
933 | 928 |
| |
934 |
| - | |
935 |
| - | |
| 929 | + | |
936 | 930 |
| |
937 |
| - | |
938 |
| - | |
939 |
| - | |
940 |
| - | |
| 931 | + | |
941 | 932 |
| |
942 | 933 |
| |
943 |
| - | |
944 |
| - | |
| 934 | + | |
| 935 | + | |
945 | 936 |
| |
946 | 937 |
| |
947 | 938 |
| |
948 | 939 |
| |
949 | 940 |
| |
950 | 941 |
| |
951 |
| - | |
952 |
| - | |
953 |
| - | |
954 |
| - | |
955 |
| - | |
956 | 942 |
| |
957 | 943 |
| |
958 | 944 |
| |
| |||
1029 | 1015 |
| |
1030 | 1016 |
| |
1031 | 1017 |
| |
1032 |
| - | |
1033 |
| - | |
| 1018 | + | |
1034 | 1019 |
| |
1035 |
| - | |
1036 |
| - | |
| 1020 | + | |
1037 | 1021 |
| |
1038 |
| - | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
1039 | 1025 |
| |
1040 | 1026 |
| |
1041 | 1027 |
| |
|
0 commit comments
Comments
(0)