73

Using Postgres 9.4, I want to create an index on a json column that will be used when searching on specific keys within the column.

For example I have an 'farm' table with a json column 'animals'.

The animals column has json objects of the general format:

'{"cow": 2, "chicken": 11, "horse": 3}'

I have tried a number of indexes (separately):

  1. create INDEX animal_index ON farm ((animal ->> 'cow'));
  2. create INDEX animal_index ON farm using gin ((animal ->> 'cow'));
  3. create INDEX animal_index ON farm using gist ((animal ->> 'cow'));

I want to run queries like:

SELECT * FROM farm WHERE (animal ->> 'cow') > 3;

and have that query use the index.

When I run this query:

SELECT * FROM farm WHERE (animal ->> 'cow') is null;

then the (1) index works, but I can't get any of the indexes to work for the inequality.

Is such an index possible?

The farm table contains only ~5000 farms, but some of them contain 100s of animals and the queries simply take too long for my use case. An index like this is the only method I can think of for speeding this query up, but perhaps there is another option.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
lnhubbell
  • 3,304
  • 5
  • 17
  • 22

1 Answers1

114

Your other two indexes won't work simply because the ->> operator returns text, while you obviously have the jsonb gin operator classes in mind. Note that you only mention json, but you actually need jsonb for advanced indexing capabilities.

To work out the best indexing strategy, you'd have to define more closely which queries to cover. Are you only interested in cows? Or all animals / all tags? Which operators are possible? Does your JSON document also include non-animal keys? What to do with those? Do you want to include rows in the index where cows (or whatever) don't show up in the JSON document at all?

Assuming:

  • We are only interested in cows at the first level of nesting.
  • The value is always a valid integer.
  • We are not interested in rows without cows.

I suggest a functional btree index, much like you already have, but cast the value to integer. I don't suppose you'd want the comparison evaluated as text (where '2' is greater than '1111').

CREATE INDEX animal_index ON farm (((animal ->> 'cow')::int));  -- !

The extra set of parentheses is required for the cast shorthand to make the syntax for the index expression unambiguous.

Use the same expression in your queries to make Postgres realize the index is applicable:

SELECT * FROM farm WHERE (animal ->> 'cow')::int > 3;

If you need a more generic jsonb index, consider:

For a known, static, trivial number of animals (like you commented), I suggest partial indexes like:

CREATE INDEX animal_index ON farm (((animal ->> 'cow')::int))
WHERE (animal ->> 'cow') IS NOT NULL;

CREATE INDEX animal_index ON farm (((animal ->> 'chicken')::int))
WHERE (animal ->> 'chicken') IS NOT NULL;

Etc.

You may have to add the index condition to the query:

SELECT * FROM farm
WHERE (animal ->> 'cow')::int > 3
AND   (animal ->> 'cow') IS NOT NULL; 

May seem redundant, but may be necessary. Test with ANALYZE!

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Thank you! Wonderful answer. I am only interested in the first level. The value is always an integer. However I am interested in some other animals. Does it make sense to simply replicate this index for each animal I'm interested in? – lnhubbell Mar 18 '16 at 15:45
  • 1
    @lnhubbell: For a *known, static, trivial* number of animals this should be the simplest and most efficient solution. I would explicitly make them partial indexes, though. See addendum above. – Erwin Brandstetter Mar 18 '16 at 17:39
  • If I need sort the jsonb sub field, only b-tree would work, and gin only support equals / contains, but can't sort, is that correct? – Eric Sep 13 '22 at 01:46
  • Basically yes. Here is the list of operators currently supported by the respective operator classes for `jsonb`: https://www.postgresql.org/docs/14/gin-builtin-opclasses.html – Erwin Brandstetter Sep 13 '22 at 02:48