Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unused index on jsonb/jsonb arrow filter #2859

Merged
merged 1 commit into from
Jul 12, 2023

Conversation

steve-chavez
Copy link
Member

Closes #2594

Before:

curl "localhost:3000/bets?data_jsonb->>contractId=eq.1" -H "Accept: application/vnd.pgrst.plan"

Aggregate  (cost=32.26..32.28 rows=1 width=112)
  ->  Seq Scan on bets  (cost=0.00..32.23 rows=6 width=36)
        Filter: ((to_jsonb(data_jsonb) ->> 'contractId'::text) = '1'::text)

Now:

curl "localhost:3000/bets?data_jsonb->>contractId=eq.1" -H "Accept: application/vnd.pgrst.plan"

Aggregate  (cost=12.67..12.69 rows=1 width=112)
  ->  Bitmap Heap Scan on bets  (cost=4.18..12.65 rows=4 width=68)
        Recheck Cond: ((data_jsonb ->> 'contractId'::text) = '1'::text)
        ->  Bitmap Index Scan on bets_data_jsonb  (cost=0.00..4.18 rows=4 width=0)
              Index Cond: ((data_jsonb ->> 'contractId'::text) = '1'::text)

Comment on lines +370 to +386
it "should use an index for ordering on a json arrow operator" $ do
r <- request methodGet "/bets?order=data_json->>contractId"
[(hAccept, "application/vnd.pgrst.plan")] ""

let resBody = simpleBody r

liftIO $ do
resBody `shouldSatisfy` (\t -> T.isInfixOf "Index" (decodeUtf8 $ LBS.toStrict t))

it "should use an index for ordering on a jsonb arrow operator" $ do
r <- request methodGet "/bets?order=data_jsonb->>contractId"
[(hAccept, "application/vnd.pgrst.plan")] ""

let resBody = simpleBody r

liftIO $ do
resBody `shouldSatisfy` (\t -> T.isInfixOf "Index" (decodeUtf8 $ LBS.toStrict t))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix these I had to add a new CoercibleOrderTerm type. This is so we know the type of the order field and not apply to_jsonb unnecessarily.

Comment on lines 231 to +238
resolveTableField table (fieldName, jp) =
case resolveTableFieldName table fieldName of
cf@CoercibleField{cfIRType=""} -> cf{cfJsonPath=jp}
cf -> cf{cfJsonPath=jp, cfIRType="json"}
-- types that are already json/jsonb don't need to be converted with `to_jsonb` for using arrow operators `data->attr`
-- this prevents indexes not applying https://github.com/PostgREST/postgrest/issues/2594
cf@CoercibleField{cfIRType="json"} -> cf{cfJsonPath=jp}
cf@CoercibleField{cfIRType="jsonb"} -> cf{cfJsonPath=jp}
-- other types will get converted `to_jsonb(col)->attr`
cf -> cf{cfJsonPath=jp, cfToJson=True}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main logic for the fix. Now is in only one place.

@steve-chavez steve-chavez marked this pull request as ready for review July 11, 2023 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Can't use JSONB indexes in where clause due to to_jsonb call
2 participants