Skip to content

[ISSUE #14981] Fix concurrent publishConfig race condition for PostgreSQL#15278

Open
wushiyuanmaimob wants to merge 1 commit into
alibaba:developfrom
wushiyuanmaimob:fix/pg-concurrent-publish-config-14981
Open

[ISSUE #14981] Fix concurrent publishConfig race condition for PostgreSQL#15278
wushiyuanmaimob wants to merge 1 commit into
alibaba:developfrom
wushiyuanmaimob:fix/pg-concurrent-publish-config-14981

Conversation

@wushiyuanmaimob

Copy link
Copy Markdown
Contributor

Fixes

Description

In a Nacos cluster using PostgreSQL as the database backend, concurrent calls to the publishConfig API for the same dataId, group, and tenant occasionally result in a 500 Internal Server Error due to UniqueIndexConflict.

Root Cause

ExternalConfigInfoPersistServiceImpl.insertOrUpdate() uses a check-then-act pattern:

  1. SELECT to check if the config exists
  2. If not exists → INSERT
  3. If exists → UPDATE

Under high concurrency, two threads can both find "not exists" and both attempt INSERT. The second one hits the unique constraint (uk_configinfo_datagrouptenant) and throws DataIntegrityViolationException.

For MySQL, this is typically handled by ON DUPLICATE KEY UPDATE or automatic retry in some deployments. PostgreSQL has no such fallback.

Fix

Catch DataIntegrityViolationException on INSERT and fall back to UPDATE, making the operation idempotent across all database backends (MySQL, PostgreSQL, Oracle, etc.).

Before:

check → INSERT → throw exception ❌

After:

check → INSERT → unique constraint violated → UPDATE ✓

Changes

File Description
ExternalConfigInfoPersistServiceImpl.java Add fallback to UPDATE on INSERT duplicate key violation (+25/-5 lines)

Testing

  • Checkstyle: 0 violations
  • Compile: PASS

This fix makes insertOrUpdate and insertOrUpdateCas idempotent under concurrent access without requiring application-level retries.

@github-actions

Copy link
Copy Markdown

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API新增配置参数修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

@github-actions

Copy link
Copy Markdown

⚠️ Commit Author Verification Failed

The following commits have author emails not linked to any GitHub account.
This will prevent CLA signing and block your PR from being merged.

Commit Author Email
77b7a20 sywu14 sywu14@iflytek.com

How to fix:

  1. Add your commit email to your GitHub account: https://github.com/settings/emails
  2. Or update your local git config to use an email already linked to GitHub:
    git config user.name "Your GitHub Username"
    git config user.email "your-github-email@example.com"
    
  3. Amend your commits and force-push:
    git rebase -i HEAD~1 # mark commits as "edit" and amend author
    git push --force-with-lease
    

This check will re-run automatically after you push.

@CLAassistant

CLAassistant commented May 31, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

…libaba#14981)

Under high concurrency, multiple threads calling publishConfig for the same
dataId/group/tenant can both pass the check-then-act check in insertOrUpdate(),
resulting in one thread getting a UniqueIndexConflict error from PostgreSQL.

Fix: Catch DataIntegrityViolationException on INSERT and fall back to UPDATE,
making the operation idempotent across all database backends (MySQL, PostgreSQL,
Oracle, etc.).

Affected methods:
- ExternalConfigInfoPersistServiceImpl.insertOrUpdate()
- ExternalConfigInfoPersistServiceImpl.insertOrUpdateCas()

Fixes alibaba#14981

Signed-off-by: sywu14 <sywu14@iflytek.com>
@wushiyuanmaimob wushiyuanmaimob force-pushed the fix/pg-concurrent-publish-config-14981 branch from 77b7a20 to 44e947e Compare May 31, 2026 02:46
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.

[Bug] PostgreSQL: Concurrent publishConfig calls lead to UniqueIndexConflict instead of idempotent update

3 participants