Syntax: |
|
Description: | Attempts to flush any queued output data to the server. Returns 0 if successful (or if the send queue is empty), -1 if it failed for some reason, or 1 if it was unable to send all the data in the send queue yet (this case can only occur if the connection is nonblocking). |
Comments: | After sending any command or data on a nonblocking connection, call PQflush. If it returns 1, wait for the socket to be write-ready and call it again; repeat until it returns 0. Once PQflush returns 0, wait for the socket to be read-ready and then read the response as described above. |
Syntax: |
|
Description: | Attempts to flush any queued output data to the server. Returns 0 if successful (or if the send queue is empty), -1 if it failed for some reason, or 1 if it was unable to send all the data in the send queue yet (this case can only occur if the connection is nonblocking). |
Comments: | After sending any command or data on a nonblocking connection, call PQflush. If it returns 1, wait for the socket to be write-ready and call it again; repeat until it returns 0. Once PQflush returns 0, wait for the socket to be read-ready and then read the response as described above. |
Syntax: | atom a = PQbackendPID(atom conn)
|
Description: | Returns the process ID (PID) of the backend server process handling this connection. |
Comments: | The backend PID is useful for debugging purposes and for comparison to NOTIFY messages (which include the PID of the notifying backend process). Note that the PID belongs to a process executing on the database server host, not the local host! |
Syntax: | atom a = PQbinaryTuples(atom res)
|
Description: | Returns 1 if the PGresult contains binary data and 0 if it contains text data. |
Comments: | This function is deprecated (except for its use in connection with COPY), because it is possible for a single PGresult to contain text data in some columns and binary data in others. PQfformat is preferred. PQbinaryTuples returns 1 only if all columns of the result are binary (format 1). |
Syntax: | object result = PQcancel(atom cancel, sequence errbuf, integer errbufsize)
|
Description: | Requests that the server abandon processing of the current command. The return value is 1 if the cancel request was successfully dispatched and 0 if not. If not, errbuf is filled with an error message explaining why not. errbuf must be a sequence of size errbufsize (the recommended size is 256 bytes). Fill it with zeros using s1 = repeat(0, 256). Successful dispatch is no guarantee that the request will have any effect, however. If the cancellation is effective, the current command will terminate early and return an error result. If the cancellation fails (say, because the server was already done processing the command), then there will be no visible result at all. PQcancel can safely be invoked from a signal handler, if the errbuf is a local variable in the signal handler. The PGcancel object is read-only as far as PQcancel is concerned, so it can also be invoked from a thread that is separate from the one manipulating the PGconn object. |
Syntax: | PQclear(atom res)
|
Description: | Frees the storage associated with a PGresult. Every command result should be freed via PQclear when it is no longer needed. |
Comments: | You can keep a PGresult object around for as long as you need it; it does not go away when you issue a new command, nor even if you close the connection. To get rid of it, you must call PQclear. Failure to do this will result in memory leaks in your application. |
Syntax: | atom a = PQclientEncoding(atom conn)
|
Description: | Returns client encoding. |
Syntax: | sequence s = PQcmdStatus(atom res)
|
Description: | Returns the command status tag from the SQL command that generated the PGresult. |
Comments: | Commonly this is just the name of the command, but it may include additional data such as the number of rows processed. The caller should not free the result directly. It will be freed when the associated PGresult handle is passed to PQclear. |
Syntax: | sequence s = PQcmdTuples(atom res)
|
Description: | Returns the number of rows affected by the SQL command. |
Comments: | This function returns a string containing the number of rows affected by the SQL statement that generated the PGresult. This function can only be used following the execution of an INSERT, UPDATE, DELETE, MOVE, or FETCH statement, or an EXECUTE of a prepared query that contains a INSERT, UPDATE, or DELETE statement. If the command that generated the PGresult was anything else, PQcmdTuples returns the empty string. The caller should not free the return value directly. It will be freed when the associated PGresult handle is passed to PQclear. |
Syntax: | atom connOptions = PQconndefaults()
|
Description: | Returns a pointer to an array containing the default connection options. This may be used to determine all possible PQconnectdb options and their current default values. The return value points to an array of PQconninfoOption structures. A null pointer is returned if memory could not be allocated. After processing the options array, free it by passing it to PQconninfoFree. If this is not done, a small amount of memory is leaked for each call to PQconndefaults. |
Comments: | The current default values will depend on environment variables and other context. Callers must treat the connection options data as read-only. |
See Also: | PQconninfoFree |
Syntax: | object result = PQconnectPoll(atom conn)
|
Description: | This differs from PQconnectStart in that it takes the conn structure instead of the conninfo string. |
Comments: | See the PostgreSQL documentation for more info on this function. |
See Also: | PQconnectStart |
Syntax: | object result = PQconnectStart(sequence conninfo)
|
Description: | Makes a connection to the database server in a nonblocking manner. |
Comments: | This function is used to open a connection to a database server such that your application's thread of execution is not blocked on remote I/O whilst doing so. The point of this approach is that the waits for I/O to complete can occur in the application's main loop, rather than down inside PQconnectdb, and so the application can manage this operation in parallel with other activities. |
See Also: | PQconnectPoll |
Syntax: | object result = PQconnectdb(sequence conninfo)
|
Description: | Makes a new connection to the database server. Unlike PQsetdbLogin, the parameter set can be extended without changing the function signature so use of this function (or its nonblocking analogues PQconnectStart and PQconnectPoll) is preferred for new application programming. |
Comments: | The passed string can be empty to use default parameters, or it can contain one or more parameter settings separated by whitespace. See the PostgreSQL docs for more info. |
See Also: | PQconnectPoll, PQconnectStart |
Syntax: | PQconninfoFree(atom connOptions)
|
Description: | Frees the memory allocated by the connOptions object. If not used, memory will be leaked by each call to PQconndefaults. |
Comments: | Using this procedure is necessary if you use the PQconndefaults function. If you do not use it, your system may run out of memory. |
See Also: | PQconndefaults |
Syntax: | atom a = PQconsumeInput(atom conn)
|
Description: | If input is available from the server, consume it. PQconsumeInput normally returns 1 indicating “no error”, but returns 0 if there was some kind of trouble (in which case PQerrorMessage can be consulted). Note that the result does not say whether any input data was actually collected. After calling PQconsumeInput, the application may check PQisBusy and/or PQnotifies to see if their state has changed. |
Comments: | PQconsumeInput may be called even if the application is not prepared to deal with a result or notification just yet. The function will read available data and save it in a buffer, thereby causing a select() read-ready indication to go away. The application can thus use PQconsumeInput to clear the select() condition immediately, and then examine the results at leisure. |
See Also: | PQerrorMessage, PQisBusy, PQnotifies |
Syntax: | sequence s = PQdb(atom conn)
|
Description: | Returns the database name of the connection. |
Syntax: | PQdisplayTuples(atom res, sequence fname, sequence mode, integer fillAlign, sequence fieldSep, integer printHeader, integer quiet)
|
Description: | Really old printing routine. |
Syntax: | atom a = PQdsplen(sequence s, integer encoding)
|
Description: | Determines display length of a multibyte encoded char at s. |
Syntax: | atom a = PQendcopy(atom conn)
|
Description: | Synchronizes with the server. |
Comments: | This function waits until the server has finished the copying. It should either be issued when the last string has been sent to the server using PQputline or when the last string has been received from the server using PGgetline. It must be issued or the server will get “out of sync” with the client. Upon return from this function, the server is ready to receive the next SQL command. The return value is 0 on successful completion, nonzero otherwise. (Use PQerrorMessage to retrieve details if the return value is nonzero.) When using PQgetResult, the application should respond to a PGRES_COPY_OUT result by executing PQgetline repeatedly, followed by PQendcopy after the terminator line is seen. It should then return to the PQgetResult loop until PQgetResult returns a null pointer. Similarly a PGRES_COPY_IN result is processed by a series of PQputline calls followed by PQendcopy, then return to the PQgetResult loop. This arrangement will ensure that a COPY command embedded in a series of SQL commands will be executed correctly. |
Syntax: | atom a = PQenv2encoding()
|
Description: | Gets the encoding id from environment variable PGCLIENTENCODING. |
Syntax: | sequence s = PQerrorMessage(atom conn)
|
Description: | Returns the error message most recently generated by an operation on the connection. |
Comments: | Nearly all libpq functions will set a message for PQerrorMessage if they fail. Note that by libpq convention, a nonempty PQerrorMessage result will include a trailing newline. The caller should not free the result directly. It will be freed when the associated PGconn handle is passed to PQfinish. The result string should not be expected to remain the same across operations on the PGconn structure. |
Syntax: | sequence s = PQescapeBytea(atom bintext, atom binlen, atom bytealen)
|
Description: | Escapes binary data for use within an SQL command with the type bytea. As with PQescapeString, this is only used when inserting data directly into an SQL command string. |
Comments: | Certain byte values must be escaped (but all byte values can be escaped) when used as part of a bytea literal in an SQL statement. In general, to escape a byte, it is converted into the three digit octal number equal to the octet value, and preceded by two backslashes. The single quote (') and backslash (\) characters have special alternative escape sequences. See Section 8.4, “Binary Data Types” for more information. PQescapeBytea performs this operation, escaping only the minimally required bytes. The from parameter points to the first byte of the string that is to be escaped, and the from_length parameter gives the number of bytes in this binary string. (A terminating zero byte is neither necessary nor counted.) The to_length parameter points to a variable that will hold the resultant escaped string length. The result string length includes the terminating zero byte of the result. PQescapeBytea returns an escaped version of the from parameter binary string in memory allocated with malloc() (a null pointer is returned if memory could not be allocated). This memory must be freed using PQfreemem when the result is no longer needed. The return string has all special characters replaced so that they can be properly processed by the PostgreSQL string literal parser, and the bytea input function. A terminating zero byte is also added. The single quotes that must surround PostgreSQL string literals are not part of the result string. |
Syntax: | sequence s = PQescapeString(sequence to, sequence from, atom length)
|
Description: | PQescapeString escapes a string for use within an SQL command. This is useful when inserting data values as literal constants in SQL commands. Certain characters (such as quotes and backslashes) must be escaped to prevent them from being interpreted specially by the SQL parser. PQescapeString performs this operation. |
Comments: | It is especially important to do proper escaping when handling strings that were received from an untrustworthy source. Otherwise there is a security risk: you are vulnerable to “SQL injection” attacks wherein unwanted SQL commands are fed to your database. Note that it is not necessary nor correct to do escaping when a data value is passed as a separate parameter in PQexecParams or its sibling routines. The parameter from points to the first character of the string that is to be escaped, and the length parameter gives the number of characters in this string. A terminating zero byte is not required, and should not be counted in length. (If a terminating zero byte is found before length bytes are processed, PQescapeString stops at the zero; the behavior is thus rather like strncpy.) to shall point to a buffer that is able to hold at least one more character than twice the value of length, otherwise the behavior is undefined. A call to PQescapeString writes an escaped version of the from string to the to buffer, replacing special characters so that they cannot cause any harm, and adding a terminating zero byte. The single quotes that must surround PostgreSQL string literals are not included in the result string; they should be provided in the SQL command that the result is inserted into. PQescapeString returns the number of characters written to to, not including the terminating zero byte. Behavior is undefined if the to and from strings overlap. |
Syntax: | atom a = PQexec(atom conn, sequence query)
|
Description: | Submits a command to the server and waits for the result. |
Comments: | Returns a PGresult pointer or possibly a null pointer. A non-null pointer will generally be returned except in out-of-memory conditions or serious errors such as inability to send the command to the server. If a null pointer is returned, it should be treated like a PGRES_FATAL_ERROR result. Use PQerrorMessage to get more information about such errors. It is allowed to include multiple SQL commands (separated by semicolons) in the command string. Multiple queries sent in a single PQexec call are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the query string to divide it into multiple transactions. Note however that the returned PGresult structure describes only the result of the last command executed from the string. Should one of the commands fail, processing of the string stops with it and the returned PGresult describes the error condition. |
See Also: | PQexecParams, PQexecPrepared |
Syntax: | atom a = PQexecParams(atom conn, sequence command, integer nParams, atom paramTypes, sequence paramValues, atom paramLengths, atom paramFormats, integer resultFormat)
|
Description: | Submits a command to the server and waits for the result, with the ability to pass parameters separately from the SQL command text. |
Comments: | PQexecParams is like PQexec, but offers additional functionality: parameter values can be specified separately from the command string proper, and query results can be requested in either text or binary format. PQexecParams is supported only in protocol 3.0 and later connections; it will fail when using protocol 2.0. |
See Also: | PQexec, PQexecPrepared |
Syntax: | atom a = PQexecPrepared(atom conn, sequence stmtName, integer nParams, sequence paramValues, atom paramLengths, atom paramFormats, integer resultFormat)
|
Description: | Sends a request to execute a prepared statement with given parameters, and waits for the result. |
Comments: | PQexecPrepared is like PQexecParams, but the command to be executed is specified by naming a previously-prepared statement, instead of giving a query string. This feature allows commands that will be used repeatedly to be parsed and planned just once, rather than each time they are executed. The statement must have been prepared previously in the current session. PQexecPrepared is supported only in protocol 3.0 and later connections; it will fail when using protocol 2.0. |
See Also: | PQexec, PQexecParams |
Syntax: | atom a = PQfformat(atom res, integer column_number)
|
Description: | Returns the format code indicating the format of the given column. Column numbers start at 0. |
Comments: | Format code zero indicates textual data representation, while format code one indicates binary representation. (Other codes are reserved for future definition.) |
Syntax: | PQfinish(atom conn)
|
Description: | Closes the connection to the server. Also frees memory used by the conn object. |
Comments: | Note that even if the server connection fails (as indicated by PQstatus), the application should call PQfinish to free the memory used by the conn object. The conn pointer must NOT be used again after PQfinish has been called! |
See Also: | PQstatus |
Syntax: | atom a = PQfmod(atom res, integer column_num)
|
Description: | Returns the type modifier of the column associated with the given column number. Column numbers start at 0. |
Comments: | The interpretation of modifier values is type-specific; they typically indicate precision or size limits. The value -1 is used to indicate “no information available”. Most data types do not use modifiers, in which case the value is always -1. |
Syntax: | atom a = PQfn(atom conn, integer fnid, atom result_buf, atom result_len, integer result_is_int, atom args, integer nargs)
|
Description: | The function PQfn requests execution of a server function via the fast-path interface: |
Comments: | The fnid argument is the OID of the function to be executed. args and nargs define the parameters to be passed to the function; they must match the declared function argument list. When the isint field of a parameter structure is true, the u.integer value is sent to the server as an integer of the indicated length (this must be 1, 2, or 4 bytes); proper byte-swapping occurs. When isint is false, the indicated number of bytes at *u.ptr are sent with no processing; the data must be in the format expected by the server for binary transmission of the function's argument data type. result_buf is the buffer in which to place the return value. The caller must have allocated sufficient space to store the return value. (There is no check!) The actual result length will be returned in the integer pointed to by result_len. If a 1, 2, or 4-byte integer result is expected, set result_is_int to 1, otherwise set it to 0. Setting result_is_int to 1 causes libpq to byte-swap the value if necessary, so that it is delivered as a proper int value for the client machine. When result_is_int is 0, the binary-format byte string sent by the server is returned unmodified. PQfn always returns a valid PGresult pointer. The result status should be checked before the result is used. The caller is responsible for freeing the PGresult with PQclear when it is no longer needed. Note that it is not possible to handle null arguments, null results, nor set-valued results when using this interface. |
Syntax: | sequence s = PQfname(atom res, integer field_num)
|
Description: | Returns the column name associated with the given column number. Column numbers start at 0. The caller should not free the result directly. It will be freed when the associated PGresult handle is passed to PQclear. |
Comments: | NULL is returned if the column number is out of range. |
Syntax: | atom a = PQfnumber(atom res, sequence field_name)
|
Description: | Returns the column number associated with the given column name. |
Comments: |
Syntax: | PQfreeCancel(atom cancel)
|
Description: | Frees a data structure created by PQgetCancel. |
Syntax: | PQfreemem(atom ptr)
|
Description: | Frees memory allocated by libpq. |
Comments: | Frees memory allocated by libpq, particularly PQescapeBytea, PQunescapeBytea, and PQnotifies. It is needed by Microsoft Windows, which cannot free memory across DLLs, unless multithreaded DLLs (/MD in VC6) are used. On other platforms, this function is the same as the standard library function free(). |
Syntax: | atom a = PQfsize(atom res, integer, column_num)
|
Description: | Returns the size in bytes of the column associated with the given column number. Column numbers start at 0. |
Comments: | PQfsize returns the space allocated for this column in a database row, in other words the size of the server's internal representation of the data type. (Accordingly, it is not really very useful to clients.) A negative value indicates the data type is variable-length. |
Syntax: | atom a = PQftable(atom res, integer, field_num)
|
Description: | Returns the OID of the table from which the given column was fetched. Column numbers start at 0. |
Comments: | InvalidOid is returned if the column number is out of range, or if the specified column is not a simple reference to a table column, or when using pre-3.0 protocol. You can query the system table pg_class to determine exactly which table is referenced. The type Oid and the constant InvalidOid will both be some integer type. |
Syntax: | atom a = PQftablecol(atom res, integer column_num)
|
Description: | Returns the column number (within its table) of the column making up the specified query result column. Query-result column numbers start at 0, but table columns have nonzero numbers. |
Comments: | Zero is returned if the column number is out of range, or if the specified column is not a simple reference to a table column, or when using pre-3.0 protocol. |
Syntax: | atom a = PQftype(atom res, integer column_num)
|
Description: | Returns the data type associated with the given column number. The integer returned is the internal OID number of the type. Column numbers start at 0. |
Comments: | You can query the system table pg_type to obtain the names and properties of the various data types. |
Syntax: | atom result = PQgetCancel(atom conn)
|
Description: | Creates a data structure containing the information needed to cancel a command issued through a particular database connection. PQgetCancel creates a PGcancel object given a PGconn connection object. It will return a null pointer if the given conn is NULL or an invalid connection. The PGcancel object is an opaque structure that is not meant to be accessed directly by the application; it can only be passed to PQcancel or PQfreeCancel. |
Comments: | Used for canceling commands. |
See Also: | PQcancel, PQfreeCancel |
Syntax: | atom a = PQgetCopyData(atom conn, sequence buffer, integer async)
|
Description: | Receives data from the server during COPY_OUT state. |
Comments: | Attempts to obtain another row of data from the server during a COPY. Data is always returned one data row at a time; if only a partial row is available, it is not returned. Successful return of a data row involves allocating a chunk of memory to hold the data. The buffer parameter must be non-NULL. *buffer is set to point to the allocated memory, or to NULL in cases where no buffer is returned. A non-NULL result buffer must be freed using PQfreemem when no longer needed. When a row is successfully returned, the return value is the number of data bytes in the row (this will always be greater than zero). The returned string is always null-terminated, though this is probably only useful for textual COPY. A result of zero indicates that the COPY is still in progress, but no row is yet available (this is only possible when async is true). A result of -1 indicates that the COPY is done. A result of -2 indicates that an error occurred (consult PQerrorMessage for the reason). When async is true (not zero), PQgetCopyData will not block waiting for input; it will return zero if the COPY is still in progress but no complete row is available. (In this case wait for read-ready and then call PQconsumeInput before calling PQgetCopyData again.) When async is false (zero), PQgetCopyData will block until data is available or the operation completes. After PQgetCopyData returns -1, call PQgetResult to obtain the final result status of the COPY command. One may wait for this result to be available in the usual way. Then return to normal operation. |
Syntax: | atom a = PQgetResult(atom conn)
|
Description: | Waits for the next result from a prior PQsendQuery, PQsendQueryParams, PQsendPrepare, or PQsendQueryPrepared call, and returns it. A null pointer is returned when the command is complete and there will be no more results. |
Comments: | PQgetResult must be called repeatedly until it returns a null pointer, indicating that the command is done. (If called when no command is active, PQgetResult will just return a null pointer at once.) Each non-null result from PQgetResult should be processed using the same PGresult accessor functions previously described. Don't forget to free each result object with PQclear when done with it. Note that PQgetResult will block only if a command is active and the necessary response data has not yet been read by PQconsumeInput. |
Syntax: | atom a = PQgetisnull(atom res, integer row_num, integer column_num)
|
Description: | Tests a field for a null value. Row and column numbers start at 0. |
Comments: | This function returns 1 if the field is null and 0 if it contains a non-null value. (Note that PQgetvalue will return an empty string, not a null pointer, for a null field.) |
Syntax: | atom a = PQgetlength(atom res, integer row_num, integer column_num)
|
Description: | Returns the actual length of a field value in bytes. Row and column numbers start at 0. |
Comments: | This is the actual data length for the particular data value, that is, the size of the object pointed to by PQgetvalue. For text data format this is the same as length(). For binary format this is essential information. Note that one should not rely on PQfsize to obtain the actual data length. |
See Also: | PQfsize |
Syntax: | atom a = PQgetline(atom conn, sequence string, integer length)
|
Description: | Reads a newline-terminated line of characters (transmitted by the server) into a buffer string of size length. Using s = repeat(0, length) is the simplest way. |
Comments: | This function copies up to length-1 characters into the buffer and converts the terminating newline into a zero byte. PQgetline returns EOF at the end of input, 0 if the entire line has been read, and 1 if the buffer is full but the terminating newline has not yet been read. Note that the application must check to see if a new line consists of the two characters \., which indicates that the server has finished sending the results of the COPY command. If the application might receive lines that are more than length-1 characters long, care is needed to be sure it recognizes the \. line correctly (and does not, for example, mistake the end of a long data line for a terminator line). |
Syntax: | atom a = PQgetlineAsync(atom conn, sequence buffer, integer bufsize)
|
Description: | Reads a row of COPY data (transmitted by the server) into a buffer without blocking. |
Comments: | This function is similar to PQgetline, but it can be used by applications that must read COPY data asynchronously, that is, without blocking. Having issued the COPY command and gotten a PGRES_COPY_OUT response, the application should call PQconsumeInput and PQgetlineAsync until the end-of-data signal is detected. Unlike PQgetline, this function takes responsibility for detecting end-of-data. On each call, PQgetlineAsync will return data if a complete data row is available in libpq's input buffer. Otherwise, no data is returned until the rest of the row arrives. The function returns -1 if the end-of-copy-data marker has been recognized, or 0 if no data is available, or a positive number giving the number of bytes of data returned. If -1 is returned, the caller must next call PQendcopy, and then return to normal processing. The data returned will not extend beyond a data-row boundary. If possible a whole row will be returned at one time. But if the buffer offered by the caller is too small to hold a row sent by the server, then a partial data row will be returned. With textual data this can be detected by testing whether the last returned byte is \n or not. (In a binary COPY, actual parsing of the COPY data format will be needed to make the equivalent determination.) The returned string is not null-terminated. (If you want to add a terminating null, be sure to pass a bufsize one smaller than the room actually available.) |
See Also: | PQgetline, PQconsumeInput |
Syntax: | atom a = PQgetssl(atom conn)
|
Description: | Returns a pointer to the SSL structure used in the connection, or null if SSL is not in use. |
Comments: | This structure can be used to verify encryption levels, check server certificates, and more. Refer to the OpenSSL documentation for information about this structure. |
Syntax: | sequence s = PQgetvalue(atom res, integer row_num, integer column_num)
|
Description: | Returns a single field value of one row of a PGresult. Row and column numbers start at 0. The caller should not free the result directly. It will be freed when the associated PGresult handle is passed to PQclear. |
Comments: | For data in text format, the value returned by PQgetvalue is a null-terminated character string representation of the field value. For data in binary format, the value is in the binary representation determined by the data type's typsend and typreceive functions. (The value is actually followed by a zero byte in this case too, but that is not ordinarily useful, since the value is likely to contain embedded nulls.) An empty string is returned if the field value is null. See PQgetisnull to distinguish null values from empty-string values. The pointer returned by PQgetvalue points to storage that is part of the PGresult structure. One should not modify the data it points to, and one must explicitly copy the data into other storage if it is to be used past the lifetime of the PGresult structure itself. |
Syntax: | sequence s = PQhost(atom conn)
|
Description: | Returns the server host name of the connection. |
Syntax: | PQinitSSL(atom do_init)
|
Description: | Tell libpq whether it needs to initialize OpenSSL. |
Syntax: | atom a = PQisBusy(atom conn)
|
Description: | Returns 1 if a command is busy, that is, PQgetResult would block waiting for input. A 0 return indicates that PQgetResult can be called with assurance of not blocking. |
Comments: | PQisBusy will not itself attempt to read data from the server; therefore PQconsumeInput must be invoked first, or the busy state will never end. |
See Also: | PQgetResult, PQconsumeInput |
Syntax: | atom a = PQisnonblocking(atom conn)
|
Description: | Returns the blocking status of the database connection. |
Comments: | Returns 1 if the connection is set to nonblocking mode and 0 if blocking. |
See Also: | PQsetnonblocking |
Syntax: | atom a = PQmakeEmptyPGresult(atom conn, atom status)
|
Description: | Constructs an empty PGresult object with the given status. |
Comments: | This is libpq's internal function to allocate and initialize an empty PGresult object. This function returns NULL if 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 conn is not null and status indicates an error, the current error message of the specified connection is copied into the PGresult. Note that PQclear should eventually be called on the object, just as with a PGresult returned by libpq itself. |
Syntax: | atom a = PQmblen(sequence s, integer encoding)
|
Description: | Determines length of a multibyte encoded char at s) |
Syntax: | atom a = PQnfields(atom res)
|
Description: | Returns the number of columns (fields) in each row of the query result. |
Syntax: | sequence s = PQnoPasswordSupplied
|
Description: | Defines this string so all uses are consistent. |
Syntax: | atom a = PQnotifies(atom conn)
|
Description: | The function PQnotifies returns the next notification from a list of unhandled notification messages received from the server. It returns a null pointer if there are no pending notifications. Once a notification is returned from PQnotifies, it is considered handled and will be removed from the list of notifications. |
Comments: | After processing a PGnotify object returned by PQnotifies, be sure to free it with PQfreemem. |
See Also: | PQfreemem |
Syntax: | atom a = PQntuples(atom res)
|
Description: | Returns the number of rows (tuples) in the query result. |
Syntax: | atom a = PQoidValue(atom res)
|
Description: | Returns a string with the OID of the inserted row, if the SQL command was an INSERT that inserted exactly one row, or a EXECUTE of a prepared statement consisting of a suitable INSERT. (The string will be 0 if the INSERT did not insert exactly one row, or if the target table does not have OIDs.) If the command was not an INSERT, returns an empty string. |
Comments: | This function is deprecated in favor of PQoidValue. It is not thread-safe. |
Syntax: | atom a = PQoidValue(atom res)
|
Description: | Returns the OID of the inserted row, if the SQL command was an INSERT that inserted exactly one row into a table that has OIDs, or a EXECUTE of a prepared query containing a suitable INSERT statement. Otherwise, this function returns InvalidOid. This function will also return InvalidOid if the table affected by the INSERT statement does not contain OIDs. |
Syntax: | sequence s = PQoptions(atom conn)
|
Description: | Returns the command-line options passed in the connection request. |
Syntax: | sequence s = PQparameterStatus(atom conn, sequence paramName)
|
Description: | Looks up a current parameter setting of the server. |
Comments: | Certain parameter values are reported by the server automatically at connection startup or whenever their values change. PQparameterStatus can be used to interrogate these settings. It returns the current value of a parameter if known, or NULL if the parameter is not known. Parameters reported as of the current release include server_version, server_encoding, client_encoding, is_superuser, session_authorization, DateStyle, TimeZone, integer_datetimes, and standard_conforming_strings. (server_encoding, TimeZone, and integer_datetimes were not reported by releases before 8.0; standard_conforming_strings was not reported by releases before 8.1.) Note that server_version, server_encoding and integer_datetimes cannot change after startup. Pre-3.0-protocol servers do not report parameter settings, but libpq includes logic to obtain values for server_version and client_encoding anyway. Applications are encouraged to use PQparameterStatus rather than ad hoc code to determine these values. (Beware however that on a pre-3.0 connection, changing client_encoding via SET after connection startup will not be reflected by PQparameterStatus.) For server_version, see also PQserverVersion, which returns the information in a numeric form that is much easier to compare against. If no value for standard_conforming_strings is reported, applications may assume it is false, that is, backslashes are treated as escapes in string literals. Also, the presence of this parameter may be taken as an indication that the escape string syntax (E'...') is accepted. Although the returned pointer is declared const, it in fact points to mutable storage associated with the PGconn structure. It is unwise to assume the pointer will remain valid across queries. |
See Also: | PQserverVersion |
Syntax: | sequence s = PQpass(atom conn)
|
Description: | Returns the password of the connection. |
Syntax: | sequence s = PQport(atom conn)
|
Description: | Returns the port of the connection. |
Syntax: | atom a = PQprepare(atom conn, sequence stmtName, sequence query, integer nParams, atom paramTypes)
|
Description: | Submits a request to create a prepared statement with the given parameters, and waits for completion. |
Comments: | PQprepare creates a prepared statement for later execution with PQexecPrepared. This feature allows commands that will be used repeatedly to be parsed and planned just once, rather than each time they are executed. PQprepare is supported only in protocol 3.0 and later connections; it will fail when using protocol 2.0. |
Syntax: | PQprint(sequence fname, sequence mode, atom res, atom ps)
|
Description: | Prints out all the rows and, optionally, the column names to the specified file, using mode mode. |
Comments: | This function was formerly used by psql to print query results, but this is no longer the case. Note that it assumes all the data is in text format. |
Syntax: | PQprintTuples(atom res, sequence fname, sequence mode, integer printAttName, integer terseOutput, integer width)
|
Description: | Really old printing routine. |
Syntax: | atom a = PQprotocolVersion(atom conn)
|
Description: | Interrogates the frontend/backend protocol being used. |
Comments: | Applications may wish to use this to determine whether certain features are supported. Currently, the possible values are 2 (2.0 protocol), 3 (3.0 protocol), or zero (connection bad). This will not change after connection startup is complete, but it could theoretically change during a connection reset. The 3.0 protocol will normally be used when communicating with PostgreSQL 7.4 or later servers; pre-7.4 servers support only protocol 2.0. (Protocol 1.0 is obsolete and not supported by libpq.) |
Syntax: | atom a = PQputCopyData(atom conn, sequence buffer, integer nbytes)
|
Description: | Sends data to the server during COPY_IN state. |
Comments: | Transmits the COPY data in the specified buffer, of length nbytes, to the server. The result is 1 if the data was sent, zero if it was not sent because the attempt would block (this case is only possible if the connection is in nonblocking mode), or -1 if an error occurred. (Use PQerrorMessage to retrieve details if the return value is -1. If the value is zero, wait for write-ready and try again.) |
See Also: | PQerrorMessage |
Syntax: |
|
Description: | Sends end-of-data indication to the server during COPY_IN state. |
Comments: | Ends the COPY_IN operation successfully if errormsg is NULL. If errormsg is not NULL then the COPY is forced to fail, with the string pointed to by errormsg used as the error message. (One should not assume that this exact error message will come back from the server, however, as the server might have already failed the COPY for its own reasons. Also note that the option to force failure does not work when using pre-3.0-protocol connections.) The result is 1 if the termination data was sent, zero if it was not sent because the attempt would block (this case is only possible if the connection is in nonblocking mode), or -1 if an error occurred. (Use PQerrorMessage to retrieve details if the return value is -1. If the value is zero, wait for write-ready and try again.) After successfully calling PQputCopyEnd, call PQgetResult to obtain the final result status of the COPY command. One may wait for this result to be available in the usual way. Then return to normal operation. |
Syntax: | atom a = PQputline(atom conn, sequence string)
|
Description: | Sends a null-terminated string to the server. Returns 0 if OK and EOF if unable to send the string. |
Comments: | The COPY data stream sent by a series of calls to PQputline has the same format as that returned by PQgetlineAsync, except that applications are not obliged to send exactly one data row per PQputline call; it is okay to send a partial line or multiple lines per call. |
Syntax: | atom a = PQputnbytes(atom conn, sequence buffer, integer nbytes)
|
Description: | Sends a non-null-terminated string to the server. Returns 0 if OK and EOF if unable to send the string. |
Comments: | This is exactly like PQputline, except that the data buffer need not be null-terminated since the number of bytes to send is specified directly. Use this procedure when sending binary data. |
See Also: | PQputline |
Syntax: | atom a = PQregisterThreadLock(atom newhandler)
|
Description: | Used to set callback that prevents concurrent access to non-thread safe functions that libpq needs. The default implementation uses a libpq internal mutex. Only required for multithreaded apps that use kerberos both within their app and for postgresql connections. |
Syntax: | integer i = PQrequestCancel(atom conn)
|
Description: | Requests that the server abandon processing of the current command. PQrequestCancel is a deprecated variant of PQcancel. It operates directly on the PGconn object, and in case of failure stores the error message in the PGconn object (whence it can be retrieved by PQerrorMessage). Although the functionality is the same, this approach creates hazards for multiple-thread programs and signal handlers, since it is possible that overwriting the PGconn's error message will mess up the operation currently in progress on the connection. |
See Also: | PQcancel, PQerrorMessage |
Syntax: | sequence s = PQresStatus(atom status)
|
Description: | Converts the enumerated type returned by PQresultStatus into a string constant describing the status code. The caller should not free the result. |
Syntax: | PQreset(atom conn)
|
Description: | Resets the communication channel to the server. This function will close the connection to the server and attempt to reestablish a new connection to the same server, using all the same parameters previously used. This may be useful for error recovery if a working connection is lost. |
Comments: | This is similar to PQresetStart and PQresetPoll, except the latter reset the channel to the server in a nonblocking manner. |
See Also: | PQresetStart, PQresetPoll |
Syntax: | object result = PQresetPoll(atom conn)
|
Description: | Used with PQresetStart to reset the communication channel to the server, in a non- blocking manner. First run PQresetStart, then if successful, poll the reset using PQresetPoll just like you would create a connection using PQconnectPoll. |
Comments: | See PQresetStart for more information. Also see the PostgreSQL documentation. |
See Also: | PQresetStart |
Syntax: | atom x1 = PQresetStart(atom conn)
|
Description: | This function will close the connection to the server and attempt to reestablish a new connection to the same server, using all the same parameters previously used. This may be useful for error recovery if a working connection is lost. It differs from PQreset in that it acts in a nonblocking manner. |
Comments: | returns an integer, 0 if failed, 1 if successful. If it returns 1, poll the reset using PQresetPoll in exactly the same way as you would create a connection using PQconnectPoll. |
See Also: | PQresetPoll, PQconnectPoll |
Syntax: | sequence s = PQresultErrorField(atom res, integer fieldcode)
|
Description: | Returns an individual field of an error report. |
Comments: | fieldcode is an error field identifier; see the symbols listed below. NULL is returned if the PGresult is not an error or warning result, or does not include the specified field. Field values will normally not include a trailing newline. The caller should not free the result directly. It will be freed when the associated PGresult handle is passed to PQclear. |
Syntax: |
|
Description: | Returns the error message associated with the command, or an empty string if there was no error. |
Comments: | If there was an error, the returned string will include a trailing newline. The caller should not free the result directly. It will be freed when the associated PGresult handle is passed to PQclear. Immediately following a PQexec or PQgetResult call, PQerrorMessage (on the connection) will return the same string as PQresultErrorMessage (on the result). However, a PGresult will retain its error message until destroyed, whereas the connection's error message will change when subsequent operations are done. Use PQresultErrorMessage when you want to know the status associated with a particular PGresult; use PQerrorMessage when you want to know the status from the latest operation on the connection. |
See Also: | PQerrorMessage |
Syntax: | atom a = PQresultStatus(atom res)
|
Description: | Returns the result status of the command. |
Comments: | PQresultStatus can return one of the following values: PGRES_EMPTY_QUERY The string sent to the server was empty. PGRES_COMMAND_OK Successful completion of a command returning no data. PGRES_TUPLES_OK Successful completion of a command returning data (such as a SELECT or SHOW). PGRES_COPY_OUT Copy Out (from server) data transfer started. PGRES_COPY_IN Copy In (to server) data transfer started. PGRES_BAD_RESPONSE The server's response was not understood. PGRES_NONFATAL_ERROR A nonfatal error (a notice or warning) occurred. PGRES_FATAL_ERROR A fatal error occurred. If the result status is PGRES_TUPLES_OK, then the functions described below can be used to retrieve the rows returned by the query. Note that a SELECT command that happens to retrieve zero rows still shows PGRES_TUPLES_OK. PGRES_COMMAND_OK is for commands that can never return rows (INSERT, UPDATE, etc.). A response of PGRES_EMPTY_QUERY may indicate a bug in the client software. A result of status PGRES_NONFATAL_ERROR will never be returned directly by PQexec or other query execution functions; results of this kind are instead passed to the notice processor. |
Syntax: | atom a = PQsendPrepare(atom conn, sequence stmtName, sequence query, atom nParams, atom paramTypes)
|
Description: | Sends a request to create a prepared statement with the given parameters, without waiting for completion. |
Comments: | This is an asynchronous version of PQprepare: it returns 1 if it was able to dispatch the request, and 0 if not. After a successful call, call PQgetResult to determine whether the server successfully created the prepared statement. The function's parameters are handled identically to PQprepare. Like PQprepare, it will not work on 2.0-protocol connections. |
See Also: | PQprepare |
Syntax: | atom a = PQsendQuery(atom conn, sequence query)
|
Description: | Submits a command to the server without waiting for the result(s). 1 is returned if the command was successfully dispatched and 0 if not (in which case, use PQerrorMessage to get more information about the failure). |
Comments: | After successfully calling PQsendQuery, call PQgetResult one or more times to obtain the results. PQsendQuery may not be called again (on the same connection) until PQgetResult has returned a null pointer, indicating that the command is done. |
See Also: | PQgetResult |
Syntax: | atom a = PQsendQueryParams(atom conn, sequence command, integer nParams, atom paramTypes, sequence paramValues, atom paramLengths, atom paramFormats, integer resultFormat)
|
Description: | Submits a command and separate parameters to the server without waiting for the result(s). |
Comments: | This is equivalent to PQsendQuery except that query parameters can be specified separately from the query string. The function's parameters are handled identically to PQexecParams. Like PQexecParams, it will not work on 2.0-protocol connections, and it allows only one command in the query string. |
See Also: | PQsendQuery |
Syntax: | atom a = PQsendQueryPrepared(atom conn, sequence stmtName, integer nParams, sequence paramValues, atom paramLengths, atom paramFormats, integer resultFormat)
|
Description: | Sends a request to execute a prepared statement with given parameters, without waiting for the result(s). |
Comments: | This is similar to PQsendQueryParams, but the command to be executed is specified by naming a previously-prepared statement, instead of giving a query string. The function's parameters are handled identically to PQexecPrepared. Like PQexecPrepared, it will not work on 2.0-protocol connections. |
See Also: | PQsendQueryParams |
Syntax: | atom a = PQserverVersion(atom conn)
|
Description: | Returns an integer representing the backend version. |
Comments: | Applications may use this to determine the version of the database server they are connected to. The number is formed by converting the major, minor, and revision numbers into two-decimal-digit numbers and appending them together. For example, version 7.4.2 will be returned as 70402, and version 8.1 will be returned as 80100 (leading zeroes are not shown). Zero is returned if the connection is bad. |
Syntax: | atom a = PQsetClientEncoding(atom conn, sequence encoding)
|
Description: | Sets the client encoding to the type contained in sequence encoding. |
Syntax: | atom a = PQsetNoticeProcessor(atom conn, atom proc, atom arg)
|
Description: | Similar to PQsetNoticeReceiver, this function sets or examines the current notice processor for a connection object. |
See Also: | PQsetNoticeReceiver |
Syntax: | atom a = PQsetNoticeReceiver(atom conn, atom proc, atom arg)
|
Description: | Similar to PQsetNoticeProcessor, this function sets or examines the current notice receiver for a connection object. |
Comments: | This function returns the previous notice receiver or processor function pointer, and sets the new value. If you supply a null function pointer, no action is taken, but the current pointer is returned. |
See Also: | PQsetNoticeProcessor |
Syntax: | object result = PQsetdbLogin(sequence pghost, sequence pgport, sequence pgoptions, sequence pgtty, sequence dbName, sequence login, sequence pwd)
|
Description: | This is the predecessor of PQconnectdb with a fixed set of parameters. It has the same functionality except that the missing parameters will always take on default values. Write an empty sequence for any one of the fixed parameters that is to be defaulted. |
Comments: | This is an older routine, it is better to use PQconnectdb. |
See Also: | PQconnectdb |
Syntax: | atom a = PQsetnonblocking(atom conn, integer arg)
|
Description: | Sets the nonblocking status of the connection. Sets the state of the connection to nonblocking if arg is 1, or blocking if arg is 0. Returns 0 if OK, -1 if error.In the nonblocking state, calls to PQsendQuery, PQputline, PQputnbytes, and PQendcopy will not block but instead return an error if they need to be called again. Note that PQexec does not honor nonblocking mode; if it is called, it will act in blocking fashion anyway. |
See Also: | PQisnonblocking |
Syntax: | atom a = PQsocket(atom conn)
|
Description: | Obtains the file descriptor number of the connection socket to the server. A valid descriptor will be greater than or equal to 0; a result of -1 indicates that no server connection is currently open. (This will not change during normal operation, but could change during connection setup or reset.) |
Syntax: | atom a = PQstatus(atom conn)
|
Description: | Returns the status of the connection. |
Comments: | The status can be one of a number of values. However, only two of these are seen outside of an asynchronous connection procedure: 0 and 1. A good connection to the database has the status 0. A failed connection attempt is signaled by status 0. Ordinarily, an OK status will remain so until PQfinish, but a communications failure might result in the status changing to 0 prematurely. In that case the application could try to recover by calling PQreset. See the entry for PQconnectStart and PQconnectPoll with regards to other status codes that might be seen. |
See Also: | PQconnectStart, PQconnectPoll |
Syntax: | PQtrace(atom conn, sequence fname, sequence mode)
|
Description: | Enables tracing of the client/server communication to a file, using mode mode. |
Comments: | On Windows, if the libpq library and an application are compiled with different flags, this function call will crash the application because the internal representation of the FILE pointers differ. Specifically, multithreaded/single-threaded, release/debug, and static/dynamic flags should be the same for the library and all applications using that library. |
Syntax: |
|
Description: | Returns the current in-transaction status of the server. |
Comments: | The status can be 0 (currently idle), 1 (a command is in progress), 2 (idle, in a valid transaction block), or 3 (idle, in a failed transaction block). 4 is reported if the connection is bad. 5 is reported only when a query has been sent to the server and not yet completed. |
Syntax: | sequence s = PQtty(atom conn)
|
Description: | Returns the debug TTY of the connection. (This is obsolete, since the server no longer pays attention to the TTY setting, but the function remains for backwards compatibility.) |
Syntax: | sequence s = PQunescapeBytea(sequence strtext, atom strlen)
|
Description: | Converts an escaped string representation of binary data into binary data [mdash ] the reverse of PQescapeBytea. This is needed when retrieving bytea data in text format, but not when retrieving it in binary format. |
Comments: | The from parameter points to an escaped string such as might be returned by PQgetvalue when applied to a bytea column. PQunescapeBytea converts this string representation into its binary representation. It returns a pointer to a buffer allocated with malloc(), or null on error, and puts the size of the buffer in to_length. The result must be freed using PQfreemem when it is no longer needed. |
Syntax: | PQuntrace(atom conn)
|
Description: | Disable tracing started by PQtrace. |
See Also: | PQtrace |
Syntax: | sequence s = PQuser(atom conn)
|
Description: | Returns the user name of the connection. |
Syntax: | atom a = lo_close(atom conn, integer fd)
|
Description: | A large object descriptor may be closed by calling this function |
Comments: | where fd is a large object descriptor returned by lo_open. On success, lo_close returns zero. On error, the return value is negative. Any large object descriptors that remain open at the end of a transaction will be closed automatically. |
Syntax: | atom a = lo_creat(atom conn, integer mode)
|
Description: | Creates a new large object. The return value is the OID that was assigned to the new large object, or InvalidOid (zero) on failure. mode is unused and ignored as of PostgreSQL 8.1; however, for backwards compatibility with earlier releases it is best to set it to INV_READ, INV_WRITE, or INV_READ | INV_WRITE. |
Syntax: | atom a = lo_create(atom conn, atom lobjId)
|
Description: | Creates a new large object. The OID to be assigned can be specified by lobjId; if so, failure occurs if that OID is already in use for some large object. If lobjId is InvalidOid (zero) then lo_create assigns an unused OID (this is the same behavior as lo_creat). The return value is the OID that was assigned to the new large object, or InvalidOid (zero) on failure. lo_create is new as of PostgreSQL 8.1; if this function is run against an older server version, it will fail and return InvalidOid. |
Syntax: | atom a = lo_export(atom conn, atom lobjId, sequence filename)
|
Description: | Used to export a large object into an operating sytem file. The lobjId argument specifies the OID of the large object to export and the filename argument specifies the operating system name of the file. Note that the file is written by the client interface library, not by the server. Returns 1 on success, -1 on failure. |
Syntax: | atom a = lo_import(atom conn, sequence filename)
|
Description: | Used to import an operating system file as a large object. Filename specifies the operating system name of the file to be imported as a large object. The return value is the OID that was assigned to the new large object, or InvalidOid (zero) on failure. Note that the file is read by the client interface library, not by the server; so it must exist in the client filesystem and be readable by the client application. |
Syntax: | atom a = lo_lseek(atom conn, integer fd, integer offset, integer whence)
|
Description: | Changes the current read or write location associated with a large object descriptor. |
Comments: | This function moves the current location pointer for the large object descriptor identified by fd to the new location specified by offset. The valid values for whence are SEEK_SET (seek from object start), SEEK_CUR (seek from current position), and SEEK_END (seek from object end). The return value is the new location pointer, or -1 on error. |
Syntax: | atom a = lo_open(atom conn, atom lobjId, integer mode)
|
Description: | To open an existing large object for reading or writing, call this function. |
Comments: | The lobjId argument specifies the OID of the large object to open. The mode bits control whether the object is opened for reading (INV_READ), writing (INV_WRITE), or both. (These symbolic constants are defined in the header file libpq/libpq-fs.h.) A large object cannot be opened before it is created. lo_open returns a (non-negative) large object descriptor for later use in lo_read, lo_write, lo_lseek, lo_tell, and lo_close. The descriptor is only valid for the duration of the current transaction. On failure, -1 is returned. |
Syntax: | atom a = lo_read(atom conn, integer fd, sequence buf, atom len)
|
Description: | Reads len bytes from large object descriptor fd into buf. The fd argument must have been returned by a previous lo_open. The number of bytes actually read is returned. In the event of an error, the return value is negative. |
Syntax: | atom a = lo_tell(atom conn, integer fd)
|
Description: | Obtains the current read or write location of a large object descriptor. If there is an error, the return value is negative. |
Syntax: | atom a = lo_unlink(atom conn, atom lobjId)
|
Description: | Used to remove a large object from the database. The lobjId argument specifies the OID of the large object to remove. Returns 1 if successful, -1 on failure. |
Syntax: | atom a = lo_write(atom conn, integer fd, sequence buf, atom len)
|
Description: | Writes len bytes from buf to large object descriptor fd. The fd argument must have been returned by a previous lo_open. The number of bytes actually written is returned. In the event of an error, the return value is negative. |