Re: Table Partitioning: Sequence jump issue 10 in 10 with serial datatype

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: DrakoRod <drakoflames(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Table Partitioning: Sequence jump issue 10 in 10 with serial datatype
Date: 2018-02-14 08:22:11
Message-ID: 77283AFE-9FA4-488B-81CB-09E2512DAC11@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> On 14 Feb 2018, at 2:48, DrakoRod <drakoflames(at)hotmail(dot)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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Konstantin Evteev 2018-02-14 11:21:37 Using standby for read-only queries in production and DML operations on primary.
Previous Message Jeremy Finzel 2018-02-14 05:41:10 Re: pglogical in postgres 9.6