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

Merged
NE
1d ago
+3848-261

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.