Transforming and casting index columns

A slow query despite an index. Wrapping a column in a function keeps it out of the index search condition (Index Cond). PostgreSQL does no implicit casting and reports it as an error.

SQL

Situation

The settlement screen looks up one day of paid orders. The developer habitually compares dates as strings, so the date column is wrapped in to_char. Despite an index, finding 131 rows for one day reads close to 20,000 blocks.

Tables

TableColumnTypeDescription
ordersstatusvarcharDONE 90%, PAID 7%, CANCELED 3%
ordersord_dttimestampOrder date and time
IndexColumns
orders_x2status, ord_dt, ord_id

One day of paid orders

BEFORE · Transformed date

SELECT *
FROM orders
WHERE status = 'PAID'
  AND to_char(ord_dt, 'YYYYMMDD') = '20250915'

Run it and check which condition searched orders_x2 (Index Cond) and how many rows it found (A-Rows).

Approach

1. Look at "with which condition" the index is used, not whether it is used

Seeing orders_x2 in the execution plan is no reason to relax. Check whether the index node's condition covers only status or also ord_dt. If the rows found in the index (A-Rows) far exceed the result rows, the index did not narrow the range.

2. Find whether the column side is transformed in the WHERE clause

Wrapping a column in a function such as to_char(ord_dt, …) makes the value stored in the index (ord_dt) differ from what is compared (the to_char result). The index is sorted by ord_dt, not by the to_char result, so it cannot set a search range.

3. Move the transformation to the value side

Consider whether you can leave the column as is and change the compared value instead, as in "at or after midnight that day, before midnight the next day".

Explanation

Transformed date → range condition

AFTER · Range condition

SELECT *
FROM orders
WHERE status = 'PAID'
  AND ord_dt >= DATE '2025-09-15'
  AND ord_dt <  DATE '2025-09-16'

Why it got faster

  • In Before, Bitmap Index Scan finds 140,577 rows with status = 'PAID' alone, and Bitmap Heap Scan reads every table block holding those rows, then filters with the to_char condition to leave 131. Buffers about 19,400.
  • In After, the Index Scan's Index Cond holds both status and the ord_dt range, so it finds only that day's 131 rows. Buffers about 23.

Why this is optimal

  • The index finds only the result rows, and only the table blocks holding them are read. One day of orders sits in one section of the table (insert order = date order).

Variation: a cast is a transformation too

TRY · ord_dt::date

SELECT *
FROM orders
WHERE status = 'PAID'
  AND ord_dt::date = DATE '2025-09-15'
  • The ::date cast also transforms the column, so it yields the same plan as Before (Buffers about 19,400).

DB difference: implicit casting

In Oracle, comparing a character column with a number makes the DB convert the column side to a number (implicit casting), which disables the index. PostgreSQL rejects such a comparison outright with an error.

SELECT * FROM orders WHERE ord_no = 2025091500000001;
-- ERROR: operator does not exist: character varying = bigint

It tells you immediately instead of slowing down silently. Writing the value side as a string ('2025091500000001') finds the single row right away through orders_x3.

Key points

  • Do not transform index columns; change the compared value instead (range condition).
  • In the execution plan, distinguish the index node's Index Cond from its Filter. Only Index Cond reduces the search range.
  • If a transformation is unavoidable, an expression index (CREATE INDEX … ON orders (to_char(ord_dt, 'YYYYMMDD'))) exists, but this lab does not change the structure.