Re: conditionally terminate psql script

From: Pavel Luzanov <p(dot)luzanov(at)postgrespro(dot)ru>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: conditionally terminate psql script
Date: 2018-12-17 12:43:45
Message-ID: 41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message hamann.w 2018-12-17 13:07:22 Re: conditionally terminate psql script
Previous Message Pavel Stehule 2018-12-17 12:42:14 Re: conditionally terminate psql script