From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joel Perren <joel.perren@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Selectivity and row count estimates for JSONB columns
Date: Wed, 03 Nov 2021 15:24:10 -0400
Message-ID: <144197.1635967450@sss.pgh.pa.us> (raw)
In-Reply-To: <CACFz3n3MRnyKa_hMBxYPRnAa-cFv67AwhjDwEB2mzicF4F+RKw@mail.gmail.com>
References: <CACFz3n13sazt00u+_AbGL5Ur9danB56sHPhP61ymnqUHQRZ1uQ@mail.gmail.com>
<CACFz3n3MRnyKa_hMBxYPRnAa-cFv67AwhjDwEB2mzicF4F+RKw@mail.gmail.com>
Joel Perren <joel.perren@gmail.com> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, joel.perren@gmail.com
Subject: Re: Selectivity and row count estimates for JSONB columns
In-Reply-To: <144197.1635967450@sss.pgh.pa.us>
References: <CACFz3n13sazt00u+_AbGL5Ur9danB56sHPhP61ymnqUHQRZ1uQ@mail.gmail.com> <CACFz3n3MRnyKa_hMBxYPRnAa-cFv67AwhjDwEB2mzicF4F+RKw@mail.gmail.com> <144197.1635967450@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox