@@ -1135,28 +1135,12 @@ toast_extract_chunk_fields(Relation toastrel, TupleDesc toasttupDesc,
11351135 }
11361136}
11371137
1138- /* ----------
1139- * create_fetch_datum_iterator -
1140- *
1141- * Initialize fetch datum iterator.
1142- * ----------
1143- */
1144- FetchDatumIterator
1145- create_fetch_datum_iterator (struct varlena * attr )
1138+ static void
1139+ create_fetch_datum_iterator_scan (FetchDatumIterator iter )
11461140{
11471141 int validIndex ;
1148- FetchDatumIterator iter ;
1149-
1150- if (!VARATT_IS_EXTERNAL_ONDISK (attr ))
1151- elog (ERROR , "create_fetch_datum_iterator shouldn't be called for non-ondisk datums" );
1152-
1153- iter = (FetchDatumIterator ) palloc0 (sizeof (FetchDatumIteratorData ));
1154-
1155- /* Must copy to access aligned fields */
1156- VARATT_EXTERNAL_GET_POINTER (iter -> toast_pointer , attr );
11571142
1158- iter -> ressize = VARATT_EXTERNAL_GET_EXTSIZE (iter -> toast_pointer );
1159- iter -> numchunks = ((iter -> ressize - 1 ) / TOAST_MAX_CHUNK_SIZE ) + 1 ;
1143+ MemoryContext oldcxt = MemoryContextSwitchTo (iter -> mcxt );
11601144
11611145 /*
11621146 * Open the toast relation and its indexes
@@ -1189,6 +1173,33 @@ create_fetch_datum_iterator(struct varlena *attr)
11891173 iter -> toastscan = systable_beginscan_ordered (iter -> toastrel , iter -> toastidxs [validIndex ],
11901174 & iter -> snapshot , 1 , & iter -> toastkey );
11911175
1176+ MemoryContextSwitchTo (oldcxt );
1177+ }
1178+
1179+ /* ----------
1180+ * create_fetch_datum_iterator -
1181+ *
1182+ * Initialize fetch datum iterator.
1183+ * ----------
1184+ */
1185+ FetchDatumIterator
1186+ create_fetch_datum_iterator (struct varlena * attr )
1187+ {
1188+ FetchDatumIterator iter ;
1189+
1190+ if (!VARATT_IS_EXTERNAL_ONDISK (attr ))
1191+ elog (ERROR , "create_fetch_datum_iterator shouldn't be called for non-ondisk datums" );
1192+
1193+ iter = (FetchDatumIterator ) palloc0 (sizeof (FetchDatumIteratorData ));
1194+
1195+ iter -> mcxt = CurrentMemoryContext ;
1196+
1197+ /* Must copy to access aligned fields */
1198+ VARATT_EXTERNAL_GET_POINTER (iter -> toast_pointer , attr );
1199+
1200+ iter -> ressize = VARATT_EXTERNAL_GET_EXTSIZE (iter -> toast_pointer );
1201+ iter -> numchunks = ((iter -> ressize - 1 ) / TOAST_MAX_CHUNK_SIZE ) + 1 ;
1202+
11921203 iter -> buf = create_toast_buffer (iter -> ressize + VARHDRSZ ,
11931204 VARATT_EXTERNAL_IS_COMPRESSED (iter -> toast_pointer ));
11941205
@@ -1204,7 +1215,7 @@ free_fetch_datum_iterator(FetchDatumIterator iter)
12041215 if (iter == NULL )
12051216 return ;
12061217
1207- if (!iter -> done )
1218+ if (!iter -> done && iter -> toastscan )
12081219 {
12091220 systable_endscan_ordered (iter -> toastscan );
12101221 toast_close_indexes (iter -> toastidxs , iter -> num_indexes , AccessShareLock );
@@ -1236,6 +1247,9 @@ fetch_datum_iterate(FetchDatumIterator iter)
12361247
12371248 Assert (iter != NULL && !iter -> done );
12381249
1250+ if (!iter -> toastscan )
1251+ create_fetch_datum_iterator_scan (iter );
1252+
12391253 ttup = systable_getnext_ordered (iter -> toastscan , ForwardScanDirection );
12401254 if (ttup == NULL )
12411255 {
@@ -1344,8 +1358,9 @@ ToastBuffer *
13441358create_toast_buffer (int32 size , bool compressed )
13451359{
13461360 ToastBuffer * buf = (ToastBuffer * ) palloc0 (sizeof (ToastBuffer ));
1347- buf -> buf = (const char * ) palloc0 (size );
1348- if (compressed ) {
1361+ buf -> buf = (const char * ) palloc (size );
1362+ if (compressed )
1363+ {
13491364 SET_VARSIZE_COMPRESSED (buf -> buf , size );
13501365 /*
13511366 * Note the constraint buf->position <= buf->limit may be broken
@@ -1375,6 +1390,7 @@ free_toast_buffer(ToastBuffer *buf)
13751390 pfree (buf );
13761391}
13771392
1393+ #if 0
13781394/* ----------
13791395 * pglz_decompress_iterate -
13801396 *
@@ -1392,7 +1408,7 @@ free_toast_buffer(ToastBuffer *buf)
13921408 */
13931409void
13941410pglz_decompress_iterate (ToastBuffer * source , ToastBuffer * dest ,
1395- DetoastIterator iter , unsigned char * destend )
1411+ DetoastIterator iter , const char * destend )
13961412{
13971413 const unsigned char * sp ;
13981414 const unsigned char * srcend ;
@@ -1525,3 +1541,4 @@ pglz_decompress_iterate(ToastBuffer *source, ToastBuffer *dest,
15251541 source -> position = (char * ) sp ;
15261542 dest -> limit = (char * ) dp ;
15271543}
1544+ #endif
0 commit comments