public inbox for [email protected]
help / color / mirror / Atom feedRe: is pg_stat_activity "transactional"? How fast does it update?
4+ messages / 3 participants
[nested] [flat]
* Re: is pg_stat_activity "transactional"? How fast does it update?
@ 2025-06-10 14:27 Tom Lane <[email protected]>
2025-06-10 15:46 ` Re: is pg_stat_activity "transactional"? How fast does it update? Dominique Devienne <[email protected]>
0 siblings, 1 reply; 4+ messages in thread
From: Tom Lane @ 2025-06-10 14:27 UTC (permalink / raw)
To: Dominique Devienne <[email protected]>; +Cc: [email protected]
Dominique Devienne <[email protected]> writes:
> So when and how fast does pg_stat_activity update, in the face of a
> PQfinish?
The shared state underlying pg_stat_activity is updated
immediately when something changes. However ...
PQfinish is asynchronous: it sends a "goodbye" message and then
closes the connection without waiting for a response. So
depending on system load, the associated backend could still be
alive for some milliseconds. There might be other client
libraries that do that differently.
Another thing that might be biting you is that a backend inspecting
pg_stat_activity() takes a snapshot of the view's contents and then
holds onto that snapshot until end of transaction. You can get
around that with pg_stat_clear_snapshot().
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: is pg_stat_activity "transactional"? How fast does it update?
2025-06-10 14:27 Re: is pg_stat_activity "transactional"? How fast does it update? Tom Lane <[email protected]>
@ 2025-06-10 15:46 ` Dominique Devienne <[email protected]>
2025-06-10 16:03 ` Re: is pg_stat_activity "transactional"? How fast does it update? Adrian Klaver <[email protected]>
2025-06-11 09:26 ` Re: is pg_stat_activity "transactional"? How fast does it update? Dominique Devienne <[email protected]>
0 siblings, 2 replies; 4+ messages in thread
From: Dominique Devienne @ 2025-06-10 15:46 UTC (permalink / raw)
To: Tom Lane <[email protected]>; +Cc: [email protected]
On Tue, Jun 10, 2025 at 4:27 PM Tom Lane <[email protected]> wrote:
> Dominique Devienne <[email protected]> writes:
> > So when and how fast does pg_stat_activity update, in the face of a
> > PQfinish?
>
> The shared state underlying pg_stat_activity is updated
> immediately when something changes. However ...
>
> PQfinish is asynchronous: it sends a "goodbye" message and then
> closes the connection without waiting for a response. So
> depending on system load, the associated backend could still be
> alive for some milliseconds. There might be other client
> libraries that do that differently.
>
> Another thing that might be biting you is that a backend inspecting
> pg_stat_activity() takes a snapshot of the view's contents and then
> holds onto that snapshot until end of transaction. You can get
> around that with pg_stat_clear_snapshot().
Many thanks Tom. Didn't know either of those facts. --DD
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: is pg_stat_activity "transactional"? How fast does it update?
2025-06-10 14:27 Re: is pg_stat_activity "transactional"? How fast does it update? Tom Lane <[email protected]>
2025-06-10 15:46 ` Re: is pg_stat_activity "transactional"? How fast does it update? Dominique Devienne <[email protected]>
@ 2025-06-10 16:03 ` Adrian Klaver <[email protected]>
1 sibling, 0 replies; 4+ messages in thread
From: Adrian Klaver @ 2025-06-10 16:03 UTC (permalink / raw)
To: Dominique Devienne <[email protected]>; Tom Lane <[email protected]>; +Cc: [email protected]
On 6/10/25 08:46, Dominique Devienne wrote:
> On Tue, Jun 10, 2025 at 4:27 PM Tom Lane <[email protected]> wrote:
>> Dominique Devienne <[email protected]> writes:
>>> So when and how fast does pg_stat_activity update, in the face of a
>>> PQfinish?
>>
>> The shared state underlying pg_stat_activity is updated
>> immediately when something changes. However ...
>>
>> PQfinish is asynchronous: it sends a "goodbye" message and then
>> closes the connection without waiting for a response. So
>> depending on system load, the associated backend could still be
>> alive for some milliseconds. There might be other client
>> libraries that do that differently.
>>
>> Another thing that might be biting you is that a backend inspecting
>> pg_stat_activity() takes a snapshot of the view's contents and then
>> holds onto that snapshot until end of transaction. You can get
>> around that with pg_stat_clear_snapshot().
>
> Many thanks Tom. Didn't know either of those facts. --DD
>
For more detail see:
https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-STATS-VIEWS
--
Adrian Klaver
[email protected]
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: is pg_stat_activity "transactional"? How fast does it update?
2025-06-10 14:27 Re: is pg_stat_activity "transactional"? How fast does it update? Tom Lane <[email protected]>
2025-06-10 15:46 ` Re: is pg_stat_activity "transactional"? How fast does it update? Dominique Devienne <[email protected]>
@ 2025-06-11 09:26 ` Dominique Devienne <[email protected]>
1 sibling, 0 replies; 4+ messages in thread
From: Dominique Devienne @ 2025-06-11 09:26 UTC (permalink / raw)
To: Tom Lane <[email protected]>; +Cc: [email protected]
On Tue, Jun 10, 2025 at 5:46 PM Dominique Devienne <[email protected]> wrote:
> On Tue, Jun 10, 2025 at 4:27 PM Tom Lane <[email protected]> wrote:
> > PQfinish is asynchronous: [...]
> > [...] pg_stat_activity() takes a snapshot of the view's contents
> Many thanks Tom. Didn't know either of those facts.
Hi. I ended up polling pg_stat_activity to wait for the disappearance
of the backend, to avoid the race in my unit tests. Initially
pg_stat_clear_snapshot() seemed to help, but then it didn't. Not
ideal, but I don't see any solution (work-around, really). There's a
new LOGIN event trigger, but no symmetrical FINISH one, and those are
restricted to privileged users, so useless in my case. Oh well. --DD
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2025-06-11 09:26 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-06-10 14:27 Re: is pg_stat_activity "transactional"? How fast does it update? Tom Lane <[email protected]>
2025-06-10 15:46 ` Dominique Devienne <[email protected]>
2025-06-10 16:03 ` Adrian Klaver <[email protected]>
2025-06-11 09:26 ` Dominique Devienne <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox