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 1lQ67Q-0000V8-0Q for pgsql-admin@arkaria.postgresql.org; Sat, 27 Mar 2021 10:24: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 1lQ67O-0002N6-Uv for pgsql-admin@arkaria.postgresql.org; Sat, 27 Mar 2021 10:24:34 +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 1lQ67O-0002Mz-Nc for pgsql-admin@lists.postgresql.org; Sat, 27 Mar 2021 10:24:34 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lQ67M-00061H-Ga for pgsql-admin@lists.postgresql.org; Sat, 27 Mar 2021 10:24:34 +0000 Received: by mail-ej1-x62b.google.com with SMTP id r12so12123952ejr.5 for ; Sat, 27 Mar 2021 03:24:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=CSq1UNM5nUM3N1bLOlwSsR37e2EOqMjK4UNm0/Sk50Q=; b=DBhKNazAfMqDWZziJRwDgmxRE26veicuWEJlYOR3V5NE4nW3xSN13TZnV306LnVY0N p9uMDzKjx0yXJNSwiyQ1Fdrld3WlpRsr2mN8GhM/5/Q2LdeCYr4ps1U5xmKRjqcwKBGm hqewhRodoCbzK9SgO8IKXbVIhOytxlnSbvY0SMQJ47WI9mOn73CVgT3n4j1YppZeDh46 PIbEl7/gbnhX2PGVruRFd7xejmCsGC8iwn5Ses/N05RU1qEB4gSu5dTEIC5cACZVUF4D /hdEOKO2oY/6ec5skaZROCT50lZu03uJZe1ip17IrQwH09sw7WlyJymn+kwGxDPt8FO6 W2/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=CSq1UNM5nUM3N1bLOlwSsR37e2EOqMjK4UNm0/Sk50Q=; b=WE3j/F0L9r79yACq1AmNanIxgsfHfpI+NbTcnOY8RgYGl4EELnHnVTH+u6RzRaEOqv 7Yn0mh26R3DT96k4Quw7kX5ZOFEyr1OBiVA6yykxKcp1uaMcXxaFlwcb04oZSSL8N1qe 3oWSAFgtsOQdy+xZyq/Ea7u7danS6OY+kAqE+L8LnFx0y3CuvIDNB3YMkQJdtjdquhYy G8spJh0nTVGvtWYcAaqaCTLO/UA8soNT76O66zF+ikbrV8zwLPIyq9mU5ny3x0PrrqdF QQgMu48gZvWvLdZdG63ni+XzlwrvaqTxzABIP45BqX1fLrovGj2x4N2MV0uXhr8tosBt iI0g== X-Gm-Message-State: AOAM5335cxtbAd8v1uKcZ5u5ogrEx2YpgH9SWXHu0m9pr5nQ6haEzZoI IdHd1hIGpHXE54dj26ZKtH/r3w== X-Google-Smtp-Source: ABdhPJyd+54Cmvn5Um/063aHj4uxVL5aIgiuBDMKXoGCPWnCWEpySrMOWZ6Sgm/ue6tTCWV9/ZjHTw== X-Received: by 2002:a17:906:a049:: with SMTP id bg9mr19589991ejb.186.1616840671625; Sat, 27 Mar 2021 03:24:31 -0700 (PDT) Received: from localhost.localdomain (62-46-255-104.adsl.highway.telekom.at. [62.46.255.104]) by smtp.gmail.com with ESMTPSA id bc16sm1119546edb.17.2021.03.27.03.24.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Mar 2021 03:24:31 -0700 (PDT) Message-ID: Subject: Re: Native Foreign Keys housekeeping time intensive for Relational Model From: Laurenz Albe To: Jacque Edmund , pgsql-admin@lists.postgresql.org Date: Sat, 27 Mar 2021 11:24:30 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2021-03-26 at 14:00 -0400, Jacque Edmund wrote: > I'm looking at a herculean project before me. > > I've already seen that my current set of FK relationships between < 100 tables and data footprint > has yielded very large lookup times for the underlying referential integrity of the FKs to vet > whether it can DELETE a row or not based on its FK relationships. That same underlying vetting > is performing a lot of "SELECT 1" statements across the related tables and its squeezing timelines. > > I have indexes on all the requisite columns participating in FKs. I don't think theres another > way of speeding up lots of row DELETEs from the particular way I have the relationship implemented > and I'm denying myself the "set session_replication_role = 'replica'" method so that I can be fully > operational without the postgres userid (aka portable to cloud environments). > > Correction. Actually, I'm thinking I can take out the system implementation of FK's and use > constraint triggers instead. That way I'd get to relax the triggers and gut the vetting that, > with my wield relationships and data, is time intensive. > > Maybe even keep postgres as the owner of the constraint triggers so they can't be hacked by app users. > > I'm guessing that, while not often, this approach happens to folks every now and then? I think that is all wrong. Foreign keys are a good thing, and the idea to get rid of them out of performance considerations is usually premature "optimizazion" that does more ill than good. It is not necessarily the right thing to index all columns that are pert of a foreign key, for keys with several columns, you need a multi-column index. You can use the query in this blog post [1] to look for missing indexes. I would be surprised if correctly indexed foreign keys would slow deletes down so that the performance becomes intolerable. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com [1]: https://www.cybertec-postgresql.com/en/index-your-foreign-key/