Prevent psql \watch from running queries that return no rows - Mailing list pgsql-hackers

From Greg Sabino Mullane
Subject Prevent psql \watch from running queries that return no rows
Date
Msg-id CAKAnmmKStATuddYxP71L+p0DHtp9Rvjze3XRoy0Dyw67VQ45UA@mail.gmail.com
Whole thread Raw
Responses Re: Prevent psql \watch from running queries that return no rows
List pgsql-hackers
Attached is a patch to allow a new behavior for the \watch command in psql. When enabled, this instructs \watch to stop running once the query returns zero rows. The use case is the scenario in which you are watching the output of something long-running such as pg_stat_progress_create_index, but once it finishes you don't need thousands of runs showing empty rows from the view.

This adds a new argument "zero" to the existing i=SEC and c=N arguments

Notes:

* Not completely convinced of the name "zero" (better than "stop_when_no_rows_returned"). Considered adding a new x=y argument, or overloading c (c=-1) but neither seemed very intuitive. On the other hand, it's tempting to stick to a single method moving forward, although this is a boolean option not a x=y one like the other two.

* Did not update help.c on purpose - no need to make \watch span two lines there.

* Considered leaving early (e.g. don't display the last empty result) but seemed better to show the final empty result as an explicit confirmation as to why it stopped.

* Quick way to test:
select * from pg_stat_activity where backend_start > now() - '20 seconds'::interval;
\watch zero

Cheers,
Greg

Attachment

pgsql-hackers by date:

Previous
From: "Tristan Partin"
Date:
Subject: Re: [PATCH] Missing dep on Catalog.pm in meson rules
Next
From: James Coleman
Date:
Subject: Re: RFC: Logging plan of the running query