📄 fe-exec.c
字号:
/*------------------------------------------------------------------------- * * fe-exec.c * functions related to sending a query down to the backend * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.176.2.3 2006/05/21 20:19:44 tgl Exp $ * *------------------------------------------------------------------------- */#include "postgres_fe.h"#include <errno.h>#include <ctype.h>#include <fcntl.h>#include "libpq-fe.h"#include "libpq-int.h"#include "mb/pg_wchar.h"#ifdef WIN32#include "win32.h"#else#include <unistd.h>#endif/* keep this in same order as ExecStatusType in libpq-fe.h */char *const pgresStatus[] = { "PGRES_EMPTY_QUERY", "PGRES_COMMAND_OK", "PGRES_TUPLES_OK", "PGRES_COPY_OUT", "PGRES_COPY_IN", "PGRES_BAD_RESPONSE", "PGRES_NONFATAL_ERROR", "PGRES_FATAL_ERROR"};/* * static state needed by PQescapeString and PQescapeBytea; initialize to * values that result in backward-compatible behavior */static int static_client_encoding = PG_SQL_ASCII;static bool static_std_strings = false;static bool PQsendQueryStart(PGconn *conn);static int PQsendQueryGuts(PGconn *conn, const char *command, const char *stmtName, int nParams, const Oid *paramTypes, const char *const * paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);static void parseInput(PGconn *conn);static bool PQexecStart(PGconn *conn);static PGresult *PQexecFinish(PGconn *conn);/* ---------------- * Space management for PGresult. * * Formerly, libpq did a separate malloc() for each field of each tuple * returned by a query. This was remarkably expensive --- malloc/free * consumed a sizable part of the application's runtime. And there is * no real need to keep track of the fields separately, since they will * all be freed together when the PGresult is released. So now, we grab * large blocks of storage from malloc and allocate space for query data * within these blocks, using a trivially simple allocator. This reduces * the number of malloc/free calls dramatically, and it also avoids * fragmentation of the malloc storage arena. * The PGresult structure itself is still malloc'd separately. We could * combine it with the first allocation block, but that would waste space * for the common case that no extra storage is actually needed (that is, * the SQL command did not return tuples). * * We also malloc the top-level array of tuple pointers separately, because * we need to be able to enlarge it via realloc, and our trivial space * allocator doesn't handle that effectively. (Too bad the FE/BE protocol * doesn't tell us up front how many tuples will be returned.) * All other subsidiary storage for a PGresult is kept in PGresult_data blocks * of size PGRESULT_DATA_BLOCKSIZE. The overhead at the start of each block * is just a link to the next one, if any. Free-space management info is * kept in the owning PGresult. * A query returning a small amount of data will thus require three malloc * calls: one for the PGresult, one for the tuples pointer array, and one * PGresult_data block. * * Only the most recently allocated PGresult_data block is a candidate to * have more stuff added to it --- any extra space left over in older blocks * is wasted. We could be smarter and search the whole chain, but the point * here is to be simple and fast. Typical applications do not keep a PGresult * around very long anyway, so some wasted space within one is not a problem. * * Tuning constants for the space allocator are: * PGRESULT_DATA_BLOCKSIZE: size of a standard allocation block, in bytes * PGRESULT_ALIGN_BOUNDARY: assumed alignment requirement for binary data * PGRESULT_SEP_ALLOC_THRESHOLD: objects bigger than this are given separate * blocks, instead of being crammed into a regular allocation block. * Requirements for correct function are: * PGRESULT_ALIGN_BOUNDARY must be a multiple of the alignment requirements * of all machine data types. (Currently this is set from configure * tests, so it should be OK automatically.) * PGRESULT_SEP_ALLOC_THRESHOLD + PGRESULT_BLOCK_OVERHEAD <= * PGRESULT_DATA_BLOCKSIZE * pqResultAlloc assumes an object smaller than the threshold will fit * in a new block. * The amount of space wasted at the end of a block could be as much as * PGRESULT_SEP_ALLOC_THRESHOLD, so it doesn't pay to make that too large. * ---------------- */#define PGRESULT_DATA_BLOCKSIZE 2048#define PGRESULT_ALIGN_BOUNDARY MAXIMUM_ALIGNOF /* from configure */#define PGRESULT_BLOCK_OVERHEAD Max(sizeof(PGresult_data), PGRESULT_ALIGN_BOUNDARY)#define PGRESULT_SEP_ALLOC_THRESHOLD (PGRESULT_DATA_BLOCKSIZE / 2)/* * PQmakeEmptyPGresult * returns a newly allocated, initialized PGresult with given status. * If conn is not NULL and status indicates an error, the conn's * errorMessage is copied. * * Note this is exported --- you wouldn't think an application would need * to build its own PGresults, but this has proven useful in both libpgtcl * and the Perl5 interface, so maybe it's not so unreasonable. */PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status){ PGresult *result; result = (PGresult *) malloc(sizeof(PGresult)); if (!result) return NULL; result->ntups = 0; result->numAttributes = 0; result->attDescs = NULL; result->tuples = NULL; result->tupArrSize = 0; result->resultStatus = status; result->cmdStatus[0] = '\0'; result->binary = 0; result->errMsg = NULL; result->errFields = NULL; result->null_field[0] = '\0'; result->curBlock = NULL; result->curOffset = 0; result->spaceLeft = 0; if (conn) { /* copy connection data we might need for operations on PGresult */ result->noticeHooks = conn->noticeHooks; result->client_encoding = conn->client_encoding; /* consider copying conn's errorMessage */ switch (status) { case PGRES_EMPTY_QUERY: case PGRES_COMMAND_OK: case PGRES_TUPLES_OK: case PGRES_COPY_OUT: case PGRES_COPY_IN: /* non-error cases */ break; default: pqSetResultError(result, conn->errorMessage.data); break; } } else { /* defaults... */ result->noticeHooks.noticeRec = NULL; result->noticeHooks.noticeRecArg = NULL; result->noticeHooks.noticeProc = NULL; result->noticeHooks.noticeProcArg = NULL; result->client_encoding = PG_SQL_ASCII; } return result;}/* * pqResultAlloc - * Allocate subsidiary storage for a PGresult. * * nBytes is the amount of space needed for the object. * If isBinary is true, we assume that we need to align the object on * a machine allocation boundary. * If isBinary is false, we assume the object is a char string and can * be allocated on any byte boundary. */void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary){ char *space; PGresult_data *block; if (!res) return NULL; if (nBytes <= 0) return res->null_field; /* * If alignment is needed, round up the current position to an alignment * boundary. */ if (isBinary) { int offset = res->curOffset % PGRESULT_ALIGN_BOUNDARY; if (offset) { res->curOffset += PGRESULT_ALIGN_BOUNDARY - offset; res->spaceLeft -= PGRESULT_ALIGN_BOUNDARY - offset; } } /* If there's enough space in the current block, no problem. */ if (nBytes <= (size_t) res->spaceLeft) { space = res->curBlock->space + res->curOffset; res->curOffset += nBytes; res->spaceLeft -= nBytes; return space; } /* * If the requested object is very large, give it its own block; this * avoids wasting what might be most of the current block to start a new * block. (We'd have to special-case requests bigger than the block size * anyway.) The object is always given binary alignment in this case. */ if (nBytes >= PGRESULT_SEP_ALLOC_THRESHOLD) { block = (PGresult_data *) malloc(nBytes + PGRESULT_BLOCK_OVERHEAD); if (!block) return NULL; space = block->space + PGRESULT_BLOCK_OVERHEAD; if (res->curBlock) { /* * Tuck special block below the active block, so that we don't * have to waste the free space in the active block. */ block->next = res->curBlock->next; res->curBlock->next = block; } else { /* Must set up the new block as the first active block. */ block->next = NULL; res->curBlock = block; res->spaceLeft = 0; /* be sure it's marked full */ } return space; } /* Otherwise, start a new block. */ block = (PGresult_data *) malloc(PGRESULT_DATA_BLOCKSIZE); if (!block) return NULL; block->next = res->curBlock; res->curBlock = block; if (isBinary) { /* object needs full alignment */ res->curOffset = PGRESULT_BLOCK_OVERHEAD; res->spaceLeft = PGRESULT_DATA_BLOCKSIZE - PGRESULT_BLOCK_OVERHEAD; } else { /* we can cram it right after the overhead pointer */ res->curOffset = sizeof(PGresult_data); res->spaceLeft = PGRESULT_DATA_BLOCKSIZE - sizeof(PGresult_data); } space = block->space + res->curOffset; res->curOffset += nBytes; res->spaceLeft -= nBytes; return space;}/* * pqResultStrdup - * Like strdup, but the space is subsidiary PGresult space. */char *pqResultStrdup(PGresult *res, const char *str){ char *space = (char *) pqResultAlloc(res, strlen(str) + 1, FALSE); if (space) strcpy(space, str); return space;}/* * pqSetResultError - * assign a new error message to a PGresult */voidpqSetResultError(PGresult *res, const char *msg){ if (!res) return; if (msg && *msg) res->errMsg = pqResultStrdup(res, msg); else res->errMsg = NULL;}/* * pqCatenateResultError - * concatenate a new error message to the one already in a PGresult */voidpqCatenateResultError(PGresult *res, const char *msg){ PQExpBufferData errorBuf; if (!res || !msg) return; initPQExpBuffer(&errorBuf); if (res->errMsg) appendPQExpBufferStr(&errorBuf, res->errMsg); appendPQExpBufferStr(&errorBuf, msg); pqSetResultError(res, errorBuf.data); termPQExpBuffer(&errorBuf);}/* * PQclear - * free's the memory associated with a PGresult */voidPQclear(PGresult *res){ PGresult_data *block; if (!res) return; /* Free all the subsidiary blocks */ while ((block = res->curBlock) != NULL) { res->curBlock = block->next; free(block); } /* Free the top-level tuple pointer array */ if (res->tuples) free(res->tuples); /* Free the PGresult structure itself */ free(res);}/* * Handy subroutine to deallocate any partially constructed async result. */voidpqClearAsyncResult(PGconn *conn){ if (conn->result) PQclear(conn->result); conn->result = NULL; conn->curTuple = NULL;}/* * This subroutine deletes any existing async result, sets conn->result * to a PGresult with status PGRES_FATAL_ERROR, and stores the current * contents of conn->errorMessage into that result. It differs from a * plain call on PQmakeEmptyPGresult() in that if there is already an * async result with status PGRES_FATAL_ERROR, the current error message * is APPENDED to the old error message instead of replacing it. This * behavior lets us report multiple error conditions properly, if necessary. * (An example where this is needed is when the backend sends an 'E' message * and immediately closes the connection --- we want to report both the * backend error and the connection closure error.) */voidpqSaveErrorResult(PGconn *conn){ /* * If no old async result, just let PQmakeEmptyPGresult make one. Likewise * if old result is not an error message. */ if (conn->result == NULL || conn->result->resultStatus != PGRES_FATAL_ERROR || conn->result->errMsg == NULL) { pqClearAsyncResult(conn); conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR); } else { /* Else, concatenate error message to existing async result. */ pqCatenateResultError(conn->result, conn->errorMessage.data); }}/* * This subroutine prepares an async result object for return to the caller. * If there is not already an async result object, build an error object * using whatever is in conn->errorMessage. In any case, clear the async * result storage and make sure PQerrorMessage will agree with the result's * error string. */PGresult *pqPrepareAsyncResult(PGconn *conn){ PGresult *res; /* * conn->result is the PGresult to return. If it is NULL (which probably * shouldn't happen) we assume there is an appropriate error message in * conn->errorMessage. */ res = conn->result; conn->result = NULL; /* handing over ownership to caller */ conn->curTuple = NULL; /* just in case */ if (!res) res = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR); else { /* * Make sure PQerrorMessage agrees with result; it could be different * if we have concatenated messages. */ resetPQExpBuffer(&conn->errorMessage); appendPQExpBufferStr(&conn->errorMessage, PQresultErrorMessage(res)); } return res;}/* * pqInternalNotice - produce an internally-generated notice message * * A format string and optional arguments can be passed. Note that we do * libpq_gettext() here, so callers need not. * * The supplied text is taken as primary message (ie., it should not include * a trailing newline, and should not be more than one line). */voidpqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...){ char msgBuf[1024]; va_list args; PGresult *res; if (hooks->noticeRec == NULL) return; /* nobody home to receive notice? */ /* Format the message */ va_start(args, fmt); vsnprintf(msgBuf, sizeof(msgBuf), libpq_gettext(fmt), args); va_end(args); msgBuf[sizeof(msgBuf) - 1] = '\0'; /* make real sure it's terminated */ /* Make a PGresult to pass to the notice receiver */ res = PQmakeEmptyPGresult(NULL, PGRES_NONFATAL_ERROR); if (!res) return; res->noticeHooks = *hooks; /* * Set up fields of notice. */ pqSaveMessageField(res, PG_DIAG_MESSAGE_PRIMARY, msgBuf); pqSaveMessageField(res, PG_DIAG_SEVERITY, libpq_gettext("NOTICE")); /* XXX should provide a SQLSTATE too? */ /* * Result text is always just the primary message + newline. If we can't * allocate it, don't bother invoking the receiver. */ res->errMsg = (char *) pqResultAlloc(res, strlen(msgBuf) + 2, FALSE); if (res->errMsg) { sprintf(res->errMsg, "%s\n", msgBuf); /* * Pass to receiver, then free it. */ (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); } PQclear(res);}/* * pqAddTuple * add a row pointer to the PGresult structure, growing it if necessary * Returns TRUE if OK, FALSE if not enough memory to add the row */intpqAddTuple(PGresult *res, PGresAttValue *tup){ if (res->ntups >= res->tupArrSize) { /* * Try to grow the array. * * We can use realloc because shallow copying of the structure is * okay. Note that the first time through, res->tuples is NULL. While * ANSI says that realloc() should act like malloc() in that case, * some old C libraries (like SunOS 4.1.x) coredump instead. On * failure realloc is supposed to return NULL without damaging the * existing allocation. Note that the positions beyond res->ntups are * garbage, not necessarily NULL. */ int newSize = (res->tupArrSize > 0) ? res->tupArrSize * 2 : 128; PGresAttValue **newTuples; if (res->tuples == NULL) newTuples = (PGresAttValue **) malloc(newSize * sizeof(PGresAttValue *));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -