#[cfg(unix)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let conninfo = std::env::args()
.nth(1)
.unwrap_or_else(|| "dbname = postgres".to_string());
let conn = libpq::Connection::new(&conninfo)?;
let res = conn.exec("SELECT pg_catalog.set_config('search_path', '', false)");
if res.status() != libpq::Status::TuplesOk {
panic!("SET failed: {:?}", conn.error_message());
}
let res = conn.exec("LISTEN TBL2");
if res.status() != libpq::Status::CommandOk {
panic!("LISTEN command failed: {:?}", conn.error_message());
}
let mut nnotifies = 0;
let sock = conn.socket()?;
let mut poll = mio::Poll::new()?;
let mut events = mio::Events::with_capacity(1);
poll.registry().register(
&mut mio::unix::SourceFd(&sock),
mio::Token(0),
mio::Interest::READABLE,
)?;
while nnotifies < 4 {
poll.poll(&mut events, None)?;
conn.consume_input()?;
while let Some(notify) = conn.notifies() {
eprintln!(
"ASYNC NOTIFY of '{}' received from backend PID {}",
notify.relname()?,
notify.be_pid()
);
nnotifies += 1;
conn.consume_input()?;
}
}
eprintln!("Done.");
Ok(())
}
#[cfg(not(unix))]
fn main() {}