[GH-ISSUE #5665] [Bug]: PostgreSQL sequence reset fails on client_inbounds junction table #3948

Closed
opened 2026-10-31 20:36:05 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @SergioSHA1 on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5665

Describe the bug

After migrating from SQLite to PostgreSQL using the panel's built-in
migration tool, PostgreSQL logs are flooded with repeated errors on
every panel restart. The errors come from a sequence-reset routine
that assumes every table has an id column and unconditionally
calls pg_get_serial_sequence(table, 'id') on it — including
client_inbounds, which is a junction table with a composite primary
key (client_id, inbound_id) and has no id column by design.

A related issue: several ALTER TABLE ... ADD COLUMN statements run
on every startup without an existence check, so they also fail with
"already exists" errors once a column has been added.

Functionality itself is not broken — clients connect and route
traffic normally — but the logs become unusable for spotting real
issues, and the pattern suggests the migration isn't idempotent.

How to reproduce the problem

  1. Set up 3x-ui with SQLite (default)
  2. Run the panel management script → option 25 (PostgreSQL Management)
    → option 2 (Migrate SQLite → PostgreSQL)
  3. After migration completes, restart the x-ui service:
    sudo systemctl restart x-ui
  4. Check PostgreSQL logs:
    sudo tail -100 /var/log/postgresql/postgresql-14-main.log

Expected behavior

The sequence-reset routine should skip tables that don't have an
id column (e.g. composite-key junction tables like
client_inbounds), and ADD COLUMN statements should check for
existence first. No errors should appear in the PostgreSQL log for
a normal, successful migration or restart.

Actual behavior

The following errors appear repeatedly, on every restart after the
PostgreSQL migration:

ERROR: column "id" does not exist at character 70
STATEMENT: SELECT setval(pg_get_serial_sequence($1, 'id'),
COALESCE((SELECT MAX(id) FROM "client_inbounds"), 1), true)
WHERE pg_get_serial_sequence($2, 'id') IS NOT NULL

ERROR: column "wg_private_key" of relation "clients" already exists
STATEMENT: ALTER TABLE "clients" ADD "wg_private_key" text

ERROR: column "reset_up" of relation "client_groups" already exists
STATEMENT: ALTER TABLE "client_groups" ADD "reset_up" bigint DEFAULT 0

client_inbounds schema for reference (composite PK, no id column
by design):

            Table "public.client_inbounds"
Column     |  Type  | Collation | Nullable | Default

---------------+--------+-----------+----------+---------
client_id | bigint | | not null |
inbound_id | bigint | | not null |
flow_override | text | | |
created_at | bigint | | |
Indexes:
"client_inbounds_pkey" PRIMARY KEY, btree (client_id, inbound_id)
"idx_client_inbounds_client_id" btree (client_id)
"idx_client_inbounds_inbound_id" btree (inbound_id)

Relevant logs

2026-06-29 18:50:48.268 UTC xui@xui ERROR:  column "wg_private_key" of relation "clients" already exists
2026-06-29 18:50:48.268 UTC xui@xui STATEMENT:  ALTER TABLE "clients" ADD "wg_private_key" text
2026-06-29 18:50:48.345 UTC xui@xui ERROR:  column "reset_up" of relation "client_groups" already exists
2026-06-29 18:50:48.345 UTC xui@xui STATEMENT:  ALTER TABLE "client_groups" ADD "reset_up" bigint DEFAULT 0
2026-06-29 18:50:48.580 UTC xui@xui ERROR:  column "id" does not exist at character 70
2026-06-29 18:50:48.580 UTC xui@xui STATEMENT:  SELECT setval(pg_get_serial_sequence($1, 'id'), COALESCE((SELECT MAX(id) FROM "client_inbounds"), 1), true)
                                 WHERE pg_get_serial_sequence($2, 'id') IS NOT NULL

Screenshots

No response

3x-ui version

3.4.2

Xray-core version

26.6.27

How did you install 3x-ui?

install.sh script

Operating system

Ubuntu 22.04.5 LTS

Which parts of the panel are affected?

Database / migrations

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @SergioSHA1 on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5665 ### Describe the bug After migrating from SQLite to PostgreSQL using the panel's built-in migration tool, PostgreSQL logs are flooded with repeated errors on every panel restart. The errors come from a sequence-reset routine that assumes every table has an id column and unconditionally calls pg_get_serial_sequence(table, 'id') on it — including client_inbounds, which is a junction table with a composite primary key (client_id, inbound_id) and has no id column by design. A related issue: several ALTER TABLE ... ADD COLUMN statements run on every startup without an existence check, so they also fail with "already exists" errors once a column has been added. Functionality itself is not broken — clients connect and route traffic normally — but the logs become unusable for spotting real issues, and the pattern suggests the migration isn't idempotent. ### How to reproduce the problem 1. Set up 3x-ui with SQLite (default) 2. Run the panel management script → option 25 (PostgreSQL Management) → option 2 (Migrate SQLite → PostgreSQL) 3. After migration completes, restart the x-ui service: sudo systemctl restart x-ui 4. Check PostgreSQL logs: sudo tail -100 /var/log/postgresql/postgresql-14-main.log ### Expected behavior The sequence-reset routine should skip tables that don't have an id column (e.g. composite-key junction tables like client_inbounds), and ADD COLUMN statements should check for existence first. No errors should appear in the PostgreSQL log for a normal, successful migration or restart. ### Actual behavior The following errors appear repeatedly, on every restart after the PostgreSQL migration: ERROR: column "id" does not exist at character 70 STATEMENT: SELECT setval(pg_get_serial_sequence($1, 'id'), COALESCE((SELECT MAX(id) FROM "client_inbounds"), 1), true) WHERE pg_get_serial_sequence($2, 'id') IS NOT NULL ERROR: column "wg_private_key" of relation "clients" already exists STATEMENT: ALTER TABLE "clients" ADD "wg_private_key" text ERROR: column "reset_up" of relation "client_groups" already exists STATEMENT: ALTER TABLE "client_groups" ADD "reset_up" bigint DEFAULT 0 client_inbounds schema for reference (composite PK, no id column by design): Table "public.client_inbounds" Column | Type | Collation | Nullable | Default ---------------+--------+-----------+----------+--------- client_id | bigint | | not null | inbound_id | bigint | | not null | flow_override | text | | | created_at | bigint | | | Indexes: "client_inbounds_pkey" PRIMARY KEY, btree (client_id, inbound_id) "idx_client_inbounds_client_id" btree (client_id) "idx_client_inbounds_inbound_id" btree (inbound_id) ### Relevant logs ```shell 2026-06-29 18:50:48.268 UTC xui@xui ERROR: column "wg_private_key" of relation "clients" already exists 2026-06-29 18:50:48.268 UTC xui@xui STATEMENT: ALTER TABLE "clients" ADD "wg_private_key" text 2026-06-29 18:50:48.345 UTC xui@xui ERROR: column "reset_up" of relation "client_groups" already exists 2026-06-29 18:50:48.345 UTC xui@xui STATEMENT: ALTER TABLE "client_groups" ADD "reset_up" bigint DEFAULT 0 2026-06-29 18:50:48.580 UTC xui@xui ERROR: column "id" does not exist at character 70 2026-06-29 18:50:48.580 UTC xui@xui STATEMENT: SELECT setval(pg_get_serial_sequence($1, 'id'), COALESCE((SELECT MAX(id) FROM "client_inbounds"), 1), true) WHERE pg_get_serial_sequence($2, 'id') IS NOT NULL ``` ### Screenshots _No response_ ### 3x-ui version 3.4.2 ### Xray-core version 26.6.27 ### How did you install 3x-ui? install.sh script ### Operating system Ubuntu 22.04.5 LTS ### Which parts of the panel are affected? Database / migrations ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:36:05 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jun 29, 2026):

Both bugs are confirmed in the v3.4.2 source.

Bug 1 — sequence reset errors on client_inbounds

resyncPostgresSequences in internal/database/migrate_data.go:279–295 iterates every model in initModels() and runs:

SELECT setval(pg_get_serial_sequence($1, 'id'),
              COALESCE((SELECT MAX(id) FROM "client_inbounds"), 1), true)
 WHERE pg_get_serial_sequence($2, 'id') IS NOT NULL

The intent is that the WHERE pg_get_serial_sequence(…) IS NOT NULL clause makes the statement a no-op for tables that have no id sequence. However, model.ClientInbound (declared in internal/database/model/model.go:691–698) maps to client_inbounds, which uses a composite primary key (client_id, inbound_id) and has no id column. PostgreSQL validates column references in the SELECT list at parse/plan time — before evaluating the WHERE condition — so SELECT MAX(id) FROM "client_inbounds" raises an error unconditionally. The application discards the error (_ = db.Exec(…).Error) so the panel is unaffected, but every PostgreSQL startup logs the failure.

The fix is to skip models whose GORM schema has no id field before issuing the SQL — e.g., by checking stmt.Schema.FieldsByDBName["id"] after parsing the model, the same way the existing code already resolves the table name.

Bug 2 — repeated ADD COLUMN … already exists errors

initModels() in internal/database/db.go:86–90 calls GORM's AutoMigrate for each model. For PostgreSQL, GORM does not use ADD COLUMN IF NOT EXISTS, so if a column was added in a prior run it issues:

ALTER TABLE clients ADD wg_private_key text
ALTER TABLE client_groups ADD reset_up bigint DEFAULT 0

The columns are real — ClientRecord.PrivateKey (wg_private_key) in model.go:630 and ClientGroup.ResetUp (reset_up) in model.go:652. The application catches these with isIgnorableDuplicateColumnErr (db.go:474–498) and suppresses the Go error, so nothing breaks. But because PostgreSQL has already logged the error server-side before the driver returns control to Go, it cannot be suppressed at the log level from application code. Using ALTER TABLE … ADD COLUMN IF NOT EXISTS (supported from PostgreSQL 9.6) in the migration path would prevent the server-side log entry entirely.

Summary

  • Functionality: unaffected — connections route normally, data is consistent.
  • PostgreSQL server logs: noisy on every restart due to both issues.
  • Affected version: v3.4.2 (latest release).
  • Fix scope: both issues are in internal/database/ and neither requires a schema change.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:4836178567 --> @github-actions[bot] commented on GitHub (Jun 29, 2026): Both bugs are confirmed in the v3.4.2 source. **Bug 1 — sequence reset errors on `client_inbounds`** `resyncPostgresSequences` in `internal/database/migrate_data.go:279–295` iterates every model in `initModels()` and runs: ```sql SELECT setval(pg_get_serial_sequence($1, 'id'), COALESCE((SELECT MAX(id) FROM "client_inbounds"), 1), true) WHERE pg_get_serial_sequence($2, 'id') IS NOT NULL ``` The intent is that the `WHERE pg_get_serial_sequence(…) IS NOT NULL` clause makes the statement a no-op for tables that have no `id` sequence. However, `model.ClientInbound` (declared in `internal/database/model/model.go:691–698`) maps to `client_inbounds`, which uses a composite primary key (`client_id`, `inbound_id`) and has no `id` column. PostgreSQL validates column references in the `SELECT` list at parse/plan time — before evaluating the `WHERE` condition — so `SELECT MAX(id) FROM "client_inbounds"` raises an error unconditionally. The application discards the error (`_ = db.Exec(…).Error`) so the panel is unaffected, but every PostgreSQL startup logs the failure. The fix is to skip models whose GORM schema has no `id` field before issuing the SQL — e.g., by checking `stmt.Schema.FieldsByDBName["id"]` after parsing the model, the same way the existing code already resolves the table name. **Bug 2 — repeated `ADD COLUMN … already exists` errors** `initModels()` in `internal/database/db.go:86–90` calls GORM's `AutoMigrate` for each model. For PostgreSQL, GORM does not use `ADD COLUMN IF NOT EXISTS`, so if a column was added in a prior run it issues: ```sql ALTER TABLE clients ADD wg_private_key text ALTER TABLE client_groups ADD reset_up bigint DEFAULT 0 ``` The columns are real — `ClientRecord.PrivateKey` (`wg_private_key`) in `model.go:630` and `ClientGroup.ResetUp` (`reset_up`) in `model.go:652`. The application catches these with `isIgnorableDuplicateColumnErr` (`db.go:474–498`) and suppresses the Go error, so nothing breaks. But because PostgreSQL has already logged the error server-side before the driver returns control to Go, it cannot be suppressed at the log level from application code. Using `ALTER TABLE … ADD COLUMN IF NOT EXISTS` (supported from PostgreSQL 9.6) in the migration path would prevent the server-side log entry entirely. **Summary** - Functionality: unaffected — connections route normally, data is consistent. - PostgreSQL server logs: noisy on every restart due to both issues. - Affected version: v3.4.2 (latest release). - Fix scope: both issues are in `internal/database/` and neither requires a schema change. *This reply was generated automatically; a maintainer may follow up.*
Author
Owner

@ArrayKamko commented on GitHub (Jun 30, 2026):

Please this Bug need Quick hotfix
after that PostgreSQL error am getting a lot of error in panel
i cant do anything rightnow

Unexpected Application Error!
Failed to fetch dynamically imported module: https://example.com/assets/NodesPage-Dol5ATUX.js
TypeError: Failed to fetch dynamically imported module: https://example.com/assets/NodesPage-Dol5ATUX.js
💿 Hey developer 👋

You can provide a way better UX than this when your app throws errors by providing your own ErrorBoundary or errorElement prop on your route. 

Unexpected Application Error!
Failed to fetch dynamically imported module: https://example.com/assets/ClientFormModal-Cd9ZKOrb.js
TypeError: Failed to fetch dynamically imported module: https://example.com/assets/ClientFormModal-Cd9ZKOrb.js
💿 Hey developer 👋

You can provide a way better UX than this when your app throws errors by providing your own ErrorBoundary or errorElement prop on your route.
<!-- gh-comment-id:4840879449 --> @ArrayKamko commented on GitHub (Jun 30, 2026): Please this Bug need Quick hotfix after that PostgreSQL error am getting a lot of error in panel i cant do anything rightnow ``` Unexpected Application Error! Failed to fetch dynamically imported module: https://example.com/assets/NodesPage-Dol5ATUX.js TypeError: Failed to fetch dynamically imported module: https://example.com/assets/NodesPage-Dol5ATUX.js 💿 Hey developer 👋 You can provide a way better UX than this when your app throws errors by providing your own ErrorBoundary or errorElement prop on your route. ``` ``` Unexpected Application Error! Failed to fetch dynamically imported module: https://example.com/assets/ClientFormModal-Cd9ZKOrb.js TypeError: Failed to fetch dynamically imported module: https://example.com/assets/ClientFormModal-Cd9ZKOrb.js 💿 Hey developer 👋 You can provide a way better UX than this when your app throws errors by providing your own ErrorBoundary or errorElement prop on your route. ```
Author
Owner

@MHSanaei commented on GitHub (Jul 2, 2026):

Fixed in 273f8872: the sequence resync now skips composite-PK tables like client_inbounds, and AutoMigrate is skipped when the table, columns, and indexes already exist — so neither the setval parse error nor the duplicate ADD COLUMN statements reach the PostgreSQL server log anymore. Will ship in the next release.

@ArrayKamko your "Failed to fetch dynamically imported module" errors are a different, client-side issue: the browser cached an old page while the panel binary was updated underneath it. A hard refresh (Ctrl+F5) after a panel update resolves it.

<!-- gh-comment-id:4863440081 --> @MHSanaei commented on GitHub (Jul 2, 2026): Fixed in 273f8872: the sequence resync now skips composite-PK tables like client_inbounds, and AutoMigrate is skipped when the table, columns, and indexes already exist — so neither the setval parse error nor the duplicate ADD COLUMN statements reach the PostgreSQL server log anymore. Will ship in the next release. @ArrayKamko your "Failed to fetch dynamically imported module" errors are a different, client-side issue: the browser cached an old page while the panel binary was updated underneath it. A hard refresh (Ctrl+F5) after a panel update resolves it.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#3948
No description provided.