Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3992034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:43:05+00:00 2026-05-20T06:43:05+00:00

I am writing a script that needs to book seats in the cinema. User

  • 0

I am writing a script that needs to book seats in the cinema.

  1. User asks for 2 seats
  2. If there are 2 seats available, system offers them to client
  3. Client can either accept them or request another 2 seats.
  4. When he finally accepts, seats are marked as “sold”

Since there can be multiple users using system simultaneously, I need a way to “lock” the rows offered to current client until certain time passes, or he requests another seats.

Currently I am marking offered seats as “locked” with a client id, and use SELECT to return them to the client (this is for MySQL, but target database is Postgres)

UPDATE seats SET status = "locked", lock_time = NOW(), lock_id = "lock1" LIMIT 2
SELECT * FROM seats WHERE lock_id = "lock1" AND lock_time > DATE_SUB(NOW(), INTERVAL 2 MINUTE)

There is a problem with that: if there’s only 1 seat available, it will still be marked as “locked” and I will have to release lock right away.

I am also pretty sure that there is a smarter way of doing that. What is the correct way of dealing with a task like that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:43:05+00:00Added an answer on May 20, 2026 at 6:43 am

    Race Condition – I think this would be better as an insert rather than an update. Two updates can run at the same time and they will not conflict with each other. If you had ‘locked seats’ table then you could reference the seat_id and make it unique. That way a race conditions would fail. But, in any case, I wrote this as an update as you have in the question although you could change it to an insert.

    It seems like you just do not want to be able to lock the seats in the first place if there are not enough available. This is easy with self joins:

    create temp table seats
    (
        id          serial,
        event_id    integer,
        locked      boolean default false
    );
    insert into seats (event_id) values (1),(1),(1),(2);
    -- this will not lock event_id = 2 since it will not have a high enough count
    update seats
    set locked = true
    from
    (
        -- get the counts so we can drop events without enough seats
        select count(*), event_id from seats group by event_id
    ) as sum,
    (
        -- you can not put limits in update; need to self-join
        select id from seats limit 2
    ) as t
    where sum.event_id = seats.event_id
    and seats.id = t.id
    and count >= 2
    

    ;

    UPDATE 2
     id | event_id | locked 
    ----+----------+--------
      3 |        1 | f
      4 |        2 | f
      2 |        1 | t
      1 |        1 | t
    (4 rows)
    

    So this ‘locks’ two seats for every event that has at least two seats 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a batch script that needs to install Microsoft Lync if the user
I'm writing a bash script that needs to delete old files. It's currently implemented
I'm writing a small shell script that needs to reverse the lines of a
I'm writing a cross-platform python script that needs to know if and where Cygwin
I'm writing a script that needs to be called at a random time during
I'm writing a bash script that needs to loop over the arguments passed into
I'm writing a bash script that needs to parse html that includes special characters
I'm writing a bash script that needs to sudo multiple commands. I can do
hi all I'm writing a simple web crawling script that needs to connect to
I'm writing a perl script that mimics gcc. This my script needs to process

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.