Re: How to *really* quit psql?

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: How to *really* quit psql?
Date: 2022-11-20 07:42:35
Message-ID: 90298ea-cc54-bed0-a9c1-62abc1579663@mines-paristech.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Hello Tom,

>> - when the current script is included from something,
>> you quit the current script and proceed after the \i of next -f, BAD
>
>> Question: is there any way to really abort a psql script from an included
>> file?
>
> Under what circumstances would it be appropriate for a script to take
> it on itself to decide that?

The use case is psql scripts which update or cleanup an application
schema. For security, some of these scripts check for conditions (eg, we
are not in production, the application schema is in the expected version,
whatever…) and should abort if the conditions are not okay. As checking
for the conditions requires a few lines of code and is always the same, a
simple approach is to include another script which does the check and
aborts the run if necessary, eg:

```sql
-- this script should not run in "prod"!
\ir not_in_prod.sql
-- should have aborted if it is a "prod" version.
DROP TABLE AllMyUsers CASCADE;
DROP TABLE QuiteImportantData CASCADE;
```

> It has no way of knowing what the next -f option is or what the user
> intended.

The intention of the user who wrote the script is to abort in some cases,
to avoid damaging the database contents.

--
Fabien.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Fabien COELHO 2022-11-20 07:54:53 Re: How to *really* quit psql?
Previous Message Andres Freund 2022-11-20 06:23:35 Re: More efficient build farm animal wakeup?