EPUB | CHM | PDF

TPSQLNotify

Top Previous Next

TPSQLNotify provides the interface to PostgreSQL Asynchronous Notification functionalities. To learn how does PostgreSQL Asynchronous Notification work, please see Asynchronous Notification section of PostgreSQL documentation.

Use TPSQLNotify to organize notifications between client applications about their actions through Postgres database server.

To use TPSQLNotify you should do the following:

1. Create a rule for table on which changes you want to be notified:

create rule InsertDetect as on INSERT to notify_test do notify recinsert

2. Add recinsert to ListenList property of TPSQLNotify component;

3. Write event handler for OnNotify event:

procedure TForm1.PSQLNotify1Notify(Sender: TObject; Event: String; Pid : Integer);
begin
   if PID <> DB.GetBackendPID then  // If it is not me...
      Memo1.Lines.Add(
      Format('Client (PID=%d) just modified (event = %s) table notify_test. Press Refresh',
      [PID, Event]));
end;

4. Link Database property of TPSQLNotify component to PSQLDatabase component;

5. To begin listen notifications, set Active property of TPSQLNotify to True.

After compilation you can see rows insertions in Memo1 component when other clients make inserts in notify_test table. Also you can do the same for DELETE and UPDATE events.

Please note, that TPSQLNotify component is not thread-safe because of its internal implementation.

See also: Properties, Methods, Events