| The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide
by The PostgreSQL Global Development Group Paperback (6"x9"), 478 pages ISBN 9781906966065 RRP £14.95 ($19.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
1.12.1 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
PQregisterEventProcis called. It is the ideal time to initialize anyinstanceDataan event procedure may need. Only one register event will be fired per event handler per connection. If the event procedure fails, the registration is aborted.typedef struct { PGconn *conn; } PGEventRegister;When aPGEVT_REGISTERevent is received, theevtInfopointer should be cast to aPGEventRegister *. This structure contains aPGconnthat should be in theCONNECTION_OKstatus; guaranteed if one callsPQregisterEventProcright after obtaining a goodPGconn. When returning a failure code, all cleanup must be performed as noPGEVT_CONNDESTROYevent will be sent. PGEVT_CONNRESET-
The connection reset event is fired on completion of
PQresetorPQresetPoll. In both cases, the event is only fired if the reset was successful. If the event procedure fails, the entire connection reset will fail; thePGconnis put intoCONNECTION_BADstatus andPQresetPollwill returnPGRES_POLLING_FAILED.typedef struct { PGconn *conn; } PGEventConnReset;When aPGEVT_CONNRESETevent is received, theevtInfopointer should be cast to aPGEventConnReset *. Although the containedPGconnwas just reset, all event data remains unchanged. This event should be used to reset/reload/requery any associatedinstanceData. Note that even if the event procedure fails to processPGEVT_CONNRESET, it will still receive aPGEVT_CONNDESTROYevent when the connection is closed. PGEVT_CONNDESTROY-
The connection destroy event is fired in response to
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 aPGEVT_CONNDESTROYevent is received, theevtInfopointer should be cast to aPGEventConnDestroy *. This event is fired prior toPQfinishperforming any other cleanup. The return value of the event procedure is ignored since there is no way of indicating a failure fromPQfinish. 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
PQgetResult. This event will only be fired after the result has been created successfully.typedef struct { PGconn *conn; PGresult *result; } PGEventResultCreate;When aPGEVT_RESULTCREATEevent is received, theevtInfopointer should be cast to aPGEventResultCreate *. Theconnis the connection used to generate the result. This is the ideal place to initialize anyinstanceDatathat needs to be associated with the result. If the event procedure fails, the result will be cleared and the failure will be propagated. The event procedure must not try toPQclearthe result object for itself. When returning a failure code, all cleanup must be performed as noPGEVT_RESULTDESTROYevent will be sent. PGEVT_RESULTCOPY-
The result copy event is fired in response to
PQcopyResult. This event will only be fired after the copy is complete. Only event procedures that have successfully handled thePGEVT_RESULTCREATEorPGEVT_RESULTCOPYevent for the source result will receivePGEVT_RESULTCOPYevents.typedef struct { const PGresult *src; PGresult *dest; } PGEventResultCopy;When aPGEVT_RESULTCOPYevent is received, theevtInfopointer should be cast to aPGEventResultCopy *. Thesrcresult is what was copied while thedestresult is the copy destination. This event can be used to provide a deep copy ofinstanceData, sincePQcopyResultcannot do that. If the event procedure fails, the entire copy operation will fail and thedestresult will be cleared. When returning a failure code, all cleanup must be performed as noPGEVT_RESULTDESTROYevent will be sent for the destination result. PGEVT_RESULTDESTROY-
The result destroy event is fired in response to a
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 aPGEVT_RESULTDESTROYevent is received, theevtInfopointer should be cast to aPGEventResultDestroy *. This event is fired prior toPQclearperforming any other cleanup. The return value of the event procedure is ignored since there is no way of indicating a failure fromPQclear. Also, an event procedure failure should not abort the process of cleaning up unwanted memory.
| ISBN 9781906966065 | The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide | See the print edition |