pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Prepared statement error with UseServerSidePrepare=1
6+ messages / 3 participants
[nested] [flat]

* Prepared statement error with UseServerSidePrepare=1
@ 2014-05-28 08:52  lev.bukovski@teliasonera.com
  0 siblings, 2 replies; 6+ messages in thread

From: lev.bukovski@teliasonera.com @ 2014-05-28 08:52 UTC (permalink / raw)
  To: pgsql-odbc

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-odbc

Attachments:

  mylog_merex28757.log (application/octet-stream, 211.4K) download

  psqlodbc_merex28757.log (application/octet-stream, 7.1K) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Prepared statement error with UseServerSidePrepare=1
@ 2014-05-28 22:17  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: lev.bukovski@teliasonera.com
  1 sibling, 0 replies; 6+ messages in thread

From: Adrian Klaver @ 2014-05-28 22:17 UTC (permalink / raw)
  To: lev.bukovski@teliasonera.com; pgsql-odbc

On 05/28/2014 01:52 AM, 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 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.

I would tail the Postgres log and see what it says when you go through 
the sequence. My suspicion is that it is a transaction rollback not 
being handled.

>
> Thanks in advance,
>
> Lev Bukovski
>
>
>
>


-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
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: Prepared statement error with UseServerSidePrepare=1
@ 2014-06-02 03:59  Hiroshi Inoue <inoue@tpf.co.jp>
  parent: lev.bukovski@teliasonera.com
  1 sibling, 1 reply; 6+ messages in thread

From: Hiroshi Inoue @ 2014-06-02 03:59 UTC (permalink / raw)
  To: lev.bukovski@teliasonera.com; pgsql-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-odbc

Attachments:

prepared_statement_error.patch (text/x-patch, 4.9K) download diff --git a/execute.c b/execute.c index 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.c index 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.c index 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.h index 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.c index 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.h index 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);
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: Prepared statement error with UseServerSidePrepare=1
@ 2014-06-02 13:11  lev.bukovski@teliasonera.com
  parent: Hiroshi Inoue <inoue@tpf.co.jp>
  0 siblings, 1 reply; 6+ messages in thread

From: lev.bukovski@teliasonera.com @ 2014-06-02 13:11 UTC (permalink / raw)
  To: inoue@tpf.co.jp; pgsql-odbc

Hi Hiroshi,

I applied your patch on psqlodbc 9.3.3 (NOT on the head branch).
Now I can't even get to insert operations, because I get error while application configuration fetch.

I have this CONFIG table, from where I load configuration parameters (SELECT VALUE2, VALUE3 FROM CONFIG WHERE VALUE1 = ?)
Each execute of the SQL returns one row. So the first execute success, but second returns with 'Null statement result in PGAPI_ExtendedFetch' error.

psqlodbc and mylog files are in attechaments.

Lev Bukovski

-----Original Message-----
From: Hiroshi Inoue [mailto:inoue@tpf.co.jp] 
Sent: 2. kesäkuuta 2014 6:59
To: Bukovski, Lev /External; pgsql-odbc@postgresql.org
Subject: Re: [ODBC] Prepared statement error with UseServerSidePrepare=1

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-odbc

Attachments:

  mylog_merex27648.log (application/octet-stream, 13.5K) download

  psqlodbc_merex27648.log (application/octet-stream, 3.2K) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Prepared statement error with UseServerSidePrepare=1
@ 2014-06-02 15:16  Hiroshi Inoue <inoue@tpf.co.jp>
  parent: lev.bukovski@teliasonera.com
  0 siblings, 1 reply; 6+ messages in thread

From: Hiroshi Inoue @ 2014-06-02 15:16 UTC (permalink / raw)
  To: lev.bukovski@teliasonera.com; pgsql-odbc

