logoalt Hacker News

tschellenbachtoday at 7:51 AM1 replyview on HN

Imagine you have a SAAS app. Microsoft and Apple are customers. You have a table of devices. Type is either windows or osx. How does postgres know how to handle the query devices, where type = OSX properly? How does it know that this matches ~0 or ~100 of rows depending on the customer?

This is the main thing the planner doesn't handle well. Postgres was built before SAAS was as big. You have different distributions per customer, and thousands of customers. In most cases the query planner will guess right, but sometimes it will fail and scan millions of rows.


Replies

fabian2ktoday at 8:12 AM

You can manually add more advanced statistics in PostgreSQL. That includes statistics over multiple columns.

https://www.postgresql.org/docs/current/planner-stats.html#P...