Improved estimates for Postgres
Hi!
I've been working on extracting more information from Postgres statistics
to better approximate some estimation errors I've faced in the past.
I've never submitted a patch before, so before I go and do that, I'd love
for some feedback on it from the hackers lurking here.
Patch is in https://github.com/lrweck/postgres/pull/1
I am also working on other patches that I might share soon.
Thanks!!
*Regards,*
*--*
*Luís R. Weck*
On Thu, 2026-11-19 at 08:57 -0300, Luís Roberto Weck wrote:
I've been working on extracting more information from Postgres statistics to better approximate some estimation errors I've faced in the past.
I've never submitted a patch before, so before I go and do that, I'd love for some feedback on it from the hackers lurking here.
Patch is in https://github.com/lrweck/postgres/pull/1
I am also working on other patches that I might share soon.
+2450 lines?
The right approach is to come up with a design first and to discuss it on pgsql-hackers.
Your monster patch seems to implement several features at once. Don't do that.
Select a single, self-contained feature and do that.
Pick a simple one for your first patch.
Make sure that you review and understand every single line of code that your chatbot
generated and can answer questions about it. Dumping generated code on human
reviewers is impolite; people would resent the waste of their time.
Read the pages with advice for beginning hackers on the PostgreSQL Wiki.
Best hang around on pgsql-hackers for a while and review a couple of patches
in a commitfest, that will get you in tune with how things work.
Yours,
Laurenz Albe
On 11/19/26 17:11, Laurenz Albe wrote:
On Thu, 2026-11-19 at 08:57 -0300, Luís Roberto Weck wrote:
I've been working on extracting more information from Postgres statistics to better approximate some estimation errors I've faced in the past.
I've never submitted a patch before, so before I go and do that, I'd love for some feedback on it from the hackers lurking here.
Patch is in https://github.com/lrweck/postgres/pull/1
I am also working on other patches that I might share soon.
+2450 lines?
The right approach is to come up with a design first and to discuss it on pgsql-hackers.
Your monster patch seems to implement several features at once. Don't do that.
Select a single, self-contained feature and do that.
Pick a simple one for your first patch.Make sure that you review and understand every single line of code that your chatbot
generated and can answer questions about it. Dumping generated code on human
reviewers is impolite; people would resent the waste of their time.Read the pages with advice for beginning hackers on the PostgreSQL Wiki.
Best hang around on pgsql-hackers for a while and review a couple of patches
in a commitfest, that will get you in tune with how things work.
That's a bit harsh, but I agree the patch needs to be split into smaller
pieces, to make review easier. It also makes it possible to commit one
piece that's ready, and keep working on the rest.
The ticket even splits it into 5 different cases, so why not to split
the patch like that? It's possible an initial commit will introduce some
shared infrastructure, or maybe the patches will have dependencies, but
that's OK.
If you split it like that, I'll take a look ...
regards
--
Tomas Vondra
Thanks for the feedback!
I agree that it is a big change. I'll split it into smaller, self-contained
pieces.
*Regards,*
*--*
*Luís R. Weck*
On Thu, Nov 19, 2026, 13:22 Tomas Vondra <tomas@vondra.me> wrote:
Show quoted text
On 11/19/26 17:11, Laurenz Albe wrote:
On Thu, 2026-11-19 at 08:57 -0300, Luís Roberto Weck wrote:
I've been working on extracting more information from Postgres
statistics to better approximate some estimation errors I've faced in the
past.I've never submitted a patch before, so before I go and do that, I'd
love for some feedback on it from the hackers lurking here.
Patch is in https://github.com/lrweck/postgres/pull/1
I am also working on other patches that I might share soon.
+2450 lines?
The right approach is to come up with a design first and to discuss it
on pgsql-hackers.
Your monster patch seems to implement several features at once. Don't
do that.
Select a single, self-contained feature and do that.
Pick a simple one for your first patch.Make sure that you review and understand every single line of code that
your chatbot
generated and can answer questions about it. Dumping generated code on
human
reviewers is impolite; people would resent the waste of their time.
Read the pages with advice for beginning hackers on the PostgreSQL Wiki.
Best hang around on pgsql-hackers for a while and review a couple of
patches
in a commitfest, that will get you in tune with how things work.
That's a bit harsh, but I agree the patch needs to be split into smaller
pieces, to make review easier. It also makes it possible to commit one
piece that's ready, and keep working on the rest.The ticket even splits it into 5 different cases, so why not to split
the patch like that? It's possible an initial commit will introduce some
shared infrastructure, or maybe the patches will have dependencies, but
that's OK.If you split it like that, I'll take a look ...
regards
--
Tomas Vondra
I was looking at inequality joins and I think I found an opportunity to
improve
how they are estimated. The planner uses a fixed 1/3 (DEFAULT_INEQ_SEL) for
them, which is a sensible default when nothing is known, but these columns
often do have MCV lists and histograms, so I think we could use them. For
a value v,
scalarineqsel() already computes the fraction of the left side satisfying
"x < v" for a constant; averaging that over the right side's distribution
turns
the join into an expectation, using statistics that are already there. It
falls
back to 1/3 when a side's statistics cannot describe it, and seven tests
compare
the estimate with the actual row count.
I would appreciate feedback on the fallbacks and on the planning cost of
the extra lookups.
The code is here for now https://github.com/lrweck/postgres/pull/5
@Tomas Vondra <tomas@vondra.me>, if you think this approach has any ground,
I'd be more than
happy to continue pursuing it! I know I need to format it as a patch and
send it to
the hackers mailing list.
*Regards,*
*--*
*Luís R. Weck*