{
    "mode": "man",
    "parameter": "NOTIFY",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/NOTIFY/7/json",
    "generated": "2026-10-04T16:21:48Z",
    "synopsis": "NOTIFY channel [ , payload ]",
    "sections": {
        "NAME": {
            "content": "NOTIFY - generate a notification\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "NOTIFY channel [ , payload ]\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The NOTIFY command sends a notification event together with an optional “payload” string to\neach client application that has previously executed LISTEN channel for the specified channel\nname in the current database. Notifications are visible to all users.\n\nNOTIFY provides a simple interprocess communication mechanism for a collection of processes\naccessing the same PostgreSQL database. A payload string can be sent along with the\nnotification, and higher-level mechanisms for passing structured data can be built by using\ntables in the database to pass additional data from notifier to listener(s).\n\nThe information passed to the client for a notification event includes the notification\nchannel name, the notifying session's server process PID, and the payload string, which is an\nempty string if it has not been specified.\n\nIt is up to the database designer to define the channel names that will be used in a given\ndatabase and what each one means. Commonly, the channel name is the same as the name of some\ntable in the database, and the notify event essentially means, “I changed this table, take a\nlook at it to see what's new”. But no such association is enforced by the NOTIFY and LISTEN\ncommands. For example, a database designer could use several different channel names to\nsignal different sorts of changes to a single table. Alternatively, the payload string could\nbe used to differentiate various cases.\n\nWhen NOTIFY is used to signal the occurrence of changes to a particular table, a useful\nprogramming technique is to put the NOTIFY in a statement trigger that is triggered by table\nupdates. In this way, notification happens automatically when the table is changed, and the\napplication programmer cannot accidentally forget to do it.\n\nNOTIFY interacts with SQL transactions in some important ways. Firstly, if a NOTIFY is\nexecuted inside a transaction, the notify events are not delivered until and unless the\ntransaction is committed. This is appropriate, since if the transaction is aborted, all the\ncommands within it have had no effect, including NOTIFY. But it can be disconcerting if one\nis expecting the notification events to be delivered immediately. Secondly, if a listening\nsession receives a notification signal while it is within a transaction, the notification\nevent will not be delivered to its connected client until just after the transaction is\ncompleted (either committed or aborted). Again, the reasoning is that if a notification were\ndelivered within a transaction that was later aborted, one would want the notification to be\nundone somehow — but the server cannot “take back” a notification once it has sent it to the\nclient. So notification events are only delivered between transactions. The upshot of this is\nthat applications using NOTIFY for real-time signaling should try to keep their transactions\nshort.\n\nIf the same channel name is signaled multiple times with identical payload strings within the\nsame transaction, only one instance of the notification event is delivered to listeners. On\nthe other hand, notifications with distinct payload strings will always be delivered as\ndistinct notifications. Similarly, notifications from different transactions will never get\nfolded into one notification. Except for dropping later instances of duplicate notifications,\nNOTIFY guarantees that notifications from the same transaction get delivered in the order\nthey were sent. It is also guaranteed that messages from different transactions are delivered\nin the order in which the transactions committed.\n\nIt is common for a client that executes NOTIFY to be listening on the same notification\nchannel itself. In that case it will get back a notification event, just like all the other\nlistening sessions. Depending on the application logic, this could result in useless work,\nfor example, reading a database table to find the same updates that that session just wrote\nout. It is possible to avoid such extra work by noticing whether the notifying session's\nserver process PID (supplied in the notification event message) is the same as one's own\nsession's PID (available from libpq). When they are the same, the notification event is one's\nown work bouncing back, and can be ignored.\n",
            "subsections": []
        },
        "PARAMETERS": {
            "content": "channel\nName of the notification channel to be signaled (any identifier).\n\npayload\nThe “payload” string to be communicated along with the notification. This must be\nspecified as a simple string literal. In the default configuration it must be shorter\nthan 8000 bytes. (If binary data or large amounts of information need to be communicated,\nit's best to put it in a database table and send the key of the record.)\n",
            "subsections": []
        },
        "NOTES": {
            "content": "There is a queue that holds notifications that have been sent but not yet processed by all\nlistening sessions. If this queue becomes full, transactions calling NOTIFY will fail at\ncommit. The queue is quite large (8GB in a standard installation) and should be sufficiently\nsized for almost every use case. However, no cleanup can take place if a session executes\nLISTEN and then enters a transaction for a very long time. Once the queue is half full you\nwill see warnings in the log file pointing you to the session that is preventing cleanup. In\nthis case you should make sure that this session ends its current transaction so that cleanup\ncan proceed.\n\nThe function pgnotificationqueueusage returns the fraction of the queue that is currently\noccupied by pending notifications. See Section 9.26 for more information.\n\nA transaction that has executed NOTIFY cannot be prepared for two-phase commit.\n",
            "subsections": [
                {
                    "name": "pg_notify",
                    "content": "To send a notification you can also use the function pgnotify(text, text). The function\ntakes the channel name as the first argument and the payload as the second. The function is\nmuch easier to use than the NOTIFY command if you need to work with non-constant channel\nnames and payloads.\n"
                }
            ]
        },
        "EXAMPLES": {
            "content": "Configure and execute a listen/notify sequence from psql:\n\nLISTEN virtual;\nNOTIFY virtual;\nAsynchronous notification \"virtual\" received from server process with PID 8448.\nNOTIFY virtual, 'This is the payload';\nAsynchronous notification \"virtual\" with payload \"This is the payload\" received from server process with PID 8448.\n\nLISTEN foo;\nSELECT pgnotify('fo' || 'o', 'pay' || 'load');\nAsynchronous notification \"foo\" with payload \"payload\" received from server process with PID 14728.\n",
            "subsections": []
        },
        "COMPATIBILITY": {
            "content": "There is no NOTIFY statement in the SQL standard.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "LISTEN(7), UNLISTEN(7)\n\nPostgreSQL 16.14                                2026                                       NOTIFY(7)",
            "subsections": []
        }
    },
    "summary": "NOTIFY - generate a notification",
    "flags": [],
    "examples": [
        "Configure and execute a listen/notify sequence from psql:",
        "LISTEN virtual;",
        "NOTIFY virtual;",
        "Asynchronous notification \"virtual\" received from server process with PID 8448.",
        "NOTIFY virtual, 'This is the payload';",
        "Asynchronous notification \"virtual\" with payload \"This is the payload\" received from server process with PID 8448.",
        "LISTEN foo;",
        "SELECT pgnotify('fo' || 'o', 'pay' || 'load');",
        "Asynchronous notification \"foo\" with payload \"payload\" received from server process with PID 14728."
    ],
    "see_also": [
        {
            "name": "LISTEN",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/LISTEN/7/json"
        },
        {
            "name": "UNLISTEN",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/UNLISTEN/7/json"
        }
    ]
}