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 1wBBUe-000osL-1y for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 13:01:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wBBUc-00Bta2-31 for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 13:01:51 +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 1wBBUc-00BtZu-1d for pgsql-hackers@lists.postgresql.org; Fri, 10 Apr 2026 13:01:51 +0000 Received: from mail-dy1-x1342.google.com ([2607:f8b0:4864:20::1342]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wBBUZ-00000000MAP-2EZL for pgsql-hackers@postgresql.org; Fri, 10 Apr 2026 13:01:49 +0000 Received: by mail-dy1-x1342.google.com with SMTP id 5a478bee46e88-2bdcf5970cdso1084919eec.0 for ; Fri, 10 Apr 2026 06:01:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775826105; cv=none; d=google.com; s=arc-20240605; b=JaSnCqJwPScHPxaaGCl2qY/WCU6f5GOHDG+dWpDRNEpjrdft6oaB/mPVN5E6t2s22n 9jL1RUJZgb30Xk3h0MemlPpUSKISQAUgOOyIrkQh2C0oJ+PMvgg78RxMJXrltejaixdR UHCOpDt60nd9CBdk2yyOpDr3TCmhHHQVVknRT1f+TPErO1BvuhPBv8j+kstv+DQQs5qS i/GJA5QvTwg6CjZONmG8V2wm3Z8Ha1WxhrCk12a1Hi9rjTIoCXk1GGYlfXEbh9R1Eh8b 60BT4L8JL6AdVgcHzqUY12IaWwtTB7xpS6cZzLvX1M3M0if7z5Z4kL+wd78ejiEfZikf 9f1Q== 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=mcs77HQHDnhQoaGO9Q1QhzWWVfAkJnzEgaTTcx9lp40=; fh=dRTCnXyBZAkfh9TvwjeAgA21SAhxbB5AqiQKeHrD7Go=; b=jlx4ZvMaLOL8nXnD94HOppYMdSMAaOx49HVhpjYMu30d+CYhlNPUX1qayWW/XxLBdn 0Rv4OcrD3LyDkqJMbD4LU4HTS67CYb0rYV3izE8PCIwGWWq0iqtlq4tkXF97z+JmlB+r RiqBSI38mhI1qcoctv+WhnNxNQZTRAKJTiwIxc9yuakENeODFCLa7T5gWIdm6UKjIqjd gK5XlGTNyxeCbCb2cHckyeSOVyIa34q2jnlkS9+F2tMe/o2AlO+Mwfi9UgMVB9T6xka8 QajkrFcqFMK0A0jxIcyAUqKalVwwR8IqKJDQVstTwVGLF97Nem6+bxzzEPhk6u/1pgiQ +PeA==; darn=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=20251104; t=1775826105; x=1776430905; darn=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=mcs77HQHDnhQoaGO9Q1QhzWWVfAkJnzEgaTTcx9lp40=; b=Kud0RypqTiuDBdF+p7KdkvHshX7lxUpzbTTjJE7KrCiVOE597hDzcRgMXpWxOyOqj7 H1xWfevDd1NLJ/yrFABJ6sUajXFuCs7gJ/922zeG/sXpyaK85S6XYGtDu8mOeuW5eoWB YQ59/6Ud9TIKq66ICQdy1ibZkLUe4yQfHyWk6p5/z421ebVbbKzPKhq6wx7Zv0hBf5Rv dj/LsikkRmOIGGpdKZfJL49oNbC/Yh0w2HILtirszbXqZV/v6qzCKyvvHQkUVSJC7LV1 6SeHdGJ9ocCX0xJqps0zHzkja4J7pzQeKLm9UU0JOMTETpwj3KNOcvpKOS0QlxpXONqH anaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775826105; x=1776430905; 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=mcs77HQHDnhQoaGO9Q1QhzWWVfAkJnzEgaTTcx9lp40=; b=m9rr8l9FbYiSfgA8LBWiL+NMg9MJtlAmrjxQWZeZyOWI4i+ZR7zjBRSHpKwt4apdPD 3iJyTBTkx+v1hL7h0nyqZHBaWidTJ8WeiIDytdbDDcf3htM3oUxID9J9pJIgPzS5jkqI xL82Wa6ZdT9OmdoSERQNcjFY6NnD6b+6+3AXxhWtSxMiyNYV59RvXV/8bRIGptGjVDyE fDDoNmB28XkdXXiLlbS11biaRVr5VjmLmy3PIZzNKU2LFNIgyAw79wMtq11BCK9gEIK2 4b81i35jkE7j0tjaEbU54vSU5R7Lkar08CKuTKHmuHaR7dCUzXd9r1zfiG4DFkREqIqf N6HQ== X-Forwarded-Encrypted: i=1; AJvYcCVhlDa39greosgfP8InsfdEVRW6y/gRQAsWte3DYhDiG8mrDJBUuH0mc4evSWofL4gcTPwf7k5z9+Q+5Xns@postgresql.org X-Gm-Message-State: AOJu0YyAb1dwLkOFSzqNuyd/rprbmHJ4gQboLVB+ezJHbUIPYYbWRL2T g47GD497jBKbiywCZst0fk48w1Aw3RI0yNm2esMymZtztRco2nquBiThek5pxku5ldpg08fu2Ok mM8dsfQ6R3lunzuN+rYapJQgiuMTsajU= X-Gm-Gg: AeBDietDLueN4fqSB9GxDn6YCdRvjSxmCBKyoNREHMP1ixtBhcgOTjbDMnHFJgKSfRA fGWv73CxR1bvK3F3/U0/ZhPxh6fef6+tcyGub39RU7IDjML/dMrP49CDm+cqzINd5+z378L0AXB 4Hzllndy6mivdyHbfzcL+7ThosTXP2iz+yJMCHiubZjBo+EOE1K9ENjlHL/tqF59OjPSCYo8G2d G8x2zoneDSNhT7wr1TUPlSvJRUU2+rrZ7rqVEQ+hd8brSOCUKYi2nSk+cTiSPO/l/k2O9PeprXr Xn7si3rf1roB4CX1H3u0K+fcQFJFgYtJK/ovOw== X-Received: by 2002:a05:7300:fb94:b0:2d1:a3ea:d8a5 with SMTP id 5a478bee46e88-2d59a18453bmr1455585eec.7.1775826104573; Fri, 10 Apr 2026 06:01:44 -0700 (PDT) MIME-Version: 1.0 References: <8CD3DF77-C4F0-4CA0-B329-2B62B5A85E3B@gmail.com> In-Reply-To: <8CD3DF77-C4F0-4CA0-B329-2B62B5A85E3B@gmail.com> From: Dapeng Wang Date: Fri, 10 Apr 2026 21:01:32 +0800 X-Gm-Features: AQROBzA-5nYTGaCmdynz_9ksclcxHIy7TPM5lE4X3bCRofo5TSOXknSz1dq3Q68 Message-ID: Subject: Re: Fix pgstat_database.c to honor passed database OIDs To: Chao Li Cc: Michael Paquier , PostgreSQL-development Content-Type: multipart/alternative; boundary="0000000000002565e4064f1abc7c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000002565e4064f1abc7c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Chao Li =E4=BA=8E2026=E5=B9=B44=E6=9C=8810=E6=97= =A5=E5=91=A8=E4=BA=94 16:45=E5=86=99=E9=81=93=EF=BC=9A > > > > On Apr 10, 2026, at 15:56, Michael Paquier wrote: > > > > On Fri, Apr 10, 2026 at 03:12:41PM +0900, Michael Paquier wrote: > >> If we decide to expand pgstat_reset() in other contexts in the > >> back-branches, we'd be silently trapped as well. > >> > >> The connect and disconnect calls are less critical, perhaps we could > >> remove the argument altogether, but I cannot get excited about that > >> either as some extensions may rely on these as currently designed. > >> > >> I cannot look at that today, will do so later.. > > > > - dbref =3D pgstat_get_entry_ref_locked(PGSTAT_KIND_DATABASE, > MyDatabaseId, InvalidOid, > > + if (!OidIsValid(dboid)) > > + return; > > + > > + dbref =3D pgstat_get_entry_ref_locked(PGSTAT_KIND_DATABASE, dboid, > InvalidOid, > > false); > > > > This bypass of an invalid database OID is actually incorrect in the > > patch. There is a stats entry with a database OID of 0, documented as > > such in [1] for shared objects. There is one test in the main > > regression test suite that triggers this case: > > SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass= ); > > > > The short answer is to remove this check based on OidIsValid(), and > > allow the timestamp be reset for this entry of 0 rather than ignore > > the update. > > > > [1]: > https://www.postgresql.org/docs/devel/monitoring-stats.html#MONITORING-PG= -STAT-DATABASE-VIEW > > -- > > Michael > > Thanks for pointing out the test. I badly excluded *.sql and *.out in my > vscode search last time. > > Then the question becomes why the test didn't fail. I looked into it, and > it seems the existing test does not check the stats_reset timestamp. I ha= ve > now added checks for the stats_reset of both database 0 and the current > database. > > With those checks in place, the test fails without this patch, and also > fails with the incorrect OidIsValid(dboid) check in v1. With the v2 patch= , > the test passes. > > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ > > Hi Evan, I tested the v2 patch on HEAD. It applies cleanly, compiles without warnings, and all 247 regression tests pass. I also manually verified the bug fix by comparing behavior before and after the patch: Without the patch, calling pg_stat_reset_single_table_counters('pg_shdescription'::regclass) incorrectly updates the current database's stats_reset timestamp while leaving the shared db entry (datid=3D0) unchanged. With the patch, the shared db entry's stats_reset is correctly updated, and the current database's timestamp is not affected. Regards, Dapeng Wang --0000000000002565e4064f1abc7c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Chao Li <li.evan.chao@gmail.com> =E4=BA=8E2026=E5=B9=B44=E6=9C= =8810=E6=97=A5=E5=91=A8=E4=BA=94 16:45=E5=86=99=E9=81=93=EF=BC=9A


> On Apr 10, 2026, at 15:56, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Fri, Apr 10, 2026 at 03:12:41PM +0900, Michael Paquier wrote:
>> If we decide to expand pgstat_reset() in other contexts in the
>> back-branches, we'd be silently trapped as well.
>>
>> The connect and disconnect calls are less critical, perhaps we cou= ld
>> remove the argument altogether, but I cannot get excited about tha= t
>> either as some extensions may rely on these as currently designed.=
>>
>> I cannot look at that today, will do so later..
>
> -=C2=A0 =C2=A0 dbref =3D pgstat_get_entry_ref_locked(PGSTAT_KIND_DATAB= ASE, MyDatabaseId, InvalidOid,
> +=C2=A0 =C2=A0 if (!OidIsValid(dboid))
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
> +
> +=C2=A0 =C2=A0 dbref =3D pgstat_get_entry_ref_locked(PGSTAT_KIND_DATAB= ASE, dboid, InvalidOid,
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0false);
>
> This bypass of an invalid database OID is actually incorrect in the > patch.=C2=A0 There is a stats entry with a database OID of 0, document= ed as
> such in [1] for shared objects.=C2=A0 There is one test in the main > regression test suite that triggers this case:
> SELECT pg_stat_reset_single_table_counters('pg_shdescription':= :regclass);
>
> The short answer is to remove this check based on OidIsValid(), and > allow the timestamp be reset for this entry of 0 rather than ignore > the update.
>
> [1]: https://www.postgresql.org/docs/devel/monitoring-stats.html#MONITORING-PG= -STAT-DATABASE-VIEW
> --
> Michael

Thanks for pointing out the test. I badly excluded *.sql and *.out in my vs= code search last time.

Then the question becomes why the test didn't fail. I looked into it, a= nd it seems the existing test does not check the stats_reset timestamp. I h= ave now added checks for the stats_reset of both database 0 and the current= database.

With those checks in place, the test fails without this patch, and also fai= ls with the incorrect OidIsValid(dboid) check in v1. With the v2 patch, the= test passes.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
ht= tps://www.highgo.com/


Hi Evan,

I tested the v2 patch on HE= AD. It applies cleanly, compiles
without warnings, and all 247 regressio= n tests pass.

I also manually verified the bug fix by comparing beha= vior
before and after the patch:

Without the patch, calling
pg= _stat_reset_single_table_counters('pg_shdescription'::regclass)
= incorrectly updates the current database's stats_reset timestamp
whi= le leaving the shared db entry (datid=3D0) unchanged.

With the patch= , the shared db entry's stats_reset is correctly
updated, and the cu= rrent database's timestamp is not affected.

Regards,
Dap= eng Wang=C2=A0
--0000000000002565e4064f1abc7c--