Duplicate ref generated in llx_paiement table (Dolibarr 21.0.0, PostgreSQL, possible race condition)

I am encountering an issue where duplicate values are generated in the ref field of the llx_paiement (Payment) table.

Environment

  • Dolibarr version: 21.0.0

  • PHP version: 8.2

  • Database: PostgreSQL

  • Hosting/Setup: Linux (Ubuntu 22 server)

Problem Description

When creating payments, the ref value is occasionally duplicated. The issue appears to be related to very short time gaps between payment creations.

Detection Query

SELECT *
FROM llx_paiement
WHERE ref IN (
    SELECT ref
    FROM llx_paiement
    GROUP BY ref
    HAVING COUNT(*) > 1
)
ORDER BY ref;

Observations

  • Duplicate ref values detected

  • The issue occurs when two payments are created within a very short time interval (≈1 second or less)

  • This suggests a possible race condition in reference generation

  • The ref column does not have a UNIQUE constraint at the database level

Questions

  1. Is this a known issue in Dolibarr 21.0.0, especially with PostgreSQL?

  2. Which numbering module is recommended to ensure unique payment references?

  3. Does Dolibarr implement any locking or transaction-safe mechanism to prevent duplicate refs?

  4. Would adding a UNIQUE constraint on ref be safe, or could it break standard behavior?

  5. Is there any official fix, patch, or recommended workaround?

Concern

I would like to avoid custom fixes that might create issues during future upgrades and prefer a standard/recommended solution.

Thank you for your support.

thank you for reporting. I wish I had your problem of too many payment creations within less than a second.

how do you get that? Are you using the API? Are you importing a file with payments?

If importing a file, my expectation is that it would be processed sequential and should not result in a race condition.
Regarding the API, it is also my expectation that it would be sequential - unless you spawn more API calls in parallel, is that what you are doing?

Or, is it independent customers that uses the online payment module in parallel at the same time?

1 Like

No, we are not using any AP or importing a file with payments.

We receive payments by multiple reception at a time from different booth.

The same payment reference has been used for two different patients.

how do you get that, is it the POS module?

No jonbendtsen its not the POS module. Its just the billing module. which is created by generating worksheet –> generating invoice —> payment by the customer