Last Updated: March 28, 2018
·
1.654K
· bennycwong

TIL: Postgres: Partial Index On Boolean Field Tp

I came across something interesting when working on a migration in a rails app:

When creating a partial index on a boolean field, IS and = in the where clause the the partial index are not the same thing.

For example:
Given a table products with a boolean column counterfeit
I created an index on counterfeit with the constraint counterfeit = true.

add_column :products, :counterfiet, :boolean
add_index :products, :id, algorithm: :concurrently, where: "(counterfiet = TRUE)"

It hits the index when I query using the below:
SELECT * FROM "products" WHERE "products"."counterfeit" = TRUE
Selects uses the index I just created.

However, if query using this next form, it does a sequential scan:
SELECT * FROM "products" WHERE "products"."counterfeit" IS TRUE

In order to make it work for either forms, I indexed using the following:

add_index :products, :id, algorithm: :concurrently, where: "(counterfiet = TRUE OR counterfiet IS TRUE)"

Now both forms use the index!

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy