I am using PostgreSQL 11.8
CREATE INDEX ix_products_product_sku_with_json_columns
ON products USING btree((product_sku))
INCLUDE ((data->>'variantCode'));
ERROR: expressions are not supported in included columns
SQL state: 0A000
Documentation says:
Expressions are not supported as included columns since they cannot be used in index-only scans.
I suppose data->>'variantCode' is an expression and that's the end of the story?
I can see two workarounds, none of which looks appealing to me:
- create a dedicated VariantCode regular SQL column in the table
- put
data->>'variantCode'as part of the index keys
How bad is the second option?
Ultimately the goal would be to do an Index-Only Scan in the following query:
SELECT data->>'variantCode', ARRAY_AGG(product_sku)
FROM products
GROUP BY data->>'variantCode'