Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVjWj-0008Os-LF for pgsql-hackers@arkaria.postgresql.org; Sun, 31 Dec 2017 19:44:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVjWh-0007bd-Vs for pgsql-hackers@arkaria.postgresql.org; Sun, 31 Dec 2017 19:44:07 +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_2) (envelope-from ) id 1eVjWh-0007bT-M7 for pgsql-hackers@lists.postgresql.org; Sun, 31 Dec 2017 19:44:07 +0000 Received: from mail-qt0-x242.google.com ([2607:f8b0:400d:c0d::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eVjWe-0005RX-IA for pgsql-hackers@postgresql.org; Sun, 31 Dec 2017 19:44:06 +0000 Received: by mail-qt0-x242.google.com with SMTP id r39so59020630qtr.13 for ; Sun, 31 Dec 2017 11:44:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :content-transfer-encoding:user-agent; bh=0ljs//fTJvY0QcbLAQgXyI0Mex/OoDw7rNst6iFbUws=; b=j9ik5Ur50yAM+6hm5jQkBEq0WpH7R5vFhcE1j4djImoWc+1ZSDXg59kSbRHrW1xrA8 r/YU1U8loooWAPV6eMixYml42ImaRZyK8wcgPP1IY6oVcCoUurOqhJ/iPIol1Vj+hDSB WETjSe4qbq4mKIU5cjExRWgFBE1U1P+iPPrr59/rFrJ7v43Ny5xJPhC+VzpaZdtgb7bf sChIY9tV+nl9LfU+vefsZ2Wbf0My66fz9eTG8xHrzcQ564c0IBzDLgBv5s1gAj2yB4MH pcLXuITjfdh1Gkvevcq8ZeD+FTaHlMiKAucxg5rkxvuTEn2A9VZz0jMHzILXqp61/07G AQKQ== 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:subject:message-id:mime-version :content-disposition:content-transfer-encoding:user-agent; bh=0ljs//fTJvY0QcbLAQgXyI0Mex/OoDw7rNst6iFbUws=; b=Bw78v5fQ2gtyzxp5k3vZv4EFyLrZHTB9oYlzL5k0WtwUnJThRcXf2GOMqNSDEd319j l+XJ1e+aAplv5iwyjix+ohVR2OQ+RpidGpQUhrVk5wmYDkcmrmXGcv/15XA4AUfHlDRN Xz+pxWfqI+ljcahUr1ZD2T2cSGJXCgBtXUUvzWeUw9GrIP6EqGT9C8gO2Iv0iEAx29ix whXeWX+efnMcxspFAa260dco5aQ4Kke+4SdS+yfQ/1J5q1uSo1rQiSoF3Rvu0wbZbds2 mimnb+UMZtggV0n6HqT42scA7z8HLqNvfOg/hFWFOpm1WWdxKQqXLB7Ax5yGC/qNim/4 k0/w== X-Gm-Message-State: AKGB3mKbfWAY0jFal+vARFE0xy1hBj3d/wD6HLr03NlEDQLns5+LK9Vq Jk2SrUR00Tr7ZRLokv+Sk1vv2YRFH6VPXVsJk2glHn/QJY7kDSG1cRKpb1Th15xVGSWdFWgrd7E gNPbddaoW+wOLkaIx4gTIBTCmVYbpruK38q43tfb9YUXnVsic3rGzTaXju7V+naV8EEz7UkfZco tmJRI= X-Google-Smtp-Source: ACJfBouHclnLsSmRAKZeuLBmL28Nk/rYtu5S76UrTevE7wCxefqSgV7J6Yn345xwyQRISTrKmMuqEw== X-Received: by 10.200.63.107 with SMTP id w40mr7745639qtk.24.1514749442351; Sun, 31 Dec 2017 11:44:02 -0800 (PST) Received: from alvin.alvh.no-ip.org ([179.56.51.13]) by smtp.gmail.com with ESMTPSA id c16sm26650083qtd.80.2017.12.31.11.44.01 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 31 Dec 2017 11:44:01 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 98A1F264; Sun, 31 Dec 2017 16:43:59 -0300 (-03) Date: Sun, 31 Dec 2017 16:43:59 -0300 From: Alvaro Herrera To: Pg Hackers Subject: Foreign keys and partitioned tables Message-ID: <20171231194359.cvojcour423ulha4@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: NeoMutt/20170306-137-4415bd-dirty (1.8.0) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This patch enables foreign key constraints to and from partitioned tables. Naturally, FKs that reference a partitioned table require unique constraints, and therefore they shares the restrictions of those: in my proposed patch, it is only possible if the partition keys are part of the unique constraint. That's not explicitly checked by the FK code, but rather just an property emergent of previous patches. As far as I can tell, no documentation changes are needed, since AFAICS we don't claim anywhere that FKs are not supported for partitioned tables. pg_dump support is not yet correct here, but otherwise this feature should work as intended, and all tests pass for me. I haven't gone exhaustively over things such as partitions created in odd ways, dropped columns, match partial, etc, so bugs, holes and non-working corner cases are still expected, but please do report any you find. This patch removes all the ONLY markers from queries in ri_triggers.c. That makes the queries work for the new use case, but I haven't figured if it breaks things for other use cases. I suppose not, since regular inheritance isn't supposed to allow foreign keys in the first place, but I haven't dug any further. Patch 0001 attached here corresponds to a squashed version of patches in other threads; it's here just for convenience. The patch to be reviewed for this thread is just 0002 and corresponding functionality. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services