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 1wAeZ5-000BpZ-2c for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 01:52:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wAeZ2-002zb2-2i for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 01:52:13 +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 1wAeZ2-002zat-1X for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 01:52:13 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wAeZ1-000000006KI-08OC for pgsql-hackers@postgresql.org; Thu, 09 Apr 2026 01:52:13 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-82cf96ca071so214387b3a.1 for ; Wed, 08 Apr 2026 18:52:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775699528; x=1776304328; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Kw2infKqYWgjntkvx8oqRmh25iKwB9tl/6MNQV6l12c=; b=M0NQ9gwnfLBb5xs4um44N+PqG3fYmZCkiie8EH6LjX9Kc3C8J1sUWRtFPMlNj8s9h8 J038FbkbD9hlmiOY472i9JK5aGadZvpqNSzsEim1nqX+nWABv6bDj207T+31sbP0ZGbO KfBEUJ9Ve+w56ATxf2VqWawAo8amRnhH0CAQI/4qdu7NhfP4VYYGBRwMrL6VVn6HUES1 ZiidsODhXwbKMyK2z6bwmjNcT1MxwUwUpyQ0ht0gnkkfDy/ncS8arg+vCJBjaORNYKu0 gMzqoGPJ4W7PzFg50yWSvmQPRrOSqvT4bgOVLWSDpKIqmAikm2JQev0dzsK5xHM3HJSG /Jlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775699528; x=1776304328; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Kw2infKqYWgjntkvx8oqRmh25iKwB9tl/6MNQV6l12c=; b=m1D3REVB6oZQdTaAhMNQNwzU3lPdWaDl2p9HX2QleUrnEH51S6qO1VN8prR6T/8D/+ Z3lfUR5bon2G2kmM8MSyy2frrCr5Fy/rlOv+M7Cnn0JGRoYb1XQhV5pkUIo506zmJkKJ uN1BhTqHa/QeBMeakwiWtInezlSBe5meLVcREGx0OKc1E+MsluRsadFB3+3882MIpPDw GxzOK9QWMwMLF3nhx8y+H5aKlswsVdb0CdlBcPiQ/+/wkoWO0Js+DY3HNzOgtS5ZimY+ xaiqF3J4xZqyAUtwOL2v8U4f31urk15HJj4lXomROMyp6u9tAXjMFxW3+Bir2cLKR1m2 8Vjg== X-Gm-Message-State: AOJu0YyXonAhJ1hiuGNxbbbFmesWDaNXM/jOjOj17fYEh4uRcAu78MZL Au/Aph9c9W7dBZgLxOp82fLV3D/lNClFncym1rh1FArZi3gVnkp/Ixt5 X-Gm-Gg: AeBDietu7T7wl1ZfOpPbV7B/mdIGWk98b2ICzAX+NjDnHA0xs/Z8s/uYQyYhyqyr6+o 37p0ZAba67f6vn4t7NWKXmKyHIvH1MYbNNhFvlv6JRuZA+x4AJR4irYKswT3w3SsUhsVNRZ26bq xw9cwzVf60BNnhWVjFsgG13oOM36V+/oU+b1FUY4FQyFhL7jmKpqXUNsRNFuSl9T7kqaG8atIod 6kd7+FmVxeM1CQkLg6rjsnfn//sn/EyAA8WYRSDZw60XjCbWnFPwFt13K+xaTOMeVdCmSITAQ3D D5IqTAtbRiFr1vLz7JiyB3ykv8VrAE/Qxlfbj1XBBkOO75UbQdr+GOCq9Q9FpT5AL+yFOJrizNp NQuA4J765lAhFaDithbGPO0LQWNEpz0Tw5FEZIvpsPEablRjcs4KWjpvx+EKWfSu4InXS8fcUAI cUJKgoPQNZAPXQp0H73B9rZ2v+TnF2324= X-Received: by 2002:a05:6a00:6c9d:b0:824:188b:c173 with SMTP id d2e1a72fcca58-82dd894e0b3mr1780605b3a.5.1775699528511; Wed, 08 Apr 2026 18:52:08 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82cf9c6f0c3sm22316114b3a.43.2026.04.08.18.52.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Apr 2026 18:52:07 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: pgstat vs aset From: Chao Li In-Reply-To: Date: Thu, 9 Apr 2026 09:51:26 +0800 Cc: pgsql-hackers@postgresql.org, Nathan Bossart , Tom Lane , Alexander Lakhin , Sami Imseih , Bharath Rupireddy , Treat , satyanarlapuram@gmail.com, tndrwang@gmail.com Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Andres Freund X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Apr 9, 2026, at 07:04, Andres Freund wrote: >=20 > Hi, >=20 > In [1] I looked at pgstat memory usage after sort of a complaint by = Nathan. > The conversion of "PgStat Shared Ref" to slab seems like an = improvement we > obviously should make [2]. >=20 > In the email I also showed how much CacheMemoryContext using a lot of = memory > after doing a database wide VACUUM (with ~100k tables), not very = surprising. >=20 >=20 > I didn't immediately look at the next few entries: > = =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=AC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=AC=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=AC=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=AC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=AC=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=90 > =E2=94=82 name =E2=94=82 total_bytes =E2=94=82 = total_nblocks =E2=94=82 free_bytes =E2=94=82 free_chunks =E2=94=82 = used_bytes =E2=94=82 > = =E2=94=9C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=A4 > =E2=94=82 CacheMemoryContext =E2=94=82 168820784 =E2=94=82 = 31 =E2=94=82 2363064 =E2=94=82 33 =E2=94=82 166457720 = =E2=94=82 > =E2=94=82 PgStat Pending =E2=94=82 27983872 =E2=94=82 = 3419 =E2=94=82 27846912 =E2=94=82 112699 =E2=94=82 136960 = =E2=94=82 > =E2=94=82 smgr relation table =E2=94=82 16777216 =E2=94=82 = 12 =E2=94=82 2797592 =E2=94=82 50 =E2=94=82 13979624 = =E2=94=82 > =E2=94=82 MdSmgr =E2=94=82 8388608 =E2=94=82 = 11 =E2=94=82 3475256 =E2=94=82 1 =E2=94=82 4913352 = =E2=94=82 > =E2=94=82 PgStat Shared Ref =E2=94=82 5767344 =E2=94=82 = 88 =E2=94=82 19712 =E2=94=82 352 =E2=94=82 5747632 = =E2=94=82 > =E2=94=82 PgStat Shared Ref Hash =E2=94=82 4195376 =E2=94=82 = 2 =E2=94=82 712 =E2=94=82 0 =E2=94=82 4194664 = =E2=94=82 > =E2=94=82 Relcache by OID =E2=94=82 4194304 =E2=94=82 = 10 =E2=94=82 57448 =E2=94=82 24 =E2=94=82 4136856 = =E2=94=82 > =E2=94=82 TopMemoryContext =E2=94=82 173264 =E2=94=82 = 7 =E2=94=82 32968 =E2=94=82 79 =E2=94=82 140296 = =E2=94=82 > = =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=B4=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=B4=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=B4=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=B4=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=B4=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=98 >=20 > When I did I was quite surprised to see "PgStat Pending" using so = much, as it > should all be freed after the stats have been submitted, a few seconds = later > at most. >=20 > After a moment of worry about having introduced a leak, and adding the > necessary columns to see more information, it's clear that all the = memory was > actually freed. It's just that we don't ever free aset blocks, even if = all the > constituent memory has been freed. So the overall context size doesn't = shrink. >=20 > That seems decidedly not great. >=20 > I wonder how bad it would be to teach aset to recognize this = situation. >=20 > But I think for this use case we actually have a more fitting memory = context > type for this workload, i.e. GenerationContext. With that the size = after the > same vacuum is >=20 > =E2=94=82 PgStat Pending =E2=94=82 134144 =E2=94=82 = 3 =E2=94=82 133848 =E2=94=82 0 =E2=94=82 296 = =E2=94=82 >=20 > Seems like a fairly obvious change. >=20 >=20 > My local experimental changes attached. >=20 >=20 > Greetings, >=20 > Andres Freund >=20 > [1] = https://postgr.es/m/fcvnawwq32mamvf66q5i3sk73xudxz5corqlgqljtocepspjps%40y= pvl6yjzy5xk > [2] It's a big enough saving that I'm kinda wondering about whether we = should > try to sneak it into 19. > I tested this patch by creating a large number of tables and then = running VACUUM. Before the patch: ``` evantest=3D# select name, total_bytes, free_bytes, used_bytes, = total_nblocks, free_chunks from pg_backend_memory_contexts where name in ('PgStat Pending', 'PgStat Shared Ref', 'PgStat Shared Ref = Hash') order by name; name | total_bytes | free_bytes | used_bytes | = total_nblocks | free_chunks = ------------------------+-------------+------------+------------+---------= ------+------------- PgStat Pending | 16384 | 8368 | 8016 | = 5 | 8 PgStat Shared Ref | 8192 | 3672 | 4520 | = 4 | 2 PgStat Shared Ref Hash | 9280 | 704 | 8576 | = 2 | 0 (3 rows) evantest=3D# vacuum; VACUUM evantest=3D# evantest=3D# select name, total_bytes, free_bytes, used_bytes, = total_nblocks, free_chunks from pg_backend_memory_contexts where name in ('PgStat Pending', 'PgStat Shared Ref', 'PgStat Shared Ref = Hash=E2=80=99) = =20 order by name; name | total_bytes | free_bytes | used_bytes | = total_nblocks | free_chunks = ------------------------+-------------+------------+------------+---------= ------+------------- PgStat Pending | 65536 | 64896 | 640 | = 11 | 244 PgStat Shared Ref | 8142848 | 79328 | 8063520 | = 997 | 1987 PgStat Shared Ref Hash | 4195392 | 704 | 4194688 | = 2 | 0 (3 rows) ``` After the patch: ``` evantest=3D# select name, total_bytes, free_bytes, used_bytes, = total_nblocks, free_chunks from pg_backend_memory_contexts=20 where name in ('PgStat Pending', 'PgStat Shared Ref', 'PgStat Shared Ref = Hash=E2=80=99) = =20 order by name; name | total_bytes | free_bytes | used_bytes | = total_nblocks | free_chunks = ------------------------+-------------+------------+------------+---------= ------+------------- PgStat Pending | 8192 | 3360 | 4832 | = 4 | 0 PgStat Shared Ref | 16802 | 13536 | 3266 | = 1 | 188 PgStat Shared Ref Hash | 9280 | 704 | 8576 | = 2 | 0 (3 rows) evantest=3D# evantest=3D# vacuum; VACUUM evantest=3D# evantest=3D# evantest=3D# evantest=3D# select name, total_bytes, free_bytes, used_bytes, = total_nblocks, free_chunks from pg_backend_memory_contexts where name in ('PgStat Pending', 'PgStat Shared Ref', 'PgStat Shared Ref = Hash=E2=80=99) order by name; name | total_bytes | free_bytes | used_bytes | = total_nblocks | free_chunks = ------------------------+-------------+------------+------------+---------= ------+------------- PgStat Pending | 37888 | 37592 | 296 | = 3 | 0 PgStat Shared Ref | 7274914 | 3672 | 7271242 | = 444 | 51 PgStat Shared Ref Hash | 4195392 | 704 | 4194688 | = 2 | 0 (3 rows) ``` For PgStat Pending, total_bytes went from about 64kB down to about 36kB. For PgStat Shared Ref, total_bytes went from about 8MB down to about = 7MB. As a comparison point, PgStat Shared Ref Hash showed no change before = and after the patch. So this does show improvement to me. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/