Class SchemaChangeUtil
- java.lang.Object
-
- org.apache.flink.cdc.connectors.postgres.utils.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.attnumwould be the best way to distinguish whether a column was dropped-and-recreated or simply renamed. However, the pgoutput relation message does not includeattnum. Even querying the database for the current attnum after the fact is unreliable due to temporal mismatch — for example, if columnCis renamed toDand a new columnCis added between two relation messages, a retroactive attnum query would see the newC'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 Summary
Constructors Constructor Description SchemaChangeUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static List<SchemaChangeEvent>inferSchemaChangeEvent(io.debezium.relational.TableId dbzTableId, 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 transformtableBeforeintotableAfter.
-
-
-
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 transformtableBeforeintotableAfter. Returns aCreateTableEventiftableBeforeis null.
-
-