I want to use Postgres array to store events per each sender. Each event is persisted as a Postgres custom type (object).
How can I query records by custom type properties? Is it possible or not?
If yes, can I put any index type on array column to speed up the queries?
Create custom type:
CREATE TYPE sender_event AS (
event_timestamp TIMESTAMP(3),
message TEXT
);
Create table with array of custom types:
CREATE TABLE sender_history (
sender varchar(30),
events sender_event[]
);
Insert few records to the table:
INSERT INTO sender_history VALUES
('sender1', ARRAY[(now(), 'Message text 1')::sender_event]),
('sender2', ARRAY[(now(), 'Message text 2')::sender_event])
How can I find all records which 'events' array contain object with "message" property equal to 'Message text 2'?
E.g. something like this (this does not work):
SELECT * FROM sender_history
WHERE events.message = 'Message text 2'
Expected result should be the second record in that table.