Re: [ADMIN] Notification

From: Jonathan Gardner <jgardner(at)jonathangardner(dot)net>
To: "Anagha Joshi" <ajoshi(at)nulinkinc(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: [ADMIN] Notification
Date: 2003-06-17 20:34:23
Message-ID: 200306171334.24931.jgardner@jonathangardner.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday 17 June 2003 05:13, Anagha Joshi wrote:
> Hi All,
> I'm new to Postgres. I'm using Postgres-7.2.4
> Can anybody guide me in detail how 'NOTIFY-LISTEN' can be implemented
> progmatically bet'n frontend and backend?
>

Might want to move this to the SQL list.

The idea is that a process will LISTEN for some notification. Another process
will NOTIFY that notification, and the one listening will get a message.

This is useful for a couple of things.

If you have an app that shows something like customer data, then you might
want to update the customer info when it changes.

Here's the table:

CREATE TABLE customer (
customer_id SERIAL PRIMARY KEY
, first_name VARCHAR(20) NOT NULL
, last_name VARCHAR(20) NOT NULL
);

When displaying the customer info for customer_id 19291, you could do
something like:

LISTEN customer_19291;

Now when someone else goes in and changes the customer info in the database,
you can have a trigger set up that will NOTIFY.

CREATE OR REPLACE FUNCTION notify_on_update() RETURNS TRIGGER AS '
BEGIN
EXECUTE ''NOTIFY ''||TG_RELNAME||''_''||NEW.customer_id;
RETURN NEW;
END
' LANGUAGE 'plpgsql';

CREATE TRIGGER notify_on_update AFTER update ON customer
FOR EACH ROW EXECUTE PROCEDURE notify_on_update();

Now you set up your application to watch for the notification. When it
receives the notification, if can update the displayed information.

Is this a good starting place?

- --
Jonathan Gardner <jgardner(at)jonathangardner(dot)net>
(was jgardn(at)alumni(dot)washington(dot)edu)
Live Free, Use Linux!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+73tPWgwF3QvpWNwRAqSQAKC/IsVy45bg4FAy4vwOK0PvBcfbjACg2XWT
D98xYLKLXVuQPkUGTiCVHVE=
=kpg2
-----END PGP SIGNATURE-----

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Jonathan Gardner 2003-06-17 20:37:37 Re: Can the frontend get notifications from the postgres server?
Previous Message Charles Hornberger 2003-06-17 20:32:33 Re: postmaster dead but backends still running?

Browse pgsql-sql by date

  From Date Subject
Next Message Jonathan Gardner 2003-06-17 20:37:37 Re: Can the frontend get notifications from the postgres server?
Previous Message Guy Fraser 2003-06-17 19:45:06 Re: Request for advice: Table design