pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: savepoint
5+ messages / 4 participants
[nested] [flat]

* Re: savepoint
@ 2005-09-02 08:41  Dave Page <dpage@vale-housing.co.uk>
  0 siblings, 1 reply; 5+ messages in thread

From: Dave Page @ 2005-09-02 08:41 UTC (permalink / raw)
  To: Hiroshi Saito <saito@inetrt.skcapi.co.jp>; +Cc: Marko Ristola <Marko.Ristola@kolumbus.fi>; pgsql-odbc

 

> -----Original Message-----
> From: Hiroshi Saito [mailto:saito@inetrt.skcapi.co.jp] 
> Sent: 02 September 2005 03:53
> To: Hiroshi Saito; Dave Page
> Cc: Marko Ristola
> Subject: Re: savepoint
> 
> Uga.. 
> sorry && missing..
> replay this.
> 
> > Hi Dave. and Dear Marko.
> > 
> > I consider many things about the importance of savepoint.
> > Probably, this will be important for the programmer of 
> > operating application. Marko has suggested this thoughtfully.
> > This is supported by the reason I also have a realistic idea.
> > Though it is the pretreatment which carries out honest 
> > correspondence to the future, I think that this is good.
> > 
> > Would you consider application? Or another suggestion.


Hi Hiroshi

Please post to the list!!

I've updated the patch as attached:

- It's a little more readable I think
- It works with libpq as well as the old comms :-)
- It should be case insensitive (otherwise, you could just use
strstr()).

There's also some minor reformatting of misc.h in there.

It is applied to CVS - thanks Hiroshi.

Regards, Dave

Attachments:

