pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: DrakoRod <drakoflames@hotmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Table Partitioning: Sequence jump issue 10 in 10 with serial datatype
Date: Wed, 14 Feb 2018 09:22:11 +0100
Message-ID: <77283AFE-9FA4-488B-81CB-09E2512DAC11@gmail.com> (raw)
In-Reply-To: <1518572939820-0.post@n3.nabble.com>
References: <1518568026195-0.post@n3.nabble.com>
	<CAKFQuwajkSW07XY-xmjOkkd40zNfaGGQ2sBJNkagwJRsiGBLDA@mail.gmail.com>
	<1518572939820-0.post@n3.nabble.com>


> On 14 Feb 2018, at 2:48, DrakoRod <drakoflames@hotmail.com> wrote:

> CREATE OR REPLACE RULE inserts_customer_part1
> AS ON INSERT TO customers
> WHERE new.id < 10000
> DO INSTEAD  INSERT INTO customers_part1 SELECT NEW.*;
> 
> CREATE OR REPLACE RULE inserts_customer_part2
> AS ON INSERT TO customers
> WHERE new.id >= 10000 AND new.id < 20000
> DO INSTEAD  INSERT INTO customers_part2 SELECT NEW.*;

Here's your problem. Rules substitute values. Since you didn't provide an id in your insert, the id column gets substituted by the default value, which happens to call nextval. You have 3 references to new.id in your rules, so the sequence increments by 3.

That's one of the reasons people usually advise to use triggers & procedures instead of rules.

> dd=# SELECT * FROM customers;                                                 
> id |  name   | other_data 
> ----+---------+------------
>  3 | XXXXXXx | YYYYYYYYYY
>  7 | XXXXXXx | YYYYYYYYYY
> 11 | XXXXXXx | YYYYYYYYYY
> 15 | XXXXXXx | YYYYYYYYYY
> 19 | XXXXXXx | YYYYYYYYYY
> 23 | XXXXXXx | YYYYYYYYYY
> (6 rows)

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





view thread (5+ messages)  latest in thread

Message-ID: <77283AFE-9FA4-488B-81CB-09E2512DAC11@gmail.com>
Permalink:  ../77283AFE-9FA4-488B-81CB-09E2512DAC11@gmail.com/
Also on:    postgresql.org/message-id/77283AFE-9FA4-488B-81CB-09E2512DAC11@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, drakoflames@hotmail.com
  Subject: Re: Table Partitioning: Sequence jump issue 10 in 10 with serial datatype
  In-Reply-To: <77283AFE-9FA4-488B-81CB-09E2512DAC11@gmail.com>
  References: <1518568026195-0.post@n3.nabble.com> <CAKFQuwajkSW07XY-xmjOkkd40zNfaGGQ2sBJNkagwJRsiGBLDA@mail.gmail.com> <1518572939820-0.post@n3.nabble.com> <77283AFE-9FA4-488B-81CB-09E2512DAC11@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox