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 1vTje2-009WSO-0C for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Dec 2025 16:35:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vTje0-004QjQ-31 for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Dec 2025 16:35:57 +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 1vTje0-004QjI-27 for pgsql-hackers@lists.postgresql.org; Thu, 11 Dec 2025 16:35:57 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vTjdz-000AeU-2Z for pgsql-hackers@lists.postgresql.org; Thu, 11 Dec 2025 16:35:56 +0000 Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-b735487129fso55698666b.0 for ; Thu, 11 Dec 2025 08:35:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765470953; x=1766075753; 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=YjLqO++YJDknpELj4scIe09+nZCEqgqwiTEyggB+mSI=; b=VyWsLc680ezKWfE5xa9wF8Gxpvu+yy3CYZuGwXegoiDJF2cEdG4awKDW+qABLm7dHU /NPvCRUO5cka5lMaGYHbjmPylIPNQEvxfX2WoODJ2iZPJtLrzHoiMKMCXqTwJzwDzsBS PhtSHxEV061JsaYjkSDep9EBwjLkL3yJ6zrHo66HJxUmtzKNCUhuRBj5lYClNJ0hFPb/ Q3a09VVwrgBhXCt3EuyZRXqk5EN7IzagqQf3q0ab3T89DlicEtcimcPS0BNGVYjTJlzj VbD1VWWdtSbjAgdDSbVH/ZLhGDEiQeAXBd2GgD6OBIhOUhEBIu6wYSdRyR+UpuDpVRU3 xLHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765470953; x=1766075753; 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=YjLqO++YJDknpELj4scIe09+nZCEqgqwiTEyggB+mSI=; b=ruTIvciN/mBJQR46PD3bqx9h6kVRCFwXYD3pZWFig/KcTdYdc3VWApswLvHEoMp667 OvFbKS70jfvSYqMLj5qz9AD8Dj2ynTPflEj8xTxjJ7Eik2ELsnlMtaLl+NvMQZFB9ke6 9A5Umin5pMwoI1Hf2wTMCUPXQ3Lwp6gqHuZ8J3ovNR5fsu5ZB4WZsMEHLIiv7vXVnN/G VAkne9g9dTg8C4LpddEDrWz59e1u8bXSiBkV0iT+OmRj4nHVSQZNWCbdY2PsIC81XXt8 P0QJlvCzhfAEPnfVdMQQvoExkhIayCNKpoTs8UmSD4mMhRvz9YefQAqn1Jfy3llk3oMe wHFQ== X-Gm-Message-State: AOJu0YwTtrenuq9UG+UJ6z1FTojYnRsuj6x78yqctZYdXoPpLfUhkO8M 3t1rRSYobXdEfAY47abw6QUb/1zFgU8Kpe5RPSPYg+IGBhm932hy/NuNLJ0OFrYuRwQcI3A/4Yx vzjgXmeZ/bZtJ39hpQyWi9xuHqrPjcAKsaf3A X-Gm-Gg: AY/fxX73TyPy1F3KYVFYOL1vDFmq/Zgo7p9yQac4L57baRk780sUC7/t6c0O5Cj/t2b ZZLD1Nrz/uDwsNA8Q9OdGPf3HnkDbWp0Su4AWYY5e6h0RJGPbUSO2FDDLXH47Y3QLixM7Cxn6Q7 S1FGj8tB0X1kSD66rFo/jVPiD4sDTBkpKVL7tELpOITvo7cH7RewZELVCGgTY/nbkCUTxKQdwbS PICRJKobQhwI1PXSzHHsNjxnNAgiDUGg6E30iSf1OsicBvYr0a8adOnwn4Prt2uDA== X-Google-Smtp-Source: AGHT+IHa3UBTi2G7T0TKPHt7ZCQWBU3PQeJ9N3yerPlCOkY1LzsQyIgTHv0f91KCJxJ/iTJ08klkLf88bctkZR87Z+A= X-Received: by 2002:a17:906:c143:b0:b73:544d:b963 with SMTP id a640c23a62f3a-b7ce823a74emr740621666b.13.1765470952819; Thu, 11 Dec 2025 08:35:52 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Thu, 11 Dec 2025 10:35:41 -0600 X-Gm-Features: AQt7F2olsqt_UrSiqNG4SnaIilcjzx_WeJbgtol9v_WZV53AudzmrZbAQzHXzKc Message-ID: Subject: Re: Re[2]: [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 > >Can pg_stat_statements.stats_since help here? > > > >for example "where stats_since > last_poll_timestamp" ? > > Actually no, monitoring tools fetch snapshots to find the difference > between snapshots. > Data for every statement is changes after each execution. > > But stats_since is inserted only once when the new statement execution > appears and is never updated during next executions. I was thinking of using stats_since to avoid fetching query text, since that does not change. But you are talking about avoiding all the stats if they have not changed. I see that now. FWIW, this was discussed back in 2017 [0], and at that time there was some support for last_executed, but the patch did not go anywhere. After looking at the patch, I have a few comments: 1/ There are whitespace errors when applying. 2/ Calling GetCurrentTimestamp while holding a spinlock is not a good idea and should be avoided. This was also a point raised in [0]. Even when we move pg_stat_statements to cumulative stats and not at the mercy of the spinlock for updating entries, i would still hesitate to add an additional GetCurrentTimestamp() for every call. I wonder if we can use GetCurrentStatementStartTimestamp() instead? ``` /* * GetCurrentStatementStartTimestamp */ TimestampTz GetCurrentStatementStartTimestamp(void) { return stmtStartTimestamp; } ``` stmtStartTimestamp is the time the query started, which seems OK for the use-case you are mentioning. But also, stmtStartTimestamp gets set at the top-level so nested entries (toplevel = false ) will just inherit the timestamp of the top-level entry. IMO, this is the most important point in the patch for now. 3/ last_executed, or maybe (last_toplevel_start) if we go with #2 should not be added under pgssEntry->Counters, but rather directory under pgssEntry. @@ -213,6 +214,7 @@ typedef struct Counters * launched */ int64 generic_plan_calls; /* number of calls using a generic plan */ int64 custom_plan_calls; /* number of calls using a custom plan */ + TimestampTz last_executed; /* timestamp of last statement execution */ } Counters; 4/ instead of a " last_executed" maybe the tests should be added to entry_timestamp.sql? [0] https://www.postgresql.org/message-id/flat/CA%2BTgmoZgZMeuN8t9pawSt6M%3DmvxKiAZ4CvPofBWwwVWeZwHe4w%40mail.gmail.com#beeebe3ca4a3dcda4ed625f7c15bb2d8 -- Sami Imseih Amazon Web Services (AWS)