savepoint.patch (application/octet-stream, 4.3K) download ? Release ? savepoint_patch ? widetypes.diff ? installer/Thumbs.db ? installer/psqlodbc.msi ? installer/psqlodbc.msm ? installer/psqlodbc.wixobj ? installer/psqlodbcm.wixobj Index: connection.c =================================================================== RCS file: /cvsroot/psqlodbc/psqlodbc/connection.c,v retrieving revision 1.109 diff -u -r1.109 connection.c --- connection.c 1 Sep 2005 14:45:16 -0000 1.109 +++ connection.c 2 Sep 2005 08:09:37 -0000 @@ -1822,7 +1822,14 @@ else if (strnicmp(cmdbuffer, "COMMIT", 6) == 0) CC_on_commit(self); else if (strnicmp(cmdbuffer, "ROLLBACK", 8) == 0) + { + /* + The method of ROLLBACK an original form .... + ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name + */ + if (PG_VERSION_LT(self, 8.0) || !(contains_token(query, " TO "))) CC_on_abort(self, NO_TRANS); + } else if (strnicmp(cmdbuffer, "END", 3) == 0) CC_on_commit(self); else if (strnicmp(cmdbuffer, "ABORT", 5) == 0) @@ -3195,7 +3202,14 @@ else if (strnicmp(cmdbuffer, "COMMIT", 6) == 0) CC_on_commit(self); else if (strnicmp(cmdbuffer, "ROLLBACK", 8) == 0) + { + /* + The method of ROLLBACK an original form .... + ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name + */ + if (PG_VERSION_LT(self, 8.0) || !(contains_token(query, " TO "))) CC_on_abort(self, NO_TRANS); + } else if (strnicmp(cmdbuffer, "END", 3) == 0) CC_on_commit(self); else if (strnicmp(cmdbuffer, "ABORT", 5) == 0) Index: misc.c =================================================================== RCS file: /cvsroot/psqlodbc/psqlodbc/misc.c,v retrieving revision 1.41 diff -u -r1.41 misc.c --- misc.c 13 Jul 2005 14:23:35 -0000 1.41 +++ misc.c 2 Sep 2005 08:04:24 -0000 @@ -447,3 +447,20 @@ } return my_strcat1(buf, fmt, s1, s, len); } + +int +contains_token(char *data, char *token) +{ + int i, tlen, dlen; + + dlen = strlen(data); + tlen = strlen(token); + + for (i = 0; i < dlen-tlen+1; i++) + { + if (!strnicmp((const char *)data+i, token, tlen)) + return 1; + } + + return 0; +} Index: misc.h =================================================================== RCS file: /cvsroot/psqlodbc/psqlodbc/misc.h,v retrieving revision 1.40 diff -u -r1.40 misc.h --- misc.h 9 Jul 2004 18:50:51 -0000 1.40 +++ misc.h 2 Sep 2005 08:03:29 -0000 @@ -116,16 +116,16 @@ #endif -void remove_newlines(char *string); -char *strncpy_null(char *dst, const char *src, int len); -char *trim(char *string); -char *make_string(const char *s, int len, char *buf, size_t bufsize); -char *make_lstring_ifneeded(ConnectionClass *, const char *s, int len, BOOL); -char *my_strcat(char *buf, const char *fmt, const char *s, int len); -char *schema_strcat(char *buf, const char *fmt, const char *s, int len, +void remove_newlines(char *string); +char *strncpy_null(char *dst, const char *src, int len); +char *trim(char *string); +char *make_string(const char *s, int len, char *buf, size_t bufsize); +char *make_lstring_ifneeded(ConnectionClass *, const char *s, int len, BOOL); +char *my_strcat(char *buf, const char *fmt, const char *s, int len); +char *schema_strcat(char *buf, const char *fmt, const char *s, int len, const char *, int, ConnectionClass *conn); -char *my_strcat1(char *buf, const char *fmt, const char *s1, const char *s, int len); -char *schema_strcat1(char *buf, const char *fmt, const char *s1, +char *my_strcat1(char *buf, const char *fmt, const char *s1, const char *s, int len); +char *schema_strcat1(char *buf, const char *fmt, const char *s1, const char *s, int len, const char *, int, ConnectionClass *conn); /* #define GET_SCHEMA_NAME(nspname) (stricmp(nspname, "public") ? nspname : "") */ @@ -137,9 +137,10 @@ #define STRCPY_TRUNCATED (-1) #define STRCPY_NULL (-2) -int my_strcpy(char *dst, int dst_len, const char *src, int src_len); - -/* Define a type for defining a constant string expression */ -#define CSTR static const char * const +int my_strcpy(char *dst, int dst_len, const char *src, int src_len); +int contains_token(char *data, char *token); + +/* Define a type for defining a constant string expression */ +#define CSTR static const char * const #endif
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: savepoint
@ 2005-09-03 13:14  Marko Ristola <marko.ristola@kolumbus.fi>
  parent: Dave Page <dpage@vale-housing.co.uk>
  0 siblings, 1 reply; 5+ messages in thread

From: Marko Ristola @ 2005-09-03 13:14 UTC (permalink / raw)
  To: Dave Page <dpage@vale-housing.co.uk>; +Cc: Hiroshi Saito <saito@inetrt.skcapi.co.jp>; pgsql-odbc


Here is a harder scenario (with pseudo SQL):

BEGIN
SAVEPOINT svp
CREATE CURSOR CC AS SELECT values from TEST LIMIT 2 -- OK
FETCH 1 -- fetched one row
FETCH 1 -- fetched one row
CLOSE CURSOR CC
ROLLBACK TO svp
END

The above works with psqlodbc.

BEGIN
Stmt 1: SAVEPOINT svp
Stmt 1: CREATE CURSOR CC AS SELECT values from TEST LIMIT 2 -- OK
Stmt 1: FETCH 1 -- fetched one row
Stmt 2: INSERT INTO VALUES -- ERROR.
Stmt 2: ROLLBACK TO svp:
   Inside PSQLODBC:
   1. PSQLDODBC calls CLOSE CURSOR CC. Error, because transaction is in 
