Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

443
Visualizações
postgres: Locking row data using python

I have a postgres( PostgreSQL 12.3) db table which consists of 100k rows and I have a python script which reads from this table and perform some action based on the data. I want to run the same script through multiple machines so that data can be processed faster. But when I run from multiple machines, I want to make sure one row is processed by only one machine at a time, basically achieving it by locking that row.

Can you provide some pointers on how locking row can be achieved through python. I am using psycopg2 module to read and update data from table, but did not find a way to lock the row data.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use SELECT ... FOR UPDATE SKIP LOCKED, which performs a row-level lock on any rows that are returned, and skips rows that are already locked by another transaction.

Session 1:

testdb=# create table tt(x text);
CREATE TABLE
testdb=# insert into tt select 'foo';
INSERT 0 1
testdb=# insert into tt select 'bar';
INSERT 0 1
testdb=# BEGIN;
BEGIN
testdb=# SELECT * FROM tt ORDER BY x LIMIT 1 FOR UPDATE SKIP LOCKED;
  x  
-----
 bar
(1 row)

testdb=# -- SELECT in session 2 is performed now.
testdb=# commit;
COMMIT

Session 2:

testdb=# BEGIN;
BEGIN
testdb=# SELECT * FROM tt ORDER BY x LIMIT 1 FOR UPDATE SKIP LOCKED;
  x  
-----
 foo
(1 row)

testdb=# commit;
COMMIT

You won't need to do anything special with psycopg2 for this, as long as your query already has a reasonably small limit (so the SELECT doesn't lock every row), adding FOR UPDATE SKIP LOCKED should do what you're asking for. See the docs on the locking clause for more details.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda