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 1vmEb2-009BUL-2O for pgsql-hackers@arkaria.postgresql.org; Sat, 31 Jan 2026 17:17:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vmEa1-008oEG-2V for pgsql-hackers@arkaria.postgresql.org; Sat, 31 Jan 2026 17:16:18 +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.96) (envelope-from ) id 1vmEa1-008oE7-1Q for pgsql-hackers@lists.postgresql.org; Sat, 31 Jan 2026 17:16:18 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vmEa0-00000000OLB-0WQU for pgsql-hackers@lists.postgresql.org; Sat, 31 Jan 2026 17:16:18 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-b86ed375d37so435961966b.3 for ; Sat, 31 Jan 2026 09:16:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769879775; cv=none; d=google.com; s=arc-20240605; b=fF/J4Nph3inqmSzgM7i7jY4hiDdy2Eyh24XaoM15RQS39aWFxtxDZWmusPKdkJrsvo b94bHBwg0w+GE0GcM9C8twFy/XnkakZ8IALxS1knMLSfQKxbs9HqsdgXw3Yp/+Q3dOFj 5TjHJvIyxexzZGIux9c4NeghM86Ua3ShrqiuZfHEvABMEsh71M7nzxQNGpCAACkkZt8C adiEx9cOKxu9dIHOtVIZjJyAdwIUiTsNACrGYites1D+CBbSHxRsWR7KSnUgYCaWBXWV H0SmW4oMH4Gw3KvMuQ/MYcME9YzKVzwd5L7h5cCIHRJBxuPH+Lhlrc5NhnN60xely0C/ xEKg== 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=y+NaEC8fn84LmlYkaL8cKRw8I0H0mGYeDLRHnV75//M=; fh=C7dG5/8IEcSGUrdipr6XsT6q4R572yEo6SYTVMc+rWA=; b=Y3nIwsT2QMnX4IUit7mm63TxMx16g8PRayhVYPCg0o7aLCV3bz/hpRddTd9gpUX+WR eXBA9SpeX7SjYZnAtmokTpGMgou7PLnm5vnsHnmlQB9fXMG8pAxhUHSdVU64joG+4tDy w65nVdjfWs8MfGS70dImxThL4wmRBTSq3UjK890Mt+Opp+oNi5cmbmdhNWcaVtQC2oAI p0DM0xBbXkI5gUBi4VI7qIWYALby+FEmSKowQKSqrTP1dClMofJrnqOSvoIQge0w3+S/ 1vfx0bKnrRmbplGivx7/48DBrkRLgHyUekNxeNJHrOPESmLwtQHsjMZBqaVbdBh0B6bb GPkw==; 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=1769879775; x=1770484575; 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=y+NaEC8fn84LmlYkaL8cKRw8I0H0mGYeDLRHnV75//M=; b=LaoF9RjJmYvPy2KNc67gD7k0lZRMK72kGrAITBS/SaeZTex8c6bABxNJwCgL6sYUpQ g1/6R8fewFSs2+mRiJfTdUSVsGdZos9ior4jTwN1T1NUcCPBwyC5jkadezEfVAipjcLA ITBdhuiD5wsu22szPlhx9j0qEOxOtjjm2lROjmzWx6AOUYEvTckUs9MVKczOJI4EdWOq oFjLMayo3S4EI0pbq0wdYeiMGx5E47qYewA7167AI4PN+5xQzBrWe1J3PbIOTNrK+5YL tDcPBa8IzJoA2MQ0eCLF+oBNy7VDBOkO6zjeRGzibV048XWZptG1rJMcCv0elSOVhL5m Sq8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769879775; x=1770484575; 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=y+NaEC8fn84LmlYkaL8cKRw8I0H0mGYeDLRHnV75//M=; b=WGAENRfIho4sxXtBaEjaIA0WIghF6vXtBHceFiyt2Ruf51Qei75ItQHv0KclzJaeYC fOz7k9kcbQw3tlV732Nlls+x9t5eqRzDGCLIO+kOcXllQIRds0aQQmmFkL83ne+KdG/R gBjn/uoaleFdzg5N/jidM9It5yzNapducgIPR8Ro/6EiPNWKS0G0wwrkV79MbL8YS3oV ndOBg0PI3TbW1+Kf9bIKX4FaNxPDytz56nddZXg/XaVSjZxDuwMSlifAD1+x3JB96YnG kuBL30knFrAjyEVISqU8IOP9fR/n5d74eTqO+GOGxh7dAfZS+vOUFn0zZNc7QhQ9Hhxc 4psA== X-Forwarded-Encrypted: i=1; AJvYcCWYe38n83ylpgPxGx5raGUvPj3z7MdYpI+rfnYsGLdvY1oTMKMgWlv7JSuRcQlaouzA6C6HV/A9KkF9MULD@lists.postgresql.org X-Gm-Message-State: AOJu0Yy6mDIRiP2rf9njGCrPdj2RaILy2k+uuyGkrSDNQzlmePoQUGJG LVtLzZGQAgha+V5KcAmabo4pT00BBt64Bgm3oyBHhr52HdtSwdKAT77mMCWNqTPb79XwAaa45ad BbaoJ4KrRRad1DVAhc1AQJ9/tsns2hZE= X-Gm-Gg: AZuq6aK436BrS3U2RikrSMvuq2SCLLmFUkYmCM3UZdh2nLT2haEsxlSoyRgu5Gk4W3R N65jSFW5UDP6ADJQ4qSMw82lsiVUpSakvxy7SgYpSOZD5jZ866kdjhlnwZcmcsbTOmkNSQ7wGXP e0t5BwcplQ1cBthzo7P7YwvF/27NnHoWVLQ/ito4H7JHOC4Djs0VNw9S0g5HsvUM0Oz2EJrWYrF p1QOr5Mqt3uWMNLkAoDT1ag05PkW3UGIxfKolziMIULpVtfx6tAzq/aF9hUMBQE7Pq6X+w= X-Received: by 2002:a17:907:9617:b0:b87:322d:a8bd with SMTP id a640c23a62f3a-b8dff5b0367mr398166866b.24.1769879774453; Sat, 31 Jan 2026 09:16:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Sat, 31 Jan 2026 11:16:03 -0600 X-Gm-Features: AZwV_QixP0Ah2aPM7fuVE3W9ZUj8wwMjHtPEpdT6V3d6Vy0FjvefmlCeur8QfD8 Message-ID: Subject: Re: Flush some statistics within running transactions To: Bertrand Drouvot Cc: Michael Paquier , Fujii Masao , pgsql-hackers@lists.postgresql.org, Zsolt Parragi Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > This makes things confusing because instead of just relying on > dlist_is_empty(&pgStatPending) to check if we need to flush anything, > the responsibility now moves to the callback, which now also has to > account for all the ANYTIME fields. > > The way I can think about making this better is to somehow track if > we have ANYTIME data that needs to be flushed a different way > ( maybe a second pgStatPending list for anytime stats ?? ) Thinking about this a bit more, it seems the FLUSH_MIXED may not be needed. Instead can we just introduce a new global variable called pgstat_report_variable_anytime which acts like pgstat_report_fixed, except it's set to true whenever we update anytime variable-numbered stats ``` #define pgstat_count_heap_scan(rel) \ do { \ pgstat_report_variable_anytime = true; \ if (pgstat_should_count_relation(rel)) \ (rel)->pgstat_info->counts.numscans++; \ } while (0) ```` and reset whenever we call pgstat_report_anytime_stat, like this: ``` void pgstat_report_anytime_stat(bool force) { ..... ......... if (!pgstat_report_variable_anytime && !pgstat_report_fixed) return; /* Flush stats outside of transaction boundary */ if (pgstat_report_variable_anytime) pgstat_flush_pending_entries(nowait, true); .... ........ pgstat_report_variable_anytime = false; } ``` and then inside pgstat_flush_pending_entries, we just look for kind_info->flush_mode == FLUSH_ANYTIME to flush. ``` /* flush the stats (with the appropriate callback), if possible */ if (anytime_only && kind_info->flush_mode == FLUSH_ANYTIME && kind_info->flush_anytime_cb != NULL) { /* Partial flush of non-transactional fields only */ did_flush = kind_info->flush_anytime_cb(entry_ref, nowait); is_partial_flush = true; } ```` With this approach, we will not enter pgstat_flush_pending_entries inside pgstat_report_anytime_stat unless we have anytime variable stats to report. Also, the anytime flush callback does not need to check if there are any variable-numbered stats to flush. This will not be needed as it is in v4-0004 ``` + /* + * Check if there are any non-transactional stats to flush. Avoid + * unnecessarily locking the entry if nothing accumulated. + */ + if (!(lstats->counts.numscans > 0 || + lstats->counts.tuples_returned > 0 || + lstats->counts.tuples_fetched > 0 || + lstats->counts.blocks_fetched > 0 || + lstats->counts.blocks_hit > 0)) + return true; ``` This feels like an easier approach to reason about and we don't need to add a third flush mode. Thoughts? -- Sami Imseih Amazon Web Services (AWS)