ERROR condition.
   2. ROLLBACK TO svp: this won't be done, because (1) failed already.
   Correct way here is to NOT CLOSE DEAD CURSORS!
Stmt 1: FETCH 1 -- fetched one row -- NOT DONE
CLOSE CURSOR CC -- NOT DONE
END

Unfortunately the PostgreSQL backend does
not report the closing of the cursor CC with a NOTIFY message.

So the full implementation requires tracking of the
cursor liveness within savepoints.

Another way is to adjust the future PostgreSQL 8.1 to notify the 
PsqlODBC about
each cursor that gets closed by the backend. That way, the Psqlodbc 
CC_send_query()
can be adjusted to react into those NOTIFY
messages. Then the fix would be to react into those NOTIFY messages, and 
the PsqlODBC
driver would traverse the stmt list and close each notified cursor 
silently. The programmer should
not close them explicitely. Programmer's cursor closing would not harm 
though, because
it is already closed.

Third and worst of the easy problem solutions is that the PsqlODBC user 
program
would inform the Statement handle so, that the CLOSE CURSOR CC will not 
be delivered
for the PostgreSQL server. Psqlodbc programmer could learn the skills to 
know
within his program, which cursors are dead. Maybe something like 
SQLSetStmtAttr(stmt2,CURSOR_IS_DEAD_ON_THE_BACKEND);
could do something like

// this is just the trivial fix, that unbinds the CURSOR,
// not good C coding inside PsqlODBC:
if (stmt->cursor != NULL) stmt->cursor[0]='\0';

Of course, this might mean good bye for ODBC conformance and hard times for
psqlodbc savepoint users: It is easier for us to solve the logical 
problem once
and give an easy programming environment for the users.


Maybe the best implementation is that the PostgreSQL Server
does not close the cursors _silently_ -- closes them, but with a NOTIFY 
message.

Then we adjust the CC_send_query() accordingly to clean up the Stmt's 
internal
structures of an already closed cursor.

Then we could implement the SAVEPOINT support for PostgreSQL 8.1.
And we would not need to create a duplicate of the CURSOR behaviour
in SAVEPOINTs

Regards,
Marko Ristola

Dave Page wrote:

> 
>
>  
>
>>-----Original Message-----
>>From: Hiroshi Saito [mailto:saito@inetrt.skcapi.co.jp] 
>>Sent: 02 September 2005 03:53
>>To: Hiroshi Saito; Dave Page
>>Cc: Marko Ristola
>>Subject: Re: savepoint
>>
>>Uga.. 
>>sorry && missing..
>>replay this.
>>
>>    
>>
>>>Hi Dave. and Dear Marko.
>>>
>>>I consider many things about the importance of savepoint.
>>>Probably, this will be important for the programmer of 
>>>operating application. Marko has suggested this thoughtfully.
>>>This is supported by the reason I also have a realistic idea.
>>>Though it is the pretreatment which carries out honest 
>>>correspondence to the future, I think that this is good.
>>>
>>>Would you consider application? Or another suggestion.
>>>      
>>>
>
>
>Hi Hiroshi
>
>Please post to the list!!
>
>I've updated the patch as attached:
>
>- It's a little more readable I think
>- It works with libpq as well as the old comms :-)
>- It should be case insensitive (otherwise, you could just use
>strstr()).
>
>There's also some minor reformatting of misc.h in there.
>
>It is applied to CVS - thanks Hiroshi.
>
>Regards, Dave
>  
>




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

* Re: savepoint
@ 2005-09-04 18:20  Dave Page <dpage@vale-housing.co.uk>
  0 siblings, 0 replies; 5+ messages in thread

From: Dave Page @ 2005-09-04 18:20 UTC (permalink / raw)
  To: Marko Ristola <marko.ristola@kolumbus.fi>; +Cc: Hiroshi Saito <saito@inetrt.skcapi.co.jp>; pgsql-odbc

