NULLs: the good, the bad, the ugly, and the unknown
Aug 5, 2022
auto_awesome
The podcast discusses challenges and new features in handling NULL values in databases, including a new feature in Postgres 15. They also share experiences with coding issues in survey data and the importance of handling null values in SQL queries. The chapter highlights resources for learning, emphasizes the importance of testing, and mentions the benefits of Postgres 11 in updating billion row tables.
Postgres 15 introduces a new capability allowing unique constraints to not distinguish null values, enabling only one null value for certain columns.
Nulls in SQL have diverse effects depending on the context, with some operations ignoring nulls while others consider them, leading to potentially unexpected results.
Deep dives
The importance of learning from mistakes
Mistakes in SQL and specifically with null values (Nals) can be painful and tricky to handle. These mistakes often go unnoticed because they don't generate errors immediately. To address this, Postgres 15 introduces a new capability allowing unique constraints to not distinguish Nals, enabling only one null value for certain columns. However, developers must be cautious when dealing with Nals and ensure proper handling to avoid unexpected behaviors.
The challenges with nulls in SQL
Nulls in SQL have diverse effects depending on the context. Some operations ignore nulls, while others consider them, leading to potentially unexpected results. For example, aggregates such as the sum function may ignore nulls, count for a column ignores them, but count star considers all rows, including nulls. Handling nulls in SQL requires constant vigilance and thorough testing to ensure proper functionality.
Addressing null-related challenges
To mitigate the risks associated with nulls, developers and reviewers should focus on understanding the nuances of handling null values in SQL. Training sessions, articles, and learning resources such as 'A Modern SQL' and 'The Art of PostgreSQL' can provide valuable insights and guidance. Additionally, proper testing and realistic test data should be employed to identify and rectify potential issues related to null values.