Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tiSBo-00GBJh-IX for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Feb 2025 05:55:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tiSBl-002ZZs-6K for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Feb 2025 05:55:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tiSBk-002ZXy-Si for pgsql-hackers@lists.postgresql.org; Thu, 13 Feb 2025 05:55:05 +0000 Received: from mail-yw1-x1133.google.com ([2607:f8b0:4864:20::1133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tiSBj-000Wdu-1s for pgsql-hackers@lists.postgresql.org; Thu, 13 Feb 2025 05:55:04 +0000 Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-6f4bc408e49so4171607b3.1 for ; Wed, 12 Feb 2025 21:55:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739426103; x=1740030903; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=cYbAJt2CINsdq9tBLOrtBGw7HLwmpbwipC6BVZuJz+4=; b=Zb2p2VyQYw4cnczdbbgtS1fIvcPRylsI/iZV/ufmJ1IQmIAcwRJ8863FhadZxh0tKG uY5PdFl6iNr8q5VlbuRNzAhtqiLRE4USidgtaHvaUard/f72PkuO0txWP8KpaeJpUm2k 1CH8YOtIpTji32piQC+bwKMu5HdE/obXWvkrSPHIToHcOm0biA2CZky8MgpJLmRCTew+ 5FpxHVUgUc48gsBCVQw1DD807JMzgfcxRaHcq647tHxgF8492SHbVVSdKWxQIZ4x6Dkt 3iZGfVyuHa5ub55dp4Ai6kiOW0ztTW9/FkGHvm8+zY70tGo6/ogNrr2c4TX7dMiYOxBd 22Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739426103; x=1740030903; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cYbAJt2CINsdq9tBLOrtBGw7HLwmpbwipC6BVZuJz+4=; b=QFMkaFxOfgEhVle47Hq2Fs+7JO7d4he2Xz+MrHoUElK36pI4E/818sEMWYukYfgseP CEzj4IWumKAi3gzuCDMYFGyJDkD5gwFakdISpnOnD4Bv1jt12DyXqs+kTb+4tG++wRzL VtJg99nImqidFC6WP7OAaJQ7d8PJh6KdYW4lK6h7tjKq1CmfFFBY3Y+Gtg2zG32F+KEL jIlERoCJxcxKQIfZP8DXnL3oJ6cE4yz6UmhZ32tkMSYirfzHto3HXRimQjso6W1riLwd z8FA6HXJxLpCe6BlxNR2Rn7xh38dYX167fYeJBgbarWw8FpsYu4nti/KIncZ1Lqo9MAC qQfg== X-Forwarded-Encrypted: i=1; AJvYcCV11l42dmasrBYUyiAr87vrNQnSAaYIUuDu1Vt5Q8J44GZ+h+tMT8wnvDFs3X8mn3Wre1r+jxNjGphz33ff@lists.postgresql.org X-Gm-Message-State: AOJu0YwzWOOb//x9sLgOF0khdGWr79rg9p5MOi6qr2q4PRhbo16k1OLg mIm55S6dPHD4HOzlBI3RwWOzKrEmQsP6RgEThihvA7hXd1kElqpfNRFUjQDP0+XCnAjyuRpo56s NVt4cmQSVW/QJZN5Bb6geTWTUWac= X-Gm-Gg: ASbGncv/7NOeJ3+xMAOt4QIB2U2UM+wSuI8i67N3RIXkZ7j8S5wRWBzsm7pSehVE1e2 M6qUZEaPM8ifonElV7UsKzDNEcQ5ccBG3Im7RL788h4W0eZiAK0HEGkbFjelWR1loDkWIQodfzw == X-Google-Smtp-Source: AGHT+IGzsdcAWsoPUf72F1WRbn6h+XGPEw9cp6On+7/pf5+buqOX1av+ZNFi5P7GLgBGN+fuk+3nxDvCIby4/MO07kQ= X-Received: by 2002:a05:690c:a93:b0:6ef:6536:bb96 with SMTP id 00721157ae682-6fb1f19ba33mr68592167b3.10.1739426102754; Wed, 12 Feb 2025 21:55:02 -0800 (PST) MIME-Version: 1.0 References: <202502050844.4lrgg3aoudat@alvherre.pgsql> In-Reply-To: From: vignesh C Date: Thu, 13 Feb 2025 11:24:51 +0530 X-Gm-Features: AWEUYZlJl2lNuCWQD_LhJVcQo10RHDSd34qUma8cZqlHlM680LlHblYCNncYuYE Message-ID: Subject: Re: Restrict publishing of partitioned table with a foreign table as partition To: Shlok Kyal Cc: =?UTF-8?Q?=C3=81lvaro_Herrera?= , Sergey Tatarintsev , pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 11 Feb 2025 at 16:55, Shlok Kyal wrote: > I have handled the above cases and added tests for the same. There is a concurrency issue with the patch: +check_partrel_has_foreign_table(Form_pg_class relform) +{ + bool has_foreign_tbl = false; + + if (relform->relkind == RELKIND_PARTITIONED_TABLE) + { + List *relids = NIL; + + relids = find_all_inheritors(relform->oid, NoLock, NULL); + + foreach_oid(relid, relids) + { + Relation rel = table_open(relid, AccessShareLock); + + if (RelationGetForm(rel)->relkind == RELKIND_FOREIGN_TABLE) + has_foreign_tbl = true; + + table_close(rel, AccessShareLock); + + if (has_foreign_tbl) + break; + } + } + + return has_foreign_tbl; +} In an ideal scenario, the creation of a foreign table should fail if there is an associated publication, as demonstrated below: CREATE TABLE t(id int) PARTITION BY RANGE(id); CREATE TABLE part1 PARTITION OF t FOR VALUES FROM (0) TO (5); CREATE TABLE part2 PARTITION OF t FOR VALUES FROM (5) TO (15) PARTITION BY RANGE(id); CREATE PUBLICATION pub1 FOR TABLE t with (publish_via_partition_root = true); postgres=# CREATE FOREIGN TABLE part22 PARTITION OF part2 FOR VALUES FROM (10) TO (15) SERVER fdw; ERROR: cannot create table foreign partition "part22" DETAIL: partition table "part2" is published with option publish_via_partition_root Consider a scenario where the publication is being created and after the check_partrel_has_foreign_table execution is done, concurrently creation of foreign table is executed, then the creation will be successful. postgres=# CREATE FOREIGN TABLE part22 PARTITION OF part2 FOR VALUES FROM (10) TO (15) SERVER fdw; CREATE FOREIGN TABLE I felt the problem here is that you have released the lock: + if (RelationGetForm(rel)->relkind == RELKIND_FOREIGN_TABLE) + has_foreign_tbl = true; + + table_close(rel, AccessShareLock); We should retain the lock to fix this issue: + if (RelationGetForm(rel)->relkind == RELKIND_FOREIGN_TABLE) + has_foreign_tbl = true; + + table_close(rel, NoLock); Regards, Vignesh