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

193
Visualizações
PostgreSQL Gap lock by SELECT ... FOR UPDATE

There is a query with a Gap Lock used at MySQL/InnoDB:

SELECT id, time, count
FROM table_a
WHERE time
    BETWEEN DATE_SUB(NOW(), INTERVAL 24 HOUR)
    AND NOW()
FOR UPDATE

It locks the time range and returns a recent record if present (during last 24 hrs). If not - the session still owns a lock over the duration of last 24 hours to safely insert a new record.

Is is possible to make the same gap lock for entire 24 hours duration (even if there are no records) in PostgreSQL?

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

0

The way to do that in PostgreSQL is to use the SERIALIZABLE isolation level for all transactions.

Then you don't need the FOR UPDATE at all. PostgreSQL won't prevent rows from being inserted in the gap, but if two transactions both read and write values in the same gap simultaneously, one of them will get a serialization error and have to redo the transaction (on the second try, it will find the gap not empty).

The concept at work here is serializability: it is acceptable if someone else inserts into the gap without reading (that transaction is logically after the one with your SELECT). But if two transactions both find the gap empty and then insert something, that would create an anomaly that is prevented by SERIALIZABLE.

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