1

I'm trying to create an optimized, partial index to find all rows where the numeric field amount is not zero.

This

CREATE INDEX amount_not_zero_idx ON my_table (amount)
WHERE amount <> 0;

is working, but it's not really what I have in mind as only the information "unlike zero" is important. What I would like to do is something like

CREATE INDEX amount_not_zero_idx ON my_table (amount <> 0)
WHERE amount <> 0;

as this should give the best performance - but PostgreSQL gives a syntax for the "<>" error.

Any suggestions?

1 Answer 1

2

Found it myself. Problem was simply missing brackets, correct syntax is

CREATE INDEX amount_not_zero_idx ON my_table ((amount <> 0))
WHERE amount <> 0;
Sign up to request clarification or add additional context in comments.

1 Comment

I typically include the PK columns in a partial index so that it can be used for lookups.

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.