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

  • SEARCH
  • Home
  • 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 7684789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:06:30+00:00 2026-05-31T19:06:30+00:00

I have a table called ticket , and it has a field called number

  • 0

I have a table called ticket, and it has a field called number and a foreign key called client that needs to work much like an auto-field (incrementing by 1 for each new record), except that the client chain needs to be able specify the starting number. This isn’t a unique field because multiple clients will undoubtedly use the same numbers (e.g. start at 1001). In my application I’m fetching the row with the highest number, and using that number + 1 to make the next record’s number. This all takes place inside of a single transaction (the fetching and the saving of the new record). Is it true that I won’t have to worry about a ticket ever getting an incorrect (duplicate) number under a high load situation, or will the transaction protect from that possibility? (note: I’m using PostgreSQL 9.x)

  • 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-31T19:06:31+00:00Added an answer on May 31, 2026 at 7:06 pm

    without locking the whole table on every insert/update, no. The way transactions work on PostgreSQL means that new rows that appear as a result of concurrent transactions never conflict with each other; and thats exactly what would be happening.

    You need to make sure that updates actually cause the same rows to conflict. You would basically need to implement something similar to the mechanic used by PostgreSQL’s native sequences.

    What I would do is add another column to the table referenced by your client column to represent the last_val of the sequence’s you’ll be using. So each transaction would look sort of like this:

    BEGIN;
    
    SET TRANSACTION SERIALIZABLE;
    
    UPDATE clients 
      SET footable_last_val = footable_last_val + 1 
      WHERE clients.id = :client_id;
    
    INSERT INTO footable(somecol, client_id, number) 
      VALUES (:somevalue, 
              :client_id, 
              (SELECT footable_last_val 
                 FROM clients 
                 WHERE clients.id = :client_id));
    
    COMMIT;
    

    So that the first update into the clients table fails due to a version conflict before reaching the insert.

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

Sidebar

Related Questions

I have a table called Orderrow. Orderrow has a a compound primary key (CPK)
I have table called posts in my DB. Each post has field called social_network
I have a table called ApprovalTasks... Approvals has a status column I also have
I have a table called BlogPost which has a 1-to-many relationship with the Comment
I have a table called ProjectRegion. It has two columns, an id and a
I have table called Direccion other called Cliente each one defined like this public
In my db I a have table called users. In that table I have
I have a table called ticket in which I want to swap two rows
I have table called FinalForgotten which only contains one field called aname. The field
Have a table called person. which has personid and contactid. if a person is

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.