Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ojJoI-0005fF-5x for pgsql-general@arkaria.postgresql.org; Fri, 14 Oct 2022 12:29:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ojJoG-0004eu-PR for pgsql-general@arkaria.postgresql.org; Fri, 14 Oct 2022 12:29:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ojJoG-0004ek-CG for pgsql-general@lists.postgresql.org; Fri, 14 Oct 2022 12:29:04 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ojJoD-0000C7-Sx for pgsql-general@lists.postgresql.org; Fri, 14 Oct 2022 12:29:03 +0000 Received: by mail-ej1-x632.google.com with SMTP id 13so10165089ejn.3 for ; Fri, 14 Oct 2022 05:29:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=AVtuLb8apaweHo8ZP2hSGNRAzU67JEA2XL/FsBoNl+4=; b=mQ0idZGOnQuAqUKFvJHMv2/Ak4NPeRuYhr5lMywH/kB6ndgSSxASaVRY6zeuteugxH fA3WyJpiFFIJcXBoMKLtTX1D+KZB/NeK2cicynaCeBlbr6Rf++O33hJLrrCf+h6oXf0j s2JxkAuk4/Pu/8ES6yY5txCrng2LfFFAkVcIPQ4tvYbLxuuU8MGg1rKDlBLcINf+jbwl D+4kasbpRteX8Ecb//2Gjmx55U7XpuWSJ0pyySr57k9vdbdQ5L157vbvbBjDbWkRvDfY IzRRE7T0wg1GfzPp7Wg+bYKyK+s/qfvJpdWNyPVC4r/GjrCbbRaU0U1u1pb1QhKEBN7Y g5cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=AVtuLb8apaweHo8ZP2hSGNRAzU67JEA2XL/FsBoNl+4=; b=V0/iOpRp/jDo9pNStGAHV3l9++zhbO7Yo6RLR9tnh/rL2RUwhfez2wnM0fZUEZ71I0 A7nSbKfvGrww9AU33JZXlMb8pklLeNSUJJFGs3iaFTFXVBPy5ui1RSLseEGHmYF/C4sZ oTRwVm3YTqdZYj6QEgMgVVHExIbDCOzF0dz6JL183X090SDGg5pD+gra8zJTiI5c92/s igBxlLAFcm94c0khTU8lwYWQxdseI5obcyQyfJTO21ZM1s9QGrwOBzrVXVTFsZH8ItKg zfbpXBi30P+DNUjJpmOf27uEseqGt+DqSjcQtw42TpDlHO65e0kUvB/B1K1+BdvLleP+ px5Q== X-Gm-Message-State: ACrzQf11ijNP4EiDS0KHz0DNNcH8pRFUVke47ANxaPq+i6VkeOivRaqc ErUM2f09S84WMRnk+39nTuI5Wg== X-Google-Smtp-Source: AMsMyM491QG4DulchrQSun4KiDV77ujzoZv6tnXlkucC7Kyqgq9SLPB8ckCc/ncH3XTwfEufUet7GQ== X-Received: by 2002:a17:907:6e19:b0:78d:cbdc:9e4a with SMTP id sd25-20020a1709076e1900b0078dcbdc9e4amr3345435ejc.120.1665750540231; Fri, 14 Oct 2022 05:29:00 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:cddf:dd82:2817:4f1:8514]) by smtp.gmail.com with ESMTPSA id k26-20020a17090632da00b0078082f95e5csm1418146ejk.204.2022.10.14.05.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 05:28:59 -0700 (PDT) Message-ID: <6cdf30719299de49a0cb70aa60b149ad8908fcce.camel@cybertec.at> Subject: Re: Number of updated rows with LibPQ From: Laurenz Albe To: Dominique Devienne , Tom Lane Cc: pgsql-general@lists.postgresql.org Date: Fri, 14 Oct 2022 14:28:59 +0200 In-Reply-To: References: <830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at> <2433846.1664993828@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2022-10-14 at 13:52 +0200, Dominique Devienne wrote: > On Wed, Oct 5, 2022 at 8:17 PM Tom Lane wrote: > > Laurenz Albe 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 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