In C, the classic short cut evalution might be something like
if ( a != NULL && a->b == 0 )
…
to avoid evaluating a structure reference if invalid…
I’m trying something similar in a postgresql trigger:
IF TG_TABLE_NAME::text != 'collection_item' OR OLD.domain = 'HERB' OR NEW.domain = 'HERB' THEN
... blah
END IF;
So if the table is collection_item it contains the field domain, but other tables don’t have that field. So in theory if table is not collection_item, it should short circuit and not evaluate OLD.domain. However this wasn’t working, it would give an error that
`ERROR: record “old” has no field “domain”
So I read here about evaluation rules,
https://www.postgresql.org/docs/9.0/sql-expressions.html#SYNTAX-EXPRESS-EVAL
It implies that if you encounter this problem you can get around it with CASE. So I tried that…
IF (CASE WHEN TG_TABLE_NAME::text != 'collection_item' THEN true WHEN OLD.domain = 'HERB' OR NEW.domain = 'HERB' THEN true ELSE false END) THEN
... blah
END IF;
but I still get “ERROR: record “old” has no field “domain”`
I can get around it with nesting IFs…
IF TG_TABLE_NAME::text = 'collection_item' THEN
IF OLD.domain = 'HERB' OR NEW.domain = 'HERB' THEN
is_target := true;
END IF;
ELSE
is_target := true;
END IF;
But I’m curious why my other attempts don’t work.
I’m expecting short cut evalution to work.