From 60d2a2b4f3a386c885bfbbee840dcb388b98a5f6 Mon Sep 17 00:00:00 2001
From: Craig Ringer <craig@2ndquadrant.com>
Date: Thu, 19 Jun 2014 11:38:35 +0800
Subject: [PATCH] Fix FE/BE protocol loss of sync due to invalid empty query

The V3 protocol specifies that the message format for Query is:

    'Q'
    [32 bit integer]
    [query string]

but psqlODBC was sending "Q \0", causing the server to wait for the second half
of the length word forever. At connection close, the server would emit:

    unexpected EOF within message length word

This issue was observed while tracking down problems with XA transactions in
MSDTC failing to commit after a successful prepare because an isolated tx
created via getLockedXAConn() got stuck here, but is not confined to that
problem.
---
 connection.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/connection.c b/connection.c
index 52f5463..f33a6a4 100644
--- a/connection.c
+++ b/connection.c
@@ -3081,7 +3081,22 @@ inolog("Discarded the first SAVEPOINT\n");
 
 					if (empty_reqs == 0)
 					{
-						SOCK_put_string(self->sock, "Q ");
+						/*
+						* Send an empty query to elicit an 'I' (EmptyQueryResponse)
+						 * message from the server. This gives us a synchronisation point
+						 * with the server, though it isn't clear why the code doesn't just
+						 * use 'S' (Sync).
+						 */
+						SOCK_put_char(self->sock, 'Q');
+						if (PROTOCOL_74(ci))
+						{
+							SOCK_put_int(self->sock, 5, 4);
+							SOCK_put_char(self->sock, '\0');
+						}
+						else
+						{
+							SOCK_put_string(self->sock, " ");
+						}
 						SOCK_flush_output(self->sock);
 						empty_reqs++;
 					}
-- 
1.9.0

