Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U9Cft-00070S-Ch for pgsql-sql@arkaria.postgresql.org; Sat, 23 Feb 2013 10:49:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U9Cfs-0002R1-Kx for pgsql-sql@arkaria.postgresql.org; Sat, 23 Feb 2013 10:49:48 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U9Cfq-0002Qt-Kv for pgsql-sql@postgresql.org; Sat, 23 Feb 2013 10:49:46 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U9Cfn-0003aa-Co for pgsql-sql@postgresql.org; Sat, 23 Feb 2013 10:49:46 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r1NAnfYQ024978; Sat, 23 Feb 2013 05:49:41 -0500 (EST) From: Tom Lane To: Ashwin Jayaprakash cc: pgsql-sql@postgresql.org Subject: Re: Update HSTORE record and then delete if it is now empty - What is the correct sql? In-reply-to: References: Comments: In-reply-to Ashwin Jayaprakash message dated "Fri, 22 Feb 2013 14:46:47 -0800" Date: Sat, 23 Feb 2013 05:49:41 -0500 Message-ID: <24976.1361616581@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Ashwin Jayaprakash writes: > Hi, here's what I'm trying to do: > - I have a table that has an HSTORE column > - I would like to delete some key-vals from it > - If after deleting key-vals, the HSTORE column is empty, I'd like to > delete the entire row > with update_qry as( > update up_del as r > set data = delete(data, 'c=>678') > where name = 'cc' > returning r.* > ) > delete from up_del > where name in (select name from update_qry) > and array_length(akeys(data), 1) is null; > *Q1: *That DELETE statement does not work Nope, it won't, because a single query can only update any particular table row once, and the DELETE plus its WITH clauses is still only a single query. If you want "no empty hstore values" to be an invariant of your data structure, then expecting every update query to implement that correctly seems like a pretty bad idea anyway. Consider using a trigger to do that, ie something like BEFORE UPDATE FOR EACH ROW DO "if new hstore value is null then delete the row and return null". A problem with that approach is that the returned count of updated rows won't be very meaningful, and RETURNING values likewise. If that's a problem for you, you could use an AFTER trigger instead, which will be a little slower but it hides the deletes behind the scenes. (Note: a DELETE issued in a trigger is a separate query, which is why it doesn't fall foul of the limitation your WITH query did.) regards, tom lane -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql