Postgres module

Listener

structsince 0.27.0Source

A dedicated, self-healing LISTEN on a pool's database. listen(pool, "jobs") takes a connection out of the pool for as long as the listener lives, LISTENs on it, and reads notifications on a fiber of its own into a queue. If the connection fails (the server restarts, the backend is terminated, the network drops) the listener returns it to the pool, takes another, LISTENs again and carries on. Postgres keeps no notifications for a session that is not connected, so those sent while the listener is reconnecting are lost; code that must not miss any should read its table again after reconnects changes.

Example
listener, error := Pg.listen(pool, "jobs", "mail");
while true {
    n, error := Pg.receive(listener);
    if error.kind != .NONE break;
    defer Pg.free_notification(*n);
    print("% on %\n", n.payload, n.channel);
}
Pg.close_listener(listener);

Not run: the example is marked ignore.

Not run: it needs a PostgreSQL server.

Fields

queue: [..] Notification
Notifications received and not yet taken, oldest first (receive). The reader waits when LISTENER_QUEUE of them are queued.
arrived: Io.Condition
closed: bool
no more will arrive
reconnects: s64
How many times the connection was replaced after a failure.
pool: *Pool
channels: [..] string
connection: *Connection
closing: bool
finished: bool

See also