From: Hiroshi Inoue <inoue@tpf.co.jp>
To: lev.bukovski@teliasonera.com
To: pgsql-odbc@postgresql.org
Subject: Re: Prepared statement error with UseServerSidePrepare=1
Date: Mon, 02 Jun 2014 12:59:28 +0900
Message-ID: <538BF6A0.1020404@tpf.co.jp> (raw)
In-Reply-To: <619ABD714EEDD84E8E582E993559DB36142ADA8C@EXMB12TSTRZ2.tcad.telia.se>
References: <619ABD714EEDD84E8E582E993559DB36142ADA8C@EXMB12TSTRZ2.tcad.telia.se>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>
Hi Lev,
Could you please try the attahced patch?
regards,
Hiroshi Inoue
(2014/05/28 17:52), lev.bukovski@teliasonera.com wrote:
> Hi,
>
> I found some strangeness in psqlodbc’s behavior. May be you can explain
> to me, do I do something wrong or there is a bug.
>
> Tested on psqlodbc 9.3.3 and postgresql 9.3.4.
>
> I have department tree table named KEY11 with 2 indexes on that:
>
> CREATE TABLE Key11 (
>
> ID INTEGER not null,
>
> Disp VARCHAR (64) not null,
>
> Search VARCHAR (64) not null,
>
> CustomerID INTEGER not null,
>
> ParentID INTEGER not null,
>
> primary KEY (Search,Disp,CustomerID,ParentID)
>
> );
>
> create unique index key11_idx on Key11 (ID);
>
> create unique index key11_disp_idx on Key11(Disp,CustomerID,ParentID);
>
> So I can’t insert same named department for same customer and on same
> organization level (ParentID).
>
> I do login to the database and prepare all SQL statements only once.
> Afterward prepared SQL statements are reused with different parameters.
>
> So I do insert new department to KEY11 table:
>
> SQLPrepare - INSERT INTO KEY11 (ID, DISP, SEARCH, CUSTOMERID, PARENTID)
> VALUES (?,?,?,?,?)
>
> SQLExecute with values for example (6022, ‘New department’, ‘NEW
> DEPARTMENT’, 2, 6021)
>
> Everything fine.
>
> Now I make another insert with same parameters:
>
> SQLExecute with values for example (6023, ‘New department’, ‘NEW
> DEPARTMENT’, 2, 6021)
>
> Fine, I get an error: duplicate key value violates unique constraint
> "key11_disp_idx"
>
> Now I rename department 6022 “New Department” -> “New Department 2”:
>
> SQLPrepare - UPDATE KEY11 SET DISP = ?, SEARCH = ? WHERE ID = ? AND
> CUSTOMERID = ?
>
> SQLExecute with values for example (‘New department 2’, ‘NEW
> DEPARTMENT’, 6022, 2)
>
> Fine again. Department’s name is renamed, so I can try to insert new
> department again. Error while executing the query
>
> Third insert with new department in parameters:
>
> SQLExecute with values for example (6024, ‘New department’, ‘NEW
> DEPARTMENT’, 2, 6021)
>
> Still an error, but only: “Error while executing the query” ?
>
> But if I query now the KEY11 table, I see that New department was added
> despite of error:
>
> select * from key11 where parentid = 6021;
>
> id | disp | search |
> customerid | parentid
>
> ------+-----------------------------+-----------------------------+------------+----------
>
> 6022 | New department 2 | NEW DEPARTMENT 2
> | 2 | 6021
>
> 6024 | New department | NEW DEPARTMENT
> | 2 | 6021
>
> (3 rows)
>
> I noticed the behavior on psqlodbc 9.2.1. In that version third insert
> returned same error, but didn’t really insert the data.
>
> If I disable UseServerSidePrepare=0, then third insert executes
> successfully.
>
> Thanks in advance,
>
> Lev Bukovski
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbcAttachments:prepared_statement_error.patch (text/x-patch, 4.9K) downloaddiff --git a/execute.c b/execute.cindex 1cc4f04..8dcc3ef 100644--- a/execute.c+++ b/execute.c@@ -988,16 +988,13 @@ PGAPI_Execute(HSTMT hstmt, UWORD flag)
else if (PREPARED_PERMANENTLY == stmt->prepared ||
PREPARED_TEMPORARILY == stmt->prepared)
{
- QResultClass *res;-
/*
* re-executing an prepared statement.
* Don't recycle the statement but
* discard the old result.
*/
recycle = FALSE;
- if (res = SC_get_Result(stmt), res)- QR_close_result(res, FALSE);+ SC_reset_result_for_rerun(stmt);
}
/*
* If SQLExecute is being called again, recycle the statement. Note
diff --git a/odbcapi.c b/odbcapi.cindex b5b5aac..bfbbae1 100644--- a/odbcapi.c+++ b/odbcapi.c@@ -386,6 +386,7 @@ SQLExecute(HSTMT StatementHandle)
else
{
StartRollbackState(stmt);
+ stmt->exec_current_row = -1;
ret = PGAPI_Execute(StatementHandle, flag);
ret = DiscardStatementSvp(stmt, ret, FALSE);
}
diff --git a/qresult.c b/qresult.cindex 361be56..1060bfa 100644--- a/qresult.c+++ b/qresult.c@@ -196,7 +196,6 @@ QR_Constructor(void)
rv->notice = NULL;
rv->conn = NULL;
rv->next = NULL;
- rv->pstatus = 0;
rv->count_backend_allocated = 0;
rv->count_keyset_allocated = 0;
rv->num_total_read = 0;
@@ -320,6 +319,25 @@ QR_close_result(QResultClass *self, BOOL destroy)
}
void
+QR_reset_for_re_execute(QResultClass *self)+{+ mylog("QResult: enter %s for %x\n", __FUNCTION__, self);+ if (!self) return;+ QR_close_result(self, FALSE);+ /* reset flags etc */+ self->flags = 0;+ QR_set_rowstart_in_cache(self, -1);+ self->recent_processed_row_count = -1;+ /* clear error info etc */+ self->rstatus = PORES_EMPTY_QUERY;+ self->aborted = FALSE;+ self->sqlstate[0] = '\0';+ self->messageref = NULL;++ mylog("QResult: exit %s\n", __FUNCTION__);+}++void
QR_Destructor(QResultClass *self)
{
mylog("QResult: enter DESTRUCTOR\n");
@@ -464,6 +482,7 @@ QR_free_memory(QResultClass *self)
self->count_backend_allocated = 0;
self->backend_tuples = NULL;
self->dataFilled = FALSE;
+ self->tupleField = NULL;
}
if (self->keyset)
{
diff --git a/qresult.h b/qresult.hindex dfae6d8..8a9315e 100644--- a/qresult.h+++ b/qresult.h@@ -226,6 +226,7 @@ int QR_next_tuple(QResultClass *self, StatementClass *, int *LastMessageType);
int QR_close(QResultClass *self);
void QR_on_close_cursor(QResultClass *self);
void QR_close_result(QResultClass *self, BOOL destroy);
+void QR_reset_for_re_execute(QResultClass *self);
char QR_fetch_tuples(QResultClass *self, ConnectionClass *conn, const char *cursor, int *LastMessageType);
void QR_free_memory(QResultClass *self);
void QR_set_command(QResultClass *self, const char *msg);
diff --git a/statement.c b/statement.cindex 0aa31ce..d6bf81a 100644--- a/statement.c+++ b/statement.c@@ -805,6 +805,27 @@ SC_initialize_and_recycle(StatementClass *self)
return SQL_SUCCESS;
}
+void+SC_reset_result_for_rerun(StatementClass *self)+{+ QResultClass *res;+ ColumnInfoClass *flds;++ if (!self) return;+ if (res = SC_get_Result(self), NULL == res)+ return;+ flds = QR_get_fields(res);+ if (NULL == flds ||+ 0 == CI_get_num_fields(flds))+ SC_set_Result(self, NULL);+ else+ {+ QR_reset_for_re_execute(res);+ self->curr_param_result = 1;+ SC_set_Curres(self, NULL);+ }+}+
/*
* Called from SQLPrepare if STMT_PREMATURE, or
* from SQLExecute if STMT_FINISHED, or
@@ -880,11 +901,10 @@ inolog("SC_clear_parse_status\n");
{
case PREPARED_PERMANENTLY:
case PREPARED_TEMPORARILY:
- QR_close_result(res, FALSE);+ SC_reset_result_for_rerun(self);
break;
default:
- QR_Destructor(res);- SC_init_Result(self);+ SC_set_Result(self, NULL);
break;
}
}
@@ -2533,7 +2553,8 @@ ReflectColumnsInfo(StatementClass *self, QResultClass *res)
if (res->num_fields > 0)
return FALSE;
pres = SC_get_Result(self);
- if (pres != res &&+ if (pres != NULL &&+ pres != res &&
pres->num_fields > 0)
{
QR_set_fields(res, QR_get_fields(pres));
@@ -2627,6 +2648,8 @@ inolog(" response_length=%d\n", response_length);
res->recent_processed_row_count = ret1;
}
}
+ else if (QR_command_successful(res))+ QR_set_rstatus(res, PORES_COMMAND_OK);
break;
case 'E': /* ErrorMessage */
handle_error_message(conn, msgbuffer, sizeof(msgbuffer), res->sqlstate, comment, res);
diff --git a/statement.h b/statement.hindex 6a138be..25f8c32 100644--- a/statement.h+++ b/statement.h@@ -466,6 +466,7 @@ char SC_Destructor(StatementClass *self);
BOOL SC_opencheck(StatementClass *self, const char *func);
RETCODE SC_initialize_and_recycle(StatementClass *self);
void SC_initialize_cols_info(StatementClass *self, BOOL DCdestroy, BOOL parseReset);
+void SC_reset_result_for_rerun(StatementClass *self);
int statement_type(const char *statement);
char parse_statement(StatementClass *stmt, BOOL);
char parse_sqlsvr(StatementClass *stmt);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: inoue@tpf.co.jp, lev.bukovski@teliasonera.com
Subject: Re: Prepared statement error with UseServerSidePrepare=1
In-Reply-To: <538BF6A0.1020404@tpf.co.jp>
References: <619ABD714EEDD84E8E582E993559DB36142ADA8C@EXMB12TSTRZ2.tcad.telia.se> <538BF6A0.1020404@tpf.co.jp>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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