Directly return Postgres jsonb binary bytes

I’ve a table with a jsonb column. when I run the below query it returns 5559 by

select pg_column_size("jsonb_column") from "Table"

while when I return the stream of bytes in my application I am getting 18,185 bytes.

I wonder if there is a functionality in postgres to return the binary/decomposed format instead of encoded text?

3

There is, but it won’t help.

Chapter 55. Frontend/Backend Protocol:

The desired format for any value is specified by a format code. Clients can specify a format code for each transmitted parameter value and for each column of a query result. Text has format code zero, binary has format code one, and all other format codes are reserved for future definition.

You can find an example use in libpq‘s PQexecParam:

resultFormat
Specify zero to obtain results in text format, or one to obtain results in binary format.

So clients just switch a bit when they want binary. npgsql hides that in src/Npgsql/Internal /DataFormat.cs:

public enum DataFormat : byte
{
    Binary,
    Text
}

static class DataFormatUtils
{
    public static DataFormat Create(short formatCode)
        => formatCode switch
        {
            0 => DataFormat.Text,
            1 => DataFormat.Binary,
            _ => throw new ArgumentOutOfRangeException(nameof(formatCode), formatCode, "Unknown postgres format code, please file a bug,")
        };

Thing is, the binary format you get from that is not necessarily the lightweight internal representation, it’s whatever the type’s _send() function produces. For simple things like date, it’s pretty much handing out the internal representation. For more complex things, like daterange, range_send() is constructing a specific “external” format, but mostly out of internal binaries.
In case of jsonb_send(), it just generates its textual representation with a version number up front and sends that:

/*
 * jsonb type send function
 *
 * Just send jsonb as a version number, then a string of text
 */
Datum
jsonb_send(PG_FUNCTION_ARGS)
{
    Jsonb      *jb = PG_GETARG_JSONB_P(0);
    StringInfoData buf;
    StringInfo  jtext = makeStringInfo();
    int         version = 1;

    (void) JsonbToCString(jtext, &jb->root, VARSIZE(jb));

    pq_begintypsend(&buf);
    pq_sendint8(&buf, version);
    pq_sendtext(&buf, jtext->data, jtext->len);
    destroyStringInfo(jtext);

    PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

That means the jsonb value gets inflated to a json size.

On top of that, the comparison you were looking at isn’t apples-to-apples because pg_column_size() tells you the effective size, after TOASTing and transparent compression:

pg_column_size ( "any" ) → integer
Shows the number of bytes used to store any individual data value. If applied directly to a table column value, this reflects any compression that was done.

Some values might be saved raw, some compressed inline, some raw but moved out of line to a TOAST table, some both moved to TOAST and then also compressed in there. TOASTing costs some additional space, compression reduces it and there’s more than one algorithm that can be used.

If your goal is to get the same value but in size closer to 5559 bytes than 18185, Postgres has pgcrypto where you can set compress-algo to 1 for zip and compress-level to an unreasonably high 9, and run your values through that. The text representation of jsonb should compress nicely, an array of similar jsonb objects even more so. Still, that does get you the same thing, in less bytes but you’ll have to undo all that to actually do anything with the them in your app.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật