PostgreSQL 中 JSON 数据类型的大小限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12632871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:33:42  来源:igfitidea点击:

Size limit of JSON data type in PostgreSQL

jsonpostgresqlpostgresql-9.2

提问by ankurvsoni

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

有谁知道 PostgreSQL 9.2 中 JSON 数据类型的大小限制是多少?

回答by j.w.r

Looking at the source for PostgreSQL 9.2.1:

查看 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:

PostgreSQL 9.3.5 更新:

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

json_in函数中的代码已经改变,但是json内部表示仍然是文本:

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, jsonis the same as a textdatatype but with JSON validation. The textdatatype's maximum size is 1GB.

因此,至少现在看来,它jsontext数据类型相同,但具有 JSON 验证。该text数据类型的最大大小为1GB

回答by Mohammad Banisaeid

For jsonbfields, if you look at the jsonb.cin the source code, you'll see this function:

对于jsonb字段,如果你查看源代码中的jsonb.c,你会看到这个函数:

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_OFFLENMASKbytes.

错误代码表示 jsonb 字符串不能超过JENTRY_OFFLENMASK字节。

In jsonb.h, that constant is defined as:

jsonb.h 中,该常量定义为:

#define JENTRY_OFFLENMASK       0x0FFFFFFF

which is 255 MB.

这是255 MB

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

我在 PostgreSQL 9.4 到 12 的源代码中检查了这一点。