1

Using Postgres 10.11.

Table schema:

create table "db"."table"
(
    id varchar(4096) not null
        constraint "db-table_pkey"
            primary key,
    etag varchar(255) not null,
    jsondata jsonb not null
);

We found to be accidentally creating an index like this:

create index "something"
    on "db"."table" (('id'::text), 
                     (jsondata -> 'someOtherField'::text), 
                     ((jsondata -> 'someOtherField2'::text) = 'false'::jsonb));

And this query would not use the above index:

select *
FROM "db"."table" 
where jsonData->'someOtherField' = '"value"'
  AND jsonData->'someOtherField2' = 'false'
  AND id = 'testid'
order by id 
limit 5

Remaking the index like this solved the issue:

create index "something"
    on "db"."table" ((id), -- JUST ID!!!
                     (jsondata -> 'someOtherField'::text), 
                     ((jsondata -> 'someOtherField2'::text) = 'false'::jsonb));

It seems we are not supposed to use single quotes on index column names. Can anyone tell me what the index was made on in the first part? Was it just made on the 'id' string constant? Could I even trigger that index with any query? Or are they the same index?

1 Answer 1

1

It seems we are not supposed to use single quotes on index column names.

Your intuition is right.

'id' is a string constant, so if you index that, all index entries will have the same constant value. Such an index is pretty useless (there are use cases for partial or unique indexes on constants though).

To reference a column, you must use unquoted id or double quoted "id".

The documentation has a whole chapter on that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.