21

I have an application where I find a Sum() of a database column for a set of records and later use that sum in a separate query, similar to the following (made up tables, but the idea is the same):

SELECT Sum(cost)
INTO v_cost_total
FROM materials
WHERE material_id >=0
AND material_id <= 10; 

[a little bit of interim work]

SELECT material_id, cost/v_cost_total
INTO v_material_id_collection, v_pct_collection
FROM materials
WHERE material_id >=0
AND material_id <= 10
FOR UPDATE; 

However, in theory someone could update the cost column on the materials table between the two queries, in which case the calculated percents will be off.

Ideally, I would just use a FOR UPDATE clause on the first query, but when I try that, I get an error:

ORA-01786: FOR UPDATE of this query expression is not allowed

Now, the work-around isn't the problem - just do an extra query to lock the rows before finding the Sum(), but that query would serve no other purpose than locking the tables. While this particular example is not time consuming, the extra query could cause a performance hit in certain situations, and it's not as clean, so I'd like to avoid having to do that.

Does anyone know of a particular reason why this is not allowed? In my head, the FOR UPDATE clause should just lock the rows that match the WHERE clause - I don't see why it matters what we are doing with those rows.

EDIT: It looks like SELECT ... FOR UPDATE can be used with analytic functions, as suggested by David Aldridge below. Here's the test script I used to prove this works.

SET serveroutput ON;

CREATE TABLE materials (
    material_id NUMBER(10,0),
    cost        NUMBER(10,2)
);
ALTER TABLE materials ADD PRIMARY KEY (material_id);
INSERT INTO materials VALUES (1,10);
INSERT INTO materials VALUES (2,30);
INSERT INTO materials VALUES (3,90);

<<LOCAL>>
DECLARE
    l_material_id materials.material_id%TYPE;
    l_cost        materials.cost%TYPE;
    l_total_cost  materials.cost%TYPE;

    CURSOR test IS
        SELECT material_id,
            cost,
            Sum(cost) OVER () total_cost
        FROM   materials
        WHERE  material_id BETWEEN 1 AND 3
        FOR UPDATE OF cost;
BEGIN
    OPEN test;
    FETCH test INTO l_material_id, l_cost, l_total_cost;
    Dbms_Output.put_line(l_material_id||' '||l_cost||' '||l_total_cost);
    FETCH test INTO l_material_id, l_cost, l_total_cost;
    Dbms_Output.put_line(l_material_id||' '||l_cost||' '||l_total_cost);
    FETCH test INTO l_material_id, l_cost, l_total_cost;
    Dbms_Output.put_line(l_material_id||' '||l_cost||' '||l_total_cost);
END LOCAL;
/

Which gives the output:

1 10 130
2 30 130
3 90 130
1
  • 1
    Is it possible to use an analytic function (sum or ratio_to_report) with select ... for update? I don't have a database available at the moment to test this with so don't know... Aug 27, 2013 at 4:03

5 Answers 5

30

The syntax select . . . for update locks records in a table to prepare for an update. When you do an aggregation, the result set no longer refers to the original rows.

In other words, there are no records in the database to update. There is just a temporary result set.

4
  • 1
    I understand that the result set itself is temporary, but the integrity of the result set depends on the underlying data, so it seems like this would be a useful feature. I guess Oracle disagrees. Oh well.
    – BimmerM3
    Aug 27, 2013 at 2:23
  • 13
    @BimmerM3 . . . No. It is a much harder problem than you might think. How would to specify the records to lock for a max(). Would it be the ones just with the max() value? Would it be the whole table, because a new value could be added or updated larger than the max? Aug 27, 2013 at 2:26
  • Ah, good point. I didn't think about functions such as max() and min(). Thanks for the response.
    – BimmerM3
    Aug 27, 2013 at 2:56
  • 1
    @GordonLinoff - A SELECT at serializable isolation level has the same problem though. Aug 27, 2013 at 7:30
3

You might try something like:

<<LOCAL>>
declare
  material_id materials.material_id%Type;
  cost        materials.cost%Type;
  total_cost  materials.cost%Type;
begin
  select material_id,
         cost,
         sum(cost) over () total_cost
  into   local.material_id,
         local.cost,
         local.total_cost 
  from   materials
  where  material_id between 1 and 3
  for update of cost;

  ...

end local;

The first row gives you the total cost, but it selects all the rows and in theory they could be locked.

I don't know if this is allowed, mind you -- be interesting to hear whether it is.

2
  • Interesting - this does work! I'll put my test script in my original post.
    – BimmerM3
    Aug 27, 2013 at 7:43
  • 1
    @BimmerM3 . . . This is an insightful approach to the problem (and worth upvoting). However, it is not that "interesting". The result set is the original set of rows. The issue with the aggregation is that the result set is not the original rows. Aug 27, 2013 at 12:33
2

For example, there is product table with id, name and stock as shown below.

product table:

id name stock
1 Apple 3
2 Orange 5
3 Lemon 8

Then, both 2 queries below can run sum() and SELECT FOR UPDATE together:

SELECT sum(stock) FROM (SELECT * FROM product FOR UPDATE) AS result;
WITH result AS (SELECT * FROM product FOR UPDATE) SELECT sum(stock) FROM result;

Output:

 sum
-----
  16
(1 row)
2
  • Will these queries block a INSERT statement to the table in another session ? Or Will an error be raised when executing UPDATE statement in the current session? If not, it possibly causes some kind of write skew. Jan 25, 2023 at 5:59
  • I don't know if write skew occurs or not with my code above. Good point!! Jan 25, 2023 at 6:09
1

For that, you can use the WITH command.

Exemple:

WITH result AS (
  -- your select
) SELECT * FROM result GROUP BY material_id;
0

Is your problem "However, in theory someone could update the cost column on the materials table between the two queries, in which case the calculated percents will be off."?

In that case , probably you can simply use a inner query as:

SELECT material_id, cost/(SELECT Sum(cost)
  FROM materials
  WHERE material_id >=0
  AND material_id <= 10)
INTO v_material_id_collection, v_pct_collection
FROM materials
WHERE material_id >=0
AND material_id <= 10;

Why do you want to lock a table? Other applications might fail if they try to update that table during that time right?

3
  • 1
    The other applications will have to wait until I release the lock, but they shouldn't fail when programmed correctly.
    – BimmerM3
    Aug 27, 2013 at 14:11
  • That depends on how the other applications are programmed.
    – A Nice Guy
    Aug 27, 2013 at 17:44
  • In the actual application that I'm talking about here, I go back and update the rows, so either way they will be locked after the update. Locking with with the FOR...UPDATE clause guarantees that no one else will change the data in the meantime. In this case, the actual likelihood of a conflict is pretty small and data integrity is important enough that its worth taking the risk of causing another session to fail.
    – BimmerM3
    Aug 27, 2013 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.