35.11. Miscellaneous Functions
As always, there are some functions that just don't fit anywhere.
Frees memory allocated by libpq.
void PQfreemem(void *ptr);
Frees memory allocated by libpq, particularly
PQnotifies. It is particularly important that this function, rather than
free(), be used on Microsoft Windows. This is because allocating memory in a DLL and releasing it in the application works only if multithreaded/single-threaded, release/debug, and static/dynamic flags are the same for the DLL and the application. On non-Microsoft Windows platforms, this function is the same as the standard library function
void PQconninfoFree(PQconninfoOption *connOptions);
PQfreememwill not do for this, since the array contains references to subsidiary strings.
Prepares the encrypted form of a Postgres Pro password.
char *PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm);
This function is intended to be used by client applications that wish to send commands like
ALTER USER joe PASSWORD 'pwd'. It is good practice not to send the original cleartext password in such a command, because it might be exposed in command logs, activity displays, and so on. Instead, use this function to convert the password to encrypted form before it is sent.
userarguments are the cleartext password, and the SQL name of the user it is for.
algorithmspecifies the encryption algorithm to use to encrypt the password. Currently supported algorithms are
offare also accepted as aliases for
md5, for compatibility with older server versions). Note that support for
scram-sha-256was introduced in Postgres Pro version 10, and will not work correctly with older server versions. If
NULL, this function will query the server for the current value of the password_encryption setting. That can block, and will fail if the current transaction is aborted, or if the connection is busy executing another query. If you wish to use the default algorithm for the server but want to avoid blocking, query
password_encryptionyourself before calling
PQencryptPasswordConn, and pass that value as the
The return value is a string allocated by
malloc. The caller can assume the string doesn't contain any special characters that would require escaping. Use
PQfreememto free the result when done with it. On error, returns
NULL, and a suitable message is stored in the connection object.
Prepares the md5-encrypted form of a Postgres Pro password.
char *PQencryptPassword(const char *passwd, const char *user);
PQencryptPasswordis an older, deprecated version of
PQencryptPasswordConn. The difference is that
PQencryptPassworddoes not require a connection object, and
md5is always used as the encryption algorithm.
Constructs an empty
PGresultobject with the given status.
PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
This is libpq's internal function to allocate and initialize an empty
PGresultobject. This function returns
NULLif memory could not be allocated. It is exported because some applications find it useful to generate result objects (particularly objects with error status) themselves. If
connis not null and
statusindicates an error, the current error message of the specified connection is copied into the
PGresult. Also, if
connis not null, any event procedures registered in the connection are copied into the
PGresult. (They do not get
PGEVT_RESULTCREATEcalls, but see
PQfireResultCreateEvents.) Note that
PQclearshould eventually be called on the object, just as with a
PGresultreturned by libpq itself.
PGEVT_RESULTCREATEevent (see Section 35.13) for each event procedure registered in the
PGresultobject. Returns non-zero for success, zero if any event procedure fails.
int PQfireResultCreateEvents(PGconn *conn, PGresult *res);
connargument is passed through to event procedures but not used directly. It can be
NULLif the event procedures won't use it.
Event procedures that have already received a
PGEVT_RESULTCOPYevent for this object are not fired again.
The main reason that this function is separate from
PQmakeEmptyPGresultis that it is often appropriate to create a
PGresultand fill it with data before invoking the event procedures.
Makes a copy of a
PGresultobject. The copy is not linked to the source result in any way and
PQclearmust be called when the copy is no longer needed. If the function fails,
PGresult *PQcopyResult(const PGresult *src, int flags);
This is not intended to make an exact copy. The returned result is always put into
PGRES_TUPLES_OKstatus, and does not copy any error message in the source. (It does copy the command status string, however.) The
flagsargument determines what else is copied. It is a bitwise OR of several flags.
PG_COPYRES_ATTRSspecifies copying the source result's attributes (column definitions).
PG_COPYRES_TUPLESspecifies copying the source result's tuples. (This implies copying the attributes, too.)
PG_COPYRES_NOTICEHOOKSspecifies copying the source result's notify hooks.
PG_COPYRES_EVENTSspecifies copying the source result's events. (But any instance data associated with the source is not copied.)
Sets the attributes of a
int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
attDescsare copied into the result. If the
numAttributesis less than one, the request is ignored and the function succeeds. If
resalready contains attributes, the function will fail. If the function fails, the return value is zero. If the function succeeds, the return value is non-zero.
Sets a tuple field value of a
int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
The function will automatically grow the result's internal tuples array as needed. However, the
tup_numargument must be less than or equal to
PQntuples, meaning this function can only grow the tuples array one tuple at a time. But any field of any existing tuple can be modified in any order. If a value at
field_numalready exists, it will be overwritten. If
lenis -1 or
NULL, the field value will be set to an SQL null value. The
valueis copied into the result's private storage, thus is no longer needed after the function returns. If the function fails, the return value is zero. If the function succeeds, the return value is non-zero.
Allocate subsidiary storage for a
void *PQresultAlloc(PGresult *res, size_t nBytes);
Any memory allocated with this function will be freed when
resis cleared. If the function fails, the return value is
NULL. The result is guaranteed to be adequately aligned for any type of data, just as for
Retrieves the number of bytes allocated for a
size_t PQresultMemorySize(const PGresult *res);
This value is the sum of all
mallocrequests associated with the
PGresultobject, that is, all the space that will be freed by
PQclear. This information can be useful for managing memory consumption.
Return the version of libpq that is being used.
The result of this function can be used to determine, at run time, whether specific functionality is available in the currently loaded version of libpq. The function can be used, for example, to determine which connection options are available in
The result is formed by multiplying the library's major version number by 10000 and adding the minor version number. For example, version 10.1 will be returned as 100001, and version 11.0 will be returned as 110000.
Prior to major version 10, Postgres Pro used three-part version numbers in which the first two parts together represented the major version. For those versions,
PQlibVersionuses two digits for each part; for example version 9.1.5 will be returned as 90105, and version 9.2.0 will be returned as 90200.
Therefore, for purposes of determining feature compatibility, applications should divide the result of
PQlibVersionby 100 not 10000 to determine a logical major version number. In all release series, only the last two digits differ between minor releases (bug-fix releases).
This function appeared in PostgreSQL version 9.1, so it cannot be used to detect required functionality in earlier versions, since calling it will create a link dependency on version 9.1 or later.