Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1miLry-0001MS-8O for pgsql-general@arkaria.postgresql.org; Wed, 03 Nov 2021 19:24:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1miLrw-0006f6-Ti for pgsql-general@arkaria.postgresql.org; Wed, 03 Nov 2021 19:24:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1miLrw-0006ex-Jz for pgsql-general@lists.postgresql.org; Wed, 03 Nov 2021 19:24:20 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1miLrq-000427-19 for pgsql-general@lists.postgresql.org; Wed, 03 Nov 2021 19:24:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 1A3JOA1F144198; Wed, 3 Nov 2021 15:24:11 -0400 From: Tom Lane To: Joel Perren cc: pgsql-general@lists.postgresql.org Subject: Re: Selectivity and row count estimates for JSONB columns In-reply-to: References: Comments: In-reply-to Joel Perren message dated "Wed, 20 Oct 2021 17:08:01 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <144196.1635967450.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 03 Nov 2021 15:24:10 -0400 Message-ID: <144197.1635967450@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Joel Perren writes: > - generic_restriction_selectivity() returns the default value (0.01) for > data types that Postgres doesn't collect standard MCV and/or histogram > statistics for. I think this is what happens with Table B which (quite > correctly) does not have these statistics in pg_stats There's nothing "correct" about that. JSONB does have sorting support (admittedly with a pretty arbitrary sort order), so I'd expect ANALYZE to collect a histogram as well as MCV values for it. Perhaps for your Table B it's omitting the histogram because the MCV list captures the entire contents of the column? Or you've got auto-analyze disabled for some reason? Anyway, assuming you're using v14, the idea of matchingsel() is to apply the given restriction clause to all the MCV and histogram entries to see how many satisfy it [1]. In principle this'll produce a far better estimate than any fixed default could provide. If you're coming out with a crappy estimate, you might be able to improve matters by increasing the column's statistics target so that more MCV and histogram entries are collected. regards, tom lane [1] https://www.postgresql.org/message-id/flat/12237.1582833074%40sss.pgh.= pa.us