X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id EFF059DCCA9; Sun, 11 Dec 2005 00:41:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 53984-03; Sun, 11 Dec 2005 00:41:26 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 5DE5D9DCC47; Sun, 11 Dec 2005 00:41:24 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id jBB4fJen082105 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 10 Dec 2005 21:41:22 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id jBB4fJIB007391; Sat, 10 Dec 2005 21:41:19 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id jBB4fJeV007390; Sat, 10 Dec 2005 21:41:19 -0700 (MST) (envelope-from mfuhr) Date: Sat, 10 Dec 2005 21:41:18 -0700 From: Michael Fuhr To: "Foster, Stephen" Cc: pgsql-general@postgresql.org, pgsql-sql@postgresql.org Subject: Re: Looking for information on PostgreSQL Stored Procedures Message-ID: <20051211044118.GA7156@winnie.fuhr.org> References: <43949F37.4000000@gmail.com> <006001c5fdff$5919d480$2101a8c0@cfgod> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <006001c5fdff$5919d480$2101a8c0@cfgod> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.026 required=5 tests=[AWL=0.026] X-Spam-Score: 0.026 X-Spam-Level: X-Archive-Number: 200512/523 X-Sequence-Number: 88086 On Sat, Dec 10, 2005 at 09:02:39PM -0600, Foster, Stephen wrote: > I did see last week something on PLPGSQL and read through that. But > there has to be something out there that goes in depth on the > SQL/Function command set(Speaking of functions/procedures). The standard functions are described in the "Functions and Operators" chapter of the documentation. Here's a link to the latest version, but use the documentation for the version you're running: http://www.postgresql.org/docs/8.1/interactive/functions.html SQL functions are documented in "Query Language (SQL) Functions": http://www.postgresql.org/docs/8.1/interactive/xfunc-sql.html > The biggest hole that I have; seems to be on Cursors; define and > opening. I think the fetching and closing is pretty straight forward. > But the Define and opening is causing some grief. Either I'm making to > far too hard or I'm really have missing something silly. What problems are you having? Without seeing what you're doing in PostgreSQL it's difficult to say what's wrong. In PL/pgSQL you can loop through query results without explicitly using a cursor; see "Looping Through Query Results": http://www.postgresql.org/docs/8.1/interactive/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING [snip example] > This is an example of the simple stored procedures like the ones I'm > trying to migrate. PLPGSQL is ok but I thought it would run better in > SQL. SQL doesn't have control structures, so if you need conditionals or loops then use a procedural language like PL/pgSQL or implement the logic on the client side. For the example you posted, the following query should have the same effect (remove records with duplicate names, if I'm reading it right): DELETE FROM mailinglist WHERE id NOT IN ( SELECT DISTINCT ON (name) id FROM mailinglist ORDER BY name, id ); I don't know how well this would perform on large data sets, especially in older versions of PostgreSQL, but you could try it. I'd recommend trying it first on a test table or in a transaction that you can roll back in case it doesn't do what you want. See the SELECT documentation for a description of the non-standard DISTINCT ON clause that the above query uses: http://www.postgresql.org/docs/8.1/interactive/sql-select.html -- Michael Fuhr