Skip to content

Conversation

@arajkumar
Copy link
Member

@arajkumar arajkumar commented Dec 9, 2025

Problem: Adding a hypertable to a publication automatically includes all existing chunks, but newly created chunks are not automatically added to the publication.

This commit fixes the problem by adding newly created chunks to all publications that the hypertable belongs to, while respecting the hypertable’s row and column filtering rules.

This ensures that logical replication works correctly for partitioned data without requiring manual chunk management.

Added a new GUC timescaledb.enable_chunk_auto_publication to control behaviour of this change.

By default timescaledb.enable_chunk_auto_publication is disabled, it could be modified without restarting the database.

@codecov
Copy link

codecov bot commented Dec 9, 2025

Codecov Report

❌ Patch coverage is 88.88889% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.54%. Comparing base (4d2e91a) to head (0f42a72).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/chunk.c 88.63% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9030      +/-   ##
==========================================
+ Coverage   82.42%   82.54%   +0.11%     
==========================================
  Files         243      243              
  Lines       47914    47930      +16     
  Branches    12229    12233       +4     
==========================================
+ Hits        39492    39562      +70     
- Misses       3555     3557       +2     
+ Partials     4867     4811      -56     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@arajkumar arajkumar changed the title Automatically add newly created chunk into hypertable publication Add new chunks to hypertable publication Dec 11, 2025
@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch 5 times, most recently from 4dc1096 to ace7c87 Compare December 11, 2025 08:06
@arajkumar arajkumar marked this pull request as ready for review December 11, 2025 09:36
@github-actions github-actions bot requested review from akuzm and pnthao December 11, 2025 09:36
@github-actions
Copy link

@pnthao, @akuzm: please review this pull request.

Powered by pull-review

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from ace7c87 to 4d7d733 Compare December 11, 2025 10:00
@arajkumar
Copy link
Member Author

CC: @gayyappan

@arajkumar arajkumar requested a review from gayyappan December 11, 2025 10:09
@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch 2 times, most recently from efbcd81 to 0cfe55f Compare December 11, 2025 10:59
INSERT INTO test_hypertable VALUES ('2024-01-03 00:00:00+00', 3, 3.0, 'data3');

-- Verify (3 chunks in pub1)
SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub1' ORDER BY schemaname, tablename;
Copy link
Member

Choose a reason for hiding this comment

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

also need a test after calling compress_chunk. we should not see a new entry in the publication table.
( this also means that direct modifications to the compressed chunk will not be visible to the publication's consumer).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

chunk_insert_into_metadata_after_lock(chunk);
chunk_create_table_constraints(ht, chunk);

/* Add chunk to publications if hypertable is in any publications */
Copy link
Member

@gayyappan gayyappan Dec 11, 2025

Choose a reason for hiding this comment

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

we need to make sure this doesn't happen if the chunk is a foreign table (OSM). I believe that doesn't go through this path. (I need to check the other paths that create a chunk, but wanted to raise that). The best place to add a test for OSM specific chunks is chunk_utils_internal.sql

Copy link
Member Author

Choose a reason for hiding this comment

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

OSM chunks have two issues:
1. For a pre-existing publication on a hypertable, when tiering is enabled, it creates a child foreign table. This child table may be skipped when adding to the publication.
2. For a hypertable that is already tiered, adding it to a publication fails immediately because foreign tables cannot be added to a publication. I think this needs to be solved in upstream Postgres, or perhaps we could override the publishing behavior on our side(?). I want to try hacking PG code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a test for case 1.

Case 2 is an upstream issue, where they don't exclude child foreign tables.

https://github.com/postgres/postgres/blob/493eb0da31be4520252e1af723342dc7ead0c3e5/src/backend/commands/publicationcmds.c#L1773-L1780

I will try fixing upstream. Other option would be overriding CREATE/ALTER publication from our extension and changing it's behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

One more thing, CREATE PUBLICATION .. FOR TABLE supports ONLY option, that will skip all child tables. This can be used on our internal context.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we use the ONLY workaround on the lake side.

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch 5 times, most recently from a6e4280 to b693f95 Compare December 12, 2025 11:27
@arajkumar arajkumar requested a review from gayyappan December 12, 2025 14:32
@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from b693f95 to 1ce636b Compare December 12, 2025 17:12
@fabriziomello
Copy link
Member

@arajkumar @gayyappan does it remove the chunk from the publication if it is dropped either by DROP TABLE statements, drop_chunks API or retention policy?

@gayyappan
Copy link
Member

@arajkumar @gayyappan does it remove the chunk from the publication if it is dropped either by DROP TABLE statements, drop_chunks API or retention policy?

Yes, it should be auto-dropped. @arajkumar would be good to add a test in chunk_publication.sql using drop_chunks API. You might also consider combining chunk_publication.sql and the compression test into 1 sql test file. ( Just a suggestion).

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from 1ce636b to 04ee992 Compare December 13, 2025 07:05
@arajkumar
Copy link
Member Author

@fabriziomello @gayyappan Added tests covering drop_chunk, DROP TABLE _chunk, TRUNCATE hypertable, merge_chunk, and split_chunk.

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from 04ee992 to ff319a7 Compare December 15, 2025 11:24
@arajkumar
Copy link
Member Author

The failure is from the following step.

  - name: Check that no new internal program errors are introduced
      # We collect the same messages when uploading to the CI checks database,
      # but it runs on different conditions (not on forks and not on flaky check),
      # and requires different data (job date), so we do it separately here.
      # The jq --exit-code option is broken with select() on jq-1.6 which we see
      # on some machines, so we use grep instead (see https://github.com/jqlang/jq/issues/1139).
      if: always() && contains(matrix.name, 'Flaky')
      run: |
        ! jq 'select(
                (.state_code == "XX000" and .error_severity != "LOG")
                or (.message | test("resource was not closed"))
            ) | [.message, .func_name,  .statement] | @tsv
        ' -r postmaster.json | grep .

