dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlDependency Class
Members Example

In This Topic
    PgSqlDependency Class
    In This Topic
    Tracks changes on the server.
    Syntax
    'Declaration
     
    Public Class PgSqlDependency 
       Implements System.IDisposable 
    public class PgSqlDependency : System.IDisposable  
    Remarks

    The PgSqlDependency class is designed to watch for changes in tables on the server. This is accomplished using LISTEN ... NOTIFY server side mechanism and recurring check requests.

    To start watching for a table in a schema perform the following steps:

    1. Create a stored function on the server. This function should issue NOTIFY "table_changed_schemaname.tablename" command, where schemaname and tablename represent names of the schema and the table you want to watch.
    2. Create a trigger that executes the stored function every time the table is altered.
    3. Create a PgSqlCommand object with CommandText property set to SQL statement like SELECT * FROM schemaname.tablename.
    4. Create a PgSqlDependency object and connect it to the PgSqlCommand object (for example, with the AddCommandDependency method).
    5. Assign a handler to the OnChange event.
    6. Prepare a PgSqlConnection object (or a connection string) to use by the PgSqlDependency object and call the Start method.

    Once you perform these steps the the PgSqlDependency object will poll the server periodically, and if the table is modified since the last call, it will invoke your event handler.

    Note that you can use single PgSqlDependency instance to watch for several tables. To do this, specify all necessary tables in the FROM clause of command's SQL statement.

    Note also that PgSqlDependency can watch for whole tables only. If you restrict the SELECT statement with WHERE clause, it will not affect notifications.

    In PostgreSQL 9.0 and higher, you can also get the infiormation about the table change, that is passed as a second parameter of the NOTIFY command.

    This class is not available in Mobile Edition.

    Example
    This sample demonstrates using PgSqlDependency object to watch for modifications in the table test.dept and operations performed. It requires on PostgreSQL 9.0 or highter. It is assumed that you have the following objects created on the server:
    CREATE OR REPLACE FUNCTION test.note_dept()
      RETURNS TRIGGER AS 
      BEGIN
      PERFORM pg_notify('table_changed_test.dept', TG_OP);
      RETURN NULL;
      END;
     LANGUAGE plpgsql;
    
    
    CREATE TRIGGER notify_dept AFTER INSERT OR UPDATE OR DELETE
      ON test.dept
      EXECUTE PROCEDURE test.note_dept();
    
    PgSqlConnection connection = new PgSqlConnection("User Id = postgres; Password = postgres; host = localhost; Port = 5432; database = testdb;");
    PgSqlDependency depend = new PgSqlDependency();
    depend.OnChange += new OnChangeEventHandler(dependency_OnChange);
    PgSqlCommand selectCommand = new PgSqlCommand("SELECT * FROM test.dept", connection);
    depend.AddCommandDependency(selectCommand);
    depend.CheckTimeout = 500;
    connection.Open();
    try {
      depend.Start(connection);
    
      //different actions with data and check correct depend work
      PgSqlCommand commandInsert = new PgSqlCommand("INSERT INTO test.dept VALUES (111, 'test', 'test');", connection);
      PgSqlCommand commandUpdate = new PgSqlCommand("UPDATE test.dept SET dname = 'test' WHERE deptno = 10;", connection);
      PgSqlCommand commandDelete = new PgSqlCommand("DELETE FROM test.dept WHERE dname = 'test';", connection);
      commandInsert.ExecuteNonQuery();
      System.Threading.Thread.Sleep(600);
      commandUpdate.ExecuteNonQuery();
      System.Threading.Thread.Sleep(600);
      commandDelete.ExecuteNonQuery();
      System.Threading.Thread.Sleep(600);
    }
    finally {
      depend.Stop();
      connection.Close();
       }
    
    ...
    
    private void dependency_OnChange(object sender, PgSqlTableChangeEventArgs e) {
    
      MessageBox.Show("Table changed: " + e.TableName + "; Operation: " + e.Info);
    }
    Dim connection As New PgSqlConnection("User Id = postgres; Password = postgres; host = localhost; Port = 5432; database = postgres;")
    Dim depend As New PgSqlDependency
    AddHandler depend.OnChange, New OnChangeEventHandler(AddressOf Me.dependency_OnChange)
            Dim selectCommand As New PgSqlCommand("SELECT * FROM test.dept", connection)
            depend.AddCommandDependency(selectCommand)
            depend.CheckTimeout = 500
            connection.Open()
            Try
                    depend.Start(connection)
                    Dim commandInsert As New PgSqlCommand("INSERT INTO test.dept VALUES (111, 'test', 'test');", connection)
                    Dim commandUpdate As New PgSqlCommand("UPDATE test.dept SET dname = 'test' WHERE deptno = 10;", connection)
                    Dim commandDelete As New PgSqlCommand("DELETE FROM test.dept WHERE dname = 'test';", connection)
                    commandInsert.ExecuteNonQuery()
                    System.Threading.Thread.Sleep(600)
                    commandUpdate.ExecuteNonQuery()
                    System.Threading.Thread.Sleep(600)
                    commandDelete.ExecuteNonQuery()
                    System.Threading.Thread.Sleep(600)
            Finally
                    depend.Stop()
                    connection.Close()
            End Try
    
            ...
    
            Private Sub dependency_OnChange(ByVal sender As Object, ByVal e As PgSqlTableChangeEventArgs)
    
            MessageBox.Show("Table changed " & e.TableName & "; Operation: " & e.Info)
    End Sub
    Inheritance Hierarchy

    System.Object
       Devart.Data.PostgreSql.PgSqlDependency

    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also