Skip to content

PostgresAgentStateStore executes ALTER TABLE even when createIfNotExist=false #3213

Open

Description

@wangjinliang1991

Title

PostgresAgentStateStore executes ALTER TABLE even when createIfNotExist=false

Description

In AgentScope Java v2.0.3, PostgresAgentStateStore always calls ensureVersionColumn() during initialization:

if (createIfNotExist) {
    createSchemaIfNotExist();
    createTableIfNotExist();
} else {
    verifySchemaExists();
    verifyTableExists();
}

ensureVersionColumn();

ensureVersionColumn() executes the following DDL:

ALTER TABLE <table>
ADD COLUMN IF NOT EXISTS version BIGINT NOT NULL DEFAULT 0;

This causes initialization to fail in production environments where the application database account only has DML permissions (SELECT, INSERT, UPDATE, DELETE) and schema changes are managed separately by DBA/Flyway/Liquibase.

Even when explicitly configuring:

PostgresAgentStateStore.builder(dataSource)
    .schemaName(schema)
    .tableName(table)
    .createIfNotExist(false)
    .build();

the store still requires ALTER TABLE permission.

Expected behavior

When createIfNotExist=false, PostgresAgentStateStore should not execute any DDL.

The required schema/table/columns can be created through an external database migration process.

Optionally, AgentScope could verify that the required version column exists without modifying the table.

For example:

if (createIfNotExist) {
    createSchemaIfNotExist();
    createTableIfNotExist();
    ensureVersionColumn();
} else {
    verifySchemaExists();
    verifyTableExists();
    verifyVersionColumnExists();
}

Another option would be to introduce a separate configuration such as autoMigrateSchema to explicitly control schema migration.

Why this matters

In many production environments, application database accounts intentionally do not have DDL permissions.

Requiring ALTER TABLE during application startup means users must either:

  1. grant unnecessary DDL privileges to the application account, or
  2. modify/fork PostgresAgentStateStore.

It would be preferable for createIfNotExist=false to support environments where database schema management is handled externally.

Version

AgentScope Java v2.0.3