Hi Marko,

Thanks for the analysis. Unfortunately I'm already spending far more
time on psqlODBC than I should be, so I won't be able to work on this
myself. Is it something you would like to work on? We could sure use
another programmer on the project, and you're obviously not afraid to
dive into the code :-)

Regards, Dave.

> -----Original Message-----
> From: Marko Ristola [mailto:marko.ristola@kolumbus.fi] 
> Sent: 03 September 2005 14:15
> To: Dave Page
> Cc: Hiroshi Saito; pgsql-odbc@postgresql.org
> Subject: Re: savepoint
> 
> 
> Here is a harder scenario (with pseudo SQL):
> 
> BEGIN
> SAVEPOINT svp
> CREATE CURSOR CC AS SELECT values from TEST LIMIT 2 -- OK
> FETCH 1 -- fetched one row
> FETCH 1 -- fetched one row
> CLOSE CURSOR CC
> ROLLBACK TO svp
> END
> 
> The above works with psqlodbc.
> 
> BEGIN
> Stmt 1: SAVEPOINT svp
> Stmt 1: CREATE CURSOR CC AS SELECT values from TEST LIMIT 2 -- OK
> Stmt 1: FETCH 1 -- fetched one row
> Stmt 2: INSERT INTO VALUES -- ERROR.
> Stmt 2: ROLLBACK TO svp:
>    Inside PSQLODBC:
>    1. PSQLDODBC calls CLOSE CURSOR CC. Error, because 
> transaction is in 
> ERROR condition.
>    2. ROLLBACK TO svp: this won't be done, because (1) failed already.
>    Correct way here is to NOT CLOSE DEAD CURSORS!
> Stmt 1: FETCH 1 -- fetched one row -- NOT DONE
> CLOSE CURSOR CC -- NOT DONE
> END
> 
> Unfortunately the PostgreSQL backend does
> not report the closing of the cursor CC with a NOTIFY message.
> 
> So the full implementation requires tracking of the
> cursor liveness within savepoints.
> 
> Another way is to adjust the future PostgreSQL 8.1 to notify the 
> PsqlODBC about
> each cursor that gets closed by the backend. That way, the Psqlodbc 
> CC_send_query()
> can be adjusted to react into those NOTIFY
> messages. Then the fix would be to react into those NOTIFY 
> messages, and 
> the PsqlODBC
> driver would traverse the stmt list and close each notified cursor 
> silently. The programmer should
> not close them explicitely. Programmer's cursor closing would 
> not harm 
> though, because
> it is already closed.
> 
> Third and worst of the easy problem solutions is that the 
> PsqlODBC user 
> program
> would inform the Statement handle so, that the CLOSE CURSOR 
> CC will not 
> be delivered
> for the PostgreSQL server. Psqlodbc programmer could learn 
> the skills to 
> know
> within his program, which cursors are dead. Maybe something like 
> SQLSetStmtAttr(stmt2,CURSOR_IS_DEAD_ON_THE_BACKEND);
> could do something like
> 
> // this is just the trivial fix, that unbinds the CURSOR,
> // not good C coding inside PsqlODBC:
> if (stmt->cursor != NULL) stmt->cursor[0]='\0';
> 
> Of course, this might mean good bye for ODBC conformance and 
> hard times for
> psqlodbc savepoint users: It is easier for us to solve the logical 
> problem once
> and give an easy programming environment for the users.
> 
> 
> Maybe the best implementation is that the PostgreSQL Server
> does not close the cursors _silently_ -- closes them, but 
> with a NOTIFY 
> message.
> 
> Then we adjust the CC_send_query() accordingly to clean up the Stmt's 
> internal
> structures of an already closed cursor.
> 
> Then we could implement the SAVEPOINT support for PostgreSQL 8.1.
> And we would not need to create a duplicate of the CURSOR behaviour
> in SAVEPOINTs
> 
> Regards,
> Marko Ristola
> 
> Dave Page wrote:
> 
> > 
> >
> >  
> >
> >>-----Original Message-----
> >>From: Hiroshi Saito [mailto:saito@inetrt.skcapi.co.jp] 
> >>Sent: 02 September 2005 03:53
> >>To: Hiroshi Saito; Dave Page
> >>Cc: Marko Ristola
> >>Subject: Re: savepoint
> >>
> >>Uga.. 
> >>sorry && missing..
> >>replay this.
> >>
> >>    
> >>
> >>>Hi Dave. and Dear Marko.
> >>>
> >>>I consider many things about the importance of savepoint.
> >>>Probably, this will be important for the programmer of 
> >>>operating application. Marko has suggested this thoughtfully.
> >>>This is supported by the reason I also have a realistic idea.
> >>>Though it is the pretreatment which carries out honest 
> >>>correspondence to the future, I think that this is good.
> >>>
> >>>Would you consider application? Or another suggestion.
> >>>      
> >>>
> >
> >
> >Hi Hiroshi
> >
> >Please post to the list!!
> >
> >I've updated the patch as attached:
> >
> >- It's a little more readable I think
> >- It works with libpq as well as the old comms :-)
> >- It should be case insensitive (otherwise, you could just use
> >strstr()).
> >
> >There's also some minor reformatting of misc.h in there.
> >
> >It is applied to CVS - thanks Hiroshi.
> >
> >Regards, Dave
> >  
> >
> 
> 



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

