postgresql_16:5.1.14.1 Event Types
PostgreSQL 16 > Client Interfaces > libpq — C Library > Event System > Event Types
The enum PGEventId names the types of events handled by the event system. All its values have names beginning with PGEVT. For each event type, there is a corresponding event info structure that carries the parameters passed to the event handlers. The event types are:
PGEVT_REGISTER
The register event occurs when (see libpq-PQregisterEventProc) is called. It is the ideal time to initialize any instanceData an event procedure may need. Only one register event will be fired per event handler per connection. If the event procedure fails (returns zero), the registration is cancelled. typedef struct { PGconn *conn; } PGEventRegister; When a PGEVT_REGISTER event is received, the evtInfo pointer should be cast to a PGEventRegister *. This structure contains a PGconn that should be in the CONNECTION_OK status; guaranteed if one calls (see libpq-PQregisterEventProc) right after obtaining a good PGconn. When returning a failure code, all cleanup must be performed as no PGEVT_CONNDESTROY event will be sent.
PGEVT_CONNRESET
The connection reset event is fired on completion of (see libpq-PQreset) or PQresetPoll. In both cases, the event is only fired if the reset was successful. The return value of the event procedure is ignored in PostgreSQL v15 and later. With earlier versions, however, it's important to return success (nonzero) or the connection will be aborted. typedef struct { PGconn *conn; } PGEventConnReset; When a PGEVT_CONNRESET event is received, the evtInfo pointer should be cast to a PGEventConnReset *. Although the contained PGconn was just reset, all event data remains unchanged. This event should be used to reset/reload/requery any associated instanceData. Note that even if the event procedure fails to process PGEVT_CONNRESET, it will still receive a PGEVT_CONNDESTROY event when the connection is closed.
PGEVT_CONNDESTROY
The connection destroy event is fired in response to (see libpq-PQfinish). It is the event procedure's responsibility to properly clean up its event data as libpq has no ability to manage this memory. Failure to clean up will lead to memory leaks. typedef struct { PGconn *conn; } PGEventConnDestroy; When a PGEVT_CONNDESTROY event is received, the evtInfo pointer should be cast to a PGEventConnDestroy *. This event is fired prior to (see libpq-PQfinish) performing any other cleanup. The return value of the event procedure is ignored since there is no way of indicating a failure from (see libpq-PQfinish). Also, an event procedure failure should not abort the process of cleaning up unwanted memory.
PGEVT_RESULTCREATE
The result creation event is fired in response to any query execution function that generates a result, including (see libpq-PQgetResult). This event will only be fired after the result has been created successfully. typedef struct { PGconn *conn; PGresult *result; } PGEventResultCreate; When a PGEVT_RESULTCREATE event is received, the evtInfo pointer should be cast to a PGEventResultCreate *. The conn is the connection used to generate the result. This is the ideal place to initialize any instanceData that needs to be associated with the result. If an event procedure fails (returns zero), that event procedure will be ignored for the remaining lifetime of the result; that is, it will not receive PGEVT_RESULTCOPY or PGEVT_RESULTDESTROY events for this result or results copied from it.
PGEVT_RESULTCOPY
The result copy event is fired in response to (see libpq-PQcopyResult). This event will only be fired after the copy is complete. Only event procedures that have successfully handled the PGEVT_RESULTCREATE or PGEVT_RESULTCOPY event for the source result will receive PGEVT_RESULTCOPY events. typedef struct { const PGresult *src; PGresult *dest; } PGEventResultCopy; When a PGEVT_RESULTCOPY event is received, the evtInfo pointer should be cast to a PGEventResultCopy *. The src result is what was copied while the dest result is the copy destination. This event can be used to provide a deep copy of instanceData, since PQcopyResult cannot do that. If an event procedure fails (returns zero), that event procedure will be ignored for the remaining lifetime of the new result; that is, it will not receive PGEVT_RESULTCOPY or PGEVT_RESULTDESTROY events for that result or results copied from it.
PGEVT_RESULTDESTROY
The result destroy event is fired in response to a (see libpq-PQclear). It is the event procedure's responsibility to properly clean up its event data as libpq has no ability to manage this memory. Failure to clean up will lead to memory leaks. typedef struct { PGresult *result; } PGEventResultDestroy; When a PGEVT_RESULTDESTROY event is received, the evtInfo pointer should be cast to a PGEventResultDestroy *. This event is fired prior to (see libpq-PQclear) performing any other cleanup. The return value of the event procedure is ignored since there is no way of indicating a failure from (see libpq-PQclear). Also, an event procedure failure should not abort the process of cleaning up unwanted memory.