LCOV - differential code coverage report
Current view: top level - src/test/modules/libpq_pipeline - libpq_pipeline.c (source / functions) Coverage Total Hit UNC UBC GNC CBC ECB DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 76.4 % 1123 858 7 258 136 722 1 92 190
Current Date: 2026-03-14 14:10:32 -0400 Functions: 88.9 % 27 24 3 19 5 1
Baseline: lcov-20260315-024220-baseline Branches: 62.9 % 652 410 9 233 17 393 93 97
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 83.3 % 6 5 1 5
(30,360] days: 92.8 % 181 168 6 7 131 37
(360..) days: 73.2 % 936 685 251 685 1
Function coverage date bins:
(30,360] days: 100.0 % 5 5 5
(360..) days: 86.4 % 22 19 3 14 5
Branch coverage date bins:
(7,30] days: 50.0 % 2 1 1 1
(30,360] days: 65.9 % 44 29 8 7 16 13
(360..) days: 62.7 % 606 380 226 380

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * libpq_pipeline.c
                                  4                 :                :  *      Verify libpq pipeline execution functionality
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *      src/test/modules/libpq_pipeline/libpq_pipeline.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #include "postgres_fe.h"
                                 17                 :                : 
                                 18                 :                : #include <sys/select.h>
                                 19                 :                : #include <sys/time.h>
                                 20                 :                : 
                                 21                 :                : #include "catalog/pg_type_d.h"
                                 22                 :                : #include "libpq-fe.h"
                                 23                 :                : #include "pg_getopt.h"
                                 24                 :                : 
                                 25                 :                : 
                                 26                 :                : static void exit_nicely(PGconn *conn);
                                 27                 :                : pg_noreturn static void pg_fatal_impl(int line, const char *fmt,...)
                                 28                 :                :             pg_attribute_printf(2, 3);
                                 29                 :                : static bool process_result(PGconn *conn, PGresult *res, int results,
                                 30                 :                :                            int numsent);
                                 31                 :                : 
                                 32                 :                : static const char *const progname = "libpq_pipeline";
                                 33                 :                : 
                                 34                 :                : /* Options and defaults */
                                 35                 :                : static char *tracefile = NULL;  /* path to PQtrace() file */
                                 36                 :                : 
                                 37                 :                : 
                                 38                 :                : #ifdef DEBUG_OUTPUT
                                 39                 :                : #define pg_debug(...)  do { fprintf(stderr, __VA_ARGS__); } while (0)
                                 40                 :                : #else
                                 41                 :                : #define pg_debug(...)
                                 42                 :                : #endif
                                 43                 :                : 
                                 44                 :                : static const char *const drop_table_sql =
                                 45                 :                : "DROP TABLE IF EXISTS pq_pipeline_demo";
                                 46                 :                : static const char *const create_table_sql =
                                 47                 :                : "CREATE UNLOGGED TABLE pq_pipeline_demo(id serial primary key, itemno integer,"
                                 48                 :                : "int8filler int8);";
                                 49                 :                : static const char *const insert_sql =
                                 50                 :                : "INSERT INTO pq_pipeline_demo(itemno) VALUES ($1)";
                                 51                 :                : static const char *const insert_sql2 =
                                 52                 :                : "INSERT INTO pq_pipeline_demo(itemno,int8filler) VALUES ($1, $2)";
                                 53                 :                : 
                                 54                 :                : /* max char length of an int32/64, plus sign and null terminator */
                                 55                 :                : #define MAXINTLEN 12
                                 56                 :                : #define MAXINT8LEN 20
                                 57                 :                : 
                                 58                 :                : static void
 1826 alvherre@alvh.no-ip.       59                 :UBC           0 : exit_nicely(PGconn *conn)
                                 60                 :                : {
                                 61                 :              0 :     PQfinish(conn);
                                 62                 :              0 :     exit(1);
                                 63                 :                : }
                                 64                 :                : 
                                 65                 :                : /*
                                 66                 :                :  * The following few functions are wrapped in macros to make the reported line
                                 67                 :                :  * number in an error match the line number of the invocation.
                                 68                 :                :  */
                                 69                 :                : 
                                 70                 :                : /*
                                 71                 :                :  * Print an error to stderr and terminate the program.
                                 72                 :                :  */
                                 73                 :                : #define pg_fatal(...) pg_fatal_impl(__LINE__, __VA_ARGS__)
                                 74                 :                : pg_noreturn static void
                                 75                 :              0 : pg_fatal_impl(int line, const char *fmt,...)
                                 76                 :                : {
                                 77                 :                :     va_list     args;
                                 78                 :                : 
                                 79                 :              0 :     fflush(stdout);
                                 80                 :                : 
                                 81                 :              0 :     fprintf(stderr, "\n%s:%d: ", progname, line);
                                 82                 :              0 :     va_start(args, fmt);
                                 83                 :              0 :     vfprintf(stderr, fmt, args);
                                 84                 :              0 :     va_end(args);
                                 85         [ #  # ]:              0 :     Assert(fmt[strlen(fmt) - 1] != '\n');
                                 86                 :              0 :     fprintf(stderr, "\n");
                                 87                 :              0 :     exit(1);
                                 88                 :                : }
                                 89                 :                : 
                                 90                 :                : /*
                                 91                 :                :  * Check that libpq next returns a PGresult with the specified status,
                                 92                 :                :  * returning the PGresult so that caller can perform additional checks.
                                 93                 :                :  */
                                 94                 :                : #define confirm_result_status(conn, status) confirm_result_status_impl(__LINE__, conn, status)
                                 95                 :                : static PGresult *
  193 tgl@sss.pgh.pa.us          96                 :GNC          56 : confirm_result_status_impl(int line, PGconn *conn, ExecStatusType status)
                                 97                 :                : {
                                 98                 :                :     PGresult   *res;
                                 99                 :                : 
  734 alvherre@alvh.no-ip.      100                 :CBC          56 :     res = PQgetResult(conn);
                                101         [ -  + ]:             56 :     if (res == NULL)
  193 tgl@sss.pgh.pa.us         102                 :UNC           0 :         pg_fatal_impl(line, "PQgetResult returned null unexpectedly: %s",
                                103                 :                :                       PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         104         [ -  + ]:GNC          56 :     if (PQresultStatus(res) != status)
  193 tgl@sss.pgh.pa.us         105                 :UNC           0 :         pg_fatal_impl(line, "PQgetResult returned status %s, expected %s: %s",
                                106                 :                :                       PQresStatus(PQresultStatus(res)),
                                107                 :                :                       PQresStatus(status),
                                108                 :                :                       PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         109                 :GNC          56 :     return res;
                                110                 :                : }
                                111                 :                : 
                                112                 :                : /*
                                113                 :                :  * Check that libpq next returns a PGresult with the specified status,
                                114                 :                :  * then free the PGresult.
                                115                 :                :  */
                                116                 :                : #define consume_result_status(conn, status) consume_result_status_impl(__LINE__, conn, status)
                                117                 :                : static void
                                118                 :             39 : consume_result_status_impl(int line, PGconn *conn, ExecStatusType status)
                                119                 :                : {
                                120                 :                :     PGresult   *res;
                                121                 :                : 
                                122                 :             39 :     res = confirm_result_status_impl(line, conn, status);
                                123                 :             39 :     PQclear(res);
                                124                 :             39 : }
                                125                 :                : 
                                126                 :                : /*
                                127                 :                :  * Check that libpq next returns a null PGresult.
                                128                 :                :  */
                                129                 :                : #define consume_null_result(conn) consume_null_result_impl(__LINE__, conn)
                                130                 :                : static void
                                131                 :            631 : consume_null_result_impl(int line, PGconn *conn)
                                132                 :                : {
                                133                 :                :     PGresult   *res;
                                134                 :                : 
                                135                 :            631 :     res = PQgetResult(conn);
                                136         [ -  + ]:            631 :     if (res != NULL)
  193 tgl@sss.pgh.pa.us         137                 :UNC           0 :         pg_fatal_impl(line, "expected NULL PGresult, got %s: %s",
                                138                 :                :                       PQresStatus(PQresultStatus(res)),
                                139                 :                :                       PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         140                 :GNC         631 : }
                                141                 :                : 
                                142                 :                : /*
                                143                 :                :  * Check that the query on the given connection got canceled.
                                144                 :                :  */
                                145                 :                : #define consume_query_cancel(conn) consume_query_cancel_impl(__LINE__, conn)
                                146                 :                : static void
                                147                 :             12 : consume_query_cancel_impl(int line, PGconn *conn)
                                148                 :                : {
                                149                 :                :     PGresult   *res;
                                150                 :                : 
                                151                 :             12 :     res = confirm_result_status_impl(line, conn, PGRES_FATAL_ERROR);
  734 alvherre@alvh.no-ip.      152         [ -  + ]:CBC          12 :     if (strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), "57014") != 0)
  734 alvherre@alvh.no-ip.      153                 :UBC           0 :         pg_fatal_impl(line, "query failed with a different error than cancellation: %s",
                                154                 :                :                       PQerrorMessage(conn));
  734 alvherre@alvh.no-ip.      155                 :CBC          12 :     PQclear(res);
                                156                 :                : 
                                157         [ -  + ]:             12 :     while (PQisBusy(conn))
  734 alvherre@alvh.no-ip.      158                 :UBC           0 :         PQconsumeInput(conn);
  734 alvherre@alvh.no-ip.      159                 :CBC          12 : }
                                160                 :                : 
                                161                 :                : /*
                                162                 :                :  * Using monitorConn, query pg_stat_activity to see that the connection with
                                163                 :                :  * the given PID is either in the given state, or waiting on the given event
                                164                 :                :  * (only one of them can be given).
                                165                 :                :  */
                                166                 :                : static void
  727                           167                 :             24 : wait_for_connection_state(int line, PGconn *monitorConn, int procpid,
                                168                 :                :                           char *state, char *event)
                                169                 :                : {
  732                           170                 :             24 :     const Oid   paramTypes[] = {INT4OID, TEXTOID};
                                171                 :                :     const char *paramValues[2];
                                172                 :             24 :     char       *pidstr = psprintf("%d", procpid);
                                173                 :                : 
  727                           174         [ -  + ]:             24 :     Assert((state == NULL) ^ (event == NULL));
                                175                 :                : 
  732                           176                 :             24 :     paramValues[0] = pidstr;
  727                           177         [ +  + ]:             24 :     paramValues[1] = state ? state : event;
                                178                 :                : 
                                179                 :                :     while (true)
  734 alvherre@alvh.no-ip.      180                 :UBC           0 :     {
                                181                 :                :         PGresult   *res;
                                182                 :                :         char       *value;
                                183                 :                : 
  727 alvherre@alvh.no-ip.      184         [ +  + ]:CBC          24 :         if (state != NULL)
                                185                 :             12 :             res = PQexecParams(monitorConn,
                                186                 :                :                                "SELECT count(*) FROM pg_stat_activity WHERE "
                                187                 :                :                                "pid = $1 AND state = $2",
                                188                 :                :                                2, paramTypes, paramValues, NULL, NULL, 0);
                                189                 :                :         else
                                190                 :             12 :             res = PQexecParams(monitorConn,
                                191                 :                :                                "SELECT count(*) FROM pg_stat_activity WHERE "
                                192                 :                :                                "pid = $1 AND wait_event = $2",
                                193                 :                :                                2, paramTypes, paramValues, NULL, NULL, 0);
                                194                 :                : 
  734                           195         [ -  + ]:             24 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
  732 alvherre@alvh.no-ip.      196                 :UBC           0 :             pg_fatal_impl(line, "could not query pg_stat_activity: %s", PQerrorMessage(monitorConn));
  734 alvherre@alvh.no-ip.      197         [ -  + ]:CBC          24 :         if (PQntuples(res) != 1)
  732 alvherre@alvh.no-ip.      198                 :UBC           0 :             pg_fatal_impl(line, "unexpected number of rows received: %d", PQntuples(res));
  734 alvherre@alvh.no-ip.      199         [ -  + ]:CBC          24 :         if (PQnfields(res) != 1)
  732 alvherre@alvh.no-ip.      200                 :UBC           0 :             pg_fatal_impl(line, "unexpected number of columns received: %d", PQnfields(res));
  734 alvherre@alvh.no-ip.      201                 :CBC          24 :         value = PQgetvalue(res, 0, 0);
  727                           202         [ +  - ]:             24 :         if (strcmp(value, "0") != 0)
                                203                 :                :         {
  734                           204                 :             24 :             PQclear(res);
                                205                 :             24 :             break;
                                206                 :                :         }
  734 alvherre@alvh.no-ip.      207                 :UBC           0 :         PQclear(res);
                                208                 :                : 
                                209                 :                :         /* wait 10ms before polling again */
                                210                 :              0 :         pg_usleep(10000);
                                211                 :                :     }
                                212                 :                : 
  732 alvherre@alvh.no-ip.      213                 :CBC          24 :     pfree(pidstr);
                                214                 :             24 : }
                                215                 :                : 
                                216                 :                : #define send_cancellable_query(conn, monitorConn) \
                                217                 :                :     send_cancellable_query_impl(__LINE__, conn, monitorConn)
                                218                 :                : static void
                                219                 :             12 : send_cancellable_query_impl(int line, PGconn *conn, PGconn *monitorConn)
                                220                 :                : {
                                221                 :                :     const char *env_wait;
                                222                 :             12 :     const Oid   paramTypes[1] = {INT4OID};
                                223                 :                : 
                                224                 :                :     /*
                                225                 :                :      * Wait for the connection to be idle, so that our check for an active
                                226                 :                :      * connection below is reliable, instead of possibly seeing an outdated
                                227                 :                :      * state.
                                228                 :                :      */
  727                           229                 :             12 :     wait_for_connection_state(line, monitorConn, PQbackendPID(conn), "idle", NULL);
                                230                 :                : 
  732                           231                 :             12 :     env_wait = getenv("PG_TEST_TIMEOUT_DEFAULT");
                                232         [ +  - ]:             12 :     if (env_wait == NULL)
                                233                 :             12 :         env_wait = "180";
                                234                 :                : 
                                235         [ -  + ]:             12 :     if (PQsendQueryParams(conn, "SELECT pg_sleep($1)", 1, paramTypes,
                                236                 :                :                           &env_wait, NULL, NULL, 0) != 1)
  732 alvherre@alvh.no-ip.      237                 :UBC           0 :         pg_fatal_impl(line, "failed to send query: %s", PQerrorMessage(conn));
                                238                 :                : 
                                239                 :                :     /*
                                240                 :                :      * Wait for the sleep to be active, because if the query is not running
                                241                 :                :      * yet, the cancel request that we send won't have any effect.
                                242                 :                :      */
  727 alvherre@alvh.no-ip.      243                 :CBC          12 :     wait_for_connection_state(line, monitorConn, PQbackendPID(conn), NULL, "PgSleep");
  734                           244                 :             12 : }
                                245                 :                : 
                                246                 :                : /*
                                247                 :                :  * Create a new connection with the same conninfo as the given one.
                                248                 :                :  */
                                249                 :                : static PGconn *
                                250                 :              2 : copy_connection(PGconn *conn)
                                251                 :                : {
                                252                 :                :     PGconn     *copyConn;
                                253                 :              2 :     PQconninfoOption *opts = PQconninfo(conn);
                                254                 :                :     const char **keywords;
                                255                 :                :     const char **vals;
  347 heikki.linnakangas@i      256                 :              2 :     int         nopts = 0;
                                257                 :                :     int         i;
                                258                 :                : 
  734 alvherre@alvh.no-ip.      259         [ +  + ]:            104 :     for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
                                260                 :            102 :         nopts++;
  347 heikki.linnakangas@i      261                 :              2 :     nopts++;                    /* for the NULL terminator */
                                262                 :                : 
   16 michael@paquier.xyz       263                 :GNC           2 :     keywords = pg_malloc_array(const char *, nopts);
                                264                 :              2 :     vals = pg_malloc_array(const char *, nopts);
                                265                 :                : 
  347 heikki.linnakangas@i      266                 :CBC           2 :     i = 0;
  734 alvherre@alvh.no-ip.      267         [ +  + ]:            104 :     for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
                                268                 :                :     {
                                269         [ +  + ]:            102 :         if (opt->val)
                                270                 :                :         {
                                271                 :             40 :             keywords[i] = opt->keyword;
                                272                 :             40 :             vals[i] = opt->val;
                                273                 :             40 :             i++;
                                274                 :                :         }
                                275                 :                :     }
                                276                 :              2 :     keywords[i] = vals[i] = NULL;
                                277                 :                : 
                                278                 :              2 :     copyConn = PQconnectdbParams(keywords, vals, false);
                                279                 :                : 
                                280         [ -  + ]:              2 :     if (PQstatus(copyConn) != CONNECTION_OK)
  734 alvherre@alvh.no-ip.      281                 :UBC           0 :         pg_fatal("Connection to database failed: %s",
                                282                 :                :                  PQerrorMessage(copyConn));
                                283                 :                : 
  193 tgl@sss.pgh.pa.us         284                 :GNC           2 :     pfree(keywords);
                                285                 :              2 :     pfree(vals);
                                286                 :              2 :     PQconninfoFree(opts);
                                287                 :                : 
  734 alvherre@alvh.no-ip.      288                 :CBC           2 :     return copyConn;
                                289                 :                : }
                                290                 :                : 
                                291                 :                : /*
                                292                 :                :  * Test query cancellation routines
                                293                 :                :  */
                                294                 :                : static void
                                295                 :              2 : test_cancel(PGconn *conn)
                                296                 :                : {
                                297                 :                :     PGcancel   *cancel;
                                298                 :                :     PGcancelConn *cancelConn;
                                299                 :                :     PGconn     *monitorConn;
                                300                 :                :     char        errorbuf[256];
                                301                 :                : 
                                302                 :              2 :     fprintf(stderr, "test cancellations... ");
                                303                 :                : 
                                304         [ -  + ]:              2 :     if (PQsetnonblocking(conn, 1) != 0)
  734 alvherre@alvh.no-ip.      305                 :UBC           0 :         pg_fatal("failed to set nonblocking mode: %s", PQerrorMessage(conn));
                                306                 :                : 
                                307                 :                :     /*
                                308                 :                :      * Make a separate connection to the database to monitor the query on the
                                309                 :                :      * main connection.
                                310                 :                :      */
  734 alvherre@alvh.no-ip.      311                 :CBC           2 :     monitorConn = copy_connection(conn);
                                312         [ -  + ]:              2 :     Assert(PQstatus(monitorConn) == CONNECTION_OK);
                                313                 :                : 
                                314                 :                :     /* test PQcancel */
                                315                 :              2 :     send_cancellable_query(conn, monitorConn);
                                316                 :              2 :     cancel = PQgetCancel(conn);
                                317         [ -  + ]:              2 :     if (!PQcancel(cancel, errorbuf, sizeof(errorbuf)))
  734 alvherre@alvh.no-ip.      318                 :UBC           0 :         pg_fatal("failed to run PQcancel: %s", errorbuf);
  193 tgl@sss.pgh.pa.us         319                 :GNC           2 :     consume_query_cancel(conn);
                                320                 :                : 
                                321                 :                :     /* PGcancel object can be reused for the next query */
  734 alvherre@alvh.no-ip.      322                 :CBC           2 :     send_cancellable_query(conn, monitorConn);
                                323         [ -  + ]:              2 :     if (!PQcancel(cancel, errorbuf, sizeof(errorbuf)))
  734 alvherre@alvh.no-ip.      324                 :UBC           0 :         pg_fatal("failed to run PQcancel: %s", errorbuf);
  193 tgl@sss.pgh.pa.us         325                 :GNC           2 :     consume_query_cancel(conn);
                                326                 :                : 
  734 alvherre@alvh.no-ip.      327                 :CBC           2 :     PQfreeCancel(cancel);
                                328                 :                : 
                                329                 :                :     /* test PQrequestCancel */
                                330                 :              2 :     send_cancellable_query(conn, monitorConn);
                                331         [ -  + ]:              2 :     if (!PQrequestCancel(conn))
  734 alvherre@alvh.no-ip.      332                 :UBC           0 :         pg_fatal("failed to run PQrequestCancel: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         333                 :GNC           2 :     consume_query_cancel(conn);
                                334                 :                : 
                                335                 :                :     /* test PQcancelBlocking */
  733 alvherre@alvh.no-ip.      336                 :CBC           2 :     send_cancellable_query(conn, monitorConn);
                                337                 :              2 :     cancelConn = PQcancelCreate(conn);
                                338         [ -  + ]:              2 :     if (!PQcancelBlocking(cancelConn))
  733 alvherre@alvh.no-ip.      339                 :UBC           0 :         pg_fatal("failed to run PQcancelBlocking: %s", PQcancelErrorMessage(cancelConn));
  193 tgl@sss.pgh.pa.us         340                 :GNC           2 :     consume_query_cancel(conn);
  733 alvherre@alvh.no-ip.      341                 :CBC           2 :     PQcancelFinish(cancelConn);
                                342                 :                : 
                                343                 :                :     /* test PQcancelCreate and then polling with PQcancelPoll */
                                344                 :              2 :     send_cancellable_query(conn, monitorConn);
                                345                 :              2 :     cancelConn = PQcancelCreate(conn);
                                346         [ -  + ]:              2 :     if (!PQcancelStart(cancelConn))
  733 alvherre@alvh.no-ip.      347                 :UBC           0 :         pg_fatal("bad cancel connection: %s", PQcancelErrorMessage(cancelConn));
                                348                 :                :     while (true)
  733 alvherre@alvh.no-ip.      349                 :CBC           2 :     {
                                350                 :                :         struct timeval tv;
                                351                 :                :         fd_set      input_mask;
                                352                 :                :         fd_set      output_mask;
                                353                 :              4 :         PostgresPollingStatusType pollres = PQcancelPoll(cancelConn);
                                354                 :              4 :         int         sock = PQcancelSocket(cancelConn);
                                355                 :                : 
                                356         [ +  + ]:              4 :         if (pollres == PGRES_POLLING_OK)
                                357                 :              2 :             break;
                                358                 :                : 
                                359         [ +  + ]:             34 :         FD_ZERO(&input_mask);
                                360         [ +  + ]:             34 :         FD_ZERO(&output_mask);
                                361      [ +  -  - ]:              2 :         switch (pollres)
                                362                 :                :         {
                                363                 :              2 :             case PGRES_POLLING_READING:
                                364                 :                :                 pg_debug("polling for reads\n");
                                365                 :              2 :                 FD_SET(sock, &input_mask);
                                366                 :              2 :                 break;
  733 alvherre@alvh.no-ip.      367                 :UBC           0 :             case PGRES_POLLING_WRITING:
                                368                 :                :                 pg_debug("polling for writes\n");
                                369                 :              0 :                 FD_SET(sock, &output_mask);
                                370                 :              0 :                 break;
                                371                 :              0 :             default:
                                372                 :              0 :                 pg_fatal("bad cancel connection: %s", PQcancelErrorMessage(cancelConn));
                                373                 :                :         }
                                374                 :                : 
  733 alvherre@alvh.no-ip.      375         [ -  + ]:CBC           2 :         if (sock < 0)
  733 alvherre@alvh.no-ip.      376                 :UBC           0 :             pg_fatal("sock did not exist: %s", PQcancelErrorMessage(cancelConn));
                                377                 :                : 
  733 alvherre@alvh.no-ip.      378                 :CBC           2 :         tv.tv_sec = 3;
                                379                 :              2 :         tv.tv_usec = 0;
                                380                 :                : 
                                381                 :                :         while (true)
                                382                 :                :         {
                                383         [ -  + ]:              2 :             if (select(sock + 1, &input_mask, &output_mask, NULL, &tv) < 0)
                                384                 :                :             {
  733 alvherre@alvh.no-ip.      385         [ #  # ]:UBC           0 :                 if (errno == EINTR)
                                386                 :              0 :                     continue;
                                387                 :              0 :                 pg_fatal("select() failed: %m");
                                388                 :                :             }
  733 alvherre@alvh.no-ip.      389                 :CBC           2 :             break;
                                390                 :                :         }
                                391                 :                :     }
                                392         [ -  + ]:              2 :     if (PQcancelStatus(cancelConn) != CONNECTION_OK)
  733 alvherre@alvh.no-ip.      393                 :UBC           0 :         pg_fatal("unexpected cancel connection status: %s", PQcancelErrorMessage(cancelConn));
  193 tgl@sss.pgh.pa.us         394                 :GNC           2 :     consume_query_cancel(conn);
                                395                 :                : 
                                396                 :                :     /*
                                397                 :                :      * test PQcancelReset works on the cancel connection and it can be reused
                                398                 :                :      * afterwards
                                399                 :                :      */
  733 alvherre@alvh.no-ip.      400                 :CBC           2 :     PQcancelReset(cancelConn);
                                401                 :                : 
                                402                 :              2 :     send_cancellable_query(conn, monitorConn);
                                403         [ -  + ]:              2 :     if (!PQcancelStart(cancelConn))
  733 alvherre@alvh.no-ip.      404                 :UBC           0 :         pg_fatal("bad cancel connection: %s", PQcancelErrorMessage(cancelConn));
                                405                 :                :     while (true)
  733 alvherre@alvh.no-ip.      406                 :CBC           2 :     {
                                407                 :                :         struct timeval tv;
                                408                 :                :         fd_set      input_mask;
                                409                 :                :         fd_set      output_mask;
                                410                 :              4 :         PostgresPollingStatusType pollres = PQcancelPoll(cancelConn);
                                411                 :              4 :         int         sock = PQcancelSocket(cancelConn);
                                412                 :                : 
                                413         [ +  + ]:              4 :         if (pollres == PGRES_POLLING_OK)
                                414                 :              2 :             break;
                                415                 :                : 
                                416         [ +  + ]:             34 :         FD_ZERO(&input_mask);
                                417         [ +  + ]:             34 :         FD_ZERO(&output_mask);
                                418      [ +  -  - ]:              2 :         switch (pollres)
                                419                 :                :         {
                                420                 :              2 :             case PGRES_POLLING_READING:
                                421                 :                :                 pg_debug("polling for reads\n");
                                422                 :              2 :                 FD_SET(sock, &input_mask);
                                423                 :              2 :                 break;
  733 alvherre@alvh.no-ip.      424                 :UBC           0 :             case PGRES_POLLING_WRITING:
                                425                 :                :                 pg_debug("polling for writes\n");
                                426                 :              0 :                 FD_SET(sock, &output_mask);
                                427                 :              0 :                 break;
                                428                 :              0 :             default:
                                429                 :              0 :                 pg_fatal("bad cancel connection: %s", PQcancelErrorMessage(cancelConn));
                                430                 :                :         }
                                431                 :                : 
  733 alvherre@alvh.no-ip.      432         [ -  + ]:CBC           2 :         if (sock < 0)
  733 alvherre@alvh.no-ip.      433                 :UBC           0 :             pg_fatal("sock did not exist: %s", PQcancelErrorMessage(cancelConn));
                                434                 :                : 
  733 alvherre@alvh.no-ip.      435                 :CBC           2 :         tv.tv_sec = 3;
                                436                 :              2 :         tv.tv_usec = 0;
                                437                 :                : 
                                438                 :                :         while (true)
                                439                 :                :         {
                                440         [ -  + ]:              2 :             if (select(sock + 1, &input_mask, &output_mask, NULL, &tv) < 0)
                                441                 :                :             {
  733 alvherre@alvh.no-ip.      442         [ #  # ]:UBC           0 :                 if (errno == EINTR)
                                443                 :              0 :                     continue;
                                444                 :              0 :                 pg_fatal("select() failed: %m");
                                445                 :                :             }
  733 alvherre@alvh.no-ip.      446                 :CBC           2 :             break;
                                447                 :                :         }
                                448                 :                :     }
                                449         [ -  + ]:              2 :     if (PQcancelStatus(cancelConn) != CONNECTION_OK)
  733 alvherre@alvh.no-ip.      450                 :UBC           0 :         pg_fatal("unexpected cancel connection status: %s", PQcancelErrorMessage(cancelConn));
  193 tgl@sss.pgh.pa.us         451                 :GNC           2 :     consume_query_cancel(conn);
                                452                 :                : 
  733 alvherre@alvh.no-ip.      453                 :CBC           2 :     PQcancelFinish(cancelConn);
  193 tgl@sss.pgh.pa.us         454                 :GNC           2 :     PQfinish(monitorConn);
                                455                 :                : 
  734 alvherre@alvh.no-ip.      456                 :CBC           2 :     fprintf(stderr, "ok\n");
                                457                 :              2 : }
                                458                 :                : 
                                459                 :                : static void
 1826                           460                 :              1 : test_disallowed_in_pipeline(PGconn *conn)
                                461                 :                : {
                                462                 :              1 :     PGresult   *res = NULL;
                                463                 :                : 
                                464                 :              1 :     fprintf(stderr, "test error cases... ");
                                465                 :                : 
                                466         [ -  + ]:              1 :     if (PQisnonblocking(conn))
 1826 alvherre@alvh.no-ip.      467                 :UBC           0 :         pg_fatal("Expected blocking connection mode");
                                468                 :                : 
 1826 alvherre@alvh.no-ip.      469         [ -  + ]:CBC           1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      470                 :UBC           0 :         pg_fatal("Unable to enter pipeline mode");
                                471                 :                : 
 1826 alvherre@alvh.no-ip.      472         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) == PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      473                 :UBC           0 :         pg_fatal("Pipeline mode not activated properly");
                                474                 :                : 
                                475                 :                :     /* PQexec should fail in pipeline mode */
 1826 alvherre@alvh.no-ip.      476                 :CBC           1 :     res = PQexec(conn, "SELECT 1");
                                477         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_FATAL_ERROR)
 1826 alvherre@alvh.no-ip.      478                 :UBC           0 :         pg_fatal("PQexec should fail in pipeline mode but succeeded");
 1269 alvherre@alvh.no-ip.      479         [ -  + ]:CBC           1 :     if (strcmp(PQerrorMessage(conn),
                                480                 :                :                "synchronous command execution functions are not allowed in pipeline mode\n") != 0)
 1269 alvherre@alvh.no-ip.      481                 :UBC           0 :         pg_fatal("did not get expected error message; got: \"%s\"",
                                482                 :                :                  PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         483                 :GNC           1 :     PQclear(res);
                                484                 :                : 
                                485                 :                :     /* PQsendQuery should fail in pipeline mode */
 1269 alvherre@alvh.no-ip.      486         [ -  + ]:CBC           1 :     if (PQsendQuery(conn, "SELECT 1") != 0)
 1269 alvherre@alvh.no-ip.      487                 :UBC           0 :         pg_fatal("PQsendQuery should fail in pipeline mode but succeeded");
 1269 alvherre@alvh.no-ip.      488         [ -  + ]:CBC           1 :     if (strcmp(PQerrorMessage(conn),
                                489                 :                :                "PQsendQuery not allowed in pipeline mode\n") != 0)
 1269 alvherre@alvh.no-ip.      490                 :UBC           0 :         pg_fatal("did not get expected error message; got: \"%s\"",
                                491                 :                :                  PQerrorMessage(conn));
                                492                 :                : 
                                493                 :                :     /* Entering pipeline mode when already in pipeline mode is OK */
 1826 alvherre@alvh.no-ip.      494         [ -  + ]:CBC           1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      495                 :UBC           0 :         pg_fatal("re-entering pipeline mode should be a no-op but failed");
                                496                 :                : 
 1826 alvherre@alvh.no-ip.      497         [ -  + ]:CBC           1 :     if (PQisBusy(conn) != 0)
 1826 alvherre@alvh.no-ip.      498                 :UBC           0 :         pg_fatal("PQisBusy should return 0 when idle in pipeline mode, returned 1");
                                499                 :                : 
                                500                 :                :     /* ok, back to normal command mode */
 1826 alvherre@alvh.no-ip.      501         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      502                 :UBC           0 :         pg_fatal("couldn't exit idle empty pipeline mode");
                                503                 :                : 
 1826 alvherre@alvh.no-ip.      504         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) != PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      505                 :UBC           0 :         pg_fatal("Pipeline mode not terminated properly");
                                506                 :                : 
                                507                 :                :     /* exiting pipeline mode when not in pipeline mode should be a no-op */
 1826 alvherre@alvh.no-ip.      508         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      509                 :UBC           0 :         pg_fatal("pipeline mode exit when not in pipeline mode should succeed but failed");
                                510                 :                : 
                                511                 :                :     /* can now PQexec again */
 1826 alvherre@alvh.no-ip.      512                 :CBC           1 :     res = PQexec(conn, "SELECT 1");
                                513         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
 1826 alvherre@alvh.no-ip.      514                 :UBC           0 :         pg_fatal("PQexec should succeed after exiting pipeline mode but failed with: %s",
                                515                 :                :                  PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         516                 :GNC           1 :     PQclear(res);
                                517                 :                : 
 1826 alvherre@alvh.no-ip.      518                 :CBC           1 :     fprintf(stderr, "ok\n");
                                519                 :              1 : }
                                520                 :                : 
                                521                 :                : static void
                                522                 :              1 : test_multi_pipelines(PGconn *conn)
                                523                 :                : {
                                524                 :              1 :     const char *dummy_params[1] = {"1"};
                                525                 :              1 :     Oid         dummy_param_oids[1] = {INT4OID};
                                526                 :                : 
                                527                 :              1 :     fprintf(stderr, "multi pipeline... ");
                                528                 :                : 
                                529                 :                :     /*
                                530                 :                :      * Queue up a couple of small pipelines and process each without returning
                                531                 :                :      * to command mode first.
                                532                 :                :      */
                                533         [ -  + ]:              1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      534                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn));
                                535                 :                : 
                                536                 :                :     /* first pipeline */
 1826 alvherre@alvh.no-ip.      537         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids,
                                538                 :                :                           dummy_params, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      539                 :UBC           0 :         pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn));
                                540                 :                : 
 1826 alvherre@alvh.no-ip.      541         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.      542                 :UBC           0 :         pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn));
                                543                 :                : 
                                544                 :                :     /* second pipeline */
 1826 alvherre@alvh.no-ip.      545         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids,
                                546                 :                :                           dummy_params, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      547                 :UBC           0 :         pg_fatal("dispatching second SELECT failed: %s", PQerrorMessage(conn));
                                548                 :                : 
                                549                 :                :     /* Skip flushing once. */
  789 michael@paquier.xyz       550         [ -  + ]:CBC           1 :     if (PQsendPipelineSync(conn) != 1)
  789 michael@paquier.xyz       551                 :UBC           0 :         pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn));
                                552                 :                : 
                                553                 :                :     /* third pipeline */
  789 michael@paquier.xyz       554         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids,
                                555                 :                :                           dummy_params, NULL, NULL, 0) != 1)
  789 michael@paquier.xyz       556                 :UBC           0 :         pg_fatal("dispatching third SELECT failed: %s", PQerrorMessage(conn));
                                557                 :                : 
 1826 alvherre@alvh.no-ip.      558         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.      559                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                                560                 :                : 
                                561                 :                :     /* OK, start processing the results */
                                562                 :                : 
                                563                 :                :     /* first pipeline */
  193 tgl@sss.pgh.pa.us         564                 :GNC           1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                                565                 :                : 
                                566                 :              1 :     consume_null_result(conn);
                                567                 :                : 
 1826 alvherre@alvh.no-ip.      568         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 0)
 1826 alvherre@alvh.no-ip.      569                 :UBC           0 :         pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly");
                                570                 :                : 
  193 tgl@sss.pgh.pa.us         571                 :GNC           1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                                572                 :                : 
                                573                 :                :     /* second pipeline */
                                574                 :              1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                                575                 :                : 
                                576                 :              1 :     consume_null_result(conn);
                                577                 :                : 
  789 michael@paquier.xyz       578         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 0)
  789 michael@paquier.xyz       579                 :UBC           0 :         pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly");
                                580                 :                : 
  193 tgl@sss.pgh.pa.us         581                 :GNC           1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                                582                 :                : 
                                583                 :                :     /* third pipeline */
                                584                 :              1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                                585                 :                : 
                                586                 :              1 :     consume_null_result(conn);
                                587                 :                : 
                                588                 :              1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                                589                 :                : 
                                590                 :                :     /* We're still in pipeline mode ... */
 1826 alvherre@alvh.no-ip.      591         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) == PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      592                 :UBC           0 :         pg_fatal("Fell out of pipeline mode somehow");
                                593                 :                : 
                                594                 :                :     /* until we end it, which we can safely do now */
 1826 alvherre@alvh.no-ip.      595         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      596                 :UBC           0 :         pg_fatal("attempt to exit pipeline mode failed when it should've succeeded: %s",
                                597                 :                :                  PQerrorMessage(conn));
                                598                 :                : 
 1826 alvherre@alvh.no-ip.      599         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) != PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      600                 :UBC           0 :         pg_fatal("exiting pipeline mode didn't seem to work");
                                601                 :                : 
 1826 alvherre@alvh.no-ip.      602                 :CBC           1 :     fprintf(stderr, "ok\n");
                                603                 :              1 : }
                                604                 :                : 
                                605                 :                : /*
                                606                 :                :  * Test behavior when a pipeline dispatches a number of commands that are
                                607                 :                :  * not flushed by a sync point.
                                608                 :                :  */
                                609                 :                : static void
 1720                           610                 :              1 : test_nosync(PGconn *conn)
                                611                 :                : {
                                612                 :              1 :     int         numqueries = 10;
                                613                 :              1 :     int         results = 0;
                                614                 :              1 :     int         sock = PQsocket(conn);
                                615                 :                : 
                                616                 :              1 :     fprintf(stderr, "nosync... ");
                                617                 :                : 
                                618         [ -  + ]:              1 :     if (sock < 0)
 1720 alvherre@alvh.no-ip.      619                 :UBC           0 :         pg_fatal("invalid socket");
                                620                 :                : 
 1720 alvherre@alvh.no-ip.      621         [ -  + ]:CBC           1 :     if (PQenterPipelineMode(conn) != 1)
 1720 alvherre@alvh.no-ip.      622                 :UBC           0 :         pg_fatal("could not enter pipeline mode");
 1720 alvherre@alvh.no-ip.      623         [ +  + ]:CBC          11 :     for (int i = 0; i < numqueries; i++)
                                624                 :                :     {
                                625                 :                :         fd_set      input_mask;
                                626                 :                :         struct timeval tv;
                                627                 :                : 
                                628         [ -  + ]:             10 :         if (PQsendQueryParams(conn, "SELECT repeat('xyzxz', 12)",
                                629                 :                :                               0, NULL, NULL, NULL, NULL, 0) != 1)
 1720 alvherre@alvh.no-ip.      630                 :UBC           0 :             pg_fatal("error sending select: %s", PQerrorMessage(conn));
 1720 alvherre@alvh.no-ip.      631                 :CBC          10 :         PQflush(conn);
                                632                 :                : 
                                633                 :                :         /*
                                634                 :                :          * If the server has written anything to us, read (some of) it now.
                                635                 :                :          */
                                636         [ +  + ]:            170 :         FD_ZERO(&input_mask);
                                637                 :             10 :         FD_SET(sock, &input_mask);
                                638                 :             10 :         tv.tv_sec = 0;
                                639                 :             10 :         tv.tv_usec = 0;
                                640         [ -  + ]:             10 :         if (select(sock + 1, &input_mask, NULL, NULL, &tv) < 0)
                                641                 :                :         {
  733 michael@paquier.xyz       642                 :UBC           0 :             fprintf(stderr, "select() failed: %m\n");
 1720 alvherre@alvh.no-ip.      643                 :              0 :             exit_nicely(conn);
                                644                 :                :         }
 1720 alvherre@alvh.no-ip.      645   [ -  +  -  - ]:CBC          10 :         if (FD_ISSET(sock, &input_mask) && PQconsumeInput(conn) != 1)
 1720 alvherre@alvh.no-ip.      646                 :UBC           0 :             pg_fatal("failed to read from server: %s", PQerrorMessage(conn));
                                647                 :                :     }
                                648                 :                : 
                                649                 :                :     /* tell server to flush its output buffer */
 1720 alvherre@alvh.no-ip.      650         [ -  + ]:CBC           1 :     if (PQsendFlushRequest(conn) != 1)
 1720 alvherre@alvh.no-ip.      651                 :UBC           0 :         pg_fatal("failed to send flush request");
 1720 alvherre@alvh.no-ip.      652                 :CBC           1 :     PQflush(conn);
                                653                 :                : 
                                654                 :                :     /* Now read all results */
                                655                 :                :     for (;;)
 1720 alvherre@alvh.no-ip.      656                 :ECB         (9) :     {
                                657                 :                :         /* We expect exactly one TUPLES_OK result for each query we sent */
  193 tgl@sss.pgh.pa.us         658                 :GNC          10 :         consume_result_status(conn, PGRES_TUPLES_OK);
                                659                 :                : 
                                660                 :                :         /* and one NULL result should follow each */
                                661                 :             10 :         consume_null_result(conn);
                                662                 :                : 
                                663                 :             10 :         results++;
                                664                 :                : 
                                665                 :                :         /* if we're done, we're done */
                                666         [ +  + ]:             10 :         if (results == numqueries)
                                667                 :              1 :             break;
                                668                 :                :     }
                                669                 :                : 
 1720 alvherre@alvh.no-ip.      670                 :CBC           1 :     fprintf(stderr, "ok\n");
                                671                 :              1 : }
                                672                 :                : 
                                673                 :                : /*
                                674                 :                :  * When an operation in a pipeline fails the rest of the pipeline is flushed. We
                                675                 :                :  * still have to get results for each pipeline item, but the item will just be
                                676                 :                :  * a PGRES_PIPELINE_ABORTED code.
                                677                 :                :  *
                                678                 :                :  * This intentionally doesn't use a transaction to wrap the pipeline. You should
                                679                 :                :  * usually use an xact, but in this case we want to observe the effects of each
                                680                 :                :  * statement.
                                681                 :                :  */
                                682                 :                : static void
 1826                           683                 :              1 : test_pipeline_abort(PGconn *conn)
                                684                 :                : {
                                685                 :              1 :     PGresult   *res = NULL;
                                686                 :              1 :     const char *dummy_params[1] = {"1"};
                                687                 :              1 :     Oid         dummy_param_oids[1] = {INT4OID};
                                688                 :                :     int         i;
                                689                 :                :     int         gotrows;
                                690                 :                :     bool        goterror;
                                691                 :                : 
                                692                 :              1 :     fprintf(stderr, "aborted pipeline... ");
                                693                 :                : 
                                694                 :              1 :     res = PQexec(conn, drop_table_sql);
                                695         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
 1826 alvherre@alvh.no-ip.      696                 :UBC           0 :         pg_fatal("dispatching DROP TABLE failed: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         697                 :GNC           1 :     PQclear(res);
                                698                 :                : 
 1826 alvherre@alvh.no-ip.      699                 :CBC           1 :     res = PQexec(conn, create_table_sql);
                                700         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
 1826 alvherre@alvh.no-ip.      701                 :UBC           0 :         pg_fatal("dispatching CREATE TABLE failed: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us         702                 :GNC           1 :     PQclear(res);
                                703                 :                : 
                                704                 :                :     /*
                                705                 :                :      * Queue up a couple of small pipelines and process each without returning
                                706                 :                :      * to command mode first. Make sure the second operation in the first
                                707                 :                :      * pipeline ERRORs.
                                708                 :                :      */
 1826 alvherre@alvh.no-ip.      709         [ -  + ]:CBC           1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      710                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn));
                                711                 :                : 
 1826 alvherre@alvh.no-ip.      712                 :CBC           1 :     dummy_params[0] = "1";
                                713         [ -  + ]:              1 :     if (PQsendQueryParams(conn, insert_sql, 1, dummy_param_oids,
                                714                 :                :                           dummy_params, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      715                 :UBC           0 :         pg_fatal("dispatching first insert failed: %s", PQerrorMessage(conn));
                                716                 :                : 
 1826 alvherre@alvh.no-ip.      717         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT no_such_function($1)",
                                718                 :                :                           1, dummy_param_oids, dummy_params,
                                719                 :                :                           NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      720                 :UBC           0 :         pg_fatal("dispatching error select failed: %s", PQerrorMessage(conn));
                                721                 :                : 
 1826 alvherre@alvh.no-ip.      722                 :CBC           1 :     dummy_params[0] = "2";
                                723         [ -  + ]:              1 :     if (PQsendQueryParams(conn, insert_sql, 1, dummy_param_oids,
                                724                 :                :                           dummy_params, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      725                 :UBC           0 :         pg_fatal("dispatching second insert failed: %s", PQerrorMessage(conn));
                                726                 :                : 
 1826 alvherre@alvh.no-ip.      727         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.      728                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                                729                 :                : 
 1826 alvherre@alvh.no-ip.      730                 :CBC           1 :     dummy_params[0] = "3";
                                731         [ -  + ]:              1 :     if (PQsendQueryParams(conn, insert_sql, 1, dummy_param_oids,
                                732                 :                :                           dummy_params, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      733                 :UBC           0 :         pg_fatal("dispatching second-pipeline insert failed: %s",
                                734                 :                :                  PQerrorMessage(conn));
                                735                 :                : 
 1826 alvherre@alvh.no-ip.      736         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.      737                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                                738                 :                : 
                                739                 :                :     /*
                                740                 :                :      * OK, start processing the pipeline results.
                                741                 :                :      *
                                742                 :                :      * We should get a command-ok for the first query, then a fatal error and
                                743                 :                :      * a pipeline aborted message for the second insert, a pipeline-end, then
                                744                 :                :      * a command-ok and a pipeline-ok for the second pipeline operation.
                                745                 :                :      */
  193 tgl@sss.pgh.pa.us         746                 :GNC           1 :     consume_result_status(conn, PGRES_COMMAND_OK);
                                747                 :                : 
                                748                 :                :     /* NULL result to signal end-of-results for this command */
                                749                 :              1 :     consume_null_result(conn);
                                750                 :                : 
                                751                 :                :     /* Second query caused error, so we expect an error next */
                                752                 :              1 :     consume_result_status(conn, PGRES_FATAL_ERROR);
                                753                 :                : 
                                754                 :                :     /* NULL result to signal end-of-results for this command */
                                755                 :              1 :     consume_null_result(conn);
                                756                 :                : 
                                757                 :                :     /*
                                758                 :                :      * pipeline should now be aborted.
                                759                 :                :      *
                                760                 :                :      * Note that we could still queue more queries at this point if we wanted;
                                761                 :                :      * they'd get added to a new third pipeline since we've already sent a
                                762                 :                :      * second. The aborted flag relates only to the pipeline being received.
                                763                 :                :      */
 1826 alvherre@alvh.no-ip.      764         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) != PQ_PIPELINE_ABORTED)
 1826 alvherre@alvh.no-ip.      765                 :UBC           0 :         pg_fatal("pipeline should be flagged as aborted but isn't");
                                766                 :                : 
                                767                 :                :     /* third query in pipeline, the second insert */
  193 tgl@sss.pgh.pa.us         768                 :GNC           1 :     consume_result_status(conn, PGRES_PIPELINE_ABORTED);
                                769                 :                : 
                                770                 :                :     /* NULL result to signal end-of-results for this command */
                                771                 :              1 :     consume_null_result(conn);
                                772                 :                : 
 1826 alvherre@alvh.no-ip.      773         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) != PQ_PIPELINE_ABORTED)
 1826 alvherre@alvh.no-ip.      774                 :UBC           0 :         pg_fatal("pipeline should be flagged as aborted but isn't");
                                775                 :                : 
                                776                 :                :     /* Ensure we're still in pipeline */
 1826 alvherre@alvh.no-ip.      777         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) == PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      778                 :UBC           0 :         pg_fatal("Fell out of pipeline mode somehow");
                                779                 :                : 
                                780                 :                :     /*
                                781                 :                :      * The end of a failed pipeline is a PGRES_PIPELINE_SYNC.
                                782                 :                :      *
                                783                 :                :      * (This is so clients know to start processing results normally again and
                                784                 :                :      * can tell the difference between skipped commands and the sync.)
                                785                 :                :      */
  193 tgl@sss.pgh.pa.us         786                 :GNC           1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                                787                 :                : 
 1826 alvherre@alvh.no-ip.      788         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) == PQ_PIPELINE_ABORTED)
 1826 alvherre@alvh.no-ip.      789                 :UBC           0 :         pg_fatal("sync should've cleared the aborted flag but didn't");
                                790                 :                : 
                                791                 :                :     /* We're still in pipeline mode... */
 1826 alvherre@alvh.no-ip.      792         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) == PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      793                 :UBC           0 :         pg_fatal("Fell out of pipeline mode somehow");
                                794                 :                : 
                                795                 :                :     /* the insert from the second pipeline */
  193 tgl@sss.pgh.pa.us         796                 :GNC           1 :     consume_result_status(conn, PGRES_COMMAND_OK);
                                797                 :                : 
                                798                 :                :     /* Read the NULL result at the end of the command */
                                799                 :              1 :     consume_null_result(conn);
                                800                 :                : 
                                801                 :                :     /* the second pipeline sync */
                                802                 :              1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                                803                 :                : 
                                804                 :                :     /* Read the NULL result at the end of the command */
                                805                 :              1 :     consume_null_result(conn);
                                806                 :                : 
                                807                 :                :     /* Try to send two queries in one command */
 1269 alvherre@alvh.no-ip.      808         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT 1; SELECT 2", 0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      809                 :UBC           0 :         pg_fatal("failed to send query: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.      810         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.      811                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.      812                 :CBC           1 :     goterror = false;
                                813         [ +  + ]:              2 :     while ((res = PQgetResult(conn)) != NULL)
                                814                 :                :     {
                                815         [ +  - ]:              1 :         switch (PQresultStatus(res))
                                816                 :                :         {
                                817                 :              1 :             case PGRES_FATAL_ERROR:
                                818         [ -  + ]:              1 :                 if (strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), "42601") != 0)
 1826 alvherre@alvh.no-ip.      819                 :UBC           0 :                     pg_fatal("expected error about multiple commands, got %s",
                                820                 :                :                              PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.      821                 :CBC           1 :                 printf("got expected %s", PQerrorMessage(conn));
                                822                 :              1 :                 goterror = true;
                                823                 :              1 :                 break;
 1826 alvherre@alvh.no-ip.      824                 :UBC           0 :             default:
                                825                 :              0 :                 pg_fatal("got unexpected status %s", PQresStatus(PQresultStatus(res)));
                                826                 :                :                 break;
                                827                 :                :         }
  193 tgl@sss.pgh.pa.us         828                 :GNC           1 :         PQclear(res);
                                829                 :                :     }
 1826 alvherre@alvh.no-ip.      830         [ -  + ]:CBC           1 :     if (!goterror)
 1826 alvherre@alvh.no-ip.      831                 :UBC           0 :         pg_fatal("did not get cannot-insert-multiple-commands error");
                                832                 :                : 
                                833                 :                :     /* the second pipeline sync */
  193 tgl@sss.pgh.pa.us         834                 :GNC           1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                                835                 :                : 
 1810 alvherre@alvh.no-ip.      836                 :CBC           1 :     fprintf(stderr, "ok\n");
                                837                 :                : 
                                838                 :                :     /* Test single-row mode with an error partways */
 1269                           839         [ -  + ]:              1 :     if (PQsendQueryParams(conn, "SELECT 1.0/g FROM generate_series(3, -1, -1) g",
                                840                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      841                 :UBC           0 :         pg_fatal("failed to send query: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.      842         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.      843                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.      844                 :CBC           1 :     PQsetSingleRowMode(conn);
                                845                 :              1 :     goterror = false;
 1810                           846                 :              1 :     gotrows = 0;
 1826                           847         [ +  + ]:              5 :     while ((res = PQgetResult(conn)) != NULL)
                                848                 :                :     {
                                849      [ +  +  - ]:              4 :         switch (PQresultStatus(res))
                                850                 :                :         {
                                851                 :              3 :             case PGRES_SINGLE_TUPLE:
                                852                 :              3 :                 printf("got row: %s\n", PQgetvalue(res, 0, 0));
 1810                           853                 :              3 :                 gotrows++;
 1826                           854                 :              3 :                 break;
                                855                 :              1 :             case PGRES_FATAL_ERROR:
                                856         [ -  + ]:              1 :                 if (strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), "22012") != 0)
 1826 alvherre@alvh.no-ip.      857                 :UBC           0 :                     pg_fatal("expected division-by-zero, got: %s (%s)",
                                858                 :                :                              PQerrorMessage(conn),
                                859                 :                :                              PQresultErrorField(res, PG_DIAG_SQLSTATE));
 1826 alvherre@alvh.no-ip.      860                 :CBC           1 :                 printf("got expected division-by-zero\n");
                                861                 :              1 :                 goterror = true;
                                862                 :              1 :                 break;
 1826 alvherre@alvh.no-ip.      863                 :UBC           0 :             default:
                                864                 :              0 :                 pg_fatal("got unexpected result %s", PQresStatus(PQresultStatus(res)));
                                865                 :                :         }
 1826 alvherre@alvh.no-ip.      866                 :CBC           4 :         PQclear(res);
                                867                 :                :     }
                                868         [ -  + ]:              1 :     if (!goterror)
 1826 alvherre@alvh.no-ip.      869                 :UBC           0 :         pg_fatal("did not get division-by-zero error");
 1810 alvherre@alvh.no-ip.      870         [ -  + ]:CBC           1 :     if (gotrows != 3)
 1810 alvherre@alvh.no-ip.      871                 :UBC           0 :         pg_fatal("did not get three rows");
                                872                 :                : 
                                873                 :                :     /* the third pipeline sync */
  193 tgl@sss.pgh.pa.us         874                 :GNC           1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                                875                 :                : 
                                876                 :                :     /* We're still in pipeline mode... */
 1826 alvherre@alvh.no-ip.      877         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) == PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      878                 :UBC           0 :         pg_fatal("Fell out of pipeline mode somehow");
                                879                 :                : 
                                880                 :                :     /* until we end it, which we can safely do now */
 1826 alvherre@alvh.no-ip.      881         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      882                 :UBC           0 :         pg_fatal("attempt to exit pipeline mode failed when it should've succeeded: %s",
                                883                 :                :                  PQerrorMessage(conn));
                                884                 :                : 
 1826 alvherre@alvh.no-ip.      885         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) != PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.      886                 :UBC           0 :         pg_fatal("exiting pipeline mode didn't seem to work");
                                887                 :                : 
                                888                 :                :     /*-
                                889                 :                :      * Since we fired the pipelines off without a surrounding xact, the results
                                890                 :                :      * should be:
                                891                 :                :      *
                                892                 :                :      * - Implicit xact started by server around 1st pipeline
                                893                 :                :      * - First insert applied
                                894                 :                :      * - Second statement aborted xact
                                895                 :                :      * - Third insert skipped
                                896                 :                :      * - Sync rolled back first implicit xact
                                897                 :                :      * - Implicit xact created by server around 2nd pipeline
                                898                 :                :      * - insert applied from 2nd pipeline
                                899                 :                :      * - Sync commits 2nd xact
                                900                 :                :      *
                                901                 :                :      * So we should only have the value 3 that we inserted.
                                902                 :                :      */
 1826 alvherre@alvh.no-ip.      903                 :CBC           1 :     res = PQexec(conn, "SELECT itemno FROM pq_pipeline_demo");
                                904                 :                : 
                                905         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
 1826 alvherre@alvh.no-ip.      906                 :UBC           0 :         pg_fatal("Expected tuples, got %s: %s",
                                907                 :                :                  PQresStatus(PQresultStatus(res)), PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.      908         [ -  + ]:CBC           1 :     if (PQntuples(res) != 1)
 1826 alvherre@alvh.no-ip.      909                 :UBC           0 :         pg_fatal("expected 1 result, got %d", PQntuples(res));
 1826 alvherre@alvh.no-ip.      910         [ +  + ]:CBC           2 :     for (i = 0; i < PQntuples(res); i++)
                                911                 :                :     {
                                912                 :              1 :         const char *val = PQgetvalue(res, i, 0);
                                913                 :                : 
                                914         [ -  + ]:              1 :         if (strcmp(val, "3") != 0)
 1826 alvherre@alvh.no-ip.      915                 :UBC           0 :             pg_fatal("expected only insert with value 3, got %s", val);
                                916                 :                :     }
                                917                 :                : 
 1826 alvherre@alvh.no-ip.      918                 :CBC           1 :     PQclear(res);
                                919                 :                : 
 1349                           920                 :              1 :     fprintf(stderr, "ok\n");
 1826                           921                 :              1 : }
                                922                 :                : 
                                923                 :                : /* State machine enum for test_pipelined_insert */
                                924                 :                : enum PipelineInsertStep
                                925                 :                : {
                                926                 :                :     BI_BEGIN_TX,
                                927                 :                :     BI_DROP_TABLE,
                                928                 :                :     BI_CREATE_TABLE,
                                929                 :                :     BI_PREPARE,
                                930                 :                :     BI_INSERT_ROWS,
                                931                 :                :     BI_COMMIT_TX,
                                932                 :                :     BI_SYNC,
                                933                 :                :     BI_DONE,
                                934                 :                : };
                                935                 :                : 
                                936                 :                : static void
                                937                 :              1 : test_pipelined_insert(PGconn *conn, int n_rows)
                                938                 :                : {
 1810                           939                 :              1 :     Oid         insert_param_oids[2] = {INT4OID, INT8OID};
                                940                 :                :     const char *insert_params[2];
                                941                 :                :     char        insert_param_0[MAXINTLEN];
                                942                 :                :     char        insert_param_1[MAXINT8LEN];
 1826                           943                 :              1 :     enum PipelineInsertStep send_step = BI_BEGIN_TX,
                                944                 :              1 :                 recv_step = BI_BEGIN_TX;
                                945                 :                :     int         rows_to_send,
                                946                 :                :                 rows_to_receive;
                                947                 :                : 
 1810                           948                 :              1 :     insert_params[0] = insert_param_0;
                                949                 :              1 :     insert_params[1] = insert_param_1;
                                950                 :                : 
 1826                           951                 :              1 :     rows_to_send = rows_to_receive = n_rows;
                                952                 :                : 
                                953                 :                :     /*
                                954                 :                :      * Do a pipelined insert into a table created at the start of the pipeline
                                955                 :                :      */
                                956         [ -  + ]:              1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.      957                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn));
                                958                 :                : 
 1826 alvherre@alvh.no-ip.      959         [ +  + ]:CBC           4 :     while (send_step != BI_PREPARE)
                                960                 :                :     {
                                961                 :                :         const char *sql;
                                962                 :                : 
                                963   [ +  +  +  - ]:              3 :         switch (send_step)
                                964                 :                :         {
                                965                 :              1 :             case BI_BEGIN_TX:
                                966                 :              1 :                 sql = "BEGIN TRANSACTION";
                                967                 :              1 :                 send_step = BI_DROP_TABLE;
                                968                 :              1 :                 break;
                                969                 :                : 
                                970                 :              1 :             case BI_DROP_TABLE:
                                971                 :              1 :                 sql = drop_table_sql;
                                972                 :              1 :                 send_step = BI_CREATE_TABLE;
                                973                 :              1 :                 break;
                                974                 :                : 
                                975                 :              1 :             case BI_CREATE_TABLE:
                                976                 :              1 :                 sql = create_table_sql;
                                977                 :              1 :                 send_step = BI_PREPARE;
                                978                 :              1 :                 break;
                                979                 :                : 
 1826 alvherre@alvh.no-ip.      980                 :UBC           0 :             default:
                                981                 :              0 :                 pg_fatal("invalid state");
                                982                 :                :                 sql = NULL;     /* keep compiler quiet */
                                983                 :                :         }
                                984                 :                : 
                                985                 :                :         pg_debug("sending: %s\n", sql);
 1826 alvherre@alvh.no-ip.      986         [ -  + ]:CBC           3 :         if (PQsendQueryParams(conn, sql,
                                987                 :                :                               0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.      988                 :UBC           0 :             pg_fatal("dispatching %s failed: %s", sql, PQerrorMessage(conn));
                                989                 :                :     }
                                990                 :                : 
 1826 alvherre@alvh.no-ip.      991         [ -  + ]:CBC           1 :     Assert(send_step == BI_PREPARE);
                                992                 :                :     pg_debug("sending: %s\n", insert_sql2);
 1810                           993         [ -  + ]:              1 :     if (PQsendPrepare(conn, "my_insert", insert_sql2, 2, insert_param_oids) != 1)
 1826 alvherre@alvh.no-ip.      994                 :UBC           0 :         pg_fatal("dispatching PREPARE failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.      995                 :CBC           1 :     send_step = BI_INSERT_ROWS;
                                996                 :                : 
                                997                 :                :     /*
                                998                 :                :      * Now we start inserting. We'll be sending enough data that we could fill
                                999                 :                :      * our output buffer, so to avoid deadlocking we need to enter nonblocking
                               1000                 :                :      * mode and consume input while we send more output. As results of each
                               1001                 :                :      * query are processed we should pop them to allow processing of the next
                               1002                 :                :      * query. There's no need to finish the pipeline before processing
                               1003                 :                :      * results.
                               1004                 :                :      */
                               1005         [ -  + ]:              1 :     if (PQsetnonblocking(conn, 1) != 0)
 1826 alvherre@alvh.no-ip.     1006                 :UBC           0 :         pg_fatal("failed to set nonblocking mode: %s", PQerrorMessage(conn));
                               1007                 :                : 
 1826 alvherre@alvh.no-ip.     1008         [ +  + ]:CBC        3539 :     while (recv_step != BI_DONE)
                               1009                 :                :     {
                               1010                 :                :         int         sock;
                               1011                 :                :         fd_set      input_mask;
                               1012                 :                :         fd_set      output_mask;
                               1013                 :                : 
                               1014                 :           3538 :         sock = PQsocket(conn);
                               1015                 :                : 
                               1016         [ -  + ]:           3538 :         if (sock < 0)
 1826 alvherre@alvh.no-ip.     1017                 :UBC           0 :             break;              /* shouldn't happen */
                               1018                 :                : 
 1826 alvherre@alvh.no-ip.     1019         [ +  + ]:CBC       60146 :         FD_ZERO(&input_mask);
                               1020                 :           3538 :         FD_SET(sock, &input_mask);
                               1021         [ +  + ]:          60146 :         FD_ZERO(&output_mask);
                               1022                 :           3538 :         FD_SET(sock, &output_mask);
                               1023                 :                : 
                               1024         [ -  + ]:           3538 :         if (select(sock + 1, &input_mask, &output_mask, NULL, NULL) < 0)
                               1025                 :                :         {
  733 michael@paquier.xyz      1026                 :UBC           0 :             fprintf(stderr, "select() failed: %m\n");
 1826 alvherre@alvh.no-ip.     1027                 :              0 :             exit_nicely(conn);
                               1028                 :                :         }
                               1029                 :                : 
                               1030                 :                :         /*
                               1031                 :                :          * Process any results, so we keep the server's output buffer free
                               1032                 :                :          * flowing and it can continue to process input
                               1033                 :                :          */
 1826 alvherre@alvh.no-ip.     1034         [ +  + ]:CBC        3538 :         if (FD_ISSET(sock, &input_mask))
                               1035                 :                :         {
                               1036                 :              3 :             PQconsumeInput(conn);
                               1037                 :                : 
                               1038                 :                :             /* Read until we'd block if we tried to read */
                               1039   [ +  +  +  + ]:           1414 :             while (!PQisBusy(conn) && recv_step < BI_DONE)
                               1040                 :                :             {
                               1041                 :                :                 PGresult   *res;
 1810 tgl@sss.pgh.pa.us        1042                 :           1411 :                 const char *cmdtag = "";
 1826 alvherre@alvh.no-ip.     1043                 :           1411 :                 const char *description = "";
                               1044                 :                :                 int         status;
                               1045                 :                : 
                               1046                 :                :                 /*
                               1047                 :                :                  * Read next result.  If no more results from this query,
                               1048                 :                :                  * advance to the next query
                               1049                 :                :                  */
                               1050                 :           1411 :                 res = PQgetResult(conn);
                               1051         [ +  + ]:           1411 :                 if (res == NULL)
                               1052                 :            705 :                     continue;
                               1053                 :                : 
                               1054                 :            706 :                 status = PGRES_COMMAND_OK;
                               1055   [ +  +  +  +  :            706 :                 switch (recv_step)
                                        +  +  +  -  
                                                 - ]
                               1056                 :                :                 {
                               1057                 :              1 :                     case BI_BEGIN_TX:
                               1058                 :              1 :                         cmdtag = "BEGIN";
                               1059                 :              1 :                         recv_step++;
                               1060                 :              1 :                         break;
                               1061                 :              1 :                     case BI_DROP_TABLE:
                               1062                 :              1 :                         cmdtag = "DROP TABLE";
                               1063                 :              1 :                         recv_step++;
                               1064                 :              1 :                         break;
                               1065                 :              1 :                     case BI_CREATE_TABLE:
                               1066                 :              1 :                         cmdtag = "CREATE TABLE";
                               1067                 :              1 :                         recv_step++;
                               1068                 :              1 :                         break;
                               1069                 :              1 :                     case BI_PREPARE:
                               1070                 :              1 :                         cmdtag = "";
                               1071                 :              1 :                         description = "PREPARE";
                               1072                 :              1 :                         recv_step++;
                               1073                 :              1 :                         break;
                               1074                 :            700 :                     case BI_INSERT_ROWS:
                               1075                 :            700 :                         cmdtag = "INSERT";
                               1076                 :            700 :                         rows_to_receive--;
                               1077         [ +  + ]:            700 :                         if (rows_to_receive == 0)
                               1078                 :              1 :                             recv_step++;
                               1079                 :            700 :                         break;
                               1080                 :              1 :                     case BI_COMMIT_TX:
                               1081                 :              1 :                         cmdtag = "COMMIT";
                               1082                 :              1 :                         recv_step++;
                               1083                 :              1 :                         break;
                               1084                 :              1 :                     case BI_SYNC:
                               1085                 :              1 :                         cmdtag = "";
                               1086                 :              1 :                         description = "SYNC";
                               1087                 :              1 :                         status = PGRES_PIPELINE_SYNC;
                               1088                 :              1 :                         recv_step++;
                               1089                 :              1 :                         break;
 1826 alvherre@alvh.no-ip.     1090                 :UBC           0 :                     case BI_DONE:
                               1091                 :                :                         /* unreachable */
 1820 tgl@sss.pgh.pa.us        1092                 :              0 :                         pg_fatal("unreachable state");
                               1093                 :                :                 }
                               1094                 :                : 
 1826 alvherre@alvh.no-ip.     1095         [ -  + ]:CBC         706 :                 if (PQresultStatus(res) != status)
 1826 alvherre@alvh.no-ip.     1096                 :UBC           0 :                     pg_fatal("%s reported status %s, expected %s\n"
                               1097                 :                :                              "Error message: \"%s\"",
                               1098                 :                :                              description, PQresStatus(PQresultStatus(res)),
                               1099                 :                :                              PQresStatus(status), PQerrorMessage(conn));
                               1100                 :                : 
 1826 alvherre@alvh.no-ip.     1101         [ -  + ]:CBC         706 :                 if (strncmp(PQcmdStatus(res), cmdtag, strlen(cmdtag)) != 0)
 1826 alvherre@alvh.no-ip.     1102                 :UBC           0 :                     pg_fatal("%s expected command tag '%s', got '%s'",
                               1103                 :                :                              description, cmdtag, PQcmdStatus(res));
                               1104                 :                : 
                               1105                 :                :                 pg_debug("Got %s OK\n", cmdtag[0] != '\0' ? cmdtag : description);
                               1106                 :                : 
 1826 alvherre@alvh.no-ip.     1107                 :CBC         706 :                 PQclear(res);
                               1108                 :                :             }
                               1109                 :                :         }
                               1110                 :                : 
                               1111                 :                :         /* Write more rows and/or the end pipeline message, if needed */
                               1112         [ +  + ]:           3538 :         if (FD_ISSET(sock, &output_mask))
                               1113                 :                :         {
                               1114                 :           3536 :             PQflush(conn);
                               1115                 :                : 
                               1116         [ +  + ]:           3536 :             if (send_step == BI_INSERT_ROWS)
                               1117                 :                :             {
 1810                          1118                 :            700 :                 snprintf(insert_param_0, MAXINTLEN, "%d", rows_to_send);
                               1119                 :                :                 /* use up some buffer space with a wide value */
 1809                          1120                 :            700 :                 snprintf(insert_param_1, MAXINT8LEN, "%lld", 1LL << 62);
                               1121                 :                : 
 1826                          1122         [ +  - ]:            700 :                 if (PQsendQueryPrepared(conn, "my_insert",
                               1123                 :                :                                         2, insert_params, NULL, NULL, 0) == 1)
                               1124                 :                :                 {
                               1125                 :                :                     pg_debug("sent row %d\n", rows_to_send);
                               1126                 :                : 
                               1127                 :            700 :                     rows_to_send--;
                               1128         [ +  + ]:            700 :                     if (rows_to_send == 0)
                               1129                 :              1 :                         send_step++;
                               1130                 :                :                 }
                               1131                 :                :                 else
                               1132                 :                :                 {
                               1133                 :                :                     /*
                               1134                 :                :                      * in nonblocking mode, so it's OK for an insert to fail
                               1135                 :                :                      * to send
                               1136                 :                :                      */
 1826 alvherre@alvh.no-ip.     1137                 :UBC           0 :                     fprintf(stderr, "WARNING: failed to send insert #%d: %s\n",
                               1138                 :                :                             rows_to_send, PQerrorMessage(conn));
                               1139                 :                :                 }
                               1140                 :                :             }
 1826 alvherre@alvh.no-ip.     1141         [ +  + ]:CBC        2836 :             else if (send_step == BI_COMMIT_TX)
                               1142                 :                :             {
                               1143         [ +  - ]:              1 :                 if (PQsendQueryParams(conn, "COMMIT",
                               1144                 :                :                                       0, NULL, NULL, NULL, NULL, 0) == 1)
                               1145                 :                :                 {
                               1146                 :                :                     pg_debug("sent COMMIT\n");
                               1147                 :              1 :                     send_step++;
                               1148                 :                :                 }
                               1149                 :                :                 else
                               1150                 :                :                 {
 1826 alvherre@alvh.no-ip.     1151                 :UBC           0 :                     fprintf(stderr, "WARNING: failed to send commit: %s\n",
                               1152                 :                :                             PQerrorMessage(conn));
                               1153                 :                :                 }
                               1154                 :                :             }
 1826 alvherre@alvh.no-ip.     1155         [ +  + ]:CBC        2835 :             else if (send_step == BI_SYNC)
                               1156                 :                :             {
                               1157         [ +  - ]:              1 :                 if (PQpipelineSync(conn) == 1)
                               1158                 :                :                 {
                               1159                 :              1 :                     fprintf(stdout, "pipeline sync sent\n");
                               1160                 :              1 :                     send_step++;
                               1161                 :                :                 }
                               1162                 :                :                 else
                               1163                 :                :                 {
 1826 alvherre@alvh.no-ip.     1164                 :UBC           0 :                     fprintf(stderr, "WARNING: pipeline sync failed: %s\n",
                               1165                 :                :                             PQerrorMessage(conn));
                               1166                 :                :                 }
                               1167                 :                :             }
                               1168                 :                :         }
                               1169                 :                :     }
                               1170                 :                : 
                               1171                 :                :     /* We've got the sync message and the pipeline should be done */
 1826 alvherre@alvh.no-ip.     1172         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1173                 :UBC           0 :         pg_fatal("attempt to exit pipeline mode failed when it should've succeeded: %s",
                               1174                 :                :                  PQerrorMessage(conn));
                               1175                 :                : 
 1826 alvherre@alvh.no-ip.     1176         [ -  + ]:CBC           1 :     if (PQsetnonblocking(conn, 0) != 0)
 1826 alvherre@alvh.no-ip.     1177                 :UBC           0 :         pg_fatal("failed to clear nonblocking mode: %s", PQerrorMessage(conn));
                               1178                 :                : 
 1826 alvherre@alvh.no-ip.     1179                 :CBC           1 :     fprintf(stderr, "ok\n");
                               1180                 :              1 : }
                               1181                 :                : 
                               1182                 :                : static void
                               1183                 :              1 : test_prepared(PGconn *conn)
                               1184                 :                : {
                               1185                 :              1 :     PGresult   *res = NULL;
                               1186                 :              1 :     Oid         param_oids[1] = {INT4OID};
                               1187                 :                :     Oid         expected_oids[4];
                               1188                 :                :     Oid         typ;
                               1189                 :                : 
                               1190                 :              1 :     fprintf(stderr, "prepared... ");
                               1191                 :                : 
                               1192         [ -  + ]:              1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1193                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1194         [ -  + ]:CBC           1 :     if (PQsendPrepare(conn, "select_one", "SELECT $1, '42', $1::numeric, "
                               1195                 :                :                       "interval '1 sec'",
                               1196                 :                :                       1, param_oids) != 1)
 1826 alvherre@alvh.no-ip.     1197                 :UBC           0 :         pg_fatal("preparing query failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1198                 :CBC           1 :     expected_oids[0] = INT4OID;
                               1199                 :              1 :     expected_oids[1] = TEXTOID;
                               1200                 :              1 :     expected_oids[2] = NUMERICOID;
                               1201                 :              1 :     expected_oids[3] = INTERVALOID;
                               1202         [ -  + ]:              1 :     if (PQsendDescribePrepared(conn, "select_one") != 1)
 1826 alvherre@alvh.no-ip.     1203                 :UBC           0 :         pg_fatal("failed to send describePrepared: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1204         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1205                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                               1206                 :                : 
  193 tgl@sss.pgh.pa.us        1207                 :GNC           1 :     consume_result_status(conn, PGRES_COMMAND_OK);
                               1208                 :                : 
                               1209                 :              1 :     consume_null_result(conn);
                               1210                 :                : 
                               1211                 :              1 :     res = confirm_result_status(conn, PGRES_COMMAND_OK);
 1826 alvherre@alvh.no-ip.     1212         [ -  + ]:CBC           1 :     if (PQnfields(res) != lengthof(expected_oids))
  894 peter@eisentraut.org     1213                 :UBC           0 :         pg_fatal("expected %zu columns, got %d",
                               1214                 :                :                  lengthof(expected_oids), PQnfields(res));
 1826 alvherre@alvh.no-ip.     1215         [ +  + ]:CBC           5 :     for (int i = 0; i < PQnfields(res); i++)
                               1216                 :                :     {
                               1217                 :              4 :         typ = PQftype(res, i);
                               1218         [ -  + ]:              4 :         if (typ != expected_oids[i])
 1826 alvherre@alvh.no-ip.     1219                 :UBC           0 :             pg_fatal("field %d: expected type %u, got %u",
                               1220                 :                :                      i, expected_oids[i], typ);
                               1221                 :                :     }
 1826 alvherre@alvh.no-ip.     1222                 :CBC           1 :     PQclear(res);
                               1223                 :                : 
  193 tgl@sss.pgh.pa.us        1224                 :GNC           1 :     consume_null_result(conn);
                               1225                 :                : 
                               1226                 :              1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                               1227                 :                : 
  985 michael@paquier.xyz      1228                 :CBC           1 :     fprintf(stderr, "closing statement..");
                               1229         [ -  + ]:              1 :     if (PQsendClosePrepared(conn, "select_one") != 1)
  985 michael@paquier.xyz      1230                 :UBC           0 :         pg_fatal("PQsendClosePrepared failed: %s", PQerrorMessage(conn));
  985 michael@paquier.xyz      1231         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
  985 michael@paquier.xyz      1232                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                               1233                 :                : 
  193 tgl@sss.pgh.pa.us        1234                 :GNC           1 :     consume_result_status(conn, PGRES_COMMAND_OK);
                               1235                 :                : 
                               1236                 :              1 :     consume_null_result(conn);
                               1237                 :                : 
                               1238                 :              1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                               1239                 :                : 
 1826 alvherre@alvh.no-ip.     1240         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1241                 :UBC           0 :         pg_fatal("could not exit pipeline mode: %s", PQerrorMessage(conn));
                               1242                 :                : 
                               1243                 :                :     /* Now that it's closed we should get an error when describing */
  985 michael@paquier.xyz      1244                 :CBC           1 :     res = PQdescribePrepared(conn, "select_one");
                               1245         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_FATAL_ERROR)
  985 michael@paquier.xyz      1246                 :UBC           0 :         pg_fatal("expected FATAL_ERROR, got %s", PQresStatus(PQresultStatus(res)));
  193 tgl@sss.pgh.pa.us        1247                 :GNC           1 :     PQclear(res);
                               1248                 :                : 
                               1249                 :                :     /*
                               1250                 :                :      * Also test the blocking close, this should not fail since closing a
                               1251                 :                :      * non-existent prepared statement is a no-op
                               1252                 :                :      */
  985 michael@paquier.xyz      1253                 :CBC           1 :     res = PQclosePrepared(conn, "select_one");
                               1254         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  985 michael@paquier.xyz      1255                 :UBC           0 :         pg_fatal("expected COMMAND_OK, got %s", PQresStatus(PQresultStatus(res)));
  193 tgl@sss.pgh.pa.us        1256                 :GNC           1 :     PQclear(res);
                               1257                 :                : 
  985 michael@paquier.xyz      1258                 :CBC           1 :     fprintf(stderr, "creating portal... ");
                               1259                 :                : 
  193 tgl@sss.pgh.pa.us        1260                 :GNC           1 :     res = PQexec(conn, "BEGIN");
                               1261         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  193 tgl@sss.pgh.pa.us        1262                 :UNC           0 :         pg_fatal("BEGIN failed: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us        1263                 :GNC           1 :     PQclear(res);
                               1264                 :                : 
                               1265                 :              1 :     res = PQexec(conn, "DECLARE cursor_one CURSOR FOR SELECT 1");
                               1266         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  193 tgl@sss.pgh.pa.us        1267                 :UNC           0 :         pg_fatal("DECLARE CURSOR failed: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us        1268                 :GNC           1 :     PQclear(res);
                               1269                 :                : 
 1826 alvherre@alvh.no-ip.     1270                 :CBC           1 :     PQenterPipelineMode(conn);
                               1271         [ -  + ]:              1 :     if (PQsendDescribePortal(conn, "cursor_one") != 1)
 1826 alvherre@alvh.no-ip.     1272                 :UBC           0 :         pg_fatal("PQsendDescribePortal failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1273         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1274                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                               1275                 :                : 
  193 tgl@sss.pgh.pa.us        1276                 :GNC           1 :     res = confirm_result_status(conn, PGRES_COMMAND_OK);
 1826 alvherre@alvh.no-ip.     1277                 :CBC           1 :     typ = PQftype(res, 0);
                               1278         [ -  + ]:              1 :     if (typ != INT4OID)
 1826 alvherre@alvh.no-ip.     1279                 :UBC           0 :         pg_fatal("portal: expected type %u, got %u",
                               1280                 :                :                  INT4OID, typ);
 1826 alvherre@alvh.no-ip.     1281                 :CBC           1 :     PQclear(res);
                               1282                 :                : 
  193 tgl@sss.pgh.pa.us        1283                 :GNC           1 :     consume_null_result(conn);
                               1284                 :                : 
                               1285                 :              1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                               1286                 :                : 
  985 michael@paquier.xyz      1287                 :CBC           1 :     fprintf(stderr, "closing portal... ");
                               1288         [ -  + ]:              1 :     if (PQsendClosePortal(conn, "cursor_one") != 1)
  985 michael@paquier.xyz      1289                 :UBC           0 :         pg_fatal("PQsendClosePortal failed: %s", PQerrorMessage(conn));
  985 michael@paquier.xyz      1290         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
  985 michael@paquier.xyz      1291                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                               1292                 :                : 
  193 tgl@sss.pgh.pa.us        1293                 :GNC           1 :     consume_result_status(conn, PGRES_COMMAND_OK);
                               1294                 :                : 
                               1295                 :              1 :     consume_null_result(conn);
                               1296                 :                : 
                               1297                 :              1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                               1298                 :                : 
 1826 alvherre@alvh.no-ip.     1299         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1300                 :UBC           0 :         pg_fatal("could not exit pipeline mode: %s", PQerrorMessage(conn));
                               1301                 :                : 
                               1302                 :                :     /* Now that it's closed we should get an error when describing */
  985 michael@paquier.xyz      1303                 :CBC           1 :     res = PQdescribePortal(conn, "cursor_one");
                               1304         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_FATAL_ERROR)
  985 michael@paquier.xyz      1305                 :UBC           0 :         pg_fatal("expected FATAL_ERROR, got %s", PQresStatus(PQresultStatus(res)));
  193 tgl@sss.pgh.pa.us        1306                 :GNC           1 :     PQclear(res);
                               1307                 :                : 
                               1308                 :                :     /*
                               1309                 :                :      * Also test the blocking close, this should not fail since closing a
                               1310                 :                :      * non-existent portal is a no-op
                               1311                 :                :      */
  985 michael@paquier.xyz      1312                 :CBC           1 :     res = PQclosePortal(conn, "cursor_one");
                               1313         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  985 michael@paquier.xyz      1314                 :UBC           0 :         pg_fatal("expected COMMAND_OK, got %s", PQresStatus(PQresultStatus(res)));
  193 tgl@sss.pgh.pa.us        1315                 :GNC           1 :     PQclear(res);
                               1316                 :                : 
 1826 alvherre@alvh.no-ip.     1317                 :CBC           1 :     fprintf(stderr, "ok\n");
                               1318                 :              1 : }
                               1319                 :                : 
                               1320                 :                : /*
                               1321                 :                :  * Test max_protocol_version options.
                               1322                 :                :  */
                               1323                 :                : static void
  347 heikki.linnakangas@i     1324                 :              1 : test_protocol_version(PGconn *conn)
                               1325                 :                : {
                               1326                 :                :     const char **keywords;
                               1327                 :                :     const char **vals;
                               1328                 :                :     int         nopts;
                               1329                 :              1 :     PQconninfoOption *opts = PQconninfo(conn);
                               1330                 :                :     int         protocol_version;
   51 jchampion@postgresql     1331                 :GNC           1 :     int         max_protocol_version_index = -1;
                               1332                 :                :     int         i;
                               1333                 :                : 
                               1334                 :                :     /* Prepare keywords/vals arrays, copied from the existing connection. */
  347 heikki.linnakangas@i     1335                 :CBC           1 :     nopts = 0;
                               1336         [ +  + ]:             52 :     for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
                               1337                 :             51 :         nopts++;
                               1338                 :              1 :     nopts++;                    /* NULL terminator */
                               1339                 :                : 
   16 michael@paquier.xyz      1340                 :GNC           1 :     keywords = pg_malloc0_array(const char *, nopts);
                               1341                 :              1 :     vals = pg_malloc0_array(const char *, nopts);
                               1342                 :                : 
  347 heikki.linnakangas@i     1343                 :CBC           1 :     i = 0;
                               1344         [ +  + ]:             52 :     for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
                               1345                 :                :     {
                               1346                 :                :         /*
                               1347                 :                :          * If the test already specified max_protocol_version, we want to
                               1348                 :                :          * replace it rather than attempting to override it. This matters when
                               1349                 :                :          * testing defaults, because empty option values at the end of the
                               1350                 :                :          * connection string won't replace earlier settings.
                               1351                 :                :          */
   51 jchampion@postgresql     1352         [ +  + ]:GNC          51 :         if (strcmp(opt->keyword, "max_protocol_version") == 0)
                               1353                 :              1 :             max_protocol_version_index = i;
                               1354         [ +  + ]:             50 :         else if (!opt->val)
                               1355                 :             31 :             continue;
                               1356                 :                : 
                               1357                 :             20 :         keywords[i] = opt->keyword;
                               1358                 :             20 :         vals[i] = opt->val;
                               1359                 :                : 
                               1360                 :             20 :         i++;
                               1361                 :                :     }
                               1362                 :                : 
                               1363         [ -  + ]:              1 :     Assert(max_protocol_version_index >= 0);
                               1364                 :                : 
                               1365                 :                :     /*
                               1366                 :                :      * Test default protocol_version (GREASE - should negotiate down to 3.2)
                               1367                 :                :      */
                               1368                 :              1 :     vals[max_protocol_version_index] = "";
                               1369                 :              1 :     conn = PQconnectdbParams(keywords, vals, false);
                               1370                 :                : 
                               1371         [ -  + ]:              1 :     if (PQstatus(conn) != CONNECTION_OK)
   51 jchampion@postgresql     1372                 :UNC           0 :         pg_fatal("Connection to database failed: %s",
                               1373                 :                :                  PQerrorMessage(conn));
                               1374                 :                : 
   51 jchampion@postgresql     1375                 :GNC           1 :     protocol_version = PQfullProtocolVersion(conn);
   20                          1376         [ -  + ]:              1 :     if (protocol_version != 30002)
   20 jchampion@postgresql     1377                 :UNC           0 :         pg_fatal("expected 30002, got %d", protocol_version);
                               1378                 :                : 
   51 jchampion@postgresql     1379                 :GNC           1 :     PQfinish(conn);
                               1380                 :                : 
                               1381                 :                :     /*
                               1382                 :                :      * Test max_protocol_version=3.0
                               1383                 :                :      */
  347 heikki.linnakangas@i     1384                 :CBC           1 :     vals[max_protocol_version_index] = "3.0";
                               1385                 :              1 :     conn = PQconnectdbParams(keywords, vals, false);
                               1386                 :                : 
                               1387         [ -  + ]:              1 :     if (PQstatus(conn) != CONNECTION_OK)
  347 heikki.linnakangas@i     1388                 :UBC           0 :         pg_fatal("Connection to database failed: %s",
                               1389                 :                :                  PQerrorMessage(conn));
                               1390                 :                : 
  347 heikki.linnakangas@i     1391                 :CBC           1 :     protocol_version = PQfullProtocolVersion(conn);
                               1392         [ -  + ]:              1 :     if (protocol_version != 30000)
  347 heikki.linnakangas@i     1393                 :UBC           0 :         pg_fatal("expected 30000, got %d", protocol_version);
                               1394                 :                : 
  347 heikki.linnakangas@i     1395                 :CBC           1 :     PQfinish(conn);
                               1396                 :                : 
                               1397                 :                :     /*
                               1398                 :                :      * Test max_protocol_version=3.1. It's not valid, we went straight from
                               1399                 :                :      * 3.0 to 3.2.
                               1400                 :                :      */
                               1401                 :              1 :     vals[max_protocol_version_index] = "3.1";
                               1402                 :              1 :     conn = PQconnectdbParams(keywords, vals, false);
                               1403                 :                : 
                               1404         [ -  + ]:              1 :     if (PQstatus(conn) != CONNECTION_BAD)
  347 heikki.linnakangas@i     1405                 :UBC           0 :         pg_fatal("Connecting with max_protocol_version 3.1 should have failed.");
                               1406                 :                : 
  347 heikki.linnakangas@i     1407                 :CBC           1 :     PQfinish(conn);
                               1408                 :                : 
                               1409                 :                :     /*
                               1410                 :                :      * Test max_protocol_version=3.2
                               1411                 :                :      */
                               1412                 :              1 :     vals[max_protocol_version_index] = "3.2";
                               1413                 :              1 :     conn = PQconnectdbParams(keywords, vals, false);
                               1414                 :                : 
                               1415         [ -  + ]:              1 :     if (PQstatus(conn) != CONNECTION_OK)
  347 heikki.linnakangas@i     1416                 :UBC           0 :         pg_fatal("Connection to database failed: %s",
                               1417                 :                :                  PQerrorMessage(conn));
                               1418                 :                : 
  347 heikki.linnakangas@i     1419                 :CBC           1 :     protocol_version = PQfullProtocolVersion(conn);
                               1420         [ -  + ]:              1 :     if (protocol_version != 30002)
  347 heikki.linnakangas@i     1421                 :UBC           0 :         pg_fatal("expected 30002, got %d", protocol_version);
                               1422                 :                : 
  347 heikki.linnakangas@i     1423                 :CBC           1 :     PQfinish(conn);
                               1424                 :                : 
                               1425                 :                :     /*
                               1426                 :                :      * Test max_protocol_version=latest. 'latest' currently means '3.2'.
                               1427                 :                :      */
                               1428                 :              1 :     vals[max_protocol_version_index] = "latest";
                               1429                 :              1 :     conn = PQconnectdbParams(keywords, vals, false);
                               1430                 :                : 
                               1431         [ -  + ]:              1 :     if (PQstatus(conn) != CONNECTION_OK)
  347 heikki.linnakangas@i     1432                 :UBC           0 :         pg_fatal("Connection to database failed: %s",
                               1433                 :                :                  PQerrorMessage(conn));
                               1434                 :                : 
  347 heikki.linnakangas@i     1435                 :CBC           1 :     protocol_version = PQfullProtocolVersion(conn);
                               1436         [ -  + ]:              1 :     if (protocol_version != 30002)
  347 heikki.linnakangas@i     1437                 :UBC           0 :         pg_fatal("expected 30002, got %d", protocol_version);
                               1438                 :                : 
  347 heikki.linnakangas@i     1439                 :CBC           1 :     PQfinish(conn);
                               1440                 :                : 
  193 tgl@sss.pgh.pa.us        1441                 :GNC           1 :     pfree(keywords);
                               1442                 :              1 :     pfree(vals);
                               1443                 :              1 :     PQconninfoFree(opts);
  347 heikki.linnakangas@i     1444                 :CBC           1 : }
                               1445                 :                : 
                               1446                 :                : /* Notice processor: print notices, and count how many we got */
                               1447                 :                : static void
 1349 alvherre@alvh.no-ip.     1448                 :              1 : notice_processor(void *arg, const char *message)
                               1449                 :                : {
 1031 tgl@sss.pgh.pa.us        1450                 :              1 :     int        *n_notices = (int *) arg;
                               1451                 :                : 
 1349 alvherre@alvh.no-ip.     1452                 :              1 :     (*n_notices)++;
                               1453                 :              1 :     fprintf(stderr, "NOTICE %d: %s", *n_notices, message);
                               1454                 :              1 : }
                               1455                 :                : 
                               1456                 :                : /* Verify behavior in "idle" state */
                               1457                 :                : static void
                               1458                 :              1 : test_pipeline_idle(PGconn *conn)
                               1459                 :                : {
                               1460                 :              1 :     int         n_notices = 0;
                               1461                 :                : 
                               1462                 :              1 :     fprintf(stderr, "\npipeline idle...\n");
                               1463                 :                : 
                               1464                 :              1 :     PQsetNoticeProcessor(conn, notice_processor, &n_notices);
                               1465                 :                : 
                               1466                 :                :     /* Try to exit pipeline mode in pipeline-idle state */
                               1467         [ -  + ]:              1 :     if (PQenterPipelineMode(conn) != 1)
 1349 alvherre@alvh.no-ip.     1468                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn));
 1269 alvherre@alvh.no-ip.     1469         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT 1", 0, NULL, NULL, NULL, NULL, 0) != 1)
 1349 alvherre@alvh.no-ip.     1470                 :UBC           0 :         pg_fatal("failed to send query: %s", PQerrorMessage(conn));
 1349 alvherre@alvh.no-ip.     1471                 :CBC           1 :     PQsendFlushRequest(conn);
                               1472                 :                : 
  193 tgl@sss.pgh.pa.us        1473                 :GNC           1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                               1474                 :                : 
                               1475                 :              1 :     consume_null_result(conn);
                               1476                 :                : 
 1269 alvherre@alvh.no-ip.     1477         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT 2", 0, NULL, NULL, NULL, NULL, 0) != 1)
 1349 alvherre@alvh.no-ip.     1478                 :UBC           0 :         pg_fatal("failed to send query: %s", PQerrorMessage(conn));
 1349 alvherre@alvh.no-ip.     1479         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) == 1)
 1349 alvherre@alvh.no-ip.     1480                 :UBC           0 :         pg_fatal("exiting pipeline succeeded when it shouldn't");
 1349 alvherre@alvh.no-ip.     1481         [ -  + ]:CBC           1 :     if (strncmp(PQerrorMessage(conn), "cannot exit pipeline mode",
                               1482                 :                :                 strlen("cannot exit pipeline mode")) != 0)
 1349 alvherre@alvh.no-ip.     1483                 :UBC           0 :         pg_fatal("did not get expected error; got: %s",
                               1484                 :                :                  PQerrorMessage(conn));
 1349 alvherre@alvh.no-ip.     1485                 :CBC           1 :     PQsendFlushRequest(conn);
                               1486                 :                : 
  193 tgl@sss.pgh.pa.us        1487                 :GNC           1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                               1488                 :                : 
                               1489                 :              1 :     consume_null_result(conn);
                               1490                 :                : 
 1349 alvherre@alvh.no-ip.     1491         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1349 alvherre@alvh.no-ip.     1492                 :UBC           0 :         pg_fatal("exiting pipeline failed: %s", PQerrorMessage(conn));
                               1493                 :                : 
 1349 alvherre@alvh.no-ip.     1494         [ -  + ]:CBC           1 :     if (n_notices > 0)
 1349 alvherre@alvh.no-ip.     1495                 :UBC           0 :         pg_fatal("got %d notice(s)", n_notices);
 1269 alvherre@alvh.no-ip.     1496                 :CBC           1 :     fprintf(stderr, "ok - 1\n");
                               1497                 :                : 
                               1498                 :                :     /* Have a WARNING in the middle of a resultset */
 1349                          1499         [ -  + ]:              1 :     if (PQenterPipelineMode(conn) != 1)
 1349 alvherre@alvh.no-ip.     1500                 :UBC           0 :         pg_fatal("entering pipeline mode failed: %s", PQerrorMessage(conn));
 1269 alvherre@alvh.no-ip.     1501         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT pg_catalog.pg_advisory_unlock(1,1)", 0, NULL, NULL, NULL, NULL, 0) != 1)
 1349 alvherre@alvh.no-ip.     1502                 :UBC           0 :         pg_fatal("failed to send query: %s", PQerrorMessage(conn));
 1349 alvherre@alvh.no-ip.     1503                 :CBC           1 :     PQsendFlushRequest(conn);
                               1504                 :                : 
  193 tgl@sss.pgh.pa.us        1505                 :GNC           1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                               1506                 :                : 
 1349 alvherre@alvh.no-ip.     1507         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1349 alvherre@alvh.no-ip.     1508                 :UBC           0 :         pg_fatal("failed to exit pipeline mode: %s", PQerrorMessage(conn));
 1269 alvherre@alvh.no-ip.     1509                 :CBC           1 :     fprintf(stderr, "ok - 2\n");
 1349                          1510                 :              1 : }
                               1511                 :                : 
                               1512                 :                : static void
 1826                          1513                 :              1 : test_simple_pipeline(PGconn *conn)
                               1514                 :                : {
                               1515                 :              1 :     const char *dummy_params[1] = {"1"};
                               1516                 :              1 :     Oid         dummy_param_oids[1] = {INT4OID};
                               1517                 :                : 
                               1518                 :              1 :     fprintf(stderr, "simple pipeline... ");
                               1519                 :                : 
                               1520                 :                :     /*
                               1521                 :                :      * Enter pipeline mode and dispatch a set of operations, which we'll then
                               1522                 :                :      * process the results of as they come in.
                               1523                 :                :      *
                               1524                 :                :      * For a simple case we should be able to do this without interim
                               1525                 :                :      * processing of results since our output buffer will give us enough slush
                               1526                 :                :      * to work with and we won't block on sending. So blocking mode is fine.
                               1527                 :                :      */
                               1528         [ -  + ]:              1 :     if (PQisnonblocking(conn))
 1826 alvherre@alvh.no-ip.     1529                 :UBC           0 :         pg_fatal("Expected blocking connection mode");
                               1530                 :                : 
 1826 alvherre@alvh.no-ip.     1531         [ -  + ]:CBC           1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1532                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn));
                               1533                 :                : 
 1826 alvherre@alvh.no-ip.     1534         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT $1",
                               1535                 :                :                           1, dummy_param_oids, dummy_params,
                               1536                 :                :                           NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.     1537                 :UBC           0 :         pg_fatal("dispatching SELECT failed: %s", PQerrorMessage(conn));
                               1538                 :                : 
 1826 alvherre@alvh.no-ip.     1539         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 0)
 1826 alvherre@alvh.no-ip.     1540                 :UBC           0 :         pg_fatal("exiting pipeline mode with work in progress should fail, but succeeded");
                               1541                 :                : 
 1826 alvherre@alvh.no-ip.     1542         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1543                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                               1544                 :                : 
  193 tgl@sss.pgh.pa.us        1545                 :GNC           1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                               1546                 :                : 
                               1547                 :              1 :     consume_null_result(conn);
                               1548                 :                : 
                               1549                 :                :     /*
                               1550                 :                :      * Even though we've processed the result there's still a sync to come and
                               1551                 :                :      * we can't exit pipeline mode yet
                               1552                 :                :      */
 1826 alvherre@alvh.no-ip.     1553         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 0)
 1826 alvherre@alvh.no-ip.     1554                 :UBC           0 :         pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly");
                               1555                 :                : 
  193 tgl@sss.pgh.pa.us        1556                 :GNC           1 :     consume_result_status(conn, PGRES_PIPELINE_SYNC);
                               1557                 :                : 
                               1558                 :              1 :     consume_null_result(conn);
                               1559                 :                : 
                               1560                 :                :     /* We're still in pipeline mode... */
 1826 alvherre@alvh.no-ip.     1561         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) == PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.     1562                 :UBC           0 :         pg_fatal("Fell out of pipeline mode somehow");
                               1563                 :                : 
                               1564                 :                :     /* ... until we end it, which we can safely do now */
 1826 alvherre@alvh.no-ip.     1565         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1566                 :UBC           0 :         pg_fatal("attempt to exit pipeline mode failed when it should've succeeded: %s",
                               1567                 :                :                  PQerrorMessage(conn));
                               1568                 :                : 
 1826 alvherre@alvh.no-ip.     1569         [ -  + ]:CBC           1 :     if (PQpipelineStatus(conn) != PQ_PIPELINE_OFF)
 1826 alvherre@alvh.no-ip.     1570                 :UBC           0 :         pg_fatal("Exiting pipeline mode didn't seem to work");
                               1571                 :                : 
 1826 alvherre@alvh.no-ip.     1572                 :CBC           1 :     fprintf(stderr, "ok\n");
                               1573                 :              1 : }
                               1574                 :                : 
                               1575                 :                : static void
                               1576                 :              1 : test_singlerowmode(PGconn *conn)
                               1577                 :                : {
                               1578                 :                :     PGresult   *res;
                               1579                 :                :     int         i;
                               1580                 :              1 :     bool        pipeline_ended = false;
                               1581                 :                : 
                               1582         [ -  + ]:              1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1583                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s",
                               1584                 :                :                  PQerrorMessage(conn));
                               1585                 :                : 
                               1586                 :                :     /* One series of three commands, using single-row mode for the first two. */
 1826 alvherre@alvh.no-ip.     1587         [ +  + ]:CBC           4 :     for (i = 0; i < 3; i++)
                               1588                 :                :     {
                               1589                 :                :         char       *param[1];
                               1590                 :                : 
                               1591                 :              3 :         param[0] = psprintf("%d", 44 + i);
                               1592                 :                : 
                               1593         [ -  + ]:              3 :         if (PQsendQueryParams(conn,
                               1594                 :                :                               "SELECT generate_series(42, $1)",
                               1595                 :                :                               1,
                               1596                 :                :                               NULL,
                               1597                 :                :                               (const char *const *) param,
                               1598                 :                :                               NULL,
                               1599                 :                :                               NULL,
                               1600                 :                :                               0) != 1)
 1826 alvherre@alvh.no-ip.     1601                 :UBC           0 :             pg_fatal("failed to send query: %s",
                               1602                 :                :                      PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1603                 :CBC           3 :         pfree(param[0]);
                               1604                 :                :     }
                               1605         [ -  + ]:              1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1606                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
                               1607                 :                : 
 1826 alvherre@alvh.no-ip.     1608         [ +  + ]:CBC           5 :     for (i = 0; !pipeline_ended; i++)
                               1609                 :                :     {
                               1610                 :              4 :         bool        first = true;
                               1611                 :                :         bool        saw_ending_tuplesok;
                               1612                 :              4 :         bool        isSingleTuple = false;
                               1613                 :                : 
                               1614                 :                :         /* Set single row mode for only first 2 SELECT queries */
                               1615         [ +  + ]:              4 :         if (i < 2)
                               1616                 :                :         {
                               1617         [ -  + ]:              2 :             if (PQsetSingleRowMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1618                 :UBC           0 :                 pg_fatal("PQsetSingleRowMode() failed for i=%d", i);
                               1619                 :                :         }
                               1620                 :                : 
                               1621                 :                :         /* Consume rows for this query */
 1826 alvherre@alvh.no-ip.     1622                 :CBC           4 :         saw_ending_tuplesok = false;
                               1623         [ +  + ]:             14 :         while ((res = PQgetResult(conn)) != NULL)
                               1624                 :                :         {
                               1625                 :             11 :             ExecStatusType est = PQresultStatus(res);
                               1626                 :                : 
                               1627         [ +  + ]:             11 :             if (est == PGRES_PIPELINE_SYNC)
                               1628                 :                :             {
                               1629                 :              1 :                 fprintf(stderr, "end of pipeline reached\n");
                               1630                 :              1 :                 pipeline_ended = true;
                               1631                 :              1 :                 PQclear(res);
                               1632         [ -  + ]:              1 :                 if (i != 3)
 1826 alvherre@alvh.no-ip.     1633                 :UBC           0 :                     pg_fatal("Expected three results, got %d", i);
 1826 alvherre@alvh.no-ip.     1634                 :CBC           1 :                 break;
                               1635                 :                :             }
                               1636                 :                : 
                               1637                 :                :             /* Expect SINGLE_TUPLE for queries 0 and 1, TUPLES_OK for 2 */
                               1638         [ +  + ]:             10 :             if (first)
                               1639                 :                :             {
                               1640   [ +  +  -  + ]:              3 :                 if (i <= 1 && est != PGRES_SINGLE_TUPLE)
 1826 alvherre@alvh.no-ip.     1641                 :UBC           0 :                     pg_fatal("Expected PGRES_SINGLE_TUPLE for query %d, got %s",
                               1642                 :                :                              i, PQresStatus(est));
 1826 alvherre@alvh.no-ip.     1643   [ +  +  -  + ]:CBC           3 :                 if (i >= 2 && est != PGRES_TUPLES_OK)
 1826 alvherre@alvh.no-ip.     1644                 :UBC           0 :                     pg_fatal("Expected PGRES_TUPLES_OK for query %d, got %s",
                               1645                 :                :                              i, PQresStatus(est));
 1826 alvherre@alvh.no-ip.     1646                 :CBC           3 :                 first = false;
                               1647                 :                :             }
                               1648                 :                : 
                               1649                 :             10 :             fprintf(stderr, "Result status %s for query %d", PQresStatus(est), i);
                               1650      [ +  +  - ]:             10 :             switch (est)
                               1651                 :                :             {
                               1652                 :              3 :                 case PGRES_TUPLES_OK:
                               1653                 :              3 :                     fprintf(stderr, ", tuples: %d\n", PQntuples(res));
                               1654                 :              3 :                     saw_ending_tuplesok = true;
                               1655         [ +  + ]:              3 :                     if (isSingleTuple)
                               1656                 :                :                     {
                               1657         [ +  - ]:              2 :                         if (PQntuples(res) == 0)
                               1658                 :              2 :                             fprintf(stderr, "all tuples received in query %d\n", i);
                               1659                 :                :                         else
 1826 alvherre@alvh.no-ip.     1660                 :UBC           0 :                             pg_fatal("Expected to follow PGRES_SINGLE_TUPLE, but received PGRES_TUPLES_OK directly instead");
                               1661                 :                :                     }
 1826 alvherre@alvh.no-ip.     1662                 :CBC           3 :                     break;
                               1663                 :                : 
                               1664                 :              7 :                 case PGRES_SINGLE_TUPLE:
                               1665                 :              7 :                     isSingleTuple = true;
                               1666                 :              7 :                     fprintf(stderr, ", %d tuple: %s\n", PQntuples(res), PQgetvalue(res, 0, 0));
                               1667                 :              7 :                     break;
                               1668                 :                : 
 1826 alvherre@alvh.no-ip.     1669                 :UBC           0 :                 default:
                               1670                 :              0 :                     pg_fatal("unexpected");
                               1671                 :                :             }
 1826 alvherre@alvh.no-ip.     1672                 :CBC          10 :             PQclear(res);
                               1673                 :                :         }
                               1674   [ +  +  -  + ]:              4 :         if (!pipeline_ended && !saw_ending_tuplesok)
 1826 alvherre@alvh.no-ip.     1675                 :UBC           0 :             pg_fatal("didn't get expected terminating TUPLES_OK");
                               1676                 :                :     }
                               1677                 :                : 
                               1678                 :                :     /*
                               1679                 :                :      * Now issue one command, get its results in with single-row mode, then
                               1680                 :                :      * issue another command, and get its results in normal mode; make sure
                               1681                 :                :      * the single-row mode flag is reset as expected.
                               1682                 :                :      */
 1248 alvherre@alvh.no-ip.     1683         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT generate_series(0, 0)",
                               1684                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1248 alvherre@alvh.no-ip.     1685                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1686                 :                :                  PQerrorMessage(conn));
 1248 alvherre@alvh.no-ip.     1687         [ -  + ]:CBC           1 :     if (PQsendFlushRequest(conn) != 1)
 1248 alvherre@alvh.no-ip.     1688                 :UBC           0 :         pg_fatal("failed to send flush request");
 1248 alvherre@alvh.no-ip.     1689         [ -  + ]:CBC           1 :     if (PQsetSingleRowMode(conn) != 1)
 1248 alvherre@alvh.no-ip.     1690                 :UBC           0 :         pg_fatal("PQsetSingleRowMode() failed");
                               1691                 :                : 
  193 tgl@sss.pgh.pa.us        1692                 :GNC           1 :     consume_result_status(conn, PGRES_SINGLE_TUPLE);
                               1693                 :                : 
                               1694                 :              1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                               1695                 :                : 
                               1696                 :              1 :     consume_null_result(conn);
                               1697                 :                : 
 1248 alvherre@alvh.no-ip.     1698         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT 1",
                               1699                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1248 alvherre@alvh.no-ip.     1700                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1701                 :                :                  PQerrorMessage(conn));
 1248 alvherre@alvh.no-ip.     1702         [ -  + ]:CBC           1 :     if (PQsendFlushRequest(conn) != 1)
 1248 alvherre@alvh.no-ip.     1703                 :UBC           0 :         pg_fatal("failed to send flush request");
                               1704                 :                : 
  193 tgl@sss.pgh.pa.us        1705                 :GNC           1 :     consume_result_status(conn, PGRES_TUPLES_OK);
                               1706                 :                : 
                               1707                 :              1 :     consume_null_result(conn);
                               1708                 :                : 
                               1709                 :                :     /*
                               1710                 :                :      * Try chunked mode as well; make sure that it correctly delivers a
                               1711                 :                :      * partial final chunk.
                               1712                 :                :      */
  708 tgl@sss.pgh.pa.us        1713         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn, "SELECT generate_series(1, 5)",
                               1714                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
  708 tgl@sss.pgh.pa.us        1715                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1716                 :                :                  PQerrorMessage(conn));
  708 tgl@sss.pgh.pa.us        1717         [ -  + ]:CBC           1 :     if (PQsendFlushRequest(conn) != 1)
  708 tgl@sss.pgh.pa.us        1718                 :UBC           0 :         pg_fatal("failed to send flush request");
  708 tgl@sss.pgh.pa.us        1719         [ -  + ]:CBC           1 :     if (PQsetChunkedRowsMode(conn, 3) != 1)
  708 tgl@sss.pgh.pa.us        1720                 :UBC           0 :         pg_fatal("PQsetChunkedRowsMode() failed");
                               1721                 :                : 
  193 tgl@sss.pgh.pa.us        1722                 :GNC           1 :     res = confirm_result_status(conn, PGRES_TUPLES_CHUNK);
  708 tgl@sss.pgh.pa.us        1723         [ -  + ]:CBC           1 :     if (PQntuples(res) != 3)
  708 tgl@sss.pgh.pa.us        1724                 :UBC           0 :         pg_fatal("Expected 3 rows, got %d", PQntuples(res));
  193 tgl@sss.pgh.pa.us        1725                 :GNC           1 :     PQclear(res);
                               1726                 :                : 
                               1727                 :              1 :     res = confirm_result_status(conn, PGRES_TUPLES_CHUNK);
  708 tgl@sss.pgh.pa.us        1728         [ -  + ]:CBC           1 :     if (PQntuples(res) != 2)
  708 tgl@sss.pgh.pa.us        1729                 :UBC           0 :         pg_fatal("Expected 2 rows, got %d", PQntuples(res));
  193 tgl@sss.pgh.pa.us        1730                 :GNC           1 :     PQclear(res);
                               1731                 :                : 
                               1732                 :              1 :     res = confirm_result_status(conn, PGRES_TUPLES_OK);
  708 tgl@sss.pgh.pa.us        1733         [ -  + ]:CBC           1 :     if (PQntuples(res) != 0)
  708 tgl@sss.pgh.pa.us        1734                 :UBC           0 :         pg_fatal("Expected 0 rows, got %d", PQntuples(res));
  193 tgl@sss.pgh.pa.us        1735                 :GNC           1 :     PQclear(res);
                               1736                 :                : 
                               1737                 :              1 :     consume_null_result(conn);
                               1738                 :                : 
 1826 alvherre@alvh.no-ip.     1739         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1740                 :UBC           0 :         pg_fatal("failed to end pipeline mode: %s", PQerrorMessage(conn));
                               1741                 :                : 
 1349 alvherre@alvh.no-ip.     1742                 :CBC           1 :     fprintf(stderr, "ok\n");
 1826                          1743                 :              1 : }
                               1744                 :                : 
                               1745                 :                : /*
                               1746                 :                :  * Simple test to verify that a pipeline is discarded as a whole when there's
                               1747                 :                :  * an error, ignoring transaction commands.
                               1748                 :                :  */
                               1749                 :                : static void
                               1750                 :              1 : test_transaction(PGconn *conn)
                               1751                 :                : {
                               1752                 :                :     PGresult   *res;
                               1753                 :                :     bool        expect_null;
                               1754                 :              1 :     int         num_syncs = 0;
                               1755                 :                : 
                               1756                 :              1 :     res = PQexec(conn, "DROP TABLE IF EXISTS pq_pipeline_tst;"
                               1757                 :                :                  "CREATE TABLE pq_pipeline_tst (id int)");
                               1758         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
 1826 alvherre@alvh.no-ip.     1759                 :UBC           0 :         pg_fatal("failed to create test table: %s",
                               1760                 :                :                  PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1761                 :CBC           1 :     PQclear(res);
                               1762                 :                : 
                               1763         [ -  + ]:              1 :     if (PQenterPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1764                 :UBC           0 :         pg_fatal("failed to enter pipeline mode: %s",
                               1765                 :                :                  PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1766         [ -  + ]:CBC           1 :     if (PQsendPrepare(conn, "rollback", "ROLLBACK", 0, NULL) != 1)
 1826 alvherre@alvh.no-ip.     1767                 :UBC           0 :         pg_fatal("could not send prepare on pipeline: %s",
                               1768                 :                :                  PQerrorMessage(conn));
                               1769                 :                : 
 1826 alvherre@alvh.no-ip.     1770         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn,
                               1771                 :                :                           "BEGIN",
                               1772                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.     1773                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1774                 :                :                  PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1775         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn,
                               1776                 :                :                           "SELECT 0/0",
                               1777                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.     1778                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1779                 :                :                  PQerrorMessage(conn));
                               1780                 :                : 
                               1781                 :                :     /*
                               1782                 :                :      * send a ROLLBACK using a prepared stmt. Doesn't work because we need to
                               1783                 :                :      * get out of the pipeline-aborted state first.
                               1784                 :                :      */
 1826 alvherre@alvh.no-ip.     1785         [ -  + ]:CBC           1 :     if (PQsendQueryPrepared(conn, "rollback", 0, NULL, NULL, NULL, 1) != 1)
 1826 alvherre@alvh.no-ip.     1786                 :UBC           0 :         pg_fatal("failed to execute prepared: %s",
                               1787                 :                :                  PQerrorMessage(conn));
                               1788                 :                : 
                               1789                 :                :     /* This insert fails because we're in pipeline-aborted state */
 1826 alvherre@alvh.no-ip.     1790         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn,
                               1791                 :                :                           "INSERT INTO pq_pipeline_tst VALUES (1)",
                               1792                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.     1793                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1794                 :                :                  PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1795         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1796                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1797                 :CBC           1 :     num_syncs++;
                               1798                 :                : 
                               1799                 :                :     /*
                               1800                 :                :      * This insert fails even though the pipeline got a SYNC, because we're in
                               1801                 :                :      * an aborted transaction
                               1802                 :                :      */
                               1803         [ -  + ]:              1 :     if (PQsendQueryParams(conn,
                               1804                 :                :                           "INSERT INTO pq_pipeline_tst VALUES (2)",
                               1805                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.     1806                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1807                 :                :                  PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1808         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1809                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1810                 :CBC           1 :     num_syncs++;
                               1811                 :                : 
                               1812                 :                :     /*
                               1813                 :                :      * Send ROLLBACK using prepared stmt. This one works because we just did
                               1814                 :                :      * PQpipelineSync above.
                               1815                 :                :      */
                               1816         [ -  + ]:              1 :     if (PQsendQueryPrepared(conn, "rollback", 0, NULL, NULL, NULL, 1) != 1)
 1826 alvherre@alvh.no-ip.     1817                 :UBC           0 :         pg_fatal("failed to execute prepared: %s",
                               1818                 :                :                  PQerrorMessage(conn));
                               1819                 :                : 
                               1820                 :                :     /*
                               1821                 :                :      * Now that we're out of a transaction and in pipeline-good mode, this
                               1822                 :                :      * insert works
                               1823                 :                :      */
 1826 alvherre@alvh.no-ip.     1824         [ -  + ]:CBC           1 :     if (PQsendQueryParams(conn,
                               1825                 :                :                           "INSERT INTO pq_pipeline_tst VALUES (3)",
                               1826                 :                :                           0, NULL, NULL, NULL, NULL, 0) != 1)
 1826 alvherre@alvh.no-ip.     1827                 :UBC           0 :         pg_fatal("failed to send query: %s",
                               1828                 :                :                  PQerrorMessage(conn));
                               1829                 :                :     /* Send two syncs now -- match up to SYNC messages below */
 1826 alvherre@alvh.no-ip.     1830         [ -  + ]:CBC           1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1831                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1832                 :CBC           1 :     num_syncs++;
                               1833         [ -  + ]:              1 :     if (PQpipelineSync(conn) != 1)
 1826 alvherre@alvh.no-ip.     1834                 :UBC           0 :         pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1835                 :CBC           1 :     num_syncs++;
                               1836                 :                : 
                               1837                 :              1 :     expect_null = false;
                               1838                 :              1 :     for (int i = 0;; i++)
                               1839                 :             19 :     {
                               1840                 :                :         ExecStatusType restype;
                               1841                 :                : 
                               1842                 :             20 :         res = PQgetResult(conn);
                               1843         [ +  + ]:             20 :         if (res == NULL)
                               1844                 :                :         {
                               1845                 :              8 :             printf("%d: got NULL result\n", i);
                               1846         [ -  + ]:              8 :             if (!expect_null)
 1826 alvherre@alvh.no-ip.     1847                 :UBC           0 :                 pg_fatal("did not expect NULL here");
 1826 alvherre@alvh.no-ip.     1848                 :CBC           8 :             expect_null = false;
                               1849                 :              8 :             continue;
                               1850                 :                :         }
                               1851                 :             12 :         restype = PQresultStatus(res);
                               1852                 :             12 :         printf("%d: got status %s", i, PQresStatus(restype));
                               1853         [ -  + ]:             12 :         if (expect_null)
 1826 alvherre@alvh.no-ip.     1854                 :UBC           0 :             pg_fatal("expected NULL");
 1826 alvherre@alvh.no-ip.     1855         [ +  + ]:CBC          12 :         if (restype == PGRES_FATAL_ERROR)
                               1856                 :              2 :             printf("; error: %s", PQerrorMessage(conn));
                               1857         [ +  + ]:             10 :         else if (restype == PGRES_PIPELINE_ABORTED)
                               1858                 :                :         {
                               1859                 :              2 :             printf(": command didn't run because pipeline aborted\n");
                               1860                 :                :         }
                               1861                 :                :         else
                               1862                 :              8 :             printf("\n");
                               1863                 :             12 :         PQclear(res);
                               1864                 :                : 
                               1865         [ +  + ]:             12 :         if (restype == PGRES_PIPELINE_SYNC)
                               1866                 :              4 :             num_syncs--;
                               1867                 :                :         else
                               1868                 :              8 :             expect_null = true;
                               1869         [ +  + ]:             12 :         if (num_syncs <= 0)
                               1870                 :              1 :             break;
                               1871                 :                :     }
                               1872                 :                : 
  193 tgl@sss.pgh.pa.us        1873                 :GNC           1 :     consume_null_result(conn);
                               1874                 :                : 
 1826 alvherre@alvh.no-ip.     1875         [ -  + ]:CBC           1 :     if (PQexitPipelineMode(conn) != 1)
 1826 alvherre@alvh.no-ip.     1876                 :UBC           0 :         pg_fatal("failed to end pipeline mode: %s", PQerrorMessage(conn));
                               1877                 :                : 
                               1878                 :                :     /* We expect to find one tuple containing the value "3" */
 1826 alvherre@alvh.no-ip.     1879                 :CBC           1 :     res = PQexec(conn, "SELECT * FROM pq_pipeline_tst");
                               1880         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
 1826 alvherre@alvh.no-ip.     1881                 :UBC           0 :         pg_fatal("failed to obtain result: %s", PQerrorMessage(conn));
 1826 alvherre@alvh.no-ip.     1882         [ -  + ]:CBC           1 :     if (PQntuples(res) != 1)
 1826 alvherre@alvh.no-ip.     1883                 :UBC           0 :         pg_fatal("did not get 1 tuple");
 1826 alvherre@alvh.no-ip.     1884         [ -  + ]:CBC           1 :     if (strcmp(PQgetvalue(res, 0, 0), "3") != 0)
 1826 alvherre@alvh.no-ip.     1885                 :UBC           0 :         pg_fatal("did not get expected tuple");
 1826 alvherre@alvh.no-ip.     1886                 :CBC           1 :     PQclear(res);
                               1887                 :                : 
                               1888                 :              1 :     fprintf(stderr, "ok\n");
                               1889                 :              1 : }
                               1890                 :                : 
                               1891                 :                : /*
                               1892                 :                :  * In this test mode we send a stream of queries, with one in the middle
                               1893                 :                :  * causing an error.  Verify that we can still send some more after the
                               1894                 :                :  * error and have libpq work properly.
                               1895                 :                :  */
                               1896                 :                : static void
 1710                          1897                 :              1 : test_uniqviol(PGconn *conn)
                               1898                 :                : {
                               1899                 :              1 :     int         sock = PQsocket(conn);
                               1900                 :                :     PGresult   *res;
                               1901                 :              1 :     Oid         paramTypes[2] = {INT8OID, INT8OID};
                               1902                 :                :     const char *paramValues[2];
                               1903                 :                :     char        paramValue0[MAXINT8LEN];
                               1904                 :                :     char        paramValue1[MAXINT8LEN];
                               1905                 :              1 :     int         ctr = 0;
                               1906                 :              1 :     int         numsent = 0;
                               1907                 :              1 :     int         results = 0;
                               1908                 :              1 :     bool        read_done = false;
                               1909                 :              1 :     bool        write_done = false;
                               1910                 :              1 :     bool        error_sent = false;
                               1911                 :              1 :     bool        got_error = false;
                               1912                 :              1 :     int         switched = 0;
                               1913                 :              1 :     int         socketful = 0;
                               1914                 :                :     fd_set      in_fds;
                               1915                 :                :     fd_set      out_fds;
                               1916                 :                : 
                               1917                 :              1 :     fprintf(stderr, "uniqviol ...");
                               1918                 :                : 
                               1919                 :              1 :     PQsetnonblocking(conn, 1);
                               1920                 :                : 
                               1921                 :              1 :     paramValues[0] = paramValue0;
                               1922                 :              1 :     paramValues[1] = paramValue1;
                               1923                 :              1 :     sprintf(paramValue1, "42");
                               1924                 :                : 
                               1925                 :              1 :     res = PQexec(conn, "drop table if exists ppln_uniqviol;"
                               1926                 :                :                  "create table ppln_uniqviol(id bigint primary key, idata bigint)");
                               1927         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
 1710 alvherre@alvh.no-ip.     1928                 :UBC           0 :         pg_fatal("failed to create table: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us        1929                 :GNC           1 :     PQclear(res);
                               1930                 :                : 
 1710 alvherre@alvh.no-ip.     1931                 :CBC           1 :     res = PQexec(conn, "begin");
                               1932         [ -  + ]:              1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
 1710 alvherre@alvh.no-ip.     1933                 :UBC           0 :         pg_fatal("failed to begin transaction: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us        1934                 :GNC           1 :     PQclear(res);
                               1935                 :                : 
 1710 alvherre@alvh.no-ip.     1936                 :CBC           1 :     res = PQprepare(conn, "insertion",
                               1937                 :                :                     "insert into ppln_uniqviol values ($1, $2) returning id",
                               1938                 :                :                     2, paramTypes);
  193 tgl@sss.pgh.pa.us        1939         [ -  + ]:GNC           1 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
 1710 alvherre@alvh.no-ip.     1940                 :UBC           0 :         pg_fatal("failed to prepare query: %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us        1941                 :GNC           1 :     PQclear(res);
                               1942                 :                : 
 1710 alvherre@alvh.no-ip.     1943         [ -  + ]:CBC           1 :     if (PQenterPipelineMode(conn) != 1)
 1710 alvherre@alvh.no-ip.     1944                 :UBC           0 :         pg_fatal("failed to enter pipeline mode");
                               1945                 :                : 
 1710 alvherre@alvh.no-ip.     1946         [ +  - ]:CBC           7 :     while (!read_done)
                               1947                 :                :     {
                               1948                 :                :         /*
                               1949                 :                :          * Avoid deadlocks by reading everything the server has sent before
                               1950                 :                :          * sending anything.  (Special precaution is needed here to process
                               1951                 :                :          * PQisBusy before testing the socket for read-readiness, because the
                               1952                 :                :          * socket does not turn read-ready after "sending" queries in aborted
                               1953                 :                :          * pipeline mode.)
                               1954                 :                :          */
                               1955         [ +  + ]:            606 :         while (PQisBusy(conn) == 0)
                               1956                 :                :         {
                               1957                 :                :             bool        new_error;
                               1958                 :                : 
                               1959         [ +  + ]:            601 :             if (results >= numsent)
                               1960                 :                :             {
                               1961         [ -  + ]:              1 :                 if (write_done)
 1710 alvherre@alvh.no-ip.     1962                 :UBC           0 :                     read_done = true;
 1710 alvherre@alvh.no-ip.     1963                 :CBC           1 :                 break;
                               1964                 :                :             }
                               1965                 :                : 
                               1966                 :            600 :             res = PQgetResult(conn);
                               1967                 :            600 :             new_error = process_result(conn, res, results, numsent);
                               1968   [ +  +  -  + ]:            600 :             if (new_error && got_error)
 1710 alvherre@alvh.no-ip.     1969                 :UBC           0 :                 pg_fatal("got two errors");
 1710 alvherre@alvh.no-ip.     1970                 :CBC         600 :             got_error |= new_error;
                               1971         [ +  + ]:            600 :             if (results++ >= numsent - 1)
                               1972                 :                :             {
                               1973         [ +  - ]:              1 :                 if (write_done)
                               1974                 :              1 :                     read_done = true;
                               1975                 :              1 :                 break;
                               1976                 :                :             }
                               1977                 :                :         }
                               1978                 :                : 
                               1979         [ +  + ]:              7 :         if (read_done)
                               1980                 :              1 :             break;
                               1981                 :                : 
                               1982         [ +  + ]:            102 :         FD_ZERO(&out_fds);
                               1983                 :              6 :         FD_SET(sock, &out_fds);
                               1984                 :                : 
                               1985         [ +  + ]:            102 :         FD_ZERO(&in_fds);
                               1986                 :              6 :         FD_SET(sock, &in_fds);
                               1987                 :                : 
                               1988   [ -  +  -  + ]:              6 :         if (select(sock + 1, &in_fds, write_done ? NULL : &out_fds, NULL, NULL) == -1)
                               1989                 :                :         {
 1710 alvherre@alvh.no-ip.     1990         [ #  # ]:UBC           0 :             if (errno == EINTR)
                               1991                 :              0 :                 continue;
                               1992                 :              0 :             pg_fatal("select() failed: %m");
                               1993                 :                :         }
                               1994                 :                : 
 1710 alvherre@alvh.no-ip.     1995   [ +  +  -  + ]:CBC           6 :         if (FD_ISSET(sock, &in_fds) && PQconsumeInput(conn) == 0)
 1710 alvherre@alvh.no-ip.     1996                 :UBC           0 :             pg_fatal("PQconsumeInput failed: %s", PQerrorMessage(conn));
                               1997                 :                : 
                               1998                 :                :         /*
                               1999                 :                :          * If the socket is writable and we haven't finished sending queries,
                               2000                 :                :          * send some.
                               2001                 :                :          */
 1710 alvherre@alvh.no-ip.     2002   [ +  -  +  + ]:CBC           6 :         if (!write_done && FD_ISSET(sock, &out_fds))
                               2003                 :                :         {
                               2004                 :                :             for (;;)
                               2005                 :            597 :             {
                               2006                 :                :                 int         flush;
                               2007                 :                : 
                               2008                 :                :                 /*
                               2009                 :                :                  * provoke uniqueness violation exactly once after having
                               2010                 :                :                  * switched to read mode.
                               2011                 :                :                  */
                               2012   [ +  +  +  +  :            600 :                 if (switched >= 1 && !error_sent && ctr % socketful >= socketful / 2)
                                              +  + ]
                               2013                 :                :                 {
                               2014                 :              1 :                     sprintf(paramValue0, "%d", numsent / 2);
                               2015                 :              1 :                     fprintf(stderr, "E");
                               2016                 :              1 :                     error_sent = true;
                               2017                 :                :                 }
                               2018                 :                :                 else
                               2019                 :                :                 {
                               2020                 :            599 :                     fprintf(stderr, ".");
                               2021                 :            599 :                     sprintf(paramValue0, "%d", ctr++);
                               2022                 :                :                 }
                               2023                 :                : 
                               2024         [ -  + ]:            600 :                 if (PQsendQueryPrepared(conn, "insertion", 2, paramValues, NULL, NULL, 0) != 1)
 1710 alvherre@alvh.no-ip.     2025                 :UBC           0 :                     pg_fatal("failed to execute prepared query: %s", PQerrorMessage(conn));
 1710 alvherre@alvh.no-ip.     2026                 :CBC         600 :                 numsent++;
                               2027                 :                : 
                               2028                 :                :                 /* Are we done writing? */
                               2029   [ +  +  +  +  :            600 :                 if (socketful != 0 && numsent % socketful == 42 && error_sent)
                                              +  + ]
                               2030                 :                :                 {
                               2031         [ -  + ]:              1 :                     if (PQsendFlushRequest(conn) != 1)
 1710 alvherre@alvh.no-ip.     2032                 :UBC           0 :                         pg_fatal("failed to send flush request");
 1710 alvherre@alvh.no-ip.     2033                 :CBC           1 :                     write_done = true;
                               2034                 :              1 :                     fprintf(stderr, "\ndone writing\n");
                               2035                 :              1 :                     PQflush(conn);
                               2036                 :              1 :                     break;
                               2037                 :                :                 }
                               2038                 :                : 
                               2039                 :                :                 /* is the outgoing socket full? */
                               2040                 :            599 :                 flush = PQflush(conn);
                               2041         [ -  + ]:            599 :                 if (flush == -1)
 1710 alvherre@alvh.no-ip.     2042                 :UBC           0 :                     pg_fatal("failed to flush: %s", PQerrorMessage(conn));
 1710 alvherre@alvh.no-ip.     2043         [ +  + ]:CBC         599 :                 if (flush == 1)
                               2044                 :                :                 {
                               2045         [ +  + ]:              2 :                     if (socketful == 0)
                               2046                 :              1 :                         socketful = numsent;
                               2047                 :              2 :                     fprintf(stderr, "\nswitch to reading\n");
                               2048                 :              2 :                     switched++;
                               2049                 :              2 :                     break;
                               2050                 :                :                 }
                               2051                 :                :             }
                               2052                 :                :         }
                               2053                 :                :     }
                               2054                 :                : 
                               2055         [ -  + ]:              1 :     if (!got_error)
 1710 alvherre@alvh.no-ip.     2056                 :UBC           0 :         pg_fatal("did not get expected error");
                               2057                 :                : 
 1710 alvherre@alvh.no-ip.     2058                 :CBC           1 :     fprintf(stderr, "ok\n");
                               2059                 :              1 : }
                               2060                 :                : 
                               2061                 :                : /*
                               2062                 :                :  * Subroutine for test_uniqviol; given a PGresult, print it out and consume
                               2063                 :                :  * the expected NULL that should follow it.
                               2064                 :                :  *
                               2065                 :                :  * Returns true if we read a fatal error message, otherwise false.
                               2066                 :                :  */
                               2067                 :                : static bool
                               2068                 :            600 : process_result(PGconn *conn, PGresult *res, int results, int numsent)
                               2069                 :                : {
                               2070                 :            600 :     bool        got_error = false;
                               2071                 :                : 
                               2072         [ -  + ]:            600 :     if (res == NULL)
 1710 alvherre@alvh.no-ip.     2073                 :UBC           0 :         pg_fatal("got unexpected NULL");
                               2074                 :                : 
 1710 alvherre@alvh.no-ip.     2075   [ +  +  +  - ]:CBC         600 :     switch (PQresultStatus(res))
                               2076                 :                :     {
                               2077                 :              1 :         case PGRES_FATAL_ERROR:
                               2078                 :              1 :             got_error = true;
                               2079                 :              1 :             fprintf(stderr, "result %d/%d (error): %s\n", results, numsent, PQerrorMessage(conn));
                               2080                 :              1 :             PQclear(res);
  193 tgl@sss.pgh.pa.us        2081                 :GNC           1 :             consume_null_result(conn);
 1710 alvherre@alvh.no-ip.     2082                 :CBC           1 :             break;
                               2083                 :                : 
                               2084                 :            418 :         case PGRES_TUPLES_OK:
                               2085                 :            418 :             fprintf(stderr, "result %d/%d: %s\n", results, numsent, PQgetvalue(res, 0, 0));
                               2086                 :            418 :             PQclear(res);
  193 tgl@sss.pgh.pa.us        2087                 :GNC         418 :             consume_null_result(conn);
 1710 alvherre@alvh.no-ip.     2088                 :CBC         418 :             break;
                               2089                 :                : 
                               2090                 :            181 :         case PGRES_PIPELINE_ABORTED:
                               2091                 :            181 :             fprintf(stderr, "result %d/%d: pipeline aborted\n", results, numsent);
  193 tgl@sss.pgh.pa.us        2092                 :GNC         181 :             PQclear(res);
                               2093                 :            181 :             consume_null_result(conn);
 1710 alvherre@alvh.no-ip.     2094                 :CBC         181 :             break;
                               2095                 :                : 
 1710 alvherre@alvh.no-ip.     2096                 :UBC           0 :         default:
                               2097                 :              0 :             pg_fatal("got unexpected %s", PQresStatus(PQresultStatus(res)));
                               2098                 :                :     }
                               2099                 :                : 
 1710 alvherre@alvh.no-ip.     2100                 :CBC         600 :     return got_error;
                               2101                 :                : }
                               2102                 :                : 
                               2103                 :                : 
                               2104                 :                : static void
 1826 alvherre@alvh.no-ip.     2105                 :UBC           0 : usage(const char *progname)
                               2106                 :                : {
                               2107                 :              0 :     fprintf(stderr, "%s tests libpq's pipeline mode.\n\n", progname);
                               2108                 :              0 :     fprintf(stderr, "Usage:\n");
 1811                          2109                 :              0 :     fprintf(stderr, "  %s [OPTION] tests\n", progname);
 1809                          2110                 :              0 :     fprintf(stderr, "  %s [OPTION] TESTNAME [CONNINFO]\n", progname);
 1811                          2111                 :              0 :     fprintf(stderr, "\nOptions:\n");
                               2112                 :              0 :     fprintf(stderr, "  -t TRACEFILE       generate a libpq trace to TRACEFILE\n");
 1809                          2113                 :              0 :     fprintf(stderr, "  -r NUMROWS         use NUMROWS as the test size\n");
 1826                          2114                 :              0 : }
                               2115                 :                : 
                               2116                 :                : static void
 1826 alvherre@alvh.no-ip.     2117                 :CBC           1 : print_test_list(void)
                               2118                 :                : {
  734                          2119                 :              1 :     printf("cancel\n");
 1826                          2120                 :              1 :     printf("disallowed_in_pipeline\n");
                               2121                 :              1 :     printf("multi_pipelines\n");
 1720                          2122                 :              1 :     printf("nosync\n");
 1826                          2123                 :              1 :     printf("pipeline_abort\n");
 1349                          2124                 :              1 :     printf("pipeline_idle\n");
 1826                          2125                 :              1 :     printf("pipelined_insert\n");
                               2126                 :              1 :     printf("prepared\n");
  347 heikki.linnakangas@i     2127                 :              1 :     printf("protocol_version\n");
 1826 alvherre@alvh.no-ip.     2128                 :              1 :     printf("simple_pipeline\n");
                               2129                 :              1 :     printf("singlerow\n");
                               2130                 :              1 :     printf("transaction\n");
 1710                          2131                 :              1 :     printf("uniqviol\n");
 1826                          2132                 :              1 : }
                               2133                 :                : 
                               2134                 :                : int
                               2135                 :             15 : main(int argc, char **argv)
                               2136                 :                : {
                               2137                 :             15 :     const char *conninfo = "";
                               2138                 :                :     PGconn     *conn;
  193 tgl@sss.pgh.pa.us        2139                 :GNC          15 :     FILE       *trace = NULL;
                               2140                 :                :     char       *testname;
 1826 alvherre@alvh.no-ip.     2141                 :CBC          15 :     int         numrows = 10000;
                               2142                 :                :     PGresult   *res;
                               2143                 :                :     int         c;
                               2144                 :                : 
 1189 peter@eisentraut.org     2145         [ +  + ]:             52 :     while ((c = getopt(argc, argv, "r:t:")) != -1)
                               2146                 :                :     {
 1811 alvherre@alvh.no-ip.     2147      [ +  +  - ]:             22 :         switch (c)
                               2148                 :                :         {
 1809                          2149                 :             13 :             case 'r':           /* numrows */
                               2150                 :             13 :                 errno = 0;
                               2151                 :             13 :                 numrows = strtol(optarg, NULL, 10);
                               2152   [ +  -  -  + ]:             13 :                 if (errno != 0 || numrows <= 0)
                               2153                 :                :                 {
 1809 alvherre@alvh.no-ip.     2154                 :UBC           0 :                     fprintf(stderr, "couldn't parse \"%s\" as a positive integer\n",
                               2155                 :                :                             optarg);
                               2156                 :              0 :                     exit(1);
                               2157                 :                :                 }
 1809 alvherre@alvh.no-ip.     2158                 :CBC          13 :                 break;
 1189 peter@eisentraut.org     2159                 :              9 :             case 't':           /* trace file */
                               2160                 :              9 :                 tracefile = pg_strdup(optarg);
                               2161                 :              9 :                 break;
                               2162                 :                :         }
                               2163                 :                :     }
                               2164                 :                : 
 1811 alvherre@alvh.no-ip.     2165         [ +  - ]:             15 :     if (optind < argc)
                               2166                 :                :     {
 1809                          2167                 :             15 :         testname = pg_strdup(argv[optind]);
 1811                          2168                 :             15 :         optind++;
                               2169                 :                :     }
                               2170                 :                :     else
                               2171                 :                :     {
 1826 alvherre@alvh.no-ip.     2172                 :UBC           0 :         usage(argv[0]);
                               2173                 :              0 :         exit(1);
                               2174                 :                :     }
                               2175                 :                : 
 1811 alvherre@alvh.no-ip.     2176         [ +  + ]:CBC          15 :     if (strcmp(testname, "tests") == 0)
                               2177                 :                :     {
                               2178                 :              1 :         print_test_list();
                               2179                 :              1 :         exit(0);
                               2180                 :                :     }
                               2181                 :                : 
                               2182         [ +  - ]:             14 :     if (optind < argc)
                               2183                 :                :     {
 1809                          2184                 :             14 :         conninfo = pg_strdup(argv[optind]);
 1811                          2185                 :             14 :         optind++;
                               2186                 :                :     }
                               2187                 :                : 
                               2188                 :                :     /* Make a connection to the database */
 1826                          2189                 :             14 :     conn = PQconnectdb(conninfo);
                               2190         [ -  + ]:             14 :     if (PQstatus(conn) != CONNECTION_OK)
                               2191                 :                :     {
 1826 alvherre@alvh.no-ip.     2192                 :UBC           0 :         fprintf(stderr, "Connection to database failed: %s\n",
                               2193                 :                :                 PQerrorMessage(conn));
                               2194                 :              0 :         exit_nicely(conn);
                               2195                 :                :     }
                               2196                 :                : 
 1810 alvherre@alvh.no-ip.     2197                 :CBC          14 :     res = PQexec(conn, "SET lc_messages TO \"C\"");
                               2198         [ -  + ]:             14 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  667 peter@eisentraut.org     2199                 :UBC           0 :         pg_fatal("failed to set \"lc_messages\": %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us        2200                 :GNC          14 :     PQclear(res);
 1124 drowley@postgresql.o     2201                 :CBC          14 :     res = PQexec(conn, "SET debug_parallel_query = off");
 1810 alvherre@alvh.no-ip.     2202         [ -  + ]:             14 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  667 peter@eisentraut.org     2203                 :UBC           0 :         pg_fatal("failed to set \"debug_parallel_query\": %s", PQerrorMessage(conn));
  193 tgl@sss.pgh.pa.us        2204                 :GNC          14 :     PQclear(res);
                               2205                 :                : 
                               2206                 :                :     /* Set the trace file, if requested */
 1811 alvherre@alvh.no-ip.     2207         [ +  + ]:CBC          14 :     if (tracefile != NULL)
                               2208                 :                :     {
 1349                          2209         [ -  + ]:              9 :         if (strcmp(tracefile, "-") == 0)
 1349 alvherre@alvh.no-ip.     2210                 :UBC           0 :             trace = stdout;
                               2211                 :                :         else
 1349 alvherre@alvh.no-ip.     2212                 :CBC           9 :             trace = fopen(tracefile, "w");
 1811                          2213         [ -  + ]:              9 :         if (trace == NULL)
 1811 alvherre@alvh.no-ip.     2214                 :UBC           0 :             pg_fatal("could not open file \"%s\": %m", tracefile);
                               2215                 :                : 
                               2216                 :                :         /* Make it line-buffered */
 1809 alvherre@alvh.no-ip.     2217                 :CBC           9 :         setvbuf(trace, NULL, PG_IOLBF, 0);
                               2218                 :                : 
 1811                          2219                 :              9 :         PQtrace(conn, trace);
 1739 noah@leadboat.com        2220                 :              9 :         PQsetTraceFlags(conn,
                               2221                 :                :                         PQTRACE_SUPPRESS_TIMESTAMPS | PQTRACE_REGRESS_MODE);
                               2222                 :                :     }
                               2223                 :                : 
  734 alvherre@alvh.no-ip.     2224         [ +  + ]:             14 :     if (strcmp(testname, "cancel") == 0)
                               2225                 :              2 :         test_cancel(conn);
                               2226         [ +  + ]:             12 :     else if (strcmp(testname, "disallowed_in_pipeline") == 0)
 1826                          2227                 :              1 :         test_disallowed_in_pipeline(conn);
 1811                          2228         [ +  + ]:             11 :     else if (strcmp(testname, "multi_pipelines") == 0)
 1826                          2229                 :              1 :         test_multi_pipelines(conn);
 1720                          2230         [ +  + ]:             10 :     else if (strcmp(testname, "nosync") == 0)
                               2231                 :              1 :         test_nosync(conn);
 1811                          2232         [ +  + ]:              9 :     else if (strcmp(testname, "pipeline_abort") == 0)
 1826                          2233                 :              1 :         test_pipeline_abort(conn);
 1349                          2234         [ +  + ]:              8 :     else if (strcmp(testname, "pipeline_idle") == 0)
                               2235                 :              1 :         test_pipeline_idle(conn);
 1811                          2236         [ +  + ]:              7 :     else if (strcmp(testname, "pipelined_insert") == 0)
 1826                          2237                 :              1 :         test_pipelined_insert(conn, numrows);
 1811                          2238         [ +  + ]:              6 :     else if (strcmp(testname, "prepared") == 0)
 1826                          2239                 :              1 :         test_prepared(conn);
  347 heikki.linnakangas@i     2240         [ +  + ]:              5 :     else if (strcmp(testname, "protocol_version") == 0)
                               2241                 :              1 :         test_protocol_version(conn);
 1811 alvherre@alvh.no-ip.     2242         [ +  + ]:              4 :     else if (strcmp(testname, "simple_pipeline") == 0)
 1826                          2243                 :              1 :         test_simple_pipeline(conn);
 1811                          2244         [ +  + ]:              3 :     else if (strcmp(testname, "singlerow") == 0)
 1826                          2245                 :              1 :         test_singlerowmode(conn);
 1811                          2246         [ +  + ]:              2 :     else if (strcmp(testname, "transaction") == 0)
 1826                          2247                 :              1 :         test_transaction(conn);
 1710                          2248         [ +  - ]:              1 :     else if (strcmp(testname, "uniqviol") == 0)
                               2249                 :              1 :         test_uniqviol(conn);
                               2250                 :                :     else
                               2251                 :                :     {
 1811 alvherre@alvh.no-ip.     2252                 :UBC           0 :         fprintf(stderr, "\"%s\" is not a recognized test name\n", testname);
 1826                          2253                 :              0 :         exit(1);
                               2254                 :                :     }
                               2255                 :                : 
                               2256                 :                :     /* close the connection to the database and cleanup */
 1826 alvherre@alvh.no-ip.     2257                 :CBC          14 :     PQfinish(conn);
                               2258                 :                : 
  193 tgl@sss.pgh.pa.us        2259   [ +  +  +  - ]:GNC          14 :     if (trace && trace != stdout)
                               2260                 :              9 :         fclose(trace);
                               2261                 :                : 
 1826 alvherre@alvh.no-ip.     2262                 :CBC          14 :     return 0;
                               2263                 :                : }
        

Generated by: LCOV version 2.4-beta