Question

My question is how can I set the default values for a certain index in variable length array column. I am trying to set the value of one of the positions to default to string 'false'.

Reason being is I want to be able to have a where clause on a query to filter items where the position value is 'true' (I am setting specific ones to true manually) but without having the rest of my data default to false on that value my query is not fetching any data.

If this is confusing let me know and I will try to clear it up..Thanks in advance

Was it helpful?

Solution

Generally, a column default is for the whole column - for the whole array in your case.

You would have to write a trigger for that. But you may not need any of this. I quote your question:

... but without having the rest of my data default to false on that value my query is not fetching any data.

You can probably avoid this if you query with:

(arr[5] = 'true') IS NOT TRUE

(which includes all cases there the flag is anything but 'true', even NULL) instead of:

arr[5] <> 'true'

And if you need this query fast you can support it with a partial index:

CREATE INDEX tbl_special_idx ON tbl (col1) -- potentially more columns
WHERE (arr[5] = 'true') IS NOT TRUE;

The important part is the WHERE clause. You can use columns in place of col1 that might cover the whole query (especially in v9.2) or that have additional conditions in your query ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top