Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WzR14-0004gR-Fl for pgsql-odbc@arkaria.postgresql.org; Tue, 24 Jun 2014 13:44:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WzR13-0001Ys-NM for pgsql-odbc@arkaria.postgresql.org; Tue, 24 Jun 2014 13:44:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WzR13-0001Ym-2g for pgsql-odbc@postgresql.org; Tue, 24 Jun 2014 13:44:05 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1WzR0w-0006VG-IQ for pgsql-odbc@postgresql.org; Tue, 24 Jun 2014 13:44:02 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 718132874B; Tue, 24 Jun 2014 06:43:53 -0700 (PDT) Received: from EX13-CAS-002.vmware.com (EX13-CAS-002.vmware.com [10.113.191.52]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 6B0EF41370; Tue, 24 Jun 2014 06:43:53 -0700 (PDT) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-010.vmware.com (10.113.191.30) with Microsoft SMTP Server (TLS) id 15.0.775.38; Tue, 24 Jun 2014 06:43:40 -0700 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.775.38; Tue, 24 Jun 2014 06:43:39 -0700 Message-ID: <53A98088.3020301@vmware.com> Date: Tue, 24 Jun 2014 16:43:36 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.5.0 MIME-Version: 1.0 To: Jan-Peter Seifert , Michael Paquier CC: Craig Ringer , PostgreSQL mailing lists Subject: Re: Crash when using 'Use Declare/Fetch' and the result set does contain one row only References: <53A81A78.6030409@2ndquadrant.com>, In-Reply-To: Content-Type: multipart/mixed; boundary="------------040809080803000001020106" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org --------------040809080803000001020106 Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit On 06/24/2014 12:06 PM, Jan-Peter Seifert wrote: > I've attached a zip file containing a Visual Studio 2010 project > tested on a 'PostgreSQL Unicode' system-DSN named TestODBC and a dump > of a test database named TestODBC. The line with the statement that > returns one row is commented out. The other returns 5 rows in the > release version - it skips the first line and returns the last row > twice though. When using the debugger it crashes on the last row. I committed the attached patch to fix this. I admit I don't understand very well how the cursor and cached result set row counting is supposed to work. But I think this is correct, so I'll commit it unless someone sees a problem with it. This patch also includes a regression test for this. I created it using your test case as reference; it's essentially the same test, but using the common regression test facilities. (You'll have to set UseDeclareFetch=1 in odbc.ini for it to exhibit the bug) Thanks for the report! - Heikki --------------040809080803000001020106 Content-Type: text/x-diff; name="fix-cursor-commit-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-cursor-commit-1.patch" commit 78bf5f8933646595eaff20667e42dc18f8700465 Author: Heikki Linnakangas Date: Tue Jun 24 16:38:00 2014 +0300 Fix bug with UseDeclareFetch=1 when a transaction is committed before fetch. If a server cursor is closed before the application has fetched any rows from the result set, the "base" of the result set's cached rowset was off-by-one. Also add a regression test for the same. This fixes the bug reported by Jan-Peter Seifert. diff --git a/qresult.c b/qresult.c index 1e793c5..a1c8bf7 100644 --- a/qresult.c +++ b/qresult.c @@ -700,7 +700,6 @@ void QR_on_close_cursor(QResultClass *self) { QR_set_cursor(self, NULL); - QR_set_has_valid_base(self); } /* diff --git a/test/expected/cursor-commit.out b/test/expected/cursor-commit.out new file mode 100644 index 0000000..5c2a124 --- /dev/null +++ b/test/expected/cursor-commit.out @@ -0,0 +1,6 @@ +\! "./src/cursor-commit-test" +connected +first row: 1 +row 2: 2 +row 3: 3 +disconnecting diff --git a/test/src/cursor-commit-test.c b/test/src/cursor-commit-test.c new file mode 100644 index 0000000..169974d --- /dev/null +++ b/test/src/cursor-commit-test.c @@ -0,0 +1,87 @@ +/* + * This test case tests for a bug in result set caching, with + * UseDeclareFetch=1, that was fixed. The bug occurred when a cursor was + * closed, due to transaction commit, before any rows were fetched from + * it. That set the "base" of the internal cached rowset incorrectly, + * off by one. + */ + +#include +#include +#include + +#include "common.h" + +int main(int argc, char **argv) +{ + int rc; + HSTMT hstmt = SQL_NULL_HSTMT; + SQLCHAR charval[100]; + SQLLEN len; + int row; + + test_connect(); + + /* Start a transaction */ + rc = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, SQL_IS_UINTEGER); + + rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); + exit(1); + } + + rc = SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE, + (SQLPOINTER) SQL_CURSOR_STATIC, SQL_IS_UINTEGER); + CHECK_STMT_RESULT(rc, "SQLSetStmtAttr failed", hstmt); + + /* + * Begin executing a query + */ + rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT g FROM generate_series(1,3) g", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + + rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, &charval, sizeof(charval), &len); + CHECK_STMT_RESULT(rc, "SQLBindCol failed", hstmt); + + /* Commit. This implicitly closes the cursor in the server. */ + rc = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_COMMIT); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to commit", SQL_HANDLE_DBC, conn); + exit(1); + } + + rc = SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 0); + CHECK_STMT_RESULT(rc, "SQLFetchScroll(FIRST) failed", hstmt); + + if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) + printf("first row: %s\n", charval); + + row = 1; + while (1) + { + rc = SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 0); + if (rc == SQL_NO_DATA) + break; + + row++; + + if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) + printf("row %d: %s\n", row, charval); + else + { + print_diag("SQLFetchScroll failed", SQL_HANDLE_STMT, hstmt); + exit(1); + } + } + + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); + + /* Clean up */ + test_disconnect(); + + return 0; +} diff --git a/test/tests b/test/tests index 7449935..1d72d56 100644 --- a/test/tests +++ b/test/tests @@ -24,6 +24,7 @@ TESTBINS = src/connect-test \ src/alter-test \ src/quotes-test \ src/cursors-test \ + src/cursor-commit-test \ src/positioned-update-test \ src/catalogfunctions-test \ src/bindcol-test \ --------------040809080803000001020106 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------040809080803000001020106--