Live data from Hacker News

How a single PostgreSQL config change improved slow query performance by 50x

amplitude.engineering

41–46 of 46 posts

Re: How a single PostgreSQL config change improved slow query performance by 50x

#41
Hmm... The author is filling a combo-box (probably just one column), yet the query is selecting all columns (SELECT *).

I would have tried selecting just the needed column (let's call it "foo"), with following indexes:

event_types (app, id)

prop_keys (event_id, foo)

This should cover the entire query with indexes (i.e. allow for index-only scan).

Re: How a single PostgreSQL config change improved slow query performance by 50x

#42
post #9

Earlier quoted context omitted.

That feels like something the database could do for itself - checking whether a range of mmap()ed blocks are actually in memory or not is a single syscall. I guess for large indexes the overhead of walking the page tables is going to be large though, so it’s not necessarily going to be a net win.

But you don't know which blocks you'll need at planning time, so you can't really check that. You could of course check if the total database size is within RAM, but it's much more common to have database much larger than RAM (say 1TB on a machine with 128GB of RAM), but the actual working set (recent data processed by queries) is much smaller.

But you don't know which blocks you'll need at planning time, so you can't really check that.

Really? Postgres doesn't know which blocks are part of which tables / indexes? That seems ... suboptimal if so.

Re: How a single PostgreSQL config change improved slow query performance by 50x

#43
post #42

Earlier quoted context omitted.

But you don't know which blocks you'll need at planning time, so you can't really check that. You could of course check if the total database size is within RAM, but it's much more common to have database much larger than RAM (say 1TB on a machine with 128GB of RAM), but the actual working set (recent data processed by queries) is much smaller.

But you don't know which blocks you'll need at planning time, so you can't really check that. Really? Postgres doesn't know which blocks are part of which tables / indexes? That seems ... suboptimal if so.

You can know it, but you've to actually evaluate the index lookups first, so ...?

Re: How a single PostgreSQL config change improved slow query performance by 50x

#44
post #42

Earlier quoted context omitted.

But you don't know which blocks you'll need at planning time, so you can't really check that. You could of course check if the total database size is within RAM, but it's much more common to have database much larger than RAM (say 1TB on a machine with 128GB of RAM), but the actual working set (recent data processed by queries) is much smaller.

But you don't know which blocks you'll need at planning time, so you can't really check that. Really? Postgres doesn't know which blocks are part of which tables / indexes? That seems ... suboptimal if so.

I guess one could implement a job which samples the pages of tables and indexes to check what percentage is typically in RAM and use that for an estimate, but to get which actual pages will be hit by a query you need to start executing it (you need to do an index lookup to see which index pages and table pages which will be accessed) which would defeat the purpose of query planning.

Re: How a single PostgreSQL config change improved slow query performance by 50x

#45

This seems like something that should be measured at startup?

Sadly that is not (at least easily) possible because random_page_cost vs. sequential_page_cost is not just about the IO system but about all factors which can affect the cost of reading pages randomly vs reading pages sequentially. E.g. how often PostgreSQL's tables are in the file cache. So how much RAM your machine has available for PostgreSQL and your access patterns matter too.

Also I imagine the some expensive SAN solutions would be pretty tricky to measure given how smart they try to be with caching and moving between different kinds of disks.

Re: How a single PostgreSQL config change improved slow query performance by 50x

#46
post #44
post #42

Earlier quoted context omitted.

But you don't know which blocks you'll need at planning time, so you can't really check that. Really? Postgres doesn't know which blocks are part of which tables / indexes? That seems ... suboptimal if so.

I guess one could implement a job which samples the pages of tables and indexes to check what percentage is typically in RAM and use that for an estimate, but to get which actual pages will be hit by a query you need to start executing it (you need to do an index lookup to see which index pages and table pages which will be accessed) which would defeat the purpose of query planning.

Granted, but (AFAICT) in this case the database was doing a full table scan because it didn’t think the index was in memory. Checking to see whether the index itself is already loaded seems like something the query planner in principle ought to be able to do efficiently. (Obviously the existing codebase might make it difficult.)
Post reply on HN