Skip to content

KNOX-3252: Handle duplicate object exception during postgres table cr…#1146

Merged
hanicz merged 1 commit intoapache:masterfrom
hanicz:KNOX-3252
Feb 13, 2026
Merged

KNOX-3252: Handle duplicate object exception during postgres table cr…#1146
hanicz merged 1 commit intoapache:masterfrom
hanicz:KNOX-3252

Conversation

@hanicz
Copy link
Contributor

@hanicz hanicz commented Feb 13, 2026

…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 exists

This 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.

    <property>
        <name>gateway.service.tokenstate.impl</name>
        <value>org.apache.knox.gateway.services.token.impl.JDBCTokenStateService</value>
    </property>
    <property>
        <name>gateway.database.type</name>
        <value>postgresql</value>
    </property>
    <property>
        <name>gateway.database.port</name>
        <value>5432</value>
    </property>
    <property>
        <name>gateway.database.host</name>
        <value>localhost</value>
    </property>
    <property>
        <name>gateway.database.name</name>
        <value>mydb</value>
    </property>

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

createTableIfNotExists(TOKENS_TABLE_NAME, databaseType.tokensTableSql());
createTableIfNotExists(TOKEN_METADATA_TABLE_NAME, databaseType.metadataTableSql());
try {
createTableIfNotExists(TOKENS_TABLE_NAME, databaseType.tokensTableSql());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that not all the supported DB vendors have CREATE TALE IF EXISTS support (or at least this was true a couple years ago).

@hanicz
Copy link
Contributor Author

hanicz commented Feb 13, 2026

target knox: failed to solve: image "docker.io/apache/knox-dev:local": already exists
Docker tests are failing with the above. Its unrelated to the changes

@hanicz hanicz merged commit 098ad0d into apache:master Feb 13, 2026
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants