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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:05:24+00:00 2026-06-17T08:05:24+00:00

I am puzzled by a weird Postgres problem I encounter in the trivial database

  • 0

I am puzzled by a weird Postgres problem I encounter in the trivial database shown below: If I first insert a tag and explicitly specify its ID and then try to insert another tag without passing an ID, then this second insert fails. If I try a third time (again without ID), the insert succeeds.

DROP DATABASE IF EXISTS mydb;
CREATE DATABASE mydb;

\c mydb

DROP SCHEMA public;
CREATE SCHEMA core;

CREATE TABLE core.tag
(
    id serial PRIMARY KEY,
    title text NOT NULL
);

-- this works: all columns specified explicitly
INSERT INTO core.tag(id, title) VALUES (1, 'known tag');

-- omitting the tag ID fails with
-- ERROR:  duplicate key value violates unique constraint "tag_pkey"
-- DETAIL:  Key (id)=(1) already exists.
INSERT INTO core.tag(title) VALUES ('unknown tag');

-- this works again ?!?
INSERT INTO core.tag(title) VALUES ('unknown tag');

The issue only seems to occur on a freshly created database and once it does, it does not seem to happen again. I have never come across anything like this – so far, I have just inserted data with or without explicit ID and AFAICS, nothing ever failed like this…

Does anyone have an idea what’s going on here ?!?

Environment: PostgreSQL 9.1.3 on Mac OSX 10.7.5

  • 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-17T08:05:25+00:00Added an answer on June 17, 2026 at 8:05 am

    Of course this fails.

    What happens?

    When you create the table, a sequence is also created that generates the values for the ID column. The sequence starts with 1 but it is only used if you do not specify a value for the ID column.

    Now when you run

    INSERT INTO core.tag(id, title) VALUES (1, 'known tag');
    

    you bypass Postgres’ automatic assigment of the ID value, the sequence “stays” at one.

    Now when you run

    INSERT INTO core.tag(title) VALUES ('unknown tag');
    

    Postgres takes the next value from the sequence – which is 1. But that alreay exists so the insert fails. After taking the value from the sequence, the next value is 2, so the subsequent insert without specifying an ID value gets the 2 and succeeds.

    The solution is to either never include the ID column in your inserts. Or – if you do – request the ID from the sequence:

    INSERT INTO core.tag(id, title) VALUES (nextval('tag_id_seq'), 'known tag');
    

    When a serial column is created it is automatically associated with a sequence which is named <table_name>_<column_name>_seq. And that’s the name I used in the above statement.

    More details about how the serial “data type” works are in the manual: http://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL

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

Sidebar

Related Questions

I have been puzzled by this weird problem and it got me stuck. Please
I have some weird problem in rendering some xhtml in IE 8 http://img709.imageshack.us/i/scrj.png/ .
Puzzled... after executing the 10 lines of code below, elem1 is defined, and elem2
I'm puzzled by this problem when writting an ARM assembly simulator in C. I've
I am puzzled with the statement that IE does not support <q> tag but
I am puzzled because I cannot figure where my bug/problem is. I have a
Some weird problem is haunting me right now: One of my apps (not yet
So, I'm puzzled by a really weird crash occurring in my app. Background :
I have this weird problem that's killing us. I have a widely used app
I'm puzzled by minidom parser handling of empty element, as shown in following code

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.