Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9sTa-00072s-At for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Mar 2019 14:27:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h9sTY-0005dO-Ul for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Mar 2019 14:27:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9sTY-0005dH-IJ for pgsql-hackers@lists.postgresql.org; Fri, 29 Mar 2019 14:27:20 +0000 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9sTV-00023c-Jq for pgsql-hackers@postgresql.org; Fri, 29 Mar 2019 14:27:19 +0000 Received: by mail-qk1-x743.google.com with SMTP id k189so1474340qkc.0 for ; Fri, 29 Mar 2019 07:27:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=I6UbigUl0EN3he8KAogyFnMYvHAqbDbwaRfYmLd4aDA=; b=QXyjDF5tURgkkztFdPKr3Jel2LbQrZn1PaPii8fNWkalETzg7DIHqUQGVqq5f0n63T stgqHk5Etk6PWgzr4nfRbO3+LA4R4/5I5oGlhQKuhc3LXtrV9ZtSuzwBQlBACERWLUPw IBXPwakKIaRw8+mkj25Ql4TNe2BlRw0yZYsip//UHZd0/Q+gmvEXleMDDrSubB6kLJLm e+eSwy8CqKG/WmAy51aaXaLy/GUXDpJnTabk8le4SMvL/hOpG7KRhEeijeKd9iGV9vxL 4iXYfKeO/7yqwi5tESspJEZ3UUAUuHQwS8K7iLN3QQbX9ApgQ3OEnnsIEV5o9Ft6sT4O DoFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=I6UbigUl0EN3he8KAogyFnMYvHAqbDbwaRfYmLd4aDA=; b=AQLK/QzKXa39IPUS2oWpg8OCkEhWDHwdX69KKEfQeNnLQ2qZFabVy09QX9XopRVGD7 Ts3n2V47wtls74B2iyPrG/1uB3qtOHaCnXii5rQmb41hUGo8/Fl0lfrALtyUPOvmj7Tq 4CYUNFcMiV5P1nFP6RomV8hhiDuMp3RjTYGmAjLo9CN8PaR58YOsljFZrnCaSPl6IEe+ DtcQ55AFe5KLWejMwzz/8qY8fDM6nMEh+SxTpiuNAwUEa0KLrP7FbmTGu5KG/PeWFg1k txQRnxtcC8ds06DpXWA1jHGvAdJJiYMArRpk37FDZyXIkEgQ2LzAc5h/eeZX3xiEXUY2 uxKw== X-Gm-Message-State: APjAAAVjube8C10PUTTyTDgFUNxJa93mSgbafFD9CkZfXOKZSEldQ138 X0ypu/yg0q5D7j6OD0YRBIszYA== X-Google-Smtp-Source: APXvYqzRpbdyO/6xv7HLJ1xlGqAicxA/OOeB4ERQ3hOqBdUkhnEaGfzePA8mA6PKp905YhEVajpE2Q== X-Received: by 2002:a37:bd81:: with SMTP id n123mr39223567qkf.249.1553869636692; Fri, 29 Mar 2019 07:27:16 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id y17sm1106990qtf.72.2019.03.29.07.27.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Mar 2019 07:27:16 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 2933C120A6D; Fri, 29 Mar 2019 11:27:14 -0300 (-03) Date: Fri, 29 Mar 2019 11:27:14 -0300 From: Alvaro Herrera To: Robert Haas Cc: Amit Langote , Amit Langote , Pg Hackers Subject: Re: partitioned tables referenced by FKs Message-ID: <20190329142714.GA22002@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019-Mar-29, Robert Haas wrote: > On Wed, Mar 20, 2019 at 11:58 AM Alvaro Herrera > wrote: > > constraint is dropped. I can only think of ugly data structure to > > support this, and adding enough hooks in dependency.c to support this is > > going to be badly received. > > I don't know why dependency.c doesn't handle this internally. If I > say that I want to delete a list of objects, some of which can't be > dropped without dropping certain other things, dependency.c ought to > be able to suspend judgement on what the problems are until it's > looked over that whole list. It seems to me that we've had to fight > with this problem before, and I don't know why every individual bit of > code that calls into that file has to be responsible for working > around it individually. Hmm, if you think this kind of situation is more widespread than this particular case, then maybe we can add the hooks I was talking about, and simplify those other cases while fixing this problem. Do you have it in your mind what cases are those? Nothing comes to mind, but I'll search around and see if I can find anything. Note that there are two separate problems here. One is how to run arbitrary code to see whether the DROP is allowed (in this case, the arbitrary code to run is RI_Final_Check which searches for referenced rows and throws ERROR if there are any). My proposed hook system seems to work OK for that. The other problem is how to decide (in dependency.c terms) that the partition can be dropped. We have these scenarios to support: 1. ALTER TABLE fk DROP CONSTRAINT -- should work cleanly 2. DROP referenced-partition -- allow only if no referenced rows 3. ALTER referenced-parent-table DETACH partition -- allow only if no referenced rows 4. DROP referenced-parent-table CASCADE -- drop the FK and partition The patch I posted in v8 allowed cases 1, 2 and 3 but failed 4. One possibility is to pass the whole list of objects to drop to the hooks; so if we see (in the pg_class-specific hook) that the FK constraint is listed as being dropped, we just don't execute RI_Final_Check. BTW, RI_Final_Check looks a bit like a silly name, now that I think about it; it was only a temporary name I picked by opposition to RI_Initial_Check. Maybe RI_PartitionRemove_Check would be more apropos? -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services