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.96) (envelope-from ) id 1vTO1M-000R8u-2c for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Dec 2025 17:30:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vTO1L-0005KH-23 for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Dec 2025 17:30:36 +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.96) (envelope-from ) id 1vTO1L-0005K8-13 for pgsql-hackers@lists.postgresql.org; Wed, 10 Dec 2025 17:30:36 +0000 Received: from mail-oo1-xc2c.google.com ([2607:f8b0:4864:20::c2c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vTO1K-0000V5-1E for pgsql-hackers@lists.postgresql.org; Wed, 10 Dec 2025 17:30:35 +0000 Received: by mail-oo1-xc2c.google.com with SMTP id 006d021491bc7-656b32a0cc4so22184eaf.1 for ; Wed, 10 Dec 2025 09:30:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765387833; x=1765992633; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=6jz3nnu/v122XkzwI2Rtg3+lQxvPmxRp0eo0i14ww8Q=; b=RN08mZkU/OwUDmoT/9Qwi562PWlX6XLhOpBFwCYyIzDi8HFps0lQ2XklibUoT6islA Z53tuAiy2V+kUeYdMBnvWB2cYuHfeulP0fLpEMXyH/8JfJlmErMnr1rlpbOOUBadSTtB 573clMTyMJH0apWsyFTaaYrUvFqeZl+saoIv8XRPSRwfMxgfAiloB813Cia6xo15cH4F 0U/nIdh4ix250ztomAlLZDUjJ+khVnU/PGV0nL9KPTgE5hI3HT+c3GStvZ0sBpIgF76f C1s+ffdi07kpU9JPi/tFg/SClElwZoWuYLcEGImJUNvlkhZlpDIZHUAVy2SqGVzXjQJ4 y5oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765387833; x=1765992633; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=6jz3nnu/v122XkzwI2Rtg3+lQxvPmxRp0eo0i14ww8Q=; b=hMy9XtOdlsAGsIxKeaUBrE1uPROtrh8VwGGNEvPGS9pK4RzPKBF1YDfkKPYsS9dRv2 bV2glPeHF3YM9Knr+K/L2kRRrNqjxIRrYRWBBInWJ1le4DP1Slrqb18k4lZShuuOQats vuVhCWC5QYYXIV3hugKM3ygWAAt/Cx6nNM9CRUml+mnhozeOONL7N1F/UOk9XvdSo8Cf Hhb5AiXH9GM8DERMIVvdnSWbqQh2/naVt8pQthCpua0FkYrKNHVggAK5n+JcYz4ysvjS cokWdupfkIo0Xk/n5os5fYo8wIF3CHoY0Rns9twMJ02aAni70S46ZkP59cWWvDrH153A 0Xug== X-Gm-Message-State: AOJu0YwS9nhdwgH2axpqb8zKOJFIB8MzzU41kwS9xLuK6XiPBw3U656Z fwbr2+YGtFYJnGWRzo4KdflkBRSZjoXn11YCJ8HR8snuG1oGCIWfe9yBSsNDjaCVdtQRywib2tO djlpUiFeo2iIOrBP/4b94LOhtobl17VbL0JRs X-Gm-Gg: ASbGncttmHWlNmUJ1lnipW+LD/UpoMSrK/2Kx8Td5rGws6RK5pj8KInh2DK/fPD1CjG jbMVAOmNDGtZUqgK2ifK0e5U5p8hyA+CgqLckCwgUO47EB8tkHw7IsFNHnZmfbrqiKiHlWxgmOR rUFSiZdKBA97oYBVAgP83SVo8AiaVWfTfVrKWAZT03AhzcIqQbZNiF45/S6vShnPX3m+RQDG4kq vPgvxhzMwPN46YqjOELbQrqBraCHEd+w6DMaTrJWRT0jwugWnFUSqscH07jGOsScdjz8DuyaEQX NJRBTw== X-Google-Smtp-Source: AGHT+IHrp6APN6wtllF+2AVKytEp+zb5psWZZ9GPsZT6/6VtMPYpBrVnQtXfyhgBk5f48n0uBlsmt+GiWM5Zu2XxP7Y= X-Received: by 2002:a05:6820:1523:b0:659:9a49:90ba with SMTP id 006d021491bc7-65b2ad104f5mr1657719eaf.57.1765387833033; Wed, 10 Dec 2025 09:30:33 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Wed, 10 Dec 2025 11:30:19 -0600 X-Gm-Features: AQt7F2odWE4ljGX8IW3WnZ04ZJj-YcYB5Zr1lubd05-7Ka9jdXdzbZ1JZHaMjUo Message-ID: Subject: Re: [PATCH] Add last_executed timestamp to pg_stat_statements To: Pavlo Golub Cc: pgsql-hackers@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, Thanks for raising this. I did not look at the patch, but I have some high level comments. > I would like to propose adding a last_executed timestamptz column to > pg_stat_statements. This column records when each tracked statement > was most recently executed. I do think there is value in adding a last_executed timestamp. I actually think last_executed should be the time the query started timestamp, so we should actually create an entry at ExecutorStart, along with calls_started and calls_completed. This is great for tracking cancelled queries. The issue is the extra overhead of tracking the query on EcecutorStart, but that should be less of an issue once we move pg_stat_statements to the cumulative statistics system, which will be possible once we get some prerequisite work to make this happen [0]. Another concern is the width of the current view. I think before we add any new attribute, pg_stat_statements fields should be split. This was discussed in [1]. > The motivation comes from real world experience with monitoring tools > like pgwatch that poll pg_stat_statements regularly. Currently, these > tools must fetch and store statistics for all statements, even those > that haven't executed recently. This creates significant storage > overhead. For a database with around 3400 statements polled every 3 > minutes, storing full query text requires roughly 2.5 MB per snapshot. > Over two weeks, this accumulates to about 17 GB. Even without query > text, storage reaches 10 GB. > > With a last_executed timestamptz, monitoring tools can simply filter > statements by "last_executed > NOW() - polling_interval" to fetch only > statements that have been executed since the last poll. This > eliminates the need for complex workarounds that some tools currently > use to identify changed statements > (https://github.com/cybertec-postgresql/pgwatch/blob/759df3a149cbbe973165547186068aa7b5332f9d/internal/metrics/metrics.yaml#L2605-L2766). Can pg_stat_statements.stats_since help here? for example "where stats_since > last_poll_timestamp" ? The client does have to track the last_poll_timestamp in that case. [0] https://www.postgresql.org/message-id/flat/CAA5RZ0s9SDOu+Z6veoJCHWk+kDeTktAtC-KY9fQ9Z6BJdDUirQ@mail.gmail.com [1] https://www.postgresql.org/message-id/03f82e6f-66a3-4c4d-935c-ea4d93871dc1%40gmail.com -- Sami Imseih Amazon Web Services (AWS)