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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:54:46+00:00 2026-05-13T18:54:46+00:00

When I use a rewrite rule that splits an insert into one table into

  • 0

When I use a rewrite rule that splits an insert into one table into inserts to two other tables where one of the inserted values has as default nextval(‘some_sequence’) with the same sequence for both tables, then the inserted default values are different in the two tables. This is probably due to simple text replacement by the rewrite rule. I had hoped instead that the default value would be first resolved and then the same value be written to both tables.

Here an example (as you probably guess, I’m trying to implement specialization/generalization using rules):

-- first and third commands can be skipped if id is defined as serial
create sequence parents_id_seq;
create table Parents(
  id integer default(nextval('parents_id_seq')) primary key,
  type varchar(50) not null check(type in ('Child1', 'Child2')),
  unique (id, type),
  attribute1 varchar(50) not null unique check(length(attribute1) > 0)
);
alter sequence parents_id_seq owned by parents.id;

The data specific to children of the first kind is kept in

create table Partial_Children1(
  id integer default(nextval('parents_id_seq')) primary key,
  type varchar(50) not null check(type = 'Child1'),
  foreign key (id, type) references Parents(id, type),
  attribute2 varchar(50) not null check(length(attribute2) > 0)
);

Next I defined a view Children1 that joins both tables above (I rewrote the view by stating explicitly what PostgreSQL does to define views according to the documentation)

create table Children1(
  id int default(nextval('parents_id_seq')),
  type varchar(50) not null check(type in ('Child1')),
  attribute1 varchar(50) not null check(length(attribute1) > 0),
  attribute2 varchar(50) not null check(length(attribute2) > 0)
);
create rule "_RETURN" as on select to Children1 do instead
  select p.*, c.attribute2
  from Parents p
    join Partial_Children1 c
      on p.id = c.id;

Finally the rewrite rule I’m having problems with:

create rule ct_i_children1 as
  on insert to Children1
  do instead (
    insert into Parents(attribute1, type)
      values(new.attribute1, 'Child1');
    insert into Partial_Children1(attribute2, type)
      values(new.attribute2, 'Child1');
  );

Trying to insert data with

insert into Children1 (attribute1, attribute2)
  values ('a1', 'a2'),
         ('b1', 'b2');

yields the error message

ERROR:  insert or update on table "partial_children1" violates foreign key constraint "partial_children1_id_fkey"
DETAIL:  Key (id,type)=(3,Child1) is not present in table "parents".

A way to solve this is replacing the second insert of the rewrite rule by

insert into Partial_Children1(id, attribute2, type)
  select p.id, new.attribute2, p.type
    from Parents p
    where p.attribute1 = new.attribute1

but this relies on the uniqueness of attribute1, which I don’t want to impose. Another solution would be to insert the values first into an temporary table, and then to select twice from there for the insertions into the two tables. But I don’t like it because of performance reasons.

Does anyone have another idea how to get the same default values in both tables (just using rules and not triggers)?

  • 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-05-13T18:54:47+00:00Added an answer on May 13, 2026 at 6:54 pm

    From the docs
    http://www.postgresql.org/docs/8.4/static/rules.html

    It (The Rule system) modifies queries
    to take rules into consideration, and
    then passes the modified query to the
    query planner for planning and
    execution

    so it first rewrites the queries without executing anything.

    you can make it work when you do not insert multipe records at once:

    create or replace rule ct_i_children1 as
      on insert to Children1
      do instead (
        insert into Parents(id, attribute1, type)
          values(nextval('parents_id_seq'), new.attribute1, 'Child1');
        insert into Partial_Children1(id, attribute2, type)
          values(currval('parents_id_seq'), new.attribute2, 'Child1');
      );
    

    Then you can do:

    insert into Children1 (attribute1, attribute2) values ('a1', 'a2');
    insert into Children1 (attribute1, attribute2) values ('b1', 'b2');
    

    but not

    insert into Children1 (attribute1, attribute2)
      values ('a1', 'a2'),
             ('b1', 'b2');
    

    So you really should not use the rules system with tricky currval() calls.

    Additionally take a look at the comments on these pages:

    • http://www.postgresql.org/docs/8.2/interactive/rules-update.html
    • http://archives.postgresql.org/pgsql-sql/2004-10/msg00195.php
    • http://archives.postgresql.org/pgsql-general/2009-06/msg00278.php

    Another tip: the support at the postgresql mailing list is as excellent as the database engine itself!

    And by the way: do your know that postgresql has support for inheritance out-of-the-box?

    • http://www.postgresql.org/docs/8.4/interactive/ddl-inherit.html

    Summary: you should use triggers or avoid multiple row inserts!

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

Sidebar

Ask A Question

Stats

  • Questions 318k
  • Answers 318k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer <?php $simpleXml = simplexml_load_string($xml); // or load your file, whatever… May 13, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer i'm not sure but this might work... $('a.form_submit').click(function(event) { var… May 13, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer I would rely on the convention that functions that will… May 13, 2026 at 11:51 pm

Related Questions

I have a file foo.bar.1 on my server and when I try to access
I'm moving a website to Wordpress and simultaneously changing the convoluted URL structure they
I have this rewrite rule that turns foo.com/test (or foo.com/test/) into foo.com/test.txt: RewriteRule ^test/?$
I have a site that needs to use mod_rewrite to direct traffic to www.example.com
I'm trying to use IIS Isapi Rewrite to do the following... I need seo-friendly

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.