Restrict publishing of partitioned table with a foreign table as partition
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
Hi,
As part of a discussion in [1]/messages/by-id/CAA4eK1Lhh4SgiYQLNiWSNKGdVSzbd53=sr2tQCKooEphDkUtgw@mail.gmail.com, I am starting this thread to address
the issue reported for foreign tables.
Logical replication of foreign tables is not supported, and we throw
an error in this case. But when we create a publication on a
partitioned table that has a foreign table as a partition, the initial
sync of such a table is successful. We should also throw an error in
such cases.
With this patch, we will throw an error when we try to create a
publication on (or add to an existing publication) a partitioned table
with a foreign table as its partition or attach such a table to
existing published tables.
[1]: /messages/by-id/CAA4eK1Lhh4SgiYQLNiWSNKGdVSzbd53=sr2tQCKooEphDkUtgw@mail.gmail.com
Thanks and Regards,
Shlok Kyal
Attachments:
v1-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchapplication/octet-stream; name=v1-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchDownload+258-1
29.01.2025 12:16, Shlok Kyal пишет:
Hi,
As part of a discussion in [1], I am starting this thread to address
the issue reported for foreign tables.Logical replication of foreign tables is not supported, and we throw
an error in this case. But when we create a publication on a
partitioned table that has a foreign table as a partition, the initial
sync of such a table is successful. We should also throw an error in
such cases.
With this patch, we will throw an error when we try to create a
publication on (or add to an existing publication) a partitioned table
with a foreign table as its partition or attach such a table to
existing published tables.[1] : /messages/by-id/CAA4eK1Lhh4SgiYQLNiWSNKGdVSzbd53=sr2tQCKooEphDkUtgw@mail.gmail.com
Thanks and Regards,
Shlok Kyal
Hi!
Thanks for patch.
I reviewed it and made some changes.
1. we should check foreign tables (not partitioned)
2. added checking for foreign table creation
3. some little corrections
See attach
Attachments:
v2-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchtext/x-patch; charset=UTF-8; name=v2-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchDownload+258-1
v2-0002-Restrict-publishing-of-foreign-tables-and-partitione.patchtext/x-patch; charset=UTF-8; name=v2-0002-Restrict-publishing-of-foreign-tables-and-partitione.patchDownload+84-17
On Wed, 29 Jan 2025 at 19:21, Sergey Tatarintsev
<s.tatarintsev@postgrespro.ru> wrote:
29.01.2025 12:16, Shlok Kyal пишет:
Hi,
As part of a discussion in [1], I am starting this thread to address
the issue reported for foreign tables.Logical replication of foreign tables is not supported, and we throw
an error in this case. But when we create a publication on a
partitioned table that has a foreign table as a partition, the initial
sync of such a table is successful. We should also throw an error in
such cases.
With this patch, we will throw an error when we try to create a
publication on (or add to an existing publication) a partitioned table
with a foreign table as its partition or attach such a table to
existing published tables.[1] : /messages/by-id/CAA4eK1Lhh4SgiYQLNiWSNKGdVSzbd53=sr2tQCKooEphDkUtgw@mail.gmail.com
Thanks and Regards,
Shlok KyalHi!
Thanks for patch.
I reviewed it and made some changes.
1. we should check foreign tables (not partitioned)
2. added checking for foreign table creation
3. some little correctionsSee attach
Hi Sergey,
I have added most of the changes in v2-0002 with small changes except
one change.
@@ -1428,6 +1427,12 @@ check_foreign_tables_in_schema(Oid schemaid)
errdetail("foreign table \"%s\" is a partition of
partitioned table \"%s\"",
get_rel_name(foreign_tbl_relid), parent_name)));
}
+ else if (relForm->relkind == RELKIND_FOREIGN_TABLE)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("cannot add relation \"%s\" to publication",
+ get_rel_name(relForm->oid)),
+ errdetail_relkind_not_supported(RELKIND_FOREIGN_TABLE)));
}
We should only throw error when foreign table is part of a partition
table in case of 'FOR TABLES IN SCHEMA' . We should not throw an error
otherwise because in case of 'FOR TABLES IN SCHEMA' foreign tables are
not published by default.
I have added the changes in v3-0001.
Thanks and Regards,
Shlok Kyal
Attachments:
v3-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchapplication/octet-stream; name=v3-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchDownload+322-1
30.01.2025 19:02, Shlok Kyal пишет:
On Wed, 29 Jan 2025 at 19:21, Sergey Tatarintsev
<s.tatarintsev@postgrespro.ru> wrote:29.01.2025 12:16, Shlok Kyal пишет:
Hi,
As part of a discussion in [1], I am starting this thread to address
the issue reported for foreign tables.Logical replication of foreign tables is not supported, and we throw
an error in this case. But when we create a publication on a
partitioned table that has a foreign table as a partition, the initial
sync of such a table is successful. We should also throw an error in
such cases.
With this patch, we will throw an error when we try to create a
publication on (or add to an existing publication) a partitioned table
with a foreign table as its partition or attach such a table to
existing published tables.[1] : /messages/by-id/CAA4eK1Lhh4SgiYQLNiWSNKGdVSzbd53=sr2tQCKooEphDkUtgw@mail.gmail.com
Thanks and Regards,
Shlok KyalHi!
Thanks for patch.
I reviewed it and made some changes.
1. we should check foreign tables (not partitioned)
2. added checking for foreign table creation
3. some little correctionsSee attach
Hi Sergey,
I have added most of the changes in v2-0002 with small changes except
one change.@@ -1428,6 +1427,12 @@ check_foreign_tables_in_schema(Oid schemaid) errdetail("foreign table \"%s\" is a partition of partitioned table \"%s\"", get_rel_name(foreign_tbl_relid), parent_name))); } + else if (relForm->relkind == RELKIND_FOREIGN_TABLE) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot add relation \"%s\" to publication", + get_rel_name(relForm->oid)), + errdetail_relkind_not_supported(RELKIND_FOREIGN_TABLE))); }We should only throw error when foreign table is part of a partition
table in case of 'FOR TABLES IN SCHEMA' . We should not throw an error
otherwise because in case of 'FOR TABLES IN SCHEMA' foreign tables are
not published by default.I have added the changes in v3-0001.
Thanks and Regards,
Shlok Kyal
Hello!
Ok, but maybe it will be correct to raise an WARNING (or at least LOG)
that some tables was skipped during publication. What do you think?
And I think we need check tables which was really published in case of
'FOR ALL TABLES' and 'FOR TABLES IN SCHEMA' in our tests. See attach.
Also it looks strange - we raise an ERROR for partitioned tables
containing foreign partitions , but just skip foreign tables as itself.
I think there should be the same behavior in both cases - raise an ERROR
or skip.
Attachments:
v4-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchtext/x-patch; charset=UTF-8; name=v4-0001-Restrict-publishing-of-partitioned-table-with-a-f.patchDownload+322-1
v4-0002-Tests-check-tables-which-was-really-published-via-FO.patchtext/x-patch; charset=UTF-8; name=v4-0002-Tests-check-tables-which-was-really-published-via-FO.patchDownload+45-4
On Fri, 31 Jan 2025 at 10:38, Sergey Tatarintsev
<s.tatarintsev@postgrespro.ru> wrote:
Ok, but maybe it will be correct to raise an WARNING (or at least LOG)
that some tables was skipped during publication. What do you think?And I think we need check tables which was really published in case of
'FOR ALL TABLES' and 'FOR TABLES IN SCHEMA' in our tests. See attach.Also it looks strange - we raise an ERROR for partitioned tables
containing foreign partitions , but just skip foreign tables as itself.
I think there should be the same behavior in both cases - raise an ERROR
or skip.
I believe this behavior is acceptable. We should throw an error for
partitioned tables that contain foreign partitions, as this would
include the data from these foreign tables during the initial sync,
while incremental changes would not be replicated. However, in the
case of foreign tables, neither the initial sync nor the incremental
sync will occur. Additionally, it is documented in [1]https://www.postgresql.org/docs/current/logical-replication-restrictions.html that foreign
tables are not supported, so this behavior is in line with the
documentation and is acceptable.
[1]: https://www.postgresql.org/docs/current/logical-replication-restrictions.html
Regards,
Vignesh
On Thu, 30 Jan 2025 at 17:32, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
@@ -1428,6 +1427,12 @@ check_foreign_tables_in_schema(Oid schemaid) errdetail("foreign table \"%s\" is a partition of partitioned table \"%s\"", get_rel_name(foreign_tbl_relid), parent_name))); } + else if (relForm->relkind == RELKIND_FOREIGN_TABLE) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot add relation \"%s\" to publication", + get_rel_name(relForm->oid)), + errdetail_relkind_not_supported(RELKIND_FOREIGN_TABLE))); }We should only throw error when foreign table is part of a partition
table in case of 'FOR TABLES IN SCHEMA' . We should not throw an error
otherwise because in case of 'FOR TABLES IN SCHEMA' foreign tables are
not published by default.I have added the changes in v3-0001.
In case of all tables publication you have retrieved all the foreign
tables and then checked if any of the foreign tables is a partition of
a partitioned table. In case of all tables in schema publication you
have retrieved all the partitioned tables and then check if it
includes foreign tables. I felt you can keep the all tables in schema
publication also similar to all tables publication(as generally the
number of foreign tables will be lesser than the tables) i.e. retrieve
all the foreign tables and then check if any of the foreign tables is
a partition of a partitioned table.
Regards,
Vignesh
On 2025-Feb-04, vignesh C wrote:
We should throw an error for partitioned tables that contain foreign
partitions, as this would include the data from these foreign tables
during the initial sync, while incremental changes would not be
replicated.
Hmm, I would support the idea of allowing partitioned tables containing
foreign partitions into publications, where only the tuples in
non-foreign partitions would be transmitted -- both during initial sync
and during replicated DML. This way, any partitioned tables with mixed
local and foreign partitions would work okay for replication. If the
subscriber wants the data in the foreign partitions, they can create the
foreign table on the subscription side and all is well. From a users
POV this is probably the most useful.
Another aspect is the case where you create the publication first and
the foreign table later. I didn't spot in the proposed patch any code
that would check whether a publication including this schema exists,
which we would have to do if we wanted to be watertight about rejecting
publications with foreign tables. But I think that would be a bad
direction to go it.
I'd rather have the table-listing code for publications skip any foreign
tables. For instance, GetPubPartitionOptionRelations() and
PublicationAddTables() should skip tables with relkind RELKIND_FOREIGN.
The sync code should also silently ignore all foreign tables.
--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"I am amazed at [the pgsql-sql] mailing list for the wonderful support, and
lack of hesitasion in answering a lost soul's question, I just wished the rest
of the mailing list could be like this." (Fotis)
/messages/by-id/200606261359.k5QDxE2p004593@auth-smtp.hol.gr
On Tue, 4 Feb 2025 at 21:21, Álvaro Herrera <alvherre@alvh.no-ip.org> wrote:
On 2025-Feb-04, vignesh C wrote:
We should throw an error for partitioned tables that contain foreign
partitions, as this would include the data from these foreign tables
during the initial sync, while incremental changes would not be
replicated.Hmm, I would support the idea of allowing partitioned tables containing
foreign partitions into publications, where only the tuples in
non-foreign partitions would be transmitted -- both during initial sync
and during replicated DML. This way, any partitioned tables with mixed
local and foreign partitions would work okay for replication. If the
subscriber wants the data in the foreign partitions, they can create the
foreign table on the subscription side and all is well. From a users
POV this is probably the most useful.Another aspect is the case where you create the publication first and
the foreign table later. I didn't spot in the proposed patch any code
that would check whether a publication including this schema exists,
which we would have to do if we wanted to be watertight about rejecting
publications with foreign tables. But I think that would be a bad
direction to go it.I'd rather have the table-listing code for publications skip any foreign
tables. For instance, GetPubPartitionOptionRelations() and
PublicationAddTables() should skip tables with relkind RELKIND_FOREIGN.
The sync code should also silently ignore all foreign tables.
We can maintain the behavior you suggested when the
PUBLISH_VIA_PARTITION_ROOT option is set to false. However, when
PUBLISH_VIA_PARTITION_ROOT is true, the table data is copied from the
root table (as intended by the user), which will also include the
foreign table data. In this case, wouldn’t it be better to throw an
error?
Regards,
Vignesh
On 2025-Feb-05, vignesh C wrote:
We can maintain the behavior you suggested when the
PUBLISH_VIA_PARTITION_ROOT option is set to false. However, when
PUBLISH_VIA_PARTITION_ROOT is true, the table data is copied from the
root table (as intended by the user), which will also include the
foreign table data. In this case, wouldn’t it be better to throw an
error?
It sounds to me a reasonable restriction that you can only add a
partitioned table to a publication if publish_via_partition_root=false.
Then the case of hybrid partitioned tables is supported, but it requires
that changes are published directly by partitions, which is sensible
anyway.
In this case, during CREATE FOREIGN TABLE of a partition with this
condition, we must check whether any publications include the schema
that the table is being created on (or attached, for ALTER TABLE ATTACH
PARTITION), and whether there are any publications that are FOR ALL
TABLES that would be affected.
(If we later figure out a way to allow publish_via_partition_root and
skip the tuples in foreign partitions, it's easy to remove the
restriction.)
--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"That sort of implies that there are Emacs keystrokes which aren't obscure.
I've been using it daily for 2 years now and have yet to discover any key
sequence which makes any sense." (Paul Thomas)
On Tue, 4 Feb 2025 at 18:31, vignesh C <vignesh21@gmail.com> wrote:
On Thu, 30 Jan 2025 at 17:32, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
@@ -1428,6 +1427,12 @@ check_foreign_tables_in_schema(Oid schemaid) errdetail("foreign table \"%s\" is a partition of partitioned table \"%s\"", get_rel_name(foreign_tbl_relid), parent_name))); } + else if (relForm->relkind == RELKIND_FOREIGN_TABLE) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot add relation \"%s\" to publication", + get_rel_name(relForm->oid)), + errdetail_relkind_not_supported(RELKIND_FOREIGN_TABLE))); }We should only throw error when foreign table is part of a partition
table in case of 'FOR TABLES IN SCHEMA' . We should not throw an error
otherwise because in case of 'FOR TABLES IN SCHEMA' foreign tables are
not published by default.I have added the changes in v3-0001.
In case of all tables publication you have retrieved all the foreign
tables and then checked if any of the foreign tables is a partition of
a partitioned table. In case of all tables in schema publication you
have retrieved all the partitioned tables and then check if it
includes foreign tables. I felt you can keep the all tables in schema
publication also similar to all tables publication(as generally the
number of foreign tables will be lesser than the tables) i.e. retrieve
all the foreign tables and then check if any of the foreign tables is
a partition of a partitioned table.
I believe you chose this approach because partitioned tables can have
their partitions as foreign tables in a different schema. As a result,
the current approach works well for the 'TABLES IN SCHEMA' case. It
would be helpful to include a brief comment explaining this reasoning
where you're handling tables in the schema publication.
Regards,
Vignesh
On Wed, 5 Feb 2025 at 14:14, Álvaro Herrera <alvherre@alvh.no-ip.org> wrote:
On 2025-Feb-05, vignesh C wrote:
We can maintain the behavior you suggested when the
PUBLISH_VIA_PARTITION_ROOT option is set to false. However, when
PUBLISH_VIA_PARTITION_ROOT is true, the table data is copied from the
root table (as intended by the user), which will also include the
foreign table data. In this case, wouldn’t it be better to throw an
error?It sounds to me a reasonable restriction that you can only add a
partitioned table to a publication if publish_via_partition_root=false.
Then the case of hybrid partitioned tables is supported, but it requires
that changes are published directly by partitions, which is sensible
anyway.
I have created a patch with the above approach.
We can create a publication on a partition table with foreign
partition when publish_via_partition_root=false. In this case foreign
partitions will not be published. And if
publish_via_partition_root=true we will throw an error.
Please find the v5 patch.
In this case, during CREATE FOREIGN TABLE of a partition with this
condition, we must check whether any publications include the schema
that the table is being created on (or attached, for ALTER TABLE ATTACH
PARTITION), and whether there are any publications that are FOR ALL
TABLES that would be affected.
I have handled the above cases and added tests for the same.
(If we later figure out a way to allow publish_via_partition_root and
skip the tuples in foreign partitions, it's easy to remove the
restriction.)
Thanks and Regards,
Shlok Kyal
Attachments:
v5-0001-Restrict-publishing-of-partitioned-table-with-for.patchapplication/octet-stream; name=v5-0001-Restrict-publishing-of-partitioned-table-with-for.patchDownload+488-18
On Mon, 10 Feb 2025 at 16:11, vignesh C <vignesh21@gmail.com> wrote:
On Tue, 4 Feb 2025 at 18:31, vignesh C <vignesh21@gmail.com> wrote:
On Thu, 30 Jan 2025 at 17:32, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
@@ -1428,6 +1427,12 @@ check_foreign_tables_in_schema(Oid schemaid) errdetail("foreign table \"%s\" is a partition of partitioned table \"%s\"", get_rel_name(foreign_tbl_relid), parent_name))); } + else if (relForm->relkind == RELKIND_FOREIGN_TABLE) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot add relation \"%s\" to publication", + get_rel_name(relForm->oid)), + errdetail_relkind_not_supported(RELKIND_FOREIGN_TABLE))); }We should only throw error when foreign table is part of a partition
table in case of 'FOR TABLES IN SCHEMA' . We should not throw an error
otherwise because in case of 'FOR TABLES IN SCHEMA' foreign tables are
not published by default.I have added the changes in v3-0001.
In case of all tables publication you have retrieved all the foreign
tables and then checked if any of the foreign tables is a partition of
a partitioned table. In case of all tables in schema publication you
have retrieved all the partitioned tables and then check if it
includes foreign tables. I felt you can keep the all tables in schema
publication also similar to all tables publication(as generally the
number of foreign tables will be lesser than the tables) i.e. retrieve
all the foreign tables and then check if any of the foreign tables is
a partition of a partitioned table.I believe you chose this approach because partitioned tables can have
their partitions as foreign tables in a different schema. As a result,
the current approach works well for the 'TABLES IN SCHEMA' case. It
would be helpful to include a brief comment explaining this reasoning
where you're handling tables in the schema publication.
Correct, I used this approach because partitioned tables can have
their foreign partitions in a different schema. I have added a comment
for the same in the v5 patch [1]/messages/by-id/CANhcyEXxjq9U7BXxCba3Njz+eHpNzAsSVY2GzbV=8iy5j=UAUA@mail.gmail.com.
[1]: /messages/by-id/CANhcyEXxjq9U7BXxCba3Njz+eHpNzAsSVY2GzbV=8iy5j=UAUA@mail.gmail.com
Thanks and Regards,
Shlok Kyal
On Tue, 11 Feb 2025 at 16:55, Shlok Kyal <shlok.kyal.oss@gmail.com> 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
On Thu, 13 Feb 2025 at 11:25, vignesh C <vignesh21@gmail.com> wrote:
On Tue, 11 Feb 2025 at 16:55, Shlok Kyal <shlok.kyal.oss@gmail.com> 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_rootConsider 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 TABLEI 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);
Hi Vignesh,
I have fixed the issue. Attached the updated v6 patch.
Thanks and Regards,
Shlok Kyal
Attachments:
v6-0001-Restrict-publishing-of-partitioned-table-with-for.patchapplication/octet-stream; name=v6-0001-Restrict-publishing-of-partitioned-table-with-for.patchDownload+488-18
On Thu, 13 Feb 2025 at 15:50, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
I have fixed the issue. Attached the updated v6 patch.
There is another concurrency issue:
In case of create publication for all tables with
publish_via_partition_root we will call check_foreign_tables:
@@ -876,6 +876,10 @@ CreatePublication(ParseState *pstate,
CreatePublicationStmt *stmt)
/* Associate objects with the publication. */
if (stmt->for_all_tables)
{
+ /* Check if any foreign table is a part of partitioned table */
+ if (publish_via_partition_root)
+ check_foreign_tables(stmt->pubname);
At the time of check in check_foreign_tables, there are no foreign
tables so this check will be successful:
+check_foreign_tables_in_schema(Oid schemaid, char *pubname)
+{
+ Relation classRel;
+ ScanKeyData key[2];
+ TableScanDesc scan;
+ HeapTuple tuple;
+
+ classRel = table_open(RelationRelationId, AccessShareLock);
+
+ ScanKeyInit(&key[0],
+ Anum_pg_class_relnamespace,
+ BTEqualStrategyNumber, F_OIDEQ,
+ schemaid);
+ ScanKeyInit(&key[1],
+ Anum_pg_class_relkind,
+ BTEqualStrategyNumber, F_CHAREQ,
+ CharGetDatum(RELKIND_PARTITIONED_TABLE));
+
+ scan = table_beginscan_catalog(classRel, 2, key);
+ while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
Now immediately after execution of this, create a foreign table:
postgres=# CREATE FOREIGN TABLE part22 PARTITION OF part2 FOR VALUES
FROM (10) TO (15) SERVER fdw;
CREATE FOREIGN TABLE
And then continue execution of create publication, it will also be successful:
postgres=# create publication pub1 for all tables with (
publish_via_partition_root =true);
CREATE PUBLICATION
One probable way to fix this is to do the search similar to
check_foreign_tables_in_schema where we can skip including schemaid
key for all tables.
How about something like the attached patch.
Regards,
Vignesh
Attachments:
Concurrency_all_tables_issue.patchtext/x-patch; charset=US-ASCII; name=Concurrency_all_tables_issue.patchDownload+12-54
On Thu, 13 Feb 2025 at 20:12, vignesh C <vignesh21@gmail.com> wrote:
On Thu, 13 Feb 2025 at 15:50, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
I have fixed the issue. Attached the updated v6 patch.
There is another concurrency issue: In case of create publication for all tables with publish_via_partition_root we will call check_foreign_tables: @@ -876,6 +876,10 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt) /* Associate objects with the publication. */ if (stmt->for_all_tables) { + /* Check if any foreign table is a part of partitioned table */ + if (publish_via_partition_root) + check_foreign_tables(stmt->pubname);At the time of check in check_foreign_tables, there are no foreign tables so this check will be successful: +check_foreign_tables_in_schema(Oid schemaid, char *pubname) +{ + Relation classRel; + ScanKeyData key[2]; + TableScanDesc scan; + HeapTuple tuple; + + classRel = table_open(RelationRelationId, AccessShareLock); + + ScanKeyInit(&key[0], + Anum_pg_class_relnamespace, + BTEqualStrategyNumber, F_OIDEQ, + schemaid); + ScanKeyInit(&key[1], + Anum_pg_class_relkind, + BTEqualStrategyNumber, F_CHAREQ, + CharGetDatum(RELKIND_PARTITIONED_TABLE)); + + scan = table_beginscan_catalog(classRel, 2, key); + while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)Now immediately after execution of this, create a foreign table:
postgres=# CREATE FOREIGN TABLE part22 PARTITION OF part2 FOR VALUES
FROM (10) TO (15) SERVER fdw;
CREATE FOREIGN TABLEAnd then continue execution of create publication, it will also be successful:
postgres=# create publication pub1 for all tables with (
publish_via_partition_root =true);
CREATE PUBLICATIONOne probable way to fix this is to do the search similar to
check_foreign_tables_in_schema where we can skip including schemaid
key for all tables.How about something like the attached patch.
Hi Vignesh,
I have used the changes suggested by you. Also I have updated the
comments and the function name.
Thanks and Regards,
Shlok Kyal
Attachments:
v7-0001-Restrict-publishing-of-partitioned-table-with-for.patchapplication/octet-stream; name=v7-0001-Restrict-publishing-of-partitioned-table-with-for.patchDownload+451-18
On Fri, 14 Feb 2025 at 12:59, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
I have used the changes suggested by you. Also I have updated the
comments and the function name.
There is another concurrency issue possible:
+/* Check if a partitioned table has a foreign partition */
+bool
+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);
Create a publication with publish_via_partition_root as true, hold the
execution after check_partrel_has_foreign_table execution finishes.
Then parallely execute the following:
CREATE TABLE t1(id int) PARTITION BY RANGE(id);
CREATE TABLE part1 PARTITION OF t1 FOR VALUES FROM (0) TO (5);
CREATE TABLE part2 PARTITION OF t1 FOR VALUES FROM (5) TO (15)
PARTITION BY RANGE(id);
CREATE FOREIGN TABLE part2_1 PARTITION OF part2 FOR VALUES FROM (10)
TO (15) SERVER fdw
Now both the partitioned table having foreign table and a publication
will be created.
Regards,
Vignesh
On Mon, 17 Feb 2025 at 20:13, vignesh C <vignesh21@gmail.com> wrote:
On Fri, 14 Feb 2025 at 12:59, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
I have used the changes suggested by you. Also I have updated the
comments and the function name.There is another concurrency issue possible: +/* Check if a partitioned table has a foreign partition */ +bool +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);Create a publication with publish_via_partition_root as true, hold the
execution after check_partrel_has_foreign_table execution finishes.
Then parallely execute the following:
CREATE TABLE t1(id int) PARTITION BY RANGE(id);
CREATE TABLE part1 PARTITION OF t1 FOR VALUES FROM (0) TO (5);
CREATE TABLE part2 PARTITION OF t1 FOR VALUES FROM (5) TO (15)
PARTITION BY RANGE(id);
CREATE FOREIGN TABLE part2_1 PARTITION OF part2 FOR VALUES FROM (10)
TO (15) SERVER fdwNow both the partitioned table having foreign table and a publication
will be created.
Hi Vignesh,
I have addressed the above issue. If we take a ShareLock on the
pg_class, we won't be able to create table concurrently, which may
address the issue. Thoughts?
I have attached the v8 patch here.
Thanks and Regards,
Shlok Kyal
Attachments:
v8-0001-Restrict-publishing-of-partitioned-table-with-for.patchapplication/octet-stream; name=v8-0001-Restrict-publishing-of-partitioned-table-with-for.patchDownload+442-18
On Tue, 18 Feb 2025 at 15:59, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
On Mon, 17 Feb 2025 at 20:13, vignesh C <vignesh21@gmail.com> wrote:
On Fri, 14 Feb 2025 at 12:59, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
I have used the changes suggested by you. Also I have updated the
comments and the function name.There is another concurrency issue possible: +/* Check if a partitioned table has a foreign partition */ +bool +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);Create a publication with publish_via_partition_root as true, hold the
execution after check_partrel_has_foreign_table execution finishes.
Then parallely execute the following:
CREATE TABLE t1(id int) PARTITION BY RANGE(id);
CREATE TABLE part1 PARTITION OF t1 FOR VALUES FROM (0) TO (5);
CREATE TABLE part2 PARTITION OF t1 FOR VALUES FROM (5) TO (15)
PARTITION BY RANGE(id);
CREATE FOREIGN TABLE part2_1 PARTITION OF part2 FOR VALUES FROM (10)
TO (15) SERVER fdwNow both the partitioned table having foreign table and a publication
will be created.Hi Vignesh,
I have addressed the above issue. If we take a ShareLock on the
pg_class, we won't be able to create table concurrently, which may
address the issue. Thoughts?
I have attached the v8 patch here.
Since pg_class is a very common table it is not a good idea to take
ShareLock on it. Will it be possible to use pg_partitioned_table table
instead?
Regards,
Vignesh
On Thu, 20 Feb 2025 at 21:18, vignesh C <vignesh21@gmail.com> wrote:
On Tue, 18 Feb 2025 at 15:59, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
On Mon, 17 Feb 2025 at 20:13, vignesh C <vignesh21@gmail.com> wrote:
On Fri, 14 Feb 2025 at 12:59, Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:
I have used the changes suggested by you. Also I have updated the
comments and the function name.There is another concurrency issue possible: +/* Check if a partitioned table has a foreign partition */ +bool +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);Create a publication with publish_via_partition_root as true, hold the
execution after check_partrel_has_foreign_table execution finishes.
Then parallely execute the following:
CREATE TABLE t1(id int) PARTITION BY RANGE(id);
CREATE TABLE part1 PARTITION OF t1 FOR VALUES FROM (0) TO (5);
CREATE TABLE part2 PARTITION OF t1 FOR VALUES FROM (5) TO (15)
PARTITION BY RANGE(id);
CREATE FOREIGN TABLE part2_1 PARTITION OF part2 FOR VALUES FROM (10)
TO (15) SERVER fdwNow both the partitioned table having foreign table and a publication
will be created.Hi Vignesh,
I have addressed the above issue. If we take a ShareLock on the
pg_class, we won't be able to create table concurrently, which may
address the issue. Thoughts?
I have attached the v8 patch here.Since pg_class is a very common table it is not a good idea to take
ShareLock on it. Will it be possible to use pg_partitioned_table table
instead?
Hi Vignesh,
I have addressed comments and I have modified the patch to take a
ShareLock on pg_partitioned_table table.
Please find the updated v9 patch.
Thanks and Regards,
Shlok Kyal