Conditional foreign keys and polymorphism in SQL: 4 Methods
© Laurenz Albe 2023 This article is about the notoriously difficult problem of “conditional foreign keys”. In object-oriented programming languages, polymorphism is a natural concept. However, it does not translate well to SQL. As a consequence, many people whose data models are driven by an object-oriented application design end up trying to implement conditional foreign […]
Postgres v16: 14 Cool New Features
PostgreSQL v16 contains many new features and enhancements. Here are PG v16’s 14 best new features: Everybody's favorite: You no longer need an alias for subqueries in FROM This in an extension to the standard, but makes porting queries from Oracle easier. Improve performance of vacuum freezing Have you ever suffered from massive anti-wraparound autovacuum […]
Indexing “LIKE” in PostgreSQL and Oracle
© Laurenz Albe 2023 Unless you use the binary collation, creating a b-tree index to support a LIKE condition in PostgreSQL is not straightforward. This keeps surprising Oracle users, who claim that a regular b-tree index will of course always support LIKE. I decided to explore the differences between Oracle and PostgreSQL when it comes […]
Why does my pg_wal keep growing?
© Laurenz Albe 2023 “Why does my pg_wal keep growing?” That’s a question I keep hearing again and again. It is an urgent question, since the PostgreSQL server will crash if pg_wal runs out of disk space. I decided to answer the question once and for all. What is pg_wal and why is it growing? […]
Bulk load performance in PostgreSQL
© Laurenz Albe 2023 There are several techniques to bulk load data into PostgreSQL. I decided to compare their performance in a simple test case. I’ll add some recommendations for parameter settings to improve the performance even more. An example table to bulk load data The table is simple enough: It is a narrow table […]
Subqueries and performance in PostgreSQL
© Laurenz Albe 2023 SQL allows you to use subqueries almost anywhere where you could have a table or column name. All you have to do is surround the query with parentheses, like (SELECT …), and you can use it in arbitrary expressions. This makes SQL a powerful language – and one that can be […]
Memory context: private memory management in PostgreSQL
© Laurenz Albe 2023 PostgreSQL uses shared memory for data shared between processes. With the exception of the dynamic shared memory segments used for exchanging data between parallel workers, the server allocates shared memory with a fixed size when it starts. But each PostgreSQL backend process also has to manage private memory to process SQL […]
Use HOT, so CLUSTER won’t rot in PostgreSQL
© Laurenz Albe 2023 CLUSTER is sometimes the last resort to squeeze performance out of an index scan. Normally, you have to repeat CLUSTER regularly to maintain good performance. In this article, I will show you how you can get away without re-CLUSTERing even in the face of concurrent UPDATEs. Thanks to Steven Hulshof for […]
ERROR: invalid byte sequence – Fix bad encoding in PostgreSQL
(Inscription seen on a T-shirt) It’s annoying to get error messages caused by encoding problems. But it is more annoying not to get error messages and end up with bad data in the database. I’ll show you how to fix bad encoding. Some basic facts about encoding in PostgreSQL Each PostgreSQL database has a server […]
PostgreSQL ALTER DEFAULT PRIVILEGES – permissions explained
© Laurenz Albe 2023 Many people have heard that ALTER DEFAULT PRIVILEGES makes it easy to allow other users access to tables. But then, many people don’t understand the command in depth, and I hear frequent complaints that ALTER DEFAULT PRIVILEGES does not work as expected. Read on if you want to know better! Default […]
Forcing a join order in PostgreSQL
© Laurenz Albe 2023 Different from many other database systems, PostgreSQL does not support query hints. That makes it difficult to force the hand of the query planner when it comes to a certain join order that you know to be good. This article explains how you can influence execution plans in PostgreSQL. Why no […]
Docker and sudden death for PostgreSQL
© Laurenz Albe 2023 This is a short war story from a customer problem. It serves as a warning that there are special considerations when running software in a Docker container. The problem description The customer is running PostgreSQL in Docker containers. They are not using the “official” image, but their own. Sometimes, under conditions […]