Re: Support json_errdetail in FRONTEND builds

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Jacob Champion <jacob(dot)champion(at)enterprisedb(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Daniel Gustafsson <daniel(at)yesql(dot)se>, alvherre(at)alvh(dot)no-ip(dot)org, Michael Paquier <michael(at)paquier(dot)xyz>
Subject: Re: Support json_errdetail in FRONTEND builds
Date: 2024-03-13 00:38:43
Message-ID: 8adce3eb-2506-b292-f2c3-734627dd4d60@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On 2024-03-12 Tu 14:43, Jacob Champion wrote:
> Hello,
>
> Both the incremental JSON [1] and OAuth [2] patchsets would be
> improved by json_errdetail(), which was removed from FRONTEND builds
> in b44669b2ca:
>
>> The routine providing a detailed error message based on the error code
>> is made backend-only, the existing code being unsafe to use in the
>> frontend as the error message may finish by being palloc'd or point to a
>> static string, so there is no way to know if the memory of the message
>> should be pfree'd or not.
> Attached is a patch to undo this, by attaching any necessary
> allocations to the JsonLexContext so the caller doesn't have to keep
> track of them.
>
> This is based on the first patch of the OAuth patchset, which
> additionally needs json_errdetail() to be safe to use from libpq
> itself. Alvaro pointed out offlist that we don't have to go that far
> to re-enable this function for the utilities, so this patch is a sort
> of middle ground between what we have now and what OAuth implements.
> (There is some additional minimization that could be done to this
> patch, but I'm hoping to keep the code structure consistent between
> the two, if the result is acceptable.)

Seems reasonable.

>
> Two notes that I wanted to point out explicitly:
> - On the other thread, Daniel contributed a destroyStringInfo()
> counterpart for makeStringInfo(), which is optional but seemed useful
> to include here.

yeah, although maybe worth a different patch.

> - After this patch, if a frontend client calls json_errdetail()
> without calling freeJsonLexContext(), it will leak memory.

Not too concerned about this:

1. we tend to be a bit more relaxed about that in frontend programs,
especially those not expected to run for long times and especially on
error paths, where in many cases the program will just exit anyway.

2. the fix is simple where it's needed.

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2024-03-13 01:15:06 Re: Improve eviction algorithm in ReorderBuffer
Previous Message Andrew Dunstan 2024-03-13 00:26:38 Re: [PROPOSAL] Skip test citext_utf8 on Windows