Live data from Hacker News

Postgres Indexes for Newbies

blog.crunchydata.com

21–30 of 43 posts

Re: Postgres Indexes for Newbies

#21
post #19
post #11

Earlier quoted context omitted.

No guaranteed order, but very typically a predictable order depending on the query plan chosen. If you’re relying on an ordering, you need to specify it in the query. However, the scan order is predictable for a given engine depending on the query plan. If it’s doing an index scan, you’ll get results in index order and if it’s doing a table scan you’ll get it in table storage order (assuming a transactional, single n…

Postgres can do parallel table and index scans. Don't rely on order without explicit order by.

True, I forgot to consider how single node (multi threaded) parallelism can look a lot like multi node parallelism

Re: Postgres Indexes for Newbies

#23

One thing I struggle with when figuring out the best indexe is creating enough junk in the database to force it to use an index vs seq scan. Anyone have tips on good data generation tools that works with foreign keys and creates a good range of data? I've looked into using queries and generate_series but it doesn't work well with UUID keys (I think?) Right now I have a bash script that creates a bunch of entries thro…

A sequential scan can be faster than an indexed search in many cases. There is a good reason optimizers often choose to do sequential scans even when an index is available. Remember, the index is displacing data in the cache. Even for data that is purely in memory, sequential scans are often friendly for CPU performance.

On modern server hardware you usually need megabytes of data in a table before sequential scans start to become suboptimal. Most database engines are designed under this assumption.

Re: Postgres Indexes for Newbies

#24

Use the index, Luke is always a great resource. [0] https://use-the-index-luke.com/

Another amazing resource are the CMU Database Systems videos with Prof. Andy Pavlo. Videos from 2021 with a different professor (Andrew Crotty) have also been uploaded, but Pavlo is just so engaging.

https://www.youtube.com/playlist?list=PLSE8ODhjZXjbohkNBWQs_... (Pavlo, 2019)

https://www.youtube.com/playlist?list=PLSE8ODhjZXjZaHA6QcxDf... (Crotty, 2021)

Re: Postgres Indexes for Newbies

#25
post #4

Earlier quoted context omitted.

BRIN indexes don't affect the storage layer, so you need to make sure your table is appropriately sorted on disk through other means. CLUSTER can do this as a one-off job (pg_repack if you need to keep the table available for writes), but won't sort new data as it's written. https://www.postgresql.org/docs/current/sql-cluster.html https://reorg.github.io/pg_repack/

For all of its greatness and (mostly well deserved) praises, the lack of a reasonable cluster index capability (as in data order at storage layer) is Postgres' biggest limit IMHO. Unfortunately, The CLUSTER command not only "blocks" the table for WRITE ops, but more importantly, it also blocks READ operations [0]. pg_repack helps, but is not always available when using a managed PG offering. Not being able to control…

Any idea what that size is? When should one be worried?

Re: Postgres Indexes for Newbies

#26
post #25

Earlier quoted context omitted.

For all of its greatness and (mostly well deserved) praises, the lack of a reasonable cluster index capability (as in data order at storage layer) is Postgres' biggest limit IMHO. Unfortunately, The CLUSTER command not only "blocks" the table for WRITE ops, but more importantly, it also blocks READ operations [0]. pg_repack helps, but is not always available when using a managed PG offering. Not being able to control…

Any idea what that size is? When should one be worried?

It depends on a lot of factors, my unscientific rule of thumb is that if the data doesn't fit in the RAM of the server, then that's when you may start worrying about how the data will be fetched from the disk when you query it (if performance matters).

The problem case is when the data needed to serve an average query is "scattered across the disk". This means that you will need to potentially fetch way more information from the disk (because data is returned by blocks/pages, not by bytes) than necessary to fulfill the query.

A worst case example: say to compute a query, you need to get 5000 rows (with a size of 100 bytes per row, 500KB in net total), if they are "perfectly scattered" (i.e. each row on a different page), you will really need to bring in 5000 pages of 8KB per page (default page size in Postgres) for a total of 40MB. By fetching 80 times more data than needed, you've essentially reduced your throughput by 80x.

Note that the example above assumes that an index (e.g. btree) can be leveraged. The index would point directly to the numerous pages, which is likely still much better compared to doing a full scan. But that index doesn't solve all your problems, only part 1.

It may not be a big deal as bringing 40MB from a disk would go fast, but this will limit 1) the number of concurrent user you'll be able to serve, 2) if you need 100,000 rows instead of 5000, then your query will take longer to process and it may be negatively noticeable by your user.

If you can co-locate the data on the disk (i.e. put them on same or contiguous pages) deterministically, then you would feel much better about your throughput. Cluster Index (or Indexed Views as it's called in SQL Server) is the typical mechanism to sort the data on the disk in RDBMS. MySQL does that by default with the primary key, but not Postgres.

Re: Postgres Indexes for Newbies

#27
post #2

I’m loving all the Postgres submissions on HN over the last couple days. My professional experience is mostly with Redshift which is only Postgres-like. I was not aware of BRIN indexes. Am I correct in assuming those are suitable for analytical workloads? Is the data implicitly sorted in a BRIN index or is there a mechanism to optimize the storage layer for BRIN indexed data?

A BRIN index is basically a minimum and maximum value in the indexed column for each megabyte of table.

Re: Postgres Indexes for Newbies

#28
post #25

Earlier quoted context omitted.

Any idea what that size is? When should one be worried?

It depends on a lot of factors, my unscientific rule of thumb is that if the data doesn't fit in the RAM of the server, then that's when you may start worrying about how the data will be fetched from the disk when you query it (if performance matters). The problem case is when the data needed to serve an average query is "scattered across the disk". This means that you will need to potentially fetch way more informat…

> is the typical mechanism to sort the data on the disk in RDBMS

SQL Server and Oracle both default to heaps (though it's rare to see a SQL Server table without a clustered index).

Re: Postgres Indexes for Newbies

#29
post #4

Earlier quoted context omitted.

BRIN indexes don't affect the storage layer, so you need to make sure your table is appropriately sorted on disk through other means. CLUSTER can do this as a one-off job (pg_repack if you need to keep the table available for writes), but won't sort new data as it's written. https://www.postgresql.org/docs/current/sql-cluster.html https://reorg.github.io/pg_repack/

For all of its greatness and (mostly well deserved) praises, the lack of a reasonable cluster index capability (as in data order at storage layer) is Postgres' biggest limit IMHO. Unfortunately, The CLUSTER command not only "blocks" the table for WRITE ops, but more importantly, it also blocks READ operations [0]. pg_repack helps, but is not always available when using a managed PG offering. Not being able to control…

Starting with PostgreSQL 11, B-tree indexes can store additional "non-key" columns in the index. These are kept in the index alongside the sorted key columns. This can avoid expensive sorts in certain query plans.

It's not perfect. Stale table statistics sometimes forces PostgreSQL to check table pages to confirm that rows are visible to the current transaction. Index-only scans don't support features like expressions either.

Post reply on HN