improve PQexec documentation

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

improve PQexec documentation

Fabien COELHO-3

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.

libpq-pqexec-doc-1.patch (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: improve PQexec documentation

Alvaro Herrera-9
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


Reply | Threaded
Open this post in threaded view
|

Re: improve PQexec documentation

Fabien COELHO-3

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.