pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
conditionally terminate psql script
11+ messages / 8 participants
[nested] [flat]

* conditionally terminate psql script
@ 2018-12-17 12:13  hamann.w@t-online.de
  0 siblings, 2 replies; 11+ messages in thread

From: hamann.w@t-online.de @ 2018-12-17 12:13 UTC (permalink / raw)
  To: 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

Best regards
Wolfgang





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-17 12:42  Pavel Stehule <pavel.stehule@gmail.com>
  parent: hamann.w@t-online.de
  1 sibling, 2 replies; 11+ messages in thread

From: Pavel Stehule @ 2018-12-17 12:42 UTC (permalink / raw)
  To: hamann.w@t-online.de; +Cc: pgsql-general

Hi

po 17. 12. 2018 v 13:14 odesílatel <hamann.w@t-online.de> napsal:

>
>
> 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
>

you need psql from PostgreSQL 10 and higher

there is a \if statement

Regards

Pavel

>
> Best regards
> Wolfgang
>
>
>

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-17 12:43  Pavel Luzanov <p.luzanov@postgrespro.ru>
  parent: hamann.w@t-online.de
  1 sibling, 0 replies; 11+ messages in thread

From: Pavel Luzanov @ 2018-12-17 12:43 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-17 13:07  hamann.w@t-online.de
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 3 replies; 11+ messages in thread

From: hamann.w@t-online.de @ 2018-12-17 13:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi,

many thanks -- too bad I am still using 9.3

Best regards
Wolfgang

>> Hi
>> >> po 17. 12. 2018 v 13:14 odesílatel <hamann.w@t-online.de> napsal:
>> >> >
>> >
>> > 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
>> >
>> >> you need psql from PostgreSQL 10 and higher
>> >> there is a \if statement
>> >> Regards
>> >> Pavel
>> >> >
>> > Best regards
>> > Wolfgang
>> >
>> >
>> >
>> 








^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-17 13:30  Pavel Luzanov <p.luzanov@postgrespro.ru>
  parent: hamann.w@t-online.de
  2 siblings, 1 reply; 11+ messages in thread

From: Pavel Luzanov @ 2018-12-17 13:30 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


On 17.12.2018 16:07, hamann.w@t-online.de wrote:
> Hi, many thanks -- too bad I am still using 9.3

In this case you can try ON_ERROR_STOP psql variable.
Something like this:

\set ON_ERROR_STOP on

do $$
declare
     total bigint;
begin
     select count(*) into total from pg_class where 1=1;
     if total = 0 then
         raise exception 'Nothing found.';
     end if;

     raise notice '% records found.', total;
end;
$$ language plpgsql;

\echo Continue execution...

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






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-17 14:31  hamann.w@t-online.de
  parent: Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 0 replies; 11+ messages in thread

From: hamann.w@t-online.de @ 2018-12-17 14:31 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi,

many thanks, I will give it a try tomorrow

Best regards
Wolfgang

>> >> On 17.12.2018 16:07, hamann.w@t-online.de wrote:
>> > Hi, many thanks -- too bad I am still using 9.3
>> >> In this case you can try ON_ERROR_STOP psql variable.
>> Something like this:
>> >> \set ON_ERROR_STOP on
>> >> do $$
>> declare
>>      total bigint;
>> begin
>>      select count(*) into total from pg_class where 1=1;
>>      if total = 0 then
>>          raise exception 'Nothing found.';
>>      end if;
>> >>      raise notice '% records found.', total;
>> end;
>> $$ language plpgsql;
>> >> \echo Continue execution...
>> >> -----
>> Pavel Luzanov
>> Postgres Professional: http://www.postgrespro.com
>> The Russian Postgres Company
>> >> >> 








^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-17 15:01  Jerry Sievers <jerry.sievers@comcast.net>
  parent: hamann.w@t-online.de
  2 siblings, 1 reply; 11+ messages in thread

From: Jerry Sievers @ 2018-12-17 15:01 UTC (permalink / raw)
  To: hamann.w@t-online.de; +Cc: pgsql-general@lists.postgresql.org

hamann.w@t-online.de writes:

> Hi,
>
> many thanks -- too bad I am still using 9.3

Just because your server backend is 9.3 does not rule out using much
newer clients, such as psql.

HTH

>
> Best regards
> Wolfgang
>
>>> Hi
>>> >> po 17. 12. 2018 v 13:14 odesílatel <hamann.w@t-online.de> napsal:
>>> >> >
>>> >
>>> > 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
>>> >
>>> >> you need psql from PostgreSQL 10 and higher
>>> >> there is a \if statement
>>> >> Regards
>>> >> Pavel
>>> >> >
>>> > Best regards
>>> > Wolfgang
>>> >
>>> >
>>> >
>>> 
>
>
>
>
>
>

