1 /*-------------------------------------------------------------------------
4 * functions that are specific to frontend/backend protocol version 3
6 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/interfaces/libpq/fe-protocol3.c
13 *-------------------------------------------------------------------------
15 #include "postgres_fe.h"
24 #ifdef HAVE_NETINET_TCP_H
25 #include <netinet/tcp.h>
30 #include "libpq-int.h"
31 #include "mb/pg_wchar.h"
32 #include "port/pg_bswap.h"
35 * This macro lists the backend message types that could be "long" (more
36 * than a couple of kilobytes).
38 #define VALID_LONG_MESSAGE_TYPE(id) \
39 ((id) == 'T' || (id) == 'D' || (id) == 'd' || (id) == 'V' || \
40 (id) == 'E' || (id) == 'N' || (id) == 'A')
43 static void handleSyncLoss(PGconn
*conn
, char id
, int msgLength
);
44 static int getRowDescriptions(PGconn
*conn
, int msgLength
);
45 static int getParamDescriptions(PGconn
*conn
, int msgLength
);
46 static int getAnotherTuple(PGconn
*conn
, int msgLength
);
47 static int getParameterStatus(PGconn
*conn
);
48 static int getNotify(PGconn
*conn
);
49 static int getCopyStart(PGconn
*conn
, ExecStatusType copytype
);
50 static int getReadyForQuery(PGconn
*conn
);
51 static void reportErrorPosition(PQExpBuffer msg
, const char *query
,
52 int loc
, int encoding
);
53 static int build_startup_packet(const PGconn
*conn
, char *packet
,
54 const PQEnvironmentOption
*options
);
58 * parseInput: if appropriate, parse input data from backend
59 * until input is exhausted or a stopping state is reached.
60 * Note that this function will NOT attempt to read more data from the backend.
63 pqParseInput3(PGconn
*conn
)
70 * Loop to parse successive complete messages available in the buffer.
75 * Try to read a message. First get the type code and length. Return
78 conn
->inCursor
= conn
->inStart
;
79 if (pqGetc(&id
, conn
))
81 if (pqGetInt(&msgLength
, 4, conn
))
85 * Try to validate message type/length here. A length less than 4 is
86 * definitely broken. Large lengths should only be believed for a few
91 handleSyncLoss(conn
, id
, msgLength
);
94 if (msgLength
> 30000 && !VALID_LONG_MESSAGE_TYPE(id
))
96 handleSyncLoss(conn
, id
, msgLength
);
101 * Can't process if message body isn't all here yet.
104 avail
= conn
->inEnd
- conn
->inCursor
;
105 if (avail
< msgLength
)
108 * Before returning, enlarge the input buffer if needed to hold
109 * the whole message. This is better than leaving it to
110 * pqReadData because we can avoid multiple cycles of realloc()
111 * when the message is large; also, we can implement a reasonable
112 * recovery strategy if we are unable to make the buffer big
115 if (pqCheckInBufferSpace(conn
->inCursor
+ (size_t) msgLength
,
119 * XXX add some better recovery code... plan is to skip over
120 * the message using its length, then report an error. For the
121 * moment, just treat this like loss of sync (which indeed it
124 handleSyncLoss(conn
, id
, msgLength
);
130 * NOTIFY and NOTICE messages can happen in any state; always process
133 * Most other messages should only be processed while in BUSY state.
134 * (In particular, in READY state we hold off further parsing until
135 * the application collects the current PGresult.)
137 * However, if the state is IDLE then we got trouble; we need to deal
138 * with the unexpected message somehow.
140 * ParameterStatus ('S') messages are a special case: in IDLE state we
141 * must process 'em (this case could happen if a new value was adopted
142 * from config file due to SIGHUP), but otherwise we hold off until
152 if (pqGetErrorNotice3(conn
, false))
155 else if (conn
->asyncStatus
!= PGASYNC_BUSY
)
157 /* If not IDLE state, just wait ... */
158 if (conn
->asyncStatus
!= PGASYNC_IDLE
)
162 * We're also notionally not-IDLE when in pipeline mode the state
163 * says "idle" (so we have completed receiving the results of one
164 * query from the server and dispatched them to the application)
165 * but another query is queued; yield back control to caller so
166 * that they can initiate processing of the next query in the
169 if (conn
->pipelineStatus
!= PQ_PIPELINE_OFF
&&
170 conn
->cmd_queue_head
!= NULL
)
174 * Unexpected message in IDLE state; need to recover somehow.
175 * ERROR messages are handled using the notice processor;
176 * ParameterStatus is handled normally; anything else is just
177 * dropped on the floor after displaying a suitable warning
178 * notice. (An ERROR is very possibly the backend telling us why
179 * it is about to close the connection, so we don't want to just
184 if (pqGetErrorNotice3(conn
, false /* treat as notice */ ))
189 if (getParameterStatus(conn
))
194 /* Any other case is unexpected and we summarily skip it */
195 pqInternalNotice(&conn
->noticeHooks
,
196 "message type 0x%02x arrived from server while idle",
198 /* Discard the unexpected message */
199 conn
->inCursor
+= msgLength
;
205 * In BUSY state, we can process everything.
209 case 'C': /* command complete */
210 if (pqGets(&conn
->workBuffer
, conn
))
212 if (conn
->result
== NULL
)
214 conn
->result
= PQmakeEmptyPGresult(conn
,
218 appendPQExpBufferStr(&conn
->errorMessage
,
219 libpq_gettext("out of memory"));
220 pqSaveErrorResult(conn
);
224 strlcpy(conn
->result
->cmdStatus
, conn
->workBuffer
.data
,
226 conn
->asyncStatus
= PGASYNC_READY
;
228 case 'E': /* error return */
229 if (pqGetErrorNotice3(conn
, true))
231 conn
->asyncStatus
= PGASYNC_READY
;
233 case 'Z': /* sync response, backend is ready for new
235 if (getReadyForQuery(conn
))
237 if (conn
->pipelineStatus
!= PQ_PIPELINE_OFF
)
239 conn
->result
= PQmakeEmptyPGresult(conn
,
240 PGRES_PIPELINE_SYNC
);
243 appendPQExpBufferStr(&conn
->errorMessage
,
244 libpq_gettext("out of memory"));
245 pqSaveErrorResult(conn
);
249 conn
->pipelineStatus
= PQ_PIPELINE_ON
;
250 conn
->asyncStatus
= PGASYNC_READY
;
256 * In simple query protocol, advance the command queue
259 if (conn
->cmd_queue_head
&&
260 conn
->cmd_queue_head
->queryclass
== PGQUERY_SIMPLE
)
261 pqCommandQueueAdvance(conn
);
262 conn
->asyncStatus
= PGASYNC_IDLE
;
265 case 'I': /* empty query */
266 if (conn
->result
== NULL
)
268 conn
->result
= PQmakeEmptyPGresult(conn
,
272 appendPQExpBufferStr(&conn
->errorMessage
,
273 libpq_gettext("out of memory"));
274 pqSaveErrorResult(conn
);
277 conn
->asyncStatus
= PGASYNC_READY
;
279 case '1': /* Parse Complete */
280 /* If we're doing PQprepare, we're done; else ignore */
281 if (conn
->cmd_queue_head
&&
282 conn
->cmd_queue_head
->queryclass
== PGQUERY_PREPARE
)
284 if (conn
->result
== NULL
)
286 conn
->result
= PQmakeEmptyPGresult(conn
,
290 appendPQExpBufferStr(&conn
->errorMessage
,
291 libpq_gettext("out of memory"));
292 pqSaveErrorResult(conn
);
295 conn
->asyncStatus
= PGASYNC_READY
;
298 case '2': /* Bind Complete */
299 case '3': /* Close Complete */
300 /* Nothing to do for these message types */
302 case 'S': /* parameter status */
303 if (getParameterStatus(conn
))
306 case 'K': /* secret key data from the backend */
309 * This is expected only during backend startup, but it's
310 * just as easy to handle it as part of the main loop.
311 * Save the data and continue processing.
313 if (pqGetInt(&(conn
->be_pid
), 4, conn
))
315 if (pqGetInt(&(conn
->be_key
), 4, conn
))
318 case 'T': /* Row Description */
319 if (conn
->result
!= NULL
&&
320 conn
->result
->resultStatus
== PGRES_FATAL_ERROR
)
323 * We've already choked for some reason. Just discard
324 * the data till we get to the end of the query.
326 conn
->inCursor
+= msgLength
;
328 else if (conn
->result
== NULL
||
329 (conn
->cmd_queue_head
&&
330 conn
->cmd_queue_head
->queryclass
== PGQUERY_DESCRIBE
))
332 /* First 'T' in a query sequence */
333 if (getRowDescriptions(conn
, msgLength
))
339 * A new 'T' message is treated as the start of
340 * another PGresult. (It is not clear that this is
341 * really possible with the current backend.) We stop
342 * parsing until the application accepts the current
345 conn
->asyncStatus
= PGASYNC_READY
;
349 case 'n': /* No Data */
352 * NoData indicates that we will not be seeing a
353 * RowDescription message because the statement or portal
354 * inquired about doesn't return rows.
356 * If we're doing a Describe, we have to pass something
357 * back to the client, so set up a COMMAND_OK result,
358 * instead of PGRES_TUPLES_OK. Otherwise we can just
359 * ignore this message.
361 if (conn
->cmd_queue_head
&&
362 conn
->cmd_queue_head
->queryclass
== PGQUERY_DESCRIBE
)
364 if (conn
->result
== NULL
)
366 conn
->result
= PQmakeEmptyPGresult(conn
,
370 appendPQExpBufferStr(&conn
->errorMessage
,
371 libpq_gettext("out of memory"));
372 pqSaveErrorResult(conn
);
375 conn
->asyncStatus
= PGASYNC_READY
;
378 case 't': /* Parameter Description */
379 if (getParamDescriptions(conn
, msgLength
))
382 case 'D': /* Data Row */
383 if (conn
->result
!= NULL
&&
384 conn
->result
->resultStatus
== PGRES_TUPLES_OK
)
386 /* Read another tuple of a normal query response */
387 if (getAnotherTuple(conn
, msgLength
))
390 else if (conn
->result
!= NULL
&&
391 conn
->result
->resultStatus
== PGRES_FATAL_ERROR
)
394 * We've already choked for some reason. Just discard
395 * tuples till we get to the end of the query.
397 conn
->inCursor
+= msgLength
;
401 /* Set up to report error at end of query */
402 appendPQExpBufferStr(&conn
->errorMessage
,
403 libpq_gettext("server sent data (\"D\" message) without prior row description (\"T\" message)\n"));
404 pqSaveErrorResult(conn
);
405 /* Discard the unexpected message */
406 conn
->inCursor
+= msgLength
;
409 case 'G': /* Start Copy In */
410 if (getCopyStart(conn
, PGRES_COPY_IN
))
412 conn
->asyncStatus
= PGASYNC_COPY_IN
;
414 case 'H': /* Start Copy Out */
415 if (getCopyStart(conn
, PGRES_COPY_OUT
))
417 conn
->asyncStatus
= PGASYNC_COPY_OUT
;
418 conn
->copy_already_done
= 0;
420 case 'W': /* Start Copy Both */
421 if (getCopyStart(conn
, PGRES_COPY_BOTH
))
423 conn
->asyncStatus
= PGASYNC_COPY_BOTH
;
424 conn
->copy_already_done
= 0;
426 case 'd': /* Copy Data */
429 * If we see Copy Data, just silently drop it. This would
430 * only occur if application exits COPY OUT mode too
433 conn
->inCursor
+= msgLength
;
435 case 'c': /* Copy Done */
438 * If we see Copy Done, just silently drop it. This is
439 * the normal case during PQendcopy. We will keep
440 * swallowing data, expecting to see command-complete for
445 appendPQExpBuffer(&conn
->errorMessage
,
446 libpq_gettext("unexpected response from server; first received character was \"%c\"\n"),
448 /* build an error result holding the error message */
449 pqSaveErrorResult(conn
);
450 /* not sure if we will see more, so go to ready state */
451 conn
->asyncStatus
= PGASYNC_READY
;
452 /* Discard the unexpected message */
453 conn
->inCursor
+= msgLength
;
455 } /* switch on protocol character */
457 /* Successfully consumed this message */
458 if (conn
->inCursor
== conn
->inStart
+ 5 + msgLength
)
460 /* trace server-to-client message */
462 pqTraceOutputMessage(conn
, conn
->inBuffer
+ conn
->inStart
, false);
464 /* Normal case: parsing agrees with specified length */
465 conn
->inStart
= conn
->inCursor
;
469 /* Trouble --- report it */
470 appendPQExpBuffer(&conn
->errorMessage
,
471 libpq_gettext("message contents do not agree with length in message type \"%c\"\n"),
473 /* build an error result holding the error message */
474 pqSaveErrorResult(conn
);
475 conn
->asyncStatus
= PGASYNC_READY
;
476 /* trust the specified message length as what to skip */
477 conn
->inStart
+= 5 + msgLength
;
483 * handleSyncLoss: clean up after loss of message-boundary sync
485 * There isn't really a lot we can do here except abandon the connection.
488 handleSyncLoss(PGconn
*conn
, char id
, int msgLength
)
490 appendPQExpBuffer(&conn
->errorMessage
,
491 libpq_gettext("lost synchronization with server: got message type \"%c\", length %d\n"),
493 /* build an error result holding the error message */
494 pqSaveErrorResult(conn
);
495 conn
->asyncStatus
= PGASYNC_READY
; /* drop out of PQgetResult wait loop */
496 /* flush input data since we're giving up on processing it */
497 pqDropConnection(conn
, true);
498 conn
->status
= CONNECTION_BAD
; /* No more connection to backend */
502 * parseInput subroutine to read a 'T' (row descriptions) message.
503 * We'll build a new PGresult structure (unless called for a Describe
504 * command for a prepared statement) containing the attribute data.
505 * Returns: 0 if processed message successfully, EOF to suspend parsing
506 * (the latter case is not actually used currently).
509 getRowDescriptions(PGconn
*conn
, int msgLength
)
517 * When doing Describe for a prepared statement, there'll already be a
518 * PGresult created by getParamDescriptions, and we should fill data into
519 * that. Otherwise, create a new, empty PGresult.
521 if (!conn
->cmd_queue_head
||
522 (conn
->cmd_queue_head
&&
523 conn
->cmd_queue_head
->queryclass
== PGQUERY_DESCRIBE
))
526 result
= conn
->result
;
528 result
= PQmakeEmptyPGresult(conn
, PGRES_COMMAND_OK
);
531 result
= PQmakeEmptyPGresult(conn
, PGRES_TUPLES_OK
);
534 errmsg
= NULL
; /* means "out of memory", see below */
535 goto advance_and_error
;
538 /* parseInput already read the 'T' label and message length. */
539 /* the next two bytes are the number of fields */
540 if (pqGetInt(&(result
->numAttributes
), 2, conn
))
542 /* We should not run out of data here, so complain */
543 errmsg
= libpq_gettext("insufficient data in \"T\" message");
544 goto advance_and_error
;
546 nfields
= result
->numAttributes
;
548 /* allocate space for the attribute descriptors */
551 result
->attDescs
= (PGresAttDesc
*)
552 pqResultAlloc(result
, nfields
* sizeof(PGresAttDesc
), true);
553 if (!result
->attDescs
)
555 errmsg
= NULL
; /* means "out of memory", see below */
556 goto advance_and_error
;
558 MemSet(result
->attDescs
, 0, nfields
* sizeof(PGresAttDesc
));
561 /* result->binary is true only if ALL columns are binary */
562 result
->binary
= (nfields
> 0) ? 1 : 0;
565 for (i
= 0; i
< nfields
; i
++)
574 if (pqGets(&conn
->workBuffer
, conn
) ||
575 pqGetInt(&tableid
, 4, conn
) ||
576 pqGetInt(&columnid
, 2, conn
) ||
577 pqGetInt(&typid
, 4, conn
) ||
578 pqGetInt(&typlen
, 2, conn
) ||
579 pqGetInt(&atttypmod
, 4, conn
) ||
580 pqGetInt(&format
, 2, conn
))
582 /* We should not run out of data here, so complain */
583 errmsg
= libpq_gettext("insufficient data in \"T\" message");
584 goto advance_and_error
;
588 * Since pqGetInt treats 2-byte integers as unsigned, we need to
589 * coerce these results to signed form.
591 columnid
= (int) ((int16
) columnid
);
592 typlen
= (int) ((int16
) typlen
);
593 format
= (int) ((int16
) format
);
595 result
->attDescs
[i
].name
= pqResultStrdup(result
,
596 conn
->workBuffer
.data
);
597 if (!result
->attDescs
[i
].name
)
599 errmsg
= NULL
; /* means "out of memory", see below */
600 goto advance_and_error
;
602 result
->attDescs
[i
].tableid
= tableid
;
603 result
->attDescs
[i
].columnid
= columnid
;
604 result
->attDescs
[i
].format
= format
;
605 result
->attDescs
[i
].typid
= typid
;
606 result
->attDescs
[i
].typlen
= typlen
;
607 result
->attDescs
[i
].atttypmod
= atttypmod
;
614 conn
->result
= result
;
617 * If we're doing a Describe, we're done, and ready to pass the result
618 * back to the client.
620 if ((!conn
->cmd_queue_head
) ||
621 (conn
->cmd_queue_head
&&
622 conn
->cmd_queue_head
->queryclass
== PGQUERY_DESCRIBE
))
624 conn
->asyncStatus
= PGASYNC_READY
;
629 * We could perform additional setup for the new result set here, but for
630 * now there's nothing else to do.
633 /* And we're done. */
637 /* Discard unsaved result, if any */
638 if (result
&& result
!= conn
->result
)
642 * Replace partially constructed result with an error result. First
643 * discard the old result to try to win back some memory.
645 pqClearAsyncResult(conn
);
648 * If preceding code didn't provide an error message, assume "out of
649 * memory" was meant. The advantage of having this special case is that
650 * freeing the old result first greatly improves the odds that gettext()
651 * will succeed in providing a translation.
654 errmsg
= libpq_gettext("out of memory for query result");
656 appendPQExpBuffer(&conn
->errorMessage
, "%s\n", errmsg
);
657 pqSaveErrorResult(conn
);
660 * Show the message as fully consumed, else pqParseInput3 will overwrite
661 * our error with a complaint about that.
663 conn
->inCursor
= conn
->inStart
+ 5 + msgLength
;
666 * Return zero to allow input parsing to continue. Subsequent "D"
667 * messages will be ignored until we get to end of data, since an error
668 * result is already set up.
674 * parseInput subroutine to read a 't' (ParameterDescription) message.
675 * We'll build a new PGresult structure containing the parameter data.
676 * Returns: 0 if processed message successfully, EOF to suspend parsing
677 * (the latter case is not actually used currently).
680 getParamDescriptions(PGconn
*conn
, int msgLength
)
683 const char *errmsg
= NULL
; /* means "out of memory", see below */
687 result
= PQmakeEmptyPGresult(conn
, PGRES_COMMAND_OK
);
689 goto advance_and_error
;
691 /* parseInput already read the 't' label and message length. */
692 /* the next two bytes are the number of parameters */
693 if (pqGetInt(&(result
->numParameters
), 2, conn
))
694 goto not_enough_data
;
695 nparams
= result
->numParameters
;
697 /* allocate space for the parameter descriptors */
700 result
->paramDescs
= (PGresParamDesc
*)
701 pqResultAlloc(result
, nparams
* sizeof(PGresParamDesc
), true);
702 if (!result
->paramDescs
)
703 goto advance_and_error
;
704 MemSet(result
->paramDescs
, 0, nparams
* sizeof(PGresParamDesc
));
707 /* get parameter info */
708 for (i
= 0; i
< nparams
; i
++)
712 if (pqGetInt(&typid
, 4, conn
))
713 goto not_enough_data
;
714 result
->paramDescs
[i
].typid
= typid
;
718 conn
->result
= result
;
723 errmsg
= libpq_gettext("insufficient data in \"t\" message");
726 /* Discard unsaved result, if any */
727 if (result
&& result
!= conn
->result
)
731 * Replace partially constructed result with an error result. First
732 * discard the old result to try to win back some memory.
734 pqClearAsyncResult(conn
);
737 * If preceding code didn't provide an error message, assume "out of
738 * memory" was meant. The advantage of having this special case is that
739 * freeing the old result first greatly improves the odds that gettext()
740 * will succeed in providing a translation.
743 errmsg
= libpq_gettext("out of memory");
744 appendPQExpBuffer(&conn
->errorMessage
, "%s\n", errmsg
);
745 pqSaveErrorResult(conn
);
748 * Show the message as fully consumed, else pqParseInput3 will overwrite
749 * our error with a complaint about that.
751 conn
->inCursor
= conn
->inStart
+ 5 + msgLength
;
754 * Return zero to allow input parsing to continue. Essentially, we've
755 * replaced the COMMAND_OK result with an error result, but since this
756 * doesn't affect the protocol state, it's fine.
762 * parseInput subroutine to read a 'D' (row data) message.
763 * We fill rowbuf with column pointers and then call the row processor.
764 * Returns: 0 if processed message successfully, EOF to suspend parsing
765 * (the latter case is not actually used currently).
768 getAnotherTuple(PGconn
*conn
, int msgLength
)
770 PGresult
*result
= conn
->result
;
771 int nfields
= result
->numAttributes
;
774 int tupnfields
; /* # fields from tuple */
775 int vlen
; /* length of the current field value */
778 /* Get the field count and make sure it's what we expect */
779 if (pqGetInt(&tupnfields
, 2, conn
))
781 /* We should not run out of data here, so complain */
782 errmsg
= libpq_gettext("insufficient data in \"D\" message");
783 goto advance_and_error
;
786 if (tupnfields
!= nfields
)
788 errmsg
= libpq_gettext("unexpected field count in \"D\" message");
789 goto advance_and_error
;
792 /* Resize row buffer if needed */
793 rowbuf
= conn
->rowBuf
;
794 if (nfields
> conn
->rowBufLen
)
796 rowbuf
= (PGdataValue
*) realloc(rowbuf
,
797 nfields
* sizeof(PGdataValue
));
800 errmsg
= NULL
; /* means "out of memory", see below */
801 goto advance_and_error
;
803 conn
->rowBuf
= rowbuf
;
804 conn
->rowBufLen
= nfields
;
807 /* Scan the fields */
808 for (i
= 0; i
< nfields
; i
++)
810 /* get the value length */
811 if (pqGetInt(&vlen
, 4, conn
))
813 /* We should not run out of data here, so complain */
814 errmsg
= libpq_gettext("insufficient data in \"D\" message");
815 goto advance_and_error
;
817 rowbuf
[i
].len
= vlen
;
820 * rowbuf[i].value always points to the next address in the data
821 * buffer even if the value is NULL. This allows row processors to
822 * estimate data sizes more easily.
824 rowbuf
[i
].value
= conn
->inBuffer
+ conn
->inCursor
;
826 /* Skip over the data value */
829 if (pqSkipnchar(vlen
, conn
))
831 /* We should not run out of data here, so complain */
832 errmsg
= libpq_gettext("insufficient data in \"D\" message");
833 goto advance_and_error
;
838 /* Process the collected row */
840 if (pqRowProcessor(conn
, &errmsg
))
841 return 0; /* normal, successful exit */
843 /* pqRowProcessor failed, fall through to report it */
848 * Replace partially constructed result with an error result. First
849 * discard the old result to try to win back some memory.
851 pqClearAsyncResult(conn
);
854 * If preceding code didn't provide an error message, assume "out of
855 * memory" was meant. The advantage of having this special case is that
856 * freeing the old result first greatly improves the odds that gettext()
857 * will succeed in providing a translation.
860 errmsg
= libpq_gettext("out of memory for query result");
862 appendPQExpBuffer(&conn
->errorMessage
, "%s\n", errmsg
);
863 pqSaveErrorResult(conn
);
866 * Show the message as fully consumed, else pqParseInput3 will overwrite
867 * our error with a complaint about that.
869 conn
->inCursor
= conn
->inStart
+ 5 + msgLength
;
872 * Return zero to allow input parsing to continue. Subsequent "D"
873 * messages will be ignored until we get to end of data, since an error
874 * result is already set up.
881 * Attempt to read an Error or Notice response message.
882 * This is possible in several places, so we break it out as a subroutine.
883 * Entry: 'E' or 'N' message type and length have already been consumed.
884 * Exit: returns 0 if successfully consumed message.
885 * returns EOF if not enough data.
888 pqGetErrorNotice3(PGconn
*conn
, bool isError
)
890 PGresult
*res
= NULL
;
891 bool have_position
= false;
892 PQExpBufferData workBuf
;
895 /* If in pipeline mode, set error indicator for it */
896 if (isError
&& conn
->pipelineStatus
!= PQ_PIPELINE_OFF
)
897 conn
->pipelineStatus
= PQ_PIPELINE_ABORTED
;
900 * If this is an error message, pre-emptively clear any incomplete query
901 * result we may have. We'd just throw it away below anyway, and
902 * releasing it before collecting the error might avoid out-of-memory.
905 pqClearAsyncResult(conn
);
908 * Since the fields might be pretty long, we create a temporary
909 * PQExpBuffer rather than using conn->workBuffer. workBuffer is intended
910 * for stuff that is expected to be short. We shouldn't use
911 * conn->errorMessage either, since this might be only a notice.
913 initPQExpBuffer(&workBuf
);
916 * Make a PGresult to hold the accumulated fields. We temporarily lie
917 * about the result status, so that PQmakeEmptyPGresult doesn't uselessly
918 * copy conn->errorMessage.
920 * NB: This allocation can fail, if you run out of memory. The rest of the
921 * function handles that gracefully, and we still try to set the error
922 * message as the connection's error message.
924 res
= PQmakeEmptyPGresult(conn
, PGRES_EMPTY_QUERY
);
926 res
->resultStatus
= isError
? PGRES_FATAL_ERROR
: PGRES_NONFATAL_ERROR
;
929 * Read the fields and save into res.
931 * While at it, save the SQLSTATE in conn->last_sqlstate, and note whether
932 * we saw a PG_DIAG_STATEMENT_POSITION field.
936 if (pqGetc(&id
, conn
))
939 break; /* terminator found */
940 if (pqGets(&workBuf
, conn
))
942 pqSaveMessageField(res
, id
, workBuf
.data
);
943 if (id
== PG_DIAG_SQLSTATE
)
944 strlcpy(conn
->last_sqlstate
, workBuf
.data
,
945 sizeof(conn
->last_sqlstate
));
946 else if (id
== PG_DIAG_STATEMENT_POSITION
)
947 have_position
= true;
951 * Save the active query text, if any, into res as well; but only if we
952 * might need it for an error cursor display, which is only true if there
953 * is a PG_DIAG_STATEMENT_POSITION field.
955 if (have_position
&& res
&& conn
->cmd_queue_head
&& conn
->cmd_queue_head
->query
)
956 res
->errQuery
= pqResultStrdup(res
, conn
->cmd_queue_head
->query
);
959 * Now build the "overall" error message for PQresultErrorMessage.
961 resetPQExpBuffer(&workBuf
);
962 pqBuildErrorMessage3(&workBuf
, res
, conn
->verbosity
, conn
->show_context
);
965 * Either save error as current async result, or just emit the notice.
970 pqSetResultError(res
, &workBuf
);
971 pqClearAsyncResult(conn
); /* redundant, but be safe */
973 if (PQExpBufferDataBroken(workBuf
))
974 appendPQExpBufferStr(&conn
->errorMessage
,
975 libpq_gettext("out of memory\n"));
977 appendPQExpBufferStr(&conn
->errorMessage
, workBuf
.data
);
981 /* if we couldn't allocate the result set, just discard the NOTICE */
985 * We can cheat a little here and not copy the message. But if we
986 * were unlucky enough to run out of memory while filling workBuf,
987 * insert "out of memory", as in pqSetResultError.
989 if (PQExpBufferDataBroken(workBuf
))
990 res
->errMsg
= libpq_gettext("out of memory\n");
992 res
->errMsg
= workBuf
.data
;
993 if (res
->noticeHooks
.noticeRec
!= NULL
)
994 res
->noticeHooks
.noticeRec(res
->noticeHooks
.noticeRecArg
, res
);
999 termPQExpBuffer(&workBuf
);
1004 termPQExpBuffer(&workBuf
);
1009 * Construct an error message from the fields in the given PGresult,
1010 * appending it to the contents of "msg".
1013 pqBuildErrorMessage3(PQExpBuffer msg
, const PGresult
*res
,
1014 PGVerbosity verbosity
, PGContextVisibility show_context
)
1017 const char *querytext
= NULL
;
1020 /* If we couldn't allocate a PGresult, just say "out of memory" */
1023 appendPQExpBufferStr(msg
, libpq_gettext("out of memory\n"));
1028 * If we don't have any broken-down fields, just return the base message.
1029 * This mainly applies if we're given a libpq-generated error result.
1031 if (res
->errFields
== NULL
)
1033 if (res
->errMsg
&& res
->errMsg
[0])
1034 appendPQExpBufferStr(msg
, res
->errMsg
);
1036 appendPQExpBufferStr(msg
, libpq_gettext("no error message available\n"));
1040 /* Else build error message from relevant fields */
1041 val
= PQresultErrorField(res
, PG_DIAG_SEVERITY
);
1043 appendPQExpBuffer(msg
, "%s: ", val
);
1045 if (verbosity
== PQERRORS_SQLSTATE
)
1048 * If we have a SQLSTATE, print that and nothing else. If not (which
1049 * shouldn't happen for server-generated errors, but might possibly
1050 * happen for libpq-generated ones), fall back to TERSE format, as
1051 * that seems better than printing nothing at all.
1053 val
= PQresultErrorField(res
, PG_DIAG_SQLSTATE
);
1056 appendPQExpBuffer(msg
, "%s\n", val
);
1059 verbosity
= PQERRORS_TERSE
;
1062 if (verbosity
== PQERRORS_VERBOSE
)
1064 val
= PQresultErrorField(res
, PG_DIAG_SQLSTATE
);
1066 appendPQExpBuffer(msg
, "%s: ", val
);
1068 val
= PQresultErrorField(res
, PG_DIAG_MESSAGE_PRIMARY
);
1070 appendPQExpBufferStr(msg
, val
);
1071 val
= PQresultErrorField(res
, PG_DIAG_STATEMENT_POSITION
);
1074 if (verbosity
!= PQERRORS_TERSE
&& res
->errQuery
!= NULL
)
1076 /* emit position as a syntax cursor display */
1077 querytext
= res
->errQuery
;
1078 querypos
= atoi(val
);
1082 /* emit position as text addition to primary message */
1083 /* translator: %s represents a digit string */
1084 appendPQExpBuffer(msg
, libpq_gettext(" at character %s"),
1090 val
= PQresultErrorField(res
, PG_DIAG_INTERNAL_POSITION
);
1093 querytext
= PQresultErrorField(res
, PG_DIAG_INTERNAL_QUERY
);
1094 if (verbosity
!= PQERRORS_TERSE
&& querytext
!= NULL
)
1096 /* emit position as a syntax cursor display */
1097 querypos
= atoi(val
);
1101 /* emit position as text addition to primary message */
1102 /* translator: %s represents a digit string */
1103 appendPQExpBuffer(msg
, libpq_gettext(" at character %s"),
1108 appendPQExpBufferChar(msg
, '\n');
1109 if (verbosity
!= PQERRORS_TERSE
)
1111 if (querytext
&& querypos
> 0)
1112 reportErrorPosition(msg
, querytext
, querypos
,
1113 res
->client_encoding
);
1114 val
= PQresultErrorField(res
, PG_DIAG_MESSAGE_DETAIL
);
1116 appendPQExpBuffer(msg
, libpq_gettext("DETAIL: %s\n"), val
);
1117 val
= PQresultErrorField(res
, PG_DIAG_MESSAGE_HINT
);
1119 appendPQExpBuffer(msg
, libpq_gettext("HINT: %s\n"), val
);
1120 val
= PQresultErrorField(res
, PG_DIAG_INTERNAL_QUERY
);
1122 appendPQExpBuffer(msg
, libpq_gettext("QUERY: %s\n"), val
);
1123 if (show_context
== PQSHOW_CONTEXT_ALWAYS
||
1124 (show_context
== PQSHOW_CONTEXT_ERRORS
&&
1125 res
->resultStatus
== PGRES_FATAL_ERROR
))
1127 val
= PQresultErrorField(res
, PG_DIAG_CONTEXT
);
1129 appendPQExpBuffer(msg
, libpq_gettext("CONTEXT: %s\n"),
1133 if (verbosity
== PQERRORS_VERBOSE
)
1135 val
= PQresultErrorField(res
, PG_DIAG_SCHEMA_NAME
);
1137 appendPQExpBuffer(msg
,
1138 libpq_gettext("SCHEMA NAME: %s\n"), val
);
1139 val
= PQresultErrorField(res
, PG_DIAG_TABLE_NAME
);
1141 appendPQExpBuffer(msg
,
1142 libpq_gettext("TABLE NAME: %s\n"), val
);
1143 val
= PQresultErrorField(res
, PG_DIAG_COLUMN_NAME
);
1145 appendPQExpBuffer(msg
,
1146 libpq_gettext("COLUMN NAME: %s\n"), val
);
1147 val
= PQresultErrorField(res
, PG_DIAG_DATATYPE_NAME
);
1149 appendPQExpBuffer(msg
,
1150 libpq_gettext("DATATYPE NAME: %s\n"), val
);
1151 val
= PQresultErrorField(res
, PG_DIAG_CONSTRAINT_NAME
);
1153 appendPQExpBuffer(msg
,
1154 libpq_gettext("CONSTRAINT NAME: %s\n"), val
);
1156 if (verbosity
== PQERRORS_VERBOSE
)
1161 valf
= PQresultErrorField(res
, PG_DIAG_SOURCE_FILE
);
1162 vall
= PQresultErrorField(res
, PG_DIAG_SOURCE_LINE
);
1163 val
= PQresultErrorField(res
, PG_DIAG_SOURCE_FUNCTION
);
1164 if (val
|| valf
|| vall
)
1166 appendPQExpBufferStr(msg
, libpq_gettext("LOCATION: "));
1168 appendPQExpBuffer(msg
, libpq_gettext("%s, "), val
);
1169 if (valf
&& vall
) /* unlikely we'd have just one */
1170 appendPQExpBuffer(msg
, libpq_gettext("%s:%s"),
1172 appendPQExpBufferChar(msg
, '\n');
1178 * Add an error-location display to the error message under construction.
1180 * The cursor location is measured in logical characters; the query string
1181 * is presumed to be in the specified encoding.
1184 reportErrorPosition(PQExpBuffer msg
, const char *query
, int loc
, int encoding
)
1186 #define DISPLAY_SIZE 60 /* screen width limit, in screen cols */
1187 #define MIN_RIGHT_CUT 10 /* try to keep this far away from EOL */
1204 /* Convert loc from 1-based to 0-based; no-op if out of range */
1209 /* Need a writable copy of the query */
1210 wquery
= strdup(query
);
1212 return; /* fail silently if out of memory */
1215 * Each character might occupy multiple physical bytes in the string, and
1216 * in some Far Eastern character sets it might take more than one screen
1217 * column as well. We compute the starting byte offset and starting
1218 * screen column of each logical character, and store these in qidx[] and
1219 * scridx[] respectively.
1222 /* we need a safe allocation size... */
1223 slen
= strlen(wquery
) + 1;
1225 qidx
= (int *) malloc(slen
* sizeof(int));
1231 scridx
= (int *) malloc(slen
* sizeof(int));
1239 /* We can optimize a bit if it's a single-byte encoding */
1240 mb_encoding
= (pg_encoding_max_length(encoding
) != 1);
1243 * Within the scanning loop, cno is the current character's logical
1244 * number, qoffset is its offset in wquery, and scroffset is its starting
1245 * logical screen column (all indexed from 0). "loc" is the logical
1246 * character number of the error location. We scan to determine loc_line
1247 * (the 1-based line number containing loc) and ibeg/iend (first character
1248 * number and last+1 character number of the line containing loc). Note
1249 * that qidx[] and scridx[] are filled only as far as iend.
1255 iend
= -1; /* -1 means not set yet */
1257 for (cno
= 0; wquery
[qoffset
] != '\0'; cno
++)
1259 char ch
= wquery
[qoffset
];
1261 qidx
[cno
] = qoffset
;
1262 scridx
[cno
] = scroffset
;
1265 * Replace tabs with spaces in the writable copy. (Later we might
1266 * want to think about coping with their variable screen width, but
1270 wquery
[qoffset
] = ' ';
1273 * If end-of-line, count lines and mark positions. Each \r or \n
1274 * counts as a line except when \r \n appear together.
1276 else if (ch
== '\r' || ch
== '\n')
1282 wquery
[qidx
[cno
- 1]] != '\r')
1284 /* extract beginning = last line start before loc. */
1289 /* set extract end. */
1291 /* done scanning. */
1301 w
= pg_encoding_dsplen(encoding
, &wquery
[qoffset
]);
1302 /* treat any non-tab control chars as width 1 */
1306 qoffset
+= PQmblenBounded(&wquery
[qoffset
], encoding
);
1310 /* We assume wide chars only exist in multibyte encodings */
1315 /* Fix up if we didn't find an end-of-line after loc */
1318 iend
= cno
; /* query length in chars, +1 */
1319 qidx
[iend
] = qoffset
;
1320 scridx
[iend
] = scroffset
;
1323 /* Print only if loc is within computed query length */
1326 /* If the line extracted is too long, we truncate it. */
1329 if (scridx
[iend
] - scridx
[ibeg
] > DISPLAY_SIZE
)
1332 * We first truncate right if it is enough. This code might be
1333 * off a space or so on enforcing MIN_RIGHT_CUT if there's a wide
1334 * character right there, but that should be okay.
1336 if (scridx
[ibeg
] + DISPLAY_SIZE
>= scridx
[loc
] + MIN_RIGHT_CUT
)
1338 while (scridx
[iend
] - scridx
[ibeg
] > DISPLAY_SIZE
)
1344 /* Truncate right if not too close to loc. */
1345 while (scridx
[loc
] + MIN_RIGHT_CUT
< scridx
[iend
])
1351 /* Truncate left if still too long. */
1352 while (scridx
[iend
] - scridx
[ibeg
] > DISPLAY_SIZE
)
1360 /* truncate working copy at desired endpoint */
1361 wquery
[qidx
[iend
]] = '\0';
1363 /* Begin building the finished message. */
1365 appendPQExpBuffer(msg
, libpq_gettext("LINE %d: "), loc_line
);
1367 appendPQExpBufferStr(msg
, "...");
1370 * While we have the prefix in the msg buffer, compute its screen
1374 for (; i
< msg
->len
; i
+= PQmblenBounded(&msg
->data
[i
], encoding
))
1376 int w
= pg_encoding_dsplen(encoding
, &msg
->data
[i
]);
1383 /* Finish up the LINE message line. */
1384 appendPQExpBufferStr(msg
, &wquery
[qidx
[ibeg
]]);
1386 appendPQExpBufferStr(msg
, "...");
1387 appendPQExpBufferChar(msg
, '\n');
1389 /* Now emit the cursor marker line. */
1390 scroffset
+= scridx
[loc
] - scridx
[ibeg
];
1391 for (i
= 0; i
< scroffset
; i
++)
1392 appendPQExpBufferChar(msg
, ' ');
1393 appendPQExpBufferChar(msg
, '^');
1394 appendPQExpBufferChar(msg
, '\n');
1405 * Attempt to read a ParameterStatus message.
1406 * This is possible in several places, so we break it out as a subroutine.
1407 * Entry: 'S' message type and length have already been consumed.
1408 * Exit: returns 0 if successfully consumed message.
1409 * returns EOF if not enough data.
1412 getParameterStatus(PGconn
*conn
)
1414 PQExpBufferData valueBuf
;
1416 /* Get the parameter name */
1417 if (pqGets(&conn
->workBuffer
, conn
))
1419 /* Get the parameter value (could be large) */
1420 initPQExpBuffer(&valueBuf
);
1421 if (pqGets(&valueBuf
, conn
))
1423 termPQExpBuffer(&valueBuf
);
1427 pqSaveParameterStatus(conn
, conn
->workBuffer
.data
, valueBuf
.data
);
1428 termPQExpBuffer(&valueBuf
);
1434 * Attempt to read a Notify response message.
1435 * This is possible in several places, so we break it out as a subroutine.
1436 * Entry: 'A' message type and length have already been consumed.
1437 * Exit: returns 0 if successfully consumed Notify message.
1438 * returns EOF if not enough data.
1441 getNotify(PGconn
*conn
)
1447 PGnotify
*newNotify
;
1449 if (pqGetInt(&be_pid
, 4, conn
))
1451 if (pqGets(&conn
->workBuffer
, conn
))
1453 /* must save name while getting extra string */
1454 svname
= strdup(conn
->workBuffer
.data
);
1457 if (pqGets(&conn
->workBuffer
, conn
))
1464 * Store the strings right after the PQnotify structure so it can all be
1465 * freed at once. We don't use NAMEDATALEN because we don't want to tie
1466 * this interface to a specific server name length.
1468 nmlen
= strlen(svname
);
1469 extralen
= strlen(conn
->workBuffer
.data
);
1470 newNotify
= (PGnotify
*) malloc(sizeof(PGnotify
) + nmlen
+ extralen
+ 2);
1473 newNotify
->relname
= (char *) newNotify
+ sizeof(PGnotify
);
1474 strcpy(newNotify
->relname
, svname
);
1475 newNotify
->extra
= newNotify
->relname
+ nmlen
+ 1;
1476 strcpy(newNotify
->extra
, conn
->workBuffer
.data
);
1477 newNotify
->be_pid
= be_pid
;
1478 newNotify
->next
= NULL
;
1479 if (conn
->notifyTail
)
1480 conn
->notifyTail
->next
= newNotify
;
1482 conn
->notifyHead
= newNotify
;
1483 conn
->notifyTail
= newNotify
;
1491 * getCopyStart - process CopyInResponse, CopyOutResponse or
1492 * CopyBothResponse message
1494 * parseInput already read the message type and length.
1497 getCopyStart(PGconn
*conn
, ExecStatusType copytype
)
1503 result
= PQmakeEmptyPGresult(conn
, copytype
);
1507 if (pqGetc(&conn
->copy_is_binary
, conn
))
1509 result
->binary
= conn
->copy_is_binary
;
1510 /* the next two bytes are the number of fields */
1511 if (pqGetInt(&(result
->numAttributes
), 2, conn
))
1513 nfields
= result
->numAttributes
;
1515 /* allocate space for the attribute descriptors */
1518 result
->attDescs
= (PGresAttDesc
*)
1519 pqResultAlloc(result
, nfields
* sizeof(PGresAttDesc
), true);
1520 if (!result
->attDescs
)
1522 MemSet(result
->attDescs
, 0, nfields
* sizeof(PGresAttDesc
));
1525 for (i
= 0; i
< nfields
; i
++)
1529 if (pqGetInt(&format
, 2, conn
))
1533 * Since pqGetInt treats 2-byte integers as unsigned, we need to
1534 * coerce these results to signed form.
1536 format
= (int) ((int16
) format
);
1537 result
->attDescs
[i
].format
= format
;
1541 conn
->result
= result
;
1550 * getReadyForQuery - process ReadyForQuery message
1553 getReadyForQuery(PGconn
*conn
)
1557 if (pqGetc(&xact_status
, conn
))
1559 switch (xact_status
)
1562 conn
->xactStatus
= PQTRANS_IDLE
;
1565 conn
->xactStatus
= PQTRANS_INTRANS
;
1568 conn
->xactStatus
= PQTRANS_INERROR
;
1571 conn
->xactStatus
= PQTRANS_UNKNOWN
;
1579 * getCopyDataMessage - fetch next CopyData message, process async messages
1581 * Returns length word of CopyData message (> 0), or 0 if no complete
1582 * message available, -1 if end of copy, -2 if error.
1585 getCopyDataMessage(PGconn
*conn
)
1594 * Do we have the next input message? To make life simpler for async
1595 * callers, we keep returning 0 until the next message is fully
1596 * available, even if it is not Copy Data.
1598 conn
->inCursor
= conn
->inStart
;
1599 if (pqGetc(&id
, conn
))
1601 if (pqGetInt(&msgLength
, 4, conn
))
1605 handleSyncLoss(conn
, id
, msgLength
);
1608 avail
= conn
->inEnd
- conn
->inCursor
;
1609 if (avail
< msgLength
- 4)
1612 * Before returning, enlarge the input buffer if needed to hold
1613 * the whole message. See notes in parseInput.
1615 if (pqCheckInBufferSpace(conn
->inCursor
+ (size_t) msgLength
- 4,
1619 * XXX add some better recovery code... plan is to skip over
1620 * the message using its length, then report an error. For the
1621 * moment, just treat this like loss of sync (which indeed it
1624 handleSyncLoss(conn
, id
, msgLength
);
1631 * If it's a legitimate async message type, process it. (NOTIFY
1632 * messages are not currently possible here, but we handle them for
1633 * completeness.) Otherwise, if it's anything except Copy Data,
1634 * report end-of-copy.
1638 case 'A': /* NOTIFY */
1639 if (getNotify(conn
))
1642 case 'N': /* NOTICE */
1643 if (pqGetErrorNotice3(conn
, false))
1646 case 'S': /* ParameterStatus */
1647 if (getParameterStatus(conn
))
1650 case 'd': /* Copy Data, pass it back to caller */
1655 * If this is a CopyDone message, exit COPY_OUT mode and let
1656 * caller read status with PQgetResult(). If we're in
1657 * COPY_BOTH mode, return to COPY_IN mode.
1659 if (conn
->asyncStatus
== PGASYNC_COPY_BOTH
)
1660 conn
->asyncStatus
= PGASYNC_COPY_IN
;
1662 conn
->asyncStatus
= PGASYNC_BUSY
;
1664 default: /* treat as end of copy */
1667 * Any other message terminates either COPY_IN or COPY_BOTH
1670 conn
->asyncStatus
= PGASYNC_BUSY
;
1674 /* trace server-to-client message */
1676 pqTraceOutputMessage(conn
, conn
->inBuffer
+ conn
->inStart
, false);
1678 /* Drop the processed message and loop around for another */
1679 conn
->inStart
= conn
->inCursor
;
1684 * PQgetCopyData - read a row of data from the backend during COPY OUT
1687 * If successful, sets *buffer to point to a malloc'd row of data, and
1688 * returns row length (always > 0) as result.
1689 * Returns 0 if no row available yet (only possible if async is true),
1690 * -1 if end of copy (consult PQgetResult), or -2 if error (consult
1694 pqGetCopyData3(PGconn
*conn
, char **buffer
, int async
)
1701 * Collect the next input message. To make life simpler for async
1702 * callers, we keep returning 0 until the next message is fully
1703 * available, even if it is not Copy Data.
1705 msgLength
= getCopyDataMessage(conn
);
1707 return msgLength
; /* end-of-copy or error */
1710 /* Don't block if async read requested */
1713 /* Need to load more data */
1714 if (pqWait(true, false, conn
) ||
1715 pqReadData(conn
) < 0)
1721 * Drop zero-length messages (shouldn't happen anyway). Otherwise
1722 * pass the data back to the caller.
1727 *buffer
= (char *) malloc(msgLength
+ 1);
1728 if (*buffer
== NULL
)
1730 appendPQExpBufferStr(&conn
->errorMessage
,
1731 libpq_gettext("out of memory\n"));
1734 memcpy(*buffer
, &conn
->inBuffer
[conn
->inCursor
], msgLength
);
1735 (*buffer
)[msgLength
] = '\0'; /* Add terminating null */
1737 /* Mark message consumed */
1738 conn
->inStart
= conn
->inCursor
+ msgLength
;
1743 /* Empty, so drop it and loop around for another */
1744 conn
->inStart
= conn
->inCursor
;
1749 * PQgetline - gets a newline-terminated string from the backend.
1751 * See fe-exec.c for documentation.
1754 pqGetline3(PGconn
*conn
, char *s
, int maxlen
)
1758 if (conn
->sock
== PGINVALID_SOCKET
||
1759 (conn
->asyncStatus
!= PGASYNC_COPY_OUT
&&
1760 conn
->asyncStatus
!= PGASYNC_COPY_BOTH
) ||
1761 conn
->copy_is_binary
)
1763 appendPQExpBufferStr(&conn
->errorMessage
,
1764 libpq_gettext("PQgetline: not doing text COPY OUT\n"));
1769 while ((status
= PQgetlineAsync(conn
, s
, maxlen
- 1)) == 0)
1771 /* need to load more data */
1772 if (pqWait(true, false, conn
) ||
1773 pqReadData(conn
) < 0)
1782 /* End of copy detected; gin up old-style terminator */
1787 /* Add null terminator, and strip trailing \n if present */
1788 if (s
[status
- 1] == '\n')
1790 s
[status
- 1] = '\0';
1801 * PQgetlineAsync - gets a COPY data row without blocking.
1803 * See fe-exec.c for documentation.
1806 pqGetlineAsync3(PGconn
*conn
, char *buffer
, int bufsize
)
1811 if (conn
->asyncStatus
!= PGASYNC_COPY_OUT
1812 && conn
->asyncStatus
!= PGASYNC_COPY_BOTH
)
1813 return -1; /* we are not doing a copy... */
1816 * Recognize the next input message. To make life simpler for async
1817 * callers, we keep returning 0 until the next message is fully available
1818 * even if it is not Copy Data. This should keep PQendcopy from blocking.
1819 * (Note: unlike pqGetCopyData3, we do not change asyncStatus here.)
1821 msgLength
= getCopyDataMessage(conn
);
1823 return -1; /* end-of-copy or error */
1825 return 0; /* no data yet */
1828 * Move data from libpq's buffer to the caller's. In the case where a
1829 * prior call found the caller's buffer too small, we use
1830 * conn->copy_already_done to remember how much of the row was already
1831 * returned to the caller.
1833 conn
->inCursor
+= conn
->copy_already_done
;
1834 avail
= msgLength
- 4 - conn
->copy_already_done
;
1835 if (avail
<= bufsize
)
1837 /* Able to consume the whole message */
1838 memcpy(buffer
, &conn
->inBuffer
[conn
->inCursor
], avail
);
1839 /* Mark message consumed */
1840 conn
->inStart
= conn
->inCursor
+ avail
;
1841 /* Reset state for next time */
1842 conn
->copy_already_done
= 0;
1847 /* We must return a partial message */
1848 memcpy(buffer
, &conn
->inBuffer
[conn
->inCursor
], bufsize
);
1849 /* The message is NOT consumed from libpq's buffer */
1850 conn
->copy_already_done
+= bufsize
;
1858 * See fe-exec.c for documentation.
1861 pqEndcopy3(PGconn
*conn
)
1865 if (conn
->asyncStatus
!= PGASYNC_COPY_IN
&&
1866 conn
->asyncStatus
!= PGASYNC_COPY_OUT
&&
1867 conn
->asyncStatus
!= PGASYNC_COPY_BOTH
)
1869 appendPQExpBufferStr(&conn
->errorMessage
,
1870 libpq_gettext("no COPY in progress\n"));
1874 /* Send the CopyDone message if needed */
1875 if (conn
->asyncStatus
== PGASYNC_COPY_IN
||
1876 conn
->asyncStatus
== PGASYNC_COPY_BOTH
)
1878 if (pqPutMsgStart('c', conn
) < 0 ||
1879 pqPutMsgEnd(conn
) < 0)
1883 * If we sent the COPY command in extended-query mode, we must issue a
1886 if (conn
->cmd_queue_head
&&
1887 conn
->cmd_queue_head
->queryclass
!= PGQUERY_SIMPLE
)
1889 if (pqPutMsgStart('S', conn
) < 0 ||
1890 pqPutMsgEnd(conn
) < 0)
1896 * make sure no data is waiting to be sent, abort if we are non-blocking
1897 * and the flush fails
1899 if (pqFlush(conn
) && pqIsnonblocking(conn
))
1902 /* Return to active duty */
1903 conn
->asyncStatus
= PGASYNC_BUSY
;
1906 * Non blocking connections may have to abort at this point. If everyone
1907 * played the game there should be no problem, but in error scenarios the
1908 * expected messages may not have arrived yet. (We are assuming that the
1909 * backend's packetizing will ensure that CommandComplete arrives along
1910 * with the CopyDone; are there corner cases where that doesn't happen?)
1912 if (pqIsnonblocking(conn
) && PQisBusy(conn
))
1915 /* Wait for the completion response */
1916 result
= PQgetResult(conn
);
1918 /* Expecting a successful result */
1919 if (result
&& result
->resultStatus
== PGRES_COMMAND_OK
)
1926 * Trouble. For backwards-compatibility reasons, we issue the error
1927 * message as if it were a notice (would be nice to get rid of this
1928 * silliness, but too many apps probably don't handle errors from
1929 * PQendcopy reasonably). Note that the app can still obtain the error
1930 * status from the PGconn object.
1932 if (conn
->errorMessage
.len
> 0)
1934 /* We have to strip the trailing newline ... pain in neck... */
1935 char svLast
= conn
->errorMessage
.data
[conn
->errorMessage
.len
- 1];
1938 conn
->errorMessage
.data
[conn
->errorMessage
.len
- 1] = '\0';
1939 pqInternalNotice(&conn
->noticeHooks
, "%s", conn
->errorMessage
.data
);
1940 conn
->errorMessage
.data
[conn
->errorMessage
.len
- 1] = svLast
;
1950 * PQfn - Send a function call to the POSTGRES backend.
1952 * See fe-exec.c for documentation.
1955 pqFunctionCall3(PGconn
*conn
, Oid fnid
,
1956 int *result_buf
, int *actual_result_len
,
1958 const PQArgBlock
*args
, int nargs
)
1960 bool needInput
= false;
1961 ExecStatusType status
= PGRES_FATAL_ERROR
;
1967 /* already validated by PQfn */
1968 Assert(conn
->pipelineStatus
== PQ_PIPELINE_OFF
);
1970 /* PQfn already validated connection state */
1972 if (pqPutMsgStart('F', conn
) < 0 || /* function call msg */
1973 pqPutInt(fnid
, 4, conn
) < 0 || /* function id */
1974 pqPutInt(1, 2, conn
) < 0 || /* # of format codes */
1975 pqPutInt(1, 2, conn
) < 0 || /* format code: BINARY */
1976 pqPutInt(nargs
, 2, conn
) < 0) /* # of args */
1978 /* error message should be set up already */
1982 for (i
= 0; i
< nargs
; ++i
)
1983 { /* len.int4 + contents */
1984 if (pqPutInt(args
[i
].len
, 4, conn
))
1986 if (args
[i
].len
== -1)
1987 continue; /* it's NULL */
1991 if (pqPutInt(args
[i
].u
.integer
, args
[i
].len
, conn
))
1996 if (pqPutnchar((char *) args
[i
].u
.ptr
, args
[i
].len
, conn
))
2001 if (pqPutInt(1, 2, conn
) < 0) /* result format code: BINARY */
2004 if (pqPutMsgEnd(conn
) < 0 ||
2012 /* Wait for some data to arrive (or for the channel to close) */
2013 if (pqWait(true, false, conn
) ||
2014 pqReadData(conn
) < 0)
2019 * Scan the message. If we run out of data, loop around to try again.
2023 conn
->inCursor
= conn
->inStart
;
2024 if (pqGetc(&id
, conn
))
2026 if (pqGetInt(&msgLength
, 4, conn
))
2030 * Try to validate message type/length here. A length less than 4 is
2031 * definitely broken. Large lengths should only be believed for a few
2036 handleSyncLoss(conn
, id
, msgLength
);
2039 if (msgLength
> 30000 && !VALID_LONG_MESSAGE_TYPE(id
))
2041 handleSyncLoss(conn
, id
, msgLength
);
2046 * Can't process if message body isn't all here yet.
2049 avail
= conn
->inEnd
- conn
->inCursor
;
2050 if (avail
< msgLength
)
2053 * Before looping, enlarge the input buffer if needed to hold the
2054 * whole message. See notes in parseInput.
2056 if (pqCheckInBufferSpace(conn
->inCursor
+ (size_t) msgLength
,
2060 * XXX add some better recovery code... plan is to skip over
2061 * the message using its length, then report an error. For the
2062 * moment, just treat this like loss of sync (which indeed it
2065 handleSyncLoss(conn
, id
, msgLength
);
2072 * We should see V or E response to the command, but might get N
2073 * and/or A notices first. We also need to swallow the final Z before
2078 case 'V': /* function result */
2079 if (pqGetInt(actual_result_len
, 4, conn
))
2081 if (*actual_result_len
!= -1)
2085 if (pqGetInt(result_buf
, *actual_result_len
, conn
))
2090 if (pqGetnchar((char *) result_buf
,
2096 /* correctly finished function result message */
2097 status
= PGRES_COMMAND_OK
;
2099 case 'E': /* error return */
2100 if (pqGetErrorNotice3(conn
, true))
2102 status
= PGRES_FATAL_ERROR
;
2104 case 'A': /* notify message */
2105 /* handle notify and go back to processing return values */
2106 if (getNotify(conn
))
2109 case 'N': /* notice */
2110 /* handle notice and go back to processing return values */
2111 if (pqGetErrorNotice3(conn
, false))
2114 case 'Z': /* backend is ready for new query */
2115 if (getReadyForQuery(conn
))
2117 /* consume the message and exit */
2118 conn
->inStart
+= 5 + msgLength
;
2119 /* if we saved a result object (probably an error), use it */
2121 return pqPrepareAsyncResult(conn
);
2122 return PQmakeEmptyPGresult(conn
, status
);
2123 case 'S': /* parameter status */
2124 if (getParameterStatus(conn
))
2128 /* The backend violates the protocol. */
2129 appendPQExpBuffer(&conn
->errorMessage
,
2130 libpq_gettext("protocol error: id=0x%x\n"),
2132 pqSaveErrorResult(conn
);
2133 /* trust the specified message length as what to skip */
2134 conn
->inStart
+= 5 + msgLength
;
2135 return pqPrepareAsyncResult(conn
);
2138 /* trace server-to-client message */
2140 pqTraceOutputMessage(conn
, conn
->inBuffer
+ conn
->inStart
, false);
2142 /* Completed this message, keep going */
2143 /* trust the specified message length as what to skip */
2144 conn
->inStart
+= 5 + msgLength
;
2149 * We fall out of the loop only upon failing to read data.
2150 * conn->errorMessage has been set by pqWait or pqReadData. We want to
2151 * append it to any already-received error message.
2153 pqSaveErrorResult(conn
);
2154 return pqPrepareAsyncResult(conn
);
2159 * Construct startup packet
2161 * Returns a malloc'd packet buffer, or NULL if out of memory
2164 pqBuildStartupPacket3(PGconn
*conn
, int *packetlen
,
2165 const PQEnvironmentOption
*options
)
2169 *packetlen
= build_startup_packet(conn
, NULL
, options
);
2170 startpacket
= (char *) malloc(*packetlen
);
2173 *packetlen
= build_startup_packet(conn
, startpacket
, options
);
2178 * Build a startup packet given a filled-in PGconn structure.
2180 * We need to figure out how much space is needed, then fill it in.
2181 * To avoid duplicate logic, this routine is called twice: the first time
2182 * (with packet == NULL) just counts the space needed, the second time
2183 * (with packet == allocated space) fills it in. Return value is the number
2187 build_startup_packet(const PGconn
*conn
, char *packet
,
2188 const PQEnvironmentOption
*options
)
2191 const PQEnvironmentOption
*next_eo
;
2194 /* Protocol version comes first. */
2197 ProtocolVersion pv
= pg_hton32(conn
->pversion
);
2199 memcpy(packet
+ packet_len
, &pv
, sizeof(ProtocolVersion
));
2201 packet_len
+= sizeof(ProtocolVersion
);
2203 /* Add user name, database name, options */
2205 #define ADD_STARTUP_OPTION(optname, optval) \
2208 strcpy(packet + packet_len, optname); \
2209 packet_len += strlen(optname) + 1; \
2211 strcpy(packet + packet_len, optval); \
2212 packet_len += strlen(optval) + 1; \
2215 if (conn
->pguser
&& conn
->pguser
[0])
2216 ADD_STARTUP_OPTION("user", conn
->pguser
);
2217 if (conn
->dbName
&& conn
->dbName
[0])
2218 ADD_STARTUP_OPTION("database", conn
->dbName
);
2219 if (conn
->replication
&& conn
->replication
[0])
2220 ADD_STARTUP_OPTION("replication", conn
->replication
);
2221 if (conn
->pgoptions
&& conn
->pgoptions
[0])
2222 ADD_STARTUP_OPTION("options", conn
->pgoptions
);
2223 if (conn
->send_appname
)
2225 /* Use appname if present, otherwise use fallback */
2226 val
= conn
->appname
? conn
->appname
: conn
->fbappname
;
2228 ADD_STARTUP_OPTION("application_name", val
);
2231 if (conn
->client_encoding_initial
&& conn
->client_encoding_initial
[0])
2232 ADD_STARTUP_OPTION("client_encoding", conn
->client_encoding_initial
);
2234 /* Add any environment-driven GUC settings needed */
2235 for (next_eo
= options
; next_eo
->envName
; next_eo
++)
2237 if ((val
= getenv(next_eo
->envName
)) != NULL
)
2239 if (pg_strcasecmp(val
, "default") != 0)
2240 ADD_STARTUP_OPTION(next_eo
->pgName
, val
);
2244 /* Add trailing terminator */
2246 packet
[packet_len
] = '\0';