0

I have a loop in transaction block that creates a temporary table,updates,exports to txt file and drops table at the end of the block. After some time I noticed that disk size was getting smaller each time loop continued.When I cancelled transaction block,disk size returned to its initial state.

How can I stop loop for allocating the disk on the following transaction ?

do
$$declare
  --variables
begin
     FOR r IN (SELECT ...) LOOP
        CREATE TEMPORARY TABLE TEST .. ;
        --UPDATE STATEMENTS
        --EXPORT (COPIES TABLE INTO TXT FILE)
        DROP TABLE TEST CASCADE;
     END LOOP;
end$$;
  • Any space allocated for temporary tables will not be released until the transaction is committed (when using on commit delete rows) or even later when the connection is closed (on commit preserve rows which is the default). – a_horse_with_no_name Dec 7 '15 at 19:27
  • Additionally: why are you creating the same table over and over again? Why don't you create it once before the loop? And why do you think you need a temporary table in the first place? What exactly does the "export" part do? – a_horse_with_no_name Dec 7 '15 at 19:27
  • @a_horse_with_no_name What if I create table in the first place,then insert and truncate on loop,would statement still allocate disk ? On export part,there is a copy statement to write the table into a txt file,that's all. – Myra Dec 7 '15 at 19:31
  • It would expect it allocate the space only once and overwrite it in each iteration. But as I hardly ever use temp tables, I am not surel. Why don't you use the select statement that builds the temp table directly in the copy statement? Or use a temporary view instead? – a_horse_with_no_name Dec 7 '15 at 19:48
  • I forgot to mention that before exporting table,there are lots of update statements.I can't select it directly because of the updates. – Myra Dec 7 '15 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.