snippetsqlMinor
How to create an "empty" partial index, or equivalent, in Postgres?
Viewed 0 times
howcreateequivalentpostgresemptypartialindex
Problem
I have a boolean column that will be false for >99.9% of the rows. I need to efficiently fetch all rows where that column is true.
What's the best option? Creating an index on the column? Creating a partial index where the column is true? But I don't know what columns the partial index would / should hold.
This doesn't parse, but is there any way to do something like:
What's the best option? Creating an index on the column? Creating a partial index where the column is true? But I don't know what columns the partial index would / should hold.
This doesn't parse, but is there any way to do something like:
CREATE INDEX mytable_cond ON mytable () WHERE cond = TRUE; ? Where the index holds literally zero columns?Solution
You are saying "rows" where I think you mean "columns".
It is not legal to specify an empty list of columns in an index. Just pick a column, preferably one with a short data type. If nothing else, just repeat the same column in both places.
Be careful how you formulate our index and queries. A
It is not legal to specify an empty list of columns in an index. Just pick a column, preferably one with a short data type. If nothing else, just repeat the same column in both places.
CREATE INDEX mytable_cond ON mytable (cond) WHERE cond = TRUE;Be careful how you formulate our index and queries. A
cond = true query cannot use a partial index defined where cond is true and vice versa, those are not considered equivalent operations.Code Snippets
CREATE INDEX mytable_cond ON mytable (cond) WHERE cond = TRUE;Context
StackExchange Database Administrators Q#264335, answer score: 2
Revisions (0)
No revisions yet.