Re: Emitting JSON to file using COPY TO

From: Nathan Bossart <nathandbossart(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joe Conway <mail(at)joeconway(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Davin Shearer <davin(at)apache(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Emitting JSON to file using COPY TO
Date: 2023-12-07 02:56:22
Message-ID: 20231207025622.GA3011676@nathanxps13
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Wed, Dec 06, 2023 at 03:20:46PM -0500, Tom Lane wrote:
> If Nathan's perf results hold up elsewhere, it seems like some
> micro-optimization around the text-pushing (appendStringInfoString)
> might be more useful than caching. The 7% spent in cache lookups
> could be worth going after later, but it's not the top of the list.

Hah, it turns out my benchmark of 110M integers really stresses the
JSONTYPE_NUMERIC path in datum_to_json_internal(). That particular path
calls strlen() twice: once for IsValidJsonNumber(), and once in
appendStringInfoString(). If I save the result from IsValidJsonNumber()
and give it to appendBinaryStringInfo() instead, the COPY goes ~8% faster.
It's probably worth giving datum_to_json_internal() a closer look in a new
thread.

diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 71ae53ff97..1951e93d9d 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -180,6 +180,7 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
{
char *outputstr;
text *jsontext;
+ int len;

check_stack_depth();

@@ -223,8 +224,8 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
* Don't call escape_json for a non-key if it's a valid JSON
* number.
*/
- if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
- appendStringInfoString(result, outputstr);
+ if (!key_scalar && IsValidJsonNumber(outputstr, (len = strlen(outputstr))))
+ appendBinaryStringInfo(result, outputstr, len);
else
escape_json(result, outputstr);
pfree(outputstr);

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message arun chirappurath 2023-12-07 03:45:06 Fwd: Disable autocommit inside dbeaver
Previous Message Euler Taveira 2023-12-07 02:42:06 Re: Emitting JSON to file using COPY TO

Browse pgsql-hackers by date

  From Date Subject
Next Message jian he 2023-12-07 03:10:59 Re: remaining sql/json patches
Previous Message Euler Taveira 2023-12-07 02:42:06 Re: Emitting JSON to file using COPY TO