Search code examples
clibpq

PQsendQueryPrepared - send values and memory management


I want to send binary data in an asynchronous way to PostgreSQL via libpg's method

int PQsendQueryPrepared(PGconn *conn,
                        const char *stmtName,
                        int nParams,
                        const char * const *paramValues,
                        const int *paramLengths,
                        const int *paramFormats,
                        int resultFormat);

However, I was not able to find any information about the memory management of passed parameters.

I run this command, and the code continues in a non-blocking manner. When can I free (or rewrite) paramValues data? Do I need to wait until PQgetResult returns NULL or is data internally copied by libpg?


Solution

  • PQsendQueryPrepared() calls PQsendQueryGuts() which in turn serializes paramValues with pqPutnchar() to pqPutMsgBytes() to memcpy(). To me it looks like you are free to do what you want with arguments once the function returns.

    While PQsendQueryPrepared() starts the transfer it may not be done till you call PQgetResult() which does an implicit flush.