Size limit of JSON data type in PostgreSQL
Asked Answered
A

2

91

Does anyone know what is the limit on the size of JSON data type in PostgreSQL 9.2?

Araby answered 28/9, 2012 at 2:37 Comment(0)
T
108

Looking at the source for PostgreSQL 9.2.1:

Source: postgresql-9.2.1\src\backend\utils\adt\json.c:
/*
 * Input.
 */
Datum
json_in(PG_FUNCTION_ARGS)
{
    char       *text = PG_GETARG_CSTRING(0);

    json_validate_cstring(text);

    /* Internal representation is the same as text, for now */
    PG_RETURN_TEXT_P(cstring_to_text(text));
}

Update for PostgreSQL 9.3.5:

The code has changed in the json_in function, but the json internal representation is still text:

Source: postgresql-9.3.5\src\backend\utils\adt\json.c:
/*
 * Input.
 */
Datum
json_in(PG_FUNCTION_ARGS)
{
    char       *json = PG_GETARG_CSTRING(0);
    text       *result = cstring_to_text(json);
    JsonLexContext *lex;

    /* validate it */
    lex = makeJsonLexContext(result, false);
    pg_parse_json(lex, &nullSemAction);

    /* Internal representation is the same as text, for now */
    PG_RETURN_TEXT_P(result);
}

So it appears that, for now at least, json is the same as a text datatype but with JSON validation. The text datatype's maximum size is 1GB.

Trinetta answered 28/9, 2012 at 3:23 Comment(7)
Does the (new since post?) Postgres jsonb data-type use something other than pure text?Semiramis
1GB! that is pretty nice. Glad that number isn't in MB.Conqueror
@Conqueror it is 1024 MB :)Socinus
isn't it 1000 mb? ;)Geiger
Technically a MB (MegaByte) is 1000 and a MiB (MebiByte) is 1024 but everyone uses MB (MegaByte) to refer to 1024Cecilycecity
jsonb looks like it's also backed by text (i'm not sure). here is a link to the file: github.com/postgres/postgres/blob/master/src/backend/utils/adt/… and to jsonb file: github.com/postgres/postgres/blob/master/src/backend/utils/adt/…Aristotelianism
@localhostdotdev: if you try to create a large jsonb value you can easily hit the error message: "total size of jsonb object elements exceeds the maximum of 268435455 bytes" which means that jsonb indeed has a limit fo 256MB, not 1GBHereditament
C
70

For jsonb fields, if you look at the jsonb.c in the source code, you'll see this function:

checkStringLen(size_t len)
{
    if (len > JENTRY_OFFLENMASK)
        ereport(ERROR,
                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                 errmsg("string too long to represent as jsonb string"),
                 errdetail("Due to an implementation restriction, jsonb strings cannot exceed %d bytes.",
                           JENTRY_OFFLENMASK)));

    return len;
}

Error code says that jsonb strings cannot exceed JENTRY_OFFLENMASK bytes.

In jsonb.h, that constant is defined as:

#define JENTRY_OFFLENMASK       0x0FFFFFFF

which is 255 MB.

I checked this in the source code for PostgreSQL 9.4 up to 15.

Commentate answered 4/2, 2020 at 13:57 Comment(2)
Not sure about the accepted answer claiming 1GB , but I ran into this exact error yesterday. And my json was just a bit over 300Mb in size.Etalon
because jsonb != jsonSpeer

© 2022 - 2024 — McMap. All rights reserved.