Skip to content

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

Closed

Description

@suwei999

In a Nacos cluster environment 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. The root cause is a SQL exception: duplicate key value violates unique constraint "uk_configinfo_datagrouptenant".

Expected behavior
The publishConfig operation should be idempotent. If the configuration already exists, it should be updated gracefully. Under high concurrency or network retries, the server should handle the race condition and ensure that one of the requests succeeds in updating the record rather than throwing a UniqueIndexConflict exception to the client.

Actual behavior
Nacos server returns a 500 error during the race condition. The logs show that PostgreSQL rejects the INSERT statement because a record with the same unique key was committed by another thread just milliseconds prior, and the current thread's "check-then-insert" logic failed to catch it.

How to Reproduce

Set up a Nacos 2.x cluster with PostgreSQL 14+ as the storage backend.

Use a stress testing tool (like JMeter or a custom script) to call the /nacos/v1/cs/configs (POST) API simultaneously with the exact same dataId, group, and namespace.

Observe that a small percentage of requests will fail with a 500 error and a PSQLException in the server logs.

Environment

Nacos Version: 2.3.2 or 2.4.0

OS: Linux / Docker

DB: PostgreSQL 15.x

Deployment: Cluster mode

Possible Solution
For MySQL, Nacos leverages ON DUPLICATE KEY UPDATE. For PostgreSQL, the implementation should be reviewed to ensure the INSERT ... ON CONFLICT (data_id, group_id, tenant_id) DO UPDATE ... syntax is correctly utilized, or the service layer should implement a more robust retry/locking mechanism to handle this specific PG constraint violation.

Activity

  1. github-actions commented on May 1, 2026

    @github-actions

    This issue has been open 7 days with no activity. This will be closed in 7 days.

  2. shiyiyue1102 commented on May 2, 2026

    @shiyiyue1102
    Collaborator

    Thanks for the detailed report and clear reproduction steps. The root cause is confirmed in the source: ExternalConfigInfoPersistServiceImpl.insertOrUpdate() uses a check-then-insert pattern (findConfigInfoState → addConfigInfo), which is susceptible to a TOCTOU race under concurrent writes. The PostgreSQL plugin (ConfigInfoMapperByPostgresql) inherits the base insert SQL without an ON CONFLICT clause, so the second concurrent INSERT hits the unique constraint on (data_id, group_id, tenant_id).

    A fix could either override the insert path in the PG mapper to use INSERT ... ON CONFLICT DO UPDATE, or catch DataIntegrityViolationException at the service layer and retry as an update. Contribution welcome.


    感谢详细的报告和清晰的复现步骤。根因已在源码中确认:ExternalConfigInfoPersistServiceImpl.insertOrUpdate() 采用 check-then-insert 模式(先 findConfigInfoState 再 addConfigInfo),并发写入时存在 TOCTOU 竞态。PostgreSQL 插件(ConfigInfoMapperByPostgresql)继承了基础 insert SQL,没有使用 ON CONFLICT 子句,因此第二个并发 INSERT 会触发 (data_id, group_id, tenant_id) 唯一约束冲突。

    修复方向可以是在 PG mapper 中覆写 insert 路径使用 INSERT ... ON CONFLICT DO UPDATE,或者在 service 层捕获 DataIntegrityViolationException 后重试为 update。欢迎社区贡献。

  3. KomachiSion commented on Jun 1, 2026

    @KomachiSion
    Collaborator

    @shiyiyue1102 这是否又倒回去出现之前的问题了, 我记得之前是因为存在不同数据库对DataIntegrityViolationException异常的不同行为,才诞生了先查询再变更的处理。

    PR #11560

  4. added a commit that references this issue on Jun 5, 2026
  5. KomachiSion commented on Jul 7, 2026

    @KomachiSion
    Collaborator

    Current solution has been refined in #15465.

    The fix only handles Spring standard DuplicateKeyException semantics.

    For ExternalConfigInfoPersistServiceImpl.insertOrUpdate, this method is the non-CAS upsert path. If the first query returns empty and the subsequent insert loses a concurrent race with a duplicate key, the operation falls back to update, preserving insert-or-update semantics.

    For insertOrUpdateCas, the method must preserve CAS semantics. If the insert branch hits a duplicate key, it returns ConfigOperateResult(false) instead of updating, so the caller can treat it as a CAS conflict.

    We intentionally do not catch all DataIntegrityViolationException as duplicate-key conflicts. DuplicateKeyException is a subclass of DataIntegrityViolationException and specifically represents primary-key or unique-constraint violations. DataIntegrityViolationException is broader and may also represent non-null, check, foreign-key, data mapping, length, or other integrity failures. Treating all of them as duplicate key would hide real database errors.

    For different datasource plugins and JDBC drivers, the same database conflict may be translated differently by Spring, for example DuplicateKeyException, broader DataIntegrityViolationException, or lower-level SQLException chains. #15465 handles the safe standard case first. Broader database-specific classification should be discussed separately in the datasource plugin layer.

  6. added a commit that references this issue on Jul 9, 2026
  7. added a commit that references this issue on Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions