The DBMS_ALERT package lets you register for, send, and receive alerts. The following table lists the supported procedures:
Function/procedure
Return type
Description
REGISTER(name)
n/a
Register to be able to receive alerts named name.
REMOVE(name)
n/a
Remove registration for the alert named name.
REMOVEALL
n/a
Remove registration for all alerts.
SIGNAL(name, message)
n/a
Signal the alert named name with message.
WAITANY(name OUT, message OUT, status OUT, timeout)
n/a
Wait for any registered alert to occur.
WAITONE(name, message OUT, status OUT, timeout)
n/a
Wait for the specified alert, name, to occur.
EDB Postgres Advanced Server's implementation of DBMS_ALERT is a partial implementation when compared to Oracle's version. Only those functions and procedures listed in the table are supported.
EDB Postgres Advanced Server allows a maximum of 500 concurrent alerts. You can use the dbms_alert.max_alerts GUC variable, located in the postgresql.conf file, to specify the maximum number of concurrent alerts allowed on a system.
To set a value for the dbms_alert.max_alerts variable, open the postgresql.conf file, which is located by default in /opt/PostgresPlus/14AS/data, with your choice of editor. Edit the dbms_alert.max_alerts parameter as shown:
alert_count
alert_count specifies the maximum number of concurrent alerts. By default, the value of dbms_alert.max_alerts is 100. To disable this feature, set dbms_alert.max_alerts to 0.
For the dbms_alert.max_alerts GUC to function correctly, the custom_variable_classes parameter must contain dbms_alerts:
After editing the postgresql.conf file parameters, you must restart the server for the changes to take effect.
REGISTER
The REGISTER procedure enables the current session to be notified of the specified alert.
Parameters
name
Name of the alert to register.
Examples
This anonymous block registers for an alert named alert_test and then waits for the signal.
REMOVE
The REMOVE procedure unregisters the session for the named alert.
Parameters
name
Name of the alert to unregister.
REMOVEALL
The REMOVEALL procedure unregisters the session for all alerts.
SIGNAL
The SIGNAL procedure signals the occurrence of the named alert.
Parameters
name
Name of the alert.
message
Information to pass with this alert.
Examples
This anonymous block signals an alert for alert_test.
WAITANY
The WAITANY procedure waits for any of the registered alerts to occur.
Parameters
name
Variable receiving the name of the alert.
message
Variable receiving the message sent by the SIGNAL procedure.
status
Status code returned by the operation. Possible values are: 0 – alert occurred; 1 – timeout occurred.
timeout
Time to wait for an alert in seconds.
Examples
This anonymous block uses the WAITANY procedure to receive an alert named alert_test or any_alert:
An anonymous block in a second session issues a signal for any_alert:
Control returns to the first anonymous block and the remainder of the code is executed:
WAITONE
The WAITONE procedure waits for the specified registered alert to occur.
Parameters
name
Name of the alert.
message
Variable receiving the message sent by the SIGNAL procedure.
status
Status code returned by the operation. Possible values are: 0 – alert occurred; 1 – timeout occurred.
timeout
Time to wait for an alert in seconds.
Examples
This anonymous block is similar to the one used in the WAITANY example except the WAITONE procedure is used to receive the alert named alert_test.
Signal sent for alert_test sent by an anonymous block in a second session:
First session is alerted, control returns to the anonymous block, and the remainder of the code is executed:
Comprehensive example
The following example uses two triggers to send alerts when the dept table or the emp table is changed. An anonymous block listens for these alerts and displays messages when an alert is received.
The following are the triggers on the dept and emp tables:
This anonymous block is executed in a session while updates to the dept and emp tables occur in other sessions:
The following changes are made by user, mary:
The following change is made by user, john:
The following is the output displayed by the anonymous block receiving the signals from the triggers: