Skip to content

feat(ingest/postgres): add include_table_partitions to skip table partitions - #21639

Merged
acrylJonny merged 16 commits into
datahub-project:masterfrom
NehaGslab:feat-postgres-skip-partitioned-tables
Nov 27, 2026
Merged

acrylJonny merged 16 commits into
datahub-project:masterfrom
NehaGslab:feat-postgres-skip-partitioned-tables

Conversation

@NehaGslab

@NehaGslab NehaGslab commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

SQLAlchemy lists each partition of a declaratively partitioned Postgres table (PARTITION BY, Postgres 10+) as a regular table. As a result, the Postgres source ingests every partition as its own dataset, next to the parent table. Tables partitioned by day or month turn into hundreds of near-duplicate datasets. The only workaround today is a table_pattern deny regex, which has to keep up with each table's partition naming.

This PR adds a config option, include_table_partitions, to the Postgres source.

source:
  type: postgres
  config:
    include_table_partitions: false
  • Default true: no behavior change for existing recipes.
  • false: tables with pg_class.relispartition = true are skipped in both table ingestion and profiling. This includes sub-partitions. The parent partitioned table is still ingested.
  • Child tables created with table inheritance (INHERITS) are not partitions and are always ingested. table_pattern can still exclude them.
  • If partitions can't be listed (server older than Postgres 10, or no access to pg_catalog), the source reports a warning and ingests all tables as before.

Changes

  • sql_common.py: adds a _get_table_names(inspector, schema) method, the same pattern as the existing _get_view_names. loop_tables and loop_profiler_requests now call it. The base version returns inspector.get_table_names(schema) unchanged, so other SQL sources behave the same.
  • postgres/source.py: adds the include_table_partitions option and overrides _get_table_names to drop partitions. Each skipped partition is counted once, even though ingestion and profiling both list tables.
  • postgres/query.py: adds PostgresQuery.get_table_partitions(), which reads pg_class.relispartition with the schema passed as a bound parameter.
  • sql_report.py: adds the report fields num_table_partitions_skipped and table_partitions_skipped.
  • Docs: adds a "Partitioned tables" section under Capabilities in postgres_post.md.

Testing

  • Unit tests (tests/unit/test_postgres_source.py):
    • With the default, partitions are kept and no extra query runs.
    • With false, partitions are skipped, the parent is kept, and each partition is counted once across both listing passes.
    • If listing partitions fails, all tables are kept and a warning is reported.
  • Integration tests (tests/integration/postgres):
    • setup.sql adds a partitioned schema: a range-partitioned table with two partitions, plus an inheritance parent and child.
    • The new recipe postgres_skip_table_partitions_to_file.yml and its expected output confirm that only events, logs and logs_child are ingested.
    • The existing expected-output files gain the new schema's datasets, with partitions included, which confirms the default is unchanged.
    • The new tables are ANALYZEd in setup.sql. Profiling with row-count estimates adds up per-table estimates, and tables that were never analyzed report -1. Without ANALYZE, the total goes negative and the telemetry math.log2 call raises. That's an existing profiler issue, unrelated to this change, and will be fixed separately.
  • Manual check against a local Postgres 14 database with a partitioned table (3 partitions), a regular table and an inheritance child:
    • Default: 7 datasets.
    • include_table_partitions: false: 4 datasets, with num_table_partitions_skipped: 3 and no warnings.

Checklist

  • The PR conforms to DataHub's Contributing Guideline (particularly PR Title Format)
  • Links to related issues (if applicable)
  • Tests for the changes have been added/updated (if applicable)
  • Docs related to the changes have been added/updated (if applicable). If a new feature has been added a Usage Guide has been added for the same.
  • For any breaking change/potential downtime/deprecation/big changes an entry has been made in Breaking Changes: not needed, the default keeps current behavior.

🤖 Generated with Claude Code


Summary by cubic

Adds include_table_partitions to the Postgres source so ingestions can skip partitions of declaratively partitioned tables. Previously each partition was ingested as its own dataset next to the parent, which for day- or month-partitioned tables meant hundreds of near-duplicate datasets that could only be excluded by maintaining a table_pattern deny regex.

Behavior

  • Default true: no change for existing recipes.
  • false: tables flagged by pg_class.relispartition, including sub-partitions, are skipped in both ingestion and profiling, and the parent table is still ingested. Partition names are listed once per database and schema and shared by both passes, so they always agree.
  • With stateful ingestion, partitions ingested before the option was enabled are soft-deleted on the next run.
  • Child tables created with INHERITS are not partitions and remain ingested. CockroachDB and TimescaleDB also report relispartition and are affected the same way.
  • If pg_class.relispartition can't be read (server older than Postgres 10), a warning that names the database is reported and all tables are ingested and profiled as before.

Written for commit 3712d43. Summary will update on new commits.

View guided diff

NehaGslab and others added 2 commits November 26, 2026 14:16
…titions

Each partition of a declaratively partitioned table (PARTITION BY, Postgres
10+) is listed by SQLAlchemy as a regular table, so it was ingested as its own
dataset next to the parent. Tables partitioned by day or month produce
hundreds of near-duplicate datasets, and the only workaround was a
table_pattern deny regex that has to track partition naming.

Add include_table_partitions (default true, so existing recipes are
unchanged). When false, the Postgres source drops tables marked
pg_class.relispartition, including sub-partitions, from both table ingestion
and profiling, and keeps the parent table. Child tables created with INHERITS
are not partitions and are still ingested. If partitions cannot be listed
(pre-10 server, no pg_catalog access), a warning is reported and all tables
are ingested as before.

- sql_common: add a _get_table_names hook, mirroring _get_view_names, used by
  loop_tables and loop_profiler_requests. The base version is unchanged
  behavior for every other SQL source.