-- 
Jerry Sievers
e: jerry.sievers@comcast.net
p: 312.241.7800




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-17 16:16  Ron <ronljohnsonjr@gmail.com>
  parent: Jerry Sievers <jerry.sievers@comcast.net>
  0 siblings, 0 replies; 11+ messages in thread

From: Ron @ 2018-12-17 16:16 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 12/17/2018 09:01 AM, Jerry Sievers wrote:
> hamann.w@t-online.de writes:
>
>> Hi,
>>
>> many thanks -- too bad I am still using 9.3
> Just because your server backend is 9.3 does not rule out using much
> newer clients, such as psql.

While technically true, and is useful, many production servers (especially 
ones that must be PCI compliant) heavily restrict who can remotely connect 
to the database, and so superusers are stuck with what's installed, since 
installing new stuff has enough paperwork and bureaucracy to make Kafka weep.

(Of course, if you must be PCI compliant, you should get off 9.3 before the 
auditors drop the hammer on you.)

-- 
Angular momentum makes the world go 'round.




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-18 21:42  Steven Lembark <lembark@wrkhors.com>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 1 reply; 11+ messages in thread

From: Steven Lembark @ 2018-12-18 21:42 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; +Cc: lembark@wrkhors.com

On Mon, 17 Dec 2018 13:42:14 +0100
Pavel Stehule <pavel.stehule@gmail.com> wrote:

> you need psql from PostgreSQL 10 and higher
> 
> there is a \if statement

Q: What is the "abort cycle command"?

e.g., 


    select  count(1) < 1 as "lacks_rows";
    from    foobar
    where   blah blah
    \gset

    \if :lacks_rows

        \echo foobar lacks rows to process.
        \echo goodnight :-)

        ?????????    <--- what goes here to stop execution?

    \endif


The point is that adding many levels of if-block logic is becomes
difficult to maintain. It would be nice to stop execution without
having to nest everything one level deeper.



-- 
Steven Lembark                                     3920 10th Ave South
Workhorse Computing                               Birmingham, AL 35222
lembark@wrkhors.com                                    +1 888 359 3508




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-18 21:47  David G. Johnston <david.g.johnston@gmail.com>
  parent: Steven Lembark <lembark@wrkhors.com>
  0 siblings, 0 replies; 11+ messages in thread

From: David G. Johnston @ 2018-12-18 21:47 UTC (permalink / raw)
  To: Steven Lembark <lembark@wrkhors.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Tue, Dec 18, 2018 at 2:44 PM Steven Lembark <lembark@wrkhors.com> wrote:
> > there is a \if statement
>
> Q: What is the "abort cycle command"?
[...]
>     \if :lacks_rows
>
>         \echo foobar lacks rows to process.
>         \echo goodnight :-)
>
>         ?????????    <--- what goes here to stop execution?

\quit

David J.




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: conditionally terminate psql script
@ 2018-12-19 01:25  Torsten Förtsch <tfoertsch123@gmail.com>
  parent: hamann.w@t-online.de
  2 siblings, 0 replies; 11+ messages in thread

From: Torsten Förtsch @ 2018-12-19 01:25 UTC (permalink / raw)
  To: hamann.w@t-online.de; +Cc: pgsql-general@lists.postgresql.org

On Mon, Dec 17, 2018 at 2:07 PM <hamann.w@t-online.de> wrote:

> many thanks -- too bad I am still using 9.3
>
>
not sure if it has been mentioned in the thread so far. But according to
the docs, 9.3 psql does support the \gset command. So you should be able to
do something like this:

select case when exists (select 1 from pg_namespace where nspname='tf')
            then '\echo schema tf exists \\ \q'
            else '\echo schema does not exist -- creating ...'
       end as cmd\gset
:cmd

create schema tf;
create table tf.i(i int);

^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2018-12-19 01:25 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-17 12:13 conditionally terminate psql script hamann.w@t-online.de
2018-12-17 12:42 ` Pavel Stehule <pavel.stehule@gmail.com>
2018-12-17 13:07   ` hamann.w@t-online.de
2018-12-17 13:30     ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2018-12-17 14:31       ` hamann.w@t-online.de
2018-12-17 15:01     ` Jerry Sievers <jerry.sievers@comcast.net>
2018-12-17 16:16       ` Ron <ronljohnsonjr@gmail.com>
2018-12-19 01:25     ` Torsten Förtsch <tfoertsch123@gmail.com>
2018-12-18 21:42   ` Steven Lembark <lembark@wrkhors.com>
2018-12-18 21:47     ` David G. Johnston <david.g.johnston@gmail.com>
2018-12-17 12:43 ` Pavel Luzanov <p.luzanov@postgrespro.ru>

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