notify.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 109 行

7
109
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "NOTIFY" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMENOTIFY \- generate a notification.SH SYNOPSIS.sp.nfNOTIFY \fIname\fR        .sp.fi.SH "DESCRIPTION".PPThe \fBNOTIFY\fR command sends a notification event to eachclient application that has previously executed\fBLISTEN \fIname\fB\fRfor the specified notification name in the current database..PP\fBNOTIFY\fR provides a simple form of signal orinterprocess communication mechanism for a collection of processesaccessing the same PostgreSQL database.Higher-level mechanisms can be built by using tables in the database topass additional data (beyond a mere notification name) from notifier tolistener(s)..PPThe information passed to the client for a notification event includes the notificationname and the notifying session's server process PID. It is up to thedatabase designer to define the notification names that will be used in a givendatabase and what each one means..PPCommonly, the notification name is the same as the name of some table inthe database, and the notify event essentially means, ``I changed this table,take a look at it to see what's new''. But no such association is enforced bythe \fBNOTIFY\fR and \fBLISTEN\fR commands. Forexample, a database designer could use several different notification namesto signal different sorts of changes to a single table..PPWhen \fBNOTIFY\fR is used to signal the occurrence of changesto a particular table, a useful programming technique is to put the\fBNOTIFY\fR in 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..PP\fBNOTIFY\fR interacts with SQL transactions in some importantways. Firstly, if a \fBNOTIFY\fR is executed inside atransaction, the notify events are not delivered until and unless thetransaction is committed. This is appropriate, since if the transactionis aborted, all the commands within it have had noeffect, including \fBNOTIFY\fR. But it can be disconcerting if oneis expecting the notification events to be delivered immediately. Secondly, ifa listening session receives a notification signal while it is within a transaction,the notification event will not be delivered to its connected client until justafter the transaction is completed (either committed or aborted). Again, thereasoning is that if a notification were delivered within a transaction that waslater aborted, one would want the notification to be undone somehow \(embutthe server cannot ``take back'' a notification once it has sent it to the client.So notification events are only delivered between transactions. The upshot of thisis that applications using \fBNOTIFY\fR for real-time signalingshould try to keep their transactions short..PP\fBNOTIFY\fR behaves like Unix signals in one importantrespect: if the same notification name is signaled multiple times in quicksuccession, recipients may get only one notification event for several executionsof \fBNOTIFY\fR. So it is a bad idea to depend on the numberof notifications received. Instead, use \fBNOTIFY\fR to wake upapplications that need to pay attention to something, and use a databaseobject (such as a sequence) to keep track of what happened or how many timesit happened..PPIt is common for a client that executes \fBNOTIFY\fRto be listening on the same notification name itself. In that caseit will get back a notification event, just like all the otherlistening sessions. Depending on the application logic, this couldresult in useless work, for example, reading a database table tofind the same updates that that session just wrote out. It ispossible to avoid such extra work by noticing whether the notifyingsession's server process PID (supplied in thenotification event message) is the same as one's own session'sPID (available from \fBlibpq\fR). When theyare the same, the notification event is one's own work bouncingback, and can be ignored. (Despite what was said in the precedingparagraph, this is a safe technique.PostgreSQL keeps self-notificationsseparate from notifications arriving from other sessions, so youcannot miss an outside notification by ignoring your ownnotifications.).SH "PARAMETERS".TP\fB\fIname\fB\fRName of the notification to be signaled (any identifier)..SH "EXAMPLES".PPConfigure and execute a listen/notify sequence from\fBpsql\fR:.sp.nfLISTEN virtual;NOTIFY virtual;Asynchronous notification "virtual" received from server process with PID 8448..sp.fi.SH "COMPATIBILITY".PPThere is no \fBNOTIFY\fR statement in the SQLstandard..SH "SEE ALSO"LISTEN [\fBlisten\fR(7)], UNLISTEN [\fBunlisten\fR(l)]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?