- sql_report: add num_table_partitions_skipped and table_partitions_skipped.
- Tests: unit tests for the filter, and a partitioned schema plus a recipe
  with the option off in the Postgres integration suite. The new tables are
  analyzed in setup.sql so profiling row-count estimates stay non-negative.
- Docs: "Partitioned tables" section in postgres_post.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added ingestion PR or Issue related to the ingestion of metadata community-contribution PR or Issue raised by member(s) of DataHub Community labels Nov 26, 2026
@cursor

cursor Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

PR Summary

Overview
Adds optional filtering for declarative Postgres table partitions so day/month partitioned tables no longer flood DataHub with near-duplicate datasets.

The Postgres source gains include_table_partitions (default true, preserving current behavior). When set to false, partition tables are detected via pg_class.relispartition (including sub-partitions), excluded from both table ingestion and profiling, and counted in num_table_partitions_skipped / table_partitions_skipped. INHERITS child tables are still ingested. Listing failures fall back to ingesting everything with a warning.

Shared SQL ingestion now routes table enumeration through an overridable _get_table_names hook in sql_common, which the Postgres source overrides and uses together with a cached per-schema partition list so ingestion and profiling stay consistent.

Documentation, integration fixtures (partitioned schema + skip-partitions recipe), and unit tests cover default behavior, skip mode, and failure handling.

Reviewed by Cursor Bugbot for commit 3712d43. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

Copy link
Copy Markdown
Contributor

Linear: ING-4342

Thanks for your contribution! We have created an internal ticket to track this PR. A member of the core DataHub team will be assigned to review it within the next few business days - you will get a follow-up comment once a reviewer is assigned.

Enable table-level profiling in the skip-partitions integration recipe so
the profiler's table listing is exercised with include_table_partitions
disabled. The golden now shows datasetProfile only for the kept tables.

Also:
- include the database in the partition-listing warning context
- document that stateful ingestion soft-deletes previously ingested
  partitions, and how CockroachDB and TimescaleDB are affected
- make the report field comment source-agnostic
- trim the get_table_partitions docstring

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NehaGslab and others added 3 commits November 26, 2026 18:18
loop_tables and loop_profiler_requests both call _get_table_names, so the
partition query ran twice per schema. If one call failed and the other
succeeded, partitions could be profiled without being ingested, and the
failure warning was reported twice.

Cache the partition names per (database, schema) on first listing,
including a failed listing as an empty set, so both passes agree. Report
skipped partitions only on the first listing, which replaces the dedupe
set. Type the test helper with Outcome instead of Any.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… schema

Add a unit test that lists two schemas in one database and one schema in
a second database, twice each. It checks that each listing drops only
its own partitions, that the partition query runs once per
(database, schema), and that each skipped partition is reported once.

Also say in the listing-failure warning that partitions are profiled as
well as ingested, since the failed listing is reused by profiling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@acrylJonny acrylJonny left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. The _get_table_names hook keeps every other SQL source unchanged. Caching the partition list per (database, schema), including a failed listing, means profiling always sees the same tables as ingestion. The failure path fails open and reports a warning. The goldens show the default path still ingests partitions, and with the option off only events, logs and logs_child are ingested and profiled. The Postgres unit tests pass locally (53), and the postgres integration job passes.

The inline comments are small and none of them block the merge. One more for a follow-up: datahub recipe probe (sqlalchemy_probe.py) lists tables with inspector.get_table_names directly. With include_table_partitions: false, the probe still reports partitions as ingested, so it disagrees with the real run. It would be worth routing the probe through the same filter, or noting the gap in the docs.

NehaGslab and others added 3 commits November 27, 2026 15:38
relispartition is also true for partitions of partitioned indexes. Those
names never match a table, so results were already correct, but filtering
on relkind IN ('r', 'p') keeps the set to the ordinary and partitioned
tables that SQLAlchemy's get_table_names lists and makes the intent
explicit. Sub-partitions (relkind 'p') are still skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ause

Every role can read pg_class by default, so the hint about read access
to pg_catalog rarely applied. Say that pg_class.relispartition could not
be read and that it needs Postgres 10+; the underlying error is already
attached via exc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…test

Compare the full list instead of its length, as the other partition
tests do, so a wrong table returned in place of a dropped one fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NehaGslab and others added 5 commits November 27, 2026 16:55
…tests

Make partitioned.events_2022_03 itself partitioned (PARTITION BY LIST)
with a DEFAULT child, events_2022_03_default. The default goldens now
include the sub-partition, and the skip-partitions golden is unchanged,
which shows sub-partitions are skipped along with direct partitions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keep only the reason for the cache: loop_tables and
loop_profiler_requests must skip the same tables, including after a
failed listing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A relkind 'p' row that is also a partition is an intermediate partitioned
table, not a leaf sub-partition; leaf partitions at any level are 'r'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@NehaGslab

Copy link
Copy Markdown
Contributor Author

@acrylJonny I have addressed the review comments, can you please review again?

@acrylJonny acrylJonny left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed, and all five comments are addressed. The partition query now limits to relkind IN ('r', 'p'), the warning points at relispartition, the cache comment is down to one line and the default test asserts the exact list. The new sub-partition events_2022_03_default is in both default goldens and left out of the skip golden. Thanks!

@acrylJonny
acrylJonny enabled auto-merge (squash) November 27, 2026 11:40
@acrylJonny
acrylJonny merged commit c952f41 into datahub-project:master Nov 27, 2026
102 of 103 checks passed

This branch was successfully deployed

1 active deployment
Preview — 3712d435 Deployed Nov 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution PR or Issue raised by member(s) of DataHub Community ingestion PR or Issue related to the ingestion of metadata

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants