Seth Woolley's Man Viewer

notify(7) - NOTIFY - generate a notification - man 7 notify

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

NOTIFY(7)                        SQL Commands                        NOTIFY(7)

       NOTIFY - generate a notification

       NOTIFY name

       The  NOTIFY  command sends a notification event to each client applica-
       tion that has previously executed LISTEN name for the specified notifi-
       cation name in(1,8) the current database.

       The  information passed to the client for a notification event includes
       the notification name and the notifying session's server  process  PID.
       It is up to the database designer to define the notification names that
       will be used in(1,8) a given database and what each one means.

       Commonly, the notification name is the same as the name of  some  table
       in(1,8)  the  database,  and the notify event essentially means, ``I changed
       this table, take a look(1,8,3 Search::Dict) at it to see what's new''. But no such associa-
       tion  is  enforced  by  the  NOTIFY and LISTEN commands. For example, a
       database designer could use several  different  notification  names  to
       signal(2,7) different sorts of changes to a single table.

       NOTIFY  provides  a simple form of signal(2,7) or interprocess communication
       mechanism for a collection of processes accessing the  same  PostgreSQL
       database.   Higher-level mechanisms can be built by using tables in(1,8) the
       database to pass additional data (beyond a mere notification name) from
       notifier to listener(s).

       When NOTIFY is used to signal(2,7) the occurrence of changes to a particular
       table, a useful programming technique is to put the NOTIFY  in(1,8)  a  rule
       that  is triggered by table updates.  In this way, notification happens
       automatically when the table is changed, and the application programmer
       can't accidentally forget to do it.

       NOTIFY interacts with SQL transactions in(1,8) some important ways. Firstly,
       if(3,n) a NOTIFY is executed inside a transaction, the notify events are not
       delivered until and unless the transaction is committed. This is appro-
       priate, since if(3,n) the transaction is aborted, all the commands within it
       have  had  no  effect, including NOTIFY. But it can be disconcerting if(3,n)
       one is expecting the notification events to be  delivered  immediately.
       Secondly,  if(3,n)  a listening session receives a notification signal(2,7) while
       it is within a transaction, the notification event will not  be  deliv-
       ered  to  its connected client until just after the transaction is com-
       pleted (either committed or aborted). Again, the reasoning is that if(3,n) a
       notification  were  delivered  within  a  transaction  that  was  later
       aborted, one would want the notification to be undone somehow---but the
       server  cannot  ``take back'' a notification once it has sent it to the
       client.  So notification events are  only  delivered  between  transac-
       tions.  The  upshot of this is that applications using NOTIFY for real-
       time(1,2,n) signaling should try to keep their transactions short.

       NOTIFY behaves like Unix signals in(1,8) one important respect: if(3,n) the  same
       notification  name  is  signaled  multiple  times  in(1,8) quick succession,
       recipients may get only one notification event for  several  executions
       of NOTIFY. So it is a bad idea to depend on the number of notifications
       received. Instead, use NOTIFY to wake up applications that need to  pay
       attention  to something, and use a database object (such as a sequence)
       to keep track of what happened or how many times it happened.

       It is common for a client that executes NOTIFY to be listening  on  the
       same  notification name itself. In that case it will get back a notifi-
       cation event, just like all the other listening sessions. Depending  on
       the  application logic, this could result in(1,8) useless work, for example,
       reading a database table to find the same  updates  that  that  session
       just  wrote  out.  It  is possible to avoid such extra work by noticing
       whether the notifying session's server process  PID  (supplied  in(1,8)  the
       notification  event  message)  is  the  same as one's own session's PID
       (available from libpq). When they are the same, the notification  event
       is  one's own work bouncing back, and can be ignored. (Despite what was
       said in(1,8) the preceding paragraph, this is a safe technique.   PostgreSQL
       keeps  self-notifications  separate  from  notifications  arriving from
       other sessions, so you cannot miss an outside notification by  ignoring
       your own notifications.)

       name   Name of the notification to be signaled (any identifier).

       Configure and execute a listen(1,2,7)/notify sequence from psql:

       LISTEN virtual(5,8);
       NOTIFY virtual(5,8);
       Asynchronous notification "virtual(5,8)" received from server process with PID 8448.

       There is no NOTIFY statement in(1,8) the SQL standard.

       LISTEN [listen(1,2,7)(7)], UNLISTEN [unlisten(l)]

SQL - Language Statements         2003-11-02                         NOTIFY(7)

References for this manual (incoming links)