PostgreSQL filtering on array length inside JSON
Without knowing your data, I can only guess that the selectivity of your index is low (which happen if the length of the array does not vary much).

One trick to overcome this might be changing the query slightly and creating a covering index.  For this, choose a NOT NULL column (for example, the primary key of the table) to count, and then include this column in the index:

CREATE INDEX n_items ON your_table (jsonb_array_length(data-&gt;'items'), id);

SELECT count(id) 
  FROM your_table
 WHERE JSONB_ARRAY_LENGTH(table.data-&gt;'items') = 2;


This will hopefully turn into an index-only scan (I tested this omitting the jsonb part, but you will  be able to tell if it works).