Thread: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

Starting with the below commit, pg_stat_reset_single_function_counters,
pg_stat_reset_single_table_counters don't just reset the stats for the
individual function, but also set pg_stat_database.stats_reset.

commit 4c468b37a281941afd3bf61c782b20def8c17047
Author: Magnus Hagander <magnus@hagander.net>
Date:   2011-02-10 15:09:35 +0100

    Track last time for statistics reset on databases and bgwriter

    Tracks one counter for each database, which is reset whenever
    the statistics for any individual object inside the database is
    reset, and one counter for the background writer.

    Tomas Vondra, reviewed by Greg Smith
     /*


@@ -4107,6 +4118,8 @@ pgstat_recv_resetsinglecounter(PgStat_MsgResetsinglecounter *msg, int len)
     if (!dbentry)
         return;
 
+    /* Set the reset timestamp for the whole database */
+    dbentry->stat_reset_timestamp = GetCurrentTimestamp();
 
     /* Remove object if it exists, ignore it if not */
     if (msg->m_resettype == RESET_TABLE)


The relevant thread is [1], with the most-on-point message at [2].
pg_stat_reset_single_*_counters were introduced in [3]


This behaviour can be trivially (and is) implemented for the shared memory
stats patch. But every time I read over that part of the code it feels just
profoundly wrong to me.  Way worse than *not* resetting
pg_stat_database.stats_reset.

Anybody that uses the time since the stats reset as part of a calculation of
transactions / sec, reads / sec or such will get completely bogus results
after a call to pg_stat_reset_single_table_counters().


Maybe I just don't understand what these reset functions are intended for?
Their introduction [3] didn't explain much either. To me the behaviour of
resetting pg_stat_database.stats_reset but nothing else in pg_stat_database
makes them kind of dangerous.

Greetings,

Andres Freund

[1] https://postgr.es/m/7177d0cd40b82409024e7c495e9d6992.squirrel%40sq.gransy.com
[2] https://www.postgresql.org/message-id/4D0E5A54.3060302%40fuzzy.cz
[3] https://www.postgresql.org/message-id/9837222c1001240837r5c103519lc6a74c37be5f1831%40mail.gmail.com



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-23 17:55:16 -0700, Andres Freund wrote:
> Maybe I just don't understand what these reset functions are intended for?
> Their introduction [3] didn't explain much either. To me the behaviour of
> resetting pg_stat_database.stats_reset but nothing else in pg_stat_database
> makes them kind of dangerous.

Forgot to add: At the very least we should document that weird behaviour,
because it's certainly not obvious.  But imo we should either remove the
behaviour or drop the functions.

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm>
         <primary>pg_stat_reset_single_table_counters</primary>
        </indexterm>
        <function>pg_stat_reset_single_table_counters</function> ( <type>oid</type> )
        <returnvalue>void</returnvalue>
       </para>
       <para>
        Resets statistics for a single table or index in the current database
        or shared across all databases in the cluster to zero.
       </para>
       <para>
        This function is restricted to superusers by default, but other users
        can be granted EXECUTE to run the function.
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm>
         <primary>pg_stat_reset_single_function_counters</primary>
        </indexterm>
        <function>pg_stat_reset_single_function_counters</function> ( <type>oid</type> )
        <returnvalue>void</returnvalue>
       </para>
       <para>
        Resets statistics for a single function in the current database to
        zero.
       </para>
       <para>
        This function is restricted to superusers by default, but other users
        can be granted EXECUTE to run the function.
       </para></entry>
      </row>


Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
"David G. Johnston"
Date:
On Wed, Mar 23, 2022 at 5:55 PM Andres Freund <andres@anarazel.de> wrote:

Starting with the below commit, pg_stat_reset_single_function_counters,
pg_stat_reset_single_table_counters don't just reset the stats for the
individual function, but also set pg_stat_database.stats_reset.

