How to add a new column if this doesn't exist
You can use INFORMATION_SCHEMA.COLUMNS to check if the column exists or not.

CREATE TABLE t(rid int, foo int);

INSERT INTO t VALUES (1,1),(2,2),(3,3);

CREATE FUNCTION fnn(id int, new_val int) RETURNS void AS $$
BEGIN

  IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 't' AND COLUMN_NAME = 'bar') THEN
      ALTER TABLE t
        ADD bar int;
  END IF;

 UPDATE t
 SET    bar = new_val
 WHERE  id = rid;

END; $$
LANGUAGE plpgsql;


SELECT fnn(1, 15);

SELECT * FROM t;

rid | foo |  bar
--: | --: | ---:
  2 |   2 | &lt;em&gt;null&lt;/em&gt;
  3 |   3 | &lt;em&gt;null&lt;/em&gt;
  1 |   1 |   15


db&lt;>fiddle here