Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vaGIJ-0062t7-1Y for pgsql-general@arkaria.postgresql.org; Mon, 29 Dec 2025 16:40:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vaGIG-00HUP8-1z for pgsql-general@arkaria.postgresql.org; Mon, 29 Dec 2025 16:40:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vaGIG-00HUP0-0a for pgsql-general@lists.postgresql.org; Mon, 29 Dec 2025 16:40:29 +0000 Received: from smtp-74.smtpout.orange.fr ([80.12.242.74] helo=smtp.smtpout.orange.fr) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vaGID-003QTj-0c for pgsql-general@postgresql.org; Mon, 29 Dec 2025 16:40:28 +0000 Received: from X250.home ([IPv6:2a01:cb14:316:8c00:506c:d588:c52a:af6e]) by smtp.orange.fr with ESMTPSA id aGIBvnoF7khdXaGIBvC72Q; Mon, 29 Dec 2025 17:40:24 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1767026424; bh=gLXE5JO3Q2bgpHUCBKbTRiNoN/T99/vPNuv6eqB7A7Q=; h=Date:From:To:Subject:Message-Id:Mime-Version; b=ED9Jwq7vOQMNl7GA2DjIaQYKIn7cMfcj+e38eLSMUeJ7z0XWcBgJ6pfpq1Cx+xlG+ 8wZB+3qG6mkuufmSkCLmCunGeB1Wr2HqTuGjYfZSlSJKbpvT1OSKbQ+TtkpeD8B+A3 ot0kEDXh/eITppQze3a7FNydliTnKHfULJR9gsnpwqbk+RfxrevAx+jhds4zSD7+/7 OuntgwbvBnbmJhBq8arv/zvmyC5KAoLZoN7sqIp//fqyYey2CDhDw5I6zkawcMSDY2 ufcG7t5Qs8R0ejGx4rFAvFlvJUOXeSQwMrOr3L2fr7vQcqvjyLiBqXhkk135e77Mgn 1Snq7Q7QWJslQ== X-ME-Helo: X250.home X-ME-Auth: dnYubGlzdHNAd2FuYWRvby5mcg== X-ME-Date: Mon, 29 Dec 2025 17:40:24 +0100 X-ME-IP: 2a01:cb14:316:8c00:506c:d588:c52a:af6e Date: Mon, 29 Dec 2025 17:40:23 +0100 From: Vincent Veyron To: depesz@depesz.com Cc: Ron Johnson , pgsql-general Subject: Re: psql: print values and return the COUNT(*) value to bash? Message-Id: <20251229174023.106f1c1f2212c7372615673c@wanadoo.fr> In-Reply-To: References: X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 29 Dec 2025 11:11:58 +0100 hubert depesz lubaczewski wrote: Hi Ron, Hubert, >=20 > Example: >=20 > =3D$ cat z.sh > #!/usr/bin/env bash >=20 > table=3Dpg_stat_all_tables > field=3Dlast_autovacuum >=20 > returned=3D"$( psql -d depesz_explain -v "t=3D$table" -v "f=3D$field" -X = << _SQL_ > select current_timestamp - min(:"f") as days_ago > , min(:"f") as oldest_date > , count(*) as cnt > from :"t" > _SQL_ > )" > row_count=3D"$( tail -n1 <<< "${returned}" | tr -cd 0-9 )" > echo "Rowcount =3D $row_count" > echo "Full output:" > echo "${returned}" >=20 > # vim: set filetype=3Dbash shiftwidth=3D4 expandtab smarttab softtabstop= =3D4 tabstop=3D4 textwidth=3D132 : >=20 > =3D$ bash z.sh > Rowcount =3D 1 > Full output: > days_ago | oldest_date | cnt > ------------------------+-------------------------------+----- > 7 days 03:48:07.348247 | 2025-12-22 07:22:32.593814+01 | 212 > (1 row) >=20 IIUC, Ron wants the 212 number? Maybe use the -t switch, and 'cut' : #!/usr/bin/env bash table=3Dpg_stat_all_tables field=3Dlast_autovacuum returned=3D"$( psql -t -d vv -v "t=3D$table" -v "f=3D$field" -X << _SQL_ select current_timestamp - min(:"f") as days_ago , min(:"f") as oldest_date , count(*) as cnt from :"t" _SQL_ )" row_count=3D"$( tail -n1 <<< "${returned}" | cut -d "|" -f 3)" echo "Rowcount =3D $row_count" echo "Full output:" echo "${returned}" --=20 Bien =E0 vous, Vincent Veyron=20 https://compta.libremen.com Logiciel libre de comptabilit=E9 g=E9n=E9rale et analytique en partie double