Example 31-2. libpq Example Program 2

From: Niko Ware <nikowareinc(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Example 31-2. libpq Example Program 2
Date: 2021-03-11 05:25:38
Message-ID: CACHGMtQmkvu3LX-BJmi9kEOuzbqBhRxuZiHXQQhHU8jVd5Xg=A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I need notifications to a C application on inserts to a table. The
notification funcion is listed below. My program and "Example 31-2. libpq
Example Program 2" receive the notification, but the payload message in
"PGnotify *notify->extra" is invalid.

The example code is located here:
https://www.postgresql.org/docs/current/libpq-example.html

I modified the code to include the "extra" member in the following
statement:

fprintf(stderr,
"ASYNC NOTIFY of '%s' received from backend PID %d:
%s\n",
notify->relname, notify->be_pid, *notify->extra*);

Both the example and my application core on an invalid address for "extra".
Using psql with LISTEN works correctly. My application works as expected
except for the payload message. I was unable to locate any example of how
to retrieve the payload in C. The "extra" member is a char*.

================================================

CREATE OR REPLACE FUNCTION OS_FB_UPDATE_FCNFY()
RETURNS trigger
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
notification TEXT;
BEGIN
notification :=
NEW.node_id || ':' ||
NEW.block_id || ':' ||
NEW.operation || ':' ||
NEW.update_class || ':' ||
NEW.update_data || ':' ||
to_char(COALESCE(NEW.time_stamp, current_timestamp), 'MM-DD-YYY
HH24:MI:SS');

PERFORM pg_notify('notifyondatachange', row_to_json(NEW)::text);

RETURN NEW;

EXCEPTION
WHEN others THEN
RAISE WARNING '[ONSITE.OS_FB_UPDATE_FCNFY] - UDF ERROR [OTHER] -
SQLSTATE: %, SQLERRM: %', SQLSTATE, SQLERRM;
END
$BODY$;

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2021-03-11 06:39:52 Re: Example 31-2. libpq Example Program 2
Previous Message Stacey Haysler 2021-03-11 03:48:58 Code of Conduct Russian Translation - Revised March 5, 2021