pg.ddx.io pgsql-odbc@postgresql.org mailing list archive
help / color / mirror / Atom feedCrash when using 'Use Declare/Fetch' and the result set does contain one row only
6+ messages / 4 participants
[nested] [flat]
* Crash when using 'Use Declare/Fetch' and the result set does contain one row only
@ 2014-06-23 11:58 Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
0 siblings, 1 reply; 6+ messages in thread
From: Jan-Peter Seifert @ 2014-06-23 11:58 UTC (permalink / raw)
To: pgsql-odbc
Hello,
Recently we switched from using plain SQL for transaction handling to the corresponding ODBC functions. Now the application crashes if 'Use Declare/Fetch' is checked in the ODBC DSN. Not everytime but when the result set does contain one row only. The course of action leading to the crash is:
1. begin transaction:
SQLSetConnectAttr(Connection, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, SQL_IS_UINTEGER)
2. declare a cursor for a select that does yield one row/value only:
SQLAllocHandle(SQL_HANDLE_STMT, ...)
SQLSetStmtAttr(Statement, SQL_ATTR_CURSOR_TYPE, SQL_CURSOR_STATIC, SQL_IS_UINTEGER)
SQLExecDirect(Statement, "SELECT col FROM tab WHERE <primary_key = value>")
SQLDescribeCol(...)
SQLBindCol(...)
3. commit transaction:
SQLEndTran(Connection, SQL_COMMIT)
SQLSetConnectAttr(Connection, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON, SQL_IS_UINTEGER)
4. move the cursor to ( or fetch ) the first result row:
SQLExtendedFetch(Statement, SQL_FETCH_FIRST, ...)
There's no crash if step 4 happens before step 3 or when using SQL commands for transaction handling.
As I understand cursors have to be declared 'with hold' to be used outside the transaction they've been declared in. However, it seems that there's no problem if the result set does contain more than one row. This yields the first result row and subsequent SQLExtendedFetch(...) calls work. Alternatively using plain SQL for transaction handling works, too.
At least psqlODBC 09.03.0210 and 09.03.0300 ( both 32 bit ) seem to be affected - tested against PostgreSQL 9.2.8 64 bit ( Ubuntu and Windows ).
Further tests using the psqlodbc 09.03.0300 unicode version compiled from source and MDAC 2.80 using 'ODBCTest (Unicode, x86)' using the same ODBC function calls showed a different behavior between debug and release version. The debug version simply crashes and the release version returns no data if the result set contains one row only.
Best regards,
Peter
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Crash when using 'Use Declare/Fetch' and the result set does contain one row only
@ 2014-06-23 12:15 Craig Ringer <craig@2ndquadrant.com>
parent: Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
0 siblings, 1 reply; 6+ messages in thread
From: Craig Ringer @ 2014-06-23 12:15 UTC (permalink / raw)
To: Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>; pgsql-odbc
On 06/23/2014 07:58 PM, Jan-Peter Seifert wrote:
> Hello,
>
> Recently we switched from using plain SQL for transaction handling to the corresponding ODBC functions. Now the application crashes if 'Use Declare/Fetch' is checked in the ODBC DSN. Not everytime but when the result set does contain one row only. The course of action leading to the crash is:
>
Any chance of a self-contained, compileable example that demonstrates this?
--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Crash when using 'Use Declare/Fetch' and the result set does contain one row only
@ 2014-06-23 12:22 Michael Paquier <michael.paquier@gmail.com>
parent: Craig Ringer <craig@2ndquadrant.com>
0 siblings, 1 reply; 6+ messages in thread
From: Michael Paquier @ 2014-06-23 12:22 UTC (permalink / raw)
To: Craig Ringer <craig@2ndquadrant.com>; +Cc: Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>; pgsql-odbc
On Mon, Jun 23, 2014 at 9:15 PM, Craig Ringer <craig@2ndquadrant.com> wrote:
> On 06/23/2014 07:58 PM, Jan-Peter Seifert wrote:
> > Hello,
> >
> > Recently we switched from using plain SQL for transaction handling to
> the corresponding ODBC functions. Now the application crashes if 'Use
> Declare/Fetch' is checked in the ODBC DSN. Not everytime but when the
> result set does contain one row only. The course of action leading to the
> crash is:
> >
>
> Any chance of a self-contained, compileable example that demonstrates this?
>
If possible, something integrated within the regression test infrastructure
in test/ would be nice. A backtrace would be a good addition as well.
--
Michael
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Crash when using 'Use Declare/Fetch' and the result set does contain one row only
@ 2014-06-24 09:06 Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Jan-Peter Seifert @ 2014-06-24 09:06 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Craig Ringer <craig@2ndquadrant.com>; pgsql-odbc
Hello,
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.
Thank you very much in advance!
Best regards,
Peter Seifert
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
Attachments:
TestODBC.zip (application/zip, 5.3K) download
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Crash when using 'Use Declare/Fetch' and the result set does contain one row only
@ 2014-06-24 13:43 Heikki Linnakangas <hlinnakangas@vmware.com>
parent: Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
0 siblings, 1 reply; 6+ messages in thread
From: Heikki Linnakangas @ 2014-06-24 13:43 UTC (permalink / raw)
To: Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>; Michael Paquier <michael.paquier@gmail.com>; +Cc: Craig Ringer <craig@2ndquadrant.com>; pgsql-odbc
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
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
Attachments:
fix-cursor-commit-1.patch (text/x-diff, 3.7K) download
commit 78bf5f8933646595eaff20667e42dc18f8700465
Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
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 <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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 \
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Crash when using 'Use Declare/Fetch' and the result set does contain one row only
@ 2014-06-25 14:13 Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
parent: Heikki Linnakangas <hlinnakangas@vmware.com>
0 siblings, 0 replies; 6+ messages in thread
From: Jan-Peter Seifert @ 2014-06-25 14:13 UTC (permalink / raw)
To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Craig Ringer <craig@2ndquadrant.com>; pgsql-odbc
> Gesendet: Dienstag, 24. Juni 2014 um 15:43 Uhr
> Von: "Heikki Linnakangas" <hlinnakangas@vmware.com>
> An: "Jan-Peter Seifert" <Jan-Peter.Seifert@gmx.de>, "Michael Paquier" <michael.paquier@gmail.com>
> Cc: "Craig Ringer" <craig@2ndquadrant.com>, "PostgreSQL mailing lists" <pgsql-odbc@postgresql.org>
> Betreff: Re: [ODBC] Crash when using 'Use Declare/Fetch' and the result set does contain one row only
> I committed the attached patch to fix this.
We did some tests using a version compiled from the current Git master and it worked well for us.
Thank you very much!
Best regards,
Peter
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2014-06-25 14:13 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-06-23 11:58 Crash when using 'Use Declare/Fetch' and the result set does contain one row only Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
2014-06-23 12:15 ` Craig Ringer <craig@2ndquadrant.com>
2014-06-23 12:22 ` Michael Paquier <michael.paquier@gmail.com>
2014-06-24 09:06 ` Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
2014-06-24 13:43 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2014-06-25 14:13 ` Jan-Peter Seifert <Jan-Peter.Seifert@gmx.de>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox