@@ -2662,7 +2662,6 @@ jsonb_toaster_save_object(Relation rel, Oid toasterid, JsonContainer *root,
26622662 if (!jsonb_toast_fields )
26632663 goto exit ;
26642664
2665-
26662665 Datum value_to_toast ;
26672666 bool compress_chunks ;
26682667
@@ -3142,6 +3141,54 @@ jsonb_toaster_validate(Oid typeoid, char storage, char compression,
31423141 return ok ;
31433142}
31443143
3144+ static Datum
3145+ jsonb_toaster_toast2 (Relation rel , Oid toasterid , Datum val , Datum compressed_val ,
3146+ int max_inline_size , int options )
3147+ {
3148+ Datum toasted_val ;
3149+ bool compress_chunks = jsonb_compress_chunks ;
3150+
3151+ if (jsonb_direct_toast && VARSIZE_ANY (compressed_val ) / 1900 * 1 + TOAST_POINTER_SIZE + 16 < max_inline_size )
3152+ {
3153+ struct varlena * chunks = NULL ;
3154+
3155+ toasted_val = jsonx_toast_save_datum_ext (rel , toasterid , val , NULL , 0 , & chunks , compress_chunks );
3156+
3157+ if (!chunks )
3158+ ;
3159+ else if (VARSIZE_ANY (chunks ) <= max_inline_size )
3160+ {
3161+ pfree (DatumGetPointer (toasted_val ));
3162+ toasted_val = PointerGetDatum (chunks );
3163+ }
3164+ else
3165+ {
3166+ if (jsonb_compress_chunk_tids )
3167+ {
3168+ struct varlena * compressed_chunks = jsonx_toast_compress_tids (chunks , max_inline_size );
3169+
3170+ if (compressed_chunks )
3171+ {
3172+ if (VARSIZE_ANY (compressed_chunks ) <= max_inline_size )
3173+ {
3174+ pfree (DatumGetPointer (toasted_val ));
3175+ toasted_val = PointerGetDatum (compressed_chunks );
3176+ }
3177+ else
3178+ pfree (compressed_chunks );
3179+ }
3180+ }
3181+
3182+ pfree (chunks );
3183+ }
3184+ }
3185+ else
3186+ toasted_val = jsonx_toast_save_datum_ext (rel , toasterid , val ,
3187+ NULL , options , NULL , compress_chunks );
3188+
3189+ return toasted_val ;
3190+ }
3191+
31453192static Datum
31463193jsonb_toaster_default_toast (Relation rel , Oid toasterid , char cmethod ,
31473194 Datum new_val , Json * new_js ,
@@ -3171,6 +3218,9 @@ jsonb_toaster_default_toast(Relation rel, Oid toasterid, char cmethod,
31713218 if (VARSIZE_ANY (compressed_val ) <= max_inline_size )
31723219 return compressed_val ;
31733220
3221+ return jsonb_toaster_toast2 (rel , toasterid , new_val , compressed_val ,
3222+ max_inline_size , options );
3223+
31743224 return jsonx_toast_save_datum_ext (rel , toasterid , compressed_val ,
31753225 NULL , options , NULL , false);
31763226}
0 commit comments