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 1og6xW-00010i-VU for pgsql-general@arkaria.postgresql.org; Wed, 05 Oct 2022 16:09:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1og6xV-00057F-FB for pgsql-general@arkaria.postgresql.org; Wed, 05 Oct 2022 16:09:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1og6xV-000573-1Q for pgsql-general@lists.postgresql.org; Wed, 05 Oct 2022 16:09:21 +0000 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1og6xS-0008R8-DY for pgsql-general@lists.postgresql.org; Wed, 05 Oct 2022 16:09:20 +0000 Received: by mail-ej1-x631.google.com with SMTP id a26so36502987ejc.4 for ; Wed, 05 Oct 2022 09:09:18 -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:to:from:subject:message-id:from:to:cc:subject:date; bh=lt0scdUxbkzzT3F2OHJW8nBYmYtJIaoAVLry3ygiBvc=; b=fgMxfGSRZFriUQq6AlJPpVQJa/xiGIxYDxGc3kuqoCpEbFDijvMfBAlM/GGG5xgJVq O43AXM2DSkWkDroHM6eKD367ZDY6SiJFey7Das4U6oPqrTKzjqqYoYhcX6jDmBZLbZOx YCZCYMDeTt1QrIclbPSEqysmhZx7wgfHkpzJ6Vtr0JCqgau7s/+JllF/kZJncildwhgR dh2zTq2gAFDGrLbrWC1SIW72cPKkHKUgwZcsnfU8Fi1gy9PdF8kBnRtcY8/Pb+x0M8Eh 6PcU8yLlietGFBclnLa+e52+xUstKkD4FfQxVHyXNALz/ON8Sw+yAtmbSbCGw9qqrSfU JlJw== 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:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date; bh=lt0scdUxbkzzT3F2OHJW8nBYmYtJIaoAVLry3ygiBvc=; b=mewW/W0Fpeozv6ClTqTUdLwa2cBKFYOZw7thVv2ypXZUndkdudBJJrNRbux7nR/mbF hCqpA2SREIj7YaGJoB+/pUuOdHYFSpkpdtmZZxdjPdLvp3IYQDN56DbKWZ0euvnRnrbT 8JrxbY328P5wiSIvWZ05paKb3KST6qz3YytLBv7hksNkL1+EY3a3UGapV3n3clmqJTpf YGlju9YErUPJ7u7ifxpTspFNlngeTs+QwHmed7S3BW6wAdmm4ESI/DLHmCwnyAs2c0AW 7Zvg9TAb9xw/Bwjr75pwS+VgVYLfW9bCnr+wf53Yd80p5qJcn8tFqBQC9Q7Vy9RK52Nj 1T8Q== X-Gm-Message-State: ACrzQf3/vY0zZj68WDgQg3KdiZY31XXvaF1iunLABJXkuQ5fWz2woGga 8wPExK3hISPweFspwuvP9aVMsQ== X-Google-Smtp-Source: AMsMyM4NSAcvMGy1zbp3OVrJ/uPnZmdkZAtCzUkmBvMTo7bjTP+Q+eigaKSMOOwjc8MZSRhIz6++Og== X-Received: by 2002:a17:907:7204:b0:783:e152:f1f1 with SMTP id dr4-20020a170907720400b00783e152f1f1mr265499ejc.119.1664986156753; Wed, 05 Oct 2022 09:09:16 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:341d:c223:a8b:69e0:f1f8]) by smtp.gmail.com with ESMTPSA id s15-20020a508d0f000000b004594754bae2sm4070024eds.71.2022.10.05.09.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 09:09:16 -0700 (PDT) Message-ID: <830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at> Subject: Re: Number of updated rows with LibPQ From: Laurenz Albe To: Dominique Devienne , pgsql-general@lists.postgresql.org Date: Wed, 05 Oct 2022 18:09:15 +0200 In-Reply-To: References: 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: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2022-10-05 at 16:38 +0200, Dominique Devienne wrote: > Hi, > > Is there a way to programmatically now how many rows an UPDATE did update? > I've read about [PQcmdTuples][1], but surely I shouldn't have to parse > that string, no? > For selects, I have [PQnTuples][2], but what to do on INSERT, UPDATE, DELETE? > Parse the result of PQcmdTuples myself??? If so, what's the 0 in the > INSERT below? > Is the output of PQcmdTuples "stable", i.e. "official"? Not locale dependent? > > Thanks, --DD > > [1]: https://www.postgresql.org/docs/14/libpq-exec.html#LIBPQ-PQCMDTUPLES > [2]: https://www.postgresql.org/docs/14/libpq-exec.html#LIBPQ-PQNTUPLES > > postgres=# create table foo (v int); > CREATE TABLE > postgres=# insert into foo values (1), (2), (3); > INSERT 0 3 > postgres=# update foo set v = 2*v where v = 2; > UPDATE 1 > postgres=# delete from foo where v = 3; > DELETE 1 > postgres=# select v from foo where v > 1; > ... > (1 row) > postgres=# delete from foo; > DELETE 2 > postgres=# drop table foo; > DROP TABLE > postgres=# 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", so you won't have to parse anything. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com