pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pavel Luzanov <p.luzanov@postgrespro.ru>
To: pgsql-general@lists.postgresql.org
Subject: Re: conditionally terminate psql script
Date: Mon, 17 Dec 2018 15:43:45 +0300
Message-ID: <41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru> (raw)
In-Reply-To: <wolfgang-1181217131349.A0420644@noten19.local>
References: <wolfgang-1181217131349.A0420644@noten19.local>

Hi

> is there a way to stop execution of a psql script if a select returns some rows (or no rows)
> The idea is to add a safety check on data, specifically to select all new rows that would conflict
> on a bulk insert, show them and stop

Look at \if command in psql (since v10):

select count(*) as total from pg_class where 1 = 1\gset
select :total = 0 as notfound\gset
\if :notfound
    \echo Nothing found.
    \q
\endif
\echo :total records found.

-----
Pavel Luzanov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

view thread (11+ messages)  latest in thread

Message-ID: <41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru>
Permalink:  ../41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru/
Also on:    postgresql.org/message-id/41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru

 · 

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: p.luzanov@postgrespro.ru
  Subject: Re: conditionally terminate psql script
  In-Reply-To: <41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru>
  References: <wolfgang-1181217131349.A0420644@noten19.local> <41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru>

* 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