commit 4c468b37a281941afd3bf61c782b20def8c17047
Author: Magnus Hagander <magnus@hagander.net>
Date:   2011-02-10 15:09:35 +0100

    Track last time for statistics reset on databases and bgwriter

    Tracks one counter for each database, which is reset whenever
    the statistics for any individual object inside the database is
    reset, and one counter for the background writer.

    Tomas Vondra, reviewed by Greg Smith
     /*
[...]
Maybe I just don't understand what these reset functions are intended for?
Their introduction [3] didn't explain much either. To me the behaviour of
resetting pg_stat_database.stats_reset but nothing else in pg_stat_database
makes them kind of dangerous.

*tl/dr;*
There seems to be three scopes here:

Cluster Stats - (add last_reset fields for consistency)
Database and Shared Object Stats (add last_reset to handle recording the resetting of just the records on this table, leaving stats_reset to be a flag meaning self-or-children)
Object Stats (add last_reset, don't need stats_reset since there are no children)

If we are OK with just changing pg_stat_database.stats_reset meanings to be less informative than what it is today (I strongly dislike such a silent behavioral change) we could simply standardize on the intended meaning of stats_reset on all three scopes, adding stats_reset as needed to track object-level resetting.

*Additional Exposition*

The description for the column declares that the field is reset when the statistics on the pg_stat_database are reset.  That is also the expected behavior, knowing when any statistics in the whole database are reset is indeed not useful.

"Time at which these statistics were last reset"

The "these" clearly refers to the statistics columns in pg_stat_database.

In fact, pg_stat_archiver.stats_reset already exists (as does pg_bgwriter.stats_reset) with (I presume) this interpretation.  This is a problem because pg_stat_database.stats_reset does not have the same meaning.  So we have to either live with inconsistency or break something.

In the vein of living with inconsistency I would suggest changing the documentation of "pg_stat.database.stats_reset" to match the present behavior.  Then add a new column (last_reset ?) to represent the existing description of "stats_reset".

I suppose we could document "stats_reset" as the itself-or-any-children reset timestamp, it's just that the archive and pgwriter don't have children in this sense while databases do.  When the pg_stat_database.last_reset field changes the pg_stat_database.stats_reset would have to match anyway.

I don't have any issue with an indicator field saying "something regarding stats has changed" at the database level.  It is much easier to monitor that and then inspect what may have changed rather than monitoring a last_reset column on every single catalog that has statistics that can be reset.

It also seems that each tracked object type needs to have its own last_reset field (we could choose to name it stats_reset too, leaving pg_stat_database.last_reset as the only anomaly) added as an implied behavior needed for such individualized resetting.  I would go with *.last_reset though and leave the absence of pg_stat_archiver.last_reset as the anomaly (or just add it redundantly for consistency).

I don't see removing existing functionality as a good course to getting a consistent implementation; we should just push forward with figuring out what is missing and fill in those gaps.  At worst if that isn't something we want to fix right now our new setup should at least leave the status quo behaviors in place.

I haven't looked into what kind of explicit resetting options are available but the above seems to cover tracking resetting regardless of how it is implemented.  I've only spot checked some of the tables to identify the pattern.

David J.

Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-23 18:47:58 -0700, David G. Johnston wrote:
> It also seems that each tracked object type needs to have its own
> last_reset field (we could choose to name it stats_reset too, leaving
> pg_stat_database.last_reset as the only anomaly) added as an implied
> behavior needed for such individualized resetting.  I would go with
> *.last_reset though and leave the absence of pg_stat_archiver.last_reset as
> the anomaly (or just add it redundantly for consistency).

It's not free to track more information. We always have the stats for the
whole system in memory at least once (stats collector currently, shared hash
table with shared memory stats patch), often more than that (stats accessing
backends).


> I don't see removing existing functionality as a good course to getting a
> consistent implementation; we should just push forward with figuring out
> what is missing and fill in those gaps.  At worst if that isn't something
> we want to fix right now our new setup should at least leave the status quo
> behaviors in place.

Well, it depends on whether there's an actual use case for those super fine
grained reset functions. Neither docs nor the thread introducing them
presented that.  We don't have SQL level stats
values either.

Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Laurenz Albe
Date:
On Wed, 2022-03-23 at 17:55 -0700, Andres Freund wrote:
> Starting with the below commit, pg_stat_reset_single_function_counters,
> pg_stat_reset_single_table_counters don't just reset the stats for the
> individual function, but also set pg_stat_database.stats_reset.

I see the point in the fine-grained reset, but I am -1 on having that
reset "pg_stat_database.stats_reset".  That would make the timestamp
mostly useless.

One could argue that resetting a single counter and *not* resetting
"pg_stat_database.stats_reset" would also be a lie, but at least it is
a smaller lie.

Yours,
Laurenz Albe




Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-24 06:27:48 +0100, Laurenz Albe wrote:
> On Wed, 2022-03-23 at 17:55 -0700, Andres Freund wrote:
> > Starting with the below commit, pg_stat_reset_single_function_counters,
> > pg_stat_reset_single_table_counters don't just reset the stats for the
> > individual function, but also set pg_stat_database.stats_reset.
> 
> I see the point in the fine-grained reset, but I am -1 on having that
> reset "pg_stat_database.stats_reset".  That would make the timestamp
> mostly useless.

Just to be clear - that's the current and long-time behaviour.

Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Tomas Vondra
Date:
On 3/24/22 01:59, Andres Freund wrote:
> Hi,
> 
> On 2022-03-23 17:55:16 -0700, Andres Freund wrote:
>> Maybe I just don't understand what these reset functions are intended for?
>> Their introduction [3] didn't explain much either. To me the behaviour of
>> resetting pg_stat_database.stats_reset but nothing else in pg_stat_database
>> makes them kind of dangerous.
> 
> Forgot to add: At the very least we should document that weird behaviour,
> because it's certainly not obvious.  But imo we should either remove the
> behaviour or drop the functions.
> 

I agree it should have been documented, but I still stand behind the
current behavior. I'm not willing to die on this hill, but I think the
reasoning was/is sound.

Firstly, calculating transactions/second, reads/second just from by
looking at pg_stat_database data (counter and stat_reset) is nonsense.
It might work for short time periods, but for anything longer it's bound
to give you bogus results - you don't even know if the system was
running at all, and so on.

Secondly, to do anything really meaningful you need to calculate deltas,
and be able to detect if some of the stats were reset for the particular
interval. And the stat_reset timestamp was designed to be a simple way
to detect that (instead of having to inspect all individual timestamps).


regards

-- 
Tomas Vondra
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-24 13:12:24 +0100, Tomas Vondra wrote:
> I agree it should have been documented, but I still stand behind the
> current behavior. I'm not willing to die on this hill, but I think the
> reasoning was/is sound.
> 
> Firstly, calculating transactions/second, reads/second just from by
> looking at pg_stat_database data (counter and stat_reset) is nonsense.
> It might work for short time periods, but for anything longer it's bound
> to give you bogus results - you don't even know if the system was
> running at all, and so on.

It's not that you'd use it as the sole means of determining the time
delta. But using it to see if stats were reset between two samples of
pg_stat_database imo makes plenty sense.


> Secondly, to do anything really meaningful you need to calculate deltas,
> and be able to detect if some of the stats were reset for the particular
> interval. And the stat_reset timestamp was designed to be a simple way
> to detect that (instead of having to inspect all individual timestamps).

I wonder if we should just split that per-database timestamp into two. One
about the pg_stat_database contents, one about per-database stats? That
doesn't have the same memory-usage-increase concerns as adding
per-table/function reset stats.

Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
"David G. Johnston"
Date:
On Tue, Mar 29, 2022 at 1:37 PM Andres Freund <andres@anarazel.de> wrote:
> Secondly, to do anything really meaningful you need to calculate deltas,
> and be able to detect if some of the stats were reset for the particular
> interval. And the stat_reset timestamp was designed to be a simple way
> to detect that (instead of having to inspect all individual timestamps).

I wonder if we should just split that per-database timestamp into two. One
about the pg_stat_database contents, one about per-database stats? That
doesn't have the same memory-usage-increase concerns as adding
per-table/function reset stats.


That seems like only half a solution.  The reasoning for doing such a split for pg_stat_database is identical to the reason that new fields should be added to pg_stat_all_tables and pg_stat_user_functions (and possibly others).

pg_stat_all_tables already has 16 bigint fields, 4 timestamptz fields, 2 names and an oid.  Seems like one more timestamptz field is a marginal increase whose presence lets us keep the already implemented per-table reset mechanism.  We should at least measure the impact that adding the field has before deciding its presence is too costly.

But then, I'm going on design theory here, I don't presently have a horse in this race.  And the fact no one has called us on this deficiency (not that I've really been looking) does suggest the status quo is at least realistic to maintain.  But on that basis I would just leave pg_stat_database alone with its single field.  And then explain that the single field covers everything, including the database statistics.  And so while it is possible to reset a subset of the statistics the field really loses its usefulness when that is done because the reset timestamp only applies to a subset.  It only regains its meaning if/when one performs a full stats reset.

David J.



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-29 14:14:05 -0700, David G. Johnston wrote:
> On Tue, Mar 29, 2022 at 1:37 PM Andres Freund <andres@anarazel.de> wrote:
> 
> > > Secondly, to do anything really meaningful you need to calculate deltas,
> > > and be able to detect if some of the stats were reset for the particular
> > > interval. And the stat_reset timestamp was designed to be a simple way
> > > to detect that (instead of having to inspect all individual timestamps).
> >
> > I wonder if we should just split that per-database timestamp into two. One
> > about the pg_stat_database contents, one about per-database stats? That
> > doesn't have the same memory-usage-increase concerns as adding
> > per-table/function reset stats.
> >
> >
> That seems like only half a solution.  The reasoning for doing such a split
> for pg_stat_database is identical to the reason that new fields should be
> added to pg_stat_all_tables and pg_stat_user_functions (and possibly
> others).

Not really IMO. There's obviously the space usage aspect - there's always
fewer pg_stat_database stats than relation stats. But more importantly, a
per-relation/function reset field wouldn't address Tomas's concern: He wants a
single thing to check to see if any stats have been reset - and that's imo a
quite reasonable desire.


> pg_stat_all_tables already has 16 bigint fields, 4 timestamptz fields, 2
> names and an oid.  Seems like one more timestamptz field is a marginal
> increase whose presence lets us keep the already implemented per-table
> reset mechanism.  We should at least measure the impact that adding the
> field has before deciding its presence is too costly.

Because of the desire for a single place to check whether there has been a
reset within a database, that's imo an orthogonal debate.

Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
"David G. Johnston"
Date:
On Tue, Mar 29, 2022 at 4:43 PM Andres Freund <andres@anarazel.de> wrote:
 
But more importantly, a
per-relation/function reset field wouldn't address Tomas's concern: He wants a
single thing to check to see if any stats have been reset - and that's imo a
quite reasonable desire.

Per the original email:

"Starting with the below commit, pg_stat_reset_single_function_counters,
pg_stat_reset_single_table_counters don't just reset the stats for the
individual function, but also set pg_stat_database.stats_reset."

Thus we already have the desired behavior, it is just poorly documented.

Now, maybe other functions aren't doing this?  If so, given these functions do, we probably should just change any outliers to match.

I'm reading Tomas's comments as basically a defense of the status quo, at least so far as the field goes.  He didn't comment on the idea of "drop the reset_[relation|function]_counters(...)" functions.  Combined, I take that as supporting the entire status quo: leaving the function and fields as-is.  I'm inclined to do the same.  I don't see any real benefit to change here as there is no user demand for it and the field change proposal is to change only one of the at least three locations that should be changed if we want to have a consistent design.  And we aren't getting user reports saying the presence of the functions is a problem (confusion or otherwise) either, so unless there is a technical reason writing these functions in the new system is undesirable we have no justification that I can see for removing the long-standing feature.

David J.


Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-29 17:06:24 -0700, David G. Johnston wrote:
> On Tue, Mar 29, 2022 at 4:43 PM Andres Freund <andres@anarazel.de> wrote:
> > But more importantly, a
> > per-relation/function reset field wouldn't address Tomas's concern: He
> > wants a
> > single thing to check to see if any stats have been reset - and that's imo
> > a
> > quite reasonable desire.
> >
> 
> Per the original email:
> 
> "Starting with the below commit, pg_stat_reset_single_function_counters,
> pg_stat_reset_single_table_counters don't just reset the stats for the
> individual function, but also set pg_stat_database.stats_reset."
> 
> Thus we already have the desired behavior, it is just poorly documented.

The problem is that it also make stats_reset useless for other purposes -
which I do consider a problem. Hence this thread.  My concern would be
mollified if I there were a separate reset timestamp counting the last
"database wide" reset time. Your comment about that was something about
relation/function level timestamps, which doesn't seem relevant.


> Now, maybe other functions aren't doing this?  If so, given these functions
> do, we probably should just change any outliers to match.

Don't think there are other functions.

Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
"David G. Johnston"
Date:
On Tue, Mar 29, 2022 at 5:50 PM Andres Freund <andres@anarazel.de> wrote:
Hi,

On 2022-03-29 17:06:24 -0700, David G. Johnston wrote:
> On Tue, Mar 29, 2022 at 4:43 PM Andres Freund <andres@anarazel.de> wrote:
> > But more importantly, a
> > per-relation/function reset field wouldn't address Tomas's concern: He
> > wants a
> > single thing to check to see if any stats have been reset - and that's imo
> > a
> > quite reasonable desire.
> >
>
> Per the original email:
>
> "Starting with the below commit, pg_stat_reset_single_function_counters,
> pg_stat_reset_single_table_counters don't just reset the stats for the
> individual function, but also set pg_stat_database.stats_reset."
>
> Thus we already have the desired behavior, it is just poorly documented.

The problem is that it also make stats_reset useless for other purposes -
which I do consider a problem. Hence this thread.  My concern would be
mollified if I there were a separate reset timestamp counting the last
"database wide" reset time. Your comment about that was something about
relation/function level timestamps, which doesn't seem relevant.

I can't figure out whether you agree that as of today stats_reset is the "database wide" reset time.  The first sentence makes it sound like you do, the first one makes it sound like you don't.

David J.

Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
"David G. Johnston"
Date:
On Tue, Mar 29, 2022 at 5:56 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Tue, Mar 29, 2022 at 5:50 PM Andres Freund <andres@anarazel.de> wrote:
Hi,

On 2022-03-29 17:06:24 -0700, David G. Johnston wrote:
> On Tue, Mar 29, 2022 at 4:43 PM Andres Freund <andres@anarazel.de> wrote:
> > But more importantly, a
> > per-relation/function reset field wouldn't address Tomas's concern: He
> > wants a
> > single thing to check to see if any stats have been reset - and that's imo
> > a
> > quite reasonable desire.
> >
>
> Per the original email:
>
> "Starting with the below commit, pg_stat_reset_single_function_counters,
> pg_stat_reset_single_table_counters don't just reset the stats for the
> individual function, but also set pg_stat_database.stats_reset."
>
> Thus we already have the desired behavior, it is just poorly documented.

The problem is that it also make stats_reset useless for other purposes -
which I do consider a problem. Hence this thread.  My concern would be
mollified if I there were a separate reset timestamp counting the last
"database wide" reset time. Your comment about that was something about
relation/function level timestamps, which doesn't seem relevant.

I can't figure out whether you agree that as of today stats_reset is the "database wide" reset time.  The first sentence makes it sound like you do, the first one makes it sound like you don't.

OK, I meant the third one seems contrary, but re-reading this all again I think I see what you are saying.

You want to add a field that only changes when "reset all stats" is executed for a given database.  Leaving stats_reset to mean "the last time any individual stat record changed".  I can get behind that.

David J.

Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Robert Haas
Date:
On Wed, Mar 23, 2022 at 8:55 PM Andres Freund <andres@anarazel.de> wrote:
> This behaviour can be trivially (and is) implemented for the shared memory
> stats patch. But every time I read over that part of the code it feels just
> profoundly wrong to me.  Way worse than *not* resetting
> pg_stat_database.stats_reset.
>
> Anybody that uses the time since the stats reset as part of a calculation of
> transactions / sec, reads / sec or such will get completely bogus results
> after a call to pg_stat_reset_single_table_counters().

Sure, but that's unavoidable anyway. If some stats have been reset and
other stats have not, you can't calculate a meaningful average over
time unless you have a specific reset time for each statistic.

To me, the current behavior feels more correct than what you propose.
Imagine for example that you are looking for tables/indexes where the
counters are 0 as a way of finding unused objects. If you know that no
counters have been zeroed in a long time, you know that this is
reliable. But under your proposal, there's no way to know this. All
you know is that the entire system wasn't reset, and therefore some of
the 0s that you are seeing might be for individual objects that were
reset.

I think of this mechanism like as answering the question "When's the
last time anybody tinkered with this thing by hand?". If it's recent,
the tinkering has a good chance of being related to whatever problem
I'm trying to solve. If it's not, it's probably unrelated.

-- 
Robert Haas
EDB: http://www.enterprisedb.com



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-30 14:57:25 -0400, Robert Haas wrote:
> On Wed, Mar 23, 2022 at 8:55 PM Andres Freund <andres@anarazel.de> wrote:
> > This behaviour can be trivially (and is) implemented for the shared memory
> > stats patch. But every time I read over that part of the code it feels just
> > profoundly wrong to me.  Way worse than *not* resetting
> > pg_stat_database.stats_reset.
> >
> > Anybody that uses the time since the stats reset as part of a calculation of
> > transactions / sec, reads / sec or such will get completely bogus results
> > after a call to pg_stat_reset_single_table_counters().
> 
> Sure, but that's unavoidable anyway. If some stats have been reset and
> other stats have not, you can't calculate a meaningful average over
> time unless you have a specific reset time for each statistic.

Individual pg_stat_database columns can't be reset independently. Other views
summarizing large parts of the system, like pg_stat_bgwriter, pg_stat_wal etc
have a stats_reset column that is only reset if their counters is also
reset. So the only reason we can't do that for pg_stat_database is that we
don't know since when pg_stat_database counters are counting.


> To me, the current behavior feels more correct than what you propose.
> Imagine for example that you are looking for tables/indexes where the
> counters are 0 as a way of finding unused objects. If you know that no
> counters have been zeroed in a long time, you know that this is
> reliable. But under your proposal, there's no way to know this. All
> you know is that the entire system wasn't reset, and therefore some of
> the 0s that you are seeing might be for individual objects that were
> reset.

My current proposal is to just have two reset times. One for the contents of
pg_stat_database (i.e. not affected by pg_stat_reset_single_*_counters()), and
one for stats within the entire database.


> I think of this mechanism like as answering the question "When's the
> last time anybody tinkered with this thing by hand?". If it's recent,
> the tinkering has a good chance of being related to whatever problem
> I'm trying to solve. If it's not, it's probably unrelated.

When I look at a database with a problem, I'll often look at pg_stat_database
to get a first impression of the type of workload running. The fact that
stats_reset doesn't reflect the age of other pg_stat_database columns makes
that much harder.

Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
"David G. Johnston"
Date:
On Wednesday, March 30, 2022, Andres Freund <andres@anarazel.de> wrote:

My current proposal is to just have two reset times. One for the contents of
pg_stat_database (i.e. not affected by pg_stat_reset_single_*_counters()), and
one for stats within the entire database.


What IS it affected by?  And does whatever affects it affect anything else?

David J. 

Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
Andres Freund
Date:
Hi,

On 2022-03-30 12:29:51 -0700, David G. Johnston wrote:
> On Wednesday, March 30, 2022, Andres Freund <andres@anarazel.de> wrote:
> > My current proposal is to just have two reset times. One for the contents
> > of
> > pg_stat_database (i.e. not affected by pg_stat_reset_single_*_counters()),
> > and
> > one for stats within the entire database.

> What IS it affected by?  And does whatever affects it affect anything else?

pg_stat_reset() resets the current database's stats. That includes the
database's row in pg_stat_database and all table and function stats.

Greetings,

Andres Freund



Re: pg_stat_reset_single_*_counters vs pg_stat_database.stats_reset

From
"David G. Johnston"
Date:
On Wed, Mar 30, 2022 at 1:39 PM Andres Freund <andres@anarazel.de> wrote:
Hi,

On 2022-03-30 12:29:51 -0700, David G. Johnston wrote:
> On Wednesday, March 30, 2022, Andres Freund <andres@anarazel.de> wrote:
> > My current proposal is to just have two reset times. One for the contents
> > of
> > pg_stat_database (i.e. not affected by pg_stat_reset_single_*_counters()),
> > and
> > one for stats within the entire database.

> What IS it affected by?  And does whatever affects it affect anything else?

pg_stat_reset() resets the current database's stats. That includes the
database's row in pg_stat_database and all table and function stats.


Right, so basically it updates both of the fields you are talking about.

The existing stats_reset field is also updated upon calling pg_stat_reset_single_*_counters()

So when the two fields are different we know that at least one relation or function statistic row is out-of-sync with the rest of the database, we just don't know which one(s).  This is an improvement over the status quo where the single timestamp cannot be trusted to mean anything useful.  The DBA can execute pg_stat_reset() to get the statistics back into a common state.

As an added bonus we will always have a reference timestamp for when the pg_stat_database database record was last reset (as well as any other statistic record that can only be reset by using pg_stat_reset).

David J.