OSDN Git Service

058a25b8033ebf656e433b651d86a989599e3a99
[pg-rex/syncrep.git] / src / interfaces / libpq / fe-protocol2.c
1 /*-------------------------------------------------------------------------
2  *
3  * fe-protocol2.c
4  *        functions that are specific to frontend/backend protocol version 2
5  *
6  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/interfaces/libpq/fe-protocol2.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres_fe.h"
16
17 #include <ctype.h>
18 #include <fcntl.h>
19
20 #include "libpq-fe.h"
21 #include "libpq-int.h"
22
23
24 #ifdef WIN32
25 #include "win32.h"
26 #else
27 #include <unistd.h>
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_TCP_H
30 #include <netinet/tcp.h>
31 #endif
32 #include <arpa/inet.h>
33 #endif
34
35
36 static int      getRowDescriptions(PGconn *conn);
37 static int      getAnotherTuple(PGconn *conn, bool binary);
38 static int      pqGetErrorNotice2(PGconn *conn, bool isError);
39 static void checkXactStatus(PGconn *conn, const char *cmdTag);
40 static int      getNotify(PGconn *conn);
41
42
43 /*
44  *              pqSetenvPoll
45  *
46  * Polls the process of passing the values of a standard set of environment
47  * variables to the backend.
48  */
49 PostgresPollingStatusType
50 pqSetenvPoll(PGconn *conn)
51 {
52         PGresult   *res;
53
54         if (conn == NULL || conn->status == CONNECTION_BAD)
55                 return PGRES_POLLING_FAILED;
56
57         /* Check whether there are any data for us */
58         switch (conn->setenv_state)
59         {
60                         /* These are reading states */
61                 case SETENV_STATE_OPTION_WAIT:
62                 case SETENV_STATE_QUERY1_WAIT:
63                 case SETENV_STATE_QUERY2_WAIT:
64                         {
65                                 /* Load waiting data */
66                                 int                     n = pqReadData(conn);
67
68                                 if (n < 0)
69                                         goto error_return;
70                                 if (n == 0)
71                                         return PGRES_POLLING_READING;
72
73                                 break;
74                         }
75
76                         /* These are writing states, so we just proceed. */
77                 case SETENV_STATE_OPTION_SEND:
78                 case SETENV_STATE_QUERY1_SEND:
79                 case SETENV_STATE_QUERY2_SEND:
80                         break;
81
82                         /* Should we raise an error if called when not active? */
83                 case SETENV_STATE_IDLE:
84                         return PGRES_POLLING_OK;
85
86                 default:
87                         printfPQExpBuffer(&conn->errorMessage,
88                                                           libpq_gettext(
89                                                                                         "invalid setenv state %c, "
90                                                                  "probably indicative of memory corruption\n"
91                                                                                         ),
92                                                           conn->setenv_state);
93                         goto error_return;
94         }
95
96         /* We will loop here until there is nothing left to do in this call. */
97         for (;;)
98         {
99                 switch (conn->setenv_state)
100                 {
101                         case SETENV_STATE_OPTION_SEND:
102                                 {
103                                         /*
104                                          * Send SET commands for stuff directed by Environment
105                                          * Options.  Note: we assume that SET commands won't start
106                                          * transaction blocks, even in a 7.3 server with
107                                          * autocommit off.
108                                          */
109                                         char            setQuery[100];  /* note length limit in
110                                                                                                  * sprintf below */
111
112                                         if (conn->next_eo->envName)
113                                         {
114                                                 const char *val;
115
116                                                 if ((val = getenv(conn->next_eo->envName)))
117                                                 {
118                                                         if (pg_strcasecmp(val, "default") == 0)
119                                                                 sprintf(setQuery, "SET %s = DEFAULT",
120                                                                                 conn->next_eo->pgName);
121                                                         else
122                                                                 sprintf(setQuery, "SET %s = '%.60s'",
123                                                                                 conn->next_eo->pgName, val);
124 #ifdef CONNECTDEBUG
125                                                         fprintf(stderr,
126                                                                   "Use environment variable %s to send %s\n",
127                                                                         conn->next_eo->envName, setQuery);
128 #endif
129                                                         if (!PQsendQuery(conn, setQuery))
130                                                                 goto error_return;
131
132                                                         conn->setenv_state = SETENV_STATE_OPTION_WAIT;
133                                                 }
134                                                 else
135                                                         conn->next_eo++;
136                                         }
137                                         else
138                                         {
139                                                 /* No more options to send, so move on to querying */
140                                                 conn->setenv_state = SETENV_STATE_QUERY1_SEND;
141                                         }
142                                         break;
143                                 }
144
145                         case SETENV_STATE_OPTION_WAIT:
146                                 {
147                                         if (PQisBusy(conn))
148                                                 return PGRES_POLLING_READING;
149
150                                         res = PQgetResult(conn);
151
152                                         if (res)
153                                         {
154                                                 if (PQresultStatus(res) != PGRES_COMMAND_OK)
155                                                 {
156                                                         PQclear(res);
157                                                         goto error_return;
158                                                 }
159                                                 PQclear(res);
160                                                 /* Keep reading until PQgetResult returns NULL */
161                                         }
162                                         else
163                                         {
164                                                 /* Query finished, so send the next option */
165                                                 conn->next_eo++;
166                                                 conn->setenv_state = SETENV_STATE_OPTION_SEND;
167                                         }
168                                         break;
169                                 }
170
171                         case SETENV_STATE_QUERY1_SEND:
172                                 {
173                                         /*
174                                          * Issue query to get information we need.      Here we must
175                                          * use begin/commit in case autocommit is off by default
176                                          * in a 7.3 server.
177                                          *
178                                          * Note: version() exists in all protocol-2.0-supporting
179                                          * backends.  In 7.3 it would be safer to write
180                                          * pg_catalog.version(), but we can't do that without
181                                          * causing problems on older versions.
182                                          */
183                                         if (!PQsendQuery(conn, "begin; select version(); end"))
184                                                 goto error_return;
185
186                                         conn->setenv_state = SETENV_STATE_QUERY1_WAIT;
187                                         return PGRES_POLLING_READING;
188                                 }
189
190                         case SETENV_STATE_QUERY1_WAIT:
191                                 {
192                                         if (PQisBusy(conn))
193                                                 return PGRES_POLLING_READING;
194
195                                         res = PQgetResult(conn);
196
197                                         if (res)
198                                         {
199                                                 char       *val;
200
201                                                 if (PQresultStatus(res) == PGRES_COMMAND_OK)
202                                                 {
203                                                         /* ignore begin/commit command results */
204                                                         PQclear(res);
205                                                         continue;
206                                                 }
207
208                                                 if (PQresultStatus(res) != PGRES_TUPLES_OK ||
209                                                         PQntuples(res) != 1)
210                                                 {
211                                                         PQclear(res);
212                                                         goto error_return;
213                                                 }
214
215                                                 /*
216                                                  * Extract server version and save as if
217                                                  * ParameterStatus
218                                                  */
219                                                 val = PQgetvalue(res, 0, 0);
220                                                 if (val && strncmp(val, "PostgreSQL ", 11) == 0)
221                                                 {
222                                                         char       *ptr;
223
224                                                         /* strip off PostgreSQL part */
225                                                         val += 11;
226
227                                                         /*
228                                                          * strip off platform part (scribbles on result,
229                                                          * naughty naughty)
230                                                          */
231                                                         ptr = strchr(val, ' ');
232                                                         if (ptr)
233                                                                 *ptr = '\0';
234
235                                                         pqSaveParameterStatus(conn, "server_version",
236                                                                                                   val);
237                                                 }
238
239                                                 PQclear(res);
240                                                 /* Keep reading until PQgetResult returns NULL */
241                                         }
242                                         else
243                                         {
244                                                 /* Query finished, move to next */
245                                                 conn->setenv_state = SETENV_STATE_QUERY2_SEND;
246                                         }
247                                         break;
248                                 }
249
250                         case SETENV_STATE_QUERY2_SEND:
251                                 {
252                                         const char *query;
253
254                                         /*
255                                          * pg_client_encoding does not exist in pre-7.2 servers.
256                                          * So we need to be prepared for an error here.  Do *not*
257                                          * start a transaction block, except in 7.3 servers where
258                                          * we need to prevent autocommit-off from starting a
259                                          * transaction anyway.
260                                          */
261                                         if (conn->sversion >= 70300 &&
262                                                 conn->sversion < 70400)
263                                                 query = "begin; select pg_catalog.pg_client_encoding(); end";
264                                         else
265                                                 query = "select pg_client_encoding()";
266                                         if (!PQsendQuery(conn, query))
267                                                 goto error_return;
268
269                                         conn->setenv_state = SETENV_STATE_QUERY2_WAIT;
270                                         return PGRES_POLLING_READING;
271                                 }
272
273                         case SETENV_STATE_QUERY2_WAIT:
274                                 {
275                                         if (PQisBusy(conn))
276                                                 return PGRES_POLLING_READING;
277
278                                         res = PQgetResult(conn);
279
280                                         if (res)
281                                         {
282                                                 const char *val;
283
284                                                 if (PQresultStatus(res) == PGRES_COMMAND_OK)
285                                                 {
286                                                         /* ignore begin/commit command results */
287                                                         PQclear(res);
288                                                         continue;
289                                                 }
290
291                                                 if (PQresultStatus(res) == PGRES_TUPLES_OK &&
292                                                         PQntuples(res) == 1)
293                                                 {
294                                                         /* Extract client encoding and save it */
295                                                         val = PQgetvalue(res, 0, 0);
296                                                         if (val && *val)        /* null should not happen, but */
297                                                                 pqSaveParameterStatus(conn, "client_encoding",
298                                                                                                           val);
299                                                 }
300                                                 else
301                                                 {
302                                                         /*
303                                                          * Error: presumably function not available, so
304                                                          * use PGCLIENTENCODING or SQL_ASCII as the
305                                                          * fallback.
306                                                          */
307                                                         val = getenv("PGCLIENTENCODING");
308                                                         if (val && *val)
309                                                                 pqSaveParameterStatus(conn, "client_encoding",
310                                                                                                           val);
311                                                         else
312                                                                 pqSaveParameterStatus(conn, "client_encoding",
313                                                                                                           "SQL_ASCII");
314                                                 }
315
316                                                 PQclear(res);
317                                                 /* Keep reading until PQgetResult returns NULL */
318                                         }
319                                         else
320                                         {
321                                                 /* Query finished, so we're done */
322                                                 conn->setenv_state = SETENV_STATE_IDLE;
323                                                 return PGRES_POLLING_OK;
324                                         }
325                                         break;
326                                 }
327
328                         default:
329                                 printfPQExpBuffer(&conn->errorMessage,
330                                                                   libpq_gettext("invalid state %c, "
331                                                            "probably indicative of memory corruption\n"),
332                                                                   conn->setenv_state);
333                                 goto error_return;
334                 }
335         }
336
337         /* Unreachable */
338
339 error_return:
340         conn->setenv_state = SETENV_STATE_IDLE;
341         return PGRES_POLLING_FAILED;
342 }
343
344
345 /*
346  * parseInput: if appropriate, parse input data from backend
347  * until input is exhausted or a stopping state is reached.
348  * Note that this function will NOT attempt to read more data from the backend.
349  */
350 void
351 pqParseInput2(PGconn *conn)
352 {
353         char            id;
354
355         /*
356          * Loop to parse successive complete messages available in the buffer.
357          */
358         for (;;)
359         {
360                 /*
361                  * Quit if in COPY_OUT state: we expect raw data from the server until
362                  * PQendcopy is called.  Don't try to parse it according to the normal
363                  * protocol.  (This is bogus.  The data lines ought to be part of the
364                  * protocol and have identifying leading characters.)
365                  */
366                 if (conn->asyncStatus == PGASYNC_COPY_OUT)
367                         return;
368
369                 /*
370                  * OK to try to read a message type code.
371                  */
372                 conn->inCursor = conn->inStart;
373                 if (pqGetc(&id, conn))
374                         return;
375
376                 /*
377                  * NOTIFY and NOTICE messages can happen in any state besides COPY
378                  * OUT; always process them right away.
379                  *
380                  * Most other messages should only be processed while in BUSY state.
381                  * (In particular, in READY state we hold off further parsing until
382                  * the application collects the current PGresult.)
383                  *
384                  * However, if the state is IDLE then we got trouble; we need to deal
385                  * with the unexpected message somehow.
386                  */
387                 if (id == 'A')
388                 {
389                         if (getNotify(conn))
390                                 return;
391                 }
392                 else if (id == 'N')
393                 {
394                         if (pqGetErrorNotice2(conn, false))
395                                 return;
396                 }
397                 else if (conn->asyncStatus != PGASYNC_BUSY)
398                 {
399                         /* If not IDLE state, just wait ... */
400                         if (conn->asyncStatus != PGASYNC_IDLE)
401                                 return;
402
403                         /*
404                          * Unexpected message in IDLE state; need to recover somehow.
405                          * ERROR messages are displayed using the notice processor;
406                          * anything else is just dropped on the floor after displaying a
407                          * suitable warning notice.  (An ERROR is very possibly the
408                          * backend telling us why it is about to close the connection, so
409                          * we don't want to just discard it...)
410                          */
411                         if (id == 'E')
412                         {
413                                 if (pqGetErrorNotice2(conn, false /* treat as notice */ ))
414                                         return;
415                         }
416                         else
417                         {
418                                 pqInternalNotice(&conn->noticeHooks,
419                                                 "message type 0x%02x arrived from server while idle",
420                                                                  id);
421                                 /* Discard the unexpected message; good idea?? */
422                                 conn->inStart = conn->inEnd;
423                                 break;
424                         }
425                 }
426                 else
427                 {
428                         /*
429                          * In BUSY state, we can process everything.
430                          */
431                         switch (id)
432                         {
433                                 case 'C':               /* command complete */
434                                         if (pqGets(&conn->workBuffer, conn))
435                                                 return;
436                                         if (conn->result == NULL)
437                                         {
438                                                 conn->result = PQmakeEmptyPGresult(conn,
439                                                                                                                    PGRES_COMMAND_OK);
440                                                 if (!conn->result)
441                                                         return;
442                                         }
443                                         strncpy(conn->result->cmdStatus, conn->workBuffer.data,
444                                                         CMDSTATUS_LEN);
445                                         checkXactStatus(conn, conn->workBuffer.data);
446                                         conn->asyncStatus = PGASYNC_READY;
447                                         break;
448                                 case 'E':               /* error return */
449                                         if (pqGetErrorNotice2(conn, true))
450                                                 return;
451                                         conn->asyncStatus = PGASYNC_READY;
452                                         break;
453                                 case 'Z':               /* backend is ready for new query */
454                                         conn->asyncStatus = PGASYNC_IDLE;
455                                         break;
456                                 case 'I':               /* empty query */
457                                         /* read and throw away the closing '\0' */
458                                         if (pqGetc(&id, conn))
459                                                 return;
460                                         if (id != '\0')
461                                                 pqInternalNotice(&conn->noticeHooks,
462                                                                                  "unexpected character %c following empty query response (\"I\" message)",
463                                                                                  id);
464                                         if (conn->result == NULL)
465                                                 conn->result = PQmakeEmptyPGresult(conn,
466                                                                                                                    PGRES_EMPTY_QUERY);
467                                         conn->asyncStatus = PGASYNC_READY;
468                                         break;
469                                 case 'K':               /* secret key data from the backend */
470
471                                         /*
472                                          * This is expected only during backend startup, but it's
473                                          * just as easy to handle it as part of the main loop.
474                                          * Save the data and continue processing.
475                                          */
476                                         if (pqGetInt(&(conn->be_pid), 4, conn))
477                                                 return;
478                                         if (pqGetInt(&(conn->be_key), 4, conn))
479                                                 return;
480                                         break;
481                                 case 'P':               /* synchronous (normal) portal */
482                                         if (pqGets(&conn->workBuffer, conn))
483                                                 return;
484                                         /* We pretty much ignore this message type... */
485                                         break;
486                                 case 'T':               /* row descriptions (start of query results) */
487                                         if (conn->result == NULL)
488                                         {
489                                                 /* First 'T' in a query sequence */
490                                                 if (getRowDescriptions(conn))
491                                                         return;
492                                         }
493                                         else
494                                         {
495                                                 /*
496                                                  * A new 'T' message is treated as the start of
497                                                  * another PGresult.  (It is not clear that this is
498                                                  * really possible with the current backend.) We stop
499                                                  * parsing until the application accepts the current
500                                                  * result.
501                                                  */
502                                                 conn->asyncStatus = PGASYNC_READY;
503                                                 return;
504                                         }
505                                         break;
506                                 case 'D':               /* ASCII data tuple */
507                                         if (conn->result != NULL)
508                                         {
509                                                 /* Read another tuple of a normal query response */
510                                                 if (getAnotherTuple(conn, FALSE))
511                                                         return;
512                                         }
513                                         else
514                                         {
515                                                 pqInternalNotice(&conn->noticeHooks,
516                                                                                  "server sent data (\"D\" message) without prior row description (\"T\" message)");
517                                                 /* Discard the unexpected message; good idea?? */
518                                                 conn->inStart = conn->inEnd;
519                                                 return;
520                                         }
521                                         break;
522                                 case 'B':               /* Binary data tuple */
523                                         if (conn->result != NULL)
524                                         {
525                                                 /* Read another tuple of a normal query response */
526                                                 if (getAnotherTuple(conn, TRUE))
527                                                         return;
528                                         }
529                                         else
530                                         {
531                                                 pqInternalNotice(&conn->noticeHooks,
532                                                                                  "server sent binary data (\"B\" message) without prior row description (\"T\" message)");
533                                                 /* Discard the unexpected message; good idea?? */
534                                                 conn->inStart = conn->inEnd;
535                                                 return;
536                                         }
537                                         break;
538                                 case 'G':               /* Start Copy In */
539                                         conn->asyncStatus = PGASYNC_COPY_IN;
540                                         break;
541                                 case 'H':               /* Start Copy Out */
542                                         conn->asyncStatus = PGASYNC_COPY_OUT;
543                                         break;
544                                         /*
545                                          * Don't need to process CopyBothResponse here because
546                                          * it never arrives from the server during protocol 2.0.
547                                          */
548                                 default:
549                                         printfPQExpBuffer(&conn->errorMessage,
550                                                                           libpq_gettext(
551                                                                                                         "unexpected response from server; first received character was \"%c\"\n"),
552                                                                           id);
553                                         /* build an error result holding the error message */
554                                         pqSaveErrorResult(conn);
555                                         /* Discard the unexpected message; good idea?? */
556                                         conn->inStart = conn->inEnd;
557                                         conn->asyncStatus = PGASYNC_READY;
558                                         return;
559                         }                                       /* switch on protocol character */
560                 }
561                 /* Successfully consumed this message */
562                 conn->inStart = conn->inCursor;
563         }
564 }
565
566 /*
567  * parseInput subroutine to read a 'T' (row descriptions) message.
568  * We build a PGresult structure containing the attribute data.
569  * Returns: 0 if completed message, EOF if not enough data yet.
570  *
571  * Note that if we run out of data, we have to release the partially
572  * constructed PGresult, and rebuild it again next time.  Fortunately,
573  * that shouldn't happen often, since 'T' messages usually fit in a packet.
574  */
575 static int
576 getRowDescriptions(PGconn *conn)
577 {
578         PGresult   *result = NULL;
579         int                     nfields;
580         int                     i;
581
582         result = PQmakeEmptyPGresult(conn, PGRES_TUPLES_OK);
583         if (!result)
584                 goto failure;
585
586         /* parseInput already read the 'T' label. */
587         /* the next two bytes are the number of fields  */
588         if (pqGetInt(&(result->numAttributes), 2, conn))
589                 goto failure;
590         nfields = result->numAttributes;
591
592         /* allocate space for the attribute descriptors */
593         if (nfields > 0)
594         {
595                 result->attDescs = (PGresAttDesc *)
596                         pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
597                 if (!result->attDescs)
598                         goto failure;
599                 MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
600         }
601
602         /* get type info */
603         for (i = 0; i < nfields; i++)
604         {
605                 int                     typid;
606                 int                     typlen;
607                 int                     atttypmod;
608
609                 if (pqGets(&conn->workBuffer, conn) ||
610                         pqGetInt(&typid, 4, conn) ||
611                         pqGetInt(&typlen, 2, conn) ||
612                         pqGetInt(&atttypmod, 4, conn))
613                         goto failure;
614
615                 /*
616                  * Since pqGetInt treats 2-byte integers as unsigned, we need to
617                  * coerce the result to signed form.
618                  */
619                 typlen = (int) ((int16) typlen);
620
621                 result->attDescs[i].name = pqResultStrdup(result,
622                                                                                                   conn->workBuffer.data);
623                 if (!result->attDescs[i].name)
624                         goto failure;
625                 result->attDescs[i].tableid = 0;
626                 result->attDescs[i].columnid = 0;
627                 result->attDescs[i].format = 0;
628                 result->attDescs[i].typid = typid;
629                 result->attDescs[i].typlen = typlen;
630                 result->attDescs[i].atttypmod = atttypmod;
631         }
632
633         /* Success! */
634         conn->result = result;
635         return 0;
636
637 failure:
638         if (result)
639                 PQclear(result);
640         return EOF;
641 }
642
643 /*
644  * parseInput subroutine to read a 'B' or 'D' (row data) message.
645  * We add another tuple to the existing PGresult structure.
646  * Returns: 0 if completed message, EOF if error or not enough data yet.
647  *
648  * Note that if we run out of data, we have to suspend and reprocess
649  * the message after more data is received.  We keep a partially constructed
650  * tuple in conn->curTuple, and avoid reallocating already-allocated storage.
651  */
652 static int
653 getAnotherTuple(PGconn *conn, bool binary)
654 {
655         PGresult   *result = conn->result;
656         int                     nfields = result->numAttributes;
657         PGresAttValue *tup;
658
659         /* the backend sends us a bitmap of which attributes are null */
660         char            std_bitmap[64]; /* used unless it doesn't fit */
661         char       *bitmap = std_bitmap;
662         int                     i;
663         size_t          nbytes;                 /* the number of bytes in bitmap  */
664         char            bmap;                   /* One byte of the bitmap */
665         int                     bitmap_index;   /* Its index */
666         int                     bitcnt;                 /* number of bits examined in current byte */
667         int                     vlen;                   /* length of the current field value */
668
669         result->binary = binary;
670
671         /* Allocate tuple space if first time for this data message */
672         if (conn->curTuple == NULL)
673         {
674                 conn->curTuple = (PGresAttValue *)
675                         pqResultAlloc(result, nfields * sizeof(PGresAttValue), TRUE);
676                 if (conn->curTuple == NULL)
677                         goto outOfMemory;
678                 MemSet(conn->curTuple, 0, nfields * sizeof(PGresAttValue));
679
680                 /*
681                  * If it's binary, fix the column format indicators.  We assume the
682                  * backend will consistently send either B or D, not a mix.
683                  */
684                 if (binary)
685                 {
686                         for (i = 0; i < nfields; i++)
687                                 result->attDescs[i].format = 1;
688                 }
689         }
690         tup = conn->curTuple;
691
692         /* Get the null-value bitmap */
693         nbytes = (nfields + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
694         /* malloc() only for unusually large field counts... */
695         if (nbytes > sizeof(std_bitmap))
696         {
697                 bitmap = (char *) malloc(nbytes);
698                 if (!bitmap)
699                         goto outOfMemory;
700         }
701
702         if (pqGetnchar(bitmap, nbytes, conn))
703                 goto EOFexit;
704
705         /* Scan the fields */
706         bitmap_index = 0;
707         bmap = bitmap[bitmap_index];
708         bitcnt = 0;
709
710         for (i = 0; i < nfields; i++)
711         {
712                 if (!(bmap & 0200))
713                 {
714                         /* if the field value is absent, make it a null string */
715                         tup[i].value = result->null_field;
716                         tup[i].len = NULL_LEN;
717                 }
718                 else
719                 {
720                         /* get the value length (the first four bytes are for length) */
721                         if (pqGetInt(&vlen, 4, conn))
722                                 goto EOFexit;
723                         if (!binary)
724                                 vlen = vlen - 4;
725                         if (vlen < 0)
726                                 vlen = 0;
727                         if (tup[i].value == NULL)
728                         {
729                                 tup[i].value = (char *) pqResultAlloc(result, vlen + 1, binary);
730                                 if (tup[i].value == NULL)
731                                         goto outOfMemory;
732                         }
733                         tup[i].len = vlen;
734                         /* read in the value */
735                         if (vlen > 0)
736                                 if (pqGetnchar((char *) (tup[i].value), vlen, conn))
737                                         goto EOFexit;
738                         /* we have to terminate this ourselves */
739                         tup[i].value[vlen] = '\0';
740                 }
741                 /* advance the bitmap stuff */
742                 bitcnt++;
743                 if (bitcnt == BITS_PER_BYTE)
744                 {
745                         bitmap_index++;
746                         bmap = bitmap[bitmap_index];
747                         bitcnt = 0;
748                 }
749                 else
750                         bmap <<= 1;
751         }
752
753         /* Success!  Store the completed tuple in the result */
754         if (!pqAddTuple(result, tup))
755                 goto outOfMemory;
756         /* and reset for a new message */
757         conn->curTuple = NULL;
758
759         if (bitmap != std_bitmap)
760                 free(bitmap);
761         return 0;
762
763 outOfMemory:
764         /* Replace partially constructed result with an error result */
765
766         /*
767          * we do NOT use pqSaveErrorResult() here, because of the likelihood that
768          * there's not enough memory to concatenate messages...
769          */
770         pqClearAsyncResult(conn);
771         printfPQExpBuffer(&conn->errorMessage,
772                                           libpq_gettext("out of memory for query result\n"));
773
774         /*
775          * XXX: if PQmakeEmptyPGresult() fails, there's probably not much we can
776          * do to recover...
777          */
778         conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
779         conn->asyncStatus = PGASYNC_READY;
780         /* Discard the failed message --- good idea? */
781         conn->inStart = conn->inEnd;
782
783 EOFexit:
784         if (bitmap != NULL && bitmap != std_bitmap)
785                 free(bitmap);
786         return EOF;
787 }
788
789
790 /*
791  * Attempt to read an Error or Notice response message.
792  * This is possible in several places, so we break it out as a subroutine.
793  * Entry: 'E' or 'N' message type has already been consumed.
794  * Exit: returns 0 if successfully consumed message.
795  *               returns EOF if not enough data.
796  */
797 static int
798 pqGetErrorNotice2(PGconn *conn, bool isError)
799 {
800         PGresult   *res = NULL;
801         PQExpBufferData workBuf;
802         char       *startp;
803         char       *splitp;
804
805         /*
806          * Since the message might be pretty long, we create a temporary
807          * PQExpBuffer rather than using conn->workBuffer.      workBuffer is intended
808          * for stuff that is expected to be short.
809          */
810         initPQExpBuffer(&workBuf);
811         if (pqGets(&workBuf, conn))
812                 goto failure;
813
814         /*
815          * Make a PGresult to hold the message.  We temporarily lie about the
816          * result status, so that PQmakeEmptyPGresult doesn't uselessly copy
817          * conn->errorMessage.
818          */
819         res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
820         if (!res)
821                 goto failure;
822         res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR;
823         res->errMsg = pqResultStrdup(res, workBuf.data);
824         if (!res->errMsg)
825                 goto failure;
826
827         /*
828          * Break the message into fields.  We can't do very much here, but we can
829          * split the severity code off, and remove trailing newlines. Also, we use
830          * the heuristic that the primary message extends only to the first
831          * newline --- anything after that is detail message.  (In some cases it'd
832          * be better classed as hint, but we can hardly be expected to guess that
833          * here.)
834          */
835         while (workBuf.len > 0 && workBuf.data[workBuf.len - 1] == '\n')
836                 workBuf.data[--workBuf.len] = '\0';
837         splitp = strstr(workBuf.data, ":  ");
838         if (splitp)
839         {
840                 /* what comes before the colon is severity */
841                 *splitp = '\0';
842                 pqSaveMessageField(res, PG_DIAG_SEVERITY, workBuf.data);
843                 startp = splitp + 3;
844         }
845         else
846         {
847                 /* can't find a colon?  oh well... */
848                 startp = workBuf.data;
849         }
850         splitp = strchr(startp, '\n');
851         if (splitp)
852         {
853                 /* what comes before the newline is primary message */
854                 *splitp++ = '\0';
855                 pqSaveMessageField(res, PG_DIAG_MESSAGE_PRIMARY, startp);
856                 /* the rest is detail; strip any leading whitespace */
857                 while (*splitp && isspace((unsigned char) *splitp))
858                         splitp++;
859                 pqSaveMessageField(res, PG_DIAG_MESSAGE_DETAIL, splitp);
860         }
861         else
862         {
863                 /* single-line message, so all primary */
864                 pqSaveMessageField(res, PG_DIAG_MESSAGE_PRIMARY, startp);
865         }
866
867         /*
868          * Either save error as current async result, or just emit the notice.
869          * Also, if it's an error and we were in a transaction block, assume the
870          * server has now gone to error-in-transaction state.
871          */
872         if (isError)
873         {
874                 pqClearAsyncResult(conn);
875                 conn->result = res;
876                 resetPQExpBuffer(&conn->errorMessage);
877                 appendPQExpBufferStr(&conn->errorMessage, res->errMsg);
878                 if (conn->xactStatus == PQTRANS_INTRANS)
879                         conn->xactStatus = PQTRANS_INERROR;
880         }
881         else
882         {
883                 if (res->noticeHooks.noticeRec != NULL)
884                         (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
885                 PQclear(res);
886         }
887
888         termPQExpBuffer(&workBuf);
889         return 0;
890
891 failure:
892         if (res)
893                 PQclear(res);
894         termPQExpBuffer(&workBuf);
895         return EOF;
896 }
897
898 /*
899  * checkXactStatus - attempt to track transaction-block status of server
900  *
901  * This is called each time we receive a command-complete message.      By
902  * watching for messages from BEGIN/COMMIT/ROLLBACK commands, we can do
903  * a passable job of tracking the server's xact status.  BUT: this does
904  * not work at all on 7.3 servers with AUTOCOMMIT OFF.  (Man, was that
905  * feature ever a mistake.)  Caveat user.
906  *
907  * The tags known here are all those used as far back as 7.0; is it worth
908  * adding those from even-older servers?
909  */
910 static void
911 checkXactStatus(PGconn *conn, const char *cmdTag)
912 {
913         if (strcmp(cmdTag, "BEGIN") == 0)
914                 conn->xactStatus = PQTRANS_INTRANS;
915         else if (strcmp(cmdTag, "COMMIT") == 0)
916                 conn->xactStatus = PQTRANS_IDLE;
917         else if (strcmp(cmdTag, "ROLLBACK") == 0)
918                 conn->xactStatus = PQTRANS_IDLE;
919         else if (strcmp(cmdTag, "START TRANSACTION") == 0)      /* 7.3 only */
920                 conn->xactStatus = PQTRANS_INTRANS;
921
922         /*
923          * Normally we get into INERROR state by detecting an Error message.
924          * However, if we see one of these tags then we know for sure the server
925          * is in abort state ...
926          */
927         else if (strcmp(cmdTag, "*ABORT STATE*") == 0)          /* pre-7.3 only */
928                 conn->xactStatus = PQTRANS_INERROR;
929 }
930
931 /*
932  * Attempt to read a Notify response message.
933  * This is possible in several places, so we break it out as a subroutine.
934  * Entry: 'A' message type and length have already been consumed.
935  * Exit: returns 0 if successfully consumed Notify message.
936  *               returns EOF if not enough data.
937  */
938 static int
939 getNotify(PGconn *conn)
940 {
941         int                     be_pid;
942         int                     nmlen;
943         PGnotify   *newNotify;
944
945         if (pqGetInt(&be_pid, 4, conn))
946                 return EOF;
947         if (pqGets(&conn->workBuffer, conn))
948                 return EOF;
949
950         /*
951          * Store the relation name right after the PQnotify structure so it can
952          * all be freed at once.  We don't use NAMEDATALEN because we don't want
953          * to tie this interface to a specific server name length.
954          */
955         nmlen = strlen(conn->workBuffer.data);
956         newNotify = (PGnotify *) malloc(sizeof(PGnotify) + nmlen + 1);
957         if (newNotify)
958         {
959                 newNotify->relname = (char *) newNotify + sizeof(PGnotify);
960                 strcpy(newNotify->relname, conn->workBuffer.data);
961                 /* fake up an empty-string extra field */
962                 newNotify->extra = newNotify->relname + nmlen;
963                 newNotify->be_pid = be_pid;
964                 newNotify->next = NULL;
965                 if (conn->notifyTail)
966                         conn->notifyTail->next = newNotify;
967                 else
968                         conn->notifyHead = newNotify;
969                 conn->notifyTail = newNotify;
970         }
971
972         return 0;
973 }
974
975
976 /*
977  * PQgetCopyData - read a row of data from the backend during COPY OUT
978  *
979  * If successful, sets *buffer to point to a malloc'd row of data, and
980  * returns row length (always > 0) as result.
981  * Returns 0 if no row available yet (only possible if async is true),
982  * -1 if end of copy (consult PQgetResult), or -2 if error (consult
983  * PQerrorMessage).
984  */
985 int
986 pqGetCopyData2(PGconn *conn, char **buffer, int async)
987 {
988         bool            found;
989         int                     msgLength;
990
991         for (;;)
992         {
993                 /*
994                  * Do we have a complete line of data?
995                  */
996                 conn->inCursor = conn->inStart;
997                 found = false;
998                 while (conn->inCursor < conn->inEnd)
999                 {
1000                         char            c = conn->inBuffer[conn->inCursor++];
1001
1002                         if (c == '\n')
1003                         {
1004                                 found = true;
1005                                 break;
1006                         }
1007                 }
1008                 if (!found)
1009                         goto nodata;
1010                 msgLength = conn->inCursor - conn->inStart;
1011
1012                 /*
1013                  * If it's the end-of-data marker, consume it, exit COPY_OUT mode, and
1014                  * let caller read status with PQgetResult().
1015                  */
1016                 if (msgLength == 3 &&
1017                         strncmp(&conn->inBuffer[conn->inStart], "\\.\n", 3) == 0)
1018                 {
1019                         conn->inStart = conn->inCursor;
1020                         conn->asyncStatus = PGASYNC_BUSY;
1021                         return -1;
1022                 }
1023
1024                 /*
1025                  * Pass the line back to the caller.
1026                  */
1027                 *buffer = (char *) malloc(msgLength + 1);
1028                 if (*buffer == NULL)
1029                 {
1030                         printfPQExpBuffer(&conn->errorMessage,
1031                                                           libpq_gettext("out of memory\n"));
1032                         return -2;
1033                 }
1034                 memcpy(*buffer, &conn->inBuffer[conn->inStart], msgLength);
1035                 (*buffer)[msgLength] = '\0';    /* Add terminating null */
1036
1037                 /* Mark message consumed */
1038                 conn->inStart = conn->inCursor;
1039
1040                 return msgLength;
1041
1042 nodata:
1043                 /* Don't block if async read requested */
1044                 if (async)
1045                         return 0;
1046                 /* Need to load more data */
1047                 if (pqWait(TRUE, FALSE, conn) ||
1048                         pqReadData(conn) < 0)
1049                         return -2;
1050         }
1051 }
1052
1053
1054 /*
1055  * PQgetline - gets a newline-terminated string from the backend.
1056  *
1057  * See fe-exec.c for documentation.
1058  */
1059 int
1060 pqGetline2(PGconn *conn, char *s, int maxlen)
1061 {
1062         int                     result = 1;             /* return value if buffer overflows */
1063
1064         if (conn->sock < 0)
1065         {
1066                 *s = '\0';
1067                 return EOF;
1068         }
1069
1070         /*
1071          * Since this is a purely synchronous routine, we don't bother to maintain
1072          * conn->inCursor; there is no need to back up.
1073          */
1074         while (maxlen > 1)
1075         {
1076                 if (conn->inStart < conn->inEnd)
1077                 {
1078                         char            c = conn->inBuffer[conn->inStart++];
1079
1080                         if (c == '\n')
1081                         {
1082                                 result = 0;             /* success exit */
1083                                 break;
1084                         }
1085                         *s++ = c;
1086                         maxlen--;
1087                 }
1088                 else
1089                 {
1090                         /* need to load more data */
1091                         if (pqWait(TRUE, FALSE, conn) ||
1092                                 pqReadData(conn) < 0)
1093                         {
1094                                 result = EOF;
1095                                 break;
1096                         }
1097                 }
1098         }
1099         *s = '\0';
1100
1101         return result;
1102 }
1103
1104 /*
1105  * PQgetlineAsync - gets a COPY data row without blocking.
1106  *
1107  * See fe-exec.c for documentation.
1108  */
1109 int
1110 pqGetlineAsync2(PGconn *conn, char *buffer, int bufsize)
1111 {
1112         int                     avail;
1113
1114         if (conn->asyncStatus != PGASYNC_COPY_OUT)
1115                 return -1;                              /* we are not doing a copy... */
1116
1117         /*
1118          * Move data from libpq's buffer to the caller's. We want to accept data
1119          * only in units of whole lines, not partial lines.  This ensures that we
1120          * can recognize the terminator line "\\.\n".  (Otherwise, if it happened
1121          * to cross a packet/buffer boundary, we might hand the first one or two
1122          * characters off to the caller, which we shouldn't.)
1123          */
1124
1125         conn->inCursor = conn->inStart;
1126
1127         avail = bufsize;
1128         while (avail > 0 && conn->inCursor < conn->inEnd)
1129         {
1130                 char            c = conn->inBuffer[conn->inCursor++];
1131
1132                 *buffer++ = c;
1133                 --avail;
1134                 if (c == '\n')
1135                 {
1136                         /* Got a complete line; mark the data removed from libpq */
1137                         conn->inStart = conn->inCursor;
1138                         /* Is it the endmarker line? */
1139                         if (bufsize - avail == 3 && buffer[-3] == '\\' && buffer[-2] == '.')
1140                                 return -1;
1141                         /* No, return the data line to the caller */
1142                         return bufsize - avail;
1143                 }
1144         }
1145
1146         /*
1147          * We don't have a complete line. We'd prefer to leave it in libpq's
1148          * buffer until the rest arrives, but there is a special case: what if the
1149          * line is longer than the buffer the caller is offering us?  In that case
1150          * we'd better hand over a partial line, else we'd get into an infinite
1151          * loop. Do this in a way that ensures we can't misrecognize a terminator
1152          * line later: leave last 3 characters in libpq buffer.
1153          */
1154         if (avail == 0 && bufsize > 3)
1155         {
1156                 conn->inStart = conn->inCursor - 3;
1157                 return bufsize - 3;
1158         }
1159         return 0;
1160 }
1161
1162 /*
1163  * PQendcopy
1164  *
1165  * See fe-exec.c for documentation.
1166  */
1167 int
1168 pqEndcopy2(PGconn *conn)
1169 {
1170         PGresult   *result;
1171
1172         if (conn->asyncStatus != PGASYNC_COPY_IN &&
1173                 conn->asyncStatus != PGASYNC_COPY_OUT)
1174         {
1175                 printfPQExpBuffer(&conn->errorMessage,
1176                                                   libpq_gettext("no COPY in progress\n"));
1177                 return 1;
1178         }
1179
1180         /*
1181          * make sure no data is waiting to be sent, abort if we are non-blocking
1182          * and the flush fails
1183          */
1184         if (pqFlush(conn) && pqIsnonblocking(conn))
1185                 return 1;
1186
1187         /* non blocking connections may have to abort at this point. */
1188         if (pqIsnonblocking(conn) && PQisBusy(conn))
1189                 return 1;
1190
1191         /* Return to active duty */
1192         conn->asyncStatus = PGASYNC_BUSY;
1193         resetPQExpBuffer(&conn->errorMessage);
1194
1195         /* Wait for the completion response */
1196         result = PQgetResult(conn);
1197
1198         /* Expecting a successful result */
1199         if (result && result->resultStatus == PGRES_COMMAND_OK)
1200         {
1201                 PQclear(result);
1202                 return 0;
1203         }
1204
1205         /*
1206          * Trouble. For backwards-compatibility reasons, we issue the error
1207          * message as if it were a notice (would be nice to get rid of this
1208          * silliness, but too many apps probably don't handle errors from
1209          * PQendcopy reasonably).  Note that the app can still obtain the error
1210          * status from the PGconn object.
1211          */
1212         if (conn->errorMessage.len > 0)
1213         {
1214                 /* We have to strip the trailing newline ... pain in neck... */
1215                 char            svLast = conn->errorMessage.data[conn->errorMessage.len - 1];
1216
1217                 if (svLast == '\n')
1218                         conn->errorMessage.data[conn->errorMessage.len - 1] = '\0';
1219                 pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
1220                 conn->errorMessage.data[conn->errorMessage.len - 1] = svLast;
1221         }
1222
1223         PQclear(result);
1224
1225         /*
1226          * The worst case is that we've lost sync with the backend entirely due to
1227          * application screwup of the copy in/out protocol. To recover, reset the
1228          * connection (talk about using a sledgehammer...)
1229          */
1230         pqInternalNotice(&conn->noticeHooks,
1231                                    "lost synchronization with server, resetting connection");
1232
1233         /*
1234          * Users doing non-blocking connections need to handle the reset
1235          * themselves, they'll need to check the connection status if we return an
1236          * error.
1237          */
1238         if (pqIsnonblocking(conn))
1239                 PQresetStart(conn);
1240         else
1241                 PQreset(conn);
1242
1243         return 1;
1244 }
1245
1246
1247 /*
1248  * PQfn - Send a function call to the POSTGRES backend.
1249  *
1250  * See fe-exec.c for documentation.
1251  */
1252 PGresult *
1253 pqFunctionCall2(PGconn *conn, Oid fnid,
1254                                 int *result_buf, int *actual_result_len,
1255                                 int result_is_int,
1256                                 const PQArgBlock *args, int nargs)
1257 {
1258         bool            needInput = false;
1259         ExecStatusType status = PGRES_FATAL_ERROR;
1260         char            id;
1261         int                     i;
1262
1263         /* PQfn already validated connection state */
1264
1265         if (pqPutMsgStart('F', false, conn) < 0 ||      /* function call msg */
1266                 pqPuts(" ", conn) < 0 ||        /* dummy string */
1267                 pqPutInt(fnid, 4, conn) != 0 || /* function id */
1268                 pqPutInt(nargs, 4, conn) != 0)  /* # of args */
1269         {
1270                 pqHandleSendFailure(conn);
1271                 return NULL;
1272         }
1273
1274         for (i = 0; i < nargs; ++i)
1275         {                                                       /* len.int4 + contents     */
1276                 if (pqPutInt(args[i].len, 4, conn))
1277                 {
1278                         pqHandleSendFailure(conn);
1279                         return NULL;
1280                 }
1281
1282                 if (args[i].isint)
1283                 {
1284                         if (pqPutInt(args[i].u.integer, 4, conn))
1285                         {
1286                                 pqHandleSendFailure(conn);
1287                                 return NULL;
1288                         }
1289                 }
1290                 else
1291                 {
1292                         if (pqPutnchar((char *) args[i].u.ptr, args[i].len, conn))
1293                         {
1294                                 pqHandleSendFailure(conn);
1295                                 return NULL;
1296                         }
1297                 }
1298         }
1299
1300         if (pqPutMsgEnd(conn) < 0 ||
1301                 pqFlush(conn))
1302         {
1303                 pqHandleSendFailure(conn);
1304                 return NULL;
1305         }
1306
1307         for (;;)
1308         {
1309                 if (needInput)
1310                 {
1311                         /* Wait for some data to arrive (or for the channel to close) */
1312                         if (pqWait(TRUE, FALSE, conn) ||
1313                                 pqReadData(conn) < 0)
1314                                 break;
1315                 }
1316
1317                 /*
1318                  * Scan the message. If we run out of data, loop around to try again.
1319                  */
1320                 conn->inCursor = conn->inStart;
1321                 needInput = true;
1322
1323                 if (pqGetc(&id, conn))
1324                         continue;
1325
1326                 /*
1327                  * We should see V or E response to the command, but might get N
1328                  * and/or A notices first. We also need to swallow the final Z before
1329                  * returning.
1330                  */
1331                 switch (id)
1332                 {
1333                         case 'V':                       /* function result */
1334                                 if (pqGetc(&id, conn))
1335                                         continue;
1336                                 if (id == 'G')
1337                                 {
1338                                         /* function returned nonempty value */
1339                                         if (pqGetInt(actual_result_len, 4, conn))
1340                                                 continue;
1341                                         if (result_is_int)
1342                                         {
1343                                                 if (pqGetInt(result_buf, 4, conn))
1344                                                         continue;
1345                                         }
1346                                         else
1347                                         {
1348                                                 if (pqGetnchar((char *) result_buf,
1349                                                                            *actual_result_len,
1350                                                                            conn))
1351                                                         continue;
1352                                         }
1353                                         if (pqGetc(&id, conn))          /* get the last '0' */
1354                                                 continue;
1355                                 }
1356                                 if (id == '0')
1357                                 {
1358                                         /* correctly finished function result message */
1359                                         status = PGRES_COMMAND_OK;
1360                                 }
1361                                 else
1362                                 {
1363                                         /* The backend violates the protocol. */
1364                                         printfPQExpBuffer(&conn->errorMessage,
1365                                                                   libpq_gettext("protocol error: id=0x%x\n"),
1366                                                                           id);
1367                                         pqSaveErrorResult(conn);
1368                                         conn->inStart = conn->inCursor;
1369                                         return pqPrepareAsyncResult(conn);
1370                                 }
1371                                 break;
1372                         case 'E':                       /* error return */
1373                                 if (pqGetErrorNotice2(conn, true))
1374                                         continue;
1375                                 status = PGRES_FATAL_ERROR;
1376                                 break;
1377                         case 'A':                       /* notify message */
1378                                 /* handle notify and go back to processing return values */
1379                                 if (getNotify(conn))
1380                                         continue;
1381                                 break;
1382                         case 'N':                       /* notice */
1383                                 /* handle notice and go back to processing return values */
1384                                 if (pqGetErrorNotice2(conn, false))
1385                                         continue;
1386                                 break;
1387                         case 'Z':                       /* backend is ready for new query */
1388                                 /* consume the message and exit */
1389                                 conn->inStart = conn->inCursor;
1390                                 /* if we saved a result object (probably an error), use it */
1391                                 if (conn->result)
1392                                         return pqPrepareAsyncResult(conn);
1393                                 return PQmakeEmptyPGresult(conn, status);
1394                         default:
1395                                 /* The backend violates the protocol. */
1396                                 printfPQExpBuffer(&conn->errorMessage,
1397                                                                   libpq_gettext("protocol error: id=0x%x\n"),
1398                                                                   id);
1399                                 pqSaveErrorResult(conn);
1400                                 conn->inStart = conn->inCursor;
1401                                 return pqPrepareAsyncResult(conn);
1402                 }
1403                 /* Completed this message, keep going */
1404                 conn->inStart = conn->inCursor;
1405                 needInput = false;
1406         }
1407
1408         /*
1409          * We fall out of the loop only upon failing to read data.
1410          * conn->errorMessage has been set by pqWait or pqReadData. We want to
1411          * append it to any already-received error message.
1412          */
1413         pqSaveErrorResult(conn);
1414         return pqPrepareAsyncResult(conn);
1415 }
1416
1417
1418 /*
1419  * Construct startup packet
1420  *
1421  * Returns a malloc'd packet buffer, or NULL if out of memory
1422  */
1423 char *
1424 pqBuildStartupPacket2(PGconn *conn, int *packetlen,
1425                                           const PQEnvironmentOption *options)
1426 {
1427         StartupPacket *startpacket;
1428
1429         *packetlen = sizeof(StartupPacket);
1430         startpacket = (StartupPacket *) malloc(sizeof(StartupPacket));
1431         if (!startpacket)
1432                 return NULL;
1433
1434         MemSet(startpacket, 0, sizeof(StartupPacket));
1435
1436         startpacket->protoVersion = htonl(conn->pversion);
1437
1438         strncpy(startpacket->user, conn->pguser, SM_USER);
1439         strncpy(startpacket->database, conn->dbName, SM_DATABASE);
1440         strncpy(startpacket->tty, conn->pgtty, SM_TTY);
1441
1442         if (conn->pgoptions)
1443                 strncpy(startpacket->options, conn->pgoptions, SM_OPTIONS);
1444
1445         return (char *) startpacket;
1446 }