Activity

  1. oss-maintainer commented on Sep 20, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed | Severity: high

    Verified against main @ 7854f0de. The report is accurate: the version-column migration runs on the path that is supposed to be DDL-free.

    Root cause

    PostgresAgentStateStore.java:127-134 — ensureVersionColumn() is called after the createIfNotExist branch, so it is unconditional:

    if (createIfNotExist) {
        createSchemaIfNotExist();
        createTableIfNotExist();
    } else {
        verifySchemaExists();
        verifyTableExists();
    }
    ensureVersionColumn();          // <-- runs in both cases

    and ensureVersionColumn() (:137-146) emits DDL directly:

    ALTER TABLE <schema>.<table> ADD COLUMN IF NOT EXISTS version BIGINT NOT NULL DEFAULT 1

    There is no read-only counterpart (verifySchemaExists :188 and verifyTableExists :208 are SELECTs against INFORMATION_SCHEMA; nothing equivalent exists for the column).

    Why IF NOT EXISTS does not save it

    PostgreSQL checks table ownership before honoring ADD COLUMN IF NOT EXISTS, so a DML-only account gets ERROR: must be owner of table … (SQLSTATE 42501) on every startup, even when the column already exists. createIfNotExist(false) therefore does not deliver the least-privilege deployment it advertises; the only workarounds today are granting DDL/ownership to the app role or forking the class.

    Not consistent with the sibling stores

    Store createIfNotExist=false behavior
    PostgresAgentStateStore.java:134 unconditional ALTER TABLE — broken for DML-only roles
    MysqlAgentStateStore.java:178-209 SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS … first (:183); ALTER only when the column is genuinely missing (:197)
    JdbcAgentStateStore.java:76-85 no DDL at all on the verify path

    So MySQL already implements exactly the shape this issue proposes; Postgres just never got the same guard.

    One correction to the report: the ALTER now defaults to 1, not 0. That was ensureVersionColumn()'s sentinel-value bug (#3162, fixed by #3165) and is separate from this DDL-permission issue — the unconditional execution is unchanged.

    Fix direction

    1. On the createIfNotExist=false path, replace ensureVersionColumn() with a read-only verifyVersionColumnExists() mirroring MysqlAgentStateStore.java:183-190, and fail with an IllegalStateException that quotes the exact migration DDL to run.
    2. Keep createSchemaIfNotExist() / createTableIfNotExist() / ensureVersionColumn() together under createIfNotExist=true.
    3. Backward compatibility: users who relied on the silent migration with createIfNotExist=false get an explicit opt-in (e.g. autoMigrateSchema) rather than the implicit DDL; DEFAULT 1 from fix(state): resolve phantom CAS conflicts on version-0 migrated rows … #3165 is preserved on that migration path. Worth noting PostgresDistributedStore uses createIfNotExist=true and is unaffected.
    4. Test gap: PostgresAgentStateStoreTest.java:128-131 asserts only that createStatement() is never called, while ensureVersionColumn() goes through prepareStatement() — that is why the existing "no auto-create" test passes despite the DDL. A regression test should assert never()).prepareStatement(startsWith("ALTER")) on the false path, plus a missing-column case expecting the new exception.

    Next step: reply /approve on this issue and a fix PR implementing items 1-4 will be generated; /revise <notes> to steer the scope, or /reject to stop. Not proposing a PR unprompted this tick: item 3 changes startup behavior for existing Postgres deployments, so the opt-in flag name is a maintainer call.


    Automated evaluation by github-manager-bot

  2. added
    bug
    area/ext/memory
    area/extensions
    on Sep 20, 2026
  3. aiyili commented on Sep 21, 2026

    @aiyili
    Contributor

    Hi, I'd like to work on this issue. Following the fix direction above, I'll scope the PR to items 1, 2 and 4, and leave item 3 (the autoMigrateSchema opt-in flag) for maintainers to decide since the flag name is a design call:

    1. On the createIfNotExist=false path, replace the unconditional ensureVersionColumn() with a read-only verifyVersionColumnExists() mirroring the MySQL store's INFORMATION_SCHEMA check, failing with an IllegalStateException that quotes the exact migration DDL to run.
    2. Keep createSchemaIfNotExist() / createTableIfNotExist() / ensureVersionColumn() together under createIfNotExist=true (the DEFAULT 1 backfill value preserved).
    3. Regression tests: assert prepareStatement(startsWith("ALTER")) is never invoked on the false path, plus a missing-column case expecting the new exception (the existing test only asserted createStatement() is never called, which the prepareStatement-based DDL bypassed).

    PR coming shortly.

  4. oss-maintainer commented on Sep 23, 2026

    @oss-maintainer
    Collaborator

    Status: 72 h feedback window expired — no automated PR will be generated, the proposal window here is closed

    The evaluation CTA above (5749751099, posted 2026-09-20T12:18:59Z) reached its 72-hour feedback
    limit at 2026-09-23T12:18:59Z with no own-line /approve, /revise, or /reject command in the
    thread, so per our workflow we skip PR generation and no competing PR will be filed from this
    proposal window. No action is needed from anyone.

    This is a timing stand-down, not a verdict on the report — and in this case the fix is already
    being shipped by the community:

    • @aiyili claimed the work here on 2026-09-21T05:52:49Z (5756001694), scoping the change to the
      read-only verifyVersionColumnExists() guard on the createIfNotExist=false path, keeping
      ensureVersionColumn() under createIfNotExist=true, and adding the prepareStatement("ALTER …")
      regression test.
    • That claim became PR fix(postgresql): no ALTER TABLE on the createIfNotExist=false path #3227 (fix(postgresql): no ALTER TABLE on the createIfNotExist=false
      path
      ). As of this check it is still OPEN and unmerged at head 5aa82149782b (last updated
      2026-09-21T07:41:27Z), so it remains the active carrier for this issue and the fix lands through
      it rather than through a bot-generated PR.

    Measured on the thread at 2026-09-23T12:36Z: 1 non-bot reply after the CTA, 0 own-line directive
    tokens in it (the slash tokens in the evaluation text itself are ours, not a reply), and 0 reactions
    on the CTA — so there was nothing to act on.

    The evaluation itself stands as recorded: PostgresAgentStateStore.java:127-134 calls
    ensureVersionColumn() unconditionally after the createIfNotExist branch, so the
    ALTER TABLE … ADD COLUMN IF NOT EXISTS version DDL still runs on the path that is supposed to be
    DDL-free, and a DML-only role hits SQLSTATE 42501 at startup. The createIfNotExist=false shape
    MySQL already implements (INFORMATION_SCHEMA.COLUMNS check before any ALTER) is the reference.

    If a maintainer would still prefer an independent, bot-generated PR for this fix despite #3227,
    leave a one-line approve on this issue and the spec will be regenerated.

    Automated by github-manager-bot

  5. aiyili commented on Sep 24, 2026

    @aiyili
    Contributor

    Following up on the 72 h stand-down: the fix is already in flight as #3227 (#3227), still open and ready for review when a maintainer has a moment.

    Scope, matching the evaluation:

    • createIfNotExist=false path: the unconditional ensureVersionColumn() is replaced by a read-only verifyVersionColumnExists() -- an INFORMATION_SCHEMA.COLUMNS check mirroring MysqlAgentStateStore, throwing IllegalStateException that quotes the exact ALTER TABLE ... ADD COLUMN IF NOT EXISTS version DDL to run via the user's own migration process.
    • ensureVersionColumn() stays under createIfNotExist=true (the DEFAULT 1 backfill value is preserved).
    • Regression: the false path asserts prepareStatement(startsWith("ALTER")) is never invoked (the previous test only guarded createStatement(), which the prepareStatement-based DDL bypassed).

    Happy to rebase onto current main before merge if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/ext/memoryarea/extensionsbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions