How to use variable settings in trigger functions?

前端 未结 3 883
死守一世寂寞
死守一世寂寞 2021-02-19 10:05

I would like to record the id of a user in the session/transaction, using SET, so I could be able to access it later in a trigger function, using current_sett

3条回答
  •  一向
    一向 (楼主)
    2021-02-19 10:31

    It is not clear why you are trying to concat NULL to user_id but it is obviously the cause of the problem. Get rid of it:

    CREATE OR REPLACE FUNCTION add_transition1() RETURNS TRIGGER AS $$
        DECLARE
            user_id integer;
        BEGIN
            user_id := current_setting('myvars.user_id')::integer;
            INSERT INTO transitions1 (user_id, house_id) VALUES (user_id, NEW.id);
            RETURN NULL;
        END;
    $$ LANGUAGE plpgsql;
    

    Note that

    SELECT 55 || NULL
    

    always gives NULL.

提交回复
热议问题