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 1voO3J-002bwC-0U for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Feb 2026 15:47:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1voO3I-004jZh-0t for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Feb 2026 15:47:24 +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 1voO3H-004jZZ-3A for pgsql-hackers@lists.postgresql.org; Fri, 06 Feb 2026 15:47:23 +0000 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1voO3F-00000000rXD-3si5 for pgsql-hackers@lists.postgresql.org; Fri, 06 Feb 2026 15:47:22 +0000 Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-65808bb859cso3311553a12.2 for ; Fri, 06 Feb 2026 07:47:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1770392839; cv=none; d=google.com; s=arc-20240605; b=GYbriDrIeILljXbjApnssU+5KSL1KzYRgEwWGAjH0x5QxfChKFumTIp7/Q6K6aoNfH Dy/cd8AVxhzB7VfMj2tnn0wTiQaMlpLXswLd6eTe+13jCo3lmFyNA67gQ83C6rO+dSc/ KNpoi9cfl+3AS90MRjgfJZRGDjxMgzUiteqbEDOZE6IsnIFsPdp3Rla10hxxkeSkO3NV 0nqrUY0oocUnKUX48sqsVVvXZnbpj+8wdDPAyAXyPdVScROE3FbWpBGTveJaXnLMkDbJ KCZPSvvp74sIbXny6tOZi2RS9VEgRPOA8i14/r//1Dh2CxYoabU+vr1NkISNukhSkatW UZGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=YfDLAGu/l64L3Gqe1wHf2L/8TyHvmFfdXK8PKoGwDco=; fh=D+ZcRE9jfMLpF5Lapaj88s0iFxh204CmGiuxlWAi0FU=; b=MnZZLMq5u2QBpSAFBwJQBlZR1BV4uA4Kmp2faVRhmvJO49+KILzfw9u/gxDMy7igUL tongwRpSOqmu0EyQjN7SSfU7ZWVu+fvi50YOb+x66YdFvbxnNTqUJzfYm+RAfDZkxxXa pbCa4Dyh1HTZXImknvQtyBlPymGMP/7kBBUojYP4Am/XNak2e38GxC2JE3VaOxkEVN7Q bh00Nh0CZZlkM8R0OE0SRGLXfad2Kj1N0m94IABTNtUNsGz3J/P2WDTTNJPoQQGwVAAL aIzebuINo72b9ssTz9E+JOsYDajM4BB0x+edO2P9Tu33AvkGvgQBjcpMOLGENotJhiCI 9YSQ==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770392839; x=1770997639; 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=YfDLAGu/l64L3Gqe1wHf2L/8TyHvmFfdXK8PKoGwDco=; b=YqtjQXq1XO3T6t6arLZ+g2nHbhWh/9anYIKoHxaF3tckzdJX0oopBlRfjjwwxAsyOx QPTpw8nlXrUPXvO+O6DCNrr6Uc5UBp+G8wkLcVAbuMhTHpyZYSwy/aLu+lmE6PEvD3dK wg56t7dQjHqxKzSPSbU1zorqs7fBAtk2zq+TyHTWzjlIT+4ENUNwIiaSZNQb2ahlfydR OADldP9czEOHE+4cMQ87HO8r+IElEkMJNd172SP4KXNtss43Rr6LLXavcZGK/xoxr3Ud e1KQteS1HQXvjjCEMlr9D7r29WCTTcKE/iFh+5bShEdSNYGirzPkXW1z08We47DANys3 JB9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770392839; x=1770997639; 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=YfDLAGu/l64L3Gqe1wHf2L/8TyHvmFfdXK8PKoGwDco=; b=hngsphV754FhsX22IihqeLBUhUL/zEO4XFjMGJN9GJ14msFY2duq2EI8abL4Zo5R7V 05ztwDakXU4FlxkX/UOTCe2f9xwZ0dxkWaigTWwEqEaHzVvTaXnyc3XZGutgSQ4/QS39 pZQP6GD8Qk/lsGboscZtvqpNFxTRltQq73FblV1SOLN3WLer6wzeaGMlxFfYEIf4RnTn G/gHBBETsuUJ9fE9ILRNZQia2yQDb6vVR2yjipWOmxAStPshQKmlqY5jDKYwls3t5bXR NmCYhi8hvzDG3R6N1IUIsT72Bcq6W/Q9CY7QT6TlIi24OWMVXCpVLhgOJBFCjqA9tkC2 BakA== X-Forwarded-Encrypted: i=1; AJvYcCXa2dlnfFG/NGnTgD68cTMp617rua6KJawIpuQ/DADmV31+WBQWjHHrYtv8MJDxViaf2+FKQDCSU1goqwJz@lists.postgresql.org X-Gm-Message-State: AOJu0YwJ3n8y26g0WU684aNus1/7F9ktdyFcXbp8yumWIv0CMMHFea5B Hddt+F0nAOB+Xu95yxn+BMA8oNgkIsBO4rgwiLnpSmaXYy3al3G76L7kEcoFP6qRyyVMR4z8FEc 4cq2eo0iaW0O0rHxi2Hau9yAwQvsMybI= X-Gm-Gg: AZuq6aJlrgB5NpRGdiYztPS6uWSfgJYE4/YMzC2XTkNsU2ju8zyduoOt85GsGVFMVQC 4wRwNVw88xFWYand0saS4mR2yI0KsO8X0CDj0+b+/7BpHfrgCQsLD/duqJ4pqeyh7V8wxrW9ohy bqUr0qPxo3ouvAGuA0c+zgysjvQ7IJ5LkoBMIMEWlM7uq9VNsrwmFaA5kAWeZ/TM75dg0GnQ/Th 61vKVM1CLATX7yoU4thxJz7droWkiyN7sAtxrUBL9r2F+2kec7PLfC+62ZbN6zrr9mlEpduMJ7x 9Q== X-Received: by 2002:a05:6402:1d4d:b0:659:3ed2:13ec with SMTP id 4fb4d7f45d1cf-6598411d6a9mr1507166a12.2.1770392838924; Fri, 06 Feb 2026 07:47:18 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Fri, 6 Feb 2026 09:47:07 -0600 X-Gm-Features: AZwV_Qjxaz_CXlFlLHKL6t4aBQ4bYmlIbzK0K_XMoKfI_bLr9F1RRstgAu99eVM Message-ID: Subject: Re: [PATCH] Add last_executed timestamp to pg_stat_statements To: Christoph Berg Cc: Pavlo Golub , Bertrand Drouvot , "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 > > WHERE last_execution_start + max_exec_time * INTERVAL '1 ms' > NOW() - > > polling_interval > > Is this extra complexity worth one saved GetCurrentTimestamp()? > > src/backend/access/transam/xact.c is calling GetCurrentTimestamp a > lot already, so I don't really buy the argument it should be avoided > at all cost in pg_stat_statements. Just storing the statement end time > would make this use case much nicer. We do call GetCurrentTimestamp() to set timestamps for xact_start, etc. But, we also take special care to avoid extra calls. ``` /* * set transaction_timestamp() (a/k/a now()). Normally, we want this to * be the same as the first command's statement_timestamp(), so don't do a * fresh GetCurrentTimestamp() call (which'd be expensive anyway). But * for transactions started inside procedures (i.e., nonatomic SPI * contexts), we do need to advance the timestamp. Also, in a parallel * worker, the timestamp should already have been provided by a call to * SetParallelStartTimestamps(). */ if (!IsParallelWorker()) { if (!SPI_inside_nonatomic_context()) xactStartTimestamp = stmtStartTimestamp; else xactStartTimestamp = GetCurrentTimestamp(); ``` > OK, here is one more try. I discovered the `total_time` argument to > the `pgss_store()` function! So we can calculate the finish time > without calling `GetCurrentTimestamp()`. > > This is version 3 of the patch adding a `stats_last_updated` column > (yes, again) to pg_stat_statements. Based on feedback, this version > improves the implementation with better performance and correctness. > > The main improvement uses `statement_start + execution_duration` with > `rint(total_time * 1000.0)` to convert milliseconds to microseconds > with proper rounding. The calculation performed BEFORE acquiring > spinlock and assigned within locked scope. Correct, this also crossed my mind. Although I would consider doing things a bit different. Instead of calculating the end time in a single column, I still think it's worth having a last_executed_start and a last_execution_time (duration), and the user can calculate this on the SQL layer. I think it's better because last_execution_start is already a known timestamp in pg_stat_activity.query_start and some tool that finds a long running query in pg_stat_activity, knowing the query_start they could then go look it up in pg_stat_statements. What I'm really getting at is separating these fields will open up more use cases, IMO. Of course, we are adding 2 new columns instead of just 1, but these values do have benefits. -- Sami Imseih Amazon Web Services (AWS)