Native PostgreSQL driver / client for Zig
0

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'master' into dev

Karl Seguin (Aug 19, 2025, 8:37 AM +0800) 032cc0a4 6a164b28

+25 -6
+17 -3
readme.md
··· 337 337 For most queries, you should use the `conn.query(...)`, `conn.row(...)` or `conn.exec(...)` methods. For queries with parameters, these methods look like: 338 338 339 339 ```zig 340 - var stmt = try Stmt.init(conn, opts) 340 + var stmt = try Stmt.init(conn, opts); 341 341 errdefer stmt.deinit(); 342 342 343 343 try stmt.prepare(sql, null); ··· 518 518 519 519 When reading a `JSON` or `JSONB` column with `[]u8`, the serialized JSON will be returned. 520 520 521 + ### PgLSN, xid8, xid 522 + PgLSN and xid8 can be bound and read as i64. 523 + 524 + xid can be bound and read as i32. 525 + 521 526 ## Listen / Notify 522 527 You can create a `pg.Listener` either from an existing `Pool` or directly. 523 528 ··· 538 543 }); 539 544 540 545 // add 1 or more channels to listen to 541 - try listener.listen("chan_1"); 542 - try listener.listen("chan_2"); 546 + try listener.listen("chan_1", .{}); 547 + try listener.listen("chan_2", .{}); 543 548 544 549 // .next() blocks until there's a notification or an error 545 550 while (listener.next()) |notification| { ··· 564 569 565 570 // same as above 566 571 ``` 572 + 573 + ### Listen Timeout 574 + When calling `listen`, a timeout in milliseconds can be specified: 575 + 576 + ```zig 577 + try listener.listen("chan_1", .{}); 578 + ``` 579 + 580 + If multiple calls to `listen` are made, the last timeout will be used. If no message is received in `timeout` milliseconds, `next()` will return `null` and `listener.err.?.err == error.WouldBlock`. 567 581 568 582 ## Reconnects 569 583 A listener will not automatically reconnect on error/disconnect. The pub/sub nature of LISTEN/NOTIFY mean that delivery is at-most-once and auto-reconnecting can hide that fact. Put the above code in a `while (true) {...}` loop.
+8 -3
src/listener.zig
··· 78 78 } 79 79 } 80 80 81 - pub fn listen(self: *Listener, channel: []const u8) !void { 81 + const ListenOpts = struct { 82 + timeout: u32 = 0, 83 + }; 84 + pub fn listen(self: *Listener, channel: []const u8, opts: ListenOpts) !void { 82 85 // LISTEN doesn't support parameterized queries. It has to be a simple query. 83 86 // We don't use proto.Query because we want to quote the identifier. 84 87 ··· 135 138 else => return error.UnexpectedDBMessage, 136 139 } 137 140 } 141 + 142 + try self._reader.startFlow(null, opts.timeout); 138 143 } 139 144 140 145 pub fn next(self: *Listener) ?NotificationResponse { ··· 209 214 } 210 215 211 216 fn testListener(l: *Listener) !void { 212 - try l.listen("chan-1"); 213 - try l.listen("chan_2"); 217 + try l.listen("chan-1", .{}); 218 + try l.listen("chan_2", .{}); 214 219 215 220 const thrd = try std.Thread.spawn(.{}, testNotifier, .{}); 216 221 {