How store as JSON the new data inside a trigger in PostgreSQL?
<p>I try to do as explained in:</p>

<p><a href="https://wiki.postgresql.org/wiki/Audit_trigger" rel="nofollow">https://wiki.postgresql.org/wiki/Audit_trigger</a></p>

<blockquote>
  <p>Auditing values as JSON</p>
  
  <p>For PostgreSQL 9.2, or 9.1 with the fantastic json_91 addon, you can
  log the old and new values in the table as structured json instead of
  flat text, giving you much more power to query your audit history.
  Just change the types of v_old_data, v_new_data, original_data and
  new_data from TEXT to json, then replace ROW(OLD.<em>) and ROW(NEW.</em>)
  with row_to_json(OLD) and row_to_json(NEW) respectively.</p>
</blockquote>

<p>However this get me a error:</p>

<pre><code>CREATE OR REPLACE FUNCTION add_log (name text, Action TEXT, data jsonb, OUT RETURNS BOOLEAN)
AS $$
BEGIN
    RETURNS = true;
END;
$$
LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION log_city() RETURNS TRIGGER AS 
$$
DECLARE
v_new_data jsonb;
BEGIN
    IF (TG_OP = 'UPDATE') THEN
        RETURN NEW;
    ELSIF (TG_OP = 'INSERT') THEN
        v_new_data := row_to_jsonb(NEW);
        EXECUTE add_log('City', 'City.New', v_new_data);
        RETURN NEW;
    END IF;
    RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$$ LANGUAGE plpgsql;

INSERT INTO Location (city, state, country) values ('a', 'b' , 'c')
</code></pre>

<p>It say:</p>

<blockquote>
  <p>ERROR:  function row_to_jsonb(location) does not exist</p>
</blockquote>

<p>If I put <code>v_new_data := row_to_jsonb(ROW(NEW));</code> then I get:</p>

<blockquote>
  <p>ERROR:  function row_to_jsonb(record) does not exist</p>
</blockquote>