Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3200771

Browse files
author
Nikita Glukhov
committed
Fix fallback to default TOAST for custom-TOASTed jsonbs
1 parent bb69550 commit 3200771

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

contrib/jsonb_toaster/jsonb_toaster.c

+37-12
Original file line numberDiff line numberDiff line change
@@ -2786,12 +2786,44 @@ jsonb_toaster_validate(Oid typeoid, char storage, char compression,
27862786
return ok;
27872787
}
27882788

2789+
static Datum
2790+
jsonb_toaster_default_toast(Relation rel, Oid toasterid, char cmethod,
2791+
Datum new_val, Json *new_js,
2792+
int max_inline_size, int options)
2793+
{
2794+
2795+
Datum compressed_val = (Datum) 0;
2796+
2797+
if (VARATT_IS_CUSTOM(new_val))
2798+
{
2799+
/* Convert custom-TOASTed jsonb to plain jsonb */
2800+
JsonbValue jbv;
2801+
2802+
JsonValueInitBinary(&jbv, JsonRoot(new_js));
2803+
2804+
new_val = PointerGetDatum(JsonEncode(&jbv, JsonbEncode, NULL));
2805+
}
2806+
2807+
if (!VARATT_IS_COMPRESSED(new_val))
2808+
{
2809+
compressed_val = toast_compress_datum(new_val, cmethod);
2810+
2811+
if (compressed_val == (Datum) 0)
2812+
compressed_val = new_val;
2813+
}
2814+
2815+
if (VARSIZE_ANY(compressed_val) <= max_inline_size)
2816+
return compressed_val;
2817+
2818+
return jsonx_toast_save_datum_ext(rel, toasterid, compressed_val,
2819+
NULL, options, NULL, false);
2820+
}
2821+
27892822
static struct varlena *
27902823
jsonb_toaster_toast(Relation rel, Oid toasterid,
27912824
Datum new_val, Datum old_val,
27922825
int max_inline_size, int options)
27932826
{
2794-
27952827
Json *new_js;
27962828
Datum res;
27972829
char cmethod = TOAST_PGLZ_COMPRESSION;
@@ -2803,17 +2835,10 @@ jsonb_toaster_toast(Relation rel, Oid toasterid,
28032835
res = jsonb_toaster_save(rel, toasterid, new_js, max_inline_size, cmethod);
28042836

28052837
if (res == (Datum) 0)
2806-
{
2807-
Datum compressed_val = (Datum) 0;
2808-
2809-
if (!VARATT_IS_COMPRESSED(new_val))
2810-
compressed_val = toast_compress_datum(new_val, cmethod);
2811-
2812-
if (compressed_val == (Datum) 0)
2813-
compressed_val = new_val;
2814-
2815-
res = jsonx_toast_save_datum_ext(rel, toasterid, compressed_val, NULL, options, NULL, false);
2816-
}
2838+
/* Fallback to default TOAST */
2839+
res = jsonb_toaster_default_toast(rel, toasterid, cmethod,
2840+
new_val, new_js,
2841+
max_inline_size, options);
28172842

28182843
res = res == (Datum) 0 ? new_val : res;
28192844

0 commit comments

Comments
 (0)