How do I tell PostgreSQL to return first encountered value instead of an aggregate column?
Table "public.cache"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+---------+-----------+---------+--------------+-------------
user_id | integer | | plain | |
object_id | integer | | plain | |
data | integer | | plain | |
data2 | boolean | | plain | |
Indexes:
"cache_object_id_user_id_key" UNIQUE CONSTRAINT, btree (object_id, user_id)
"cache_user_id_object_id_key" UNIQUE CONSTRAINT, btree (user_id, object_id)
Has OIDs: no
Query grouping by object_id and data2 will make hash aggregate, what I want to avoid.
SELECT object_id, data2 FROM cache GROUP BY object_id, data2;
I found bool_or() but it will scan all values in bad case.
SELECT object_id, bool_or(data2) FROM cache GROUP BY object_id;
Moreover, there is no such function for any datatype. What I want to do is to get any of the values from data2 column so that engine don't have to iterate all rows.
What about data column which is integer?