How to check if value is an integer with plpgsql?

I use this function in a trigger:

CREATE OR REPLACE FUNCTION xx() RETURNS trigger AS $xx$
    BEGIN   
        INSERT INTO my_log (x, y, z) VALUES (NEW.x, NEW.y, current_setting('myvar.user'));
        RETURN NULL;
    END;
$xx$ LANGUAGE plpgsql;

Now I would like to check if "myvar.user" is a valid integer, and if not, execute another INSERT statement.

How can i do this?

+5
source share
2 answers
SELECT  current_setting('myvar.user') ~ '^[0-9]+$'
+9
source

Taken from archives.postgresql.org :

CREATE FUNCTION isnumeric(text) RETURNS boolean AS '
SELECT $1 ~ ''^[0-9]+$''
' LANGUAGE 'sql';
+2
source

All Articles