If you know that the association between language and room does not change, you can feed this information to Postgres by way of an IMMUTABLE
function.
CREATE OR REPLACE FUNCTION room_lang(int)
RETURNS varchar(12) AS
$$
SELECT lang FROM room WHERE id = $1
$$ LANGUAGE sql IMMUTABLE;
And use this for partial indexes:
CREATE INDEX idx_en ON message ...
WHERE room_lang(room) = 'english';
CREATE INDEX idx_es ON message ...
WHERE room_lang(room) = 'spanish';
Of course, you have to recreate any such index, if you change anything in room
that breaks the promise of "immutability", thereby breaking the index ...
Use a compatible WHERE
clause for your queries to let Postgres know it can use the index:
SELECT ...
WHERE room_lang(room) = 'english';
Here is a related example for indexes with an IMMUTABLE
function with a lot more details:
Does PostgreSQL support "accent insensitive" collations?
Aside: I'd rather use just text
instead of varchar(12)
.