KNOX-3252: Handle duplicate object exception during postgres table cr…#1146
Merged
hanicz merged 1 commit intoapache:masterfrom Feb 13, 2026
Merged
KNOX-3252: Handle duplicate object exception during postgres table cr…#1146hanicz merged 1 commit intoapache:masterfrom
hanicz merged 1 commit intoapache:masterfrom
Conversation
…eation race condition
pzampino
approved these changes
Feb 13, 2026
| createTableIfNotExists(TOKENS_TABLE_NAME, databaseType.tokensTableSql()); | ||
| createTableIfNotExists(TOKEN_METADATA_TABLE_NAME, databaseType.metadataTableSql()); | ||
| try { | ||
| createTableIfNotExists(TOKENS_TABLE_NAME, databaseType.tokensTableSql()); |
Contributor
There was a problem hiding this comment.
Not necessarily for this PR, but I wonder if executing these in a single Statement execution might help minimize the potential for the "already exists" error while improving efficiency slightly.
Contributor
Author
|
|
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.
…eation race condition
KNOX-3252 - Intermittent Postgres type already exists exception
What changes were proposed in this pull request?
The issue is a race condition during table creation when multiple Knox instances are starting up simultaneously. At the initial "isTableExists" check the tables are not yet created. So Knox-instance-0 and Knox-instance-1 will both go ahead with the creation. Postgres will create a record in pg_class and pg_type as well. However the process that gets there later will get the exception due to the record that was created by the other process in pg_type.
2026-02-10 21:19:44,825 ERROR knox.gateway (TokenStateServiceFactory.java:createService(63)) - Error while initiatalizing org.apache.knox.gateway.services.token.impl.JDBCTokenStateService: Error while initiating JDBCTokenStateService: org.postgresql.util.PSQLException: ERROR: type "knox_tokens" already existsThis change catches the duplicate object SQL state and skips the table creation.
How was this patch tested?
Manually tested with a local setup. I had postgres running in docker desktop and a local Knox instance.
I created the below type in the Postgres DB to simulate the race condition
CREATE TYPE knox_tokens AS ENUM ('active', 'expired', 'revoked');Integration Tests
N/A
UI changes
N/A