Re: PSQL commands: \quit_if, \quit_unless

From: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Andrew Dunstan <andrew(at)dunslane(dot)net>, PostgreSQL <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PSQL commands: \quit_if, \quit_unless
Date: 2017-01-18 16:24:10
Message-ID: CADkLM=fSz3v5kGmudoktOWBO1btjhsq5Rz3Xb8ismmXU3c8F=g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Jan 18, 2017 at 12:08 AM, Michael Paquier <michael(dot)paquier(at)gmail(dot)com
> wrote:

> On Wed, Jan 18, 2017 at 3:24 AM, Robert Haas <robertmhaas(at)gmail(dot)com>
> wrote:
> > On Sat, Jan 14, 2017 at 12:22 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >>
> >> $ cat loop.sql
> >> \if :x < 1000
> >> \echo :x
> >> \set x :x + 1
> >> \include loop.sql
> >> \fi
> >> $ psql --set x=0 -f loop.sql
> >>
> >> Somebody is going to think of that workaround for not having loops, and
> >> then whine about how psql runs out of file descriptors and/or stack.
> >
> > Hmm, I think somebody just DID think of it.
> >
> > But personally this doesn't upset me a bit. If somebody complains
> > about that particular thing, I think that would be an excellent time
> > to suggest that they write a patch to add a looping construct.
>
> Agreed.
>
> As far as I can see on this thread, something could be done, it is
> just that we don't know yet at which extent things could be done with
> the first shot. There are many things that could be done, but at least
> I'd suggest to get \if, \fi and \quit to satisfy the first
> requirements of this thread, and let loops out of it. I have switched
> the patch as "returned with feedback" as getting a new patch is going
> to require some thoughts to get the context handling done correctly on
> psql side.
> --
> Michael
>

Fabien is pressed for time, so I've been speaking with him out-of-thread
about how I should go about implementing it.

The v1 patch will be \if <expr>, \elseif <expr>, \else, \endif, where <expr>
will be naively evaluated via ParseVariableBool().

\ifs and \endifs must be in the same "file" (each MainLoop will start a new
if-stack). This is partly for sanity (you can see the pairings unless the
programmer is off in \gset meta-land), partly for ease of design (data
structures live in MainLoop), but mostly because it would an absolute
requirement if we ever got around to doing \while.

I hope to have something ready for the next commitfest.

As for the fate of \quit_if, I can see it both ways. On the one hand, it's
super-simple, already written, and handy.

On the other hand, it's easily replaced by

\if <expr>
\q
\endif

So I'll leave that as a separate reviewable patch.

As for loops, I don't think anyone was pushing for implementing \while now,
only to have a decision about what it would look like and how it would
work. There's a whole lot of recording infrastructure (the input could be a
stream) needed to make it happen. Moreover, I think \gexec scratched a lot
of the itches that would have been solved via a psql looping structure.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Karl O. Pinc 2017-01-18 16:26:43 Re: Patch to implement pg_current_logfile() function
Previous Message Tom Lane 2017-01-18 16:12:13 Re: move collation import to backend