in Pl/PgSQL, do commit every 5000 records
in Pl/PgSQL, do commit every 5000 records
От:
Emi Lu <emilu@encs.concordia.ca>
Дата:
Good morning, In a plpgsql function, I am trying to insert 900, 000 records into several tables. I remembered people mentioned before that it is better and more efficient to commit actions for let's say every 5000 records' insertion. May I get more inputs about why and how this commit can speed up the transaction please? Thanks a lot, Ying
Re: in Pl/PgSQL, do commit every 5000 records
От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Fri, Mar 10, 2006 at 09:36:16 -0500, Emi Lu wrote: > Good morning, > > In a plpgsql function, I am trying to insert 900, 000 records into > several tables. I remembered people mentioned before that it is better > and more efficient to commit actions for let's say every 5000 records' > insertion. You can't do commits inside of a function. I think you are misremembering advice about not do inserts with a transaction per row which will have a lot of overhead for all of the commits. You can do savepoints inside of a function, but those are going to slow things down, not speed them up.
Re: in Pl/PgSQL, do commit every 5000 records
От:
Emi Lu <emilu@encs.concordia.ca>
Дата:
Hi Bruno,
>You can't do commits inside of a function.
>
The example I have is:
CREATE OR REPLACE function test() returns boolean AS $$
DECLARE
... ...
counter INTEGER := 0;
BEGIN
... ...
query_value := ' .....' ;
OPEN curs1 FOR EXECUTE query_value;
LOOP
FETCH curs1 INTO studid;
EXIT WHEN NOT FOUND;
query_value := ' INSERT INTO ... ...';
EXECUTE query_value ;
counter := counter + 1 ;
IF counter%5000 = 0 THEN
counter := 0;
COMMIT;
END IF;
END LOOP;
CLOSE curs1;
...
END;
... ...
The above function works ok.
"can't do commits inside of a function " , do you mean although the
function complied ok and run successfully, but it did not really commit
insertion actions at every 5000 records?
>I think you are misremembering advice about not do inserts with a transaction per row which will have
>a lot of overhead for all of the commits.
>
Re: in Pl/PgSQL, do commit every 5000 records
От:
Emi Lu <emilu@encs.concordia.ca>
Дата:
I got the answer. Although the compile passed, when it reaches 5000, the commit command causes a "SPI_ERROR_TRANSACTION" exception. Thank you for all your hint. >> You can't do commits inside of a function. > > > The example I have is: > > CREATE OR REPLACE function test() returns boolean AS $$ > DECLARE > ... ... > counter INTEGER := 0; > BEGIN > ... ... > query_value := ' .....' ; > OPEN curs1 FOR EXECUTE query_value; > LOOP > FETCH curs1 INTO studid; > EXIT WHEN NOT FOUND; > > query_value := ' INSERT INTO ... ...'; > EXECUTE query_value ; > > counter := counter + 1 ; > IF counter%5000 = 0 THEN > counter := 0; > COMMIT; > END IF; > > END LOOP; > > > CLOSE curs1; ... > END; > > ... ... > > The above function works ok. > "can't do commits inside of a function " , do you mean although the > function complied ok and run successfully, but it did not really > commit insertion actions at every 5000 records? > >> I think you are misremembering advice about not do inserts with a >> transaction per row which will have >> a lot of overhead for all of the commits >
Re: in Pl/PgSQL, do commit every 5000 records
От:
"Florian G. Pflug" <fgp@phlo.org>
Дата:
Emi Lu wrote: > The example I have is: > > CREATE OR REPLACE function test() returns boolean AS $$ > DECLARE > ... ... > counter INTEGER := 0; > BEGIN > ... ... > query_value := ' .....' ; > OPEN curs1 FOR EXECUTE query_value; > LOOP > FETCH curs1 INTO studid; > EXIT WHEN NOT FOUND; > > query_value := ' INSERT INTO ... ...'; > EXECUTE query_value ; > > counter := counter + 1 ; > IF counter%5000 = 0 THEN > counter := 0; > COMMIT; > END IF; > > END LOOP; > > > CLOSE curs1; ... > END; Are you aware of the "insert into
Re: in Pl/PgSQL, do commit every 5000 records
От:
Emi Lu <emilu@encs.concordia.ca>
Дата:
Florian G. Pflug wrote: > Emi Lu wrote: > >> The example I have is: >> >> CREATE OR REPLACE function test() returns boolean AS $$ >> DECLARE >> ... ... >> counter INTEGER := 0; >> BEGIN >> ... ... >> query_value := ' .....' ; >> OPEN curs1 FOR EXECUTE query_value; >> LOOP >> FETCH curs1 INTO studid; >> EXIT WHEN NOT FOUND; >> >> query_value := ' INSERT INTO ... ...'; >> EXECUTE query_value ; >> >> counter := counter + 1 ; >> IF counter%5000 = 0 THEN >> counter := 0; >> COMMIT; >> END IF; >> >> END LOOP; >> >> >> CLOSE curs1; ... >> END; > > Are you aware of the "insert into
Re: in Pl/PgSQL, do commit every 5000 records
От:
"Florian G. Pflug" <fgp@phlo.org>
Дата:
Emi Lu wrote: > Florian G. Pflug wrote: < snipped code of stored procedure > >> >> Are you aware of the "insert into
Re: in Pl/PgSQL, do commit every 5000 records
От:
Emi Lu <emilu@encs.concordia.ca>
Дата:
> Florian G. Pflug wrote: > < snipped code of stored procedure > > >>> >>> Are you aware of the "insert into
Re: in Pl/PgSQL, do commit every 5000 records
Emi Lu wrote: >> Florian G. Pflug wrote: >> < snipped code of stored procedure > >>>> Are you aware of the "insert into
Re: in Pl/PgSQL, do commit every 5000 records
От:
"Merlin Moncure" <mmoncure@gmail.com>
Дата:
> >> In general, if you have the choice between looping over a large result > >> in a stored procedure (or, even worse, in a client app) and letting the > >> backend do the looping, then letting the backend handle it is nearly > >> always > >> faster. There are different reasons why a large query might not always be the best approach. Unfortunately it is the only approach on the server side. Large queries tend to become less and less practical when the database becomes really big. Just as a 'for example', it would be nice to be able to do part of a large complex job, stop it, and continue it again later. stored procedures (not functions) are suppoesed to give you this power and allow you to do things which are non-transactional like vacuum. merlin
Re: in Pl/PgSQL, do commit every 5000 records
От:
Emi Lu <emilu@encs.concordia.ca>
Дата:
Hi Merlin,
>> >> In general, if you have the choice between looping over a large result
>> >> in a stored procedure (or, even worse, in a client app) and letting the
>> >> backend do the looping, then letting the backend handle it is nearly
>> >> always
>> >> faster.
>>
>>
>
>There are different reasons why a large query might not always be the
>best approach. Unfortunately it is the only approach on the server
>side.
>
>Large queries tend to become less and less practical when the database
>becomes really big. Just as a 'for example', it would be nice to be
>able to do part of a large complex job, stop it, and continue it again
>later.
>
>
Also combined the suggestions from Florian,
>> use the exception support in plpgsql to prevent the whole
transaction from rolling back in case of an error.
>> Only the statements _inside_ the block where you caught the error
would roll back.
I will try separate my huge data computation into several pieces
something like:
declare
...
begin
...
-- step1
BEGIN
...
insert into (select ... ... from ... where ... )
EXCEPTION WHEN ...... THEN
-- do nothing
END;
-- step2
BEGIN
...
UPDATE tableA from ... WHERE ... ;
EXCEPTION WHEN ...... THEN
-- do nothing
END;
...
...
end;
If I understood correctly, "begin ... exception when .. then ... end"
can work the same way as commit. In another way, if commands in the
sub-block (such as step1) run successfully, data in this part (step1) is
committed. Then step2, step3... stepN that are all under "begin..
exception.. end" sub-blocks will be run and "committed" one by one.
>stored procedures (not functions) are suppoesed to give you this power
>and allow you to do things which are non-transactional like vacuum.
>
>
"To define a procedure, i.e. a function that returns nothing, just
specify RETURNS VOID. "
Copied from
http://www.postgresql.org/docs/8.1/interactive/sql-createfunction.html
So, a stored procedure is "a void function", right?
Thanks,
Ying
Re: in Pl/PgSQL, do commit every 5000 records
От:
"Merlin Moncure" <mmoncure@gmail.com>
Дата:
> I will try separate my huge data computation into several pieces > something like: [...] > If I understood correctly, "begin ... exception when .. then ... end" > can work the same way as commit. In another way, if commands in the > sub-block (such as step1) run successfully, data in this part (step1) is > committed. Then step2, step3... stepN that are all under "begin.. > exception.. end" sub-blocks will be run and "committed" one by one. begin...exception...end; does not break up the transaction into smaller subtransactions. it does however allow graceful handling from errors inside a function but that is not what you are looking for. To put it another way, it is impossible for any part of the work inside the function to become visible to other backends unless you leave the function without error and the transaction that wraps it (if there is one) is comitted. > "To define a procedure, i.e. a function that returns nothing, just > specify RETURNS VOID. " > Copied from > http://www.postgresql.org/docs/8.1/interactive/sql-createfunction.html > > So, a stored procedure is "a void function", right? yes and no. Yes in that a procedure differs from a function in that it returns no value. No in that 'pure' stored procedures discussed some months back in hackers (and IIRC not implemented yet) are not transactional entities. They are like server side sql scripts that take parameters. Try running vacuum inside a function...it doen't work but it would inside a non function based stored procedure. I think your best bet is to first try Florian's approach of the monolithic query and see if it works for you...if it does, great. If not, you have basically three options: 1. in transaction cursor: declare your input cursor in transaction and fetch x rows at a time (say, 1000) and write them back over another transaction comitting as you go. This is insensitive in that you can't see changes as you loop through the set. 2.non transactional cursor: using only one connection you declare your cursor 'with hold' and loop over and insert over same connection. Just beware that postgresql must materialize 'with hold' cursors into a temporary table. This approach is also insensitive. 3. client side table browsing. You can browse your tables ISAM style. This is a fancy way of saying you use client code to loop over a table ordered on a key. This approach can be sensitive (one connection) or insensitive (two connections) while preserving the ablity to commit as you go. merlin