Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uOzxM-00FGKE-Oa for pgsql-general@arkaria.postgresql.org; Tue, 10 Jun 2025 14:28:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uOzxK-005GCF-R9 for pgsql-general@arkaria.postgresql.org; Tue, 10 Jun 2025 14:28:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uOzxK-005GC6-Gz for pgsql-general@lists.postgresql.org; Tue, 10 Jun 2025 14:28:03 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uOzxJ-001FUR-0W for pgsql-general@lists.postgresql.org; Tue, 10 Jun 2025 14:28:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 55AERvc7755976; Tue, 10 Jun 2025 10:27:57 -0400 From: Tom Lane To: Dominique Devienne cc: pgsql-general@lists.postgresql.org Subject: Re: is pg_stat_activity "transactional"? How fast does it update? In-reply-to: References: Comments: In-reply-to Dominique Devienne message dated "Tue, 10 Jun 2025 14:50:11 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <755974.1749565677.1@sss.pgh.pa.us> Date: Tue, 10 Jun 2025 10:27:57 -0400 Message-ID: <755975.1749565677@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dominique Devienne 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