I am trying to establish a non-blocking connection to a PostgreSQL database using the libpq library in C, and I want to be able to trigger a notification or callback function once the connection is established.
I know that PQconnectStart() can be used to initiate a non-blocking connection and that PQconnectPoll() can be used to check the status of the connection, but I'm not sure how to trigger a notification or callback function once the connection is established.
PGconn *conn;
PGconnStatusType status;
conn = PQconnectStart("dbname=mydatabase user=myusername password=mypassword");
if (conn == NULL) {
fprintf(stderr, "Connection initialization failed\n");
exit(1);
}
while ((status = PQconnectPoll(conn)) != CONNECTION_OK) {
if (status == PGRES_POLLING_FAILED) {
fprintf(stderr, "Connection failed: %s\n", PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
}
// Connection is established, how do I trigger a notification or callback function?
// Do something with the connection here...
PQfinish(conn);