Giter Site home page Giter Site logo

postgres-skip-locked-surprise's Introduction

postgres-skip-locked-surprise

A reproduction of some surprising and possibly buggy behaviour in postgres' skip locked

Explanation

This example is extracted from some real code which demonstrated some surprising behaviour when using SKIP LOCKED which appears to show multiple transactions matching the same data when they should not. It is a fairly minimal scenario, but as the bug only manifests intermittently when running concurrent transactions I've used Go to run the queries as part of a test suite.

The schema itself isn't really optimal for the task at hand, but is inherited from before the code was changed to use SKIP LOCKED to make it act as a queue, and as far as I can tell it should still work as expected.

We have a table of accounts which have associated imports. Each account will eventually have a matching import, but initially begins without one. The relevant code implements queue-like behaviour, with multiple processes looking for accounts with stale data, excluding any accounts which have imports which do not have a status of completed. When a worker identifies accounts which are overdue for an update, the import is set to a pending status (creating a new row if one didn't previously exist) and an RPC call is made to begin processing that import.

My understanding of the combination of FOR UPDATE and SKIP LOCKED should mean that worker processes all select disjoint subsets of rows, and therefore we should never get duplicate accounts. The surprising and possibly buggy behaviour is that we do occasionally get duplicate accounts produced when running a bunch of concurrent workers. I'm unsure whether there is actually a postgres bug here, or whether I've overlooked something in the way these concurrent transactions interact.

The closest thing I've found to an answer for what's going on is that if I remove the requirement that some accounts do not yet have records for imports, change the query to use an INNER join, and include the imports table in the lock - then I can no longer reproduce the problem. However, I'm unable to follow why this makes a difference.

Running

Start a postgres instance

docker compose up -d

Setup the schema

cat setup.sql | docker compose exec -T postgres psql

Run the query, this should match 'one' the first time and 'nothing' after that

cat find_overdue.sql | docker compose exec -T postgres psql

To reproduce the bug requires running find_overdue.sql lots of times at once. We should expect that 'one' only appears once.

Via Go

The go/ subdir contains a program which demonstrates the issue, including the concurrency orchestration.

postgres-skip-locked-surprise's People

Contributors

glenjamin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.