Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z7AX7-0002JV-IA for pgsql-bugs@arkaria.postgresql.org; Mon, 22 Jun 2015 22:49:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Z7AX6-0007v4-SE for pgsql-bugs@arkaria.postgresql.org; Mon, 22 Jun 2015 22:49:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z78Qs-0007Te-Pl for pgsql-bugs@postgresql.org; Mon, 22 Jun 2015 20:35:06 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1Z78Qp-0002vG-Bs for pgsql-bugs@postgresql.org; Mon, 22 Jun 2015 20:35:05 +0000 MIME-Version: 1.0 Subject: BUG #13462: Impossible to use COPY FORMAT BINARY in chunks through libpq To: pgsql-bugs@postgresql.org From: socketpair@gmail.com Date: Mon, 22 Jun 2015 20:34:11 +0000 Message-ID: <20150622203411.3873.26064@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.3 (/) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 13462 Logged by: Korenberg Mark Email address: socketpair@gmail.com PostgreSQL version: 9.3.8 Operating system: Linux Description: If I use PQputCopyData() to pass parts of the binary protocol (as documented in http://www.postgresql.org/docs/9.3/static/sql-copy.html ), postgres binary socket data gets corrupted (i.e. protocol is broken). It is unexpected that libpq inserts \d\0\0\0 on every PQputCopyData() call even in BINARY COPY mode. So, it is impossible to write a program that generate tuples in binary form dynamically without merging them into one big buffer. Part of program: ==================================== static const struct { char magic[11]; int32_t zero1; int32_t zero2; } __attribute__ ((packed)) bin_header = {"PGCOPY\n\377\r\n",0,0}; if (PQputCopyData (conn, (const char *) &bin_header, sizeof (bin_header)) != 1) { fprintf (stderr, "PQputCopyData failed\n"); return 4; } struct { int16_t fields; int32_t ip1len; uint32_t ip1; int32_t ip2len; uint32_t ip2; } __attribute__ ((packed)) binary_line = { 2, 4, 0x01020304, 4, 0x05060708 }; for (i = 0; i < repeats; i++) { if (PQputCopyData (conn, (const char *) &binary_line, sizeof (binary_line)) != 1) { fprintf (stderr, "PQputCopyData failed\n"); return 4; } } static const int16_t copy_end = -1; if (PQputCopyData (conn, (const char *) ©_end, sizeof (copy_end)) != 1) { fprintf (stderr, "PQputCopyData failed\n"); return 4; } res = PQgetResult (conn); if (PQresultStatus (res) != PGRES_COMMAND_OK) { fprintf (stderr, "COPY failed: %s\n", PQerrorMessage (conn)); PQclear (res); return 3; } PQclear (res); =============================================== will generate: "d\0\0\0\27PGCOPY\n\377\r\n\0\0\0\0\0\0\0\0\0d\0\0\0\26\2\0\4\0\0\0\4\3\2\1\4\0\0\0\10\7\6\5d\0\0\0\6\377\377X\0\0\0\4" but actually should generate (not sure about tail) "d\0\0\0\27PGCOPY\n\377\r\n\0\0\0\0\0\0\0\0\0\2\0\4\0\0\0\4\3\2\1\4\0\0\0\10\7\6\5\377\377X\0\0\0\4" -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs