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.
Postgres Indexes for Newbies
21–30 of 43 posts
Re: Postgres Indexes for Newbies
#22Use the index, Luke is always a great resource. [0] https://use-the-index-luke.com/
Re: Postgres Indexes for Newbies
#23One 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…
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
#24Use the index, Luke is always a great resource. [0] https://use-the-index-luke.com/
https://www.youtube.com/playlist?list=PLSE8ODhjZXjbohkNBWQs_... (Pavlo, 2019)
https://www.youtube.com/playlist?list=PLSE8ODhjZXjZaHA6QcxDf... (Crotty, 2021)
Re: Postgres Indexes for Newbies
#25Earlier 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…
Re: Postgres Indexes for Newbies
#26Earlier 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?
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
#27I’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?
Re: Postgres Indexes for Newbies
#28Earlier 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…
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
#29Earlier 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…
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.