pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Dominique Devienne <ddevienne@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Number of updated rows with LibPQ
Date: Fri, 14 Oct 2022 14:28:59 +0200
Message-ID: <6cdf30719299de49a0cb70aa60b149ad8908fcce.camel@cybertec.at> (raw)
In-Reply-To: <CAFCRh-8b8RGAUYifOje2zSwUCrK02da-jGtj-=vhFjJn1yWQcg@mail.gmail.com>
References: <CAFCRh-_Z+ox_W4+yZTOC31d39b7w23jWStvK0SXV1qfht9tRLQ@mail.gmail.com>
	<830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at>
	<2433846.1664993828@sss.pgh.pa.us>
	<CAFCRh-8b8RGAUYifOje2zSwUCrK02da-jGtj-=vhFjJn1yWQcg@mail.gmail.com>

On Fri, 2022-10-14 at 13:52 +0200, Dominique Devienne wrote:
> On Wed, Oct 5, 2022 at 8:17 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > Laurenz Albe <laurenz.albe@cybertec.at> writes:
> > > On Wed, 2022-10-05 at 16:38 +0200, Dominique Devienne wrote:
> > > Yes, you have to use PQcmdTuples(), and you have to convert the string to an integer.
> > > But don't worry: the result will *not* be "INSERT 0 5", it will be just "5"
> 
> Thanks. What's the leading 0 though, then?
> I guessed it might be the number of rows returned, but it isn't, see below:
> 
> postgres=# create table foo (id serial primary key, v int);
> CREATE TABLE
> postgres=# insert into foo (v) values (1), (2) returning id;
>  id
> ----
>   1
>   2
> (2 rows)
> 
> INSERT 0 2

That 0 is the OID of the newly inserted tuple.
Since there are no more tables WITH OIDS, the number is always 0, but
is left in the output for compatibility reasons.

> > Yeah, just applying atoi() or atol() to the result should be enough.
> 
> Thanks too. Since I'm in C++, I used <charconv> instead, and
> discovered it can be empty something, not 0.
> I guess atoi() would have hidden that distinction, and worked anyway
> (returning 0).
> 
> In the same topic, I've noticed an INSERT returns PGRES_COMMAND_OK,
> while an INSERT-RETURNING returns PGRES_TUPLES_OK. So there's no way
> to use the status to distinguish a SELECT from anything else? A RETURNING clause
> makes any statement supporting it an hybrid of a command and a query,
> but then how
> does one know the exact "kind" of the statement?
> 
> E.g. So how does psql show INSERT in either cases? By parsing the SQL
> itself, client-side?
> Or is there a libpq API on PGresult that would allow to get the type
> of statement the result is from?

The command tag is not what you look at.

You simply check the result from an INSERT statement.  If it is PGRES_TUPLES_OK,
it must have been INSERT ... RETRUNING.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com





view thread (8+ messages)  latest in thread

Message-ID: <6cdf30719299de49a0cb70aa60b149ad8908fcce.camel@cybertec.at>
Permalink:  ../6cdf30719299de49a0cb70aa60b149ad8908fcce.camel@cybertec.at/
Also on:    postgresql.org/message-id/6cdf30719299de49a0cb70aa60b149ad8908fcce.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, ddevienne@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: Number of updated rows with LibPQ
  In-Reply-To: <6cdf30719299de49a0cb70aa60b149ad8908fcce.camel@cybertec.at>
  References: <CAFCRh-_Z+ox_W4+yZTOC31d39b7w23jWStvK0SXV1qfht9tRLQ@mail.gmail.com> <830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at> <2433846.1664993828@sss.pgh.pa.us> <CAFCRh-8b8RGAUYifOje2zSwUCrK02da-jGtj-=vhFjJn1yWQcg@mail.gmail.com> <6cdf30719299de49a0cb70aa60b149ad8908fcce.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox