pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Mungall <cjm@fruitfly.org>
To: pgsql-sql@postgresql.org
Subject: Common patterns for 'store' functions
Date: Fri, 26 Aug 2005 17:08:25 -0700 (PDT)
Message-ID: <Pine.OSX.4.58.0508241437580.24231@skerryvore.dhcp.lbl.gov> (raw)


I find store functions fairly useful; eg for any table 'foo', the function

  store_foo(v1,v2,...,vn) returns int;

will perform an insert (if a unique key is not present) or an update (if
the unique key is present)

v1,...,vn may be values for columns in the table foo, or perhaps also for
tables foo_a, foo_b, c_foo related to foo by foreign keys (in a highly
normalised db, it can be convenient to combine these multiple
update/inserts into a single function call).

These functions can be implemented in an application/middleware layer (eg
perl,java,..) or as pgsql functions. Both approaches have their strengths
and weaknesses. For my purposes, sometimes pgsql is preferable. For one
thing, pgsql store functions will be faster since it involves less
client-server I/O.

However, coding these can be fairly tedious. There is the additional
problem that it's generally desirable to provide multiple signatures for
different permutations of v1,...,vn.

This can be unwieldy when a store function has lots of possible values. In
these cases, the application layer approach has a big advantage, as the
arguments to a function call generally don't have to be position
dependent. As far as I am aware, there is no equivalent way to do this in
plpgsql.

Is this a common use case, or do people typically do this in the
application/middleware layer?

If other people are doing this, are there any common design patterns they
would like to share? What about code that helps auto-generate some of
these functions? Or even super-clever middleware that can decide whether
to do it in the application layer or autogenerate some helper
functions...?

Cheers
Chris



Message-ID: <Pine.OSX.4.58.0508241437580.24231@skerryvore.dhcp.lbl.gov>
Permalink:  ../Pine.OSX.4.58.0508241437580.24231@skerryvore.dhcp.lbl.gov/
Also on:    postgresql.org/message-id/Pine.OSX.4.58.0508241437580.24231@skerryvore.dhcp.lbl.gov

 · 

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-sql@postgresql.org
  Cc: cjm@fruitfly.org
  Subject: Re: Common patterns for 'store' functions
  In-Reply-To: <Pine.OSX.4.58.0508241437580.24231@skerryvore.dhcp.lbl.gov>
  References: <Pine.OSX.4.58.0508241437580.24231@skerryvore.dhcp.lbl.gov>

* 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