Re: improve PQexec documentation

Lists: pgsql-hackers
From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: improve PQexec documentation
Date: 2019-04-12 08:38:49
Message-ID: alpine.DEB.2.21.1904121016310.8912@lancre
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Hello devs,

I'm looking at psql's use of PQexec for implementing some feature.

When running with multiple SQL commands, the doc is not very helpful.

From the source code I gathered that PQexec returns the first COPY results
if any, and if not the last non-empty results, unless all is empty in
which case an empty result is returned. So * marks the returned result
in the following examples:

INSERT ... \; * COPY ... \; SELECT ... \; \;
SELECT ... \; UPDATE ... \; * SELECT ... \; \;
\; \; * ;

The attached patch tries to improve the documentation based on my
understanding.

IMVHO, psql's code is kind of a mess to work around this strange behavior,
as there is a loop over results within PQexec, then another one after
PQexec if there were some COPY.

--
Fabien.

Attachment Content-Type Size
libpq-pqexec-doc-1.patch text/x-diff 1.2 KB

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: improve PQexec documentation
Date: 2019-04-12 13:12:11
Message-ID: 20190412131211.GA7287@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2019-Apr-12, Fabien COELHO wrote:

> I'm looking at psql's use of PQexec for implementing some feature.
>
> When running with multiple SQL commands, the doc is not very helpful.
>
> From the source code I gathered that PQexec returns the first COPY results
> if any, and if not the last non-empty results, unless all is empty in which
> case an empty result is returned.

I'm not sure we necessarily want to document this behavior. If it was
super helpful for some reason, or if we thought we would never change
it, then it would make sense to document it in minute detail. But
otherwise I think documenting it sets a promise that we would (try to)
never change it in the future, which I don't necessarily agree with --
particularly since it's somewhat awkward to use.

I'm inclined to reject this patch.

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: improve PQexec documentation
Date: 2019-04-12 15:51:29
Message-ID: alpine.DEB.2.21.1904121734510.2780@lancre
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Hello Alvaro,

>> I'm looking at psql's use of PQexec for implementing some feature.
>>
>> When running with multiple SQL commands, the doc is not very helpful.
>>
>> From the source code I gathered that PQexec returns the first COPY results
>> if any, and if not the last non-empty results, unless all is empty in which
>> case an empty result is returned.
>
> I'm not sure we necessarily want to document this behavior. If it was
> super helpful for some reason, or if we thought we would never change
> it, then it would make sense to document it in minute detail. But
> otherwise I think documenting it sets a promise that we would (try to)
> never change it in the future, which I don't necessarily agree with --
> particularly since it's somewhat awkward to use.
>
> I'm inclined to reject this patch.

Hmmm. I obviously agree that PQexec is beyond awkward.

Now I'm not sure how anyone is expected to guess the actual function
working from the available documentation, and without this knowledge I
cannot see how to write meaningful code for the multiple query case.

Basically it seems to have been designed for simple queries, and then
accomodated somehow for the multiple case but with a strange non
systematic approach.

I think it would have been much simpler and straightforward to always
return the first result and let the client do whatever it wants
afterwards. However, as it has existed for quite some time, I'm unsure how
likely it is to change as it would break existing code, so documenting its
behavior seems logical. I'd be all in favor of changing the behavior, but
I'm pessimistic that it could pass. Keeping the current status (not really
documented & awkward behavior) seems rather strange.

--
Fabien.


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: improve PQexec documentation
Date: 2019-07-05 06:47:32
Message-ID: c027c1d1-78de-4d8c-d0df-ac2066520697@2ndquadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 2019-04-12 17:51, Fabien COELHO wrote:
> Hmmm. I obviously agree that PQexec is beyond awkward.
>
> Now I'm not sure how anyone is expected to guess the actual function
> working from the available documentation, and without this knowledge I
> cannot see how to write meaningful code for the multiple query case.

But you're not really supposed to use it for multiple queries or
multiple result sets anyway. There are other functions for this.

If a source code comment in libpq or psql would help explaining some of
the current code, then we could add that. But I am also not sure that
enshrining the current behavior on the API documentation is desirable.

> Basically it seems to have been designed for simple queries, and then
> accomodated somehow for the multiple case but with a strange non
> systematic approach.

probably

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: improve PQexec documentation
Date: 2019-08-01 09:12:35
Message-ID: CA+hUKGKYAxfFy1-zj72HRYkRAcB5=WjpfmVAR2+0SswaPfqrwQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Apr 13, 2019 at 1:12 AM Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:
> I'm inclined to reject this patch.

On Fri, Jul 5, 2019 at 6:47 PM Peter Eisentraut
<peter(dot)eisentraut(at)2ndquadrant(dot)com> wrote:
> But you're not really supposed to use it for multiple queries or
> multiple result sets anyway. There are other functions for this.
>
> If a source code comment in libpq or psql would help explaining some of
> the current code, then we could add that. But I am also not sure that
> enshrining the current behavior on the API documentation is desirable.

Hi Fabien,

Based on the above, I have marked this as "Returned with feedback".

--
Thomas Munro
https://enterprisedb.com