Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pNz3X-00089S-DG for pgsql-admin@arkaria.postgresql.org; Fri, 03 Feb 2023 16:36:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pNz3W-0002sH-1u for pgsql-admin@arkaria.postgresql.org; Fri, 03 Feb 2023 16:36:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pNz3V-0002qk-LL for pgsql-admin@lists.postgresql.org; Fri, 03 Feb 2023 16:36:53 +0000 Received: from mail-oa1-x29.google.com ([2001:4860:4864:20::29]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pNz3P-0005xZ-BX for pgsql-admin@lists.postgresql.org; Fri, 03 Feb 2023 16:36:53 +0000 Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-169ba826189so7289715fac.2 for ; Fri, 03 Feb 2023 08:36:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=OagHsu7raoAVLH+GBJj2GlZLu5fOd+h5qN5w7utKi7E=; b=KysWcMAiJO1xO46CO/qXBi2/ajddjsrTfrlDjqzFbZ6TmeUa50lyWr5YPOcPzk4Ee2 UX+0nup++imAFfZ5g2bznji/gzzT8WcBw4Uc40bFpWRRaSdmYHx3ofbT2Wtcpvixnmrl VIo7Iu0u/I5vNbPDfoPsue8TIuFZhyy7p7pmG4VvtXkULnjDsHhyhU3LhmzQXLz+C200 UXqhZ4sj69J5487ItDut1+FSbIQj0pdxH6dOqnQ8SyTnu+e8qKp9SHAw6gaSk2+/c+k5 0QU3Iqb0dM59OaCFi4h2QMy/REDEl368Xl0gBDjCh6nQHM1dN+Cw9jSPe2i8AcP/q37o HIRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=OagHsu7raoAVLH+GBJj2GlZLu5fOd+h5qN5w7utKi7E=; b=i7SV/qkH4w+jiwELn7GsqKrZyEz/NMmbtUrV5IxrggOzcNZMHSOEiBsL45+U2/z08U 56J0clIHXDTPnJRH+l8RPOxNFM9JyBYFlxwM5hk1x/nOv8BTc7lZm8tfZveJ0TEXi+Uo EK8IGyy0mMgQXg5qhFxhrvhlvIFz6gRO9I24dFRbbhlhGL4Zw8RzteWRYrIfx62qTSx2 c2klUY4NRzcrU/mJOj6NpiAmlamuixGenS6qL2uxwQpLB/L1/7BsDLyZ4CVhjoqMqUJ5 nlmmJymmg8nvPU5F2J5DTSVa/rjXhcwZDKA0yGDGZeoNTgxZ6uGhzIQ0Lw2g/VFdteau +j1A== X-Gm-Message-State: AO0yUKV8B/Gp7Eqi7017NQcO3Ppj7jHg1xy4kkyU8YXod2EPrKNkt/cJ Rq7DBC50fz8CdZ7RNg1cC4iK0bcTmUo= X-Google-Smtp-Source: AK7set+kcqk34axmff0/cEr0tZxNZPWunRIzM5/LaAHw+W8BW46KA53ir7dj+wCRjfU57FPfVpFKSQ== X-Received: by 2002:a05:6870:41d4:b0:15b:8856:f0cb with SMTP id z20-20020a05687041d400b0015b8856f0cbmr7315666oac.57.1675442205062; Fri, 03 Feb 2023 08:36:45 -0800 (PST) Received: from ?IPV6:2600:1700:bd2c:b810::43? ([2600:1700:bd2c:b810::43]) by smtp.googlemail.com with ESMTPSA id m13-20020a4add0d000000b00500c1100651sm1119269oou.45.2023.02.03.08.36.44 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 03 Feb 2023 08:36:44 -0800 (PST) Content-Type: multipart/alternative; boundary="------------JipaWwwy5i4WLUTtearrCzj5" Message-ID: Date: Fri, 3 Feb 2023 10:36:43 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: Archiving old data when foreign keys involved Content-Language: en-US To: pgsql-admin@lists.postgresql.org References: From: Ron In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------JipaWwwy5i4WLUTtearrCzj5 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2/3/23 10:11, Albin Ary wrote: > Hello, > I am writing to ask for your advice regarding the deletion of old data > when foreign keys are involved. > > I have this database which has quite a complex schema (generated by ORM), > very normalized with a lot of foreign key relationships for some tables. > > So the situation is that some certain tables reference columns from other > tables and also have columns which are being referenced from certain other > tables. > > Because some tables are getting big and this is affecting the overall > performance (mainly insert performance) I was thinking of creating some > denormalized tables in which I will keep the necessary relevant data and > delete older records afterwards. This is ok for the app from a business > point of view, but technically I am not sure how it could be tackled. > > Is there any tool which might help with this kind of situation? I found an information_schema query on stackexchange which got me 95% of the way to the correct FK dependency chain.  Using that, some trial and error, I developed an order by which to delete from tables. Sadly and distressingly, I can't find it anymore.  But it /does/ mean that what you want /can and has been/ done before. -- Born in Arizona, moved to Babylonia. --------------JipaWwwy5i4WLUTtearrCzj5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit On 2/3/23 10:11, Albin Ary wrote:
Hello, 
I am writing to ask for your advice regarding the deletion of old data when foreign keys are involved. 

I have this database which has quite a complex schema (generated by ORM), very normalized with a lot of foreign key relationships for some tables. 

So the situation is that some certain tables reference columns from other tables and also have columns which are being referenced from certain other tables. 

Because some tables are getting big and this is affecting the overall performance (mainly insert performance) I was thinking of creating some denormalized tables in which I will keep the necessary relevant data and delete older records afterwards. This is ok for the app from a business point of view, but technically I am not sure how it could be tackled. 

Is there any tool which might help with this kind of situation?

I found an information_schema query on stackexchange which got me 95% of the way to the correct FK dependency chain.  Using that, some trial and error, I developed an order by which to delete from tables.

Sadly and distressingly, I can't find it anymore.  But it does mean that what you want can and has been done before.

--
Born in Arizona, moved to Babylonia.
--------------JipaWwwy5i4WLUTtearrCzj5--