Skip to content

feat: add DBI non-blocking async core support - #224

Draft
cjac wants to merge 6 commits into
bucardo:masterfrom
LLC-Technologies-Collier:feature/dbi-non-blocking-core
Draft

cjac wants to merge 6 commits into
bucardo:masterfrom
LLC-Technologies-Collier:feature/dbi-non-blocking-core

Conversation

@cjac

@cjac cjac commented Dec 2, 2026 •

Copy link
Copy Markdown

Add DBI Non-Blocking Async Core Support (perl5-dbi/dbi#185)

Overview

This pull request bridges DBD::Pg's existing libpq asynchronous execution plumbing (PG_ASYNC, pg_ready, pg_result, pg_socket) to the new DBI core non-blocking asynchronous interface proposed in perl5-dbi/dbi#185 (documented in DBI::DBD under ASYNCHRONOUS EXECUTION SUPPORT).

All new code is gated on #ifdef DBIcf_Async, so DBD::Pg remains 100% source- and binary-compatible with older DBI releases.


Summary of Changes

  1. Internal Driver Readiness Hooks (Pg.xs)

    • Implemented internal _async_read_ready() and _async_write_ready() on both DBD::Pg::db and DBD::Pg::st under #ifdef DBIcf_Async (wrapped by DBI core into $on_ready_cb for DBI::EventLoop backends).
    • _async_read_ready() polls pg_db_ready(); if still busy (0), returns 0 without clearing flags; once complete (> 0), clears AsyncWantRead/AsyncWantWrite via DBIh_ASYNC_CLEAR_WANT, emits on_remove via DBIh_ASYNC_NOTIFY_WATCHER, and finalizes the result via pg_db_result() (returning undef strictly on error).
    • _async_write_ready() drives PQflush() and clears AsyncWantWrite once the write buffer drains.
  2. Non-Blocking execute() & Mid-Flight Teardown (dbdimp.c, dbdimp.h)

    • In dbd_st_execute(), when DBIc_ASYNC(imp_sth) is active, enables PG_ASYNC so all PQsendQuery / PQsendQueryParams / PQsendQueryPrepared paths run non-blocking.
    • On early async return, sets AsyncWantRead (and AsyncWantWrite if PQflush() returns 1), leaves $h->err untouched (zero $h->err overloading; callers inspect DBI::is_wouldblock($sth)), and notifies DBI's active DBI::EventLoop watcher (on_add) via DBIh_ASYNC_NOTIFY_WATCHER with PQsocket(conn).
    • Mid-flight teardown safety: dbd_db_disconnect() and dbd_st_finish() emit on_remove and clear AsyncWantRead/AsyncWantWrite before closing sockets or discarding pending results.
  3. Integration Test Suite (t/09async_dbi.t)

    • Verifies $dbh->{Async} = 1 capability acceptance and inheritance onto prepared statement handles.
    • Verifies non-blocking $sth->execute() returns '0E0', sets AsyncWantRead (DBI::is_wouldblock($sth) true, $sth->err untouched), and fires on_add with $dbh->{pg_socket} and $on_ready_cb.
    • Verifies readiness completion clears AsyncWantRead, emits on_remove, and $sth->fetch_async_row() returns the expected result row.
    • Automatically skips when built/run against a DBI without non-blocking core support (!defined &DBI::is_wouldblock).

Testing

Tested against a live postgres:17-alpine container using perl5-dbi/dbi#185 (feature/non-blocking-core):

  • t/08async.t: 127/127 passed (zero regressions on existing PG_ASYNC behavior)
  • t/09async_dbi.t: 14/14 passed

@cjac
cjac force-pushed the feature/dbi-non-blocking-core branch from 3a29e36 to 5baccdd Compare December 2, 2026 19:59
@cjac
cjac force-pushed the feature/dbi-non-blocking-core branch from 5baccdd to c5eccd6 Compare December 2, 2026 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant