Repository navigation
feat(ingest/postgres): add include_table_partitions to skip table partitions - #21639
acrylJonny merged 16 commits into
Conversation
…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>
PR SummaryOverview The Postgres source gains Shared SQL ingestion now routes table enumeration through an overridable 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. |
|
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>
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>
…b.com/NehaGslab/datahub into feat-postgres-skip-partitioned-tables
… 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
left a comment
There was a problem hiding this comment.
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.
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>
…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>
…b.com/NehaGslab/datahub into feat-postgres-skip-partitioned-tables
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>
|
@acrylJonny I have addressed the review comments, can you please review again? |
acrylJonny
left a comment
There was a problem hiding this comment.
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!
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 atable_patterndeny 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.true: no behavior change for existing recipes.false: tables withpg_class.relispartition = trueare skipped in both table ingestion and profiling. This includes sub-partitions. The parent partitioned table is still ingested.INHERITS) are not partitions and are always ingested.table_patterncan still exclude them.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_tablesandloop_profiler_requestsnow call it. The base version returnsinspector.get_table_names(schema)unchanged, so other SQL sources behave the same.postgres/source.py: adds theinclude_table_partitionsoption and overrides_get_table_namesto drop partitions. Each skipped partition is counted once, even though ingestion and profiling both list tables.postgres/query.py: addsPostgresQuery.get_table_partitions(), which readspg_class.relispartitionwith the schema passed as a bound parameter.sql_report.py: adds the report fieldsnum_table_partitions_skippedandtable_partitions_skipped.postgres_post.md.Testing
tests/unit/test_postgres_source.py):false, partitions are skipped, the parent is kept, and each partition is counted once across both listing passes.tests/integration/postgres):setup.sqladds apartitionedschema: a range-partitioned table with two partitions, plus an inheritance parent and child.postgres_skip_table_partitions_to_file.ymland its expected output confirm that onlyevents,logsandlogs_childare ingested.ANALYZEd insetup.sql. Profiling with row-count estimates adds up per-table estimates, and tables that were never analyzed report-1. WithoutANALYZE, the total goes negative and the telemetrymath.log2call raises. That's an existing profiler issue, unrelated to this change, and will be fixed separately.include_table_partitions: false: 4 datasets, withnum_table_partitions_skipped: 3and no warnings.Checklist
🤖 Generated with Claude Code
Summary by cubic
Adds
include_table_partitionsto 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 atable_patterndeny regex.Behavior
true: no change for existing recipes.false: tables flagged bypg_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.INHERITSare not partitions and remain ingested. CockroachDB and TimescaleDB also reportrelispartitionand are affected the same way.pg_class.relispartitioncan'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.