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 1viVXU-008HQu-0l for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Jan 2026 10:34:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1viVXT-006XZt-15 for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Jan 2026 10:34:15 +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 1viVXS-006XZl-31 for pgsql-hackers@lists.postgresql.org; Wed, 21 Jan 2026 10:34:15 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1viVXQ-001hVs-2c for pgsql-hackers@lists.postgresql.org; Wed, 21 Jan 2026 10:34:15 +0000 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-430f2ee2f00so3405690f8f.3 for ; Wed, 21 Jan 2026 02:34:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768991652; x=1769596452; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=7yIB090qK/1P/wD+EOnpiJt9miSmi2ub4yXHhRIGHSo=; b=XUx61PxkbvfmWlCZQX2ZLyo9jG+vWVJPSyJx9FW/kEy/hnp78XF/OHJGSlFcLVnFtl IbTeYVw0HEntH77cFHsgjmZ1UPwAGZU2W2dmiMsYCJn3eOPfblr28O6PhBZwTcVPs1D4 vIQ5HFlPavdQmujpaGghhQ517Bkv8gdHrF61uuXXQWOXWL59d9A5HIPlFGAAI2N66ue3 8CTuCy233mP1zgHwLe/krE807vJ/8Oyy6hTAVhgo6k/wCyq4kxDQ5RNxClGy0Bzb/BzX xIud4784J/thYxOyQTME04V3LwlXq8xYWdkFvPgxiaFWoyAsjF1Hx7HaxSU2zLDIHMos 9r8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768991652; x=1769596452; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7yIB090qK/1P/wD+EOnpiJt9miSmi2ub4yXHhRIGHSo=; b=Xx3d3UyKPt4wK+U82n7O1W1ZrhPy8YhTzQcLv2teMv5kRG56CKdpLsJMnvlKb9YYgA tDbgLPQA7wduD/lzgYhpZFdokAiOu0WxtFB0rLQHxj8XphYWcAIRcpfWHOLEKVDw5pts YUFyKeW5Jxaik4UgjCiAgzTQxCFRnetPcNAHbqjavvtWF4YPg/QLqRhj9252axmGeshf HYpJKCXvz16M8P5mkvIq6jQQe+i3T2Y+e9FiHxOZVgwpKVIP3yXS+NjPK6ETKsZVZn8D T0+EXM0gYo9JvkYx2tM+DSTpqSyEvYKjdJ2yu+wPiN5oagzXeLfYn/FbzjSpJdvPQ3Ag niWA== X-Gm-Message-State: AOJu0YzSd93mHyx0Lvv4GcSMzG2CVqGt/ytrrOCxiJDPD4HvPA04ZHYS XFxRWXbogQih47Mn0dFJPPLmAqpxuNnItzfMOEmnYElMmtIvBxzYWOpT X-Gm-Gg: AZuq6aIALxqWZCmSRcdH1v7k3Sg0ZIdN9T2VZ/OSYsPwki9PP9+tU5BMrZy8hH0XAVm UAwkTWbJUcYem8iANdMtBXVHwWugJsZT4mI+AlwDf6y4WvV+FODmxfLHihO5TB3iaSzIQn18HPt OT40lA5i/1KwsYTp8lDus451j2Ni12Baclh+o5Vba4u2ho55tpeepI0le6NFEf05BL/5rKMDFbT 8b0zIuX1lVutZ/26JMnxuNW5yWl85q2ssNq9vhKCW/glF9RJcp1PWb8FBJxFOHzgIPxtD3EK4ru bTGs8o57G2bRqZXJM3xTZb+nzSlRLHszxDmSDkNx+Zkb6ZcZiJVzXhOretL1qe/2tyDupu8+/eI rbF+eOD8uFyHFH7acOsf0n9iUIdeaMvriR792q2HCT1+RmiFo1irxaSPhf3LsgETKrRGE+fW6sW nO4iQDAvbGTY1WE83jxKoXh31T/Buy0/5DXxChdc/N0oM+SiPiahabMYtX3sSmQdwD3EjGj+7Cf TRoajsrDmTPDUPEB048QvBTKyoP6cBv9/Ls7qDAGOyPcA== X-Received: by 2002:a05:6000:4383:b0:433:1d30:45f with SMTP id ffacd0b85a97d-4358ff3f098mr6951297f8f.1.1768991651368; Wed, 21 Jan 2026 02:34:11 -0800 (PST) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4359f4a7ac7sm2602778f8f.20.2026.01.21.02.34.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jan 2026 02:34:10 -0800 (PST) Date: Wed, 21 Jan 2026 10:34:09 +0000 From: Bertrand Drouvot To: Sami Imseih Cc: pgsql-hackers@lists.postgresql.org, Zsolt Parragi Subject: Re: Flush some statistics within running transactions Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="lCc1MWfC7i2uqs8g" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --lCc1MWfC7i2uqs8g Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On Tue, Jan 20, 2026 at 01:27:55PM -0600, Sami Imseih wrote: > I have some more comments: Thanks! > -- v2-0001 > > #1. > > +/* When to call pgstat_report_anytime_stat() again */ > +#define PGSTAT_ANYTIME_FLUSH_INTERVAL 1000 > + > > We should just use PGSTAT_MIN_INTERVAL. Okay, done. We can still switch to a dedicated one if we feel the need later on. > #2. > > instead of ".flush_behavior", maybe ".flush_mode"? "mode" in the name is better > for configuration fields. Sounds good. > #3. > > FLUSH_AT_TXN_BOUNDARY should be the first value in PgStat_FlushBehavior. > Otherwise kinds ( built-in or custom ) that do not specify a flush_behavior > will default to FLUSH_ANYTIME. I don't think this is what we want. > FLUSH_AT_TXN_BOUNDARY should be the default. Good point, agreed and done. > #4. Can we add a test here? Maybe generate some wal inside a long > running transaction and > make sure the stats are updated after > 1 second I'm not sure, that's also somehow the purpose of 0002 (with 039549d70f6 being reverted). 0001 and 0002 could be merged and pushed as one commit. That said I'm not opposed if you feel strongly about it. > -- v2-0003 > > #1. Should we maybe make this a bit longer? maybe 2 or 3 seconds? > May make the tests slightly longer, but maybe better for test stability. > > ``` > +step s1_sleep: SELECT pg_sleep(1.5); > +pg_sleep > +-------- > ``` Not sure, we could increase if we see the test failing. > #2. > + /* > + * 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) > + has_nontxn_stats = true; > + > + if (!has_nontxn_stats) > + return true; > Can we just do this without a has_nontxn_stats? Yeah. > #3. > + are updated while the transactions are in progress. This means > that we can see > + those statistics being updated without having to wait until the transaction > + finishes. > + > > The "This means ...... " line used several times does not add value, IMO. > "are updated while the transactions are in progress." is sufficient. Removed. > > #4. > + > + > + All the statistics are updated while the transactions are in > progress, except > + for xact_commit, > xact_rollback, > + tup_inserted, > tup_updated and > + tup_deleted that are updated only when > the transactions > + finish. > + > + > > Only these 5 fields from pgstat_relation_flush_anytime_cb, so only the below are > "All the statistics are updated while the transactions are in progress", right? > > numscans > tuples_returned > tuples_fetched > blocks_fetched > blocks_hit No, 0003 also changes the flush mode for the database KIND. All the fields that I mentioned are inherited from relations stats and are flushed only at transaction boundaries (so they don't appear in pg_stat_database until the transaction finishes). Does that make sense? (if the database kind is not switched to flush any time then none would appear while the transaction is in progress, even the ones inherited from relations stats). PFA v3, also taking care of Zsolt's comment (thanks!) done up-thread. Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --lCc1MWfC7i2uqs8g Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Add-pgstat_report_anytime_stat-for-periodic-stats.patch"