Skip to content

Conversation

abhi-airspace-intelligence
Copy link
Contributor

@abhi-airspace-intelligence abhi-airspace-intelligence commented Aug 28, 2025

What kind of change does this PR introduce?

Adds support for partitioned tables. It does this through a very convoluted sql query that I basically guess-and-checked until it worked. It basically uses a bunch of heuristics to confirm that if a table is a child table of a partitioned table, it's allowed to treat their PKs as its own.

What is the current behavior?

Fixes #296

What is the new behavior?

Partitioned tables are treated as a single table, and replicate as one unit.

Additional context

Note that this is stacked on top of my other MR because I found this bug after fixing that bug. I will also note that I happened to find a race condition in this PR where a replicate worker tries to push events while a sync worker is waiting for a schema. I solved this by simply blocking all workers until all schemas are fixed, but this is a suboptimal solution, to say the least.

@abhi-airspace-intelligence abhi-airspace-intelligence requested a review from a team as a code owner August 28, 2025 20:25
@abhi-airspace-intelligence abhi-airspace-intelligence changed the title Add partitioned table support feat(postgres): add partitioned table support Aug 28, 2025
@abhi-airspace-intelligence abhi-airspace-intelligence force-pushed the abhi/add-partitioned-table-support branch 4 times, most recently from e65c749 to 230f109 Compare August 29, 2025 18:20
Copy link
Contributor

@iambriccardo iambriccardo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits:

  • We generally use only lowercase queries (this is a standard we adopt at Supabase).
  • Lets first merge the PR related to PG 14 support and then rebase it on this one to have a clean diff.

// Verify no data is there.
let table_rows = destination.get_table_rows().await;
assert!(table_rows.is_empty());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was likely deleted by mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't deleted by mistake (at the time), I originally had a fix in this branch that fixed a race condition I found. I solved this through a pretty hacky mean, so this test failed unless I removed this statement. #291 contains to the correct fix, so I reverted the change.

if let Some(server_version) = self.server_version
&& server_version.get() >= 150000
{
// PostgreSQL 15+ supports FOR ALL TABLES IN SCHEMA syntax
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// PostgreSQL 15+ supports FOR ALL TABLES IN SCHEMA syntax
// PostgreSQL 15+ supports FOR TABLES IN SCHEMA syntax

"select schemaname, tablename from pg_publication_tables where pubname = {};",
"select pt.schemaname, pt.tablename from pg_publication_tables pt
join pg_class c on c.relname = pt.tablename
join pg_namespace n on n.oid = c.relnamespace AND n.nspname = pt.schemaname
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there the need to perform this JOIN? Seems like we don't really need to validate namespaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There wasn't, no. I think this was leftover code that I included attempting to fix the race issue I encountered. I cannot replicate it anymore.

@abhi-airspace-intelligence abhi-airspace-intelligence force-pushed the abhi/add-partitioned-table-support branch 6 times, most recently from 4670481 to c983ec9 Compare September 2, 2025 14:21
@abhi-airspace-intelligence
Copy link
Contributor Author

This MR has mostly been rewritten, as I discovered there wasn't an easy way to capture new partitions being created. I did discover publish_via_partition_root, which treats a partitioned table as the "root" table in the eyes of logical replication which does exactly the behavior I want (and is likely what consumers want as well).

@abhi-airspace-intelligence
Copy link
Contributor Author

Tests are broken again on the partial branch. Can be solved by this patch: uni-intelligence@c0ec81f

@abhi-airspace-intelligence abhi-airspace-intelligence force-pushed the abhi/add-partitioned-table-support branch 4 times, most recently from 96cc8de to 6522d79 Compare September 10, 2025 20:23
@abhi-airspace-intelligence abhi-airspace-intelligence force-pushed the abhi/add-partitioned-table-support branch 5 times, most recently from 67cc277 to 4893b04 Compare September 17, 2025 15:36
@abhi-airspace-intelligence abhi-airspace-intelligence force-pushed the abhi/add-partitioned-table-support branch from 4893b04 to 6d5132b Compare September 22, 2025 15:04
Signed-off-by: Abhi Agarwal <abhi@airspace-intelligence.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Partitioned tables do not work directly, due to lack of PK enforcement
2 participants