feat(ingest/postgres): add include_table_partitions to skip table partitions
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 withpg_class.relispartition = trueare 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_patterncan 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_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.- 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.sqladds apartitionedschema: a range-partitioned table with two partitions, plus an inheritance parent and child.- The new recipe
postgres_skip_table_partitions_to_file.ymland its expected output confirm that onlyevents,logsandlogs_childare 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 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.
- 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, withnum_table_partitions_skipped: 3and 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 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.- With stateful ingestion, partitions ingested before the option was enabled are soft-deleted on the next run.
- Child tables created with
INHERITSare not partitions and remain ingested. CockroachDB and TimescaleDB also reportrelispartitionand are affected the same way. - If
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.