Skip to content

Proposal: Add Notice Processor Support to sqlgen #118

@mcraveiro

Description

@mcraveiro

Hi sqlgen developers,

Thanks very much for a great library. This is a summary of a problem I faced whilst developing with Claude code. When I get a moment I will have a go at implementing this feature, but if you have any feedback it would be appreciated.

Cheers

AI Generated summary starts here (with minor massaging by me).

Problem

PostgreSQL NOTICE messages (from RAISE NOTICE in PL/pgSQL functions) go directly to stderr via libpq's default notice processor. Applications using sqlgen cannot capture or redirect these messages to their own logging infrastructure.

Technical Details

  • libpq provides PQsetNoticeProcessor(PGconn*, callback, void*) to customize notice handling
  • sqlgen's PostgresV2Connection wraps PGconn* but doesn't set a notice processor
  • sqlgen's ConnectionPool creates connections upfront but doesn't expose them
  • sqlgen's Session holds the connection privately with no accessor

Current sqlgen Architecture:

ConnectionPool<Connection>
└── vector<pair<Ref<Connection>, atomic_flag>>                                               
      └── postgres::Connection
             └── PostgresV2Connection
                    └── PGconn* (accessible via ptr())

Proposed Solution

Add an optional notice callback to ConnectionPoolConfig or postgres::Credentials:

  • Option 1: Callback in pool config:
struct ConnectionPoolConfig {
    size_t size = 4;
    size_t num_attempts = 10;
    size_t wait_time_in_seconds = 1;
    std::function<void(const char*)> notice_handler;  // NEW
};
  • Option 2: Callback in postgres::Credentials or Connection constructor:

In PostgresV2Connection::make(), after PQconnectdb(), call:

if (notice_handler) {
    PQsetNoticeProcessor(conn, [](void* arg, const char* msg) {
        auto* handler = static_cast<std::function<void(const char*)>*>(arg);
        (*handler)(msg);
    }, &notice_handler);
}

Use Case

ORE Studio [1] test infrastructure provisions/deprovisions tenants using PostgreSQL functions that emit NOTICE messages. We want to capture these in boost::log instead of stderr.

[1] OreStudio GitHub Project

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions