X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 201E6D6E7E for ; Fri, 26 Aug 2005 21:09:11 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 31008-05 for ; Sat, 27 Aug 2005 00:09:05 +0000 (GMT) Received: from mail.fruitfly.org (ci.lbl.gov [131.243.192.220]) by svr1.postgresql.org (Postfix) with ESMTP id 60D8DD6D91 for ; Fri, 26 Aug 2005 21:09:04 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.fruitfly.org (Postfix on SuSE Linux eMail Server 3.0) with ESMTP id 6D62D210E89 for ; Fri, 26 Aug 2005 17:08:28 -0700 (PDT) Received: from mail.fruitfly.org ([127.0.0.1]) by localhost (mail.fruitfly.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 14966-03 for ; Fri, 26 Aug 2005 17:08:27 -0700 (PDT) Received: from skerryvore.dhcp.lbl.gov (skerryvore.dhcp.lbl.gov [131.243.195.208]) by mail.fruitfly.org (Postfix on SuSE Linux eMail Server 3.0) with ESMTP id 17BF9210D5D for ; Fri, 26 Aug 2005 17:08:27 -0700 (PDT) Date: Fri, 26 Aug 2005 17:08:25 -0700 (PDT) From: Chris Mungall X-X-Sender: cjm@skerryvore.dhcp.lbl.gov To: pgsql-sql@postgresql.org Subject: Common patterns for 'store' functions Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new using ClamAV at fruitfly.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 required=5 tests=[FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200508/251 X-Sequence-Number: 22479 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