sorry if my question has been asked before or if it's too obvious but i really need to clear this. thanks for your help.
in a multi-user interface, if the same transaccion from different users arrive to server at the same time, what will happen?
i have the next table:
create table table_x (
cod char(2) not null,
desc varchar(45) not null,
primary key (cod)
);
where these calls arrive at the same time to the server:
call sp_s('1','a');
call sp_s('1','b');
call sp_s('1','c');
what sp_s (in param_a char(2), in param_b varchar(45)) does is:
declare var_count tinyint default 0;
set var_count=
(select count(*)
from table_x
where cod=param_a);
if(var_count=0) then
insert into table_x values (param_a, param_b);
else
update table_x set desc=param_b
where cod=param_a;
end if;
if the registry doesn't exists it inserts it, but if it exists the sp updates it.
is this a good way to do it without using autoincrement? whats the best way to avoid concurrency if i'm not using auto_increment?
thanks very much for your time.
pg_advisory_lockto overcome such issues. Before select I obtain the advisory lock by integer value I'm working with, then I'm sure the block isn't executed simultaneously.