* Re: savepoint
@ 2005-09-06 09:01  Hiroshi Saito <saito@inetrt.skcapi.co.jp>
  parent: Marko Ristola <marko.ristola@kolumbus.fi>
  0 siblings, 1 reply; 5+ messages in thread

From: Hiroshi Saito @ 2005-09-06 09:01 UTC (permalink / raw)
  To: Marko Ristola <marko.ristola@kolumbus.fi>; Dave Page <dpage@vale-housing.co.uk>; +Cc: pgsql-odbc

Hi Marko.

> BEGIN
> Stmt 1: SAVEPOINT svp
> Stmt 1: CREATE CURSOR CC AS SELECT values from TEST LIMIT 2 -- OK
> Stmt 1: FETCH 1 -- fetched one row
> Stmt 2: INSERT INTO VALUES -- ERROR.
> Stmt 2: ROLLBACK TO svp:
>    Inside PSQLODBC:
>    1. PSQLDODBC calls CLOSE CURSOR CC. Error, because transaction is in 
> ERROR condition.
>    2. ROLLBACK TO svp: this won't be done, because (1) failed already.
>    Correct way here is to NOT CLOSE DEAD CURSORS!
> Stmt 1: FETCH 1 -- fetched one row -- NOT DONE
> CLOSE CURSOR CC -- NOT DONE
> END

Umm, Actually, the cursor name will not be found when it ROLLBACK out of cursor.
It is a phenomenon which happens even if it is not psqlodbc....
I consider the following scenario.

CREATE TABLE svtest (k1 varchar(19) PRIMARY KEY,d1 varchar(10));
INSERT INTO svtest VALUES('X','1');
INSERT INTO svtest VALUES('Y','2');
INSERT INTO svtest VALUES('Z','3');
BEGIN;
SAVEPOINT svp1;
DECLARE CC CURSOR FOR SELECT * from svtest LIMIT 2;
FETCH FIRST FROM CC;
SAVEPOINT svp2;
INSERT INTO svtest values('X','99');
ROLLBACK TO svp2;
INSERT INTO svtest values('A','0');
FETCH FIRST FROM CC;
FETCH NEXT FROM CC;
CLOSE CC;
END;
SELECT * FROM svtest;
DROP TABLE svtest CASCADE;

The above works with psqlodbc. However, Updating cursor is not considered carefully. 

Regards,
Hiroshi Saito




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

