1 /*-------------------------------------------------------------------------
7 * miscellaneous useful functions
9 * The communication routines here are analogous to the ones in
10 * backend/libpq/pqcomm.c and backend/libpq/pqcomprim.c, but operate
11 * in the considerably different environment of the frontend libpq.
12 * In particular, we work with a bare nonblock-mode socket, rather than
13 * a stdio stream, so that we can avoid unwanted blocking of the application.
15 * XXX: MOVE DEBUG PRINTOUT TO HIGHER LEVEL. As is, block and restart
16 * will cause repeat printouts.
18 * We must speak the same transmitted data representations as the backend
22 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
23 * Portions Copyright (c) 1994, Regents of the University of California
26 * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.103 2003/10/19 21:36:41 tgl Exp $
28 *-------------------------------------------------------------------------
31 #include "postgres_fe.h"
37 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
52 #ifdef HAVE_SYS_POLL_H
55 #ifdef HAVE_SYS_SELECT_H
56 #include <sys/select.h>
60 #include "libpq-int.h"
62 #include "mb/pg_wchar.h"
65 static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn);
66 static int pqSendSome(PGconn *conn, int len);
67 static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
69 static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
73 * pqGetc: get 1 character from the connection
75 * All these routines return 0 on success, EOF on error.
76 * Note that for the Get routines, EOF only means there is not enough
77 * data in the buffer, not that there is necessarily a hard error.
80 pqGetc(char *result, PGconn *conn)
82 if (conn->inCursor >= conn->inEnd)
85 *result = conn->inBuffer[conn->inCursor++];
88 fprintf(conn->Pfdebug, "From backend> %c\n", *result);
95 * pqPutc: write 1 char to the current message
98 pqPutc(char c, PGconn *conn)
100 if (pqPutMsgBytes(&c, 1, conn))
104 fprintf(conn->Pfdebug, "To backend> %c\n", c);
112 * get a null-terminated string from the connection,
113 * and store it in an expansible PQExpBuffer.
114 * If we run out of memory, all of the string is still read,
115 * but the excess characters are silently discarded.
118 pqGets(PQExpBuffer buf, PGconn *conn)
120 /* Copy conn data to locals for faster search loop */
121 char *inBuffer = conn->inBuffer;
122 int inCursor = conn->inCursor;
123 int inEnd = conn->inEnd;
126 while (inCursor < inEnd && inBuffer[inCursor])
129 if (inCursor >= inEnd)
132 slen = inCursor - conn->inCursor;
134 resetPQExpBuffer(buf);
135 appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
137 conn->inCursor = ++inCursor;
140 fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
148 * pqPuts: write a null-terminated string to the current message
151 pqPuts(const char *s, PGconn *conn)
153 if (pqPutMsgBytes(s, strlen(s) + 1, conn))
157 fprintf(conn->Pfdebug, "To backend> '%s'\n", s);
164 * get a string of exactly len bytes in buffer s, no null termination
167 pqGetnchar(char *s, size_t len, PGconn *conn)
169 if (len < 0 || len > (size_t) (conn->inEnd - conn->inCursor))
172 memcpy(s, conn->inBuffer + conn->inCursor, len);
173 /* no terminating null */
175 conn->inCursor += len;
178 fprintf(conn->Pfdebug, "From backend (%lu)> %.*s\n", (unsigned long) len, (int) len, s);
185 * write exactly len bytes to the current message
188 pqPutnchar(const char *s, size_t len, PGconn *conn)
190 if (pqPutMsgBytes(s, len, conn))
194 fprintf(conn->Pfdebug, "To backend> %.*s\n", (int) len, s);
201 * read a 2 or 4 byte integer and convert from network byte order
202 * to local byte order
205 pqGetInt(int *result, size_t bytes, PGconn *conn)
213 if (conn->inCursor + 2 > conn->inEnd)
215 memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
217 *result = (int) ntohs(tmp2);
220 if (conn->inCursor + 4 > conn->inEnd)
222 memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
224 *result = (int) ntohl(tmp4);
227 pqInternalNotice(&conn->noticeHooks,
228 "integer of size %lu not supported by pqGetInt",
229 (unsigned long) bytes);
234 fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long) bytes, *result);
241 * write an integer of 2 or 4 bytes, converting from host byte order
242 * to network byte order.
245 pqPutInt(int value, size_t bytes, PGconn *conn)
253 tmp2 = htons((uint16) value);
254 if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
258 tmp4 = htonl((uint32) value);
259 if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
263 pqInternalNotice(&conn->noticeHooks,
264 "integer of size %lu not supported by pqPutInt",
265 (unsigned long) bytes);
270 fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long) bytes, value);
276 * Make sure conn's output buffer can hold bytes_needed bytes (caller must
277 * include already-stored data into the value!)
279 * Returns 0 on success, EOF if failed to enlarge buffer
282 pqCheckOutBufferSpace(int bytes_needed, PGconn *conn)
284 int newsize = conn->outBufSize;
287 if (bytes_needed <= newsize)
291 * If we need to enlarge the buffer, we first try to double it in
292 * size; if that doesn't work, enlarge in multiples of 8K. This
293 * avoids thrashing the malloc pool by repeated small enlargements.
295 * Note: tests for newsize > 0 are to catch integer overflow.
300 } while (bytes_needed > newsize && newsize > 0);
302 if (bytes_needed <= newsize)
304 newbuf = realloc(conn->outBuffer, newsize);
307 /* realloc succeeded */
308 conn->outBuffer = newbuf;
309 conn->outBufSize = newsize;
314 newsize = conn->outBufSize;
318 } while (bytes_needed > newsize && newsize > 0);
320 if (bytes_needed <= newsize)
322 newbuf = realloc(conn->outBuffer, newsize);
325 /* realloc succeeded */
326 conn->outBuffer = newbuf;
327 conn->outBufSize = newsize;
332 /* realloc failed. Probably out of memory */
333 printfPQExpBuffer(&conn->errorMessage,
334 "cannot allocate memory for output buffer\n");
339 * Make sure conn's input buffer can hold bytes_needed bytes (caller must
340 * include already-stored data into the value!)
342 * Returns 0 on success, EOF if failed to enlarge buffer
345 pqCheckInBufferSpace(int bytes_needed, PGconn *conn)
347 int newsize = conn->inBufSize;
350 if (bytes_needed <= newsize)
354 * If we need to enlarge the buffer, we first try to double it in
355 * size; if that doesn't work, enlarge in multiples of 8K. This
356 * avoids thrashing the malloc pool by repeated small enlargements.
358 * Note: tests for newsize > 0 are to catch integer overflow.
363 } while (bytes_needed > newsize && newsize > 0);
365 if (bytes_needed <= newsize)
367 newbuf = realloc(conn->inBuffer, newsize);
370 /* realloc succeeded */
371 conn->inBuffer = newbuf;
372 conn->inBufSize = newsize;
377 newsize = conn->inBufSize;
381 } while (bytes_needed > newsize && newsize > 0);
383 if (bytes_needed <= newsize)
385 newbuf = realloc(conn->inBuffer, newsize);
388 /* realloc succeeded */
389 conn->inBuffer = newbuf;
390 conn->inBufSize = newsize;
395 /* realloc failed. Probably out of memory */
396 printfPQExpBuffer(&conn->errorMessage,
397 "cannot allocate memory for input buffer\n");
402 * pqPutMsgStart: begin construction of a message to the server
404 * msg_type is the message type byte, or 0 for a message without type byte
405 * (only startup messages have no type byte)
407 * force_len forces the message to have a length word; otherwise, we add
408 * a length word if protocol 3.
410 * Returns 0 on success, EOF on error
412 * The idea here is that we construct the message in conn->outBuffer,
413 * beginning just past any data already in outBuffer (ie, at
414 * outBuffer+outCount). We enlarge the buffer as needed to hold the message.
415 * When the message is complete, we fill in the length word (if needed) and
416 * then advance outCount past the message, making it eligible to send.
418 * The state variable conn->outMsgStart points to the incomplete message's
419 * length word: it is either outCount or outCount+1 depending on whether
420 * there is a type byte. If we are sending a message without length word
421 * (pre protocol 3.0 only), then outMsgStart is -1. The state variable
422 * conn->outMsgEnd is the end of the data collected so far.
425 pqPutMsgStart(char msg_type, bool force_len, PGconn *conn)
430 /* allow room for message type byte */
432 endPos = conn->outCount + 1;
434 endPos = conn->outCount;
436 /* do we want a length word? */
437 if (force_len || PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
440 /* allow room for message length */
446 /* make sure there is room for message header */
447 if (pqCheckOutBufferSpace(endPos, conn))
449 /* okay, save the message type byte if any */
451 conn->outBuffer[conn->outCount] = msg_type;
452 /* set up the message pointers */
453 conn->outMsgStart = lenPos;
454 conn->outMsgEnd = endPos;
455 /* length word, if needed, will be filled in by pqPutMsgEnd */
458 fprintf(conn->Pfdebug, "To backend> Msg %c\n",
459 msg_type ? msg_type : ' ');
465 * pqPutMsgBytes: add bytes to a partially-constructed message
467 * Returns 0 on success, EOF on error
470 pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
472 /* make sure there is room for it */
473 if (pqCheckOutBufferSpace(conn->outMsgEnd + len, conn))
475 /* okay, save the data */
476 memcpy(conn->outBuffer + conn->outMsgEnd, buf, len);
477 conn->outMsgEnd += len;
478 /* no Pfdebug call here, caller should do it */
483 * pqPutMsgEnd: finish constructing a message and possibly send it
485 * Returns 0 on success, EOF on error
487 * We don't actually send anything here unless we've accumulated at least
488 * 8K worth of data (the typical size of a pipe buffer on Unix systems).
489 * This avoids sending small partial packets. The caller must use pqFlush
490 * when it's important to flush all the data out to the server.
493 pqPutMsgEnd(PGconn *conn)
496 fprintf(conn->Pfdebug, "To backend> Msg complete, length %u\n",
497 conn->outMsgEnd - conn->outCount);
499 /* Fill in length word if needed */
500 if (conn->outMsgStart >= 0)
502 uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
504 msgLen = htonl(msgLen);
505 memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
508 /* Make message eligible to send */
509 conn->outCount = conn->outMsgEnd;
511 if (conn->outCount >= 8192)
513 int toSend = conn->outCount - (conn->outCount % 8192);
515 if (pqSendSome(conn, toSend) < 0)
517 /* in nonblock mode, don't complain if unable to send it all */
524 * pqReadData: read more data, if any is available
525 * Possible return values:
526 * 1: successfully loaded at least one more byte
527 * 0: no data is presently available, but no error detected
528 * -1: error detected (including EOF = connection closure);
529 * conn->errorMessage set
530 * NOTE: callers must not assume that pointers or indexes into conn->inBuffer
531 * remain valid across this call!
535 pqReadData(PGconn *conn)
543 printfPQExpBuffer(&conn->errorMessage,
544 libpq_gettext("connection not open\n"));
548 /* Left-justify any data in the buffer to make room */
549 if (conn->inStart < conn->inEnd)
551 if (conn->inStart > 0)
553 memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
554 conn->inEnd - conn->inStart);
555 conn->inEnd -= conn->inStart;
556 conn->inCursor -= conn->inStart;
562 /* buffer is logically empty, reset it */
563 conn->inStart = conn->inCursor = conn->inEnd = 0;
567 * If the buffer is fairly full, enlarge it. We need to be able to
568 * enlarge the buffer in case a single message exceeds the initial
569 * buffer size. We enlarge before filling the buffer entirely so as
570 * to avoid asking the kernel for a partial packet. The magic constant
571 * here should be large enough for a TCP packet or Unix pipe
572 * bufferload. 8K is the usual pipe buffer size, so...
574 if (conn->inBufSize - conn->inEnd < 8192)
576 if (pqCheckInBufferSpace(conn->inEnd + 8192, conn))
579 * We don't insist that the enlarge worked, but we need some
582 if (conn->inBufSize - conn->inEnd < 100)
583 return -1; /* errorMessage already set */
587 /* OK, try to read some data */
589 nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
590 conn->inBufSize - conn->inEnd);
593 if (SOCK_ERRNO == EINTR)
595 /* Some systems return EAGAIN/EWOULDBLOCK for no data */
597 if (SOCK_ERRNO == EAGAIN)
600 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
601 if (SOCK_ERRNO == EWOULDBLOCK)
604 /* We might get ECONNRESET here if using TCP and backend died */
606 if (SOCK_ERRNO == ECONNRESET)
607 goto definitelyFailed;
609 printfPQExpBuffer(&conn->errorMessage,
610 libpq_gettext("could not receive data from server: %s\n"),
611 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
616 conn->inEnd += nread;
619 * Hack to deal with the fact that some kernels will only give us
620 * back 1 packet per recv() call, even if we asked for more and
621 * there is more available. If it looks like we are reading a
622 * long message, loop back to recv() again immediately, until we
623 * run out of data or buffer space. Without this, the
624 * block-and-restart behavior of libpq's higher levels leads to
625 * O(N^2) performance on long messages.
627 * Since we left-justified the data above, conn->inEnd gives the
628 * amount of data already read in the current message. We
629 * consider the message "long" once we have acquired 32k ...
631 if (conn->inEnd > 32768 &&
632 (conn->inBufSize - conn->inEnd) >= 8192)
641 return 1; /* got a zero read after successful tries */
644 * A return value of 0 could mean just that no data is now available,
645 * or it could mean EOF --- that is, the server has closed the
646 * connection. Since we have the socket in nonblock mode, the only way
647 * to tell the difference is to see if select() is saying that the
648 * file is ready. Grumble. Fortunately, we don't expect this path to
649 * be taken much, since in normal practice we should not be trying to
650 * read data unless the file selected for reading already.
652 * In SSL mode it's even worse: SSL_read() could say WANT_READ and then
653 * data could arrive before we make the pqReadReady() test. So we
654 * must play dumb and assume there is more data, relying on the SSL
655 * layer to detect true EOF.
663 switch (pqReadReady(conn))
666 /* definitely no data available */
672 goto definitelyFailed;
676 * Still not sure that it's EOF, because some data could have just
680 nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
681 conn->inBufSize - conn->inEnd);
684 if (SOCK_ERRNO == EINTR)
686 /* Some systems return EAGAIN/EWOULDBLOCK for no data */
688 if (SOCK_ERRNO == EAGAIN)
691 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
692 if (SOCK_ERRNO == EWOULDBLOCK)
695 /* We might get ECONNRESET here if using TCP and backend died */
697 if (SOCK_ERRNO == ECONNRESET)
698 goto definitelyFailed;
700 printfPQExpBuffer(&conn->errorMessage,
701 libpq_gettext("could not receive data from server: %s\n"),
702 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
707 conn->inEnd += nread;
712 * OK, we are getting a zero read even though select() says ready.
713 * This means the connection has been closed. Cope.
716 printfPQExpBuffer(&conn->errorMessage,
718 "server closed the connection unexpectedly\n"
719 "\tThis probably means the server terminated abnormally\n"
720 "\tbefore or while processing the request.\n"));
721 conn->status = CONNECTION_BAD; /* No more connection to backend */
722 pqsecure_close(conn);
723 closesocket(conn->sock);
730 * pqSendSome: send data waiting in the output buffer.
732 * len is how much to try to send (typically equal to outCount, but may
735 * Return 0 on success, -1 on failure and 1 when not all data could be sent
736 * because the socket would block and the connection is non-blocking.
739 pqSendSome(PGconn *conn, int len)
741 char *ptr = conn->outBuffer;
742 int remaining = conn->outCount;
747 printfPQExpBuffer(&conn->errorMessage,
748 libpq_gettext("connection not open\n"));
752 /* while there's still data to send */
758 sent = pqsecure_write(conn, ptr, len);
763 * Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble. If
764 * it's EPIPE or ECONNRESET, assume we've lost the backend
765 * connection permanently.
773 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
784 printfPQExpBuffer(&conn->errorMessage,
786 "server closed the connection unexpectedly\n"
787 "\tThis probably means the server terminated abnormally\n"
788 "\tbefore or while processing the request.\n"));
791 * We used to close the socket here, but that's a bad
792 * idea since there might be unread data waiting
793 * (typically, a NOTICE message from the backend
794 * telling us it's committing hara-kiri...). Leave
795 * the socket open until pqReadData finds no more data
796 * can be read. But abandon attempt to send data.
802 printfPQExpBuffer(&conn->errorMessage,
803 libpq_gettext("could not send data to server: %s\n"),
804 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
805 /* We don't assume it's a fatal error... */
820 * We didn't send it all, wait till we can send more.
822 * If the connection is in non-blocking mode we don't wait, but
823 * return 1 to indicate that data is still pending.
825 if (pqIsnonblocking(conn))
832 * There are scenarios in which we can't send data because the
833 * communications channel is full, but we cannot expect the server
834 * to clear the channel eventually because it's blocked trying to
835 * send data to us. (This can happen when we are sending a large
836 * amount of COPY data, and the server has generated lots of
837 * NOTICE responses.) To avoid a deadlock situation, we must be
838 * prepared to accept and buffer incoming data before we try
839 * again. Furthermore, it is possible that such incoming data
840 * might not arrive until after we've gone to sleep. Therefore,
841 * we wait for either read ready or write ready.
843 if (pqReadData(conn) < 0)
845 result = -1; /* error message already set up */
848 if (pqWait(TRUE, TRUE, conn))
856 /* shift the remaining contents of the buffer */
858 memmove(conn->outBuffer, ptr, remaining);
859 conn->outCount = remaining;
866 * pqFlush: send any data waiting in the output buffer
868 * Return 0 on success, -1 on failure and 1 when not all data could be sent
869 * because the socket would block and the connection is non-blocking.
872 pqFlush(PGconn *conn)
875 fflush(conn->Pfdebug);
877 if (conn->outCount > 0)
878 return pqSendSome(conn, conn->outCount);
885 * pqWait: wait until we can read or write the connection socket
887 * JAB: If SSL enabled and used and forRead, buffered bytes short-circuit the
890 * We also stop waiting and return if the kernel flags an exception condition
891 * on the socket. The actual error condition will be detected and reported
892 * when the caller tries to read or write the socket.
895 pqWait(int forRead, int forWrite, PGconn *conn)
897 return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
901 * pqWaitTimed: wait, but not past finish_time.
903 * If finish_time is exceeded then we return failure (EOF). This is like
904 * the response for a kernel exception because we don't want the caller
905 * to try to read/write in that case.
907 * finish_time = ((time_t) -1) disables the wait limit.
910 pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
914 result = pqSocketCheck(conn, forRead, forWrite, finish_time);
917 return EOF; /* errorMessage is already set */
921 printfPQExpBuffer(&conn->errorMessage,
922 libpq_gettext("timeout expired\n"));
930 * pqReadReady: is select() saying the file is ready to read?
931 * Returns -1 on failure, 0 if not ready, 1 if ready.
934 pqReadReady(PGconn *conn)
936 return pqSocketCheck(conn, 1, 0, (time_t) 0);
940 * pqWriteReady: is select() saying the file is ready to write?
941 * Returns -1 on failure, 0 if not ready, 1 if ready.
944 pqWriteReady(PGconn *conn)
946 return pqSocketCheck(conn, 0, 1, (time_t) 0);
950 * Checks a socket, using poll or select, for data to be read, written,
951 * or both. Returns >0 if one or more conditions are met, 0 if it timed
952 * out, -1 if an error occurred.
954 * If SSL is in use, the SSL buffer is checked prior to checking the socket
955 * for read data directly.
958 pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
966 printfPQExpBuffer(&conn->errorMessage,
967 libpq_gettext("socket not open\n"));
972 /* Check for SSL library buffering read bytes */
973 if (forRead && conn->ssl && SSL_pending(conn->ssl) > 0)
975 /* short-circuit the select */
980 /* We will retry as long as we get EINTR */
982 result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
983 while (result < 0 && SOCK_ERRNO == EINTR);
989 printfPQExpBuffer(&conn->errorMessage,
990 libpq_gettext("select() failed: %s\n"),
991 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
999 * Check a file descriptor for read and/or write data, possibly waiting.
1000 * If neither forRead nor forWrite are set, immediately return a timeout
1001 * condition (without waiting). Return >0 if condition is met, 0
1002 * if a timeout occurred, -1 if an error or interrupt occurred.
1004 * Timeout is infinite if end_time is -1. Timeout is immediate (no blocking)
1005 * if end_time is 0 (or indeed, any time before now).
1008 pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
1010 /* We use poll(2) if available, otherwise select(2) */
1012 struct pollfd input_fd;
1015 if (!forRead && !forWrite)
1019 input_fd.events = POLLERR;
1020 input_fd.revents = 0;
1023 input_fd.events |= POLLIN;
1025 input_fd.events |= POLLOUT;
1027 /* Compute appropriate timeout interval */
1028 if (end_time == ((time_t) -1))
1032 time_t now = time(NULL);
1035 timeout_ms = (end_time - now) * 1000;
1040 return poll(&input_fd, 1, timeout_ms);
1042 #else /* !HAVE_POLL */
1047 struct timeval timeout;
1048 struct timeval *ptr_timeout;
1050 if (!forRead && !forWrite)
1053 FD_ZERO(&input_mask);
1054 FD_ZERO(&output_mask);
1055 FD_ZERO(&except_mask);
1057 FD_SET(sock, &input_mask);
1059 FD_SET(sock, &output_mask);
1060 FD_SET(sock, &except_mask);
1062 /* Compute appropriate timeout interval */
1063 if (end_time == ((time_t) -1))
1067 time_t now = time(NULL);
1070 timeout.tv_sec = end_time - now;
1073 timeout.tv_usec = 0;
1074 ptr_timeout = &timeout;
1077 return select(sock + 1, &input_mask, &output_mask,
1078 &except_mask, ptr_timeout);
1079 #endif /* HAVE_POLL */
1084 * A couple of "miscellaneous" multibyte related functions. They used
1085 * to be in fe-print.c but that file is doomed.
1089 * returns the byte length of the word beginning s, using the
1090 * specified encoding.
1093 PQmblen(const unsigned char *s, int encoding)
1095 return (pg_encoding_mblen(encoding, s));
1099 * Get encoding id from environment variable PGCLIENTENCODING.
1102 PQenv2encoding(void)
1105 int encoding = PG_SQL_ASCII;
1107 str = getenv("PGCLIENTENCODING");
1108 if (str && *str != '\0')
1109 encoding = pg_char_to_encoding(str);
1117 libpq_gettext(const char *msgid)
1119 static int already_bound = 0;
1124 bindtextdomain("libpq", LOCALEDIR);
1127 return dgettext("libpq", msgid);
1130 #endif /* ENABLE_NLS */