In this script I expected the output to be:
NOTICE: 00000: Outside value is: hello
NOTICE: 00000: Inside value is: hello
but the output was:
NOTICE: 00000: Outside value is: hello
NOTICE: 00000: Inside value is: my_variable
Why is PostgreSQL using the name of the variable as a string literal when passing it to the function? Notice I didn't put single quotes around the variable name, so I was not expecting it to be interpreted as a string literal.
Here is the script:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table(my_field text);
CREATE OR REPLACE FUNCTION my_function() RETURNS TRIGGER AS $$
BEGIN
RAISE NOTICE 'Inside value is: %', TG_ARGV[0];
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DO $$
DECLARE
my_variable text := 'hello';
BEGIN
RAISE NOTICE 'Outside value is: %', my_variable;
CREATE TRIGGER my_trigger
AFTER INSERT ON my_table
FOR EACH ROW
EXECUTE PROCEDURE my_function(my_variable);
END
$$;
INSERT INTO my_table VALUES('some value');