1 /*-------------------------------------------------------------------------
4 * functions that are specific to frontend/backend protocol version 2
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/interfaces/libpq/fe-protocol2.c,v 1.22 2006/03/05 15:59:09 momjian Exp $
13 *-------------------------------------------------------------------------
15 #include "postgres_fe.h"
22 #include "libpq-int.h"
24 #include "mb/pg_wchar.h"
30 #include <netinet/in.h>
31 #ifdef HAVE_NETINET_TCP_H
32 #include <netinet/tcp.h>
34 #include <arpa/inet.h>
38 static int getRowDescriptions(PGconn *conn);
39 static int getAnotherTuple(PGconn *conn, bool binary);
40 static int pqGetErrorNotice2(PGconn *conn, bool isError);
41 static void checkXactStatus(PGconn *conn, const char *cmdTag);
42 static int getNotify(PGconn *conn);
48 * Polls the process of passing the values of a standard set of environment
49 * variables to the backend.
51 PostgresPollingStatusType
52 pqSetenvPoll(PGconn *conn)
56 if (conn == NULL || conn->status == CONNECTION_BAD)
57 return PGRES_POLLING_FAILED;
59 /* Check whether there are any data for us */
60 switch (conn->setenv_state)
62 /* These are reading states */
63 case SETENV_STATE_OPTION_WAIT:
64 case SETENV_STATE_QUERY1_WAIT:
65 case SETENV_STATE_QUERY2_WAIT:
67 /* Load waiting data */
68 int n = pqReadData(conn);
73 return PGRES_POLLING_READING;
78 /* These are writing states, so we just proceed. */
79 case SETENV_STATE_OPTION_SEND:
80 case SETENV_STATE_QUERY1_SEND:
81 case SETENV_STATE_QUERY2_SEND:
84 /* Should we raise an error if called when not active? */
85 case SETENV_STATE_IDLE:
86 return PGRES_POLLING_OK;
89 printfPQExpBuffer(&conn->errorMessage,
91 "invalid setenv state %c, "
92 "probably indicative of memory corruption\n"
98 /* We will loop here until there is nothing left to do in this call. */
101 switch (conn->setenv_state)
103 case SETENV_STATE_OPTION_SEND:
106 * Send SET commands for stuff directed by Environment
107 * Options. Note: we assume that SET commands won't start
108 * transaction blocks, even in a 7.3 server with
111 char setQuery[100]; /* note length limit in
114 if (conn->next_eo->envName)
118 if ((val = getenv(conn->next_eo->envName)))
120 if (pg_strcasecmp(val, "default") == 0)
121 sprintf(setQuery, "SET %s = DEFAULT",
122 conn->next_eo->pgName);
124 sprintf(setQuery, "SET %s = '%.60s'",
125 conn->next_eo->pgName, val);
128 "Use environment variable %s to send %s\n",
129 conn->next_eo->envName, setQuery);
131 if (!PQsendQuery(conn, setQuery))
134 conn->setenv_state = SETENV_STATE_OPTION_WAIT;
141 /* No more options to send, so move on to querying */
142 conn->setenv_state = SETENV_STATE_QUERY1_SEND;
147 case SETENV_STATE_OPTION_WAIT:
150 return PGRES_POLLING_READING;
152 res = PQgetResult(conn);
156 if (PQresultStatus(res) != PGRES_COMMAND_OK)
162 /* Keep reading until PQgetResult returns NULL */
166 /* Query finished, so send the next option */
168 conn->setenv_state = SETENV_STATE_OPTION_SEND;
173 case SETENV_STATE_QUERY1_SEND:
176 * Issue query to get information we need. Here we must
177 * use begin/commit in case autocommit is off by default
180 * Note: version() exists in all protocol-2.0-supporting
181 * backends. In 7.3 it would be safer to write
182 * pg_catalog.version(), but we can't do that without
183 * causing problems on older versions.
185 if (!PQsendQuery(conn, "begin; select version(); end"))
188 conn->setenv_state = SETENV_STATE_QUERY1_WAIT;
189 return PGRES_POLLING_READING;
192 case SETENV_STATE_QUERY1_WAIT:
195 return PGRES_POLLING_READING;
197 res = PQgetResult(conn);
203 if (PQresultStatus(res) == PGRES_COMMAND_OK)
205 /* ignore begin/commit command results */
210 if (PQresultStatus(res) != PGRES_TUPLES_OK ||
218 * Extract server version and save as if
221 val = PQgetvalue(res, 0, 0);
222 if (val && strncmp(val, "PostgreSQL ", 11) == 0)
226 /* strip off PostgreSQL part */
230 * strip off platform part (scribbles on result,
233 ptr = strchr(val, ' ');
237 pqSaveParameterStatus(conn, "server_version",
242 /* Keep reading until PQgetResult returns NULL */
246 /* Query finished, move to next */
247 conn->setenv_state = SETENV_STATE_QUERY2_SEND;
252 case SETENV_STATE_QUERY2_SEND:
257 * pg_client_encoding does not exist in pre-7.2 servers.
258 * So we need to be prepared for an error here. Do *not*
259 * start a transaction block, except in 7.3 servers where
260 * we need to prevent autocommit-off from starting a
261 * transaction anyway.
263 if (conn->sversion >= 70300 &&
264 conn->sversion < 70400)
265 query = "begin; select pg_catalog.pg_client_encoding(); end";
267 query = "select pg_client_encoding()";
268 if (!PQsendQuery(conn, query))
271 conn->setenv_state = SETENV_STATE_QUERY2_WAIT;
272 return PGRES_POLLING_READING;
275 case SETENV_STATE_QUERY2_WAIT:
278 return PGRES_POLLING_READING;
280 res = PQgetResult(conn);
286 if (PQresultStatus(res) == PGRES_COMMAND_OK)
288 /* ignore begin/commit command results */
293 if (PQresultStatus(res) == PGRES_TUPLES_OK &&
296 /* Extract client encoding and save it */
297 val = PQgetvalue(res, 0, 0);
298 if (val && *val) /* null should not happen, but */
299 pqSaveParameterStatus(conn, "client_encoding",
305 * Error: presumably function not available, so
306 * use PGCLIENTENCODING or SQL_ASCII as the
309 val = getenv("PGCLIENTENCODING");
311 pqSaveParameterStatus(conn, "client_encoding",
314 pqSaveParameterStatus(conn, "client_encoding",
319 /* Keep reading until PQgetResult returns NULL */
323 /* Query finished, so we're done */
324 conn->setenv_state = SETENV_STATE_IDLE;
325 return PGRES_POLLING_OK;
331 printfPQExpBuffer(&conn->errorMessage,
332 libpq_gettext("invalid state %c, "
333 "probably indicative of memory corruption\n"),
342 conn->setenv_state = SETENV_STATE_IDLE;
343 return PGRES_POLLING_FAILED;
348 * parseInput: if appropriate, parse input data from backend
349 * until input is exhausted or a stopping state is reached.
350 * Note that this function will NOT attempt to read more data from the backend.
353 pqParseInput2(PGconn *conn)
358 * Loop to parse successive complete messages available in the buffer.
363 * Quit if in COPY_OUT state: we expect raw data from the server until
364 * PQendcopy is called. Don't try to parse it according to the normal
365 * protocol. (This is bogus. The data lines ought to be part of the
366 * protocol and have identifying leading characters.)
368 if (conn->asyncStatus == PGASYNC_COPY_OUT)
372 * OK to try to read a message type code.
374 conn->inCursor = conn->inStart;
375 if (pqGetc(&id, conn))
379 * NOTIFY and NOTICE messages can happen in any state besides COPY
380 * OUT; always process them right away.
382 * Most other messages should only be processed while in BUSY state.
383 * (In particular, in READY state we hold off further parsing until
384 * the application collects the current PGresult.)
386 * However, if the state is IDLE then we got trouble; we need to deal
387 * with the unexpected message somehow.
396 if (pqGetErrorNotice2(conn, false))
399 else if (conn->asyncStatus != PGASYNC_BUSY)
401 /* If not IDLE state, just wait ... */
402 if (conn->asyncStatus != PGASYNC_IDLE)
406 * Unexpected message in IDLE state; need to recover somehow.
407 * ERROR messages are displayed using the notice processor;
408 * anything else is just dropped on the floor after displaying a
409 * suitable warning notice. (An ERROR is very possibly the
410 * backend telling us why it is about to close the connection, so
411 * we don't want to just discard it...)
415 if (pqGetErrorNotice2(conn, false /* treat as notice */ ))
420 pqInternalNotice(&conn->noticeHooks,
421 "message type 0x%02x arrived from server while idle",
423 /* Discard the unexpected message; good idea?? */
424 conn->inStart = conn->inEnd;
431 * In BUSY state, we can process everything.
435 case 'C': /* command complete */
436 if (pqGets(&conn->workBuffer, conn))
438 if (conn->result == NULL)
440 conn->result = PQmakeEmptyPGresult(conn,
445 strncpy(conn->result->cmdStatus, conn->workBuffer.data,
447 checkXactStatus(conn, conn->workBuffer.data);
448 conn->asyncStatus = PGASYNC_READY;
450 case 'E': /* error return */
451 if (pqGetErrorNotice2(conn, true))
453 conn->asyncStatus = PGASYNC_READY;
455 case 'Z': /* backend is ready for new query */
456 conn->asyncStatus = PGASYNC_IDLE;
458 case 'I': /* empty query */
459 /* read and throw away the closing '\0' */
460 if (pqGetc(&id, conn))
463 pqInternalNotice(&conn->noticeHooks,
464 "unexpected character %c following empty query response (\"I\" message)",
466 if (conn->result == NULL)
467 conn->result = PQmakeEmptyPGresult(conn,
469 conn->asyncStatus = PGASYNC_READY;
471 case 'K': /* secret key data from the backend */
474 * This is expected only during backend startup, but it's
475 * just as easy to handle it as part of the main loop.
476 * Save the data and continue processing.
478 if (pqGetInt(&(conn->be_pid), 4, conn))
480 if (pqGetInt(&(conn->be_key), 4, conn))
483 case 'P': /* synchronous (normal) portal */
484 if (pqGets(&conn->workBuffer, conn))
486 /* We pretty much ignore this message type... */
488 case 'T': /* row descriptions (start of query results) */
489 if (conn->result == NULL)
491 /* First 'T' in a query sequence */
492 if (getRowDescriptions(conn))
498 * A new 'T' message is treated as the start of
499 * another PGresult. (It is not clear that this is
500 * really possible with the current backend.) We stop
501 * parsing until the application accepts the current
504 conn->asyncStatus = PGASYNC_READY;
508 case 'D': /* ASCII data tuple */
509 if (conn->result != NULL)
511 /* Read another tuple of a normal query response */
512 if (getAnotherTuple(conn, FALSE))
517 pqInternalNotice(&conn->noticeHooks,
518 "server sent data (\"D\" message) without prior row description (\"T\" message)");
519 /* Discard the unexpected message; good idea?? */
520 conn->inStart = conn->inEnd;
524 case 'B': /* Binary data tuple */
525 if (conn->result != NULL)
527 /* Read another tuple of a normal query response */
528 if (getAnotherTuple(conn, TRUE))
533 pqInternalNotice(&conn->noticeHooks,
534 "server sent binary data (\"B\" message) without prior row description (\"T\" message)");
535 /* Discard the unexpected message; good idea?? */
536 conn->inStart = conn->inEnd;
540 case 'G': /* Start Copy In */
541 conn->asyncStatus = PGASYNC_COPY_IN;
543 case 'H': /* Start Copy Out */
544 conn->asyncStatus = PGASYNC_COPY_OUT;
547 printfPQExpBuffer(&conn->errorMessage,
549 "unexpected response from server; first received character was \"%c\"\n"),
551 /* build an error result holding the error message */
552 pqSaveErrorResult(conn);
553 /* Discard the unexpected message; good idea?? */
554 conn->inStart = conn->inEnd;
555 conn->asyncStatus = PGASYNC_READY;
557 } /* switch on protocol character */
559 /* Successfully consumed this message */
560 conn->inStart = conn->inCursor;
565 * parseInput subroutine to read a 'T' (row descriptions) message.
566 * We build a PGresult structure containing the attribute data.
567 * Returns: 0 if completed message, EOF if not enough data yet.
569 * Note that if we run out of data, we have to release the partially
570 * constructed PGresult, and rebuild it again next time. Fortunately,
571 * that shouldn't happen often, since 'T' messages usually fit in a packet.
574 getRowDescriptions(PGconn *conn)
576 PGresult *result = NULL;
580 result = PQmakeEmptyPGresult(conn, PGRES_TUPLES_OK);
584 /* parseInput already read the 'T' label. */
585 /* the next two bytes are the number of fields */
586 if (pqGetInt(&(result->numAttributes), 2, conn))
588 nfields = result->numAttributes;
590 /* allocate space for the attribute descriptors */
593 result->attDescs = (PGresAttDesc *)
594 pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
595 if (!result->attDescs)
597 MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
601 for (i = 0; i < nfields; i++)
607 if (pqGets(&conn->workBuffer, conn) ||
608 pqGetInt(&typid, 4, conn) ||
609 pqGetInt(&typlen, 2, conn) ||
610 pqGetInt(&atttypmod, 4, conn))
614 * Since pqGetInt treats 2-byte integers as unsigned, we need to
615 * coerce the result to signed form.
617 typlen = (int) ((int16) typlen);
619 result->attDescs[i].name = pqResultStrdup(result,
620 conn->workBuffer.data);
621 if (!result->attDescs[i].name)
623 result->attDescs[i].tableid = 0;
624 result->attDescs[i].columnid = 0;
625 result->attDescs[i].format = 0;
626 result->attDescs[i].typid = typid;
627 result->attDescs[i].typlen = typlen;
628 result->attDescs[i].atttypmod = atttypmod;
632 conn->result = result;
642 * parseInput subroutine to read a 'B' or 'D' (row data) message.
643 * We add another tuple to the existing PGresult structure.
644 * Returns: 0 if completed message, EOF if error or not enough data yet.
646 * Note that if we run out of data, we have to suspend and reprocess
647 * the message after more data is received. We keep a partially constructed
648 * tuple in conn->curTuple, and avoid reallocating already-allocated storage.
651 getAnotherTuple(PGconn *conn, bool binary)
653 PGresult *result = conn->result;
654 int nfields = result->numAttributes;
657 /* the backend sends us a bitmap of which attributes are null */
658 char std_bitmap[64]; /* used unless it doesn't fit */
659 char *bitmap = std_bitmap;
661 size_t nbytes; /* the number of bytes in bitmap */
662 char bmap; /* One byte of the bitmap */
663 int bitmap_index; /* Its index */
664 int bitcnt; /* number of bits examined in current byte */
665 int vlen; /* length of the current field value */
667 result->binary = binary;
669 /* Allocate tuple space if first time for this data message */
670 if (conn->curTuple == NULL)
672 conn->curTuple = (PGresAttValue *)
673 pqResultAlloc(result, nfields * sizeof(PGresAttValue), TRUE);
674 if (conn->curTuple == NULL)
676 MemSet(conn->curTuple, 0, nfields * sizeof(PGresAttValue));
679 * If it's binary, fix the column format indicators. We assume the
680 * backend will consistently send either B or D, not a mix.
684 for (i = 0; i < nfields; i++)
685 result->attDescs[i].format = 1;
688 tup = conn->curTuple;
690 /* Get the null-value bitmap */
691 nbytes = (nfields + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
692 /* malloc() only for unusually large field counts... */
693 if (nbytes > sizeof(std_bitmap))
695 bitmap = (char *) malloc(nbytes);
700 if (pqGetnchar(bitmap, nbytes, conn))
703 /* Scan the fields */
705 bmap = bitmap[bitmap_index];
708 for (i = 0; i < nfields; i++)
712 /* if the field value is absent, make it a null string */
713 tup[i].value = result->null_field;
714 tup[i].len = NULL_LEN;
718 /* get the value length (the first four bytes are for length) */
719 if (pqGetInt(&vlen, 4, conn))
725 if (tup[i].value == NULL)
727 tup[i].value = (char *) pqResultAlloc(result, vlen + 1, binary);
728 if (tup[i].value == NULL)
732 /* read in the value */
734 if (pqGetnchar((char *) (tup[i].value), vlen, conn))
736 /* we have to terminate this ourselves */
737 tup[i].value[vlen] = '\0';
739 /* advance the bitmap stuff */
741 if (bitcnt == BITS_PER_BYTE)
744 bmap = bitmap[bitmap_index];
751 /* Success! Store the completed tuple in the result */
752 if (!pqAddTuple(result, tup))
754 /* and reset for a new message */
755 conn->curTuple = NULL;
757 if (bitmap != std_bitmap)
762 /* Replace partially constructed result with an error result */
765 * we do NOT use pqSaveErrorResult() here, because of the likelihood that
766 * there's not enough memory to concatenate messages...
768 pqClearAsyncResult(conn);
769 printfPQExpBuffer(&conn->errorMessage,
770 libpq_gettext("out of memory for query result\n"));
773 * XXX: if PQmakeEmptyPGresult() fails, there's probably not much we can
776 conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
777 conn->asyncStatus = PGASYNC_READY;
778 /* Discard the failed message --- good idea? */
779 conn->inStart = conn->inEnd;
782 if (bitmap != NULL && bitmap != std_bitmap)
789 * Attempt to read an Error or Notice response message.
790 * This is possible in several places, so we break it out as a subroutine.
791 * Entry: 'E' or 'N' message type has already been consumed.
792 * Exit: returns 0 if successfully consumed message.
793 * returns EOF if not enough data.
796 pqGetErrorNotice2(PGconn *conn, bool isError)
798 PGresult *res = NULL;
799 PQExpBufferData workBuf;
804 * Since the message might be pretty long, we create a temporary
805 * PQExpBuffer rather than using conn->workBuffer. workBuffer is intended
806 * for stuff that is expected to be short.
808 initPQExpBuffer(&workBuf);
809 if (pqGets(&workBuf, conn))
813 * Make a PGresult to hold the message. We temporarily lie about the
814 * result status, so that PQmakeEmptyPGresult doesn't uselessly copy
815 * conn->errorMessage.
817 res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
820 res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR;
821 res->errMsg = pqResultStrdup(res, workBuf.data);
826 * Break the message into fields. We can't do very much here, but we can
827 * split the severity code off, and remove trailing newlines. Also, we use
828 * the heuristic that the primary message extends only to the first
829 * newline --- anything after that is detail message. (In some cases it'd
830 * be better classed as hint, but we can hardly be expected to guess that
833 while (workBuf.len > 0 && workBuf.data[workBuf.len - 1] == '\n')
834 workBuf.data[--workBuf.len] = '\0';
835 splitp = strstr(workBuf.data, ": ");
838 /* what comes before the colon is severity */
840 pqSaveMessageField(res, PG_DIAG_SEVERITY, workBuf.data);
845 /* can't find a colon? oh well... */
846 startp = workBuf.data;
848 splitp = strchr(startp, '\n');
851 /* what comes before the newline is primary message */
853 pqSaveMessageField(res, PG_DIAG_MESSAGE_PRIMARY, startp);
854 /* the rest is detail; strip any leading whitespace */
855 while (*splitp && isspace((unsigned char) *splitp))
857 pqSaveMessageField(res, PG_DIAG_MESSAGE_DETAIL, splitp);
861 /* single-line message, so all primary */
862 pqSaveMessageField(res, PG_DIAG_MESSAGE_PRIMARY, startp);
866 * Either save error as current async result, or just emit the notice.
867 * Also, if it's an error and we were in a transaction block, assume the
868 * server has now gone to error-in-transaction state.
872 pqClearAsyncResult(conn);
874 resetPQExpBuffer(&conn->errorMessage);
875 appendPQExpBufferStr(&conn->errorMessage, res->errMsg);
876 if (conn->xactStatus == PQTRANS_INTRANS)
877 conn->xactStatus = PQTRANS_INERROR;
881 if (res->noticeHooks.noticeRec != NULL)
882 (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
886 termPQExpBuffer(&workBuf);
892 termPQExpBuffer(&workBuf);
897 * checkXactStatus - attempt to track transaction-block status of server
899 * This is called each time we receive a command-complete message. By
900 * watching for messages from BEGIN/COMMIT/ROLLBACK commands, we can do
901 * a passable job of tracking the server's xact status. BUT: this does
902 * not work at all on 7.3 servers with AUTOCOMMIT OFF. (Man, was that
903 * feature ever a mistake.) Caveat user.
905 * The tags known here are all those used as far back as 7.0; is it worth
906 * adding those from even-older servers?
909 checkXactStatus(PGconn *conn, const char *cmdTag)
911 if (strcmp(cmdTag, "BEGIN") == 0)
912 conn->xactStatus = PQTRANS_INTRANS;
913 else if (strcmp(cmdTag, "COMMIT") == 0)
914 conn->xactStatus = PQTRANS_IDLE;
915 else if (strcmp(cmdTag, "ROLLBACK") == 0)
916 conn->xactStatus = PQTRANS_IDLE;
917 else if (strcmp(cmdTag, "START TRANSACTION") == 0) /* 7.3 only */
918 conn->xactStatus = PQTRANS_INTRANS;
921 * Normally we get into INERROR state by detecting an Error message.
922 * However, if we see one of these tags then we know for sure the server
923 * is in abort state ...
925 else if (strcmp(cmdTag, "*ABORT STATE*") == 0) /* pre-7.3 only */
926 conn->xactStatus = PQTRANS_INERROR;
930 * Attempt to read a Notify response message.
931 * This is possible in several places, so we break it out as a subroutine.
932 * Entry: 'A' message type and length have already been consumed.
933 * Exit: returns 0 if successfully consumed Notify message.
934 * returns EOF if not enough data.
937 getNotify(PGconn *conn)
943 if (pqGetInt(&be_pid, 4, conn))
945 if (pqGets(&conn->workBuffer, conn))
949 * Store the relation name right after the PQnotify structure so it can
950 * all be freed at once. We don't use NAMEDATALEN because we don't want
951 * to tie this interface to a specific server name length.
953 nmlen = strlen(conn->workBuffer.data);
954 newNotify = (PGnotify *) malloc(sizeof(PGnotify) + nmlen + 1);
957 newNotify->relname = (char *) newNotify + sizeof(PGnotify);
958 strcpy(newNotify->relname, conn->workBuffer.data);
959 /* fake up an empty-string extra field */
960 newNotify->extra = newNotify->relname + nmlen;
961 newNotify->be_pid = be_pid;
962 newNotify->next = NULL;
963 if (conn->notifyTail)
964 conn->notifyTail->next = newNotify;
966 conn->notifyHead = newNotify;
967 conn->notifyTail = newNotify;
975 * PQgetCopyData - read a row of data from the backend during COPY OUT
977 * If successful, sets *buffer to point to a malloc'd row of data, and
978 * returns row length (always > 0) as result.
979 * Returns 0 if no row available yet (only possible if async is true),
980 * -1 if end of copy (consult PQgetResult), or -2 if error (consult
984 pqGetCopyData2(PGconn *conn, char **buffer, int async)
992 * Do we have a complete line of data?
994 conn->inCursor = conn->inStart;
996 while (conn->inCursor < conn->inEnd)
998 char c = conn->inBuffer[conn->inCursor++];
1008 msgLength = conn->inCursor - conn->inStart;
1011 * If it's the end-of-data marker, consume it, exit COPY_OUT mode, and
1012 * let caller read status with PQgetResult().
1014 if (msgLength == 3 &&
1015 strncmp(&conn->inBuffer[conn->inStart], "\\.\n", 3) == 0)
1017 conn->inStart = conn->inCursor;
1018 conn->asyncStatus = PGASYNC_BUSY;
1023 * Pass the line back to the caller.
1025 *buffer = (char *) malloc(msgLength + 1);
1026 if (*buffer == NULL)
1028 printfPQExpBuffer(&conn->errorMessage,
1029 libpq_gettext("out of memory\n"));
1032 memcpy(*buffer, &conn->inBuffer[conn->inStart], msgLength);
1033 (*buffer)[msgLength] = '\0'; /* Add terminating null */
1035 /* Mark message consumed */
1036 conn->inStart = conn->inCursor;
1041 /* Don't block if async read requested */
1044 /* Need to load more data */
1045 if (pqWait(TRUE, FALSE, conn) ||
1046 pqReadData(conn) < 0)
1053 * PQgetline - gets a newline-terminated string from the backend.
1055 * See fe-exec.c for documentation.
1058 pqGetline2(PGconn *conn, char *s, int maxlen)
1060 int result = 1; /* return value if buffer overflows */
1069 * Since this is a purely synchronous routine, we don't bother to maintain
1070 * conn->inCursor; there is no need to back up.
1074 if (conn->inStart < conn->inEnd)
1076 char c = conn->inBuffer[conn->inStart++];
1080 result = 0; /* success exit */
1088 /* need to load more data */
1089 if (pqWait(TRUE, FALSE, conn) ||
1090 pqReadData(conn) < 0)
1103 * PQgetlineAsync - gets a COPY data row without blocking.
1105 * See fe-exec.c for documentation.
1108 pqGetlineAsync2(PGconn *conn, char *buffer, int bufsize)
1112 if (conn->asyncStatus != PGASYNC_COPY_OUT)
1113 return -1; /* we are not doing a copy... */
1116 * Move data from libpq's buffer to the caller's. We want to accept data
1117 * only in units of whole lines, not partial lines. This ensures that we
1118 * can recognize the terminator line "\\.\n". (Otherwise, if it happened
1119 * to cross a packet/buffer boundary, we might hand the first one or two
1120 * characters off to the caller, which we shouldn't.)
1123 conn->inCursor = conn->inStart;
1126 while (avail > 0 && conn->inCursor < conn->inEnd)
1128 char c = conn->inBuffer[conn->inCursor++];
1134 /* Got a complete line; mark the data removed from libpq */
1135 conn->inStart = conn->inCursor;
1136 /* Is it the endmarker line? */
1137 if (bufsize - avail == 3 && buffer[-3] == '\\' && buffer[-2] == '.')
1139 /* No, return the data line to the caller */
1140 return bufsize - avail;
1145 * We don't have a complete line. We'd prefer to leave it in libpq's
1146 * buffer until the rest arrives, but there is a special case: what if the
1147 * line is longer than the buffer the caller is offering us? In that case
1148 * we'd better hand over a partial line, else we'd get into an infinite
1149 * loop. Do this in a way that ensures we can't misrecognize a terminator
1150 * line later: leave last 3 characters in libpq buffer.
1152 if (avail == 0 && bufsize > 3)
1154 conn->inStart = conn->inCursor - 3;
1163 * See fe-exec.c for documentation.
1166 pqEndcopy2(PGconn *conn)
1170 if (conn->asyncStatus != PGASYNC_COPY_IN &&
1171 conn->asyncStatus != PGASYNC_COPY_OUT)
1173 printfPQExpBuffer(&conn->errorMessage,
1174 libpq_gettext("no COPY in progress\n"));
1179 * make sure no data is waiting to be sent, abort if we are non-blocking
1180 * and the flush fails
1182 if (pqFlush(conn) && pqIsnonblocking(conn))
1185 /* non blocking connections may have to abort at this point. */
1186 if (pqIsnonblocking(conn) && PQisBusy(conn))
1189 /* Return to active duty */
1190 conn->asyncStatus = PGASYNC_BUSY;
1191 resetPQExpBuffer(&conn->errorMessage);
1193 /* Wait for the completion response */
1194 result = PQgetResult(conn);
1196 /* Expecting a successful result */
1197 if (result && result->resultStatus == PGRES_COMMAND_OK)
1204 * Trouble. For backwards-compatibility reasons, we issue the error
1205 * message as if it were a notice (would be nice to get rid of this
1206 * silliness, but too many apps probably don't handle errors from
1207 * PQendcopy reasonably). Note that the app can still obtain the error
1208 * status from the PGconn object.
1210 if (conn->errorMessage.len > 0)
1212 /* We have to strip the trailing newline ... pain in neck... */
1213 char svLast = conn->errorMessage.data[conn->errorMessage.len - 1];
1216 conn->errorMessage.data[conn->errorMessage.len - 1] = '\0';
1217 pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
1218 conn->errorMessage.data[conn->errorMessage.len - 1] = svLast;
1224 * The worst case is that we've lost sync with the backend entirely due to
1225 * application screwup of the copy in/out protocol. To recover, reset the
1226 * connection (talk about using a sledgehammer...)
1228 pqInternalNotice(&conn->noticeHooks,
1229 "lost synchronization with server, resetting connection");
1232 * Users doing non-blocking connections need to handle the reset
1233 * themselves, they'll need to check the connection status if we return an
1236 if (pqIsnonblocking(conn))
1246 * PQfn - Send a function call to the POSTGRES backend.
1248 * See fe-exec.c for documentation.
1251 pqFunctionCall2(PGconn *conn, Oid fnid,
1252 int *result_buf, int *actual_result_len,
1254 const PQArgBlock *args, int nargs)
1256 bool needInput = false;
1257 ExecStatusType status = PGRES_FATAL_ERROR;
1261 /* PQfn already validated connection state */
1263 if (pqPutMsgStart('F', false, conn) < 0 || /* function call msg */
1264 pqPuts(" ", conn) < 0 || /* dummy string */
1265 pqPutInt(fnid, 4, conn) != 0 || /* function id */
1266 pqPutInt(nargs, 4, conn) != 0) /* # of args */
1268 pqHandleSendFailure(conn);
1272 for (i = 0; i < nargs; ++i)
1273 { /* len.int4 + contents */
1274 if (pqPutInt(args[i].len, 4, conn))
1276 pqHandleSendFailure(conn);
1282 if (pqPutInt(args[i].u.integer, 4, conn))
1284 pqHandleSendFailure(conn);
1290 if (pqPutnchar((char *) args[i].u.ptr, args[i].len, conn))
1292 pqHandleSendFailure(conn);
1298 if (pqPutMsgEnd(conn) < 0 ||
1301 pqHandleSendFailure(conn);
1309 /* Wait for some data to arrive (or for the channel to close) */
1310 if (pqWait(TRUE, FALSE, conn) ||
1311 pqReadData(conn) < 0)
1316 * Scan the message. If we run out of data, loop around to try again.
1318 conn->inCursor = conn->inStart;
1321 if (pqGetc(&id, conn))
1325 * We should see V or E response to the command, but might get N
1326 * and/or A notices first. We also need to swallow the final Z before
1331 case 'V': /* function result */
1332 if (pqGetc(&id, conn))
1336 /* function returned nonempty value */
1337 if (pqGetInt(actual_result_len, 4, conn))
1341 if (pqGetInt(result_buf, 4, conn))
1346 if (pqGetnchar((char *) result_buf,
1351 if (pqGetc(&id, conn)) /* get the last '0' */
1356 /* correctly finished function result message */
1357 status = PGRES_COMMAND_OK;
1361 /* The backend violates the protocol. */
1362 printfPQExpBuffer(&conn->errorMessage,
1363 libpq_gettext("protocol error: id=0x%x\n"),
1365 pqSaveErrorResult(conn);
1366 conn->inStart = conn->inCursor;
1367 return pqPrepareAsyncResult(conn);
1370 case 'E': /* error return */
1371 if (pqGetErrorNotice2(conn, true))
1373 status = PGRES_FATAL_ERROR;
1375 case 'A': /* notify message */
1376 /* handle notify and go back to processing return values */
1377 if (getNotify(conn))
1380 case 'N': /* notice */
1381 /* handle notice and go back to processing return values */
1382 if (pqGetErrorNotice2(conn, false))
1385 case 'Z': /* backend is ready for new query */
1386 /* consume the message and exit */
1387 conn->inStart = conn->inCursor;
1388 /* if we saved a result object (probably an error), use it */
1390 return pqPrepareAsyncResult(conn);
1391 return PQmakeEmptyPGresult(conn, status);
1393 /* The backend violates the protocol. */
1394 printfPQExpBuffer(&conn->errorMessage,
1395 libpq_gettext("protocol error: id=0x%x\n"),
1397 pqSaveErrorResult(conn);
1398 conn->inStart = conn->inCursor;
1399 return pqPrepareAsyncResult(conn);
1401 /* Completed this message, keep going */
1402 conn->inStart = conn->inCursor;
1407 * We fall out of the loop only upon failing to read data.
1408 * conn->errorMessage has been set by pqWait or pqReadData. We want to
1409 * append it to any already-received error message.
1411 pqSaveErrorResult(conn);
1412 return pqPrepareAsyncResult(conn);
1417 * Construct startup packet
1419 * Returns a malloc'd packet buffer, or NULL if out of memory
1422 pqBuildStartupPacket2(PGconn *conn, int *packetlen,
1423 const PQEnvironmentOption *options)
1425 StartupPacket *startpacket;
1427 *packetlen = sizeof(StartupPacket);
1428 startpacket = (StartupPacket *) malloc(sizeof(StartupPacket));
1432 MemSet(startpacket, 0, sizeof(StartupPacket));
1434 startpacket->protoVersion = htonl(conn->pversion);
1436 strncpy(startpacket->user, conn->pguser, SM_USER);
1437 strncpy(startpacket->database, conn->dbName, SM_DATABASE);
1438 strncpy(startpacket->tty, conn->pgtty, SM_TTY);
1440 if (conn->pgoptions)
1441 strncpy(startpacket->options, conn->pgoptions, SM_OPTIONS);
1443 return (char *) startpacket;