Re: proposal - psql - use pager for \watch command
Pavel Stehule <pavel.stehule@gmail.com>
From: Pavel Stehule <pavel.stehule@gmail.com>
To: Thomas Munro <thomas.munro@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2021-03-03T18:16:47Z
Lists: pgsql-hackers
Attachments
- PSQL_WATCH_PAGER-20200303.patch (text/x-patch) patch
Hi út 16. 2. 2021 v 2:49 odesílatel Thomas Munro <thomas.munro@gmail.com> napsal: > On Fri, Jan 8, 2021 at 10:36 PM Pavel Stehule <pavel.stehule@gmail.com> > wrote: > > ne 19. 4. 2020 v 19:27 odesílatel Pavel Stehule <pavel.stehule@gmail.com> > napsal: > >> last week I finished pspg 3.0 https://github.com/okbob/pspg . pspg now > supports pipes, named pipes very well. Today the pspg can be used as pager > for output of \watch command. Sure, psql needs attached patch. > >> > >> I propose new psql environment variable PSQL_WATCH_PAGER. When this > variable is not empty, then \watch command starts specified pager, and > redirect output to related pipe. When pipe is closed - by pager, then > \watch cycle is leaved. > >> > >> If you want to test proposed feature, you need a pspg with > cb4114f98318344d162a84b895a3b7f8badec241 commit. > >> > >> Then you can set your env > >> > >> export PSQL_WATCH_PAGER="pspg --stream" > >> psql > >> > >> SELECT * FROM pg_stat_database; > >> \watch 1 > >> > >> Comments, notes? > > I tried this out with pspg 4.1 from my package manager. It seems > really useful, especially for demos. I like it! > > * Set up rendering options, in particular, disable the pager, > because > * nobody wants to be prompted while watching the output of > 'watch'. > */ > - myopt.topt.pager = 0; > + if (!pagerpipe) > + myopt.topt.pager = 0; > > Obsolete comment. > fixed > +static bool sigpipe_received = false; > > This should be "static volatile sig_atomic_t", and I suppose our > convention name for that variable would be got_SIGPIPE. Would it be > possible to ignore SIGPIPE instead, and then rely on another way of > knowing that the pager has quit? But... hmm: > > - long s = Min(i, 1000L); > + long s = Min(i, pagerpipe ? 100L : > 1000L); > > I haven't studied this (preexisting) polling loop, but I don't like > it. I understand that it's there because on some systems, pg_usleep() > won't wake up for SIGINT (^C), but now it's being used for a secondary > purpose, that I haven't fully understood. After I quit pspg (by > pressing q) while running \watch 10, I have to wait until the end of a > 10 second cycle before it tries to write to the pipe again, unless I > also press ^C. I feel like it has to be possible to achieve "precise" > behaviour somehow when you quit; maybe something like waiting for > readiness on the pager's stderr, or something like that -- I haven't > thought hard about this and I admit that I have no idea how this works > on Windows. > > I rewrote this mechanism (it was broken, because the timing of SIGPIPE is different, then I expected). An implementation can be significantly simpler - just detect with waitpid any closed child and react. You proposed it. Sometimes I see a message like this after I quit pspg: > > postgres=# \watch 10 > input stream was closed > I don't see this message. But I use fresh 4.3 pspg please, see attached patch Regards Pavel
Commits
-
Fix \watch's interaction with libedit on ^C.
- 7c5953b7b5dc 10.22 landed
- 21ed12b14a32 11.17 landed
- 09224a35cdaa 12.12 landed
- e5b5b4448ce0 13.8 landed
- ab7fef0acfbd 14.5 landed
- 31b485f883a7 15.0 landed
- 21267b647719 16.0 landed
-
Add PSQL_WATCH_PAGER for psql's \watch command.
- 7c09d2797ecd 15.0 landed
-
psql: Show all query results by default
- 3a5130672296 14.0 cited
-
Add a comment warning against use of pg_usleep() for long sleeps.
- 8c1a71d36f5d 9.4.0 cited