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 1lX1qK-0005Uf-F4 for pgsql-novice@arkaria.postgresql.org; Thu, 15 Apr 2021 13:15:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lX1qI-0003Rd-Mh for pgsql-novice@arkaria.postgresql.org; Thu, 15 Apr 2021 13:15:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lX1qI-0003RV-Ef for pgsql-novice@lists.postgresql.org; Thu, 15 Apr 2021 13:15:34 +0000 Received: from mout.gmx.net ([212.227.15.19]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lX1qD-0001s4-36 for pgsql-novice@lists.postgresql.org; Thu, 15 Apr 2021 13:15:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1618492525; bh=47lezEnN9eD3azqtiB4S7E+O60PlEjEVGV28PJawqno=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=BTEBaGIftXZ47uMmKi2AhBOIDAmVSLpQy5oltfJp3GJWlcmZ3PRgtj3s1eAfmKIr9 34GO6+1M7aPz+VPBcHgg68LMq9kP600lK884Y+qWeY0JA+G9LC/Sn8/Sj4l3oklOut a2uq//LxXocYpuoSUCOam86IHpQjCIYezcmZcFcs= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from mail.defcon1.lan ([93.15.31.113]) by mail.gmx.net (mrgmx004 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MKbgE-1lENxR24Ls-00Ktlh; Thu, 15 Apr 2021 15:15:25 +0200 Date: Thu, 15 Apr 2021 15:15:23 +0200 From: Bzzzz To: pgsql-novice@lists.postgresql.org Cc: steve@tusol.co.uk Subject: Re: Advice on foreign key and cascading delete design - postgresql 12.6 Message-ID: <20210415151523.3a59e334@msi.defcon1.lan> In-Reply-To: References: <3190280.1618325238@sss.pgh.pa.us> <20210413180841.3ba6e741@msi.defcon1.lan> <20210414142719.181ffd62@msi.defcon1.lan> Organization: Anyone, anywhere but in banana demokratik republik of france or UERSS. Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:hycB1MWfmP9ds50VOSZ+cJK5ymtcW4yBR59tMPc6wX6EbEE2UcY xDa5wqVZGnu43ViJe3U9YEetmonH9JuPhwy6fxDk/YdxFbdXsjXyc7JMAVYVrq+E6/Sl2Kq 163Ukse9wtfqSbgxUB+ejUdqSaW5w2pZaZULE4cETeJNIZ4Eg92qKaBbgQKvOgqB26OUcSa pJNURmfTAnL17dVY0qQNA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:/llYrKdjduk=:AJi0OQ9C8Z5uv9YmCfniR8 xyKITzgyws3ephejD9mrQ8HfWlPjNNRlVUA36H4r7Tgl6MdxCm1+nJUfAAnTrPo4NQozlzuVV KbhiZQ0zoi8HDyL0hkl0N81ArMZl/+07aLUUazivCLXD6+aPelKtpwQ3wKOoGtb2XrLxuNArN UDLEIpOmDr3NrCFh9agQiJ1ef3K1eROBQWrEWTzA7DtoRAv6bDzxf3hLCUa2duYhT2RD/Ciu9 AOn70d/BrpZ2RuSLO1+ABZXu8qEi9F/gY/v1rmkKthGOwGNbgjEsAxNESKcAtlNU8pkqzIh2I By15imWU0G8chnYlKPKozguaJ9s59HAoVVPQa2KmT7Pyljb9Rr+mVKkn9guRtjM6t+e/nRkvH 0DClC5sx/XmJYhb7kE/vGue5xQt61/mbdRF1PfDQQR3Ucqx3s5mYi7bzcghfeSQUkzjF7b74B NdcsQLvdgKg33YvYuJxBxKmps55Ak6/q+QSrSb11h2TjFJTKlsYY4qPyEC9E+gd9i/dpZnZPM yr4kb4hK6t3eolnRClm19C/Q3wm4vn5SkpMpJP/YxSwW9D6uIbji56Not5xcDwLmGyOdshyER 4PZ5bfiz2T4qIGsrVP1BHMVioGPMurCqlL3mqmSytT5qBoDhH7p3p5AFw2VmDbu/etbOWtsir mSAQugaCgRUisdMA0XZbwIiR6Przwx3UxwwdDjJt5IhZ2xTaOR5JhCf6J5IpKwKqqArCcpIwW 13wOVMVq/khMQOo7jmQrOycfP8dyfqJtramI4e/Ewgiu9y70+/yTEVMjVXuTvhJUZ5bvHCixL n79LUlZgGOPocCpaETLUjwnK7XgS3cyCD7iovdg1kAg+kFjpaci76QZW8Gh9Np3HFQS4RzcTl cAvz0bncqJRhQZ8a6tCkSZe0zjudgNve4zzDIWZWxcjsWZLAN5bIHPo59Ts0Nwo1SZSocrxpx Clv6cY0d9YfgmR1eZoGZhO7j42RIwGQSF/dhzJp3WaWxFDEMT5yH71n+qlDKGIgYgdzymIiLR lmHH6kGCwGkpgeEt8lhdRP3mJ4UMs8HpdYkGBd7lQP1kS1a439MBicIiLR/Qo75iK9U3bNeUB 80CjxEzGSgStp+3rqKYBbm/2S2VJ74sRzOwAUdkdExt4sT0r8S9arr1gDpdHICetaNu3C6Wpm tCslVPOV6SmUnMMB+qY6+9NGQ76OZs3JOgvnX7iS67pHerCp5Y1DMVe6l2BuQGoK7IEfk= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 15 Apr 2021 08:07:20 +0100 "Steve Tucknott (TuSol)" wrote: [2nd sent, the first one wasn't on the ML] > Thanks Jean-Yves, that makes more sense - although I do find > 'normalised' structures slightly frustrating even though I can see that > the structure works. > You say that it's bad design having the foreigntablename on the > dependency as it's not practical if the number of dependent tables is > large, =20 > but why is it any better then having many 'intermediate > main-to- dependency' tables? =20 Because of=E2=80=A6 normalized structure ;-p) Using it, there is no repetition of any kind. > I think I've gone full circle in that I currently control the orphans > via hand cranked code - but I hadn't functionalised it. It seems that > another option is to make the tidy up generic by using the information > schemas in a function that purges all records that carry a > 'foreigntablename' and 'foreignrecno' for the main table. =20 You just read old_table and write new_table with the columns you need (transforming 'foreigntablename' and 'foreignrecno' references into foreign keys references in this process), write the wanted functions and triggers, test them thoroughly in a test DB, triple-check everything's ok, delete the original table and finally, rename the new_table to the old name and re-check the whole shebang. Or you can make a text dump of old_table, modify it with sed and go on with the rest of the process. I would do all of this in a test DB then, when everything is checked, make a dump of it, manually edit this dump to only keep what is needed and apply it to the regular DB. This would avoid downtime and make (almost) inevitable blunders acceptable. HINT: write down the whole procedure sequentially on a large sheet of paper with notes where needed, also write down your functions and triggers, write down the test procedures, triple-check everything's consistent and proceed step by step with no stress. > Thanks again for the time and effort you've put into explaining this > for me. =20 You're welcome. Jean-Yves