pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Collin Peters <cadiolis@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Insert into VIEW using RULE. Not possible to use nextval()?
Date: Thu, 3 May 2007 10:04:26 -0700
Message-ID: <df01c91b0705031004vbedf4d8h8c76a102617e9ccb@mail.gmail.com> (raw)

I am having the same problem that is documented elsewhere in the
archives.  Namely when you have a INSERT RULE on a VIEW the nextval()
function doesn't behave properly (or it doesn't behave how I'd like it
to).

http://archives.postgresql.org/pgsql-sql/2003-07/msg00333.php

I'm wondering if anything has changed for this in regards to best
practices?  Suggested solutions are to change to a trigger or use
currval() for your secondary INSERTS inside the RULE.

A trigger does not apply to my case as I am basically using this as a
shortcut to manually doing two INSERTs.  Is there any problems with
using the currval() approach?  If I use that within the same call is
there any chance of that not returning the correct value? (e.g. if
this INSERT RULE is being called a 1000 times at once, is it
guaranteed to be correct?

Another option I see is to place the INSERT inside a LOOP.  For
example instead of:
INSERT INTO 	user_activity_single(user_activity_id, activity_date,
user_activity_type_id, user_activity_action_id, user_id,  div1)
SELECT		nextval('user_activity_user_activity_id_seq'), etc....

have:

FOR mviews IN 	
  SELECT nextval('user_activity_user_activity_id_seq') as id,
CURRENT_DATE, 1, 2, 27, 'foo'
LOOP
    INSERT INTO 	user_activity_single(mviews.id, etc...)
END LOOP;

Performance wise this doesn't seem as good.  In my case the SELECT
statement would be around 4000 records.

Any tips for me?

Regards,
Collin Peters



view thread (6+ messages)  latest in thread

Message-ID: <df01c91b0705031004vbedf4d8h8c76a102617e9ccb@mail.gmail.com>
Permalink:  ../df01c91b0705031004vbedf4d8h8c76a102617e9ccb@mail.gmail.com/
Also on:    postgresql.org/message-id/df01c91b0705031004vbedf4d8h8c76a102617e9ccb@mail.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: cadiolis@gmail.com
  Subject: Re: Insert into VIEW using RULE. Not possible to use nextval()?
  In-Reply-To: <df01c91b0705031004vbedf4d8h8c76a102617e9ccb@mail.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