I am using Postgres12 and DBeaver. I insert thousands of data using a procedure like this :
do $$
declare
ctr_max_ID integer;
ctr_loop Integer := 1;
begin
loop
exit when ctr_loop = 5001;
ctr_loop := ctr_loop + 1;
select max (cast(id as Integer)) into ctr_max_ID from table1 ;
insert into table1 (id, data) values (ctr_max_ID + 1, 'DUMMY data');
raise notice 'ctr_loop: %', ctr_loop;
end loop;
end; $$;
While that procedure running, I issued another select statement "Select * From table1 Where ID = '1';"
This statement is never executed until the procedure finish running. I also run a statement for another table "Select * From table2 Limit 1"
. This is not executed until stored procedure finish running.
Is this normal ? Or is there some setting somewhere that specifies query has to run one at a time ?
thanks
max() + 1
approach is a horrible way to generate a unique id. The main reason being, that this is not safe for concurrent use. A sequence is a much more efficient (and safe) way to do that. Btw: why are you storing numbers in varchar (or text) columns?select * from table1
should be blocked by this approach. I assume there is more going on, than you are showing us.