create table queue (pk bigint primary key, status integer, ts timestamptz);
insert into queue select generate_series(1,1000000), 1, current_timestamp;

create or replace function process_queue()
returns void
language plpgsql
as $$
DECLARE
  idval int;
  statval int;
BEGIN

  SELECT pk, status INTO idval, statval
  FROM queue
  WHERE status = 1
  LIMIT 1
  FOR UPDATE SKIP LOCKED;

  IF FOUND THEN
    UPDATE queue SET status = 2
	WHERE pk = idval;
  END IF;

END;
$$;
