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 1uOyQw-00EhLD-4G for pgsql-general@arkaria.postgresql.org; Tue, 10 Jun 2025 12:50:30 +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 1uOyQr-004HL3-MK for pgsql-general@arkaria.postgresql.org; Tue, 10 Jun 2025 12:50:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uOyQr-004HKu-8u for pgsql-general@lists.postgresql.org; Tue, 10 Jun 2025 12:50:25 +0000 Received: from mail-oa1-x2f.google.com ([2001:4860:4864:20::2f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uOyQp-001FYw-2g for pgsql-general@lists.postgresql.org; Tue, 10 Jun 2025 12:50:24 +0000 Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-2e8f84653c3so1243625fac.0 for ; Tue, 10 Jun 2025 05:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749559823; x=1750164623; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=OzaCYMGqZm9izPBjkoSS4VjXzlHK3b37fw1VJ53FqmI=; b=Knxn9+f24QzuXGokUm6fH/MUuNGCuT/3G0qQiDuCL9Zh6KNrONMKcWLAjg9aure/JC L0rlhTGSQtXR4vIjiNtQsdyJanr3W5sgBwICoHdzEFYZrJBnMIXVgFefuiE064pVoLTK bwZqgFG24B8kKNGPOhjTPcKak+8eJg7C6qizVr/vxHEnUTphdjurHfzzejLc7sMwHXiK SA7vijxZhOSOFicJwcb77h8k9VW3WfjJ4F2j34GHBGPWLYGKmMwTEqokmM3ysQQsNXsL AlKJPMeMrgb7RPOXMLek3RKncK5ssYU6MfPCyWUpZmp0ZD4CuNFWa7G7IIgh59XPp4RL c0kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749559823; x=1750164623; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=OzaCYMGqZm9izPBjkoSS4VjXzlHK3b37fw1VJ53FqmI=; b=DNrPUEnTmKvv7U0xc8twkFrCltJBAVaY0rvF5by/xjFPmDPqX3faMRxT6rK9TI5zmB X4gAPiYqA8qMVYb2DW84F2gsv6qKUMdO/yXCeZrJxlawpxHKgViEq3+c2ZxkUbl8rYp1 EekWGQAYuqDfutYNCgbrvuNbJaSn4DNOzRT/ERY45ZCuFC7r5IeMVVe6Fe028XYxN7I0 T0e9vOd25VMOSYaO34u5VKFTD0RDCEsqQRdFL/uwZ3IzLHVy0DX/D9Mbu1KZ7zJzlecb xqcAW2ZiF04abp5I0HNJ5hD3zCyvMMXXK65qvBAHNhVGBgeq4721T9xBhmLBtQk+5ddq LvnA== X-Gm-Message-State: AOJu0YwTEXBxij06Di/8JpY++AK8+Nz3mnzOl4rd0GY9ShKefyNAm7WV Eh5p9WY0kdJxzfEjOPgBuN9NrRSrjT5nYbqLNw6K/dX2QxCbUcQXk5xyc7Ks4XKnM0rnFFHlB+R 57SeZmaWBIdrTLxiNGTZMLzsBusqXHRRIwRKl X-Gm-Gg: ASbGncu0UpKIYTFesE217V92G7Y6ripONgawxGBtHAfgBLquJ8Q+Iq9Mf62MIiYimQC dzKC5yJI6GWAbJXT9hfT4q8GAcKyy8mgK13q3OogygL978P39FOqlYvhunIhwF3hmRNS+TK565d qWJ9/XaWYWMbEM63yKQjRT4g2GWv1RTYZ3tAtQRDa31NJIow== X-Google-Smtp-Source: AGHT+IECYn7YSNTcwg+7MB8g7wiAKpjLwRswz88vYG4Cr18a9Wlrt+XyX9SlAAGXjMTysVMlvwHWzlotZ1wLkQ9qQ9w= X-Received: by 2002:a05:6871:289:b0:2c2:30e9:b15f with SMTP id 586e51a60fabf-2ea00c2d489mr9534674fac.20.1749559823119; Tue, 10 Jun 2025 05:50:23 -0700 (PDT) MIME-Version: 1.0 From: Dominique Devienne Date: Tue, 10 Jun 2025 14:50:11 +0200 X-Gm-Features: AX0GCFttTvCSeN-EoCg4LXjbni_ln0WG7b77XmZqX-n3Yc4OSizziPGSV-9xfHc Message-ID: Subject: is pg_stat_activity "transactional"? How fast does it update? To: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi. We're getting unit-test failures that look like data-races, which somehow are getting very frequent recently, tripping our CI. Basically we have a `services` table, for service discovery, which records the backend PID of its main DB Connection. Such that we can account for "stale" / "zombie" services, which are still registered (e.g. crashed), but who's connection is gone, when querying that `services` table, like so: ```sql select s.name, s.backend_pid, ... from services s join pg_stat_activity a on a.pid = s.backend_pid where a.datname = current_database() ``` The unit-test code is "linear", i.e. single-threaded, with pseudo-code: 1) Open Connection outer 2) Open Connection inner 3) register service using inner (i.e. add row in `services` with backend PID of inner) 4) Close Connection inner (calls PQfinish) 5) Run query above on outer. Sometimes still sees that "stale" row, despite closing inner. There's is no question about the ordering above. i.e. PQfinish(inner) returned before the query is run. there's of course the possibility of another (3rd) connection reusing the same backend PID, but that's remote a chance I believe. And I don't know of any other reliable ID for a connection, than its backend PID. So when and how fast does pg_stat_activity update, in the face of a PQfinish? What other scheme to detect "table rows" associated to "stale" connections? Thanks for any insights, --DD