Class SchemaChangeUtil


  • public class SchemaChangeUtil
    extends Object
    Utilities for inferring CDC schema change events by comparing before/after Debezium table schemas.

    PostgreSQL DDL has exactly four operations that structurally change a table schema:

    • Add column — always appended at the end (PostgreSQL does not support adding a column at an arbitrary position).
    • Drop column — removes a column by name.
    • Rename column — changes a column's name while preserving its position and type.
    • Alter column type — changes a column's type while preserving its name and position.

    Ideally, PostgreSQL's pg_attribute.attnum would be the best way to distinguish whether a column was dropped-and-recreated or simply renamed. However, the pgoutput relation message does not include attnum. Even querying the database for the current attnum after the fact is unreliable due to temporal mismatch — for example, if column C is renamed to D and a new column C is added between two relation messages, a retroactive attnum query would see the new C's attnum, not the original one, leading to incorrect inference.

    Instead, this utility infers schema changes by comparing the before/after column lists and computing the minimum edit using the four operations above. The main inherent ambiguity is the last-column scenario: renaming the last column looks identical to dropping it and adding a new column with the same name at the end, since both produce the same before/after column list. The algorithm resolves this by always preferring the interpretation with the fewest operations (rename costs 1; drop + add costs 2), so a last-column name change is inferred as a rename.

    • Constructor Detail

      • SchemaChangeUtil

        public SchemaChangeUtil()
    • Method Detail

      • inferSchemaChangeEvent

        public static List<SchemaChangeEvent> inferSchemaChangeEvent​(io.debezium.relational.TableId dbzTableId,
                                                                     @Nullable
                                                                     io.debezium.relational.Table tableBefore,
                                                                     io.debezium.relational.Table tableAfter,
                                                                     PostgresSourceConfig sourceConfig,
                                                                     io.debezium.connector.postgresql.TypeRegistry typeRegistry)
        Infers the minimum list of schema change events that transform tableBefore into tableAfter. Returns a CreateTableEvent if tableBefore is null.