Re: Primary Key Increment Doesn't Seem Correct Under Table Partition

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Yan Cheng Cheok <yccheok(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Primary Key Increment Doesn't Seem Correct Under Table Partition
Date: 2010-01-27 18:44:08
Message-ID: 7E47FCCB-BC6A-4C52-90E9-6A70DE33C4A2@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 27 Jan 2010, at 2:00, Yan Cheng Cheok wrote:
>>> However, whenever I insert row into measurement table,
>> I realize its primary key value is going from 2, 4, 6, 8,
>> 10...
>>>
>>> May I know how can I prevent this?
>>
>> Apparently nextval on that sequence gets called multiple
>> times in your queries.
>>
>
> Do you have any idea why it happens so?

It's something you're doing in your queries that causes this. You haven't provided any info about that, so I can only guess.
The problem could for example be that you're selecting nextval() of the sequence and then don't fill in the value for that column in a subsequent INSERT, causing the default to call nextval() again.

>> Also, is it necessary to create index for
>> measurement_id found in measurement's child table? I am
>> concern on the read speed.

> But...
>
> measurement table does contains
>
> CONSTRAINT pk_measurement_id PRIMARY KEY (measurement_id),
>
> when measurement_0 child table inherits from measurement table, isn't it will inherit PRIMARY KEY (measurement_id)?
>
> Do I still have to create index for measurement_0.measurement_id ?

Ah I see, you were asking about table inheritance. To quote the documentation at http://www.postgresql.org/docs/8.4/interactive/ddl-inherit.html:

"All check constraints and not-null constraints on a parent table are automatically inherited by its children. Other types of constraints (unique, primary key, and foreign key constraints) are not inherited."

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4b60897b10606504295220!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2010-01-27 18:50:28 Re: Primary Key Increment Doesn't Seem Correct Under Table Partition
Previous Message Joe Conway 2010-01-27 17:15:11 PL/R Windows binary for PostgreSQL 8.4.x available for testing