(2014/06/02 22:11), lev.bukovski@teliasonera.com wrote:
> Hi Hiroshi,
>
> I applied your patch on psqlodbc 9.3.3 (NOT on the head branch).
> Now I can't even get to insert operations, because I get error while application configuration fetch.
>
> I have this CONFIG table, from where I load configuration parameters (SELECT VALUE2, VALUE3 FROM CONFIG WHERE VALUE1 = ?)
> Each execute of the SQL returns one row. So the first execute success, but second returns with 'Null statement result in PGAPI_ExtendedFetch' error.
>
> psqlodbc and mylog files are in attechaments.

Thanks for the report.
Please apply the attached patch also.

It's the same as the one attached in the thread
  [ODBC] Problems using SQLFetch on prepared INSERT with RETURNING clause
.

regards,
Hiroshi Inoue

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

insertreturning_twice.patch (text/x-patch, 554B) download diff --git a/statement.c b/statement.c index 0aa31ce..4bbf132 100644 --- a/statement.c +++ b/statement.c @@ -317,6 +317,7 @@ PGAPI_FreeStmt(HSTMT hstmt, { return SQL_ERROR; } + SC_set_Curres(stmt, NULL); } else if (fOption == SQL_RESET_PARAMS) SC_free_params(stmt, STMT_FREE_PARAMS_ALL); @@ -2260,6 +2261,8 @@ inolog("res->next=%p\n", tres); last->next = res; self->curr_param_result = 1; } + if (NULL == SC_get_Curres(self)) + SC_set_Curres(self, SC_get_Result(self)); ipdopts = SC_get_IPDF(self); has_out_para = FALSE;
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: Prepared statement error with UseServerSidePrepare=1
@ 2014-06-03 07:44  lev.bukovski@teliasonera.com
  parent: Hiroshi Inoue <inoue@tpf.co.jp>
  0 siblings, 0 replies; 6+ messages in thread

From: lev.bukovski@teliasonera.com @ 2014-06-03 07:44 UTC (permalink / raw)
  To: inoue@tpf.co.jp; pgsql-odbc

Hi,
The patches together are working. All problems I noticed in psqlodbc 9.3.3 are gone. Thank you very much.
Now, I guess, I have to test my application all over again.
When can I expect a new release of the psqlodbc driver?

Lev Bukovski

-----Original Message-----
From: Hiroshi Inoue [mailto:inoue@tpf.co.jp] 
Sent: 2. kesäkuuta 2014 18:17
To: Bukovski, Lev /External; pgsql-odbc@postgresql.org
Subject: Re: [ODBC] Prepared statement error with UseServerSidePrepare=1

(2014/06/02 22:11), lev.bukovski@teliasonera.com wrote:
> Hi Hiroshi,
>
> I applied your patch on psqlodbc 9.3.3 (NOT on the head branch).
> Now I can't even get to insert operations, because I get error while application configuration fetch.
>
> I have this CONFIG table, from where I load configuration parameters 
> (SELECT VALUE2, VALUE3 FROM CONFIG WHERE VALUE1 = ?) Each execute of the SQL returns one row. So the first execute success, but second returns with 'Null statement result in PGAPI_ExtendedFetch' error.
>
> psqlodbc and mylog files are in attechaments.

Thanks for the report.
Please apply the attached patch also.

It's the same as the one attached in the thread
  [ODBC] Problems using SQLFetch on prepared INSERT with RETURNING clause .

regards,
Hiroshi Inoue



-- 
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-03 07:44 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-05-28 08:52 Prepared statement error with UseServerSidePrepare=1 lev.bukovski@teliasonera.com
2014-05-28 22:17 ` Adrian Klaver <adrian.klaver@aklaver.com>
2014-06-02 03:59 ` Hiroshi Inoue <inoue@tpf.co.jp>
2014-06-02 13:11   ` lev.bukovski@teliasonera.com
2014-06-02 15:16     ` Hiroshi Inoue <inoue@tpf.co.jp>
2014-06-03 07:44       ` lev.bukovski@teliasonera.com

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