From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Optimize escaping of JSON strings
Date: Sat, 27 Jul 2024 11:46:57 +0000
Message-ID: <E1sXft3-001Rr7-BX@gemulon.postgresql.org> (raw)
Optimize escaping of JSON strings
There were quite a few places where we either had a non-NUL-terminated
string or a text Datum which we needed to call escape_json() on. Many of
these places required that a temporary string was created due to the fact
that escape_json() needs a NUL-terminated cstring. For text types, those
first had to be converted to cstring before calling escape_json() on them.
Here we introduce two new functions to make escaping JSON more optimal:
escape_json_text() can be given a text Datum to append onto the given
buffer. This is more optimal as it foregoes the need to convert the text
Datum into a cstring. A temporary allocation is only required if the text
Datum needs to be detoasted.
escape_json_with_len() can be used when the length of the cstring is
already known or the given string isn't NUL-terminated. Having this
allows various places which were creating a temporary NUL-terminated
string to just call escape_json_with_len() without any temporary memory
allocations.
Discussion: https://postgr.es/m/CAApHDvpLXwMZvbCKcdGfU9XQjGCDm7tFpRdTXuB9PVgpNUYfEQ@mail.gmail.com
Reviewed-by: Melih Mutlu, Heikki Linnakangas
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/17a5871d9d58ea639b6d1ba4f0ce58462d55a33c
Modified Files
--------------
contrib/hstore/hstore_io.c | 41 ++++-----
src/backend/backup/backup_manifest.c | 2 +-
src/backend/utils/adt/json.c | 156 +++++++++++++++++++++++------------
src/backend/utils/adt/jsonb.c | 2 +-
src/backend/utils/adt/jsonfuncs.c | 34 ++++----
src/backend/utils/adt/jsonpath.c | 15 +++-
src/include/utils/json.h | 2 +
7 files changed, 151 insertions(+), 101 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-committers@postgresql.org
Cc: drowley@postgresql.org, pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: Optimize escaping of JSON strings
In-Reply-To: <E1sXft3-001Rr7-BX@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox