Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZLupZ-0007Qy-8Q for pgsql-sql@arkaria.postgresql.org; Sun, 02 Aug 2015 15:05:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZLupY-0003vY-R7 for pgsql-sql@arkaria.postgresql.org; Sun, 02 Aug 2015 15:05:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZLuoY-00018C-V7 for pgsql-sql@postgresql.org; Sun, 02 Aug 2015 15:04:39 +0000 Received: from arbun.splivalo.hr ([78.47.9.189]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZLuoV-0000Un-B3 for pgsql-sql@postgresql.org; Sun, 02 Aug 2015 15:04:38 +0000 Received: from [192.168.43.19] (unknown [46.188.213.41]) (using TLSv1 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: mario) by arbun.splivalo.hr (Postfix) with ESMTPSA id 1609A120A4F for ; Sun, 2 Aug 2015 17:04:33 +0200 (CEST) Message-ID: <55BE3180.5020600@splivalo.hr> Date: Sun, 02 Aug 2015 17:04:32 +0200 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.8.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Getting the list of foreign keys (for deleting data from the database) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.0 (--) 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 I have a large, in-house built, ERP system that I need to clean up from old/stale data. As all the tables are FK-related I could do 'DELETE FROM' from the top-most table (invoices, or stock documents, or whatever) to remove all data from all the related tables, but that is, of course, extremely slow (The datadir is around 20GB in size, and I need to remove 4/5 of the data from the database - fiscal years 2014, 2013, 2012 and 2011 - only 2015 should remain). Instead of doing DELETE FROM table WHERE date_created < '2015-01-01' I was thinking of doing something like this: SELECT foo_drop_all_constraints(); SELECT * FROM table INTO table_copy WHERE date_created >= '2015-01-01'; DROP TABLE table; ALTER TABLE table_copy RENAME TO table; SELECT foo_restore_all_constraints(); Of course, this is simple if I have only one table, but when there is over 400 tables that are 'linked' with foreign keys, things get a bit complicated. Suppose I have a table_detail that has column table_id which is FK pointing to table(id), I would need to do something like this: SELECT foo_drop_all_constraints(); SELECT * FROM table INTO table_copy WHERE date_created >= '2015-01-01'; SELECT table_detail.* INTO table_detail_copy FROM table_detail JOIN table_copy ON table_detail.table_id = table_copy.id DROP TABLE table; DROP TABLE table_copy ALTER TABLE table_copy RENAME TO table; ALTER TABLE table_detail_copy TO table_detail; SELECT foo_restore_all_constraints(); Now, what am I asking is - is there a tool which would help me find all the _detail tables? I know I could query pg_constraints and similar views but before I go onto hacking into those I'm wondering if there is something that could aid me in doing so. Of course, if this is not the best approach I'd appreciate different views/opinions. Mario -- Mario Splivalo mario@splivalo.hr "I can do it quick, I can do it cheap, I can do it well. Pick any two." -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql