You don't need to pass NEW.anything
as an argument; it's automatically supplied to the trigger function as the variable NEW
. So your trigger can just access NEW.fk_measurement
from anywhere within it, so long as tg_op
is INSERT
or UPDATE
(NEW
is not set for DELETE
or TRUNCATE
triggers).
Triggers can take arguments, but they appear (as zero323 notes) in TG_ARGV
. They don't need to be used for column values, and in fact cannot reference column values; their purpose is for things like parameterized triggers, triggers with optional functionality, etc.
On re-reading your question you do need a parameter for the column name of interest. Unfortunately it's very awkward to access columns by dynamic name in the NEW
tuple.
You'd create the trigger as:
CREATE TRIGGER earth_based_measurement_locations_tg1
BEFORE INSERT OR UPDATE
ON measurements.earth_based_measurement_locations
FOR EACH ROW
EXECUTE PROCEDURE c.is_earth_based_measurement_tg('fk_measurement');
i.e. passing the colname as a string literal.
The trigger procedure would be defined as:
CREATE OR REPLACE FUNCTION is_earth_based_measurement_tg() RETURNS trigger AS $$
BEGIN
IF tg_op = 'INSERT' OR tg_op = 'UPDATE' THEN
PERFORM my_func(hstore(NEW) -> TG_ARGV[0]);
END IF;
END;
$$ LANGUAGE plpgsql;
... or whatever else you want to do with the value you extract from the column.
You cannot set the value of the column in NEW
via a dynamic column name in current PostgreSQL versions (up to and including 9.4).
Note that we convert the NEW
tuple into a hstore, then de-reference it by column name.