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.98.2) (envelope-from ) id 1xPjB7-00000000rgR-3rHO for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Nov 2026 16:22:07 +0000 Received: from localhost ([127.0.0.1]:57128 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xPirG-000000049ly-0O5T for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Nov 2026 16:01:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:49906) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPiTe-00000003mNn-1USO for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 15:37:10 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPiTc-00000000scz-0cQo for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 15:37:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=yqwFyhX+tpRAkvEuULLssUtVf3UDm3HHhMYloypbT9M=; b=6Qa0UCZI2vP6Vv7L8IeaHrjS4T ZRJbTxmxe73Ei7szdXowMswxnIsPNDip03+R+zovXu+YFMeY14ExYoI7AXUn9AmJb4wDnQx6EV1Nw kBEC6yebCG10nDEkOofDCOTv6+Xwcgvhgrs+01yDObQB4ITvNm13I9CFS/wCbgl/c82/HFwr7v32R 7GurILNadS6aYuUg0mjK1kQV7dNighFDzFcTarIYcuFJhgpiqC3wj8h1+28xYo8bwyAOt9kFbfFJW L8e23W9PwzIFukJYOA82JyVWvb8HJoTZM++YNpwQsBqrm9kmSaKq+HdbWp8PsSXh9Gg5hdizNWl62 FKgj22yw==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPiTa-000000022Bu-2Bpw for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 15:37:06 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xPiTY-000000057wP-2y7d for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 15:37:05 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19849: queries fail after ANALYZE with expression stats on arrays To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 125atishprak@gmail.com Reply-To: 125atishprak@gmail.com, pgsql-bugs@lists.postgresql.org Date: Sat, 07 Nov 2026 15:36:50 +0000 Message-ID: <19849-38b8bd2fea807154@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19849 Logged by: Atish Prakash Email address: 125atishprak@gmail.com PostgreSQL version: 18.6 Operating system: Linux x86_64 Description: =20 With extended statistics on an array or tsvector expression, once ANALYZE finds no elements common enough to store, queries that use the statistics fail at planning time with an internal error. Elements that are all distinct are enough to trigger this, as is a column of empty arrays. CREATE TABLE tags(id int, a int[]); INSERT INTO tags SELECT i, ARRAY[i, i + 1000000] FROM generate_series(1, 50000) i; CREATE STATISTICS s_a ON (a || a) FROM tags; ANALYZE tags; SELECT count(*) FROM tags WHERE (a || a) @> '{5}'; Actual, on 19rc1 and master: ERROR: unexpected null value in cached tuple for catalog pg_statistic column stavalues3 Expected, as on 18.6: the query runs and returns 1. Also failing the same way: - (t || t) on text[], with @> - (v || v) on tsvector, with @@ (the message names stavalues1) - the array operators && and <@, and =3D ANY A range expression with no non-empty values also stores a NULL stavalues, but the query I tried, (a * a) && '[1,2)', did not fail. A plain column with the same data, and an expression index with the same data, do not fail. Cause: commit 261f89a976 ("Track the maximum possible frequency of non-MCE array elements") stores a statistics slot even when there are no values. It changed the test in update_attstats() from numvalues > 0 to stavalues !=3D NULL, but serialize_expr_stats() in extended_stats.c still tests numvalues[k] > 0, so the slot kind and numbers are stored with a NULL stavalues. get_attstatsslot() then fails in SysCacheGetAttrNotNull(). I identified the commit from the code and the commit message, and did not bisect. A possible patch is attached (0001-fix-expression-stats-empty-stavalues.patch). It changes that test to match update_attstats(). With it, the queries above return correct results on master, and all 239 core regression tests pass. It adds no new test. Found with statistics round-trip tests. I reproduced and checked the result myself.