Working Around a Case Where the Postgres Planner Is "Not Very Smart"
We discovered a Postgres quirk that prevented an existing index from supporting an index-only scan. Here's how we worked around the quirk to achieve a 2x performance win.
https://heap.io/blog/when-the-postgres-planner-is-not-very-smart
@DevMisc
#postgres #performance
We discovered a Postgres quirk that prevented an existing index from supporting an index-only scan. Here's how we worked around the quirk to achieve a 2x performance win.
https://heap.io/blog/when-the-postgres-planner-is-not-very-smart
@DevMisc
#postgres #performance
SQLite vs MySQL vs PostgreSQL — A Detailed Guide
...in this story I would be going through one of the major confusion which newbies have regarding various flavours of SQL...
https://medium.com/codex/sqlite-vs-mysql-vs-postgresql-a-detailed-guide
@DevMisc
#database #postgres #sql
...in this story I would be going through one of the major confusion which newbies have regarding various flavours of SQL...
https://medium.com/codex/sqlite-vs-mysql-vs-postgresql-a-detailed-guide
@DevMisc
#database #postgres #sql
How to Get or Create in PostgreSQL
https://hakibenita.com/postgresql-get-or-create
@DevMisc (🟠 comments)
#postgres #database #learn #misc
- Implementing "get or create" functionality correctly in PostgreSQL can be tricky, with potential issues around race conditions, concurrency, and bloat.
- A simple INSERT statement is not idempotent, as executing it with the same input twice will trigger a unique constraint violation error.
- To provide idempotency, the process needs to handle two situations: 1) if the tag already exists, return the existing tag, and 2) if the tag does not exist, create it and return the new tag.
- Using a unique constraint violation to handle "get or create" can lead to bloat, as new rows are first inserted and then marked as dead if a duplicate is found.
- Checking if a tag exists before inserting it (the "look before you leap" approach) can suffer from time-of-check to time-of-use issues when used concurrently.
- The "ask for forgiveness" approach using INSERT ON CONFLICT DO NOTHING is a better way to handle "get or create" without generating bloat.
- Concatenating results from the target table and a WITH clause using UNION ALL can handle visibility issues with data-modifying statements in the WITH clause.
- Even the "ask for forgiveness" approach can suffer from concurrency issues, as a race condition can still occur between checking if a tag exists and inserting it.
- Using INSERT ON CONFLICT with a DO NOTHING clause is the most robust solution, providing idempotency, concurrency safety, and preventing bloat.
- Starting in PostgreSQL 17, the MERGE statement with RETURNING can provide an alternative approach that doesn't require unique or exclusion constraints on the target table.
https://hakibenita.com/postgresql-get-or-create
@DevMisc (
#postgres #database #learn #misc
Please open Telegram to view this post
VIEW IN TELEGRAM
❤2