db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Matrigali <mikem_...@sbcglobal.net>
Subject Re: Updating rows with an open cursor...what is the expected behavior?
Date Tue, 12 Jun 2007 18:39:21 GMT


Army wrote:
> While investigating DERBY-2805 I was trying to understand the purpose of 
> "ResultSetNode.markOrderingDependent()" as it is used in the 
> language/optimizer layer of code.  In particular, see 
> IndexRowToBaseRowNode:
> 
>     /**
>      * Notify the underlying result set tree that the result is
>      * ordering dependent.  (For example, no bulk fetch on an index
>      * if under an IndexRowToBaseRow.)
>      */
>     void markOrderingDependent()
>     {
>         /* NOTE: We use a different method to tell a FBT that
>          * it cannot do a bulk fetch as the ordering issues are
>          * specific to a FBT being under an IRTBR as opposed to a
>          * FBT being under a PRN, etc.
>          */
>         source.disableBulkFetch();
>     }
> 
> As part of some testing I ran statements in ij which effectively do the 
> following:
> 
>   1. Get a SELECT cursor on the table that specifies an ORDER BY
>   2. In the middle of iterating through the table, update one of the rows.
>   3. Continue iterating through the cursor.
> 
> To my surprise, the results of the query differs depending on whether 
> Derby does an Index Scan or a Table Scan on the underlying table.  In 
> the case of an Index Scan the open cursor will see the updated row; but 
> in the case of a Table Scan the cursor will *not* see the updated row.  
> A quick look at the query plan shows that in both cases Derby is doing 
> "share row locking".
> 
> To see this in ij, do the following (I haven't tried this in JDBC yet...):
> 
> autocommit off;
> create table str(c1 int, c2 int, c3 int);
> insert into str values (1, 1, 2), (1, 2, 3), (1, 3, 4), (1, 4, 5);
> create index str1 on str(c1, c2);
> commit;
> 
> -- Force index scan.
> 
> get cursor c1 as
>   'select c2, c3 from str --DERBY-PROPERTIES index=str1
>    where c1 = 1 order by c2';
> 
> next c1;  -- returns "1, 2"
> update str set c2 = 4 where c2 = 2;
> next c1;  -- returns "3, 4"
> next c1;  -- returns "4, 3" ==> updated row is picked up
> next c1;  -- returns "4, 5"
> next c1;  -- no current row
> close c1;
> rollback;
> 
> -- Force table scan.
> 
> get cursor c1 as
>   'select c2, c3 from str --DERBY-PROPERTIES index=null
>    where c1 = 1 order by c2';
> 
> next c1;  -- returns "1, 2"
> update str set c2 = 4 where c2 = 2;
> next c1;  -- returns "2, 3" ==> updated row is *not* picked up
> next c1;  -- returns "3, 4"
> next c1;  -- returns "4, 5"
> next c1;  -- no current row
> close c1;
> rollback;
> 
> Is there a reason we get different results for the two query plans, or 
> is this a bug?  If it's a bug, then what is the correct behavior for 
> this kind of thing?
After looking at the query wanted to point out something else going on.
Often adding an order by will cause the scan to insensitive to the 
update as we have to first get all the rows, sort them all before we
give a row back to caller.  But in the above case Derby figured out
order by c2 could be done using index on (c1, c2) because c1 was fixed
by where c1=1.  So I assume the query plan did sort elimination in
favor of the index scan.

If you order by c3 I think you will never see the update affect the
rows returned.
> 
> Note that if I comment out the call to "disableBulkFetch()" in the code 
> shown at the top of this mail, then the order of the rows changes for 
> the Index Scan (they are no longer in correct order), but the open 
> cursor still picks up the updated row.
> 
> I plan to see if I can reproduce this behavior using a JDBC program, but 
> if anyone has any thoughts/insight in the meantime, I'd appreciate the 
> pointers. And apologies in advance if I'm missing something obvious...
> 
> Army
> 
> 

Mime
View raw message