diff options
author | David Rowley <drowley@postgresql.org> | 2024-07-27 23:46:07 +1200 |
---|---|---|
committer | David Rowley <drowley@postgresql.org> | 2024-07-27 23:46:07 +1200 |
commit | 17a5871d9d58ea639b6d1ba4f0ce58462d55a33c (patch) | |
tree | 197ee9a63a458f6980870bb30f75f3693f449aa9 /src/include/utils/json.h | |
parent | 67427f10093a9c50c79e1dbfdcd1698433e8a88f (diff) | |
download | postgresql-17a5871d9d58ea639b6d1ba4f0ce58462d55a33c.tar.gz postgresql-17a5871d9d58ea639b6d1ba4f0ce58462d55a33c.zip |
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
Diffstat (limited to 'src/include/utils/json.h')
-rw-r--r-- | src/include/utils/json.h | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/include/utils/json.h b/src/include/utils/json.h index 6d7f1b387d1..79c1062e1bf 100644 --- a/src/include/utils/json.h +++ b/src/include/utils/json.h @@ -18,6 +18,8 @@ /* functions in json.c */ extern void escape_json(StringInfo buf, const char *str); +extern void escape_json_with_len(StringInfo buf, const char *str, int len); +extern void escape_json_text(StringInfo buf, const text *txt); extern char *JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp); extern bool to_json_is_immutable(Oid typoid); |