Re: Feature improvement: can we add queryId for pg_catalog.pg_stat_activity view?

From: Nitin Jadhav <nitinjadhavpostgres(at)gmail(dot)com>
To: Julien Rouhaud <rjuju123(at)gmail(dot)com>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>, Hannu Krosing <hannuk(at)google(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Michael Paquier <michael(at)paquier(dot)xyz>, torikoshia <torikoshia(at)oss(dot)nttdata(dot)com>, Atsushi Torikoshi <atorik(at)gmail(dot)com>, Tatsuro Yamada <tatsuro(dot)yamada(dot)tf(at)nttcom(dot)co(dot)jp>, Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>, Evgeny Efimkin <efimkin(at)yandex-team(dot)ru>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Feature improvement: can we add queryId for pg_catalog.pg_stat_activity view?
Date: 2021-04-07 12:45:27
Message-ID: CAMm1aWbTTc7AJrAZa7q1QgNNqdNkFP_-owKHDR=zEjQGqevnpg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>
> >
> > 1.
> > +void
> > +pgstat_report_queryid(uint64 queryId, bool force)
> > +{
> > + volatile PgBackendStatus *beentry = MyBEEntry;
> > +
> > + if (!beentry)
> > + return;
> > +
> > + /*
> > + * if track_activities is disabled, st_queryid should already have been
> > + * reset
> > + */
> > + if (!pgstat_track_activities)
> > + return;
> >
> > The above two conditions can be clubbed together in a single condition.
> Right, I just kept it separate as the comment is only relevant for the 2nd
> test. I'm fine with merging both if needed.

I feel we should merge both of the conditions as it is done in
pgstat_report_xact_timestamp(). Probably we can write a common comment to
explain both the conditions.

> 2.
> > +/* ----------
> > + * pgstat_get_my_queryid() -
> > + *
> > + * Return current backend's query identifier.
> > + */
> > +uint64
> > +pgstat_get_my_queryid(void)
> > +{
> > + if (!MyBEEntry)
> > + return 0;
> > +
> > + return MyBEEntry->st_queryid;
> > +}
> >
> > Is it safe to directly read the data from MyBEEntry without
> > calling pgstat_begin_read_activity() and pgstat_end_read_activity().
> Kindly
> > ref pgstat_get_backend_current_activity() for more information. Kindly
> let
> > me know if I am wrong.
> This field is only written by a backend for its own entry.
> pg_stat_get_activity already has required protection, so the rest of the
> calls
> to read that field shouldn't have any risk of reading torn values on
> platform
> where this isn't an atomic operation due to concurrent write, as it will be
> from the same backend that originally wrote it. It avoids some overhead to
> retrieve the queryid, but if people think it's worth having the loop (or a
> comment explaining why there's no loop) I'm also fine with it.

Thanks for the explanation. Please add a comment explaining why there is no
loop.

Thanks and Regards,
Nitin Jadhav

On Tue, Apr 6, 2021 at 8:40 PM Julien Rouhaud <rjuju123(at)gmail(dot)com> wrote:

> On Tue, Apr 06, 2021 at 08:05:19PM +0530, Nitin Jadhav wrote:
> >
> > 1.
> > +void
> > +pgstat_report_queryid(uint64 queryId, bool force)
> > +{
> > + volatile PgBackendStatus *beentry = MyBEEntry;
> > +
> > + if (!beentry)
> > + return;
> > +
> > + /*
> > + * if track_activities is disabled, st_queryid should already have been
> > + * reset
> > + */
> > + if (!pgstat_track_activities)
> > + return;
> >
> > The above two conditions can be clubbed together in a single condition.
>
> Right, I just kept it separate as the comment is only relevant for the 2nd
> test. I'm fine with merging both if needed.
>
> > 2.
> > +/* ----------
> > + * pgstat_get_my_queryid() -
> > + *
> > + * Return current backend's query identifier.
> > + */
> > +uint64
> > +pgstat_get_my_queryid(void)
> > +{
> > + if (!MyBEEntry)
> > + return 0;
> > +
> > + return MyBEEntry->st_queryid;
> > +}
> >
> > Is it safe to directly read the data from MyBEEntry without
> > calling pgstat_begin_read_activity() and pgstat_end_read_activity().
> Kindly
> > ref pgstat_get_backend_current_activity() for more information. Kindly
> let
> > me know if I am wrong.
>
> This field is only written by a backend for its own entry.
> pg_stat_get_activity already has required protection, so the rest of the
> calls
> to read that field shouldn't have any risk of reading torn values on
> platform
> where this isn't an atomic operation due to concurrent write, as it will be
> from the same backend that originally wrote it. It avoids some overhead to
> retrieve the queryid, but if people think it's worth having the loop (or a
> comment explaining why there's no loop) I'm also fine with it.
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nitin Jadhav 2021-04-07 12:47:11 Re: Feature improvement: can we add queryId for pg_catalog.pg_stat_activity view?
Previous Message Ashutosh Bapat 2021-04-07 12:34:28 Re: CREATE SEQUENCE with RESTART option