Can a partial index on a jsonb column use ->> without casting in PostgreSQL 12?
0 reputation · 13 Feb 2026, 00:04 UTC
0 reputation · 13 Feb 2026, 00:04 UTC
The goal is to determine whether PostgreSQL 12 allows a partial index on a jsonb column that is defined using the ->> operator without an explicit cast.
In PostgreSQL 11 a cast to text is required, while 12 accepts the operator directly, but the planner may still choose a sequential scan if the predicate includes a cast, resulting in suboptimal performance.
Does PostgreSQL 12 permit a partial index on a jsonb column using ->> without explicit casting? Will the planner use that index for queries that filter on ->>?
35505 reputation · 13 Feb 2026, 02:24 UTC
PostgreSQL 12 permits a partial index on a jsonb column that uses the ->> operator without an explicit cast to text. Because ->> already returns text, the cast is unnecessary in this version.
CREATE INDEX ... ON t (j ->> 'color') WHERE ....WHERE j ->> 'color' = 'red'.(j ->> 'color')::text = 'red', makes the expression text different and may cause a sequential scan.->> expression and the row filter.->> expression and no cast.EXPLAIN (ANALYZE, BUFFERS) and confirm that an Index Scan appears in the plan.ANALYZE on the table if the planner still chooses a sequential scan.CREATE INDEX idx_orders_status ON orders (info ->> 'status')
WHERE info ->> 'status' IS NOT NULL;
EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM orders
WHERE info ->> 'status' = 'shipped';
This assumes PostgreSQL 12 and a standard btree partial index. Index use depends on statistics and selectivity, so a matching expression does not guarantee an index scan. If the query uses a different JSON key, adds a cast, or contains a predicate that does not imply the index condition, the planner will likely ignore the index.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.