Re: PATCH: Batch/pipelining support for libpq

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Vaishnavi Prabakaran" <vaishnaviprabakaran(at)gmail(dot)com>
Cc: "Michael Paquier" <michael(dot)paquier(at)gmail(dot)com>,"Prabakaran, Vaishnavi" <VaishnaviP(at)fast(dot)au(dot)fujitsu(dot)com>,"Craig Ringer" <craig(at)2ndquadrant(dot)com>,"Haribabu Kommi" <kommi(dot)haribabu(at)gmail(dot)com>,"Tsunakawa, Takayuki" <tsunakawa(dot)takay(at)jp(dot)fujitsu(dot)com>,"Dmitry Igrishin" <dmitigr(at)gmail(dot)com>,"Andres Freund" <andres(at)anarazel(dot)de>,"PostgreSQL Hackers" <pgsql-hackers(at)postgresql(dot)org>,"Manuel Kniep" <m(dot)kniep(at)web(dot)de>,"fujita(dot)etsuro(at)lab(dot)ntt(dot)co(dot)jp" <fujita(dot)etsuro(at)lab(dot)ntt(dot)co(dot)jp>,"Iwata, Aya" <iwata(dot)aya(at)jp(dot)fujitsu(dot)com>
Subject: Re: PATCH: Batch/pipelining support for libpq
Date: 2017-03-09 21:43:46
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Vaishnavi Prabakaran wrote:

> if (PQbatchStatus(st->con) != PQBATCH_MODE_ON)
> But, it is better to use if (PQbatchStatus(st->con) ==
> PQBATCH_MODE_OFF) for this verification. Reason is there is one more state
> in batch mode - PQBATCH_MODE_ABORTED. So, if the batch mode is in aborted
> status, this check will still assume that the connection is not in batch
> mode.
> In a same way, "if(PQbatchStatus(st->con) == PQBATCH_MODE_ON)" check is
> better to be modified as "PQbatchStatus(st->con) != PQBATCH_MODE_OFF".

Agreed, these two tests need to be changed to account for the
PQBATCH_MODE_ABORTED case. Fixed in new patch.

> 2. @@ -2207,7 +2227,47 @@ doCustom(TState *thread, CState *st, StatsData
> *agg)
> + if (PQbatchStatus(st->con) != PQBATCH_MODE_OFF)
> + {
> + commandFailed(st, "already in batch mode");
> + break;
> + }
> This is not required as below PQbatchBegin() internally does this
> verification.
> + PQbatchBegin(st->con);

The point of this test is to specifically forbid a sequence like this
...(no \endbatch)
and according to the doc for PQbatchBegin, it looks like the return
code won't tell us:
"Causes a connection to enter batch mode if it is currently idle or
already in batch mode.
int PQbatchBegin(PGconn *conn);
Returns 1 for success"

My understanding is that "already in batch mode" is not an error.

"Returns 0 and has no effect if the connection is not currently
idle, i.e. it has a result ready, is waiting for more input from
the server, etc. This function does not actually send anything to
the server, it just changes the libpq connection state"

> 3. It is better to check the return value of PQbatchBegin() rather than
> assuming success. E.g: PQbatchBegin() will return false if the connection
> is in copy in/out/both states.

Given point #2 above, I think both tests are needed:
if (PQbatchStatus(st->con) != PQBATCH_MODE_OFF)
commandFailed(st, "already in batch mode");
if (PQbatchBegin(st->con) == 0)
commandFailed(st, "failed to start a batch");

> > 3. In relation to #2, PQsendQuery() is not forbidden in batch mode
> > although I don't think it can work with it, as it's based on the old
> > protocol.
> >
> It is actually forbidden and you can see the error message "cannot
> PQsendQuery in batch mode, use PQsendQueryParams" when you use
> PQsendQuery().

Sorry for blaming the innocent piece of code, looking closer
it's pgbench that does not display the message.
With the simple query protocol, sendCommand() does essentially:

r = PQsendQuery(st->con, sql);

... other stuff...

if (r == 0)
if (debug)
fprintf(stderr, "client %d could not send %s\n",
st->id, command->argv[0]);
return false;

So only in debug mode does it inform the user that it failed, and
even then, it does not display PQerrorMessage(st->con).

In non-debug mode it's opaque to the user. If it always fail, it appears
to loop forever. The caller has this comment that is relevant to the problem:

if (!sendCommand(st, command))
* Failed. Stay in CSTATE_START_COMMAND state, to
* retry. ??? What the point or retrying? Should
* rather abort?

I think it doesn't bother anyone up to now because the immediate
failure modes of PQsendQuery() are resource allocation or protocol
failures that tend to never happen.

Anyway currently \beginbatch avoids the problem by checking
whether querymode == QUERY_SIMPLE, to fail gracefully
instead of letting the endless loop happen.

Best regards,
Daniel Vérité
PostgreSQL-powered mailer:
Twitter: @DanielVerite

Attachment Content-Type Size
pgbench-batch-mode-v2.patch text/plain 5.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Stephen Frost 2017-03-09 21:48:49 Re: contrib modules and relkind check
Previous Message Andres Freund 2017-03-09 21:41:51 Re: exposing wait events for non-backends (was: Tracking wait event for latches)