[FLINK-39409][connect/postgres] Support tables without primary key for incremental snapshot#4367
Open
JNSimba wants to merge 7 commits intoapache:masterfrom
Open
[FLINK-39409][connect/postgres] Support tables without primary key for incremental snapshot#4367JNSimba wants to merge 7 commits intoapache:masterfrom
JNSimba wants to merge 7 commits intoapache:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for PostgreSQL tables without primary keys when running in incremental snapshot mode, aligning behavior with the existing MySQL approach.
Changes:
- Extend incremental snapshot split-key extraction and output buffering to handle Debezium records with
nullkeys (no-PK tables). - Add PostgreSQL-side validation to require
REPLICA IDENTITY FULLfor no-PK tables. - Add end-to-end Postgres IT coverage and new DDL for a no-PK test table.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-postgres-cdc/src/test/resources/ddl/inventory_no_pk.sql | Adds a no-PK test table and seed data (with REPLICA IDENTITY FULL). |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-postgres-cdc/src/test/java/org/apache/flink/cdc/connectors/postgres/table/PostgreSQLConnectorITCase.java | Adds IT cases for no-PK tables with/without chunk key. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-postgres-cdc/src/main/java/org/apache/flink/cdc/connectors/postgres/source/PostgresDialect.java | Validates REPLICA IDENTITY FULL for no-PK tables during schema discovery. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-cdc-base/src/main/java/org/apache/flink/cdc/connectors/base/utils/SplitKeyUtils.java | Extracts chunk key from value (before/after) when record key is null. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-cdc-base/src/main/java/org/apache/flink/cdc/connectors/base/source/reader/external/JdbcSourceFetchTaskContext.java | Supports buffer rewrite/merge logic for no-PK by using before/after structs as buffer keys. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-cdc-base/src/main/java/org/apache/flink/cdc/connectors/base/source/reader/external/IncrementalSourceScanFetcher.java | Uses value after struct as initial snapshot buffer key when record key is null. |
Comments suppressed due to low confidence (1)
flink-cdc-connect/flink-cdc-source-connectors/flink-cdc-base/src/main/java/org/apache/flink/cdc/connectors/base/source/reader/external/JdbcSourceFetchTaskContext.java:142
- Typo/grammar in exception message: "the the record" should be corrected to "the record" (and/or rephrase for clarity).
throw new IllegalStateException(
String.format(
"Data change record shouldn't use READ operation, the the record is %s.",
changeRecord));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...tgres-cdc/src/main/java/org/apache/flink/cdc/connectors/postgres/source/PostgresDialect.java
Show resolved
Hide resolved
...tgres-cdc/src/main/java/org/apache/flink/cdc/connectors/postgres/source/PostgresDialect.java
Outdated
Show resolved
Hide resolved
...tgres-cdc/src/main/java/org/apache/flink/cdc/connectors/postgres/source/PostgresDialect.java
Show resolved
Hide resolved
.../org/apache/flink/cdc/connectors/base/source/reader/external/JdbcSourceFetchTaskContext.java
Outdated
Show resolved
Hide resolved
...s/flink-cdc-base/src/main/java/org/apache/flink/cdc/connectors/base/utils/SplitKeyUtils.java
Show resolved
Hide resolved
...dc-source-connectors/flink-connector-postgres-cdc/src/test/resources/ddl/inventory_no_pk.sql
Outdated
Show resolved
Hide resolved
.../src/test/java/org/apache/flink/cdc/connectors/postgres/table/PostgreSQLConnectorITCase.java
Outdated
Show resolved
Hide resolved
.../src/test/java/org/apache/flink/cdc/connectors/postgres/table/PostgreSQLConnectorITCase.java
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for PostgreSQL tables without primary keys in incremental snapshot mode,
following the same approach as MySQL (PR #2150).
Changes
JDBC Base Layer (flink-cdc-base):
SplitKeyUtils.getSplitKey(): Handle null record key by extracting chunk key from value'safter/before struct instead of record key
IncrementalSourceScanFetcher.pollWithBuffer(): Use after struct as buffer key whenrecord key is null (no-PK table)
JdbcSourceFetchTaskContext.rewriteOutputBuffer(): Support no-PK merge logic — for tableswithout primary key, use before/after struct as buffer key for CREATE/UPDATE/DELETE operations
PostgreSQL Connector (flink-connector-postgres-cdc):
PostgresDialect: Validate that tables without primary key must haveREPLICA IDENTITY FULLset, querying
pg_class.relreplidentand failing fast with a clear error message if notTests:
products_no_pktable DDL (without PK, withREPLICA IDENTITY FULL)testNoPKTableWithChunkKey: End-to-end test for no-PK table withscan.incremental.snapshot.chunk.key-columnconfiguredtestNoPKTableWithoutChunkKey: VerifyValidationExceptionis thrown when chunk key column is not specifiedDesign Decisions
Map<Struct, SourceRecord>inFetchTask.Context.rewriteOutputBuffer()unchanged to avoid breaking MongoDB and other connectors. For no-PK tables, the full row struct
(all columns via REPLICA IDENTITY FULL) serves as the buffer key.
crosses split boundaries, only at-least-once semantics can be guaranteed (consistent with MySQL behavior).
full before image in WAL for UPDATE/DELETE events. The connector validates this at startup.