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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:38:48+00:00 2026-05-13T00:38:48+00:00

I would like to replace some of the sequences I use for id’s in

  • 0

I would like to replace some of the sequences I use for id’s in my postgresql db with my own custom made id generator. The generator would produce a random number with a checkdigit at the end. So this:

SELECT nextval('customers')

would be replaced by something like this:

SELECT get_new_rand_id('customer')

The function would then return a numerical value such as: [1-9][0-9]{9} where the last digit is a checksum.

The concerns I have is:

  1. How do I make the thing atomic
  2. How do I avoid returning the same id twice (this would be caught by trying to insert it into a column with unique constraint but then its to late to I think)
  3. Is this a good idea at all?

Note1: I do not want to use uuid since it is to be communicated with customers and 10 digits is far simpler to communicate than the 36 character uuid.

Note2: The function would rarely be called with SELECT get_new_rand_id() but would be assigned as default value on the id-column instead of nextval().

EDIT: Ok, good discussusion below! Here are some explanation for why:

  1. So why would I over-comlicate things this way? The purpouse is to hide the primary key from the customers.

    I give each new customer a unique
    customerId (generated serial number in
    the db). Since I communicate that
    number with the customer it is a
    fairly simple task for my competitors
    to monitor my business (there are
    other numbers such as invoice nr and
    order nr that have the same
    properties). It is this monitoring I
    would like to make a little bit
    harder (note: not impossible but
    harder).

  2. Why the check digit?

    Before there was any talk of hiding the serial nr I added a checkdigit to ordernr since there were klumbsy fingers at some points in the production, and my thought was that this would be a good practice to keep in the future.

After reading the discussion I can certainly see that my approach is not the best way to solve my problem, but I have no other good idea of how to solve it, so please help me out here.

  1. Should I add an extra column where I put the id I expose to the customer and keep the serial as primary key?
  2. How can I generate the id to expose in a sane and efficient way?
  3. Is the checkdigit necessary?
  • 1 1 Answer
  • 2 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-13T00:38:48+00:00Added an answer on May 13, 2026 at 12:38 am

    For generating unique and random-looking identifiers from a serial, using ciphers might be a good idea. Since their output is bijective (there is a one-to-one mapping between input and output values) — you will not have any collisions, unlike hashes. Which means your identifiers don’t have to be as long as hashes.

    Most cryptographic ciphers work on 64-bit or larger blocks, but the PostgreSQL wiki has an example PL/pgSQL procedure for a “non-cryptographic” cipher function that works on (32-bit) int type. Disclaimer: I have not tried using this function myself.

    To use it for your primary keys, run the CREATE FUNCTION call from the wiki page, and then on your empty tables do:

    ALTER TABLE foo ALTER COLUMN foo_id SET DEFAULT pseudo_encrypt(nextval('foo_foo_id_seq')::int);
    

    And voila!

    pg=> insert into foo (foo_id) values(default);
    pg=> insert into foo (foo_id) values(default);
    pg=> insert into foo (foo_id) values(default);
    pg=> select * from foo;
      foo_id   
    ------------
     1241588087
     1500453386
     1755259484
    (4 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my script I check some files and would like to replace a part
I would like to replace the first character ' x ' with the number
As fast as possible, I would like to replace the first zeros in some
I would like to replace symbols with HTMLtags. For example: This :is: some :text:
I would like to replace specific String characters based on some integers value passed
I have URLs with some special characters and i would like to replace them
I have some text contain html tags, I would like to replace all links
I would like to replace drop down list (that has on change event that
I would like to replace blank spaces/white spaces in a string with commas. STR1=This
I would like to replace a part of the image with my image in

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.