@arajkumar
Copy link
Member Author

@fabriziomello @gayyappan PTAL.

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from ff319a7 to b7644b5 Compare December 17, 2025 12:46
-- Verify final state (8 chunks)
SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub' ORDER BY schemaname, tablename;

-- Verify chunk removal via DROP TABLE
Copy link
Member Author

Choose a reason for hiding this comment

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

@gayyappan Here is a test that covers drop chunk in 3 scenarios.

  1. drop_chunk SQL api
  2. DROP TABLE chunk_table
  3. TRUNCATE TABLE

Copy link
Member

@gayyappan gayyappan left a comment

Choose a reason for hiding this comment

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

Excellent test case coverage. Thank you @arajkumar

@arajkumar
Copy link
Member Author

@fabriziomello @akuzm Could you please review this PR? Thank you.

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from b7644b5 to 848d0b2 Compare December 19, 2025 12:55
@svenklemm
Copy link
Member

Please add a GUC to control this behaviour

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch 2 times, most recently from ac6f987 to 7fd83ce Compare December 19, 2025 19:04
@arajkumar arajkumar requested a review from svenklemm December 19, 2025 19:07
@arajkumar
Copy link
Member Author

Please add a GUC to control this behaviour

@svenklemm Thanks for reviewing this PR.

Added a GUC timescaledb.enable_chunk_auto_publication, enabled by default.

NULL,
NULL);

DefineCustomBoolVariable(MAKE_EXTOPTION("enable_chunk_auto_publication"),
Copy link
Member

Choose a reason for hiding this comment

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

this should be off by default since we dont support logical replication for hypertables and this needs additional infrastructure to work correctly

Copy link
Member Author

Choose a reason for hiding this comment

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

@svenklemm Ty! now it is false by default.

@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from 7fd83ce to 0401c11 Compare January 12, 2026 13:41
Problem: Adding a hypertable to a publication automatically includes all existing chunks, but newly created chunks are not automatically added to the publication.

This commit fixes the problem by adding newly created chunks to all publications that the hypertable belongs to, while respecting the hypertable’s row and column filtering rules.

This ensures that logical replication works correctly for partitioned data without requiring manual chunk management.

Added a new GUC timescaledb.enable_chunk_auto_publication to control behaviour of this change.

By default timescaledb.enable_chunk_auto_publication is disabled, it could be modified without restarting the database.

Signed-off-by: Arunprasad Rajkumar <ar.arunprasad@gmail.com>
@arajkumar arajkumar force-pushed the arajkumar/add-chunks-2-publication branch from 0401c11 to 0f42a72 Compare January 13, 2026 13:18
@arajkumar arajkumar requested review from a team and svenklemm January 13, 2026 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants