Class PostgresAgentStateStore

java.lang.Object
io.agentscope.extensions.postgresql.state.PostgresAgentStateStore
All Implemented Interfaces:
io.agentscope.core.state.AgentStateStore

public class PostgresAgentStateStore extends Object implements io.agentscope.core.state.AgentStateStore
PostgreSQL database-based AgentStateStore implementation.

Stores session state in PostgreSQL tables with the following structure:

 CREATE TABLE IF NOT EXISTS "agentscope"."agentscope_sessions" (
     session_id VARCHAR(255) NOT NULL,
     state_key  VARCHAR(255) NOT NULL,
     item_index INT          NOT NULL DEFAULT 0,
     state_data TEXT         NOT NULL,
     created_at TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
     updated_at TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (session_id, state_key, item_index)
 );
 
  • Single state: stored as JSON with item_index = 0
  • List state: each item stored in a separate row with item_index = 0, 1, 2, ...
  • Constructor Details

    • PostgresAgentStateStore

      public PostgresAgentStateStore(DataSource dataSource)
      Creates a PostgresAgentStateStore with default settings.

      Uses default schema (agentscope) and table (agentscope_sessions) and does NOT auto-create the schema or table.

    • PostgresAgentStateStore

      public PostgresAgentStateStore(DataSource dataSource, boolean createIfNotExist)
      Creates a PostgresAgentStateStore with optional auto-creation.

      Uses default schema and table. If createIfNotExist is true, the schema and table are created automatically if they don't exist.

    • PostgresAgentStateStore

      public PostgresAgentStateStore(DataSource dataSource, String schemaName, String tableName, boolean createIfNotExist)
      Creates a PostgresAgentStateStore with custom schema/table and optional auto-creation.
  • Method Details

    • supportsVersioning

      public boolean supportsVersioning()
      Specified by:
      supportsVersioning in interface io.agentscope.core.state.AgentStateStore
    • save

      public void save(String userId, String sessionId, String key, io.agentscope.core.state.State value)
      Specified by:
      save in interface io.agentscope.core.state.AgentStateStore
    • getVersioned

      public <T extends io.agentscope.core.state.State> io.agentscope.core.state.VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
      Specified by:
      getVersioned in interface io.agentscope.core.state.AgentStateStore
    • saveIfVersion

      public long saveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion)
      Specified by:
      saveIfVersion in interface io.agentscope.core.state.AgentStateStore
    • save

      public void save(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values)
      Specified by:
      save in interface io.agentscope.core.state.AgentStateStore
    • get

      public <T extends io.agentscope.core.state.State> Optional<T> get(String userId, String sessionId, String key, Class<T> type)
      Specified by:
      get in interface io.agentscope.core.state.AgentStateStore
    • getList

      public <T extends io.agentscope.core.state.State> List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
      Specified by:
      getList in interface io.agentscope.core.state.AgentStateStore
    • exists

      public boolean exists(String userId, String sessionId)
      Specified by:
      exists in interface io.agentscope.core.state.AgentStateStore
    • delete

      public void delete(String userId, String sessionId)
      Specified by:
      delete in interface io.agentscope.core.state.AgentStateStore
    • listSessionIds

      public Set<String> listSessionIds(String userId)
      Specified by:
      listSessionIds in interface io.agentscope.core.state.AgentStateStore
    • close

      public void close()
      Specified by:
      close in interface io.agentscope.core.state.AgentStateStore
    • truncateAllSessions

      public int truncateAllSessions()
      Truncate session table from the database (for testing or cleanup).

      This method clears all session records by executing a TRUNCATE TABLE statement on the sessions table. TRUNCATE is faster than DELETE as it resets the table without logging individual row deletions and reclaims storage space immediately.

      Note: In PostgreSQL, TRUNCATE TABLE is DDL and triggers an implicit commit. For that reason, this method executes the statement directly instead of routing it through executeInWriteTransaction(Connection, SqlOperation).

      Returns:
      typically 0 if successful
    • getSchemaName

      public String getSchemaName()
    • getTableName

      public String getTableName()
    • getDataSource

      public DataSource getDataSource()
    • validateSessionId

      protected void validateSessionId(String sessionId)
    • builder

      public static PostgresAgentStateStore.Builder builder(DataSource dataSource)