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 8648275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:16:34+00:00 2026-06-12T13:16:34+00:00

OK, I’m making user database in PostgresQL, which I have been using for a

  • 0

OK, I’m making user database in PostgresQL, which I have been using for a month or two, but am not very familiar with. Currently, each user that registers is auto-assigned a unique ID via a PostgresQL sequence. This has always been the practice and has always worked fine, HOWEVER…
I have a request from the client to be able to manually enter users certain IDs. I’m not expecting more than 300 users, so I was reserving IDs 500-600 for manually-entered IDs. (If they register online, they get the auto-incremented number, most likely not over 300. If they need a manual ID, they are given a pre-determined ID between 500 and 600 only.) Yes, I am 99.9999% sure that there will not be more than 300 ‘auto-id’ users.

I would really like to be able to give any manual ID I’d like and then if they register online and the ID is already taken, they get the next available ID. I realize this defeats the purpose of the sequence, however, I’m not sure what my other option is. I only have a certain number of IDs I can give out, so I’d rather not just ‘max+1’, if possible – I’d like to “fill in the gaps”. I’m fairly sure the answer lies somewhere in updating the sequence when an ID is manually entered, but that sounds like a bad idea, not sure why.

If it’s not a great idea, just say – “Hey, you’re a moron. Who put you in charge of a database?” and we’ll all go about our day. Thanks for your time.

  • 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-06-12T13:16:36+00:00Added an answer on June 12, 2026 at 1:16 pm

    I’d probably DROP the sequence completely and ALTER TABLE to remove the default from the column. Then, during INSERT, I’d write something like:

    BEGIN;
    LOCK TABLE users IN EXCLUSIVE MODE;
    INSERT INTO users (user_id, blah, blah)
    VALUES (
       coalesce(
           requested_id_or_null_if_none_supplied,
           (SELECT coalesce(max(user_id),0) FROM users)+1
       ),
       'blah',
       'blah'
    );
    COMMIT;
    

    This has bad concurrency. Exactly one transaction can be inserting a user at any given moment. Given the volumes you’re working with that should be perfectly fine so long as you keep your transactions short.

    Consider adding a new field that’s used as a public display identifier, separate to the database’s internal primary key. Use this field for display for the customer’s purposes. Let them put whatever they want in it, just declare it UNIQUE as part of a suitable unique key.

    If you do that you can keep the underlying number assignment using a sequence the same, so you don’t have to change how you refer to the users elsewhere in the DB. You don’t have to change how any of the rest of the app refers to users internally. When displaying a user identifier, just look up the “user number” for display and use that. When the user enters a user number, look up the primary key of the matching row. The user number is only needed for input/output as an identifier for the end user, it doesn’t get referenced in the rest of the database.

    This is why database/app design wisdom stresses that you should generally not expose generated keys to the user. Eventually, if they can see them they’ll inevitably want to be able to change them with “fun” results. Much easier to give them their own public identifier to play with. If they suddenly decide it should be alphanumeric, but only on Tuesdays with a full moon – that’s OK, you can do that.

    Once you’ve split the “display identifier” and “unique internal row key” into separate things, you’re free to do whatever you want with the display identifier, including easily letting them change it.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article

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.