Re: [PATCHES] [DOCS] Partition: use triggers instead of rules

2007-11-30 Thread David Fetter
On Fri, Nov 30, 2007 at 12:34:05PM +0530, NikhilS wrote:
> Hi,
> 
> Another reason to go along with triggers is that "COPY" honors
> triggers, but does not honor rules. While trying to do bulk inserts
> into a parent of partitioned tables where rules are being employed,
> the COPY operation will not be so straightforward.

Folks,

Does my latest patch attached address this well enough?

Cheers,
David.
-- 
David Fetter <[EMAIL PROTECTED]> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter  XMPP: [EMAIL PROTECTED]

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
Index: doc/src/sgml/ddl.sgml
===
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/ddl.sgml,v
retrieving revision 1.77
diff -c -r1.77 ddl.sgml
*** doc/src/sgml/ddl.sgml   28 Nov 2007 15:42:31 -  1.77
--- doc/src/sgml/ddl.sgml   1 Dec 2007 04:55:46 -
***
*** 2510,2564 

 
  If data will be added only to the latest partition, we can
! set up a very simple rule to insert data. We must
! redefine this each month so that it always points to the
! current partition:
! 
! 
! CREATE OR REPLACE RULE measurement_current_partition AS
! ON INSERT TO measurement
! DO INSTEAD
! INSERT INTO measurement_y2006m01 VALUES ( NEW.city_id,
!   NEW.logdate,
!   NEW.peaktemp,
!   NEW.unitsales );
  
  
  We might want to insert data and have the server automatically
  locate the partition into which the row should be added. We
! could do this with a more complex set of rules as shown below:
  
  
! CREATE RULE measurement_insert_y2004m02 AS
! ON INSERT TO measurement WHERE
! ( logdate >= DATE '2004-02-01' AND logdate < DATE '2004-03-01' )
! DO INSTEAD
! INSERT INTO measurement_y2004m02 VALUES ( NEW.city_id,
!   NEW.logdate,
!   NEW.peaktemp,
!   NEW.unitsales );
! ...
! CREATE RULE measurement_insert_y2005m12 AS
! ON INSERT TO measurement WHERE
! ( logdate >= DATE '2005-12-01' AND logdate < DATE '2006-01-01' )
! DO INSTEAD
! INSERT INTO measurement_y2005m12 VALUES ( NEW.city_id,
!   NEW.logdate,
!   NEW.peaktemp,
!   NEW.unitsales );
! CREATE RULE measurement_insert_y2006m01 AS
! ON INSERT TO measurement WHERE
! ( logdate >= DATE '2006-01-01' AND logdate < DATE '2006-02-01' )
! DO INSTEAD
! INSERT INTO measurement_y2006m01 VALUES ( NEW.city_id,
!   NEW.logdate,
!   NEW.peaktemp,
!   NEW.unitsales );
! 
! 
! Note that the WHERE clause in each rule
! exactly matches the CHECK
! constraint for its partition.
 

   
--- 2510,2589 

 
  If data will be added only to the latest partition, we can
! set up a very simple trigger function to insert data.  We must
! redefine this each month so that it always points to the current
! partition:
! 
! 
! CREATE OR REPLACE FUNCTION measurement_current_partition()
! RETURNS TRIGGER
! LANGUAGE plpgsql
! AS $$
! BEGIN
! INSERT INTO measurement_y2006m01
! VALUES (
! NEW.city_id,
! NEW.logdate,
! NEW.peaktemp,
! NEW.unitsales
! );
! RETURN NEW;
! END;
! $$;
! 
! 
! The first time we create the table, we create a trigger which
! calls the above trigger function.  When we replace the trigger
! function, we don't need to replace the trigger.
! 
! 
! CREATE TRIGGER insert_measurement_current_partition
! BEFORE INSERT
! ON measurement
! EXECUTE PROCEDURE measurement_current_partition();
  
  
  We might want to insert data and have the server automatically
  locate the partition into which the row should be added. We
! could do this with a more complex trigger function as shown
! below:
  
  
! CREATE OR REPLACE FUNCTION measurement_insert()
! RETURNS TRIGGER
! LANGUAGE plpgsql
! AS $$
! BEGIN
! IF ( logdate >= DATE '2004-02-01' AND logdate < DATE '2004-03-01' ) 
THEN
! 
! INSERT INTO measurement_y2004m02
! VALUES (
! NEW.city_id,
! NEW.logdate,
! NEW.peaktemp,
! NEW.unitsales
! );
! ELSIF ( logdate >= DATE '2005-12-01' AND logdate < DATE 
'2006-01-01' ) THEN
! ...
! ELSIF ( logdate >= DATE '2008-01-01' AND logdate < DATE 
'2006-02-01' ) THEN
! INSER

Re: [PATCHES] [DOCS] Partition: use triggers instead of rules

2007-11-30 Thread Simon Riggs
On Thu, 2007-11-29 at 09:52 -0800, Joshua D. Drake wrote:

> In any of the above cases a trigger is going to work better than a
> rule with the exceptions of what TGL pointed out and in simpler
> partitioning environments where the number of partitions are very low.

Agreed to this and in general to JD's points.


Tom's point about Rules being statement-level is valid only when all
rows from an INSERT SELECT go into one partition. If that were the case
then it seems strange to spend lots of time designing a rules
infrastructure anyway.

If you try to do lots of partitions and RULEs then it sucks. I spoke
against their inclusion originally and do so again now. My point then
was you don't need this for most partitioning applications.

What nobody has mentioned is that Triggers suck as well, so neither
Triggers nor Rules should be given centre stage. COPY only makes sense
running into the table you are loading and if you're trying to load
large amounts of data using INSERTs + anything then you need a whack.
Rob did a beautiful de-construction of all of this in Montreal, BTW,
with humour too.

Current PostgreSQL partitioning is not the same as Oracle's and papering
over the cracks doesn't help anybody much. ISTM we should say to people
to use COPY into a named partition for high speed, plus these other
suggestions if you want some fancy logic, but go careful, cos they're
slow.

It would be nice to have an example of using CREATE TABLE LIKE + COPY in
same transaction, then ALTER TABLE ... INHERITS to add the partition
onto the main table. That is now the fastest way in 8.3.

I'll leave it to y'all from here though. 

-- 
  Simon Riggs
  2ndQuadrant  http://www.2ndQuadrant.com


---(end of broadcast)---
TIP 6: explain analyze is your friend