* Re: savepoint
@ 2005-09-06 16:53  Marko Ristola <Marko.Ristola@kolumbus.fi>
  parent: Hiroshi Saito <saito@inetrt.skcapi.co.jp>
  0 siblings, 0 replies; 5+ messages in thread

From: Marko Ristola @ 2005-09-06 16:53 UTC (permalink / raw)
  To: Hiroshi Saito <saito@inetrt.skcapi.co.jp>; +Cc: Dave Page <dpage@vale-housing.co.uk>; pgsql-odbc

Hiroshi Saito wrote:

>
>The above works with psqlodbc. However, Updating cursor is not considered carefully. 
>
>Regards,
>Hiroshi Saito
>
>  
>
Thats good that it works with your scenario.
My own feeling about my example was, that the exact case I described
wasn't very good.

I did find the cursor problem during this summer, when I played with
savepoints.
That was before the libpq was put into the CVS.
I tried then hard to understand the whole problem, not just some cases
of it.
I tried to make a robust implementation.

The problem case was and is as follows:
1. An ODBC statement handle holds an internal cursor.
2. That cursor has been rolled back by the backend via calling ROLLBACK
TO SAVEPOINT
    and the transaction is under READY state.
    Or then the transaction is under FAILED state, and the backend has
removed the cursor, or
    will remove the cursor during ROLLBACK TO SAVEPOINT.
3. Statement closing tries to close the nonexistent cursor.
This causes the current transaction to go into the FAILED state, if it
is not already in that state,
thus making things possibly worse.

This occurred to me, when I was playing with UseDeclareFetch=1 and with
savepoint testing.
There the ODBC did try to roll back the nonexistent cursor. Maybe the
transaction
was in the failed state at that point.

As you already might have read my previous email, UseDeclareFetch=1
doesn't work
with the current libpq ODBC driver. So I can't test it's behaviour now.

Here is one scenario with UseDeclareFetch=1 and Fetch=2, but I don't
know  wether it works,
because I can't test it. The backend fetches at most 2 rows per fetch.

NEW_STMT_HANDLE=SELECT * from test1 LIMIT 20000000;
foreach row in CC ; do
    SAVEPOINT S1;
    INSERT each fetched row INTO TABLE test2;
    RELEASE S1 (or ROLLBACK TO S1 on failure).
done
DROP_STMT

With the above pseudo code, psqlodbc FETCHes twenty million rows
from the backend with two rows per internal PSQLODBC fetch. A low
memory consumption can be garanteed on the client side with 32 bit
computers.

Unfortunately PostgreSQL uses here indexes, because it assumes, that using
cursors means that only a fraction will be fetched.
Without UseDeclareFetch, the program would crash.

Conclusion:
These problem scenarios occur only with ODBC statement handle maintained
cursors.
Do you know, wether there any other such uses, than the UseDeclareFetch=1?

If the internal cursor is created and closed within the one single ODBC
call, this problem
scenario doesn't occur, or it can be fixed with easy coding.

When the ODBC using program has the responsibility on tracking and
closing the open CURSORs,
the savepoints are able to work nicely without the savepoint
maintenance code inside psqlodbc. The application programmer knows,
when it's cursors exist, and when not.

So maybe I have kept this problem too big, and tried to solve it
with too much work after all.

Regards,
Marko




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


end of thread, other threads:[~2005-09-06 16:53 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-02 08:41 Re: savepoint Dave Page <dpage@vale-housing.co.uk>
2005-09-03 13:14 ` Marko Ristola <marko.ristola@kolumbus.fi>
2005-09-06 09:01   ` Hiroshi Saito <saito@inetrt.skcapi.co.jp>
2005-09-06 16:53     ` Marko Ristola <Marko.Ristola@kolumbus.fi>
2005-09-04 18:20 Re: savepoint Dave Page <dpage@vale-housing.co.uk>

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