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 1wxQAq-002hl9-16 for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Aug 2026 14:24:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wxQAp-00DYzG-0D for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Aug 2026 14:24:47 +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 1wxQAo-00DYz8-2G for pgsql-hackers@lists.postgresql.org; Fri, 21 Aug 2026 14:24:46 +0000 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wxQAm-00000000WAF-1OUI for pgsql-hackers@postgresql.org; Fri, 21 Aug 2026 14:24:46 +0000 Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-c15f020a223so161386266b.1 for ; Fri, 21 Aug 2026 07:24:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=planetscale.com; s=google; t=1787322283; x=1787927083; darn=postgresql.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=j22iend0VyEP51kNw4bYy7FAa+jUnVdu7QNSiGeauzE=; b=Mu+hk4fgaEBSDzzjFZZcf4RcFRsrszDqlRo/41cCNfsOp3NA5qkD74iNNrOafaFlKt igc9d+uLT9m/ZUQAYgRygGcne8fmIMbojKXuvMXVghX1xQOJFe5scgDR/0MYiJAaaIIl UdvFjeagCD3BZciPjeyl09yb1NTdoCbWta27xxtig1C+9f1m1Xd//pwFTyN2zNdN6Nwx FkwVaUw67JDD8tlCUVyisheqKbL5qApXhhip8mwzgCBD3tOS3gu6Jhisq9bX4B+9Pxqi csSXb6U/HKfDJiVd/lech3NgvTLtaewhfBI5cS7iB9qxiF+X2YhcljOEe1ueec58ByZj 3cog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787322283; x=1787927083; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=j22iend0VyEP51kNw4bYy7FAa+jUnVdu7QNSiGeauzE=; b=cdJTNmjJYzG2eMZPPdF4ly9rwIszxyT3Hef0k9wJ7yaqheVQ0kzJX5V0Ul/FzPhIFL OaESV8OAhYDwDbxzI613W5kc4nMGGWcwDF1zjG8C6t+yFvoeZPNImyH5JyXF0bIOa1YT q2cBFZiCdQ9CTVrWJTmyOb2q0J20lfp1Obt9l4v68hr+5082gqApUzWe+BOnpbOTc3oR UDJ2+JyBN2MiWZX400tP1QeDdgyHgTWTZxE9sHCgYuYhxdHJViPcoPvTcM3n8J8vA3WR PEF3Knmk9uEysETuRXomyQ7YzvGD9ylVnKItISPn7wRHEew4Qq89xwVrHq6TQShko7Nk qpQg== X-Gm-Message-State: AFuF++nJcjrzft2HQSHHjyRDhvhdSO8zXw4NwQxKD+ecZZKwPY/6TWSj wLfMiUB0V2F7ye+ptlpR1GmEElfOLb2inQbR+ATR69zjFxmzsygSjcawcdAJ+5A2mus= X-Gm-Gg: AR+sD10L0CetKlrp2PIPD40TDEWLJ/76uQNfRbjbkoj+A5l6atGo0FNqsxnt7arP1q/ zZ+wSsUPFliQZVMH6XEF6XE27dWzElXbAMSLbhjOPGdWCli56b3Q7BkZkpNlj3wbQQ3Ovw0tCgo ndKjC5GlQvZV5GHuWqUIo5R7ttSEX809rGctzPppd+9TJF4D5q46TQh0o3ZBBbpJBr8MPeItV9o 4gzTTJ1S9pVrrAzE4roN/cYACqX7UcAZ/lhdL6wj79jOE1KaqbCqhJdzJW9XWnnLtkwYRJ+9wZ4 cH8kJjUQpn3HAuAlVGecOL7FFSF0aDj5mrk4h5s/+TgDI8hDsu6Jp2qoapkl6BobYcFd0TKbAxZ c650zR+K31W1Pn8UCw1/IoPrpWCh4KlpSvZwAbyaQnhMcr6novUdYdymgRxsfMeX7tFhUsv0Ku+ djGFD5vUqtYCu7gFg1WE0xBEy1sVWLnz5QG/9X8AS2YcfjNH58S+ZKu70l0movernMNe7l1BGYP KBSccjfz2IxKAE4/n7fVz1kPzplQQnZYD1FWdHph69AtUSdecpGAO57iS05 X-Received: by 2002:a17:906:fd81:b0:c21:35d0:f43d with SMTP id a640c23a62f3a-c246a6c52c2mr679655166b.16.1787322282960; Fri, 21 Aug 2026 07:24:42 -0700 (PDT) Received: from ?IPV6:2a04:4540:6534:7700:410f:fe0e:f0c4:7d63? ([2a04:4540:6534:7700:410f:fe0e:f0c4:7d63]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c24591df465sm468849266b.48.2026.08.21.07.24.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Aug 2026 07:24:42 -0700 (PDT) Message-ID: <19204305-0261-4b63-81fd-85da5be78adc@planetscale.com> Date: Fri, 21 Aug 2026 16:24:42 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Many of psql's describe functions bloat cache / waste mem To: xiaoyu liu , andres@anarazel.de Cc: pgsql-hackers@postgresql.org References: <7u2wehplakrobc5rvuomgurjqc3osns2n6fdxsajvhpqbzarmi@4kknsc6ymuja> Content-Language: en-GB From: Jan Nidzwetzki In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Hackers, On 24.07.26 05:17, xiaoyu liu wrote: [...] > If anyone has already started reviewing it, or sees an issue with the > current approach, please let me know. Thanks for the patch. I tested it and it applied to the current master branch (a1bb92fb), and check-world passes. I checked the size of the CacheMemoryContext before and after running \df: master@a1bb92fb: 8,705,984 B patch applied: 1,264,384 B So the patch clearly reduces the size of the CacheMemoryContext populated by this query. I also tested the query plan shape. As mentioned in the comment of the patch, the OID lookup subquery becomes an InitPlan: QUERY PLAN --------------------------------------------------------------------------------------------------------- Sort (cost=268.31..268.32 rows=1 width=224) Sort Key: n.nspname, p.proname, (pg_get_function_arguments(p.oid)) InitPlan array_1 -> Seq Scan on pg_namespace (cost=0.00..1.06 rows=2 width=4) Filter: (nspname = ANY ('{pg_catalog,information_schema}'::name[])) -> Nested Loop Left Join (cost=0.00..267.24 rows=1 width=224) Join Filter: (n.oid = p.pronamespace) -> Seq Scan on pg_proc p (cost=0.00..266.11 rows=1 width=73) Filter: ((pronamespace <> ALL ((InitPlan array_1).col1)) AND pg_function_is_visible(oid)) -> Seq Scan on pg_namespace n (cost=0.00..1.05 rows=5 width=68) (10 rows) Now a 'Nested Loop Left Join' is used. As Andres pointed out, this LEFT JOIN could be changed to a JOIN. Tests ===== I think I found an issue in the test. According to the test description, it ensures "filters system functions before testing visibility". To do so, the filter must run first, then pg_function_is_visible(). This happens currently as desired in the query plan. Filter: ((pronamespace <> ALL ((InitPlan array_1).col1)) AND pg_function_is_visible(oid)) However, PostgreSQL could reorder both conditions (they are ordered that way because of their costs). But the costs could change for whatever reason. For example, if you run "ALTER FUNCTION pg_catalog.pg_function_is_visible(oid) COST 1;", the conditions in the query filter flip to: Filter: (pg_function_is_visible(oid) AND (pronamespace <> ALL ((InitPlan array_1).col1))) In this case, pg_function_is_visible() runs for every row found in the catalog. I verified this using 'funccount' and running \df in parallel (in my database I have 6 user functions and 3455 catalog functions defined): # Filter first and then pg_function_is_visible(oid) $ sudo funccount-bpfcc "$(pg_config --bindir)/postgres:pg_function_is_visible" [...] FUNC COUNT pg_function_is_visible 6 Detaching... # pg_function_is_visible(oid) first and then the filter $ sudo funccount-bpfcc "$(pg_config --bindir)/postgres:pg_function_is_visible" [...] FUNC COUNT pg_function_is_visible 3461 Detaching... The regex only checks the SQL text that psql generates, not the resulting query plan. That text is identical in both cases, so the test cannot detect this regression at all: with the procost changed as above, pg_function_is_visible() is called for every row in pg_proc and 001_basic.pl still passes. That said, I am not sure if we need such kind of test in the end. For example, 112e40b867b reordered clauses for plan reasons in describe.c and went in without tests. Other \d commands ================= As Andres outlined, there are more \d commands affected by this problem and only \df is changed by the current version of this patch. For example, \do uses the old shape of the query plan where 'pg_operator_is_visible' is called for every operator. The query plan for \do: QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=133.01..133.41 rows=161 width=256) Sort Key: n.nspname, o.oprname, (CASE WHEN (o.oprkind = 'l'::"char") THEN NULL::text ELSE format_type(o.oprleft, NULL::integer) END), (CASE WHEN (o.oprkind = 'r'::"char") THEN NULL::text ELSE format_type(o.oprright, NULL::integer) END) -> Hash Join (cost=1.11..127.10 rows=161 width=256) Hash Cond: (o.oprnamespace = n.oid) -> Seq Scan on pg_operator o (cost=0.00..42.18 rows=268 width=89) Filter: pg_operator_is_visible(oid) -> Hash (cost=1.07..1.07 rows=3 width=68) -> Seq Scan on pg_namespace n (cost=0.00..1.07 rows=3 width=68) Filter: ((nspname <> 'pg_catalog'::name) AND (nspname <> 'information_schema'::name)) (9 rows) What do you think about introducing a helper function like 'appendSystemSchemaFilter()' that adds the filter condition as proposed in the patch, and calling it from the other places in describe.c that exclude the system schemas? Best regards Jan -- Jan Nidzwetzki PlanetScale Postgres Core Team