cannot ALTER TABLE because it is being used by active queries in this session

70 Views Asked by At

In a loop based on the results of a query, a table is used in which it is necessary to disable/enable the trigger in the body of the loop,

DO $$
declare
  rec                record; 
begin  
  for rec in (
         SELECT *                 
            FROM  mytable
         )
 loop  
        --Disable trigger
        ALTER TABLE mytable DISABLE TRIGGER mytable_trg;        
         /*....*/        
        --Enable trigger
        ALTER TABLE supr.object_fields_tbl ENABLE TRIGGER mytable_trg;      
 end loop;
 end;
$$ LANGUAGE plpgsql;

but receive an error:

SQL Error [55006]: ERROR: cannot ALTER TABLE "mytable" because it is being used by active queries in this session

How can I use a table in a loop and disable/enable its trigger in the body of the loop?

0

There are 0 best solutions below