functional indices with functions taking row as argument

Started by Tomek Zielonkaabout 25 years ago1 messagesgeneral
Jump to latest
#1Tomek Zielonka
tomek-lists@mult.i.pl

Hi.

I have a table which represents clients' sessions / orders.

CREATE TABLE sessions (
id INTEGER NOT NULL DEFAULT nextval('sessions_seq'),
...
finished TIMESTAMP,
bl_confirmed BOOL NOT NULL DEFAULT false,
bl_sent BOOL NOT NULL DEFAULT false,
bl_paid BOOL NOT NULL DEFAULT false,
bl_ok BOOL NOT NULL DEFAULT false,
PRIMARY KEY (id)
);

bl_* fields define state of the session. I define some conditions based on
those booleans and other fields. Example:

CREATE FUNCTION is_session_to_be_sent (SESSIONS) RETURNS BOOL AS '
SELECT ($1.bl_ok) AND ($1.bl_confirmed) AND (NOT $1.bl_sent)
' LANGUAGE 'sql';

I don't want to explicitly pass those three arguments to the function, because
there may be more of them in the future. So I pass SESSIONS type arg.

It would be nice, if I could create functional index on this table. Then the
following query might be faster.

SELECT * FROM sessions s WHERE is_session_to_be_sent(s) = true;

Unfortunately, it seems that there is no appropriate syntax for creating such
an index.

Hope I'm wrong ;)

(I see a workaround - additional boolean field updated in triggers)

greetings,
Tom

--
.signature: Too many levels of symbolic links